sdio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <linux/mmc/sdio_func.h>
  15. #include "core.h"
  16. #include "bus.h"
  17. #include "sdio_bus.h"
  18. #include "mmc_ops.h"
  19. #include "sd_ops.h"
  20. #include "sdio_ops.h"
  21. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  22. {
  23. struct sdio_func *func;
  24. BUG_ON(fn > SDIO_MAX_FUNCS);
  25. func = sdio_alloc_func(card);
  26. if (IS_ERR(func))
  27. return PTR_ERR(func);
  28. func->num = fn;
  29. card->sdio_func[fn - 1] = func;
  30. return 0;
  31. }
  32. /*
  33. * Host is being removed. Free up the current card.
  34. */
  35. static void mmc_sdio_remove(struct mmc_host *host)
  36. {
  37. int i;
  38. BUG_ON(!host);
  39. BUG_ON(!host->card);
  40. for (i = 0;i < host->card->sdio_funcs;i++) {
  41. if (host->card->sdio_func[i]) {
  42. sdio_remove_func(host->card->sdio_func[i]);
  43. host->card->sdio_func[i] = NULL;
  44. }
  45. }
  46. mmc_remove_card(host->card);
  47. host->card = NULL;
  48. }
  49. /*
  50. * Card detection callback from host.
  51. */
  52. static void mmc_sdio_detect(struct mmc_host *host)
  53. {
  54. int err;
  55. BUG_ON(!host);
  56. BUG_ON(!host->card);
  57. mmc_claim_host(host);
  58. /*
  59. * Just check if our card has been removed.
  60. */
  61. err = mmc_select_card(host->card);
  62. mmc_release_host(host);
  63. if (err) {
  64. mmc_sdio_remove(host);
  65. mmc_claim_host(host);
  66. mmc_detach_bus(host);
  67. mmc_release_host(host);
  68. }
  69. }
  70. static const struct mmc_bus_ops mmc_sdio_ops = {
  71. .remove = mmc_sdio_remove,
  72. .detect = mmc_sdio_detect,
  73. };
  74. /*
  75. * Starting point for SDIO card init.
  76. */
  77. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  78. {
  79. int err;
  80. int i, funcs;
  81. struct mmc_card *card;
  82. BUG_ON(!host);
  83. BUG_ON(!host->claimed);
  84. mmc_attach_bus(host, &mmc_sdio_ops);
  85. /*
  86. * Sanity check the voltages that the card claims to
  87. * support.
  88. */
  89. if (ocr & 0x7F) {
  90. printk(KERN_WARNING "%s: card claims to support voltages "
  91. "below the defined range. These will be ignored.\n",
  92. mmc_hostname(host));
  93. ocr &= ~0x7F;
  94. }
  95. if (ocr & MMC_VDD_165_195) {
  96. printk(KERN_WARNING "%s: SDIO card claims to support the "
  97. "incompletely defined 'low voltage range'. This "
  98. "will be ignored.\n", mmc_hostname(host));
  99. ocr &= ~MMC_VDD_165_195;
  100. }
  101. host->ocr = mmc_select_voltage(host, ocr);
  102. /*
  103. * Can we support the voltage(s) of the card(s)?
  104. */
  105. if (!host->ocr) {
  106. err = -EINVAL;
  107. goto err;
  108. }
  109. /*
  110. * Inform the card of the voltage
  111. */
  112. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  113. if (err)
  114. goto err;
  115. /*
  116. * The number of functions on the card is encoded inside
  117. * the ocr.
  118. */
  119. funcs = (ocr & 0x70000000) >> 28;
  120. /*
  121. * Allocate card structure.
  122. */
  123. card = mmc_alloc_card(host);
  124. if (IS_ERR(card)) {
  125. err = PTR_ERR(card);
  126. goto err;
  127. }
  128. card->type = MMC_TYPE_SDIO;
  129. card->sdio_funcs = funcs;
  130. host->card = card;
  131. /*
  132. * Set card RCA.
  133. */
  134. err = mmc_send_relative_addr(host, &card->rca);
  135. if (err)
  136. goto remove;
  137. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  138. /*
  139. * Select card, as all following commands rely on that.
  140. */
  141. err = mmc_select_card(card);
  142. if (err)
  143. goto remove;
  144. /*
  145. * Initialize (but don't add) all present functions.
  146. */
  147. for (i = 0;i < funcs;i++) {
  148. err = sdio_init_func(host->card, i + 1);
  149. if (err)
  150. goto remove;
  151. }
  152. mmc_release_host(host);
  153. /*
  154. * First add the card to the driver model...
  155. */
  156. err = mmc_add_card(host->card);
  157. if (err)
  158. goto remove_added;
  159. /*
  160. * ...then the SDIO functions.
  161. */
  162. for (i = 0;i < funcs;i++) {
  163. err = sdio_add_func(host->card->sdio_func[i]);
  164. if (err)
  165. goto remove_added;
  166. }
  167. return 0;
  168. remove_added:
  169. /* Remove without lock if the device has been added. */
  170. mmc_sdio_remove(host);
  171. mmc_claim_host(host);
  172. remove:
  173. /* And with lock if it hasn't been added. */
  174. if (host->card)
  175. mmc_sdio_remove(host);
  176. err:
  177. mmc_detach_bus(host);
  178. mmc_release_host(host);
  179. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  180. mmc_hostname(host), err);
  181. return err;
  182. }