If kernel/config.h is included in yportenv.h, then yportenv.h must be
[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.4 2005-07-31 00:28:04 charles Exp $";
33
34 #include "yportenv.h"
35
36 #include "yaffs_ecc.h"
37
38 static const unsigned char column_parity_table[] = {
39 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, 
40 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, 
41 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, 
42 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, 
43 0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 0x65, 0x30, 0x3c, 0x69, 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, 
55 };
56
57
58 static int yaffs_CountBits(unsigned char x)
59 {
60         int r = 0;
61         while(x)
62         {
63                 if(x & 1) r++;
64                 x >>= 1;
65         }
66         return r;
67 }
68
69 static int yaffs_CountBits32(unsigned  x)
70 {
71         int r = 0;
72         while(x)
73         {
74                 if(x & 1) r++;
75                 x >>= 1;
76         }
77         return r;
78 }
79
80
81 void yaffs_ECCCalculate(const unsigned char *data,unsigned char *ecc)
82 {
83         unsigned int i;
84         
85         unsigned char col_parity = 0;
86         unsigned char line_parity = 0;
87         unsigned char line_parity_prime = 0;
88         unsigned char t;
89         unsigned char b;
90         
91         for(i = 0; i < 256; i++)
92         {
93                 b = column_parity_table[*data++];
94                 col_parity ^= b;
95
96                 if(b & 0x01) // odd number of bits in the byte
97                 {
98                         line_parity ^= i;
99                         line_parity_prime ^= ~i;
100                 }
101                 
102         }
103         
104         ecc[2] = (~col_parity) | 0x03;
105         
106         t = 0;
107         if(line_parity       & 0x80) t |= 0x80;
108         if(line_parity_prime & 0x80) t |= 0x40;
109         if(line_parity       & 0x40) t |= 0x20;
110         if(line_parity_prime & 0x40) t |= 0x10;
111         if(line_parity       & 0x20) t |= 0x08;
112         if(line_parity_prime & 0x20) t |= 0x04;
113         if(line_parity       & 0x10) t |= 0x02;
114         if(line_parity_prime & 0x10) t |= 0x01;
115         ecc[1] = ~t;
116         
117         t = 0;
118         if(line_parity       & 0x08) t |= 0x80;
119         if(line_parity_prime & 0x08) t |= 0x40;
120         if(line_parity       & 0x04) t |= 0x20;
121         if(line_parity_prime & 0x04) t |= 0x10;
122         if(line_parity       & 0x02) t |= 0x08;
123         if(line_parity_prime & 0x02) t |= 0x04;
124         if(line_parity       & 0x01) t |= 0x02;
125         if(line_parity_prime & 0x01) t |= 0x01;
126         ecc[0] = ~t;
127
128 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
129         // Swap the bytes into the wrong order
130         t = ecc[0];
131         ecc[0] = ecc[1];
132         ecc[1] = t;
133 #endif 
134 }
135
136 int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc, const unsigned char *test_ecc)
137 {
138         unsigned char d0, d1, d2; // deltas 
139
140         d0 = read_ecc[0] ^ test_ecc[0];
141         d1 = read_ecc[1] ^ test_ecc[1];
142         d2 = read_ecc[2] ^ test_ecc[2];
143         
144         
145         
146         if((d0 | d1 | d2) == 0)
147         {
148                 // no error
149                 return 0;
150         }
151         
152         if( ((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
153             ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
154             ((d2 ^ (d2 >> 1)) & 0x54) == 0x54)
155         {
156                 // Single bit (recoverable) error in data
157
158                 unsigned byte;
159                 unsigned bit;
160
161 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
162                 // swap the bytes to correct for the wrong order
163                 unsigned char t;
164                 
165                 t = d0;
166                 d0 = d1;
167                 d1 = t;
168 #endif
169                 
170                 bit = byte = 0;
171                 
172                 
173                 if(d1 & 0x80) byte |= 0x80;
174                 if(d1 & 0x20) byte |= 0x40;
175                 if(d1 & 0x08) byte |= 0x20;
176                 if(d1 & 0x02) byte |= 0x10;
177                 if(d0 & 0x80) byte |= 0x08;
178                 if(d0 & 0x20) byte |= 0x04;
179                 if(d0 & 0x08) byte |= 0x02;
180                 if(d0 & 0x02) byte |= 0x01;
181
182                 if(d2 & 0x80) bit |= 0x04;
183                 if(d2 & 0x20) bit |= 0x02;
184                 if(d2 & 0x08) bit |= 0x01;
185
186                 data[byte] ^= (1 << bit);
187                 
188                 return 1;
189         }
190         
191         if((yaffs_CountBits(d0)+yaffs_CountBits(d1)+yaffs_CountBits(d2)) == 1)
192         {
193                 // Reccoverable error in ecc
194                 
195                 read_ecc[0] = test_ecc[0];
196                 read_ecc[1] = test_ecc[1];
197                 read_ecc[2] = test_ecc[2];
198                 
199                 return 1;
200         }
201         
202         // Unrecoverable error
203         
204         return -1;
205             
206
207 }
208
209
210
211 void yaffs_ECCCalculateOther(const unsigned char *data,unsigned nBytes, yaffs_ECCOther *eccOther)
212 {
213         unsigned int i;
214         
215         unsigned char col_parity = 0;
216         unsigned line_parity = 0;
217         unsigned line_parity_prime = 0;
218         unsigned char b;
219         
220         for(i = 0; i < nBytes; i++)
221         {
222                 b = column_parity_table[*data++];
223                 col_parity ^= b;
224
225                 if(b & 0x01) // odd number of bits in the byte
226                 {
227                         line_parity ^= i;
228                         line_parity_prime ^= ~i;
229                 }
230                 
231         }
232         
233         eccOther->colParity = (col_parity >> 2) & 0x3f;
234         eccOther->lineParity = line_parity;
235         eccOther->lineParityPrime = line_parity_prime;
236 }
237
238 int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes, yaffs_ECCOther *read_ecc, const yaffs_ECCOther  *test_ecc)
239 {
240         unsigned char cDelta; // column parity delta
241         unsigned lDelta; // line parity delta
242         unsigned lDeltaPrime; // line parity delta
243         unsigned bit;
244
245         cDelta = read_ecc->colParity ^ test_ecc->colParity;
246         lDelta = read_ecc->lineParity ^ test_ecc->lineParity;   
247         lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
248         
249         if((cDelta | lDelta | lDeltaPrime) == 0)
250         {
251                 // no error
252                 return 0;
253         }
254         
255         if( lDelta == ~lDeltaPrime &&
256              (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) // Not correct 
257         {
258                 // Single bit (recoverable) error in data
259
260                 bit = 0;
261
262                 if(cDelta & 0x20) bit |= 0x04;
263                 if(cDelta & 0x08) bit |= 0x02;
264                 if(cDelta & 0x02) bit |= 0x01;
265
266                 data[lDelta] ^= (1 << bit);
267                 
268                 return 1;
269         }
270         
271         if((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) + yaffs_CountBits(cDelta)) == 1)
272         {
273                 // Reccoverable error in ecc
274                 
275                 *read_ecc  = *test_ecc;         
276                 return 1;
277         }
278         
279         // Unrecoverable error
280         
281         return -1;
282             
283
284 }
285
286
287