Revert "spi: bitbang: only toggle bitchanges"
PATCH 1/1 Revert "spi: bitbang: only toggle bitchanges"
This reverts commit 232a5adc5199 ("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).
--- drivers/spi/spi-bitbang-txrx.h | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/spi/spi-bitbang-txrx.h +4 -14
@@ -49,17 +49,12 @@ bitbang_txrx_be_cpha0(struct spi_device *spi, {/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */ -bool oldbit = !(word &1 );/* clock starts at inactive polarity */ for (word <<= (32 - bits);likely (bits); bits--) {/* setup MSB (to slave) on trailing edge */ -if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) { -setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 ); - } - } +if ((flags &SPI_MASTER_NO_TX ) ==0 ) +setmosi (spi, word & (1 <<31 ));spidelay (nsecs);/* T(setup) */ setsck (spi, !cpol);
@@ -81,18 +76,13 @@ bitbang_txrx_be_cpha1(struct spi_device *spi, {/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */ -bool oldbit = !(word & (1 <<31 ));/* clock starts at inactive polarity */ for (word <<= (32 - bits);likely (bits); bits--) {/* setup MSB (to slave) on leading edge */ setsck (spi, !cpol); -if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) { -setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 ); - } - } +if ((flags &SPI_MASTER_NO_TX ) ==0 ) +setmosi (spi, word & (1 <<31 ));spidelay (nsecs);/* T(setup) */ setsck (spi, cpol);
Signature · 6 lines
2.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Lars Persson
This reverts commit
232a5adc5199("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.
This might be a real issue but fixing it does not require a revert.
Quoted 3 lines
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).
No, any non-zero value is true in C. I assume you're talking about this section of the diff:
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 60
Quoted 4 lines
drivers/spi/spi-bitbang-txrx.h +0
-4-if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) { -setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 );
which looks fine - we see if the top bit is the same as the last top bit we wrote, if it isn't we update the value output and record what we just wrote.
Quoted 16 lines · Mark Brown
Quoted 5 lines · Lars Persson
This reverts commit
232a5adc5199("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.This might be a real issue but fixing it does not require a revert.
Quoted 3 lines
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).
No, any non-zero value is true in C. I assume you're talking about this section of the diff:
Quoted 8 lines
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 60
Quoted 4 lines
drivers/spi/spi-bitbang-txrx.h +0
-4-if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) { -setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 );which looks fine - we see if the top bit is the same as the last top bit we wrote, if it isn't we update the value output and record what we just wrote.
No this is a misunderstanding about the semantics of the bool type. When assigning to the bool any non-zero value becomes 1. When comparing an integer against the bool, the bool is promoted to an integer.
If we previously transmitted a one, oldbit equals 1. If current bit is a one, then (word & (1 << 31)) equals 1<<31.
((1<<31) != 1) is TRUE so we call setmosi again, hence no optimization for consecutive ones.
Please do not mix bool and bitwise operations.
- Lars
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 7 lines · Mark Brown
Quoted 5 lines · Lars Persson
This reverts commit
232a5adc5199("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.
Quoted 8 lines
This might be a real issue but fixing it does not require a revert.
Quoted 3 lines
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).
No, any non-zero value is true in C. I assume you're talking about this section of the diff:
While any value will be treated as true, the inverse of a value (!) can only ever be 0 or 1, so
Quoted 1 line
Quoted 1 line
- bool oldbit = !(word & 1);
makes oldbit to be either 0 or 1, and here
Quoted 1 line
Quoted 1 line
- if ((word & (1 << 31)) != oldbit) {
we compare the bool with an unsigned integer, thus the bool gets promoted to unsigned integer as well, and we compare
0 or 1 with 0 or (1 << 31), so if oldbit is 1 this condition will always evaluate to true.
Feel free to verify with a compiler of your choice ;-)
The fix for this is to use !!(word & (1 << 31)).
Jonas
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 26 lines · Lars Persson
Quoted 16 lines · Mark Brown
Quoted 5 lines · Lars Persson
This reverts commit
232a5adc5199("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.This might be a real issue but fixing it does not require a revert.
Quoted 3 lines
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).
No, any non-zero value is true in C. I assume you're talking about this section of the diff:
Quoted 8 lines
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 60
Quoted 4 lines
drivers/spi/spi-bitbang-txrx.h +0
-4-if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) { -setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 );which looks fine - we see if the top bit is the same as the last top bit we wrote, if it isn't we update the value output and record what we just wrote.
Quoted 4 lines
No this is a misunderstanding about the semantics of the bool type. When assigning to the bool any non-zero value becomes 1. When comparing an integer against the bool, the bool is promoted to an integer.
From the patch context alone it is not clear that oldbit is of type bool. I think this is where the confusion came from. By just looking at the path fragment itself you'd think that oldbit is a unsigned int given its usage pattern. But it becomes obvious that the code wont work if you know oldbit is a bool.
- Lars
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 20 lines · Lars Persson
This reverts commit
232a5adc5199("spi: bitbang: only toggle bitchanges") because it breaks bitbanged SPI on our MIPS system. I found two problems with the patch:
- oldbit must initially be computed from bit position 7, 15 or 31 of word depending on the value of bits.
- The optimization also does not eliminate consecutive ones because the compare of 1<<31 and 1 will be false (a bool only takes values 0 or 1).Signed-off-by Lars Persson <[email protected]>--- drivers/spi/spi-bitbang-txrx.h | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)drivers/spi/spi-bitbang-txrx.h +0
-0@@ -49,17 +49,12 @@ bitbang_txrx_be_cpha0(struct spi_device *spi, {
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 59
Quoted 10 lines
drivers/spi/spi-bitbang-txrx.h +0
-3/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */ -bool oldbit = !(word &1 );/* clock starts at inactive polarity */ for (word <<= (32 - bits);likely (bits); bits--) {/* setup MSB (to slave) on trailing edge */ -if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) {
This needs to become:
if (!!(word & (1 << 31)) != oldbit) {so oldbit really get compared by bool and not by int.
Quoted 11 lines
drivers/spi/spi-bitbang-txrx.h +2
-4-setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 ); - } - } +if ((flags &SPI_MASTER_NO_TX ) ==0 ) +setmosi (spi, word & (1 <<31 ));spidelay (nsecs);/* T(setup) */ drivers/spi/spi-bitbang-txrx.h +0
-0setsck (spi, !cpol);@@ -81,18 +76,13 @@ bitbang_txrx_be_cpha1(struct spi_device *spi, {
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha1(struct spi_device *spi, · line 91
Quoted 10 lines
drivers/spi/spi-bitbang-txrx.h +0
-3/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */ -bool oldbit = !(word & (1 <<31 ));/* clock starts at inactive polarity */ for (word <<= (32 - bits);likely (bits); bits--) {/* setup MSB (to slave) on leading edge */ setsck (spi, !cpol); -if ((flags &SPI_MASTER_NO_TX ) ==0 ) { -if ((word & (1 <<31 )) != oldbit) {
and this needs to become:
if (!!(word & (1 << 31)) != oldbit) {drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha1(struct spi_device *spi, · line 88
Quoted 9 lines
drivers/spi/spi-bitbang-txrx.h +2
-4-setmosi (spi, word & (1 <<31 )); - oldbit = word & (1 <<31 ); - } - } +if ((flags &SPI_MASTER_NO_TX ) ==0 ) +setmosi (spi, word & (1 <<31 ));spidelay (nsecs);/* T(setup) */ setsck (spi, cpol);
Sorry for the issue,
Michael
Signature · 8 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 7 lines · Lars-Peter Clausen
Quoted 4 lines · Lars Persson
No this is a misunderstanding about the semantics of the bool type. When assigning to the bool any non-zero value becomes 1. When comparing an integer against the bool, the bool is promoted to an integer.
Quoted 5 lines
From the patch context alone it is not clear that oldbit is of type bool. I think this is where the confusion came from. By just looking at the path fragment itself you'd think that oldbit is a unsigned int given its usage pattern. But it becomes obvious that the code wont work if you know oldbit is a bool.
...and obviously the clear intention is that it should be an unsigned integer so the appropriate thing to do is to fix that.