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 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
Date: Thu, 19 Nov 2026 10:28:01 +0000 [thread overview]
Message-ID: <sashiko-outbox-222953@kernel.org> (raw)
In-Reply-To: <20261119100513.433044-3-prakriti@synopsys.com>
Thank you for your contribution! Sashiko AI review found 13 potential
issues to consider:
- [High] spi: dw-g2: dw_spi_g2_supports_mem_ops() accepts ops
misexecuted by reg_op()
- [Medium] spi: dw-g2: debugfs init before bus_num allocation and leak
on probe error
- [Medium] spi: dw-g2: missing SPI_CPOL and SPI_CPHA setup in
dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: missing spi_controller_put() in
dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: debugfs leak, UAF, and -1 bus_num in
dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: devm IRQ outlives reset and dws in probe() and
remove()
- [Medium] spi: dw-g2: unwired dw_spi_g2_transfer_one() locks up CPU if
called
- [Medium] spi: dw-g2: missing txe, txaf, and wsf IRQ requests in
dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: unvalidated num-cs and clobbered dws->caps in
probe and init
- [Medium] spi: dw-g2: missing linux/bitfield.h include for FIELD_PREP()
- [Medium] spi: dw-g2: unchecked dw_spi_g2_wait_idle() and missing
SR_TFE check
- [Low] spi: dw-g2: MAINTAINERS glob misses spi-dw-g2.h and bad
kernel-doc markers
- [Low] spi: dw-g2: non-kernel-doc comments using /** in
spi-dw-g2-core.c
--
commit 6b1bdfd4c5618c387e6d2d343a2ddf90fcf357ed
Author: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
This commit adds the core and MMIO driver for the Synopsys DesignWare SSI
Gen2 (DWC_ssi v2.02a) SPI controller, supporting probe, command-only
transfers, and short register operations via polling.
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e68a4337a651a..e0f97d2792b1d 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -27310,6 +27310,7 @@ M: Siddhant Kumar <sidkumar@synopsys.com>
> L: linux-spi@vger.kernel.org
> S: Maintained
> F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
> +F: drivers/spi/spi-dw-g2-*.[ch]
[Severity: Low]
Does the hyphen before the wildcard cause get_maintainer.pl to miss
drivers/spi/spi-dw-g2.h? Using drivers/spi/spi-dw-g2*.[ch] would match
both the header and the .c files.
> diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
> new file mode 100644
> index 0000000000000..7b0928f671033
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-core.c
[ ... ]
> +static void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws)
> +{
> + char name[32];
> +
> + snprintf(name, sizeof(name), "dw_spi_g2.%d", dws->ctlr->bus_num);
> +
> + dws->debugfs = debugfs_create_dir(name, NULL);
> + if (!dws->debugfs)
> + return;
> +
> + dws->regset.base = dws->regs;
> + dws->regset.regs = dw_spi_g2_dbg_regs;
> + dws->regset.nregs = ARRAY_SIZE(dw_spi_g2_dbg_regs);
> +
> + debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
> +}
[Severity: Medium]
Does calling dw_spi_g2_debugfs_init() from dw_spi_g2_init() run before
spi_register_controller() assigns a dynamic bus_num?
When a Device Tree node has no spiN alias, dws->ctlr->bus_num is still -1
during dw_spi_g2_init(), so this creates /sys/kernel/debug/dw_spi_g2.-1
and collides if a second controller is present. In addition,
debugfs_create_dir() returns an ERR_PTR() when debugfs is disabled or
fails, so checking !dws->debugfs does not catch errors (though
debugfs_create_regset32() handles an ERR_PTR() parent).
[ ... ]
> +static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)
> +{
[ ... ]
> + 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() (and dw_spi_g2_update_config()) also set
DW_SPI_G2_CTRLR0_SCPOL and DW_SPI_G2_CTRLR0_SCPHA from spi->mode?
dw_spi_g2_init() advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but
neither function programs the polarity or phase bits into CTRLR0, forcing
all transfers to run in SPI Mode 0 and breaking communication with Mode
1, 2, or 3 devices.
[ ... ]
> +static int dw_spi_g2_xfer_fifo(struct dw_spi_g2 *dws,
> + const u8 *tx_buf, u32 tx_len,
> + u8 *rx_buf, u32 rx_len)
> +{
[ ... ]
> + if (!rx_buf || !rx_len) {
> + dw_spi_g2_wait_idle(dws);
> + return 0;
> + }
> +
> + /* RX phase */
> + timeout = jiffies + msecs_to_jiffies(1000);
> + while (rx_got < rx_len) {
> + sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> + if (sr & DW_SPI_G2_SR_RFNE) {
> + rx_buf[rx_got++] = dw_spi_g2_readl(dws->regs,
> + DW_SPI_G2_DR0) &
> + 0xff;
> + timeout = jiffies + msecs_to_jiffies(1000);
> + } else if (time_after(jiffies, timeout)) {
> + dev_err(dws->dev,
> + "xfer_fifo: RX timeout after %u/%u bytes (SR=0x%08x RXFLR=%u)\n",
> + rx_got, rx_len,
> + dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR),
> + dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR));
> + return -ETIMEDOUT;
> + }
> + cpu_relax();
> + }
> +
> + dw_spi_g2_wait_idle(dws);
> + return 0;
> +}
[Severity: Medium]
Can ignoring the return value of dw_spi_g2_wait_idle() here report false
success when a transfer times out, and can dw_spi_g2_wait_idle() return
before the TX FIFO has started shifting?
dw_spi_g2_wait_idle() in drivers/spi/spi-dw-g2.h only polls
!(sr & DW_SPI_G2_SR_BUSY) without checking (sr & DW_SPI_G2_SR_TFE).
Because SR_BUSY is driven by the serial shifter in the ssi_clk domain,
reading SR immediately after writing DR0 can observe SR_BUSY == 0 while
bytes are still queued in the TX FIFO (SR_TFE == 0), causing the caller's
dw_spi_g2_transfer_end() to clear SSIENR and abort the transfer early.
Also, if dw_spi_g2_wait_idle() times out, dw_spi_g2_xfer_fifo() discards
the -ETIMEDOUT error and returns 0.
[ ... ]
> +/**
> + * Command-only operations (WREN, WRDI, Chip Erase, Sector Erase, etc.)
> + * TMOD=1 (TX-Only) — Send opcode + optional address, no data phase
> + */
[ ... ]
> +/**
> + * Indirect read operations
> + */
> +
> +/*
> + * ========================================================================
> + * SPI-MEM interface
> + * ========================================================================
> + */
> +
> +static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
> + const struct spi_mem_op *op)
> +{
> + return spi_mem_default_supports_op(mem, op);
> +}
[Severity: Low]
The two section comments above ("Command-only operations..." and
"Indirect read operations") start with /** instead of /*, which triggers
scripts/kernel-doc warnings under W=1 builds.
[Severity: High]
Does dw_spi_g2_supports_mem_ops() accept operations that
dw_spi_g2_reg_op() and dw_spi_g2_cmd_only() cannot execute as requested?
Because ctlr->mode_bits advertises Dual, Quad, and Octal modes,
spi_mem_default_supports_op() accepts multi-wire operations, operations
with dummy cycles, and 2-byte opcodes. However, dw_spi_g2_cmd_only_setup()
and dw_spi_g2_reg_op_setup() hardcode DW_SPI_G2_FRF_STANDARD (1-1-1 mode)
and write 0 to DW_SPI_G2_SPI_CTRLR0, and dw_spi_g2_reg_op() only sends a
1-byte opcode (tx_buf[0] = op->cmd.opcode) and omits op->dummy.nbytes
entirely, resulting in malformed flash transactions on the wire. Even
after commit "spi: dw-g2: Add SPI-NAND support" and commit "spi: dw-g2:
Add indirect read and write", dw_spi_g2_is_reg_op() routes all transfers
up to 256 bytes into dw_spi_g2_reg_op() in 1-1-1 mode with 1-byte opcodes,
and TX register/command ops still drop dummy cycles.
[ ... ]
> +void dw_spi_g2_update_config(struct dw_spi_g2 *dws, struct spi_device *spi,
> + struct dw_spi_g2_cfg *cfg)
> +{
> + u32 ctrlr0 = 0;
> + u32 div;
> +
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, cfg->dfs - 1);
> + ctrlr0 |= FIELD_PREP(DW_SPI_G2_CTRLR0_TMOD_MASK, cfg->tmode);
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(dws->cs), ctrlr0);
> +
> + if (cfg->tmode == DW_SPI_G2_CTRLR0_TMOD_RO)
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR1(dws->cs), cfg->ndf - 1);
> +
> + div = DIV_ROUND_UP(dws->max_freq, cfg->freq);
> + if (div & 1)
> + div++;
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_BAUDR(dws->cs), div);
> +}
> +
[ ... ]
> +int dw_spi_g2_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
> + struct spi_transfer *xfer)
> +{
[ ... ]
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_update_config(dws, spi, &cfg);
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, dws->fifo_len / 2);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, (dws->fifo_len / 2) - 1);
> +
> + dw_spi_g2_enable(dws, true);
> +
> + local_irq_disable();
> + /* Polling-based full-duplex transfer */
> + while (dws->tx_len || dws->rx_len) {
> + sr = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SR);
> +
> + /* Transmit if FIFO not full and bytes remaining */
> + if (dws->tx_len && (sr & DW_SPI_G2_SR_TFNF)) {
> + u8 val = 0;
> +
> + if (dws->tx) {
> + val = *(u8 *)dws->tx;
> + dws->tx += 1;
> + }
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, val);
> + dws->tx_len--;
> + }
> +
> + /* Receive if FIFO not empty and bytes expected */
> + if (dws->rx_len && (sr & DW_SPI_G2_SR_RFNE)) {
> + u8 val = dw_spi_g2_readl(dws->regs, DW_SPI_G2_DR0);
> +
> + if (dws->rx) {
> + *(u8 *)dws->rx = val;
> + dws->rx += 1;
> + }
> + dws->rx_len--;
> + }
> + cpu_relax();
> + }
> + local_irq_enable();
[Severity: Medium]
Should dw_spi_g2_transfer_one() and dw_spi_g2_update_config() be removed
since ctlr->transfer_one is never wired up and neither symbol is exported?
If dw_spi_g2_transfer_one() is ever called, it never enables the target CS
block or asserts DW_SPI_G2_SER, so the hardware never shifts out the TX
FIFO and the unbounded loop under local_irq_disable() will hard-lock the
CPU waiting for DW_SPI_G2_SR_RFNE. In addition, dw_spi_g2_update_config()
writes per-CS registers without disabling the target CS block and divides
by cfg->freq without guarding against 0.
> diff --git a/drivers/spi/spi-dw-g2-mmio.c b/drivers/spi/spi-dw-g2-mmio.c
> new file mode 100644
> index 0000000000000..6ef8f926e0eb9
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2-mmio.c
> @@ -0,0 +1,196 @@
[ ... ]
> +static int dw_spi_g2_mmio_probe(struct platform_device *pdev)
> +{
[ ... ]
> + /*
> + * IRQ.
> + *
> + * 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. We accept both DT forms:
> + *
> + * - Named-line DT: one entry per output, with interrupt-names
> + * in {"rxf","txe","txaf","wsf"}. Each line is requested
> + * and bound to the shared dw_spi_g2_irq() decoder.
> + * - Single-line DT: combined ssi_intr at index 0, used by
> + * integrations that OR the sources in HW.
> + *
> + * Either way dws->irq holds the "primary" line (rxf when
> + * named, index 0 otherwise) so debugfs and PM paths stay
> + * source-agnostic.
> + */
> + dws->irq = platform_get_irq_byname_optional(pdev, "rxf");
> + if (dws->irq < 0)
> + dws->irq = platform_get_irq(pdev, 0);
> + if (dws->irq < 0) {
> + ret = dws->irq;
> + goto err_put;
> + }
[Severity: Medium]
Where are the "txe", "txaf", and "wsf" interrupt lines requested when
named interrupts are used in Device Tree?
Both the commit message and the comment above state that each of {"rxf",
"txe", "txaf", "wsf"} is requested and bound to dw_spi_g2_irq(), but
dw_spi_g2_mmio_probe() only queries "rxf" (falling back to index 0 even if
"rxf" returned -EPROBE_DEFER) and calls devm_request_irq() once for
dws->irq.
[ ... ]
> + reset_control_deassert(dwsmmio->rstc);
> +
> + /* Read num-cs from device tree */
> + ret = device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
> + if (ret) {
> + dev_err(&pdev->dev, "Failed to read 'num-cs' property: %d\n", ret);
> + goto err_reset;
> + }
> + ctlr->num_chipselect = num_cs;
> +
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_SER, 0);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_DEDICATED_CS_EN,
> + GENMASK(ctlr->num_chipselect - 1, 0));
> + dw_spi_g2_enable(dws, false);
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_IMR, 0);
> + /* Clear all interrupts by writing 1s to defined ICR bits */
> + dw_spi_g2_writel(dws->regs, DW_SPI_G2_ICR, DW_SPI_G2_ICR_ALL);
> +
> + /*
> + * Latch the static controller identity once. VERSION_ID and
> + * HW_CAPABILITIES are read-only and never change at runtime.
> + */
> + dws->version_id = dw_spi_g2_readl(dws->regs, DW_SPI_G2_VERSION_ID);
> + dws->hw_caps = dw_spi_g2_readl(dws->regs, DW_SPI_G2_HW_CAPABILITIES);
> + dws->caps = dws->hw_caps;
[Severity: Medium]
Can an omitted or out-of-range num-cs property break probe or trigger
undefined shift behavior in GENMASK(ctlr->num_chipselect - 1, 0)?
In Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml, num-cs
is optional with a valid range of 1..4, so probe should default num-cs
when absent and reject values outside 1..4 before evaluating GENMASK() or
indexing per-CS register blocks. Also, the return value of
reset_control_deassert() is not checked before accessing MMIO registers,
and the writes to DW_SPI_G2_DEDICATED_CS_EN and dws->caps = dws->hw_caps
here are immediately overwritten with 0 by dw_spi_g2_init() below.
[ ... ]
> + /* core init */
> + ret = dw_spi_g2_init(dws);
> + if (ret)
> + goto err_reset;
> +
> + ret = devm_request_irq(&pdev->dev, dws->irq, dw_spi_g2_irq, 0,
> + dev_name(&pdev->dev), dws);
> + if (ret)
> + goto err_reset;
> +
> + pm_runtime_enable(&pdev->dev);
> +
> + ret = spi_register_controller(ctlr);
> + if (ret)
> + goto err_pm;
> +
> + dev_info(&pdev->dev, "DW SSI G2 MMIO probed\n");
> + return 0;
> +
> +err_pm:
> + pm_runtime_disable(&pdev->dev);
> +err_reset:
> + reset_control_assert(dwsmmio->rstc);
> +err_put:
> + spi_controller_put(ctlr);
> + return ret;
> +}
[Severity: Medium]
Does failing at devm_request_irq() or spi_register_controller() leak the
debugfs directory created by dw_spi_g2_init() and leave a dangling
&dws->regset pointer after spi_controller_put(ctlr) frees ctlr?
Because err_pm and err_reset do not call dw_spi_g2_deinit(dws),
/sys/kernel/debug/dw_spi_g2.<bus_num>/registers remains registered
pointing to freed memory, causing a use-after-free if read.
[Severity: Medium]
Can dw_spi_g2_irq() fire after reset_control_assert() and
spi_controller_put(ctlr) on the err_pm error path (or after
reset_control_assert() in dw_spi_g2_mmio_remove())?
dws->irq is requested with devm_request_irq(), which is not freed until
after probe() or remove() returns, while ctlr (containing dws) is
allocated with the non-devm spi_alloc_host() and freed immediately at
err_put, and dwsmmio->rstc is asserted before devres releases the IRQ.
> +
> +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);
> +
> + spi_unregister_controller(ctlr);
> + dw_spi_g2_deinit(&dwsmmio->dws);
> + pm_runtime_disable(&pdev->dev);
> + reset_control_assert(dwsmmio->rstc);
> +}
[Severity: Medium]
Does dw_spi_g2_mmio_remove() leak ctlr and dwsmmio?
ctlr is allocated with spi_alloc_host() rather than devm_spi_alloc_host(),
and spi_unregister_controller() only drops the registration reference from
device_add(), so the initial allocation reference is never released
without a matching spi_controller_put(ctlr) (or switching probe to
devm_spi_alloc_host()).
> diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
> new file mode 100644
> index 0000000000000..1106be0af7b65
> --- /dev/null
> +++ b/drivers/spi/spi-dw-g2.h
> @@ -0,0 +1,396 @@
> +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
> +
> +#ifndef _SPI_DW_G2_H
> +#define _SPI_DW_G2_H
> +
> +#include <linux/spi/spi.h>
> +#include <linux/io.h>
> +#include <linux/device.h>
> +#include <linux/irqreturn.h>
> +#include <linux/iopoll.h>
> +#include <linux/spi/spi-mem.h>
[Severity: Medium]
Does drivers/spi/spi-dw-g2-core.c fail to build at this commit on
architectures that do not transitively include linux/bitfield.h?
dw_spi_g2_build_ctrlr0() and dw_spi_g2_update_config() use FIELD_PREP(),
but <linux/bitfield.h> is only added to drivers/spi/spi-dw-g2.h later in
commit "spi: dw-g2: Add XIP support", breaking bisection on architectures
such as x86_64 and riscv.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119100513.433044-1-prakriti@synopsys.com?part=2
next prev parent reply other threads:[~2026-11-19 10:28 UTC|newest]
Thread overview: 11+ 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 [this message]
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
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-222953@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