Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Prakriti Sharma" <Prakriti.Sharma@synopsys.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 6/6] spi: dw-g2: Add XIP support
Date: Thu, 19 Nov 2026 10:32:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-222959@kernel.org> (raw)
In-Reply-To: <20261119100513.433044-7-prakriti@synopsys.com>

Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] spi: dw-g2: broken NOR XIP write path in
  dw_spi_g2_dirmap_write()
- [High] spi: dw-g2: address truncation and shift overflow in
  dw_spi_g2_dirmap_read()
- [High] spi: dw-g2: ignored desc->info.offset and bounds overflow in
  dirmap ops
- [High] spi: dw-g2: stale XIP_CTRL and XIP_WRITE_CTRL left set for
  exec_mem_op
- [High] spi: dw-g2: MMIO write after reset and missing put in
  dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: sampling mode bits clobbered after
  dw_spi_g2_set_sampling_mode()
- [Medium] spi: dw-g2: broken per-CS rx_sample_dly tracking and missing
  DT default
- [Medium] spi: dw-g2: 8-bit mask on 16-bit BAUDR in
  dw_spi_g2_update_ddr_drive_edge()
- [Medium] spi: dw-g2: dw_spi_g2_op_freq() ignores max_speed_hz and
  reg_op skips BAUDR
- [Medium] spi: dw-g2: missing dw_spi_g2_deinit() on probe failure leaks
  debugfs
- [Medium] spi: dw-g2: missing op validation in dirmap_create and
  unchecked errors
- [Low] spi: dw-g2: unused DMA struct fields and duplicate undocumented
  sysfs files
--

commit 31552d5f4b52b9bd42041e49fac8bf977363c65c
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add XIP support

This commit adds memory-mapped execute-in-place (XIP) support via the
spi-mem direct-mapping interface, along with runtime mode controls, per-op
baud rate programming, and RX sampling configuration.

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index a94d4a7870dfd..2e265459381d1 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
[ ... ]
> @@ -152,28 +261,61 @@ int dw_spi_g2_setup(struct spi_device *spi)
>  	dw_spi_g2_target_disable(dws, dws->cs);
>  	dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
>  
> +	/* Configure RX sample delay for chip select */
> +	if (dws->cs < DW_SPI_G2_MAX_CS) {
> +		u32 rx_dly_ns;
> +
> +		if (device_property_read_u32(&spi->dev, "rx-sample-delay-ns",
> +					     &rx_dly_ns))
> +			rx_dly_ns = dws->def_rx_sample_dly_ns;
> +
> +		dws->rx_sample_dly[dws->cs] =
> +			DIV_ROUND_CLOSEST(rx_dly_ns,
> +					  NSEC_PER_SEC / dws->max_freq);
> +	}
[ ... ]
> @@ -230,6 +372,30 @@ static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
>  	return ctrlr0;
>  }
>  
> +static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
> +{
> +	int drain = 0;
> +
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
> +
> +	/* Drain any stale RX data left from previous transfers */
> +	while (dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR) > 0 &&
> +	       drain < dws->fifo_len) {
> +		(void)dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
> +		drain++;
> +	}
> +
> +	/* Update RX sample delay for current CS if value differs. */
> +	if (dws->cs < DW_SPI_G2_MAX_CS &&
> +	    dws->rx_sample_dly[dws->cs] != dws->cur_rx_sample_dly) {
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs),
> +				 dws->rx_sample_dly[dws->cs]);
> +		dws->cur_rx_sample_dly = dws->rx_sample_dly[dws->cs];
> +	}
> +}

[Severity: Medium]
Does tracking per-CS DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) registers with a
single scalar dws->cur_rx_sample_dly skip programming other chip selects?
If CS0 and CS1 share the same non-zero rx_sample_dly value, the first
transfer on CS0 updates dws->cur_rx_sample_dly, causing subsequent
transfers on CS1 to see a match and leave DW_SPI_G2_RX_SAMPLE_DELAY(1)
unprogrammed.

In addition, dws->def_rx_sample_dly_ns is never read from the controller
device node during probe, XIP transfers never call
dw_spi_g2_controller_reset() to program DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs),
and dw_spi_g2_hw_init() does not reset the cached delay state after a
hardware reset.  Writing DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) directly in
dw_spi_g2_setup() and restoring it in dw_spi_g2_hw_init() would avoid the
stale cache.

[ ... ]

> @@ -629,6 +808,90 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
>  	return ret;
>  }
>  
> +/**
> + * dw_spi_g2_op_freq - Choose the SPI clock frequency for a given op
> + * @dws: Driver state
> + * @op: SPI memory operation
> + *
> + * Prefer op->max_freq. If it is unset, use a conservative fraction of
> + * the controller clock rather than dws->max_freq, which can overclock
> + * a slow flash.
> + *
> + * Return: Frequency in Hz to program into BAUDR.
> + */
> +u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
> +		      const struct spi_mem_op *op)
> +{
> +	u32 f = op->max_freq;
> +
> +	if (!f)
> +		f = dws->max_freq / 8;
> +
> +	return f;
> +}

[Severity: Medium]
Can dw_spi_g2_op_freq() ignore the target device's spi-max-frequency?
Unlike spi_mem_exec_op(), the spi_mem_dirmap_create(),
spi_mem_dirmap_read(), and spi_mem_dirmap_write() paths do not run
spi_mem_adjust_op_freq() on desc->info.op_tmpl, so op->max_freq is 0 on
standard SPI-NOR and SPI-NAND dirmap templates.  Falling back to
dws->max_freq / 8 instead of dws->curr_spi->max_speed_hz (and not clamping
f to dws->curr_spi->max_speed_hz) can overclock flashes rated below
dws->max_freq / 8 or throttle faster flashes.

Also, while dw_spi_g2_mem_caps advertises .per_op_freq = true, neither
dw_spi_g2_reg_op_setup() nor dw_spi_g2_cmd_only_setup() calls
dw_spi_g2_program_baudr(), so small transfers handled by dw_spi_g2_reg_op()
and command-only ops run at whatever BAUDR the previous operation left
behind.

> +
> +/**
> + * dw_spi_g2_program_baudr - Program per-CS BAUDR for a given target freq
> + * @dws: Driver state
> + * @freq: Target SPI clock frequency in Hz
> + *
> + * SCKDV must be even (or 1 for passthrough). Must be called with SSI
> + * disabled. If @freq is 0, fall back to a conservative fraction of the
> + * controller clock. No-op if no valid frequency is available or the
> + * active CS is out of range.
> + */
> +void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
> +{
> +	u32 div, baud;
> +
> +	if (dws->cs >= DW_SPI_G2_MAX_CS)
> +		return;
> +
> +	if (!freq)
> +		freq = dws->max_freq / 8;
> +	if (!freq)
> +		return;
> +
> +	div = DIV_ROUND_UP(dws->clk_rate, freq);
> +	if (div <= 1) {
> +		baud = 1;	/* Passthrough mode */
> +	} else {
> +		if (div & 1)
> +			div++;	/* SCKDV must be even */
> +		baud = div;
> +	}
> +
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), baud);
> +}
> +
> +/**
> + * dw_spi_g2_update_ddr_drive_edge - Program per-CS DDR transmit drive edge
> + * @dws: Driver state
> + * @op: SPI memory operation (must have a DDR/DTR phase)
> + *
> + * At BAUDR 1 or 2 the drive edge is set for edge-aligned data so the TX
> + * delay line can centre the clock; at higher divisors the register returns
> + * to 0 (edge-aligned with sclk_out, default DDR behaviour).
> + */
> +static void dw_spi_g2_update_ddr_drive_edge(struct dw_spi_g2 *dws,
> +					    const struct spi_mem_op *op)
> +{
> +	u32 baud;
> +
> +	if (!op->cmd.dtr && !op->addr.dtr && !op->data.dtr)
> +		return;
> +
> +	if (dws->cs >= DW_SPI_G2_MAX_CS)
> +		return;
> +
> +	baud = dw_spi_g2_readl(dws->regs, DW_SPI_G2_BAUDR(dws->cs)) & 0xFF;
> +	if (!baud)
> +		baud = 1;
> +
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs),
> +			 (baud <= 2) ? 1 : 0);
> +}

[Severity: Medium]
Should dw_spi_g2_update_ddr_drive_edge() mask DW_SPI_G2_BAUDR(dws->cs) with
0xFFFF instead of 0xFF?  Because BAUDR is a 16-bit divisor register,
masking with 0xFF truncates any divisor >= 256 to its low byte and
misprograms DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs) to 1 whenever the low byte is
0 or 2 (such as divisors 256 or 258).

In addition, dw_spi_g2_program_baudr() does not clamp baud to
DW_SPI_G2_MAX_BAUDR (65534) the way dw_spi_g2_setup() does, so a low
frequency producing div >= 65535 rounds up to 0x10000 and writes 0 into the
16-bit divisor field.

[ ... ]

> @@ -823,9 +1106,25 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,
>  	if (op->addr.dtr || op->data.dtr)
>  		spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
>  
> +	/*
> +	 * For writes, sampling mode is less critical but enforce
> +	 * mutual exclusivity for consistency with read path.
> +	 */
> +	if (op->data.dtr) {
> +		dw_spi_g2_set_sampling_mode(dws, dws->cs,
> +					    DW_SPI_G2_SAMPLING_DATA_STROBE);
> +	} else {
> +		dw_spi_g2_set_sampling_mode(dws, dws->cs,
> +					    DW_SPI_G2_SAMPLING_RX_DELAY);
> +	}
>  	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
>  	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
>  	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);

[Severity: Medium]
Doesn't writing ctrlr0 and spictrlr0 here immediately overwrite the
register bits that dw_spi_g2_set_sampling_mode() just modified in hardware?
Because local spictrlr0 in dw_spi_g2_indirect_write_setup() never sets
DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK, the DW_SPI_G2_SPI_CTRLR0(dws->cs)
write clobbers the strobe bit set by dw_spi_g2_set_sampling_mode().

Similarly, in dw_spi_g2_indirect_read_setup(), dw_spi_g2_set_sampling_mode()
reads and writes the stale DW_SPI_G2_SPI_CTRLR0(dws->cs) value in hardware
right before it is overwritten with local spictrlr0, and in
dw_spi_g2_xip_setup() calling dw_spi_g2_set_sampling_mode() sets
DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK in DW_SPI_G2_SPI_CTRLR0(dws->cs)
after the XIP callers explicitly zeroed SPI_CTRLR0.

[ ... ]

> +int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	const struct spi_mem_op *op = desc->info.op_tmpl;
> +	u32 ctrlr0;
> +	int ret;
> +
> +	if (!dws->xip_mem) {
> +		dev_dbg(dws->dev, "XIP memory region not available\n");
> +		return -EOPNOTSUPP;
> +	}
> +
> +	dev_dbg(dws->dev, "dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n",
> +		op->cmd.opcode, op->addr.nbytes,
> +		op->data.dir == SPI_MEM_DATA_IN ? "READ" : "WRITE",
> +		op->dummy.nbytes);
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	/*
> +	 * Configure CTRLR0 with data frame size and SPI mode settings.
> +	 * XIP mode uses these settings for all memory-mapped accesses.
> +	 */
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +
> +	ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> +					DW_SPI_G2_FRF_STANDARD);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +
> +	/* Configure XIP registers for read/write operations */
> +	ret = dw_spi_g2_xip_setup(dws, op,
> +				  op->data.dir == SPI_MEM_DATA_OUT);
> +	/*
> +	 * xip_setup enables the target (sets BIT(cs) in
> +	 * DEDICATED_CS_EN) to program per-CS registers.
> +	 * Clean it up so stale bits don't leak into
> +	 * cmd_only / reg_op operations on other CS lines.
> +	 * dirmap_read/write will re-enable when needed.
> +	 */
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +	return ret;
> +}

[Severity: Medium]
Does dw_spi_g2_dirmap_create() validate the operation template before
accepting the direct mapping?  spi_mem_dirmap_create() calls
ctlr->mem_ops->dirmap_create() without calling supports_op first, and
neither dw_spi_g2_dirmap_create() nor dw_spi_g2_xip_setup() checks
dw_spi_g2_supports_mem_ops(desc->mem, op), dws->cs < DW_SPI_G2_MAX_CS,
op->cmd.nbytes <= 2, or op->addr.nbytes * 8U <= DW_SPI_G2_ADDR_L_MAX_BITS.

Also, the return values of dw_spi_g2_xip_setup() (in
dw_spi_g2_dirmap_read(), dw_spi_g2_dirmap_write_nor_xip(), and
dw_spi_g2_dirmap_write()) and dw_spi_g2_wait_idle() (in
dw_spi_g2_cmd_only(), dw_spi_g2_dirmap_write_nor_xip(), and
dw_spi_g2_dirmap_write()) are ignored, masking setup errors and controller
timeouts as successful transfers.

> +
> +/**
> + * dw_spi_g2_dirmap_read - Direct memory-mapped read from flash
> + * @desc: Direct mapping descriptor
> + * @offs: Offset within the mapped region
> + * @len: Number of bytes to read
> + * @buf: Destination buffer
> + *
> + * Performs direct memory-mapped read using XIP mode. The controller
> + * automatically sends the configured command and address for each access.
> + * Supports 8/16/32-bit data frame sizes for optimal performance.
> + *
> + * Return: Number of bytes read on success, negative error code on failure
> + */
> +ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
> +			      u64 offs, size_t len, void *buf)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	void __iomem *src;
> +	u32 ctrlr0;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (!dws->use_xip) {
> +		struct spi_mem_op op = *desc->info.op_tmpl;
> +		size_t chunk, done = 0;
> +		unsigned int dummy_shift = 0;
> +		int ret;
> +
> +		/* Clear XIP registers so they don't interfere with RO mode */
> +		dw_spi_g2_enable(dws, false);
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_CTRL(dws->cs), 0);
> +
> +		/*
> +		 * Fold dummy bytes into the address field.
> +		 *
> +		 * To handle WAIT_CYCLES in RO mode --
> +		 * the RX FIFO stays empty when WAIT_CYCLES > 0.  Work around
> +		 * this by absorbing the dummy phase into the address: shift the
> +		 * real address left by (dummy_nbytes * 8) bits and zero-pad.
> +		 * This makes WAIT_CYCLES=0 while the flash still receives the
> +		 * correct number of clock cycles before data.
> +		 *
> +		 * With TT1 (1-1-x), both address and folded-dummy bits travel
> +		 * on the standard single lane, so the flash sees:
> +		 *   [real addr MSB..LSB] [zero-pad dummy clocks] [data on x lanes]
> +		 */
> +		if (op.dummy.nbytes) {
> +			dummy_shift = op.dummy.nbytes * 8;
> +			op.addr.nbytes += op.dummy.nbytes;
> +			op.dummy.nbytes = 0;
> +		}
> +
> +		while (done < len) {
> +			chunk = min_t(size_t, len - done, dws->fifo_len);
> +
> +			op.addr.val = (u64)(offs + done) << dummy_shift;
> +			op.data.nbytes = chunk;
> +			op.data.buf.in = (u8 *)buf + done;
> +
> +			ret = dw_spi_g2_indirect_read(dws, &op);
> +			if (ret)
> +				return ret;
> +
> +			done += chunk;
> +		}
> +
> +		return (ssize_t)len;
> +	}

[Severity: High]
Doesn't folding op.dummy.nbytes into op.addr.nbytes and shifting
op.addr.val left by dummy_shift truncate the flash address in
dw_spi_g2_indirect_read()?  Because dws->use_xip defaults to false while
dw_spi_g2_dirmap_create() succeeds whenever dws->xip_mem is present,
default dirmap reads take this path and call dw_spi_g2_indirect_read(),
which casts chunk_op.addr.val to u32 and writes only a single 32-bit word:

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_indirect_read() {
    ...
		if (chunk_op.addr.nbytes) {
			dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
					 (u32)chunk_op.addr.val);
			tx_entries++;
		}
    ...
}

Whenever the original op.addr.nbytes + op.dummy.nbytes exceeds 4 bytes
(such as a 3-byte or 4-byte SPI-NOR read with dummy bytes, or a 2-byte
SPI-NAND 1-4-4 read with 4 dummy bytes), shifting left by dummy_shift
pushes the upper address bits (or the entire address when
op.dummy.nbytes >= 4) past bit 31 where the u32 cast discards them, reading
from the wrong flash offset.  When op.dummy.nbytes >= 8 (such as Octal or
Octal DTR reads), dummy_shift >= 64 triggers an undefined 64-bit shift and
overflows the 4-bit DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK field in
dw_spi_g2_indirect_read_setup().  Moreover, when op.dummy.buswidth > 1 in
1-1-x (TT0) mode, op.dummy.nbytes is sized for the wider dummy buswidth
rather than a single address lane, so adding op.dummy.nbytes to
op.addr.nbytes sends the wrong number of dummy clock cycles.

> +
> +	if (!dws->xip_mem)
> +		return -EOPNOTSUPP;
> +
> +	if (offs + len > dws->xip_len)
> +		return -EINVAL;
> +
> +	/*
> +	 * Restore full XIP register state. An indirect transfer (from a
> +	 * prior mode switch) may have left CTRLR0 in TMOD_RO/TO and
> +	 * SPI_CTRLR0 with non-XIP framing, which corrupts XIP accesses.
> +	 */
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +	ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> +					DW_SPI_G2_FRF_STANDARD);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> +	dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, false);
> +
> +	src = dws->xip_mem + offs;

[Severity: High]
Shouldn't dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() add
desc->info.offset to offs?  In the spi-mem dirmap interface, offs is
relative to desc->info.offset; for example, spinand_create_dirmap() encodes
the plane select bit in info.offset, so ignoring desc->info.offset here
(and in the indirect fallback and write paths) causes multi-plane SPI-NAND
reads and writes on plane >= 1 to access plane 0 instead.

Also, dw_spi_g2_dirmap_create() never checks that desc->info.offset and
desc->info.length fit within dws->xip_len (preventing
spi_mem_dirmap_create() from falling back to non-dirmap ops when the flash
exceeds the XIP window), and the check offs + len > dws->xip_len in
dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() can wrap around on
64-bit unsigned addition.

> +
> +	/* Enable XIP mode for read */
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, BIT(dws->cs));
> +	dw_spi_g2_enable(dws, true);
> +
> +	/*
> +	 * PIO read from XIP window. The XIP engine turns each AHB read
> +	 * into a (cmd + addr + dummy + data) SPI transaction.
> +	 */
> +	memcpy_fromio(buf, src, len);
> +
> +	/* Disable XIP mode and clean up DEDICATED_CS_EN */
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_SER, 0);
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +
> +	return (ssize_t)len;
> +}

[Severity: High]
Does leaving DW_SPI_G2_XIP_CTRL(dws->cs) and
DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) non-zero after dw_spi_g2_dirmap_create(),
dw_spi_g2_dirmap_read(), and dw_spi_g2_dirmap_write() interfere with
subsequent dw_spi_g2_exec_mem_op() FIFO transfers?  As noted in the
comments in the indirect branches of dw_spi_g2_dirmap_read() and
dw_spi_g2_dirmap_write(), non-zero XIP_CTRL and XIP_WRITE_CTRL registers
cause the controller to misinterpret FIFO RO/TO-mode operations, yet
neither the XIP teardown paths nor the exec_mem_op setup helpers
(dw_spi_g2_cmd_only_setup(), dw_spi_g2_reg_op_setup(),
dw_spi_g2_indirect_read_setup(), dw_spi_g2_indirect_write_setup()) clear
those registers.

[ ... ]

> +ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
> +			       u64 offs, size_t len, const void *buf)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	void __iomem *dst;
> +	u32 ctrlr0;
> +	bool use_indirect = false;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	/*
> +	 * NOR Page Program (addr_nbytes >= 3) cannot ride the XIP-write
> +	 * engine in burst mode -- the engine emits one cmd+addr+data SPI
> +	 * transaction per AHB write but the SPI-NOR framework only issues
> +	 * WREN once before dirmap_write(), so every transaction past the
> +	 * first arrives with write-enable cleared and is silently dropped.
> +	 * NAND Program Load (addr_nbytes == 2) is XIP-friendly because the
> +	 * on-die page buffer has no per-byte write-enable gating, so it goes
> +	 * to the NAND-XIP-BURST path below.
> +	 */
> +	if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
> +		use_indirect = true;
> +
> +	/*
> +	 * NOR with XIP enabled: route through the byte-by-byte XIP-write
> +	 * helper so the XIP-write engine is exercised end-to-end.
> +	 */
> +	if (use_indirect && dws->use_xip && dws->xip_mem &&
> +	    desc->info.op_tmpl->addr.nbytes >= 3) {
> +		dev_info(dws->dev,
> +			 "dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
> +			 desc->info.op_tmpl->cmd.opcode,
> +			 (unsigned long long)offs, len);
> +		return dw_spi_g2_dirmap_write_nor_xip(dws, desc, offs, len,
> +						      buf);
> +	}

[Severity: High]
Why does dw_spi_g2_dirmap_write() route NOR writes to
dw_spi_g2_dirmap_write_nor_xip() when the commit message states that NOR
page-program writes stay on the indirect FIFO path?

In dw_spi_g2_dirmap_write_nor_xip(), the controller driver bypasses the
spi-nor layer and hardcodes 1-1-1 SDR WREN (0x06) and RDSR1 (0x05) commands
along with 1-byte writeb() transactions for every byte:

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_dirmap_write_nor_xip() {
	const struct spi_mem_op wren_op = SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_WREN, 1),
						     SPI_MEM_OP_NO_ADDR,
						     SPI_MEM_OP_NO_DUMMY,
						     SPI_MEM_OP_NO_DATA);
    ...
	for (i = 0; i < len; i++) {
		const struct spi_mem_op rdsr_op =
			SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_RDSR1, 1),
				   SPI_MEM_OP_NO_ADDR,
				   SPI_MEM_OP_NO_DUMMY,
				   SPI_MEM_OP_DATA_IN(1, &sr1, 1));
    ...
		ret = dw_spi_g2_cmd_only(dws, &wren_op);
}

This breaks flashes operating in 4-4-4 QPI, 8-8-8 Octal, or 8D-8D-8D Octal
DTR mode (where 1-1-1 SDR 0x06/0x05 commands are invalid and 1-byte page
programs violate 2-byte DTR / internal-ECC alignment), and it invokes
dw_spi_g2_cmd_only() (a TO-mode FIFO transfer) while
DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) is still non-zero.

In addition, all three routing branches in dw_spi_g2_dirmap_write() call
dev_info() on every write call, which will flood the kernel log during
normal flash writes.

> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> index 6ef8f926e0eb9..9620ae6e5c23c 100644
> --- a/drivers/spi/spi-dw-g2-mmio.c
> +++ b/drivers/spi/spi-dw-g2-mmio.c
[ ... ]
> @@ -151,7 +386,29 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
>  	if (ret)
>  		goto err_pm;
>  
> -	dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
> +	ret = device_create_file(&pdev->dev, &dev_attr_xip_mode);
> +	if (ret)
> +		dev_warn(&pdev->dev,
> +			 "Failed to create xip_mode sysfs attribute: %d\n",
> +			 ret);
> +
> +	if (dws->xip_mem) {
> +		ret = device_create_file(&pdev->dev,
> +					 &dev_attr_xip_continuous_mode);
> +		if (ret)
> +			dev_warn(&pdev->dev,
> +				 "Failed to create xip_continuous_mode sysfs: %d\n",
> +				 ret);
> +
> +		ret = device_create_file(&pdev->dev,
> +					 &dev_attr_xip_prefetch_mode);
> +		if (ret)
> +			dev_warn(&pdev->dev,
> +				 "Failed to create xip_prefetch_mode sysfs: %d\n",
> +				 ret);
> +	}
> +	dev_info(&pdev->dev, "DW SSI G2 MMIO probed (mode=%s)\n",
> +		 dws->use_xip ? "XIP" : "indirect");
>  	return 0;
>  
>  err_pm:

[Severity: Medium]
If devm_request_irq() or spi_register_controller() fails after
dw_spi_g2_init(dws) has created the debugfs directory and files, doesn't
jumping to err_reset or err_pm without calling dw_spi_g2_deinit(dws) leak
the debugfs entries?  Because spi_controller_put(ctlr) frees ctlr and the
embedded dws structure on that error path, the leaked debugfs files retain
dangling pointers to dws and dws->regset, causing a use-after-free if they
are accessed in debugfs.

> @@ -168,10 +425,14 @@ static void dw_spi_g2_mmio_remove(struct platform_device *pdev)
>  	struct spi_controller *ctlr = platform_get_drvdata(pdev);
>  	struct dw_spi_g2_mmio *dwsmmio = spi_controller_get_devdata(ctlr);
>  
> +	device_remove_file(&pdev->dev, &dev_attr_xip_mode);
> +	device_remove_file(&pdev->dev, &dev_attr_xip_continuous_mode);
> +	device_remove_file(&pdev->dev, &dev_attr_xip_prefetch_mode);
>  	spi_unregister_controller(ctlr);
>  	dw_spi_g2_deinit(&dwsmmio->dws);
>  	pm_runtime_disable(&pdev->dev);
>  	reset_control_assert(dwsmmio->rstc);
> +	dw_spi_g2_enable(&dwsmmio->dws, false);
>  }

[Severity: High]
Doesn't calling dw_spi_g2_enable(&dwsmmio->dws, false) after
reset_control_assert(dwsmmio->rstc) write to DW_SPI_G2_SSIENR while the
controller is held in hardware reset?  On many SoCs, MMIO accesses to an IP
block with its reset line asserted trigger a synchronous external abort or
bus hang; disabling the controller should happen before asserting reset.

Also, dw_spi_g2_mmio_probe() allocates ctlr with non-managed
spi_alloc_host(), and spi_unregister_controller(ctlr) only drops the
reference taken by spi_register_controller(), so dw_spi_g2_mmio_remove()
still needs to call spi_controller_put(ctlr) to avoid leaking ctlr and
dwsmmio.

> diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
> index 3091697238d7d..162d233902982 100644
> --- a/drivers/spi/spi-dw-g2.h
> +++ b/drivers/spi/spi-dw-g2.h
[ ... ]
> @@ -260,9 +371,32 @@ struct dw_spi_g2 {
>  	struct completion       mem_done;
>  	int                     error;
>  
> +	/* XIP (Direct Mapping) support */
> +	void __iomem		*xip_mem;
> +	resource_size_t		xip_phys;
> +	size_t			xip_len;
> +	bool			use_xip;
> +	bool			xip_continuous_mode;
> +	bool			xip_prefetch_mode;
> +
> +	/* Per-CS RX sample delay (in SSI clock cycles) */
> +	u8			rx_sample_dly[DW_SPI_G2_MAX_CS];
> +	u8			cur_rx_sample_dly;
> +	u32			def_rx_sample_dly_ns;
> +
>  	/* Flash type detection for timing workarounds */
>  	bool			is_nand;
>  
> +
> +	/* DMA support for FIFO/Indirect operations (External DMA) */
> +	struct dma_chan		*tx_chan;
> +	struct dma_chan		*rx_chan;
> +	struct completion	tx_dma_complete;
> +	struct completion	rx_dma_complete;
> +	bool			use_dma_indirect;  /* DMA for FIFO operations */
> +	bool			use_dma_read;      /* DMA for XIP (placeholder) */
> +	u32			dma_caps;          /* DMA capability flags */
> +
>  #ifdef CONFIG_DEBUG_FS
>  	struct dentry *debugfs;
>  	struct debugfs_regset32 regset;
[ ... ]
> @@ -319,6 +456,17 @@ int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op);
>  
>  irqreturn_t dw_spi_g2_irq(int irq, void *dev_id);
>  
> +/* XIP/Direct mapping support */
> +int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc);
> +ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
> +			      u64 offs, size_t len, void *buf);
> +ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
> +			       u64 offs, size_t len, const void *buf);
> +
> +u32 dw_spi_g2_op_freq(struct dw_spi_g2 *dws,
> +		      const struct spi_mem_op *op);
> +void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq);

[Severity: Low]
These seven DMA fields in struct dw_spi_g2 are unused in this patch, and
dw_spi_g2_dirmap_create(), dw_spi_g2_dirmap_read(),
dw_spi_g2_dirmap_write(), dw_spi_g2_op_freq(), dw_spi_g2_program_baudr(),
and dw_spi_g2_set_sampling_mode() are only used within
drivers/spi/spi-dw-g2-core.c and can be static.  Also,
dw_spi_g2_mmio_probe() creates undocumented sysfs attributes (xip_mode,
xip_continuous_mode, xip_prefetch_mode) via device_create_file() after
spi_register_controller() that duplicate the debugfs controls mentioned in
the commit message.

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

      reply	other threads:[~2026-11-19 10:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 10:05 [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
2026-11-19 10:05 ` [PATCH 1/6] dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding Prakriti Sharma
2026-11-19 10:05 ` [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver Prakriti Sharma
2026-11-19 10:28   ` sashiko-bot
2026-11-19 10:05 ` [PATCH 3/6] spi: dw-g2: Add indirect read and write Prakriti Sharma
2026-11-19 10:27   ` sashiko-bot
2026-11-19 10:05 ` [PATCH 4/6] spi: dw-g2: Add SPI-NAND support Prakriti Sharma
2026-11-19 10:25   ` sashiko-bot
2026-11-19 10:05 ` [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties Prakriti Sharma
2026-11-19 10:14   ` sashiko-bot
2026-11-19 10:05 ` [PATCH 6/6] spi: dw-g2: Add XIP support Prakriti Sharma
2026-11-19 10:32   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-222959@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Prakriti.Sharma@synopsys.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox