sdio.c 6.1 KB

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