2 * Xilinx Zynq UltraScale+ MPSoC Quad-SPI (QSPI) controller driver
5 * Copyright (C) 2009 - 2015 Xilinx, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dmaengine.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_address.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spinlock.h>
26 #include <linux/workqueue.h>
28 /* Generic QSPI register offsets */
29 #define GQSPI_CONFIG_OFST 0x00000100
30 #define GQSPI_ISR_OFST 0x00000104
31 #define GQSPI_IDR_OFST 0x0000010C
32 #define GQSPI_IER_OFST 0x00000108
33 #define GQSPI_IMASK_OFST 0x00000110
34 #define GQSPI_EN_OFST 0x00000114
35 #define GQSPI_TXD_OFST 0x0000011C
36 #define GQSPI_RXD_OFST 0x00000120
37 #define GQSPI_TX_THRESHOLD_OFST 0x00000128
38 #define GQSPI_RX_THRESHOLD_OFST 0x0000012C
39 #define GQSPI_LPBK_DLY_ADJ_OFST 0x00000138
40 #define GQSPI_GEN_FIFO_OFST 0x00000140
41 #define GQSPI_SEL_OFST 0x00000144
42 #define GQSPI_GF_THRESHOLD_OFST 0x00000150
43 #define GQSPI_FIFO_CTRL_OFST 0x0000014C
44 #define GQSPI_QSPIDMA_DST_CTRL_OFST 0x0000080C
45 #define GQSPI_QSPIDMA_DST_SIZE_OFST 0x00000804
46 #define GQSPI_QSPIDMA_DST_STS_OFST 0x00000808
47 #define GQSPI_QSPIDMA_DST_I_STS_OFST 0x00000814
48 #define GQSPI_QSPIDMA_DST_I_EN_OFST 0x00000818
49 #define GQSPI_QSPIDMA_DST_I_DIS_OFST 0x0000081C
50 #define GQSPI_QSPIDMA_DST_I_MASK_OFST 0x00000820
51 #define GQSPI_QSPIDMA_DST_ADDR_OFST 0x00000800
52 #define GQSPI_QSPIDMA_DST_ADDR_MSB_OFST 0x00000828
54 /* GQSPI register bit masks */
55 #define GQSPI_SEL_MASK 0x00000001
56 #define GQSPI_EN_MASK 0x00000001
57 #define GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK 0x00000020
58 #define GQSPI_ISR_WR_TO_CLR_MASK 0x00000002
59 #define GQSPI_IDR_ALL_MASK 0x00000FBE
60 #define GQSPI_CFG_MODE_EN_MASK 0xC0000000
61 #define GQSPI_CFG_GEN_FIFO_START_MODE_MASK 0x20000000
62 #define GQSPI_CFG_ENDIAN_MASK 0x04000000
63 #define GQSPI_CFG_EN_POLL_TO_MASK 0x00100000
64 #define GQSPI_CFG_WP_HOLD_MASK 0x00080000
65 #define GQSPI_CFG_BAUD_RATE_DIV_MASK 0x00000038
66 #define GQSPI_CFG_CLK_PHA_MASK 0x00000004
67 #define GQSPI_CFG_CLK_POL_MASK 0x00000002
68 #define GQSPI_CFG_START_GEN_FIFO_MASK 0x10000000
69 #define GQSPI_GENFIFO_IMM_DATA_MASK 0x000000FF
70 #define GQSPI_GENFIFO_DATA_XFER 0x00000100
71 #define GQSPI_GENFIFO_EXP 0x00000200
72 #define GQSPI_GENFIFO_MODE_SPI 0x00000400
73 #define GQSPI_GENFIFO_MODE_DUALSPI 0x00000800
74 #define GQSPI_GENFIFO_MODE_QUADSPI 0x00000C00
75 #define GQSPI_GENFIFO_MODE_MASK 0x00000C00
76 #define GQSPI_GENFIFO_CS_LOWER 0x00001000
77 #define GQSPI_GENFIFO_CS_UPPER 0x00002000
78 #define GQSPI_GENFIFO_BUS_LOWER 0x00004000
79 #define GQSPI_GENFIFO_BUS_UPPER 0x00008000
80 #define GQSPI_GENFIFO_BUS_BOTH 0x0000C000
81 #define GQSPI_GENFIFO_BUS_MASK 0x0000C000
82 #define GQSPI_GENFIFO_TX 0x00010000
83 #define GQSPI_GENFIFO_RX 0x00020000
84 #define GQSPI_GENFIFO_STRIPE 0x00040000
85 #define GQSPI_GENFIFO_POLL 0x00080000
86 #define GQSPI_GENFIFO_EXP_START 0x00000100
87 #define GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK 0x00000004
88 #define GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK 0x00000002
89 #define GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK 0x00000001
90 #define GQSPI_ISR_RXEMPTY_MASK 0x00000800
91 #define GQSPI_ISR_GENFIFOFULL_MASK 0x00000400
92 #define GQSPI_ISR_GENFIFONOT_FULL_MASK 0x00000200
93 #define GQSPI_ISR_TXEMPTY_MASK 0x00000100
94 #define GQSPI_ISR_GENFIFOEMPTY_MASK 0x00000080
95 #define GQSPI_ISR_RXFULL_MASK 0x00000020
96 #define GQSPI_ISR_RXNEMPTY_MASK 0x00000010
97 #define GQSPI_ISR_TXFULL_MASK 0x00000008
98 #define GQSPI_ISR_TXNOT_FULL_MASK 0x00000004
99 #define GQSPI_ISR_POLL_TIME_EXPIRE_MASK 0x00000002
100 #define GQSPI_IER_TXNOT_FULL_MASK 0x00000004
101 #define GQSPI_IER_RXEMPTY_MASK 0x00000800
102 #define GQSPI_IER_POLL_TIME_EXPIRE_MASK 0x00000002
103 #define GQSPI_IER_RXNEMPTY_MASK 0x00000010
104 #define GQSPI_IER_GENFIFOEMPTY_MASK 0x00000080
105 #define GQSPI_IER_TXEMPTY_MASK 0x00000100
106 #define GQSPI_QSPIDMA_DST_INTR_ALL_MASK 0x000000FE
107 #define GQSPI_QSPIDMA_DST_STS_WTC 0x0000E000
108 #define GQSPI_CFG_MODE_EN_DMA_MASK 0x80000000
109 #define GQSPI_ISR_IDR_MASK 0x00000994
110 #define GQSPI_QSPIDMA_DST_I_EN_DONE_MASK 0x00000002
111 #define GQSPI_QSPIDMA_DST_I_STS_DONE_MASK 0x00000002
112 #define GQSPI_IRQ_MASK 0x00000980
114 #define GQSPI_CFG_BAUD_RATE_DIV_SHIFT 3
115 #define GQSPI_GENFIFO_CS_SETUP 0x4
116 #define GQSPI_GENFIFO_CS_HOLD 0x3
117 #define GQSPI_TXD_DEPTH 64
118 #define GQSPI_RX_FIFO_THRESHOLD 32
119 #define GQSPI_RX_FIFO_FILL (GQSPI_RX_FIFO_THRESHOLD * 4)
120 #define GQSPI_TX_FIFO_THRESHOLD_RESET_VAL 32
121 #define GQSPI_TX_FIFO_FILL (GQSPI_TXD_DEPTH -\
122 GQSPI_TX_FIFO_THRESHOLD_RESET_VAL)
123 #define GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL 0X10
124 #define GQSPI_QSPIDMA_DST_CTRL_RESET_VAL 0x803FFA00
125 #define GQSPI_SELECT_FLASH_CS_LOWER 0x1
126 #define GQSPI_SELECT_FLASH_CS_UPPER 0x2
127 #define GQSPI_SELECT_FLASH_CS_BOTH 0x3
128 #define GQSPI_SELECT_FLASH_BUS_LOWER 0x1
129 #define GQSPI_SELECT_FLASH_BUS_UPPER 0x2
130 #define GQSPI_SELECT_FLASH_BUS_BOTH 0x3
131 #define GQSPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */
132 #define GQSPI_BAUD_DIV_SHIFT 2 /* Baud rate divisor shift */
133 #define GQSPI_SELECT_MODE_SPI 0x1
134 #define GQSPI_SELECT_MODE_DUALSPI 0x2
135 #define GQSPI_SELECT_MODE_QUADSPI 0x4
136 #define GQSPI_DMA_UNALIGN 0x3
137 #define GQSPI_DEFAULT_NUM_CS 1 /* Default number of chip selects */
139 #define SPI_AUTOSUSPEND_TIMEOUT 3000
140 enum mode_type {GQSPI_MODE_IO, GQSPI_MODE_DMA};
141 static const struct zynqmp_eemi_ops *eemi_ops;
144 * struct zynqmp_qspi - Defines qspi driver instance
145 * @regs: Virtual address of the QSPI controller registers
146 * @refclk: Pointer to the peripheral clock
147 * @pclk: Pointer to the APB clock
149 * @dev: Pointer to struct device
150 * @txbuf: Pointer to the TX buffer
151 * @rxbuf: Pointer to the RX buffer
152 * @bytes_to_transfer: Number of bytes left to transfer
153 * @bytes_to_receive: Number of bytes left to receive
154 * @genfifocs: Used for chip select
155 * @genfifobus: Used to select the upper or lower bus
156 * @dma_rx_bytes: Remaining bytes to receive by DMA mode
157 * @dma_addr: DMA address after mapping the kernel buffer
158 * @genfifoentry: Used for storing the genfifoentry instruction.
159 * @mode: Defines the mode in which QSPI is operating
169 int bytes_to_transfer;
170 int bytes_to_receive;
180 * zynqmp_gqspi_read: For GQSPI controller read operation
181 * @xqspi: Pointer to the zynqmp_qspi structure
182 * @offset: Offset from where to read
184 static u32 zynqmp_gqspi_read(struct zynqmp_qspi *xqspi, u32 offset)
186 return readl_relaxed(xqspi->regs + offset);
190 * zynqmp_gqspi_write: For GQSPI controller write operation
191 * @xqspi: Pointer to the zynqmp_qspi structure
192 * @offset: Offset where to write
193 * @val: Value to be written
195 static inline void zynqmp_gqspi_write(struct zynqmp_qspi *xqspi, u32 offset,
198 writel_relaxed(val, (xqspi->regs + offset));
202 * zynqmp_gqspi_selectslave: For selection of slave device
203 * @instanceptr: Pointer to the zynqmp_qspi structure
204 * @flashcs: For chip select
205 * @flashbus: To check which bus is selected- upper or lower
207 static void zynqmp_gqspi_selectslave(struct zynqmp_qspi *instanceptr,
208 u8 slavecs, u8 slavebus)
211 * Bus and CS lines selected here will be updated in the instance and
212 * used for subsequent GENFIFO entries during transfer.
215 /* Choose slave select line */
217 case GQSPI_SELECT_FLASH_CS_BOTH:
218 instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER |
219 GQSPI_GENFIFO_CS_UPPER;
221 case GQSPI_SELECT_FLASH_CS_UPPER:
222 instanceptr->genfifocs = GQSPI_GENFIFO_CS_UPPER;
224 case GQSPI_SELECT_FLASH_CS_LOWER:
225 instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER;
228 dev_warn(instanceptr->dev, "Invalid slave select\n");
233 case GQSPI_SELECT_FLASH_BUS_BOTH:
234 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER |
235 GQSPI_GENFIFO_BUS_UPPER;
237 case GQSPI_SELECT_FLASH_BUS_UPPER:
238 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_UPPER;
240 case GQSPI_SELECT_FLASH_BUS_LOWER:
241 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER;
244 dev_warn(instanceptr->dev, "Invalid slave bus\n");
249 * zynqmp_qspi_init_hw: Initialize the hardware
250 * @xqspi: Pointer to the zynqmp_qspi structure
252 * The default settings of the QSPI controller's configurable parameters on
255 * - TX threshold set to 1
256 * - RX threshold set to 1
257 * - Flash memory interface mode enabled
258 * This function performs the following actions
259 * - Disable and clear all the interrupts
260 * - Enable manual slave select
261 * - Enable manual start
262 * - Deselect all the chip select lines
263 * - Set the little endian mode of TX FIFO and
264 * - Enable the QSPI controller
266 static void zynqmp_qspi_init_hw(struct zynqmp_qspi *xqspi)
270 /* Select the GQSPI mode */
271 zynqmp_gqspi_write(xqspi, GQSPI_SEL_OFST, GQSPI_SEL_MASK);
272 /* Clear and disable interrupts */
273 zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST,
274 zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST) |
275 GQSPI_ISR_WR_TO_CLR_MASK);
276 /* Clear the DMA STS */
277 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
278 zynqmp_gqspi_read(xqspi,
279 GQSPI_QSPIDMA_DST_I_STS_OFST));
280 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_STS_OFST,
281 zynqmp_gqspi_read(xqspi,
282 GQSPI_QSPIDMA_DST_STS_OFST) |
283 GQSPI_QSPIDMA_DST_STS_WTC);
284 zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_IDR_ALL_MASK);
285 zynqmp_gqspi_write(xqspi,
286 GQSPI_QSPIDMA_DST_I_DIS_OFST,
287 GQSPI_QSPIDMA_DST_INTR_ALL_MASK);
288 /* Disable the GQSPI */
289 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
290 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
291 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
293 config_reg |= GQSPI_CFG_GEN_FIFO_START_MODE_MASK;
294 /* Little endian by default */
295 config_reg &= ~GQSPI_CFG_ENDIAN_MASK;
296 /* Disable poll time out */
297 config_reg &= ~GQSPI_CFG_EN_POLL_TO_MASK;
299 config_reg |= GQSPI_CFG_WP_HOLD_MASK;
300 /* Clear pre-scalar by default */
301 config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
303 config_reg &= ~GQSPI_CFG_CLK_PHA_MASK;
305 config_reg &= ~GQSPI_CFG_CLK_POL_MASK;
306 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
308 /* Clear the TX and RX FIFO */
309 zynqmp_gqspi_write(xqspi, GQSPI_FIFO_CTRL_OFST,
310 GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK |
311 GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK |
312 GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK);
313 /* Set by default to allow for high frequencies */
314 zynqmp_gqspi_write(xqspi, GQSPI_LPBK_DLY_ADJ_OFST,
315 zynqmp_gqspi_read(xqspi, GQSPI_LPBK_DLY_ADJ_OFST) |
316 GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK);
317 /* Reset thresholds */
318 zynqmp_gqspi_write(xqspi, GQSPI_TX_THRESHOLD_OFST,
319 GQSPI_TX_FIFO_THRESHOLD_RESET_VAL);
320 zynqmp_gqspi_write(xqspi, GQSPI_RX_THRESHOLD_OFST,
321 GQSPI_RX_FIFO_THRESHOLD);
322 zynqmp_gqspi_write(xqspi, GQSPI_GF_THRESHOLD_OFST,
323 GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL);
324 zynqmp_gqspi_selectslave(xqspi,
325 GQSPI_SELECT_FLASH_CS_LOWER,
326 GQSPI_SELECT_FLASH_BUS_LOWER);
328 zynqmp_gqspi_write(xqspi,
329 GQSPI_QSPIDMA_DST_CTRL_OFST,
330 GQSPI_QSPIDMA_DST_CTRL_RESET_VAL);
332 /* Enable the GQSPI */
333 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
337 * zynqmp_qspi_copy_read_data: Copy data to RX buffer
338 * @xqspi: Pointer to the zynqmp_qspi structure
339 * @data: The variable where data is stored
340 * @size: Number of bytes to be copied from data to RX buffer
342 static void zynqmp_qspi_copy_read_data(struct zynqmp_qspi *xqspi,
345 memcpy(xqspi->rxbuf, &data, size);
346 xqspi->rxbuf += size;
347 xqspi->bytes_to_receive -= size;
351 * zynqmp_prepare_transfer_hardware: Prepares hardware for transfer.
352 * @master: Pointer to the spi_master structure which provides
353 * information about the controller.
355 * This function enables SPI master controller.
357 * Return: 0 on success; error value otherwise
359 static int zynqmp_prepare_transfer_hardware(struct spi_master *master)
361 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
363 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
368 * zynqmp_unprepare_transfer_hardware: Relaxes hardware after transfer
369 * @master: Pointer to the spi_master structure which provides
370 * information about the controller.
372 * This function disables the SPI master controller.
376 static int zynqmp_unprepare_transfer_hardware(struct spi_master *master)
378 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
380 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
385 * zynqmp_qspi_chipselect: Select or deselect the chip select line
386 * @qspi: Pointer to the spi_device structure
387 * @is_high: Select(0) or deselect (1) the chip select line
389 static void zynqmp_qspi_chipselect(struct spi_device *qspi, bool is_high)
391 struct zynqmp_qspi *xqspi = spi_master_get_devdata(qspi->master);
393 u32 genfifoentry = 0x0, statusreg;
395 genfifoentry |= GQSPI_GENFIFO_MODE_SPI;
396 genfifoentry |= xqspi->genfifobus;
399 genfifoentry |= xqspi->genfifocs;
400 genfifoentry |= GQSPI_GENFIFO_CS_SETUP;
402 genfifoentry |= GQSPI_GENFIFO_CS_HOLD;
405 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
407 /* Dummy generic FIFO entry */
408 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
410 /* Manually start the generic FIFO command */
411 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
412 zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
413 GQSPI_CFG_START_GEN_FIFO_MASK);
415 timeout = jiffies + msecs_to_jiffies(1000);
417 /* Wait until the generic FIFO command is empty */
419 statusreg = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
421 if ((statusreg & GQSPI_ISR_GENFIFOEMPTY_MASK) &&
422 (statusreg & GQSPI_ISR_TXEMPTY_MASK))
426 } while (!time_after_eq(jiffies, timeout));
428 if (time_after_eq(jiffies, timeout))
429 dev_err(xqspi->dev, "Chip select timed out\n");
433 * zynqmp_qspi_setup_transfer: Configure QSPI controller for specified
435 * @qspi: Pointer to the spi_device structure
436 * @transfer: Pointer to the spi_transfer structure which provides
437 * information about next transfer setup parameters
439 * Sets the operational mode of QSPI controller for the next QSPI transfer and
440 * sets the requested clock frequency.
445 * If the requested frequency is not an exact match with what can be
446 * obtained using the pre-scalar value, the driver sets the clock
447 * frequency which is lower than the requested frequency (maximum lower)
450 * If the requested frequency is higher or lower than that is supported
451 * by the QSPI controller the driver will set the highest or lowest
452 * frequency supported by controller.
454 static int zynqmp_qspi_setup_transfer(struct spi_device *qspi,
455 struct spi_transfer *transfer)
457 struct zynqmp_qspi *xqspi = spi_master_get_devdata(qspi->master);
459 u32 config_reg, req_hz, baud_rate_val = 0;
462 req_hz = transfer->speed_hz;
464 req_hz = qspi->max_speed_hz;
466 /* Set the clock frequency */
467 /* If req_hz == 0, default to lowest speed */
468 clk_rate = clk_get_rate(xqspi->refclk);
470 while ((baud_rate_val < GQSPI_BAUD_DIV_MAX) &&
472 (GQSPI_BAUD_DIV_SHIFT << baud_rate_val)) > req_hz)
475 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
477 /* Set the QSPI clock phase and clock polarity */
478 config_reg &= (~GQSPI_CFG_CLK_PHA_MASK) & (~GQSPI_CFG_CLK_POL_MASK);
480 if (qspi->mode & SPI_CPHA)
481 config_reg |= GQSPI_CFG_CLK_PHA_MASK;
482 if (qspi->mode & SPI_CPOL)
483 config_reg |= GQSPI_CFG_CLK_POL_MASK;
485 config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
486 config_reg |= (baud_rate_val << GQSPI_CFG_BAUD_RATE_DIV_SHIFT);
487 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
492 * zynqmp_qspi_setup: Configure the QSPI controller
493 * @qspi: Pointer to the spi_device structure
495 * Sets the operational mode of QSPI controller for the next QSPI transfer,
496 * baud rate and divisor value to setup the requested qspi clock.
498 * Return: 0 on success; error value otherwise.
500 static int zynqmp_qspi_setup(struct spi_device *qspi)
502 if (qspi->master->busy)
508 * zynqmp_qspi_filltxfifo: Fills the TX FIFO as long as there is room in
509 * the FIFO or the bytes required to be
511 * @xqspi: Pointer to the zynqmp_qspi structure
512 * @size: Number of bytes to be copied from TX buffer to TX FIFO
514 static void zynqmp_qspi_filltxfifo(struct zynqmp_qspi *xqspi, int size)
516 u32 count = 0, intermediate;
518 while ((xqspi->bytes_to_transfer > 0) && (count < size)) {
519 memcpy(&intermediate, xqspi->txbuf, 4);
520 zynqmp_gqspi_write(xqspi, GQSPI_TXD_OFST, intermediate);
522 if (xqspi->bytes_to_transfer >= 4) {
524 xqspi->bytes_to_transfer -= 4;
526 xqspi->txbuf += xqspi->bytes_to_transfer;
527 xqspi->bytes_to_transfer = 0;
534 * zynqmp_qspi_readrxfifo: Fills the RX FIFO as long as there is room in
536 * @xqspi: Pointer to the zynqmp_qspi structure
537 * @size: Number of bytes to be copied from RX buffer to RX FIFO
539 static void zynqmp_qspi_readrxfifo(struct zynqmp_qspi *xqspi, u32 size)
544 while ((count < size) && (xqspi->bytes_to_receive > 0)) {
545 if (xqspi->bytes_to_receive >= 4) {
546 (*(u32 *) xqspi->rxbuf) =
547 zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
549 xqspi->bytes_to_receive -= 4;
552 data = zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
553 count += xqspi->bytes_to_receive;
554 zynqmp_qspi_copy_read_data(xqspi, data,
555 xqspi->bytes_to_receive);
556 xqspi->bytes_to_receive = 0;
562 * zynqmp_process_dma_irq: Handler for DMA done interrupt of QSPI
564 * @xqspi: zynqmp_qspi instance pointer
566 * This function handles DMA interrupt only.
568 static void zynqmp_process_dma_irq(struct zynqmp_qspi *xqspi)
570 u32 config_reg, genfifoentry;
572 dma_unmap_single(xqspi->dev, xqspi->dma_addr,
573 xqspi->dma_rx_bytes, DMA_FROM_DEVICE);
574 xqspi->rxbuf += xqspi->dma_rx_bytes;
575 xqspi->bytes_to_receive -= xqspi->dma_rx_bytes;
576 xqspi->dma_rx_bytes = 0;
578 /* Disabling the DMA interrupts */
579 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_DIS_OFST,
580 GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
582 if (xqspi->bytes_to_receive > 0) {
583 /* Switch to IO mode,for remaining bytes to receive */
584 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
585 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
586 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
588 /* Initiate the transfer of remaining bytes */
589 genfifoentry = xqspi->genfifoentry;
590 genfifoentry |= xqspi->bytes_to_receive;
591 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
593 /* Dummy generic FIFO entry */
594 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
597 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
598 (zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
599 GQSPI_CFG_START_GEN_FIFO_MASK));
601 /* Enable the RX interrupts for IO mode */
602 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
603 GQSPI_IER_GENFIFOEMPTY_MASK |
604 GQSPI_IER_RXNEMPTY_MASK |
605 GQSPI_IER_RXEMPTY_MASK);
610 * zynqmp_qspi_irq: Interrupt service routine of the QSPI controller
612 * @dev_id: Pointer to the xqspi structure
614 * This function handles TX empty only.
615 * On TX empty interrupt this function reads the received data from RX FIFO
616 * and fills the TX FIFO if there is any data remaining to be transferred.
618 * Return: IRQ_HANDLED when interrupt is handled
619 * IRQ_NONE otherwise.
621 static irqreturn_t zynqmp_qspi_irq(int irq, void *dev_id)
623 struct spi_master *master = dev_id;
624 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
626 u32 status, mask, dma_status = 0;
628 status = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
629 zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST, status);
630 mask = (status & ~(zynqmp_gqspi_read(xqspi, GQSPI_IMASK_OFST)));
632 /* Read and clear DMA status */
633 if (xqspi->mode == GQSPI_MODE_DMA) {
635 zynqmp_gqspi_read(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST);
636 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
640 if (mask & GQSPI_ISR_TXNOT_FULL_MASK) {
641 zynqmp_qspi_filltxfifo(xqspi, GQSPI_TX_FIFO_FILL);
645 if (dma_status & GQSPI_QSPIDMA_DST_I_STS_DONE_MASK) {
646 zynqmp_process_dma_irq(xqspi);
648 } else if (!(mask & GQSPI_IER_RXEMPTY_MASK) &&
649 (mask & GQSPI_IER_GENFIFOEMPTY_MASK)) {
650 zynqmp_qspi_readrxfifo(xqspi, GQSPI_RX_FIFO_FILL);
654 if ((xqspi->bytes_to_receive == 0) && (xqspi->bytes_to_transfer == 0)
655 && ((status & GQSPI_IRQ_MASK) == GQSPI_IRQ_MASK)) {
656 zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_ISR_IDR_MASK);
657 spi_finalize_current_transfer(master);
664 * zynqmp_qspi_selectspimode: Selects SPI mode - x1 or x2 or x4.
665 * @xqspi: xqspi is a pointer to the GQSPI instance
666 * @spimode: spimode - SPI or DUAL or QUAD.
667 * Return: Mask to set desired SPI mode in GENFIFO entry.
669 static inline u32 zynqmp_qspi_selectspimode(struct zynqmp_qspi *xqspi,
675 case GQSPI_SELECT_MODE_DUALSPI:
676 mask = GQSPI_GENFIFO_MODE_DUALSPI;
678 case GQSPI_SELECT_MODE_QUADSPI:
679 mask = GQSPI_GENFIFO_MODE_QUADSPI;
681 case GQSPI_SELECT_MODE_SPI:
682 mask = GQSPI_GENFIFO_MODE_SPI;
685 dev_warn(xqspi->dev, "Invalid SPI mode\n");
692 * zynq_qspi_setuprxdma: This function sets up the RX DMA operation
693 * @xqspi: xqspi is a pointer to the GQSPI instance.
695 static void zynq_qspi_setuprxdma(struct zynqmp_qspi *xqspi)
697 u32 rx_bytes, rx_rem, config_reg;
699 u64 dma_align = (u64)(uintptr_t)xqspi->rxbuf;
701 if ((xqspi->bytes_to_receive < 8) ||
702 ((dma_align & GQSPI_DMA_UNALIGN) != 0x0)) {
703 /* Setting to IO mode */
704 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
705 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
706 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
707 xqspi->mode = GQSPI_MODE_IO;
708 xqspi->dma_rx_bytes = 0;
712 rx_rem = xqspi->bytes_to_receive % 4;
713 rx_bytes = (xqspi->bytes_to_receive - rx_rem);
715 addr = dma_map_single(xqspi->dev, (void *)xqspi->rxbuf,
716 rx_bytes, DMA_FROM_DEVICE);
717 if (dma_mapping_error(xqspi->dev, addr))
718 dev_err(xqspi->dev, "ERR:rxdma:memory not mapped\n");
720 xqspi->dma_rx_bytes = rx_bytes;
721 xqspi->dma_addr = addr;
722 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_OFST,
723 (u32)(addr & 0xffffffff));
724 addr = ((addr >> 16) >> 16);
725 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_MSB_OFST,
726 ((u32)addr) & 0xfff);
728 /* Enabling the DMA mode */
729 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
730 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
731 config_reg |= GQSPI_CFG_MODE_EN_DMA_MASK;
732 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
734 /* Switch to DMA mode */
735 xqspi->mode = GQSPI_MODE_DMA;
737 /* Write the number of bytes to transfer */
738 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_SIZE_OFST, rx_bytes);
742 * zynqmp_qspi_txrxsetup: This function checks the TX/RX buffers in
743 * the transfer and sets up the GENFIFO entries,
744 * TX FIFO as required.
745 * @xqspi: xqspi is a pointer to the GQSPI instance.
746 * @transfer: It is a pointer to the structure containing transfer data.
747 * @genfifoentry: genfifoentry is pointer to the variable in which
748 * GENFIFO mask is returned to calling function
750 static void zynqmp_qspi_txrxsetup(struct zynqmp_qspi *xqspi,
751 struct spi_transfer *transfer,
757 if ((xqspi->txbuf != NULL) && (xqspi->rxbuf == NULL)) {
758 /* Setup data to be TXed */
759 *genfifoentry &= ~GQSPI_GENFIFO_RX;
760 *genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
761 *genfifoentry |= GQSPI_GENFIFO_TX;
763 zynqmp_qspi_selectspimode(xqspi, transfer->tx_nbits);
764 xqspi->bytes_to_transfer = transfer->len;
765 if (xqspi->mode == GQSPI_MODE_DMA) {
766 config_reg = zynqmp_gqspi_read(xqspi,
768 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
769 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
771 xqspi->mode = GQSPI_MODE_IO;
773 zynqmp_qspi_filltxfifo(xqspi, GQSPI_TXD_DEPTH);
774 /* Discard RX data */
775 xqspi->bytes_to_receive = 0;
776 } else if ((xqspi->txbuf == NULL) && (xqspi->rxbuf != NULL)) {
780 *genfifoentry &= ~GQSPI_GENFIFO_TX;
782 *genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
783 *genfifoentry |= GQSPI_GENFIFO_RX;
785 zynqmp_qspi_selectspimode(xqspi, transfer->rx_nbits);
786 xqspi->bytes_to_transfer = 0;
787 xqspi->bytes_to_receive = transfer->len;
788 zynq_qspi_setuprxdma(xqspi);
793 * zynqmp_qspi_start_transfer: Initiates the QSPI transfer
794 * @master: Pointer to the spi_master structure which provides
795 * information about the controller.
796 * @qspi: Pointer to the spi_device structure
797 * @transfer: Pointer to the spi_transfer structure which provide information
798 * about next transfer parameters
800 * This function fills the TX FIFO, starts the QSPI transfer, and waits for the
801 * transfer to be completed.
803 * Return: Number of bytes transferred in the last transfer
805 static int zynqmp_qspi_start_transfer(struct spi_master *master,
806 struct spi_device *qspi,
807 struct spi_transfer *transfer)
809 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
810 u32 genfifoentry = 0x0, transfer_len;
812 xqspi->txbuf = transfer->tx_buf;
813 xqspi->rxbuf = transfer->rx_buf;
815 zynqmp_qspi_setup_transfer(qspi, transfer);
817 genfifoentry |= xqspi->genfifocs;
818 genfifoentry |= xqspi->genfifobus;
820 zynqmp_qspi_txrxsetup(xqspi, transfer, &genfifoentry);
822 if (xqspi->mode == GQSPI_MODE_DMA)
823 transfer_len = xqspi->dma_rx_bytes;
825 transfer_len = transfer->len;
827 xqspi->genfifoentry = genfifoentry;
828 if ((transfer_len) < GQSPI_GENFIFO_IMM_DATA_MASK) {
829 genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
830 genfifoentry |= transfer_len;
831 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
833 int tempcount = transfer_len;
834 u32 exponent = 8; /* 2^8 = 256 */
835 u8 imm_data = tempcount & 0xFF;
837 tempcount &= ~(tempcount & 0xFF);
838 /* Immediate entry */
839 if (tempcount != 0) {
840 /* Exponent entries */
841 genfifoentry |= GQSPI_GENFIFO_EXP;
842 while (tempcount != 0) {
843 if (tempcount & GQSPI_GENFIFO_EXP_START) {
845 ~GQSPI_GENFIFO_IMM_DATA_MASK;
846 genfifoentry |= exponent;
847 zynqmp_gqspi_write(xqspi,
851 tempcount = tempcount >> 1;
856 genfifoentry &= ~GQSPI_GENFIFO_EXP;
857 genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
858 genfifoentry |= (u8) (imm_data & 0xFF);
859 zynqmp_gqspi_write(xqspi,
860 GQSPI_GEN_FIFO_OFST, genfifoentry);
864 if ((xqspi->mode == GQSPI_MODE_IO) &&
865 (xqspi->rxbuf != NULL)) {
866 /* Dummy generic FIFO entry */
867 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
870 /* Since we are using manual mode */
871 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
872 zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
873 GQSPI_CFG_START_GEN_FIFO_MASK);
875 if (xqspi->txbuf != NULL)
876 /* Enable interrupts for TX */
877 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
878 GQSPI_IER_TXEMPTY_MASK |
879 GQSPI_IER_GENFIFOEMPTY_MASK |
880 GQSPI_IER_TXNOT_FULL_MASK);
882 if (xqspi->rxbuf != NULL) {
883 /* Enable interrupts for RX */
884 if (xqspi->mode == GQSPI_MODE_DMA) {
885 /* Enable DMA interrupts */
886 zynqmp_gqspi_write(xqspi,
887 GQSPI_QSPIDMA_DST_I_EN_OFST,
888 GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
890 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
891 GQSPI_IER_GENFIFOEMPTY_MASK |
892 GQSPI_IER_RXNEMPTY_MASK |
893 GQSPI_IER_RXEMPTY_MASK);
897 return transfer->len;
901 * zynqmp_qspi_suspend: Suspend method for the QSPI driver
902 * @_dev: Address of the platform_device structure
904 * This function stops the QSPI driver queue and disables the QSPI controller
908 static int __maybe_unused zynqmp_qspi_suspend(struct device *dev)
910 struct spi_master *master = dev_get_drvdata(dev);
912 spi_master_suspend(master);
914 zynqmp_unprepare_transfer_hardware(master);
920 * zynqmp_qspi_resume: Resume method for the QSPI driver
921 * @dev: Address of the platform_device structure
923 * The function starts the QSPI driver queue and initializes the QSPI
926 * Return: 0 on success; error value otherwise
928 static int __maybe_unused zynqmp_qspi_resume(struct device *dev)
930 struct spi_master *master = dev_get_drvdata(dev);
931 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
934 ret = clk_enable(xqspi->pclk);
936 dev_err(dev, "Cannot enable APB clock.\n");
940 ret = clk_enable(xqspi->refclk);
942 dev_err(dev, "Cannot enable device clock.\n");
943 clk_disable(xqspi->pclk);
947 spi_master_resume(master);
949 clk_disable(xqspi->refclk);
950 clk_disable(xqspi->pclk);
955 * zynqmp_runtime_suspend - Runtime suspend method for the SPI driver
956 * @dev: Address of the platform_device structure
958 * This function disables the clocks
962 static int __maybe_unused zynqmp_runtime_suspend(struct device *dev)
964 struct spi_master *master = dev_get_drvdata(dev);
965 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
967 clk_disable(xqspi->refclk);
968 clk_disable(xqspi->pclk);
974 * zynqmp_runtime_resume - Runtime resume method for the SPI driver
975 * @dev: Address of the platform_device structure
977 * This function enables the clocks
979 * Return: 0 on success and error value on error
981 static int __maybe_unused zynqmp_runtime_resume(struct device *dev)
983 struct spi_master *master = dev_get_drvdata(dev);
984 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
987 ret = clk_enable(xqspi->pclk);
989 dev_err(dev, "Cannot enable APB clock.\n");
993 ret = clk_enable(xqspi->refclk);
995 dev_err(dev, "Cannot enable device clock.\n");
996 clk_disable(xqspi->pclk);
1003 static const struct dev_pm_ops zynqmp_qspi_dev_pm_ops = {
1004 SET_RUNTIME_PM_OPS(zynqmp_runtime_suspend,
1005 zynqmp_runtime_resume, NULL)
1006 SET_SYSTEM_SLEEP_PM_OPS(zynqmp_qspi_suspend, zynqmp_qspi_resume)
1010 * zynqmp_qspi_probe: Probe method for the QSPI driver
1011 * @pdev: Pointer to the platform_device structure
1013 * This function initializes the driver data structures and the hardware.
1015 * Return: 0 on success; error value otherwise
1017 static int zynqmp_qspi_probe(struct platform_device *pdev)
1020 struct spi_master *master;
1021 struct zynqmp_qspi *xqspi;
1022 struct resource *res;
1023 struct device *dev = &pdev->dev;
1025 eemi_ops = zynqmp_pm_get_eemi_ops();
1026 if (IS_ERR(eemi_ops))
1027 return PTR_ERR(eemi_ops);
1029 master = spi_alloc_master(&pdev->dev, sizeof(*xqspi));
1033 xqspi = spi_master_get_devdata(master);
1034 master->dev.of_node = pdev->dev.of_node;
1035 platform_set_drvdata(pdev, master);
1037 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1038 xqspi->regs = devm_ioremap_resource(&pdev->dev, res);
1039 if (IS_ERR(xqspi->regs)) {
1040 ret = PTR_ERR(xqspi->regs);
1045 xqspi->pclk = devm_clk_get(&pdev->dev, "pclk");
1046 if (IS_ERR(xqspi->pclk)) {
1047 dev_err(dev, "pclk clock not found.\n");
1048 ret = PTR_ERR(xqspi->pclk);
1052 ret = clk_prepare_enable(xqspi->pclk);
1054 dev_err(dev, "Unable to enable APB clock.\n");
1058 xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk");
1059 if (IS_ERR(xqspi->refclk)) {
1060 dev_err(dev, "ref_clk clock not found.\n");
1061 ret = PTR_ERR(xqspi->refclk);
1065 ret = clk_prepare_enable(xqspi->refclk);
1067 dev_err(dev, "Unable to enable device clock.\n");
1071 pm_runtime_use_autosuspend(&pdev->dev);
1072 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1073 pm_runtime_set_active(&pdev->dev);
1074 pm_runtime_enable(&pdev->dev);
1075 /* QSPI controller initializations */
1076 zynqmp_qspi_init_hw(xqspi);
1078 pm_runtime_mark_last_busy(&pdev->dev);
1079 pm_runtime_put_autosuspend(&pdev->dev);
1080 xqspi->irq = platform_get_irq(pdev, 0);
1081 if (xqspi->irq <= 0) {
1083 dev_err(dev, "irq resource not found\n");
1086 ret = devm_request_irq(&pdev->dev, xqspi->irq, zynqmp_qspi_irq,
1087 0, pdev->name, master);
1090 dev_err(dev, "request_irq failed\n");
1094 master->num_chipselect = GQSPI_DEFAULT_NUM_CS;
1096 master->setup = zynqmp_qspi_setup;
1097 master->set_cs = zynqmp_qspi_chipselect;
1098 master->transfer_one = zynqmp_qspi_start_transfer;
1099 master->prepare_transfer_hardware = zynqmp_prepare_transfer_hardware;
1100 master->unprepare_transfer_hardware =
1101 zynqmp_unprepare_transfer_hardware;
1102 master->max_speed_hz = clk_get_rate(xqspi->refclk) / 2;
1103 master->bits_per_word_mask = SPI_BPW_MASK(8);
1104 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD |
1105 SPI_TX_DUAL | SPI_TX_QUAD;
1107 if (master->dev.parent == NULL)
1108 master->dev.parent = &master->dev;
1110 ret = spi_register_master(master);
1117 pm_runtime_set_suspended(&pdev->dev);
1118 pm_runtime_disable(&pdev->dev);
1119 clk_disable_unprepare(xqspi->refclk);
1121 clk_disable_unprepare(xqspi->pclk);
1123 spi_master_put(master);
1129 * zynqmp_qspi_remove: Remove method for the QSPI driver
1130 * @pdev: Pointer to the platform_device structure
1132 * This function is called if a device is physically removed from the system or
1133 * if the driver module is being unloaded. It frees all resources allocated to
1138 static int zynqmp_qspi_remove(struct platform_device *pdev)
1140 struct spi_master *master = platform_get_drvdata(pdev);
1141 struct zynqmp_qspi *xqspi = spi_master_get_devdata(master);
1143 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
1144 clk_disable_unprepare(xqspi->refclk);
1145 clk_disable_unprepare(xqspi->pclk);
1146 pm_runtime_set_suspended(&pdev->dev);
1147 pm_runtime_disable(&pdev->dev);
1149 spi_unregister_master(master);
1154 static const struct of_device_id zynqmp_qspi_of_match[] = {
1155 { .compatible = "xlnx,zynqmp-qspi-1.0", },
1156 { /* End of table */ }
1159 MODULE_DEVICE_TABLE(of, zynqmp_qspi_of_match);
1161 static struct platform_driver zynqmp_qspi_driver = {
1162 .probe = zynqmp_qspi_probe,
1163 .remove = zynqmp_qspi_remove,
1165 .name = "zynqmp-qspi",
1166 .of_match_table = zynqmp_qspi_of_match,
1167 .pm = &zynqmp_qspi_dev_pm_ops,
1171 module_platform_driver(zynqmp_qspi_driver);
1173 MODULE_AUTHOR("Xilinx, Inc.");
1174 MODULE_DESCRIPTION("Xilinx Zynqmp QSPI driver");
1175 MODULE_LICENSE("GPL");