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