blob: f669974af17b5f64e6407159cd56c319c6ad719c [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>
20#include <ubi_uboot.h>
21#include <wait_bit.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070024#include <linux/err.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053025
26#define GQSPI_GFIFO_STRT_MODE_MASK BIT(29)
27#define GQSPI_CONFIG_MODE_EN_MASK (3 << 30)
28#define GQSPI_CONFIG_DMA_MODE (2 << 30)
29#define GQSPI_CONFIG_CPHA_MASK BIT(2)
30#define GQSPI_CONFIG_CPOL_MASK BIT(1)
31
32/*
33 * QSPI Interrupt Registers bit Masks
34 *
35 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
36 * bit definitions.
37 */
38#define GQSPI_IXR_TXNFULL_MASK 0x00000004 /* QSPI TX FIFO Overflow */
39#define GQSPI_IXR_TXFULL_MASK 0x00000008 /* QSPI TX FIFO is full */
40#define GQSPI_IXR_RXNEMTY_MASK 0x00000010 /* QSPI RX FIFO Not Empty */
41#define GQSPI_IXR_GFEMTY_MASK 0x00000080 /* QSPI Generic FIFO Empty */
42#define GQSPI_IXR_ALL_MASK (GQSPI_IXR_TXNFULL_MASK | \
43 GQSPI_IXR_RXNEMTY_MASK)
44
45/*
46 * QSPI Enable Register bit Masks
47 *
48 * This register is used to enable or disable the QSPI controller
49 */
50#define GQSPI_ENABLE_ENABLE_MASK 0x00000001 /* QSPI Enable Bit Mask */
51
52#define GQSPI_GFIFO_LOW_BUS BIT(14)
53#define GQSPI_GFIFO_CS_LOWER BIT(12)
54#define GQSPI_GFIFO_UP_BUS BIT(15)
55#define GQSPI_GFIFO_CS_UPPER BIT(13)
56#define GQSPI_SPI_MODE_QSPI (3 << 10)
57#define GQSPI_SPI_MODE_SPI BIT(10)
58#define GQSPI_SPI_MODE_DUAL_SPI (2 << 10)
59#define GQSPI_IMD_DATA_CS_ASSERT 5
60#define GQSPI_IMD_DATA_CS_DEASSERT 5
61#define GQSPI_GFIFO_TX BIT(16)
62#define GQSPI_GFIFO_RX BIT(17)
63#define GQSPI_GFIFO_STRIPE_MASK BIT(18)
64#define GQSPI_GFIFO_IMD_MASK 0xFF
65#define GQSPI_GFIFO_EXP_MASK BIT(9)
66#define GQSPI_GFIFO_DATA_XFR_MASK BIT(8)
67#define GQSPI_STRT_GEN_FIFO BIT(28)
68#define GQSPI_GEN_FIFO_STRT_MOD BIT(29)
69#define GQSPI_GFIFO_WP_HOLD BIT(19)
70#define GQSPI_BAUD_DIV_MASK (7 << 3)
71#define GQSPI_DFLT_BAUD_RATE_DIV BIT(3)
72#define GQSPI_GFIFO_ALL_INT_MASK 0xFBE
73#define GQSPI_DMA_DST_I_STS_DONE BIT(1)
74#define GQSPI_DMA_DST_I_STS_MASK 0xFE
75#define MODEBITS 0x6
76
77#define GQSPI_GFIFO_SELECT BIT(0)
78#define GQSPI_FIFO_THRESHOLD 1
79
80#define SPI_XFER_ON_BOTH 0
81#define SPI_XFER_ON_LOWER 1
82#define SPI_XFER_ON_UPPER 2
83
84#define GQSPI_DMA_ALIGN 0x4
85#define GQSPI_MAX_BAUD_RATE_VAL 7
86#define GQSPI_DFLT_BAUD_RATE_VAL 2
87
88#define GQSPI_TIMEOUT 100000000
89
90#define GQSPI_BAUD_DIV_SHIFT 2
91#define GQSPI_LPBK_DLY_ADJ_LPBK_SHIFT 5
92#define GQSPI_LPBK_DLY_ADJ_DLY_1 0x2
93#define GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT 3
94#define GQSPI_LPBK_DLY_ADJ_DLY_0 0x3
95#define GQSPI_USE_DATA_DLY 0x1
96#define GQSPI_USE_DATA_DLY_SHIFT 31
97#define GQSPI_DATA_DLY_ADJ_VALUE 0x2
98#define GQSPI_DATA_DLY_ADJ_SHIFT 28
99#define TAP_DLY_BYPASS_LQSPI_RX_VALUE 0x1
100#define TAP_DLY_BYPASS_LQSPI_RX_SHIFT 2
101#define GQSPI_DATA_DLY_ADJ_OFST 0x000001F8
102#define IOU_TAPDLY_BYPASS_OFST 0xFF180390
103#define GQSPI_LPBK_DLY_ADJ_LPBK_MASK 0x00000020
104#define GQSPI_FREQ_40MHZ 40000000
105#define GQSPI_FREQ_100MHZ 100000000
106#define GQSPI_FREQ_150MHZ 150000000
107#define IOU_TAPDLY_BYPASS_MASK 0x7
108
109#define GQSPI_REG_OFFSET 0x100
110#define GQSPI_DMA_REG_OFFSET 0x800
111
112/* QSPI register offsets */
113struct zynqmp_qspi_regs {
114 u32 confr; /* 0x00 */
115 u32 isr; /* 0x04 */
116 u32 ier; /* 0x08 */
117 u32 idisr; /* 0x0C */
118 u32 imaskr; /* 0x10 */
119 u32 enbr; /* 0x14 */
120 u32 dr; /* 0x18 */
121 u32 txd0r; /* 0x1C */
122 u32 drxr; /* 0x20 */
123 u32 sicr; /* 0x24 */
124 u32 txftr; /* 0x28 */
125 u32 rxftr; /* 0x2C */
126 u32 gpior; /* 0x30 */
127 u32 reserved0; /* 0x34 */
128 u32 lpbkdly; /* 0x38 */
129 u32 reserved1; /* 0x3C */
130 u32 genfifo; /* 0x40 */
131 u32 gqspisel; /* 0x44 */
132 u32 reserved2; /* 0x48 */
133 u32 gqfifoctrl; /* 0x4C */
134 u32 gqfthr; /* 0x50 */
135 u32 gqpollcfg; /* 0x54 */
136 u32 gqpollto; /* 0x58 */
137 u32 gqxfersts; /* 0x5C */
138 u32 gqfifosnap; /* 0x60 */
139 u32 gqrxcpy; /* 0x64 */
140 u32 reserved3[36]; /* 0x68 */
141 u32 gqspidlyadj; /* 0xF8 */
142};
143
144struct zynqmp_qspi_dma_regs {
145 u32 dmadst; /* 0x00 */
146 u32 dmasize; /* 0x04 */
147 u32 dmasts; /* 0x08 */
148 u32 dmactrl; /* 0x0C */
149 u32 reserved0; /* 0x10 */
150 u32 dmaisr; /* 0x14 */
151 u32 dmaier; /* 0x18 */
152 u32 dmaidr; /* 0x1C */
153 u32 dmaimr; /* 0x20 */
154 u32 dmactrl2; /* 0x24 */
155 u32 dmadstmsb; /* 0x28 */
156};
157
158DECLARE_GLOBAL_DATA_PTR;
159
Simon Glassb75b15b2020-12-03 16:55:23 -0700160struct zynqmp_qspi_plat {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530161 struct zynqmp_qspi_regs *regs;
162 struct zynqmp_qspi_dma_regs *dma_regs;
163 u32 frequency;
164 u32 speed_hz;
165};
166
167struct zynqmp_qspi_priv {
168 struct zynqmp_qspi_regs *regs;
169 struct zynqmp_qspi_dma_regs *dma_regs;
170 const void *tx_buf;
171 void *rx_buf;
172 unsigned int len;
173 int bytes_to_transfer;
174 int bytes_to_receive;
175 unsigned int is_inst;
176 unsigned int cs_change:1;
177};
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
225static void zynqmp_qspi_fill_gen_fifo(struct zynqmp_qspi_priv *priv,
226 u32 gqspi_fifo_reg)
227{
228 struct zynqmp_qspi_regs *regs = priv->regs;
229 int ret = 0;
230
231 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_GFEMTY_MASK, 1,
232 GQSPI_TIMEOUT, 1);
233 if (ret)
234 printf("%s Timeout\n", __func__);
235
236 writel(gqspi_fifo_reg, &regs->genfifo);
237}
238
239static void zynqmp_qspi_chipselect(struct zynqmp_qspi_priv *priv, int is_on)
240{
241 u32 gqspi_fifo_reg = 0;
242
243 if (is_on) {
244 gqspi_fifo_reg = zynqmp_qspi_bus_select(priv);
245 gqspi_fifo_reg |= GQSPI_SPI_MODE_SPI |
246 GQSPI_IMD_DATA_CS_ASSERT;
247 } else {
248 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS;
249 gqspi_fifo_reg |= GQSPI_IMD_DATA_CS_DEASSERT;
250 }
251
252 debug("GFIFO_CMD_CS: 0x%x\n", gqspi_fifo_reg);
253
254 zynqmp_qspi_fill_gen_fifo(priv, gqspi_fifo_reg);
255}
256
257void zynqmp_qspi_set_tapdelay(struct udevice *bus, u32 baudrateval)
258{
Simon Glass95588622020-12-22 19:30:28 -0700259 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530260 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
261 struct zynqmp_qspi_regs *regs = priv->regs;
262 u32 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate;
263 u32 reqhz = 0;
264
265 clk_rate = plat->frequency;
266 reqhz = (clk_rate / (GQSPI_BAUD_DIV_SHIFT << baudrateval));
267
268 debug("%s, req_hz:%d, clk_rate:%d, baudrateval:%d\n",
269 __func__, reqhz, clk_rate, baudrateval);
270
271 if (reqhz < GQSPI_FREQ_40MHZ) {
272 zynqmp_mmio_read(IOU_TAPDLY_BYPASS_OFST, &tapdlybypass);
273 tapdlybypass |= (TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
274 TAP_DLY_BYPASS_LQSPI_RX_SHIFT);
Siva Durga Prasad Paladugu05ddbdf2019-03-07 16:08:48 +0530275 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530276 zynqmp_mmio_read(IOU_TAPDLY_BYPASS_OFST, &tapdlybypass);
277 tapdlybypass |= (TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
278 TAP_DLY_BYPASS_LQSPI_RX_SHIFT);
279 lpbkdlyadj = readl(&regs->lpbkdly);
280 lpbkdlyadj |= (GQSPI_LPBK_DLY_ADJ_LPBK_MASK);
281 datadlyadj = readl(&regs->gqspidlyadj);
282 datadlyadj |= ((GQSPI_USE_DATA_DLY << GQSPI_USE_DATA_DLY_SHIFT)
283 | (GQSPI_DATA_DLY_ADJ_VALUE <<
284 GQSPI_DATA_DLY_ADJ_SHIFT));
Siva Durga Prasad Paladugu05ddbdf2019-03-07 16:08:48 +0530285 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530286 lpbkdlyadj = readl(&regs->lpbkdly);
287 lpbkdlyadj |= ((GQSPI_LPBK_DLY_ADJ_LPBK_MASK) |
288 GQSPI_LPBK_DLY_ADJ_DLY_0);
289 }
290
291 zynqmp_mmio_write(IOU_TAPDLY_BYPASS_OFST, IOU_TAPDLY_BYPASS_MASK,
292 tapdlybypass);
293 writel(lpbkdlyadj, &regs->lpbkdly);
294 writel(datadlyadj, &regs->gqspidlyadj);
295}
296
297static int zynqmp_qspi_set_speed(struct udevice *bus, uint speed)
298{
Simon Glass95588622020-12-22 19:30:28 -0700299 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530300 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
301 struct zynqmp_qspi_regs *regs = priv->regs;
302 u32 confr;
303 u8 baud_rate_val = 0;
304
305 debug("%s\n", __func__);
306 if (speed > plat->frequency)
307 speed = plat->frequency;
308
309 /* Set the clock frequency */
310 confr = readl(&regs->confr);
311 if (speed == 0) {
312 /* Set baudrate x8, if the freq is 0 */
313 baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL;
314 } else if (plat->speed_hz != speed) {
315 while ((baud_rate_val < 8) &&
316 ((plat->frequency /
317 (2 << baud_rate_val)) > speed))
318 baud_rate_val++;
319
320 if (baud_rate_val > GQSPI_MAX_BAUD_RATE_VAL)
321 baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL;
322
323 plat->speed_hz = plat->frequency / (2 << baud_rate_val);
324 }
325 confr &= ~GQSPI_BAUD_DIV_MASK;
326 confr |= (baud_rate_val << 3);
327 writel(confr, &regs->confr);
328
329 zynqmp_qspi_set_tapdelay(bus, baud_rate_val);
330 debug("regs=%p, speed=%d\n", priv->regs, plat->speed_hz);
331
332 return 0;
333}
334
335static int zynqmp_qspi_probe(struct udevice *bus)
336{
Simon Glassb75b15b2020-12-03 16:55:23 -0700337 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530338 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
339 struct clk clk;
340 unsigned long clock;
341 int ret;
342
343 debug("%s: bus:%p, priv:%p\n", __func__, bus, priv);
344
345 priv->regs = plat->regs;
346 priv->dma_regs = plat->dma_regs;
347
348 ret = clk_get_by_index(bus, 0, &clk);
349 if (ret < 0) {
Sean Anderson241232a2020-09-15 10:45:12 -0400350 dev_err(bus, "failed to get clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530351 return ret;
352 }
353
354 clock = clk_get_rate(&clk);
355 if (IS_ERR_VALUE(clock)) {
Sean Anderson241232a2020-09-15 10:45:12 -0400356 dev_err(bus, "failed to get rate\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530357 return clock;
358 }
359 debug("%s: CLK %ld\n", __func__, clock);
360
361 ret = clk_enable(&clk);
362 if (ret && ret != -ENOSYS) {
Sean Anderson241232a2020-09-15 10:45:12 -0400363 dev_err(bus, "failed to enable clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530364 return ret;
365 }
366 plat->frequency = clock;
367 plat->speed_hz = plat->frequency / 2;
368
369 /* init the zynq spi hw */
370 zynqmp_qspi_init_hw(priv);
371
372 return 0;
373}
374
375static int zynqmp_qspi_set_mode(struct udevice *bus, uint mode)
376{
377 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
378 struct zynqmp_qspi_regs *regs = priv->regs;
379 u32 confr;
380
381 debug("%s\n", __func__);
382 /* Set the SPI Clock phase and polarities */
383 confr = readl(&regs->confr);
384 confr &= ~(GQSPI_CONFIG_CPHA_MASK |
385 GQSPI_CONFIG_CPOL_MASK);
386
387 if (mode & SPI_CPHA)
388 confr |= GQSPI_CONFIG_CPHA_MASK;
389 if (mode & SPI_CPOL)
390 confr |= GQSPI_CONFIG_CPOL_MASK;
391
392 writel(confr, &regs->confr);
393
394 return 0;
395}
396
397static int zynqmp_qspi_fill_tx_fifo(struct zynqmp_qspi_priv *priv, u32 size)
398{
399 u32 data;
400 int ret = 0;
401 struct zynqmp_qspi_regs *regs = priv->regs;
402 u32 *buf = (u32 *)priv->tx_buf;
403 u32 len = size;
404
405 debug("TxFIFO: 0x%x, size: 0x%x\n", readl(&regs->isr),
406 size);
407
408 while (size) {
409 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_TXNFULL_MASK, 1,
410 GQSPI_TIMEOUT, 1);
411 if (ret) {
412 printf("%s: Timeout\n", __func__);
413 return ret;
414 }
415
416 if (size >= 4) {
417 writel(*buf, &regs->txd0r);
418 buf++;
419 size -= 4;
420 } else {
421 switch (size) {
422 case 1:
423 data = *((u8 *)buf);
424 buf += 1;
425 data |= GENMASK(31, 8);
426 break;
427 case 2:
428 data = *((u16 *)buf);
429 buf += 2;
430 data |= GENMASK(31, 16);
431 break;
432 case 3:
T Karthik Reddycc59fc92020-11-19 05:00:36 -0700433 data = *buf;
434 buf += 3;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530435 data |= GENMASK(31, 24);
436 break;
437 }
438 writel(data, &regs->txd0r);
439 size = 0;
440 }
441 }
442
443 priv->tx_buf += len;
444 return 0;
445}
446
447static void zynqmp_qspi_genfifo_cmd(struct zynqmp_qspi_priv *priv)
448{
449 u32 gen_fifo_cmd;
450 u32 bytecount = 0;
451
452 while (priv->len) {
453 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
454 gen_fifo_cmd |= GQSPI_GFIFO_TX | GQSPI_SPI_MODE_SPI;
455 gen_fifo_cmd |= *(u8 *)priv->tx_buf;
456 bytecount++;
457 priv->len--;
458 priv->tx_buf = (u8 *)priv->tx_buf + 1;
459
460 debug("GFIFO_CMD_Cmd = 0x%x\n", gen_fifo_cmd);
461
462 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
463 }
464}
465
466static u32 zynqmp_qspi_calc_exp(struct zynqmp_qspi_priv *priv,
467 u32 *gen_fifo_cmd)
468{
469 u32 expval = 8;
470 u32 len;
471
472 while (1) {
473 if (priv->len > 255) {
474 if (priv->len & (1 << expval)) {
475 *gen_fifo_cmd &= ~GQSPI_GFIFO_IMD_MASK;
476 *gen_fifo_cmd |= GQSPI_GFIFO_EXP_MASK;
477 *gen_fifo_cmd |= expval;
478 priv->len -= (1 << expval);
479 return expval;
480 }
481 expval++;
482 } else {
483 *gen_fifo_cmd &= ~(GQSPI_GFIFO_IMD_MASK |
484 GQSPI_GFIFO_EXP_MASK);
485 *gen_fifo_cmd |= (u8)priv->len;
486 len = (u8)priv->len;
487 priv->len = 0;
488 return len;
489 }
490 }
491}
492
493static int zynqmp_qspi_genfifo_fill_tx(struct zynqmp_qspi_priv *priv)
494{
495 u32 gen_fifo_cmd;
496 u32 len;
497 int ret = 0;
498
499 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
500 gen_fifo_cmd |= GQSPI_GFIFO_TX |
501 GQSPI_GFIFO_DATA_XFR_MASK;
502
503 gen_fifo_cmd |= GQSPI_SPI_MODE_SPI;
504
505 while (priv->len) {
506 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
507 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
508
509 debug("GFIFO_CMD_TX:0x%x\n", gen_fifo_cmd);
510
511 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
512 ret = zynqmp_qspi_fill_tx_fifo(priv,
513 1 << len);
514 else
515 ret = zynqmp_qspi_fill_tx_fifo(priv,
516 len);
517
518 if (ret)
519 return ret;
520 }
521 return ret;
522}
523
524static int zynqmp_qspi_start_dma(struct zynqmp_qspi_priv *priv,
525 u32 gen_fifo_cmd, u32 *buf)
526{
527 u32 addr;
528 u32 size, len;
529 u32 actuallen = priv->len;
530 int ret = 0;
531 struct zynqmp_qspi_dma_regs *dma_regs = priv->dma_regs;
532
533 writel((unsigned long)buf, &dma_regs->dmadst);
534 writel(roundup(priv->len, ARCH_DMA_MINALIGN), &dma_regs->dmasize);
535 writel(GQSPI_DMA_DST_I_STS_MASK, &dma_regs->dmaier);
536 addr = (unsigned long)buf;
537 size = roundup(priv->len, ARCH_DMA_MINALIGN);
538 flush_dcache_range(addr, addr + size);
539
540 while (priv->len) {
541 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
542 if (!(gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK) &&
543 (len % ARCH_DMA_MINALIGN)) {
544 gen_fifo_cmd &= ~GENMASK(7, 0);
545 gen_fifo_cmd |= roundup(len, ARCH_DMA_MINALIGN);
546 }
547 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
548
549 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
550 }
551
552 ret = wait_for_bit_le32(&dma_regs->dmaisr, GQSPI_DMA_DST_I_STS_DONE,
553 1, GQSPI_TIMEOUT, 1);
554 if (ret) {
555 printf("DMA Timeout:0x%x\n", readl(&dma_regs->dmaisr));
556 return -ETIMEDOUT;
557 }
558
559 writel(GQSPI_DMA_DST_I_STS_DONE, &dma_regs->dmaisr);
560
561 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
562 (unsigned long)buf, (unsigned long)priv->rx_buf, *buf,
563 actuallen);
564
565 if (buf != priv->rx_buf)
566 memcpy(priv->rx_buf, buf, actuallen);
567
568 return 0;
569}
570
571static int zynqmp_qspi_genfifo_fill_rx(struct zynqmp_qspi_priv *priv)
572{
573 u32 gen_fifo_cmd;
574 u32 *buf;
575 u32 actuallen = priv->len;
576
577 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
578 gen_fifo_cmd |= GQSPI_GFIFO_RX |
579 GQSPI_GFIFO_DATA_XFR_MASK;
580
581 gen_fifo_cmd |= GQSPI_SPI_MODE_SPI;
582
583 /*
584 * Check if receive buffer is aligned to 4 byte and length
585 * is multiples of four byte as we are using dma to receive.
586 */
587 if (!((unsigned long)priv->rx_buf & (GQSPI_DMA_ALIGN - 1)) &&
588 !(actuallen % GQSPI_DMA_ALIGN)) {
589 buf = (u32 *)priv->rx_buf;
590 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
591 }
592
593 ALLOC_CACHE_ALIGN_BUFFER(u8, tmp, roundup(priv->len,
594 GQSPI_DMA_ALIGN));
595 buf = (u32 *)tmp;
596 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
597}
598
599static int zynqmp_qspi_start_transfer(struct zynqmp_qspi_priv *priv)
600{
601 int ret = 0;
602
603 if (priv->is_inst) {
604 if (priv->tx_buf)
605 zynqmp_qspi_genfifo_cmd(priv);
606 else
607 return -EINVAL;
608 } else {
609 if (priv->tx_buf)
610 ret = zynqmp_qspi_genfifo_fill_tx(priv);
611 else if (priv->rx_buf)
612 ret = zynqmp_qspi_genfifo_fill_rx(priv);
613 else
614 return -EINVAL;
615 }
616 return ret;
617}
618
619static int zynqmp_qspi_transfer(struct zynqmp_qspi_priv *priv)
620{
621 static unsigned int cs_change = 1;
622 int status = 0;
623
624 debug("%s\n", __func__);
625
626 while (1) {
627 /* Select the chip if required */
628 if (cs_change)
629 zynqmp_qspi_chipselect(priv, 1);
630
631 cs_change = priv->cs_change;
632
633 if (!priv->tx_buf && !priv->rx_buf && priv->len) {
634 status = -EINVAL;
635 break;
636 }
637
638 /* Request the transfer */
639 if (priv->len) {
640 status = zynqmp_qspi_start_transfer(priv);
641 priv->is_inst = 0;
642 if (status < 0)
643 break;
644 }
645
646 if (cs_change)
647 /* Deselect the chip */
648 zynqmp_qspi_chipselect(priv, 0);
649 break;
650 }
651
652 return status;
653}
654
655static int zynqmp_qspi_claim_bus(struct udevice *dev)
656{
657 struct udevice *bus = dev->parent;
658 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
659 struct zynqmp_qspi_regs *regs = priv->regs;
660
661 writel(GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
662
663 return 0;
664}
665
666static int zynqmp_qspi_release_bus(struct udevice *dev)
667{
668 struct udevice *bus = dev->parent;
669 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
670 struct zynqmp_qspi_regs *regs = priv->regs;
671
672 writel(~GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
673
674 return 0;
675}
676
677int zynqmp_qspi_xfer(struct udevice *dev, unsigned int bitlen, const void *dout,
678 void *din, unsigned long flags)
679{
680 struct udevice *bus = dev->parent;
681 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
682
683 debug("%s: priv: 0x%08lx bitlen: %d dout: 0x%08lx ", __func__,
684 (unsigned long)priv, bitlen, (unsigned long)dout);
685 debug("din: 0x%08lx flags: 0x%lx\n", (unsigned long)din, flags);
686
687 priv->tx_buf = dout;
688 priv->rx_buf = din;
689 priv->len = bitlen / 8;
690
691 /*
692 * Assume that the beginning of a transfer with bits to
693 * transmit must contain a device command.
694 */
695 if (dout && flags & SPI_XFER_BEGIN)
696 priv->is_inst = 1;
697 else
698 priv->is_inst = 0;
699
700 if (flags & SPI_XFER_END)
701 priv->cs_change = 1;
702 else
703 priv->cs_change = 0;
704
705 zynqmp_qspi_transfer(priv);
706
707 return 0;
708}
709
710static const struct dm_spi_ops zynqmp_qspi_ops = {
711 .claim_bus = zynqmp_qspi_claim_bus,
712 .release_bus = zynqmp_qspi_release_bus,
713 .xfer = zynqmp_qspi_xfer,
714 .set_speed = zynqmp_qspi_set_speed,
715 .set_mode = zynqmp_qspi_set_mode,
716};
717
718static const struct udevice_id zynqmp_qspi_ids[] = {
719 { .compatible = "xlnx,zynqmp-qspi-1.0" },
Michal Simeked373eb2018-11-29 08:48:28 +0100720 { .compatible = "xlnx,versal-qspi-1.0" },
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530721 { }
722};
723
724U_BOOT_DRIVER(zynqmp_qspi) = {
725 .name = "zynqmp_qspi",
726 .id = UCLASS_SPI,
727 .of_match = zynqmp_qspi_ids,
728 .ops = &zynqmp_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700729 .of_to_plat = zynqmp_qspi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700730 .plat_auto = sizeof(struct zynqmp_qspi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700731 .priv_auto = sizeof(struct zynqmp_qspi_priv),
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530732 .probe = zynqmp_qspi_probe,
733};