Skip to contentSkip to conversation

spi: SuperH MSIOF SPI Master driver

PATCH In review Magnus Damm 3 participants 5 messages Nov 24, 2009 – Nov 26, 2009 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: SuperH MSIOF SPI Master driver

From: Magnus Damm <[email protected]>

This patch adds SPI Master support for the SuperH MSIOF hardware block. Full duplex, spi mode 0-3, active high cs, 3-wire and lsb first should all be supported, but the driver has so far only been tested with "mmc_spi".

The MSIOF hardware comes with 32-bit FIFOs for receive and transmit, and this driver simply breaks the SPI messages into FIFO-sized chunks. The MSIOF hardware manages the pins for clock, receive and transmit (sck/miso/mosi), but the chip select pin is managed by software and must be configured as a regular GPIO pin by the board code.

Performance wise there is still room for improvement, but on a Ecovec board with the built-in sh7724 MSIOF0 this driver gets Mini-sd read speeds of about half a megabyte per second.

Future work include better clock setup and merging of 8-bit transfers into 32-bit words to reduce interrupt load and improve throughput.

---

 drivers/spi/Kconfig          |    7 
 drivers/spi/Makefile         |    1 
 drivers/spi/spi.c            |    1 
 drivers/spi/spi_sh_msiof.c   |  675 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/spi/sh_msiof.h |   10 
 5 files changed, 693 insertions(+), 1 deletion(-)
drivers/spi/Kconfig +7 -0
@@ -215,6 +215,13 @@ config SPI_S3C24XX_GPIO
 	  the inbuilt hardware cannot provide the transfer mode, or
 	  where the board is using non hardware connected pins.
 
+config SPI_SH_MSIOF
+	tristate "SuperH MSIOF SPI controller"
+	depends on SUPERH && HAVE_CLK
+	select SPI_BITBANG
+	help
+	  SPI driver for SuperH MSIOF blocks.
+
 config SPI_SH_SCI
 	tristate "SuperH SCI SPI controller"
 	depends on SUPERH
drivers/spi/Makefile +1 -0
@@ -32,6 +32,7 @@ obj-$(CONFIG_SPI_S3C24XX)		+= spi_s3c24x
 obj-$(CONFIG_SPI_TXX9)			+= spi_txx9.o
 obj-$(CONFIG_SPI_XILINX)		+= xilinx_spi.o
 obj-$(CONFIG_SPI_SH_SCI)		+= spi_sh_sci.o
+obj-$(CONFIG_SPI_SH_MSIOF)		+= spi_sh_msiof.o
 obj-$(CONFIG_SPI_STMP3XXX)		+= spi_stmp.o
 # 	... add above this line ...
 
drivers/spi/spi.c +0 -1
@@ -17,7 +17,6 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-
 #include <linux/kernel.h>
 #include <linux/device.h>
 #include <linux/init.h>
drivers/spi/spi_sh_msiof.c new file +675 -0

675 lines not on this page · raw message

include/linux/spi/sh_msiof.h new file +10 -0
@@ -0,0 +1,10 @@
+#ifndef __SPI_SH_MSIOF_H__
+#define __SPI_SH_MSIOF_H__
+
+struct sh_msiof_spi_info {
+	int tx_fifo_override;
+	int rx_fifo_override;
+	u16 num_chipselect;
+};
+
+#endif /* __SPI_SH_MSIOF_H__ */
#1 lore raw
Quoted 28 lines · On Tue, 24 Nov 2009 21:55:31 +0900 Magnus Damm <[email protected]> wrote:

From: Magnus Damm <[email protected]>

This patch adds SPI Master support for the SuperH MSIOF hardware block. Full duplex, spi mode 0-3, active high cs, 3-wire and lsb first should all be supported, but the driver has so far only been tested with "mmc_spi".

The MSIOF hardware comes with 32-bit FIFOs for receive and transmit, and this driver simply breaks the SPI messages into FIFO-sized chunks. The MSIOF hardware manages the pins for clock, receive and transmit (sck/miso/mosi), but the chip select pin is managed by software and must be configured as a regular GPIO pin by the board code.

Performance wise there is still room for improvement, but on a Ecovec board with the built-in sh7724 MSIOF0 this driver gets Mini-sd read speeds of about half a megabyte per second.

Future work include better clock setup and merging of 8-bit transfers into 32-bit words to reduce interrupt load and improve throughput.

...

drivers/spi/spi.c line 22

Quoted 7 lines
drivers/spi/spi.c +0 -1
@@ -17,7 +17,6 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-
 #include <linux/kernel.h>
 #include <linux/device.h>
 #include <linux/init.h>

whoops?

Quoted 5 lines
drivers/spi/spi_sh_msiof.c new file +2 -0
@@ -0,0 +1,675 @@
+/*
+ * SuperH MSIOF SPI Master Interface

drivers/spi/spi_sh_msiof.c line 12

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+ *
+ * Copyright (c) 2009 Magnus Damm
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/kernel.h>

But not consistently.

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+#include <linux/init.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/completion.h>
+#include <linux/pm_runtime.h>
+#include <linux/gpio.h>
+#include <linux/bitmap.h>
+#include <linux/clk.h>
+#include <linux/io.h>

drivers/spi/spi_sh_msiof.c line 32

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/sh_msiof.h>
+
+#include <asm/spi.h>
+#include <asm/unaligned.h>
+
+struct sh_msiof_spi_priv {
+	struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */

Well if that's the case then spi_bitbang.c needs smacking. What causes this requirement?

Quoted 16 lines
drivers/spi/spi_sh_msiof.c new file +13 -0
+	void __iomem *mapbase;
+	struct clk *clk;
+	struct platform_device *pdev;
+	struct sh_msiof_spi_info *info;
+	struct completion done;
+	unsigned long flags;
+	int tx_fifo_size;
+	int rx_fifo_size;
+};
+
 
...
 
+static void sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
+				     unsigned long clr, unsigned long set)
+{

drivers/spi/spi_sh_msiof.c line 107

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+	unsigned long mask = clr | set;
+	unsigned long data;
+
+	data = sh_msiof_read(p, CTR);
+	data &= ~clr;
+	data |= set;
+	sh_msiof_write(p, CTR, data);
+
+	while ((sh_msiof_read(p, CTR) & mask) != set)
+		;

hm, confidence. No timeout needed here?

Quoted 20 lines
drivers/spi/spi_sh_msiof.c new file +20 -0
+}
+
+static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
+{
+	struct sh_msiof_spi_priv *p = data;
+
+	/* just disable the interrupt and wake up */
+	sh_msiof_write(p, IER, 0);
+
+	complete(&p->done);
+
+	return IRQ_HANDLED;
+}
+
+static struct {
+	unsigned short div;
+	unsigned short scr;
+} sh_msiof_spi_clk_table[] = {
+	{ 1, 0x0007 },
+	{ 2, 0x0000 },

drivers/spi/spi_sh_msiof.c line 137

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+	{ 4, 0x0001 },
+	{ 8, 0x0002 },
+	{ 16, 0x0003 },
+	{ 32, 0x0004 },
+	{ 64, 0x1f00 },
+	{ 128, 0x1f01 },
+	{ 256, 0x1f02 },
+	{ 512, 0x1f03 },
+	{ 1024, 0x1f04 },
+};

Could be const (to save some .data) btu I think the compiler does that itself nowadays.

drivers/spi/spi_sh_msiof.c line 149

Quoted 11 lines
drivers/spi/spi_sh_msiof.c new file +11 -0
+static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
+				      unsigned long parent_rate,
+				      unsigned long spi_hz)
+{
+	unsigned long div = 1024;
+	int k;
+
+	if (!spi_hz || !parent_rate)
+		WARN_ON(1);
+	else
+		div = parent_rate / spi_hz;

This could be more neatly coded as

	if (!WARN_ON(!spi_hz || !parent_rate))
		div = parent_rate / spi_hz;

also, if this warning ever triggers, you won't know whether it was due to !spi_hx or to !parent_rate, which might make you sad.

Can spi_hz and parent_rate ever be zero here anyway? If not, zap it. If so, why? Programming bug?

drivers/spi/spi_sh_msiof.c line 158

Quoted 8 lines
drivers/spi/spi_sh_msiof.c new file +8 -0
+	/* TODO: make more fine grained */
+
+	for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
+		if (sh_msiof_spi_clk_table[k].div >= div)
+			break;
+	}
+
+	k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);

actually it's pretty obvious from all the above that k should have been size_t.

Quoted 23 lines
drivers/spi/spi_sh_msiof.c new file +23 -0
+	sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
+	sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
+}
+
+static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
+				      int cpol, int cpha,
+				      int tx_hi_z, int lsb_first)
+{
+	unsigned long tmp;
+	int edge;
+
+	/*
+	 * CPOL CPHA     TSCKIZ RSCKIZ TEDG REDG(!)
+	 *    0    0         10     10    1    0
+	 *    0    1         10     10    0    1
+	 *    1    0         11     11    0    1
+	 *    1    1         11     11    1    0
+	 *
+	 * (!) Note: REDG is inverted recommended data sheet setting
+	 */
+
+	sh_msiof_write(p, FCTR, 0);
+	sh_msiof_write(p, TMDR1, 0xe2000005 | (lsb_first << 24));

drivers/spi/spi_sh_msiof.c line 192

Quoted 10 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+	sh_msiof_write(p, RMDR1, 0x22000005 | (lsb_first << 24));
+
+	tmp = 0xa0000000;
+	tmp |= cpol << 30; /* TSCKIZ */
+	tmp |= cpol << 28; /* RSCKIZ */
+
+	edge = cpol ? cpha : !cpha;
+
+	tmp |= edge << 27; /* TEDG */
+	tmp |= !edge << 26; /* REDG */

um, OK. This mixture of logical-not with bit-operations is a bit woozy but I don't see any actual bugs there.

drivers/spi/spi_sh_msiof.c line 516

Quoted 11 lines
drivers/spi/spi_sh_msiof.c new file +8 -0
+	tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
+	sh_msiof_write(p, CTR, tmp);
+}
+
 
...
 
+static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
+				  u32 word, u8 bits)
+{
+	BUG_ON(1); /* unused but needed by bitbang code */
	BUG();
Quoted 6 lines
drivers/spi/spi_sh_msiof.c new file +3 -0
+	return 0;
+}
+
 
...
#2 lore raw

Hi Andrew,

Quoted 15 lines · Andrew Morton

drivers/spi/spi.c    2009-11-24 20:39:48.000000000 +0900 line 22

Quoted 11 lines · On Tue, 24 Nov 2009 21:55:31 +0900 Magnus Damm <[email protected]> wrote:

This patch adds SPI Master support for the SuperH MSIOF

drivers/spi/spi.c    2009-11-24 20:39:48.000000000 +0900 renamed from drivers/spi/spi.c +0 -1
@@ -17,7 +17,6 @@
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
-
  #include <linux/kernel.h>
  #include <linux/device.h>
  #include <linux/init.h>

whoops?

Yeah, i totally missed that one.

Quoted 9 lines
Quoted 9 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +6 -0
@@ -0,0 +1,675 @@
+/*
+ * SuperH MSIOF SPI Master Interface
+ *
+ * Copyright (c) 2009 Magnus Damm
+ *
+ * This program is free software; you can redistribute it and/or modify
Quoted 8 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 12

Quoted 6 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +6 -0
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/kernel.h>

But not consistently.

Never. =)

Quoted 15 lines
Quoted 15 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +15 -0
+#include <linux/init.h>
+#include <linux/delay.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/completion.h>
+#include <linux/pm_runtime.h>
+#include <linux/gpio.h>
+#include <linux/bitmap.h>
+#include <linux/clk.h>
+#include <linux/io.h>
+
+#include <linux/spi/spi.h>
+#include <linux/spi/spi_bitbang.h>
+#include <linux/spi/sh_msiof.h>
+
Quoted 8 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 32

Quoted 5 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +5 -0
+#include <asm/spi.h>
+#include <asm/unaligned.h>
+
+struct sh_msiof_spi_priv {
+     struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */

Well if that's the case then spi_bitbang.c needs smacking.  What causes this requirement?

The spi_bitbang.c functions spi_bitbang_setup() and spi_bitbang_transfer() use spi_master_get_devdata() to get the bitbang data structure. Most drivers want to be able to get a pointer to private data using spi_master_get_devdata(), so the bitbang-structure-as-first-member trick is used so the driver code and the bitbang functions can coexist.

A couple of in-tree drivers do the same thing and store the bitbang structure as the first member. For instance, have a look at omap_uwire.c and spi_gpio.c.

Maybe adding a spi_alloc_bitbang_master() function to spi_bitbang.c that does the bitbang structure allocation behind the scenes would be a good thing. The bitbang drivers can then use that function instead of spi_alloc_master(). The bitbang code itself can retrieve the bitbang structure by doing offset calculations from the master pointer. That would free up the private pointer.

There may be better ways to do this though...

Quoted 7 lines
Quoted 7 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +7 -0
+static void sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
+                                  unsigned long clr, unsigned long set)
+{
+     unsigned long mask = clr | set;
+     unsigned long data;
+
+     data = sh_msiof_read(p, CTR);
Quoted 8 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 45

Quoted 6 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +6 -0
+     data &= ~clr;
+     data |= set;
+     sh_msiof_write(p, CTR, data);
+
+     while ((sh_msiof_read(p, CTR) & mask) != set)
+             ;

hm, confidence.  No timeout needed here?

Good plan, I'll add some timeout handling code.

Quoted 11 lines
Quoted 11 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +11 -0
+static struct {
+     unsigned short div;
+     unsigned short scr;
+} sh_msiof_spi_clk_table[] = {
+     { 1, 0x0007 },
+     { 2, 0x0000 },
+     { 4, 0x0001 },
+     { 8, 0x0002 },
+     { 16, 0x0003 },
+     { 32, 0x0004 },
+     { 64, 0x1f00 },
Quoted 8 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 61

Quoted 5 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +5 -0
+     { 128, 0x1f01 },
+     { 256, 0x1f02 },
+     { 512, 0x1f03 },
+     { 1024, 0x1f04 },
+};

Could be const (to save some .data) btu I think the compiler does that itself nowadays.

I'll fix that up.

Quoted 14 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 72

Quoted 11 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +11 -0
+static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
+                                   unsigned long parent_rate,
+                                   unsigned long spi_hz)
+{
+     unsigned long div = 1024;
+     int k;
+
+     if (!spi_hz || !parent_rate)
+             WARN_ON(1);
+     else
+             div = parent_rate / spi_hz;

This could be more neatly coded as

Quoted 8 lines

       if (!WARN_ON(!spi_hz || !parent_rate))
               div = parent_rate / spi_hz;

also, if this warning ever triggers, you won't know whether it was due to !spi_hx or to !parent_rate, which might make you sad.

Can spi_hz and parent_rate ever be zero here anyway?  If not, zap it. If so, why?  Programming bug?

I wanted to fail gracefully if the spi layer or the clock framework handed us zeroes. They are most likely not supposed to do so, but it probably doesn't hurt to keep the check. This to catch bugs that may be around - the spi setup code flow is pretty complex and the clock framework implementation may vary from processor to processor.

Quoted 11 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 80

Quoted 8 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +8 -0
+     /* TODO: make more fine grained */
+
+     for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
+             if (sh_msiof_spi_clk_table[k].div >= div)
+                     break;
+     }
+
+     k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);

actually it's pretty obvious from all the above that k should have been size_t.

Cool, will fix.

Quoted 6 lines

drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 line 84

Quoted 4 lines
drivers/spi/spi_sh_msiof.c   2009-11-24 20:39:49.000000000 +0900 new file +4 -0
+static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
+                               u32 word, u8 bits)
+{
+     BUG_ON(1); /* unused but needed by bitbang code */

       BUG();

That makes sense. =)

Thanks for your review!

/ magnus

#3 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton

drivers/spi/spi_sh_msiof.c line 32

Quoted 2 lines · On Tue, 24 Nov 2009 21:55:31 +0900 Magnus Damm <[email protected]> wrote:
drivers/spi/spi_sh_msiof.c new file +2 -0
+struct sh_msiof_spi_priv {
+	struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */

Well if that's the case then spi_bitbang.c needs smacking. What causes this requirement?

spi_bitbang causes this requirement by being lazy with regards to private data stashing. Both the drivers and the bitbang code use spi_master_get_devdata() for getting their private data, which wraps in to a dev_get_drvdata().

This needs to be reworked so that struct spi_master has its own private data pointer which helper code like spi_bitbang can use, while freeing up the struct device private data pointer so it can be freely used by drivers as normal.

This is the same sort of private data through casting whimsy that the framebuffer drivers used to employ, only fortunately that behaviour never made it out of 2.3.x kernels -- until now!

Quoted 13 lines
Quoted 13 lines
drivers/spi/spi_sh_msiof.c new file +10 -0
+	void __iomem *mapbase;
+	struct clk *clk;
+	struct platform_device *pdev;
+	struct sh_msiof_spi_info *info;
+	struct completion done;
+	unsigned long flags;
+	int tx_fifo_size;
+	int rx_fifo_size;
+};
+
 
...
Quoted 16 lines

drivers/spi/spi_sh_msiof.c line 107

Quoted 13 lines
drivers/spi/spi_sh_msiof.c new file +13 -0
+static void sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
+				     unsigned long clr, unsigned long set)
+{
+	unsigned long mask = clr | set;
+	unsigned long data;
+
+	data = sh_msiof_read(p, CTR);
+	data &= ~clr;
+	data |= set;
+	sh_msiof_write(p, CTR, data);
+
+	while ((sh_msiof_read(p, CTR) & mask) != set)
+		;

hm, confidence. No timeout needed here?

This definitely needs a timeout, nothing involving SPI inspires confidence. A cpu_relax() to prevent the compiler from optimizing the loop out would help, too.

#4 lore raw
Quoted 16 lines · On Thu, 26 Nov 2009 15:43:16 +0900 Paul Mundt…
Quoted 7 lines
Quoted 7 lines
drivers/spi/spi_sh_msiof.c new file +7 -0
+static void sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
+				     unsigned long clr, unsigned long set)
+{
+	unsigned long mask = clr | set;
+	unsigned long data;
+
+	data = sh_msiof_read(p, CTR);
Quoted 9 lines

drivers/spi/spi_sh_msiof.c line 107

Quoted 6 lines
drivers/spi/spi_sh_msiof.c new file +6 -0
+	data &= ~clr;
+	data |= set;
+	sh_msiof_write(p, CTR, data);
+
+	while ((sh_msiof_read(p, CTR) & mask) != set)
+		;

hm, confidence. No timeout needed here?

Quoted 3 lines

This definitely needs a timeout, nothing involving SPI inspires confidence. A cpu_relax() to prevent the compiler from optimizing the loop out would help, too.

We generally don't bother with the relax in an IO polling loop like this. It involves an IO read/write which the compiler cannot fiddle with and I believe that CPUs will generally take the opportunity to have a little snooze while the slow IO operation is happening.

Can't hurt though ;)

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july