mmc.c 16 KB

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