1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright 2013 Freescale Semiconductor, Inc.
5 // Freescale DSPI driver
6 // This file contains a driver for the Freescale DSPI
9 #include <linux/delay.h>
10 #include <linux/dmaengine.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/sched.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/spi-fsl-dspi.h>
28 #include <linux/spi/spi_bitbang.h>
29 #include <linux/time.h>
31 #define DRIVER_NAME "fsl-dspi"
33 #define DSPI_FIFO_SIZE 4
34 #define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
37 #define SPI_MCR_MASTER (1 << 31)
38 #define SPI_MCR_PCSIS (0x3F << 16)
39 #define SPI_MCR_CLR_TXF (1 << 11)
40 #define SPI_MCR_CLR_RXF (1 << 10)
41 #define SPI_MCR_XSPI (1 << 3)
44 #define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
46 #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
47 #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
48 #define SPI_CTAR_CPOL(x) ((x) << 26)
49 #define SPI_CTAR_CPHA(x) ((x) << 25)
50 #define SPI_CTAR_LSBFE(x) ((x) << 24)
51 #define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
52 #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
53 #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
54 #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
55 #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
56 #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
57 #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
58 #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
59 #define SPI_CTAR_SCALE_BITS 0xf
61 #define SPI_CTAR0_SLAVE 0x0c
64 #define SPI_SR_EOQF 0x10000000
65 #define SPI_SR_TCFQF 0x80000000
66 #define SPI_SR_CLEAR 0xdaad0000
68 #define SPI_RSER_TFFFE BIT(25)
69 #define SPI_RSER_TFFFD BIT(24)
70 #define SPI_RSER_RFDFE BIT(17)
71 #define SPI_RSER_RFDFD BIT(16)
74 #define SPI_RSER_EOQFE 0x10000000
75 #define SPI_RSER_TCFQE 0x80000000
77 #define SPI_PUSHR 0x34
78 #define SPI_PUSHR_CMD_CONT (1 << 15)
79 #define SPI_PUSHR_CONT (SPI_PUSHR_CMD_CONT << 16)
80 #define SPI_PUSHR_CMD_CTAS(x) (((x) & 0x0003) << 12)
81 #define SPI_PUSHR_CTAS(x) (SPI_PUSHR_CMD_CTAS(x) << 16)
82 #define SPI_PUSHR_CMD_EOQ (1 << 11)
83 #define SPI_PUSHR_EOQ (SPI_PUSHR_CMD_EOQ << 16)
84 #define SPI_PUSHR_CMD_CTCNT (1 << 10)
85 #define SPI_PUSHR_CTCNT (SPI_PUSHR_CMD_CTCNT << 16)
86 #define SPI_PUSHR_CMD_PCS(x) ((1 << x) & 0x003f)
87 #define SPI_PUSHR_PCS(x) (SPI_PUSHR_CMD_PCS(x) << 16)
88 #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
90 #define SPI_PUSHR_SLAVE 0x34
93 #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
95 #define SPI_TXFR0 0x3c
96 #define SPI_TXFR1 0x40
97 #define SPI_TXFR2 0x44
98 #define SPI_TXFR3 0x48
99 #define SPI_RXFR0 0x7c
100 #define SPI_RXFR1 0x80
101 #define SPI_RXFR2 0x84
102 #define SPI_RXFR3 0x88
104 #define SPI_CTARE(x) (0x11c + (((x) & 0x3) * 4))
105 #define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
106 #define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
108 #define SPI_SREX 0x13c
110 #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
111 #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
112 #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
113 #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
115 #define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
116 #define SPI_FRAME_EBITS_MASK SPI_CTARE_FMSZE(1)
118 /* Register offsets for regmap_pushr */
119 #define PUSHR_CMD 0x0
122 #define SPI_CS_INIT 0x01
123 #define SPI_CS_ASSERT 0x02
124 #define SPI_CS_DROP 0x04
126 #define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
133 enum dspi_trans_mode
{
139 struct fsl_dspi_devtype_data
{
140 enum dspi_trans_mode trans_mode
;
145 static const struct fsl_dspi_devtype_data vf610_data
= {
146 .trans_mode
= DSPI_DMA_MODE
,
147 .max_clock_factor
= 2,
150 static const struct fsl_dspi_devtype_data ls1021a_v1_data
= {
151 .trans_mode
= DSPI_TCFQ_MODE
,
152 .max_clock_factor
= 8,
156 static const struct fsl_dspi_devtype_data ls2085a_data
= {
157 .trans_mode
= DSPI_TCFQ_MODE
,
158 .max_clock_factor
= 8,
161 static const struct fsl_dspi_devtype_data coldfire_data
= {
162 .trans_mode
= DSPI_EOQ_MODE
,
163 .max_clock_factor
= 8,
166 struct fsl_dspi_dma
{
167 /* Length of transfer in words of DSPI_FIFO_SIZE */
171 struct dma_chan
*chan_tx
;
172 dma_addr_t tx_dma_phys
;
173 struct completion cmd_tx_complete
;
174 struct dma_async_tx_descriptor
*tx_desc
;
177 struct dma_chan
*chan_rx
;
178 dma_addr_t rx_dma_phys
;
179 struct completion cmd_rx_complete
;
180 struct dma_async_tx_descriptor
*rx_desc
;
184 struct spi_master
*master
;
185 struct platform_device
*pdev
;
187 struct regmap
*regmap
;
188 struct regmap
*regmap_pushr
;
192 struct spi_transfer
*cur_transfer
;
193 struct spi_message
*cur_msg
;
194 struct chip_data
*cur_chip
;
203 const struct fsl_dspi_devtype_data
*devtype_data
;
205 wait_queue_head_t waitq
;
208 struct fsl_dspi_dma
*dma
;
211 static u32
dspi_pop_tx(struct fsl_dspi
*dspi
)
216 if (dspi
->bytes_per_word
== 1)
217 txdata
= *(u8
*)dspi
->tx
;
218 else if (dspi
->bytes_per_word
== 2)
219 txdata
= *(u16
*)dspi
->tx
;
220 else /* dspi->bytes_per_word == 4 */
221 txdata
= *(u32
*)dspi
->tx
;
222 dspi
->tx
+= dspi
->bytes_per_word
;
224 dspi
->len
-= dspi
->bytes_per_word
;
228 static u32
dspi_pop_tx_pushr(struct fsl_dspi
*dspi
)
230 u16 cmd
= dspi
->tx_cmd
, data
= dspi_pop_tx(dspi
);
233 cmd
|= SPI_PUSHR_CMD_CONT
;
234 return cmd
<< 16 | data
;
237 static void dspi_push_rx(struct fsl_dspi
*dspi
, u32 rxdata
)
242 /* Mask of undefined bits */
243 rxdata
&= (1 << dspi
->bits_per_word
) - 1;
245 if (dspi
->bytes_per_word
== 1)
246 *(u8
*)dspi
->rx
= rxdata
;
247 else if (dspi
->bytes_per_word
== 2)
248 *(u16
*)dspi
->rx
= rxdata
;
249 else /* dspi->bytes_per_word == 4 */
250 *(u32
*)dspi
->rx
= rxdata
;
251 dspi
->rx
+= dspi
->bytes_per_word
;
254 static void dspi_tx_dma_callback(void *arg
)
256 struct fsl_dspi
*dspi
= arg
;
257 struct fsl_dspi_dma
*dma
= dspi
->dma
;
259 complete(&dma
->cmd_tx_complete
);
262 static void dspi_rx_dma_callback(void *arg
)
264 struct fsl_dspi
*dspi
= arg
;
265 struct fsl_dspi_dma
*dma
= dspi
->dma
;
269 for (i
= 0; i
< dma
->curr_xfer_len
; i
++)
270 dspi_push_rx(dspi
, dspi
->dma
->rx_dma_buf
[i
]);
273 complete(&dma
->cmd_rx_complete
);
276 static int dspi_next_xfer_dma_submit(struct fsl_dspi
*dspi
)
278 struct fsl_dspi_dma
*dma
= dspi
->dma
;
279 struct device
*dev
= &dspi
->pdev
->dev
;
283 for (i
= 0; i
< dma
->curr_xfer_len
; i
++)
284 dspi
->dma
->tx_dma_buf
[i
] = dspi_pop_tx_pushr(dspi
);
286 dma
->tx_desc
= dmaengine_prep_slave_single(dma
->chan_tx
,
289 DMA_SLAVE_BUSWIDTH_4_BYTES
,
291 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
293 dev_err(dev
, "Not able to get desc for DMA xfer\n");
297 dma
->tx_desc
->callback
= dspi_tx_dma_callback
;
298 dma
->tx_desc
->callback_param
= dspi
;
299 if (dma_submit_error(dmaengine_submit(dma
->tx_desc
))) {
300 dev_err(dev
, "DMA submit failed\n");
304 dma
->rx_desc
= dmaengine_prep_slave_single(dma
->chan_rx
,
307 DMA_SLAVE_BUSWIDTH_4_BYTES
,
309 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
311 dev_err(dev
, "Not able to get desc for DMA xfer\n");
315 dma
->rx_desc
->callback
= dspi_rx_dma_callback
;
316 dma
->rx_desc
->callback_param
= dspi
;
317 if (dma_submit_error(dmaengine_submit(dma
->rx_desc
))) {
318 dev_err(dev
, "DMA submit failed\n");
322 reinit_completion(&dspi
->dma
->cmd_rx_complete
);
323 reinit_completion(&dspi
->dma
->cmd_tx_complete
);
325 dma_async_issue_pending(dma
->chan_rx
);
326 dma_async_issue_pending(dma
->chan_tx
);
328 time_left
= wait_for_completion_timeout(&dspi
->dma
->cmd_tx_complete
,
329 DMA_COMPLETION_TIMEOUT
);
330 if (time_left
== 0) {
331 dev_err(dev
, "DMA tx timeout\n");
332 dmaengine_terminate_all(dma
->chan_tx
);
333 dmaengine_terminate_all(dma
->chan_rx
);
337 time_left
= wait_for_completion_timeout(&dspi
->dma
->cmd_rx_complete
,
338 DMA_COMPLETION_TIMEOUT
);
339 if (time_left
== 0) {
340 dev_err(dev
, "DMA rx timeout\n");
341 dmaengine_terminate_all(dma
->chan_tx
);
342 dmaengine_terminate_all(dma
->chan_rx
);
349 static int dspi_dma_xfer(struct fsl_dspi
*dspi
)
351 struct fsl_dspi_dma
*dma
= dspi
->dma
;
352 struct device
*dev
= &dspi
->pdev
->dev
;
353 struct spi_message
*message
= dspi
->cur_msg
;
354 int curr_remaining_bytes
;
355 int bytes_per_buffer
;
358 curr_remaining_bytes
= dspi
->len
;
359 bytes_per_buffer
= DSPI_DMA_BUFSIZE
/ DSPI_FIFO_SIZE
;
360 while (curr_remaining_bytes
) {
361 /* Check if current transfer fits the DMA buffer */
362 dma
->curr_xfer_len
= curr_remaining_bytes
363 / dspi
->bytes_per_word
;
364 if (dma
->curr_xfer_len
> bytes_per_buffer
)
365 dma
->curr_xfer_len
= bytes_per_buffer
;
367 ret
= dspi_next_xfer_dma_submit(dspi
);
369 dev_err(dev
, "DMA transfer failed\n");
374 dma
->curr_xfer_len
* dspi
->bytes_per_word
;
375 curr_remaining_bytes
-= len
;
376 message
->actual_length
+= len
;
377 if (curr_remaining_bytes
< 0)
378 curr_remaining_bytes
= 0;
386 static int dspi_request_dma(struct fsl_dspi
*dspi
, phys_addr_t phy_addr
)
388 struct fsl_dspi_dma
*dma
;
389 struct dma_slave_config cfg
;
390 struct device
*dev
= &dspi
->pdev
->dev
;
393 dma
= devm_kzalloc(dev
, sizeof(*dma
), GFP_KERNEL
);
397 dma
->chan_rx
= dma_request_slave_channel(dev
, "rx");
399 dev_err(dev
, "rx dma channel not available\n");
404 dma
->chan_tx
= dma_request_slave_channel(dev
, "tx");
406 dev_err(dev
, "tx dma channel not available\n");
411 dma
->tx_dma_buf
= dma_alloc_coherent(dev
, DSPI_DMA_BUFSIZE
,
412 &dma
->tx_dma_phys
, GFP_KERNEL
);
413 if (!dma
->tx_dma_buf
) {
418 dma
->rx_dma_buf
= dma_alloc_coherent(dev
, DSPI_DMA_BUFSIZE
,
419 &dma
->rx_dma_phys
, GFP_KERNEL
);
420 if (!dma
->rx_dma_buf
) {
425 cfg
.src_addr
= phy_addr
+ SPI_POPR
;
426 cfg
.dst_addr
= phy_addr
+ SPI_PUSHR
;
427 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
428 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
429 cfg
.src_maxburst
= 1;
430 cfg
.dst_maxburst
= 1;
432 cfg
.direction
= DMA_DEV_TO_MEM
;
433 ret
= dmaengine_slave_config(dma
->chan_rx
, &cfg
);
435 dev_err(dev
, "can't configure rx dma channel\n");
437 goto err_slave_config
;
440 cfg
.direction
= DMA_MEM_TO_DEV
;
441 ret
= dmaengine_slave_config(dma
->chan_tx
, &cfg
);
443 dev_err(dev
, "can't configure tx dma channel\n");
445 goto err_slave_config
;
449 init_completion(&dma
->cmd_tx_complete
);
450 init_completion(&dma
->cmd_rx_complete
);
455 dma_free_coherent(dev
, DSPI_DMA_BUFSIZE
,
456 dma
->rx_dma_buf
, dma
->rx_dma_phys
);
458 dma_free_coherent(dev
, DSPI_DMA_BUFSIZE
,
459 dma
->tx_dma_buf
, dma
->tx_dma_phys
);
461 dma_release_channel(dma
->chan_tx
);
463 dma_release_channel(dma
->chan_rx
);
465 devm_kfree(dev
, dma
);
471 static void dspi_release_dma(struct fsl_dspi
*dspi
)
473 struct fsl_dspi_dma
*dma
= dspi
->dma
;
474 struct device
*dev
= &dspi
->pdev
->dev
;
478 dma_unmap_single(dev
, dma
->tx_dma_phys
,
479 DSPI_DMA_BUFSIZE
, DMA_TO_DEVICE
);
480 dma_release_channel(dma
->chan_tx
);
484 dma_unmap_single(dev
, dma
->rx_dma_phys
,
485 DSPI_DMA_BUFSIZE
, DMA_FROM_DEVICE
);
486 dma_release_channel(dma
->chan_rx
);
491 static void hz_to_spi_baud(char *pbr
, char *br
, int speed_hz
,
492 unsigned long clkrate
)
494 /* Valid baud rate pre-scaler values */
495 int pbr_tbl
[4] = {2, 3, 5, 7};
496 int brs
[16] = { 2, 4, 6, 8,
498 256, 512, 1024, 2048,
499 4096, 8192, 16384, 32768 };
500 int scale_needed
, scale
, minscale
= INT_MAX
;
503 scale_needed
= clkrate
/ speed_hz
;
504 if (clkrate
% speed_hz
)
507 for (i
= 0; i
< ARRAY_SIZE(brs
); i
++)
508 for (j
= 0; j
< ARRAY_SIZE(pbr_tbl
); j
++) {
509 scale
= brs
[i
] * pbr_tbl
[j
];
510 if (scale
>= scale_needed
) {
511 if (scale
< minscale
) {
520 if (minscale
== INT_MAX
) {
521 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
523 *pbr
= ARRAY_SIZE(pbr_tbl
) - 1;
524 *br
= ARRAY_SIZE(brs
) - 1;
528 static void ns_delay_scale(char *psc
, char *sc
, int delay_ns
,
529 unsigned long clkrate
)
531 int pscale_tbl
[4] = {1, 3, 5, 7};
532 int scale_needed
, scale
, minscale
= INT_MAX
;
536 scale_needed
= div_u64_rem((u64
)delay_ns
* clkrate
, NSEC_PER_SEC
,
541 for (i
= 0; i
< ARRAY_SIZE(pscale_tbl
); i
++)
542 for (j
= 0; j
<= SPI_CTAR_SCALE_BITS
; j
++) {
543 scale
= pscale_tbl
[i
] * (2 << j
);
544 if (scale
>= scale_needed
) {
545 if (scale
< minscale
) {
554 if (minscale
== INT_MAX
) {
555 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
557 *psc
= ARRAY_SIZE(pscale_tbl
) - 1;
558 *sc
= SPI_CTAR_SCALE_BITS
;
562 static void fifo_write(struct fsl_dspi
*dspi
)
564 regmap_write(dspi
->regmap
, SPI_PUSHR
, dspi_pop_tx_pushr(dspi
));
567 static void cmd_fifo_write(struct fsl_dspi
*dspi
)
569 u16 cmd
= dspi
->tx_cmd
;
572 cmd
|= SPI_PUSHR_CMD_CONT
;
573 regmap_write(dspi
->regmap_pushr
, PUSHR_CMD
, cmd
);
576 static void tx_fifo_write(struct fsl_dspi
*dspi
, u16 txdata
)
578 regmap_write(dspi
->regmap_pushr
, PUSHR_TX
, txdata
);
581 static void dspi_tcfq_write(struct fsl_dspi
*dspi
)
583 /* Clear transfer count */
584 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CTCNT
;
586 if (dspi
->devtype_data
->xspi_mode
&& dspi
->bits_per_word
> 16) {
587 /* Write two TX FIFO entries first, and then the corresponding
590 u32 data
= dspi_pop_tx(dspi
);
592 if (dspi
->cur_chip
->ctar_val
& SPI_CTAR_LSBFE(1)) {
594 tx_fifo_write(dspi
, data
& 0xFFFF);
595 tx_fifo_write(dspi
, data
>> 16);
598 tx_fifo_write(dspi
, data
>> 16);
599 tx_fifo_write(dspi
, data
& 0xFFFF);
601 cmd_fifo_write(dspi
);
603 /* Write one entry to both TX FIFO and CMD FIFO
610 static u32
fifo_read(struct fsl_dspi
*dspi
)
614 regmap_read(dspi
->regmap
, SPI_POPR
, &rxdata
);
618 static void dspi_tcfq_read(struct fsl_dspi
*dspi
)
620 dspi_push_rx(dspi
, fifo_read(dspi
));
623 static void dspi_eoq_write(struct fsl_dspi
*dspi
)
625 int fifo_size
= DSPI_FIFO_SIZE
;
627 /* Fill TX FIFO with as many transfers as possible */
628 while (dspi
->len
&& fifo_size
--) {
629 /* Request EOQF for last transfer in FIFO */
630 if (dspi
->len
== dspi
->bytes_per_word
|| fifo_size
== 0)
631 dspi
->tx_cmd
|= SPI_PUSHR_CMD_EOQ
;
632 /* Clear transfer count for first transfer in FIFO */
633 if (fifo_size
== (DSPI_FIFO_SIZE
- 1))
634 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CTCNT
;
635 /* Write combined TX FIFO and CMD FIFO entry */
640 static void dspi_eoq_read(struct fsl_dspi
*dspi
)
642 int fifo_size
= DSPI_FIFO_SIZE
;
644 /* Read one FIFO entry at and push to rx buffer */
645 while ((dspi
->rx
< dspi
->rx_end
) && fifo_size
--)
646 dspi_push_rx(dspi
, fifo_read(dspi
));
649 static int dspi_transfer_one_message(struct spi_master
*master
,
650 struct spi_message
*message
)
652 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
653 struct spi_device
*spi
= message
->spi
;
654 struct spi_transfer
*transfer
;
656 enum dspi_trans_mode trans_mode
;
658 message
->actual_length
= 0;
660 list_for_each_entry(transfer
, &message
->transfers
, transfer_list
) {
661 dspi
->cur_transfer
= transfer
;
662 dspi
->cur_msg
= message
;
663 dspi
->cur_chip
= spi_get_ctldata(spi
);
664 /* Prepare command word for CMD FIFO */
665 dspi
->tx_cmd
= SPI_PUSHR_CMD_CTAS(0) |
666 SPI_PUSHR_CMD_PCS(spi
->chip_select
);
667 if (list_is_last(&dspi
->cur_transfer
->transfer_list
,
668 &dspi
->cur_msg
->transfers
)) {
669 /* Leave PCS activated after last transfer when
672 if (transfer
->cs_change
)
673 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CONT
;
675 /* Keep PCS active between transfers in same message
676 * when cs_change is not set, and de-activate PCS
677 * between transfers in the same message when
680 if (!transfer
->cs_change
)
681 dspi
->tx_cmd
|= SPI_PUSHR_CMD_CONT
;
684 dspi
->void_write_data
= dspi
->cur_chip
->void_write_data
;
686 dspi
->tx
= transfer
->tx_buf
;
687 dspi
->rx
= transfer
->rx_buf
;
688 dspi
->rx_end
= dspi
->rx
+ transfer
->len
;
689 dspi
->len
= transfer
->len
;
690 /* Validated transfer specific frame size (defaults applied) */
691 dspi
->bits_per_word
= transfer
->bits_per_word
;
692 if (transfer
->bits_per_word
<= 8)
693 dspi
->bytes_per_word
= 1;
694 else if (transfer
->bits_per_word
<= 16)
695 dspi
->bytes_per_word
= 2;
697 dspi
->bytes_per_word
= 4;
699 regmap_update_bits(dspi
->regmap
, SPI_MCR
,
700 SPI_MCR_CLR_TXF
| SPI_MCR_CLR_RXF
,
701 SPI_MCR_CLR_TXF
| SPI_MCR_CLR_RXF
);
702 regmap_write(dspi
->regmap
, SPI_CTAR(0),
703 dspi
->cur_chip
->ctar_val
|
704 SPI_FRAME_BITS(transfer
->bits_per_word
));
705 if (dspi
->devtype_data
->xspi_mode
)
706 regmap_write(dspi
->regmap
, SPI_CTARE(0),
707 SPI_FRAME_EBITS(transfer
->bits_per_word
)
708 | SPI_CTARE_DTCP(1));
710 trans_mode
= dspi
->devtype_data
->trans_mode
;
711 switch (trans_mode
) {
713 regmap_write(dspi
->regmap
, SPI_RSER
, SPI_RSER_EOQFE
);
714 dspi_eoq_write(dspi
);
717 regmap_write(dspi
->regmap
, SPI_RSER
, SPI_RSER_TCFQE
);
718 dspi_tcfq_write(dspi
);
721 regmap_write(dspi
->regmap
, SPI_RSER
,
722 SPI_RSER_TFFFE
| SPI_RSER_TFFFD
|
723 SPI_RSER_RFDFE
| SPI_RSER_RFDFD
);
724 status
= dspi_dma_xfer(dspi
);
727 dev_err(&dspi
->pdev
->dev
, "unsupported trans_mode %u\n",
733 if (trans_mode
!= DSPI_DMA_MODE
) {
734 if (wait_event_interruptible(dspi
->waitq
,
736 dev_err(&dspi
->pdev
->dev
,
737 "wait transfer complete fail!\n");
741 if (transfer
->delay_usecs
)
742 udelay(transfer
->delay_usecs
);
746 message
->status
= status
;
747 spi_finalize_current_message(master
);
752 static int dspi_setup(struct spi_device
*spi
)
754 struct chip_data
*chip
;
755 struct fsl_dspi
*dspi
= spi_master_get_devdata(spi
->master
);
756 struct fsl_dspi_platform_data
*pdata
;
757 u32 cs_sck_delay
= 0, sck_cs_delay
= 0;
758 unsigned char br
= 0, pbr
= 0, pcssck
= 0, cssck
= 0;
759 unsigned char pasc
= 0, asc
= 0;
760 unsigned long clkrate
;
762 /* Only alloc on first setup */
763 chip
= spi_get_ctldata(spi
);
765 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
770 pdata
= dev_get_platdata(&dspi
->pdev
->dev
);
773 of_property_read_u32(spi
->dev
.of_node
, "fsl,spi-cs-sck-delay",
776 of_property_read_u32(spi
->dev
.of_node
, "fsl,spi-sck-cs-delay",
779 cs_sck_delay
= pdata
->cs_sck_delay
;
780 sck_cs_delay
= pdata
->sck_cs_delay
;
783 chip
->void_write_data
= 0;
785 clkrate
= clk_get_rate(dspi
->clk
);
786 hz_to_spi_baud(&pbr
, &br
, spi
->max_speed_hz
, clkrate
);
788 /* Set PCS to SCK delay scale values */
789 ns_delay_scale(&pcssck
, &cssck
, cs_sck_delay
, clkrate
);
791 /* Set After SCK delay scale values */
792 ns_delay_scale(&pasc
, &asc
, sck_cs_delay
, clkrate
);
794 chip
->ctar_val
= SPI_CTAR_CPOL(spi
->mode
& SPI_CPOL
? 1 : 0)
795 | SPI_CTAR_CPHA(spi
->mode
& SPI_CPHA
? 1 : 0)
796 | SPI_CTAR_LSBFE(spi
->mode
& SPI_LSB_FIRST
? 1 : 0)
797 | SPI_CTAR_PCSSCK(pcssck
)
798 | SPI_CTAR_CSSCK(cssck
)
799 | SPI_CTAR_PASC(pasc
)
804 spi_set_ctldata(spi
, chip
);
809 static void dspi_cleanup(struct spi_device
*spi
)
811 struct chip_data
*chip
= spi_get_ctldata((struct spi_device
*)spi
);
813 dev_dbg(&spi
->dev
, "spi_device %u.%u cleanup\n",
814 spi
->master
->bus_num
, spi
->chip_select
);
819 static irqreturn_t
dspi_interrupt(int irq
, void *dev_id
)
821 struct fsl_dspi
*dspi
= (struct fsl_dspi
*)dev_id
;
822 struct spi_message
*msg
= dspi
->cur_msg
;
823 enum dspi_trans_mode trans_mode
;
827 regmap_read(dspi
->regmap
, SPI_SR
, &spi_sr
);
828 regmap_write(dspi
->regmap
, SPI_SR
, spi_sr
);
831 if (spi_sr
& (SPI_SR_EOQF
| SPI_SR_TCFQF
)) {
832 /* Get transfer counter (in number of SPI transfers). It was
833 * reset to 0 when transfer(s) were started.
835 regmap_read(dspi
->regmap
, SPI_TCR
, &spi_tcr
);
836 spi_tcnt
= SPI_TCR_GET_TCNT(spi_tcr
);
837 /* Update total number of bytes that were transferred */
838 msg
->actual_length
+= spi_tcnt
* dspi
->bytes_per_word
;
840 trans_mode
= dspi
->devtype_data
->trans_mode
;
841 switch (trans_mode
) {
846 dspi_tcfq_read(dspi
);
849 dev_err(&dspi
->pdev
->dev
, "unsupported trans_mode %u\n",
856 wake_up_interruptible(&dspi
->waitq
);
858 switch (trans_mode
) {
860 dspi_eoq_write(dspi
);
863 dspi_tcfq_write(dspi
);
866 dev_err(&dspi
->pdev
->dev
,
867 "unsupported trans_mode %u\n",
876 static const struct of_device_id fsl_dspi_dt_ids
[] = {
877 { .compatible
= "fsl,vf610-dspi", .data
= &vf610_data
, },
878 { .compatible
= "fsl,ls1021a-v1.0-dspi", .data
= &ls1021a_v1_data
, },
879 { .compatible
= "fsl,ls2085a-dspi", .data
= &ls2085a_data
, },
882 MODULE_DEVICE_TABLE(of
, fsl_dspi_dt_ids
);
884 #ifdef CONFIG_PM_SLEEP
885 static int dspi_suspend(struct device
*dev
)
887 struct spi_master
*master
= dev_get_drvdata(dev
);
888 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
890 spi_master_suspend(master
);
891 clk_disable_unprepare(dspi
->clk
);
893 pinctrl_pm_select_sleep_state(dev
);
898 static int dspi_resume(struct device
*dev
)
900 struct spi_master
*master
= dev_get_drvdata(dev
);
901 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
904 pinctrl_pm_select_default_state(dev
);
906 ret
= clk_prepare_enable(dspi
->clk
);
909 spi_master_resume(master
);
913 #endif /* CONFIG_PM_SLEEP */
915 static SIMPLE_DEV_PM_OPS(dspi_pm
, dspi_suspend
, dspi_resume
);
917 static const struct regmap_range dspi_volatile_ranges
[] = {
918 regmap_reg_range(SPI_MCR
, SPI_TCR
),
919 regmap_reg_range(SPI_SR
, SPI_SR
),
920 regmap_reg_range(SPI_PUSHR
, SPI_RXFR3
),
923 static const struct regmap_access_table dspi_volatile_table
= {
924 .yes_ranges
= dspi_volatile_ranges
,
925 .n_yes_ranges
= ARRAY_SIZE(dspi_volatile_ranges
),
928 static const struct regmap_config dspi_regmap_config
= {
932 .max_register
= 0x88,
933 .volatile_table
= &dspi_volatile_table
,
936 static const struct regmap_range dspi_xspi_volatile_ranges
[] = {
937 regmap_reg_range(SPI_MCR
, SPI_TCR
),
938 regmap_reg_range(SPI_SR
, SPI_SR
),
939 regmap_reg_range(SPI_PUSHR
, SPI_RXFR3
),
940 regmap_reg_range(SPI_SREX
, SPI_SREX
),
943 static const struct regmap_access_table dspi_xspi_volatile_table
= {
944 .yes_ranges
= dspi_xspi_volatile_ranges
,
945 .n_yes_ranges
= ARRAY_SIZE(dspi_xspi_volatile_ranges
),
948 static const struct regmap_config dspi_xspi_regmap_config
[] = {
953 .max_register
= 0x13c,
954 .volatile_table
= &dspi_xspi_volatile_table
,
965 static void dspi_init(struct fsl_dspi
*dspi
)
967 regmap_write(dspi
->regmap
, SPI_MCR
, SPI_MCR_MASTER
| SPI_MCR_PCSIS
|
968 (dspi
->devtype_data
->xspi_mode
? SPI_MCR_XSPI
: 0));
969 regmap_write(dspi
->regmap
, SPI_SR
, SPI_SR_CLEAR
);
970 if (dspi
->devtype_data
->xspi_mode
)
971 regmap_write(dspi
->regmap
, SPI_CTARE(0),
972 SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
975 static int dspi_probe(struct platform_device
*pdev
)
977 struct device_node
*np
= pdev
->dev
.of_node
;
978 struct spi_master
*master
;
979 struct fsl_dspi
*dspi
;
980 struct resource
*res
;
981 const struct regmap_config
*regmap_config
;
983 struct fsl_dspi_platform_data
*pdata
;
984 int ret
= 0, cs_num
, bus_num
;
986 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct fsl_dspi
));
990 dspi
= spi_master_get_devdata(master
);
992 dspi
->master
= master
;
994 master
->transfer
= NULL
;
995 master
->setup
= dspi_setup
;
996 master
->transfer_one_message
= dspi_transfer_one_message
;
997 master
->dev
.of_node
= pdev
->dev
.of_node
;
999 master
->cleanup
= dspi_cleanup
;
1000 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
1002 pdata
= dev_get_platdata(&pdev
->dev
);
1004 master
->num_chipselect
= pdata
->cs_num
;
1005 master
->bus_num
= pdata
->bus_num
;
1007 dspi
->devtype_data
= &coldfire_data
;
1010 ret
= of_property_read_u32(np
, "spi-num-chipselects", &cs_num
);
1012 dev_err(&pdev
->dev
, "can't get spi-num-chipselects\n");
1013 goto out_master_put
;
1015 master
->num_chipselect
= cs_num
;
1017 ret
= of_property_read_u32(np
, "bus-num", &bus_num
);
1019 dev_err(&pdev
->dev
, "can't get bus-num\n");
1020 goto out_master_put
;
1022 master
->bus_num
= bus_num
;
1024 dspi
->devtype_data
= of_device_get_match_data(&pdev
->dev
);
1025 if (!dspi
->devtype_data
) {
1026 dev_err(&pdev
->dev
, "can't get devtype_data\n");
1028 goto out_master_put
;
1032 if (dspi
->devtype_data
->xspi_mode
)
1033 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 32);
1035 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
1037 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1038 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1040 ret
= PTR_ERR(base
);
1041 goto out_master_put
;
1044 if (dspi
->devtype_data
->xspi_mode
)
1045 regmap_config
= &dspi_xspi_regmap_config
[0];
1047 regmap_config
= &dspi_regmap_config
;
1048 dspi
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
, regmap_config
);
1049 if (IS_ERR(dspi
->regmap
)) {
1050 dev_err(&pdev
->dev
, "failed to init regmap: %ld\n",
1051 PTR_ERR(dspi
->regmap
));
1052 ret
= PTR_ERR(dspi
->regmap
);
1053 goto out_master_put
;
1056 if (dspi
->devtype_data
->xspi_mode
) {
1057 dspi
->regmap_pushr
= devm_regmap_init_mmio(
1058 &pdev
->dev
, base
+ SPI_PUSHR
,
1059 &dspi_xspi_regmap_config
[1]);
1060 if (IS_ERR(dspi
->regmap_pushr
)) {
1062 "failed to init pushr regmap: %ld\n",
1063 PTR_ERR(dspi
->regmap_pushr
));
1064 ret
= PTR_ERR(dspi
->regmap_pushr
);
1065 goto out_master_put
;
1069 dspi
->clk
= devm_clk_get(&pdev
->dev
, "dspi");
1070 if (IS_ERR(dspi
->clk
)) {
1071 ret
= PTR_ERR(dspi
->clk
);
1072 dev_err(&pdev
->dev
, "unable to get clock\n");
1073 goto out_master_put
;
1075 ret
= clk_prepare_enable(dspi
->clk
);
1077 goto out_master_put
;
1080 dspi
->irq
= platform_get_irq(pdev
, 0);
1081 if (dspi
->irq
< 0) {
1082 dev_err(&pdev
->dev
, "can't get platform irq\n");
1087 ret
= devm_request_irq(&pdev
->dev
, dspi
->irq
, dspi_interrupt
, 0,
1090 dev_err(&pdev
->dev
, "Unable to attach DSPI interrupt\n");
1094 if (dspi
->devtype_data
->trans_mode
== DSPI_DMA_MODE
) {
1095 ret
= dspi_request_dma(dspi
, res
->start
);
1097 dev_err(&pdev
->dev
, "can't get dma channels\n");
1102 master
->max_speed_hz
=
1103 clk_get_rate(dspi
->clk
) / dspi
->devtype_data
->max_clock_factor
;
1105 init_waitqueue_head(&dspi
->waitq
);
1106 platform_set_drvdata(pdev
, master
);
1108 ret
= spi_register_master(master
);
1110 dev_err(&pdev
->dev
, "Problem registering DSPI master\n");
1117 clk_disable_unprepare(dspi
->clk
);
1119 spi_master_put(master
);
1124 static int dspi_remove(struct platform_device
*pdev
)
1126 struct spi_master
*master
= platform_get_drvdata(pdev
);
1127 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
1129 /* Disconnect from the SPI framework */
1130 dspi_release_dma(dspi
);
1131 clk_disable_unprepare(dspi
->clk
);
1132 spi_unregister_master(dspi
->master
);
1137 static struct platform_driver fsl_dspi_driver
= {
1138 .driver
.name
= DRIVER_NAME
,
1139 .driver
.of_match_table
= fsl_dspi_dt_ids
,
1140 .driver
.owner
= THIS_MODULE
,
1141 .driver
.pm
= &dspi_pm
,
1142 .probe
= dspi_probe
,
1143 .remove
= dspi_remove
,
1145 module_platform_driver(fsl_dspi_driver
);
1147 MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1148 MODULE_LICENSE("GPL");
1149 MODULE_ALIAS("platform:" DRIVER_NAME
);