nand_ecc.c 15 KB

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