spi: axi-spi-engine: fix stale SYNC IRQ pending
Subsystems (2)
v3 cover spi: axi-spi-engine: fix stale SYNC IRQ pending
Both spi_engine_setup() and spi_engine_trigger_enable() send a SYNC(1) command and poll SYNC_ID, but never clear INT_PENDING[SYNC] afterward. This leaves a stale pending bit that fires spuriously when INT_SYNC is enabled later by some driver, causing premature transfer completion at low SPI clock frequencies (more likely). Fix both functions by clearing the pending bit after polling.
v3 summary:
* Added a dummy read after the clear command to flush the posted writes
before dropping the lock.
* Link to v2: https://lore.kernel.org/linux-spi/[email protected]/T/#t
v2 summary:
* Replaced "Link" tag for "Closes" tag.
* Included "Fixes" tag.
* Added new patch applying similar fix for the spi_engine_trigger_enable(),
as pointed out by David.
* While at it, replaced plain "1000" by "1 * USEC_PER_MSEC" for better
clarity.
* Link to v1: https://lore.kernel.org/linux-spi/04d51e99b8cddce51113db933d806e83848f04f5.1788313558.git.Jonathan.Santos@analog.com/T/#t
Jonathan Santos (2):
spi: axi-spi-engine: fix stale SYNC IRQ pending
spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable
drivers/spi/spi-axi-spi-engine.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
base-commit: 04434a1d0f311d76b1f15fa987918b471a0b8c6a
Signature · 1 line
2.34.1
PATCH v3 1/2 spi: axi-spi-engine: fix stale SYNC IRQ pending
spi_engine_setup() sends a SYNC(1) command and polls SYNC_ID to confirm it was parsed by the FPGA, but never clears the corresponding interrupt pending bit (INT_PENDING[SYNC]). When the first real SPI transfer starts and INT_SYNC is enabled, that stale pending bit fires immediately, causing the IRQ handler to see the leftover SYNC_ID from setup, match it against the current transfer's ID, and prematurely signal completion before the hardware finishes.
This race manifests at low SPI clock frequencies (~2-3 MHz), where the FPGA takes long enough to execute the transfer that handler is parsed before it finishes. At higher SCLK rates the transfer completes fast enough that the issue is masked.
Fix this by clearing INT_PENDING[SYNC] after the polled SYNC, ensuring no stale interrupt is left pending.
--- drivers/spi/spi-axi-spi-engine.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/spi/spi-axi-spi-engine.c +10 -2
@@ -887,6 +887,7 @@ static int spi_engine_setup(struct spi_device *device)struct spi_controller *host = device->controller;struct spi_engine *spi_engine =spi_controller_get_devdata (host);unsigned int reg; +int ret;if (device->mode &SPI_CS_HIGH ) spi_engine->cs_inv |=BIT (spi_get_chipselect (device,0 ));
@@ -922,8 +923,15 @@ static int spi_engine_setup(struct spi_device *device)writel_relaxed (SPI_ENGINE_CMD_SYNC (1 ), spi_engine->base +SPI_ENGINE_REG_CMD_FIFO ); -return readl_relaxed_poll_timeout (spi_engine->base +SPI_ENGINE_REG_SYNC_ID , - reg, reg ==1 ,1 ,1000 ); + ret =readl_relaxed_poll_timeout (spi_engine->base +SPI_ENGINE_REG_SYNC_ID , + reg, reg ==1 ,1 ,1 *USEC_PER_MSEC ); + +/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is later enabled. */ +writel_relaxed (SPI_ENGINE_INT_SYNC , spi_engine->base +SPI_ENGINE_REG_INT_PENDING ); +/* Read back to flush posted write. */ +readl_relaxed (spi_engine->base +SPI_ENGINE_REG_INT_PENDING ); + +return ret; } static int spi_engine_transfer_one_message(struct spi_controller *host,
Signature · 1 line
2.34.1
PATCH v3 2/2 spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable
In reply to Jonathan Santosspi_engine_trigger_enable() sends SYNC(1) and polls SYNC_ID, it never clears the INT_PENDING[SYNC] bit afterward. This leaves a stale pending interrupt that can fire spuriously when INT_SYNC is later enabled.
Simillarly to the fix on spi_engine_setup(), clear INT_PENDING[SYNC] after polling the SYNC_ID.
--- drivers/spi/spi-axi-spi-engine.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/spi/spi-axi-spi-engine.c +7 -1
@@ -1036,7 +1036,13 @@ static int spi_engine_trigger_enable(struct spi_offload *offload) spi_engine->base + SPI_ENGINE_REG_CMD_FIFO); ret =readl_relaxed_poll_timeout (spi_engine->base +SPI_ENGINE_REG_SYNC_ID , - reg, reg ==1 ,1 ,1000 ); + reg, reg ==1 ,1 ,1 *USEC_PER_MSEC ); + +/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is later enabled */ +writel_relaxed (SPI_ENGINE_INT_SYNC , spi_engine->base +SPI_ENGINE_REG_INT_PENDING ); +/* Read back to flush posted write. */ +readl_relaxed (spi_engine->base +SPI_ENGINE_REG_INT_PENDING ); +if (ret)return ret;
Signature · 1 line
2.34.1
Mark Brown
maintainerQuoted 10 lines · Jonathan Santos
spi: axi-spi-engine: fix stale SYNC IRQ pending
Both
spi_engine_setup()andspi_engine_trigger_enable()send a SYNC(1) command and pollSYNC_ID, but never clear INT_PENDING[SYNC] afterward. This leaves a stale pending bit that fires spuriously whenINT_SYNCis enabled later by some driver, causing premature transfer completion at low SPI clock frequencies (more likely). Fix both functions by clearing the pending bit after polling.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-7.4
Thanks!
[1/2] spi: axi-spi-engine: fix stale SYNC IRQ pending
https://git.kernel.org/broonie/misc/c/ea29c686f96a [2/2] spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable https://git.kernel.org/broonie/misc/c/18febc10a353
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark