1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2021 Sunplus Inc.
3 // Author: Li-hao Kuo <lhjeff911@gmail.com>
5 #include <linux/bitfield.h>
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/reset.h>
15 #include <linux/spi/spi.h>
17 #define SP7021_DATA_RDY_REG 0x0044
18 #define SP7021_SLAVE_DMA_CTRL_REG 0x0048
19 #define SP7021_SLAVE_DMA_LENGTH_REG 0x004c
20 #define SP7021_SLAVE_DMA_ADDR_REG 0x004c
22 #define SP7021_SLAVE_DATA_RDY BIT(0)
23 #define SP7021_SLAVE_SW_RST BIT(1)
24 #define SP7021_SLA_DMA_W_INT BIT(8)
25 #define SP7021_SLAVE_CLR_INT BIT(8)
26 #define SP7021_SLAVE_DMA_EN BIT(0)
27 #define SP7021_SLAVE_DMA_RW BIT(6)
28 #define SP7021_SLAVE_DMA_CMD GENMASK(3, 2)
30 #define SP7021_FIFO_REG 0x0034
31 #define SP7021_SPI_STATUS_REG 0x0038
32 #define SP7021_SPI_CONFIG_REG 0x003c
33 #define SP7021_INT_BUSY_REG 0x004c
34 #define SP7021_DMA_CTRL_REG 0x0050
36 #define SP7021_SPI_START_FD BIT(0)
37 #define SP7021_FD_SW_RST BIT(1)
38 #define SP7021_TX_EMP_FLAG BIT(2)
39 #define SP7021_RX_EMP_FLAG BIT(4)
40 #define SP7021_RX_FULL_FLAG BIT(5)
41 #define SP7021_FINISH_FLAG BIT(6)
43 #define SP7021_TX_CNT_MASK GENMASK(11, 8)
44 #define SP7021_RX_CNT_MASK GENMASK(15, 12)
45 #define SP7021_TX_LEN_MASK GENMASK(23, 16)
46 #define SP7021_GET_LEN_MASK GENMASK(31, 24)
47 #define SP7021_SET_TX_LEN GENMASK(23, 16)
48 #define SP7021_SET_XFER_LEN GENMASK(31, 24)
50 #define SP7021_CPOL_FD BIT(0)
51 #define SP7021_CPHA_R BIT(1)
52 #define SP7021_CPHA_W BIT(2)
53 #define SP7021_LSB_SEL BIT(4)
54 #define SP7021_CS_POR BIT(5)
55 #define SP7021_FD_SEL BIT(6)
57 #define SP7021_RX_UNIT GENMASK(8, 7)
58 #define SP7021_TX_UNIT GENMASK(10, 9)
59 #define SP7021_TX_EMP_FLAG_MASK BIT(11)
60 #define SP7021_RX_FULL_FLAG_MASK BIT(14)
61 #define SP7021_FINISH_FLAG_MASK BIT(15)
62 #define SP7021_CLEAN_RW_BYTE GENMASK(10, 7)
63 #define SP7021_CLEAN_FLUG_MASK GENMASK(15, 11)
64 #define SP7021_CLK_MASK GENMASK(31, 16)
66 #define SP7021_INT_BYPASS BIT(3)
67 #define SP7021_CLR_MASTER_INT BIT(6)
69 #define SP7021_SPI_DATA_SIZE (255)
70 #define SP7021_FIFO_DATA_LEN (16)
73 SP7021_MASTER_MODE = 0,
74 SP7021_SLAVE_MODE = 1,
77 struct sp7021_spi_ctlr {
79 struct spi_controller *ctlr;
87 struct reset_control *rstc;
91 struct mutex buf_lock;
92 struct completion isr_done;
93 struct completion slave_isr;
94 unsigned int rx_cur_len;
95 unsigned int tx_cur_len;
96 unsigned int data_unit;
101 static irqreturn_t sp7021_spi_slave_irq(int irq, void *dev)
103 struct sp7021_spi_ctlr *pspim = dev;
104 unsigned int data_status;
106 data_status = readl(pspim->s_base + SP7021_DATA_RDY_REG);
107 data_status |= SP7021_SLAVE_CLR_INT;
108 writel(data_status , pspim->s_base + SP7021_DATA_RDY_REG);
109 complete(&pspim->slave_isr);
113 static int sp7021_spi_slave_abort(struct spi_controller *ctlr)
115 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
117 complete(&pspim->slave_isr);
118 complete(&pspim->isr_done);
122 static int sp7021_spi_slave_tx(struct spi_device *spi, struct spi_transfer *xfer)
124 struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
127 reinit_completion(&pspim->slave_isr);
128 value = SP7021_SLAVE_DMA_EN | SP7021_SLAVE_DMA_RW | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
129 writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
130 writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
131 writel(xfer->tx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
132 value = readl(pspim->s_base + SP7021_DATA_RDY_REG);
133 value |= SP7021_SLAVE_DATA_RDY;
134 writel(value, pspim->s_base + SP7021_DATA_RDY_REG);
135 if (wait_for_completion_interruptible(&pspim->isr_done)) {
136 dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
142 static int sp7021_spi_slave_rx(struct spi_device *spi, struct spi_transfer *xfer)
144 struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
147 reinit_completion(&pspim->isr_done);
148 value = SP7021_SLAVE_DMA_EN | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
149 writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
150 writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
151 writel(xfer->rx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
152 if (wait_for_completion_interruptible(&pspim->isr_done)) {
153 dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
156 writel(SP7021_SLAVE_SW_RST, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
160 static void sp7021_spi_master_rb(struct sp7021_spi_ctlr *pspim, unsigned int len)
164 for (i = 0; i < len; i++) {
165 pspim->rx_buf[pspim->rx_cur_len] =
166 readl(pspim->m_base + SP7021_FIFO_REG);
171 static void sp7021_spi_master_wb(struct sp7021_spi_ctlr *pspim, unsigned int len)
175 for (i = 0; i < len; i++) {
176 writel(pspim->tx_buf[pspim->tx_cur_len],
177 pspim->m_base + SP7021_FIFO_REG);
182 static irqreturn_t sp7021_spi_master_irq(int irq, void *dev)
184 struct sp7021_spi_ctlr *pspim = dev;
185 unsigned int tx_cnt, total_len;
186 unsigned int tx_len, rx_cnt;
187 unsigned int fd_status;
188 bool isrdone = false;
191 fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
192 tx_cnt = FIELD_GET(SP7021_TX_CNT_MASK, fd_status);
193 tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
194 total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
196 if ((fd_status & SP7021_TX_EMP_FLAG) && (fd_status & SP7021_RX_EMP_FLAG) && total_len == 0)
199 if (tx_len == 0 && total_len == 0)
202 spin_lock_irq(&pspim->lock);
204 rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
205 if (fd_status & SP7021_RX_FULL_FLAG)
206 rx_cnt = pspim->data_unit;
208 tx_cnt = min(tx_len - pspim->tx_cur_len, pspim->data_unit - tx_cnt);
209 dev_dbg(pspim->dev, "fd_st=0x%x rx_c:%d tx_c:%d tx_l:%d",
210 fd_status, rx_cnt, tx_cnt, tx_len);
213 sp7021_spi_master_rb(pspim, rx_cnt);
215 sp7021_spi_master_wb(pspim, tx_cnt);
217 fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
218 tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
219 total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
221 if (fd_status & SP7021_FINISH_FLAG || tx_len == pspim->tx_cur_len) {
222 while (total_len != pspim->rx_cur_len) {
223 fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
224 total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
225 if (fd_status & SP7021_RX_FULL_FLAG)
226 rx_cnt = pspim->data_unit;
228 rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
231 sp7021_spi_master_rb(pspim, rx_cnt);
233 value = readl(pspim->m_base + SP7021_INT_BUSY_REG);
234 value |= SP7021_CLR_MASTER_INT;
235 writel(value, pspim->m_base + SP7021_INT_BUSY_REG);
236 writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
241 complete(&pspim->isr_done);
242 spin_unlock_irq(&pspim->lock);
246 static void sp7021_prep_transfer(struct spi_controller *ctlr, struct spi_device *spi)
248 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
250 pspim->tx_cur_len = 0;
251 pspim->rx_cur_len = 0;
252 pspim->data_unit = SP7021_FIFO_DATA_LEN;
255 // preliminary set CS, CPOL, CPHA and LSB
256 static int sp7021_spi_controller_prepare_message(struct spi_controller *ctlr,
257 struct spi_message *msg)
259 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
260 struct spi_device *s = msg->spi;
263 valus = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
264 valus |= SP7021_FD_SW_RST;
265 writel(valus, pspim->m_base + SP7021_SPI_STATUS_REG);
267 if (s->mode & SPI_CPOL)
268 rs |= SP7021_CPOL_FD;
270 if (s->mode & SPI_LSB_FIRST)
271 rs |= SP7021_LSB_SEL;
273 if (s->mode & SPI_CS_HIGH)
276 if (s->mode & SPI_CPHA)
281 rs |= FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
282 pspim->xfer_conf = rs;
283 if (pspim->xfer_conf & SP7021_CPOL_FD)
284 writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
289 static void sp7021_spi_setup_clk(struct spi_controller *ctlr, struct spi_transfer *xfer)
291 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
292 u32 clk_rate, clk_sel, div;
294 clk_rate = clk_get_rate(pspim->spi_clk);
295 div = max(2U, clk_rate / xfer->speed_hz);
297 clk_sel = (div / 2) - 1;
298 pspim->xfer_conf &= ~SP7021_CLK_MASK;
299 pspim->xfer_conf |= FIELD_PREP(SP7021_CLK_MASK, clk_sel);
300 writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
303 static int sp7021_spi_master_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
304 struct spi_transfer *xfer)
306 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
307 unsigned long timeout = msecs_to_jiffies(1000);
308 unsigned int xfer_cnt, xfer_len, last_len;
309 unsigned int i, len_temp;
312 xfer_cnt = xfer->len / SP7021_SPI_DATA_SIZE;
313 last_len = xfer->len % SP7021_SPI_DATA_SIZE;
315 for (i = 0; i <= xfer_cnt; i++) {
316 mutex_lock(&pspim->buf_lock);
317 sp7021_prep_transfer(ctlr, spi);
318 sp7021_spi_setup_clk(ctlr, xfer);
319 reinit_completion(&pspim->isr_done);
324 xfer_len = SP7021_SPI_DATA_SIZE;
326 pspim->tx_buf = xfer->tx_buf + i * SP7021_SPI_DATA_SIZE;
327 pspim->rx_buf = xfer->rx_buf + i * SP7021_SPI_DATA_SIZE;
329 if (pspim->tx_cur_len < xfer_len) {
330 len_temp = min(pspim->data_unit, xfer_len);
331 sp7021_spi_master_wb(pspim, len_temp);
333 reg_temp = readl(pspim->m_base + SP7021_SPI_CONFIG_REG);
334 reg_temp &= ~SP7021_CLEAN_RW_BYTE;
335 reg_temp &= ~SP7021_CLEAN_FLUG_MASK;
336 reg_temp |= SP7021_FD_SEL | SP7021_FINISH_FLAG_MASK |
337 SP7021_TX_EMP_FLAG_MASK | SP7021_RX_FULL_FLAG_MASK |
338 FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
339 writel(reg_temp, pspim->m_base + SP7021_SPI_CONFIG_REG);
341 reg_temp = FIELD_PREP(SP7021_SET_TX_LEN, xfer_len) |
342 FIELD_PREP(SP7021_SET_XFER_LEN, xfer_len) |
344 writel(reg_temp, pspim->m_base + SP7021_SPI_STATUS_REG);
346 if (!wait_for_completion_interruptible_timeout(&pspim->isr_done, timeout)) {
347 dev_err(&spi->dev, "wait_for_completion err\n");
348 mutex_unlock(&pspim->buf_lock);
352 reg_temp = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
353 if (reg_temp & SP7021_FINISH_FLAG) {
354 writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
355 writel(readl(pspim->m_base + SP7021_SPI_CONFIG_REG) &
356 SP7021_CLEAN_FLUG_MASK, pspim->m_base + SP7021_SPI_CONFIG_REG);
359 if (pspim->xfer_conf & SP7021_CPOL_FD)
360 writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
362 mutex_unlock(&pspim->buf_lock);
367 static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
368 struct spi_transfer *xfer)
370 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
371 struct device *dev = pspim->dev;
374 if (xfer->tx_buf && !xfer->rx_buf) {
375 xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
376 xfer->len, DMA_TO_DEVICE);
377 if (dma_mapping_error(dev, xfer->tx_dma))
379 ret = sp7021_spi_slave_tx(spi, xfer);
380 dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
381 } else if (xfer->rx_buf && !xfer->tx_buf) {
382 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
384 if (dma_mapping_error(dev, xfer->rx_dma))
386 ret = sp7021_spi_slave_rx(spi, xfer);
387 dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
389 dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
393 spi_finalize_current_transfer(ctlr);
397 static void sp7021_spi_disable_unprepare(void *data)
399 clk_disable_unprepare(data);
402 static void sp7021_spi_reset_control_assert(void *data)
404 reset_control_assert(data);
407 static int sp7021_spi_controller_probe(struct platform_device *pdev)
409 struct device *dev = &pdev->dev;
410 struct sp7021_spi_ctlr *pspim;
411 struct spi_controller *ctlr;
414 pdev->id = of_alias_get_id(pdev->dev.of_node, "sp_spi");
416 if (device_property_read_bool(dev, "spi-slave"))
417 mode = SP7021_SLAVE_MODE;
419 mode = SP7021_MASTER_MODE;
421 if (mode == SP7021_SLAVE_MODE)
422 ctlr = devm_spi_alloc_slave(dev, sizeof(*pspim));
424 ctlr = devm_spi_alloc_master(dev, sizeof(*pspim));
427 device_set_node(&ctlr->dev, dev_fwnode(dev));
428 ctlr->bus_num = pdev->id;
429 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
430 ctlr->auto_runtime_pm = true;
431 ctlr->prepare_message = sp7021_spi_controller_prepare_message;
432 if (mode == SP7021_SLAVE_MODE) {
433 ctlr->transfer_one = sp7021_spi_slave_transfer_one;
434 ctlr->slave_abort = sp7021_spi_slave_abort;
435 ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
437 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
438 ctlr->min_speed_hz = 40000;
439 ctlr->max_speed_hz = 25000000;
440 ctlr->use_gpio_descriptors = true;
441 ctlr->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
442 ctlr->transfer_one = sp7021_spi_master_transfer_one;
444 platform_set_drvdata(pdev, ctlr);
445 pspim = spi_controller_get_devdata(ctlr);
449 spin_lock_init(&pspim->lock);
450 mutex_init(&pspim->buf_lock);
451 init_completion(&pspim->isr_done);
452 init_completion(&pspim->slave_isr);
454 pspim->m_base = devm_platform_ioremap_resource_byname(pdev, "master");
455 if (IS_ERR(pspim->m_base))
456 return dev_err_probe(dev, PTR_ERR(pspim->m_base), "m_base get fail\n");
458 pspim->s_base = devm_platform_ioremap_resource_byname(pdev, "slave");
459 if (IS_ERR(pspim->s_base))
460 return dev_err_probe(dev, PTR_ERR(pspim->s_base), "s_base get fail\n");
462 pspim->m_irq = platform_get_irq_byname(pdev, "master_risc");
463 if (pspim->m_irq < 0)
466 pspim->s_irq = platform_get_irq_byname(pdev, "slave_risc");
467 if (pspim->s_irq < 0)
470 pspim->spi_clk = devm_clk_get(dev, NULL);
471 if (IS_ERR(pspim->spi_clk))
472 return dev_err_probe(dev, PTR_ERR(pspim->spi_clk), "clk get fail\n");
474 pspim->rstc = devm_reset_control_get_exclusive(dev, NULL);
475 if (IS_ERR(pspim->rstc))
476 return dev_err_probe(dev, PTR_ERR(pspim->rstc), "rst get fail\n");
478 ret = clk_prepare_enable(pspim->spi_clk);
480 return dev_err_probe(dev, ret, "failed to enable clk\n");
482 ret = devm_add_action_or_reset(dev, sp7021_spi_disable_unprepare, pspim->spi_clk);
486 ret = reset_control_deassert(pspim->rstc);
488 return dev_err_probe(dev, ret, "failed to deassert reset\n");
490 ret = devm_add_action_or_reset(dev, sp7021_spi_reset_control_assert, pspim->rstc);
494 ret = devm_request_irq(dev, pspim->m_irq, sp7021_spi_master_irq,
495 IRQF_TRIGGER_RISING, pdev->name, pspim);
499 ret = devm_request_irq(dev, pspim->s_irq, sp7021_spi_slave_irq,
500 IRQF_TRIGGER_RISING, pdev->name, pspim);
504 pm_runtime_enable(dev);
505 ret = spi_register_controller(ctlr);
507 pm_runtime_disable(dev);
508 return dev_err_probe(dev, ret, "spi_register_master fail\n");
513 static int sp7021_spi_controller_remove(struct platform_device *pdev)
515 struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
517 spi_unregister_controller(ctlr);
518 pm_runtime_disable(&pdev->dev);
519 pm_runtime_set_suspended(&pdev->dev);
523 static int __maybe_unused sp7021_spi_controller_suspend(struct device *dev)
525 struct spi_controller *ctlr = dev_get_drvdata(dev);
526 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
528 return reset_control_assert(pspim->rstc);
531 static int __maybe_unused sp7021_spi_controller_resume(struct device *dev)
533 struct spi_controller *ctlr = dev_get_drvdata(dev);
534 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
536 reset_control_deassert(pspim->rstc);
537 return clk_prepare_enable(pspim->spi_clk);
541 static int sp7021_spi_runtime_suspend(struct device *dev)
543 struct spi_controller *ctlr = dev_get_drvdata(dev);
544 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
546 return reset_control_assert(pspim->rstc);
549 static int sp7021_spi_runtime_resume(struct device *dev)
551 struct spi_controller *ctlr = dev_get_drvdata(dev);
552 struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
554 return reset_control_deassert(pspim->rstc);
558 static const struct dev_pm_ops sp7021_spi_pm_ops = {
559 SET_RUNTIME_PM_OPS(sp7021_spi_runtime_suspend,
560 sp7021_spi_runtime_resume, NULL)
561 SET_SYSTEM_SLEEP_PM_OPS(sp7021_spi_controller_suspend,
562 sp7021_spi_controller_resume)
565 static const struct of_device_id sp7021_spi_controller_ids[] = {
566 { .compatible = "sunplus,sp7021-spi" },
569 MODULE_DEVICE_TABLE(of, sp7021_spi_controller_ids);
571 static struct platform_driver sp7021_spi_controller_driver = {
572 .probe = sp7021_spi_controller_probe,
573 .remove = sp7021_spi_controller_remove,
575 .name = "sunplus,sp7021-spi-controller",
576 .of_match_table = sp7021_spi_controller_ids,
577 .pm = &sp7021_spi_pm_ops,
580 module_platform_driver(sp7021_spi_controller_driver);
582 MODULE_AUTHOR("Li-hao Kuo <lhjeff911@gmail.com>");
583 MODULE_DESCRIPTION("Sunplus SPI controller driver");
584 MODULE_LICENSE("GPL");