nand_ecc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/mtd.h>
  45. #include <linux/mtd/nand.h>
  46. #include <linux/mtd/nand_ecc.h>
  47. #include <asm/byteorder.h>
  48. #else
  49. #include <stdint.h>
  50. struct mtd_info;
  51. #define EXPORT_SYMBOL(x) /* x */
  52. #define MODULE_LICENSE(x) /* x */
  53. #define MODULE_AUTHOR(x) /* x */
  54. #define MODULE_DESCRIPTION(x) /* x */
  55. #define printk printf
  56. #define KERN_ERR ""
  57. #endif
  58. /*
  59. * invparity is a 256 byte table that contains the odd parity
  60. * for each byte. So if the number of bits in a byte is even,
  61. * the array element is 1, and when the number of bits is odd
  62. * the array eleemnt is 0.
  63. */
  64. static const char invparity[256] = {
  65. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  66. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  67. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  68. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  69. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  70. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  71. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  72. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  73. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  74. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  75. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  76. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  77. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
  78. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  79. 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
  80. 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
  81. };
  82. /*
  83. * bitsperbyte contains the number of bits per byte
  84. * this is only used for testing and repairing parity
  85. * (a precalculated value slightly improves performance)
  86. */
  87. static const char bitsperbyte[256] = {
  88. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  89. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  90. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  91. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  92. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  93. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  94. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  95. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  96. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  97. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  98. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  99. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  100. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  101. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  102. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  103. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  104. };
  105. /*
  106. * addressbits is a lookup table to filter out the bits from the xor-ed
  107. * ecc data that identify the faulty location.
  108. * this is only used for repairing parity
  109. * see the comments in nand_correct_data for more details
  110. */
  111. static const char addressbits[256] = {
  112. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  113. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  114. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  115. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  116. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  117. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  118. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  119. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  120. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  121. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  122. 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01,
  123. 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03,
  124. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  125. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  126. 0x04, 0x04, 0x05, 0x05, 0x04, 0x04, 0x05, 0x05,
  127. 0x06, 0x06, 0x07, 0x07, 0x06, 0x06, 0x07, 0x07,
  128. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  129. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  130. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  131. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  132. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  133. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  134. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  135. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  136. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  137. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  138. 0x08, 0x08, 0x09, 0x09, 0x08, 0x08, 0x09, 0x09,
  139. 0x0a, 0x0a, 0x0b, 0x0b, 0x0a, 0x0a, 0x0b, 0x0b,
  140. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  141. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f,
  142. 0x0c, 0x0c, 0x0d, 0x0d, 0x0c, 0x0c, 0x0d, 0x0d,
  143. 0x0e, 0x0e, 0x0f, 0x0f, 0x0e, 0x0e, 0x0f, 0x0f
  144. };
  145. /**
  146. * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
  147. * block
  148. * @mtd: MTD block structure
  149. * @buf: input buffer with raw data
  150. * @code: output buffer with ECC
  151. */
  152. int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  153. unsigned char *code)
  154. {
  155. int i;
  156. const uint32_t *bp = (uint32_t *)buf;
  157. /* 256 or 512 bytes/ecc */
  158. const uint32_t eccsize_mult =
  159. (((struct nand_chip *)mtd->priv)->ecc.size) >> 8;
  160. uint32_t cur; /* current value in buffer */
  161. /* rp0..rp15..rp17 are the various accumulated parities (per byte) */
  162. uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
  163. uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16;
  164. uint32_t uninitialized_var(rp17); /* to make compiler happy */
  165. uint32_t par; /* the cumulative parity for all data */
  166. uint32_t tmppar; /* the cumulative parity for this iteration;
  167. for rp12, rp14 and rp16 at the end of the
  168. loop */
  169. par = 0;
  170. rp4 = 0;
  171. rp6 = 0;
  172. rp8 = 0;
  173. rp10 = 0;
  174. rp12 = 0;
  175. rp14 = 0;
  176. rp16 = 0;
  177. /*
  178. * The loop is unrolled a number of times;
  179. * This avoids if statements to decide on which rp value to update
  180. * Also we process the data by longwords.
  181. * Note: passing unaligned data might give a performance penalty.
  182. * It is assumed that the buffers are aligned.
  183. * tmppar is the cumulative sum of this iteration.
  184. * needed for calculating rp12, rp14, rp16 and par
  185. * also used as a performance improvement for rp6, rp8 and rp10
  186. */
  187. for (i = 0; i < eccsize_mult << 2; i++) {
  188. cur = *bp++;
  189. tmppar = cur;
  190. rp4 ^= cur;
  191. cur = *bp++;
  192. tmppar ^= cur;
  193. rp6 ^= tmppar;
  194. cur = *bp++;
  195. tmppar ^= cur;
  196. rp4 ^= cur;
  197. cur = *bp++;
  198. tmppar ^= cur;
  199. rp8 ^= tmppar;
  200. cur = *bp++;
  201. tmppar ^= cur;
  202. rp4 ^= cur;
  203. rp6 ^= cur;
  204. cur = *bp++;
  205. tmppar ^= cur;
  206. rp6 ^= cur;
  207. cur = *bp++;
  208. tmppar ^= cur;
  209. rp4 ^= cur;
  210. cur = *bp++;
  211. tmppar ^= cur;
  212. rp10 ^= tmppar;
  213. cur = *bp++;
  214. tmppar ^= cur;
  215. rp4 ^= cur;
  216. rp6 ^= cur;
  217. rp8 ^= cur;
  218. cur = *bp++;
  219. tmppar ^= cur;
  220. rp6 ^= cur;
  221. rp8 ^= cur;
  222. cur = *bp++;
  223. tmppar ^= cur;
  224. rp4 ^= cur;
  225. rp8 ^= cur;
  226. cur = *bp++;
  227. tmppar ^= cur;
  228. rp8 ^= cur;
  229. cur = *bp++;
  230. tmppar ^= cur;
  231. rp4 ^= cur;
  232. rp6 ^= cur;
  233. cur = *bp++;
  234. tmppar ^= cur;
  235. rp6 ^= cur;
  236. cur = *bp++;
  237. tmppar ^= cur;
  238. rp4 ^= cur;
  239. cur = *bp++;
  240. tmppar ^= cur;
  241. par ^= tmppar;
  242. if ((i & 0x1) == 0)
  243. rp12 ^= tmppar;
  244. if ((i & 0x2) == 0)
  245. rp14 ^= tmppar;
  246. if (eccsize_mult == 2 && (i & 0x4) == 0)
  247. rp16 ^= tmppar;
  248. }
  249. /*
  250. * handle the fact that we use longword operations
  251. * we'll bring rp4..rp14..rp16 back to single byte entities by
  252. * shifting and xoring first fold the upper and lower 16 bits,
  253. * then the upper and lower 8 bits.
  254. */
  255. rp4 ^= (rp4 >> 16);
  256. rp4 ^= (rp4 >> 8);
  257. rp4 &= 0xff;
  258. rp6 ^= (rp6 >> 16);
  259. rp6 ^= (rp6 >> 8);
  260. rp6 &= 0xff;
  261. rp8 ^= (rp8 >> 16);
  262. rp8 ^= (rp8 >> 8);
  263. rp8 &= 0xff;
  264. rp10 ^= (rp10 >> 16);
  265. rp10 ^= (rp10 >> 8);
  266. rp10 &= 0xff;
  267. rp12 ^= (rp12 >> 16);
  268. rp12 ^= (rp12 >> 8);
  269. rp12 &= 0xff;
  270. rp14 ^= (rp14 >> 16);
  271. rp14 ^= (rp14 >> 8);
  272. rp14 &= 0xff;
  273. if (eccsize_mult == 2) {
  274. rp16 ^= (rp16 >> 16);
  275. rp16 ^= (rp16 >> 8);
  276. rp16 &= 0xff;
  277. }
  278. /*
  279. * we also need to calculate the row parity for rp0..rp3
  280. * This is present in par, because par is now
  281. * rp3 rp3 rp2 rp2 in little endian and
  282. * rp2 rp2 rp3 rp3 in big endian
  283. * as well as
  284. * rp1 rp0 rp1 rp0 in little endian and
  285. * rp0 rp1 rp0 rp1 in big endian
  286. * First calculate rp2 and rp3
  287. */
  288. #ifdef __BIG_ENDIAN
  289. rp2 = (par >> 16);
  290. rp2 ^= (rp2 >> 8);
  291. rp2 &= 0xff;
  292. rp3 = par & 0xffff;
  293. rp3 ^= (rp3 >> 8);
  294. rp3 &= 0xff;
  295. #else
  296. rp3 = (par >> 16);
  297. rp3 ^= (rp3 >> 8);
  298. rp3 &= 0xff;
  299. rp2 = par & 0xffff;
  300. rp2 ^= (rp2 >> 8);
  301. rp2 &= 0xff;
  302. #endif
  303. /* reduce par to 16 bits then calculate rp1 and rp0 */
  304. par ^= (par >> 16);
  305. #ifdef __BIG_ENDIAN
  306. rp0 = (par >> 8) & 0xff;
  307. rp1 = (par & 0xff);
  308. #else
  309. rp1 = (par >> 8) & 0xff;
  310. rp0 = (par & 0xff);
  311. #endif
  312. /* finally reduce par to 8 bits */
  313. par ^= (par >> 8);
  314. par &= 0xff;
  315. /*
  316. * and calculate rp5..rp15..rp17
  317. * note that par = rp4 ^ rp5 and due to the commutative property
  318. * of the ^ operator we can say:
  319. * rp5 = (par ^ rp4);
  320. * The & 0xff seems superfluous, but benchmarking learned that
  321. * leaving it out gives slightly worse results. No idea why, probably
  322. * it has to do with the way the pipeline in pentium is organized.
  323. */
  324. rp5 = (par ^ rp4) & 0xff;
  325. rp7 = (par ^ rp6) & 0xff;
  326. rp9 = (par ^ rp8) & 0xff;
  327. rp11 = (par ^ rp10) & 0xff;
  328. rp13 = (par ^ rp12) & 0xff;
  329. rp15 = (par ^ rp14) & 0xff;
  330. if (eccsize_mult == 2)
  331. rp17 = (par ^ rp16) & 0xff;
  332. /*
  333. * Finally calculate the ecc bits.
  334. * Again here it might seem that there are performance optimisations
  335. * possible, but benchmarks showed that on the system this is developed
  336. * the code below is the fastest
  337. */
  338. #ifdef CONFIG_MTD_NAND_ECC_SMC
  339. code[0] =
  340. (invparity[rp7] << 7) |
  341. (invparity[rp6] << 6) |
  342. (invparity[rp5] << 5) |
  343. (invparity[rp4] << 4) |
  344. (invparity[rp3] << 3) |
  345. (invparity[rp2] << 2) |
  346. (invparity[rp1] << 1) |
  347. (invparity[rp0]);
  348. code[1] =
  349. (invparity[rp15] << 7) |
  350. (invparity[rp14] << 6) |
  351. (invparity[rp13] << 5) |
  352. (invparity[rp12] << 4) |
  353. (invparity[rp11] << 3) |
  354. (invparity[rp10] << 2) |
  355. (invparity[rp9] << 1) |
  356. (invparity[rp8]);
  357. #else
  358. code[1] =
  359. (invparity[rp7] << 7) |
  360. (invparity[rp6] << 6) |
  361. (invparity[rp5] << 5) |
  362. (invparity[rp4] << 4) |
  363. (invparity[rp3] << 3) |
  364. (invparity[rp2] << 2) |
  365. (invparity[rp1] << 1) |
  366. (invparity[rp0]);
  367. code[0] =
  368. (invparity[rp15] << 7) |
  369. (invparity[rp14] << 6) |
  370. (invparity[rp13] << 5) |
  371. (invparity[rp12] << 4) |
  372. (invparity[rp11] << 3) |
  373. (invparity[rp10] << 2) |
  374. (invparity[rp9] << 1) |
  375. (invparity[rp8]);
  376. #endif
  377. if (eccsize_mult == 1)
  378. code[2] =
  379. (invparity[par & 0xf0] << 7) |
  380. (invparity[par & 0x0f] << 6) |
  381. (invparity[par & 0xcc] << 5) |
  382. (invparity[par & 0x33] << 4) |
  383. (invparity[par & 0xaa] << 3) |
  384. (invparity[par & 0x55] << 2) |
  385. 3;
  386. else
  387. code[2] =
  388. (invparity[par & 0xf0] << 7) |
  389. (invparity[par & 0x0f] << 6) |
  390. (invparity[par & 0xcc] << 5) |
  391. (invparity[par & 0x33] << 4) |
  392. (invparity[par & 0xaa] << 3) |
  393. (invparity[par & 0x55] << 2) |
  394. (invparity[rp17] << 1) |
  395. (invparity[rp16] << 0);
  396. return 0;
  397. }
  398. EXPORT_SYMBOL(nand_calculate_ecc);
  399. /**
  400. * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  401. * @mtd: MTD block structure
  402. * @buf: raw data read from the chip
  403. * @read_ecc: ECC from the chip
  404. * @calc_ecc: the ECC calculated from raw data
  405. *
  406. * Detect and correct a 1 bit error for 256/512 byte block
  407. */
  408. int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
  409. unsigned char *read_ecc, unsigned char *calc_ecc)
  410. {
  411. unsigned char b0, b1, b2, bit_addr;
  412. unsigned int byte_addr;
  413. /* 256 or 512 bytes/ecc */
  414. const uint32_t eccsize_mult =
  415. (((struct nand_chip *)mtd->priv)->ecc.size) >> 8;
  416. /*
  417. * b0 to b2 indicate which bit is faulty (if any)
  418. * we might need the xor result more than once,
  419. * so keep them in a local var
  420. */
  421. #ifdef CONFIG_MTD_NAND_ECC_SMC
  422. b0 = read_ecc[0] ^ calc_ecc[0];
  423. b1 = read_ecc[1] ^ calc_ecc[1];
  424. #else
  425. b0 = read_ecc[1] ^ calc_ecc[1];
  426. b1 = read_ecc[0] ^ calc_ecc[0];
  427. #endif
  428. b2 = read_ecc[2] ^ calc_ecc[2];
  429. /* check if there are any bitfaults */
  430. /* repeated if statements are slightly more efficient than switch ... */
  431. /* ordered in order of likelihood */
  432. if ((b0 | b1 | b2) == 0)
  433. return 0; /* no error */
  434. if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
  435. (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
  436. ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) ||
  437. (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) {
  438. /* single bit error */
  439. /*
  440. * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty
  441. * byte, cp 5/3/1 indicate the faulty bit.
  442. * A lookup table (called addressbits) is used to filter
  443. * the bits from the byte they are in.
  444. * A marginal optimisation is possible by having three
  445. * different lookup tables.
  446. * One as we have now (for b0), one for b2
  447. * (that would avoid the >> 1), and one for b1 (with all values
  448. * << 4). However it was felt that introducing two more tables
  449. * hardly justify the gain.
  450. *
  451. * The b2 shift is there to get rid of the lowest two bits.
  452. * We could also do addressbits[b2] >> 1 but for the
  453. * performace it does not make any difference
  454. */
  455. if (eccsize_mult == 1)
  456. byte_addr = (addressbits[b1] << 4) + addressbits[b0];
  457. else
  458. byte_addr = (addressbits[b2 & 0x3] << 8) +
  459. (addressbits[b1] << 4) + addressbits[b0];
  460. bit_addr = addressbits[b2 >> 2];
  461. /* flip the bit */
  462. buf[byte_addr] ^= (1 << bit_addr);
  463. return 1;
  464. }
  465. /* count nr of bits; use table lookup, faster than calculating it */
  466. if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
  467. return 1; /* error in ecc data; no action needed */
  468. printk(KERN_ERR "uncorrectable error : ");
  469. return -1;
  470. }
  471. EXPORT_SYMBOL(nand_correct_data);
  472. MODULE_LICENSE("GPL");
  473. MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
  474. MODULE_DESCRIPTION("Generic NAND ECC support");