uboot/drivers/mtd/nand/raw/nand_bch.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * This file provides ECC correction for more than 1 bit per block of data,
   4 * using binary BCH codes. It relies on the generic BCH library lib/bch.c.
   5 *
   6 * Copyright © 2011 Ivan Djelic <ivan.djelic@parrot.com>
   7 *
   8 */
   9
  10#include <common.h>
  11#include <dm/devres.h>
  12/*#include <asm/io.h>*/
  13#include <linux/types.h>
  14
  15#include <linux/bitops.h>
  16#include <linux/mtd/mtd.h>
  17#include <linux/mtd/rawnand.h>
  18#include <linux/mtd/nand_bch.h>
  19#include <linux/bch.h>
  20#include <malloc.h>
  21
  22/**
  23 * struct nand_bch_control - private NAND BCH control structure
  24 * @bch:       BCH control structure
  25 * @ecclayout: private ecc layout for this BCH configuration
  26 * @errloc:    error location array
  27 * @eccmask:   XOR ecc mask, allows erased pages to be decoded as valid
  28 */
  29struct nand_bch_control {
  30        struct bch_control   *bch;
  31        struct nand_ecclayout ecclayout;
  32        unsigned int         *errloc;
  33        unsigned char        *eccmask;
  34};
  35
  36/**
  37 * nand_bch_calculate_ecc - [NAND Interface] Calculate ECC for data block
  38 * @mtd:        MTD block structure
  39 * @buf:        input buffer with raw data
  40 * @code:       output buffer with ECC
  41 */
  42int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  43                           unsigned char *code)
  44{
  45        const struct nand_chip *chip = mtd_to_nand(mtd);
  46        struct nand_bch_control *nbc = chip->ecc.priv;
  47        unsigned int i;
  48
  49        memset(code, 0, chip->ecc.bytes);
  50        encode_bch(nbc->bch, buf, chip->ecc.size, code);
  51
  52        /* apply mask so that an erased page is a valid codeword */
  53        for (i = 0; i < chip->ecc.bytes; i++)
  54                code[i] ^= nbc->eccmask[i];
  55
  56        return 0;
  57}
  58
  59/**
  60 * nand_bch_correct_data - [NAND Interface] Detect and correct bit error(s)
  61 * @mtd:        MTD block structure
  62 * @buf:        raw data read from the chip
  63 * @read_ecc:   ECC from the chip
  64 * @calc_ecc:   the ECC calculated from raw data
  65 *
  66 * Detect and correct bit errors for a data byte block
  67 */
  68int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  69                          unsigned char *read_ecc, unsigned char *calc_ecc)
  70{
  71        const struct nand_chip *chip = mtd_to_nand(mtd);
  72        struct nand_bch_control *nbc = chip->ecc.priv;
  73        unsigned int *errloc = nbc->errloc;
  74        int i, count;
  75
  76        count = decode_bch(nbc->bch, NULL, chip->ecc.size, read_ecc, calc_ecc,
  77                           NULL, errloc);
  78        if (count > 0) {
  79                for (i = 0; i < count; i++) {
  80                        if (errloc[i] < (chip->ecc.size*8))
  81                                /* error is located in data, correct it */
  82                                buf[errloc[i] >> 3] ^= (1 << (errloc[i] & 7));
  83                        /* else error in ecc, no action needed */
  84
  85                        pr_debug("%s: corrected bitflip %u\n",
  86                                 __func__, errloc[i]);
  87                }
  88        } else if (count < 0) {
  89                printk(KERN_ERR "ecc unrecoverable error\n");
  90                count = -EBADMSG;
  91        }
  92        return count;
  93}
  94
  95/**
  96 * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  97 * @mtd:        MTD block structure
  98 *
  99 * Returns:
 100 *  a pointer to a new NAND BCH control structure, or NULL upon failure
 101 *
 102 * Initialize NAND BCH error correction. Parameters @eccsize and @eccbytes
 103 * are used to compute BCH parameters m (Galois field order) and t (error
 104 * correction capability). @eccbytes should be equal to the number of bytes
 105 * required to store m*t bits, where m is such that 2^m-1 > @eccsize*8.
 106 *
 107 * Example: to configure 4 bit correction per 512 bytes, you should pass
 108 * @eccsize = 512  (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
 109 * @eccbytes = 7   (7 bytes are required to store m*t = 13*4 = 52 bits)
 110 */
 111struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
 112{
 113        struct nand_chip *nand = mtd_to_nand(mtd);
 114        unsigned int m, t, eccsteps, i;
 115        struct nand_ecclayout *layout = nand->ecc.layout;
 116        struct nand_bch_control *nbc = NULL;
 117        unsigned char *erased_page;
 118        unsigned int eccsize = nand->ecc.size;
 119        unsigned int eccbytes = nand->ecc.bytes;
 120        unsigned int eccstrength = nand->ecc.strength;
 121
 122        if (!eccbytes && eccstrength) {
 123                eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
 124                nand->ecc.bytes = eccbytes;
 125        }
 126
 127        if (!eccsize || !eccbytes) {
 128                printk(KERN_WARNING "ecc parameters not supplied\n");
 129                goto fail;
 130        }
 131
 132        m = fls(1+8*eccsize);
 133        t = (eccbytes*8)/m;
 134
 135        nbc = kzalloc(sizeof(*nbc), GFP_KERNEL);
 136        if (!nbc)
 137                goto fail;
 138
 139        nbc->bch = init_bch(m, t, 0);
 140        if (!nbc->bch)
 141                goto fail;
 142
 143        /* verify that eccbytes has the expected value */
 144        if (nbc->bch->ecc_bytes != eccbytes) {
 145                printk(KERN_WARNING "invalid eccbytes %u, should be %u\n",
 146                       eccbytes, nbc->bch->ecc_bytes);
 147                goto fail;
 148        }
 149
 150        eccsteps = mtd->writesize/eccsize;
 151
 152        /* if no ecc placement scheme was provided, build one */
 153        if (!layout) {
 154
 155                /* handle large page devices only */
 156                if (mtd->oobsize < 64) {
 157                        printk(KERN_WARNING "must provide an oob scheme for "
 158                               "oobsize %d\n", mtd->oobsize);
 159                        goto fail;
 160                }
 161
 162                layout = &nbc->ecclayout;
 163                layout->eccbytes = eccsteps*eccbytes;
 164
 165                /* reserve 2 bytes for bad block marker */
 166                if (layout->eccbytes+2 > mtd->oobsize) {
 167                        printk(KERN_WARNING "no suitable oob scheme available "
 168                               "for oobsize %d eccbytes %u\n", mtd->oobsize,
 169                               eccbytes);
 170                        goto fail;
 171                }
 172                /* put ecc bytes at oob tail */
 173                for (i = 0; i < layout->eccbytes; i++)
 174                        layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
 175
 176                layout->oobfree[0].offset = 2;
 177                layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
 178
 179                nand->ecc.layout = layout;
 180        }
 181
 182        /* sanity checks */
 183        if (8*(eccsize+eccbytes) >= (1 << m)) {
 184                printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
 185                goto fail;
 186        }
 187        if (layout->eccbytes != (eccsteps*eccbytes)) {
 188                printk(KERN_WARNING "invalid ecc layout\n");
 189                goto fail;
 190        }
 191
 192        nbc->eccmask = kmalloc(eccbytes, GFP_KERNEL);
 193        nbc->errloc = kmalloc(t*sizeof(*nbc->errloc), GFP_KERNEL);
 194        if (!nbc->eccmask || !nbc->errloc)
 195                goto fail;
 196        /*
 197         * compute and store the inverted ecc of an erased ecc block
 198         */
 199        erased_page = kmalloc(eccsize, GFP_KERNEL);
 200        if (!erased_page)
 201                goto fail;
 202
 203        memset(erased_page, 0xff, eccsize);
 204        memset(nbc->eccmask, 0, eccbytes);
 205        encode_bch(nbc->bch, erased_page, eccsize, nbc->eccmask);
 206        kfree(erased_page);
 207
 208        for (i = 0; i < eccbytes; i++)
 209                nbc->eccmask[i] ^= 0xff;
 210
 211        if (!eccstrength)
 212                nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
 213
 214        return nbc;
 215fail:
 216        nand_bch_free(nbc);
 217        return NULL;
 218}
 219
 220/**
 221 * nand_bch_free - [NAND Interface] Release NAND BCH ECC resources
 222 * @nbc:        NAND BCH control structure
 223 */
 224void nand_bch_free(struct nand_bch_control *nbc)
 225{
 226        if (nbc) {
 227                free_bch(nbc->bch);
 228                kfree(nbc->errloc);
 229                kfree(nbc->eccmask);
 230                kfree(nbc);
 231        }
 232}
 233