mmcsd_core.c 18 KB

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