Lines Matching +full:spi +full:- +full:src +full:- +full:clk
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2017-2021, Rockchip Inc.
6 * Author: Shawn Lin <shawn.lin@rock-chips.com>
8 * Jon Lin <Jon.lin@rock-chips.com>
12 #include <linux/clk.h>
14 #include <linux/dma-mapping.h>
22 #include <linux/spi/spi-mem.h>
124 /* Src or Dst addr for master */
154 * devices (0-3), however I have only been able to test a single CS (CS 0)
159 /* The SFC can transfer max 16KB - 1 at one time
175 struct clk *hclk;
176 struct clk *clk; member
192 writel_relaxed(SFC_RCVR_RESET, sfc->regbase + SFC_RCVR); in rockchip_sfc_reset()
194 err = readl_poll_timeout(sfc->regbase + SFC_RCVR, status, in rockchip_sfc_reset()
198 dev_err(sfc->dev, "SFC reset never finished\n"); in rockchip_sfc_reset()
201 writel_relaxed(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_reset()
203 dev_dbg(sfc->dev, "reset\n"); in rockchip_sfc_reset()
210 return (u16)(readl(sfc->regbase + SFC_VER) & 0xffff); in rockchip_sfc_get_version()
223 reg = readl(sfc->regbase + SFC_IMR); in rockchip_sfc_irq_unmask()
225 writel(reg, sfc->regbase + SFC_IMR); in rockchip_sfc_irq_unmask()
233 reg = readl(sfc->regbase + SFC_IMR); in rockchip_sfc_irq_mask()
235 writel(reg, sfc->regbase + SFC_IMR); in rockchip_sfc_irq_mask()
240 writel(0, sfc->regbase + SFC_CTRL); in rockchip_sfc_init()
241 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_init()
244 writel(SFC_LEN_CTRL_TRB_SEL, sfc->regbase + SFC_LEN_CTRL); in rockchip_sfc_init()
254 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status, in rockchip_sfc_wait_txfifo_ready()
258 dev_dbg(sfc->dev, "sfc wait tx fifo timeout\n"); in rockchip_sfc_wait_txfifo_ready()
260 return -ETIMEDOUT; in rockchip_sfc_wait_txfifo_ready()
271 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status, in rockchip_sfc_wait_rxfifo_ready()
275 dev_dbg(sfc->dev, "sfc wait rx fifo timeout\n"); in rockchip_sfc_wait_rxfifo_ready()
277 return -ETIMEDOUT; in rockchip_sfc_wait_rxfifo_ready()
285 if (unlikely(op->dummy.nbytes && !op->addr.nbytes)) { in rockchip_sfc_adjust_op_work()
290 op->addr.nbytes = op->dummy.nbytes; in rockchip_sfc_adjust_op_work()
291 op->addr.buswidth = op->dummy.buswidth; in rockchip_sfc_adjust_op_work()
292 op->addr.val = 0xFFFFFFFFF; in rockchip_sfc_adjust_op_work()
294 op->dummy.nbytes = 0; in rockchip_sfc_adjust_op_work()
306 cmd = op->cmd.opcode; in rockchip_sfc_xfer_setup()
307 ctrl |= ((op->cmd.buswidth >> 1) << SFC_CTRL_CMD_BITS_SHIFT); in rockchip_sfc_xfer_setup()
310 if (op->addr.nbytes) { in rockchip_sfc_xfer_setup()
311 if (op->addr.nbytes == 4) { in rockchip_sfc_xfer_setup()
313 } else if (op->addr.nbytes == 3) { in rockchip_sfc_xfer_setup()
317 writel(op->addr.nbytes * 8 - 1, sfc->regbase + SFC_ABIT); in rockchip_sfc_xfer_setup()
320 ctrl |= ((op->addr.buswidth >> 1) << SFC_CTRL_ADDR_BITS_SHIFT); in rockchip_sfc_xfer_setup()
324 if (op->dummy.nbytes) { in rockchip_sfc_xfer_setup()
325 if (op->dummy.buswidth == 4) in rockchip_sfc_xfer_setup()
326 cmd |= op->dummy.nbytes * 2 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
327 else if (op->dummy.buswidth == 2) in rockchip_sfc_xfer_setup()
328 cmd |= op->dummy.nbytes * 4 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
330 cmd |= op->dummy.nbytes * 8 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
334 if (sfc->version >= SFC_VER_4) /* Clear it if no data to transfer */ in rockchip_sfc_xfer_setup()
335 writel(len, sfc->regbase + SFC_LEN_EXT); in rockchip_sfc_xfer_setup()
339 if (op->data.dir == SPI_MEM_DATA_OUT) in rockchip_sfc_xfer_setup()
342 ctrl |= ((op->data.buswidth >> 1) << SFC_CTRL_DATA_BITS_SHIFT); in rockchip_sfc_xfer_setup()
344 if (!len && op->addr.nbytes) in rockchip_sfc_xfer_setup()
349 cmd |= spi_get_chipselect(mem->spi, 0) << SFC_CMD_CS_SHIFT; in rockchip_sfc_xfer_setup()
351 dev_dbg(sfc->dev, "sfc addr.nbytes=%x(x%d) dummy.nbytes=%x(x%d)\n", in rockchip_sfc_xfer_setup()
352 op->addr.nbytes, op->addr.buswidth, in rockchip_sfc_xfer_setup()
353 op->dummy.nbytes, op->dummy.buswidth); in rockchip_sfc_xfer_setup()
354 dev_dbg(sfc->dev, "sfc ctrl=%x cmd=%x addr=%llx len=%x\n", in rockchip_sfc_xfer_setup()
355 ctrl, cmd, op->addr.val, len); in rockchip_sfc_xfer_setup()
357 writel(ctrl, sfc->regbase + SFC_CTRL); in rockchip_sfc_xfer_setup()
358 writel(cmd, sfc->regbase + SFC_CMD); in rockchip_sfc_xfer_setup()
359 if (op->addr.nbytes) in rockchip_sfc_xfer_setup()
360 writel(op->addr.val, sfc->regbase + SFC_ADDR); in rockchip_sfc_xfer_setup()
379 iowrite32_rep(sfc->regbase + SFC_DATA, buf, write_words); in rockchip_sfc_write_fifo()
381 dwords -= write_words; in rockchip_sfc_write_fifo()
390 writel(tmp, sfc->regbase + SFC_DATA); in rockchip_sfc_write_fifo()
411 ioread32_rep(sfc->regbase + SFC_DATA, buf, read_words); in rockchip_sfc_read_fifo()
413 dwords -= read_words; in rockchip_sfc_read_fifo()
421 tmp = readl(sfc->regbase + SFC_DATA); in rockchip_sfc_read_fifo()
430 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_fifo_transfer_dma()
431 writel((u32)dma_buf, sfc->regbase + SFC_DMA_ADDR); in rockchip_sfc_fifo_transfer_dma()
432 writel(SFC_DMA_TRIGGER_START, sfc->regbase + SFC_DMA_TRIGGER); in rockchip_sfc_fifo_transfer_dma()
440 dev_dbg(sfc->dev, "sfc xfer_poll len=%x\n", len); in rockchip_sfc_xfer_data_poll()
442 if (op->data.dir == SPI_MEM_DATA_OUT) in rockchip_sfc_xfer_data_poll()
443 return rockchip_sfc_write_fifo(sfc, op->data.buf.out, len); in rockchip_sfc_xfer_data_poll()
445 return rockchip_sfc_read_fifo(sfc, op->data.buf.in, len); in rockchip_sfc_xfer_data_poll()
453 dev_dbg(sfc->dev, "sfc xfer_dma len=%x\n", len); in rockchip_sfc_xfer_data_dma()
455 if (op->data.dir == SPI_MEM_DATA_OUT) in rockchip_sfc_xfer_data_dma()
456 memcpy(sfc->buffer, op->data.buf.out, len); in rockchip_sfc_xfer_data_dma()
458 ret = rockchip_sfc_fifo_transfer_dma(sfc, sfc->dma_buffer, len); in rockchip_sfc_xfer_data_dma()
459 if (!wait_for_completion_timeout(&sfc->cp, msecs_to_jiffies(2000))) { in rockchip_sfc_xfer_data_dma()
460 dev_err(sfc->dev, "DMA wait for transfer finish timeout\n"); in rockchip_sfc_xfer_data_dma()
461 ret = -ETIMEDOUT; in rockchip_sfc_xfer_data_dma()
464 if (op->data.dir == SPI_MEM_DATA_IN) in rockchip_sfc_xfer_data_dma()
465 memcpy(op->data.buf.in, sfc->buffer, len); in rockchip_sfc_xfer_data_dma()
475 ret = readl_poll_timeout(sfc->regbase + SFC_SR, status, in rockchip_sfc_xfer_done()
479 dev_err(sfc->dev, "wait sfc idle timeout\n"); in rockchip_sfc_xfer_done()
482 ret = -EIO; in rockchip_sfc_xfer_done()
490 struct rockchip_sfc *sfc = spi_controller_get_devdata(mem->spi->controller); in rockchip_sfc_exec_mem_op()
491 u32 len = op->data.nbytes; in rockchip_sfc_exec_mem_op()
494 if (unlikely(mem->spi->max_speed_hz != sfc->frequency)) { in rockchip_sfc_exec_mem_op()
495 ret = clk_set_rate(sfc->clk, mem->spi->max_speed_hz); in rockchip_sfc_exec_mem_op()
498 sfc->frequency = mem->spi->max_speed_hz; in rockchip_sfc_exec_mem_op()
499 dev_dbg(sfc->dev, "set_freq=%dHz real_freq=%ldHz\n", in rockchip_sfc_exec_mem_op()
500 sfc->frequency, clk_get_rate(sfc->clk)); in rockchip_sfc_exec_mem_op()
506 if (likely(sfc->use_dma) && len >= SFC_DMA_TRANS_THRETHOLD) { in rockchip_sfc_exec_mem_op()
507 init_completion(&sfc->cp); in rockchip_sfc_exec_mem_op()
515 dev_err(sfc->dev, "xfer data failed ret %d dir %d\n", ret, op->data.dir); in rockchip_sfc_exec_mem_op()
517 return -EIO; in rockchip_sfc_exec_mem_op()
526 struct rockchip_sfc *sfc = spi_controller_get_devdata(mem->spi->controller); in rockchip_sfc_adjust_op_size()
528 op->data.nbytes = min(op->data.nbytes, sfc->max_iosize); in rockchip_sfc_adjust_op_size()
543 reg = readl(sfc->regbase + SFC_RISR); in rockchip_sfc_irq_handler()
546 writel_relaxed(reg, sfc->regbase + SFC_ICLR); in rockchip_sfc_irq_handler()
549 complete(&sfc->cp); in rockchip_sfc_irq_handler()
559 struct device *dev = &pdev->dev; in rockchip_sfc_probe()
564 host = devm_spi_alloc_host(&pdev->dev, sizeof(*sfc)); in rockchip_sfc_probe()
566 return -ENOMEM; in rockchip_sfc_probe()
568 host->flags = SPI_CONTROLLER_HALF_DUPLEX; in rockchip_sfc_probe()
569 host->mem_ops = &rockchip_sfc_mem_ops; in rockchip_sfc_probe()
570 host->dev.of_node = pdev->dev.of_node; in rockchip_sfc_probe()
571 host->mode_bits = SPI_TX_QUAD | SPI_TX_DUAL | SPI_RX_QUAD | SPI_RX_DUAL; in rockchip_sfc_probe()
572 host->max_speed_hz = SFC_MAX_SPEED; in rockchip_sfc_probe()
573 host->num_chipselect = SFC_MAX_CHIPSELECT_NUM; in rockchip_sfc_probe()
576 sfc->dev = dev; in rockchip_sfc_probe()
578 sfc->regbase = devm_platform_ioremap_resource(pdev, 0); in rockchip_sfc_probe()
579 if (IS_ERR(sfc->regbase)) in rockchip_sfc_probe()
580 return PTR_ERR(sfc->regbase); in rockchip_sfc_probe()
582 sfc->clk = devm_clk_get(&pdev->dev, "clk_sfc"); in rockchip_sfc_probe()
583 if (IS_ERR(sfc->clk)) { in rockchip_sfc_probe()
584 dev_err(&pdev->dev, "Failed to get sfc interface clk\n"); in rockchip_sfc_probe()
585 return PTR_ERR(sfc->clk); in rockchip_sfc_probe()
588 sfc->hclk = devm_clk_get(&pdev->dev, "hclk_sfc"); in rockchip_sfc_probe()
589 if (IS_ERR(sfc->hclk)) { in rockchip_sfc_probe()
590 dev_err(&pdev->dev, "Failed to get sfc ahb clk\n"); in rockchip_sfc_probe()
591 return PTR_ERR(sfc->hclk); in rockchip_sfc_probe()
594 sfc->use_dma = !of_property_read_bool(sfc->dev->of_node, in rockchip_sfc_probe()
595 "rockchip,sfc-no-dma"); in rockchip_sfc_probe()
597 if (sfc->use_dma) { in rockchip_sfc_probe()
604 sfc->buffer = dmam_alloc_coherent(dev, SFC_MAX_IOSIZE_VER3, in rockchip_sfc_probe()
605 &sfc->dma_buffer, in rockchip_sfc_probe()
607 if (!sfc->buffer) in rockchip_sfc_probe()
608 return -ENOMEM; in rockchip_sfc_probe()
611 ret = clk_prepare_enable(sfc->hclk); in rockchip_sfc_probe()
613 dev_err(&pdev->dev, "Failed to enable ahb clk\n"); in rockchip_sfc_probe()
617 ret = clk_prepare_enable(sfc->clk); in rockchip_sfc_probe()
619 dev_err(&pdev->dev, "Failed to enable interface clk\n"); in rockchip_sfc_probe()
629 0, pdev->name, sfc); in rockchip_sfc_probe()
640 sfc->max_iosize = rockchip_sfc_get_max_iosize(sfc); in rockchip_sfc_probe()
641 sfc->version = rockchip_sfc_get_version(sfc); in rockchip_sfc_probe()
650 clk_disable_unprepare(sfc->clk); in rockchip_sfc_probe()
652 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_probe()
664 clk_disable_unprepare(sfc->clk); in rockchip_sfc_remove()
665 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_remove()
676 .name = "rockchip-sfc",
686 MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
688 MODULE_AUTHOR("Jon Lin <Jon.lin@rock-chips.com>");