yaffs_ecc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <[email protected]>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. /*
  14. * This code implements the ECC algorithm used in SmartMedia.
  15. *
  16. * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  17. * The two unused bit are set to 1.
  18. * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  19. * blocks are used on a 512-byte NAND page.
  20. *
  21. */
  22. /* Table generated by gen-ecc.c
  23. * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  24. * for each byte of data. These are instead provided in a table in bits7..2.
  25. * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  26. * this bytes influence on the line parity.
  27. */
  28. #include "yportenv.h"
  29. #include "yaffs_ecc.h"
  30. static const unsigned char column_parity_table[] = {
  31. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  32. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  33. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  34. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  35. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  36. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  37. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  38. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  39. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  40. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  41. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  42. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  43. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  44. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  45. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  46. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  47. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  48. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  49. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  50. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  51. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  52. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  53. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  54. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  55. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  56. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  57. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  58. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  59. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  60. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  61. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  62. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  63. };
  64. /* Count the bits in an unsigned char or a U32 */
  65. static int yaffs_count_bits(unsigned char x)
  66. {
  67. int r = 0;
  68. while (x) {
  69. if (x & 1)
  70. r++;
  71. x >>= 1;
  72. }
  73. return r;
  74. }
  75. static int yaffs_count_bits32(unsigned x)
  76. {
  77. int r = 0;
  78. while (x) {
  79. if (x & 1)
  80. r++;
  81. x >>= 1;
  82. }
  83. return r;
  84. }
  85. /* Calculate the ECC for a 256-byte block of data */
  86. void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
  87. {
  88. unsigned int i;
  89. unsigned char col_parity = 0;
  90. unsigned char line_parity = 0;
  91. unsigned char line_parity_prime = 0;
  92. unsigned char t;
  93. unsigned char b;
  94. for (i = 0; i < 256; i++) {
  95. b = column_parity_table[*data++];
  96. col_parity ^= b;
  97. if (b & 0x01) { /* odd number of bits in the byte */
  98. line_parity ^= i;
  99. line_parity_prime ^= ~i;
  100. }
  101. }
  102. ecc[2] = (~col_parity) | 0x03;
  103. t = 0;
  104. if (line_parity & 0x80)
  105. t |= 0x80;
  106. if (line_parity_prime & 0x80)
  107. t |= 0x40;
  108. if (line_parity & 0x40)
  109. t |= 0x20;
  110. if (line_parity_prime & 0x40)
  111. t |= 0x10;
  112. if (line_parity & 0x20)
  113. t |= 0x08;
  114. if (line_parity_prime & 0x20)
  115. t |= 0x04;
  116. if (line_parity & 0x10)
  117. t |= 0x02;
  118. if (line_parity_prime & 0x10)
  119. t |= 0x01;
  120. ecc[1] = ~t;
  121. t = 0;
  122. if (line_parity & 0x08)
  123. t |= 0x80;
  124. if (line_parity_prime & 0x08)
  125. t |= 0x40;
  126. if (line_parity & 0x04)
  127. t |= 0x20;
  128. if (line_parity_prime & 0x04)
  129. t |= 0x10;
  130. if (line_parity & 0x02)
  131. t |= 0x08;
  132. if (line_parity_prime & 0x02)
  133. t |= 0x04;
  134. if (line_parity & 0x01)
  135. t |= 0x02;
  136. if (line_parity_prime & 0x01)
  137. t |= 0x01;
  138. ecc[0] = ~t;
  139. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  140. /* Swap the bytes into the wrong order */
  141. t = ecc[0];
  142. ecc[0] = ecc[1];
  143. ecc[1] = t;
  144. #endif
  145. }
  146. /* Correct the ECC on a 256 byte block of data */
  147. int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  148. const unsigned char *test_ecc)
  149. {
  150. unsigned char d0, d1, d2; /* deltas */
  151. d0 = read_ecc[0] ^ test_ecc[0];
  152. d1 = read_ecc[1] ^ test_ecc[1];
  153. d2 = read_ecc[2] ^ test_ecc[2];
  154. if ((d0 | d1 | d2) == 0)
  155. return 0; /* no error */
  156. if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  157. ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  158. ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  159. /* Single bit (recoverable) error in data */
  160. unsigned byte;
  161. unsigned bit;
  162. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  163. /* swap the bytes to correct for the wrong order */
  164. unsigned char t;
  165. t = d0;
  166. d0 = d1;
  167. d1 = t;
  168. #endif
  169. bit = byte = 0;
  170. if (d1 & 0x80)
  171. byte |= 0x80;
  172. if (d1 & 0x20)
  173. byte |= 0x40;
  174. if (d1 & 0x08)
  175. byte |= 0x20;
  176. if (d1 & 0x02)
  177. byte |= 0x10;
  178. if (d0 & 0x80)
  179. byte |= 0x08;
  180. if (d0 & 0x20)
  181. byte |= 0x04;
  182. if (d0 & 0x08)
  183. byte |= 0x02;
  184. if (d0 & 0x02)
  185. byte |= 0x01;
  186. if (d2 & 0x80)
  187. bit |= 0x04;
  188. if (d2 & 0x20)
  189. bit |= 0x02;
  190. if (d2 & 0x08)
  191. bit |= 0x01;
  192. data[byte] ^= (1 << bit);
  193. return 1; /* Corrected the error */
  194. }
  195. if ((yaffs_count_bits(d0) +
  196. yaffs_count_bits(d1) +
  197. yaffs_count_bits(d2)) == 1) {
  198. /* Reccoverable error in ecc */
  199. read_ecc[0] = test_ecc[0];
  200. read_ecc[1] = test_ecc[1];
  201. read_ecc[2] = test_ecc[2];
  202. return 1; /* Corrected the error */
  203. }
  204. /* Unrecoverable error */
  205. return -1;
  206. }
  207. /*
  208. * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  209. */
  210. void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  211. yaffs_ECCOther *eccOther)
  212. {
  213. unsigned int i;
  214. unsigned char col_parity = 0;
  215. unsigned line_parity = 0;
  216. unsigned line_parity_prime = 0;
  217. unsigned char b;
  218. for (i = 0; i < n_bytes; i++) {
  219. b = column_parity_table[*data++];
  220. col_parity ^= b;
  221. if (b & 0x01) {
  222. /* odd number of bits in the byte */
  223. line_parity ^= i;
  224. line_parity_prime ^= ~i;
  225. }
  226. }
  227. eccOther->colParity = (col_parity >> 2) & 0x3f;
  228. eccOther->lineParity = line_parity;
  229. eccOther->lineParityPrime = line_parity_prime;
  230. }
  231. int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  232. yaffs_ECCOther *read_ecc,
  233. const yaffs_ECCOther *test_ecc)
  234. {
  235. unsigned char cDelta; /* column parity delta */
  236. unsigned lDelta; /* line parity delta */
  237. unsigned lDeltaPrime; /* line parity delta */
  238. unsigned bit;
  239. cDelta = read_ecc->colParity ^ test_ecc->colParity;
  240. lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
  241. lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
  242. if ((cDelta | lDelta | lDeltaPrime) == 0)
  243. return 0; /* no error */
  244. if (lDelta == ~lDeltaPrime &&
  245. (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) {
  246. /* Single bit (recoverable) error in data */
  247. bit = 0;
  248. if (cDelta & 0x20)
  249. bit |= 0x04;
  250. if (cDelta & 0x08)
  251. bit |= 0x02;
  252. if (cDelta & 0x02)
  253. bit |= 0x01;
  254. if (lDelta >= n_bytes)
  255. return -1;
  256. data[lDelta] ^= (1 << bit);
  257. return 1; /* corrected */
  258. }
  259. if ((yaffs_count_bits32(lDelta) + yaffs_count_bits32(lDeltaPrime) +
  260. yaffs_count_bits(cDelta)) == 1) {
  261. /* Reccoverable error in ecc */
  262. *read_ecc = *test_ecc;
  263. return 1; /* corrected */
  264. }
  265. /* Unrecoverable error */
  266. return -1;
  267. }