mtd: rawnand: zynq: Fixes and software ECC support
Subsystems (3)
Miquel Raynal (DAVE)
maintainercover mtd: rawnand: zynq: Fixes and software ECC support
This driver is deeply broken in many ways. I've tried to clean it up by:
- flagging the most hiddeous parts (where Micron on-die ECC support is hardcoded)
- enabling DT support, eg. for reading the ECC configuration
- fixing software ECC support
- aligning the software ECC OOB layout to the rest of the world and allow a working interoperability in Linux.
---
Miquel Raynal (DAVE) (5):
mtd: rawnand: zynq: Align the layout structures with the driver namespace
mtd: rawnand: zynq: Add comments about very bad practices
mtd: rawnand: zynq: Make sure an of node is attached
mtd: rawnand: zynq: Do not fail the probe for no reason
mtd: rawnand: zynq: Fix Software ECC engine support
drivers/mtd/nand/raw/zynq_nand.c | 115 ++++++++++----------------------------- 1 file changed, 28 insertions(+), 87 deletions(-)
---
base-commit: 744cf5d4e398c5f657e5c3eeb44d00fa5175dc85
change-id: 20260522-dave-upstream-nand-fixes-60a5336517a2
Best regards,
Signature · 1 line
Miquel Raynal <[email protected]>
Miquel Raynal (DAVE)
maintainerPATCH 1/5 mtd: rawnand: zynq: Align the layout structures with the driver namespace
nand_oob_16/64 are names already used in the core and it is very misleading to reuse them here for something that is Zynq specific. It works because the core structures are not exported, but it is bad practice, so rename them to prefix them "zynq_" instead.
--- drivers/mtd/nand/raw/zynq_nand.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/mtd/nand/raw/zynq_nand.c +4 -4
@@ -185,7 +185,7 @@ static const struct zynq_nand_command_format zynq_nand_commands[] = { };/* Define default oob placement schemes for large and small page devices */ -static struct nand_ecclayout nand_oob_16 = { +static struct nand_ecclayout zynq_oob_16 = { .eccbytes =3 , .eccpos = {0 ,1 ,2 }, .oobfree = {
@@ -193,7 +193,7 @@ static struct nand_ecclayout nand_oob_16 = { } }; -static struct nand_ecclayout nand_oob_64 = { +static struct nand_ecclayout zynq_oob_64 = { .eccbytes =12 , .eccpos = {52 ,53 ,54 ,55 ,56 ,57 ,
@@ -1258,9 +1258,9 @@ static int zynq_nand_probe(struct udevice *dev) }if (mtd->oobsize ==16 ) - nand_chip->ecc.layout = &nand_oob_16; + nand_chip->ecc.layout = &zynq_oob_16;else if (mtd->oobsize ==64 ) - nand_chip->ecc.layout = &nand_oob_64; + nand_chip->ecc.layout = &zynq_oob_64;else printf ("%s: No oob layout found\n" , __func__); }
Signature · 1 line
2.53.0
Miquel Raynal (DAVE)
maintainerPATCH 2/5 mtd: rawnand: zynq: Add comments about very bad practices
In reply to Miquel Raynal (DAVE)The driver goes through very (very) wrong steps during initialization as it completely hardcodes support for Micron chips.
Flag these sections as undesirable in case someone ever cares about fixing this properly, or simply stumbles about these pieces of code and wonders why this is there.
--- drivers/mtd/nand/raw/zynq_nand.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/mtd/nand/raw/zynq_nand.c +8 -0
@@ -203,6 +203,10 @@ static struct nand_ecclayout zynq_oob_64 = { } }; +/* +* FIXME: This is a very (Micron) specific layout, which should not reside at +* the spi/ level. +*/ static struct nand_ecclayout ondie_nand_oob_64 = { .eccbytes =32 ,
@@ -1155,6 +1159,10 @@ static int zynq_nand_probe(struct udevice *dev) maf_id = nand_chip->read_byte (mtd); dev_id = nand_chip->read_byte (mtd); +/* +* FIXME: This is horribly wrong, it checks if Micron chips have on-die +* ECC support enabled. This should be moved away. +*/ if ((maf_id == 0x2c) && ((dev_id == 0xf1) || (dev_id == 0xa1) || (dev_id == 0xb1) || (dev_id == 0xaa) || (dev_id == 0xba) ||
Signature · 1 line
2.53.0
Miquel Raynal (DAVE)
maintainerPATCH 3/5 mtd: rawnand: zynq: Make sure an of node is attached
In reply to Miquel Raynal (DAVE)If no ofnode is attached by the controller driver, DT parsing does not happen.
--- drivers/mtd/nand/raw/zynq_nand.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/mtd/nand/raw/zynq_nand.c +5 -0
@@ -1103,6 +1103,11 @@ static int zynq_nand_probe(struct udevice *dev)return dm_scan_fdt_dev (dev); } +if (!ofnode_get_child_count (of_nand)) +nand_set_flash_node (nand_chip, of_nand); +else +nand_set_flash_node (nand_chip,ofnode_first_subnode (of_nand)); +if (ofnode_read_resource (of_nand,0 , &res)) {printf ("Failed to get nand resource\n" );return -ENODEV ;
Signature · 1 line
2.53.0
Miquel Raynal (DAVE)
maintainerPATCH 4/5 mtd: rawnand: zynq: Do not fail the probe for no reason
In reply to Miquel Raynal (DAVE)If there is no indication about 16b buswidth, no need to fail the probe, just assume it is the (very standard) 8b case.
--- drivers/mtd/nand/raw/zynq_nand.c | 6 ------ 1 file changed, 6 deletions(-)
drivers/mtd/nand/raw/zynq_nand.c +0 -6
@@ -1134,12 +1134,6 @@ static int zynq_nand_probe(struct udevice *dev) nand_chip->write_buf = zynq_nand_write_buf; is_16bit_bw =zynq_nand_check_is_16bit_bw_flash (); -if (is_16bit_bw ==NAND_BW_UNKNOWN ) { -printf ("%s: Unable detect NAND based on MIO settings\n" , - __func__); -return -EINVAL ; - } -if (is_16bit_bw ==NAND_BW_16BIT ) nand_chip->options =NAND_BUSWIDTH_16 ;
Signature · 1 line
2.53.0
Miquel Raynal (DAVE)
maintainerPATCH 5/5 mtd: rawnand: zynq: Fix Software ECC engine support
In reply to Miquel Raynal (DAVE)Software ECC engine was not partially working but more importantly was unaligned with the default software layout that has been in place for decades due to the fact that the driver was enforcing the hardware ECC layout (for wrong reasons). Fix the software ECC implementation, which has the side effect of also aligning it with Linux and making the interoperability work.
--- drivers/mtd/nand/raw/zynq_nand.c | 88 +++++----------------------------------- 1 file changed, 11 insertions(+), 77 deletions(-)
drivers/mtd/nand/raw/zynq_nand.c +11 -77
@@ -631,34 +631,6 @@ static int zynq_nand_write_page_hwecc(struct mtd_info *mtd,return 0 ; } -/* -* zynq_nand_write_page_swecc - [REPLACABLE] software ecc based page -* write function -* @mtd: mtd info structure -* @chip: nand chip info structure -* @buf: data buffer -* @oob_required: must write chip->oob_poi to OOB -*/ -static int zynq_nand_write_page_swecc (struct mtd_info *mtd, -struct nand_chip *chip,const u8 *buf,int oob_required,int page) -{ -int i, eccsize = chip->ecc.size; -int eccbytes = chip->ecc.bytes; -int eccsteps = chip->ecc.steps; -u8 *ecc_calc = chip->buffers->ecccalc; -const u8 *p = buf; -u32 *eccpos = chip->ecc.layout->eccpos; - -/* Software ecc calculation */ -for (i =0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) - chip->ecc.calculate (mtd, p, &ecc_calc[i]); - -for (i =0 ; i < chip->ecc.total; i++) - chip->oob_poi[eccpos[i]] = ecc_calc[i]; - -return chip->ecc.write_page_raw (mtd, chip, buf,1 , page); -} -/* * nand_read_page_hwecc - Hardware ECC based page read function * @mtd: Pointer to the mtd info structure
@@ -736,48 +708,6 @@ static int zynq_nand_read_page_hwecc(struct mtd_info *mtd,return 0 ; } -/* -* zynq_nand_read_page_swecc - [REPLACABLE] software ecc based page -* read function -* @mtd: mtd info structure -* @chip: nand chip info structure -* @buf: buffer to store read data -* @page: page number to read -*/ -static int zynq_nand_read_page_swecc (struct mtd_info *mtd, -struct nand_chip *chip,u8 *buf,int oob_required,int page) -{ -int i, eccsize = chip->ecc.size; -int eccbytes = chip->ecc.bytes; -int eccsteps = chip->ecc.steps; -u8 *p = buf; -u8 *ecc_calc = chip->buffers->ecccalc; -u8 *ecc_code = chip->buffers->ecccode; -u32 *eccpos = chip->ecc.layout->eccpos; - - chip->ecc.read_page_raw (mtd, chip, buf,1 , page); - -for (i =0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) - chip->ecc.calculate (mtd, p, &ecc_calc[i]); - -for (i =0 ; i < chip->ecc.total; i++) - ecc_code[i] = chip->oob_poi[eccpos[i]]; - - eccsteps = chip->ecc.steps; - p = buf; - -for (i =0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { -int stat; - - stat = chip->ecc.correct (mtd, p, &ecc_code[i], &ecc_calc[i]); -if (stat <0 ) - mtd->ecc_stats.failed++; -else - mtd->ecc_stats.corrected += stat; - } -return 0 ; -} -/* * zynq_nand_select_chip - Select the flash device * @mtd: Pointer to the mtd_info structure
@@ -1219,6 +1149,16 @@ static int zynq_nand_probe(struct udevice *dev)/* Use the BBT pattern descriptors */ nand_chip->bbt_td = &bbt_main_descr; nand_chip->bbt_md = &bbt_mirror_descr; + }else if (nand_chip->ecc.mode ==NAND_ECC_SOFT || + nand_chip->ecc.mode ==NAND_ECC_SOFT_BCH || + nand_chip->ecc.mode ==NAND_ECC_NONE ) { +/* Make sure the ECC engine is off */ + ecc_cfg =readl (&smc->reg->emcr); + ecc_cfg &= ~ZYNQ_MEMC_NAND_ECC_MODE_MASK ; +writel (ecc_cfg, &smc->reg->emcr); + + nand_chip->ecc.read_page_raw = zynq_nand_read_page_raw; + nand_chip->ecc.write_page_raw = zynq_nand_write_page_raw; }else {/* Hardware ECC generates 3 bytes ECC code for each 512 bytes */ nand_chip->ecc.mode =NAND_ECC_HW ;
@@ -1255,13 +1195,7 @@ static int zynq_nand_probe(struct udevice *dev) &smc->reg->emcr);break ;default : - nand_chip->ecc.mode =NAND_ECC_SOFT ; - nand_chip->ecc.calculate = nand_calculate_ecc; - nand_chip->ecc.correct = nand_correct_data; - nand_chip->ecc.read_page = zynq_nand_read_page_swecc; - nand_chip->ecc.write_page = zynq_nand_write_page_swecc; - nand_chip->ecc.size =256 ; -break ; +return -EINVAL ; }if (mtd->oobsize ==16 )
Signature · 1 line
2.53.0