nand_ecc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * This file contains an ECC algorithm from Toshiba that detects and
  3. * corrects 1 bit errors in a 256 byte block of data.
  4. *
  5. * drivers/mtd/nand/nand_ecc.c
  6. *
  7. * Copyright (C) 2000-2004 Steven J. Hill (sjhill@realitydiluted.com)
  8. * Toshiba America Electronics Components, Inc.
  9. *
  10. * $Id: nand_ecc.c,v 1.14 2004/06/16 15:34:37 gleixner Exp $
  11. *
  12. * This file is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 or (at your option) any
  15. * later version.
  16. *
  17. * This file is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  20. * for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this file; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  26. * As a special exception, if other files instantiate templates or use
  27. * macros or inline functions from these files, or you compile these
  28. * files and link them with other works to produce a work based on these
  29. * files, these files do not by themselves cause the resulting work to be
  30. * covered by the GNU General Public License. However the source code for
  31. * these files must still be made available in accordance with section (3)
  32. * of the GNU General Public License.
  33. *
  34. * This exception does not invalidate any other reasons why a work based on
  35. * this file might be covered by the GNU General Public License.
  36. */
  37. #include <common.h>
  38. #ifdef CONFIG_NEW_NAND_CODE
  39. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  40. #include<linux/mtd/mtd.h>
  41. /*
  42. * Pre-calculated 256-way 1 byte column parity
  43. */
  44. static const u_char nand_ecc_precalc_table[] = {
  45. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
  46. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  47. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  48. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  49. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  50. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  51. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  52. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  53. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  54. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  55. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  56. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  57. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  58. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  59. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  60. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
  61. };
  62. /**
  63. * nand_trans_result - [GENERIC] create non-inverted ECC
  64. * @reg2: line parity reg 2
  65. * @reg3: line parity reg 3
  66. * @ecc_code: ecc
  67. *
  68. * Creates non-inverted ECC code from line parity
  69. */
  70. static void nand_trans_result(u_char reg2, u_char reg3,
  71. u_char *ecc_code)
  72. {
  73. u_char a, b, i, tmp1, tmp2;
  74. /* Initialize variables */
  75. a = b = 0x80;
  76. tmp1 = tmp2 = 0;
  77. /* Calculate first ECC byte */
  78. for (i = 0; i < 4; i++) {
  79. if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
  80. tmp1 |= b;
  81. b >>= 1;
  82. if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
  83. tmp1 |= b;
  84. b >>= 1;
  85. a >>= 1;
  86. }
  87. /* Calculate second ECC byte */
  88. b = 0x80;
  89. for (i = 0; i < 4; i++) {
  90. if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
  91. tmp2 |= b;
  92. b >>= 1;
  93. if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
  94. tmp2 |= b;
  95. b >>= 1;
  96. a >>= 1;
  97. }
  98. /* Store two of the ECC bytes */
  99. ecc_code[0] = tmp1;
  100. ecc_code[1] = tmp2;
  101. }
  102. /**
  103. * nand_calculate_ecc - [NAND Interface] Calculate 3 byte ECC code for 256 byte block
  104. * @mtd: MTD block structure
  105. * @dat: raw data
  106. * @ecc_code: buffer for ECC
  107. */
  108. int nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  109. {
  110. u_char idx, reg1, reg2, reg3;
  111. int j;
  112. /* Initialize variables */
  113. reg1 = reg2 = reg3 = 0;
  114. ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
  115. /* Build up column parity */
  116. for(j = 0; j < 256; j++) {
  117. /* Get CP0 - CP5 from table */
  118. idx = nand_ecc_precalc_table[dat[j]];
  119. reg1 ^= (idx & 0x3f);
  120. /* All bit XOR = 1 ? */
  121. if (idx & 0x40) {
  122. reg3 ^= (u_char) j;
  123. reg2 ^= ~((u_char) j);
  124. }
  125. }
  126. /* Create non-inverted ECC code from line parity */
  127. nand_trans_result(reg2, reg3, ecc_code);
  128. /* Calculate final ECC code */
  129. ecc_code[0] = ~ecc_code[0];
  130. ecc_code[1] = ~ecc_code[1];
  131. ecc_code[2] = ((~reg1) << 2) | 0x03;
  132. return 0;
  133. }
  134. /**
  135. * nand_correct_data - [NAND Interface] Detect and correct bit error(s)
  136. * @mtd: MTD block structure
  137. * @dat: raw data read from the chip
  138. * @read_ecc: ECC from the chip
  139. * @calc_ecc: the ECC calculated from raw data
  140. *
  141. * Detect and correct a 1 bit error for 256 byte block
  142. */
  143. int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  144. {
  145. u_char a, b, c, d1, d2, d3, add, bit, i;
  146. /* Do error detection */
  147. d1 = calc_ecc[0] ^ read_ecc[0];
  148. d2 = calc_ecc[1] ^ read_ecc[1];
  149. d3 = calc_ecc[2] ^ read_ecc[2];
  150. if ((d1 | d2 | d3) == 0) {
  151. /* No errors */
  152. return 0;
  153. }
  154. else {
  155. a = (d1 ^ (d1 >> 1)) & 0x55;
  156. b = (d2 ^ (d2 >> 1)) & 0x55;
  157. c = (d3 ^ (d3 >> 1)) & 0x54;
  158. /* Found and will correct single bit error in the data */
  159. if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
  160. c = 0x80;
  161. add = 0;
  162. a = 0x80;
  163. for (i=0; i<4; i++) {
  164. if (d1 & c)
  165. add |= a;
  166. c >>= 2;
  167. a >>= 1;
  168. }
  169. c = 0x80;
  170. for (i=0; i<4; i++) {
  171. if (d2 & c)
  172. add |= a;
  173. c >>= 2;
  174. a >>= 1;
  175. }
  176. bit = 0;
  177. b = 0x04;
  178. c = 0x80;
  179. for (i=0; i<3; i++) {
  180. if (d3 & c)
  181. bit |= b;
  182. c >>= 2;
  183. b >>= 1;
  184. }
  185. b = 0x01;
  186. a = dat[add];
  187. a ^= (b << bit);
  188. dat[add] = a;
  189. return 1;
  190. } else {
  191. i = 0;
  192. while (d1) {
  193. if (d1 & 0x01)
  194. ++i;
  195. d1 >>= 1;
  196. }
  197. while (d2) {
  198. if (d2 & 0x01)
  199. ++i;
  200. d2 >>= 1;
  201. }
  202. while (d3) {
  203. if (d3 & 0x01)
  204. ++i;
  205. d3 >>= 1;
  206. }
  207. if (i == 1) {
  208. /* ECC Code Error Correction */
  209. read_ecc[0] = calc_ecc[0];
  210. read_ecc[1] = calc_ecc[1];
  211. read_ecc[2] = calc_ecc[2];
  212. return 2;
  213. }
  214. else {
  215. /* Uncorrectable Error */
  216. return -1;
  217. }
  218. }
  219. }
  220. /* Should never happen */
  221. return -1;
  222. }
  223. #endif /* CONFIG_COMMANDS & CFG_CMD_NAND */
  224. #endif /* CONFIG_NEW_NAND_CODE */