nand_ecc.c 16 KB

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