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>
24 #include <linux/spi/spi-mem.h>
127 /* Src or Dst addr for master */
177 struct clk *hclk;
178 struct clk *clk; member
195 writel_relaxed(SFC_RCVR_RESET, sfc->regbase + SFC_RCVR); in rockchip_sfc_reset()
197 err = readl_poll_timeout(sfc->regbase + SFC_RCVR, status, in rockchip_sfc_reset()
201 dev_err(sfc->dev, "SFC reset never finished\n"); in rockchip_sfc_reset()
204 writel_relaxed(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_reset()
206 dev_dbg(sfc->dev, "reset\n"); in rockchip_sfc_reset()
213 return (u16)(readl(sfc->regbase + SFC_VER) & 0xffff); in rockchip_sfc_get_version()
223 if (sfc->version >= SFC_VER_8) in rockchip_sfc_clk_set_rate()
224 return clk_set_rate(sfc->clk, speed * 2); in rockchip_sfc_clk_set_rate()
226 return clk_set_rate(sfc->clk, speed); in rockchip_sfc_clk_set_rate()
231 if (sfc->version >= SFC_VER_8) in rockchip_sfc_clk_get_rate()
232 return clk_get_rate(sfc->clk) / 2; in rockchip_sfc_clk_get_rate()
234 return clk_get_rate(sfc->clk); in rockchip_sfc_clk_get_rate()
242 reg = readl(sfc->regbase + SFC_IMR); in rockchip_sfc_irq_unmask()
244 writel(reg, sfc->regbase + SFC_IMR); in rockchip_sfc_irq_unmask()
252 reg = readl(sfc->regbase + SFC_IMR); in rockchip_sfc_irq_mask()
254 writel(reg, sfc->regbase + SFC_IMR); in rockchip_sfc_irq_mask()
259 writel(0, sfc->regbase + SFC_CTRL); in rockchip_sfc_init()
260 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_init()
263 writel(SFC_LEN_CTRL_TRB_SEL, sfc->regbase + SFC_LEN_CTRL); in rockchip_sfc_init()
273 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status, in rockchip_sfc_wait_txfifo_ready()
277 dev_dbg(sfc->dev, "sfc wait tx fifo timeout\n"); in rockchip_sfc_wait_txfifo_ready()
279 return -ETIMEDOUT; in rockchip_sfc_wait_txfifo_ready()
290 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status, in rockchip_sfc_wait_rxfifo_ready()
294 dev_dbg(sfc->dev, "sfc wait rx fifo timeout\n"); in rockchip_sfc_wait_rxfifo_ready()
296 return -ETIMEDOUT; in rockchip_sfc_wait_rxfifo_ready()
304 if (unlikely(op->dummy.nbytes && !op->addr.nbytes)) { in rockchip_sfc_adjust_op_work()
309 op->addr.nbytes = op->dummy.nbytes; in rockchip_sfc_adjust_op_work()
310 op->addr.buswidth = op->dummy.buswidth; in rockchip_sfc_adjust_op_work()
311 op->addr.val = 0xFFFFFFFFF; in rockchip_sfc_adjust_op_work()
313 op->dummy.nbytes = 0; in rockchip_sfc_adjust_op_work()
323 u8 cs = spi_get_chipselect(mem->spi, 0); in rockchip_sfc_xfer_setup()
326 cmd = op->cmd.opcode; in rockchip_sfc_xfer_setup()
327 ctrl |= ((op->cmd.buswidth >> 1) << SFC_CTRL_CMD_BITS_SHIFT); in rockchip_sfc_xfer_setup()
330 if (op->addr.nbytes) { in rockchip_sfc_xfer_setup()
331 if (op->addr.nbytes == 4) { in rockchip_sfc_xfer_setup()
333 } else if (op->addr.nbytes == 3) { in rockchip_sfc_xfer_setup()
337 writel(op->addr.nbytes * 8 - 1, in rockchip_sfc_xfer_setup()
338 sfc->regbase + cs * SFC_CS1_REG_OFFSET + SFC_ABIT); in rockchip_sfc_xfer_setup()
341 ctrl |= ((op->addr.buswidth >> 1) << SFC_CTRL_ADDR_BITS_SHIFT); in rockchip_sfc_xfer_setup()
345 if (op->dummy.nbytes) { in rockchip_sfc_xfer_setup()
346 if (op->dummy.buswidth == 4) in rockchip_sfc_xfer_setup()
347 cmd |= op->dummy.nbytes * 2 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
348 else if (op->dummy.buswidth == 2) in rockchip_sfc_xfer_setup()
349 cmd |= op->dummy.nbytes * 4 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
351 cmd |= op->dummy.nbytes * 8 << SFC_CMD_DUMMY_SHIFT; in rockchip_sfc_xfer_setup()
355 if (sfc->version >= SFC_VER_4) /* Clear it if no data to transfer */ in rockchip_sfc_xfer_setup()
356 writel(len, sfc->regbase + SFC_LEN_EXT); in rockchip_sfc_xfer_setup()
360 if (op->data.dir == SPI_MEM_DATA_OUT) in rockchip_sfc_xfer_setup()
363 ctrl |= ((op->data.buswidth >> 1) << SFC_CTRL_DATA_BITS_SHIFT); in rockchip_sfc_xfer_setup()
365 if (!len && op->addr.nbytes) in rockchip_sfc_xfer_setup()
372 dev_dbg(sfc->dev, "sfc addr.nbytes=%x(x%d) dummy.nbytes=%x(x%d)\n", in rockchip_sfc_xfer_setup()
373 op->addr.nbytes, op->addr.buswidth, in rockchip_sfc_xfer_setup()
374 op->dummy.nbytes, op->dummy.buswidth); in rockchip_sfc_xfer_setup()
375 dev_dbg(sfc->dev, "sfc ctrl=%x cmd=%x addr=%llx len=%x\n", in rockchip_sfc_xfer_setup()
376 ctrl, cmd, op->addr.val, len); in rockchip_sfc_xfer_setup()
378 writel(ctrl, sfc->regbase + cs * SFC_CS1_REG_OFFSET + SFC_CTRL); in rockchip_sfc_xfer_setup()
379 writel(cmd, sfc->regbase + SFC_CMD); in rockchip_sfc_xfer_setup()
380 if (op->addr.nbytes) in rockchip_sfc_xfer_setup()
381 writel(op->addr.val, sfc->regbase + SFC_ADDR); in rockchip_sfc_xfer_setup()
400 iowrite32_rep(sfc->regbase + SFC_DATA, buf, write_words); in rockchip_sfc_write_fifo()
402 dwords -= write_words; in rockchip_sfc_write_fifo()
411 writel(tmp, sfc->regbase + SFC_DATA); in rockchip_sfc_write_fifo()
432 ioread32_rep(sfc->regbase + SFC_DATA, buf, read_words); in rockchip_sfc_read_fifo()
434 dwords -= read_words; in rockchip_sfc_read_fifo()
442 tmp = readl(sfc->regbase + SFC_DATA); in rockchip_sfc_read_fifo()
451 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR); in rockchip_sfc_fifo_transfer_dma()
452 writel((u32)dma_buf, sfc->regbase + SFC_DMA_ADDR); in rockchip_sfc_fifo_transfer_dma()
453 writel(SFC_DMA_TRIGGER_START, sfc->regbase + SFC_DMA_TRIGGER); in rockchip_sfc_fifo_transfer_dma()
461 dev_dbg(sfc->dev, "sfc xfer_poll len=%x\n", len); in rockchip_sfc_xfer_data_poll()
463 if (op->data.dir == SPI_MEM_DATA_OUT) in rockchip_sfc_xfer_data_poll()
464 return rockchip_sfc_write_fifo(sfc, op->data.buf.out, len); in rockchip_sfc_xfer_data_poll()
466 return rockchip_sfc_read_fifo(sfc, op->data.buf.in, len); in rockchip_sfc_xfer_data_poll()
474 dev_dbg(sfc->dev, "sfc xfer_dma len=%x\n", len); in rockchip_sfc_xfer_data_dma()
476 if (op->data.dir == SPI_MEM_DATA_OUT) { in rockchip_sfc_xfer_data_dma()
477 memcpy(sfc->buffer, op->data.buf.out, len); in rockchip_sfc_xfer_data_dma()
478 dma_sync_single_for_device(sfc->dev, sfc->dma_buffer, len, DMA_TO_DEVICE); in rockchip_sfc_xfer_data_dma()
481 ret = rockchip_sfc_fifo_transfer_dma(sfc, sfc->dma_buffer, len); in rockchip_sfc_xfer_data_dma()
482 if (!wait_for_completion_timeout(&sfc->cp, msecs_to_jiffies(2000))) { in rockchip_sfc_xfer_data_dma()
483 dev_err(sfc->dev, "DMA wait for transfer finish timeout\n"); in rockchip_sfc_xfer_data_dma()
484 ret = -ETIMEDOUT; in rockchip_sfc_xfer_data_dma()
488 if (op->data.dir == SPI_MEM_DATA_IN) { in rockchip_sfc_xfer_data_dma()
489 dma_sync_single_for_cpu(sfc->dev, sfc->dma_buffer, len, DMA_FROM_DEVICE); in rockchip_sfc_xfer_data_dma()
490 memcpy(op->data.buf.in, sfc->buffer, len); in rockchip_sfc_xfer_data_dma()
505 ret = readl_poll_timeout(sfc->regbase + SFC_SR, status, in rockchip_sfc_xfer_done()
511 ret = readl_poll_timeout(sfc->regbase + SFC_SR, status, in rockchip_sfc_xfer_done()
515 dev_err(sfc->dev, "wait sfc idle timeout\n"); in rockchip_sfc_xfer_done()
518 ret = -EIO; in rockchip_sfc_xfer_done()
526 struct rockchip_sfc *sfc = spi_controller_get_devdata(mem->spi->controller); in rockchip_sfc_exec_mem_op()
527 u32 len = op->data.nbytes; in rockchip_sfc_exec_mem_op()
529 u8 cs = spi_get_chipselect(mem->spi, 0); in rockchip_sfc_exec_mem_op()
531 ret = pm_runtime_get_sync(sfc->dev); in rockchip_sfc_exec_mem_op()
533 pm_runtime_put_noidle(sfc->dev); in rockchip_sfc_exec_mem_op()
537 if (unlikely(op->max_freq != sfc->speed[cs]) && in rockchip_sfc_exec_mem_op()
538 !has_acpi_companion(sfc->dev)) { in rockchip_sfc_exec_mem_op()
539 ret = rockchip_sfc_clk_set_rate(sfc, op->max_freq); in rockchip_sfc_exec_mem_op()
542 sfc->speed[cs] = op->max_freq; in rockchip_sfc_exec_mem_op()
543 dev_dbg(sfc->dev, "set_freq=%dHz real_freq=%ldHz\n", in rockchip_sfc_exec_mem_op()
544 sfc->speed[cs], rockchip_sfc_clk_get_rate(sfc)); in rockchip_sfc_exec_mem_op()
550 if (likely(sfc->use_dma) && len >= SFC_DMA_TRANS_THRETHOLD && !(len & 0x3)) { in rockchip_sfc_exec_mem_op()
551 init_completion(&sfc->cp); in rockchip_sfc_exec_mem_op()
559 dev_err(sfc->dev, "xfer data failed ret %d dir %d\n", ret, op->data.dir); in rockchip_sfc_exec_mem_op()
561 ret = -EIO; in rockchip_sfc_exec_mem_op()
568 pm_runtime_mark_last_busy(sfc->dev); in rockchip_sfc_exec_mem_op()
569 pm_runtime_put_autosuspend(sfc->dev); in rockchip_sfc_exec_mem_op()
576 struct rockchip_sfc *sfc = spi_controller_get_devdata(mem->spi->controller); in rockchip_sfc_adjust_op_size()
578 op->data.nbytes = min(op->data.nbytes, sfc->max_iosize); in rockchip_sfc_adjust_op_size()
597 reg = readl(sfc->regbase + SFC_RISR); in rockchip_sfc_irq_handler()
600 writel_relaxed(reg, sfc->regbase + SFC_ICLR); in rockchip_sfc_irq_handler()
603 complete(&sfc->cp); in rockchip_sfc_irq_handler()
613 struct device *dev = &pdev->dev; in rockchip_sfc_probe()
619 host = devm_spi_alloc_host(&pdev->dev, sizeof(*sfc)); in rockchip_sfc_probe()
621 return -ENOMEM; in rockchip_sfc_probe()
623 host->flags = SPI_CONTROLLER_HALF_DUPLEX; in rockchip_sfc_probe()
624 host->mem_ops = &rockchip_sfc_mem_ops; in rockchip_sfc_probe()
625 host->mem_caps = &rockchip_sfc_mem_caps; in rockchip_sfc_probe()
626 host->dev.of_node = pdev->dev.of_node; in rockchip_sfc_probe()
627 host->mode_bits = SPI_TX_QUAD | SPI_TX_DUAL | SPI_RX_QUAD | SPI_RX_DUAL; in rockchip_sfc_probe()
628 host->max_speed_hz = SFC_MAX_SPEED; in rockchip_sfc_probe()
629 host->num_chipselect = SFC_MAX_CHIPSELECT_NUM; in rockchip_sfc_probe()
632 sfc->dev = dev; in rockchip_sfc_probe()
633 sfc->host = host; in rockchip_sfc_probe()
635 sfc->regbase = devm_platform_ioremap_resource(pdev, 0); in rockchip_sfc_probe()
636 if (IS_ERR(sfc->regbase)) in rockchip_sfc_probe()
637 return PTR_ERR(sfc->regbase); in rockchip_sfc_probe()
639 if (!has_acpi_companion(&pdev->dev)) in rockchip_sfc_probe()
640 sfc->clk = devm_clk_get(&pdev->dev, "clk_sfc"); in rockchip_sfc_probe()
641 if (IS_ERR(sfc->clk)) in rockchip_sfc_probe()
642 return dev_err_probe(&pdev->dev, PTR_ERR(sfc->clk), in rockchip_sfc_probe()
643 "Failed to get sfc interface clk\n"); in rockchip_sfc_probe()
645 if (!has_acpi_companion(&pdev->dev)) in rockchip_sfc_probe()
646 sfc->hclk = devm_clk_get(&pdev->dev, "hclk_sfc"); in rockchip_sfc_probe()
647 if (IS_ERR(sfc->hclk)) in rockchip_sfc_probe()
648 return dev_err_probe(&pdev->dev, PTR_ERR(sfc->hclk), in rockchip_sfc_probe()
649 "Failed to get sfc ahb clk\n"); in rockchip_sfc_probe()
651 if (has_acpi_companion(&pdev->dev)) { in rockchip_sfc_probe()
652 ret = device_property_read_u32(&pdev->dev, "clock-frequency", &val); in rockchip_sfc_probe()
654 return dev_err_probe(&pdev->dev, ret, in rockchip_sfc_probe()
655 "Failed to find clock-frequency in ACPI\n"); in rockchip_sfc_probe()
657 sfc->speed[i] = val; in rockchip_sfc_probe()
660 sfc->use_dma = !of_property_read_bool(sfc->dev->of_node, "rockchip,sfc-no-dma"); in rockchip_sfc_probe()
662 ret = clk_prepare_enable(sfc->hclk); in rockchip_sfc_probe()
664 dev_err(&pdev->dev, "Failed to enable ahb clk\n"); in rockchip_sfc_probe()
668 ret = clk_prepare_enable(sfc->clk); in rockchip_sfc_probe()
670 dev_err(&pdev->dev, "Failed to enable interface clk\n"); in rockchip_sfc_probe()
680 0, pdev->name, sfc); in rockchip_sfc_probe()
692 sfc->version = rockchip_sfc_get_version(sfc); in rockchip_sfc_probe()
693 sfc->max_iosize = rockchip_sfc_get_max_iosize(sfc); in rockchip_sfc_probe()
701 if (sfc->use_dma) { in rockchip_sfc_probe()
702 sfc->buffer = (u8 *)__get_free_pages(GFP_KERNEL | GFP_DMA32, in rockchip_sfc_probe()
703 get_order(sfc->max_iosize)); in rockchip_sfc_probe()
704 if (!sfc->buffer) { in rockchip_sfc_probe()
705 ret = -ENOMEM; in rockchip_sfc_probe()
708 sfc->dma_buffer = virt_to_phys(sfc->buffer); in rockchip_sfc_probe()
720 free_pages((unsigned long)sfc->buffer, get_order(sfc->max_iosize)); in rockchip_sfc_probe()
728 clk_disable_unprepare(sfc->clk); in rockchip_sfc_probe()
730 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_probe()
738 struct spi_controller *host = sfc->host; in rockchip_sfc_remove()
741 free_pages((unsigned long)sfc->buffer, get_order(sfc->max_iosize)); in rockchip_sfc_remove()
743 clk_disable_unprepare(sfc->clk); in rockchip_sfc_remove()
744 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_remove()
752 clk_disable_unprepare(sfc->clk); in rockchip_sfc_runtime_suspend()
753 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_runtime_suspend()
763 ret = clk_prepare_enable(sfc->hclk); in rockchip_sfc_runtime_resume()
767 ret = clk_prepare_enable(sfc->clk); in rockchip_sfc_runtime_resume()
769 clk_disable_unprepare(sfc->hclk); in rockchip_sfc_runtime_resume()
823 .name = "rockchip-sfc",
834 MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
836 MODULE_AUTHOR("Jon Lin <Jon.lin@rock-chips.com>");