[PATCH v11 17/26] mtd: rawnand: sunxi: preserve bad-block markers on erased OOB reads
From: James Hilliard
Date: Fri Nov 20 2026 - 20:12:57 EST
A bad block can appear good if its marker is the only programmed byte
in an otherwise erased ECC step. If the controller reports that step as
uncorrectable, the erased-page check counts a 0x00 marker as eight
bitflips. With BCH16 it replaces the marker with 0xff in the OOB buffer
that the NAND core uses to check the block.
Leave the reserved marker bytes untouched when checking the physical
first step for erased data. If a hardware pattern supplied the OOB
contents instead, read those marker bytes from flash after the OOB tail.
This preserves the column for sequential reads and leaves the normal
corrected path and data-only read traffic unchanged.
Fixes: 146b503e1027 ("mtd: nand: sunxi: fix bitflips in erased pages")
Fixes: 4be4e03efc7f ("mtd: nand: sunxi: add randomizer support")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index 5415ac6960b4..f8bc6e3c3ac9 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -1111,6 +1111,7 @@ struct sunxi_nfc_ecc_read {
bool oob_required;
bool dma;
bool has_erased_chunk;
+ bool read_raw_bbm;
};
static unsigned int sunxi_nfc_hw_ecc_read_count(struct nand_chip *nand, int slot)
@@ -1167,6 +1168,12 @@ static int sunxi_nfc_hw_ecc_read_error(struct nand_chip *nand,
if (ret)
return ret;
+ /* A factory bad-block marker is not an erased-page bitflip. */
+ if (!step) {
+ oob += SUNXI_NFC_BBM_SIZE;
+ oob_len -= SUNXI_NFC_BBM_SIZE;
+ }
+
ret = nand_check_erased_ecc_chunk(data, ecc->size, oob, oob_len, NULL, 0,
ecc->strength);
@@ -1251,8 +1258,11 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
if (!read->dma || randomized)
memset(data, pattern, ecc->size);
- if (read->oob_required)
+ if (read->oob_required) {
memset(oob, pattern, ecc->bytes + user_data_sz);
+ if (!step)
+ read->read_raw_bbm = true;
+ }
if (pattern == 0xff)
read->has_erased_chunk = true;
continue;
@@ -1321,6 +1331,14 @@ static int sunxi_nfc_oob_finish_read(struct nand_chip *nand,
return ret;
}
+ /* The pattern supplied no marker bytes. Read them after the OOB tail. */
+ if (read->read_raw_bbm) {
+ ret = sunxi_nfc_read_column(nand, read->page, nand_to_mtd(nand)->writesize,
+ nand->oob_poi, SUNXI_NFC_BBM_SIZE);
+ if (ret)
+ return ret;
+ }
+
return read->max_bitflips;
}
--
2.53.0