Skip to contentSkip to conversation

Add Efinix FPGA SPI programming support

PATCH Superseded [email protected] 4 participants 12 messages Nov 19, 2025 – Feb 2

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add Efinix FPGA SPI programming support

From: Ian Dannapel <[email protected]>

Hi all,

This is the v5 version of the series that adds support for programming Efinix FPGAs over SPI using the fpga-mgr subsystem.

The driver implements the passive-serial configuration protocol and allows loading full bitstreams via an SPI controller. The binding describes the Efinix Trion SPI interface and documents the DT requirements for enabling the driver.

All v4 remarks should now be adressed.
Please let me know if further adjustments are needed.

Testing:
- Verified on an Efinix Trion T13 custom board
- Tested full bitstream configuration over SPI at 25 MHz

Changelog since v4:

dt-bindings: fpga: Add Efinix SPI programming bindings:
- efinix,spi.yaml: remove unused binding efinix,fpga-spi
- Rename efinix,spi.yaml to match a compatible (efinix,trion-spi.yaml)
- efinix,trion-spi.yaml: remove notes not relevant to hardware description

fpga-mgr: Add Efinix SPI programming driver
- efinix-spi.c: improve driver description header
- efinix-spi.c: replace hardcoded numbers with macros
- efinix-spi.c: use spi_message_init_with_transfers instead of spi message init + tail
- efinix-spi.c: rework write logic to avoid deasserting the cs or resetting the hardware in between write chunks

Ian Dannapel (3):
dt-bindings: vendor-prefix: Add prefix for Efinix, Inc.
dt-bindings: fpga: Add Efinix SPI programming bindings
fpga-mgr: Add Efinix SPI programming driver

 .../bindings/fpga/efinix,trion-spi.yaml       |  77 ++++++
 .../devicetree/bindings/vendor-prefixes.yaml  |   2 +
 drivers/fpga/Kconfig                          |   7 +
 drivers/fpga/Makefile                         |   1 +
 drivers/fpga/efinix-spi.c                     | 256 ++++++++++++++++++
 5 files changed, 343 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml
 create mode 100644 drivers/fpga/efinix-spi.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/3 dt-bindings: vendor-prefix: Add prefix for Efinix, Inc.

From: Ian Dannapel <[email protected]>

Add entry for Efinix, Inc. (https://www.efinixinc.com/)

---
 Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/vendor-prefixes.yaml +2 -0
index f1d1882009ba..afe41267df32 100644
@@ -469,6 +469,8 @@ patternProperties:
     description: Emtop Embedded Solutions
   "^eeti,.*":
     description: eGalax_eMPIA Technology Inc
+  "^efinix,.*":
+    description: Efinix, Inc.
   "^egnite,.*":
     description: egnite GmbH
   "^einfochips,.*":
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v5 1/3] dt-bindings: vendor-prefix: Add prefix for Efinix, Inc.

Quoted 6 lines · On 19/11/2025 19:47, [email protected] wrote:

From: Ian Dannapel <[email protected]>

Add entry for Efinix, Inc. (https://www.efinixinc.com/)

There are no blank lines between tags. Please start using b4 (b4 trailers).

Quoted 3 lines

---

Best regards,
Krzysztof

#3 lore raw

PATCH v5 2/3 dt-bindings: fpga: Add Efinix SPI programming bindings

In reply to [email protected]

From: Ian Dannapel <[email protected]>

Add device tree bindings documentation for configuring Efinix FPGA using serial SPI passive programming mode.

---
 .../bindings/fpga/efinix,trion-spi.yaml       | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +77 -0
new file mode 100644
index 000000000000..9ac37e5e5094
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/fpga/efinix,spi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Efinix SPI FPGA Manager
+
+maintainers:
+  - Ian Dannapel <[email protected]>
+
+description: |
+  Efinix FPGAs (Trion, Topaz, and Titanium families) support loading bitstreams
+  through "SPI Passive Mode".
+  Note: Additional pins hogs for bus width configuration must be set
+  elsewhere, if necessary. Only bus width 1x serial is supported.
+
+  References:
+  - https://www.efinixinc.com/docs/an006-configuring-trion-fpgas-v6.3.pdf
+  - https://www.efinixinc.com/docs/an033-configuring-titanium-fpgas-v2.8.pdf
+  - https://www.efinixinc.com/docs/an061-configuring-topaz-fpgas-v1.1.pdf
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    enum:
+      - efinix,trion-spi
+      - efinix,titanium-spi
+      - efinix,topaz-spi
+
+  spi-cpha: true
+
+  spi-cpol: true
+
+  spi-max-frequency:
+    maximum: 25000000
+
+  reg:
+    maxItems: 1
+
+  reset-gpios:
+    description:
+      reset and re-configuration trigger pin (low active)
+    maxItems: 1
+
+  cdone-gpios:
+    description:
+      optional configuration done status pin (high active)
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - reset-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+      cs-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>;
+      fpga-mgr@0 {
+        compatible = "efinix,trion-spi";
+        reg = <0>;
+        spi-max-frequency = <25000000>;
+        spi-cpha;
+        spi-cpol;
+        reset-gpios = <&gpio4 17 GPIO_ACTIVE_LOW>;
+        cdone-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
+      };
+    };
+...
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v5 2/3] dt-bindings: fpga: Add Efinix SPI programming bindings

Quoted 11 lines · On Wed, 19 Nov 2025 19:47:05 +0100, [email protected] wrote:

From: Ian Dannapel <[email protected]>

Add device tree bindings documentation for configuring Efinix FPGA using serial SPI passive programming mode.

---
 .../bindings/fpga/efinix,trion-spi.yaml       | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml: $id: Cannot determine base path from $id, relative path/filename doesn't match actual path or filename
$id: http://devicetree.org/schemas/fpga/efinix,spi.yaml
file: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

#5 lore raw

Re: [PATCH v5 2/3] dt-bindings: fpga: Add Efinix SPI programming bindings

In reply to [email protected]
Quoted 38 lines · On 19/11/2025 19:47, [email protected] wrote:

From: Ian Dannapel <[email protected]>

Add device tree bindings documentation for configuring Efinix FPGA using serial SPI passive programming mode.

---
 .../bindings/fpga/efinix,trion-spi.yaml       | 77 +++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +21 -0
new file mode 100644
index 000000000000..9ac37e5e5094
@@ -0,0 +1,77 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/fpga/efinix,spi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Efinix SPI FPGA Manager
+
+maintainers:
+  - Ian Dannapel <[email protected]>
+
+description: |
+  Efinix FPGAs (Trion, Topaz, and Titanium families) support loading bitstreams
+  through "SPI Passive Mode".
+  Note: Additional pins hogs for bus width configuration must be set
+  elsewhere, if necessary. Only bus width 1x serial is supported.
+
+  References:
+  - https://www.efinixinc.com/docs/an006-configuring-trion-fpgas-v6.3.pdf
+  - https://www.efinixinc.com/docs/an033-configuring-titanium-fpgas-v2.8.pdf
+  - https://www.efinixinc.com/docs/an061-configuring-topaz-fpgas-v1.1.pdf

Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml line 31

Quoted 10 lines
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +10 -0
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    enum:
+      - efinix,trion-spi
+      - efinix,titanium-spi
+      - efinix,topaz-spi

Nothing improved. You received comments about this - twice or more even
- so I feel like you just don't care about review.

NAK

Best regards,
Krzysztof

#6 lore raw

Re: [PATCH v5 2/3] dt-bindings: fpga: Add Efinix SPI programming bindings

Hello Krzysztof,

Quoted 10 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml line 31

Quoted 6 lines
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +6 -0
+properties:
+  compatible:
+    enum:
+      - efinix,trion-spi
+      - efinix,titanium-spi
+      - efinix,topaz-spi

Nothing improved. You received comments about this - twice or more even
- so I feel like you just don't care about review.

Sorry I am lost here, this is based on a similar driver, but it is probably very outdated.
So the fallback pattern would be okay? Eg:
items:
- enum:
- efinix,trion-config
- efinix,titanium-config
- efinix,topaz-config
- const: efinix,fpga-config
Or would a single compatible be better since currently from the drivers perspective there is
no difference in hardware?

Quoted 4 lines

NAK

Best regards,
Krzysztof

Thanks for review
Best regards,
Ian

#7 lore raw

Re: [PATCH v5 2/3] dt-bindings: fpga: Add Efinix SPI programming bindings

Quoted 13 lines · Ian Dannapel

Hello Krzysztof,

Quoted 10 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml line 31

Quoted 6 lines
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +6 -0
+properties:
+  compatible:
+    enum:
+      - efinix,trion-spi
+      - efinix,titanium-spi
+      - efinix,topaz-spi

Nothing improved. You received comments about this - twice or more even
- so I feel like you just don't care about review.

Quoted 12 lines

Sorry I am lost here, this is based on a similar driver, but it is probably very outdated.
So the fallback pattern would be okay? Eg:
items:
- enum:
- efinix,trion-config
- efinix,titanium-config
- efinix,topaz-config
- const: efinix,fpga-config
Or would a single compatible be better since currently from the drivers perspective there is
no difference in hardware?

Almost, because I think you asked about this and I instead asked to use one specific device as the fallback. Choose the oldest device and use it the fallback for two others. There is plenty of examples - including example-schema - using it with oneOf syntax.

Best regards,
Krzysztof

#8 lore raw

Re: [PATCH v5 2/3] dt-bindings: fpga: Add Efinix SPI programming bindings

Hi Krzysztof,

Quoted 28 lines · Krzysztof Kozlowski
Quoted 13 lines · Ian Dannapel

Hello Krzysztof,

Quoted 10 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml line 31

Quoted 6 lines
Documentation/devicetree/bindings/fpga/efinix,trion-spi.yaml new file +6 -0
+properties:
+  compatible:
+    enum:
+      - efinix,trion-spi
+      - efinix,titanium-spi
+      - efinix,topaz-spi

Nothing improved. You received comments about this - twice or more even
- so I feel like you just don't care about review.

Quoted 12 lines

Sorry I am lost here, this is based on a similar driver, but it is probably very outdated.
So the fallback pattern would be okay? Eg:
items:
- enum:
- efinix,trion-config
- efinix,titanium-config
- efinix,topaz-config
- const: efinix,fpga-config
Or would a single compatible be better since currently from the drivers perspective there is
no difference in hardware?

Quoted 7 lines

Almost, because I think you asked about this and I instead asked to use one specific device as the fallback. Choose the oldest device and use it the fallback for two others. There is plenty of examples - including example-schema - using it with oneOf syntax.

Best regards,
Krzysztof

Just making sure before sending a new version. Like this? compatible:
oneOf:
- items:
- enum:
- efinix,titanium-config
- efinix,topaz-config
- const: efinix,trion-config
- const: efinix,trion-config

Regards,
Ian

#9 lore raw

PATCH v5 3/3 fpga-mgr: Add Efinix SPI programming driver

In reply to [email protected]

From: Ian Dannapel <[email protected]>

Add a new driver for loading binary firmware to configuration RAM using "SPI passive mode" on Efinix FPGAs.

---
 drivers/fpga/Kconfig      |   7 ++
 drivers/fpga/Makefile     |   1 +
 drivers/fpga/efinix-spi.c | 256 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+)
 create mode 100644 drivers/fpga/efinix-spi.c
drivers/fpga/Kconfig +7 -0
index 37b35f58f0df..b5d60ba62900 100644
@@ -83,6 +83,13 @@ config FPGA_MGR_XILINX_SPI
 	  FPGA manager driver support for Xilinx FPGA configuration
 	  over slave serial interface.
 
+config FPGA_MGR_EFINIX_SPI
+	tristate "Efinix FPGA configuration over SPI"
+	depends on SPI
+	help
+	  FPGA manager driver support for Efinix FPGAs configuration over SPI
+	  (passive mode only).
+
 config FPGA_MGR_ICE40_SPI
 	tristate "Lattice iCE40 SPI"
 	depends on OF && SPI
drivers/fpga/Makefile +1 -0
index aeb89bb13517..adbd51d2cd1e 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_FPGA_MGR_TS73XX)		+= ts73xx-fpga.o
 obj-$(CONFIG_FPGA_MGR_XILINX_CORE)	+= xilinx-core.o
 obj-$(CONFIG_FPGA_MGR_XILINX_SELECTMAP)	+= xilinx-selectmap.o
 obj-$(CONFIG_FPGA_MGR_XILINX_SPI)	+= xilinx-spi.o
+obj-$(CONFIG_FPGA_MGR_EFINIX_SPI)	+= efinix-spi.o
 obj-$(CONFIG_FPGA_MGR_ZYNQ_FPGA)	+= zynq-fpga.o
 obj-$(CONFIG_FPGA_MGR_ZYNQMP_FPGA)	+= zynqmp-fpga.o
 obj-$(CONFIG_FPGA_MGR_VERSAL_FPGA)	+= versal-fpga.o
drivers/fpga/efinix-spi.c new file +256 -0

256 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v5 3/3] fpga-mgr: Add Efinix SPI programming driver

Quoted 18 lines · On Wed, Nov 19, 2025 at 07:47:06PM +0100, [email protected] wrote:

From: Ian Dannapel <[email protected]>

Add a new driver for loading binary firmware to configuration RAM using "SPI passive mode" on Efinix FPGAs.

---
 drivers/fpga/Kconfig      |   7 ++
 drivers/fpga/Makefile     |   1 +
 drivers/fpga/efinix-spi.c | 256 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 264 insertions(+)
 create mode 100644 drivers/fpga/efinix-spi.c
drivers/fpga/Kconfig +0 -0
index 37b35f58f0df..b5d60ba62900 100644

drivers/fpga/Kconfig config FPGA_MGR_XILINX_SPI · line 92

Quoted 10 lines
drivers/fpga/Kconfig +7 -0
@@ -83,6 +83,13 @@ config FPGA_MGR_XILINX_SPI
 	  FPGA manager driver support for Xilinx FPGA configuration
 	  over slave serial interface.
 
+config FPGA_MGR_EFINIX_SPI
+	tristate "Efinix FPGA configuration over SPI"
+	depends on SPI
+	help
+	  FPGA manager driver support for Efinix FPGAs configuration over SPI
+	  (passive mode only).
+

Any reason insert it here? If no add it at tail.

drivers/fpga/Makefile obj-$(CONFIG_FPGA_MGR_TS73XX) += ts73xx-fpga.o · line 21

Quoted 12 lines
drivers/fpga/Kconfig +0 -0
 config FPGA_MGR_ICE40_SPI
 	tristate "Lattice iCE40 SPI"
 	depends on OF && SPI
drivers/fpga/Makefile +1 -0
index aeb89bb13517..adbd51d2cd1e 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_FPGA_MGR_TS73XX)		+= ts73xx-fpga.o
 obj-$(CONFIG_FPGA_MGR_XILINX_CORE)	+= xilinx-core.o
 obj-$(CONFIG_FPGA_MGR_XILINX_SELECTMAP)	+= xilinx-selectmap.o
 obj-$(CONFIG_FPGA_MGR_XILINX_SPI)	+= xilinx-spi.o
+obj-$(CONFIG_FPGA_MGR_EFINIX_SPI)	+= efinix-spi.o

Ditto, maybe add it at tail of "FPGA Manager Drivers"?

Quoted 20 lines
drivers/fpga/Makefile +0 -0
 obj-$(CONFIG_FPGA_MGR_ZYNQ_FPGA)	+= zynq-fpga.o
 obj-$(CONFIG_FPGA_MGR_ZYNQMP_FPGA)	+= zynqmp-fpga.o
 obj-$(CONFIG_FPGA_MGR_VERSAL_FPGA)	+= versal-fpga.o
drivers/fpga/efinix-spi.c new file +11 -0
new file mode 100644
index 000000000000..953cf94f1f03
@@ -0,0 +1,256 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * FPGA Manager Driver for Efinix
+ *
+ * Copyright (C) 2025 iris-GmbH infrared & intelligent sensors
+ *
+ * Ian Dannapel <[email protected]>
+ *
+ * Load Efinix FPGA firmware over SPI using the serial configuration interface.
+ *
+ * Note: Only passive mode (host initiates transfer) is currently supported.

drivers/fpga/efinix-spi.c line 21

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+ */
+
+#include <linux/delay.h>
+#include <linux/fpga/fpga-mgr.h>
+#include <linux/gpio/consumer.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/spi/spi.h>
+
+/* 13 dummy bytes → 104 SPI clock cycles (8 bits each)

Wrong format for multi-line comments, please see:

Documentation/process/coding-style.rst

drivers/fpga/efinix-spi.c line 26

Quoted 5 lines
drivers/fpga/efinix-spi.c new file +5 -0
+ * Used to meet the requirement for >100 clock cycles idle sequence.
+ */
+#define EFINIX_SPI_IDLE_CYCLES_BYTES 13
+
+/* tDMIN: Minimum time between deassertion of CRESET_N to first

ditto, please go through the entire patch for this.

Quoted 39 lines
drivers/fpga/efinix-spi.c new file +39 -0
+ * valid configuration data. (32 µs)
+ */
+#define EFINIX_TDMIN_US_MIN    35
+#define EFINIX_TDMIN_US_MAX    40
+
+/* tCRESET_N: Minimum CRESET_N low pulse width required to
+ * trigger re-configuration. (320 ns)
+ */
+#define EFINIX_TCRESETN_DELAY_MIN_US  1
+#define EFINIX_TCRESETN_DELAY_MAX_US  2
+
+/* tUSER: Minimum configuration duration after CDONE goes high
+ * before entering user mode. (25 µs)
+ */
+#define EFINIX_TUSER_US_MIN    30
+#define EFINIX_TUSER_US_MAX    35
+
+struct efinix_spi_conf {
+	struct spi_device *spi;
+	struct gpio_desc *cdone;
+	struct gpio_desc *reset;
+	bool bus_locked;
+};
+
+static void efinix_spi_reset(struct efinix_spi_conf *conf)
+{
+	gpiod_set_value(conf->reset, 1);
+	usleep_range(EFINIX_TCRESETN_DELAY_MIN_US, EFINIX_TCRESETN_DELAY_MAX_US);
+	gpiod_set_value(conf->reset, 0);
+	usleep_range(EFINIX_TDMIN_US_MIN, EFINIX_TDMIN_US_MAX);
+}
+
+static enum fpga_mgr_states efinix_spi_state(struct fpga_manager *mgr)
+{
+	struct efinix_spi_conf *conf = mgr->priv;
+
+	if (conf->cdone && gpiod_get_value(conf->cdone) == 1)
+		return FPGA_MGR_STATE_OPERATING;
+

drivers/fpga/efinix-spi.c line 75

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+	return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static int efinix_spi_write_init(struct fpga_manager *mgr,
+				 struct fpga_image_info *info,
+				 const char *buf, size_t count)
+{
+	struct efinix_spi_conf *conf = mgr->priv;
+	struct spi_transfer assert_cs = {
+		.cs_change = 1,

You've addressed an important concern, please briefly describe in this function & changelog: what's the HW requirement, how to solve it by cs_change & spi_bus_lock().

Quoted 32 lines
drivers/fpga/efinix-spi.c new file +32 -0
+	};
+	struct spi_message message;
+	int ret;
+
+	if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
+		dev_err(&mgr->dev, "Partial reconfiguration not supported\n");
+		return -EOPNOTSUPP;
+	}
+
+	spi_bus_lock(conf->spi->controller);
+	conf->bus_locked = true;
+	spi_message_init_with_transfers(&message, &assert_cs, 1);
+	ret = spi_sync_locked(conf->spi, &message);
+	if (ret) {
+		spi_bus_unlock(conf->spi->controller);
+		conf->bus_locked = false;
+		return ret;
+	}
+
+	/* Reset with CS asserted */
+	efinix_spi_reset(conf);
+
+	return 0;
+}
+
+static int efinix_spi_write(struct fpga_manager *mgr, const char *buf,
+			    size_t count)
+{
+	struct spi_transfer write_xfer = {
+		.tx_buf = buf,
+		.len = count,
+		.cs_change = 1, /* Keep CS asserted */

drivers/fpga/efinix-spi.c line 117

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+	};
+	struct efinix_spi_conf *conf = mgr->priv;
+	struct spi_message message;
+	int ret;
+
+	spi_message_init_with_transfers(&message, &write_xfer, 1);
+	ret = spi_sync_locked(conf->spi, &message);
+	if (ret) {
+		dev_err(&mgr->dev, "SPI error in firmware write: %d\n", ret);
+		if (conf->bus_locked) {

Do we really need this flag? If we failed to lock bus on write_init(), we can't get here, is it?

Quoted 4 lines
drivers/fpga/efinix-spi.c new file +4 -0
+			spi_bus_unlock(conf->spi->controller);
+			conf->bus_locked = false;
+		}
+	}

drivers/fpga/efinix-spi.c line 131

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+	return ret;
+}
+
+static int efinix_spi_write_complete(struct fpga_manager *mgr,
+				     struct fpga_image_info *info)
+{
+	unsigned long timeout =
+		jiffies + usecs_to_jiffies(info->config_complete_timeout_us);
+	struct spi_transfer clk_cycles = {
+		.len = EFINIX_SPI_IDLE_CYCLES_BYTES,

Here we also release the cs, is it? Please add comments here.

drivers/fpga/efinix-spi.c line 135

Quoted 4 lines
drivers/fpga/efinix-spi.c new file +4 -0
+	};
+	struct efinix_spi_conf *conf = mgr->priv;
+	struct spi_message message;
+	int ret = -1, done = 0;

Why initialize these 2 variables, IIUC they are all re-assigned before any usage.

Quoted 75 lines
drivers/fpga/efinix-spi.c new file +75 -0
+	bool expired = false;
+	u8 *dummy_buf;
+
+	dummy_buf = kzalloc(EFINIX_SPI_IDLE_CYCLES_BYTES, GFP_KERNEL);
+	if (!dummy_buf) {
+		ret = -ENOMEM;
+		goto unlock_spi;
+	}
+
+	clk_cycles.tx_buf = dummy_buf;
+	spi_message_init_with_transfers(&message, &clk_cycles, 1);
+	ret = spi_sync_locked(conf->spi, &message);
+	if (ret) {
+		dev_err(&mgr->dev, "SPI error in write complete: %d\n", ret);
+		goto free_buf;
+	}
+
+	if (conf->cdone) {
+		while (!expired) {
+			done = gpiod_get_value(conf->cdone);
+			if (done < 0) {
+				ret = done;
+				goto free_buf;
+			}
+			if (done)
+				break;
+
+			usleep_range(10, 20);
+			expired = time_after(jiffies, timeout);
+		}
+
+		if (expired) {
+			dev_err(&mgr->dev, "Timeout waiting for CDONE\n");
+			ret = -ETIMEDOUT;
+			goto free_buf;
+		}
+	}
+
+	usleep_range(EFINIX_TUSER_US_MIN, EFINIX_TUSER_US_MAX);
+
+free_buf:
+	kfree(dummy_buf);
+unlock_spi:
+	if (conf->bus_locked) {
+		spi_bus_unlock(conf->spi->controller);
+		conf->bus_locked = false;
+	}
+	return ret;
+}
+
+static const struct fpga_manager_ops efinix_spi_ops = {
+	.state = efinix_spi_state,
+	.write_init = efinix_spi_write_init,
+	.write = efinix_spi_write,
+	.write_complete = efinix_spi_write_complete,
+};
+
+static int efinix_spi_probe(struct spi_device *spi)
+{
+	struct efinix_spi_conf *conf;
+	struct fpga_manager *mgr;
+
+	conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
+	if (!conf)
+		return -ENOMEM;
+
+	conf->spi = spi;
+
+	conf->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
+	if (IS_ERR(conf->reset))
+		return dev_err_probe(&spi->dev, PTR_ERR(conf->reset),
+				     "Failed to get RESET gpio\n");
+
+	if (!(spi->mode & SPI_CPHA) || !(spi->mode & SPI_CPOL))
+		return dev_err_probe(&spi->dev, -EINVAL,

drivers/fpga/efinix-spi.c line 220

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+				     "Unsupported SPI mode, set CPHA and CPOL\n");
+
+	conf->cdone = devm_gpiod_get_optional(&spi->dev, "cdone", GPIOD_IN);
+	if (IS_ERR(conf->cdone))
+		return dev_err_probe(&spi->dev, PTR_ERR(conf->cdone),
+				     "Failed to get CDONE gpio\n");
+
+	mgr = devm_fpga_mgr_register(&spi->dev,
+				     "Efinix FPGA Manager",
+				     &efinix_spi_ops, conf);

The initialization sequence is quite random...

1. if (!(spi->mode & SPI_CPHA) || !(spi->mode & SPI_CPOL))
2. conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
3. conf->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
4. conf->cdone = devm_gpiod_get_optional(&spi->dev, "cdone", GPIOD_IN);
5. Initialize other fields in conf.
6. mgr = devm_fpga_mgr_register()
7. return PTR_ERR_OR_ZERO(mgr);

Is it better?

Thanks,
Yilun

Quoted 40 lines
drivers/fpga/efinix-spi.c new file +36 -0
+
+	conf->bus_locked = false;
+
+	return PTR_ERR_OR_ZERO(mgr);
+}
+
+static const struct of_device_id efinix_spi_of_match[] = {
+	{ .compatible = "efinix,trion-spi", },
+	{ .compatible = "efinix,titanium-spi", },
+	{ .compatible = "efinix,topaz-spi", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, efinix_spi_of_match);
+
+static const struct spi_device_id efinix_ids[] = {
+	{ "trion-spi", 0 },
+	{ "titanium-spi", 0 },
+	{ "topaz-spi", 0 },
+	{},
+};
+MODULE_DEVICE_TABLE(spi, efinix_ids);
+
+static struct spi_driver efinix_spi_driver = {
+	.driver = {
+		.name = "efinix-spi",
+		.of_match_table = efinix_spi_of_match,
+	},
+	.probe = efinix_spi_probe,
+	.id_table = efinix_ids,
+};
+
+module_spi_driver(efinix_spi_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Ian Dannapel <[email protected]>");
+MODULE_DESCRIPTION("Efinix FPGA SPI Programming Driver");
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v5 3/3] fpga-mgr: Add Efinix SPI programming driver

Hey Yilun,
thank you very much for your review

Quoted 10 lines · Xu Yilun

drivers/fpga/Kconfig config FPGA_MGR_XILINX_SPI · line 92

Quoted 8 lines
drivers/fpga/Kconfig +7 -0
+config FPGA_MGR_EFINIX_SPI
+     tristate "Efinix FPGA configuration over SPI"
+     depends on SPI
+     help
+       FPGA manager driver support for Efinix FPGAs configuration over SPI
+       (passive mode only).
+

Any reason insert it here? If no add it at tail.

No reason at all, will move to the back.

Quoted 7 lines
Quoted 5 lines

obj-$(CONFIG_FPGA_MGR_XILINX_SELECTMAP) += xilinx-selectmap.o
obj-$(CONFIG_FPGA_MGR_XILINX_SPI) += xilinx-spi.o
+obj-$(CONFIG_FPGA_MGR_EFINIX_SPI) += efinix-spi.o

Ditto, maybe add it at tail of "FPGA Manager Drivers"?

of course

Quoted 7 lines

drivers/fpga/efinix-spi.c line 21

Quoted 3 lines
drivers/fpga/efinix-spi.c new file +3 -0
+#include <linux/spi/spi.h>
+
+/* 13 dummy bytes → 104 SPI clock cycles (8 bits each)

Wrong format for multi-line comments, please see:

Documentation/process/coding-style.rst

okay, will fix all comments styling

Quoted 54 lines

drivers/fpga/efinix-spi.c line 26

Quoted 6 lines

+ * Used to meet the requirement for >100 clock cycles idle sequence.
+ */

drivers/fpga/efinix-spi.c new file +3 -0
+#define EFINIX_SPI_IDLE_CYCLES_BYTES 13
+
+/* tDMIN: Minimum time between deassertion of CRESET_N to first

ditto, please go through the entire patch for this.

Quoted 45 lines

+ * valid configuration data. (32 µs)
+ */

drivers/fpga/efinix-spi.c new file +43 -0
+#define EFINIX_TDMIN_US_MIN    35
+#define EFINIX_TDMIN_US_MAX    40
+
+/* tCRESET_N: Minimum CRESET_N low pulse width required to
+ * trigger re-configuration. (320 ns)
+ */
+#define EFINIX_TCRESETN_DELAY_MIN_US  1
+#define EFINIX_TCRESETN_DELAY_MAX_US  2
+
+/* tUSER: Minimum configuration duration after CDONE goes high
+ * before entering user mode. (25 µs)
+ */
+#define EFINIX_TUSER_US_MIN    30
+#define EFINIX_TUSER_US_MAX    35
+
+struct efinix_spi_conf {
+     struct spi_device *spi;
+     struct gpio_desc *cdone;
+     struct gpio_desc *reset;
+     bool bus_locked;
+};
+
+static void efinix_spi_reset(struct efinix_spi_conf *conf)
+{
+     gpiod_set_value(conf->reset, 1);
+     usleep_range(EFINIX_TCRESETN_DELAY_MIN_US, EFINIX_TCRESETN_DELAY_MAX_US);
+     gpiod_set_value(conf->reset, 0);
+     usleep_range(EFINIX_TDMIN_US_MIN, EFINIX_TDMIN_US_MAX);
+}
+
+static enum fpga_mgr_states efinix_spi_state(struct fpga_manager *mgr)
+{
+     struct efinix_spi_conf *conf = mgr->priv;
+
+     if (conf->cdone && gpiod_get_value(conf->cdone) == 1)
+             return FPGA_MGR_STATE_OPERATING;
+
+     return FPGA_MGR_STATE_UNKNOWN;
+}
+
+static int efinix_spi_write_init(struct fpga_manager *mgr,
+                              struct fpga_image_info *info,
+                              const char *buf, size_t count)
Quoted 8 lines

drivers/fpga/efinix-spi.c line 75

Quoted 4 lines
drivers/fpga/efinix-spi.c new file +4 -0
+{
+     struct efinix_spi_conf *conf = mgr->priv;
+     struct spi_transfer assert_cs = {
+             .cs_change = 1,

You've addressed an important concern, please briefly describe in this function & changelog: what's the HW requirement, how to solve it by cs_change & spi_bus_lock().

OK. changelog means description in the commit message?

Quoted 38 lines
Quoted 38 lines
drivers/fpga/efinix-spi.c new file +37 -0
+     };
+     struct spi_message message;
+     int ret;
+
+     if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
+             dev_err(&mgr->dev, "Partial reconfiguration not supported\n");
+             return -EOPNOTSUPP;
+     }
+
+     spi_bus_lock(conf->spi->controller);
+     conf->bus_locked = true;
+     spi_message_init_with_transfers(&message, &assert_cs, 1);
+     ret = spi_sync_locked(conf->spi, &message);
+     if (ret) {
+             spi_bus_unlock(conf->spi->controller);
+             conf->bus_locked = false;
+             return ret;
+     }
+
+     /* Reset with CS asserted */
+     efinix_spi_reset(conf);
+
+     return 0;
+}
+
+static int efinix_spi_write(struct fpga_manager *mgr, const char *buf,
+                         size_t count)
+{
+     struct spi_transfer write_xfer = {
+             .tx_buf = buf,
+             .len = count,
+             .cs_change = 1, /* Keep CS asserted */
+     };
+     struct efinix_spi_conf *conf = mgr->priv;
+     struct spi_message message;
+     int ret;
+
Quoted 8 lines

drivers/fpga/efinix-spi.c line 117

Quoted 5 lines
drivers/fpga/efinix-spi.c new file +5 -0
+     spi_message_init_with_transfers(&message, &write_xfer, 1);
+     ret = spi_sync_locked(conf->spi, &message);
+     if (ret) {
+             dev_err(&mgr->dev, "SPI error in firmware write: %d\n", ret);
+             if (conf->bus_locked) {

Do we really need this flag? If we failed to lock bus on write_init(), we can't get here, is it?

Its actually not needed, it's just extra protection in case the API is misused, eg write_complete() is executed even if write() failed. But since this is not the case, I will remove it.

Quoted 17 lines
Quoted 5 lines
drivers/fpga/efinix-spi.c new file +4 -0
+                     spi_bus_unlock(conf->spi->controller);
+                     conf->bus_locked = false;
+             }
+     }

drivers/fpga/efinix-spi.c line 131

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+     return ret;
+}
+
+static int efinix_spi_write_complete(struct fpga_manager *mgr,
+                                  struct fpga_image_info *info)
+{
+     unsigned long timeout =
+             jiffies + usecs_to_jiffies(info->config_complete_timeout_us);
+     struct spi_transfer clk_cycles = {
+             .len = EFINIX_SPI_IDLE_CYCLES_BYTES,

Here we also release the cs, is it? Please add comments here.

Exactly, I think I should also add .cs_change state to explicitly release it.

Quoted 8 lines

drivers/fpga/efinix-spi.c line 135

Quoted 5 lines
drivers/fpga/efinix-spi.c new file +4 -0
+     };
+     struct efinix_spi_conf *conf = mgr->priv;
+     struct spi_message message;
+     int ret = -1, done = 0;

Why initialize these 2 variables, IIUC they are all re-assigned before any usage.

will fix

Quoted 97 lines
Quoted 76 lines
drivers/fpga/efinix-spi.c new file +75 -0
+     bool expired = false;
+     u8 *dummy_buf;
+
+     dummy_buf = kzalloc(EFINIX_SPI_IDLE_CYCLES_BYTES, GFP_KERNEL);
+     if (!dummy_buf) {
+             ret = -ENOMEM;
+             goto unlock_spi;
+     }
+
+     clk_cycles.tx_buf = dummy_buf;
+     spi_message_init_with_transfers(&message, &clk_cycles, 1);
+     ret = spi_sync_locked(conf->spi, &message);
+     if (ret) {
+             dev_err(&mgr->dev, "SPI error in write complete: %d\n", ret);
+             goto free_buf;
+     }
+
+     if (conf->cdone) {
+             while (!expired) {
+                     done = gpiod_get_value(conf->cdone);
+                     if (done < 0) {
+                             ret = done;
+                             goto free_buf;
+                     }
+                     if (done)
+                             break;
+
+                     usleep_range(10, 20);
+                     expired = time_after(jiffies, timeout);
+             }
+
+             if (expired) {
+                     dev_err(&mgr->dev, "Timeout waiting for CDONE\n");
+                     ret = -ETIMEDOUT;
+                     goto free_buf;
+             }
+     }
+
+     usleep_range(EFINIX_TUSER_US_MIN, EFINIX_TUSER_US_MAX);
+
+free_buf:
+     kfree(dummy_buf);
+unlock_spi:
+     if (conf->bus_locked) {
+             spi_bus_unlock(conf->spi->controller);
+             conf->bus_locked = false;
+     }
+     return ret;
+}
+
+static const struct fpga_manager_ops efinix_spi_ops = {
+     .state = efinix_spi_state,
+     .write_init = efinix_spi_write_init,
+     .write = efinix_spi_write,
+     .write_complete = efinix_spi_write_complete,
+};
+
+static int efinix_spi_probe(struct spi_device *spi)
+{
+     struct efinix_spi_conf *conf;
+     struct fpga_manager *mgr;
+
+     conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
+     if (!conf)
+             return -ENOMEM;
+
+     conf->spi = spi;
+
+     conf->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
+     if (IS_ERR(conf->reset))
+             return dev_err_probe(&spi->dev, PTR_ERR(conf->reset),
+                                  "Failed to get RESET gpio\n");
+
+     if (!(spi->mode & SPI_CPHA) || !(spi->mode & SPI_CPOL))
+             return dev_err_probe(&spi->dev, -EINVAL,

drivers/fpga/efinix-spi.c line 220

Quoted 10 lines
drivers/fpga/efinix-spi.c new file +10 -0
+                                  "Unsupported SPI mode, set CPHA and CPOL\n");
+
+     conf->cdone = devm_gpiod_get_optional(&spi->dev, "cdone", GPIOD_IN);
+     if (IS_ERR(conf->cdone))
+             return dev_err_probe(&spi->dev, PTR_ERR(conf->cdone),
+                                  "Failed to get CDONE gpio\n");
+
+     mgr = devm_fpga_mgr_register(&spi->dev,
+                                  "Efinix FPGA Manager",
+                                  &efinix_spi_ops, conf);

The initialization sequence is quite random...

1. if (!(spi->mode & SPI_CPHA) || !(spi->mode & SPI_CPOL))
2. conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
3. conf->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_HIGH);
4. conf->cdone = devm_gpiod_get_optional(&spi->dev, "cdone", GPIOD_IN);
5. Initialize other fields in conf.
6. mgr = devm_fpga_mgr_register()
7. return PTR_ERR_OR_ZERO(mgr);

Quoted 1 line

Is it better?

I agree, I will reorder the initialization!

Regards,
Ian