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
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
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()
354 * transferred is non-zero in cdns_spi_irq()
357 spi_finalize_current_transfer(master); in cdns_spi_irq()
362 trans_cnt = xspi->rx_bytes - xspi->tx_bytes; in cdns_spi_irq()
369 if (xspi->rxbuf) in cdns_spi_irq()
370 *xspi->rxbuf++ = data; in cdns_spi_irq()
372 xspi->rx_bytes--; in cdns_spi_irq()
373 trans_cnt--; in cdns_spi_irq()
376 if (xspi->tx_bytes) { in cdns_spi_irq()
383 spi_finalize_current_transfer(master); in cdns_spi_irq()
391 static int cdns_prepare_message(struct spi_master *master, in cdns_prepare_message() argument
394 cdns_spi_config_clock_mode(msg->spi); in cdns_prepare_message()
399 * cdns_transfer_one - Initiates the SPI transfer
400 * @master: Pointer to spi_master structure
410 static int cdns_transfer_one(struct spi_master *master, in cdns_transfer_one() argument
414 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_transfer_one()
416 xspi->txbuf = transfer->tx_buf; in cdns_transfer_one()
417 xspi->rxbuf = transfer->rx_buf; in cdns_transfer_one()
418 xspi->tx_bytes = transfer->len; in cdns_transfer_one()
419 xspi->rx_bytes = transfer->len; in cdns_transfer_one()
426 return transfer->len; in cdns_transfer_one()
430 * cdns_prepare_transfer_hardware - Prepares hardware for transfer.
431 * @master: Pointer to the spi_master structure which provides
434 * This function enables SPI master controller.
438 static int cdns_prepare_transfer_hardware(struct spi_master *master) in cdns_prepare_transfer_hardware() argument
440 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_prepare_transfer_hardware()
448 * cdns_unprepare_transfer_hardware - Relaxes hardware after transfer
449 * @master: Pointer to the spi_master structure which provides
452 * This function disables the SPI master controller.
456 static int cdns_unprepare_transfer_hardware(struct spi_master *master) in cdns_unprepare_transfer_hardware() argument
458 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_unprepare_transfer_hardware()
466 * cdns_spi_probe - Probe method for the SPI driver
476 struct spi_master *master; in cdns_spi_probe() local
480 master = spi_alloc_master(&pdev->dev, sizeof(*xspi)); in cdns_spi_probe()
481 if (!master) in cdns_spi_probe()
482 return -ENOMEM; in cdns_spi_probe()
484 xspi = spi_master_get_devdata(master); in cdns_spi_probe()
485 master->dev.of_node = pdev->dev.of_node; in cdns_spi_probe()
486 platform_set_drvdata(pdev, master); in cdns_spi_probe()
488 xspi->regs = devm_platform_ioremap_resource(pdev, 0); in cdns_spi_probe()
489 if (IS_ERR(xspi->regs)) { in cdns_spi_probe()
490 ret = PTR_ERR(xspi->regs); in cdns_spi_probe()
494 xspi->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_spi_probe()
495 if (IS_ERR(xspi->pclk)) { in cdns_spi_probe()
496 dev_err(&pdev->dev, "pclk clock not found.\n"); in cdns_spi_probe()
497 ret = PTR_ERR(xspi->pclk); in cdns_spi_probe()
501 xspi->ref_clk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_spi_probe()
502 if (IS_ERR(xspi->ref_clk)) { in cdns_spi_probe()
503 dev_err(&pdev->dev, "ref_clk clock not found.\n"); in cdns_spi_probe()
504 ret = PTR_ERR(xspi->ref_clk); in cdns_spi_probe()
508 ret = clk_prepare_enable(xspi->pclk); in cdns_spi_probe()
510 dev_err(&pdev->dev, "Unable to enable APB clock.\n"); in cdns_spi_probe()
514 ret = clk_prepare_enable(xspi->ref_clk); in cdns_spi_probe()
516 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_spi_probe()
520 pm_runtime_use_autosuspend(&pdev->dev); in cdns_spi_probe()
521 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in cdns_spi_probe()
522 pm_runtime_get_noresume(&pdev->dev); in cdns_spi_probe()
523 pm_runtime_set_active(&pdev->dev); in cdns_spi_probe()
524 pm_runtime_enable(&pdev->dev); in cdns_spi_probe()
526 ret = of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs); in cdns_spi_probe()
528 master->num_chipselect = CDNS_SPI_DEFAULT_NUM_CS; in cdns_spi_probe()
530 master->num_chipselect = num_cs; in cdns_spi_probe()
532 ret = of_property_read_u32(pdev->dev.of_node, "is-decoded-cs", in cdns_spi_probe()
533 &xspi->is_decoded_cs); in cdns_spi_probe()
535 xspi->is_decoded_cs = 0; in cdns_spi_probe()
542 ret = -ENXIO; in cdns_spi_probe()
546 ret = devm_request_irq(&pdev->dev, irq, cdns_spi_irq, in cdns_spi_probe()
547 0, pdev->name, master); in cdns_spi_probe()
549 ret = -ENXIO; in cdns_spi_probe()
550 dev_err(&pdev->dev, "request_irq failed\n"); in cdns_spi_probe()
554 master->use_gpio_descriptors = true; in cdns_spi_probe()
555 master->prepare_transfer_hardware = cdns_prepare_transfer_hardware; in cdns_spi_probe()
556 master->prepare_message = cdns_prepare_message; in cdns_spi_probe()
557 master->transfer_one = cdns_transfer_one; in cdns_spi_probe()
558 master->unprepare_transfer_hardware = cdns_unprepare_transfer_hardware; in cdns_spi_probe()
559 master->set_cs = cdns_spi_chipselect; in cdns_spi_probe()
560 master->auto_runtime_pm = true; in cdns_spi_probe()
561 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; in cdns_spi_probe()
563 xspi->clk_rate = clk_get_rate(xspi->ref_clk); in cdns_spi_probe()
565 master->max_speed_hz = xspi->clk_rate / 4; in cdns_spi_probe()
566 xspi->speed_hz = master->max_speed_hz; in cdns_spi_probe()
568 master->bits_per_word_mask = SPI_BPW_MASK(8); in cdns_spi_probe()
570 pm_runtime_mark_last_busy(&pdev->dev); in cdns_spi_probe()
571 pm_runtime_put_autosuspend(&pdev->dev); in cdns_spi_probe()
573 ret = spi_register_master(master); in cdns_spi_probe()
575 dev_err(&pdev->dev, "spi_register_master failed\n"); in cdns_spi_probe()
582 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_probe()
583 pm_runtime_disable(&pdev->dev); in cdns_spi_probe()
584 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_probe()
586 clk_disable_unprepare(xspi->pclk); in cdns_spi_probe()
588 spi_master_put(master); in cdns_spi_probe()
593 * cdns_spi_remove - Remove method for the SPI driver
604 struct spi_master *master = platform_get_drvdata(pdev); in cdns_spi_remove() local
605 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_remove()
609 clk_disable_unprepare(xspi->ref_clk); in cdns_spi_remove()
610 clk_disable_unprepare(xspi->pclk); in cdns_spi_remove()
611 pm_runtime_set_suspended(&pdev->dev); in cdns_spi_remove()
612 pm_runtime_disable(&pdev->dev); in cdns_spi_remove()
614 spi_unregister_master(master); in cdns_spi_remove()
620 * cdns_spi_suspend - Suspend method for the SPI driver
630 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_suspend() local
632 return spi_master_suspend(master); in cdns_spi_suspend()
636 * cdns_spi_resume - Resume method for the SPI driver
645 struct spi_master *master = dev_get_drvdata(dev); in cdns_spi_resume() local
646 struct cdns_spi *xspi = spi_master_get_devdata(master); in cdns_spi_resume()
649 return spi_master_resume(master); in cdns_spi_resume()
653 * cdns_spi_runtime_resume - Runtime resume method for the SPI driver
662 struct spi_master *master = dev_get_drvdata(dev); in cnds_runtime_resume() local
663 struct cdns_spi *xspi = spi_master_get_devdata(master); in cnds_runtime_resume()
666 ret = clk_prepare_enable(xspi->pclk); in cnds_runtime_resume()
668 dev_err(dev, "Cannot enable APB clock.\n"); in cnds_runtime_resume()
672 ret = clk_prepare_enable(xspi->ref_clk); in cnds_runtime_resume()
674 dev_err(dev, "Cannot enable device clock.\n"); in cnds_runtime_resume()
675 clk_disable_unprepare(xspi->pclk); in cnds_runtime_resume()
682 * cdns_spi_runtime_suspend - Runtime suspend method for the SPI driver
691 struct spi_master *master = dev_get_drvdata(dev); in cnds_runtime_suspend() local
692 struct cdns_spi *xspi = spi_master_get_devdata(master); in cnds_runtime_suspend()
694 clk_disable_unprepare(xspi->ref_clk); in cnds_runtime_suspend()
695 clk_disable_unprepare(xspi->pclk); in cnds_runtime_suspend()
707 { .compatible = "xlnx,zynq-spi-r1p6" },
708 { .compatible = "cdns,spi-r1p6" },
713 /* cdns_spi_driver - This structure defines the SPI subsystem platform driver */