Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpi-4.9.y: spi driver reports bogus maximum frequency #2165

Closed
skoehler opened this issue Aug 16, 2017 · 12 comments
Closed

rpi-4.9.y: spi driver reports bogus maximum frequency #2165

skoehler opened this issue Aug 16, 2017 · 12 comments
Labels
Waiting for external input Waiting for a comment from the originator of the issue, or a collaborator. Waiting for internal comment Waiting for comment from a member of the Raspberry Pi engineering team

Comments

@skoehler
Copy link
Contributor

skoehler commented Aug 16, 2017

A bug was reported for the MRAA library that the SPI frequency cannot be set higher than 500kHz:
eclipse/mraa#255

It turns out that this boils down to a potential problem with the Raspberry Pi's SPI driver. In response to the SPI_IOC_RD_MAX_SPEED_HZ ioctl, the kernel driver reports some frequency slightly above 500kHz. MRAA then rounds down any requested frequency to the reported maximum speed.

It is known that the Raspberry Pi support speeds of up to 125MHz. So the value reported by the driver should be incorrect. I suggest that the driver is fixed to report the actual maximum speed obtained when the divisor is equal to 2.

Meanwhile, MRAA has been patched to ignore the maximum speed reported by the Raspberry Pi driver. Yet, I though that I should report the bug here so that the kernel driver is actually fixed.

While I did not test it, the bug may also affect other branches such as rpi-4.10.y, rpi-4.11.y, rpi-4.12.y, and rpi-4.13.y.

@pelwell
Copy link
Contributor

pelwell commented Aug 16, 2017

I can confirm the symptoms, but fortunately the error comes from Device Tree. Since the introduction of Raspberry Pi Device Tree in rpi-3.15.y, the standard spidev nodes have had an spi-max-frequency (the DT equivalent of max_speed_hz) of 500000. This value has even propagated to the overlays enabling spi1 and spi2.

I'm slightly nervous that changing the maximum now will cause applications currently passing in zero as a speed (or querying the maximum and using that) to fail because their devices can't run at 125MHz. However, that counts as bad coding, and hopefully the usual gradual rollout (rpi-update, apt-get upgrade, major Raspbian release) would catch at least some of them before the change goes mainstream.

I'm inclined to change all of the RPi spi-max-frequencys to 125000000 - any objections, @popcornmix?

@popcornmix
Copy link
Collaborator

The 500kHz value is clearly wrong so should be changed.
Are we sure that 125MHz is actually achievable?
While I suspect the peripheral can internally handle that speed, I am suspecting the gpio slew may make that speed unusable.
@P33M any thoughts?

@P33M
Copy link
Contributor

P33M commented Aug 16, 2017

125MHz is probably out past the realms of sanity from a hardware perspective. For example, the SD card interface (4-bit) can just about run at 100MHz and this is with much shorter PCB traces that don't stretch across the board to a pin header.

I'd say sub-100MHz (say 62.5MHz) would be the maximum speed I'd expect to work at all reliably.

@pelwell
Copy link
Contributor

pelwell commented Aug 16, 2017

I don't think we should be artificially restricting the range to what is going to be usable. The DT bindings say describe spi-max-frequency as:

Maximum SPI clocking speed of device in Hz.

But since this device is spidev (a userspace API), the value is effectively the maximum bus speed. The SPI host driver would not restrict a kernel SPI device driver to a lower value, so why should spidev be treated any differently?

@popcornmix
Copy link
Collaborator

Okay, if max freq is treated as theoretical limit (much like arm_freq=1500), rather than a usable default then I'm happy with the change to 125MHz.

pelwell pushed a commit that referenced this issue Aug 16, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
@pelwell
Copy link
Contributor

pelwell commented Aug 16, 2017

I've pushed a patch to rpi-4.9.y, and will update the other branches tomorrow unless someone complains.

popcornmix added a commit to raspberrypi/firmware that referenced this issue Aug 16, 2017
kernel: config: Enable CONFIG_BRCMDBG temporarily for debugging
See: raspberrypi/linux#1342

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz
See: raspberrypi/linux#2165
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this issue Aug 16, 2017
kernel: config: Enable CONFIG_BRCMDBG temporarily for debugging
See: raspberrypi/linux#1342

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz
See: raspberrypi/linux#2165
popcornmix added a commit to raspberrypi/firmware that referenced this issue Aug 16, 2017
kernel: config: Enable CONFIG_BRCMDBG temporarily for debugging
See: raspberrypi/linux#1342

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz
See: raspberrypi/linux#2165
@popcornmix
Copy link
Collaborator

Latest rpi-update kernel includes the patch @pelwell pushed earlier.

@notro
Copy link
Contributor

notro commented Aug 17, 2017

Wouldn't it better to add a dtparam to change this?
I suspect a lot of setups that don't care about speed rely on the default value.

spi-max-frequency specifies the maximum a certain spi client can handle (and in the spidev case we don't know the client), and setting it to 125MHz certainly is a stretch :-)
I know of only one spi display (CPLD in front) that can actually handle that and in 4k bursts only.

Or better yet, put the spidev node in an overlay and let 'dtparam=spi=on' load it for backwards compatibility.

Userspace can also change the spidev speed using SPI_IOC_WR_MAX_SPEED_HZ:
http://elixir.free-electrons.com/linux/v4.9/source/drivers/spi/spidev.c#L477

@pelwell
Copy link
Contributor

pelwell commented Aug 17, 2017

I'm not keen on expecting the userspace code to know that the default maximum speed isn't actually the maximum - the MRAA check seems sensible - which is why I was happy to increase the default.

I would like to change spidev node in the DT to be disabled by default to remove the need for real SPI device overlays to disable the corresponding spidev node (i.e. opt-in to spidev rather than opt-out), but that would require spidev users to also indicate which they want to enable - all or nothing is too coarse, and we can't remove the spidev-disabling fragments until they are guaranteed not to be needed.

We could defer the selective spidev enabling, requested by dtparam=spi=on, until all overlays have been processed, but that puts even more special case code in the firmware when I'd like to reduce it. spidev is a bit of a mess altogether - I2C handles user-space access (/dev/i2c-) in a cleaner way.

bulletmark added a commit to bulletmark/pifaceio that referenced this issue Aug 20, 2017
The linux-raspberrypi kernel update to 4.9.43 included a change to the
default reported max speed of the linux spidev, from 100KHz to 125MHz.

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz.
See raspberrypi/linux#2165.

It appears the reported max speed is used as the default if you don't
specify it for the device. The MCP23S17 chip used in the PiFace digital
board can only handle 10MHz but we were using the default so it was not
working after the recent kernel update. This change explicitly sets the
maximum speed after opening the device.
popcornmix pushed a commit that referenced this issue Oct 29, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
DanielChesters added a commit to DanielChesters/pifacecommon that referenced this issue Nov 1, 2017
raspbian-autopush pushed a commit to raspbian-packages/linux-4.9 that referenced this issue Nov 2, 2017
commit 071bba3
Author: Phil Elwell <phil@raspberrypi.org>
Date:   Wed Aug 16 16:52:50 2017 +0100

    BCM270X_DT: Set spidev spi-max-frequency to 125MHz
    
    The BCM2835 SPI controllers have a maximum bus clock of half the system
    clock speed, so with the 250MHz system clock found on Raspberry Pis
    you get a theoretical maximum bus speed of 125MHz. Note that this
    speed is unlikely to be reliable, and the maximum usable bus spee will
    depend on both the attached device and the wiring.
    
    See: raspberrypi/linux#2165
    Signed-off-by: Phil Elwell <phil@raspberrypi.org>


Gbp-Pq: Topic rpi
Gbp-Pq: Name rpi_1335_071bba328d62a0df87d0b4d94797681c3850ac9a.patch
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 18, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 19, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit to pelwell/linux that referenced this issue Nov 21, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
pelwell pushed a commit that referenced this issue Nov 21, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
popcornmix pushed a commit that referenced this issue Nov 21, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
ryncsn pushed a commit to ryncsn/linux-rasp that referenced this issue Nov 21, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: raspberrypi#2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
popcornmix pushed a commit that referenced this issue Nov 24, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
popcornmix pushed a commit that referenced this issue Dec 1, 2017
The BCM2835 SPI controllers have a maximum bus clock of half the system
clock speed, so with the 250MHz system clock found on Raspberry Pis
you get a theoretical maximum bus speed of 125MHz. Note that this
speed is unlikely to be reliable, and the maximum usable bus spee will
depend on both the attached device and the wiring.

See: #2165
Signed-off-by: Phil Elwell <phil@raspberrypi.org>
@JamesH65
Copy link
Contributor

JamesH65 commented Dec 4, 2017

@notro @pelwell Reading through the comments, I believe this can now be closed?

@JamesH65 JamesH65 added Waiting for internal comment Waiting for comment from a member of the Raspberry Pi engineering team Waiting for external input Waiting for a comment from the originator of the issue, or a collaborator. labels Dec 4, 2017
@pelwell
Copy link
Contributor

pelwell commented Dec 4, 2017

There has been some fallout, as predicted, from applications that don't specify their own transfer speed, and I'm sure there'll be more as people upgrade, but I'll deal with it as it happens.

Trekky12 added a commit to Trekky12/node-rpi-ws2801 that referenced this issue Jan 3, 2018
The raspberry linux kernel updated the default spi frequency to 125MHz:

kernel: BCM270X_DT: Set spidev spi-max-frequency to 125MHz.
See raspberrypi/linux#2165.

The WS2801 LEDs can only handle 10 MHz, but when not set the default value is used, which is too much.
This commit explicit includes the spi package and sets the max speed for the spidev
raspbian-autopush pushed a commit to raspbian-packages/linux-4.9 that referenced this issue Apr 7, 2018
commit a670cb1
Author: Phil Elwell <phil@raspberrypi.org>
Date:   Wed Aug 16 16:52:50 2017 +0100

    BCM270X_DT: Set spidev spi-max-frequency to 125MHz
    
    The BCM2835 SPI controllers have a maximum bus clock of half the system
    clock speed, so with the 250MHz system clock found on Raspberry Pis
    you get a theoretical maximum bus speed of 125MHz. Note that this
    speed is unlikely to be reliable, and the maximum usable bus spee will
    depend on both the attached device and the wiring.
    
    See: raspberrypi/linux#2165
    Signed-off-by: Phil Elwell <phil@raspberrypi.org>


Gbp-Pq: Topic rpi
Gbp-Pq: Name rpi_1337_a670cb1ba7a2ff3ae4d56b67107fa94ee72089ff.patch
@JamesH65
Copy link
Contributor

@pelwell Appears closable...?

@pelwell pelwell closed this as completed Jun 27, 2018
raspbian-autopush pushed a commit to raspbian-packages/linux-4.9 that referenced this issue Nov 11, 2018
commit a670cb1
Author: Phil Elwell <phil@raspberrypi.org>
Date:   Wed Aug 16 16:52:50 2017 +0100

    BCM270X_DT: Set spidev spi-max-frequency to 125MHz
    
    The BCM2835 SPI controllers have a maximum bus clock of half the system
    clock speed, so with the 250MHz system clock found on Raspberry Pis
    you get a theoretical maximum bus speed of 125MHz. Note that this
    speed is unlikely to be reliable, and the maximum usable bus spee will
    depend on both the attached device and the wiring.
    
    See: raspberrypi/linux#2165
    Signed-off-by: Phil Elwell <phil@raspberrypi.org>


Gbp-Pq: Topic rpi
Gbp-Pq: Name rpi_1337_a670cb1ba7a2ff3ae4d56b67107fa94ee72089ff.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting for external input Waiting for a comment from the originator of the issue, or a collaborator. Waiting for internal comment Waiting for comment from a member of the Raspberry Pi engineering team
Projects
None yet
Development

No branches or pull requests

6 participants