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