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