Skip to contentSkip to conversation

spi: bitbang: Fix lsb-first Rx

PATCH In review Robin Murphy 2 participants 5 messages Aug 3, 2022 – Aug 15, 2022 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: bitbang: Fix lsb-first Rx

Shifting the recieved bit by "bits" inserts it at the top of the *currently remaining* Tx data, so we end up accumulating the whole transfer into bit 0 of the output word. Oops.

For the algorithm to work as intended, we need to remember where the top of the *original* word was, and shift Rx to there.

Fixes 1847e3046c52 ("spi: gpio: Implement LSB First bitbang support")
---
 drivers/spi/spi-bitbang-txrx.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/spi/spi-bitbang-txrx.h +4 -2
index 267342dfa738..2dcbe166df63 100644
@@ -116,6 +116,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
 {
 	/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
 
+	u8 rxbit = bits - 1;
 	u32 oldbit = !(word & 1);
 	/* clock starts at inactive polarity */
 	for (; likely(bits); bits--) {
@@ -135,7 +136,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
 		/* sample LSB (from slave) on leading edge */
 		word >>= 1;
 		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
 		setsck(spi, cpol);
 	}
 	return word;
@@ -148,6 +149,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
 {
 	/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
 
+	u8 rxbit = bits - 1;
 	u32 oldbit = !(word & 1);
 	/* clock starts at inactive polarity */
 	for (; likely(bits); bits--) {
@@ -168,7 +170,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
 		/* sample LSB (from slave) on trailing edge */
 		word >>= 1;
 		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
 	}
 	return word;
 }
Signature · 1 line
2.36.1.dirty
maintainer
#1 lore raw
Quoted 6 lines · Robin Murphy

Shifting the recieved bit by "bits" inserts it at the top of the *currently remaining* Tx data, so we end up accumulating the whole transfer into bit 0 of the output word. Oops.

For the algorithm to work as intended, we need to remember where the top of the *original* word was, and shift Rx to there.

So if this never worked we presumably have some systems out there which somehow rely on the old behaviour that we need to fix somehow - copying in everyone from the original change...

Quoted 47 lines
Fixes 1847e3046c52 ("spi: gpio: Implement LSB First bitbang support")
---
 drivers/spi/spi-bitbang-txrx.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/spi/spi-bitbang-txrx.h +4 -2
index 267342dfa738..2dcbe166df63 100644
@@ -116,6 +116,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
 {
 	/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
 
+	u8 rxbit = bits - 1;
 	u32 oldbit = !(word & 1);
 	/* clock starts at inactive polarity */
 	for (; likely(bits); bits--) {
@@ -135,7 +136,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
 		/* sample LSB (from slave) on leading edge */
 		word >>= 1;
 		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
 		setsck(spi, cpol);
 	}
 	return word;
@@ -148,6 +149,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
 {
 	/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
 
+	u8 rxbit = bits - 1;
 	u32 oldbit = !(word & 1);
 	/* clock starts at inactive polarity */
 	for (; likely(bits); bits--) {
@@ -168,7 +170,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
 		/* sample LSB (from slave) on trailing edge */
 		word >>= 1;
 		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
 	}
 	return word;
 }
Signature · 1 line
2.36.1.dirty
#2 lore raw
Quoted 11 lines · Mark Brown
Quoted 6 lines · Robin Murphy

Shifting the recieved bit by "bits" inserts it at the top of the *currently remaining* Tx data, so we end up accumulating the whole transfer into bit 0 of the output word. Oops.

For the algorithm to work as intended, we need to remember where the top of the *original* word was, and shift Rx to there.

So if this never worked we presumably have some systems out there which somehow rely on the old behaviour that we need to fix somehow - copying in everyone from the original change...

Hmm, I can't imagine anyone's relying too critically on data transfer corruption :/

Apparently these TM16x8 chips are the only thing so far to even want lsb-first bitbang support at all, so I strongly suspect I'm the first person to try the receive side of it (since Rockchip's SPI hardware doesn't seem to support 3-wire half-duplex). It actually took me quite a while to figure out that this wasn't my bug in trying to hook up the matrix-keymap stuff, since half the bytes I was expecting to get back do only have bit 0 set anyway.

Cheers,
Robin.

Quoted 47 lines
Quoted 47 lines
Fixes 1847e3046c52 ("spi: gpio: Implement LSB First bitbang support")
---
  drivers/spi/spi-bitbang-txrx.h | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/spi/spi-bitbang-txrx.h +4 -2
index 267342dfa738..2dcbe166df63 100644
@@ -116,6 +116,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
  {
  	/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
 
+	u8 rxbit = bits - 1;
  	u32 oldbit = !(word & 1);
  	/* clock starts at inactive polarity */
  	for (; likely(bits); bits--) {
@@ -135,7 +136,7 @@ bitbang_txrx_le_cpha0(struct spi_device *spi,
  		/* sample LSB (from slave) on leading edge */
  		word >>= 1;
  		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
  		setsck(spi, cpol);
  	}
  	return word;
@@ -148,6 +149,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
  {
  	/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
 
+	u8 rxbit = bits - 1;
  	u32 oldbit = !(word & 1);
  	/* clock starts at inactive polarity */
  	for (; likely(bits); bits--) {
@@ -168,7 +170,7 @@ bitbang_txrx_le_cpha1(struct spi_device *spi,
  		/* sample LSB (from slave) on trailing edge */
  		word >>= 1;
  		if ((flags & SPI_MASTER_NO_RX) == 0)
-			word |= getmiso(spi) << (bits - 1);
+			word |= getmiso(spi) << rxbit;
  	}
  	return word;
  }
Signature · 1 line
2.36.1.dirty
maintainer
#3 lore raw
Quoted 7 lines · Robin Murphy
Quoted 2 lines · Mark Brown

So if this never worked we presumably have some systems out there which somehow rely on the old behaviour that we need to fix somehow - copying

Hmm, I can't imagine anyone's relying too critically on data transfer corruption :/

Unless it happens to get corrupted into what is actually wanted - it wouldn't be the first time I've seen driver code bodge around some issue in the core. Doesn't seem super likely here given the nature of the bug but it's alway worth checking.

maintainer
#4 lore raw
In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

Shifting the recieved bit by "bits" inserts it at the top of the *currently remaining* Tx data, so we end up accumulating the whole transfer into bit 0 of the output word. Oops.

For the algorithm to work as intended, we need to remember where the top of the *original* word was, and shift Rx to there.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/1] spi: bitbang: Fix lsb-first Rx

      commit: 46f7ac3d7892e808c9ba01c39da6bb85cda26ecd

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark