* [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
@ 2026-11-23 9:44 ` Binbin Zhou
2026-11-23 9:52 ` sashiko-bot
2026-11-23 9:44 ` [PATCH 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible Binbin Zhou
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Binbin Zhou @ 2026-11-23 9:44 UTC (permalink / raw)
To: Binbin Zhou, Huacai Chen, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Yinbo Zhu, Mark Brown
Cc: Huacai Chen, Xuerui Wang, loongarch, devicetree, linux-spi,
Bingxiong Li, Binbin Zhou
The SPI flash controller on Loongson-2K0300 shares the same register
layout and functionality as the one on Loongson-2K1000. Add a dedicated
compatible string `loongson,ls2k0300-spi` with
`loongson,ls2k1000-spi` as fallback to allow proper DT binding.
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
---
Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
index de9d32feadf5..8a36dbece788 100644
--- a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
+++ b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
@@ -19,6 +19,7 @@ properties:
- loongson,ls2k1000-spi
- items:
- enum:
+ - loongson,ls2k0300-spi
- loongson,ls2k0500-spi
- const: loongson,ls2k1000-spi
@@ -28,6 +29,9 @@ properties:
clocks:
maxItems: 1
+ interrupts:
+ maxItems: 1
+
required:
- compatible
- reg
--
2.52.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible
2026-11-23 9:44 ` [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
@ 2026-11-23 9:52 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-23 9:52 UTC (permalink / raw)
To: Binbin Zhou; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] spi: dt-bindings: changelog omits addition of interrupts
property
--
commit 69ba92a75f7fc4022285efadae2a4866e7663918
Author: Binbin Zhou <zhoubinbin@loongson.cn>
spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible
This commit adds the loongson,ls2k0300-spi compatible string and an
optional interrupts property to the Loongson LS2K SPI binding schema.
> diff --git a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
> index de9d32feadf59..8a36dbece7885 100644
> --- a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
> +++ b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
> @@ -19,6 +19,7 @@ properties:
> - loongson,ls2k1000-spi
> - items:
> - enum:
> + - loongson,ls2k0300-spi
> - loongson,ls2k0500-spi
> - const: loongson,ls2k1000-spi
>
> @@ -28,6 +29,9 @@ properties:
> clocks:
> maxItems: 1
>
> + interrupts:
> + maxItems: 1
[Severity: Low]
This isn't a bug, but the commit message only mentions adding the
loongson,ls2k0300-spi compatible string and omits the addition of the
interrupts property to the schema.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795425389.git.zhoubinbin@loongson.cn?part=1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
2026-11-23 9:44 ` [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
@ 2026-11-23 9:44 ` Binbin Zhou
2026-11-23 9:44 ` [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver Binbin Zhou
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Binbin Zhou @ 2026-11-23 9:44 UTC (permalink / raw)
To: Binbin Zhou, Huacai Chen, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Yinbo Zhu, Mark Brown
Cc: Huacai Chen, Xuerui Wang, loongarch, devicetree, linux-spi,
Bingxiong Li, Binbin Zhou
Loongson-2K0300 integrates a new SPI-IO controller with enhanced
registers and extended capabilities (interrupt, DMA, larger FIFO, etc.)
which is distinct from the legacy SPI flash controller.
Define the new compatible `loongson,ls2k0300-spi-io` and require the
associated DMA and interrupt properties for this variant.
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
---
.../bindings/spi/loongson,ls2k-spi.yaml | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
index 8a36dbece788..b113d10c5bd9 100644
--- a/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
+++ b/Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
@@ -16,6 +16,7 @@ properties:
compatible:
oneOf:
- enum:
+ - loongson,ls2k0300-spi-io
- loongson,ls2k1000-spi
- items:
- enum:
@@ -32,6 +33,14 @@ properties:
interrupts:
maxItems: 1
+ dmas:
+ maxItems: 2
+
+ dma-names:
+ items:
+ - const: tx
+ - const: rx
+
required:
- compatible
- reg
@@ -39,6 +48,23 @@ required:
unevaluatedProperties: false
+if:
+ properties:
+ compatible:
+ contains:
+ const: loongson,ls2k0300-spi-io
+
+then:
+ required:
+ - dmas
+ - dma-names
+ - interrupts
+
+else:
+ properties:
+ dmas: false
+ dma-names: false
+
examples:
- |
spi0: spi@1fff0220{
@@ -48,3 +74,18 @@ examples:
#address-cells = <1>;
#size-cells = <0>;
};
+
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+ #include <dt-bindings/clock/loongson,ls2k-clk.h>
+
+ spi@1610c000 {
+ compatible = "loongson,ls2k0300-spi-io";
+ reg = <0x1610c000 0x2000>;
+ clocks = <&clk LS2K0300_CLK_DEV_GATE>;
+ interrupt-parent = <&liointc0>;
+ interrupts = <6 IRQ_TYPE_LEVEL_HIGH>;
+ dmas = <&dma_router 14 1 0x8e>,
+ <&dma_router 14 0 0x8e>;
+ dma-names = "tx", "rx";
+ };
--
2.52.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
2026-11-23 9:44 ` [PATCH 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
2026-11-23 9:44 ` [PATCH 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible Binbin Zhou
@ 2026-11-23 9:44 ` Binbin Zhou
2026-11-23 10:05 ` sashiko-bot
2026-11-23 9:45 ` [PATCH 4/5] spi: loongson-io: Add DMA support to SPI-IO driver Binbin Zhou
2026-11-23 9:45 ` [PATCH 5/5] MAINTAINERS: Add myself as Loongson SPI maintainer Binbin Zhou
4 siblings, 1 reply; 8+ messages in thread
From: Binbin Zhou @ 2026-11-23 9:44 UTC (permalink / raw)
To: Binbin Zhou, Huacai Chen, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Yinbo Zhu, Mark Brown
Cc: Huacai Chen, Xuerui Wang, loongarch, devicetree, linux-spi,
Bingxiong Li, Binbin Zhou
Add a platform driver for the Loongson-2K0300 SPI-IO controller.
On the Loongson-2K0300, SPI2 and SPI3 reside in the chip IO address
space and only support IO-space access.
Support 8-bit/16-bit/32-bit words, SPI modes 0-3, LSB-first,
programmable clocking and hardware chip select.
Transfer handling is interrupt-driven in small FIFO batches of
up to four bytes. On EOT IRQ the driver drains RX data and issues
the next batch until the transfer completes. This keeps each
interrupt short and works without requiring DMA providers to be
available at probe time.
Signed-off-by: Bingxiong Li <libingxiong@loongson.cn>
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
---
MAINTAINERS | 1 +
drivers/spi/Kconfig | 16 +
drivers/spi/Makefile | 1 +
drivers/spi/spi-loongson-io.c | 737 ++++++++++++++++++++++++++++++++++
4 files changed, 755 insertions(+)
create mode 100644 drivers/spi/spi-loongson-io.c
diff --git a/MAINTAINERS b/MAINTAINERS
index ce1b9e514e08..70789ce6e507 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15742,6 +15742,7 @@ L: linux-spi@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
F: drivers/spi/spi-loongson-core.c
+F: drivers/spi/spi-loongson-io.c
F: drivers/spi/spi-loongson-pci.c
F: drivers/spi/spi-loongson-plat.c
F: drivers/spi/spi-loongson.h
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index a7b343d124a9..5551b883ce1e 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -635,6 +635,22 @@ config SPI_LOONGSON_CORE
tristate
depends on LOONGARCH || COMPILE_TEST
+config SPI_LOONGSON_IO
+ tristate "Loongson SPI-IO Controller Driver"
+ depends on LOONGARCH || COMPILE_TEST
+ depends on OF
+ help
+ This enables support for the Loongson SPI-IO controller, which is
+ a separate SPI IP block found on Loongson-2K0300 SPI2/SPI3.
+
+ It is a generic SPI master for externally connected SPI
+ peripherals. It does not provide memory-mapped SPI flash
+ access (no spi-mem support). Transfers can be interrupt-driven
+ or DMA-driven, with runtime-selectable 8/16/32-bit frame widths
+
+ This driver can also be built as a module. If so, the module
+ will be called spi-loongson-io.
+
config SPI_LOONGSON_PCI
tristate "Loongson SPI Controller PCI Driver Support"
select SPI_LOONGSON_CORE
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index b6ab37271555..3af58eaa391a 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -83,6 +83,7 @@ obj-$(CONFIG_SPI_KSPI2) += spi-kspi2.o
obj-$(CONFIG_SPI_LJCA) += spi-ljca.o
obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o
obj-$(CONFIG_SPI_LOONGSON_CORE) += spi-loongson-core.o
+obj-$(CONFIG_SPI_LOONGSON_IO) += spi-loongson-io.o
obj-$(CONFIG_SPI_LOONGSON_PCI) += spi-loongson-pci.o
obj-$(CONFIG_SPI_LOONGSON_PLATFORM) += spi-loongson-plat.o
obj-$(CONFIG_SPI_LP8841_RTC) += spi-lp8841-rtc.o
diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
new file mode 100644
index 000000000000..e273c9f4a943
--- /dev/null
+++ b/drivers/spi/spi-loongson-io.c
@@ -0,0 +1,737 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// Platform driver for Loongson-2K0300 SPI IO Support
+//
+// Copyright (C) 2017-2026 Loongson Technology Corporation Limited
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/string.h>
+
+/* Loongson SPI-IO Controller registers */
+#define LOONGSON_SPI_IO_CR1_REG 0x00
+#define LOONGSON_SPI_IO_CR2_REG 0x04
+#define LOONGSON_SPI_IO_CR3_REG 0x08
+#define LOONGSON_SPI_IO_CR4_REG 0x0c
+#define LOONGSON_SPI_IO_IER_REG 0x10
+#define LOONGSON_SPI_IO_SR1_REG 0x14
+#define LOONGSON_SPI_IO_SR2_REG 0x18
+#define LOONGSON_SPI_IO_CFG1_REG 0x20
+#define LOONGSON_SPI_IO_CFG2_REG 0x24
+#define LOONGSON_SPI_IO_CFG3_REG 0x28
+#define LOONGSON_SPI_IO_CRC1_REG 0x30
+#define LOONGSON_SPI_IO_CRC2_REG 0x34
+#define LOONGSON_SPI_IO_DR_REG 0x40
+
+/* Bitfields of LOONGSON_SPI_IO_CR1_REG */
+#define LOONGSON_SPI_IO_CR1_SPE BIT(0)
+#define LOONGSON_SPI_IO_CR1_CSTART BIT(1)
+#define LOONGSON_SPI_IO_CR1_AUTOSUS BIT(2)
+#define LOONGSON_SPI_IO_CR1_SSREV BIT(8)
+
+#define LOONGSON_SPI_IO_CR1_START_MASK (LOONGSON_SPI_IO_CR1_CSTART | \
+ LOONGSON_SPI_IO_CR1_AUTOSUS)
+
+/* Bitfields of LOONGSON_SPI_IO_CR2_REG */
+#define LOONGSON_SPI_IO_CR2_RXFTHLV GENMASK(1, 0)
+#define LOONGSON_SPI_IO_CR2_TXFTHLV GENMASK(9, 8)
+
+/* Bitfields of LOONGSON_SPI_IO_CR3_REG */
+#define LOONGSON_SPI_IO_CR3_TSIZE GENMASK(15, 0)
+
+/* Bitfields of LOONGSON_SPI_IO_CR4_REG */
+#define LOONGSON_SPI_IO_CR4_CTSIZE GENMASK(15, 0)
+
+/* Bitfields of LOONGSON_SPI_IO_IER_REG */
+#define LOONGSON_SPI_IO_IER_RXAIE BIT(0)
+#define LOONGSON_SPI_IO_IER_TXAIE BIT(1)
+#define LOONGSON_SPI_IO_IER_DXAIE BIT(2)
+#define LOONGSON_SPI_IO_IER_RXEIE BIT(4)
+#define LOONGSON_SPI_IO_IER_TXEIE BIT(5)
+#define LOONGSON_SPI_IO_IER_SUSPIE BIT(7)
+#define LOONGSON_SPI_IO_IER_OVRIE BIT(8)
+#define LOONGSON_SPI_IO_IER_UDRIE BIT(9)
+#define LOONGSON_SPI_IO_IER_CRCEIE BIT(10)
+#define LOONGSON_SPI_IO_IER_MODFIE BIT(11)
+#define LOONGSON_SPI_IO_IER_EOTIE BIT(15)
+
+/* Bitfields of LOONGSON_SPI_IO_SR1_REG */
+#define LOONGSON_SPI_IO_SR1_RXA BIT(0)
+#define LOONGSON_SPI_IO_SR1_TXA BIT(1)
+#define LOONGSON_SPI_IO_SR1_DXA BIT(2)
+#define LOONGSON_SPI_IO_SR1_RXE BIT(4)
+#define LOONGSON_SPI_IO_SR1_TXE BIT(5)
+#define LOONGSON_SPI_IO_SR1_SUSP BIT(7)
+#define LOONGSON_SPI_IO_SR1_OVR BIT(8)
+#define LOONGSON_SPI_IO_SR1_UDR BIT(9)
+#define LOONGSON_SPI_IO_SR1_CRCE BIT(10)
+#define LOONGSON_SPI_IO_SR1_MODF BIT(11)
+#define LOONGSON_SPI_IO_SR1_EOT BIT(15)
+
+#define LOONGSON_SPI_IO_SR1_ERR_MASK (LOONGSON_SPI_IO_SR1_OVR | \
+ LOONGSON_SPI_IO_SR1_UDR | \
+ LOONGSON_SPI_IO_SR1_MODF | \
+ LOONGSON_SPI_IO_SR1_CRCE)
+
+/* Bitfields of LOONGSON_SPI_IO_SR2_REG */
+#define LOONGSON_SPI_IO_SR2_RXFLV GENMASK(2, 0)
+#define LOONGSON_SPI_IO_SR2_TXFLV GENMASK(10, 8)
+
+/* Bitfields of LOONGSON_SPI_IO_CFG1_REG */
+#define LOONGSON_SPI_IO_CFG1_CPOL BIT(0)
+#define LOONGSON_SPI_IO_CFG1_CPHA BIT(1)
+#define LOONGSON_SPI_IO_CFG1_LSBFRST BIT(7)
+#define LOONGSON_SPI_IO_CFG1_DSIZE GENMASK(12, 8)
+
+/* Bitfields of LOONGSON_SPI_IO_CFG2_REG */
+#define LOONGSON_SPI_IO_CFG2_BRDEC GENMASK(7, 2)
+#define LOONGSON_SPI_IO_CFG2_BRINT GENMASK(15, 8)
+
+#define LOONGSON_SPI_IO_CFG2_MIN_BRINT 2
+#define LOONGSON_SPI_IO_CFG2_MAX_BRINT FIELD_MAX(LOONGSON_SPI_IO_CFG2_BRINT)
+
+/* Bitfields of LOONGSON_SPI_IO_CFG3_REG */
+#define LOONGSON_SPI_IO_CFG3_MSTR BIT(0)
+#define LOONGSON_SPI_IO_CFG3_DIOSWP BIT(1)
+#define LOONGSON_SPI_IO_CFG3_DIE BIT(2)
+#define LOONGSON_SPI_IO_CFG3_DOE BIT(3)
+#define LOONGSON_SPI_IO_CFG3_SSMODE GENMASK(9, 8)
+
+/* Bitfields of LOONGSON_SPI_IO_CRC1_REG */
+#define LOONGSON_SPI_IO_CRC1_CRCEN BIT(0)
+#define LOONGSON_SPI_IO_CRC1_CRCPOLY GENMASK(31, 1)
+
+/* Bitfields of LOONGSON_SPI_IO_CRC2_REG */
+#define LOONGSON_SPI_IO_CRC2_RCRCINI BIT(0)
+#define LOONGSON_SPI_IO_CRC2_TCRCINI BIT(1)
+#define LOONGSON_SPI_IO_CRC2_CRCSIZE GENMASK(12, 8)
+
+/**
+ * enum xfer_mode - Loongson SPI transfer mode
+ * @XFER_IDLE: no transfer is in progress
+ * @XFER_INTERRUPT: interrupt-driven batch transfer
+ */
+enum xfer_mode {
+ XFER_IDLE,
+ XFER_INTERRUPT,
+};
+
+/**
+ * struct loongson_spi_priv - private data for the Loongson SPI controller
+ * @controller: SPI controller instance
+ * @dev: device backing the controller
+ * @base: MMIO base address of the SPI controller
+ * @hz: current SPI bus clock in Hz
+ * @clk_rate: parent clock rate in Hz
+ * @mode: current SPI mode bits
+ * @bits_per_word: current word size in bits
+ * @frame_bytes: bytes occupied by one frame in the FIFO and transfer buffers
+ * @irq: interrupt number
+ * @tx_buf: current transmit buffer for interrupt mode
+ * @rx_buf: current receive buffer for interrupt mode
+ * @remaining: bytes left in the current interrupt transfer
+ * @xfer_ret: result of the current transfer
+ * @batch_size: number of bytes in the current interrupt batch
+ * @xfer_completion: completion signaled when the current transfer finishes
+ * @xfer_mode: current transfer mode
+ */
+struct loongson_spi_priv {
+ struct spi_controller *controller;
+ struct device *dev;
+ void __iomem *base;
+ u32 hz;
+ u64 clk_rate;
+ u8 mode;
+ u8 bits_per_word;
+ u8 frame_bytes;
+
+ int irq;
+ const u8 *tx_buf;
+ u8 *rx_buf;
+ u32 remaining;
+ int xfer_ret;
+ u32 batch_size;
+ struct completion xfer_completion;
+ enum xfer_mode xfer_mode;
+};
+
+static void loongson_spi_writel(struct loongson_spi_priv *priv, u32 reg, u32 data)
+{
+ writel(data, priv->base + reg);
+}
+
+static u32 loongson_spi_readl(struct loongson_spi_priv *priv, u32 reg)
+{
+ return readl(priv->base + reg);
+}
+
+static void loongson_spi_update_bits(struct loongson_spi_priv *priv, u32 reg,
+ u32 mask, u32 val)
+{
+ u32 tmp = readl(priv->base + reg);
+
+ tmp &= ~mask;
+ tmp |= val & mask;
+ writel(tmp, priv->base + reg);
+}
+
+/**
+ * loongson_spi_io_read_dr() - Read one complete frame from the RX FIFO
+ * @priv: controller private data
+ *
+ * The APB read width follows the 1, 2 or 4 byte FIFO storage width selected
+ * by DSIZE. The unused high bits are zero and the low byte is stored first.
+ *
+ * Return: one frame in a little-endian container.
+ */
+static u32 loongson_spi_io_read_dr(struct loongson_spi_priv *priv)
+{
+ void __iomem *dr = priv->base + LOONGSON_SPI_IO_DR_REG;
+
+ if (priv->frame_bytes == 1)
+ return readb(dr);
+ if (priv->frame_bytes == 2)
+ return readw(dr);
+
+ return readl(dr);
+}
+
+/**
+ * loongson_spi_io_write_dr() - Write one complete frame to the TX FIFO
+ * @priv: controller private data
+ * @data: frame in a little-endian container
+ *
+ * Select the APB write strobes for a 1, 2 or 4 byte FIFO access according to
+ * the storage width selected by DSIZE.
+ */
+static void loongson_spi_io_write_dr(struct loongson_spi_priv *priv, u32 data)
+{
+ void __iomem *dr = priv->base + LOONGSON_SPI_IO_DR_REG;
+
+ if (priv->frame_bytes == 1)
+ writeb(data, dr);
+ else if (priv->frame_bytes == 2)
+ writew(data, dr);
+ else
+ writel(data, dr);
+}
+
+/**
+ * loongson_spi_io_cfg_write() - Write a configuration register
+ * @priv: controller private data
+ * @reg: configuration register offset
+ * @val: value to write
+ *
+ * CFG1/CFG2/CFG3 and CRCx writes are rejected while CR1.SPE is set.
+ * Temporarily disable the peripheral and restore CR1 around the write.
+ * The caller must ensure that no transfer is active.
+ */
+static void loongson_spi_io_cfg_write(struct loongson_spi_priv *priv, u32 reg, u32 val)
+{
+ u32 cr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_CR1_REG);
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR1_REG,
+ cr1 & ~LOONGSON_SPI_IO_CR1_SPE);
+ loongson_spi_writel(priv, reg, val);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR1_REG, cr1);
+}
+
+/**
+ * loongson_spi_io_timeout() - Calculate a transfer completion timeout
+ * @priv: controller private data
+ * @xfer: SPI transfer being timed
+ *
+ * Use twice the calculated wire time, with a minimum timeout of one second.
+ *
+ * Return: timeout in jiffies.
+ */
+static unsigned long loongson_spi_io_timeout(struct loongson_spi_priv *priv,
+ struct spi_transfer *xfer)
+{
+ u64 frames = xfer->len / priv->frame_bytes;
+ u64 nsec = frames * priv->bits_per_word * NSEC_PER_SEC;
+ u32 rate = priv->hz ? priv->hz : 1000;
+ u64 msec = DIV_ROUND_UP_ULL(nsec, (u64)rate * NSEC_PER_MSEC);
+
+ return msecs_to_jiffies(max_t(u64, msec * 2, 1000));
+}
+
+static void loongson_spi_io_set_thresh(struct loongson_spi_priv *priv)
+{
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
+ LOONGSON_SPI_IO_CR2_RXFTHLV |
+ LOONGSON_SPI_IO_CR2_TXFTHLV, 0);
+}
+
+/**
+ * loongson_spi_io_set_cs() - Set the hardware chip-select output
+ * @spi: SPI target device
+ * @high: true to assert chip select
+ *
+ * The SSREV bit controls the hardware chip-select assertion state.
+ */
+static void loongson_spi_io_set_cs(struct spi_device *spi, bool high)
+{
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
+ u32 val = high ? LOONGSON_SPI_IO_CR1_SSREV : 0;
+
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_SSREV, val);
+}
+
+/**
+ * loongson_spi_io_set_clk() - Program the SPI bus clock divider
+ * @priv: controller private data
+ * @hz: requested SPI bus clock in Hz
+ */
+static void loongson_spi_io_set_clk(struct loongson_spi_priv *priv, unsigned int hz)
+{
+ u32 val;
+
+ val = clamp_val(DIV_ROUND_UP_ULL(priv->clk_rate, hz),
+ LOONGSON_SPI_IO_CFG2_MIN_BRINT, LOONGSON_SPI_IO_CFG2_MAX_BRINT);
+
+ loongson_spi_io_cfg_write(priv, LOONGSON_SPI_IO_CFG2_REG,
+ FIELD_PREP(LOONGSON_SPI_IO_CFG2_BRINT, val));
+
+ priv->hz = hz;
+}
+
+/**
+ * loongson_spi_io_set_mode() - Apply SPI mode bits and word size
+ * @priv: controller private data
+ * @mode: SPI mode bits from the target device
+ * @bits_per_word: word size in bits
+ *
+ * Configure CPOL, CPHA and LSB_FIRST. DSIZE is programmed as the word size
+ * minus one; pin direction and SPI_3WIRE are handled separately per transfer.
+ */
+static void loongson_spi_io_set_mode(struct loongson_spi_priv *priv, u32 mode,
+ u8 bits_per_word)
+{
+ u32 val = 0;
+
+ if (mode & SPI_CPOL)
+ val |= LOONGSON_SPI_IO_CFG1_CPOL;
+
+ if (mode & SPI_CPHA)
+ val |= LOONGSON_SPI_IO_CFG1_CPHA;
+
+ if (mode & SPI_LSB_FIRST)
+ val |= LOONGSON_SPI_IO_CFG1_LSBFRST;
+
+ val |= FIELD_PREP(LOONGSON_SPI_IO_CFG1_DSIZE, bits_per_word - 1);
+ loongson_spi_io_cfg_write(priv, LOONGSON_SPI_IO_CFG1_REG, val);
+
+ priv->mode = mode;
+ priv->bits_per_word = bits_per_word;
+
+ if (bits_per_word <= 8)
+ priv->frame_bytes = 1;
+ else if (bits_per_word <= 16)
+ priv->frame_bytes = 2;
+ else
+ priv->frame_bytes = 4;
+}
+
+/**
+ * loongson_spi_io_set_pin_mode() - Select MISO/MOSI directions for a transfer
+ * @spi: SPI target device
+ * @xfer: SPI transfer being prepared
+ *
+ * Normal mode supports full-duplex and TX-only/RX-only simplex transfers.
+ * In SPI_3WIRE mode, TX drives MOSI while RX switches MOSI to an input.
+ * Buffers supplied by SPI core for dummy TX/RX do not select a real direction.
+ *
+ * Return: 0 on success, or %-EINVAL if a 3-wire transfer requests both
+ * directions in the same transfer.
+ */
+static int loongson_spi_io_set_pin_mode(struct spi_device *spi,
+ struct spi_transfer *xfer)
+{
+ struct spi_controller *ctrl = spi->controller;
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(ctrl);
+ bool tx = xfer->tx_buf && xfer->tx_buf != ctrl->dummy_tx;
+ bool rx = xfer->rx_buf && xfer->rx_buf != ctrl->dummy_rx;
+ u32 val = LOONGSON_SPI_IO_CFG3_MSTR;
+
+ if (spi->mode & SPI_3WIRE) {
+ if (tx && rx) {
+ dev_err(&spi->dev, "3-wire mode is half-duplex only");
+ return -EINVAL;
+ }
+
+ if (rx)
+ val |= LOONGSON_SPI_IO_CFG3_DIOSWP |
+ LOONGSON_SPI_IO_CFG3_DIE;
+ else
+ val |= LOONGSON_SPI_IO_CFG3_DOE;
+ } else {
+ /* No buffer in either direction defaults to full-duplex. */
+ if (tx || !rx)
+ val |= LOONGSON_SPI_IO_CFG3_DOE;
+ if (rx || !tx)
+ val |= LOONGSON_SPI_IO_CFG3_DIE;
+ }
+
+ if (loongson_spi_readl(priv, LOONGSON_SPI_IO_CFG3_REG) != val)
+ loongson_spi_io_cfg_write(priv, LOONGSON_SPI_IO_CFG3_REG, val);
+
+ return 0;
+}
+
+/**
+ * loongson_spi_io_update_state() - Apply per-transfer clock and device mode
+ * @spi: SPI target device
+ * @t: SPI transfer, or NULL when only the device mode needs updating
+ */
+static void loongson_spi_io_update_state(struct spi_device *spi, struct spi_transfer *t)
+{
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
+ u8 bits_per_word = spi->bits_per_word;
+
+ if (t && t->bits_per_word)
+ bits_per_word = t->bits_per_word;
+
+ if (t && priv->hz != t->speed_hz)
+ loongson_spi_io_set_clk(priv, t->speed_hz);
+
+ if (((spi->mode ^ priv->mode) & (SPI_MODE_X_MASK | SPI_LSB_FIRST | SPI_3WIRE)) ||
+ priv->bits_per_word != bits_per_word)
+ loongson_spi_io_set_mode(priv, spi->mode, bits_per_word);
+}
+
+/**
+ * loongson_spi_io_setup() - Validate an SPI target chip select
+ * @spi: SPI target device being configured
+ *
+ * Return: 0 if the chip select is supported, otherwise %-EINVAL.
+ */
+static int loongson_spi_io_setup(struct spi_device *spi)
+{
+ if (spi_get_chipselect(spi, 0) >= spi->controller->num_chipselect)
+ return -EINVAL;
+
+ return 0;
+}
+
+/**
+ * loongson_spi_io_start_batch() - Start one interrupt-driven FIFO batch
+ * @priv: controller private data
+ *
+ * Preload up to four FIFO bytes using whole frames, with zero-filled frames
+ * when no TX buffer is present. TSIZE counts frames rather than bytes.
+ *
+ * Return: number of bytes in the started batch, or 0 if no data remains.
+ */
+static int loongson_spi_io_start_batch(struct loongson_spi_priv *priv)
+{
+ u32 batch_size, frames;
+ u32 tx_data;
+
+ if (priv->remaining == 0)
+ return 0;
+
+ batch_size = min_t(u32, priv->remaining, 4);
+ frames = batch_size / priv->frame_bytes;
+ priv->batch_size = batch_size;
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR3_REG, frames - 1);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR4_REG, 0);
+
+ for (unsigned int i = 0; i < batch_size; i += priv->frame_bytes) {
+ tx_data = 0;
+ if (priv->tx_buf) {
+ memcpy(&tx_data, priv->tx_buf, priv->frame_bytes);
+ priv->tx_buf += priv->frame_bytes;
+ }
+ loongson_spi_io_write_dr(priv, tx_data);
+ }
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG, LOONGSON_SPI_IO_SR1_EOT);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_START_MASK,
+ LOONGSON_SPI_IO_CR1_START_MASK);
+
+ return batch_size;
+}
+
+/**
+ * loongson_spi_io_irq_handler() - Handle SPI error and end-of-transfer IRQs
+ * @irq: interrupt number
+ * @dev_id: controller private data
+ *
+ * In interrupt mode, consume the current RX batch and start the next one.
+ * Controller errors disable further interrupts and complete the wait with an
+ * error status.
+ *
+ * Return: %IRQ_HANDLED for a recognized error or EOT, otherwise %IRQ_NONE.
+ */
+static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
+{
+ struct loongson_spi_priv *priv = dev_id;
+ u32 rx_data;
+ u32 sr1;
+
+ sr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_SR1_REG);
+
+ if (sr1 & LOONGSON_SPI_IO_SR1_ERR_MASK) {
+ dev_err(&priv->controller->dev, "SPI error: SR1=0x%x", sr1);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
+ priv->xfer_ret = -EIO;
+ complete(&priv->xfer_completion);
+ return IRQ_HANDLED;
+ }
+
+ if (!(sr1 & LOONGSON_SPI_IO_SR1_EOT))
+ return IRQ_NONE;
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG, LOONGSON_SPI_IO_SR1_EOT);
+
+ switch (priv->xfer_mode) {
+ case XFER_INTERRUPT:
+ for (unsigned int i = 0; i < priv->batch_size;
+ i += priv->frame_bytes) {
+ rx_data = loongson_spi_io_read_dr(priv);
+ if (priv->rx_buf) {
+ memcpy(priv->rx_buf, &rx_data, priv->frame_bytes);
+ priv->rx_buf += priv->frame_bytes;
+ }
+ }
+ priv->remaining -= priv->batch_size;
+
+ if (priv->remaining == 0) {
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
+ LOONGSON_SPI_IO_IER_EOTIE, 0);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_CSTART, 0);
+ priv->xfer_ret = 0;
+ complete(&priv->xfer_completion);
+ } else {
+ loongson_spi_io_start_batch(priv);
+ }
+ break;
+ case XFER_IDLE:
+ default:
+ dev_warn(&priv->controller->dev, "EOT with no active transfer");
+ break;
+ }
+
+ return IRQ_HANDLED;
+}
+
+/**
+ * loongson_spi_io_transfer_one() - Execute one SPI transfer
+ * @ctrl: SPI controller
+ * @spi: SPI target device
+ * @xfer: transfer to execute
+ *
+ * Apply clock, mode and pin-direction state before running interrupt-driven
+ * FIFO batches.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int loongson_spi_io_transfer_one(struct spi_controller *ctrl, struct spi_device *spi,
+ struct spi_transfer *xfer)
+{
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
+ int ret;
+
+ loongson_spi_io_update_state(spi, xfer);
+
+ if (xfer->len == 0)
+ return 0;
+
+ if (xfer->len % priv->frame_bytes) {
+ dev_err(&spi->dev, "length %u is not aligned to %u-byte frames",
+ xfer->len, priv->frame_bytes);
+ return -EINVAL;
+ }
+
+ ret = loongson_spi_io_set_pin_mode(spi, xfer);
+ if (ret)
+ return ret;
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
+ LOONGSON_SPI_IO_SR1_EOT | LOONGSON_SPI_IO_SR1_ERR_MASK);
+
+ loongson_spi_io_set_thresh(priv);
+
+ priv->tx_buf = xfer->tx_buf;
+ priv->rx_buf = xfer->rx_buf;
+ priv->remaining = xfer->len;
+ priv->xfer_ret = 0;
+
+ reinit_completion(&priv->xfer_completion);
+ priv->xfer_mode = XFER_INTERRUPT;
+
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_AUTOSUS,
+ LOONGSON_SPI_IO_CR1_AUTOSUS);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
+ LOONGSON_SPI_IO_IER_EOTIE,
+ LOONGSON_SPI_IO_IER_EOTIE);
+
+ loongson_spi_io_start_batch(priv);
+
+ ret = wait_for_completion_timeout(&priv->xfer_completion,
+ loongson_spi_io_timeout(priv, xfer));
+ priv->xfer_mode = XFER_IDLE;
+
+ if (ret == 0) {
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
+ dev_err(&spi->dev, "Interrupt transfer timeout");
+ return -ETIMEDOUT;
+ }
+
+ return priv->xfer_ret;
+}
+
+/**
+ * loongson_spi_io_reginit() - Restore the basic controller configuration
+ * @priv: controller private data
+ *
+ * Select master full-duplex pin directions and enable the SPI peripheral.
+ */
+static void loongson_spi_io_reginit(struct loongson_spi_priv *priv)
+{
+ u32 cr1;
+
+ /* CFG3 is only writable while SPE is clear; cfg_write() handles that. */
+ loongson_spi_io_cfg_write(priv, LOONGSON_SPI_IO_CFG3_REG,
+ LOONGSON_SPI_IO_CFG3_MSTR |
+ LOONGSON_SPI_IO_CFG3_DIE |
+ LOONGSON_SPI_IO_CFG3_DOE);
+
+ cr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_CR1_REG);
+ cr1 &= ~LOONGSON_SPI_IO_CR1_START_MASK;
+ cr1 |= LOONGSON_SPI_IO_CR1_SPE;
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR1_REG, cr1);
+}
+
+/**
+ * loongson_spi_io_handle_err() - Quiesce hardware after a failed message
+ * @ctrl: SPI controller
+ * @msg: failed SPI message
+ *
+ * Stop the engine, discard stale RX FIFO data, clear status and software
+ * transfer state, then restore the basic configuration.
+ */
+static void loongson_spi_io_handle_err(struct spi_controller *ctrl,
+ struct spi_message *msg)
+{
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(ctrl);
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
+ synchronize_irq(priv->irq);
+
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_START_MASK, 0);
+
+ loongson_spi_io_set_thresh(priv);
+
+ while (FIELD_GET(LOONGSON_SPI_IO_SR2_RXFLV,
+ loongson_spi_readl(priv, LOONGSON_SPI_IO_SR2_REG)))
+ loongson_spi_io_read_dr(priv);
+
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
+ LOONGSON_SPI_IO_SR1_EOT | LOONGSON_SPI_IO_SR1_ERR_MASK);
+
+ priv->tx_buf = NULL;
+ priv->rx_buf = NULL;
+ priv->remaining = 0;
+ priv->batch_size = 0;
+ priv->xfer_ret = 0;
+ priv->xfer_mode = XFER_IDLE;
+ reinit_completion(&priv->xfer_completion);
+
+ loongson_spi_io_reginit(priv);
+}
+
+static int loongson_spi_io_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct loongson_spi_priv *priv;
+ struct spi_controller *host;
+ struct clk *clk;
+ int ret;
+
+ host = devm_spi_alloc_host(dev, sizeof(*priv));
+ if (!host)
+ return -ENOMEM;
+
+ host->mode_bits = SPI_MODE_X_MASK | SPI_LSB_FIRST | SPI_3WIRE;
+ host->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
+ host->setup = loongson_spi_io_setup;
+ host->transfer_one = loongson_spi_io_transfer_one;
+ host->set_cs = loongson_spi_io_set_cs;
+ host->handle_err = loongson_spi_io_handle_err;
+ host->num_chipselect = 1;
+ dev_set_drvdata(dev, host);
+
+ priv = spi_controller_get_devdata(host);
+
+ priv->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(priv->base))
+ return PTR_ERR(priv->base);
+
+ priv->controller = host;
+ priv->dev = dev;
+ priv->xfer_mode = XFER_IDLE;
+ priv->batch_size = 0;
+
+ init_completion(&priv->xfer_completion);
+
+ clk = devm_clk_get_enabled(dev, NULL);
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk), "unable to get clock");
+ priv->clk_rate = clk_get_rate(clk);
+ if (!priv->clk_rate)
+ return dev_err_probe(dev, -EINVAL, "clock rate is zero");
+
+ /* the divider is >= 2, and BRINT is 8 bits wide. */
+ host->max_speed_hz = (u32)min_t(u64, div_u64(priv->clk_rate, 2), U32_MAX);
+ host->min_speed_hz = (u32)min_t(u64, DIV_ROUND_UP_ULL(priv->clk_rate, 255),
+ U32_MAX);
+
+ priv->irq = platform_get_irq(pdev, 0);
+ if (priv->irq < 0)
+ return dev_err_probe(dev, priv->irq, "no IRQ resource");
+
+ ret = devm_request_irq(dev, priv->irq, loongson_spi_io_irq_handler,
+ IRQF_SHARED, dev_name(dev), priv);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to request IRQ");
+
+ loongson_spi_io_reginit(priv);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
+
+ return devm_spi_register_controller(dev, host);
+}
+
+static const struct of_device_id loongson_spi_io_id_table[] = {
+ { .compatible = "loongson,ls2k0300-spi-io" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, loongson_spi_io_id_table);
+
+static struct platform_driver loongson_spi_io_plat_driver = {
+ .probe = loongson_spi_io_probe,
+ .driver = {
+ .name = "loongson-spi-io",
+ .of_match_table = loongson_spi_io_id_table,
+ },
+};
+module_platform_driver(loongson_spi_io_plat_driver);
+
+MODULE_DESCRIPTION("Loongson spi io controller driver");
+MODULE_AUTHOR("Loongson Technology Corporation Limited");
+MODULE_LICENSE("GPL");
--
2.52.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver
2026-11-23 9:44 ` [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver Binbin Zhou
@ 2026-11-23 10:05 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-11-23 10:05 UTC (permalink / raw)
To: Binbin Zhou; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] spi: loongson-io: missing error IRQ enables and uncleared SR1
in ISR
- [High] spi: loongson-io: 32-bit divisor truncation in
loongson_spi_io_timeout()
- [Medium] spi: loongson-io: IRQ enabled before hardware and batch init
in probe/xfer
- [Medium] spi: loongson-io: unsynchronized timeout in
loongson_spi_io_transfer_one()
- [Low] spi: loongson-io: inverted @high CS contract in
loongson_spi_io_set_cs()
--
commit 7a2b450ad49a89d82f040f8bc7f2f42d5aadeb60
Author: Binbin Zhou <zhoubinbin@loongson.cn>
spi: loongson-io: Add SPI-IO interrupt-driven driver
This commit adds an interrupt-driven platform driver for the Loongson-2K0300
SPI-IO controller (SPI2 and SPI3).
> diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
> new file mode 100644
> index 0000000000000..e273c9f4a9430
> --- /dev/null
> +++ b/drivers/spi/spi-loongson-io.c
> @@ -0,0 +1,737 @@
[ ... ]
> +static unsigned long loongson_spi_io_timeout(struct loongson_spi_priv *priv,
> + struct spi_transfer *xfer)
> +{
> + u64 frames = xfer->len / priv->frame_bytes;
> + u64 nsec = frames * priv->bits_per_word * NSEC_PER_SEC;
> + u32 rate = priv->hz ? priv->hz : 1000;
> + u64 msec = DIV_ROUND_UP_ULL(nsec, (u64)rate * NSEC_PER_MSEC);
> +
> + return msecs_to_jiffies(max_t(u64, msec * 2, 1000));
> +}
[Severity: High]
Does DIV_ROUND_UP_ULL() truncate the 64-bit divisor (u64)rate * NSEC_PER_MSEC
to 32 bits?
DIV_ROUND_UP_ULL() expands to do_div(), which casts its divisor to uint32_t.
For any SPI clock rate >= 4,295 Hz, (u64)rate * NSEC_PER_MSEC exceeds
U32_MAX, corrupting the timeout calculation, and when rate is a multiple of
67,108,864 Hz (2^26 Hz), the truncated 32-bit divisor becomes zero and causes
a division-by-zero panic. In addition, because transfers run in 4-byte IRQ
batches, computing the timeout from ideal wire speed alone leaves large
high-speed transfers clamped at the 1000 ms floor despite per-batch interrupt
and MMIO overhead; using DIV64_U64_ROUND_UP() avoids the divisor truncation.
[ ... ]
> +/**
> + * loongson_spi_io_set_cs() - Set the hardware chip-select output
> + * @spi: SPI target device
> + * @high: true to assert chip select
> + *
> + * The SSREV bit controls the hardware chip-select assertion state.
> + */
> +static void loongson_spi_io_set_cs(struct spi_device *spi, bool high)
> +{
> + struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
> + u32 val = high ? LOONGSON_SPI_IO_CR1_SSREV : 0;
> +
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> + LOONGSON_SPI_IO_CR1_SSREV, val);
> +}
[Severity: Low]
Is the @high polarity in loongson_spi_io_set_cs() inverted relative to the
SPI core's set_cs() contract?
Because SPI_CS_HIGH is not set in host->mode_bits, spi_set_cs() passes
high == false (logic low) to assert an active-low chip select and
high == true (logic high) to deassert it. If LOONGSON_SPI_IO_CR1_SSREV
asserts chip select as the comment states, the ternary condition is inverted;
if it drives the CS line high, the kernel-doc comment is inverted.
[ ... ]
> +static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
> +{
> + struct loongson_spi_priv *priv = dev_id;
> + u32 rx_data;
> + u32 sr1;
> +
> + sr1 = loongson_spi_readl(priv, LOONGSON_SPI_IO_SR1_REG);
> +
> + if (sr1 & LOONGSON_SPI_IO_SR1_ERR_MASK) {
> + dev_err(&priv->controller->dev, "SPI error: SR1=0x%x", sr1);
> + loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
> + priv->xfer_ret = -EIO;
> + complete(&priv->xfer_completion);
> + return IRQ_HANDLED;
> + }
[Severity: High]
Does the error path in loongson_spi_io_irq_handler() leave
LOONGSON_SPI_IO_SR1_ERR_MASK latched in LOONGSON_SPI_IO_SR1_REG while none of
the error interrupt bits are enabled in LOONGSON_SPI_IO_IER_REG?
loongson_spi_io_transfer_one() only enables LOONGSON_SPI_IO_IER_EOTIE, so an
overrun, underrun, mode fault, or CRC error that halts a transfer before EOT
never raises an interrupt and stalls until the transfer timeout. Conversely,
because loongson_spi_io_irq_handler() does not mask sr1 against
LOONGSON_SPI_IO_IER_REG and does not write LOONGSON_SPI_IO_SR1_ERR_MASK back
to LOONGSON_SPI_IO_SR1_REG to clear the status bits, any latched error bit
causes every subsequent interrupt from another device on the shared IRQ line
to re-enter this error branch, log an error, and complete
priv->xfer_completion while idle.
[ ... ]
> + reinit_completion(&priv->xfer_completion);
> + priv->xfer_mode = XFER_INTERRUPT;
> +
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> + LOONGSON_SPI_IO_CR1_AUTOSUS,
> + LOONGSON_SPI_IO_CR1_AUTOSUS);
> + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
> + LOONGSON_SPI_IO_IER_EOTIE,
> + LOONGSON_SPI_IO_IER_EOTIE);
> +
> + loongson_spi_io_start_batch(priv);
> +
> + ret = wait_for_completion_timeout(&priv->xfer_completion,
> + loongson_spi_io_timeout(priv, xfer));
> + priv->xfer_mode = XFER_IDLE;
> +
> + if (ret == 0) {
> + loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
> + dev_err(&spi->dev, "Interrupt transfer timeout");
> + return -ETIMEDOUT;
> + }
> +
> + return priv->xfer_ret;
> +}
[Severity: Medium]
Can a timeout in loongson_spi_io_transfer_one() race with an in-flight
loongson_spi_io_irq_handler() on another CPU?
CPU0 CPU1
loongson_spi_io_transfer_one()
wait_for_completion_timeout()
/* times out */
loongson_spi_io_irq_handler()
switch (priv->xfer_mode)
case XFER_INTERRUPT:
priv->xfer_mode = XFER_IDLE;
loongson_spi_writel(..., IER_REG, 0);
return -ETIMEDOUT;
spi_transfer_one_message()
spi_dma_sync_for_cpu()
memcpy(priv->rx_buf, ...)
loongson_spi_io_start_batch()
loongson_spi_update_bits(CR1_REG)
spi_set_cs()
loongson_spi_io_set_cs()
loongson_spi_update_bits(CR1_REG) <-- unlocked RMW race
loongson_spi_io_handle_err()
synchronize_irq(priv->irq)
Because loongson_spi_io_transfer_one() returns -ETIMEDOUT without a spinlock
or synchronize_irq(priv->irq), spi_transfer_one_message() runs
spi_dma_sync_for_cpu() and loongson_spi_io_set_cs() before calling
loongson_spi_io_handle_err(). A concurrent ISR that already read
XFER_INTERRUPT can touch priv->rx_buf and priv->tx_buf, start another hardware
batch, and perform an unlocked read-modify-write on LOONGSON_SPI_IO_CR1_REG
concurrently with loongson_spi_io_set_cs(), clobbering
LOONGSON_SPI_IO_CR1_SSREV or LOONGSON_SPI_IO_CR1_CSTART.
[ ... ]
> + ret = devm_request_irq(dev, priv->irq, loongson_spi_io_irq_handler,
> + IRQF_SHARED, dev_name(dev), priv);
> + if (ret)
> + return dev_err_probe(dev, ret, "failed to request IRQ");
> +
> + loongson_spi_io_reginit(priv);
> + loongson_spi_writel(priv, LOONGSON_SPI_IO_IER_REG, 0);
[Severity: Medium]
Can an interrupt on the shared IRQ line fire in loongson_spi_io_probe() before
the controller registers are initialized and LOONGSON_SPI_IO_IER_REG is
masked?
devm_request_irq() registers the IRQF_SHARED handler before
loongson_spi_io_reginit() runs and before LOONGSON_SPI_IO_IER_REG is cleared
(and without clearing stale status flags in LOONGSON_SPI_IO_SR1_REG).
Similarly, in loongson_spi_io_transfer_one(), priv->xfer_mode = XFER_INTERRUPT
and LOONGSON_SPI_IO_IER_EOTIE are set before loongson_spi_io_start_batch()
initializes priv->batch_size and clears LOONGSON_SPI_IO_SR1_EOT, leaving a
window where a shared or stale IRQ sees the previous transfer's
priv->batch_size.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795425389.git.zhoubinbin@loongson.cn?part=3
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 4/5] spi: loongson-io: Add DMA support to SPI-IO driver
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
` (2 preceding siblings ...)
2026-11-23 9:44 ` [PATCH 3/5] spi: loongson-io: Add SPI-IO interrupt-driven driver Binbin Zhou
@ 2026-11-23 9:45 ` Binbin Zhou
2026-11-23 9:45 ` [PATCH 5/5] MAINTAINERS: Add myself as Loongson SPI maintainer Binbin Zhou
4 siblings, 0 replies; 8+ messages in thread
From: Binbin Zhou @ 2026-11-23 9:45 UTC (permalink / raw)
To: Binbin Zhou, Huacai Chen, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Yinbo Zhu, Mark Brown
Cc: Huacai Chen, Xuerui Wang, loongarch, devicetree, linux-spi,
Bingxiong Li, Binbin Zhou
Request optional "tx" and "rx" DMA channels. Their absence is
non-fatal and leaves the driver in interrupt-only mode so probe
does not fail when DMA providers are not yet available.
DMA is enabled for transfers that meet all of: four-byte
alignment, length at or above the runtime-tunable dma_threshold,
and both TX and RX buffers present.
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
---
drivers/spi/spi-loongson-io.c | 399 +++++++++++++++++++++++++++++++++-
1 file changed, 388 insertions(+), 11 deletions(-)
diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
index e273c9f4a943..c153e483ab41 100644
--- a/drivers/spi/spi-loongson-io.c
+++ b/drivers/spi/spi-loongson-io.c
@@ -7,6 +7,8 @@
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/completion.h>
+#include <linux/dmaengine.h>
+#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
@@ -14,6 +16,7 @@
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
+#include <linux/scatterlist.h>
#include <linux/string.h>
/* Loongson SPI-IO Controller registers */
@@ -42,11 +45,20 @@
/* Bitfields of LOONGSON_SPI_IO_CR2_REG */
#define LOONGSON_SPI_IO_CR2_RXFTHLV GENMASK(1, 0)
+#define LOONGSON_SPI_IO_CR2_RXDMAEN BIT(7)
#define LOONGSON_SPI_IO_CR2_TXFTHLV GENMASK(9, 8)
+#define LOONGSON_SPI_IO_CR2_TXDMAEN BIT(15)
+
+#define LOONGSON_SPI_IO_CR2_DMAEN_MASK (LOONGSON_SPI_IO_CR2_RXDMAEN | \
+ LOONGSON_SPI_IO_CR2_TXDMAEN)
+
+#define LOONGSON_SPI_IO_CR2_MAX_FTHLV FIELD_MAX(LOONGSON_SPI_IO_CR2_RXFTHLV)
/* Bitfields of LOONGSON_SPI_IO_CR3_REG */
#define LOONGSON_SPI_IO_CR3_TSIZE GENMASK(15, 0)
+#define LOONGSON_SPI_IO_MAX_FRAMES (FIELD_MAX(LOONGSON_SPI_IO_CR3_TSIZE) + 1)
+
/* Bitfields of LOONGSON_SPI_IO_CR4_REG */
#define LOONGSON_SPI_IO_CR4_CTSIZE GENMASK(15, 0)
@@ -118,10 +130,12 @@
* enum xfer_mode - Loongson SPI transfer mode
* @XFER_IDLE: no transfer is in progress
* @XFER_INTERRUPT: interrupt-driven batch transfer
+ * @XFER_DMA: DMA-driven transfer
*/
enum xfer_mode {
XFER_IDLE,
XFER_INTERRUPT,
+ XFER_DMA,
};
/**
@@ -141,6 +155,10 @@ enum xfer_mode {
* @xfer_ret: result of the current transfer
* @batch_size: number of bytes in the current interrupt batch
* @xfer_completion: completion signaled when the current transfer finishes
+ * @dma_tx: DMA channel for TX
+ * @dma_rx: DMA channel for RX
+ * @use_dma: true if both DMA channels are available
+ * @phys_base: physical base address used for DMA slave configuration
* @xfer_mode: current transfer mode
*/
struct loongson_spi_priv {
@@ -160,9 +178,19 @@ struct loongson_spi_priv {
int xfer_ret;
u32 batch_size;
struct completion xfer_completion;
+
+ struct dma_chan *dma_tx;
+ struct dma_chan *dma_rx;
+ bool use_dma;
+ phys_addr_t phys_base;
enum xfer_mode xfer_mode;
};
+/* Module parameter: DMA threshold (bytes), adjustable at runtime */
+static unsigned int dma_threshold = 1024;
+module_param(dma_threshold, uint, 0644);
+MODULE_PARM_DESC(dma_threshold, "Minimum transfer length to use DMA (default 1024)");
+
static void loongson_spi_writel(struct loongson_spi_priv *priv, u32 reg, u32 data)
{
writel(data, priv->base + reg);
@@ -264,11 +292,59 @@ static unsigned long loongson_spi_io_timeout(struct loongson_spi_priv *priv,
return msecs_to_jiffies(max_t(u64, msec * 2, 1000));
}
-static void loongson_spi_io_set_thresh(struct loongson_spi_priv *priv)
+static void loongson_spi_io_set_thresh(struct loongson_spi_priv *priv, bool dma)
{
+ u32 val = 0;
+
+ if (dma)
+ val = FIELD_PREP(LOONGSON_SPI_IO_CR2_RXFTHLV, LOONGSON_SPI_IO_CR2_MAX_FTHLV) |
+ FIELD_PREP(LOONGSON_SPI_IO_CR2_TXFTHLV, LOONGSON_SPI_IO_CR2_MAX_FTHLV);
+
loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
LOONGSON_SPI_IO_CR2_RXFTHLV |
- LOONGSON_SPI_IO_CR2_TXFTHLV, 0);
+ LOONGSON_SPI_IO_CR2_TXFTHLV, val);
+}
+
+/**
+ * loongson_spi_io_dma_drain_rx() - Drain RX FIFO residue after DMA
+ * @priv: controller private data
+ * @xfer: completed SPI transfer
+ *
+ * DMA can leave a tail shorter than its request threshold in the RX FIFO.
+ * Both DMA channels must already be quiescent. Synchronize the RX mapping,
+ * append the residue to the receive buffer, then return mapping ownership
+ * to the device. Residue from a dummy RX buffer is discarded by SPI core.
+ */
+static void loongson_spi_io_dma_drain_rx(struct loongson_spi_priv *priv,
+ struct spi_transfer *xfer)
+{
+ struct device *rx_dev = dmaengine_get_dma_device(priv->dma_rx);
+ u32 rxflv = FIELD_GET(LOONGSON_SPI_IO_SR2_RXFLV,
+ loongson_spi_readl(priv, LOONGSON_SPI_IO_SR2_REG));
+ u8 frame_bytes = priv->frame_bytes;
+ u8 *dst = NULL;
+ u32 data;
+
+ if (!rxflv)
+ return;
+
+ if (xfer->rx_buf && rxflv <= xfer->len)
+ dst = (u8 *)xfer->rx_buf + xfer->len - rxflv;
+
+ /* SPI core skips cache sync while transfer_one() is running. */
+ dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
+ loongson_spi_io_set_thresh(priv, false);
+
+ while (rxflv) {
+ data = loongson_spi_io_read_dr(priv);
+ if (dst) {
+ memcpy(dst, &data, frame_bytes);
+ dst += frame_bytes;
+ }
+ rxflv -= frame_bytes;
+ }
+
+ dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
}
/**
@@ -423,6 +499,42 @@ static int loongson_spi_io_setup(struct spi_device *spi)
return 0;
}
+/**
+ * loongson_spi_can_dma() - Check whether a transfer can use DMA
+ * @ctrl: SPI controller
+ * @spi: SPI target device
+ * @xfer: SPI transfer to check
+ *
+ * DMA requires both channels, a four-byte-aligned length and a transfer at
+ * least as large as dma_threshold. Three-wire transfers use interrupt mode
+ * because the DMA path requires both TX and RX scatterlists.
+ *
+ * Return: true when DMA is supported for @xfer.
+ */
+static bool loongson_spi_can_dma(struct spi_controller *ctrl, struct spi_device *spi,
+ struct spi_transfer *xfer)
+{
+ struct loongson_spi_priv *priv = spi_controller_get_devdata(ctrl);
+
+ if (!priv->use_dma)
+ return false;
+
+ /* The DMA path requires both TX and RX scatterlists. */
+ if (spi->mode & SPI_3WIRE)
+ return false;
+
+ if (xfer->len < dma_threshold)
+ return false;
+
+ if (!xfer->tx_buf && !xfer->rx_buf)
+ return false;
+
+ if (xfer->len % 4)
+ return false;
+
+ return true;
+}
+
/**
* loongson_spi_io_start_batch() - Start one interrupt-driven FIFO batch
* @priv: controller private data
@@ -464,14 +576,191 @@ static int loongson_spi_io_start_batch(struct loongson_spi_priv *priv)
return batch_size;
}
+/**
+ * loongson_spi_dma_config_and_submit() - Prepare RX and TX slave DMA
+ * @priv: controller private data
+ * @xfer: SPI transfer with scatterlists mapped by SPI core
+ *
+ * Configure both DMA channels for the shared data register and submit RX
+ * before TX. Descriptors remain pending until the caller issues them.
+ *
+ * Return: 0 on success, or a negative error code on configuration,
+ * preparation or submission failure.
+ */
+static int loongson_spi_dma_config_and_submit(struct loongson_spi_priv *priv,
+ struct spi_transfer *xfer)
+{
+ struct dma_async_tx_descriptor *tx_desc, *rx_desc;
+ struct dma_slave_config tx_cfg = {0}, rx_cfg = {0};
+ struct device *dev = priv->dev;
+ int ret;
+
+ tx_cfg.direction = DMA_MEM_TO_DEV;
+ tx_cfg.dst_addr = priv->phys_base + LOONGSON_SPI_IO_DR_REG;
+ tx_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+ tx_cfg.dst_maxburst = 1;
+
+ ret = dmaengine_slave_config(priv->dma_tx, &tx_cfg);
+ if (ret) {
+ dev_err(dev, "TX DMA config failed: %d", ret);
+ return ret;
+ }
+
+ rx_cfg.direction = DMA_DEV_TO_MEM;
+ rx_cfg.src_addr = priv->phys_base + LOONGSON_SPI_IO_DR_REG;
+ rx_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+ rx_cfg.src_maxburst = 1;
+
+ ret = dmaengine_slave_config(priv->dma_rx, &rx_cfg);
+ if (ret) {
+ dev_err(dev, "RX DMA config failed: %d", ret);
+ return ret;
+ }
+
+ rx_desc = dmaengine_prep_slave_sg(priv->dma_rx, xfer->rx_sg.sgl,
+ xfer->rx_sg.nents, DMA_DEV_TO_MEM,
+ DMA_CTRL_ACK);
+ if (!rx_desc) {
+ dev_err(dev, "RX DMA prep_slave_sg failed");
+ return -EINVAL;
+ }
+
+ ret = dma_submit_error(dmaengine_submit(rx_desc));
+ if (ret) {
+ dev_err(dev, "RX DMA submit failed");
+ return ret;
+ }
+
+ tx_desc = dmaengine_prep_slave_sg(priv->dma_tx, xfer->tx_sg.sgl,
+ xfer->tx_sg.nents, DMA_MEM_TO_DEV,
+ DMA_CTRL_ACK);
+ if (!tx_desc) {
+ dev_err(dev, "TX DMA prep_slave_sg failed");
+ return -EINVAL;
+ }
+
+ ret = dma_submit_error(dmaengine_submit(tx_desc));
+ if (ret) {
+ dev_err(dev, "TX DMA submit failed");
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * loongson_spi_dma_wait_eot() - Wait for the current DMA chunk to finish
+ * @priv: controller private data
+ * @xfer: SPI transfer used to calculate the timeout
+ *
+ * Return: 0 after EOT, %-ETIMEDOUT if EOT does not arrive, or the error
+ * recorded by the IRQ handler.
+ */
+static int loongson_spi_dma_wait_eot(struct loongson_spi_priv *priv,
+ struct spi_transfer *xfer)
+{
+ if (!wait_for_completion_timeout(&priv->xfer_completion,
+ loongson_spi_io_timeout(priv, xfer))) {
+ dev_err(priv->dev, "DMA transfer timeout (no EOT)");
+ return -ETIMEDOUT;
+ }
+
+ return priv->xfer_ret;
+}
+
+/**
+ * loongson_spi_dma_transfer() - Run one DMA-driven SPI transfer
+ * @priv: controller private data
+ * @xfer: SPI transfer with TX and RX scatterlists
+ * @bus_started: set to true once CSTART has been written
+ *
+ * One pair of DMA descriptors covers the complete transfer. The SPI engine
+ * is restarted in chunks of at most %LOONGSON_SPI_IO_MAX_FRAMES because its
+ * transfer-count register is 16 bits wide. RX is issued before TX.
+ *
+ * All exits disable and synchronize both DMA channels. A successful transfer
+ * then drains the RX FIFO residue. @bus_started tells the caller whether a
+ * failure can safely retry the transfer in interrupt mode: once it is true,
+ * retrying would duplicate bus traffic and is therefore not allowed.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int loongson_spi_dma_transfer(struct loongson_spi_priv *priv,
+ struct spi_transfer *xfer,
+ bool *bus_started)
+{
+ u32 frames = xfer->len / priv->frame_bytes;
+ u32 done, chunk_frames;
+ int ret;
+
+ *bus_started = false;
+
+ ret = loongson_spi_dma_config_and_submit(priv, xfer);
+ if (ret)
+ goto out_stop;
+
+ priv->xfer_ret = 0;
+ priv->xfer_mode = XFER_DMA;
+
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_AUTOSUS,
+ LOONGSON_SPI_IO_CR1_AUTOSUS);
+
+ /* RX must be ready before TX can start filling the controller FIFO. */
+ dma_async_issue_pending(priv->dma_rx);
+ dma_async_issue_pending(priv->dma_tx);
+
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
+ LOONGSON_SPI_IO_CR2_DMAEN_MASK,
+ LOONGSON_SPI_IO_CR2_DMAEN_MASK);
+
+ for (done = 0; done < frames; done += chunk_frames) {
+ chunk_frames = min_t(u32, frames - done,
+ LOONGSON_SPI_IO_MAX_FRAMES);
+
+ priv->xfer_ret = 0;
+ reinit_completion(&priv->xfer_completion);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR3_REG,
+ chunk_frames - 1);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_CR4_REG, 0);
+ loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
+ LOONGSON_SPI_IO_SR1_EOT);
+
+ *bus_started = true;
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_CSTART,
+ LOONGSON_SPI_IO_CR1_CSTART);
+
+ ret = loongson_spi_dma_wait_eot(priv, xfer);
+ if (ret)
+ break;
+ }
+
+out_stop:
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_CSTART, 0);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
+ LOONGSON_SPI_IO_CR2_DMAEN_MASK, 0);
+ priv->xfer_mode = XFER_IDLE;
+
+ /* Quiesce DMA before letting the CPU handle the RX FIFO residue. */
+ dmaengine_terminate_sync(priv->dma_tx);
+ dmaengine_terminate_sync(priv->dma_rx);
+
+ if (!ret && *bus_started)
+ loongson_spi_io_dma_drain_rx(priv, xfer);
+
+ return ret;
+}
+
/**
* loongson_spi_io_irq_handler() - Handle SPI error and end-of-transfer IRQs
* @irq: interrupt number
* @dev_id: controller private data
*
* In interrupt mode, consume the current RX batch and start the next one.
- * Controller errors disable further interrupts and complete the wait with an
- * error status.
+ * In DMA mode, stop the engine and wake the chunk waiter. Controller errors
+ * disable further interrupts and complete the wait with an error status.
*
* Return: %IRQ_HANDLED for a recognized error or EOT, otherwise %IRQ_NONE.
*/
@@ -497,6 +786,12 @@ static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG, LOONGSON_SPI_IO_SR1_EOT);
switch (priv->xfer_mode) {
+ case XFER_DMA:
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
+ LOONGSON_SPI_IO_CR1_CSTART, 0);
+ priv->xfer_ret = 0;
+ complete(&priv->xfer_completion);
+ break;
case XFER_INTERRUPT:
for (unsigned int i = 0; i < priv->batch_size;
i += priv->frame_bytes) {
@@ -534,8 +829,10 @@ static irqreturn_t loongson_spi_io_irq_handler(int irq, void *dev_id)
* @spi: SPI target device
* @xfer: transfer to execute
*
- * Apply clock, mode and pin-direction state before running interrupt-driven
- * FIFO batches.
+ * Apply clock, mode and pin-direction state before selecting DMA or
+ * interrupt-driven FIFO batches. DMA setup failures may fall back to
+ * interrupt mode only while the bus has not started; runtime DMA failures
+ * are returned to SPI core without replaying the transfer.
*
* Return: 0 on success, or a negative error code.
*/
@@ -543,6 +840,8 @@ static int loongson_spi_io_transfer_one(struct spi_controller *ctrl, struct spi_
struct spi_transfer *xfer)
{
struct loongson_spi_priv *priv = spi_controller_get_devdata(spi->controller);
+ bool bus_started = false;
+ bool use_dma;
int ret;
loongson_spi_io_update_state(spi, xfer);
@@ -563,7 +862,27 @@ static int loongson_spi_io_transfer_one(struct spi_controller *ctrl, struct spi_
loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
LOONGSON_SPI_IO_SR1_EOT | LOONGSON_SPI_IO_SR1_ERR_MASK);
- loongson_spi_io_set_thresh(priv);
+ use_dma = ctrl->can_dma && ctrl->can_dma(ctrl, spi, xfer);
+ loongson_spi_io_set_thresh(priv, use_dma);
+
+ if (use_dma) {
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_SR1_REG,
+ LOONGSON_SPI_IO_SR1_EOT,
+ LOONGSON_SPI_IO_SR1_EOT);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
+ LOONGSON_SPI_IO_IER_EOTIE,
+ LOONGSON_SPI_IO_IER_EOTIE);
+ ret = loongson_spi_dma_transfer(priv, xfer, &bus_started);
+ if (!ret)
+ return 0;
+ if (bus_started) {
+ dev_err(&spi->dev, "DMA transfer failed: %d", ret);
+ return ret;
+ }
+
+ dev_dbg(&spi->dev, "DMA setup failed (%d), using interrupt mode", ret);
+ loongson_spi_io_set_thresh(priv, false);
+ }
priv->tx_buf = xfer->tx_buf;
priv->rx_buf = xfer->rx_buf;
@@ -622,8 +941,8 @@ static void loongson_spi_io_reginit(struct loongson_spi_priv *priv)
* @ctrl: SPI controller
* @msg: failed SPI message
*
- * Stop the engine, discard stale RX FIFO data, clear status and software
- * transfer state, then restore the basic configuration.
+ * Stop the engine and DMA channels, discard stale RX FIFO data, clear
+ * status and software transfer state, then restore the basic configuration.
*/
static void loongson_spi_io_handle_err(struct spi_controller *ctrl,
struct spi_message *msg)
@@ -635,8 +954,15 @@ static void loongson_spi_io_handle_err(struct spi_controller *ctrl,
loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
LOONGSON_SPI_IO_CR1_START_MASK, 0);
+ loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
+ LOONGSON_SPI_IO_CR2_DMAEN_MASK, 0);
- loongson_spi_io_set_thresh(priv);
+ loongson_spi_io_set_thresh(priv, false);
+
+ if (priv->use_dma) {
+ dmaengine_terminate_sync(priv->dma_tx);
+ dmaengine_terminate_sync(priv->dma_rx);
+ }
while (FIELD_GET(LOONGSON_SPI_IO_SR2_RXFLV,
loongson_spi_readl(priv, LOONGSON_SPI_IO_SR2_REG)))
@@ -656,11 +982,40 @@ static void loongson_spi_io_handle_err(struct spi_controller *ctrl,
loongson_spi_io_reginit(priv);
}
+/**
+ * loongson_spi_io_request_dma_chan() - Request an optional DMA channel
+ * @dev: controller device
+ * @name: DMA channel name, either "tx" or "rx"
+ *
+ * Absence of either channel is non-fatal and selects interrupt-only mode.
+ * Probe deferral is preserved so the DMA provider can be probed later.
+ *
+ * Return: a DMA channel on success, NULL when unavailable, or an ERR_PTR
+ * containing %-EPROBE_DEFER.
+ */
+static struct dma_chan *loongson_spi_io_request_dma_chan(struct device *dev,
+ const char *name)
+{
+ struct dma_chan *chan = devm_dma_request_chan(dev, name);
+
+ if (IS_ERR(chan)) {
+ int ret = PTR_ERR(chan);
+
+ if (ret == -EPROBE_DEFER)
+ return chan;
+ dev_info(dev, "%s DMA unavailable with %d, using interrupt mode", name, ret);
+ return NULL;
+ }
+
+ return chan;
+}
+
static int loongson_spi_io_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct loongson_spi_priv *priv;
struct spi_controller *host;
+ struct resource *res;
struct clk *clk;
int ret;
@@ -679,13 +1034,15 @@ static int loongson_spi_io_probe(struct platform_device *pdev)
priv = spi_controller_get_devdata(host);
- priv->base = devm_platform_ioremap_resource(pdev, 0);
+ priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
if (IS_ERR(priv->base))
return PTR_ERR(priv->base);
priv->controller = host;
+ priv->phys_base = res->start;
priv->dev = dev;
priv->xfer_mode = XFER_IDLE;
+ priv->use_dma = false;
priv->batch_size = 0;
init_completion(&priv->xfer_completion);
@@ -702,6 +1059,26 @@ static int loongson_spi_io_probe(struct platform_device *pdev)
host->min_speed_hz = (u32)min_t(u64, DIV_ROUND_UP_ULL(priv->clk_rate, 255),
U32_MAX);
+ priv->dma_tx = loongson_spi_io_request_dma_chan(dev, "tx");
+ if (IS_ERR(priv->dma_tx))
+ return PTR_ERR(priv->dma_tx);
+
+ priv->dma_rx = loongson_spi_io_request_dma_chan(dev, "rx");
+ if (IS_ERR(priv->dma_rx))
+ return PTR_ERR(priv->dma_rx);
+
+ if (priv->dma_tx && priv->dma_rx) {
+ priv->use_dma = true;
+ /* Let SPI core provide and map dummy buffers for full-duplex DMA. */
+ host->flags |= SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
+ host->dma_tx = priv->dma_tx;
+ host->dma_rx = priv->dma_rx;
+ host->can_dma = loongson_spi_can_dma;
+ dev_info(dev, "DMA channels acquired");
+ } else {
+ dev_info(dev, "DMA unavailable, using interrupt mode only");
+ }
+
priv->irq = platform_get_irq(pdev, 0);
if (priv->irq < 0)
return dev_err_probe(dev, priv->irq, "no IRQ resource");
--
2.52.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 5/5] MAINTAINERS: Add myself as Loongson SPI maintainer
2026-11-23 9:44 [PATCH 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
` (3 preceding siblings ...)
2026-11-23 9:45 ` [PATCH 4/5] spi: loongson-io: Add DMA support to SPI-IO driver Binbin Zhou
@ 2026-11-23 9:45 ` Binbin Zhou
4 siblings, 0 replies; 8+ messages in thread
From: Binbin Zhou @ 2026-11-23 9:45 UTC (permalink / raw)
To: Binbin Zhou, Huacai Chen, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Yinbo Zhu, Mark Brown
Cc: Huacai Chen, Xuerui Wang, loongarch, devicetree, linux-spi,
Bingxiong Li, Binbin Zhou
Add myself as a maintainer for the Loongson SPI driver. I have been
working on the Loongson SPI flash and SPI IO controller drivers and
would like to help review and maintain them.
Signed-off-by: Binbin Zhou <zhoubinbin@loongson.cn>
---
MAINTAINERS | 1 +
1 file changed, 1 insertion(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 70789ce6e507..8223f1b61348 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15738,6 +15738,7 @@ F: include/dt-bindings/clock/loongson,ls2k-clk.h
LOONGSON SPI DRIVER
M: Yinbo Zhu <zhuyinbo@loongson.cn>
+M: Binbin Zhou <zhoubinbin@loongson.cn>
L: linux-spi@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/spi/loongson,ls2k-spi.yaml
--
2.52.0
^ permalink raw reply related [flat|nested] 8+ messages in thread