mmcsd_core.c 18 KB

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