nand_ecc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. * @buf: input buffer with raw data
  149. * @eccsize: data bytes per ECC step (256 or 512)
  150. * @code: output buffer with ECC
  151. */
  152. void __nand_calculate_ecc(const unsigned char *buf, unsigned int eccsize,
  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 = eccsize >> 8;
  159. uint32_t cur; /* current value in buffer */
  160. /* rp0..rp15..rp17 are the various accumulated parities (per byte) */
  161. uint32_t rp0, rp1, rp2, rp3, rp4, rp5, rp6, rp7;
  162. uint32_t rp8, rp9, rp10, rp11, rp12, rp13, rp14, rp15, rp16;
  163. uint32_t uninitialized_var(rp17); /* to make compiler happy */
  164. uint32_t par; /* the cumulative parity for all data */
  165. uint32_t tmppar; /* the cumulative parity for this iteration;
  166. for rp12, rp14 and rp16 at the end of the
  167. loop */
  168. par = 0;
  169. rp4 = 0;
  170. rp6 = 0;
  171. rp8 = 0;
  172. rp10 = 0;
  173. rp12 = 0;
  174. rp14 = 0;
  175. rp16 = 0;
  176. /*
  177. * The loop is unrolled a number of times;
  178. * This avoids if statements to decide on which rp value to update
  179. * Also we process the data by longwords.
  180. * Note: passing unaligned data might give a performance penalty.
  181. * It is assumed that the buffers are aligned.
  182. * tmppar is the cumulative sum of this iteration.
  183. * needed for calculating rp12, rp14, rp16 and par
  184. * also used as a performance improvement for rp6, rp8 and rp10
  185. */
  186. for (i = 0; i < eccsize_mult << 2; i++) {
  187. cur = *bp++;
  188. tmppar = cur;
  189. rp4 ^= cur;
  190. cur = *bp++;
  191. tmppar ^= cur;
  192. rp6 ^= tmppar;
  193. cur = *bp++;
  194. tmppar ^= cur;
  195. rp4 ^= cur;
  196. cur = *bp++;
  197. tmppar ^= cur;
  198. rp8 ^= tmppar;
  199. cur = *bp++;
  200. tmppar ^= cur;
  201. rp4 ^= cur;
  202. rp6 ^= cur;
  203. cur = *bp++;
  204. tmppar ^= cur;
  205. rp6 ^= cur;
  206. cur = *bp++;
  207. tmppar ^= cur;
  208. rp4 ^= cur;
  209. cur = *bp++;
  210. tmppar ^= cur;
  211. rp10 ^= tmppar;
  212. cur = *bp++;
  213. tmppar ^= cur;
  214. rp4 ^= cur;
  215. rp6 ^= cur;
  216. rp8 ^= cur;
  217. cur = *bp++;
  218. tmppar ^= cur;
  219. rp6 ^= cur;
  220. rp8 ^= cur;
  221. cur = *bp++;
  222. tmppar ^= cur;
  223. rp4 ^= cur;
  224. rp8 ^= cur;
  225. cur = *bp++;
  226. tmppar ^= cur;
  227. rp8 ^= cur;
  228. cur = *bp++;
  229. tmppar ^= cur;
  230. rp4 ^= cur;
  231. rp6 ^= cur;
  232. cur = *bp++;
  233. tmppar ^= cur;
  234. rp6 ^= cur;
  235. cur = *bp++;
  236. tmppar ^= cur;
  237. rp4 ^= cur;
  238. cur = *bp++;
  239. tmppar ^= cur;
  240. par ^= tmppar;
  241. if ((i & 0x1) == 0)
  242. rp12 ^= tmppar;
  243. if ((i & 0x2) == 0)
  244. rp14 ^= tmppar;
  245. if (eccsize_mult == 2 && (i & 0x4) == 0)
  246. rp16 ^= tmppar;
  247. }
  248. /*
  249. * handle the fact that we use longword operations
  250. * we'll bring rp4..rp14..rp16 back to single byte entities by
  251. * shifting and xoring first fold the upper and lower 16 bits,
  252. * then the upper and lower 8 bits.
  253. */
  254. rp4 ^= (rp4 >> 16);
  255. rp4 ^= (rp4 >> 8);
  256. rp4 &= 0xff;
  257. rp6 ^= (rp6 >> 16);
  258. rp6 ^= (rp6 >> 8);
  259. rp6 &= 0xff;
  260. rp8 ^= (rp8 >> 16);
  261. rp8 ^= (rp8 >> 8);
  262. rp8 &= 0xff;
  263. rp10 ^= (rp10 >> 16);
  264. rp10 ^= (rp10 >> 8);
  265. rp10 &= 0xff;
  266. rp12 ^= (rp12 >> 16);
  267. rp12 ^= (rp12 >> 8);
  268. rp12 &= 0xff;
  269. rp14 ^= (rp14 >> 16);
  270. rp14 ^= (rp14 >> 8);
  271. rp14 &= 0xff;
  272. if (eccsize_mult == 2) {
  273. rp16 ^= (rp16 >> 16);
  274. rp16 ^= (rp16 >> 8);
  275. rp16 &= 0xff;
  276. }
  277. /*
  278. * we also need to calculate the row parity for rp0..rp3
  279. * This is present in par, because par is now
  280. * rp3 rp3 rp2 rp2 in little endian and
  281. * rp2 rp2 rp3 rp3 in big endian
  282. * as well as
  283. * rp1 rp0 rp1 rp0 in little endian and
  284. * rp0 rp1 rp0 rp1 in big endian
  285. * First calculate rp2 and rp3
  286. */
  287. #ifdef __BIG_ENDIAN
  288. rp2 = (par >> 16);
  289. rp2 ^= (rp2 >> 8);
  290. rp2 &= 0xff;
  291. rp3 = par & 0xffff;
  292. rp3 ^= (rp3 >> 8);
  293. rp3 &= 0xff;
  294. #else
  295. rp3 = (par >> 16);
  296. rp3 ^= (rp3 >> 8);
  297. rp3 &= 0xff;
  298. rp2 = par & 0xffff;
  299. rp2 ^= (rp2 >> 8);
  300. rp2 &= 0xff;
  301. #endif
  302. /* reduce par to 16 bits then calculate rp1 and rp0 */
  303. par ^= (par >> 16);
  304. #ifdef __BIG_ENDIAN
  305. rp0 = (par >> 8) & 0xff;
  306. rp1 = (par & 0xff);
  307. #else
  308. rp1 = (par >> 8) & 0xff;
  309. rp0 = (par & 0xff);
  310. #endif
  311. /* finally reduce par to 8 bits */
  312. par ^= (par >> 8);
  313. par &= 0xff;
  314. /*
  315. * and calculate rp5..rp15..rp17
  316. * note that par = rp4 ^ rp5 and due to the commutative property
  317. * of the ^ operator we can say:
  318. * rp5 = (par ^ rp4);
  319. * The & 0xff seems superfluous, but benchmarking learned that
  320. * leaving it out gives slightly worse results. No idea why, probably
  321. * it has to do with the way the pipeline in pentium is organized.
  322. */
  323. rp5 = (par ^ rp4) & 0xff;
  324. rp7 = (par ^ rp6) & 0xff;
  325. rp9 = (par ^ rp8) & 0xff;
  326. rp11 = (par ^ rp10) & 0xff;
  327. rp13 = (par ^ rp12) & 0xff;
  328. rp15 = (par ^ rp14) & 0xff;
  329. if (eccsize_mult == 2)
  330. rp17 = (par ^ rp16) & 0xff;
  331. /*
  332. * Finally calculate the ECC bits.
  333. * Again here it might seem that there are performance optimisations
  334. * possible, but benchmarks showed that on the system this is developed
  335. * the code below is the fastest
  336. */
  337. #ifdef CONFIG_MTD_NAND_ECC_SMC
  338. code[0] =
  339. (invparity[rp7] << 7) |
  340. (invparity[rp6] << 6) |
  341. (invparity[rp5] << 5) |
  342. (invparity[rp4] << 4) |
  343. (invparity[rp3] << 3) |
  344. (invparity[rp2] << 2) |
  345. (invparity[rp1] << 1) |
  346. (invparity[rp0]);
  347. code[1] =
  348. (invparity[rp15] << 7) |
  349. (invparity[rp14] << 6) |
  350. (invparity[rp13] << 5) |
  351. (invparity[rp12] << 4) |
  352. (invparity[rp11] << 3) |
  353. (invparity[rp10] << 2) |
  354. (invparity[rp9] << 1) |
  355. (invparity[rp8]);
  356. #else
  357. code[1] =
  358. (invparity[rp7] << 7) |
  359. (invparity[rp6] << 6) |
  360. (invparity[rp5] << 5) |
  361. (invparity[rp4] << 4) |
  362. (invparity[rp3] << 3) |
  363. (invparity[rp2] << 2) |
  364. (invparity[rp1] << 1) |
  365. (invparity[rp0]);
  366. code[0] =
  367. (invparity[rp15] << 7) |
  368. (invparity[rp14] << 6) |
  369. (invparity[rp13] << 5) |
  370. (invparity[rp12] << 4) |
  371. (invparity[rp11] << 3) |
  372. (invparity[rp10] << 2) |
  373. (invparity[rp9] << 1) |
  374. (invparity[rp8]);
  375. #endif
  376. if (eccsize_mult == 1)
  377. code[2] =
  378. (invparity[par & 0xf0] << 7) |
  379. (invparity[par & 0x0f] << 6) |
  380. (invparity[par & 0xcc] << 5) |
  381. (invparity[par & 0x33] << 4) |
  382. (invparity[par & 0xaa] << 3) |
  383. (invparity[par & 0x55] << 2) |
  384. 3;
  385. else
  386. code[2] =
  387. (invparity[par & 0xf0] << 7) |
  388. (invparity[par & 0x0f] << 6) |
  389. (invparity[par & 0xcc] << 5) |
  390. (invparity[par & 0x33] << 4) |
  391. (invparity[par & 0xaa] << 3) |
  392. (invparity[par & 0x55] << 2) |
  393. (invparity[rp17] << 1) |
  394. (invparity[rp16] << 0);
  395. }
  396. EXPORT_SYMBOL(__nand_calculate_ecc);
  397. /**
  398. * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256/512-byte
  399. * block
  400. * @mtd: MTD block structure
  401. * @buf: input buffer with raw data
  402. * @code: output buffer with ECC
  403. */
  404. int nand_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  405. unsigned char *code)
  406. {
  407. __nand_calculate_ecc(buf,
  408. ((struct nand_chip *)mtd->priv)->ecc.size, code);
  409. return 0;
  410. }
  411. EXPORT_SYMBOL(nand_calculate_ecc);
  412. /**
  413. * __nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  414. * @buf: raw data read from the chip
  415. * @read_ecc: ECC from the chip
  416. * @calc_ecc: the ECC calculated from raw data
  417. * @eccsize: data bytes per ECC step (256 or 512)
  418. *
  419. * Detect and correct a 1 bit error for eccsize byte block
  420. */
  421. int __nand_correct_data(unsigned char *buf,
  422. unsigned char *read_ecc, unsigned char *calc_ecc,
  423. unsigned int eccsize)
  424. {
  425. unsigned char b0, b1, b2, bit_addr;
  426. unsigned int byte_addr;
  427. /* 256 or 512 bytes/ecc */
  428. const uint32_t eccsize_mult = eccsize >> 8;
  429. /*
  430. * b0 to b2 indicate which bit is faulty (if any)
  431. * we might need the xor result more than once,
  432. * so keep them in a local var
  433. */
  434. #ifdef CONFIG_MTD_NAND_ECC_SMC
  435. b0 = read_ecc[0] ^ calc_ecc[0];
  436. b1 = read_ecc[1] ^ calc_ecc[1];
  437. #else
  438. b0 = read_ecc[1] ^ calc_ecc[1];
  439. b1 = read_ecc[0] ^ calc_ecc[0];
  440. #endif
  441. b2 = read_ecc[2] ^ calc_ecc[2];
  442. /* check if there are any bitfaults */
  443. /* repeated if statements are slightly more efficient than switch ... */
  444. /* ordered in order of likelihood */
  445. if ((b0 | b1 | b2) == 0)
  446. return 0; /* no error */
  447. if ((((b0 ^ (b0 >> 1)) & 0x55) == 0x55) &&
  448. (((b1 ^ (b1 >> 1)) & 0x55) == 0x55) &&
  449. ((eccsize_mult == 1 && ((b2 ^ (b2 >> 1)) & 0x54) == 0x54) ||
  450. (eccsize_mult == 2 && ((b2 ^ (b2 >> 1)) & 0x55) == 0x55))) {
  451. /* single bit error */
  452. /*
  453. * rp17/rp15/13/11/9/7/5/3/1 indicate which byte is the faulty
  454. * byte, cp 5/3/1 indicate the faulty bit.
  455. * A lookup table (called addressbits) is used to filter
  456. * the bits from the byte they are in.
  457. * A marginal optimisation is possible by having three
  458. * different lookup tables.
  459. * One as we have now (for b0), one for b2
  460. * (that would avoid the >> 1), and one for b1 (with all values
  461. * << 4). However it was felt that introducing two more tables
  462. * hardly justify the gain.
  463. *
  464. * The b2 shift is there to get rid of the lowest two bits.
  465. * We could also do addressbits[b2] >> 1 but for the
  466. * performance it does not make any difference
  467. */
  468. if (eccsize_mult == 1)
  469. byte_addr = (addressbits[b1] << 4) + addressbits[b0];
  470. else
  471. byte_addr = (addressbits[b2 & 0x3] << 8) +
  472. (addressbits[b1] << 4) + addressbits[b0];
  473. bit_addr = addressbits[b2 >> 2];
  474. /* flip the bit */
  475. buf[byte_addr] ^= (1 << bit_addr);
  476. return 1;
  477. }
  478. /* count nr of bits; use table lookup, faster than calculating it */
  479. if ((bitsperbyte[b0] + bitsperbyte[b1] + bitsperbyte[b2]) == 1)
  480. return 1; /* error in ECC data; no action needed */
  481. printk(KERN_ERR "uncorrectable error : ");
  482. return -1;
  483. }
  484. EXPORT_SYMBOL(__nand_correct_data);
  485. /**
  486. * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  487. * @mtd: MTD block structure
  488. * @buf: raw data read from the chip
  489. * @read_ecc: ECC from the chip
  490. * @calc_ecc: the ECC calculated from raw data
  491. *
  492. * Detect and correct a 1 bit error for 256/512 byte block
  493. */
  494. int nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
  495. unsigned char *read_ecc, unsigned char *calc_ecc)
  496. {
  497. return __nand_correct_data(buf, read_ecc, calc_ecc,
  498. ((struct nand_chip *)mtd->priv)->ecc.size);
  499. }
  500. EXPORT_SYMBOL(nand_correct_data);
  501. MODULE_LICENSE("GPL");
  502. MODULE_AUTHOR("Frans Meulenbroeks <fransmeulenbroeks@gmail.com>");
  503. MODULE_DESCRIPTION("Generic NAND ECC support");