mmc.c 16 KB

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