sdio.c 12 KB

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