]> git.sur5r.net Git - u-boot/blobdiff - drivers/mtd/nand/nand_bch.c
mtd: nand: Rename nand.h into rawnand.h
[u-boot] / drivers / mtd / nand / nand_bch.c
index 7835fce29392e2bcc470ede1e39013faee12be10..b7c1171d50a06131ca59006f69cb6a2cbbf8013f 100644 (file)
@@ -4,19 +4,7 @@
  *
  * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
  *
- * This file is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 or (at your option) any
- * later version.
- *
- * This file is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this file; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+  * SPDX-License-Identifier:   GPL-2.0+
  */
 
 #include <common.h>
@@ -25,7 +13,7 @@
 
 #include <linux/bitops.h>
 #include <linux/mtd/mtd.h>
-#include <linux/mtd/nand.h>
+#include <linux/mtd/rawnand.h>
 #include <linux/mtd/nand_bch.h>
 #include <linux/bch.h>
 #include <malloc.h>
@@ -53,7 +41,7 @@ struct nand_bch_control {
 int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
                           unsigned char *code)
 {
-       const struct nand_chip *chip = mtd->priv;
+       const struct nand_chip *chip = mtd_to_nand(mtd);
        struct nand_bch_control *nbc = chip->ecc.priv;
        unsigned int i;
 
@@ -79,7 +67,7 @@ int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
 int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
                          unsigned char *read_ecc, unsigned char *calc_ecc)
 {
-       const struct nand_chip *chip = mtd->priv;
+       const struct nand_chip *chip = mtd_to_nand(mtd);
        struct nand_bch_control *nbc = chip->ecc.priv;
        unsigned int *errloc = nbc->errloc;
        int i, count;
@@ -93,12 +81,12 @@ int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
                                buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
                        /* else error in ecc, no action needed */
 
-                       MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: corrected bitflip %u\n",
-                             __func__, errloc[i]);
+                       pr_debug("%s: corrected bitflip %u\n",
+                                __func__, errloc[i]);
                }
        } else if (count < 0) {
                printk(KERN_ERR "ecc unrecoverable error\n");
-               count = -1;
+               count = -EBADMSG;
        }
        return count;
 }
@@ -106,9 +94,6 @@ int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
 /**
  * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  * @mtd:       MTD block structure
- * @eccsize:   ecc block size in bytes
- * @eccbytes:  ecc length in bytes
- * @ecclayout: output default layout
  *
  * Returns:
  *  a pointer to a new NAND BCH control structure, or NULL upon failure
@@ -122,14 +107,21 @@ int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  * @eccsize = 512  (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  * @eccbytes = 7   (7 bytes are required to store m*t = 13*4 = 52 bits)
  */
-struct nand_bch_control *
-nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
-             struct nand_ecclayout **ecclayout)
+struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
 {
+       struct nand_chip *nand = mtd_to_nand(mtd);
        unsigned int m, t, eccsteps, i;
-       struct nand_ecclayout *layout;
+       struct nand_ecclayout *layout = nand->ecc.layout;
        struct nand_bch_control *nbc = NULL;
        unsigned char *erased_page;
+       unsigned int eccsize = nand->ecc.size;
+       unsigned int eccbytes = nand->ecc.bytes;
+       unsigned int eccstrength = nand->ecc.strength;
+
+       if (!eccbytes && eccstrength) {
+               eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
+               nand->ecc.bytes = eccbytes;
+       }
 
        if (!eccsize || !eccbytes) {
                printk(KERN_WARNING "ecc parameters not supplied\n");
@@ -157,7 +149,7 @@ nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
        eccsteps = mtd->writesize/eccsize;
 
        /* if no ecc placement scheme was provided, build one */
-       if (!*ecclayout) {
+       if (!layout) {
 
                /* handle large page devices only */
                if (mtd->oobsize < 64) {
@@ -183,7 +175,7 @@ nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
                layout->oobfree[0].offset = 2;
                layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
 
-               *ecclayout = layout;
+               nand->ecc.layout = layout;
        }
 
        /* sanity checks */
@@ -191,7 +183,7 @@ nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
                printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
                goto fail;
        }
-       if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
+       if (layout->eccbytes != (eccsteps*eccbytes)) {
                printk(KERN_WARNING "invalid ecc layout\n");
                goto fail;
        }
@@ -215,6 +207,9 @@ nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
        for (i = 0; i < eccbytes; i++)
                nbc->eccmask[i] ^= 0xff;
 
+       if (!eccstrength)
+               nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
+
        return nbc;
 fail:
        nand_bch_free(nbc);