blob: 2fc28b6bee2b4c45083eb4e2cd7bb0347d5768f6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jagan Teki46d0a992015-08-17 18:38:06 +05302/*
3 * (C) Copyright 2013 Xilinx, Inc.
4 * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
5 *
6 * Xilinx Zynq Quad-SPI(QSPI) controller driver (master mode only)
Jagan Teki46d0a992015-08-17 18:38:06 +05307 */
8
T Karthik Reddyea836be2020-02-04 05:47:45 -07009#include <clk.h>
Jagan Teki46d0a992015-08-17 18:38:06 +053010#include <common.h>
11#include <dm.h>
T Karthik Reddyea836be2020-02-04 05:47:45 -070012#include <dm/device_compat.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Jagan Teki46d0a992015-08-17 18:38:06 +053014#include <malloc.h>
15#include <spi.h>
16#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Jagan Teki46d0a992015-08-17 18:38:06 +053018
19DECLARE_GLOBAL_DATA_PTR;
20
21/* zynq qspi register bit masks ZYNQ_QSPI_<REG>_<BIT>_MASK */
Jagan Teki736b4df2015-10-22 20:40:16 +053022#define ZYNQ_QSPI_CR_IFMODE_MASK BIT(31) /* Flash intrface mode*/
23#define ZYNQ_QSPI_CR_MSA_MASK BIT(15) /* Manual start enb */
24#define ZYNQ_QSPI_CR_MCS_MASK BIT(14) /* Manual chip select */
25#define ZYNQ_QSPI_CR_PCS_MASK BIT(10) /* Peri chip select */
Jagan Teki9cf2ffb2015-10-22 21:06:37 +053026#define ZYNQ_QSPI_CR_FW_MASK GENMASK(7, 6) /* FIFO width */
27#define ZYNQ_QSPI_CR_SS_MASK GENMASK(13, 10) /* Slave Select */
28#define ZYNQ_QSPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
Jagan Teki736b4df2015-10-22 20:40:16 +053029#define ZYNQ_QSPI_CR_CPHA_MASK BIT(2) /* Clock phase */
30#define ZYNQ_QSPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
31#define ZYNQ_QSPI_CR_MSTREN_MASK BIT(0) /* Mode select */
32#define ZYNQ_QSPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
33#define ZYNQ_QSPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
Jagan Teki9cf2ffb2015-10-22 21:06:37 +053034#define ZYNQ_QSPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
Jagan Teki736b4df2015-10-22 20:40:16 +053035#define ZYNQ_QSPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
Nathan Rossi04a44d32015-12-09 00:44:40 +100036#define ZYNQ_QSPI_LQSPICFG_LQMODE_MASK BIT(31) /* Linear QSPI Mode */
Jagan Teki46d0a992015-08-17 18:38:06 +053037
38/* zynq qspi Transmit Data Register */
39#define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst */
40#define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst */
41#define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst */
42#define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst */
43
44#define ZYNQ_QSPI_TXFIFO_THRESHOLD 1 /* Tx FIFO threshold level*/
45#define ZYNQ_QSPI_RXFIFO_THRESHOLD 32 /* Rx FIFO threshold level */
46
47#define ZYNQ_QSPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
48#define ZYNQ_QSPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
49#define ZYNQ_QSPI_CR_SS_SHIFT 10 /* Slave select shift */
50
51#define ZYNQ_QSPI_FIFO_DEPTH 63
Ashok Reddy Somaf44bd3b2020-05-18 01:11:00 -060052#define ZYNQ_QSPI_WAIT (CONFIG_SYS_HZ / 100) /* 10 ms */
Jagan Teki46d0a992015-08-17 18:38:06 +053053
54/* zynq qspi register set */
55struct zynq_qspi_regs {
56 u32 cr; /* 0x00 */
57 u32 isr; /* 0x04 */
58 u32 ier; /* 0x08 */
59 u32 idr; /* 0x0C */
60 u32 imr; /* 0x10 */
61 u32 enr; /* 0x14 */
62 u32 dr; /* 0x18 */
63 u32 txd0r; /* 0x1C */
64 u32 drxr; /* 0x20 */
65 u32 sicr; /* 0x24 */
66 u32 txftr; /* 0x28 */
67 u32 rxftr; /* 0x2C */
68 u32 gpior; /* 0x30 */
69 u32 reserved0[19];
70 u32 txd1r; /* 0x80 */
71 u32 txd2r; /* 0x84 */
72 u32 txd3r; /* 0x88 */
Nathan Rossi04a44d32015-12-09 00:44:40 +100073 u32 reserved1[5];
74 u32 lqspicfg; /* 0xA0 */
75 u32 lqspists; /* 0xA4 */
Jagan Teki46d0a992015-08-17 18:38:06 +053076};
77
78/* zynq qspi platform data */
Simon Glass8a8d24b2020-12-03 16:55:23 -070079struct zynq_qspi_plat {
Jagan Teki46d0a992015-08-17 18:38:06 +053080 struct zynq_qspi_regs *regs;
81 u32 frequency; /* input frequency */
82 u32 speed_hz;
83};
84
85/* zynq qspi priv */
86struct zynq_qspi_priv {
87 struct zynq_qspi_regs *regs;
88 u8 cs;
89 u8 mode;
90 u8 fifo_depth;
91 u32 freq; /* required frequency */
92 const void *tx_buf;
93 void *rx_buf;
94 unsigned len;
95 int bytes_to_transfer;
96 int bytes_to_receive;
97 unsigned int is_inst;
98 unsigned cs_change:1;
99};
100
Simon Glassd1998a92020-12-03 16:55:21 -0700101static int zynq_qspi_of_to_plat(struct udevice *bus)
Jagan Teki46d0a992015-08-17 18:38:06 +0530102{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700103 struct zynq_qspi_plat *plat = bus->plat;
Jagan Teki46d0a992015-08-17 18:38:06 +0530104 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700105 int node = dev_of_offset(bus);
Jagan Teki46d0a992015-08-17 18:38:06 +0530106
107 plat->regs = (struct zynq_qspi_regs *)fdtdec_get_addr(blob,
108 node, "reg");
109
Jagan Teki46d0a992015-08-17 18:38:06 +0530110 return 0;
111}
112
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700113/**
114 * zynq_qspi_init_hw - Initialize the hardware
115 * @priv: Pointer to the zynq_qspi_priv structure
116 *
117 * The default settings of the QSPI controller's configurable parameters on
118 * reset are
119 * - Master mode
120 * - Baud rate divisor is set to 2
121 * - Threshold value for TX FIFO not full interrupt is set to 1
122 * - Flash memory interface mode enabled
123 * - Size of the word to be transferred as 8 bit
124 * This function performs the following actions
125 * - Disable and clear all the interrupts
126 * - Enable manual slave select
127 * - Enable auto start
128 * - Deselect all the chip select lines
129 * - Set the size of the word to be transferred as 32 bit
130 * - Set the little endian mode of TX FIFO and
131 * - Enable the QSPI controller
132 */
Jagan Teki46d0a992015-08-17 18:38:06 +0530133static void zynq_qspi_init_hw(struct zynq_qspi_priv *priv)
134{
135 struct zynq_qspi_regs *regs = priv->regs;
136 u32 confr;
137
138 /* Disable QSPI */
139 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
140
141 /* Disable Interrupts */
142 writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->idr);
143
144 /* Clear the TX and RX threshold reg */
145 writel(ZYNQ_QSPI_TXFIFO_THRESHOLD, &regs->txftr);
146 writel(ZYNQ_QSPI_RXFIFO_THRESHOLD, &regs->rxftr);
147
148 /* Clear the RX FIFO */
149 while (readl(&regs->isr) & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)
150 readl(&regs->drxr);
151
152 /* Clear Interrupts */
153 writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->isr);
154
155 /* Manual slave select and Auto start */
156 confr = readl(&regs->cr);
157 confr &= ~ZYNQ_QSPI_CR_MSA_MASK;
158 confr |= ZYNQ_QSPI_CR_IFMODE_MASK | ZYNQ_QSPI_CR_MCS_MASK |
159 ZYNQ_QSPI_CR_PCS_MASK | ZYNQ_QSPI_CR_FW_MASK |
160 ZYNQ_QSPI_CR_MSTREN_MASK;
161 writel(confr, &regs->cr);
162
Nathan Rossi04a44d32015-12-09 00:44:40 +1000163 /* Disable the LQSPI feature */
164 confr = readl(&regs->lqspicfg);
165 confr &= ~ZYNQ_QSPI_LQSPICFG_LQMODE_MASK;
166 writel(confr, &regs->lqspicfg);
167
Jagan Teki46d0a992015-08-17 18:38:06 +0530168 /* Enable SPI */
169 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
170}
171
172static int zynq_qspi_probe(struct udevice *bus)
173{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700174 struct zynq_qspi_plat *plat = dev_get_plat(bus);
Jagan Teki46d0a992015-08-17 18:38:06 +0530175 struct zynq_qspi_priv *priv = dev_get_priv(bus);
T Karthik Reddyea836be2020-02-04 05:47:45 -0700176 struct clk clk;
177 unsigned long clock;
178 int ret;
Jagan Teki46d0a992015-08-17 18:38:06 +0530179
180 priv->regs = plat->regs;
181 priv->fifo_depth = ZYNQ_QSPI_FIFO_DEPTH;
182
T Karthik Reddyea836be2020-02-04 05:47:45 -0700183 ret = clk_get_by_name(bus, "ref_clk", &clk);
184 if (ret < 0) {
185 dev_err(bus, "failed to get clock\n");
186 return ret;
187 }
188
189 clock = clk_get_rate(&clk);
190 if (IS_ERR_VALUE(clock)) {
191 dev_err(bus, "failed to get rate\n");
192 return clock;
193 }
194
195 ret = clk_enable(&clk);
196 if (ret && ret != -ENOSYS) {
197 dev_err(bus, "failed to enable clock\n");
198 return ret;
199 }
200
Jagan Teki46d0a992015-08-17 18:38:06 +0530201 /* init the zynq spi hw */
202 zynq_qspi_init_hw(priv);
203
T Karthik Reddyea836be2020-02-04 05:47:45 -0700204 plat->frequency = clock;
205 plat->speed_hz = plat->frequency / 2;
206
207 debug("%s: max-frequency=%d\n", __func__, plat->speed_hz);
208
Jagan Teki46d0a992015-08-17 18:38:06 +0530209 return 0;
210}
211
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700212/**
Jagan Teki46d0a992015-08-17 18:38:06 +0530213 * zynq_qspi_read_data - Copy data to RX buffer
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700214 * @priv: Pointer to the zynq_qspi_priv structure
Jagan Teki46d0a992015-08-17 18:38:06 +0530215 * @data: The 32 bit variable where data is stored
216 * @size: Number of bytes to be copied from data to RX buffer
217 */
218static void zynq_qspi_read_data(struct zynq_qspi_priv *priv, u32 data, u8 size)
219{
220 u8 byte3;
221
222 debug("%s: data 0x%04x rx_buf addr: 0x%08x size %d\n", __func__ ,
223 data, (unsigned)(priv->rx_buf), size);
224
225 if (priv->rx_buf) {
226 switch (size) {
227 case 1:
228 *((u8 *)priv->rx_buf) = data;
229 priv->rx_buf += 1;
230 break;
231 case 2:
232 *((u16 *)priv->rx_buf) = data;
233 priv->rx_buf += 2;
234 break;
235 case 3:
236 *((u16 *)priv->rx_buf) = data;
237 priv->rx_buf += 2;
238 byte3 = (u8)(data >> 16);
239 *((u8 *)priv->rx_buf) = byte3;
240 priv->rx_buf += 1;
241 break;
242 case 4:
243 /* Can not assume word aligned buffer */
244 memcpy(priv->rx_buf, &data, size);
245 priv->rx_buf += 4;
246 break;
247 default:
248 /* This will never execute */
249 break;
250 }
251 }
252 priv->bytes_to_receive -= size;
253 if (priv->bytes_to_receive < 0)
254 priv->bytes_to_receive = 0;
255}
256
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700257/**
Jagan Teki46d0a992015-08-17 18:38:06 +0530258 * zynq_qspi_write_data - Copy data from TX buffer
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700259 * @priv: Pointer to the zynq_qspi_priv structure
Jagan Teki46d0a992015-08-17 18:38:06 +0530260 * @data: Pointer to the 32 bit variable where data is to be copied
261 * @size: Number of bytes to be copied from TX buffer to data
262 */
263static void zynq_qspi_write_data(struct zynq_qspi_priv *priv,
264 u32 *data, u8 size)
265{
266 if (priv->tx_buf) {
267 switch (size) {
268 case 1:
269 *data = *((u8 *)priv->tx_buf);
270 priv->tx_buf += 1;
271 *data |= 0xFFFFFF00;
272 break;
273 case 2:
274 *data = *((u16 *)priv->tx_buf);
275 priv->tx_buf += 2;
276 *data |= 0xFFFF0000;
277 break;
278 case 3:
279 *data = *((u16 *)priv->tx_buf);
280 priv->tx_buf += 2;
281 *data |= (*((u8 *)priv->tx_buf) << 16);
282 priv->tx_buf += 1;
283 *data |= 0xFF000000;
284 break;
285 case 4:
286 /* Can not assume word aligned buffer */
287 memcpy(data, priv->tx_buf, size);
288 priv->tx_buf += 4;
289 break;
290 default:
291 /* This will never execute */
292 break;
293 }
294 } else {
295 *data = 0;
296 }
297
298 debug("%s: data 0x%08x tx_buf addr: 0x%08x size %d\n", __func__,
299 *data, (u32)priv->tx_buf, size);
300
301 priv->bytes_to_transfer -= size;
302 if (priv->bytes_to_transfer < 0)
303 priv->bytes_to_transfer = 0;
304}
305
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700306/**
307 * zynq_qspi_chipselect - Select or deselect the chip select line
308 * @priv: Pointer to the zynq_qspi_priv structure
309 * @is_on: Select(1) or deselect (0) the chip select line
310 */
Jagan Teki46d0a992015-08-17 18:38:06 +0530311static void zynq_qspi_chipselect(struct zynq_qspi_priv *priv, int is_on)
312{
313 u32 confr;
314 struct zynq_qspi_regs *regs = priv->regs;
315
316 confr = readl(&regs->cr);
317
318 if (is_on) {
319 /* Select the slave */
320 confr &= ~ZYNQ_QSPI_CR_SS_MASK;
321 confr |= (~(1 << priv->cs) << ZYNQ_QSPI_CR_SS_SHIFT) &
322 ZYNQ_QSPI_CR_SS_MASK;
323 } else
324 /* Deselect the slave */
325 confr |= ZYNQ_QSPI_CR_SS_MASK;
326
327 writel(confr, &regs->cr);
328}
329
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700330/**
Jagan Teki46d0a992015-08-17 18:38:06 +0530331 * zynq_qspi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700332 * @priv: Pointer to the zynq_qspi_priv structure
333 * @size: Number of bytes to be copied to fifo
Jagan Teki46d0a992015-08-17 18:38:06 +0530334 */
335static void zynq_qspi_fill_tx_fifo(struct zynq_qspi_priv *priv, u32 size)
336{
337 u32 data = 0;
338 u32 fifocount = 0;
339 unsigned len, offset;
340 struct zynq_qspi_regs *regs = priv->regs;
341 static const unsigned offsets[4] = {
342 ZYNQ_QSPI_TXD_00_00_OFFSET, ZYNQ_QSPI_TXD_00_01_OFFSET,
343 ZYNQ_QSPI_TXD_00_10_OFFSET, ZYNQ_QSPI_TXD_00_11_OFFSET };
344
345 while ((fifocount < size) &&
346 (priv->bytes_to_transfer > 0)) {
347 if (priv->bytes_to_transfer >= 4) {
348 if (priv->tx_buf) {
349 memcpy(&data, priv->tx_buf, 4);
350 priv->tx_buf += 4;
351 } else {
352 data = 0;
353 }
354 writel(data, &regs->txd0r);
355 priv->bytes_to_transfer -= 4;
356 fifocount++;
357 } else {
358 /* Write TXD1, TXD2, TXD3 only if TxFIFO is empty. */
359 if (!(readl(&regs->isr)
360 & ZYNQ_QSPI_IXR_TXOW_MASK) &&
361 !priv->rx_buf)
362 return;
363 len = priv->bytes_to_transfer;
364 zynq_qspi_write_data(priv, &data, len);
365 offset = (priv->rx_buf) ? offsets[0] : offsets[len];
366 writel(data, &regs->cr + (offset / 4));
367 }
368 }
369}
370
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700371/**
Jagan Teki46d0a992015-08-17 18:38:06 +0530372 * zynq_qspi_irq_poll - Interrupt service routine of the QSPI controller
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700373 * @priv: Pointer to the zynq_qspi structure
Jagan Teki46d0a992015-08-17 18:38:06 +0530374 *
375 * This function handles TX empty and Mode Fault interrupts only.
376 * On TX empty interrupt this function reads the received data from RX FIFO and
377 * fills the TX FIFO if there is any data remaining to be transferred.
378 * On Mode Fault interrupt this function indicates that transfer is completed,
379 * the SPI subsystem will identify the error as the remaining bytes to be
380 * transferred is non-zero.
381 *
382 * returns: 0 for poll timeout
383 * 1 transfer operation complete
384 */
385static int zynq_qspi_irq_poll(struct zynq_qspi_priv *priv)
386{
387 struct zynq_qspi_regs *regs = priv->regs;
388 u32 rxindex = 0;
389 u32 rxcount;
390 u32 status, timeout;
391
392 /* Poll until any of the interrupt status bits are set */
393 timeout = get_timer(0);
394 do {
395 status = readl(&regs->isr);
396 } while ((status == 0) &&
Ashok Reddy Somaf44bd3b2020-05-18 01:11:00 -0600397 (get_timer(timeout) < ZYNQ_QSPI_WAIT));
Jagan Teki46d0a992015-08-17 18:38:06 +0530398
399 if (status == 0) {
400 printf("zynq_qspi_irq_poll: Timeout!\n");
401 return -ETIMEDOUT;
402 }
403
404 writel(status, &regs->isr);
405
406 /* Disable all interrupts */
407 writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->idr);
408 if ((status & ZYNQ_QSPI_IXR_TXOW_MASK) ||
409 (status & ZYNQ_QSPI_IXR_RXNEMPTY_MASK)) {
410 /*
411 * This bit is set when Tx FIFO has < THRESHOLD entries. We have
412 * the THRESHOLD value set to 1, so this bit indicates Tx FIFO
413 * is empty
414 */
415 rxcount = priv->bytes_to_receive - priv->bytes_to_transfer;
416 rxcount = (rxcount % 4) ? ((rxcount/4)+1) : (rxcount/4);
417 while ((rxindex < rxcount) &&
418 (rxindex < ZYNQ_QSPI_RXFIFO_THRESHOLD)) {
419 /* Read out the data from the RX FIFO */
420 u32 data;
421 data = readl(&regs->drxr);
422
423 if (priv->bytes_to_receive >= 4) {
424 if (priv->rx_buf) {
425 memcpy(priv->rx_buf, &data, 4);
426 priv->rx_buf += 4;
427 }
428 priv->bytes_to_receive -= 4;
429 } else {
430 zynq_qspi_read_data(priv, data,
431 priv->bytes_to_receive);
432 }
433 rxindex++;
434 }
435
436 if (priv->bytes_to_transfer) {
437 /* There is more data to send */
438 zynq_qspi_fill_tx_fifo(priv,
439 ZYNQ_QSPI_RXFIFO_THRESHOLD);
440
441 writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
442 } else {
443 /*
444 * If transfer and receive is completed then only send
445 * complete signal
446 */
447 if (!priv->bytes_to_receive) {
448 /* return operation complete */
449 writel(ZYNQ_QSPI_IXR_ALL_MASK,
450 &regs->idr);
451 return 1;
452 }
453 }
454 }
455
456 return 0;
457}
458
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700459/**
Jagan Teki46d0a992015-08-17 18:38:06 +0530460 * zynq_qspi_start_transfer - Initiates the QSPI transfer
Ashok Reddy Soma17fbb592020-01-28 07:39:04 -0700461 * @priv: Pointer to the zynq_qspi_priv structure
Jagan Teki46d0a992015-08-17 18:38:06 +0530462 *
463 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
464 * transfer to be completed.
465 *
466 * returns: Number of bytes transferred in the last transfer
467 */
468static int zynq_qspi_start_transfer(struct zynq_qspi_priv *priv)
469{
470 u32 data = 0;
471 struct zynq_qspi_regs *regs = priv->regs;
472
473 debug("%s: qspi: 0x%08x transfer: 0x%08x len: %d\n", __func__,
474 (u32)priv, (u32)priv, priv->len);
475
476 priv->bytes_to_transfer = priv->len;
477 priv->bytes_to_receive = priv->len;
478
479 if (priv->len < 4)
480 zynq_qspi_fill_tx_fifo(priv, priv->len);
481 else
482 zynq_qspi_fill_tx_fifo(priv, priv->fifo_depth);
483
484 writel(ZYNQ_QSPI_IXR_ALL_MASK, &regs->ier);
Jagan Teki46d0a992015-08-17 18:38:06 +0530485
486 /* wait for completion */
487 do {
488 data = zynq_qspi_irq_poll(priv);
489 } while (data == 0);
490
491 return (priv->len) - (priv->bytes_to_transfer);
492}
493
494static int zynq_qspi_transfer(struct zynq_qspi_priv *priv)
495{
496 unsigned cs_change = 1;
497 int status = 0;
498
499 while (1) {
500 /* Select the chip if required */
501 if (cs_change)
502 zynq_qspi_chipselect(priv, 1);
503
504 cs_change = priv->cs_change;
505
506 if (!priv->tx_buf && !priv->rx_buf && priv->len) {
507 status = -1;
508 break;
509 }
510
511 /* Request the transfer */
512 if (priv->len) {
513 status = zynq_qspi_start_transfer(priv);
514 priv->is_inst = 0;
515 }
516
517 if (status != priv->len) {
518 if (status > 0)
519 status = -EMSGSIZE;
520 debug("zynq_qspi_transfer:%d len:%d\n",
521 status, priv->len);
522 break;
523 }
524 status = 0;
525
526 if (cs_change)
527 /* Deselect the chip */
528 zynq_qspi_chipselect(priv, 0);
529
530 break;
531 }
532
Vipul Kumar240cd752018-06-25 14:13:57 +0530533 return status;
Jagan Teki46d0a992015-08-17 18:38:06 +0530534}
535
536static int zynq_qspi_claim_bus(struct udevice *dev)
537{
538 struct udevice *bus = dev->parent;
539 struct zynq_qspi_priv *priv = dev_get_priv(bus);
540 struct zynq_qspi_regs *regs = priv->regs;
541
542 writel(ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
543
544 return 0;
545}
546
547static int zynq_qspi_release_bus(struct udevice *dev)
548{
549 struct udevice *bus = dev->parent;
550 struct zynq_qspi_priv *priv = dev_get_priv(bus);
551 struct zynq_qspi_regs *regs = priv->regs;
552
553 writel(~ZYNQ_QSPI_ENR_SPI_EN_MASK, &regs->enr);
554
555 return 0;
556}
557
558static int zynq_qspi_xfer(struct udevice *dev, unsigned int bitlen,
559 const void *dout, void *din, unsigned long flags)
560{
561 struct udevice *bus = dev->parent;
562 struct zynq_qspi_priv *priv = dev_get_priv(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700563 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Jagan Teki46d0a992015-08-17 18:38:06 +0530564
565 priv->cs = slave_plat->cs;
566 priv->tx_buf = dout;
567 priv->rx_buf = din;
568 priv->len = bitlen / 8;
569
Jagan Tekie5e0d682015-10-25 09:31:54 +0530570 debug("zynq_qspi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700571 dev_seq(bus), slave_plat->cs, bitlen, priv->len, flags);
Jagan Teki46d0a992015-08-17 18:38:06 +0530572
573 /*
574 * Festering sore.
575 * Assume that the beginning of a transfer with bits to
576 * transmit must contain a device command.
577 */
578 if (dout && flags & SPI_XFER_BEGIN)
579 priv->is_inst = 1;
580 else
581 priv->is_inst = 0;
582
583 if (flags & SPI_XFER_END)
584 priv->cs_change = 1;
585 else
586 priv->cs_change = 0;
587
588 zynq_qspi_transfer(priv);
589
590 return 0;
591}
592
593static int zynq_qspi_set_speed(struct udevice *bus, uint speed)
594{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700595 struct zynq_qspi_plat *plat = bus->plat;
Jagan Teki46d0a992015-08-17 18:38:06 +0530596 struct zynq_qspi_priv *priv = dev_get_priv(bus);
597 struct zynq_qspi_regs *regs = priv->regs;
598 uint32_t confr;
599 u8 baud_rate_val = 0;
600
601 if (speed > plat->frequency)
602 speed = plat->frequency;
603
604 /* Set the clock frequency */
605 confr = readl(&regs->cr);
606 if (speed == 0) {
607 /* Set baudrate x8, if the freq is 0 */
608 baud_rate_val = 0x2;
609 } else if (plat->speed_hz != speed) {
610 while ((baud_rate_val < ZYNQ_QSPI_CR_BAUD_MAX) &&
611 ((plat->frequency /
612 (2 << baud_rate_val)) > speed))
613 baud_rate_val++;
614
615 plat->speed_hz = speed / (2 << baud_rate_val);
616 }
617 confr &= ~ZYNQ_QSPI_CR_BAUD_MASK;
618 confr |= (baud_rate_val << ZYNQ_QSPI_CR_BAUD_SHIFT);
619
620 writel(confr, &regs->cr);
621 priv->freq = speed;
622
Jagan Tekie5e0d682015-10-25 09:31:54 +0530623 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
Jagan Teki46d0a992015-08-17 18:38:06 +0530624
625 return 0;
626}
627
628static int zynq_qspi_set_mode(struct udevice *bus, uint mode)
629{
630 struct zynq_qspi_priv *priv = dev_get_priv(bus);
631 struct zynq_qspi_regs *regs = priv->regs;
632 uint32_t confr;
633
634 /* Set the SPI Clock phase and polarities */
635 confr = readl(&regs->cr);
636 confr &= ~(ZYNQ_QSPI_CR_CPHA_MASK | ZYNQ_QSPI_CR_CPOL_MASK);
637
Jagan Teki2775e912015-09-08 01:39:44 +0530638 if (mode & SPI_CPHA)
Jagan Teki46d0a992015-08-17 18:38:06 +0530639 confr |= ZYNQ_QSPI_CR_CPHA_MASK;
Jagan Teki2775e912015-09-08 01:39:44 +0530640 if (mode & SPI_CPOL)
Jagan Teki46d0a992015-08-17 18:38:06 +0530641 confr |= ZYNQ_QSPI_CR_CPOL_MASK;
642
643 writel(confr, &regs->cr);
644 priv->mode = mode;
645
Jagan Tekie5e0d682015-10-25 09:31:54 +0530646 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
Jagan Teki46d0a992015-08-17 18:38:06 +0530647
648 return 0;
649}
650
651static const struct dm_spi_ops zynq_qspi_ops = {
652 .claim_bus = zynq_qspi_claim_bus,
653 .release_bus = zynq_qspi_release_bus,
654 .xfer = zynq_qspi_xfer,
655 .set_speed = zynq_qspi_set_speed,
656 .set_mode = zynq_qspi_set_mode,
657};
658
659static const struct udevice_id zynq_qspi_ids[] = {
660 { .compatible = "xlnx,zynq-qspi-1.0" },
661 { }
662};
663
664U_BOOT_DRIVER(zynq_qspi) = {
665 .name = "zynq_qspi",
666 .id = UCLASS_SPI,
667 .of_match = zynq_qspi_ids,
668 .ops = &zynq_qspi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700669 .of_to_plat = zynq_qspi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700670 .plat_auto = sizeof(struct zynq_qspi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700671 .priv_auto = sizeof(struct zynq_qspi_priv),
Jagan Teki46d0a992015-08-17 18:38:06 +0530672 .probe = zynq_qspi_probe,
673};