nand_ecc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * This file contains an ECC algorithm that detects and corrects 1 bit
  3. * errors in a 256 byte block of data.
  4. *
  5. * drivers/mtd/nand/nand_ecc.c
  6. *
  7. * Copyright © 2008 Koninklijke Philips Electronics NV.
  8. * Author: Frans Meulenbroeks
  9. *
  10. * Completely replaces the previous ECC implementation which was written by:
  11. * Steven J. Hill (sjhill@realitydiluted.com)
  12. * Thomas Gleixner (tglx@linutronix.de)
  13. *
  14. * Information on how this algorithm works and how it was developed
  15. * can be found in Documentation/mtd/nand_ecc.txt
  16. *
  17. * This file is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2 or (at your option) any
  20. * later version.
  21. *
  22. * This file is distributed in the hope that it will be useful, but WITHOUT
  23. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  24. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25. * for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this file; if not, write to the Free Software Foundation, Inc.,
  29. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  30. *
  31. */
  32. /*
  33. * The STANDALONE macro is useful when running the code outside the kernel
  34. * e.g. when running the code in a testbed or a benchmark program.
  35. * When STANDALONE is used, the module related macros are commented out
  36. * as well as the linux include files.
  37. * Instead a private definition of mtd_info is given to satisfy the compiler
  38. * (the code does not use mtd_info, so the code does not care)
  39. */
  40. #ifndef STANDALONE
  41. #include <linux/types.h>
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/mtd/nand_ecc.h>
  45. #else
  46. #include <stdint.h>
  47. struct mtd_info;
  48. #define EXPORT_SYMBOL(x) /* x */
  49. #define MODULE_LICENSE(x) /* x */
  50. #define MODULE_AUTHOR(x) /* x */
  51. #define MODULE_DESCRIPTION(x) /* x */
  52. #endif
  53. /*
  54. * invparity is a 256 byte table that contains the odd parity
  55. * for each byte. So if the number of bits in a byte is even,
  56. * the array element is 1, and when the number of bits is odd
  57. * the array eleemnt is 0.
  58. */
  59. static const char invparity[256] = {
  60. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  61. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  62. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  63. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  64. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  65. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  66. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  67. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  68. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  69. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  70. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  71. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  72. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  73. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  74. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  75. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
  76. };
  77. /*
  78. * bitsperbyte contains the number of bits per byte
  79. * this is only used for testing and repairing parity
  80. * (a precalculated value slightly improves performance)
  81. */
  82. static const char bitsperbyte[256] = {
  83. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  84. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  85. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  86. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  87. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  88. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  89. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  90. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  91. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  92. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  93. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  94. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  95. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  96. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  97. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  98. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  99. };
  100. /*
  101. * addressbits is a lookup table to filter out the bits from the xor-ed
  102. * ecc data that identify the faulty location.
  103. * this is only used for repairing parity
  104. * see the comments in nand_correct_data for more details
  105. */
  106. static const char addressbits[256] = {
  107. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  108. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  109. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  110. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  111. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  112. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  113. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  114. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  115. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  116. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  117. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  118. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  119. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  120. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  121. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  122. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  123. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  124. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  125. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  126. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  127. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  128. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  129. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  130. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  131. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  132. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  133. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  134. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  135. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  136. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  137. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  138. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
  139. };
  140. /**
  141. * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
  142. * @mtd: MTD block structure (unused)
  143. * @dat: raw data
  144. * @ecc_code: buffer for ECC
  145. */
  146. int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  147. unsigned char *code)
  148. {
  149. int i;
  150. const uint32_t *bp = (uint32_t *)buf;
  151. uint32_t cur; /* current value in buffer */
  152. /* rp0..rp15 are the various accumulated parities (per byte) */
  153. uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
  154. uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15;
  155. uint32_t par; /* the cumulative parity for all data */
  156. uint32_t tmppar; /* the cumulative parity for this iteration;
  157. for rp12 and rp14 at the end of the loop */
  158. par = 0;
  159. rp4 = 0;
  160. rp6 = 0;
  161. rp8 = 0;
  162. rp10 = 0;
  163. rp12 = 0;
  164. rp14 = 0;
  165. /*
  166. * The loop is unrolled a number of times;
  167. * This avoids if statements to decide on which rp value to update
  168. * Also we process the data by longwords.
  169. * Note: passing unaligned data might give a performance penalty.
  170. * It is assumed that the buffers are aligned.
  171. * tmppar is the cumulative sum of this iteration.
  172. * needed for calculating rp12, rp14 and par
  173. * also used as a performance improvement for rp6, rp8 and rp10
  174. */
  175. for (i = 0; i < 4; i++) {
  176. cur = *bp++;
  177. tmppar = cur;
  178. rp4 ^= cur;
  179. cur = *bp++;
  180. tmppar ^= cur;
  181. rp6 ^= tmppar;
  182. cur = *bp++;
  183. tmppar ^= cur;
  184. rp4 ^= cur;
  185. cur = *bp++;
  186. tmppar ^= cur;
  187. rp8 ^= tmppar;
  188. cur = *bp++;
  189. tmppar ^= cur;
  190. rp4 ^= cur;
  191. rp6 ^= cur;
  192. cur = *bp++;
  193. tmppar ^= cur;
  194. rp6 ^= cur;
  195. cur = *bp++;
  196. tmppar ^= cur;
  197. rp4 ^= cur;
  198. cur = *bp++;
  199. tmppar ^= cur;
  200. rp10 ^= tmppar;
  201. cur = *bp++;
  202. tmppar ^= cur;
  203. rp4 ^= cur;
  204. rp6 ^= cur;
  205. rp8 ^= cur;
  206. cur = *bp++;
  207. tmppar ^= cur;
  208. rp6 ^= cur;
  209. rp8 ^= cur;
  210. cur = *bp++;
  211. tmppar ^= cur;
  212. rp4 ^= cur;
  213. rp8 ^= cur;
  214. cur = *bp++;
  215. tmppar ^= cur;
  216. rp8 ^= cur;
  217. cur = *bp++;
  218. tmppar ^= cur;
  219. rp4 ^= cur;
  220. rp6 ^= cur;
  221. cur = *bp++;
  222. tmppar ^= cur;
  223. rp6 ^= cur;
  224. cur = *bp++;
  225. tmppar ^= cur;
  226. rp4 ^= cur;
  227. cur = *bp++;
  228. tmppar ^= cur;
  229. par ^= tmppar;
  230. if ((i & 0x1) == 0)
  231. rp12 ^= tmppar;
  232. if ((i & 0x2) == 0)
  233. rp14 ^= tmppar;
  234. }
  235. /*
  236. * handle the fact that we use longword operations
  237. * we'll bring rp4..rp14 back to single byte entities by shifting and
  238. * xoring first fold the upper and lower 16 bits,
  239. * then the upper and lower 8 bits.
  240. */
  241. rp4 ^= (rp4 >> 16);
  242. rp4 ^= (rp4 >> 8);
  243. rp4 &= 0xff;
  244. rp6 ^= (rp6 >> 16);
  245. rp6 ^= (rp6 >> 8);
  246. rp6 &= 0xff;
  247. rp8 ^= (rp8 >> 16);
  248. rp8 ^= (rp8 >> 8);
  249. rp8 &= 0xff;
  250. rp10 ^= (rp10 >> 16);
  251. rp10 ^= (rp10 >> 8);
  252. rp10 &= 0xff;
  253. rp12 ^= (rp12 >> 16);
  254. rp12 ^= (rp12 >> 8);
  255. rp12 &= 0xff;
  256. rp14 ^= (rp14 >> 16);
  257. rp14 ^= (rp14 >> 8);
  258. rp14 &= 0xff;
  259. /*
  260. * we also need to calculate the row parity for rp0..rp3
  261. * This is present in par, because par is now
  262. * rp3 rp3 rp2 rp2
  263. * as well as
  264. * rp1 rp0 rp1 rp0
  265. * First calculate rp2 and rp3
  266. * (and yes: rp2 = (par ^ rp3) & 0xff; but doing that did not
  267. * give a performance improvement)
  268. */
  269. rp3 = (par >> 16);
  270. rp3 ^= (rp3 >> 8);
  271. rp3 &= 0xff;
  272. rp2 = par & 0xffff;
  273. rp2 ^= (rp2 >> 8);
  274. rp2 &= 0xff;
  275. /* reduce par to 16 bits then calculate rp1 and rp0 */
  276. par ^= (par >> 16);
  277. rp1 = (par >> 8) & 0xff;
  278. rp0 = (par & 0xff);
  279. /* finally reduce par to 8 bits */
  280. par ^= (par >> 8);
  281. par &= 0xff;
  282. /*
  283. * and calculate rp5..rp15
  284. * note that par = rp4 ^ rp5 and due to the commutative property
  285. * of the ^ operator we can say:
  286. * rp5 = (par ^ rp4);
  287. * The & 0xff seems superfluous, but benchmarking learned that
  288. * leaving it out gives slightly worse results. No idea why, probably
  289. * it has to do with the way the pipeline in pentium is organized.
  290. */
  291. rp5 = (par ^ rp4) & 0xff;
  292. rp7 = (par ^ rp6) & 0xff;
  293. rp9 = (par ^ rp8) & 0xff;
  294. rp11 = (par ^ rp10) & 0xff;
  295. rp13 = (par ^ rp12) & 0xff;
  296. rp15 = (par ^ rp14) & 0xff;
  297. /*
  298. * Finally calculate the ecc bits.
  299. * Again here it might seem that there are performance optimisations
  300. * possible, but benchmarks showed that on the system this is developed
  301. * the code below is the fastest
  302. */
  303. #ifdef CONFIG_MTD_NAND_ECC_SMC
  304. code[0] =
  305. (invparity[rp7] << 7) |
  306. (invparity[rp6] << 6) |
  307. (invparity[rp5] << 5) |
  308. (invparity[rp4] << 4) |
  309. (invparity[rp3] << 3) |
  310. (invparity[rp2] << 2) |
  311. (invparity[rp1] << 1) |
  312. (invparity[rp0]);
  313. code[1] =
  314. (invparity[rp15] << 7) |
  315. (invparity[rp14] << 6) |
  316. (invparity[rp13] << 5) |
  317. (invparity[rp12] << 4) |
  318. (invparity[rp11] << 3) |
  319. (invparity[rp10] << 2) |
  320. (invparity[rp9] << 1) |
  321. (invparity[rp8]);
  322. #else
  323. code[1] =
  324. (invparity[rp7] << 7) |
  325. (invparity[rp6] << 6) |
  326. (invparity[rp5] << 5) |
  327. (invparity[rp4] << 4) |
  328. (invparity[rp3] << 3) |
  329. (invparity[rp2] << 2) |
  330. (invparity[rp1] << 1) |
  331. (invparity[rp0]);
  332. code[0] =
  333. (invparity[rp15] << 7) |
  334. (invparity[rp14] << 6) |
  335. (invparity[rp13] << 5) |
  336. (invparity[rp12] << 4) |
  337. (invparity[rp11] << 3) |
  338. (invparity[rp10] << 2) |
  339. (invparity[rp9] << 1) |
  340. (invparity[rp8]);
  341. #endif
  342. code[2] =
  343. (invparity[par & 0xf0] << 7) |
  344. (invparity[par & 0x0f] << 6) |
  345. (invparity[par & 0xcc] << 5) |
  346. (invparity[par & 0x33] << 4) |
  347. (invparity[par & 0xaa] << 3) |
  348. (invparity[par & 0x55] << 2) |
  349. 3;
  350. return 0;
  351. }
  352. EXPORT_SYMBOL(nand_calculate_ecc);
  353. /**
  354. * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  355. * @mtd: MTD block structure (unused)
  356. * @dat: raw data read from the chip
  357. * @read_ecc: ECC from the chip
  358. * @calc_ecc: the ECC calculated from raw data
  359. *
  360. * Detect and correct a 1 bit error for 256 byte block
  361. */
  362. int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
  363. unsigned char *read_ecc, unsigned char *calc_ecc)
  364. {
  365. int nr_bits;
  366. unsigned char b0, b1, b2;
  367. unsigned char byte_addr, bit_addr;
  368. /*
  369. * b0 to b2 indicate which bit is faulty (if any)
  370. * we might need the xor result more than once,
  371. * so keep them in a local var
  372. */
  373. #ifdef CONFIG_MTD_NAND_ECC_SMC
  374. b0 = read_ecc[0] ^ calc_ecc[0];
  375. b1 = read_ecc[1] ^ calc_ecc[1];
  376. #else
  377. b0 = read_ecc[1] ^ calc_ecc[1];
  378. b1 = read_ecc[0] ^ calc_ecc[0];
  379. #endif
  380. b2 = read_ecc[2] ^ calc_ecc[2];
  381. /* check if there are any bitfaults */
  382. /* count nr of bits; use table lookup, faster than calculating it */
  383. nr_bits = bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2];
  384. /* repeated if statements are slightly more efficient than switch ... */
  385. /* ordered in order of likelihood */
  386. if (nr_bits == 0)
  387. return 0; /* no error */
  388. if (nr_bits == 11) { /* correctable error */
  389. /*
  390. * rp15/13/11/9/7/5/3/1 indicate which byte is the faulty byte
  391. * cp 5/3/1 indicate the faulty bit.
  392. * A lookup table (called addressbits) is used to filter
  393. * the bits from the byte they are in.
  394. * A marginal optimisation is possible by having three
  395. * different lookup tables.
  396. * One as we have now (for b0), one for b2
  397. * (that would avoid the >> 1), and one for b1 (with all values
  398. * << 4). However it was felt that introducing two more tables
  399. * hardly justify the gain.
  400. *
  401. * The b2 shift is there to get rid of the lowest two bits.
  402. * We could also do addressbits[b2] >> 1 but for the
  403. * performace it does not make any difference
  404. */
  405. byte_addr = (addressbits[b1] << 4) + addressbits[b0];
  406. bit_addr = addressbits[b2 >> 2];
  407. /* flip the bit */
  408. buf[byte_addr] ^= (1 << bit_addr);
  409. return 1;
  410. }
  411. if (nr_bits == 1)
  412. return 1; /* error in ecc data; no action needed */
  413. return -1;
  414. }
  415. EXPORT_SYMBOL(nand_correct_data);
  416. MODULE_LICENSE("GPL");
  417. MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
  418. MODULE_DESCRIPTION("Generic NAND ECC support");