spi: bitbang: only toggle bitchanges
PATCH 1/1 spi: bitbang: only toggle bitchanges
The current implementation of bitbang_txrx_be_cpha0 and bitbang_txrx_be_cpha1 always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.
This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
--- drivers/spi/spi-bitbang-txrx.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/spi/spi-bitbang-txrx.h +10 -2
@@ -49,12 +49,16 @@ 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 ) -setmosi (spi, word & (1 <<31 )); +if ((word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + }spidelay (nsecs);/* T(setup) */ setsck (spi, !cpol);
@@ -76,13 +80,17 @@ 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@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 ) -setmosi (spi, word & (1 <<31 )); +if ((word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + }spidelay (nsecs);/* T(setup) */ setsck (spi, cpol);
Signature · 1 line
2.1.4
Mark Brown
maintainerPATCH 1/1 spi: bitbang: only toggle bitchanges
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 62
Quoted 7 lines · Michael Grzeschik
drivers/spi/spi-bitbang-txrx.h +4
-1if ((flags &SPI_MASTER_NO_TX ) ==0 ) -setmosi (spi, word & (1 <<31 )); +if ((word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + }spidelay (nsecs);/* T(setup) */
This change is fine but for legibility can you please add braces around
the outer if as well?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150331/d1710f9b/attachment.sig>
PATCH v2 1/1 spi: bitbang: only toggle bitchanges
The current implementation of bitbang_txrx_be_cpha0 and bitbang_txrx_be_cpha1 always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.
This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
---
v2: added missing braces
drivers/spi/spi-bitbang-txrx.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/spi/spi-bitbang-txrx.h +14 -4
@@ -49,12 +49,17 @@ 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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if ((word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, !cpol);
@@ -76,13 +81,18 @@ 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@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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if ((word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, cpol);
Signature · 1 line
2.1.4
Mark Brown
maintainerPATCH v2 1/1 spi: bitbang: only toggle bitchanges
Quoted 4 lines · Michael Grzeschik
The current implementation of
bitbang_txrx_be_cpha0andbitbang_txrx_be_cpha1always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.
Applied, thanks.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150331/4c018f9a/attachment.sig>
PATCH v2 1/1 spi: bitbang: only toggle bitchanges
In reply to Michael GrzeschikQuoted 21 lines · Michael Grzeschik
The current implementation of
bitbang_txrx_be_cpha0andbitbang_txrx_be_cpha1always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
Signed-off-by Michael Grzeschik <[email protected]>---
v2: added missing bracesdrivers/spi/spi-bitbang-txrx.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)drivers/spi/spi-bitbang-txrx.h +0
-0
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 52
Quoted 4 lines
drivers/spi/spi-bitbang-txrx.h +1
-0@@ -49,12 +49,17 @@ 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 );
Is it intentional you check the first bit (word & 1) here? Everywhere else you use (word & 31).
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 58
Quoted 8 lines
drivers/spi/spi-bitbang-txrx.h +2
-2/* 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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if ((word & (1 <<31 )) != oldbit) {
You are comparing a bool to an int, and ((word & (1 << 31)) will always be != true (== 1). Your condition needs to be !!(word & (1 << 31)) != oldbit .
Quoted 11 lines
drivers/spi/spi-bitbang-txrx.h +4
-0+setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, !cpol);@@ -76,13 +81,18 @@ bitbang_txrx_be_cpha1(struct spi_device *spi, {/* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
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 +3
-2+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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if ((word & (1 <<31 )) != oldbit) {
Same issue here.
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha1(struct spi_device *spi,
Quoted 8 lines
drivers/spi/spi-bitbang-txrx.h +4
-1+setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, cpol); --
Regards
Jonas
PATCH v2 1/1 spi: bitbang: only toggle bitchanges
Quoted 26 lines · Jonas Gorski
Quoted 24 lines · Michael Grzeschik
The current implementation of
bitbang_txrx_be_cpha0andbitbang_txrx_be_cpha1always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
Signed-off-by Michael Grzeschik <[email protected]>---
v2: added missing bracesdrivers/spi/spi-bitbang-txrx.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)drivers/spi/spi-bitbang-txrx.h +0
-0@@ -49,12 +49,17 @@ bitbang_txrx_be_cpha0(struct spi_device *spi, {/* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
Quoted 4 lines
drivers/spi/spi-bitbang-txrx.h bitbang_txrx_be_cpha0(struct spi_device *spi, · line 52
Quoted 1 line
drivers/spi/spi-bitbang-txrx.h +1
-0+bool oldbit = !(word &1 );Is it intentional you check the first bit (word & 1) here? Everywhere else you use (word & 31).
Doh! I was reworking this from an _le_ variant of the same code. This hunk unfortunately is an leftover... :/
Mark, should I send a corrected version or do you squash the 31 into the applied patch?
Thanks for the hint,
Michael
Signature · 4 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 |
Mark Brown
maintainerPATCH v2 1/1 spi: bitbang: only toggle bitchanges
Quoted 2 lines · Michael Grzeschik
Mark, should I send a corrected version or do you squash the 31 into the applied patch?
If you could send me a patch (either incremental or squashed is fine)
that'd be good - I'd prefer to have something from you as I'd need to
set up a test system for bitbang.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150331/49311aa4/attachment.sig>
PATCH v3 1/1 spi: bitbang: only toggle bitchanges
The current implementation of bitbang_txrx_be_cpha0 and bitbang_txrx_be_cpha1 always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.
This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
---
v2: - added missing braces
v3: - fixed initial value of oldbit
- added !! to condition for bool comparison
drivers/spi/spi-bitbang-txrx.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/spi/spi-bitbang-txrx.h +14 -4
@@ -49,12 +49,17 @@ 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 <<31 ));/* 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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if (!!(word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, !cpol);
@@ -76,13 +81,18 @@ 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@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 ) -setmosi (spi, word & (1 <<31 )); +if ((flags &SPI_MASTER_NO_TX ) ==0 ) { +if (!!(word & (1 <<31 )) != oldbit) { +setmosi (spi, word & (1 <<31 )); + oldbit = word & (1 <<31 ); + } + }spidelay (nsecs);/* T(setup) */ setsck (spi, cpol);
Signature · 1 line
2.1.4
Mark Brown
maintainerPATCH v3 1/1 spi: bitbang: only toggle bitchanges
Quoted 8 lines · Michael Grzeschik
The current implementation of
bitbang_txrx_be_cpha0andbitbang_txrx_be_cpha1always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIO actually has not to be changed.This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
Do you have any benchmarking or anything showing that this offers a
practical benefit? Generally a bitbanging system would be using memory
mapped GPIOs for the bus so we're talking about replacing a memory
access with some shifts and logic operations here and while I can
believe that something that runs in cache is going to be an overall win
over going out to a device it's not quite so clear cut as it could be.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150418/1e85d2d5/attachment.sig>
PATCH v3 1/1 spi: bitbang: only toggle bitchanges
Quoted 11 lines · Mark Brown
Quoted 3 lines · Michael Grzeschik
The current implementation of
bitbang_txrx_be_cpha0andbitbang_txrx_be_cpha1always call setmosi. That runs into several unnecessary calls into the gpiolib when the level of the GPIOactually
Quoted 5 lines
has not to be changed.
This patch changes the routines to remember the last GPIO level and only calls setmosi if an change has to be made. This way it improves the transfer throughput.
Quoted 6 lines
Do you have any benchmarking or anything showing that this offers a practical benefit? Generally a bitbanging system would be using memory mapped GPIOs for the bus so we're talking about replacing a memory access with some shifts and logic operations here and while I can believe that something that runs in cache is going to be an overall win over going out to a device it's not quite so clear cut as it could be.
IIRC we had a reduction of about 30% when programming an FPGA via SPI. But Michael can give you the exact numbers.
Marc
Mark Brown
maintainerPATCH v3 1/1 spi: bitbang: only toggle bitchanges
Quoted 2 lines · Marc Kleine-Budde
IIRC we had a reduction of about 30% when programming an FPGA via SPI. But Michael can give you the exact numbers.
OK, that's definitely worth writing home about - putting that in the
commit message would have helped a lot! I've applied the patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150418/dc5213ca/attachment.sig>