[PATCH v11 06/26] mtd: rawnand: sunxi: use page reads to reposition small-page NAND

From: James Hilliard

Date: Fri Nov 20 2026 - 20:11:30 EST


On a chip with 512-byte pages, an ECC read can fetch bytes from the
wrong column. The driver uses nand_change_read_column_op() to move
between main data and OOB and to reread a possibly erased chunk. That
helper rejects small pages because it uses large-page random-output
commands. Some callers then continue without repositioning the chip.

Use nand_read_page_op() to select the requested small-page column.
The NAND core chooses READ0, READ1 or READOOB and sends the page
address again. Keep the random-column command on larger pages.

Use PIO for full-page ECC reads and writes on small pages: the DMA
sequencer uses large-page random-column commands. Sequential small-page
writes need no change-column command.

This fixes the command sequence for an otherwise supported layout. On
8-bit small-page NAND the factory marker at byte 5 still lies outside
the reserved marker bytes. On 16-bit NAND the core uses byte 0, but
the driver does not program the controller for a 16-bit bus.

Fixes: 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/r/20260913041642.E28F81F000FF@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 36 ++++++++++++++++++++++++------------
1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index 086618dee999..9d7e3e6f22b2 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -1028,6 +1028,16 @@ static void sunxi_nfc_hw_ecc_update_stats(struct nand_chip *nand,
}
}

+static int sunxi_nfc_read_column(struct nand_chip *nand, int page,
+ unsigned int column, void *buf, unsigned int len)
+{
+ /* Small-page NAND needs a new READ0/READ1/READOOB and the page address. */
+ if (nand_to_mtd(nand)->writesize <= 512)
+ return nand_read_page_op(nand, page, column, buf, len);
+
+ return nand_change_read_column_op(nand, column, buf, len, false);
+}
+
/* Shared across a read's transfers; buf remains the full-page buffer base. */
struct sunxi_nfc_ecc_read {
u8 *buf;
@@ -1111,10 +1121,10 @@ static int sunxi_nfc_hw_ecc_read_error(struct nand_chip *nand,

/* Check the physical representation for bitflips in erased pages. */
if (nand->options & NAND_NEED_SCRAMBLING)
- nand_change_read_column_op(nand, data_off, data, ecc->size, false);
+ sunxi_nfc_read_column(nand, read->page, data_off, data, ecc->size);

- nand_change_read_column_op(nand, nand_to_mtd(nand)->writesize + oob_off,
- oob, oob_len, false);
+ sunxi_nfc_read_column(nand, read->page, nand_to_mtd(nand)->writesize + oob_off,
+ oob, oob_len);

ret = nand_check_erased_ecc_chunk(data, ecc->size, oob, oob_len, NULL, 0,
ecc->strength);
@@ -1141,8 +1151,8 @@ static void sunxi_nfc_hw_ecc_read_extra_oob(struct nand_chip *nand,
return;

if (!cur_off || *cur_off != (offset + mtd->writesize))
- nand_change_read_column_op(nand, mtd->writesize + offset,
- NULL, 0, false);
+ sunxi_nfc_read_column(nand, page, mtd->writesize + offset,
+ NULL, 0);

if (!randomize)
sunxi_nfc_read_buf(nand, oob + offset, len);
@@ -1200,11 +1210,12 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
if (read->oob_required) {
/* Keep the existing PIO and DMA representations of parity. */
if (read->dma) {
- nand_change_read_column_op(nand, mtd->writesize + oob_off,
- oob, ecc->bytes + user_data_sz, false);
+ sunxi_nfc_read_column(nand, read->page,
+ mtd->writesize + oob_off,
+ oob, ecc->bytes + user_data_sz);
} else {
- nand_change_read_column_op(nand, mtd->writesize + oob_off,
- NULL, 0, false);
+ sunxi_nfc_read_column(nand, read->page,
+ mtd->writesize + oob_off, NULL, 0);
sunxi_nfc_randomizer_read_buf(nand, oob,
ecc->bytes + user_data_sz,
true, read->page);
@@ -1260,12 +1271,12 @@ static int sunxi_nfc_hw_ecc_read_chunk(struct nand_chip *nand,
int ret;

if (*cur_off != data_off)
- nand_change_read_column_op(nand, data_off, NULL, 0, false);
+ sunxi_nfc_read_column(nand, page, data_off, NULL, 0);

sunxi_nfc_randomizer_read_buf(nand, NULL, ecc->size, false, page);

if (data_off + ecc->size != oob_off)
- nand_change_read_column_op(nand, oob_off, NULL, 0, false);
+ sunxi_nfc_read_column(nand, page, oob_off, NULL, 0);

ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
if (ret)
@@ -2105,7 +2116,8 @@ static int sunxi_nand_hw_ecc_ctrl_init(struct nand_chip *nand,
ecc->write_oob = sunxi_nfc_hw_ecc_write_oob;
mtd_set_ooblayout(mtd, &sunxi_nand_ooblayout_ops);

- if (nfc->dmac || nfc->use_mdma) {
+ /* The DMA page sequencer uses large-page random-column commands. */
+ if (mtd->writesize > 512 && (nfc->dmac || nfc->use_mdma)) {
ecc->read_page = sunxi_nfc_hw_ecc_read_page_dma;
ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage_dma;
ecc->write_page = sunxi_nfc_hw_ecc_write_page_dma;

--
2.53.0