/drivers/mmc/core/sdio.c

https://bitbucket.org/ndreys/linux-sunxi · C · 1000 lines · 626 code · 176 blank · 198 comment · 179 complexity · 5aae0dca7fbb4f5dbcab6dab387d473d MD5 · raw file

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