mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>,
	Prakriti Sharma <Prakriti.Sharma@synopsys.com>
Subject: [PATCH 3/6] spi: dw-g2: Add indirect read and write
Date: Thu, 19 Nov 2026 15:35:10 +0530	[thread overview]
Message-ID: <20261119100513.433044-4-prakriti@synopsys.com> (raw)
In-Reply-To: <20261119100513.433044-1-prakriti@synopsys.com>

Add the FIFO indirect path used for spi-mem data transfers that are
not short register operations.

Reads use receive-only mode. Writes use transmit-only mode and send
the command, address, and payload as one continuous FIFO stream.

Signed-off-by: Prakriti Sharma <prakriti@synopsys.com>
Signed-off-by: Siddhant Kumar <sidkumar@synopsys.com>
---
 drivers/spi/spi-dw-g2-core.c | 242 ++++++++++++++++++++++++++++++++++-
 1 file changed, 236 insertions(+), 6 deletions(-)

diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index 7b0928f67103..c640d6a19260 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));
+	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;
+		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);
+
+	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);
+
+	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;
+	if (op->data.dtr)
+		spictrlr0 |= DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK;
+
+	dw_spi_g2_writel(dws->regs, DW_SPI_G2_SPI_CTRLR0(dws->cs), spictrlr0);
+
+	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.
+ * 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)
+{
+	u32 tx_entries;
+	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;
+	}
+
+	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);
+
+	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++;
+	}
+
+	/* Assert CS - controller begins: sends TX FIFO, then receives */
+	dw_spi_g2_cs_assert(dws, dws->cs);
+
+	/* Receive data via polling */
+	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;
+}
+
+/**
+ * Indirect write operations (Page Program)
+ * Uses TMOD=TO (Transmit-Only) mode
+ */
+
+/**
+ * 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);
+}
+
+/**
+ * dw_spi_g2_indirect_write - Execute indirect write operation
+ * @dws: Driver state
+ * @op: SPI memory operation
+ *
+ * Writes data to flash using TO (transmit-only) mode. The controller sends
+ * instruction, address, and data bytes as a continuous stream.
+ *
+ * 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;
+	u32 prefill, pos;
+
+	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;
+	}
+
+	dw_spi_g2_indirect_write_setup(dws, op);
+
+	/* Allocate and build TX buffer: [cmd][addr][data] */
+	tx_buf = kzalloc(tx_total, GFP_KERNEL);
+	if (!tx_buf)
+		return -ENOMEM;
+
+	dw_spi_g2_fill_tx_buf(tx_buf, op, true);
+
+	/*
+	 * Split enable sequence:
+	 * Enable SSI (without CS), pre-fill TX FIFO, then assert CS.
+	 * FIFO pointers are reset while SSIENR=0, so writes before
+	 * enabling would be discarded.
+	 */
+	dw_spi_g2_target_enable(dws, dws->cs);
+	dw_spi_g2_enable(dws, true);
+
+	/* Pre-fill FIFO to capacity or total size */
+	prefill = min_t(u32, tx_total, dws->fifo_len);
+	for (pos = 0; pos < prefill; pos++)
+		dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, tx_buf[pos]);
+
+	/* 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;
+}
+
 /*
  * ========================================================================
  * SPI-MEM interface
@@ -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;
 }
-- 
2.34.1


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

Thread overview: 8+ 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:05 ` Prakriti Sharma [this message]
2026-11-19 10:05 ` [PATCH 4/6] spi: dw-g2: Add SPI-NAND support Prakriti Sharma
2026-11-19 10:05 ` [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties Prakriti Sharma
2026-11-19 12:18   ` Krzysztof Kozlowski
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=20261119100513.433044-4-prakriti@synopsys.com \
    --to=prakriti.sharma@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®