sdio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/mmc/host.h>
  13. #include <linux/mmc/card.h>
  14. #include "core.h"
  15. #include "bus.h"
  16. #include "mmc_ops.h"
  17. #include "sd_ops.h"
  18. #include "sdio_ops.h"
  19. /*
  20. * Host is being removed. Free up the current card.
  21. */
  22. static void mmc_sdio_remove(struct mmc_host *host)
  23. {
  24. BUG_ON(!host);
  25. BUG_ON(!host->card);
  26. mmc_remove_card(host->card);
  27. host->card = NULL;
  28. }
  29. /*
  30. * Card detection callback from host.
  31. */
  32. static void mmc_sdio_detect(struct mmc_host *host)
  33. {
  34. int err;
  35. BUG_ON(!host);
  36. BUG_ON(!host->card);
  37. mmc_claim_host(host);
  38. /*
  39. * Just check if our card has been removed.
  40. */
  41. err = mmc_select_card(host->card);
  42. mmc_release_host(host);
  43. if (err) {
  44. mmc_sdio_remove(host);
  45. mmc_claim_host(host);
  46. mmc_detach_bus(host);
  47. mmc_release_host(host);
  48. }
  49. }
  50. static const struct mmc_bus_ops mmc_sdio_ops = {
  51. .remove = mmc_sdio_remove,
  52. .detect = mmc_sdio_detect,
  53. };
  54. /*
  55. * Starting point for SDIO card init.
  56. */
  57. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  58. {
  59. int err;
  60. int funcs;
  61. struct mmc_card *card;
  62. BUG_ON(!host);
  63. BUG_ON(!host->claimed);
  64. mmc_attach_bus(host, &mmc_sdio_ops);
  65. /*
  66. * Sanity check the voltages that the card claims to
  67. * support.
  68. */
  69. if (ocr & 0x7F) {
  70. printk(KERN_WARNING "%s: card claims to support voltages "
  71. "below the defined range. These will be ignored.\n",
  72. mmc_hostname(host));
  73. ocr &= ~0x7F;
  74. }
  75. if (ocr & MMC_VDD_165_195) {
  76. printk(KERN_WARNING "%s: SDIO card claims to support the "
  77. "incompletely defined 'low voltage range'. This "
  78. "will be ignored.\n", mmc_hostname(host));
  79. ocr &= ~MMC_VDD_165_195;
  80. }
  81. host->ocr = mmc_select_voltage(host, ocr);
  82. /*
  83. * Can we support the voltage(s) of the card(s)?
  84. */
  85. if (!host->ocr) {
  86. err = -EINVAL;
  87. goto err;
  88. }
  89. /*
  90. * Inform the card of the voltage
  91. */
  92. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  93. if (err)
  94. goto err;
  95. /*
  96. * The number of functions on the card is encoded inside
  97. * the ocr.
  98. */
  99. funcs = (ocr & 0x70000000) >> 28;
  100. /*
  101. * Allocate card structure.
  102. */
  103. card = mmc_alloc_card(host);
  104. if (IS_ERR(card)) {
  105. err = PTR_ERR(card);
  106. goto err;
  107. }
  108. card->type = MMC_TYPE_SDIO;
  109. /*
  110. * Set card RCA.
  111. */
  112. err = mmc_send_relative_addr(host, &card->rca);
  113. if (err)
  114. goto free_card;
  115. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  116. /*
  117. * Select card, as all following commands rely on that.
  118. */
  119. err = mmc_select_card(card);
  120. if (err)
  121. goto free_card;
  122. host->card = card;
  123. mmc_release_host(host);
  124. err = mmc_add_card(host->card);
  125. if (err)
  126. goto reclaim_host;
  127. return 0;
  128. reclaim_host:
  129. mmc_claim_host(host);
  130. free_card:
  131. mmc_remove_card(card);
  132. host->card = NULL;
  133. err:
  134. mmc_detach_bus(host);
  135. mmc_release_host(host);
  136. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  137. mmc_hostname(host), err);
  138. return err;
  139. }