Skip to contentSkip to conversation

spi: support the SpacemiT K1 SPI controller

PATCH Superseded Alex Elder 5 participants 15 messages Sep 19, 2025 – Sep 22, 2025

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

v2 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 2 incorporates changes suggested during review of v1.

-Alex

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

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         |  87 ++
 .../boot/dts/spacemit/k1-bananapi-f3.dts      |   7 +
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  |  20 +
 arch/riscv/boot/dts/spacemit/k1.dtsi          |  16 +
 drivers/spi/Kconfig                           |   8 +
 drivers/spi/Makefile                          |   1 +
 drivers/spi/spi-spacemit-k1.c                 | 968 ++++++++++++++++++
 7 files changed, 1107 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml
 create mode 100644 drivers/spi/spi-spacemit-k1.c

base-commit: 846bd2225ec3cfa8be046655e02b9457ed41973e

Signature · 1 line
2.48.1
author
#1 lore raw

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

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

---
v2: - Use enum rather than const for the binding compatible string
- Omit the label and status property in the binding example
- No more spacemit,k1-ssp-id property
- Interrupts no longer specified with interrupts-extended

 .../bindings/spi/spacemit,k1-spi.yaml         | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml
Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml new file +87 -0
new file mode 100644
index 0000000000000..a6978212ff570
@@ -0,0 +1,87 @@
+# 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
maintainer Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] dt-bindings: spi: add SpacemiT K1 SPI support

In reply to Alex Elder
Quoted 9 lines · Alex Elder

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

---
v2: - Use enum rather than const for the binding compatible string
- Omit the label and status property in the binding example
- No more spacemit,k1-ssp-id property
- Interrupts no longer specified with interrupts-extended

Quoted 4 lines
 .../bindings/spi/spacemit,k1-spi.yaml         | 87 +++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/spacemit,k1-spi.yaml
author
#4 lore raw

PATCH v2 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.

---
v2: - The SPI_SPACEMIT_K1 config option is added in sorted order
- The spi-spacemit-k1.o make target is now added in sorted order
- 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
- CONFIG_MMP_PDMA is checked at runtime, and if it is not enabled, DMA will not be used
- 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"

 drivers/spi/Kconfig           |   8 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/spi-spacemit-k1.c | 968 ++++++++++++++++++++++++++++++++++
 3 files changed, 977 insertions(+)
 create mode 100644 drivers/spi/spi-spacemit-k1.c
drivers/spi/Kconfig +8 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ 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
+	default 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 eefaeca097456..637d750ead996 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 +968 -0

968 lines not on this page · raw message

Signature · 1 line
2.48.1
#5 lore raw

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

Hi Alex,

Quoted 8 lines · Alex Elder

[...]

drivers/spi/Kconfig +0 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ config SPI_SG2044_NOR
 	  also supporting 3Byte address devices and 4Byte address

drivers/spi/Kconfig config SPI_SG2044_NOR · line 1081

Quoted 10 lines
drivers/spi/Kconfig +8 -0
 	  devices.
 
+config SPI_SPACEMIT_K1
+	tristate "K1 SPI Controller"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on OF
+	default ARCH_SPACEMIT
+	help
+	  Enable support for the SpacemiT K1 SPI controller.
+

We could still add:

imply MMP_PDMA if ARCH_SPACEMIT

To add a "recommended" dependency. This way, enabling SPI_SPACEMIT_K1 automatically enables MMP_PDMA, but if the user is willing to fiddle around, they can explicitly disable it. What do you think?

Quoted 20 lines
drivers/spi/Kconfig +0 -0
 config SPI_SPRD
 	tristate "Spreadtrum SPI controller"
 	depends on ARCH_SPRD || COMPILE_TEST
 
[...]
drivers/spi/spi-spacemit-k1.c new file +5 -0
new file mode 100644
index 0000000000000..8d564fe6c4303
@@ -0,0 +1,968 @@
 
[...]
 
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *rx = &drv_data->rx;
+	u32 bytes = drv_data->bytes;
+	u32 val;

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+
+	val = readl(drv_data->base + SSP_DATAR);
+	rx->resid -= bytes;
+
+	if (!rx->buf)
+		return;	/* Null reader: discard the data */
+
+	if (bytes == 1)
+		*(u8 *)rx->buf = val;
+	else if (bytes == 1)

Typo? else if (bytes == 2)

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

Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +3 -0
+		*(u16 *)rx->buf = val;
+	else
+		*(u32 *)rx->buf = val;

Maybe

	else if (bytes == 4)
		*(u32 *)rx->buf = val;
	else
		WARN_ON_ONCE(1);

Just to make the pattern consistent? Same for k1_spi_write_word.

Quoted 15 lines
drivers/spi/spi-spacemit-k1.c new file +12 -0
+
+	rx->buf += bytes;
+}
 
[...]
 
+static int k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+	struct device *dev = drv_data->dev;
+	int rx_ret;
+	int tx_ret;
+
+	/* We must get both DMA channels, or neither of them */
+	rx_ret = k1_spi_dma_setup_io(drv_data, true);
+	if (rx_ret == -EPROBE_DEFER)

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+		return -EPROBE_DEFER;
+
+	tx_ret = k1_spi_dma_setup_io(drv_data, false);
+
+	/* If neither is specified, we don't use DMA */
+	if (rx_ret == -ENODEV && tx_ret == -ENODEV)
+		return 0;
+
+	if (rx_ret || tx_ret)
+		goto err_cleanup;

This seems a bit convoluted. I'm wondering if all this explicit handling really is necessary - if we get an error at probe time, can we just return that error and let devres handle the rest? With the special case that if we get both -ENODEV then disable DMA.

k1_spi_dma_cleanup_io seems to handle unmapping and termination of DMA, which we... don't need, right?

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

Quoted 6 lines
drivers/spi/spi-spacemit-k1.c new file +6 -0
+
+	drv_data->dummy = devm_kzalloc(dev, SZ_2K, GFP_KERNEL);
+	if (drv_data->dummy)
+		return 0;		/* Success! */
+
+	dev_warn(dev, "error allocating DMA dummy buffer; DMA disabled\n");

Just return -ENOMEM. If we can't even allocate 2K of buffer, we're doomed anyway.

Quoted 23 lines
drivers/spi/spi-spacemit-k1.c new file +20 -0
+err_cleanup:
+	if (tx_ret) {
+		if (tx_ret != -EPROBE_DEFER)
+			dev_err(dev, "error requesting DMA TX DMA channel\n");
+	} else {
+		k1_spi_dma_cleanup_io(drv_data, false);
+	}
+
+	if (rx_ret)
+		dev_err(dev, "error requesting DMA RX DMA channel\n");
+	else
+		k1_spi_dma_cleanup_io(drv_data, true);
+
+	if (tx_ret == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
+
+	/* Return success if we don't get the dummy buffer; PIO will be used */
+
+	return rx_ret ? : tx_ret ? : 0;
+}
 
[...]
 

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+static int devm_k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_driver_data **ptr;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_MMP_PDMA)) {
+		dev_warn(drv_data->dev, "DMA not available; using PIO\n");
+		return 0;
+	}
+

Shouldn't be necessary if we do the "imply" thing in Kconfig.

Quoted 11 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +6 -0
+static void k1_spi_host_init(struct k1_spi_driver_data *drv_data)
+{
 
[...]
 
+
+	ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
+	if (!ret) {
+		host->max_speed_hz = clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ,

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+					   K1_SPI_MAX_SPEED_HZ);
+		if (host->max_speed_hz != max_speed_hz)
+			dev_warn(dev, "spi-max-frequency %u out of range, using %u\n",
+				max_speed_hz, host->max_speed_hz);
+	} else {
+		if (ret != -EINVAL)
+			dev_warn(dev, "bad spi-max-frequency, using %u\n",
+				 K1_SPI_DEFAULT_MAX_SPEED_HZ);
+		host->max_speed_hz = K1_SPI_DEFAULT_MAX_SPEED_HZ;
+	}

I think it makes sense to have spi-max-frequency default to K1_SPI_DEFAULT_MAX_SPEED_HZ, but if the value is out of range just print a message and return an error, to get whoever wrote the bad value to fix it.

This range seems to be fixed by hardware, so, it should also be specified in the bindings.

Quoted 17 lines
drivers/spi/spi-spacemit-k1.c new file +14 -0
+}
+
 
[...]
 
+
+static int k1_spi_probe(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data;
+	struct device *dev = &pdev->dev;
+	struct reset_control *reset;
+	struct spi_controller *host;
+	struct resource *iores;
+	struct clk *clk_bus;
+	int ret;
+
+	host = devm_spi_alloc_host(dev, sizeof(*drv_data));

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+	if (!host)
+		return -ENOMEM;
+	drv_data = spi_controller_get_devdata(host);
+	drv_data->controller = host;
+	platform_set_drvdata(pdev, drv_data);
+	drv_data->dev = dev;
+	init_completion(&drv_data->completion);
+
+	drv_data->base = devm_platform_get_and_ioremap_resource(pdev, 0,
+								&iores);

Maybe

    devm_platform_ioremap_resource(pdev, 0);

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

Quoted 5 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +3 -0
+
+MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver");
+MODULE_LICENSE("GPL");

Maybe MODULE_AUTHOR()?

Vivian "dramforever" Wang

author
#6 lore raw

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

Quoted 24 lines · Vivian Wang

Hi Alex,

Quoted 8 lines · Alex Elder

[...]

drivers/spi/Kconfig +0 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ config SPI_SG2044_NOR
  	  also supporting 3Byte address devices and 4Byte address

drivers/spi/Kconfig config SPI_SG2044_NOR · line 1081

Quoted 10 lines
drivers/spi/Kconfig +8 -0
  	  devices.
 
+config SPI_SPACEMIT_K1
+	tristate "K1 SPI Controller"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on OF
+	default ARCH_SPACEMIT
+	help
+	  Enable support for the SpacemiT K1 SPI controller.
+

We could still add:

Quoted 1 line

imply MMP_PDMA if ARCH_SPACEMIT

I have never used "imply", I guess it's new (as of 2016)...

This sounds like a good suggestion. This would mean MMP_PDMA would by default take the same value as SPI_SPACEMIT_K1 (if the former's dependencies were met), while allowing it to be deselected for the configuration.

Quoted 3 lines

To add a "recommended" dependency. This way, enabling SPI_SPACEMIT_K1 automatically enables MMP_PDMA, but if the user is willing to fiddle around, they can explicitly disable it. What do you think?

I like it.

Quoted 24 lines
Quoted 24 lines
drivers/spi/Kconfig +0 -0
  config SPI_SPRD
  	tristate "Spreadtrum SPI controller"
  	depends on ARCH_SPRD || COMPILE_TEST
 
[...]
drivers/spi/spi-spacemit-k1.c new file +9 -0
new file mode 100644
index 0000000000000..8d564fe6c4303
@@ -0,0 +1,968 @@
 
[...]
 
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *rx = &drv_data->rx;
+	u32 bytes = drv_data->bytes;
+	u32 val;
+
+	val = readl(drv_data->base + SSP_DATAR);
+	rx->resid -= bytes;
+
Quoted 8 lines

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

Quoted 6 lines
drivers/spi/spi-spacemit-k1.c new file +6 -0
+	if (!rx->buf)
+		return;	/* Null reader: discard the data */
+
+	if (bytes == 1)
+		*(u8 *)rx->buf = val;
+	else if (bytes == 1)

Typo? else if (bytes == 2)

Wow. Yes that is an error that I'll correct.

Quoted 10 lines

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

Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +3 -0
+		*(u16 *)rx->buf = val;
+	else
+		*(u32 *)rx->buf = val;

Maybe

	else if (bytes == 4)
		*(u32 *)rx->buf = val;
	else
		WARN_ON_ONCE(1);

The value of bytes will be 1, 2, or 4, which we can tell by inspection. At one time I had a switch statement with a default, but I decided to leave out the default, which won't happen.

Quoted 1 line

Just to make the pattern consistent? Same for k1_spi_write_word.

Consistent with what?

Quoted 21 lines
Quoted 21 lines
drivers/spi/spi-spacemit-k1.c new file +18 -0
+	rx->buf += bytes;
+}
 
[...]
 
+static int k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+	struct device *dev = drv_data->dev;
+	int rx_ret;
+	int tx_ret;
+
+	/* We must get both DMA channels, or neither of them */
+	rx_ret = k1_spi_dma_setup_io(drv_data, true);
+	if (rx_ret == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
+
+	tx_ret = k1_spi_dma_setup_io(drv_data, false);
+
+	/* If neither is specified, we don't use DMA */
+	if (rx_ret == -ENODEV && tx_ret == -ENODEV)
+		return 0;
Quoted 8 lines

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

Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +3 -0
+
+	if (rx_ret || tx_ret)
+		goto err_cleanup;

This seems a bit convoluted. I'm wondering if all this explicit handling really is necessary - if we get an error at probe time, can we just return that error and let devres handle the rest? With the special case that if we get both -ENODEV then disable DMA.

I agree it seems it should be less complex.

I'm trying to ensure that both channels are set up, or that neither channel is set up, or that we try again if we get -EPROBE_DEFER. And if there's something wrong with the configuration (only one of TX and RX is set up successfully), an error occurs.

RX TX Result
-- -- ------
0 0 0 (DMA)
-ENODEV -ENODEV 0 (PIO)
-EPROBE_DEFER (anything) -EPROBE_DEFER (try again)
(anything) -EPROBE_DEFER -EPROBE_DEFER (try again)
0 -ENODEV -ENODEV (error, abort probe)
-ENODEV 0 -ENODEV (error, abort probe)
error (anything) error (error, abort probe)
(anything) error error (error, abort probe)

Finally, if the buffer allocation fails:
0 0 0 (PIO; clean up TX and RX)

Let me think about this. I'll see if I can find a simpler way to achieve the above result, relying on devres to clean things up. I'd have to change k1_spi_dma_cleanup(), but you might be right that it could be done more simply.

Quoted 12 lines

k1_spi_dma_cleanup_io seems to handle unmapping and termination of DMA, which we... don't need, right?

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

Quoted 6 lines
drivers/spi/spi-spacemit-k1.c new file +6 -0
+
+	drv_data->dummy = devm_kzalloc(dev, SZ_2K, GFP_KERNEL);
+	if (drv_data->dummy)
+		return 0;		/* Success! */
+
+	dev_warn(dev, "error allocating DMA dummy buffer; DMA disabled\n");

Just return -ENOMEM. If we can't even allocate 2K of buffer, we're doomed anyway.

You're generally right, but I don't want my code to assume that.

Quoted 23 lines
Quoted 23 lines
drivers/spi/spi-spacemit-k1.c new file +20 -0
+err_cleanup:
+	if (tx_ret) {
+		if (tx_ret != -EPROBE_DEFER)
+			dev_err(dev, "error requesting DMA TX DMA channel\n");
+	} else {
+		k1_spi_dma_cleanup_io(drv_data, false);
+	}
+
+	if (rx_ret)
+		dev_err(dev, "error requesting DMA RX DMA channel\n");
+	else
+		k1_spi_dma_cleanup_io(drv_data, true);
+
+	if (tx_ret == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
+
+	/* Return success if we don't get the dummy buffer; PIO will be used */
+
+	return rx_ret ? : tx_ret ? : 0;
+}
 
[...]
Quoted 12 lines

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+static int devm_k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_driver_data **ptr;
+	int ret;
+
+	if (!IS_ENABLED(CONFIG_MMP_PDMA)) {
+		dev_warn(drv_data->dev, "DMA not available; using PIO\n");
+		return 0;
+	}
+

Shouldn't be necessary if we do the "imply" thing in Kconfig.

The messages I provide are based on an assumption that using DMA is desirable and it will normally be used by this driver. So if it won't be used, I'd like to provide that information.

On the other hand, I don't issue a warning if neither of the channels is configured in the DTB.

I'm not going to commit either way on keeping/removing this. If someone else weighs in I'm open to changing it.

Quoted 17 lines
Quoted 17 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +12 -0
+static void k1_spi_host_init(struct k1_spi_driver_data *drv_data)
+{
 
[...]
 
+
+	ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
+	if (!ret) {
+		host->max_speed_hz = clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ,
+					   K1_SPI_MAX_SPEED_HZ);
+		if (host->max_speed_hz != max_speed_hz)
+			dev_warn(dev, "spi-max-frequency %u out of range, using %u\n",
+				max_speed_hz, host->max_speed_hz);
+	} else {
+		if (ret != -EINVAL)
Quoted 8 lines

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

Quoted 4 lines
drivers/spi/spi-spacemit-k1.c new file +4 -0
+			dev_warn(dev, "bad spi-max-frequency, using %u\n",
+				 K1_SPI_DEFAULT_MAX_SPEED_HZ);
+		host->max_speed_hz = K1_SPI_DEFAULT_MAX_SPEED_HZ;
+	}

I think it makes sense to have spi-max-frequency default to K1_SPI_DEFAULT_MAX_SPEED_HZ, but if the value is out of range just print a message and return an error, to get whoever wrote the bad value to fix it.

These errors just shouldn't happen. But the way I handle this, it allows the SPI controller to still be used, even if the administrator can't really update the DTB.

Quoted 2 lines

This range seems to be fixed by hardware, so, it should also be specified in the bindings.

I define the hardware limits here, and enforce them, in case the bindings specify an out-of-range value. Again, this is an error that just shouldn't occur in practice, but the code is defensive.

Most of your comments really made me think about how errors are handled. I appreciate it.

-Alex

Quoted 43 lines
Quoted 18 lines
drivers/spi/spi-spacemit-k1.c new file +14 -0
+}
+
 
[...]
 
+
+static int k1_spi_probe(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data;
+	struct device *dev = &pdev->dev;
+	struct reset_control *reset;
+	struct spi_controller *host;
+	struct resource *iores;
+	struct clk *clk_bus;
+	int ret;
+
+	host = devm_spi_alloc_host(dev, sizeof(*drv_data));

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+	if (!host)
+		return -ENOMEM;
+	drv_data = spi_controller_get_devdata(host);
+	drv_data->controller = host;
+	platform_set_drvdata(pdev, drv_data);
+	drv_data->dev = dev;
+	init_completion(&drv_data->completion);
+
+	drv_data->base = devm_platform_get_and_ioremap_resource(pdev, 0,
+								&iores);

Maybe

     devm_platform_ioremap_resource(pdev, 0);

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

Quoted 5 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +3 -0
+
+MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver");
+MODULE_LICENSE("GPL");

Maybe MODULE_AUTHOR()?

Vivian "dramforever" Wang

#7 lore raw

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

Quoted 35 lines · Alex Elder
Quoted 19 lines · Vivian Wang

[...]

drivers/spi/spi-spacemit-k1.c new file +15 -0
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+    struct k1_spi_io *rx = &drv_data->rx;
+    u32 bytes = drv_data->bytes;
+    u32 val;
+
+    val = readl(drv_data->base + SSP_DATAR);
+    rx->resid -= bytes;
+
+    if (!rx->buf)
+        return;    /* Null reader: discard the data */
+
+    if (bytes == 1)
+        *(u8 *)rx->buf = val;
+    else if (bytes == 1)
Quoted 1 line

Typo? else if (bytes == 2)

Wow.  Yes that is an error that I'll correct.

Quoted 10 lines
Quoted 3 lines

+        *(u16 *)rx->buf = val;
+    else
+        *(u32 *)rx->buf = val;

Maybe

    else if (bytes == 4)
        *(u32 *)rx->buf = val;
    else
        WARN_ON_ONCE(1);

Quoted 9 lines

The value of bytes will be 1, 2, or 4, which we can tell by inspection.  At one time I had a switch statement with a default, but I decided to leave out the default, which won't happen.

Quoted 1 line

Just to make the pattern consistent? Same for k1_spi_write_word.

Consistent with what?

I was just thinking it would be clearer if the code states clearly:

    1 -> u8
    2 -> u16
    4 -> u32
    anything else -> shouldn't happen

As is, it wasn't obvious to me that we're just handling 4 as u32. Maybe we're just capping it at u32, and 8 is also handled.

Well, maybe I'm just not familiar with SPI stuff, and word size above 4 doesn't make sense anyway.

It could also be a comment

    else /* 4 */

Just a suggestion, no strong preference from me.

Vivian "dramforever" Wang

author
#8 lore raw

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

Quoted 53 lines · Vivian Wang
Quoted 35 lines · Alex Elder
Quoted 19 lines · Vivian Wang

[...]

drivers/spi/spi-spacemit-k1.c new file +15 -0
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+    struct k1_spi_io *rx = &drv_data->rx;
+    u32 bytes = drv_data->bytes;
+    u32 val;
+
+    val = readl(drv_data->base + SSP_DATAR);
+    rx->resid -= bytes;
+
+    if (!rx->buf)
+        return;    /* Null reader: discard the data */
+
+    if (bytes == 1)
+        *(u8 *)rx->buf = val;
+    else if (bytes == 1)
Quoted 1 line

Typo? else if (bytes == 2)

Wow.  Yes that is an error that I'll correct.

Quoted 10 lines
Quoted 3 lines

+        *(u16 *)rx->buf = val;
+    else
+        *(u32 *)rx->buf = val;

Maybe

    else if (bytes == 4)
        *(u32 *)rx->buf = val;
    else
        WARN_ON_ONCE(1);

Quoted 9 lines

The value of bytes will be 1, 2, or 4, which we can tell by inspection.  At one time I had a switch statement with a default, but I decided to leave out the default, which won't happen.

Quoted 1 line

Just to make the pattern consistent? Same for k1_spi_write_word.

Consistent with what?

I was just thinking it would be clearer if the code states clearly:

     1 -> u8
     2 -> u16
     4 -> u32
     anything else -> shouldn't happen
Quoted 5 lines

As is, it wasn't obvious to me that we're just handling 4 as u32. Maybe we're just capping it at u32, and 8 is also handled.

Well, maybe I'm just not familiar with SPI stuff, and word size above 4 doesn't make sense anyway.

Understood. I only know it because I checked. And I do want my code to me understandable, so I'll add a comment as you suggest below.

Thank you.

-Alex

Quoted 9 lines

It could also be a comment

     else /* 4 */

Just a suggestion, no strong preference from me.

Vivian "dramforever" Wang

author
#9 lore raw

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

In reply to Alex Elder
Quoted 84 lines · Alex Elder
Quoted 23 lines · Vivian Wang

Hi Alex,

Quoted 9 lines · Alex Elder

[...]

drivers/spi/Kconfig +0 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ config SPI_SG2044_NOR

        also supporting 3Byte address devices and 4Byte address devices.

drivers/spi/Kconfig config SPI_SG2044_NOR · line 1081

Quoted 8 lines
drivers/spi/Kconfig +8 -0
+config SPI_SPACEMIT_K1
+    tristate "K1 SPI Controller"
+    depends on ARCH_SPACEMIT || COMPILE_TEST
+    depends on OF
+    default ARCH_SPACEMIT
+    help
+      Enable support for the SpacemiT K1 SPI controller.
+

We could still add:

Quoted 1 line

    imply MMP_PDMA if ARCH_SPACEMIT

I have never used "imply", I guess it's new (as of 2016)...

This sounds like a good suggestion.  This would mean MMP_PDMA would by default take the same value as SPI_SPACEMIT_K1 (if the former's dependencies were met), while allowing it to be deselected for the configuration.
 > To add a "recommended" dependency. This way, enabling SPI_SPACEMIT_K1

Quoted 2 lines

automatically enables MMP_PDMA, but if the user is willing to fiddle around, they can explicitly disable it. What do you think?

I like it.

Quoted 24 lines
Quoted 24 lines

  config SPI_SPRD
      tristate "Spreadtrum SPI controller"
      depends on ARCH_SPRD || COMPILE_TEST

[...]

drivers/spi/spi-spacemit-k1.c new file +9 -0
new file mode 100644
index 0000000000000..8d564fe6c4303
@@ -0,0 +1,968 @@
 
[...]
 
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+    struct k1_spi_io *rx = &drv_data->rx;
+    u32 bytes = drv_data->bytes;
+    u32 val;
+
+    val = readl(drv_data->base + SSP_DATAR);
+    rx->resid -= bytes;
+
Quoted 8 lines
Quoted 6 lines

+    if (!rx->buf)
+        return;    /* Null reader: discard the data */ +
+    if (bytes == 1)
+        *(u8 *)rx->buf = val;
+    else if (bytes == 1)

Typo? else if (bytes == 2)

Wow.  Yes that is an error that I'll correct.

Quoted 10 lines
Quoted 3 lines

+        *(u16 *)rx->buf = val;
+    else
+        *(u32 *)rx->buf = val;

Maybe

    else if (bytes == 4)
        *(u32 *)rx->buf = val;
    else
        WARN_ON_ONCE(1);

Quoted 8 lines

The value of bytes will be 1, 2, or 4, which we can tell by inspection.  At one time I had a switch statement with a default, but I decided to leave out the default, which won't happen.

Quoted 1 line

Just to make the pattern consistent? Same for k1_spi_write_word.

Consistent with what?

FYI in updating my code I now realize that k1_spi_write() and k1_spi_read() were done differently, and that's what you were talking about. (I assumed they both used the same pattern, not checking in the case bytes == 4.)

That was the basis of my "consistent with what?" question and I'm sorry I didn't realize I was mistaken about how this was written.

They're both going to look roughly like this now:

        if (bytes == 1)
                *(u8 *)rx->buf = val;
        else if (bytes == 2)
                *(u16 *)rx->buf = val;
        else    /* bytes == 4 */
                *(u32 *)rx->buf = val;
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines

+    rx->buf += bytes;

drivers/spi/spi-spacemit-k1.c new file +17 -0
+}
 
[...]
 
+static int k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+    struct device *dev = drv_data->dev;
+    int rx_ret;
+    int tx_ret;
+
+    /* We must get both DMA channels, or neither of them */
+    rx_ret = k1_spi_dma_setup_io(drv_data, true);
+    if (rx_ret == -EPROBE_DEFER)
+        return -EPROBE_DEFER;
+
+    tx_ret = k1_spi_dma_setup_io(drv_data, false);
+
+    /* If neither is specified, we don't use DMA */
+    if (rx_ret == -ENODEV && tx_ret == -ENODEV)
+        return 0;
Quoted 8 lines
Quoted 3 lines

+
+    if (rx_ret || tx_ret)
+        goto err_cleanup;

This seems a bit convoluted. I'm wondering if all this explicit handling really is necessary - if we get an error at probe time, can we just return that error and let devres handle the rest? With the special case that if we get both -ENODEV then disable DMA.

Quoted 1 line

I agree it seems it should be less complex.

Your suggestion is to simplify this code by falling back on the devres mechanism to take care of cleaning things up when an error is returned.

But that won't work, because this function is called by the devm_k1_spi_dma_setup() devres wrapper function.

If this function returns an error, that function assumes failure, so it never sets up the resource, and never calls the cleanup function. This function must restore everything to the previous state if it returns an error.

Quoted 17 lines

I'm trying to ensure that both channels are set up, or that neither channel is set up, or that we try again if we get -EPROBE_DEFER.  And if there's something wrong with the configuration (only one of TX and RX is set up successfully), an error occurs.

RX        TX        Result
--        --        ------
0        0        0    (DMA)

(quoted diff) +0 -2
-ENODEV        -ENODEV        0    (PIO)
-EPROBE_DEFER    (anything)    -EPROBE_DEFER (try again)

(anything)    -EPROBE_DEFER    -EPROBE_DEFER (try again) 0        -ENODEV        -ENODEV    (error, abort probe) -ENODEV        0        -ENODEV    (error, abort probe) error        (anything)    error    (error, abort probe) (anything)    error        error    (error, abort probe)

Quoted 7 lines

Finally, if the buffer allocation fails:
0        0        0    (PIO; clean up TX and RX)

Let me think about this.  I'll see if I can find a simpler way to achieve the above result, relying on devres to clean things up.  I'd have to change k1_spi_dma_cleanup(), but you might be right that it could be done more simply.

Version 3 of this series will keep this code similar to v2.

Quoted 3 lines
Quoted 3 lines

k1_spi_dma_cleanup_io seems to handle unmapping and termination of DMA, which we... don't need, right?

You're right. Those cleanup activities are not necessary, because they're meant to clean up stuff that happens *after* setup completes--once we perform at least one DMA.

HOWEVER, because devm_k1_spi_dma_setup() assumes this function returns everything to its previous state, the channels need to be freed explicitly on error (which did not occur in the v2 code).

And because devm_k1_spi_dma_setup() is already arranging for resource-managed cleanup, I'm just going to request the channel "directly" in k1_spi_dma_setup_io() rather than layering another devm_*() call there. Then I'll explicitly release the channel in k1_spi_dma_cleanup_io().

So k1_spi_dma_setup() will basically stay the same.

The same logic applies to allocating the dummy buffer; it will be done with kzalloc(), and will be freed in k1_spi_dma_cleanup().

This ended up needing to be fixed, even though it wasn't exactly what you were looking for. So either way, I'm glad you asked about this.

Thanks.

-Alex

Quoted 146 lines
Quoted 8 lines
Quoted 5 lines

+    drv_data->dummy = devm_kzalloc(dev, SZ_2K, GFP_KERNEL); +    if (drv_data->dummy)
+        return 0;        /* Success! */
+
+    dev_warn(dev, "error allocating DMA dummy buffer; DMA disabled\n");

Just return -ENOMEM. If we can't even allocate 2K of buffer, we're doomed anyway.

You're generally right, but I don't want my code to assume that.

Quoted 24 lines
Quoted 24 lines
drivers/spi/spi-spacemit-k1.c new file +17 -0
+err_cleanup:
+    if (tx_ret) {
+        if (tx_ret != -EPROBE_DEFER)
+            dev_err(dev, "error requesting DMA TX DMA channel\n");
+    } else {
+        k1_spi_dma_cleanup_io(drv_data, false);
+    }
+
+    if (rx_ret)
+        dev_err(dev, "error requesting DMA RX DMA channel\n");
+    else
+        k1_spi_dma_cleanup_io(drv_data, true);
+
+    if (tx_ret == -EPROBE_DEFER)
+        return -EPROBE_DEFER;
+
+    /* Return success if we don't get the dummy buffer; PIO will be 

used */

drivers/spi/spi-spacemit-k1.c new file +3 -0
+
+    return rx_ret ? : tx_ret ? : 0;
+}
 
[...]
Quoted 12 lines

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+static int devm_k1_spi_dma_setup(struct k1_spi_driver_data *drv_data)
+{
+    struct k1_spi_driver_data **ptr;
+    int ret;
+
+    if (!IS_ENABLED(CONFIG_MMP_PDMA)) {
+        dev_warn(drv_data->dev, "DMA not available; using PIO\n");
+        return 0;
+    }
+

Shouldn't be necessary if we do the "imply" thing in Kconfig.

The messages I provide are based on an assumption that using DMA is desirable and it will normally be used by this driver. So if it won't be used, I'd like to provide that information.

On the other hand, I don't issue a warning if neither of the channels is configured in the DTB.

I'm not going to commit either way on keeping/removing this. If someone else weighs in I'm open to changing it.

Quoted 18 lines
Quoted 18 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +9 -0
+static void k1_spi_host_init(struct k1_spi_driver_data *drv_data)
+{
 
[...]
 
+
+    ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
+    if (!ret) {
+        host->max_speed_hz = clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ,
+                       K1_SPI_MAX_SPEED_HZ);
+        if (host->max_speed_hz != max_speed_hz)
+            dev_warn(dev, "spi-max-frequency %u out of range, using 

%u\n",
+                max_speed_hz, host->max_speed_hz); +    } else {
+        if (ret != -EINVAL)

Quoted 8 lines
Quoted 4 lines

+            dev_warn(dev, "bad spi-max-frequency, using %u\n", +                 K1_SPI_DEFAULT_MAX_SPEED_HZ);
+        host->max_speed_hz = K1_SPI_DEFAULT_MAX_SPEED_HZ; +    }

I think it makes sense to have spi-max-frequency default to K1_SPI_DEFAULT_MAX_SPEED_HZ, but if the value is out of range just print a message and return an error, to get whoever wrote the bad value to fix it.

These errors just shouldn't happen.  But the way I handle this, it allows the SPI controller to still be used, even if the administrator can't really update the DTB.

Quoted 2 lines

This range seems to be fixed by hardware, so, it should also be specified in the bindings.

I define the hardware limits here, and enforce them, in case the bindings specify an out-of-range value.  Again, this is an error that just shouldn't occur in practice, but the code is defensive.

Most of your comments really made me think about how errors are handled.  I appreciate it.

                    -Alex

Quoted 43 lines
Quoted 18 lines
drivers/spi/spi-spacemit-k1.c new file +14 -0
+}
+
 
[...]
 
+
+static int k1_spi_probe(struct platform_device *pdev)
+{
+    struct k1_spi_driver_data *drv_data;
+    struct device *dev = &pdev->dev;
+    struct reset_control *reset;
+    struct spi_controller *host;
+    struct resource *iores;
+    struct clk *clk_bus;
+    int ret;
+
+    host = devm_spi_alloc_host(dev, sizeof(*drv_data));

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+    if (!host)
+        return -ENOMEM;
+    drv_data = spi_controller_get_devdata(host);
+    drv_data->controller = host;
+    platform_set_drvdata(pdev, drv_data);
+    drv_data->dev = dev;
+    init_completion(&drv_data->completion);
+
+    drv_data->base = devm_platform_get_and_ioremap_resource(pdev, 0,
+                                &iores);

Maybe

     devm_platform_ioremap_resource(pdev, 0);

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

Quoted 5 lines

[...]

drivers/spi/spi-spacemit-k1.c new file +3 -0
+
+MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver");
+MODULE_LICENSE("GPL");

Maybe MODULE_AUTHOR()?

Vivian "dramforever" Wang

#10 lore raw

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

In reply to Alex Elder
Quoted 63 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.

---
v2: - The SPI_SPACEMIT_K1 config option is added in sorted order
- The spi-spacemit-k1.o make target is now added in sorted order
- 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
- CONFIG_MMP_PDMA is checked at runtime, and if it is not enabled, DMA will not be used
- 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"

 drivers/spi/Kconfig           |   8 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/spi-spacemit-k1.c | 968 ++++++++++++++++++++++++++++++++++
 3 files changed, 977 insertions(+)
 create mode 100644 drivers/spi/spi-spacemit-k1.c
drivers/spi/Kconfig +8 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ 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
+	default 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 eefaeca097456..637d750ead996 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 +0 -0
new file mode 100644
index 0000000000000..8d564fe6c4303

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

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

SpacemiT?

Quoted 2 lines
drivers/spi/spi-spacemit-k1.c new file +2 -0
+ */
+

[...]

Quoted 5 lines
drivers/spi/spi-spacemit-k1.c new file +5 -0
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *rx = &drv_data->rx;
+	u32 bytes = drv_data->bytes;
+	u32 val;

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+
+	val = readl(drv_data->base + SSP_DATAR);
+	rx->resid -= bytes;
+
+	if (!rx->buf)
+		return;	/* Null reader: discard the data */
+
+	if (bytes == 1)
+		*(u8 *)rx->buf = val;
+	else if (bytes == 1)

typo?

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+		*(u16 *)rx->buf = val;
+	else
+		*(u32 *)rx->buf = val;
+
+	rx->buf += bytes;
+}
+

[...]

Quoted 9 lines
drivers/spi/spi-spacemit-k1.c new file +9 -0
+static bool k1_spi_write(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *tx = &drv_data->tx;
+	unsigned int count;
+	u32 val;
+
+	if (!tx->resid)
+		return true;	/* Nothing more to send */
+

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+	/* See how many slots in the TX FIFO are available */
+	val = readl(drv_data->base + SSP_STATUS);
+	count = FIELD_GET(SSP_STATUS_TFL, val);
+
+	/* A zero count means the FIFO is either full or empty */
+	if (!count) {
+		if (val & SSP_STATUS_TNF)
+			count = K1_SPI_FIFO_SIZE;
+		else
+			return false;	/* No room in the FIFO */

please early return:

if (!(val & SSP_STATUS_TNF))
return false;
count = K1_SPI_FIFO_SIZE;

Quoted 13 lines
drivers/spi/spi-spacemit-k1.c new file +13 -0
+	}
+
+	/*
+	 * Limit how much we try to send at a time, to reduce the
+	 * chance the other side can overrun our RX FIFO.
+	 */
+	count = min3(count, K1_SPI_THRESH, tx->resid);
+	while (count--)
+		k1_spi_write_word(drv_data);
+
+	return !tx->resid;
+}
+

[...]

Quoted 16 lines
drivers/spi/spi-spacemit-k1.c new file +16 -0
+
+static int k1_spi_transfer_one_message(struct spi_controller *host,
+					   struct spi_message *message)
+{
+	struct k1_spi_driver_data *drv_data = spi_controller_get_devdata(host);
+	struct completion *completion = &drv_data->completion;
+	struct spi_transfer *transfer;
+	u32 val;
+
+	drv_data->message = message;
+
+	/* Message status starts out successful; set to -EIO on error */
+	message->status = 0;
+
+	/* Hold frame low to avoid losing transferred data */
+	val = readl(drv_data->base + SSP_TOP_CTRL);

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+	val |= TOP_HOLD_FRAME_LOW;
+	writel(val, drv_data->base + SSP_TOP_CTRL);
+
+	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;

we don't need return a error code?

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

Quoted 9 lines
drivers/spi/spi-spacemit-k1.c new file +9 -0
+		}
+
+		k1_spi_transfer_wait(drv_data);
+
+		k1_spi_transfer_end(drv_data, transfer);
+
+		/* If an error has occurred, we're done */
+		if (message->status)
+			break;

ditto.

[...]

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

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+}
+
+static void k1_spi_dma_cleanup(struct device *dev, void *res)
+{
+	struct k1_spi_driver_data *drv_data = res;
+
+	if (k1_spi_dma_enabled(drv_data)) {

if (!k1_spi_dma_enabled(drv_data))
return;

- Troy

Quoted 219 lines
drivers/spi/spi-spacemit-k1.c new file +211 -0
+		k1_spi_dma_cleanup_io(drv_data, false);
+		k1_spi_dma_cleanup_io(drv_data, true);
+	}
+}
+
+
+static const struct of_device_id k1_spi_dt_ids[] = {
+	{ .compatible = "spacemit,k1-spi", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, k1_spi_dt_ids);
+
+static void k1_spi_host_init(struct k1_spi_driver_data *drv_data)
+{
+	struct device_node *np = dev_of_node(drv_data->dev);
+	struct spi_controller *host = drv_data->controller;
+	struct device *dev = drv_data->dev;
+	u32 max_speed_hz;
+	int ret;
+
+	host->dev.of_node = np;
+	host->dev.parent = drv_data->dev;
+	host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
+	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
+	host->num_chipselect = 1;
+
+	if (k1_spi_dma_enabled(drv_data))
+		host->dma_alignment = K1_SPI_DMA_ALIGNMENT;
+	host->cleanup = k1_spi_cleanup;
+	host->setup = k1_spi_setup;
+	host->transfer_one_message = k1_spi_transfer_one_message;
+
+	ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
+	if (!ret) {
+		host->max_speed_hz = clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ,
+					   K1_SPI_MAX_SPEED_HZ);
+		if (host->max_speed_hz != max_speed_hz)
+			dev_warn(dev, "spi-max-frequency %u out of range, using %u\n",
+				max_speed_hz, host->max_speed_hz);
+	} else {
+		if (ret != -EINVAL)
+			dev_warn(dev, "bad spi-max-frequency, using %u\n",
+				 K1_SPI_DEFAULT_MAX_SPEED_HZ);
+		host->max_speed_hz = K1_SPI_DEFAULT_MAX_SPEED_HZ;
+	}
+}
+
+/* Set our registers to a known initial state */
+static void
+k1_spi_register_reset(struct k1_spi_driver_data *drv_data, bool initial)
+{
+	u32 val = 0;
+
+	writel(0, drv_data->base + SSP_TOP_CTRL);
+
+	if (initial) {
+		/*
+		 * The TX and RX FIFO thresholds are the same no matter
+		 * what the speed or bits per word, so we can just set
+		 * them once.  The thresholds are one more than the values
+		 * in the register.
+		 */
+		val = FIELD_PREP(FIFO_RFT_MASK, K1_SPI_THRESH - 1);
+		val |= FIELD_PREP(FIFO_TFT_MASK, K1_SPI_THRESH - 1);
+	}
+	writel(val, drv_data->base + SSP_FIFO_CTRL);
+
+	writel(0, drv_data->base + SSP_INT_EN);
+	writel(0, drv_data->base + SSP_TIMEOUT);
+
+	/* Clear any pending interrupt conditions */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);
+}
+
+static irqreturn_t k1_spi_ssp_isr(int irq, void *dev_id)
+{
+	struct k1_spi_driver_data *drv_data = dev_id;
+	bool rx_done;
+	bool tx_done;
+	u32 val;
+
+	/* Get status and clear pending interrupts */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);
+
+	if (!drv_data->message)
+		return IRQ_NONE;
+
+	/* Check for a TX underrun or RX underrun first */
+	if (val & (SSP_STATUS_TUR | SSP_STATUS_ROR)) {
+		/* Disable all interrupts on error */
+		writel(0, drv_data->base + SSP_INT_EN);
+
+		drv_data->message->status = -EIO;
+		complete(&drv_data->completion);
+
+		return IRQ_HANDLED;
+	}
+
+	/* Drain the RX FIFO first, then transmit what we can */
+	rx_done = k1_spi_read(drv_data);
+	tx_done = k1_spi_write(drv_data);
+
+	/* Disable interrupts if we're done transferring either direction */
+	if (rx_done || tx_done) {
+		/* If both are done, disable all interrupts */
+		if (rx_done && tx_done) {
+			val = 0;
+		} else {
+			val = readl(drv_data->base + SSP_INT_EN);
+			if (rx_done)
+				val &= ~(SSP_INT_EN_TINTE | SSP_INT_EN_RIE);
+			if (tx_done)
+				val &= ~SSP_INT_EN_TIE;
+		}
+		writel(val, drv_data->base + SSP_INT_EN);
+	}
+
+	if (rx_done && tx_done)
+		complete(&drv_data->completion);
+
+	return IRQ_HANDLED;
+}
+
+static int k1_spi_probe(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data;
+	struct device *dev = &pdev->dev;
+	struct reset_control *reset;
+	struct spi_controller *host;
+	struct resource *iores;
+	struct clk *clk_bus;
+	int ret;
+
+	host = devm_spi_alloc_host(dev, sizeof(*drv_data));
+	if (!host)
+		return -ENOMEM;
+	drv_data = spi_controller_get_devdata(host);
+	drv_data->controller = host;
+	platform_set_drvdata(pdev, drv_data);
+	drv_data->dev = dev;
+	init_completion(&drv_data->completion);
+
+	drv_data->base = devm_platform_get_and_ioremap_resource(pdev, 0,
+								&iores);
+	if (IS_ERR(drv_data->base))
+		return dev_err_probe(dev, PTR_ERR(drv_data->base),
+				     "error mapping memory\n");
+	drv_data->base_addr = iores->start;
+
+	ret = devm_k1_spi_dma_setup(drv_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "error setting up DMA\n");
+
+	k1_spi_host_init(drv_data);
+
+	clk_bus = devm_clk_get_enabled(dev, "bus");
+	if (IS_ERR(clk_bus))
+		return dev_err_probe(dev, PTR_ERR(clk_bus),
+				     "error getting/enabling bus clock\n");
+	drv_data->bus_rate = clk_get_rate(clk_bus);
+
+	drv_data->clk = devm_clk_get_enabled(dev, "core");
+	if (IS_ERR(drv_data->clk))
+		return dev_err_probe(dev, PTR_ERR(drv_data->clk),
+				     "error getting/enabling core clock\n");
+
+	reset = devm_reset_control_get_exclusive_deasserted(dev, NULL);
+	if (IS_ERR(reset))
+		return dev_err_probe(dev, PTR_ERR(reset),
+				     "error getting/deasserting reset\n");
+
+	k1_spi_register_reset(drv_data, true);
+
+	drv_data->irq = platform_get_irq(pdev, 0);
+	if (drv_data->irq < 0)
+		return dev_err_probe(dev, drv_data->irq, "error getting IRQ\n");
+
+	ret = devm_request_irq(dev, drv_data->irq, k1_spi_ssp_isr,
+			       IRQF_SHARED, dev_name(dev), drv_data);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "error requesting IRQ\n");
+
+	ret = devm_spi_register_controller(dev, host);
+	if (ret)
+		dev_err(dev, "error registering controller\n");
+
+	return ret;
+}
+
+static void k1_spi_remove(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data = platform_get_drvdata(pdev);
+
+	k1_spi_register_reset(drv_data, false);
+}
+
+static struct platform_driver k1_spi_driver = {
+	.driver = {
+		.name		= "k1-spi",
+		.of_match_table	= k1_spi_dt_ids,
+	},
+	.probe			= k1_spi_probe,
+	.remove			= k1_spi_remove,
+};
+
+module_platform_driver(k1_spi_driver);
+
+MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver");
+MODULE_LICENSE("GPL");
Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#11 lore raw

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

Quoted 63 lines · Troy Mitchell
Quoted 62 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.

---
v2: - The SPI_SPACEMIT_K1 config option is added in sorted order
- The spi-spacemit-k1.o make target is now added in sorted order
- 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
- CONFIG_MMP_PDMA is checked at runtime, and if it is not enabled, DMA will not be used
- 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"

  drivers/spi/Kconfig           |   8 +
  drivers/spi/Makefile          |   1 +
  drivers/spi/spi-spacemit-k1.c | 968 ++++++++++++++++++++++++++++++++++
  3 files changed, 977 insertions(+)

create mode 100644 drivers/spi/spi-spacemit-k1.c

drivers/spi/Kconfig +8 -0
index 82fa5eb3b8684..4f6c446c6bc16 100644
@@ -1071,6 +1071,14 @@ 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
+	default 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 eefaeca097456..637d750ead996 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 +0 -0
new file mode 100644
index 0000000000000..8d564fe6c4303
Quoted 8 lines

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

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

SpacemiT?

Already addressed this suggestion, which Yixun also made.

https://lore.kernel.org/lkml/[email protected]/

Quoted 4 lines
Quoted 3 lines
drivers/spi/spi-spacemit-k1.c new file +2 -0
+ */
+

[...]

Quoted 16 lines
Quoted 5 lines
drivers/spi/spi-spacemit-k1.c new file +5 -0
+static void k1_spi_read_word(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *rx = &drv_data->rx;
+	u32 bytes = drv_data->bytes;
+	u32 val;

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +10 -0
+
+	val = readl(drv_data->base + SSP_DATAR);
+	rx->resid -= bytes;
+
+	if (!rx->buf)
+		return;	/* Null reader: discard the data */
+
+	if (bytes == 1)
+		*(u8 *)rx->buf = val;
+	else if (bytes == 1)

typo?

Yes, Vivian noticed this too. It's a bug, and it will be fixed in the next version.

https://lore.kernel.org/lkml/[email protected]/

Quoted 21 lines
Quoted 8 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+		*(u16 *)rx->buf = val;
+	else
+		*(u32 *)rx->buf = val;
+
+	rx->buf += bytes;
+}
+

[...]

Quoted 12 lines
drivers/spi/spi-spacemit-k1.c new file +12 -0
+static bool k1_spi_write(struct k1_spi_driver_data *drv_data)
+{
+	struct k1_spi_io *tx = &drv_data->tx;
+	unsigned int count;
+	u32 val;
+
+	if (!tx->resid)
+		return true;	/* Nothing more to send */
+
+	/* See how many slots in the TX FIFO are available */
+	val = readl(drv_data->base + SSP_STATUS);
+	count = FIELD_GET(SSP_STATUS_TFL, val);
Quoted 8 lines

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

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+
+	/* A zero count means the FIFO is either full or empty */
+	if (!count) {
+		if (val & SSP_STATUS_TNF)
+			count = K1_SPI_FIFO_SIZE;
+		else
+			return false;	/* No room in the FIFO */

please early return:

OK.

Quoted 37 lines

if (!(val & SSP_STATUS_TNF))
return false;
count = K1_SPI_FIFO_SIZE;

Quoted 13 lines
drivers/spi/spi-spacemit-k1.c new file +13 -0
+	}
+
+	/*
+	 * Limit how much we try to send at a time, to reduce the
+	 * chance the other side can overrun our RX FIFO.
+	 */
+	count = min3(count, K1_SPI_THRESH, tx->resid);
+	while (count--)
+		k1_spi_write_word(drv_data);
+
+	return !tx->resid;
+}
+

[...]

Quoted 19 lines
drivers/spi/spi-spacemit-k1.c new file +19 -0
+
+static int k1_spi_transfer_one_message(struct spi_controller *host,
+					   struct spi_message *message)
+{
+	struct k1_spi_driver_data *drv_data = spi_controller_get_devdata(host);
+	struct completion *completion = &drv_data->completion;
+	struct spi_transfer *transfer;
+	u32 val;
+
+	drv_data->message = message;
+
+	/* Message status starts out successful; set to -EIO on error */
+	message->status = 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);
+
Quoted 8 lines

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

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;

we don't need return a error code?

You're right. This always returns 0. I think I'll just return message->status.

Thank you for catching this. I'll fix it in the next version.

-Alex

Quoted 244 lines

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

Quoted 10 lines
drivers/spi/spi-spacemit-k1.c new file +9 -0
+		}
+
+		k1_spi_transfer_wait(drv_data);
+
+		k1_spi_transfer_end(drv_data, transfer);
+
+		/* If an error has occurred, we're done */
+		if (message->status)
+			break;

ditto.

[...]

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

Quoted 7 lines
drivers/spi/spi-spacemit-k1.c new file +7 -0
+}
+
+static void k1_spi_dma_cleanup(struct device *dev, void *res)
+{
+	struct k1_spi_driver_data *drv_data = res;
+
+	if (k1_spi_dma_enabled(drv_data)) {

if (!k1_spi_dma_enabled(drv_data))
return;

- Troy

Quoted 219 lines
drivers/spi/spi-spacemit-k1.c new file +211 -0
+		k1_spi_dma_cleanup_io(drv_data, false);
+		k1_spi_dma_cleanup_io(drv_data, true);
+	}
+}
+
+
+static const struct of_device_id k1_spi_dt_ids[] = {
+	{ .compatible = "spacemit,k1-spi", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, k1_spi_dt_ids);
+
+static void k1_spi_host_init(struct k1_spi_driver_data *drv_data)
+{
+	struct device_node *np = dev_of_node(drv_data->dev);
+	struct spi_controller *host = drv_data->controller;
+	struct device *dev = drv_data->dev;
+	u32 max_speed_hz;
+	int ret;
+
+	host->dev.of_node = np;
+	host->dev.parent = drv_data->dev;
+	host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
+	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
+	host->num_chipselect = 1;
+
+	if (k1_spi_dma_enabled(drv_data))
+		host->dma_alignment = K1_SPI_DMA_ALIGNMENT;
+	host->cleanup = k1_spi_cleanup;
+	host->setup = k1_spi_setup;
+	host->transfer_one_message = k1_spi_transfer_one_message;
+
+	ret = of_property_read_u32(np, "spi-max-frequency", &max_speed_hz);
+	if (!ret) {
+		host->max_speed_hz = clamp(max_speed_hz, K1_SPI_MIN_SPEED_HZ,
+					   K1_SPI_MAX_SPEED_HZ);
+		if (host->max_speed_hz != max_speed_hz)
+			dev_warn(dev, "spi-max-frequency %u out of range, using %u\n",
+				max_speed_hz, host->max_speed_hz);
+	} else {
+		if (ret != -EINVAL)
+			dev_warn(dev, "bad spi-max-frequency, using %u\n",
+				 K1_SPI_DEFAULT_MAX_SPEED_HZ);
+		host->max_speed_hz = K1_SPI_DEFAULT_MAX_SPEED_HZ;
+	}
+}
+
+/* Set our registers to a known initial state */
+static void
+k1_spi_register_reset(struct k1_spi_driver_data *drv_data, bool initial)
+{
+	u32 val = 0;
+
+	writel(0, drv_data->base + SSP_TOP_CTRL);
+
+	if (initial) {
+		/*
+		 * The TX and RX FIFO thresholds are the same no matter
+		 * what the speed or bits per word, so we can just set
+		 * them once.  The thresholds are one more than the values
+		 * in the register.
+		 */
+		val = FIELD_PREP(FIFO_RFT_MASK, K1_SPI_THRESH - 1);
+		val |= FIELD_PREP(FIFO_TFT_MASK, K1_SPI_THRESH - 1);
+	}
+	writel(val, drv_data->base + SSP_FIFO_CTRL);
+
+	writel(0, drv_data->base + SSP_INT_EN);
+	writel(0, drv_data->base + SSP_TIMEOUT);
+
+	/* Clear any pending interrupt conditions */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);
+}
+
+static irqreturn_t k1_spi_ssp_isr(int irq, void *dev_id)
+{
+	struct k1_spi_driver_data *drv_data = dev_id;
+	bool rx_done;
+	bool tx_done;
+	u32 val;
+
+	/* Get status and clear pending interrupts */
+	val = readl(drv_data->base + SSP_STATUS);
+	writel(val, drv_data->base + SSP_STATUS);
+
+	if (!drv_data->message)
+		return IRQ_NONE;
+
+	/* Check for a TX underrun or RX underrun first */
+	if (val & (SSP_STATUS_TUR | SSP_STATUS_ROR)) {
+		/* Disable all interrupts on error */
+		writel(0, drv_data->base + SSP_INT_EN);
+
+		drv_data->message->status = -EIO;
+		complete(&drv_data->completion);
+
+		return IRQ_HANDLED;
+	}
+
+	/* Drain the RX FIFO first, then transmit what we can */
+	rx_done = k1_spi_read(drv_data);
+	tx_done = k1_spi_write(drv_data);
+
+	/* Disable interrupts if we're done transferring either direction */
+	if (rx_done || tx_done) {
+		/* If both are done, disable all interrupts */
+		if (rx_done && tx_done) {
+			val = 0;
+		} else {
+			val = readl(drv_data->base + SSP_INT_EN);
+			if (rx_done)
+				val &= ~(SSP_INT_EN_TINTE | SSP_INT_EN_RIE);
+			if (tx_done)
+				val &= ~SSP_INT_EN_TIE;
+		}
+		writel(val, drv_data->base + SSP_INT_EN);
+	}
+
+	if (rx_done && tx_done)
+		complete(&drv_data->completion);
+
+	return IRQ_HANDLED;
+}
+
+static int k1_spi_probe(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data;
+	struct device *dev = &pdev->dev;
+	struct reset_control *reset;
+	struct spi_controller *host;
+	struct resource *iores;
+	struct clk *clk_bus;
+	int ret;
+
+	host = devm_spi_alloc_host(dev, sizeof(*drv_data));
+	if (!host)
+		return -ENOMEM;
+	drv_data = spi_controller_get_devdata(host);
+	drv_data->controller = host;
+	platform_set_drvdata(pdev, drv_data);
+	drv_data->dev = dev;
+	init_completion(&drv_data->completion);
+
+	drv_data->base = devm_platform_get_and_ioremap_resource(pdev, 0,
+								&iores);
+	if (IS_ERR(drv_data->base))
+		return dev_err_probe(dev, PTR_ERR(drv_data->base),
+				     "error mapping memory\n");
+	drv_data->base_addr = iores->start;
+
+	ret = devm_k1_spi_dma_setup(drv_data);
+	if (ret)
+		return dev_err_probe(dev, ret, "error setting up DMA\n");
+
+	k1_spi_host_init(drv_data);
+
+	clk_bus = devm_clk_get_enabled(dev, "bus");
+	if (IS_ERR(clk_bus))
+		return dev_err_probe(dev, PTR_ERR(clk_bus),
+				     "error getting/enabling bus clock\n");
+	drv_data->bus_rate = clk_get_rate(clk_bus);
+
+	drv_data->clk = devm_clk_get_enabled(dev, "core");
+	if (IS_ERR(drv_data->clk))
+		return dev_err_probe(dev, PTR_ERR(drv_data->clk),
+				     "error getting/enabling core clock\n");
+
+	reset = devm_reset_control_get_exclusive_deasserted(dev, NULL);
+	if (IS_ERR(reset))
+		return dev_err_probe(dev, PTR_ERR(reset),
+				     "error getting/deasserting reset\n");
+
+	k1_spi_register_reset(drv_data, true);
+
+	drv_data->irq = platform_get_irq(pdev, 0);
+	if (drv_data->irq < 0)
+		return dev_err_probe(dev, drv_data->irq, "error getting IRQ\n");
+
+	ret = devm_request_irq(dev, drv_data->irq, k1_spi_ssp_isr,
+			       IRQF_SHARED, dev_name(dev), drv_data);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "error requesting IRQ\n");
+
+	ret = devm_spi_register_controller(dev, host);
+	if (ret)
+		dev_err(dev, "error registering controller\n");
+
+	return ret;
+}
+
+static void k1_spi_remove(struct platform_device *pdev)
+{
+	struct k1_spi_driver_data *drv_data = platform_get_drvdata(pdev);
+
+	k1_spi_register_reset(drv_data, false);
+}
+
+static struct platform_driver k1_spi_driver = {
+	.driver = {
+		.name		= "k1-spi",
+		.of_match_table	= k1_spi_dt_ids,
+	},
+	.probe			= k1_spi_probe,
+	.remove			= k1_spi_remove,
+};
+
+module_platform_driver(k1_spi_driver);
+
+MODULE_DESCRIPTION("SpacemiT K1 SPI controller driver");
+MODULE_LICENSE("GPL");
Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#12 lore raw

PATCH v2 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.)

---
v2: - 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

 .../boot/dts/spacemit/k1-bananapi-f3.dts      |  7 +++++++
 arch/riscv/boot/dts/spacemit/k1-pinctrl.dtsi  | 20 +++++++++++++++++++
 arch/riscv/boot/dts/spacemit/k1.dtsi          | 16 +++++++++++++++
 3 files changed, 43 insertions(+)
arch/riscv/boot/dts/spacemit/k1-bananapi-f3.dts +7 -0
index 2aaaff77831e1..d9d865fbe320e 100644
@@ -14,6 +14,7 @@ aliases {
 		ethernet0 = &eth0;
 		ethernet1 = &eth1;
 		serial0 = &uart0;
+		spi3 = &spi3;
 	};
 
 	chosen {
@@ -92,6 +93,12 @@ &pdma {
 	status = "okay";
 };
 
+&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 aff19c86d5ff3..205c201a3005c 100644
@@ -76,4 +76,24 @@ pwm14-1-pins {
 			drive-strength = <32>;
 		};
 	};
+
+	ssp3_0_cfg: ssp3-0-cfg {
+		ssp3-0-no-pull-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 +16 -0
index 6cdcd80a7c83b..f8c37d16968e4 100644
@@ -856,6 +856,22 @@ storage-bus {
 			#size-cells = <2>;
 			dma-ranges = <0x0 0x00000000 0x0 0x00000000 0x0 0x80000000>;
 
+			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";
+			};
+
 			emmc: mmc@d4281000 {
 				compatible = "spacemit,k1-sdhci";
 				reg = <0x0 0xd4281000 0x0 0x200>;
Signature · 1 line
2.48.1
#13 lore raw

Re: [PATCH v2 3/3] riscv: dts: spacemit: define a SPI controller node

Quoted 16 lines · Alex Elder

[...]

arch/riscv/boot/dts/spacemit/k1.dtsi +6 -0
index 6cdcd80a7c83b..f8c37d16968e4 100644
@@ -856,6 +856,22 @@ storage-bus {
 			#size-cells = <2>;
 			dma-ranges = <0x0 0x00000000 0x0 0x00000000 0x0 0x80000000>;
 
+			spi3: spi@d401c000 {
+				compatible = "spacemit,k1-spi";
+				reg = <0x0 0xd401c000 0x0 0x30>;
+				#address-cells = <1>;
+				#size-cells = <0>;
+				clocks = <&syscon_apbc CLK_SSP3>,

arch/riscv/boot/dts/spacemit/k1.dtsi storage-bus { · line 874

Quoted 10 lines
arch/riscv/boot/dts/spacemit/k1.dtsi +10 -0
+					 <&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";
+			};
+

Is storage-bus the right place for SPI? I'd have thought that SPI wouldn't need its own dma-ranges if it does DMA though &pdma.

I know "dram_range4" is where SpacemiT put it but I'm not sure if that makes sense now.

Vivian "dramforever" Wang

author
#14 lore raw

Re: [PATCH v2 3/3] riscv: dts: spacemit: define a SPI controller node

Quoted 32 lines · Vivian Wang
Quoted 16 lines · Alex Elder

[...]

arch/riscv/boot/dts/spacemit/k1.dtsi +6 -0
index 6cdcd80a7c83b..f8c37d16968e4 100644
@@ -856,6 +856,22 @@ storage-bus {
  			#size-cells = <2>;
  			dma-ranges = <0x0 0x00000000 0x0 0x00000000 0x0 0x80000000>;
 
+			spi3: spi@d401c000 {
+				compatible = "spacemit,k1-spi";
+				reg = <0x0 0xd401c000 0x0 0x30>;
+				#address-cells = <1>;
+				#size-cells = <0>;
+				clocks = <&syscon_apbc CLK_SSP3>,

arch/riscv/boot/dts/spacemit/k1.dtsi storage-bus { · line 874

Quoted 10 lines
arch/riscv/boot/dts/spacemit/k1.dtsi +10 -0
+					 <&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";
+			};
+

Is storage-bus the right place for SPI? I'd have thought that SPI wouldn't need its own dma-ranges if it does DMA though &pdma.

Quoted 2 lines

I know "dram_range4" is where SpacemiT put it but I'm not sure if that makes sense now.

You're right. It belongs in the dma-bus region. I will fix that in v3.

-Alex

Quoted 3 lines

Vivian "dramforever" Wang