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