]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/mtd/nand/nand_ecc.c
[MTD NAND] Indent all of drivers/mtd/nand/*.c.
[mirror_ubuntu-zesty-kernel.git] / drivers / mtd / nand / nand_ecc.c
1 /*
2 * This file contains an ECC algorithm from Toshiba that detects and
3 * corrects 1 bit errors in a 256 byte block of data.
4 *
5 * drivers/mtd/nand/nand_ecc.c
6 *
7 * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com)
8 * Toshiba America Electronics Components, Inc.
9 *
10 * $Id: nand_ecc.c,v 1.15 2005/11/07 11:14:30 gleixner Exp $
11 *
12 * This file is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 or (at your option) any
15 * later version.
16 *
17 * This file is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this file; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 *
26 * As a special exception, if other files instantiate templates or use
27 * macros or inline functions from these files, or you compile these
28 * files and link them with other works to produce a work based on these
29 * files, these files do not by themselves cause the resulting work to be
30 * covered by the GNU General Public License. However the source code for
31 * these files must still be made available in accordance with section (3)
32 * of the GNU General Public License.
33 *
34 * This exception does not invalidate any other reasons why a work based on
35 * this file might be covered by the GNU General Public License.
36 */
37
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/mtd/nand_ecc.h>
42
43 /*
44 * Pre-calculated 256-way 1 byte column parity
45 */
46 static const u_char nand_ecc_precalc_table[] = {
47 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
48 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
49 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
50 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
51 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
52 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
53 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
54 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
55 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
56 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
57 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
58 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
59 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
60 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
61 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
62 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
63 };
64
65 /**
66 * nand_trans_result - [GENERIC] create non-inverted ECC
67 * @reg2: line parity reg 2
68 * @reg3: line parity reg 3
69 * @ecc_code: ecc
70 *
71 * Creates non-inverted ECC code from line parity
72 */
73 static void nand_trans_result(u_char reg2, u_char reg3, u_char *ecc_code)
74 {
75 u_char a, b, i, tmp1, tmp2;
76
77 /* Initialize variables */
78 a = b = 0x80;
79 tmp1 = tmp2 = 0;
80
81 /* Calculate first ECC byte */
82 for (i = 0; i < 4; i++) {
83 if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
84 tmp1 |= b;
85 b >>= 1;
86 if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
87 tmp1 |= b;
88 b >>= 1;
89 a >>= 1;
90 }
91
92 /* Calculate second ECC byte */
93 b = 0x80;
94 for (i = 0; i < 4; i++) {
95 if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
96 tmp2 |= b;
97 b >>= 1;
98 if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
99 tmp2 |= b;
100 b >>= 1;
101 a >>= 1;
102 }
103
104 /* Store two of the ECC bytes */
105 ecc_code[0] = tmp1;
106 ecc_code[1] = tmp2;
107 }
108
109 /**
110 * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block
111 * @mtd: MTD block structure
112 * @dat: raw data
113 * @ecc_code: buffer for ECC
114 */
115 int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
116 {
117 u_char idx, reg1, reg2, reg3;
118 int j;
119
120 /* Initialize variables */
121 reg1 = reg2 = reg3 = 0;
122 ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
123
124 /* Build up column parity */
125 for (j = 0; j < 256; j++) {
126
127 /* Get CP0 - CP5 from table */
128 idx = nand_ecc_precalc_table[dat[j]];
129 reg1 ^= (idx & 0x3f);
130
131 /* All bit XOR = 1 ? */
132 if (idx & 0x40) {
133 reg3 ^= (u_char) j;
134 reg2 ^= ~((u_char) j);
135 }
136 }
137
138 /* Create non-inverted ECC code from line parity */
139 nand_trans_result(reg2, reg3, ecc_code);
140
141 /* Calculate final ECC code */
142 ecc_code[0] = ~ecc_code[0];
143 ecc_code[1] = ~ecc_code[1];
144 ecc_code[2] = ((~reg1) << 2) | 0x03;
145 return 0;
146 }
147
148 /**
149 * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
150 * @mtd: MTD block structure
151 * @dat: raw data read from the chip
152 * @read_ecc: ECC from the chip
153 * @calc_ecc: the ECC calculated from raw data
154 *
155 * Detect and correct a 1 bit error for 256 byte block
156 */
157 int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
158 {
159 u_char a, b, c, d1, d2, d3, add, bit, i;
160
161 /* Do error detection */
162 d1 = calc_ecc[0] ^ read_ecc[0];
163 d2 = calc_ecc[1] ^ read_ecc[1];
164 d3 = calc_ecc[2] ^ read_ecc[2];
165
166 if ((d1 | d2 | d3) == 0) {
167 /* No errors */
168 return 0;
169 } else {
170 a = (d1 ^ (d1 >> 1)) & 0x55;
171 b = (d2 ^ (d2 >> 1)) & 0x55;
172 c = (d3 ^ (d3 >> 1)) & 0x54;
173
174 /* Found and will correct single bit error in the data */
175 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
176 c = 0x80;
177 add = 0;
178 a = 0x80;
179 for (i = 0; i < 4; i++) {
180 if (d1 & c)
181 add |= a;
182 c >>= 2;
183 a >>= 1;
184 }
185 c = 0x80;
186 for (i = 0; i < 4; i++) {
187 if (d2 & c)
188 add |= a;
189 c >>= 2;
190 a >>= 1;
191 }
192 bit = 0;
193 b = 0x04;
194 c = 0x80;
195 for (i = 0; i < 3; i++) {
196 if (d3 & c)
197 bit |= b;
198 c >>= 2;
199 b >>= 1;
200 }
201 b = 0x01;
202 a = dat[add];
203 a ^= (b << bit);
204 dat[add] = a;
205 return 1;
206 } else {
207 i = 0;
208 while (d1) {
209 if (d1 & 0x01)
210 ++i;
211 d1 >>= 1;
212 }
213 while (d2) {
214 if (d2 & 0x01)
215 ++i;
216 d2 >>= 1;
217 }
218 while (d3) {
219 if (d3 & 0x01)
220 ++i;
221 d3 >>= 1;
222 }
223 if (i == 1) {
224 /* ECC Code Error Correction */
225 read_ecc[0] = calc_ecc[0];
226 read_ecc[1] = calc_ecc[1];
227 read_ecc[2] = calc_ecc[2];
228 return 2;
229 } else {
230 /* Uncorrectable Error */
231 return -1;
232 }
233 }
234 }
235
236 /* Should never happen */
237 return -1;
238 }
239
240 EXPORT_SYMBOL(nand_calculate_ecc);
241 EXPORT_SYMBOL(nand_correct_data);
242
243 MODULE_LICENSE("GPL");
244 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
245 MODULE_DESCRIPTION("Generic NAND ECC support");