Skip to contentSkip to conversation

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

PATCH In review Prakriti Sharma 3 participants 13 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover spi: dw-g2: add Synopsys DWC SSI v2.02a controller support

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/[email protected]/

Signature · 1 line
2.34.1
#5 lore raw

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

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.

---
 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
MAINTAINERS +1 -0
index e68a4337a651..90b358076e5c 100644
@@ -27310,6 +27310,7 @@ M:	Siddhant Kumar <[email protected]>
 L:	[email protected]
 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 <[email protected]>
drivers/spi/Kconfig +21 -0
index a7b343d124a9..3c2a7ea543fd 100644
@@ -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"
 
drivers/spi/Makefile +3 -0
index b6ab37271555..f9c86827ebc2 100644
@@ -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
drivers/spi/spi-dw-g2-core.c new file +925 -0

925 lines not on this page · raw message

drivers/spi/spi-dw-g2-mmio.c new file +168 -0

168 lines not on this page · raw message

drivers/spi/spi-dw-g2.h new file +388 -0

388 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 3/5 spi: dw-g2: Add support for indirect read and write for spi-mem transfers

In reply to 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.

---
 drivers/spi/spi-dw-g2-core.c | 389 ++++++++++++++++++++++++++++++++++-
 drivers/spi/spi-dw-g2.h      |   8 +
 2 files changed, 396 insertions(+), 1 deletion(-)
drivers/spi/spi-dw-g2-core.c +388 -1
index b2a3e3e8670e..88766706ef52 100644
@@ -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;
 }
 
drivers/spi/spi-dw-g2.h +8 -0
index ab1319ec0564..4063fe2bbc30 100644
@@ -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__ */
Signature · 1 line
2.34.1
#1 lore raw

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

In reply to Prakriti Sharma

From: Siddhant Kumar <[email protected]>

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.

---
 .../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
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +191 -0
new file mode 100644
index 000000000000..7294c9b2694b
@@ -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 <[email protected]>
+  - Prakriti Sharma <[email protected]>
+
+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>;
+      };
+    };
+...
MAINTAINERS +7 -0
index ce1b9e514e08..e68a4337a651 100644
@@ -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 <[email protected]>
+M:	Siddhant Kumar <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
+
 SYNOPSYS HSDK RESET CONTROLLER DRIVER
 M:	Eugeniy Paltsev <[email protected]>
 S:	Supported
Signature · 1 line
2.34.1
#9 lore raw

PATCH v2 4/5 spi: dw-g2: Add support for SPI-NAND devices

In reply to 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.

---
 drivers/spi/spi-dw-g2-core.c | 66 +++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)
drivers/spi/spi-dw-g2-core.c +65 -1
index 88766706ef52..f50dcfe2d854 100644
@@ -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;
 }
Signature · 1 line
2.34.1
#11 lore raw

PATCH v2 5/5 spi: dw-g2: Add support for memory-mapped XIP

In reply to 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.

---
 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(-)
drivers/spi/spi-dw-g2-core.c +816 -38
index f50dcfe2d854..972690cf47b9 100644
@@ -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");
drivers/spi/spi-dw-g2-mmio.c +235 -0

259 lines not on this page · raw message

drivers/spi/spi-dw-g2.h +93 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding

In reply to Prakriti Sharma
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…

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 <[email protected]>

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.

Quoted 1 line

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.

Quoted 6 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +0 -0
new file mode 100644
index 0000000000000..7294c9b2694b4
@@ -0,0 +1,191 @@

[ ... ]

Quoted 25 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +25 -0
+  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:

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 68

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+    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.

[ ... ]

Quoted 9 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +9 -0
+  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: |

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 120

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+      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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#10 lore raw

Re: [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices

In reply to Prakriti Sharma
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…

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 <[email protected]>

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().

Quoted 19 lines
drivers/spi/spi-dw-g2-core.c +6 -0
index 88766706ef525..f50dcfe2d854c 100644
@@ -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)
 

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 691

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +8 -0
 	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.

Quoted 34 lines
drivers/spi/spi-dw-g2-core.c +30 -0
@@ -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 ||

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1106

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +8 -0
+	    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.

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1119

Quoted 12 lines
drivers/spi/spi-dw-g2-core.c +10 -0
 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?

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1122

Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +2 -1
+
 	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)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

Re: [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller

In reply to Prakriti Sharma
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…

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 <[email protected]>

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.

Quoted 6 lines
drivers/spi/spi-dw-g2-core.c new file +0 -0
new file mode 100644
index 0000000000000..b2a3e3e8670eb
@@ -0,0 +1,925 @@

[ ... ]

Quoted 2 lines
drivers/spi/spi-dw-g2-core.c new file +2 -0
+static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
+{

[ ... ]

drivers/spi/spi-dw-g2-core.c line 174

Quoted 7 lines
drivers/spi/spi-dw-g2-core.c new file +7 -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;
+}

[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.

[ ... ]

Quoted 3 lines
drivers/spi/spi-dw-g2-core.c new file +3 -0
+static int dw_spi_g2_cmd_only(struct dw_spi_g2 *dws,
+			      const struct spi_mem_op *op)
+{

[ ... ]

drivers/spi/spi-dw-g2-core.c line 499

Quoted 11 lines
drivers/spi/spi-dw-g2-core.c new file +11 -0
+	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.

[ ... ]

Quoted 4 lines
drivers/spi/spi-dw-g2-core.c new file +4 -0
+static int dw_spi_g2_transfer_one(struct spi_controller *ctlr,
+				  struct spi_device *spi,
+				  struct spi_transfer *xfer)
+{

[ ... ]

drivers/spi/spi-dw-g2-core.c line 810

Quoted 5 lines
drivers/spi/spi-dw-g2-core.c new file +5 -0
+	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().

drivers/spi/spi-dw-g2-core.c line 821

Quoted 11 lines
drivers/spi/spi-dw-g2-core.c new file +11 -0
+
+	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.

[ ... ]

Quoted 2 lines
drivers/spi/spi-dw-g2-core.c new file +2 -0
+int dw_spi_g2_init(struct dw_spi_g2 *dws)
+{

[ ... ]

drivers/spi/spi-dw-g2-core.c line 904

Quoted 5 lines
drivers/spi/spi-dw-g2-core.c new file +5 -0
+	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().

drivers/spi/spi-dw-g2-core.c line 916

Quoted 12 lines
drivers/spi/spi-dw-g2-core.c new file +12 -0
+
+	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.

Quoted 6 lines
drivers/spi/spi-dw-g2-mmio.c new file +0 -0
new file mode 100644
index 0000000000000..1be68c4250e67
@@ -0,0 +1,168 @@

[ ... ]

drivers/spi/spi-dw-g2-mmio.c line 125

Quoted 12 lines
drivers/spi/spi-dw-g2-mmio.c new file +12 -0
+		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().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers

In reply to Prakriti Sharma
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…

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 <[email protected]>

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.

Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +0 -0
index b2a3e3e8670eb..88766706ef525 100644

[ ... ]

Quoted 50 lines
drivers/spi/spi-dw-g2-core.c +46 -0
@@ -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);

drivers/spi/spi-dw-g2-core.c static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws) · line 294

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+
+	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.

Quoted 3 lines
drivers/spi/spi-dw-g2-core.c +0 -0
@@ -664,6 +758,294 @@ void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
 			 dw_spi_g2_baudr(dws, freq));
 }

[ ... ]

drivers/spi/spi-dw-g2-core.c void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq) · line 802

Quoted 5 lines
drivers/spi/spi-dw-g2-core.c +5 -0
+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.

[ ... ]

Quoted 3 lines
drivers/spi/spi-dw-g2-core.c +3 -0
+static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
+				   const struct spi_mem_op *op)
+{

[ ... ]

drivers/spi/spi-dw-g2-core.c void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq) · line 931

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+	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.

[ ... ]

Quoted 3 lines
drivers/spi/spi-dw-g2-core.c +3 -0
+static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
+				    const struct spi_mem_op *op)
+{

[ ... ]

Quoted 7 lines
drivers/spi/spi-dw-g2-core.c +7 -0
+	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);

drivers/spi/spi-dw-g2-core.c void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq) · line 1042

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#12 lore raw

Re: [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP

In reply to Prakriti Sharma
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…

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 <[email protected]>

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.

Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +0 -0
index f50dcfe2d854c..972690cf47b9f 100644

[ ... ]

Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +0 -0
@@ -1061,6 +1114,634 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
 	return ret;
 }
 

[ ... ]

Quoted 31 lines
drivers/spi/spi-dw-g2-core.c +31 -0
+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);
+

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1375

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+	/*
+	 * 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.

Quoted 38 lines
drivers/spi/spi-dw-g2-core.c +38 -0
+
+	/* 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);

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1423

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -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.

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1436

Quoted 13 lines
drivers/spi/spi-dw-g2-core.c +13 -0
+
+		/*
+		 * 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.

Quoted 8 lines
drivers/spi/spi-dw-g2-core.c +8 -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;

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1454

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+
+			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.

[ ... ]

Quoted 5 lines
drivers/spi/spi-dw-g2-core.c +5 -0
+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)
+{

[ ... ]

Quoted 14 lines
drivers/spi/spi-dw-g2-core.c +14 -0
+		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;

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1606

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+			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.

[ ... ]

Quoted 81 lines
drivers/spi/spi-dw-g2-core.c +81 -0
+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

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1733

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+	 * 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.

drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1743

Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10 -0
+
+	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.

Quoted 12 lines
drivers/spi/spi-dw-g2-mmio.c +4 -0
index 1be68c4250e67..1f776d42ef8a2 100644
@@ -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;

drivers/spi/spi-dw-g2-mmio.c struct dw_spi_g2_mmio { · line 34

Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c +10 -0
+
+	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/.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#3 lore raw

Re: [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding

In reply to Prakriti Sharma
Quoted 13 lines · Prakriti Sharma

From: Siddhant Kumar <[email protected]>

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.

Quoted 8 lines
---
 .../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

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

yamllint warnings/errors:
./Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml:148:36: [warning] too few spaces after comma (commas) ./Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml:149:34: [warning] too few spaces after comma (commas)

dtschema/dtc warnings/errors:

doc reference errors (make refcheckdocs):

See https://patchwork.kernel.org/project/devicetree/patch/ba68924289d632fd49c4e1a42459e3a426edfd2b.1795683279.git.prakriti@synopsys.com

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

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

pip3 install dtschema --upgrade

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

maintainer
#4 lore raw

Re: [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding

In reply to Prakriti Sharma
Quoted 30 lines · Prakriti Sharma

From: Siddhant Kumar <[email protected]>

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.

---
 .../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
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +3 -0
new file mode 100644
index 000000000000..7294c9b2694b
@@ -0,0 +1,191 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 13

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+$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 <[email protected]>
+  - Prakriti Sharma <[email protected]>
+
+description: |

No need for '|' here.

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 20

Quoted 7 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +7 -0
+  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

Is that the only version of the IP?

We generally don't allow only a IP compatible because there are typically SoC specific quirks. If there's no SoC (or some FPGA validation platform?) yet, you can do something like:

compatible:
items:
- not: {}
- const: snps,dwc-ssi-2.02a

Quoted 5 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +5 -0
+
+  reg:
+    minItems: 1
+    items:
+      - description: SSI controller register space

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 35

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+      - description: XIP memory-mapped window for direct flash access
+
+  reg-names:
+    minItems: 1
+    items:
+      - const: ssi_base
+      - const: xip_mem
+
+  interrupts:
+    description: |

Don't need '|'.

Quoted 11 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +11 -0
+      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

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 56

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+      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]

I would assume it's either 1 combined or all 4 interrupts, so define the order.

items:
- const: rxf
- const: txe
- const: txaf
- const: wsf

And drop minItems/maxItems.

Quoted 34 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +34 -0
+
+  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

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 100

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -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.

Drop. If this is needed, then it goes in the child nodes.

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 106

Quoted 6 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +6 -0
+
+  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.

This should be dropped. OS policy as sashiko points out. And you can determine XIP capable by presence of the 2nd reg entry.

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 114

Quoted 8 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +8 -0
+
+  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.

That's a property of the flash, not the controller.

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 120

Quoted 6 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +6 -0
+
+  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.

Why would you not want this always?

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 125

Quoted 5 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +5 -0
+
+patternProperties:
+  "^.*@[0-9a-f]+$":
+    type: object
+    $ref: spi-peripheral-props.yaml

This is not how this schema is used. Drop.

Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml line 135

Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +10 -0
+    additionalProperties: true
+
+    properties:
+      reg:
+        minimum: 0
+        maximum: 3
+
+      rx-sample-delay-ns:
+        description:
+          Per-device Rx sample delay override, in nanoseconds.

This property is already supported in spi-peripheral-props.yaml, so no need to define it again here.

Rob