Skip to contentSkip to conversation

spi: driver for Marvell Orion SPI controllers]

PATCH saeed 1 participant 1 message Jul 6, 2008 2 messages not in the archive SPI

lore
Share & tools
lore

saeed

author
#2 lore raw
In reply to a message not in the archive
Quoted 11 lines

From: David Brownell <david-b-yBeKhBN/[email protected]>
To: Lennert Buytenhek <buytenh-OLH4Qvv75CYX/[email protected]>
Subject: Re: [PATCH] spi: driver for Marvell Orion SPI controllers
Date: Fri, 27 Jun 2008 18:14:29 -0700
User-Agent: KMail/1.9.9
Cc: spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org, [email protected]
In-Reply-To: <20080626183911.GC16994-mfnYTeDhw6uOVk/H6u/4e9i2O/[email protected]>

This has some deep assumptions that this controller only ever has one chipselect. Is that really not going to be worked around by using GPIOs? If so, color me surprised...

That's true, the driver supports only 1 slave, and the num_chipselect is set accordingly.

Quoted 7 lines

I'd encourage you to change the structure earlier, rather than later, to make such (almost inevitable) tweaks easier. Or at least, flag places you're assuming there can never be more than one slave -- even possibly a copout comment up at the top with a big "**WARNING** flag documenting that little surprise.

I made the code ready to support multi slaves by making the orion_spi_setup_transfer() the only place where hw settings can be changed, and that functoin guaranteed to be invoked only when no other transaction is taking place.

Quoted 7 lines · Lennert Buytenhek
Quoted 2 lines

+#define SPI_REG(orion_spi, reg)                (((u32)orion_spi->base) + (reg))

Inline functions please, not macros-with-unparenthesized-params.

done

Quoted 16 lines

Review

Quoted 11 lines
(quoted diff) +9 -0
+static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
+{
+	if (size == 16) {
+		orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
+				  ORION_SPI_IF_8_16_BIT_MODE);
+	} else if (size == 8) {
+		orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
+				  ORION_SPI_IF_8_16_BIT_MODE);
+	} else {

... always writes register bank, except on error cases; OK, but I would have filtered out the error cases from orion_spi_setup() or orion_spi_transfer(). In fact I notice transfer() has such a test but it just doesn't match this one!

done

Quoted 15 lines

Review

Quoted 12 lines
(quoted diff) +10 -0
+		pr_debug("Bad bits per word value %d (only 8 or 16 are "
+			 "allowed).\n", size);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+
+static u8 prescale[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };

Should be const ... I suppose this generates better code than the obvious "for" loop?

the new code will calculate the needed ratio using divisions, this "lookup" code that uses that array is deleted

Quoted 49 lines
Quoted 12 lines
(quoted diff) +10 -0
+
+static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int rate)
+{
+	u32 min_offset = 0xFFFFFFFF;
+	u8 best_idx = 100;
+	int i;
+	u32 cpu_clk;
+	u32 reg;
+	struct orion_spi *orion_spi;
+	struct orion_spi_info *spi_info;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	...
+
+	if (best_idx > ARRAY_SIZE(prescale))
+		return -EINVAL;
+
+	/* Convert the array index to SPI clock divisor value.	*/
+	reg = readl(SPI_REG(orion_spi, ORION_SPI_IF_CONFIG_REG));
+	reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | (best_idx + 0x12));
+	writel(reg, SPI_REG(orion_spi, ORION_SPI_IF_CONFIG_REG));

... always writes register bank, except on error cases ...

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+	return 0;
+}
+
+/*
+ * called only when no transfer is active to this device

I think you mean "any" device, in fact. This one looks OK at a first glance.

Review

Quoted 7 lines

+ */

(quoted diff) +6 -0
+static int orion_spi_setup_transfer(struct spi_device *spi,
+				    struct spi_transfer *t)
+{
+	...
+}
+

But *HERE* I see a bunch of issues, or at least things that will break the first time someone tries to support a second slave on this controller.

Quoted 3 lines

Remember that spi_setup(spi) may be called at any time "spi" has no pending messages ... very much in particular, when some other device is active.

this function modified so it will not change the settings, that will be done by orion_spi_setup_transfer() before each transfer message

Quoted 24 lines
Quoted 10 lines
(quoted diff) +9 -0
+static int orion_spi_setup(struct spi_device *spi)
+{
+	struct orion_spi_state *spi_state;
+	struct orion_spi *orion_spi;
+	struct orion_spi_info *spi_info;
+
+	spi_state = spi->controller_state;
+	orion_spi = spi_master_get_devdata(spi->master);
+	spi_info = orion_spi->spi_info;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	if (spi->bits_per_word == 0) {
+		spi->bits_per_word = ORION_SPI_DEF_BPW;
+	} else if (spi->bits_per_word < 8 || spi->bits_per_word > 16) {
+		dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
+			spi->bits_per_word);
+		return -EINVAL;
+	}
+
+	if (orion_spi_set_transfer_size(orion_spi, spi->bits_per_word) != 0)

OK, you may just have trashed transfers that are now taking place at another wordsize to a different device...

Quoted 12 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+		return -EINVAL;
+
+	if (!spi_state) {
+		spi_state = kzalloc(sizeof(*spi_state), GFP_KERNEL);
+		if (!spi_state)
+			return -ENOMEM;
+		spi->controller_state = spi_state;
+	}
+	spi_state->data_width = spi->bits_per_word;

You don't need to record bits-per-word; it's already held for you in the spi_device.

done, this makes no need for the spi state struction

Quoted 13 lines

Review

Quoted 5 lines
(quoted diff) +3 -0
+
+	dev_dbg(&spi->dev, "setup: Set MAX Speed to %d ", spi->max_speed_hz);
+	if (orion_spi_baudrate_set(spi, spi->max_speed_hz) == 0)

Here you may have just trashed transfers that are now taking place at a different bitrate (say, slower) to a different device...

Looks to me like you should probably update spi_state so that it holds the default register settings for that device ... or maybe just have this code validate the settings.

Quoted 8 lines

Speaking of which ... what about the spi->mode bits? This driver totally ignores CPOL/CPHA (the four SPI modes), the chipselect polarity, and so on.

At an *absolute minimum* it should verify that spi->mode is the single valeu this evidently handles. Notice how other drivers handle this: spi_setup() rejects calls when any of the bits in spi->mode aren't understood by the driver.

done, I also added the max and min speeds of the controller, the max speed will be the default speed. and error code will be return if the requested speed is lower that the min speed.

Quoted 27 lines
Quoted 27 lines
(quoted diff) +25 -0
+		dev_dbg(&spi->dev, "OK");
+	else
+		dev_dbg(&spi->dev, "Failed");
+
+	return 0;
+}
+
+static int orion_spi_transfer(struct spi_device *spi, struct spi_message *m)
+{
+	struct orion_spi *orion_spi;
+	unsigned long flags;
+	struct spi_transfer *t;
+
+	m->actual_length = 0;
+	m->status = 0;
+
+	/* reject invalid messages and transfers */
+	if (list_empty(&m->transfers) || !m->complete)
+		return -EINVAL;
+
+	list_for_each_entry(t, &m->transfers, transfer_list) {
+		const void *tx_buf = t->tx_buf;
+		void *rx_buf = t->rx_buf;
+		unsigned len = t->len;
+
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+		if (t->speed_hz > ORION_SPI_MAX_FREQ
+				|| (len && !(rx_buf || tx_buf))
+				|| (t->bits_per_word &&
+					(t->bits_per_word < 8
+					|| t->bits_per_word > 16))) {

See? this requires bits-per-word between 8 and 16 bits, but the other place insists 8 *or* 16 bits.

this code is deleted as the sanity check is done from the orion_spi_set_transfer_size()

Quoted 64 lines
Quoted 19 lines
(quoted diff) +17 -0
+			dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
+					t->speed_hz,
+					len,
+					tx_buf ? "tx" : "",
+					rx_buf ? "rx" : "",
+					t->bits_per_word);
+			return -EINVAL;
+		}
+	}
+
+	orion_spi = spi_master_get_devdata(spi->master);
+
+	spin_lock_irqsave(&orion_spi->lock, flags);
+	list_add_tail(&m->queue, &orion_spi->msg_queue);
+	queue_work(orion_spi_wq, &orion_spi->work);
+	spin_unlock_irqrestore(&orion_spi->lock, flags);
+

Review

Quoted 10 lines
(quoted diff) +9 -0
+	return 0;
+}
+
 
+static struct platform_driver orion_spi_driver = {
+	.driver = {
+		.name	= DRIVER_NAME,
+		.owner	= THIS_MODULE,
+	},
+	.remove		= __exit_p(orion_spi_remove),

Good, I always like to see drivers do the right thing with respect to runtime footprint shrinkage. For platform drivers that's usually what you did here: __exit_p() for the remove function, and use platform_driver_probe().

Review

Quoted 8 lines
(quoted diff) +8 -0
+};
+
+static int __init orion_spi_init(void)
+{
+	orion_spi_wq = create_singlethread_workqueue(
+				orion_spi_driver.driver.name);
+	if (orion_spi_wq == NULL)
+		return -1;

This should probably be "return -ENOMEM;" ... never return "-1" when the interface requires a negative errno.

(quoted diff) +0 -0
@@ -0,0 +1,17 @@

Review

Quoted 12 lines
(quoted diff) +12 -0
+/*
+ * include/linux/spi/orion_spi.h
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#ifndef __LINUX_SPI_ORION_SPI_H
+#define __LINUX_SPI_ORION_SPI_H
+
+struct orion_spi_info {

What's a "tclk"? And why can't you just

Quoted 4 lines
	tclk = clk_get_rate(clk_get(NULL, "cpu"));

or whatever? This looks likely to break whenever
anyone re-clocks a board.

The tclk is the clock rate in Hz of the controller. we can't use the clk framework since it's not implemented yet.

Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +5 -0
+	u32	tclk;
+};
+
+
+#endif

Here is the updated patch:
From 80e39e25f9dd35100f80b799f032711c3673da79 Mon Sep 17 00:00:00 2001
From: Shadi Ammouri <[email protected]>
Date: Fri, 27 Jun 2008 19:10:06 +0200
Subject: [PATCH] spi: driver for Marvell Orion SPI controllers

This adds a SPI driver for the SPI controller found in various Marvell Orion ARM SoCs.

---
 drivers/spi/Kconfig           |    6 +
 drivers/spi/Makefile          |    1 +
 drivers/spi/orion_spi.c       |  545 +++++++++++++++++++++++++++++++++++++++++
 include/linux/spi/orion_spi.h |   17 ++
 4 files changed, 569 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/orion_spi.c
 create mode 100644 include/linux/spi/orion_spi.h
drivers/spi/Kconfig +6 -0
index 66ec5d8..be4ba17 100644
@@ -149,6 +149,12 @@ config SPI_OMAP24XX
 	  SPI master controller for OMAP24xx/OMAP34xx Multichannel SPI
 	  (McSPI) modules.
 
+config SPI_ORION
+	tristate "Orion SPI master (EXPERIMENTAL)"
+	depends on SPI_MASTER && PLAT_ORION && EXPERIMENTAL
+	help
+	  This enables using the SPI master controller on the Orion chips.
+
 config SPI_PXA2XX
 	tristate "PXA2xx SSP SPI master"
 	depends on SPI_MASTER && ARCH_PXA && EXPERIMENTAL
drivers/spi/Makefile +1 -0
index 7fca043..2d90a56 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_SPI_AU1550)		+= au1550_spi.o
 obj-$(CONFIG_SPI_BUTTERFLY)		+= spi_butterfly.o
 obj-$(CONFIG_SPI_IMX)			+= spi_imx.o
 obj-$(CONFIG_SPI_LM70_LLP)		+= spi_lm70llp.o
+obj-$(CONFIG_SPI_ORION)			+= orion_spi.o
 obj-$(CONFIG_SPI_PXA2XX)		+= pxa2xx_spi.o
 obj-$(CONFIG_SPI_OMAP_UWIRE)		+= omap_uwire.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
drivers/spi/orion_spi.c new file +545 -0

545 lines not on this page · raw message

include/linux/spi/orion_spi.h new file +17 -0
new file mode 100644
index 0000000..0781855
@@ -0,0 +1,17 @@
+/*
+ * include/linux/spi/orion_spi.h
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#ifndef __LINUX_SPI_ORION_SPI_H
+#define __LINUX_SPI_ORION_SPI_H
+
+struct orion_spi_info {
+	u32	tclk;
+};
+
+
+#endif
Signature · 1 line
1.5.2.5

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

_______________________________________________
spi-devel-general mailing list spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/spi-devel-general