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