Lines Matching +full:rx +full:- +full:fifo +full:- +full:depth

1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2008 - 2014 Xilinx, Inc.
7 * based on Blackfin On-Chip SPI Driver (spi_bfin5xx.c)
23 #define CDNS_SPI_NAME "cdns-spi"
36 #define CDNS_SPI_THLD 0x28 /* Transmit FIFO Watermark Register,RW */
61 * SPI Configuration Register - Baud rate and slave select
80 #define CDNS_SPI_IXR_TXOW 0x00000004 /* SPI TX FIFO Overwater */
82 #define CDNS_SPI_IXR_RXNEMTY 0x00000010 /* SPI RX FIFO Not Empty */
100 * struct cdns_spi - This definition defines spi driver instance
106 * @rxbuf: Pointer to the RX buffer
111 * @tx_fifo_depth: Depth of the TX FIFO
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
144 * divisor is set to 4, threshold value for TX FIFO not full interrupt is set
154 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
160 /* Clear the RX FIFO */ in cdns_spi_init_hw()
170 * cdns_spi_chipselect - Select or deselect the chip select line
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
207 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); 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()
234 * cdns_spi_config_clock_freq - Sets clock frequency
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
288 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); 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()
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
333 * On TX empty interrupt this function reads the received data from RX FIFO and
334 * fills the TX FIFO if there is any data remaining to be transferred.
337 * transferred is non-zero.
355 * transferred is non-zero in cdns_spi_irq()
363 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
365 /* Read out the data from the RX FIFO */ 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
406 * This function fills the TX FIFO, starts the SPI transfer and
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()
427 return transfer->len; in cdns_transfer_one()
431 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
449 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
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
476 * IP. The FIFO threshold register is sized so that its maximum value can be the
477 * FIFO size - 1. This is used to detect the size of the FIFO.
481 /* The MSBs will get truncated giving us the size of the FIFO */ in cdns_spi_detect_fifo_depth()
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()
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
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 */