mmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * File : mmc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-06-15 hichard first version
  23. */
  24. #include <drivers/mmcsd_core.h>
  25. #include <drivers/mmc.h>
  26. #define DBG_ENABLE
  27. #define DBG_SECTION_NAME "[SDIO]"
  28. #ifdef RT_SDIO_DEBUG
  29. #define DBG_LEVEL DBG_LOG
  30. #else
  31. #define DBG_LEVEL DBG_INFO
  32. #endif /* RT_SDIO_DEBUG */
  33. #define DBG_COLOR
  34. #include <rtdbg.h>
  35. static const rt_uint32_t tran_unit[] =
  36. {
  37. 10000, 100000, 1000000, 10000000,
  38. 0, 0, 0, 0
  39. };
  40. static const rt_uint8_t tran_value[] =
  41. {
  42. 0, 10, 12, 13, 15, 20, 25, 30,
  43. 35, 40, 45, 50, 55, 60, 70, 80,
  44. };
  45. static const rt_uint32_t tacc_uint[] =
  46. {
  47. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  48. };
  49. static const rt_uint8_t tacc_value[] =
  50. {
  51. 0, 10, 12, 13, 15, 20, 25, 30,
  52. 35, 40, 45, 50, 55, 60, 70, 80,
  53. };
  54. rt_inline rt_uint32_t GET_BITS(rt_uint32_t *resp,
  55. rt_uint32_t start,
  56. rt_uint32_t size)
  57. {
  58. const rt_int32_t __size = size;
  59. const rt_uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
  60. const rt_int32_t __off = 3 - ((start) / 32);
  61. const rt_int32_t __shft = (start) & 31;
  62. rt_uint32_t __res;
  63. __res = resp[__off] >> __shft;
  64. if (__size + __shft > 32)
  65. __res |= resp[__off-1] << ((32 - __shft) % 32);
  66. return __res & __mask;
  67. }
  68. /*
  69. * Given a 128-bit response, decode to our card CSD structure.
  70. */
  71. static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card)
  72. {
  73. rt_uint32_t a, b;
  74. struct rt_mmcsd_csd *csd = &card->csd;
  75. rt_uint32_t *resp = card->resp_csd;
  76. /*
  77. * We only understand CSD structure v1.1 and v1.2.
  78. * v1.2 has extra information in bits 15, 11 and 10.
  79. * We also support eMMC v4.4 & v4.41.
  80. */
  81. csd->csd_structure = GET_BITS(resp, 126, 2);
  82. if (csd->csd_structure == 0) {
  83. LOG_E("unrecognised CSD structure version %d!", csd->csd_structure);
  84. return -RT_ERROR;
  85. }
  86. csd->taac = GET_BITS(resp, 112, 8);
  87. csd->nsac = GET_BITS(resp, 104, 8);
  88. csd->tran_speed = GET_BITS(resp, 96, 8);
  89. csd->card_cmd_class = GET_BITS(resp, 84, 12);
  90. csd->rd_blk_len = GET_BITS(resp, 80, 4);
  91. csd->rd_blk_part = GET_BITS(resp, 79, 1);
  92. csd->wr_blk_misalign = GET_BITS(resp, 78, 1);
  93. csd->rd_blk_misalign = GET_BITS(resp, 77, 1);
  94. csd->dsr_imp = GET_BITS(resp, 76, 1);
  95. csd->c_size = GET_BITS(resp, 62, 12);
  96. csd->c_size_mult = GET_BITS(resp, 47, 3);
  97. csd->r2w_factor = GET_BITS(resp, 26, 3);
  98. csd->wr_blk_len = GET_BITS(resp, 22, 4);
  99. csd->wr_blk_partial = GET_BITS(resp, 21, 1);
  100. csd->csd_crc = GET_BITS(resp, 1, 7);
  101. card->card_blksize = 1 << csd->rd_blk_len;
  102. card->tacc_clks = csd->nsac * 100;
  103. card->tacc_ns = (tacc_uint[csd->taac&0x07] * tacc_value[(csd->taac&0x78)>>3] + 9) / 10;
  104. card->max_data_rate = tran_unit[csd->tran_speed&0x07] * tran_value[(csd->tran_speed&0x78)>>3];
  105. if (csd->wr_blk_len >= 9) {
  106. a = GET_BITS(resp, 42, 5);
  107. b = GET_BITS(resp, 37, 5);
  108. card->erase_size = (a + 1) * (b + 1);
  109. card->erase_size <<= csd->wr_blk_len - 9;
  110. }
  111. return 0;
  112. }
  113. /*
  114. * Read extended CSD.
  115. */
  116. static int mmc_get_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t **new_ext_csd)
  117. {
  118. void *ext_csd;
  119. struct rt_mmcsd_req req;
  120. struct rt_mmcsd_cmd cmd;
  121. struct rt_mmcsd_data data;
  122. *new_ext_csd = RT_NULL;
  123. if (GET_BITS(card->resp_cid, 122, 4) < 4)
  124. return 0;
  125. /*
  126. * As the ext_csd is so large and mostly unused, we don't store the
  127. * raw block in mmc_card.
  128. */
  129. ext_csd = rt_malloc(512);
  130. if (!ext_csd) {
  131. LOG_E("alloc memory failed when get ext csd!");
  132. return -RT_ENOMEM;
  133. }
  134. rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
  135. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  136. rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
  137. req.cmd = &cmd;
  138. req.data = &data;
  139. cmd.cmd_code = SEND_EXT_CSD;
  140. cmd.arg = 0;
  141. /* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
  142. * rely on callers to never use this with "native" calls for reading
  143. * CSD or CID. Native versions of those commands use the R2 type,
  144. * not R1 plus a data block.
  145. */
  146. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
  147. data.blksize = 512;
  148. data.blks = 1;
  149. data.flags = DATA_DIR_READ;
  150. data.buf = ext_csd;
  151. /*
  152. * Some cards require longer data read timeout than indicated in CSD.
  153. * Address this by setting the read timeout to a "reasonably high"
  154. * value. For the cards tested, 300ms has proven enough. If necessary,
  155. * this value can be increased if other problematic cards require this.
  156. */
  157. data.timeout_ns = 300000000;
  158. data.timeout_clks = 0;
  159. mmcsd_send_request(card->host, &req);
  160. if (cmd.err)
  161. return cmd.err;
  162. if (data.err)
  163. return data.err;
  164. *new_ext_csd = ext_csd;
  165. return 0;
  166. }
  167. /*
  168. * Decode extended CSD.
  169. */
  170. static int mmc_parse_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
  171. {
  172. if(card == RT_NULL || ext_csd == RT_NULL)
  173. {
  174. LOG_E("emmc parse ext csd fail, invaild args");
  175. return -1;
  176. }
  177. card->flags |= CARD_FLAG_HIGHSPEED;
  178. card->hs_max_data_rate = 200000000;
  179. card->card_capacity = *((rt_uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
  180. card->card_capacity *= card->card_blksize;
  181. card->card_capacity >>= 10; /* unit:KB */
  182. LOG_I("emmc card capacity %d KB.", card->card_capacity);
  183. return 0;
  184. }
  185. /**
  186. * mmc_switch - modify EXT_CSD register
  187. * @card: the MMC card associated with the data transfer
  188. * @set: cmd set values
  189. * @index: EXT_CSD register index
  190. * @value: value to program into EXT_CSD register
  191. *
  192. * Modifies the EXT_CSD register for selected card.
  193. */
  194. static int mmc_switch(struct rt_mmcsd_card *card, rt_uint8_t set,
  195. rt_uint8_t index, rt_uint8_t value)
  196. {
  197. int err;
  198. struct rt_mmcsd_host *host = card->host;
  199. struct rt_mmcsd_cmd cmd = {0};
  200. cmd.cmd_code = SWITCH;
  201. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  202. (index << 16) | (value << 8) | set;
  203. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
  204. err = mmcsd_send_cmd(host, &cmd, 3);
  205. if (err)
  206. return err;
  207. return 0;
  208. }
  209. static int mmc_compare_ext_csds(struct rt_mmcsd_card *card,
  210. rt_uint8_t *ext_csd, rt_uint32_t bus_width)
  211. {
  212. rt_uint8_t *bw_ext_csd;
  213. int err;
  214. if (bus_width == MMCSD_BUS_WIDTH_1)
  215. return 0;
  216. err = mmc_get_ext_csd(card, &bw_ext_csd);
  217. if (err || bw_ext_csd == RT_NULL) {
  218. err = -RT_ERROR;
  219. goto out;
  220. }
  221. /* only compare read only fields */
  222. err = !((ext_csd[EXT_CSD_PARTITION_SUPPORT] == bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  223. (ext_csd[EXT_CSD_ERASED_MEM_CONT] == bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  224. (ext_csd[EXT_CSD_REV] == bw_ext_csd[EXT_CSD_REV]) &&
  225. (ext_csd[EXT_CSD_STRUCTURE] == bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  226. (ext_csd[EXT_CSD_CARD_TYPE] == bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  227. (ext_csd[EXT_CSD_S_A_TIMEOUT] == bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  228. (ext_csd[EXT_CSD_HC_WP_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  229. (ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT] == bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  230. (ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  231. (ext_csd[EXT_CSD_SEC_TRIM_MULT] == bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  232. (ext_csd[EXT_CSD_SEC_ERASE_MULT] == bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  233. (ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] == bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  234. (ext_csd[EXT_CSD_TRIM_MULT] == bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  235. (ext_csd[EXT_CSD_SEC_CNT + 0] == bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  236. (ext_csd[EXT_CSD_SEC_CNT + 1] == bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  237. (ext_csd[EXT_CSD_SEC_CNT + 2] == bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  238. (ext_csd[EXT_CSD_SEC_CNT + 3] == bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
  239. (ext_csd[EXT_CSD_PWR_CL_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
  240. (ext_csd[EXT_CSD_PWR_CL_26_195] == bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
  241. (ext_csd[EXT_CSD_PWR_CL_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
  242. (ext_csd[EXT_CSD_PWR_CL_26_360] == bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
  243. (ext_csd[EXT_CSD_PWR_CL_200_195] == bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
  244. (ext_csd[EXT_CSD_PWR_CL_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
  245. (ext_csd[EXT_CSD_PWR_CL_DDR_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
  246. (ext_csd[EXT_CSD_PWR_CL_DDR_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
  247. (ext_csd[EXT_CSD_PWR_CL_DDR_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
  248. if (err)
  249. err = -RT_ERROR;
  250. out:
  251. rt_free(bw_ext_csd);
  252. return err;
  253. }
  254. /*
  255. * Select the bus width amoung 4-bit and 8-bit(SDR).
  256. * If the bus width is changed successfully, return the selected width value.
  257. * Zero is returned instead of error value if the wide width is not supported.
  258. */
  259. static int mmc_select_bus_width(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
  260. {
  261. rt_uint32_t ext_csd_bits[] = {
  262. EXT_CSD_BUS_WIDTH_8,
  263. EXT_CSD_BUS_WIDTH_4,
  264. EXT_CSD_BUS_WIDTH_1
  265. };
  266. rt_uint32_t bus_widths[] = {
  267. MMCSD_BUS_WIDTH_8,
  268. MMCSD_BUS_WIDTH_4,
  269. MMCSD_BUS_WIDTH_1
  270. };
  271. struct rt_mmcsd_host *host = card->host;
  272. unsigned idx, bus_width = 0;
  273. int err = 0;
  274. if (GET_BITS(card->resp_cid, 122, 4) < 4)
  275. return 0;
  276. /*
  277. * Unlike SD, MMC cards dont have a configuration register to notify
  278. * supported bus width. So bus test command should be run to identify
  279. * the supported bus width or compare the ext csd values of current
  280. * bus width and ext csd values of 1 bit mode read earlier.
  281. */
  282. for (idx = 0; idx < sizeof(bus_widths)/sizeof(rt_uint32_t); idx++) {
  283. /*
  284. * Host is capable of 8bit transfer, then switch
  285. * the device to work in 8bit transfer mode. If the
  286. * mmc switch command returns error then switch to
  287. * 4bit transfer mode. On success set the corresponding
  288. * bus width on the host.
  289. */
  290. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  291. EXT_CSD_BUS_WIDTH,
  292. ext_csd_bits[idx]);
  293. if (err)
  294. continue;
  295. bus_width = bus_widths[idx];
  296. mmcsd_set_bus_width(host, bus_width);
  297. mmcsd_delay_ms(20); //delay 10ms
  298. err = mmc_compare_ext_csds(card, ext_csd, bus_width);
  299. if (!err) {
  300. err = bus_width;
  301. break;
  302. } else {
  303. switch(ext_csd_bits[idx]){
  304. case 0:
  305. LOG_E("switch to bus width 1 bit failed!");
  306. break;
  307. case 1:
  308. LOG_E("switch to bus width 4 bit failed!");
  309. break;
  310. case 2:
  311. LOG_E("switch to bus width 8 bit failed!");
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. }
  318. return err;
  319. }
  320. rt_err_t mmc_send_op_cond(struct rt_mmcsd_host *host,
  321. rt_uint32_t ocr, rt_uint32_t *rocr)
  322. {
  323. struct rt_mmcsd_cmd cmd;
  324. rt_uint32_t i;
  325. rt_err_t err = RT_EOK;
  326. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  327. cmd.cmd_code = SEND_OP_COND;
  328. cmd.arg = controller_is_spi(host) ? 0 : ocr;
  329. cmd.flags = RESP_SPI_R1 | RESP_R3 | CMD_BCR;
  330. for (i = 100; i; i--) {
  331. err = mmcsd_send_cmd(host, &cmd, 3);
  332. if (err)
  333. break;
  334. /* if we're just probing, do a single pass */
  335. if (ocr == 0)
  336. break;
  337. /* otherwise wait until reset completes */
  338. if (controller_is_spi(host)) {
  339. if (!(cmd.resp[0] & R1_SPI_IDLE))
  340. break;
  341. } else {
  342. if (cmd.resp[0] & CARD_BUSY)
  343. break;
  344. }
  345. err = -RT_ETIMEOUT;
  346. mmcsd_delay_ms(10); //delay 10ms
  347. }
  348. if (rocr && !controller_is_spi(host))
  349. *rocr = cmd.resp[0];
  350. return err;
  351. }
  352. static rt_err_t mmc_set_card_addr(struct rt_mmcsd_host *host, rt_uint32_t rca)
  353. {
  354. rt_err_t err;
  355. struct rt_mmcsd_cmd cmd;
  356. rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
  357. cmd.cmd_code = SET_RELATIVE_ADDR;
  358. cmd.arg = rca << 16;
  359. cmd.flags = RESP_R1 | CMD_AC;
  360. err = mmcsd_send_cmd(host, &cmd, 3);
  361. if (err)
  362. return err;
  363. return 0;
  364. }
  365. static rt_int32_t mmcsd_mmc_init_card(struct rt_mmcsd_host *host,
  366. rt_uint32_t ocr)
  367. {
  368. rt_int32_t err;
  369. rt_uint32_t resp[4];
  370. rt_uint32_t rocr = 0;
  371. rt_uint32_t max_data_rate;
  372. rt_uint8_t *ext_csd = RT_NULL;
  373. struct rt_mmcsd_card *card = RT_NULL;
  374. mmcsd_go_idle(host);
  375. /* The extra bit indicates that we support high capacity */
  376. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  377. if (err)
  378. goto err;
  379. if (controller_is_spi(host))
  380. {
  381. err = mmcsd_spi_use_crc(host, 1);
  382. if (err)
  383. goto err1;
  384. }
  385. if (controller_is_spi(host))
  386. err = mmcsd_get_cid(host, resp);
  387. else
  388. err = mmcsd_all_get_cid(host, resp);
  389. if (err)
  390. goto err;
  391. card = rt_malloc(sizeof(struct rt_mmcsd_card));
  392. if (!card)
  393. {
  394. LOG_E("malloc card failed!");
  395. err = -RT_ENOMEM;
  396. goto err;
  397. }
  398. rt_memset(card, 0, sizeof(struct rt_mmcsd_card));
  399. card->card_type = CARD_TYPE_MMC;
  400. card->host = host;
  401. card->rca = 1;
  402. rt_memcpy(card->resp_cid, resp, sizeof(card->resp_cid));
  403. /*
  404. * For native busses: get card RCA and quit open drain mode.
  405. */
  406. if (!controller_is_spi(host))
  407. {
  408. err = mmc_set_card_addr(host, card->rca);
  409. if (err)
  410. goto err1;
  411. mmcsd_set_bus_mode(host, MMCSD_BUSMODE_PUSHPULL);
  412. }
  413. err = mmcsd_get_csd(card, card->resp_csd);
  414. if (err)
  415. goto err1;
  416. err = mmcsd_parse_csd(card);
  417. if (err)
  418. goto err1;
  419. if (!controller_is_spi(host))
  420. {
  421. err = mmcsd_select_card(card);
  422. if (err)
  423. goto err1;
  424. }
  425. /*
  426. * Fetch and process extended CSD.
  427. */
  428. err = mmc_get_ext_csd(card, &ext_csd);
  429. if (err)
  430. goto err1;
  431. err = mmc_parse_ext_csd(card, ext_csd);
  432. if (err)
  433. goto err1;
  434. /* If doing byte addressing, check if required to do sector
  435. * addressing. Handle the case of <2GB cards needing sector
  436. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  437. * ocr register has bit 30 set for sector addressing.
  438. */
  439. if (!(card->flags & CARD_FLAG_SDHC) && (rocr & (1<<30)))
  440. card->flags |= CARD_FLAG_SDHC;
  441. /* set bus speed */
  442. max_data_rate = (unsigned int)-1;
  443. if (card->flags & CARD_FLAG_HIGHSPEED)
  444. {
  445. if (max_data_rate > card->hs_max_data_rate)
  446. max_data_rate = card->hs_max_data_rate;
  447. }
  448. else if (max_data_rate > card->max_data_rate)
  449. {
  450. max_data_rate = card->max_data_rate;
  451. }
  452. mmcsd_set_clock(host, max_data_rate);
  453. /*switch bus width*/
  454. mmc_select_bus_width(card, ext_csd);
  455. host->card = card;
  456. rt_free(ext_csd);
  457. return 0;
  458. err1:
  459. rt_free(card);
  460. err:
  461. return err;
  462. }
  463. /*
  464. * Starting point for mmc card init.
  465. */
  466. rt_int32_t init_mmc(struct rt_mmcsd_host *host, rt_uint32_t ocr)
  467. {
  468. rt_int32_t err;
  469. rt_uint32_t current_ocr;
  470. /*
  471. * We need to get OCR a different way for SPI.
  472. */
  473. if (controller_is_spi(host))
  474. {
  475. err = mmcsd_spi_read_ocr(host, 0, &ocr);
  476. if (err)
  477. goto err;
  478. }
  479. current_ocr = mmcsd_select_voltage(host, ocr);
  480. /*
  481. * Can we support the voltage(s) of the card(s)?
  482. */
  483. if (!current_ocr)
  484. {
  485. err = -RT_ERROR;
  486. goto err;
  487. }
  488. /*
  489. * Detect and init the card.
  490. */
  491. err = mmcsd_mmc_init_card(host, current_ocr);
  492. if (err)
  493. goto err;
  494. mmcsd_host_unlock(host);
  495. err = rt_mmcsd_blk_probe(host->card);
  496. if (err)
  497. goto remove_card;
  498. mmcsd_host_lock(host);
  499. return 0;
  500. remove_card:
  501. mmcsd_host_lock(host);
  502. rt_mmcsd_blk_remove(host->card);
  503. rt_free(host->card);
  504. host->card = RT_NULL;
  505. err:
  506. LOG_E("init MMC card failed!");
  507. return err;
  508. }