Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 2 | * This file contains an ECC algorithm that detects and corrects 1 bit |
| 3 | * errors in a 256 byte block of data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * drivers/mtd/nand/nand_ecc.c |
| 6 | * |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 7 | * Copyright (C) 2008 Koninklijke Philips Electronics NV. |
| 8 | * Author: Frans Meulenbroeks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 10 | * 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 |
| 15 | * can be found in Documentation/nand/ecc.txt |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 20 | * later version. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 25 | * for more details. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 26 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 30 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | */ |
| 32 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 33 | /* |
| 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) |
| 40 | */ |
| 41 | #ifndef STANDALONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/types.h> |
| 43 | #include <linux/kernel.h> |
| 44 | #include <linux/module.h> |
| 45 | #include <linux/mtd/nand_ecc.h> |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 46 | #else |
| 47 | typedef uint32_t unsigned long |
| 48 | struct mtd_info { |
| 49 | int dummy; |
| 50 | }; |
| 51 | #define EXPORT_SYMBOL(x) /* x */ |
| 52 | |
| 53 | #define MODULE_LICENSE(x) /* x */ |
| 54 | #define MODULE_AUTHOR(x) /* x */ |
| 55 | #define MODULE_DESCRIPTION(x) /* x */ |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 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 |
| 81 | }; |
| 82 | |
| 83 | /* |
| 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) |
| 87 | */ |
| 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, |
| 105 | }; |
| 106 | |
| 107 | /* |
| 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 |
| 112 | */ |
| 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /** |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 149 | * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 150 | * @mtd: MTD block structure (unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * @dat: raw data |
| 152 | * @ecc_code: buffer for ECC |
| 153 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 154 | int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf, |
| 155 | unsigned char *code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 157 | int i; |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 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 */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 166 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 167 | par = 0; |
| 168 | rp4 = 0; |
| 169 | rp6 = 0; |
| 170 | rp8 = 0; |
| 171 | rp10 = 0; |
| 172 | rp12 = 0; |
| 173 | rp14 = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 174 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 175 | /* |
| 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 |
| 184 | */ |
| 185 | for (i = 0; i < 4; i++) { |
| 186 | cur = *bp++; |
| 187 | tmppar = cur; |
| 188 | rp4 ^= cur; |
| 189 | cur = *bp++; |
| 190 | tmppar ^= cur; |
| 191 | rp6 ^= tmppar; |
| 192 | cur = *bp++; |
| 193 | tmppar ^= cur; |
| 194 | rp4 ^= cur; |
| 195 | cur = *bp++; |
| 196 | tmppar ^= cur; |
| 197 | rp8 ^= tmppar; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 198 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 199 | cur = *bp++; |
| 200 | tmppar ^= cur; |
| 201 | rp4 ^= cur; |
| 202 | rp6 ^= cur; |
| 203 | cur = *bp++; |
| 204 | tmppar ^= cur; |
| 205 | rp6 ^= cur; |
| 206 | cur = *bp++; |
| 207 | tmppar ^= cur; |
| 208 | rp4 ^= cur; |
| 209 | cur = *bp++; |
| 210 | tmppar ^= cur; |
| 211 | rp10 ^= tmppar; |
| 212 | |
| 213 | cur = *bp++; |
| 214 | tmppar ^= cur; |
| 215 | rp4 ^= cur; |
| 216 | rp6 ^= cur; |
| 217 | rp8 ^= cur; |
| 218 | cur = *bp++; |
| 219 | tmppar ^= cur; |
| 220 | rp6 ^= cur; |
| 221 | rp8 ^= cur; |
| 222 | cur = *bp++; |
| 223 | tmppar ^= cur; |
| 224 | rp4 ^= cur; |
| 225 | rp8 ^= cur; |
| 226 | cur = *bp++; |
| 227 | tmppar ^= cur; |
| 228 | rp8 ^= cur; |
| 229 | |
| 230 | cur = *bp++; |
| 231 | tmppar ^= cur; |
| 232 | rp4 ^= cur; |
| 233 | rp6 ^= cur; |
| 234 | cur = *bp++; |
| 235 | tmppar ^= cur; |
| 236 | rp6 ^= cur; |
| 237 | cur = *bp++; |
| 238 | tmppar ^= cur; |
| 239 | rp4 ^= cur; |
| 240 | cur = *bp++; |
| 241 | tmppar ^= cur; |
| 242 | |
| 243 | par ^= tmppar; |
| 244 | if ((i & 0x1) == 0) |
| 245 | rp12 ^= tmppar; |
| 246 | if ((i & 0x2) == 0) |
| 247 | rp14 ^= tmppar; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 249 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 250 | /* |
| 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. |
| 255 | */ |
| 256 | rp4 ^= (rp4 >> 16); |
| 257 | rp4 ^= (rp4 >> 8); |
| 258 | rp4 &= 0xff; |
| 259 | rp6 ^= (rp6 >> 16); |
| 260 | rp6 ^= (rp6 >> 8); |
| 261 | rp6 &= 0xff; |
| 262 | rp8 ^= (rp8 >> 16); |
| 263 | rp8 ^= (rp8 >> 8); |
| 264 | rp8 &= 0xff; |
| 265 | rp10 ^= (rp10 >> 16); |
| 266 | rp10 ^= (rp10 >> 8); |
| 267 | rp10 &= 0xff; |
| 268 | rp12 ^= (rp12 >> 16); |
| 269 | rp12 ^= (rp12 >> 8); |
| 270 | rp12 &= 0xff; |
| 271 | rp14 ^= (rp14 >> 16); |
| 272 | rp14 ^= (rp14 >> 8); |
| 273 | rp14 &= 0xff; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 274 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 275 | /* |
| 276 | * we also need to calculate the row parity for rp0..rp3 |
| 277 | * This is present in par, because par is now |
| 278 | * rp3 rp3 rp2 rp2 |
| 279 | * as well as |
| 280 | * rp1 rp0 rp1 rp0 |
| 281 | * First calculate rp2 and rp3 |
| 282 | * (and yes: rp2 = (par ^ rp3) & 0xff; but doing that did not |
| 283 | * give a performance improvement) |
| 284 | */ |
| 285 | rp3 = (par >> 16); |
| 286 | rp3 ^= (rp3 >> 8); |
| 287 | rp3 &= 0xff; |
| 288 | rp2 = par & 0xffff; |
| 289 | rp2 ^= (rp2 >> 8); |
| 290 | rp2 &= 0xff; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 291 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 292 | /* reduce par to 16 bits then calculate rp1 and rp0 */ |
| 293 | par ^= (par >> 16); |
| 294 | rp1 = (par >> 8) & 0xff; |
| 295 | rp0 = (par & 0xff); |
| 296 | |
| 297 | /* finally reduce par to 8 bits */ |
| 298 | par ^= (par >> 8); |
| 299 | par &= 0xff; |
| 300 | |
| 301 | /* |
| 302 | * and calculate rp5..rp15 |
| 303 | * note that par = rp4 ^ rp5 and due to the commutative property |
| 304 | * of the ^ operator we can say: |
| 305 | * rp5 = (par ^ rp4); |
| 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. |
| 309 | */ |
| 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; |
| 316 | |
| 317 | /* |
| 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 |
| 322 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 323 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 324 | code[0] = |
| 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) | |
| 332 | (invparity[rp0]); |
| 333 | code[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) | |
| 341 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 342 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 343 | code[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) | |
| 351 | (invparity[rp0]); |
| 352 | code[0] = |
| 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) | |
| 360 | (invparity[rp8]); |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 361 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 362 | code[2] = |
| 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) | |
| 369 | 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | } |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 372 | EXPORT_SYMBOL(nand_calculate_ecc); |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /** |
| 375 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 376 | * @mtd: MTD block structure (unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | * @dat: raw data read from the chip |
| 378 | * @read_ecc: ECC from the chip |
| 379 | * @calc_ecc: the ECC calculated from raw data |
| 380 | * |
| 381 | * Detect and correct a 1 bit error for 256 byte block |
| 382 | */ |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 383 | int nand_correct_data(struct mtd_info *mtd, unsigned char *buf, |
| 384 | unsigned char *read_ecc, unsigned char *calc_ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 386 | int nr_bits; |
| 387 | unsigned char b0, b1, b2; |
| 388 | unsigned char byte_addr, bit_addr; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 389 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 390 | /* |
| 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 |
| 394 | */ |
Timo Lindhorst | fc02919 | 2006-11-27 13:35:49 +0100 | [diff] [blame] | 395 | #ifdef CONFIG_MTD_NAND_ECC_SMC |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 396 | b0 = read_ecc[0] ^ calc_ecc[0]; |
| 397 | b1 = read_ecc[1] ^ calc_ecc[1]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 398 | #else |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 399 | b0 = read_ecc[1] ^ calc_ecc[1]; |
| 400 | b1 = read_ecc[0] ^ calc_ecc[0]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 401 | #endif |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 402 | b2 = read_ecc[2] ^ calc_ecc[2]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 403 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 404 | /* check if there are any bitfaults */ |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 405 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 406 | /* count nr of bits; use table lookup, faster than calculating it */ |
| 407 | nr_bits = bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]; |
Thomas Gleixner | 819d6a3 | 2006-05-23 11:32:45 +0200 | [diff] [blame] | 408 | |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 409 | /* repeated if statements are slightly more efficient than switch ... */ |
| 410 | /* ordered in order of likelihood */ |
| 411 | if (nr_bits == 0) |
| 412 | return (0); /* no error */ |
| 413 | if (nr_bits == 11) { /* correctable error */ |
| 414 | /* |
| 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. |
| 425 | * |
| 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 |
| 429 | */ |
| 430 | byte_addr = (addressbits[b1] << 4) + addressbits[b0]; |
| 431 | bit_addr = addressbits[b2 >> 2]; |
| 432 | /* flip the bit */ |
| 433 | buf[byte_addr] ^= (1 << bit_addr); |
| 434 | return (1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 436 | if (nr_bits == 1) |
| 437 | return (1); /* error in ecc data; no action needed */ |
| 438 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | EXPORT_SYMBOL(nand_correct_data); |
| 441 | |
| 442 | MODULE_LICENSE("GPL"); |
frans | e6cf5df | 2008-08-15 23:14:31 +0200 | [diff] [blame^] | 443 | MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | MODULE_DESCRIPTION("Generic NAND ECC support"); |