Devicetree
 help / color / mirror / Atom feed
* [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support
@ 2026-11-30  2:39 Prakriti Sharma
  2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

Add a driver and Device Tree binding for the Synopsys DesignWare SSI Gen2
(DWC_ssi v2.02a) controller.

The controller uses a per-chip-select register map. This posting covers the
MMIO glue, IRQ-driven transfer_one() for regular SPI (spi-mem stays polled),
spi-mem command-only and short register ops, FIFO indirect reads/writes,
SPI-NAND handling, and memory-mapped XIP through spi-mem direct mapping.

The series was exercised with SPI-NOR and Winbond W25N01JW SPI-NAND.

Changes in v2 (addressing v1 review on linux-spi):
- One complete binding in patch 1/5: spi: dt-bindings: subject prefix,
  interrupts required, num-cs 1..4, XIP/reg properties.
- (base driver): MAINTAINERS spi-dw-g2*; drop debugfs;
  dev_warn_once; scaled FIFO timeouts; IRQ full-duplex transfer_one/set_cs;
  selective ICR; mandatory named IRQs; num-cs validation; devm probe.
- (XIP): sysfs-only knobs (no debugfs); normal conditionals; no
  module_param; reject illegal continuous-mode toggles with -EINVAL.
- bitfield.h, kernel-doc section banners, unused tx_entries dropped.

Prakriti Sharma (4):
  spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller
  spi: dw-g2: Add support for indirect read and write for spi-mem
    transfers
  spi: dw-g2: Add support for SPI-NAND devices
  spi: dw-g2: Add support for memory-mapped XIP

Siddhant Kumar (1):
  spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller
    binding

 .../bindings/spi/snps,dwc-ssi-2.02a.yaml      |  191 ++
 MAINTAINERS                                   |    8 +
 drivers/spi/Kconfig                           |   21 +
 drivers/spi/Makefile                          |    3 +
 drivers/spi/spi-dw-g2-core.c                  | 2154 +++++++++++++++++
 drivers/spi/spi-dw-g2-mmio.c                  |  403 +++
 drivers/spi/spi-dw-g2.h                       |  489 ++++
 7 files changed, 3269 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
 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


base-commit: 6ac2371c7e3134613584b34e439fd3e8f6185a6f
v1: https://lore.kernel.org/linux-spi/20261119100513.433044-1-prakriti@synopsys.com/

-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding
  2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
@ 2026-11-30  2:39 ` Prakriti Sharma
  2026-11-30  2:47   ` sashiko-bot
  2026-11-30  2:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

From: Siddhant Kumar <sidkumar@synopsys.com>

Add the Device Tree binding for the Synopsys DesignWare SSI Gen2
(DWC_ssi v2.02a) SPI controller to enable support for this hardware.
Describe the MMIO register region, the optional XIP memory-mapped region,
interrupt topology, and properties for XIP, continuous read, and prefetch
modes to guide SoC integrators and driver authors in describing hardware
properly in Device Tree.

This binding is required to support systems integrating the DWC SSI v2.02a
controller, which features per-chip-select register banks and advanced SPI
modes.

Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
 .../bindings/spi/snps,dwc-ssi-2.02a.yaml      | 191 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 198 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..7294c9b2694b
--- /dev/null
+++ b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
@@ -0,0 +1,191 @@
+# 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.
+
+properties:
+  compatible:
+    const: snps,dwc-ssi-2.02a
+
+  reg:
+    minItems: 1
+    items:
+      - description: SSI controller register space
+      - description: XIP memory-mapped window for direct flash access
+
+  reg-names:
+    minItems: 1
+    items:
+      - const: ssi_base
+      - const: xip_mem
+
+  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.
+
+  snps,xip-enabled:
+    type: boolean
+    description: |
+      Enable the controller XIP engine for memory-mapped flash access through
+      the second reg window (xip_mem). Requires that window to be described.
+
+  snps,xip-continuous-mode:
+    type: boolean
+    description: |
+      Set XIP_CTRL continuous-transfer mode so the controller omits the
+      command byte on subsequent sequential reads while CS stays asserted.
+      The attached flash must support continuous read (for example Fast Read
+      Quad I/O with mode bits). Requires snps,xip-enabled.
+
+  snps,xip-prefetch-mode:
+    type: boolean
+    description: |
+      Set XIP_CTRL hardware prefetch so the controller may fetch the next
+      line after a completed XIP access. Requires snps,xip-enabled.
+
+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
+  - interrupts
+  - "#address-cells"
+  - "#size-cells"
+  - clocks
+
+dependencies:
+  snps,xip-continuous-mode: [ snps,xip-enabled ]
+  snps,xip-prefetch-mode: [ snps,xip-enabled ]
+
+allOf:
+  - $ref: spi-controller.yaml#
+  - if:
+      required:
+        - snps,xip-enabled
+    then:
+      properties:
+        reg:
+          minItems: 2
+        reg-names:
+          minItems: 2
+      required:
+        - reg-names
+
+examples:
+  - |
+    spi@20100000 {
+      compatible = "snps,dwc-ssi-2.02a";
+      reg = <0x20100000 0x1000>, <0x60000000 0x10000000>;
+      reg-names = "ssi_base", "xip_mem";
+      #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>;
+      snps,xip-enabled;
+
+      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] 11+ messages in thread

* [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller
  2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
  2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
@ 2026-11-30  2:39 ` Prakriti Sharma
  2026-11-30  3:01   ` sashiko-bot
  2026-11-30  2:39 ` [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers Prakriti Sharma
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

Introduce a platform driver for the Synopsys DesignWare (DWC) SSI Gen2
(v2.02a) MMIO SPI controller. This driver provides register access helpers,
interrupt-driven transfer_one() support for standard SPI devices, and
implements spi-mem command-only and short register (TR) operations.

It enables standard and extended SPI and spi-mem functionality on modern
platforms, allowing connection to SPI flash and other devices.

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 | 925 +++++++++++++++++++++++++++++++++++
 drivers/spi/spi-dw-g2-mmio.c | 168 +++++++
 drivers/spi/spi-dw-g2.h      | 388 +++++++++++++++
 6 files changed, 1506 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..90b358076e5c 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*
 
 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..b2a3e3e8670e
--- /dev/null
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -0,0 +1,925 @@
+// 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/ktime.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi-mem.h>
+#include <linux/bitfield.h>
+
+#include "spi-dw-g2.h"
+
+/**
+ * dw_spi_g2_baudr - Compute the BAUDR value for a target SCLK rate
+ * @dws: Driver state
+ * @speed: Target SPI clock frequency in Hz
+ *
+ * BAUDR is an even divisor of ssi_clk, except for 1 (passthrough).
+ *
+ * Return: BAUDR value, clamped to the hardware maximum
+ */
+static u32 dw_spi_g2_baudr(struct dw_spi_g2 *dws, u32 speed)
+{
+	u32 div;
+
+	if (!speed)
+		return DW_SPI_G2_MAX_BAUDR;
+
+	div = DIV_ROUND_UP(dws->clk_rate, speed);
+	if (div <= 1)
+		return 1;
+
+	return min(round_up(div, 2), DW_SPI_G2_MAX_BAUDR);
+}
+
+/**
+ * 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, 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_once(&spi->dev,
+			      "speed %u Hz below minimum %u Hz, clamping\n",
+			      speed, min_speed);
+		speed = min_speed;
+	}
+
+	baud = dw_spi_g2_baudr(dws, speed);
+
+	/* 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);
+
+	/* 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);
+
+		if (dws->rx_sample_dly[dws->cs] !=
+		    dws->cur_rx_sample_dly[dws->cs]) {
+			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->cs] =
+				dws->rx_sample_dly[dws->cs];
+		}
+	}
+
+	dev_dbg(&spi->dev,
+		"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;
+}
+
+/**
+ * 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;
+}
+
+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_IMR, 0);
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
+	dws->irq_xfer = false;
+
+	/* 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[dws->cs]) {
+		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->cs] = dws->rx_sample_dly[dws->cs];
+	}
+}
+
+/**
+ * dw_spi_g2_op_is_std_spi - True if @op is single-line SDR SPI
+ * @op: SPI memory operation
+ *
+ * reg_op() / cmd_only() program FRF_STANDARD and SPI_CTRLR0=0, so they
+ * must not claim Dual/Quad/Octal or DTR operations.
+ */
+static bool dw_spi_g2_op_is_std_spi(const struct spi_mem_op *op)
+{
+	if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
+		return false;
+	if (op->cmd.buswidth > 1)
+		return false;
+	if (op->addr.nbytes && op->addr.buswidth > 1)
+		return false;
+	if (op->dummy.nbytes && op->dummy.buswidth > 1)
+		return false;
+	if (op->data.nbytes && op->data.buswidth > 1)
+		return false;
+
+	return true;
+}
+
+/**
+ * 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
+ *
+ * Routes short standard-SPI ops through TR/TO FIFO mode when the total
+ * size fits the FIFO. Multi-wire / DTR ops must use the enhanced RO/TO
+ * paths so they are not mis-executed as 1-1-1.
+ *
+ * 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(struct dw_spi_g2 *dws,
+				       const struct spi_mem_op *op)
+{
+	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 dw_spi_g2_op_is_std_spi(op) &&
+	       op->data.nbytes > 0 && total <= limit;
+}
+
+/**
+ * dw_spi_g2_xfer_timeout_us - Timeout for clocking a number of bytes
+ * @dws: Driver state
+ * @len: Number of bytes on the bus
+ *
+ * Twice the single-line bus time at the BAUDR programmed for the active
+ * chip select, plus a fixed slack for FIFO and scheduling latency.
+ *
+ * Return: Timeout in microseconds
+ */
+static u64 dw_spi_g2_xfer_timeout_us(struct dw_spi_g2 *dws, u32 len)
+{
+	u32 baud = dw_spi_g2_readl(dws->regs, DW_SPI_G2_BAUDR(dws->cs)) & 0xFFFF;
+	u32 hz = dws->clk_rate / max(baud, 1U);
+	u64 us;
+
+	if (!hz)
+		return DW_SPI_G2_TIMEOUT_US;
+
+	us = DIV_ROUND_UP_ULL((u64)len * BITS_PER_BYTE * USEC_PER_SEC, hz);
+
+	return 2 * us + DW_SPI_G2_XFER_SLACK_US;
+}
+
+/**
+ * dw_spi_g2_xfer_fifo - Polled FIFO transfer
+ * @dws: Driver state
+ * @tx_buf: TX buffer, or NULL to send zeros
+ * @tx_len: Number of bytes to transmit
+ * @rx_buf: RX buffer, or NULL to discard received bytes
+ * @rx_len: Number of bytes to receive
+ *
+ * Services the TX and RX FIFOs together so a full-duplex (TMOD=TR)
+ * transfer of any length works. When both directions are active, the
+ * number of frames in flight is bounded by the FIFO depth so the RX FIFO
+ * cannot overflow. The whole transfer has a single deadline derived from
+ * its length and the programmed bus speed.
+ *
+ * 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;
+	ktime_t deadline;
+	u32 sr;
+
+	deadline = ktime_add_us(ktime_get(),
+				dw_spi_g2_xfer_timeout_us(dws,
+							  max(tx_len, rx_len)));
+
+	while (tx_sent < tx_len || rx_got < rx_len) {
+		sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+
+		if (rx_got < rx_len && (sr & DW_SPI_G2_SR_RFNE)) {
+			u8 val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
+
+			if (rx_buf)
+				rx_buf[rx_got] = val;
+			rx_got++;
+			continue;
+		}
+
+		if (tx_sent < tx_len && (sr & DW_SPI_G2_SR_TFNF) &&
+		    (rx_got == rx_len || tx_sent - rx_got < dws->fifo_len)) {
+			dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
+					 tx_buf ? tx_buf[tx_sent] : 0);
+			tx_sent++;
+			continue;
+		}
+
+		if (ktime_after(ktime_get(), deadline)) {
+			dev_err(dws->dev,
+				"FIFO timeout: tx %u/%u rx %u/%u SR=0x%08x\n",
+				tx_sent, tx_len, rx_got, rx_len, sr);
+			return -ETIMEDOUT;
+		}
+		cpu_relax();
+	}
+
+	return dw_spi_g2_wait_idle(dws);
+}
+
+static u32 dw_spi_g2_irq_rx_max(struct dw_spi_g2 *dws)
+{
+	u32 rxflr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR);
+
+	return min(rxflr, dws->irq_rx_rem);
+}
+
+static u32 dw_spi_g2_irq_tx_max(struct dw_spi_g2 *dws)
+{
+	u32 txflr = dws->fifo_len - dw_spi_g2_readl(dws->regs, DW_SPI_G2_TXFLR);
+
+	return min(txflr, dws->irq_tx_rem);
+}
+
+static void dw_spi_g2_irq_reader(struct dw_spi_g2 *dws)
+{
+	u32 max = dw_spi_g2_irq_rx_max(dws);
+
+	while (max--) {
+		u32 idx = dws->irq_len - dws->irq_rx_rem;
+		u8 val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
+
+		if (dws->irq_rx)
+			dws->irq_rx[idx] = val;
+		dws->irq_rx_rem--;
+	}
+}
+
+static void dw_spi_g2_irq_writer(struct dw_spi_g2 *dws)
+{
+	u32 max = dw_spi_g2_irq_tx_max(dws);
+
+	while (max--) {
+		u32 tx_sent = dws->irq_len - dws->irq_tx_rem;
+		u32 rx_got = dws->irq_len - dws->irq_rx_rem;
+
+		if (dws->irq_rx_rem &&
+		    tx_sent - rx_got >= dws->fifo_len)
+			break;
+
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
+				 dws->irq_tx ?
+				 dws->irq_tx[dws->irq_len - dws->irq_tx_rem] : 0);
+		dws->irq_tx_rem--;
+	}
+}
+
+static void dw_spi_g2_irq_finalize(struct dw_spi_g2 *dws, int status)
+{
+	struct spi_controller *ctlr = dws->ctlr;
+
+	dws->irq_xfer = false;
+	dw_spi_g2_mask_intr(dws, DW_SPI_G2_IMR_XFER_ERR);
+	dw_spi_g2_enable(dws, false);
+
+	if (status && ctlr->cur_msg)
+		ctlr->cur_msg->status = status;
+
+	spi_finalize_current_transfer(ctlr);
+}
+
+static void dw_spi_g2_irq_xfer_setup(struct dw_spi_g2 *dws)
+{
+	u32 level;
+
+	level = min_t(u32, dws->fifo_len / 2, dws->irq_len);
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, level);
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, level ? level - 1 : 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;
+}
+
+/**
+ * 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_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+	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[16];
+	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_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);
+	ret = dw_spi_g2_wait_idle(dws);
+
+	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_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+	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 buf[DW_SPI_G2_REG_OP_BUF_SIZE];
+	u32 len = 0;
+	u32 echo_bytes;
+	u32 i;
+	int ret;
+
+	if (op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes +
+	    op->data.nbytes > sizeof(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);
+
+	/*
+	 * Standard-SPI byte stream only (gated by is_reg_op). Use
+	 * fill_tx_buf() so multi-byte opcodes are not truncated.
+	 */
+	if (op->data.dir == SPI_MEM_DATA_OUT) {
+		if (!op->data.buf.out) {
+			dev_err(dws->dev, "TX buffer is NULL\n");
+			return -EINVAL;
+		}
+		len = dw_spi_g2_fill_tx_buf(buf, op, false);
+		if (op->dummy.nbytes) {
+			memset(&buf[len], 0x00, op->dummy.nbytes);
+			len += op->dummy.nbytes;
+		}
+		memcpy(&buf[len], op->data.buf.out, op->data.nbytes);
+		len += op->data.nbytes;
+	} else {
+		if (!op->data.buf.in) {
+			dev_err(dws->dev, "RX buffer is NULL\n");
+			return -EINVAL;
+		}
+		len = dw_spi_g2_fill_tx_buf(buf, op, false);
+		memset(&buf[len], 0x00, op->dummy.nbytes + op->data.nbytes);
+		len += op->dummy.nbytes + op->data.nbytes;
+	}
+
+	echo_bytes = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
+
+	dw_spi_g2_reg_op_setup(dws, op);
+
+	dw_spi_g2_target_enable(dws, dws->cs);
+	dw_spi_g2_enable(dws, true);
+
+	/* dw_spi_g2_is_reg_op() guarantees the whole stream fits the FIFO */
+	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);
+
+	if (op->data.dir == SPI_MEM_DATA_IN) {
+		/* TMOD=TR returns one byte per byte sent */
+		ret = dw_spi_g2_xfer_fifo(dws, NULL, 0, buf, len);
+		if (!ret)
+			memcpy(op->data.buf.in, &buf[echo_bytes],
+			       op->data.nbytes);
+	} else {
+		u32 sr;
+
+		/*
+		 * Prefill + CS: wait until the TX FIFO has drained and the
+		 * shifter is idle. Polling only !BUSY can return early
+		 * before SER propagates into the ssi_clk domain.
+		 */
+		ret = readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
+					 (sr & DW_SPI_G2_SR_TFE) &&
+					 !(sr & DW_SPI_G2_SR_BUSY),
+					 1, DW_SPI_G2_TIMEOUT_US);
+	}
+
+	dw_spi_g2_transfer_end(dws);
+	dw_spi_g2_controller_reset(dws);
+
+	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 unset, fall back to the target device's
+ * spi-max-frequency and clamp to that cap.
+ *
+ * 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;
+	u32 cap = dws->curr_spi ? dws->curr_spi->max_speed_hz : 0;
+
+	if (!f) {
+		if (cap)
+			f = cap;
+		else if (dws->max_freq)
+			f = dws->max_freq;
+	}
+
+	if (cap && f > cap)
+		f = cap;
+
+	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)
+{
+	if (dws->cs >= DW_SPI_G2_MAX_CS)
+		return;
+
+	if (!freq) {
+		if (dws->curr_spi && dws->curr_spi->max_speed_hz)
+			freq = dws->curr_spi->max_speed_hz;
+		else
+			freq = dws->max_freq;
+	}
+	if (!freq)
+		return;
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs),
+			 dw_spi_g2_baudr(dws, freq));
+}
+
+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.nbytes)
+		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);
+
+	if (!op || !op->cmd.nbytes) {
+		dev_err(dws->dev, "Invalid operation: missing command\n");
+		return -EINVAL;
+	}
+
+	if (op->cmd.nbytes > 2) {
+		dev_err(dws->dev, "command length %u exceeds maximum 2 bytes\n",
+			op->cmd.nbytes);
+		return -EINVAL;
+	}
+
+	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;
+	}
+
+	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;
+	}
+
+	dws->cs = spi_get_chipselect(spi, 0);
+	dws->curr_spi = spi;
+
+	if (op->data.dir == SPI_MEM_NO_DATA) {
+		if (!dw_spi_g2_op_is_std_spi(op))
+			return -EOPNOTSUPP;
+		return dw_spi_g2_cmd_only(dws, op);
+	}
+
+	if (dw_spi_g2_is_reg_op(dws, op))
+		return dw_spi_g2_reg_op(dws, op);
+
+	/* Bulk transfers arrive in the indirect-read/write patch. */
+	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,
+};
+
+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_set_cs - Drive the native chip select for a message
+ * @spi: SPI device
+ * @enable: Requested chip select line level
+ */
+static void dw_spi_g2_set_cs(struct spi_device *spi, bool enable)
+{
+	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
+	bool cs_high = !!(spi->mode & SPI_CS_HIGH);
+
+	if (cs_high == enable)
+		dw_spi_g2_cs_assert(dws, spi_get_chipselect(spi, 0));
+	else
+		dw_spi_g2_cs_deassert(dws, spi_get_chipselect(spi, 0));
+}
+
+/**
+ * dw_spi_g2_transfer_one - Execute one transfer for a regular SPI device
+ * @ctlr: SPI controller
+ * @spi: SPI device
+ * @xfer: Transfer to execute
+ *
+ * IRQ-driven full-duplex (TMOD=TR) transfer in standard SPI mode, following
+ * the DesignWare Gen1 pattern (TXEI/RXFI, return 1, finalize from IRQ).
+ * spi-mem and indirect/XIP paths continue to use polled FIFO I/O.
+ *
+ * Return: 1 if the transfer is in progress, 0 if @xfer->len is zero,
+ * or a negative error code on setup failure
+ */
+static 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);
+	u32 ctrlr0;
+
+	if (!xfer->len)
+		return 0;
+
+	dws->cs = spi_get_chipselect(spi, 0);
+	dws->curr_spi = spi;
+
+	dws->irq_tx = xfer->tx_buf;
+	dws->irq_rx = xfer->rx_buf;
+	dws->irq_len = xfer->len;
+	dws->irq_tx_rem = xfer->len;
+	dws->irq_rx_rem = xfer->len;
+
+	ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
+					DW_SPI_G2_FRF_STANDARD);
+	if (spi->mode & SPI_CPOL)
+		ctrlr0 |= DW_SPI_G2_CTRLR0_SCPOL;
+	if (spi->mode & SPI_CPHA)
+		ctrlr0 |= DW_SPI_G2_CTRLR0_SCPHA;
+
+	dw_spi_g2_enable(dws, false);
+	dw_spi_g2_target_disable(dws, dws->cs);
+	dw_spi_g2_mask_intr(dws, DW_SPI_G2_IMR_XFER_ERR);
+
+	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_BAUDR(dws->cs),
+			 dw_spi_g2_baudr(dws, xfer->speed_hz));
+
+	dw_spi_g2_target_enable(dws, dws->cs);
+	dw_spi_g2_enable(dws, true);
+
+	/* Ensure irq_xfer is visible before unmasking TXEI/RXFI. */
+	smp_wmb();
+	dws->irq_xfer = true;
+
+	dw_spi_g2_irq_xfer_setup(dws);
+	dw_spi_g2_irq_writer(dws);
+	dw_spi_g2_umask_intr(dws, DW_SPI_G2_IMR_XFER_ERR);
+
+	return 1;
+}
+
+/**
+ * dw_spi_g2_irq - SPI controller interrupt handler
+ * @irq: IRQ number
+ * @dev_id: Driver state
+ *
+ * Services TXEI/RXFI while transfer_one() has a message in flight. When
+ * idle, only FIFO overflow/underflow sources are handled. Threshold events
+ * are cleared by FIFO service; error sources are acknowledged via ICR.
+ *
+ * Return: IRQ_HANDLED if an event was serviced, IRQ_NONE otherwise
+ */
+irqreturn_t dw_spi_g2_irq(int irq, void *dev_id)
+{
+	struct dw_spi_g2 *dws = dev_id;
+	u32 isr, icr = 0;
+
+	isr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_ISR);
+	if (!isr)
+		return IRQ_NONE;
+
+	if (isr & DW_SPI_G2_ISR_FIFO_ERR) {
+		if (dws->irq_xfer) {
+			icr = isr & DW_SPI_G2_ISR_FIFO_ERR;
+			dev_err_ratelimited(dws->dev,
+					    "FIFO error during transfer: ISR=0x%x\n",
+					    isr);
+			dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, icr);
+			dw_spi_g2_irq_finalize(dws, -EIO);
+			return IRQ_HANDLED;
+		}
+
+		icr = isr & DW_SPI_G2_ISR_FIFO_ERR;
+		dev_warn_ratelimited(dws->dev,
+				     "FIFO error interrupt: ISR=0x%x\n", isr);
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, icr);
+		return IRQ_HANDLED;
+	}
+
+	if (!dws->irq_xfer)
+		return IRQ_NONE;
+
+	dw_spi_g2_irq_reader(dws);
+	if (!dws->irq_rx_rem) {
+		dw_spi_g2_irq_finalize(dws, 0);
+		return IRQ_HANDLED;
+	}
+
+	if (dws->irq_rx_rem <= dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFTLR))
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, dws->irq_rx_rem - 1);
+
+	if (isr & DW_SPI_G2_ISR_TXEI) {
+		dw_spi_g2_irq_writer(dws);
+		if (!dws->irq_tx_rem)
+			dw_spi_g2_mask_intr(dws, DW_SPI_G2_ISR_TXEI);
+	}
+
+	return IRQ_HANDLED;
+}
+EXPORT_SYMBOL_NS_GPL(dw_spi_g2_irq, "SPI_DW_G2_CORE");
+
+int dw_spi_g2_init(struct dw_spi_g2 *dws)
+{
+	struct spi_controller *ctlr = dws->ctlr;
+	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);
+
+	for (cs = 0; cs < DW_SPI_G2_MAX_CS; cs++)
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(cs), 1);
+
+	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;
+
+	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->set_cs = dw_spi_g2_set_cs;
+	ctlr->transfer_one = dw_spi_g2_transfer_one;
+	ctlr->mem_ops = &dw_spi_g2_mem_ops;
+
+	dev_dbg(dws->dev, "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");
+
+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..1be68c4250e6
--- /dev/null
+++ b/drivers/spi/spi-dw-g2-mmio.c
@@ -0,0 +1,168 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.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 device *dev = &pdev->dev;
+	struct dw_spi_g2_mmio *dwsmmio;
+	struct dw_spi_g2 *dws;
+	struct resource *mem;
+	struct spi_controller *ctlr;
+	u32 num_cs = DW_SPI_G2_MAX_CS;
+	int irq, i;
+	int ret;
+
+	ctlr = devm_spi_alloc_host(dev, sizeof(*dwsmmio));
+	if (!ctlr)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, ctlr);
+
+	dwsmmio = spi_controller_get_devdata(ctlr);
+	dws = &dwsmmio->dws;
+
+	dws->dev = dev;
+	dws->ctlr = ctlr;
+
+	/* MMIO */
+	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	dws->regs = devm_ioremap_resource(dev, mem);
+	if (IS_ERR(dws->regs))
+		return PTR_ERR(dws->regs);
+
+	/* clocks */
+	dwsmmio->clk = devm_clk_get_enabled(dev, NULL);
+	if (IS_ERR(dwsmmio->clk))
+		return PTR_ERR(dwsmmio->clk);
+
+	dwsmmio->pclk = devm_clk_get_optional_enabled(dev, "pclk");
+	if (IS_ERR(dwsmmio->pclk))
+		return PTR_ERR(dwsmmio->pclk);
+
+	dws->clk_rate = clk_get_rate(dwsmmio->clk);
+	dev_dbg(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_deasserted(dev, NULL);
+	if (IS_ERR(dwsmmio->rstc))
+		return PTR_ERR(dwsmmio->rstc);
+
+	device_property_read_u32(dev, "num-cs", &num_cs);
+	if (num_cs < 1 || num_cs > DW_SPI_G2_MAX_CS)
+		return dev_err_probe(dev, -EINVAL, "invalid num-cs %u (1..%u)\n",
+				     num_cs, DW_SPI_G2_MAX_CS);
+	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->dev.of_node = pdev->dev.of_node;
+
+	spi_controller_set_devdata(ctlr, dws);
+
+	/* core init */
+	ret = dw_spi_g2_init(dws);
+	if (ret)
+		return ret;
+
+	/*
+	 * The IP has a combined interrupt output and per-source outputs;
+	 * the SoC may wire up either. Request every named line the DT
+	 * declares (rxf, txe, txaf, wsf) or fall back to a single
+	 * combined interrupt. The handler always decodes ISR centrally.
+	 */
+	{
+		static const char * const irq_names[] = {
+			"rxf", "txe", "txaf", "wsf",
+		};
+		bool any_named = false;
+
+		for (i = 0; i < ARRAY_SIZE(irq_names); i++) {
+			irq = platform_get_irq_byname_optional(pdev, irq_names[i]);
+			if (irq < 0)
+				continue;
+
+			ret = devm_request_irq(dev, irq, dw_spi_g2_irq, 0,
+					       dev_name(dev), dws);
+			if (ret)
+				return dev_err_probe(dev, ret,
+						     "failed to request IRQ %s\n",
+						     irq_names[i]);
+			any_named = true;
+		}
+
+		if (!any_named) {
+			irq = platform_get_irq(pdev, 0);
+			if (irq < 0)
+				return dev_err_probe(dev, irq,
+						     "no interrupt specified\n");
+
+			ret = devm_request_irq(dev, irq, dw_spi_g2_irq, 0,
+					       dev_name(dev), dws);
+			if (ret)
+				return dev_err_probe(dev, ret,
+						     "failed to request IRQ 0\n");
+		}
+	}
+
+	return devm_spi_register_controller(dev, ctlr);
+}
+
+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,
+	.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..ab1319ec0564
--- /dev/null
+++ b/drivers/spi/spi-dw-g2.h
@@ -0,0 +1,388 @@
+/* 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>
+
+/* 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 */
+#define DW_SPI_G2_ISR_FIFO_ERR		(DW_SPI_G2_ISR_TXOI | DW_SPI_G2_ISR_RXUI | \
+					 DW_SPI_G2_ISR_RXOI | DW_SPI_G2_ISR_TXUI)
+#define DW_SPI_G2_IMR_XFER		(DW_SPI_G2_ISR_TXEI | DW_SPI_G2_ISR_RXFI)
+#define DW_SPI_G2_IMR_XFER_ERR		(DW_SPI_G2_IMR_XFER | DW_SPI_G2_ISR_FIFO_ERR)
+
+/* 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
+
+/*
+ * 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) */
+
+/* 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 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
+
+/* Slack added to the bus-time based FIFO transfer timeout */
+#define DW_SPI_G2_XFER_SLACK_US	10000U
+
+/* 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 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).
+ * 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 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 {
+	struct device          *dev;
+	struct spi_controller  *ctlr;
+
+	void __iomem           *regs;
+
+	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;
+
+	struct spi_device       *curr_spi;
+
+	/* Per-CS RX sample delay (in SSI clock cycles) */
+	u8			rx_sample_dly[DW_SPI_G2_MAX_CS];
+	u8			cur_rx_sample_dly[DW_SPI_G2_MAX_CS];
+	u32			def_rx_sample_dly_ns;
+
+	/* IRQ-driven transfer_one() state (spi-mem stays on polled FIFO) */
+	const u8		*irq_tx;
+	u8			*irq_rx;
+	u32			irq_len;
+	u32			irq_tx_rem;
+	u32			irq_rx_rem;
+	bool			irq_xfer;
+};
+
+/* 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);
+}
+
+static inline void dw_spi_g2_mask_intr(struct dw_spi_g2 *dws, u32 mask)
+{
+	u32 new_mask = dw_spi_g2_readl(dws->regs, DW_SPI_G2_IMR) & ~mask;
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, new_mask);
+}
+
+static inline void dw_spi_g2_umask_intr(struct dw_spi_g2 *dws, u32 mask)
+{
+	u32 new_mask = dw_spi_g2_readl(dws->regs, DW_SPI_G2_IMR) | mask;
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, new_mask);
+}
+
+/* APIs */
+int dw_spi_g2_setup(struct spi_device *spi);
+
+int dw_spi_g2_init(struct dw_spi_g2 *dws);
+
+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);
+
+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)
+{
+	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: 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] 11+ messages in thread

* [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers
  2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
  2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
  2026-11-30  2:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
@ 2026-11-30  2:39 ` Prakriti Sharma
  2026-11-30  3:03   ` sashiko-bot
  2026-11-30  2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
  2026-11-30  2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP Prakriti Sharma
  4 siblings, 1 reply; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

Enable the FIFO indirect path to handle spi-mem data transfers for the
DesignWare SPI G2 controller. This allows the driver to support bulk
memory operations required by flash devices using the spi-mem interface.

Previously, only register-based operations were supported, which limited
compatibility with spi-mem users that require direct data reads or writes.
With this change, read and write operations using the controller's FIFO
indirect path are properly handled, improving compatibility and performance
for memory operations.

Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
 drivers/spi/spi-dw-g2-core.c | 389 ++++++++++++++++++++++++++++++++++-
 drivers/spi/spi-dw-g2.h      |   8 +
 2 files changed, 396 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index b2a3e3e8670e..88766706ef52 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -119,6 +119,43 @@ int dw_spi_g2_setup(struct spi_device *spi)
 	return 0;
 }
 
+static void dw_spi_g2_apply_sampling_fields(u32 *ctrlr0, u32 *spictrlr0,
+					    enum dw_spi_g2_sampling_mode mode)
+{
+	switch (mode) {
+	case DW_SPI_G2_SAMPLING_RX_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:
+		*ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN;
+		*spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+		break;
+	}
+}
+
+/**
+ * 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)
+{
+	u32 ctrlr0, spictrlr0;
+
+	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));
+	dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0, mode);
+
+	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);
+}
+
 /**
  * dw_spi_g2_build_ctrlr0 - Build CTRLR0 register value
  * @dws: Driver state
@@ -199,6 +236,63 @@ static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
 	}
 }
 
+/**
+ * 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
+ *
+ * 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
+ */
+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);
+
+	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_op_is_std_spi - True if @op is single-line SDR SPI
  * @op: SPI memory operation
@@ -664,6 +758,294 @@ void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
 			 dw_spi_g2_baudr(dws, freq));
 }
 
+/**
+ * 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)) & 0xFFFF;
+	if (!baud)
+		baud = 1;
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs),
+			 (baud <= 2) ? 1 : 0);
+}
+
+/**
+ * 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;
+		/*
+		 * 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);
+			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);
+
+	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;
+
+	/*
+	 * 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;
+
+	if (op->data.dtr)
+		dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+						DW_SPI_G2_SAMPLING_DATA_STROBE);
+	else
+		dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+						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),
+			 op->data.nbytes - 1);
+	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,
+			       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 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)
+{
+	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;
+	}
+
+	/*
+	 * One CS assertion per op. Larger reads are clamped in
+	 * adjust_mem_op_size() to fifo_len so spi-mem / spinand advance
+	 * the address themselves (required for SPI-NAND continuous read).
+	 */
+	if (op->data.nbytes > dws->fifo_len) {
+		dev_err(dws->dev,
+			"indirect_read: len %u exceeds FIFO %u (adjust_op_size bug?)\n",
+			op->data.nbytes, dws->fifo_len);
+		return -EINVAL;
+	}
+
+	dev_dbg(dws->dev, "indirect_read: cmd=0x%02x addr=0x%llx len=%u\n",
+		op->cmd.opcode, op->addr.val, op->data.nbytes);
+
+	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);
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
+	if (op->addr.nbytes)
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
+
+	dw_spi_g2_cs_assert(dws, dws->cs);
+
+	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;
+}
+
+/**
+ * dw_spi_g2_indirect_write_setup - Configure controller for indirect write
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * 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;
+
+	/*
+	 * For writes, sampling mode is less critical but enforce
+	 * mutual exclusivity for consistency with read path.
+	 */
+	if (op->data.dtr)
+		dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+						DW_SPI_G2_SAMPLING_DATA_STROBE);
+	else
+		dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+						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);
+}
+
+/**
+ * dw_spi_g2_indirect_write - Execute indirect write operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Writes data to flash using TMOD=TO (transmit-only).
+ * CS is asserted before SSI enable so Program Load sees data as soon as
+ * the shifter starts. Enabling SSI with an empty FIFO first can clock out
+ * a spurious 0x00 before the payload is queued.
+ *
+ * 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;
+	int ret = 0;
+
+	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;
+	}
+
+	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);
+
+	tx_buf = kzalloc(tx_total, GFP_KERNEL);
+	if (!tx_buf)
+		return -ENOMEM;
+
+	dw_spi_g2_fill_tx_buf(tx_buf, op, true);
+
+	dw_spi_g2_target_enable(dws, dws->cs);
+	dw_spi_g2_cs_assert(dws, dws->cs);
+	dw_spi_g2_enable(dws, true);
+
+	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;
+	}
+
+	dw_spi_g2_transfer_end(dws);
+	dw_spi_g2_controller_reset(dws);
+
+	kfree(tx_buf);
+	return ret;
+}
+
 static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
 				       const struct spi_mem_op *op)
 {
@@ -720,7 +1102,12 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
 	if (dw_spi_g2_is_reg_op(dws, op))
 		return dw_spi_g2_reg_op(dws, op);
 
-	/* Bulk transfers arrive in the indirect-read/write patch. */
+	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;
 }
 
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
index ab1319ec0564..4063fe2bbc30 100644
--- a/drivers/spi/spi-dw-g2.h
+++ b/drivers/spi/spi-dw-g2.h
@@ -97,6 +97,10 @@
 #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)
@@ -385,4 +389,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] 11+ messages in thread

* [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices
  2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
                   ` (2 preceding siblings ...)
  2026-11-30  2:39 ` [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers Prakriti Sharma
@ 2026-11-30  2:39 ` Prakriti Sharma
  2026-11-30  2:59   ` sashiko-bot
  2026-11-30  2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP Prakriti Sharma
  4 siblings, 1 reply; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

Enable SPI-NAND aware sizing and operation routing in the DW-G2 SPI driver.
SPI-NAND devices require precise chunk sizing for continuous reads without
hidden chunking, and operation routing to handle device-specific timing.
These changes allow proper operation with SPI-NAND while preserving
compatibility with other SPI memory devices.

Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
 drivers/spi/spi-dw-g2-core.c | 66 +++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index 88766706ef52..f50dcfe2d854 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -339,6 +339,12 @@ static inline bool dw_spi_g2_is_reg_op(struct dw_spi_g2 *dws,
 	       op->data.nbytes > 0 && total <= limit;
 }
 
+static bool dw_spi_g2_spi_is_nand(struct spi_device *spi)
+{
+	return spi && spi->dev.of_node &&
+	       of_device_is_compatible(spi->dev.of_node, "spi-nand");
+}
+
 /**
  * dw_spi_g2_xfer_timeout_us - Timeout for clocking a number of bytes
  * @dws: Driver state
@@ -675,6 +681,15 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
 
 	dw_spi_g2_cs_assert(dws, dws->cs);
 
+	/*
+	 * SPI-NAND: short settle after CS before ID/feature reads.
+	 * Check the current device (not a sticky controller flag).
+	 */
+	if (dw_spi_g2_spi_is_nand(dws->curr_spi)) {
+		(void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
+		usleep_range(10, 20);
+	}
+
 	if (op->data.dir == SPI_MEM_DATA_IN) {
 		/* TMOD=TR returns one byte per byte sent */
 		ret = dw_spi_g2_xfer_fifo(dws, NULL, 0, buf, len);
@@ -1049,13 +1064,62 @@ 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)
 {
+	bool op_is_dtr =
+		op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
+
+	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;
+		if (op->dummy.nbytes && op->dummy.dtr != op->cmd.dtr)
+			return false;
+	}
+
+	/*
+	 * cmd_only / indirect_write stay FRF_STANDARD. Reject multi-wire
+	 * or DTR data-out and command-only so spi-mem falls back to 1-1-1.
+	 * Multi-wire reads use the enhanced RO path when get_tt() works.
+	 */
+	if (op->data.dir == SPI_MEM_DATA_OUT ||
+	    op->data.dir == SPI_MEM_NO_DATA) {
+		if (!dw_spi_g2_op_is_std_spi(op))
+			return false;
+	} else if (!dw_spi_g2_op_is_std_spi(op) &&
+		   dw_spi_g2_get_tt(op) < 0) {
+		return false;
+	}
+
 	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)
 {
+	struct dw_spi_g2 *dws = spi_controller_get_devdata(mem->spi->controller);
+	u32 limit = DW_G2_MAX_CHUNK;
+
+	/*
+	 * Reads must fit one CS assertion (RX FIFO depth). Hidden
+	 * driver-side chunking would toggle CS and break SPI-NAND
+	 * continuous reads; let spi-mem / spinand advance the address.
+	 */
+	if (op->data.dir == SPI_MEM_DATA_IN && dws->fifo_len)
+		limit = min(limit, dws->fifo_len);
+
 	if (op->data.nbytes)
-		op->data.nbytes = min_t(u32, op->data.nbytes, DW_G2_MAX_CHUNK);
+		op->data.nbytes = min_t(u32, op->data.nbytes, limit);
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP
  2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
                   ` (3 preceding siblings ...)
  2026-11-30  2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
@ 2026-11-30  2:39 ` Prakriti Sharma
  2026-11-30  3:09   ` sashiko-bot
  4 siblings, 1 reply; 11+ messages in thread
From: Prakriti Sharma @ 2026-11-30  2:39 UTC (permalink / raw)
  To: Mark Brown, linux-spi
  Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, devicetree,
	Philipp Zabel, linux-kernel, Siddhant Kumar, Jose Abreu,
	Nelson Costa, Prakriti Sharma

Enable memory-mapped eXecute-In-Place (XIP) support through spi-mem direct
mapping and allow control of XIP features via device tree and sysfs. This
allows direct and more efficient access to SPI flash memory for systems
that require fast instruction or data fetches from external flash.

Add controls for enabling XIP mode and advanced features like continuous
and prefetch modes to better support a wider range of use-cases and improve
performance for compatible flashes.

Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
 drivers/spi/spi-dw-g2-core.c | 854 +++++++++++++++++++++++++++++++++--
 drivers/spi/spi-dw-g2-mmio.c | 235 ++++++++++
 drivers/spi/spi-dw-g2.h      |  93 ++++
 3 files changed, 1144 insertions(+), 38 deletions(-)

diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index f50dcfe2d854..972690cf47b9 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -156,6 +156,18 @@ void dw_spi_g2_set_sampling_mode(struct dw_spi_g2 *dws, u8 cs,
 	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(cs), spictrlr0);
 }
 
+static void dw_spi_g2_clear_xip_engine(struct dw_spi_g2 *dws)
+{
+	u32 cs = dws->cs;
+
+	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);
+	if (cs < DW_SPI_G2_MAX_CS) {
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CTRL(cs), 0);
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WRITE_CTRL(cs), 0);
+	}
+}
+
 /**
  * dw_spi_g2_build_ctrlr0 - Build CTRLR0 register value
  * @dws: Driver state
@@ -219,6 +231,7 @@ static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
 	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);
 	dws->irq_xfer = false;
+	dw_spi_g2_clear_xip_engine(dws);
 
 	/* Drain any stale RX data left from previous transfers */
 	while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
@@ -236,6 +249,24 @@ static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
 	}
 }
 
+static int dw_spi_g2_dirmap_check_bounds(struct spi_mem_dirmap_desc *desc,
+					 struct dw_spi_g2 *dws, u64 offs,
+					 size_t len)
+{
+	u64 base = desc->info.offset;
+
+	if (base >= dws->xip_len)
+		return -EINVAL;
+	if (desc->info.length > dws->xip_len - base)
+		return -EINVAL;
+	if (offs >= desc->info.length || len > desc->info.length - offs)
+		return -EINVAL;
+	if (dws->use_xip && base + offs > dws->xip_len - len)
+		return -EINVAL;
+
+	return 0;
+}
+
 /**
  * dw_spi_g2_get_frf - Determine frame format from operation bus widths
  * @op: SPI memory operation
@@ -535,6 +566,19 @@ static u32 dw_spi_g2_fill_tx_buf(u8 *buf, const struct spi_mem_op *op,
 	return pos;
 }
 
+/*
+ * spi-mem operations are issued as a single FIFO stream: the native chip
+ * select is de-asserted by the controller as soon as the TX FIFO drains,
+ * so the opcode, address, dummy and data bytes cannot be sent as separate
+ * spi_transfers. Each path below queues the whole stream (or as much as
+ * the FIFO holds) with SER cleared, then sets SER to start the transfer.
+ */
+
+/*
+ * 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
@@ -801,6 +845,10 @@ static void dw_spi_g2_update_ddr_drive_edge(struct dw_spi_g2 *dws,
 			 (baud <= 2) ? 1 : 0);
 }
 
+/*
+ * Indirect read operations
+ */
+
 /**
  * dw_spi_g2_indirect_read_setup - Configure controller for indirect read
  * @dws: Driver state
@@ -955,6 +1003,11 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *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
@@ -1061,6 +1114,634 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
 	return ret;
 }
 
+/*
+ * XIP (direct mapping) support
+ */
+
+static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
+				       const struct spi_mem_op *op);
+
+/**
+ * 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 are enabled via device tree when supported.
+		 */
+		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;
+	}
+
+	dws->cs = spi_get_chipselect(spi, 0);
+	dws->curr_spi = spi;
+
+	if (dws->cs >= DW_SPI_G2_MAX_CS)
+		return -EINVAL;
+
+	if (!dw_spi_g2_supports_mem_ops(desc->mem, op))
+		return -EOPNOTSUPP;
+
+	if (desc->info.offset >= dws->xip_len ||
+	    desc->info.length > dws->xip_len - desc->info.offset)
+		return -EINVAL;
+
+	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);
+
+	/*
+	 * 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);
+	u8 __iomem *src;
+	u32 ctrlr0;
+	int ret;
+
+	dws->cs = spi_get_chipselect(spi, 0);
+	dws->curr_spi = spi;
+
+	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
+		return -EINVAL;
+
+	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;
+
+		/*
+		 * Fold dummy bytes into the address field when RO mode is used
+		 * without the XIP engine. Plain RO with WAIT_CYCLES > 0 can
+		 * leave the RX FIFO empty; XIP uses WAIT_CYCLES normally.
+		 */
+		if (op.dummy.nbytes) {
+			if (op.addr.nbytes + op.dummy.nbytes > 4)
+				return -EOPNOTSUPP;
+			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)(desc->info.offset + 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;
+
+	/*
+	 * 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);
+	ret = dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, false);
+	if (ret)
+		return ret;
+
+	src = (u8 __iomem *)dws->xip_mem + desc->info.offset + 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 mapped region (relative to info.offset)
+ * @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.
+ */
+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;
+	u8 __iomem *dst;
+	ssize_t result;
+	u32 ctrlr0;
+	u8 sr1;
+	size_t i;
+	int ret;
+	u64 flash_offs = desc->info.offset + offs;
+
+	if (!dws->xip_mem)
+		return -EOPNOTSUPP;
+
+	dst = (u8 __iomem *)dws->xip_mem + flash_offs;
+
+	dev_dbg(dws->dev,
+		"dirmap_write_nor_xip: BEGIN offs=0x%llx len=%zu (byte-by-byte WREN/WIP)\n",
+		(unsigned long long)flash_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;
+
+		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);
+		ret = dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
+		if (ret) {
+			result = ret;
+			goto out_unwind;
+		}
+
+		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);
+
+		ret = dw_spi_g2_wait_idle(dws);
+		if (ret) {
+			result = ret;
+			goto out_unwind;
+		}
+
+		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);
+
+		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);
+	}
+
+	return (ssize_t)len;
+
+out_unwind:
+	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.
+ *   - 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.
+ *   - NOR Page Program (addr_nbytes >= 3) with XIP disabled: indirect
+ *     FIFO path (spi-nor issues WREN once per dirmap call).
+ *
+ * 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);
+	u8 __iomem *dst;
+	u32 ctrlr0;
+	bool use_indirect = false;
+	int ret;
+
+	dws->cs = spi_get_chipselect(spi, 0);
+	dws->curr_spi = spi;
+
+	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
+		return -EINVAL;
+
+	if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
+		use_indirect = true;
+
+	if (use_indirect && dws->use_xip && dws->xip_mem &&
+	    desc->info.op_tmpl->addr.nbytes >= 3) {
+		dev_dbg(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;
+
+		dev_dbg(dws->dev,
+			"dirmap_write: ROUTE=INDIRECT-FIFO cmd=0x%02x offs=0x%llx len=%zu\n",
+			op.cmd.opcode, (unsigned long long)offs, len);
+
+		op.addr.val = desc->info.offset + 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;
+
+	dev_dbg(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);
+	ret = dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
+	if (ret)
+		return ret;
+
+	dst = (u8 __iomem *)dws->xip_mem + desc->info.offset + 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;
+}
+
 static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
 				       const struct spi_mem_op *op)
 {
@@ -1105,6 +1786,22 @@ 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)
 {
 	struct dw_spi_g2 *dws = spi_controller_get_devdata(mem->spi->controller);
@@ -1130,17 +1827,20 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
 	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",
@@ -1148,15 +1848,44 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
 		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;
+		/*
+		 * 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);
+			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_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) {
 		if (!dw_spi_g2_op_is_std_spi(op))
 			return -EOPNOTSUPP;
@@ -1179,8 +1908,18 @@ 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,
 };
 
+/*
+ * FIFO depth is a synthesis-time parameter not exposed by any
+ * discoverable register and varies per instantiation. 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 SSIENR=0.
+ */
 static u32 dw_spi_g2_detect_fifo_len(struct dw_spi_g2 *dws)
 {
 	u32 fifo;
@@ -1274,6 +2013,83 @@ static int dw_spi_g2_transfer_one(struct spi_controller *ctlr,
 	return 1;
 }
 
+/**
+ * dw_spi_g2_hw_init - Put the controller hardware in a known state
+ * @dws: Driver state
+ *
+ * Disables the SSI, clears chip selects and dedicated CS enables, masks
+ * and clears interrupts, and sets the DDR drive edge for every CS. Per-CS
+ * transfer registers are programmed by each operation.
+ */
+static void 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);
+		dws->cur_rx_sample_dly[cs] = U8_MAX;
+	}
+}
+
+/**
+ * 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 (enabled per transfer_one; mem ops poll)
+ *   - Sets FIFO depth and mode bits
+ *   - Registers the transfer_one and spi-mem operations
+ *
+ * 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;
+
+	dw_spi_g2_hw_init(dws);
+
+	/* 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->set_cs = dw_spi_g2_set_cs;
+	ctlr->transfer_one = dw_spi_g2_transfer_one;
+	ctlr->mem_ops = &dw_spi_g2_mem_ops;
+	ctlr->mem_caps = &dw_spi_g2_mem_caps;
+
+	dev_dbg(dws->dev, "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_irq - SPI controller interrupt handler
  * @irq: IRQ number
@@ -1334,43 +2150,5 @@ irqreturn_t dw_spi_g2_irq(int irq, void *dev_id)
 }
 EXPORT_SYMBOL_NS_GPL(dw_spi_g2_irq, "SPI_DW_G2_CORE");
 
-int dw_spi_g2_init(struct dw_spi_g2 *dws)
-{
-	struct spi_controller *ctlr = dws->ctlr;
-	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);
-
-	for (cs = 0; cs < DW_SPI_G2_MAX_CS; cs++)
-		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(cs), 1);
-
-	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;
-
-	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->set_cs = dw_spi_g2_set_cs;
-	ctlr->transfer_one = dw_spi_g2_transfer_one;
-	ctlr->mem_ops = &dw_spi_g2_mem_ops;
-
-	dev_dbg(dws->dev, "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");
-
 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
index 1be68c4250e6..1f776d42ef8a 100644
--- a/drivers/spi/spi-dw-g2-mmio.c
+++ b/drivers/spi/spi-dw-g2-mmio.c
@@ -18,6 +18,196 @@ struct dw_spi_g2_mmio {
 	struct reset_control	*rstc;
 };
 
+static int dw_spi_g2_xip_mode_set(struct dw_spi_g2 *dws, int val)
+{
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	if (val && !dws->xip_mem) {
+		dev_err(dws->dev,
+			"Cannot enable XIP: no memory-mapped region available\n");
+		return -EINVAL;
+	}
+
+	dws->use_xip = !!val;
+	return 0;
+}
+
+static int dw_spi_g2_xip_continuous_mode_set(struct dw_spi_g2 *dws, int val)
+{
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	if (val && !dws->xip_mem) {
+		dev_err(dws->dev, "Cannot enable continuous mode: no XIP region\n");
+		return -EINVAL;
+	}
+
+	if (val && !dws->use_xip) {
+		dev_err(dws->dev, "Cannot enable continuous mode: XIP not active\n");
+		return -EINVAL;
+	}
+
+	if (val) {
+		if (!device_property_read_bool(dws->dev, "snps,xip-continuous-mode")) {
+			dev_err(dws->dev,
+				"Continuous mode requires snps,xip-continuous-mode in DT and flash that supports continuous read (e.g. 0xEB)\n");
+			return -EINVAL;
+		}
+		dws->xip_continuous_mode = true;
+	} else {
+		dws->xip_continuous_mode = false;
+	}
+
+	return 0;
+}
+
+static int dw_spi_g2_xip_prefetch_mode_set(struct dw_spi_g2 *dws, int val)
+{
+	if (val != 0 && val != 1)
+		return -EINVAL;
+
+	if (val && !dws->xip_mem) {
+		dev_err(dws->dev, "Cannot enable prefetch: no XIP region\n");
+		return -EINVAL;
+	}
+
+	if (val && !dws->use_xip) {
+		dev_err(dws->dev, "Cannot enable prefetch: XIP not active\n");
+		return -EINVAL;
+	}
+
+	if (val) {
+		if (!device_property_read_bool(dws->dev, "snps,xip-prefetch-mode")) {
+			dev_err(dws->dev,
+				"Prefetch requires snps,xip-prefetch-mode in DT and flash that supports continuous read (e.g. 0xEB)\n");
+			return -EINVAL;
+		}
+		dws->xip_prefetch_mode = true;
+	} else {
+		dws->xip_prefetch_mode = false;
+	}
+
+	return 0;
+}
+
+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);
+
+	if (dws->use_xip)
+		return sysfs_emit(buf, "1\n");
+
+	return sysfs_emit(buf, "0\n");
+}
+
+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 (dw_spi_g2_xip_mode_set(dws, val))
+		return -EINVAL;
+
+	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);
+
+	if (dws->xip_continuous_mode)
+		return sysfs_emit(buf, "1\n");
+
+	return sysfs_emit(buf, "0\n");
+}
+
+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 (dw_spi_g2_xip_continuous_mode_set(dws, val))
+		return -EINVAL;
+
+	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);
+
+	if (dws->xip_prefetch_mode)
+		return sysfs_emit(buf, "1\n");
+
+	return sysfs_emit(buf, "0\n");
+}
+
+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 (dw_spi_g2_xip_prefetch_mode_set(dws, val))
+		return -EINVAL;
+
+	return count;
+}
+
+static DEVICE_ATTR_RW(xip_prefetch_mode);
+
+static umode_t dw_spi_g2_attr_is_visible(struct kobject *kobj,
+					 struct attribute *attr, int n)
+{
+	struct spi_controller *ctlr = dev_get_drvdata(kobj_to_dev(kobj));
+	struct dw_spi_g2 *dws = spi_controller_get_devdata(ctlr);
+
+	return dws->xip_mem ? attr->mode : 0;
+}
+
+static struct attribute *dw_spi_g2_attrs[] = {
+	&dev_attr_xip_mode.attr,
+	&dev_attr_xip_continuous_mode.attr,
+	&dev_attr_xip_prefetch_mode.attr,
+	NULL
+};
+
+static const struct attribute_group dw_spi_g2_group = {
+	.attrs = dw_spi_g2_attrs,
+	.is_visible = dw_spi_g2_attr_is_visible,
+};
+__ATTRIBUTE_GROUPS(dw_spi_g2);
+
 static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -47,6 +237,24 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
 	if (IS_ERR(dws->regs))
 		return PTR_ERR(dws->regs);
 
+	/* XIP memory region for direct mapping */
+	mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	if (mem) {
+		dws->xip_mem = devm_ioremap_resource(dev, mem);
+		if (IS_ERR(dws->xip_mem)) {
+			dev_warn(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_dbg(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(dev, "XIP memory region not specified, direct mapping disabled\n");
+	}
+
 	/* clocks */
 	dwsmmio->clk = devm_clk_get_enabled(dev, NULL);
 	if (IS_ERR(dwsmmio->clk))
@@ -91,6 +299,32 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
 
 	dws->max_freq = clk_get_rate(dwsmmio->clk);
 
+	/*
+	 * XIP (direct mapping) is enabled when a second memory region is
+	 * present and snps,xip-enabled is set. It can also be toggled at
+	 * runtime via the xip_mode sysfs attribute.
+	 */
+	dws->use_xip = dws->xip_mem &&
+		       device_property_read_bool(&pdev->dev, "snps,xip-enabled");
+
+	device_property_read_u32(dev, "rx-sample-delay-ns",
+				 &dws->def_rx_sample_dly_ns);
+
+	dws->xip_continuous_mode = false;
+	dws->xip_prefetch_mode = false;
+
+	if (dws->xip_mem && dws->use_xip) {
+		dws->xip_continuous_mode =
+			device_property_read_bool(&pdev->dev,
+						  "snps,xip-continuous-mode");
+		dws->xip_prefetch_mode =
+			device_property_read_bool(&pdev->dev,
+						  "snps,xip-prefetch-mode");
+	}
+
+	dev_dbg(&pdev->dev, "transfer mode: %s\n",
+		dws->use_xip ? "XIP" : "indirect");
+
 	/* SPI Controller Setup */
 	ctlr->bus_num = of_alias_get_id(pdev->dev.of_node, "spi");
 	if (ctlr->bus_num < 0)
@@ -158,6 +392,7 @@ static struct platform_driver dw_spi_g2_mmio_driver = {
 	.driver = {
 		.name = "dw-spi-g2-mmio",
 		.of_match_table = dw_spi_g2_of_match,
+		.dev_groups = dw_spi_g2_groups,
 	},
 };
 
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
index 4063fe2bbc30..55e46a16b595 100644
--- a/drivers/spi/spi-dw-g2.h
+++ b/drivers/spi/spi-dw-g2.h
@@ -29,6 +29,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
@@ -261,6 +339,14 @@ struct dw_spi_g2 {
 
 	struct spi_device       *curr_spi;
 
+	/* 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[DW_SPI_G2_MAX_CS];
@@ -318,6 +404,13 @@ 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);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding
  2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
@ 2026-11-30  2:47   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  2:47 UTC (permalink / raw)
  To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 3 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: redundant word binding in
  subject
- [Low] dt-bindings: spi: snps,dwc-ssi-2.02a: interrupts ordered before
  clocks
--

commit e008bf8dcdd6fda311d76264fb0cb3d47975ceeb
Author: Siddhant Kumar <sidkumar@synopsys.com>

spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding

This commit adds the Device Tree binding schema for the Synopsys DesignWare
SSI Gen2 (DWC_ssi v2.02a) SPI controller and adds a corresponding entry in
MAINTAINERS.

> spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding

[Severity: Low]
The "dt-bindings:" prefix already indicates that this patch adds a binding,
so the trailing word "binding" in the subject line is redundant and can be
dropped.

> 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 0000000000000..7294c9b2694b4
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> @@ -0,0 +1,191 @@

[ ... ]

> +  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

[Severity: Low]
To follow the canonical DT schema property ordering (compatible, reg,
reg-names, clocks, clock-names, interrupts, interrupt-names), clocks and
clock-names should be placed before interrupts and interrupt-names here in
properties, as well as in the required list and the example node below.

[ ... ]

> +  snps,xip-enabled:
> +    type: boolean
> +    description: |
> +      Enable the controller XIP engine for memory-mapped flash access through
> +      the second reg window (xip_mem). Requires that window to be described.
> +
> +  snps,xip-continuous-mode:
> +    type: boolean
> +    description: |
> +      Set XIP_CTRL continuous-transfer mode so the controller omits the
> +      command byte on subsequent sequential reads while CS stays asserted.
> +      The attached flash must support continuous read (for example Fast Read
> +      Quad I/O with mode bits). Requires snps,xip-enabled.
> +
> +  snps,xip-prefetch-mode:
> +    type: boolean
> +    description: |
> +      Set XIP_CTRL hardware prefetch so the controller may fetch the next
> +      line after a completed XIP access. Requires snps,xip-enabled.

[Severity: Medium]
Do these three properties encode driver runtime policy rather than hardware
characteristics?

Whether the SoC wires an XIP memory-mapped window is already described by
the presence of the xip_mem reg entry, making snps,xip-enabled a software
switch for whether the driver uses direct mapping over indirect FIFO
transfers.  Similarly, snps,xip-continuous-mode depends on the attached
flash device's protocol capabilities, and snps,xip-prefetch-mode is a
workload-dependent tuning knob rather than a fixed property of the
controller integration.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795683279.git.prakriti@synopsys.com?part=1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices
  2026-11-30  2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
@ 2026-11-30  2:59   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  2:59 UTC (permalink / raw)
  To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] spi: dw-g2: dw_spi_g2_adjust_mem_op_size() misroutes 1-1-1
  reads to RO mode
- [Medium] spi: dw-g2: clamping reads to fifo_len breaks SPI-NAND
  continuous reads
- [Medium] spi: dw-g2: misplaced SPI-NAND delay after CS assert in
  dw_spi_g2_reg_op()
- [Medium] spi: dw-g2: broken op validation in
  dw_spi_g2_supports_mem_ops()
--

commit 693b6c9d55f285455a0b9914fa2d4d0ffd449b31
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add support for SPI-NAND devices

This commit adds a post-CS delay for SPI-NAND register operations, DTR and
transfer-type checks in dw_spi_g2_supports_mem_ops(), and RX FIFO read size
clamping in dw_spi_g2_adjust_mem_op_size().

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index 88766706ef525..f50dcfe2d854c 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
> @@ -339,6 +339,12 @@ static inline bool dw_spi_g2_is_reg_op(struct dw_spi_g2 *dws,
>  	       op->data.nbytes > 0 && total <= limit;
>  }
>  
> +static bool dw_spi_g2_spi_is_nand(struct spi_device *spi)
> +{
> +	return spi && spi->dev.of_node &&
> +	       of_device_is_compatible(spi->dev.of_node, "spi-nand");
> +}
> +
>  /**
>   * dw_spi_g2_xfer_timeout_us - Timeout for clocking a number of bytes
>   * @dws: Driver state
> @@ -675,6 +681,15 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
>  
>  	dw_spi_g2_cs_assert(dws, dws->cs);
>  
> +	/*
> +	 * SPI-NAND: short settle after CS before ID/feature reads.
> +	 * Check the current device (not a sticky controller flag).
> +	 */
> +	if (dw_spi_g2_spi_is_nand(dws->curr_spi)) {
> +		(void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> +		usleep_range(10, 20);
> +	}

[Severity: Medium]
Does usleep_range() here actually delay SCLK after CS assertion on the bus?

Before dw_spi_g2_cs_assert() writes BIT(cs) to DW_SPI_G2_SER, the controller
is already enabled and all len bytes of the transfer have been pre-filled into
DW_SPI_G2_DR0:

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_reg_op() {
	...
	dw_spi_g2_target_enable(dws, dws->cs);
	dw_spi_g2_enable(dws, true);

	/* dw_spi_g2_is_reg_op() guarantees the whole stream fits the FIFO */
	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);
	...
}

In DWC_ssi hardware, writing SER while SSIENR is set and the TX FIFO is
non-empty starts shifting out the queued frames immediately and deasserts CS
as soon as the TX FIFO drains.  Sleeping after writing SER does not insert any
CS-to-SCLK setup time on the wire, and instead adds a 10-20 us CPU stall to
every SPI-NAND register operation, including every status poll iteration in
spi_mem_poll_status() and DATA_OUT feature writes.

> @@ -1049,13 +1064,62 @@ 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)
>  {
> +	bool op_is_dtr =
> +		op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
> +
> +	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;
> +		if (op->dummy.nbytes && op->dummy.dtr != op->cmd.dtr)
> +			return false;
> +	}
> +
> +	/*
> +	 * cmd_only / indirect_write stay FRF_STANDARD. Reject multi-wire
> +	 * or DTR data-out and command-only so spi-mem falls back to 1-1-1.
> +	 * Multi-wire reads use the enhanced RO path when get_tt() works.
> +	 */
> +	if (op->data.dir == SPI_MEM_DATA_OUT ||
> +	    op->data.dir == SPI_MEM_NO_DATA) {
> +		if (!dw_spi_g2_op_is_std_spi(op))
> +			return false;
> +	} else if (!dw_spi_g2_op_is_std_spi(op) &&
> +		   dw_spi_g2_get_tt(op) < 0) {
> +		return false;
> +	}
> +
>  	return spi_mem_default_supports_op(mem, op);
>  }

[Severity: Medium]
Does dw_spi_g2_supports_mem_ops() match the operations that the controller and
dw_spi_g2_indirect_read() actually support?

There are three mismatches in this validation:

1. In this commit, ctlr->mem_caps is not set until the later commit
"spi: dw-g2: Add support for memory-mapped XIP" in this series, so
spi_mem_default_supports_op() rejects all DTR operations.  Even once mem_caps
is wired up, requiring addr.dtr, dummy.dtr, and data.dtr to equal cmd.dtr
rejects all 1S-xD-xD mixed STR/DTR operations (even though
dw_spi_g2_indirect_read_setup() programs INST_DDR_EN and DDR_EN
independently), while checking only op->data.buswidth == 1 fails to reject
1-wire DTR command or address phases.

2. Both dw_spi_g2_supports_mem_ops() and dw_spi_g2_op_is_std_spi() guard data
phase checks with op->data.nbytes instead of op->data.dir != SPI_MEM_NO_DATA.
When spi_mem_dirmap_create() checks a template with op->data.nbytes == 0 (such
as a 1S-1S-4S page read or program load template), dw_spi_g2_op_is_std_spi()
returns true, misclassifying multi-wire templates as standard 1-1-1 SPI and
accepting unsupported multi-wire SPI_MEM_DATA_OUT dirmap templates.

3. For enhanced reads, checking only dw_spi_g2_get_tt(op) < 0 does not verify
that op->data.buswidth > 1 and matches any multi-wire command or address
phase (since CTRLR0.FRF selects a single enhanced buswidth), that dummy cycles
fit within DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX (31) before
dw_spi_g2_indirect_read_setup() fails at runtime with -EINVAL, or that
op->addr.nbytes <= 4 for the 32-bit address write to DW_SPI_G2_DR0.

>  int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
>  {
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(mem->spi->controller);
> +	u32 limit = DW_G2_MAX_CHUNK;
> +
> +	/*
> +	 * Reads must fit one CS assertion (RX FIFO depth). Hidden
> +	 * driver-side chunking would toggle CS and break SPI-NAND
> +	 * continuous reads; let spi-mem / spinand advance the address.
> +	 */
> +	if (op->data.dir == SPI_MEM_DATA_IN && dws->fifo_len)
> +		limit = min(limit, dws->fifo_len);

[Severity: Medium]
Doesn't clamping SPI_MEM_DATA_IN reads to dws->fifo_len (one CS assertion per
256-byte FIFO) still break SPI-NAND continuous reads?

A SPI-NAND continuous read requires reading across multiple pages under a
single uninterrupted CS assertion because deasserting CS terminates continuous
read mode on the flash chip.  When a read is clamped to dws->fifo_len and returns
after 256 bytes, spinand_read_from_cache_op() sees remaining bytes with
req->continuous set, treats the controller as broken, sets
spinand->cont_read_possible = false, and returns -EAGAIN:

drivers/mtd/nand/spi/core.c:spinand_read_from_cache_op() {
	...
		if (nbytes && req->continuous) {
			spinand->cont_read_possible = false;
			return -EAGAIN;
		}
	...
}

If the controller must deassert CS after every dws->fifo_len bytes in FIFO
mode, should ctlr->mem_caps set .no_cs_assertion = true so SPI-NAND does not
attempt continuous reads in the first place?

> +
>  	if (op->data.nbytes)
> -		op->data.nbytes = min_t(u32, op->data.nbytes, DW_G2_MAX_CHUNK);
> +		op->data.nbytes = min_t(u32, op->data.nbytes, limit);

[Severity: High]
Does clamping op->data.nbytes to dws->fifo_len here misroute standard 1-1-1
reads into dw_spi_g2_indirect_read()?

For a 1-1-1 read larger than dws->fifo_len, dw_spi_g2_adjust_mem_op_size()
clamps op->data.nbytes to dws->fifo_len (for example, 256 bytes) without
subtracting op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes or bounding by
DW_SPI_G2_REG_OP_BUF_SIZE.  When dw_spi_g2_exec_mem_op() then checks
dw_spi_g2_is_reg_op():

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_is_reg_op() {
	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 dw_spi_g2_op_is_std_spi(op) &&
	       op->data.nbytes > 0 && total <= limit;
}

total is 256 + cmd + addr + dummy, which exceeds limit, so
dw_spi_g2_is_reg_op() returns false and dw_spi_g2_exec_mem_op() falls through
to dw_spi_g2_indirect_read().  However, dw_spi_g2_indirect_read_setup()
programs TMOD_RO with DW_SPI_G2_FRF_STANDARD for 1-1-1 operations; in standard
SPI mode the DWC_ssi hardware ignores SPI_CTRLR0 instruction/address/wait-cycle
framing, truncates the 32-bit address write to DW_SPI_G2_DR0 to 8 bits, and
clocks in receive bytes in TMOD_RO without transmitting the command, address,
or dummy phases on MOSI.

For standard 1-1-1 reads (dw_spi_g2_op_is_std_spi(op)), should limit in
dw_spi_g2_adjust_mem_op_size() be capped at
min(dws->fifo_len, DW_SPI_G2_REG_OP_BUF_SIZE) - (op->cmd.nbytes +
op->addr.nbytes + op->dummy.nbytes)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795683279.git.prakriti@synopsys.com?part=4

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller
  2026-11-30  2:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
@ 2026-11-30  3:01   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  3:01 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: premature transfer abort in dw_spi_g2_cmd_only()
  and wait_idle()
- [High] spi: dw-g2: missing ctlr->handle_err leaves IRQ transfer active
  on timeout
- [High] spi: dw-g2: unlocked concurrent execution of dw_spi_g2_irq()
  across IRQs
- [Medium] spi: dw-g2: missing SPI_CPOL and SPI_CPHA setup in
  dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: missing ctlr->max_speed_hz setup in
  dw_spi_g2_init()
- [Medium] spi: dw-g2: missing ctlr->max_speed_hz and clobbered caps in
  dw_spi_g2_init()
- [Low] spi: dw-g2: redundant TXEI interrupt on short transfers in
  dw_spi_g2_transfer_one()
--

commit 164e383f9dc370e3fcd8dcc242bc57e332d6ec75
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller

This commit introduces a platform driver for the Synopsys DesignWare SSI
Gen2 (v2.02a) MMIO SPI controller with interrupt-driven standard transfers
and spi-mem command-only and short register operations.

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> new file mode 100644
> index 0000000000000..b2a3e3e8670eb
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-core.c
> @@ -0,0 +1,925 @@
[ ... ]
> +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() also encode DW_SPI_G2_CTRLR0_SCPOL and
DW_SPI_G2_CTRLR0_SCPHA from dws->curr_spi->mode?

dw_spi_g2_transfer_one() ORs those bits in after calling
dw_spi_g2_build_ctrlr0(), but dw_spi_g2_cmd_only_setup() and
dw_spi_g2_reg_op_setup() write the return value directly to
DW_SPI_G2_CTRLR0(dws->cs).  Any spi-mem flash device configured for SPI
Mode 1, 2, or 3 will have its transactions clocked in Mode 0.

[ ... ]

> +static int dw_spi_g2_cmd_only(struct dw_spi_g2 *dws,
> +			      const struct spi_mem_op *op)
> +{
[ ... ]
> +	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);
> +	ret = dw_spi_g2_wait_idle(dws);
> +
> +	dw_spi_g2_transfer_end(dws);
> +	dw_spi_g2_controller_reset(dws);
> +
> +	return ret;
> +}

[Severity: High]
Can dw_spi_g2_wait_idle() return before the TX FIFO starts shifting out,
causing dw_spi_g2_transfer_end() to abort the command?

dw_spi_g2_wait_idle() only checks !(sr & DW_SPI_G2_SR_BUSY):

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);
}

As the comment in dw_spi_g2_reg_op() notes for the same prefill and CS
assertion pattern, polling only !DW_SPI_G2_SR_BUSY without
DW_SPI_G2_SR_TFE can return early before DW_SPI_G2_SER propagates into the
ssi_clk domain.  When that happens in dw_spi_g2_cmd_only() or
dw_spi_g2_xfer_fifo(), disabling SSIENR and deasserting CS flushes the
queued bytes from the TX FIFO and drops the SPI command.

[ ... ]

> +static int dw_spi_g2_transfer_one(struct spi_controller *ctlr,
> +				  struct spi_device *spi,
> +				  struct spi_transfer *xfer)
> +{
[ ... ]
> +	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_BAUDR(dws->cs),
> +			 dw_spi_g2_baudr(dws, xfer->speed_hz));

[Severity: Medium]
Can xfer->speed_hz be 0 here when a device relies on the controller's
default maximum speed?

Because dw_spi_g2_init() does not set ctlr->max_speed_hz = dws->max_freq,
the SPI core leaves spi->max_speed_hz and xfer->speed_hz at 0 when a
peripheral does not specify a maximum frequency (and does not clamp higher
speeds to dws->max_freq).  dw_spi_g2_baudr(dws, 0) then returns
DW_SPI_G2_MAX_BAUDR (65534), dropping the bus clock to its slowest
possible divider instead of dws->max_freq.  In addition,
dw_spi_g2_mmio_probe() calls of_alias_get_id(pdev->dev.of_node, "spi"),
which is undocumented in the DT binding and already handled by
spi_register_controller().

> +
> +	dw_spi_g2_target_enable(dws, dws->cs);
> +	dw_spi_g2_enable(dws, true);
> +
> +	/* Ensure irq_xfer is visible before unmasking TXEI/RXFI. */
> +	smp_wmb();
> +	dws->irq_xfer = true;
> +
> +	dw_spi_g2_irq_xfer_setup(dws);
> +	dw_spi_g2_irq_writer(dws);
> +	dw_spi_g2_umask_intr(dws, DW_SPI_G2_IMR_XFER_ERR);

[Severity: Low]
Does unmasking DW_SPI_G2_ISR_TXEI unconditionally after
dw_spi_g2_irq_writer() trigger an extra interrupt on every transfer that
fits in the TX FIFO?

When xfer->len <= dws->fifo_len, dw_spi_g2_irq_writer() pushes the entire
transfer into the TX FIFO and decrements dws->irq_tx_rem to 0 before
interrupts are unmasked.  Unmasking DW_SPI_G2_ISR_TXEI anyway causes a
TXEI interrupt to fire only for dw_spi_g2_irq() to see !dws->irq_tx_rem
and mask DW_SPI_G2_ISR_TXEI.

[ ... ]

> +int dw_spi_g2_init(struct dw_spi_g2 *dws)
> +{
[ ... ]
> +	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;

[Severity: Medium]
Does dw_spi_g2_init() clobber dws->caps and leave ctlr->max_speed_hz
unset?

dw_spi_g2_mmio_probe() reads DW_SPI_G2_HW_CAPABILITIES into
dws->caps = dws->hw_caps and programs DW_SPI_G2_DEDICATED_CS_EN right
before calling dw_spi_g2_init(), which zeroes both DW_SPI_G2_DEDICATED_CS_EN
and dws->caps.  Also, ctlr->max_speed_hz is never initialized from
dws->max_freq, so transfers without an explicit speed fall back to
dw_spi_g2_baudr(dws, 0) (DW_SPI_G2_MAX_BAUDR) in dw_spi_g2_transfer_one()
and override the baud rate programmed by dw_spi_g2_setup().

> +
> +	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->set_cs = dw_spi_g2_set_cs;
> +	ctlr->transfer_one = dw_spi_g2_transfer_one;
> +	ctlr->mem_ops = &dw_spi_g2_mem_ops;

[Severity: High]
What stops the in-flight IRQ transfer if spi_transfer_wait() times out?

Without a ctlr->handle_err callback to mask interrupts, clear
dws->irq_xfer, and disable the controller, a timed-out message is
finalized by the SPI core while DW_SPI_G2_IMR_XFER_ERR remains unmasked
and dws->irq_tx / dws->irq_rx still point to the caller's transfer
buffers:

CPU0                              CPU1
spi_transfer_one_message()
  dw_spi_g2_transfer_one()
    dws->irq_rx = xfer->rx_buf
    dws->irq_xfer = true
    dw_spi_g2_umask_intr(...)
  spi_transfer_wait() -> -ETIMEDOUT
  spi_finalize_current_message()
  /* caller frees xfer->rx_buf */
                                  <hardirq>
                                  dw_spi_g2_irq()
                                    dw_spi_g2_irq_reader()
                                      dws->irq_rx[idx] = val  <-- use-after-free

In addition, if a threshold interrupt fires in dw_spi_g2_irq() while
!dws->irq_xfer, returning IRQ_NONE without masking DW_SPI_G2_IMR leaves
the level-triggered interrupt asserted.

> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> new file mode 100644
> index 0000000000000..1be68c4250e67
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-mmio.c
> @@ -0,0 +1,168 @@
[ ... ]
> +		static const char * const irq_names[] = {
> +			"rxf", "txe", "txaf", "wsf",
> +		};
> +		bool any_named = false;
> +
> +		for (i = 0; i < ARRAY_SIZE(irq_names); i++) {
> +			irq = platform_get_irq_byname_optional(pdev, irq_names[i]);
> +			if (irq < 0)
> +				continue;
> +
> +			ret = devm_request_irq(dev, irq, dw_spi_g2_irq, 0,
> +					       dev_name(dev), dws);

[Severity: High]
When the platform wires separate IRQ lines for "rxf" and "txe", can
dw_spi_g2_irq() run concurrently on two CPUs without locking?

Distinct IRQ numbers are not serialized against each other, and
dw_spi_g2_transfer_one() unmasks both DW_SPI_G2_ISR_RXFI and
DW_SPI_G2_ISR_TXEI:

CPU0 (IRQ "rxf")                  CPU1 (IRQ "txe")
dw_spi_g2_irq()                   dw_spi_g2_irq()
  dw_spi_g2_irq_reader()            dw_spi_g2_irq_reader()
    max = dw_spi_g2_irq_rx_max()      max = dw_spi_g2_irq_rx_max()
    idx = irq_len - irq_rx_rem        idx = irq_len - irq_rx_rem
    readl(DW_SPI_G2_DR0)              readl(DW_SPI_G2_DR0)
    irq_rx[idx] = val                 irq_rx[idx] = val
    irq_rx_rem--                      irq_rx_rem--  <-- u32 underflow / OOB write

Both handlers also race on dw_spi_g2_irq_writer(), read-modify-write
updates to DW_SPI_G2_IMR, and dw_spi_g2_irq_finalize(), and if the first
CPU drains the FIFO before the second CPU reads DW_SPI_G2_ISR, the second
handler sees isr == 0 and returns IRQ_NONE.

Also, in dw_spi_g2_transfer_one(), smp_wmb() is placed before
dws->irq_xfer = true rather than between dws->irq_xfer = true and
dw_spi_g2_umask_intr(), and "if (irq < 0) continue;" here silently drops
-EPROBE_DEFER from platform_get_irq_byname_optional().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795683279.git.prakriti@synopsys.com?part=2

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers
  2026-11-30  2:39 ` [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers Prakriti Sharma
@ 2026-11-30  3:03   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  3:03 UTC (permalink / raw)
  To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] spi: dw-g2: 1-1-1 reads misconfigured with TMOD_RO in
  dw_spi_g2_indirect_read()
- [High] spi: dw-g2: premature TX completion 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: missing CPOL/CPHA setup in
  dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: missing TX FIFO prefill before CS in
  dw_spi_g2_indirect_write()
- [Medium] spi: dw-g2: broken transfer type and buswidth checks in
  dw_spi_g2_get_tt()
--

commit 1790b23a62fec2a1f2834ed7c77750ccba4ceadb
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add support for indirect read and write for spi-mem transfers

This commit adds FIFO indirect read and write helpers for spi-mem data
transfers on the DesignWare SPI G2 controller.

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index b2a3e3e8670eb..88766706ef525 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
[ ... ]
> @@ -199,6 +236,63 @@ static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
>  	}
>  }
>  
> +/**
> + * 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
> + *
> + * 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
> + */
> +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);
> +
> +	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;
> +}

[Severity: Medium]
Does dw_spi_g2_get_tt() reject valid multi-wire reads that have no address
phase?  When op->cmd.buswidth > 1 and op->addr.nbytes == 0 (such as 4-0-4
or 8D-0-8D status and ID reads), cmd_m is true and addr_m is false, so
dw_spi_g2_get_tt() returns -EOPNOTSUPP instead of
DW_SPI_G2_TRANS_TYPE_TT2.

Also, because dw_spi_g2_get_frf() programs a single SPI_FRF from the widest
phase, dw_spi_g2_get_tt() does not verify that all multi-wire phases share
the same buswidth as op->data.buswidth.

> @@ -664,6 +758,294 @@ void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
>  			 dw_spi_g2_baudr(dws, freq));
>  }
[ ... ]
> +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_indirect_read_setup() (and dw_spi_g2_indirect_write_setup()
below) omit SPI_CPOL and SPI_CPHA when programming DW_SPI_G2_CTRLR0?

dw_spi_g2_build_ctrlr0() only sets the DFS, TMOD, and FRF fields (unlike
dw_spi_g2_transfer_one(), which ORs in DW_SPI_G2_CTRLR0_SCPOL and
DW_SPI_G2_CTRLR0_SCPHA afterwards), so indirect spi-mem operations always
run in SPI Mode 0 even when dws->curr_spi->mode selects Mode 3.

[ ... ]

> +static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> +				   const struct spi_mem_op *op)
> +{
[ ... ]
> +	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);
> +
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
> +	if (op->addr.nbytes)
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);

[Severity: High]
Can standard 1-1-1 SPI reads work when routed through
dw_spi_g2_indirect_read()?

When a 1-1-1 read has op->data.nbytes == dws->fifo_len, cmd + addr +
dummy + data exceeds dws->fifo_len, so dw_spi_g2_is_reg_op() returns false
and dw_spi_g2_exec_mem_op() dispatches the read here.  For a 1-1-1 op,
dw_spi_g2_get_frf() returns DW_SPI_G2_FRF_STANDARD, pairing
DW_SPI_G2_CTRLR0_TMOD_RO with standard frame format while relying on
SPI_CTRLR0 instruction, address, and wait-cycle framing that only operates
in enhanced multi-wire modes.

[Severity: Medium]
Does casting op->addr.val to u32 and writing DW_SPI_G2_DR0 once truncate
addresses longer than 4 bytes?

dw_spi_g2_exec_mem_op() allows op->addr.nbytes up to
DW_SPI_G2_ADDR_L_MAX_BITS / 8 (7 bytes) and dw_spi_g2_indirect_read_setup()
programs DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK for the full address length.
When op->addr.nbytes > 4, the controller expects two 32-bit address entries
in the TX FIFO, so writing a single 32-bit word drops the upper address
bits and leaves the transfer waiting until dw_spi_g2_xfer_fifo() times out.

[ ... ]

> +static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
> +				    const struct spi_mem_op *op)
> +{
[ ... ]
> +	dw_spi_g2_fill_tx_buf(tx_buf, op, true);
> +
> +	dw_spi_g2_target_enable(dws, dws->cs);
> +	dw_spi_g2_cs_assert(dws, dws->cs);
> +	dw_spi_g2_enable(dws, true);
> +
> +	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;
> +	}
> +
> +	dw_spi_g2_transfer_end(dws);

[Severity: Medium]
Can asserting CS and enabling SSI before prefilling the TX FIFO cause CS to
deassert mid-write?

Unlike dw_spi_g2_reg_op() and dw_spi_g2_indirect_read(), which prefill
DW_SPI_G2_DR0 before calling dw_spi_g2_cs_assert(), this path enables the
controller with SER asserted and an empty FIFO.  Once dw_spi_g2_xfer_fifo()
writes the first byte, hardware immediately moves it to the shift register,
so any interrupt or scheduling delay longer than one byte time drains the
shifter with an empty TX FIFO and deasserts CS in the middle of the page
program.

[Severity: High]
Can dw_spi_g2_indirect_write() call dw_spi_g2_transfer_end() before the TX
FIFO has drained?

For a TX-only transfer, dw_spi_g2_xfer_fifo() finishes by calling
dw_spi_g2_wait_idle(), which only polls !(sr & DW_SPI_G2_SR_BUSY) without
checking (sr & DW_SPI_G2_SR_TFE) as dw_spi_g2_reg_op() does.  If SR_BUSY
reads 0 while queued bytes are still in the TX FIFO,
dw_spi_g2_transfer_end() disables SSI and deasserts CS before the remaining
bytes are shifted out.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795683279.git.prakriti@synopsys.com?part=3

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP
  2026-11-30  2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP Prakriti Sharma
@ 2026-11-30  3:09   ` sashiko-bot
  0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-30  3:09 UTC (permalink / raw)
  To: Prakriti Sharma; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] spi: dw-g2: broken non-XIP fallback in dw_spi_g2_dirmap_read()
- [High] spi: dw-g2: broken byte-by-byte XIP writes in
  dw_spi_g2_dirmap_write()
- [Medium] spi: dw-g2: unchecked dw_spi_g2_wait_idle() and missing
  SR_TFE check
- [Medium] spi: dw-g2: missing SPI_CPOL and SPI_CPHA in
  dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: unlocked sysfs XIP state updates and stale flags
  on disable
- [Medium] spi: dw-g2: hidden CS toggling in dw_spi_g2_dirmap_read()
  breaks NAND cont read
- [Low] spi: dw-g2: shadowed ret declaration in dw_spi_g2_dirmap_read()
- [Low] spi: dw-g2: false timeout in dw_spi_g2_dirmap_write_nor_xip()
  WIP poll
--

commit 429c9f081b444c98b1718b411f07b7d5c936d4a9
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add support for memory-mapped XIP

This commit adds spi-mem direct mapping support for memory-mapped XIP
access along with device tree and sysfs controls for XIP, continuous, and
prefetch modes.

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index f50dcfe2d854c..972690cf47b9f 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c

[ ... ]

> @@ -1061,6 +1114,634 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
>  	return ret;
>  }
>  

[ ... ]

> +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;
> +	}
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dws->cs >= DW_SPI_G2_MAX_CS)
> +		return -EINVAL;
> +
> +	if (!dw_spi_g2_supports_mem_ops(desc->mem, op))
> +		return -EOPNOTSUPP;
> +
> +	if (desc->info.offset >= dws->xip_len ||
> +	    desc->info.length > dws->xip_len - desc->info.offset)
> +		return -EINVAL;
> +
> +	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);
> +
> +	/*
> +	 * 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);

[Severity: Medium]
Does dw_spi_g2_build_ctrlr0() omit SPI_CPOL and SPI_CPHA when programming
DW_SPI_G2_CTRLR0?

dw_spi_g2_init() advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, and
dw_spi_g2_transfer_one() manually ORs DW_SPI_G2_CTRLR0_SCPOL and
DW_SPI_G2_CTRLR0_SCPHA into ctrlr0, but dw_spi_g2_build_ctrlr0() itself
only sets DFS, TMOD, and FRF.  As a result, dw_spi_g2_dirmap_create(),
dw_spi_g2_dirmap_read(), dw_spi_g2_dirmap_write_nor_xip(),
dw_spi_g2_dirmap_write(), and the indirect spi-mem setup helpers always
configure CTRLR0 for SPI Mode 0, breaking flashes configured for SPI Mode
1, 2, or 3.

> +
> +	/* 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);
> +	u8 __iomem *src;
> +	u32 ctrlr0;
> +	int ret;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
> +		return -EINVAL;
> +
> +	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;

[Severity: Low]
This isn't a bug, but the inner declaration of ret shadows the outer ret
variable declared at the start of dw_spi_g2_dirmap_read(), which triggers a
-Wshadow warning.

> +
> +		/*
> +		 * Fold dummy bytes into the address field when RO mode is used
> +		 * without the XIP engine. Plain RO with WAIT_CYCLES > 0 can
> +		 * leave the RX FIFO empty; XIP uses WAIT_CYCLES normally.
> +		 */
> +		if (op.dummy.nbytes) {
> +			if (op.addr.nbytes + op.dummy.nbytes > 4)
> +				return -EOPNOTSUPP;
> +			dummy_shift = op.dummy.nbytes * 8;
> +			op.addr.nbytes += op.dummy.nbytes;
> +			op.dummy.nbytes = 0;
> +		}

[Severity: High]
Does returning -EOPNOTSUPP here break flash reads whenever the XIP memory
resource is mapped in DT without snps,xip-enabled (or after xip_mode is
disabled via sysfs)?

dw_spi_g2_dirmap_create() succeeds whenever dws->xip_mem is present, which
leaves desc->nodirmap false.  When spi_mem_dirmap_read() then calls
dw_spi_g2_dirmap_read(), it propagates errors directly without falling back
to spi_mem_no_dirmap_read():

drivers/spi/spi-mem.c:spi_mem_dirmap_read() {
    ...
	if (desc->nodirmap) {
		ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
	} else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
		ret = spi_mem_access_start(desc->mem);
		if (ret)
			return ret;

		ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);

		spi_mem_access_end(desc->mem);
    ...
}

Because op.addr.nbytes + op.dummy.nbytes exceeds 4 for any 4-byte-addressed
read with dummy cycles, as well as 3-byte 1-2-2/1-4-4/1-8-8 reads and
2-byte 1-4-4 SPI-NAND reads, all such reads fail with -EOPNOTSUPP when
dws->use_xip is false.  Even when op.addr.nbytes + op.dummy.nbytes <= 4,
folding dummy bytes into op.addr.nbytes actively drives 0x00 on the bus
instead of tri-stating via WAIT_CYCLES, miscalculates the dummy clock count
whenever op.dummy.buswidth != op.addr.buswidth, and sends 1-1-1 reads
through TMOD_RO enhanced framing instead of TMOD_TR.

> +
> +		while (done < len) {
> +			chunk = min_t(size_t, len - done, dws->fifo_len);
> +
> +			op.addr.val = (u64)(desc->info.offset + 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: Medium]
Can this chunking loop corrupt multi-page SPI-NAND continuous reads by
toggling CS between FIFO chunks while returning the full len?

As noted in dw_spi_g2_adjust_mem_op_size(), SPI-NAND continuous reads
forbid toggling CS mid-transfer.  In spinand_read_from_cache_op(), the
caller detects CS toggling only when spi_mem_dirmap_read() returns a
partial byte count (ret < nbytes), at which point it sets
cont_read_possible = false and retries with -EAGAIN.  Because
dw_spi_g2_mem_caps does not set .no_cs_assertion = true, flashes that
enable continuous read via a configuration register without a separate
cont_read_cache opcode template (such as Macronix MX35LF2GE4AD) use dirmap
for continuous reads.  Looping over dw_spi_g2_indirect_read() in FIFO-sized
chunks (or toggling CS between AHB bursts in XIP mode when
!dws->xip_continuous_mode) deasserts CS mid-page while returning len,
bypassing spinand's fallback check.

[ ... ]

> +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)
> +{

[ ... ]

> +		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);
> +	}

[Severity: Low]
Can this loop return a false -ETIMEDOUT if the task is preempted between
dw_spi_g2_reg_op() and time_after(jiffies, deadline)?  If the first status
read sees DW_SPI_G2_NOR_SR1_WIP set and the task is then preempted past
deadline, time_after() fires without re-reading sr1 after the deadline.

[ ... ]

> +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);
> +	u8 __iomem *dst;
> +	u32 ctrlr0;
> +	bool use_indirect = false;
> +	int ret;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
> +		return -EINVAL;
> +
> +	if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
> +		use_indirect = true;
> +
> +	if (use_indirect && dws->use_xip && dws->xip_mem &&
> +	    desc->info.op_tmpl->addr.nbytes >= 3) {
> +		dev_dbg(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;
> +
> +		dev_dbg(dws->dev,
> +			"dirmap_write: ROUTE=INDIRECT-FIFO cmd=0x%02x offs=0x%llx len=%zu\n",
> +			op.cmd.opcode, (unsigned long long)offs, len);
> +
> +		op.addr.val = desc->info.offset + 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;
> +
> +	dev_dbg(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);
> +	ret = dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
> +	if (ret)
> +		return ret;
> +
> +	dst = (u8 __iomem *)dws->xip_mem + desc->info.offset + 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);
> +	}

[Severity: High]
Won't emitting a separate SPI transaction per byte in these two XIP write
paths corrupt SPI-NAND page writes and break SPI-NOR flashes?

On the SPI-NAND path (addr.nbytes < 3), spinand_create_dirmap() sets up the
write descriptor using update_cache, which uses SPINAND_PROG_LOAD (0x02 or
0x32) rather than Random Data Load (0x84 or 0x34).  Because each writeb()
emits a separate 0x02/0x32 transaction with its own CS assertion, standard
SPI-NAND flashes reset the on-die page cache buffer to 0xFF on every byte,
wiping out bytes 0..i-1.

On the SPI-NOR path (addr.nbytes >= 3), dw_spi_g2_dirmap_write_nor_xip()
issues hardcoded 1-1-1 SDR WREN (0x06) and RDSR1 (0x05) ops and a 1-byte
Page Program per byte inside the controller driver.  Aside from turning a
single 256-byte page program into 256 separate flash program cycles with
udelay(1) busy-polling under ctlr->io_mutex, this breaks SPI-NOR flashes
with internal ECC (which forbid multiple partial page programs to the same
ECC unit) and flashes in 4-4-4 or 8D-8D-8D modes where 1-1-1 SDR opcodes
and 1-byte writes are invalid.  Rejecting SPI_MEM_DATA_OUT in
dw_spi_g2_dirmap_create() (or always routing writes through
dw_spi_g2_indirect_write()) would let spi-nor and spinand use the FIFO write
path directly.

> +
> +	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;
> +}

[Severity: Medium]
Should dw_spi_g2_dirmap_write() check the return value of
dw_spi_g2_wait_idle(dws) instead of ignoring timeouts and returning len?

Also, dw_spi_g2_wait_idle() in drivers/spi/spi-dw-g2.h only checks
!(sr & DW_SPI_G2_SR_BUSY) without requiring (sr & DW_SPI_G2_SR_TFE).  As
noted in dw_spi_g2_reg_op(), polling only !BUSY after prefilling the TX
FIFO and asserting SER (such as in dw_spi_g2_cmd_only() during
dw_spi_g2_dirmap_write_nor_xip()) can return before SER propagates into the
ssi_clk domain and before the TX FIFO drains.

> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> index 1be68c4250e67..1f776d42ef8a2 100644
> --- a/drivers/spi/spi-dw-g2-mmio.c
> +++ b/drivers/spi/spi-dw-g2-mmio.c
> @@ -18,6 +18,196 @@ struct dw_spi_g2_mmio {
>  	struct reset_control	*rstc;
>  };
>  
> +static int dw_spi_g2_xip_mode_set(struct dw_spi_g2 *dws, int val)
> +{
> +	if (val != 0 && val != 1)
> +		return -EINVAL;
> +
> +	if (val && !dws->xip_mem) {
> +		dev_err(dws->dev,
> +			"Cannot enable XIP: no memory-mapped region available\n");
> +		return -EINVAL;
> +	}
> +
> +	dws->use_xip = !!val;
> +	return 0;
> +}

[Severity: Medium]
When xip_mode is set to 0 via sysfs, does dw_spi_g2_xip_mode_set() leave
dws->xip_continuous_mode and dws->xip_prefetch_mode set to true, and can
the sysfs store callbacks race with in-flight dirmap transfers?

Both dw_spi_g2_xip_continuous_mode_set() and
dw_spi_g2_xip_prefetch_mode_set() require dws->use_xip to be active, but
disabling dws->use_xip does not clear those flags, leaving sysfs reporting
1 for continuous and prefetch modes while XIP is off and re-arming them if
XIP is later re-enabled.  In addition, xip_mode_store(),
xip_continuous_mode_store(), and xip_prefetch_mode_store() modify these
fields without holding ctlr->io_mutex, racing with concurrent
dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() calls that read
dws->use_xip multiple times across dependent branches.  Finally, the new
xip_mode, xip_continuous_mode, and xip_prefetch_mode sysfs attributes have
no entries under Documentation/ABI/.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795683279.git.prakriti@synopsys.com?part=5

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-30  3:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
2026-11-30  2:47   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
2026-11-30  3:01   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers Prakriti Sharma
2026-11-30  3:03   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
2026-11-30  2:59   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP Prakriti Sharma
2026-11-30  3:09   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox