mmc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * linux/drivers/mmc/core/mmc.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
  6. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/mmc.h>
  16. #include "core.h"
  17. #include "bus.h"
  18. #include "mmc_ops.h"
  19. static const unsigned int tran_exp[] = {
  20. 10000, 100000, 1000000, 10000000,
  21. 0, 0, 0, 0
  22. };
  23. static const unsigned char tran_mant[] = {
  24. 0, 10, 12, 13, 15, 20, 25, 30,
  25. 35, 40, 45, 50, 55, 60, 70, 80,
  26. };
  27. static const unsigned int tacc_exp[] = {
  28. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  29. };
  30. static const unsigned int tacc_mant[] = {
  31. 0, 10, 12, 13, 15, 20, 25, 30,
  32. 35, 40, 45, 50, 55, 60, 70, 80,
  33. };
  34. #define UNSTUFF_BITS(resp,start,size) \
  35. ({ \
  36. const int __size = size; \
  37. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  38. const int __off = 3 - ((start) / 32); \
  39. const int __shft = (start) & 31; \
  40. u32 __res; \
  41. \
  42. __res = resp[__off] >> __shft; \
  43. if (__size + __shft > 32) \
  44. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  45. __res & __mask; \
  46. })
  47. /*
  48. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  49. */
  50. static int mmc_decode_cid(struct mmc_card *card)
  51. {
  52. u32 *resp = card->raw_cid;
  53. /*
  54. * The selection of the format here is based upon published
  55. * specs from sandisk and from what people have reported.
  56. */
  57. switch (card->csd.mmca_vsn) {
  58. case 0: /* MMC v1.0 - v1.2 */
  59. case 1: /* MMC v1.4 */
  60. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  61. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  62. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  63. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  64. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  65. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  66. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  67. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  68. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  69. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  70. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  71. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  72. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  73. break;
  74. case 2: /* MMC v2.0 - v2.2 */
  75. case 3: /* MMC v3.1 - v3.3 */
  76. case 4: /* MMC v4 */
  77. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  78. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  79. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  80. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  81. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  82. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  83. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  84. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  85. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  86. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  87. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  88. break;
  89. default:
  90. printk(KERN_ERR "%s: card has unknown MMCA version %d\n",
  91. mmc_hostname(card->host), card->csd.mmca_vsn);
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Given a 128-bit response, decode to our card CSD structure.
  98. */
  99. static int mmc_decode_csd(struct mmc_card *card)
  100. {
  101. struct mmc_csd *csd = &card->csd;
  102. unsigned int e, m, csd_struct;
  103. u32 *resp = card->raw_csd;
  104. /*
  105. * We only understand CSD structure v1.1 and v1.2.
  106. * v1.2 has extra information in bits 15, 11 and 10.
  107. */
  108. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  109. if (csd_struct != 1 && csd_struct != 2) {
  110. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  111. mmc_hostname(card->host), csd_struct);
  112. return -EINVAL;
  113. }
  114. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  115. m = UNSTUFF_BITS(resp, 115, 4);
  116. e = UNSTUFF_BITS(resp, 112, 3);
  117. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  118. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  119. m = UNSTUFF_BITS(resp, 99, 4);
  120. e = UNSTUFF_BITS(resp, 96, 3);
  121. csd->max_dtr = tran_exp[e] * tran_mant[m];
  122. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  123. e = UNSTUFF_BITS(resp, 47, 3);
  124. m = UNSTUFF_BITS(resp, 62, 12);
  125. csd->capacity = (1 + m) << (e + 2);
  126. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  127. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  128. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  129. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  130. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  131. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  132. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  133. return 0;
  134. }
  135. /*
  136. * Read and decode extended CSD.
  137. */
  138. static int mmc_read_ext_csd(struct mmc_card *card)
  139. {
  140. int err;
  141. u8 *ext_csd;
  142. BUG_ON(!card);
  143. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  144. return 0;
  145. /*
  146. * As the ext_csd is so large and mostly unused, we don't store the
  147. * raw block in mmc_card.
  148. */
  149. ext_csd = kmalloc(512, GFP_KERNEL);
  150. if (!ext_csd) {
  151. printk(KERN_ERR "%s: could not allocate a buffer to "
  152. "receive the ext_csd.\n", mmc_hostname(card->host));
  153. return -ENOMEM;
  154. }
  155. err = mmc_send_ext_csd(card, ext_csd);
  156. if (err) {
  157. /* If the host or the card can't do the switch,
  158. * fail more gracefully. */
  159. if ((err != -EINVAL)
  160. && (err != -ENOSYS)
  161. && (err != -EFAULT))
  162. goto out;
  163. /*
  164. * High capacity cards should have this "magic" size
  165. * stored in their CSD.
  166. */
  167. if (card->csd.capacity == (4096 * 512)) {
  168. printk(KERN_ERR "%s: unable to read EXT_CSD "
  169. "on a possible high capacity card. "
  170. "Card will be ignored.\n",
  171. mmc_hostname(card->host));
  172. } else {
  173. printk(KERN_WARNING "%s: unable to read "
  174. "EXT_CSD, performance might "
  175. "suffer.\n",
  176. mmc_hostname(card->host));
  177. err = 0;
  178. }
  179. goto out;
  180. }
  181. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  182. if (card->ext_csd.rev > 5) {
  183. printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
  184. "version %d\n", mmc_hostname(card->host),
  185. card->ext_csd.rev);
  186. err = -EINVAL;
  187. goto out;
  188. }
  189. if (card->ext_csd.rev >= 2) {
  190. card->ext_csd.sectors =
  191. ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
  192. ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
  193. ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
  194. ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
  195. if (card->ext_csd.sectors)
  196. mmc_card_set_blockaddr(card);
  197. }
  198. switch (ext_csd[EXT_CSD_CARD_TYPE]) {
  199. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  200. card->ext_csd.hs_max_dtr = 52000000;
  201. break;
  202. case EXT_CSD_CARD_TYPE_26:
  203. card->ext_csd.hs_max_dtr = 26000000;
  204. break;
  205. default:
  206. /* MMC v4 spec says this cannot happen */
  207. printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
  208. "support any high-speed modes.\n",
  209. mmc_hostname(card->host));
  210. goto out;
  211. }
  212. if (card->ext_csd.rev >= 3) {
  213. u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
  214. /* Sleep / awake timeout in 100ns units */
  215. if (sa_shift > 0 && sa_shift <= 0x17)
  216. card->ext_csd.sa_timeout =
  217. 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
  218. }
  219. out:
  220. kfree(ext_csd);
  221. return err;
  222. }
  223. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  224. card->raw_cid[2], card->raw_cid[3]);
  225. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  226. card->raw_csd[2], card->raw_csd[3]);
  227. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  228. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  229. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  230. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  231. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  232. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  233. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  234. static struct attribute *mmc_std_attrs[] = {
  235. &dev_attr_cid.attr,
  236. &dev_attr_csd.attr,
  237. &dev_attr_date.attr,
  238. &dev_attr_fwrev.attr,
  239. &dev_attr_hwrev.attr,
  240. &dev_attr_manfid.attr,
  241. &dev_attr_name.attr,
  242. &dev_attr_oemid.attr,
  243. &dev_attr_serial.attr,
  244. NULL,
  245. };
  246. static struct attribute_group mmc_std_attr_group = {
  247. .attrs = mmc_std_attrs,
  248. };
  249. static const struct attribute_group *mmc_attr_groups[] = {
  250. &mmc_std_attr_group,
  251. NULL,
  252. };
  253. static struct device_type mmc_type = {
  254. .groups = mmc_attr_groups,
  255. };
  256. /*
  257. * Handle the detection and initialisation of a card.
  258. *
  259. * In the case of a resume, "oldcard" will contain the card
  260. * we're trying to reinitialise.
  261. */
  262. static int mmc_init_card(struct mmc_host *host, u32 ocr,
  263. struct mmc_card *oldcard)
  264. {
  265. struct mmc_card *card;
  266. int err;
  267. u32 cid[4];
  268. unsigned int max_dtr;
  269. BUG_ON(!host);
  270. WARN_ON(!host->claimed);
  271. /*
  272. * Since we're changing the OCR value, we seem to
  273. * need to tell some cards to go back to the idle
  274. * state. We wait 1ms to give cards time to
  275. * respond.
  276. */
  277. mmc_go_idle(host);
  278. /* The extra bit indicates that we support high capacity */
  279. err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
  280. if (err)
  281. goto err;
  282. /*
  283. * For SPI, enable CRC as appropriate.
  284. */
  285. if (mmc_host_is_spi(host)) {
  286. err = mmc_spi_set_crc(host, use_spi_crc);
  287. if (err)
  288. goto err;
  289. }
  290. /*
  291. * Fetch CID from card.
  292. */
  293. if (mmc_host_is_spi(host))
  294. err = mmc_send_cid(host, cid);
  295. else
  296. err = mmc_all_send_cid(host, cid);
  297. if (err)
  298. goto err;
  299. if (oldcard) {
  300. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
  301. err = -ENOENT;
  302. goto err;
  303. }
  304. card = oldcard;
  305. } else {
  306. /*
  307. * Allocate card structure.
  308. */
  309. card = mmc_alloc_card(host, &mmc_type);
  310. if (IS_ERR(card)) {
  311. err = PTR_ERR(card);
  312. goto err;
  313. }
  314. card->type = MMC_TYPE_MMC;
  315. card->rca = 1;
  316. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  317. }
  318. /*
  319. * For native busses: set card RCA and quit open drain mode.
  320. */
  321. if (!mmc_host_is_spi(host)) {
  322. err = mmc_set_relative_addr(card);
  323. if (err)
  324. goto free_card;
  325. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  326. }
  327. if (!oldcard) {
  328. /*
  329. * Fetch CSD from card.
  330. */
  331. err = mmc_send_csd(card, card->raw_csd);
  332. if (err)
  333. goto free_card;
  334. err = mmc_decode_csd(card);
  335. if (err)
  336. goto free_card;
  337. err = mmc_decode_cid(card);
  338. if (err)
  339. goto free_card;
  340. }
  341. /*
  342. * Select card, as all following commands rely on that.
  343. */
  344. if (!mmc_host_is_spi(host)) {
  345. err = mmc_select_card(card);
  346. if (err)
  347. goto free_card;
  348. }
  349. if (!oldcard) {
  350. /*
  351. * Fetch and process extended CSD.
  352. */
  353. err = mmc_read_ext_csd(card);
  354. if (err)
  355. goto free_card;
  356. }
  357. /*
  358. * Activate high speed (if supported)
  359. */
  360. if ((card->ext_csd.hs_max_dtr != 0) &&
  361. (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
  362. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  363. EXT_CSD_HS_TIMING, 1);
  364. if (err && err != -EBADMSG)
  365. goto free_card;
  366. if (err) {
  367. printk(KERN_WARNING "%s: switch to highspeed failed\n",
  368. mmc_hostname(card->host));
  369. err = 0;
  370. } else {
  371. mmc_card_set_highspeed(card);
  372. mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
  373. }
  374. }
  375. /*
  376. * Compute bus speed.
  377. */
  378. max_dtr = (unsigned int)-1;
  379. if (mmc_card_highspeed(card)) {
  380. if (max_dtr > card->ext_csd.hs_max_dtr)
  381. max_dtr = card->ext_csd.hs_max_dtr;
  382. } else if (max_dtr > card->csd.max_dtr) {
  383. max_dtr = card->csd.max_dtr;
  384. }
  385. mmc_set_clock(host, max_dtr);
  386. /*
  387. * Activate wide bus (if supported).
  388. */
  389. if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
  390. (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
  391. unsigned ext_csd_bit, bus_width;
  392. if (host->caps & MMC_CAP_8_BIT_DATA) {
  393. ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
  394. bus_width = MMC_BUS_WIDTH_8;
  395. } else {
  396. ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
  397. bus_width = MMC_BUS_WIDTH_4;
  398. }
  399. err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  400. EXT_CSD_BUS_WIDTH, ext_csd_bit);
  401. if (err && err != -EBADMSG)
  402. goto free_card;
  403. if (err) {
  404. printk(KERN_WARNING "%s: switch to bus width %d "
  405. "failed\n", mmc_hostname(card->host),
  406. 1 << bus_width);
  407. err = 0;
  408. } else {
  409. mmc_set_bus_width(card->host, bus_width);
  410. }
  411. }
  412. if (!oldcard)
  413. host->card = card;
  414. return 0;
  415. free_card:
  416. if (!oldcard)
  417. mmc_remove_card(card);
  418. err:
  419. return err;
  420. }
  421. /*
  422. * Host is being removed. Free up the current card.
  423. */
  424. static void mmc_remove(struct mmc_host *host)
  425. {
  426. BUG_ON(!host);
  427. BUG_ON(!host->card);
  428. mmc_remove_card(host->card);
  429. host->card = NULL;
  430. }
  431. /*
  432. * Card detection callback from host.
  433. */
  434. static void mmc_detect(struct mmc_host *host)
  435. {
  436. int err;
  437. BUG_ON(!host);
  438. BUG_ON(!host->card);
  439. mmc_claim_host(host);
  440. /*
  441. * Just check if our card has been removed.
  442. */
  443. err = mmc_send_status(host->card, NULL);
  444. mmc_release_host(host);
  445. if (err) {
  446. mmc_remove(host);
  447. mmc_claim_host(host);
  448. mmc_detach_bus(host);
  449. mmc_release_host(host);
  450. }
  451. }
  452. /*
  453. * Suspend callback from host.
  454. */
  455. static int mmc_suspend(struct mmc_host *host)
  456. {
  457. BUG_ON(!host);
  458. BUG_ON(!host->card);
  459. mmc_claim_host(host);
  460. if (!mmc_host_is_spi(host))
  461. mmc_deselect_cards(host);
  462. host->card->state &= ~MMC_STATE_HIGHSPEED;
  463. mmc_release_host(host);
  464. return 0;
  465. }
  466. /*
  467. * Resume callback from host.
  468. *
  469. * This function tries to determine if the same card is still present
  470. * and, if so, restore all state to it.
  471. */
  472. static int mmc_resume(struct mmc_host *host)
  473. {
  474. int err;
  475. BUG_ON(!host);
  476. BUG_ON(!host->card);
  477. mmc_claim_host(host);
  478. err = mmc_init_card(host, host->ocr, host->card);
  479. mmc_release_host(host);
  480. return err;
  481. }
  482. static void mmc_power_restore(struct mmc_host *host)
  483. {
  484. host->card->state &= ~MMC_STATE_HIGHSPEED;
  485. mmc_claim_host(host);
  486. mmc_init_card(host, host->ocr, host->card);
  487. mmc_release_host(host);
  488. }
  489. static int mmc_sleep(struct mmc_host *host)
  490. {
  491. struct mmc_card *card = host->card;
  492. int err = -ENOSYS;
  493. if (card && card->ext_csd.rev >= 3) {
  494. err = mmc_card_sleepawake(host, 1);
  495. if (err < 0)
  496. pr_debug("%s: Error %d while putting card into sleep",
  497. mmc_hostname(host), err);
  498. }
  499. return err;
  500. }
  501. static int mmc_awake(struct mmc_host *host)
  502. {
  503. struct mmc_card *card = host->card;
  504. int err = -ENOSYS;
  505. if (card && card->ext_csd.rev >= 3) {
  506. err = mmc_card_sleepawake(host, 0);
  507. if (err < 0)
  508. pr_debug("%s: Error %d while awaking sleeping card",
  509. mmc_hostname(host), err);
  510. }
  511. return err;
  512. }
  513. static const struct mmc_bus_ops mmc_ops = {
  514. .awake = mmc_awake,
  515. .sleep = mmc_sleep,
  516. .remove = mmc_remove,
  517. .detect = mmc_detect,
  518. .suspend = NULL,
  519. .resume = NULL,
  520. .power_restore = mmc_power_restore,
  521. };
  522. static const struct mmc_bus_ops mmc_ops_unsafe = {
  523. .awake = mmc_awake,
  524. .sleep = mmc_sleep,
  525. .remove = mmc_remove,
  526. .detect = mmc_detect,
  527. .suspend = mmc_suspend,
  528. .resume = mmc_resume,
  529. .power_restore = mmc_power_restore,
  530. };
  531. static void mmc_attach_bus_ops(struct mmc_host *host)
  532. {
  533. const struct mmc_bus_ops *bus_ops;
  534. if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
  535. bus_ops = &mmc_ops_unsafe;
  536. else
  537. bus_ops = &mmc_ops;
  538. mmc_attach_bus(host, bus_ops);
  539. }
  540. /*
  541. * Starting point for MMC card init.
  542. */
  543. int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
  544. {
  545. int err;
  546. BUG_ON(!host);
  547. WARN_ON(!host->claimed);
  548. mmc_attach_bus_ops(host);
  549. /*
  550. * We need to get OCR a different way for SPI.
  551. */
  552. if (mmc_host_is_spi(host)) {
  553. err = mmc_spi_read_ocr(host, 1, &ocr);
  554. if (err)
  555. goto err;
  556. }
  557. /*
  558. * Sanity check the voltages that the card claims to
  559. * support.
  560. */
  561. if (ocr & 0x7F) {
  562. printk(KERN_WARNING "%s: card claims to support voltages "
  563. "below the defined range. These will be ignored.\n",
  564. mmc_hostname(host));
  565. ocr &= ~0x7F;
  566. }
  567. host->ocr = mmc_select_voltage(host, ocr);
  568. /*
  569. * Can we support the voltage of the card?
  570. */
  571. if (!host->ocr) {
  572. err = -EINVAL;
  573. goto err;
  574. }
  575. /*
  576. * Detect and init the card.
  577. */
  578. err = mmc_init_card(host, host->ocr, NULL);
  579. if (err)
  580. goto err;
  581. mmc_release_host(host);
  582. err = mmc_add_card(host->card);
  583. if (err)
  584. goto remove_card;
  585. return 0;
  586. remove_card:
  587. mmc_remove_card(host->card);
  588. host->card = NULL;
  589. mmc_claim_host(host);
  590. err:
  591. mmc_detach_bus(host);
  592. mmc_release_host(host);
  593. printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
  594. mmc_hostname(host), err);
  595. return err;
  596. }