sdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 "sd.h"
  19. #include "sdio_bus.h"
  20. #include "mmc_ops.h"
  21. #include "sd_ops.h"
  22. #include "sdio_ops.h"
  23. #include "sdio_cis.h"
  24. static int sdio_read_fbr(struct sdio_func *func)
  25. {
  26. int ret;
  27. unsigned char data;
  28. ret = mmc_io_rw_direct(func->card, 0, 0,
  29. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  30. if (ret)
  31. goto out;
  32. data &= 0x0f;
  33. if (data == 0x0f) {
  34. ret = mmc_io_rw_direct(func->card, 0, 0,
  35. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  36. if (ret)
  37. goto out;
  38. }
  39. func->class = data;
  40. out:
  41. return ret;
  42. }
  43. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  44. {
  45. int ret;
  46. struct sdio_func *func;
  47. BUG_ON(fn > SDIO_MAX_FUNCS);
  48. func = sdio_alloc_func(card);
  49. if (IS_ERR(func))
  50. return PTR_ERR(func);
  51. func->num = fn;
  52. ret = sdio_read_fbr(func);
  53. if (ret)
  54. goto fail;
  55. ret = sdio_read_func_cis(func);
  56. if (ret)
  57. goto fail;
  58. card->sdio_func[fn - 1] = func;
  59. return 0;
  60. fail:
  61. /*
  62. * It is okay to remove the function here even though we hold
  63. * the host lock as we haven't registered the device yet.
  64. */
  65. sdio_remove_func(func);
  66. return ret;
  67. }
  68. static int sdio_read_cccr(struct mmc_card *card)
  69. {
  70. int ret;
  71. int cccr_vsn;
  72. unsigned char data;
  73. memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  74. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
  75. if (ret)
  76. goto out;
  77. cccr_vsn = data & 0x0f;
  78. if (cccr_vsn > SDIO_CCCR_REV_1_20) {
  79. printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
  80. mmc_hostname(card->host), cccr_vsn);
  81. return -EINVAL;
  82. }
  83. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  84. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
  85. if (ret)
  86. goto out;
  87. if (data & SDIO_CCCR_CAP_SMB)
  88. card->cccr.multi_block = 1;
  89. if (data & SDIO_CCCR_CAP_LSC)
  90. card->cccr.low_speed = 1;
  91. if (data & SDIO_CCCR_CAP_4BLS)
  92. card->cccr.wide_bus = 1;
  93. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  94. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
  95. if (ret)
  96. goto out;
  97. if (data & SDIO_POWER_SMPC)
  98. card->cccr.high_power = 1;
  99. }
  100. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  101. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
  102. if (ret)
  103. goto out;
  104. if (data & SDIO_SPEED_SHS)
  105. card->cccr.high_speed = 1;
  106. }
  107. out:
  108. return ret;
  109. }
  110. static int sdio_enable_wide(struct mmc_card *card)
  111. {
  112. int ret;
  113. u8 ctrl;
  114. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  115. return 0;
  116. if (card->cccr.low_speed && !card->cccr.wide_bus)
  117. return 0;
  118. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  119. if (ret)
  120. return ret;
  121. ctrl |= SDIO_BUS_WIDTH_4BIT;
  122. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  123. if (ret)
  124. return ret;
  125. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  126. return 0;
  127. }
  128. /*
  129. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  130. * of the card. This may be required on certain setups of boards,
  131. * controllers and embedded sdio device which do not need the card's
  132. * pull-up. As a result, card detection is disabled and power is saved.
  133. */
  134. static int sdio_disable_cd(struct mmc_card *card)
  135. {
  136. int ret;
  137. u8 ctrl;
  138. if (!card->cccr.disable_cd)
  139. return 0;
  140. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  141. if (ret)
  142. return ret;
  143. ctrl |= SDIO_BUS_CD_DISABLE;
  144. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  145. }
  146. /*
  147. * Devices that remain active during a system suspend are
  148. * put back into 1-bit mode.
  149. */
  150. static int sdio_disable_wide(struct mmc_card *card)
  151. {
  152. int ret;
  153. u8 ctrl;
  154. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  155. return 0;
  156. if (card->cccr.low_speed && !card->cccr.wide_bus)
  157. return 0;
  158. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  159. if (ret)
  160. return ret;
  161. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  162. return 0;
  163. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  164. ctrl |= SDIO_BUS_ASYNC_INT;
  165. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  166. if (ret)
  167. return ret;
  168. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  169. return 0;
  170. }
  171. /*
  172. * Test if the card supports high-speed mode and, if so, switch to it.
  173. */
  174. static int sdio_enable_hs(struct mmc_card *card)
  175. {
  176. int ret;
  177. u8 speed;
  178. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  179. return 0;
  180. if (!card->cccr.high_speed)
  181. return 0;
  182. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  183. if (ret)
  184. return ret;
  185. speed |= SDIO_SPEED_EHS;
  186. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  187. if (ret)
  188. return ret;
  189. return 1;
  190. }
  191. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  192. {
  193. unsigned max_dtr;
  194. if (mmc_card_highspeed(card)) {
  195. /*
  196. * The SDIO specification doesn't mention how
  197. * the CIS transfer speed register relates to
  198. * high-speed, but it seems that 50 MHz is
  199. * mandatory.
  200. */
  201. max_dtr = 50000000;
  202. } else {
  203. max_dtr = card->cis.max_dtr;
  204. }
  205. return max_dtr;
  206. }
  207. /*
  208. * Handle the detection and initialisation of a card.
  209. *
  210. * In the case of a resume, "oldcard" will contain the card
  211. * we're trying to reinitialise.
  212. */
  213. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  214. struct mmc_card *oldcard, int powered_resume)
  215. {
  216. struct mmc_card *card;
  217. int err;
  218. BUG_ON(!host);
  219. WARN_ON(!host->claimed);
  220. /*
  221. * Inform the card of the voltage
  222. */
  223. if (!powered_resume) {
  224. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  225. if (err)
  226. goto err;
  227. }
  228. /*
  229. * For SPI, enable CRC as appropriate.
  230. */
  231. if (mmc_host_is_spi(host)) {
  232. err = mmc_spi_set_crc(host, use_spi_crc);
  233. if (err)
  234. goto err;
  235. }
  236. /*
  237. * Allocate card structure.
  238. */
  239. card = mmc_alloc_card(host, NULL);
  240. if (IS_ERR(card)) {
  241. err = PTR_ERR(card);
  242. goto err;
  243. }
  244. card->type = MMC_TYPE_SDIO;
  245. /*
  246. * Call the optional HC's init_card function to handle quirks.
  247. */
  248. if (host->ops->init_card)
  249. host->ops->init_card(host, card);
  250. /*
  251. * For native busses: set card RCA and quit open drain mode.
  252. */
  253. if (!powered_resume && !mmc_host_is_spi(host)) {
  254. err = mmc_send_relative_addr(host, &card->rca);
  255. if (err)
  256. goto remove;
  257. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  258. }
  259. /*
  260. * Select card, as all following commands rely on that.
  261. */
  262. if (!powered_resume && !mmc_host_is_spi(host)) {
  263. err = mmc_select_card(card);
  264. if (err)
  265. goto remove;
  266. }
  267. /*
  268. * Read the common registers.
  269. */
  270. err = sdio_read_cccr(card);
  271. if (err)
  272. goto remove;
  273. /*
  274. * Read the common CIS tuples.
  275. */
  276. err = sdio_read_common_cis(card);
  277. if (err)
  278. goto remove;
  279. if (oldcard) {
  280. int same = (card->cis.vendor == oldcard->cis.vendor &&
  281. card->cis.device == oldcard->cis.device);
  282. mmc_remove_card(card);
  283. if (!same) {
  284. err = -ENOENT;
  285. goto err;
  286. }
  287. card = oldcard;
  288. return 0;
  289. }
  290. /*
  291. * Switch to high-speed (if supported).
  292. */
  293. err = sdio_enable_hs(card);
  294. if (err > 0)
  295. mmc_sd_go_highspeed(card);
  296. else if (err)
  297. goto remove;
  298. /*
  299. * Change to the card's maximum speed.
  300. */
  301. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  302. /*
  303. * Switch to wider bus (if supported).
  304. */
  305. err = sdio_enable_wide(card);
  306. if (err)
  307. goto remove;
  308. if (!oldcard)
  309. host->card = card;
  310. return 0;
  311. remove:
  312. if (!oldcard)
  313. mmc_remove_card(card);
  314. err:
  315. return err;
  316. }
  317. /*
  318. * Host is being removed. Free up the current card.
  319. */
  320. static void mmc_sdio_remove(struct mmc_host *host)
  321. {
  322. int i;
  323. BUG_ON(!host);
  324. BUG_ON(!host->card);
  325. for (i = 0;i < host->card->sdio_funcs;i++) {
  326. if (host->card->sdio_func[i]) {
  327. sdio_remove_func(host->card->sdio_func[i]);
  328. host->card->sdio_func[i] = NULL;
  329. }
  330. }
  331. mmc_remove_card(host->card);
  332. host->card = NULL;
  333. }
  334. /*
  335. * Card detection callback from host.
  336. */
  337. static void mmc_sdio_detect(struct mmc_host *host)
  338. {
  339. int err;
  340. BUG_ON(!host);
  341. BUG_ON(!host->card);
  342. mmc_claim_host(host);
  343. /*
  344. * Just check if our card has been removed.
  345. */
  346. err = mmc_select_card(host->card);
  347. mmc_release_host(host);
  348. if (err) {
  349. mmc_sdio_remove(host);
  350. mmc_claim_host(host);
  351. mmc_detach_bus(host);
  352. mmc_release_host(host);
  353. }
  354. }
  355. /*
  356. * SDIO suspend. We need to suspend all functions separately.
  357. * Therefore all registered functions must have drivers with suspend
  358. * and resume methods. Failing that we simply remove the whole card.
  359. */
  360. static int mmc_sdio_suspend(struct mmc_host *host)
  361. {
  362. int i, err = 0;
  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. if (!pmops || !pmops->suspend || !pmops->resume) {
  368. /* force removal of entire card in that case */
  369. err = -ENOSYS;
  370. } else
  371. err = pmops->suspend(&func->dev);
  372. if (err)
  373. break;
  374. }
  375. }
  376. while (err && --i >= 0) {
  377. struct sdio_func *func = host->card->sdio_func[i];
  378. if (func && sdio_func_present(func) && func->dev.driver) {
  379. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  380. pmops->resume(&func->dev);
  381. }
  382. }
  383. if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
  384. mmc_claim_host(host);
  385. sdio_disable_wide(host->card);
  386. mmc_release_host(host);
  387. }
  388. return err;
  389. }
  390. static int mmc_sdio_resume(struct mmc_host *host)
  391. {
  392. int i, err;
  393. BUG_ON(!host);
  394. BUG_ON(!host->card);
  395. /* Basic card reinitialization. */
  396. mmc_claim_host(host);
  397. err = mmc_sdio_init_card(host, host->ocr, host->card,
  398. (host->pm_flags & MMC_PM_KEEP_POWER));
  399. if (!err)
  400. /* We may have switched to 1-bit mode during suspend. */
  401. err = sdio_enable_wide(host->card);
  402. if (!err && host->sdio_irqs)
  403. mmc_signal_sdio_irq(host);
  404. mmc_release_host(host);
  405. /*
  406. * If the card looked to be the same as before suspending, then
  407. * we proceed to resume all card functions. If one of them returns
  408. * an error then we simply return that error to the core and the
  409. * card will be redetected as new. It is the responsibility of
  410. * the function driver to perform further tests with the extra
  411. * knowledge it has of the card to confirm the card is indeed the
  412. * same as before suspending (same MAC address for network cards,
  413. * etc.) and return an error otherwise.
  414. */
  415. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  416. struct sdio_func *func = host->card->sdio_func[i];
  417. if (func && sdio_func_present(func) && func->dev.driver) {
  418. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  419. err = pmops->resume(&func->dev);
  420. }
  421. }
  422. return err;
  423. }
  424. static const struct mmc_bus_ops mmc_sdio_ops = {
  425. .remove = mmc_sdio_remove,
  426. .detect = mmc_sdio_detect,
  427. .suspend = mmc_sdio_suspend,
  428. .resume = mmc_sdio_resume,
  429. };
  430. /*
  431. * Starting point for SDIO card init.
  432. */
  433. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  434. {
  435. int err;
  436. int i, funcs;
  437. struct mmc_card *card;
  438. BUG_ON(!host);
  439. WARN_ON(!host->claimed);
  440. mmc_attach_bus(host, &mmc_sdio_ops);
  441. /*
  442. * Sanity check the voltages that the card claims to
  443. * support.
  444. */
  445. if (ocr & 0x7F) {
  446. printk(KERN_WARNING "%s: card claims to support voltages "
  447. "below the defined range. These will be ignored.\n",
  448. mmc_hostname(host));
  449. ocr &= ~0x7F;
  450. }
  451. host->ocr = mmc_select_voltage(host, ocr);
  452. /*
  453. * Can we support the voltage(s) of the card(s)?
  454. */
  455. if (!host->ocr) {
  456. err = -EINVAL;
  457. goto err;
  458. }
  459. /*
  460. * Detect and init the card.
  461. */
  462. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  463. if (err)
  464. goto err;
  465. card = host->card;
  466. /*
  467. * The number of functions on the card is encoded inside
  468. * the ocr.
  469. */
  470. funcs = (ocr & 0x70000000) >> 28;
  471. card->sdio_funcs = 0;
  472. /*
  473. * If needed, disconnect card detection pull-up resistor.
  474. */
  475. err = sdio_disable_cd(card);
  476. if (err)
  477. goto remove;
  478. /*
  479. * Initialize (but don't add) all present functions.
  480. */
  481. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  482. err = sdio_init_func(host->card, i + 1);
  483. if (err)
  484. goto remove;
  485. }
  486. mmc_release_host(host);
  487. /*
  488. * First add the card to the driver model...
  489. */
  490. err = mmc_add_card(host->card);
  491. if (err)
  492. goto remove_added;
  493. /*
  494. * ...then the SDIO functions.
  495. */
  496. for (i = 0;i < funcs;i++) {
  497. err = sdio_add_func(host->card->sdio_func[i]);
  498. if (err)
  499. goto remove_added;
  500. }
  501. return 0;
  502. remove_added:
  503. /* Remove without lock if the device has been added. */
  504. mmc_sdio_remove(host);
  505. mmc_claim_host(host);
  506. remove:
  507. /* And with lock if it hasn't been added. */
  508. if (host->card)
  509. mmc_sdio_remove(host);
  510. err:
  511. mmc_detach_bus(host);
  512. mmc_release_host(host);
  513. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  514. mmc_hostname(host), err);
  515. return err;
  516. }