Lines Matching +full:- +full:spi

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell Orion SPI controller driver
6 * Copyright (C) 2007-2008 Marvell Ltd.
14 #include <linux/spi/spi.h>
73 * have both is for managing the armada-370-spi case with old
110 return orion_spi->base + reg; in spi_reg()
135 static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed) in orion_spi_baudrate_set() argument
144 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_baudrate_set()
145 devdata = orion_spi->devdata; in orion_spi_baudrate_set()
147 tclk_hz = clk_get_rate(orion_spi->clk); in orion_spi_baudrate_set()
149 if (devdata->typ == ARMADA_SPI) { in orion_spi_baudrate_set()
177 sppr = fls(divider) - 4; in orion_spi_baudrate_set()
184 divider = (divider + two_pow_sppr - 1) & -two_pow_sppr; in orion_spi_baudrate_set()
193 sppr = fls(divider) - 4; in orion_spi_baudrate_set()
202 return -EINVAL; in orion_spi_baudrate_set()
216 return -EINVAL; in orion_spi_baudrate_set()
221 /* Convert the rate to SPI clock divisor value. */ in orion_spi_baudrate_set()
226 reg = ((reg & ~devdata->prescale_mask) | prescale); in orion_spi_baudrate_set()
233 orion_spi_mode_set(struct spi_device *spi) in orion_spi_mode_set() argument
238 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_mode_set()
242 if (spi->mode & SPI_CPOL) in orion_spi_mode_set()
244 if (spi->mode & SPI_CPHA) in orion_spi_mode_set()
246 if (spi->mode & SPI_LSB_FIRST) in orion_spi_mode_set()
255 orion_spi_50mhz_ac_timing_erratum(struct spi_device *spi, unsigned int speed) in orion_spi_50mhz_ac_timing_erratum() argument
260 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_50mhz_ac_timing_erratum()
263 * Erratum description: (Erratum NO. FE-9144572) The device in orion_spi_50mhz_ac_timing_erratum()
264 * SPI interface supports frequencies of up to 50 MHz. in orion_spi_50mhz_ac_timing_erratum()
266 * 250 MHz and the SPI interfaces is configured for 50MHz SPI in orion_spi_50mhz_ac_timing_erratum()
268 * reads from the SPI device. in orion_spi_50mhz_ac_timing_erratum()
271 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration in orion_spi_50mhz_ac_timing_erratum()
273 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1 in orion_spi_50mhz_ac_timing_erratum()
279 if (clk_get_rate(orion_spi->clk) == 250000000 && in orion_spi_50mhz_ac_timing_erratum()
280 speed == 50000000 && spi->mode & SPI_CPOL && in orion_spi_50mhz_ac_timing_erratum()
281 spi->mode & SPI_CPHA) in orion_spi_50mhz_ac_timing_erratum()
293 orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t) in orion_spi_setup_transfer() argument
296 unsigned int speed = spi->max_speed_hz; in orion_spi_setup_transfer()
297 unsigned int bits_per_word = spi->bits_per_word; in orion_spi_setup_transfer()
300 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_setup_transfer()
302 if ((t != NULL) && t->speed_hz) in orion_spi_setup_transfer()
303 speed = t->speed_hz; in orion_spi_setup_transfer()
305 if ((t != NULL) && t->bits_per_word) in orion_spi_setup_transfer()
306 bits_per_word = t->bits_per_word; in orion_spi_setup_transfer()
308 orion_spi_mode_set(spi); in orion_spi_setup_transfer()
310 if (orion_spi->devdata->is_errata_50mhz_ac) in orion_spi_setup_transfer()
311 orion_spi_50mhz_ac_timing_erratum(spi, speed); in orion_spi_setup_transfer()
313 rc = orion_spi_baudrate_set(spi, speed); in orion_spi_setup_transfer()
327 static void orion_spi_set_cs(struct spi_device *spi, bool enable) in orion_spi_set_cs() argument
333 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_set_cs()
338 /* Clear existing chip-select and assertion state */ in orion_spi_set_cs()
348 val |= ORION_SPI_CS(spi_get_chipselect(spi, 0)); in orion_spi_set_cs()
378 return -1; in orion_spi_wait_till_ready()
382 orion_spi_write_read_8bit(struct spi_device *spi, in orion_spi_write_read_8bit() argument
389 cs_single_byte = spi->mode & SPI_CS_WORD; in orion_spi_write_read_8bit()
391 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_write_read_8bit()
394 orion_spi_set_cs(spi, 0); in orion_spi_write_read_8bit()
410 orion_spi_set_cs(spi, 1); in orion_spi_write_read_8bit()
414 dev_err(&spi->dev, "TXS timed out\n"); in orion_spi_write_read_8bit()
415 return -1; in orion_spi_write_read_8bit()
422 orion_spi_set_cs(spi, 1); in orion_spi_write_read_8bit()
431 orion_spi_write_read_16bit(struct spi_device *spi, in orion_spi_write_read_16bit() argument
437 if (spi->mode & SPI_CS_WORD) { in orion_spi_write_read_16bit()
438 dev_err(&spi->dev, "SPI_CS_WORD is only supported for 8 bit words\n"); in orion_spi_write_read_16bit()
439 return -1; in orion_spi_write_read_16bit()
442 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_write_read_16bit()
456 dev_err(&spi->dev, "TXS timed out\n"); in orion_spi_write_read_16bit()
457 return -1; in orion_spi_write_read_16bit()
467 orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer) in orion_spi_write_read() argument
472 int cs = spi_get_chipselect(spi, 0); in orion_spi_write_read()
475 word_len = spi->bits_per_word; in orion_spi_write_read()
476 count = xfer->len; in orion_spi_write_read()
478 orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_write_read()
481 * Use SPI direct write mode if base address is available in orion_spi_write_read()
485 vaddr = orion_spi->child[cs].direct_access.vaddr; in orion_spi_write_read()
487 if (vaddr && xfer->tx_buf && word_len == 8 && (spi->mode & SPI_CS_WORD) == 0) { in orion_spi_write_read()
492 * Send the TX-data to the SPI device via the direct in orion_spi_write_read()
495 iowrite32_rep(vaddr, xfer->tx_buf, cnt); in orion_spi_write_read()
497 u32 *buf = (u32 *)xfer->tx_buf; in orion_spi_write_read()
506 const u8 *tx = xfer->tx_buf; in orion_spi_write_read()
507 u8 *rx = xfer->rx_buf; in orion_spi_write_read()
510 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0) in orion_spi_write_read()
512 count--; in orion_spi_write_read()
513 spi_delay_exec(&xfer->word_delay, xfer); in orion_spi_write_read()
516 const u16 *tx = xfer->tx_buf; in orion_spi_write_read()
517 u16 *rx = xfer->rx_buf; in orion_spi_write_read()
520 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0) in orion_spi_write_read()
522 count -= 2; in orion_spi_write_read()
523 spi_delay_exec(&xfer->word_delay, xfer); in orion_spi_write_read()
528 return xfer->len - count; in orion_spi_write_read()
532 struct spi_device *spi, in orion_spi_transfer_one() argument
537 status = orion_spi_setup_transfer(spi, t); in orion_spi_transfer_one()
541 if (t->len) in orion_spi_transfer_one()
542 orion_spi_write_read(spi, t); in orion_spi_transfer_one()
547 static int orion_spi_setup(struct spi_device *spi) in orion_spi_setup() argument
551 struct orion_spi *orion_spi = spi_controller_get_devdata(spi->controller); in orion_spi_setup()
552 struct device *dev = orion_spi->dev; in orion_spi_setup()
557 ret = orion_spi_setup_transfer(spi, NULL); in orion_spi_setup()
571 /* Don't deassert CS between the direct mapped SPI transfers */ in orion_spi_reset()
616 .compatible = "marvell,orion-spi",
620 .compatible = "marvell,armada-370-spi",
624 .compatible = "marvell,armada-375-spi",
628 .compatible = "marvell,armada-380-spi",
632 .compatible = "marvell,armada-390-spi",
636 .compatible = "marvell,armada-xp-spi",
648 struct orion_spi *spi; in orion_spi_probe() local
654 host = spi_alloc_host(&pdev->dev, sizeof(*spi)); in orion_spi_probe()
656 dev_dbg(&pdev->dev, "host allocation failed\n"); in orion_spi_probe()
657 return -ENOMEM; in orion_spi_probe()
660 if (pdev->id != -1) in orion_spi_probe()
661 host->bus_num = pdev->id; in orion_spi_probe()
662 if (pdev->dev.of_node) { in orion_spi_probe()
665 if (!of_property_read_u32(pdev->dev.of_node, "cell-index", in orion_spi_probe()
667 host->bus_num = cell_index; in orion_spi_probe()
670 /* we support all 4 SPI modes and LSB first option */ in orion_spi_probe()
671 host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST | SPI_CS_WORD; in orion_spi_probe()
672 host->set_cs = orion_spi_set_cs; in orion_spi_probe()
673 host->transfer_one = orion_spi_transfer_one; in orion_spi_probe()
674 host->num_chipselect = ORION_NUM_CHIPSELECTS; in orion_spi_probe()
675 host->setup = orion_spi_setup; in orion_spi_probe()
676 host->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16); in orion_spi_probe()
677 host->auto_runtime_pm = true; in orion_spi_probe()
678 host->use_gpio_descriptors = true; in orion_spi_probe()
679 host->flags = SPI_CONTROLLER_GPIO_SS; in orion_spi_probe()
683 spi = spi_controller_get_devdata(host); in orion_spi_probe()
684 spi->host = host; in orion_spi_probe()
685 spi->dev = &pdev->dev; in orion_spi_probe()
687 devdata = device_get_match_data(&pdev->dev); in orion_spi_probe()
689 spi->devdata = devdata; in orion_spi_probe()
691 spi->clk = devm_clk_get_enabled(&pdev->dev, NULL); in orion_spi_probe()
692 if (IS_ERR(spi->clk)) { in orion_spi_probe()
693 status = PTR_ERR(spi->clk); in orion_spi_probe()
698 spi->axi_clk = devm_clk_get(&pdev->dev, "axi"); in orion_spi_probe()
699 if (PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) { in orion_spi_probe()
700 status = -EPROBE_DEFER; in orion_spi_probe()
703 if (!IS_ERR(spi->axi_clk)) in orion_spi_probe()
704 clk_prepare_enable(spi->axi_clk); in orion_spi_probe()
706 tclk_hz = clk_get_rate(spi->clk); in orion_spi_probe()
709 * With old device tree, armada-370-spi could be used with in orion_spi_probe()
715 if (of_device_is_compatible(pdev->dev.of_node, in orion_spi_probe()
716 "marvell,armada-370-spi")) in orion_spi_probe()
717 host->max_speed_hz = min(devdata->max_hz, in orion_spi_probe()
718 DIV_ROUND_UP(tclk_hz, devdata->min_divisor)); in orion_spi_probe()
719 else if (devdata->min_divisor) in orion_spi_probe()
720 host->max_speed_hz = in orion_spi_probe()
721 DIV_ROUND_UP(tclk_hz, devdata->min_divisor); in orion_spi_probe()
723 host->max_speed_hz = devdata->max_hz; in orion_spi_probe()
724 host->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor); in orion_spi_probe()
726 spi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r); in orion_spi_probe()
727 if (IS_ERR(spi->base)) { in orion_spi_probe()
728 status = PTR_ERR(spi->base); in orion_spi_probe()
732 for_each_available_child_of_node(pdev->dev.of_node, np) { in orion_spi_probe()
736 /* Get chip-select number from the "reg" property */ in orion_spi_probe()
739 dev_err(&pdev->dev, in orion_spi_probe()
746 * Check if an address is configured for this SPI device. If in orion_spi_probe()
752 status = of_address_to_resource(pdev->dev.of_node, cs + 1, r); in orion_spi_probe()
759 * This needs to get extended for the direct SPI NOR / SPI NAND in orion_spi_probe()
762 dir_acc = &spi->child[cs].direct_access; in orion_spi_probe()
763 dir_acc->vaddr = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE); in orion_spi_probe()
764 if (!dir_acc->vaddr) { in orion_spi_probe()
765 status = -ENOMEM; in orion_spi_probe()
769 dir_acc->size = PAGE_SIZE; in orion_spi_probe()
771 dev_info(&pdev->dev, "CS%d configured for direct access\n", cs); in orion_spi_probe()
774 pm_runtime_set_active(&pdev->dev); in orion_spi_probe()
775 pm_runtime_use_autosuspend(&pdev->dev); in orion_spi_probe()
776 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); in orion_spi_probe()
777 pm_runtime_enable(&pdev->dev); in orion_spi_probe()
779 status = orion_spi_reset(spi); in orion_spi_probe()
783 host->dev.of_node = pdev->dev.of_node; in orion_spi_probe()
791 pm_runtime_disable(&pdev->dev); in orion_spi_probe()
793 clk_disable_unprepare(spi->axi_clk); in orion_spi_probe()
803 struct orion_spi *spi = spi_controller_get_devdata(host); in orion_spi_remove() local
805 pm_runtime_get_sync(&pdev->dev); in orion_spi_remove()
806 clk_disable_unprepare(spi->axi_clk); in orion_spi_remove()
809 pm_runtime_disable(&pdev->dev); in orion_spi_remove()
818 struct orion_spi *spi = spi_controller_get_devdata(host); in orion_spi_runtime_suspend() local
820 clk_disable_unprepare(spi->axi_clk); in orion_spi_runtime_suspend()
821 clk_disable_unprepare(spi->clk); in orion_spi_runtime_suspend()
828 struct orion_spi *spi = spi_controller_get_devdata(host); in orion_spi_runtime_resume() local
830 if (!IS_ERR(spi->axi_clk)) in orion_spi_runtime_resume()
831 clk_prepare_enable(spi->axi_clk); in orion_spi_runtime_resume()
832 return clk_prepare_enable(spi->clk); in orion_spi_runtime_resume()
854 MODULE_DESCRIPTION("Orion SPI driver");