* [PATCH v2 0/6] spi: spi-sun4i: assorted fixes
@ 2026-09-07 15:31 Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 1/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH Jonas Rebmann
` (5 more replies)
0 siblings, 6 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann,
Tobias Schramm
This is a series of patches for the spi-sun4i driver, some of which have
been sitting around for a while.
1-4 are fixes and 5 is a small patch that introduces reporting of the
effective SPI speed.
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
Changes in v2:
- Add Patch 5/6 to fix division by zero potential in 6/6
- 3/6: Correctly subtract 1 to gain the register value for SUN4I_CLK_CTL_CDR1
- Mitigate race condition potential in 4/6 by disabling and syncing
interrupts before sun4i_spi_drain_fifo() in the timeout path
- Link to v1: https://patch.msgid.link/20260902-spi-sun4i-fixes-v1-0-19985ef75673@pengutronix.de
---
Jonas Rebmann (1):
spi: spi-sun4i: sun4i_spi_transfer_one(): add error handling at clock setup
Marc Kleine-Budde (5):
spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH
spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround
spi: spi-sun4i: sun4i_spi_transfer_one(): fix setting of clock rate
spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
spi: spi-sun4i: sun4i_spi_transfer_one(): report effectively used speed_hz of transfer
drivers/spi/spi-sun4i.c | 68 +++++++++++++++++++++++++++++--------------------
1 file changed, 40 insertions(+), 28 deletions(-)
---
base-commit: 89a312991dc6e638a36adc43ccb91dbc25504c04
change-id: 20260902-spi-sun4i-fixes-1a50e880dc7b
Best regards,
--
Jonas Rebmann <jre@pengutronix.de>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 2/6] spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround Jonas Rebmann
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann
From: Marc Kleine-Budde <mkl@pengutronix.de>
In commit 6d9fe44bd73d ("spi: sun4i: fix FIFO limit"), the TX-FIFO is
filled max to SUN4I_FIFO_DEPTH - 1 (= 63) bytes to work around timeouts
observed on A10s SoCs.
Commit 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO
size") added support for transfers larger than the FIFO size. But this
commit only enabled the TX-FIFO empty interrupt for transfers larger
than the FIFO (= 64) bytes.
This breaks transfers with exactly 64 bytes: the TX-FIFO is only filled
with 63 bytes but the interrupt to refill the FIFO is not triggered. The
problem can be reproduced with the following command:
| spidev_test -D /dev/spidev0.1 -S 64 -s 20000000 -I 1
|
| [ 7797.548745] spi_master spi0: spi0.1: timeout transferring 64 bytes@20000000Hz for 110(100)ms
| [ 7797.557237] spidev spi0.1: SPI transfer failed: -110
| [ 7797.562308] spi_master spi0: failed to transfer one message from queue
| [ 7797.568936] spi_master spi0: noqueue transfer failed
To fix the problem enable the TX-FIFO interrupt if the total TX length
is larger than SUN4I_FIFO_DEPTH - 1 (= 63) bytes.
Fixes: 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO size")
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index d5c16392cd4d..2e2324453905 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -322,7 +322,7 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TC |
SUN4I_INT_CTL_RF_F34);
/* Only enable Tx FIFO interrupt if we really need it */
- if (tx_len > SUN4I_FIFO_DEPTH)
+ if (tx_len > SUN4I_FIFO_DEPTH - 1)
sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TF_E34);
/* Start the transfer */
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/6] spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 1/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 3/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix setting of clock rate Jonas Rebmann
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann
From: Marc Kleine-Budde <mkl@pengutronix.de>
In commit 6d9fe44bd73d ("spi: sun4i: fix FIFO limit"), the TX FIFO is
filled max to SUN4I_FIFO_DEPTH - 1 (= 63) bytes to work around
timeouts observed on A10s SoCs.
Commit 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO
size") added support for transfers larger than the FIFO size. This
commit did not consider the A10 workaround (limit the TX FIFO fill
size to SUN4I_FIFO_DEPTH - 1 (= 63) bytes) for refilling the TX FIFO
in the IRQ handler.
To apply the workaround independent from where sun4i_spi_fill_fifo()
is called, remove the length argument from the function and directly
take the max fill level of SUN4I_FIFO_DEPTH - 1 into account when
calculating the free space in the FIFO.
Due to the lack of HW this patch has not been tested on an A10 SoC,
but on an A20 SoC. It was not possible to reproduce the timeout on the
A20.
Fixes: 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO size")
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index 2e2324453905..3649bcabcc9a 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -142,15 +142,21 @@ static inline void sun4i_spi_drain_fifo(struct sun4i_spi *sspi, int len)
}
}
-static inline void sun4i_spi_fill_fifo(struct sun4i_spi *sspi, int len)
+static inline void sun4i_spi_fill_fifo(struct sun4i_spi *sspi)
{
u32 cnt;
+ int len;
u8 byte;
- /* See how much data we can fit */
- cnt = SUN4I_FIFO_DEPTH - sun4i_spi_get_tx_fifo_count(sspi);
+ /*
+ * See how much data we can fit
+ *
+ * Filling the FIFO fully causes timeout for some reason
+ * at least on spi2 on A10s
+ */
+ cnt = SUN4I_FIFO_DEPTH - 1 - sun4i_spi_get_tx_fifo_count(sspi);
- len = min3(len, (int)cnt, sspi->len);
+ len = min_t(int, cnt, sspi->len);
while (len--) {
byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
@@ -311,12 +317,8 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
sun4i_spi_write(sspi, SUN4I_BURST_CNT_REG, SUN4I_BURST_CNT(tfr->len));
sun4i_spi_write(sspi, SUN4I_XMIT_CNT_REG, SUN4I_XMIT_CNT(tx_len));
- /*
- * Fill the TX FIFO
- * Filling the FIFO fully causes timeout for some reason
- * at least on spi2 on A10s
- */
- sun4i_spi_fill_fifo(sspi, SUN4I_FIFO_DEPTH - 1);
+ /* Fill the TX FIFO */
+ sun4i_spi_fill_fifo(sspi);
/* Enable the interrupts */
sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TC |
@@ -373,7 +375,7 @@ static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
/* Transmit FIFO 3/4 empty */
if (status & SUN4I_INT_CTL_TF_E34) {
- sun4i_spi_fill_fifo(sspi, SUN4I_FIFO_DEPTH);
+ sun4i_spi_fill_fifo(sspi);
if (!sspi->len)
/* nothing left to transmit */
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix setting of clock rate
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 1/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 2/6] spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt Jonas Rebmann
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann
From: Marc Kleine-Budde <mkl@pengutronix.de>
A SPI transfer defines the _maximum_ speed of the SPI transfer. However
the driver doesn't take into account that the clock divider is always
rounded down (due to integer arithmetic). This results in a too high
clock rate for the SPI transfer.
E.g.: with an mclk_rate of 24 MHz and an SPI transfer speed of 10 MHz,
the original code calculates a reg of "0", which results in an effective
divider of "2" and a 12 MHz clock for the SPI transfer.
Use DIV_ROUND_UP() instead of a plain integer division to fix the
problem.
While there simplify the divider calculation for the CDR1 case, use
order_base_2() instead of two ilog2() calculations.
Fixes: b5f6517948cc ("spi: sunxi: Add Allwinner A10 SPI controller driver")
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index 3649bcabcc9a..8a9dcd3b6b8f 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -212,7 +212,7 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
struct spi_transfer *tfr)
{
struct sun4i_spi *sspi = spi_controller_get_devdata(host);
- unsigned int mclk_rate, div;
+ unsigned int mclk_rate, div, div_cdr1, div_cdr2;
unsigned long time_left;
unsigned int start, end, tx_time;
unsigned int tx_len = 0;
@@ -296,15 +296,13 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
* First try CDR2, and if we can't reach the expected
* frequency, fall back to CDR1.
*/
- div = mclk_rate / (2 * tfr->speed_hz);
- if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
- if (div > 0)
- div--;
-
- reg = SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
+ div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
+ div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
+ if (div_cdr2 <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
+ reg = SUN4I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN4I_CLK_CTL_DRS;
} else {
- div = ilog2(mclk_rate) - ilog2(tfr->speed_hz);
- reg = SUN4I_CLK_CTL_CDR1(div);
+ div = min(SUN4I_CLK_CTL_CDR1_MASK + 1, order_base_2(div_cdr1));
+ reg = SUN4I_CLK_CTL_CDR1(div - 1);
}
sun4i_spi_write(sspi, SUN4I_CLK_CTL_REG, reg);
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
` (2 preceding siblings ...)
2026-09-07 15:31 ` [PATCH v2 3/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix setting of clock rate Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
2026-09-07 15:44 ` sashiko-bot
2026-09-07 15:31 ` [PATCH v2 5/6] spi: spi-sun4i: sun4i_spi_transfer_one(): add error handling at clock setup Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 6/6] spi: spi-sun4i: sun4i_spi_transfer_one(): report effectively used speed_hz of transfer Jonas Rebmann
5 siblings, 1 reply; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann,
Tobias Schramm
From: Marc Kleine-Budde <mkl@pengutronix.de>
In commit 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO
size"), support for transfers larger than the FIFO size was added.
This commit moves the draining of the RX-FIFO from
sun4i_spi_transfer_one() (after completion of the transfer) to the IRQ
handler when the IRQ "transfer complete" is handled. In addition, the
IRQ "RX-FIFO 3/4 full" is activated for all transfers.
However, this does not take into account that the RX-IRQ for transfers
that exceed 3/4 of the FIFO size is still pending after the IRQ
"transfer complete" has been processed. All interrupt sources are only
deactivated after the wait_for_completion_timeout() in
sun4i_spi_transfer_one().
This opens a race window for "RX-FIFO 3/4 full" interrupts to come.
The sequence is as follows:
| sun4i_spi_transfer_one()
| sun4i_spi_fill_fifo() // fill TX-FIFO with 48 bytes
| // enable RX-FIFO 3/4 full IRQ
| wait_for_completion_timeout();
|
| // SPI controller transfers 48 bytes
| // SPI controller issues "transfer complete" and "RX-FIFO 3/4 full" IRQ
|
| // IRQ handler start
| sun4i_spi_handler()
| // ACK "transfer complete" IRQ
| sun4i_spi_drain_fifo();
| complete(); ----.
| return IRQ_HANDLED; \
| // IRQ handler end \__ race
| / window
| // wait_for_completion_timeout() continues /
| // disable all IRQ sources ----'
Avoid the race condition by disabling all interrupts when handling the
"transfer complete" IRQ and before calling complete(). Also move the
draining of the RX-FIFO back into sun4i_spi_transfer_one() where it
was before commit 196737912da5 ("spi: sun4i: Allow transfers larger than
FIFO size").
This has the added benefit of spending a little less time in the IRQ
handler.
Cc: Tobias Schramm <t.schramm@manjaro.org>
Fixes: 196737912da5 ("spi: sun4i: Allow transfers larger than FIFO size")
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index 8a9dcd3b6b8f..ea8be0170fbf 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -82,6 +82,8 @@ struct sun4i_spi {
struct completion done;
+ int irq;
+
const u8 *tx_buf;
u8 *rx_buf;
int len;
@@ -333,6 +335,7 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
start = jiffies;
time_left = wait_for_completion_timeout(&sspi->done,
msecs_to_jiffies(tx_time));
+
end = jiffies;
if (!time_left) {
dev_warn(&host->dev,
@@ -340,12 +343,11 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
dev_name(&spi->dev), tfr->len, tfr->speed_hz,
jiffies_to_msecs(end - start), tx_time);
ret = -ETIMEDOUT;
- goto out;
+ sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
+ synchronize_irq(sspi->irq);
}
-
-out:
- sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
+ sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
return ret;
}
@@ -357,8 +359,7 @@ static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
/* Transfer complete */
if (status & SUN4I_INT_CTL_TC) {
- sun4i_spi_write(sspi, SUN4I_INT_STA_REG, SUN4I_INT_CTL_TC);
- sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
+ sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
complete(&sspi->done);
return IRQ_HANDLED;
}
@@ -456,6 +457,7 @@ static int sun4i_spi_probe(struct platform_device *pdev)
return ret;
}
+ sspi->irq = irq;
sspi->host = host;
host->max_speed_hz = 100 * 1000 * 1000;
host->min_speed_hz = 3 * 1000;
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 5/6] spi: spi-sun4i: sun4i_spi_transfer_one(): add error handling at clock setup
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
` (3 preceding siblings ...)
2026-09-07 15:31 ` [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 6/6] spi: spi-sun4i: sun4i_spi_transfer_one(): report effectively used speed_hz of transfer Jonas Rebmann
5 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann
Bail out if clk_get_rate() or clk_set_rate() fail during
sun4i_spi_transfer_one(). This might happen if the clock isn't known yet
or fails to set the requested rate.
This also ensures that mclk_rate is nonzero and can be safely divided by
in a future commit.
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index ea8be0170fbf..94e1c7bbd632 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -279,9 +279,17 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
/* Ensure that we have a parent clock fast enough */
mclk_rate = clk_get_rate(sspi->mclk);
+ if (!mclk_rate)
+ return -EINVAL;
+
if (mclk_rate < (2 * tfr->speed_hz)) {
- clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
+ ret = clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
+ if (ret)
+ return ret;
+
mclk_rate = clk_get_rate(sspi->mclk);
+ if (!mclk_rate)
+ return -EINVAL;
}
/*
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 6/6] spi: spi-sun4i: sun4i_spi_transfer_one(): report effectively used speed_hz of transfer
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
` (4 preceding siblings ...)
2026-09-07 15:31 ` [PATCH v2 5/6] spi: spi-sun4i: sun4i_spi_transfer_one(): add error handling at clock setup Jonas Rebmann
@ 2026-09-07 15:31 ` Jonas Rebmann
5 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 15:31 UTC (permalink / raw)
To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
Maxime Ripard, Alexandru Gagniuc, Olliver Schinagl
Cc: linux-spi, linux-arm-kernel, linux-sunxi, linux-kernel,
Mark Brown, kernel, Marc Kleine-Budde, Jonas Rebmann
From: Marc Kleine-Budde <mkl@pengutronix.de>
Report the effectively used speed_hz for the transfer by setting
tfr->effective_speed_hz.
For more information see commit 5d7e2b5ed585 ("spi: core: allow
reporting the effectivly used speed_hz for a transfer"), which adds this
feature to the SPI core.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
drivers/spi/spi-sun4i.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
index 94e1c7bbd632..59941b38b757 100644
--- a/drivers/spi/spi-sun4i.c
+++ b/drivers/spi/spi-sun4i.c
@@ -310,9 +310,11 @@ static int sun4i_spi_transfer_one(struct spi_controller *host,
div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
if (div_cdr2 <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
reg = SUN4I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN4I_CLK_CTL_DRS;
+ tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2);
} else {
div = min(SUN4I_CLK_CTL_CDR1_MASK + 1, order_base_2(div_cdr1));
reg = SUN4I_CLK_CTL_CDR1(div - 1);
+ tfr->effective_speed_hz = mclk_rate / (1 << div);
}
sun4i_spi_write(sspi, SUN4I_CLK_CTL_REG, reg);
--
2.55.0.123.gf60db8d575
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
2026-09-07 15:31 ` [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt Jonas Rebmann
@ 2026-09-07 15:44 ` sashiko-bot
2026-09-07 17:14 ` Jonas Rebmann
0 siblings, 1 reply; 9+ messages in thread
From: sashiko-bot @ 2026-09-07 15:44 UTC (permalink / raw)
To: Jonas Rebmann; +Cc: linux-sunxi
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Missing Interrupt Acknowledgment causes race condition and data corruption.
Pre-existing issues:
- [High] Interrupt requested before clocks enabled and synchronization primitives initialized.
--
commit 56eb9747daccb855b98e335a87134c61f8b8487e
Author: Marc Kleine-Budde <mkl@pengutronix.de>
spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
This commit moves RX-FIFO draining back to sun4i_spi_transfer_one() and
disables all interrupts in the IRQ handler when processing the transfer
complete interrupt to close a race window.
> diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
> index 8a9dcd3b6b8f4..ea8be0170fbfd 100644
> --- a/drivers/spi/spi-sun4i.c
> +++ b/drivers/spi/spi-sun4i.c
[ ... ]
> @@ -357,8 +359,7 @@ static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
>
> /* Transfer complete */
> if (status & SUN4I_INT_CTL_TC) {
> - sun4i_spi_write(sspi, SUN4I_INT_STA_REG, SUN4I_INT_CTL_TC);
> - sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
> + sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
[Severity: High]
Does this change leave the Transfer Complete (TC) bit active in the status
register?
It looks like replacing the write to SUN4I_INT_STA_REG with a write to
SUN4I_INT_CTL_REG masks the interrupt, but fails to acknowledge and clear
the pending status bit.
If a spurious interrupt fires during the setup of a new transfer, could this
cause the new transfer to complete prematurely? For example, in
sun4i_spi_transfer_one(), there is a window between reinitializing the
completion and clearing the status register:
reinit_completion(&sspi->done);
sspi->tx_buf = tfr->tx_buf;
sspi->rx_buf = tfr->rx_buf;
sspi->len = tfr->len;
/* Clear pending interrupts */
sun4i_spi_write(sspi, SUN4I_INT_STA_REG, ~0);
If a spurious interrupt arrives just before SUN4I_INT_STA_REG is cleared,
the handler will read the stale TC bit and call complete(&sspi->done). The
wait_for_completion_timeout() would then return immediately, potentially
leading to use-after-free of DMA buffers or silent data corruption since the
hardware might still be active.
> complete(&sspi->done);
> return IRQ_HANDLED;
> }
[ ... ]
> @@ -456,6 +457,7 @@ static int sun4i_spi_probe(struct platform_device *pdev)
> return ret;
> }
>
> + sspi->irq = irq;
[Severity: High]
This is a pre-existing issue, but looking at the surrounding probe function,
is it safe to request the IRQ before the clocks are enabled and the
completion is initialized?
In sun4i_spi_probe(), devm_request_irq() is called just above this hunk,
enabling the IRQ line. However, the completion structure is initialized much
later via init_completion(&sspi->done), and the AHB/module clocks required
for MMIO access are only enabled during sun4i_spi_runtime_resume().
If the bootloader left the interrupt asserted (e.g., during kexec) or a
spurious interrupt fires before initialization is complete, the handler
sun4i_spi_handler() could execute early. Accessing MMIO registers without
the AHB clock enabled often causes a synchronous external abort on ARM.
Additionally, calling complete() on an uninitialized completion structure
dereferences an invalid waitqueue lock, resulting in an Oops.
> sspi->host = host;
> host->max_speed_hz = 100 * 1000 * 1000;
> host->min_speed_hz = 3 * 1000;
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260907-spi-sun4i-fixes-v2-0-7e805662b9bd@pengutronix.de?part=4
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
2026-09-07 15:44 ` sashiko-bot
@ 2026-09-07 17:14 ` Jonas Rebmann
0 siblings, 0 replies; 9+ messages in thread
From: Jonas Rebmann @ 2026-09-07 17:14 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-sunxi, kernel@pengutronix.de, Marc Kleine-Budde
Dear humans,
On 2026-09-07 17:44, sashiko-bot@kernel.org wrote:
> spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt
>
> This commit moves RX-FIFO draining back to sun4i_spi_transfer_one() and
> disables all interrupts in the IRQ handler when processing the transfer
> complete interrupt to close a race window.
>
>> diff --git a/drivers/spi/spi-sun4i.c b/drivers/spi/spi-sun4i.c
>> index 8a9dcd3b6b8f4..ea8be0170fbfd 100644
>> --- a/drivers/spi/spi-sun4i.c
>> +++ b/drivers/spi/spi-sun4i.c
>
> [ ... ]
>
>> @@ -357,8 +359,7 @@ static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
>>
>> /* Transfer complete */
>> if (status & SUN4I_INT_CTL_TC) {
>> - sun4i_spi_write(sspi, SUN4I_INT_STA_REG, SUN4I_INT_CTL_TC);
>> - sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
>> + sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
>
> [Severity: High]
> Does this change leave the Transfer Complete (TC) bit active in the status
> register?
Temporarily.
> It looks like replacing the write to SUN4I_INT_STA_REG with a write to
> SUN4I_INT_CTL_REG masks the interrupt, but fails to acknowledge and clear
> the pending status bit.
>
> If a spurious interrupt fires during the setup of a new transfer, could this
> cause the new transfer to complete prematurely? For example, in
> sun4i_spi_transfer_one(), there is a window between reinitializing the
> completion and clearing the status register:
>
> reinit_completion(&sspi->done);
> sspi->tx_buf = tfr->tx_buf;
> sspi->rx_buf = tfr->rx_buf;
> sspi->len = tfr->len;
>
> /* Clear pending interrupts */
> sun4i_spi_write(sspi, SUN4I_INT_STA_REG, ~0);
>
> If a spurious interrupt arrives just before SUN4I_INT_STA_REG is cleared,
> the handler will read the stale TC bit and call complete(&sspi->done). The
> wait_for_completion_timeout() would then return immediately, potentially
> leading to use-after-free of DMA buffers or silent data corruption since the
> hardware might still be active.
Zeroing INT_CTL_REG disables all interrupts.
reinit_completion() does *not* enable interrupts early in
sun4i_spi_transfer_one():
static inline void reinit_completion(struct completion *x)
{
x->done = 0;
}
TC in SUN4I_INT_STA_REG is then write-1-cleared:
sun4i_spi_write(sspi, SUN4I_INT_STA_REG, ~0);
Only after that, later in sun4i_spi_transfer_one(), interrupts are
enabled in sun4i_spi_transfer_one():
/* Enable the interrupts */
sun4i_spi_enable_interrupt(sspi, SUN4I_INT_CTL_TC |
SUN4I_INT_CTL_RF_F34);
Hence, the omission of clearing the TC bit register in
sun4i_spi_handler() poses no risk of the described issue.
Regards,
Jonas
--
Pengutronix e.K. | Jonas Rebmann |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-09-07 17:14 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-07 15:31 [PATCH v2 0/6] spi: spi-sun4i: assorted fixes Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 1/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix transfers for SUN4I_FIFO_DEPTH Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 2/6] spi: spi-sun4i: sun4i_spi_fill_fifo(): always apply A10 FIFO fill level workaround Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 3/6] spi: spi-sun4i: sun4i_spi_transfer_one(): fix setting of clock rate Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 4/6] spi: spi-sun4i: sun4i_spi_handler(): fix race condition between transfer completion and RX FIFO interrupt Jonas Rebmann
2026-09-07 15:44 ` sashiko-bot
2026-09-07 17:14 ` Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 5/6] spi: spi-sun4i: sun4i_spi_transfer_one(): add error handling at clock setup Jonas Rebmann
2026-09-07 15:31 ` [PATCH v2 6/6] spi: spi-sun4i: sun4i_spi_transfer_one(): report effectively used speed_hz of transfer Jonas Rebmann
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.