drv_sdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * 2020-05-23 liuduanfei first version
  9. */
  10. #include "board.h"
  11. #include "drv_sdio.h"
  12. #include <dfs_fs.h>
  13. #ifdef BSP_USING_SDMMC
  14. #define DBG_TAG "drv.sdio"
  15. #ifdef DRV_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* DRV_DEBUG */
  20. #include <rtdbg.h>
  21. static struct rt_mmcsd_host *host;
  22. #define SDIO_TX_RX_COMPLETE_TIMEOUT_LOOPS (100000)
  23. struct sdio_pkg
  24. {
  25. struct rt_mmcsd_cmd *cmd;
  26. void *buff;
  27. rt_uint32_t flag;
  28. };
  29. struct rthw_sdio
  30. {
  31. struct rt_mmcsd_host *host;
  32. struct stm32_sdio_des sdio_des;
  33. struct rt_event event;
  34. struct sdio_pkg *pkg;
  35. };
  36. rt_align(SDIO_ALIGN_LEN)
  37. static rt_uint8_t cache_buf[SDIO_BUFF_SIZE];
  38. /**
  39. * @brief This function get order from sdio.
  40. * @param data
  41. * @retval sdio order
  42. */
  43. static int get_order(rt_uint32_t data)
  44. {
  45. int order = 0;
  46. switch (data)
  47. {
  48. case 1:
  49. order = 0;
  50. break;
  51. case 2:
  52. order = 1;
  53. break;
  54. case 4:
  55. order = 2;
  56. break;
  57. case 8:
  58. order = 3;
  59. break;
  60. case 16:
  61. order = 4;
  62. break;
  63. case 32:
  64. order = 5;
  65. break;
  66. case 64:
  67. order = 6;
  68. break;
  69. case 128:
  70. order = 7;
  71. break;
  72. case 256:
  73. order = 8;
  74. break;
  75. case 512:
  76. order = 9;
  77. break;
  78. case 1024:
  79. order = 10;
  80. break;
  81. case 2048:
  82. order = 11;
  83. break;
  84. case 4096:
  85. order = 12;
  86. break;
  87. case 8192:
  88. order = 13;
  89. break;
  90. case 16384:
  91. order = 14;
  92. break;
  93. default :
  94. order = 0;
  95. break;
  96. }
  97. return order;
  98. }
  99. /**
  100. * @brief This function wait sdio cmd completed.
  101. * @param sdio rthw_sdio
  102. * @retval None
  103. */
  104. static void rthw_sdio_wait_completed(struct rthw_sdio *sdio)
  105. {
  106. rt_uint32_t status;
  107. struct rt_mmcsd_cmd *cmd = sdio->pkg->cmd;
  108. struct stm32_sdio *hw_sdio = sdio->sdio_des.hw_sdio;
  109. if (rt_event_recv(&sdio->event, 0xffffffff, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  110. rt_tick_from_millisecond(5000), &status) != RT_EOK)
  111. {
  112. LOG_E("wait cmd completed timeout");
  113. cmd->err = -RT_ETIMEOUT;
  114. return;
  115. }
  116. cmd->resp[0] = hw_sdio->resp1;
  117. if (resp_type(cmd) == RESP_R2)
  118. {
  119. cmd->resp[1] = hw_sdio->resp2;
  120. cmd->resp[2] = hw_sdio->resp3;
  121. cmd->resp[3] = hw_sdio->resp4;
  122. }
  123. if (status & SDIO_ERRORS)
  124. {
  125. if ((status & SDMMC_STA_CCRCFAIL) && (resp_type(cmd) & (RESP_R3 | RESP_R4)))
  126. {
  127. cmd->err = RT_EOK;
  128. }
  129. else
  130. {
  131. cmd->err = -RT_ERROR;
  132. }
  133. }
  134. if (cmd->err == RT_EOK)
  135. {
  136. LOG_D("sta:0x%08X [%08X %08X %08X %08X]", status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  137. }
  138. else
  139. {
  140. LOG_D("send command error = %d", cmd->err);
  141. }
  142. }
  143. /**
  144. * @brief This function send command.
  145. * @param sdio rthw_sdio
  146. * @param pkg sdio package
  147. * @retval None
  148. */
  149. static void rthw_sdio_send_command(struct rthw_sdio *sdio, struct sdio_pkg *pkg)
  150. {
  151. struct rt_mmcsd_cmd *cmd = pkg->cmd;
  152. struct rt_mmcsd_data *data = cmd->data;
  153. struct stm32_sdio *hw_sdio = sdio->sdio_des.hw_sdio;
  154. rt_uint32_t reg_cmd;
  155. /* save pkg */
  156. sdio->pkg = pkg;
  157. LOG_D("CMD:%d ARG:0x%08x RES:%s%s%s%s%s%s%s%s%s rw:%c len:%d blksize:%d\n",
  158. cmd->cmd_code,
  159. cmd->arg,
  160. resp_type(cmd) == RESP_NONE ? "NONE" : "",
  161. resp_type(cmd) == RESP_R1 ? "R1" : "",
  162. resp_type(cmd) == RESP_R1B ? "R1B" : "",
  163. resp_type(cmd) == RESP_R2 ? "R2" : "",
  164. resp_type(cmd) == RESP_R3 ? "R3" : "",
  165. resp_type(cmd) == RESP_R4 ? "R4" : "",
  166. resp_type(cmd) == RESP_R5 ? "R5" : "",
  167. resp_type(cmd) == RESP_R6 ? "R6" : "",
  168. resp_type(cmd) == RESP_R7 ? "R7" : "",
  169. data ? (data->flags & DATA_DIR_WRITE ? 'w' : 'r') : '-',
  170. data ? data->blks * data->blksize : 0,
  171. data ? data->blksize : 0
  172. );
  173. hw_sdio->mask |= SDIO_MASKR_ALL;
  174. reg_cmd = cmd->cmd_code | SDMMC_CMD_CPSMEN;
  175. /* data pre configuration */
  176. if (data != RT_NULL)
  177. {
  178. SCB_CleanInvalidateDCache();
  179. reg_cmd |= SDMMC_CMD_CMDTRANS;
  180. hw_sdio->mask &= ~(SDMMC_MASK_CMDRENDIE | SDMMC_MASK_CMDSENTIE);
  181. hw_sdio->dtimer = HW_SDIO_DATATIMEOUT;
  182. hw_sdio->dlen = data->blks * data->blksize;
  183. hw_sdio->dctrl = (get_order(data->blksize)<<4) | (data->flags & DATA_DIR_READ ? SDMMC_DCTRL_DTDIR : 0);
  184. hw_sdio->idmabase0r = (rt_uint32_t)cache_buf;
  185. hw_sdio->idmatrlr = SDMMC_IDMA_IDMAEN;
  186. }
  187. if (resp_type(cmd) == RESP_R2)
  188. reg_cmd |= SDMMC_CMD_WAITRESP;
  189. else if(resp_type(cmd) != RESP_NONE)
  190. reg_cmd |= SDMMC_CMD_WAITRESP_0;
  191. hw_sdio->arg = cmd->arg;
  192. hw_sdio->cmd = reg_cmd;
  193. /* wait completed */
  194. rthw_sdio_wait_completed(sdio);
  195. /* Waiting for data to be sent to completion */
  196. if (data != RT_NULL)
  197. {
  198. volatile rt_uint32_t count = SDIO_TX_RX_COMPLETE_TIMEOUT_LOOPS;
  199. while (count && (hw_sdio->sta & SDMMC_STA_DPSMACT))
  200. {
  201. count--;
  202. }
  203. if ((count == 0) || (hw_sdio->sta & SDIO_ERRORS))
  204. {
  205. cmd->err = -RT_ERROR;
  206. }
  207. }
  208. /* data post configuration */
  209. if (data != RT_NULL)
  210. {
  211. if (data->flags & DATA_DIR_READ)
  212. {
  213. rt_memcpy(data->buf, cache_buf, data->blks * data->blksize);
  214. SCB_CleanInvalidateDCache();
  215. }
  216. }
  217. }
  218. /**
  219. * @brief This function send sdio request.
  220. * @param sdio rthw_sdio
  221. * @param req request
  222. * @retval None
  223. */
  224. static void rthw_sdio_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  225. {
  226. struct sdio_pkg pkg;
  227. struct rthw_sdio *sdio = host->private_data;
  228. struct rt_mmcsd_data *data;
  229. if (req->cmd != RT_NULL)
  230. {
  231. rt_memset(&pkg, 0, sizeof(pkg));
  232. data = req->cmd->data;
  233. pkg.cmd = req->cmd;
  234. if (data != RT_NULL)
  235. {
  236. rt_uint32_t size = data->blks * data->blksize;
  237. RT_ASSERT(size <= SDIO_BUFF_SIZE);
  238. if (data->flags & DATA_DIR_WRITE)
  239. {
  240. rt_memcpy(cache_buf, data->buf, size);
  241. }
  242. }
  243. rthw_sdio_send_command(sdio, &pkg);
  244. }
  245. if (req->stop != RT_NULL)
  246. {
  247. rt_memset(&pkg, 0, sizeof(pkg));
  248. pkg.cmd = req->stop;
  249. rthw_sdio_send_command(sdio, &pkg);
  250. }
  251. mmcsd_req_complete(sdio->host);
  252. }
  253. /**
  254. * @brief This function interrupt process function.
  255. * @param host rt_mmcsd_host
  256. * @retval None
  257. */
  258. void rthw_sdio_irq_process(struct rt_mmcsd_host *host)
  259. {
  260. struct rthw_sdio *sdio = host->private_data;
  261. struct stm32_sdio *hw_sdio = sdio->sdio_des.hw_sdio;
  262. rt_uint32_t intstatus = hw_sdio->sta;
  263. /* clear irq flag*/
  264. hw_sdio->icr = intstatus;
  265. rt_event_send(&sdio->event, intstatus);
  266. }
  267. /**
  268. * @brief This function config sdio.
  269. * @param host rt_mmcsd_host
  270. * @param io_cfg rt_mmcsd_io_cfg
  271. * @retval None
  272. */
  273. static void rthw_sdio_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  274. {
  275. rt_uint32_t temp, clk_src;
  276. rt_uint32_t clk = io_cfg->clock;
  277. struct rthw_sdio *sdio = host->private_data;
  278. struct stm32_sdio *hw_sdio = sdio->sdio_des.hw_sdio;
  279. LOG_D("clk:%dK width:%s%s%s power:%s%s%s",
  280. clk/1000,
  281. io_cfg->bus_width == MMCSD_BUS_WIDTH_8 ? "8" : "",
  282. io_cfg->bus_width == MMCSD_BUS_WIDTH_4 ? "4" : "",
  283. io_cfg->bus_width == MMCSD_BUS_WIDTH_1 ? "1" : "",
  284. io_cfg->power_mode == MMCSD_POWER_OFF ? "OFF" : "",
  285. io_cfg->power_mode == MMCSD_POWER_UP ? "UP" : "",
  286. io_cfg->power_mode == MMCSD_POWER_ON ? "ON" : ""
  287. );
  288. clk_src = SDIO_CLOCK_FREQ;
  289. if (clk > 0)
  290. {
  291. if (clk > host->freq_max)
  292. clk = host->freq_max;
  293. temp = DIV_ROUND_UP(clk_src, 2 * clk);
  294. if (temp > 0x3FF)
  295. temp = 0x3FF;
  296. }
  297. if (io_cfg->bus_width == MMCSD_BUS_WIDTH_4)
  298. temp |= SDMMC_CLKCR_WIDBUS_0;
  299. else if (io_cfg->bus_width == MMCSD_BUS_WIDTH_8)
  300. temp |= SDMMC_CLKCR_WIDBUS_1;
  301. hw_sdio->clkcr = temp;
  302. if (io_cfg->power_mode == MMCSD_POWER_ON)
  303. hw_sdio->power |= SDMMC_POWER_PWRCTRL;
  304. }
  305. static const struct rt_mmcsd_host_ops ops =
  306. {
  307. rthw_sdio_request,
  308. rthw_sdio_iocfg,
  309. RT_NULL,
  310. RT_NULL,
  311. };
  312. /**
  313. * @brief This function create mmcsd host.
  314. * @param sdio_des stm32_sdio_des
  315. * @retval rt_mmcsd_host
  316. */
  317. struct rt_mmcsd_host *sdio_host_create(struct stm32_sdio_des *sdio_des)
  318. {
  319. struct rt_mmcsd_host *host;
  320. struct rthw_sdio *sdio = RT_NULL;
  321. if (sdio_des == RT_NULL)
  322. {
  323. return RT_NULL;
  324. }
  325. sdio = rt_malloc(sizeof(struct rthw_sdio));
  326. if (sdio == RT_NULL)
  327. {
  328. LOG_E("malloc rthw_sdio fail");
  329. return RT_NULL;
  330. }
  331. rt_memset(sdio, 0, sizeof(struct rthw_sdio));
  332. host = mmcsd_alloc_host();
  333. if (host == RT_NULL)
  334. {
  335. LOG_E("alloc host fail");
  336. goto err;
  337. }
  338. rt_memcpy(&sdio->sdio_des, sdio_des, sizeof(struct stm32_sdio_des));
  339. sdio->sdio_des.hw_sdio = (struct stm32_sdio *)SDIO_BASE_ADDRESS;
  340. rt_event_init(&sdio->event, "sdio", RT_IPC_FLAG_FIFO);
  341. /* set host default attributes */
  342. host->ops = &ops;
  343. host->freq_min = 400 * 1000;
  344. host->freq_max = SDIO_MAX_FREQ;
  345. host->valid_ocr = VDD_32_33 | VDD_33_34;/* The voltage range supported is 3.2v-3.4v */
  346. #ifndef SDIO_USING_1_BIT
  347. host->flags = MMCSD_BUSWIDTH_4 | MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED;
  348. #else
  349. host->flags = MMCSD_MUTBLKWRITE | MMCSD_SUP_HIGHSPEED;
  350. #endif
  351. host->max_seg_size = SDIO_BUFF_SIZE;
  352. host->max_dma_segs = 1;
  353. host->max_blk_size = 512;
  354. host->max_blk_count = 512;
  355. /* link up host and sdio */
  356. sdio->host = host;
  357. host->private_data = sdio;
  358. /* ready to change */
  359. mmcsd_change(host);
  360. return host;
  361. err:
  362. if (sdio) rt_free(sdio);
  363. return RT_NULL;
  364. }
  365. void SDMMC1_IRQHandler(void)
  366. {
  367. /* enter interrupt */
  368. rt_interrupt_enter();
  369. /* Process All SDIO Interrupt Sources */
  370. rthw_sdio_irq_process(host);
  371. /* leave interrupt */
  372. rt_interrupt_leave();
  373. }
  374. int rt_hw_sdio_init(void)
  375. {
  376. struct stm32_sdio_des sdio_des;
  377. SD_HandleTypeDef hsd;
  378. hsd.Instance = SDMMC1;
  379. HAL_SD_MspInit(&hsd);
  380. host = sdio_host_create(&sdio_des);
  381. if (host == RT_NULL)
  382. {
  383. LOG_E("host create fail");
  384. return RT_NULL;
  385. }
  386. return 0;
  387. }
  388. INIT_DEVICE_EXPORT(rt_hw_sdio_init);
  389. int mnt_init(void)
  390. {
  391. rt_thread_delay(RT_TICK_PER_SECOND);
  392. if (dfs_mount("sd0", "/", "elm", 0, 0) != 0)
  393. {
  394. rt_kprintf("file system mount failed!\n");
  395. }
  396. else
  397. {
  398. rt_kprintf("file system mount success!\n");
  399. }
  400. return 0;
  401. }
  402. INIT_ENV_EXPORT(mnt_init);
  403. #endif /* BSP_USING_SDMMC */