[PATCH v11 25/26] mtd: rawnand: sunxi: reuse ECC status within each transfer

From: James Hilliard

Date: Fri Nov 20 2026 - 20:13:12 EST


The pattern ID register describes all slots in a completed transfer,
but the driver rereads it for each matching step. Four consecutive
hardware slots also share one correction-counter register, which the
driver rereads for each ordinary step in that group.

Take a fresh snapshot for each PIO step or DMA batch. When the
controller places the pattern-found bits in the ECC status register,
use the same read for both. Read the pattern ID at most once per batch
if any slot reports a pattern without an ECC error. Read each packed
counter word only when a slot needs it and reuse it for the other slots
in that group of four. Consume the results before starting the next
ECC transfer.

Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 87 ++++++++++++++++++++++++++++-----------
1 file changed, 64 insertions(+), 23 deletions(-)

diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index a722636f2e15..37f01801cf48 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -1166,12 +1166,56 @@ struct sunxi_nfc_ecc_read {
bool read_raw_bbm;
};

-static unsigned int sunxi_nfc_hw_ecc_read_count(struct nand_chip *nand, int slot)
+/*
+ * Transfer-local ECC results. Consume the pattern ID and correction counts
+ * before issuing another ECC transfer.
+ */
+struct sunxi_nfc_ecc_snapshot {
+ u32 status;
+ u32 pattern_found;
+ u32 pattern_id;
+ u32 count_reg;
+ u32 count;
+};
+
+static void sunxi_nfc_hw_ecc_read_pattern_id(struct nand_chip *nand,
+ struct sunxi_nfc_ecc_snapshot *snapshot)
+{
+ struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+
+ snapshot->pattern_id = readl(nfc->regs + NFC_REG_PAT_ID(nfc));
+}
+
+static void sunxi_nfc_hw_ecc_read_status(struct nand_chip *nand,
+ struct sunxi_nfc_ecc_snapshot *snapshot)
+{
+ struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+ u32 pattern_found;
+
+ pattern_found = readl(nfc->regs + nfc->caps->reg_pat_found);
+ snapshot->pattern_found = field_get(NFC_ECC_PAT_FOUND_MSK(nfc), pattern_found);
+ if (nfc->caps->reg_pat_found == NFC_REG_ECC_ST)
+ snapshot->status = pattern_found;
+ else
+ snapshot->status = readl(nfc->regs + NFC_REG_ECC_ST);
+ /* A new operation, including every PIO step, invalidates the count word. */
+ snapshot->count_reg = ~0U;
+}
+
+static unsigned int
+sunxi_nfc_hw_ecc_read_count(struct nand_chip *nand,
+ struct sunxi_nfc_ecc_snapshot *snapshot, int slot)
{
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
- u32 count = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(nfc, slot));
+ u32 reg = NFC_REG_ECC_ERR_CNT(nfc, slot);

- return NFC_ECC_ERR_CNT(slot, count);
+ /* Four consecutive DMA steps share one error-counter register. */
+ if (snapshot->count_reg != reg) {
+ snapshot->count = readl(nfc->regs + reg);
+ snapshot->count_reg = reg;
+ }
+
+ return NFC_ECC_ERR_CNT(slot, snapshot->count);
}

/*
@@ -1275,7 +1319,7 @@ static int sunxi_nfc_hw_ecc_read_extra_oob(struct nand_chip *nand,
/* Interpret one PIO step or a completed DMA batch before reusing its status. */
static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
struct sunxi_nfc_ecc_read *read,
- u32 status, u32 pattern_found,
+ struct sunxi_nfc_ecc_snapshot *snapshot,
int first_step, int nsteps)
{
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
@@ -1286,6 +1330,9 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
unsigned int reg_index = 0, user_data_sz;
int slot, ret;

+ if (snapshot->pattern_found & ~snapshot->status)
+ sunxi_nfc_hw_ecc_read_pattern_id(nand, snapshot);
+
for (slot = 0; slot < nsteps; slot++, reg_index += user_data_sz / 4) {
int step = first_step + slot;
int oob_off = sunxi_get_oob_offset(sunxi_nand, ecc, step);
@@ -1297,16 +1344,15 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
if (step < read->first_req_step)
continue;

- if (status & NFC_ECC_ERR(slot)) {
+ if (snapshot->status & NFC_ECC_ERR(slot)) {
/* Preserve PIO data before the raw erased-page check. */
if (!read->dma && !randomized)
memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
continue;
}

- if (pattern_found & BIT(slot)) {
- u32 pattern_id = readl(nfc->regs + NFC_REG_PAT_ID(nfc));
- u8 pattern = pattern_id & BIT(slot) ? 0xff : 0;
+ if (snapshot->pattern_found & BIT(slot)) {
+ u8 pattern = snapshot->pattern_id & BIT(slot) ? 0xff : 0;

if (!read->dma || randomized)
memset(data, pattern, ecc->size);
@@ -1320,7 +1366,7 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
continue;
}

- bitflips = sunxi_nfc_hw_ecc_read_count(nand, slot);
+ bitflips = sunxi_nfc_hw_ecc_read_count(nand, snapshot, slot);

if (!read->dma)
memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
@@ -1353,14 +1399,14 @@ static int sunxi_nfc_oob_read_batch(struct nand_chip *nand,
sunxi_nfc_hw_ecc_update_stats(nand, &read->max_bitflips, bitflips);
}

- if (!(status & NFC_ECC_ERR_MSK(nfc)))
+ if (!(snapshot->status & NFC_ECC_ERR_MSK(nfc)))
return 0;

/* Finish consuming the batch's corrected OOB before raw error rereads. */
for (slot = 0; slot < nsteps; slot++) {
int step = first_step + slot;

- if (step < read->first_req_step || !(status & NFC_ECC_ERR(slot)))
+ if (step < read->first_req_step || !(snapshot->status & NFC_ECC_ERR(slot)))
continue;

ret = sunxi_nfc_hw_ecc_read_error(nand, read, step);
@@ -1402,10 +1448,10 @@ static int sunxi_nfc_hw_ecc_read_chunk(struct nand_chip *nand,
unsigned int user_data_sz = sunxi_nfc_user_data_sz(sunxi_nand, step);
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
struct nand_ecc_ctrl *ecc = &nand->ecc;
+ struct sunxi_nfc_ecc_snapshot snapshot;
int data_off = step * ecc->size;
int oob_off = nand_to_mtd(nand)->writesize + sunxi_get_oob_offset(sunxi_nand, ecc, step);
int page = read->page;
- u32 pattern_found;
int ret;

if (*cur_off != data_off) {
@@ -1441,12 +1487,9 @@ static int sunxi_nfc_hw_ecc_read_chunk(struct nand_chip *nand,

*cur_off = oob_off + ecc->bytes + user_data_sz;

- pattern_found = readl(nfc->regs + nfc->caps->reg_pat_found);
- pattern_found = field_get(NFC_ECC_PAT_FOUND_MSK(nfc), pattern_found);
+ sunxi_nfc_hw_ecc_read_status(nand, &snapshot);

- return sunxi_nfc_oob_read_batch(nand, read,
- readl(nfc->regs + NFC_REG_ECC_ST),
- pattern_found, step, 1);
+ return sunxi_nfc_oob_read_batch(nand, read, &snapshot, step, 1);
}

static int sunxi_nfc_hw_ecc_read_batch_dma(struct nand_chip *nand,
@@ -1457,10 +1500,11 @@ static int sunxi_nfc_hw_ecc_read_batch_dma(struct nand_chip *nand,
struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
struct mtd_info *mtd = nand_to_mtd(nand);
struct nand_ecc_ctrl *ecc = &nand->ecc;
+ struct sunxi_nfc_ecc_snapshot snapshot;
struct scatterlist sg;
int page = read->page;
int ret, i;
- u32 status, pattern_found, wait;
+ u32 wait;

if (first_step) {
ret = sunxi_nfc_read_column(nand, page, first_step * ecc->size,
@@ -1522,12 +1566,9 @@ static int sunxi_nfc_hw_ecc_read_batch_dma(struct nand_chip *nand,
if (ret)
return ret;

- status = readl(nfc->regs + NFC_REG_ECC_ST);
- pattern_found = readl(nfc->regs + nfc->caps->reg_pat_found);
- pattern_found = field_get(NFC_ECC_PAT_FOUND_MSK(nfc), pattern_found);
+ sunxi_nfc_hw_ecc_read_status(nand, &snapshot);

- return sunxi_nfc_oob_read_batch(nand, read, status, pattern_found,
- first_step, nchunks);
+ return sunxi_nfc_oob_read_batch(nand, read, &snapshot, first_step, nchunks);
}

static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,

--
2.53.0