1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright 2013 Freescale Semiconductor, Inc.
6 // Freescale DSPI driver
7 // This file contains a driver for the Freescale DSPI
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regmap.h>
26 #include <linux/sched.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/spi-fsl-dspi.h>
29 #include <linux/spi/spi_bitbang.h>
30 #include <linux/time.h>
32 #define DRIVER_NAME "fsl-dspi"
35 #define DSPI_FIFO_SIZE 16
37 #define DSPI_FIFO_SIZE 4
39 #define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
42 #define SPI_MCR_MASTER (1 << 31)
43 #define SPI_MCR_PCSIS (0x3F << 16)
44 #define SPI_MCR_CLR_TXF (1 << 11)
45 #define SPI_MCR_CLR_RXF (1 << 10)
46 #define SPI_MCR_XSPI (1 << 3)
47 #define SPI_MCR_DIS_TXF (1 << 13)
48 #define SPI_MCR_DIS_RXF (1 << 12)
49 #define SPI_MCR_HALT (1 << 0)
52 #define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
54 #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
55 #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
56 #define SPI_CTAR_CPOL(x) ((x) << 26)
57 #define SPI_CTAR_CPHA(x) ((x) << 25)
58 #define SPI_CTAR_LSBFE(x) ((x) << 24)
59 #define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
60 #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
61 #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
62 #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
63 #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
64 #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
65 #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
66 #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
67 #define SPI_CTAR_SCALE_BITS 0xf
69 #define SPI_CTAR0_SLAVE 0x0c
72 #define SPI_SR_EOQF 0x10000000
73 #define SPI_SR_TCFQF 0x80000000
74 #define SPI_SR_CLEAR 0xdaad0000
76 #define SPI_RSER_TFFFE BIT(25)
77 #define SPI_RSER_TFFFD BIT(24)
78 #define SPI_RSER_RFDFE BIT(17)
79 #define SPI_RSER_RFDFD BIT(16)
82 #define SPI_RSER_EOQFE 0x10000000
83 #define SPI_RSER_TCFQE 0x80000000
85 #define SPI_PUSHR 0x34
86 #define SPI_PUSHR_CMD_CONT (1 << 15)
87 #define SPI_PUSHR_CONT (SPI_PUSHR_CMD_CONT << 16)
88 #define SPI_PUSHR_CMD_CTAS(x) (((x) & 0x0003) << 12)
89 #define SPI_PUSHR_CTAS(x) (SPI_PUSHR_CMD_CTAS(x) << 16)
90 #define SPI_PUSHR_CMD_EOQ (1 << 11)
91 #define SPI_PUSHR_EOQ (SPI_PUSHR_CMD_EOQ << 16)
92 #define SPI_PUSHR_CMD_CTCNT (1 << 10)
93 #define SPI_PUSHR_CTCNT (SPI_PUSHR_CMD_CTCNT << 16)
94 #define SPI_PUSHR_CMD_PCS(x) ((1 << x) & 0x003f)
95 #define SPI_PUSHR_PCS(x) (SPI_PUSHR_CMD_PCS(x) << 16)
96 #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
98 #define SPI_PUSHR_SLAVE 0x34
100 #define SPI_POPR 0x38
101 #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
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
112 #define SPI_CTARE(x) (0x11c + (((x) & 0x3) * 4))
113 #define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
114 #define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
116 #define SPI_SREX 0x13c
118 #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
119 #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
120 #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
121 #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
123 #define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
124 #define SPI_FRAME_EBITS_MASK SPI_CTARE_FMSZE(1)
126 /* Register offsets for regmap_pushr */
127 #define PUSHR_CMD 0x0
130 #define SPI_CS_INIT 0x01
131 #define SPI_CS_ASSERT 0x02
132 #define SPI_CS_DROP 0x04
134 #define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
141 enum dspi_trans_mode
{
147 struct fsl_dspi_devtype_data
{
148 enum dspi_trans_mode trans_mode
;
153 static const struct fsl_dspi_devtype_data vf610_data
= {
154 .trans_mode
= DSPI_DMA_MODE
,
155 .max_clock_factor
= 2,
158 static const struct fsl_dspi_devtype_data ls1021a_v1_data
= {
159 .trans_mode
= DSPI_TCFQ_MODE
,
160 .max_clock_factor
= 8,
164 static const struct fsl_dspi_devtype_data ls2085a_data
= {
165 .trans_mode
= DSPI_TCFQ_MODE
,
166 .max_clock_factor
= 8,
169 static const struct fsl_dspi_devtype_data coldfire_data
= {
170 .trans_mode
= DSPI_EOQ_MODE
,
171 .max_clock_factor
= 8,
174 struct fsl_dspi_dma
{
175 /* Length of transfer in words of DSPI_FIFO_SIZE */
179 struct dma_chan
*chan_tx
;
180 dma_addr_t tx_dma_phys
;
181 struct completion cmd_tx_complete
;
182 struct dma_async_tx_descriptor
*tx_desc
;
185 struct dma_chan
*chan_rx
;
186 dma_addr_t rx_dma_phys
;
187 struct completion cmd_rx_complete
;
188 struct dma_async_tx_descriptor
*rx_desc
;
192 struct spi_master
*master
;
193 struct platform_device
*pdev
;
195 struct regmap
*regmap
;
196 struct regmap
*regmap_pushr
;
200 struct spi_transfer
*cur_transfer
;
201 struct spi_message
*cur_msg
;
202 struct chip_data
*cur_chip
;
211 const struct fsl_dspi_devtype_data
*devtype_data
;
213 wait_queue_head_t waitq
;
216 struct fsl_dspi_dma
*dma
;
219 static u32
dspi_pop_tx(struct fsl_dspi
*dspi
)
224 if (dspi
->bytes_per_word
== 1)
225 txdata
= *(u8
*)dspi
->tx
;
226 else if (dspi
->bytes_per_word
== 2)
227 txdata
= *(u16
*)dspi
->tx
;
228 else /* dspi->bytes_per_word == 4 */
229 txdata
= *(u32
*)dspi
->tx
;
230 dspi
->tx
+= dspi
->bytes_per_word
;
232 dspi
->len
-= dspi
->bytes_per_word
;
236 static u32
dspi_pop_tx_pushr(struct fsl_dspi
*dspi
)
238 u16 cmd
= dspi
->tx_cmd
, data
= dspi_pop_tx(dspi
);
241 cmd
|= SPI_PUSHR_CMD_CONT
;
242 return cmd
<< 16 | data
;
245 static void dspi_push_rx(struct fsl_dspi
*dspi
, u32 rxdata
)
250 /* Mask of undefined bits */
251 rxdata
&= (1 << dspi
->bits_per_word
) - 1;
253 if (dspi
->bytes_per_word
== 1)
254 *(u8
*)dspi
->rx
= rxdata
;
255 else if (dspi
->bytes_per_word
== 2)
256 *(u16
*)dspi
->rx
= rxdata
;
257 else /* dspi->bytes_per_word == 4 */
258 *(u32
*)dspi
->rx
= rxdata
;
259 dspi
->rx
+= dspi
->bytes_per_word
;
262 static void dspi_tx_dma_callback(void *arg
)
264 struct fsl_dspi
*dspi
= arg
;
265 struct fsl_dspi_dma
*dma
= dspi
->dma
;
267 complete(&dma
->cmd_tx_complete
);
270 static void dspi_rx_dma_callback(void *arg
)
272 struct fsl_dspi
*dspi
= arg
;
273 struct fsl_dspi_dma
*dma
= dspi
->dma
;
277 for (i
= 0; i
< dma
->curr_xfer_len
; i
++)
278 dspi_push_rx(dspi
, dspi
->dma
->rx_dma_buf
[i
]);
281 complete(&dma
->cmd_rx_complete
);
284 static int dspi_next_xfer_dma_submit(struct fsl_dspi
*dspi
)
286 struct fsl_dspi_dma
*dma
= dspi
->dma
;
287 struct device
*dev
= &dspi
->pdev
->dev
;
291 for (i
= 0; i
< dma
->curr_xfer_len
; i
++)
292 dspi
->dma
->tx_dma_buf
[i
] = dspi_pop_tx_pushr(dspi
);
294 dma
->tx_desc
= dmaengine_prep_slave_single(dma
->chan_tx
,
297 DMA_SLAVE_BUSWIDTH_4_BYTES
,
299 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
301 dev_err(dev
, "Not able to get desc for DMA xfer\n");
305 dma
->tx_desc
->callback
= dspi_tx_dma_callback
;
306 dma
->tx_desc
->callback_param
= dspi
;
307 if (dma_submit_error(dmaengine_submit(dma
->tx_desc
))) {
308 dev_err(dev
, "DMA submit failed\n");
312 dma
->rx_desc
= dmaengine_prep_slave_single(dma
->chan_rx
,
315 DMA_SLAVE_BUSWIDTH_4_BYTES
,
317 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
319 dev_err(dev
, "Not able to get desc for DMA xfer\n");
323 dma
->rx_desc
->callback
= dspi_rx_dma_callback
;
324 dma
->rx_desc
->callback_param
= dspi
;
325 if (dma_submit_error(dmaengine_submit(dma
->rx_desc
))) {
326 dev_err(dev
, "DMA submit failed\n");
330 reinit_completion(&dspi
->dma
->cmd_rx_complete
);
331 reinit_completion(&dspi
->dma
->cmd_tx_complete
);
333 dma_async_issue_pending(dma
->chan_rx
);
334 dma_async_issue_pending(dma
->chan_tx
);
336 time_left
= wait_for_completion_timeout(&dspi
->dma
->cmd_tx_complete
,
337 DMA_COMPLETION_TIMEOUT
);
338 if (time_left
== 0) {
339 dev_err(dev
, "DMA tx timeout\n");
340 dmaengine_terminate_all(dma
->chan_tx
);
341 dmaengine_terminate_all(dma
->chan_rx
);
345 time_left
= wait_for_completion_timeout(&dspi
->dma
->cmd_rx_complete
,
346 DMA_COMPLETION_TIMEOUT
);
347 if (time_left
== 0) {
348 dev_err(dev
, "DMA rx timeout\n");
349 dmaengine_terminate_all(dma
->chan_tx
);
350 dmaengine_terminate_all(dma
->chan_rx
);
357 static int dspi_dma_xfer(struct fsl_dspi
*dspi
)
359 struct fsl_dspi_dma
*dma
= dspi
->dma
;
360 struct device
*dev
= &dspi
->pdev
->dev
;
361 struct spi_message
*message
= dspi
->cur_msg
;
362 int curr_remaining_bytes
;
363 int bytes_per_buffer
;
366 curr_remaining_bytes
= dspi
->len
;
367 bytes_per_buffer
= DSPI_DMA_BUFSIZE
/ DSPI_FIFO_SIZE
;
368 while (curr_remaining_bytes
) {
369 /* Check if current transfer fits the DMA buffer */
370 dma
->curr_xfer_len
= curr_remaining_bytes
371 / dspi
->bytes_per_word
;
372 if (dma
->curr_xfer_len
> bytes_per_buffer
)
373 dma
->curr_xfer_len
= bytes_per_buffer
;
375 ret
= dspi_next_xfer_dma_submit(dspi
);
377 dev_err(dev
, "DMA transfer failed\n");
382 dma
->curr_xfer_len
* dspi
->bytes_per_word
;
383 curr_remaining_bytes
-= len
;
384 message
->actual_length
+= len
;
385 if (curr_remaining_bytes
< 0)
386 curr_remaining_bytes
= 0;
394 static int dspi_request_dma(struct fsl_dspi
*dspi
, phys_addr_t phy_addr
)
396 struct fsl_dspi_dma
*dma
;
397 struct dma_slave_config cfg
;
398 struct device
*dev
= &dspi
->pdev
->dev
;
401 dma
= devm_kzalloc(dev
, sizeof(*dma
), GFP_KERNEL
);
405 dma
->chan_rx
= dma_request_slave_channel(dev
, "rx");
407 dev_err(dev
, "rx dma channel not available\n");
412 dma
->chan_tx
= dma_request_slave_channel(dev
, "tx");
414 dev_err(dev
, "tx dma channel not available\n");
419 dma
->tx_dma_buf
= dma_alloc_coherent(dev
, DSPI_DMA_BUFSIZE
,
420 &dma
->tx_dma_phys
, GFP_KERNEL
);
421 if (!dma
->tx_dma_buf
) {
426 dma
->rx_dma_buf
= dma_alloc_coherent(dev
, DSPI_DMA_BUFSIZE
,
427 &dma
->rx_dma_phys
, GFP_KERNEL
);
428 if (!dma
->rx_dma_buf
) {
433 cfg
.src_addr
= phy_addr
+ SPI_POPR
;
434 cfg
.dst_addr
= phy_addr
+ SPI_PUSHR
;
435 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
436 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
437 cfg
.src_maxburst
= 1;
438 cfg
.dst_maxburst
= 1;
440 cfg
.direction
= DMA_DEV_TO_MEM
;
441 ret
= dmaengine_slave_config(dma
->chan_rx
, &cfg
);
443 dev_err(dev
, "can't configure rx dma channel\n");
445 goto err_slave_config
;
448 cfg
.direction
= DMA_MEM_TO_DEV
;
449 ret
= dmaengine_slave_config(dma
->chan_tx
, &cfg
);
451 dev_err(dev
, "can't configure tx dma channel\n");
453 goto err_slave_config
;
457 init_completion(&dma
->cmd_tx_complete
);
458 init_completion(&dma
->cmd_rx_complete
);
463 dma_free_coherent(dev
, DSPI_DMA_BUFSIZE
,
464 dma
->rx_dma_buf
, dma
->rx_dma_phys
);
466 dma_free_coherent(dev
, DSPI_DMA_BUFSIZE
,
467 dma
->tx_dma_buf
, dma
->tx_dma_phys
);
469 dma_release_channel(dma
->chan_tx
);
471 dma_release_channel(dma
->chan_rx
);
473 devm_kfree(dev
, dma
);
479 static void dspi_release_dma(struct fsl_dspi
*dspi
)
481 struct fsl_dspi_dma
*dma
= dspi
->dma
;
482 struct device
*dev
= &dspi
->pdev
->dev
;
486 dma_unmap_single(dev
, dma
->tx_dma_phys
,
487 DSPI_DMA_BUFSIZE
, DMA_TO_DEVICE
);
488 dma_release_channel(dma
->chan_tx
);
492 dma_unmap_single(dev
, dma
->rx_dma_phys
,
493 DSPI_DMA_BUFSIZE
, DMA_FROM_DEVICE
);
494 dma_release_channel(dma
->chan_rx
);
499 static void hz_to_spi_baud(char *pbr
, char *br
, int speed_hz
,
500 unsigned long clkrate
)
502 /* Valid baud rate pre-scaler values */
503 int pbr_tbl
[4] = {2, 3, 5, 7};
504 int brs
[16] = { 2, 4, 6, 8,
506 256, 512, 1024, 2048,
507 4096, 8192, 16384, 32768 };
508 int scale_needed
, scale
, minscale
= INT_MAX
;
511 scale_needed
= clkrate
/ speed_hz
;
512 if (clkrate
% speed_hz
)
515 for (i
= 0; i
< ARRAY_SIZE(brs
); i
++)
516 for (j
= 0; j
< ARRAY_SIZE(pbr_tbl
); j
++) {
517 scale
= brs
[i
] * pbr_tbl
[j
];
518 if (scale
>= scale_needed
) {
519 if (scale
< minscale
) {
528 if (minscale
== INT_MAX
) {
529 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
531 *pbr
= ARRAY_SIZE(pbr_tbl
) - 1;
532 *br
= ARRAY_SIZE(brs
) - 1;
536 static void ns_delay_scale(char *psc
, char *sc
, int delay_ns
,
537 unsigned long clkrate
)
539 int pscale_tbl
[4] = {1, 3, 5, 7};
540 int scale_needed
, scale
, minscale
= INT_MAX
;
544 scale_needed
= div_u64_rem((u64
)delay_ns
* clkrate
, NSEC_PER_SEC
,
549 for (i
= 0; i
< ARRAY_SIZE(pscale_tbl
); i
++)
550 for (j
= 0; j
<= SPI_CTAR_SCALE_BITS
; j
++) {
551 scale
= pscale_tbl
[i
] * (2 << j
);
552 if (scale
>= scale_needed
) {
553 if (scale
< minscale
) {
562 if (minscale
== INT_MAX
) {
563 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
565 *psc
= ARRAY_SIZE(pscale_tbl
) - 1;
566 *sc
= SPI_CTAR_SCALE_BITS
;
570 static void fifo_write(struct fsl_dspi
*dspi
)
572 regmap_write(dspi
->regmap
, SPI_PUSHR
, dspi_pop_tx_pushr(dspi
));
575 static void cmd_fifo_write(struct fsl_dspi
*dspi
)
577 u16 cmd
= dspi
->tx_cmd
;
580 cmd
|= SPI_PUSHR_CMD_CONT
;
581 regmap_write(dspi
->regmap_pushr
, PUSHR_CMD
, cmd
);
584 static void tx_fifo_write(struct fsl_dspi
*dspi
, u16 txdata
)
586 regmap_write(dspi
->regmap_pushr
, PUSHR_TX
, txdata
);
589 static void dspi_tcfq_write(struct fsl_dspi
*dspi
)
591 /* Clear transfer count */
592 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CTCNT
;
594 if (dspi
->devtype_data
->xspi_mode
&& dspi
->bits_per_word
> 16) {
595 /* Write two TX FIFO entries first, and then the corresponding
598 u32 data
= dspi_pop_tx(dspi
);
600 if (dspi
->cur_chip
->ctar_val
& SPI_CTAR_LSBFE(1)) {
602 tx_fifo_write(dspi
, data
& 0xFFFF);
603 tx_fifo_write(dspi
, data
>> 16);
606 tx_fifo_write(dspi
, data
>> 16);
607 tx_fifo_write(dspi
, data
& 0xFFFF);
609 cmd_fifo_write(dspi
);
611 /* Write one entry to both TX FIFO and CMD FIFO
618 static u32
fifo_read(struct fsl_dspi
*dspi
)
622 regmap_read(dspi
->regmap
, SPI_POPR
, &rxdata
);
626 static void dspi_tcfq_read(struct fsl_dspi
*dspi
)
628 dspi_push_rx(dspi
, fifo_read(dspi
));
631 static void dspi_eoq_write(struct fsl_dspi
*dspi
)
633 int fifo_size
= DSPI_FIFO_SIZE
;
634 u16 xfer_cmd
= dspi
->tx_cmd
;
636 /* Fill TX FIFO with as many transfers as possible */
637 while (dspi
->len
&& fifo_size
--) {
638 dspi
->tx_cmd
= xfer_cmd
;
639 /* Request EOQF for last transfer in FIFO */
640 if (dspi
->len
== dspi
->bytes_per_word
|| fifo_size
== 0)
641 dspi
->tx_cmd
|= SPI_PUSHR_CMD_EOQ
;
642 /* Clear transfer count for first transfer in FIFO */
643 if (fifo_size
== (DSPI_FIFO_SIZE
- 1))
644 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CTCNT
;
645 /* Write combined TX FIFO and CMD FIFO entry */
650 static void dspi_eoq_read(struct fsl_dspi
*dspi
)
652 int fifo_size
= DSPI_FIFO_SIZE
;
654 /* Read one FIFO entry at and push to rx buffer */
655 while ((dspi
->rx
< dspi
->rx_end
) && fifo_size
--)
656 dspi_push_rx(dspi
, fifo_read(dspi
));
659 static int dspi_transfer_one_message(struct spi_master
*master
,
660 struct spi_message
*message
)
662 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
663 struct spi_device
*spi
= message
->spi
;
664 struct spi_transfer
*transfer
;
666 enum dspi_trans_mode trans_mode
;
668 message
->actual_length
= 0;
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 /* Prepare command word for CMD FIFO */
675 dspi
->tx_cmd
= SPI_PUSHR_CMD_CTAS(0) |
676 SPI_PUSHR_CMD_PCS(spi
->chip_select
);
677 if (list_is_last(&dspi
->cur_transfer
->transfer_list
,
678 &dspi
->cur_msg
->transfers
)) {
679 /* Leave PCS activated after last transfer when
682 if (transfer
->cs_change
)
683 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CONT
;
685 /* Keep PCS active between transfers in same message
686 * when cs_change is not set, and de-activate PCS
687 * between transfers in the same message when
690 if (!transfer
->cs_change
)
691 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CONT
;
694 dspi
->void_write_data
= dspi
->cur_chip
->void_write_data
;
696 dspi
->tx
= transfer
->tx_buf
;
697 dspi
->rx
= transfer
->rx_buf
;
698 dspi
->rx_end
= dspi
->rx
+ transfer
->len
;
699 dspi
->len
= transfer
->len
;
700 /* Validated transfer specific frame size (defaults applied) */
701 dspi
->bits_per_word
= transfer
->bits_per_word
;
702 if (transfer
->bits_per_word
<= 8)
703 dspi
->bytes_per_word
= 1;
704 else if (transfer
->bits_per_word
<= 16)
705 dspi
->bytes_per_word
= 2;
707 dspi
->bytes_per_word
= 4;
709 regmap_update_bits(dspi
->regmap
, SPI_MCR
,
710 SPI_MCR_CLR_TXF
| SPI_MCR_CLR_RXF
,
711 SPI_MCR_CLR_TXF
| SPI_MCR_CLR_RXF
);
712 regmap_write(dspi
->regmap
, SPI_CTAR(0),
713 dspi
->cur_chip
->ctar_val
|
714 SPI_FRAME_BITS(transfer
->bits_per_word
));
715 if (dspi
->devtype_data
->xspi_mode
)
716 regmap_write(dspi
->regmap
, SPI_CTARE(0),
717 SPI_FRAME_EBITS(transfer
->bits_per_word
)
718 | SPI_CTARE_DTCP(1));
720 trans_mode
= dspi
->devtype_data
->trans_mode
;
721 switch (trans_mode
) {
723 regmap_write(dspi
->regmap
, SPI_RSER
, SPI_RSER_EOQFE
);
724 dspi_eoq_write(dspi
);
727 regmap_write(dspi
->regmap
, SPI_RSER
, SPI_RSER_TCFQE
);
728 dspi_tcfq_write(dspi
);
731 regmap_write(dspi
->regmap
, SPI_RSER
,
732 SPI_RSER_TFFFE
| SPI_RSER_TFFFD
|
733 SPI_RSER_RFDFE
| SPI_RSER_RFDFD
);
734 status
= dspi_dma_xfer(dspi
);
737 dev_err(&dspi
->pdev
->dev
, "unsupported trans_mode %u\n",
743 if (trans_mode
!= DSPI_DMA_MODE
) {
744 if (wait_event_interruptible(dspi
->waitq
,
746 dev_err(&dspi
->pdev
->dev
,
747 "wait transfer complete fail!\n");
751 if (transfer
->delay_usecs
)
752 udelay(transfer
->delay_usecs
);
756 message
->status
= status
;
757 spi_finalize_current_message(master
);
762 static int dspi_setup(struct spi_device
*spi
)
764 struct chip_data
*chip
;
765 struct fsl_dspi
*dspi
= spi_master_get_devdata(spi
->master
);
766 struct fsl_dspi_platform_data
*pdata
;
767 u32 cs_sck_delay
= 0, sck_cs_delay
= 0;
768 unsigned char br
= 0, pbr
= 0, pcssck
= 0, cssck
= 0;
769 unsigned char pasc
= 0, asc
= 0;
770 unsigned long clkrate
;
772 /* Only alloc on first setup */
773 chip
= spi_get_ctldata(spi
);
775 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
780 pdata
= dev_get_platdata(&dspi
->pdev
->dev
);
783 of_property_read_u32(spi
->dev
.of_node
, "fsl,spi-cs-sck-delay",
786 of_property_read_u32(spi
->dev
.of_node
, "fsl,spi-sck-cs-delay",
789 cs_sck_delay
= pdata
->cs_sck_delay
;
790 sck_cs_delay
= pdata
->sck_cs_delay
;
793 chip
->void_write_data
= 0;
795 clkrate
= clk_get_rate(dspi
->clk
);
796 hz_to_spi_baud(&pbr
, &br
, spi
->max_speed_hz
, clkrate
);
798 /* Set PCS to SCK delay scale values */
799 ns_delay_scale(&pcssck
, &cssck
, cs_sck_delay
, clkrate
);
801 /* Set After SCK delay scale values */
802 ns_delay_scale(&pasc
, &asc
, sck_cs_delay
, clkrate
);
804 chip
->ctar_val
= SPI_CTAR_CPOL(spi
->mode
& SPI_CPOL
? 1 : 0)
805 | SPI_CTAR_CPHA(spi
->mode
& SPI_CPHA
? 1 : 0)
806 | SPI_CTAR_LSBFE(spi
->mode
& SPI_LSB_FIRST
? 1 : 0)
807 | SPI_CTAR_PCSSCK(pcssck
)
808 | SPI_CTAR_CSSCK(cssck
)
809 | SPI_CTAR_PASC(pasc
)
814 spi_set_ctldata(spi
, chip
);
819 static void dspi_cleanup(struct spi_device
*spi
)
821 struct chip_data
*chip
= spi_get_ctldata((struct spi_device
*)spi
);
823 dev_dbg(&spi
->dev
, "spi_device %u.%u cleanup\n",
824 spi
->master
->bus_num
, spi
->chip_select
);
829 static irqreturn_t
dspi_interrupt(int irq
, void *dev_id
)
831 struct fsl_dspi
*dspi
= (struct fsl_dspi
*)dev_id
;
832 struct spi_message
*msg
= dspi
->cur_msg
;
833 enum dspi_trans_mode trans_mode
;
837 regmap_read(dspi
->regmap
, SPI_SR
, &spi_sr
);
838 regmap_write(dspi
->regmap
, SPI_SR
, spi_sr
);
841 if (spi_sr
& (SPI_SR_EOQF
| SPI_SR_TCFQF
)) {
842 /* Get transfer counter (in number of SPI transfers). It was
843 * reset to 0 when transfer(s) were started.
845 regmap_read(dspi
->regmap
, SPI_TCR
, &spi_tcr
);
846 spi_tcnt
= SPI_TCR_GET_TCNT(spi_tcr
);
847 /* Update total number of bytes that were transferred */
848 msg
->actual_length
+= spi_tcnt
* dspi
->bytes_per_word
;
850 trans_mode
= dspi
->devtype_data
->trans_mode
;
851 switch (trans_mode
) {
856 dspi_tcfq_read(dspi
);
859 dev_err(&dspi
->pdev
->dev
, "unsupported trans_mode %u\n",
866 wake_up_interruptible(&dspi
->waitq
);
868 switch (trans_mode
) {
870 dspi_eoq_write(dspi
);
873 dspi_tcfq_write(dspi
);
876 dev_err(&dspi
->pdev
->dev
,
877 "unsupported trans_mode %u\n",
886 static const struct of_device_id fsl_dspi_dt_ids
[] = {
887 { .compatible
= "fsl,vf610-dspi", .data
= &vf610_data
, },
888 { .compatible
= "fsl,ls1021a-v1.0-dspi", .data
= &ls1021a_v1_data
, },
889 { .compatible
= "fsl,ls2085a-dspi", .data
= &ls2085a_data
, },
892 MODULE_DEVICE_TABLE(of
, fsl_dspi_dt_ids
);
894 #ifdef CONFIG_PM_SLEEP
895 static int dspi_suspend(struct device
*dev
)
897 struct spi_master
*master
= dev_get_drvdata(dev
);
898 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
901 disable_irq(dspi
->irq
);
902 spi_master_suspend(master
);
903 clk_disable_unprepare(dspi
->clk
);
905 pinctrl_pm_select_sleep_state(dev
);
910 static int dspi_resume(struct device
*dev
)
912 struct spi_master
*master
= dev_get_drvdata(dev
);
913 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
916 pinctrl_pm_select_default_state(dev
);
918 ret
= clk_prepare_enable(dspi
->clk
);
921 spi_master_resume(master
);
923 enable_irq(dspi
->irq
);
927 #endif /* CONFIG_PM_SLEEP */
929 static SIMPLE_DEV_PM_OPS(dspi_pm
, dspi_suspend
, dspi_resume
);
931 static const struct regmap_range dspi_volatile_ranges
[] = {
932 regmap_reg_range(SPI_MCR
, SPI_TCR
),
933 regmap_reg_range(SPI_SR
, SPI_SR
),
934 regmap_reg_range(SPI_PUSHR
, SPI_RXFR3
),
937 static const struct regmap_access_table dspi_volatile_table
= {
938 .yes_ranges
= dspi_volatile_ranges
,
939 .n_yes_ranges
= ARRAY_SIZE(dspi_volatile_ranges
),
942 static const struct regmap_config dspi_regmap_config
= {
946 .max_register
= 0x88,
947 .volatile_table
= &dspi_volatile_table
,
950 static const struct regmap_range dspi_xspi_volatile_ranges
[] = {
951 regmap_reg_range(SPI_MCR
, SPI_TCR
),
952 regmap_reg_range(SPI_SR
, SPI_SR
),
953 regmap_reg_range(SPI_PUSHR
, SPI_RXFR3
),
954 regmap_reg_range(SPI_SREX
, SPI_SREX
),
957 static const struct regmap_access_table dspi_xspi_volatile_table
= {
958 .yes_ranges
= dspi_xspi_volatile_ranges
,
959 .n_yes_ranges
= ARRAY_SIZE(dspi_xspi_volatile_ranges
),
962 static const struct regmap_config dspi_xspi_regmap_config
[] = {
967 .max_register
= 0x13c,
968 .volatile_table
= &dspi_xspi_volatile_table
,
979 static void dspi_init(struct fsl_dspi
*dspi
)
981 regmap_write(dspi
->regmap
, SPI_MCR
, SPI_MCR_MASTER
| SPI_MCR_PCSIS
|
982 (dspi
->devtype_data
->xspi_mode
? SPI_MCR_XSPI
: 0));
983 regmap_write(dspi
->regmap
, SPI_SR
, SPI_SR_CLEAR
);
984 if (dspi
->devtype_data
->xspi_mode
)
985 regmap_write(dspi
->regmap
, SPI_CTARE(0),
986 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
989 static int dspi_probe(struct platform_device
*pdev
)
991 struct device_node
*np
= pdev
->dev
.of_node
;
992 struct spi_master
*master
;
993 struct fsl_dspi
*dspi
;
994 struct resource
*res
;
995 const struct regmap_config
*regmap_config
;
997 struct fsl_dspi_platform_data
*pdata
;
998 int ret
= 0, cs_num
, bus_num
;
1000 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct fsl_dspi
));
1004 dspi
= spi_master_get_devdata(master
);
1006 dspi
->master
= master
;
1008 master
->transfer
= NULL
;
1009 master
->setup
= dspi_setup
;
1010 master
->transfer_one_message
= dspi_transfer_one_message
;
1011 master
->dev
.of_node
= pdev
->dev
.of_node
;
1013 master
->cleanup
= dspi_cleanup
;
1014 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
1016 pdata
= dev_get_platdata(&pdev
->dev
);
1018 master
->num_chipselect
= pdata
->cs_num
;
1019 master
->bus_num
= pdata
->bus_num
;
1021 dspi
->devtype_data
= &coldfire_data
;
1024 ret
= of_property_read_u32(np
, "spi-num-chipselects", &cs_num
);
1026 dev_err(&pdev
->dev
, "can't get spi-num-chipselects\n");
1027 goto out_master_put
;
1029 master
->num_chipselect
= cs_num
;
1031 ret
= of_property_read_u32(np
, "bus-num", &bus_num
);
1033 dev_err(&pdev
->dev
, "can't get bus-num\n");
1034 goto out_master_put
;
1036 master
->bus_num
= bus_num
;
1038 dspi
->devtype_data
= of_device_get_match_data(&pdev
->dev
);
1039 if (!dspi
->devtype_data
) {
1040 dev_err(&pdev
->dev
, "can't get devtype_data\n");
1042 goto out_master_put
;
1046 if (dspi
->devtype_data
->xspi_mode
)
1047 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1049 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
1051 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1052 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1054 ret
= PTR_ERR(base
);
1055 goto out_master_put
;
1058 if (dspi
->devtype_data
->xspi_mode
)
1059 regmap_config
= &dspi_xspi_regmap_config
[0];
1061 regmap_config
= &dspi_regmap_config
;
1062 dspi
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
, regmap_config
);
1063 if (IS_ERR(dspi
->regmap
)) {
1064 dev_err(&pdev
->dev
, "failed to init regmap: %ld\n",
1065 PTR_ERR(dspi
->regmap
));
1066 ret
= PTR_ERR(dspi
->regmap
);
1067 goto out_master_put
;
1070 if (dspi
->devtype_data
->xspi_mode
) {
1071 dspi
->regmap_pushr
= devm_regmap_init_mmio(
1072 &pdev
->dev
, base
+ SPI_PUSHR
,
1073 &dspi_xspi_regmap_config
[1]);
1074 if (IS_ERR(dspi
->regmap_pushr
)) {
1076 "failed to init pushr regmap: %ld\n",
1077 PTR_ERR(dspi
->regmap_pushr
));
1078 ret
= PTR_ERR(dspi
->regmap_pushr
);
1079 goto out_master_put
;
1083 dspi
->clk
= devm_clk_get(&pdev
->dev
, "dspi");
1084 if (IS_ERR(dspi
->clk
)) {
1085 ret
= PTR_ERR(dspi
->clk
);
1086 dev_err(&pdev
->dev
, "unable to get clock\n");
1087 goto out_master_put
;
1089 ret
= clk_prepare_enable(dspi
->clk
);
1091 goto out_master_put
;
1094 dspi
->irq
= platform_get_irq(pdev
, 0);
1095 if (dspi
->irq
< 0) {
1096 dev_err(&pdev
->dev
, "can't get platform irq\n");
1101 ret
= request_threaded_irq(dspi
->irq
, dspi_interrupt
, NULL
,
1102 IRQF_SHARED
, pdev
->name
, dspi
);
1104 dev_err(&pdev
->dev
, "Unable to attach DSPI interrupt\n");
1108 if (dspi
->devtype_data
->trans_mode
== DSPI_DMA_MODE
) {
1109 ret
= dspi_request_dma(dspi
, res
->start
);
1111 dev_err(&pdev
->dev
, "can't get dma channels\n");
1116 master
->max_speed_hz
=
1117 clk_get_rate(dspi
->clk
) / dspi
->devtype_data
->max_clock_factor
;
1119 init_waitqueue_head(&dspi
->waitq
);
1120 platform_set_drvdata(pdev
, master
);
1122 ret
= spi_register_master(master
);
1124 dev_err(&pdev
->dev
, "Problem registering DSPI master\n");
1132 free_irq(dspi
->irq
, dspi
);
1134 clk_disable_unprepare(dspi
->clk
);
1136 spi_master_put(master
);
1141 static int dspi_remove(struct platform_device
*pdev
)
1143 struct spi_master
*master
= platform_get_drvdata(pdev
);
1144 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
1146 /* Disconnect from the SPI framework */
1147 spi_unregister_controller(dspi
->master
);
1149 /* Disable RX and TX */
1150 regmap_update_bits(dspi
->regmap
, SPI_MCR
,
1151 SPI_MCR_DIS_TXF
| SPI_MCR_DIS_RXF
,
1152 SPI_MCR_DIS_TXF
| SPI_MCR_DIS_RXF
);
1155 regmap_update_bits(dspi
->regmap
, SPI_MCR
, SPI_MCR_HALT
, SPI_MCR_HALT
);
1157 dspi_release_dma(dspi
);
1159 free_irq(dspi
->irq
, dspi
);
1160 clk_disable_unprepare(dspi
->clk
);
1165 static void dspi_shutdown(struct platform_device
*pdev
)
1167 struct spi_controller
*ctlr
= platform_get_drvdata(pdev
);
1168 struct fsl_dspi
*dspi
= spi_controller_get_devdata(ctlr
);
1170 /* Disable RX and TX */
1171 regmap_update_bits(dspi
->regmap
, SPI_MCR
,
1172 SPI_MCR_DIS_TXF
| SPI_MCR_DIS_RXF
,
1173 SPI_MCR_DIS_TXF
| SPI_MCR_DIS_RXF
);
1176 regmap_update_bits(dspi
->regmap
, SPI_MCR
, SPI_MCR_HALT
, SPI_MCR_HALT
);
1178 dspi_release_dma(dspi
);
1179 clk_disable_unprepare(dspi
->clk
);
1180 spi_unregister_controller(dspi
->master
);
1183 static struct platform_driver fsl_dspi_driver
= {
1184 .driver
.name
= DRIVER_NAME
,
1185 .driver
.of_match_table
= fsl_dspi_dt_ids
,
1186 .driver
.owner
= THIS_MODULE
,
1187 .driver
.pm
= &dspi_pm
,
1188 .probe
= dspi_probe
,
1189 .remove
= dspi_remove
,
1190 .shutdown
= dspi_shutdown
,
1192 module_platform_driver(fsl_dspi_driver
);
1194 MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1195 MODULE_LICENSE("GPL");
1196 MODULE_ALIAS("platform:" DRIVER_NAME
);