Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file contains an ECC algorithm from Toshiba that detects and |
| 3 | * corrects 1 bit errors in a 256 byte block of data. |
| 4 | * |
| 5 | * drivers/mtd/nand/nand_ecc.c |
| 6 | * |
| 7 | * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com) |
| 8 | * Toshiba America Electronics Components, Inc. |
| 9 | * |
| 10 | * $Id: nand_ecc.c,v 1.14 2004/06/16 15:34:37 gleixner Exp $ |
| 11 | * |
| 12 | * This file is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 2 or (at your option) any |
| 15 | * later version. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 16 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 17 | * This file is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * for more details. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 21 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this file; if not, write to the Free Software Foundation, Inc., |
| 24 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 25 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 26 | * As a special exception, if other files instantiate templates or use |
| 27 | * macros or inline functions from these files, or you compile these |
| 28 | * files and link them with other works to produce a work based on these |
| 29 | * files, these files do not by themselves cause the resulting work to be |
| 30 | * covered by the GNU General Public License. However the source code for |
| 31 | * these files must still be made available in accordance with section (3) |
| 32 | * of the GNU General Public License. |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 33 | * |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 34 | * This exception does not invalidate any other reasons why a work based on |
| 35 | * this file might be covered by the GNU General Public License. |
| 36 | */ |
| 37 | |
| 38 | #include <common.h> |
| 39 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 40 | #ifdef CFG_NAND_LEGACY |
| 41 | #error CFG_NAND_LEGACY defined in a file not using the legacy NAND support! |
| 42 | #endif |
| 43 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 44 | #if (CONFIG_COMMANDS & CFG_CMD_NAND) |
| 45 | |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 46 | #include<linux/mtd/mtd.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Pre-calculated 256-way 1 byte column parity |
| 49 | */ |
| 50 | static const u_char nand_ecc_precalc_table[] = { |
| 51 | 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00, |
| 52 | 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65, |
| 53 | 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66, |
| 54 | 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03, |
| 55 | 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69, |
| 56 | 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c, |
| 57 | 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f, |
| 58 | 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a, |
| 59 | 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a, |
| 60 | 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f, |
| 61 | 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c, |
| 62 | 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69, |
| 63 | 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03, |
| 64 | 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66, |
| 65 | 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65, |
| 66 | 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00 |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * nand_trans_result - [GENERIC] create non-inverted ECC |
| 72 | * @reg2: line parity reg 2 |
| 73 | * @reg3: line parity reg 3 |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 74 | * @ecc_code: ecc |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 75 | * |
| 76 | * Creates non-inverted ECC code from line parity |
| 77 | */ |
| 78 | static void nand_trans_result(u_char reg2, u_char reg3, |
| 79 | u_char *ecc_code) |
| 80 | { |
| 81 | u_char a, b, i, tmp1, tmp2; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 82 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 83 | /* Initialize variables */ |
| 84 | a = b = 0x80; |
| 85 | tmp1 = tmp2 = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 86 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 87 | /* Calculate first ECC byte */ |
| 88 | for (i = 0; i < 4; i++) { |
| 89 | if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */ |
| 90 | tmp1 |= b; |
| 91 | b >>= 1; |
| 92 | if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */ |
| 93 | tmp1 |= b; |
| 94 | b >>= 1; |
| 95 | a >>= 1; |
| 96 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 97 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 98 | /* Calculate second ECC byte */ |
| 99 | b = 0x80; |
| 100 | for (i = 0; i < 4; i++) { |
| 101 | if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */ |
| 102 | tmp2 |= b; |
| 103 | b >>= 1; |
| 104 | if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */ |
| 105 | tmp2 |= b; |
| 106 | b >>= 1; |
| 107 | a >>= 1; |
| 108 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 109 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 110 | /* Store two of the ECC bytes */ |
| 111 | ecc_code[0] = tmp1; |
| 112 | ecc_code[1] = tmp2; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block |
| 117 | * @mtd: MTD block structure |
| 118 | * @dat: raw data |
| 119 | * @ecc_code: buffer for ECC |
| 120 | */ |
| 121 | int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code) |
| 122 | { |
| 123 | u_char idx, reg1, reg2, reg3; |
| 124 | int j; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 125 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 126 | /* Initialize variables */ |
| 127 | reg1 = reg2 = reg3 = 0; |
| 128 | ecc_code[0] = ecc_code[1] = ecc_code[2] = 0; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 129 | |
| 130 | /* Build up column parity */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 131 | for(j = 0; j < 256; j++) { |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 132 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 133 | /* Get CP0 - CP5 from table */ |
| 134 | idx = nand_ecc_precalc_table[dat[j]]; |
| 135 | reg1 ^= (idx & 0x3f); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 136 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 137 | /* All bit XOR = 1 ? */ |
| 138 | if (idx & 0x40) { |
| 139 | reg3 ^= (u_char) j; |
| 140 | reg2 ^= ~((u_char) j); |
| 141 | } |
| 142 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 143 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 144 | /* Create non-inverted ECC code from line parity */ |
| 145 | nand_trans_result(reg2, reg3, ecc_code); |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 146 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 147 | /* Calculate final ECC code */ |
| 148 | ecc_code[0] = ~ecc_code[0]; |
| 149 | ecc_code[1] = ~ecc_code[1]; |
| 150 | ecc_code[2] = ((~reg1) << 2) | 0x03; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * nand_correct_data - [NAND Interface] Detect and correct bit error(s) |
| 156 | * @mtd: MTD block structure |
| 157 | * @dat: raw data read from the chip |
| 158 | * @read_ecc: ECC from the chip |
| 159 | * @calc_ecc: the ECC calculated from raw data |
| 160 | * |
| 161 | * Detect and correct a 1 bit error for 256 byte block |
| 162 | */ |
| 163 | int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc) |
| 164 | { |
| 165 | u_char a, b, c, d1, d2, d3, add, bit, i; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 166 | |
| 167 | /* Do error detection */ |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 168 | d1 = calc_ecc[0] ^ read_ecc[0]; |
| 169 | d2 = calc_ecc[1] ^ read_ecc[1]; |
| 170 | d3 = calc_ecc[2] ^ read_ecc[2]; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 171 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 172 | if ((d1 | d2 | d3) == 0) { |
| 173 | /* No errors */ |
| 174 | return 0; |
| 175 | } |
| 176 | else { |
| 177 | a = (d1 ^ (d1 >> 1)) & 0x55; |
| 178 | b = (d2 ^ (d2 >> 1)) & 0x55; |
| 179 | c = (d3 ^ (d3 >> 1)) & 0x54; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 180 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 181 | /* Found and will correct single bit error in the data */ |
| 182 | if ((a == 0x55) && (b == 0x55) && (c == 0x54)) { |
| 183 | c = 0x80; |
| 184 | add = 0; |
| 185 | a = 0x80; |
| 186 | for (i=0; i<4; i++) { |
| 187 | if (d1 & c) |
| 188 | add |= a; |
| 189 | c >>= 2; |
| 190 | a >>= 1; |
| 191 | } |
| 192 | c = 0x80; |
| 193 | for (i=0; i<4; i++) { |
| 194 | if (d2 & c) |
| 195 | add |= a; |
| 196 | c >>= 2; |
| 197 | a >>= 1; |
| 198 | } |
| 199 | bit = 0; |
| 200 | b = 0x04; |
| 201 | c = 0x80; |
| 202 | for (i=0; i<3; i++) { |
| 203 | if (d3 & c) |
| 204 | bit |= b; |
| 205 | c >>= 2; |
| 206 | b >>= 1; |
| 207 | } |
| 208 | b = 0x01; |
| 209 | a = dat[add]; |
| 210 | a ^= (b << bit); |
| 211 | dat[add] = a; |
| 212 | return 1; |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 213 | } else { |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 214 | i = 0; |
| 215 | while (d1) { |
| 216 | if (d1 & 0x01) |
| 217 | ++i; |
| 218 | d1 >>= 1; |
| 219 | } |
| 220 | while (d2) { |
| 221 | if (d2 & 0x01) |
| 222 | ++i; |
| 223 | d2 >>= 1; |
| 224 | } |
| 225 | while (d3) { |
| 226 | if (d3 & 0x01) |
| 227 | ++i; |
| 228 | d3 >>= 1; |
| 229 | } |
| 230 | if (i == 1) { |
| 231 | /* ECC Code Error Correction */ |
| 232 | read_ecc[0] = calc_ecc[0]; |
| 233 | read_ecc[1] = calc_ecc[1]; |
| 234 | read_ecc[2] = calc_ecc[2]; |
| 235 | return 2; |
| 236 | } |
| 237 | else { |
| 238 | /* Uncorrectable Error */ |
| 239 | return -1; |
| 240 | } |
| 241 | } |
| 242 | } |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 243 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 244 | /* Should never happen */ |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | #endif /* CONFIG_COMMANDS & CFG_CMD_NAND */ |