yaffs_ecc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 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
  19. * such ECC blocks are used on a 512-byte NAND page.
  20. *
  21. */
  22. #include "yportenv.h"
  23. #include "yaffs_ecc.h"
  24. /* Table generated by gen-ecc.c
  25. * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  26. * for each byte of data. These are instead provided in a table in bits7..2.
  27. * Bit 0 of each entry indicates whether the entry has an odd or even parity,
  28. * and therefore this bytes influence on the line parity.
  29. */
  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_calc(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. }
  119. /* Correct the ECC on a 256 byte block of data */
  120. int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  121. const unsigned char *test_ecc)
  122. {
  123. unsigned char d0, d1, d2; /* deltas */
  124. d0 = read_ecc[0] ^ test_ecc[0];
  125. d1 = read_ecc[1] ^ test_ecc[1];
  126. d2 = read_ecc[2] ^ test_ecc[2];
  127. if ((d0 | d1 | d2) == 0)
  128. return 0; /* no error */
  129. if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  130. ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  131. ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  132. /* Single bit (recoverable) error in data */
  133. unsigned byte;
  134. unsigned bit;
  135. bit = byte = 0;
  136. if (d1 & 0x80)
  137. byte |= 0x80;
  138. if (d1 & 0x20)
  139. byte |= 0x40;
  140. if (d1 & 0x08)
  141. byte |= 0x20;
  142. if (d1 & 0x02)
  143. byte |= 0x10;
  144. if (d0 & 0x80)
  145. byte |= 0x08;
  146. if (d0 & 0x20)
  147. byte |= 0x04;
  148. if (d0 & 0x08)
  149. byte |= 0x02;
  150. if (d0 & 0x02)
  151. byte |= 0x01;
  152. if (d2 & 0x80)
  153. bit |= 0x04;
  154. if (d2 & 0x20)
  155. bit |= 0x02;
  156. if (d2 & 0x08)
  157. bit |= 0x01;
  158. data[byte] ^= (1 << bit);
  159. return 1; /* Corrected the error */
  160. }
  161. if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
  162. /* Reccoverable error in ecc */
  163. read_ecc[0] = test_ecc[0];
  164. read_ecc[1] = test_ecc[1];
  165. read_ecc[2] = test_ecc[2];
  166. return 1; /* Corrected the error */
  167. }
  168. /* Unrecoverable error */
  169. return -1;
  170. }
  171. /*
  172. * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  173. */
  174. void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  175. struct yaffs_ecc_other *ecc_other)
  176. {
  177. unsigned int i;
  178. unsigned char col_parity = 0;
  179. unsigned line_parity = 0;
  180. unsigned line_parity_prime = 0;
  181. unsigned char b;
  182. for (i = 0; i < n_bytes; i++) {
  183. b = column_parity_table[*data++];
  184. col_parity ^= b;
  185. if (b & 0x01) {
  186. /* odd number of bits in the byte */
  187. line_parity ^= i;
  188. line_parity_prime ^= ~i;
  189. }
  190. }
  191. ecc_other->col_parity = (col_parity >> 2) & 0x3f;
  192. ecc_other->line_parity = line_parity;
  193. ecc_other->line_parity_prime = line_parity_prime;
  194. }
  195. int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  196. struct yaffs_ecc_other *read_ecc,
  197. const struct yaffs_ecc_other *test_ecc)
  198. {
  199. unsigned char delta_col; /* column parity delta */
  200. unsigned delta_line; /* line parity delta */
  201. unsigned delta_line_prime; /* line parity delta */
  202. unsigned bit;
  203. delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
  204. delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
  205. delta_line_prime =
  206. read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
  207. if ((delta_col | delta_line | delta_line_prime) == 0)
  208. return 0; /* no error */
  209. if (delta_line == ~delta_line_prime &&
  210. (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
  211. /* Single bit (recoverable) error in data */
  212. bit = 0;
  213. if (delta_col & 0x20)
  214. bit |= 0x04;
  215. if (delta_col & 0x08)
  216. bit |= 0x02;
  217. if (delta_col & 0x02)
  218. bit |= 0x01;
  219. if (delta_line >= n_bytes)
  220. return -1;
  221. data[delta_line] ^= (1 << bit);
  222. return 1; /* corrected */
  223. }
  224. if ((hweight32(delta_line) +
  225. hweight32(delta_line_prime) +
  226. hweight8(delta_col)) == 1) {
  227. /* Reccoverable error in ecc */
  228. *read_ecc = *test_ecc;
  229. return 1; /* corrected */
  230. }
  231. /* Unrecoverable error */
  232. return -1;
  233. }