get rid of $Id$ - it has never helped us and it has broken too many patches ;)
[openwrt.git] / target / linux / generic-2.6 / files / fs / yaffs2 / yaffs_ecc.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /*
15  * This code implements the ECC algorithm used in SmartMedia.
16  *
17  * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
18  * The two unused bit are set to 1.
19  * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
20  * blocks are used on a 512-byte NAND page.
21  *
22  */
23
24 /* Table generated by gen-ecc.c
25  * Using a table means we do not have to calculate p1..p4 and p1'..p4'
26  * for each byte of data. These are instead provided in a table in bits7..2.
27  * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
28  * this bytes influence on the line parity.
29  */
30
31 const char *yaffs_ecc_c_version =
32
33 #include "yportenv.h"
34
35 #include "yaffs_ecc.h"
36
37 static const unsigned char column_parity_table[] = {
38         0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
39         0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
40         0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
41         0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
42         0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
43         0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
44         0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
45         0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
46         0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
47         0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
48         0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
49         0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
50         0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
51         0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
52         0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
53         0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
54         0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
55         0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
56         0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
57         0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
58         0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
59         0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
60         0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
61         0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
62         0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
63         0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
64         0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
65         0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
66         0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
67         0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
68         0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
69         0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
70 };
71
72 /* Count the bits in an unsigned char or a U32 */
73
74 static int yaffs_CountBits(unsigned char x)
75 {
76         int r = 0;
77         while (x) {
78                 if (x & 1)
79                         r++;
80                 x >>= 1;
81         }
82         return r;
83 }
84
85 static int yaffs_CountBits32(unsigned x)
86 {
87         int r = 0;
88         while (x) {
89                 if (x & 1)
90                         r++;
91                 x >>= 1;
92         }
93         return r;
94 }
95
96 /* Calculate the ECC for a 256-byte block of data */
97 void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
98 {
99         unsigned int i;
100
101         unsigned char col_parity = 0;
102         unsigned char line_parity = 0;
103         unsigned char line_parity_prime = 0;
104         unsigned char t;
105         unsigned char b;
106
107         for (i = 0; i < 256; i++) {
108                 b = column_parity_table[*data++];
109                 col_parity ^= b;
110
111                 if (b & 0x01)   // odd number of bits in the byte
112                 {
113                         line_parity ^= i;
114                         line_parity_prime ^= ~i;
115                 }
116
117         }
118
119         ecc[2] = (~col_parity) | 0x03;
120
121         t = 0;
122         if (line_parity & 0x80)
123                 t |= 0x80;
124         if (line_parity_prime & 0x80)
125                 t |= 0x40;
126         if (line_parity & 0x40)
127                 t |= 0x20;
128         if (line_parity_prime & 0x40)
129                 t |= 0x10;
130         if (line_parity & 0x20)
131                 t |= 0x08;
132         if (line_parity_prime & 0x20)
133                 t |= 0x04;
134         if (line_parity & 0x10)
135                 t |= 0x02;
136         if (line_parity_prime & 0x10)
137                 t |= 0x01;
138         ecc[1] = ~t;
139
140         t = 0;
141         if (line_parity & 0x08)
142                 t |= 0x80;
143         if (line_parity_prime & 0x08)
144                 t |= 0x40;
145         if (line_parity & 0x04)
146                 t |= 0x20;
147         if (line_parity_prime & 0x04)
148                 t |= 0x10;
149         if (line_parity & 0x02)
150                 t |= 0x08;
151         if (line_parity_prime & 0x02)
152                 t |= 0x04;
153         if (line_parity & 0x01)
154                 t |= 0x02;
155         if (line_parity_prime & 0x01)
156                 t |= 0x01;
157         ecc[0] = ~t;
158
159 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
160         // Swap the bytes into the wrong order
161         t = ecc[0];
162         ecc[0] = ecc[1];
163         ecc[1] = t;
164 #endif
165 }
166
167
168 /* Correct the ECC on a 256 byte block of data */
169
170 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
171                      const unsigned char *test_ecc)
172 {
173         unsigned char d0, d1, d2;       /* deltas */
174
175         d0 = read_ecc[0] ^ test_ecc[0];
176         d1 = read_ecc[1] ^ test_ecc[1];
177         d2 = read_ecc[2] ^ test_ecc[2];
178
179         if ((d0 | d1 | d2) == 0)
180                 return 0; /* no error */
181
182         if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
183             ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
184             ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
185                 /* Single bit (recoverable) error in data */
186
187                 unsigned byte;
188                 unsigned bit;
189
190 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
191                 // swap the bytes to correct for the wrong order
192                 unsigned char t;
193
194                 t = d0;
195                 d0 = d1;
196                 d1 = t;
197 #endif
198
199                 bit = byte = 0;
200
201                 if (d1 & 0x80)
202                         byte |= 0x80;
203                 if (d1 & 0x20)
204                         byte |= 0x40;
205                 if (d1 & 0x08)
206                         byte |= 0x20;
207                 if (d1 & 0x02)
208                         byte |= 0x10;
209                 if (d0 & 0x80)
210                         byte |= 0x08;
211                 if (d0 & 0x20)
212                         byte |= 0x04;
213                 if (d0 & 0x08)
214                         byte |= 0x02;
215                 if (d0 & 0x02)
216                         byte |= 0x01;
217
218                 if (d2 & 0x80)
219                         bit |= 0x04;
220                 if (d2 & 0x20)
221                         bit |= 0x02;
222                 if (d2 & 0x08)
223                         bit |= 0x01;
224
225                 data[byte] ^= (1 << bit);
226
227                 return 1; /* Corrected the error */
228         }
229
230         if ((yaffs_CountBits(d0) +
231              yaffs_CountBits(d1) +
232              yaffs_CountBits(d2)) ==  1) {
233                 /* Reccoverable error in ecc */
234
235                 read_ecc[0] = test_ecc[0];
236                 read_ecc[1] = test_ecc[1];
237                 read_ecc[2] = test_ecc[2];
238
239                 return 1; /* Corrected the error */
240         }
241
242         /* Unrecoverable error */
243
244         return -1;
245
246 }
247
248
249 /*
250  * ECCxxxOther does ECC calcs on arbitrary n bytes of data
251  */
252 void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
253                              yaffs_ECCOther * eccOther)
254 {
255         unsigned int i;
256
257         unsigned char col_parity = 0;
258         unsigned line_parity = 0;
259         unsigned line_parity_prime = 0;
260         unsigned char b;
261
262         for (i = 0; i < nBytes; i++) {
263                 b = column_parity_table[*data++];
264                 col_parity ^= b;
265
266                 if (b & 0x01)    {
267                         /* odd number of bits in the byte */
268                         line_parity ^= i;
269                         line_parity_prime ^= ~i;
270                 }
271
272         }
273
274         eccOther->colParity = (col_parity >> 2) & 0x3f;
275         eccOther->lineParity = line_parity;
276         eccOther->lineParityPrime = line_parity_prime;
277 }
278
279 int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
280                           yaffs_ECCOther * read_ecc,
281                           const yaffs_ECCOther * test_ecc)
282 {
283         unsigned char cDelta;   /* column parity delta */
284         unsigned lDelta;        /* line parity delta */
285         unsigned lDeltaPrime;   /* line parity delta */
286         unsigned bit;
287
288         cDelta = read_ecc->colParity ^ test_ecc->colParity;
289         lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
290         lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
291
292         if ((cDelta | lDelta | lDeltaPrime) == 0)
293                 return 0; /* no error */
294
295         if (lDelta == ~lDeltaPrime &&
296             (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
297         {
298                 /* Single bit (recoverable) error in data */
299
300                 bit = 0;
301
302                 if (cDelta & 0x20)
303                         bit |= 0x04;
304                 if (cDelta & 0x08)
305                         bit |= 0x02;
306                 if (cDelta & 0x02)
307                         bit |= 0x01;
308
309                 if(lDelta >= nBytes)
310                         return -1;
311
312                 data[lDelta] ^= (1 << bit);
313
314                 return 1; /* corrected */
315         }
316
317         if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
318              yaffs_CountBits(cDelta)) == 1) {
319                 /* Reccoverable error in ecc */
320
321                 *read_ecc = *test_ecc;
322                 return 1; /* corrected */
323         }
324
325         /* Unrecoverable error */
326
327         return -1;
328
329 }
330