diff options
Diffstat (limited to 'drivers/mtd/nand/nand_ecc.c')
| -rw-r--r-- | drivers/mtd/nand/nand_ecc.c | 227 |
1 files changed, 88 insertions, 139 deletions
diff --git a/drivers/mtd/nand/nand_ecc.c b/drivers/mtd/nand/nand_ecc.c index 40ac909150a3..2a163e4084df 100644 --- a/drivers/mtd/nand/nand_ecc.c +++ b/drivers/mtd/nand/nand_ecc.c | |||
| @@ -7,6 +7,8 @@ | |||
| 7 | * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com) | 7 | * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com) |
| 8 | * Toshiba America Electronics Components, Inc. | 8 | * Toshiba America Electronics Components, Inc. |
| 9 | * | 9 | * |
| 10 | * Copyright (C) 2006 Thomas Gleixner <tglx@linutronix.de> | ||
| 11 | * | ||
| 10 | * $Id: nand_ecc.c,v 1.15 2005/11/07 11:14:30 gleixner Exp $ | 12 | * $Id: nand_ecc.c,v 1.15 2005/11/07 11:14:30 gleixner Exp $ |
| 11 | * | 13 | * |
| 12 | * This file is free software; you can redistribute it and/or modify it | 14 | * This file is free software; you can redistribute it and/or modify it |
| @@ -62,90 +64,76 @@ static const u_char nand_ecc_precalc_table[] = { | |||
| 62 | 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00 | 64 | 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00 |
| 63 | }; | 65 | }; |
| 64 | 66 | ||
| 65 | |||
| 66 | /** | 67 | /** |
| 67 | * nand_trans_result - [GENERIC] create non-inverted ECC | 68 | * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code |
| 68 | * @reg2: line parity reg 2 | 69 | * for 256 byte block |
| 69 | * @reg3: line parity reg 3 | ||
| 70 | * @ecc_code: ecc | ||
| 71 | * | ||
| 72 | * Creates non-inverted ECC code from line parity | ||
| 73 | */ | ||
| 74 | static void nand_trans_result(u_char reg2, u_char reg3, | ||
| 75 | u_char *ecc_code) | ||
| 76 | { | ||
| 77 | u_char a, b, i, tmp1, tmp2; | ||
| 78 | |||
| 79 | /* Initialize variables */ | ||
| 80 | a = b = 0x80; | ||
| 81 | tmp1 = tmp2 = 0; | ||
| 82 | |||
| 83 | /* Calculate first ECC byte */ | ||
| 84 | for (i = 0; i < 4; i++) { | ||
| 85 | if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */ | ||
| 86 | tmp1 |= b; | ||
| 87 | b >>= 1; | ||
| 88 | if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */ | ||
| 89 | tmp1 |= b; | ||
| 90 | b >>= 1; | ||
| 91 | a >>= 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | /* Calculate second ECC byte */ | ||
| 95 | b = 0x80; | ||
| 96 | for (i = 0; i < 4; i++) { | ||
| 97 | if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */ | ||
| 98 | tmp2 |= b; | ||
| 99 | b >>= 1; | ||
| 100 | if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */ | ||
| 101 | tmp2 |= b; | ||
| 102 | b >>= 1; | ||
| 103 | a >>= 1; | ||
| 104 | } | ||
| 105 | |||
| 106 | /* Store two of the ECC bytes */ | ||
| 107 | ecc_code[0] = tmp1; | ||
| 108 | ecc_code[1] = tmp2; | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block | ||
| 113 | * @mtd: MTD block structure | 70 | * @mtd: MTD block structure |
| 114 | * @dat: raw data | 71 | * @dat: raw data |
| 115 | * @ecc_code: buffer for ECC | 72 | * @ecc_code: buffer for ECC |
| 116 | */ | 73 | */ |
| 117 | int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code) | 74 | int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 75 | u_char *ecc_code) | ||
| 118 | { | 76 | { |
| 119 | u_char idx, reg1, reg2, reg3; | 77 | uint8_t idx, reg1, reg2, reg3, tmp1, tmp2; |
| 120 | int j; | 78 | int i; |
| 121 | 79 | ||
| 122 | /* Initialize variables */ | 80 | /* Initialize variables */ |
| 123 | reg1 = reg2 = reg3 = 0; | 81 | reg1 = reg2 = reg3 = 0; |
| 124 | ecc_code[0] = ecc_code[1] = ecc_code[2] = 0; | ||
| 125 | 82 | ||
| 126 | /* Build up column parity */ | 83 | /* Build up column parity */ |
| 127 | for(j = 0; j < 256; j++) { | 84 | for(i = 0; i < 256; i++) { |
| 128 | |||
| 129 | /* Get CP0 - CP5 from table */ | 85 | /* Get CP0 - CP5 from table */ |
| 130 | idx = nand_ecc_precalc_table[dat[j]]; | 86 | idx = nand_ecc_precalc_table[*dat++]; |
| 131 | reg1 ^= (idx & 0x3f); | 87 | reg1 ^= (idx & 0x3f); |
| 132 | 88 | ||
| 133 | /* All bit XOR = 1 ? */ | 89 | /* All bit XOR = 1 ? */ |
| 134 | if (idx & 0x40) { | 90 | if (idx & 0x40) { |
| 135 | reg3 ^= (u_char) j; | 91 | reg3 ^= (uint8_t) i; |
| 136 | reg2 ^= ~((u_char) j); | 92 | reg2 ^= ~((uint8_t) i); |
| 137 | } | 93 | } |
| 138 | } | 94 | } |
| 139 | 95 | ||
| 140 | /* Create non-inverted ECC code from line parity */ | 96 | /* Create non-inverted ECC code from line parity */ |
| 141 | nand_trans_result(reg2, reg3, ecc_code); | 97 | tmp1 = (reg3 & 0x80) >> 0; /* B7 -> B7 */ |
| 98 | tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */ | ||
| 99 | tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */ | ||
| 100 | tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */ | ||
| 101 | tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */ | ||
| 102 | tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */ | ||
| 103 | tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */ | ||
| 104 | tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */ | ||
| 105 | |||
| 106 | tmp2 = (reg3 & 0x08) << 4; /* B3 -> B7 */ | ||
| 107 | tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */ | ||
| 108 | tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */ | ||
| 109 | tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */ | ||
| 110 | tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */ | ||
| 111 | tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */ | ||
| 112 | tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */ | ||
| 113 | tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */ | ||
| 142 | 114 | ||
| 143 | /* Calculate final ECC code */ | 115 | /* Calculate final ECC code */ |
| 144 | ecc_code[0] = ~ecc_code[0]; | 116 | #ifdef CONFIG_NAND_ECC_SMC |
| 145 | ecc_code[1] = ~ecc_code[1]; | 117 | ecc_code[0] = ~tmp2; |
| 118 | ecc_code[1] = ~tmp1; | ||
| 119 | #else | ||
| 120 | ecc_code[0] = ~tmp1; | ||
| 121 | ecc_code[1] = ~tmp2; | ||
| 122 | #endif | ||
| 146 | ecc_code[2] = ((~reg1) << 2) | 0x03; | 123 | ecc_code[2] = ((~reg1) << 2) | 0x03; |
| 124 | |||
| 147 | return 0; | 125 | return 0; |
| 148 | } | 126 | } |
| 127 | EXPORT_SYMBOL(nand_calculate_ecc); | ||
| 128 | |||
| 129 | static inline int countbits(uint32_t byte) | ||
| 130 | { | ||
| 131 | int res = 0; | ||
| 132 | |||
| 133 | for (;byte; byte >>= 1) | ||
| 134 | res += byte & 0x01; | ||
| 135 | return res; | ||
| 136 | } | ||
| 149 | 137 | ||
| 150 | /** | 138 | /** |
| 151 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) | 139 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) |
| @@ -156,93 +144,54 @@ int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code | |||
| 156 | * | 144 | * |
| 157 | * Detect and correct a 1 bit error for 256 byte block | 145 | * Detect and correct a 1 bit error for 256 byte block |
| 158 | */ | 146 | */ |
| 159 | int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc) | 147 | int nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 148 | u_char *read_ecc, u_char *calc_ecc) | ||
| 160 | { | 149 | { |
| 161 | u_char a, b, c, d1, d2, d3, add, bit, i; | 150 | uint8_t s0, s1, s2; |
| 151 | |||
| 152 | #ifdef CONFIG_NAND_ECC_SMC | ||
| 153 | s0 = calc_ecc[0] ^ read_ecc[0]; | ||
| 154 | s1 = calc_ecc[1] ^ read_ecc[1]; | ||
| 155 | s2 = calc_ecc[2] ^ read_ecc[2]; | ||
| 156 | #else | ||
| 157 | s1 = calc_ecc[0] ^ read_ecc[0]; | ||
| 158 | s0 = calc_ecc[1] ^ read_ecc[1]; | ||
| 159 | s2 = calc_ecc[2] ^ read_ecc[2]; | ||
| 160 | #endif | ||
| 161 | if ((s0 | s1 | s2) == 0) | ||
| 162 | return 0; | ||
| 162 | 163 | ||
| 163 | /* Do error detection */ | 164 | /* Check for a single bit error */ |
| 164 | d1 = calc_ecc[0] ^ read_ecc[0]; | 165 | if( ((s0 ^ (s0 >> 1)) & 0x55) == 0x55 && |
| 165 | d2 = calc_ecc[1] ^ read_ecc[1]; | 166 | ((s1 ^ (s1 >> 1)) & 0x55) == 0x55 && |
| 166 | d3 = calc_ecc[2] ^ read_ecc[2]; | 167 | ((s2 ^ (s2 >> 1)) & 0x54) == 0x54) { |
| 167 | 168 | ||
| 168 | if ((d1 | d2 | d3) == 0) { | 169 | uint32_t byteoffs, bitnum; |
| 169 | /* No errors */ | 170 | |
| 170 | return 0; | 171 | byteoffs = (s1 << 0) & 0x80; |
| 171 | } | 172 | byteoffs |= (s1 << 1) & 0x40; |
| 172 | else { | 173 | byteoffs |= (s1 << 2) & 0x20; |
| 173 | a = (d1 ^ (d1 >> 1)) & 0x55; | 174 | byteoffs |= (s1 << 3) & 0x10; |
| 174 | b = (d2 ^ (d2 >> 1)) & 0x55; | 175 | |
| 175 | c = (d3 ^ (d3 >> 1)) & 0x54; | 176 | byteoffs |= (s0 >> 4) & 0x08; |
| 176 | 177 | byteoffs |= (s0 >> 3) & 0x04; | |
| 177 | /* Found and will correct single bit error in the data */ | 178 | byteoffs |= (s0 >> 2) & 0x02; |
| 178 | if ((a == 0x55) && (b == 0x55) && (c == 0x54)) { | 179 | byteoffs |= (s0 >> 1) & 0x01; |
| 179 | c = 0x80; | 180 | |
| 180 | add = 0; | 181 | bitnum = (s2 >> 5) & 0x04; |
| 181 | a = 0x80; | 182 | bitnum |= (s2 >> 4) & 0x02; |
| 182 | for (i=0; i<4; i++) { | 183 | bitnum |= (s2 >> 3) & 0x01; |
| 183 | if (d1 & c) | 184 | |
| 184 | add |= a; | 185 | dat[byteoffs] ^= (1 << bitnum); |
| 185 | c >>= 2; | 186 | |
| 186 | a >>= 1; | 187 | return 1; |
| 187 | } | ||
| 188 | c = 0x80; | ||
| 189 | for (i=0; i<4; i++) { | ||
| 190 | if (d2 & c) | ||
| 191 | add |= a; | ||
| 192 | c >>= 2; | ||
| 193 | a >>= 1; | ||
| 194 | } | ||
| 195 | bit = 0; | ||
| 196 | b = 0x04; | ||
| 197 | c = 0x80; | ||
| 198 | for (i=0; i<3; i++) { | ||
| 199 | if (d3 & c) | ||
| 200 | bit |= b; | ||
| 201 | c >>= 2; | ||
| 202 | b >>= 1; | ||
| 203 | } | ||
| 204 | b = 0x01; | ||
| 205 | a = dat[add]; | ||
| 206 | a ^= (b << bit); | ||
| 207 | dat[add] = a; | ||
| 208 | return 1; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | i = 0; | ||
| 212 | while (d1) { | ||
| 213 | if (d1 & 0x01) | ||
| 214 | ++i; | ||
| 215 | d1 >>= 1; | ||
| 216 | } | ||
| 217 | while (d2) { | ||
| 218 | if (d2 & 0x01) | ||
| 219 | ++i; | ||
| 220 | d2 >>= 1; | ||
| 221 | } | ||
| 222 | while (d3) { | ||
| 223 | if (d3 & 0x01) | ||
| 224 | ++i; | ||
| 225 | d3 >>= 1; | ||
| 226 | } | ||
| 227 | if (i == 1) { | ||
| 228 | /* ECC Code Error Correction */ | ||
| 229 | read_ecc[0] = calc_ecc[0]; | ||
| 230 | read_ecc[1] = calc_ecc[1]; | ||
| 231 | read_ecc[2] = calc_ecc[2]; | ||
| 232 | return 2; | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | /* Uncorrectable Error */ | ||
| 236 | return -1; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | 188 | } |
| 240 | 189 | ||
| 241 | /* Should never happen */ | 190 | if(countbits(s0 | ((uint32_t)s1 << 8) | ((uint32_t)s2 <<16)) == 1) |
| 191 | return 1; | ||
| 192 | |||
| 242 | return -1; | 193 | return -1; |
| 243 | } | 194 | } |
| 244 | |||
| 245 | EXPORT_SYMBOL(nand_calculate_ecc); | ||
| 246 | EXPORT_SYMBOL(nand_correct_data); | 195 | EXPORT_SYMBOL(nand_correct_data); |
| 247 | 196 | ||
| 248 | MODULE_LICENSE("GPL"); | 197 | MODULE_LICENSE("GPL"); |
