[PATCH 5/5] spi: atmel: fix division by zero when transfer speed is zero
From: shao.mingyin
Date: Fri Nov 06 2026 - 10:50:09 EST
From: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
atmel_spi_one_transfer() calls atmel_spi_set_xfer_speed() for every
transfer. A device whose max_speed_hz is zero (no spi-max-frequency
property, or created through the spidev sysfs new_device interface
without a speed) leaves the transfer speed at zero, and the divider
calculation divides by it. spi_setup() would only clamp the speed if
host->max_speed_hz was set, which this driver does not do.
On cores without a hardware divider this reports "Division by zero in
kernel" and dumps a stack trace, and on cores with one the quotient is
silently zero; in both cases the existing "too high" check then rejects
the transfer.
Clamp the divisor to 1 Hz so the zero never reaches the division.
Fixes: d3b72c7e6bf3 ("spi: atmel: add support for changing message transfer speed")
Signed-off-by: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
---
diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c
index ffc896e4621a..deebf7afabbc 100644
--- a/drivers/spi/spi-atmel.c
+++ b/drivers/spi/spi-atmel.c
@@ -922,8 +922,9 @@ static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
/*
* Calculate the lowest divider that satisfies the
* constraint, assuming div32/fdiv/mbz == 0.
+ * A device with max_speed_hz == 0 must not divide by zero.
*/
- scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
+ scbr = DIV_ROUND_UP(bus_hz, max_t(u32, xfer->speed_hz, 1));
/*
* If the resulting divider doesn't fit into the