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
102 * struct cdns_spi - This definition defines spi driver instance
104 * @ref_clk: Pointer to the peripheral clock
105 * @pclk: Pointer to the APB clock
106 * @speed_hz: Current SPI bus clock speed in Hz
130 return readl_relaxed(xspi->regs + offset); in cdns_spi_read()
135 writel_relaxed(val, xspi->regs + offset); in cdns_spi_write()
139 * cdns_spi_init_hw - Initialize the hardware and configure the SPI controller
142 * On reset the SPI controller is configured to be in master mode, baud rate
153 if (xspi->is_decoded_cs) in cdns_spi_init_hw()
169 * cdns_spi_chipselect - Select or deselect the chip select line
175 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_chipselect()
186 if (!(xspi->is_decoded_cs)) in cdns_spi_chipselect()
187 ctrl_reg |= ((~(CDNS_SPI_SS0 << spi->chip_select)) << in cdns_spi_chipselect()
191 ctrl_reg |= (spi->chip_select << CDNS_SPI_SS_SHIFT) & in cdns_spi_chipselect()
199 * cdns_spi_config_clock_mode - Sets clock polarity and phase
202 * Sets the requested clock polarity and phase.
206 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_mode()
212 /* Set the SPI clock phase and clock polarity */ in cdns_spi_config_clock_mode()
214 if (spi->mode & SPI_CPHA) in cdns_spi_config_clock_mode()
216 if (spi->mode & SPI_CPOL) in cdns_spi_config_clock_mode()
221 * Just writing the CR register does not seem to apply the clock in cdns_spi_config_clock_mode()
222 * setting changes. This is problematic when changing the clock in cdns_spi_config_clock_mode()
223 * polarity as it will cause the SPI slave to see spurious clock in cdns_spi_config_clock_mode()
233 * cdns_spi_config_clock_freq - Sets clock frequency
238 * Sets the requested clock frequency.
240 * obtained using the prescalar value the driver sets the clock frequency which
249 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_config_clock_freq()
253 frequency = clk_get_rate(xspi->ref_clk); in cdns_spi_config_clock_freq()
257 /* Set the clock frequency */ in cdns_spi_config_clock_freq()
258 if (xspi->speed_hz != transfer->speed_hz) { in cdns_spi_config_clock_freq()
262 (frequency / (2 << baud_rate_val)) > transfer->speed_hz) in cdns_spi_config_clock_freq()
268 xspi->speed_hz = frequency / (2 << baud_rate_val); in cdns_spi_config_clock_freq()
274 * cdns_spi_setup_transfer - Configure SPI controller for specified transfer
280 * sets the requested clock frequency.
287 struct cdns_spi *xspi = spi_master_get_devdata(spi->master); in cdns_spi_setup_transfer()
291 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u clock speed\n", in cdns_spi_setup_transfer()
292 __func__, spi->mode, spi->bits_per_word, in cdns_spi_setup_transfer()
293 xspi->speed_hz); in cdns_spi_setup_transfer()
299 * cdns_spi_fill_tx_fifo - Fills the TX FIFO with as many bytes as possible
307 (xspi->tx_bytes > 0)) { in cdns_spi_fill_tx_fifo()
316 if (xspi->txbuf) in cdns_spi_fill_tx_fifo()
317 cdns_spi_write(xspi, CDNS_SPI_TXD, *xspi->txbuf++); in cdns_spi_fill_tx_fifo()
321 xspi->tx_bytes--; in cdns_spi_fill_tx_fifo()
327 * cdns_spi_irq - Interrupt service routine of the SPI controller
336 * transferred is non-zero.
342 struct spi_master *master = dev_id; in cdns_spi_irq() local
343 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_irq()
353 * transferred is non-zero in cdns_spi_irq()
356 spi_finalize_current_transfer(master); in cdns_spi_irq()
361 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
368 if (xspi->rxbuf) in cdns_spi_irq()
369 *xspi->rxbuf++ = data; in cdns_spi_irq()
371 xspi->rx_bytes--; in cdns_spi_irq()
372 trans_cnt--; in cdns_spi_irq()
375 if (xspi->tx_bytes) { in cdns_spi_irq()
382 spi_finalize_current_transfer(master); in cdns_spi_irq()
390 static int cdns_prepare_message(struct spi_master *master, in cdns_prepare_message() argument
393 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
398 * cdns_transfer_one - Initiates the SPI transfer
399 * @master: Pointer to spi_master structure
409 static int cdns_transfer_one(struct spi_master *master, in cdns_transfer_one() argument
413 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_transfer_one()
415 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
416 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
417 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
418 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
425 return transfer->len; in cdns_transfer_one()
429 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
430 * @master: Pointer to the spi_master structure which provides
433 * This function enables SPI master controller.
437 static int cdns_prepare_transfer_hardware(struct spi_master *master) in cdns_prepare_transfer_hardware() argument
439 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_prepare_transfer_hardware()
447 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
448 * @master: Pointer to the spi_master structure which provides
451 * This function disables the SPI master controller.
455 static int cdns_unprepare_transfer_hardware(struct spi_master *master) in cdns_unprepare_transfer_hardware() argument
457 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_unprepare_transfer_hardware()
465 * cdns_spi_probe - Probe method for the SPI driver
475 struct spi_master *master; in cdns_spi_probe() local
479 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
480 if (!master) in cdns_spi_probe()
481 return -ENOMEM; in cdns_spi_probe()
483 xspi = spi_master_get_devdata(master); in cdns_spi_probe()
484 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
485 platform_set_drvdata(pdev, master); in cdns_spi_probe()
487 xspi->regs = devm_platform_ioremap_resource(pdev, 0); in cdns_spi_probe()
488 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
489 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
493 xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_spi_probe()
494 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
495 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
496 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
500 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_spi_probe()
501 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
502 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
503 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
507 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_probe()
509 dev_err(&pdev->dev, "Unable to enable APB clock.\n"); in cdns_spi_probe()
513 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_probe()
515 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_spi_probe()
519 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
521 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
523 master->num_chipselect = num_cs; in cdns_spi_probe()
525 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
526 &xspi->is_decoded_cs); in cdns_spi_probe()
528 xspi->is_decoded_cs = 0; in cdns_spi_probe()
533 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
534 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
535 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
536 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
540 ret = -ENXIO; in cdns_spi_probe()
544 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
545 0, pdev->name, master); in cdns_spi_probe()
547 ret = -ENXIO; in cdns_spi_probe()
548 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
552 master->use_gpio_descriptors = true; in cdns_spi_probe()
553 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
554 master->prepare_message = cdns_prepare_message; in cdns_spi_probe()
555 master->transfer_one = cdns_transfer_one; in cdns_spi_probe()
556 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
557 master->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
558 master->auto_runtime_pm = true; in cdns_spi_probe()
559 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in cdns_spi_probe()
562 master->max_speed_hz = clk_get_rate(xspi->ref_clk) / 4; in cdns_spi_probe()
563 xspi->speed_hz = master->max_speed_hz; in cdns_spi_probe()
565 master->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
567 ret = spi_register_master(master); in cdns_spi_probe()
569 dev_err(&pdev->dev, "spi_register_master failed\n"); in cdns_spi_probe()
576 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
577 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
578 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_probe()
580 clk_disable_unprepare(xspi->pclk); in cdns_spi_probe()
582 spi_master_put(master); in cdns_spi_probe()
587 * cdns_spi_remove - Remove method for the SPI driver
598 struct spi_master *master = platform_get_drvdata(pdev); in cdns_spi_remove() local
599 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_remove()
603 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_remove()
604 clk_disable_unprepare(xspi->pclk); in cdns_spi_remove()
605 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
606 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
608 spi_unregister_master(master); in cdns_spi_remove()
614 * cdns_spi_suspend - Suspend method for the SPI driver
624 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_suspend() local
626 return spi_master_suspend(master); in cdns_spi_suspend()
630 * cdns_spi_resume - Resume method for the SPI driver
639 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_resume() local
640 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_resume()
643 return spi_master_resume(master); in cdns_spi_resume()
647 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
656 struct spi_master *master = dev_get_drvdata(dev); in cnds_runtime_resume() local
657 struct cdns_spi *xspi = spi_master_get_devdata(master); in cnds_runtime_resume()
660 ret = clk_prepare_enable(xspi->pclk); in cnds_runtime_resume()
662 dev_err(dev, "Cannot enable APB clock.\n"); in cnds_runtime_resume()
666 ret = clk_prepare_enable(xspi->ref_clk); in cnds_runtime_resume()
668 dev_err(dev, "Cannot enable device clock.\n"); in cnds_runtime_resume()
669 clk_disable_unprepare(xspi->pclk); in cnds_runtime_resume()
676 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
685 struct spi_master *master = dev_get_drvdata(dev); in cnds_runtime_suspend() local
686 struct cdns_spi *xspi = spi_master_get_devdata(master); in cnds_runtime_suspend()
688 clk_disable_unprepare(xspi->ref_clk); in cnds_runtime_suspend()
689 clk_disable_unprepare(xspi->pclk); in cnds_runtime_suspend()
701 { .compatible = "xlnx,zynq-spi-r1p6" },
702 { .compatible = "cdns,spi-r1p6" },
707 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */