yaffs_ecc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  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. /* XXX U-BOOT XXX */
  29. #include <common.h>
  30. const char *yaffs_ecc_c_version =
  31. "$Id: yaffs_ecc.c,v 1.9 2007/02/14 01:09:06 wookey Exp $";
  32. #include "yportenv.h"
  33. #include "yaffs_ecc.h"
  34. static const unsigned char column_parity_table[] = {
  35. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  36. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  37. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  38. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  39. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  40. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  41. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  42. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  43. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  44. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  45. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  46. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  47. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  48. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  49. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  50. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  51. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  52. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  53. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  54. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  55. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  56. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  57. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  58. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  59. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  60. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  61. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  62. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  63. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  64. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  65. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  66. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  67. };
  68. /* Count the bits in an unsigned char or a U32 */
  69. static int yaffs_CountBits(unsigned char x)
  70. {
  71. int r = 0;
  72. while (x) {
  73. if (x & 1)
  74. r++;
  75. x >>= 1;
  76. }
  77. return r;
  78. }
  79. static int yaffs_CountBits32(unsigned x)
  80. {
  81. int r = 0;
  82. while (x) {
  83. if (x & 1)
  84. r++;
  85. x >>= 1;
  86. }
  87. return r;
  88. }
  89. /* Calculate the ECC for a 256-byte block of data */
  90. void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
  91. {
  92. unsigned int i;
  93. unsigned char col_parity = 0;
  94. unsigned char line_parity = 0;
  95. unsigned char line_parity_prime = 0;
  96. unsigned char t;
  97. unsigned char b;
  98. for (i = 0; i < 256; i++) {
  99. b = column_parity_table[*data++];
  100. col_parity ^= b;
  101. if (b & 0x01) // odd number of bits in the byte
  102. {
  103. line_parity ^= i;
  104. line_parity_prime ^= ~i;
  105. }
  106. }
  107. ecc[2] = (~col_parity) | 0x03;
  108. t = 0;
  109. if (line_parity & 0x80)
  110. t |= 0x80;
  111. if (line_parity_prime & 0x80)
  112. t |= 0x40;
  113. if (line_parity & 0x40)
  114. t |= 0x20;
  115. if (line_parity_prime & 0x40)
  116. t |= 0x10;
  117. if (line_parity & 0x20)
  118. t |= 0x08;
  119. if (line_parity_prime & 0x20)
  120. t |= 0x04;
  121. if (line_parity & 0x10)
  122. t |= 0x02;
  123. if (line_parity_prime & 0x10)
  124. t |= 0x01;
  125. ecc[1] = ~t;
  126. t = 0;
  127. if (line_parity & 0x08)
  128. t |= 0x80;
  129. if (line_parity_prime & 0x08)
  130. t |= 0x40;
  131. if (line_parity & 0x04)
  132. t |= 0x20;
  133. if (line_parity_prime & 0x04)
  134. t |= 0x10;
  135. if (line_parity & 0x02)
  136. t |= 0x08;
  137. if (line_parity_prime & 0x02)
  138. t |= 0x04;
  139. if (line_parity & 0x01)
  140. t |= 0x02;
  141. if (line_parity_prime & 0x01)
  142. t |= 0x01;
  143. ecc[0] = ~t;
  144. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  145. // Swap the bytes into the wrong order
  146. t = ecc[0];
  147. ecc[0] = ecc[1];
  148. ecc[1] = t;
  149. #endif
  150. }
  151. /* Correct the ECC on a 256 byte block of data */
  152. int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  153. const unsigned char *test_ecc)
  154. {
  155. unsigned char d0, d1, d2; /* deltas */
  156. d0 = read_ecc[0] ^ test_ecc[0];
  157. d1 = read_ecc[1] ^ test_ecc[1];
  158. d2 = read_ecc[2] ^ test_ecc[2];
  159. if ((d0 | d1 | d2) == 0)
  160. return 0; /* no error */
  161. if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  162. ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  163. ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  164. /* Single bit (recoverable) error in data */
  165. unsigned byte;
  166. unsigned bit;
  167. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  168. // swap the bytes to correct for the wrong order
  169. unsigned char t;
  170. t = d0;
  171. d0 = d1;
  172. d1 = t;
  173. #endif
  174. bit = byte = 0;
  175. if (d1 & 0x80)
  176. byte |= 0x80;
  177. if (d1 & 0x20)
  178. byte |= 0x40;
  179. if (d1 & 0x08)
  180. byte |= 0x20;
  181. if (d1 & 0x02)
  182. byte |= 0x10;
  183. if (d0 & 0x80)
  184. byte |= 0x08;
  185. if (d0 & 0x20)
  186. byte |= 0x04;
  187. if (d0 & 0x08)
  188. byte |= 0x02;
  189. if (d0 & 0x02)
  190. byte |= 0x01;
  191. if (d2 & 0x80)
  192. bit |= 0x04;
  193. if (d2 & 0x20)
  194. bit |= 0x02;
  195. if (d2 & 0x08)
  196. bit |= 0x01;
  197. data[byte] ^= (1 << bit);
  198. return 1; /* Corrected the error */
  199. }
  200. if ((yaffs_CountBits(d0) +
  201. yaffs_CountBits(d1) +
  202. yaffs_CountBits(d2)) == 1) {
  203. /* Reccoverable error in ecc */
  204. read_ecc[0] = test_ecc[0];
  205. read_ecc[1] = test_ecc[1];
  206. read_ecc[2] = test_ecc[2];
  207. return 1; /* Corrected the error */
  208. }
  209. /* Unrecoverable error */
  210. return -1;
  211. }
  212. /*
  213. * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  214. */
  215. void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  216. yaffs_ECCOther * eccOther)
  217. {
  218. unsigned int i;
  219. unsigned char col_parity = 0;
  220. unsigned line_parity = 0;
  221. unsigned line_parity_prime = 0;
  222. unsigned char b;
  223. for (i = 0; i < nBytes; i++) {
  224. b = column_parity_table[*data++];
  225. col_parity ^= b;
  226. if (b & 0x01) {
  227. /* odd number of bits in the byte */
  228. line_parity ^= i;
  229. line_parity_prime ^= ~i;
  230. }
  231. }
  232. eccOther->colParity = (col_parity >> 2) & 0x3f;
  233. eccOther->lineParity = line_parity;
  234. eccOther->lineParityPrime = line_parity_prime;
  235. }
  236. int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  237. yaffs_ECCOther * read_ecc,
  238. const yaffs_ECCOther * test_ecc)
  239. {
  240. unsigned char cDelta; /* column parity delta */
  241. unsigned lDelta; /* line parity delta */
  242. unsigned lDeltaPrime; /* line parity delta */
  243. unsigned bit;
  244. cDelta = read_ecc->colParity ^ test_ecc->colParity;
  245. lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
  246. lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
  247. if ((cDelta | lDelta | lDeltaPrime) == 0)
  248. return 0; /* no error */
  249. if (lDelta == ~lDeltaPrime &&
  250. (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
  251. {
  252. /* Single bit (recoverable) error in data */
  253. bit = 0;
  254. if (cDelta & 0x20)
  255. bit |= 0x04;
  256. if (cDelta & 0x08)
  257. bit |= 0x02;
  258. if (cDelta & 0x02)
  259. bit |= 0x01;
  260. if(lDelta >= nBytes)
  261. return -1;
  262. data[lDelta] ^= (1 << bit);
  263. return 1; /* corrected */
  264. }
  265. if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
  266. yaffs_CountBits(cDelta)) == 1) {
  267. /* Reccoverable error in ecc */
  268. *read_ecc = *test_ecc;
  269. return 1; /* corrected */
  270. }
  271. /* Unrecoverable error */
  272. return -1;
  273. }