mmcsd_core.c 18 KB

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