* [RFC PATCH v3] spi: fsl-lpspi: add atomic transfer support
@ 2026-11-27 12:58 ChunYuZhiQiang
2026-11-27 13:26 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: ChunYuZhiQiang @ 2026-11-27 12:58 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
before the last frame and wait for the frame-complete flag after each
transfer. 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 v3:
- poll the receive FIFO empty flag instead of SR_RDF; RDF requires the
RX FIFO count to exceed RXWATER, which the one-word-at-a-time loop
never reaches for multi-word transfers;
- terminate continuous mode before the last frame and wait for FCF,
matching the normal PIO path, instead of polling TCF with CONTC set;
- program CPOL/CPHA and the first command before asserting GPIO CS, as
fsl_lpspi_prepare_message() does;
- use spi_bpw_to_bytes() for the length check so 17..24-bit transfers
with a 4-byte stride are accepted;
- set effective_speed_hz and actual_length in the atomic path;
- size the poll timeout from the frame size instead of assuming 8 bits;
- replace the AI placeholder wording with a plain disclosure.
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 v3] spi: fsl-lpspi: add atomic transfer support
2026-11-27 12:58 [RFC PATCH v3] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
@ 2026-11-27 13:26 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 13:26 UTC (permalink / raw)
To: ChunYuZhiQiang; +Cc: imx, Frank.Li
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] spi: fsl-lpspi: premature TCR_CONTC clear in
fsl_lpspi_pio_xfer_atomic()
- [High] spi: fsl-lpspi: broken TCR_CONTC handling in
fsl_lpspi_transfer_atomic()
--
commit 1c6b4fd740bc599cecc8d77f094463efe4a15041
Author: ChunYuZhiQiang <chunyuzhiqiang@yinhe.ht>
spi: fsl-lpspi: add atomic transfer support
This commit implements the transfer_atomic() callback for the i.MX LPSPI
controller using PIO polling, caches the peripheral clock rate for atomic
contexts, and registers sys_off handlers to runtime-resume the controller
before poweroff and restart.
> 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);
> + if (ret)
> + return ret;
[Severity: High]
Does writing IMX7ULP_TCR with TCR_CONTC cleared before calling
fsl_lpspi->tx() push the command word into the TX FIFO ahead of the last
data word rather than after it?
Writes to IMX7ULP_TCR and IMX7ULP_TDR are queued sequentially in the same
TX FIFO, and the value read from IMX7ULP_TCR still has TCR_CONT set. In
the normal PIO path, fsl_lpspi_write_tx_fifo() pushes all data words via
fsl_lpspi->tx() until fsl_lpspi->remain reaches 0 before writing
IMX7ULP_TCR with TCR_CONTC cleared:
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);
}
...
}
Queuing the command word before fsl_lpspi->tx() writes the final data word
leaves continuous mode active after that final word is shifted out.
Single-word atomic transfers will never assert SR_FCF and will time out,
while multi-word transfers will end the continuous frame one word early
and transmit the last word in a new unterminated continuous transfer.
> + writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
> + return 0;
> +}
> +
> +static int fsl_lpspi_transfer_atomic(struct spi_device *spi,
> + struct spi_message *msg)
> +{
[ ... ]
> + 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;
> + }
[Severity: High]
Does setting first_xfer = false after fsl_lpspi_pio_xfer_atomic() and
fsl_lpspi_reset() cause subsequent transfers in a multi-transfer message
to start with TCR_CONTC set when no continuous transfer is active?
At the end of each transfer, fsl_lpspi_pio_xfer_atomic() terminates the
frame and waits for SR_FCF, and fsl_lpspi_reset() disables the module and
flushes the TX and RX FIFOs. On the next transfer in msg->transfers,
fsl_lpspi->is_first_byte is set to false, so fsl_lpspi_set_cmd() programs
TCR_CONT | TCR_CONTC into IMX7ULP_TCR even though the previous continuous
transfer already completed and the controller was reset. Combined with
fsl_lpspi_pio_xfer_atomic() clearing TCR_CONTC before writing the last
data word to IMX7ULP_TDR, multi-transfer atomic messages will fail or time
out.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127125827.22857-1-chunyuzhiqiang@yinhe.ht?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 13:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 12:58 [RFC PATCH v3] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
2026-11-27 13:26 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox