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 v2 4/5] spi: dw-g2: Add support for SPI-NAND devices
Date: Mon, 30 Nov 2026 02:59:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-240279@kernel.org> (raw)
In-Reply-To: <5f3488118892b90457aeb5696b7a003e4cef274a.1795683279.git.prakriti@synopsys.com>

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

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

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

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

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

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

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

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

	/* dw_spi_g2_is_reg_op() guarantees the whole stream fits the FIFO */
	for (i = 0; i < len; i++)
		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, buf[i]);

	dw_spi_g2_cs_assert(dws, dws->cs);
	...
}

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

> @@ -1049,13 +1064,62 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
>  static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
>  				       const struct spi_mem_op *op)
>  {
> +	bool op_is_dtr =
> +		op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
> +
> +	if (op_is_dtr) {
> +		/*
> +		 * DTR requires enhanced SPI modes (Dual/Quad/Octal).
> +		 * Reject standard-width DTR since it has no practical use
> +		 * and the hardware doesn't support it meaningfully.
> +		 */
> +		if (op->data.nbytes && op->data.buswidth == 1)
> +			return false;
> +
> +		/*
> +		 * In DTR mode all phases must agree on DTR — reject
> +		 * mixed STR/DTR within a single operation.
> +		 */
> +		if (op->addr.nbytes && op->addr.dtr != op->cmd.dtr)
> +			return false;
> +		if (op->data.nbytes && op->data.dtr != op->cmd.dtr)
> +			return false;
> +		if (op->dummy.nbytes && op->dummy.dtr != op->cmd.dtr)
> +			return false;
> +	}
> +
> +	/*
> +	 * cmd_only / indirect_write stay FRF_STANDARD. Reject multi-wire
> +	 * or DTR data-out and command-only so spi-mem falls back to 1-1-1.
> +	 * Multi-wire reads use the enhanced RO path when get_tt() works.
> +	 */
> +	if (op->data.dir == SPI_MEM_DATA_OUT ||
> +	    op->data.dir == SPI_MEM_NO_DATA) {
> +		if (!dw_spi_g2_op_is_std_spi(op))
> +			return false;
> +	} else if (!dw_spi_g2_op_is_std_spi(op) &&
> +		   dw_spi_g2_get_tt(op) < 0) {
> +		return false;
> +	}
> +
>  	return spi_mem_default_supports_op(mem, op);
>  }

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

There are three mismatches in this validation:

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

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

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

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

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

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

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

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

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

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

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

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_is_reg_op() {
	u32 total = op->cmd.nbytes + op->addr.nbytes +
		    op->dummy.nbytes + op->data.nbytes;
	u32 limit = min_t(u32, dws->fifo_len, DW_SPI_G2_REG_OP_BUF_SIZE);

	return dw_spi_g2_op_is_std_spi(op) &&
	       op->data.nbytes > 0 && total <= limit;
}

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

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

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

  reply	other threads:[~2026-11-30  2:59 UTC|newest]

Thread overview: 11+ 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  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 [this message]
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

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-240279@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