blob: 4e718c545c6459d64d79f6f09f82e295cdf6a77e [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>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053013#include <asm/io.h>
14#include <clk.h>
15#include <dm.h>
16#include <malloc.h>
17#include <memalign.h>
18#include <spi.h>
Brandon Maier4d9cce72021-01-20 10:39:46 -060019#include <spi-mem.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053020#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>
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -060025#include <linux/sizes.h>
Ashok Reddy Somae3c77a62022-08-25 06:59:01 -060026#include <zynqmp_firmware.h>
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053027
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)
33
34/*
35 * QSPI Interrupt Registers bit Masks
36 *
37 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
38 * bit definitions.
39 */
40#define GQSPI_IXR_TXNFULL_MASK 0x00000004 /* QSPI TX FIFO Overflow */
41#define GQSPI_IXR_TXFULL_MASK 0x00000008 /* QSPI TX FIFO is full */
Ashok Reddy Soma26f77d72021-10-19 19:43:00 +053042#define GQSPI_IXR_TXFIFOEMPTY_MASK 0x00000100 /* QSPI TX FIFO is Empty */
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053043#define GQSPI_IXR_RXNEMTY_MASK 0x00000010 /* QSPI RX FIFO Not Empty */
44#define GQSPI_IXR_GFEMTY_MASK 0x00000080 /* QSPI Generic FIFO Empty */
Ashok Reddy Soma2af829f2021-05-25 06:36:27 -060045#define GQSPI_IXR_GFNFULL_MASK 0x00000200 /* QSPI GENFIFO not full */
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053046#define GQSPI_IXR_ALL_MASK (GQSPI_IXR_TXNFULL_MASK | \
47 GQSPI_IXR_RXNEMTY_MASK)
48
49/*
50 * QSPI Enable Register bit Masks
51 *
52 * This register is used to enable or disable the QSPI controller
53 */
54#define GQSPI_ENABLE_ENABLE_MASK 0x00000001 /* QSPI Enable Bit Mask */
55
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
79#define MODEBITS 0x6
80
81#define GQSPI_GFIFO_SELECT BIT(0)
82#define GQSPI_FIFO_THRESHOLD 1
Ashok Reddy Soma822a2432021-08-20 07:43:17 -060083#define GQSPI_GENFIFO_THRESHOLD 31
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +053084
85#define SPI_XFER_ON_BOTH 0
86#define SPI_XFER_ON_LOWER 1
87#define SPI_XFER_ON_UPPER 2
88
89#define GQSPI_DMA_ALIGN 0x4
90#define GQSPI_MAX_BAUD_RATE_VAL 7
91#define GQSPI_DFLT_BAUD_RATE_VAL 2
92
93#define GQSPI_TIMEOUT 100000000
94
95#define GQSPI_BAUD_DIV_SHIFT 2
96#define GQSPI_LPBK_DLY_ADJ_LPBK_SHIFT 5
97#define GQSPI_LPBK_DLY_ADJ_DLY_1 0x2
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
Ashok Reddy Somae3c77a62022-08-25 06:59:01 -0600107#define IOU_TAPDLY_BYPASS_OFST !IS_ENABLED(CONFIG_ARCH_VERSAL) ? \
108 0xFF180390 : 0xF103003C
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530109#define GQSPI_LPBK_DLY_ADJ_LPBK_MASK 0x00000020
Ashok Reddy Somae3c77a62022-08-25 06:59:01 -0600110#define GQSPI_FREQ_37_5MHZ 37500000
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530111#define GQSPI_FREQ_40MHZ 40000000
112#define GQSPI_FREQ_100MHZ 100000000
113#define GQSPI_FREQ_150MHZ 150000000
114#define IOU_TAPDLY_BYPASS_MASK 0x7
115
116#define GQSPI_REG_OFFSET 0x100
117#define GQSPI_DMA_REG_OFFSET 0x800
118
119/* QSPI register offsets */
120struct zynqmp_qspi_regs {
121 u32 confr; /* 0x00 */
122 u32 isr; /* 0x04 */
123 u32 ier; /* 0x08 */
124 u32 idisr; /* 0x0C */
125 u32 imaskr; /* 0x10 */
126 u32 enbr; /* 0x14 */
127 u32 dr; /* 0x18 */
128 u32 txd0r; /* 0x1C */
129 u32 drxr; /* 0x20 */
130 u32 sicr; /* 0x24 */
131 u32 txftr; /* 0x28 */
132 u32 rxftr; /* 0x2C */
133 u32 gpior; /* 0x30 */
134 u32 reserved0; /* 0x34 */
135 u32 lpbkdly; /* 0x38 */
136 u32 reserved1; /* 0x3C */
137 u32 genfifo; /* 0x40 */
138 u32 gqspisel; /* 0x44 */
139 u32 reserved2; /* 0x48 */
140 u32 gqfifoctrl; /* 0x4C */
141 u32 gqfthr; /* 0x50 */
142 u32 gqpollcfg; /* 0x54 */
143 u32 gqpollto; /* 0x58 */
144 u32 gqxfersts; /* 0x5C */
145 u32 gqfifosnap; /* 0x60 */
146 u32 gqrxcpy; /* 0x64 */
147 u32 reserved3[36]; /* 0x68 */
148 u32 gqspidlyadj; /* 0xF8 */
149};
150
151struct zynqmp_qspi_dma_regs {
152 u32 dmadst; /* 0x00 */
153 u32 dmasize; /* 0x04 */
154 u32 dmasts; /* 0x08 */
155 u32 dmactrl; /* 0x0C */
156 u32 reserved0; /* 0x10 */
157 u32 dmaisr; /* 0x14 */
158 u32 dmaier; /* 0x18 */
159 u32 dmaidr; /* 0x1C */
160 u32 dmaimr; /* 0x20 */
161 u32 dmactrl2; /* 0x24 */
162 u32 dmadstmsb; /* 0x28 */
163};
164
Simon Glassb75b15b2020-12-03 16:55:23 -0700165struct zynqmp_qspi_plat {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530166 struct zynqmp_qspi_regs *regs;
167 struct zynqmp_qspi_dma_regs *dma_regs;
168 u32 frequency;
169 u32 speed_hz;
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600170 unsigned int io_mode;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530171};
172
173struct zynqmp_qspi_priv {
174 struct zynqmp_qspi_regs *regs;
175 struct zynqmp_qspi_dma_regs *dma_regs;
176 const void *tx_buf;
177 void *rx_buf;
178 unsigned int len;
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600179 unsigned int io_mode;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530180 int bytes_to_transfer;
181 int bytes_to_receive;
Brandon Maier4d9cce72021-01-20 10:39:46 -0600182 const struct spi_mem_op *op;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530183};
184
Simon Glassaad29ae2020-12-03 16:55:21 -0700185static int zynqmp_qspi_of_to_plat(struct udevice *bus)
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530186{
Simon Glass95588622020-12-22 19:30:28 -0700187 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530188
189 debug("%s\n", __func__);
190
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900191 plat->regs = (struct zynqmp_qspi_regs *)(dev_read_addr(bus) +
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530192 GQSPI_REG_OFFSET);
193 plat->dma_regs = (struct zynqmp_qspi_dma_regs *)
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900194 (dev_read_addr(bus) + GQSPI_DMA_REG_OFFSET);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530195
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600196 plat->io_mode = dev_read_bool(bus, "has-io-mode");
197
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530198 return 0;
199}
200
201static void zynqmp_qspi_init_hw(struct zynqmp_qspi_priv *priv)
202{
203 u32 config_reg;
204 struct zynqmp_qspi_regs *regs = priv->regs;
205
206 writel(GQSPI_GFIFO_SELECT, &regs->gqspisel);
207 writel(GQSPI_GFIFO_ALL_INT_MASK, &regs->idisr);
208 writel(GQSPI_FIFO_THRESHOLD, &regs->txftr);
209 writel(GQSPI_FIFO_THRESHOLD, &regs->rxftr);
Ashok Reddy Soma822a2432021-08-20 07:43:17 -0600210 writel(GQSPI_GENFIFO_THRESHOLD, &regs->gqfthr);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530211 writel(GQSPI_GFIFO_ALL_INT_MASK, &regs->isr);
Ashok Reddy Soma822a2432021-08-20 07:43:17 -0600212 writel(~GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530213
214 config_reg = readl(&regs->confr);
215 config_reg &= ~(GQSPI_GFIFO_STRT_MODE_MASK |
216 GQSPI_CONFIG_MODE_EN_MASK);
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600217 config_reg |= GQSPI_GFIFO_WP_HOLD | GQSPI_DFLT_BAUD_RATE_DIV;
218 config_reg |= GQSPI_GFIFO_STRT_MODE_MASK;
219 if (!priv->io_mode)
220 config_reg |= GQSPI_CONFIG_DMA_MODE;
221
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530222 writel(config_reg, &regs->confr);
223
224 writel(GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
225}
226
227static u32 zynqmp_qspi_bus_select(struct zynqmp_qspi_priv *priv)
228{
229 u32 gqspi_fifo_reg = 0;
230
231 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS |
232 GQSPI_GFIFO_CS_LOWER;
233
234 return gqspi_fifo_reg;
235}
236
Brandon Maier4d9cce72021-01-20 10:39:46 -0600237static u32 zynqmp_qspi_genfifo_mode(u8 buswidth)
238{
239 switch (buswidth) {
240 case 1:
241 return GQSPI_SPI_MODE_SPI;
242 case 2:
243 return GQSPI_SPI_MODE_DUAL_SPI;
244 case 4:
245 return GQSPI_SPI_MODE_QSPI;
246 default:
247 debug("Unsupported bus width %u\n", buswidth);
248 return GQSPI_SPI_MODE_SPI;
249 }
250}
251
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530252static void zynqmp_qspi_fill_gen_fifo(struct zynqmp_qspi_priv *priv,
253 u32 gqspi_fifo_reg)
254{
255 struct zynqmp_qspi_regs *regs = priv->regs;
Ashok Reddy Soma2af829f2021-05-25 06:36:27 -0600256 u32 config_reg, ier;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530257 int ret = 0;
258
Ashok Reddy Soma1c35adc2021-08-20 07:43:16 -0600259 writel(gqspi_fifo_reg, &regs->genfifo);
260
Ashok Reddy Soma2af829f2021-05-25 06:36:27 -0600261 config_reg = readl(&regs->confr);
262 /* Manual start if needed */
263 config_reg |= GQSPI_STRT_GEN_FIFO;
264 writel(config_reg, &regs->confr);
265
266 /* Enable interrupts */
267 ier = readl(&regs->ier);
Ashok Reddy Soma1c35adc2021-08-20 07:43:16 -0600268 ier |= GQSPI_IXR_GFEMTY_MASK;
Ashok Reddy Soma2af829f2021-05-25 06:36:27 -0600269 writel(ier, &regs->ier);
270
Ashok Reddy Soma1c35adc2021-08-20 07:43:16 -0600271 /* Wait until the gen fifo is empty to write the new command */
272 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_GFEMTY_MASK, 1,
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530273 GQSPI_TIMEOUT, 1);
274 if (ret)
275 printf("%s Timeout\n", __func__);
276
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530277}
278
279static void zynqmp_qspi_chipselect(struct zynqmp_qspi_priv *priv, int is_on)
280{
281 u32 gqspi_fifo_reg = 0;
282
283 if (is_on) {
284 gqspi_fifo_reg = zynqmp_qspi_bus_select(priv);
285 gqspi_fifo_reg |= GQSPI_SPI_MODE_SPI |
286 GQSPI_IMD_DATA_CS_ASSERT;
287 } else {
288 gqspi_fifo_reg = GQSPI_GFIFO_LOW_BUS;
289 gqspi_fifo_reg |= GQSPI_IMD_DATA_CS_DEASSERT;
290 }
291
292 debug("GFIFO_CMD_CS: 0x%x\n", gqspi_fifo_reg);
293
294 zynqmp_qspi_fill_gen_fifo(priv, gqspi_fifo_reg);
295}
296
297void zynqmp_qspi_set_tapdelay(struct udevice *bus, u32 baudrateval)
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 tapdlybypass = 0, lpbkdlyadj = 0, datadlyadj = 0, clk_rate;
303 u32 reqhz = 0;
304
305 clk_rate = plat->frequency;
306 reqhz = (clk_rate / (GQSPI_BAUD_DIV_SHIFT << baudrateval));
307
308 debug("%s, req_hz:%d, clk_rate:%d, baudrateval:%d\n",
309 __func__, reqhz, clk_rate, baudrateval);
310
Ashok Reddy Somae3c77a62022-08-25 06:59:01 -0600311 if (!IS_ENABLED(CONFIG_ARCH_VERSAL)) {
312 if (reqhz <= GQSPI_FREQ_40MHZ) {
313 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
314 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
315 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
316 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
317 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
318 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK;
319 datadlyadj = (GQSPI_USE_DATA_DLY <<
320 GQSPI_USE_DATA_DLY_SHIFT) |
321 (GQSPI_DATA_DLY_ADJ_VALUE <<
322 GQSPI_DATA_DLY_ADJ_SHIFT);
323 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
324 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK |
325 GQSPI_LPBK_DLY_ADJ_DLY_0;
326 }
327 zynqmp_mmio_write(IOU_TAPDLY_BYPASS_OFST,
328 IOU_TAPDLY_BYPASS_MASK, tapdlybypass);
329 } else {
330 if (reqhz <= GQSPI_FREQ_37_5MHZ) {
331 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
332 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
333 } else if (reqhz <= GQSPI_FREQ_100MHZ) {
334 tapdlybypass = TAP_DLY_BYPASS_LQSPI_RX_VALUE <<
335 TAP_DLY_BYPASS_LQSPI_RX_SHIFT;
336 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK;
337 datadlyadj = GQSPI_USE_DATA_DLY <<
338 GQSPI_USE_DATA_DLY_SHIFT;
339 } else if (reqhz <= GQSPI_FREQ_150MHZ) {
340 lpbkdlyadj = GQSPI_LPBK_DLY_ADJ_LPBK_MASK |
341 (GQSPI_LPBK_DLY_ADJ_DLY_1 <<
342 GQSPI_LPBK_DLY_ADJ_DLY_1_SHIFT);
343 }
344 writel(tapdlybypass, IOU_TAPDLY_BYPASS_OFST);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530345 }
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530346 writel(lpbkdlyadj, &regs->lpbkdly);
347 writel(datadlyadj, &regs->gqspidlyadj);
348}
349
350static int zynqmp_qspi_set_speed(struct udevice *bus, uint speed)
351{
Simon Glass95588622020-12-22 19:30:28 -0700352 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530353 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
354 struct zynqmp_qspi_regs *regs = priv->regs;
355 u32 confr;
356 u8 baud_rate_val = 0;
357
358 debug("%s\n", __func__);
359 if (speed > plat->frequency)
360 speed = plat->frequency;
361
Brandon Maierb8003d52021-01-20 14:28:30 -0600362 if (plat->speed_hz != speed) {
363 /* Set the clock frequency */
364 /* If speed == 0, default to lowest speed */
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530365 while ((baud_rate_val < 8) &&
366 ((plat->frequency /
367 (2 << baud_rate_val)) > speed))
368 baud_rate_val++;
369
370 if (baud_rate_val > GQSPI_MAX_BAUD_RATE_VAL)
371 baud_rate_val = GQSPI_DFLT_BAUD_RATE_VAL;
372
373 plat->speed_hz = plat->frequency / (2 << baud_rate_val);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530374
Brandon Maierb8003d52021-01-20 14:28:30 -0600375 confr = readl(&regs->confr);
376 confr &= ~GQSPI_BAUD_DIV_MASK;
377 confr |= (baud_rate_val << 3);
378 writel(confr, &regs->confr);
379 zynqmp_qspi_set_tapdelay(bus, baud_rate_val);
380
381 debug("regs=%p, speed=%d\n", priv->regs, plat->speed_hz);
382 }
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530383
384 return 0;
385}
386
387static int zynqmp_qspi_probe(struct udevice *bus)
388{
Simon Glassb75b15b2020-12-03 16:55:23 -0700389 struct zynqmp_qspi_plat *plat = dev_get_plat(bus);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530390 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
391 struct clk clk;
392 unsigned long clock;
393 int ret;
394
395 debug("%s: bus:%p, priv:%p\n", __func__, bus, priv);
396
397 priv->regs = plat->regs;
398 priv->dma_regs = plat->dma_regs;
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600399 priv->io_mode = plat->io_mode;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530400
401 ret = clk_get_by_index(bus, 0, &clk);
402 if (ret < 0) {
Sean Anderson241232a2020-09-15 10:45:12 -0400403 dev_err(bus, "failed to get clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530404 return ret;
405 }
406
407 clock = clk_get_rate(&clk);
408 if (IS_ERR_VALUE(clock)) {
Sean Anderson241232a2020-09-15 10:45:12 -0400409 dev_err(bus, "failed to get rate\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530410 return clock;
411 }
412 debug("%s: CLK %ld\n", __func__, clock);
413
414 ret = clk_enable(&clk);
Michal Simek41710952021-02-09 15:28:15 +0100415 if (ret) {
Sean Anderson241232a2020-09-15 10:45:12 -0400416 dev_err(bus, "failed to enable clock\n");
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530417 return ret;
418 }
419 plat->frequency = clock;
420 plat->speed_hz = plat->frequency / 2;
421
422 /* init the zynq spi hw */
423 zynqmp_qspi_init_hw(priv);
424
425 return 0;
426}
427
428static int zynqmp_qspi_set_mode(struct udevice *bus, uint mode)
429{
430 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
431 struct zynqmp_qspi_regs *regs = priv->regs;
432 u32 confr;
433
434 debug("%s\n", __func__);
435 /* Set the SPI Clock phase and polarities */
436 confr = readl(&regs->confr);
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600437 confr &= ~(GQSPI_CONFIG_CPHA_MASK | GQSPI_CONFIG_CPOL_MASK);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530438
439 if (mode & SPI_CPHA)
440 confr |= GQSPI_CONFIG_CPHA_MASK;
441 if (mode & SPI_CPOL)
442 confr |= GQSPI_CONFIG_CPOL_MASK;
443
444 writel(confr, &regs->confr);
445
446 return 0;
447}
448
449static int zynqmp_qspi_fill_tx_fifo(struct zynqmp_qspi_priv *priv, u32 size)
450{
451 u32 data;
452 int ret = 0;
453 struct zynqmp_qspi_regs *regs = priv->regs;
454 u32 *buf = (u32 *)priv->tx_buf;
455 u32 len = size;
456
457 debug("TxFIFO: 0x%x, size: 0x%x\n", readl(&regs->isr),
458 size);
459
460 while (size) {
461 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_TXNFULL_MASK, 1,
462 GQSPI_TIMEOUT, 1);
463 if (ret) {
464 printf("%s: Timeout\n", __func__);
465 return ret;
466 }
467
468 if (size >= 4) {
469 writel(*buf, &regs->txd0r);
470 buf++;
471 size -= 4;
472 } else {
473 switch (size) {
474 case 1:
475 data = *((u8 *)buf);
476 buf += 1;
477 data |= GENMASK(31, 8);
478 break;
479 case 2:
480 data = *((u16 *)buf);
481 buf += 2;
482 data |= GENMASK(31, 16);
483 break;
484 case 3:
T Karthik Reddycc59fc92020-11-19 05:00:36 -0700485 data = *buf;
486 buf += 3;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530487 data |= GENMASK(31, 24);
488 break;
489 }
490 writel(data, &regs->txd0r);
491 size = 0;
492 }
493 }
494
Ashok Reddy Soma26f77d72021-10-19 19:43:00 +0530495 ret = wait_for_bit_le32(&regs->isr, GQSPI_IXR_TXFIFOEMPTY_MASK, 1,
496 GQSPI_TIMEOUT, 1);
497 if (ret) {
498 printf("%s: Timeout\n", __func__);
499 return ret;
500 }
501
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530502 priv->tx_buf += len;
503 return 0;
504}
505
506static void zynqmp_qspi_genfifo_cmd(struct zynqmp_qspi_priv *priv)
507{
Brandon Maier4d9cce72021-01-20 10:39:46 -0600508 const struct spi_mem_op *op = priv->op;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530509 u32 gen_fifo_cmd;
Brandon Maier4d9cce72021-01-20 10:39:46 -0600510 u8 i, dummy_cycles, addr;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530511
Brandon Maier4d9cce72021-01-20 10:39:46 -0600512 /* Send opcode */
513 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
514 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->cmd.buswidth);
515 gen_fifo_cmd |= GQSPI_GFIFO_TX;
516 gen_fifo_cmd |= op->cmd.opcode;
517 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
518
519 /* Send address */
520 for (i = 0; i < op->addr.nbytes; i++) {
521 addr = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
522
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530523 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600524 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->addr.buswidth);
525 gen_fifo_cmd |= GQSPI_GFIFO_TX;
526 gen_fifo_cmd |= addr;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530527
528 debug("GFIFO_CMD_Cmd = 0x%x\n", gen_fifo_cmd);
529
530 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
531 }
Brandon Maier4d9cce72021-01-20 10:39:46 -0600532
533 /* Send dummy */
534 if (op->dummy.nbytes) {
535 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
536
537 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
538 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(op->dummy.buswidth);
539 gen_fifo_cmd &= ~(GQSPI_GFIFO_TX | GQSPI_GFIFO_RX);
540 gen_fifo_cmd |= GQSPI_GFIFO_DATA_XFR_MASK;
541 gen_fifo_cmd |= dummy_cycles;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530542 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
543 }
544}
545
546static u32 zynqmp_qspi_calc_exp(struct zynqmp_qspi_priv *priv,
547 u32 *gen_fifo_cmd)
548{
549 u32 expval = 8;
550 u32 len;
551
552 while (1) {
553 if (priv->len > 255) {
554 if (priv->len & (1 << expval)) {
555 *gen_fifo_cmd &= ~GQSPI_GFIFO_IMD_MASK;
556 *gen_fifo_cmd |= GQSPI_GFIFO_EXP_MASK;
557 *gen_fifo_cmd |= expval;
558 priv->len -= (1 << expval);
559 return expval;
560 }
561 expval++;
562 } else {
563 *gen_fifo_cmd &= ~(GQSPI_GFIFO_IMD_MASK |
564 GQSPI_GFIFO_EXP_MASK);
565 *gen_fifo_cmd |= (u8)priv->len;
566 len = (u8)priv->len;
567 priv->len = 0;
568 return len;
569 }
570 }
571}
572
573static int zynqmp_qspi_genfifo_fill_tx(struct zynqmp_qspi_priv *priv)
574{
575 u32 gen_fifo_cmd;
576 u32 len;
577 int ret = 0;
578
579 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600580 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600581 gen_fifo_cmd |= GQSPI_GFIFO_TX | GQSPI_GFIFO_DATA_XFR_MASK;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530582
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530583 while (priv->len) {
584 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
585 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
586
587 debug("GFIFO_CMD_TX:0x%x\n", gen_fifo_cmd);
588
589 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600590 ret = zynqmp_qspi_fill_tx_fifo(priv, 1 << len);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530591 else
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600592 ret = zynqmp_qspi_fill_tx_fifo(priv, len);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530593
594 if (ret)
595 return ret;
596 }
597 return ret;
598}
599
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600600static int zynqmp_qspi_start_io(struct zynqmp_qspi_priv *priv,
601 u32 gen_fifo_cmd, u32 *buf)
602{
603 u32 len;
604 u32 actuallen = priv->len;
605 u32 config_reg, ier, isr;
606 u32 timeout = GQSPI_TIMEOUT;
607 struct zynqmp_qspi_regs *regs = priv->regs;
608 u32 last_bits;
609 u32 *traverse = buf;
610
611 while (priv->len) {
612 len = zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
613 /* If exponent bit is set, reset immediate to be 2^len */
614 if (gen_fifo_cmd & GQSPI_GFIFO_EXP_MASK)
615 priv->bytes_to_receive = (1 << len);
616 else
617 priv->bytes_to_receive = len;
618 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
619 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
620 /* Manual start */
621 config_reg = readl(&regs->confr);
622 config_reg |= GQSPI_STRT_GEN_FIFO;
623 writel(config_reg, &regs->confr);
624 /* Enable RX interrupts for IO mode */
625 ier = readl(&regs->ier);
626 ier |= GQSPI_IXR_ALL_MASK;
627 writel(ier, &regs->ier);
628 while (priv->bytes_to_receive && timeout) {
629 isr = readl(&regs->isr);
630 if (isr & GQSPI_IXR_RXNEMTY_MASK) {
631 if (priv->bytes_to_receive >= 4) {
632 *traverse = readl(&regs->drxr);
633 traverse++;
634 priv->bytes_to_receive -= 4;
635 } else {
636 last_bits = readl(&regs->drxr);
637 memcpy(traverse, &last_bits,
638 priv->bytes_to_receive);
639 priv->bytes_to_receive = 0;
640 }
641 timeout = GQSPI_TIMEOUT;
642 } else {
643 udelay(1);
644 timeout--;
645 }
646 }
647
648 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
649 (unsigned long)buf, (unsigned long)priv->rx_buf,
650 *buf, actuallen);
651 if (!timeout) {
652 printf("IO timeout: %d\n", readl(&regs->isr));
653 return -1;
654 }
655 }
656
657 return 0;
658}
659
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530660static int zynqmp_qspi_start_dma(struct zynqmp_qspi_priv *priv,
661 u32 gen_fifo_cmd, u32 *buf)
662{
663 u32 addr;
Ashok Reddy Soma822a2432021-08-20 07:43:17 -0600664 u32 size;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530665 u32 actuallen = priv->len;
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600666 u32 totallen = priv->len;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530667 int ret = 0;
668 struct zynqmp_qspi_dma_regs *dma_regs = priv->dma_regs;
669
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600670 while (totallen) {
671 if (totallen >= SZ_512M)
672 priv->len = SZ_256M;
673 else
674 priv->len = totallen;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530675
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600676 totallen -= priv->len; /* Save remaining bytes length to read */
677 actuallen = priv->len; /* Actual number of bytes reading */
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530678
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600679 writel((unsigned long)buf, &dma_regs->dmadst);
680 writel(roundup(priv->len, GQSPI_DMA_ALIGN), &dma_regs->dmasize);
681 writel(GQSPI_DMA_DST_I_STS_MASK, &dma_regs->dmaier);
682 addr = (unsigned long)buf;
683 size = roundup(priv->len, GQSPI_DMA_ALIGN);
684 flush_dcache_range(addr, addr + size);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530685
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600686 while (priv->len) {
687 zynqmp_qspi_calc_exp(priv, &gen_fifo_cmd);
688 zynqmp_qspi_fill_gen_fifo(priv, gen_fifo_cmd);
689
690 debug("GFIFO_CMD_RX:0x%x\n", gen_fifo_cmd);
691 }
692
693 ret = wait_for_bit_le32(&dma_regs->dmaisr,
694 GQSPI_DMA_DST_I_STS_DONE, 1,
695 GQSPI_TIMEOUT, 1);
696 if (ret) {
697 printf("DMA Timeout:0x%x\n", readl(&dma_regs->dmaisr));
698 return -ETIMEDOUT;
699 }
700
701 writel(GQSPI_DMA_DST_I_STS_DONE, &dma_regs->dmaisr);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530702
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600703 debug("buf:0x%lx, rxbuf:0x%lx, *buf:0x%x len: 0x%x\n",
704 (unsigned long)buf, (unsigned long)priv->rx_buf, *buf,
705 actuallen);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530706
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600707 if (buf != priv->rx_buf)
708 memcpy(priv->rx_buf, buf, actuallen);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530709
Ashok Reddy Soma2d322cc2022-08-25 06:59:04 -0600710 buf = (u32 *)((u8 *)buf + actuallen);
711 priv->rx_buf = (u8 *)priv->rx_buf + actuallen;
712 }
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530713
714 return 0;
715}
716
717static int zynqmp_qspi_genfifo_fill_rx(struct zynqmp_qspi_priv *priv)
718{
719 u32 gen_fifo_cmd;
720 u32 *buf;
721 u32 actuallen = priv->len;
722
723 gen_fifo_cmd = zynqmp_qspi_bus_select(priv);
Brandon Maier4d9cce72021-01-20 10:39:46 -0600724 gen_fifo_cmd |= zynqmp_qspi_genfifo_mode(priv->op->data.buswidth);
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600725 gen_fifo_cmd |= GQSPI_GFIFO_RX | GQSPI_GFIFO_DATA_XFR_MASK;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530726
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530727 /*
728 * Check if receive buffer is aligned to 4 byte and length
729 * is multiples of four byte as we are using dma to receive.
730 */
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600731 if ((!((unsigned long)priv->rx_buf & (GQSPI_DMA_ALIGN - 1)) &&
732 !(actuallen % GQSPI_DMA_ALIGN)) || priv->io_mode) {
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530733 buf = (u32 *)priv->rx_buf;
Ashok Reddy Soma96db8b62022-08-25 06:59:03 -0600734 if (priv->io_mode)
735 return zynqmp_qspi_start_io(priv, gen_fifo_cmd, buf);
736 else
737 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530738 }
739
Ashok Reddy Soma7b4bded2022-08-25 06:59:05 -0600740 ALLOC_CACHE_ALIGN_BUFFER(u8, tmp, roundup(priv->len, GQSPI_DMA_ALIGN));
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530741 buf = (u32 *)tmp;
742 return zynqmp_qspi_start_dma(priv, gen_fifo_cmd, buf);
743}
744
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530745static int zynqmp_qspi_claim_bus(struct udevice *dev)
746{
747 struct udevice *bus = dev->parent;
748 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
749 struct zynqmp_qspi_regs *regs = priv->regs;
750
751 writel(GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
752
753 return 0;
754}
755
756static int zynqmp_qspi_release_bus(struct udevice *dev)
757{
758 struct udevice *bus = dev->parent;
759 struct zynqmp_qspi_priv *priv = dev_get_priv(bus);
760 struct zynqmp_qspi_regs *regs = priv->regs;
761
762 writel(~GQSPI_ENABLE_ENABLE_MASK, &regs->enbr);
763
764 return 0;
765}
766
Brandon Maier4d9cce72021-01-20 10:39:46 -0600767static int zynqmp_qspi_exec_op(struct spi_slave *slave,
768 const struct spi_mem_op *op)
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530769{
Brandon Maier4d9cce72021-01-20 10:39:46 -0600770 struct zynqmp_qspi_priv *priv = dev_get_priv(slave->dev->parent);
771 int ret = 0;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530772
Brandon Maier4d9cce72021-01-20 10:39:46 -0600773 priv->op = op;
774 priv->tx_buf = op->data.buf.out;
775 priv->rx_buf = op->data.buf.in;
776 priv->len = op->data.nbytes;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530777
Brandon Maier4d9cce72021-01-20 10:39:46 -0600778 zynqmp_qspi_chipselect(priv, 1);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530779
Brandon Maier4d9cce72021-01-20 10:39:46 -0600780 /* Send opcode, addr, dummy */
781 zynqmp_qspi_genfifo_cmd(priv);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530782
Brandon Maier4d9cce72021-01-20 10:39:46 -0600783 /* Request the transfer */
784 if (op->data.dir == SPI_MEM_DATA_IN)
785 ret = zynqmp_qspi_genfifo_fill_rx(priv);
786 else if (op->data.dir == SPI_MEM_DATA_OUT)
787 ret = zynqmp_qspi_genfifo_fill_tx(priv);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530788
Brandon Maier4d9cce72021-01-20 10:39:46 -0600789 zynqmp_qspi_chipselect(priv, 0);
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530790
Brandon Maier4d9cce72021-01-20 10:39:46 -0600791 return ret;
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530792}
793
Brandon Maier4d9cce72021-01-20 10:39:46 -0600794static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = {
795 .exec_op = zynqmp_qspi_exec_op,
796};
797
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530798static const struct dm_spi_ops zynqmp_qspi_ops = {
799 .claim_bus = zynqmp_qspi_claim_bus,
800 .release_bus = zynqmp_qspi_release_bus,
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530801 .set_speed = zynqmp_qspi_set_speed,
802 .set_mode = zynqmp_qspi_set_mode,
Brandon Maier4d9cce72021-01-20 10:39:46 -0600803 .mem_ops = &zynqmp_qspi_mem_ops,
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530804};
805
806static const struct udevice_id zynqmp_qspi_ids[] = {
807 { .compatible = "xlnx,zynqmp-qspi-1.0" },
Michal Simeked373eb2018-11-29 08:48:28 +0100808 { .compatible = "xlnx,versal-qspi-1.0" },
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530809 { }
810};
811
812U_BOOT_DRIVER(zynqmp_qspi) = {
813 .name = "zynqmp_qspi",
814 .id = UCLASS_SPI,
815 .of_match = zynqmp_qspi_ids,
816 .ops = &zynqmp_qspi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700817 .of_to_plat = zynqmp_qspi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700818 .plat_auto = sizeof(struct zynqmp_qspi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700819 .priv_auto = sizeof(struct zynqmp_qspi_priv),
Siva Durga Prasad Paladugu76597382018-07-04 17:31:23 +0530820 .probe = zynqmp_qspi_probe,
821};