18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell Orion SPI controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Shadi Ammouri <shadi@marvell.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Marvell Ltd.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
178c2ecf20Sopenharmony_ci#include <linux/of.h>
188c2ecf20Sopenharmony_ci#include <linux/of_address.h>
198c2ecf20Sopenharmony_ci#include <linux/of_device.h>
208c2ecf20Sopenharmony_ci#include <linux/clk.h>
218c2ecf20Sopenharmony_ci#include <linux/sizes.h>
228c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define DRIVER_NAME			"orion_spi"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Runtime PM autosuspend timeout: PM is fairly light on this driver */
278c2ecf20Sopenharmony_ci#define SPI_AUTOSUSPEND_TIMEOUT		200
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Some SoCs using this driver support up to 8 chip selects.
308c2ecf20Sopenharmony_ci * It is up to the implementer to only use the chip selects
318c2ecf20Sopenharmony_ci * that are available.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define ORION_NUM_CHIPSELECTS		8
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define ORION_SPI_WAIT_RDY_MAX_LOOP	2000 /* in usec */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define ORION_SPI_IF_CTRL_REG		0x00
388c2ecf20Sopenharmony_ci#define ORION_SPI_IF_CONFIG_REG		0x04
398c2ecf20Sopenharmony_ci#define ORION_SPI_IF_RXLSBF		BIT(14)
408c2ecf20Sopenharmony_ci#define ORION_SPI_IF_TXLSBF		BIT(13)
418c2ecf20Sopenharmony_ci#define ORION_SPI_DATA_OUT_REG		0x08
428c2ecf20Sopenharmony_ci#define ORION_SPI_DATA_IN_REG		0x0c
438c2ecf20Sopenharmony_ci#define ORION_SPI_INT_CAUSE_REG		0x10
448c2ecf20Sopenharmony_ci#define ORION_SPI_TIMING_PARAMS_REG	0x18
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Register for the "Direct Mode" */
478c2ecf20Sopenharmony_ci#define SPI_DIRECT_WRITE_CONFIG_REG	0x20
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define ORION_SPI_TMISO_SAMPLE_MASK	(0x3 << 6)
508c2ecf20Sopenharmony_ci#define ORION_SPI_TMISO_SAMPLE_1	(1 << 6)
518c2ecf20Sopenharmony_ci#define ORION_SPI_TMISO_SAMPLE_2	(2 << 6)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define ORION_SPI_MODE_CPOL		(1 << 11)
548c2ecf20Sopenharmony_ci#define ORION_SPI_MODE_CPHA		(1 << 12)
558c2ecf20Sopenharmony_ci#define ORION_SPI_IF_8_16_BIT_MODE	(1 << 5)
568c2ecf20Sopenharmony_ci#define ORION_SPI_CLK_PRESCALE_MASK	0x1F
578c2ecf20Sopenharmony_ci#define ARMADA_SPI_CLK_PRESCALE_MASK	0xDF
588c2ecf20Sopenharmony_ci#define ORION_SPI_MODE_MASK		(ORION_SPI_MODE_CPOL | \
598c2ecf20Sopenharmony_ci					 ORION_SPI_MODE_CPHA)
608c2ecf20Sopenharmony_ci#define ORION_SPI_CS_MASK	0x1C
618c2ecf20Sopenharmony_ci#define ORION_SPI_CS_SHIFT	2
628c2ecf20Sopenharmony_ci#define ORION_SPI_CS(cs)	((cs << ORION_SPI_CS_SHIFT) & \
638c2ecf20Sopenharmony_ci					ORION_SPI_CS_MASK)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cienum orion_spi_type {
668c2ecf20Sopenharmony_ci	ORION_SPI,
678c2ecf20Sopenharmony_ci	ARMADA_SPI,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct orion_spi_dev {
718c2ecf20Sopenharmony_ci	enum orion_spi_type	typ;
728c2ecf20Sopenharmony_ci	/*
738c2ecf20Sopenharmony_ci	 * min_divisor and max_hz should be exclusive, the only we can
748c2ecf20Sopenharmony_ci	 * have both is for managing the armada-370-spi case with old
758c2ecf20Sopenharmony_ci	 * device tree
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci	unsigned long		max_hz;
788c2ecf20Sopenharmony_ci	unsigned int		min_divisor;
798c2ecf20Sopenharmony_ci	unsigned int		max_divisor;
808c2ecf20Sopenharmony_ci	u32			prescale_mask;
818c2ecf20Sopenharmony_ci	bool			is_errata_50mhz_ac;
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistruct orion_direct_acc {
858c2ecf20Sopenharmony_ci	void __iomem		*vaddr;
868c2ecf20Sopenharmony_ci	u32			size;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct orion_child_options {
908c2ecf20Sopenharmony_ci	struct orion_direct_acc direct_access;
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct orion_spi {
948c2ecf20Sopenharmony_ci	struct spi_master	*master;
958c2ecf20Sopenharmony_ci	void __iomem		*base;
968c2ecf20Sopenharmony_ci	struct clk              *clk;
978c2ecf20Sopenharmony_ci	struct clk              *axi_clk;
988c2ecf20Sopenharmony_ci	const struct orion_spi_dev *devdata;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	struct orion_child_options	child[ORION_NUM_CHIPSELECTS];
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	return orion_spi->base + reg;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void
1098c2ecf20Sopenharmony_ciorion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	void __iomem *reg_addr = spi_reg(orion_spi, reg);
1128c2ecf20Sopenharmony_ci	u32 val;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	val = readl(reg_addr);
1158c2ecf20Sopenharmony_ci	val |= mask;
1168c2ecf20Sopenharmony_ci	writel(val, reg_addr);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline void
1208c2ecf20Sopenharmony_ciorion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	void __iomem *reg_addr = spi_reg(orion_spi, reg);
1238c2ecf20Sopenharmony_ci	u32 val;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	val = readl(reg_addr);
1268c2ecf20Sopenharmony_ci	val &= ~mask;
1278c2ecf20Sopenharmony_ci	writel(val, reg_addr);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	u32 tclk_hz;
1338c2ecf20Sopenharmony_ci	u32 rate;
1348c2ecf20Sopenharmony_ci	u32 prescale;
1358c2ecf20Sopenharmony_ci	u32 reg;
1368c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
1378c2ecf20Sopenharmony_ci	const struct orion_spi_dev *devdata;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
1408c2ecf20Sopenharmony_ci	devdata = orion_spi->devdata;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	tclk_hz = clk_get_rate(orion_spi->clk);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (devdata->typ == ARMADA_SPI) {
1458c2ecf20Sopenharmony_ci		/*
1468c2ecf20Sopenharmony_ci		 * Given the core_clk (tclk_hz) and the target rate (speed) we
1478c2ecf20Sopenharmony_ci		 * determine the best values for SPR (in [0 .. 15]) and SPPR (in
1488c2ecf20Sopenharmony_ci		 * [0..7]) such that
1498c2ecf20Sopenharmony_ci		 *
1508c2ecf20Sopenharmony_ci		 * 	core_clk / (SPR * 2 ** SPPR)
1518c2ecf20Sopenharmony_ci		 *
1528c2ecf20Sopenharmony_ci		 * is as big as possible but not bigger than speed.
1538c2ecf20Sopenharmony_ci		 */
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		/* best integer divider: */
1568c2ecf20Sopenharmony_ci		unsigned divider = DIV_ROUND_UP(tclk_hz, speed);
1578c2ecf20Sopenharmony_ci		unsigned spr, sppr;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		if (divider < 16) {
1608c2ecf20Sopenharmony_ci			/* This is the easy case, divider is less than 16 */
1618c2ecf20Sopenharmony_ci			spr = divider;
1628c2ecf20Sopenharmony_ci			sppr = 0;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		} else {
1658c2ecf20Sopenharmony_ci			unsigned two_pow_sppr;
1668c2ecf20Sopenharmony_ci			/*
1678c2ecf20Sopenharmony_ci			 * Find the highest bit set in divider. This and the
1688c2ecf20Sopenharmony_ci			 * three next bits define SPR (apart from rounding).
1698c2ecf20Sopenharmony_ci			 * SPPR is then the number of zero bits that must be
1708c2ecf20Sopenharmony_ci			 * appended:
1718c2ecf20Sopenharmony_ci			 */
1728c2ecf20Sopenharmony_ci			sppr = fls(divider) - 4;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci			/*
1758c2ecf20Sopenharmony_ci			 * As SPR only has 4 bits, we have to round divider up
1768c2ecf20Sopenharmony_ci			 * to the next multiple of 2 ** sppr.
1778c2ecf20Sopenharmony_ci			 */
1788c2ecf20Sopenharmony_ci			two_pow_sppr = 1 << sppr;
1798c2ecf20Sopenharmony_ci			divider = (divider + two_pow_sppr - 1) & -two_pow_sppr;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci			/*
1828c2ecf20Sopenharmony_ci			 * recalculate sppr as rounding up divider might have
1838c2ecf20Sopenharmony_ci			 * increased it enough to change the position of the
1848c2ecf20Sopenharmony_ci			 * highest set bit. In this case the bit that now
1858c2ecf20Sopenharmony_ci			 * doesn't make it into SPR is 0, so there is no need to
1868c2ecf20Sopenharmony_ci			 * round again.
1878c2ecf20Sopenharmony_ci			 */
1888c2ecf20Sopenharmony_ci			sppr = fls(divider) - 4;
1898c2ecf20Sopenharmony_ci			spr = divider >> sppr;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci			/*
1928c2ecf20Sopenharmony_ci			 * Now do range checking. SPR is constructed to have a
1938c2ecf20Sopenharmony_ci			 * width of 4 bits, so this is fine for sure. So we
1948c2ecf20Sopenharmony_ci			 * still need to check for sppr to fit into 3 bits:
1958c2ecf20Sopenharmony_ci			 */
1968c2ecf20Sopenharmony_ci			if (sppr > 7)
1978c2ecf20Sopenharmony_ci				return -EINVAL;
1988c2ecf20Sopenharmony_ci		}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci		prescale = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr;
2018c2ecf20Sopenharmony_ci	} else {
2028c2ecf20Sopenharmony_ci		/*
2038c2ecf20Sopenharmony_ci		 * the supported rates are: 4,6,8...30
2048c2ecf20Sopenharmony_ci		 * round up as we look for equal or less speed
2058c2ecf20Sopenharmony_ci		 */
2068c2ecf20Sopenharmony_ci		rate = DIV_ROUND_UP(tclk_hz, speed);
2078c2ecf20Sopenharmony_ci		rate = roundup(rate, 2);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		/* check if requested speed is too small */
2108c2ecf20Sopenharmony_ci		if (rate > 30)
2118c2ecf20Sopenharmony_ci			return -EINVAL;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		if (rate < 4)
2148c2ecf20Sopenharmony_ci			rate = 4;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		/* Convert the rate to SPI clock divisor value.	*/
2178c2ecf20Sopenharmony_ci		prescale = 0x10 + rate/2;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
2218c2ecf20Sopenharmony_ci	reg = ((reg & ~devdata->prescale_mask) | prescale);
2228c2ecf20Sopenharmony_ci	writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return 0;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic void
2288c2ecf20Sopenharmony_ciorion_spi_mode_set(struct spi_device *spi)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	u32 reg;
2318c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
2368c2ecf20Sopenharmony_ci	reg &= ~ORION_SPI_MODE_MASK;
2378c2ecf20Sopenharmony_ci	if (spi->mode & SPI_CPOL)
2388c2ecf20Sopenharmony_ci		reg |= ORION_SPI_MODE_CPOL;
2398c2ecf20Sopenharmony_ci	if (spi->mode & SPI_CPHA)
2408c2ecf20Sopenharmony_ci		reg |= ORION_SPI_MODE_CPHA;
2418c2ecf20Sopenharmony_ci	if (spi->mode & SPI_LSB_FIRST)
2428c2ecf20Sopenharmony_ci		reg |= ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF;
2438c2ecf20Sopenharmony_ci	else
2448c2ecf20Sopenharmony_ci		reg &= ~(ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void
2508c2ecf20Sopenharmony_ciorion_spi_50mhz_ac_timing_erratum(struct spi_device *spi, unsigned int speed)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	u32 reg;
2538c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/*
2588c2ecf20Sopenharmony_ci	 * Erratum description: (Erratum NO. FE-9144572) The device
2598c2ecf20Sopenharmony_ci	 * SPI interface supports frequencies of up to 50 MHz.
2608c2ecf20Sopenharmony_ci	 * However, due to this erratum, when the device core clock is
2618c2ecf20Sopenharmony_ci	 * 250 MHz and the SPI interfaces is configured for 50MHz SPI
2628c2ecf20Sopenharmony_ci	 * clock and CPOL=CPHA=1 there might occur data corruption on
2638c2ecf20Sopenharmony_ci	 * reads from the SPI device.
2648c2ecf20Sopenharmony_ci	 * Erratum Workaround:
2658c2ecf20Sopenharmony_ci	 * Work in one of the following configurations:
2668c2ecf20Sopenharmony_ci	 * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
2678c2ecf20Sopenharmony_ci	 * Register".
2688c2ecf20Sopenharmony_ci	 * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
2698c2ecf20Sopenharmony_ci	 * Register" before setting the interface.
2708c2ecf20Sopenharmony_ci	 */
2718c2ecf20Sopenharmony_ci	reg = readl(spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
2728c2ecf20Sopenharmony_ci	reg &= ~ORION_SPI_TMISO_SAMPLE_MASK;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (clk_get_rate(orion_spi->clk) == 250000000 &&
2758c2ecf20Sopenharmony_ci			speed == 50000000 && spi->mode & SPI_CPOL &&
2768c2ecf20Sopenharmony_ci			spi->mode & SPI_CPHA)
2778c2ecf20Sopenharmony_ci		reg |= ORION_SPI_TMISO_SAMPLE_2;
2788c2ecf20Sopenharmony_ci	else
2798c2ecf20Sopenharmony_ci		reg |= ORION_SPI_TMISO_SAMPLE_1; /* This is the default value */
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	writel(reg, spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/*
2858c2ecf20Sopenharmony_ci * called only when no transfer is active on the bus
2868c2ecf20Sopenharmony_ci */
2878c2ecf20Sopenharmony_cistatic int
2888c2ecf20Sopenharmony_ciorion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
2918c2ecf20Sopenharmony_ci	unsigned int speed = spi->max_speed_hz;
2928c2ecf20Sopenharmony_ci	unsigned int bits_per_word = spi->bits_per_word;
2938c2ecf20Sopenharmony_ci	int	rc;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if ((t != NULL) && t->speed_hz)
2988c2ecf20Sopenharmony_ci		speed = t->speed_hz;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if ((t != NULL) && t->bits_per_word)
3018c2ecf20Sopenharmony_ci		bits_per_word = t->bits_per_word;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	orion_spi_mode_set(spi);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (orion_spi->devdata->is_errata_50mhz_ac)
3068c2ecf20Sopenharmony_ci		orion_spi_50mhz_ac_timing_erratum(spi, speed);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	rc = orion_spi_baudrate_set(spi, speed);
3098c2ecf20Sopenharmony_ci	if (rc)
3108c2ecf20Sopenharmony_ci		return rc;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (bits_per_word == 16)
3138c2ecf20Sopenharmony_ci		orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
3148c2ecf20Sopenharmony_ci				  ORION_SPI_IF_8_16_BIT_MODE);
3158c2ecf20Sopenharmony_ci	else
3168c2ecf20Sopenharmony_ci		orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
3178c2ecf20Sopenharmony_ci				  ORION_SPI_IF_8_16_BIT_MODE);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return 0;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic void orion_spi_set_cs(struct spi_device *spi, bool enable)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/*
3298c2ecf20Sopenharmony_ci	 * If this line is using a GPIO to control chip select, this internal
3308c2ecf20Sopenharmony_ci	 * .set_cs() function will still be called, so we clear any previous
3318c2ecf20Sopenharmony_ci	 * chip select. The CS we activate will not have any elecrical effect,
3328c2ecf20Sopenharmony_ci	 * as it is handled by a GPIO, but that doesn't matter. What we need
3338c2ecf20Sopenharmony_ci	 * is to deassert the old chip select and assert some other chip select.
3348c2ecf20Sopenharmony_ci	 */
3358c2ecf20Sopenharmony_ci	orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, ORION_SPI_CS_MASK);
3368c2ecf20Sopenharmony_ci	orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG,
3378c2ecf20Sopenharmony_ci			  ORION_SPI_CS(spi->chip_select));
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/*
3408c2ecf20Sopenharmony_ci	 * Chip select logic is inverted from spi_set_cs(). For lines using a
3418c2ecf20Sopenharmony_ci	 * GPIO to do chip select SPI_CS_HIGH is enforced and inversion happens
3428c2ecf20Sopenharmony_ci	 * in the GPIO library, but we don't care about that, because in those
3438c2ecf20Sopenharmony_ci	 * cases we are dealing with an unused native CS anyways so the polarity
3448c2ecf20Sopenharmony_ci	 * doesn't matter.
3458c2ecf20Sopenharmony_ci	 */
3468c2ecf20Sopenharmony_ci	if (!enable)
3478c2ecf20Sopenharmony_ci		orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
3488c2ecf20Sopenharmony_ci	else
3498c2ecf20Sopenharmony_ci		orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	int i;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
3578c2ecf20Sopenharmony_ci		if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
3588c2ecf20Sopenharmony_ci			return 1;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		udelay(1);
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return -1;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic inline int
3678c2ecf20Sopenharmony_ciorion_spi_write_read_8bit(struct spi_device *spi,
3688c2ecf20Sopenharmony_ci			  const u8 **tx_buf, u8 **rx_buf)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	void __iomem *tx_reg, *rx_reg, *int_reg;
3718c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
3748c2ecf20Sopenharmony_ci	tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
3758c2ecf20Sopenharmony_ci	rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
3768c2ecf20Sopenharmony_ci	int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* clear the interrupt cause register */
3798c2ecf20Sopenharmony_ci	writel(0x0, int_reg);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (tx_buf && *tx_buf)
3828c2ecf20Sopenharmony_ci		writel(*(*tx_buf)++, tx_reg);
3838c2ecf20Sopenharmony_ci	else
3848c2ecf20Sopenharmony_ci		writel(0, tx_reg);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (orion_spi_wait_till_ready(orion_spi) < 0) {
3878c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "TXS timed out\n");
3888c2ecf20Sopenharmony_ci		return -1;
3898c2ecf20Sopenharmony_ci	}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (rx_buf && *rx_buf)
3928c2ecf20Sopenharmony_ci		*(*rx_buf)++ = readl(rx_reg);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	return 1;
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic inline int
3988c2ecf20Sopenharmony_ciorion_spi_write_read_16bit(struct spi_device *spi,
3998c2ecf20Sopenharmony_ci			   const u16 **tx_buf, u16 **rx_buf)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	void __iomem *tx_reg, *rx_reg, *int_reg;
4028c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
4058c2ecf20Sopenharmony_ci	tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
4068c2ecf20Sopenharmony_ci	rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
4078c2ecf20Sopenharmony_ci	int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/* clear the interrupt cause register */
4108c2ecf20Sopenharmony_ci	writel(0x0, int_reg);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (tx_buf && *tx_buf)
4138c2ecf20Sopenharmony_ci		writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
4148c2ecf20Sopenharmony_ci	else
4158c2ecf20Sopenharmony_ci		writel(0, tx_reg);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (orion_spi_wait_till_ready(orion_spi) < 0) {
4188c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "TXS timed out\n");
4198c2ecf20Sopenharmony_ci		return -1;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	if (rx_buf && *rx_buf)
4238c2ecf20Sopenharmony_ci		put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	return 1;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic unsigned int
4298c2ecf20Sopenharmony_ciorion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	unsigned int count;
4328c2ecf20Sopenharmony_ci	int word_len;
4338c2ecf20Sopenharmony_ci	struct orion_spi *orion_spi;
4348c2ecf20Sopenharmony_ci	int cs = spi->chip_select;
4358c2ecf20Sopenharmony_ci	void __iomem *vaddr;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	word_len = spi->bits_per_word;
4388c2ecf20Sopenharmony_ci	count = xfer->len;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	orion_spi = spi_master_get_devdata(spi->master);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	/*
4438c2ecf20Sopenharmony_ci	 * Use SPI direct write mode if base address is available. Otherwise
4448c2ecf20Sopenharmony_ci	 * fall back to PIO mode for this transfer.
4458c2ecf20Sopenharmony_ci	 */
4468c2ecf20Sopenharmony_ci	vaddr = orion_spi->child[cs].direct_access.vaddr;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (vaddr && xfer->tx_buf && word_len == 8) {
4498c2ecf20Sopenharmony_ci		unsigned int cnt = count / 4;
4508c2ecf20Sopenharmony_ci		unsigned int rem = count % 4;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		/*
4538c2ecf20Sopenharmony_ci		 * Send the TX-data to the SPI device via the direct
4548c2ecf20Sopenharmony_ci		 * mapped address window
4558c2ecf20Sopenharmony_ci		 */
4568c2ecf20Sopenharmony_ci		iowrite32_rep(vaddr, xfer->tx_buf, cnt);
4578c2ecf20Sopenharmony_ci		if (rem) {
4588c2ecf20Sopenharmony_ci			u32 *buf = (u32 *)xfer->tx_buf;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci			iowrite8_rep(vaddr, &buf[cnt], rem);
4618c2ecf20Sopenharmony_ci		}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		return count;
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (word_len == 8) {
4678c2ecf20Sopenharmony_ci		const u8 *tx = xfer->tx_buf;
4688c2ecf20Sopenharmony_ci		u8 *rx = xfer->rx_buf;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci		do {
4718c2ecf20Sopenharmony_ci			if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
4728c2ecf20Sopenharmony_ci				goto out;
4738c2ecf20Sopenharmony_ci			count--;
4748c2ecf20Sopenharmony_ci			spi_delay_exec(&xfer->word_delay, xfer);
4758c2ecf20Sopenharmony_ci		} while (count);
4768c2ecf20Sopenharmony_ci	} else if (word_len == 16) {
4778c2ecf20Sopenharmony_ci		const u16 *tx = xfer->tx_buf;
4788c2ecf20Sopenharmony_ci		u16 *rx = xfer->rx_buf;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci		do {
4818c2ecf20Sopenharmony_ci			if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
4828c2ecf20Sopenharmony_ci				goto out;
4838c2ecf20Sopenharmony_ci			count -= 2;
4848c2ecf20Sopenharmony_ci			spi_delay_exec(&xfer->word_delay, xfer);
4858c2ecf20Sopenharmony_ci		} while (count);
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ciout:
4898c2ecf20Sopenharmony_ci	return xfer->len - count;
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int orion_spi_transfer_one(struct spi_master *master,
4938c2ecf20Sopenharmony_ci					struct spi_device *spi,
4948c2ecf20Sopenharmony_ci					struct spi_transfer *t)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	int status = 0;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	status = orion_spi_setup_transfer(spi, t);
4998c2ecf20Sopenharmony_ci	if (status < 0)
5008c2ecf20Sopenharmony_ci		return status;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (t->len)
5038c2ecf20Sopenharmony_ci		orion_spi_write_read(spi, t);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return status;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic int orion_spi_setup(struct spi_device *spi)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	return orion_spi_setup_transfer(spi, NULL);
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic int orion_spi_reset(struct orion_spi *orion_spi)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	/* Verify that the CS is deasserted */
5168c2ecf20Sopenharmony_ci	orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/* Don't deassert CS between the direct mapped SPI transfers */
5198c2ecf20Sopenharmony_ci	writel(0, spi_reg(orion_spi, SPI_DIRECT_WRITE_CONFIG_REG));
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return 0;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic const struct orion_spi_dev orion_spi_dev_data = {
5258c2ecf20Sopenharmony_ci	.typ = ORION_SPI,
5268c2ecf20Sopenharmony_ci	.min_divisor = 4,
5278c2ecf20Sopenharmony_ci	.max_divisor = 30,
5288c2ecf20Sopenharmony_ci	.prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
5298c2ecf20Sopenharmony_ci};
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_cistatic const struct orion_spi_dev armada_370_spi_dev_data = {
5328c2ecf20Sopenharmony_ci	.typ = ARMADA_SPI,
5338c2ecf20Sopenharmony_ci	.min_divisor = 4,
5348c2ecf20Sopenharmony_ci	.max_divisor = 1920,
5358c2ecf20Sopenharmony_ci	.max_hz = 50000000,
5368c2ecf20Sopenharmony_ci	.prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
5378c2ecf20Sopenharmony_ci};
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic const struct orion_spi_dev armada_xp_spi_dev_data = {
5408c2ecf20Sopenharmony_ci	.typ = ARMADA_SPI,
5418c2ecf20Sopenharmony_ci	.max_hz = 50000000,
5428c2ecf20Sopenharmony_ci	.max_divisor = 1920,
5438c2ecf20Sopenharmony_ci	.prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
5448c2ecf20Sopenharmony_ci};
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic const struct orion_spi_dev armada_375_spi_dev_data = {
5478c2ecf20Sopenharmony_ci	.typ = ARMADA_SPI,
5488c2ecf20Sopenharmony_ci	.min_divisor = 15,
5498c2ecf20Sopenharmony_ci	.max_divisor = 1920,
5508c2ecf20Sopenharmony_ci	.prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
5518c2ecf20Sopenharmony_ci};
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic const struct orion_spi_dev armada_380_spi_dev_data = {
5548c2ecf20Sopenharmony_ci	.typ = ARMADA_SPI,
5558c2ecf20Sopenharmony_ci	.max_hz = 50000000,
5568c2ecf20Sopenharmony_ci	.max_divisor = 1920,
5578c2ecf20Sopenharmony_ci	.prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
5588c2ecf20Sopenharmony_ci	.is_errata_50mhz_ac = true,
5598c2ecf20Sopenharmony_ci};
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic const struct of_device_id orion_spi_of_match_table[] = {
5628c2ecf20Sopenharmony_ci	{
5638c2ecf20Sopenharmony_ci		.compatible = "marvell,orion-spi",
5648c2ecf20Sopenharmony_ci		.data = &orion_spi_dev_data,
5658c2ecf20Sopenharmony_ci	},
5668c2ecf20Sopenharmony_ci	{
5678c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-370-spi",
5688c2ecf20Sopenharmony_ci		.data = &armada_370_spi_dev_data,
5698c2ecf20Sopenharmony_ci	},
5708c2ecf20Sopenharmony_ci	{
5718c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-375-spi",
5728c2ecf20Sopenharmony_ci		.data = &armada_375_spi_dev_data,
5738c2ecf20Sopenharmony_ci	},
5748c2ecf20Sopenharmony_ci	{
5758c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-380-spi",
5768c2ecf20Sopenharmony_ci		.data = &armada_380_spi_dev_data,
5778c2ecf20Sopenharmony_ci	},
5788c2ecf20Sopenharmony_ci	{
5798c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-390-spi",
5808c2ecf20Sopenharmony_ci		.data = &armada_xp_spi_dev_data,
5818c2ecf20Sopenharmony_ci	},
5828c2ecf20Sopenharmony_ci	{
5838c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-xp-spi",
5848c2ecf20Sopenharmony_ci		.data = &armada_xp_spi_dev_data,
5858c2ecf20Sopenharmony_ci	},
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	{}
5888c2ecf20Sopenharmony_ci};
5898c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic int orion_spi_probe(struct platform_device *pdev)
5928c2ecf20Sopenharmony_ci{
5938c2ecf20Sopenharmony_ci	const struct of_device_id *of_id;
5948c2ecf20Sopenharmony_ci	const struct orion_spi_dev *devdata;
5958c2ecf20Sopenharmony_ci	struct spi_master *master;
5968c2ecf20Sopenharmony_ci	struct orion_spi *spi;
5978c2ecf20Sopenharmony_ci	struct resource *r;
5988c2ecf20Sopenharmony_ci	unsigned long tclk_hz;
5998c2ecf20Sopenharmony_ci	int status = 0;
6008c2ecf20Sopenharmony_ci	struct device_node *np;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	master = spi_alloc_master(&pdev->dev, sizeof(*spi));
6038c2ecf20Sopenharmony_ci	if (master == NULL) {
6048c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "master allocation failed\n");
6058c2ecf20Sopenharmony_ci		return -ENOMEM;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (pdev->id != -1)
6098c2ecf20Sopenharmony_ci		master->bus_num = pdev->id;
6108c2ecf20Sopenharmony_ci	if (pdev->dev.of_node) {
6118c2ecf20Sopenharmony_ci		u32 cell_index;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci		if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
6148c2ecf20Sopenharmony_ci					  &cell_index))
6158c2ecf20Sopenharmony_ci			master->bus_num = cell_index;
6168c2ecf20Sopenharmony_ci	}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	/* we support all 4 SPI modes and LSB first option */
6198c2ecf20Sopenharmony_ci	master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
6208c2ecf20Sopenharmony_ci	master->set_cs = orion_spi_set_cs;
6218c2ecf20Sopenharmony_ci	master->transfer_one = orion_spi_transfer_one;
6228c2ecf20Sopenharmony_ci	master->num_chipselect = ORION_NUM_CHIPSELECTS;
6238c2ecf20Sopenharmony_ci	master->setup = orion_spi_setup;
6248c2ecf20Sopenharmony_ci	master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
6258c2ecf20Sopenharmony_ci	master->auto_runtime_pm = true;
6268c2ecf20Sopenharmony_ci	master->use_gpio_descriptors = true;
6278c2ecf20Sopenharmony_ci	master->flags = SPI_MASTER_GPIO_SS;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, master);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	spi = spi_master_get_devdata(master);
6328c2ecf20Sopenharmony_ci	spi->master = master;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
6358c2ecf20Sopenharmony_ci	devdata = (of_id) ? of_id->data : &orion_spi_dev_data;
6368c2ecf20Sopenharmony_ci	spi->devdata = devdata;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	spi->clk = devm_clk_get(&pdev->dev, NULL);
6398c2ecf20Sopenharmony_ci	if (IS_ERR(spi->clk)) {
6408c2ecf20Sopenharmony_ci		status = PTR_ERR(spi->clk);
6418c2ecf20Sopenharmony_ci		goto out;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	status = clk_prepare_enable(spi->clk);
6458c2ecf20Sopenharmony_ci	if (status)
6468c2ecf20Sopenharmony_ci		goto out;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* The following clock is only used by some SoCs */
6498c2ecf20Sopenharmony_ci	spi->axi_clk = devm_clk_get(&pdev->dev, "axi");
6508c2ecf20Sopenharmony_ci	if (PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) {
6518c2ecf20Sopenharmony_ci		status = -EPROBE_DEFER;
6528c2ecf20Sopenharmony_ci		goto out_rel_clk;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	if (!IS_ERR(spi->axi_clk))
6558c2ecf20Sopenharmony_ci		clk_prepare_enable(spi->axi_clk);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	tclk_hz = clk_get_rate(spi->clk);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	/*
6608c2ecf20Sopenharmony_ci	 * With old device tree, armada-370-spi could be used with
6618c2ecf20Sopenharmony_ci	 * Armada XP, however for this SoC the maximum frequency is
6628c2ecf20Sopenharmony_ci	 * 50MHz instead of tclk/4. On Armada 370, tclk cannot be
6638c2ecf20Sopenharmony_ci	 * higher than 200MHz. So, in order to be able to handle both
6648c2ecf20Sopenharmony_ci	 * SoCs, we can take the minimum of 50MHz and tclk/4.
6658c2ecf20Sopenharmony_ci	 */
6668c2ecf20Sopenharmony_ci	if (of_device_is_compatible(pdev->dev.of_node,
6678c2ecf20Sopenharmony_ci					"marvell,armada-370-spi"))
6688c2ecf20Sopenharmony_ci		master->max_speed_hz = min(devdata->max_hz,
6698c2ecf20Sopenharmony_ci				DIV_ROUND_UP(tclk_hz, devdata->min_divisor));
6708c2ecf20Sopenharmony_ci	else if (devdata->min_divisor)
6718c2ecf20Sopenharmony_ci		master->max_speed_hz =
6728c2ecf20Sopenharmony_ci			DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
6738c2ecf20Sopenharmony_ci	else
6748c2ecf20Sopenharmony_ci		master->max_speed_hz = devdata->max_hz;
6758c2ecf20Sopenharmony_ci	master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6788c2ecf20Sopenharmony_ci	spi->base = devm_ioremap_resource(&pdev->dev, r);
6798c2ecf20Sopenharmony_ci	if (IS_ERR(spi->base)) {
6808c2ecf20Sopenharmony_ci		status = PTR_ERR(spi->base);
6818c2ecf20Sopenharmony_ci		goto out_rel_axi_clk;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	for_each_available_child_of_node(pdev->dev.of_node, np) {
6858c2ecf20Sopenharmony_ci		struct orion_direct_acc *dir_acc;
6868c2ecf20Sopenharmony_ci		u32 cs;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci		/* Get chip-select number from the "reg" property */
6898c2ecf20Sopenharmony_ci		status = of_property_read_u32(np, "reg", &cs);
6908c2ecf20Sopenharmony_ci		if (status) {
6918c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
6928c2ecf20Sopenharmony_ci				"%pOF has no valid 'reg' property (%d)\n",
6938c2ecf20Sopenharmony_ci				np, status);
6948c2ecf20Sopenharmony_ci			continue;
6958c2ecf20Sopenharmony_ci		}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci		/*
6988c2ecf20Sopenharmony_ci		 * Check if an address is configured for this SPI device. If
6998c2ecf20Sopenharmony_ci		 * not, the MBus mapping via the 'ranges' property in the 'soc'
7008c2ecf20Sopenharmony_ci		 * node is not configured and this device should not use the
7018c2ecf20Sopenharmony_ci		 * direct mode. In this case, just continue with the next
7028c2ecf20Sopenharmony_ci		 * device.
7038c2ecf20Sopenharmony_ci		 */
7048c2ecf20Sopenharmony_ci		status = of_address_to_resource(pdev->dev.of_node, cs + 1, r);
7058c2ecf20Sopenharmony_ci		if (status)
7068c2ecf20Sopenharmony_ci			continue;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		/*
7098c2ecf20Sopenharmony_ci		 * Only map one page for direct access. This is enough for the
7108c2ecf20Sopenharmony_ci		 * simple TX transfer which only writes to the first word.
7118c2ecf20Sopenharmony_ci		 * This needs to get extended for the direct SPI NOR / SPI NAND
7128c2ecf20Sopenharmony_ci		 * support, once this gets implemented.
7138c2ecf20Sopenharmony_ci		 */
7148c2ecf20Sopenharmony_ci		dir_acc = &spi->child[cs].direct_access;
7158c2ecf20Sopenharmony_ci		dir_acc->vaddr = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
7168c2ecf20Sopenharmony_ci		if (!dir_acc->vaddr) {
7178c2ecf20Sopenharmony_ci			status = -ENOMEM;
7188c2ecf20Sopenharmony_ci			goto out_rel_axi_clk;
7198c2ecf20Sopenharmony_ci		}
7208c2ecf20Sopenharmony_ci		dir_acc->size = PAGE_SIZE;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	pm_runtime_set_active(&pdev->dev);
7268c2ecf20Sopenharmony_ci	pm_runtime_use_autosuspend(&pdev->dev);
7278c2ecf20Sopenharmony_ci	pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
7288c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	status = orion_spi_reset(spi);
7318c2ecf20Sopenharmony_ci	if (status < 0)
7328c2ecf20Sopenharmony_ci		goto out_rel_pm;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	master->dev.of_node = pdev->dev.of_node;
7358c2ecf20Sopenharmony_ci	status = spi_register_master(master);
7368c2ecf20Sopenharmony_ci	if (status < 0)
7378c2ecf20Sopenharmony_ci		goto out_rel_pm;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return status;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ciout_rel_pm:
7428c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
7438c2ecf20Sopenharmony_ciout_rel_axi_clk:
7448c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->axi_clk);
7458c2ecf20Sopenharmony_ciout_rel_clk:
7468c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->clk);
7478c2ecf20Sopenharmony_ciout:
7488c2ecf20Sopenharmony_ci	spi_master_put(master);
7498c2ecf20Sopenharmony_ci	return status;
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic int orion_spi_remove(struct platform_device *pdev)
7548c2ecf20Sopenharmony_ci{
7558c2ecf20Sopenharmony_ci	struct spi_master *master = platform_get_drvdata(pdev);
7568c2ecf20Sopenharmony_ci	struct orion_spi *spi = spi_master_get_devdata(master);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	pm_runtime_get_sync(&pdev->dev);
7598c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->axi_clk);
7608c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->clk);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	spi_unregister_master(master);
7638c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	return 0;
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRIVER_NAME);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
7718c2ecf20Sopenharmony_cistatic int orion_spi_runtime_suspend(struct device *dev)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	struct spi_master *master = dev_get_drvdata(dev);
7748c2ecf20Sopenharmony_ci	struct orion_spi *spi = spi_master_get_devdata(master);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->axi_clk);
7778c2ecf20Sopenharmony_ci	clk_disable_unprepare(spi->clk);
7788c2ecf20Sopenharmony_ci	return 0;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic int orion_spi_runtime_resume(struct device *dev)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	struct spi_master *master = dev_get_drvdata(dev);
7848c2ecf20Sopenharmony_ci	struct orion_spi *spi = spi_master_get_devdata(master);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	if (!IS_ERR(spi->axi_clk))
7878c2ecf20Sopenharmony_ci		clk_prepare_enable(spi->axi_clk);
7888c2ecf20Sopenharmony_ci	return clk_prepare_enable(spi->clk);
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci#endif
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic const struct dev_pm_ops orion_spi_pm_ops = {
7938c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
7948c2ecf20Sopenharmony_ci			   orion_spi_runtime_resume,
7958c2ecf20Sopenharmony_ci			   NULL)
7968c2ecf20Sopenharmony_ci};
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_cistatic struct platform_driver orion_spi_driver = {
7998c2ecf20Sopenharmony_ci	.driver = {
8008c2ecf20Sopenharmony_ci		.name	= DRIVER_NAME,
8018c2ecf20Sopenharmony_ci		.pm	= &orion_spi_pm_ops,
8028c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(orion_spi_of_match_table),
8038c2ecf20Sopenharmony_ci	},
8048c2ecf20Sopenharmony_ci	.probe		= orion_spi_probe,
8058c2ecf20Sopenharmony_ci	.remove		= orion_spi_remove,
8068c2ecf20Sopenharmony_ci};
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cimodule_platform_driver(orion_spi_driver);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Orion SPI driver");
8118c2ecf20Sopenharmony_ciMODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
8128c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
813