Skip to contentSkip to conversation

2/5: Updates to SPI and mmc_spi: clock with cs inactive, kernel 2.6.19

PATCH New Hans-Peter Nilsson 2 participants 6 messages Jan 25, 2007 – Jan 27, 2007

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

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

There was a comment in the mmc_spi.c glue driver at <URL:http://www.gossamer-threads.com/lists/linux/kernel/671939#671939>:

+                * some cards seemed happier if they were initialized first
+                * by the native MMC stack, not SPI ... and in other cases
+                * rmmod/modprobe of mmc_spi helped the card work better,
+                * even without power cycling
+                *
+                * FIXME find out what that important state is, which is
+                * not reset here... and makes robustness problems

I think I've spotted the problem, or at least a problem with a solution that fits the description. What's missing is "at least 74 SD clocks to the SD card with keeping CMD line to high. In case of SPI mode, CS shall be held to high during 74 clock cycles" (from Section 6.4.1, in "Simplified Physical Layer Specification 2.0"). This is to happen before sending CMD0. I have only one card (of 7) where this fails, a Viking Interworks 256 MB card. It matches the description in the comment; it never initializes, gives invalid replies (with bit 7 set) without this, but works fine with it.

The gotcha is that the SPI framework didn't have a way to express transfers with chip-select inactive. Sure, you can set chip-select to inactive for a period of *time*, but never while also toggling the clock. So here's an implementation for that.

I made this functionality optional, with the updated mmc_spi driver trying anyway if the spi host doesn't provide the function. So, no SPI drivers require updating, unless they really want to work with mmc_spi flawlessly with all cards. I initially thought the function important enough to warrant mandatory implementation for all SPI drivers, but it hasn't been needed before, so I reconsidered. Also, some SPI drivers seem to implement the chip-select function as optional, a clear hint to this being optional IMHO. I looked at the existing drivers and went as far as writing proof-of-concept patches for them, but as I can't test them, I'll just stop there and call that a feasibility study. The spi_bitbang looked like it could have always can_cs_inactive = 1, but then I noticed what spi_mpc83xx.c does in its mpc83xx_spi_chipselect() (register writes only on BITBANG_CS_ACTIVE supposedly controlling the clock) and so I think setting the capability is better be left to the respective caller (typically just before its call to spi_bitbang_start), and just adjust the various chipselect calls i spi_bitbang.

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

include/linux/spi/spi.h +23 -3
diff -upr a/include/linux/spi/spi.h b/include/linux/spi/spi.h
@@ -153,6 +153,8 @@ 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_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
  *	device's SPI controller; protocol code may call this.
  * @transfer: adds a message to the controller's transfer queue.
@@ -185,6 +187,12 @@ struct spi_master {
 	 */
 	u16			num_chipselect;
 
+	/* clients that want to toggle the clock while chipselect is
+	 * inactive (has different polarity compared to when data is
+	 * output) must test this bit.
+	 */
+	unsigned		can_cs_inactive:1;
+
 	/* setup mode and clock, etc (spi driver may call many times) */
 	int			(*setup)(struct spi_device *spi);
 
@@ -202,7 +210,8 @@ struct spi_master {
 	 *   arbitration algorithm is unspecified (round robin, fifo,
 	 *   priority, reservations, preemption, etc)
 	 *
-	 * + Chipselect stays active during the entire message
+	 * + Chipselect stays active (or inactive when using
+	 *   spi_transfer.cs_inactive) during the entire message
 	 *   (unless modified by spi_transfer.cs_change != 0).
 	 * + The message transfers use clock and SPI mode parameters
 	 *   previously established by setup() for this device
@@ -278,6 +287,15 @@ extern struct spi_master *spi_busnum_to_
  * @bits_per_word: select a bits_per_word other then the device default
  *      for this transfer. If 0 the default (from spi_device) is used.
  * @cs_change: affects chipselect after this transfer completes
+ * @cs_inactive: if the host supports this function (see spi_device
+ *	.can_cs_inactive) and if this is 1, chipselect will be inactive
+ *	during this transfer.  The value of cs_change is correspondingly
+ *	affected; a cs_change = 1 in a transfer with cs_inactive = 1 means
+ *	"changing back to active".  Input and output data are undefined
+ *	with regards to signal levels on the hardware; the length is the
+ *	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.
  * @delay_usecs: microseconds to delay after this transfer before
  *	(optionally) changing the chipselect status, then starting
  *	the next transfer or completing this spi_message.
@@ -296,8 +314,9 @@ extern struct spi_master *spi_busnum_to_
  * 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
+ * All SPI transfers start with the relevant chipselect active (except of
+ * course with cs_inactive = 1 in the transfer, it's inactive).  Normally
+ * it stays that way until after the last transfer in a message.  Drivers
  * can affect the chipselect signal using cs_change:
  *
  * (i) If the transfer isn't the last one in the message, this flag is
@@ -331,6 +350,7 @@ struct spi_transfer {
 	dma_addr_t	rx_dma;
 
 	unsigned	cs_change:1;
+	unsigned	cs_inactive:1;
 	u8		bits_per_word;
 	u16		delay_usecs;
 	u32		speed_hz;
drivers/spi/spi_bitbang.c +19 -4
diff -upr a/drivers/spi/spi_bitbang.c b/drivers/spi/spi_bitbang.c
@@ -279,6 +279,7 @@ static void bitbang_work(void *_bitbang)
 		struct spi_transfer	*t = NULL;
 		unsigned		tmp;
 		unsigned		cs_change;
+		unsigned		cs_inactive;
 		int			status;
 		int			(*setup_transfer)(struct spi_device *,
 						struct spi_transfer *);
@@ -297,10 +298,14 @@ static void bitbang_work(void *_bitbang)
 		spi = m->spi;
 		tmp = 0;
 		cs_change = 1;
+		cs_inactive = 0;
 		status = 0;
 		setup_transfer = NULL;
 
 		list_for_each_entry (t, &m->transfers, transfer_list) {
+			cs_inactive = t->cs_inactive;
+			BUG_ON(cs_inactive && !spi->master->can_cs_inactive);
+
 			if (bitbang->shutdown) {
 				status = -ESHUTDOWN;
 				break;
@@ -327,11 +332,15 @@ static void bitbang_work(void *_bitbang)
 			 * selected ...)
 			 */
 			if (cs_change) {
-				bitbang->chipselect(spi, BITBANG_CS_ACTIVE);
+				bitbang->chipselect(spi,
+						    cs_inactive
+						    ? BITBANG_CS_INACTIVE
+						    : BITBANG_CS_ACTIVE);
 				ndelay(nsecs);
 			}
 			cs_change = t->cs_change;
-			if (!t->tx_buf && !t->rx_buf && t->len) {
+			if (!t->tx_buf && !t->rx_buf && t->len
+			    && !cs_inactive) {
 				status = -EINVAL;
 				break;
 			}
@@ -369,7 +378,10 @@ static void bitbang_work(void *_bitbang)
 			 * may be needed to terminate a mode or command
 			 */
 			ndelay(nsecs);
-			bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
+			bitbang->chipselect(spi,
+					    cs_inactive
+					    ? BITBANG_CS_ACTIVE
+					    : BITBANG_CS_INACTIVE);
 			ndelay(nsecs);
 		}
 
@@ -386,7 +398,10 @@ static void bitbang_work(void *_bitbang)
 		 */
 		if (!(status == 0 && cs_change)) {
 			ndelay(nsecs);
-			bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
+			bitbang->chipselect(spi,
+					    cs_inactive
+					    ? BITBANG_CS_ACTIVE
+					    : BITBANG_CS_INACTIVE);
 			ndelay(nsecs);
 		}
 

brgds, H-P

#1 lore raw
Quoted 10 lines · Hans-Peter Nilsson
(quoted diff) +7 -0
+                * some cards seemed happier if they were initialized first
+                * by the native MMC stack, not SPI ... and in other cases
+                * rmmod/modprobe of mmc_spi helped the card work better,
+                * even without power cycling
+                *
+                * FIXME find out what that important state is, which is
+                * not reset here... and makes robustness problems

I think I've spotted the problem, or at least a problem with a solution that fits the description.

Good, that was nasty ...

Quoted 9 lines

What's missing is "at least
74 SD clocks to the SD card with keeping CMD line to high. In case of SPI mode, CS shall be held to high during 74 clock cycles" (from Section 6.4.1, in "Simplified Physical Layer Specification 2.0"). This is to happen before sending CMD0. I have only one card (of 7) where this fails, a Viking Interworks 256 MB card. It matches the description in the comment; it never initializes, gives invalid replies (with bit 7 set) without this, but works fine with it.

That rings a bell.

Quoted 4 lines

The gotcha is that the SPI framework didn't have a way to express transfers with chip-select inactive. Sure, you can set chip-select to inactive for a period of *time*, but never while also toggling the clock. So here's an implementation for that.

Just so we don't lose count here: this is the *third* example of an SPI protocol tweaking option that seems to be needed just to support an MMC-over-SPI stack:

- Claiming the SPI bus so that chipselect can stay high even between spi_message interactions (although you seemed not to run into that one);

- Efficiency in the routine "poll for status" operation, where data must be read over MISO (CS high) until 0xff bytes stop;

- This issue, where a deselected device must be clocked.

I knew that not every SPI controller driver would be able to support that particular stack...

Quoted 4 lines

I made this functionality optional, with the updated mmc_spi driver trying anyway if the spi host doesn't provide the function. So, no SPI drivers require updating, unless they really want to work with mmc_spi flawlessly with all cards.

At a first glance, this seems like a reasonable approach. Let me think about it a bit. The ability for a controller driver to advertise a capability is reasonable, but so far it hasn't been used with SPI. (It ought to suffice just to reject unsupported requests, modulo the issue of trying to sort out exactly what was unsupported.)

Quoted 3 lines

I initially thought the function important enough to warrant mandatory implementation for all SPI drivers, but it hasn't been needed before, so I reconsidered.

Right. Unfortunately there's not a lot of protocol tweaking support we can currently expect to be "mandatory". Testing such requirements would be problematic too.

Quoted 3 lines

Also, some SPI drivers seem
to implement the chip-select function as optional, a clear hint to this being optional IMHO.

There's no "chip select" feature in the API. It's only part of the protocol described by messages delivered to the controller driver. Don't confuse the "bitbang" framework (which works with more than just bitbanged hardware, of course) with the API; it's just an implementation aid, and not one that all implementors would choose to use.

Quoted 11 lines

I looked at the existing drivers
and went as far as writing proof-of-concept patches for them, but as I can't test them, I'll just stop there and call that a feasibility study. The spi_bitbang looked like it could have always can_cs_inactive = 1, but then I noticed what spi_mpc83xx.c does in its mpc83xx_spi_chipselect() (register writes only on BITBANG_CS_ACTIVE supposedly controlling the clock) and so I think setting the capability is better be left to the respective caller (typically just before its call to spi_bitbang_start), and just adjust the various chipselect calls i spi_bitbang.

Sounds fair.

- Dave

#2 lore raw
Quoted 9 lines

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

Quoted 4 lines · Hans-Peter Nilsson

The gotcha is that the SPI framework didn't have a way to express transfers with chip-select inactive. Sure, you can set chip-select to inactive for a period of *time*, but never while also toggling the clock. So here's an implementation for that.

Quoted 7 lines

Just so we don't lose count here: this is the *third* example of an SPI protocol tweaking option that seems to be needed just to support an MMC-over-SPI stack:

- Claiming the SPI bus so that chipselect can stay high even between spi_message interactions (although you seemed not to run into that one);

You mentioned this is the reason for the spi_get_exclusive-patch you sent privately (thanks). I hadn't seen that, and from my reading of the standard I read "bus transaction" (their wording for what's between cs deactivation) as a single byte or at least send and reply part of a command as separate "bus transactions". Yeah, there's only one SPI device on my bus. To simulate other devices on the bus, chatting along when the SD/MMC SPI device has chip-select inactive, I experimented with sending "random" stuff while in the chip-select function like so:

(in chip_select_function at the end, added)
...

        if (value != BITBANG_CS_ACTIVE)
                send_random(spi);
}

void send_random(struct spi_device *spi)

{
        u8 buf[256];
        int i;

        static int value = 0;
        static int n = 0;

        struct spi_transfer t;
        memset (&t, 0, sizeof t);
        t.tx_buf = buf;
        t.len = ++n;

        for (i = 0; i < n; i++)
                buf[i] = value++;

        if (crisv32_spi_sser_dma_txrx_bufs(spi, &t) != n)
                panic ("badness\n");

        if (n == sizeof buf)
                n = 1;
}

... and *now* I see it. ;-) No card works. Hm. Modulo bugs in the above, I agree you need that claim-stuff if there's more than one device. Still, I guess that's actually a rare configuration.

Quoted 2 lines

- Efficiency in the routine "poll for status" operation, where data must be read over MISO (CS high) until 0xff bytes stop;

That's for read. For write, it's until the 0x0 bytes stop. But this is a performance feature, not critical for basic function.

Quoted 1 line

- This issue, where a deselected device must be clocked.

I retract this patch and suggestion, given your later suggestion. (Not worthwhile as a feature.)

Quoted 2 lines

I knew that not every SPI controller driver would be able to support that particular stack...

Not all are important as long as mmc_spi provides fallback solutions, or what works all the time with some cards.

Quoted 10 lines
Quoted 3 lines

Also, some SPI drivers seem
to implement the chip-select function as optional, a clear hint to this being optional IMHO.

There's no "chip select" feature in the API. It's only part of the protocol described by messages delivered to the controller driver. Don't confuse the "bitbang" framework (which works with more than just bitbanged hardware, of course) with the API; it's just an implementation aid, and not one that all implementors would choose to use.

Let me rephrase: some controllers (yes, they are using the bitbang framework but that's beside the point) do not deactivate chip-select; they may not have that signal programmable. Therefore, some are using SPI as if chip-select is an optional part of the function, so maybe it should be left as that.

brgds, H-P

#3 lore raw
Quoted 20 lines · Hans-Peter Nilsson
Quoted 9 lines

From: David Brownell <[email protected]>

Just so we don't lose count here: this is the *third* example of an SPI protocol tweaking option that seems to be needed just to support an MMC-over-SPI stack:

- Claiming the SPI bus so that chipselect can stay high even between spi_message interactions (although you seemed not to run into that one);

You mentioned this is the reason for the spi_get_exclusive-patch you sent privately (thanks). I hadn't seen that, and from my reading of the standard I read "bus transaction" (their wording for what's between cs deactivation) as a single byte or at least send and reply part of a command as separate "bus transactions". Yeah, there's only one SPI device on my bus. To simulate other devices on the bus, chatting along when the SD/MMC SPI device has chip-select inactive, I experimented with sending "random" stuff while in the chip-select function like so:

Quoted 6 lines

...

... and *now* I see it. ;-) No card works. Hm. Modulo bugs in the above, I agree you need that claim-stuff if there's more than one device. Still, I guess that's actually a rare configuration.

The issue is generic. It shows up with JTAG (which you can view as an application of SPI) as well as various other tools I've seen. As for rare ... hard to say. I don't design boards, and the ones where I look at SPI support have real MMC hardware so they don't need mmc-over-spi.

Quoted 5 lines
Quoted 2 lines

- Efficiency in the routine "poll for status" operation, where data must be read over MISO (CS high) until 0xff bytes stop;

That's for read. For write, it's until the 0x0 bytes stop. But this is a performance feature, not critical for basic function.

Right.

Quoted 4 lines
Quoted 1 line

- This issue, where a deselected device must be clocked.

I retract this patch and suggestion, given your later suggestion. (Not worthwhile as a feature.)

OK, fine. Temporary toggling of chipselect polarity ... that doesn't _quite_ feel like a hack!

Quoted 10 lines
Quoted 6 lines

There's no "chip select" feature in the API. It's only part of the protocol described by messages delivered to the controller driver. Don't confuse the "bitbang" framework (which works with more than just bitbanged hardware, of course) with the API; it's just an implementation aid, and not one that all implementors would choose to use.

Let me rephrase: some controllers (yes, they are using the bitbang framework but that's beside the point) do not deactivate chip-select; they may not have that signal programmable.

I think those controllers are best viewed as not talking SPI.

Or alternatively, as being so bare-bones that they expect to have chipselects managed outside the controller. (Which is not a bad approach after all.)

- Dave

#4 lore raw
In reply to Hans-Peter Nilsson
Quoted 18 lines · Hans-Peter Nilsson

There was a comment in the mmc_spi.c glue driver at <URL:http://www.gossamer-threads.com/lists/linux/kernel/671939#671939>:

(quoted diff) +7 -0
+                * some cards seemed happier if they were initialized first
+                * by the native MMC stack, not SPI ... and in other cases
+                * rmmod/modprobe of mmc_spi helped the card work better,
+                * even without power cycling
+                *
+                * FIXME find out what that important state is, which is
+                * not reset here... and makes robustness problems

I think I've spotted the problem, or at least a problem with a solution that fits the description. What's missing is "at least 74 SD clocks to the SD card with keeping CMD line to high. In case of SPI mode, CS shall be held to high during 74 clock cycles" (from Section 6.4.1, in "Simplified Physical Layer Specification 2.0"). ...

Quoted 4 lines

The gotcha is that the SPI framework didn't have a way to express transfers with chip-select inactive. Sure, you can set chip-select to inactive for a period of *time*, but never while also toggling the clock.

Actually it _does_ have a way to handle it, if you think about the problem a bit differently ... focus on high/low, not "inactive":

	spi->mode |= SPI_CS_HIGH;
	spi_setup(spi);
	// chipselect is now low (conventionally active)

	// ... then high during this next transfer:
	... write 74+ zero bits (10+ bytes)
	// now low again (conventionally "active")

	spi->mode &= ~SPI_CS_HIGH;
	spi_setup(spi);	
	// now high (conventionally "inactive")

That is, for just one one transfer, say the device uses inverse chipselect polarity: active-high, not active-low. Then back to normal. Right? So long as nobody else can access that SPI bus (the claim/release issue), all should be fine.

That mechanism has been defined for some time, but not widely implemented; a few chips rquite that semantic for chipselect in normal operation.

If you agree on this, please update your patch #4 accordingly. You may need to update your SPI controller driver to handle this issue too. (ISTR punting on making the bitbang framework handle it, but so long as the chipselect is managed with a GPIO this ought to be almost trivial.)

- Dave

#5 lore raw

PATCH 1/1 take 2: mmc_spi update, 2.6.19

Quoted 11 lines

From: David Brownell <[email protected]>
Date: Thu, 25 Jan 2007 17:28:11 -0800

Quoted 4 lines · Hans-Peter Nilsson

The gotcha is that the SPI framework didn't have a way to express transfers with chip-select inactive. Sure, you can set chip-select to inactive for a period of *time*, but never while also toggling the clock.

Actually it _does_ have a way to handle it, if you think about the problem a bit differently ... focus on high/low, not "inactive":

... (Using "spi->mode |= SPI_CS_HIGH" and calling spi_setup)...

I did, but I believed that "setup" should happen only before any transfer. Allright, it's documented as callable multiple times and you define the API anyway :) and it's one less feature to maintain and worry about to make everyone implement correctly, so I agree this is better.

Quoted 1 line

If you agree on this, please update your patch #4 accordingly.

Here we go. Only the mmc_spi part is updated; the other ones are identical. I resend them anyway to keep this message self-contained. (Except it uses the previous default_tx_value, patch, bother. :)

(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 +1500 -0

1,500 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