sdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  129. * of the card. This may be required on certain setups of boards,
  130. * controllers and embedded sdio device which do not need the card's
  131. * pull-up. As a result, card detection is disabled and power is saved.
  132. */
  133. static int sdio_disable_cd(struct mmc_card *card)
  134. {
  135. int ret;
  136. u8 ctrl;
  137. if (!card->cccr.disable_cd)
  138. return 0;
  139. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  140. if (ret)
  141. return ret;
  142. ctrl |= SDIO_BUS_CD_DISABLE;
  143. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  144. }
  145. /*
  146. * Test if the card supports high-speed mode and, if so, switch to it.
  147. */
  148. static int sdio_enable_hs(struct mmc_card *card)
  149. {
  150. int ret;
  151. u8 speed;
  152. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  153. return 0;
  154. if (!card->cccr.high_speed)
  155. return 0;
  156. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  157. if (ret)
  158. return ret;
  159. speed |= SDIO_SPEED_EHS;
  160. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  161. if (ret)
  162. return ret;
  163. mmc_card_set_highspeed(card);
  164. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  165. return 0;
  166. }
  167. /*
  168. * Handle the detection and initialisation of a card.
  169. *
  170. * In the case of a resume, "oldcard" will contain the card
  171. * we're trying to reinitialise.
  172. */
  173. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  174. struct mmc_card *oldcard)
  175. {
  176. struct mmc_card *card;
  177. int err;
  178. BUG_ON(!host);
  179. WARN_ON(!host->claimed);
  180. /*
  181. * Inform the card of the voltage
  182. */
  183. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  184. if (err)
  185. goto err;
  186. /*
  187. * For SPI, enable CRC as appropriate.
  188. */
  189. if (mmc_host_is_spi(host)) {
  190. err = mmc_spi_set_crc(host, use_spi_crc);
  191. if (err)
  192. goto err;
  193. }
  194. /*
  195. * Allocate card structure.
  196. */
  197. card = mmc_alloc_card(host, NULL);
  198. if (IS_ERR(card)) {
  199. err = PTR_ERR(card);
  200. goto err;
  201. }
  202. card->type = MMC_TYPE_SDIO;
  203. /*
  204. * For native busses: set card RCA and quit open drain mode.
  205. */
  206. if (!mmc_host_is_spi(host)) {
  207. err = mmc_send_relative_addr(host, &card->rca);
  208. if (err)
  209. goto remove;
  210. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  211. }
  212. /*
  213. * Select card, as all following commands rely on that.
  214. */
  215. if (!mmc_host_is_spi(host)) {
  216. err = mmc_select_card(card);
  217. if (err)
  218. goto remove;
  219. }
  220. /*
  221. * Read the common registers.
  222. */
  223. err = sdio_read_cccr(card);
  224. if (err)
  225. goto remove;
  226. /*
  227. * Read the common CIS tuples.
  228. */
  229. err = sdio_read_common_cis(card);
  230. if (err)
  231. goto remove;
  232. if (oldcard) {
  233. int same = (card->cis.vendor == oldcard->cis.vendor &&
  234. card->cis.device == oldcard->cis.device);
  235. mmc_remove_card(card);
  236. if (!same) {
  237. err = -ENOENT;
  238. goto err;
  239. }
  240. card = oldcard;
  241. }
  242. /*
  243. * Switch to high-speed (if supported).
  244. */
  245. err = sdio_enable_hs(card);
  246. if (err)
  247. goto remove;
  248. /*
  249. * Change to the card's maximum speed.
  250. */
  251. if (mmc_card_highspeed(card)) {
  252. /*
  253. * The SDIO specification doesn't mention how
  254. * the CIS transfer speed register relates to
  255. * high-speed, but it seems that 50 MHz is
  256. * mandatory.
  257. */
  258. mmc_set_clock(host, 50000000);
  259. } else {
  260. mmc_set_clock(host, card->cis.max_dtr);
  261. }
  262. /*
  263. * Switch to wider bus (if supported).
  264. */
  265. err = sdio_enable_wide(card);
  266. if (err)
  267. goto remove;
  268. if (!oldcard)
  269. host->card = card;
  270. return 0;
  271. remove:
  272. if (!oldcard)
  273. mmc_remove_card(card);
  274. err:
  275. return err;
  276. }
  277. /*
  278. * Host is being removed. Free up the current card.
  279. */
  280. static void mmc_sdio_remove(struct mmc_host *host)
  281. {
  282. int i;
  283. BUG_ON(!host);
  284. BUG_ON(!host->card);
  285. for (i = 0;i < host->card->sdio_funcs;i++) {
  286. if (host->card->sdio_func[i]) {
  287. sdio_remove_func(host->card->sdio_func[i]);
  288. host->card->sdio_func[i] = NULL;
  289. }
  290. }
  291. mmc_remove_card(host->card);
  292. host->card = NULL;
  293. }
  294. /*
  295. * Card detection callback from host.
  296. */
  297. static void mmc_sdio_detect(struct mmc_host *host)
  298. {
  299. int err;
  300. BUG_ON(!host);
  301. BUG_ON(!host->card);
  302. mmc_claim_host(host);
  303. /*
  304. * Just check if our card has been removed.
  305. */
  306. err = mmc_select_card(host->card);
  307. mmc_release_host(host);
  308. if (err) {
  309. mmc_sdio_remove(host);
  310. mmc_claim_host(host);
  311. mmc_detach_bus(host);
  312. mmc_release_host(host);
  313. }
  314. }
  315. /*
  316. * SDIO suspend. We need to suspend all functions separately.
  317. * Therefore all registered functions must have drivers with suspend
  318. * and resume methods. Failing that we simply remove the whole card.
  319. */
  320. static void mmc_sdio_suspend(struct mmc_host *host)
  321. {
  322. int i;
  323. /* make sure all registered functions can suspend/resume */
  324. for (i = 0; i < host->card->sdio_funcs; i++) {
  325. struct sdio_func *func = host->card->sdio_func[i];
  326. if (func && sdio_func_present(func) && func->dev.driver) {
  327. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  328. if (!pmops || !pmops->suspend || !pmops->resume) {
  329. /* just remove the entire card in that case */
  330. mmc_sdio_remove(host);
  331. mmc_claim_host(host);
  332. mmc_detach_bus(host);
  333. mmc_release_host(host);
  334. return;
  335. }
  336. }
  337. }
  338. /* now suspend them */
  339. for (i = 0; i < host->card->sdio_funcs; i++) {
  340. struct sdio_func *func = host->card->sdio_func[i];
  341. if (func && sdio_func_present(func) && func->dev.driver) {
  342. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  343. pmops->suspend(&func->dev);
  344. }
  345. }
  346. }
  347. static void mmc_sdio_resume(struct mmc_host *host)
  348. {
  349. int i, err;
  350. BUG_ON(!host);
  351. BUG_ON(!host->card);
  352. mmc_claim_host(host);
  353. err = mmc_sdio_init_card(host, host->ocr, host->card);
  354. mmc_release_host(host);
  355. if (err) {
  356. mmc_sdio_remove(host);
  357. mmc_claim_host(host);
  358. mmc_detach_bus(host);
  359. mmc_release_host(host);
  360. return;
  361. }
  362. /* resume all functions */
  363. for (i = 0; i < host->card->sdio_funcs; i++) {
  364. struct sdio_func *func = host->card->sdio_func[i];
  365. if (func && sdio_func_present(func) && func->dev.driver) {
  366. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  367. pmops->resume(&func->dev);
  368. }
  369. }
  370. }
  371. static const struct mmc_bus_ops mmc_sdio_ops = {
  372. .remove = mmc_sdio_remove,
  373. .detect = mmc_sdio_detect,
  374. .suspend = mmc_sdio_suspend,
  375. .resume = mmc_sdio_resume,
  376. };
  377. /*
  378. * Starting point for SDIO card init.
  379. */
  380. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  381. {
  382. int err;
  383. int i, funcs;
  384. struct mmc_card *card;
  385. BUG_ON(!host);
  386. WARN_ON(!host->claimed);
  387. mmc_attach_bus(host, &mmc_sdio_ops);
  388. /*
  389. * Sanity check the voltages that the card claims to
  390. * support.
  391. */
  392. if (ocr & 0x7F) {
  393. printk(KERN_WARNING "%s: card claims to support voltages "
  394. "below the defined range. These will be ignored.\n",
  395. mmc_hostname(host));
  396. ocr &= ~0x7F;
  397. }
  398. host->ocr = mmc_select_voltage(host, ocr);
  399. /*
  400. * Can we support the voltage(s) of the card(s)?
  401. */
  402. if (!host->ocr) {
  403. err = -EINVAL;
  404. goto err;
  405. }
  406. /*
  407. * Detect and init the card.
  408. */
  409. err = mmc_sdio_init_card(host, host->ocr, NULL);
  410. if (err)
  411. goto err;
  412. card = host->card;
  413. /*
  414. * The number of functions on the card is encoded inside
  415. * the ocr.
  416. */
  417. card->sdio_funcs = funcs = (ocr & 0x70000000) >> 28;
  418. /*
  419. * If needed, disconnect card detection pull-up resistor.
  420. */
  421. err = sdio_disable_cd(card);
  422. if (err)
  423. goto remove;
  424. /*
  425. * Initialize (but don't add) all present functions.
  426. */
  427. for (i = 0;i < funcs;i++) {
  428. err = sdio_init_func(host->card, i + 1);
  429. if (err)
  430. goto remove;
  431. }
  432. mmc_release_host(host);
  433. /*
  434. * First add the card to the driver model...
  435. */
  436. err = mmc_add_card(host->card);
  437. if (err)
  438. goto remove_added;
  439. /*
  440. * ...then the SDIO functions.
  441. */
  442. for (i = 0;i < funcs;i++) {
  443. err = sdio_add_func(host->card->sdio_func[i]);
  444. if (err)
  445. goto remove_added;
  446. }
  447. return 0;
  448. remove_added:
  449. /* Remove without lock if the device has been added. */
  450. mmc_sdio_remove(host);
  451. mmc_claim_host(host);
  452. remove:
  453. /* And with lock if it hasn't been added. */
  454. if (host->card)
  455. mmc_sdio_remove(host);
  456. err:
  457. mmc_detach_bus(host);
  458. mmc_release_host(host);
  459. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  460. mmc_hostname(host), err);
  461. return err;
  462. }