* [PATCH 1/6] dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:05 ` [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver Prakriti Sharma
` (4 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
From: Siddhant Kumar <sidkumar@synopsys.com>
Add a binding for the Synopsys DesignWare Synchronous Serial Interface
(DWC_ssi v2.02a). The controller has a per-chip-select register map
and dedicated chip-select control.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
.../bindings/spi/snps,dwc-ssi-2.02a.yaml | 144 ++++++++++++++++++
MAINTAINERS | 7 +
2 files changed, 151 insertions(+)
create mode 100644 Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
diff --git a/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
new file mode 100644
index 000000000000..7cccd627f809
--- /dev/null
+++ b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
@@ -0,0 +1,144 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/spi/snps,dwc-ssi-2.02a.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Synopsys DesignWare SSI Gen2 SPI Controller
+
+maintainers:
+ - Siddhant Kumar <sidkumar@synopsys.com>
+ - Prakriti Sharma <prakriti@synopsys.com>
+
+description: |
+ The Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a) is an enhanced SPI
+ controller featuring per-chip-select register banks, dedicated CS control,
+ and support for Standard/Dual/Quad/Octal SPI modes with DDR capability.
+
+allOf:
+ - $ref: spi-controller.yaml#
+
+properties:
+ compatible:
+ const: snps,dwc-ssi-2.02a
+
+ reg:
+ maxItems: 1
+
+ interrupts:
+ description: |
+ The DWC SSI Gen2 IP exposes per-source interrupt outputs alongside a
+ combined ssi_intr. SoC integrators may route either the combined line,
+ the individual lines, or any subset. Provide either a single combined
+ IRQ, or one entry per declared name in interrupt-names (matching order).
+ minItems: 1
+ maxItems: 4
+
+ interrupt-names:
+ description: |
+ Optional. Naming for each physical IRQ line when the SoC breaks the
+ controller's interrupt output into multiple wires. Each name is gated
+ by its corresponding ISR bit in the controller:
+ "rxf" - RX FIFO threshold (SSI_RXF_INTR, ISR.RXFI)
+ "txe" - TX FIFO threshold (SSI_TXE_INTR, ISR.TXEI)
+ "txaf" - TX FIFO almost full (ssi_txaf_intr, ISR.TXAFI)
+ "wsf" - Write status fail (spi_wr_status_fail_intr, ISR.SPIWSFI)
+ Omit this property if the SoC combines all sources onto one wire.
+ minItems: 1
+ maxItems: 4
+ items:
+ enum: [rxf, txe, txaf, wsf]
+
+ clocks:
+ minItems: 1
+ items:
+ - description: SSI controller reference clock
+ - description: APB interface clock
+
+ clock-names:
+ minItems: 1
+ items:
+ - const: ssi_clk
+ - const: pclk
+
+ resets:
+ maxItems: 1
+
+ num-cs:
+ minimum: 1
+ maximum: 4
+ description:
+ Number of chip selects supported by the controller.
+
+ dmas:
+ items:
+ - description: TX DMA Channel
+ - description: RX DMA Channel
+
+ dma-names:
+ items:
+ - const: tx
+ - const: rx
+
+ rx-sample-delay-ns:
+ default: 0
+ description: |
+ Default value of the rx-sample-delay-ns property.
+ This value will be used if the property is not explicitly defined
+ for a SPI slave device.
+
+ SPI Rx sample delay offset, unit is nanoseconds.
+ The delay from the default sample time before the actual sample of the
+ rxd input signal occurs. The "rx_sample_delay" is an optional feature
+ of the designware controller, and the upper limit is also subject to
+ controller configuration.
+
+patternProperties:
+ "^.*@[0-9a-f]+$":
+ type: object
+ $ref: spi-peripheral-props.yaml
+ additionalProperties: true
+
+ properties:
+ reg:
+ minimum: 0
+ maximum: 3
+
+ rx-sample-delay-ns:
+ description:
+ Per-device Rx sample delay override, in nanoseconds.
+
+unevaluatedProperties: false
+
+required:
+ - compatible
+ - reg
+ - "#address-cells"
+ - "#size-cells"
+ - clocks
+
+examples:
+ - |
+ spi@20100000 {
+ compatible = "snps,dwc-ssi-2.02a";
+ reg = <0x20100000 0x1000>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+ interrupts = <0 42 4>;
+ clocks = <&spi_clk>;
+ clock-names = "ssi_clk";
+ num-cs = <2>;
+ dmas = <&dma0 10>, <&dma0 11>;
+ dma-names = "tx", "rx";
+ rx-sample-delay-ns = <3>;
+
+ flash@0 {
+ compatible = "jedec,spi-nor";
+ reg = <0>;
+ spi-max-frequency = <50000000>;
+ spi-rx-bus-width = <4>;
+ spi-tx-bus-width = <4>;
+ rx-sample-delay-ns = <7>;
+ };
+ };
+...
diff --git a/MAINTAINERS b/MAINTAINERS
index ce1b9e514e08..e68a4337a651 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -27304,6 +27304,13 @@ S: Supported
F: Documentation/admin-guide/perf/dwc_pcie_pmu.rst
F: drivers/perf/dwc_pcie_pmu.c
+SYNOPSYS DESIGNWARE SSI GEN2 SPI DRIVER
+M: Prakriti Sharma <prakriti@synopsys.com>
+M: Siddhant Kumar <sidkumar@synopsys.com>
+L: linux-spi@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
+
SYNOPSYS HSDK RESET CONTROLLER DRIVER
M: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
S: Supported
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
2026-11-19 10:05 ` [PATCH 1/6] dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:28 ` sashiko-bot
2026-11-19 10:05 ` [PATCH 3/6] spi: dw-g2: Add indirect read and write Prakriti Sharma
` (3 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
Add a driver for the Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a)
controller. Support probe, the MMIO glue, command-only transfers, and
short register operations.
Transfers are polled. The IRQ is claimed, including the optional
per-source rxf, txe, txaf, and wsf lines.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
MAINTAINERS | 1 +
drivers/spi/Kconfig | 21 +
drivers/spi/Makefile | 3 +
drivers/spi/spi-dw-g2-core.c | 896 +++++++++++++++++++++++++++++++++++
drivers/spi/spi-dw-g2-mmio.c | 196 ++++++++
drivers/spi/spi-dw-g2.h | 396 ++++++++++++++++
6 files changed, 1513 insertions(+)
create mode 100644 drivers/spi/spi-dw-g2-core.c
create mode 100644 drivers/spi/spi-dw-g2-mmio.c
create mode 100644 drivers/spi/spi-dw-g2.h
diff --git a/MAINTAINERS b/MAINTAINERS
index e68a4337a651..e0f97d2792b1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -27310,6 +27310,7 @@ M: Siddhant Kumar <sidkumar@synopsys.com>
L: linux-spi@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
+F: drivers/spi/spi-dw-g2-*.[ch]
SYNOPSYS HSDK RESET CONTROLLER DRIVER
M: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index a7b343d124a9..3c2a7ea543fd 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -381,6 +381,27 @@ config SPI_DESIGNWARE
if SPI_DESIGNWARE
+config SPI_DW_SSI_G2
+ tristate "Synopsys Designware SSI Gen2 controller"
+ depends on SPI_DESIGNWARE
+ help
+ Synopsys DesignWare SSI Gen2 is a SPI controller that supports
+ standard, dual, quad, and octal SPI modes. It provides high
+ performance SPI communication using the spi-mem interface.
+ Enable this option if you have a device with a DW SSI Gen2
+ controller and want to access SPI flash memories.
+
+config SPI_DW_SSI_G2_MMIO
+ tristate "Memory-mapped io interface driver for DW SSI Gen2 core"
+ depends on HAS_IOMEM
+ depends on SPI_DW_SSI_G2
+ help
+ Memory-mapped I/O interface driver for the Synopsys DesignWare
+ SSI Gen2 SPI controller. Enable this for platforms that access
+ the controller registers via memory-mapped I/O. This is the
+ most common access method for SoC-integrated instances of the
+ DW SSI Gen2 controller.
+
config SPI_DW_DMA
bool "DMA support for DW SPI controller"
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index b6ab37271555..f9c86827ebc2 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -57,6 +57,9 @@ spi-dw-y := spi-dw-core.o
spi-dw-$(CONFIG_SPI_DW_DMA) += spi-dw-dma.o
obj-$(CONFIG_SPI_DW_MMIO) += spi-dw-mmio.o
obj-$(CONFIG_SPI_DW_PCI) += spi-dw-pci.o
+obj-$(CONFIG_SPI_DW_SSI_G2) += spi-dw-g2.o
+spi-dw-g2-y := spi-dw-g2-core.o
+obj-$(CONFIG_SPI_DW_SSI_G2_MMIO) += spi-dw-g2-mmio.o
obj-$(CONFIG_SPI_EP93XX) += spi-ep93xx.o
obj-$(CONFIG_SPI_FALCON) += spi-falcon.o
obj-$(CONFIG_SPI_FSI) += spi-fsi.o
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
new file mode 100644
index 000000000000..7b0928f67103
--- /dev/null
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -0,0 +1,896 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/of.h>
+#include <linux/delay.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi-mem.h>
+
+#include "spi-dw-g2.h"
+
+/*
+ * ========================================================================
+ * DebugFS support - exposes controller registers for debugging
+ * ========================================================================
+ */
+
+#ifdef CONFIG_DEBUG_FS
+#define DW_SPI_G2_DBG_REG(_name, _off) \
+ { .name = _name, .offset = _off }
+
+static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = {
+ DW_SPI_G2_DBG_REG("SSIENR", DW_SPI_G2_SSIENR),
+ DW_SPI_G2_DBG_REG("SLAVE_EN", DW_SPI_G2_SER),
+ DW_SPI_G2_DBG_REG("CTRLR0_CS1", DW_SPI_G2_CTRLR0(1)),
+ DW_SPI_G2_DBG_REG("CTRLR1_CS1", DW_SPI_G2_CTRLR1(1)),
+ DW_SPI_G2_DBG_REG("SPI_CTRLR0_CS1", DW_SPI_G2_SPI_CTRLR0(1)),
+ DW_SPI_G2_DBG_REG("SPI_CTRLR1_CS1", DW_SPI_G2_SPI_CTRLR1(1)),
+ DW_SPI_G2_DBG_REG("BAUDR1", DW_SPI_G2_BAUDR(1)),
+ DW_SPI_G2_DBG_REG("TXFTLR", DW_SPI_G2_TXFTLR),
+ DW_SPI_G2_DBG_REG("RXFTLR", DW_SPI_G2_RXFTLR),
+ DW_SPI_G2_DBG_REG("TXFLR", DW_SPI_G2_TXFLR),
+ DW_SPI_G2_DBG_REG("RXFLR", DW_SPI_G2_RXFLR),
+ DW_SPI_G2_DBG_REG("SR", DW_SPI_G2_SR),
+ DW_SPI_G2_DBG_REG("IMR", DW_SPI_G2_IMR),
+ DW_SPI_G2_DBG_REG("ISR", DW_SPI_G2_ISR),
+ DW_SPI_G2_DBG_REG("DEDICATED_CS", DW_SPI_G2_DEDICATED_CS_EN),
+};
+
+/**
+ * dw_spi_g2_debugfs_init - Create debugfs entries for register inspection
+ * @dws: Driver state
+ *
+ * Creates a debugfs directory at /sys/kernel/debug/dw_spi_g2.<bus_num>/
+ * with a "registers" file showing current controller register values.
+ * Useful for debugging transfer issues or verifying configuration.
+ */
+static void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws)
+{
+ char name[32];
+
+ snprintf(name, sizeof(name), "dw_spi_g2.%d", dws->ctlr->bus_num);
+
+ dws->debugfs = debugfs_create_dir(name, NULL);
+ if (!dws->debugfs)
+ return;
+
+ dws->regset.base = dws->regs;
+ dws->regset.regs = dw_spi_g2_dbg_regs;
+ dws->regset.nregs = ARRAY_SIZE(dw_spi_g2_dbg_regs);
+
+ debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
+}
+
+/**
+ * dw_spi_g2_debugfs_remove - Remove debugfs entries
+ * @dws: Driver state
+ */
+static void dw_spi_g2_debugfs_remove(struct dw_spi_g2 *dws)
+{
+ debugfs_remove_recursive(dws->debugfs);
+ dws->debugfs = NULL;
+}
+#else
+static inline void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws)
+{
+}
+
+static inline void dw_spi_g2_debugfs_remove(struct dw_spi_g2 *dws)
+{
+}
+#endif
+
+/*
+ * ========================================================================
+ * SPI controller setup and initialization
+ * ========================================================================
+ */
+
+/**
+ * dw_spi_g2_setup - Configure baud rate for a specific SPI device
+ * @spi: SPI device being configured
+ *
+ * Called when a SPI device is added or its parameters change. Calculates
+ * and programs the baud rate divider for the device's requested speed.
+ *
+ * The baud rate register (BAUDR) uses an even divisor:
+ * actual_speed = ssi_clk / BAUDR
+ * BAUDR = 1 is a special case (passthrough, no division)
+ * BAUDR must be even for values > 1
+ *
+ * Per-CS registers are written with the CS register block disabled
+ * (DEDICATED_CS_EN bit cleared) to ensure the write takes effect.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int dw_spi_g2_setup(struct spi_device *spi)
+{
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+ u32 baud, speed, div, min_speed;
+
+ /* Get primary chip select (index 0 = first CS of this device) */
+ dws->cs = spi_get_chipselect(spi, 0);
+
+ /*
+ * Calculate minimum achievable speed: slowest speed occurs at
+ * maximum baud divisor (clk_rate / MAX_BAUDR).
+ */
+ min_speed = DIV_ROUND_UP(dws->clk_rate, DW_SPI_G2_MAX_BAUDR);
+
+ /* Determine target speed (capped at controller maximum) */
+ speed = spi->max_speed_hz;
+ if (!speed || speed > dws->max_freq)
+ speed = dws->max_freq;
+
+ /*
+ * Clamp speed to achievable range. If requested speed is below
+ * hardware minimum, clamp to min_speed
+ */
+ if (speed < min_speed) {
+ dev_warn(&spi->dev, "speed %u Hz below minimum %u Hz, clamping\n",
+ speed, min_speed);
+ speed = min_speed;
+ }
+
+ /*
+ * Calculate baud rate divisor (must be even, except for 1).
+ * Since speed >= min_speed, div is guaranteed <= MAX_BAUDR.
+ */
+ div = DIV_ROUND_UP(dws->clk_rate, speed);
+ if (div <= 1) {
+ baud = 1; /* Passthrough mode - no division */
+ } else {
+ if (div & 1)
+ div++; /* Round up to next even number */
+ baud = div;
+ }
+
+ /* Write baud rate to per-CS register (with CS block disabled) */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
+
+ dev_dbg(&spi->dev,
+ "setup: CS=%u mode=%u bpw=%u speed=%u baud=%u\n",
+ dws->cs, spi->mode, spi->bits_per_word, speed, baud);
+
+ return 0;
+}
+
+static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
+{
+ int drain = 0;
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+ /* Clear all interrupts by writing 1s to defined ICR bits */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ /* Drain any stale RX data left from previous transfers */
+ while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
+ drain < dws->fifo_len) {
+ (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
+ drain++;
+ }
+}
+
+/**
+ * dw_spi_g2_build_ctrlr0 - Build CTRLR0 register value
+ * @dws: Driver state
+ * @tmod: Transfer mode (TR=0, TO=1, RO=2, EEPROM=3)
+ * @frf: Frame format (Standard=0, Dual=1, Quad=2, Octal=3)
+ *
+ * Constructs the CTRLR0 register value with transfer mode, frame format,
+ * data frame size, and SPI mode (CPOL/CPHA) settings.
+ *
+ * Return: Constructed CTRLR0 register value
+ */
+static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
+{
+ u32 ctrlr0 = 0;
+ u8 dfs;
+
+ if (!dws->curr_spi) {
+ dev_err(dws->dev, "curr_spi not set\n");
+ return 0;
+ }
+
+ dfs = dws->curr_spi->bits_per_word ? dws->curr_spi->bits_per_word : 8;
+
+ /*
+ * Validate data frame size (DFS).
+ * Hardware supports 8, 16, and 32-bit transfers (DFS_ENCODE values 7, 15, 31).
+ * Current spi-mem implementation uses 8-bit only (per Linux framework).
+ * For 16/32-bit support via legacy transfer_one path, additional
+ * FIFO handling logic would be required.
+ */
+ if (dfs != (DW_SPI_G2_DFS_8BIT + 1) &&
+ dfs != (DW_SPI_G2_DFS_16BIT + 1) &&
+ dfs != (DW_SPI_G2_DFS_32BIT + 1)) {
+ dev_err(dws->dev, "Unsupported DFS: %u (must be 8, 16, or 32)\n",
+ dfs);
+ return 0;
+ }
+
+ /* Validate FRF compatibility with DFS */
+ if (frf != DW_SPI_G2_FRF_STANDARD && dfs != (DW_SPI_G2_DFS_8BIT + 1)) {
+ dev_err(dws->dev,
+ "Multi-line SPI modes require 8-bit transfers (dfs=%u frf=%u)\n",
+ dfs, frf);
+ return 0;
+ }
+
+ ctrlr0 |=
+ FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, DW_SPI_G2_DFS_ENCODE(dfs));
+ ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_TMOD_MASK, tmod);
+ ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_FRF_MASK, frf);
+
+ return ctrlr0;
+}
+
+/**
+ * dw_spi_g2_get_frf - Determine frame format from operation bus widths
+ * @op: SPI memory operation
+ *
+ * Examines the command, address, and data bus widths to determine the
+ * widest mode in use, which defines the frame format.
+ *
+ * Return: Frame format
+ */
+static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)
+{
+ /* Use the widest bus mode across cmd/addr/data phases */
+ u32 width = max3(op->cmd.buswidth,
+ op->addr.buswidth ? op->addr.buswidth : 1,
+ op->data.buswidth ? op->data.buswidth : 1);
+
+ switch (width) {
+ case 2:
+ return DW_SPI_G2_FRF_DUAL;
+ case 4:
+ return DW_SPI_G2_FRF_QUAD;
+ case 8:
+ return DW_SPI_G2_FRF_OCTAL;
+ default:
+ return DW_SPI_G2_FRF_STANDARD;
+ }
+}
+
+/**
+ * dw_spi_g2_get_tt - Determine transfer type from operation bus widths
+ * @op: SPI memory operation
+ *
+ * The transfer type (TT) field in SPI_CTRLR0 defines which phases use
+ * multi-line mode. TT0 = standard (1-1-1), TT1 = data multi-line (1-1-x),
+ * TT2 = addr+data multi-line (1-x-x). TT3 (x-x-x) is not supported.
+ *
+ * Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns
+ */
+static int dw_spi_g2_get_tt(const struct spi_mem_op *op)
+{
+ bool cmd_m = (op->cmd.buswidth > 1);
+ bool addr_m = (op->addr.nbytes && op->addr.buswidth > 1);
+ bool data_m = (op->data.nbytes && op->data.buswidth > 1);
+
+ if (!cmd_m && !addr_m && !data_m)
+ return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-1 */
+ if (!cmd_m && !addr_m && data_m)
+ return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-1-x */
+ if (!cmd_m && addr_m && data_m)
+ return DW_SPI_G2_TRANS_TYPE_TT2; /* 1-x-x */
+
+ return -EOPNOTSUPP;
+}
+
+/**
+ * dw_spi_g2_is_reg_op - Check if operation is a register read/write
+ * @op: SPI memory operation
+ *
+ * Register operations are single-byte commands with no address (e.g.,
+ * RDSR, WRSR, RDID). Data must fit within the fixed-size stack buffer.
+ */
+static inline bool dw_spi_g2_is_reg_op(const struct spi_mem_op *op)
+{
+ return !op->addr.nbytes && op->data.nbytes > 0 &&
+ op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
+}
+
+/**
+ * dw_spi_g2_xfer_fifo - Low-level FIFO transfer using polling
+ * @dws: Driver state
+ * @tx_buf: TX buffer (or NULL if no TX)
+ * @tx_len: Number of bytes to transmit
+ * @rx_buf: RX buffer (or NULL if no RX expected)
+ * @rx_len: Number of bytes to receive
+ *
+ * Performs a polled FIFO transfer. Writes tx_len bytes from tx_buf to DR0,
+ * checking SR.TFNF (TX FIFO not full) before each write. For RX, waits for
+ * RXFLR to indicate data availability, then reads rx_len bytes from DR0.
+ *
+ * Return: 0 on success, -ETIMEDOUT on timeout
+ */
+static int dw_spi_g2_xfer_fifo(struct dw_spi_g2 *dws,
+ const u8 *tx_buf, u32 tx_len,
+ u8 *rx_buf, u32 rx_len)
+{
+ u32 tx_sent = 0;
+ u32 rx_got = 0;
+ u32 sr;
+ unsigned long timeout;
+
+ /* TX phase: push bytes checking TFNF */
+ timeout = jiffies + msecs_to_jiffies(1000);
+ while (tx_sent < tx_len) {
+ sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+ if (sr & DW_SPI_G2_SR_TFNF) {
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
+ tx_buf[tx_sent]);
+ tx_sent++;
+ timeout = jiffies + msecs_to_jiffies(1000);
+ } else if (time_after(jiffies, timeout)) {
+ dev_err(dws->dev,
+ "xfer_fifo: TX timeout after %u/%u bytes (SR=0x%08x)\n",
+ tx_sent, tx_len, sr);
+ return -ETIMEDOUT;
+ }
+ cpu_relax();
+ }
+
+ if (!rx_buf || !rx_len) {
+ dw_spi_g2_wait_idle(dws);
+ return 0;
+ }
+
+ /* RX phase */
+ timeout = jiffies + msecs_to_jiffies(1000);
+ while (rx_got < rx_len) {
+ sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+ if (sr & DW_SPI_G2_SR_RFNE) {
+ rx_buf[rx_got++] = dw_spi_g2_readl(dws->regs,
+ DW_SPI_G2_DR0) &
+ 0xff;
+ timeout = jiffies + msecs_to_jiffies(1000);
+ } else if (time_after(jiffies, timeout)) {
+ dev_err(dws->dev,
+ "xfer_fifo: RX timeout after %u/%u bytes (SR=0x%08x RXFLR=%u)\n",
+ rx_got, rx_len,
+ dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR),
+ dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR));
+ return -ETIMEDOUT;
+ }
+ cpu_relax();
+ }
+
+ dw_spi_g2_wait_idle(dws);
+ return 0;
+}
+
+/**
+ * dw_spi_g2_fill_tx_buf - Serialize command/address/data into TX buffer
+ * @buf: Destination buffer
+ * @op: SPI memory operation
+ * @include_data: true to include data bytes, false for command+address only
+ *
+ * Builds a byte stream from a spi_mem_op structure:
+ * [opcode bytes MSB..LSB] [address bytes MSB..LSB] [data bytes (optional)].
+ *
+ * Return: Total number of bytes written to buf
+ */
+static u32 dw_spi_g2_fill_tx_buf(u8 *buf, const struct spi_mem_op *op,
+ bool include_data)
+{
+ u32 pos = 0;
+
+ /* Serialize multi-byte opcode (MSB first) */
+ for (u32 i = 0; i < op->cmd.nbytes; i++)
+ buf[pos++] =
+ (op->cmd.opcode >> (8 * (op->cmd.nbytes - 1 - i))) & 0xff;
+
+ /* Serialize address (MSB first) */
+ for (u32 i = 0; i < op->addr.nbytes; i++)
+ buf[pos++] =
+ (op->addr.val >> (8 * (op->addr.nbytes - 1 - i))) & 0xff;
+
+ /* Optionally append data bytes for write operations */
+ if (include_data && op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) {
+ memcpy(&buf[pos], op->data.buf.out, op->data.nbytes);
+ pos += op->data.nbytes;
+ }
+
+ return pos;
+}
+
+/**
+ * Command-only operations (WREN, WRDI, Chip Erase, Sector Erase, etc.)
+ * TMOD=1 (TX-Only) — Send opcode + optional address, no data phase
+ */
+
+/**
+ * dw_spi_g2_cmd_only_setup - Configure controller for command-only operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Configures TMOD=TO (transmit-only), standard SPI mode..
+ */
+static void dw_spi_g2_cmd_only_setup(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TO,
+ DW_SPI_G2_FRF_STANDARD);
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
+}
+
+/**
+ * dw_spi_g2_cmd_only - Execute command-only operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Sends a command with optional address but no data payload.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_cmd_only(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u8 buf[8];
+ u32 len;
+ int ret;
+
+ dw_spi_g2_cmd_only_setup(dws, op);
+
+ len = dw_spi_g2_fill_tx_buf(buf, op, false);
+
+ dw_spi_g2_transfer_begin(dws);
+ ret = dw_spi_g2_xfer_fifo(dws, buf, len, NULL, 0);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ return ret;
+}
+
+static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u8 tmod;
+ u32 ctrlr0;
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ if (op->data.dir == SPI_MEM_DATA_IN) {
+ tmod = DW_SPI_G2_CTRLR0_TMOD_TR;
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ } else {
+ tmod = DW_SPI_G2_CTRLR0_TMOD_TO;
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ }
+
+ ctrlr0 = dw_spi_g2_build_ctrlr0(dws, tmod, DW_SPI_G2_FRF_STANDARD);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
+}
+
+static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
+{
+ u8 tx_buf[DW_SPI_G2_REG_OP_MAX_DATA + 1];
+ u32 tx_len;
+ int ret;
+
+ /* Validate data length fits in stack buffer */
+ if (op->data.nbytes > DW_SPI_G2_REG_OP_MAX_DATA) {
+ dev_err(dws->dev, "reg_op data length %u exceeds max %u\n",
+ op->data.nbytes, DW_SPI_G2_REG_OP_MAX_DATA);
+ return -EINVAL;
+ }
+
+ dw_spi_g2_reg_op_setup(dws, op);
+
+ dw_spi_g2_transfer_begin(dws);
+
+ if (op->data.dir == SPI_MEM_DATA_OUT) {
+ if (!op->data.buf.out) {
+ dev_err(dws->dev, "TX buffer is NULL\n");
+ return -EINVAL;
+ }
+ /* TX: [opcode] [data bytes] */
+ tx_buf[0] = op->cmd.opcode;
+ memcpy(&tx_buf[1], op->data.buf.out, op->data.nbytes);
+ tx_len = 1 + op->data.nbytes;
+ ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_len, NULL, 0);
+ } else {
+ if (!op->data.buf.in) {
+ dev_err(dws->dev, "RX buffer is NULL\n");
+ return -EINVAL;
+ }
+ /*
+ * TR mode: each TX byte produces one RX byte.
+ * Push opcode + nbytes dummy 0x00s, receive 1+nbytes,
+ * discard first RX byte (echo of opcode phase).
+ */
+ u8 rx_tmp[DW_SPI_G2_REG_OP_MAX_DATA + 1];
+
+ tx_buf[0] = op->cmd.opcode;
+ memset(&tx_buf[1], 0x00, op->data.nbytes);
+ tx_len = 1 + op->data.nbytes;
+ ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_len, rx_tmp, tx_len);
+ if (!ret)
+ memcpy(op->data.buf.in, &rx_tmp[1], op->data.nbytes);
+ }
+
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+
+ return ret;
+}
+
+/**
+ * Indirect read operations
+ */
+
+/*
+ * ========================================================================
+ * SPI-MEM interface
+ * ========================================================================
+ */
+
+static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
+ const struct spi_mem_op *op)
+{
+ return spi_mem_default_supports_op(mem, op);
+}
+
+int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
+{
+ if (op->data.dir == SPI_MEM_DATA_IN)
+ op->data.nbytes = min_t(u32, op->data.nbytes, DW_G2_MAX_CHUNK);
+ else if (op->data.dir == SPI_MEM_DATA_OUT)
+ op->data.nbytes = min_t(u32, op->data.nbytes, DW_G2_MAX_CHUNK);
+
+ return 0;
+}
+
+static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
+ const struct spi_mem_op *op)
+{
+ struct spi_device *spi = mem->spi;
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+
+ /* Validate operation structure */
+ if (!op || !op->cmd.nbytes) {
+ dev_err(dws->dev, "Invalid operation: missing command\n");
+ return -EINVAL;
+ }
+
+ /* Validate command length */
+ if (op->cmd.nbytes > 2) {
+ dev_err(dws->dev, "command length %u exceeds maximum 2 bytes\n",
+ op->cmd.nbytes);
+ return -EINVAL;
+ }
+
+ /* Validate address length */
+ if (op->addr.nbytes &&
+ op->addr.nbytes * 8U > DW_SPI_G2_ADDR_L_MAX_BITS) {
+ dev_err(dws->dev, "address length %u bytes (%u bits) exceeds max %u bits\n",
+ op->addr.nbytes, op->addr.nbytes * 8U, DW_SPI_G2_ADDR_L_MAX_BITS);
+ return -EINVAL;
+ }
+
+ /* Validate dummy cycles */
+ if (op->dummy.nbytes) {
+ u32 dummy_cycles;
+
+ if (!op->dummy.buswidth || op->dummy.buswidth > 8) {
+ dev_err(dws->dev, "invalid dummy buswidth %u\n",
+ op->dummy.buswidth);
+ return -EINVAL;
+ }
+ dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX) {
+ dev_err(dws->dev, "dummy cycles %u exceed maximum %u\n",
+ dummy_cycles, DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX);
+ return -EINVAL;
+ }
+ }
+
+ /* Validate data length against controller limits */
+ if (op->data.nbytes > DW_G2_MAX_CHUNK) {
+ dev_err(dws->dev, "data length %u exceeds maximum %u\n",
+ op->data.nbytes, DW_G2_MAX_CHUNK);
+ return -EINVAL;
+ }
+
+ /* Get primary chip select (index 0 = first CS of this device) */
+ dws->cs = spi_get_chipselect(spi, 0);
+ dws->curr_spi = spi;
+
+ dev_dbg(dws->dev,
+ "exec_mem_op: cmd=0x%02x addr_len=%u data_len=%u dir=%d\n",
+ op->cmd.opcode, op->addr.nbytes, op->data.nbytes,
+ op->data.dir);
+
+ if (op->data.dir == SPI_MEM_NO_DATA)
+ return dw_spi_g2_cmd_only(dws, op);
+
+ if (dw_spi_g2_is_reg_op(op))
+ return dw_spi_g2_reg_op(dws, op);
+
+ /*
+ * Bulk reads and writes are added by the indirect-transfer patch.
+ * This patch handles commands and short register operations only.
+ */
+ if (op->data.dir != SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op(op))
+ return -EOPNOTSUPP;
+
+ return -EOPNOTSUPP;
+}
+
+static const struct spi_controller_mem_ops dw_spi_g2_mem_ops = {
+ .supports_op = dw_spi_g2_supports_mem_ops,
+ .adjust_op_size = dw_spi_g2_adjust_mem_op_size,
+ .exec_op = dw_spi_g2_exec_mem_op,
+};
+
+/*
+ * FIFO depth is a synthesis-time parameter not exposed by any
+ * discoverable register and varies per instantiation (e.g. 256 on
+ * TDA54, 32 on the AM72 VDK). TXFTLR's field is only log2(fifo_depth)
+ * bits wide, so writing increasing values and reading back finds where
+ * the hardware clamps it - same probe spi-dw-core.c uses for this IP.
+ * Caller must have the controller disabled (SSIENR=0).
+ */
+static u32 dw_spi_g2_detect_fifo_len(struct dw_spi_g2 *dws)
+{
+ u32 fifo;
+
+ for (fifo = 1; fifo < DW_SPI_G2_FIFO_LEN; fifo++) {
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, fifo);
+ if (dw_spi_g2_readl(dws->regs, DW_SPI_G2_TXFTLR) != fifo)
+ break;
+ }
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+
+ return (fifo == 1) ? DW_SPI_G2_FIFO_LEN : fifo;
+}
+
+/**
+ * dw_spi_g2_init - Initialize the SPI controller
+ * @dws: Driver state
+ *
+ * Performs one-time controller initialization:
+ * - Disables SSI and clears chip selects
+ * - Masks all interrupts (driver uses polling only)
+ * - Sets FIFO depth and mode bits
+ * - Registers spi-mem operations
+ * - Creates debugfs entries
+ *
+ * Called once during probe. The controller is left in a disabled state;
+ * individual transfers will enable it as needed.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int dw_spi_g2_init(struct dw_spi_g2 *dws)
+{
+ struct spi_controller *ctlr = dws->ctlr;
+
+ dev_info(dws->dev, "DW SSI G2 core init\n");
+
+ /* Disable controller and reset to clean state */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN, 0);
+
+ /* Mask all interrupts — this driver uses polling only */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
+ /* Clear all interrupts by writing 1s to defined ICR bits */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ /* Hardware capabilities */
+ dws->fifo_len = dw_spi_g2_detect_fifo_len(dws);
+ dws->caps = 0;
+
+ if (!dws->max_freq)
+ dws->max_freq = DW_SPI_G2_DEF_MAX_FREQ;
+
+ /* supported SPI modes to the framework */
+ ctlr->mode_bits = SPI_CPOL | SPI_CPHA |
+ SPI_TX_DUAL | SPI_RX_DUAL |
+ SPI_TX_QUAD | SPI_RX_QUAD | SPI_TX_OCTAL | SPI_RX_OCTAL;
+
+ ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
+ ctlr->flags = 0;
+
+ ctlr->setup = dw_spi_g2_setup;
+ ctlr->mem_ops = &dw_spi_g2_mem_ops;
+
+ dw_spi_g2_debugfs_init(dws);
+ init_completion(&dws->mem_done);
+
+ dev_info(dws->dev, "Core Init done: fifo=%u max_freq=%u\n",
+ dws->fifo_len, dws->max_freq);
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(dw_spi_g2_init, "SPI_DW_G2_CORE");
+
+/**
+ * dw_spi_g2_deinit - Clean up controller resources
+ * @dws: Driver state
+ *
+ * Called during driver removal to clean up debugfs and other resources.
+ */
+void dw_spi_g2_deinit(struct dw_spi_g2 *dws)
+{
+ dw_spi_g2_debugfs_remove(dws);
+}
+EXPORT_SYMBOL_NS_GPL(dw_spi_g2_deinit, "SPI_DW_G2_CORE");
+
+/*
+ * ========================================================================
+ * Legacy SPI transfer_one interface (not actively used with spi-mem)
+ * ========================================================================
+ */
+
+/**
+ * dw_spi_g2_update_config - Update controller configuration for a transfer
+ * @dws: Driver state
+ * @spi: SPI device
+ * @cfg: Transfer configuration
+ *
+ * Programs CTRLR0, CTRLR1, and BAUDR for a transfer. Used by the legacy
+ * transfer_one path (not used when operating via spi-mem).
+ */
+void dw_spi_g2_update_config(struct dw_spi_g2 *dws, struct spi_device *spi,
+ struct dw_spi_g2_cfg *cfg)
+{
+ u32 ctrlr0 = 0;
+ u32 div;
+
+ ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, cfg->dfs - 1);
+ ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_TMOD_MASK, cfg->tmode);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+
+ if (cfg->tmode == DW_SPI_G2_CTRLR0_TMOD_RO)
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), cfg->ndf - 1);
+
+ div = DIV_ROUND_UP(dws->max_freq, cfg->freq);
+ if (div & 1)
+ div++;
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), div);
+}
+
+/**
+ * dw_spi_g2_transfer_one - Execute a single SPI transfer (legacy interface)
+ * @ctlr: SPI controller
+ * @spi: SPI device
+ * @xfer: Transfer to execute
+ *
+ * Implements the legacy transfer_one callback for the SPI framework.
+ * This path is NOT used when the controller operates via spi-mem
+ * (which is the primary mode for flash operations). Provided for
+ * compatibility with non-memory SPI devices.
+ *
+ * Uses polling-based full-duplex (TMOD=TR) transfers.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int dw_spi_g2_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
+ struct spi_transfer *xfer)
+{
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+ struct dw_spi_g2_cfg cfg = {
+ .tmode = DW_SPI_G2_CTRLR0_TMOD_TR,
+ .dfs = xfer->bits_per_word,
+ .freq = xfer->speed_hz,
+ };
+ u32 sr;
+
+ dws->tx = xfer->tx_buf;
+ dws->rx = xfer->rx_buf;
+ dws->tx_len = xfer->len;
+ dws->rx_len = xfer->len;
+ dws->n_bytes = 1;
+
+ /* Ensure writes to device reach memory before enable */
+ wmb();
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_update_config(dws, spi, &cfg);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, dws->fifo_len / 2);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, (dws->fifo_len / 2) - 1);
+
+ dw_spi_g2_enable(dws, true);
+
+ local_irq_disable();
+ /* Polling-based full-duplex transfer */
+ while (dws->tx_len || dws->rx_len) {
+ sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+
+ /* Transmit if FIFO not full and bytes remaining */
+ if (dws->tx_len && (sr & DW_SPI_G2_SR_TFNF)) {
+ u8 val = 0;
+
+ if (dws->tx) {
+ val = *(u8 *)dws->tx;
+ dws->tx += 1;
+ }
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, val);
+ dws->tx_len--;
+ }
+
+ /* Receive if FIFO not empty and bytes expected */
+ if (dws->rx_len && (sr & DW_SPI_G2_SR_RFNE)) {
+ u8 val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
+
+ if (dws->rx) {
+ *(u8 *)dws->rx = val;
+ dws->rx += 1;
+ }
+ dws->rx_len--;
+ }
+ cpu_relax();
+ }
+ local_irq_enable();
+
+ /* Wait for transfer completion */
+ if (dw_spi_g2_wait_idle(dws)) {
+ dev_err(dws->dev, "transfer_one: timeout waiting for idle\n");
+ dw_spi_g2_enable(dws, false);
+ return -ETIMEDOUT;
+ }
+
+ dw_spi_g2_enable(dws, false);
+
+ spi_finalize_current_transfer(ctlr);
+ return 0;
+}
+
+/**
+ * dw_spi_g2_irq - Minimal interrupt handler for error detection
+ * @irq: IRQ number
+ * @dev_id: Driver state
+ *
+ * This driver operates in polling mode with all interrupts masked (IMR=0).
+ * This handler should normally not be invoked. It exists only to catch
+ * unexpected error interrupts that may occur despite masking.
+ *
+ * Return: IRQ_HANDLED if interrupt was ours, IRQ_NONE otherwise
+ */
+irqreturn_t dw_spi_g2_irq(int irq, void *dev_id)
+{
+ struct dw_spi_g2 *dws = dev_id;
+ u32 isr;
+
+ isr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_ISR);
+ if (!isr)
+ return IRQ_NONE;
+
+ /* Check for error conditions */
+ if (isr & (DW_SPI_G2_ISR_RXOI | DW_SPI_G2_ISR_RXUI |
+ DW_SPI_G2_ISR_TXOI | DW_SPI_G2_ISR_TXUI)) {
+ dev_warn(dws->dev, "Error interrupt: ISR=0x%x\n", isr);
+ dws->error = -EIO;
+ }
+
+ /* Clear all interrupts by writing 1s to defined ICR bits */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ return IRQ_HANDLED;
+}
+EXPORT_SYMBOL_NS_GPL(dw_spi_g2_irq, "SPI_DW_G2_CORE");
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("DesignWare SPI Gen2 SSI Controller");
diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
new file mode 100644
index 000000000000..6ef8f926e0eb
--- /dev/null
+++ b/drivers/spi/spi-dw-g2-mmio.c
@@ -0,0 +1,196 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+
+#include <linux/clk.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/reset.h>
+#include <linux/spi/spi.h>
+#include <linux/interrupt.h>
+
+#include "spi-dw-g2.h"
+
+struct dw_spi_g2_mmio {
+ struct dw_spi_g2 dws;
+ struct clk *clk;
+ struct clk *pclk;
+ struct reset_control *rstc;
+};
+
+static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
+{
+ struct dw_spi_g2_mmio *dwsmmio;
+ struct dw_spi_g2 *dws;
+ struct resource *mem;
+ struct spi_controller *ctlr;
+ u32 num_cs;
+ int ret;
+
+ ctlr = spi_alloc_host(&pdev->dev, sizeof(*dwsmmio));
+ if (!ctlr)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, ctlr);
+
+ dwsmmio = spi_controller_get_devdata(ctlr);
+ dws = &dwsmmio->dws;
+
+ dws->dev = &pdev->dev;
+ dws->ctlr = ctlr;
+
+ /* MMIO */
+ mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ dws->regs = devm_ioremap_resource(&pdev->dev, mem);
+ if (IS_ERR(dws->regs)) {
+ ret = PTR_ERR(dws->regs);
+ goto err_put;
+ }
+
+ /*
+ * IRQ.
+ *
+ * The DWC SSI Gen2 IP exposes per-source interrupt outputs
+ * alongside a combined ssi_intr. SoC integrators may route
+ * either the combined line, the individual lines, or any
+ * subset. We accept both DT forms:
+ *
+ * - Named-line DT: one entry per output, with interrupt-names
+ * in {"rxf","txe","txaf","wsf"}. Each line is requested
+ * and bound to the shared dw_spi_g2_irq() decoder.
+ * - Single-line DT: combined ssi_intr at index 0, used by
+ * integrations that OR the sources in HW.
+ *
+ * Either way dws->irq holds the "primary" line (rxf when
+ * named, index 0 otherwise) so debugfs and PM paths stay
+ * source-agnostic.
+ */
+ dws->irq = platform_get_irq_byname_optional(pdev, "rxf");
+ if (dws->irq < 0)
+ dws->irq = platform_get_irq(pdev, 0);
+ if (dws->irq < 0) {
+ ret = dws->irq;
+ goto err_put;
+ }
+
+ /* clocks */
+ dwsmmio->clk = devm_clk_get_enabled(&pdev->dev, NULL);
+ if (IS_ERR(dwsmmio->clk)) {
+ ret = PTR_ERR(dwsmmio->clk);
+ goto err_put;
+ }
+
+ dwsmmio->pclk = devm_clk_get_optional_enabled(&pdev->dev, "pclk");
+ if (IS_ERR(dwsmmio->pclk)) {
+ ret = PTR_ERR(dwsmmio->pclk);
+ goto err_put;
+ }
+
+ dws->clk_rate = clk_get_rate(dwsmmio->clk);
+ dev_dbg(&pdev->dev, "SSI clock rate = %u Hz\n", dws->clk_rate);
+
+ /* Reset control (shared: multiple IPs may use same reset line) */
+ dwsmmio->rstc = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
+ if (IS_ERR(dwsmmio->rstc)) {
+ ret = PTR_ERR(dwsmmio->rstc);
+ goto err_put;
+ }
+
+ reset_control_deassert(dwsmmio->rstc);
+
+ /* Read num-cs from device tree */
+ ret = device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
+ if (ret) {
+ dev_err(&pdev->dev, "Failed to read 'num-cs' property: %d\n", ret);
+ goto err_reset;
+ }
+ ctlr->num_chipselect = num_cs;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN,
+ GENMASK(ctlr->num_chipselect - 1, 0));
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
+ /* Clear all interrupts by writing 1s to defined ICR bits */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ /*
+ * Latch the static controller identity once. VERSION_ID and
+ * HW_CAPABILITIES are read-only and never change at runtime.
+ */
+ dws->version_id = dw_spi_g2_readl(dws->regs, DW_SPI_G2_VERSION_ID);
+ dws->hw_caps = dw_spi_g2_readl(dws->regs, DW_SPI_G2_HW_CAPABILITIES);
+ dws->caps = dws->hw_caps;
+
+ dev_dbg(dws->dev, "VERSION_ID = 0x%08x\n", dws->version_id);
+ dev_dbg(dws->dev, "HW_CAPS = 0x%08x\n", dws->hw_caps);
+
+ dws->max_freq = clk_get_rate(dwsmmio->clk);
+ /* SPI Controller Setup */
+ ctlr->bus_num = of_alias_get_id(pdev->dev.of_node, "spi");
+ if (ctlr->bus_num < 0)
+ ctlr->bus_num = pdev->id;
+ ctlr->transfer_one = NULL;
+ ctlr->dev.of_node = pdev->dev.of_node;
+
+ spi_controller_set_devdata(ctlr, dws);
+
+ /* core init */
+ ret = dw_spi_g2_init(dws);
+ if (ret)
+ goto err_reset;
+
+ ret = devm_request_irq(&pdev->dev, dws->irq, dw_spi_g2_irq, 0,
+ dev_name(&pdev->dev), dws);
+ if (ret)
+ goto err_reset;
+
+ pm_runtime_enable(&pdev->dev);
+
+ ret = spi_register_controller(ctlr);
+ if (ret)
+ goto err_pm;
+
+ dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
+ return 0;
+
+err_pm:
+ pm_runtime_disable(&pdev->dev);
+err_reset:
+ reset_control_assert(dwsmmio->rstc);
+err_put:
+ spi_controller_put(ctlr);
+ return ret;
+}
+
+static void dw_spi_g2_mmio_remove(struct platform_device *pdev)
+{
+ struct spi_controller *ctlr = platform_get_drvdata(pdev);
+ struct dw_spi_g2_mmio *dwsmmio = spi_controller_get_devdata(ctlr);
+
+ spi_unregister_controller(ctlr);
+ dw_spi_g2_deinit(&dwsmmio->dws);
+ pm_runtime_disable(&pdev->dev);
+ reset_control_assert(dwsmmio->rstc);
+}
+
+static const struct of_device_id dw_spi_g2_of_match[] = {
+ { .compatible = "snps,dwc-ssi-2.02a", .data = dw_spi_g2_init},
+ { }
+};
+MODULE_DEVICE_TABLE(of, dw_spi_g2_of_match);
+
+static struct platform_driver dw_spi_g2_mmio_driver = {
+ .probe = dw_spi_g2_mmio_probe,
+ .remove = dw_spi_g2_mmio_remove,
+ .driver = {
+ .name = "dw-spi-g2-mmio",
+ .of_match_table = dw_spi_g2_of_match,
+ },
+};
+
+module_platform_driver(dw_spi_g2_mmio_driver);
+
+MODULE_IMPORT_NS("SPI_DW_G2_CORE");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("DesignWare SSI G2 MMIO driver");
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
new file mode 100644
index 000000000000..1106be0af7b6
--- /dev/null
+++ b/drivers/spi/spi-dw-g2.h
@@ -0,0 +1,396 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
+
+#ifndef _SPI_DW_G2_H
+#define _SPI_DW_G2_H
+
+#include <linux/spi/spi.h>
+#include <linux/io.h>
+#include <linux/device.h>
+#include <linux/irqreturn.h>
+#include <linux/iopoll.h>
+#include <linux/spi/spi-mem.h>
+
+#ifdef CONFIG_DEBUG_FS
+#include <linux/debugfs.h>
+#endif
+
+/* Identification */
+#define DW_SPI_G2_REG_MAP_VERSION 0x00
+#define DW_SPI_G2_HW_CAPABILITIES 0x04
+#define DW_SPI_G2_VERSION_ID 0x8C
+
+/* Dedicated CS Offset */
+#define DW_SPI_G2_DEDICATED_CS_BASE 0x1AC
+#define DW_SPI_G2_DEDICATED_CS_STRIDE 0x14
+#define DW_SPI_G2_DEDICATED_CS_EN 0x50
+#define DW_SPI_G2_CS_OFFSET(cs) \
+ (DW_SPI_G2_DEDICATED_CS_BASE + (cs) * DW_SPI_G2_DEDICATED_CS_STRIDE)
+
+/* Dedicated CS Blocks */
+#define DW_SPI_G2_CTRLR0(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x04)
+#define DW_SPI_G2_SPI_CTRLR0(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x08)
+#define DW_SPI_G2_BAUDR(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x0C)
+#define DW_SPI_G2_CTRLR1(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x10)
+#define DW_SPI_G2_SPI_CTRLR1(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x14)
+
+#define DW_SPI_G2_SSIENR 0x4C
+#define DW_SPI_G2_SER 0x54
+#define DW_SPI_G2_TXFTLR 0x58
+#define DW_SPI_G2_RXFTLR 0x5c
+#define DW_SPI_G2_TXFLR 0x60
+#define DW_SPI_G2_RXFLR 0x64
+#define DW_SPI_G2_SR 0x6C
+#define DW_SPI_G2_DR0 0x90
+#define DW_SPI_G2_DR1 0x94
+#define DW_SPI_G2_DR(i) (DW_SPI_G2_DR0 + ((i) * 4))
+#define DW_SPI_G2_IMR 0x70
+#define DW_SPI_G2_ISR 0x74
+#define DW_SPI_G2_RISR 0x78
+#define DW_SPI_G2_ICR 0x7C
+#define DW_SPI_G2_IDR 0x88
+
+/* ISR bit masks (interrupt status) */
+#define DW_SPI_G2_ISR_TXEI BIT(0) /* TX FIFO Empty */
+#define DW_SPI_G2_ISR_TXOI BIT(1) /* TX FIFO Overflow */
+#define DW_SPI_G2_ISR_RXUI BIT(2) /* RX FIFO Underflow */
+#define DW_SPI_G2_ISR_RXOI BIT(3) /* RX FIFO Overflow */
+#define DW_SPI_G2_ISR_RXFI BIT(4) /* RX FIFO Full */
+#define DW_SPI_G2_ISR_MSTI BIT(5) /* Multi-Master Error */
+#define DW_SPI_G2_ISR_XRXOI BIT(6) /* Extended RX Overflow */
+#define DW_SPI_G2_ISR_TXUI BIT(7) /* TX FIFO Underflow */
+
+/* ICR bit masks (write 1 to clear corresponding interrupt) */
+#define DW_SPI_G2_ICR_TXOICR BIT(1) /* TX FIFO Overflow Clear */
+#define DW_SPI_G2_ICR_RXUICR BIT(2) /* RX FIFO Underflow Clear */
+#define DW_SPI_G2_ICR_RXOICR BIT(3) /* RX FIFO Overflow Clear */
+#define DW_SPI_G2_ICR_MSTICR BIT(5) /* Multi-Master Clear */
+#define DW_SPI_G2_ICR_XRXOICR BIT(6) /* Extended RX Overflow Clear */
+#define DW_SPI_G2_ICR_TXUICR BIT(7) /* TX FIFO Underflow Clear */
+#define DW_SPI_G2_ICR_AXIEICR BIT(8) /* AXI Error Clear */
+#define DW_SPI_G2_ICR_SPITEICR BIT(10) /* SPI Transmit Error Clear */
+#define DW_SPI_G2_ICR_DONEICR BIT(11) /* SSI Done Clear */
+#define DW_SPI_G2_ICR_SPIWSFICR BIT(13) /* SPI Write Status Fail Clear */
+#define DW_SPI_G2_ICR_ECCCEICR BIT(15) /* ECC Correctable Error Clear */
+#define DW_SPI_G2_ICR_ECCUCEICR BIT(16) /* ECC Uncorrectable Error Clear */
+#define DW_SPI_G2_ICR_PVTTICR BIT(17) /* PVT Tracking Interrupt Clear */
+/* Clear all writable bits */
+#define DW_SPI_G2_ICR_ALL 0x0003AFFF
+
+#define DW_SPI_G2_RX_SAMPLE_DELAY 0x24
+
+/* CTRLR0 fields */
+#define DW_SPI_G2_CTRLR0_DFS_MASK GENMASK(4, 0)
+#define DW_SPI_G2_CTRLR0_FRF_MASK GENMASK(15, 14)
+#define DW_SPI_G2_CTRLR0_TMOD_MASK GENMASK(11, 10)
+#define DW_SPI_G2_CTRLR0_SCPHA BIT(8)
+#define DW_SPI_G2_CTRLR0_SCPOL BIT(9)
+#define DW_SPI_G2_CTRLR0_SRL BIT(12)
+
+/* SPI_CTRLR0 fields */
+#define DW_SPI_G2_SPI_CTRLR0_TRANS_TYPE_SHIFT 0
+#define DW_SPI_G2_SPI_CTRLR0_TRANS_TYPE_MASK GENMASK(1, 0)
+#define DW_SPI_G2_SPI_CTRLR0_ADDR_L_SHIFT 2
+#define DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK GENMASK(5, 2)
+#define DW_SPI_G2_SPI_CTRLR0_INST_L_SHIFT 8
+#define DW_SPI_G2_SPI_CTRLR0_INST_L_MASK GENMASK(9, 8)
+#define DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLE_SHIFT 11
+#define DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLE_MASK GENMASK(15, 11)
+#define DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX 0x1FU
+#define DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK BIT(16)
+#define DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK BIT(17)
+#define DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK BIT(18)
+
+#define DW_SPI_G2_SPI_CTRLR0_ADDR_L_MAX 0xFU
+#define DW_SPI_G2_SPI_CTRLR0_ADDR_L_BITS_PER_STEP 4U
+#define DW_SPI_G2_ADDR_L_MAX_BITS \
+ ((unsigned int)(DW_SPI_G2_SPI_CTRLR0_ADDR_L_MAX * \
+ DW_SPI_G2_SPI_CTRLR0_ADDR_L_BITS_PER_STEP))
+
+/* TMOD values */
+#define DW_SPI_G2_CTRLR0_TMOD_TR 0x0 /* xmit & recv */
+#define DW_SPI_G2_CTRLR0_TMOD_TO 0x1 /* xmit only */
+#define DW_SPI_G2_CTRLR0_TMOD_RO 0x2 /* recv only */
+#define DW_SPI_G2_CTRLR0_TMOD_EEPROMREAD 0x3 /* eeprom read mode */
+
+/* FRF */
+#define DW_SPI_G2_FRF_STANDARD 0x0
+#define DW_SPI_G2_FRF_DUAL 0x1
+#define DW_SPI_G2_FRF_QUAD 0x2
+#define DW_SPI_G2_FRF_OCTAL 0x3
+
+/* Transfer Type */
+#define DW_SPI_G2_TRANS_TYPE_TT0 0x0
+#define DW_SPI_G2_TRANS_TYPE_TT1 0x1
+#define DW_SPI_G2_TRANS_TYPE_TT2 0x2
+#define DW_SPI_G2_TRANS_TYPE_TT3 0x3
+
+/* SR bits */
+#define DW_SPI_G2_SR_BUSY BIT(0)
+#define DW_SPI_G2_SR_TFNF BIT(1)
+#define DW_SPI_G2_SR_TFE BIT(2)
+#define DW_SPI_G2_SR_RFNE BIT(3)
+#define DW_SPI_G2_SR_RFF BIT(4)
+#define DW_SPI_G2_SR_WIP BIT(7)
+
+/* DFS encoding */
+#define DW_SPI_G2_DFS_8BIT 7U
+#define DW_SPI_G2_DFS_16BIT 15U
+#define DW_SPI_G2_DFS_32BIT 31U
+#define DW_SPI_G2_DFS_ENCODE(bits) ((bits) - 1)
+
+/* INST_L encoding */
+#define DW_SPI_G2_INST_L_0 0x0
+#define DW_SPI_G2_INST_L_4 0x1
+#define DW_SPI_G2_INST_L_8 0x2
+#define DW_SPI_G2_INST_L_16 0x3
+
+static inline u32 dw_spi_g2_inst_len(unsigned int n)
+{
+ switch (n) {
+ case 0:
+ return DW_SPI_G2_INST_L_0;
+ case 1:
+ return DW_SPI_G2_INST_L_8;
+ case 2:
+ return DW_SPI_G2_INST_L_16;
+ default:
+ return DW_SPI_G2_INST_L_8;
+ }
+}
+
+/* ADDR_L encoding */
+#define DW_SPI_G2_ADDR_L_0 0x0
+#define DW_SPI_G2_ADDR_L_4 0x1
+#define DW_SPI_G2_ADDR_L_8 0x2
+#define DW_SPI_G2_ADDR_L_16 0x4
+#define DW_SPI_G2_ADDR_L_24 0x6
+#define DW_SPI_G2_ADDR_L_32 0x8
+
+/*
+ * Map @addr_nbytes to ADDR_L register encoding.
+ *
+ * ADDR_L encoding: Each increment = 4 bits. ADDR_L=n means n×4 bits.
+ * ADDR_L=0 == 0 bits, ADDR_L=2 == 8 bits (1 byte), ADDR_L=4 == 16 bits (2 bytes), etc.
+ *
+ * Formula: ADDR_L = (addr_bits / 4) = (addr_nbytes × 8) / 4 = addr_nbytes × 2
+ *
+ * Note: This function assumes byte-aligned addresses (multiple of 8 bits), which
+ * matches the Linux spi-mem framework's addr.nbytes field. The hardware supports
+ * non-byte-aligned lengths (4, 12, 20 bits, etc.) via other ADDR_L values (1, 3, 5),
+ * but the spi-mem framework does not expose bit-level addressing.
+ *
+ * Caller must ensure addr_nbytes * 8 <= DW_SPI_G2_ADDR_L_MAX_BITS (60 bits).
+ */
+static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)
+{
+ return addr_nbytes ? addr_nbytes * 2U : 0U;
+}
+
+/*
+ * Maximum chunk size for indirect reads.
+ * 64KB allows efficient large transfers while staying well within typical
+ * controller limits. The FIFO (256 bytes) drains continuously during
+ * the transfer via polling.
+ */
+#define DW_G2_MAX_CHUNK 256U
+
+/* Timeout in microseconds for polling operations */
+#define DW_SPI_G2_TIMEOUT_US 200000U
+
+/* Default max frequency in Hz (100 MHz) */
+#define DW_SPI_G2_DEF_MAX_FREQ 100000000U
+
+/* Default FIFO depth */
+#define DW_SPI_G2_FIFO_LEN 256U
+
+/*
+ * Maximum baud rate divisor value for BAUDR register.
+ * BAUDR encoding: bit[0]=passthrough_en, bits[15:1]=SCKDV (divisor).
+ * Max SCKDV = 0x7FFF (32767), encoded as BAUDR = 0xFFFE (65534).
+ * Note: divisor must be even (BAUDR >= 2), except BAUDR=1 (passthrough).
+ */
+#define DW_SPI_G2_MAX_BAUDR 65534U
+
+/*
+ * Maximum data bytes for register operations (stack buffer size - 1 for opcode).
+ * Register ops use small stack buffers: tx_buf[16] and rx_tmp[16].
+ * Buffer layout: [opcode (1 byte)][data (up to 15 bytes)].
+ * This is sufficient for typical SPI NOR register commands:
+ * - RDID: 3 bytes
+ * - RDSR: 1-2 bytes
+ * - RDFR: 1 byte
+ * - etc.
+ * Larger transfers use indirect read/write with FIFO (up to DW_G2_MAX_CHUNK).
+ */
+#define DW_SPI_G2_REG_OP_MAX_DATA 15U
+
+struct dw_spi_g2_cfg {
+ u8 tmode;
+ u8 dfs;
+ u32 ndf;
+ u32 freq;
+};
+
+struct dw_spi_g2 {
+ struct device *dev;
+ struct spi_controller *ctlr;
+
+ void __iomem *regs;
+ resource_size_t paddr;
+ int irq;
+
+ u32 cs;
+ u32 fifo_len;
+ u32 caps;
+ /* Static controller identity, read once at probe */
+ u32 version_id;
+ u32 hw_caps;
+ u32 max_freq;
+ u32 clk_rate;
+
+ /* Current message transfer state info */
+ const void *tx;
+ unsigned int tx_len;
+ void *rx;
+ unsigned int rx_len;
+ u8 n_bytes;
+
+ struct spi_device *curr_spi;
+ struct completion mem_done;
+ int error;
+
+#ifdef CONFIG_DEBUG_FS
+ struct dentry *debugfs;
+ struct debugfs_regset32 regset;
+#endif
+};
+
+struct dw_spi_g2_memtrx {
+ u8 opcode;
+ u8 inst_len;
+ u32 addr;
+ u8 addr_len;
+ u8 dummy_cycles;
+ u32 data_len;
+ bool is_read;
+ bool is_write;
+ const u8 *tx;
+ u8 *rx;
+};
+
+/* Low-level register access */
+/*
+ * Use the barrier-embedding readl()/writel() accessors (not the *_relaxed
+ * variants) so every register access is ordered with respect to normal
+ * memory: writel()'s wmb drains prior stores before the MMIO write and
+ * readl()'s rmb completes the read before subsequent accesses. This
+ * guarantees, for example, that DR0 TX-FIFO fills reach the controller
+ * before the CS-asserting SER write triggers the transfer, without
+ * needing explicit wmb() barriers on the transfer paths.
+ */
+static inline u32 dw_spi_g2_readl(void __iomem *base, u32 off)
+{
+ return readl(base + off);
+}
+
+static inline void dw_spi_g2_writel(void __iomem *base, u32 off, u32 val)
+{
+ writel(val, base + off);
+}
+
+/* APIs */
+int dw_spi_g2_setup(struct spi_device *spi);
+
+int dw_spi_g2_init(struct dw_spi_g2 *dws);
+
+void dw_spi_g2_deinit(struct dw_spi_g2 *dws);
+
+void dw_spi_g2_update_config(struct dw_spi_g2 *dws, struct spi_device *spi,
+ struct dw_spi_g2_cfg *cfg);
+
+int dw_spi_g2_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
+ struct spi_transfer *xfer);
+
+int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op);
+
+irqreturn_t dw_spi_g2_irq(int irq, void *dev_id);
+
+/* Helpers */
+static inline void dw_spi_g2_enable(struct dw_spi_g2 *dws, bool en)
+{
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SSIENR, en ? 1 : 0);
+}
+
+static inline void dw_spi_g2_target_disable(struct dw_spi_g2 *dws, u32 cs)
+{
+ u32 reg_val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DEDICATED_CS_EN);
+
+ reg_val &= ~BIT(cs);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN, reg_val);
+}
+
+static inline void dw_spi_g2_target_enable(struct dw_spi_g2 *dws, u32 cs)
+{
+ u32 reg_val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DEDICATED_CS_EN);
+
+ reg_val |= BIT(cs);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN, reg_val);
+}
+
+static inline void dw_spi_g2_cs_assert(struct dw_spi_g2 *dws, u32 cs)
+{
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, BIT(cs));
+}
+
+static inline void dw_spi_g2_cs_deassert(struct dw_spi_g2 *dws, u32 cs)
+{
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+}
+
+static inline int dw_spi_g2_wait_tfnf(struct dw_spi_g2 *dws)
+{
+ u32 sr;
+
+ return readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
+ sr & DW_SPI_G2_SR_TFNF, 1, DW_SPI_G2_TIMEOUT_US);
+}
+
+static inline int dw_spi_g2_wait_rfne(struct dw_spi_g2 *dws)
+{
+ u32 sr;
+
+ return readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
+ sr & DW_SPI_G2_SR_RFNE, 1, DW_SPI_G2_TIMEOUT_US);
+}
+
+static inline int dw_spi_g2_wait_idle(struct dw_spi_g2 *dws)
+{
+ u32 sr;
+
+ return readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
+ !(sr & DW_SPI_G2_SR_BUSY), 1, DW_SPI_G2_TIMEOUT_US);
+}
+
+/*
+ * Transfer control: enable CS register block, assert CS line, enable SSI.
+ * Starts the SPI transfer — controller begins clocking data.
+ */
+static inline void dw_spi_g2_transfer_begin(struct dw_spi_g2 *dws)
+{
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_cs_assert(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+}
+
+/*
+ * Transfer control: disable SSI, deassert CS line, disable CS register block.
+ * Ends the SPI transfer and cleanly releases the bus.
+ */
+static inline void dw_spi_g2_transfer_end(struct dw_spi_g2 *dws)
+{
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_cs_deassert(dws, dws->cs);
+ dw_spi_g2_target_disable(dws, dws->cs);
+}
+
+#endif /* __SPI_DW_G2_H__ */
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
2026-11-19 10:05 ` [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver Prakriti Sharma
@ 2026-11-19 10:28 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-19 10:28 UTC (permalink / raw)
To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 13 potential
issues to consider:
- [High] spi: dw-g2: dw_spi_g2_supports_mem_ops() accepts ops
misexecuted by reg_op()
- [Medium] spi: dw-g2: debugfs init before bus_num allocation and leak
on probe error
- [Medium] spi: dw-g2: missing SPI_CPOL and SPI_CPHA setup in
dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: missing spi_controller_put() in
dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: debugfs leak, UAF, and -1 bus_num in
dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: devm IRQ outlives reset and dws in probe() and
remove()
- [Medium] spi: dw-g2: unwired dw_spi_g2_transfer_one() locks up CPU if
called
- [Medium] spi: dw-g2: missing txe, txaf, and wsf IRQ requests in
dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: unvalidated num-cs and clobbered dws->caps in
probe and init
- [Medium] spi: dw-g2: missing linux/bitfield.h include for FIELD_PREP()
- [Medium] spi: dw-g2: unchecked dw_spi_g2_wait_idle() and missing
SR_TFE check
- [Low] spi: dw-g2: MAINTAINERS glob misses spi-dw-g2.h and bad
kernel-doc markers
- [Low] spi: dw-g2: non-kernel-doc comments using /** in
spi-dw-g2-core.c
--
commit 6b1bdfd4c5618c387e6d2d343a2ddf90fcf357ed
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
This commit adds the core and MMIO driver for the Synopsys DesignWare SSI
Gen2 (DWC_ssi v2.02a) SPI controller, supporting probe, command-only
transfers, and short register operations via polling.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e68a4337a651a..e0f97d2792b1d 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -27310,6 +27310,7 @@ M: Siddhant Kumar <sidkumar@synopsys.com>
> L: linux-spi@vger.kernel.org
> S: Maintained
> F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> +F: drivers/spi/spi-dw-g2-*.[ch]
[Severity: Low]
Does the hyphen before the wildcard cause get_maintainer.pl to miss
drivers/spi/spi-dw-g2.h? Using drivers/spi/spi-dw-g2*.[ch] would match
both the header and the .c files.
> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> new file mode 100644
> index 0000000000000..7b0928f671033
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-core.c
[ ... ]
> +static void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws)
> +{
> + char name[32];
> +
> + snprintf(name, sizeof(name), "dw_spi_g2.%d", dws->ctlr->bus_num);
> +
> + dws->debugfs = debugfs_create_dir(name, NULL);
> + if (!dws->debugfs)
> + return;
> +
> + dws->regset.base = dws->regs;
> + dws->regset.regs = dw_spi_g2_dbg_regs;
> + dws->regset.nregs = ARRAY_SIZE(dw_spi_g2_dbg_regs);
> +
> + debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
> +}
[Severity: Medium]
Does calling dw_spi_g2_debugfs_init() from dw_spi_g2_init() run before
spi_register_controller() assigns a dynamic bus_num?
When a Device Tree node has no spiN alias, dws->ctlr->bus_num is still -1
during dw_spi_g2_init(), so this creates /sys/kernel/debug/dw_spi_g2.-1
and collides if a second controller is present. In addition,
debugfs_create_dir() returns an ERR_PTR() when debugfs is disabled or
fails, so checking !dws->debugfs does not catch errors (though
debugfs_create_regset32() handles an ERR_PTR() parent).
[ ... ]
> +static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
> +{
[ ... ]
> + ctrlr0 |=
> + FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, DW_SPI_G2_DFS_ENCODE(dfs));
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_TMOD_MASK, tmod);
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_FRF_MASK, frf);
> +
> + return ctrlr0;
> +}
[Severity: Medium]
Should dw_spi_g2_build_ctrlr0() (and dw_spi_g2_update_config()) also set
DW_SPI_G2_CTRLR0_SCPOL and DW_SPI_G2_CTRLR0_SCPHA from spi->mode?
dw_spi_g2_init() advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but
neither function programs the polarity or phase bits into CTRLR0, forcing
all transfers to run in SPI Mode 0 and breaking communication with Mode
1, 2, or 3 devices.
[ ... ]
> +static int dw_spi_g2_xfer_fifo(struct dw_spi_g2 *dws,
> + const u8 *tx_buf, u32 tx_len,
> + u8 *rx_buf, u32 rx_len)
> +{
[ ... ]
> + if (!rx_buf || !rx_len) {
> + dw_spi_g2_wait_idle(dws);
> + return 0;
> + }
> +
> + /* RX phase */
> + timeout = jiffies + msecs_to_jiffies(1000);
> + while (rx_got < rx_len) {
> + sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> + if (sr & DW_SPI_G2_SR_RFNE) {
> + rx_buf[rx_got++] = dw_spi_g2_readl(dws->regs,
> + DW_SPI_G2_DR0) &
> + 0xff;
> + timeout = jiffies + msecs_to_jiffies(1000);
> + } else if (time_after(jiffies, timeout)) {
> + dev_err(dws->dev,
> + "xfer_fifo: RX timeout after %u/%u bytes (SR=0x%08x RXFLR=%u)\n",
> + rx_got, rx_len,
> + dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR),
> + dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR));
> + return -ETIMEDOUT;
> + }
> + cpu_relax();
> + }
> +
> + dw_spi_g2_wait_idle(dws);
> + return 0;
> +}
[Severity: Medium]
Can ignoring the return value of dw_spi_g2_wait_idle() here report false
success when a transfer times out, and can dw_spi_g2_wait_idle() return
before the TX FIFO has started shifting?
dw_spi_g2_wait_idle() in drivers/spi/spi-dw-g2.h only polls
!(sr & DW_SPI_G2_SR_BUSY) without checking (sr & DW_SPI_G2_SR_TFE).
Because SR_BUSY is driven by the serial shifter in the ssi_clk domain,
reading SR immediately after writing DR0 can observe SR_BUSY == 0 while
bytes are still queued in the TX FIFO (SR_TFE == 0), causing the caller's
dw_spi_g2_transfer_end() to clear SSIENR and abort the transfer early.
Also, if dw_spi_g2_wait_idle() times out, dw_spi_g2_xfer_fifo() discards
the -ETIMEDOUT error and returns 0.
[ ... ]
> +/**
> + * Command-only operations (WREN, WRDI, Chip Erase, Sector Erase, etc.)
> + * TMOD=1 (TX-Only) — Send opcode + optional address, no data phase
> + */
[ ... ]
> +/**
> + * Indirect read operations
> + */
> +
> +/*
> + * ========================================================================
> + * SPI-MEM interface
> + * ========================================================================
> + */
> +
> +static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
> + const struct spi_mem_op *op)
> +{
> + return spi_mem_default_supports_op(mem, op);
> +}
[Severity: Low]
The two section comments above ("Command-only operations..." and
"Indirect read operations") start with /** instead of /*, which triggers
scripts/kernel-doc warnings under W=1 builds.
[Severity: High]
Does dw_spi_g2_supports_mem_ops() accept operations that
dw_spi_g2_reg_op() and dw_spi_g2_cmd_only() cannot execute as requested?
Because ctlr->mode_bits advertises Dual, Quad, and Octal modes,
spi_mem_default_supports_op() accepts multi-wire operations, operations
with dummy cycles, and 2-byte opcodes. However, dw_spi_g2_cmd_only_setup()
and dw_spi_g2_reg_op_setup() hardcode DW_SPI_G2_FRF_STANDARD (1-1-1 mode)
and write 0 to DW_SPI_G2_SPI_CTRLR0, and dw_spi_g2_reg_op() only sends a
1-byte opcode (tx_buf[0] = op->cmd.opcode) and omits op->dummy.nbytes
entirely, resulting in malformed flash transactions on the wire. Even
after commit "spi: dw-g2: Add SPI-NAND support" and commit "spi: dw-g2:
Add indirect read and write", dw_spi_g2_is_reg_op() routes all transfers
up to 256 bytes into dw_spi_g2_reg_op() in 1-1-1 mode with 1-byte opcodes,
and TX register/command ops still drop dummy cycles.
[ ... ]
> +void dw_spi_g2_update_config(struct dw_spi_g2 *dws, struct spi_device *spi,
> + struct dw_spi_g2_cfg *cfg)
> +{
> + u32 ctrlr0 = 0;
> + u32 div;
> +
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, cfg->dfs - 1);
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_TMOD_MASK, cfg->tmode);
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +
> + if (cfg->tmode == DW_SPI_G2_CTRLR0_TMOD_RO)
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), cfg->ndf - 1);
> +
> + div = DIV_ROUND_UP(dws->max_freq, cfg->freq);
> + if (div & 1)
> + div++;
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), div);
> +}
> +
[ ... ]
> +int dw_spi_g2_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
> + struct spi_transfer *xfer)
> +{
[ ... ]
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_update_config(dws, spi, &cfg);
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, dws->fifo_len / 2);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, (dws->fifo_len / 2) - 1);
> +
> + dw_spi_g2_enable(dws, true);
> +
> + local_irq_disable();
> + /* Polling-based full-duplex transfer */
> + while (dws->tx_len || dws->rx_len) {
> + sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> +
> + /* Transmit if FIFO not full and bytes remaining */
> + if (dws->tx_len && (sr & DW_SPI_G2_SR_TFNF)) {
> + u8 val = 0;
> +
> + if (dws->tx) {
> + val = *(u8 *)dws->tx;
> + dws->tx += 1;
> + }
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, val);
> + dws->tx_len--;
> + }
> +
> + /* Receive if FIFO not empty and bytes expected */
> + if (dws->rx_len && (sr & DW_SPI_G2_SR_RFNE)) {
> + u8 val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
> +
> + if (dws->rx) {
> + *(u8 *)dws->rx = val;
> + dws->rx += 1;
> + }
> + dws->rx_len--;
> + }
> + cpu_relax();
> + }
> + local_irq_enable();
[Severity: Medium]
Should dw_spi_g2_transfer_one() and dw_spi_g2_update_config() be removed
since ctlr->transfer_one is never wired up and neither symbol is exported?
If dw_spi_g2_transfer_one() is ever called, it never enables the target CS
block or asserts DW_SPI_G2_SER, so the hardware never shifts out the TX
FIFO and the unbounded loop under local_irq_disable() will hard-lock the
CPU waiting for DW_SPI_G2_SR_RFNE. In addition, dw_spi_g2_update_config()
writes per-CS registers without disabling the target CS block and divides
by cfg->freq without guarding against 0.
> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> new file mode 100644
> index 0000000000000..6ef8f926e0eb9
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-mmio.c
> @@ -0,0 +1,196 @@
[ ... ]
> +static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
> +{
[ ... ]
> + /*
> + * IRQ.
> + *
> + * The DWC SSI Gen2 IP exposes per-source interrupt outputs
> + * alongside a combined ssi_intr. SoC integrators may route
> + * either the combined line, the individual lines, or any
> + * subset. We accept both DT forms:
> + *
> + * - Named-line DT: one entry per output, with interrupt-names
> + * in {"rxf","txe","txaf","wsf"}. Each line is requested
> + * and bound to the shared dw_spi_g2_irq() decoder.
> + * - Single-line DT: combined ssi_intr at index 0, used by
> + * integrations that OR the sources in HW.
> + *
> + * Either way dws->irq holds the "primary" line (rxf when
> + * named, index 0 otherwise) so debugfs and PM paths stay
> + * source-agnostic.
> + */
> + dws->irq = platform_get_irq_byname_optional(pdev, "rxf");
> + if (dws->irq < 0)
> + dws->irq = platform_get_irq(pdev, 0);
> + if (dws->irq < 0) {
> + ret = dws->irq;
> + goto err_put;
> + }
[Severity: Medium]
Where are the "txe", "txaf", and "wsf" interrupt lines requested when
named interrupts are used in Device Tree?
Both the commit message and the comment above state that each of {"rxf",
"txe", "txaf", "wsf"} is requested and bound to dw_spi_g2_irq(), but
dw_spi_g2_mmio_probe() only queries "rxf" (falling back to index 0 even if
"rxf" returned -EPROBE_DEFER) and calls devm_request_irq() once for
dws->irq.
[ ... ]
> + reset_control_deassert(dwsmmio->rstc);
> +
> + /* Read num-cs from device tree */
> + ret = device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
> + if (ret) {
> + dev_err(&pdev->dev, "Failed to read 'num-cs' property: %d\n", ret);
> + goto err_reset;
> + }
> + ctlr->num_chipselect = num_cs;
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN,
> + GENMASK(ctlr->num_chipselect - 1, 0));
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
> + /* Clear all interrupts by writing 1s to defined ICR bits */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
> +
> + /*
> + * Latch the static controller identity once. VERSION_ID and
> + * HW_CAPABILITIES are read-only and never change at runtime.
> + */
> + dws->version_id = dw_spi_g2_readl(dws->regs, DW_SPI_G2_VERSION_ID);
> + dws->hw_caps = dw_spi_g2_readl(dws->regs, DW_SPI_G2_HW_CAPABILITIES);
> + dws->caps = dws->hw_caps;
[Severity: Medium]
Can an omitted or out-of-range num-cs property break probe or trigger
undefined shift behavior in GENMASK(ctlr->num_chipselect - 1, 0)?
In Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml, num-cs
is optional with a valid range of 1..4, so probe should default num-cs
when absent and reject values outside 1..4 before evaluating GENMASK() or
indexing per-CS register blocks. Also, the return value of
reset_control_deassert() is not checked before accessing MMIO registers,
and the writes to DW_SPI_G2_DEDICATED_CS_EN and dws->caps = dws->hw_caps
here are immediately overwritten with 0 by dw_spi_g2_init() below.
[ ... ]
> + /* core init */
> + ret = dw_spi_g2_init(dws);
> + if (ret)
> + goto err_reset;
> +
> + ret = devm_request_irq(&pdev->dev, dws->irq, dw_spi_g2_irq, 0,
> + dev_name(&pdev->dev), dws);
> + if (ret)
> + goto err_reset;
> +
> + pm_runtime_enable(&pdev->dev);
> +
> + ret = spi_register_controller(ctlr);
> + if (ret)
> + goto err_pm;
> +
> + dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
> + return 0;
> +
> +err_pm:
> + pm_runtime_disable(&pdev->dev);
> +err_reset:
> + reset_control_assert(dwsmmio->rstc);
> +err_put:
> + spi_controller_put(ctlr);
> + return ret;
> +}
[Severity: Medium]
Does failing at devm_request_irq() or spi_register_controller() leak the
debugfs directory created by dw_spi_g2_init() and leave a dangling
&dws->regset pointer after spi_controller_put(ctlr) frees ctlr?
Because err_pm and err_reset do not call dw_spi_g2_deinit(dws),
/sys/kernel/debug/dw_spi_g2.<bus_num>/registers remains registered
pointing to freed memory, causing a use-after-free if read.
[Severity: Medium]
Can dw_spi_g2_irq() fire after reset_control_assert() and
spi_controller_put(ctlr) on the err_pm error path (or after
reset_control_assert() in dw_spi_g2_mmio_remove())?
dws->irq is requested with devm_request_irq(), which is not freed until
after probe() or remove() returns, while ctlr (containing dws) is
allocated with the non-devm spi_alloc_host() and freed immediately at
err_put, and dwsmmio->rstc is asserted before devres releases the IRQ.
> +
> +static void dw_spi_g2_mmio_remove(struct platform_device *pdev)
> +{
> + struct spi_controller *ctlr = platform_get_drvdata(pdev);
> + struct dw_spi_g2_mmio *dwsmmio = spi_controller_get_devdata(ctlr);
> +
> + spi_unregister_controller(ctlr);
> + dw_spi_g2_deinit(&dwsmmio->dws);
> + pm_runtime_disable(&pdev->dev);
> + reset_control_assert(dwsmmio->rstc);
> +}
[Severity: Medium]
Does dw_spi_g2_mmio_remove() leak ctlr and dwsmmio?
ctlr is allocated with spi_alloc_host() rather than devm_spi_alloc_host(),
and spi_unregister_controller() only drops the registration reference from
device_add(), so the initial allocation reference is never released
without a matching spi_controller_put(ctlr) (or switching probe to
devm_spi_alloc_host()).
> diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
> new file mode 100644
> index 0000000000000..1106be0af7b65
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2.h
> @@ -0,0 +1,396 @@
> +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
> +
> +#ifndef _SPI_DW_G2_H
> +#define _SPI_DW_G2_H
> +
> +#include <linux/spi/spi.h>
> +#include <linux/io.h>
> +#include <linux/device.h>
> +#include <linux/irqreturn.h>
> +#include <linux/iopoll.h>
> +#include <linux/spi/spi-mem.h>
[Severity: Medium]
Does drivers/spi/spi-dw-g2-core.c fail to build at this commit on
architectures that do not transitively include linux/bitfield.h?
dw_spi_g2_build_ctrlr0() and dw_spi_g2_update_config() use FIELD_PREP(),
but <linux/bitfield.h> is only added to drivers/spi/spi-dw-g2.h later in
commit "spi: dw-g2: Add XIP support", breaking bisection on architectures
such as x86_64 and riscv.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=2
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 3/6] spi: dw-g2: Add indirect read and write
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
2026-11-19 10:05 ` [PATCH 1/6] dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding Prakriti Sharma
2026-11-19 10:05 ` [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:27 ` sashiko-bot
2026-11-19 10:05 ` [PATCH 4/6] spi: dw-g2: Add SPI-NAND support Prakriti Sharma
` (2 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
Add the FIFO indirect path used for spi-mem data transfers that are
not short register operations.
Reads use receive-only mode. Writes use transmit-only mode and send
the command, address, and payload as one continuous FIFO stream.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
drivers/spi/spi-dw-g2-core.c | 242 ++++++++++++++++++++++++++++++++++-
1 file changed, 236 insertions(+), 6 deletions(-)
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index 7b0928f67103..c640d6a19260 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -538,6 +538,237 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
* Indirect read operations
*/
+/**
+ * dw_spi_g2_indirect_read_setup - Configure controller for indirect read
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Sets up TMOD=RO with enhanced SPI framing.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_RO,
+ dw_spi_g2_get_frf(op));
+ u32 spictrlr0 = 0;
+ u32 dummy_cycles = 0;
+ int tt;
+
+ if (op->dummy.nbytes) {
+ if (!op->dummy.buswidth || op->dummy.buswidth > 8) {
+ dev_err(dws->dev, "Invalid dummy buswidth %u\n",
+ op->dummy.buswidth);
+ return -EINVAL;
+ }
+ dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX) {
+ dev_err(dws->dev, "Dummy cycles %u exceed maximum %u\n",
+ dummy_cycles, DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX);
+ return -EINVAL;
+ }
+ }
+
+ tt = dw_spi_g2_get_tt(op);
+ if (tt < 0) {
+ dev_err(dws->dev, "Unsupported transfer type: cmd=%u addr=%u data=%u\n",
+ op->cmd.buswidth,
+ op->addr.nbytes ? op->addr.buswidth : 0,
+ op->data.nbytes ? op->data.buswidth : 0);
+ return tt;
+ }
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs),
+ op->data.nbytes - 1);
+
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_TRANS_TYPE_MASK, tt);
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_INST_L_MASK,
+ dw_spi_g2_inst_len(op->cmd.nbytes));
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK,
+ dw_spi_g2_addr_len(op->addr.nbytes));
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLE_MASK,
+ dummy_cycles);
+
+ if (op->cmd.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
+ if (op->addr.dtr || op->data.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
+ if (op->data.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR,
+ min_t(u32, (dws->fifo_len / 2) - 1,
+ op->data.nbytes ? op->data.nbytes : 1));
+ return 0;
+}
+
+/**
+ * dw_spi_g2_indirect_read - Execute indirect read operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Reads data from flash using RO (receive-only) mode.
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 tx_entries;
+ u8 dummy_buf = 0;
+ int ret;
+
+ if (!op->data.nbytes) {
+ dev_err(dws->dev, "Zero-length read not allowed\n");
+ return -EINVAL;
+ }
+
+ if (!op->data.buf.in) {
+ dev_err(dws->dev, "RX buffer is NULL\n");
+ return -EINVAL;
+ }
+
+ ret = dw_spi_g2_indirect_read_setup(dws, op);
+ if (ret)
+ return ret;
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ tx_entries = 0;
+
+ /* Write instruction */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
+ tx_entries++;
+
+ /* Write address as a packed 32-bit value (one FIFO entry) */
+ if (op->addr.nbytes) {
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
+ tx_entries++;
+ }
+
+ /* Assert CS - controller begins: sends TX FIFO, then receives */
+ dw_spi_g2_cs_assert(dws, dws->cs);
+
+ /* Receive data via polling */
+ ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
+ op->data.buf.in, op->data.nbytes);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+
+ return ret;
+}
+
+/**
+ * Indirect write operations (Page Program)
+ * Uses TMOD=TO (Transmit-Only) mode
+ */
+
+/**
+ * dw_spi_g2_indirect_write_setup - Configure controller for indirect write
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Sets up TMOD=TO (transmit-only), standard SPI mode.
+ */
+static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TO,
+ DW_SPI_G2_FRF_STANDARD);
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
+}
+
+/**
+ * dw_spi_g2_indirect_write - Execute indirect write operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Writes data to flash using TO (transmit-only) mode. The controller sends
+ * instruction, address, and data bytes as a continuous stream.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 tx_total = op->cmd.nbytes + op->addr.nbytes + op->data.nbytes;
+ u8 *tx_buf;
+ u32 prefill, pos;
+
+ if (!op->data.nbytes) {
+ dev_err(dws->dev, "Zero-length write not allowed\n");
+ return -EINVAL;
+ }
+
+ if (!op->data.buf.out) {
+ dev_err(dws->dev, "TX buffer is NULL\n");
+ return -EINVAL;
+ }
+
+ dw_spi_g2_indirect_write_setup(dws, op);
+
+ /* Allocate and build TX buffer: [cmd][addr][data] */
+ tx_buf = kzalloc(tx_total, GFP_KERNEL);
+ if (!tx_buf)
+ return -ENOMEM;
+
+ dw_spi_g2_fill_tx_buf(tx_buf, op, true);
+
+ /*
+ * Split enable sequence:
+ * Enable SSI (without CS), pre-fill TX FIFO, then assert CS.
+ * FIFO pointers are reset while SSIENR=0, so writes before
+ * enabling would be discarded.
+ */
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ /* Pre-fill FIFO to capacity or total size */
+ prefill = min_t(u32, tx_total, dws->fifo_len);
+ for (pos = 0; pos < prefill; pos++)
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, tx_buf[pos]);
+
+ /* Assert CS - transfer starts */
+ dw_spi_g2_cs_assert(dws, dws->cs);
+
+ /* Refill FIFO with remaining bytes (if any) via polling */
+ if (pos < tx_total) {
+ int ret = dw_spi_g2_xfer_fifo(dws, &tx_buf[pos],
+ tx_total - pos, NULL, 0);
+ if (ret) {
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ kfree(tx_buf);
+ return ret;
+ }
+ }
+
+ /* Wait for transfer completion */
+ dw_spi_g2_wait_idle(dws);
+
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+
+ kfree(tx_buf);
+ return 0;
+}
+
/*
* ========================================================================
* SPI-MEM interface
@@ -626,12 +857,11 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
if (dw_spi_g2_is_reg_op(op))
return dw_spi_g2_reg_op(dws, op);
- /*
- * Bulk reads and writes are added by the indirect-transfer patch.
- * This patch handles commands and short register operations only.
- */
- if (op->data.dir != SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op(op))
- return -EOPNOTSUPP;
+ if (op->data.dir == SPI_MEM_DATA_IN)
+ return dw_spi_g2_indirect_read(dws, op);
+
+ if (op->data.dir == SPI_MEM_DATA_OUT)
+ return dw_spi_g2_indirect_write(dws, op);
return -EOPNOTSUPP;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 3/6] spi: dw-g2: Add indirect read and write
2026-11-19 10:05 ` [PATCH 3/6] spi: dw-g2: Add indirect read and write Prakriti Sharma
@ 2026-11-19 10:27 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-19 10:27 UTC (permalink / raw)
To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] spi: dw-g2: multi-line writes sent in 1-1-1 mode in
dw_spi_g2_indirect_write()
- [High] spi: dw-g2: premature TX termination and ignored timeout in
dw_spi_g2_indirect_write()
- [Medium] spi: dw-g2: 64-bit address truncated to u32 in
dw_spi_g2_indirect_read()
- [Medium] spi: dw-g2: dw_spi_g2_exec_mem_op() routes non-1-1-1 ops to
dw_spi_g2_reg_op()
- [Medium] spi: dw-g2: missing CPOL/CPHA setup in
dw_spi_g2_build_ctrlr0()
- [Low] spi: dw-g2: unused variable tx_entries in
dw_spi_g2_indirect_read()
- [Low] spi: dw-g2: malformed kernel-doc comment above
dw_spi_g2_indirect_write_setup()
--
commit 5c69b8ce5159adba9406d4ca9e6fadba79d41ae5
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
spi: dw-g2: Add indirect read and write
This commit adds the FIFO-based indirect read (receive-only) and indirect
write (transmit-only) paths for spi-mem data transfers.
> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index 7b0928f671033..c640d6a192600 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
> @@ -538,6 +538,237 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
> * Indirect read operations
> */
>
> +/**
> + * dw_spi_g2_indirect_read_setup - Configure controller for indirect read
> + * @dws: Driver state
> + * @op: SPI memory operation
> + *
> + * Sets up TMOD=RO with enhanced SPI framing.
> + *
> + * Return: 0 on success, negative error code on failure
> + */
> +static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> +{
> + u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_RO,
> + dw_spi_g2_get_frf(op));
[Severity: Medium]
Does dw_spi_g2_build_ctrlr0() omit DW_SPI_G2_CTRLR0_SCPOL and
DW_SPI_G2_CTRLR0_SCPHA from ctrlr0?
The controller advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but
dw_spi_g2_build_ctrlr0() never checks dws->curr_spi->mode, so indirect
reads and writes always program CTRLR0 for SPI Mode 0 and fail on flash
devices configured for SPI Mode 1, 2, or 3.
[ ... ]
> +static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> +{
> + u32 tx_entries;
> + u8 dummy_buf = 0;
> + int ret;
[ ... ]
> + tx_entries = 0;
> +
> + /* Write instruction */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
> + tx_entries++;
> +
> + /* Write address as a packed 32-bit value (one FIFO entry) */
> + if (op->addr.nbytes) {
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
> + tx_entries++;
> + }
[Severity: Low]
tx_entries is incremented here but never read, which triggers a
-Wunused-but-set-variable warning under W=1 builds.
[Severity: Medium]
Does casting op->addr.val to u32 and writing a single DR0 entry break
reads withop->addr.nbytes > 4?
dw_spi_g2_exec_mem_op() allows addresses up to DW_SPI_G2_ADDR_L_MAX_BITS
(60 bits) and dw_spi_g2_indirect_read_setup() programs ADDR_L for the
full op->addr.nbytes (and commit "spi: dw-g2: Add XIP and dirmap
support" folds dummy bytes into op.addr.nbytes before calling
dw_spi_g2_indirect_read()). When ADDR_L > 8 (more than 32 bits), the
controller expects two 32-bit address entries in the TX FIFO before
starting the receive phase, so writing only one truncated 32-bit word
stalls the transfer until dw_spi_g2_xfer_fifo() times out.
[ ... ]
> +/**
> + * Indirect write operations (Page Program)
> + * Uses TMOD=TO (Transmit-Only) mode
> + */
[Severity: Low]
This section comment starts with /** instead of /*, which causes a
scripts/kernel-doc warning on W=1 builds.
> +/**
> + * dw_spi_g2_indirect_write_setup - Configure controller for indirect write
> + * @dws: Driver state
> + * @op: SPI memory operation
> + *
> + * Sets up TMOD=TO (transmit-only), standard SPI mode.
> + */
> +static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> +{
> + u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TO,
> + DW_SPI_G2_FRF_STANDARD);
> +
> + dw_spi_g2_controller_reset(dws);
> + dw_spi_g2_target_disable(dws, dws->cs);
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
> +}
[Severity: High]
Can dw_spi_g2_indirect_write_setup() and dw_spi_g2_indirect_write() send
multi-line write operations in single-wire 1-1-1 mode?
dw_spi_g2_init() sets SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL in
ctlr->mode_bits and dw_spi_g2_supports_mem_ops() delegates directly to
spi_mem_default_supports_op(), so spi-mem negotiates multi-wire Page
Program operations (such as 1-1-4 or 1-4-4 Quad writes). However,
dw_spi_g2_indirect_write_setup() hardcodes DW_SPI_G2_FRF_STANDARD with
DW_SPI_G2_SPI_CTRLR0 set to 0, and dw_spi_g2_indirect_write() streams
[cmd][addr][data] over IO0 without multi-line framing or dummy cycles,
corrupting the data written to flash.
[ ... ]
> + /* Assert CS - transfer starts */
> + dw_spi_g2_cs_assert(dws, dws->cs);
> +
> + /* Refill FIFO with remaining bytes (if any) via polling */
> + if (pos < tx_total) {
> + int ret = dw_spi_g2_xfer_fifo(dws, &tx_buf[pos],
> + tx_total - pos, NULL, 0);
> + if (ret) {
> + dw_spi_g2_transfer_end(dws);
> + dw_spi_g2_controller_reset(dws);
> + kfree(tx_buf);
> + return ret;
> + }
> + }
> +
> + /* Wait for transfer completion */
> + dw_spi_g2_wait_idle(dws);
> +
> + dw_spi_g2_transfer_end(dws);
> + dw_spi_g2_controller_reset(dws);
> +
> + kfree(tx_buf);
> + return 0;
> +}
[Severity: High]
Can dw_spi_g2_wait_idle() return before the pre-filled TX FIFO has
drained, and does ignoring its return value hide transfer timeouts?
drivers/spi/spi-dw-g2.h:dw_spi_g2_wait_idle() {
u32 sr;
return readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
!(sr & DW_SPI_G2_SR_BUSY), 1, DW_SPI_G2_TIMEOUT_US);
}
When tx_total <= dws->fifo_len, dw_spi_g2_indirect_write() asserts CS and
immediately calls dw_spi_g2_wait_idle(). Because SER is written in the
APB clock domain while frames begin shifting on the divided ssi_clk,
DW_SPI_G2_SR_BUSY can still read 0 before the first byte is popped from
the TX FIFO (while DW_SPI_G2_SR_TFE is 0), causing
dw_spi_g2_transfer_end() to disable SSIENR and discard the queued write.
In addition, both dw_spi_g2_indirect_write() and dw_spi_g2_xfer_fifo()
ignore the -ETIMEDOUT return value of dw_spi_g2_wait_idle(). Commit
"spi: dw-g2: Add SPI-NAND support" later routes indirect writes through
dw_spi_g2_xfer_fifo(), which still leaves dw_spi_g2_wait_idle() missing
a DW_SPI_G2_SR_TFE check and its return value unchecked.
> @@ -626,12 +857,11 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
> if (dw_spi_g2_is_reg_op(op))
> return dw_spi_g2_reg_op(dws, op);
>
> - /*
> - * Bulk reads and writes are added by the indirect-transfer patch.
> - * This patch handles commands and short register operations only.
> - */
> - if (op->data.dir != SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op(op))
> - return -EOPNOTSUPP;
> + if (op->data.dir == SPI_MEM_DATA_IN)
> + return dw_spi_g2_indirect_read(dws, op);
> +
> + if (op->data.dir == SPI_MEM_DATA_OUT)
> + return dw_spi_g2_indirect_write(dws, op);
>
> return -EOPNOTSUPP;
> }
[Severity: Medium]
Does checking dw_spi_g2_is_reg_op() before the indirect paths misroute
short non-1-1-1 operations to dw_spi_g2_reg_op()?
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_is_reg_op() {
return !op->addr.nbytes && op->data.nbytes > 0 &&
op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
}
dw_spi_g2_is_reg_op() does not check that op->cmd.nbytes == 1,
op->dummy.nbytes == 0, all phase buswidths are 1, and DTR is disabled,
whereas dw_spi_g2_reg_op() only sends a 1-byte opcode in 1-1-1 standard
SPI mode without dummy cycles. Short operations using dummy cycles,
2-byte opcodes, multi-wire buswidths, or DTR are sent through
dw_spi_g2_reg_op() instead of dw_spi_g2_indirect_read(), and commit
"spi: dw-g2: Add SPI-NAND support" widens dw_spi_g2_is_reg_op() to match
addressed operations up to fifo_len bytes with the same 1-1-1 restriction.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=3
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 4/6] spi: dw-g2: Add SPI-NAND support
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
` (2 preceding siblings ...)
2026-11-19 10:05 ` [PATCH 3/6] spi: dw-g2: Add indirect read and write Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:25 ` sashiko-bot
2026-11-19 10:05 ` [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties Prakriti Sharma
2026-11-19 10:05 ` [PATCH 6/6] spi: dw-g2: Add XIP support Prakriti Sharma
5 siblings, 1 reply; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
Handle SPI-NAND flash in the same spi-mem path as SPI-NOR.
NAND uses a 2-byte address and program-load writes land in the page
buffer, so short operations such as JEDEC ID and status reads take
the register path. A short delay after chip-select assertion, plus
barriers around that write, keeps the ID read ordered on this
integration.
Tested with a Winbond W25N01JW SPI-NAND flash.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
drivers/spi/spi-dw-g2-core.c | 370 +++++++++++++++++++++++++----------
drivers/spi/spi-dw-g2.h | 20 +-
2 files changed, 277 insertions(+), 113 deletions(-)
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index c640d6a19260..a94d4a7870df 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -262,9 +262,13 @@ static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)
* dw_spi_g2_get_tt - Determine transfer type from operation bus widths
* @op: SPI memory operation
*
- * The transfer type (TT) field in SPI_CTRLR0 defines which phases use
- * multi-line mode. TT0 = standard (1-1-1), TT1 = data multi-line (1-1-x),
- * TT2 = addr+data multi-line (1-x-x). TT3 (x-x-x) is not supported.
+ * TRANS_TYPE in SPI_CTRLR0 / XIP_CTRL controls only the instruction and
+ * address phases. Data always uses the enhanced mode set by SPI_FRF in
+ * CTRLR0 (or FRF in XIP_CTRL).
+ *
+ * TT0 (0x0): instruction standard, address standard (1-1-x)
+ * TT1 (0x1): instruction standard, address enhanced (1-x-x)
+ * TT2 (0x2): instruction enhanced, address enhanced (x-x-x)
*
* Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns
*/
@@ -272,29 +276,37 @@ static int dw_spi_g2_get_tt(const struct spi_mem_op *op)
{
bool cmd_m = (op->cmd.buswidth > 1);
bool addr_m = (op->addr.nbytes && op->addr.buswidth > 1);
- bool data_m = (op->data.nbytes && op->data.buswidth > 1);
- if (!cmd_m && !addr_m && !data_m)
- return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-1 */
- if (!cmd_m && !addr_m && data_m)
- return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-1-x */
- if (!cmd_m && addr_m && data_m)
- return DW_SPI_G2_TRANS_TYPE_TT2; /* 1-x-x */
+ if (!cmd_m && !addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-x */
+ if (!cmd_m && addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-x-x */
+ if (cmd_m && addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT2; /* x-x-x */
return -EOPNOTSUPP;
}
/**
- * dw_spi_g2_is_reg_op - Check if operation is a register read/write
+ * dw_spi_g2_is_reg_op - Check if operation fits in TR (transmit-receive) mode
+ * @dws: Driver state (provides runtime fifo_len)
* @op: SPI memory operation
*
- * Register operations are single-byte commands with no address (e.g.,
- * RDSR, WRSR, RDID). Data must fit within the fixed-size stack buffer.
+ * Routes operations through TR mode when total transfer size (cmd + addr +
+ * dummy + data) fits within the FIFO. This avoids RO mode with SPI_CTRLR0
+ * (enhanced SPI framing) which some platforms don't fully support.
+ *
+ * Limit is min(fifo_len, compile-time buffer size) to stay within both
+ * the hardware FIFO and the stack-allocated buffers in reg_op().
*/
-static inline bool dw_spi_g2_is_reg_op(const struct spi_mem_op *op)
+static inline bool dw_spi_g2_is_reg_op(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
{
- return !op->addr.nbytes && op->data.nbytes > 0 &&
- op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
+ u32 total = op->cmd.nbytes + op->addr.nbytes +
+ op->dummy.nbytes + op->data.nbytes;
+ u32 limit = min_t(u32, dws->fifo_len, DW_SPI_G2_REG_OP_BUF_SIZE);
+
+ return op->data.nbytes > 0 && total <= limit;
}
/**
@@ -483,49 +495,132 @@ static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws,
static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
{
- u8 tx_buf[DW_SPI_G2_REG_OP_MAX_DATA + 1];
- u32 tx_len;
- int ret;
-
- /* Validate data length fits in stack buffer */
- if (op->data.nbytes > DW_SPI_G2_REG_OP_MAX_DATA) {
- dev_err(dws->dev, "reg_op data length %u exceeds max %u\n",
- op->data.nbytes, DW_SPI_G2_REG_OP_MAX_DATA);
+ u8 tx_buf[DW_SPI_G2_REG_OP_BUF_SIZE];
+ u32 tx_len = 0;
+ u32 echo_bytes;
+ int ret = 0;
+ u32 i;
+
+ if (op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes +
+ op->data.nbytes > sizeof(tx_buf)) {
+ dev_err(dws->dev, "reg_op transfer too large: cmd=%u addr=%u dummy=%u data=%u\n",
+ op->cmd.nbytes, op->addr.nbytes, op->dummy.nbytes,
+ op->data.nbytes);
return -EINVAL;
}
+ dev_dbg(dws->dev, "reg_op: cmd=0x%02x dir=%d data_len=%u\n",
+ op->cmd.opcode, op->data.dir, op->data.nbytes);
+
dw_spi_g2_reg_op_setup(dws, op);
- dw_spi_g2_transfer_begin(dws);
+ /* Build complete TX buffer: [cmd] [addr] [dummy] [data/zeros] */
+ tx_buf[tx_len++] = op->cmd.opcode;
+
+ for (i = op->addr.nbytes; i > 0; i--)
+ tx_buf[tx_len++] = (op->addr.val >> ((i - 1) * 8)) & 0xFF;
if (op->data.dir == SPI_MEM_DATA_OUT) {
if (!op->data.buf.out) {
dev_err(dws->dev, "TX buffer is NULL\n");
return -EINVAL;
}
- /* TX: [opcode] [data bytes] */
- tx_buf[0] = op->cmd.opcode;
- memcpy(&tx_buf[1], op->data.buf.out, op->data.nbytes);
- tx_len = 1 + op->data.nbytes;
- ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_len, NULL, 0);
+ memcpy(&tx_buf[tx_len], op->data.buf.out, op->data.nbytes);
+ tx_len += op->data.nbytes;
} else {
if (!op->data.buf.in) {
dev_err(dws->dev, "RX buffer is NULL\n");
return -EINVAL;
}
- /*
- * TR mode: each TX byte produces one RX byte.
- * Push opcode + nbytes dummy 0x00s, receive 1+nbytes,
- * discard first RX byte (echo of opcode phase).
- */
- u8 rx_tmp[DW_SPI_G2_REG_OP_MAX_DATA + 1];
-
- tx_buf[0] = op->cmd.opcode;
- memset(&tx_buf[1], 0x00, op->data.nbytes);
- tx_len = 1 + op->data.nbytes;
- ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_len, rx_tmp, tx_len);
- if (!ret)
- memcpy(op->data.buf.in, &rx_tmp[1], op->data.nbytes);
+ if (op->dummy.nbytes) {
+ memset(&tx_buf[tx_len], 0x00, op->dummy.nbytes);
+ tx_len += op->dummy.nbytes;
+ }
+ memset(&tx_buf[tx_len], 0x00, op->data.nbytes);
+ tx_len += op->data.nbytes;
+ }
+
+ echo_bytes = 1 + op->addr.nbytes + op->dummy.nbytes;
+
+ dev_dbg(dws->dev, "reg_op: tx_len=%u echo=%u\n", tx_len, echo_bytes);
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ /*
+ * Write TX data to FIFO with TFNF polling.
+ * Although tx_len is limited by dw_spi_g2_is_reg_op() to fifo_len,
+ * we still poll TFNF for safety and to handle any hardware quirks.
+ */
+ for (i = 0; i < tx_len; i++) {
+ u32 timeout = DW_SPI_G2_TIMEOUT_US;
+
+ /* Wait for TX FIFO to have space */
+ while (!(dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR) & DW_SPI_G2_SR_TFNF)) {
+ if (--timeout == 0) {
+ dev_err(dws->dev, "reg_op: TX FIFO full timeout\n");
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ return -ETIMEDOUT;
+ }
+ udelay(1);
+ }
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, tx_buf[i]);
+ }
+
+ /* Ensure all FIFO writes complete before CS assertion */
+ wmb();
+
+ dw_spi_g2_cs_assert(dws, dws->cs);
+
+ /*
+ * CS settling delay for NAND flash:
+ *
+ * NAND flash requires a settling period after CS assertion before
+ * data transactions can begin reliably. This is a hardware timing requirement.
+ *
+ * This is specific to NAND flash behavior where CS-to-valid-data timing
+ * is more stringent than NOR flash. The delay applies to small
+ * register operations (reg_op path) which are commonly used for:
+ * - READID (0x9F)
+ * - GET_FEATURE (0x0F)
+ * - SET_FEATURE (0x1F)
+ * - READ_STATUS (0x0F with feature address)
+ *
+ * Note: Only applies to NAND (detected via DT "spi-nand" compatible)
+ * to avoid performance impact on NOR flash.
+ */
+ if (dws->is_nand) {
+ /* Force a register read to ensure CS assertion propagates */
+ (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+ usleep_range(10, 20);
+ }
+
+ dw_spi_g2_wait_idle(dws);
+
+ /* For TR mode reads, harvest RX FIFO after the burst completes */
+ if (op->data.dir == SPI_MEM_DATA_IN) {
+ u32 rxflr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR);
+ u32 rx_got = min_t(u32, rxflr, tx_len);
+
+ /* Ensure RXFLR read completes before reading RX FIFO */
+ rmb();
+
+ for (i = 0; i < rx_got; i++) {
+ u8 val = dw_spi_g2_readl(dws->regs,
+ DW_SPI_G2_DR0) & 0xFF;
+ if (i >= echo_bytes &&
+ (i - echo_bytes) < op->data.nbytes)
+ ((u8 *)op->data.buf.in)[i - echo_bytes] = val;
+ }
+
+ if (rx_got < echo_bytes + op->data.nbytes) {
+ dev_err(dws->dev,
+ "reg_op RX short: got %u, need %u\n",
+ rx_got, echo_bytes + op->data.nbytes);
+ ret = -EIO;
+ }
}
dw_spi_g2_transfer_end(dws);
@@ -615,14 +710,17 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
* @dws: Driver state
* @op: SPI memory operation
*
- * Reads data from flash using RO (receive-only) mode.
+ * Reads data from flash using RO (receive-only) mode with enhanced SPI
+ * framing via SPI_CTRLR0.
* Return: 0 on success, negative error code on failure
*/
static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
const struct spi_mem_op *op)
{
- u32 tx_entries;
+ struct spi_mem_op chunk_op;
u8 dummy_buf = 0;
+ u32 total, done = 0;
+ u32 tx_entries;
int ret;
if (!op->data.nbytes) {
@@ -635,35 +733,67 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
return -EINVAL;
}
- ret = dw_spi_g2_indirect_read_setup(dws, op);
- if (ret)
- return ret;
+ total = op->data.nbytes;
+ dev_dbg(dws->dev, "indirect_read: cmd=0x%02x addr=0x%llx len=%u\n",
+ op->cmd.opcode, op->addr.val, total);
- dw_spi_g2_target_enable(dws, dws->cs);
- dw_spi_g2_enable(dws, true);
+ /*
+ * Split into chunks no larger than dws->fifo_len so the RX FIFO
+ * never overflows. Each iteration reprograms CTRLR1/NDF and
+ * re-issues cmd+addr with the column advanced. A single
+ * >fifo_len request leaves the controller idle (SR.BUSY=0,
+ * RXFLR=0) after the first FIFO worth of data, causing an
+ * RX timeout.
+ */
+ chunk_op = *op;
+
+ while (done < total) {
+ u32 chunk = min_t(u32, total - done, dws->fifo_len);
+
+ chunk_op.addr.val = op->addr.val + done;
+ chunk_op.data.nbytes = chunk;
+ chunk_op.data.buf.in = (u8 *)op->data.buf.in + done;
- tx_entries = 0;
+ ret = dw_spi_g2_indirect_read_setup(dws, &chunk_op);
+ if (ret)
+ return ret;
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
- /* Write instruction */
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
- tx_entries++;
+ tx_entries = 0;
- /* Write address as a packed 32-bit value (one FIFO entry) */
- if (op->addr.nbytes) {
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
+ /* Write instruction to DR0 */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
+ chunk_op.cmd.opcode);
tx_entries++;
- }
- /* Assert CS - controller begins: sends TX FIFO, then receives */
- dw_spi_g2_cs_assert(dws, dws->cs);
+ /*
+ * Write address as a single packed value.
+ * In enhanced SPI framing (RO mode with SPI_CTRLR0), the
+ * hardware extracts ADDR_L bits from this write.
+ */
+ if (chunk_op.addr.nbytes) {
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
+ (u32)chunk_op.addr.val);
+ tx_entries++;
+ }
- /* Receive data via polling */
- ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
- op->data.buf.in, op->data.nbytes);
- dw_spi_g2_transfer_end(dws);
- dw_spi_g2_controller_reset(dws);
+ /* Assert CS - controller begins: sends TX FIFO, then receives */
+ dw_spi_g2_cs_assert(dws, dws->cs);
- return ret;
+ /* Receive data via polling */
+ ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
+ chunk_op.data.buf.in, chunk);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ if (ret)
+ return ret;
+
+ done += chunk;
+ }
+
+ return 0;
}
/**
@@ -676,20 +806,26 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
* @dws: Driver state
* @op: SPI memory operation
*
- * Sets up TMOD=TO (transmit-only), standard SPI mode.
+ * Sets up TMOD=TO (transmit-only)
*/
static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
const struct spi_mem_op *op)
{
u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TO,
DW_SPI_G2_FRF_STANDARD);
+ u32 spictrlr0 = 0;
dw_spi_g2_controller_reset(dws);
dw_spi_g2_target_disable(dws, dws->cs);
+ if (op->cmd.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
+ if (op->addr.dtr || op->data.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
+
dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
}
@@ -699,8 +835,12 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
* @dws: Driver state
* @op: SPI memory operation
*
- * Writes data to flash using TO (transmit-only) mode. The controller sends
- * instruction, address, and data bytes as a continuous stream.
+ * Writes data to flash using TMOD=TO (transmit-only).
+ * NOTE: CS is asserted before SSI enable for NAND compatibility in VDK.
+ * This non-standard sequence is required because:
+ * 1. Controller reset in setup clears FIFO (can't prefill while disabled)
+ * 2. Enabling SSI before CS causes 0x00 transmission from empty FIFO
+ * 3. NAND Program Load requires data immediately when CS asserts
*
* Return: 0 on success, negative error code on failure
*/
@@ -709,7 +849,7 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
{
u32 tx_total = op->cmd.nbytes + op->addr.nbytes + op->data.nbytes;
u8 *tx_buf;
- u32 prefill, pos;
+ int ret = 0;
if (!op->data.nbytes) {
dev_err(dws->dev, "Zero-length write not allowed\n");
@@ -721,52 +861,36 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
return -EINVAL;
}
+ dev_dbg(dws->dev, "indirect_write: cmd=0x%02x addr=0x%llx len=%u\n",
+ op->cmd.opcode, op->addr.val, op->data.nbytes);
+
dw_spi_g2_indirect_write_setup(dws, op);
- /* Allocate and build TX buffer: [cmd][addr][data] */
tx_buf = kzalloc(tx_total, GFP_KERNEL);
if (!tx_buf)
return -ENOMEM;
dw_spi_g2_fill_tx_buf(tx_buf, op, true);
- /*
- * Split enable sequence:
- * Enable SSI (without CS), pre-fill TX FIFO, then assert CS.
- * FIFO pointers are reset while SSIENR=0, so writes before
- * enabling would be discarded.
- */
dw_spi_g2_target_enable(dws, dws->cs);
- dw_spi_g2_enable(dws, true);
-
- /* Pre-fill FIFO to capacity or total size */
- prefill = min_t(u32, tx_total, dws->fifo_len);
- for (pos = 0; pos < prefill; pos++)
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, tx_buf[pos]);
-
- /* Assert CS - transfer starts */
dw_spi_g2_cs_assert(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
- /* Refill FIFO with remaining bytes (if any) via polling */
- if (pos < tx_total) {
- int ret = dw_spi_g2_xfer_fifo(dws, &tx_buf[pos],
- tx_total - pos, NULL, 0);
- if (ret) {
- dw_spi_g2_transfer_end(dws);
- dw_spi_g2_controller_reset(dws);
- kfree(tx_buf);
- return ret;
- }
+ ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_total, NULL, 0);
+ if (ret) {
+ dev_err(dws->dev,
+ "indirect_write: xfer_fifo failed: %d\n", ret);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ kfree(tx_buf);
+ return ret;
}
- /* Wait for transfer completion */
- dw_spi_g2_wait_idle(dws);
-
dw_spi_g2_transfer_end(dws);
dw_spi_g2_controller_reset(dws);
kfree(tx_buf);
- return 0;
+ return ret;
}
/*
@@ -778,9 +902,46 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
const struct spi_mem_op *op)
{
+ struct spi_device *spi = mem->spi;
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+ bool op_is_dtr =
+ op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
+
+ /*
+ * Detect NAND flash by checking device tree compatible string.
+ * This is used for VDK timing workarounds.
+ */
+ if (spi->dev.of_node) {
+ if (of_device_is_compatible(spi->dev.of_node, "spi-nand"))
+ dws->is_nand = true;
+ }
+
+ if (op_is_dtr) {
+ /*
+ * DTR requires enhanced SPI modes (Dual/Quad/Octal).
+ * Reject standard-width DTR since it has no practical use
+ * and the hardware doesn't support it meaningfully.
+ */
+ if (op->data.nbytes && op->data.buswidth == 1)
+ return false;
+
+ /*
+ * In DTR mode all phases must agree on DTR — reject
+ * mixed STR/DTR within a single operation.
+ */
+ if (op->addr.nbytes && op->addr.dtr != op->cmd.dtr)
+ return false;
+ if (op->data.nbytes && op->data.dtr != op->cmd.dtr)
+ return false;
+ }
+
return spi_mem_default_supports_op(mem, op);
}
+static const struct spi_controller_mem_caps dw_spi_g2_mem_caps = {
+ .dtr = true,
+};
+
int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
{
if (op->data.dir == SPI_MEM_DATA_IN)
@@ -846,15 +1007,13 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
dws->cs = spi_get_chipselect(spi, 0);
dws->curr_spi = spi;
- dev_dbg(dws->dev,
- "exec_mem_op: cmd=0x%02x addr_len=%u data_len=%u dir=%d\n",
- op->cmd.opcode, op->addr.nbytes, op->data.nbytes,
- op->data.dir);
+ dev_dbg(dws->dev, "exec_op: cmd=0x%02x addr=0x%llx len=%u dir=%d\n",
+ op->cmd.opcode, op->addr.val, op->data.nbytes, op->data.dir);
if (op->data.dir == SPI_MEM_NO_DATA)
return dw_spi_g2_cmd_only(dws, op);
- if (dw_spi_g2_is_reg_op(op))
+ if (dw_spi_g2_is_reg_op(dws, op))
return dw_spi_g2_reg_op(dws, op);
if (op->data.dir == SPI_MEM_DATA_IN)
@@ -943,6 +1102,7 @@ int dw_spi_g2_init(struct dw_spi_g2 *dws)
ctlr->setup = dw_spi_g2_setup;
ctlr->mem_ops = &dw_spi_g2_mem_ops;
+ ctlr->mem_caps = &dw_spi_g2_mem_caps;
dw_spi_g2_debugfs_init(dws);
init_completion(&dws->mem_done);
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
index 1106be0af7b6..3091697238d7 100644
--- a/drivers/spi/spi-dw-g2.h
+++ b/drivers/spi/spi-dw-g2.h
@@ -186,13 +186,8 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)
return addr_nbytes ? addr_nbytes * 2U : 0U;
}
-/*
- * Maximum chunk size for indirect reads.
- * 64KB allows efficient large transfers while staying well within typical
- * controller limits. The FIFO (256 bytes) drains continuously during
- * the transfer via polling.
- */
-#define DW_G2_MAX_CHUNK 256U
+/* Maximum data chunk per spi_mem_op. */
+#define DW_G2_MAX_CHUNK (64 * 1024U)
/* Timeout in microseconds for polling operations */
#define DW_SPI_G2_TIMEOUT_US 200000U
@@ -222,7 +217,13 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)
* - etc.
* Larger transfers use indirect read/write with FIFO (up to DW_G2_MAX_CHUNK).
*/
-#define DW_SPI_G2_REG_OP_MAX_DATA 15U
+/*
+ * Maximum total transfer size (cmd + addr + dummy + data) for reg_op
+ * (TR mode). Must fit within both the TX and RX FIFOs. Each FIFO has
+ * fifo_len entries (typically 256); we use a compile-time cap here and
+ * check against the runtime fifo_len in is_reg_op().
+ */
+ #define DW_SPI_G2_REG_OP_BUF_SIZE 256U
struct dw_spi_g2_cfg {
u8 tmode;
@@ -259,6 +260,9 @@ struct dw_spi_g2 {
struct completion mem_done;
int error;
+ /* Flash type detection for timing workarounds */
+ bool is_nand;
+
#ifdef CONFIG_DEBUG_FS
struct dentry *debugfs;
struct debugfs_regset32 regset;
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 4/6] spi: dw-g2: Add SPI-NAND support
2026-11-19 10:05 ` [PATCH 4/6] spi: dw-g2: Add SPI-NAND support Prakriti Sharma
@ 2026-11-19 10:25 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-19 10:25 UTC (permalink / raw)
To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] spi: dw-g2: dw_spi_g2_is_reg_op() routes multi-IO and DTR ops
to 1-1-1 reg_op
- [High] spi: dw-g2: hidden CS-toggling chunks in
dw_spi_g2_indirect_read() break NAND
- [High] spi: dw-g2: dw_spi_g2_reg_op() races SR_BUSY after CS assert
and drops errors
- [High] spi: dw-g2: broken multi-IO/DTR writes, DTR wait cycles, and op
validation
- [Medium] spi: dw-g2: dw_spi_g2_indirect_write() removes FIFO prefill
before CS assert
- [Medium] spi: dw-g2: dws->is_nand is mutated unlocked and sticky
across chip selects
- [Low] spi: dw-g2: unused write-only variable tx_entries in
dw_spi_g2_indirect_read()
--
commit 9669d454b276956e6dd4740dc840ec3191c46b35
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
spi: dw-g2: Add SPI-NAND support
This commit updates the DW SSI G2 spi-mem implementation to handle SPI-NAND
devices by routing FIFO-sized operations through TR mode, chunking indirect
reads, and adding DTR support and a NAND CS settling delay.
> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index c640d6a192600..a94d4a7870dfd 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
> @@ -262,9 +262,13 @@ static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)
> * dw_spi_g2_get_tt - Determine transfer type from operation bus widths
> * @op: SPI memory operation
> *
> - * The transfer type (TT) field in SPI_CTRLR0 defines which phases use
> - * multi-line mode. TT0 = standard (1-1-1), TT1 = data multi-line (1-1-x),
> - * TT2 = addr+data multi-line (1-x-x). TT3 (x-x-x) is not supported.
> + * TRANS_TYPE in SPI_CTRLR0 / XIP_CTRL controls only the instruction and
> + * address phases. Data always uses the enhanced mode set by SPI_FRF in
> + * CTRLR0 (or FRF in XIP_CTRL).
> + *
> + * TT0 (0x0): instruction standard, address standard (1-1-x)
> + * TT1 (0x1): instruction standard, address enhanced (1-x-x)
> + * TT2 (0x2): instruction enhanced, address enhanced (x-x-x)
> *
> * Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns
> */
> @@ -272,29 +276,37 @@ static int dw_spi_g2_get_tt(const struct spi_mem_op *op)
> {
> bool cmd_m = (op->cmd.buswidth > 1);
> bool addr_m = (op->addr.nbytes && op->addr.buswidth > 1);
> - bool data_m = (op->data.nbytes && op->data.buswidth > 1);
>
> - if (!cmd_m && !addr_m && !data_m)
> - return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-1 */
> - if (!cmd_m && !addr_m && data_m)
> - return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-1-x */
> - if (!cmd_m && addr_m && data_m)
> - return DW_SPI_G2_TRANS_TYPE_TT2; /* 1-x-x */
> + if (!cmd_m && !addr_m)
> + return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-x */
> + if (!cmd_m && addr_m)
> + return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-x-x */
> + if (cmd_m && addr_m)
> + return DW_SPI_G2_TRANS_TYPE_TT2; /* x-x-x */
>
> return -EOPNOTSUPP;
> }
>
> /**
> - * dw_spi_g2_is_reg_op - Check if operation is a register read/write
> + * dw_spi_g2_is_reg_op - Check if operation fits in TR (transmit-receive) mode
> + * @dws: Driver state (provides runtime fifo_len)
> * @op: SPI memory operation
> *
> - * Register operations are single-byte commands with no address (e.g.,
> - * RDSR, WRSR, RDID). Data must fit within the fixed-size stack buffer.
> + * Routes operations through TR mode when total transfer size (cmd + addr +
> + * dummy + data) fits within the FIFO. This avoids RO mode with SPI_CTRLR0
> + * (enhanced SPI framing) which some platforms don't fully support.
> + *
> + * Limit is min(fifo_len, compile-time buffer size) to stay within both
> + * the hardware FIFO and the stack-allocated buffers in reg_op().
> */
> -static inline bool dw_spi_g2_is_reg_op(const struct spi_mem_op *op)
> +static inline bool dw_spi_g2_is_reg_op(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> {
> - return !op->addr.nbytes && op->data.nbytes > 0 &&
> - op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
> + u32 total = op->cmd.nbytes + op->addr.nbytes +
> + op->dummy.nbytes + op->data.nbytes;
> + u32 limit = min_t(u32, dws->fifo_len, DW_SPI_G2_REG_OP_BUF_SIZE);
> +
> + return op->data.nbytes > 0 && total <= limit;
> }
[Severity: High]
Does dw_spi_g2_is_reg_op() route multi-IO, DTR, and 2-byte-opcode
operations up to 256 bytes into the 1-1-1 standard SPI path in
dw_spi_g2_reg_op()?
dw_spi_g2_reg_op_setup() hardcodes DW_SPI_G2_FRF_STANDARD and writes 0 to
DW_SPI_G2_SPI_CTRLR0, so any Dual, Quad, Octal, or DTR operation whose
total length fits in min(dws->fifo_len, 256) (such as a 64-byte SPI-NAND
OOB read using 1-1-4 Quad Read From Cache 0x6b) is clocked out on a single
data wire in STR mode.
In addition, dw_spi_g2_reg_op() serializes only one byte of op->cmd.opcode
and sets echo_bytes = 1 + op->addr.nbytes + op->dummy.nbytes (truncating
2-byte opcodes), drops op->dummy.nbytes on SPI_MEM_DATA_OUT, and clocks
dummy bytes as 1-bit data on SPI_MEM_DATA_IN. Conversely, a standard
1-1-1 read larger than dws->fifo_len falls through to
dw_spi_g2_indirect_read(), which sets DW_SPI_G2_FRF_STANDARD (where
SPI_CTRLR0 enhanced framing is inactive) while writing chunk_op.addr.val
as a single DR0 entry.
> @@ -483,49 +495,132 @@ static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws,
>
> static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
> {
[ ... ]
> + /* Ensure all FIFO writes complete before CS assertion */
> + wmb();
> +
> + dw_spi_g2_cs_assert(dws, dws->cs);
> +
> + /*
> + * CS settling delay for NAND flash:
> + *
> + * NAND flash requires a settling period after CS assertion before
> + * data transactions can begin reliably. This is a hardware timing requirement.
> + *
> + * This is specific to NAND flash behavior where CS-to-valid-data timing
> + * is more stringent than NOR flash. The delay applies to small
> + * register operations (reg_op path) which are commonly used for:
> + * - READID (0x9F)
> + * - GET_FEATURE (0x0F)
> + * - SET_FEATURE (0x1F)
> + * - READ_STATUS (0x0F with feature address)
> + *
> + * Note: Only applies to NAND (detected via DT "spi-nand" compatible)
> + * to avoid performance impact on NOR flash.
> + */
> + if (dws->is_nand) {
> + /* Force a register read to ensure CS assertion propagates */
> + (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> + usleep_range(10, 20);
> + }
> +
> + dw_spi_g2_wait_idle(dws);
> +
> + /* For TR mode reads, harvest RX FIFO after the burst completes */
> + if (op->data.dir == SPI_MEM_DATA_IN) {
> + u32 rxflr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR);
> + u32 rx_got = min_t(u32, rxflr, tx_len);
> +
> + /* Ensure RXFLR read completes before reading RX FIFO */
> + rmb();
> +
> + for (i = 0; i < rx_got; i++) {
> + u8 val = dw_spi_g2_readl(dws->regs,
> + DW_SPI_G2_DR0) & 0xFF;
> + if (i >= echo_bytes &&
> + (i - echo_bytes) < op->data.nbytes)
> + ((u8 *)op->data.buf.in)[i - echo_bytes] = val;
> + }
> +
> + if (rx_got < echo_bytes + op->data.nbytes) {
> + dev_err(dws->dev,
> + "reg_op RX short: got %u, need %u\n",
> + rx_got, echo_bytes + op->data.nbytes);
> + ret = -EIO;
> + }
> }
>
> dw_spi_g2_transfer_end(dws);
[Severity: High]
Can dw_spi_g2_wait_idle() return before DW_SPI_G2_SR_BUSY asserts after
dw_spi_g2_cs_assert()?
dw_spi_g2_wait_idle() only checks !(sr & DW_SPI_G2_SR_BUSY) without
checking DW_SPI_G2_SR_TFE. In DWC SSI hardware, writing SER does not set
SR_BUSY until SER synchronizes into the ssi_clk domain. When dws->is_nand
is false (such as on SPI-NOR), polling SR immediately after
dw_spi_g2_cs_assert() can observe SR_BUSY == 0 before shifting starts:
SPI_MEM_DATA_IN then reads RXFLR == 0 and fails with -EIO, while
SPI_MEM_DATA_OUT calls dw_spi_g2_transfer_end() and aborts the transfer
while the TX FIFO is still full.
Also, dw_spi_g2_wait_idle()'s return value is ignored, so a busy timeout
on SPI_MEM_DATA_OUT silently returns 0.
> @@ -615,14 +710,17 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
> * @dws: Driver state
> * @op: SPI memory operation
> *
> - * Reads data from flash using RO (receive-only) mode.
> + * Reads data from flash using RO (receive-only) mode with enhanced SPI
> + * framing via SPI_CTRLR0.
> * Return: 0 on success, negative error code on failure
> */
> static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> const struct spi_mem_op *op)
> {
> - u32 tx_entries;
> + struct spi_mem_op chunk_op;
> u8 dummy_buf = 0;
> + u32 total, done = 0;
> + u32 tx_entries;
> int ret;
>
> if (!op->data.nbytes) {
> @@ -635,35 +733,67 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> return -EINVAL;
> }
>
> - ret = dw_spi_g2_indirect_read_setup(dws, op);
> - if (ret)
> - return ret;
> + total = op->data.nbytes;
> + dev_dbg(dws->dev, "indirect_read: cmd=0x%02x addr=0x%llx len=%u\n",
> + op->cmd.opcode, op->addr.val, total);
>
> - dw_spi_g2_target_enable(dws, dws->cs);
> - dw_spi_g2_enable(dws, true);
> + /*
> + * Split into chunks no larger than dws->fifo_len so the RX FIFO
> + * never overflows. Each iteration reprograms CTRLR1/NDF and
> + * re-issues cmd+addr with the column advanced. A single
> + * >fifo_len request leaves the controller idle (SR.BUSY=0,
> + * RXFLR=0) after the first FIFO worth of data, causing an
> + * RX timeout.
> + */
> + chunk_op = *op;
> +
> + while (done < total) {
> + u32 chunk = min_t(u32, total - done, dws->fifo_len);
> +
> + chunk_op.addr.val = op->addr.val + done;
> + chunk_op.data.nbytes = chunk;
> + chunk_op.data.buf.in = (u8 *)op->data.buf.in + done;
>
> - tx_entries = 0;
> + ret = dw_spi_g2_indirect_read_setup(dws, &chunk_op);
> + if (ret)
> + return ret;
> +
> + dw_spi_g2_target_enable(dws, dws->cs);
> + dw_spi_g2_enable(dws, true);
>
> - /* Write instruction */
> - dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
> - tx_entries++;
> + tx_entries = 0;
>
> - /* Write address as a packed 32-bit value (one FIFO entry) */
> - if (op->addr.nbytes) {
> - dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
> + /* Write instruction to DR0 */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
> + chunk_op.cmd.opcode);
> tx_entries++;
> - }
>
> - /* Assert CS - controller begins: sends TX FIFO, then receives */
> - dw_spi_g2_cs_assert(dws, dws->cs);
> + /*
> + * Write address as a single packed value.
> + * In enhanced SPI framing (RO mode with SPI_CTRLR0), the
> + * hardware extracts ADDR_L bits from this write.
> + */
> + if (chunk_op.addr.nbytes) {
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
> + (u32)chunk_op.addr.val);
> + tx_entries++;
> + }
[Severity: Low]
tx_entries is assigned and incremented here, but never read, which triggers
a -Wunused-but-set-variable compiler warning.
>
> - /* Receive data via polling */
> - ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
> - op->data.buf.in, op->data.nbytes);
> - dw_spi_g2_transfer_end(dws);
> - dw_spi_g2_controller_reset(dws);
> + /* Assert CS - controller begins: sends TX FIFO, then receives */
> + dw_spi_g2_cs_assert(dws, dws->cs);
>
> - return ret;
> + /* Receive data via polling */
> + ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
> + chunk_op.data.buf.in, chunk);
> + dw_spi_g2_transfer_end(dws);
> + dw_spi_g2_controller_reset(dws);
> + if (ret)
> + return ret;
> +
> + done += chunk;
> + }
> +
> + return 0;
> }
[Severity: High]
Won't splitting a single spi_mem_op across multiple CS assertions here while
raising DW_G2_MAX_CHUNK to 64 KB break SPI-NAND continuous reads?
In drivers/mtd/nand/spi/core.c, spinand_read_from_cache_op() relies on
adjust_op_size() clamping op->data.nbytes to the controller's single-CS
transfer limit so it can fall back to buffered page reads when a continuous
read cannot complete in one CS assertion. On Winbond W25N01JW, continuous
read is enabled by default and uses a 1-byte dummy address (addr.nbytes = 1,
addr.val = 0) while streaming from column 0 for as long as CS stays low;
toggling CS every dws->fifo_len bytes aborts the continuous stream in the
flash and overflows the 1-byte dummy address once done >= 256.
If the controller cannot receive more than dws->fifo_len bytes in one CS
assertion, dw_spi_g2_adjust_mem_op_size() should clamp SPI_MEM_DATA_IN to
dws->fifo_len so the spi-mem and spinand callers advance the address and
manage CS boundaries themselves.
> @@ -676,20 +806,26 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> * @dws: Driver state
> * @op: SPI memory operation
> *
> - * Sets up TMOD=TO (transmit-only), standard SPI mode.
> + * Sets up TMOD=TO (transmit-only)
> */
> static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
> const struct spi_mem_op *op)
> {
> u32 ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TO,
> DW_SPI_G2_FRF_STANDARD);
> + u32 spictrlr0 = 0;
>
> dw_spi_g2_controller_reset(dws);
> dw_spi_g2_target_disable(dws, dws->cs);
>
> + if (op->cmd.dtr)
> + spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
> + if (op->addr.dtr || op->data.dtr)
> + spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
> +
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> - dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
> }
[ ... ]
> @@ -721,52 +861,36 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
> return -EINVAL;
> }
>
> + dev_dbg(dws->dev, "indirect_write: cmd=0x%02x addr=0x%llx len=%u\n",
> + op->cmd.opcode, op->addr.val, op->data.nbytes);
> +
> dw_spi_g2_indirect_write_setup(dws, op);
>
> - /* Allocate and build TX buffer: [cmd][addr][data] */
> tx_buf = kzalloc(tx_total, GFP_KERNEL);
> if (!tx_buf)
> return -ENOMEM;
>
> dw_spi_g2_fill_tx_buf(tx_buf, op, true);
>
> - /*
> - * Split enable sequence:
> - * Enable SSI (without CS), pre-fill TX FIFO, then assert CS.
> - * FIFO pointers are reset while SSIENR=0, so writes before
> - * enabling would be discarded.
> - */
> dw_spi_g2_target_enable(dws, dws->cs);
> - dw_spi_g2_enable(dws, true);
> -
> - /* Pre-fill FIFO to capacity or total size */
> - prefill = min_t(u32, tx_total, dws->fifo_len);
> - for (pos = 0; pos < prefill; pos++)
> - dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, tx_buf[pos]);
> -
> - /* Assert CS - transfer starts */
> dw_spi_g2_cs_assert(dws, dws->cs);
> + dw_spi_g2_enable(dws, true);
>
> - /* Refill FIFO with remaining bytes (if any) via polling */
> - if (pos < tx_total) {
> - int ret = dw_spi_g2_xfer_fifo(dws, &tx_buf[pos],
> - tx_total - pos, NULL, 0);
> - if (ret) {
> - dw_spi_g2_transfer_end(dws);
> - dw_spi_g2_controller_reset(dws);
> - kfree(tx_buf);
> - return ret;
> - }
> + ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_total, NULL, 0);
[Severity: Medium]
Can asserting CS and enabling SSI before pre-filling the TX FIFO cause a TX
FIFO underrun that deasserts CS mid-write?
With SER and SSIENR both active, DWC SSI starts shifting as soon as the
first byte is written to DR0 in dw_spi_g2_xfer_fifo(), and automatically
deasserts CS if the TX FIFO drains empty before the next byte arrives.
Because dw_spi_g2_xfer_fifo() runs in preemptible context and reads
DW_SPI_G2_SR before each byte write, an interrupt or scheduling delay after
the opcode byte can drain the 1-byte FIFO and toggle CS during a page
program.
> @@ -778,9 +902,46 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
> static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
> const struct spi_mem_op *op)
> {
> + struct spi_device *spi = mem->spi;
> + struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> + bool op_is_dtr =
> + op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
> +
> + /*
> + * Detect NAND flash by checking device tree compatible string.
> + * This is used for VDK timing workarounds.
> + */
> + if (spi->dev.of_node) {
> + if (of_device_is_compatible(spi->dev.of_node, "spi-nand"))
> + dws->is_nand = true;
> + }
[Severity: Medium]
Isn't dws->is_nand controller-wide state that gets latched permanently across
all chip selects and modified without holding any controller lock?
supports_op() is called before spi_mem_access_start() acquires the bus
locks, and once any child node matches "spi-nand", dws->is_nand remains
true even when talking to a non-NAND device on another chip select.
> +
> + if (op_is_dtr) {
> + /*
> + * DTR requires enhanced SPI modes (Dual/Quad/Octal).
> + * Reject standard-width DTR since it has no practical use
> + * and the hardware doesn't support it meaningfully.
> + */
> + if (op->data.nbytes && op->data.buswidth == 1)
> + return false;
> +
> + /*
> + * In DTR mode all phases must agree on DTR — reject
> + * mixed STR/DTR within a single operation.
> + */
> + if (op->addr.nbytes && op->addr.dtr != op->cmd.dtr)
> + return false;
> + if (op->data.nbytes && op->data.dtr != op->cmd.dtr)
> + return false;
> + }
> +
> return spi_mem_default_supports_op(mem, op);
> }
[Severity: High]
Does dw_spi_g2_supports_mem_ops() advertise multi-IO and DTR write and
command-only operations that the driver only transmits in 1-1-1 STR mode?
ctlr->mode_bits includes SPI_TX_DUAL, SPI_TX_QUAD, and SPI_TX_OCTAL, and
dw_spi_g2_mem_caps enables .dtr = true, so dw_spi_g2_supports_mem_ops()
accepts operations such as Winbond W25N01JW's 1-1-4 Program Load (0x32) or
8D-8D-8D writes. However, dw_spi_g2_indirect_write_setup() and
dw_spi_g2_cmd_only_setup() both hardcode DW_SPI_G2_FRF_STANDARD (where the
DDR bits written to SPI_CTRLR0 are ignored) and leave INST_L and ADDR_L at
0, sending all bytes over a single wire.
There are two related DTR and buswidth issues in this path:
1. In dw_spi_g2_indirect_read_setup() and dw_spi_g2_exec_mem_op(),
dummy_cycles is computed as (op->dummy.nbytes * 8) / op->dummy.buswidth
without halving for DTR mode (later partially adjusted in commit
"spi: dw-g2: Add XIP support"), programming twice the required wait
cycles.
2. dw_spi_g2_supports_mem_ops() does not verify that op->dummy.dtr matches
op->cmd.dtr, that cmd/addr buswidth > 1 is rejected when
op->data.nbytes == 0, or that multi-line phase buswidths are supported by
dw_spi_g2_get_tt() and the single CTRLR0.FRF field (for example,
cmd.buswidth > 1 with addr.nbytes == 0 passes supports_op() and then
fails in dw_spi_g2_get_tt() with -EOPNOTSUPP).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=4
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
` (3 preceding siblings ...)
2026-11-19 10:05 ` [PATCH 4/6] spi: dw-g2: Add SPI-NAND support Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:14 ` sashiko-bot
2026-11-19 10:05 ` [PATCH 6/6] spi: dw-g2: Add XIP support Prakriti Sharma
5 siblings, 1 reply; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
Document the optional XIP memory region and the properties that select
execute-in-place, continuous mode, and prefetch.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
.../bindings/spi/snps,dwc-ssi-2.02a.yaml | 59 ++++++++++++++++++-
1 file changed, 57 insertions(+), 2 deletions(-)
diff --git a/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
index 7cccd627f809..efbeb59313fa 100644
--- a/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
+++ b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
@@ -23,7 +23,18 @@ properties:
const: snps,dwc-ssi-2.02a
reg:
- maxItems: 1
+ minItems: 1
+ maxItems: 2
+ description: |
+ Controller register base and optional XIP memory region.
+ - First entry: SSI controller register space
+ - Second entry (optional): XIP memory-mapped region for direct flash access
+
+ reg-names:
+ minItems: 1
+ items:
+ - const: ssi_base
+ - const: xip_mem
interrupts:
description: |
@@ -93,6 +104,48 @@ properties:
of the designware controller, and the upper limit is also subject to
controller configuration.
+ snps,xip-enabled:
+ type: boolean
+ description: |
+ Enable XIP (eXecute-In-Place) mode for direct memory-mapped access to
+ SPI flash. When enabled, the controller's XIP region is mapped into
+ CPU address space for zero-copy reads and writes. It requires the
+ xip_mem entry in reg.
+
+ XIP is used only when this property is present and the module
+ parameter spi_dw_g2_mmio.xip_mode=1 is set. The default xip_mode=0
+ disables XIP. Runtime control is available through sysfs.
+
+ snps,xip-continuous-mode:
+ type: boolean
+ description: |
+ Enable XIP continuous transfer mode. When enabled, the controller omits
+ the command byte on subsequent sequential reads from the same chip select,
+ saving 8 SPI clock cycles per access. This improves throughput for code
+ execution from XIP flash.
+
+ Requirements:
+ - Flash device must support continuous read mode (e.g., Fast Read Quad
+ I/O with mode bits)
+ - Must use consistent command across all accesses
+ - Not compatible with mixed command types
+
+ The controller automatically re-sends the command when CS is de-asserted
+ or transfer type changes.
+
+ snps,xip-prefetch-mode:
+ type: boolean
+ description: |
+ Enable XIP hardware prefetch mode. The controller speculatively fetches
+ the next cache line when the current access completes, anticipating
+ sequential reads. This reduces latency for instruction fetches during
+ XIP code execution.
+
+ Best for: Sequential code execution, linear data reads
+ Not ideal for: Random access patterns, small sporadic reads
+
+ Prefetch is only active when XIP direct mapping is enabled.
+
patternProperties:
"^.*@[0-9a-f]+$":
type: object
@@ -121,7 +174,8 @@ examples:
- |
spi@20100000 {
compatible = "snps,dwc-ssi-2.02a";
- reg = <0x20100000 0x1000>;
+ reg = <0x20100000 0x1000>, <0x60000000 0x10000000>;
+ reg-names = "ssi_base", "xip_mem";
#address-cells = <1>;
#size-cells = <0>;
interrupts = <0 42 4>;
@@ -131,6 +185,7 @@ examples:
dmas = <&dma0 10>, <&dma0 11>;
dma-names = "tx", "rx";
rx-sample-delay-ns = <3>;
+ snps,xip-enabled;
flash@0 {
compatible = "jedec,spi-nor";
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties
2026-11-19 10:05 ` [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties Prakriti Sharma
@ 2026-11-19 10:14 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-19 10:14 UTC (permalink / raw)
To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] dt-bindings: spi: snps,dwc-ssi-2.02a: XIP properties encode
OS policy
- [Low] dt-bindings: spi: snps,dwc-ssi-2.02a: missing reg items and XIP
dependencies
--
commit ea2effa98a511e888bf97a068c9c5ac14822f4eb
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
dt-bindings: spi: Document DWC SSI XIP properties
This commit documents an optional XIP memory region and boolean properties
for execute-in-place, continuous mode, and prefetch in the Synopsys DWC SSI
v2.02a binding.
> diff --git a/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> index 7cccd627f8095..efbeb59313fa3 100644
> --- a/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> +++ b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> @@ -23,7 +23,18 @@ properties:
> const: snps,dwc-ssi-2.02a
>
> reg:
> - maxItems: 1
> + minItems: 1
> + maxItems: 2
> + description: |
> + Controller register base and optional XIP memory region.
> + - First entry: SSI controller register space
> + - Second entry (optional): XIP memory-mapped region for direct flash access
> +
> + reg-names:
> + minItems: 1
> + items:
> + - const: ssi_base
> + - const: xip_mem
[Severity: Low]
Should reg use an items list with individual descriptions for each entry
(like clocks and dmas in this schema) instead of maxItems: 2 with a bulleted
description?
Also, although the snps,xip-enabled description states that it requires the
xip_mem entry in reg, the schema has no dependencies or allOf/if/then
constraint enforcing minItems: 2 on reg and reg-names when the XIP
properties are present, so dt_binding_check and dtbs_check will not catch a
missing xip_mem region.
> @@ -93,6 +104,48 @@ properties:
> of the designware controller, and the upper limit is also subject to
> controller configuration.
>
> + snps,xip-enabled:
> + type: boolean
> + description: |
> + Enable XIP (eXecute-In-Place) mode for direct memory-mapped access to
> + SPI flash. When enabled, the controller's XIP region is mapped into
> + CPU address space for zero-copy reads and writes. It requires the
> + xip_mem entry in reg.
> +
> + XIP is used only when this property is present and the module
> + parameter spi_dw_g2_mmio.xip_mode=1 is set. The default xip_mode=0
> + disables XIP. Runtime control is available through sysfs.
> +
> + snps,xip-continuous-mode:
> + type: boolean
> + description: |
> + Enable XIP continuous transfer mode. When enabled, the controller omits
> + the command byte on subsequent sequential reads from the same chip select,
> + saving 8 SPI clock cycles per access. This improves throughput for code
> + execution from XIP flash.
> +
> + Requirements:
> + - Flash device must support continuous read mode (e.g., Fast Read Quad
> + I/O with mode bits)
> + - Must use consistent command across all accesses
> + - Not compatible with mixed command types
> +
> + The controller automatically re-sends the command when CS is de-asserted
> + or transfer type changes.
> +
> + snps,xip-prefetch-mode:
> + type: boolean
> + description: |
> + Enable XIP hardware prefetch mode. The controller speculatively fetches
> + the next cache line when the current access completes, anticipating
> + sequential reads. This reduces latency for instruction fetches during
> + XIP code execution.
> +
> + Best for: Sequential code execution, linear data reads
> + Not ideal for: Random access patterns, small sporadic reads
> +
> + Prefetch is only active when XIP direct mapping is enabled.
[Severity: Medium]
Do snps,xip-enabled, snps,xip-continuous-mode, and snps,xip-prefetch-mode
encode OS and workload policy rather than hardware description?
Whether an XIP memory-mapped window exists is already described by the
xip_mem reg entry and the controller's capability registers;
snps,xip-enabled acts as a software policy switch and documents Linux-specific
module parameters (spi_dw_g2_mmio.xip_mode=1) and sysfs interfaces in the
binding. Similarly, snps,xip-continuous-mode depends on the attached flash
device and protocol layer rather than a static controller property, and
snps,xip-prefetch-mode describes workload tuning (sequential versus random
access patterns).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=5
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 6/6] spi: dw-g2: Add XIP support
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
` (4 preceding siblings ...)
2026-11-19 10:05 ` [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties Prakriti Sharma
@ 2026-11-19 10:05 ` Prakriti Sharma
2026-11-19 10:32 ` sashiko-bot
5 siblings, 1 reply; 12+ messages in thread
From: Prakriti Sharma @ 2026-11-19 10:05 UTC (permalink / raw)
To: Mark Brown, linux-spi
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
Philipp Zabel, linux-kernel, Siddhant Kumar, Prakriti Sharma
Add memory-mapped execute-in-place through the spi-mem direct-mapping
helpers.
NOR page-program writes stay on the indirect FIFO path. XIP issues a
separate SPI transaction per access, and page program needs one
continuous transaction. Direct mapping can be switched at runtime
through debugfs.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
drivers/spi/spi-dw-g2-core.c | 1074 +++++++++++++++++++++++++++++++++-
drivers/spi/spi-dw-g2-mmio.c | 263 ++++++++-
drivers/spi/spi-dw-g2.h | 166 +++++-
3 files changed, 1469 insertions(+), 34 deletions(-)
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index a94d4a7870df..2e265459381d 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -36,8 +36,112 @@ static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = {
DW_SPI_G2_DBG_REG("IMR", DW_SPI_G2_IMR),
DW_SPI_G2_DBG_REG("ISR", DW_SPI_G2_ISR),
DW_SPI_G2_DBG_REG("DEDICATED_CS", DW_SPI_G2_DEDICATED_CS_EN),
+ DW_SPI_G2_DBG_REG("CS0_RX_SAMPLE_DLY", DW_SPI_G2_RX_SAMPLE_DELAY(0)),
+ DW_SPI_G2_DBG_REG("CS1_RX_SAMPLE_DLY", DW_SPI_G2_RX_SAMPLE_DELAY(1)),
+ DW_SPI_G2_DBG_REG("CS2_RX_SAMPLE_DLY", DW_SPI_G2_RX_SAMPLE_DELAY(2)),
+ DW_SPI_G2_DBG_REG("CS3_RX_SAMPLE_DLY", DW_SPI_G2_RX_SAMPLE_DELAY(3)),
+ DW_SPI_G2_DBG_REG("CS0_DDR_DRV_EDGE", DW_SPI_G2_DDR_DRIVE_EDGE(0)),
+ DW_SPI_G2_DBG_REG("CS1_DDR_DRV_EDGE", DW_SPI_G2_DDR_DRIVE_EDGE(1)),
+ DW_SPI_G2_DBG_REG("CS2_DDR_DRV_EDGE", DW_SPI_G2_DDR_DRIVE_EDGE(2)),
};
+static int xip_mode_get(void *data, u64 *val)
+{
+ struct dw_spi_g2 *dws = data;
+ *val = dws->use_xip ? 1 : 0;
+ return 0;
+}
+
+static int xip_mode_set(void *data, u64 val)
+{
+ struct dw_spi_g2 *dws = data;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dws->dev, "Cannot enable XIP: no memory-mapped region\n");
+ return -EINVAL;
+ }
+
+ dws->use_xip = !!val;
+ dev_info(dws->dev, "Transfer mode set to %s\n",
+ dws->use_xip ? "XIP (direct)" : "indirect (FIFO)");
+
+ return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(xip_mode_fops, xip_mode_get, xip_mode_set, "%llu\n");
+
+
+static int xip_continuous_mode_get(void *data, u64 *val)
+{
+ struct dw_spi_g2 *dws = data;
+ *val = dws->xip_continuous_mode ? 1 : 0;
+ return 0;
+}
+
+static int xip_continuous_mode_set(void *data, u64 val)
+{
+ struct dw_spi_g2 *dws = data;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dws->dev,
+ "Cannot enable XIP continuous mode: no XIP region\n");
+ return -EINVAL;
+ }
+
+ dws->xip_continuous_mode = !!val;
+ dev_info(dws->dev, "XIP continuous transfer mode: %s\n",
+ dws->xip_continuous_mode ? "enabled" : "disabled");
+
+ if (dws->xip_continuous_mode)
+ dev_info(dws->dev,
+ "Requires flash command supporting continuous read (e.g., 0xEB Quad I/O)\n");
+
+ return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(xip_continuous_mode_fops, xip_continuous_mode_get,
+ xip_continuous_mode_set, "%llu\n");
+
+static int xip_prefetch_mode_get(void *data, u64 *val)
+{
+ struct dw_spi_g2 *dws = data;
+ *val = dws->xip_prefetch_mode ? 1 : 0;
+ return 0;
+}
+
+static int xip_prefetch_mode_set(void *data, u64 val)
+{
+ struct dw_spi_g2 *dws = data;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dws->dev,
+ "Cannot enable XIP prefetch mode: no XIP region\n");
+ return -EINVAL;
+ }
+
+ dws->xip_prefetch_mode = !!val;
+ dev_info(dws->dev, "XIP prefetch mode: %s\n",
+ dws->xip_prefetch_mode ? "enabled" : "disabled");
+
+ if (dws->xip_prefetch_mode)
+ dev_info(dws->dev,
+ "Requires flash command supporting continuous read (e.g., 0xEB Quad I/O)\n");
+
+ return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(xip_prefetch_mode_fops, xip_prefetch_mode_get,
+ xip_prefetch_mode_set, "%llu\n");
+
/**
* dw_spi_g2_debugfs_init - Create debugfs entries for register inspection
* @dws: Driver state
@@ -61,6 +165,11 @@ static void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws)
dws->regset.nregs = ARRAY_SIZE(dw_spi_g2_dbg_regs);
debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
+ debugfs_create_file("xip_mode", 0644, dws->debugfs, dws, &xip_mode_fops);
+ debugfs_create_file("xip_continuous_mode", 0644, dws->debugfs, dws,
+ &xip_continuous_mode_fops);
+ debugfs_create_file("xip_prefetch_mode", 0644, dws->debugfs, dws,
+ &xip_prefetch_mode_fops);
}
/**
@@ -152,28 +261,61 @@ int dw_spi_g2_setup(struct spi_device *spi)
dw_spi_g2_target_disable(dws, dws->cs);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
+ /* Configure RX sample delay for chip select */
+ if (dws->cs < DW_SPI_G2_MAX_CS) {
+ u32 rx_dly_ns;
+
+ if (device_property_read_u32(&spi->dev, "rx-sample-delay-ns",
+ &rx_dly_ns))
+ rx_dly_ns = dws->def_rx_sample_dly_ns;
+
+ dws->rx_sample_dly[dws->cs] =
+ DIV_ROUND_CLOSEST(rx_dly_ns,
+ NSEC_PER_SEC / dws->max_freq);
+ }
+
dev_dbg(&spi->dev,
- "setup: CS=%u mode=%u bpw=%u speed=%u baud=%u\n",
- dws->cs, spi->mode, spi->bits_per_word, speed, baud);
+ "setup: CS=%u mode=%u bpw=%u speed=%u baud=%u rx_dly=%u\n",
+ dws->cs, spi->mode, spi->bits_per_word, speed, baud,
+ dws->cs < DW_SPI_G2_MAX_CS ? dws->rx_sample_dly[dws->cs] : 0);
return 0;
}
-static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
+/**
+ * dw_spi_g2_set_sampling_mode - Configure RX sampling mode
+ * @dws: Driver state
+ * @cs: Chip select
+ * @mode: Sampling mode to enable
+ *
+ * RX_DELAY uses RX_SAMPLE_DELAY. DATA_STROBE uses the flash DQS/RXDS
+ * input for DTR transfers. Must be called with SSI disabled.
+ */
+void dw_spi_g2_set_sampling_mode(struct dw_spi_g2 *dws, u8 cs,
+ enum dw_spi_g2_sampling_mode mode)
{
- int drain = 0;
+ u32 ctrlr0, spictrlr0;
- dw_spi_g2_enable(dws, false);
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
- /* Clear all interrupts by writing 1s to defined ICR bits */
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+ ctrlr0 = dw_spi_g2_readl(dws->regs, DW_SPI_G2_CTRLR0(cs));
+ spictrlr0 = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SPI_CTRLR0(cs));
- /* Drain any stale RX data left from previous transfers */
- while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
- drain < dws->fifo_len) {
- (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
- drain++;
+ switch (mode) {
+ case DW_SPI_G2_SAMPLING_RX_DELAY:
+ /* Disable CLK_LOOP_EN and SPI_RXDS_EN for basic RX_SAMPLE_DELAY */
+ ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN;
+ spictrlr0 &= ~DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ break;
+
+
+ case DW_SPI_G2_SAMPLING_DATA_STROBE:
+ /* Enable SPI_RXDS_EN, disable CLK_LOOP_EN for DQS/strobe mode */
+ ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN;
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ break;
}
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(cs), spictrlr0);
}
/**
@@ -230,6 +372,30 @@ static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
return ctrlr0;
}
+static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
+{
+ int drain = 0;
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ /* Drain any stale RX data left from previous transfers */
+ while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
+ drain < dws->fifo_len) {
+ (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
+ drain++;
+ }
+
+ /* Update RX sample delay for current CS if value differs. */
+ if (dws->cs < DW_SPI_G2_MAX_CS &&
+ dws->rx_sample_dly[dws->cs] != dws->cur_rx_sample_dly) {
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs),
+ dws->rx_sample_dly[dws->cs]);
+ dws->cur_rx_sample_dly = dws->rx_sample_dly[dws->cs];
+ }
+}
+
/**
* dw_spi_g2_get_frf - Determine frame format from operation bus widths
* @op: SPI memory operation
@@ -456,16 +622,29 @@ static int dw_spi_g2_cmd_only(struct dw_spi_g2 *dws,
{
u8 buf[8];
u32 len;
+ u32 i;
int ret;
+ dev_dbg(dws->dev, "cmd_only: cmd=0x%02x addr=0x%llx\n",
+ op->cmd.opcode, op->addr.val);
+
dw_spi_g2_cmd_only_setup(dws, op);
len = dw_spi_g2_fill_tx_buf(buf, op, false);
- dw_spi_g2_transfer_begin(dws);
- ret = dw_spi_g2_xfer_fifo(dws, buf, len, NULL, 0);
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ for (i = 0; i < len; i++)
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, buf[i]);
+
+ dw_spi_g2_cs_assert(dws, dws->cs);
+ dw_spi_g2_wait_idle(dws);
+ ret = 0;
+
dw_spi_g2_transfer_end(dws);
dw_spi_g2_controller_reset(dws);
+
return ret;
}
@@ -629,6 +808,90 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
return ret;
}
+/**
+ * dw_spi_g2_op_freq - Choose the SPI clock frequency for a given op
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Prefer op->max_freq. If it is unset, use a conservative fraction of
+ * the controller clock rather than dws->max_freq, which can overclock
+ * a slow flash.
+ *
+ * Return: Frequency in Hz to program into BAUDR.
+ */
+u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 f = op->max_freq;
+
+ if (!f)
+ f = dws->max_freq / 8;
+
+ return f;
+}
+
+/**
+ * dw_spi_g2_program_baudr - Program per-CS BAUDR for a given target freq
+ * @dws: Driver state
+ * @freq: Target SPI clock frequency in Hz
+ *
+ * SCKDV must be even (or 1 for passthrough). Must be called with SSI
+ * disabled. If @freq is 0, fall back to a conservative fraction of the
+ * controller clock. No-op if no valid frequency is available or the
+ * active CS is out of range.
+ */
+void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
+{
+ u32 div, baud;
+
+ if (dws->cs >= DW_SPI_G2_MAX_CS)
+ return;
+
+ if (!freq)
+ freq = dws->max_freq / 8;
+ if (!freq)
+ return;
+
+ div = DIV_ROUND_UP(dws->clk_rate, freq);
+ if (div <= 1) {
+ baud = 1; /* Passthrough mode */
+ } else {
+ if (div & 1)
+ div++; /* SCKDV must be even */
+ baud = div;
+ }
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
+}
+
+/**
+ * dw_spi_g2_update_ddr_drive_edge - Program per-CS DDR transmit drive edge
+ * @dws: Driver state
+ * @op: SPI memory operation (must have a DDR/DTR phase)
+ *
+ * At BAUDR 1 or 2 the drive edge is set for edge-aligned data so the TX
+ * delay line can centre the clock; at higher divisors the register returns
+ * to 0 (edge-aligned with sclk_out, default DDR behaviour).
+ */
+static void dw_spi_g2_update_ddr_drive_edge(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 baud;
+
+ if (!op->cmd.dtr && !op->addr.dtr && !op->data.dtr)
+ return;
+
+ if (dws->cs >= DW_SPI_G2_MAX_CS)
+ return;
+
+ baud = dw_spi_g2_readl(dws->regs, DW_SPI_G2_BAUDR(dws->cs)) & 0xFF;
+ if (!baud)
+ baud = 1;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs),
+ (baud <= 2) ? 1 : 0);
+}
+
/**
* Indirect read operations
*/
@@ -658,6 +921,12 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
return -EINVAL;
}
dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ /*
+ * In DTR mode two bits are clocked per cycle, so the dummy
+ * phase needs half as many controller cycles as in SDR mode.
+ */
+ if (op->data.dtr && dummy_cycles)
+ dummy_cycles /= 2;
if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX) {
dev_err(dws->dev, "Dummy cycles %u exceed maximum %u\n",
dummy_cycles, DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX);
@@ -693,11 +962,25 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,
spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
if (op->addr.dtr || op->data.dtr)
spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
- if (op->data.dtr)
- spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ /*
+ * DTR mode with data strobe: set RXDS_EN and enforce mutual
+ * exclusivity with the RX-delay sampling mode.
+ */
+ if (op->data.dtr) {
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ } else {
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_RX_DELAY);
+ }
dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
+ /* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */
+ dw_spi_g2_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR,
min_t(u32, (dws->fifo_len / 2) - 1,
@@ -823,9 +1106,25 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
if (op->addr.dtr || op->data.dtr)
spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
+ /*
+ * For writes, sampling mode is less critical but enforce
+ * mutual exclusivity for consistency with read path.
+ */
+ if (op->data.dtr) {
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ } else {
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_RX_DELAY);
+ }
dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
+
+ /* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */
+ dw_spi_g2_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
}
@@ -893,6 +1192,679 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
return ret;
}
+/*
+ * ========================================================================
+ * XIP (Direct Mapping) Support
+ * ========================================================================
+ */
+
+/**
+ * dw_spi_g2_xip_setup - Configure XIP mode for direct memory access
+ * @dws: Driver state
+ * @op: SPI memory operation template
+ * @is_write: true for write operations, false for read operations
+ *
+ * Configures the XIP registers to enable direct memory-mapped access.
+ * This allows CPU to read/write flash directly via memory bus without
+ * going through the SPI controller's data registers.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_xip_setup(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op,
+ bool is_write)
+{
+ u32 xip_ctrl = 0;
+ u32 dummy_cycles = 0;
+ int tt;
+
+ if (!dws->xip_mem) {
+ dev_err(dws->dev, "XIP memory region not mapped\n");
+ return -EINVAL;
+ }
+
+ tt = dw_spi_g2_get_tt(op);
+ if (tt < 0) {
+ dev_err(dws->dev, "Unsupported XIP transfer type\n");
+ return tt;
+ }
+
+ if (op->dummy.nbytes) {
+ if (!op->dummy.buswidth || op->dummy.buswidth > 8)
+ return -EINVAL;
+ dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ /*
+ * In DTR mode two bits are clocked per cycle, so the dummy
+ * phase needs half as many controller cycles as in SDR mode.
+ */
+ if (op->data.dtr && dummy_cycles)
+ dummy_cycles /= 2;
+ if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX)
+ return -EINVAL;
+ }
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ if (!is_write) {
+ /* Configure XIP read control */
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_CTRL_FRF_MASK,
+ dw_spi_g2_get_frf(op));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_CTRL_TRANS_TYPE_MASK, tt);
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_CTRL_ADDR_L_MASK,
+ dw_spi_g2_addr_len(op->addr.nbytes));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_CTRL_INST_L_MASK,
+ dw_spi_g2_inst_len(op->cmd.nbytes));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_CTRL_WAIT_CYCLES_MASK,
+ dummy_cycles);
+ /*
+ * DFS_HC (Data Frame Size - Hardware Controlled): let the
+ * XIP engine derive the frame size from the AHB transaction
+ * width instead of CTRLR0.DFS. With DFS_HC cleared the
+ * engine uses CTRLR0.DFS (which is 8, since
+ * ctlr->bits_per_word_mask = SPI_BPW_MASK(8)) and packs
+ * each AHB beat MSB-byte-first on the wire, which the
+ * little-endian AXI wrapper stores byte-reversed in memory
+ * (per-32-bit-word byte-swap on dirmap reads). Mirrors the
+ * XIP_WR_CTRL_DFS_HC bit already set on the write path.
+ */
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_DFS_HC;
+
+ if (op->data.dtr) {
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_DDR_EN;
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_RXDS_EN;
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ } else {
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_RX_DELAY);
+ }
+ if (op->cmd.dtr)
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_INST_DDR_EN;
+ if (op->cmd.opcode != 0)
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_INST_EN;
+
+ /* Per-op BAUDR: matched ops at op->max_freq, else safe rate */
+ dw_spi_g2_program_baudr(dws,
+ dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
+ /*
+ * XIP Performance Optimizations
+ *
+ * Continuous Transfer Mode (CONT_XFER_EN):
+ * When enabled, the controller omits the command byte on
+ * subsequent sequential reads from the same CS.
+ *
+ * Requirements:
+ * - Flash must support continuous read mode (e.g., 0xEB Fast
+ * Read Quad I/O with mode bits)
+ * - Must use consistent command across all accesses
+ * - CS must remain asserted between transfers
+ *
+ * The controller automatically re-sends the command when:
+ * - CS is de-asserted (chip select change)
+ * - Transfer type changes (e.g., switching from quad to single)
+ *
+ * Prefetch Mode (PREFETCH_EN):
+ * The controller speculatively fetches the next cache line
+ * when the current access completes, anticipating sequential
+ * reads. This reduces latency for instruction fetches during
+ * XIP code execution.
+ *
+ * Best for: Sequential code execution, linear data reads
+ *
+ * Both features are disabled by default for maximum flash
+ * compatibility and can be enabled via module parameters or
+ * device tree properties.
+ */
+ if (dws->xip_continuous_mode && op->cmd.opcode != 0) {
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_CONT_XFER_EN;
+
+ /*
+ * Program XIP_CNT_TIME_OUT
+ * In continuous mode, this hclk-cycle timeout deselects
+ * the target if no AHB request arrives within the
+ * window. Must be written while SSI is disabled.
+ */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CNT_TIME_OUT,
+ FIELD_PREP(DW_SPI_G2_XIP_XTOC_MASK,
+ DW_SPI_G2_XIP_XTOC_DEFAULT));
+ }
+
+ if (dws->xip_prefetch_mode)
+ xip_ctrl |= DW_SPI_G2_XIP_CTRL_PREFETCH_EN;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CTRL(dws->cs),
+ xip_ctrl);
+
+ if (op->cmd.opcode != 0) {
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_INCR_INST(dws->cs),
+ op->cmd.opcode);
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_WRAP_INST(dws->cs),
+ op->cmd.opcode);
+ }
+ } else {
+ /* Configure XIP write control */
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_WR_CTRL_FRF_MASK,
+ dw_spi_g2_get_frf(op));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_WR_CTRL_TRANS_TYPE_MASK, tt);
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_WR_CTRL_ADDR_L_MASK,
+ dw_spi_g2_addr_len(op->addr.nbytes));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_WR_CTRL_INST_L_MASK,
+ dw_spi_g2_inst_len(op->cmd.nbytes));
+ xip_ctrl |= FIELD_PREP(DW_SPI_G2_XIP_WR_CTRL_WAIT_CYCLES_MASK,
+ dummy_cycles);
+ xip_ctrl |= DW_SPI_G2_XIP_WR_CTRL_DFS_HC;
+
+ if (op->data.dtr) {
+ xip_ctrl |= DW_SPI_G2_XIP_WR_CTRL_DDR_EN;
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ } else {
+ dw_spi_g2_set_sampling_mode(dws, dws->cs,
+ DW_SPI_G2_SAMPLING_RX_DELAY);
+ }
+ if (op->cmd.dtr)
+ xip_ctrl |= DW_SPI_G2_XIP_WR_CTRL_INST_DDR_EN;
+
+ /* Per-op BAUDR: matched ops at op->max_freq, else safe rate */
+ dw_spi_g2_program_baudr(dws,
+ dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_WRITE_CTRL(dws->cs),
+ xip_ctrl);
+
+ if (op->cmd.opcode != 0) {
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_WRITE_INCR_INST(dws->cs),
+ op->cmd.opcode);
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_WRITE_WRAP_INST(dws->cs),
+ op->cmd.opcode);
+ }
+ }
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+
+ dev_dbg(dws->dev, "xip_setup: %s cmd=0x%02x addr_bytes=%u dummy=%u cs=%u\n",
+ is_write ? "WRITE" : "READ",
+ op->cmd.opcode, op->addr.nbytes, dummy_cycles, dws->cs);
+
+ return 0;
+}
+
+/**
+ * dw_spi_g2_dirmap_create - Create a direct mapping for XIP access
+ * @desc: Direct mapping descriptor
+ *
+ * Sets up XIP mode configuration for the specified operation template.
+ * The actual memory mapping is expected to be set up by the platform driver.
+ *
+ * This configures both CTRLR0 (for data frame size and SPI mode) and the
+ * XIP-specific registers (for command, address, and transfer type).
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc)
+{
+ struct spi_device *spi = desc->mem->spi;
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+ const struct spi_mem_op *op = desc->info.op_tmpl;
+ u32 ctrlr0;
+ int ret;
+
+ if (!dws->xip_mem) {
+ dev_dbg(dws->dev, "XIP memory region not available\n");
+ return -EOPNOTSUPP;
+ }
+
+ dev_dbg(dws->dev, "dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n",
+ op->cmd.opcode, op->addr.nbytes,
+ op->data.dir == SPI_MEM_DATA_IN ? "READ" : "WRITE",
+ op->dummy.nbytes);
+
+ dws->cs = spi_get_chipselect(spi, 0);
+ dws->curr_spi = spi;
+
+ /*
+ * Configure CTRLR0 with data frame size and SPI mode settings.
+ * XIP mode uses these settings for all memory-mapped accesses.
+ */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
+ DW_SPI_G2_FRF_STANDARD);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+
+ /* Configure XIP registers for read/write operations */
+ ret = dw_spi_g2_xip_setup(dws, op,
+ op->data.dir == SPI_MEM_DATA_OUT);
+ /*
+ * xip_setup enables the target (sets BIT(cs) in
+ * DEDICATED_CS_EN) to program per-CS registers.
+ * Clean it up so stale bits don't leak into
+ * cmd_only / reg_op operations on other CS lines.
+ * dirmap_read/write will re-enable when needed.
+ */
+ dw_spi_g2_target_disable(dws, dws->cs);
+ return ret;
+}
+
+/**
+ * dw_spi_g2_dirmap_read - Direct memory-mapped read from flash
+ * @desc: Direct mapping descriptor
+ * @offs: Offset within the mapped region
+ * @len: Number of bytes to read
+ * @buf: Destination buffer
+ *
+ * Performs direct memory-mapped read using XIP mode. The controller
+ * automatically sends the configured command and address for each access.
+ * Supports 8/16/32-bit data frame sizes for optimal performance.
+ *
+ * Return: Number of bytes read on success, negative error code on failure
+ */
+ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
+ u64 offs, size_t len, void *buf)
+{
+ struct spi_device *spi = desc->mem->spi;
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+ void __iomem *src;
+ u32 ctrlr0;
+
+ dws->cs = spi_get_chipselect(spi, 0);
+ dws->curr_spi = spi;
+
+ if (!dws->use_xip) {
+ struct spi_mem_op op = *desc->info.op_tmpl;
+ size_t chunk, done = 0;
+ unsigned int dummy_shift = 0;
+ int ret;
+
+ /* Clear XIP registers so they don't interfere with RO mode */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CTRL(dws->cs), 0);
+
+ /*
+ * Fold dummy bytes into the address field.
+ *
+ * To handle WAIT_CYCLES in RO mode --
+ * the RX FIFO stays empty when WAIT_CYCLES > 0. Work around
+ * this by absorbing the dummy phase into the address: shift the
+ * real address left by (dummy_nbytes * 8) bits and zero-pad.
+ * This makes WAIT_CYCLES=0 while the flash still receives the
+ * correct number of clock cycles before data.
+ *
+ * With TT1 (1-1-x), both address and folded-dummy bits travel
+ * on the standard single lane, so the flash sees:
+ * [real addr MSB..LSB] [zero-pad dummy clocks] [data on x lanes]
+ */
+ if (op.dummy.nbytes) {
+ dummy_shift = op.dummy.nbytes * 8;
+ op.addr.nbytes += op.dummy.nbytes;
+ op.dummy.nbytes = 0;
+ }
+
+ while (done < len) {
+ chunk = min_t(size_t, len - done, dws->fifo_len);
+
+ op.addr.val = (u64)(offs + done) << dummy_shift;
+ op.data.nbytes = chunk;
+ op.data.buf.in = (u8 *)buf + done;
+
+ ret = dw_spi_g2_indirect_read(dws, &op);
+ if (ret)
+ return ret;
+
+ done += chunk;
+ }
+
+ return (ssize_t)len;
+ }
+
+ if (!dws->xip_mem)
+ return -EOPNOTSUPP;
+
+ if (offs + len > dws->xip_len)
+ return -EINVAL;
+
+ /*
+ * Restore full XIP register state. An indirect transfer (from a
+ * prior mode switch) may have left CTRLR0 in TMOD_RO/TO and
+ * SPI_CTRLR0 with non-XIP framing, which corrupts XIP accesses.
+ */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+ ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
+ DW_SPI_G2_FRF_STANDARD);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, false);
+
+ src = dws->xip_mem + offs;
+
+ /* Enable XIP mode for read */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, BIT(dws->cs));
+ dw_spi_g2_enable(dws, true);
+
+ /*
+ * PIO read from XIP window. The XIP engine turns each AHB read
+ * into a (cmd + addr + dummy + data) SPI transaction.
+ */
+ memcpy_fromio(buf, src, len);
+
+ /* Disable XIP mode and clean up DEDICATED_CS_EN */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ return (ssize_t)len;
+}
+
+/**
+ * dw_spi_g2_dirmap_write_nor_xip - byte-by-byte NOR XIP write hack
+ * @dws: driver state
+ * @desc: dirmap descriptor (XIP write template lives in info.op_tmpl)
+ * @offs: byte offset inside the flash
+ * @len: byte count to program
+ * @buf: data to program
+ *
+ * Single-byte XIP write protocol: for every byte we issue WREN
+ * out-of-band, arm the XIP write engine, do exactly one writeb() into
+ * the XIP window (the engine emits one cmd+addr+1-data Page Program),
+ * then poll SR1 until WIP clears.
+ *
+ * Functionally correct but ~3 SPI transactions per byte, so dramatically
+ * slower than the indirect-FIFO path (one cmd+addr+up-to-256 Page Program
+ * per call). It is selected automatically whenever XIP is enabled on the
+ * controller (dws->use_xip && dws->xip_mem != NULL), so any NOR write
+ * issued through dirmap_write() exercises the XIP-write engine end-to-end.
+ */
+static ssize_t dw_spi_g2_dirmap_write_nor_xip(struct dw_spi_g2 *dws,
+ struct spi_mem_dirmap_desc *desc,
+ u64 offs, size_t len,
+ const void *buf)
+{
+ const struct spi_mem_op wren_op = SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_WREN, 1),
+ SPI_MEM_OP_NO_ADDR,
+ SPI_MEM_OP_NO_DUMMY,
+ SPI_MEM_OP_NO_DATA);
+ const u8 *src = buf;
+ void __iomem *dst;
+ ssize_t result;
+ u32 ctrlr0;
+ u8 sr1;
+ size_t i;
+ int ret;
+
+ if (!dws->xip_mem)
+ return -EOPNOTSUPP;
+
+ if (len > dws->xip_len || offs > dws->xip_len - len)
+ return -EINVAL;
+
+ dst = dws->xip_mem + offs;
+
+ dev_dbg(dws->dev,
+ "dirmap_write_nor_xip: BEGIN offs=0x%llx len=%zu (byte-by-byte WREN/WIP)\n",
+ (unsigned long long)offs, len);
+
+ for (i = 0; i < len; i++) {
+ const struct spi_mem_op rdsr_op =
+ SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_RDSR1, 1),
+ SPI_MEM_OP_NO_ADDR,
+ SPI_MEM_OP_NO_DUMMY,
+ SPI_MEM_OP_DATA_IN(1, &sr1, 1));
+ unsigned long deadline;
+
+ dev_dbg(dws->dev,
+ "dirmap_write_nor_xip: byte %zu/%zu @ +0x%zx\n",
+ i + 1, len, (size_t)offs + i);
+
+ /* WREN: latch the write-enable bit so the next Page Program is accepted. */
+ ret = dw_spi_g2_cmd_only(dws, &wren_op);
+ if (ret) {
+ dev_err(dws->dev,
+ "XIP NOR write: WREN failed at byte %zu: %d\n",
+ i, ret);
+ result = ret;
+ goto out_unwind;
+ }
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ ctrlr0 = dw_spi_g2_build_ctrlr0(dws,
+ DW_SPI_G2_CTRLR0_TMOD_TR,
+ DW_SPI_G2_FRF_STANDARD);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs),
+ ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER,
+ BIT(dws->cs));
+ dw_spi_g2_enable(dws, true);
+
+ writeb(src[i], dst + i);
+
+ dw_spi_g2_wait_idle(dws);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ /* WIP poll: spin on SR1.WIP until the program completes. */
+ sr1 = 0;
+ deadline = jiffies +
+ msecs_to_jiffies(DW_SPI_G2_NOR_WIP_TIMEOUT_MS);
+ do {
+ ret = dw_spi_g2_reg_op(dws, &rdsr_op);
+ if (ret) {
+ dev_err(dws->dev,
+ "XIP NOR write: WIP poll failed at byte %zu: %d\n",
+ i, ret);
+ result = ret;
+ goto out_unwind;
+ }
+ if (!(sr1 & DW_SPI_G2_NOR_SR1_WIP))
+ break;
+ if (time_after(jiffies, deadline)) {
+ dev_err(dws->dev,
+ "XIP NOR write: WIP timeout at byte %zu\n",
+ i);
+ result = -ETIMEDOUT;
+ goto out_unwind;
+ }
+ udelay(1);
+ } while (1);
+ }
+
+ dev_dbg(dws->dev,
+ "dirmap_write_nor_xip: END offs=0x%llx len=%zu OK\n",
+ (unsigned long long)offs, len);
+
+ return (ssize_t)len;
+
+out_unwind:
+ /*
+ * On any error mid-loop the XIP-write engine may be left armed
+ * (XIP_WR_SER set, SSI enabled). Tear it down so the next op sees
+ * a clean controller state regardless of where the failure landed.
+ */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+ return result;
+}
+
+/**
+ * dw_spi_g2_dirmap_write - Direct memory-mapped write to flash
+ * @desc: Direct mapping descriptor
+ * @offs: Offset within the mapped region
+ * @len: Number of bytes to write
+ * @buf: Source buffer
+ *
+ * Performs direct memory-mapped write using XIP mode. The controller
+ * automatically sends the configured command and address for each access.
+ * Supports 8/16/32-bit data frame sizes for optimal performance.
+ *
+ * Routing rules:
+ * - NAND Program Load (addr_nbytes == 2): XIP burst path, pushed as a
+ * writeb() loop. The on-die page buffer auto-increments the column
+ * counter on each transaction; the per-byte writeb() pace is required
+ * because this controller's XIP-write engine only emits the first
+ * byte of each multi-beat AHB store, so any wider PIO/DMA write
+ * would corrupt the payload past the first byte of each beat.
+ * - NOR Page Program (addr_nbytes >= 3) with XIP enabled: byte-by-byte
+ * XIP-write helper (dw_spi_g2_dirmap_write_nor_xip). The
+ * XIP_WRITE_CTRL register has no auto-WREN bit, so a bursted XIP
+ * write to NOR would drop everything past the first transaction
+ * (write-enable cleared). Doing one WREN + 1-byte XIP store + WIP poll per byte
+ * is slow but functional.
+ * - NOR Page Program (addr_nbytes >= 3) with XIP disabled: indirect
+ * FIFO path. Builds one cmd+addr+up-to-256-data SPI transaction in
+ * software; correct and dramatically faster than the XIP-write
+ * helper, used as a fallback when no XIP region is mapped.
+ *
+ * Return: Number of bytes written on success, negative error code on failure
+ */
+ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
+ u64 offs, size_t len, const void *buf)
+{
+ struct spi_device *spi = desc->mem->spi;
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+ void __iomem *dst;
+ u32 ctrlr0;
+ bool use_indirect = false;
+
+ dws->cs = spi_get_chipselect(spi, 0);
+ dws->curr_spi = spi;
+
+ /*
+ * NOR Page Program (addr_nbytes >= 3) cannot ride the XIP-write
+ * engine in burst mode -- the engine emits one cmd+addr+data SPI
+ * transaction per AHB write but the SPI-NOR framework only issues
+ * WREN once before dirmap_write(), so every transaction past the
+ * first arrives with write-enable cleared and is silently dropped.
+ * NAND Program Load (addr_nbytes == 2) is XIP-friendly because the
+ * on-die page buffer has no per-byte write-enable gating, so it goes
+ * to the NAND-XIP-BURST path below.
+ */
+ if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
+ use_indirect = true;
+
+ /*
+ * NOR with XIP enabled: route through the byte-by-byte XIP-write
+ * helper so the XIP-write engine is exercised end-to-end.
+ */
+ if (use_indirect && dws->use_xip && dws->xip_mem &&
+ desc->info.op_tmpl->addr.nbytes >= 3) {
+ dev_info(dws->dev,
+ "dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
+ desc->info.op_tmpl->cmd.opcode,
+ (unsigned long long)offs, len);
+ return dw_spi_g2_dirmap_write_nor_xip(dws, desc, offs, len,
+ buf);
+ }
+
+ if (use_indirect) {
+ struct spi_mem_op op = *desc->info.op_tmpl;
+ int ret;
+
+ dev_info(dws->dev,
+ "dirmap_write: ROUTE=NOR-INDIRECT-FIFO cmd=0x%02x offs=0x%llx len=%zu\n",
+ op.cmd.opcode, (unsigned long long)offs, len);
+
+ /*
+ * dirmap_create configured XIP_WRITE_CTRL with opcode,
+ * address length, etc. These non-zero values can cause
+ * the controller to misinterpret a plain FIFO (TO-mode)
+ * write. Zero the XIP write registers so the indirect
+ * path behaves identically to exec_mem_op.
+ */
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
+ dw_spi_g2_writel(dws->regs,
+ DW_SPI_G2_XIP_WRITE_CTRL(dws->cs), 0);
+
+ op.addr.val = offs;
+ op.data.nbytes = len;
+ op.data.buf.out = buf;
+
+ ret = dw_spi_g2_indirect_write(dws, &op);
+ return ret ? ret : (ssize_t)len;
+ }
+
+ /* XIP write path — only reached for NAND Program Load */
+ if (!dws->xip_mem)
+ return -EOPNOTSUPP;
+
+ if (offs + len > dws->xip_len)
+ return -EINVAL;
+
+ dev_info(dws->dev,
+ "dirmap_write: ROUTE=NAND-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
+ desc->info.op_tmpl->cmd.opcode,
+ (unsigned long long)offs, len);
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+ ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
+ DW_SPI_G2_FRF_STANDARD);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
+ dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
+
+ dst = dws->xip_mem + offs;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, BIT(dws->cs));
+ dw_spi_g2_enable(dws, true);
+
+ /*
+ * PIO write into the XIP window. The XIP engine turns every AHB
+ * write into one (cmd + addr + data) SPI transaction, but on
+ * this controller only the first byte of each AHB beat is shifted
+ * out to the flash -- a multi-byte AHB store (writel/writeq, as
+ * memcpy_toio() emits on arm64 with the MMU on) drops every byte
+ * past the first. The corruption pattern in flash is unmistakable:
+ * buf[0..7] = 00 01 02 03 04 05 06 07
+ * flash[0..7] = 00 04 ff ff ff ff ff ff <- only +0 and +4 land
+ * confirming two 32-bit AHB beats per writeq, each beat emitting
+ * only its low byte. We must therefore push the payload as a
+ * writeb() loop so every byte is its own AHB beat -> its own SPI
+ * Program-Load transaction. The on-die NAND page buffer
+ * auto-increments its column counter on each transaction, so the
+ * full page lands correctly.
+ */
+ {
+ const u8 *src = buf;
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ writeb(src[i], dst + i);
+ }
+
+ dw_spi_g2_wait_idle(dws);
+
+ /* Disable XIP WR mode and clean up DEDICATED_CS_EN */
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ return (ssize_t)len;
+}
+
/*
* ========================================================================
* SPI-MEM interface
@@ -938,8 +1910,20 @@ static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
return spi_mem_default_supports_op(mem, op);
}
+/*
+ * Controller capabilities advertised to the spi-mem core:
+ * - .dtr = true: controller can run DTR (octal-DTR / DDR) phases.
+ * - .per_op_freq = true: dw_spi_g2_program_baudr() reprograms BAUDR
+ * at the start of every op, so the spi-mem core may set
+ * op->max_freq below mem->spi->max_speed_hz on a per-op basis
+ * (e.g. SPI-NAND cache reads capping at ~50-104 MHz, or an
+ * SFDP-derived per-op cap). Without this flag, spi-mem rejects
+ * any op where op->max_freq differs from mem->spi->max_speed_hz
+ * (see spi-mem.c::spi_mem_check_op()).
+ */
static const struct spi_controller_mem_caps dw_spi_g2_mem_caps = {
.dtr = true,
+ .per_op_freq = true,
};
int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
@@ -989,6 +1973,13 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
return -EINVAL;
}
dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ /*
+ * In DTR mode two bits are clocked per cycle, so halve the
+ * cycle count derived from dummy.nbytes before checking the
+ * hardware limit.
+ */
+ if (op->data.dtr && dummy_cycles)
+ dummy_cycles /= 2;
if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX) {
dev_err(dws->dev, "dummy cycles %u exceed maximum %u\n",
dummy_cycles, DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX);
@@ -1029,6 +2020,9 @@ static const struct spi_controller_mem_ops dw_spi_g2_mem_ops = {
.supports_op = dw_spi_g2_supports_mem_ops,
.adjust_op_size = dw_spi_g2_adjust_mem_op_size,
.exec_op = dw_spi_g2_exec_mem_op,
+ .dirmap_create = dw_spi_g2_dirmap_create,
+ .dirmap_read = dw_spi_g2_dirmap_read,
+ .dirmap_write = dw_spi_g2_dirmap_write,
};
/*
@@ -1075,15 +2069,7 @@ int dw_spi_g2_init(struct dw_spi_g2 *dws)
dev_info(dws->dev, "DW SSI G2 core init\n");
- /* Disable controller and reset to clean state */
- dw_spi_g2_enable(dws, false);
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN, 0);
-
- /* Mask all interrupts — this driver uses polling only */
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
- /* Clear all interrupts by writing 1s to defined ICR bits */
- dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+ dw_spi_g2_hw_init(dws);
/* Hardware capabilities */
dws->fifo_len = dw_spi_g2_detect_fifo_len(dws);
@@ -1126,6 +2112,42 @@ void dw_spi_g2_deinit(struct dw_spi_g2 *dws)
}
EXPORT_SYMBOL_NS_GPL(dw_spi_g2_deinit, "SPI_DW_G2_CORE");
+/**
+ * dw_spi_g2_hw_init - Re-initialize controller hardware after power-up
+ * @dws: Driver state
+ *
+ * Brings the controller back to a known clean state after clocks are
+ * restored (e.g. resume from suspend). Resets SSI enable, chip selects,
+ * dedicated CS, interrupt mask, and clears any pending interrupts.
+ *
+ * Does NOT reconfigure per-CS transfer registers; those are set up
+ * on-demand by the first spi-mem operation after resume.
+ *
+ * Return: 0 on success
+ */
+int dw_spi_g2_hw_init(struct dw_spi_g2 *dws)
+{
+ u8 cs;
+
+ dw_spi_g2_enable(dws, false);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+
+ /*
+ * Set DDR_DRIVE_EDGE for all CS. Value of 1 means data is driven
+ * on the falling edge of ssi_clk in DDR mode, which aligns the
+ * output with the sampling window on the flash side.
+ */
+ for (cs = 0; cs < DW_SPI_G2_MAX_CS; cs++)
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(cs), 1);
+
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(dw_spi_g2_hw_init, "SPI_DW_G2_CORE");
+
/*
* ========================================================================
* Legacy SPI transfer_one interface (not actively used with spi-mem)
diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
index 6ef8f926e0eb..9620ae6e5c23 100644
--- a/drivers/spi/spi-dw-g2-mmio.c
+++ b/drivers/spi/spi-dw-g2-mmio.c
@@ -11,6 +11,42 @@
#include "spi-dw-g2.h"
+/*
+ * Module parameter: select transfer mode at load time.
+ * 1 = XIP (memory-mapped direct access)
+ * 0 = indirect (default, FIFO-based, uses exec_mem_op path)
+ *
+ * Can also be changed at runtime via sysfs:
+ * /sys/devices/.../xip_mode
+ */
+static int xip_mode;
+module_param(xip_mode, int, 0644);
+MODULE_PARM_DESC(xip_mode, "Transfer mode: 0=indirect (default), 1=XIP");
+
+/*
+ * XIP Performance Optimization Parameters
+ *
+ * xip_continuous_mode / xip_prefetch_mode:
+ * -1 = auto / use DT property (default; DT also defaults to disabled)
+ * 0 = force disabled (overrides DT)
+ * 1 = force enabled (overrides DT, requires flash support, e.g., 0xEB)
+ *
+ * Both modes default to DISABLED unless explicitly enabled via DT or
+ * module parameter, because they require flash commands that support
+ * continuous read mode. Using them with incompatible commands like 0x6B
+ * (Fast Read Quad Output) causes data corruption.
+ */
+static int xip_continuous_mode = -1;
+module_param(xip_continuous_mode, int, 0644);
+MODULE_PARM_DESC(xip_continuous_mode,
+ "XIP continuous transfer mode: -1=auto/DT (default), 0=disabled, 1=enabled");
+
+static int xip_prefetch_mode = -1;
+module_param(xip_prefetch_mode, int, 0644);
+MODULE_PARM_DESC(xip_prefetch_mode,
+ "XIP prefetch mode: -1=auto/DT (default), 0=disabled, 1=enabled");
+
+
struct dw_spi_g2_mmio {
struct dw_spi_g2 dws;
struct clk *clk;
@@ -18,6 +54,119 @@ struct dw_spi_g2_mmio {
struct reset_control *rstc;
};
+static ssize_t xip_mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+
+ return sysfs_emit(buf, "%d\n", dws->use_xip ? 1 : 0);
+}
+
+static ssize_t xip_mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+ int val;
+
+ if (kstrtoint(buf, 0, &val))
+ return -EINVAL;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dev, "Cannot enable XIP: no memory-mapped region available\n");
+ return -EINVAL;
+ }
+
+ dws->use_xip = !!val;
+ dev_info(dev, "Transfer mode set to %s\n",
+ dws->use_xip ? "XIP (direct)" : "indirect (FIFO)");
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(xip_mode);
+
+static ssize_t xip_continuous_mode_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+
+ return sysfs_emit(buf, "%d\n", dws->xip_continuous_mode ? 1 : 0);
+}
+
+static ssize_t xip_continuous_mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+ int val;
+
+ if (kstrtoint(buf, 0, &val))
+ return -EINVAL;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dev, "Cannot enable continuous mode: no XIP region\n");
+ return -EINVAL;
+ }
+
+ dws->xip_continuous_mode = !!val;
+ dev_info(dev, "XIP continuous transfer mode: %s\n",
+ dws->xip_continuous_mode ? "enabled" : "disabled");
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(xip_continuous_mode);
+
+static ssize_t xip_prefetch_mode_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+
+ return sysfs_emit(buf, "%d\n", dws->xip_prefetch_mode ? 1 : 0);
+}
+
+static ssize_t xip_prefetch_mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = dev_get_drvdata(dev);
+ struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+ int val;
+
+ if (kstrtoint(buf, 0, &val))
+ return -EINVAL;
+
+ if (val != 0 && val != 1)
+ return -EINVAL;
+
+ if (val && !dws->xip_mem) {
+ dev_err(dev, "Cannot enable prefetch: no XIP region\n");
+ return -EINVAL;
+ }
+
+ dws->xip_prefetch_mode = !!val;
+ dev_info(dev, "XIP prefetch mode: %s\n",
+ dws->xip_prefetch_mode ? "enabled" : "disabled");
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(xip_prefetch_mode);
+
static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
{
struct dw_spi_g2_mmio *dwsmmio;
@@ -47,6 +196,24 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
goto err_put;
}
+ /* XIP memory region for direct mapping */
+ mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+ if (mem) {
+ dws->xip_mem = devm_ioremap_resource(&pdev->dev, mem);
+ if (IS_ERR(dws->xip_mem)) {
+ dev_warn(&pdev->dev, "Failed to map XIP region, direct mapping disabled\n");
+ dws->xip_mem = NULL;
+ } else {
+ dws->xip_phys = mem->start;
+ dws->xip_len = resource_size(mem);
+ dev_info(&pdev->dev, "XIP region mapped: phys=0x%llx len=0x%zx\n",
+ (unsigned long long)dws->xip_phys, dws->xip_len);
+ }
+ } else {
+ dws->xip_mem = NULL;
+ dev_dbg(&pdev->dev, "XIP memory region not specified, direct mapping disabled\n");
+ }
+
/*
* IRQ.
*
@@ -126,6 +293,74 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
dev_dbg(dws->dev, "HW_CAPS = 0x%08x\n", dws->hw_caps);
dws->max_freq = clk_get_rate(dwsmmio->clk);
+
+ /*
+ * XIP (Direct Mapping) mode configuration.
+ *
+ * XIP is enabled ONLY if ALL conditions are met:
+ * 1. XIP memory region available in DT
+ * 2. Device tree has snps,xip-enabled property
+ * 3. Module parameter xip_mode=1 (or runtime sysfs)
+ *
+ * This provides safe default (disabled) while allowing controlled enabling.
+ * Runtime control via sysfs: /sys/devices/.../xip_mode
+ */
+ bool dt_xip_enabled = device_property_read_bool(&pdev->dev, "snps,xip-enabled");
+ bool param_xip_enabled = xip_mode != 0;
+
+ dws->use_xip = dws->xip_mem && dt_xip_enabled && param_xip_enabled;
+
+ if (dws->xip_mem) {
+ dev_info(&pdev->dev, "XIP mode: %s (DT=%s, param=%s, both required)\n",
+ dws->use_xip ? "enabled" : "disabled",
+ dt_xip_enabled ? "yes" : "no",
+ param_xip_enabled ? "yes" : "no");
+ } else {
+ dev_info(&pdev->dev, "XIP mode: disabled (no memory region in DT)\n");
+ }
+
+ /*
+ * XIP Performance Optimization Configuration
+ *
+ * Both modes default to DISABLED for safety - they require flash
+ * commands that support continuous read mode (e.g., 0xEB Quad I/O).
+ * Standard 0x6B (Fast Read Quad Output) does NOT support these
+ * optimizations and will cause data corruption.
+ *
+ * Resolution order (last wins):
+ * 1. Default: false
+ * 2. DT property (snps,xip-{continuous,prefetch}-mode) enables
+ * 3. Module parameter (xip_{continuous,prefetch}_mode) overrides DT
+ * Use module param value 0 to force-disable even when DT enables.
+ *
+ * Runtime override is also possible via debugfs/sysfs after probe.
+ */
+ dws->xip_continuous_mode = false;
+ dws->xip_prefetch_mode = false;
+
+ if (dws->xip_mem) {
+ if (device_property_read_bool(&pdev->dev,
+ "snps,xip-continuous-mode"))
+ dws->xip_continuous_mode = true;
+ if (device_property_read_bool(&pdev->dev,
+ "snps,xip-prefetch-mode"))
+ dws->xip_prefetch_mode = true;
+
+ /* Module params override DT (>=0 forces explicit value) */
+ if (xip_continuous_mode >= 0)
+ dws->xip_continuous_mode = !!xip_continuous_mode;
+ if (xip_prefetch_mode >= 0)
+ dws->xip_prefetch_mode = !!xip_prefetch_mode;
+
+ dev_info(&pdev->dev, "XIP continuous transfer mode: %s\n",
+ dws->xip_continuous_mode ? "enabled" : "disabled");
+ dev_info(&pdev->dev, "XIP prefetch mode: %s\n",
+ dws->xip_prefetch_mode ? "enabled" : "disabled");
+ if (dws->xip_continuous_mode || dws->xip_prefetch_mode)
+ dev_info(&pdev->dev,
+ "XIP optimizations require flash supporting continuous read (e.g., 0xEB)\n");
+ }
+
/* SPI Controller Setup */
ctlr->bus_num = of_alias_get_id(pdev->dev.of_node, "spi");
if (ctlr->bus_num < 0)
@@ -151,7 +386,29 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
if (ret)
goto err_pm;
- dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
+ ret = device_create_file(&pdev->dev, &dev_attr_xip_mode);
+ if (ret)
+ dev_warn(&pdev->dev,
+ "Failed to create xip_mode sysfs attribute: %d\n",
+ ret);
+
+ if (dws->xip_mem) {
+ ret = device_create_file(&pdev->dev,
+ &dev_attr_xip_continuous_mode);
+ if (ret)
+ dev_warn(&pdev->dev,
+ "Failed to create xip_continuous_mode sysfs: %d\n",
+ ret);
+
+ ret = device_create_file(&pdev->dev,
+ &dev_attr_xip_prefetch_mode);
+ if (ret)
+ dev_warn(&pdev->dev,
+ "Failed to create xip_prefetch_mode sysfs: %d\n",
+ ret);
+ }
+ dev_info(&pdev->dev, "DW SSI G2 MMIO probed (mode=%s)\n",
+ dws->use_xip ? "XIP" : "indirect");
return 0;
err_pm:
@@ -168,10 +425,14 @@ static void dw_spi_g2_mmio_remove(struct platform_device *pdev)
struct spi_controller *ctlr = platform_get_drvdata(pdev);
struct dw_spi_g2_mmio *dwsmmio = spi_controller_get_devdata(ctlr);
+ device_remove_file(&pdev->dev, &dev_attr_xip_mode);
+ device_remove_file(&pdev->dev, &dev_attr_xip_continuous_mode);
+ device_remove_file(&pdev->dev, &dev_attr_xip_prefetch_mode);
spi_unregister_controller(ctlr);
dw_spi_g2_deinit(&dwsmmio->dws);
pm_runtime_disable(&pdev->dev);
reset_control_assert(dwsmmio->rstc);
+ dw_spi_g2_enable(&dwsmmio->dws, false);
}
static const struct of_device_id dw_spi_g2_of_match[] = {
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
index 3091697238d7..162d23390298 100644
--- a/drivers/spi/spi-dw-g2.h
+++ b/drivers/spi/spi-dw-g2.h
@@ -3,12 +3,16 @@
#ifndef _SPI_DW_G2_H
#define _SPI_DW_G2_H
-#include <linux/spi/spi.h>
-#include <linux/io.h>
+#include <linux/bitfield.h>
+#include <linux/delay.h>
#include <linux/device.h>
-#include <linux/irqreturn.h>
+#include <linux/io.h>
#include <linux/iopoll.h>
+#include <linux/irqreturn.h>
+#include <linux/slab.h>
+#include <linux/spi/spi.h>
#include <linux/spi/spi-mem.h>
+#include <linux/string.h>
#ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
@@ -24,7 +28,7 @@
#define DW_SPI_G2_DEDICATED_CS_STRIDE 0x14
#define DW_SPI_G2_DEDICATED_CS_EN 0x50
#define DW_SPI_G2_CS_OFFSET(cs) \
- (DW_SPI_G2_DEDICATED_CS_BASE + (cs) * DW_SPI_G2_DEDICATED_CS_STRIDE)
+ (DW_SPI_G2_DEDICATED_CS_BASE + (cs) * DW_SPI_G2_DEDICATED_CS_STRIDE)
/* Dedicated CS Blocks */
#define DW_SPI_G2_CTRLR0(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x04)
@@ -33,6 +37,84 @@
#define DW_SPI_G2_CTRLR1(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x10)
#define DW_SPI_G2_SPI_CTRLR1(cs) (DW_SPI_G2_CS_OFFSET(cs) + 0x14)
+/*
+ * XIP (eXecute In Place) registers for direct memory-mapped access
+ */
+#define DW_SPI_G2_XIP_HEADER 0x124
+#define DW_SPI_G2_XIP_SER 0x128
+#define DW_SPI_G2_XIP_CNT_TIME_OUT 0x12C
+#define DW_SPI_G2_XIP_WR_SER 0x134
+
+/*
+ * XIP_CNT_TIME_OUT fields
+ * XTOC[7:0]: timeout in hclk cycles to deselect target in continuous mode
+ * if no AHB request arrives within this time. Max value 0xFF.
+ */
+#define DW_SPI_G2_XIP_XTOC_MASK GENMASK(7, 0)
+#define DW_SPI_G2_XIP_XTOC_DEFAULT 0xFFU
+
+/* Dedicated CS XIP register base offsets (per-CS stride = 0x10) */
+#define DW_SPI_G2_XIP_CTRL_BASE 0x22C
+#define DW_SPI_G2_XIP_CTRL_STRIDE 0x10
+#define DW_SPI_G2_XIP_CTRL(cs) \
+ (DW_SPI_G2_XIP_CTRL_BASE + (cs) * DW_SPI_G2_XIP_CTRL_STRIDE)
+#define DW_SPI_G2_XIP_MODE_BITS(cs) (DW_SPI_G2_XIP_CTRL(cs) + 0x04)
+#define DW_SPI_G2_XIP_INCR_INST(cs) (DW_SPI_G2_XIP_CTRL(cs) + 0x08)
+#define DW_SPI_G2_XIP_WRAP_INST(cs) (DW_SPI_G2_XIP_CTRL(cs) + 0x0C)
+
+/* XIP Write registers (per-CS stride = 0x0C: 3 regs × 4 bytes) */
+#define DW_SPI_G2_XIP_WR_CTRL_BASE 0x270
+#define DW_SPI_G2_XIP_WR_CTRL_STRIDE 0x0C
+#define DW_SPI_G2_XIP_WRITE_CTRL(cs) \
+ (DW_SPI_G2_XIP_WR_CTRL_BASE + (cs) * DW_SPI_G2_XIP_WR_CTRL_STRIDE)
+#define DW_SPI_G2_XIP_WRITE_INCR_INST(cs) (DW_SPI_G2_XIP_WRITE_CTRL(cs) + 0x04)
+#define DW_SPI_G2_XIP_WRITE_WRAP_INST(cs) (DW_SPI_G2_XIP_WRITE_CTRL(cs) + 0x08)
+
+/* XIP_CTRL fields (SSI_CSx_XIP_CTRL register) */
+#define DW_SPI_G2_XIP_CTRL_FRF_MASK GENMASK(1, 0)
+#define DW_SPI_G2_XIP_CTRL_TRANS_TYPE_MASK GENMASK(3, 2)
+#define DW_SPI_G2_XIP_CTRL_ADDR_L_MASK GENMASK(7, 4)
+#define DW_SPI_G2_XIP_CTRL_INST_L_MASK GENMASK(10, 9)
+#define DW_SPI_G2_XIP_CTRL_MD_BITS_EN BIT(12)
+#define DW_SPI_G2_XIP_CTRL_WAIT_CYCLES_MASK GENMASK(17, 13)
+#define DW_SPI_G2_XIP_CTRL_DFS_HC BIT(18)
+#define DW_SPI_G2_XIP_CTRL_DDR_EN BIT(19)
+#define DW_SPI_G2_XIP_CTRL_INST_DDR_EN BIT(20)
+#define DW_SPI_G2_XIP_CTRL_RXDS_EN BIT(21)
+#define DW_SPI_G2_XIP_CTRL_INST_EN BIT(22)
+#define DW_SPI_G2_XIP_CTRL_CONT_XFER_EN BIT(23)
+#define DW_SPI_G2_XIP_CTRL_HYPERBUS_EN BIT(24)
+#define DW_SPI_G2_XIP_CTRL_RXDS_SIG_EN BIT(25)
+#define DW_SPI_G2_XIP_CTRL_XIP_MBL_MASK GENMASK(27, 26)
+#define DW_SPI_G2_XIP_CTRL_PREFETCH_EN BIT(29)
+#define DW_SPI_G2_XIP_CTRL_RXDS_VL_EN BIT(30)
+
+/* XIP_WRITE_CTRL fields (SSI_CSx_XIP_WRITE_CTRL register) */
+#define DW_SPI_G2_XIP_WR_CTRL_FRF_MASK GENMASK(1, 0)
+#define DW_SPI_G2_XIP_WR_CTRL_TRANS_TYPE_MASK GENMASK(3, 2)
+#define DW_SPI_G2_XIP_WR_CTRL_ADDR_L_MASK GENMASK(7, 4)
+#define DW_SPI_G2_XIP_WR_CTRL_INST_L_MASK GENMASK(9, 8)
+#define DW_SPI_G2_XIP_WR_CTRL_DDR_EN BIT(10)
+#define DW_SPI_G2_XIP_WR_CTRL_INST_DDR_EN BIT(11)
+#define DW_SPI_G2_XIP_WR_CTRL_HYPERBUS_EN BIT(12)
+#define DW_SPI_G2_XIP_WR_CTRL_RXDS_SIG_EN BIT(13)
+#define DW_SPI_G2_XIP_WR_CTRL_DM_EN BIT(14)
+#define DW_SPI_G2_XIP_WR_CTRL_WAIT_CYCLES_MASK GENMASK(20, 16)
+#define DW_SPI_G2_XIP_WR_CTRL_DFS_HC BIT(21)
+
+/*
+ * NOR-XIP-write helper constants (used by dw_spi_g2_dirmap_write_nor_xip())
+ *
+ * The XIP_WRITE_CTRL register has no auto-WREN and no auto-WIP-poll
+ * support, so a NOR Page Program issued through the XIP-write engine
+ * must be wrapped per-byte with WREN + 1-byte XIP store + WIP poll.
+ * These opcodes are JEDEC standard and apply to all SPI-NOR devices.
+ */
+#define DW_SPI_G2_NOR_OP_WREN 0x06 /* Write Enable */
+#define DW_SPI_G2_NOR_OP_RDSR1 0x05 /* Read Status Register 1 */
+#define DW_SPI_G2_NOR_SR1_WIP BIT(0) /* Write-In-Progress in SR1 */
+#define DW_SPI_G2_NOR_WIP_TIMEOUT_MS 100 /* WIP poll timeout */
+
#define DW_SPI_G2_SSIENR 0x4C
#define DW_SPI_G2_SER 0x54
#define DW_SPI_G2_TXFTLR 0x58
@@ -49,6 +131,7 @@
#define DW_SPI_G2_ICR 0x7C
#define DW_SPI_G2_IDR 0x88
+
/* ISR bit masks (interrupt status) */
#define DW_SPI_G2_ISR_TXEI BIT(0) /* TX FIFO Empty */
#define DW_SPI_G2_ISR_TXOI BIT(1) /* TX FIFO Overflow */
@@ -76,7 +159,32 @@
/* Clear all writable bits */
#define DW_SPI_G2_ICR_ALL 0x0003AFFF
-#define DW_SPI_G2_RX_SAMPLE_DELAY 0x24
+/*
+ * Per-CS DDR drive edge registers.
+ * Controls which clock edge drives output data in DDR/DTR mode.
+ * CSL offsets: CS0=0x218, CS1=0x21C, CS2=0x220, CS3=0x224 (stride=4).
+ */
+#define DW_SPI_G2_DDR_DRIVE_EDGE_BASE 0x218
+#define DW_SPI_G2_DDR_DRIVE_EDGE(cs) (DW_SPI_G2_DDR_DRIVE_EDGE_BASE + (cs) * 0x04)
+
+/*
+ * Per-CS RX sample delay registers.
+ * The register at 0x24 is DEDICATED_CS_RX_SAMPLE_DELAY_OFFSET (a pointer
+ * register, resetval = 0x200). The actual per-CS data registers start at
+ * 0x204 with a 4-byte stride.
+ */
+#define DW_SPI_G2_RX_SAMPLE_DLY_BASE 0x204
+#define DW_SPI_G2_RX_SAMPLE_DELAY(cs) (DW_SPI_G2_RX_SAMPLE_DLY_BASE + (cs) * 0x04)
+
+
+/* CTRLR0 CLK_LOOP_EN (cleared for RX_SAMPLE_DELAY / RXDS modes) */
+#define DW_SPI_G2_CTRLR0_CLK_LOOP_EN BIT(18)
+
+/* RX sampling modes (mutually exclusive) */
+enum dw_spi_g2_sampling_mode {
+ DW_SPI_G2_SAMPLING_RX_DELAY, /* RX_SAMPLE_DELAY register */
+ DW_SPI_G2_SAMPLING_DATA_STROBE, /* RXDS/DQS from flash (DTR mode) */
+};
/* CTRLR0 fields */
#define DW_SPI_G2_CTRLR0_DFS_MASK GENMASK(4, 0)
@@ -198,6 +306,9 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)
/* Default FIFO depth */
#define DW_SPI_G2_FIFO_LEN 256U
+/* Maximum chip selects supported */
+#define DW_SPI_G2_MAX_CS 4U
+
/*
* Maximum baud rate divisor value for BAUDR register.
* BAUDR encoding: bit[0]=passthrough_en, bits[15:1]=SCKDV (divisor).
@@ -223,7 +334,7 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)
* fifo_len entries (typically 256); we use a compile-time cap here and
* check against the runtime fifo_len in is_reg_op().
*/
- #define DW_SPI_G2_REG_OP_BUF_SIZE 256U
+#define DW_SPI_G2_REG_OP_BUF_SIZE 256U
struct dw_spi_g2_cfg {
u8 tmode;
@@ -252,7 +363,7 @@ struct dw_spi_g2 {
/* Current message transfer state info */
const void *tx;
unsigned int tx_len;
- void *rx;
+ void *rx;
unsigned int rx_len;
u8 n_bytes;
@@ -260,9 +371,32 @@ struct dw_spi_g2 {
struct completion mem_done;
int error;
+ /* XIP (Direct Mapping) support */
+ void __iomem *xip_mem;
+ resource_size_t xip_phys;
+ size_t xip_len;
+ bool use_xip;
+ bool xip_continuous_mode;
+ bool xip_prefetch_mode;
+
+ /* Per-CS RX sample delay (in SSI clock cycles) */
+ u8 rx_sample_dly[DW_SPI_G2_MAX_CS];
+ u8 cur_rx_sample_dly;
+ u32 def_rx_sample_dly_ns;
+
/* Flash type detection for timing workarounds */
bool is_nand;
+
+ /* DMA support for FIFO/Indirect operations (External DMA) */
+ struct dma_chan *tx_chan;
+ struct dma_chan *rx_chan;
+ struct completion tx_dma_complete;
+ struct completion rx_dma_complete;
+ bool use_dma_indirect; /* DMA for FIFO operations */
+ bool use_dma_read; /* DMA for XIP (placeholder) */
+ u32 dma_caps; /* DMA capability flags */
+
#ifdef CONFIG_DEBUG_FS
struct dentry *debugfs;
struct debugfs_regset32 regset;
@@ -309,6 +443,9 @@ int dw_spi_g2_init(struct dw_spi_g2 *dws);
void dw_spi_g2_deinit(struct dw_spi_g2 *dws);
+int dw_spi_g2_hw_init(struct dw_spi_g2 *dws);
+
+
void dw_spi_g2_update_config(struct dw_spi_g2 *dws, struct spi_device *spi,
struct dw_spi_g2_cfg *cfg);
@@ -319,6 +456,17 @@ int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op);
irqreturn_t dw_spi_g2_irq(int irq, void *dev_id);
+/* XIP/Direct mapping support */
+int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc);
+ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
+ u64 offs, size_t len, void *buf);
+ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
+ u64 offs, size_t len, const void *buf);
+
+u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op);
+void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq);
+
/* Helpers */
static inline void dw_spi_g2_enable(struct dw_spi_g2 *dws, bool en)
{
@@ -397,4 +545,8 @@ static inline void dw_spi_g2_transfer_end(struct dw_spi_g2 *dws)
dw_spi_g2_target_disable(dws, dws->cs);
}
+/* Sampling mode configuration */
+void dw_spi_g2_set_sampling_mode(struct dw_spi_g2 *dws, u8 cs,
+ enum dw_spi_g2_sampling_mode mode);
+
#endif /* __SPI_DW_G2_H__ */
--
2.34.1
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH 6/6] spi: dw-g2: Add XIP support
2026-11-19 10:05 ` [PATCH 6/6] spi: dw-g2: Add XIP support Prakriti Sharma
@ 2026-11-19 10:32 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-19 10:32 UTC (permalink / raw)
To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] spi: dw-g2: broken NOR XIP write path in
dw_spi_g2_dirmap_write()
- [High] spi: dw-g2: address truncation and shift overflow in
dw_spi_g2_dirmap_read()
- [High] spi: dw-g2: ignored desc->info.offset and bounds overflow in
dirmap ops
- [High] spi: dw-g2: stale XIP_CTRL and XIP_WRITE_CTRL left set for
exec_mem_op
- [High] spi: dw-g2: MMIO write after reset and missing put in
dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: sampling mode bits clobbered after
dw_spi_g2_set_sampling_mode()
- [Medium] spi: dw-g2: broken per-CS rx_sample_dly tracking and missing
DT default
- [Medium] spi: dw-g2: 8-bit mask on 16-bit BAUDR in
dw_spi_g2_update_ddr_drive_edge()
- [Medium] spi: dw-g2: dw_spi_g2_op_freq() ignores max_speed_hz and
reg_op skips BAUDR
- [Medium] spi: dw-g2: missing dw_spi_g2_deinit() on probe failure leaks
debugfs
- [Medium] spi: dw-g2: missing op validation in dirmap_create and
unchecked errors
- [Low] spi: dw-g2: unused DMA struct fields and duplicate undocumented
sysfs files
--
commit 31552d5f4b52b9bd42041e49fac8bf977363c65c
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
spi: dw-g2: Add XIP support
This commit adds memory-mapped execute-in-place (XIP) support via the
spi-mem direct-mapping interface, along with runtime mode controls, per-op
baud rate programming, and RX sampling configuration.
> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index a94d4a7870dfd..2e265459381d1 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
[ ... ]
> @@ -152,28 +261,61 @@ int dw_spi_g2_setup(struct spi_device *spi)
> dw_spi_g2_target_disable(dws, dws->cs);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
>
> + /* Configure RX sample delay for chip select */
> + if (dws->cs < DW_SPI_G2_MAX_CS) {
> + u32 rx_dly_ns;
> +
> + if (device_property_read_u32(&spi->dev, "rx-sample-delay-ns",
> + &rx_dly_ns))
> + rx_dly_ns = dws->def_rx_sample_dly_ns;
> +
> + dws->rx_sample_dly[dws->cs] =
> + DIV_ROUND_CLOSEST(rx_dly_ns,
> + NSEC_PER_SEC / dws->max_freq);
> + }
[ ... ]
> @@ -230,6 +372,30 @@ static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
> return ctrlr0;
> }
>
> +static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
> +{
> + int drain = 0;
> +
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
> +
> + /* Drain any stale RX data left from previous transfers */
> + while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
> + drain < dws->fifo_len) {
> + (void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
> + drain++;
> + }
> +
> + /* Update RX sample delay for current CS if value differs. */
> + if (dws->cs < DW_SPI_G2_MAX_CS &&
> + dws->rx_sample_dly[dws->cs] != dws->cur_rx_sample_dly) {
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs),
> + dws->rx_sample_dly[dws->cs]);
> + dws->cur_rx_sample_dly = dws->rx_sample_dly[dws->cs];
> + }
> +}
[Severity: Medium]
Does tracking per-CS DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) registers with a
single scalar dws->cur_rx_sample_dly skip programming other chip selects?
If CS0 and CS1 share the same non-zero rx_sample_dly value, the first
transfer on CS0 updates dws->cur_rx_sample_dly, causing subsequent
transfers on CS1 to see a match and leave DW_SPI_G2_RX_SAMPLE_DELAY(1)
unprogrammed.
In addition, dws->def_rx_sample_dly_ns is never read from the controller
device node during probe, XIP transfers never call
dw_spi_g2_controller_reset() to program DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs),
and dw_spi_g2_hw_init() does not reset the cached delay state after a
hardware reset. Writing DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) directly in
dw_spi_g2_setup() and restoring it in dw_spi_g2_hw_init() would avoid the
stale cache.
[ ... ]
> @@ -629,6 +808,90 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
> return ret;
> }
>
> +/**
> + * dw_spi_g2_op_freq - Choose the SPI clock frequency for a given op
> + * @dws: Driver state
> + * @op: SPI memory operation
> + *
> + * Prefer op->max_freq. If it is unset, use a conservative fraction of
> + * the controller clock rather than dws->max_freq, which can overclock
> + * a slow flash.
> + *
> + * Return: Frequency in Hz to program into BAUDR.
> + */
> +u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> +{
> + u32 f = op->max_freq;
> +
> + if (!f)
> + f = dws->max_freq / 8;
> +
> + return f;
> +}
[Severity: Medium]
Can dw_spi_g2_op_freq() ignore the target device's spi-max-frequency?
Unlike spi_mem_exec_op(), the spi_mem_dirmap_create(),
spi_mem_dirmap_read(), and spi_mem_dirmap_write() paths do not run
spi_mem_adjust_op_freq() on desc->info.op_tmpl, so op->max_freq is 0 on
standard SPI-NOR and SPI-NAND dirmap templates. Falling back to
dws->max_freq / 8 instead of dws->curr_spi->max_speed_hz (and not clamping
f to dws->curr_spi->max_speed_hz) can overclock flashes rated below
dws->max_freq / 8 or throttle faster flashes.
Also, while dw_spi_g2_mem_caps advertises .per_op_freq = true, neither
dw_spi_g2_reg_op_setup() nor dw_spi_g2_cmd_only_setup() calls
dw_spi_g2_program_baudr(), so small transfers handled by dw_spi_g2_reg_op()
and command-only ops run at whatever BAUDR the previous operation left
behind.
> +
> +/**
> + * dw_spi_g2_program_baudr - Program per-CS BAUDR for a given target freq
> + * @dws: Driver state
> + * @freq: Target SPI clock frequency in Hz
> + *
> + * SCKDV must be even (or 1 for passthrough). Must be called with SSI
> + * disabled. If @freq is 0, fall back to a conservative fraction of the
> + * controller clock. No-op if no valid frequency is available or the
> + * active CS is out of range.
> + */
> +void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
> +{
> + u32 div, baud;
> +
> + if (dws->cs >= DW_SPI_G2_MAX_CS)
> + return;
> +
> + if (!freq)
> + freq = dws->max_freq / 8;
> + if (!freq)
> + return;
> +
> + div = DIV_ROUND_UP(dws->clk_rate, freq);
> + if (div <= 1) {
> + baud = 1; /* Passthrough mode */
> + } else {
> + if (div & 1)
> + div++; /* SCKDV must be even */
> + baud = div;
> + }
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
> +}
> +
> +/**
> + * dw_spi_g2_update_ddr_drive_edge - Program per-CS DDR transmit drive edge
> + * @dws: Driver state
> + * @op: SPI memory operation (must have a DDR/DTR phase)
> + *
> + * At BAUDR 1 or 2 the drive edge is set for edge-aligned data so the TX
> + * delay line can centre the clock; at higher divisors the register returns
> + * to 0 (edge-aligned with sclk_out, default DDR behaviour).
> + */
> +static void dw_spi_g2_update_ddr_drive_edge(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op)
> +{
> + u32 baud;
> +
> + if (!op->cmd.dtr && !op->addr.dtr && !op->data.dtr)
> + return;
> +
> + if (dws->cs >= DW_SPI_G2_MAX_CS)
> + return;
> +
> + baud = dw_spi_g2_readl(dws->regs, DW_SPI_G2_BAUDR(dws->cs)) & 0xFF;
> + if (!baud)
> + baud = 1;
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs),
> + (baud <= 2) ? 1 : 0);
> +}
[Severity: Medium]
Should dw_spi_g2_update_ddr_drive_edge() mask DW_SPI_G2_BAUDR(dws->cs) with
0xFFFF instead of 0xFF? Because BAUDR is a 16-bit divisor register,
masking with 0xFF truncates any divisor >= 256 to its low byte and
misprograms DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs) to 1 whenever the low byte is
0 or 2 (such as divisors 256 or 258).
In addition, dw_spi_g2_program_baudr() does not clamp baud to
DW_SPI_G2_MAX_BAUDR (65534) the way dw_spi_g2_setup() does, so a low
frequency producing div >= 65535 rounds up to 0x10000 and writes 0 into the
16-bit divisor field.
[ ... ]
> @@ -823,9 +1106,25 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
> if (op->addr.dtr || op->data.dtr)
> spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
>
> + /*
> + * For writes, sampling mode is less critical but enforce
> + * mutual exclusivity for consistency with read path.
> + */
> + if (op->data.dtr) {
> + dw_spi_g2_set_sampling_mode(dws, dws->cs,
> + DW_SPI_G2_SAMPLING_DATA_STROBE);
> + } else {
> + dw_spi_g2_set_sampling_mode(dws, dws->cs,
> + DW_SPI_G2_SAMPLING_RX_DELAY);
> + }
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
[Severity: Medium]
Doesn't writing ctrlr0 and spictrlr0 here immediately overwrite the
register bits that dw_spi_g2_set_sampling_mode() just modified in hardware?
Because local spictrlr0 in dw_spi_g2_indirect_write_setup() never sets
DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK, the DW_SPI_G2_SPI_CTRLR0(dws->cs)
write clobbers the strobe bit set by dw_spi_g2_set_sampling_mode().
Similarly, in dw_spi_g2_indirect_read_setup(), dw_spi_g2_set_sampling_mode()
reads and writes the stale DW_SPI_G2_SPI_CTRLR0(dws->cs) value in hardware
right before it is overwritten with local spictrlr0, and in
dw_spi_g2_xip_setup() calling dw_spi_g2_set_sampling_mode() sets
DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK in DW_SPI_G2_SPI_CTRLR0(dws->cs)
after the XIP callers explicitly zeroed SPI_CTRLR0.
[ ... ]
> +int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc)
> +{
> + struct spi_device *spi = desc->mem->spi;
> + struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> + const struct spi_mem_op *op = desc->info.op_tmpl;
> + u32 ctrlr0;
> + int ret;
> +
> + if (!dws->xip_mem) {
> + dev_dbg(dws->dev, "XIP memory region not available\n");
> + return -EOPNOTSUPP;
> + }
> +
> + dev_dbg(dws->dev, "dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n",
> + op->cmd.opcode, op->addr.nbytes,
> + op->data.dir == SPI_MEM_DATA_IN ? "READ" : "WRITE",
> + op->dummy.nbytes);
> +
> + dws->cs = spi_get_chipselect(spi, 0);
> + dws->curr_spi = spi;
> +
> + /*
> + * Configure CTRLR0 with data frame size and SPI mode settings.
> + * XIP mode uses these settings for all memory-mapped accesses.
> + */
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_target_disable(dws, dws->cs);
> +
> + ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> + DW_SPI_G2_FRF_STANDARD);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +
> + /* Configure XIP registers for read/write operations */
> + ret = dw_spi_g2_xip_setup(dws, op,
> + op->data.dir == SPI_MEM_DATA_OUT);
> + /*
> + * xip_setup enables the target (sets BIT(cs) in
> + * DEDICATED_CS_EN) to program per-CS registers.
> + * Clean it up so stale bits don't leak into
> + * cmd_only / reg_op operations on other CS lines.
> + * dirmap_read/write will re-enable when needed.
> + */
> + dw_spi_g2_target_disable(dws, dws->cs);
> + return ret;
> +}
[Severity: Medium]
Does dw_spi_g2_dirmap_create() validate the operation template before
accepting the direct mapping? spi_mem_dirmap_create() calls
ctlr->mem_ops->dirmap_create() without calling supports_op first, and
neither dw_spi_g2_dirmap_create() nor dw_spi_g2_xip_setup() checks
dw_spi_g2_supports_mem_ops(desc->mem, op), dws->cs < DW_SPI_G2_MAX_CS,
op->cmd.nbytes <= 2, or op->addr.nbytes * 8U <= DW_SPI_G2_ADDR_L_MAX_BITS.
Also, the return values of dw_spi_g2_xip_setup() (in
dw_spi_g2_dirmap_read(), dw_spi_g2_dirmap_write_nor_xip(), and
dw_spi_g2_dirmap_write()) and dw_spi_g2_wait_idle() (in
dw_spi_g2_cmd_only(), dw_spi_g2_dirmap_write_nor_xip(), and
dw_spi_g2_dirmap_write()) are ignored, masking setup errors and controller
timeouts as successful transfers.
> +
> +/**
> + * dw_spi_g2_dirmap_read - Direct memory-mapped read from flash
> + * @desc: Direct mapping descriptor
> + * @offs: Offset within the mapped region
> + * @len: Number of bytes to read
> + * @buf: Destination buffer
> + *
> + * Performs direct memory-mapped read using XIP mode. The controller
> + * automatically sends the configured command and address for each access.
> + * Supports 8/16/32-bit data frame sizes for optimal performance.
> + *
> + * Return: Number of bytes read on success, negative error code on failure
> + */
> +ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
> + u64 offs, size_t len, void *buf)
> +{
> + struct spi_device *spi = desc->mem->spi;
> + struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> + void __iomem *src;
> + u32 ctrlr0;
> +
> + dws->cs = spi_get_chipselect(spi, 0);
> + dws->curr_spi = spi;
> +
> + if (!dws->use_xip) {
> + struct spi_mem_op op = *desc->info.op_tmpl;
> + size_t chunk, done = 0;
> + unsigned int dummy_shift = 0;
> + int ret;
> +
> + /* Clear XIP registers so they don't interfere with RO mode */
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CTRL(dws->cs), 0);
> +
> + /*
> + * Fold dummy bytes into the address field.
> + *
> + * To handle WAIT_CYCLES in RO mode --
> + * the RX FIFO stays empty when WAIT_CYCLES > 0. Work around
> + * this by absorbing the dummy phase into the address: shift the
> + * real address left by (dummy_nbytes * 8) bits and zero-pad.
> + * This makes WAIT_CYCLES=0 while the flash still receives the
> + * correct number of clock cycles before data.
> + *
> + * With TT1 (1-1-x), both address and folded-dummy bits travel
> + * on the standard single lane, so the flash sees:
> + * [real addr MSB..LSB] [zero-pad dummy clocks] [data on x lanes]
> + */
> + if (op.dummy.nbytes) {
> + dummy_shift = op.dummy.nbytes * 8;
> + op.addr.nbytes += op.dummy.nbytes;
> + op.dummy.nbytes = 0;
> + }
> +
> + while (done < len) {
> + chunk = min_t(size_t, len - done, dws->fifo_len);
> +
> + op.addr.val = (u64)(offs + done) << dummy_shift;
> + op.data.nbytes = chunk;
> + op.data.buf.in = (u8 *)buf + done;
> +
> + ret = dw_spi_g2_indirect_read(dws, &op);
> + if (ret)
> + return ret;
> +
> + done += chunk;
> + }
> +
> + return (ssize_t)len;
> + }
[Severity: High]
Doesn't folding op.dummy.nbytes into op.addr.nbytes and shifting
op.addr.val left by dummy_shift truncate the flash address in
dw_spi_g2_indirect_read()? Because dws->use_xip defaults to false while
dw_spi_g2_dirmap_create() succeeds whenever dws->xip_mem is present,
default dirmap reads take this path and call dw_spi_g2_indirect_read(),
which casts chunk_op.addr.val to u32 and writes only a single 32-bit word:
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_indirect_read() {
...
if (chunk_op.addr.nbytes) {
dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
(u32)chunk_op.addr.val);
tx_entries++;
}
...
}
Whenever the original op.addr.nbytes + op.dummy.nbytes exceeds 4 bytes
(such as a 3-byte or 4-byte SPI-NOR read with dummy bytes, or a 2-byte
SPI-NAND 1-4-4 read with 4 dummy bytes), shifting left by dummy_shift
pushes the upper address bits (or the entire address when
op.dummy.nbytes >= 4) past bit 31 where the u32 cast discards them, reading
from the wrong flash offset. When op.dummy.nbytes >= 8 (such as Octal or
Octal DTR reads), dummy_shift >= 64 triggers an undefined 64-bit shift and
overflows the 4-bit DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK field in
dw_spi_g2_indirect_read_setup(). Moreover, when op.dummy.buswidth > 1 in
1-1-x (TT0) mode, op.dummy.nbytes is sized for the wider dummy buswidth
rather than a single address lane, so adding op.dummy.nbytes to
op.addr.nbytes sends the wrong number of dummy clock cycles.
> +
> + if (!dws->xip_mem)
> + return -EOPNOTSUPP;
> +
> + if (offs + len > dws->xip_len)
> + return -EINVAL;
> +
> + /*
> + * Restore full XIP register state. An indirect transfer (from a
> + * prior mode switch) may have left CTRLR0 in TMOD_RO/TO and
> + * SPI_CTRLR0 with non-XIP framing, which corrupts XIP accesses.
> + */
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_target_disable(dws, dws->cs);
> + ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> + DW_SPI_G2_FRF_STANDARD);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> + dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, false);
> +
> + src = dws->xip_mem + offs;
[Severity: High]
Shouldn't dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() add
desc->info.offset to offs? In the spi-mem dirmap interface, offs is
relative to desc->info.offset; for example, spinand_create_dirmap() encodes
the plane select bit in info.offset, so ignoring desc->info.offset here
(and in the indirect fallback and write paths) causes multi-plane SPI-NAND
reads and writes on plane >= 1 to access plane 0 instead.
Also, dw_spi_g2_dirmap_create() never checks that desc->info.offset and
desc->info.length fit within dws->xip_len (preventing
spi_mem_dirmap_create() from falling back to non-dirmap ops when the flash
exceeds the XIP window), and the check offs + len > dws->xip_len in
dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() can wrap around on
64-bit unsigned addition.
> +
> + /* Enable XIP mode for read */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, BIT(dws->cs));
> + dw_spi_g2_enable(dws, true);
> +
> + /*
> + * PIO read from XIP window. The XIP engine turns each AHB read
> + * into a (cmd + addr + dummy + data) SPI transaction.
> + */
> + memcpy_fromio(buf, src, len);
> +
> + /* Disable XIP mode and clean up DEDICATED_CS_EN */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_target_disable(dws, dws->cs);
> +
> + return (ssize_t)len;
> +}
[Severity: High]
Does leaving DW_SPI_G2_XIP_CTRL(dws->cs) and
DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) non-zero after dw_spi_g2_dirmap_create(),
dw_spi_g2_dirmap_read(), and dw_spi_g2_dirmap_write() interfere with
subsequent dw_spi_g2_exec_mem_op() FIFO transfers? As noted in the
comments in the indirect branches of dw_spi_g2_dirmap_read() and
dw_spi_g2_dirmap_write(), non-zero XIP_CTRL and XIP_WRITE_CTRL registers
cause the controller to misinterpret FIFO RO/TO-mode operations, yet
neither the XIP teardown paths nor the exec_mem_op setup helpers
(dw_spi_g2_cmd_only_setup(), dw_spi_g2_reg_op_setup(),
dw_spi_g2_indirect_read_setup(), dw_spi_g2_indirect_write_setup()) clear
those registers.
[ ... ]
> +ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
> + u64 offs, size_t len, const void *buf)
> +{
> + struct spi_device *spi = desc->mem->spi;
> + struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> + void __iomem *dst;
> + u32 ctrlr0;
> + bool use_indirect = false;
> +
> + dws->cs = spi_get_chipselect(spi, 0);
> + dws->curr_spi = spi;
> +
> + /*
> + * NOR Page Program (addr_nbytes >= 3) cannot ride the XIP-write
> + * engine in burst mode -- the engine emits one cmd+addr+data SPI
> + * transaction per AHB write but the SPI-NOR framework only issues
> + * WREN once before dirmap_write(), so every transaction past the
> + * first arrives with write-enable cleared and is silently dropped.
> + * NAND Program Load (addr_nbytes == 2) is XIP-friendly because the
> + * on-die page buffer has no per-byte write-enable gating, so it goes
> + * to the NAND-XIP-BURST path below.
> + */
> + if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
> + use_indirect = true;
> +
> + /*
> + * NOR with XIP enabled: route through the byte-by-byte XIP-write
> + * helper so the XIP-write engine is exercised end-to-end.
> + */
> + if (use_indirect && dws->use_xip && dws->xip_mem &&
> + desc->info.op_tmpl->addr.nbytes >= 3) {
> + dev_info(dws->dev,
> + "dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
> + desc->info.op_tmpl->cmd.opcode,
> + (unsigned long long)offs, len);
> + return dw_spi_g2_dirmap_write_nor_xip(dws, desc, offs, len,
> + buf);
> + }
[Severity: High]
Why does dw_spi_g2_dirmap_write() route NOR writes to
dw_spi_g2_dirmap_write_nor_xip() when the commit message states that NOR
page-program writes stay on the indirect FIFO path?
In dw_spi_g2_dirmap_write_nor_xip(), the controller driver bypasses the
spi-nor layer and hardcodes 1-1-1 SDR WREN (0x06) and RDSR1 (0x05) commands
along with 1-byte writeb() transactions for every byte:
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_dirmap_write_nor_xip() {
const struct spi_mem_op wren_op = SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_WREN, 1),
SPI_MEM_OP_NO_ADDR,
SPI_MEM_OP_NO_DUMMY,
SPI_MEM_OP_NO_DATA);
...
for (i = 0; i < len; i++) {
const struct spi_mem_op rdsr_op =
SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_RDSR1, 1),
SPI_MEM_OP_NO_ADDR,
SPI_MEM_OP_NO_DUMMY,
SPI_MEM_OP_DATA_IN(1, &sr1, 1));
...
ret = dw_spi_g2_cmd_only(dws, &wren_op);
}
This breaks flashes operating in 4-4-4 QPI, 8-8-8 Octal, or 8D-8D-8D Octal
DTR mode (where 1-1-1 SDR 0x06/0x05 commands are invalid and 1-byte page
programs violate 2-byte DTR / internal-ECC alignment), and it invokes
dw_spi_g2_cmd_only() (a TO-mode FIFO transfer) while
DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) is still non-zero.
In addition, all three routing branches in dw_spi_g2_dirmap_write() call
dev_info() on every write call, which will flood the kernel log during
normal flash writes.
> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> index 6ef8f926e0eb9..9620ae6e5c23c 100644
> --- a/drivers/spi/spi-dw-g2-mmio.c
> +++ b/drivers/spi/spi-dw-g2-mmio.c
[ ... ]
> @@ -151,7 +386,29 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
> if (ret)
> goto err_pm;
>
> - dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
> + ret = device_create_file(&pdev->dev, &dev_attr_xip_mode);
> + if (ret)
> + dev_warn(&pdev->dev,
> + "Failed to create xip_mode sysfs attribute: %d\n",
> + ret);
> +
> + if (dws->xip_mem) {
> + ret = device_create_file(&pdev->dev,
> + &dev_attr_xip_continuous_mode);
> + if (ret)
> + dev_warn(&pdev->dev,
> + "Failed to create xip_continuous_mode sysfs: %d\n",
> + ret);
> +
> + ret = device_create_file(&pdev->dev,
> + &dev_attr_xip_prefetch_mode);
> + if (ret)
> + dev_warn(&pdev->dev,
> + "Failed to create xip_prefetch_mode sysfs: %d\n",
> + ret);
> + }
> + dev_info(&pdev->dev, "DW SSI G2 MMIO probed (mode=%s)\n",
> + dws->use_xip ? "XIP" : "indirect");
> return 0;
>
> err_pm:
[Severity: Medium]
If devm_request_irq() or spi_register_controller() fails after
dw_spi_g2_init(dws) has created the debugfs directory and files, doesn't
jumping to err_reset or err_pm without calling dw_spi_g2_deinit(dws) leak
the debugfs entries? Because spi_controller_put(ctlr) frees ctlr and the
embedded dws structure on that error path, the leaked debugfs files retain
dangling pointers to dws and dws->regset, causing a use-after-free if they
are accessed in debugfs.
> @@ -168,10 +425,14 @@ static void dw_spi_g2_mmio_remove(struct platform_device *pdev)
> struct spi_controller *ctlr = platform_get_drvdata(pdev);
> struct dw_spi_g2_mmio *dwsmmio = spi_controller_get_devdata(ctlr);
>
> + device_remove_file(&pdev->dev, &dev_attr_xip_mode);
> + device_remove_file(&pdev->dev, &dev_attr_xip_continuous_mode);
> + device_remove_file(&pdev->dev, &dev_attr_xip_prefetch_mode);
> spi_unregister_controller(ctlr);
> dw_spi_g2_deinit(&dwsmmio->dws);
> pm_runtime_disable(&pdev->dev);
> reset_control_assert(dwsmmio->rstc);
> + dw_spi_g2_enable(&dwsmmio->dws, false);
> }
[Severity: High]
Doesn't calling dw_spi_g2_enable(&dwsmmio->dws, false) after
reset_control_assert(dwsmmio->rstc) write to DW_SPI_G2_SSIENR while the
controller is held in hardware reset? On many SoCs, MMIO accesses to an IP
block with its reset line asserted trigger a synchronous external abort or
bus hang; disabling the controller should happen before asserting reset.
Also, dw_spi_g2_mmio_probe() allocates ctlr with non-managed
spi_alloc_host(), and spi_unregister_controller(ctlr) only drops the
reference taken by spi_register_controller(), so dw_spi_g2_mmio_remove()
still needs to call spi_controller_put(ctlr) to avoid leaking ctlr and
dwsmmio.
> diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
> index 3091697238d7d..162d233902982 100644
> --- a/drivers/spi/spi-dw-g2.h
> +++ b/drivers/spi/spi-dw-g2.h
[ ... ]
> @@ -260,9 +371,32 @@ struct dw_spi_g2 {
> struct completion mem_done;
> int error;
>
> + /* XIP (Direct Mapping) support */
> + void __iomem *xip_mem;
> + resource_size_t xip_phys;
> + size_t xip_len;
> + bool use_xip;
> + bool xip_continuous_mode;
> + bool xip_prefetch_mode;
> +
> + /* Per-CS RX sample delay (in SSI clock cycles) */
> + u8 rx_sample_dly[DW_SPI_G2_MAX_CS];
> + u8 cur_rx_sample_dly;
> + u32 def_rx_sample_dly_ns;
> +
> /* Flash type detection for timing workarounds */
> bool is_nand;
>
> +
> + /* DMA support for FIFO/Indirect operations (External DMA) */
> + struct dma_chan *tx_chan;
> + struct dma_chan *rx_chan;
> + struct completion tx_dma_complete;
> + struct completion rx_dma_complete;
> + bool use_dma_indirect; /* DMA for FIFO operations */
> + bool use_dma_read; /* DMA for XIP (placeholder) */
> + u32 dma_caps; /* DMA capability flags */
> +
> #ifdef CONFIG_DEBUG_FS
> struct dentry *debugfs;
> struct debugfs_regset32 regset;
[ ... ]
> @@ -319,6 +456,17 @@ int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op);
>
> irqreturn_t dw_spi_g2_irq(int irq, void *dev_id);
>
> +/* XIP/Direct mapping support */
> +int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc);
> +ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
> + u64 offs, size_t len, void *buf);
> +ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
> + u64 offs, size_t len, const void *buf);
> +
> +u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
> + const struct spi_mem_op *op);
> +void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq);
[Severity: Low]
These seven DMA fields in struct dw_spi_g2 are unused in this patch, and
dw_spi_g2_dirmap_create(), dw_spi_g2_dirmap_read(),
dw_spi_g2_dirmap_write(), dw_spi_g2_op_freq(), dw_spi_g2_program_baudr(),
and dw_spi_g2_set_sampling_mode() are only used within
drivers/spi/spi-dw-g2-core.c and can be static. Also,
dw_spi_g2_mmio_probe() creates undocumented sysfs attributes (xip_mode,
xip_continuous_mode, xip_prefetch_mode) via device_create_file() after
spi_register_controller() that duplicate the debugfs controls mentioned in
the commit message.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=6
^ permalink raw reply [flat|nested] 12+ messages in thread