]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/nand/nand_ecc.c
Merge with /home/wd/git/u-boot/testing-NAND/ to add new NAND handling.
[karo-tx-uboot.git] / drivers / nand / nand_ecc.c
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.
16  *
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.
21  *
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.
25  *
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.
33  *
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
40 #ifdef CONFIG_NEW_NAND_CODE
41 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
42
43 #include<linux/mtd/mtd.h>
44 /*
45  * Pre-calculated 256-way 1 byte column parity
46  */
47 static const u_char nand_ecc_precalc_table[] = {
48         0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
49         0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
50         0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
51         0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
52         0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
53         0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
54         0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
55         0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
56         0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
57         0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
58         0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
59         0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
60         0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
61         0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
62         0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
63         0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
64 };
65
66
67 /**
68  * nand_trans_result - [GENERIC] create non-inverted ECC
69  * @reg2:       line parity reg 2
70  * @reg3:       line parity reg 3
71  * @ecc_code:   ecc
72  *
73  * Creates non-inverted ECC code from line parity
74  */
75 static void nand_trans_result(u_char reg2, u_char reg3,
76         u_char *ecc_code)
77 {
78         u_char a, b, i, tmp1, tmp2;
79
80         /* Initialize variables */
81         a = b = 0x80;
82         tmp1 = tmp2 = 0;
83
84         /* Calculate first ECC byte */
85         for (i = 0; i < 4; i++) {
86                 if (reg3 & a)           /* LP15,13,11,9 --> ecc_code[0] */
87                         tmp1 |= b;
88                 b >>= 1;
89                 if (reg2 & a)           /* LP14,12,10,8 --> ecc_code[0] */
90                         tmp1 |= b;
91                 b >>= 1;
92                 a >>= 1;
93         }
94
95         /* Calculate second ECC byte */
96         b = 0x80;
97         for (i = 0; i < 4; i++) {
98                 if (reg3 & a)           /* LP7,5,3,1 --> ecc_code[1] */
99                         tmp2 |= b;
100                 b >>= 1;
101                 if (reg2 & a)           /* LP6,4,2,0 --> ecc_code[1] */
102                         tmp2 |= b;
103                 b >>= 1;
104                 a >>= 1;
105         }
106
107         /* Store two of the ECC bytes */
108         ecc_code[0] = tmp1;
109         ecc_code[1] = tmp2;
110 }
111
112 /**
113  * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block
114  * @mtd:        MTD block structure
115  * @dat:        raw data
116  * @ecc_code:   buffer for ECC
117  */
118 int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
119 {
120         u_char idx, reg1, reg2, reg3;
121         int j;
122
123         /* Initialize variables */
124         reg1 = reg2 = reg3 = 0;
125         ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
126
127         /* Build up column parity */
128         for(j = 0; j < 256; j++) {
129
130                 /* Get CP0 - CP5 from table */
131                 idx = nand_ecc_precalc_table[dat[j]];
132                 reg1 ^= (idx & 0x3f);
133
134                 /* All bit XOR = 1 ? */
135                 if (idx & 0x40) {
136                         reg3 ^= (u_char) j;
137                         reg2 ^= ~((u_char) j);
138                 }
139         }
140
141         /* Create non-inverted ECC code from line parity */
142         nand_trans_result(reg2, reg3, ecc_code);
143
144         /* Calculate final ECC code */
145         ecc_code[0] = ~ecc_code[0];
146         ecc_code[1] = ~ecc_code[1];
147         ecc_code[2] = ((~reg1) << 2) | 0x03;
148         return 0;
149 }
150
151 /**
152  * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
153  * @mtd:        MTD block structure
154  * @dat:        raw data read from the chip
155  * @read_ecc:   ECC from the chip
156  * @calc_ecc:   the ECC calculated from raw data
157  *
158  * Detect and correct a 1 bit error for 256 byte block
159  */
160 int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
161 {
162         u_char a, b, c, d1, d2, d3, add, bit, i;
163
164         /* Do error detection */
165         d1 = calc_ecc[0] ^ read_ecc[0];
166         d2 = calc_ecc[1] ^ read_ecc[1];
167         d3 = calc_ecc[2] ^ read_ecc[2];
168
169         if ((d1 | d2 | d3) == 0) {
170                 /* No errors */
171                 return 0;
172         }
173         else {
174                 a = (d1 ^ (d1 >> 1)) & 0x55;
175                 b = (d2 ^ (d2 >> 1)) & 0x55;
176                 c = (d3 ^ (d3 >> 1)) & 0x54;
177
178                 /* Found and will correct single bit error in the data */
179                 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
180                         c = 0x80;
181                         add = 0;
182                         a = 0x80;
183                         for (i=0; i<4; i++) {
184                                 if (d1 & c)
185                                         add |= a;
186                                 c >>= 2;
187                                 a >>= 1;
188                         }
189                         c = 0x80;
190                         for (i=0; i<4; i++) {
191                                 if (d2 & c)
192                                         add |= a;
193                                 c >>= 2;
194                                 a >>= 1;
195                         }
196                         bit = 0;
197                         b = 0x04;
198                         c = 0x80;
199                         for (i=0; i<3; i++) {
200                                 if (d3 & c)
201                                         bit |= b;
202                                 c >>= 2;
203                                 b >>= 1;
204                         }
205                         b = 0x01;
206                         a = dat[add];
207                         a ^= (b << bit);
208                         dat[add] = a;
209                         return 1;
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         }
240
241         /* Should never happen */
242         return -1;
243 }
244
245 #endif  /* CONFIG_COMMANDS & CFG_CMD_NAND */
246 #endif /* CONFIG_NEW_NAND_CODE */
247