Jiri Slaby c5b460
From: Kevin Groeneveld <kgroeneveld@lenbrook.com>
Jiri Slaby c5b460
Date: Sat, 18 Mar 2023 18:21:32 -0400
Jiri Slaby c5b460
Subject: [PATCH] spi: spi-imx: fix MX51_ECSPI_* macros when cs > 3
Jiri Slaby c5b460
MIME-Version: 1.0
Jiri Slaby c5b460
Content-Type: text/plain; charset=UTF-8
Jiri Slaby c5b460
Content-Transfer-Encoding: 8bit
Jiri Slaby c5b460
References: bsc#1012628
Jiri Slaby c5b460
Patch-mainline: 6.3.4
Jiri Slaby c5b460
Git-commit: 87c614175bbf28d3fd076dc2d166bac759e41427
Jiri Slaby c5b460
Jiri Slaby c5b460
[ Upstream commit 87c614175bbf28d3fd076dc2d166bac759e41427 ]
Jiri Slaby c5b460
Jiri Slaby c5b460
When using gpio based chip select the cs value can go outside the range
Jiri Slaby c5b460
0 – 3. The various MX51_ECSPI_* macros did not take this into consideration
Jiri Slaby c5b460
resulting in possible corruption of the configuration.
Jiri Slaby c5b460
Jiri Slaby c5b460
For example for any cs value over 3 the SCLKPHA bits would not be set and
Jiri Slaby c5b460
other values in the register possibly corrupted.
Jiri Slaby c5b460
Jiri Slaby c5b460
One way to fix this is to just mask the cs bits to 2 bits. This still
Jiri Slaby c5b460
allows all 4 native chip selects to work as well as gpio chip selects
Jiri Slaby c5b460
(which can use any of the 4 chip select configurations).
Jiri Slaby c5b460
Jiri Slaby c5b460
Signed-off-by: Kevin Groeneveld <kgroeneveld@lenbrook.com>
Jiri Slaby c5b460
Link: https://lore.kernel.org/r/20230318222132.3373-1-kgroeneveld@lenbrook.com
Jiri Slaby c5b460
Signed-off-by: Mark Brown <broonie@kernel.org>
Jiri Slaby c5b460
Signed-off-by: Sasha Levin <sashal@kernel.org>
Jiri Slaby c5b460
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Jiri Slaby c5b460
---
Jiri Slaby c5b460
 drivers/spi/spi-imx.c | 24 ++++++++++++++++++------
Jiri Slaby c5b460
 1 file changed, 18 insertions(+), 6 deletions(-)
Jiri Slaby c5b460
Jiri Slaby c5b460
diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c
Jiri Slaby c5b460
index 6c9c87cd..a2a5ada6 100644
Jiri Slaby c5b460
--- a/drivers/spi/spi-imx.c
Jiri Slaby c5b460
+++ b/drivers/spi/spi-imx.c
Jiri Slaby c5b460
@@ -252,6 +252,18 @@ static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device
Jiri Slaby c5b460
 	return true;
Jiri Slaby c5b460
 }
Jiri Slaby c5b460
 
Jiri Slaby c5b460
+/*
Jiri Slaby c5b460
+ * Note the number of natively supported chip selects for MX51 is 4. Some
Jiri Slaby c5b460
+ * devices may have less actual SS pins but the register map supports 4. When
Jiri Slaby c5b460
+ * using gpio chip selects the cs values passed into the macros below can go
Jiri Slaby c5b460
+ * outside the range 0 - 3. We therefore need to limit the cs value to avoid
Jiri Slaby c5b460
+ * corrupting bits outside the allocated locations.
Jiri Slaby c5b460
+ *
Jiri Slaby c5b460
+ * The simplest way to do this is to just mask the cs bits to 2 bits. This
Jiri Slaby c5b460
+ * still allows all 4 native chip selects to work as well as gpio chip selects
Jiri Slaby c5b460
+ * (which can use any of the 4 chip select configurations).
Jiri Slaby c5b460
+ */
Jiri Slaby c5b460
+
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL		0x08
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_ENABLE		(1 <<  0)
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_XCH		(1 <<  2)
Jiri Slaby c5b460
@@ -260,16 +272,16 @@ static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_DRCTL(drctl)	((drctl) << 16)
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_POSTDIV_OFFSET	8
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_PREDIV_OFFSET	12
Jiri Slaby c5b460
-#define MX51_ECSPI_CTRL_CS(cs)		((cs) << 18)
Jiri Slaby c5b460
+#define MX51_ECSPI_CTRL_CS(cs)		((cs & 3) << 18)
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_BL_OFFSET	20
Jiri Slaby c5b460
 #define MX51_ECSPI_CTRL_BL_MASK		(0xfff << 20)
Jiri Slaby c5b460
 
Jiri Slaby c5b460
 #define MX51_ECSPI_CONFIG	0x0c
Jiri Slaby c5b460
-#define MX51_ECSPI_CONFIG_SCLKPHA(cs)	(1 << ((cs) +  0))
Jiri Slaby c5b460
-#define MX51_ECSPI_CONFIG_SCLKPOL(cs)	(1 << ((cs) +  4))
Jiri Slaby c5b460
-#define MX51_ECSPI_CONFIG_SBBCTRL(cs)	(1 << ((cs) +  8))
Jiri Slaby c5b460
-#define MX51_ECSPI_CONFIG_SSBPOL(cs)	(1 << ((cs) + 12))
Jiri Slaby c5b460
-#define MX51_ECSPI_CONFIG_SCLKCTL(cs)	(1 << ((cs) + 20))
Jiri Slaby c5b460
+#define MX51_ECSPI_CONFIG_SCLKPHA(cs)	(1 << ((cs & 3) +  0))
Jiri Slaby c5b460
+#define MX51_ECSPI_CONFIG_SCLKPOL(cs)	(1 << ((cs & 3) +  4))
Jiri Slaby c5b460
+#define MX51_ECSPI_CONFIG_SBBCTRL(cs)	(1 << ((cs & 3) +  8))
Jiri Slaby c5b460
+#define MX51_ECSPI_CONFIG_SSBPOL(cs)	(1 << ((cs & 3) + 12))
Jiri Slaby c5b460
+#define MX51_ECSPI_CONFIG_SCLKCTL(cs)	(1 << ((cs & 3) + 20))
Jiri Slaby c5b460
 
Jiri Slaby c5b460
 #define MX51_ECSPI_INT		0x10
Jiri Slaby c5b460
 #define MX51_ECSPI_INT_TEEN		(1 <<  0)
Jiri Slaby c5b460
-- 
Jiri Slaby c5b460
2.35.3
Jiri Slaby c5b460