From: Prakriti Sharma <Prakriti.Sharma@synopsys.com>
To: Mark Brown <broonie@kernel.org>, linux-spi@vger.kernel.org
Cc: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
devicetree@vger.kernel.org,
Philipp Zabel <p.zabel@pengutronix.de>,
linux-kernel@vger.kernel.org,
Siddhant Kumar <Siddhant.Kumar@synopsys.com>,
Jose Abreu <Jose.Abreu@synopsys.com>,
Nelson Costa <Nelson.Costa@synopsys.com>,
Prakriti Sharma <Prakriti.Sharma@synopsys.com>
Subject: [PATCH v2 3/5] spi: dw-g2: Add support for indirect read and write for spi-mem transfers
Date: Mon, 30 Nov 2026 08:09:37 +0530 [thread overview]
Message-ID: <ce428fca677733983f1581b0afaf6a73c2b335ea.1795683279.git.prakriti@synopsys.com> (raw)
In-Reply-To: <cover.1795683279.git.prakriti@synopsys.com>
Enable the FIFO indirect path to handle spi-mem data transfers for the
DesignWare SPI G2 controller. This allows the driver to support bulk
memory operations required by flash devices using the spi-mem interface.
Previously, only register-based operations were supported, which limited
compatibility with spi-mem users that require direct data reads or writes.
With this change, read and write operations using the controller's FIFO
indirect path are properly handled, improving compatibility and performance
for memory operations.
Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
drivers/spi/spi-dw-g2-core.c | 389 ++++++++++++++++++++++++++++++++++-
drivers/spi/spi-dw-g2.h | 8 +
2 files changed, 396 insertions(+), 1 deletion(-)
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index b2a3e3e8670e..88766706ef52 100644
--- a/drivers/spi/spi-dw-g2-core.c
+++ b/drivers/spi/spi-dw-g2-core.c
@@ -119,6 +119,43 @@ int dw_spi_g2_setup(struct spi_device *spi)
return 0;
}
+static void dw_spi_g2_apply_sampling_fields(u32 *ctrlr0, u32 *spictrlr0,
+ enum dw_spi_g2_sampling_mode mode)
+{
+ switch (mode) {
+ case DW_SPI_G2_SAMPLING_RX_DELAY:
+ *ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN;
+ *spictrlr0 &= ~DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ break;
+ case DW_SPI_G2_SAMPLING_DATA_STROBE:
+ *ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN;
+ *spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+ break;
+ }
+}
+
+/**
+ * dw_spi_g2_set_sampling_mode - Configure RX sampling mode
+ * @dws: Driver state
+ * @cs: Chip select
+ * @mode: Sampling mode to enable
+ *
+ * RX_DELAY uses RX_SAMPLE_DELAY. DATA_STROBE uses the flash DQS/RXDS
+ * input for DTR transfers. Must be called with SSI disabled.
+ */
+void dw_spi_g2_set_sampling_mode(struct dw_spi_g2 *dws, u8 cs,
+ enum dw_spi_g2_sampling_mode mode)
+{
+ u32 ctrlr0, spictrlr0;
+
+ ctrlr0 = dw_spi_g2_readl(dws->regs, DW_SPI_G2_CTRLR0(cs));
+ spictrlr0 = dw_spi_g2_readl(dws->regs, DW_SPI_G2_SPI_CTRLR0(cs));
+ dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0, mode);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_CTRLR0(cs), ctrlr0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(cs), spictrlr0);
+}
+
/**
* dw_spi_g2_build_ctrlr0 - Build CTRLR0 register value
* @dws: Driver state
@@ -199,6 +236,63 @@ static void dw_spi_g2_controller_reset(struct dw_spi_g2 *dws)
}
}
+/**
+ * dw_spi_g2_get_frf - Determine frame format from operation bus widths
+ * @op: SPI memory operation
+ *
+ * Examines the command, address, and data bus widths to determine the
+ * widest mode in use, which defines the frame format.
+ *
+ * Return: Frame format
+ */
+static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)
+{
+ /* Use the widest bus mode across cmd/addr/data phases */
+ u32 width = max3(op->cmd.buswidth,
+ op->addr.buswidth ? op->addr.buswidth : 1,
+ op->data.buswidth ? op->data.buswidth : 1);
+
+ switch (width) {
+ case 2:
+ return DW_SPI_G2_FRF_DUAL;
+ case 4:
+ return DW_SPI_G2_FRF_QUAD;
+ case 8:
+ return DW_SPI_G2_FRF_OCTAL;
+ default:
+ return DW_SPI_G2_FRF_STANDARD;
+ }
+}
+
+/**
+ * dw_spi_g2_get_tt - Determine transfer type from operation bus widths
+ * @op: SPI memory operation
+ *
+ * TRANS_TYPE in SPI_CTRLR0 / XIP_CTRL controls only the instruction and
+ * address phases. Data always uses the enhanced mode set by SPI_FRF in
+ * CTRLR0 (or FRF in XIP_CTRL).
+ *
+ * TT0 (0x0): instruction standard, address standard (1-1-x)
+ * TT1 (0x1): instruction standard, address enhanced (1-x-x)
+ * TT2 (0x2): instruction enhanced, address enhanced (x-x-x)
+ *
+ * Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns
+ */
+static int dw_spi_g2_get_tt(const struct spi_mem_op *op)
+{
+ bool cmd_m = (op->cmd.buswidth > 1);
+ bool addr_m = (op->addr.nbytes && op->addr.buswidth > 1);
+
+ if (!cmd_m && !addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT0; /* 1-1-x */
+ if (!cmd_m && addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT1; /* 1-x-x */
+ if (cmd_m && addr_m)
+ return DW_SPI_G2_TRANS_TYPE_TT2; /* x-x-x */
+
+ return -EOPNOTSUPP;
+}
+
/**
* dw_spi_g2_op_is_std_spi - True if @op is single-line SDR SPI
* @op: SPI memory operation
@@ -664,6 +758,294 @@ void dw_spi_g2_program_baudr(struct dw_spi_g2 *dws, u32 freq)
dw_spi_g2_baudr(dws, freq));
}
+/**
+ * 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)) & 0xFFFF;
+ if (!baud)
+ baud = 1;
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs),
+ (baud <= 2) ? 1 : 0);
+}
+
+/**
+ * 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));
+ u32 spictrlr0 = 0;
+ u32 dummy_cycles = 0;
+ int tt;
+
+ if (op->dummy.nbytes) {
+ if (!op->dummy.buswidth || op->dummy.buswidth > 8) {
+ dev_err(dws->dev, "Invalid dummy buswidth %u\n",
+ op->dummy.buswidth);
+ return -EINVAL;
+ }
+ dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth;
+ /*
+ * In DTR mode two bits are clocked per cycle, so the dummy
+ * phase needs half as many controller cycles as in SDR mode.
+ */
+ if (op->data.dtr && dummy_cycles)
+ dummy_cycles /= 2;
+ if (dummy_cycles > DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX) {
+ dev_err(dws->dev, "Dummy cycles %u exceed maximum %u\n",
+ dummy_cycles, DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX);
+ return -EINVAL;
+ }
+ }
+
+ tt = dw_spi_g2_get_tt(op);
+ if (tt < 0) {
+ dev_err(dws->dev, "Unsupported transfer type: cmd=%u addr=%u data=%u\n",
+ op->cmd.buswidth,
+ op->addr.nbytes ? op->addr.buswidth : 0,
+ op->data.nbytes ? op->data.buswidth : 0);
+ return tt;
+ }
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_TRANS_TYPE_MASK, tt);
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_INST_L_MASK,
+ dw_spi_g2_inst_len(op->cmd.nbytes));
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK,
+ dw_spi_g2_addr_len(op->addr.nbytes));
+ spictrlr0 |= FIELD_PREP(DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLE_MASK,
+ dummy_cycles);
+
+ if (op->cmd.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
+ if (op->addr.dtr || op->data.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK;
+
+ /*
+ * DTR mode with data strobe: set RXDS_EN and enforce mutual
+ * exclusivity with the RX-delay sampling mode.
+ */
+ if (op->data.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+
+ if (op->data.dtr)
+ dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ else
+ dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+ 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),
+ op->data.nbytes - 1);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
+
+ /* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */
+ dw_spi_g2_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR,
+ min_t(u32, (dws->fifo_len / 2) - 1,
+ op->data.nbytes ? op->data.nbytes : 1));
+ return 0;
+}
+
+/**
+ * dw_spi_g2_indirect_read - Execute indirect read operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Reads data from flash using RO (receive-only) mode with enhanced SPI
+ * framing via SPI_CTRLR0.
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u8 dummy_buf = 0;
+ int ret;
+
+ if (!op->data.nbytes) {
+ dev_err(dws->dev, "Zero-length read not allowed\n");
+ return -EINVAL;
+ }
+
+ if (!op->data.buf.in) {
+ dev_err(dws->dev, "RX buffer is NULL\n");
+ return -EINVAL;
+ }
+
+ /*
+ * One CS assertion per op. Larger reads are clamped in
+ * adjust_mem_op_size() to fifo_len so spi-mem / spinand advance
+ * the address themselves (required for SPI-NAND continuous read).
+ */
+ if (op->data.nbytes > dws->fifo_len) {
+ dev_err(dws->dev,
+ "indirect_read: len %u exceeds FIFO %u (adjust_op_size bug?)\n",
+ op->data.nbytes, dws->fifo_len);
+ return -EINVAL;
+ }
+
+ dev_dbg(dws->dev, "indirect_read: cmd=0x%02x addr=0x%llx len=%u\n",
+ op->cmd.opcode, op->addr.val, op->data.nbytes);
+
+ ret = dw_spi_g2_indirect_read_setup(dws, op);
+ if (ret)
+ return ret;
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
+ if (op->addr.nbytes)
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
+
+ dw_spi_g2_cs_assert(dws, dws->cs);
+
+ ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
+ op->data.buf.in, op->data.nbytes);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+
+ return ret;
+}
+
+/**
+ * dw_spi_g2_indirect_write_setup - Configure controller for indirect write
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Sets up TMOD=TO (transmit-only)
+ */
+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);
+ u32 spictrlr0 = 0;
+
+ dw_spi_g2_controller_reset(dws);
+ dw_spi_g2_target_disable(dws, dws->cs);
+
+ if (op->cmd.dtr)
+ spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK;
+ 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_apply_sampling_fields(&ctrlr0, &spictrlr0,
+ DW_SPI_G2_SAMPLING_DATA_STROBE);
+ else
+ dw_spi_g2_apply_sampling_fields(&ctrlr0, &spictrlr0,
+ 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);
+
+ /* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */
+ dw_spi_g2_program_baudr(dws, dw_spi_g2_op_freq(dws, op));
+ dw_spi_g2_update_ddr_drive_edge(dws, op);
+
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_TXFTLR, 0);
+ dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, 0);
+}
+
+/**
+ * dw_spi_g2_indirect_write - Execute indirect write operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Writes data to flash using TMOD=TO (transmit-only).
+ * CS is asserted before SSI enable so Program Load sees data as soon as
+ * the shifter starts. Enabling SSI with an empty FIFO first can clock out
+ * a spurious 0x00 before the payload is queued.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,
+ const struct spi_mem_op *op)
+{
+ u32 tx_total = op->cmd.nbytes + op->addr.nbytes + op->data.nbytes;
+ u8 *tx_buf;
+ int ret = 0;
+
+ if (!op->data.nbytes) {
+ dev_err(dws->dev, "Zero-length write not allowed\n");
+ return -EINVAL;
+ }
+
+ if (!op->data.buf.out) {
+ dev_err(dws->dev, "TX buffer is NULL\n");
+ return -EINVAL;
+ }
+
+ dev_dbg(dws->dev, "indirect_write: cmd=0x%02x addr=0x%llx len=%u\n",
+ op->cmd.opcode, op->addr.val, op->data.nbytes);
+
+ dw_spi_g2_indirect_write_setup(dws, op);
+
+ tx_buf = kzalloc(tx_total, GFP_KERNEL);
+ if (!tx_buf)
+ return -ENOMEM;
+
+ dw_spi_g2_fill_tx_buf(tx_buf, op, true);
+
+ dw_spi_g2_target_enable(dws, dws->cs);
+ dw_spi_g2_cs_assert(dws, dws->cs);
+ dw_spi_g2_enable(dws, true);
+
+ ret = dw_spi_g2_xfer_fifo(dws, tx_buf, tx_total, NULL, 0);
+ if (ret) {
+ dev_err(dws->dev,
+ "indirect_write: xfer_fifo failed: %d\n", ret);
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+ kfree(tx_buf);
+ return ret;
+ }
+
+ dw_spi_g2_transfer_end(dws);
+ dw_spi_g2_controller_reset(dws);
+
+ kfree(tx_buf);
+ return ret;
+}
+
static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,
const struct spi_mem_op *op)
{
@@ -720,7 +1102,12 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,
if (dw_spi_g2_is_reg_op(dws, op))
return dw_spi_g2_reg_op(dws, op);
- /* Bulk transfers arrive in the indirect-read/write patch. */
+ 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;
}
diff --git a/drivers/spi/spi-dw-g2.h b/drivers/spi/spi-dw-g2.h
index ab1319ec0564..4063fe2bbc30 100644
--- a/drivers/spi/spi-dw-g2.h
+++ b/drivers/spi/spi-dw-g2.h
@@ -97,6 +97,10 @@
#define DW_SPI_G2_CTRLR0_CLK_LOOP_EN BIT(18)
/* RX sampling modes (mutually exclusive) */
+enum dw_spi_g2_sampling_mode {
+ DW_SPI_G2_SAMPLING_RX_DELAY, /* RX_SAMPLE_DELAY register */
+ DW_SPI_G2_SAMPLING_DATA_STROBE, /* RXDS/DQS from flash (DTR mode) */
+};
/* CTRLR0 fields */
#define DW_SPI_G2_CTRLR0_DFS_MASK GENMASK(4, 0)
@@ -385,4 +389,8 @@ static inline void dw_spi_g2_transfer_end(struct dw_spi_g2 *dws)
dw_spi_g2_target_disable(dws, dws->cs);
}
+/* Sampling mode configuration */
+void dw_spi_g2_set_sampling_mode(struct dw_spi_g2 *dws, u8 cs,
+ enum dw_spi_g2_sampling_mode mode);
+
#endif /* __SPI_DW_G2_H__ */
--
2.34.1
next prev parent reply other threads:[~2026-11-30 2:40 UTC|newest]
Thread overview: 6+ 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:39 ` [PATCH v2 2/5] spi: dw-g2: Add support for Synopsys DWC SSI v2.02a controller Prakriti Sharma
2026-11-30 2:39 ` Prakriti Sharma [this message]
2026-11-30 2:39 ` [PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices Prakriti Sharma
2026-11-30 2:39 ` [PATCH v2 5/5] spi: dw-g2: Add support for memory-mapped XIP 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=ce428fca677733983f1581b0afaf6a73c2b335ea.1795683279.git.prakriti@synopsys.com \
--to=prakriti.sharma@synopsys.com \
--cc=Jose.Abreu@synopsys.com \
--cc=Nelson.Costa@synopsys.com \
--cc=Siddhant.Kumar@synopsys.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=p.zabel@pengutronix.de \
--cc=robh@kernel.org \
/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
all inboxes | Powered by JetHome®