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