[PATCH] nand: gpmc: Handle bitflips in erased pages when using BCH ECC engine

David Rivshin drivshin at awxrd.com
Thu Nov 18 19:25:24 CET 2021


From: David Rivshin <DRivshin at allworx.com>

In the case of an erased (sub)page both the data and ECC are all 0xFF
bytes. This fails the normal ECC verification, as the computed ECC of
all-0xFF is not also 0xFF. The GPMC NAND driver attempted to detect
erased pages by checking that the ECC bytes are all-0xFF, but this had
two problems:
1) bitflips in the data were not corrected, so the data looked not-erased
2) bitflips in the ECC bytes were reported as uncorrectable ECC errors

The equivalent Linux driver [1] correctly handles this by counting the
number of 0-bits in the combination of data and ECC bytes. If the number
of 0-bits is less than the amount of bits correctable by the selected
ECC algorithm, then it is treated as an erased page with correctable
bitflips.

Implement similar, though simplified, logic in omap_correct_data_bch().

[1] see omap_elm_correct_data() in omap2.c

Signed-off-by: David Rivshin <drivshin at allworx.com>
---
 drivers/mtd/nand/raw/omap_gpmc.c | 45 +++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/drivers/mtd/nand/raw/omap_gpmc.c b/drivers/mtd/nand/raw/omap_gpmc.c
index 97fd5690f5..5cfabe5c3c 100644
--- a/drivers/mtd/nand/raw/omap_gpmc.c
+++ b/drivers/mtd/nand/raw/omap_gpmc.c
@@ -506,19 +506,45 @@ static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
 	/* check calculated ecc */
 	for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
 		if (calc_ecc[i] != 0x00)
-			ecc_flag = 1;
+			goto not_ecc_match;
 	}
-	if (!ecc_flag)
-		return 0;
+	return 0;
+not_ecc_match:
 
-	/* check for whether its a erased-page */
-	ecc_flag = 0;
-	for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
+	/* check for whether it's an erased-page */
+	for (i = 0; i < ecc->bytes; i++) {
 		if (read_ecc[i] != 0xff)
-			ecc_flag = 1;
+			goto not_erased;
+	}
+	for (i = 0; i < SECTOR_BYTES; i++) {
+		if (dat[i] != 0xff)
+			goto not_erased;
+	}
+	return 0;
+not_erased:
+
+	/*
+	 * Check for whether it's an erased page with a correctable
+	 * number of bitflips. Erased pages have all 1's in the data,
+	 * so we just compute the number of 0 bits in the data and
+	 * see if it's under the correction threshold.
+	 *
+	 * NOTE: The check for a perfect erased page above is faster for
+	 * the more common case, even though it's logically redundant.
+	 */
+	for (i = 0; i < ecc->bytes; i++)
+		error_count += hweight8(~read_ecc[i]);
+
+	for (i = 0; i < SECTOR_BYTES; i++)
+		error_count += hweight8(~dat[i]);
+
+	if (error_count <= ecc->strength) {
+		memset(read_ecc, 0xFF, ecc->bytes);
+		memset(dat, 0xFF, SECTOR_BYTES);
+		debug("nand: %u bit-flip(s) corrected in erased page\n",
+		      error_count);
+		return error_count;
 	}
-	if (!ecc_flag)
-		return 0;
 
 	/*
 	 * while reading ECC result we read it in big endian.
@@ -538,6 +564,7 @@ static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
 	}
 	/* use elm module to check for errors */
 	elm_config(bch_type);
+	error_count = 0;
 	err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
 	if (err)
 		return err;

base-commit: d80bb749fab53da72c4a0e09b8c2d2aaa3103c91
-- 
2.31.1



More information about the U-Boot mailing list