Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / mtd / devices / docecc.c
1 /*
2  * ECC algorithm for M-systems disk on chip. We use the excellent Reed
3  * Solmon code of Phil Karn (karn@ka9q.ampr.org) available under the
4  * GNU GPL License. The rest is simply to convert the disk on chip
5  * syndrom into a standard syndom.
6  *
7  * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 
8  * Copyright (C) 2000 Netgem S.A.
9  *
10  * $Id: docecc.c,v 1.5 2003/05/21 15:15:06 dwmw2 Exp $
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/errno.h>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <linux/miscdevice.h>
32 #include <linux/pci.h>
33 #include <linux/delay.h>
34 #include <linux/slab.h>
35 #include <linux/sched.h>
36 #include <linux/init.h>
37 #include <linux/types.h>
38
39 #include <linux/mtd/compatmac.h> /* for min() in older kernels */
40 #include <linux/mtd/mtd.h>
41 #include <linux/mtd/doc2000.h>
42
43 #define DEBUG 0
44 /* need to undef it (from asm/termbits.h) */
45 #undef B0
46
47 #define MM 10 /* Symbol size in bits */
48 #define KK (1023-4) /* Number of data symbols per block */
49 #define B0 510 /* First root of generator polynomial, alpha form */
50 #define PRIM 1 /* power of alpha used to generate roots of generator poly */
51 #define NN ((1 << MM) - 1)
52
53 typedef unsigned short dtype;
54
55 /* 1+x^3+x^10 */
56 static const int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
57
58 /* This defines the type used to store an element of the Galois Field
59  * used by the code. Make sure this is something larger than a char if
60  * if anything larger than GF(256) is used.
61  *
62  * Note: unsigned char will work up to GF(256) but int seems to run
63  * faster on the Pentium.
64  */
65 typedef int gf;
66
67 /* No legal value in index form represents zero, so
68  * we need a special value for this purpose
69  */
70 #define A0      (NN)
71
72 /* Compute x % NN, where NN is 2**MM - 1,
73  * without a slow divide
74  */
75 static inline gf
76 modnn(int x)
77 {
78   while (x >= NN) {
79     x -= NN;
80     x = (x >> MM) + (x & NN);
81   }
82   return x;
83 }
84
85 #define CLEAR(a,n) {\
86 int ci;\
87 for(ci=(n)-1;ci >=0;ci--)\
88 (a)[ci] = 0;\
89 }
90
91 #define COPY(a,b,n) {\
92 int ci;\
93 for(ci=(n)-1;ci >=0;ci--)\
94 (a)[ci] = (b)[ci];\
95 }
96
97 #define COPYDOWN(a,b,n) {\
98 int ci;\
99 for(ci=(n)-1;ci >=0;ci--)\
100 (a)[ci] = (b)[ci];\
101 }
102
103 #define Ldec 1
104
105 /* generate GF(2**m) from the irreducible polynomial p(X) in Pp[0]..Pp[m]
106    lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
107                    polynomial form -> index form  index_of[j=alpha**i] = i
108    alpha=2 is the primitive element of GF(2**m)
109    HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
110         Let @ represent the primitive element commonly called "alpha" that
111    is the root of the primitive polynomial p(x). Then in GF(2^m), for any
112    0 <= i <= 2^m-2,
113         @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
114    where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
115    of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
116    example the polynomial representation of @^5 would be given by the binary
117    representation of the integer "alpha_to[5]".
118                    Similarily, index_of[] can be used as follows:
119         As above, let @ represent the primitive element of GF(2^m) that is
120    the root of the primitive polynomial p(x). In order to find the power
121    of @ (alpha) that has the polynomial representation
122         a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
123    we consider the integer "i" whose binary representation with a(0) being LSB
124    and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
125    "index_of[i]". Now, @^index_of[i] is that element whose polynomial 
126     representation is (a(0),a(1),a(2),...,a(m-1)).
127    NOTE:
128         The element alpha_to[2^m-1] = 0 always signifying that the
129    representation of "@^infinity" = 0 is (0,0,0,...,0).
130         Similarily, the element index_of[0] = A0 always signifying
131    that the power of alpha which has the polynomial representation
132    (0,0,...,0) is "infinity".
133  
134 */
135
136 static void
137 generate_gf(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1])
138 {
139   register int i, mask;
140
141   mask = 1;
142   Alpha_to[MM] = 0;
143   for (i = 0; i < MM; i++) {
144     Alpha_to[i] = mask;
145     Index_of[Alpha_to[i]] = i;
146     /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
147     if (Pp[i] != 0)
148       Alpha_to[MM] ^= mask;     /* Bit-wise EXOR operation */
149     mask <<= 1; /* single left-shift */
150   }
151   Index_of[Alpha_to[MM]] = MM;
152   /*
153    * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
154    * poly-repr of @^i shifted left one-bit and accounting for any @^MM
155    * term that may occur when poly-repr of @^i is shifted.
156    */
157   mask >>= 1;
158   for (i = MM + 1; i < NN; i++) {
159     if (Alpha_to[i - 1] >= mask)
160       Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
161     else
162       Alpha_to[i] = Alpha_to[i - 1] << 1;
163     Index_of[Alpha_to[i]] = i;
164   }
165   Index_of[0] = A0;
166   Alpha_to[NN] = 0;
167 }
168
169 /*
170  * Performs ERRORS+ERASURES decoding of RS codes. bb[] is the content
171  * of the feedback shift register after having processed the data and
172  * the ECC.
173  *
174  * Return number of symbols corrected, or -1 if codeword is illegal
175  * or uncorrectable. If eras_pos is non-null, the detected error locations
176  * are written back. NOTE! This array must be at least NN-KK elements long.
177  * The corrected data are written in eras_val[]. They must be xor with the data
178  * to retrieve the correct data : data[erase_pos[i]] ^= erase_val[i] .
179  * 
180  * First "no_eras" erasures are declared by the calling program. Then, the
181  * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
182  * If the number of channel errors is not greater than "t_after_eras" the
183  * transmitted codeword will be recovered. Details of algorithm can be found
184  * in R. Blahut's "Theory ... of Error-Correcting Codes".
185
186  * Warning: the eras_pos[] array must not contain duplicate entries; decoder failure
187  * will result. The decoder *could* check for this condition, but it would involve
188  * extra time on every decoding operation.
189  * */
190 static int
191 eras_dec_rs(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1],
192             gf bb[NN - KK + 1], gf eras_val[NN-KK], int eras_pos[NN-KK], 
193             int no_eras)
194 {
195   int deg_lambda, el, deg_omega;
196   int i, j, r,k;
197   gf u,q,tmp,num1,num2,den,discr_r;
198   gf lambda[NN-KK + 1], s[NN-KK + 1];   /* Err+Eras Locator poly
199                                          * and syndrome poly */
200   gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
201   gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
202   int syn_error, count;
203
204   syn_error = 0;
205   for(i=0;i<NN-KK;i++)
206       syn_error |= bb[i];
207
208   if (!syn_error) {
209     /* if remainder is zero, data[] is a codeword and there are no
210      * errors to correct. So return data[] unmodified
211      */
212     count = 0;
213     goto finish;
214   }
215   
216   for(i=1;i<=NN-KK;i++){
217     s[i] = bb[0];
218   }
219   for(j=1;j<NN-KK;j++){
220     if(bb[j] == 0)
221       continue;
222     tmp = Index_of[bb[j]];
223     
224     for(i=1;i<=NN-KK;i++)
225       s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*PRIM*j)];
226   }
227
228   /* undo the feedback register implicit multiplication and convert
229      syndromes to index form */
230
231   for(i=1;i<=NN-KK;i++) {
232       tmp = Index_of[s[i]];
233       if (tmp != A0)
234           tmp = modnn(tmp + 2 * KK * (B0+i-1)*PRIM);
235       s[i] = tmp;
236   }
237   
238   CLEAR(&lambda[1],NN-KK);
239   lambda[0] = 1;
240
241   if (no_eras > 0) {
242     /* Init lambda to be the erasure locator polynomial */
243     lambda[1] = Alpha_to[modnn(PRIM * eras_pos[0])];
244     for (i = 1; i < no_eras; i++) {
245       u = modnn(PRIM*eras_pos[i]);
246       for (j = i+1; j > 0; j--) {
247         tmp = Index_of[lambda[j - 1]];
248         if(tmp != A0)
249           lambda[j] ^= Alpha_to[modnn(u + tmp)];
250       }
251     }
252 #if DEBUG >= 1
253     /* Test code that verifies the erasure locator polynomial just constructed
254        Needed only for decoder debugging. */
255     
256     /* find roots of the erasure location polynomial */
257     for(i=1;i<=no_eras;i++)
258       reg[i] = Index_of[lambda[i]];
259     count = 0;
260     for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
261       q = 1;
262       for (j = 1; j <= no_eras; j++)
263         if (reg[j] != A0) {
264           reg[j] = modnn(reg[j] + j);
265           q ^= Alpha_to[reg[j]];
266         }
267       if (q != 0)
268         continue;
269       /* store root and error location number indices */
270       root[count] = i;
271       loc[count] = k;
272       count++;
273     }
274     if (count != no_eras) {
275       printf("\n lambda(x) is WRONG\n");
276       count = -1;
277       goto finish;
278     }
279 #if DEBUG >= 2
280     printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
281     for (i = 0; i < count; i++)
282       printf("%d ", loc[i]);
283     printf("\n");
284 #endif
285 #endif
286   }
287   for(i=0;i<NN-KK+1;i++)
288     b[i] = Index_of[lambda[i]];
289   
290   /*
291    * Begin Berlekamp-Massey algorithm to determine error+erasure
292    * locator polynomial
293    */
294   r = no_eras;
295   el = no_eras;
296   while (++r <= NN-KK) {        /* r is the step number */
297     /* Compute discrepancy at the r-th step in poly-form */
298     discr_r = 0;
299     for (i = 0; i < r; i++){
300       if ((lambda[i] != 0) && (s[r - i] != A0)) {
301         discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
302       }
303     }
304     discr_r = Index_of[discr_r];        /* Index form */
305     if (discr_r == A0) {
306       /* 2 lines below: B(x) <-- x*B(x) */
307       COPYDOWN(&b[1],b,NN-KK);
308       b[0] = A0;
309     } else {
310       /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
311       t[0] = lambda[0];
312       for (i = 0 ; i < NN-KK; i++) {
313         if(b[i] != A0)
314           t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
315         else
316           t[i+1] = lambda[i+1];
317       }
318       if (2 * el <= r + no_eras - 1) {
319         el = r + no_eras - el;
320         /*
321          * 2 lines below: B(x) <-- inv(discr_r) *
322          * lambda(x)
323          */
324         for (i = 0; i <= NN-KK; i++)
325           b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
326       } else {
327         /* 2 lines below: B(x) <-- x*B(x) */
328         COPYDOWN(&b[1],b,NN-KK);
329         b[0] = A0;
330       }
331       COPY(lambda,t,NN-KK+1);
332     }
333   }
334
335   /* Convert lambda to index form and compute deg(lambda(x)) */
336   deg_lambda = 0;
337   for(i=0;i<NN-KK+1;i++){
338     lambda[i] = Index_of[lambda[i]];
339     if(lambda[i] != A0)
340       deg_lambda = i;
341   }
342   /*
343    * Find roots of the error+erasure locator polynomial by Chien
344    * Search
345    */
346   COPY(&reg[1],&lambda[1],NN-KK);
347   count = 0;            /* Number of roots of lambda(x) */
348   for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
349     q = 1;
350     for (j = deg_lambda; j > 0; j--){
351       if (reg[j] != A0) {
352         reg[j] = modnn(reg[j] + j);
353         q ^= Alpha_to[reg[j]];
354       }
355     }
356     if (q != 0)
357       continue;
358     /* store root (index-form) and error location number */
359     root[count] = i;
360     loc[count] = k;
361     /* If we've already found max possible roots,
362      * abort the search to save time
363      */
364     if(++count == deg_lambda)
365       break;
366   }
367   if (deg_lambda != count) {
368     /*
369      * deg(lambda) unequal to number of roots => uncorrectable
370      * error detected
371      */
372     count = -1;
373     goto finish;
374   }
375   /*
376    * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
377    * x**(NN-KK)). in index form. Also find deg(omega).
378    */
379   deg_omega = 0;
380   for (i = 0; i < NN-KK;i++){
381     tmp = 0;
382     j = (deg_lambda < i) ? deg_lambda : i;
383     for(;j >= 0; j--){
384       if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
385         tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
386     }
387     if(tmp != 0)
388       deg_omega = i;
389     omega[i] = Index_of[tmp];
390   }
391   omega[NN-KK] = A0;
392   
393   /*
394    * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
395    * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
396    */
397   for (j = count-1; j >=0; j--) {
398     num1 = 0;
399     for (i = deg_omega; i >= 0; i--) {
400       if (omega[i] != A0)
401         num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
402     }
403     num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
404     den = 0;
405     
406     /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
407     for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
408       if(lambda[i+1] != A0)
409         den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
410     }
411     if (den == 0) {
412 #if DEBUG >= 1
413       printf("\n ERROR: denominator = 0\n");
414 #endif
415       /* Convert to dual- basis */
416       count = -1;
417       goto finish;
418     }
419     /* Apply error to data */
420     if (num1 != 0) {
421         eras_val[j] = Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
422     } else {
423         eras_val[j] = 0;
424     }
425   }
426  finish:
427   for(i=0;i<count;i++)
428       eras_pos[i] = loc[i];
429   return count;
430 }
431
432 /***************************************************************************/
433 /* The DOC specific code begins here */
434
435 #define SECTOR_SIZE 512
436 /* The sector bytes are packed into NB_DATA MM bits words */
437 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / MM)
438
439 /* 
440  * Correct the errors in 'sector[]' by using 'ecc1[]' which is the
441  * content of the feedback shift register applyied to the sector and
442  * the ECC. Return the number of errors corrected (and correct them in
443  * sector), or -1 if error 
444  */
445 int doc_decode_ecc(unsigned char sector[SECTOR_SIZE], unsigned char ecc1[6])
446 {
447     int parity, i, nb_errors;
448     gf bb[NN - KK + 1];
449     gf error_val[NN-KK];
450     int error_pos[NN-KK], pos, bitpos, index, val;
451     dtype *Alpha_to, *Index_of;
452
453     /* init log and exp tables here to save memory. However, it is slower */
454     Alpha_to = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
455     if (!Alpha_to)
456         return -1;
457     
458     Index_of = kmalloc((NN + 1) * sizeof(dtype), GFP_KERNEL);
459     if (!Index_of) {
460         kfree(Alpha_to);
461         return -1;
462     }
463
464     generate_gf(Alpha_to, Index_of);
465
466     parity = ecc1[1];
467
468     bb[0] =  (ecc1[4] & 0xff) | ((ecc1[5] & 0x03) << 8);
469     bb[1] = ((ecc1[5] & 0xfc) >> 2) | ((ecc1[2] & 0x0f) << 6);
470     bb[2] = ((ecc1[2] & 0xf0) >> 4) | ((ecc1[3] & 0x3f) << 4);
471     bb[3] = ((ecc1[3] & 0xc0) >> 6) | ((ecc1[0] & 0xff) << 2);
472
473     nb_errors = eras_dec_rs(Alpha_to, Index_of, bb, 
474                             error_val, error_pos, 0);
475     if (nb_errors <= 0)
476         goto the_end;
477
478     /* correct the errors */
479     for(i=0;i<nb_errors;i++) {
480         pos = error_pos[i];
481         if (pos >= NB_DATA && pos < KK) {
482             nb_errors = -1;
483             goto the_end;
484         }
485         if (pos < NB_DATA) {
486             /* extract bit position (MSB first) */
487             pos = 10 * (NB_DATA - 1 - pos) - 6;
488             /* now correct the following 10 bits. At most two bytes
489                can be modified since pos is even */
490             index = (pos >> 3) ^ 1;
491             bitpos = pos & 7;
492             if ((index >= 0 && index < SECTOR_SIZE) || 
493                 index == (SECTOR_SIZE + 1)) {
494                 val = error_val[i] >> (2 + bitpos);
495                 parity ^= val;
496                 if (index < SECTOR_SIZE)
497                     sector[index] ^= val;
498             }
499             index = ((pos >> 3) + 1) ^ 1;
500             bitpos = (bitpos + 10) & 7;
501             if (bitpos == 0)
502                 bitpos = 8;
503             if ((index >= 0 && index < SECTOR_SIZE) || 
504                 index == (SECTOR_SIZE + 1)) {
505                 val = error_val[i] << (8 - bitpos);
506                 parity ^= val;
507                 if (index < SECTOR_SIZE)
508                     sector[index] ^= val;
509             }
510         }
511     }
512     
513     /* use parity to test extra errors */
514     if ((parity & 0xff) != 0)
515         nb_errors = -1;
516
517  the_end:
518     kfree(Alpha_to);
519     kfree(Index_of);
520     return nb_errors;
521 }
522
523 EXPORT_SYMBOL_GPL(doc_decode_ecc);
524
525 MODULE_LICENSE("GPL");
526 MODULE_AUTHOR("Fabrice Bellard <fabrice.bellard@netgem.com>");
527 MODULE_DESCRIPTION("ECC code for correcting errors detected by DiskOnChip 2000 and Millennium ECC hardware");