[U-Boot] [PATCH] spi: kirkwood: avoid divide by zero crash

Calling .set_speed with zero speed is definitely a bug. Return an error value to handle that gracefully instead of crashing.
Signed-off-by: Baruch Siach baruch@tkos.co.il --- drivers/spi/kirkwood_spi.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/drivers/spi/kirkwood_spi.c b/drivers/spi/kirkwood_spi.c index 0c6bd295cde9..9a27ac36822e 100644 --- a/drivers/spi/kirkwood_spi.c +++ b/drivers/spi/kirkwood_spi.c @@ -257,6 +257,8 @@ static int mvebu_spi_set_speed(struct udevice *bus, uint hz) struct kwspi_registers *reg = plat->spireg; u32 data;
+ if (hz == 0) + return -EINVAL; /* calculate spi clock prescaller using max_hz */ data = ((CONFIG_SYS_TCLK / 2) / hz) + 0x10; data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;

On Thu, Nov 9, 2017 at 5:09 PM, Baruch Siach baruch@tkos.co.il wrote:
Calling .set_speed with zero speed is definitely a bug. Return an error value to handle that gracefully instead of crashing.
Signed-off-by: Baruch Siach baruch@tkos.co.il
drivers/spi/kirkwood_spi.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/drivers/spi/kirkwood_spi.c b/drivers/spi/kirkwood_spi.c index 0c6bd295cde9..9a27ac36822e 100644 --- a/drivers/spi/kirkwood_spi.c +++ b/drivers/spi/kirkwood_spi.c @@ -257,6 +257,8 @@ static int mvebu_spi_set_speed(struct udevice *bus, uint hz) struct kwspi_registers *reg = plat->spireg; u32 data;
if (hz == 0)
return -EINVAL;
This isn't true, if hz 0 we should go with minimum divizer, usually tclk we can get it from dts as plat->frequency and check the same with speed to get the final speed.
thanks!
participants (2)
-
Baruch Siach
-
Jagan Teki