blob: befabddf897a4b87c21f8fce6b65a96f7db26ae2 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * drivers/spi/spi-fsl-dspi.c
3 *
4 * Copyright 2013 Freescale Semiconductor, Inc.
5 *
6 * Freescale DSPI driver
7 * This file contains a driver for the Freescale DSPI
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/dmaengine.h>
19#include <linux/dma-mapping.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/kernel.h>
25#include <linux/math64.h>
26#include <linux/module.h>
27#include <linux/of.h>
28#include <linux/of_device.h>
29#include <linux/pinctrl/consumer.h>
30#include <linux/platform_device.h>
31#include <linux/pm_runtime.h>
32#include <linux/regmap.h>
33#include <linux/sched.h>
34#include <linux/spi/spi.h>
35#include <linux/spi/spi_bitbang.h>
36#include <linux/time.h>
37
38#define DRIVER_NAME "fsl-dspi"
39
40#define TRAN_STATE_RX_VOID 0x01
41#define TRAN_STATE_TX_VOID 0x02
42#define TRAN_STATE_WORD_ODD_NUM 0x04
43
44#define DSPI_FIFO_SIZE 4
45#define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
46
47#define SPI_MCR 0x00
48#define SPI_MCR_MASTER (1 << 31)
49#define SPI_MCR_PCSIS (0x3F << 16)
50#define SPI_MCR_CLR_TXF (1 << 11)
51#define SPI_MCR_CLR_RXF (1 << 10)
52#define SPI_MCR_DIS_TXF (1 << 13)
53#define SPI_MCR_DIS_RXF (1 << 12)
54#define SPI_MCR_HALT (1 << 0)
55
56#define SPI_TCR 0x08
57#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
58
59#define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
60#define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
61#define SPI_CTAR_CPOL(x) ((x) << 26)
62#define SPI_CTAR_CPHA(x) ((x) << 25)
63#define SPI_CTAR_LSBFE(x) ((x) << 24)
64#define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
65#define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
66#define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
67#define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
68#define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
69#define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
70#define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
71#define SPI_CTAR_BR(x) ((x) & 0x0000000f)
72#define SPI_CTAR_SCALE_BITS 0xf
73
74#define SPI_CTAR0_SLAVE 0x0c
75
76#define SPI_SR 0x2c
77#define SPI_SR_EOQF 0x10000000
78#define SPI_SR_TCFQF 0x80000000
79#define SPI_SR_CLEAR 0x9aaf0000
80
81#define SPI_RSER_TFFFE BIT(25)
82#define SPI_RSER_TFFFD BIT(24)
83#define SPI_RSER_RFDFE BIT(17)
84#define SPI_RSER_RFDFD BIT(16)
85
86#define SPI_RSER 0x30
87#define SPI_RSER_EOQFE 0x10000000
88#define SPI_RSER_TCFQE 0x80000000
89
90#define SPI_PUSHR 0x34
91#define SPI_PUSHR_CONT (1 << 31)
92#define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
93#define SPI_PUSHR_EOQ (1 << 27)
94#define SPI_PUSHR_CTCNT (1 << 26)
95#define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
96#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
97
98#define SPI_PUSHR_SLAVE 0x34
99
100#define SPI_POPR 0x38
101#define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
102
103#define SPI_TXFR0 0x3c
104#define SPI_TXFR1 0x40
105#define SPI_TXFR2 0x44
106#define SPI_TXFR3 0x48
107#define SPI_RXFR0 0x7c
108#define SPI_RXFR1 0x80
109#define SPI_RXFR2 0x84
110#define SPI_RXFR3 0x88
111
112#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
113#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
114#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
115#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
116
117#define SPI_CS_INIT 0x01
118#define SPI_CS_ASSERT 0x02
119#define SPI_CS_DROP 0x04
120
121#define SPI_TCR_TCNT_MAX 0x10000
122
123#define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
124
125struct chip_data {
126 u32 mcr_val;
127 u32 ctar_val;
128 u16 void_write_data;
129};
130
131enum dspi_trans_mode {
132 DSPI_EOQ_MODE = 0,
133 DSPI_TCFQ_MODE,
134 DSPI_DMA_MODE,
135};
136
137struct fsl_dspi_devtype_data {
138 enum dspi_trans_mode trans_mode;
139 u8 max_clock_factor;
140};
141
142static const struct fsl_dspi_devtype_data vf610_data = {
143 .trans_mode = DSPI_DMA_MODE,
144 .max_clock_factor = 2,
145};
146
147static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
148 .trans_mode = DSPI_TCFQ_MODE,
149 .max_clock_factor = 8,
150};
151
152static const struct fsl_dspi_devtype_data ls2085a_data = {
153 .trans_mode = DSPI_TCFQ_MODE,
154 .max_clock_factor = 8,
155};
156
157struct fsl_dspi_dma {
158 /* Length of transfer in words of DSPI_FIFO_SIZE */
159 u32 curr_xfer_len;
160
161 u32 *tx_dma_buf;
162 struct dma_chan *chan_tx;
163 dma_addr_t tx_dma_phys;
164 struct completion cmd_tx_complete;
165 struct dma_async_tx_descriptor *tx_desc;
166
167 u32 *rx_dma_buf;
168 struct dma_chan *chan_rx;
169 dma_addr_t rx_dma_phys;
170 struct completion cmd_rx_complete;
171 struct dma_async_tx_descriptor *rx_desc;
172};
173
174struct fsl_dspi {
175 struct spi_master *master;
176 struct platform_device *pdev;
177
178 struct regmap *regmap;
179 int irq;
180 struct clk *clk;
181
182 struct spi_transfer *cur_transfer;
183 struct spi_message *cur_msg;
184 struct chip_data *cur_chip;
185 size_t len;
186 void *tx;
187 void *tx_end;
188 void *rx;
189 void *rx_end;
190 char dataflags;
191 u8 cs;
192 u16 void_write_data;
193 u32 cs_change;
194 const struct fsl_dspi_devtype_data *devtype_data;
195
196 wait_queue_head_t waitq;
197 u32 waitflags;
198
199 u32 spi_tcnt;
200 struct fsl_dspi_dma *dma;
201};
202
203static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word);
204
205static inline int is_double_byte_mode(struct fsl_dspi *dspi)
206{
207 unsigned int val;
208
209 regmap_read(dspi->regmap, SPI_CTAR(0), &val);
210
211 return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
212}
213
214static void dspi_tx_dma_callback(void *arg)
215{
216 struct fsl_dspi *dspi = arg;
217 struct fsl_dspi_dma *dma = dspi->dma;
218
219 complete(&dma->cmd_tx_complete);
220}
221
222static void dspi_rx_dma_callback(void *arg)
223{
224 struct fsl_dspi *dspi = arg;
225 struct fsl_dspi_dma *dma = dspi->dma;
226 int rx_word;
227 int i;
228 u16 d;
229
230 rx_word = is_double_byte_mode(dspi);
231
232 if (!(dspi->dataflags & TRAN_STATE_RX_VOID)) {
233 for (i = 0; i < dma->curr_xfer_len; i++) {
234 d = dspi->dma->rx_dma_buf[i];
235 rx_word ? (*(u16 *)dspi->rx = d) :
236 (*(u8 *)dspi->rx = d);
237 dspi->rx += rx_word + 1;
238 }
239 }
240
241 complete(&dma->cmd_rx_complete);
242}
243
244static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
245{
246 struct fsl_dspi_dma *dma = dspi->dma;
247 struct device *dev = &dspi->pdev->dev;
248 int time_left;
249 int tx_word;
250 int i;
251
252 tx_word = is_double_byte_mode(dspi);
253
254 for (i = 0; i < dma->curr_xfer_len; i++) {
255 dspi->dma->tx_dma_buf[i] = dspi_data_to_pushr(dspi, tx_word);
256 if ((dspi->cs_change) && (!dspi->len))
257 dspi->dma->tx_dma_buf[i] &= ~SPI_PUSHR_CONT;
258 }
259
260 dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
261 dma->tx_dma_phys,
262 dma->curr_xfer_len *
263 DMA_SLAVE_BUSWIDTH_4_BYTES,
264 DMA_MEM_TO_DEV,
265 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
266 if (!dma->tx_desc) {
267 dev_err(dev, "Not able to get desc for DMA xfer\n");
268 return -EIO;
269 }
270
271 dma->tx_desc->callback = dspi_tx_dma_callback;
272 dma->tx_desc->callback_param = dspi;
273 if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
274 dev_err(dev, "DMA submit failed\n");
275 return -EINVAL;
276 }
277
278 dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
279 dma->rx_dma_phys,
280 dma->curr_xfer_len *
281 DMA_SLAVE_BUSWIDTH_4_BYTES,
282 DMA_DEV_TO_MEM,
283 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
284 if (!dma->rx_desc) {
285 dev_err(dev, "Not able to get desc for DMA xfer\n");
286 return -EIO;
287 }
288
289 dma->rx_desc->callback = dspi_rx_dma_callback;
290 dma->rx_desc->callback_param = dspi;
291 if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
292 dev_err(dev, "DMA submit failed\n");
293 return -EINVAL;
294 }
295
296 reinit_completion(&dspi->dma->cmd_rx_complete);
297 reinit_completion(&dspi->dma->cmd_tx_complete);
298
299 dma_async_issue_pending(dma->chan_rx);
300 dma_async_issue_pending(dma->chan_tx);
301
302 time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
303 DMA_COMPLETION_TIMEOUT);
304 if (time_left == 0) {
305 dev_err(dev, "DMA tx timeout\n");
306 dmaengine_terminate_all(dma->chan_tx);
307 dmaengine_terminate_all(dma->chan_rx);
308 return -ETIMEDOUT;
309 }
310
311 time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
312 DMA_COMPLETION_TIMEOUT);
313 if (time_left == 0) {
314 dev_err(dev, "DMA rx timeout\n");
315 dmaengine_terminate_all(dma->chan_tx);
316 dmaengine_terminate_all(dma->chan_rx);
317 return -ETIMEDOUT;
318 }
319
320 return 0;
321}
322
323static int dspi_dma_xfer(struct fsl_dspi *dspi)
324{
325 struct fsl_dspi_dma *dma = dspi->dma;
326 struct device *dev = &dspi->pdev->dev;
327 int curr_remaining_bytes;
328 int bytes_per_buffer;
329 int word = 1;
330 int ret = 0;
331
332 if (is_double_byte_mode(dspi))
333 word = 2;
334 curr_remaining_bytes = dspi->len;
335 bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
336 while (curr_remaining_bytes) {
337 /* Check if current transfer fits the DMA buffer */
338 dma->curr_xfer_len = curr_remaining_bytes / word;
339 if (dma->curr_xfer_len > bytes_per_buffer)
340 dma->curr_xfer_len = bytes_per_buffer;
341
342 ret = dspi_next_xfer_dma_submit(dspi);
343 if (ret) {
344 dev_err(dev, "DMA transfer failed\n");
345 goto exit;
346
347 } else {
348 curr_remaining_bytes -= dma->curr_xfer_len * word;
349 if (curr_remaining_bytes < 0)
350 curr_remaining_bytes = 0;
351 }
352 }
353
354exit:
355 return ret;
356}
357
358static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
359{
360 struct fsl_dspi_dma *dma;
361 struct dma_slave_config cfg;
362 struct device *dev = &dspi->pdev->dev;
363 int ret;
364
365 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
366 if (!dma)
367 return -ENOMEM;
368
369 dma->chan_rx = dma_request_slave_channel(dev, "rx");
370 if (!dma->chan_rx) {
371 dev_err(dev, "rx dma channel not available\n");
372 ret = -ENODEV;
373 return ret;
374 }
375
376 dma->chan_tx = dma_request_slave_channel(dev, "tx");
377 if (!dma->chan_tx) {
378 dev_err(dev, "tx dma channel not available\n");
379 ret = -ENODEV;
380 goto err_tx_channel;
381 }
382
383 dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
384 &dma->tx_dma_phys, GFP_KERNEL);
385 if (!dma->tx_dma_buf) {
386 ret = -ENOMEM;
387 goto err_tx_dma_buf;
388 }
389
390 dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
391 &dma->rx_dma_phys, GFP_KERNEL);
392 if (!dma->rx_dma_buf) {
393 ret = -ENOMEM;
394 goto err_rx_dma_buf;
395 }
396
397 cfg.src_addr = phy_addr + SPI_POPR;
398 cfg.dst_addr = phy_addr + SPI_PUSHR;
399 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
400 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
401 cfg.src_maxburst = 1;
402 cfg.dst_maxburst = 1;
403
404 cfg.direction = DMA_DEV_TO_MEM;
405 ret = dmaengine_slave_config(dma->chan_rx, &cfg);
406 if (ret) {
407 dev_err(dev, "can't configure rx dma channel\n");
408 ret = -EINVAL;
409 goto err_slave_config;
410 }
411
412 cfg.direction = DMA_MEM_TO_DEV;
413 ret = dmaengine_slave_config(dma->chan_tx, &cfg);
414 if (ret) {
415 dev_err(dev, "can't configure tx dma channel\n");
416 ret = -EINVAL;
417 goto err_slave_config;
418 }
419
420 dspi->dma = dma;
421 init_completion(&dma->cmd_tx_complete);
422 init_completion(&dma->cmd_rx_complete);
423
424 return 0;
425
426err_slave_config:
427 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
428 dma->rx_dma_buf, dma->rx_dma_phys);
429err_rx_dma_buf:
430 dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
431 dma->tx_dma_buf, dma->tx_dma_phys);
432err_tx_dma_buf:
433 dma_release_channel(dma->chan_tx);
434err_tx_channel:
435 dma_release_channel(dma->chan_rx);
436
437 devm_kfree(dev, dma);
438 dspi->dma = NULL;
439
440 return ret;
441}
442
443static void dspi_release_dma(struct fsl_dspi *dspi)
444{
445 struct fsl_dspi_dma *dma = dspi->dma;
446 struct device *dev = &dspi->pdev->dev;
447
448 if (dma) {
449 if (dma->chan_tx) {
450 dma_unmap_single(dev, dma->tx_dma_phys,
451 DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
452 dma_release_channel(dma->chan_tx);
453 }
454
455 if (dma->chan_rx) {
456 dma_unmap_single(dev, dma->rx_dma_phys,
457 DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
458 dma_release_channel(dma->chan_rx);
459 }
460 }
461}
462
463static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
464 unsigned long clkrate)
465{
466 /* Valid baud rate pre-scaler values */
467 int pbr_tbl[4] = {2, 3, 5, 7};
468 int brs[16] = { 2, 4, 6, 8,
469 16, 32, 64, 128,
470 256, 512, 1024, 2048,
471 4096, 8192, 16384, 32768 };
472 int scale_needed, scale, minscale = INT_MAX;
473 int i, j;
474
475 scale_needed = clkrate / speed_hz;
476 if (clkrate % speed_hz)
477 scale_needed++;
478
479 for (i = 0; i < ARRAY_SIZE(brs); i++)
480 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
481 scale = brs[i] * pbr_tbl[j];
482 if (scale >= scale_needed) {
483 if (scale < minscale) {
484 minscale = scale;
485 *br = i;
486 *pbr = j;
487 }
488 break;
489 }
490 }
491
492 if (minscale == INT_MAX) {
493 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
494 speed_hz, clkrate);
495 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
496 *br = ARRAY_SIZE(brs) - 1;
497 }
498}
499
500static void ns_delay_scale(char *psc, char *sc, int delay_ns,
501 unsigned long clkrate)
502{
503 int pscale_tbl[4] = {1, 3, 5, 7};
504 int scale_needed, scale, minscale = INT_MAX;
505 int i, j;
506 u32 remainder;
507
508 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
509 &remainder);
510 if (remainder)
511 scale_needed++;
512
513 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
514 for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
515 scale = pscale_tbl[i] * (2 << j);
516 if (scale >= scale_needed) {
517 if (scale < minscale) {
518 minscale = scale;
519 *psc = i;
520 *sc = j;
521 }
522 break;
523 }
524 }
525
526 if (minscale == INT_MAX) {
527 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
528 delay_ns, clkrate);
529 *psc = ARRAY_SIZE(pscale_tbl) - 1;
530 *sc = SPI_CTAR_SCALE_BITS;
531 }
532}
533
534static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
535{
536 u16 d16;
537
538 if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
539 d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
540 else
541 d16 = dspi->void_write_data;
542
543 dspi->tx += tx_word + 1;
544 dspi->len -= tx_word + 1;
545
546 return SPI_PUSHR_TXDATA(d16) |
547 SPI_PUSHR_PCS(dspi->cs) |
548 SPI_PUSHR_CTAS(0) |
549 SPI_PUSHR_CONT;
550}
551
552static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
553{
554 u16 d;
555 unsigned int val;
556
557 regmap_read(dspi->regmap, SPI_POPR, &val);
558 d = SPI_POPR_RXDATA(val);
559
560 if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
561 rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
562
563 dspi->rx += rx_word + 1;
564}
565
566static int dspi_eoq_write(struct fsl_dspi *dspi)
567{
568 int tx_count = 0;
569 int tx_word;
570 u32 dspi_pushr = 0;
571
572 tx_word = is_double_byte_mode(dspi);
573
574 while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
575 /* If we are in word mode, only have a single byte to transfer
576 * switch to byte mode temporarily. Will switch back at the
577 * end of the transfer.
578 */
579 if (tx_word && (dspi->len == 1)) {
580 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
581 regmap_update_bits(dspi->regmap, SPI_CTAR(0),
582 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
583 tx_word = 0;
584 }
585
586 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
587
588 if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
589 /* last transfer in the transfer */
590 dspi_pushr |= SPI_PUSHR_EOQ;
591 if ((dspi->cs_change) && (!dspi->len))
592 dspi_pushr &= ~SPI_PUSHR_CONT;
593 } else if (tx_word && (dspi->len == 1))
594 dspi_pushr |= SPI_PUSHR_EOQ;
595
596 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
597
598 tx_count++;
599 }
600
601 return tx_count * (tx_word + 1);
602}
603
604static int dspi_eoq_read(struct fsl_dspi *dspi)
605{
606 int rx_count = 0;
607 int rx_word = is_double_byte_mode(dspi);
608
609 while ((dspi->rx < dspi->rx_end)
610 && (rx_count < DSPI_FIFO_SIZE)) {
611 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
612 rx_word = 0;
613
614 dspi_data_from_popr(dspi, rx_word);
615 rx_count++;
616 }
617
618 return rx_count;
619}
620
621static int dspi_tcfq_write(struct fsl_dspi *dspi)
622{
623 int tx_word;
624 u32 dspi_pushr = 0;
625
626 tx_word = is_double_byte_mode(dspi);
627
628 if (tx_word && (dspi->len == 1)) {
629 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
630 regmap_update_bits(dspi->regmap, SPI_CTAR(0),
631 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
632 tx_word = 0;
633 }
634
635 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
636
637 if ((dspi->cs_change) && (!dspi->len))
638 dspi_pushr &= ~SPI_PUSHR_CONT;
639
640 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
641
642 return tx_word + 1;
643}
644
645static void dspi_tcfq_read(struct fsl_dspi *dspi)
646{
647 int rx_word = is_double_byte_mode(dspi);
648
649 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
650 rx_word = 0;
651
652 dspi_data_from_popr(dspi, rx_word);
653}
654
655static int dspi_transfer_one_message(struct spi_master *master,
656 struct spi_message *message)
657{
658 struct fsl_dspi *dspi = spi_master_get_devdata(master);
659 struct spi_device *spi = message->spi;
660 struct spi_transfer *transfer;
661 int status = 0;
662 enum dspi_trans_mode trans_mode;
663 u32 spi_tcr;
664
665 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
666 dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
667
668 message->actual_length = 0;
669
670 list_for_each_entry(transfer, &message->transfers, transfer_list) {
671 dspi->cur_transfer = transfer;
672 dspi->cur_msg = message;
673 dspi->cur_chip = spi_get_ctldata(spi);
674 dspi->cs = spi->chip_select;
675 dspi->cs_change = 0;
676 if (list_is_last(&dspi->cur_transfer->transfer_list,
677 &dspi->cur_msg->transfers) || transfer->cs_change)
678 dspi->cs_change = 1;
679 dspi->void_write_data = dspi->cur_chip->void_write_data;
680
681 dspi->dataflags = 0;
682 dspi->tx = (void *)transfer->tx_buf;
683 dspi->tx_end = dspi->tx + transfer->len;
684 dspi->rx = transfer->rx_buf;
685 dspi->rx_end = dspi->rx + transfer->len;
686 dspi->len = transfer->len;
687
688 if (!dspi->rx)
689 dspi->dataflags |= TRAN_STATE_RX_VOID;
690
691 if (!dspi->tx)
692 dspi->dataflags |= TRAN_STATE_TX_VOID;
693
694 regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
695 regmap_update_bits(dspi->regmap, SPI_MCR,
696 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
697 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
698 regmap_write(dspi->regmap, SPI_CTAR(0),
699 dspi->cur_chip->ctar_val);
700
701 trans_mode = dspi->devtype_data->trans_mode;
702 switch (trans_mode) {
703 case DSPI_EOQ_MODE:
704 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
705 dspi_eoq_write(dspi);
706 break;
707 case DSPI_TCFQ_MODE:
708 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
709 dspi_tcfq_write(dspi);
710 break;
711 case DSPI_DMA_MODE:
712 regmap_write(dspi->regmap, SPI_RSER,
713 SPI_RSER_TFFFE | SPI_RSER_TFFFD |
714 SPI_RSER_RFDFE | SPI_RSER_RFDFD);
715 status = dspi_dma_xfer(dspi);
716 break;
717 default:
718 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
719 trans_mode);
720 status = -EINVAL;
721 goto out;
722 }
723
724 if (trans_mode != DSPI_DMA_MODE) {
725 if (wait_event_interruptible(dspi->waitq,
726 dspi->waitflags))
727 dev_err(&dspi->pdev->dev,
728 "wait transfer complete fail!\n");
729 dspi->waitflags = 0;
730 }
731
732 if (transfer->delay_usecs)
733 udelay(transfer->delay_usecs);
734 }
735
736out:
737 message->status = status;
738 spi_finalize_current_message(master);
739
740 return status;
741}
742
743static int dspi_setup(struct spi_device *spi)
744{
745 struct chip_data *chip;
746 struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
747 u32 cs_sck_delay = 0, sck_cs_delay = 0;
748 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
749 unsigned char pasc = 0, asc = 0, fmsz = 0;
750 unsigned long clkrate;
751
752 if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
753 fmsz = spi->bits_per_word - 1;
754 } else {
755 pr_err("Invalid wordsize\n");
756 return -ENODEV;
757 }
758
759 /* Only alloc on first setup */
760 chip = spi_get_ctldata(spi);
761 if (chip == NULL) {
762 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
763 if (!chip)
764 return -ENOMEM;
765 }
766
767 of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
768 &cs_sck_delay);
769
770 of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
771 &sck_cs_delay);
772
773 chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
774 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
775
776 chip->void_write_data = 0;
777
778 clkrate = clk_get_rate(dspi->clk);
779 hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
780
781 /* Set PCS to SCK delay scale values */
782 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
783
784 /* Set After SCK delay scale values */
785 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
786
787 chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
788 | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
789 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
790 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
791 | SPI_CTAR_PCSSCK(pcssck)
792 | SPI_CTAR_CSSCK(cssck)
793 | SPI_CTAR_PASC(pasc)
794 | SPI_CTAR_ASC(asc)
795 | SPI_CTAR_PBR(pbr)
796 | SPI_CTAR_BR(br);
797
798 spi_set_ctldata(spi, chip);
799
800 return 0;
801}
802
803static void dspi_cleanup(struct spi_device *spi)
804{
805 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
806
807 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
808 spi->master->bus_num, spi->chip_select);
809
810 kfree(chip);
811}
812
813static irqreturn_t dspi_interrupt(int irq, void *dev_id)
814{
815 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
816 struct spi_message *msg = dspi->cur_msg;
817 enum dspi_trans_mode trans_mode;
818 u32 spi_sr, spi_tcr;
819 u32 spi_tcnt, tcnt_diff;
820 int tx_word;
821
822 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
823 regmap_write(dspi->regmap, SPI_SR, spi_sr);
824
825
826 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
827 tx_word = is_double_byte_mode(dspi);
828
829 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
830 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
831 /*
832 * The width of SPI Transfer Counter in SPI_TCR is 16bits,
833 * so the max couner is 65535. When the counter reach 65535,
834 * it will wrap around, counter reset to zero.
835 * spi_tcnt my be less than dspi->spi_tcnt, it means the
836 * counter already wrapped around.
837 * SPI Transfer Counter is a counter of transmitted frames.
838 * The size of frame maybe two bytes.
839 */
840 tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
841 % SPI_TCR_TCNT_MAX;
842 tcnt_diff *= (tx_word + 1);
843 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
844 tcnt_diff--;
845
846 msg->actual_length += tcnt_diff;
847
848 dspi->spi_tcnt = spi_tcnt;
849
850 trans_mode = dspi->devtype_data->trans_mode;
851 switch (trans_mode) {
852 case DSPI_EOQ_MODE:
853 dspi_eoq_read(dspi);
854 break;
855 case DSPI_TCFQ_MODE:
856 dspi_tcfq_read(dspi);
857 break;
858 default:
859 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
860 trans_mode);
861 return IRQ_HANDLED;
862 }
863
864 if (!dspi->len) {
865 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
866 regmap_update_bits(dspi->regmap,
867 SPI_CTAR(0),
868 SPI_FRAME_BITS_MASK,
869 SPI_FRAME_BITS(16));
870 dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
871 }
872
873 dspi->waitflags = 1;
874 wake_up_interruptible(&dspi->waitq);
875 } else {
876 switch (trans_mode) {
877 case DSPI_EOQ_MODE:
878 dspi_eoq_write(dspi);
879 break;
880 case DSPI_TCFQ_MODE:
881 dspi_tcfq_write(dspi);
882 break;
883 default:
884 dev_err(&dspi->pdev->dev,
885 "unsupported trans_mode %u\n",
886 trans_mode);
887 }
888 }
889
890 return IRQ_HANDLED;
891 }
892
893 return IRQ_NONE;
894}
895
896static const struct of_device_id fsl_dspi_dt_ids[] = {
897 { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
898 { .compatible = "fsl,ls1021a-v1.0-dspi",
899 .data = (void *)&ls1021a_v1_data, },
900 { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
901 { /* sentinel */ }
902};
903MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
904
905#ifdef CONFIG_PM_SLEEP
906static int dspi_suspend(struct device *dev)
907{
908 struct spi_master *master = dev_get_drvdata(dev);
909 struct fsl_dspi *dspi = spi_master_get_devdata(master);
910
911 if (dspi->irq)
912 disable_irq(dspi->irq);
913 spi_master_suspend(master);
914 clk_disable_unprepare(dspi->clk);
915
916 pinctrl_pm_select_sleep_state(dev);
917
918 return 0;
919}
920
921static int dspi_resume(struct device *dev)
922{
923 struct spi_master *master = dev_get_drvdata(dev);
924 struct fsl_dspi *dspi = spi_master_get_devdata(master);
925 int ret;
926
927 pinctrl_pm_select_default_state(dev);
928
929 ret = clk_prepare_enable(dspi->clk);
930 if (ret)
931 return ret;
932 spi_master_resume(master);
933 if (dspi->irq)
934 enable_irq(dspi->irq);
935
936 return 0;
937}
938#endif /* CONFIG_PM_SLEEP */
939
940static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
941
942static const struct regmap_config dspi_regmap_config = {
943 .reg_bits = 32,
944 .val_bits = 32,
945 .reg_stride = 4,
946 .max_register = 0x88,
947};
948
949static void dspi_init(struct fsl_dspi *dspi)
950{
951 regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
952}
953
954static int dspi_probe(struct platform_device *pdev)
955{
956 struct device_node *np = pdev->dev.of_node;
957 struct spi_master *master;
958 struct fsl_dspi *dspi;
959 struct resource *res;
960 void __iomem *base;
961 int ret = 0, cs_num, bus_num;
962
963 master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
964 if (!master)
965 return -ENOMEM;
966
967 dspi = spi_master_get_devdata(master);
968 dspi->pdev = pdev;
969 dspi->master = master;
970
971 master->transfer = NULL;
972 master->setup = dspi_setup;
973 master->transfer_one_message = dspi_transfer_one_message;
974 master->dev.of_node = pdev->dev.of_node;
975
976 master->cleanup = dspi_cleanup;
977 master->mode_bits = SPI_CPOL | SPI_CPHA;
978 master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
979 SPI_BPW_MASK(16);
980
981 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
982 if (ret < 0) {
983 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
984 goto out_master_put;
985 }
986 master->num_chipselect = cs_num;
987
988 ret = of_property_read_u32(np, "bus-num", &bus_num);
989 if (ret < 0) {
990 dev_err(&pdev->dev, "can't get bus-num\n");
991 goto out_master_put;
992 }
993 master->bus_num = bus_num;
994
995 dspi->devtype_data = of_device_get_match_data(&pdev->dev);
996 if (!dspi->devtype_data) {
997 dev_err(&pdev->dev, "can't get devtype_data\n");
998 ret = -EFAULT;
999 goto out_master_put;
1000 }
1001
1002 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1003 base = devm_ioremap_resource(&pdev->dev, res);
1004 if (IS_ERR(base)) {
1005 ret = PTR_ERR(base);
1006 goto out_master_put;
1007 }
1008
1009 dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
1010 &dspi_regmap_config);
1011 if (IS_ERR(dspi->regmap)) {
1012 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
1013 PTR_ERR(dspi->regmap));
1014 ret = PTR_ERR(dspi->regmap);
1015 goto out_master_put;
1016 }
1017
1018 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
1019 if (IS_ERR(dspi->clk)) {
1020 ret = PTR_ERR(dspi->clk);
1021 dev_err(&pdev->dev, "unable to get clock\n");
1022 goto out_master_put;
1023 }
1024 ret = clk_prepare_enable(dspi->clk);
1025 if (ret)
1026 goto out_master_put;
1027
1028 dspi_init(dspi);
1029 dspi->irq = platform_get_irq(pdev, 0);
1030 if (dspi->irq < 0) {
1031 dev_err(&pdev->dev, "can't get platform irq\n");
1032 ret = dspi->irq;
1033 goto out_clk_put;
1034 }
1035
1036 ret = request_threaded_irq(dspi->irq, dspi_interrupt, NULL,
1037 IRQF_SHARED, pdev->name, dspi);
1038 if (ret < 0) {
1039 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1040 goto out_clk_put;
1041 }
1042
1043 if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
1044 ret = dspi_request_dma(dspi, res->start);
1045 if (ret < 0) {
1046 dev_err(&pdev->dev, "can't get dma channels\n");
1047 goto out_free_irq;
1048 }
1049 }
1050
1051 master->max_speed_hz =
1052 clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
1053
1054 init_waitqueue_head(&dspi->waitq);
1055 platform_set_drvdata(pdev, master);
1056
1057 ret = spi_register_master(master);
1058 if (ret != 0) {
1059 dev_err(&pdev->dev, "Problem registering DSPI master\n");
1060 goto out_free_irq;
1061 }
1062
1063 return ret;
1064
1065out_free_irq:
1066 if (dspi->irq)
1067 free_irq(dspi->irq, dspi);
1068out_clk_put:
1069 clk_disable_unprepare(dspi->clk);
1070out_master_put:
1071 spi_master_put(master);
1072
1073 return ret;
1074}
1075
1076static int dspi_remove(struct platform_device *pdev)
1077{
1078 struct spi_master *master = platform_get_drvdata(pdev);
1079 struct fsl_dspi *dspi = spi_master_get_devdata(master);
1080
1081 /* Disconnect from the SPI framework */
1082 spi_unregister_controller(dspi->master);
1083
1084 /* Disable RX and TX */
1085 regmap_update_bits(dspi->regmap, SPI_MCR,
1086 SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF,
1087 SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF);
1088
1089 /* Stop Running */
1090 regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT);
1091
1092 dspi_release_dma(dspi);
1093 if (dspi->irq)
1094 free_irq(dspi->irq, dspi);
1095 clk_disable_unprepare(dspi->clk);
1096
1097 return 0;
1098}
1099
1100static void dspi_shutdown(struct platform_device *pdev)
1101{
1102 dspi_remove(pdev);
1103}
1104
1105static struct platform_driver fsl_dspi_driver = {
1106 .driver.name = DRIVER_NAME,
1107 .driver.of_match_table = fsl_dspi_dt_ids,
1108 .driver.owner = THIS_MODULE,
1109 .driver.pm = &dspi_pm,
1110 .probe = dspi_probe,
1111 .remove = dspi_remove,
1112 .shutdown = dspi_shutdown,
1113};
1114module_platform_driver(fsl_dspi_driver);
1115
1116MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1117MODULE_LICENSE("GPL");
1118MODULE_ALIAS("platform:" DRIVER_NAME);