Patchwork spi_mpc8xxx.c: chip select polarity problem

login
register
mail settings
Submitter Torsten Fleischer
Date Nov. 25, 2009, 8:41 p.m.
Message ID <200911252141.59549.to-fleischer@t-online.de>
Download mbox | patch
Permalink /patch/39462/
State Superseded
Delegated to: Kumar Gala
Headers show

Comments

Torsten Fleischer - Nov. 25, 2009, 8:41 p.m.
On Wen, Nov 25, 2009 at 01:33:57 Grant Likely wrote:
> Thanks.  However, there needs to be a proper description of what this
> patch does to go in the commit header.  Can you please write one?
> 
> Thanks,
> g.
> 
[...]

The initialization of the chip selects is removed from the probe() function of 
the spi_mpc8xxx driver, because the driver doesn't know the polarity of the 
chip selects of the SPI devices at the time of its initialization.

For this reason the initialization of the several chip selects is postponed 
to the point of time when the very first SPI transfer to the associated device
occurs.


Signed-off-by: Torsten Fleischer <to-fleischer@t-online.de>
---
Grant Likely - Nov. 25, 2009, 10:11 p.m.
On Wed, Nov 25, 2009 at 1:41 PM, Torsten Fleischer
<to-fleischer@t-online.de> wrote:
> On Wen, Nov 25, 2009 at 01:33:57 Grant Likely wrote:
>> Thanks.  However, there needs to be a proper description of what this
>> patch does to go in the commit header.  Can you please write one?
>>
>> Thanks,
>> g.
>>
> [...]
>
> The initialization of the chip selects is removed from the probe() function of
> the spi_mpc8xxx driver, because the driver doesn't know the polarity of the
> chip selects of the SPI devices at the time of its initialization.
>
> For this reason the initialization of the several chip selects is postponed
> to the point of time when the very first SPI transfer to the associated device
> occurs.
>
>
> Signed-off-by: Torsten Fleischer <to-fleischer@t-online.de>

Ah.  I understand what you're doing now.   Hmmm.  This approach
concerns me because it relies on firmware or platform code to get CS
gpios set up properly before the driver is probed.  Firmware doesn't
always get it right, and I prefer to avoid platform specific setups as
much as possible.  Why can't the CS polarity be encoded into the
device tree so the driver *does* have the polarity data at probe time?

g.

> ---
>
> diff -u -r -N linux-2.6.31.6_orig//drivers/spi/spi_mpc8xxx.c linux-2.6.31.6/drivers/spi/spi_mpc8xxx.c
> --- linux-2.6.31.6_orig//drivers/spi/spi_mpc8xxx.c      2009-11-10 01:32:31.000000000 +0100
> +++ linux-2.6.31.6/drivers/spi/spi_mpc8xxx.c    2009-11-19 08:15:33.000000000 +0100
> @@ -114,6 +114,7 @@
>        u32 rx_shift;           /* RX data reg shift when in qe mode */
>        u32 tx_shift;           /* TX data reg shift when in qe mode */
>        u32 hw_mode;            /* Holds HW mode register settings */
> +       int initialized;
>  };
>
>  static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
> @@ -503,15 +504,52 @@
>
>        return ret;
>  }
> +
> +
> +struct mpc8xxx_spi_probe_info {
> +       struct fsl_spi_platform_data pdata;
> +       int *gpios;
> +       bool *alow_flags;
> +};
> +
> +static struct mpc8xxx_spi_probe_info *
> +to_of_pinfo(struct fsl_spi_platform_data *pdata)
> +{
> +       return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
> +}
> +
> +static int mpc8xxx_spi_cs_init(struct spi_device *spi)
> +{
> +       struct device *dev = spi->dev.parent;
> +       struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
> +       u16 cs = spi->chip_select;
> +       int gpio = pinfo->gpios[cs];
> +       bool on = pinfo->alow_flags[cs] ^ !(spi->mode & SPI_CS_HIGH);
> +
> +       return gpio_direction_output(gpio, on);
> +}
> +
>  static int mpc8xxx_spi_transfer(struct spi_device *spi,
>                                struct spi_message *m)
>  {
>        struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
> +       struct spi_mpc8xxx_cs *cs = spi->controller_state;
>        unsigned long flags;
>
>        m->actual_length = 0;
>        m->status = -EINPROGRESS;
>
> +       if (cs && !cs->initialized) {
> +               int ret;
> +
> +               ret = mpc8xxx_spi_cs_init(spi);
> +               if (ret) {
> +                       dev_dbg(&spi->dev, "cs_init failed: %d\n", ret);
> +                       return ret;
> +               }
> +               cs->initialized = 1;
> +       }
> +
>        spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
>        list_add_tail(&m->queue, &mpc8xxx_spi->queue);
>        queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
> @@ -648,18 +686,6 @@
>        return 0;
>  }
>
> -struct mpc8xxx_spi_probe_info {
> -       struct fsl_spi_platform_data pdata;
> -       int *gpios;
> -       bool *alow_flags;
> -};
> -
> -static struct mpc8xxx_spi_probe_info *
> -to_of_pinfo(struct fsl_spi_platform_data *pdata)
> -{
> -       return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
> -}
> -
>  static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
>  {
>        struct device *dev = spi->dev.parent;
> @@ -720,14 +746,6 @@
>
>                pinfo->gpios[i] = gpio;
>                pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
> -
> -               ret = gpio_direction_output(pinfo->gpios[i],
> -                                           pinfo->alow_flags[i]);
> -               if (ret) {
> -                       dev_err(dev, "can't set output direction for gpio "
> -                               "#%d: %d\n", i, ret);
> -                       goto err_loop;
> -               }
>        }
>
>        pdata->max_chipselect = ngpios;
>
Anton Vorontsov - Nov. 26, 2009, 12:12 p.m.
On Wed, Nov 25, 2009 at 03:11:57PM -0700, Grant Likely wrote:
> On Wed, Nov 25, 2009 at 1:41 PM, Torsten Fleischer
> <to-fleischer@t-online.de> wrote:
> > On Wen, Nov 25, 2009 at 01:33:57 Grant Likely wrote:
> >> Thanks.  However, there needs to be a proper description of what this
> >> patch does to go in the commit header.  Can you please write one?
> >>
> >> Thanks,
> >> g.
> >>
> > [...]
> >
> > The initialization of the chip selects is removed from the probe() function of
> > the spi_mpc8xxx driver, because the driver doesn't know the polarity of the
> > chip selects of the SPI devices at the time of its initialization.
> >
> > For this reason the initialization of the several chip selects is postponed
> > to the point of time when the very first SPI transfer to the associated device
> > occurs.
> >
> >
> > Signed-off-by: Torsten Fleischer <to-fleischer@t-online.de>
> 
> Ah.  I understand what you're doing now.   Hmmm.  This approach
> concerns me because it relies on firmware or platform code to get CS
> gpios set up properly before the driver is probed.

Yes, that was said at the very beginning of this thread.

>  Firmware doesn't
> always get it right, and I prefer to avoid platform specific setups as
> much as possible.  Why can't the CS polarity be encoded into the
> device tree so the driver *does* have the polarity data at probe time?

We have the spi-cs-high property, but it duplicates compatible
property. 'compatible' is enough to tell whether some device has
cs-high or cs-low (device's driver knows that already).

The problem is that SPI bus master doesn't know all the devices,
so it can't extract that information from the compatible string.
To workaround that we can use 'spi-cs-high', but that's ugly
workaround.

SPI modes (0,1,2,3) is another matter, some devices can work in
several modes, so 'spi-cpol' and 'spi-cpha' are actually useful.
Torsten Fleischer - Nov. 26, 2009, 5:27 p.m.
On Thu, Nov 26, 2009 at 13:12:04 Anton Vorontsov wrote:
[...]
> > Ah.  I understand what you're doing now.   Hmmm.  This approach
> > concerns me because it relies on firmware or platform code to get CS
> > gpios set up properly before the driver is probed.
> 
> Yes, that was said at the very beginning of this thread.
> 
> >  Firmware doesn't
> > always get it right, and I prefer to avoid platform specific setups as
> > much as possible.  Why can't the CS polarity be encoded into the
> > device tree so the driver *does* have the polarity data at probe time?
> 
> We have the spi-cs-high property, but it duplicates compatible
> property. 'compatible' is enough to tell whether some device has
> cs-high or cs-low (device's driver knows that already).
> 
> The problem is that SPI bus master doesn't know all the devices,
> so it can't extract that information from the compatible string.
> To workaround that we can use 'spi-cs-high', but that's ugly
> workaround.
> 
> SPI modes (0,1,2,3) is another matter, some devices can work in
> several modes, so 'spi-cpol' and 'spi-cpha' are actually useful.
> 
To get a sane initial state the needed GPIOs can be set to be inputs during 
the driver's initialization.
This requires pull-up/pull-down resistors connected to the chip select 
lines. I think we can assume that they exist, because the GPIOs are all inputs 
after the controller's hardware reset and the resistors are needed to have a 
well-defined voltage level on the chip select lines. Normally the level is set 
so that the devices are disabled.

Therefore, it doesn't matter if the firmware sets the GPIOs wrong.
Grant Likely - Nov. 26, 2009, 6:16 p.m.
On Thu, Nov 26, 2009 at 5:12 AM, Anton Vorontsov
<avorontsov@ru.mvista.com> wrote:
> On Wed, Nov 25, 2009 at 03:11:57PM -0700, Grant Likely wrote:
>> On Wed, Nov 25, 2009 at 1:41 PM, Torsten Fleischer
>> <to-fleischer@t-online.de> wrote:
>> > On Wen, Nov 25, 2009 at 01:33:57 Grant Likely wrote:
>> >> Thanks.  However, there needs to be a proper description of what this
>> >> patch does to go in the commit header.  Can you please write one?
>> >>
>> >> Thanks,
>> >> g.
>> >>
>> > [...]
>> >
>> > The initialization of the chip selects is removed from the probe() function of
>> > the spi_mpc8xxx driver, because the driver doesn't know the polarity of the
>> > chip selects of the SPI devices at the time of its initialization.
>> >
>> > For this reason the initialization of the several chip selects is postponed
>> > to the point of time when the very first SPI transfer to the associated device
>> > occurs.
>> >
>> >
>> > Signed-off-by: Torsten Fleischer <to-fleischer@t-online.de>
>>
>> Ah.  I understand what you're doing now.   Hmmm.  This approach
>> concerns me because it relies on firmware or platform code to get CS
>> gpios set up properly before the driver is probed.
>
> Yes, that was said at the very beginning of this thread.

I also came in part way through as I wasn't an SPI maintainer when
this thread started.  :-)

>>  Firmware doesn't
>> always get it right, and I prefer to avoid platform specific setups as
>> much as possible.  Why can't the CS polarity be encoded into the
>> device tree so the driver *does* have the polarity data at probe time?
>
> We have the spi-cs-high property, but it duplicates compatible
> property. 'compatible' is enough to tell whether some device has
> cs-high or cs-low (device's driver knows that already).

But the device's driver isn't controlling the CS line, the SPI bus
driver is.  Besides, there is no guarantee that all drivers will
actualy be loaded before something starts using SPI.  The bus driver
*must* know what the active state of each CS line is before activating
any devices.

The spi bus binding is deficient in this case.  A property (can be
optional) need to be added to the spi bus node to explicitly state the
CS polarities.  It's not entirely sane to look for a "spi-cs-high'
property in the spi device nodes because the SPI bus may not be fully
populated (ie. if a device happens to be sitting on the bus, but isn't
in the device tree yet).  Before any SPI transactions go out, it is
the responsibility of the bus driver to ensure all CS lines are in the
correct state.

> The problem is that SPI bus master doesn't know all the devices,
> so it can't extract that information from the compatible string.
> To workaround that we can use 'spi-cs-high', but that's ugly
> workaround.

It doesn't need to know about the devices, but is must know how all of
its CS lines behave.  So it isn't an really an ugly workaround, but I
do think the binding is insufficient for the SPI bus driver (see
below)

> SPI modes (0,1,2,3) is another matter, some devices can work in
> several modes, so 'spi-cpol' and 'spi-cpha' are actually useful.

yes.  spi-cpol and spi-cpha are actually properties of the device, and
belong in the spi device node, not the spi bus node.

The spi-cs-high property is defined in
Documentation/powerpc/dts-bindings/spi-bus.txt, but it definitely was
a mistake for the reasons I described above.  It does work in some
cases, but a property at the bus node would be better.  The driver can
still fallback to looking for spi-cs-high properties in child nodes.

Currently the spi-cs-high property is parsed in the
of_register_spi_devices() function, but the CS polarity needs to be
known before registering devices.  It needs to be factored out into
another utility function callable by spi bus drivers so that it can
get polarity data at probe time.

Cheers,
g.
Grant Likely - Nov. 26, 2009, 6:18 p.m.
On Thu, Nov 26, 2009 at 10:27 AM, Torsten Fleischer
<to-fleischer@t-online.de> wrote:
> On Thu, Nov 26, 2009 at 13:12:04 Anton Vorontsov wrote:
> [...]
>> > Ah.  I understand what you're doing now.   Hmmm.  This approach
>> > concerns me because it relies on firmware or platform code to get CS
>> > gpios set up properly before the driver is probed.
>>
>> Yes, that was said at the very beginning of this thread.
>>
>> >  Firmware doesn't
>> > always get it right, and I prefer to avoid platform specific setups as
>> > much as possible.  Why can't the CS polarity be encoded into the
>> > device tree so the driver *does* have the polarity data at probe time?
>>
>> We have the spi-cs-high property, but it duplicates compatible
>> property. 'compatible' is enough to tell whether some device has
>> cs-high or cs-low (device's driver knows that already).
>>
>> The problem is that SPI bus master doesn't know all the devices,
>> so it can't extract that information from the compatible string.
>> To workaround that we can use 'spi-cs-high', but that's ugly
>> workaround.
>>
>> SPI modes (0,1,2,3) is another matter, some devices can work in
>> several modes, so 'spi-cpol' and 'spi-cpha' are actually useful.
>>
> To get a sane initial state the needed GPIOs can be set to be inputs during
> the driver's initialization.
> This requires pull-up/pull-down resistors connected to the chip select
> lines. I think we can assume that they exist, because the GPIOs are all inputs
> after the controller's hardware reset and the resistors are needed to have a
> well-defined voltage level on the chip select lines. Normally the level is set
> so that the devices are disabled.
>
> Therefore, it doesn't matter if the firmware sets the GPIOs wrong.

No, that's just shifting responsibility from firmware to hardware.
There is just as much broken hardware out there as broken firmware.
The assumption cannot be made that the initial state of the pin is the
inactive state of the CS line.  Plus, some GPIO pins are output only
and the inital state cannot be read.

g.
Anton Vorontsov - Nov. 26, 2009, 6:41 p.m.
On Thu, Nov 26, 2009 at 11:16:34AM -0700, Grant Likely wrote:
[...]
> The spi-cs-high property is defined in
> Documentation/powerpc/dts-bindings/spi-bus.txt, but it definitely was
> a mistake

Yup.

> Currently the spi-cs-high property is parsed in the
> of_register_spi_devices() function, but the CS polarity needs to be
> known before registering devices.  It needs to be factored out into
> another utility function callable by spi bus drivers so that it can
> get polarity data at probe time.

Untill we have this, Torsten's patch is a real improvement, and
works for non-broken hw/fw.

So I think it should be applied.

Thanks,
Grant Likely - Nov. 26, 2009, 6:50 p.m.
On Thu, Nov 26, 2009 at 11:41 AM, Anton Vorontsov
<avorontsov@ru.mvista.com> wrote:
> On Thu, Nov 26, 2009 at 11:16:34AM -0700, Grant Likely wrote:
> [...]
>> The spi-cs-high property is defined in
>> Documentation/powerpc/dts-bindings/spi-bus.txt, but it definitely was
>> a mistake
>
> Yup.
>
>> Currently the spi-cs-high property is parsed in the
>> of_register_spi_devices() function, but the CS polarity needs to be
>> known before registering devices.  It needs to be factored out into
>> another utility function callable by spi bus drivers so that it can
>> get polarity data at probe time.
>
> Untill we have this, Torsten's patch is a real improvement, and
> works for non-broken hw/fw.
>
> So I think it should be applied.

I disagree since it only band-aids the problem and uglifies the driver
in the process.  In the immediate term the driver needs to be changed
to read the spi-cs-high property out of the child nodes before
registering the devices.  I'm not going to apply this patch.

g.
Anton Vorontsov - Nov. 26, 2009, 7:01 p.m.
On Thu, Nov 26, 2009 at 11:50:05AM -0700, Grant Likely wrote:
> On Thu, Nov 26, 2009 at 11:41 AM, Anton Vorontsov
> <avorontsov@ru.mvista.com> wrote:
> > On Thu, Nov 26, 2009 at 11:16:34AM -0700, Grant Likely wrote:
> > [...]
> >> The spi-cs-high property is defined in
> >> Documentation/powerpc/dts-bindings/spi-bus.txt, but it definitely was
> >> a mistake
> >
> > Yup.
> >
> >> Currently the spi-cs-high property is parsed in the
> >> of_register_spi_devices() function, but the CS polarity needs to be
> >> known before registering devices.  It needs to be factored out into
> >> another utility function callable by spi bus drivers so that it can
> >> get polarity data at probe time.
> >
> > Untill we have this, Torsten's patch is a real improvement, and
> > works for non-broken hw/fw.
> >
> > So I think it should be applied.
> 
> I disagree since it only band-aids the problem and uglifies the driver
> in the process.  In the immediate term the driver needs to be changed
> to read the spi-cs-high property out of the child nodes before
> registering the devices.

Hm. I thought we agreed that spi-cs-high is not good? Why do you
encourage using it then? We'll have to uglify the driver with
legacy device-tree handling code.
Grant Likely - Nov. 26, 2009, 7:17 p.m.
On Thu, Nov 26, 2009 at 12:01 PM, Anton Vorontsov
<avorontsov@ru.mvista.com> wrote:
> On Thu, Nov 26, 2009 at 11:50:05AM -0700, Grant Likely wrote:
>> On Thu, Nov 26, 2009 at 11:41 AM, Anton Vorontsov
>> <avorontsov@ru.mvista.com> wrote:
>> > On Thu, Nov 26, 2009 at 11:16:34AM -0700, Grant Likely wrote:
>> > [...]
>> >> The spi-cs-high property is defined in
>> >> Documentation/powerpc/dts-bindings/spi-bus.txt, but it definitely was
>> >> a mistake
>> >
>> > Yup.
>> >
>> >> Currently the spi-cs-high property is parsed in the
>> >> of_register_spi_devices() function, but the CS polarity needs to be
>> >> known before registering devices.  It needs to be factored out into
>> >> another utility function callable by spi bus drivers so that it can
>> >> get polarity data at probe time.
>> >
>> > Untill we have this, Torsten's patch is a real improvement, and
>> > works for non-broken hw/fw.
>> >
>> > So I think it should be applied.
>>
>> I disagree since it only band-aids the problem and uglifies the driver
>> in the process.  In the immediate term the driver needs to be changed
>> to read the spi-cs-high property out of the child nodes before
>> registering the devices.
>
> Hm. I thought we agreed that spi-cs-high is not good? Why do you
> encourage using it then? We'll have to uglify the driver with
> legacy device-tree handling code.

spi-cs-high is definitely not a complete solution, but it isn't
actively evil either.  Plus it is documented and (presumably) in
active use. so support for it should not be dropped.

Regardless, there needs to be a library function for parsing all the
SPI child nodes and returning the active state for each GPIO chip
select.  All the code for parsing the old spi-cs-high properties can
be contained in the same place as a new yet-to-be-defined bus node cs
polarity property.  The rework to the driver itself is not ugly.

g.

Patch

diff -u -r -N linux-2.6.31.6_orig//drivers/spi/spi_mpc8xxx.c linux-2.6.31.6/drivers/spi/spi_mpc8xxx.c
--- linux-2.6.31.6_orig//drivers/spi/spi_mpc8xxx.c	2009-11-10 01:32:31.000000000 +0100
+++ linux-2.6.31.6/drivers/spi/spi_mpc8xxx.c	2009-11-19 08:15:33.000000000 +0100
@@ -114,6 +114,7 @@ 
 	u32 rx_shift;		/* RX data reg shift when in qe mode */
 	u32 tx_shift;		/* TX data reg shift when in qe mode */
 	u32 hw_mode;		/* Holds HW mode register settings */
+	int initialized;
 };
 
 static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
@@ -503,15 +504,52 @@ 
 
 	return ret;
 }
+
+
+struct mpc8xxx_spi_probe_info {
+	struct fsl_spi_platform_data pdata;
+	int *gpios;
+	bool *alow_flags;
+};
+
+static struct mpc8xxx_spi_probe_info *
+to_of_pinfo(struct fsl_spi_platform_data *pdata)
+{
+	return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
+}
+
+static int mpc8xxx_spi_cs_init(struct spi_device *spi)
+{
+	struct device *dev = spi->dev.parent;
+	struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
+	u16 cs = spi->chip_select;
+	int gpio = pinfo->gpios[cs];
+	bool on = pinfo->alow_flags[cs] ^ !(spi->mode & SPI_CS_HIGH);
+
+	return gpio_direction_output(gpio, on);
+}
+
 static int mpc8xxx_spi_transfer(struct spi_device *spi,
 				struct spi_message *m)
 {
 	struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
+	struct spi_mpc8xxx_cs *cs = spi->controller_state;
 	unsigned long flags;
 
 	m->actual_length = 0;
 	m->status = -EINPROGRESS;
 
+	if (cs && !cs->initialized) {
+		int ret;
+
+		ret = mpc8xxx_spi_cs_init(spi);
+		if (ret) {
+			dev_dbg(&spi->dev, "cs_init failed: %d\n", ret);
+			return ret;
+		}
+		cs->initialized = 1;
+	}
+
 	spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
 	list_add_tail(&m->queue, &mpc8xxx_spi->queue);
 	queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
@@ -648,18 +686,6 @@ 
 	return 0;
 }
 
-struct mpc8xxx_spi_probe_info {
-	struct fsl_spi_platform_data pdata;
-	int *gpios;
-	bool *alow_flags;
-};
-
-static struct mpc8xxx_spi_probe_info *
-to_of_pinfo(struct fsl_spi_platform_data *pdata)
-{
-	return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
-}
-
 static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
 {
 	struct device *dev = spi->dev.parent;
@@ -720,14 +746,6 @@ 
 
 		pinfo->gpios[i] = gpio;
 		pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
-
-		ret = gpio_direction_output(pinfo->gpios[i],
-					    pinfo->alow_flags[i]);
-		if (ret) {
-			dev_err(dev, "can't set output direction for gpio "
-				"#%d: %d\n", i, ret);
-			goto err_loop;
-		}
 	}
 
 	pdata->max_chipselect = ngpios;