Lines Matching +full:clock +full:- +full:master

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)
23 #define CDNS_SPI_NAME "cdns-spi"
46 #define CDNS_SPI_CR_CPHA 0x00000004 /* Clock Phase Control */
47 #define CDNS_SPI_CR_CPOL 0x00000002 /* Clock Polarity Control */
51 #define CDNS_SPI_CR_MSTREN 0x00000001 /* Master Enable Mask */
61 * SPI Configuration Register - Baud rate and slave select
100 * struct cdns_spi - This definition defines spi driver instance
102 * @ref_clk: Pointer to the peripheral clock
103 * @pclk: Pointer to the APB clock
104 * @speed_hz: Current SPI bus clock speed in Hz
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
154 if (xspi->is_decoded_cs) 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
203 * Sets the requested clock polarity and phase.
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()
222 * Just writing the CR register does not seem to apply the clock in cdns_spi_config_clock_mode()
223 * setting changes. This is problematic when changing the clock 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
239 * Sets the requested clock frequency.
241 * obtained using the prescalar value the driver sets the clock frequency which
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()
258 /* Set the clock frequency */ 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
281 * sets the requested clock frequency.
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
337 * transferred is non-zero.
343 struct spi_master *master = dev_id; in cdns_spi_irq() local
344 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_irq()
355 * transferred is non-zero in cdns_spi_irq()
358 spi_finalize_current_transfer(master); 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()
384 spi_finalize_current_transfer(master); in cdns_spi_irq()
392 static int cdns_prepare_message(struct spi_master *master, in cdns_prepare_message() argument
395 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
400 * cdns_transfer_one - Initiates the SPI transfer
401 * @master: Pointer to spi_master structure
411 static int cdns_transfer_one(struct spi_master *master, in cdns_transfer_one() argument
415 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_transfer_one()
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.
432 * @master: Pointer to the spi_master structure which provides
435 * This function enables SPI master controller.
439 static int cdns_prepare_transfer_hardware(struct spi_master *master) in cdns_prepare_transfer_hardware() argument
441 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_prepare_transfer_hardware()
449 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
450 * @master: Pointer to the spi_master structure which provides
453 * This function disables the SPI master controller when no slave selected.
457 static int cdns_unprepare_transfer_hardware(struct spi_master *master) in cdns_unprepare_transfer_hardware() argument
459 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_unprepare_transfer_hardware()
472 * cdns_spi_detect_fifo_depth - Detect the FIFO depth of the hardware
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
500 struct spi_master *master; in cdns_spi_probe() local
504 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
505 if (!master) in cdns_spi_probe()
506 return -ENOMEM; in cdns_spi_probe()
508 xspi = spi_master_get_devdata(master); in cdns_spi_probe()
509 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
510 platform_set_drvdata(pdev, master); 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()
599 ret = spi_register_master(master); 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()
614 spi_master_put(master); in cdns_spi_probe()
619 * cdns_spi_remove - Remove method for the SPI driver
630 struct spi_master *master = platform_get_drvdata(pdev); in cdns_spi_remove() local
631 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_remove()
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()
640 spi_unregister_master(master); in cdns_spi_remove()
646 * cdns_spi_suspend - Suspend method for the SPI driver
656 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_suspend() local
658 return spi_master_suspend(master); in cdns_spi_suspend()
662 * cdns_spi_resume - Resume method for the SPI driver
671 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_resume() local
672 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_resume()
675 return spi_master_resume(master); in cdns_spi_resume()
679 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
688 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_runtime_resume() local
689 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_runtime_resume()
692 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_runtime_resume()
694 dev_err(dev, "Cannot enable APB clock.\n"); in cdns_spi_runtime_resume()
698 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_runtime_resume()
700 dev_err(dev, "Cannot enable device clock.\n"); 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
717 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_runtime_suspend() local
718 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_runtime_suspend()
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 */