blob: f8d13d193e308581f5f4ce8c46966b2f8bb8b0d4 [file] [log] [blame]
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Xilinx
4 *
5 * Xilinx ZynqMP Generic Quad-SPI(QSPI) controller driver(master mode only)
6 */
7
8#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053011#include <asm/arch/sys_proto.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053014#include <asm/io.h>
15#include <clk.h>
16#include <dm.h>
17#include <malloc.h>
18#include <memalign.h>
19#include <spi.h>
Brandon Maier4d9cce72021-01-20 10:39:46 -060020#include <spi-mem.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053021#include <ubi_uboot.h>
22#include <wait_bit.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <linux/err.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053026
27#define GQSPI_GFIFO_STRT_MODE_MASK BIT(29)
28#define GQSPI_CONFIG_MODE_EN_MASK (3 << 30)
29#define GQSPI_CONFIG_DMA_MODE (2 << 30)
30#define GQSPI_CONFIG_CPHA_MASK BIT(2)
31#define GQSPI_CONFIG_CPOL_MASK BIT(1)
32
33/*
34 * QSPI Interrupt Registers bit Masks
35 *
36 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
37 * bit definitions.
38 */
39#define GQSPI_IXR_TXNFULL_MASK 0x00000004 /* QSPI TX FIFO Overflow */
40#define GQSPI_IXR_TXFULL_MASK 0x00000008 /* QSPI TX FIFO is full */
41#define GQSPI_IXR_RXNEMTY_MASK 0x00000010 /* QSPI RX FIFO Not Empty */
42#define GQSPI_IXR_GFEMTY_MASK 0x00000080 /* QSPI Generic FIFO Empty */
43#define GQSPI_IXR_ALL_MASK (GQSPI_IXR_TXNFULL_MASK | \
44 GQSPI_IXR_RXNEMTY_MASK)
45
46/*
47 * QSPI Enable Register bit Masks
48 *
49 * This register is used to enable or disable the QSPI controller
50 */
51#define GQSPI_ENABLE_ENABLE_MASK 0x00000001 /* QSPI Enable Bit Mask */
52
53#define GQSPI_GFIFO_LOW_BUS BIT(14)
54#define GQSPI_GFIFO_CS_LOWER BIT(12)
55#define GQSPI_GFIFO_UP_BUS BIT(15)
56#define GQSPI_GFIFO_CS_UPPER BIT(13)
57#define GQSPI_SPI_MODE_QSPI (3 << 10)
58#define GQSPI_SPI_MODE_SPI BIT(10)
59#define GQSPI_SPI_MODE_DUAL_SPI (2 << 10)
60#define GQSPI_IMD_DATA_CS_ASSERT 5
61#define GQSPI_IMD_DATA_CS_DEASSERT 5
62#define GQSPI_GFIFO_TX BIT(16)
63#define GQSPI_GFIFO_RX BIT(17)
64#define GQSPI_GFIFO_STRIPE_MASK BIT(18)
65#define GQSPI_GFIFO_IMD_MASK 0xFF
66#define GQSPI_GFIFO_EXP_MASK BIT(9)
67#define GQSPI_GFIFO_DATA_XFR_MASK BIT(8)
68#define GQSPI_STRT_GEN_FIFO BIT(28)
69#define GQSPI_GEN_FIFO_STRT_MOD BIT(29)
70#define GQSPI_GFIFO_WP_HOLD BIT(19)
71#define GQSPI_BAUD_DIV_MASK (7 << 3)
72#define GQSPI_DFLT_BAUD_RATE_DIV BIT(3)
73#define GQSPI_GFIFO_ALL_INT_MASK 0xFBE
74#define GQSPI_DMA_DST_I_STS_DONE BIT(1)
75#define GQSPI_DMA_DST_I_STS_MASK 0xFE
76#define MODEBITS 0x6
77
78#define GQSPI_GFIFO_SELECT BIT(0)
79#define GQSPI_FIFO_THRESHOLD 1
80
81#define SPI_XFER_ON_BOTH 0
82#define SPI_XFER_ON_LOWER 1
83#define SPI_XFER_ON_UPPER 2
84
85#define GQSPI_DMA_ALIGN 0x4
86#define GQSPI_MAX_BAUD_RATE_VAL 7
87#define GQSPI_DFLT_BAUD_RATE_VAL 2
88
89#define GQSPI_TIMEOUT 100000000
90
91#define GQSPI_BAUD_DIV_SHIFT 2
92#define GQSPI_LPBK_DLY_ADJ_LPBK_SHIFT 5
93#define GQSPI_LPBK_DLY_ADJ_DLY_1 0x2
94#define GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT 3
95#define GQSPI_LPBK_DLY_ADJ_DLY_0 0x3
96#define GQSPI_USE_DATA_DLY 0x1
97#define GQSPI_USE_DATA_DLY_SHIFT 31
98#define GQSPI_DATA_DLY_ADJ_VALUE 0x2
99#define GQSPI_DATA_DLY_ADJ_SHIFT 28
100#define TAP_DLY_BYPASS_LQSPI_RX_VALUE 0x1
101#define TAP_DLY_BYPASS_LQSPI_RX_SHIFT 2
102#define GQSPI_DATA_DLY_ADJ_OFST 0x000001F8
103#define IOU_TAPDLY_BYPASS_OFST 0xFF180390
104#define GQSPI_LPBK_DLY_ADJ_LPBK_MASK 0x00000020
105#define GQSPI_FREQ_40MHZ 40000000
106#define GQSPI_FREQ_100MHZ 100000000
107#define GQSPI_FREQ_150MHZ 150000000
108#define IOU_TAPDLY_BYPASS_MASK 0x7
109
110#define GQSPI_REG_OFFSET 0x100
111#define GQSPI_DMA_REG_OFFSET 0x800
112
113/* QSPI register offsets */
114struct zynqmp_qspi_regs {
115 u32 confr; /* 0x00 */
116 u32 isr; /* 0x04 */
117 u32 ier; /* 0x08 */
118 u32 idisr; /* 0x0C */
119 u32 imaskr; /* 0x10 */
120 u32 enbr; /* 0x14 */
121 u32 dr; /* 0x18 */
122 u32 txd0r; /* 0x1C */
123 u32 drxr; /* 0x20 */
124 u32 sicr; /* 0x24 */
125 u32 txftr; /* 0x28 */
126 u32 rxftr; /* 0x2C */
127 u32 gpior; /* 0x30 */
128 u32 reserved0; /* 0x34 */
129 u32 lpbkdly; /* 0x38 */
130 u32 reserved1; /* 0x3C */
131 u32 genfifo; /* 0x40 */
132 u32 gqspisel; /* 0x44 */
133 u32 reserved2; /* 0x48 */
134 u32 gqfifoctrl; /* 0x4C */
135 u32 gqfthr; /* 0x50 */
136 u32 gqpollcfg; /* 0x54 */
137 u32 gqpollto; /* 0x58 */
138 u32 gqxfersts; /* 0x5C */
139 u32 gqfifosnap; /* 0x60 */
140 u32 gqrxcpy; /* 0x64 */
141 u32 reserved3[36]; /* 0x68 */
142 u32 gqspidlyadj; /* 0xF8 */
143};
144
145struct zynqmp_qspi_dma_regs {
146 u32 dmadst; /* 0x00 */
147 u32 dmasize; /* 0x04 */
148 u32 dmasts; /* 0x08 */
149 u32 dmactrl; /* 0x0C */
150 u32 reserved0; /* 0x10 */
151 u32 dmaisr; /* 0x14 */
152 u32 dmaier; /* 0x18 */
153 u32 dmaidr; /* 0x1C */
154 u32 dmaimr; /* 0x20 */
155 u32 dmactrl2; /* 0x24 */
156 u32 dmadstmsb; /* 0x28 */
157};
158
159DECLARE_GLOBAL_DATA_PTR;
160
Simon Glassb75b15b2020-12-03 16:55:23 -0700161struct zynqmp_qspi_plat {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530162 struct zynqmp_qspi_regs *regs;
163 struct zynqmp_qspi_dma_regs *dma_regs;
164 u32 frequency;
165 u32 speed_hz;
166};
167
168struct zynqmp_qspi_priv {
169 struct zynqmp_qspi_regs *regs;
170 struct zynqmp_qspi_dma_regs *dma_regs;
171 const void *tx_buf;
172 void *rx_buf;
173 unsigned int len;
174 int bytes_to_transfer;
175 int bytes_to_receive;
Brandon Maier4d9cce72021-01-20 10:39:46 -0600176 const struct spi_mem_op *op;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530177};
178
Simon Glassaad29ae2020-12-03 16:55:21 -0700179static int zynqmp_qspi_of_to_plat(struct udevice *bus)
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530180{
Simon Glass95588622020-12-22 19:30:28 -0700181 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530182
183 debug("%s\n", __func__);
184
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900185 plat->regs = (struct zynqmp_qspi_regs *)(dev_read_addr(bus) +
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530186 GQSPI_REG_OFFSET);
187 plat->dma_regs = (struct zynqmp_qspi_dma_regs *)
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900188 (dev_read_addr(bus) + GQSPI_DMA_REG_OFFSET);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530189
190 return 0;
191}
192
193static void zynqmp_qspi_init_hw(struct zynqmp_qspi_priv *priv)
194{
195 u32 config_reg;
196 struct zynqmp_qspi_regs *regs = priv->regs;
197
198 writel(GQSPI_GFIFO_SELECT, &regs->gqspisel);
199 writel(GQSPI_GFIFO_ALL_INT_MASK, &regs->idisr);
200 writel(GQSPI_FIFO_THRESHOLD, &regs->txftr);
201 writel(GQSPI_FIFO_THRESHOLD, &regs->rxftr);
202 writel(GQSPI_GFIFO_ALL_INT_MASK, &regs->isr);
203
204 config_reg = readl(&regs->confr);
205 config_reg &= ~(GQSPI_GFIFO_STRT_MODE_MASK |
206 GQSPI_CONFIG_MODE_EN_MASK);
207 config_reg |= GQSPI_CONFIG_DMA_MODE |
208 GQSPI_GFIFO_WP_HOLD |
209 GQSPI_DFLT_BAUD_RATE_DIV;
210 writel(config_reg, &regs->confr);
211
212 writel(GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
213}
214
215static u32 zynqmp_qspi_bus_select(struct zynqmp_qspi_priv *priv)
216{
217 u32 gqspi_fifo_reg = 0;
218
219 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS |
220 GQSPI_GFIFO_CS_LOWER;
221
222 return gqspi_fifo_reg;
223}
224
Brandon Maier4d9cce72021-01-20 10:39:46 -0600225static u32 zynqmp_qspi_genfifo_mode(u8 buswidth)
226{
227 switch (buswidth) {
228 case 1:
229 return GQSPI_SPI_MODE_SPI;
230 case 2:
231 return GQSPI_SPI_MODE_DUAL_SPI;
232 case 4:
233 return GQSPI_SPI_MODE_QSPI;
234 default:
235 debug("Unsupported bus width %u\n", buswidth);
236 return GQSPI_SPI_MODE_SPI;
237 }
238}
239
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530240static void zynqmp_qspi_fill_gen_fifo(struct zynqmp_qspi_priv *priv,
241 u32 gqspi_fifo_reg)
242{
243 struct zynqmp_qspi_regs *regs = priv->regs;
244 int ret = 0;
245
246 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_GFEMTY_MASK, 1,
247 GQSPI_TIMEOUT, 1);
248 if (ret)
249 printf("%s Timeout\n", __func__);
250
251 writel(gqspi_fifo_reg, &regs->genfifo);
252}
253
254static void zynqmp_qspi_chipselect(struct zynqmp_qspi_priv *priv, int is_on)
255{
256 u32 gqspi_fifo_reg = 0;
257
258 if (is_on) {
259 gqspi_fifo_reg = zynqmp_qspi_bus_select(priv);
260 gqspi_fifo_reg |= GQSPI_SPI_MODE_SPI |
261 GQSPI_IMD_DATA_CS_ASSERT;
262 } else {
263 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS;
264 gqspi_fifo_reg |= GQSPI_IMD_DATA_CS_DEASSERT;
265 }
266
267 debug("GFIFO_CMD_CS: 0x%x\n", gqspi_fifo_reg);
268
269 zynqmp_qspi_fill_gen_fifo(priv, gqspi_fifo_reg);
270}
271
272void zynqmp_qspi_set_tapdelay(struct udevice *bus, u32 baudrateval)
273{
Simon Glass95588622020-12-22 19:30:28 -0700274 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530275 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
276 struct zynqmp_qspi_regs *regs = priv->regs;
277 u32 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate;
278 u32 reqhz = 0;
279
280 clk_rate = plat->frequency;
281 reqhz = (clk_rate / (GQSPI_BAUD_DIV_SHIFT << baudrateval));
282
283 debug("%s, req_hz:%d, clk_rate:%d, baudrateval:%d\n",
284 __func__, reqhz, clk_rate, baudrateval);
285
286 if (reqhz < GQSPI_FREQ_40MHZ) {
287 zynqmp_mmio_read(IOU_TAPDLY_BYPASS_OFST, &tapdlybypass);
288 tapdlybypass |= (TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
289 TAP_DLY_BYPASS_LQSPI_RX_SHIFT);
Siva Durga Prasad Paladugu05ddbdf2019-03-07 16:08:48 +0530290 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530291 zynqmp_mmio_read(IOU_TAPDLY_BYPASS_OFST, &tapdlybypass);
292 tapdlybypass |= (TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
293 TAP_DLY_BYPASS_LQSPI_RX_SHIFT);
294 lpbkdlyadj = readl(&regs->lpbkdly);
295 lpbkdlyadj |= (GQSPI_LPBK_DLY_ADJ_LPBK_MASK);
296 datadlyadj = readl(&regs->gqspidlyadj);
297 datadlyadj |= ((GQSPI_USE_DATA_DLY << GQSPI_USE_DATA_DLY_SHIFT)
298 | (GQSPI_DATA_DLY_ADJ_VALUE <<
299 GQSPI_DATA_DLY_ADJ_SHIFT));
Siva Durga Prasad Paladugu05ddbdf2019-03-07 16:08:48 +0530300 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530301 lpbkdlyadj = readl(&regs->lpbkdly);
302 lpbkdlyadj |= ((GQSPI_LPBK_DLY_ADJ_LPBK_MASK) |
303 GQSPI_LPBK_DLY_ADJ_DLY_0);
304 }
305
306 zynqmp_mmio_write(IOU_TAPDLY_BYPASS_OFST, IOU_TAPDLY_BYPASS_MASK,
307 tapdlybypass);
308 writel(lpbkdlyadj, &regs->lpbkdly);
309 writel(datadlyadj, &regs->gqspidlyadj);
310}
311
312static int zynqmp_qspi_set_speed(struct udevice *bus, uint speed)
313{
Simon Glass95588622020-12-22 19:30:28 -0700314 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530315 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
316 struct zynqmp_qspi_regs *regs = priv->regs;
317 u32 confr;
318 u8 baud_rate_val = 0;
319
320 debug("%s\n", __func__);
321 if (speed > plat->frequency)
322 speed = plat->frequency;
323
Brandon Maierb8003d52021-01-20 14:28:30 -0600324 if (plat->speed_hz != speed) {
325 /* Set the clock frequency */
326 /* If speed == 0, default to lowest speed */
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530327 while ((baud_rate_val < 8) &&
328 ((plat->frequency /
329 (2 << baud_rate_val)) > speed))
330 baud_rate_val++;
331
332 if (baud_rate_val > GQSPI_MAX_BAUD_RATE_VAL)
333 baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL;
334
335 plat->speed_hz = plat->frequency / (2 << baud_rate_val);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530336
Brandon Maierb8003d52021-01-20 14:28:30 -0600337 confr = readl(&regs->confr);
338 confr &= ~GQSPI_BAUD_DIV_MASK;
339 confr |= (baud_rate_val << 3);
340 writel(confr, &regs->confr);
341 zynqmp_qspi_set_tapdelay(bus, baud_rate_val);
342
343 debug("regs=%p, speed=%d\n", priv->regs, plat->speed_hz);
344 }
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530345
346 return 0;
347}
348
349static int zynqmp_qspi_probe(struct udevice *bus)
350{
Simon Glassb75b15b2020-12-03 16:55:23 -0700351 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530352 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
353 struct clk clk;
354 unsigned long clock;
355 int ret;
356
357 debug("%s: bus:%p, priv:%p\n", __func__, bus, priv);
358
359 priv->regs = plat->regs;
360 priv->dma_regs = plat->dma_regs;
361
362 ret = clk_get_by_index(bus, 0, &clk);
363 if (ret < 0) {
Sean Anderson241232a2020-09-15 10:45:12 -0400364 dev_err(bus, "failed to get clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530365 return ret;
366 }
367
368 clock = clk_get_rate(&clk);
369 if (IS_ERR_VALUE(clock)) {
Sean Anderson241232a2020-09-15 10:45:12 -0400370 dev_err(bus, "failed to get rate\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530371 return clock;
372 }
373 debug("%s: CLK %ld\n", __func__, clock);
374
375 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +0100376 if (ret) {
Sean Anderson241232a2020-09-15 10:45:12 -0400377 dev_err(bus, "failed to enable clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530378 return ret;
379 }
380 plat->frequency = clock;
381 plat->speed_hz = plat->frequency / 2;
382
383 /* init the zynq spi hw */
384 zynqmp_qspi_init_hw(priv);
385
386 return 0;
387}
388
389static int zynqmp_qspi_set_mode(struct udevice *bus, uint mode)
390{
391 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
392 struct zynqmp_qspi_regs *regs = priv->regs;
393 u32 confr;
394
395 debug("%s\n", __func__);
396 /* Set the SPI Clock phase and polarities */
397 confr = readl(&regs->confr);
398 confr &= ~(GQSPI_CONFIG_CPHA_MASK |
399 GQSPI_CONFIG_CPOL_MASK);
400
401 if (mode & SPI_CPHA)
402 confr |= GQSPI_CONFIG_CPHA_MASK;
403 if (mode & SPI_CPOL)
404 confr |= GQSPI_CONFIG_CPOL_MASK;
405
406 writel(confr, &regs->confr);
407
408 return 0;
409}
410
411static int zynqmp_qspi_fill_tx_fifo(struct zynqmp_qspi_priv *priv, u32 size)
412{
413 u32 data;
414 int ret = 0;
415 struct zynqmp_qspi_regs *regs = priv->regs;
416 u32 *buf = (u32 *)priv->tx_buf;
417 u32 len = size;
418
419 debug("TxFIFO: 0x%x, size: 0x%x\n", readl(&regs->isr),
420 size);
421
422 while (size) {
423 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_TXNFULL_MASK, 1,
424 GQSPI_TIMEOUT, 1);
425 if (ret) {
426 printf("%s: Timeout\n", __func__);
427 return ret;
428 }
429
430 if (size >= 4) {
431 writel(*buf, &regs->txd0r);
432 buf++;
433 size -= 4;
434 } else {
435 switch (size) {
436 case 1:
437 data = *((u8 *)buf);
438 buf += 1;
439 data |= GENMASK(31, 8);
440 break;
441 case 2:
442 data = *((u16 *)buf);
443 buf += 2;
444 data |= GENMASK(31, 16);
445 break;
446 case 3:
T Karthik Reddycc59fc92020-11-19 05:00:36 -0700447 data = *buf;
448 buf += 3;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530449 data |= GENMASK(31, 24);
450 break;
451 }
452 writel(data, &regs->txd0r);
453 size = 0;
454 }
455 }
456
457 priv->tx_buf += len;
458 return 0;
459}
460
461static void zynqmp_qspi_genfifo_cmd(struct zynqmp_qspi_priv *priv)
462{
Brandon Maier4d9cce72021-01-20 10:39:46 -0600463 const struct spi_mem_op *op = priv->op;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530464 u32 gen_fifo_cmd;
Brandon Maier4d9cce72021-01-20 10:39:46 -0600465 u8 i, dummy_cycles, addr;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530466
Brandon Maier4d9cce72021-01-20 10:39:46 -0600467 /* Send opcode */
468 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
469 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->cmd.buswidth);
470 gen_fifo_cmd |= GQSPI_GFIFO_TX;
471 gen_fifo_cmd |= op->cmd.opcode;
472 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
473
474 /* Send address */
475 for (i = 0; i < op->addr.nbytes; i++) {
476 addr = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
477
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530478 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600479 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->addr.buswidth);
480 gen_fifo_cmd |= GQSPI_GFIFO_TX;
481 gen_fifo_cmd |= addr;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530482
483 debug("GFIFO_CMD_Cmd = 0x%x\n", gen_fifo_cmd);
484
485 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
486 }
Brandon Maier4d9cce72021-01-20 10:39:46 -0600487
488 /* Send dummy */
489 if (op->dummy.nbytes) {
490 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
491
492 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
493 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->dummy.buswidth);
494 gen_fifo_cmd &= ~(GQSPI_GFIFO_TX | GQSPI_GFIFO_RX);
495 gen_fifo_cmd |= GQSPI_GFIFO_DATA_XFR_MASK;
496 gen_fifo_cmd |= dummy_cycles;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530497 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
498 }
499}
500
501static u32 zynqmp_qspi_calc_exp(struct zynqmp_qspi_priv *priv,
502 u32 *gen_fifo_cmd)
503{
504 u32 expval = 8;
505 u32 len;
506
507 while (1) {
508 if (priv->len > 255) {
509 if (priv->len & (1 << expval)) {
510 *gen_fifo_cmd &= ~GQSPI_GFIFO_IMD_MASK;
511 *gen_fifo_cmd |= GQSPI_GFIFO_EXP_MASK;
512 *gen_fifo_cmd |= expval;
513 priv->len -= (1 << expval);
514 return expval;
515 }
516 expval++;
517 } else {
518 *gen_fifo_cmd &= ~(GQSPI_GFIFO_IMD_MASK |
519 GQSPI_GFIFO_EXP_MASK);
520 *gen_fifo_cmd |= (u8)priv->len;
521 len = (u8)priv->len;
522 priv->len = 0;
523 return len;
524 }
525 }
526}
527
528static int zynqmp_qspi_genfifo_fill_tx(struct zynqmp_qspi_priv *priv)
529{
530 u32 gen_fifo_cmd;
531 u32 len;
532 int ret = 0;
533
534 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600535 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530536 gen_fifo_cmd |= GQSPI_GFIFO_TX |
537 GQSPI_GFIFO_DATA_XFR_MASK;
538
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530539 while (priv->len) {
540 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
541 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
542
543 debug("GFIFO_CMD_TX:0x%x\n", gen_fifo_cmd);
544
545 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
546 ret = zynqmp_qspi_fill_tx_fifo(priv,
547 1 << len);
548 else
549 ret = zynqmp_qspi_fill_tx_fifo(priv,
550 len);
551
552 if (ret)
553 return ret;
554 }
555 return ret;
556}
557
558static int zynqmp_qspi_start_dma(struct zynqmp_qspi_priv *priv,
559 u32 gen_fifo_cmd, u32 *buf)
560{
561 u32 addr;
562 u32 size, len;
563 u32 actuallen = priv->len;
564 int ret = 0;
565 struct zynqmp_qspi_dma_regs *dma_regs = priv->dma_regs;
566
567 writel((unsigned long)buf, &dma_regs->dmadst);
568 writel(roundup(priv->len, ARCH_DMA_MINALIGN), &dma_regs->dmasize);
569 writel(GQSPI_DMA_DST_I_STS_MASK, &dma_regs->dmaier);
570 addr = (unsigned long)buf;
571 size = roundup(priv->len, ARCH_DMA_MINALIGN);
572 flush_dcache_range(addr, addr + size);
573
574 while (priv->len) {
575 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
576 if (!(gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK) &&
577 (len % ARCH_DMA_MINALIGN)) {
578 gen_fifo_cmd &= ~GENMASK(7, 0);
579 gen_fifo_cmd |= roundup(len, ARCH_DMA_MINALIGN);
580 }
581 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
582
583 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
584 }
585
586 ret = wait_for_bit_le32(&dma_regs->dmaisr, GQSPI_DMA_DST_I_STS_DONE,
587 1, GQSPI_TIMEOUT, 1);
588 if (ret) {
589 printf("DMA Timeout:0x%x\n", readl(&dma_regs->dmaisr));
590 return -ETIMEDOUT;
591 }
592
593 writel(GQSPI_DMA_DST_I_STS_DONE, &dma_regs->dmaisr);
594
595 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
596 (unsigned long)buf, (unsigned long)priv->rx_buf, *buf,
597 actuallen);
598
599 if (buf != priv->rx_buf)
600 memcpy(priv->rx_buf, buf, actuallen);
601
602 return 0;
603}
604
605static int zynqmp_qspi_genfifo_fill_rx(struct zynqmp_qspi_priv *priv)
606{
607 u32 gen_fifo_cmd;
608 u32 *buf;
609 u32 actuallen = priv->len;
610
611 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600612 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530613 gen_fifo_cmd |= GQSPI_GFIFO_RX |
614 GQSPI_GFIFO_DATA_XFR_MASK;
615
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530616 /*
617 * Check if receive buffer is aligned to 4 byte and length
618 * is multiples of four byte as we are using dma to receive.
619 */
620 if (!((unsigned long)priv->rx_buf & (GQSPI_DMA_ALIGN - 1)) &&
621 !(actuallen % GQSPI_DMA_ALIGN)) {
622 buf = (u32 *)priv->rx_buf;
623 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
624 }
625
626 ALLOC_CACHE_ALIGN_BUFFER(u8, tmp, roundup(priv->len,
627 GQSPI_DMA_ALIGN));
628 buf = (u32 *)tmp;
629 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
630}
631
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530632static int zynqmp_qspi_claim_bus(struct udevice *dev)
633{
634 struct udevice *bus = dev->parent;
635 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
636 struct zynqmp_qspi_regs *regs = priv->regs;
637
638 writel(GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
639
640 return 0;
641}
642
643static int zynqmp_qspi_release_bus(struct udevice *dev)
644{
645 struct udevice *bus = dev->parent;
646 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
647 struct zynqmp_qspi_regs *regs = priv->regs;
648
649 writel(~GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
650
651 return 0;
652}
653
Brandon Maier4d9cce72021-01-20 10:39:46 -0600654static int zynqmp_qspi_exec_op(struct spi_slave *slave,
655 const struct spi_mem_op *op)
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530656{
Brandon Maier4d9cce72021-01-20 10:39:46 -0600657 struct zynqmp_qspi_priv *priv = dev_get_priv(slave->dev->parent);
658 int ret = 0;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530659
Brandon Maier4d9cce72021-01-20 10:39:46 -0600660 priv->op = op;
661 priv->tx_buf = op->data.buf.out;
662 priv->rx_buf = op->data.buf.in;
663 priv->len = op->data.nbytes;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530664
Brandon Maier4d9cce72021-01-20 10:39:46 -0600665 zynqmp_qspi_chipselect(priv, 1);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530666
Brandon Maier4d9cce72021-01-20 10:39:46 -0600667 /* Send opcode, addr, dummy */
668 zynqmp_qspi_genfifo_cmd(priv);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530669
Brandon Maier4d9cce72021-01-20 10:39:46 -0600670 /* Request the transfer */
671 if (op->data.dir == SPI_MEM_DATA_IN)
672 ret = zynqmp_qspi_genfifo_fill_rx(priv);
673 else if (op->data.dir == SPI_MEM_DATA_OUT)
674 ret = zynqmp_qspi_genfifo_fill_tx(priv);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530675
Brandon Maier4d9cce72021-01-20 10:39:46 -0600676 zynqmp_qspi_chipselect(priv, 0);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530677
Brandon Maier4d9cce72021-01-20 10:39:46 -0600678 return ret;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530679}
680
Brandon Maier4d9cce72021-01-20 10:39:46 -0600681static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = {
682 .exec_op = zynqmp_qspi_exec_op,
683};
684
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530685static const struct dm_spi_ops zynqmp_qspi_ops = {
686 .claim_bus = zynqmp_qspi_claim_bus,
687 .release_bus = zynqmp_qspi_release_bus,
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530688 .set_speed = zynqmp_qspi_set_speed,
689 .set_mode = zynqmp_qspi_set_mode,
Brandon Maier4d9cce72021-01-20 10:39:46 -0600690 .mem_ops = &zynqmp_qspi_mem_ops,
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530691};
692
693static const struct udevice_id zynqmp_qspi_ids[] = {
694 { .compatible = "xlnx,zynqmp-qspi-1.0" },
Michal Simeked373eb2018-11-29 08:48:28 +0100695 { .compatible = "xlnx,versal-qspi-1.0" },
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530696 { }
697};
698
699U_BOOT_DRIVER(zynqmp_qspi) = {
700 .name = "zynqmp_qspi",
701 .id = UCLASS_SPI,
702 .of_match = zynqmp_qspi_ids,
703 .ops = &zynqmp_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700704 .of_to_plat = zynqmp_qspi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700705 .plat_auto = sizeof(struct zynqmp_qspi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700706 .priv_auto = sizeof(struct zynqmp_qspi_priv),
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530707 .probe = zynqmp_qspi_probe,
708};