[PATCH v2] spi: atmel: poll short PIO transfers

From: Jens Emil Schulz Østergaard

Date: Fri Nov 13 2026 - 10:40:44 EST


Every PIO transfer enables the controller interrupt and sleeps in
wait_for_completion_timeout() until the interrupt handler has drained the
receive data. For a transfer of a few bytes, the interrupt, the
completion and the two context switches take several times longer than
the transfer on the wire. Devices that do their register IO over SPI,
such as SPI attached Ethernet switches, issue one message of a few short
transfers per register access, so this overhead dominates their register
access time.

Busy-wait on the status register instead of enabling the interrupt when
the transfer is estimated to take less than 30 us, which is the limit
spi-bcm2835, spi-imx and spi-stm32 use. The estimate uses the effective
clock rate and the word delay.

An earlier interrupt driven transfer, for example one that timed out,
can leave the receive and overrun interrupts enabled. Such an interrupt
can be delivered while a poll is in progress, and with PREEMPT_RT its
threaded handler can run at any later time.

Set the poll flag and mask these interrupts under the controller lock
before a polled transfer is started. Take the lock in the interrupt
handler before it reads the status register, and return without touching
the controller while the poll flag is set, so that the handler cannot
consume the status we are polling for.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@xxxxxxxxxxxxx>
---
Changes in v2:
- Drop the polling_limit_us module parameter, poll below a fixed 30 us
- Set the poll flag and mask the interrupts under the lock, and let the
interrupt handler take the lock and return while a poll is in progress
- Drop the remaining-bytes check in the poll, the interrupt handler no
longer runs while a poll is in progress
- Link to v1: https://lore.kernel.org/r/20261112-spi-atmel-poll-short-pio-transfers-v1-1-240ccc868cc3@xxxxxxxxxxxxx
---
drivers/spi/spi-atmel.c | 85 +++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 79 insertions(+), 6 deletions(-)

diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c
index ffc1292b08c1..ea89a75850b6 100644
--- a/drivers/spi/spi-atmel.c
+++ b/drivers/spi/spi-atmel.c
@@ -234,6 +234,8 @@
*/
#define DMA_MIN_BYTES 16

+#define POLL_LIMIT_US 30
+
#define AUTOSUSPEND_TIMEOUT 2000

struct atmel_spi_caps {
@@ -274,6 +276,7 @@ struct atmel_spi {

bool use_dma;
bool use_pdc;
+ bool poll;

bool keep_cs;

@@ -706,7 +709,8 @@ static void atmel_spi_next_xfer_single(struct spi_controller *host,
xfer->bits_per_word);

/* Enable relevant interrupts */
- spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
+ if (!as->poll)
+ spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
}

/*
@@ -776,7 +780,8 @@ static void atmel_spi_next_xfer_fifo(struct spi_controller *host,
* Enable RX FIFO Threshold Flag interrupt to be notified about
* transfer completion.
*/
- spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES));
+ if (!as->poll)
+ spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES));
}

/*
@@ -1151,6 +1156,13 @@ atmel_spi_pio_interrupt(int irq, void *dev_id)
struct spi_transfer *xfer;
int ret = IRQ_NONE;

+ atmel_spi_lock(as);
+
+ if (as->poll) {
+ atmel_spi_unlock(as);
+ return IRQ_NONE;
+ }
+
imr = spi_readl(as, IMR);
status = spi_readl(as, SR);
pending = status & imr;
@@ -1178,8 +1190,6 @@ atmel_spi_pio_interrupt(int irq, void *dev_id)
complete(&as->xfer_completion);

} else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) {
- atmel_spi_lock(as);
-
if (as->current_remaining_bytes) {
ret = IRQ_HANDLED;
xfer = as->current_transfer;
@@ -1189,14 +1199,14 @@ atmel_spi_pio_interrupt(int irq, void *dev_id)

complete(&as->xfer_completion);
}
-
- atmel_spi_unlock(as);
} else {
WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
ret = IRQ_HANDLED;
spi_writel(as, IDR, pending);
}

+ atmel_spi_unlock(as);
+
return ret;
}

@@ -1375,6 +1385,56 @@ static void atmel_spi_set_cs(struct spi_device *spi, bool enable)

}

+static bool atmel_spi_use_poll(struct atmel_spi *as,
+ struct spi_transfer *xfer)
+{
+ u32 hz = xfer->effective_speed_hz ?: xfer->speed_hz;
+ int word_delay_ns;
+ u32 words;
+ u64 us;
+
+ if (as->use_pdc || atmel_spi_use_dma(as, xfer) || !hz)
+ return false;
+
+ us = div_u64((u64)xfer->len * BITS_PER_BYTE * USEC_PER_SEC, hz);
+
+ word_delay_ns = spi_delay_to_ns(&xfer->word_delay, xfer);
+ if (word_delay_ns > 0) {
+ words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word,
+ BITS_PER_BYTE);
+ us += div_u64((u64)words * word_delay_ns, NSEC_PER_USEC);
+ }
+
+ return us < POLL_LIMIT_US;
+}
+
+static int atmel_spi_poll_xfer(struct atmel_spi *as,
+ struct spi_transfer *xfer)
+{
+ u32 done = as->fifo_size ? SPI_BIT(RXFTHF) : SPI_BIT(RDRF);
+ u32 status;
+ int ret;
+
+ ret = readl_poll_timeout(as->regs + SPI_SR, status,
+ status & (done | SPI_BIT(OVRES)),
+ 0, POLL_LIMIT_US * 10);
+ if (ret) {
+ dev_err(&as->pdev->dev, "spi transfer timeout\n");
+ return -EIO;
+ }
+
+ if (status & SPI_BIT(OVRES)) {
+ dev_warn(&as->pdev->dev, "overrun\n");
+ return -EIO;
+ }
+
+ atmel_spi_lock(as);
+ atmel_spi_pump_pio_data(as, xfer);
+ atmel_spi_unlock(as);
+
+ return 0;
+}
+
static int atmel_spi_one_transfer(struct spi_controller *host,
struct spi_device *spi,
struct spi_transfer *xfer)
@@ -1409,9 +1469,15 @@ static int atmel_spi_one_transfer(struct spi_controller *host,

atmel_spi_set_xfer_speed(as, spi, xfer);

+ atmel_spi_lock(as);
as->done_status = 0;
as->current_transfer = xfer;
as->current_remaining_bytes = xfer->len;
+ as->poll = atmel_spi_use_poll(as, xfer);
+ if (as->poll)
+ spi_writel(as, IDR, (as->fifo_size ? SPI_BIT(RXFTHF) :
+ SPI_BIT(RDRF)) | SPI_BIT(OVRES));
+ atmel_spi_unlock(as);
while (as->current_remaining_bytes) {
reinit_completion(&as->xfer_completion);

@@ -1437,6 +1503,13 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
atmel_spi_lock(as);
atmel_spi_next_xfer_pio(host, xfer);
atmel_spi_unlock(as);
+
+ if (as->poll) {
+ as->done_status = atmel_spi_poll_xfer(as, xfer);
+ if (as->done_status)
+ break;
+ continue;
+ }
}

dma_timeout = msecs_to_jiffies(spi_controller_xfer_timeout(host, xfer));

---
base-commit: 2bc7eb398cd0f89fd2331d5a1a8fe7ed17addd35
change-id: 20261112-spi-atmel-poll-short-pio-transfers-bd8fcb9c19bb

Best regards,
--
Jens Emil Schulz Østergaard <jensemil.schulzostergaard@xxxxxxxxxxxxx>