Lines Matching +full:spi +full:- +full:controller
2 * Cadence SPI controller driver (master mode only)
4 * Copyright (C) 2008 - 2014 Xilinx, Inc.
6 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
24 #include <linux/spi/spi.h>
27 #define CDNS_SPI_NAME "cdns-spi"
44 * SPI Configuration Register bit Masks
47 * of the SPI controller
65 * SPI Configuration Register - Baud rate and slave select
78 * SPI Interrupt Registers bit Masks
83 #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */
84 #define CDNS_SPI_IXR_MODF 0x00000002 /* SPI Mode Fault */
85 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
88 #define CDNS_SPI_IXR_TXFULL 0x00000008 /* SPI TX Full */
89 #define CDNS_SPI_IXR_ALL 0x0000007F /* SPI all interrupts */
92 * SPI Enable Register bit Masks
94 * This register is used to enable or disable the SPI controller
96 #define CDNS_SPI_ER_ENABLE 0x00000001 /* SPI Enable Bit Mask */
97 #define CDNS_SPI_ER_DISABLE 0x0 /* SPI Disable Bit Mask */
99 /* SPI FIFO depth in bytes */
106 * struct cdns_spi - This definition defines spi driver instance
107 * @regs: Virtual address of the SPI controller registers
110 * @speed_hz: Current SPI bus clock speed in Hz
135 /* Macros for the SPI controller read/write */
138 return readl_relaxed(xspi->regs + offset); in cdns_spi_read()
143 writel_relaxed(val, xspi->regs + offset); in cdns_spi_write()
147 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
150 * On reset the SPI controller is configured to be in master mode, baud rate
153 * This function initializes the SPI controller to disable and clear all the
155 * chip select lines, and enable the SPI controller.
161 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
177 * cdns_spi_chipselect - Select or deselect the chip select line
178 * @spi: Pointer to the spi_device structure
181 static void cdns_spi_chipselect(struct spi_device *spi, bool is_high) in cdns_spi_chipselect() argument
183 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_chipselect()
194 if (!(xspi->is_decoded_cs)) in cdns_spi_chipselect()
195 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) << in cdns_spi_chipselect()
199 ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) & in cdns_spi_chipselect()
207 * cdns_spi_config_clock_mode - Sets clock polarity and phase
208 * @spi: Pointer to the spi_device structure
212 static void cdns_spi_config_clock_mode(struct spi_device *spi) in cdns_spi_config_clock_mode() argument
214 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_mode()
220 /* Set the SPI clock phase and clock polarity */ in cdns_spi_config_clock_mode()
222 if (spi->mode & SPI_CPHA) in cdns_spi_config_clock_mode()
224 if (spi->mode & SPI_CPOL) in cdns_spi_config_clock_mode()
231 * polarity as it will cause the SPI slave to see spurious clock in cdns_spi_config_clock_mode()
241 * cdns_spi_config_clock_freq - Sets clock frequency
242 * @spi: Pointer to the spi_device structure
250 * the requested frequency is higher or lower than that is supported by the SPI
251 * controller the driver will set the highest or lowest frequency supported by
252 * controller.
254 static void cdns_spi_config_clock_freq(struct spi_device *spi, in cdns_spi_config_clock_freq() argument
257 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_freq()
261 frequency = clk_get_rate(xspi->ref_clk); in cdns_spi_config_clock_freq()
266 if (xspi->speed_hz != transfer->speed_hz) { in cdns_spi_config_clock_freq()
270 (frequency / (2 << baud_rate_val)) > transfer->speed_hz) in cdns_spi_config_clock_freq()
276 xspi->speed_hz = frequency / (2 << baud_rate_val); in cdns_spi_config_clock_freq()
282 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
283 * @spi: Pointer to the spi_device structure
287 * Sets the operational mode of SPI controller for the next SPI transfer and
292 static int cdns_spi_setup_transfer(struct spi_device *spi, in cdns_spi_setup_transfer() argument
295 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_setup_transfer()
297 cdns_spi_config_clock_freq(spi, transfer); in cdns_spi_setup_transfer()
299 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", in cdns_spi_setup_transfer()
300 __func__, spi->mode, spi->bits_per_word, in cdns_spi_setup_transfer()
301 xspi->speed_hz); in cdns_spi_setup_transfer()
307 * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
315 (xspi->tx_bytes > 0)) { in cdns_spi_fill_tx_fifo()
318 * then spi control did't work thoroughly, add one byte delay in cdns_spi_fill_tx_fifo()
324 if (xspi->txbuf) in cdns_spi_fill_tx_fifo()
325 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); in cdns_spi_fill_tx_fifo()
329 xspi->tx_bytes--; in cdns_spi_fill_tx_fifo()
335 * cdns_spi_irq - Interrupt service routine of the SPI controller
343 * the SPI subsystem will identify the error as the remaining bytes to be
344 * transferred is non-zero.
359 /* Indicate that transfer is completed, the SPI subsystem will in cdns_spi_irq()
361 * transferred is non-zero in cdns_spi_irq()
369 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
376 if (xspi->rxbuf) in cdns_spi_irq()
377 *xspi->rxbuf++ = data; in cdns_spi_irq()
379 xspi->rx_bytes--; in cdns_spi_irq()
380 trans_cnt--; in cdns_spi_irq()
383 if (xspi->tx_bytes) { in cdns_spi_irq()
401 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
406 * cdns_transfer_one - Initiates the SPI transfer
408 * @spi: Pointer to the spi_device structure
412 * This function fills the TX FIFO, starts the SPI transfer and
418 struct spi_device *spi, in cdns_transfer_one() argument
423 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
424 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
425 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
426 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
428 cdns_spi_setup_transfer(spi, transfer); in cdns_transfer_one()
433 return transfer->len; in cdns_transfer_one()
437 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
439 * information about the controller.
441 * This function enables SPI master controller.
455 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
457 * information about the controller.
459 * This function disables the SPI master controller.
472 static int cdns_spi_setup(struct spi_device *spi) in cdns_spi_setup() argument
475 int ret = -EINVAL; in cdns_spi_setup()
476 struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi); in cdns_spi_setup()
478 /* this is a pin managed by the controller, leave it alone */ in cdns_spi_setup()
479 if (spi->cs_gpio == -ENOENT) in cdns_spi_setup()
486 return -ENOMEM; in cdns_spi_setup()
487 cdns_spi_data->gpio_requested = false; in cdns_spi_setup()
488 spi_set_ctldata(spi, cdns_spi_data); in cdns_spi_setup()
492 if (!cdns_spi_data->gpio_requested && gpio_is_valid(spi->cs_gpio)) { in cdns_spi_setup()
493 ret = gpio_request_one(spi->cs_gpio, in cdns_spi_setup()
494 (spi->mode & SPI_CS_HIGH) ? in cdns_spi_setup()
496 dev_name(&spi->dev)); in cdns_spi_setup()
498 dev_err(&spi->dev, "can't request chipselect gpio %d\n", in cdns_spi_setup()
499 spi->cs_gpio); in cdns_spi_setup()
501 cdns_spi_data->gpio_requested = true; in cdns_spi_setup()
503 if (gpio_is_valid(spi->cs_gpio)) { in cdns_spi_setup()
504 int mode = ((spi->mode & SPI_CS_HIGH) ? in cdns_spi_setup()
507 ret = gpio_direction_output(spi->cs_gpio, mode); in cdns_spi_setup()
509 dev_err(&spi->dev, "chipselect gpio %d setup failed (%d)\n", in cdns_spi_setup()
510 spi->cs_gpio, ret); in cdns_spi_setup()
517 static void cdns_spi_cleanup(struct spi_device *spi) in cdns_spi_cleanup() argument
519 struct cdns_spi_device_data *cdns_spi_data = spi_get_ctldata(spi); in cdns_spi_cleanup()
522 if (cdns_spi_data->gpio_requested) in cdns_spi_cleanup()
523 gpio_free(spi->cs_gpio); in cdns_spi_cleanup()
525 spi_set_ctldata(spi, NULL); in cdns_spi_cleanup()
531 * cdns_spi_probe - Probe method for the SPI driver
546 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
548 return -ENOMEM; in cdns_spi_probe()
551 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
555 xspi->regs = devm_ioremap_resource(&pdev->dev, res); in cdns_spi_probe()
556 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
557 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
561 xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_spi_probe()
562 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
563 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
564 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
568 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_spi_probe()
569 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
570 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
571 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
575 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_probe()
577 dev_err(&pdev->dev, "Unable to enable APB clock.\n"); in cdns_spi_probe()
581 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_probe()
583 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_spi_probe()
587 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
589 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
591 master->num_chipselect = num_cs; in cdns_spi_probe()
593 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
594 &xspi->is_decoded_cs); in cdns_spi_probe()
596 xspi->is_decoded_cs = 0; in cdns_spi_probe()
598 /* SPI controller initializations */ in cdns_spi_probe()
601 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
602 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
603 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
604 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
608 ret = -ENXIO; in cdns_spi_probe()
609 dev_err(&pdev->dev, "irq number is invalid\n"); in cdns_spi_probe()
613 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
614 0, pdev->name, master); in cdns_spi_probe()
616 ret = -ENXIO; in cdns_spi_probe()
617 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
621 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
622 master->prepare_message = cdns_prepare_message; in cdns_spi_probe()
623 master->transfer_one = cdns_transfer_one; in cdns_spi_probe()
624 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
625 master->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
626 master->setup = cdns_spi_setup; in cdns_spi_probe()
627 master->cleanup = cdns_spi_cleanup; in cdns_spi_probe()
628 master->auto_runtime_pm = true; in cdns_spi_probe()
629 master->mode_bits = SPI_CPOL | SPI_CPHA; in cdns_spi_probe()
632 master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4; in cdns_spi_probe()
633 xspi->speed_hz = master->max_speed_hz; in cdns_spi_probe()
635 master->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
639 dev_err(&pdev->dev, "spi_register_master failed\n"); in cdns_spi_probe()
646 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
647 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
648 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_probe()
650 clk_disable_unprepare(xspi->pclk); in cdns_spi_probe()
657 * cdns_spi_remove - Remove method for the SPI driver
673 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_remove()
674 clk_disable_unprepare(xspi->pclk); in cdns_spi_remove()
675 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
676 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
684 * cdns_spi_suspend - Suspend method for the SPI driver
687 * This function disables the SPI controller and
700 * cdns_spi_resume - Resume method for the SPI driver
717 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
730 ret = clk_prepare_enable(xspi->pclk); in cnds_runtime_resume()
736 ret = clk_prepare_enable(xspi->ref_clk); in cnds_runtime_resume()
739 clk_disable_unprepare(xspi->pclk); in cnds_runtime_resume()
746 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
758 clk_disable_unprepare(xspi->ref_clk); in cnds_runtime_suspend()
759 clk_disable_unprepare(xspi->pclk); in cnds_runtime_suspend()
771 { .compatible = "xlnx,zynq-spi-r1p6" },
772 { .compatible = "cdns,spi-r1p6" },
777 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */
791 MODULE_DESCRIPTION("Cadence SPI driver");