nand_ecc.c 15 KB

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