Lines Matching +full:zynq +full:- +full:spi +full:- +full:r1p6

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cadence SPI controller driver (host and target mode)
5 * Copyright (C) 2008 - 2014 Xilinx, Inc.
7 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
21 #include <linux/spi/spi.h>
24 #define CDNS_SPI_NAME "cdns-spi"
41 * SPI Configuration Register bit Masks
44 * of the SPI controller
62 * SPI Configuration Register - Baud rate and target select
76 * SPI Interrupt Registers bit Masks
81 #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */
82 #define CDNS_SPI_IXR_MODF 0x00000002 /* SPI Mode Fault */
83 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
86 #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */
87 #define CDNS_SPI_IXR_ALL 0x0000007F /* SPI all interrupts */
90 * SPI Enable Register bit Masks
92 * This register is used to enable or disable the SPI controller
94 #define CDNS_SPI_ER_ENABLE 0x00000001 /* SPI Enable Bit Mask */
95 #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */
101 * struct cdns_spi - This definition defines spi driver instance
102 * @regs: Virtual address of the SPI controller registers
106 * @speed_hz: Current SPI bus clock speed in Hz
130 /* Macros for the SPI controller read/write */
133 return readl_relaxed(xspi->regs + offset); in cdns_spi_read()
138 writel_relaxed(val, xspi->regs + offset); in cdns_spi_write()
142 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
145 * * On reset the SPI controller is configured to target or host mode.
149 * This function initializes the SPI controller to disable and clear all the
151 * chip select lines, and enable the SPI controller.
160 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
176 * cdns_spi_chipselect - Select or deselect the chip select line
177 * @spi: Pointer to the spi_device structure
180 static void cdns_spi_chipselect(struct spi_device *spi, bool is_high) in cdns_spi_chipselect() argument
182 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller); in cdns_spi_chipselect()
193 if (!(xspi->is_decoded_cs)) in cdns_spi_chipselect()
194 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi_get_chipselect(spi, 0))) << in cdns_spi_chipselect()
198 ctrl_reg |= (spi_get_chipselect(spi, 0) << CDNS_SPI_SS_SHIFT) & in cdns_spi_chipselect()
206 * cdns_spi_config_clock_mode - Sets clock polarity and phase
207 * @spi: Pointer to the spi_device structure
211 static void cdns_spi_config_clock_mode(struct spi_device *spi) in cdns_spi_config_clock_mode() argument
213 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller); in cdns_spi_config_clock_mode()
219 /* Set the SPI clock phase and clock polarity */ in cdns_spi_config_clock_mode()
221 if (spi->mode & SPI_CPHA) in cdns_spi_config_clock_mode()
223 if (spi->mode & SPI_CPOL) in cdns_spi_config_clock_mode()
230 * polarity as it will cause the SPI target to see spurious clock in cdns_spi_config_clock_mode()
240 * cdns_spi_config_clock_freq - Sets clock frequency
241 * @spi: Pointer to the spi_device structure
249 * the requested frequency is higher or lower than that is supported by the SPI
253 static void cdns_spi_config_clock_freq(struct spi_device *spi, in cdns_spi_config_clock_freq() argument
256 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller); in cdns_spi_config_clock_freq()
260 frequency = xspi->clk_rate; in cdns_spi_config_clock_freq()
265 if (xspi->speed_hz != transfer->speed_hz) { in cdns_spi_config_clock_freq()
269 (frequency / (2 << baud_rate_val)) > transfer->speed_hz) in cdns_spi_config_clock_freq()
275 xspi->speed_hz = frequency / (2 << baud_rate_val); in cdns_spi_config_clock_freq()
281 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
282 * @spi: Pointer to the spi_device structure
286 * Sets the operational mode of SPI controller for the next SPI transfer and
291 static int cdns_spi_setup_transfer(struct spi_device *spi, in cdns_spi_setup_transfer() argument
294 struct cdns_spi *xspi = spi_controller_get_devdata(spi->controller); in cdns_spi_setup_transfer()
296 cdns_spi_config_clock_freq(spi, transfer); in cdns_spi_setup_transfer()
298 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", in cdns_spi_setup_transfer()
299 __func__, spi->mode, spi->bits_per_word, in cdns_spi_setup_transfer()
300 xspi->speed_hz); in cdns_spi_setup_transfer()
306 * cdns_spi_process_fifo - Fills the TX FIFO, and drain the RX FIFO
313 ntx = clamp(ntx, 0, xspi->tx_bytes); in cdns_spi_process_fifo()
314 nrx = clamp(nrx, 0, xspi->rx_bytes); in cdns_spi_process_fifo()
316 xspi->tx_bytes -= ntx; in cdns_spi_process_fifo()
317 xspi->rx_bytes -= nrx; in cdns_spi_process_fifo()
323 if (xspi->rxbuf) in cdns_spi_process_fifo()
324 *xspi->rxbuf++ = data; in cdns_spi_process_fifo()
326 nrx--; in cdns_spi_process_fifo()
330 if (xspi->txbuf) in cdns_spi_process_fifo()
331 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); in cdns_spi_process_fifo()
335 ntx--; in cdns_spi_process_fifo()
342 * cdns_spi_irq - Interrupt service routine of the SPI controller
350 * the SPI subsystem will identify the error as the remaining bytes to be
351 * transferred is non-zero.
367 /* Indicate that transfer is completed, the SPI subsystem will in cdns_spi_irq()
369 * transferred is non-zero in cdns_spi_irq()
376 int trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
379 trans_cnt -= threshold; in cdns_spi_irq()
384 if (xspi->tx_bytes < xspi->tx_fifo_depth >> 1) in cdns_spi_irq()
387 if (xspi->tx_bytes) { in cdns_spi_irq()
410 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
415 * cdns_transfer_one - Initiates the SPI transfer
417 * @spi: Pointer to the spi_device structure
421 * This function in host mode fills the TX FIFO, starts the SPI transfer and
428 struct spi_device *spi, in cdns_transfer_one() argument
433 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
434 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
435 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
436 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
439 cdns_spi_setup_transfer(spi, transfer); in cdns_transfer_one()
444 if (xspi->tx_bytes > xspi->tx_fifo_depth) in cdns_transfer_one()
445 cdns_spi_write(xspi, CDNS_SPI_THLD, xspi->tx_fifo_depth >> 1); in cdns_transfer_one()
449 * then spi control didn't work thoroughly, add one byte delay in cdns_transfer_one()
454 cdns_spi_process_fifo(xspi, xspi->tx_fifo_depth, 0); in cdns_transfer_one()
457 return transfer->len; in cdns_transfer_one()
461 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
465 * This function enables SPI host controller.
479 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
483 * This function disables the SPI host controller when no target selected.
492 unsigned int cnt = xspi->tx_fifo_depth; in cdns_unprepare_transfer_hardware()
495 while (cnt--) in cdns_unprepare_transfer_hardware()
499 /* Disable the SPI if target is deselected */ in cdns_unprepare_transfer_hardware()
511 * cdns_spi_detect_fifo_depth - Detect the FIFO depth of the hardware
514 * The depth of the TX FIFO is a synthesis configuration parameter of the SPI
516 * FIFO size - 1. This is used to detect the size of the FIFO.
522 xspi->tx_fifo_depth = cdns_spi_read(xspi, CDNS_SPI_THLD) + 1; in cdns_spi_detect_fifo_depth()
529 * cdns_target_abort - Abort target transfer
550 * cdns_spi_probe - Probe method for the SPI driver
565 target = of_property_read_bool(pdev->dev.of_node, "spi-slave"); in cdns_spi_probe()
567 ctlr = spi_alloc_target(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
569 ctlr = spi_alloc_host(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
572 return -ENOMEM; in cdns_spi_probe()
575 ctlr->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
578 xspi->regs = devm_platform_ioremap_resource(pdev, 0); in cdns_spi_probe()
579 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
580 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
584 xspi->pclk = devm_clk_get_enabled(&pdev->dev, "pclk"); in cdns_spi_probe()
585 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
586 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
587 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
592 xspi->ref_clk = devm_clk_get_enabled(&pdev->dev, "ref_clk"); in cdns_spi_probe()
593 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
594 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
595 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
599 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
600 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
601 pm_runtime_get_noresume(&pdev->dev); in cdns_spi_probe()
602 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
603 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
605 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
607 ctlr->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
609 ctlr->num_chipselect = num_cs; in cdns_spi_probe()
611 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
612 &xspi->is_decoded_cs); in cdns_spi_probe()
614 xspi->is_decoded_cs = 0; in cdns_spi_probe()
619 /* SPI controller initializations */ in cdns_spi_probe()
628 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
629 0, pdev->name, ctlr); in cdns_spi_probe()
631 ret = -ENXIO; in cdns_spi_probe()
632 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
636 ctlr->use_gpio_descriptors = true; in cdns_spi_probe()
637 ctlr->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
638 ctlr->prepare_message = cdns_prepare_message; in cdns_spi_probe()
639 ctlr->transfer_one = cdns_transfer_one; in cdns_spi_probe()
640 ctlr->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
641 ctlr->mode_bits = SPI_CPOL | SPI_CPHA; in cdns_spi_probe()
642 ctlr->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
645 ctlr->mode_bits |= SPI_CS_HIGH; in cdns_spi_probe()
646 ctlr->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
647 ctlr->auto_runtime_pm = true; in cdns_spi_probe()
648 xspi->clk_rate = clk_get_rate(xspi->ref_clk); in cdns_spi_probe()
650 ctlr->max_speed_hz = xspi->clk_rate / 4; in cdns_spi_probe()
651 xspi->speed_hz = ctlr->max_speed_hz; in cdns_spi_probe()
652 pm_runtime_mark_last_busy(&pdev->dev); in cdns_spi_probe()
653 pm_runtime_put_autosuspend(&pdev->dev); in cdns_spi_probe()
655 ctlr->mode_bits |= SPI_NO_CS; in cdns_spi_probe()
656 ctlr->target_abort = cdns_target_abort; in cdns_spi_probe()
660 dev_err(&pdev->dev, "spi_register_controller failed\n"); in cdns_spi_probe()
668 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
669 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
677 * cdns_spi_remove - Remove method for the SPI driver
691 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
692 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
698 * cdns_spi_suspend - Suspend method for the SPI driver
701 * This function disables the SPI controller and
714 * cdns_spi_resume - Resume method for the SPI driver
731 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
744 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_runtime_resume()
750 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_runtime_resume()
753 clk_disable_unprepare(xspi->pclk); in cdns_spi_runtime_resume()
760 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
772 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_runtime_suspend()
773 clk_disable_unprepare(xspi->pclk); in cdns_spi_runtime_suspend()
785 { .compatible = "xlnx,zynq-spi-r1p6" },
786 { .compatible = "cdns,spi-r1p6" },
791 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
805 MODULE_DESCRIPTION("Cadence SPI driver");