[PATCH] spi: spacemit: avoid 32-bit integer overflow warning
Alex Elder
elder at riscstar.com
Tue May 5 11:21:11 PDT 2026
On 5/5/26 12:57 PM, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd at arndb.de>
>
> Calculating the bit rate in nanoseconds does not fit into a 32-bit 'int'
> type, as gcc-16 points out:
>
> drivers/spi/spi-spacemit-k1.c: In function 'k1_spi_set_speed':
> drivers/spi/spi-spacemit-k1.c:393:38: error: integer overflow in expression of type 'long int' results in '-589934592' [-Werror=overflow]
> 393 | nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
>
> Cast the value to a 64-bit constant to force the longer type for the
> calculation.
>
> Fixes: efcd8b9d1111 ("spi: spacemit: introduce SpacemiT K1 SPI controller driver")
> Signed-off-by: Arnd Bergmann <arnd at arndb.de>
Thanks Arnd. I diagnosed that when I first saw the warning and my
fix was identical to yours. I believe Mark already merged the
fix.
-Alex
> ----
> There is probably a way to rework the algorithm to avoid both the 64-bit
> multiplication and the following division, but I kept this as a minimal
> change.
> ---
> drivers/spi/spi-spacemit-k1.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi-spacemit-k1.c b/drivers/spi/spi-spacemit-k1.c
> index 99db429db0b2..215fe66d27b4 100644
> --- a/drivers/spi/spi-spacemit-k1.c
> +++ b/drivers/spi/spi-spacemit-k1.c
> @@ -390,7 +390,7 @@ static int k1_spi_set_speed(struct k1_spi_driver_data *drv_data,
> * ticks_per_word = BITS_PER_BYTE * drv_data->bytes;
> * We do the divide last for better accuracy.
> */
> - nsec_per_word = NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
> + nsec_per_word = (u64)NSEC_PER_SEC * BITS_PER_BYTE * drv_data->bytes;
> nsec_per_word = DIV_ROUND_UP_ULL(nsec_per_word, drv_data->rate);
>
> /*
More information about the linux-riscv
mailing list