Skip to content

Commit 730bbfa

Browse files
lategoodbyebroonie
authored andcommitted
spi: spi-fsl-lpspi: Fix scldiv calculation
The effective SPI clock frequency should never exceed speed_hz otherwise this might result in undefined behavior of the SPI device. Currently the scldiv calculation could violate this constraint. For the example parameters perclk_rate = 24 MHz and speed_hz = 7 MHz, the function fsl_lpspi_set_bitrate will determine perscale = 0 and scldiv = 1, which is a effective SPI clock of 8 MHz. So fix this by rounding up the quotient of perclk_rate and speed_hz. While this never change within the loop, we can pull this out. Fixes: 5314987 ("spi: imx: add lpspi bus driver") Signed-off-by: Stefan Wahren <wahrenst@gmx.net> Link: https://patch.msgid.link/20240804113611.83613-1-wahrenst@gmx.net Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent e4c4638 commit 730bbfa

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/spi/spi-fsl-lpspi.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
296296
static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
297297
{
298298
struct lpspi_config config = fsl_lpspi->config;
299-
unsigned int perclk_rate, scldiv;
299+
unsigned int perclk_rate, scldiv, div;
300300
u8 prescale;
301301

302302
perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
@@ -313,8 +313,10 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
313313
return -EINVAL;
314314
}
315315

316+
div = DIV_ROUND_UP(perclk_rate, config.speed_hz);
317+
316318
for (prescale = 0; prescale < 8; prescale++) {
317-
scldiv = perclk_rate / config.speed_hz / (1 << prescale) - 2;
319+
scldiv = div / (1 << prescale) - 2;
318320
if (scldiv < 256) {
319321
fsl_lpspi->config.prescale = prescale;
320322
break;

0 commit comments

Comments
 (0)