1 // SPDX-License-Identifier: GPL-2.0+
3 // Freescale MXS SPI master driver
5 // Copyright 2012 DENX Software Engineering, GmbH.
6 // Copyright 2012 Freescale Semiconductor, Inc.
7 // Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 // Rework and transition to new API by:
10 // Marek Vasut <marex@denx.de>
12 // Based on previous attempt by:
13 // Fabio Estevam <fabio.estevam@freescale.com>
15 // Based on code from U-Boot bootloader by:
16 // Marek Vasut <marex@denx.de>
18 // Based on spi-stmp.c, which is:
19 // Author: Dmitry Pervushin <dimka@embeddedalley.com>
21 #include <linux/kernel.h>
22 #include <linux/ioport.h>
24 #include <linux/of_device.h>
25 #include <linux/of_gpio.h>
26 #include <linux/platform_device.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/dmaengine.h>
31 #include <linux/highmem.h>
32 #include <linux/clk.h>
33 #include <linux/err.h>
34 #include <linux/completion.h>
35 #include <linux/gpio.h>
36 #include <linux/regulator/consumer.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/module.h>
39 #include <linux/stmp_device.h>
40 #include <linux/spi/spi.h>
41 #include <linux/spi/mxs-spi.h>
43 #define DRIVER_NAME "mxs-spi"
45 /* Use 10S timeout for very long transfers, it should suffice. */
46 #define SSP_TIMEOUT 10000
48 #define SG_MAXLEN 0xff00
51 * Flags for txrx functions. More efficient that using an argument register for
54 #define TXRX_WRITE (1<<0) /* This is a write */
55 #define TXRX_DEASSERT_CS (1<<1) /* De-assert CS at end of txrx */
60 unsigned int sck
; /* Rate requested (vs actual) */
63 static int mxs_spi_setup_transfer(struct spi_device
*dev
,
64 const struct spi_transfer
*t
)
66 struct mxs_spi
*spi
= spi_master_get_devdata(dev
->master
);
67 struct mxs_ssp
*ssp
= &spi
->ssp
;
68 const unsigned int hz
= min(dev
->max_speed_hz
, t
->speed_hz
);
71 dev_err(&dev
->dev
, "SPI clock rate of zero not allowed\n");
76 mxs_ssp_set_clk_rate(ssp
, hz
);
78 * Save requested rate, hz, rather than the actual rate,
79 * ssp->clk_rate. Otherwise we would set the rate every transfer
80 * when the actual rate is not quite the same as requested rate.
84 * Perhaps we should return an error if the actual clock is
85 * nowhere close to what was requested?
89 writel(BM_SSP_CTRL0_LOCK_CS
,
90 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
92 writel(BF_SSP_CTRL1_SSP_MODE(BV_SSP_CTRL1_SSP_MODE__SPI
) |
93 BF_SSP_CTRL1_WORD_LENGTH(BV_SSP_CTRL1_WORD_LENGTH__EIGHT_BITS
) |
94 ((dev
->mode
& SPI_CPOL
) ? BM_SSP_CTRL1_POLARITY
: 0) |
95 ((dev
->mode
& SPI_CPHA
) ? BM_SSP_CTRL1_PHASE
: 0),
96 ssp
->base
+ HW_SSP_CTRL1(ssp
));
98 writel(0x0, ssp
->base
+ HW_SSP_CMD0
);
99 writel(0x0, ssp
->base
+ HW_SSP_CMD1
);
104 static u32
mxs_spi_cs_to_reg(unsigned cs
)
109 * i.MX28 Datasheet: 17.10.1: HW_SSP_CTRL0
111 * The bits BM_SSP_CTRL0_WAIT_FOR_CMD and BM_SSP_CTRL0_WAIT_FOR_IRQ
112 * in HW_SSP_CTRL0 register do have multiple usage, please refer to
113 * the datasheet for further details. In SPI mode, they are used to
114 * toggle the chip-select lines (nCS pins).
117 select
|= BM_SSP_CTRL0_WAIT_FOR_CMD
;
119 select
|= BM_SSP_CTRL0_WAIT_FOR_IRQ
;
124 static int mxs_ssp_wait(struct mxs_spi
*spi
, int offset
, int mask
, bool set
)
126 const unsigned long timeout
= jiffies
+ msecs_to_jiffies(SSP_TIMEOUT
);
127 struct mxs_ssp
*ssp
= &spi
->ssp
;
131 reg
= readl_relaxed(ssp
->base
+ offset
);
140 } while (time_before(jiffies
, timeout
));
145 static void mxs_ssp_dma_irq_callback(void *param
)
147 struct mxs_spi
*spi
= param
;
152 static irqreturn_t
mxs_ssp_irq_handler(int irq
, void *dev_id
)
154 struct mxs_ssp
*ssp
= dev_id
;
156 dev_err(ssp
->dev
, "%s[%i] CTRL1=%08x STATUS=%08x\n",
158 readl(ssp
->base
+ HW_SSP_CTRL1(ssp
)),
159 readl(ssp
->base
+ HW_SSP_STATUS(ssp
)));
163 static int mxs_spi_txrx_dma(struct mxs_spi
*spi
,
164 unsigned char *buf
, int len
,
167 struct mxs_ssp
*ssp
= &spi
->ssp
;
168 struct dma_async_tx_descriptor
*desc
= NULL
;
169 const bool vmalloced_buf
= is_vmalloc_addr(buf
);
170 const int desc_len
= vmalloced_buf
? PAGE_SIZE
: SG_MAXLEN
;
171 const int sgs
= DIV_ROUND_UP(len
, desc_len
);
175 struct page
*vm_page
;
178 struct scatterlist sg
;
184 dma_xfer
= kcalloc(sgs
, sizeof(*dma_xfer
), GFP_KERNEL
);
188 reinit_completion(&spi
->c
);
190 /* Chip select was already programmed into CTRL0 */
191 ctrl0
= readl(ssp
->base
+ HW_SSP_CTRL0
);
192 ctrl0
&= ~(BM_SSP_CTRL0_XFER_COUNT
| BM_SSP_CTRL0_IGNORE_CRC
|
194 ctrl0
|= BM_SSP_CTRL0_DATA_XFER
;
196 if (!(flags
& TXRX_WRITE
))
197 ctrl0
|= BM_SSP_CTRL0_READ
;
199 /* Queue the DMA data transfer. */
200 for (sg_count
= 0; sg_count
< sgs
; sg_count
++) {
201 /* Prepare the transfer descriptor. */
202 min
= min(len
, desc_len
);
205 * De-assert CS on last segment if flag is set (i.e., no more
206 * transfers will follow)
208 if ((sg_count
+ 1 == sgs
) && (flags
& TXRX_DEASSERT_CS
))
209 ctrl0
|= BM_SSP_CTRL0_IGNORE_CRC
;
211 if (ssp
->devid
== IMX23_SSP
) {
212 ctrl0
&= ~BM_SSP_CTRL0_XFER_COUNT
;
216 dma_xfer
[sg_count
].pio
[0] = ctrl0
;
217 dma_xfer
[sg_count
].pio
[3] = min
;
220 vm_page
= vmalloc_to_page(buf
);
226 sg_init_table(&dma_xfer
[sg_count
].sg
, 1);
227 sg_set_page(&dma_xfer
[sg_count
].sg
, vm_page
,
228 min
, offset_in_page(buf
));
230 sg_init_one(&dma_xfer
[sg_count
].sg
, buf
, min
);
233 ret
= dma_map_sg(ssp
->dev
, &dma_xfer
[sg_count
].sg
, 1,
234 (flags
& TXRX_WRITE
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
239 /* Queue the PIO register write transfer. */
240 desc
= dmaengine_prep_slave_sg(ssp
->dmach
,
241 (struct scatterlist
*)dma_xfer
[sg_count
].pio
,
242 (ssp
->devid
== IMX23_SSP
) ? 1 : 4,
244 sg_count
? DMA_PREP_INTERRUPT
: 0);
247 "Failed to get PIO reg. write descriptor.\n");
252 desc
= dmaengine_prep_slave_sg(ssp
->dmach
,
253 &dma_xfer
[sg_count
].sg
, 1,
254 (flags
& TXRX_WRITE
) ? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
,
255 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
259 "Failed to get DMA data write descriptor.\n");
266 * The last descriptor must have this callback,
267 * to finish the DMA transaction.
269 desc
->callback
= mxs_ssp_dma_irq_callback
;
270 desc
->callback_param
= spi
;
272 /* Start the transfer. */
273 dmaengine_submit(desc
);
274 dma_async_issue_pending(ssp
->dmach
);
276 if (!wait_for_completion_timeout(&spi
->c
,
277 msecs_to_jiffies(SSP_TIMEOUT
))) {
278 dev_err(ssp
->dev
, "DMA transfer timeout\n");
280 dmaengine_terminate_all(ssp
->dmach
);
287 while (--sg_count
>= 0) {
289 dma_unmap_sg(ssp
->dev
, &dma_xfer
[sg_count
].sg
, 1,
290 (flags
& TXRX_WRITE
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
298 static int mxs_spi_txrx_pio(struct mxs_spi
*spi
,
299 unsigned char *buf
, int len
,
302 struct mxs_ssp
*ssp
= &spi
->ssp
;
304 writel(BM_SSP_CTRL0_IGNORE_CRC
,
305 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_CLR
);
308 if (len
== 0 && (flags
& TXRX_DEASSERT_CS
))
309 writel(BM_SSP_CTRL0_IGNORE_CRC
,
310 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
312 if (ssp
->devid
== IMX23_SSP
) {
313 writel(BM_SSP_CTRL0_XFER_COUNT
,
314 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_CLR
);
316 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
318 writel(1, ssp
->base
+ HW_SSP_XFER_SIZE
);
321 if (flags
& TXRX_WRITE
)
322 writel(BM_SSP_CTRL0_READ
,
323 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_CLR
);
325 writel(BM_SSP_CTRL0_READ
,
326 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
328 writel(BM_SSP_CTRL0_RUN
,
329 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
331 if (mxs_ssp_wait(spi
, HW_SSP_CTRL0
, BM_SSP_CTRL0_RUN
, 1))
334 if (flags
& TXRX_WRITE
)
335 writel(*buf
, ssp
->base
+ HW_SSP_DATA(ssp
));
337 writel(BM_SSP_CTRL0_DATA_XFER
,
338 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
340 if (!(flags
& TXRX_WRITE
)) {
341 if (mxs_ssp_wait(spi
, HW_SSP_STATUS(ssp
),
342 BM_SSP_STATUS_FIFO_EMPTY
, 0))
345 *buf
= (readl(ssp
->base
+ HW_SSP_DATA(ssp
)) & 0xff);
348 if (mxs_ssp_wait(spi
, HW_SSP_CTRL0
, BM_SSP_CTRL0_RUN
, 0))
360 static int mxs_spi_transfer_one(struct spi_master
*master
,
361 struct spi_message
*m
)
363 struct mxs_spi
*spi
= spi_master_get_devdata(master
);
364 struct mxs_ssp
*ssp
= &spi
->ssp
;
365 struct spi_transfer
*t
;
369 /* Program CS register bits here, it will be used for all transfers. */
370 writel(BM_SSP_CTRL0_WAIT_FOR_CMD
| BM_SSP_CTRL0_WAIT_FOR_IRQ
,
371 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_CLR
);
372 writel(mxs_spi_cs_to_reg(m
->spi
->chip_select
),
373 ssp
->base
+ HW_SSP_CTRL0
+ STMP_OFFSET_REG_SET
);
375 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
377 status
= mxs_spi_setup_transfer(m
->spi
, t
);
381 /* De-assert on last transfer, inverted by cs_change flag */
382 flag
= (&t
->transfer_list
== m
->transfers
.prev
) ^ t
->cs_change
?
383 TXRX_DEASSERT_CS
: 0;
386 * Small blocks can be transfered via PIO.
387 * Measured by empiric means:
389 * dd if=/dev/mtdblock0 of=/dev/null bs=1024k count=1
391 * DMA only: 2.164808 seconds, 473.0KB/s
392 * Combined: 1.676276 seconds, 610.9KB/s
395 writel(BM_SSP_CTRL1_DMA_ENABLE
,
396 ssp
->base
+ HW_SSP_CTRL1(ssp
) +
397 STMP_OFFSET_REG_CLR
);
400 status
= mxs_spi_txrx_pio(spi
,
402 t
->len
, flag
| TXRX_WRITE
);
404 status
= mxs_spi_txrx_pio(spi
,
408 writel(BM_SSP_CTRL1_DMA_ENABLE
,
409 ssp
->base
+ HW_SSP_CTRL1(ssp
) +
410 STMP_OFFSET_REG_SET
);
413 status
= mxs_spi_txrx_dma(spi
,
414 (void *)t
->tx_buf
, t
->len
,
417 status
= mxs_spi_txrx_dma(spi
,
423 stmp_reset_block(ssp
->base
);
427 m
->actual_length
+= t
->len
;
431 spi_finalize_current_message(master
);
436 static int mxs_spi_runtime_suspend(struct device
*dev
)
438 struct spi_master
*master
= dev_get_drvdata(dev
);
439 struct mxs_spi
*spi
= spi_master_get_devdata(master
);
440 struct mxs_ssp
*ssp
= &spi
->ssp
;
443 clk_disable_unprepare(ssp
->clk
);
445 ret
= pinctrl_pm_select_idle_state(dev
);
447 int ret2
= clk_prepare_enable(ssp
->clk
);
450 dev_warn(dev
, "Failed to reenable clock after failing pinctrl request (pinctrl: %d, clk: %d)\n",
457 static int mxs_spi_runtime_resume(struct device
*dev
)
459 struct spi_master
*master
= dev_get_drvdata(dev
);
460 struct mxs_spi
*spi
= spi_master_get_devdata(master
);
461 struct mxs_ssp
*ssp
= &spi
->ssp
;
464 ret
= pinctrl_pm_select_default_state(dev
);
468 ret
= clk_prepare_enable(ssp
->clk
);
470 pinctrl_pm_select_idle_state(dev
);
475 static int __maybe_unused
mxs_spi_suspend(struct device
*dev
)
477 struct spi_master
*master
= dev_get_drvdata(dev
);
480 ret
= spi_master_suspend(master
);
484 if (!pm_runtime_suspended(dev
))
485 return mxs_spi_runtime_suspend(dev
);
490 static int __maybe_unused
mxs_spi_resume(struct device
*dev
)
492 struct spi_master
*master
= dev_get_drvdata(dev
);
495 if (!pm_runtime_suspended(dev
))
496 ret
= mxs_spi_runtime_resume(dev
);
502 ret
= spi_master_resume(master
);
503 if (ret
< 0 && !pm_runtime_suspended(dev
))
504 mxs_spi_runtime_suspend(dev
);
509 static const struct dev_pm_ops mxs_spi_pm
= {
510 SET_RUNTIME_PM_OPS(mxs_spi_runtime_suspend
,
511 mxs_spi_runtime_resume
, NULL
)
512 SET_SYSTEM_SLEEP_PM_OPS(mxs_spi_suspend
, mxs_spi_resume
)
515 static const struct of_device_id mxs_spi_dt_ids
[] = {
516 { .compatible
= "fsl,imx23-spi", .data
= (void *) IMX23_SSP
, },
517 { .compatible
= "fsl,imx28-spi", .data
= (void *) IMX28_SSP
, },
520 MODULE_DEVICE_TABLE(of
, mxs_spi_dt_ids
);
522 static int mxs_spi_probe(struct platform_device
*pdev
)
524 const struct of_device_id
*of_id
=
525 of_match_device(mxs_spi_dt_ids
, &pdev
->dev
);
526 struct device_node
*np
= pdev
->dev
.of_node
;
527 struct spi_master
*master
;
530 struct resource
*iores
;
534 int ret
= 0, irq_err
;
537 * Default clock speed for the SPI core. 160MHz seems to
538 * work reasonably well with most SPI flashes, so use this
539 * as a default. Override with "clock-frequency" DT prop.
541 const int clk_freq_default
= 160000000;
543 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
544 irq_err
= platform_get_irq(pdev
, 0);
548 base
= devm_ioremap_resource(&pdev
->dev
, iores
);
550 return PTR_ERR(base
);
552 clk
= devm_clk_get(&pdev
->dev
, NULL
);
556 devid
= (enum mxs_ssp_id
) of_id
->data
;
557 ret
= of_property_read_u32(np
, "clock-frequency",
560 clk_freq
= clk_freq_default
;
562 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spi
));
566 platform_set_drvdata(pdev
, master
);
568 master
->transfer_one_message
= mxs_spi_transfer_one
;
569 master
->bits_per_word_mask
= SPI_BPW_MASK(8);
570 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
571 master
->num_chipselect
= 3;
572 master
->dev
.of_node
= np
;
573 master
->flags
= SPI_MASTER_HALF_DUPLEX
;
574 master
->auto_runtime_pm
= true;
576 spi
= spi_master_get_devdata(master
);
578 ssp
->dev
= &pdev
->dev
;
583 init_completion(&spi
->c
);
585 ret
= devm_request_irq(&pdev
->dev
, irq_err
, mxs_ssp_irq_handler
, 0,
586 dev_name(&pdev
->dev
), ssp
);
588 goto out_master_free
;
590 ssp
->dmach
= dma_request_slave_channel(&pdev
->dev
, "rx-tx");
592 dev_err(ssp
->dev
, "Failed to request DMA\n");
594 goto out_master_free
;
597 pm_runtime_enable(ssp
->dev
);
598 if (!pm_runtime_enabled(ssp
->dev
)) {
599 ret
= mxs_spi_runtime_resume(ssp
->dev
);
601 dev_err(ssp
->dev
, "runtime resume failed\n");
602 goto out_dma_release
;
606 ret
= pm_runtime_get_sync(ssp
->dev
);
608 dev_err(ssp
->dev
, "runtime_get_sync failed\n");
609 goto out_pm_runtime_disable
;
612 clk_set_rate(ssp
->clk
, clk_freq
);
614 ret
= stmp_reset_block(ssp
->base
);
616 goto out_pm_runtime_put
;
618 ret
= devm_spi_register_master(&pdev
->dev
, master
);
620 dev_err(&pdev
->dev
, "Cannot register SPI master, %d\n", ret
);
621 goto out_pm_runtime_put
;
624 pm_runtime_put(ssp
->dev
);
629 pm_runtime_put(ssp
->dev
);
630 out_pm_runtime_disable
:
631 pm_runtime_disable(ssp
->dev
);
633 dma_release_channel(ssp
->dmach
);
635 spi_master_put(master
);
639 static int mxs_spi_remove(struct platform_device
*pdev
)
641 struct spi_master
*master
;
645 master
= platform_get_drvdata(pdev
);
646 spi
= spi_master_get_devdata(master
);
649 pm_runtime_disable(&pdev
->dev
);
650 if (!pm_runtime_status_suspended(&pdev
->dev
))
651 mxs_spi_runtime_suspend(&pdev
->dev
);
653 dma_release_channel(ssp
->dmach
);
658 static struct platform_driver mxs_spi_driver
= {
659 .probe
= mxs_spi_probe
,
660 .remove
= mxs_spi_remove
,
663 .of_match_table
= mxs_spi_dt_ids
,
668 module_platform_driver(mxs_spi_driver
);
670 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
671 MODULE_DESCRIPTION("MXS SPI master driver");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:mxs-spi");