sd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * linux/drivers/mmc/core/sd.c
  3. *
  4. * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
  5. * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
  6. * Copyright (C) 2005-2007 Pierre Ossman, 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/slab.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/mmc/sd.h>
  18. #include "core.h"
  19. #include "bus.h"
  20. #include "mmc_ops.h"
  21. #include "sd_ops.h"
  22. static const unsigned int tran_exp[] = {
  23. 10000, 100000, 1000000, 10000000,
  24. 0, 0, 0, 0
  25. };
  26. static const unsigned char tran_mant[] = {
  27. 0, 10, 12, 13, 15, 20, 25, 30,
  28. 35, 40, 45, 50, 55, 60, 70, 80,
  29. };
  30. static const unsigned int tacc_exp[] = {
  31. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  32. };
  33. static const unsigned int tacc_mant[] = {
  34. 0, 10, 12, 13, 15, 20, 25, 30,
  35. 35, 40, 45, 50, 55, 60, 70, 80,
  36. };
  37. #define UNSTUFF_BITS(resp,start,size) \
  38. ({ \
  39. const int __size = size; \
  40. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  41. const int __off = 3 - ((start) / 32); \
  42. const int __shft = (start) & 31; \
  43. u32 __res; \
  44. \
  45. __res = resp[__off] >> __shft; \
  46. if (__size + __shft > 32) \
  47. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  48. __res & __mask; \
  49. })
  50. /*
  51. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  52. */
  53. void mmc_decode_cid(struct mmc_card *card)
  54. {
  55. u32 *resp = card->raw_cid;
  56. memset(&card->cid, 0, sizeof(struct mmc_cid));
  57. /*
  58. * SD doesn't currently have a version field so we will
  59. * have to assume we can parse this.
  60. */
  61. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  62. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  63. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  64. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  65. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  66. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  67. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  68. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  69. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  70. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  71. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  72. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  73. card->cid.year += 2000; /* SD cards year offset */
  74. }
  75. /*
  76. * Given a 128-bit response, decode to our card CSD structure.
  77. */
  78. static int mmc_decode_csd(struct mmc_card *card)
  79. {
  80. struct mmc_csd *csd = &card->csd;
  81. unsigned int e, m, csd_struct;
  82. u32 *resp = card->raw_csd;
  83. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  84. switch (csd_struct) {
  85. case 0:
  86. m = UNSTUFF_BITS(resp, 115, 4);
  87. e = UNSTUFF_BITS(resp, 112, 3);
  88. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  89. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  90. m = UNSTUFF_BITS(resp, 99, 4);
  91. e = UNSTUFF_BITS(resp, 96, 3);
  92. csd->max_dtr = tran_exp[e] * tran_mant[m];
  93. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  94. e = UNSTUFF_BITS(resp, 47, 3);
  95. m = UNSTUFF_BITS(resp, 62, 12);
  96. csd->capacity = (1 + m) << (e + 2);
  97. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  98. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  99. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  100. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  101. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  102. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  103. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  104. break;
  105. case 1:
  106. /*
  107. * This is a block-addressed SDHC card. Most
  108. * interesting fields are unused and have fixed
  109. * values. To avoid getting tripped by buggy cards,
  110. * we assume those fixed values ourselves.
  111. */
  112. mmc_card_set_blockaddr(card);
  113. csd->tacc_ns = 0; /* Unused */
  114. csd->tacc_clks = 0; /* Unused */
  115. m = UNSTUFF_BITS(resp, 99, 4);
  116. e = UNSTUFF_BITS(resp, 96, 3);
  117. csd->max_dtr = tran_exp[e] * tran_mant[m];
  118. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  119. m = UNSTUFF_BITS(resp, 48, 22);
  120. csd->capacity = (1 + m) << 10;
  121. csd->read_blkbits = 9;
  122. csd->read_partial = 0;
  123. csd->write_misalign = 0;
  124. csd->read_misalign = 0;
  125. csd->r2w_factor = 4; /* Unused */
  126. csd->write_blkbits = 9;
  127. csd->write_partial = 0;
  128. break;
  129. default:
  130. printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
  131. mmc_hostname(card->host), csd_struct);
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Given a 64-bit response, decode to our card SCR structure.
  138. */
  139. static int mmc_decode_scr(struct mmc_card *card)
  140. {
  141. struct sd_scr *scr = &card->scr;
  142. unsigned int scr_struct;
  143. u32 resp[4];
  144. resp[3] = card->raw_scr[1];
  145. resp[2] = card->raw_scr[0];
  146. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  147. if (scr_struct != 0) {
  148. printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
  149. mmc_hostname(card->host), scr_struct);
  150. return -EINVAL;
  151. }
  152. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  153. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  154. return 0;
  155. }
  156. /*
  157. * Fetches and decodes switch information
  158. */
  159. static int mmc_read_switch(struct mmc_card *card)
  160. {
  161. int err;
  162. u8 *status;
  163. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  164. return 0;
  165. if (!(card->csd.cmdclass & CCC_SWITCH)) {
  166. printk(KERN_WARNING "%s: card lacks mandatory switch "
  167. "function, performance might suffer.\n",
  168. mmc_hostname(card->host));
  169. return 0;
  170. }
  171. err = -EIO;
  172. status = kmalloc(64, GFP_KERNEL);
  173. if (!status) {
  174. printk(KERN_ERR "%s: could not allocate a buffer for "
  175. "switch capabilities.\n", mmc_hostname(card->host));
  176. return -ENOMEM;
  177. }
  178. err = mmc_sd_switch(card, 0, 0, 1, status);
  179. if (err) {
  180. /* If the host or the card can't do the switch,
  181. * fail more gracefully. */
  182. if ((err != -EINVAL)
  183. && (err != -ENOSYS)
  184. && (err != -EFAULT))
  185. goto out;
  186. printk(KERN_WARNING "%s: problem reading switch "
  187. "capabilities, performance might suffer.\n",
  188. mmc_hostname(card->host));
  189. err = 0;
  190. goto out;
  191. }
  192. if (status[13] & 0x02)
  193. card->sw_caps.hs_max_dtr = 50000000;
  194. out:
  195. kfree(status);
  196. return err;
  197. }
  198. /*
  199. * Test if the card supports high-speed mode and, if so, switch to it.
  200. */
  201. int mmc_sd_switch_hs(struct mmc_card *card)
  202. {
  203. int err;
  204. u8 *status;
  205. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  206. return 0;
  207. if (!(card->csd.cmdclass & CCC_SWITCH))
  208. return 0;
  209. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  210. return 0;
  211. if (card->sw_caps.hs_max_dtr == 0)
  212. return 0;
  213. err = -EIO;
  214. status = kmalloc(64, GFP_KERNEL);
  215. if (!status) {
  216. printk(KERN_ERR "%s: could not allocate a buffer for "
  217. "switch capabilities.\n", mmc_hostname(card->host));
  218. return -ENOMEM;
  219. }
  220. err = mmc_sd_switch(card, 1, 0, 1, status);
  221. if (err)
  222. goto out;
  223. if ((status[16] & 0xF) != 1) {
  224. printk(KERN_WARNING "%s: Problem switching card "
  225. "into high-speed mode!\n",
  226. mmc_hostname(card->host));
  227. err = 0;
  228. } else {
  229. err = 1;
  230. }
  231. out:
  232. kfree(status);
  233. return err;
  234. }
  235. MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  236. card->raw_cid[2], card->raw_cid[3]);
  237. MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  238. card->raw_csd[2], card->raw_csd[3]);
  239. MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  240. MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  241. MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  242. MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  243. MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  244. MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
  245. MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  246. MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
  247. static struct attribute *sd_std_attrs[] = {
  248. &dev_attr_cid.attr,
  249. &dev_attr_csd.attr,
  250. &dev_attr_scr.attr,
  251. &dev_attr_date.attr,
  252. &dev_attr_fwrev.attr,
  253. &dev_attr_hwrev.attr,
  254. &dev_attr_manfid.attr,
  255. &dev_attr_name.attr,
  256. &dev_attr_oemid.attr,
  257. &dev_attr_serial.attr,
  258. NULL,
  259. };
  260. static struct attribute_group sd_std_attr_group = {
  261. .attrs = sd_std_attrs,
  262. };
  263. static const struct attribute_group *sd_attr_groups[] = {
  264. &sd_std_attr_group,
  265. NULL,
  266. };
  267. struct device_type sd_type = {
  268. .groups = sd_attr_groups,
  269. };
  270. /*
  271. * Fetch CID from card.
  272. */
  273. int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
  274. {
  275. int err;
  276. /*
  277. * Since we're changing the OCR value, we seem to
  278. * need to tell some cards to go back to the idle
  279. * state. We wait 1ms to give cards time to
  280. * respond.
  281. */
  282. mmc_go_idle(host);
  283. /*
  284. * If SD_SEND_IF_COND indicates an SD 2.0
  285. * compliant card and we should set bit 30
  286. * of the ocr to indicate that we can handle
  287. * block-addressed SDHC cards.
  288. */
  289. err = mmc_send_if_cond(host, ocr);
  290. if (!err)
  291. ocr |= 1 << 30;
  292. err = mmc_send_app_op_cond(host, ocr, NULL);
  293. if (err)
  294. return err;
  295. if (mmc_host_is_spi(host))
  296. err = mmc_send_cid(host, cid);
  297. else
  298. err = mmc_all_send_cid(host, cid);
  299. return err;
  300. }
  301. int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
  302. {
  303. int err;
  304. /*
  305. * Fetch CSD from card.
  306. */
  307. err = mmc_send_csd(card, card->raw_csd);
  308. if (err)
  309. return err;
  310. err = mmc_decode_csd(card);
  311. if (err)
  312. return err;
  313. return 0;
  314. }
  315. int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
  316. bool reinit)
  317. {
  318. int err;
  319. if (!reinit) {
  320. /*
  321. * Fetch SCR from card.
  322. */
  323. err = mmc_app_send_scr(card, card->raw_scr);
  324. if (err)
  325. return err;
  326. err = mmc_decode_scr(card);
  327. if (err)
  328. return err;
  329. /*
  330. * Fetch switch information from card.
  331. */
  332. err = mmc_read_switch(card);
  333. if (err)
  334. return err;
  335. }
  336. /*
  337. * For SPI, enable CRC as appropriate.
  338. * This CRC enable is located AFTER the reading of the
  339. * card registers because some SDHC cards are not able
  340. * to provide valid CRCs for non-512-byte blocks.
  341. */
  342. if (mmc_host_is_spi(host)) {
  343. err = mmc_spi_set_crc(host, use_spi_crc);
  344. if (err)
  345. return err;
  346. }
  347. /*
  348. * Check if read-only switch is active.
  349. */
  350. if (!reinit) {
  351. int ro = -1;
  352. if (host->ops->get_ro)
  353. ro = host->ops->get_ro(host);
  354. if (ro < 0) {
  355. printk(KERN_WARNING "%s: host does not "
  356. "support reading read-only "
  357. "switch. assuming write-enable.\n",
  358. mmc_hostname(host));
  359. } else if (ro > 0) {
  360. mmc_card_set_readonly(card);
  361. }
  362. }
  363. return 0;
  364. }
  365. unsigned mmc_sd_get_max_clock(struct mmc_card *card)
  366. {
  367. unsigned max_dtr = (unsigned int)-1;
  368. if (mmc_card_highspeed(card)) {
  369. if (max_dtr > card->sw_caps.hs_max_dtr)
  370. max_dtr = card->sw_caps.hs_max_dtr;
  371. } else if (max_dtr > card->csd.max_dtr) {
  372. max_dtr = card->csd.max_dtr;
  373. }
  374. return max_dtr;
  375. }
  376. void mmc_sd_go_highspeed(struct mmc_card *card)
  377. {
  378. mmc_card_set_highspeed(card);
  379. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  380. }
  381. /*
  382. * Handle the detection and initialisation of a card.
  383. *
  384. * In the case of a resume, "oldcard" will contain the card
  385. * we're trying to reinitialise.
  386. */
  387. static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
  388. struct mmc_card *oldcard)
  389. {
  390. struct mmc_card *card;
  391. int err;
  392. u32 cid[4];
  393. BUG_ON(!host);
  394. WARN_ON(!host->claimed);
  395. err = mmc_sd_get_cid(host, ocr, cid);
  396. if (err)
  397. return err;
  398. if (oldcard) {
  399. if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
  400. return -ENOENT;
  401. card = oldcard;
  402. } else {
  403. /*
  404. * Allocate card structure.
  405. */
  406. card = mmc_alloc_card(host, &sd_type);
  407. if (IS_ERR(card))
  408. return PTR_ERR(card);
  409. card->type = MMC_TYPE_SD;
  410. memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
  411. }
  412. /*
  413. * For native busses: get card RCA and quit open drain mode.
  414. */
  415. if (!mmc_host_is_spi(host)) {
  416. err = mmc_send_relative_addr(host, &card->rca);
  417. if (err)
  418. return err;
  419. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  420. }
  421. if (!oldcard) {
  422. err = mmc_sd_get_csd(host, card);
  423. if (err)
  424. return err;
  425. mmc_decode_cid(card);
  426. }
  427. /*
  428. * Select card, as all following commands rely on that.
  429. */
  430. if (!mmc_host_is_spi(host)) {
  431. err = mmc_select_card(card);
  432. if (err)
  433. return err;
  434. }
  435. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  436. if (err)
  437. goto free_card;
  438. /*
  439. * Attempt to change to high-speed (if supported)
  440. */
  441. err = mmc_sd_switch_hs(card);
  442. if (err > 0)
  443. mmc_sd_go_highspeed(card);
  444. else if (err)
  445. goto free_card;
  446. /*
  447. * Set bus speed.
  448. */
  449. mmc_set_clock(host, mmc_sd_get_max_clock(card));
  450. /*
  451. * Switch to wider bus (if supported).
  452. */
  453. if ((host->caps & MMC_CAP_4_BIT_DATA) &&
  454. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  455. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  456. if (err)
  457. goto free_card;
  458. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  459. }
  460. host->card = card;
  461. return 0;
  462. free_card:
  463. if (!oldcard)
  464. mmc_remove_card(card);
  465. return err;
  466. }
  467. /*
  468. * Host is being removed. Free up the current card.
  469. */
  470. static void mmc_sd_remove(struct mmc_host *host)
  471. {
  472. BUG_ON(!host);
  473. BUG_ON(!host->card);
  474. mmc_remove_card(host->card);
  475. host->card = NULL;
  476. }
  477. /*
  478. * Card detection callback from host.
  479. */
  480. static void mmc_sd_detect(struct mmc_host *host)
  481. {
  482. int err;
  483. BUG_ON(!host);
  484. BUG_ON(!host->card);
  485. mmc_claim_host(host);
  486. /*
  487. * Just check if our card has been removed.
  488. */
  489. err = mmc_send_status(host->card, NULL);
  490. mmc_release_host(host);
  491. if (err) {
  492. mmc_sd_remove(host);
  493. mmc_claim_host(host);
  494. mmc_detach_bus(host);
  495. mmc_release_host(host);
  496. }
  497. }
  498. /*
  499. * Suspend callback from host.
  500. */
  501. static int mmc_sd_suspend(struct mmc_host *host)
  502. {
  503. BUG_ON(!host);
  504. BUG_ON(!host->card);
  505. mmc_claim_host(host);
  506. if (!mmc_host_is_spi(host))
  507. mmc_deselect_cards(host);
  508. host->card->state &= ~MMC_STATE_HIGHSPEED;
  509. mmc_release_host(host);
  510. return 0;
  511. }
  512. /*
  513. * Resume callback from host.
  514. *
  515. * This function tries to determine if the same card is still present
  516. * and, if so, restore all state to it.
  517. */
  518. static int mmc_sd_resume(struct mmc_host *host)
  519. {
  520. int err;
  521. BUG_ON(!host);
  522. BUG_ON(!host->card);
  523. mmc_claim_host(host);
  524. err = mmc_sd_init_card(host, host->ocr, host->card);
  525. mmc_release_host(host);
  526. return err;
  527. }
  528. static void mmc_sd_power_restore(struct mmc_host *host)
  529. {
  530. host->card->state &= ~MMC_STATE_HIGHSPEED;
  531. mmc_claim_host(host);
  532. mmc_sd_init_card(host, host->ocr, host->card);
  533. mmc_release_host(host);
  534. }
  535. static const struct mmc_bus_ops mmc_sd_ops = {
  536. .remove = mmc_sd_remove,
  537. .detect = mmc_sd_detect,
  538. .suspend = NULL,
  539. .resume = NULL,
  540. .power_restore = mmc_sd_power_restore,
  541. };
  542. static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
  543. .remove = mmc_sd_remove,
  544. .detect = mmc_sd_detect,
  545. .suspend = mmc_sd_suspend,
  546. .resume = mmc_sd_resume,
  547. .power_restore = mmc_sd_power_restore,
  548. };
  549. static void mmc_sd_attach_bus_ops(struct mmc_host *host)
  550. {
  551. const struct mmc_bus_ops *bus_ops;
  552. if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable)
  553. bus_ops = &mmc_sd_ops_unsafe;
  554. else
  555. bus_ops = &mmc_sd_ops;
  556. mmc_attach_bus(host, bus_ops);
  557. }
  558. /*
  559. * Starting point for SD card init.
  560. */
  561. int mmc_attach_sd(struct mmc_host *host, u32 ocr)
  562. {
  563. int err;
  564. BUG_ON(!host);
  565. WARN_ON(!host->claimed);
  566. mmc_sd_attach_bus_ops(host);
  567. /*
  568. * We need to get OCR a different way for SPI.
  569. */
  570. if (mmc_host_is_spi(host)) {
  571. mmc_go_idle(host);
  572. err = mmc_spi_read_ocr(host, 0, &ocr);
  573. if (err)
  574. goto err;
  575. }
  576. /*
  577. * Sanity check the voltages that the card claims to
  578. * support.
  579. */
  580. if (ocr & 0x7F) {
  581. printk(KERN_WARNING "%s: card claims to support voltages "
  582. "below the defined range. These will be ignored.\n",
  583. mmc_hostname(host));
  584. ocr &= ~0x7F;
  585. }
  586. if (ocr & MMC_VDD_165_195) {
  587. printk(KERN_WARNING "%s: SD card claims to support the "
  588. "incompletely defined 'low voltage range'. This "
  589. "will be ignored.\n", mmc_hostname(host));
  590. ocr &= ~MMC_VDD_165_195;
  591. }
  592. host->ocr = mmc_select_voltage(host, ocr);
  593. /*
  594. * Can we support the voltage(s) of the card(s)?
  595. */
  596. if (!host->ocr) {
  597. err = -EINVAL;
  598. goto err;
  599. }
  600. /*
  601. * Detect and init the card.
  602. */
  603. err = mmc_sd_init_card(host, host->ocr, NULL);
  604. if (err)
  605. goto err;
  606. mmc_release_host(host);
  607. err = mmc_add_card(host->card);
  608. if (err)
  609. goto remove_card;
  610. return 0;
  611. remove_card:
  612. mmc_remove_card(host->card);
  613. host->card = NULL;
  614. mmc_claim_host(host);
  615. err:
  616. mmc_detach_bus(host);
  617. mmc_release_host(host);
  618. printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
  619. mmc_hostname(host), err);
  620. return err;
  621. }