Skip to contentSkip to conversation

spi: mt65xx: set host->max_speed_hz in probe

PATCH New Ilia Snezhkov 1 participant 1 message Nov 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 spi: mt65xx: set host->max_speed_hz in probe

spi-mt65xx leaves host->max_speed_hz unset (0). When a client device omits spi-max-frequency in DT, spi_setup() keeps spi->max_speed_hz == 0, and transfers or spi-mem ops without an explicit speed reach mtk_spi_prepare_transfer() with speed_hz == 0 and mtk_spi_transfer_wait() with mem->spi->max_speed_hz == 0, causing division by zero in both DIV_ROUND_UP(mdata->spi_clk_hz, speed_hz) and
div_u64(ms, mem->spi->max_speed_hz).

Validate that spi_clk_hz is at least 2 Hz and set host->max_speed_hz to mdata->spi_clk_hz / 2 (the controller's maximum SCK rate at div = 1, sck_time = 1) so the SPI core populates spi->max_speed_hz, xfer->speed_hz, and op->max_freq for all transfer paths.

Fixes a568231f4632 ("spi: mediatek: Add spi bus for Mediatek MT8173")

---
Changes in v2:
- Set host->max_speed_hz = mdata->spi_clk_hz / 2 in mtk_spi_probe() (and reject spi_clk_hz < 2) instead of only guarding speed_hz in mtk_spi_prepare_transfer(), covering mtk_spi_transfer_wait() as well (suggested by Roman Vivchar).
- Move AI coding assistant disclosure below ---.
- Link to v1: https://lore.kernel.org/r/[email protected]

Found and drafted with an AI coding assistant.

 drivers/spi/spi-mt65xx.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/spi/spi-mt65xx.c +8 -0
index 8b4deef..7e94b4d 100644
@@ -1285,6 +1285,14 @@ static int mtk_spi_probe(struct platform_device *pdev)
 	}
 
 	mdata->spi_clk_hz = clk_get_rate(mdata->spi_clk);
+	if (mdata->spi_clk_hz < 2) {
+		clk_disable_unprepare(mdata->spi_clk);
+		clk_disable_unprepare(mdata->spi_hclk);
+		return dev_err_probe(dev, -EINVAL, "invalid spi-clk rate %u\n",
+				     mdata->spi_clk_hz);
+	}
+
+	host->max_speed_hz = mdata->spi_clk_hz / 2;
 
 	if (mdata->dev_comp->no_need_unprepare) {
 		clk_disable(mdata->spi_clk);
Signature · 1 line
2.54.0 (Apple Git-157)