Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] spi: mt65xx: set host->max_speed_hz in probe
@ 2026-11-27  2:49 Ilia Snezhkov via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-27  2:49 UTC (permalink / raw)
  To: broonie
  Cc: matthias.bgg, angelogioacchino.delregno, Roman Vivchar, linux-spi,
	linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

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")
Suggested-by: Roman Vivchar <rva333@protonmail.com>
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
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/20261126003314.49090-1-iasnezhkov@gmail.com

Found and drafted with an AI coding assistant.

 drivers/spi/spi-mt65xx.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/spi/spi-mt65xx.c b/drivers/spi/spi-mt65xx.c
index 8b4deef..7e94b4d 100644
--- a/drivers/spi/spi-mt65xx.c
+++ b/drivers/spi/spi-mt65xx.c
@@ -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);
-- 
2.54.0 (Apple Git-157)





^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-27  3:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  2:49 [PATCH v2] spi: mt65xx: set host->max_speed_hz in probe Ilia Snezhkov via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox