yaffs Fixed some more bugs in quick tests.
[yaffs2.git] / yaffs_ecc.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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 #include "yportenv.h"
32
33 #include "yaffs_ecc.h"
34
35 static const unsigned char column_parity_table[] = {
36         0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
37         0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
38         0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
39         0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
40         0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
41         0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
42         0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
43         0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
44         0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
45         0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
46         0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
47         0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
48         0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
49         0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
50         0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
51         0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
52         0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
53         0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
54         0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
55         0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
56         0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
57         0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
58         0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
59         0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
60         0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
61         0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
62         0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
63         0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
64         0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
65         0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
66         0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
67         0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
68 };
69
70 /* Count the bits in an unsigned char or a U32 */
71
72 static int yaffs_count_bits(unsigned char x)
73 {
74         int r = 0;
75         while (x) {
76                 if (x & 1)
77                         r++;
78                 x >>= 1;
79         }
80         return r;
81 }
82
83 static int yaffs_count_bits32(unsigned x)
84 {
85         int r = 0;
86         while (x) {
87                 if (x & 1)
88                         r++;
89                 x >>= 1;
90         }
91         return r;
92 }
93
94 /* Calculate the ECC for a 256-byte block of data */
95 void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
96 {
97         unsigned int i;
98
99         unsigned char col_parity = 0;
100         unsigned char line_parity = 0;
101         unsigned char line_parity_prime = 0;
102         unsigned char t;
103         unsigned char b;
104
105         for (i = 0; i < 256; i++) {
106                 b = column_parity_table[*data++];
107                 col_parity ^= b;
108
109                 if (b & 0x01) { /* odd number of bits in the byte */
110                         line_parity ^= i;
111                         line_parity_prime ^= ~i;
112                 }
113         }
114
115         ecc[2] = (~col_parity) | 0x03;
116
117         t = 0;
118         if (line_parity & 0x80)
119                 t |= 0x80;
120         if (line_parity_prime & 0x80)
121                 t |= 0x40;
122         if (line_parity & 0x40)
123                 t |= 0x20;
124         if (line_parity_prime & 0x40)
125                 t |= 0x10;
126         if (line_parity & 0x20)
127                 t |= 0x08;
128         if (line_parity_prime & 0x20)
129                 t |= 0x04;
130         if (line_parity & 0x10)
131                 t |= 0x02;
132         if (line_parity_prime & 0x10)
133                 t |= 0x01;
134         ecc[1] = ~t;
135
136         t = 0;
137         if (line_parity & 0x08)
138                 t |= 0x80;
139         if (line_parity_prime & 0x08)
140                 t |= 0x40;
141         if (line_parity & 0x04)
142                 t |= 0x20;
143         if (line_parity_prime & 0x04)
144                 t |= 0x10;
145         if (line_parity & 0x02)
146                 t |= 0x08;
147         if (line_parity_prime & 0x02)
148                 t |= 0x04;
149         if (line_parity & 0x01)
150                 t |= 0x02;
151         if (line_parity_prime & 0x01)
152                 t |= 0x01;
153         ecc[0] = ~t;
154
155 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
156         /* Swap the bytes into the wrong order */
157         t = ecc[0];
158         ecc[0] = ecc[1];
159         ecc[1] = t;
160 #endif
161 }
162
163 /* Correct the ECC on a 256 byte block of data */
164
165 int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
166                       const unsigned char *test_ecc)
167 {
168         unsigned char d0, d1, d2;       /* deltas */
169
170         d0 = read_ecc[0] ^ test_ecc[0];
171         d1 = read_ecc[1] ^ test_ecc[1];
172         d2 = read_ecc[2] ^ test_ecc[2];
173
174         if ((d0 | d1 | d2) == 0)
175                 return 0;       /* no error */
176
177         if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
178             ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
179             ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
180                 /* Single bit (recoverable) error in data */
181
182                 unsigned byte;
183                 unsigned bit;
184
185 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
186                 /* swap the bytes to correct for the wrong order */
187                 unsigned char t;
188
189                 t = d0;
190                 d0 = d1;
191                 d1 = t;
192 #endif
193
194                 bit = byte = 0;
195
196                 if (d1 & 0x80)
197                         byte |= 0x80;
198                 if (d1 & 0x20)
199                         byte |= 0x40;
200                 if (d1 & 0x08)
201                         byte |= 0x20;
202                 if (d1 & 0x02)
203                         byte |= 0x10;
204                 if (d0 & 0x80)
205                         byte |= 0x08;
206                 if (d0 & 0x20)
207                         byte |= 0x04;
208                 if (d0 & 0x08)
209                         byte |= 0x02;
210                 if (d0 & 0x02)
211                         byte |= 0x01;
212
213                 if (d2 & 0x80)
214                         bit |= 0x04;
215                 if (d2 & 0x20)
216                         bit |= 0x02;
217                 if (d2 & 0x08)
218                         bit |= 0x01;
219
220                 data[byte] ^= (1 << bit);
221
222                 return 1;       /* Corrected the error */
223         }
224
225         if ((yaffs_count_bits(d0) +
226              yaffs_count_bits(d1) + yaffs_count_bits(d2)) == 1) {
227                 /* Reccoverable error in ecc */
228
229                 read_ecc[0] = test_ecc[0];
230                 read_ecc[1] = test_ecc[1];
231                 read_ecc[2] = test_ecc[2];
232
233                 return 1;       /* Corrected the error */
234         }
235
236         /* Unrecoverable error */
237
238         return -1;
239
240 }
241
242 /*
243  * ECCxxxOther does ECC calcs on arbitrary n bytes of data
244  */
245 void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
246                           struct yaffs_ecc_other *ecc_other)
247 {
248         unsigned int i;
249
250         unsigned char col_parity = 0;
251         unsigned line_parity = 0;
252         unsigned line_parity_prime = 0;
253         unsigned char b;
254
255         for (i = 0; i < n_bytes; i++) {
256                 b = column_parity_table[*data++];
257                 col_parity ^= b;
258
259                 if (b & 0x01) {
260                         /* odd number of bits in the byte */
261                         line_parity ^= i;
262                         line_parity_prime ^= ~i;
263                 }
264
265         }
266
267         ecc_other->col_parity = (col_parity >> 2) & 0x3f;
268         ecc_other->line_parity = line_parity;
269         ecc_other->line_parity_prime = line_parity_prime;
270 }
271
272 int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
273                             struct yaffs_ecc_other *read_ecc,
274                             const struct yaffs_ecc_other *test_ecc)
275 {
276         unsigned char delta_col;        /* column parity delta */
277         unsigned delta_line;    /* line parity delta */
278         unsigned delta_line_prime;      /* line parity delta */
279         unsigned bit;
280
281         delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
282         delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
283         delta_line_prime =
284             read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
285
286         if ((delta_col | delta_line | delta_line_prime) == 0)
287                 return 0;       /* no error */
288
289         if (delta_line == ~delta_line_prime &&
290             (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
291                 /* Single bit (recoverable) error in data */
292
293                 bit = 0;
294
295                 if (delta_col & 0x20)
296                         bit |= 0x04;
297                 if (delta_col & 0x08)
298                         bit |= 0x02;
299                 if (delta_col & 0x02)
300                         bit |= 0x01;
301
302                 if (delta_line >= n_bytes)
303                         return -1;
304
305                 data[delta_line] ^= (1 << bit);
306
307                 return 1;       /* corrected */
308         }
309
310         if ((yaffs_count_bits32(delta_line) +
311              yaffs_count_bits32(delta_line_prime) +
312              yaffs_count_bits(delta_col)) == 1) {
313                 /* Reccoverable error in ecc */
314
315                 *read_ecc = *test_ecc;
316                 return 1;       /* corrected */
317         }
318
319         /* Unrecoverable error */
320
321         return -1;
322 }