Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] spi: airoha-snfi: strip plane select bit from dirmap offs
@ 2026-11-30 16:27 Vikrant Yadav
  0 siblings, 0 replies; only message in thread
From: Vikrant Yadav @ 2026-11-30 16:27 UTC (permalink / raw)
  To: lorenzo, ray.liu
  Cc: broonie, mikhail.kshevet, linux-arm-kernel, linux-spi,
	Vikrant Yadav

When a SPI NAND chip sets SPINAND_HAS_READ_PLANE_SELECT_BIT or
SPINAND_HAS_PROG_PLANE_SELECT_BIT (e.g. Macronix MX35LF2GE4AB), the
SPI NAND core ORs the plane into the column address it passes to the
dirmap callbacks as offs:

    offs = (plane << fls(page_size)) | column

For a 2048-byte page, plane 1 sets bit 12, so offs is 0x1000 for a
data or data+OOB access and 0x1800 for an OOB-only read.

airoha_snand_dirmap_read() and airoha_snand_dirmap_write() use offs as
a plain byte offset into txrx_buf (SPI_NAND_CACHE_SIZE, 4352 bytes) and
to size the transfer. With the plane bit left in, on plane 1:

 - bytes = round_up(offs + len, 64) is inflated (6208 instead of 2112
   for an OOB-only read), so the NFI is programmed with an oversized
   transfer length.
 - read: memcpy(buf, txrx_buf + offs, len) copies from past the end of
   txrx_buf.
 - write: memset()/memcpy() on txrx_buf + offs write past the end of
   txrx_buf, corrupting memory.

The plane is already selected through desc->info.offset (set at dirmap
creation as plane << fls(page_size)), which the driver writes to the
hardware address registers (REG_SPI_NFI_RD_CTL3 for read,
REG_SPI_NFI_PG_CTL2 for write). Only the use of offs as a buffer index
and length is wrong.

Clear the plane bit from offs at the start of both functions:

    offs &= ~desc->info.offset;

info.offset contains only the plane bit, so the column is unchanged.
This is a no-op for plane 0 and for chips without the plane select
flags, and works whether the chip sets the read flag, the program flag
or both.

Fixes: 902c0ea18a97 ("spi: airoha: avoid reading flash page settings from SNFI registers during driver startup")
Signed-off-by: Vikrant Yadav <vikrantyadav4802@gmail.com>
---
 drivers/spi/spi-airoha-snfi.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/spi/spi-airoha-snfi.c b/drivers/spi/spi-airoha-snfi.c
index 95bfde7c8e7f..2217df3c3677 100644
--- a/drivers/spi/spi-airoha-snfi.c
+++ b/drivers/spi/spi-airoha-snfi.c
@@ -565,6 +565,9 @@ static ssize_t airoha_snand_dirmap_read(struct spi_mem_dirmap_desc *desc,
 
 	as_ctrl = spi_controller_get_devdata(spi->controller);
 
+	/* offs may carry the plane select bit, keep only the column */
+	offs &= ~desc->info.offset;
+
 	/* minimum oob size is 64 */
 	bytes = round_up(offs + len, 64);
 
@@ -758,6 +761,9 @@ static ssize_t airoha_snand_dirmap_write(struct spi_mem_dirmap_desc *desc,
 
 	as_ctrl = spi_controller_get_devdata(spi->controller);
 
+	/* offs may carry the plane select bit, keep only the column */
+	offs &= ~desc->info.offset;
+
 	/* minimum oob size is 64 */
 	bytes = round_up(offs + len, 64);
 
-- 
2.34.1



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 16:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:27 [PATCH] spi: airoha-snfi: strip plane select bit from dirmap offs Vikrant Yadav

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox