mmc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. return cmd.err;
  137. if (data.err)
  138. return data.err;
  139. *new_ext_csd = ext_csd;
  140. return 0;
  141. }
  142. /*
  143. * Decode extended CSD.
  144. */
  145. static int mmc_parse_ext_csd(struct mmcsd_card *card, uint8_t *ext_csd)
  146. {
  147. uint64_t card_capacity = 0;
  148. if(card == NULL || ext_csd == NULL)
  149. {
  150. TRACE_ERROR("emmc parse ext csd fail, invaild args");
  151. return -1;
  152. }
  153. card->flags |= CARD_FLAG_HIGHSPEED;
  154. card->hs_max_data_rate = 52000000;
  155. card_capacity = *((uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
  156. card->card_blknr = *((uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
  157. card_capacity *= card->card_blksize;
  158. card_capacity >>= 10; /* unit:KB */
  159. card->card_capacity = card_capacity;
  160. TRACE_INFO("emmc card capacity %d KB.", card->card_capacity);
  161. return 0;
  162. }
  163. /**
  164. * mmc_switch - modify EXT_CSD register
  165. * @card: the MMC card associated with the data transfer
  166. * @set: cmd set values
  167. * @index: EXT_CSD register index
  168. * @value: value to program into EXT_CSD register
  169. *
  170. * Modifies the EXT_CSD register for selected card.
  171. */
  172. static int mmc_switch(struct mmcsd_card *card, uint8_t set,
  173. uint8_t index, uint8_t value)
  174. {
  175. int err;
  176. struct mmcsd_host *host = card->host;
  177. struct mmcsd_cmd cmd = {0};
  178. cmd.cmd_code = SWITCH;
  179. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  180. (index << 16) | (value << 8) | set;
  181. cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
  182. err = mmcsd_send_cmd(host, &cmd, 3);
  183. if (err)
  184. return err;
  185. return 0;
  186. }
  187. static int mmc_compare_ext_csds(struct mmcsd_card *card,
  188. uint8_t *ext_csd, uint32_t bus_width)
  189. {
  190. uint8_t *bw_ext_csd;
  191. int err;
  192. if (bus_width == MMCSD_BUS_WIDTH_1)
  193. return 0;
  194. err = mmc_get_ext_csd(card, &bw_ext_csd);
  195. if (err || bw_ext_csd == NULL) {
  196. err = -1;
  197. goto out;
  198. }
  199. /* only compare read only fields */
  200. err = !((ext_csd[EXT_CSD_PARTITION_SUPPORT] == bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
  201. (ext_csd[EXT_CSD_ERASED_MEM_CONT] == bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
  202. (ext_csd[EXT_CSD_REV] == bw_ext_csd[EXT_CSD_REV]) &&
  203. (ext_csd[EXT_CSD_STRUCTURE] == bw_ext_csd[EXT_CSD_STRUCTURE]) &&
  204. (ext_csd[EXT_CSD_CARD_TYPE] == bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
  205. (ext_csd[EXT_CSD_S_A_TIMEOUT] == bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
  206. (ext_csd[EXT_CSD_HC_WP_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
  207. (ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT] == bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
  208. (ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
  209. (ext_csd[EXT_CSD_SEC_TRIM_MULT] == bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
  210. (ext_csd[EXT_CSD_SEC_ERASE_MULT] == bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
  211. (ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] == bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
  212. (ext_csd[EXT_CSD_TRIM_MULT] == bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
  213. (ext_csd[EXT_CSD_SEC_CNT + 0] == bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
  214. (ext_csd[EXT_CSD_SEC_CNT + 1] == bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
  215. (ext_csd[EXT_CSD_SEC_CNT + 2] == bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
  216. (ext_csd[EXT_CSD_SEC_CNT + 3] == bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
  217. (ext_csd[EXT_CSD_PWR_CL_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
  218. (ext_csd[EXT_CSD_PWR_CL_26_195] == bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
  219. (ext_csd[EXT_CSD_PWR_CL_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
  220. (ext_csd[EXT_CSD_PWR_CL_26_360] == bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
  221. (ext_csd[EXT_CSD_PWR_CL_200_195] == bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
  222. (ext_csd[EXT_CSD_PWR_CL_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
  223. (ext_csd[EXT_CSD_PWR_CL_DDR_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
  224. (ext_csd[EXT_CSD_PWR_CL_DDR_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
  225. (ext_csd[EXT_CSD_PWR_CL_DDR_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
  226. if (err)
  227. err = -1;
  228. out:
  229. vPortFree(bw_ext_csd);
  230. return err;
  231. }
  232. static int mmc_select_speed_mode(struct mmcsd_card *card, uint8_t *ext_csd, EXT_CSD_HS_types mode)
  233. {
  234. static uint32_t speed_mode = 0;
  235. uint8_t *bw_ext_csd;
  236. int err = 0;
  237. if(card == NULL || ext_csd == NULL){
  238. TRACE_ERROR("emmc parse ext csd fail, invaild args");
  239. return -1;
  240. }
  241. speed_mode = *((uint32_t *)&ext_csd[EXT_CSD_HS_TIMING]);
  242. if(speed_mode != mode){
  243. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, mode);
  244. if (err)
  245. goto out;
  246. err = mmc_get_ext_csd(card, &bw_ext_csd);
  247. if (err)
  248. goto out;
  249. speed_mode = *((uint32_t *)&bw_ext_csd[EXT_CSD_HS_TIMING]);
  250. if(speed_mode != mode){
  251. printf("mmc_select_speed_mode fail!\n");
  252. err =-1;
  253. goto out;
  254. }
  255. *((uint32_t *)&ext_csd[EXT_CSD_HS_TIMING]) = speed_mode;
  256. }
  257. out:
  258. if(bw_ext_csd)
  259. vPortFree(bw_ext_csd);
  260. return err;
  261. }
  262. /*
  263. * Select the bus width amoung 4-bit and 8-bit(SDR).
  264. * If the bus width is changed successfully, return the selected width value.
  265. * Zero is returned instead of error value if the wide width is not supported.
  266. */
  267. static int mmc_select_bus_width(struct mmcsd_card *card, uint8_t *ext_csd)
  268. {
  269. uint32_t ext_csd_bits[] = {
  270. EXT_CSD_BUS_WIDTH_8,
  271. EXT_CSD_BUS_WIDTH_4,
  272. EXT_CSD_BUS_WIDTH_1
  273. };
  274. uint32_t bus_widths[] = {
  275. MMCSD_BUS_WIDTH_8,
  276. MMCSD_BUS_WIDTH_4,
  277. MMCSD_BUS_WIDTH_1
  278. };
  279. struct mmcsd_host *host = card->host;
  280. unsigned idx, bus_width = 0;
  281. int err = 0;
  282. if (GET_BITS(card->resp_csd, 122, 4) < 4)
  283. return 0;
  284. /*
  285. * Unlike SD, MMC cards dont have a configuration register to notify
  286. * supported bus width. So bus test command should be run to identify
  287. * the supported bus width or compare the ext csd values of current
  288. * bus width and ext csd values of 1 bit mode read earlier.
  289. */
  290. for (idx = 0; idx < sizeof(bus_widths)/sizeof(uint32_t); idx++) {
  291. /*
  292. * Host is capable of 8bit transfer, then switch
  293. * the device to work in 8bit transfer mode. If the
  294. * mmc switch command returns error then switch to
  295. * 4bit transfer mode. On success set the corresponding
  296. * bus width on the host. Meanwhile, mmc core would
  297. * bail out early if corresponding bus capable wasn't
  298. * set by drivers.
  299. */
  300. if ((!(host->flags & MMCSD_BUSWIDTH_8) &&
  301. ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_8) ||
  302. (!(host->flags & MMCSD_BUSWIDTH_4) &&
  303. (ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_4 ||
  304. ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_8)))
  305. continue;
  306. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  307. EXT_CSD_BUS_WIDTH,
  308. ext_csd_bits[idx]);
  309. if (err)
  310. continue;
  311. bus_width = bus_widths[idx];
  312. mmcsd_set_bus_width(host, bus_width);
  313. mmcsd_delay_ms(20); //delay 10ms
  314. err = mmc_compare_ext_csds(card, ext_csd, bus_width);
  315. if (!err) {
  316. err = bus_width;
  317. break;
  318. } else {
  319. switch(ext_csd_bits[idx]){
  320. case 0:
  321. TRACE_ERROR("switch to bus width 1 bit failed!");
  322. break;
  323. case 1:
  324. TRACE_ERROR("switch to bus width 4 bit failed!");
  325. break;
  326. case 2:
  327. TRACE_ERROR("switch to bus width 8 bit failed!");
  328. break;
  329. default:
  330. break;
  331. }
  332. }
  333. }
  334. return err;
  335. }
  336. int mmc_send_op_cond(struct mmcsd_host *host,
  337. uint32_t ocr, uint32_t *rocr)
  338. {
  339. struct mmcsd_cmd cmd;
  340. uint32_t i;
  341. int err = 0;
  342. memset(&cmd, 0, sizeof(struct mmcsd_cmd));
  343. cmd.cmd_code = SEND_OP_COND;
  344. cmd.arg = controller_is_spi(host) ? 0 : ocr;
  345. cmd.flags = RESP_SPI_R1 | RESP_R3 | CMD_BCR;
  346. for (i = 100; i; i--) {
  347. err = mmcsd_send_cmd(host, &cmd, 3);
  348. if (err)
  349. break;
  350. /* if we're just probing, do a single pass */
  351. if (ocr == 0)
  352. break;
  353. /* otherwise wait until reset completes */
  354. if (controller_is_spi(host)) {
  355. if (!(cmd.resp[0] & R1_SPI_IDLE))
  356. break;
  357. } else {
  358. if (cmd.resp[0] & CARD_BUSY)
  359. break;
  360. }
  361. err = -1;
  362. mmcsd_delay_ms(10); //delay 10ms
  363. }
  364. if (rocr && !controller_is_spi(host))
  365. *rocr = cmd.resp[0];
  366. return err;
  367. }
  368. static int mmc_set_card_addr(struct mmcsd_host *host, uint32_t rca)
  369. {
  370. int err;
  371. struct mmcsd_cmd cmd;
  372. memset(&cmd, 0, sizeof(struct mmcsd_cmd));
  373. cmd.cmd_code = SET_RELATIVE_ADDR;
  374. cmd.arg = rca << 16;
  375. cmd.flags = RESP_R1 | CMD_AC;
  376. err = mmcsd_send_cmd(host, &cmd, 3);
  377. if (err)
  378. return err;
  379. return 0;
  380. }
  381. static int32_t mmcsd_mmc_init_card(struct mmcsd_host *host,
  382. uint32_t ocr)
  383. {
  384. int32_t err;
  385. uint32_t resp[4];
  386. uint32_t rocr = 0;
  387. uint32_t max_data_rate;
  388. uint8_t *ext_csd = NULL;
  389. struct mmcsd_card *card = NULL;
  390. mmcsd_go_idle(host);
  391. /* The extra bit indicates that we support high capacity */
  392. err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
  393. if (err)
  394. goto err;
  395. if (controller_is_spi(host))
  396. {
  397. err = mmcsd_spi_use_crc(host, 1);
  398. if (err)
  399. goto err1;
  400. }
  401. if (controller_is_spi(host))
  402. err = mmcsd_get_cid(host, resp);
  403. else
  404. err = mmcsd_all_get_cid(host, resp);
  405. if (err)
  406. goto err;
  407. card = pvPortMalloc(sizeof(struct mmcsd_card));
  408. if (!card)
  409. {
  410. TRACE_ERROR("malloc card failed!");
  411. err = -ENOMEM;
  412. goto err;
  413. }
  414. memset(card, 0, sizeof(struct mmcsd_card));
  415. card->card_type = CARD_TYPE_MMC;
  416. card->host = host;
  417. card->rca = 1;
  418. memcpy(card->resp_cid, resp, sizeof(card->resp_cid));
  419. /*
  420. * For native busses: get card RCA and quit open drain mode.
  421. */
  422. if (!controller_is_spi(host))
  423. {
  424. err = mmc_set_card_addr(host, card->rca);
  425. if (err)
  426. goto err1;
  427. mmcsd_set_bus_mode(host, MMCSD_BUSMODE_PUSHPULL);
  428. }
  429. err = mmcsd_get_csd(card, card->resp_csd);
  430. if (err)
  431. goto err1;
  432. err = mmcsd_parse_csd(card);
  433. if (err)
  434. goto err1;
  435. if (!controller_is_spi(host))
  436. {
  437. err = mmcsd_select_card(card);
  438. if (err)
  439. goto err1;
  440. }
  441. /*
  442. * Fetch and process extended CSD.
  443. */
  444. err = mmc_get_ext_csd(card, &ext_csd);
  445. if (err || ext_csd == NULL)
  446. goto err1;
  447. err = mmc_parse_ext_csd(card, ext_csd);
  448. if (err)
  449. goto err1;
  450. mmc_select_speed_mode(card, ext_csd,EXT_CSD_HS_TIMING_HIGH_SPEED);
  451. /* If doing byte addressing, check if required to do sector
  452. * addressing. Handle the case of <2GB cards needing sector
  453. * addressing. See section 8.1 JEDEC Standard JED84-A441;
  454. * ocr register has bit 30 set for sector addressing.
  455. */
  456. if (!(card->flags & CARD_FLAG_SDHC) && (rocr & (1<<30)))
  457. card->flags |= CARD_FLAG_SDHC;
  458. /* set bus speed */
  459. if (card->flags & CARD_FLAG_HIGHSPEED)
  460. max_data_rate = card->hs_max_data_rate;
  461. else
  462. max_data_rate = card->max_data_rate;
  463. mmcsd_set_clock(host, max_data_rate);
  464. /*switch bus width*/
  465. mmc_select_bus_width(card, ext_csd);
  466. host->card = card;
  467. vPortFree(ext_csd);
  468. return 0;
  469. err1:
  470. if (ext_csd)
  471. vPortFree(ext_csd);
  472. vPortFree(card);
  473. err:
  474. return err;
  475. }
  476. /*
  477. * Starting point for mmc card init.
  478. */
  479. int32_t init_mmc(struct mmcsd_host *host, uint32_t ocr)
  480. {
  481. int32_t err;
  482. uint32_t current_ocr;
  483. /*
  484. * We need to get OCR a different way for SPI.
  485. */
  486. if (controller_is_spi(host))
  487. {
  488. err = mmcsd_spi_read_ocr(host, 0, &ocr);
  489. if (err)
  490. goto err;
  491. }
  492. current_ocr = mmcsd_select_voltage(host, ocr);
  493. /*
  494. * Can we support the voltage(s) of the card(s)?
  495. */
  496. if (!current_ocr)
  497. {
  498. err = -1;
  499. goto err;
  500. }
  501. /*
  502. * Detect and init the card.
  503. */
  504. err = mmcsd_mmc_init_card(host, current_ocr);
  505. if (err)
  506. goto err;
  507. mmcsd_host_unlock(host);
  508. mmcsd_host_lock(host);
  509. return 0;
  510. err:
  511. TRACE_ERROR("init MMC card failed!");
  512. return err;
  513. }