All of lore.kernel.org
 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 v2 5/5] spi: dw-g2: Add support for memory-mapped XIP
Date: Mon, 30 Nov 2026 03:09:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-240290@kernel.org> (raw)
In-Reply-To: <8845ded246694877df046ab21efbe1ef4bad50e9.1795683279.git.prakriti@synopsys.com>

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

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

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

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

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

[ ... ]

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

[ ... ]

> +int dw_spi_g2_dirmap_create(struct spi_mem_dirmap_desc *desc)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	const struct spi_mem_op *op = desc->info.op_tmpl;
> +	u32 ctrlr0;
> +	int ret;
> +
> +	if (!dws->xip_mem) {
> +		dev_dbg(dws->dev, "XIP memory region not available\n");
> +		return -EOPNOTSUPP;
> +	}
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dws->cs >= DW_SPI_G2_MAX_CS)
> +		return -EINVAL;
> +
> +	if (!dw_spi_g2_supports_mem_ops(desc->mem, op))
> +		return -EOPNOTSUPP;
> +
> +	if (desc->info.offset >= dws->xip_len ||
> +	    desc->info.length > dws->xip_len - desc->info.offset)
> +		return -EINVAL;
> +
> +	dev_dbg(dws->dev, "dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n",
> +		op->cmd.opcode, op->addr.nbytes,
> +		op->data.dir == SPI_MEM_DATA_IN ? "READ" : "WRITE",
> +		op->dummy.nbytes);
> +
> +	/*
> +	 * Configure CTRLR0 with data frame size and SPI mode settings.
> +	 * XIP mode uses these settings for all memory-mapped accesses.
> +	 */
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +
> +	ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> +					DW_SPI_G2_FRF_STANDARD);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);

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

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

> +
> +	/* Configure XIP registers for read/write operations */
> +	ret = dw_spi_g2_xip_setup(dws, op,
> +				  op->data.dir == SPI_MEM_DATA_OUT);
> +	/*
> +	 * xip_setup enables the target (sets BIT(cs) in
> +	 * DEDICATED_CS_EN) to program per-CS registers.
> +	 * Clean it up so stale bits don't leak into
> +	 * cmd_only / reg_op operations on other CS lines.
> +	 * dirmap_read/write will re-enable when needed.
> +	 */
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +	return ret;
> +}
> +
> +/**
> + * dw_spi_g2_dirmap_read - Direct memory-mapped read from flash
> + * @desc: Direct mapping descriptor
> + * @offs: Offset within the mapped region
> + * @len: Number of bytes to read
> + * @buf: Destination buffer
> + *
> + * Performs direct memory-mapped read using XIP mode. The controller
> + * automatically sends the configured command and address for each access.
> + * Supports 8/16/32-bit data frame sizes for optimal performance.
> + *
> + * Return: Number of bytes read on success, negative error code on failure
> + */
> +ssize_t dw_spi_g2_dirmap_read(struct spi_mem_dirmap_desc *desc,
> +			      u64 offs, size_t len, void *buf)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	u8 __iomem *src;
> +	u32 ctrlr0;
> +	int ret;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
> +		return -EINVAL;
> +
> +	if (!dws->use_xip) {
> +		struct spi_mem_op op = *desc->info.op_tmpl;
> +		size_t chunk, done = 0;
> +		unsigned int dummy_shift = 0;
> +		int ret;

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

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

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

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

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

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

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

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

> +
> +		while (done < len) {
> +			chunk = min_t(size_t, len - done, dws->fifo_len);
> +
> +			op.addr.val = (u64)(desc->info.offset + offs + done)
> +				<< dummy_shift;
> +			op.data.nbytes = chunk;
> +			op.data.buf.in = (u8 *)buf + done;
> +
> +			ret = dw_spi_g2_indirect_read(dws, &op);
> +			if (ret)
> +				return ret;
> +
> +			done += chunk;
> +		}
> +
> +		return (ssize_t)len;
> +	}

[Severity: Medium]
Can this chunking loop corrupt multi-page SPI-NAND continuous reads by
toggling CS between FIFO chunks while returning the full len?

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

[ ... ]

> +static ssize_t dw_spi_g2_dirmap_write_nor_xip(struct dw_spi_g2 *dws,
> +					      struct spi_mem_dirmap_desc *desc,
> +					      u64 offs, size_t len,
> +					      const void *buf)
> +{

[ ... ]

> +		sr1 = 0;
> +		deadline = jiffies +
> +			   msecs_to_jiffies(DW_SPI_G2_NOR_WIP_TIMEOUT_MS);
> +		do {
> +			ret = dw_spi_g2_reg_op(dws, &rdsr_op);
> +			if (ret) {
> +				dev_err(dws->dev,
> +					"XIP NOR write: WIP poll failed at byte %zu: %d\n",
> +					i, ret);
> +				result = ret;
> +				goto out_unwind;
> +			}
> +			if (!(sr1 & DW_SPI_G2_NOR_SR1_WIP))
> +				break;
> +			if (time_after(jiffies, deadline)) {
> +				dev_err(dws->dev,
> +					"XIP NOR write: WIP timeout at byte %zu\n",
> +					i);
> +				result = -ETIMEDOUT;
> +				goto out_unwind;
> +			}
> +			udelay(1);
> +		} while (1);
> +	}

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

[ ... ]

> +ssize_t dw_spi_g2_dirmap_write(struct spi_mem_dirmap_desc *desc,
> +			       u64 offs, size_t len, const void *buf)
> +{
> +	struct spi_device *spi = desc->mem->spi;
> +	struct dw_spi_g2 *dws = spi_controller_get_devdata(spi->controller);
> +	u8 __iomem *dst;
> +	u32 ctrlr0;
> +	bool use_indirect = false;
> +	int ret;
> +
> +	dws->cs = spi_get_chipselect(spi, 0);
> +	dws->curr_spi = spi;
> +
> +	if (dw_spi_g2_dirmap_check_bounds(desc, dws, offs, len))
> +		return -EINVAL;
> +
> +	if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >= 3)
> +		use_indirect = true;
> +
> +	if (use_indirect && dws->use_xip && dws->xip_mem &&
> +	    desc->info.op_tmpl->addr.nbytes >= 3) {
> +		dev_dbg(dws->dev,
> +			"dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
> +			desc->info.op_tmpl->cmd.opcode,
> +			(unsigned long long)offs, len);
> +		return dw_spi_g2_dirmap_write_nor_xip(dws, desc, offs, len, buf);
> +	}
> +
> +	if (use_indirect) {
> +		struct spi_mem_op op = *desc->info.op_tmpl;
> +
> +		dev_dbg(dws->dev,
> +			"dirmap_write: ROUTE=INDIRECT-FIFO cmd=0x%02x offs=0x%llx len=%zu\n",
> +			op.cmd.opcode, (unsigned long long)offs, len);
> +
> +		op.addr.val = desc->info.offset + offs;
> +		op.data.nbytes = len;
> +		op.data.buf.out = buf;
> +
> +		ret = dw_spi_g2_indirect_write(dws, &op);
> +		return ret ? ret : (ssize_t)len;
> +	}
> +
> +	/* XIP write path - only reached for NAND Program Load */
> +	if (!dws->xip_mem)
> +		return -EOPNOTSUPP;
> +
> +	dev_dbg(dws->dev,
> +		"dirmap_write: ROUTE=NAND-XIP cmd=0x%02x offs=0x%llx len=%zu\n",
> +		desc->info.op_tmpl->cmd.opcode,
> +		(unsigned long long)offs, len);
> +
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +	ctrlr0 = dw_spi_g2_build_ctrlr0(dws, DW_SPI_G2_CTRLR0_TMOD_TR,
> +					DW_SPI_G2_FRF_STANDARD);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> +	ret = dw_spi_g2_xip_setup(dws, desc->info.op_tmpl, true);
> +	if (ret)
> +		return ret;
> +
> +	dst = (u8 __iomem *)dws->xip_mem + desc->info.offset + offs;
> +
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, BIT(dws->cs));
> +	dw_spi_g2_enable(dws, true);
> +
> +	/*
> +	 * PIO write into the XIP window. The XIP engine turns every AHB
> +	 * write into one (cmd + addr + data) SPI transaction, but on
> +	 * this controller only the first byte of each AHB beat is shifted
> +	 * out to the flash -- a multi-byte AHB store (writel/writeq, as
> +	 * memcpy_toio() emits on arm64 with the MMU on) drops every byte
> +	 * past the first. The corruption pattern in flash is unmistakable:
> +	 *   buf[0..7]   = 00 01 02 03 04 05 06 07
> +	 *   flash[0..7] = 00 04 ff ff ff ff ff ff   <- only +0 and +4 land
> +	 * confirming two 32-bit AHB beats per writeq, each beat emitting
> +	 * only its low byte. We must therefore push the payload as a
> +	 * writeb() loop so every byte is its own AHB beat -> its own SPI
> +	 * Program-Load transaction. The on-die NAND page buffer
> +	 * auto-increments its column counter on each transaction, so the
> +	 * full page lands correctly.
> +	 */
> +	{
> +		const u8 *src = buf;
> +		size_t i;
> +
> +		for (i = 0; i < len; i++)
> +			writeb(src[i], dst + i);
> +	}

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

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

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

> +
> +	dw_spi_g2_wait_idle(dws);
> +
> +	/* Disable XIP WR mode and clean up DEDICATED_CS_EN */
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_XIP_WR_SER, 0);
> +	dw_spi_g2_enable(dws, false);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +
> +	return (ssize_t)len;
> +}

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

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

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

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

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

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

      reply	other threads:[~2026-11-30  3:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  2:39 [PATCH v2 0/5] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Prakriti Sharma
2026-11-30  2:39 ` [PATCH v2 1/5] spi: dt-bindings: snps,dwc-ssi-2.02a: Add DWC SSI v2.02a controller binding Prakriti Sharma
2026-11-30  2:47   ` sashiko-bot
2026-11-30 13:59   ` Rob Herring (Arm)
2026-11-30 16:41   ` Rob Herring
2026-11-30  2:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
2026-11-30  3:01   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers Prakriti Sharma
2026-11-30  3:03   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
2026-11-30  2:59   ` sashiko-bot
2026-11-30  2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP Prakriti Sharma
2026-11-30  3:09   ` sashiko-bot [this 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-240290@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.