sdio.c 19 KB

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