1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Xilinx Zynq UltraScale+ MPSoC Quad-SPI (QSPI) controller driver
6 * Copyright (C) 2009 - 2015 Xilinx, Inc.
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/firmware/xlnx-zynqmp.h>
14 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spinlock.h>
23 #include <linux/workqueue.h>
24 #include <linux/spi/spi-mem.h>
26 /* Generic QSPI register offsets */
27 #define GQSPI_CONFIG_OFST 0x00000100
28 #define GQSPI_ISR_OFST 0x00000104
29 #define GQSPI_IDR_OFST 0x0000010C
30 #define GQSPI_IER_OFST 0x00000108
31 #define GQSPI_IMASK_OFST 0x00000110
32 #define GQSPI_EN_OFST 0x00000114
33 #define GQSPI_TXD_OFST 0x0000011C
34 #define GQSPI_RXD_OFST 0x00000120
35 #define GQSPI_TX_THRESHOLD_OFST 0x00000128
36 #define GQSPI_RX_THRESHOLD_OFST 0x0000012C
37 #define GQSPI_LPBK_DLY_ADJ_OFST 0x00000138
38 #define GQSPI_GEN_FIFO_OFST 0x00000140
39 #define GQSPI_SEL_OFST 0x00000144
40 #define GQSPI_GF_THRESHOLD_OFST 0x00000150
41 #define GQSPI_FIFO_CTRL_OFST 0x0000014C
42 #define GQSPI_QSPIDMA_DST_CTRL_OFST 0x0000080C
43 #define GQSPI_QSPIDMA_DST_SIZE_OFST 0x00000804
44 #define GQSPI_QSPIDMA_DST_STS_OFST 0x00000808
45 #define GQSPI_QSPIDMA_DST_I_STS_OFST 0x00000814
46 #define GQSPI_QSPIDMA_DST_I_EN_OFST 0x00000818
47 #define GQSPI_QSPIDMA_DST_I_DIS_OFST 0x0000081C
48 #define GQSPI_QSPIDMA_DST_I_MASK_OFST 0x00000820
49 #define GQSPI_QSPIDMA_DST_ADDR_OFST 0x00000800
50 #define GQSPI_QSPIDMA_DST_ADDR_MSB_OFST 0x00000828
52 /* GQSPI register bit masks */
53 #define GQSPI_SEL_MASK 0x00000001
54 #define GQSPI_EN_MASK 0x00000001
55 #define GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK 0x00000020
56 #define GQSPI_ISR_WR_TO_CLR_MASK 0x00000002
57 #define GQSPI_IDR_ALL_MASK 0x00000FBE
58 #define GQSPI_CFG_MODE_EN_MASK 0xC0000000
59 #define GQSPI_CFG_GEN_FIFO_START_MODE_MASK 0x20000000
60 #define GQSPI_CFG_ENDIAN_MASK 0x04000000
61 #define GQSPI_CFG_EN_POLL_TO_MASK 0x00100000
62 #define GQSPI_CFG_WP_HOLD_MASK 0x00080000
63 #define GQSPI_CFG_BAUD_RATE_DIV_MASK 0x00000038
64 #define GQSPI_CFG_CLK_PHA_MASK 0x00000004
65 #define GQSPI_CFG_CLK_POL_MASK 0x00000002
66 #define GQSPI_CFG_START_GEN_FIFO_MASK 0x10000000
67 #define GQSPI_GENFIFO_IMM_DATA_MASK 0x000000FF
68 #define GQSPI_GENFIFO_DATA_XFER 0x00000100
69 #define GQSPI_GENFIFO_EXP 0x00000200
70 #define GQSPI_GENFIFO_MODE_SPI 0x00000400
71 #define GQSPI_GENFIFO_MODE_DUALSPI 0x00000800
72 #define GQSPI_GENFIFO_MODE_QUADSPI 0x00000C00
73 #define GQSPI_GENFIFO_MODE_MASK 0x00000C00
74 #define GQSPI_GENFIFO_CS_LOWER 0x00001000
75 #define GQSPI_GENFIFO_CS_UPPER 0x00002000
76 #define GQSPI_GENFIFO_BUS_LOWER 0x00004000
77 #define GQSPI_GENFIFO_BUS_UPPER 0x00008000
78 #define GQSPI_GENFIFO_BUS_BOTH 0x0000C000
79 #define GQSPI_GENFIFO_BUS_MASK 0x0000C000
80 #define GQSPI_GENFIFO_TX 0x00010000
81 #define GQSPI_GENFIFO_RX 0x00020000
82 #define GQSPI_GENFIFO_STRIPE 0x00040000
83 #define GQSPI_GENFIFO_POLL 0x00080000
84 #define GQSPI_GENFIFO_EXP_START 0x00000100
85 #define GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK 0x00000004
86 #define GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK 0x00000002
87 #define GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK 0x00000001
88 #define GQSPI_ISR_RXEMPTY_MASK 0x00000800
89 #define GQSPI_ISR_GENFIFOFULL_MASK 0x00000400
90 #define GQSPI_ISR_GENFIFONOT_FULL_MASK 0x00000200
91 #define GQSPI_ISR_TXEMPTY_MASK 0x00000100
92 #define GQSPI_ISR_GENFIFOEMPTY_MASK 0x00000080
93 #define GQSPI_ISR_RXFULL_MASK 0x00000020
94 #define GQSPI_ISR_RXNEMPTY_MASK 0x00000010
95 #define GQSPI_ISR_TXFULL_MASK 0x00000008
96 #define GQSPI_ISR_TXNOT_FULL_MASK 0x00000004
97 #define GQSPI_ISR_POLL_TIME_EXPIRE_MASK 0x00000002
98 #define GQSPI_IER_TXNOT_FULL_MASK 0x00000004
99 #define GQSPI_IER_RXEMPTY_MASK 0x00000800
100 #define GQSPI_IER_POLL_TIME_EXPIRE_MASK 0x00000002
101 #define GQSPI_IER_RXNEMPTY_MASK 0x00000010
102 #define GQSPI_IER_GENFIFOEMPTY_MASK 0x00000080
103 #define GQSPI_IER_TXEMPTY_MASK 0x00000100
104 #define GQSPI_QSPIDMA_DST_INTR_ALL_MASK 0x000000FE
105 #define GQSPI_QSPIDMA_DST_STS_WTC 0x0000E000
106 #define GQSPI_CFG_MODE_EN_DMA_MASK 0x80000000
107 #define GQSPI_ISR_IDR_MASK 0x00000994
108 #define GQSPI_QSPIDMA_DST_I_EN_DONE_MASK 0x00000002
109 #define GQSPI_QSPIDMA_DST_I_STS_DONE_MASK 0x00000002
110 #define GQSPI_IRQ_MASK 0x00000980
112 #define GQSPI_CFG_BAUD_RATE_DIV_SHIFT 3
113 #define GQSPI_GENFIFO_CS_SETUP 0x4
114 #define GQSPI_GENFIFO_CS_HOLD 0x3
115 #define GQSPI_TXD_DEPTH 64
116 #define GQSPI_RX_FIFO_THRESHOLD 32
117 #define GQSPI_RX_FIFO_FILL (GQSPI_RX_FIFO_THRESHOLD * 4)
118 #define GQSPI_TX_FIFO_THRESHOLD_RESET_VAL 32
119 #define GQSPI_TX_FIFO_FILL (GQSPI_TXD_DEPTH -\
120 GQSPI_TX_FIFO_THRESHOLD_RESET_VAL)
121 #define GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL 0X10
122 #define GQSPI_QSPIDMA_DST_CTRL_RESET_VAL 0x803FFA00
123 #define GQSPI_SELECT_FLASH_CS_LOWER 0x1
124 #define GQSPI_SELECT_FLASH_CS_UPPER 0x2
125 #define GQSPI_SELECT_FLASH_CS_BOTH 0x3
126 #define GQSPI_SELECT_FLASH_BUS_LOWER 0x1
127 #define GQSPI_SELECT_FLASH_BUS_UPPER 0x2
128 #define GQSPI_SELECT_FLASH_BUS_BOTH 0x3
129 #define GQSPI_BAUD_DIV_MAX 7 /* Baud rate divisor maximum */
130 #define GQSPI_BAUD_DIV_SHIFT 2 /* Baud rate divisor shift */
131 #define GQSPI_SELECT_MODE_SPI 0x1
132 #define GQSPI_SELECT_MODE_DUALSPI 0x2
133 #define GQSPI_SELECT_MODE_QUADSPI 0x4
134 #define GQSPI_DMA_UNALIGN 0x3
135 #define GQSPI_DEFAULT_NUM_CS 1 /* Default number of chip selects */
137 #define GQSPI_MAX_NUM_CS 2 /* Maximum number of chip selects */
139 #define SPI_AUTOSUSPEND_TIMEOUT 3000
140 enum mode_type {GQSPI_MODE_IO, GQSPI_MODE_DMA};
143 * struct zynqmp_qspi - Defines qspi driver instance
144 * @regs: Virtual address of the QSPI controller registers
145 * @refclk: Pointer to the peripheral clock
146 * @pclk: Pointer to the APB clock
148 * @dev: Pointer to struct device
149 * @txbuf: Pointer to the TX buffer
150 * @rxbuf: Pointer to the RX buffer
151 * @bytes_to_transfer: Number of bytes left to transfer
152 * @bytes_to_receive: Number of bytes left to receive
153 * @genfifocs: Used for chip select
154 * @genfifobus: Used to select the upper or lower bus
155 * @dma_rx_bytes: Remaining bytes to receive by DMA mode
156 * @dma_addr: DMA address after mapping the kernel buffer
157 * @genfifoentry: Used for storing the genfifoentry instruction.
158 * @mode: Defines the mode in which QSPI is operating
159 * @data_completion: completion structure
162 struct spi_controller *ctlr;
170 int bytes_to_transfer;
171 int bytes_to_receive;
178 struct completion data_completion;
179 struct mutex op_lock;
183 * zynqmp_gqspi_read - For GQSPI controller read operation
184 * @xqspi: Pointer to the zynqmp_qspi structure
185 * @offset: Offset from where to read
186 * Return: Value at the offset
188 static u32 zynqmp_gqspi_read(struct zynqmp_qspi *xqspi, u32 offset)
190 return readl_relaxed(xqspi->regs + offset);
194 * zynqmp_gqspi_write - For GQSPI controller write operation
195 * @xqspi: Pointer to the zynqmp_qspi structure
196 * @offset: Offset where to write
197 * @val: Value to be written
199 static inline void zynqmp_gqspi_write(struct zynqmp_qspi *xqspi, u32 offset,
202 writel_relaxed(val, (xqspi->regs + offset));
206 * zynqmp_gqspi_selectslave - For selection of slave device
207 * @instanceptr: Pointer to the zynqmp_qspi structure
208 * @slavecs: For chip select
209 * @slavebus: To check which bus is selected- upper or lower
211 static void zynqmp_gqspi_selectslave(struct zynqmp_qspi *instanceptr,
212 u8 slavecs, u8 slavebus)
215 * Bus and CS lines selected here will be updated in the instance and
216 * used for subsequent GENFIFO entries during transfer.
219 /* Choose slave select line */
221 case GQSPI_SELECT_FLASH_CS_BOTH:
222 instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER |
223 GQSPI_GENFIFO_CS_UPPER;
225 case GQSPI_SELECT_FLASH_CS_UPPER:
226 instanceptr->genfifocs = GQSPI_GENFIFO_CS_UPPER;
228 case GQSPI_SELECT_FLASH_CS_LOWER:
229 instanceptr->genfifocs = GQSPI_GENFIFO_CS_LOWER;
232 dev_warn(instanceptr->dev, "Invalid slave select\n");
237 case GQSPI_SELECT_FLASH_BUS_BOTH:
238 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER |
239 GQSPI_GENFIFO_BUS_UPPER;
241 case GQSPI_SELECT_FLASH_BUS_UPPER:
242 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_UPPER;
244 case GQSPI_SELECT_FLASH_BUS_LOWER:
245 instanceptr->genfifobus = GQSPI_GENFIFO_BUS_LOWER;
248 dev_warn(instanceptr->dev, "Invalid slave bus\n");
253 * zynqmp_qspi_init_hw - Initialize the hardware
254 * @xqspi: Pointer to the zynqmp_qspi structure
256 * The default settings of the QSPI controller's configurable parameters on
259 * - TX threshold set to 1
260 * - RX threshold set to 1
261 * - Flash memory interface mode enabled
262 * This function performs the following actions
263 * - Disable and clear all the interrupts
264 * - Enable manual slave select
265 * - Enable manual start
266 * - Deselect all the chip select lines
267 * - Set the little endian mode of TX FIFO and
268 * - Enable the QSPI controller
270 static void zynqmp_qspi_init_hw(struct zynqmp_qspi *xqspi)
274 /* Select the GQSPI mode */
275 zynqmp_gqspi_write(xqspi, GQSPI_SEL_OFST, GQSPI_SEL_MASK);
276 /* Clear and disable interrupts */
277 zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST,
278 zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST) |
279 GQSPI_ISR_WR_TO_CLR_MASK);
280 /* Clear the DMA STS */
281 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
282 zynqmp_gqspi_read(xqspi,
283 GQSPI_QSPIDMA_DST_I_STS_OFST));
284 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_STS_OFST,
285 zynqmp_gqspi_read(xqspi,
286 GQSPI_QSPIDMA_DST_STS_OFST) |
287 GQSPI_QSPIDMA_DST_STS_WTC);
288 zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_IDR_ALL_MASK);
289 zynqmp_gqspi_write(xqspi,
290 GQSPI_QSPIDMA_DST_I_DIS_OFST,
291 GQSPI_QSPIDMA_DST_INTR_ALL_MASK);
292 /* Disable the GQSPI */
293 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
294 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
295 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
297 config_reg |= GQSPI_CFG_GEN_FIFO_START_MODE_MASK;
298 /* Little endian by default */
299 config_reg &= ~GQSPI_CFG_ENDIAN_MASK;
300 /* Disable poll time out */
301 config_reg &= ~GQSPI_CFG_EN_POLL_TO_MASK;
303 config_reg |= GQSPI_CFG_WP_HOLD_MASK;
304 /* Clear pre-scalar by default */
305 config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
307 config_reg &= ~GQSPI_CFG_CLK_PHA_MASK;
309 config_reg &= ~GQSPI_CFG_CLK_POL_MASK;
310 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
312 /* Clear the TX and RX FIFO */
313 zynqmp_gqspi_write(xqspi, GQSPI_FIFO_CTRL_OFST,
314 GQSPI_FIFO_CTRL_RST_RX_FIFO_MASK |
315 GQSPI_FIFO_CTRL_RST_TX_FIFO_MASK |
316 GQSPI_FIFO_CTRL_RST_GEN_FIFO_MASK);
317 /* Set by default to allow for high frequencies */
318 zynqmp_gqspi_write(xqspi, GQSPI_LPBK_DLY_ADJ_OFST,
319 zynqmp_gqspi_read(xqspi, GQSPI_LPBK_DLY_ADJ_OFST) |
320 GQSPI_LPBK_DLY_ADJ_USE_LPBK_MASK);
321 /* Reset thresholds */
322 zynqmp_gqspi_write(xqspi, GQSPI_TX_THRESHOLD_OFST,
323 GQSPI_TX_FIFO_THRESHOLD_RESET_VAL);
324 zynqmp_gqspi_write(xqspi, GQSPI_RX_THRESHOLD_OFST,
325 GQSPI_RX_FIFO_THRESHOLD);
326 zynqmp_gqspi_write(xqspi, GQSPI_GF_THRESHOLD_OFST,
327 GQSPI_GEN_FIFO_THRESHOLD_RESET_VAL);
328 zynqmp_gqspi_selectslave(xqspi,
329 GQSPI_SELECT_FLASH_CS_LOWER,
330 GQSPI_SELECT_FLASH_BUS_LOWER);
332 zynqmp_gqspi_write(xqspi,
333 GQSPI_QSPIDMA_DST_CTRL_OFST,
334 GQSPI_QSPIDMA_DST_CTRL_RESET_VAL);
336 /* Enable the GQSPI */
337 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
341 * zynqmp_qspi_copy_read_data - Copy data to RX buffer
342 * @xqspi: Pointer to the zynqmp_qspi structure
343 * @data: The variable where data is stored
344 * @size: Number of bytes to be copied from data to RX buffer
346 static void zynqmp_qspi_copy_read_data(struct zynqmp_qspi *xqspi,
349 memcpy(xqspi->rxbuf, &data, size);
350 xqspi->rxbuf += size;
351 xqspi->bytes_to_receive -= size;
355 * zynqmp_qspi_chipselect - Select or deselect the chip select line
356 * @qspi: Pointer to the spi_device structure
357 * @is_high: Select(0) or deselect (1) the chip select line
359 static void zynqmp_qspi_chipselect(struct spi_device *qspi, bool is_high)
361 struct zynqmp_qspi *xqspi = spi_master_get_devdata(qspi->master);
363 u32 genfifoentry = 0, statusreg;
365 genfifoentry |= GQSPI_GENFIFO_MODE_SPI;
368 if (!qspi->chip_select) {
369 xqspi->genfifobus = GQSPI_GENFIFO_BUS_LOWER;
370 xqspi->genfifocs = GQSPI_GENFIFO_CS_LOWER;
372 xqspi->genfifobus = GQSPI_GENFIFO_BUS_UPPER;
373 xqspi->genfifocs = GQSPI_GENFIFO_CS_UPPER;
375 genfifoentry |= xqspi->genfifobus;
376 genfifoentry |= xqspi->genfifocs;
377 genfifoentry |= GQSPI_GENFIFO_CS_SETUP;
379 genfifoentry |= GQSPI_GENFIFO_CS_HOLD;
382 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
384 /* Manually start the generic FIFO command */
385 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
386 zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
387 GQSPI_CFG_START_GEN_FIFO_MASK);
389 timeout = jiffies + msecs_to_jiffies(1000);
391 /* Wait until the generic FIFO command is empty */
393 statusreg = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
395 if ((statusreg & GQSPI_ISR_GENFIFOEMPTY_MASK) &&
396 (statusreg & GQSPI_ISR_TXEMPTY_MASK))
399 } while (!time_after_eq(jiffies, timeout));
401 if (time_after_eq(jiffies, timeout))
402 dev_err(xqspi->dev, "Chip select timed out\n");
406 * zynqmp_qspi_selectspimode - Selects SPI mode - x1 or x2 or x4.
407 * @xqspi: xqspi is a pointer to the GQSPI instance
408 * @spimode: spimode - SPI or DUAL or QUAD.
409 * Return: Mask to set desired SPI mode in GENFIFO entry.
411 static inline u32 zynqmp_qspi_selectspimode(struct zynqmp_qspi *xqspi,
417 case GQSPI_SELECT_MODE_DUALSPI:
418 mask = GQSPI_GENFIFO_MODE_DUALSPI;
420 case GQSPI_SELECT_MODE_QUADSPI:
421 mask = GQSPI_GENFIFO_MODE_QUADSPI;
423 case GQSPI_SELECT_MODE_SPI:
424 mask = GQSPI_GENFIFO_MODE_SPI;
427 dev_warn(xqspi->dev, "Invalid SPI mode\n");
434 * zynqmp_qspi_config_op - Configure QSPI controller for specified
436 * @xqspi: Pointer to the zynqmp_qspi structure
437 * @qspi: Pointer to the spi_device structure
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_config_op(struct zynqmp_qspi *xqspi,
455 struct spi_device *qspi)
458 u32 config_reg, baud_rate_val = 0;
460 /* Set the clock frequency */
461 /* If req_hz == 0, default to lowest speed */
462 clk_rate = clk_get_rate(xqspi->refclk);
464 while ((baud_rate_val < GQSPI_BAUD_DIV_MAX) &&
466 (GQSPI_BAUD_DIV_SHIFT << baud_rate_val)) > qspi->max_speed_hz)
469 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
471 /* Set the QSPI clock phase and clock polarity */
472 config_reg &= (~GQSPI_CFG_CLK_PHA_MASK) & (~GQSPI_CFG_CLK_POL_MASK);
474 if (qspi->mode & SPI_CPHA)
475 config_reg |= GQSPI_CFG_CLK_PHA_MASK;
476 if (qspi->mode & SPI_CPOL)
477 config_reg |= GQSPI_CFG_CLK_POL_MASK;
479 config_reg &= ~GQSPI_CFG_BAUD_RATE_DIV_MASK;
480 config_reg |= (baud_rate_val << GQSPI_CFG_BAUD_RATE_DIV_SHIFT);
481 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
486 * zynqmp_qspi_setup_op - Configure the QSPI controller
487 * @qspi: Pointer to the spi_device structure
489 * Sets the operational mode of QSPI controller for the next QSPI transfer,
490 * baud rate and divisor value to setup the requested qspi clock.
492 * Return: 0 on success; error value otherwise.
494 static int zynqmp_qspi_setup_op(struct spi_device *qspi)
496 struct spi_controller *ctlr = qspi->master;
497 struct zynqmp_qspi *xqspi = spi_controller_get_devdata(ctlr);
502 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
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) && (xqspi->txbuf)) {
519 if (xqspi->bytes_to_transfer >= 4) {
520 memcpy(&intermediate, xqspi->txbuf, 4);
522 xqspi->bytes_to_transfer -= 4;
525 memcpy(&intermediate, xqspi->txbuf,
526 xqspi->bytes_to_transfer);
527 xqspi->txbuf += xqspi->bytes_to_transfer;
528 xqspi->bytes_to_transfer = 0;
529 count += xqspi->bytes_to_transfer;
531 zynqmp_gqspi_write(xqspi, GQSPI_TXD_OFST, intermediate);
536 * zynqmp_qspi_readrxfifo - Fills the RX FIFO as long as there is room in
538 * @xqspi: Pointer to the zynqmp_qspi structure
539 * @size: Number of bytes to be copied from RX buffer to RX FIFO
541 static void zynqmp_qspi_readrxfifo(struct zynqmp_qspi *xqspi, u32 size)
546 while ((count < size) && (xqspi->bytes_to_receive > 0)) {
547 if (xqspi->bytes_to_receive >= 4) {
548 (*(u32 *)xqspi->rxbuf) =
549 zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
551 xqspi->bytes_to_receive -= 4;
554 data = zynqmp_gqspi_read(xqspi, GQSPI_RXD_OFST);
555 count += xqspi->bytes_to_receive;
556 zynqmp_qspi_copy_read_data(xqspi, data,
557 xqspi->bytes_to_receive);
558 xqspi->bytes_to_receive = 0;
564 * zynqmp_qspi_fillgenfifo - Fills the GENFIFO.
565 * @xqspi: Pointer to the zynqmp_qspi structure
566 * @nbits: Transfer/Receive buswidth.
567 * @genfifoentry: Variable in which GENFIFO mask is saved
569 static void zynqmp_qspi_fillgenfifo(struct zynqmp_qspi *xqspi, u8 nbits,
572 u32 transfer_len = 0;
575 genfifoentry &= ~GQSPI_GENFIFO_RX;
576 genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
577 genfifoentry |= GQSPI_GENFIFO_TX;
578 transfer_len = xqspi->bytes_to_transfer;
579 } else if (xqspi->rxbuf) {
580 genfifoentry &= ~GQSPI_GENFIFO_TX;
581 genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
582 genfifoentry |= GQSPI_GENFIFO_RX;
583 if (xqspi->mode == GQSPI_MODE_DMA)
584 transfer_len = xqspi->dma_rx_bytes;
586 transfer_len = xqspi->bytes_to_receive;
588 /* Sending dummy circles here */
589 genfifoentry &= ~(GQSPI_GENFIFO_TX | GQSPI_GENFIFO_RX);
590 genfifoentry |= GQSPI_GENFIFO_DATA_XFER;
591 transfer_len = xqspi->bytes_to_transfer;
593 genfifoentry |= zynqmp_qspi_selectspimode(xqspi, nbits);
594 xqspi->genfifoentry = genfifoentry;
596 if ((transfer_len) < GQSPI_GENFIFO_IMM_DATA_MASK) {
597 genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
598 genfifoentry |= transfer_len;
599 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
601 int tempcount = transfer_len;
602 u32 exponent = 8; /* 2^8 = 256 */
603 u8 imm_data = tempcount & 0xFF;
605 tempcount &= ~(tempcount & 0xFF);
606 /* Immediate entry */
607 if (tempcount != 0) {
608 /* Exponent entries */
609 genfifoentry |= GQSPI_GENFIFO_EXP;
610 while (tempcount != 0) {
611 if (tempcount & GQSPI_GENFIFO_EXP_START) {
613 ~GQSPI_GENFIFO_IMM_DATA_MASK;
614 genfifoentry |= exponent;
615 zynqmp_gqspi_write(xqspi,
619 tempcount = tempcount >> 1;
624 genfifoentry &= ~GQSPI_GENFIFO_EXP;
625 genfifoentry &= ~GQSPI_GENFIFO_IMM_DATA_MASK;
626 genfifoentry |= (u8)(imm_data & 0xFF);
627 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST,
631 if (xqspi->mode == GQSPI_MODE_IO && xqspi->rxbuf) {
632 /* Dummy generic FIFO entry */
633 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
638 * zynqmp_process_dma_irq - Handler for DMA done interrupt of QSPI
640 * @xqspi: zynqmp_qspi instance pointer
642 * This function handles DMA interrupt only.
644 static void zynqmp_process_dma_irq(struct zynqmp_qspi *xqspi)
646 u32 config_reg, genfifoentry;
648 dma_unmap_single(xqspi->dev, xqspi->dma_addr,
649 xqspi->dma_rx_bytes, DMA_FROM_DEVICE);
650 xqspi->rxbuf += xqspi->dma_rx_bytes;
651 xqspi->bytes_to_receive -= xqspi->dma_rx_bytes;
652 xqspi->dma_rx_bytes = 0;
654 /* Disabling the DMA interrupts */
655 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_DIS_OFST,
656 GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
658 if (xqspi->bytes_to_receive > 0) {
659 /* Switch to IO mode,for remaining bytes to receive */
660 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
661 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
662 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
664 /* Initiate the transfer of remaining bytes */
665 genfifoentry = xqspi->genfifoentry;
666 genfifoentry |= xqspi->bytes_to_receive;
667 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, genfifoentry);
669 /* Dummy generic FIFO entry */
670 zynqmp_gqspi_write(xqspi, GQSPI_GEN_FIFO_OFST, 0x0);
673 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
674 (zynqmp_gqspi_read(xqspi,
676 GQSPI_CFG_START_GEN_FIFO_MASK));
678 /* Enable the RX interrupts for IO mode */
679 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
680 GQSPI_IER_GENFIFOEMPTY_MASK |
681 GQSPI_IER_RXNEMPTY_MASK |
682 GQSPI_IER_RXEMPTY_MASK);
687 * zynqmp_qspi_irq - Interrupt service routine of the QSPI controller
689 * @dev_id: Pointer to the xqspi structure
691 * This function handles TX empty only.
692 * On TX empty interrupt this function reads the received data from RX FIFO
693 * and fills the TX FIFO if there is any data remaining to be transferred.
695 * Return: IRQ_HANDLED when interrupt is handled
696 * IRQ_NONE otherwise.
698 static irqreturn_t zynqmp_qspi_irq(int irq, void *dev_id)
700 struct zynqmp_qspi *xqspi = (struct zynqmp_qspi *)dev_id;
701 irqreturn_t ret = IRQ_NONE;
702 u32 status, mask, dma_status = 0;
704 status = zynqmp_gqspi_read(xqspi, GQSPI_ISR_OFST);
705 zynqmp_gqspi_write(xqspi, GQSPI_ISR_OFST, status);
706 mask = (status & ~(zynqmp_gqspi_read(xqspi, GQSPI_IMASK_OFST)));
708 /* Read and clear DMA status */
709 if (xqspi->mode == GQSPI_MODE_DMA) {
711 zynqmp_gqspi_read(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST);
712 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_I_STS_OFST,
716 if (mask & GQSPI_ISR_TXNOT_FULL_MASK) {
717 zynqmp_qspi_filltxfifo(xqspi, GQSPI_TX_FIFO_FILL);
721 if (dma_status & GQSPI_QSPIDMA_DST_I_STS_DONE_MASK) {
722 zynqmp_process_dma_irq(xqspi);
724 } else if (!(mask & GQSPI_IER_RXEMPTY_MASK) &&
725 (mask & GQSPI_IER_GENFIFOEMPTY_MASK)) {
726 zynqmp_qspi_readrxfifo(xqspi, GQSPI_RX_FIFO_FILL);
730 if (xqspi->bytes_to_receive == 0 && xqspi->bytes_to_transfer == 0 &&
731 ((status & GQSPI_IRQ_MASK) == GQSPI_IRQ_MASK)) {
732 zynqmp_gqspi_write(xqspi, GQSPI_IDR_OFST, GQSPI_ISR_IDR_MASK);
733 complete(&xqspi->data_completion);
740 * zynqmp_qspi_setuprxdma - This function sets up the RX DMA operation
741 * @xqspi: xqspi is a pointer to the GQSPI instance.
743 static int zynqmp_qspi_setuprxdma(struct zynqmp_qspi *xqspi)
745 u32 rx_bytes, rx_rem, config_reg;
747 u64 dma_align = (u64)(uintptr_t)xqspi->rxbuf;
749 if (xqspi->bytes_to_receive < 8 ||
750 ((dma_align & GQSPI_DMA_UNALIGN) != 0x0)) {
751 /* Setting to IO mode */
752 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
753 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
754 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
755 xqspi->mode = GQSPI_MODE_IO;
756 xqspi->dma_rx_bytes = 0;
760 rx_rem = xqspi->bytes_to_receive % 4;
761 rx_bytes = (xqspi->bytes_to_receive - rx_rem);
763 addr = dma_map_single(xqspi->dev, (void *)xqspi->rxbuf,
764 rx_bytes, DMA_FROM_DEVICE);
765 if (dma_mapping_error(xqspi->dev, addr)) {
766 dev_err(xqspi->dev, "ERR:rxdma:memory not mapped\n");
770 xqspi->dma_rx_bytes = rx_bytes;
771 xqspi->dma_addr = addr;
772 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_OFST,
773 (u32)(addr & 0xffffffff));
774 addr = ((addr >> 16) >> 16);
775 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_ADDR_MSB_OFST,
776 ((u32)addr) & 0xfff);
778 /* Enabling the DMA mode */
779 config_reg = zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST);
780 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
781 config_reg |= GQSPI_CFG_MODE_EN_DMA_MASK;
782 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST, config_reg);
784 /* Switch to DMA mode */
785 xqspi->mode = GQSPI_MODE_DMA;
787 /* Write the number of bytes to transfer */
788 zynqmp_gqspi_write(xqspi, GQSPI_QSPIDMA_DST_SIZE_OFST, rx_bytes);
794 * zynqmp_qspi_write_op - This function sets up the GENFIFO entries,
795 * TX FIFO, and fills the TX FIFO with as many
797 * @xqspi: Pointer to the GQSPI instance.
798 * @tx_nbits: Transfer buswidth.
799 * @genfifoentry: Variable in which GENFIFO mask is returned
800 * to calling function
802 static void zynqmp_qspi_write_op(struct zynqmp_qspi *xqspi, u8 tx_nbits,
807 zynqmp_qspi_fillgenfifo(xqspi, tx_nbits, genfifoentry);
808 zynqmp_qspi_filltxfifo(xqspi, GQSPI_TXD_DEPTH);
809 if (xqspi->mode == GQSPI_MODE_DMA) {
810 config_reg = zynqmp_gqspi_read(xqspi,
812 config_reg &= ~GQSPI_CFG_MODE_EN_MASK;
813 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
815 xqspi->mode = GQSPI_MODE_IO;
820 * zynqmp_qspi_read_op - This function sets up the GENFIFO entries and
822 * @xqspi: xqspi is a pointer to the GQSPI instance.
823 * @rx_nbits: Receive buswidth.
824 * @genfifoentry: genfifoentry is pointer to the variable in which
825 * GENFIFO mask is returned to calling function
827 static int zynqmp_qspi_read_op(struct zynqmp_qspi *xqspi, u8 rx_nbits,
832 ret = zynqmp_qspi_setuprxdma(xqspi);
835 zynqmp_qspi_fillgenfifo(xqspi, rx_nbits, genfifoentry);
841 * zynqmp_qspi_suspend - Suspend method for the QSPI driver
842 * @dev: Address of the platform_device structure
844 * This function stops the QSPI driver queue and disables the QSPI controller
848 static int __maybe_unused zynqmp_qspi_suspend(struct device *dev)
850 struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
851 struct spi_controller *ctlr = xqspi->ctlr;
854 ret = spi_controller_suspend(ctlr);
858 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
864 * zynqmp_qspi_resume - Resume method for the QSPI driver
865 * @dev: Address of the platform_device structure
867 * The function starts the QSPI driver queue and initializes the QSPI
870 * Return: 0 on success; error value otherwise
872 static int __maybe_unused zynqmp_qspi_resume(struct device *dev)
874 struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
875 struct spi_controller *ctlr = xqspi->ctlr;
877 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, GQSPI_EN_MASK);
879 spi_controller_resume(ctlr);
885 * zynqmp_runtime_suspend - Runtime suspend method for the SPI driver
886 * @dev: Address of the platform_device structure
888 * This function disables the clocks
892 static int __maybe_unused zynqmp_runtime_suspend(struct device *dev)
894 struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
896 clk_disable_unprepare(xqspi->refclk);
897 clk_disable_unprepare(xqspi->pclk);
903 * zynqmp_runtime_resume - Runtime resume method for the SPI driver
904 * @dev: Address of the platform_device structure
906 * This function enables the clocks
908 * Return: 0 on success and error value on error
910 static int __maybe_unused zynqmp_runtime_resume(struct device *dev)
912 struct zynqmp_qspi *xqspi = dev_get_drvdata(dev);
915 ret = clk_prepare_enable(xqspi->pclk);
917 dev_err(dev, "Cannot enable APB clock.\n");
921 ret = clk_prepare_enable(xqspi->refclk);
923 dev_err(dev, "Cannot enable device clock.\n");
924 clk_disable_unprepare(xqspi->pclk);
932 * zynqmp_qspi_exec_op() - Initiates the QSPI transfer
933 * @mem: The SPI memory
934 * @op: The memory operation to execute
936 * Executes a memory operation.
938 * This function first selects the chip and starts the memory operation.
940 * Return: 0 in case of success, a negative error code otherwise.
942 static int zynqmp_qspi_exec_op(struct spi_mem *mem,
943 const struct spi_mem_op *op)
945 struct zynqmp_qspi *xqspi = spi_controller_get_devdata
948 u32 genfifoentry = 0;
949 u16 opcode = op->cmd.opcode;
952 dev_dbg(xqspi->dev, "cmd:%#x mode:%d.%d.%d.%d\n",
953 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
954 op->dummy.buswidth, op->data.buswidth);
956 mutex_lock(&xqspi->op_lock);
957 zynqmp_qspi_config_op(xqspi, mem->spi);
958 zynqmp_qspi_chipselect(mem->spi, false);
959 genfifoentry |= xqspi->genfifocs;
960 genfifoentry |= xqspi->genfifobus;
962 if (op->cmd.opcode) {
963 reinit_completion(&xqspi->data_completion);
964 xqspi->txbuf = &opcode;
966 xqspi->bytes_to_transfer = op->cmd.nbytes;
967 xqspi->bytes_to_receive = 0;
968 zynqmp_qspi_write_op(xqspi, op->cmd.buswidth, genfifoentry);
969 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
970 zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
971 GQSPI_CFG_START_GEN_FIFO_MASK);
972 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
973 GQSPI_IER_GENFIFOEMPTY_MASK |
974 GQSPI_IER_TXNOT_FULL_MASK);
975 if (!wait_for_completion_timeout
976 (&xqspi->data_completion, msecs_to_jiffies(1000))) {
982 if (op->addr.nbytes) {
983 xqspi->txbuf = &opaddr;
984 for (i = 0; i < op->addr.nbytes; i++) {
985 *(((u8 *)xqspi->txbuf) + i) = op->addr.val >>
986 (8 * (op->addr.nbytes - i - 1));
989 reinit_completion(&xqspi->data_completion);
991 xqspi->bytes_to_transfer = op->addr.nbytes;
992 xqspi->bytes_to_receive = 0;
993 zynqmp_qspi_write_op(xqspi, op->addr.buswidth, genfifoentry);
994 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
995 zynqmp_gqspi_read(xqspi,
997 GQSPI_CFG_START_GEN_FIFO_MASK);
998 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
999 GQSPI_IER_TXEMPTY_MASK |
1000 GQSPI_IER_GENFIFOEMPTY_MASK |
1001 GQSPI_IER_TXNOT_FULL_MASK);
1002 if (!wait_for_completion_timeout
1003 (&xqspi->data_completion, msecs_to_jiffies(1000))) {
1009 if (op->dummy.nbytes) {
1010 xqspi->txbuf = NULL;
1011 xqspi->rxbuf = NULL;
1013 * xqspi->bytes_to_transfer here represents the dummy circles
1014 * which need to be sent.
1016 xqspi->bytes_to_transfer = op->dummy.nbytes * 8 / op->dummy.buswidth;
1017 xqspi->bytes_to_receive = 0;
1019 * Using op->data.buswidth instead of op->dummy.buswidth here because
1020 * we need to use it to configure the correct SPI mode.
1022 zynqmp_qspi_write_op(xqspi, op->data.buswidth,
1024 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
1025 zynqmp_gqspi_read(xqspi, GQSPI_CONFIG_OFST) |
1026 GQSPI_CFG_START_GEN_FIFO_MASK);
1029 if (op->data.nbytes) {
1030 reinit_completion(&xqspi->data_completion);
1031 if (op->data.dir == SPI_MEM_DATA_OUT) {
1032 xqspi->txbuf = (u8 *)op->data.buf.out;
1033 xqspi->rxbuf = NULL;
1034 xqspi->bytes_to_transfer = op->data.nbytes;
1035 xqspi->bytes_to_receive = 0;
1036 zynqmp_qspi_write_op(xqspi, op->data.buswidth,
1038 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
1040 (xqspi, GQSPI_CONFIG_OFST) |
1041 GQSPI_CFG_START_GEN_FIFO_MASK);
1042 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
1043 GQSPI_IER_TXEMPTY_MASK |
1044 GQSPI_IER_GENFIFOEMPTY_MASK |
1045 GQSPI_IER_TXNOT_FULL_MASK);
1047 xqspi->txbuf = NULL;
1048 xqspi->rxbuf = (u8 *)op->data.buf.in;
1049 xqspi->bytes_to_receive = op->data.nbytes;
1050 xqspi->bytes_to_transfer = 0;
1051 err = zynqmp_qspi_read_op(xqspi, op->data.buswidth,
1056 zynqmp_gqspi_write(xqspi, GQSPI_CONFIG_OFST,
1058 (xqspi, GQSPI_CONFIG_OFST) |
1059 GQSPI_CFG_START_GEN_FIFO_MASK);
1060 if (xqspi->mode == GQSPI_MODE_DMA) {
1062 (xqspi, GQSPI_QSPIDMA_DST_I_EN_OFST,
1063 GQSPI_QSPIDMA_DST_I_EN_DONE_MASK);
1065 zynqmp_gqspi_write(xqspi, GQSPI_IER_OFST,
1066 GQSPI_IER_GENFIFOEMPTY_MASK |
1067 GQSPI_IER_RXNEMPTY_MASK |
1068 GQSPI_IER_RXEMPTY_MASK);
1071 if (!wait_for_completion_timeout
1072 (&xqspi->data_completion, msecs_to_jiffies(1000)))
1078 zynqmp_qspi_chipselect(mem->spi, true);
1079 mutex_unlock(&xqspi->op_lock);
1084 static const struct dev_pm_ops zynqmp_qspi_dev_pm_ops = {
1085 SET_RUNTIME_PM_OPS(zynqmp_runtime_suspend,
1086 zynqmp_runtime_resume, NULL)
1087 SET_SYSTEM_SLEEP_PM_OPS(zynqmp_qspi_suspend, zynqmp_qspi_resume)
1090 static const struct spi_controller_mem_ops zynqmp_qspi_mem_ops = {
1091 .exec_op = zynqmp_qspi_exec_op,
1095 * zynqmp_qspi_probe - Probe method for the QSPI driver
1096 * @pdev: Pointer to the platform_device structure
1098 * This function initializes the driver data structures and the hardware.
1100 * Return: 0 on success; error value otherwise
1102 static int zynqmp_qspi_probe(struct platform_device *pdev)
1105 struct spi_controller *ctlr;
1106 struct zynqmp_qspi *xqspi;
1107 struct device *dev = &pdev->dev;
1108 struct device_node *np = dev->of_node;
1111 ctlr = spi_alloc_master(&pdev->dev, sizeof(*xqspi));
1115 xqspi = spi_controller_get_devdata(ctlr);
1118 platform_set_drvdata(pdev, xqspi);
1120 xqspi->regs = devm_platform_ioremap_resource(pdev, 0);
1121 if (IS_ERR(xqspi->regs)) {
1122 ret = PTR_ERR(xqspi->regs);
1126 xqspi->pclk = devm_clk_get(&pdev->dev, "pclk");
1127 if (IS_ERR(xqspi->pclk)) {
1128 dev_err(dev, "pclk clock not found.\n");
1129 ret = PTR_ERR(xqspi->pclk);
1133 xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk");
1134 if (IS_ERR(xqspi->refclk)) {
1135 dev_err(dev, "ref_clk clock not found.\n");
1136 ret = PTR_ERR(xqspi->refclk);
1140 ret = clk_prepare_enable(xqspi->pclk);
1142 dev_err(dev, "Unable to enable APB clock.\n");
1146 ret = clk_prepare_enable(xqspi->refclk);
1148 dev_err(dev, "Unable to enable device clock.\n");
1152 init_completion(&xqspi->data_completion);
1154 mutex_init(&xqspi->op_lock);
1156 pm_runtime_use_autosuspend(&pdev->dev);
1157 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1158 pm_runtime_set_active(&pdev->dev);
1159 pm_runtime_enable(&pdev->dev);
1161 ret = pm_runtime_get_sync(&pdev->dev);
1163 dev_err(&pdev->dev, "Failed to pm_runtime_get_sync: %d\n", ret);
1167 /* QSPI controller initializations */
1168 zynqmp_qspi_init_hw(xqspi);
1170 xqspi->irq = platform_get_irq(pdev, 0);
1171 if (xqspi->irq <= 0) {
1175 ret = devm_request_irq(&pdev->dev, xqspi->irq, zynqmp_qspi_irq,
1176 0, pdev->name, xqspi);
1179 dev_err(dev, "request_irq failed\n");
1183 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
1187 ret = of_property_read_u32(np, "num-cs", &num_cs);
1189 ctlr->num_chipselect = GQSPI_DEFAULT_NUM_CS;
1190 } else if (num_cs > GQSPI_MAX_NUM_CS) {
1192 dev_err(&pdev->dev, "only %d chip selects are available\n",
1196 ctlr->num_chipselect = num_cs;
1199 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
1200 ctlr->mem_ops = &zynqmp_qspi_mem_ops;
1201 ctlr->setup = zynqmp_qspi_setup_op;
1202 ctlr->max_speed_hz = clk_get_rate(xqspi->refclk) / 2;
1203 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
1204 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD |
1205 SPI_TX_DUAL | SPI_TX_QUAD;
1206 ctlr->dev.of_node = np;
1207 ctlr->auto_runtime_pm = true;
1209 ret = devm_spi_register_controller(&pdev->dev, ctlr);
1211 dev_err(&pdev->dev, "spi_register_controller failed\n");
1215 pm_runtime_mark_last_busy(&pdev->dev);
1216 pm_runtime_put_autosuspend(&pdev->dev);
1221 pm_runtime_put_sync(&pdev->dev);
1222 pm_runtime_set_suspended(&pdev->dev);
1223 pm_runtime_disable(&pdev->dev);
1224 clk_disable_unprepare(xqspi->refclk);
1226 clk_disable_unprepare(xqspi->pclk);
1228 spi_controller_put(ctlr);
1234 * zynqmp_qspi_remove - Remove method for the QSPI driver
1235 * @pdev: Pointer to the platform_device structure
1237 * This function is called if a device is physically removed from the system or
1238 * if the driver module is being unloaded. It frees all resources allocated to
1243 static int zynqmp_qspi_remove(struct platform_device *pdev)
1245 struct zynqmp_qspi *xqspi = platform_get_drvdata(pdev);
1247 zynqmp_gqspi_write(xqspi, GQSPI_EN_OFST, 0x0);
1248 clk_disable_unprepare(xqspi->refclk);
1249 clk_disable_unprepare(xqspi->pclk);
1250 pm_runtime_set_suspended(&pdev->dev);
1251 pm_runtime_disable(&pdev->dev);
1256 static const struct of_device_id zynqmp_qspi_of_match[] = {
1257 { .compatible = "xlnx,zynqmp-qspi-1.0", },
1258 { /* End of table */ }
1261 MODULE_DEVICE_TABLE(of, zynqmp_qspi_of_match);
1263 static struct platform_driver zynqmp_qspi_driver = {
1264 .probe = zynqmp_qspi_probe,
1265 .remove = zynqmp_qspi_remove,
1267 .name = "zynqmp-qspi",
1268 .of_match_table = zynqmp_qspi_of_match,
1269 .pm = &zynqmp_qspi_dev_pm_ops,
1273 module_platform_driver(zynqmp_qspi_driver);
1275 MODULE_AUTHOR("Xilinx, Inc.");
1276 MODULE_DESCRIPTION("Xilinx Zynqmp QSPI driver");
1277 MODULE_LICENSE("GPL");