mmcsd_core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. * 2011-07-25 weety first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/mmcsd_core.h>
  12. #include <drivers/sd.h>
  13. #include <drivers/mmc.h>
  14. #include <drivers/sdio.h>
  15. #define DBG_TAG "SDIO"
  16. #ifdef RT_SDIO_DEBUG
  17. #define DBG_LVL DBG_LOG
  18. #else
  19. #define DBG_LVL DBG_INFO
  20. #endif /* RT_SDIO_DEBUG */
  21. #include <rtdbg.h>
  22. #ifndef RT_MMCSD_STACK_SIZE
  23. #define RT_MMCSD_STACK_SIZE 1024
  24. #endif
  25. #ifndef RT_MMCSD_THREAD_PREORITY
  26. #if (RT_THREAD_PRIORITY_MAX == 32)
  27. #define RT_MMCSD_THREAD_PREORITY 0x16
  28. #else
  29. #define RT_MMCSD_THREAD_PREORITY 0x40
  30. #endif
  31. #endif
  32. //static struct rt_semaphore mmcsd_sem;
  33. static struct rt_thread mmcsd_detect_thread;
  34. static rt_uint8_t mmcsd_stack[RT_MMCSD_STACK_SIZE];
  35. static struct rt_mailbox mmcsd_detect_mb;
  36. static rt_uint32_t mmcsd_detect_mb_pool[4];
  37. static struct rt_mailbox mmcsd_hotpluge_mb;
  38. static rt_uint32_t mmcsd_hotpluge_mb_pool[4];
  39. void mmcsd_host_lock(struct rt_mmcsd_host *host)
  40. {
  41. rt_mutex_take(&host->bus_lock, RT_WAITING_FOREVER);
  42. }
  43. void mmcsd_host_unlock(struct rt_mmcsd_host *host)
  44. {
  45. rt_mutex_release(&host->bus_lock);
  46. }
  47. void mmcsd_req_complete(struct rt_mmcsd_host *host)
  48. {
  49. rt_sem_release(&host->sem_ack);
  50. }
  51. void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
  52. {
  53. do {
  54. req->cmd->retries--;
  55. req->cmd->err = 0;
  56. req->cmd->mrq = req;
  57. if (req->data)
  58. {
  59. req->cmd->data = req->data;
  60. req->data->err = 0;
  61. req->data->mrq = req;
  62. if (req->stop)
  63. {
  64. req->data->stop = req->stop;
  65. req->stop->err = 0;
  66. req->stop->mrq = req;
  67. }
  68. }
  69. host->ops->request(host, req);
  70. rt_sem_take(&host->sem_ack, RT_WAITING_FOREVER);
  71. } while(req->cmd->err && (req->cmd->retries > 0));
  72. }
  73. rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host,
  74. struct rt_mmcsd_cmd *cmd,
  75. int retries)
  76. {
  77. struct rt_mmcsd_req req;
  78. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  79. rt_memset(cmd->resp, 0, sizeof(cmd->resp));
  80. cmd->retries = retries;
  81. req.cmd = cmd;
  82. cmd->data = RT_NULL;
  83. mmcsd_send_request(host, &req);
  84. return cmd->err;
  85. }
  86. rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host)
  87. {
  88. rt_int32_t err;
  89. struct rt_mmcsd_cmd cmd;
  90. if (!controller_is_spi(host))
  91. {
  92. mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
  93. mmcsd_delay_ms(1);
  94. }
  95. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  96. cmd.cmd_code = GO_IDLE_STATE;
  97. cmd.arg = 0;
  98. cmd.flags = RESP_SPI_R1 | RESP_NONE | CMD_BC;
  99. err = mmcsd_send_cmd(host, &cmd, 0);
  100. mmcsd_delay_ms(1);
  101. if (!controller_is_spi(host))
  102. {
  103. mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
  104. mmcsd_delay_ms(1);
  105. }
  106. return err;
  107. }
  108. rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host,
  109. rt_int32_t high_capacity,
  110. rt_uint32_t *ocr)
  111. {
  112. struct rt_mmcsd_cmd cmd;
  113. rt_int32_t err;
  114. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  115. cmd.cmd_code = SPI_READ_OCR;
  116. cmd.arg = high_capacity ? (1 << 30) : 0;
  117. cmd.flags = RESP_SPI_R3;
  118. err = mmcsd_send_cmd(host, &cmd, 0);
  119. *ocr = cmd.resp[1];
  120. return err;
  121. }
  122. rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  123. {
  124. rt_int32_t err;
  125. struct rt_mmcsd_cmd cmd;
  126. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  127. cmd.cmd_code = ALL_SEND_CID;
  128. cmd.arg = 0;
  129. cmd.flags = RESP_R2 | CMD_BCR;
  130. err = mmcsd_send_cmd(host, &cmd, 3);
  131. if (err)
  132. return err;
  133. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  134. return 0;
  135. }
  136. rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
  137. {
  138. rt_int32_t err, i;
  139. struct rt_mmcsd_req req;
  140. struct rt_mmcsd_cmd cmd;
  141. struct rt_mmcsd_data data;
  142. rt_uint32_t *buf = RT_NULL;
  143. if (!controller_is_spi(host))
  144. {
  145. if (!host->card)
  146. return -RT_ERROR;
  147. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  148. cmd.cmd_code = SEND_CID;
  149. cmd.arg = host->card->rca << 16;
  150. cmd.flags = RESP_R2 | CMD_AC;
  151. err = mmcsd_send_cmd(host, &cmd, 3);
  152. if (err)
  153. return err;
  154. rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
  155. return 0;
  156. }
  157. buf = (rt_uint32_t *)rt_malloc(16);
  158. if (!buf)
  159. {
  160. LOG_E("allocate memory failed!");
  161. return -RT_ENOMEM;
  162. }
  163. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  164. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  165. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  166. req.cmd = &cmd;
  167. req.data = &data;
  168. cmd.cmd_code = SEND_CID;
  169. cmd.arg = 0;
  170. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  171. * rely on callers to never use this with "native" calls for reading
  172. * CSD or CID. Native versions of those commands use the R2 type,
  173. * not R1 plus a data block.
  174. */
  175. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  176. data.blksize = 16;
  177. data.blks = 1;
  178. data.flags = DATA_DIR_READ;
  179. data.buf = buf;
  180. /*
  181. * The spec states that CSR and CID accesses have a timeout
  182. * of 64 clock cycles.
  183. */
  184. data.timeout_ns = 0;
  185. data.timeout_clks = 64;
  186. mmcsd_send_request(host, &req);
  187. if (cmd.err || data.err)
  188. {
  189. rt_free(buf);
  190. return -RT_ERROR;
  191. }
  192. for (i = 0;i < 4;i++)
  193. cid[i] = buf[i];
  194. rt_free(buf);
  195. return 0;
  196. }
  197. rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd)
  198. {
  199. rt_int32_t err, i;
  200. struct rt_mmcsd_req req;
  201. struct rt_mmcsd_cmd cmd;
  202. struct rt_mmcsd_data data;
  203. rt_uint32_t *buf = RT_NULL;
  204. if (!controller_is_spi(card->host))
  205. {
  206. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  207. cmd.cmd_code = SEND_CSD;
  208. cmd.arg = card->rca << 16;
  209. cmd.flags = RESP_R2 | CMD_AC;
  210. err = mmcsd_send_cmd(card->host, &cmd, 3);
  211. if (err)
  212. return err;
  213. rt_memcpy(csd, cmd.resp, sizeof(rt_uint32_t) * 4);
  214. return 0;
  215. }
  216. buf = (rt_uint32_t*)rt_malloc(16);
  217. if (!buf)
  218. {
  219. LOG_E("allocate memory failed!");
  220. return -RT_ENOMEM;
  221. }
  222. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  223. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  224. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  225. req.cmd = &cmd;
  226. req.data = &data;
  227. cmd.cmd_code = SEND_CSD;
  228. cmd.arg = 0;
  229. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  230. * rely on callers to never use this with "native" calls for reading
  231. * CSD or CID. Native versions of those commands use the R2 type,
  232. * not R1 plus a data block.
  233. */
  234. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  235. data.blksize = 16;
  236. data.blks = 1;
  237. data.flags = DATA_DIR_READ;
  238. data.buf = buf;
  239. /*
  240. * The spec states that CSR and CID accesses have a timeout
  241. * of 64 clock cycles.
  242. */
  243. data.timeout_ns = 0;
  244. data.timeout_clks = 64;
  245. mmcsd_send_request(card->host, &req);
  246. if (cmd.err || data.err)
  247. {
  248. rt_free(buf);
  249. return -RT_ERROR;
  250. }
  251. for (i = 0;i < 4;i++)
  252. csd[i] = buf[i];
  253. rt_free(buf);
  254. return 0;
  255. }
  256. static rt_int32_t _mmcsd_select_card(struct rt_mmcsd_host *host,
  257. struct rt_mmcsd_card *card)
  258. {
  259. rt_int32_t err;
  260. struct rt_mmcsd_cmd cmd;
  261. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  262. cmd.cmd_code = SELECT_CARD;
  263. if (card)
  264. {
  265. cmd.arg = card->rca << 16;
  266. cmd.flags = RESP_R1 | CMD_AC;
  267. }
  268. else
  269. {
  270. cmd.arg = 0;
  271. cmd.flags = RESP_NONE | CMD_AC;
  272. }
  273. err = mmcsd_send_cmd(host, &cmd, 3);
  274. if (err)
  275. return err;
  276. return 0;
  277. }
  278. rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card)
  279. {
  280. return _mmcsd_select_card(card->host, card);
  281. }
  282. rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *card)
  283. {
  284. return _mmcsd_select_card(card->host, RT_NULL);
  285. }
  286. rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc)
  287. {
  288. struct rt_mmcsd_cmd cmd;
  289. rt_int32_t err;
  290. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  291. cmd.cmd_code = SPI_CRC_ON_OFF;
  292. cmd.flags = RESP_SPI_R1;
  293. cmd.arg = use_crc;
  294. err = mmcsd_send_cmd(host, &cmd, 0);
  295. if (!err)
  296. host->spi_use_crc = use_crc;
  297. return err;
  298. }
  299. rt_inline void mmcsd_set_iocfg(struct rt_mmcsd_host *host)
  300. {
  301. struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
  302. mmcsd_dbg("clock %uHz busmode %u powermode %u cs %u Vdd %u "
  303. "width %u \n",
  304. io_cfg->clock, io_cfg->bus_mode,
  305. io_cfg->power_mode, io_cfg->chip_select, io_cfg->vdd,
  306. io_cfg->bus_width);
  307. host->ops->set_iocfg(host, io_cfg);
  308. }
  309. /*
  310. * Control chip select pin on a host.
  311. */
  312. void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode)
  313. {
  314. host->io_cfg.chip_select = mode;
  315. mmcsd_set_iocfg(host);
  316. }
  317. /*
  318. * Sets the host clock to the highest possible frequency that
  319. * is below "hz".
  320. */
  321. void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk)
  322. {
  323. if (clk < host->freq_min)
  324. {
  325. LOG_W("clock too low!");
  326. }
  327. host->io_cfg.clock = clk;
  328. mmcsd_set_iocfg(host);
  329. }
  330. /*
  331. * Change the bus mode (open drain/push-pull) of a host.
  332. */
  333. void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode)
  334. {
  335. host->io_cfg.bus_mode = mode;
  336. mmcsd_set_iocfg(host);
  337. }
  338. /*
  339. * Change data bus width of a host.
  340. */
  341. void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width)
  342. {
  343. host->io_cfg.bus_width = width;
  344. mmcsd_set_iocfg(host);
  345. }
  346. void mmcsd_set_data_timeout(struct rt_mmcsd_data *data,
  347. const struct rt_mmcsd_card *card)
  348. {
  349. rt_uint32_t mult;
  350. if (card->card_type == CARD_TYPE_SDIO)
  351. {
  352. data->timeout_ns = 1000000000; /* SDIO card 1s */
  353. data->timeout_clks = 0;
  354. return;
  355. }
  356. /*
  357. * SD cards use a 100 multiplier rather than 10
  358. */
  359. mult = (card->card_type == CARD_TYPE_SD) ? 100 : 10;
  360. /*
  361. * Scale up the multiplier (and therefore the timeout) by
  362. * the r2w factor for writes.
  363. */
  364. if (data->flags & DATA_DIR_WRITE)
  365. mult <<= card->csd.r2w_factor;
  366. data->timeout_ns = card->tacc_ns * mult;
  367. data->timeout_clks = card->tacc_clks * mult;
  368. /*
  369. * SD cards also have an upper limit on the timeout.
  370. */
  371. if (card->card_type == CARD_TYPE_SD)
  372. {
  373. rt_uint32_t timeout_us, limit_us;
  374. timeout_us = data->timeout_ns / 1000;
  375. timeout_us += data->timeout_clks * 1000 /
  376. (card->host->io_cfg.clock / 1000);
  377. if (data->flags & DATA_DIR_WRITE)
  378. /*
  379. * The limit is really 250 ms, but that is
  380. * insufficient for some crappy cards.
  381. */
  382. limit_us = 300000;
  383. else
  384. limit_us = 100000;
  385. /*
  386. * SDHC cards always use these fixed values.
  387. */
  388. if (timeout_us > limit_us || card->flags & CARD_FLAG_SDHC)
  389. {
  390. data->timeout_ns = limit_us * 1000; /* SDHC card fixed 250ms */
  391. data->timeout_clks = 0;
  392. }
  393. }
  394. if (controller_is_spi(card->host))
  395. {
  396. if (data->flags & DATA_DIR_WRITE)
  397. {
  398. if (data->timeout_ns < 1000000000)
  399. data->timeout_ns = 1000000000; /* 1s */
  400. }
  401. else
  402. {
  403. if (data->timeout_ns < 100000000)
  404. data->timeout_ns = 100000000; /* 100ms */
  405. }
  406. }
  407. }
  408. /*
  409. * Mask off any voltages we don't support and select
  410. * the lowest voltage
  411. */
  412. rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  413. {
  414. int bit;
  415. extern int __rt_ffs(int value);
  416. ocr &= host->valid_ocr;
  417. bit = __rt_ffs(ocr);
  418. if (bit)
  419. {
  420. bit -= 1;
  421. ocr &= 3 << bit;
  422. host->io_cfg.vdd = bit;
  423. mmcsd_set_iocfg(host);
  424. }
  425. else
  426. {
  427. LOG_W("host doesn't support card's voltages!");
  428. ocr = 0;
  429. }
  430. return ocr;
  431. }
  432. static void mmcsd_power_up(struct rt_mmcsd_host *host)
  433. {
  434. int bit = __rt_fls(host->valid_ocr) - 1;
  435. host->io_cfg.vdd = bit;
  436. if (controller_is_spi(host))
  437. {
  438. host->io_cfg.chip_select = MMCSD_CS_HIGH;
  439. host->io_cfg.bus_mode = MMCSD_BUSMODE_PUSHPULL;
  440. }
  441. else
  442. {
  443. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  444. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  445. }
  446. host->io_cfg.power_mode = MMCSD_POWER_UP;
  447. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  448. mmcsd_set_iocfg(host);
  449. /*
  450. * This delay should be sufficient to allow the power supply
  451. * to reach the minimum voltage.
  452. */
  453. mmcsd_delay_ms(10);
  454. host->io_cfg.clock = host->freq_min;
  455. host->io_cfg.power_mode = MMCSD_POWER_ON;
  456. mmcsd_set_iocfg(host);
  457. /*
  458. * This delay must be at least 74 clock sizes, or 1 ms, or the
  459. * time required to reach a stable voltage.
  460. */
  461. mmcsd_delay_ms(10);
  462. }
  463. static void mmcsd_power_off(struct rt_mmcsd_host *host)
  464. {
  465. host->io_cfg.clock = 0;
  466. host->io_cfg.vdd = 0;
  467. if (!controller_is_spi(host))
  468. {
  469. host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
  470. host->io_cfg.chip_select = MMCSD_CS_IGNORE;
  471. }
  472. host->io_cfg.power_mode = MMCSD_POWER_OFF;
  473. host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
  474. mmcsd_set_iocfg(host);
  475. }
  476. int mmcsd_wait_cd_changed(rt_int32_t timeout)
  477. {
  478. struct rt_mmcsd_host *host;
  479. if (rt_mb_recv(&mmcsd_hotpluge_mb, (rt_ubase_t *)&host, timeout) == RT_EOK)
  480. {
  481. if(host->card == RT_NULL)
  482. {
  483. return MMCSD_HOST_UNPLUGED;
  484. }
  485. else
  486. {
  487. return MMCSD_HOST_PLUGED;
  488. }
  489. }
  490. return -RT_ETIMEOUT;
  491. }
  492. RTM_EXPORT(mmcsd_wait_cd_changed);
  493. void mmcsd_change(struct rt_mmcsd_host *host)
  494. {
  495. rt_mb_send(&mmcsd_detect_mb, (rt_uint32_t)host);
  496. }
  497. void mmcsd_detect(void *param)
  498. {
  499. struct rt_mmcsd_host *host;
  500. rt_uint32_t ocr;
  501. rt_int32_t err;
  502. while (1)
  503. {
  504. if (rt_mb_recv(&mmcsd_detect_mb, (rt_ubase_t *)&host, RT_WAITING_FOREVER) == RT_EOK)
  505. {
  506. if (host->card == RT_NULL)
  507. {
  508. mmcsd_host_lock(host);
  509. mmcsd_power_up(host);
  510. mmcsd_go_idle(host);
  511. mmcsd_send_if_cond(host, host->valid_ocr);
  512. err = sdio_io_send_op_cond(host, 0, &ocr);
  513. if (!err)
  514. {
  515. if (init_sdio(host, ocr))
  516. mmcsd_power_off(host);
  517. mmcsd_host_unlock(host);
  518. continue;
  519. }
  520. /*
  521. * detect SD card
  522. */
  523. err = mmcsd_send_app_op_cond(host, 0, &ocr);
  524. if (!err)
  525. {
  526. if (init_sd(host, ocr))
  527. mmcsd_power_off(host);
  528. mmcsd_host_unlock(host);
  529. rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
  530. continue;
  531. }
  532. /*
  533. * detect mmc card
  534. */
  535. err = mmc_send_op_cond(host, 0, &ocr);
  536. if (!err)
  537. {
  538. if (init_mmc(host, ocr))
  539. mmcsd_power_off(host);
  540. mmcsd_host_unlock(host);
  541. rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
  542. continue;
  543. }
  544. mmcsd_host_unlock(host);
  545. }
  546. else
  547. {
  548. /* card removed */
  549. mmcsd_host_lock(host);
  550. if (host->card->sdio_function_num != 0)
  551. {
  552. LOG_W("unsupport sdio card plug out!");
  553. }
  554. else
  555. {
  556. rt_mmcsd_blk_remove(host->card);
  557. rt_free(host->card);
  558. host->card = RT_NULL;
  559. }
  560. mmcsd_host_unlock(host);
  561. rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
  562. }
  563. }
  564. }
  565. }
  566. struct rt_mmcsd_host *mmcsd_alloc_host(void)
  567. {
  568. struct rt_mmcsd_host *host;
  569. host = rt_malloc(sizeof(struct rt_mmcsd_host));
  570. if (!host)
  571. {
  572. LOG_E("alloc host failed");
  573. return RT_NULL;
  574. }
  575. rt_memset(host, 0, sizeof(struct rt_mmcsd_host));
  576. host->max_seg_size = 65535;
  577. host->max_dma_segs = 1;
  578. host->max_blk_size = 512;
  579. host->max_blk_count = 4096;
  580. rt_mutex_init(&host->bus_lock, "sd_bus_lock", RT_IPC_FLAG_FIFO);
  581. rt_sem_init(&host->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
  582. return host;
  583. }
  584. void mmcsd_free_host(struct rt_mmcsd_host *host)
  585. {
  586. rt_mutex_detach(&host->bus_lock);
  587. rt_sem_detach(&host->sem_ack);
  588. rt_free(host);
  589. }
  590. int rt_mmcsd_core_init(void)
  591. {
  592. rt_err_t ret;
  593. /* initialize detect SD cart thread */
  594. /* initialize mailbox and create detect SD card thread */
  595. ret = rt_mb_init(&mmcsd_detect_mb, "mmcsdmb",
  596. &mmcsd_detect_mb_pool[0], sizeof(mmcsd_detect_mb_pool) / sizeof(mmcsd_detect_mb_pool[0]),
  597. RT_IPC_FLAG_FIFO);
  598. RT_ASSERT(ret == RT_EOK);
  599. ret = rt_mb_init(&mmcsd_hotpluge_mb, "mmcsdhotplugmb",
  600. &mmcsd_hotpluge_mb_pool[0], sizeof(mmcsd_hotpluge_mb_pool) / sizeof(mmcsd_hotpluge_mb_pool[0]),
  601. RT_IPC_FLAG_FIFO);
  602. RT_ASSERT(ret == RT_EOK);
  603. ret = rt_thread_init(&mmcsd_detect_thread, "mmcsd_detect", mmcsd_detect, RT_NULL,
  604. &mmcsd_stack[0], RT_MMCSD_STACK_SIZE, RT_MMCSD_THREAD_PREORITY, 20);
  605. if (ret == RT_EOK)
  606. {
  607. rt_thread_startup(&mmcsd_detect_thread);
  608. }
  609. rt_sdio_init();
  610. return 0;
  611. }
  612. INIT_PREV_EXPORT(rt_mmcsd_core_init);