yaffs_ecc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <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. #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. /* Calculate the ECC for a 256-byte block of data */
  65. void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
  66. {
  67. unsigned int i;
  68. unsigned char col_parity = 0;
  69. unsigned char line_parity = 0;
  70. unsigned char line_parity_prime = 0;
  71. unsigned char t;
  72. unsigned char b;
  73. for (i = 0; i < 256; i++) {
  74. b = column_parity_table[*data++];
  75. col_parity ^= b;
  76. if (b & 0x01) { /* odd number of bits in the byte */
  77. line_parity ^= i;
  78. line_parity_prime ^= ~i;
  79. }
  80. }
  81. ecc[2] = (~col_parity) | 0x03;
  82. t = 0;
  83. if (line_parity & 0x80)
  84. t |= 0x80;
  85. if (line_parity_prime & 0x80)
  86. t |= 0x40;
  87. if (line_parity & 0x40)
  88. t |= 0x20;
  89. if (line_parity_prime & 0x40)
  90. t |= 0x10;
  91. if (line_parity & 0x20)
  92. t |= 0x08;
  93. if (line_parity_prime & 0x20)
  94. t |= 0x04;
  95. if (line_parity & 0x10)
  96. t |= 0x02;
  97. if (line_parity_prime & 0x10)
  98. t |= 0x01;
  99. ecc[1] = ~t;
  100. t = 0;
  101. if (line_parity & 0x08)
  102. t |= 0x80;
  103. if (line_parity_prime & 0x08)
  104. t |= 0x40;
  105. if (line_parity & 0x04)
  106. t |= 0x20;
  107. if (line_parity_prime & 0x04)
  108. t |= 0x10;
  109. if (line_parity & 0x02)
  110. t |= 0x08;
  111. if (line_parity_prime & 0x02)
  112. t |= 0x04;
  113. if (line_parity & 0x01)
  114. t |= 0x02;
  115. if (line_parity_prime & 0x01)
  116. t |= 0x01;
  117. ecc[0] = ~t;
  118. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  119. /* Swap the bytes into the wrong order */
  120. t = ecc[0];
  121. ecc[0] = ecc[1];
  122. ecc[1] = t;
  123. #endif
  124. }
  125. /* Correct the ECC on a 256 byte block of data */
  126. int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  127. const unsigned char *test_ecc)
  128. {
  129. unsigned char d0, d1, d2; /* deltas */
  130. d0 = read_ecc[0] ^ test_ecc[0];
  131. d1 = read_ecc[1] ^ test_ecc[1];
  132. d2 = read_ecc[2] ^ test_ecc[2];
  133. if ((d0 | d1 | d2) == 0)
  134. return 0; /* no error */
  135. if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  136. ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  137. ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  138. /* Single bit (recoverable) error in data */
  139. unsigned byte;
  140. unsigned bit;
  141. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  142. /* swap the bytes to correct for the wrong order */
  143. unsigned char t;
  144. t = d0;
  145. d0 = d1;
  146. d1 = t;
  147. #endif
  148. bit = byte = 0;
  149. if (d1 & 0x80)
  150. byte |= 0x80;
  151. if (d1 & 0x20)
  152. byte |= 0x40;
  153. if (d1 & 0x08)
  154. byte |= 0x20;
  155. if (d1 & 0x02)
  156. byte |= 0x10;
  157. if (d0 & 0x80)
  158. byte |= 0x08;
  159. if (d0 & 0x20)
  160. byte |= 0x04;
  161. if (d0 & 0x08)
  162. byte |= 0x02;
  163. if (d0 & 0x02)
  164. byte |= 0x01;
  165. if (d2 & 0x80)
  166. bit |= 0x04;
  167. if (d2 & 0x20)
  168. bit |= 0x02;
  169. if (d2 & 0x08)
  170. bit |= 0x01;
  171. data[byte] ^= (1 << bit);
  172. return 1; /* Corrected the error */
  173. }
  174. if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
  175. /* Reccoverable error in ecc */
  176. read_ecc[0] = test_ecc[0];
  177. read_ecc[1] = test_ecc[1];
  178. read_ecc[2] = test_ecc[2];
  179. return 1; /* Corrected the error */
  180. }
  181. /* Unrecoverable error */
  182. return -1;
  183. }
  184. /*
  185. * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  186. */
  187. void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  188. struct yaffs_ecc_other *ecc_other)
  189. {
  190. unsigned int i;
  191. unsigned char col_parity = 0;
  192. unsigned line_parity = 0;
  193. unsigned line_parity_prime = 0;
  194. unsigned char b;
  195. for (i = 0; i < n_bytes; i++) {
  196. b = column_parity_table[*data++];
  197. col_parity ^= b;
  198. if (b & 0x01) {
  199. /* odd number of bits in the byte */
  200. line_parity ^= i;
  201. line_parity_prime ^= ~i;
  202. }
  203. }
  204. ecc_other->col_parity = (col_parity >> 2) & 0x3f;
  205. ecc_other->line_parity = line_parity;
  206. ecc_other->line_parity_prime = line_parity_prime;
  207. }
  208. int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  209. struct yaffs_ecc_other *read_ecc,
  210. const struct yaffs_ecc_other *test_ecc)
  211. {
  212. unsigned char delta_col; /* column parity delta */
  213. unsigned delta_line; /* line parity delta */
  214. unsigned delta_line_prime; /* line parity delta */
  215. unsigned bit;
  216. delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
  217. delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
  218. delta_line_prime =
  219. read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
  220. if ((delta_col | delta_line | delta_line_prime) == 0)
  221. return 0; /* no error */
  222. if (delta_line == ~delta_line_prime &&
  223. (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
  224. /* Single bit (recoverable) error in data */
  225. bit = 0;
  226. if (delta_col & 0x20)
  227. bit |= 0x04;
  228. if (delta_col & 0x08)
  229. bit |= 0x02;
  230. if (delta_col & 0x02)
  231. bit |= 0x01;
  232. if (delta_line >= n_bytes)
  233. return -1;
  234. data[delta_line] ^= (1 << bit);
  235. return 1; /* corrected */
  236. }
  237. if ((hweight32(delta_line) +
  238. hweight32(delta_line_prime) +
  239. hweight8(delta_col)) == 1) {
  240. /* Reccoverable error in ecc */
  241. *read_ecc = *test_ecc;
  242. return 1; /* corrected */
  243. }
  244. /* Unrecoverable error */
  245. return -1;
  246. }