nanddrv_file.c 11 KB

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