blob: 3e0200618af304ce4e5c9405a4c3c947f75e87ed [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright 2013 Freescale Semiconductor, Inc.
Olivier Deprez0e641232021-09-23 10:07:05 +02004// Copyright 2020 NXP
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005//
6// Freescale DSPI driver
7// This file contains a driver for the Freescale DSPI
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/dmaengine.h>
12#include <linux/dma-mapping.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#include <linux/interrupt.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/kernel.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <linux/module.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/of_device.h>
17#include <linux/pinctrl/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include <linux/regmap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <linux/spi/spi.h>
20#include <linux/spi/spi-fsl-dspi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021
David Brazdil0f672f62019-12-10 10:32:29 +000022#define DRIVER_NAME "fsl-dspi"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000023
24#ifdef CONFIG_M5441x
25#define DSPI_FIFO_SIZE 16
26#else
27#define DSPI_FIFO_SIZE 4
28#endif
29#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
30
David Brazdil0f672f62019-12-10 10:32:29 +000031#define SPI_MCR 0x00
32#define SPI_MCR_MASTER BIT(31)
33#define SPI_MCR_PCSIS (0x3F << 16)
34#define SPI_MCR_CLR_TXF BIT(11)
35#define SPI_MCR_CLR_RXF BIT(10)
36#define SPI_MCR_XSPI BIT(3)
Olivier Deprez0e641232021-09-23 10:07:05 +020037#define SPI_MCR_DIS_TXF BIT(13)
38#define SPI_MCR_DIS_RXF BIT(12)
39#define SPI_MCR_HALT BIT(0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040
David Brazdil0f672f62019-12-10 10:32:29 +000041#define SPI_TCR 0x08
42#define SPI_TCR_GET_TCNT(x) (((x) & GENMASK(31, 16)) >> 16)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043
David Brazdil0f672f62019-12-10 10:32:29 +000044#define SPI_CTAR(x) (0x0c + (((x) & GENMASK(1, 0)) * 4))
45#define SPI_CTAR_FMSZ(x) (((x) << 27) & GENMASK(30, 27))
46#define SPI_CTAR_CPOL BIT(26)
47#define SPI_CTAR_CPHA BIT(25)
48#define SPI_CTAR_LSBFE BIT(24)
49#define SPI_CTAR_PCSSCK(x) (((x) << 22) & GENMASK(23, 22))
50#define SPI_CTAR_PASC(x) (((x) << 20) & GENMASK(21, 20))
51#define SPI_CTAR_PDT(x) (((x) << 18) & GENMASK(19, 18))
52#define SPI_CTAR_PBR(x) (((x) << 16) & GENMASK(17, 16))
53#define SPI_CTAR_CSSCK(x) (((x) << 12) & GENMASK(15, 12))
54#define SPI_CTAR_ASC(x) (((x) << 8) & GENMASK(11, 8))
55#define SPI_CTAR_DT(x) (((x) << 4) & GENMASK(7, 4))
56#define SPI_CTAR_BR(x) ((x) & GENMASK(3, 0))
57#define SPI_CTAR_SCALE_BITS 0xf
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058
David Brazdil0f672f62019-12-10 10:32:29 +000059#define SPI_CTAR0_SLAVE 0x0c
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060
David Brazdil0f672f62019-12-10 10:32:29 +000061#define SPI_SR 0x2c
62#define SPI_SR_TCFQF BIT(31)
63#define SPI_SR_EOQF BIT(28)
64#define SPI_SR_TFUF BIT(27)
65#define SPI_SR_TFFF BIT(25)
66#define SPI_SR_CMDTCF BIT(23)
67#define SPI_SR_SPEF BIT(21)
68#define SPI_SR_RFOF BIT(19)
69#define SPI_SR_TFIWF BIT(18)
70#define SPI_SR_RFDF BIT(17)
71#define SPI_SR_CMDFFF BIT(16)
72#define SPI_SR_CLEAR (SPI_SR_TCFQF | SPI_SR_EOQF | \
73 SPI_SR_TFUF | SPI_SR_TFFF | \
74 SPI_SR_CMDTCF | SPI_SR_SPEF | \
75 SPI_SR_RFOF | SPI_SR_TFIWF | \
76 SPI_SR_RFDF | SPI_SR_CMDFFF)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077
David Brazdil0f672f62019-12-10 10:32:29 +000078#define SPI_RSER_TFFFE BIT(25)
79#define SPI_RSER_TFFFD BIT(24)
80#define SPI_RSER_RFDFE BIT(17)
81#define SPI_RSER_RFDFD BIT(16)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082
David Brazdil0f672f62019-12-10 10:32:29 +000083#define SPI_RSER 0x30
84#define SPI_RSER_TCFQE BIT(31)
85#define SPI_RSER_EOQFE BIT(28)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086
David Brazdil0f672f62019-12-10 10:32:29 +000087#define SPI_PUSHR 0x34
88#define SPI_PUSHR_CMD_CONT BIT(15)
89#define SPI_PUSHR_CMD_CTAS(x) (((x) << 12 & GENMASK(14, 12)))
90#define SPI_PUSHR_CMD_EOQ BIT(11)
91#define SPI_PUSHR_CMD_CTCNT BIT(10)
92#define SPI_PUSHR_CMD_PCS(x) (BIT(x) & GENMASK(5, 0))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
David Brazdil0f672f62019-12-10 10:32:29 +000094#define SPI_PUSHR_SLAVE 0x34
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095
David Brazdil0f672f62019-12-10 10:32:29 +000096#define SPI_POPR 0x38
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
David Brazdil0f672f62019-12-10 10:32:29 +000098#define SPI_TXFR0 0x3c
99#define SPI_TXFR1 0x40
100#define SPI_TXFR2 0x44
101#define SPI_TXFR3 0x48
102#define SPI_RXFR0 0x7c
103#define SPI_RXFR1 0x80
104#define SPI_RXFR2 0x84
105#define SPI_RXFR3 0x88
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106
David Brazdil0f672f62019-12-10 10:32:29 +0000107#define SPI_CTARE(x) (0x11c + (((x) & GENMASK(1, 0)) * 4))
108#define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
109#define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110
David Brazdil0f672f62019-12-10 10:32:29 +0000111#define SPI_SREX 0x13c
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112
David Brazdil0f672f62019-12-10 10:32:29 +0000113#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
114#define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
116/* Register offsets for regmap_pushr */
David Brazdil0f672f62019-12-10 10:32:29 +0000117#define PUSHR_CMD 0x0
118#define PUSHR_TX 0x2
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119
David Brazdil0f672f62019-12-10 10:32:29 +0000120#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121
122struct chip_data {
David Brazdil0f672f62019-12-10 10:32:29 +0000123 u32 ctar_val;
124 u16 void_write_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125};
126
127enum dspi_trans_mode {
128 DSPI_EOQ_MODE = 0,
129 DSPI_TCFQ_MODE,
130 DSPI_DMA_MODE,
131};
132
133struct fsl_dspi_devtype_data {
David Brazdil0f672f62019-12-10 10:32:29 +0000134 enum dspi_trans_mode trans_mode;
135 u8 max_clock_factor;
136 bool xspi_mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137};
138
139static const struct fsl_dspi_devtype_data vf610_data = {
David Brazdil0f672f62019-12-10 10:32:29 +0000140 .trans_mode = DSPI_DMA_MODE,
141 .max_clock_factor = 2,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142};
143
144static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
David Brazdil0f672f62019-12-10 10:32:29 +0000145 .trans_mode = DSPI_TCFQ_MODE,
146 .max_clock_factor = 8,
147 .xspi_mode = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148};
149
150static const struct fsl_dspi_devtype_data ls2085a_data = {
David Brazdil0f672f62019-12-10 10:32:29 +0000151 .trans_mode = DSPI_TCFQ_MODE,
152 .max_clock_factor = 8,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153};
154
155static const struct fsl_dspi_devtype_data coldfire_data = {
David Brazdil0f672f62019-12-10 10:32:29 +0000156 .trans_mode = DSPI_EOQ_MODE,
157 .max_clock_factor = 8,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158};
159
160struct fsl_dspi_dma {
161 /* Length of transfer in words of DSPI_FIFO_SIZE */
David Brazdil0f672f62019-12-10 10:32:29 +0000162 u32 curr_xfer_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163
David Brazdil0f672f62019-12-10 10:32:29 +0000164 u32 *tx_dma_buf;
165 struct dma_chan *chan_tx;
166 dma_addr_t tx_dma_phys;
167 struct completion cmd_tx_complete;
168 struct dma_async_tx_descriptor *tx_desc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169
David Brazdil0f672f62019-12-10 10:32:29 +0000170 u32 *rx_dma_buf;
171 struct dma_chan *chan_rx;
172 dma_addr_t rx_dma_phys;
173 struct completion cmd_rx_complete;
174 struct dma_async_tx_descriptor *rx_desc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000175};
176
177struct fsl_dspi {
David Brazdil0f672f62019-12-10 10:32:29 +0000178 struct spi_controller *ctlr;
179 struct platform_device *pdev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180
David Brazdil0f672f62019-12-10 10:32:29 +0000181 struct regmap *regmap;
182 struct regmap *regmap_pushr;
183 int irq;
184 struct clk *clk;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000185
David Brazdil0f672f62019-12-10 10:32:29 +0000186 struct spi_transfer *cur_transfer;
187 struct spi_message *cur_msg;
188 struct chip_data *cur_chip;
189 size_t len;
190 const void *tx;
191 void *rx;
192 void *rx_end;
193 u16 void_write_data;
194 u16 tx_cmd;
195 u8 bits_per_word;
196 u8 bytes_per_word;
197 const struct fsl_dspi_devtype_data *devtype_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198
Olivier Deprez0e641232021-09-23 10:07:05 +0200199 struct completion xfer_done;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200
David Brazdil0f672f62019-12-10 10:32:29 +0000201 struct fsl_dspi_dma *dma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202};
203
204static u32 dspi_pop_tx(struct fsl_dspi *dspi)
205{
206 u32 txdata = 0;
207
208 if (dspi->tx) {
209 if (dspi->bytes_per_word == 1)
210 txdata = *(u8 *)dspi->tx;
211 else if (dspi->bytes_per_word == 2)
212 txdata = *(u16 *)dspi->tx;
213 else /* dspi->bytes_per_word == 4 */
214 txdata = *(u32 *)dspi->tx;
215 dspi->tx += dspi->bytes_per_word;
216 }
217 dspi->len -= dspi->bytes_per_word;
218 return txdata;
219}
220
221static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
222{
223 u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
224
David Brazdil0f672f62019-12-10 10:32:29 +0000225 if (spi_controller_is_slave(dspi->ctlr))
226 return data;
227
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228 if (dspi->len > 0)
229 cmd |= SPI_PUSHR_CMD_CONT;
230 return cmd << 16 | data;
231}
232
233static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
234{
235 if (!dspi->rx)
236 return;
237
David Brazdil0f672f62019-12-10 10:32:29 +0000238 /* Mask off undefined bits */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239 rxdata &= (1 << dspi->bits_per_word) - 1;
240
241 if (dspi->bytes_per_word == 1)
242 *(u8 *)dspi->rx = rxdata;
243 else if (dspi->bytes_per_word == 2)
244 *(u16 *)dspi->rx = rxdata;
245 else /* dspi->bytes_per_word == 4 */
246 *(u32 *)dspi->rx = rxdata;
247 dspi->rx += dspi->bytes_per_word;
248}
249
250static void dspi_tx_dma_callback(void *arg)
251{
252 struct fsl_dspi *dspi = arg;
253 struct fsl_dspi_dma *dma = dspi->dma;
254
255 complete(&dma->cmd_tx_complete);
256}
257
258static void dspi_rx_dma_callback(void *arg)
259{
260 struct fsl_dspi *dspi = arg;
261 struct fsl_dspi_dma *dma = dspi->dma;
262 int i;
263
264 if (dspi->rx) {
265 for (i = 0; i < dma->curr_xfer_len; i++)
266 dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
267 }
268
269 complete(&dma->cmd_rx_complete);
270}
271
272static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
273{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274 struct device *dev = &dspi->pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000275 struct fsl_dspi_dma *dma = dspi->dma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276 int time_left;
277 int i;
278
279 for (i = 0; i < dma->curr_xfer_len; i++)
280 dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
281
282 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
283 dma->tx_dma_phys,
284 dma->curr_xfer_len *
285 DMA_SLAVE_BUSWIDTH_4_BYTES,
286 DMA_MEM_TO_DEV,
287 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
288 if (!dma->tx_desc) {
289 dev_err(dev, "Not able to get desc for DMA xfer\n");
290 return -EIO;
291 }
292
293 dma->tx_desc->callback = dspi_tx_dma_callback;
294 dma->tx_desc->callback_param = dspi;
295 if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
296 dev_err(dev, "DMA submit failed\n");
297 return -EINVAL;
298 }
299
300 dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
301 dma->rx_dma_phys,
302 dma->curr_xfer_len *
303 DMA_SLAVE_BUSWIDTH_4_BYTES,
304 DMA_DEV_TO_MEM,
305 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
306 if (!dma->rx_desc) {
307 dev_err(dev, "Not able to get desc for DMA xfer\n");
308 return -EIO;
309 }
310
311 dma->rx_desc->callback = dspi_rx_dma_callback;
312 dma->rx_desc->callback_param = dspi;
313 if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
314 dev_err(dev, "DMA submit failed\n");
315 return -EINVAL;
316 }
317
318 reinit_completion(&dspi->dma->cmd_rx_complete);
319 reinit_completion(&dspi->dma->cmd_tx_complete);
320
321 dma_async_issue_pending(dma->chan_rx);
322 dma_async_issue_pending(dma->chan_tx);
323
David Brazdil0f672f62019-12-10 10:32:29 +0000324 if (spi_controller_is_slave(dspi->ctlr)) {
325 wait_for_completion_interruptible(&dspi->dma->cmd_rx_complete);
326 return 0;
327 }
328
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329 time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
David Brazdil0f672f62019-12-10 10:32:29 +0000330 DMA_COMPLETION_TIMEOUT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331 if (time_left == 0) {
332 dev_err(dev, "DMA tx timeout\n");
333 dmaengine_terminate_all(dma->chan_tx);
334 dmaengine_terminate_all(dma->chan_rx);
335 return -ETIMEDOUT;
336 }
337
338 time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
David Brazdil0f672f62019-12-10 10:32:29 +0000339 DMA_COMPLETION_TIMEOUT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 if (time_left == 0) {
341 dev_err(dev, "DMA rx timeout\n");
342 dmaengine_terminate_all(dma->chan_tx);
343 dmaengine_terminate_all(dma->chan_rx);
344 return -ETIMEDOUT;
345 }
346
347 return 0;
348}
349
350static int dspi_dma_xfer(struct fsl_dspi *dspi)
351{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352 struct spi_message *message = dspi->cur_msg;
David Brazdil0f672f62019-12-10 10:32:29 +0000353 struct device *dev = &dspi->pdev->dev;
354 struct fsl_dspi_dma *dma = dspi->dma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000355 int curr_remaining_bytes;
356 int bytes_per_buffer;
357 int ret = 0;
358
359 curr_remaining_bytes = dspi->len;
360 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
361 while (curr_remaining_bytes) {
362 /* Check if current transfer fits the DMA buffer */
363 dma->curr_xfer_len = curr_remaining_bytes
364 / dspi->bytes_per_word;
365 if (dma->curr_xfer_len > bytes_per_buffer)
366 dma->curr_xfer_len = bytes_per_buffer;
367
368 ret = dspi_next_xfer_dma_submit(dspi);
369 if (ret) {
370 dev_err(dev, "DMA transfer failed\n");
371 goto exit;
372
373 } else {
374 const int len =
375 dma->curr_xfer_len * dspi->bytes_per_word;
376 curr_remaining_bytes -= len;
377 message->actual_length += len;
378 if (curr_remaining_bytes < 0)
379 curr_remaining_bytes = 0;
380 }
381 }
382
383exit:
384 return ret;
385}
386
387static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
388{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 struct device *dev = &dspi->pdev->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000390 struct dma_slave_config cfg;
391 struct fsl_dspi_dma *dma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392 int ret;
393
394 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
395 if (!dma)
396 return -ENOMEM;
397
398 dma->chan_rx = dma_request_slave_channel(dev, "rx");
399 if (!dma->chan_rx) {
400 dev_err(dev, "rx dma channel not available\n");
401 ret = -ENODEV;
402 return ret;
403 }
404
405 dma->chan_tx = dma_request_slave_channel(dev, "tx");
406 if (!dma->chan_tx) {
407 dev_err(dev, "tx dma channel not available\n");
408 ret = -ENODEV;
409 goto err_tx_channel;
410 }
411
412 dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
David Brazdil0f672f62019-12-10 10:32:29 +0000413 &dma->tx_dma_phys, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414 if (!dma->tx_dma_buf) {
415 ret = -ENOMEM;
416 goto err_tx_dma_buf;
417 }
418
419 dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
David Brazdil0f672f62019-12-10 10:32:29 +0000420 &dma->rx_dma_phys, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000421 if (!dma->rx_dma_buf) {
422 ret = -ENOMEM;
423 goto err_rx_dma_buf;
424 }
425
Olivier Deprez0e641232021-09-23 10:07:05 +0200426 memset(&cfg, 0, sizeof(cfg));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000427 cfg.src_addr = phy_addr + SPI_POPR;
428 cfg.dst_addr = phy_addr + SPI_PUSHR;
429 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
430 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
431 cfg.src_maxburst = 1;
432 cfg.dst_maxburst = 1;
433
434 cfg.direction = DMA_DEV_TO_MEM;
435 ret = dmaengine_slave_config(dma->chan_rx, &cfg);
436 if (ret) {
437 dev_err(dev, "can't configure rx dma channel\n");
438 ret = -EINVAL;
439 goto err_slave_config;
440 }
441
442 cfg.direction = DMA_MEM_TO_DEV;
443 ret = dmaengine_slave_config(dma->chan_tx, &cfg);
444 if (ret) {
445 dev_err(dev, "can't configure tx dma channel\n");
446 ret = -EINVAL;
447 goto err_slave_config;
448 }
449
450 dspi->dma = dma;
451 init_completion(&dma->cmd_tx_complete);
452 init_completion(&dma->cmd_rx_complete);
453
454 return 0;
455
456err_slave_config:
457 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
458 dma->rx_dma_buf, dma->rx_dma_phys);
459err_rx_dma_buf:
460 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
461 dma->tx_dma_buf, dma->tx_dma_phys);
462err_tx_dma_buf:
463 dma_release_channel(dma->chan_tx);
464err_tx_channel:
465 dma_release_channel(dma->chan_rx);
466
467 devm_kfree(dev, dma);
468 dspi->dma = NULL;
469
470 return ret;
471}
472
473static void dspi_release_dma(struct fsl_dspi *dspi)
474{
475 struct fsl_dspi_dma *dma = dspi->dma;
476 struct device *dev = &dspi->pdev->dev;
477
David Brazdil0f672f62019-12-10 10:32:29 +0000478 if (!dma)
479 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480
David Brazdil0f672f62019-12-10 10:32:29 +0000481 if (dma->chan_tx) {
482 dma_unmap_single(dev, dma->tx_dma_phys,
483 DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
484 dma_release_channel(dma->chan_tx);
485 }
486
487 if (dma->chan_rx) {
488 dma_unmap_single(dev, dma->rx_dma_phys,
489 DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
490 dma_release_channel(dma->chan_rx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 }
492}
493
494static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
David Brazdil0f672f62019-12-10 10:32:29 +0000495 unsigned long clkrate)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000496{
497 /* Valid baud rate pre-scaler values */
498 int pbr_tbl[4] = {2, 3, 5, 7};
499 int brs[16] = { 2, 4, 6, 8,
David Brazdil0f672f62019-12-10 10:32:29 +0000500 16, 32, 64, 128,
501 256, 512, 1024, 2048,
502 4096, 8192, 16384, 32768 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 int scale_needed, scale, minscale = INT_MAX;
504 int i, j;
505
506 scale_needed = clkrate / speed_hz;
507 if (clkrate % speed_hz)
508 scale_needed++;
509
510 for (i = 0; i < ARRAY_SIZE(brs); i++)
511 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
512 scale = brs[i] * pbr_tbl[j];
513 if (scale >= scale_needed) {
514 if (scale < minscale) {
515 minscale = scale;
516 *br = i;
517 *pbr = j;
518 }
519 break;
520 }
521 }
522
523 if (minscale == INT_MAX) {
524 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
525 speed_hz, clkrate);
526 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
527 *br = ARRAY_SIZE(brs) - 1;
528 }
529}
530
531static void ns_delay_scale(char *psc, char *sc, int delay_ns,
David Brazdil0f672f62019-12-10 10:32:29 +0000532 unsigned long clkrate)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 int scale_needed, scale, minscale = INT_MAX;
David Brazdil0f672f62019-12-10 10:32:29 +0000535 int pscale_tbl[4] = {1, 3, 5, 7};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000536 u32 remainder;
David Brazdil0f672f62019-12-10 10:32:29 +0000537 int i, j;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538
539 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
David Brazdil0f672f62019-12-10 10:32:29 +0000540 &remainder);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000541 if (remainder)
542 scale_needed++;
543
544 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
545 for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
546 scale = pscale_tbl[i] * (2 << j);
547 if (scale >= scale_needed) {
548 if (scale < minscale) {
549 minscale = scale;
550 *psc = i;
551 *sc = j;
552 }
553 break;
554 }
555 }
556
557 if (minscale == INT_MAX) {
558 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
559 delay_ns, clkrate);
560 *psc = ARRAY_SIZE(pscale_tbl) - 1;
561 *sc = SPI_CTAR_SCALE_BITS;
562 }
563}
564
565static void fifo_write(struct fsl_dspi *dspi)
566{
567 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi));
568}
569
570static void cmd_fifo_write(struct fsl_dspi *dspi)
571{
572 u16 cmd = dspi->tx_cmd;
573
574 if (dspi->len > 0)
575 cmd |= SPI_PUSHR_CMD_CONT;
576 regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd);
577}
578
579static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata)
580{
581 regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata);
582}
583
584static void dspi_tcfq_write(struct fsl_dspi *dspi)
585{
586 /* Clear transfer count */
587 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
588
589 if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200590 /* Write the CMD FIFO entry first, and then the two
591 * corresponding TX FIFO entries.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592 */
593 u32 data = dspi_pop_tx(dspi);
594
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595 cmd_fifo_write(dspi);
Olivier Deprez0e641232021-09-23 10:07:05 +0200596 tx_fifo_write(dspi, data & 0xFFFF);
597 tx_fifo_write(dspi, data >> 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598 } else {
599 /* Write one entry to both TX FIFO and CMD FIFO
600 * simultaneously.
601 */
602 fifo_write(dspi);
603 }
604}
605
606static u32 fifo_read(struct fsl_dspi *dspi)
607{
608 u32 rxdata = 0;
609
610 regmap_read(dspi->regmap, SPI_POPR, &rxdata);
611 return rxdata;
612}
613
614static void dspi_tcfq_read(struct fsl_dspi *dspi)
615{
616 dspi_push_rx(dspi, fifo_read(dspi));
617}
618
619static void dspi_eoq_write(struct fsl_dspi *dspi)
620{
621 int fifo_size = DSPI_FIFO_SIZE;
622 u16 xfer_cmd = dspi->tx_cmd;
623
624 /* Fill TX FIFO with as many transfers as possible */
625 while (dspi->len && fifo_size--) {
626 dspi->tx_cmd = xfer_cmd;
627 /* Request EOQF for last transfer in FIFO */
628 if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
629 dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
630 /* Clear transfer count for first transfer in FIFO */
631 if (fifo_size == (DSPI_FIFO_SIZE - 1))
632 dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
633 /* Write combined TX FIFO and CMD FIFO entry */
634 fifo_write(dspi);
635 }
636}
637
638static void dspi_eoq_read(struct fsl_dspi *dspi)
639{
640 int fifo_size = DSPI_FIFO_SIZE;
641
David Brazdil0f672f62019-12-10 10:32:29 +0000642 /* Read one FIFO entry and push to rx buffer */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643 while ((dspi->rx < dspi->rx_end) && fifo_size--)
644 dspi_push_rx(dspi, fifo_read(dspi));
645}
646
David Brazdil0f672f62019-12-10 10:32:29 +0000647static int dspi_rxtx(struct fsl_dspi *dspi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000648{
David Brazdil0f672f62019-12-10 10:32:29 +0000649 struct spi_message *msg = dspi->cur_msg;
650 enum dspi_trans_mode trans_mode;
651 u16 spi_tcnt;
652 u32 spi_tcr;
653
654 /* Get transfer counter (in number of SPI transfers). It was
655 * reset to 0 when transfer(s) were started.
656 */
657 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
658 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
659 /* Update total number of bytes that were transferred */
660 msg->actual_length += spi_tcnt * dspi->bytes_per_word;
661
662 trans_mode = dspi->devtype_data->trans_mode;
663 if (trans_mode == DSPI_EOQ_MODE)
664 dspi_eoq_read(dspi);
665 else if (trans_mode == DSPI_TCFQ_MODE)
666 dspi_tcfq_read(dspi);
667
668 if (!dspi->len)
669 /* Success! */
670 return 0;
671
672 if (trans_mode == DSPI_EOQ_MODE)
673 dspi_eoq_write(dspi);
674 else if (trans_mode == DSPI_TCFQ_MODE)
675 dspi_tcfq_write(dspi);
676
677 return -EINPROGRESS;
678}
679
680static int dspi_poll(struct fsl_dspi *dspi)
681{
682 int tries = 1000;
683 u32 spi_sr;
684
685 do {
686 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
687 regmap_write(dspi->regmap, SPI_SR, spi_sr);
688
689 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF))
690 break;
691 } while (--tries);
692
693 if (!tries)
694 return -ETIMEDOUT;
695
696 return dspi_rxtx(dspi);
697}
698
699static irqreturn_t dspi_interrupt(int irq, void *dev_id)
700{
701 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
702 u32 spi_sr;
703
704 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
705 regmap_write(dspi->regmap, SPI_SR, spi_sr);
706
707 if (!(spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)))
708 return IRQ_NONE;
709
Olivier Deprez0e641232021-09-23 10:07:05 +0200710 if (dspi_rxtx(dspi) == 0)
711 complete(&dspi->xfer_done);
David Brazdil0f672f62019-12-10 10:32:29 +0000712
713 return IRQ_HANDLED;
714}
715
716static int dspi_transfer_one_message(struct spi_controller *ctlr,
717 struct spi_message *message)
718{
719 struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000720 struct spi_device *spi = message->spi;
David Brazdil0f672f62019-12-10 10:32:29 +0000721 enum dspi_trans_mode trans_mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722 struct spi_transfer *transfer;
723 int status = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724
725 message->actual_length = 0;
726
727 list_for_each_entry(transfer, &message->transfers, transfer_list) {
728 dspi->cur_transfer = transfer;
729 dspi->cur_msg = message;
730 dspi->cur_chip = spi_get_ctldata(spi);
731 /* Prepare command word for CMD FIFO */
732 dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
David Brazdil0f672f62019-12-10 10:32:29 +0000733 SPI_PUSHR_CMD_PCS(spi->chip_select);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 if (list_is_last(&dspi->cur_transfer->transfer_list,
735 &dspi->cur_msg->transfers)) {
736 /* Leave PCS activated after last transfer when
737 * cs_change is set.
738 */
739 if (transfer->cs_change)
740 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
741 } else {
742 /* Keep PCS active between transfers in same message
743 * when cs_change is not set, and de-activate PCS
744 * between transfers in the same message when
745 * cs_change is set.
746 */
747 if (!transfer->cs_change)
748 dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
749 }
750
751 dspi->void_write_data = dspi->cur_chip->void_write_data;
752
753 dspi->tx = transfer->tx_buf;
754 dspi->rx = transfer->rx_buf;
755 dspi->rx_end = dspi->rx + transfer->len;
756 dspi->len = transfer->len;
757 /* Validated transfer specific frame size (defaults applied) */
758 dspi->bits_per_word = transfer->bits_per_word;
759 if (transfer->bits_per_word <= 8)
760 dspi->bytes_per_word = 1;
761 else if (transfer->bits_per_word <= 16)
762 dspi->bytes_per_word = 2;
763 else
764 dspi->bytes_per_word = 4;
765
766 regmap_update_bits(dspi->regmap, SPI_MCR,
767 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
768 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
769 regmap_write(dspi->regmap, SPI_CTAR(0),
770 dspi->cur_chip->ctar_val |
771 SPI_FRAME_BITS(transfer->bits_per_word));
772 if (dspi->devtype_data->xspi_mode)
773 regmap_write(dspi->regmap, SPI_CTARE(0),
David Brazdil0f672f62019-12-10 10:32:29 +0000774 SPI_FRAME_EBITS(transfer->bits_per_word) |
775 SPI_CTARE_DTCP(1));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000776
777 trans_mode = dspi->devtype_data->trans_mode;
778 switch (trans_mode) {
779 case DSPI_EOQ_MODE:
780 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
781 dspi_eoq_write(dspi);
782 break;
783 case DSPI_TCFQ_MODE:
784 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
785 dspi_tcfq_write(dspi);
786 break;
787 case DSPI_DMA_MODE:
788 regmap_write(dspi->regmap, SPI_RSER,
David Brazdil0f672f62019-12-10 10:32:29 +0000789 SPI_RSER_TFFFE | SPI_RSER_TFFFD |
790 SPI_RSER_RFDFE | SPI_RSER_RFDFD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791 status = dspi_dma_xfer(dspi);
792 break;
793 default:
794 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
795 trans_mode);
796 status = -EINVAL;
797 goto out;
798 }
799
David Brazdil0f672f62019-12-10 10:32:29 +0000800 if (!dspi->irq) {
801 do {
802 status = dspi_poll(dspi);
803 } while (status == -EINPROGRESS);
804 } else if (trans_mode != DSPI_DMA_MODE) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200805 wait_for_completion(&dspi->xfer_done);
806 reinit_completion(&dspi->xfer_done);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000807 }
808
809 if (transfer->delay_usecs)
810 udelay(transfer->delay_usecs);
811 }
812
813out:
814 message->status = status;
David Brazdil0f672f62019-12-10 10:32:29 +0000815 spi_finalize_current_message(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000816
817 return status;
818}
819
820static int dspi_setup(struct spi_device *spi)
821{
David Brazdil0f672f62019-12-10 10:32:29 +0000822 struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000823 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000824 u32 cs_sck_delay = 0, sck_cs_delay = 0;
825 struct fsl_dspi_platform_data *pdata;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000826 unsigned char pasc = 0, asc = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000827 struct chip_data *chip;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 unsigned long clkrate;
829
830 /* Only alloc on first setup */
831 chip = spi_get_ctldata(spi);
832 if (chip == NULL) {
833 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
834 if (!chip)
835 return -ENOMEM;
836 }
837
838 pdata = dev_get_platdata(&dspi->pdev->dev);
839
840 if (!pdata) {
841 of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
David Brazdil0f672f62019-12-10 10:32:29 +0000842 &cs_sck_delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000843
844 of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
David Brazdil0f672f62019-12-10 10:32:29 +0000845 &sck_cs_delay);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000846 } else {
847 cs_sck_delay = pdata->cs_sck_delay;
848 sck_cs_delay = pdata->sck_cs_delay;
849 }
850
851 chip->void_write_data = 0;
852
853 clkrate = clk_get_rate(dspi->clk);
854 hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
855
856 /* Set PCS to SCK delay scale values */
857 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
858
859 /* Set After SCK delay scale values */
860 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
861
David Brazdil0f672f62019-12-10 10:32:29 +0000862 chip->ctar_val = 0;
863 if (spi->mode & SPI_CPOL)
864 chip->ctar_val |= SPI_CTAR_CPOL;
865 if (spi->mode & SPI_CPHA)
866 chip->ctar_val |= SPI_CTAR_CPHA;
867
868 if (!spi_controller_is_slave(dspi->ctlr)) {
869 chip->ctar_val |= SPI_CTAR_PCSSCK(pcssck) |
870 SPI_CTAR_CSSCK(cssck) |
871 SPI_CTAR_PASC(pasc) |
872 SPI_CTAR_ASC(asc) |
873 SPI_CTAR_PBR(pbr) |
874 SPI_CTAR_BR(br);
875
876 if (spi->mode & SPI_LSB_FIRST)
877 chip->ctar_val |= SPI_CTAR_LSBFE;
878 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000879
880 spi_set_ctldata(spi, chip);
881
882 return 0;
883}
884
885static void dspi_cleanup(struct spi_device *spi)
886{
887 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
888
889 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000890 spi->controller->bus_num, spi->chip_select);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000891
892 kfree(chip);
893}
894
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000895static const struct of_device_id fsl_dspi_dt_ids[] = {
896 { .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
897 { .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
898 { .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
899 { /* sentinel */ }
900};
901MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
902
903#ifdef CONFIG_PM_SLEEP
904static int dspi_suspend(struct device *dev)
905{
David Brazdil0f672f62019-12-10 10:32:29 +0000906 struct spi_controller *ctlr = dev_get_drvdata(dev);
907 struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000908
Olivier Deprez0e641232021-09-23 10:07:05 +0200909 if (dspi->irq)
910 disable_irq(dspi->irq);
David Brazdil0f672f62019-12-10 10:32:29 +0000911 spi_controller_suspend(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912 clk_disable_unprepare(dspi->clk);
913
914 pinctrl_pm_select_sleep_state(dev);
915
916 return 0;
917}
918
919static int dspi_resume(struct device *dev)
920{
David Brazdil0f672f62019-12-10 10:32:29 +0000921 struct spi_controller *ctlr = dev_get_drvdata(dev);
922 struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923 int ret;
924
925 pinctrl_pm_select_default_state(dev);
926
927 ret = clk_prepare_enable(dspi->clk);
928 if (ret)
929 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000930 spi_controller_resume(ctlr);
Olivier Deprez0e641232021-09-23 10:07:05 +0200931 if (dspi->irq)
932 enable_irq(dspi->irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933
934 return 0;
935}
936#endif /* CONFIG_PM_SLEEP */
937
938static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
939
940static const struct regmap_range dspi_volatile_ranges[] = {
941 regmap_reg_range(SPI_MCR, SPI_TCR),
942 regmap_reg_range(SPI_SR, SPI_SR),
943 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
944};
945
946static const struct regmap_access_table dspi_volatile_table = {
David Brazdil0f672f62019-12-10 10:32:29 +0000947 .yes_ranges = dspi_volatile_ranges,
948 .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949};
950
951static const struct regmap_config dspi_regmap_config = {
David Brazdil0f672f62019-12-10 10:32:29 +0000952 .reg_bits = 32,
953 .val_bits = 32,
954 .reg_stride = 4,
955 .max_register = 0x88,
956 .volatile_table = &dspi_volatile_table,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957};
958
959static const struct regmap_range dspi_xspi_volatile_ranges[] = {
960 regmap_reg_range(SPI_MCR, SPI_TCR),
961 regmap_reg_range(SPI_SR, SPI_SR),
962 regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
963 regmap_reg_range(SPI_SREX, SPI_SREX),
964};
965
966static const struct regmap_access_table dspi_xspi_volatile_table = {
David Brazdil0f672f62019-12-10 10:32:29 +0000967 .yes_ranges = dspi_xspi_volatile_ranges,
968 .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000969};
970
971static const struct regmap_config dspi_xspi_regmap_config[] = {
972 {
David Brazdil0f672f62019-12-10 10:32:29 +0000973 .reg_bits = 32,
974 .val_bits = 32,
975 .reg_stride = 4,
976 .max_register = 0x13c,
977 .volatile_table = &dspi_xspi_volatile_table,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000978 },
979 {
David Brazdil0f672f62019-12-10 10:32:29 +0000980 .name = "pushr",
981 .reg_bits = 16,
982 .val_bits = 16,
983 .reg_stride = 2,
984 .max_register = 0x2,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000985 },
986};
987
988static void dspi_init(struct fsl_dspi *dspi)
989{
David Brazdil0f672f62019-12-10 10:32:29 +0000990 unsigned int mcr = SPI_MCR_PCSIS;
991
992 if (dspi->devtype_data->xspi_mode)
993 mcr |= SPI_MCR_XSPI;
994 if (!spi_controller_is_slave(dspi->ctlr))
995 mcr |= SPI_MCR_MASTER;
996
997 regmap_write(dspi->regmap, SPI_MCR, mcr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000998 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
999 if (dspi->devtype_data->xspi_mode)
1000 regmap_write(dspi->regmap, SPI_CTARE(0),
1001 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
1002}
1003
1004static int dspi_probe(struct platform_device *pdev)
1005{
1006 struct device_node *np = pdev->dev.of_node;
David Brazdil0f672f62019-12-10 10:32:29 +00001007 const struct regmap_config *regmap_config;
1008 struct fsl_dspi_platform_data *pdata;
1009 struct spi_controller *ctlr;
1010 int ret, cs_num, bus_num;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011 struct fsl_dspi *dspi;
1012 struct resource *res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013 void __iomem *base;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001014
David Brazdil0f672f62019-12-10 10:32:29 +00001015 ctlr = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
1016 if (!ctlr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001017 return -ENOMEM;
1018
David Brazdil0f672f62019-12-10 10:32:29 +00001019 dspi = spi_controller_get_devdata(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001020 dspi->pdev = pdev;
David Brazdil0f672f62019-12-10 10:32:29 +00001021 dspi->ctlr = ctlr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001022
David Brazdil0f672f62019-12-10 10:32:29 +00001023 ctlr->setup = dspi_setup;
1024 ctlr->transfer_one_message = dspi_transfer_one_message;
1025 ctlr->dev.of_node = pdev->dev.of_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001026
David Brazdil0f672f62019-12-10 10:32:29 +00001027 ctlr->cleanup = dspi_cleanup;
1028 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029
1030 pdata = dev_get_platdata(&pdev->dev);
1031 if (pdata) {
David Brazdil0f672f62019-12-10 10:32:29 +00001032 ctlr->num_chipselect = pdata->cs_num;
1033 ctlr->bus_num = pdata->bus_num;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001034
1035 dspi->devtype_data = &coldfire_data;
1036 } else {
1037
1038 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
1039 if (ret < 0) {
1040 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001041 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001042 }
David Brazdil0f672f62019-12-10 10:32:29 +00001043 ctlr->num_chipselect = cs_num;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001044
1045 ret = of_property_read_u32(np, "bus-num", &bus_num);
1046 if (ret < 0) {
1047 dev_err(&pdev->dev, "can't get bus-num\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001048 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001049 }
David Brazdil0f672f62019-12-10 10:32:29 +00001050 ctlr->bus_num = bus_num;
1051
1052 if (of_property_read_bool(np, "spi-slave"))
1053 ctlr->slave = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001054
1055 dspi->devtype_data = of_device_get_match_data(&pdev->dev);
1056 if (!dspi->devtype_data) {
1057 dev_err(&pdev->dev, "can't get devtype_data\n");
1058 ret = -EFAULT;
David Brazdil0f672f62019-12-10 10:32:29 +00001059 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060 }
1061 }
1062
1063 if (dspi->devtype_data->xspi_mode)
David Brazdil0f672f62019-12-10 10:32:29 +00001064 ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001065 else
David Brazdil0f672f62019-12-10 10:32:29 +00001066 ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067
1068 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1069 base = devm_ioremap_resource(&pdev->dev, res);
1070 if (IS_ERR(base)) {
1071 ret = PTR_ERR(base);
David Brazdil0f672f62019-12-10 10:32:29 +00001072 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 }
1074
1075 if (dspi->devtype_data->xspi_mode)
1076 regmap_config = &dspi_xspi_regmap_config[0];
1077 else
1078 regmap_config = &dspi_regmap_config;
1079 dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
1080 if (IS_ERR(dspi->regmap)) {
1081 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1082 PTR_ERR(dspi->regmap));
1083 ret = PTR_ERR(dspi->regmap);
David Brazdil0f672f62019-12-10 10:32:29 +00001084 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001085 }
1086
1087 if (dspi->devtype_data->xspi_mode) {
1088 dspi->regmap_pushr = devm_regmap_init_mmio(
1089 &pdev->dev, base + SPI_PUSHR,
1090 &dspi_xspi_regmap_config[1]);
1091 if (IS_ERR(dspi->regmap_pushr)) {
1092 dev_err(&pdev->dev,
1093 "failed to init pushr regmap: %ld\n",
1094 PTR_ERR(dspi->regmap_pushr));
1095 ret = PTR_ERR(dspi->regmap_pushr);
David Brazdil0f672f62019-12-10 10:32:29 +00001096 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001097 }
1098 }
1099
1100 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1101 if (IS_ERR(dspi->clk)) {
1102 ret = PTR_ERR(dspi->clk);
1103 dev_err(&pdev->dev, "unable to get clock\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001104 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001105 }
1106 ret = clk_prepare_enable(dspi->clk);
1107 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +00001108 goto out_ctlr_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001109
1110 dspi_init(dspi);
David Brazdil0f672f62019-12-10 10:32:29 +00001111
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001112 dspi->irq = platform_get_irq(pdev, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00001113 if (dspi->irq <= 0) {
1114 dev_info(&pdev->dev,
1115 "can't get platform irq, using poll mode\n");
1116 dspi->irq = 0;
1117 goto poll_mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118 }
1119
Olivier Deprez0e641232021-09-23 10:07:05 +02001120 ret = request_threaded_irq(dspi->irq, dspi_interrupt, NULL,
1121 IRQF_SHARED, pdev->name, dspi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122 if (ret < 0) {
1123 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1124 goto out_clk_put;
1125 }
1126
Olivier Deprez0e641232021-09-23 10:07:05 +02001127 init_completion(&dspi->xfer_done);
David Brazdil0f672f62019-12-10 10:32:29 +00001128
1129poll_mode:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
1131 ret = dspi_request_dma(dspi, res->start);
1132 if (ret < 0) {
1133 dev_err(&pdev->dev, "can't get dma channels\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02001134 goto out_free_irq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001135 }
1136 }
1137
David Brazdil0f672f62019-12-10 10:32:29 +00001138 ctlr->max_speed_hz =
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139 clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
1140
David Brazdil0f672f62019-12-10 10:32:29 +00001141 platform_set_drvdata(pdev, ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142
David Brazdil0f672f62019-12-10 10:32:29 +00001143 ret = spi_register_controller(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001144 if (ret != 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00001145 dev_err(&pdev->dev, "Problem registering DSPI ctlr\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02001146 goto out_release_dma;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001147 }
1148
1149 return ret;
1150
Olivier Deprez0e641232021-09-23 10:07:05 +02001151out_release_dma:
1152 dspi_release_dma(dspi);
1153out_free_irq:
1154 if (dspi->irq)
1155 free_irq(dspi->irq, dspi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001156out_clk_put:
1157 clk_disable_unprepare(dspi->clk);
David Brazdil0f672f62019-12-10 10:32:29 +00001158out_ctlr_put:
1159 spi_controller_put(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160
1161 return ret;
1162}
1163
1164static int dspi_remove(struct platform_device *pdev)
1165{
David Brazdil0f672f62019-12-10 10:32:29 +00001166 struct spi_controller *ctlr = platform_get_drvdata(pdev);
1167 struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001168
1169 /* Disconnect from the SPI framework */
David Brazdil0f672f62019-12-10 10:32:29 +00001170 spi_unregister_controller(dspi->ctlr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171
Olivier Deprez0e641232021-09-23 10:07:05 +02001172 /* Disable RX and TX */
1173 regmap_update_bits(dspi->regmap, SPI_MCR,
1174 SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF,
1175 SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF);
1176
1177 /* Stop Running */
1178 regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT);
1179
1180 dspi_release_dma(dspi);
1181 if (dspi->irq)
1182 free_irq(dspi->irq, dspi);
1183 clk_disable_unprepare(dspi->clk);
1184
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001185 return 0;
1186}
1187
Olivier Deprez0e641232021-09-23 10:07:05 +02001188static void dspi_shutdown(struct platform_device *pdev)
1189{
1190 dspi_remove(pdev);
1191}
1192
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001193static struct platform_driver fsl_dspi_driver = {
David Brazdil0f672f62019-12-10 10:32:29 +00001194 .driver.name = DRIVER_NAME,
1195 .driver.of_match_table = fsl_dspi_dt_ids,
1196 .driver.owner = THIS_MODULE,
1197 .driver.pm = &dspi_pm,
1198 .probe = dspi_probe,
1199 .remove = dspi_remove,
Olivier Deprez0e641232021-09-23 10:07:05 +02001200 .shutdown = dspi_shutdown,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001201};
1202module_platform_driver(fsl_dspi_driver);
1203
1204MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1205MODULE_LICENSE("GPL");
1206MODULE_ALIAS("platform:" DRIVER_NAME);