1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2018 MediaTek Inc.
5 #include <linux/device.h>
6 #include <linux/dma-mapping.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/spi/spi.h>
16 #define SPIS_IRQ_EN_REG 0x0
17 #define SPIS_IRQ_CLR_REG 0x4
18 #define SPIS_IRQ_ST_REG 0x8
19 #define SPIS_IRQ_MASK_REG 0xc
20 #define SPIS_CFG_REG 0x10
21 #define SPIS_RX_DATA_REG 0x14
22 #define SPIS_TX_DATA_REG 0x18
23 #define SPIS_RX_DST_REG 0x1c
24 #define SPIS_TX_SRC_REG 0x20
25 #define SPIS_DMA_CFG_REG 0x30
26 #define SPIS_SOFT_RST_REG 0x40
29 #define DMA_DONE_EN BIT(7)
30 #define DATA_DONE_EN BIT(2)
31 #define RSTA_DONE_EN BIT(1)
32 #define CMD_INVALID_EN BIT(0)
35 #define DMA_DONE_ST BIT(7)
36 #define DATA_DONE_ST BIT(2)
37 #define RSTA_DONE_ST BIT(1)
38 #define CMD_INVALID_ST BIT(0)
40 /* SPIS_IRQ_MASK_REG */
41 #define DMA_DONE_MASK BIT(7)
42 #define DATA_DONE_MASK BIT(2)
43 #define RSTA_DONE_MASK BIT(1)
44 #define CMD_INVALID_MASK BIT(0)
47 #define SPIS_TX_ENDIAN BIT(7)
48 #define SPIS_RX_ENDIAN BIT(6)
49 #define SPIS_TXMSBF BIT(5)
50 #define SPIS_RXMSBF BIT(4)
51 #define SPIS_CPHA BIT(3)
52 #define SPIS_CPOL BIT(2)
53 #define SPIS_TX_EN BIT(1)
54 #define SPIS_RX_EN BIT(0)
56 /* SPIS_DMA_CFG_REG */
57 #define TX_DMA_TRIG_EN BIT(31)
58 #define TX_DMA_EN BIT(30)
59 #define RX_DMA_EN BIT(29)
60 #define TX_DMA_LEN 0xfffff
62 /* SPIS_SOFT_RST_REG */
63 #define SPIS_DMA_ADDR_EN BIT(1)
64 #define SPIS_SOFT_RST BIT(0)
66 struct mtk_spi_slave {
70 struct completion xfer_done;
71 struct spi_transfer *cur_transfer;
73 const struct mtk_spi_compatible *dev_comp;
76 struct mtk_spi_compatible {
77 const u32 max_fifo_size;
81 static const struct mtk_spi_compatible mt2712_compat = {
84 static const struct mtk_spi_compatible mt8195_compat = {
89 static const struct of_device_id mtk_spi_slave_of_match[] = {
90 { .compatible = "mediatek,mt2712-spi-slave",
91 .data = (void *)&mt2712_compat,},
92 { .compatible = "mediatek,mt8195-spi-slave",
93 .data = (void *)&mt8195_compat,},
96 MODULE_DEVICE_TABLE(of, mtk_spi_slave_of_match);
98 static void mtk_spi_slave_disable_dma(struct mtk_spi_slave *mdata)
102 reg_val = readl(mdata->base + SPIS_DMA_CFG_REG);
103 reg_val &= ~RX_DMA_EN;
104 reg_val &= ~TX_DMA_EN;
105 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
108 static void mtk_spi_slave_disable_xfer(struct mtk_spi_slave *mdata)
112 reg_val = readl(mdata->base + SPIS_CFG_REG);
113 reg_val &= ~SPIS_TX_EN;
114 reg_val &= ~SPIS_RX_EN;
115 writel(reg_val, mdata->base + SPIS_CFG_REG);
118 static int mtk_spi_slave_wait_for_completion(struct mtk_spi_slave *mdata)
120 if (wait_for_completion_interruptible(&mdata->xfer_done) ||
121 mdata->slave_aborted) {
122 dev_err(mdata->dev, "interrupted\n");
129 static int mtk_spi_slave_prepare_message(struct spi_controller *ctlr,
130 struct spi_message *msg)
132 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
133 struct spi_device *spi = msg->spi;
137 cpha = spi->mode & SPI_CPHA ? 1 : 0;
138 cpol = spi->mode & SPI_CPOL ? 1 : 0;
140 reg_val = readl(mdata->base + SPIS_CFG_REG);
142 reg_val |= SPIS_CPHA;
144 reg_val &= ~SPIS_CPHA;
146 reg_val |= SPIS_CPOL;
148 reg_val &= ~SPIS_CPOL;
150 if (spi->mode & SPI_LSB_FIRST)
151 reg_val &= ~(SPIS_TXMSBF | SPIS_RXMSBF);
153 reg_val |= SPIS_TXMSBF | SPIS_RXMSBF;
155 reg_val &= ~SPIS_TX_ENDIAN;
156 reg_val &= ~SPIS_RX_ENDIAN;
157 writel(reg_val, mdata->base + SPIS_CFG_REG);
162 static int mtk_spi_slave_fifo_transfer(struct spi_controller *ctlr,
163 struct spi_device *spi,
164 struct spi_transfer *xfer)
166 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
167 int reg_val, cnt, remainder, ret;
169 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
171 reg_val = readl(mdata->base + SPIS_CFG_REG);
173 reg_val |= SPIS_RX_EN;
175 reg_val |= SPIS_TX_EN;
176 writel(reg_val, mdata->base + SPIS_CFG_REG);
180 iowrite32_rep(mdata->base + SPIS_TX_DATA_REG,
183 remainder = xfer->len % 4;
184 if (xfer->tx_buf && remainder > 0) {
186 memcpy(®_val, xfer->tx_buf + cnt * 4, remainder);
187 writel(reg_val, mdata->base + SPIS_TX_DATA_REG);
190 ret = mtk_spi_slave_wait_for_completion(mdata);
192 mtk_spi_slave_disable_xfer(mdata);
193 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
199 static int mtk_spi_slave_dma_transfer(struct spi_controller *ctlr,
200 struct spi_device *spi,
201 struct spi_transfer *xfer)
203 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
204 struct device *dev = mdata->dev;
207 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
210 /* tx_buf is a const void* where we need a void * for
213 void *nonconst_tx = (void *)xfer->tx_buf;
215 xfer->tx_dma = dma_map_single(dev, nonconst_tx,
216 xfer->len, DMA_TO_DEVICE);
217 if (dma_mapping_error(dev, xfer->tx_dma)) {
219 goto disable_transfer;
224 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
225 xfer->len, DMA_FROM_DEVICE);
226 if (dma_mapping_error(dev, xfer->rx_dma)) {
232 writel(xfer->tx_dma, mdata->base + SPIS_TX_SRC_REG);
233 writel(xfer->rx_dma, mdata->base + SPIS_RX_DST_REG);
235 writel(SPIS_DMA_ADDR_EN, mdata->base + SPIS_SOFT_RST_REG);
237 /* enable config reg tx rx_enable */
238 reg_val = readl(mdata->base + SPIS_CFG_REG);
240 reg_val |= SPIS_TX_EN;
242 reg_val |= SPIS_RX_EN;
243 writel(reg_val, mdata->base + SPIS_CFG_REG);
247 reg_val |= (xfer->len - 1) & TX_DMA_LEN;
248 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
250 reg_val = readl(mdata->base + SPIS_DMA_CFG_REG);
252 reg_val |= TX_DMA_EN;
254 reg_val |= RX_DMA_EN;
255 reg_val |= TX_DMA_TRIG_EN;
256 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
258 ret = mtk_spi_slave_wait_for_completion(mdata);
266 dma_unmap_single(dev, xfer->rx_dma,
267 xfer->len, DMA_FROM_DEVICE);
271 dma_unmap_single(dev, xfer->tx_dma,
272 xfer->len, DMA_TO_DEVICE);
275 mtk_spi_slave_disable_dma(mdata);
276 mtk_spi_slave_disable_xfer(mdata);
277 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
282 static int mtk_spi_slave_transfer_one(struct spi_controller *ctlr,
283 struct spi_device *spi,
284 struct spi_transfer *xfer)
286 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
288 reinit_completion(&mdata->xfer_done);
289 mdata->slave_aborted = false;
290 mdata->cur_transfer = xfer;
292 if (xfer->len > mdata->dev_comp->max_fifo_size)
293 return mtk_spi_slave_dma_transfer(ctlr, spi, xfer);
295 return mtk_spi_slave_fifo_transfer(ctlr, spi, xfer);
298 static int mtk_spi_slave_setup(struct spi_device *spi)
300 struct mtk_spi_slave *mdata = spi_controller_get_devdata(spi->master);
303 reg_val = DMA_DONE_EN | DATA_DONE_EN |
304 RSTA_DONE_EN | CMD_INVALID_EN;
305 writel(reg_val, mdata->base + SPIS_IRQ_EN_REG);
307 reg_val = DMA_DONE_MASK | DATA_DONE_MASK |
308 RSTA_DONE_MASK | CMD_INVALID_MASK;
309 writel(reg_val, mdata->base + SPIS_IRQ_MASK_REG);
311 mtk_spi_slave_disable_dma(mdata);
312 mtk_spi_slave_disable_xfer(mdata);
317 static int mtk_slave_abort(struct spi_controller *ctlr)
319 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
321 mdata->slave_aborted = true;
322 complete(&mdata->xfer_done);
327 static irqreturn_t mtk_spi_slave_interrupt(int irq, void *dev_id)
329 struct spi_controller *ctlr = dev_id;
330 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
331 struct spi_transfer *trans = mdata->cur_transfer;
332 u32 int_status, reg_val, cnt, remainder;
334 int_status = readl(mdata->base + SPIS_IRQ_ST_REG);
335 writel(int_status, mdata->base + SPIS_IRQ_CLR_REG);
340 if ((int_status & DMA_DONE_ST) &&
341 ((int_status & DATA_DONE_ST) ||
342 (int_status & RSTA_DONE_ST))) {
343 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
346 dma_unmap_single(mdata->dev, trans->tx_dma,
347 trans->len, DMA_TO_DEVICE);
349 dma_unmap_single(mdata->dev, trans->rx_dma,
350 trans->len, DMA_FROM_DEVICE);
352 mtk_spi_slave_disable_dma(mdata);
353 mtk_spi_slave_disable_xfer(mdata);
356 if ((!(int_status & DMA_DONE_ST)) &&
357 ((int_status & DATA_DONE_ST) ||
358 (int_status & RSTA_DONE_ST))) {
359 cnt = trans->len / 4;
361 ioread32_rep(mdata->base + SPIS_RX_DATA_REG,
363 remainder = trans->len % 4;
364 if (trans->rx_buf && remainder > 0) {
365 reg_val = readl(mdata->base + SPIS_RX_DATA_REG);
366 memcpy(trans->rx_buf + (cnt * 4),
367 ®_val, remainder);
370 mtk_spi_slave_disable_xfer(mdata);
373 if (int_status & CMD_INVALID_ST) {
374 dev_warn(&ctlr->dev, "cmd invalid\n");
378 mdata->cur_transfer = NULL;
379 complete(&mdata->xfer_done);
384 static int mtk_spi_slave_probe(struct platform_device *pdev)
386 struct spi_controller *ctlr;
387 struct mtk_spi_slave *mdata;
389 const struct of_device_id *of_id;
391 ctlr = spi_alloc_slave(&pdev->dev, sizeof(*mdata));
393 dev_err(&pdev->dev, "failed to alloc spi slave\n");
397 ctlr->auto_runtime_pm = true;
398 ctlr->dev.of_node = pdev->dev.of_node;
399 ctlr->mode_bits = SPI_CPOL | SPI_CPHA;
400 ctlr->mode_bits |= SPI_LSB_FIRST;
402 ctlr->prepare_message = mtk_spi_slave_prepare_message;
403 ctlr->transfer_one = mtk_spi_slave_transfer_one;
404 ctlr->setup = mtk_spi_slave_setup;
405 ctlr->slave_abort = mtk_slave_abort;
407 of_id = of_match_node(mtk_spi_slave_of_match, pdev->dev.of_node);
409 dev_err(&pdev->dev, "failed to probe of_node\n");
413 mdata = spi_controller_get_devdata(ctlr);
414 mdata->dev_comp = of_id->data;
416 if (mdata->dev_comp->must_rx)
417 ctlr->flags = SPI_MASTER_MUST_RX;
419 platform_set_drvdata(pdev, ctlr);
421 init_completion(&mdata->xfer_done);
422 mdata->dev = &pdev->dev;
423 mdata->base = devm_platform_ioremap_resource(pdev, 0);
424 if (IS_ERR(mdata->base)) {
425 ret = PTR_ERR(mdata->base);
429 irq = platform_get_irq(pdev, 0);
435 ret = devm_request_irq(&pdev->dev, irq, mtk_spi_slave_interrupt,
436 IRQF_TRIGGER_NONE, dev_name(&pdev->dev), ctlr);
438 dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
442 mdata->spi_clk = devm_clk_get(&pdev->dev, "spi");
443 if (IS_ERR(mdata->spi_clk)) {
444 ret = PTR_ERR(mdata->spi_clk);
445 dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
449 ret = clk_prepare_enable(mdata->spi_clk);
451 dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
455 pm_runtime_enable(&pdev->dev);
457 ret = devm_spi_register_controller(&pdev->dev, ctlr);
460 "failed to register slave controller(%d)\n", ret);
461 clk_disable_unprepare(mdata->spi_clk);
462 goto err_disable_runtime_pm;
465 clk_disable_unprepare(mdata->spi_clk);
469 err_disable_runtime_pm:
470 pm_runtime_disable(&pdev->dev);
472 spi_controller_put(ctlr);
477 static int mtk_spi_slave_remove(struct platform_device *pdev)
479 pm_runtime_disable(&pdev->dev);
484 #ifdef CONFIG_PM_SLEEP
485 static int mtk_spi_slave_suspend(struct device *dev)
487 struct spi_controller *ctlr = dev_get_drvdata(dev);
488 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
491 ret = spi_controller_suspend(ctlr);
495 if (!pm_runtime_suspended(dev))
496 clk_disable_unprepare(mdata->spi_clk);
501 static int mtk_spi_slave_resume(struct device *dev)
503 struct spi_controller *ctlr = dev_get_drvdata(dev);
504 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
507 if (!pm_runtime_suspended(dev)) {
508 ret = clk_prepare_enable(mdata->spi_clk);
510 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
515 ret = spi_controller_resume(ctlr);
517 clk_disable_unprepare(mdata->spi_clk);
521 #endif /* CONFIG_PM_SLEEP */
524 static int mtk_spi_slave_runtime_suspend(struct device *dev)
526 struct spi_controller *ctlr = dev_get_drvdata(dev);
527 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
529 clk_disable_unprepare(mdata->spi_clk);
534 static int mtk_spi_slave_runtime_resume(struct device *dev)
536 struct spi_controller *ctlr = dev_get_drvdata(dev);
537 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
540 ret = clk_prepare_enable(mdata->spi_clk);
542 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
548 #endif /* CONFIG_PM */
550 static const struct dev_pm_ops mtk_spi_slave_pm = {
551 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_slave_suspend, mtk_spi_slave_resume)
552 SET_RUNTIME_PM_OPS(mtk_spi_slave_runtime_suspend,
553 mtk_spi_slave_runtime_resume, NULL)
556 static struct platform_driver mtk_spi_slave_driver = {
558 .name = "mtk-spi-slave",
559 .pm = &mtk_spi_slave_pm,
560 .of_match_table = mtk_spi_slave_of_match,
562 .probe = mtk_spi_slave_probe,
563 .remove = mtk_spi_slave_remove,
566 module_platform_driver(mtk_spi_slave_driver);
568 MODULE_DESCRIPTION("MTK SPI Slave Controller driver");
569 MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
570 MODULE_LICENSE("GPL v2");
571 MODULE_ALIAS("platform:mtk-spi-slave");