nanddrv_file.c 12 KB

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