sdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * linux/drivers/mmc/sdio.c
  3. *
  4. * Copyright 2006-2007 Pierre Ossman
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/sdio.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include "core.h"
  18. #include "bus.h"
  19. #include "sd.h"
  20. #include "sdio_bus.h"
  21. #include "mmc_ops.h"
  22. #include "sd_ops.h"
  23. #include "sdio_ops.h"
  24. #include "sdio_cis.h"
  25. static int sdio_read_fbr(struct sdio_func *func)
  26. {
  27. int ret;
  28. unsigned char data;
  29. ret = mmc_io_rw_direct(func->card, 0, 0,
  30. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  31. if (ret)
  32. goto out;
  33. data &= 0x0f;
  34. if (data == 0x0f) {
  35. ret = mmc_io_rw_direct(func->card, 0, 0,
  36. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  37. if (ret)
  38. goto out;
  39. }
  40. func->class = data;
  41. out:
  42. return ret;
  43. }
  44. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  45. {
  46. int ret;
  47. struct sdio_func *func;
  48. BUG_ON(fn > SDIO_MAX_FUNCS);
  49. func = sdio_alloc_func(card);
  50. if (IS_ERR(func))
  51. return PTR_ERR(func);
  52. func->num = fn;
  53. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  54. ret = sdio_read_fbr(func);
  55. if (ret)
  56. goto fail;
  57. ret = sdio_read_func_cis(func);
  58. if (ret)
  59. goto fail;
  60. } else {
  61. func->vendor = func->card->cis.vendor;
  62. func->device = func->card->cis.device;
  63. func->max_blksize = func->card->cis.blksize;
  64. }
  65. card->sdio_func[fn - 1] = func;
  66. return 0;
  67. fail:
  68. /*
  69. * It is okay to remove the function here even though we hold
  70. * the host lock as we haven't registered the device yet.
  71. */
  72. sdio_remove_func(func);
  73. return ret;
  74. }
  75. static int sdio_read_cccr(struct mmc_card *card)
  76. {
  77. int ret;
  78. int cccr_vsn;
  79. unsigned char data;
  80. memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  81. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
  82. if (ret)
  83. goto out;
  84. cccr_vsn = data & 0x0f;
  85. if (cccr_vsn > SDIO_CCCR_REV_1_20) {
  86. printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
  87. mmc_hostname(card->host), cccr_vsn);
  88. return -EINVAL;
  89. }
  90. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  91. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
  92. if (ret)
  93. goto out;
  94. if (data & SDIO_CCCR_CAP_SMB)
  95. card->cccr.multi_block = 1;
  96. if (data & SDIO_CCCR_CAP_LSC)
  97. card->cccr.low_speed = 1;
  98. if (data & SDIO_CCCR_CAP_4BLS)
  99. card->cccr.wide_bus = 1;
  100. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  101. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
  102. if (ret)
  103. goto out;
  104. if (data & SDIO_POWER_SMPC)
  105. card->cccr.high_power = 1;
  106. }
  107. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  108. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
  109. if (ret)
  110. goto out;
  111. if (data & SDIO_SPEED_SHS)
  112. card->cccr.high_speed = 1;
  113. }
  114. out:
  115. return ret;
  116. }
  117. static int sdio_enable_wide(struct mmc_card *card)
  118. {
  119. int ret;
  120. u8 ctrl;
  121. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  122. return 0;
  123. if (card->cccr.low_speed && !card->cccr.wide_bus)
  124. return 0;
  125. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  126. if (ret)
  127. return ret;
  128. ctrl |= SDIO_BUS_WIDTH_4BIT;
  129. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  130. if (ret)
  131. return ret;
  132. return 1;
  133. }
  134. /*
  135. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  136. * of the card. This may be required on certain setups of boards,
  137. * controllers and embedded sdio device which do not need the card's
  138. * pull-up. As a result, card detection is disabled and power is saved.
  139. */
  140. static int sdio_disable_cd(struct mmc_card *card)
  141. {
  142. int ret;
  143. u8 ctrl;
  144. if (!card->cccr.disable_cd)
  145. return 0;
  146. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  147. if (ret)
  148. return ret;
  149. ctrl |= SDIO_BUS_CD_DISABLE;
  150. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  151. }
  152. /*
  153. * Devices that remain active during a system suspend are
  154. * put back into 1-bit mode.
  155. */
  156. static int sdio_disable_wide(struct mmc_card *card)
  157. {
  158. int ret;
  159. u8 ctrl;
  160. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  161. return 0;
  162. if (card->cccr.low_speed && !card->cccr.wide_bus)
  163. return 0;
  164. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  165. if (ret)
  166. return ret;
  167. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  168. return 0;
  169. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  170. ctrl |= SDIO_BUS_ASYNC_INT;
  171. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  172. if (ret)
  173. return ret;
  174. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  175. return 0;
  176. }
  177. static int sdio_enable_4bit_bus(struct mmc_card *card)
  178. {
  179. int err;
  180. if (card->type == MMC_TYPE_SDIO)
  181. return sdio_enable_wide(card);
  182. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  183. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  184. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  185. if (err)
  186. return err;
  187. } else
  188. return 0;
  189. err = sdio_enable_wide(card);
  190. if (err <= 0)
  191. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  192. return err;
  193. }
  194. /*
  195. * Test if the card supports high-speed mode and, if so, switch to it.
  196. */
  197. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  198. {
  199. int ret;
  200. u8 speed;
  201. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  202. return 0;
  203. if (!card->cccr.high_speed)
  204. return 0;
  205. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  206. if (ret)
  207. return ret;
  208. if (enable)
  209. speed |= SDIO_SPEED_EHS;
  210. else
  211. speed &= ~SDIO_SPEED_EHS;
  212. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  213. if (ret)
  214. return ret;
  215. return 1;
  216. }
  217. /*
  218. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  219. */
  220. static int sdio_enable_hs(struct mmc_card *card)
  221. {
  222. int ret;
  223. ret = mmc_sdio_switch_hs(card, true);
  224. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  225. return ret;
  226. ret = mmc_sd_switch_hs(card);
  227. if (ret <= 0)
  228. mmc_sdio_switch_hs(card, false);
  229. return ret;
  230. }
  231. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  232. {
  233. unsigned max_dtr;
  234. if (mmc_card_highspeed(card)) {
  235. /*
  236. * The SDIO specification doesn't mention how
  237. * the CIS transfer speed register relates to
  238. * high-speed, but it seems that 50 MHz is
  239. * mandatory.
  240. */
  241. max_dtr = 50000000;
  242. } else {
  243. max_dtr = card->cis.max_dtr;
  244. }
  245. if (card->type == MMC_TYPE_SD_COMBO)
  246. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  247. return max_dtr;
  248. }
  249. /*
  250. * Handle the detection and initialisation of a card.
  251. *
  252. * In the case of a resume, "oldcard" will contain the card
  253. * we're trying to reinitialise.
  254. */
  255. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  256. struct mmc_card *oldcard, int powered_resume)
  257. {
  258. struct mmc_card *card;
  259. int err;
  260. BUG_ON(!host);
  261. WARN_ON(!host->claimed);
  262. /*
  263. * Inform the card of the voltage
  264. */
  265. if (!powered_resume) {
  266. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  267. if (err)
  268. goto err;
  269. }
  270. /*
  271. * For SPI, enable CRC as appropriate.
  272. */
  273. if (mmc_host_is_spi(host)) {
  274. err = mmc_spi_set_crc(host, use_spi_crc);
  275. if (err)
  276. goto err;
  277. }
  278. /*
  279. * Allocate card structure.
  280. */
  281. card = mmc_alloc_card(host, NULL);
  282. if (IS_ERR(card)) {
  283. err = PTR_ERR(card);
  284. goto err;
  285. }
  286. if (ocr & R4_MEMORY_PRESENT
  287. && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) {
  288. card->type = MMC_TYPE_SD_COMBO;
  289. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  290. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  291. mmc_remove_card(card);
  292. return -ENOENT;
  293. }
  294. } else {
  295. card->type = MMC_TYPE_SDIO;
  296. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  297. mmc_remove_card(card);
  298. return -ENOENT;
  299. }
  300. }
  301. /*
  302. * Call the optional HC's init_card function to handle quirks.
  303. */
  304. if (host->ops->init_card)
  305. host->ops->init_card(host, card);
  306. /*
  307. * For native busses: set card RCA and quit open drain mode.
  308. */
  309. if (!powered_resume && !mmc_host_is_spi(host)) {
  310. err = mmc_send_relative_addr(host, &card->rca);
  311. if (err)
  312. goto remove;
  313. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  314. }
  315. /*
  316. * Read CSD, before selecting the card
  317. */
  318. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  319. err = mmc_sd_get_csd(host, card);
  320. if (err)
  321. return err;
  322. mmc_decode_cid(card);
  323. }
  324. /*
  325. * Select card, as all following commands rely on that.
  326. */
  327. if (!powered_resume && !mmc_host_is_spi(host)) {
  328. err = mmc_select_card(card);
  329. if (err)
  330. goto remove;
  331. }
  332. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  333. /*
  334. * This is non-standard SDIO device, meaning it doesn't
  335. * have any CIA (Common I/O area) registers present.
  336. * It's host's responsibility to fill cccr and cis
  337. * structures in init_card().
  338. */
  339. mmc_set_clock(host, card->cis.max_dtr);
  340. if (card->cccr.high_speed) {
  341. mmc_card_set_highspeed(card);
  342. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  343. }
  344. goto finish;
  345. }
  346. /*
  347. * Read the common registers.
  348. */
  349. err = sdio_read_cccr(card);
  350. if (err)
  351. goto remove;
  352. /*
  353. * Read the common CIS tuples.
  354. */
  355. err = sdio_read_common_cis(card);
  356. if (err)
  357. goto remove;
  358. if (oldcard) {
  359. int same = (card->cis.vendor == oldcard->cis.vendor &&
  360. card->cis.device == oldcard->cis.device);
  361. mmc_remove_card(card);
  362. if (!same)
  363. return -ENOENT;
  364. card = oldcard;
  365. }
  366. if (card->type == MMC_TYPE_SD_COMBO) {
  367. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  368. /* handle as SDIO-only card if memory init failed */
  369. if (err) {
  370. mmc_go_idle(host);
  371. if (mmc_host_is_spi(host))
  372. /* should not fail, as it worked previously */
  373. mmc_spi_set_crc(host, use_spi_crc);
  374. card->type = MMC_TYPE_SDIO;
  375. } else
  376. card->dev.type = &sd_type;
  377. }
  378. /*
  379. * If needed, disconnect card detection pull-up resistor.
  380. */
  381. err = sdio_disable_cd(card);
  382. if (err)
  383. goto remove;
  384. /*
  385. * Switch to high-speed (if supported).
  386. */
  387. err = sdio_enable_hs(card);
  388. if (err > 0)
  389. mmc_sd_go_highspeed(card);
  390. else if (err)
  391. goto remove;
  392. /*
  393. * Change to the card's maximum speed.
  394. */
  395. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  396. /*
  397. * Switch to wider bus (if supported).
  398. */
  399. err = sdio_enable_4bit_bus(card);
  400. if (err > 0)
  401. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  402. else if (err)
  403. goto remove;
  404. finish:
  405. if (!oldcard)
  406. host->card = card;
  407. return 0;
  408. remove:
  409. if (!oldcard)
  410. mmc_remove_card(card);
  411. err:
  412. return err;
  413. }
  414. /*
  415. * Host is being removed. Free up the current card.
  416. */
  417. static void mmc_sdio_remove(struct mmc_host *host)
  418. {
  419. int i;
  420. BUG_ON(!host);
  421. BUG_ON(!host->card);
  422. for (i = 0;i < host->card->sdio_funcs;i++) {
  423. if (host->card->sdio_func[i]) {
  424. sdio_remove_func(host->card->sdio_func[i]);
  425. host->card->sdio_func[i] = NULL;
  426. }
  427. }
  428. mmc_remove_card(host->card);
  429. host->card = NULL;
  430. }
  431. /*
  432. * Card detection callback from host.
  433. */
  434. static void mmc_sdio_detect(struct mmc_host *host)
  435. {
  436. int err;
  437. BUG_ON(!host);
  438. BUG_ON(!host->card);
  439. /* Make sure card is powered before detecting it */
  440. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  441. err = pm_runtime_get_sync(&host->card->dev);
  442. if (err < 0)
  443. goto out;
  444. }
  445. mmc_claim_host(host);
  446. /*
  447. * Just check if our card has been removed.
  448. */
  449. err = mmc_select_card(host->card);
  450. mmc_release_host(host);
  451. /*
  452. * Tell PM core it's OK to power off the card now.
  453. *
  454. * The _sync variant is used in order to ensure that the card
  455. * is left powered off in case an error occurred, and the card
  456. * is going to be removed.
  457. *
  458. * Since there is no specific reason to believe a new user
  459. * is about to show up at this point, the _sync variant is
  460. * desirable anyway.
  461. */
  462. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  463. pm_runtime_put_sync(&host->card->dev);
  464. out:
  465. if (err) {
  466. mmc_sdio_remove(host);
  467. mmc_claim_host(host);
  468. mmc_detach_bus(host);
  469. mmc_release_host(host);
  470. }
  471. }
  472. /*
  473. * SDIO suspend. We need to suspend all functions separately.
  474. * Therefore all registered functions must have drivers with suspend
  475. * and resume methods. Failing that we simply remove the whole card.
  476. */
  477. static int mmc_sdio_suspend(struct mmc_host *host)
  478. {
  479. int i, err = 0;
  480. for (i = 0; i < host->card->sdio_funcs; i++) {
  481. struct sdio_func *func = host->card->sdio_func[i];
  482. if (func && sdio_func_present(func) && func->dev.driver) {
  483. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  484. if (!pmops || !pmops->suspend || !pmops->resume) {
  485. /* force removal of entire card in that case */
  486. err = -ENOSYS;
  487. } else
  488. err = pmops->suspend(&func->dev);
  489. if (err)
  490. break;
  491. }
  492. }
  493. while (err && --i >= 0) {
  494. struct sdio_func *func = host->card->sdio_func[i];
  495. if (func && sdio_func_present(func) && func->dev.driver) {
  496. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  497. pmops->resume(&func->dev);
  498. }
  499. }
  500. if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
  501. mmc_claim_host(host);
  502. sdio_disable_wide(host->card);
  503. mmc_release_host(host);
  504. }
  505. return err;
  506. }
  507. static int mmc_sdio_resume(struct mmc_host *host)
  508. {
  509. int i, err = 0;
  510. BUG_ON(!host);
  511. BUG_ON(!host->card);
  512. /* Basic card reinitialization. */
  513. mmc_claim_host(host);
  514. /* No need to reinitialize powered-resumed nonremovable cards */
  515. if (mmc_card_is_removable(host) || !mmc_card_is_powered_resumed(host))
  516. err = mmc_sdio_init_card(host, host->ocr, host->card,
  517. (host->pm_flags & MMC_PM_KEEP_POWER));
  518. else if (mmc_card_is_powered_resumed(host)) {
  519. /* We may have switched to 1-bit mode during suspend */
  520. err = sdio_enable_4bit_bus(host->card);
  521. if (err > 0) {
  522. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  523. err = 0;
  524. }
  525. }
  526. if (!err && host->sdio_irqs)
  527. mmc_signal_sdio_irq(host);
  528. mmc_release_host(host);
  529. /*
  530. * If the card looked to be the same as before suspending, then
  531. * we proceed to resume all card functions. If one of them returns
  532. * an error then we simply return that error to the core and the
  533. * card will be redetected as new. It is the responsibility of
  534. * the function driver to perform further tests with the extra
  535. * knowledge it has of the card to confirm the card is indeed the
  536. * same as before suspending (same MAC address for network cards,
  537. * etc.) and return an error otherwise.
  538. */
  539. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  540. struct sdio_func *func = host->card->sdio_func[i];
  541. if (func && sdio_func_present(func) && func->dev.driver) {
  542. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  543. err = pmops->resume(&func->dev);
  544. }
  545. }
  546. return err;
  547. }
  548. static int mmc_sdio_power_restore(struct mmc_host *host)
  549. {
  550. int ret;
  551. BUG_ON(!host);
  552. BUG_ON(!host->card);
  553. mmc_claim_host(host);
  554. ret = mmc_sdio_init_card(host, host->ocr, host->card,
  555. (host->pm_flags & MMC_PM_KEEP_POWER));
  556. if (!ret && host->sdio_irqs)
  557. mmc_signal_sdio_irq(host);
  558. mmc_release_host(host);
  559. return ret;
  560. }
  561. static const struct mmc_bus_ops mmc_sdio_ops = {
  562. .remove = mmc_sdio_remove,
  563. .detect = mmc_sdio_detect,
  564. .suspend = mmc_sdio_suspend,
  565. .resume = mmc_sdio_resume,
  566. .power_restore = mmc_sdio_power_restore,
  567. };
  568. /*
  569. * Starting point for SDIO card init.
  570. */
  571. int mmc_attach_sdio(struct mmc_host *host)
  572. {
  573. int err, i, funcs;
  574. u32 ocr;
  575. struct mmc_card *card;
  576. BUG_ON(!host);
  577. WARN_ON(!host->claimed);
  578. err = mmc_send_io_op_cond(host, 0, &ocr);
  579. if (err)
  580. return err;
  581. mmc_attach_bus(host, &mmc_sdio_ops);
  582. if (host->ocr_avail_sdio)
  583. host->ocr_avail = host->ocr_avail_sdio;
  584. /*
  585. * Sanity check the voltages that the card claims to
  586. * support.
  587. */
  588. if (ocr & 0x7F) {
  589. printk(KERN_WARNING "%s: card claims to support voltages "
  590. "below the defined range. These will be ignored.\n",
  591. mmc_hostname(host));
  592. ocr &= ~0x7F;
  593. }
  594. host->ocr = mmc_select_voltage(host, ocr);
  595. /*
  596. * Can we support the voltage(s) of the card(s)?
  597. */
  598. if (!host->ocr) {
  599. err = -EINVAL;
  600. goto err;
  601. }
  602. /*
  603. * Detect and init the card.
  604. */
  605. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  606. if (err)
  607. goto err;
  608. card = host->card;
  609. /*
  610. * Enable runtime PM only if supported by host+card+board
  611. */
  612. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  613. /*
  614. * Let runtime PM core know our card is active
  615. */
  616. err = pm_runtime_set_active(&card->dev);
  617. if (err)
  618. goto remove;
  619. /*
  620. * Enable runtime PM for this card
  621. */
  622. pm_runtime_enable(&card->dev);
  623. }
  624. /*
  625. * The number of functions on the card is encoded inside
  626. * the ocr.
  627. */
  628. funcs = (ocr & 0x70000000) >> 28;
  629. card->sdio_funcs = 0;
  630. /*
  631. * Initialize (but don't add) all present functions.
  632. */
  633. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  634. err = sdio_init_func(host->card, i + 1);
  635. if (err)
  636. goto remove;
  637. /*
  638. * Enable Runtime PM for this func (if supported)
  639. */
  640. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  641. pm_runtime_enable(&card->sdio_func[i]->dev);
  642. }
  643. /*
  644. * First add the card to the driver model...
  645. */
  646. mmc_release_host(host);
  647. err = mmc_add_card(host->card);
  648. mmc_claim_host(host);
  649. if (err)
  650. goto remove_added;
  651. /*
  652. * ...then the SDIO functions.
  653. */
  654. for (i = 0;i < funcs;i++) {
  655. err = sdio_add_func(host->card->sdio_func[i]);
  656. if (err)
  657. goto remove_added;
  658. }
  659. return 0;
  660. remove_added:
  661. /* Remove without lock if the device has been added. */
  662. mmc_release_host(host);
  663. mmc_sdio_remove(host);
  664. mmc_claim_host(host);
  665. remove:
  666. /* And with lock if it hasn't been added. */
  667. mmc_release_host(host);
  668. if (host->card)
  669. mmc_sdio_remove(host);
  670. mmc_claim_host(host);
  671. err:
  672. mmc_detach_bus(host);
  673. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  674. mmc_hostname(host), err);
  675. return err;
  676. }