1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2013, Texas Instruments, Incorporated
11 #include <asm/cache.h>
13 #include <asm/arch/omap.h>
19 #include <asm/omap_gpio.h>
20 #include <asm/omap_common.h>
21 #include <asm/ti-common/ti-edma3.h>
22 #include <linux/err.h>
23 #include <linux/kernel.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /* ti qpsi register bit masks */
30 #define QSPI_TIMEOUT 2000000
31 #define QSPI_FCLK 192000000
32 #define QSPI_DRA7XX_FCLK 76800000
33 #define QSPI_WLEN_MAX_BITS 128
34 #define QSPI_WLEN_MAX_BYTES (QSPI_WLEN_MAX_BITS >> 3)
35 #define QSPI_WLEN_MASK QSPI_WLEN(QSPI_WLEN_MAX_BITS)
37 #define QSPI_CLK_EN BIT(31)
38 #define QSPI_CLK_DIV_MAX 0xffff
40 #define QSPI_EN_CS(n) (n << 28)
41 #define QSPI_WLEN(n) ((n-1) << 19)
42 #define QSPI_3_PIN BIT(18)
43 #define QSPI_RD_SNGL BIT(16)
44 #define QSPI_WR_SNGL (2 << 16)
45 #define QSPI_INVAL (4 << 16)
46 #define QSPI_RD_QUAD (7 << 16)
48 #define QSPI_CKPHA(n) (1 << (2 + n*8))
49 #define QSPI_CSPOL(n) (1 << (1 + n*8))
50 #define QSPI_CKPOL(n) (1 << (n*8))
52 #define QSPI_WC BIT(1)
53 #define QSPI_BUSY BIT(0)
54 #define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY)
55 #define QSPI_XFER_DONE QSPI_WC
56 #define MM_SWITCH 0x01
57 #define MEM_CS(cs) ((cs + 1) << 8)
58 #define MEM_CS_UNSELECT 0xfffff8ff
60 #define QSPI_SETUP0_READ_NORMAL (0x0 << 12)
61 #define QSPI_SETUP0_READ_DUAL (0x1 << 12)
62 #define QSPI_SETUP0_READ_QUAD (0x3 << 12)
63 #define QSPI_SETUP0_ADDR_SHIFT (8)
64 #define QSPI_SETUP0_DBITS_SHIFT (10)
66 #define TI_QSPI_SETUP_REG(priv, cs) (&(priv)->base->setup0 + (cs))
68 /* ti qspi register set */
101 struct ti_qspi_regs *base;
109 static int ti_qspi_set_speed(struct udevice *bus, uint hz)
111 struct ti_qspi_priv *priv = dev_get_priv(bus);
117 clk_div = DIV_ROUND_UP(priv->fclk, hz) - 1;
119 /* truncate clk_div value to QSPI_CLK_DIV_MAX */
120 if (clk_div > QSPI_CLK_DIV_MAX)
121 clk_div = QSPI_CLK_DIV_MAX;
123 debug("ti_spi_set_speed: hz: %d, clock divider %d\n", hz, clk_div);
126 writel(readl(&priv->base->clk_ctrl) & ~QSPI_CLK_EN,
127 &priv->base->clk_ctrl);
128 /* enable SCLK and program the clk divider */
129 writel(QSPI_CLK_EN | clk_div, &priv->base->clk_ctrl);
134 static void ti_qspi_cs_deactivate(struct ti_qspi_priv *priv)
136 writel(priv->cmd | QSPI_INVAL, &priv->base->cmd);
137 /* dummy readl to ensure bus sync */
138 readl(&priv->base->cmd);
141 static void ti_qspi_ctrl_mode_mmap(void *ctrl_mod_mmap, int cs, bool enable)
145 val = readl(ctrl_mod_mmap);
149 val &= MEM_CS_UNSELECT;
150 writel(val, ctrl_mod_mmap);
153 static int ti_qspi_xfer(struct udevice *dev, unsigned int bitlen,
154 const void *dout, void *din, unsigned long flags)
156 struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
157 struct ti_qspi_priv *priv;
159 uint words = bitlen >> 3; /* fixed 8-bit word length */
160 const uchar *txp = dout;
164 unsigned int cs = slave->cs;
167 priv = dev_get_priv(bus);
169 if (cs > priv->num_cs) {
170 debug("invalid qspi chip select\n");
178 debug("spi_xfer: Non byte aligned SPI transfer\n");
182 /* Setup command reg */
184 priv->cmd |= QSPI_WLEN(8);
185 priv->cmd |= QSPI_EN_CS(cs);
186 if (priv->mode & SPI_3WIRE)
187 priv->cmd |= QSPI_3_PIN;
196 if (words >= QSPI_WLEN_MAX_BYTES) {
197 u32 *txbuf = (u32 *)txp;
200 data = cpu_to_be32(*txbuf++);
201 writel(data, &priv->base->data3);
202 data = cpu_to_be32(*txbuf++);
203 writel(data, &priv->base->data2);
204 data = cpu_to_be32(*txbuf++);
205 writel(data, &priv->base->data1);
206 data = cpu_to_be32(*txbuf++);
207 writel(data, &priv->base->data);
208 cmd &= ~QSPI_WLEN_MASK;
209 cmd |= QSPI_WLEN(QSPI_WLEN_MAX_BITS);
210 xfer_len = QSPI_WLEN_MAX_BYTES;
212 writeb(*txp, &priv->base->data);
215 debug("tx cmd %08x dc %08x\n",
216 cmd | QSPI_WR_SNGL, priv->dc);
217 writel(cmd | QSPI_WR_SNGL, &priv->base->cmd);
218 status = readl(&priv->base->status);
219 timeout = QSPI_TIMEOUT;
220 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
222 printf("spi_xfer: TX timeout!\n");
225 status = readl(&priv->base->status);
228 debug("tx done, status %08x\n", status);
231 debug("rx cmd %08x dc %08x\n",
232 ((u32)(priv->cmd | QSPI_RD_SNGL)), priv->dc);
233 writel(priv->cmd | QSPI_RD_SNGL, &priv->base->cmd);
234 status = readl(&priv->base->status);
235 timeout = QSPI_TIMEOUT;
236 while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) {
238 printf("spi_xfer: RX timeout!\n");
241 status = readl(&priv->base->status);
243 *rxp++ = readl(&priv->base->data);
245 debug("rx done, status %08x, read %02x\n",
251 /* Terminate frame */
252 if (flags & SPI_XFER_END)
253 ti_qspi_cs_deactivate(priv);
258 /* TODO: control from sf layer to here through dm-spi */
259 static void ti_qspi_copy_mmap(void *data, void *offset, size_t len)
261 #if defined(CONFIG_TI_EDMA3) && !defined(CONFIG_DMA)
262 unsigned int addr = (unsigned int) (data);
263 unsigned int edma_slot_num = 1;
265 /* Invalidate the area, so no writeback into the RAM races with DMA */
266 invalidate_dcache_range(addr, addr + roundup(len, ARCH_DMA_MINALIGN));
268 /* enable edma3 clocks */
269 enable_edma3_clocks();
271 /* Call edma3 api to do actual DMA transfer */
272 edma3_transfer(EDMA3_BASE, edma_slot_num, data, offset, len);
274 /* disable edma3 clocks */
275 disable_edma3_clocks();
277 memcpy_fromio(data, offset, len);
280 *((unsigned int *)offset) += len;
283 static void ti_qspi_setup_mmap_read(struct ti_qspi_priv *priv, int cs,
284 u8 opcode, u8 data_nbits, u8 addr_width,
289 switch (data_nbits) {
291 memval |= QSPI_SETUP0_READ_QUAD;
294 memval |= QSPI_SETUP0_READ_DUAL;
297 memval |= QSPI_SETUP0_READ_NORMAL;
301 memval |= ((addr_width - 1) << QSPI_SETUP0_ADDR_SHIFT |
302 dummy_bytes << QSPI_SETUP0_DBITS_SHIFT);
304 writel(memval, TI_QSPI_SETUP_REG(priv, cs));
307 static int ti_qspi_set_mode(struct udevice *bus, uint mode)
309 struct ti_qspi_priv *priv = dev_get_priv(bus);
313 priv->dc |= QSPI_CKPHA(0);
315 priv->dc |= QSPI_CKPOL(0);
316 if (mode & SPI_CS_HIGH)
317 priv->dc |= QSPI_CSPOL(0);
322 static int ti_qspi_exec_mem_op(struct spi_slave *slave,
323 const struct spi_mem_op *op)
325 struct dm_spi_slave_platdata *slave_plat;
326 struct ti_qspi_priv *priv;
331 bus = slave->dev->parent;
332 priv = dev_get_priv(bus);
333 slave_plat = dev_get_parent_platdata(slave->dev);
335 /* Only optimize read path. */
336 if (!op->data.nbytes || op->data.dir != SPI_MEM_DATA_IN ||
337 !op->addr.nbytes || op->addr.nbytes > 4)
340 /* Address exceeds MMIO window size, fall back to regular mode. */
342 if (from + op->data.nbytes > priv->mmap_size)
345 ti_qspi_setup_mmap_read(priv, slave_plat->cs, op->cmd.opcode,
346 op->data.buswidth, op->addr.nbytes,
349 ti_qspi_copy_mmap((void *)op->data.buf.in,
350 (void *)priv->memory_map + from, op->data.nbytes);
355 static int ti_qspi_claim_bus(struct udevice *dev)
357 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
358 struct ti_qspi_priv *priv;
362 priv = dev_get_priv(bus);
364 if (slave_plat->cs > priv->num_cs) {
365 debug("invalid qspi chip select\n");
369 writel(MM_SWITCH, &priv->base->memswitch);
370 if (priv->ctrl_mod_mmap)
371 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap,
372 slave_plat->cs, true);
374 writel(priv->dc, &priv->base->dc);
375 writel(0, &priv->base->cmd);
376 writel(0, &priv->base->data);
378 priv->dc <<= slave_plat->cs * 8;
379 writel(priv->dc, &priv->base->dc);
384 static int ti_qspi_release_bus(struct udevice *dev)
386 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
387 struct ti_qspi_priv *priv;
391 priv = dev_get_priv(bus);
393 writel(~MM_SWITCH, &priv->base->memswitch);
394 if (priv->ctrl_mod_mmap)
395 ti_qspi_ctrl_mode_mmap(priv->ctrl_mod_mmap,
396 slave_plat->cs, false);
398 writel(0, &priv->base->dc);
399 writel(0, &priv->base->cmd);
400 writel(0, &priv->base->data);
401 writel(0, TI_QSPI_SETUP_REG(priv, slave_plat->cs));
406 static int ti_qspi_probe(struct udevice *bus)
408 struct ti_qspi_priv *priv = dev_get_priv(bus);
410 priv->fclk = dev_get_driver_data(bus);
415 static void *map_syscon_chipselects(struct udevice *bus)
417 #if CONFIG_IS_ENABLED(SYSCON)
418 struct udevice *syscon;
419 struct regmap *regmap;
423 err = uclass_get_device_by_phandle(UCLASS_SYSCON, bus,
424 "syscon-chipselects", &syscon);
426 debug("%s: unable to find syscon device (%d)\n", __func__,
431 regmap = syscon_get_regmap(syscon);
432 if (IS_ERR(regmap)) {
433 debug("%s: unable to find regmap (%ld)\n", __func__,
438 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(bus),
439 "syscon-chipselects", &len);
440 if (len < 2*sizeof(fdt32_t)) {
441 debug("%s: offset not available\n", __func__);
445 return fdtdec_get_number(cell + 1, 1) + regmap_get_range(regmap, 0);
448 addr = devfdt_get_addr_index(bus, 2);
449 return (addr == FDT_ADDR_T_NONE) ? NULL :
450 map_physmem(addr, 0, MAP_NOCACHE);
454 static int ti_qspi_ofdata_to_platdata(struct udevice *bus)
456 struct ti_qspi_priv *priv = dev_get_priv(bus);
457 const void *blob = gd->fdt_blob;
458 int node = dev_of_offset(bus);
459 fdt_addr_t mmap_addr;
460 fdt_addr_t mmap_size;
462 priv->ctrl_mod_mmap = map_syscon_chipselects(bus);
463 priv->base = map_physmem(devfdt_get_addr(bus),
464 sizeof(struct ti_qspi_regs), MAP_NOCACHE);
465 mmap_addr = devfdt_get_addr_size_index(bus, 1, &mmap_size);
466 priv->memory_map = map_physmem(mmap_addr, mmap_size, MAP_NOCACHE);
467 priv->mmap_size = mmap_size;
469 priv->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", -1);
470 if (priv->max_hz < 0) {
471 debug("Error: Max frequency missing\n");
474 priv->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
476 debug("%s: regs=<0x%x>, max-frequency=%d\n", __func__,
477 (int)priv->base, priv->max_hz);
482 static const struct spi_controller_mem_ops ti_qspi_mem_ops = {
483 .exec_op = ti_qspi_exec_mem_op,
486 static const struct dm_spi_ops ti_qspi_ops = {
487 .claim_bus = ti_qspi_claim_bus,
488 .release_bus = ti_qspi_release_bus,
489 .xfer = ti_qspi_xfer,
490 .set_speed = ti_qspi_set_speed,
491 .set_mode = ti_qspi_set_mode,
492 .mem_ops = &ti_qspi_mem_ops,
495 static const struct udevice_id ti_qspi_ids[] = {
496 { .compatible = "ti,dra7xxx-qspi", .data = QSPI_DRA7XX_FCLK},
497 { .compatible = "ti,am4372-qspi", .data = QSPI_FCLK},
501 U_BOOT_DRIVER(ti_qspi) = {
504 .of_match = ti_qspi_ids,
506 .ofdata_to_platdata = ti_qspi_ofdata_to_platdata,
507 .priv_auto_alloc_size = sizeof(struct ti_qspi_priv),
508 .probe = ti_qspi_probe,