Side effect of SPI GPIO descriptor usage
Hi,
A relatively recent patch [1] to the spi-bcm2835 driver modified it to use GPIO descriptors for chip select handling. A side effect of this change is to set the SPI_MODE_CS_HIGH flag for devices connected to the controller, which seems strange since it happens for devices that require the usual active-low chip select.
This change came to light when a user reported that the SPI-Py library
(a client of the spidev driver) wasn't working on 5.4, which was traced to it overwriting the SPI mode flags when it was only trying to set the
CPHA and CPOL flags. This had the affect of inverting the chip select
line, with the obvious consequences. That corruption of the flags is clearly an error, but what if the application and library were genuinely trying to specify that the attached device required an active-high chip select? Would it now require that they _clear_ the CS_HIGH flag?
Thanks,
Phil
[1] 3bd158c56a56 ("spi: bcm2835: Convert to use CS GPIO descriptors")
A message that is not in the archive (sent off-list or not yet received): db6a1e17-49a3-e3ed-7713-56b7763713d6-FnsA7b+Nu9XbIbC87yuRow@public.gmane.org
Quoted 5 lines · Phil Elwell
A relatively recent patch [1] to the spi-bcm2835 driver modified it to use GPIO descriptors for chip select handling. A side effect of this change is to set the
SPI_MODE_CS_HIGHflag for devices connected to the controller, which seems strange since it happens for devices that require the usual active-low chip select.
I take it you mean SPI_CS_HIGH rather than SPI_MODE_CS_HIGH? I can't see anything in that change which sets the flag, can you be more specific? The change no longer acts on SPI_CS_HIGH when requesting the GPIO but I can't see anything which *sets* the flag. It does not visbily modify mode at all, nor did the original code.
Quoted 8 lines
This change came to light when a user reported that the SPI-Py library (a client of the spidev driver) wasn't working on 5.4, which was traced to it overwriting the SPI mode flags when it was only trying to set the CPHA and CPOL flags. This had the affect of inverting the chip select line, with the obvious consequences. That corruption of the flags is clearly an error, but what if the application and library were genuinely trying to specify that the attached device required an active-high chip select? Would it now require that they _clear_ the
CS_HIGHflag?
I can't follow what you're saying here at all, sorry. Can you please be more specific? You appear to be saying that the issue is that the application modified the mode in some way and this was acted on apparently not in the expected way.
In general it's a bad idea to modify mode at runtime, and it's a bad idea to mix multiple means of configuring the polarity of the chip select (eg, mixing DT configuration with other means).
_______________________________________________
linux-rpi-kernel mailing list linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
http://lists.infradead.org/mailman/listinfo/linux-rpi-kernel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi Mark,
Quoted 9 lines · Mark Brown
Quoted 5 lines · Phil Elwell
A relatively recent patch [1] to the spi-bcm2835 driver modified it to use GPIO descriptors for chip select handling. A side effect of this change is to set the
SPI_MODE_CS_HIGHflag for devices connected to the controller, which seems strange since it happens for devices that require the usual active-low chip select.I take it you mean
SPI_CS_HIGHrather thanSPI_MODE_CS_HIGH?
Sorry, yes - SPI_CS_HIGH.
Quoted 3 lines
I
can't see anything in that change which sets the flag, can you be more specific?
bcm2835_spi_probe in spi-bcm2835.c sets ctrl->use_gpio_descriptors to true, and of_spi_parse_dt in spi.c includes the following:
/*
* For descriptors associated with the device, polarity inversion is
* handled in the gpiolib, so all gpio chip selects are "active high"
* in the logical sense, the gpiolib will invert the line if need be. */
if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods &&
ctlr->cs_gpiods[spi->chip_select]) spi->mode |= SPI_CS_HIGH;
Quoted 14 lines
The change no longer acts on
SPI_CS_HIGHwhen
requesting the GPIO but I can't see anything which *sets* the flag. It does not visbily modify mode at all, nor did the original code.Quoted 8 lines
This change came to light when a user reported that the SPI-Py library (a client of the spidev driver) wasn't working on 5.4, which was traced to it overwriting the SPI mode flags when it was only trying to set the CPHA and CPOL flags. This had the affect of inverting the chip select line, with the obvious consequences. That corruption of the flags is clearly an error, but what if the application and library were genuinely trying to specify that the attached device required an active-high chip select? Would it now require that they _clear_ the
CS_HIGHflag?
Quoted 4 lines
I can't follow what you're saying here at all, sorry. Can you please be more specific? You appear to be saying that the issue is that the application modified the mode in some way and this was acted on apparently not in the expected way.
That's correct. Although the trampling of the mode flags was erroneous, actually the resulting flags were correct - SPI_CS_HIGH was not set, yet the chip select line was clearly being driven in an active-high fashion.
Quoted 3 lines
In general it's a bad idea to modify mode at runtime, and it's a bad idea to mix multiple means of configuring the polarity of the chip select (eg, mixing DT configuration with other means).
Applications using spidev to implement user-space drivers need to be able to set SPI mode, CS polarity etc. at run time. I agree that there are many ways to set the polarity of a chip select, and it may be that
too many are being used with GPIO CSs declared by DT, but I don't think that completely explains the connection between use_gpio_descriptors and the automatic setting of SPI_CS_HIGH.
Phil
A message that is not in the archive (sent off-list or not yet received): 1e0b7cd6-cf9e-2b68-f875-8015c8ffc326-FnsA7b+Nu9XbIbC87yuRow@public.gmane.org
Quoted 10 lines · Phil Elwell
Quoted 5 lines · Mark Brown
On Mon, Dec 02, 2019 at 12:10:11PM +0000, Phil Elwell wrote:
I
can't see anything in that change which sets the flag, can you be more specific?
bcm2835_spi_probeinspi-bcm2835.csetsctrl->use_gpio_descriptorsto true, andof_spi_parse_dtinspi.cincludes the following:
Quoted 8 lines
/*
* For descriptors associated with the device, polarity inversion is
* handled in the gpiolib, so all gpio chip selects are "active high"
* in the logical sense, the gpiolib will invert the line if need be. */
if ((ctlr->use_gpio_descriptors) &&ctlr->cs_gpiods&&ctlr->cs_gpiods[spi->chip_select]) spi->mode |= SPI_CS_HIGH;
Right, I see what you're saying now.
Quoted 6 lines
Quoted 3 lines
In general it's a bad idea to modify mode at runtime, and it's a bad idea to mix multiple means of configuring the polarity of the chip select (eg, mixing DT configuration with other means).
Applications using spidev to implement user-space drivers need to be able to set SPI mode, CS polarity etc. at run time. I agree that there
I'm nervous of spidev user doing stuff like that with the chip selects, with DT even spidev devices should be registered normally, you will get a complaint if you register a raw spidev. There's no free pass for "oh, spidev can do anything we don't care" here - the DT should describe the hardware, if some of the hardware happens to be implemented by spidev then fine.
That said we do have other in kernel users that do change modes at runtime, though I'm not convinced many of them have GPIO chip selects. Linus?
Quoted 4 lines
are many ways to set the polarity of a chip select, and it may be that too many are being used with GPIO CSs declared by DT, but I don't think that completely explains the connection between
use_gpio_descriptorsand the automatic setting ofSPI_CS_HIGH.
That's the result of there being too many places where the polarity of the chip select is set so the code is pushing to at least only implement this in one place so things are clearer and more consistent.
_______________________________________________
linux-rpi-kernel mailing list linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
http://lists.infradead.org/mailman/listinfo/linux-rpi-kernel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 11 lines · Mark Brown
Quoted 2 lines · Phil Elwell
Applications using spidev to implement user-space drivers need to be able to set SPI mode, CS polarity etc. at run time. I agree that there
I'm nervous of spidev user doing stuff like that with the chip selects, with DT even spidev devices should be registered normally, you will get a complaint if you register a raw spidev. There's no free pass for "oh, spidev can do anything we don't care" here - the DT should describe the hardware, if some of the hardware happens to be implemented by spidev then fine.
I agree with this. In 99 cases out of 100 it turns out that the userspace driver is a substandard version of a driver that should actually be in a place such as drivers/iio and what we end up supporting is offended userspace driver authors who are acting against the interests of the kernel community.
I have come to accept the usecase of userspace GPIO for things like industrial automation one-offs, random hackerspace projects, and prototypes all highly custom, and with such high friction with kernel internals and pain to carry forward should be expected.
With devices on SPI and I2C I get really sceptic. What are those hardware devices, and what makes them so fantastic that they cannot use a kernel driver like everyone else?
Quoted 3 lines
That said we do have other in kernel users that do change modes at runtime, though I'm not convinced many of them have GPIO chip selects. Linus?
I looked it over and most are devices like board files and RTC clocks that enforce SPI_CS_HIGH. They seem to be native chip selects so it will work as expected but I will look it over so I haven't broken any of them.
and I can find one more outlier:
drivers/mmc/host/mmc_spi.c, function mmc_spi_initsequence()
This very clearly want the inverse polarity of whatever was the default, but the code assumes that SPI_CS_HIGH implies that this is the actual physical level and forces it high, then low. I will send a patch for this: what they want to achieve is the inverse of whatever was configured.
The driver drivers/net/wireless/ti/wlcore/spi.c does exactly this already.
I am a bit sorry about the semantics of "HIGH" here when the right word should rather be "asserted": it's named like this for historical reasons.
An alternative would be to let SPI use gpiod_set_raw_value() to steamroll all the inversion semantics in the GPIO library, and SPI_CS_HIGH would literally mean to drive the physical line high.
This may seem like an intuitively correct solution but it has implications for everything using device tree to define SPI clients: spi-cs-high in a device tree means that the CS is active high, which gpiolib currently detects and handles by way of disabling internal inversion (the default for SPI devices is active low). Previously the SPI core would set SPI_CS_HIGH and we would then just double-assign that in the SPI core and then again in some drivers, it doesn't seem much better to me.
So I will look these over, but if people (and especially the SPI maintainer) prefer the semantic that SPI_CS_HIGH does not mean "asserted" but takes direct control of the physical level of the CS line, I can do that instead.
I could also additionally patch the whole kernel to rename SPI_CS_HIGH into SPI_CS_ASSERTED if this makes things better. (Would be a two-step patch rocket adding the additional define and switching everyone over before deleting the SPI_CS_HIGH.)
Yours,
Linus Walleij
A message that is not in the archive (sent off-list or not yet received): CACRpkdbqNw+h6MphF7kLAwym-F3G+ZebMDB2BMt0bzA6DfM0OA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 9 lines · Linus Walleij
and I can find one more outlier:
drivers/mmc/host/mmc_spi.c, functionmmc_spi_initsequence()This very clearly want the inverse polarity of whatever was the default, but the code assumes that
SPI_CS_HIGHimplies that this is the actual physical level and forces it high, then low. I will send a patch for this: what they want to achieve is the inverse of whatever was configured.
Yes, that's the first one I looked at when I was poking around and I agree with that analysis.
Quoted 3 lines
I am a bit sorry about the semantics of "HIGH" here when the right word should rather be "asserted": it's named like this for historical reasons.
There's quite a few things I'd do differently if I were designing the API.
Quoted 5 lines
An alternative would be to let SPI use
gpiod_set_raw_value()to steamroll all the inversion semantics in the GPIO library, andSPI_CS_HIGHwould literally mean to drive the physical line high.
Quoted 9 lines
This may seem like an intuitively correct solution but it has implications for everything using device tree to define SPI clients: spi-cs-high in a device tree means that the CS is active high, which gpiolib currently detects and handles by way of disabling internal inversion (the default for SPI devices is active low). Previously the SPI core would set
SPI_CS_HIGHand we would then just double-assign that in the SPI core and then again in some drivers, it doesn't seem much better to me.
Yeah, my general thought is that trying to do anything other than making a new interface for this and actively deleting all the old ones is probably just going to be pushing the problem around and not actually any better.
Quoted 5 lines
I could also additionally patch the whole kernel to rename
SPI_CS_HIGHintoSPI_CS_ASSERTEDif this makes things better. (Would be a two-step patch rocket adding the additional define and switching everyone over before deleting theSPI_CS_HIGH.)
I think that's probably worth it mainly in the context of also auditing all the users and possibly making a bigger change which makes it harder for users to trigger problems. That's a lot more work though and needs more thought about what a better interface would be.
_______________________________________________
linux-rpi-kernel mailing list linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
http://lists.infradead.org/mailman/listinfo/linux-rpi-kernel
Quoted 4 lines · Linus Walleij
I looked it over and most are devices like board files and RTC clocks that enforce
SPI_CS_HIGH. They seem to be native chip selects so it will work as expected but I will look it over so I haven't broken any of them.
I looked all over:
drivers/mfd/ezx-pcap.c
Enforces SPI_CS_HIGH if platform data PCAP_CS_AH is set. Nothing (no board file) in the kernel sets it. No action.
drivers/mfd/mc13xxx-spi.cdrivers/mfd/motorola-cpcap.c
Hardcodes both SPI_MODE_0 and SPI_CS_HIGH.
Should be patched to |= SPI_MODE_0 and
^= SPI_CS_HIGH. I sent patches.
drivers/rtc/rts-ds1302.cdrivers/rtc/rts-ds1305.cdrivers/rtc/rtc-pcf2123.c
These just have some text comments on the importance of using an active high chip select. That then gets defined in the boardfile or the device tree which is fine. Any GPIO lines used for this can be flagged as active high or low appropriately.
drivers/rtc/rtc-ds1343.c:
Hardcodes SPI_CS_HIGH, has no in-tree users (bo board files refer to it), does not support device tree. So there is no way to break it. Sent a patch anyways.
drivers/rtc/rtc-rx6110.c
Prints a warning if not flagged SPI_CS_HIGH, wil print that warning if used with a GPIO chip select from device tree. No regression, also no in-tree users.
drivers/staging/fb_uc1611.c drivers/staging/fb_watterott.c
A staging driver hardcoding SPI_CS_HIGH, sent a patch for this too so we don't gain any bad examples.
Yours,
Linus Walleij
A message that is not in the archive (sent off-list or not yet received): CACRpkdaxXKMqL4Y8yFu+U7WhpXgq6Yr+hx4rYd3aDzc+-dLDFw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Hi Linus,
Quoted 4 lines · Linus Walleij
drivers/rtc/rtc-rx6110.c
Prints a warning if not flaggedSPI_CS_HIGH, wil print that warning if used with a GPIO chip select from device tree. No regression, also no in-tree users.
So this warning will trigger if the board designer added an inverter, e.g. to make it work with an SPI controller that doesn't support CS_HIGH...
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 6 lines · Linus Walleij
Quoted 1 line · Mark Brown
On Mon, Dec 02, 2019 at 02:11:51PM +0000, Phil Elwell wrote:
and I can find one more outlier:
Quoted 7 lines
drivers/mmc/host/mmc_spi.c, functionmmc_spi_initsequence()This very clearly want the inverse polarity of whatever was the default, but the code assumes that
SPI_CS_HIGHimplies that this is the actual physical level and forces it high, then low. I will send a patch for this: what they want to achieve is the inverse of whatever was configured.
Linus, hold on.
MMC SPI is abusing protocol, you can't fix it in logical way. I recommend to read the protocol specification.
P.S. I would be glad to be wrong, though.
Signature · 2 lines
With Best Regards, Andy Shevchenko