[PATCH v2 4/5] spi: dw-g2: Add support for SPI-NAND devices
From: Prakriti Sharma
Date: Sun Nov 29 2026 - 21:40:59 EST
Enable SPI-NAND aware sizing and operation routing in the DW-G2 SPI driver.
SPI-NAND devices require precise chunk sizing for continuous reads without
hidden chunking, and operation routing to handle device-specific timing.
These changes allow proper operation with SPI-NAND while preserving
compatibility with other SPI memory devices.
Signed-off-by: Prakriti Sharma <prakriti@xxxxxxxxxxxx>
Signed-off-by: Siddhant Kumar <sidkumar@xxxxxxxxxxxx>
---
drivers/spi/spi-dw-g2-core.c | 66 +++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/drivers/spi/spi-dw-g2-core.c b/drivers/spi/spi-dw-g2-core.c
index 88766706ef52..f50dcfe2d854 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);
+ }
+
if (op->data.dir == SPI_MEM_DATA_IN) {
/* TMOD=TR returns one byte per byte sent */
ret = dw_spi_g2_xfer_fifo(dws, NULL, 0, buf, len);
@@ -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);
}
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);
+
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);
return 0;
}
--
2.34.1