]>
Commit | Line | Data |
---|---|---|
67dca5e5 NSR |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Copyright (C) 2019 Xilinx, Inc. | |
4 | * | |
5 | * Author: Naga Sureshkumar Relli <nagasure@xilinx.com> | |
6 | */ | |
7 | ||
8 | #include <linux/clk.h> | |
9 | #include <linux/delay.h> | |
67dca5e5 NSR |
10 | #include <linux/interrupt.h> |
11 | #include <linux/io.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> | |
19 | ||
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 */ | |
39 | ||
40 | /* | |
41 | * QSPI Configuration Register bit Masks | |
42 | * | |
43 | * This register contains various control bits that effect the operation | |
44 | * of the QSPI controller | |
45 | */ | |
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_SSCTRL_MASK BIT(10) /* Slave Select Mask */ | |
54 | #define ZYNQ_QSPI_CONFIG_FWIDTH_MASK GENMASK(7, 6) /* FIFO width */ | |
55 | #define ZYNQ_QSPI_CONFIG_MSTREN_MASK BIT(0) /* Master Mode */ | |
56 | ||
57 | /* | |
58 | * QSPI Configuration Register - Baud rate and slave select | |
59 | * | |
60 | * These are the values used in the calculation of baud rate divisor and | |
61 | * setting the slave select. | |
62 | */ | |
941be723 MR |
63 | #define ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX GENMASK(2, 0) /* Baud rate maximum */ |
64 | #define ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT 3 /* Baud rate divisor shift */ | |
65 | #define ZYNQ_QSPI_CONFIG_PCS 10 /* Peripheral Chip Select */ | |
67dca5e5 NSR |
66 | |
67 | /* | |
68 | * QSPI Interrupt Registers bit Masks | |
69 | * | |
70 | * All the four interrupt registers (Status/Mask/Enable/Disable) have the same | |
71 | * bit definitions. | |
72 | */ | |
73 | #define ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK BIT(0) /* QSPI RX FIFO Overflow */ | |
74 | #define ZYNQ_QSPI_IXR_TXNFULL_MASK BIT(2) /* QSPI TX FIFO Overflow */ | |
75 | #define ZYNQ_QSPI_IXR_TXFULL_MASK BIT(3) /* QSPI TX FIFO is full */ | |
76 | #define ZYNQ_QSPI_IXR_RXNEMTY_MASK BIT(4) /* QSPI RX FIFO Not Empty */ | |
77 | #define ZYNQ_QSPI_IXR_RXF_FULL_MASK BIT(5) /* QSPI RX FIFO is full */ | |
78 | #define ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK BIT(6) /* QSPI TX FIFO Underflow */ | |
79 | #define ZYNQ_QSPI_IXR_ALL_MASK (ZYNQ_QSPI_IXR_RX_OVERFLOW_MASK | \ | |
80 | ZYNQ_QSPI_IXR_TXNFULL_MASK | \ | |
81 | ZYNQ_QSPI_IXR_TXFULL_MASK | \ | |
82 | ZYNQ_QSPI_IXR_RXNEMTY_MASK | \ | |
83 | ZYNQ_QSPI_IXR_RXF_FULL_MASK | \ | |
84 | ZYNQ_QSPI_IXR_TXF_UNDRFLOW_MASK) | |
85 | #define ZYNQ_QSPI_IXR_RXTX_MASK (ZYNQ_QSPI_IXR_TXNFULL_MASK | \ | |
86 | ZYNQ_QSPI_IXR_RXNEMTY_MASK) | |
87 | ||
88 | /* | |
89 | * QSPI Enable Register bit Masks | |
90 | * | |
91 | * This register is used to enable or disable the QSPI controller | |
92 | */ | |
93 | #define ZYNQ_QSPI_ENABLE_ENABLE_MASK BIT(0) /* QSPI Enable Bit Mask */ | |
94 | ||
95 | /* | |
96 | * QSPI Linear Configuration Register | |
97 | * | |
98 | * It is named Linear Configuration but it controls other modes when not in | |
99 | * linear mode also. | |
100 | */ | |
044ac826 MR |
101 | #define ZYNQ_QSPI_LCFG_TWO_MEM BIT(30) /* LQSPI Two memories */ |
102 | #define ZYNQ_QSPI_LCFG_SEP_BUS BIT(29) /* LQSPI Separate bus */ | |
103 | #define ZYNQ_QSPI_LCFG_U_PAGE BIT(28) /* LQSPI Upper Page */ | |
67dca5e5 NSR |
104 | |
105 | #define ZYNQ_QSPI_LCFG_DUMMY_SHIFT 8 | |
106 | ||
107 | #define ZYNQ_QSPI_FAST_READ_QOUT_CODE 0x6B /* read instruction code */ | |
108 | #define ZYNQ_QSPI_FIFO_DEPTH 63 /* FIFO depth in words */ | |
109 | #define ZYNQ_QSPI_RX_THRESHOLD 32 /* Rx FIFO threshold level */ | |
110 | #define ZYNQ_QSPI_TX_THRESHOLD 1 /* Tx FIFO threshold level */ | |
111 | ||
112 | /* | |
113 | * The modebits configurable by the driver to make the SPI support different | |
114 | * data formats | |
115 | */ | |
116 | #define ZYNQ_QSPI_MODEBITS (SPI_CPOL | SPI_CPHA) | |
117 | ||
118 | /* Default number of chip selects */ | |
119 | #define ZYNQ_QSPI_DEFAULT_NUM_CS 1 | |
120 | ||
121 | /** | |
122 | * struct zynq_qspi - Defines qspi driver instance | |
123 | * @regs: Virtual address of the QSPI controller registers | |
124 | * @refclk: Pointer to the peripheral clock | |
125 | * @pclk: Pointer to the APB clock | |
126 | * @irq: IRQ number | |
127 | * @txbuf: Pointer to the TX buffer | |
128 | * @rxbuf: Pointer to the RX buffer | |
129 | * @tx_bytes: Number of bytes left to transfer | |
130 | * @rx_bytes: Number of bytes left to receive | |
131 | * @data_completion: completion structure | |
132 | */ | |
133 | struct zynq_qspi { | |
134 | struct device *dev; | |
135 | void __iomem *regs; | |
136 | struct clk *refclk; | |
137 | struct clk *pclk; | |
138 | int irq; | |
139 | u8 *txbuf; | |
140 | u8 *rxbuf; | |
141 | int tx_bytes; | |
142 | int rx_bytes; | |
143 | struct completion data_completion; | |
144 | }; | |
145 | ||
146 | /* | |
147 | * Inline functions for the QSPI controller read/write | |
148 | */ | |
149 | static inline u32 zynq_qspi_read(struct zynq_qspi *xqspi, u32 offset) | |
150 | { | |
151 | return readl_relaxed(xqspi->regs + offset); | |
152 | } | |
153 | ||
154 | static inline void zynq_qspi_write(struct zynq_qspi *xqspi, u32 offset, | |
155 | u32 val) | |
156 | { | |
157 | writel_relaxed(val, xqspi->regs + offset); | |
158 | } | |
159 | ||
160 | /** | |
161 | * zynq_qspi_init_hw - Initialize the hardware | |
162 | * @xqspi: Pointer to the zynq_qspi structure | |
163 | * | |
164 | * The default settings of the QSPI controller's configurable parameters on | |
165 | * reset are | |
166 | * - Master mode | |
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 | |
179 | */ | |
180 | static void zynq_qspi_init_hw(struct zynq_qspi *xqspi) | |
181 | { | |
182 | u32 config_reg; | |
183 | ||
184 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0); | |
185 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IDIS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK); | |
186 | ||
187 | /* Disable linear mode as the boot loader may have used it */ | |
188 | zynq_qspi_write(xqspi, ZYNQ_QSPI_LINEAR_CFG_OFFSET, 0); | |
189 | ||
190 | /* Clear the RX FIFO */ | |
191 | while (zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET) & | |
192 | ZYNQ_QSPI_IXR_RXNEMTY_MASK) | |
193 | zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET); | |
194 | ||
195 | zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, ZYNQ_QSPI_IXR_ALL_MASK); | |
196 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); | |
197 | config_reg &= ~(ZYNQ_QSPI_CONFIG_MSTREN_MASK | | |
198 | ZYNQ_QSPI_CONFIG_CPOL_MASK | | |
199 | ZYNQ_QSPI_CONFIG_CPHA_MASK | | |
200 | ZYNQ_QSPI_CONFIG_BDRATE_MASK | | |
201 | ZYNQ_QSPI_CONFIG_SSFORCE_MASK | | |
202 | ZYNQ_QSPI_CONFIG_MANSRTEN_MASK | | |
203 | ZYNQ_QSPI_CONFIG_MANSRT_MASK); | |
204 | config_reg |= (ZYNQ_QSPI_CONFIG_MSTREN_MASK | | |
205 | ZYNQ_QSPI_CONFIG_SSFORCE_MASK | | |
206 | ZYNQ_QSPI_CONFIG_FWIDTH_MASK | | |
207 | ZYNQ_QSPI_CONFIG_IFMODE_MASK); | |
208 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); | |
209 | ||
210 | zynq_qspi_write(xqspi, ZYNQ_QSPI_RX_THRESH_OFFSET, | |
211 | ZYNQ_QSPI_RX_THRESHOLD); | |
212 | zynq_qspi_write(xqspi, ZYNQ_QSPI_TX_THRESH_OFFSET, | |
213 | ZYNQ_QSPI_TX_THRESHOLD); | |
214 | ||
215 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, | |
216 | ZYNQ_QSPI_ENABLE_ENABLE_MASK); | |
217 | } | |
218 | ||
219 | static bool zynq_qspi_supports_op(struct spi_mem *mem, | |
220 | const struct spi_mem_op *op) | |
221 | { | |
222 | if (!spi_mem_default_supports_op(mem, op)) | |
223 | return false; | |
224 | ||
225 | /* | |
226 | * The number of address bytes should be equal to or less than 3 bytes. | |
227 | */ | |
228 | if (op->addr.nbytes > 3) | |
229 | return false; | |
230 | ||
231 | return true; | |
232 | } | |
233 | ||
234 | /** | |
235 | * zynq_qspi_rxfifo_op - Read 1..4 bytes from RxFIFO to RX buffer | |
236 | * @xqspi: Pointer to the zynq_qspi structure | |
237 | * @size: Number of bytes to be read (1..4) | |
238 | */ | |
239 | static void zynq_qspi_rxfifo_op(struct zynq_qspi *xqspi, unsigned int size) | |
240 | { | |
241 | u32 data; | |
242 | ||
243 | data = zynq_qspi_read(xqspi, ZYNQ_QSPI_RXD_OFFSET); | |
244 | ||
245 | if (xqspi->rxbuf) { | |
246 | memcpy(xqspi->rxbuf, ((u8 *)&data) + 4 - size, size); | |
247 | xqspi->rxbuf += size; | |
248 | } | |
249 | ||
250 | xqspi->rx_bytes -= size; | |
251 | if (xqspi->rx_bytes < 0) | |
252 | xqspi->rx_bytes = 0; | |
253 | } | |
254 | ||
255 | /** | |
256 | * zynq_qspi_txfifo_op - Write 1..4 bytes from TX buffer to TxFIFO | |
257 | * @xqspi: Pointer to the zynq_qspi structure | |
258 | * @size: Number of bytes to be written (1..4) | |
259 | */ | |
260 | static void zynq_qspi_txfifo_op(struct zynq_qspi *xqspi, unsigned int size) | |
261 | { | |
262 | static const unsigned int offset[4] = { | |
263 | ZYNQ_QSPI_TXD_00_01_OFFSET, ZYNQ_QSPI_TXD_00_10_OFFSET, | |
264 | ZYNQ_QSPI_TXD_00_11_OFFSET, ZYNQ_QSPI_TXD_00_00_OFFSET }; | |
265 | u32 data; | |
266 | ||
267 | if (xqspi->txbuf) { | |
268 | data = 0xffffffff; | |
269 | memcpy(&data, xqspi->txbuf, size); | |
270 | xqspi->txbuf += size; | |
271 | } else { | |
272 | data = 0; | |
273 | } | |
274 | ||
275 | xqspi->tx_bytes -= size; | |
276 | zynq_qspi_write(xqspi, offset[size - 1], data); | |
277 | } | |
278 | ||
279 | /** | |
280 | * zynq_qspi_chipselect - Select or deselect the chip select line | |
281 | * @spi: Pointer to the spi_device structure | |
282 | * @assert: 1 for select or 0 for deselect the chip select line | |
283 | */ | |
284 | static void zynq_qspi_chipselect(struct spi_device *spi, bool assert) | |
285 | { | |
9b10fa36 MR |
286 | struct spi_controller *ctlr = spi->master; |
287 | struct zynq_qspi *xqspi = spi_controller_get_devdata(ctlr); | |
67dca5e5 NSR |
288 | u32 config_reg; |
289 | ||
290 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); | |
291 | if (assert) { | |
292 | /* Select the slave */ | |
293 | config_reg &= ~ZYNQ_QSPI_CONFIG_SSCTRL_MASK; | |
294 | config_reg |= (((~(BIT(spi->chip_select))) << | |
941be723 | 295 | ZYNQ_QSPI_CONFIG_PCS) & |
67dca5e5 NSR |
296 | ZYNQ_QSPI_CONFIG_SSCTRL_MASK); |
297 | } else { | |
298 | config_reg |= ZYNQ_QSPI_CONFIG_SSCTRL_MASK; | |
299 | } | |
300 | ||
301 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); | |
302 | } | |
303 | ||
304 | /** | |
305 | * zynq_qspi_config_op - Configure QSPI controller for specified transfer | |
306 | * @xqspi: Pointer to the zynq_qspi structure | |
307 | * @qspi: Pointer to the spi_device structure | |
308 | * | |
309 | * Sets the operational mode of QSPI controller for the next QSPI transfer and | |
310 | * sets the requested clock frequency. | |
311 | * | |
312 | * Return: 0 on success and -EINVAL on invalid input parameter | |
313 | * | |
314 | * Note: If the requested frequency is not an exact match with what can be | |
315 | * obtained using the prescalar value, the driver sets the clock frequency which | |
316 | * is lower than the requested frequency (maximum lower) for the transfer. If | |
317 | * the requested frequency is higher or lower than that is supported by the QSPI | |
318 | * controller the driver will set the highest or lowest frequency supported by | |
319 | * controller. | |
320 | */ | |
321 | static int zynq_qspi_config_op(struct zynq_qspi *xqspi, struct spi_device *spi) | |
322 | { | |
323 | u32 config_reg, baud_rate_val = 0; | |
324 | ||
325 | /* | |
326 | * Set the clock frequency | |
327 | * The baud rate divisor is not a direct mapping to the value written | |
328 | * into the configuration register (config_reg[5:3]) | |
329 | * i.e. 000 - divide by 2 | |
330 | * 001 - divide by 4 | |
331 | * ---------------- | |
332 | * 111 - divide by 256 | |
333 | */ | |
941be723 | 334 | while ((baud_rate_val < ZYNQ_QSPI_CONFIG_BAUD_DIV_MAX) && |
67dca5e5 NSR |
335 | (clk_get_rate(xqspi->refclk) / (2 << baud_rate_val)) > |
336 | spi->max_speed_hz) | |
337 | baud_rate_val++; | |
338 | ||
339 | config_reg = zynq_qspi_read(xqspi, ZYNQ_QSPI_CONFIG_OFFSET); | |
340 | ||
341 | /* Set the QSPI clock phase and clock polarity */ | |
342 | config_reg &= (~ZYNQ_QSPI_CONFIG_CPHA_MASK) & | |
343 | (~ZYNQ_QSPI_CONFIG_CPOL_MASK); | |
344 | if (spi->mode & SPI_CPHA) | |
345 | config_reg |= ZYNQ_QSPI_CONFIG_CPHA_MASK; | |
346 | if (spi->mode & SPI_CPOL) | |
347 | config_reg |= ZYNQ_QSPI_CONFIG_CPOL_MASK; | |
348 | ||
349 | config_reg &= ~ZYNQ_QSPI_CONFIG_BDRATE_MASK; | |
941be723 | 350 | config_reg |= (baud_rate_val << ZYNQ_QSPI_CONFIG_BAUD_DIV_SHIFT); |
67dca5e5 NSR |
351 | zynq_qspi_write(xqspi, ZYNQ_QSPI_CONFIG_OFFSET, config_reg); |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | /** | |
357 | * zynq_qspi_setup - Configure the QSPI controller | |
358 | * @spi: Pointer to the spi_device structure | |
359 | * | |
360 | * Sets the operational mode of QSPI controller for the next QSPI transfer, baud | |
361 | * rate and divisor value to setup the requested qspi clock. | |
362 | * | |
363 | * Return: 0 on success and error value on failure | |
364 | */ | |
365 | static int zynq_qspi_setup_op(struct spi_device *spi) | |
366 | { | |
9b10fa36 MR |
367 | struct spi_controller *ctlr = spi->master; |
368 | struct zynq_qspi *qspi = spi_controller_get_devdata(ctlr); | |
67dca5e5 | 369 | |
9b10fa36 | 370 | if (ctlr->busy) |
67dca5e5 NSR |
371 | return -EBUSY; |
372 | ||
373 | clk_enable(qspi->refclk); | |
374 | clk_enable(qspi->pclk); | |
375 | zynq_qspi_write(qspi, ZYNQ_QSPI_ENABLE_OFFSET, | |
376 | ZYNQ_QSPI_ENABLE_ENABLE_MASK); | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | /** | |
382 | * zynq_qspi_write_op - Fills the TX FIFO with as many bytes as possible | |
383 | * @xqspi: Pointer to the zynq_qspi structure | |
384 | * @txcount: Maximum number of words to write | |
385 | * @txempty: Indicates that TxFIFO is empty | |
386 | */ | |
387 | static void zynq_qspi_write_op(struct zynq_qspi *xqspi, int txcount, | |
388 | bool txempty) | |
389 | { | |
390 | int count, len, k; | |
391 | ||
392 | len = xqspi->tx_bytes; | |
393 | if (len && len < 4) { | |
394 | /* | |
395 | * We must empty the TxFIFO between accesses to TXD0, | |
396 | * TXD1, TXD2, TXD3. | |
397 | */ | |
398 | if (txempty) | |
399 | zynq_qspi_txfifo_op(xqspi, len); | |
400 | ||
401 | return; | |
402 | } | |
403 | ||
404 | count = len / 4; | |
405 | if (count > txcount) | |
406 | count = txcount; | |
407 | ||
408 | if (xqspi->txbuf) { | |
ba3ce8cb NSR |
409 | iowrite32_rep(xqspi->regs + ZYNQ_QSPI_TXD_00_00_OFFSET, |
410 | xqspi->txbuf, count); | |
67dca5e5 NSR |
411 | xqspi->txbuf += count * 4; |
412 | } else { | |
413 | for (k = 0; k < count; k++) | |
414 | writel_relaxed(0, xqspi->regs + | |
415 | ZYNQ_QSPI_TXD_00_00_OFFSET); | |
416 | } | |
417 | ||
418 | xqspi->tx_bytes -= count * 4; | |
419 | } | |
420 | ||
421 | /** | |
422 | * zynq_qspi_read_op - Drains the RX FIFO by as many bytes as possible | |
423 | * @xqspi: Pointer to the zynq_qspi structure | |
424 | * @rxcount: Maximum number of words to read | |
425 | */ | |
426 | static void zynq_qspi_read_op(struct zynq_qspi *xqspi, int rxcount) | |
427 | { | |
428 | int count, len, k; | |
429 | ||
430 | len = xqspi->rx_bytes - xqspi->tx_bytes; | |
431 | count = len / 4; | |
432 | if (count > rxcount) | |
433 | count = rxcount; | |
434 | if (xqspi->rxbuf) { | |
ba3ce8cb NSR |
435 | ioread32_rep(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET, |
436 | xqspi->rxbuf, count); | |
67dca5e5 NSR |
437 | xqspi->rxbuf += count * 4; |
438 | } else { | |
439 | for (k = 0; k < count; k++) | |
440 | readl_relaxed(xqspi->regs + ZYNQ_QSPI_RXD_OFFSET); | |
441 | } | |
442 | xqspi->rx_bytes -= count * 4; | |
443 | len -= count * 4; | |
444 | ||
445 | if (len && len < 4 && count < rxcount) | |
446 | zynq_qspi_rxfifo_op(xqspi, len); | |
447 | } | |
448 | ||
449 | /** | |
450 | * zynq_qspi_irq - Interrupt service routine of the QSPI controller | |
451 | * @irq: IRQ number | |
452 | * @dev_id: Pointer to the xqspi structure | |
453 | * | |
454 | * This function handles TX empty only. | |
455 | * On TX empty interrupt this function reads the received data from RX FIFO and | |
456 | * fills the TX FIFO if there is any data remaining to be transferred. | |
457 | * | |
458 | * Return: IRQ_HANDLED when interrupt is handled; IRQ_NONE otherwise. | |
459 | */ | |
460 | static irqreturn_t zynq_qspi_irq(int irq, void *dev_id) | |
461 | { | |
462 | u32 intr_status; | |
463 | bool txempty; | |
464 | struct zynq_qspi *xqspi = (struct zynq_qspi *)dev_id; | |
465 | ||
466 | intr_status = zynq_qspi_read(xqspi, ZYNQ_QSPI_STATUS_OFFSET); | |
467 | zynq_qspi_write(xqspi, ZYNQ_QSPI_STATUS_OFFSET, intr_status); | |
468 | ||
469 | if ((intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK) || | |
470 | (intr_status & ZYNQ_QSPI_IXR_RXNEMTY_MASK)) { | |
471 | /* | |
472 | * This bit is set when Tx FIFO has < THRESHOLD entries. | |
473 | * We have the THRESHOLD value set to 1, | |
474 | * so this bit indicates Tx FIFO is empty. | |
475 | */ | |
476 | txempty = !!(intr_status & ZYNQ_QSPI_IXR_TXNFULL_MASK); | |
477 | /* Read out the data from the RX FIFO */ | |
478 | zynq_qspi_read_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD); | |
479 | if (xqspi->tx_bytes) { | |
480 | /* There is more data to send */ | |
481 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_RX_THRESHOLD, | |
482 | txempty); | |
483 | } else { | |
484 | /* | |
485 | * If transfer and receive is completed then only send | |
486 | * complete signal. | |
487 | */ | |
488 | if (!xqspi->rx_bytes) { | |
489 | zynq_qspi_write(xqspi, | |
490 | ZYNQ_QSPI_IDIS_OFFSET, | |
491 | ZYNQ_QSPI_IXR_RXTX_MASK); | |
492 | complete(&xqspi->data_completion); | |
493 | } | |
494 | } | |
495 | return IRQ_HANDLED; | |
496 | } | |
497 | ||
498 | return IRQ_NONE; | |
499 | } | |
500 | ||
501 | /** | |
502 | * zynq_qspi_exec_mem_op() - Initiates the QSPI transfer | |
503 | * @mem: the SPI memory | |
504 | * @op: the memory operation to execute | |
505 | * | |
506 | * Executes a memory operation. | |
507 | * | |
508 | * This function first selects the chip and starts the memory operation. | |
509 | * | |
510 | * Return: 0 in case of success, a negative error code otherwise. | |
511 | */ | |
512 | static int zynq_qspi_exec_mem_op(struct spi_mem *mem, | |
513 | const struct spi_mem_op *op) | |
514 | { | |
515 | struct zynq_qspi *xqspi = spi_controller_get_devdata(mem->spi->master); | |
516 | int err = 0, i; | |
517 | u8 *tmpbuf; | |
518 | ||
519 | dev_dbg(xqspi->dev, "cmd:%#x mode:%d.%d.%d.%d\n", | |
520 | op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, | |
521 | op->dummy.buswidth, op->data.buswidth); | |
522 | ||
523 | zynq_qspi_chipselect(mem->spi, true); | |
524 | zynq_qspi_config_op(xqspi, mem->spi); | |
525 | ||
526 | if (op->cmd.opcode) { | |
527 | reinit_completion(&xqspi->data_completion); | |
528 | xqspi->txbuf = (u8 *)&op->cmd.opcode; | |
529 | xqspi->rxbuf = NULL; | |
530 | xqspi->tx_bytes = sizeof(op->cmd.opcode); | |
531 | xqspi->rx_bytes = sizeof(op->cmd.opcode); | |
532 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); | |
533 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, | |
534 | ZYNQ_QSPI_IXR_RXTX_MASK); | |
535 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, | |
536 | msecs_to_jiffies(1000))) | |
537 | err = -ETIMEDOUT; | |
538 | } | |
539 | ||
540 | if (op->addr.nbytes) { | |
541 | for (i = 0; i < op->addr.nbytes; i++) { | |
542 | xqspi->txbuf[i] = op->addr.val >> | |
543 | (8 * (op->addr.nbytes - i - 1)); | |
544 | } | |
545 | ||
546 | reinit_completion(&xqspi->data_completion); | |
547 | xqspi->rxbuf = NULL; | |
548 | xqspi->tx_bytes = op->addr.nbytes; | |
549 | xqspi->rx_bytes = op->addr.nbytes; | |
550 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); | |
551 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, | |
552 | ZYNQ_QSPI_IXR_RXTX_MASK); | |
553 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, | |
554 | msecs_to_jiffies(1000))) | |
555 | err = -ETIMEDOUT; | |
556 | } | |
557 | ||
558 | if (op->dummy.nbytes) { | |
559 | tmpbuf = kzalloc(op->dummy.nbytes, GFP_KERNEL); | |
560 | memset(tmpbuf, 0xff, op->dummy.nbytes); | |
561 | reinit_completion(&xqspi->data_completion); | |
562 | xqspi->txbuf = tmpbuf; | |
563 | xqspi->rxbuf = NULL; | |
564 | xqspi->tx_bytes = op->dummy.nbytes; | |
565 | xqspi->rx_bytes = op->dummy.nbytes; | |
566 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); | |
567 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, | |
568 | ZYNQ_QSPI_IXR_RXTX_MASK); | |
569 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, | |
570 | msecs_to_jiffies(1000))) | |
571 | err = -ETIMEDOUT; | |
572 | ||
573 | kfree(tmpbuf); | |
574 | } | |
575 | ||
576 | if (op->data.nbytes) { | |
577 | reinit_completion(&xqspi->data_completion); | |
578 | if (op->data.dir == SPI_MEM_DATA_OUT) { | |
579 | xqspi->txbuf = (u8 *)op->data.buf.out; | |
580 | xqspi->tx_bytes = op->data.nbytes; | |
581 | xqspi->rxbuf = NULL; | |
582 | xqspi->rx_bytes = op->data.nbytes; | |
583 | } else { | |
584 | xqspi->txbuf = NULL; | |
585 | xqspi->rxbuf = (u8 *)op->data.buf.in; | |
586 | xqspi->rx_bytes = op->data.nbytes; | |
587 | xqspi->tx_bytes = op->data.nbytes; | |
588 | } | |
589 | ||
590 | zynq_qspi_write_op(xqspi, ZYNQ_QSPI_FIFO_DEPTH, true); | |
591 | zynq_qspi_write(xqspi, ZYNQ_QSPI_IEN_OFFSET, | |
592 | ZYNQ_QSPI_IXR_RXTX_MASK); | |
593 | if (!wait_for_completion_interruptible_timeout(&xqspi->data_completion, | |
594 | msecs_to_jiffies(1000))) | |
595 | err = -ETIMEDOUT; | |
596 | } | |
597 | zynq_qspi_chipselect(mem->spi, false); | |
598 | ||
599 | return err; | |
600 | } | |
601 | ||
602 | static const struct spi_controller_mem_ops zynq_qspi_mem_ops = { | |
603 | .supports_op = zynq_qspi_supports_op, | |
604 | .exec_op = zynq_qspi_exec_mem_op, | |
605 | }; | |
606 | ||
607 | /** | |
608 | * zynq_qspi_probe - Probe method for the QSPI driver | |
609 | * @pdev: Pointer to the platform_device structure | |
610 | * | |
611 | * This function initializes the driver data structures and the hardware. | |
612 | * | |
613 | * Return: 0 on success and error value on failure | |
614 | */ | |
615 | static int zynq_qspi_probe(struct platform_device *pdev) | |
616 | { | |
617 | int ret = 0; | |
618 | struct spi_controller *ctlr; | |
619 | struct device *dev = &pdev->dev; | |
620 | struct device_node *np = dev->of_node; | |
621 | struct zynq_qspi *xqspi; | |
67dca5e5 NSR |
622 | u32 num_cs; |
623 | ||
624 | ctlr = spi_alloc_master(&pdev->dev, sizeof(*xqspi)); | |
625 | if (!ctlr) | |
626 | return -ENOMEM; | |
627 | ||
628 | xqspi = spi_controller_get_devdata(ctlr); | |
629 | xqspi->dev = dev; | |
630 | platform_set_drvdata(pdev, xqspi); | |
ae91a439 | 631 | xqspi->regs = devm_platform_ioremap_resource(pdev, 0); |
67dca5e5 NSR |
632 | if (IS_ERR(xqspi->regs)) { |
633 | ret = PTR_ERR(xqspi->regs); | |
634 | goto remove_master; | |
635 | } | |
636 | ||
637 | xqspi->pclk = devm_clk_get(&pdev->dev, "pclk"); | |
638 | if (IS_ERR(xqspi->pclk)) { | |
639 | dev_err(&pdev->dev, "pclk clock not found.\n"); | |
640 | ret = PTR_ERR(xqspi->pclk); | |
641 | goto remove_master; | |
642 | } | |
643 | ||
644 | init_completion(&xqspi->data_completion); | |
645 | ||
646 | xqspi->refclk = devm_clk_get(&pdev->dev, "ref_clk"); | |
647 | if (IS_ERR(xqspi->refclk)) { | |
648 | dev_err(&pdev->dev, "ref_clk clock not found.\n"); | |
649 | ret = PTR_ERR(xqspi->refclk); | |
650 | goto remove_master; | |
651 | } | |
652 | ||
653 | ret = clk_prepare_enable(xqspi->pclk); | |
654 | if (ret) { | |
655 | dev_err(&pdev->dev, "Unable to enable APB clock.\n"); | |
656 | goto remove_master; | |
657 | } | |
658 | ||
659 | ret = clk_prepare_enable(xqspi->refclk); | |
660 | if (ret) { | |
661 | dev_err(&pdev->dev, "Unable to enable device clock.\n"); | |
662 | goto clk_dis_pclk; | |
663 | } | |
664 | ||
665 | /* QSPI controller initializations */ | |
666 | zynq_qspi_init_hw(xqspi); | |
667 | ||
668 | xqspi->irq = platform_get_irq(pdev, 0); | |
669 | if (xqspi->irq <= 0) { | |
670 | ret = -ENXIO; | |
67dca5e5 NSR |
671 | goto remove_master; |
672 | } | |
673 | ret = devm_request_irq(&pdev->dev, xqspi->irq, zynq_qspi_irq, | |
674 | 0, pdev->name, xqspi); | |
675 | if (ret != 0) { | |
676 | ret = -ENXIO; | |
677 | dev_err(&pdev->dev, "request_irq failed\n"); | |
678 | goto remove_master; | |
679 | } | |
680 | ||
681 | ret = of_property_read_u32(np, "num-cs", | |
682 | &num_cs); | |
087622d0 | 683 | if (ret < 0) { |
67dca5e5 | 684 | ctlr->num_chipselect = ZYNQ_QSPI_DEFAULT_NUM_CS; |
087622d0 MR |
685 | } else if (num_cs > ZYNQ_QSPI_DEFAULT_NUM_CS) { |
686 | dev_err(&pdev->dev, "anything but CS0 is not yet supported\n"); | |
687 | goto remove_master; | |
688 | } else { | |
67dca5e5 | 689 | ctlr->num_chipselect = num_cs; |
087622d0 | 690 | } |
67dca5e5 NSR |
691 | |
692 | ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | | |
693 | SPI_TX_DUAL | SPI_TX_QUAD; | |
694 | ctlr->mem_ops = &zynq_qspi_mem_ops; | |
695 | ctlr->setup = zynq_qspi_setup_op; | |
696 | ctlr->max_speed_hz = clk_get_rate(xqspi->refclk) / 2; | |
697 | ctlr->dev.of_node = np; | |
8eb2fd00 | 698 | ret = devm_spi_register_controller(&pdev->dev, ctlr); |
67dca5e5 NSR |
699 | if (ret) { |
700 | dev_err(&pdev->dev, "spi_register_master failed\n"); | |
701 | goto clk_dis_all; | |
702 | } | |
703 | ||
704 | return ret; | |
705 | ||
706 | clk_dis_all: | |
707 | clk_disable_unprepare(xqspi->refclk); | |
708 | clk_dis_pclk: | |
709 | clk_disable_unprepare(xqspi->pclk); | |
710 | remove_master: | |
711 | spi_controller_put(ctlr); | |
712 | ||
713 | return ret; | |
714 | } | |
715 | ||
716 | /** | |
717 | * zynq_qspi_remove - Remove method for the QSPI driver | |
718 | * @pdev: Pointer to the platform_device structure | |
719 | * | |
720 | * This function is called if a device is physically removed from the system or | |
721 | * if the driver module is being unloaded. It frees all resources allocated to | |
722 | * the device. | |
723 | * | |
724 | * Return: 0 on success and error value on failure | |
725 | */ | |
726 | static int zynq_qspi_remove(struct platform_device *pdev) | |
727 | { | |
728 | struct zynq_qspi *xqspi = platform_get_drvdata(pdev); | |
729 | ||
730 | zynq_qspi_write(xqspi, ZYNQ_QSPI_ENABLE_OFFSET, 0); | |
731 | ||
732 | clk_disable_unprepare(xqspi->refclk); | |
733 | clk_disable_unprepare(xqspi->pclk); | |
734 | ||
735 | return 0; | |
736 | } | |
737 | ||
738 | static const struct of_device_id zynq_qspi_of_match[] = { | |
739 | { .compatible = "xlnx,zynq-qspi-1.0", }, | |
740 | { /* end of table */ } | |
741 | }; | |
742 | ||
743 | MODULE_DEVICE_TABLE(of, zynq_qspi_of_match); | |
744 | ||
745 | /* | |
746 | * zynq_qspi_driver - This structure defines the QSPI platform driver | |
747 | */ | |
748 | static struct platform_driver zynq_qspi_driver = { | |
749 | .probe = zynq_qspi_probe, | |
750 | .remove = zynq_qspi_remove, | |
751 | .driver = { | |
752 | .name = "zynq-qspi", | |
753 | .of_match_table = zynq_qspi_of_match, | |
754 | }, | |
755 | }; | |
756 | ||
757 | module_platform_driver(zynq_qspi_driver); | |
758 | ||
759 | MODULE_AUTHOR("Xilinx, Inc."); | |
760 | MODULE_DESCRIPTION("Xilinx Zynq QSPI driver"); | |
761 | MODULE_LICENSE("GPL"); |