drv_sdh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-7-7 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_SDH)
  14. #include <rtdevice.h>
  15. #include "NuMicro.h"
  16. #include <drv_pdma.h>
  17. #include <string.h>
  18. #if defined(RT_USING_DFS)
  19. #include <dfs_fs.h>
  20. #include <dfs_file.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <sys/stat.h>
  24. #include <sys/statfs.h>
  25. #endif
  26. /* Private define ---------------------------------------------------------------*/
  27. #if defined(NU_SDH_MOUNT_ON_ROOT)
  28. #ifndef NU_SDH_MOUNTPOINT_SDH0
  29. #define NU_SDH_MOUNTPOINT_SDH0 "/"
  30. #endif
  31. #else
  32. #if !defined(NU_SDH_MOUNTPOINT_ROOT)
  33. #define NU_SDH_MOUNTPOINT_ROOT "/mnt"
  34. #endif
  35. #endif
  36. #if !defined(NU_SDH_MOUNTPOINT_SDH0)
  37. #define NU_SDH_MOUNTPOINT_SDH0 NU_SDH_MOUNTPOINT_ROOT"/sd0"
  38. #endif
  39. #if defined(NU_SDH_USING_PDMA)
  40. #define NU_SDH_MEMCPY nu_pdma_memcpy
  41. #else
  42. #define NU_SDH_MEMCPY memcpy
  43. #endif
  44. enum
  45. {
  46. SDH_START = -1,
  47. #if defined(BSP_USING_SDH0)
  48. SDH0_IDX,
  49. #endif
  50. SDH_CNT
  51. };
  52. #define SDH_BLOCK_SIZE 512ul
  53. #if defined(NU_SDH_HOTPLUG)
  54. #define NU_SDH_TID_STACK_SIZE 1024
  55. #endif
  56. #if defined(NU_SDH_HOTPLUG)
  57. enum
  58. {
  59. NU_SDH_CARD_DETECTED_SD0 = (1 << 0),
  60. NU_SDH_CARD_EVENT_ALL = (NU_SDH_CARD_DETECTED_SD0)
  61. };
  62. #endif
  63. /* Private typedef --------------------------------------------------------------*/
  64. struct nu_sdh
  65. {
  66. struct rt_device dev;
  67. char *name;
  68. #if defined(NU_SDH_HOTPLUG)
  69. char *mounted_point;
  70. #endif
  71. SDH_T *base;
  72. uint32_t is_card_inserted;
  73. SDH_INFO_T *info;
  74. struct rt_semaphore lock;
  75. uint8_t *pbuf;
  76. };
  77. typedef struct nu_sdh *nu_sdh_t;
  78. #if defined(NU_SDH_HOTPLUG)
  79. static struct rt_thread sdh_tid;
  80. static rt_uint8_t sdh_stack[NU_SDH_TID_STACK_SIZE];
  81. #endif
  82. /* Private functions ------------------------------------------------------------*/
  83. static void nu_sdh_isr(nu_sdh_t sdh);
  84. static rt_err_t nu_sdh_init(rt_device_t dev);
  85. static rt_err_t nu_sdh_open(rt_device_t dev, rt_uint16_t oflag);
  86. static rt_err_t nu_sdh_close(rt_device_t dev);
  87. static rt_size_t nu_sdh_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t blk_nb);
  88. static rt_err_t nu_sdh_control(rt_device_t dev, int cmd, void *args);
  89. static int rt_hw_sdh_init(void);
  90. #if defined(NU_SDH_HOTPLUG)
  91. static rt_bool_t nu_sdh_hotplug_is_mounted(const char *mounting_path);
  92. static void sdh_hotplugger(void *param);
  93. static rt_err_t nu_sdh_hotplug_mount(nu_sdh_t sdh);
  94. static rt_err_t nu_sdh_hotplug_unmount(nu_sdh_t sdh);
  95. #endif
  96. /* Public functions -------------------------------------------------------------*/
  97. /* Private variables ------------------------------------------------------------*/
  98. static struct nu_sdh nu_sdh_arr [] =
  99. {
  100. #if defined(BSP_USING_SDH0)
  101. {
  102. .name = "sdh0",
  103. #if defined(NU_SDH_HOTPLUG)
  104. .mounted_point = NU_SDH_MOUNTPOINT_SDH0,
  105. #endif
  106. .base = SDH0,
  107. .info = &SD0,
  108. },
  109. #endif
  110. {0}
  111. }; /* struct nu_sdh nu_sdh_arr [] */
  112. static struct rt_event sdh_event;
  113. static void nu_sdh_isr(nu_sdh_t sdh)
  114. {
  115. SDH_T *sdh_base = sdh->base;
  116. unsigned int volatile isr;
  117. unsigned int volatile ier;
  118. // FMI data abort interrupt
  119. if (sdh_base->GINTSTS & SDH_GINTSTS_DTAIF_Msk)
  120. {
  121. /* ResetAllEngine() */
  122. sdh_base->GCTL |= SDH_GCTL_GCTLRST_Msk;
  123. }
  124. //----- SD interrupt status
  125. isr = sdh_base->INTSTS;
  126. if (isr & SDH_INTSTS_BLKDIF_Msk)
  127. {
  128. // block down
  129. g_u8SDDataReadyFlag = TRUE;
  130. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_BLKDIF_Msk);
  131. }
  132. if (isr & SDH_INTSTS_CDIF_Msk) // card detect
  133. {
  134. #if defined(NU_SDH_HOTPLUG)
  135. if (sdh->base == SDH0)
  136. rt_event_send(&sdh_event, NU_SDH_CARD_DETECTED_SD0);
  137. #endif
  138. /* Clear CDIF interrupt flag */
  139. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_CDIF_Msk);
  140. }
  141. // CRC error interrupt
  142. if (isr & SDH_INTSTS_CRCIF_Msk)
  143. {
  144. if (!(isr & SDH_INTSTS_CRC16_Msk))
  145. {
  146. /* CRC_16 error */
  147. // TODO: handle CRC 16 error
  148. }
  149. else if (!(isr & SDH_INTSTS_CRC7_Msk))
  150. {
  151. if (!g_u8R3Flag)
  152. {
  153. /* CRC_7 error */
  154. // TODO: handle CRC 7 error
  155. }
  156. }
  157. /* Clear CRCIF interrupt flag */
  158. SDH_CLR_INT_FLAG(sdh_base, SDH_INTSTS_CRCIF_Msk);
  159. }
  160. /* Data-in timeout */
  161. if (isr & SDH_INTSTS_DITOIF_Msk)
  162. {
  163. sdh_base->INTSTS |= SDH_INTSTS_DITOIF_Msk;
  164. }
  165. /* Response-in timeout interrupt */
  166. if (isr & SDH_INTSTS_RTOIF_Msk)
  167. {
  168. sdh_base->INTSTS |= SDH_INTSTS_RTOIF_Msk;
  169. }
  170. }
  171. #if defined(BSP_USING_SDH0)
  172. void SDH0_IRQHandler(void)
  173. {
  174. /* enter interrupt */
  175. rt_interrupt_enter();
  176. nu_sdh_isr(&nu_sdh_arr[SDH0_IDX]);
  177. /* leave interrupt */
  178. rt_interrupt_leave();
  179. }
  180. #endif
  181. /* RT-Thread Device Driver Interface */
  182. static rt_err_t nu_sdh_init(rt_device_t dev)
  183. {
  184. return RT_EOK;
  185. }
  186. static rt_err_t nu_sdh_open(rt_device_t dev, rt_uint16_t oflag)
  187. {
  188. nu_sdh_t sdh = (nu_sdh_t)dev;
  189. RT_ASSERT(dev != RT_NULL);
  190. return (SDH_Probe(sdh->base) == 0) ? RT_EOK : -(RT_ERROR);
  191. }
  192. static rt_err_t nu_sdh_close(rt_device_t dev)
  193. {
  194. return RT_EOK;
  195. }
  196. static rt_size_t nu_sdh_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t blk_nb)
  197. {
  198. rt_uint32_t ret = 0;
  199. nu_sdh_t sdh = (nu_sdh_t)dev;
  200. RT_ASSERT(dev != RT_NULL);
  201. RT_ASSERT(buffer != RT_NULL);
  202. rt_sem_take(&sdh->lock, RT_WAITING_FOREVER);
  203. /* Check alignment. */
  204. if (((uint32_t)buffer & 0x03) != 0)
  205. {
  206. /* Non-aligned. */
  207. uint32_t i;
  208. uint8_t *copy_buffer = (uint8_t *)buffer;
  209. sdh->pbuf = rt_malloc(SDH_BLOCK_SIZE);
  210. if (sdh->pbuf == RT_NULL)
  211. goto exit_nu_sdh_read;
  212. for (i = 0; i < blk_nb; i++)
  213. {
  214. /* Read to temp buffer from specified sector. */
  215. ret = SDH_Read(sdh->base, &sdh->pbuf[0], pos, 1);
  216. if (ret != Successful)
  217. goto exit_nu_sdh_read;
  218. /* Move to user's buffer */
  219. NU_SDH_MEMCPY((void *)copy_buffer, (void *)&sdh->pbuf[0], SDH_BLOCK_SIZE);
  220. pos ++;
  221. copy_buffer += SDH_BLOCK_SIZE;
  222. }
  223. }
  224. else
  225. {
  226. /* Read to user's buffer from specified sector. */
  227. ret = SDH_Read(sdh->base, (uint8_t *)buffer, pos, blk_nb);
  228. }
  229. exit_nu_sdh_read:
  230. if (sdh->pbuf)
  231. {
  232. rt_free(sdh->pbuf);
  233. sdh->pbuf = RT_NULL;
  234. }
  235. rt_sem_release(&sdh->lock);
  236. if (ret == Successful)
  237. return blk_nb;
  238. rt_kprintf("Read failed: %d, buffer 0x%08x\n", ret, buffer);
  239. rt_set_errno(-RT_ENOSYS);
  240. return 0;
  241. }
  242. static rt_size_t nu_sdh_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t blk_nb)
  243. {
  244. rt_uint32_t ret = 0;
  245. nu_sdh_t sdh = (nu_sdh_t)dev;
  246. RT_ASSERT(dev != RT_NULL);
  247. RT_ASSERT(buffer != RT_NULL);
  248. rt_sem_take(&sdh->lock, RT_WAITING_FOREVER);
  249. /* Check alignment. */
  250. if (((uint32_t)buffer & 0x03) != 0)
  251. {
  252. /* Non-aligned. */
  253. uint32_t i;
  254. uint8_t *copy_buffer = (uint8_t *)buffer;
  255. sdh->pbuf = rt_malloc(SDH_BLOCK_SIZE);
  256. if (sdh->pbuf == RT_NULL)
  257. goto exit_nu_sdh_write;
  258. for (i = 0; i < blk_nb; i++)
  259. {
  260. NU_SDH_MEMCPY((void *)&sdh->pbuf[0], copy_buffer, SDH_BLOCK_SIZE);
  261. ret = SDH_Write(sdh->base, (uint8_t *)&sdh->pbuf[0], pos, 1);
  262. if (ret != Successful)
  263. goto exit_nu_sdh_write;
  264. pos++;
  265. copy_buffer += SDH_BLOCK_SIZE;
  266. }
  267. }
  268. else
  269. {
  270. /* Write to device directly. */
  271. ret = SDH_Write(sdh->base, (uint8_t *)buffer, pos, blk_nb);
  272. }
  273. exit_nu_sdh_write:
  274. if (sdh->pbuf)
  275. {
  276. rt_free(sdh->pbuf);
  277. sdh->pbuf = RT_NULL;
  278. }
  279. rt_sem_release(&sdh->lock);
  280. if (ret == Successful) return blk_nb;
  281. rt_kprintf("write failed: %d, buffer 0x%08x\n", ret, buffer);
  282. rt_set_errno(-RT_ENOSYS);
  283. return 0;
  284. }
  285. static rt_err_t nu_sdh_control(rt_device_t dev, int cmd, void *args)
  286. {
  287. nu_sdh_t sdh = (nu_sdh_t)dev;
  288. RT_ASSERT(dev != RT_NULL);
  289. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  290. {
  291. SDH_INFO_T *sdh_info = sdh->info;
  292. struct rt_device_blk_geometry *geometry;
  293. geometry = (struct rt_device_blk_geometry *)args;
  294. if (geometry == RT_NULL) return -RT_ERROR;
  295. geometry->bytes_per_sector = sdh_info->sectorSize;
  296. geometry->block_size = sdh_info->sectorSize;
  297. geometry->sector_count = sdh_info->totalSectorN;
  298. }
  299. return RT_EOK;
  300. }
  301. static int rt_hw_sdh_init(void)
  302. {
  303. int i;
  304. rt_err_t ret = RT_EOK;
  305. rt_uint32_t flags = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE;
  306. rt_event_init(&sdh_event, "sdh_event", RT_IPC_FLAG_FIFO);
  307. for (i = (SDH_START + 1); i < SDH_CNT; i++)
  308. {
  309. /* Register sdcard device */
  310. nu_sdh_arr[i].dev.type = RT_Device_Class_Block;
  311. nu_sdh_arr[i].dev.init = nu_sdh_init;
  312. nu_sdh_arr[i].dev.open = nu_sdh_open;
  313. nu_sdh_arr[i].dev.close = nu_sdh_close;
  314. nu_sdh_arr[i].dev.read = nu_sdh_read;
  315. nu_sdh_arr[i].dev.write = nu_sdh_write;
  316. nu_sdh_arr[i].dev.control = nu_sdh_control;
  317. /* Private */
  318. nu_sdh_arr[i].dev.user_data = (void *)&nu_sdh_arr[i];
  319. rt_sem_init(&nu_sdh_arr[i].lock, "sdhlock", 1, RT_IPC_FLAG_FIFO);
  320. SDH_Open(nu_sdh_arr[i].base, CardDetect_From_GPIO);
  321. /* Enable NVIC IRQ */
  322. if (nu_sdh_arr[i].base == SDH0)
  323. NVIC_EnableIRQ(SDH0_IRQn);
  324. nu_sdh_arr[i].pbuf = RT_NULL;
  325. ret = rt_device_register(&nu_sdh_arr[i].dev, nu_sdh_arr[i].name, flags);
  326. RT_ASSERT(ret == RT_EOK);
  327. }
  328. return (int)ret;
  329. }
  330. INIT_BOARD_EXPORT(rt_hw_sdh_init);
  331. #if defined(NU_SDH_HOTPLUG)
  332. static rt_bool_t nu_sdh_hotplug_is_mounted(const char *mounting_path)
  333. {
  334. rt_bool_t ret = RT_FALSE;
  335. #if defined(RT_USING_DFS)
  336. struct dfs_filesystem *psFS = dfs_filesystem_lookup(mounting_path);
  337. if (psFS == RT_NULL)
  338. {
  339. goto exit_nu_sdh_hotplug_is_mounted;
  340. }
  341. else if (!rt_memcmp(psFS->path, mounting_path, rt_strlen(mounting_path)))
  342. {
  343. ret = RT_TRUE;
  344. }
  345. else
  346. {
  347. ret = RT_FALSE;
  348. }
  349. #endif
  350. exit_nu_sdh_hotplug_is_mounted:
  351. return ret;
  352. }
  353. static rt_err_t nu_sdh_hotplug_mount(nu_sdh_t sdh)
  354. {
  355. rt_err_t ret = RT_ERROR;
  356. DIR *t;
  357. #if defined(RT_USING_DFS)
  358. if (nu_sdh_hotplug_is_mounted(sdh->mounted_point) == RT_TRUE)
  359. {
  360. ret = RT_EOK;
  361. goto exit_nu_sdh_hotplug_mount;
  362. }
  363. /* Check the SD folder path is valid. */
  364. if ((t = opendir(sdh->mounted_point)) != RT_NULL)
  365. {
  366. closedir(t);
  367. }
  368. #if !defined(NU_SDH_MOUNT_ON_ROOT)
  369. else
  370. {
  371. /* Check the ROOT path is valid. */
  372. if ((t = opendir(NU_SDH_MOUNTPOINT_ROOT)) != RT_NULL)
  373. {
  374. closedir(t);
  375. }
  376. else if ((ret = mkdir(NU_SDH_MOUNTPOINT_ROOT, 0)) != RT_EOK)
  377. {
  378. rt_kprintf("Failed to mkdir %s\n", NU_SDH_MOUNTPOINT_ROOT);
  379. goto exit_nu_sdh_hotplug_mount;
  380. }
  381. if ((ret = mkdir(sdh->mounted_point, 0)) != RT_EOK)
  382. {
  383. rt_kprintf("Failed to mkdir %s\n", sdh->mounted_point);
  384. goto exit_nu_sdh_hotplug_mount;
  385. }
  386. } //else
  387. #endif
  388. if ((ret = dfs_mount(sdh->name, sdh->mounted_point, "elm", 0, 0)) == 0)
  389. {
  390. rt_kprintf("Mounted %s on %s\n", sdh->name, sdh->mounted_point);
  391. }
  392. else
  393. {
  394. rt_kprintf("Failed to mount %s on %s\n", sdh->name, sdh->mounted_point);
  395. ret = RT_ERROR;
  396. }
  397. exit_nu_sdh_hotplug_mount:
  398. #endif
  399. return -(ret);
  400. }
  401. static rt_err_t nu_sdh_hotplug_unmount(nu_sdh_t sdh)
  402. {
  403. rt_err_t ret = RT_ERROR;
  404. #if defined(RT_USING_DFS)
  405. if (nu_sdh_hotplug_is_mounted(sdh->mounted_point) == RT_FALSE)
  406. {
  407. ret = RT_EOK;
  408. goto exit_nu_sdh_hotplug_unmount;
  409. }
  410. ret = dfs_unmount(sdh->mounted_point);
  411. if (ret != RT_EOK)
  412. {
  413. rt_kprintf("Failed to unmount %s.\n", sdh->mounted_point);
  414. }
  415. else
  416. {
  417. rt_kprintf("Succeed to unmount %s.\n", sdh->mounted_point);
  418. ret = RT_EOK;
  419. }
  420. #endif
  421. exit_nu_sdh_hotplug_unmount:
  422. return -(ret);
  423. }
  424. static void nu_card_detector(nu_sdh_t sdh)
  425. {
  426. SDH_T *sdh_base = sdh->base;
  427. unsigned int volatile isr = sdh_base->INTSTS;
  428. if (isr & SDH_INTSTS_CDSTS_Msk)
  429. {
  430. /* Card removed */
  431. sdh->info->IsCardInsert = FALSE; // SDISR_CD_Card = 1 means card remove for GPIO mode
  432. rt_memset((void *)sdh->info, 0, sizeof(SDH_INFO_T));
  433. nu_sdh_hotplug_unmount(sdh);
  434. }
  435. else
  436. {
  437. SDH_Open(sdh_base, CardDetect_From_GPIO);
  438. if (!SDH_Probe(sdh_base))
  439. {
  440. /* Card inserted */
  441. nu_sdh_hotplug_mount(sdh);
  442. }
  443. }
  444. }
  445. static void sdh_hotplugger(void *param)
  446. {
  447. rt_uint32_t e;
  448. int i;
  449. for (i = (SDH_START + 1); i < SDH_CNT; i++)
  450. {
  451. if (SDH_IS_CARD_PRESENT(nu_sdh_arr[i].base))
  452. {
  453. nu_sdh_hotplug_mount(&nu_sdh_arr[i]);
  454. }
  455. }
  456. while (1)
  457. {
  458. if (rt_event_recv(&sdh_event, (NU_SDH_CARD_EVENT_ALL),
  459. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  460. RT_WAITING_FOREVER, &e) == RT_EOK)
  461. {
  462. /* Debounce */
  463. rt_thread_delay(200);
  464. switch (e)
  465. {
  466. #if defined(BSP_USING_SDH0)
  467. case NU_SDH_CARD_DETECTED_SD0:
  468. nu_card_detector(&nu_sdh_arr[SDH0_IDX]);
  469. break;
  470. #endif
  471. default:
  472. break;
  473. } //switch(e)
  474. } //if
  475. } /* while(1) */
  476. }
  477. int mnt_init_sdcard_hotplug(void)
  478. {
  479. rt_thread_init(&sdh_tid, "hotplug", sdh_hotplugger, NULL, sdh_stack, sizeof(sdh_stack), RT_THREAD_PRIORITY_MAX - 2, 10);
  480. rt_thread_startup(&sdh_tid);
  481. return 0;
  482. }
  483. INIT_ENV_EXPORT(mnt_init_sdcard_hotplug);
  484. #endif
  485. #endif //#if defined(BSP_USING_SDH)