nanddrv_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include <rtdevice.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define NAND_SIM "nand.bin"
  6. #if 1
  7. #define OOB_SIZE 64
  8. #define PAGE_DATA_SIZE 2048
  9. #define PAGE_PER_BLOCK 64
  10. #define ECC_SIZE ((PAGE_DATA_SIZE) * 3 / 256)
  11. #define BLOCK_NUM 512
  12. #else
  13. #define OOB_SIZE 16
  14. #define PAGE_DATA_SIZE 512
  15. #define PAGE_PER_BLOCK 32
  16. #define ECC_SIZE ((PAGE_DATA_SIZE) * 3 / 256)
  17. #define BLOCK_NUM 512
  18. #endif
  19. #define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK)
  20. #define PAGE_SIZE (PAGE_DATA_SIZE + OOB_SIZE)
  21. static unsigned char block_data[BLOCK_SIZE];
  22. static struct rt_mtd_nand_device _nanddrv_file_device;
  23. static FILE *file = NULL;
  24. static rt_uint8_t CountBitsInByte(rt_uint8_t byte)
  25. {
  26. rt_uint8_t count = 0;
  27. while (byte > 0)
  28. {
  29. if (byte & 1)
  30. {
  31. count++;
  32. }
  33. byte >>= 1;
  34. }
  35. return count;
  36. }
  37. static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
  38. {
  39. rt_uint32_t i;
  40. rt_uint8_t columnSum = 0;
  41. rt_uint8_t evenLineCode = 0;
  42. rt_uint8_t oddLineCode = 0;
  43. rt_uint8_t evenColumnCode = 0;
  44. rt_uint8_t oddColumnCode = 0;
  45. // Xor all bytes together to get the column sum;
  46. // At the same time, calculate the even and odd line codes
  47. for (i = 0; i < 256; i++)
  48. {
  49. columnSum ^= data[i];
  50. // If the xor sum of the byte is 0, then this byte has no incidence on
  51. // the computed code; so check if the sum is 1.
  52. if ((CountBitsInByte(data[i]) & 1) == 1)
  53. {
  54. // Parity groups are formed by forcing a particular index bit to 0
  55. // (even) or 1 (odd).
  56. // Example on one byte:
  57. //
  58. // bits (dec) 7 6 5 4 3 2 1 0
  59. // (bin) 111 110 101 100 011 010 001 000
  60. // '---'---'---'----------.
  61. // |
  62. // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 |
  63. // P2' ooooooo eeeeeee ooooooo eeeeeee P2 |
  64. // P1' ooo eee ooo eee ooo eee ooo eee P1 |
  65. // |
  66. // We can see that: |
  67. // - P4 -> bit 2 of index is 0 --------------------'
  68. // - P4' -> bit 2 of index is 1.
  69. // - P2 -> bit 1 of index if 0.
  70. // - etc...
  71. // We deduce that a bit position has an impact on all even Px if
  72. // the log2(x)nth bit of its index is 0
  73. // ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)
  74. // and on all odd Px' if the log2(x)nth bit of its index is 1
  75. // ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)
  76. //
  77. // As such, we calculate all the possible Px and Px' values at the
  78. // same time in two variables, evenLineCode and oddLineCode, such as
  79. // evenLineCode bits: P128 P64 P32 P16 P8 P4 P2 P1
  80. // oddLineCode bits: P128' P64' P32' P16' P8' P4' P2' P1'
  81. //
  82. evenLineCode ^= (255 - i);
  83. oddLineCode ^= i;
  84. }
  85. }
  86. // At this point, we have the line parities, and the column sum. First, We
  87. // must caculate the parity group values on the column sum.
  88. for (i = 0; i < 8; i++)
  89. {
  90. if (columnSum & 1)
  91. {
  92. evenColumnCode ^= (7 - i);
  93. oddColumnCode ^= i;
  94. }
  95. columnSum >>= 1;
  96. }
  97. // Now, we must interleave the parity values, to obtain the following layout:
  98. // Code[0] = Line1
  99. // Code[1] = Line2
  100. // Code[2] = Column
  101. // Line = Px' Px P(x-1)- P(x-1) ...
  102. // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit
  103. code[0] = 0;
  104. code[1] = 0;
  105. code[2] = 0;
  106. for (i = 0; i < 4; i++)
  107. {
  108. code[0] <<= 2;
  109. code[1] <<= 2;
  110. code[2] <<= 2;
  111. // Line 1
  112. if ((oddLineCode & 0x80) != 0)
  113. {
  114. code[0] |= 2;
  115. }
  116. if ((evenLineCode & 0x80) != 0)
  117. {
  118. code[0] |= 1;
  119. }
  120. // Line 2
  121. if ((oddLineCode & 0x08) != 0)
  122. {
  123. code[1] |= 2;
  124. }
  125. if ((evenLineCode & 0x08) != 0)
  126. {
  127. code[1] |= 1;
  128. }
  129. // Column
  130. if ((oddColumnCode & 0x04) != 0)
  131. {
  132. code[2] |= 2;
  133. }
  134. if ((evenColumnCode & 0x04) != 0)
  135. {
  136. code[2] |= 1;
  137. }
  138. oddLineCode <<= 1;
  139. evenLineCode <<= 1;
  140. oddColumnCode <<= 1;
  141. evenColumnCode <<= 1;
  142. }
  143. // Invert codes (linux compatibility)
  144. code[0] = (~(rt_uint32_t)code[0]);
  145. code[1] = (~(rt_uint32_t)code[1]);
  146. code[2] = (~(rt_uint32_t)code[2]);
  147. }
  148. void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode)
  149. {
  150. while (dwSize > 0)
  151. {
  152. Compute256(pucData, puCode) ;
  153. pucData += 256;
  154. puCode += 3;
  155. dwSize -= 256;
  156. }
  157. }
  158. /* read chip id */
  159. static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device)
  160. {
  161. return 0x00;
  162. }
  163. /* read/write/move page */
  164. static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device,
  165. rt_off_t page,
  166. rt_uint8_t *data, rt_uint32_t data_len,
  167. rt_uint8_t *spare, rt_uint32_t spare_len)
  168. {
  169. rt_uint32_t offset;
  170. rt_uint8_t oob_ecc [ECC_SIZE];
  171. rt_uint8_t ecc [ECC_SIZE];
  172. page = page + device->block_start * device->pages_per_block;
  173. if (page / device->pages_per_block > device->block_end)
  174. {
  175. return -RT_EIO;
  176. }
  177. /* write page */
  178. offset = page * PAGE_SIZE;
  179. if (data != NULL && data_len != 0)
  180. {
  181. fseek(file, offset, SEEK_SET);
  182. fread(data, data_len, 1, file);
  183. if (data_len == PAGE_DATA_SIZE)
  184. {
  185. /* read ecc size */
  186. fread(oob_ecc, ECC_SIZE, 1, file);
  187. /* verify ECC */
  188. ecc_hamming_compute256x(data, PAGE_DATA_SIZE, &ecc[0]);
  189. if (memcmp(&oob_ecc[0], &ecc[0], ECC_SIZE) != 0)
  190. return -RT_MTD_EECC;
  191. }
  192. }
  193. if (spare != NULL && spare_len)
  194. {
  195. offset = page * PAGE_SIZE + PAGE_DATA_SIZE;
  196. fseek(file, offset, SEEK_SET);
  197. fread(spare, spare_len, 1, file);
  198. }
  199. return RT_EOK;
  200. }
  201. static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device,
  202. rt_off_t page,
  203. const rt_uint8_t *data, rt_uint32_t data_len,
  204. const rt_uint8_t *oob, rt_uint32_t spare_len)
  205. {
  206. rt_uint32_t offset;
  207. rt_uint8_t ecc[ECC_SIZE];
  208. page = page + device->block_start * device->pages_per_block;
  209. if (page / device->pages_per_block > device->block_end)
  210. {
  211. return -RT_EIO;
  212. }
  213. /* write page */
  214. offset = page * PAGE_SIZE;
  215. if (data != RT_NULL && data_len != 0)
  216. {
  217. fseek(file, offset, SEEK_SET);
  218. fwrite(data, data_len, 1, file);
  219. if (data_len == PAGE_DATA_SIZE)
  220. {
  221. /*write the ecc information */
  222. ecc_hamming_compute256x(data, PAGE_DATA_SIZE, ecc);
  223. fwrite(ecc, ECC_SIZE, 1, file);
  224. }
  225. }
  226. if (oob != RT_NULL && spare_len != 0)
  227. {
  228. offset = page * PAGE_SIZE + PAGE_DATA_SIZE + ECC_SIZE;
  229. fseek(file, offset, SEEK_SET);
  230. fwrite(&oob[ECC_SIZE], spare_len-ECC_SIZE, 1, file);
  231. }
  232. return RT_EOK;
  233. }
  234. static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to)
  235. {
  236. rt_uint32_t offset;
  237. rt_uint8_t page_buffer[PAGE_DATA_SIZE];
  238. rt_uint8_t oob_buffer[OOB_SIZE];
  239. from = from + device->block_start * device->pages_per_block;
  240. to = to + device->block_start * device->pages_per_block;
  241. if (from / device->pages_per_block > device->block_end ||
  242. to / device->pages_per_block > device->block_end)
  243. {
  244. return -RT_EIO;
  245. }
  246. if (device->plane_num > 1)
  247. {
  248. rt_uint32_t mask;
  249. rt_uint16_t from_block, to_block;
  250. from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
  251. to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
  252. mask = device->plane_num - 1;
  253. if ((from_block & mask) != (to_block & mask))
  254. {
  255. rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
  256. return -RT_EIO;
  257. }
  258. }
  259. /* read page */
  260. offset = from * PAGE_SIZE;
  261. fseek(file, offset, SEEK_SET);
  262. fread(page_buffer, sizeof(page_buffer), 1, file);
  263. fread(oob_buffer, sizeof(oob_buffer), 1, file);
  264. /* write page */
  265. offset = to * PAGE_SIZE;
  266. fseek(file, offset, SEEK_SET);
  267. fwrite(page_buffer, sizeof(page_buffer), 1, file);
  268. fwrite(oob_buffer, sizeof(oob_buffer), 1, file);
  269. return RT_EOK;
  270. }
  271. /* erase block */
  272. static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
  273. {
  274. if (block > BLOCK_NUM) return -RT_EIO;
  275. /* add the start blocks */
  276. block = block + device->block_start;
  277. fseek(file, block * BLOCK_SIZE, SEEK_SET);
  278. fwrite(block_data, sizeof(block_data), 1, file);
  279. return RT_EOK;
  280. }
  281. const static struct rt_mtd_nand_driver_ops _ops =
  282. {
  283. nanddrv_file_read_id,
  284. nanddrv_file_read_page,
  285. nanddrv_file_write_page,
  286. nanddrv_file_move_page,
  287. nanddrv_file_erase_block,
  288. RT_NULL,
  289. RT_NULL,
  290. };
  291. void nand_eraseall(void);
  292. void rt_hw_mtd_nand_init(void)
  293. {
  294. rt_uint16_t ecc_size;
  295. rt_uint32_t size;
  296. memset(block_data, 0xff, sizeof(block_data));
  297. /* open file */
  298. file = fopen(NAND_SIM, "rb+");
  299. if (file == NULL)
  300. {
  301. file = fopen(NAND_SIM, "wb+");
  302. }
  303. fseek(file, 0, SEEK_END);
  304. size = ftell(file);
  305. fseek(file, 0, SEEK_SET);
  306. if (size < BLOCK_NUM * BLOCK_SIZE)
  307. {
  308. rt_uint32_t index;
  309. fseek(file, 0, SEEK_SET);
  310. for (index = 0; index < BLOCK_NUM; index ++)
  311. {
  312. fwrite(block_data, sizeof(block_data), 1, file);
  313. }
  314. }
  315. fseek(file, 0, SEEK_SET);
  316. ecc_size = (PAGE_DATA_SIZE) * 3 / 256;
  317. _nanddrv_file_device.plane_num = 2;
  318. _nanddrv_file_device.oob_size = OOB_SIZE;
  319. _nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
  320. _nanddrv_file_device.page_size = PAGE_DATA_SIZE;
  321. _nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
  322. _nanddrv_file_device.block_start = 0;
  323. _nanddrv_file_device.block_end = BLOCK_NUM / 2;
  324. _nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
  325. _nanddrv_file_device.ops = &_ops;
  326. rt_mtd_nand_register_device("nand0", &_nanddrv_file_device);
  327. }
  328. #if defined(RT_USING_FINSH)
  329. #include <finsh.h>
  330. void nand_eraseall()
  331. {
  332. int index;
  333. for (index = 0; index < _nanddrv_file_device.block_total; index ++)
  334. {
  335. nanddrv_file_erase_block(&_nanddrv_file_device, index);
  336. }
  337. }
  338. FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash);
  339. #endif //RT_USING_FINSH