]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/mtd/nand/fsmc_nand.c
mtd: flash drivers set ecc strength
[karo-tx-linux.git] / drivers / mtd / nand / fsmc_nand.c
index e53b76064133fc52bded214fb57a94696abcb891..588e3733c3a43883fe8bb4a8fcc3a623f2aabdff 100644 (file)
@@ -34,7 +34,7 @@
 #include <linux/amba/bus.h>
 #include <mtd/mtd-abi.h>
 
-static struct nand_ecclayout fsmc_ecc1_layout = {
+static struct nand_ecclayout fsmc_ecc1_128_layout = {
        .eccbytes = 24,
        .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
                66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
@@ -50,7 +50,127 @@ static struct nand_ecclayout fsmc_ecc1_layout = {
        }
 };
 
-static struct nand_ecclayout fsmc_ecc4_lp_layout = {
+static struct nand_ecclayout fsmc_ecc1_64_layout = {
+       .eccbytes = 12,
+       .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
+       .oobfree = {
+               {.offset = 8, .length = 8},
+               {.offset = 24, .length = 8},
+               {.offset = 40, .length = 8},
+               {.offset = 56, .length = 8},
+       }
+};
+
+static struct nand_ecclayout fsmc_ecc1_16_layout = {
+       .eccbytes = 3,
+       .eccpos = {2, 3, 4},
+       .oobfree = {
+               {.offset = 8, .length = 8},
+       }
+};
+
+/*
+ * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
+ * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
+ * bytes are free for use.
+ */
+static struct nand_ecclayout fsmc_ecc4_256_layout = {
+       .eccbytes = 208,
+       .eccpos = {  2,   3,   4,   5,   6,   7,   8,
+               9,  10,  11,  12,  13,  14,
+               18,  19,  20,  21,  22,  23,  24,
+               25,  26,  27,  28,  29,  30,
+               34,  35,  36,  37,  38,  39,  40,
+               41,  42,  43,  44,  45,  46,
+               50,  51,  52,  53,  54,  55,  56,
+               57,  58,  59,  60,  61,  62,
+               66,  67,  68,  69,  70,  71,  72,
+               73,  74,  75,  76,  77,  78,
+               82,  83,  84,  85,  86,  87,  88,
+               89,  90,  91,  92,  93,  94,
+               98,  99, 100, 101, 102, 103, 104,
+               105, 106, 107, 108, 109, 110,
+               114, 115, 116, 117, 118, 119, 120,
+               121, 122, 123, 124, 125, 126,
+               130, 131, 132, 133, 134, 135, 136,
+               137, 138, 139, 140, 141, 142,
+               146, 147, 148, 149, 150, 151, 152,
+               153, 154, 155, 156, 157, 158,
+               162, 163, 164, 165, 166, 167, 168,
+               169, 170, 171, 172, 173, 174,
+               178, 179, 180, 181, 182, 183, 184,
+               185, 186, 187, 188, 189, 190,
+               194, 195, 196, 197, 198, 199, 200,
+               201, 202, 203, 204, 205, 206,
+               210, 211, 212, 213, 214, 215, 216,
+               217, 218, 219, 220, 221, 222,
+               226, 227, 228, 229, 230, 231, 232,
+               233, 234, 235, 236, 237, 238,
+               242, 243, 244, 245, 246, 247, 248,
+               249, 250, 251, 252, 253, 254
+       },
+       .oobfree = {
+               {.offset = 15, .length = 3},
+               {.offset = 31, .length = 3},
+               {.offset = 47, .length = 3},
+               {.offset = 63, .length = 3},
+               {.offset = 79, .length = 3},
+               {.offset = 95, .length = 3},
+               {.offset = 111, .length = 3},
+               {.offset = 127, .length = 3},
+               {.offset = 143, .length = 3},
+               {.offset = 159, .length = 3},
+               {.offset = 175, .length = 3},
+               {.offset = 191, .length = 3},
+               {.offset = 207, .length = 3},
+               {.offset = 223, .length = 3},
+               {.offset = 239, .length = 3},
+               {.offset = 255, .length = 1}
+       }
+};
+
+/*
+ * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
+ * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
+ * bytes are free for use.
+ */
+static struct nand_ecclayout fsmc_ecc4_224_layout = {
+       .eccbytes = 104,
+       .eccpos = {  2,   3,   4,   5,   6,   7,   8,
+               9,  10,  11,  12,  13,  14,
+               18,  19,  20,  21,  22,  23,  24,
+               25,  26,  27,  28,  29,  30,
+               34,  35,  36,  37,  38,  39,  40,
+               41,  42,  43,  44,  45,  46,
+               50,  51,  52,  53,  54,  55,  56,
+               57,  58,  59,  60,  61,  62,
+               66,  67,  68,  69,  70,  71,  72,
+               73,  74,  75,  76,  77,  78,
+               82,  83,  84,  85,  86,  87,  88,
+               89,  90,  91,  92,  93,  94,
+               98,  99, 100, 101, 102, 103, 104,
+               105, 106, 107, 108, 109, 110,
+               114, 115, 116, 117, 118, 119, 120,
+               121, 122, 123, 124, 125, 126
+       },
+       .oobfree = {
+               {.offset = 15, .length = 3},
+               {.offset = 31, .length = 3},
+               {.offset = 47, .length = 3},
+               {.offset = 63, .length = 3},
+               {.offset = 79, .length = 3},
+               {.offset = 95, .length = 3},
+               {.offset = 111, .length = 3},
+               {.offset = 127, .length = 97}
+       }
+};
+
+/*
+ * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
+ * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
+ * bytes are free for use.
+ */
+static struct nand_ecclayout fsmc_ecc4_128_layout = {
        .eccbytes = 104,
        .eccpos = {  2,   3,   4,   5,   6,   7,   8,
                9,  10,  11,  12,  13,  14,
@@ -81,6 +201,45 @@ static struct nand_ecclayout fsmc_ecc4_lp_layout = {
        }
 };
 
+/*
+ * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
+ * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
+ * bytes are free for use.
+ */
+static struct nand_ecclayout fsmc_ecc4_64_layout = {
+       .eccbytes = 52,
+       .eccpos = {  2,   3,   4,   5,   6,   7,   8,
+               9,  10,  11,  12,  13,  14,
+               18,  19,  20,  21,  22,  23,  24,
+               25,  26,  27,  28,  29,  30,
+               34,  35,  36,  37,  38,  39,  40,
+               41,  42,  43,  44,  45,  46,
+               50,  51,  52,  53,  54,  55,  56,
+               57,  58,  59,  60,  61,  62,
+       },
+       .oobfree = {
+               {.offset = 15, .length = 3},
+               {.offset = 31, .length = 3},
+               {.offset = 47, .length = 3},
+               {.offset = 63, .length = 1},
+       }
+};
+
+/*
+ * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
+ * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
+ * byte is free for use.
+ */
+static struct nand_ecclayout fsmc_ecc4_16_layout = {
+       .eccbytes = 13,
+       .eccpos = { 0,  1,  2,  3,  6,  7, 8,
+               9, 10, 11, 12, 13, 14
+       },
+       .oobfree = {
+               {.offset = 15, .length = 1},
+       }
+};
+
 /*
  * ECC placement definitions in oobfree type format.
  * There are 13 bytes of ecc for every 512 byte block and it has to be read
@@ -103,16 +262,6 @@ static struct fsmc_eccplace fsmc_ecc4_lp_place = {
        }
 };
 
-static struct nand_ecclayout fsmc_ecc4_sp_layout = {
-       .eccbytes = 13,
-       .eccpos = { 0,  1,  2,  3,  6,  7, 8,
-               9, 10, 11, 12, 13, 14
-       },
-       .oobfree = {
-               {.offset = 15, .length = 1},
-       }
-};
-
 static struct fsmc_eccplace fsmc_ecc4_sp_place = {
        .eccplace = {
                {.offset = 0, .length = 4},
@@ -391,6 +540,20 @@ static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
        return 0;
 }
 
+/* Count the number of 0's in buff upto a max of max_bits */
+static int count_written_bits(uint8_t *buff, int size, int max_bits)
+{
+       int k, written_bits = 0;
+
+       for (k = 0; k < size; k++) {
+               written_bits += hweight8(~buff[k]);
+               if (written_bits > max_bits)
+                       break;
+       }
+
+       return written_bits;
+}
+
 /*
  * fsmc_read_page_hwecc
  * @mtd:       mtd info structure
@@ -426,7 +589,6 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
        uint8_t *oob = (uint8_t *)&ecc_oob[0];
 
        for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
-
                chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
                chip->ecc.hwctl(mtd, NAND_ECC_READ);
                chip->read_buf(mtd, p, eccsize);
@@ -447,7 +609,7 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
                        j += len;
                }
 
-               memcpy(&ecc_code[i], oob, 13);
+               memcpy(&ecc_code[i], oob, chip->ecc.bytes);
                chip->ecc.calculate(mtd, p, &ecc_calc[i]);
 
                stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
@@ -461,7 +623,7 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
 }
 
 /*
- * fsmc_correct_data
+ * fsmc_bch8_correct_data
  * @mtd:       mtd info structure
  * @dat:       buffer of read data
  * @read_ecc:  ecc read from device spare area
@@ -470,19 +632,51 @@ static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  * calc_ecc is a 104 bit information containing maximum of 8 error
  * offset informations of 13 bits each in 512 bytes of read data.
  */
-static int fsmc_correct_data(struct mtd_info *mtd, uint8_t *dat,
+static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
                             uint8_t *read_ecc, uint8_t *calc_ecc)
 {
        struct fsmc_nand_data *host = container_of(mtd,
                                        struct fsmc_nand_data, mtd);
+       struct nand_chip *chip = mtd->priv;
        struct fsmc_regs *regs = host->regs_va;
        unsigned int bank = host->bank;
-       uint16_t err_idx[8];
-       uint64_t ecc_data[2];
+       uint32_t err_idx[8];
        uint32_t num_err, i;
+       uint32_t ecc1, ecc2, ecc3, ecc4;
 
-       /* The calculated ecc is actually the correction index in data */
-       memcpy(ecc_data, calc_ecc, 13);
+       num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
+
+       /* no bit flipping */
+       if (likely(num_err == 0))
+               return 0;
+
+       /* too many errors */
+       if (unlikely(num_err > 8)) {
+               /*
+                * This is a temporary erase check. A newly erased page read
+                * would result in an ecc error because the oob data is also
+                * erased to FF and the calculated ecc for an FF data is not
+                * FF..FF.
+                * This is a workaround to skip performing correction in case
+                * data is FF..FF
+                *
+                * Logic:
+                * For every page, each bit written as 0 is counted until these
+                * number of bits are greater than 8 (the maximum correction
+                * capability of FSMC for each 512 + 13 bytes)
+                */
+
+               int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
+               int bits_data = count_written_bits(dat, chip->ecc.size, 8);
+
+               if ((bits_ecc + bits_data) <= 8) {
+                       if (bits_data)
+                               memset(dat, 0xff, chip->ecc.size);
+                       return bits_data;
+               }
+
+               return -EBADMSG;
+       }
 
        /*
         * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
@@ -493,27 +687,26 @@ static int fsmc_correct_data(struct mtd_info *mtd, uint8_t *dat,
         * uint64_t array and error offset indexes are populated in err_idx
         * array
         */
-       for (i = 0; i < 8; i++) {
-               if (i == 4) {
-                       err_idx[4] = ((ecc_data[1] & 0x1) << 12) | ecc_data[0];
-                       ecc_data[1] >>= 1;
-                       continue;
-               }
-               err_idx[i] = (ecc_data[i/4] & 0x1FFF);
-               ecc_data[i/4] >>= 13;
-       }
-
-       num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
-
-       if (num_err == 0xF)
-               return -EBADMSG;
+       ecc1 = readl(&regs->bank_regs[bank].ecc1);
+       ecc2 = readl(&regs->bank_regs[bank].ecc2);
+       ecc3 = readl(&regs->bank_regs[bank].ecc3);
+       ecc4 = readl(&regs->bank_regs[bank].sts);
+
+       err_idx[0] = (ecc1 >> 0) & 0x1FFF;
+       err_idx[1] = (ecc1 >> 13) & 0x1FFF;
+       err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
+       err_idx[3] = (ecc2 >> 7) & 0x1FFF;
+       err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
+       err_idx[5] = (ecc3 >> 1) & 0x1FFF;
+       err_idx[6] = (ecc3 >> 14) & 0x1FFF;
+       err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
 
        i = 0;
        while (num_err--) {
                change_bit(0, (unsigned long *)&err_idx[i]);
                change_bit(1, (unsigned long *)&err_idx[i]);
 
-               if (err_idx[i] <= 512 * 8) {
+               if (err_idx[i] <= chip->ecc.size * 8) {
                        change_bit(err_idx[i], (unsigned long *)dat);
                        i++;
                }
@@ -568,27 +761,28 @@ static int __init fsmc_nand_probe(struct platform_device *pdev)
                goto err_probe1;
        }
 
-       host->resaddr = request_mem_region(res->start + PLAT_NAND_ALE,
+       host->resaddr = request_mem_region(res->start + pdata->ale_off,
                        resource_size(res), pdev->name);
        if (!host->resaddr) {
                ret = -EIO;
                goto err_probe1;
        }
 
-       host->addr_va = ioremap(res->start + PLAT_NAND_ALE, resource_size(res));
+       host->addr_va = ioremap(res->start + pdata->ale_off,
+                       resource_size(res));
        if (!host->addr_va) {
                ret = -EIO;
                goto err_probe1;
        }
 
-       host->rescmd = request_mem_region(res->start + PLAT_NAND_CLE,
+       host->rescmd = request_mem_region(res->start + pdata->cle_off,
                        resource_size(res), pdev->name);
        if (!host->rescmd) {
                ret = -EIO;
                goto err_probe1;
        }
 
-       host->cmd_va = ioremap(res->start + PLAT_NAND_CLE, resource_size(res));
+       host->cmd_va = ioremap(res->start + pdata->cle_off, resource_size(res));
        if (!host->cmd_va) {
                ret = -EIO;
                goto err_probe1;
@@ -667,12 +861,14 @@ static int __init fsmc_nand_probe(struct platform_device *pdev)
        if (AMBA_REV_BITS(host->pid) >= 8) {
                nand->ecc.read_page = fsmc_read_page_hwecc;
                nand->ecc.calculate = fsmc_read_hwecc_ecc4;
-               nand->ecc.correct = fsmc_correct_data;
+               nand->ecc.correct = fsmc_bch8_correct_data;
                nand->ecc.bytes = 13;
+               nand->ecc.strength = 8;
        } else {
                nand->ecc.calculate = fsmc_read_hwecc_ecc1;
                nand->ecc.correct = nand_correct_data;
                nand->ecc.bytes = 3;
+               nand->ecc.strength = 1;
        }
 
        /*
@@ -685,15 +881,48 @@ static int __init fsmc_nand_probe(struct platform_device *pdev)
        }
 
        if (AMBA_REV_BITS(host->pid) >= 8) {
-               if (host->mtd.writesize == 512) {
-                       nand->ecc.layout = &fsmc_ecc4_sp_layout;
+               switch (host->mtd.oobsize) {
+               case 16:
+                       nand->ecc.layout = &fsmc_ecc4_16_layout;
                        host->ecc_place = &fsmc_ecc4_sp_place;
-               } else {
-                       nand->ecc.layout = &fsmc_ecc4_lp_layout;
+                       break;
+               case 64:
+                       nand->ecc.layout = &fsmc_ecc4_64_layout;
+                       host->ecc_place = &fsmc_ecc4_lp_place;
+                       break;
+               case 128:
+                       nand->ecc.layout = &fsmc_ecc4_128_layout;
                        host->ecc_place = &fsmc_ecc4_lp_place;
+                       break;
+               case 224:
+                       nand->ecc.layout = &fsmc_ecc4_224_layout;
+                       host->ecc_place = &fsmc_ecc4_lp_place;
+                       break;
+               case 256:
+                       nand->ecc.layout = &fsmc_ecc4_256_layout;
+                       host->ecc_place = &fsmc_ecc4_lp_place;
+                       break;
+               default:
+                       printk(KERN_WARNING "No oob scheme defined for "
+                              "oobsize %d\n", mtd->oobsize);
+                       BUG();
                }
        } else {
-               nand->ecc.layout = &fsmc_ecc1_layout;
+               switch (host->mtd.oobsize) {
+               case 16:
+                       nand->ecc.layout = &fsmc_ecc1_16_layout;
+                       break;
+               case 64:
+                       nand->ecc.layout = &fsmc_ecc1_64_layout;
+                       break;
+               case 128:
+                       nand->ecc.layout = &fsmc_ecc1_128_layout;
+                       break;
+               default:
+                       printk(KERN_WARNING "No oob scheme defined for "
+                              "oobsize %d\n", mtd->oobsize);
+                       BUG();
+               }
        }
 
        /* Second stage of scan to fill MTD data-structures */
@@ -713,13 +942,13 @@ static int __init fsmc_nand_probe(struct platform_device *pdev)
         * Check for partition info passed
         */
        host->mtd.name = "nand";
-       ret = mtd_device_parse_register(&host->mtd, NULL, 0,
-                       host->mtd.size <= 0x04000000 ?
-                               partition_info_16KB_blk :
-                               partition_info_128KB_blk,
-                       host->mtd.size <= 0x04000000 ?
-                               ARRAY_SIZE(partition_info_16KB_blk) :
-                               ARRAY_SIZE(partition_info_128KB_blk));
+       ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
+                                       host->mtd.size <= 0x04000000 ?
+                                       partition_info_16KB_blk :
+                                       partition_info_128KB_blk,
+                                       host->mtd.size <= 0x04000000 ?
+                                       ARRAY_SIZE(partition_info_16KB_blk) :
+                                       ARRAY_SIZE(partition_info_128KB_blk));
        if (ret)
                goto err_probe;