mtd: nand: read_page() returns max_bitflips
authorMike Dunn <mikedunn@newsguy.com>
Wed, 25 Apr 2012 19:06:09 +0000 (12:06 -0700)
committerDavid Woodhouse <David.Woodhouse@intel.com>
Mon, 14 May 2012 04:12:06 +0000 (23:12 -0500)
The ecc.read_page() method for nand drivers is changed to return the maximum
number of bitflips that were corrected on any one region covering an ecc step,
This patch doesn't change what the nand code returns to mtd.

This v2 includes the change to the fsl_ifc_nand driver requested by Scott¹.

¹ http://lists.infradead.org/pipermail/linux-mtd/2012-April/040883.html

Signed-off-by: Mike Dunn <mikedunn@newsguy.com>
Acked-by (freescale changes): Scott Wood <scottwood@freescale.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
drivers/mtd/nand/atmel_nand.c
drivers/mtd/nand/bcm_umi_bch.c
drivers/mtd/nand/cafe_nand.c
drivers/mtd/nand/denali.c
drivers/mtd/nand/docg4.c
drivers/mtd/nand/fsl_elbc_nand.c
drivers/mtd/nand/fsl_ifc_nand.c
drivers/mtd/nand/fsmc_nand.c
drivers/mtd/nand/nand_base.c
drivers/mtd/nand/sh_flctl.c

index 2165576a1c67df0e623752970fdfdd141f3ea931..9a7876e8de4220bed1adfcec223e8b467698a4d2 100644 (file)
@@ -335,6 +335,7 @@ static int atmel_nand_read_page(struct mtd_info *mtd,
        uint8_t *oob = chip->oob_poi;
        uint8_t *ecc_pos;
        int stat;
+       unsigned int max_bitflips = 0;
 
        /*
         * Errata: ALE is incorrectly wired up to the ECC controller
@@ -371,10 +372,12 @@ static int atmel_nand_read_page(struct mtd_info *mtd,
        /* check if there's an error */
        stat = chip->ecc.correct(mtd, p, oob, NULL);
 
-       if (stat < 0)
+       if (stat < 0) {
                mtd->ecc_stats.failed++;
-       else
+       } else {
                mtd->ecc_stats.corrected += stat;
+               max_bitflips = max_t(unsigned int, max_bitflips, stat);
+       }
 
        /* get back to oob start (end of page) */
        chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
@@ -382,7 +385,7 @@ static int atmel_nand_read_page(struct mtd_info *mtd,
        /* read the oob */
        chip->read_buf(mtd, oob, mtd->oobsize);
 
-       return 0;
+       return max_bitflips;
 }
 
 /*
index a930666d0687655e1f5fe27c1604c34e38fe6911..f8472b49567a2d84d02ae26b64d71a402ba12593 100644 (file)
@@ -116,6 +116,7 @@ static int bcm_umi_bch_read_page_hwecc(struct mtd_info *mtd,
        uint8_t eccCalc[NAND_ECC_NUM_BYTES];
        int sectorOobSize = mtd->oobsize / eccsteps;
        int stat;
+       unsigned int max_bitflips = 0;
 
        for (sectorIdx = 0; sectorIdx < eccsteps;
                        sectorIdx++, datap += eccsize) {
@@ -177,9 +178,10 @@ static int bcm_umi_bch_read_page_hwecc(struct mtd_info *mtd,
                        }
 #endif
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
                }
        }
-       return 0;
+       return max_bitflips;
 }
 
 /****************************************************************************
index 2973d97bd529b2fc2cdeb369e59577ddbdb1d0bb..886ff3a064c296479fd64dd1462e93551fca81f6 100644 (file)
@@ -383,6 +383,7 @@ static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
                               uint8_t *buf, int page)
 {
        struct cafe_priv *cafe = mtd->priv;
+       unsigned int max_bitflips = 0;
 
        cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
                     cafe_readl(cafe, NAND_ECC_RESULT),
@@ -449,10 +450,11 @@ static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
                } else {
                        dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
                        mtd->ecc_stats.corrected += n;
+                       max_bitflips = max_t(unsigned int, max_bitflips, n);
                }
        }
 
-       return 0;
+       return max_bitflips;
 }
 
 static struct nand_ecclayout cafe_oobinfo_2048 = {
index a1048c7c5d2c747ae75cc924ff4876d3bade156a..1b346474dba8969cde0a3a82c23c6f18b53e520e 100644 (file)
@@ -924,9 +924,10 @@ bool is_erased(uint8_t *buf, int len)
 #define ECC_LAST_ERR(x)                ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
 
 static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
-                                       uint32_t irq_status)
+                      uint32_t irq_status, unsigned int *max_bitflips)
 {
        bool check_erased_page = false;
+       unsigned int bitflips = 0;
 
        if (irq_status & INTR_STATUS__ECC_ERR) {
                /* read the ECC errors. we'll ignore them for now */
@@ -965,6 +966,7 @@ static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
                                        /* correct the ECC error */
                                        buf[offset] ^= err_correction_value;
                                        denali->mtd.ecc_stats.corrected++;
+                                       bitflips++;
                                }
                        } else {
                                /* if the error is not correctable, need to
@@ -984,6 +986,7 @@ static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf,
                clear_interrupts(denali);
                denali_set_intr_modes(denali, true);
        }
+       *max_bitflips = bitflips;
        return check_erased_page;
 }
 
@@ -1121,6 +1124,7 @@ static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
 static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
                            uint8_t *buf, int page)
 {
+       unsigned int max_bitflips;
        struct denali_nand_info *denali = mtd_to_denali(mtd);
 
        dma_addr_t addr = denali->buf.dma_buf;
@@ -1153,7 +1157,7 @@ static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
 
        memcpy(buf, denali->buf.buf, mtd->writesize);
 
-       check_erased_page = handle_ecc(denali, buf, irq_status);
+       check_erased_page = handle_ecc(denali, buf, irq_status, &max_bitflips);
        denali_enable_dma(denali, false);
 
        if (check_erased_page) {
@@ -1167,7 +1171,7 @@ static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
                                denali->mtd.ecc_stats.failed++;
                }
        }
-       return 0;
+       return max_bitflips;
 }
 
 static int denali_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
index b08202664543200553255f89c00669e203783d14..8e7da0170a2b70867418bc1bcd218d583c5d7873 100644 (file)
@@ -720,6 +720,7 @@ static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
        struct docg4_priv *doc = nand->priv;
        void __iomem *docptr = doc->virtadr;
        uint16_t status, edc_err, *buf16;
+       int bits_corrected = 0;
 
        dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
 
@@ -772,7 +773,7 @@ static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
 
                /* If bitflips are reported, attempt to correct with ecc */
                if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
-                       int bits_corrected = correct_data(mtd, buf, page);
+                       bits_corrected = correct_data(mtd, buf, page);
                        if (bits_corrected == -EBADMSG)
                                mtd->ecc_stats.failed++;
                        else
@@ -781,7 +782,7 @@ static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
        }
 
        writew(0, docptr + DOC_DATAEND);
-       return 0;
+       return bits_corrected;
 }
 
 
index 80b5264f0a32f031a10f5b50e50987a1184646e1..8638b5e734cc9aa8290f817f1559d4b3f185997e 100644 (file)
@@ -75,6 +75,7 @@ struct fsl_elbc_fcm_ctrl {
        unsigned int use_mdr;    /* Non zero if the MDR is to be set      */
        unsigned int oob;        /* Non zero if operating on OOB data     */
        unsigned int counter;    /* counter for the initializations       */
+       unsigned int max_bitflips;  /* Saved during READ0 cmd             */
 };
 
 /* These map to the positions used by the FCM hardware ECC generator */
@@ -253,6 +254,8 @@ static int fsl_elbc_run_command(struct mtd_info *mtd)
        if (chip->ecc.mode != NAND_ECC_HW)
                return 0;
 
+       elbc_fcm_ctrl->max_bitflips = 0;
+
        if (elbc_fcm_ctrl->read_bytes == mtd->writesize + mtd->oobsize) {
                uint32_t lteccr = in_be32(&lbc->lteccr);
                /*
@@ -262,11 +265,16 @@ static int fsl_elbc_run_command(struct mtd_info *mtd)
                 * bits 28-31 are uncorrectable errors, marked elsewhere.
                 * for small page nand only 1 bit is used.
                 * if the ELBC doesn't have the lteccr register it reads 0
+                * FIXME: 4 bits can be corrected on NANDs with 2k pages, so
+                * count the number of sub-pages with bitflips and update
+                * ecc_stats.corrected accordingly.
                 */
                if (lteccr & 0x000F000F)
                        out_be32(&lbc->lteccr, 0x000F000F); /* clear lteccr */
-               if (lteccr & 0x000F0000)
+               if (lteccr & 0x000F0000) {
                        mtd->ecc_stats.corrected++;
+                       elbc_fcm_ctrl->max_bitflips = 1;
+               }
        }
 
        return 0;
@@ -743,13 +751,17 @@ static int fsl_elbc_read_page(struct mtd_info *mtd,
                              uint8_t *buf,
                              int page)
 {
+       struct fsl_elbc_mtd *priv = chip->priv;
+       struct fsl_lbc_ctrl *ctrl = priv->ctrl;
+       struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
+
        fsl_elbc_read_buf(mtd, buf, mtd->writesize);
        fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
 
        if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
                mtd->ecc_stats.failed++;
 
-       return 0;
+       return elbc_fcm_ctrl->max_bitflips;
 }
 
 /* ECC will be calculated automatically, and errors will be detected in
@@ -814,11 +826,6 @@ static int fsl_elbc_chip_init(struct fsl_elbc_mtd *priv)
                chip->ecc.size = 512;
                chip->ecc.bytes = 3;
                chip->ecc.strength = 1;
-               /*
-                * FIXME: can hardware ecc correct 4 bitflips if page size is
-                * 2k?  Then does hardware report number of corrections for this
-                * case?  If so, ecc_stats reporting needs to be fixed as well.
-                */
        } else {
                /* otherwise fall back to default software ECC */
                chip->ecc.mode = NAND_ECC_SOFT;
index 872cc9605291762d5d4eebd9e2977c9229a05de3..0adde96704184928987ac6c85f782954a9f29a5c 100644 (file)
@@ -63,6 +63,7 @@ struct fsl_ifc_nand_ctrl {
        unsigned int oob;       /* Non zero if operating on OOB data    */
        unsigned int eccread;   /* Non zero for a full-page ECC read    */
        unsigned int counter;   /* counter for the initializations      */
+       unsigned int max_bitflips;  /* Saved during READ0 cmd           */
 };
 
 static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl;
@@ -262,6 +263,8 @@ static void fsl_ifc_run_command(struct mtd_info *mtd)
        if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
                dev_err(priv->dev, "NAND Flash Write Protect Error\n");
 
+       nctrl->max_bitflips = 0;
+
        if (nctrl->eccread) {
                int errors;
                int bufnum = nctrl->page & priv->bufnum_mask;
@@ -290,6 +293,9 @@ static void fsl_ifc_run_command(struct mtd_info *mtd)
                        }
 
                        mtd->ecc_stats.corrected += errors;
+                       nctrl->max_bitflips = max_t(unsigned int,
+                                                   nctrl->max_bitflips,
+                                                   errors);
                }
 
                nctrl->eccread = 0;
@@ -698,6 +704,7 @@ static int fsl_ifc_read_page(struct mtd_info *mtd,
 {
        struct fsl_ifc_mtd *priv = chip->priv;
        struct fsl_ifc_ctrl *ctrl = priv->ctrl;
+       struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
 
        fsl_ifc_read_buf(mtd, buf, mtd->writesize);
        fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
@@ -708,7 +715,7 @@ static int fsl_ifc_read_page(struct mtd_info *mtd,
        if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
                mtd->ecc_stats.failed++;
 
-       return 0;
+       return nctrl->max_bitflips;
 }
 
 /* ECC will be calculated automatically, and errors will be detected in
index 9d7f4171e0776925449edcc51fa3637ea3c8f3da..6bf59fdde263a020f05529980861d4ce545dabde 100644 (file)
@@ -720,6 +720,7 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
         */
        uint16_t ecc_oob[7];
        uint8_t *oob = (uint8_t *)&ecc_oob[0];
+       unsigned int max_bitflips = 0;
 
        for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
                chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
@@ -748,13 +749,15 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
 
                stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
        }
 
-       return 0;
+       return max_bitflips;
 }
 
 /*
index 671f228d4c7c15d85f684fa67d6140867b60a7da..8718eaf8269fc97e57dab9e72d8edebe3fea52e3 100644 (file)
@@ -1138,6 +1138,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
        uint8_t *ecc_calc = chip->buffers->ecccalc;
        uint8_t *ecc_code = chip->buffers->ecccode;
        uint32_t *eccpos = chip->ecc.layout->eccpos;
+       unsigned int max_bitflips = 0;
 
        chip->ecc.read_page_raw(mtd, chip, buf, page);
 
@@ -1154,12 +1155,14 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
                int stat;
 
                stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
        }
-       return 0;
+       return max_bitflips;
 }
 
 /**
@@ -1180,6 +1183,7 @@ static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
        int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
        int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
        int index = 0;
+       unsigned int max_bitflips = 0;
 
        /* Column address within the page aligned to ECC size (256bytes) */
        start_step = data_offs / chip->ecc.size;
@@ -1244,12 +1248,14 @@ static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
 
                stat = chip->ecc.correct(mtd, p,
                        &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
        }
-       return 0;
+       return max_bitflips;
 }
 
 /**
@@ -1271,6 +1277,7 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        uint8_t *ecc_calc = chip->buffers->ecccalc;
        uint8_t *ecc_code = chip->buffers->ecccode;
        uint32_t *eccpos = chip->ecc.layout->eccpos;
+       unsigned int max_bitflips = 0;
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
                chip->ecc.hwctl(mtd, NAND_ECC_READ);
@@ -1289,12 +1296,14 @@ static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
                int stat;
 
                stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
        }
-       return 0;
+       return max_bitflips;
 }
 
 /**
@@ -1320,6 +1329,7 @@ static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
        uint8_t *ecc_code = chip->buffers->ecccode;
        uint32_t *eccpos = chip->ecc.layout->eccpos;
        uint8_t *ecc_calc = chip->buffers->ecccalc;
+       unsigned int max_bitflips = 0;
 
        /* Read the OOB area first */
        chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
@@ -1337,12 +1347,14 @@ static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
 
                stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
        }
-       return 0;
+       return max_bitflips;
 }
 
 /**
@@ -1363,6 +1375,7 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
        int eccsteps = chip->ecc.steps;
        uint8_t *p = buf;
        uint8_t *oob = chip->oob_poi;
+       unsigned int max_bitflips = 0;
 
        for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
                int stat;
@@ -1379,10 +1392,12 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
                chip->read_buf(mtd, oob, eccbytes);
                stat = chip->ecc.correct(mtd, p, oob, NULL);
 
-               if (stat < 0)
+               if (stat < 0) {
                        mtd->ecc_stats.failed++;
-               else
+               } else {
                        mtd->ecc_stats.corrected += stat;
+                       max_bitflips = max_t(unsigned int, max_bitflips, stat);
+               }
 
                oob += eccbytes;
 
@@ -1397,7 +1412,7 @@ static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
        if (i)
                chip->read_buf(mtd, oob, i);
 
-       return 0;
+       return max_bitflips;
 }
 
 /**
@@ -1588,7 +1603,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
        if (oob)
                ops->oobretlen = ops->ooblen - oobreadlen;
 
-       if (ret)
+       if (ret < 0)
                return ret;
 
        if (mtd->ecc_stats.failed - stats.failed)
index e9b2b260de3ae081bbeb2e04bbff4d61902003b3..4ea3e20169bafbd2a649fb52ba4095a18a258e8b 100644 (file)
@@ -359,7 +359,7 @@ static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
                if (flctl->hwecc_cant_correct[i])
                        mtd->ecc_stats.failed++;
                else
-                       mtd->ecc_stats.corrected += 0;
+                       mtd->ecc_stats.corrected += 0; /* FIXME */
        }
 
        return 0;