Skip to content

Commit 8455230

Browse files
lategoodbyegregkh
authored andcommitted
spi: spi-fsl-lpspi: Adjust type of scldiv
[ Upstream commit fa8ecda ] The target value of scldiv is just a byte, but its calculation in fsl_lpspi_set_bitrate could be negative. So use an adequate type to store the result and avoid overflows. After that this needs range check adjustments, but this should make the code less opaque. Signed-off-by: Stefan Wahren <wahrenst@gmx.net> Reviewed-by: Frank Li <Frank.Li@nxp.com> Link: https://patch.msgid.link/20240930093056.93418-2-wahrenst@gmx.net Signed-off-by: Mark Brown <broonie@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 3946e07 commit 8455230

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

drivers/spi/spi-fsl-lpspi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
315315
static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
316316
{
317317
struct lpspi_config config = fsl_lpspi->config;
318-
unsigned int perclk_rate, scldiv, div;
318+
unsigned int perclk_rate, div;
319319
u8 prescale_max;
320320
u8 prescale;
321+
int scldiv;
321322

322323
perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
323324
prescale_max = fsl_lpspi->devtype_data->prescale_max;
@@ -338,13 +339,13 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
338339

339340
for (prescale = 0; prescale <= prescale_max; prescale++) {
340341
scldiv = div / (1 << prescale) - 2;
341-
if (scldiv < 256) {
342+
if (scldiv >= 0 && scldiv < 256) {
342343
fsl_lpspi->config.prescale = prescale;
343344
break;
344345
}
345346
}
346347

347-
if (scldiv >= 256)
348+
if (scldiv < 0 || scldiv >= 256)
348349
return -EINVAL;
349350

350351
writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),

0 commit comments

Comments
 (0)