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