spi: fix m25p80 when the cs_change hint is honored
PATCH 1/1 spi: fix m25p80 when the cs_change hint is honored
The m25p80 driver uses spi_write_then_read() and spi_write() (among others) for interaction with the SPI controller. The only way to signal the end of SPI transaction to the m25p80 chip according to its datasheet, is to deactivate the chip select. Currently, an SPI master driver that honors the cs_change hint may not deactivate the chip select after the transaction when cs_change == 0, leading to data corruption. Other SPI devices may also be affected by this behaviour.
Fix this issue, and while at it, also make spi_read() do the same for consistency.
--- drivers/spi/spi.c | 1 + include/linux/spi/spi.h | 2 ++ 2 files changed, 3 insertions(+), 0 deletions(-)
drivers/spi/spi.c +1 -0
@@ -690,6 +690,7 @@ int spi_write_then_read(struct spi_device *spi,memcpy (local_buf, txbuf, n_tx); x[0 ].tx_buf = local_buf; x[1 ].rx_buf = local_buf + n_tx; + x[1 ].cs_change =1 ;/* do the i/o */ status =spi_sync (spi, &message);
include/linux/spi/spi.h +2 -0
@@ -609,6 +609,7 @@ spi_write(struct spi_device *spi, const u8 *buf, size_t len)struct spi_transfer t = { .tx_buf = buf, .len = len, + .cs_change =1 , };struct spi_message m;
@@ -633,6 +634,7 @@ spi_read(struct spi_device *spi, u8 *buf, size_t len)struct spi_transfer t = { .rx_buf = buf, .len = len, + .cs_change =1 , };struct spi_message m;
Signature · 9 lines
1.6.2.4 ------------------------------------------------------------------------------ Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensing option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment. http://p.sf.net/sfu/businessobjects
A message that is not in the archive (sent off-list or not yet received): 1242731340-12160-1-git-send-email-baruch-NswTu9S1W3P6gbPvEgmw2w@public.gmane.org
Quoted 6 lines · Baruch Siach
The m25p80 driver uses
spi_write_then_read()andspi_write()(among others) for interaction with the SPI controller. The only way to signal the end of SPI transaction to the m25p80 chip according to its datasheet, is to deactivate the chip select. Currently, an SPI master driver that honors thecs_changehint may not deactivate the chip select after the transaction whencs_change== 0, leading to data corruption.
NAK. You mis-read the spec. What controller driver were you using? That seems to be the source of the bug you're observing. It's not terminating a spi_message correctly.
Normal behavior of a spi_message is to keep the chipselect active during the entire message, then deactivate it after the last transfer. Setting the cs_change flag *changes* that behavior ... allowing either
(a) brief mid-message deselection, usually to make a message hold a composite transaction, or else (b) hinting that immediate deselection isn't neccessary, as a possible performance tweak for some drivers.
Note that while (a) is mandatory -- drivers that can't do it must reject the messages using it -- (b) is optional. When the next message goes to a different device, obviously the chip selection lines will need to change.
See below for more details:
Quoted 17 lines
Other SPI devices may also be affected by
this behaviour.Fix this issue, and while at it, also make
spi_read()do the same for consistency.Signed-off-by Baruch Siach <[email protected]>--- drivers/spi/spi.c | 1 + include/linux/spi/spi.h | 2 ++ 2 files changed, 3 insertions(+), 0 deletions(-)drivers/spi/spi.c +0
-0
drivers/spi/spi.c int spi_write_then_read(struct spi_device *spi, · line 693
Quoted 4 lines
drivers/spi/spi.c +1
-0@@ -690,6 +690,7 @@ int spi_write_then_read(struct spi_device *spi,memcpy (local_buf, txbuf, n_tx); x[0 ].tx_buf = local_buf; x[1 ].rx_buf = local_buf + n_tx; + x[1 ].cs_change =1 ;
One does *NOT* want to keep the chip selected after the read.
include/linux/spi/spi.h spi_write(struct spi_device *spi, const u8 *buf, size_t len) · line 612
Quoted 12 lines
drivers/spi/spi.c +0
-0/* do the i/o */ status =spi_sync (spi, &message);include/linux/spi/spi.h +1
-0@@ -609,6 +609,7 @@ spi_write(struct spi_device *spi, const u8 *buf, size_t len)struct spi_transfer t = { .tx_buf = buf, .len = len, + .cs_change =1 ,
... or after the write ...
include/linux/spi/spi.h spi_read(struct spi_device *spi, u8 *buf, size_t len) · line 637
Quoted 8 lines
include/linux/spi/spi.h +1
-0};struct spi_message m;@@ -633,6 +634,7 @@ spi_read(struct spi_device *spi, u8 *buf, size_t len)struct spi_transfer t = { .rx_buf = buf, .len = len, + .cs_change =1 ,
... or after this read.
Those are all *completely normal* transactions.
- Dave
Quoted 7 lines
include/linux/spi/spi.h +0
-0};struct spi_message m;Signature · 1 line
1.6.2.4
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensing option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
A message that is not in the archive (sent off-list or not yet received): 200905192134.14316.david-b-yBeKhBN/[email protected]
Hi David,
Quoted 11 lines · David Brownell
Normal behavior of a
spi_messageis to keep the chipselect active during the entire message, then deactivate it after the last transfer. Setting thecs_changeflag *changes* that behavior ... allowing either
(a) brief mid-message deselection, usually to make a message hold a composite transaction, or else (b) hinting that immediate deselection isn't neccessary, as a possible performance tweak for some drivers.Note that while (a) is mandatory -- drivers that can't do it must reject the messages using it -- (b) is optional. When the next message goes to a different device, obviously the chip selection lines will need to change.
Thanks for the clarification.
If I understood correctly then, cs_change has two opposite meanings:
1. If cs_change != 0 in spi_transfer that is not the last in the current spi_message, then deactivate the chip select between this spi_transfer and the next.
2. If cs_change != 0 in spi_transfer that is that last in the current spi_message, then the SPI controller is allowed to NOT deactivate the chip select after the current spi_message.
Is this correct? If so would a clarification of the relevant text at include/linux/spi/spi.h be accepted?
baruch
Signature · 10 lines
~. .~ Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
- [email protected] - tel: +972.2.679.5364, http://www.tkos.co.il -
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjectsQuoted 3 lines · Baruch Siach
Thanks for the clarification.
If I understood correctly then,
cs_changehas two opposite meanings:
No, it has exactly one meaning -- *change* normal behavior -- but that gets applied in two different contexts, where "normal" is two things:
Quoted 9 lines
1. If
cs_change!= 0 inspi_transferthat is not the last in the currentspi_message, then deactivate the chip select between thisspi_transferand the next.2. If
cs_change!= 0 inspi_transferthat is that last in the currentspi_message, then the SPI controller is allowed to NOT deactivate the chip select after the currentspi_message.Is this correct?
Modulo nuances on #2, yes. See the kerneldoc for more info.
Quoted 2 lines
If so would a clarification of the relevant text at
include/linux/spi/spi.hbe accepted?
In what way is the existing language unclear? There are two paragraphs in the "struct spi_transfer" kerneldoc.
Quoted 2 lines
baruch
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensing option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects
A message that is not in the archive (sent off-list or not yet received): 200905192209.19125.david-b-yBeKhBN/[email protected]
Hi David,
Quoted 6 lines · David Brownell
Quoted 2 lines · Baruch Siach
If so would a clarification of the relevant text at
include/linux/spi/spi.hbe accepted?In what way is the existing language unclear? There are two paragraphs in the "struct
spi_transfer" kerneldoc.
I (wrongly) understood the name cs_change as saying "chip select change", that is, "deactivate the chip select". Then I read the following text from include/linux/spi/spi.h:
"(ii) When the transfer is the last one in the message, the chip may stay selected until the next transfer."
I (wrongly again) understood that this description applies to that case of cs_change == 0. I'd rephrase the above sentence as follows:
"(ii) When the transfer is the last one in the message, and cs_change != 0, the chip may stay selected until the next transfer."
Does this make sense?
baruch
Signature · 10 lines
~. .~ Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
- [email protected] - tel: +972.2.679.5364, http://www.tkos.co.il -
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
http://p.sf.net/sfu/businessobjects