00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <linux/types.h>
00043 #if 0
00044 #include <linux/kernel.h>
00045 #include <linux/module.h>
00046 #include <linux/mtd/nand_ecc.h>
00047 #endif
00048
00049 #include "nand_ecc.h"
00050
00051
00052
00053
00054 static const u_char nand_ecc_precalc_table[] = {
00055 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
00056 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
00057 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
00058 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
00059 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
00060 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
00061 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
00062 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
00063 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
00064 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
00065 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
00066 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
00067 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
00068 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
00069 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
00070 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
00071 };
00072
00073
00082 static void nand_trans_result(u_char reg2, u_char reg3,
00083 u_char *ecc_code)
00084 {
00085 u_char a, b, i, tmp1, tmp2;
00086
00087
00088 a = b = 0x80;
00089 tmp1 = tmp2 = 0;
00090
00091
00092 for (i = 0; i < 4; i++) {
00093 if (reg3 & a)
00094 tmp1 |= b;
00095 b >>= 1;
00096 if (reg2 & a)
00097 tmp1 |= b;
00098 b >>= 1;
00099 a >>= 1;
00100 }
00101
00102
00103 b = 0x80;
00104 for (i = 0; i < 4; i++) {
00105 if (reg3 & a)
00106 tmp2 |= b;
00107 b >>= 1;
00108 if (reg2 & a)
00109 tmp2 |= b;
00110 b >>= 1;
00111 a >>= 1;
00112 }
00113
00114
00115 ecc_code[0] = tmp1;
00116 ecc_code[1] = tmp2;
00117 }
00118
00125 int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
00126 {
00127 u_char idx, reg1, reg2, reg3;
00128 int j;
00129
00130
00131 reg1 = reg2 = reg3 = 0;
00132 ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
00133
00134
00135 for(j = 0; j < 256; j++) {
00136
00137
00138 idx = nand_ecc_precalc_table[dat[j]];
00139 reg1 ^= (idx & 0x3f);
00140
00141
00142 if (idx & 0x40) {
00143 reg3 ^= (u_char) j;
00144 reg2 ^= ~((u_char) j);
00145 }
00146 }
00147
00148
00149 nand_trans_result(reg2, reg3, ecc_code);
00150
00151
00152 ecc_code[0] = ~ecc_code[0];
00153 ecc_code[1] = ~ecc_code[1];
00154 ecc_code[2] = ((~reg1) << 2) | 0x03;
00155 return 0;
00156 }
00157
00167 int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
00168 {
00169 u_char a, b, c, d1, d2, d3, add, bit, i;
00170
00171
00172 d1 = calc_ecc[0] ^ read_ecc[0];
00173 d2 = calc_ecc[1] ^ read_ecc[1];
00174 d3 = calc_ecc[2] ^ read_ecc[2];
00175
00176 if ((d1 | d2 | d3) == 0) {
00177
00178 return 0;
00179 }
00180 else {
00181 a = (d1 ^ (d1 >> 1)) & 0x55;
00182 b = (d2 ^ (d2 >> 1)) & 0x55;
00183 c = (d3 ^ (d3 >> 1)) & 0x54;
00184
00185
00186 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
00187 c = 0x80;
00188 add = 0;
00189 a = 0x80;
00190 for (i=0; i<4; i++) {
00191 if (d1 & c)
00192 add |= a;
00193 c >>= 2;
00194 a >>= 1;
00195 }
00196 c = 0x80;
00197 for (i=0; i<4; i++) {
00198 if (d2 & c)
00199 add |= a;
00200 c >>= 2;
00201 a >>= 1;
00202 }
00203 bit = 0;
00204 b = 0x04;
00205 c = 0x80;
00206 for (i=0; i<3; i++) {
00207 if (d3 & c)
00208 bit |= b;
00209 c >>= 2;
00210 b >>= 1;
00211 }
00212 b = 0x01;
00213 a = dat[add];
00214 a ^= (b << bit);
00215 dat[add] = a;
00216 return 1;
00217 }
00218 else {
00219 i = 0;
00220 while (d1) {
00221 if (d1 & 0x01)
00222 ++i;
00223 d1 >>= 1;
00224 }
00225 while (d2) {
00226 if (d2 & 0x01)
00227 ++i;
00228 d2 >>= 1;
00229 }
00230 while (d3) {
00231 if (d3 & 0x01)
00232 ++i;
00233 d3 >>= 1;
00234 }
00235 if (i == 1) {
00236
00237 read_ecc[0] = calc_ecc[0];
00238 read_ecc[1] = calc_ecc[1];
00239 read_ecc[2] = calc_ecc[2];
00240 return 2;
00241 }
00242 else {
00243
00244 return -1;
00245 }
00246 }
00247 }
00248
00249
00250 return -1;
00251 }
00252
00253 EXPORT_SYMBOL(nand_calculate_ecc);
00254 EXPORT_SYMBOL(nand_correct_data);
00255
00256 MODULE_LICENSE("GPL");
00257 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
00258 MODULE_DESCRIPTION("Generic NAND ECC support");