sdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. /*
  314. * Update oldcard with the new RCA received from the SDIO
  315. * device -- we're doing this so that it's updated in the
  316. * "card" struct when oldcard overwrites that later.
  317. */
  318. if (oldcard)
  319. oldcard->rca = card->rca;
  320. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  321. }
  322. /*
  323. * Read CSD, before selecting the card
  324. */
  325. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  326. err = mmc_sd_get_csd(host, card);
  327. if (err)
  328. return err;
  329. mmc_decode_cid(card);
  330. }
  331. /*
  332. * Select card, as all following commands rely on that.
  333. */
  334. if (!powered_resume && !mmc_host_is_spi(host)) {
  335. err = mmc_select_card(card);
  336. if (err)
  337. goto remove;
  338. }
  339. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  340. /*
  341. * This is non-standard SDIO device, meaning it doesn't
  342. * have any CIA (Common I/O area) registers present.
  343. * It's host's responsibility to fill cccr and cis
  344. * structures in init_card().
  345. */
  346. mmc_set_clock(host, card->cis.max_dtr);
  347. if (card->cccr.high_speed) {
  348. mmc_card_set_highspeed(card);
  349. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  350. }
  351. goto finish;
  352. }
  353. /*
  354. * Read the common registers.
  355. */
  356. err = sdio_read_cccr(card);
  357. if (err)
  358. goto remove;
  359. /*
  360. * Read the common CIS tuples.
  361. */
  362. err = sdio_read_common_cis(card);
  363. if (err)
  364. goto remove;
  365. if (oldcard) {
  366. int same = (card->cis.vendor == oldcard->cis.vendor &&
  367. card->cis.device == oldcard->cis.device);
  368. mmc_remove_card(card);
  369. if (!same)
  370. return -ENOENT;
  371. card = oldcard;
  372. }
  373. mmc_fixup_device(card);
  374. if (card->type == MMC_TYPE_SD_COMBO) {
  375. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  376. /* handle as SDIO-only card if memory init failed */
  377. if (err) {
  378. mmc_go_idle(host);
  379. if (mmc_host_is_spi(host))
  380. /* should not fail, as it worked previously */
  381. mmc_spi_set_crc(host, use_spi_crc);
  382. card->type = MMC_TYPE_SDIO;
  383. } else
  384. card->dev.type = &sd_type;
  385. }
  386. /*
  387. * If needed, disconnect card detection pull-up resistor.
  388. */
  389. err = sdio_disable_cd(card);
  390. if (err)
  391. goto remove;
  392. /*
  393. * Switch to high-speed (if supported).
  394. */
  395. err = sdio_enable_hs(card);
  396. if (err > 0)
  397. mmc_sd_go_highspeed(card);
  398. else if (err)
  399. goto remove;
  400. /*
  401. * Change to the card's maximum speed.
  402. */
  403. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  404. /*
  405. * Switch to wider bus (if supported).
  406. */
  407. err = sdio_enable_4bit_bus(card);
  408. if (err > 0)
  409. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  410. else if (err)
  411. goto remove;
  412. finish:
  413. if (!oldcard)
  414. host->card = card;
  415. return 0;
  416. remove:
  417. if (!oldcard)
  418. mmc_remove_card(card);
  419. err:
  420. return err;
  421. }
  422. /*
  423. * Host is being removed. Free up the current card.
  424. */
  425. static void mmc_sdio_remove(struct mmc_host *host)
  426. {
  427. int i;
  428. BUG_ON(!host);
  429. BUG_ON(!host->card);
  430. for (i = 0;i < host->card->sdio_funcs;i++) {
  431. if (host->card->sdio_func[i]) {
  432. sdio_remove_func(host->card->sdio_func[i]);
  433. host->card->sdio_func[i] = NULL;
  434. }
  435. }
  436. mmc_remove_card(host->card);
  437. host->card = NULL;
  438. }
  439. /*
  440. * Card detection callback from host.
  441. */
  442. static void mmc_sdio_detect(struct mmc_host *host)
  443. {
  444. int err;
  445. BUG_ON(!host);
  446. BUG_ON(!host->card);
  447. /* Make sure card is powered before detecting it */
  448. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  449. err = pm_runtime_get_sync(&host->card->dev);
  450. if (err < 0)
  451. goto out;
  452. }
  453. mmc_claim_host(host);
  454. /*
  455. * Just check if our card has been removed.
  456. */
  457. err = mmc_select_card(host->card);
  458. mmc_release_host(host);
  459. /*
  460. * Tell PM core it's OK to power off the card now.
  461. *
  462. * The _sync variant is used in order to ensure that the card
  463. * is left powered off in case an error occurred, and the card
  464. * is going to be removed.
  465. *
  466. * Since there is no specific reason to believe a new user
  467. * is about to show up at this point, the _sync variant is
  468. * desirable anyway.
  469. */
  470. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  471. pm_runtime_put_sync(&host->card->dev);
  472. out:
  473. if (err) {
  474. mmc_sdio_remove(host);
  475. mmc_claim_host(host);
  476. mmc_detach_bus(host);
  477. mmc_release_host(host);
  478. }
  479. }
  480. /*
  481. * SDIO suspend. We need to suspend all functions separately.
  482. * Therefore all registered functions must have drivers with suspend
  483. * and resume methods. Failing that we simply remove the whole card.
  484. */
  485. static int mmc_sdio_suspend(struct mmc_host *host)
  486. {
  487. int i, err = 0;
  488. for (i = 0; i < host->card->sdio_funcs; i++) {
  489. struct sdio_func *func = host->card->sdio_func[i];
  490. if (func && sdio_func_present(func) && func->dev.driver) {
  491. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  492. if (!pmops || !pmops->suspend || !pmops->resume) {
  493. /* force removal of entire card in that case */
  494. err = -ENOSYS;
  495. } else
  496. err = pmops->suspend(&func->dev);
  497. if (err)
  498. break;
  499. }
  500. }
  501. while (err && --i >= 0) {
  502. struct sdio_func *func = host->card->sdio_func[i];
  503. if (func && sdio_func_present(func) && func->dev.driver) {
  504. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  505. pmops->resume(&func->dev);
  506. }
  507. }
  508. if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
  509. mmc_claim_host(host);
  510. sdio_disable_wide(host->card);
  511. mmc_release_host(host);
  512. }
  513. return err;
  514. }
  515. static int mmc_sdio_resume(struct mmc_host *host)
  516. {
  517. int i, err = 0;
  518. BUG_ON(!host);
  519. BUG_ON(!host->card);
  520. /* Basic card reinitialization. */
  521. mmc_claim_host(host);
  522. /* No need to reinitialize powered-resumed nonremovable cards */
  523. if (mmc_card_is_removable(host) || !mmc_card_is_powered_resumed(host))
  524. err = mmc_sdio_init_card(host, host->ocr, host->card,
  525. (host->pm_flags & MMC_PM_KEEP_POWER));
  526. else if (mmc_card_is_powered_resumed(host)) {
  527. /* We may have switched to 1-bit mode during suspend */
  528. err = sdio_enable_4bit_bus(host->card);
  529. if (err > 0) {
  530. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  531. err = 0;
  532. }
  533. }
  534. if (!err && host->sdio_irqs)
  535. mmc_signal_sdio_irq(host);
  536. mmc_release_host(host);
  537. /*
  538. * If the card looked to be the same as before suspending, then
  539. * we proceed to resume all card functions. If one of them returns
  540. * an error then we simply return that error to the core and the
  541. * card will be redetected as new. It is the responsibility of
  542. * the function driver to perform further tests with the extra
  543. * knowledge it has of the card to confirm the card is indeed the
  544. * same as before suspending (same MAC address for network cards,
  545. * etc.) and return an error otherwise.
  546. */
  547. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  548. struct sdio_func *func = host->card->sdio_func[i];
  549. if (func && sdio_func_present(func) && func->dev.driver) {
  550. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  551. err = pmops->resume(&func->dev);
  552. }
  553. }
  554. return err;
  555. }
  556. static int mmc_sdio_power_restore(struct mmc_host *host)
  557. {
  558. int ret;
  559. BUG_ON(!host);
  560. BUG_ON(!host->card);
  561. mmc_claim_host(host);
  562. ret = mmc_sdio_init_card(host, host->ocr, host->card,
  563. (host->pm_flags & MMC_PM_KEEP_POWER));
  564. if (!ret && host->sdio_irqs)
  565. mmc_signal_sdio_irq(host);
  566. mmc_release_host(host);
  567. return ret;
  568. }
  569. static const struct mmc_bus_ops mmc_sdio_ops = {
  570. .remove = mmc_sdio_remove,
  571. .detect = mmc_sdio_detect,
  572. .suspend = mmc_sdio_suspend,
  573. .resume = mmc_sdio_resume,
  574. .power_restore = mmc_sdio_power_restore,
  575. };
  576. /*
  577. * Starting point for SDIO card init.
  578. */
  579. int mmc_attach_sdio(struct mmc_host *host)
  580. {
  581. int err, i, funcs;
  582. u32 ocr;
  583. struct mmc_card *card;
  584. BUG_ON(!host);
  585. WARN_ON(!host->claimed);
  586. err = mmc_send_io_op_cond(host, 0, &ocr);
  587. if (err)
  588. return err;
  589. mmc_attach_bus(host, &mmc_sdio_ops);
  590. if (host->ocr_avail_sdio)
  591. host->ocr_avail = host->ocr_avail_sdio;
  592. /*
  593. * Sanity check the voltages that the card claims to
  594. * support.
  595. */
  596. if (ocr & 0x7F) {
  597. printk(KERN_WARNING "%s: card claims to support voltages "
  598. "below the defined range. These will be ignored.\n",
  599. mmc_hostname(host));
  600. ocr &= ~0x7F;
  601. }
  602. host->ocr = mmc_select_voltage(host, ocr);
  603. /*
  604. * Can we support the voltage(s) of the card(s)?
  605. */
  606. if (!host->ocr) {
  607. err = -EINVAL;
  608. goto err;
  609. }
  610. /*
  611. * Detect and init the card.
  612. */
  613. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  614. if (err)
  615. goto err;
  616. card = host->card;
  617. /*
  618. * Enable runtime PM only if supported by host+card+board
  619. */
  620. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  621. /*
  622. * Let runtime PM core know our card is active
  623. */
  624. err = pm_runtime_set_active(&card->dev);
  625. if (err)
  626. goto remove;
  627. /*
  628. * Enable runtime PM for this card
  629. */
  630. pm_runtime_enable(&card->dev);
  631. }
  632. /*
  633. * The number of functions on the card is encoded inside
  634. * the ocr.
  635. */
  636. funcs = (ocr & 0x70000000) >> 28;
  637. card->sdio_funcs = 0;
  638. /*
  639. * Initialize (but don't add) all present functions.
  640. */
  641. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  642. err = sdio_init_func(host->card, i + 1);
  643. if (err)
  644. goto remove;
  645. /*
  646. * Enable Runtime PM for this func (if supported)
  647. */
  648. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  649. pm_runtime_enable(&card->sdio_func[i]->dev);
  650. }
  651. /*
  652. * First add the card to the driver model...
  653. */
  654. mmc_release_host(host);
  655. err = mmc_add_card(host->card);
  656. if (err)
  657. goto remove_added;
  658. /*
  659. * ...then the SDIO functions.
  660. */
  661. for (i = 0;i < funcs;i++) {
  662. err = sdio_add_func(host->card->sdio_func[i]);
  663. if (err)
  664. goto remove_added;
  665. }
  666. mmc_claim_host(host);
  667. return 0;
  668. remove_added:
  669. /* Remove without lock if the device has been added. */
  670. mmc_sdio_remove(host);
  671. mmc_claim_host(host);
  672. remove:
  673. /* And with lock if it hasn't been added. */
  674. mmc_release_host(host);
  675. if (host->card)
  676. mmc_sdio_remove(host);
  677. mmc_claim_host(host);
  678. err:
  679. mmc_detach_bus(host);
  680. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  681. mmc_hostname(host), err);
  682. return err;
  683. }