Skip to contentSkip to conversation

spi: airoha-snfi: strip plane select bit from dirmap offs

PATCH In review Vikrant Yadav 1 participant 2 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: airoha-snfi: strip plane select bit from dirmap offs

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")
---
 drivers/spi/spi-airoha-snfi.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/spi/spi-airoha-snfi.c +6 -0
index 95bfde7c8e7f..2217df3c3677 100644
@@ -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);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 69 lines · 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")
---
 drivers/spi/spi-airoha-snfi.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/spi/spi-airoha-snfi.c +6 -1
index 95bfde7c8e7f..2217df3c3677 100644
@@ -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);
 
--
Quoted 2 lines

2.34.1

Adding Mikhail to Cc. I missed him in the first mail. He wrote the earlier plane fix in this driver.

Thanks,
Vikrant