1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Xilinx
5 * Xilinx ZynqMP Generic Quad-SPI(QSPI) controller driver(master mode only)
11 #include <asm/arch/sys_proto.h>
12 #include <asm/cache.h>
20 #include <ubi_uboot.h>
22 #include <dm/device_compat.h>
23 #include <linux/bitops.h>
24 #include <linux/err.h>
25 #include <linux/sizes.h>
26 #include <zynqmp_firmware.h>
28 #define GQSPI_GFIFO_STRT_MODE_MASK BIT(29)
29 #define GQSPI_CONFIG_MODE_EN_MASK (3 << 30)
30 #define GQSPI_CONFIG_DMA_MODE (2 << 30)
31 #define GQSPI_CONFIG_CPHA_MASK BIT(2)
32 #define GQSPI_CONFIG_CPOL_MASK BIT(1)
35 * QSPI Interrupt Registers bit Masks
37 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
40 #define GQSPI_IXR_TXNFULL_MASK 0x00000004 /* QSPI TX FIFO Overflow */
41 #define GQSPI_IXR_TXFULL_MASK 0x00000008 /* QSPI TX FIFO is full */
42 #define GQSPI_IXR_TXFIFOEMPTY_MASK 0x00000100 /* QSPI TX FIFO is Empty */
43 #define GQSPI_IXR_RXNEMTY_MASK 0x00000010 /* QSPI RX FIFO Not Empty */
44 #define GQSPI_IXR_GFEMTY_MASK 0x00000080 /* QSPI Generic FIFO Empty */
45 #define GQSPI_IXR_GFNFULL_MASK 0x00000200 /* QSPI GENFIFO not full */
46 #define GQSPI_IXR_ALL_MASK (GQSPI_IXR_TXNFULL_MASK | \
47 GQSPI_IXR_RXNEMTY_MASK)
50 * QSPI Enable Register bit Masks
52 * This register is used to enable or disable the QSPI controller
54 #define GQSPI_ENABLE_ENABLE_MASK 0x00000001 /* QSPI Enable Bit Mask */
56 #define GQSPI_GFIFO_LOW_BUS BIT(14)
57 #define GQSPI_GFIFO_CS_LOWER BIT(12)
58 #define GQSPI_GFIFO_UP_BUS BIT(15)
59 #define GQSPI_GFIFO_CS_UPPER BIT(13)
60 #define GQSPI_SPI_MODE_QSPI (3 << 10)
61 #define GQSPI_SPI_MODE_SPI BIT(10)
62 #define GQSPI_SPI_MODE_DUAL_SPI (2 << 10)
63 #define GQSPI_IMD_DATA_CS_ASSERT 5
64 #define GQSPI_IMD_DATA_CS_DEASSERT 5
65 #define GQSPI_GFIFO_TX BIT(16)
66 #define GQSPI_GFIFO_RX BIT(17)
67 #define GQSPI_GFIFO_STRIPE_MASK BIT(18)
68 #define GQSPI_GFIFO_IMD_MASK 0xFF
69 #define GQSPI_GFIFO_EXP_MASK BIT(9)
70 #define GQSPI_GFIFO_DATA_XFR_MASK BIT(8)
71 #define GQSPI_STRT_GEN_FIFO BIT(28)
72 #define GQSPI_GEN_FIFO_STRT_MOD BIT(29)
73 #define GQSPI_GFIFO_WP_HOLD BIT(19)
74 #define GQSPI_BAUD_DIV_MASK (7 << 3)
75 #define GQSPI_DFLT_BAUD_RATE_DIV BIT(3)
76 #define GQSPI_GFIFO_ALL_INT_MASK 0xFBE
77 #define GQSPI_DMA_DST_I_STS_DONE BIT(1)
78 #define GQSPI_DMA_DST_I_STS_MASK 0xFE
81 #define GQSPI_GFIFO_SELECT BIT(0)
82 #define GQSPI_FIFO_THRESHOLD 1
83 #define GQSPI_GENFIFO_THRESHOLD 31
85 #define SPI_XFER_ON_BOTH 0
86 #define SPI_XFER_ON_LOWER 1
87 #define SPI_XFER_ON_UPPER 2
89 #define GQSPI_DMA_ALIGN 0x4
90 #define GQSPI_MAX_BAUD_RATE_VAL 7
91 #define GQSPI_DFLT_BAUD_RATE_VAL 2
93 #define GQSPI_TIMEOUT 100000000
95 #define GQSPI_BAUD_DIV_SHIFT 2
96 #define GQSPI_LPBK_DLY_ADJ_LPBK_SHIFT 5
97 #define GQSPI_LPBK_DLY_ADJ_DLY_1 0x1
98 #define GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT 3
99 #define GQSPI_LPBK_DLY_ADJ_DLY_0 0x3
100 #define GQSPI_USE_DATA_DLY 0x1
101 #define GQSPI_USE_DATA_DLY_SHIFT 31
102 #define GQSPI_DATA_DLY_ADJ_VALUE 0x2
103 #define GQSPI_DATA_DLY_ADJ_SHIFT 28
104 #define TAP_DLY_BYPASS_LQSPI_RX_VALUE 0x1
105 #define TAP_DLY_BYPASS_LQSPI_RX_SHIFT 2
106 #define GQSPI_DATA_DLY_ADJ_OFST 0x000001F8
107 #define IOU_TAPDLY_BYPASS_OFST !(IS_ENABLED(CONFIG_ARCH_VERSAL) || \
108 IS_ENABLED(CONFIG_ARCH_VERSAL_NET)) ? \
109 0xFF180390 : 0xF103003C
110 #define GQSPI_LPBK_DLY_ADJ_LPBK_MASK 0x00000020
111 #define GQSPI_FREQ_37_5MHZ 37500000
112 #define GQSPI_FREQ_40MHZ 40000000
113 #define GQSPI_FREQ_100MHZ 100000000
114 #define GQSPI_FREQ_150MHZ 150000000
115 #define IOU_TAPDLY_BYPASS_MASK 0x7
117 #define GQSPI_REG_OFFSET 0x100
118 #define GQSPI_DMA_REG_OFFSET 0x800
120 /* QSPI register offsets */
121 struct zynqmp_qspi_regs {
122 u32 confr; /* 0x00 */
125 u32 idisr; /* 0x0C */
126 u32 imaskr; /* 0x10 */
129 u32 txd0r; /* 0x1C */
132 u32 txftr; /* 0x28 */
133 u32 rxftr; /* 0x2C */
134 u32 gpior; /* 0x30 */
135 u32 reserved0; /* 0x34 */
136 u32 lpbkdly; /* 0x38 */
137 u32 reserved1; /* 0x3C */
138 u32 genfifo; /* 0x40 */
139 u32 gqspisel; /* 0x44 */
140 u32 reserved2; /* 0x48 */
141 u32 gqfifoctrl; /* 0x4C */
142 u32 gqfthr; /* 0x50 */
143 u32 gqpollcfg; /* 0x54 */
144 u32 gqpollto; /* 0x58 */
145 u32 gqxfersts; /* 0x5C */
146 u32 gqfifosnap; /* 0x60 */
147 u32 gqrxcpy; /* 0x64 */
148 u32 reserved3[36]; /* 0x68 */
149 u32 gqspidlyadj; /* 0xF8 */
152 struct zynqmp_qspi_dma_regs {
153 u32 dmadst; /* 0x00 */
154 u32 dmasize; /* 0x04 */
155 u32 dmasts; /* 0x08 */
156 u32 dmactrl; /* 0x0C */
157 u32 reserved0; /* 0x10 */
158 u32 dmaisr; /* 0x14 */
159 u32 dmaier; /* 0x18 */
160 u32 dmaidr; /* 0x1C */
161 u32 dmaimr; /* 0x20 */
162 u32 dmactrl2; /* 0x24 */
163 u32 dmadstmsb; /* 0x28 */
166 struct zynqmp_qspi_plat {
167 struct zynqmp_qspi_regs *regs;
168 struct zynqmp_qspi_dma_regs *dma_regs;
171 unsigned int io_mode;
174 struct zynqmp_qspi_priv {
175 struct zynqmp_qspi_regs *regs;
176 struct zynqmp_qspi_dma_regs *dma_regs;
180 unsigned int io_mode;
181 int bytes_to_transfer;
182 int bytes_to_receive;
183 const struct spi_mem_op *op;
186 __weak int zynqmp_mmio_write(const u32 address, const u32 mask, const u32 value)
191 static int zynqmp_qspi_of_to_plat(struct udevice *bus)
193 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
195 debug("%s\n", __func__);
197 plat->regs = (struct zynqmp_qspi_regs *)(dev_read_addr(bus) +
199 plat->dma_regs = (struct zynqmp_qspi_dma_regs *)
200 (dev_read_addr(bus) + GQSPI_DMA_REG_OFFSET);
202 plat->io_mode = dev_read_bool(bus, "has-io-mode");
207 static void zynqmp_qspi_init_hw(struct zynqmp_qspi_priv *priv)
210 struct zynqmp_qspi_regs *regs = priv->regs;
212 writel(GQSPI_GFIFO_SELECT, ®s->gqspisel);
213 writel(GQSPI_GFIFO_ALL_INT_MASK, ®s->idisr);
214 writel(GQSPI_FIFO_THRESHOLD, ®s->txftr);
215 writel(GQSPI_FIFO_THRESHOLD, ®s->rxftr);
216 writel(GQSPI_GENFIFO_THRESHOLD, ®s->gqfthr);
217 writel(GQSPI_GFIFO_ALL_INT_MASK, ®s->isr);
218 writel(~GQSPI_ENABLE_ENABLE_MASK, ®s->enbr);
220 config_reg = readl(®s->confr);
221 config_reg &= ~(GQSPI_GFIFO_STRT_MODE_MASK |
222 GQSPI_CONFIG_MODE_EN_MASK);
223 config_reg |= GQSPI_GFIFO_WP_HOLD | GQSPI_DFLT_BAUD_RATE_DIV;
224 config_reg |= GQSPI_GFIFO_STRT_MODE_MASK;
226 config_reg |= GQSPI_CONFIG_DMA_MODE;
228 writel(config_reg, ®s->confr);
230 writel(GQSPI_ENABLE_ENABLE_MASK, ®s->enbr);
233 static u32 zynqmp_qspi_bus_select(struct zynqmp_qspi_priv *priv)
235 u32 gqspi_fifo_reg = 0;
237 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS |
238 GQSPI_GFIFO_CS_LOWER;
240 return gqspi_fifo_reg;
243 static u32 zynqmp_qspi_genfifo_mode(u8 buswidth)
247 return GQSPI_SPI_MODE_SPI;
249 return GQSPI_SPI_MODE_DUAL_SPI;
251 return GQSPI_SPI_MODE_QSPI;
253 debug("Unsupported bus width %u\n", buswidth);
254 return GQSPI_SPI_MODE_SPI;
258 static void zynqmp_qspi_fill_gen_fifo(struct zynqmp_qspi_priv *priv,
261 struct zynqmp_qspi_regs *regs = priv->regs;
265 writel(gqspi_fifo_reg, ®s->genfifo);
267 config_reg = readl(®s->confr);
268 /* Manual start if needed */
269 config_reg |= GQSPI_STRT_GEN_FIFO;
270 writel(config_reg, ®s->confr);
272 /* Enable interrupts */
273 ier = readl(®s->ier);
274 ier |= GQSPI_IXR_GFEMTY_MASK;
275 writel(ier, ®s->ier);
277 /* Wait until the gen fifo is empty to write the new command */
278 ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_GFEMTY_MASK, 1,
281 printf("%s Timeout\n", __func__);
285 static void zynqmp_qspi_chipselect(struct zynqmp_qspi_priv *priv, int is_on)
287 u32 gqspi_fifo_reg = 0;
290 gqspi_fifo_reg = zynqmp_qspi_bus_select(priv);
291 gqspi_fifo_reg |= GQSPI_SPI_MODE_SPI |
292 GQSPI_IMD_DATA_CS_ASSERT;
294 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS;
295 gqspi_fifo_reg |= GQSPI_IMD_DATA_CS_DEASSERT;
298 debug("GFIFO_CMD_CS: 0x%x\n", gqspi_fifo_reg);
300 zynqmp_qspi_fill_gen_fifo(priv, gqspi_fifo_reg);
303 static void zynqmp_qspi_set_tapdelay(struct udevice *bus, u32 baudrateval)
305 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
306 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
307 struct zynqmp_qspi_regs *regs = priv->regs;
308 u32 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate;
311 clk_rate = plat->frequency;
312 reqhz = (clk_rate / (GQSPI_BAUD_DIV_SHIFT << baudrateval));
314 debug("%s, req_hz:%d, clk_rate:%d, baudrateval:%d\n",
315 __func__, reqhz, clk_rate, baudrateval);
317 if (!(IS_ENABLED(CONFIG_ARCH_VERSAL) ||
318 IS_ENABLED(CONFIG_ARCH_VERSAL_NET))) {
319 if (reqhz <= GQSPI_FREQ_40MHZ) {
320 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
321 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
322 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
323 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
324 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
325 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK;
326 datadlyadj = (GQSPI_USE_DATA_DLY <<
327 GQSPI_USE_DATA_DLY_SHIFT) |
328 (GQSPI_DATA_DLY_ADJ_VALUE <<
329 GQSPI_DATA_DLY_ADJ_SHIFT);
330 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
331 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK |
332 GQSPI_LPBK_DLY_ADJ_DLY_0;
334 zynqmp_mmio_write(IOU_TAPDLY_BYPASS_OFST,
335 IOU_TAPDLY_BYPASS_MASK, tapdlybypass);
337 if (reqhz <= GQSPI_FREQ_37_5MHZ) {
338 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
339 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
340 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
341 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
342 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
343 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK;
344 datadlyadj = GQSPI_USE_DATA_DLY <<
345 GQSPI_USE_DATA_DLY_SHIFT;
346 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
347 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK |
348 (GQSPI_LPBK_DLY_ADJ_DLY_1 <<
349 GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT);
351 writel(tapdlybypass, IOU_TAPDLY_BYPASS_OFST);
353 writel(lpbkdlyadj, ®s->lpbkdly);
354 writel(datadlyadj, ®s->gqspidlyadj);
357 static int zynqmp_qspi_set_speed(struct udevice *bus, uint speed)
359 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
360 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
361 struct zynqmp_qspi_regs *regs = priv->regs;
363 u8 baud_rate_val = 0;
365 debug("%s\n", __func__);
366 if (speed > plat->frequency)
367 speed = plat->frequency;
369 if (plat->speed_hz != speed) {
370 /* Set the clock frequency */
371 /* If speed == 0, default to lowest speed */
372 while ((baud_rate_val < 8) &&
374 (2 << baud_rate_val)) > speed))
377 if (baud_rate_val > GQSPI_MAX_BAUD_RATE_VAL)
378 baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL;
380 plat->speed_hz = plat->frequency / (2 << baud_rate_val);
382 confr = readl(®s->confr);
383 confr &= ~GQSPI_BAUD_DIV_MASK;
384 confr |= (baud_rate_val << 3);
385 writel(confr, ®s->confr);
386 zynqmp_qspi_set_tapdelay(bus, baud_rate_val);
388 debug("regs=%p, speed=%d\n", priv->regs, plat->speed_hz);
394 static int zynqmp_qspi_probe(struct udevice *bus)
396 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
397 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
402 debug("%s: bus:%p, priv:%p\n", __func__, bus, priv);
404 priv->regs = plat->regs;
405 priv->dma_regs = plat->dma_regs;
406 priv->io_mode = plat->io_mode;
408 ret = clk_get_by_index(bus, 0, &clk);
410 dev_err(bus, "failed to get clock\n");
414 clock = clk_get_rate(&clk);
415 if (IS_ERR_VALUE(clock)) {
416 dev_err(bus, "failed to get rate\n");
419 debug("%s: CLK %ld\n", __func__, clock);
421 ret = clk_enable(&clk);
423 dev_err(bus, "failed to enable clock\n");
426 plat->frequency = clock;
427 plat->speed_hz = plat->frequency / 2;
429 /* init the zynq spi hw */
430 zynqmp_qspi_init_hw(priv);
435 static int zynqmp_qspi_set_mode(struct udevice *bus, uint mode)
437 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
438 struct zynqmp_qspi_regs *regs = priv->regs;
441 debug("%s\n", __func__);
442 /* Set the SPI Clock phase and polarities */
443 confr = readl(®s->confr);
444 confr &= ~(GQSPI_CONFIG_CPHA_MASK | GQSPI_CONFIG_CPOL_MASK);
447 confr |= GQSPI_CONFIG_CPHA_MASK;
449 confr |= GQSPI_CONFIG_CPOL_MASK;
451 writel(confr, ®s->confr);
456 static int zynqmp_qspi_fill_tx_fifo(struct zynqmp_qspi_priv *priv, u32 size)
460 struct zynqmp_qspi_regs *regs = priv->regs;
461 u32 *buf = (u32 *)priv->tx_buf;
464 debug("TxFIFO: 0x%x, size: 0x%x\n", readl(®s->isr),
468 ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_TXNFULL_MASK, 1,
471 printf("%s: Timeout\n", __func__);
476 writel(*buf, ®s->txd0r);
484 data |= GENMASK(31, 8);
487 data = *((u16 *)buf);
489 data |= GENMASK(31, 16);
494 data |= GENMASK(31, 24);
497 writel(data, ®s->txd0r);
502 ret = wait_for_bit_le32(®s->isr, GQSPI_IXR_TXFIFOEMPTY_MASK, 1,
505 printf("%s: Timeout\n", __func__);
513 static void zynqmp_qspi_genfifo_cmd(struct zynqmp_qspi_priv *priv)
515 const struct spi_mem_op *op = priv->op;
517 u8 i, dummy_cycles, addr;
520 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
521 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->cmd.buswidth);
522 gen_fifo_cmd |= GQSPI_GFIFO_TX;
523 gen_fifo_cmd |= op->cmd.opcode;
524 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
527 for (i = 0; i < op->addr.nbytes; i++) {
528 addr = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
530 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
531 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->addr.buswidth);
532 gen_fifo_cmd |= GQSPI_GFIFO_TX;
533 gen_fifo_cmd |= addr;
535 debug("GFIFO_CMD_Cmd = 0x%x\n", gen_fifo_cmd);
537 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
541 if (op->dummy.nbytes) {
542 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
544 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
545 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->dummy.buswidth);
546 gen_fifo_cmd &= ~(GQSPI_GFIFO_TX | GQSPI_GFIFO_RX);
547 gen_fifo_cmd |= GQSPI_GFIFO_DATA_XFR_MASK;
548 gen_fifo_cmd |= dummy_cycles;
549 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
553 static u32 zynqmp_qspi_calc_exp(struct zynqmp_qspi_priv *priv,
560 if (priv->len > 255) {
561 if (priv->len & (1 << expval)) {
562 *gen_fifo_cmd &= ~GQSPI_GFIFO_IMD_MASK;
563 *gen_fifo_cmd |= GQSPI_GFIFO_EXP_MASK;
564 *gen_fifo_cmd |= expval;
565 priv->len -= (1 << expval);
570 *gen_fifo_cmd &= ~(GQSPI_GFIFO_IMD_MASK |
571 GQSPI_GFIFO_EXP_MASK);
572 *gen_fifo_cmd |= (u8)priv->len;
580 static int zynqmp_qspi_genfifo_fill_tx(struct zynqmp_qspi_priv *priv)
586 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
587 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
588 gen_fifo_cmd |= GQSPI_GFIFO_TX | GQSPI_GFIFO_DATA_XFR_MASK;
591 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
592 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
594 debug("GFIFO_CMD_TX:0x%x\n", gen_fifo_cmd);
596 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
597 ret = zynqmp_qspi_fill_tx_fifo(priv, 1 << len);
599 ret = zynqmp_qspi_fill_tx_fifo(priv, len);
607 static int zynqmp_qspi_start_io(struct zynqmp_qspi_priv *priv,
608 u32 gen_fifo_cmd, u32 *buf)
611 u32 actuallen = priv->len;
612 u32 config_reg, ier, isr;
613 u32 timeout = GQSPI_TIMEOUT;
614 struct zynqmp_qspi_regs *regs = priv->regs;
619 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
620 /* If exponent bit is set, reset immediate to be 2^len */
621 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
622 priv->bytes_to_receive = (1 << len);
624 priv->bytes_to_receive = len;
625 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
626 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
628 config_reg = readl(®s->confr);
629 config_reg |= GQSPI_STRT_GEN_FIFO;
630 writel(config_reg, ®s->confr);
631 /* Enable RX interrupts for IO mode */
632 ier = readl(®s->ier);
633 ier |= GQSPI_IXR_ALL_MASK;
634 writel(ier, ®s->ier);
635 while (priv->bytes_to_receive && timeout) {
636 isr = readl(®s->isr);
637 if (isr & GQSPI_IXR_RXNEMTY_MASK) {
638 if (priv->bytes_to_receive >= 4) {
639 *traverse = readl(®s->drxr);
641 priv->bytes_to_receive -= 4;
643 last_bits = readl(®s->drxr);
644 memcpy(traverse, &last_bits,
645 priv->bytes_to_receive);
646 priv->bytes_to_receive = 0;
648 timeout = GQSPI_TIMEOUT;
655 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
656 (unsigned long)buf, (unsigned long)priv->rx_buf,
659 printf("IO timeout: %d\n", readl(®s->isr));
667 static int zynqmp_qspi_start_dma(struct zynqmp_qspi_priv *priv,
668 u32 gen_fifo_cmd, u32 *buf)
672 u32 actuallen = priv->len;
673 u32 totallen = priv->len;
675 struct zynqmp_qspi_dma_regs *dma_regs = priv->dma_regs;
678 if (totallen >= SZ_512M)
681 priv->len = totallen;
683 totallen -= priv->len; /* Save remaining bytes length to read */
684 actuallen = priv->len; /* Actual number of bytes reading */
686 writel(lower_32_bits((unsigned long)buf), &dma_regs->dmadst);
687 writel(upper_32_bits((unsigned long)buf) & GENMASK(11, 0),
688 &dma_regs->dmadstmsb);
689 writel(roundup(priv->len, GQSPI_DMA_ALIGN), &dma_regs->dmasize);
690 writel(GQSPI_DMA_DST_I_STS_MASK, &dma_regs->dmaier);
691 addr = (unsigned long)buf;
692 size = roundup(priv->len, GQSPI_DMA_ALIGN);
693 flush_dcache_range(addr, addr + size);
696 zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
697 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
699 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
702 ret = wait_for_bit_le32(&dma_regs->dmaisr,
703 GQSPI_DMA_DST_I_STS_DONE, 1,
706 printf("DMA Timeout:0x%x\n", readl(&dma_regs->dmaisr));
710 writel(GQSPI_DMA_DST_I_STS_DONE, &dma_regs->dmaisr);
712 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
713 (unsigned long)buf, (unsigned long)priv->rx_buf, *buf,
716 if (buf != priv->rx_buf)
717 memcpy(priv->rx_buf, buf, actuallen);
719 buf = (u32 *)((u8 *)buf + actuallen);
720 priv->rx_buf = (u8 *)priv->rx_buf + actuallen;
726 static int zynqmp_qspi_genfifo_fill_rx(struct zynqmp_qspi_priv *priv)
730 u32 actuallen = priv->len;
732 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
733 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
734 gen_fifo_cmd |= GQSPI_GFIFO_RX | GQSPI_GFIFO_DATA_XFR_MASK;
737 * Check if receive buffer is aligned to 4 byte and length
738 * is multiples of four byte as we are using dma to receive.
740 if ((!((unsigned long)priv->rx_buf & (GQSPI_DMA_ALIGN - 1)) &&
741 !(actuallen % GQSPI_DMA_ALIGN)) || priv->io_mode) {
742 buf = (u32 *)priv->rx_buf;
744 return zynqmp_qspi_start_io(priv, gen_fifo_cmd, buf);
746 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
749 ALLOC_CACHE_ALIGN_BUFFER(u8, tmp, roundup(priv->len, GQSPI_DMA_ALIGN));
751 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
754 static int zynqmp_qspi_claim_bus(struct udevice *dev)
756 struct udevice *bus = dev->parent;
757 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
758 struct zynqmp_qspi_regs *regs = priv->regs;
760 writel(GQSPI_ENABLE_ENABLE_MASK, ®s->enbr);
765 static int zynqmp_qspi_release_bus(struct udevice *dev)
767 struct udevice *bus = dev->parent;
768 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
769 struct zynqmp_qspi_regs *regs = priv->regs;
771 writel(~GQSPI_ENABLE_ENABLE_MASK, ®s->enbr);
776 static int zynqmp_qspi_exec_op(struct spi_slave *slave,
777 const struct spi_mem_op *op)
779 struct zynqmp_qspi_priv *priv = dev_get_priv(slave->dev->parent);
783 priv->tx_buf = op->data.buf.out;
784 priv->rx_buf = op->data.buf.in;
785 priv->len = op->data.nbytes;
787 zynqmp_qspi_chipselect(priv, 1);
789 /* Send opcode, addr, dummy */
790 zynqmp_qspi_genfifo_cmd(priv);
792 /* Request the transfer */
793 if (op->data.dir == SPI_MEM_DATA_IN)
794 ret = zynqmp_qspi_genfifo_fill_rx(priv);
795 else if (op->data.dir == SPI_MEM_DATA_OUT)
796 ret = zynqmp_qspi_genfifo_fill_tx(priv);
798 zynqmp_qspi_chipselect(priv, 0);
803 static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = {
804 .exec_op = zynqmp_qspi_exec_op,
807 static const struct dm_spi_ops zynqmp_qspi_ops = {
808 .claim_bus = zynqmp_qspi_claim_bus,
809 .release_bus = zynqmp_qspi_release_bus,
810 .set_speed = zynqmp_qspi_set_speed,
811 .set_mode = zynqmp_qspi_set_mode,
812 .mem_ops = &zynqmp_qspi_mem_ops,
815 static const struct udevice_id zynqmp_qspi_ids[] = {
816 { .compatible = "xlnx,zynqmp-qspi-1.0" },
817 { .compatible = "xlnx,versal-qspi-1.0" },
821 U_BOOT_DRIVER(zynqmp_qspi) = {
822 .name = "zynqmp_qspi",
824 .of_match = zynqmp_qspi_ids,
825 .ops = &zynqmp_qspi_ops,
826 .of_to_plat = zynqmp_qspi_of_to_plat,
827 .plat_auto = sizeof(struct zynqmp_qspi_plat),
828 .priv_auto = sizeof(struct zynqmp_qspi_priv),
829 .probe = zynqmp_qspi_probe,