1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Xilinx, Inc.
5 * Author: Naga Sureshkumar Relli <nagasure@xilinx.com>
9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
17 #include <linux/workqueue.h>
18 #include <linux/spi/spi-mem.h>
20 /* Register offset definitions */
21 #define ZYNQ_QSPI_CONFIG_OFFSET 0x00 /* Configuration Register, RW */
22 #define ZYNQ_QSPI_STATUS_OFFSET 0x04 /* Interrupt Status Register, RO */
23 #define ZYNQ_QSPI_IEN_OFFSET 0x08 /* Interrupt Enable Register, WO */
24 #define ZYNQ_QSPI_IDIS_OFFSET 0x0C /* Interrupt Disable Reg, WO */
25 #define ZYNQ_QSPI_IMASK_OFFSET 0x10 /* Interrupt Enabled Mask Reg,RO */
26 #define ZYNQ_QSPI_ENABLE_OFFSET 0x14 /* Enable/Disable Register, RW */
27 #define ZYNQ_QSPI_DELAY_OFFSET 0x18 /* Delay Register, RW */
28 #define ZYNQ_QSPI_TXD_00_00_OFFSET 0x1C /* Transmit 4-byte inst, WO */
29 #define ZYNQ_QSPI_TXD_00_01_OFFSET 0x80 /* Transmit 1-byte inst, WO */
30 #define ZYNQ_QSPI_TXD_00_10_OFFSET 0x84 /* Transmit 2-byte inst, WO */
31 #define ZYNQ_QSPI_TXD_00_11_OFFSET 0x88 /* Transmit 3-byte inst, WO */
32 #define ZYNQ_QSPI_RXD_OFFSET 0x20 /* Data Receive Register, RO */
33 #define ZYNQ_QSPI_SIC_OFFSET 0x24 /* Slave Idle Count Register, RW */
34 #define ZYNQ_QSPI_TX_THRESH_OFFSET 0x28 /* TX FIFO Watermark Reg, RW */
35 #define ZYNQ_QSPI_RX_THRESH_OFFSET 0x2C /* RX FIFO Watermark Reg, RW */
36 #define ZYNQ_QSPI_GPIO_OFFSET 0x30 /* GPIO Register, RW */
37 #define ZYNQ_QSPI_LINEAR_CFG_OFFSET 0xA0 /* Linear Adapter Config Ref, RW */
38 #define ZYNQ_QSPI_MOD_ID_OFFSET 0xFC /* Module ID Register, RO */
41 * QSPI Configuration Register bit Masks
43 * This register contains various control bits that effect the operation
44 * of the QSPI controller
46 #define ZYNQ_QSPI_CONFIG_IFMODE_MASK BIT(31) /* Flash Memory Interface */
47 #define ZYNQ_QSPI_CONFIG_MANSRT_MASK BIT(16) /* Manual TX Start */
48 #define ZYNQ_QSPI_CONFIG_MANSRTEN_MASK BIT(15) /* Enable Manual TX Mode */
49 #define ZYNQ_QSPI_CONFIG_SSFORCE_MASK BIT(14) /* Manual Chip Select */
50 #define ZYNQ_QSPI_CONFIG_BDRATE_MASK GENMASK(5, 3) /* Baud Rate Mask */
51 #define ZYNQ_QSPI_CONFIG_CPHA_MASK BIT(2) /* Clock Phase Control */
52 #define ZYNQ_QSPI_CONFIG_CPOL_MASK BIT(1) /* Clock Polarity Control */
53 #define ZYNQ_QSPI_CONFIG_FWIDTH_MASK GENMASK(7, 6) /* FIFO width */
54 #define ZYNQ_QSPI_CONFIG_MSTREN_MASK BIT(0) /* Master Mode */
57 * QSPI Configuration Register - Baud rate and slave select
59 * These are the values used in the calculation of baud rate divisor and
60 * setting the slave select.
62 #define ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX GENMASK(2, 0) /* Baud rate maximum */
63 #define ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift */
64 #define ZYNQ_QSPI_CONFIG_PCS BIT(10) /* Peripheral Chip Select */
67 * QSPI Interrupt Registers bit Masks
69 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
72 #define ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK BIT(0) /* QSPI RX FIFO Overflow */
73 #define ZYNQ_QSPI_IXR_TXNFULL_MASK BIT(2) /* QSPI TX FIFO Overflow */
74 #define ZYNQ_QSPI_IXR_TXFULL_MASK BIT(3) /* QSPI TX FIFO is full */
75 #define ZYNQ_QSPI_IXR_RXNEMTY_MASK BIT(4) /* QSPI RX FIFO Not Empty */
76 #define ZYNQ_QSPI_IXR_RXF_FULL_MASK BIT(5) /* QSPI RX FIFO is full */
77 #define ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK BIT(6) /* QSPI TX FIFO Underflow */
78 #define ZYNQ_QSPI_IXR_ALL_MASK (ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK | \
79 ZYNQ_QSPI_IXR_TXNFULL_MASK | \
80 ZYNQ_QSPI_IXR_TXFULL_MASK | \
81 ZYNQ_QSPI_IXR_RXNEMTY_MASK | \
82 ZYNQ_QSPI_IXR_RXF_FULL_MASK | \
83 ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK)
84 #define ZYNQ_QSPI_IXR_RXTX_MASK (ZYNQ_QSPI_IXR_TXNFULL_MASK | \
85 ZYNQ_QSPI_IXR_RXNEMTY_MASK)
88 * QSPI Enable Register bit Masks
90 * This register is used to enable or disable the QSPI controller
92 #define ZYNQ_QSPI_ENABLE_ENABLE_MASK BIT(0) /* QSPI Enable Bit Mask */
95 * QSPI Linear Configuration Register
97 * It is named Linear Configuration but it controls other modes when not in
100 #define ZYNQ_QSPI_LCFG_TWO_MEM BIT(30) /* LQSPI Two memories */
101 #define ZYNQ_QSPI_LCFG_SEP_BUS BIT(29) /* LQSPI Separate bus */
102 #define ZYNQ_QSPI_LCFG_U_PAGE BIT(28) /* LQSPI Upper Page */
104 #define ZYNQ_QSPI_LCFG_DUMMY_SHIFT 8
106 #define ZYNQ_QSPI_FAST_READ_QOUT_CODE 0x6B /* read instruction code */
107 #define ZYNQ_QSPI_FIFO_DEPTH 63 /* FIFO depth in words */
108 #define ZYNQ_QSPI_RX_THRESHOLD 32 /* Rx FIFO threshold level */
109 #define ZYNQ_QSPI_TX_THRESHOLD 1 /* Tx FIFO threshold level */
112 * The modebits configurable by the driver to make the SPI support different
115 #define ZYNQ_QSPI_MODEBITS (SPI_CPOL | SPI_CPHA)
117 /* Maximum number of chip selects */
118 #define ZYNQ_QSPI_MAX_NUM_CS 2
121 * struct zynq_qspi - Defines qspi driver instance
122 * @regs: Virtual address of the QSPI controller registers
123 * @refclk: Pointer to the peripheral clock
124 * @pclk: Pointer to the APB clock
126 * @txbuf: Pointer to the TX buffer
127 * @rxbuf: Pointer to the RX buffer
128 * @tx_bytes: Number of bytes left to transfer
129 * @rx_bytes: Number of bytes left to receive
130 * @data_completion: completion structure
142 struct completion data_completion
;
146 * Inline functions for the QSPI controller read/write
148 static inline u32
zynq_qspi_read(struct zynq_qspi
*xqspi
, u32 offset
)
150 return readl_relaxed(xqspi
->regs
+ offset
);
153 static inline void zynq_qspi_write(struct zynq_qspi
*xqspi
, u32 offset
,
156 writel_relaxed(val
, xqspi
->regs
+ offset
);
160 * zynq_qspi_init_hw - Initialize the hardware
161 * @xqspi: Pointer to the zynq_qspi structure
162 * @num_cs: Number of connected CS (to enable dual memories if needed)
164 * The default settings of the QSPI controller's configurable parameters on
167 * - Baud rate divisor is set to 2
168 * - Tx threshold set to 1l Rx threshold set to 32
169 * - Flash memory interface mode enabled
170 * - Size of the word to be transferred as 8 bit
171 * This function performs the following actions
172 * - Disable and clear all the interrupts
173 * - Enable manual slave select
174 * - Enable manual start
175 * - Deselect all the chip select lines
176 * - Set the size of the word to be transferred as 32 bit
177 * - Set the little endian mode of TX FIFO and
178 * - Enable the QSPI controller
180 static void zynq_qspi_init_hw(struct zynq_qspi
*xqspi
, unsigned int num_cs
)
184 zynq_qspi_write(xqspi
, ZYNQ_QSPI_ENABLE_OFFSET
, 0);
185 zynq_qspi_write(xqspi
, ZYNQ_QSPI_IDIS_OFFSET
, ZYNQ_QSPI_IXR_ALL_MASK
);
187 /* Disable linear mode as the boot loader may have used it */
189 /* At the same time, enable dual mode if more than 1 CS is available */
191 config_reg
|= ZYNQ_QSPI_LCFG_TWO_MEM
;
193 zynq_qspi_write(xqspi
, ZYNQ_QSPI_LINEAR_CFG_OFFSET
, config_reg
);
195 /* Clear the RX FIFO */
196 while (zynq_qspi_read(xqspi
, ZYNQ_QSPI_STATUS_OFFSET
) &
197 ZYNQ_QSPI_IXR_RXNEMTY_MASK
)
198 zynq_qspi_read(xqspi
, ZYNQ_QSPI_RXD_OFFSET
);
200 zynq_qspi_write(xqspi
, ZYNQ_QSPI_STATUS_OFFSET
, ZYNQ_QSPI_IXR_ALL_MASK
);
201 config_reg
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
);
202 config_reg
&= ~(ZYNQ_QSPI_CONFIG_MSTREN_MASK
|
203 ZYNQ_QSPI_CONFIG_CPOL_MASK
|
204 ZYNQ_QSPI_CONFIG_CPHA_MASK
|
205 ZYNQ_QSPI_CONFIG_BDRATE_MASK
|
206 ZYNQ_QSPI_CONFIG_SSFORCE_MASK
|
207 ZYNQ_QSPI_CONFIG_MANSRTEN_MASK
|
208 ZYNQ_QSPI_CONFIG_MANSRT_MASK
);
209 config_reg
|= (ZYNQ_QSPI_CONFIG_MSTREN_MASK
|
210 ZYNQ_QSPI_CONFIG_SSFORCE_MASK
|
211 ZYNQ_QSPI_CONFIG_FWIDTH_MASK
|
212 ZYNQ_QSPI_CONFIG_IFMODE_MASK
);
213 zynq_qspi_write(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
, config_reg
);
215 zynq_qspi_write(xqspi
, ZYNQ_QSPI_RX_THRESH_OFFSET
,
216 ZYNQ_QSPI_RX_THRESHOLD
);
217 zynq_qspi_write(xqspi
, ZYNQ_QSPI_TX_THRESH_OFFSET
,
218 ZYNQ_QSPI_TX_THRESHOLD
);
220 zynq_qspi_write(xqspi
, ZYNQ_QSPI_ENABLE_OFFSET
,
221 ZYNQ_QSPI_ENABLE_ENABLE_MASK
);
224 static bool zynq_qspi_supports_op(struct spi_mem
*mem
,
225 const struct spi_mem_op
*op
)
227 if (!spi_mem_default_supports_op(mem
, op
))
231 * The number of address bytes should be equal to or less than 3 bytes.
233 if (op
->addr
.nbytes
> 3)
240 * zynq_qspi_rxfifo_op - Read 1..4 bytes from RxFIFO to RX buffer
241 * @xqspi: Pointer to the zynq_qspi structure
242 * @size: Number of bytes to be read (1..4)
244 static void zynq_qspi_rxfifo_op(struct zynq_qspi
*xqspi
, unsigned int size
)
248 data
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_RXD_OFFSET
);
251 memcpy(xqspi
->rxbuf
, ((u8
*)&data
) + 4 - size
, size
);
252 xqspi
->rxbuf
+= size
;
255 xqspi
->rx_bytes
-= size
;
256 if (xqspi
->rx_bytes
< 0)
261 * zynq_qspi_txfifo_op - Write 1..4 bytes from TX buffer to TxFIFO
262 * @xqspi: Pointer to the zynq_qspi structure
263 * @size: Number of bytes to be written (1..4)
265 static void zynq_qspi_txfifo_op(struct zynq_qspi
*xqspi
, unsigned int size
)
267 static const unsigned int offset
[4] = {
268 ZYNQ_QSPI_TXD_00_01_OFFSET
, ZYNQ_QSPI_TXD_00_10_OFFSET
,
269 ZYNQ_QSPI_TXD_00_11_OFFSET
, ZYNQ_QSPI_TXD_00_00_OFFSET
};
274 memcpy(&data
, xqspi
->txbuf
, size
);
275 xqspi
->txbuf
+= size
;
280 xqspi
->tx_bytes
-= size
;
281 zynq_qspi_write(xqspi
, offset
[size
- 1], data
);
285 * zynq_qspi_chipselect - Select or deselect the chip select line
286 * @spi: Pointer to the spi_device structure
287 * @assert: 1 for select or 0 for deselect the chip select line
289 static void zynq_qspi_chipselect(struct spi_device
*spi
, bool assert)
291 struct spi_controller
*ctlr
= spi
->master
;
292 struct zynq_qspi
*xqspi
= spi_controller_get_devdata(ctlr
);
295 /* Select the lower (CS0) or upper (CS1) memory */
296 if (ctlr
->num_chipselect
> 1) {
297 config_reg
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_LINEAR_CFG_OFFSET
);
298 if (!spi
->chip_select
)
299 config_reg
&= ~ZYNQ_QSPI_LCFG_U_PAGE
;
301 config_reg
|= ZYNQ_QSPI_LCFG_U_PAGE
;
303 zynq_qspi_write(xqspi
, ZYNQ_QSPI_LINEAR_CFG_OFFSET
, config_reg
);
306 /* Ground the line to assert the CS */
307 config_reg
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
);
309 config_reg
&= ~ZYNQ_QSPI_CONFIG_PCS
;
311 config_reg
|= ZYNQ_QSPI_CONFIG_PCS
;
313 zynq_qspi_write(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
, config_reg
);
317 * zynq_qspi_config_op - Configure QSPI controller for specified transfer
318 * @xqspi: Pointer to the zynq_qspi structure
319 * @qspi: Pointer to the spi_device structure
321 * Sets the operational mode of QSPI controller for the next QSPI transfer and
322 * sets the requested clock frequency.
324 * Return: 0 on success and -EINVAL on invalid input parameter
326 * Note: If the requested frequency is not an exact match with what can be
327 * obtained using the prescalar value, the driver sets the clock frequency which
328 * is lower than the requested frequency (maximum lower) for the transfer. If
329 * the requested frequency is higher or lower than that is supported by the QSPI
330 * controller the driver will set the highest or lowest frequency supported by
333 static int zynq_qspi_config_op(struct zynq_qspi
*xqspi
, struct spi_device
*spi
)
335 u32 config_reg
, baud_rate_val
= 0;
338 * Set the clock frequency
339 * The baud rate divisor is not a direct mapping to the value written
340 * into the configuration register (config_reg[5:3])
341 * i.e. 000 - divide by 2
344 * 111 - divide by 256
346 while ((baud_rate_val
< ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX
) &&
347 (clk_get_rate(xqspi
->refclk
) / (2 << baud_rate_val
)) >
351 config_reg
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
);
353 /* Set the QSPI clock phase and clock polarity */
354 config_reg
&= (~ZYNQ_QSPI_CONFIG_CPHA_MASK
) &
355 (~ZYNQ_QSPI_CONFIG_CPOL_MASK
);
356 if (spi
->mode
& SPI_CPHA
)
357 config_reg
|= ZYNQ_QSPI_CONFIG_CPHA_MASK
;
358 if (spi
->mode
& SPI_CPOL
)
359 config_reg
|= ZYNQ_QSPI_CONFIG_CPOL_MASK
;
361 config_reg
&= ~ZYNQ_QSPI_CONFIG_BDRATE_MASK
;
362 config_reg
|= (baud_rate_val
<< ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT
);
363 zynq_qspi_write(xqspi
, ZYNQ_QSPI_CONFIG_OFFSET
, config_reg
);
369 * zynq_qspi_setup - Configure the QSPI controller
370 * @spi: Pointer to the spi_device structure
372 * Sets the operational mode of QSPI controller for the next QSPI transfer, baud
373 * rate and divisor value to setup the requested qspi clock.
375 * Return: 0 on success and error value on failure
377 static int zynq_qspi_setup_op(struct spi_device
*spi
)
379 struct spi_controller
*ctlr
= spi
->master
;
380 struct zynq_qspi
*qspi
= spi_controller_get_devdata(ctlr
);
385 clk_enable(qspi
->refclk
);
386 clk_enable(qspi
->pclk
);
387 zynq_qspi_write(qspi
, ZYNQ_QSPI_ENABLE_OFFSET
,
388 ZYNQ_QSPI_ENABLE_ENABLE_MASK
);
394 * zynq_qspi_write_op - Fills the TX FIFO with as many bytes as possible
395 * @xqspi: Pointer to the zynq_qspi structure
396 * @txcount: Maximum number of words to write
397 * @txempty: Indicates that TxFIFO is empty
399 static void zynq_qspi_write_op(struct zynq_qspi
*xqspi
, int txcount
,
404 len
= xqspi
->tx_bytes
;
405 if (len
&& len
< 4) {
407 * We must empty the TxFIFO between accesses to TXD0,
411 zynq_qspi_txfifo_op(xqspi
, len
);
421 iowrite32_rep(xqspi
->regs
+ ZYNQ_QSPI_TXD_00_00_OFFSET
,
422 xqspi
->txbuf
, count
);
423 xqspi
->txbuf
+= count
* 4;
425 for (k
= 0; k
< count
; k
++)
426 writel_relaxed(0, xqspi
->regs
+
427 ZYNQ_QSPI_TXD_00_00_OFFSET
);
430 xqspi
->tx_bytes
-= count
* 4;
434 * zynq_qspi_read_op - Drains the RX FIFO by as many bytes as possible
435 * @xqspi: Pointer to the zynq_qspi structure
436 * @rxcount: Maximum number of words to read
438 static void zynq_qspi_read_op(struct zynq_qspi
*xqspi
, int rxcount
)
442 len
= xqspi
->rx_bytes
- xqspi
->tx_bytes
;
447 ioread32_rep(xqspi
->regs
+ ZYNQ_QSPI_RXD_OFFSET
,
448 xqspi
->rxbuf
, count
);
449 xqspi
->rxbuf
+= count
* 4;
451 for (k
= 0; k
< count
; k
++)
452 readl_relaxed(xqspi
->regs
+ ZYNQ_QSPI_RXD_OFFSET
);
454 xqspi
->rx_bytes
-= count
* 4;
457 if (len
&& len
< 4 && count
< rxcount
)
458 zynq_qspi_rxfifo_op(xqspi
, len
);
462 * zynq_qspi_irq - Interrupt service routine of the QSPI controller
464 * @dev_id: Pointer to the xqspi structure
466 * This function handles TX empty only.
467 * On TX empty interrupt this function reads the received data from RX FIFO and
468 * fills the TX FIFO if there is any data remaining to be transferred.
470 * Return: IRQ_HANDLED when interrupt is handled; IRQ_NONE otherwise.
472 static irqreturn_t
zynq_qspi_irq(int irq
, void *dev_id
)
476 struct zynq_qspi
*xqspi
= (struct zynq_qspi
*)dev_id
;
478 intr_status
= zynq_qspi_read(xqspi
, ZYNQ_QSPI_STATUS_OFFSET
);
479 zynq_qspi_write(xqspi
, ZYNQ_QSPI_STATUS_OFFSET
, intr_status
);
481 if ((intr_status
& ZYNQ_QSPI_IXR_TXNFULL_MASK
) ||
482 (intr_status
& ZYNQ_QSPI_IXR_RXNEMTY_MASK
)) {
484 * This bit is set when Tx FIFO has < THRESHOLD entries.
485 * We have the THRESHOLD value set to 1,
486 * so this bit indicates Tx FIFO is empty.
488 txempty
= !!(intr_status
& ZYNQ_QSPI_IXR_TXNFULL_MASK
);
489 /* Read out the data from the RX FIFO */
490 zynq_qspi_read_op(xqspi
, ZYNQ_QSPI_RX_THRESHOLD
);
491 if (xqspi
->tx_bytes
) {
492 /* There is more data to send */
493 zynq_qspi_write_op(xqspi
, ZYNQ_QSPI_RX_THRESHOLD
,
497 * If transfer and receive is completed then only send
500 if (!xqspi
->rx_bytes
) {
501 zynq_qspi_write(xqspi
,
502 ZYNQ_QSPI_IDIS_OFFSET
,
503 ZYNQ_QSPI_IXR_RXTX_MASK
);
504 complete(&xqspi
->data_completion
);
514 * zynq_qspi_exec_mem_op() - Initiates the QSPI transfer
515 * @mem: the SPI memory
516 * @op: the memory operation to execute
518 * Executes a memory operation.
520 * This function first selects the chip and starts the memory operation.
522 * Return: 0 in case of success, a negative error code otherwise.
524 static int zynq_qspi_exec_mem_op(struct spi_mem
*mem
,
525 const struct spi_mem_op
*op
)
527 struct zynq_qspi
*xqspi
= spi_controller_get_devdata(mem
->spi
->master
);
531 dev_dbg(xqspi
->dev
, "cmd:%#x mode:%d.%d.%d.%d\n",
532 op
->cmd
.opcode
, op
->cmd
.buswidth
, op
->addr
.buswidth
,
533 op
->dummy
.buswidth
, op
->data
.buswidth
);
535 zynq_qspi_chipselect(mem
->spi
, true);
536 zynq_qspi_config_op(xqspi
, mem
->spi
);
538 if (op
->cmd
.opcode
) {
539 reinit_completion(&xqspi
->data_completion
);
540 xqspi
->txbuf
= (u8
*)&op
->cmd
.opcode
;
542 xqspi
->tx_bytes
= sizeof(op
->cmd
.opcode
);
543 xqspi
->rx_bytes
= sizeof(op
->cmd
.opcode
);
544 zynq_qspi_write_op(xqspi
, ZYNQ_QSPI_FIFO_DEPTH
, true);
545 zynq_qspi_write(xqspi
, ZYNQ_QSPI_IEN_OFFSET
,
546 ZYNQ_QSPI_IXR_RXTX_MASK
);
547 if (!wait_for_completion_interruptible_timeout(&xqspi
->data_completion
,
548 msecs_to_jiffies(1000)))
552 if (op
->addr
.nbytes
) {
553 for (i
= 0; i
< op
->addr
.nbytes
; i
++) {
554 xqspi
->txbuf
[i
] = op
->addr
.val
>>
555 (8 * (op
->addr
.nbytes
- i
- 1));
558 reinit_completion(&xqspi
->data_completion
);
560 xqspi
->tx_bytes
= op
->addr
.nbytes
;
561 xqspi
->rx_bytes
= op
->addr
.nbytes
;
562 zynq_qspi_write_op(xqspi
, ZYNQ_QSPI_FIFO_DEPTH
, true);
563 zynq_qspi_write(xqspi
, ZYNQ_QSPI_IEN_OFFSET
,
564 ZYNQ_QSPI_IXR_RXTX_MASK
);
565 if (!wait_for_completion_interruptible_timeout(&xqspi
->data_completion
,
566 msecs_to_jiffies(1000)))
570 if (op
->dummy
.nbytes
) {
571 tmpbuf
= kzalloc(op
->dummy
.nbytes
, GFP_KERNEL
);
572 memset(tmpbuf
, 0xff, op
->dummy
.nbytes
);
573 reinit_completion(&xqspi
->data_completion
);
574 xqspi
->txbuf
= tmpbuf
;
576 xqspi
->tx_bytes
= op
->dummy
.nbytes
;
577 xqspi
->rx_bytes
= op
->dummy
.nbytes
;
578 zynq_qspi_write_op(xqspi
, ZYNQ_QSPI_FIFO_DEPTH
, true);
579 zynq_qspi_write(xqspi
, ZYNQ_QSPI_IEN_OFFSET
,
580 ZYNQ_QSPI_IXR_RXTX_MASK
);
581 if (!wait_for_completion_interruptible_timeout(&xqspi
->data_completion
,
582 msecs_to_jiffies(1000)))
588 if (op
->data
.nbytes
) {
589 reinit_completion(&xqspi
->data_completion
);
590 if (op
->data
.dir
== SPI_MEM_DATA_OUT
) {
591 xqspi
->txbuf
= (u8
*)op
->data
.buf
.out
;
592 xqspi
->tx_bytes
= op
->data
.nbytes
;
594 xqspi
->rx_bytes
= op
->data
.nbytes
;
597 xqspi
->rxbuf
= (u8
*)op
->data
.buf
.in
;
598 xqspi
->rx_bytes
= op
->data
.nbytes
;
599 xqspi
->tx_bytes
= op
->data
.nbytes
;
602 zynq_qspi_write_op(xqspi
, ZYNQ_QSPI_FIFO_DEPTH
, true);
603 zynq_qspi_write(xqspi
, ZYNQ_QSPI_IEN_OFFSET
,
604 ZYNQ_QSPI_IXR_RXTX_MASK
);
605 if (!wait_for_completion_interruptible_timeout(&xqspi
->data_completion
,
606 msecs_to_jiffies(1000)))
609 zynq_qspi_chipselect(mem
->spi
, false);
614 static const struct spi_controller_mem_ops zynq_qspi_mem_ops
= {
615 .supports_op
= zynq_qspi_supports_op
,
616 .exec_op
= zynq_qspi_exec_mem_op
,
620 * zynq_qspi_probe - Probe method for the QSPI driver
621 * @pdev: Pointer to the platform_device structure
623 * This function initializes the driver data structures and the hardware.
625 * Return: 0 on success and error value on failure
627 static int zynq_qspi_probe(struct platform_device
*pdev
)
630 struct spi_controller
*ctlr
;
631 struct device
*dev
= &pdev
->dev
;
632 struct device_node
*np
= dev
->of_node
;
633 struct zynq_qspi
*xqspi
;
636 ctlr
= spi_alloc_master(&pdev
->dev
, sizeof(*xqspi
));
640 xqspi
= spi_controller_get_devdata(ctlr
);
642 platform_set_drvdata(pdev
, xqspi
);
643 xqspi
->regs
= devm_platform_ioremap_resource(pdev
, 0);
644 if (IS_ERR(xqspi
->regs
)) {
645 ret
= PTR_ERR(xqspi
->regs
);
649 xqspi
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
650 if (IS_ERR(xqspi
->pclk
)) {
651 dev_err(&pdev
->dev
, "pclk clock not found.\n");
652 ret
= PTR_ERR(xqspi
->pclk
);
656 init_completion(&xqspi
->data_completion
);
658 xqspi
->refclk
= devm_clk_get(&pdev
->dev
, "ref_clk");
659 if (IS_ERR(xqspi
->refclk
)) {
660 dev_err(&pdev
->dev
, "ref_clk clock not found.\n");
661 ret
= PTR_ERR(xqspi
->refclk
);
665 ret
= clk_prepare_enable(xqspi
->pclk
);
667 dev_err(&pdev
->dev
, "Unable to enable APB clock.\n");
671 ret
= clk_prepare_enable(xqspi
->refclk
);
673 dev_err(&pdev
->dev
, "Unable to enable device clock.\n");
677 xqspi
->irq
= platform_get_irq(pdev
, 0);
678 if (xqspi
->irq
<= 0) {
682 ret
= devm_request_irq(&pdev
->dev
, xqspi
->irq
, zynq_qspi_irq
,
683 0, pdev
->name
, xqspi
);
686 dev_err(&pdev
->dev
, "request_irq failed\n");
690 ret
= of_property_read_u32(np
, "num-cs",
693 ctlr
->num_chipselect
= 1;
694 } else if (num_cs
> ZYNQ_QSPI_MAX_NUM_CS
) {
695 dev_err(&pdev
->dev
, "only 2 chip selects are available\n");
698 ctlr
->num_chipselect
= num_cs
;
701 ctlr
->mode_bits
= SPI_RX_DUAL
| SPI_RX_QUAD
|
702 SPI_TX_DUAL
| SPI_TX_QUAD
;
703 ctlr
->mem_ops
= &zynq_qspi_mem_ops
;
704 ctlr
->setup
= zynq_qspi_setup_op
;
705 ctlr
->max_speed_hz
= clk_get_rate(xqspi
->refclk
) / 2;
706 ctlr
->dev
.of_node
= np
;
708 /* QSPI controller initializations */
709 zynq_qspi_init_hw(xqspi
, ctlr
->num_chipselect
);
711 ret
= devm_spi_register_controller(&pdev
->dev
, ctlr
);
713 dev_err(&pdev
->dev
, "spi_register_master failed\n");
720 clk_disable_unprepare(xqspi
->refclk
);
722 clk_disable_unprepare(xqspi
->pclk
);
724 spi_controller_put(ctlr
);
730 * zynq_qspi_remove - Remove method for the QSPI driver
731 * @pdev: Pointer to the platform_device structure
733 * This function is called if a device is physically removed from the system or
734 * if the driver module is being unloaded. It frees all resources allocated to
737 * Return: 0 on success and error value on failure
739 static int zynq_qspi_remove(struct platform_device
*pdev
)
741 struct zynq_qspi
*xqspi
= platform_get_drvdata(pdev
);
743 zynq_qspi_write(xqspi
, ZYNQ_QSPI_ENABLE_OFFSET
, 0);
745 clk_disable_unprepare(xqspi
->refclk
);
746 clk_disable_unprepare(xqspi
->pclk
);
751 static const struct of_device_id zynq_qspi_of_match
[] = {
752 { .compatible
= "xlnx,zynq-qspi-1.0", },
753 { /* end of table */ }
756 MODULE_DEVICE_TABLE(of
, zynq_qspi_of_match
);
759 * zynq_qspi_driver - This structure defines the QSPI platform driver
761 static struct platform_driver zynq_qspi_driver
= {
762 .probe
= zynq_qspi_probe
,
763 .remove
= zynq_qspi_remove
,
766 .of_match_table
= zynq_qspi_of_match
,
770 module_platform_driver(zynq_qspi_driver
);
772 MODULE_AUTHOR("Xilinx, Inc.");
773 MODULE_DESCRIPTION("Xilinx Zynq QSPI driver");
774 MODULE_LICENSE("GPL");