* [RFC PATCH v2 0/1] spi: fsl-lpspi: add atomic transfer support
@ 2026-11-27 7:34 ChunYuZhiQiang
2026-11-27 7:34 ` [RFC PATCH v2 1/1] " ChunYuZhiQiang
2026-11-27 12:50 ` [RFC PATCH v2 0/1] " Mark Brown
0 siblings, 2 replies; 4+ messages in thread
From: ChunYuZhiQiang @ 2026-11-27 7:34 UTC (permalink / raw)
To: Mark Brown; +Cc: Frank Li, linux-spi, imx, linux-kernel, Heiko Stübner
This is a resend of the fsl_lpspi follow-up to Heiko Stuebner's "spi:
Implement a atomic transfer as a last resort" series. v1 was based on an
earlier revision of that series and no longer applied; this version is
based on current broonie/spi for-next, where Heiko's v4 is applied
(for-next 63083539d2bf).
The panic entry point and its pstore/NVMEM user are split into a
follow-up series, so this one only adds a second transfer_atomic()
implementation next to Rockchip's.
The callback uses PIO polling with DMA and interrupts disabled, resets the
controller before the message, handles chip select and delays through the
core helpers, and caches the per-clock rate because clk_get_rate() takes
the clock prepare lock. Poweroff and restart prepare handlers
runtime-resume the controller before shutdown, matching Rockchip.
Only host mode registers the callback.
Changes in v2:
- rebased onto current for-next with Heiko's v4 applied;
- split spi_sync_atomic() and the pstore/NVMEM user into a separate
series to keep this controller-only.
Testing: compile-tested on x86 with COMPILE_TEST and on arm64, including
against the for-next version of the driver. Hardware validation on
i.MX8QXP is still pending.
This cover letter and the patch were prepared with AI assistance; the
submitter must review them before submission.
Thanks,
ChunYuZhiQiang
Signed-off-by: ChunYuZhiQiang <chunyuzhiqiang@yinhe.ht>
ChunYuZhiQiang (1):
spi: fsl-lpspi: add atomic transfer support
drivers/spi/spi-fsl-lpspi.c | 171 ++++++++++++++++++++++++++++++++++--
1 file changed, 163 insertions(+), 8 deletions(-)
base-commit: 63083539d2bf955ccf84042d4b044d1133752bed
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH v2 1/1] spi: fsl-lpspi: add atomic transfer support
2026-11-27 7:34 [RFC PATCH v2 0/1] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
@ 2026-11-27 7:34 ` ChunYuZhiQiang
2026-11-27 20:49 ` Heiko Stübner
2026-11-27 12:50 ` [RFC PATCH v2 0/1] " Mark Brown
1 sibling, 1 reply; 4+ messages in thread
From: ChunYuZhiQiang @ 2026-11-27 7:34 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.
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 prepared with AI assistance; the submitter must review it
before submission.
Signed-off-by: ChunYuZhiQiang <chunyuzhiqiang@yinhe.ht>
---
drivers/spi/spi-fsl-lpspi.c | 171 ++++++++++++++++++++++++++++++++++--
1 file changed, 163 insertions(+), 8 deletions(-)
diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c
index ed395f822..e37761d05 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,114 @@ 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, timeout_us;
+ int ret;
+
+ /* One frame plus margin, at least 1ms. */
+ timeout_us = DIV_ROUND_UP(8 * USEC_PER_SEC, fsl_lpspi->config.speed_hz);
+ timeout_us = max(timeout_us * 2, 1000U);
+
+ 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;
+ fsl_lpspi->tx(fsl_lpspi);
+
+ ret = readl_poll_timeout_atomic(fsl_lpspi->base + IMX7ULP_SR,
+ sr, sr & SR_RDF, 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_TCF, 0, timeout_us);
+ if (ret)
+ return ret;
+
+ writel(SR_TCF, 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, *next;
+ bool first = true;
+ bool keep_cs = false;
+ int ret = 0;
+
+ if (!pm_runtime_active(fsl_lpspi->dev))
+ return -ESHUTDOWN;
+
+ fsl_lpspi->usedma = false;
+ fsl_lpspi_reset(fsl_lpspi);
+ writel(0, fsl_lpspi->base + IMX7ULP_DER);
+
+ t = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
+ spi_set_cs(spi, !t->cs_off, false);
+
+ list_for_each_entry(t, &msg->transfers, transfer_list) {
+ if (t->len) {
+ fsl_lpspi->is_first_byte = first;
+ ret = fsl_lpspi_setup_transfer(controller, spi, t, true);
+ if (ret)
+ break;
+ if (t->len %
+ fsl_lpspi_bytes_per_word(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;
+
+ fsl_lpspi_reset(fsl_lpspi);
+ first = 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);
+ }
+ }
+
+ if (!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 +903,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 +997,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 +1090,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 +1107,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 +1119,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;
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC PATCH v2 0/1] spi: fsl-lpspi: add atomic transfer support
2026-11-27 7:34 [RFC PATCH v2 0/1] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
2026-11-27 7:34 ` [RFC PATCH v2 1/1] " ChunYuZhiQiang
@ 2026-11-27 12:50 ` Mark Brown
1 sibling, 0 replies; 4+ messages in thread
From: Mark Brown @ 2026-11-27 12:50 UTC (permalink / raw)
To: ChunYuZhiQiang; +Cc: Frank Li, linux-spi, imx, linux-kernel, Heiko Stübner
[-- Attachment #1: Type: text/plain, Size: 693 bytes --]
On Fri, Nov 27, 2026 at 03:34:53PM +0800, ChunYuZhiQiang wrote:
> This is a resend of the fsl_lpspi follow-up to Heiko Stuebner's "spi:
> Implement a atomic transfer as a last resort" series. v1 was based on an
> earlier revision of that series and no longer applied; this version is
> based on current broonie/spi for-next, where Heiko's v4 is applied
> (for-next 63083539d2bf).
Please don't send cover letters for single patches, if there is anything
that needs saying put it in the changelog of the patch or after the ---
if it's administrative stuff. This reduces mail volume and ensures that
any important information is recorded in the changelog rather than being
lost.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC PATCH v2 1/1] spi: fsl-lpspi: add atomic transfer support
2026-11-27 7:34 ` [RFC PATCH v2 1/1] " ChunYuZhiQiang
@ 2026-11-27 20:49 ` Heiko Stübner
0 siblings, 0 replies; 4+ messages in thread
From: Heiko Stübner @ 2026-11-27 20:49 UTC (permalink / raw)
To: Mark Brown, ChunYuZhiQiang; +Cc: Frank Li, linux-spi, imx, linux-kernel
Am Freitag, 27. November 2026, 08:34:54 Mitteleuropäische Normalzeit schrieb ChunYuZhiQiang:
> This patch was prepared with AI assistance; the submitter must review it
> before submission.
This sentence is very confusing.
So an LLM generated this. Did you actually review what was generated?
Because this looks more as an instruction for you as the submitter, so
I would assume this line not "surviving" the review step, if it happens.
Heiko
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-27 20:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 7:34 [RFC PATCH v2 0/1] spi: fsl-lpspi: add atomic transfer support ChunYuZhiQiang
2026-11-27 7:34 ` [RFC PATCH v2 1/1] " ChunYuZhiQiang
2026-11-27 20:49 ` Heiko Stübner
2026-11-27 12:50 ` [RFC PATCH v2 0/1] " Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®