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 3/6] spi: dw-g2: Add indirect read and write
Date: Thu, 19 Nov 2026 10:27:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-222952@kernel.org> (raw)
In-Reply-To: <20261119100513.433044-4-prakriti@synopsys.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] spi: dw-g2: multi-line writes sent in 1-1-1 mode in
  dw_spi_g2_indirect_write()
- [High] spi: dw-g2: premature TX termination and ignored timeout 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: dw_spi_g2_exec_mem_op() routes non-1-1-1 ops to
  dw_spi_g2_reg_op()
- [Medium] spi: dw-g2: missing CPOL/CPHA setup in
  dw_spi_g2_build_ctrlr0()
- [Low] spi: dw-g2: unused variable tx_entries in
  dw_spi_g2_indirect_read()
- [Low] spi: dw-g2: malformed kernel-doc comment above
  dw_spi_g2_indirect_write_setup()
--

commit 5c69b8ce5159adba9406d4ca9e6fadba79d41ae5
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>

spi: dw-g2: Add indirect read and write

This commit adds the FIFO-based indirect read (receive-only) and indirect
write (transmit-only) paths for spi-mem data transfers.

> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> index 7b0928f671033..c640d6a192600 100644
> --- a/drivers/spi/spi-dw-g2-core.c
> +++ b/drivers/spi/spi-dw-g2-core.c
> @@ -538,6 +538,237 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)
>   * Indirect read operations
>   */
>  
> +/**
> + * 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));

[Severity: Medium]
Does dw_spi_g2_build_ctrlr0() omit DW_SPI_G2_CTRLR0_SCPOL and
DW_SPI_G2_CTRLR0_SCPHA from ctrlr0?

The controller advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but
dw_spi_g2_build_ctrlr0() never checks dws->curr_spi->mode, so indirect
reads and writes always program CTRLR0 for SPI Mode 0 and fail on flash
devices configured for SPI Mode 1, 2, or 3.

[ ... ]

> +static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
> +				   const struct spi_mem_op *op)
> +{
> +	u32 tx_entries;
> +	u8 dummy_buf = 0;
> +	int ret;

[ ... ]

> +	tx_entries = 0;
> +
> +	/* Write instruction */
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
> +	tx_entries++;
> +
> +	/* Write address as a packed 32-bit value (one FIFO entry) */
> +	if (op->addr.nbytes) {
> +		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
> +		tx_entries++;
> +	}

[Severity: Low]
tx_entries is incremented here but never read, which triggers a
-Wunused-but-set-variable warning under W=1 builds.

[Severity: Medium]
Does casting op->addr.val to u32 and writing a single DR0 entry break
reads withop->addr.nbytes > 4?

dw_spi_g2_exec_mem_op() allows addresses up to DW_SPI_G2_ADDR_L_MAX_BITS
(60 bits) and dw_spi_g2_indirect_read_setup() programs ADDR_L for the
full op->addr.nbytes (and commit "spi: dw-g2: Add XIP and dirmap
support" folds dummy bytes into op.addr.nbytes before calling
dw_spi_g2_indirect_read()).  When ADDR_L > 8 (more than 32 bits), the
controller expects two 32-bit address entries in the TX FIFO before
starting the receive phase, so writing only one truncated 32-bit word
stalls the transfer until dw_spi_g2_xfer_fifo() times out.

[ ... ]

> +/**
> + * Indirect write operations (Page Program)
> + * Uses TMOD=TO (Transmit-Only) mode
> + */

[Severity: Low]
This section comment starts with /** instead of /*, which causes a
scripts/kernel-doc warning on W=1 builds.

> +/**
> + * dw_spi_g2_indirect_write_setup - Configure controller for indirect write
> + * @dws: Driver state
> + * @op: SPI memory operation
> + *
> + * Sets up TMOD=TO (transmit-only), standard SPI mode.
> + */
> +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);
> +
> +	dw_spi_g2_controller_reset(dws);
> +	dw_spi_g2_target_disable(dws, dws->cs);
> +
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
> +	dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
> +}

[Severity: High]
Can dw_spi_g2_indirect_write_setup() and dw_spi_g2_indirect_write() send
multi-line write operations in single-wire 1-1-1 mode?

dw_spi_g2_init() sets SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL in
ctlr->mode_bits and dw_spi_g2_supports_mem_ops() delegates directly to
spi_mem_default_supports_op(), so spi-mem negotiates multi-wire Page
Program operations (such as 1-1-4 or 1-4-4 Quad writes).  However,
dw_spi_g2_indirect_write_setup() hardcodes DW_SPI_G2_FRF_STANDARD with
DW_SPI_G2_SPI_CTRLR0 set to 0, and dw_spi_g2_indirect_write() streams
[cmd][addr][data] over IO0 without multi-line framing or dummy cycles,
corrupting the data written to flash.

[ ... ]

> +	/* Assert CS - transfer starts */
> +	dw_spi_g2_cs_assert(dws, dws->cs);
> +
> +	/* Refill FIFO with remaining bytes (if any) via polling */
> +	if (pos < tx_total) {
> +		int ret = dw_spi_g2_xfer_fifo(dws, &tx_buf[pos],
> +					      tx_total - pos, NULL, 0);
> +		if (ret) {
> +			dw_spi_g2_transfer_end(dws);
> +			dw_spi_g2_controller_reset(dws);
> +			kfree(tx_buf);
> +			return ret;
> +		}
> +	}
> +
> +	/* Wait for transfer completion */
> +	dw_spi_g2_wait_idle(dws);
> +
> +	dw_spi_g2_transfer_end(dws);
> +	dw_spi_g2_controller_reset(dws);
> +
> +	kfree(tx_buf);
> +	return 0;
> +}

[Severity: High]
Can dw_spi_g2_wait_idle() return before the pre-filled TX FIFO has
drained, and does ignoring its return value hide transfer timeouts?

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

When tx_total <= dws->fifo_len, dw_spi_g2_indirect_write() asserts CS and
immediately calls dw_spi_g2_wait_idle().  Because SER is written in the
APB clock domain while frames begin shifting on the divided ssi_clk,
DW_SPI_G2_SR_BUSY can still read 0 before the first byte is popped from
the TX FIFO (while DW_SPI_G2_SR_TFE is 0), causing
dw_spi_g2_transfer_end() to disable SSIENR and discard the queued write.
In addition, both dw_spi_g2_indirect_write() and dw_spi_g2_xfer_fifo()
ignore the -ETIMEDOUT return value of dw_spi_g2_wait_idle().  Commit
"spi: dw-g2: Add SPI-NAND support" later routes indirect writes through
dw_spi_g2_xfer_fifo(), which still leaves dw_spi_g2_wait_idle() missing
a DW_SPI_G2_SR_TFE check and its return value unchecked.

> @@ -626,12 +857,11 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
>  	if (dw_spi_g2_is_reg_op(op))
>  		return dw_spi_g2_reg_op(dws, op);
>  
> -	/*
> -	 * Bulk reads and writes are added by the indirect-transfer patch.
> -	 * This patch handles commands and short register operations only.
> -	 */
> -	if (op->data.dir != SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op(op))
> -		return -EOPNOTSUPP;
> +	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;
>  }

[Severity: Medium]
Does checking dw_spi_g2_is_reg_op() before the indirect paths misroute
short non-1-1-1 operations to dw_spi_g2_reg_op()?

drivers/spi/spi-dw-g2-core.c:dw_spi_g2_is_reg_op() {
	return !op->addr.nbytes && op->data.nbytes > 0 &&
	       op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
}

dw_spi_g2_is_reg_op() does not check that op->cmd.nbytes == 1,
op->dummy.nbytes == 0, all phase buswidths are 1, and DTR is disabled,
whereas dw_spi_g2_reg_op() only sends a 1-byte opcode in 1-1-1 standard
SPI mode without dummy cycles.  Short operations using dummy cycles,
2-byte opcodes, multi-wire buswidths, or DTR are sent through
dw_spi_g2_reg_op() instead of dw_spi_g2_indirect_read(), and commit
"spi: dw-g2: Add SPI-NAND support" widens dw_spi_g2_is_reg_op() to match
addressed operations up to fifo_len bytes with the same 1-1-1 restriction.

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

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

Thread overview: 18+ 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 20:11   ` Mark Brown
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 [this message]
2026-11-20  0:07   ` kernel test robot
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 12:18   ` Krzysztof Kozlowski
2026-11-19 10:05 ` [PATCH 6/6] spi: dw-g2: Add XIP support Prakriti Sharma
2026-11-19 10:32   ` sashiko-bot
2026-11-19 20:11   ` Mark Brown
2026-11-19 21:29   ` kernel test robot
2026-11-19 20:11 ` [PATCH 0/6] spi: dw-g2: add Synopsys DWC SSI v2.02a controller support Mark Brown

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-222952@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.