sdio.c 5.3 KB

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