linux/drivers/mtd/nand/raw/nand_bch.c
<<
>>
Prefs
   1/*
   2 * This file provides ECC correction for more than 1 bit per block of data,
   3 * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
   4 *
   5 * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
   6 *
   7 * This file is free software; you can redistribute it and/or modify it
   8 * under the terms of the GNU General Public License as published by the
   9 * Free Software Foundation; either version 2 or (at your option) any
  10 * later version.
  11 *
  12 * This file is distributed in the hope that it will be useful, but WITHOUT
  13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15 * for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License along
  18 * with this file; if not, write to the Free Software Foundation, Inc.,
  19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20 */
  21
  22#include <linux/types.h>
  23#include <linux/kernel.h>
  24#include <linux/module.h>
  25#include <linux/slab.h>
  26#include <linux/bitops.h>
  27#include <linux/mtd/mtd.h>
  28#include <linux/mtd/rawnand.h>
  29#include <linux/mtd/nand_bch.h>
  30#include <linux/bch.h>
  31
  32/**
  33 * struct nand_bch_control - private NAND BCH control structure
  34 * @bch:       BCH control structure
  35 * @errloc:    error location array
  36 * @eccmask:   XOR ecc mask, allows erased pages to be decoded as valid
  37 */
  38struct nand_bch_control {
  39        struct bch_control   *bch;
  40        unsigned int         *errloc;
  41        unsigned char        *eccmask;
  42};
  43
  44/**
  45 * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
  46 * @chip:       NAND chip object
  47 * @buf:        input buffer with raw data
  48 * @code:       output buffer with ECC
  49 */
  50int nand_bch_calculate_ecc(struct nand_chip *chip, const unsigned char *buf,
  51                           unsigned char *code)
  52{
  53        struct nand_bch_control *nbc = chip->ecc.priv;
  54        unsigned int i;
  55
  56        memset(code, 0, chip->ecc.bytes);
  57        encode_bch(nbc->bch, buf, chip->ecc.size, code);
  58
  59        /* apply mask so that an erased page is a valid codeword */
  60        for (i = 0; i < chip->ecc.bytes; i++)
  61                code[i] ^= nbc->eccmask[i];
  62
  63        return 0;
  64}
  65EXPORT_SYMBOL(nand_bch_calculate_ecc);
  66
  67/**
  68 * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  69 * @chip:       NAND chip object
  70 * @buf:        raw data read from the chip
  71 * @read_ecc:   ECC from the chip
  72 * @calc_ecc:   the ECC calculated from raw data
  73 *
  74 * Detect and correct bit errors for a data byte block
  75 */
  76int nand_bch_correct_data(struct nand_chip *chip, unsigned char *buf,
  77                          unsigned char *read_ecc, unsigned char *calc_ecc)
  78{
  79        struct nand_bch_control *nbc = chip->ecc.priv;
  80        unsigned int *errloc = nbc->errloc;
  81        int i, count;
  82
  83        count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  84                           NULL, errloc);
  85        if (count > 0) {
  86                for (i = 0; i < count; i++) {
  87                        if (errloc[i] < (chip->ecc.size*8))
  88                                /* error is located in data, correct it */
  89                                buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  90                        /* else error in ecc, no action needed */
  91
  92                        pr_debug("%s: corrected bitflip %u\n", __func__,
  93                                        errloc[i]);
  94                }
  95        } else if (count < 0) {
  96                pr_err("ecc unrecoverable error\n");
  97                count = -EBADMSG;
  98        }
  99        return count;
 100}
 101EXPORT_SYMBOL(nand_bch_correct_data);
 102
 103/**
 104 * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
 105 * @mtd:        MTD block structure
 106 *
 107 * Returns:
 108 *  a pointer to a new NAND BCH control structure, or NULL upon failure
 109 *
 110 * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
 111 * are used to compute BCH parameters m (Galois field order) and t (error
 112 * correction capability). @eccbytes should be equal to the number of bytes
 113 * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
 114 *
 115 * Example: to configure 4 bit correction per 512 bytes, you should pass
 116 * @eccsize = 512  (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
 117 * @eccbytes = 7   (7 bytes are required to store m*t = 13*4 = 52 bits)
 118 */
 119struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
 120{
 121        struct nand_chip *nand = mtd_to_nand(mtd);
 122        unsigned int m, t, eccsteps, i;
 123        struct nand_bch_control *nbc = NULL;
 124        unsigned char *erased_page;
 125        unsigned int eccsize = nand->ecc.size;
 126        unsigned int eccbytes = nand->ecc.bytes;
 127        unsigned int eccstrength = nand->ecc.strength;
 128
 129        if (!eccbytes && eccstrength) {
 130                eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
 131                nand->ecc.bytes = eccbytes;
 132        }
 133
 134        if (!eccsize || !eccbytes) {
 135                pr_warn("ecc parameters not supplied\n");
 136                goto fail;
 137        }
 138
 139        m = fls(1+8*eccsize);
 140        t = (eccbytes*8)/m;
 141
 142        nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
 143        if (!nbc)
 144                goto fail;
 145
 146        nbc->bch = init_bch(m, t, 0);
 147        if (!nbc->bch)
 148                goto fail;
 149
 150        /* verify that eccbytes has the expected value */
 151        if (nbc->bch->ecc_bytes != eccbytes) {
 152                pr_warn("invalid eccbytes %u, should be %u\n",
 153                        eccbytes, nbc->bch->ecc_bytes);
 154                goto fail;
 155        }
 156
 157        eccsteps = mtd->writesize/eccsize;
 158
 159        /* Check that we have an oob layout description. */
 160        if (!mtd->ooblayout) {
 161                pr_warn("missing oob scheme");
 162                goto fail;
 163        }
 164
 165        /* sanity checks */
 166        if (8*(eccsize+eccbytes) >= (1 << m)) {
 167                pr_warn("eccsize %u is too large\n", eccsize);
 168                goto fail;
 169        }
 170
 171        /*
 172         * ecc->steps and ecc->total might be used by mtd->ooblayout->ecc(),
 173         * which is called by mtd_ooblayout_count_eccbytes().
 174         * Make sure they are properly initialized before calling
 175         * mtd_ooblayout_count_eccbytes().
 176         * FIXME: we should probably rework the sequencing in nand_scan_tail()
 177         * to avoid setting those fields twice.
 178         */
 179        nand->ecc.steps = eccsteps;
 180        nand->ecc.total = eccsteps * eccbytes;
 181        if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) {
 182                pr_warn("invalid ecc layout\n");
 183                goto fail;
 184        }
 185
 186        nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
 187        nbc->errloc = kmalloc_array(t, sizeof(*nbc->errloc), GFP_KERNEL);
 188        if (!nbc->eccmask || !nbc->errloc)
 189                goto fail;
 190        /*
 191         * compute and store the inverted ecc of an erased ecc block
 192         */
 193        erased_page = kmalloc(eccsize, GFP_KERNEL);
 194        if (!erased_page)
 195                goto fail;
 196
 197        memset(erased_page, 0xff, eccsize);
 198        memset(nbc->eccmask, 0, eccbytes);
 199        encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
 200        kfree(erased_page);
 201
 202        for (i = 0; i < eccbytes; i++)
 203                nbc->eccmask[i] ^= 0xff;
 204
 205        if (!eccstrength)
 206                nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
 207
 208        return nbc;
 209fail:
 210        nand_bch_free(nbc);
 211        return NULL;
 212}
 213EXPORT_SYMBOL(nand_bch_init);
 214
 215/**
 216 * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
 217 * @nbc:        NAND BCH control structure
 218 */
 219void nand_bch_free(struct nand_bch_control *nbc)
 220{
 221        if (nbc) {
 222                free_bch(nbc->bch);
 223                kfree(nbc->errloc);
 224                kfree(nbc->eccmask);
 225                kfree(nbc);
 226        }
 227}
 228EXPORT_SYMBOL(nand_bch_free);
 229
 230MODULE_LICENSE("GPL");
 231MODULE_AUTHOR("Ivan Djelic <ivan.djelic@parrot.com>");
 232MODULE_DESCRIPTION("NAND software BCH ECC support");
 233