blob: fd19787c9ce7b237ed3551cf9790f0799c27acdc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
franse6cf5df2008-08-15 23:14:31 +02002 * This file contains an ECC algorithm that detects and corrects 1 bit
3 * errors in a 256 byte block of data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * drivers/mtd/nand/nand_ecc.c
6 *
David Woodhouseccbcd6c2008-08-16 11:01:31 +01007 * Copyright © 2008 Koninklijke Philips Electronics NV.
8 * Author: Frans Meulenbroeks
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
franse6cf5df2008-08-15 23:14:31 +020010 * Completely replaces the previous ECC implementation which was written by:
11 * Steven J. Hill (sjhill@realitydiluted.com)
12 * Thomas Gleixner (tglx@linutronix.de)
13 *
14 * Information on how this algorithm works and how it was developed
David Woodhouseccbcd6c2008-08-16 11:01:31 +010015 * can be found in Documentation/mtd/nand_ecc.txt
Thomas Gleixner819d6a32006-05-23 11:32:45 +020016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 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
20 * later version.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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
25 * for more details.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
franse6cf5df2008-08-15 23:14:31 +020033/*
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.
David Woodhouseccbcd6c2008-08-16 11:01:31 +010038 * Instead a private definition of mtd_info is given to satisfy the compiler
franse6cf5df2008-08-15 23:14:31 +020039 * (the code does not use mtd_info, so the code does not care)
40 */
41#ifndef STANDALONE
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
43#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/mtd/nand_ecc.h>
frans1077be52008-08-20 21:11:50 +020046#include <asm/byteorder.h>
franse6cf5df2008-08-15 23:14:31 +020047#else
David Woodhouseccbcd6c2008-08-16 11:01:31 +010048#include <stdint.h>
49struct mtd_info;
franse6cf5df2008-08-15 23:14:31 +020050#define EXPORT_SYMBOL(x) /* x */
51
52#define MODULE_LICENSE(x) /* x */
53#define MODULE_AUTHOR(x) /* x */
54#define MODULE_DESCRIPTION(x) /* x */
frans1077be52008-08-20 21:11:50 +020055
56#define printk printf
57#define KERN_ERR ""
franse6cf5df2008-08-15 23:14:31 +020058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
franse6cf5df2008-08-15 23:14:31 +020061 * invparity is a 256 byte table that contains the odd parity
62 * for each byte. So if the number of bits in a byte is even,
63 * the array element is 1, and when the number of bits is odd
64 * the array eleemnt is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
franse6cf5df2008-08-15 23:14:31 +020066static const char invparity[256] = {
67 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
68 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
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 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
72 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
73 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
74 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
75 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
76 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
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 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
80 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
81 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
82 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
83};
84
85/*
86 * bitsperbyte contains the number of bits per byte
87 * this is only used for testing and repairing parity
88 * (a precalculated value slightly improves performance)
89 */
90static const char bitsperbyte[256] = {
91 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
92 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
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 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
96 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
97 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
98 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
99 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
100 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
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 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
104 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
105 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
106 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
107};
108
109/*
110 * addressbits is a lookup table to filter out the bits from the xor-ed
111 * ecc data that identify the faulty location.
112 * this is only used for repairing parity
113 * see the comments in nand_correct_data for more details
114 */
115static const char addressbits[256] = {
116 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
117 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
118 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
119 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
120 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
121 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
122 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
123 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
124 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
125 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
126 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
127 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
128 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
129 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
130 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
131 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
132 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
133 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
134 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
135 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
136 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
137 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
138 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
139 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
140 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
141 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
142 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
143 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
144 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
145 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
146 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
147 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/**
Randy Dunlap844d3b42006-06-28 21:48:27 -0700151 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
franse6cf5df2008-08-15 23:14:31 +0200152 * @mtd: MTD block structure (unused)
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100153 * @buf: input buffer with raw data
154 * @code: output buffer with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
franse6cf5df2008-08-15 23:14:31 +0200156int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
157 unsigned char *code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200159 int i;
franse6cf5df2008-08-15 23:14:31 +0200160 const uint32_t *bp = (uint32_t *)buf;
161 uint32_t cur; /* current value in buffer */
162 /* rp0..rp15 are the various accumulated parities (per byte) */
163 uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
164 uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;
165 uint32_t par; /* the cumulative parity for all data */
166 uint32_t tmppar; /* the cumulative parity for this iteration;
167 for rp12 and rp14 at the end of the loop */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000168
franse6cf5df2008-08-15 23:14:31 +0200169 par = 0;
170 rp4 = 0;
171 rp6 = 0;
172 rp8 = 0;
173 rp10 = 0;
174 rp12 = 0;
175 rp14 = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000176
franse6cf5df2008-08-15 23:14:31 +0200177 /*
178 * The loop is unrolled a number of times;
179 * This avoids if statements to decide on which rp value to update
180 * Also we process the data by longwords.
181 * Note: passing unaligned data might give a performance penalty.
182 * It is assumed that the buffers are aligned.
183 * tmppar is the cumulative sum of this iteration.
184 * needed for calculating rp12, rp14 and par
185 * also used as a performance improvement for rp6, rp8 and rp10
186 */
187 for (i = 0; i < 4; i++) {
188 cur = *bp++;
189 tmppar = cur;
190 rp4 ^= cur;
191 cur = *bp++;
192 tmppar ^= cur;
193 rp6 ^= tmppar;
194 cur = *bp++;
195 tmppar ^= cur;
196 rp4 ^= cur;
197 cur = *bp++;
198 tmppar ^= cur;
199 rp8 ^= tmppar;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000200
franse6cf5df2008-08-15 23:14:31 +0200201 cur = *bp++;
202 tmppar ^= cur;
203 rp4 ^= cur;
204 rp6 ^= cur;
205 cur = *bp++;
206 tmppar ^= cur;
207 rp6 ^= cur;
208 cur = *bp++;
209 tmppar ^= cur;
210 rp4 ^= cur;
211 cur = *bp++;
212 tmppar ^= cur;
213 rp10 ^= tmppar;
214
215 cur = *bp++;
216 tmppar ^= cur;
217 rp4 ^= cur;
218 rp6 ^= cur;
219 rp8 ^= cur;
220 cur = *bp++;
221 tmppar ^= cur;
222 rp6 ^= cur;
223 rp8 ^= cur;
224 cur = *bp++;
225 tmppar ^= cur;
226 rp4 ^= cur;
227 rp8 ^= cur;
228 cur = *bp++;
229 tmppar ^= cur;
230 rp8 ^= cur;
231
232 cur = *bp++;
233 tmppar ^= cur;
234 rp4 ^= cur;
235 rp6 ^= cur;
236 cur = *bp++;
237 tmppar ^= cur;
238 rp6 ^= cur;
239 cur = *bp++;
240 tmppar ^= cur;
241 rp4 ^= cur;
242 cur = *bp++;
243 tmppar ^= cur;
244
245 par ^= tmppar;
246 if ((i & 0x1) == 0)
247 rp12 ^= tmppar;
248 if ((i & 0x2) == 0)
249 rp14 ^= tmppar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000251
franse6cf5df2008-08-15 23:14:31 +0200252 /*
253 * handle the fact that we use longword operations
254 * we'll bring rp4..rp14 back to single byte entities by shifting and
255 * xoring first fold the upper and lower 16 bits,
256 * then the upper and lower 8 bits.
257 */
258 rp4 ^= (rp4 >> 16);
259 rp4 ^= (rp4 >> 8);
260 rp4 &= 0xff;
261 rp6 ^= (rp6 >> 16);
262 rp6 ^= (rp6 >> 8);
263 rp6 &= 0xff;
264 rp8 ^= (rp8 >> 16);
265 rp8 ^= (rp8 >> 8);
266 rp8 &= 0xff;
267 rp10 ^= (rp10 >> 16);
268 rp10 ^= (rp10 >> 8);
269 rp10 &= 0xff;
270 rp12 ^= (rp12 >> 16);
271 rp12 ^= (rp12 >> 8);
272 rp12 &= 0xff;
273 rp14 ^= (rp14 >> 16);
274 rp14 ^= (rp14 >> 8);
275 rp14 &= 0xff;
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200276
franse6cf5df2008-08-15 23:14:31 +0200277 /*
278 * we also need to calculate the row parity for rp0..rp3
279 * This is present in par, because par is now
frans1077be52008-08-20 21:11:50 +0200280 * rp3 rp3 rp2 rp2 in little endian and
281 * rp2 rp2 rp3 rp3 in big endian
franse6cf5df2008-08-15 23:14:31 +0200282 * as well as
frans1077be52008-08-20 21:11:50 +0200283 * rp1 rp0 rp1 rp0 in little endian and
284 * rp0 rp1 rp0 rp1 in big endian
franse6cf5df2008-08-15 23:14:31 +0200285 * First calculate rp2 and rp3
franse6cf5df2008-08-15 23:14:31 +0200286 */
frans1077be52008-08-20 21:11:50 +0200287#ifdef __BIG_ENDIAN
288 rp2 = (par >> 16);
289 rp2 ^= (rp2 >> 8);
290 rp2 &= 0xff;
291 rp3 = par & 0xffff;
292 rp3 ^= (rp3 >> 8);
293 rp3 &= 0xff;
294#else
franse6cf5df2008-08-15 23:14:31 +0200295 rp3 = (par >> 16);
296 rp3 ^= (rp3 >> 8);
297 rp3 &= 0xff;
298 rp2 = par & 0xffff;
299 rp2 ^= (rp2 >> 8);
300 rp2 &= 0xff;
frans1077be52008-08-20 21:11:50 +0200301#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000302
franse6cf5df2008-08-15 23:14:31 +0200303 /* reduce par to 16 bits then calculate rp1 and rp0 */
304 par ^= (par >> 16);
frans1077be52008-08-20 21:11:50 +0200305#ifdef __BIG_ENDIAN
306 rp0 = (par >> 8) & 0xff;
307 rp1 = (par & 0xff);
308#else
franse6cf5df2008-08-15 23:14:31 +0200309 rp1 = (par >> 8) & 0xff;
310 rp0 = (par & 0xff);
frans1077be52008-08-20 21:11:50 +0200311#endif
franse6cf5df2008-08-15 23:14:31 +0200312
313 /* finally reduce par to 8 bits */
314 par ^= (par >> 8);
315 par &= 0xff;
316
317 /*
318 * and calculate rp5..rp15
319 * note that par = rp4 ^ rp5 and due to the commutative property
320 * of the ^ operator we can say:
321 * rp5 = (par ^ rp4);
322 * The & 0xff seems superfluous, but benchmarking learned that
323 * leaving it out gives slightly worse results. No idea why, probably
324 * it has to do with the way the pipeline in pentium is organized.
325 */
326 rp5 = (par ^ rp4) & 0xff;
327 rp7 = (par ^ rp6) & 0xff;
328 rp9 = (par ^ rp8) & 0xff;
329 rp11 = (par ^ rp10) & 0xff;
330 rp13 = (par ^ rp12) & 0xff;
331 rp15 = (par ^ rp14) & 0xff;
332
333 /*
334 * Finally calculate the ecc bits.
335 * Again here it might seem that there are performance optimisations
336 * possible, but benchmarks showed that on the system this is developed
337 * the code below is the fastest
338 */
Timo Lindhorstfc029192006-11-27 13:35:49 +0100339#ifdef CONFIG_MTD_NAND_ECC_SMC
franse6cf5df2008-08-15 23:14:31 +0200340 code[0] =
341 (invparity[rp7] << 7) |
342 (invparity[rp6] << 6) |
343 (invparity[rp5] << 5) |
344 (invparity[rp4] << 4) |
345 (invparity[rp3] << 3) |
346 (invparity[rp2] << 2) |
347 (invparity[rp1] << 1) |
348 (invparity[rp0]);
349 code[1] =
350 (invparity[rp15] << 7) |
351 (invparity[rp14] << 6) |
352 (invparity[rp13] << 5) |
353 (invparity[rp12] << 4) |
354 (invparity[rp11] << 3) |
355 (invparity[rp10] << 2) |
356 (invparity[rp9] << 1) |
357 (invparity[rp8]);
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200358#else
franse6cf5df2008-08-15 23:14:31 +0200359 code[1] =
360 (invparity[rp7] << 7) |
361 (invparity[rp6] << 6) |
362 (invparity[rp5] << 5) |
363 (invparity[rp4] << 4) |
364 (invparity[rp3] << 3) |
365 (invparity[rp2] << 2) |
366 (invparity[rp1] << 1) |
367 (invparity[rp0]);
368 code[0] =
369 (invparity[rp15] << 7) |
370 (invparity[rp14] << 6) |
371 (invparity[rp13] << 5) |
372 (invparity[rp12] << 4) |
373 (invparity[rp11] << 3) |
374 (invparity[rp10] << 2) |
375 (invparity[rp9] << 1) |
376 (invparity[rp8]);
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200377#endif
franse6cf5df2008-08-15 23:14:31 +0200378 code[2] =
379 (invparity[par & 0xf0] << 7) |
380 (invparity[par & 0x0f] << 6) |
381 (invparity[par & 0xcc] << 5) |
382 (invparity[par & 0x33] << 4) |
383 (invparity[par & 0xaa] << 3) |
384 (invparity[par & 0x55] << 2) |
385 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387}
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200388EXPORT_SYMBOL(nand_calculate_ecc);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/**
391 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
franse6cf5df2008-08-15 23:14:31 +0200392 * @mtd: MTD block structure (unused)
Alexey Korolev17c1d2b2008-08-20 22:32:08 +0100393 * @buf: raw data read from the chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * @read_ecc: ECC from the chip
395 * @calc_ecc: the ECC calculated from raw data
396 *
397 * Detect and correct a 1 bit error for 256 byte block
398 */
franse6cf5df2008-08-15 23:14:31 +0200399int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
400 unsigned char *read_ecc, unsigned char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
franse6cf5df2008-08-15 23:14:31 +0200402 unsigned char b0, b1, b2;
403 unsigned char byte_addr, bit_addr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404
franse6cf5df2008-08-15 23:14:31 +0200405 /*
406 * b0 to b2 indicate which bit is faulty (if any)
407 * we might need the xor result more than once,
408 * so keep them in a local var
409 */
Timo Lindhorstfc029192006-11-27 13:35:49 +0100410#ifdef CONFIG_MTD_NAND_ECC_SMC
franse6cf5df2008-08-15 23:14:31 +0200411 b0 = read_ecc[0] ^ calc_ecc[0];
412 b1 = read_ecc[1] ^ calc_ecc[1];
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200413#else
franse6cf5df2008-08-15 23:14:31 +0200414 b0 = read_ecc[1] ^ calc_ecc[1];
415 b1 = read_ecc[0] ^ calc_ecc[0];
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200416#endif
franse6cf5df2008-08-15 23:14:31 +0200417 b2 = read_ecc[2] ^ calc_ecc[2];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000418
franse6cf5df2008-08-15 23:14:31 +0200419 /* check if there are any bitfaults */
Thomas Gleixner819d6a32006-05-23 11:32:45 +0200420
franse6cf5df2008-08-15 23:14:31 +0200421 /* repeated if statements are slightly more efficient than switch ... */
422 /* ordered in order of likelihood */
frans1077be52008-08-20 21:11:50 +0200423
424 if ((b0 | b1 | b2) == 0)
David Woodhouseccbcd6c2008-08-16 11:01:31 +0100425 return 0; /* no error */
frans1077be52008-08-20 21:11:50 +0200426
427 if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
428 (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
429 (((b2 ^ (b2 >> 1)) & 0x54) == 0x54)) { /* single bit error */
franse6cf5df2008-08-15 23:14:31 +0200430 /*
431 * rp15/13/11/9/7/5/3/1 indicate which byte is the faulty byte
432 * cp 5/3/1 indicate the faulty bit.
433 * A lookup table (called addressbits) is used to filter
434 * the bits from the byte they are in.
435 * A marginal optimisation is possible by having three
436 * different lookup tables.
437 * One as we have now (for b0), one for b2
438 * (that would avoid the >> 1), and one for b1 (with all values
439 * << 4). However it was felt that introducing two more tables
440 * hardly justify the gain.
441 *
442 * The b2 shift is there to get rid of the lowest two bits.
443 * We could also do addressbits[b2] >> 1 but for the
444 * performace it does not make any difference
445 */
446 byte_addr = (addressbits[b1] << 4) + addressbits[b0];
447 bit_addr = addressbits[b2 >> 2];
448 /* flip the bit */
449 buf[byte_addr] ^= (1 << bit_addr);
David Woodhouseccbcd6c2008-08-16 11:01:31 +0100450 return 1;
frans1077be52008-08-20 21:11:50 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
frans1077be52008-08-20 21:11:50 +0200453 /* count nr of bits; use table lookup, faster than calculating it */
454 if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
David Woodhouseccbcd6c2008-08-16 11:01:31 +0100455 return 1; /* error in ecc data; no action needed */
frans1077be52008-08-20 21:11:50 +0200456
457 printk(KERN_ERR "uncorrectable error : ");
franse6cf5df2008-08-15 23:14:31 +0200458 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460EXPORT_SYMBOL(nand_correct_data);
461
462MODULE_LICENSE("GPL");
franse6cf5df2008-08-15 23:14:31 +0200463MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464MODULE_DESCRIPTION("Generic NAND ECC support");