2 * This file contains an ECC algorithm that detects and corrects 1 bit
3 * errors in a 256 byte block of data.
5 * drivers/mtd/nand/nand_ecc.c
7 * Copyright (C) 2008 Koninklijke Philips Electronics NV.
8 * Author: Frans Meulenbroeks
10 * Completely replaces the previous ECC implementation which was written by:
11 * Steven J. Hill (sjhill@realitydiluted.com)
12 * Thomas Gleixner (tglx@linutronix.de)
14 * Information on how this algorithm works and how it was developed
15 * can be found in Documentation/nand/ecc.txt
17 * This file is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 or (at your option) any
22 * This file is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License along
28 * with this file; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
34 * The STANDALONE macro is useful when running the code outside the kernel
35 * e.g. when running the code in a testbed or a benchmark program.
36 * When STANDALONE is used, the module related macros are commented out
37 * as well as the linux include files.
38 * Instead a private definition of mtd_into is given to satisfy the compiler
39 * (the code does not use mtd_info, so the code does not care)
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/mtd/nand_ecc.h>
47 typedef uint32_t unsigned long
51 #define EXPORT_SYMBOL(x) /* x */
53 #define MODULE_LICENSE(x) /* x */
54 #define MODULE_AUTHOR(x) /* x */
55 #define MODULE_DESCRIPTION(x) /* x */
59 * invparity is a 256 byte table that contains the odd parity
60 * for each byte. So if the number of bits in a byte is even,
61 * the array element is 1, and when the number of bits is odd
62 * the array eleemnt is 0.
64 static const char invparity[256] = {
65 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
66 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
67 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
68 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
69 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
70 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
71 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
72 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
73 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
74 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
75 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
76 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
77 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
78 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
79 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
80 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
84 * bitsperbyte contains the number of bits per byte
85 * this is only used for testing and repairing parity
86 * (a precalculated value slightly improves performance)
88 static const char bitsperbyte[256] = {
89 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
90 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
91 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
92 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
93 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
94 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
95 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
96 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
97 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
98 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
99 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
100 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
101 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
102 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
103 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
104 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
108 * addressbits is a lookup table to filter out the bits from the xor-ed
109 * ecc data that identify the faulty location.
110 * this is only used for repairing parity
111 * see the comments in nand_correct_data for more details
113 static const char addressbits[256] = {
114 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
115 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
116 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
117 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
118 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
119 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
120 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
121 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
122 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
123 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
124 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
125 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
126 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
127 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
128 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
129 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
130 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
131 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
132 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
133 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
134 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
135 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
136 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
137 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
138 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
139 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
140 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
141 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
142 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
143 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
144 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
145 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
149 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
150 * @mtd: MTD block structure (unused)
152 * @ecc_code: buffer for ECC
154 int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
158 const uint32_t *bp = (uint32_t *)buf;
159 uint32_t cur; /* current value in buffer */
160 /* rp0..rp15 are the various accumulated parities (per byte) */
161 uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
162 uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;
163 uint32_t par; /* the cumulative parity for all data */
164 uint32_t tmppar; /* the cumulative parity for this iteration;
165 for rp12 and rp14 at the end of the loop */
176 * The loop is unrolled a number of times;
177 * This avoids if statements to decide on which rp value to update
178 * Also we process the data by longwords.
179 * Note: passing unaligned data might give a performance penalty.
180 * It is assumed that the buffers are aligned.
181 * tmppar is the cumulative sum of this iteration.
182 * needed for calculating rp12, rp14 and par
183 * also used as a performance improvement for rp6, rp8 and rp10
185 for (i = 0; i < 4; i++) {
251 * handle the fact that we use longword operations
252 * we'll bring rp4..rp14 back to single byte entities by shifting and
253 * xoring first fold the upper and lower 16 bits,
254 * then the upper and lower 8 bits.
265 rp10 ^= (rp10 >> 16);
268 rp12 ^= (rp12 >> 16);
271 rp14 ^= (rp14 >> 16);
276 * we also need to calculate the row parity for rp0..rp3
277 * This is present in par, because par is now
281 * First calculate rp2 and rp3
282 * (and yes: rp2 = (par ^ rp3) & 0xff; but doing that did not
283 * give a performance improvement)
292 /* reduce par to 16 bits then calculate rp1 and rp0 */
294 rp1 = (par >> 8) & 0xff;
297 /* finally reduce par to 8 bits */
302 * and calculate rp5..rp15
303 * note that par = rp4 ^ rp5 and due to the commutative property
304 * of the ^ operator we can say:
306 * The & 0xff seems superfluous, but benchmarking learned that
307 * leaving it out gives slightly worse results. No idea why, probably
308 * it has to do with the way the pipeline in pentium is organized.
310 rp5 = (par ^ rp4) & 0xff;
311 rp7 = (par ^ rp6) & 0xff;
312 rp9 = (par ^ rp8) & 0xff;
313 rp11 = (par ^ rp10) & 0xff;
314 rp13 = (par ^ rp12) & 0xff;
315 rp15 = (par ^ rp14) & 0xff;
318 * Finally calculate the ecc bits.
319 * Again here it might seem that there are performance optimisations
320 * possible, but benchmarks showed that on the system this is developed
321 * the code below is the fastest
323 #ifdef CONFIG_MTD_NAND_ECC_SMC
325 (invparity[rp7] << 7) |
326 (invparity[rp6] << 6) |
327 (invparity[rp5] << 5) |
328 (invparity[rp4] << 4) |
329 (invparity[rp3] << 3) |
330 (invparity[rp2] << 2) |
331 (invparity[rp1] << 1) |
334 (invparity[rp15] << 7) |
335 (invparity[rp14] << 6) |
336 (invparity[rp13] << 5) |
337 (invparity[rp12] << 4) |
338 (invparity[rp11] << 3) |
339 (invparity[rp10] << 2) |
340 (invparity[rp9] << 1) |
344 (invparity[rp7] << 7) |
345 (invparity[rp6] << 6) |
346 (invparity[rp5] << 5) |
347 (invparity[rp4] << 4) |
348 (invparity[rp3] << 3) |
349 (invparity[rp2] << 2) |
350 (invparity[rp1] << 1) |
353 (invparity[rp15] << 7) |
354 (invparity[rp14] << 6) |
355 (invparity[rp13] << 5) |
356 (invparity[rp12] << 4) |
357 (invparity[rp11] << 3) |
358 (invparity[rp10] << 2) |
359 (invparity[rp9] << 1) |
363 (invparity[par & 0xf0] << 7) |
364 (invparity[par & 0x0f] << 6) |
365 (invparity[par & 0xcc] << 5) |
366 (invparity[par & 0x33] << 4) |
367 (invparity[par & 0xaa] << 3) |
368 (invparity[par & 0x55] << 2) |
372 EXPORT_SYMBOL(nand_calculate_ecc);
375 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
376 * @mtd: MTD block structure (unused)
377 * @dat: raw data read from the chip
378 * @read_ecc: ECC from the chip
379 * @calc_ecc: the ECC calculated from raw data
381 * Detect and correct a 1 bit error for 256 byte block
383 int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
384 unsigned char *read_ecc, unsigned char *calc_ecc)
387 unsigned char b0, b1, b2;
388 unsigned char byte_addr, bit_addr;
391 * b0 to b2 indicate which bit is faulty (if any)
392 * we might need the xor result more than once,
393 * so keep them in a local var
395 #ifdef CONFIG_MTD_NAND_ECC_SMC
396 b0 = read_ecc[0] ^ calc_ecc[0];
397 b1 = read_ecc[1] ^ calc_ecc[1];
399 b0 = read_ecc[1] ^ calc_ecc[1];
400 b1 = read_ecc[0] ^ calc_ecc[0];
402 b2 = read_ecc[2] ^ calc_ecc[2];
404 /* check if there are any bitfaults */
406 /* count nr of bits; use table lookup, faster than calculating it */
407 nr_bits = bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2];
409 /* repeated if statements are slightly more efficient than switch ... */
410 /* ordered in order of likelihood */
412 return (0); /* no error */
413 if (nr_bits == 11) { /* correctable error */
415 * rp15/13/11/9/7/5/3/1 indicate which byte is the faulty byte
416 * cp 5/3/1 indicate the faulty bit.
417 * A lookup table (called addressbits) is used to filter
418 * the bits from the byte they are in.
419 * A marginal optimisation is possible by having three
420 * different lookup tables.
421 * One as we have now (for b0), one for b2
422 * (that would avoid the >> 1), and one for b1 (with all values
423 * << 4). However it was felt that introducing two more tables
424 * hardly justify the gain.
426 * The b2 shift is there to get rid of the lowest two bits.
427 * We could also do addressbits[b2] >> 1 but for the
428 * performace it does not make any difference
430 byte_addr = (addressbits[b1] << 4) + addressbits[b0];
431 bit_addr = addressbits[b2 >> 2];
433 buf[byte_addr] ^= (1 << bit_addr);
437 return (1); /* error in ecc data; no action needed */
440 EXPORT_SYMBOL(nand_correct_data);
442 MODULE_LICENSE("GPL");
443 MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
444 MODULE_DESCRIPTION("Generic NAND ECC support");