1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2013, Texas Instruments, Incorporated
10 #include <asm/arch/omap.h>
15 #include <asm/omap_gpio.h>
16 #include <asm/omap_common.h>
17 #include <asm/ti-common/ti-edma3.h>
18 #include <linux/kernel.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 /* ti qpsi register bit masks */
25 #define QSPI_TIMEOUT 2000000
26 #define QSPI_FCLK 192000000
27 #define QSPI_DRA7XX_FCLK 76800000
28 #define QSPI_WLEN_MAX_BITS 128
29 #define QSPI_WLEN_MAX_BYTES (QSPI_WLEN_MAX_BITS >> 3)
30 #define QSPI_WLEN_MASK QSPI_WLEN(QSPI_WLEN_MAX_BITS)
32 #define QSPI_CLK_EN BIT(31)
33 #define QSPI_CLK_DIV_MAX 0xffff
35 #define QSPI_EN_CS(n) (n << 28)
36 #define QSPI_WLEN(n) ((n-1) << 19)
37 #define QSPI_3_PIN BIT(18)
38 #define QSPI_RD_SNGL BIT(16)
39 #define QSPI_WR_SNGL (2 << 16)
40 #define QSPI_INVAL (4 << 16)
41 #define QSPI_RD_QUAD (7 << 16)
43 #define QSPI_DD(m, n) (m << (3 + n*8))
44 #define QSPI_CKPHA(n) (1 << (2 + n*8))
45 #define QSPI_CSPOL(n) (1 << (1 + n*8))
46 #define QSPI_CKPOL(n) (1 << (n*8))
48 #define QSPI_WC BIT(1)
49 #define QSPI_BUSY BIT(0)
50 #define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY)
51 #define QSPI_XFER_DONE QSPI_WC
52 #define MM_SWITCH 0x01
53 #define MEM_CS(cs) ((cs + 1) << 8)
54 #define MEM_CS_UNSELECT 0xfffff8ff
55 #define MMAP_START_ADDR_DRA 0x5c000000
56 #define MMAP_START_ADDR_AM43x 0x30000000
57 #define CORE_CTRL_IO 0x4a002558
59 #define QSPI_CMD_READ (0x3 << 0)
60 #define QSPI_CMD_READ_DUAL (0x6b << 0)
61 #define QSPI_CMD_READ_QUAD (0x6c << 0)
62 #define QSPI_CMD_READ_FAST (0x0b << 0)
63 #define QSPI_SETUP0_NUM_A_BYTES (0x3 << 8)
64 #define QSPI_SETUP0_NUM_D_BYTES_NO_BITS (0x0 << 10)
65 #define QSPI_SETUP0_NUM_D_BYTES_8_BITS (0x1 << 10)
66 #define QSPI_SETUP0_READ_NORMAL (0x0 << 12)
67 #define QSPI_SETUP0_READ_DUAL (0x1 << 12)
68 #define QSPI_SETUP0_READ_QUAD (0x3 << 12)
69 #define QSPI_CMD_WRITE (0x12 << 16)
70 #define QSPI_NUM_DUMMY_BITS (0x0 << 24)
72 /* ti qspi register set */
100 struct ti_qspi_priv {
101 #ifndef CONFIG_DM_SPI
102 struct spi_slave slave;
108 struct ti_qspi_regs *base;
116 static void ti_spi_set_speed(struct ti_qspi_priv *priv, uint hz)
123 clk_div = DIV_ROUND_UP(priv->fclk, hz) - 1;
125 /* truncate clk_div value to QSPI_CLK_DIV_MAX */
126 if (clk_div > QSPI_CLK_DIV_MAX)
127 clk_div = QSPI_CLK_DIV_MAX;
129 debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div);
132 writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN,
133 &priv->base->clk_ctrl);
134 /* enable SCLK and program the clk divider */
135 writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl);
138 static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv)
140 writel(priv->cmd | QSPI_INVAL, &priv->base->cmd);
141 /* dummy readl to ensure bus sync */
142 readl(&priv->base->cmd);
145 static int __ti_qspi_set_mode(struct ti_qspi_priv *priv, unsigned int mode)
149 priv->dc |= QSPI_CKPHA(0);
151 priv->dc |= QSPI_CKPOL(0);
152 if (mode & SPI_CS_HIGH)
153 priv->dc |= QSPI_CSPOL(0);
158 static int __ti_qspi_claim_bus(struct ti_qspi_priv *priv, int cs)
160 writel(priv->dc, &priv->base->dc);
161 writel(0, &priv->base->cmd);
162 writel(0, &priv->base->data);
165 writel(priv->dc, &priv->base->dc);
170 static void __ti_qspi_release_bus(struct ti_qspi_priv *priv)
172 writel(0, &priv->base->dc);
173 writel(0, &priv->base->cmd);
174 writel(0, &priv->base->data);
177 static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable)
181 val = readl(ctrl_mod_mmap);
185 val &= MEM_CS_UNSELECT;
186 writel(val, ctrl_mod_mmap);
189 static int __ti_qspi_xfer(struct ti_qspi_priv *priv, unsigned int bitlen,
190 const void *dout, void *din, unsigned long flags,
193 uint words = bitlen >> 3; /* fixed 8-bit word length */
194 const uchar *txp = dout;
199 /* Setup mmap flags */
200 if (flags & SPI_XFER_MMAP) {
201 writel(MM_SWITCH, &priv->base->memswitch);
202 if (priv->ctrl_mod_mmap)
203 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap, cs, true);
205 } else if (flags & SPI_XFER_MMAP_END) {
206 writel(~MM_SWITCH, &priv->base->memswitch);
207 if (priv->ctrl_mod_mmap)
208 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap, cs, false);
216 debug("spi_xfer: Non byte aligned SPI transfer\n");
220 /* Setup command reg */
222 priv->cmd |= QSPI_WLEN(8);
223 priv->cmd |= QSPI_EN_CS(cs);
224 if (priv->mode & SPI_3WIRE)
225 priv->cmd |= QSPI_3_PIN;
234 if (words >= QSPI_WLEN_MAX_BYTES) {
235 u32 *txbuf = (u32 *)txp;
238 data = cpu_to_be32(*txbuf++);
239 writel(data, &priv->base->data3);
240 data = cpu_to_be32(*txbuf++);
241 writel(data, &priv->base->data2);
242 data = cpu_to_be32(*txbuf++);
243 writel(data, &priv->base->data1);
244 data = cpu_to_be32(*txbuf++);
245 writel(data, &priv->base->data);
246 cmd &= ~QSPI_WLEN_MASK;
247 cmd |= QSPI_WLEN(QSPI_WLEN_MAX_BITS);
248 xfer_len = QSPI_WLEN_MAX_BYTES;
250 writeb(*txp, &priv->base->data);
253 debug("tx cmd %08x dc %08x\n",
254 cmd | QSPI_WR_SNGL, priv->dc);
255 writel(cmd | QSPI_WR_SNGL, &priv->base->cmd);
256 status = readl(&priv->base->status);
257 timeout = QSPI_TIMEOUT;
258 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
260 printf("spi_xfer: TX timeout!\n");
263 status = readl(&priv->base->status);
266 debug("tx done, status %08x\n", status);
269 debug("rx cmd %08x dc %08x\n",
270 ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc);
271 writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd);
272 status = readl(&priv->base->status);
273 timeout = QSPI_TIMEOUT;
274 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
276 printf("spi_xfer: RX timeout!\n");
279 status = readl(&priv->base->status);
281 *rxp++ = readl(&priv->base->data);
283 debug("rx done, status %08x, read %02x\n",
289 /* Terminate frame */
290 if (flags & SPI_XFER_END)
291 ti_qspi_cs_deactivate(priv);
296 /* TODO: control from sf layer to here through dm-spi */
297 #if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA)
298 void spi_flash_copy_mmap(void *data, void *offset, size_t len)
300 unsigned int addr = (unsigned int) (data);
301 unsigned int edma_slot_num = 1;
303 /* Invalidate the area, so no writeback into the RAM races with DMA */
304 invalidate_dcache_range(addr, addr + roundup(len, ARCH_DMA_MINALIGN));
306 /* enable edma3 clocks */
307 enable_edma3_clocks();
309 /* Call edma3 api to do actual DMA transfer */
310 edma3_transfer(EDMA3_BASE, edma_slot_num, data, offset, len);
312 /* disable edma3 clocks */
313 disable_edma3_clocks();
315 *((unsigned int *)offset) += len;
319 #ifndef CONFIG_DM_SPI
321 static inline struct ti_qspi_priv *to_ti_qspi_priv(struct spi_slave *slave)
323 return container_of(slave, struct ti_qspi_priv, slave);
326 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
331 void spi_cs_activate(struct spi_slave *slave)
333 /* CS handled in xfer */
337 void spi_cs_deactivate(struct spi_slave *slave)
339 struct ti_qspi_priv *priv = to_ti_qspi_priv(slave);
340 ti_qspi_cs_deactivate(priv);
348 static void ti_spi_setup_spi_register(struct ti_qspi_priv *priv)
352 #ifdef CONFIG_QSPI_QUAD_SUPPORT
353 struct spi_slave *slave = &priv->slave;
354 memval |= (QSPI_CMD_READ_QUAD | QSPI_SETUP0_NUM_A_BYTES |
355 QSPI_SETUP0_NUM_D_BYTES_8_BITS |
356 QSPI_SETUP0_READ_QUAD | QSPI_CMD_WRITE |
357 QSPI_NUM_DUMMY_BITS);
358 slave->mode |= SPI_RX_QUAD;
360 memval |= QSPI_CMD_READ | QSPI_SETUP0_NUM_A_BYTES |
361 QSPI_SETUP0_NUM_D_BYTES_NO_BITS |
362 QSPI_SETUP0_READ_NORMAL | QSPI_CMD_WRITE |
366 writel(memval, &priv->base->setup0);
369 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
370 unsigned int max_hz, unsigned int mode)
372 struct ti_qspi_priv *priv;
375 gpio_request(CONFIG_QSPI_SEL_GPIO, "qspi_gpio");
376 gpio_direction_output(CONFIG_QSPI_SEL_GPIO, 1);
379 priv = spi_alloc_slave(struct ti_qspi_priv, bus, cs);
381 printf("SPI_error: Fail to allocate ti_qspi_priv\n");
385 priv->base = (struct ti_qspi_regs *)QSPI_BASE;
387 #if defined(CONFIG_DRA7XX)
388 priv->ctrl_mod_mmap = (void *)CORE_CTRL_IO;
389 priv->slave.memory_map = (void *)MMAP_START_ADDR_DRA;
390 priv->fclk = QSPI_DRA7XX_FCLK;
392 priv->slave.memory_map = (void *)MMAP_START_ADDR_AM43x;
393 priv->fclk = QSPI_FCLK;
396 ti_spi_set_speed(priv, max_hz);
398 #ifdef CONFIG_TI_SPI_MMAP
399 ti_spi_setup_spi_register(priv);
405 void spi_free_slave(struct spi_slave *slave)
407 struct ti_qspi_priv *priv = to_ti_qspi_priv(slave);
411 int spi_claim_bus(struct spi_slave *slave)
413 struct ti_qspi_priv *priv = to_ti_qspi_priv(slave);
415 debug("%s: bus:%i cs:%i\n", __func__, priv->slave.bus, priv->slave.cs);
416 __ti_qspi_set_mode(priv, priv->mode);
417 return __ti_qspi_claim_bus(priv, priv->slave.cs);
419 void spi_release_bus(struct spi_slave *slave)
421 struct ti_qspi_priv *priv = to_ti_qspi_priv(slave);
423 debug("%s: bus:%i cs:%i\n", __func__, priv->slave.bus, priv->slave.cs);
424 __ti_qspi_release_bus(priv);
427 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
428 void *din, unsigned long flags)
430 struct ti_qspi_priv *priv = to_ti_qspi_priv(slave);
432 debug("spi_xfer: bus:%i cs:%i bitlen:%i flags:%lx\n",
433 priv->slave.bus, priv->slave.cs, bitlen, flags);
434 return __ti_qspi_xfer(priv, bitlen, dout, din, flags, priv->slave.cs);
437 #else /* CONFIG_DM_SPI */
439 static void __ti_qspi_setup_memorymap(struct ti_qspi_priv *priv,
440 struct spi_slave *slave,
444 u32 mode = slave->mode & (SPI_RX_QUAD | SPI_RX_DUAL);
447 writel(0, &priv->base->setup0);
451 memval = QSPI_SETUP0_NUM_A_BYTES | QSPI_CMD_WRITE | QSPI_NUM_DUMMY_BITS;
455 memval |= QSPI_CMD_READ_QUAD;
456 memval |= QSPI_SETUP0_NUM_D_BYTES_8_BITS;
457 memval |= QSPI_SETUP0_READ_QUAD;
458 slave->mode |= SPI_RX_QUAD;
461 memval |= QSPI_CMD_READ_DUAL;
462 memval |= QSPI_SETUP0_NUM_D_BYTES_8_BITS;
463 memval |= QSPI_SETUP0_READ_DUAL;
466 memval |= QSPI_CMD_READ;
467 memval |= QSPI_SETUP0_NUM_D_BYTES_NO_BITS;
468 memval |= QSPI_SETUP0_READ_NORMAL;
472 writel(memval, &priv->base->setup0);
476 static int ti_qspi_set_speed(struct udevice *bus, uint max_hz)
478 struct ti_qspi_priv *priv = dev_get_priv(bus);
480 ti_spi_set_speed(priv, max_hz);
485 static int ti_qspi_set_mode(struct udevice *bus, uint mode)
487 struct ti_qspi_priv *priv = dev_get_priv(bus);
488 return __ti_qspi_set_mode(priv, mode);
491 static int ti_qspi_claim_bus(struct udevice *dev)
493 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
494 struct spi_slave *slave = dev_get_parent_priv(dev);
495 struct ti_qspi_priv *priv;
499 priv = dev_get_priv(bus);
501 if (slave_plat->cs > priv->num_cs) {
502 debug("invalid qspi chip select\n");
506 __ti_qspi_setup_memorymap(priv, slave, true);
508 return __ti_qspi_claim_bus(priv, slave_plat->cs);
511 static int ti_qspi_release_bus(struct udevice *dev)
513 struct spi_slave *slave = dev_get_parent_priv(dev);
514 struct ti_qspi_priv *priv;
518 priv = dev_get_priv(bus);
520 __ti_qspi_setup_memorymap(priv, slave, false);
521 __ti_qspi_release_bus(priv);
526 static int ti_qspi_xfer(struct udevice *dev, unsigned int bitlen,
527 const void *dout, void *din, unsigned long flags)
529 struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
530 struct ti_qspi_priv *priv;
534 priv = dev_get_priv(bus);
536 if (slave->cs > priv->num_cs) {
537 debug("invalid qspi chip select\n");
541 return __ti_qspi_xfer(priv, bitlen, dout, din, flags, slave->cs);
544 static int ti_qspi_probe(struct udevice *bus)
546 struct ti_qspi_priv *priv = dev_get_priv(bus);
548 priv->fclk = dev_get_driver_data(bus);
553 static void *map_syscon_chipselects(struct udevice *bus)
555 #if CONFIG_IS_ENABLED(SYSCON)
556 struct udevice *syscon;
557 struct regmap *regmap;
561 err = uclass_get_device_by_phandle(UCLASS_SYSCON, bus,
562 "syscon-chipselects", &syscon);
564 debug("%s: unable to find syscon device (%d)\n", __func__,
569 regmap = syscon_get_regmap(syscon);
570 if (IS_ERR(regmap)) {
571 debug("%s: unable to find regmap (%ld)\n", __func__,
576 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus),
577 "syscon-chipselects", &len);
578 if (len < 2*sizeof(fdt32_t)) {
579 debug("%s: offset not available\n", __func__);
583 return fdtdec_get_number(cell + 1, 1) + regmap_get_range(regmap, 0);
586 addr = devfdt_get_addr_index(bus, 2);
587 return (addr == FDT_ADDR_T_NONE) ? NULL :
588 map_physmem(addr, 0, MAP_NOCACHE);
592 static int ti_qspi_ofdata_to_platdata(struct udevice *bus)
594 struct ti_qspi_priv *priv = dev_get_priv(bus);
595 const void *blob = gd->fdt_blob;
596 int node = dev_of_offset(bus);
598 priv->ctrl_mod_mmap = map_syscon_chipselects(bus);
599 priv->base = map_physmem(devfdt_get_addr(bus),
600 sizeof(struct ti_qspi_regs), MAP_NOCACHE);
601 priv->memory_map = map_physmem(devfdt_get_addr_index(bus, 1), 0,
604 priv->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", -1);
605 if (priv->max_hz < 0) {
606 debug("Error: Max frequency missing\n");
609 priv->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
611 debug("%s: regs=<0x%x>, max-frequency=%d\n", __func__,
612 (int)priv->base, priv->max_hz);
617 static int ti_qspi_child_pre_probe(struct udevice *dev)
619 struct spi_slave *slave = dev_get_parent_priv(dev);
620 struct udevice *bus = dev_get_parent(dev);
621 struct ti_qspi_priv *priv = dev_get_priv(bus);
623 slave->memory_map = priv->memory_map;
627 static const struct dm_spi_ops ti_qspi_ops = {
628 .claim_bus = ti_qspi_claim_bus,
629 .release_bus = ti_qspi_release_bus,
630 .xfer = ti_qspi_xfer,
631 .set_speed = ti_qspi_set_speed,
632 .set_mode = ti_qspi_set_mode,
635 static const struct udevice_id ti_qspi_ids[] = {
636 { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK},
637 { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK},
641 U_BOOT_DRIVER(ti_qspi) = {
644 .of_match = ti_qspi_ids,
646 .ofdata_to_platdata = ti_qspi_ofdata_to_platdata,
647 .priv_auto_alloc_size = sizeof(struct ti_qspi_priv),
648 .probe = ti_qspi_probe,
649 .child_pre_probe = ti_qspi_child_pre_probe,
651 #endif /* CONFIG_DM_SPI */