drv_sdif.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2023/7/11 liqiaozhong init SD card and mount file system
  11. * 2023/11/8 zhugengyu add interrupt handling for dma waiting, unify function naming
  12. * 2024/4/7 zhugengyu support use two sdif device
  13. */
  14. /***************************** Include Files *********************************/
  15. #include "rtconfig.h"
  16. #ifdef BSP_USING_SDIF
  17. #include <rthw.h>
  18. #include <rtdef.h>
  19. #include <rtthread.h>
  20. #include <rtdevice.h>
  21. #include <rtdbg.h>
  22. #include <drivers/dev_mmcsd_core.h>
  23. #ifdef RT_USING_SMART
  24. #include "ioremap.h"
  25. #endif
  26. #include "mm_aspace.h"
  27. #include "interrupt.h"
  28. #define LOG_TAG "sdif_drv"
  29. #include "drv_log.h"
  30. #include "ftypes.h"
  31. #include "fparameters.h"
  32. #include "fcpu_info.h"
  33. #include "fsdif_timing.h"
  34. #include "fsdif.h"
  35. #include "fsdif_hw.h"
  36. #include "drv_sdif.h"
  37. /************************** Constant Definitions *****************************/
  38. #define SDIF_CARD_TYPE_MICRO_SD 1
  39. #define SDIF_CARD_TYPE_EMMC 2
  40. #define SDIF_CARD_TYPE_SDIO 3
  41. #define SDIF_DMA_BLK_SZ 512U
  42. #define SDIF_MAX_BLK_TRANS 20U
  43. #define SDIF_DMA_ALIGN SDIF_DMA_BLK_SZ
  44. /* preserve pointer to host instance */
  45. static struct rt_mmcsd_host *mmc_host[FSDIF_NUM] = {RT_NULL};
  46. /**************************** Type Definitions *******************************/
  47. typedef struct
  48. {
  49. FSdif sdif;
  50. rt_int32_t sd_type;
  51. FSdifIDmaDesc *rw_desc;
  52. uintptr_t rw_desc_dma;
  53. rt_size_t rw_desc_num;
  54. struct rt_event event;
  55. #define SDIF_EVENT_CARD_DETECTED (1 << 0)
  56. #define SDIF_EVENT_COMMAND_DONE (1 << 1)
  57. #define SDIF_EVENT_DATA_DONE (1 << 2)
  58. #define SDIF_EVENT_ERROR_OCCUR (1 << 3)
  59. #define SDIF_EVENT_SDIO_IRQ (1 << 4)
  60. void *aligned_buffer;
  61. uintptr_t aligned_buffer_dma;
  62. rt_size_t aligned_buffer_size;
  63. FSdifCmdData req_cmd;
  64. FSdifCmdData req_stop;
  65. FSdifData req_data;
  66. } sdif_info_t;
  67. /************************** Variable Definitions *****************************/
  68. /***************** Macros (Inline Functions) Definitions *********************/
  69. /******************************* Functions *********************************/
  70. static void sdif_host_relax(void)
  71. {
  72. rt_thread_mdelay(1);
  73. }
  74. void sdif_change(rt_uint32_t id)
  75. {
  76. RT_ASSERT(id < FSDIF_NUM);
  77. if (mmc_host[id])
  78. {
  79. mmcsd_change(mmc_host[id]);
  80. }
  81. }
  82. rt_int32_t sdif_card_inserted(rt_uint32_t id)
  83. {
  84. RT_ASSERT(id < FSDIF_NUM);
  85. if (mmc_host[id])
  86. {
  87. return mmc_host[id]->ops->get_card_status(mmc_host[id]);
  88. }
  89. return 0;
  90. }
  91. static void sdif_card_detect_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
  92. {
  93. struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
  94. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  95. rt_event_send(&host_info->event, SDIF_EVENT_CARD_DETECTED);
  96. sdif_change(host_info->sdif.config.instance_id);
  97. }
  98. static void sdif_command_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
  99. {
  100. struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
  101. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  102. rt_event_send(&host_info->event, SDIF_EVENT_COMMAND_DONE);
  103. }
  104. static void sdif_data_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
  105. {
  106. struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
  107. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  108. rt_event_send(&host_info->event, SDIF_EVENT_DATA_DONE);
  109. }
  110. static void sdif_sdio_irq_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
  111. {
  112. struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
  113. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  114. rt_event_send(&host_info->event, SDIF_EVENT_SDIO_IRQ);
  115. }
  116. static void sdif_error_occur_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
  117. {
  118. struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
  119. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  120. LOG_E("Error occur !!!");
  121. LOG_E("Status: 0x%x, dmac status: 0x%x.", status, dmac_status);
  122. if (status & FSDIF_INT_RE_BIT)
  123. LOG_E("Response err. 0x%x", FSDIF_INT_RE_BIT);
  124. if (status & FSDIF_INT_RTO_BIT)
  125. LOG_E("Response timeout. 0x%x", FSDIF_INT_RTO_BIT);
  126. if (dmac_status & FSDIF_DMAC_STATUS_DU)
  127. LOG_E("Descriptor un-readable. 0x%x", FSDIF_DMAC_STATUS_DU);
  128. if (status & FSDIF_INT_DCRC_BIT)
  129. LOG_E("Data CRC error. 0x%x", FSDIF_INT_DCRC_BIT);
  130. if (status & FSDIF_INT_RCRC_BIT)
  131. LOG_E("Data CRC error. 0x%x", FSDIF_INT_RCRC_BIT);
  132. rt_event_send(&host_info->event, SDIF_EVENT_ERROR_OCCUR);
  133. }
  134. static rt_err_t sdif_pre_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  135. {
  136. rt_err_t err = RT_EOK;
  137. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  138. if (host_info->sd_type != SDIF_CARD_TYPE_SDIO)
  139. {
  140. /* ignore SDIO detect command */
  141. if ((req->cmd->cmd_code == SD_IO_SEND_OP_COND) ||
  142. (req->cmd->cmd_code == SD_IO_RW_DIRECT))
  143. {
  144. req->cmd->err = -1;
  145. mmcsd_req_complete(host);
  146. err = RT_EEMPTY;
  147. }
  148. }
  149. if (host_info->sd_type == SDIF_CARD_TYPE_EMMC)
  150. {
  151. /* ignore micro SD detect command, not in eMMC spec. */
  152. if ((req->cmd->cmd_code == SD_APP_OP_COND) ||
  153. (req->cmd->cmd_code == APP_CMD))
  154. {
  155. req->cmd->err = -1;
  156. mmcsd_req_complete(host);
  157. err = RT_EEMPTY;
  158. }
  159. /* ignore mmcsd_send_if_cond(CMD-8) which will failed for eMMC
  160. but check cmd arg to let SEND_EXT_CSD (CMD-8) run */
  161. if ((req->cmd->cmd_code == SD_SEND_IF_COND) &&
  162. (req->cmd->arg == 0x1AA)) /* 0x1AA is the send_if_cond pattern, use it by care */
  163. {
  164. req->cmd->err = -1;
  165. mmcsd_req_complete(host);
  166. err = RT_EEMPTY;
  167. }
  168. }
  169. if ((req->cmd->cmd_code == READ_MULTIPLE_BLOCK) ||
  170. (req->cmd->cmd_code == WRITE_MULTIPLE_BLOCK)) /* set block count */
  171. {
  172. struct rt_mmcsd_req sbc;
  173. struct rt_mmcsd_cmd sbc_cmd;
  174. rt_memset(&sbc, 0, sizeof(sbc));
  175. rt_memset(&sbc_cmd, 0, sizeof(sbc_cmd));
  176. sbc_cmd.cmd_code = SET_BLOCK_COUNT;
  177. RT_ASSERT(req->data);
  178. sbc_cmd.arg = req->data->blks;
  179. sbc_cmd.flags = RESP_R1;
  180. LOG_I("set block_count = %d", req->data->blks);
  181. sbc.data = RT_NULL;
  182. sbc.cmd = &sbc_cmd;
  183. sbc.stop = RT_NULL;
  184. sbc.sbc = RT_NULL;
  185. mmcsd_send_request(host, &sbc);
  186. err = sbc_cmd.err;
  187. if (req->cmd->busy_timeout < 1000) /* in case rt-thread do not give wait timeout */
  188. {
  189. req->cmd->busy_timeout = 5000;
  190. }
  191. }
  192. return err;
  193. }
  194. static void sdif_convert_command_info(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *in_cmd, struct rt_mmcsd_data *in_data, FSdifCmdData *out_req)
  195. {
  196. FSdifCmdData *out_cmd = out_req;
  197. FSdifData *out_data = out_req->data_p;
  198. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  199. out_cmd->flag = 0U;
  200. if (in_cmd->cmd_code == GO_IDLE_STATE)
  201. {
  202. out_cmd->flag |= FSDIF_CMD_FLAG_NEED_INIT;
  203. }
  204. if (in_cmd->cmd_code == GO_INACTIVE_STATE)
  205. {
  206. out_cmd->flag |= FSDIF_CMD_FLAG_NEED_AUTO_STOP | FSDIF_CMD_FLAG_ABORT;
  207. }
  208. if (resp_type(in_cmd) != RESP_NONE)
  209. {
  210. out_cmd->flag |= FSDIF_CMD_FLAG_EXP_RESP;
  211. if (resp_type(in_cmd) == RESP_R2)
  212. {
  213. /* need 136 bits long response */
  214. out_cmd->flag |= FSDIF_CMD_FLAG_EXP_LONG_RESP;
  215. }
  216. if ((resp_type(in_cmd) != RESP_R3) &&
  217. (resp_type(in_cmd) != RESP_R4))
  218. {
  219. /* most cmds need CRC */
  220. out_cmd->flag |= FSDIF_CMD_FLAG_NEED_RESP_CRC;
  221. }
  222. }
  223. if (in_data)
  224. {
  225. RT_ASSERT(out_data);
  226. out_cmd->flag |= FSDIF_CMD_FLAG_EXP_DATA;
  227. if (in_data->flags & DATA_DIR_READ)
  228. {
  229. out_cmd->flag |= FSDIF_CMD_FLAG_READ_DATA;
  230. out_data->buf = (void *)in_data->buf;
  231. out_data->buf_dma = (uintptr_t)in_data->buf + PV_OFFSET;
  232. }
  233. else if (in_data->flags & DATA_DIR_WRITE)
  234. {
  235. out_cmd->flag |= FSDIF_CMD_FLAG_WRITE_DATA;
  236. out_data->buf = (void *)in_data->buf;
  237. out_data->buf_dma = (uintptr_t)in_data->buf + PV_OFFSET;
  238. }
  239. else
  240. {
  241. RT_ASSERT(0);
  242. }
  243. out_data->blksz = in_data->blksize;
  244. out_data->blkcnt = in_data->blks;
  245. out_data->datalen = in_data->blksize * in_data->blks;
  246. /* handle unaligned input buffer */
  247. if (out_data->buf_dma % SDIF_DMA_ALIGN)
  248. {
  249. RT_ASSERT(out_data->datalen <= host_info->aligned_buffer_size);
  250. out_data->buf = host_info->aligned_buffer;
  251. out_data->buf_dma = (uintptr_t)host_info->aligned_buffer + PV_OFFSET;
  252. if (in_data->flags & DATA_DIR_WRITE)
  253. {
  254. /* copy the data need to write to sd card */
  255. memcpy(out_data->buf, in_data->buf, out_data->datalen);
  256. }
  257. }
  258. LOG_D("buf@%p, blksz: %d, datalen: %ld",
  259. out_data->buf,
  260. out_data->blksz,
  261. out_data->datalen);
  262. }
  263. out_cmd->cmdidx = in_cmd->cmd_code;
  264. out_cmd->cmdarg = in_cmd->arg;
  265. LOG_D("cmdarg: 0x%x", out_cmd->cmdarg);
  266. }
  267. static rt_err_t sdif_do_transfer(struct rt_mmcsd_host *host, FSdifCmdData *req_cmd, rt_int32_t timeout_ms)
  268. {
  269. FError ret = FT_SUCCESS;
  270. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  271. rt_uint32_t event = 0U;
  272. rt_uint32_t wait_event = 0U;
  273. LOG_I("cmd-%d sending", req_cmd->cmdidx);
  274. if (req_cmd->data_p == RT_NULL)
  275. {
  276. wait_event = SDIF_EVENT_COMMAND_DONE;
  277. }
  278. else
  279. {
  280. wait_event = SDIF_EVENT_COMMAND_DONE | SDIF_EVENT_DATA_DONE;
  281. }
  282. ret = FSdifDMATransfer(&host_info->sdif, req_cmd);
  283. if (ret != FT_SUCCESS)
  284. {
  285. LOG_E("FSdifDMATransfer() fail.");
  286. return -RT_ERROR;
  287. }
  288. while (TRUE)
  289. {
  290. /*
  291. * transfer without data: wait COMMAND_DONE event
  292. * transfer with data: wait COMMAND_DONE and DATA_DONE event
  293. */
  294. if (rt_event_recv(&host_info->event,
  295. (wait_event),
  296. (RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR),
  297. rt_tick_from_millisecond(1000),
  298. &event) == RT_EOK)
  299. {
  300. (void)FSdifGetCmdResponse(&host_info->sdif, req_cmd);
  301. break;
  302. }
  303. /*
  304. * transfer with error: check if ERROR_OCCUR event exists, no wait
  305. */
  306. if (rt_event_recv(&host_info->event,
  307. (SDIF_EVENT_ERROR_OCCUR),
  308. (RT_EVENT_FLAG_AND | RT_WAITING_NO),
  309. 0,
  310. &event) == RT_EOK)
  311. {
  312. LOG_E("Sdif DMA transfer endup with error !!!");
  313. return -RT_EIO;
  314. }
  315. timeout_ms -= 1000;
  316. if (timeout_ms <= 0)
  317. {
  318. LOG_E("Sdif DMA transfer endup with timeout !!!");
  319. return -RT_EIO;
  320. }
  321. }
  322. return RT_EOK;
  323. }
  324. static void sdif_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  325. {
  326. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  327. FSdifCmdData *req_cmd = &host_info->req_cmd;
  328. FSdifData *req_data = &host_info->req_data;
  329. rt_err_t err = sdif_pre_request(host, req);
  330. if (err != RT_EOK)
  331. {
  332. return;
  333. }
  334. rt_memset(req_cmd, 0, sizeof(FSdifCmdData));
  335. if (req->data)
  336. {
  337. rt_memset(req_data, 0, sizeof(FSdifData));
  338. req_cmd->data_p = req_data;
  339. }
  340. else
  341. {
  342. req_cmd->data_p = RT_NULL;
  343. }
  344. sdif_convert_command_info(host, req->cmd, req->data, req_cmd);
  345. req->cmd->err = sdif_do_transfer(host, req_cmd, req->cmd->busy_timeout);
  346. if (resp_type(req->cmd) & RESP_MASK)
  347. {
  348. if (resp_type(req->cmd) == RESP_R2)
  349. {
  350. req->cmd->resp[3] = req_cmd->response[0];
  351. req->cmd->resp[2] = req_cmd->response[1];
  352. req->cmd->resp[1] = req_cmd->response[2];
  353. req->cmd->resp[0] = req_cmd->response[3];
  354. }
  355. else
  356. {
  357. req->cmd->resp[0] = req_cmd->response[0];
  358. }
  359. }
  360. if (req->data && (req->data->flags & DATA_DIR_READ))
  361. {
  362. /* if it is read sd card, copy data to unaligned buffer and return */
  363. if ((uintptr)req->data->buf % SDIF_DMA_ALIGN)
  364. {
  365. rt_memcpy((void *)req->data->buf,
  366. (void *)host_info->aligned_buffer,
  367. req_cmd->data_p->datalen);
  368. }
  369. }
  370. mmcsd_req_complete(host);
  371. }
  372. static void sdif_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
  373. {
  374. FError ret = FT_SUCCESS;
  375. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  376. FSdif *sdif = &host_info->sdif;
  377. uintptr base_addr = sdif->config.base_addr;
  378. if (0 != io_cfg->clock)
  379. {
  380. ret = FSdifSetClkFreq(sdif, io_cfg->clock);
  381. if (ret != FT_SUCCESS)
  382. {
  383. LOG_E("FSdifSetClkFreq fail.");
  384. }
  385. }
  386. switch (io_cfg->bus_width)
  387. {
  388. case MMCSD_BUS_WIDTH_1:
  389. FSdifSetBusWidth(base_addr, 1U);
  390. break;
  391. case MMCSD_BUS_WIDTH_4:
  392. FSdifSetBusWidth(base_addr, 4U);
  393. break;
  394. case MMCSD_BUS_WIDTH_8:
  395. FSdifSetBusWidth(base_addr, 8U);
  396. break;
  397. default:
  398. LOG_E("Invalid bus width %d", io_cfg->bus_width);
  399. break;
  400. }
  401. }
  402. static rt_int32_t sdif_card_status(struct rt_mmcsd_host *host)
  403. {
  404. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  405. FSdif *sdif = &host_info->sdif;
  406. uintptr base_addr = sdif->config.base_addr;
  407. return FSdifCheckIfCardExists(base_addr) ? 1 : 0;
  408. }
  409. static const struct rt_mmcsd_host_ops ops =
  410. {
  411. .request = sdif_send_request,
  412. .set_iocfg = sdif_set_iocfg,
  413. .get_card_status = sdif_card_status,
  414. .enable_sdio_irq = RT_NULL,
  415. .execute_tuning = RT_NULL,
  416. };
  417. static void sdif_ctrl_setup_interrupt(struct rt_mmcsd_host *host)
  418. {
  419. sdif_info_t *host_info = (sdif_info_t *)host->private_data;
  420. FSdif *sdif = &(host_info->sdif);
  421. FSdifConfig *config_p = &sdif->config;
  422. rt_uint32_t cpu_id = rt_hw_cpu_id();
  423. rt_hw_interrupt_set_target_cpus(config_p->irq_num, cpu_id);
  424. rt_hw_interrupt_set_priority(config_p->irq_num, 0xd0);
  425. /* register intr callback */
  426. rt_hw_interrupt_install(config_p->irq_num,
  427. FSdifInterruptHandler,
  428. sdif,
  429. NULL);
  430. /* enable irq */
  431. rt_hw_interrupt_umask(config_p->irq_num);
  432. FSdifRegisterEvtHandler(sdif, FSDIF_EVT_CARD_DETECTED, sdif_card_detect_callback, host);
  433. FSdifRegisterEvtHandler(sdif, FSDIF_EVT_ERR_OCCURE, sdif_error_occur_callback, host);
  434. FSdifRegisterEvtHandler(sdif, FSDIF_EVT_CMD_DONE, sdif_command_done_callback, host);
  435. FSdifRegisterEvtHandler(sdif, FSDIF_EVT_DATA_DONE, sdif_data_done_callback, host);
  436. FSdifRegisterEvtHandler(sdif, FSDIF_EVT_SDIO_IRQ, sdif_sdio_irq_callback, host);
  437. return;
  438. }
  439. static rt_err_t sdif_host_init(rt_uint32_t id, rt_uint32_t type)
  440. {
  441. struct rt_mmcsd_host *host = RT_NULL;
  442. sdif_info_t *host_info = RT_NULL;
  443. const FSdifConfig *default_sdif_config = RT_NULL;
  444. FSdifConfig sdif_config;
  445. rt_err_t result = RT_EOK;
  446. host = mmcsd_alloc_host();
  447. if (!host)
  448. {
  449. LOG_E("Alloc host failed");
  450. result = RT_ENOMEM;
  451. goto err_free;
  452. }
  453. host_info = rt_malloc(sizeof(sdif_info_t));
  454. if (!host_info)
  455. {
  456. LOG_E("Malloc host_info failed");
  457. result = RT_ENOMEM;
  458. goto err_free;
  459. }
  460. rt_memset(host_info, 0, sizeof(*host_info));
  461. result = rt_event_init(&host_info->event, "sdif_event", RT_IPC_FLAG_FIFO);
  462. RT_ASSERT(RT_EOK == result);
  463. host_info->aligned_buffer_size = SDIF_DMA_BLK_SZ * SDIF_MAX_BLK_TRANS;
  464. host_info->aligned_buffer = rt_malloc_align(host_info->aligned_buffer_size,
  465. SDIF_DMA_ALIGN);
  466. if (!host_info->aligned_buffer)
  467. {
  468. LOG_E("Malloc aligned buffer failed");
  469. result = RT_ENOMEM;
  470. goto err_free;
  471. }
  472. host_info->aligned_buffer_dma = (uintptr_t)host_info->aligned_buffer + PV_OFFSET;
  473. rt_memset(host_info->aligned_buffer, 0, host_info->aligned_buffer_size);
  474. host_info->rw_desc_num = (SDIF_DMA_BLK_SZ * SDIF_MAX_BLK_TRANS) / FSDIF_IDMAC_MAX_BUF_SIZE + 1;
  475. host_info->rw_desc = rt_malloc_align(host_info->rw_desc_num * sizeof(FSdifIDmaDesc),
  476. SDIF_DMA_ALIGN);
  477. if (!host_info->rw_desc)
  478. {
  479. LOG_E("Malloc rw_desc failed");
  480. result = RT_ENOMEM;
  481. goto err_free;
  482. }
  483. host_info->rw_desc_dma = (uintptr_t)host_info->rw_desc + PV_OFFSET;
  484. rt_memset(host_info->rw_desc, 0, host_info->rw_desc_num * sizeof(FSdifIDmaDesc));
  485. /* host data init */
  486. host->ops = &ops;
  487. host->freq_min = FSDIF_CLK_SPEED_400KHZ;
  488. if (type == SDIF_CARD_TYPE_MICRO_SD)
  489. {
  490. host->freq_max = FSDIF_CLK_SPEED_50_MHZ;
  491. }
  492. else
  493. {
  494. host->freq_max = FSDIF_CLK_SPEED_52_MHZ;
  495. }
  496. host->valid_ocr = VDD_32_33 | VDD_33_34; /* voltage 3.3v */
  497. host->flags = MMCSD_MUTBLKWRITE | MMCSD_BUSWIDTH_4;
  498. host->max_seg_size = SDIF_DMA_BLK_SZ; /* used in block_dev.c */
  499. host->max_dma_segs = SDIF_MAX_BLK_TRANS; /* physical segment number */
  500. host->max_blk_size = SDIF_DMA_BLK_SZ; /* all the 4 para limits size of one blk tran */
  501. host->max_blk_count = SDIF_MAX_BLK_TRANS;
  502. host->private_data = host_info;
  503. host->name[0] = 's';
  504. host->name[1] = 'd';
  505. host->name[2] = '0' + id;
  506. host->name[3] = '\0';
  507. mmc_host[id] = host;
  508. RT_ASSERT((default_sdif_config = FSdifLookupConfig(id)) != RT_NULL);
  509. sdif_config = *default_sdif_config;
  510. #ifdef RT_USING_SMART
  511. sdif_config.base_addr = (uintptr)rt_ioremap((void *)sdif_config.base_addr, 0x1000);
  512. #endif
  513. sdif_config.trans_mode = FSDIF_IDMA_TRANS_MODE;
  514. if (type == SDIF_CARD_TYPE_MICRO_SD)
  515. {
  516. sdif_config.non_removable = FALSE; /* TF card is removable on board */
  517. }
  518. else if (type == SDIF_CARD_TYPE_EMMC)
  519. {
  520. sdif_config.non_removable = TRUE; /* eMMC is unremovable on board */
  521. }
  522. sdif_config.get_tuning = FSdifGetTimingSetting;
  523. if (FSDIF_SUCCESS != FSdifCfgInitialize(&host_info->sdif, &sdif_config))
  524. {
  525. LOG_E("SDIF controller init failed.");
  526. result = RT_EIO;
  527. goto err_free;
  528. }
  529. if (FSDIF_SUCCESS != FSdifSetIDMAList(&host_info->sdif,
  530. host_info->rw_desc,
  531. host_info->rw_desc_dma,
  532. host_info->rw_desc_num))
  533. {
  534. LOG_E("SDIF controller setup DMA failed.");
  535. result = RT_EIO;
  536. goto err_free;
  537. }
  538. FSdifRegisterRelaxHandler(&host_info->sdif, sdif_host_relax); /* SDIF delay for a while */
  539. host_info->sd_type = type;
  540. LOG_I("Init sdif-%d as %d", id, type);
  541. /* setup interrupt */
  542. sdif_ctrl_setup_interrupt(host);
  543. return result;
  544. err_free:
  545. if (host)
  546. {
  547. mmcsd_free_host(host);
  548. }
  549. if (host_info)
  550. {
  551. if (host_info->aligned_buffer)
  552. {
  553. rt_free(host_info->aligned_buffer);
  554. host_info->aligned_buffer = RT_NULL;
  555. host_info->aligned_buffer_size = 0U;
  556. }
  557. if (host_info->rw_desc)
  558. {
  559. rt_free(host_info->rw_desc);
  560. host_info->rw_desc = RT_NULL;
  561. host_info->rw_desc_num = 0;
  562. }
  563. rt_free(host_info);
  564. }
  565. return result;
  566. }
  567. int rt_hw_sdif_init(void)
  568. {
  569. int status = RT_EOK;
  570. rt_uint32_t sd_type;
  571. FSdifTimingInit();
  572. #ifdef USING_SDIF0
  573. #if defined(USE_SDIF0_TF)
  574. sd_type = SDIF_CARD_TYPE_MICRO_SD;
  575. #elif defined(USE_SDIF0_EMMC)
  576. sd_type = SDIF_CARD_TYPE_EMMC;
  577. #endif
  578. status = sdif_host_init(FSDIF0_ID, sd_type);
  579. if (status != RT_EOK)
  580. {
  581. LOG_E("SDIF0 init failed, status = %d", status);
  582. return status;
  583. }
  584. #endif
  585. #ifdef USING_SDIF1
  586. #if defined(USE_SDIF1_TF)
  587. sd_type = SDIF_CARD_TYPE_MICRO_SD;
  588. #elif defined(USE_SDIF1_EMMC)
  589. sd_type = SDIF_CARD_TYPE_EMMC;
  590. #endif
  591. status = sdif_host_init(FSDIF1_ID, sd_type);
  592. if (status != RT_EOK)
  593. {
  594. LOG_E("SDIF0 init failed, status = %d", status);
  595. return status;
  596. }
  597. #endif
  598. return status;
  599. }
  600. INIT_DEVICE_EXPORT(rt_hw_sdif_init);
  601. #endif // #ifdef BSP_USING_SDIF