Lines Matching +full:zynq +full:- +full:spi +full:- +full:r1p6
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cadence SPI controller driver (master mode only)
5 * Copyright (C) 2008 - 2014 Xilinx, Inc.
7 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
20 #include <linux/spi/spi.h>
23 #define CDNS_SPI_NAME "cdns-spi"
40 * SPI Configuration Register bit Masks
43 * of the SPI controller
61 * SPI Configuration Register - Baud rate and slave select
75 * SPI Interrupt Registers bit Masks
80 #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */
81 #define CDNS_SPI_IXR_MODF 0x00000002 /* SPI Mode Fault */
82 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
85 #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */
86 #define CDNS_SPI_IXR_ALL 0x0000007F /* SPI all interrupts */
89 * SPI Enable Register bit Masks
91 * This register is used to enable or disable the SPI controller
93 #define CDNS_SPI_ER_ENABLE 0x00000001 /* SPI Enable Bit Mask */
94 #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */
100 * struct cdns_spi - This definition defines spi driver instance
101 * @regs: Virtual address of the SPI controller registers
104 * @speed_hz: Current SPI bus clock speed in Hz
128 /* Macros for the SPI controller read/write */
131 return readl_relaxed(xspi->regs + offset); in cdns_spi_read()
136 writel_relaxed(val, xspi->regs + offset); in cdns_spi_write()
140 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
143 * On reset the SPI controller is configured to be in master mode, baud rate
146 * This function initializes the SPI controller to disable and clear all the
148 * chip select lines, and enable the SPI controller.
154 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
170 * cdns_spi_chipselect - Select or deselect the chip select line
171 * @spi: Pointer to the spi_device structure
174 static void cdns_spi_chipselect(struct spi_device *spi, bool is_high) in cdns_spi_chipselect() argument
176 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_chipselect()
187 if (!(xspi->is_decoded_cs)) in cdns_spi_chipselect()
188 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) << in cdns_spi_chipselect()
192 ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) & in cdns_spi_chipselect()
200 * cdns_spi_config_clock_mode - Sets clock polarity and phase
201 * @spi: Pointer to the spi_device structure
205 static void cdns_spi_config_clock_mode(struct spi_device *spi) in cdns_spi_config_clock_mode() argument
207 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_mode()
213 /* Set the SPI clock phase and clock polarity */ in cdns_spi_config_clock_mode()
215 if (spi->mode & SPI_CPHA) in cdns_spi_config_clock_mode()
217 if (spi->mode & SPI_CPOL) in cdns_spi_config_clock_mode()
224 * polarity as it will cause the SPI slave to see spurious clock in cdns_spi_config_clock_mode()
234 * cdns_spi_config_clock_freq - Sets clock frequency
235 * @spi: Pointer to the spi_device structure
243 * the requested frequency is higher or lower than that is supported by the SPI
247 static void cdns_spi_config_clock_freq(struct spi_device *spi, in cdns_spi_config_clock_freq() argument
250 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_freq()
254 frequency = xspi->clk_rate; in cdns_spi_config_clock_freq()
259 if (xspi->speed_hz != transfer->speed_hz) { in cdns_spi_config_clock_freq()
263 (frequency / (2 << baud_rate_val)) > transfer->speed_hz) in cdns_spi_config_clock_freq()
269 xspi->speed_hz = frequency / (2 << baud_rate_val); in cdns_spi_config_clock_freq()
275 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
276 * @spi: Pointer to the spi_device structure
280 * Sets the operational mode of SPI controller for the next SPI transfer and
285 static int cdns_spi_setup_transfer(struct spi_device *spi, in cdns_spi_setup_transfer() argument
288 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_setup_transfer()
290 cdns_spi_config_clock_freq(spi, transfer); in cdns_spi_setup_transfer()
292 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", in cdns_spi_setup_transfer()
293 __func__, spi->mode, spi->bits_per_word, in cdns_spi_setup_transfer()
294 xspi->speed_hz); in cdns_spi_setup_transfer()
300 * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
307 while ((trans_cnt < xspi->tx_fifo_depth) && in cdns_spi_fill_tx_fifo()
308 (xspi->tx_bytes > 0)) { in cdns_spi_fill_tx_fifo()
311 * then spi control did't work thoroughly, add one byte delay in cdns_spi_fill_tx_fifo()
317 if (xspi->txbuf) in cdns_spi_fill_tx_fifo()
318 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); in cdns_spi_fill_tx_fifo()
322 xspi->tx_bytes--; in cdns_spi_fill_tx_fifo()
328 * cdns_spi_irq - Interrupt service routine of the SPI controller
336 * the SPI subsystem will identify the error as the remaining bytes to be
337 * transferred is non-zero.
353 /* Indicate that transfer is completed, the SPI subsystem will in cdns_spi_irq()
355 * transferred is non-zero in cdns_spi_irq()
363 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
370 if (xspi->rxbuf) in cdns_spi_irq()
371 *xspi->rxbuf++ = data; in cdns_spi_irq()
373 xspi->rx_bytes--; in cdns_spi_irq()
374 trans_cnt--; in cdns_spi_irq()
377 if (xspi->tx_bytes) { in cdns_spi_irq()
395 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
400 * cdns_transfer_one - Initiates the SPI transfer
402 * @spi: Pointer to the spi_device structure
406 * This function fills the TX FIFO, starts the SPI transfer and
412 struct spi_device *spi, in cdns_transfer_one() argument
417 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
418 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
419 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
420 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
422 cdns_spi_setup_transfer(spi, transfer); in cdns_transfer_one()
427 return transfer->len; in cdns_transfer_one()
431 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
435 * This function enables SPI master controller.
449 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
453 * This function disables the SPI master controller when no slave selected.
462 /* Disable the SPI if slave is deselected */ in cdns_unprepare_transfer_hardware()
472 * cdns_spi_detect_fifo_depth - Detect the FIFO depth of the hardware
475 * The depth of the TX FIFO is a synthesis configuration parameter of the SPI
477 * FIFO size - 1. This is used to detect the size of the FIFO.
483 xspi->tx_fifo_depth = cdns_spi_read(xspi, CDNS_SPI_THLD) + 1; in cdns_spi_detect_fifo_depth()
490 * cdns_spi_probe - Probe method for the SPI driver
504 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
506 return -ENOMEM; in cdns_spi_probe()
509 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
512 xspi->regs = devm_platform_ioremap_resource(pdev, 0); in cdns_spi_probe()
513 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
514 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
518 xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_spi_probe()
519 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
520 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
521 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
525 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_spi_probe()
526 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
527 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
528 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
532 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_probe()
534 dev_err(&pdev->dev, "Unable to enable APB clock.\n"); in cdns_spi_probe()
538 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_probe()
540 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_spi_probe()
544 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
545 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
546 pm_runtime_get_noresume(&pdev->dev); in cdns_spi_probe()
547 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
548 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
550 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
552 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
554 master->num_chipselect = num_cs; in cdns_spi_probe()
556 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
557 &xspi->is_decoded_cs); in cdns_spi_probe()
559 xspi->is_decoded_cs = 0; in cdns_spi_probe()
563 /* SPI controller initializations */ in cdns_spi_probe()
568 ret = -ENXIO; in cdns_spi_probe()
572 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
573 0, pdev->name, master); in cdns_spi_probe()
575 ret = -ENXIO; in cdns_spi_probe()
576 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
580 master->use_gpio_descriptors = true; in cdns_spi_probe()
581 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
582 master->prepare_message = cdns_prepare_message; in cdns_spi_probe()
583 master->transfer_one = cdns_transfer_one; in cdns_spi_probe()
584 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
585 master->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
586 master->auto_runtime_pm = true; in cdns_spi_probe()
587 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in cdns_spi_probe()
589 xspi->clk_rate = clk_get_rate(xspi->ref_clk); in cdns_spi_probe()
591 master->max_speed_hz = xspi->clk_rate / 4; in cdns_spi_probe()
592 xspi->speed_hz = master->max_speed_hz; in cdns_spi_probe()
594 master->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
596 pm_runtime_mark_last_busy(&pdev->dev); in cdns_spi_probe()
597 pm_runtime_put_autosuspend(&pdev->dev); in cdns_spi_probe()
601 dev_err(&pdev->dev, "spi_register_master failed\n"); in cdns_spi_probe()
608 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
609 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
610 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_probe()
612 clk_disable_unprepare(xspi->pclk); in cdns_spi_probe()
619 * cdns_spi_remove - Remove method for the SPI driver
635 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_remove()
636 clk_disable_unprepare(xspi->pclk); in cdns_spi_remove()
637 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
638 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
646 * cdns_spi_suspend - Suspend method for the SPI driver
649 * This function disables the SPI controller and
662 * cdns_spi_resume - Resume method for the SPI driver
679 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
692 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_runtime_resume()
698 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_runtime_resume()
701 clk_disable_unprepare(xspi->pclk); in cdns_spi_runtime_resume()
708 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
720 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_runtime_suspend()
721 clk_disable_unprepare(xspi->pclk); in cdns_spi_runtime_suspend()
733 { .compatible = "xlnx,zynq-spi-r1p6" },
734 { .compatible = "cdns,spi-r1p6" },
739 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
753 MODULE_DESCRIPTION("Cadence SPI driver");