Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi_mpc83xx: fix prescale modulus calculation

Long ago I've noticed (but didn't pay much attention) that
spi_mpc83xx using PM calculations that differs from what
specs describe. I.e.

u8 pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 4);

While specs says: "The SPI baud rate generator clock source (either
system clock or system clock divided by 16, depending on DIV16 bit) is
divided by 4 * ([PM] + 1), a range from 4 to 64.".

Thus " - 1" is missing in the spi_mpc83xx's formula.

Why nobody noticed that bug? Probably because sysclk usually less then
user expects, e.g. you expect 200 MHz, but real clock is 198 MHz,
and integer rounding helps when this formula is used.

Suppose it's SPI in QE, SYSCLK at 198 MHz, thus SPIBRG at 99MHz, 25 MHz
requested.

PM = (99MHz / ( 25 MHz * 4 )), PM == 0, output SPICLK will be 24.75 MHz

At lower frequencies this bug is more noticeable, though.

And this bug shows itself in all its beauty if SYSCLK is equal or a bit
more than you expect (200 MHz SYSCLK, 100 MHz SPIBRG):
PM = (100MHz / ( 25 MHz * 4 )), PM == 1, output SPICLK will be 12.625 MHz!

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Anton Vorontsov and committed by
Linus Torvalds
a44648b0 e24a4d1e

+6 -2
+6 -2
drivers/spi/spi_mpc83xx.c
··· 148 148 if (value == BITBANG_CS_ACTIVE) { 149 149 u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode); 150 150 u32 len = spi->bits_per_word; 151 + u8 pm; 152 + 151 153 if (len == 32) 152 154 len = 0; 153 155 else ··· 172 170 regval |= SPMODE_LEN(len); 173 171 174 172 if ((mpc83xx_spi->spibrg / spi->max_speed_hz) >= 64) { 175 - u8 pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 64); 173 + pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 64) - 1; 176 174 if (pm > 0x0f) { 177 175 dev_err(&spi->dev, "Requested speed is too " 178 176 "low: %d Hz. Will use %d Hz instead.\n", ··· 182 180 } 183 181 regval |= SPMODE_PM(pm) | SPMODE_DIV16; 184 182 } else { 185 - u8 pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 4); 183 + pm = mpc83xx_spi->spibrg / (spi->max_speed_hz * 4); 184 + if (pm) 185 + pm--; 186 186 regval |= SPMODE_PM(pm); 187 187 } 188 188