Skip to contentSkip to conversation

3/5: Updates to SPI and mmc_spi: tx_default, kernel 2.6.19

PATCH In review Hans-Peter Nilsson 2 participants 12 messages Jan 25, 2007 – Jan 28, 2007

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 3/5: Updates to SPI and mmc_spi: tx_default, kernel 2.6.19

(Please CC me on replies, I'm not subscribed to LKML or the SPI list. Thanks.)

The SD/MMC SPI-based protocol isn't really duplex. In the normal case there's either information transmitted or received, not both simultaneously. The unused data is always 0xff; ones. Unfortunately, the SPI framework leaves outgoing data for a left-out tx_buf just as "undefined" with no other way to send constant data than to actually pass a buffer full of ones. I imagine other SPI-based protocols being in the same situation so here's a feature implementation: a controller can say it supports a default value for the bit being shifted out whenever there's no tx_buf. I measured it to improve read performance by 3% for SD/MMC SPI (timing "dd" of the MMC block device) for a DMA-based controller. As with the cs_interactive patch, the functionality is optional for a driver/controller, but I include an update for the spi_bitbang framework, for clients that don't implement their own bitbang->txrx_bufs function. I don't expect it to save much performance for bitbanged transfers (though the lessened memory and cache footprint might be nice); it's mostly to provide an example implementation. (To make sure, I did measure it for spi_crisv32_gpio and found no measurable difference; less than .5% difference with same difference between equal samples).

Tested together with the other patches on the spi_crisv32_sser and spi_crisv32_gpio drivers (not yet in the kernel, will IIUC be submitted as part of the usual arch-maintainer-pushes).

This patch is intended to be applied on top of the cs_inactive patch (the previous one, 2/5).

include/linux/spi/spi.h +15 -1
diff -upr a/include/linux/spi/spi.h b/include/linux/spi/spi.h
@@ -153,6 +153,9 @@ static inline void spi_unregister_driver
  *	SPI slaves, and are numbered from zero to num_chipselects.
  *	each slave has a chipselect signal, but it's common that not
  *	every chipselect is connected to a slave.
+ * @can_tx_default: the controller can specify the "default" bit value:
+ *	if this bit is set and there's no tx data in a message,
+ *	spi_transfer.tx_default then controls the data shifted out.
  * @can_cs_inactive: the controller can send data (or at least toggle the
  *	clock with undefined data output) while having chipselect inactive.
  * @setup: updates the device mode and clocking records used by a
@@ -187,6 +190,11 @@ struct spi_master {
 	 */
 	u16			num_chipselect;
 
+	/* clients can check this to see if they can avoid passing
+	 * transfers where the tx buffer is just zero or is just ones.
+	 */
+	unsigned		can_tx_default:1;
+
 	/* clients that want to toggle the clock while chipselect is
 	 * inactive (has different polarity compared to when data is
 	 * output) must test this bit.
@@ -296,6 +304,9 @@ extern struct spi_master *spi_busnum_to_
  *	important information, and it's ok to pass NULL for both tx_buf
  *	and rx_buf.  It is an error to set this bit if spi_device
  *	.can_cs_inactive == 0.
+ * @tx_default: tx data value (0 or 1) in absence of tx_buf, *iff* the
+ *      master controller supports it (see spi_master.can_tx_default),
+ *      otherwise the tx data is undefined and this bit is unused.
  * @delay_usecs: microseconds to delay after this transfer before
  *	(optionally) changing the chipselect status, then starting
  *	the next transfer or completing this spi_message.
@@ -308,7 +319,9 @@ extern struct spi_master *spi_busnum_to_
  * underlying driver uses dma.
  *
  * If the transmit buffer is null, undefined data will be shifted out
- * while filling rx_buf.  If the receive buffer is null, the data
+ * while filling rx_buf, unless the master controller has indicated
+ * can_tx_default = 1, in which case the data shifted out is specified by
+ * spi_message.tx_default.  If the receive buffer is null, the data
  * shifted in will be discarded.  Only "len" bytes shift out (or in).
  * It's an error to try to shift out a partial word.  (For example, by
  * shifting out three bytes with word size of sixteen or twenty bits;
@@ -351,6 +364,7 @@ struct spi_transfer {
 
 	unsigned	cs_change:1;
 	unsigned	cs_inactive:1;
+	unsigned	tx_default:1;
 	u8		bits_per_word;
 	u16		delay_usecs;
 	u32		speed_hz;
drivers/spi/spi_bitbang.c +18 -0
diff -upr a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c
@@ -77,6 +77,8 @@ static unsigned bitbang_txrx_8(
 
 		if (tx)
 			word = *tx++;
+		else if (t->tx_default)
+			word = 0xff;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -103,6 +105,8 @@ static unsigned bitbang_txrx_16(
 
 		if (tx)
 			word = *tx++;
+		else if (t->tx_default)
+			word = 0xffff;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -129,6 +133,8 @@ static unsigned bitbang_txrx_32(
 
 		if (tx)
 			word = *tx++;
+		else if (t->tx_default)
+			word = 0xffffffffu;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -305,6 +311,7 @@ static void bitbang_work(void *_bitbang)
 		list_for_each_entry (t, &m->transfers, transfer_list) {
 			cs_inactive = t->cs_inactive;
 			BUG_ON(cs_inactive && !spi->master->can_cs_inactive);
+			BUG_ON(t->tx_default && !spi->master->can_tx_default);
 
 			if (bitbang->shutdown) {
 				status = -ESHUTDOWN;
@@ -481,6 +488,17 @@ int spi_bitbang_start(struct spi_bitbang
 	if (!bitbang->txrx_bufs) {
 		bitbang->use_dma = 0;
 		bitbang->txrx_bufs = spi_bitbang_bufs;
+
+		/*
+		 * Because we use spi_bitbang_bufs, spi_bitbang_setup
+		 * must been called (at least from the function
+		 * overriding it) to set cs->txrx_bufs, so the
+		 * function looping over the transfer bytes must be
+		 * one of bitbang_txrx_(8|16|32) where we implement
+		 * tx_default.
+		 */
+		bitbang->master->can_tx_default = 1;
+
 		if (!bitbang->master->setup) {
 			if (!bitbang->setup_transfer)
 				bitbang->setup_transfer =

brgds, H-P

#1 lore raw
Quoted 7 lines · Hans-Peter Nilsson

(Please CC me on replies, I'm not subscribed to LKML or the SPI list. Thanks.)

The SD/MMC SPI-based protocol isn't really duplex. In the normal case there's either information transmitted or received, not both simultaneously. The unused data is always 0xff; ones. Unfortunately, the SPI framework leaves outgoing data for a left-out tx_buf just as "undefined"

In current kernels that's actually changed. The value to be shifted out is now specified ... as all-zeroes, which is what various chips need to receive, and various (half-duplex/Microwire) controllers seem to be doing in any case.

If that's an issue -- and MMC-over-SPI needs to specify some other value -- then please re-issue this patch against 2.6.20, including the update to the bitbang driver ("reference implementation").

I think a better way to package this would be to define a new flag for spi->mode, since controller drivers are already supposed to be checking that to make sure they handle all the options which have been specified. That flag could work in conjunction with a byte provided in the spi_device, that would be used instead of zero. (Some folk have noted that when debugging, it's easier if the pattern there is neither all-ones nor all-zeroes ... something that a digital scope will show is easier to work with.)

- Dave

#2 lore raw

PATCH 1/2 take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 12 lines

From: David Brownell <[email protected]>
Date: Thu, 25 Jan 2007 05:02:56 -0800

Quoted 7 lines · Hans-Peter Nilsson

(Please CC me on replies, I'm not subscribed to LKML or the SPI list. Thanks.)

The SD/MMC SPI-based protocol isn't really duplex. In the normal case there's either information transmitted or received, not both simultaneously. The unused data is always 0xff; ones. Unfortunately, the SPI framework leaves outgoing data for a left-out tx_buf just as "undefined"

Quoted 7 lines

In current kernels that's actually changed. The value to be shifted out is now specified ... as all-zeroes, which is what various chips need to receive, and various (half-duplex/Microwire) controllers seem to be doing in any case.

If that's an issue -- and MMC-over-SPI needs to specify some other value --

Well, it *is* specified as ones (0xff). Curiously enough, I can't point to a statement in the "Simplified Physical Layer Specification 2.0". It's likely specified in the "7.5 SPI Bus Timing Diagrams", which is "blank for the Simplified Specification". If you look at SanDisk's OEM documents for "MultiMediaCard"/ "Reduced-Size MultiMediaCard" v1.3, you can see that *that* document nicely complements the "Simplified Specification", and specifies it as 0xff (see "5.23 SPI Bus Timing Diagrams"). FWIW, I found this referenced from somewhere I-forgot when I STFW'd for SD/MMC SPI documentation or Linux drivers, so I'm not "ratting them out" for providing documentation withheld elsewhere. :-} Besides, I think they're one of those making the rules for the SDcard organization.

...and anyway, I just *had* to try: well, it doesn't work with 0. :-)

Quoted 4 lines

I think a better way to package this would be to define a new flag for spi->mode, since controller drivers are already supposed to be checking that to make sure they handle all the options which have been specified.

But it's unspecified what the controller drivers should do with flags don't recognize (should they refuse? warn? ignore? - I see they all ignore) so with that suggestion, we'll have a situation where drivers wouldn't know about the new flag and would silently not work.

Perhaps it can be "legislated" or at least strongly suggested that SPI drivers must return -EINVAL for flags in spi->mode that they don't recognise? Typically, for 2.6.19 flags, in their master-setup method:

if (spi->mode & ~(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST))

	return -EINVAL;

(I think I'll do exactly that with the drivers I wrote.)

Quoted 2 lines

That flag could work in conjunction with
a byte

Or why not a 32-bit word! If controllers can only handle non-0 values for bits_per_word <= 8, they can check for that. (Before you suggest a 64-bit value, consider that the 64-bit-ness would supposedly be rarely used and the value is likely to be looked up in inner loops like for the bitbanged driver. Ok, there are ways around that, it's just that I don't think a 64-bit value would be more useful than the implications. We can always change it when a use-case appears. I guess that implies having it a u8, but typically the 32 vs 8 bit difference is much less than 64 vs 32. Toolshed alert!)

Quoted 4 lines

provided in the spi_device, that would be used instead of zero. (Some folk have noted that when debugging, it's easier if the pattern there is neither all-ones nor all-zeroes ... something that a digital scope will show is easier to work with.)

FWIW, I defined it as a single bit in that patch, because that's what my HW can do when the transmitter is disabled - and because MOSI *is* a single-valued signal. ;-) Also, I made it per-transfer, which for mmc_spi wouldn't mean much, only perhaps to possibly drop all-zero buffers, which of course implied that someone would have to scan them first anyway (i.e. no fewer memory accesses), so I guess I had no serious use-case.

Still, making it a u32 (or u8), with spi->bits_per_word specifying the actual length, makes sense for example to implementations around a shift-register but with no DMA. A controller can return -EINVAL (shouldn't that be -ENOSYS?) if it finds a default_tx_word it can't support simply enough. If we define returning -EINVAL as always ok and that the driver/client part is responsible for a fallback, I think we have agreement.

While I was looking, I noticed that the memory-layout of non-8-bit words for SPI is a bit unclear. The endianness of data shifted out doesn't really define endianness in memory or whether 24-bit words bytes are in order {hi, med, low, pad} or indeed {pad, low, med, hi or whatever combination. For a LE architecture, storing data as LE in memory makes sense, and both with and without padding makes sense too. Perhaps best to just document that it's undefined?

Quoted 2 lines

then please re-issue this patch against 2.6.20, including the update to the bitbang driver ("reference implementation").

I used patch-2.6.20-rc6; I see not too many SPI things changed besides defining the previously undefined as 0. I didn't find a reason to introduce a mode-flag; it should always be enough to look at the word. A flag is redundant with default_tx_word != 0 (in 2.6.20 semantics). So, I just added that word and adjusted the bitbang-driver. As a courtesy, I also added simple bail-out code for the other drivers (not compiled and submitted separately).

Here's an updated patch for 2.6.20-rc6. Tested against 2.6.19 and adjusted to 2.6.20-rx6; it applies to 2.6.19, except for the last hunk of spi.h which is in a comment and due to that s/undefined data/zeroes/. It's just the first line changed, the rest is the result of M-x fill-paragraph.

BTW, I hope I'm unnecessarily pointing out that if you'd prefer just initializing "word = spi->default_tx_word;" that may (depending on your gcc version) introduce a memory access which we can avoid. Sure, it's just bitbanging SPI, but why not win.

include/linux/spi/spi.h +12 -6
diff -upr a/include/linux/spi/spi.h b/include/linux/spi/spi.h
@@ -43,6 +43,10 @@ extern struct bus_type spi_bus_type;
  *	This may be changed by the device's driver, or left at the
  *	default (0) indicating protocol words are eight bit bytes.
  *	The spi_transfer.bits_per_word can override this for each transfer.
+ * @default_tx_word: This word, of size bits_per_word, will be the value
+ *	shifted out if the transmit buffer is null.  This may be changed
+ *	by the device's driver, but the controller doesn't have to accept
+ *	all values.  The default for a controller is 0, and is always valid.
  * @irq: Negative, or the number passed to request_irq() to receive
  *	interrupts from this device.
  * @controller_state: Controller's runtime state
@@ -72,6 +76,7 @@ struct spi_device {
 #define	SPI_CS_HIGH	0x04			/* chipselect active high? */
 #define	SPI_LSB_FIRST	0x08			/* per-word bits-on-wire */
 	u8			bits_per_word;
+	u32			default_tx_word;
 	int			irq;
 	void			*controller_state;
 	void			*controller_data;
@@ -289,12 +294,13 @@ extern struct spi_master *spi_busnum_to_
  * the data being transferred; that may reduce overhead, when the
  * underlying driver uses dma.
  *
- * If the transmit buffer is null, zeroes will be shifted out
- * while filling rx_buf.  If the receive buffer is null, the data
- * shifted in will be discarded.  Only "len" bytes shift out (or in).
- * It's an error to try to shift out a partial word.  (For example, by
- * shifting out three bytes with word size of sixteen or twenty bits;
- * the former uses two bytes per word, the latter uses four bytes.)
+ * If the transmit buffer is null, the word in spi_device.default_tx_word
+ * will be shifted out while filling rx_buf.  If the receive buffer is
+ * null, the data shifted in will be discarded.  Only "len" bytes shift
+ * out (or in).  It's an error to try to shift out a partial word.  (For
+ * example, by shifting out three bytes with word size of sixteen or
+ * twenty bits; the former uses two bytes per word, the latter uses four
+ * bytes.)
  *
  * All SPI transfers start with the relevant chipselect active.  Normally
  * it stays selected until after the last transfer in a message.  Drivers
drivers/spi/spi_bitbang.c +4 -7
diff -upr a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c
@@ -73,10 +73,9 @@ static unsigned bitbang_txrx_8(
 	u8			*rx = t->rx_buf;
 
 	while (likely(count > 0)) {
-		u8		word = 0;
+		u8		word;
 
-		if (tx)
-			word = *tx++;
+		word = (tx != NULL) ? *tx++ : spi->default_tx_word;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -101,8 +100,7 @@ static unsigned bitbang_txrx_16(
 	while (likely(count > 1)) {
 		u16		word = 0;
 
-		if (tx)
-			word = *tx++;
+		word = (tx != NULL) ? *tx++ : spi->default_tx_word;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -127,8 +125,7 @@ static unsigned bitbang_txrx_32(
 	while (likely(count > 3)) {
 		u32		word = 0;
 
-		if (tx)
-			word = *tx++;
+		word = (tx != NULL) ? *tx++ : spi->default_tx_word;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;

brgds, H-P

#3 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 17 lines · Hans-Peter Nilsson
Quoted 17 lines

From: David Brownell <[email protected]>
Date: Thu, 25 Jan 2007 05:02:56 -0800

Quoted 7 lines · Hans-Peter Nilsson

(Please CC me on replies, I'm not subscribed to LKML or the SPI list. Thanks.)

The SD/MMC SPI-based protocol isn't really duplex. In the normal case there's either information transmitted or received, not both simultaneously. The unused data is always 0xff; ones. Unfortunately, the SPI framework leaves outgoing data for a left-out tx_buf just as "undefined"

In current kernels that's actually changed. The value to be shifted out is now specified ... as all-zeroes, which is what various chips need to receive, and various (half-duplex/Microwire) controllers seem to be doing in any case.

Quoted 4 lines
Quoted 2 lines

If that's an issue -- and MMC-over-SPI needs to specify some other value --

Well, it *is* specified as ones (0xff).

Fair enough; I don't recall that I actually saw a spec saying that.

Quoted 8 lines
Quoted 4 lines

I think a better way to package this would be to define a new flag for spi->mode, since controller drivers are already supposed to be checking that to make sure they handle all the options which have been specified.

But it's unspecified what the controller drivers should do with flags don't recognize (should they refuse? warn? ignore? - I see they all ignore)

They should refuse.

Quoted 4 lines

if (spi->mode & ~(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST))

	return -EINVAL;

(I think I'll do exactly that with the drivers I wrote.)

Something like that, yes. It might be nice to have a KERN_DEBUG message saying which flags were troublesome; production systems will of course never trigger those failures, so there's no point in having a message that's not automagically compiled-out.

Quoted 3 lines
Quoted 1 line

That flag could work in conjunction with a byte

Or why not a 32-bit word!

If a driver wants a repeating 32-bit pattern, they should just provide a properly initialized tx buffer.

Quoted 3 lines

FWIW, I defined it as a single bit in that patch, because that's what my HW can do when the transmitter is disabled - and because MOSI *is* a single-valued signal. ;-)

I wouldn't mind a single bit flag either, saying whether to shift out all ones or all zeroes. The controller driver would morph it to something appropriate ... 0xff, 0xffffffff, etc.

Quoted 2 lines

While I was looking, I noticed that the memory-layout of non-8-bit words for SPI is a bit unclear.

The SPI_LSB_FIRST flag describes bit order on the wire.

In-memory bit ordering should be native/CPU bit order (which may be slightly unclear). So the confusion is not for 8, 16, or 32 bit words ... but 9, 12, 20, and other sample sizes.

Quoted 7 lines

The endianness of
data shifted out doesn't really define endianness in memory or whether 24-bit words bytes are in order {hi, med, low, pad} or indeed {pad, low, med, hi or whatever combination. For a LE architecture, storing data as LE in memory makes sense, and both with and without padding makes sense too. Perhaps best to just document that it's undefined?

No, the point is to alow portable drivers. So that must be specified. What I'm writing up is that it must be right justified, so its the MSBs that are undefined (for RX) or ignored (TX).

Quoted 11 lines
Quoted 2 lines

then please re-issue this patch against 2.6.20, including the update to the bitbang driver ("reference implementation").

I used patch-2.6.20-rc6; I see not too many SPI things changed besides defining the previously undefined as 0. I didn't find a reason to introduce a mode-flag; it should always be enough to look at the word. A flag is redundant with default_tx_word != 0 (in 2.6.20 semantics). So, I just added that word and adjusted the bitbang-driver. As a courtesy, I also added simple bail-out code for the other drivers (not compiled and submitted separately).

OK.

Quoted 1 line

Here's an updated patch for 2.6.20-rc6.

Thanks, I'll eyeball it and merge some version.

- Dave

#4 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 9 lines

From: David Brownell <[email protected]>
Date: Fri, 26 Jan 2007 15:21:15 -0800

Quoted 3 lines
Quoted 1 line

That flag could work in conjunction with a byte

Or why not a 32-bit word!

If a driver wants a repeating 32-bit pattern, they should just provide a properly initialized tx buffer.

I'm suggesting the default_tx_word be able to fill out "common" values of bits_per_word when > 8, else it'd be useless as a 1-filler for larger sizes than byte. Making it a single bit then wouldn't cater to your debug-with-scope use-case.

brgds, H-P

#5 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 7 lines · Hans-Peter Nilsson
Quoted 7 lines

From: David Brownell <[email protected]>
Date: Fri, 26 Jan 2007 15:21:15 -0800

Quoted 3 lines
Quoted 1 line

That flag could work in conjunction with a byte

Or why not a 32-bit word!

Quoted 7 lines
Quoted 2 lines

If a driver wants a repeating 32-bit pattern, they should just provide a properly initialized tx buffer.

I'm suggesting the default_tx_word be able to fill out "common" values of bits_per_word when > 8, else it'd be useless as a 1-filler for larger sizes than byte. Making it a single bit then wouldn't cater to your debug-with-scope use-case.

That wasn't my idea, it was borrowed from someone else's debug arsenal. ;)

In any case, I think that sort of debug assist doesn't need to stay in production system. See the patch I posted previously...

- Dave

#6 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

In reply to David Brownell
Quoted 7 lines · David Brownell
Quoted 3 lines

FWIW, I defined it as a single bit in that patch, because that's what my HW can do when the transmitter is disabled - and because MOSI *is* a single-valued signal. ;-)

I wouldn't mind a single bit flag either, saying whether to shift out all ones or all zeroes. The controller driver would morph it to something appropriate ... 0xff, 0xffffffff, etc.

In fact, how about this one instead? It uses a bit in spi->mode since that's pretty easy to test. And while it doesn't get rid of the multiple conditionals when the tx buffer is null, it does let the other values be constants (0, ~0) that compilers like.

(I can update your patch #4, etc, to match.)

- Dave

include/linux/spi/spi.h +4 -2
Index: g26/include/linux/spi/spi.h
===================================================================
@@ -71,6 +71,7 @@ struct spi_device {
 #define	SPI_MODE_3	(SPI_CPOL|SPI_CPHA)
 #define	SPI_CS_HIGH	0x04			/* chipselect active high? */
 #define	SPI_LSB_FIRST	0x08			/* per-word bits-on-wire */
+#define	SPI_TX_1	0x10			/* shift out ones on rx-only */
 	u8			bits_per_word;
 	int			irq;
 	void			*controller_state;
@@ -296,8 +297,9 @@ extern struct spi_master *spi_busnum_to_
  * the data being transferred; that may reduce overhead, when the
  * underlying driver uses dma.
  *
- * If the transmit buffer is null, zeroes will be shifted out
- * while filling rx_buf.  If the receive buffer is null, the data
+ * If the transmit buffer is null, zeroes will be shifted out while
+ * filling rx_buf, unless SPI_TX_1 is set in spi->mode (in which case
+ * ones will be shifted out).  If the receive buffer is null, the data
  * shifted in will be discarded.  Only "len" bytes shift out (or in).
  * It's an error to try to shift out a partial word.  (For example, by
  * shifting out three bytes with word size of sixteen or twenty bits;
drivers/spi/spi_bitbang.c +21 -4
Index: g26/drivers/spi/spi_bitbang.c
===================================================================
@@ -73,10 +73,14 @@ static unsigned bitbang_txrx_8(
 	u8			*rx = t->rx_buf;
 
 	while (likely(count > 0)) {
-		u8		word = 0;
+		u8		word;
 
 		if (tx)
 			word = *tx++;
+		else if (spi->mode & SPI_TX_1)
+			word = ~0;
+		else
+			word = 0;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -99,10 +103,14 @@ static unsigned bitbang_txrx_16(
 	u16			*rx = t->rx_buf;
 
 	while (likely(count > 1)) {
-		u16		word = 0;
+		u16		word;
 
 		if (tx)
 			word = *tx++;
+		else if (spi->mode & SPI_TX_1)
+			word = ~0;
+		else
+			word = 0;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -125,10 +133,14 @@ static unsigned bitbang_txrx_32(
 	u32			*rx = t->rx_buf;
 
 	while (likely(count > 3)) {
-		u32		word = 0;
+		u32		word;
 
 		if (tx)
 			word = *tx++;
+		else if (spi->mode & SPI_TX_1)
+			word = ~0;
+		else
+			word = 0;
 		word = txrx_word(spi, ns, word, bits);
 		if (rx)
 			*rx++ = word;
@@ -176,6 +188,8 @@ int spi_bitbang_setup_transfer(struct sp
 }
 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
 
+#define MODEBITS	(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_TX_1)
+
 /**
  * spi_bitbang_setup - default setup for per-word I/O loops
  */
@@ -192,8 +206,11 @@ int spi_bitbang_setup(struct spi_device 
 	 * just bitbang_txrx_le_cphaX() routines shifting the other way, and
 	 * some hardware controllers also have this support.
 	 */
-	if ((spi->mode & SPI_LSB_FIRST) != 0)
+	if (spi->mode & ~MODEBITS) {
+		dev_dbg(&spi->dev, "unsupported SPI mode bits %04x\n",
+				spi->mode & ~MODEBITS);
 		return -EINVAL;
+	}
 
 	if (!cs) {
 		cs = kzalloc(sizeof *cs, GFP_KERNEL);
#7 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 7 lines

From: David Brownell <[email protected]>
Date: Fri, 26 Jan 2007 20:21:54 -0800

In fact, how about this one instead? It uses a bit in spi->mode since that's pretty easy to test. And while it doesn't get rid of the multiple conditionals when the tx buffer is null, it does let the other values be constants (0, ~0) that compilers like.

I'm fine, except the typo.

Quoted 1 line

(I can update your patch #4, etc, to match.)

Or I could, and test it to boot. :)

Quoted 1 line

+#define MODEBITS (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_TX_1)

Add SPI_LSB_FIRST (still supported by spi_bitbang, I presume :)

brgds, H-P

#8 lore raw

Re: [PATCH 1/2] take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

Quoted 4 lines

Date: Sat, 27 Jan 2007 21:19:29 +0100
From: Hans-Peter Nilsson <[email protected]>

Add SPI_LSB_FIRST (still supported by spi_bitbang, I presume :)

Never mind, I just noticed that I can't read.

brgds, H-P

#9 lore raw

Re: [PATCH] take 3: mmc_spi with SPI_TX_1 2.6.20-rc6

In reply to Hans-Peter Nilsson
Quoted 6 lines

Date: Sat, 27 Jan 2007 21:19:29 +0100
From: Hans-Peter Nilsson <[email protected]>

Quoted 3 lines

From: David Brownell <[email protected]>
Date: Fri, 26 Jan 2007 20:21:54 -0800
(I can update your patch #4, etc, to match.)

Or I could, and test it to boot. :)

Lightly tested: mounted, deleted a file, verified contents elsewhere (that the directory entry was gone).

(Previous authors: Mike Lavender, David Brownell, missing explicit signoff)

drivers/mmc/Kconfig +11 -0
diff -uprN a/drivers/mmc/Kconfig b/drivers/mmc/Kconfig
@@ -125,4 +125,15 @@ config MMC_TIFM_SD
           To compile this driver as a module, choose M here: the
 	  module will be called tifm_sd.
 
+config MMC_SPI
+	tristate "MMC/SD over SPI"
+	depends on MMC && SPI && EXPERIMENTAL
+	help
+	  Some systems accss MMC/SD cards using the SPI protocol instead of
+	  using an MMC/SD controller.  The disadvantage of using SPI is that
+	  it's often not as fast; its compensating advantage is that SPI is
+	  available on many systems without MMC/SD controllers.
+
+	  If unsure, or if your system has no SPI controller driver, say N.
+
 endmenu
drivers/mmc/Makefile +1 -0
diff -uprN a/drivers/mmc/Makefile b/drivers/mmc/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_MMC_AU1X)		+= au1xmmc.o
 obj-$(CONFIG_MMC_OMAP)		+= omap.o
 obj-$(CONFIG_MMC_AT91RM9200)	+= at91_mci.o
 obj-$(CONFIG_MMC_TIFM_SD)	+= tifm_sd.o
+obj-$(CONFIG_MMC_SPI)		+= mmc_spi.o
 
 mmc_core-y := mmc.o mmc_sysfs.o sdio.o
 mmc_core-$(CONFIG_BLOCK) += mmc_queue.o
drivers/mmc/mmc_spi.c +1499 -0

1,499 lines not on this page · raw message

include/linux/spi/mmc_spi.h +34 -0
diff -uprN a/include/linux/spi/mmc_spi.h b/include/linux/spi/mmc_spi.h
@@ -0,0 +1,34 @@
+#ifndef __LINUX_SPI_MMC_SPI_H
+#define __LINUX_SPI_MMC_SPI_H
+
+#include <linux/mmc/protocol.h>
+#include <linux/interrupt.h>
+
+struct device;
+struct mmc_host;
+
+/* something to put in platform_data of a device being used
+ * to manage an MMC/SD card slot
+ *
+ * REVISIT this isn't spi-specific, any card slot should be
+ * able to handle it
+ */
+struct mmc_spi_platform_data {
+	/* driver activation and (optional) card detect irq */
+	int (*init)(struct device *,
+		irqreturn_t (*)(int, void *),
+		void *);
+	void (*exit)(struct device *, void *);
+
+	/* how long to debounce card detect, in jiffies */
+	unsigned long detect_delay;
+
+	/* sense switch on sd cards */
+	int (*get_ro)(struct device *);
+
+	/* power management */
+	unsigned int ocr_mask;			/* available voltages */
+	void (*setpower)(struct device *, unsigned int);
+};
+
+#endif /* __LINUX_SPI_MMC_SPI_H */

brgds, H-P

#10 lore raw

Re: [PATCH] take 4: mmc_spi with SPI_TX_1 2.6.20-rc6

Quoted 5 lines

Date: Sat, 27 Jan 2007 21:59:49 +0100
From: Hans-Peter Nilsson <[email protected]>

Lightly tested: mounted, deleted a file, verified contents elsewhere (that the directory entry was gone).

...though not without SPI_TX_1 support, because then I'd have noticed a few typos. Sorry about that.

Review

Quoted 10 lines

--- a/drivers/mmc/mmc_spi.c 1970-01-01 01:00:00.000000000 +0100

(quoted diff) +7 -0
+mmc_spi_setup_message(
 
+		/*
+		 * Maybe because we specified SPI_TX_1, and the controller
+		 * can't handle it.  We'll fall back on using a buffer
+		 * with ones.
+		 */
+		spi->mode |= (SPI_CPOL|SPI_CPHA);

Should have been assignment, "=". Bah.

Review

Quoted 3 lines
(quoted diff) +3 -0
+	mmc = mmc_alloc_host(sizeof *host
+			     + ((host->spi->mode & SPI_TX_1) == 0
+				? ONES_BUFFER_SIZE : 0), &spi->dev);

Not healthy before "host" is assigned. I missed a big fat warning from gcc.

This one is tested a little more; it's just the two bugs above fixed.

(Previous authors: Mike Lavender, David Brownell, no explicit signoff)

drivers/mmc/Kconfig +11 -0
diff -uprN a/drivers/mmc/Kconfig b/drivers/mmc/Kconfig
@@ -125,4 +125,15 @@ config MMC_TIFM_SD
           To compile this driver as a module, choose M here: the
 	  module will be called tifm_sd.
 
+config MMC_SPI
+	tristate "MMC/SD over SPI"
+	depends on MMC && SPI && EXPERIMENTAL
+	help
+	  Some systems accss MMC/SD cards using the SPI protocol instead of
+	  using an MMC/SD controller.  The disadvantage of using SPI is that
+	  it's often not as fast; its compensating advantage is that SPI is
+	  available on many systems without MMC/SD controllers.
+
+	  If unsure, or if your system has no SPI controller driver, say N.
+
 endmenu
drivers/mmc/Makefile +1 -0
diff -uprN a/drivers/mmc/Makefile b/drivers/mmc/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_MMC_AU1X)		+= au1xmmc.o
 obj-$(CONFIG_MMC_OMAP)		+= omap.o
 obj-$(CONFIG_MMC_AT91RM9200)	+= at91_mci.o
 obj-$(CONFIG_MMC_TIFM_SD)	+= tifm_sd.o
+obj-$(CONFIG_MMC_SPI)		+= mmc_spi.o
 
 mmc_core-y := mmc.o mmc_sysfs.o sdio.o
 mmc_core-$(CONFIG_BLOCK) += mmc_queue.o
drivers/mmc/mmc_spi.c +1499 -0

1,499 lines not on this page · raw message

include/linux/spi/mmc_spi.h +34 -0
@@ -0,0 +1,34 @@
+#ifndef __LINUX_SPI_MMC_SPI_H
+#define __LINUX_SPI_MMC_SPI_H
+
+#include <linux/mmc/protocol.h>
+#include <linux/interrupt.h>
+
+struct device;
+struct mmc_host;
+
+/* something to put in platform_data of a device being used
+ * to manage an MMC/SD card slot
+ *
+ * REVISIT this isn't spi-specific, any card slot should be
+ * able to handle it
+ */
+struct mmc_spi_platform_data {
+	/* driver activation and (optional) card detect irq */
+	int (*init)(struct device *,
+		irqreturn_t (*)(int, void *),
+		void *);
+	void (*exit)(struct device *, void *);
+
+	/* how long to debounce card detect, in jiffies */
+	unsigned long detect_delay;
+
+	/* sense switch on sd cards */
+	int (*get_ro)(struct device *);
+
+	/* power management */
+	unsigned int ocr_mask;			/* available voltages */
+	void (*setpower)(struct device *, unsigned int);
+};
+
+#endif /* __LINUX_SPI_MMC_SPI_H */

brgds, H-P

brgds, H-P

#11 lore raw

PATCH 2/2 take 2: (was-kind-of: 3/5 SPI tx_default) 2.6.20-rc6

In reply to David Brownell

Just some trivial patches to make sure the existing drivers in the official tree don't *silently* fail with the introduction of default_tx_word. Never tested in any way. Not sure if this is all, for example I didn't look in arch/*.

drivers/spi/pxa2xx_spi.c +3 -0
diff -upr a/drivers/spi/pxa2xx_spi.c b/drivers/spi/pxa2xx_spi.c
@@ -1077,6 +1077,9 @@ static int setup(struct spi_device *spi)
 	if (!spi->bits_per_word)
 		spi->bits_per_word = 8;
 
+	if (spi->default_tx_word != 0)
+		return -EINVAL;
+
 	if (drv_data->ssp_type != PXA25x_SSP
 		&& (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
 		dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
drivers/spi/spi_mpc83xx.c +3 -0
diff -upr a/drivers/spi/spi_mpc83xx.c b/drivers/spi/spi_mpc83xx.c
@@ -238,6 +238,9 @@ static int mpc83xx_spi_setup(struct spi_
 	struct mpc83xx_spi *mpc83xx_spi;
 	int retval;
 
+	if (spi->default_tx_word != 0)
+		return -EINVAL;
+
 	if (!spi->max_speed_hz)
 		return -EINVAL;
 
drivers/spi/spi_s3c24xx.c +3 -0
diff -upr a/drivers/spi/spi_s3c24xx.c b/drivers/spi/spi_s3c24xx.c
@@ -153,6 +153,9 @@ static int s3c24xx_spi_setup(struct spi_
 {
 	int ret;
 
+	if (spi->default_tx_word != 0)
+		return -EINVAL;
+
 	if (!spi->bits_per_word)
 		spi->bits_per_word = 8;
 

brgds, H-P