sdio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. SDIO_FBR_BASE(func->num) + 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. SDIO_FBR_BASE(func->num) + 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. static int sdio_enable_wide(struct mmc_card *card)
  110. {
  111. int ret;
  112. u8 ctrl;
  113. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  114. return 0;
  115. if (card->cccr.low_speed && !card->cccr.wide_bus)
  116. return 0;
  117. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  118. if (ret)
  119. return ret;
  120. ctrl |= SDIO_BUS_WIDTH_4BIT;
  121. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  122. if (ret)
  123. return ret;
  124. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  125. return 0;
  126. }
  127. /*
  128. * Host is being removed. Free up the current card.
  129. */
  130. static void mmc_sdio_remove(struct mmc_host *host)
  131. {
  132. int i;
  133. BUG_ON(!host);
  134. BUG_ON(!host->card);
  135. for (i = 0;i < host->card->sdio_funcs;i++) {
  136. if (host->card->sdio_func[i]) {
  137. sdio_remove_func(host->card->sdio_func[i]);
  138. host->card->sdio_func[i] = NULL;
  139. }
  140. }
  141. mmc_remove_card(host->card);
  142. host->card = NULL;
  143. }
  144. /*
  145. * Card detection callback from host.
  146. */
  147. static void mmc_sdio_detect(struct mmc_host *host)
  148. {
  149. int err;
  150. BUG_ON(!host);
  151. BUG_ON(!host->card);
  152. mmc_claim_host(host);
  153. /*
  154. * Just check if our card has been removed.
  155. */
  156. err = mmc_select_card(host->card);
  157. mmc_release_host(host);
  158. if (err) {
  159. mmc_sdio_remove(host);
  160. mmc_claim_host(host);
  161. mmc_detach_bus(host);
  162. mmc_release_host(host);
  163. }
  164. }
  165. static const struct mmc_bus_ops mmc_sdio_ops = {
  166. .remove = mmc_sdio_remove,
  167. .detect = mmc_sdio_detect,
  168. };
  169. /*
  170. * Starting point for SDIO card init.
  171. */
  172. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  173. {
  174. int err;
  175. int i, funcs;
  176. struct mmc_card *card;
  177. BUG_ON(!host);
  178. WARN_ON(!host->claimed);
  179. mmc_attach_bus(host, &mmc_sdio_ops);
  180. /*
  181. * Sanity check the voltages that the card claims to
  182. * support.
  183. */
  184. if (ocr & 0x7F) {
  185. printk(KERN_WARNING "%s: card claims to support voltages "
  186. "below the defined range. These will be ignored.\n",
  187. mmc_hostname(host));
  188. ocr &= ~0x7F;
  189. }
  190. if (ocr & MMC_VDD_165_195) {
  191. printk(KERN_WARNING "%s: SDIO card claims to support the "
  192. "incompletely defined 'low voltage range'. This "
  193. "will be ignored.\n", mmc_hostname(host));
  194. ocr &= ~MMC_VDD_165_195;
  195. }
  196. host->ocr = mmc_select_voltage(host, ocr);
  197. /*
  198. * Can we support the voltage(s) of the card(s)?
  199. */
  200. if (!host->ocr) {
  201. err = -EINVAL;
  202. goto err;
  203. }
  204. /*
  205. * Inform the card of the voltage
  206. */
  207. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  208. if (err)
  209. goto err;
  210. /*
  211. * For SPI, enable CRC as appropriate.
  212. */
  213. if (mmc_host_is_spi(host)) {
  214. err = mmc_spi_set_crc(host, use_spi_crc);
  215. if (err)
  216. goto err;
  217. }
  218. /*
  219. * The number of functions on the card is encoded inside
  220. * the ocr.
  221. */
  222. funcs = (ocr & 0x70000000) >> 28;
  223. /*
  224. * Allocate card structure.
  225. */
  226. card = mmc_alloc_card(host, NULL);
  227. if (IS_ERR(card)) {
  228. err = PTR_ERR(card);
  229. goto err;
  230. }
  231. card->type = MMC_TYPE_SDIO;
  232. card->sdio_funcs = funcs;
  233. host->card = card;
  234. /*
  235. * For native busses: set card RCA and quit open drain mode.
  236. */
  237. if (!mmc_host_is_spi(host)) {
  238. err = mmc_send_relative_addr(host, &card->rca);
  239. if (err)
  240. goto remove;
  241. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  242. }
  243. /*
  244. * Select card, as all following commands rely on that.
  245. */
  246. if (!mmc_host_is_spi(host)) {
  247. err = mmc_select_card(card);
  248. if (err)
  249. goto remove;
  250. }
  251. /*
  252. * Read the common registers.
  253. */
  254. err = sdio_read_cccr(card);
  255. if (err)
  256. goto remove;
  257. /*
  258. * Read the common CIS tuples.
  259. */
  260. err = sdio_read_common_cis(card);
  261. if (err)
  262. goto remove;
  263. /*
  264. * No support for high-speed yet, so just set
  265. * the card's maximum speed.
  266. */
  267. mmc_set_clock(host, card->cis.max_dtr);
  268. /*
  269. * Switch to wider bus (if supported).
  270. */
  271. err = sdio_enable_wide(card);
  272. if (err)
  273. goto remove;
  274. /*
  275. * Initialize (but don't add) all present functions.
  276. */
  277. for (i = 0;i < funcs;i++) {
  278. err = sdio_init_func(host->card, i + 1);
  279. if (err)
  280. goto remove;
  281. }
  282. mmc_release_host(host);
  283. /*
  284. * First add the card to the driver model...
  285. */
  286. err = mmc_add_card(host->card);
  287. if (err)
  288. goto remove_added;
  289. /*
  290. * ...then the SDIO functions.
  291. */
  292. for (i = 0;i < funcs;i++) {
  293. err = sdio_add_func(host->card->sdio_func[i]);
  294. if (err)
  295. goto remove_added;
  296. }
  297. return 0;
  298. remove_added:
  299. /* Remove without lock if the device has been added. */
  300. mmc_sdio_remove(host);
  301. mmc_claim_host(host);
  302. remove:
  303. /* And with lock if it hasn't been added. */
  304. if (host->card)
  305. mmc_sdio_remove(host);
  306. err:
  307. mmc_detach_bus(host);
  308. mmc_release_host(host);
  309. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  310. mmc_hostname(host), err);
  311. return err;
  312. }