Skip to contentSkip to conversation

spi: support the SpacemiT K1 SPI controller

PATCH In review Alex Elder 2 participants 6 messages Nov 14, 2025 – Nov 20, 2025

Subsystems (4)
lore
Share & tools
lore

Alex Elder

author
#0 lore raw

v7 cover spi: support the SpacemiT K1 SPI controller

This series adds support for the SPI controller found in the SpacemiT K1 SoC. The driver currently supports only master mode. The controller has two 32-entry FIFOs and supports PIO and DMA for transfers.

Version 7 fixes two bugs, and now builds the driver as a kernel module by default.

(Note, this is a distinct series from the QSPI driver, which was merged recently.)

-Alex

This series is available here:
https://github.com/riscstar/linux/tree/outgoing/spi-v7

Between version 6 and version 7:
- DIV_ROUND_UP_ULL() is now used when setting the speed, to address two errors reported by the Intel kernel test robot on 32-bit builds
- Fixed a bug interpreting the resource pointer in k1_spi_dma_cleanup()
- The driver is now built as a module by default, if ARCH_SPACEMIT is defined

Here is version 6 of this series:
https://lore.kernel.org/lkml/[email protected]/

Between version 5 and version 6:
- Rebase only

Here is version 5 of this series:
https://lore.kernel.org/lkml/[email protected]/

Between version 4 and version 5:
- Added Yixun's Reviewed-by tag on patch 3

Here is version 4 of this series:
https://lore.kernel.org/lkml/[email protected]/

Between version 3 and version 4 (all suggested by Yixun):
- Fixed an underrun/overrun comment error
- Renamed a pinctrl node
- Formatted dmas and dma-names properties on one line

Here is version 3 of this series:
https://lore.kernel.org/lkml/[email protected]/

Between version 2 and version 3:
- Add Conor's Acked-by to patch 1
- Add Rob's Reviewed-by to patch 1
- Added imply_PDMA to the SPI_SPACEMIT_K1 Kconfig option
- Fixed a bug pointed out by Vivian (and Troy) in word-sized reads
- Added a comment stating we use 1, 2, or 4 bytes per word
- Cleaned up DMA channels properly in case of failure setting up
- No longer use devm_*() for allocating DMA channels or buffer
- Moved the SPI controller into the dma-bus memory region

Here is version 2 of this series:
https://lore.kernel.org/lkml/[email protected]/

Between version 1 and version 2:
- Use enum rather than const for the binding compatible string
- Omit the label and status property in the binding example
- The spi-spacemit-k1.o make target is now added in sorted order
- The SPI_SPACEMIT_K1 config option is added in sorted order
- The SPI_SPACEMIT_K1 config does *not* depend on MMP_PDMA, however MMP_PDMA is checked at runtime, and if not enabled, DMA will not be used
- Read/modify/writes of registers no longer use an additional "virt" variable to hold the address accessed
- The k1_spi_driver_data->ioaddr field has been renamed base
- The DMA address for the base address is maintained, rather than saving the DMA address of the data register
- The spi-max-frequency property value is now bounds checked
- A local variable is now initialized to 0 in k1_spi_write_word()
- The driver name is now "k1-spi"
- DT aliases are used rather than spacemit,k1-ssp-id for bus number
- The order of two pin control properties was changed as requested
- Clock names and DMA names are now on one line in the "k1.dtsi"
- The interrupts property is used rather than interrupts-extended
- The order of two pin control properties was changed as requested
- Clock names and DMA names are now on one line in the "k1.dtsi"
- The interrupts property is used rather than interrupts-extended

Here is version 1 of this series:
https://lore.kernel.org/lkml/[email protected]/

Alex Elder (3):
dt-bindings: spi: add SpacemiT K1 SPI support
spi: spacemit: introduce SpacemiT K1 SPI controller driver
riscv: dts: spacemit: define a SPI controller node

 .../bindings/spi/spacemit,k1-spi.yaml         |  84 ++
 .../boot/dts/spacemit/k1-bananapi-f3.dts      |   7 +
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  |  20 +
 arch/riscv/boot/dts/spacemit/k1.dtsi          |  15 +
 drivers/spi/Kconfig                           |   9 +
 drivers/spi/Makefile                          |   1 +
 drivers/spi/spi-spacemit-k1.c                 | 966 ++++++++++++++++++
 7 files changed, 1102 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml
 create mode 100644 drivers/spi/spi-spacemit-k1.c

base-commit: 0f2995693867bfb26197b117cd55624ddc57582f

Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v7 1/3 dt-bindings: spi: add SpacemiT K1 SPI support

Add support for the SPI controller implemented by the SpacemiT K1 SoC.

---
 .../bindings/spi/spacemit,k1-spi.yaml         | 84 +++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml
Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml new file +84 -0
new file mode 100644
index 0000000000000..e82c7f8d0b981
@@ -0,0 +1,84 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/spi/spacemit,k1-spi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 SoC Serial Peripheral Interface (SPI)
+
+maintainers:
+  - Alex Elder <[email protected]>
+
+description:
+  The SpacemiT K1 SoC implements a SPI controller that has two 32-entry
+  FIFOs, for transmit and receive.  Details are currently available in
+  section 18.2.1 of the K1 User Manual, found in the SpacemiT Keystone
+  K1 Documentation[1].  The controller transfers words using PIO.  DMA
+  transfers are supported as well, if both TX and RX DMA channels are
+  specified,
+
+  [1] https://developer.spacemit.com/documentation
+
+allOf:
+  - $ref: /schemas/spi/spi-controller.yaml#
+
+properties:
+  compatible:
+    const: spacemit,k1-spi
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: Core clock
+      - description: Bus clock
+
+  clock-names:
+    items:
+      - const: core
+      - const: bus
+
+  resets:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  dmas:
+    items:
+      - description: RX DMA channel
+      - description: TX DMA channel
+
+  dma-names:
+    items:
+      - const: rx
+      - const: tx
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+  - interrupts
+
+unevaluatedProperties: false
+
+examples:
+  - |
+
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+    spi@d401c000 {
+        compatible = "spacemit,k1-spi";
+        reg = <0xd401c000 0x30>;
+        #address-cells = <1>;
+        #size-cells = <0>;
+        clocks = <&syscon_apbc CLK_SSP3>,
+                 <&syscon_apbc CLK_SSP3_BUS>;
+        clock-names = "core", "bus";
+        resets = <&syscon_apbc RESET_SSP3>;
+        interrupts = <55>;
+        dmas = <&pdma 20>, <&pdma 19>;
+        dma-names = "rx", "tx";
+    };
Signature · 1 line
2.48.1
author
#2 lore raw

PATCH v7 2/3 spi: spacemit: introduce SpacemiT K1 SPI controller driver

In reply to Alex Elder

This patch introduces the driver for the SPI controller found in the SpacemiT K1 SoC. Currently the driver supports master mode only. The SPI hardware implements RX and TX FIFOs, 32 entries each, and supports both PIO and DMA mode transfers.

---
v7: - The driver is built as a kernel module by default
- A bug interpreting the resource pointer has been fixed when cleaning up the DMA state
- A error that was reported when COMPILE_TEST built two 32-bit architectures has been fixed by using DIV_ROUND_UP_ULL() rather than DIV_ROUND_UP() in k1_spi_set_speed()

 drivers/spi/Kconfig           |   9 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/spi-spacemit-k1.c | 966 ++++++++++++++++++++++++++++++++++
 3 files changed, 976 insertions(+)
 create mode 100644 drivers/spi/spi-spacemit-k1.c
drivers/spi/Kconfig +9 -0
index 592d46c9998bb..7bba7095c64ff 100644
@@ -1072,6 +1072,15 @@ config SPI_SG2044_NOR
 	  also supporting 3Byte address devices and 4Byte address
 	  devices.
 
+config SPI_SPACEMIT_K1
+	tristate "K1 SPI Controller"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on OF
+	imply MMP_PDMA if ARCH_SPACEMIT
+	default m if ARCH_SPACEMIT
+	help
+	  Enable support for the SpacemiT K1 SPI controller.
+
 config SPI_SPRD
 	tristate "Spreadtrum SPI controller"
 	depends on ARCH_SPRD || COMPILE_TEST
drivers/spi/Makefile +1 -0
index 8ff74a13faaa8..c17e7d62dbe16 100644
@@ -140,6 +140,7 @@ obj-$(CONFIG_SPI_SIFIVE)		+= spi-sifive.o
 obj-$(CONFIG_SPI_SLAVE_MT27XX)          += spi-slave-mt27xx.o
 obj-$(CONFIG_SPI_SN_F_OSPI)		+= spi-sn-f-ospi.o
 obj-$(CONFIG_SPI_SG2044_NOR)	+= spi-sg2044-nor.o
+obj-$(CONFIG_SPI_SPACEMIT_K1)		+= spi-spacemit-k1.o
 obj-$(CONFIG_SPI_SPRD)			+= spi-sprd.o
 obj-$(CONFIG_SPI_SPRD_ADI)		+= spi-sprd-adi.o
 obj-$(CONFIG_SPI_STM32) 		+= spi-stm32.o
drivers/spi/spi-spacemit-k1.c new file +966 -0

966 lines not on this page · raw message

Signature · 1 line
2.48.1
maintainer
#3 lore raw

Re: [PATCH v7 2/3] spi: spacemit: introduce SpacemiT K1 SPI controller driver

Quoted 4 lines · Alex Elder

This patch introduces the driver for the SPI controller found in the SpacemiT K1 SoC. Currently the driver supports master mode only. The SPI hardware implements RX and TX FIFOs, 32 entries each, and supports both PIO and DMA mode transfers.

This looks mostly good but there's a bit of open coding that looks like the driver could make more use of the core.

drivers/spi/spi-spacemit-k1.c line 7

Quoted 8 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
@@ -0,0 +1,966 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 SPI controller driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ * Copyright (c) 2023, spacemit Corporation.
+ */

Please make the entire comment a C++ one so things look more intentional.

drivers/spi/spi-spacemit-k1.c line 133

Quoted 8 lines
drivers/spi/spi-spacemit-k1.c new file +8 -0
+static bool k1_spi_map_dma_buffer(struct k1_spi_io *io, size_t len, void *dummy)
+{
+	struct device *dmadev = io->chan->device->dev;
+	unsigned int nents = DIV_ROUND_UP(len, SZ_2K);
+	struct sg_table *sgt = &io->sgt;
+	void *bufp = io->buf ? : dummy;
+	struct scatterlist *sg;
+	unsigned int i;

The SPI core can do DMA mapping for you, the only thing this is doing that's unusual is that it's imposing a fixed 2K limit on block sizes. If this limit comes from the DMA controller (which looks to be the case since we feed the entire table into the DMA controller at once?) the core will already DTRT here, assuming the DMA controller correctly advertises this restriction.

Quoted 2 lines
(quoted diff) +2 -0
+static bool k1_spi_map_dma_buffers(struct k1_spi_driver_data *drv_data)
+{

...

drivers/spi/spi-spacemit-k1.c line 178

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+	/* Don't bother with DMA if we can't do even a single burst */
+	if (drv_data->len < dma_burst_size)
+		return false;
+
+	/* We won't use DMA if the transfer is too big, either */
+	if (drv_data->len > K1_SPI_MAX_DMA_LEN)
+		return false;

The core has a can_dma() callback for this.

Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +3 -0
+static int k1_spi_transfer_one_message(struct spi_controller *host,
+					   struct spi_message *message)
+{

...

drivers/spi/spi-spacemit-k1.c line 582

Quoted 4 lines
drivers/spi/spi-spacemit-k1.c new file +4 -0
+	/* Hold frame low to avoid losing transferred data */
+	val = readl(drv_data->base + SSP_TOP_CTRL);
+	val |= TOP_HOLD_FRAME_LOW;
+	writel(val, drv_data->base + SSP_TOP_CTRL);

This looks like it should be a set_cs() operation?

drivers/spi/spi-spacemit-k1.c line 595

Quoted 13 lines
drivers/spi/spi-spacemit-k1.c new file +13 -0
+
+	list_for_each_entry(transfer, &message->transfers, transfer_list) {
+		reinit_completion(completion);
+
+		/* Issue the next transfer */
+		if (!k1_spi_transfer_start(drv_data, transfer)) {
+			message->status = -EIO;
+			break;
+		}
+
+		k1_spi_transfer_wait(drv_data);
+
+		k1_spi_transfer_end(drv_data, transfer);

Why not just implement the transfer_one() callback? This just looks like it's duplicating code.

drivers/spi/spi-spacemit-k1.c line 840

Quoted 6 lines
drivers/spi/spi-spacemit-k1.c new file +5 -0
+static irqreturn_t k1_spi_ssp_isr(int irq, void *dev_id)
+{
 
+	/* Get status and clear pending interrupts */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);

This unconditionally acknowledges all interrupts even if we didn't handle anything...

author
#4 lore raw

Re: [PATCH v7 2/3] spi: spacemit: introduce SpacemiT K1 SPI controller driver

Quoted 6 lines · Mark Brown
Quoted 4 lines · Alex Elder

This patch introduces the driver for the SPI controller found in the SpacemiT K1 SoC. Currently the driver supports master mode only. The SPI hardware implements RX and TX FIFOs, 32 entries each, and supports both PIO and DMA mode transfers.

Sorry for the delay responding to this. You provided some really great feedback and I appreciate it.

Quoted 14 lines

This looks mostly good but there's a bit of open coding that looks like the driver could make more use of the core.

drivers/spi/spi-spacemit-k1.c line 7

Quoted 8 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
@@ -0,0 +1,966 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SpacemiT K1 SPI controller driver
+ *
+ * Copyright (C) 2025 by RISCstar Solutions Corporation.  All rights reserved.
+ * Copyright (c) 2023, spacemit Corporation.
+ */

Please make the entire comment a C++ one so things look more intentional.

I do see in drivers/spi/ files that start just like above, and others that start with all "//" comments (more of the former than the latter). I really don't prefer using "//" for anything but the SPDX ID, but... since you requested it I will make that change.

Quoted 11 lines

drivers/spi/spi-spacemit-k1.c line 133

Quoted 8 lines
drivers/spi/spi-spacemit-k1.c new file +8 -0
+static bool k1_spi_map_dma_buffer(struct k1_spi_io *io, size_t len, void *dummy)
+{
+	struct device *dmadev = io->chan->device->dev;
+	unsigned int nents = DIV_ROUND_UP(len, SZ_2K);
+	struct sg_table *sgt = &io->sgt;
+	void *bufp = io->buf ? : dummy;
+	struct scatterlist *sg;
+	unsigned int i;

The SPI core can do DMA mapping for you, the only thing this is doing that's unusual is that it's imposing a fixed 2K limit on block sizes.

OK. I will use the DMA support provided by the core.

Quoted 4 lines

If this limit comes from the DMA controller (which looks to be the case since we feed the entire table into the DMA controller at once?) the core will already DTRT here, assuming the DMA controller correctly advertises this restriction.

I will verify this.

Quoted 21 lines
Quoted 2 lines
(quoted diff) +2 -0
+static bool k1_spi_map_dma_buffers(struct k1_spi_driver_data *drv_data)
+{

...

drivers/spi/spi-spacemit-k1.c line 178

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+	/* Don't bother with DMA if we can't do even a single burst */
+	if (drv_data->len < dma_burst_size)
+		return false;
+
+	/* We won't use DMA if the transfer is too big, either */
+	if (drv_data->len > K1_SPI_MAX_DMA_LEN)
+		return false;

The core has a can_dma() callback for this.

Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +3 -0
+static int k1_spi_transfer_one_message(struct spi_controller *host,
+					   struct spi_message *message)
+{

...

Quoted 6 lines

drivers/spi/spi-spacemit-k1.c line 582

Quoted 4 lines
drivers/spi/spi-spacemit-k1.c new file +4 -0
+	/* Hold frame low to avoid losing transferred data */
+	val = readl(drv_data->base + SSP_TOP_CTRL);
+	val |= TOP_HOLD_FRAME_LOW;
+	writel(val, drv_data->base + SSP_TOP_CTRL);

This looks like it should be a set_cs() operation?

I'll implement that, along with using spi_transfer_one_message().

Quoted 7 lines
Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+	list_for_each_entry(transfer, &message->transfers, transfer_list) {
+		reinit_completion(completion);
+
+		/* Issue the next transfer */
+		if (!k1_spi_transfer_start(drv_data, transfer)) {
+			message->status = -EIO;
+			break;
Quoted 8 lines

drivers/spi/spi-spacemit-k1.c line 595

Quoted 5 lines
drivers/spi/spi-spacemit-k1.c new file +5 -0
+		}
+
+		k1_spi_transfer_wait(drv_data);
+
+		k1_spi_transfer_end(drv_data, transfer);

Why not just implement the transfer_one() callback? This just looks like it's duplicating code.

I'm working on this now, and it's the reason for the delay. Methodically switching things over to the generic interface has been taking some time, but I think I'm close now.

Quoted 9 lines

drivers/spi/spi-spacemit-k1.c line 840

Quoted 6 lines
drivers/spi/spi-spacemit-k1.c new file +5 -0
+static irqreturn_t k1_spi_ssp_isr(int irq, void *dev_id)
+{
 
+	/* Get status and clear pending interrupts */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);

This unconditionally acknowledges all interrupts even if we didn't handle anything...This is a good observation.

There are only 6 interrupt conditions that get cleared. Three are errors, and the other three are read/write FIFO "ready" interrupts. The code that follows handles all of those, so doing this right away was a sort of shorthand.

That said, there was a chance for an early return (if the message pointer was null), and that should be checked before we clear the status register.

In any case, my work the last day or so has included a lot of tweaks to the handler. I'll try to make it clear what's done makes sense.

Thank you very much for the review. I wish I had looked harder at just using spi_transfer_one_message() before. But I *love* suggestions that will make the code become smaller and simpler.

-Alex

author
#5 lore raw

PATCH v7 3/3 riscv: dts: spacemit: define a SPI controller node

In reply to Alex Elder

Define a node for the fourth SoC SPI controller (number 3) on the SpacemiT K1 SoC.

Enable it on the Banana Pi BPI-F3 board, which exposes this feature via its GPIO block:

  GPIO PIN 19:  MOSI
  GPIO PIN 21:  MISO
  GPIO PIN 23:  SCLK
  GPIO PIN 24:  SS (inverted)

Define pincontrol configurations for the pins as used on that board.

(This was tested using a GigaDevice GD25Q64E SPI NOR chip.)

---
 .../boot/dts/spacemit/k1-bananapi-f3.dts      |  7 +++++++
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  | 20 +++++++++++++++++++
 arch/riscv/boot/dts/spacemit/k1.dtsi          | 15 ++++++++++++++
 3 files changed, 42 insertions(+)
arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts +7 -0
index 02f218a16318e..bdbfd04f21853 100644
@@ -16,6 +16,7 @@ aliases {
 		serial0 = &uart0;
 		i2c2 = &i2c2;
 		i2c8 = &i2c8;
+		spi3 = &spi3;
 	};
 
 	chosen {
@@ -264,6 +265,12 @@ dldo7 {
 	};
 };
 
+&spi3 {
+	pinctrl-0 = <&ssp3_0_cfg>;
+	pinctrl-names = "default";
+	status = "okay";
+};
+
 &uart0 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&uart0_2_cfg>;
arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi +20 -0
index e922e05ff856d..9b4f4949412f3 100644
@@ -537,4 +537,24 @@ pwm14-1-pins {
 			drive-strength = <32>;
 		};
 	};
+
+	ssp3_0_cfg: ssp3-0-cfg {
+		ssp3-0-pins {
+			pinmux = <K1_PADCONF(75, 2)>,	/* SCLK */
+				 <K1_PADCONF(77, 2)>,	/* MOSI  */
+				 <K1_PADCONF(78, 2)>;	/* MISO */
+
+			bias-disable;
+			drive-strength = <19>;
+			power-source = <3300>;
+		};
+
+		ssp3-0-frm-pins {
+			pinmux = <K1_PADCONF(76, 2)>;	/* FRM (frame) */
+
+			bias-pull-up = <0>;
+			drive-strength = <19>;
+			power-source = <3300>;
+		};
+	};
 };
arch/riscv/boot/dts/spacemit/k1.dtsi +15 -0
index 7818ca4979b6a..cb4016886861e 100644
@@ -919,6 +919,21 @@ qspi: spi@d420c000 {
 				status = "disabled";
 			};
 
+			spi3: spi@d401c000 {
+				compatible = "spacemit,k1-spi";
+				reg = <0x0 0xd401c000 0x0 0x30>;
+				#address-cells = <1>;
+				#size-cells = <0>;
+				clocks = <&syscon_apbc CLK_SSP3>,
+					 <&syscon_apbc CLK_SSP3_BUS>;
+				clock-names = "core", "bus";
+				resets = <&syscon_apbc RESET_SSP3>;
+				interrupts = <55>;
+				dmas = <&pdma 20>, <&pdma 19>;
+				dma-names = "rx", "tx";
+				status = "disabled";
+			};
+
 			/* sec_uart1: 0xf0612000, not available from Linux */
 		};
 
Signature · 1 line
2.48.1