blob: eb44e2a664d64dbf910e92007b9e420607f53fd1 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0+
2// Copyright (c) 2018 MediaTek Inc.
3
4#include <linux/clk.h>
5#include <linux/device.h>
6#include <linux/dma-mapping.h>
7#include <linux/err.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>
13
14#define SPIS_IRQ_EN_REG 0x0
15#define SPIS_IRQ_CLR_REG 0x4
16#define SPIS_IRQ_ST_REG 0x8
17#define SPIS_IRQ_MASK_REG 0xc
18#define SPIS_CFG_REG 0x10
19#define SPIS_RX_DATA_REG 0x14
20#define SPIS_TX_DATA_REG 0x18
21#define SPIS_RX_DST_REG 0x1c
22#define SPIS_TX_SRC_REG 0x20
23#define SPIS_DMA_CFG_REG 0x30
24#define SPIS_SOFT_RST_REG 0x40
25
26/* SPIS_IRQ_EN_REG */
27#define DMA_DONE_EN BIT(7)
28#define DATA_DONE_EN BIT(2)
29#define RSTA_DONE_EN BIT(1)
30#define CMD_INVALID_EN BIT(0)
31
32/* SPIS_IRQ_ST_REG */
33#define DMA_DONE_ST BIT(7)
34#define DATA_DONE_ST BIT(2)
35#define RSTA_DONE_ST BIT(1)
36#define CMD_INVALID_ST BIT(0)
37
38/* SPIS_IRQ_MASK_REG */
39#define DMA_DONE_MASK BIT(7)
40#define DATA_DONE_MASK BIT(2)
41#define RSTA_DONE_MASK BIT(1)
42#define CMD_INVALID_MASK BIT(0)
43
44/* SPIS_CFG_REG */
45#define SPIS_TX_ENDIAN BIT(7)
46#define SPIS_RX_ENDIAN BIT(6)
47#define SPIS_TXMSBF BIT(5)
48#define SPIS_RXMSBF BIT(4)
49#define SPIS_CPHA BIT(3)
50#define SPIS_CPOL BIT(2)
51#define SPIS_TX_EN BIT(1)
52#define SPIS_RX_EN BIT(0)
53
54/* SPIS_DMA_CFG_REG */
55#define TX_DMA_TRIG_EN BIT(31)
56#define TX_DMA_EN BIT(30)
57#define RX_DMA_EN BIT(29)
58#define TX_DMA_LEN 0xfffff
59
60/* SPIS_SOFT_RST_REG */
61#define SPIS_DMA_ADDR_EN BIT(1)
62#define SPIS_SOFT_RST BIT(0)
63
64#define MTK_SPI_SLAVE_MAX_FIFO_SIZE 512U
65
66struct mtk_spi_slave {
67 struct device *dev;
68 void __iomem *base;
69 struct clk *spis_clk;
70 struct completion xfer_done;
71 struct spi_transfer *cur_transfer;
72 bool slave_aborted;
73};
74
75static const struct of_device_id mtk_spi_slave_of_match[] = {
76 { .compatible = "mediatek,mt2712-spi-slave", },
77 { .compatible = "mediatek,mt2731-spi-slave", },
78 {}
79};
80MODULE_DEVICE_TABLE(of, mtk_spi_slave_of_match);
81
82static void mtk_spi_slave_disable_dma(struct mtk_spi_slave *mdata)
83{
84 u32 reg_val;
85
86 reg_val = readl(mdata->base + SPIS_DMA_CFG_REG);
87 reg_val &= ~RX_DMA_EN;
88 reg_val &= ~TX_DMA_EN;
89 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
90}
91
92static void mtk_spi_slave_disable_xfer(struct mtk_spi_slave *mdata)
93{
94 u32 reg_val;
95
96 reg_val = readl(mdata->base + SPIS_CFG_REG);
97 reg_val &= ~SPIS_TX_EN;
98 reg_val &= ~SPIS_RX_EN;
99 writel(reg_val, mdata->base + SPIS_CFG_REG);
100}
101
102static int mtk_spi_slave_wait_for_completion(struct mtk_spi_slave *mdata)
103{
104 if (wait_for_completion_interruptible(&mdata->xfer_done) ||
105 mdata->slave_aborted) {
106 dev_err(mdata->dev, "interrupted\n");
107 return -EINTR;
108 }
109
110 return 0;
111}
112
113static int mtk_spi_slave_prepare_message(struct spi_controller *ctlr,
114 struct spi_message *msg)
115{
116 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
117 struct spi_device *spi = msg->spi;
118 bool cpha, cpol;
119 u32 reg_val;
120
121 cpha = spi->mode & SPI_CPHA ? 1 : 0;
122 cpol = spi->mode & SPI_CPOL ? 1 : 0;
123
124 reg_val = readl(mdata->base + SPIS_CFG_REG);
125 if (cpha)
126 reg_val |= SPIS_CPHA;
127 else
128 reg_val &= ~SPIS_CPHA;
129 if (cpol)
130 reg_val |= SPIS_CPOL;
131 else
132 reg_val &= ~SPIS_CPOL;
133
134 if (spi->mode & SPI_LSB_FIRST)
135 reg_val &= ~(SPIS_TXMSBF | SPIS_RXMSBF);
136 else
137 reg_val |= SPIS_TXMSBF | SPIS_RXMSBF;
138
139 reg_val &= ~SPIS_TX_ENDIAN;
140 reg_val &= ~SPIS_RX_ENDIAN;
141 writel(reg_val, mdata->base + SPIS_CFG_REG);
142
143 return 0;
144}
145
146static int mtk_spi_slave_fifo_transfer(struct spi_controller *ctlr,
147 struct spi_device *spi,
148 struct spi_transfer *xfer)
149{
150 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
151 int reg_val, cnt, remainder, ret;
152
153 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
154
155 reg_val = readl(mdata->base + SPIS_CFG_REG);
156 if (xfer->rx_buf)
157 reg_val |= SPIS_RX_EN;
158 if (xfer->tx_buf)
159 reg_val |= SPIS_TX_EN;
160 writel(reg_val, mdata->base + SPIS_CFG_REG);
161
162 cnt = xfer->len / 4;
163 if (xfer->tx_buf)
164 iowrite32_rep(mdata->base + SPIS_TX_DATA_REG,
165 xfer->tx_buf, cnt);
166
167 remainder = xfer->len % 4;
168 if (xfer->tx_buf && remainder > 0) {
169 reg_val = 0;
170 memcpy(&reg_val, xfer->tx_buf + cnt * 4, remainder);
171 writel(reg_val, mdata->base + SPIS_TX_DATA_REG);
172 }
173
174 ret = mtk_spi_slave_wait_for_completion(mdata);
175 if (ret) {
176 mtk_spi_slave_disable_xfer(mdata);
177 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
178 }
179
180 return ret;
181}
182
183static int mtk_spi_slave_dma_transfer(struct spi_controller *ctlr,
184 struct spi_device *spi,
185 struct spi_transfer *xfer)
186{
187 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
188 struct device *dev = mdata->dev;
189 int reg_val, ret;
190
191 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
192
193 if (xfer->tx_buf) {
194 /* tx_buf is a const void* where we need a void * for
195 * the dma mapping
196 */
197 void *nonconst_tx = (void *)xfer->tx_buf;
198
199 xfer->tx_dma = dma_map_single(dev, nonconst_tx,
200 xfer->len, DMA_TO_DEVICE);
201 if (dma_mapping_error(dev, xfer->tx_dma)) {
202 ret = -ENOMEM;
203 goto disable_transfer;
204 }
205 }
206
207 if (xfer->rx_buf) {
208 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
209 xfer->len, DMA_FROM_DEVICE);
210 if (dma_mapping_error(dev, xfer->rx_dma)) {
211 ret = -ENOMEM;
212 goto unmap_txdma;
213 }
214 }
215
216 writel(xfer->tx_dma, mdata->base + SPIS_TX_SRC_REG);
217 writel(xfer->rx_dma, mdata->base + SPIS_RX_DST_REG);
218
219 writel(SPIS_DMA_ADDR_EN, mdata->base + SPIS_SOFT_RST_REG);
220
221 /* enable config reg tx rx_enable */
222 reg_val = readl(mdata->base + SPIS_CFG_REG);
223 if (xfer->tx_buf)
224 reg_val |= SPIS_TX_EN;
225 if (xfer->rx_buf)
226 reg_val |= SPIS_RX_EN;
227 writel(reg_val, mdata->base + SPIS_CFG_REG);
228
229 /* config dma */
230 reg_val = 0;
231 reg_val |= (xfer->len - 1) & TX_DMA_LEN;
232 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
233
234 reg_val = readl(mdata->base + SPIS_DMA_CFG_REG);
235 if (xfer->tx_buf)
236 reg_val |= TX_DMA_EN;
237 if (xfer->rx_buf)
238 reg_val |= RX_DMA_EN;
239 reg_val |= TX_DMA_TRIG_EN;
240 writel(reg_val, mdata->base + SPIS_DMA_CFG_REG);
241
242 ret = mtk_spi_slave_wait_for_completion(mdata);
243 if (ret)
244 goto unmap_rxdma;
245
246 return 0;
247
248unmap_rxdma:
249 if (xfer->rx_buf)
250 dma_unmap_single(dev, xfer->rx_dma,
251 xfer->len, DMA_FROM_DEVICE);
252
253unmap_txdma:
254 if (xfer->tx_buf)
255 dma_unmap_single(dev, xfer->tx_dma,
256 xfer->len, DMA_TO_DEVICE);
257
258disable_transfer:
259 mtk_spi_slave_disable_dma(mdata);
260 mtk_spi_slave_disable_xfer(mdata);
261 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
262
263 return ret;
264}
265
266static int mtk_spi_slave_transfer_one(struct spi_controller *ctlr,
267 struct spi_device *spi,
268 struct spi_transfer *xfer)
269{
270 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
271
272 reinit_completion(&mdata->xfer_done);
273 mdata->slave_aborted = false;
274 mdata->cur_transfer = xfer;
275
276 if (xfer->len > MTK_SPI_SLAVE_MAX_FIFO_SIZE)
277 return mtk_spi_slave_dma_transfer(ctlr, spi, xfer);
278 else
279 return mtk_spi_slave_fifo_transfer(ctlr, spi, xfer);
280}
281
282static int mtk_spi_slave_setup(struct spi_device *spi)
283{
284 struct mtk_spi_slave *mdata = spi_controller_get_devdata(spi->master);
285 u32 reg_val;
286
287 reg_val = DMA_DONE_EN | DATA_DONE_EN |
288 RSTA_DONE_EN | CMD_INVALID_EN;
289 writel(reg_val, mdata->base + SPIS_IRQ_EN_REG);
290
291 reg_val = DMA_DONE_MASK | DATA_DONE_MASK |
292 RSTA_DONE_MASK | CMD_INVALID_MASK;
293 writel(reg_val, mdata->base + SPIS_IRQ_MASK_REG);
294
295 mtk_spi_slave_disable_dma(mdata);
296 mtk_spi_slave_disable_xfer(mdata);
297
298 return 0;
299}
300
301static int mtk_slave_abort(struct spi_controller *ctlr)
302{
303 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
304
305 mdata->slave_aborted = true;
306 complete(&mdata->xfer_done);
307
308 return 0;
309}
310
311static irqreturn_t mtk_spi_slave_interrupt(int irq, void *dev_id)
312{
313 struct spi_controller *ctlr = dev_id;
314 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
315 struct spi_transfer *trans = mdata->cur_transfer;
316 u32 int_status, reg_val, cnt, remainder;
317
318 int_status = readl(mdata->base + SPIS_IRQ_ST_REG);
319 writel(int_status, mdata->base + SPIS_IRQ_CLR_REG);
320
321 if (!trans)
322 return IRQ_NONE;
323
324 if ((int_status & DMA_DONE_ST) &&
325 ((int_status & DATA_DONE_ST) ||
326 (int_status & RSTA_DONE_ST))) {
327 writel(SPIS_SOFT_RST, mdata->base + SPIS_SOFT_RST_REG);
328
329 if (trans->tx_buf)
330 dma_unmap_single(mdata->dev, trans->tx_dma,
331 trans->len, DMA_TO_DEVICE);
332 if (trans->rx_buf)
333 dma_unmap_single(mdata->dev, trans->rx_dma,
334 trans->len, DMA_FROM_DEVICE);
335
336 mtk_spi_slave_disable_dma(mdata);
337 mtk_spi_slave_disable_xfer(mdata);
338 }
339
340 if ((!(int_status & DMA_DONE_ST)) &&
341 ((int_status & DATA_DONE_ST) ||
342 (int_status & RSTA_DONE_ST))) {
343 cnt = trans->len / 4;
344 if (trans->rx_buf)
345 ioread32_rep(mdata->base + SPIS_RX_DATA_REG,
346 trans->rx_buf, cnt);
347 remainder = trans->len % 4;
348 if (trans->rx_buf && remainder > 0) {
349 reg_val = readl(mdata->base + SPIS_RX_DATA_REG);
350 memcpy(trans->rx_buf + (cnt * 4),
351 &reg_val, remainder);
352 }
353
354 mtk_spi_slave_disable_xfer(mdata);
355 }
356
357 if (int_status & CMD_INVALID_ST) {
358 dev_warn(&ctlr->dev, "cmd invalid\n");
359 return IRQ_NONE;
360 }
361
362 mdata->cur_transfer = NULL;
363 complete(&mdata->xfer_done);
364
365 return IRQ_HANDLED;
366}
367
368static int mtk_spi_slave_probe(struct platform_device *pdev)
369{
370 struct spi_controller *ctlr;
371 struct mtk_spi_slave *mdata;
372 struct resource *res;
373 int irq, ret;
374
375 ctlr = spi_alloc_slave(&pdev->dev, sizeof(*mdata));
376 if (!ctlr) {
377 dev_err(&pdev->dev, "failed to alloc spi slave\n");
378 return -ENOMEM;
379 }
380
381 ctlr->auto_runtime_pm = true;
382 ctlr->dev.of_node = pdev->dev.of_node;
383 ctlr->mode_bits = SPI_CPOL | SPI_CPHA;
384 ctlr->mode_bits |= SPI_LSB_FIRST;
385
386 ctlr->prepare_message = mtk_spi_slave_prepare_message;
387 ctlr->transfer_one = mtk_spi_slave_transfer_one;
388 ctlr->setup = mtk_spi_slave_setup;
389 ctlr->slave_abort = mtk_slave_abort;
390
391 mdata = spi_controller_get_devdata(ctlr);
392
393 platform_set_drvdata(pdev, ctlr);
394
395 init_completion(&mdata->xfer_done);
396
397 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
398 if (!res) {
399 ret = -ENODEV;
400 dev_err(&pdev->dev, "failed to determine base address\n");
401 goto err_put_ctlr;
402 }
403
404 mdata->dev = &pdev->dev;
405
406 mdata->base = devm_ioremap_resource(&pdev->dev, res);
407 if (IS_ERR(mdata->base)) {
408 ret = PTR_ERR(mdata->base);
409 goto err_put_ctlr;
410 }
411
412 irq = platform_get_irq(pdev, 0);
413 if (irq < 0) {
414 dev_err(&pdev->dev, "failed to get irq (%d)\n", irq);
415 ret = irq;
416 goto err_put_ctlr;
417 }
418
419 ret = devm_request_irq(&pdev->dev, irq, mtk_spi_slave_interrupt,
420 IRQF_TRIGGER_NONE, dev_name(&pdev->dev), ctlr);
421 if (ret) {
422 dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
423 goto err_put_ctlr;
424 }
425
426 mdata->spis_clk = devm_clk_get(&pdev->dev, "spis-clk");
427 if (IS_ERR(mdata->spis_clk)) {
428 ret = PTR_ERR(mdata->spis_clk);
429 dev_err(&pdev->dev, "failed to get spis-clk: %d\n", ret);
430 goto err_put_ctlr;
431 }
432
433 ret = clk_prepare_enable(mdata->spis_clk);
434 if (ret < 0) {
435 dev_err(&pdev->dev, "failed to enable spis_clk (%d)\n", ret);
436 goto err_put_ctlr;
437 }
438
439 pm_runtime_enable(&pdev->dev);
440
441 ret = devm_spi_register_controller(&pdev->dev, ctlr);
442 if (ret) {
443 dev_err(&pdev->dev,
444 "failed to register slave controller(%d)\n", ret);
445 clk_disable_unprepare(mdata->spis_clk);
446 goto err_disable_runtime_pm;
447 }
448
449 clk_disable_unprepare(mdata->spis_clk);
450
451 return 0;
452
453err_disable_runtime_pm:
454 pm_runtime_disable(&pdev->dev);
455err_put_ctlr:
456 spi_controller_put(ctlr);
457
458 return ret;
459}
460
461static int mtk_spi_slave_remove(struct platform_device *pdev)
462{
463 pm_runtime_disable(&pdev->dev);
464
465 return 0;
466}
467
468#ifdef CONFIG_PM_SLEEP
469static int mtk_spi_slave_suspend(struct device *dev)
470{
471 struct spi_controller *ctlr = dev_get_drvdata(dev);
472 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
473 int ret;
474
475 ret = spi_controller_suspend(ctlr);
476 if (ret)
477 return ret;
478
479 if (!pm_runtime_suspended(dev))
480 clk_disable_unprepare(mdata->spis_clk);
481
482 return ret;
483}
484
485static int mtk_spi_slave_resume(struct device *dev)
486{
487 struct spi_controller *ctlr = dev_get_drvdata(dev);
488 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
489 int ret;
490
491 if (!pm_runtime_suspended(dev)) {
492 ret = clk_prepare_enable(mdata->spis_clk);
493 if (ret < 0) {
494 dev_err(dev, "failed to enable spis_clk (%d)\n", ret);
495 return ret;
496 }
497 }
498
499 ret = spi_controller_resume(ctlr);
500 if (ret < 0)
501 clk_disable_unprepare(mdata->spis_clk);
502
503 return ret;
504}
505#endif /* CONFIG_PM_SLEEP */
506
507#ifdef CONFIG_PM
508static int mtk_spi_slave_runtime_suspend(struct device *dev)
509{
510 struct spi_controller *ctlr = dev_get_drvdata(dev);
511 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
512
513 clk_disable_unprepare(mdata->spis_clk);
514
515 return 0;
516}
517
518static int mtk_spi_slave_runtime_resume(struct device *dev)
519{
520 struct spi_controller *ctlr = dev_get_drvdata(dev);
521 struct mtk_spi_slave *mdata = spi_controller_get_devdata(ctlr);
522 int ret;
523
524 ret = clk_prepare_enable(mdata->spis_clk);
525 if (ret < 0) {
526 dev_err(dev, "failed to enable spis_clk (%d)\n", ret);
527 return ret;
528 }
529
530 return 0;
531}
532#endif /* CONFIG_PM */
533
534static const struct dev_pm_ops mtk_spi_slave_pm = {
535 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_slave_suspend, mtk_spi_slave_resume)
536 SET_RUNTIME_PM_OPS(mtk_spi_slave_runtime_suspend,
537 mtk_spi_slave_runtime_resume, NULL)
538};
539
540static struct platform_driver mtk_spi_slave_driver = {
541 .driver = {
542 .name = "mtk-spi-slave",
543 .pm = &mtk_spi_slave_pm,
544 .of_match_table = mtk_spi_slave_of_match,
545 },
546 .probe = mtk_spi_slave_probe,
547 .remove = mtk_spi_slave_remove,
548};
549
550module_platform_driver(mtk_spi_slave_driver);
551
552MODULE_DESCRIPTION("MTK SPI Slave Controller driver");
553MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
554MODULE_LICENSE("GPL v2");
555MODULE_ALIAS("platform:mtk-spi-slave");