* [PATCH] nand: gpmc: Handle bitflips in erased pages when using BCH ECC engine
@ 2021-11-18 18:25 David Rivshin
2022-01-17 18:36 ` Tom Rini
0 siblings, 1 reply; 2+ messages in thread
From: David Rivshin @ 2021-11-18 18:25 UTC (permalink / raw)
To: u-boot; +Cc: Tom Rini
From: David Rivshin <DRivshin@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@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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] nand: gpmc: Handle bitflips in erased pages when using BCH ECC engine
2021-11-18 18:25 [PATCH] nand: gpmc: Handle bitflips in erased pages when using BCH ECC engine David Rivshin
@ 2022-01-17 18:36 ` Tom Rini
0 siblings, 0 replies; 2+ messages in thread
From: Tom Rini @ 2022-01-17 18:36 UTC (permalink / raw)
To: David Rivshin; +Cc: u-boot
[-- Attachment #1: Type: text/plain, Size: 1124 bytes --]
On Thu, Nov 18, 2021 at 01:25:24PM -0500, David Rivshin wrote:
> From: David Rivshin <DRivshin@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@allworx.com>
Applied to u-boot/master, thanks!
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-01-17 18:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-18 18:25 [PATCH] nand: gpmc: Handle bitflips in erased pages when using BCH ECC engine David Rivshin
2022-01-17 18:36 ` Tom Rini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox