* [RFC PATCH v4] spi: fsl-lpspi: add atomic transfer support
@ 2026-11-27 14:02 ChunYuZhiQiang
2026-11-27 14:19 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: ChunYuZhiQiang @ 2026-11-27 14:02 UTC (permalink / raw)
To: Mark Brown; +Cc: Frank Li, linux-spi, imx, linux-kernel, Heiko Stübner
Implement transfer_atomic() for the i.MX LPSPI controller, following the
atomic transfer API added by Heiko Stuebner. The callback uses PIO
polling with interrupts and DMA disabled, handles chip select through
spi_set_cs(), and runs the message synchronously without completions.
The controller is reset before the message so state left by a crashed
transfer does not leak into the atomic path.
The atomic path cannot call clk_get_rate(), because it takes the clock
prepare lock. Cache the per-clock rate at probe and refresh it in the
normal transfer path; the atomic path only uses the cached value. A
zero rate is rejected instead of programming a bogus divisor.
Program clock polarity and phase before the core asserts the GPIO chip
select, matching fsl_lpspi_prepare_message(). Terminate continuous mode
after the last data word, matching fsl_lpspi_write_tx_fifo(), and wait
for the frame-complete flag after each transfer. The FIFO reset between
transfers leaves TCR_CONT set, so the next command can continue with
CONTC as in the normal transfer path. Poll the receive FIFO empty flag
rather than the FIFO watermark so multi-word transfers do not time out,
use spi_bpw_to_bytes() for the length check so 17..24-bit transfers with
a 4-byte stride are accepted, and populate effective_speed_hz and
actual_length for the atomic path.
Only host mode registers the callback. Poweroff and restart prepare
handlers runtime-resume the controller so the atomic path can be used
from the shutdown notifiers, matching the Rockchip implementation.
The driver is compile-tested on x86 with COMPILE_TEST and on arm64;
hardware validation on i.MX8QXP is pending.
This patch was developed with AI assistance.
Signed-off-by: ChunYuZhiQiang <chunyuzhiqiang@yinhe.ht>
---
Changes in v4:
- queue the TCR_CONTC-clear command after the last data word, matching
fsl_lpspi_write_tx_fifo(), instead of before it; TCR and TDR share the
same FIFO, so the old order ended a frame early;
- document that the inter-transfer FIFO reset leaves TCR_CONT set, so
the next command can continue with CONTC as in the normal path.
drivers/spi/spi-fsl-lpspi.c | 205 ++++++++++++++++++++++++++++++++++--
1 file changed, 197 insertions(+), 8 deletions(-)
diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
index ed395f822dab1..bc76a564137b9 100644
--- a/drivers/spi/spi-fsl-lpspi.c
+++ b/drivers/spi/spi-fsl-lpspi.c
@@ -14,6 +14,7 @@
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
+#include <linux/iopoll.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
@@ -22,6 +23,7 @@
#include <linux/platform_device.h>
#include <linux/dma/imx-dma.h>
#include <linux/pm_runtime.h>
+#include <linux/reboot.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
@@ -112,6 +114,7 @@ struct fsl_lpspi_data {
unsigned long base_phys;
struct clk *clk_ipg;
struct clk *clk_per;
+ u32 perclk_rate;
bool is_target;
bool is_only_cs1;
bool is_first_byte;
@@ -335,7 +338,8 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
dev_dbg(fsl_lpspi->dev, "FCR=0x%08x\n", temp);
}
-static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
+static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi,
+ bool atomic)
{
struct lpspi_config config = fsl_lpspi->config;
unsigned int perclk_rate, div;
@@ -343,7 +347,14 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
u8 prescale;
int scldiv;
- perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
+ if (atomic) {
+ perclk_rate = fsl_lpspi->perclk_rate;
+ } else {
+ perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
+ fsl_lpspi->perclk_rate = perclk_rate;
+ }
+ if (!perclk_rate)
+ return -EINVAL;
prescale_max = fsl_lpspi->devtype_data->prescale_max ?: 7;
if (!config.speed_hz) {
@@ -427,13 +438,13 @@ static int fsl_lpspi_dma_configure(struct spi_controller *controller)
return 0;
}
-static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
+static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi, bool atomic)
{
u32 temp;
int ret;
if (!fsl_lpspi->is_target) {
- ret = fsl_lpspi_set_bitrate(fsl_lpspi);
+ ret = fsl_lpspi_set_bitrate(fsl_lpspi, atomic);
if (ret)
return ret;
}
@@ -464,7 +475,7 @@ static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
static int fsl_lpspi_setup_transfer(struct spi_controller *controller,
struct spi_device *spi,
- struct spi_transfer *t)
+ struct spi_transfer *t, bool atomic)
{
struct fsl_lpspi_data *fsl_lpspi =
spi_controller_get_devdata(spi->controller);
@@ -496,7 +507,7 @@ static int fsl_lpspi_setup_transfer(struct spi_controller *controller,
fsl_lpspi->watermark = min(fsl_lpspi->txfifosize, t->len);
- return fsl_lpspi_config(fsl_lpspi);
+ return fsl_lpspi_config(fsl_lpspi, atomic);
}
static int fsl_lpspi_prepare_message(struct spi_controller *controller,
@@ -515,7 +526,7 @@ static int fsl_lpspi_prepare_message(struct spi_controller *controller,
fsl_lpspi->is_first_byte = true;
fsl_lpspi->usedma = false;
- ret = fsl_lpspi_setup_transfer(controller, spi, t);
+ ret = fsl_lpspi_setup_transfer(controller, spi, t, false);
fsl_lpspi->usedma = fsl_lpspi_can_dma(controller, spi, t);
@@ -774,6 +785,148 @@ static int fsl_lpspi_pio_transfer(struct fsl_lpspi_data *fsl_lpspi,
return ret;
}
+static int fsl_lpspi_pio_xfer_atomic(struct fsl_lpspi_data *fsl_lpspi,
+ struct spi_transfer *t)
+{
+ u32 sr, temp, timeout_us;
+ unsigned int bytes_per_word;
+ int ret;
+
+ /* One frame plus margin, at least 1ms. */
+ timeout_us = DIV_ROUND_UP(fsl_lpspi->config.bpw * USEC_PER_SEC,
+ fsl_lpspi->config.speed_hz);
+ timeout_us = max(timeout_us * 2, 1000U);
+
+ bytes_per_word = spi_bpw_to_bytes(fsl_lpspi->config.bpw);
+ fsl_lpspi->tx_buf = t->tx_buf;
+ fsl_lpspi->rx_buf = t->rx_buf;
+ fsl_lpspi->remain = t->len;
+
+ while (fsl_lpspi->remain) {
+ ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_SR,
+ sr, sr & SR_TDF, 0, timeout_us);
+ if (ret)
+ return ret;
+
+ /* Terminate continuous mode before the last frame. */
+ if (fsl_lpspi->remain == bytes_per_word) {
+ temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
+ temp &= ~TCR_CONTC;
+ writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
+ }
+
+ fsl_lpspi->tx(fsl_lpspi);
+
+ ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_RSR,
+ sr, !(sr & RSR_RXEMPTY), 0,
+ timeout_us);
+ if (ret)
+ return ret;
+ fsl_lpspi->rx(fsl_lpspi);
+ }
+
+ ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_SR, sr,
+ sr & SR_FCF, 0, timeout_us);
+ if (ret)
+ return ret;
+
+ writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
+ return 0;
+}
+
+static int fsl_lpspi_transfer_atomic(struct spi_device *spi,
+ struct spi_message *msg)
+{
+ struct spi_controller *controller = spi->controller;
+ struct fsl_lpspi_data *fsl_lpspi =
+ spi_controller_get_devdata(controller);
+ struct spi_transfer *t, *first, *next;
+ bool keep_cs = false;
+ bool first_xfer = true;
+ int ret = 0;
+
+ if (!pm_runtime_active(fsl_lpspi->dev))
+ return -ESHUTDOWN;
+
+ msg->actual_length = 0;
+ fsl_lpspi->usedma = false;
+ fsl_lpspi_reset(fsl_lpspi);
+ writel(0, fsl_lpspi->base + IMX7ULP_DER);
+
+ first = list_first_entry(&msg->transfers, struct spi_transfer,
+ transfer_list);
+
+ /*
+ * Match fsl_lpspi_prepare_message(): program CPOL/CPHA and the
+ * first command before the core asserts GPIO chip select.
+ */
+ fsl_lpspi->is_first_byte = true;
+ ret = fsl_lpspi_setup_transfer(controller, spi, first, true);
+ if (ret)
+ goto out;
+
+ fsl_lpspi_set_cmd(fsl_lpspi);
+ writel(0, fsl_lpspi->base + IMX7ULP_IER);
+ writel(CR_RRF | CR_RTF, fsl_lpspi->base + IMX7ULP_CR);
+ writel(SR_CLEAR_MASK, fsl_lpspi->base + IMX7ULP_SR);
+
+ spi_set_cs(spi, !first->cs_off, false);
+
+ list_for_each_entry(t, &msg->transfers, transfer_list) {
+ if (t->len) {
+ fsl_lpspi->is_first_byte = first_xfer;
+ ret = fsl_lpspi_setup_transfer(controller, spi, t, true);
+ if (ret)
+ break;
+
+ t->effective_speed_hz = fsl_lpspi->config.effective_speed_hz;
+
+ if (t->len %
+ spi_bpw_to_bytes(fsl_lpspi->config.bpw)) {
+ ret = -EINVAL;
+ break;
+ }
+
+ fsl_lpspi_set_cmd(fsl_lpspi);
+ fsl_lpspi->is_first_byte = false;
+
+ ret = fsl_lpspi_pio_xfer_atomic(fsl_lpspi, t);
+ if (ret)
+ break;
+
+ msg->actual_length += t->len;
+ fsl_lpspi_reset(fsl_lpspi);
+ first_xfer = false;
+ }
+
+ spi_transfer_delay_exec(t);
+
+ if (t->cs_change) {
+ if (list_is_last(&t->transfer_list, &msg->transfers)) {
+ keep_cs = true;
+ } else {
+ if (!t->cs_off)
+ spi_set_cs(spi, false, false);
+ spi_transfer_cs_change_delay_exec(msg, t);
+ next = list_next_entry(t, transfer_list);
+ if (!next->cs_off)
+ spi_set_cs(spi, true, false);
+ }
+ } else if (!list_is_last(&t->transfer_list, &msg->transfers)) {
+ next = list_next_entry(t, transfer_list);
+ if (t->cs_off != next->cs_off)
+ spi_set_cs(spi, !next->cs_off, false);
+ }
+ }
+
+out:
+ if (ret || !keep_cs)
+ spi_set_cs(spi, false, false);
+ fsl_lpspi_reset(fsl_lpspi);
+ msg->status = ret;
+ return ret;
+}
+
static int fsl_lpspi_transfer_one(struct spi_controller *controller,
struct spi_device *spi,
struct spi_transfer *t)
@@ -784,7 +937,7 @@ static int fsl_lpspi_transfer_one(struct spi_controller *controller,
fsl_lpspi->usedma = fsl_lpspi_can_dma(controller, spi, t);
- ret = fsl_lpspi_setup_transfer(controller, spi, t);
+ ret = fsl_lpspi_setup_transfer(controller, spi, t, false);
if (ret < 0)
return ret;
@@ -878,6 +1031,19 @@ static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
return 0;
}
+static int fsl_lpspi_power_off(struct sys_off_data *data)
+{
+ struct fsl_lpspi_data *fsl_lpspi = data->cb_data;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(fsl_lpspi->dev);
+ if (ret)
+ dev_warn(fsl_lpspi->dev,
+ "failed to resume controller for poweroff: %d\n", ret);
+
+ return 0;
+}
+
static int fsl_lpspi_probe(struct platform_device *pdev)
{
const struct fsl_lpspi_devtype_data *devtype_data;
@@ -958,6 +1124,9 @@ static int fsl_lpspi_probe(struct platform_device *pdev)
goto out_pm_get;
}
+ /* The atomic transfer path cannot call clk_get_rate(). */
+ fsl_lpspi->perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
+
temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);
fsl_lpspi->txfifosize = 1 << (temp & 0x0f);
fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);
@@ -972,6 +1141,8 @@ static int fsl_lpspi_probe(struct platform_device *pdev)
controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
controller->prepare_message = fsl_lpspi_prepare_message;
controller->transfer_one = fsl_lpspi_transfer_one;
+ if (!fsl_lpspi->is_target)
+ controller->transfer_atomic = fsl_lpspi_transfer_atomic;
controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
@@ -982,6 +1153,24 @@ static int fsl_lpspi_probe(struct platform_device *pdev)
if (!fsl_lpspi->is_target)
controller->use_gpio_descriptors = true;
+ if (!fsl_lpspi->is_target) {
+ ret = devm_register_sys_off_handler(&pdev->dev,
+ SYS_OFF_MODE_POWER_OFF_PREPARE,
+ SYS_OFF_PRIO_DEFAULT,
+ fsl_lpspi_power_off,
+ fsl_lpspi);
+ if (ret)
+ goto out_pm_get;
+
+ ret = devm_register_sys_off_handler(&pdev->dev,
+ SYS_OFF_MODE_RESTART_PREPARE,
+ SYS_OFF_PRIO_DEFAULT,
+ fsl_lpspi_power_off,
+ fsl_lpspi);
+ if (ret)
+ goto out_pm_get;
+ }
+
ret = fsl_lpspi_dma_init(&pdev->dev, fsl_lpspi, controller);
if (ret == -EPROBE_DEFER)
goto out_pm_get;
base-commit: 63083539d2bf955ccf84042d4b044d1133752bed
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [RFC PATCH v4] spi: fsl-lpspi: add atomic transfer support
2026-11-27 14:02 [RFC PATCH v4] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
@ 2026-11-27 14:19 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 14:19 UTC (permalink / raw)
To: ChunYuZhiQiang; +Cc: imx, Frank.Li
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] spi: fsl-lpspi: premature TCR_CONTC clear in
fsl_lpspi_pio_xfer_atomic()
--
commit f448f305f9614918db9e4edf6fb2c18c134df2de
Author: ChunYuZhiQiang <chunyuzhiqiang@yinhe.ht>
spi: fsl-lpspi: add atomic transfer support
This commit implements transfer_atomic() for the i.MX LPSPI controller
using PIO polling, caches the per-clock rate for atomic context, and
registers poweroff and restart prepare handlers to runtime-resume the
controller.
> diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
> index ed395f822dab1..bc76a564137b9 100644
> --- a/drivers/spi/spi-fsl-lpspi.c
> +++ b/drivers/spi/spi-fsl-lpspi.c
[ ... ]
> @@ -774,6 +785,148 @@ static int fsl_lpspi_pio_transfer(struct fsl_lpspi_data *fsl_lpspi,
> return ret;
> }
>
> +static int fsl_lpspi_pio_xfer_atomic(struct fsl_lpspi_data *fsl_lpspi,
> + struct spi_transfer *t)
> +{
> + u32 sr, temp, timeout_us;
> + unsigned int bytes_per_word;
> + int ret;
> +
> + /* One frame plus margin, at least 1ms. */
> + timeout_us = DIV_ROUND_UP(fsl_lpspi->config.bpw * USEC_PER_SEC,
> + fsl_lpspi->config.speed_hz);
> + timeout_us = max(timeout_us * 2, 1000U);
> +
> + bytes_per_word = spi_bpw_to_bytes(fsl_lpspi->config.bpw);
> + fsl_lpspi->tx_buf = t->tx_buf;
> + fsl_lpspi->rx_buf = t->rx_buf;
> + fsl_lpspi->remain = t->len;
> +
> + while (fsl_lpspi->remain) {
> + ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_SR,
> + sr, sr & SR_TDF, 0, timeout_us);
> + if (ret)
> + return ret;
> +
> + /* Terminate continuous mode before the last frame. */
> + if (fsl_lpspi->remain == bytes_per_word) {
> + temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
> + temp &= ~TCR_CONTC;
> + writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
> + }
> +
> + fsl_lpspi->tx(fsl_lpspi);
> +
> + ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_RSR,
> + sr, !(sr & RSR_RXEMPTY), 0,
> + timeout_us);
> + if (ret)
> + return ret;
> + fsl_lpspi->rx(fsl_lpspi);
> + }
> +
> + ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_SR, sr,
> + sr & SR_FCF, 0, timeout_us);
[Severity: High]
Does writing IMX7ULP_TCR with TCR_CONTC cleared before calling
fsl_lpspi->tx() queue the terminating command word ahead of the final data
word in the shared TX FIFO?
Because temp read from IMX7ULP_TCR still has TCR_CONT set and writes to
IMX7ULP_TCR and IMX7ULP_TDR share the TX FIFO, a single-word atomic
transfer pushes the command word before the data word and queues no
command word after it, so continuous mode never ends and the SR_FCF poll
times out with -ETIMEDOUT. Multi-word transfers end the frame one word
early and leave the final word in an unterminated continuous transfer.
In the normal PIO path, fsl_lpspi_write_tx_fifo() pushes the data words
via fsl_lpspi->tx() first and clears TCR_CONTC only after
fsl_lpspi->remain reaches zero:
drivers/spi/spi-fsl-lpspi.c:fsl_lpspi_write_tx_fifo() {
...
while (txfifo_cnt < fsl_lpspi->txfifosize && fsl_lpspi->remain) {
fsl_lpspi->tx(fsl_lpspi);
txfifo_cnt++;
}
if (txfifo_cnt < fsl_lpspi->txfifosize) {
if (!fsl_lpspi->is_target) {
temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
temp &= ~TCR_CONTC;
writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
}
...
}
Wait for SR_TDF and write IMX7ULP_TCR with TCR_CONTC cleared after the
final data word has been pushed by fsl_lpspi->tx(), as the commit message
describes.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127140228.48851-1-chunyuzhiqiang@yinhe.ht?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 14:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 14:02 [RFC PATCH v4] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
2026-11-27 14:19 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox