Skip to contentSkip to conversation

spi: Add PPC4xx SPI driver

PATCH Superseded Stefan Roese 7 participants 15 messages Nov 25, 2008 – Jan 6, 2009 5 messages not in the archive SPI

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 spi: Add PPC4xx SPI driver

This adds a SPI driver for the SPI controller found in the IBM/AMCC 4xx PowerPC's.

---
Changes in v5:
- Don't call setupxfer() from setup() so that the baudrate etc won't get changed while another transfer is active, as suggested by David Brownell.
- module_{init,exit} moved directly to the functions to which they apply.
- Use __func__ instead of __FUNCTION__.

Changes in v4:
- Added fixes suggested by Josh Boyer
- Changed compatible property from "ibm,spi" to "ibm,ppc4xx-spi"

Changes in v3:
- When the device is removed the GPIOs are released. The memory for the GPIO array is freed.

Changes in v2:
- Now the gpios property is correctly decoded and the resulting gpio numbers are used as the devices chip selects.

 drivers/spi/Kconfig      |    7 +
 drivers/spi/Makefile     |    1 +
 drivers/spi/spi_ppc4xx.c |  597 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 605 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/spi_ppc4xx.c
drivers/spi/Kconfig +7 -0
index b9d0efb..69d5fee 100644
@@ -155,6 +155,13 @@ config SPI_ORION
 	help
 	  This enables using the SPI master controller on the Orion chips.
 
+config SPI_PPC4xx
+	tristate "PPC4xx SPI Controller"
+	depends on 4xx && SPI_MASTER
+	select SPI_BITBANG
+	help
+	  This selects a driver for the PPC4xx SPI Controller.
+
 config SPI_PXA2XX
 	tristate "PXA2xx SSP SPI master"
 	depends on ARCH_PXA && EXPERIMENTAL
drivers/spi/Makefile +1 -0
index ccf18de..a2e5816 100644
@@ -24,6 +24,7 @@ obj-$(CONFIG_SPI_OMAP24XX)		+= omap2_mcspi.o
 obj-$(CONFIG_SPI_ORION)			+= orion_spi.o
 obj-$(CONFIG_SPI_MPC52xx_PSC)		+= mpc52xx_psc_spi.o
 obj-$(CONFIG_SPI_MPC83xx)		+= spi_mpc83xx.o
+obj-$(CONFIG_SPI_PPC4xx)		+= spi_ppc4xx.o
 obj-$(CONFIG_SPI_S3C24XX_GPIO)		+= spi_s3c24xx_gpio.o
 obj-$(CONFIG_SPI_S3C24XX)		+= spi_s3c24xx.o
 obj-$(CONFIG_SPI_TXX9)			+= spi_txx9.o
drivers/spi/spi_ppc4xx.c new file +597 -0

597 lines not on this page · raw message

Signature · 8 lines
1.6.0.4


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
#1 lore raw
Quoted 7 lines · Stefan Roese

This adds a SPI driver for the SPI controller found in the IBM/AMCC 4xx PowerPC's.

---

I have a question as to how to use this driver. of_num_gpios() starts testing for gpio's at num = 0, and stops at the first invalid one. However, gpio numbers are apparently allocated dynamically from 255 down, meaning that there probably is no gpio-0.

For example, on my Sequoia board I have gpiochip176, gpiochip192, and gpiochip224. So, of_num_gpios() returns zero, even though there are 72 gpio's on my board.

This gets back to an earlier discussion about setting the gpio index of each controller, which was rejected, IIRC. If we could set the base gpio of each chip, we could start at zero and use consecutive numbers. Failing that, it seems that Stefan's SPI driver needs to probe the entire 0-255 gpio space.

How is this intended to work? An example .dts would be greatly appreciated.

Steve

#2 lore raw
Quoted 11 lines · Steven A. Falco
Quoted 7 lines · Stefan Roese

This adds a SPI driver for the SPI controller found in the IBM/AMCC 4xx PowerPC's.

---

How is this intended to work? An example .dts would be greatly appreciated.

Answered my own question. The gpios must be directly under the spi node rather than elsewhere in the tree. This works:

			SPI0: spi@ef600900 {
				compatible = "ibm,ppc4xx-spi";
				reg = <ef600900 7>;
				interrupts = <8 4>;
				interrupt-parent = <&UIC0>;

				gpios = <&GPIO1 14 0>;
			};

A message that is not in the archive (sent off-list or not yet received): 1227628088-10849-1-git-send-email-sr-ynQEQJNshbs@public.gmane.org

#4 lore raw
Quoted 4 lines · Stefan Roese

Changes in v5:
- Don't call setupxfer() from setup() so that the baudrate etc won't get changed while another transfer is active, as suggested by David Brownell.

Better, but this still doesn't seem quite right:

Quoted 5 lines
drivers/spi/spi_ppc4xx.c new file +5 -0
+static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t)
+{
+	struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	unsigned char cdm = 0;

drivers/spi/spi_ppc4xx.c line 157

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+	int scr;
+	u8 bpw;
+
+	/* Write new configration */
+	out_8(&hw->regs->mode, cs->mode);
+
+	/*
+	 * Allow platform reduce the interrupt load on the CPU during SPI
+	 * transfers. We do not target maximum performance, but rather allow
+	 * platform to limit SPI bus frequency and interrupt rate.

That comment doesn't seem even vaguely related to the code it allegedly refers to ... nothing in this routine affects the IRQ load.

Quoted 36 lines
drivers/spi/spi_ppc4xx.c new file +36 -0
+	 */
+	bpw = t ? t->bits_per_word : spi->bits_per_word;
+	cs->speed_hz = t ? min(t->speed_hz, spi->max_speed_hz) :
+		spi->max_speed_hz;
+
+	if (bpw != 8) {
+		dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
+		return -EINVAL;
+	}
+
+	if (cs->speed_hz == 0) {
+		dev_err(&spi->dev, "invalid speed_hz (must be non-zero)\n");
+		return -EINVAL;
+	}
+
+	/* set the clock */
+	/* opb_freq was already divided by 4 */
+	scr = (hw->opb_freq / cs->speed_hz) - 1;
+
+	if (scr > 0)
+		cdm = min(scr, 0xff);
+
+	dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm,
+		cs->speed_hz);
+
+	if (in_8(&hw->regs->cdm) != cdm)
+		out_8(&hw->regs->cdm, cdm);
+
+	spin_lock(&hw->bitbang.lock);
+	if (!hw->bitbang.busy) {
+		hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
+		/* need to ndelay here? */
+	}
+	spin_unlock(&hw->bitbang.lock);
+
+	return 0;

drivers/spi/spi_ppc4xx.c line 203

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+}
+
+static int spi_ppc4xx_setup(struct spi_device *spi)
+{
+	int ret;
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	int init = 0;
+
+	if (!spi->bits_per_word)
+		spi->bits_per_word = 8;

Given the above restrictions, it'd be better to

	if (spi->bits_per_word != 8)
		return -EINVAL;

On the general policy of reporting such errors as near as practical to the place they appear ... otherwise it gets hard to track them down, since the faults get reported a long time later, well after the point drivers expect to see such reports. Likewise with spi->max_speed_hz.

Quoted 34 lines
drivers/spi/spi_ppc4xx.c new file +34 -0
+
+	if (spi->mode & ~MODEBITS) {
+		dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
+			spi->mode & ~MODEBITS);
+		return -EINVAL;
+	}
+
+	if (cs == NULL) {
+		cs = kzalloc(sizeof *cs, GFP_KERNEL);
+		if (!cs)
+			return -ENOMEM;
+		spi->controller_state = cs;
+
+		/*
+		 * First time called, so let's init the SPI controller
+		 * at the end of this function
+		 */
+		init = 1;
+	}
+
+	/*
+	 * We set all bits of the SPI0_MODE register, so,
+	 * no need to read-modify-write
+	 */
+	cs->mode = SPI_PPC4XX_MODE_SPE;
+
+	switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
+	case SPI_MODE_0:
+		cs->mode |= SPI_CLK_MODE0;
+		break;
+	case SPI_MODE_1:
+		cs->mode |= SPI_CLK_MODE1;
+		break;
+	case SPI_MODE_2:

drivers/spi/spi_ppc4xx.c line 247

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+		cs->mode |= SPI_CLK_MODE2;
+		break;
+	case SPI_MODE_3:
+		cs->mode |= SPI_CLK_MODE3;
+		break;
+	}
+
+	if (spi->mode & SPI_LSB_FIRST) {
+		/* this assumes that bit 7 is the LSb! */
+		cs->mode |= SPI_PPC4XX_MODE_RD;

The Linux bit numbering convention is that BIT(0) is the LSB, so that comment is nonsensical. BIT(7) will always bee the MSB of an 8-bit byte.

If the issue is a PPC convention that BIT(0) is the MSB, then please adjust comments accordingly. Or better, just strike the comment ... bit numbering is irrelevant here, the only requirement is that LSB_FIRST causes the LSB to be sent first, instead of the MSB.

drivers/spi/spi_ppc4xx.c line 256

Quoted 9 lines
drivers/spi/spi_ppc4xx.c new file +9 -0
+	}
+
+	/*
+	 * New configuration (mode, speed etc) will be written to the
+	 * controller in spi_ppc4xx_setupxfer(). Only call
+	 * spi_ppc4xx_setupxfer() directly upon first initialization.
+	 */
+	if (init) {
+		ret = spi_ppc4xx_setupxfer(spi, NULL);

Here it is, calling setupxfer()... despite one of the goals of the v5 patch being to *not* do that from spi_setup()!

I suspect what you must intend here is to just force the slave to be deselected. If so, then just call your

   hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);

directly, instead of letting setupxfer() trash register state that may be controlling some active transfer.

drivers/spi/spi_ppc4xx.c line 268

Quoted 12 lines
drivers/spi/spi_ppc4xx.c new file +12 -0
+		if (ret < 0) {
+			dev_err(&spi->dev, "setupxfer returned %d\n", ret);
+			return ret;
+		}
+	}
+
+	dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n",
+		__func__, spi->mode, spi->bits_per_word,
+		spi->max_speed_hz);
+
+	return 0;
+}

------------------------------------------------------------------------------

A message that is not in the archive (sent off-list or not yet received): 200812271130.12513.david-b-yBeKhBN/[email protected]

#6 lore raw
Quoted 7 lines · David Brownell
Quoted 4 lines · Stefan Roese

Changes in v5:
- Don't call setupxfer() from setup() so that the baudrate etc won't get changed while another transfer is active, as suggested by David Brownell.

Better, but this still doesn't seem quite right:

David, thanks for the review. Please find some comments below.

Quoted 11 lines
Quoted 11 lines

+static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t) +{

drivers/spi/spi_ppc4xx.c new file +9 -0
+	struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	unsigned char cdm = 0;
+	int scr;
+	u8 bpw;
+
+	/* Write new configration */
+	out_8(&hw->regs->mode, cs->mode);
+
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 157

Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	/*
+	 * Allow platform reduce the interrupt load on the CPU during SPI
+	 * transfers. We do not target maximum performance, but rather allow
+	 * platform to limit SPI bus frequency and interrupt rate.

That comment doesn't seem even vaguely related to the code it allegedly refers to ... nothing in this routine affects the IRQ load.

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

Quoted 49 lines
Quoted 36 lines
drivers/spi/spi_ppc4xx.c new file +36 -0
+	 */
+	bpw = t ? t->bits_per_word : spi->bits_per_word;
+	cs->speed_hz = t ? min(t->speed_hz, spi->max_speed_hz) :
+		spi->max_speed_hz;
+
+	if (bpw != 8) {
+		dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
+		return -EINVAL;
+	}
+
+	if (cs->speed_hz == 0) {
+		dev_err(&spi->dev, "invalid speed_hz (must be non-zero)\n");
+		return -EINVAL;
+	}
+
+	/* set the clock */
+	/* opb_freq was already divided by 4 */
+	scr = (hw->opb_freq / cs->speed_hz) - 1;
+
+	if (scr > 0)
+		cdm = min(scr, 0xff);
+
+	dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm,
+		cs->speed_hz);
+
+	if (in_8(&hw->regs->cdm) != cdm)
+		out_8(&hw->regs->cdm, cdm);
+
+	spin_lock(&hw->bitbang.lock);
+	if (!hw->bitbang.busy) {
+		hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
+		/* need to ndelay here? */
+	}
+	spin_unlock(&hw->bitbang.lock);
+
+	return 0;

drivers/spi/spi_ppc4xx.c line 203

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+}
+
+static int spi_ppc4xx_setup(struct spi_device *spi)
+{
+	int ret;
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	int init = 0;
+
+	if (!spi->bits_per_word)
+		spi->bits_per_word = 8;

Given the above restrictions, it'd be better to

Quoted 8 lines
	if (spi->bits_per_word != 8)
		return -EINVAL;

On the general policy of reporting such errors as near as practical to the place they appear ... otherwise it gets hard to track them down, since the faults get reported a long time later, well after the point drivers expect to see such reports.

OK, point gotten. Will change in next version.

Quoted 1 line

Likewise with spi->max_speed_hz.

OK.

Quoted 49 lines
Quoted 34 lines
drivers/spi/spi_ppc4xx.c new file +34 -0
+
+	if (spi->mode & ~MODEBITS) {
+		dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
+			spi->mode & ~MODEBITS);
+		return -EINVAL;
+	}
+
+	if (cs == NULL) {
+		cs = kzalloc(sizeof *cs, GFP_KERNEL);
+		if (!cs)
+			return -ENOMEM;
+		spi->controller_state = cs;
+
+		/*
+		 * First time called, so let's init the SPI controller
+		 * at the end of this function
+		 */
+		init = 1;
+	}
+
+	/*
+	 * We set all bits of the SPI0_MODE register, so,
+	 * no need to read-modify-write
+	 */
+	cs->mode = SPI_PPC4XX_MODE_SPE;
+
+	switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
+	case SPI_MODE_0:
+		cs->mode |= SPI_CLK_MODE0;
+		break;
+	case SPI_MODE_1:
+		cs->mode |= SPI_CLK_MODE1;
+		break;
+	case SPI_MODE_2:

drivers/spi/spi_ppc4xx.c line 247

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+		cs->mode |= SPI_CLK_MODE2;
+		break;
+	case SPI_MODE_3:
+		cs->mode |= SPI_CLK_MODE3;
+		break;
+	}
+
+	if (spi->mode & SPI_LSB_FIRST) {
+		/* this assumes that bit 7 is the LSb! */
+		cs->mode |= SPI_PPC4XX_MODE_RD;

The Linux bit numbering convention is that BIT(0) is the LSB, so that comment is nonsensical. BIT(7) will always bee the MSB of an 8-bit byte.

Quoted 5 lines

If the issue is a PPC convention that BIT(0) is the MSB, then please adjust comments accordingly. Or better, just strike the comment ... bit numbering is irrelevant here, the only requirement is that LSB_FIRST causes the LSB to be sent first, instead of the MSB.

OK, comment removed.

Quoted 4 lines
Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	}
+
+	/*
+	 * New configuration (mode, speed etc) will be written to the
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 256

Quoted 5 lines
drivers/spi/spi_ppc4xx.c new file +5 -0
+	 * controller in spi_ppc4xx_setupxfer(). Only call
+	 * spi_ppc4xx_setupxfer() directly upon first initialization.
+	 */
+	if (init) {
+		ret = spi_ppc4xx_setupxfer(spi, NULL);

Here it is, calling setupxfer()... despite one of the goals of the v5 patch being to *not* do that from spi_setup()!

It's only called upon first driver initialization (as the comment above explains).

Quoted 6 lines

I suspect what you must intend here is to just force the slave to be deselected. If so, then just call your

   hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);

directly,

No, this doesn't work. I just tried it in my next driver version. Somehow the communication doesn't start up when setupxfer is not called at least once upon initialization. Sorry, but I worked on this driver quite some time ago and don't remember the details now.

Quoted 2 lines

instead of letting setupxfer() trash register state that may be controlling some active transfer.

As this call is done only upon first driver initialization, no active transfer can be interrupted. Or did I miss something here?

Please let me know if this approach is acceptable for now, or if I need to dig into this deeper. The problem is that I don't have the time for this now so further patch versions would come (much) later.

Otherwise I will send a new patch version with the fixes/changes mentioned above tomorrow.

Thanks.

Best regards,
Stefan

------------------------------------------------------------------------------

#7 lore raw
In reply to David Brownell
Quoted 7 lines · David Brownell
Quoted 4 lines · Stefan Roese

Changes in v5:
- Don't call setupxfer() from setup() so that the baudrate etc won't get changed while another transfer is active, as suggested by David Brownell.

Better, but this still doesn't seem quite right:

David, thanks for the review. Please find some comments below.

Quoted 11 lines
Quoted 11 lines

+static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t) +{

drivers/spi/spi_ppc4xx.c new file +9 -0
+	struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	unsigned char cdm = 0;
+	int scr;
+	u8 bpw;
+
+	/* Write new configration */
+	out_8(&hw->regs->mode, cs->mode);
+
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 157

Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	/*
+	 * Allow platform reduce the interrupt load on the CPU during SPI
+	 * transfers. We do not target maximum performance, but rather allow
+	 * platform to limit SPI bus frequency and interrupt rate.

That comment doesn't seem even vaguely related to the code it allegedly refers to ... nothing in this routine affects the IRQ load.

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

Quoted 49 lines
Quoted 36 lines
drivers/spi/spi_ppc4xx.c new file +36 -0
+	 */
+	bpw = t ? t->bits_per_word : spi->bits_per_word;
+	cs->speed_hz = t ? min(t->speed_hz, spi->max_speed_hz) :
+		spi->max_speed_hz;
+
+	if (bpw != 8) {
+		dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
+		return -EINVAL;
+	}
+
+	if (cs->speed_hz == 0) {
+		dev_err(&spi->dev, "invalid speed_hz (must be non-zero)\n");
+		return -EINVAL;
+	}
+
+	/* set the clock */
+	/* opb_freq was already divided by 4 */
+	scr = (hw->opb_freq / cs->speed_hz) - 1;
+
+	if (scr > 0)
+		cdm = min(scr, 0xff);
+
+	dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm,
+		cs->speed_hz);
+
+	if (in_8(&hw->regs->cdm) != cdm)
+		out_8(&hw->regs->cdm, cdm);
+
+	spin_lock(&hw->bitbang.lock);
+	if (!hw->bitbang.busy) {
+		hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
+		/* need to ndelay here? */
+	}
+	spin_unlock(&hw->bitbang.lock);
+
+	return 0;

drivers/spi/spi_ppc4xx.c line 203

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+}
+
+static int spi_ppc4xx_setup(struct spi_device *spi)
+{
+	int ret;
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	int init = 0;
+
+	if (!spi->bits_per_word)
+		spi->bits_per_word = 8;

Given the above restrictions, it'd be better to

Quoted 8 lines
	if (spi->bits_per_word != 8)
		return -EINVAL;

On the general policy of reporting such errors as near as practical to the place they appear ... otherwise it gets hard to track them down, since the faults get reported a long time later, well after the point drivers expect to see such reports.

OK, point gotten. Will change in next version.

Quoted 1 line

Likewise with spi->max_speed_hz.

OK.

Quoted 49 lines
Quoted 34 lines
drivers/spi/spi_ppc4xx.c new file +34 -0
+
+	if (spi->mode & ~MODEBITS) {
+		dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
+			spi->mode & ~MODEBITS);
+		return -EINVAL;
+	}
+
+	if (cs == NULL) {
+		cs = kzalloc(sizeof *cs, GFP_KERNEL);
+		if (!cs)
+			return -ENOMEM;
+		spi->controller_state = cs;
+
+		/*
+		 * First time called, so let's init the SPI controller
+		 * at the end of this function
+		 */
+		init = 1;
+	}
+
+	/*
+	 * We set all bits of the SPI0_MODE register, so,
+	 * no need to read-modify-write
+	 */
+	cs->mode = SPI_PPC4XX_MODE_SPE;
+
+	switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
+	case SPI_MODE_0:
+		cs->mode |= SPI_CLK_MODE0;
+		break;
+	case SPI_MODE_1:
+		cs->mode |= SPI_CLK_MODE1;
+		break;
+	case SPI_MODE_2:

drivers/spi/spi_ppc4xx.c line 247

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+		cs->mode |= SPI_CLK_MODE2;
+		break;
+	case SPI_MODE_3:
+		cs->mode |= SPI_CLK_MODE3;
+		break;
+	}
+
+	if (spi->mode & SPI_LSB_FIRST) {
+		/* this assumes that bit 7 is the LSb! */
+		cs->mode |= SPI_PPC4XX_MODE_RD;

The Linux bit numbering convention is that BIT(0) is the LSB, so that comment is nonsensical. BIT(7) will always bee the MSB of an 8-bit byte.

Quoted 5 lines

If the issue is a PPC convention that BIT(0) is the MSB, then please adjust comments accordingly. Or better, just strike the comment ... bit numbering is irrelevant here, the only requirement is that LSB_FIRST causes the LSB to be sent first, instead of the MSB.

OK, comment removed.

Quoted 4 lines
Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	}
+
+	/*
+	 * New configuration (mode, speed etc) will be written to the
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 256

Quoted 5 lines
drivers/spi/spi_ppc4xx.c new file +5 -0
+	 * controller in spi_ppc4xx_setupxfer(). Only call
+	 * spi_ppc4xx_setupxfer() directly upon first initialization.
+	 */
+	if (init) {
+		ret = spi_ppc4xx_setupxfer(spi, NULL);

Here it is, calling setupxfer()... despite one of the goals of the v5 patch being to *not* do that from spi_setup()!

It's only called upon first driver initialization (as the comment above explains).

Quoted 6 lines

I suspect what you must intend here is to just force the slave to be deselected. If so, then just call your

   hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);

directly,

No, this doesn't work. I just tried it in my next driver version. Somehow the communication doesn't start up when setupxfer is not called at least once upon initialization. Sorry, but I worked on this driver quite some time ago and don't remember the details now.

Quoted 2 lines

instead of letting setupxfer() trash register state that may be controlling some active transfer.

As this call is done only upon first driver initialization, no active transfer can be interrupted. Or did I miss something here?

Please let me know if this approach is acceptable for now, or if I need to dig into this deeper. The problem is that I don't have the time for this now so further patch versions would come (much) later.

Otherwise I will send a new patch version with the fixes/changes mentioned above tomorrow.

Thanks.

Best regards,
Stefan

#8 lore raw
In reply to David Brownell
Quoted 7 lines · David Brownell
Quoted 4 lines · Stefan Roese

Changes in v5:
- Don't call setupxfer() from setup() so that the baudrate etc won't get changed while another transfer is active, as suggested by David Brownell.

Better, but this still doesn't seem quite right:

David, thanks for the review. Please find some comments below.

Quoted 11 lines
Quoted 11 lines

+static int spi_ppc4xx_setupxfer(struct spi_device *spi, struct spi_transfer *t) +{

drivers/spi/spi_ppc4xx.c new file +9 -0
+	struct ppc4xx_spi *hw = spi_master_get_devdata(spi->master);
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	unsigned char cdm = 0;
+	int scr;
+	u8 bpw;
+
+	/* Write new configration */
+	out_8(&hw->regs->mode, cs->mode);
+
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 157

Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	/*
+	 * Allow platform reduce the interrupt load on the CPU during SPI
+	 * transfers. We do not target maximum performance, but rather allow
+	 * platform to limit SPI bus frequency and interrupt rate.

That comment doesn't seem even vaguely related to the code it allegedly refers to ... nothing in this routine affects the IRQ load.

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

Quoted 49 lines
Quoted 36 lines
drivers/spi/spi_ppc4xx.c new file +36 -0
+	 */
+	bpw = t ? t->bits_per_word : spi->bits_per_word;
+	cs->speed_hz = t ? min(t->speed_hz, spi->max_speed_hz) :
+		spi->max_speed_hz;
+
+	if (bpw != 8) {
+		dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
+		return -EINVAL;
+	}
+
+	if (cs->speed_hz == 0) {
+		dev_err(&spi->dev, "invalid speed_hz (must be non-zero)\n");
+		return -EINVAL;
+	}
+
+	/* set the clock */
+	/* opb_freq was already divided by 4 */
+	scr = (hw->opb_freq / cs->speed_hz) - 1;
+
+	if (scr > 0)
+		cdm = min(scr, 0xff);
+
+	dev_dbg(&spi->dev, "setting pre-scaler to %d (hz %d)\n", cdm,
+		cs->speed_hz);
+
+	if (in_8(&hw->regs->cdm) != cdm)
+		out_8(&hw->regs->cdm, cdm);
+
+	spin_lock(&hw->bitbang.lock);
+	if (!hw->bitbang.busy) {
+		hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
+		/* need to ndelay here? */
+	}
+	spin_unlock(&hw->bitbang.lock);
+
+	return 0;

drivers/spi/spi_ppc4xx.c line 203

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+}
+
+static int spi_ppc4xx_setup(struct spi_device *spi)
+{
+	int ret;
+	struct spi_ppc4xx_cs *cs = spi->controller_state;
+	int init = 0;
+
+	if (!spi->bits_per_word)
+		spi->bits_per_word = 8;

Given the above restrictions, it'd be better to

Quoted 8 lines
	if (spi->bits_per_word != 8)
		return -EINVAL;

On the general policy of reporting such errors as near as practical to the place they appear ... otherwise it gets hard to track them down, since the faults get reported a long time later, well after the point drivers expect to see such reports.

OK, point gotten. Will change in next version.

Quoted 1 line

Likewise with spi->max_speed_hz.

OK.

Quoted 49 lines
Quoted 34 lines
drivers/spi/spi_ppc4xx.c new file +34 -0
+
+	if (spi->mode & ~MODEBITS) {
+		dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
+			spi->mode & ~MODEBITS);
+		return -EINVAL;
+	}
+
+	if (cs == NULL) {
+		cs = kzalloc(sizeof *cs, GFP_KERNEL);
+		if (!cs)
+			return -ENOMEM;
+		spi->controller_state = cs;
+
+		/*
+		 * First time called, so let's init the SPI controller
+		 * at the end of this function
+		 */
+		init = 1;
+	}
+
+	/*
+	 * We set all bits of the SPI0_MODE register, so,
+	 * no need to read-modify-write
+	 */
+	cs->mode = SPI_PPC4XX_MODE_SPE;
+
+	switch (spi->mode & (SPI_CPHA | SPI_CPOL)) {
+	case SPI_MODE_0:
+		cs->mode |= SPI_CLK_MODE0;
+		break;
+	case SPI_MODE_1:
+		cs->mode |= SPI_CLK_MODE1;
+		break;
+	case SPI_MODE_2:

drivers/spi/spi_ppc4xx.c line 247

Quoted 10 lines
drivers/spi/spi_ppc4xx.c new file +10 -0
+		cs->mode |= SPI_CLK_MODE2;
+		break;
+	case SPI_MODE_3:
+		cs->mode |= SPI_CLK_MODE3;
+		break;
+	}
+
+	if (spi->mode & SPI_LSB_FIRST) {
+		/* this assumes that bit 7 is the LSb! */
+		cs->mode |= SPI_PPC4XX_MODE_RD;

The Linux bit numbering convention is that BIT(0) is the LSB, so that comment is nonsensical. BIT(7) will always bee the MSB of an 8-bit byte.

Quoted 5 lines

If the issue is a PPC convention that BIT(0) is the MSB, then please adjust comments accordingly. Or better, just strike the comment ... bit numbering is irrelevant here, the only requirement is that LSB_FIRST causes the LSB to be sent first, instead of the MSB.

OK, comment removed.

Quoted 4 lines
Quoted 4 lines
drivers/spi/spi_ppc4xx.c new file +4 -0
+	}
+
+	/*
+	 * New configuration (mode, speed etc) will be written to the
Quoted 8 lines

drivers/spi/spi_ppc4xx.c line 256

Quoted 5 lines
drivers/spi/spi_ppc4xx.c new file +5 -0
+	 * controller in spi_ppc4xx_setupxfer(). Only call
+	 * spi_ppc4xx_setupxfer() directly upon first initialization.
+	 */
+	if (init) {
+		ret = spi_ppc4xx_setupxfer(spi, NULL);

Here it is, calling setupxfer()... despite one of the goals of the v5 patch being to *not* do that from spi_setup()!

It's only called upon first driver initialization (as the comment above explains).

Quoted 6 lines

I suspect what you must intend here is to just force the slave to be deselected. If so, then just call your

   hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);

directly,

No, this doesn't work. I just tried it in my next driver version. Somehow the communication doesn't start up when setupxfer is not called at least once upon initialization. Sorry, but I worked on this driver quite some time ago and don't remember the details now.

Quoted 2 lines

instead of letting setupxfer() trash register state that may be controlling some active transfer.

As this call is done only upon first driver initialization, no active transfer can be interrupted. Or did I miss something here?

Please let me know if this approach is acceptable for now, or if I need to dig into this deeper. The problem is that I don't have the time for this now so further patch versions would come (much) later.

Otherwise I will send a new patch version with the fixes/changes mentioned above tomorrow.

Thanks.

Best regards,
Stefan

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Dear Stefan,

Quoted 13 lines · In message <[email protected]> you wrote:
Quoted 12 lines

drivers/spi/spi_ppc4xx.c line 157

Quoted 8 lines
drivers/spi/spi_ppc4xx.c new file +7 -0
+	/* Write new configration */
+	out_8(&hw->regs->mode, cs->mode);
+
+	/*
+	 * Allow platform reduce the interrupt load on the CPU during SPI
+	 * transfers. We do not target maximum performance, but rather allow
+	 * platform to limit SPI bus frequency and interrupt rate.

That comment doesn't seem even vaguely related to the code it allegedly refers to ... nothing in this routine affects the IRQ load.

Quoted 4 lines

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

On PPC4xx systems we get one interrupt per transferred byte, i. e. there is a direct relation between SPI bus frequency and interrupt rate.

Since this is a PPC4xx SPI driver, the comment is actually helpful to understand the operation of the driver especially in relation to the limitations of the hardware.

Instead of removing the comment (which leaves the casual reader clueless) it would probably better to add a sentence explaining the one byte / one interrupt situation?

Best regards,

Wolfgang Denk

Signature · 8 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: [email protected]
...when fits of creativity run strong, more than  one  programmer  or
writer  has  been  known to abandon the desktop for the more spacious
floor.                                             - Fred Brooks, Jr.

------------------------------------------------------------------------------

A message that is not in the archive (sent off-list or not yet received): 20090105193117.E5AD98387CDB-C2Gvrrd9BC/j/ljBK/[email protected]

#12 lore raw

Hi Wolfgang,

Quoted 9 lines · Wolfgang Denk
Quoted 4 lines

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

On PPC4xx systems we get one interrupt per transferred byte, i. e. there is a direct relation between SPI bus frequency and interrupt rate.

Quoted 7 lines

Since this is a PPC4xx SPI driver, the comment is actually helpful to understand the operation of the driver especially in relation to the limitations of the hardware.

Instead of removing the comment (which leaves the casual reader clueless) it would probably better to add a sentence explaining the one byte / one interrupt situation?

OK, I'll this explanation instead.

Thanks.

Best regards,
Stefan

------------------------------------------------------------------------------

#13 lore raw
In reply to Wolfgang Denk

Hi Wolfgang,

Quoted 9 lines · Wolfgang Denk
Quoted 4 lines

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

On PPC4xx systems we get one interrupt per transferred byte, i. e. there is a direct relation between SPI bus frequency and interrupt rate.

Quoted 7 lines

Since this is a PPC4xx SPI driver, the comment is actually helpful to understand the operation of the driver especially in relation to the limitations of the hardware.

Instead of removing the comment (which leaves the casual reader clueless) it would probably better to add a sentence explaining the one byte / one interrupt situation?

OK, I'll this explanation instead.

Thanks.

Best regards,
Stefan

#14 lore raw
In reply to Wolfgang Denk

Hi Wolfgang,

Quoted 9 lines · Wolfgang Denk
Quoted 4 lines

IIRC (I didn't write the original version of this driver) then this comments simply refers to the fact that the platform can select a maximum SPI frequency via spi->max_speed_hz which is pretty obvious. So I'll just remove this comment in the next version.

On PPC4xx systems we get one interrupt per transferred byte, i. e. there is a direct relation between SPI bus frequency and interrupt rate.

Quoted 7 lines

Since this is a PPC4xx SPI driver, the comment is actually helpful to understand the operation of the driver especially in relation to the limitations of the hardware.

Instead of removing the comment (which leaves the casual reader clueless) it would probably better to add a sentence explaining the one byte / one interrupt situation?

OK, I'll this explanation instead.

Thanks.

Best regards,
Stefan

#15 lore raw
In reply to a message not in the archive

Also:

Quoted 3 lines

+static struct of_platform_driver spi_ppc4xx_of_driver = { +       .owner = THIS_MODULE,
+       .name = DRIVER_NAME,

I'd hope the PPC folk eliminate this duplication soonish. Those fields are obvious duplicates of the driver model fields...

Quoted 8 lines

+       .match_table = spi_ppc4xx_of_match,
+       .probe = spi_ppc4xx_of_probe,
+       .remove = __exit_p(spi_ppc4xx_of_remove),
+       .driver = {
+               .name = DRIVER_NAME,
+               .owner = THIS_MODULE,
+       },
+};

------------------------------------------------------------------------------

#16 lore raw

Hi David, Stefan,

Quoted 10 lines · On Sat, 27 Dec 2008 11:32:25 -0800 David Brownell <[email protected]> wrote:

Also:

Quoted 3 lines

+static struct of_platform_driver spi_ppc4xx_of_driver = { +       .owner = THIS_MODULE,
+       .name = DRIVER_NAME,

I'd hope the PPC folk eliminate this duplication soonish. Those fields are obvious duplicates of the driver model fields...

We are on the path (have been for some time). Only the driver model versions should be filled in for new drivers. For old drivers we copy the values into the driver model fields if they are not already set. We intend to remove the of_platform_drivers versions of "name" and "owner" soon.

Quoted 8 lines
Quoted 8 lines

+       .match_table = spi_ppc4xx_of_match,
+       .probe = spi_ppc4xx_of_probe,
+       .remove = __exit_p(spi_ppc4xx_of_remove),
+       .driver = {
+               .name = DRIVER_NAME,
+               .owner = THIS_MODULE,
+       },
+};

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://ozlabs.org/mailman/listinfo/linuxppc-dev

#17 lore raw
In reply to a message not in the archive

Hi Stefan,

Quoted 7 lines · Stefan Roese

This adds a SPI driver for the SPI controller found in the IBM/AMCC 4xx PowerPC's.

---

[...]

drivers/spi/spi_ppc4xx.c line 373

Quoted 8 lines
drivers/spi/spi_ppc4xx.c new file +8 -0
+static unsigned int of_num_gpios(struct device_node *node)
+{
+	unsigned int num = 0;
+
+	while (gpio_is_valid(of_get_gpio(node, num)))
+		num++;
+	return num;
+}

FYI, as of commit 749820928a2fd47ff536773d869d2c3f8038b7d1 ("of/gpio:
Implement of_gpio_count()") you can use of_gpio_count() from linux/of_gpio.h.

Signature · 5 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2

------------------------------------------------------------------------------

A message that is not in the archive (sent off-list or not yet received): 20090106020603.GA14074-wnGakbxT3iijyJ0x5qLZdcN33GVbZNy3@public.gmane.org

#19 lore raw

Hi Anton,

Quoted 10 lines · Anton Vorontsov
Quoted 7 lines

This adds a SPI driver for the SPI controller found in the IBM/AMCC 4xx PowerPC's.

---

[...]

Quoted 12 lines

drivers/spi/spi_ppc4xx.c line 373

Quoted 8 lines
drivers/spi/spi_ppc4xx.c new file +8 -0
+static unsigned int of_num_gpios(struct device_node *node)
+{
+	unsigned int num = 0;
+
+	while (gpio_is_valid(of_get_gpio(node, num)))
+		num++;
+	return num;
+}

FYI, as of commit 749820928a2fd47ff536773d869d2c3f8038b7d1 ("of/gpio:
Implement of_gpio_count()") you can use of_gpio_count() from linux/of_gpio.h.

Thanks for the hint. I'll use it in the next version.

Best regards,
Stefan

------------------------------------------------------------------------------