sdio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. return 1;
  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. static int sdio_enable_4bit_bus(struct mmc_card *card)
  171. {
  172. int err;
  173. if (card->type == MMC_TYPE_SDIO)
  174. return sdio_enable_wide(card);
  175. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  176. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  177. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  178. if (err)
  179. return err;
  180. } else
  181. return 0;
  182. err = sdio_enable_wide(card);
  183. if (err <= 0)
  184. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  185. return err;
  186. }
  187. /*
  188. * Test if the card supports high-speed mode and, if so, switch to it.
  189. */
  190. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  191. {
  192. int ret;
  193. u8 speed;
  194. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  195. return 0;
  196. if (!card->cccr.high_speed)
  197. return 0;
  198. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  199. if (ret)
  200. return ret;
  201. if (enable)
  202. speed |= SDIO_SPEED_EHS;
  203. else
  204. speed &= ~SDIO_SPEED_EHS;
  205. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  206. if (ret)
  207. return ret;
  208. return 1;
  209. }
  210. /*
  211. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  212. */
  213. static int sdio_enable_hs(struct mmc_card *card)
  214. {
  215. int ret;
  216. ret = mmc_sdio_switch_hs(card, true);
  217. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  218. return ret;
  219. ret = mmc_sd_switch_hs(card);
  220. if (ret <= 0)
  221. mmc_sdio_switch_hs(card, false);
  222. return ret;
  223. }
  224. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  225. {
  226. unsigned max_dtr;
  227. if (mmc_card_highspeed(card)) {
  228. /*
  229. * The SDIO specification doesn't mention how
  230. * the CIS transfer speed register relates to
  231. * high-speed, but it seems that 50 MHz is
  232. * mandatory.
  233. */
  234. max_dtr = 50000000;
  235. } else {
  236. max_dtr = card->cis.max_dtr;
  237. }
  238. if (card->type == MMC_TYPE_SD_COMBO)
  239. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  240. return max_dtr;
  241. }
  242. /*
  243. * Handle the detection and initialisation of a card.
  244. *
  245. * In the case of a resume, "oldcard" will contain the card
  246. * we're trying to reinitialise.
  247. */
  248. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  249. struct mmc_card *oldcard, int powered_resume)
  250. {
  251. struct mmc_card *card;
  252. int err;
  253. BUG_ON(!host);
  254. WARN_ON(!host->claimed);
  255. /*
  256. * Inform the card of the voltage
  257. */
  258. if (!powered_resume) {
  259. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  260. if (err)
  261. goto err;
  262. }
  263. /*
  264. * For SPI, enable CRC as appropriate.
  265. */
  266. if (mmc_host_is_spi(host)) {
  267. err = mmc_spi_set_crc(host, use_spi_crc);
  268. if (err)
  269. goto err;
  270. }
  271. /*
  272. * Allocate card structure.
  273. */
  274. card = mmc_alloc_card(host, NULL);
  275. if (IS_ERR(card)) {
  276. err = PTR_ERR(card);
  277. goto err;
  278. }
  279. err = mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid);
  280. if (!err) {
  281. card->type = MMC_TYPE_SD_COMBO;
  282. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  283. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  284. mmc_remove_card(card);
  285. return -ENOENT;
  286. }
  287. } else {
  288. card->type = MMC_TYPE_SDIO;
  289. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  290. mmc_remove_card(card);
  291. return -ENOENT;
  292. }
  293. }
  294. /*
  295. * Call the optional HC's init_card function to handle quirks.
  296. */
  297. if (host->ops->init_card)
  298. host->ops->init_card(host, card);
  299. /*
  300. * For native busses: set card RCA and quit open drain mode.
  301. */
  302. if (!powered_resume && !mmc_host_is_spi(host)) {
  303. err = mmc_send_relative_addr(host, &card->rca);
  304. if (err)
  305. goto remove;
  306. mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
  307. }
  308. /*
  309. * Read CSD, before selecting the card
  310. */
  311. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  312. err = mmc_sd_get_csd(host, card);
  313. if (err)
  314. return err;
  315. mmc_decode_cid(card);
  316. }
  317. /*
  318. * Select card, as all following commands rely on that.
  319. */
  320. if (!powered_resume && !mmc_host_is_spi(host)) {
  321. err = mmc_select_card(card);
  322. if (err)
  323. goto remove;
  324. }
  325. /*
  326. * Read the common registers.
  327. */
  328. err = sdio_read_cccr(card);
  329. if (err)
  330. goto remove;
  331. /*
  332. * Read the common CIS tuples.
  333. */
  334. err = sdio_read_common_cis(card);
  335. if (err)
  336. goto remove;
  337. if (oldcard) {
  338. int same = (card->cis.vendor == oldcard->cis.vendor &&
  339. card->cis.device == oldcard->cis.device);
  340. mmc_remove_card(card);
  341. if (!same)
  342. return -ENOENT;
  343. card = oldcard;
  344. return 0;
  345. }
  346. if (card->type == MMC_TYPE_SD_COMBO) {
  347. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  348. /* handle as SDIO-only card if memory init failed */
  349. if (err) {
  350. mmc_go_idle(host);
  351. if (mmc_host_is_spi(host))
  352. /* should not fail, as it worked previously */
  353. mmc_spi_set_crc(host, use_spi_crc);
  354. card->type = MMC_TYPE_SDIO;
  355. } else
  356. card->dev.type = &sd_type;
  357. }
  358. /*
  359. * If needed, disconnect card detection pull-up resistor.
  360. */
  361. err = sdio_disable_cd(card);
  362. if (err)
  363. goto remove;
  364. /*
  365. * Switch to high-speed (if supported).
  366. */
  367. err = sdio_enable_hs(card);
  368. if (err > 0)
  369. mmc_sd_go_highspeed(card);
  370. else if (err)
  371. goto remove;
  372. /*
  373. * Change to the card's maximum speed.
  374. */
  375. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  376. /*
  377. * Switch to wider bus (if supported).
  378. */
  379. err = sdio_enable_4bit_bus(card);
  380. if (err > 0)
  381. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  382. else if (err)
  383. goto remove;
  384. if (!oldcard)
  385. host->card = card;
  386. return 0;
  387. remove:
  388. if (!oldcard)
  389. mmc_remove_card(card);
  390. err:
  391. return err;
  392. }
  393. /*
  394. * Host is being removed. Free up the current card.
  395. */
  396. static void mmc_sdio_remove(struct mmc_host *host)
  397. {
  398. int i;
  399. BUG_ON(!host);
  400. BUG_ON(!host->card);
  401. for (i = 0;i < host->card->sdio_funcs;i++) {
  402. if (host->card->sdio_func[i]) {
  403. sdio_remove_func(host->card->sdio_func[i]);
  404. host->card->sdio_func[i] = NULL;
  405. }
  406. }
  407. mmc_remove_card(host->card);
  408. host->card = NULL;
  409. }
  410. /*
  411. * Card detection callback from host.
  412. */
  413. static void mmc_sdio_detect(struct mmc_host *host)
  414. {
  415. int err;
  416. BUG_ON(!host);
  417. BUG_ON(!host->card);
  418. mmc_claim_host(host);
  419. /*
  420. * Just check if our card has been removed.
  421. */
  422. err = mmc_select_card(host->card);
  423. mmc_release_host(host);
  424. if (err) {
  425. mmc_sdio_remove(host);
  426. mmc_claim_host(host);
  427. mmc_detach_bus(host);
  428. mmc_release_host(host);
  429. }
  430. }
  431. /*
  432. * SDIO suspend. We need to suspend all functions separately.
  433. * Therefore all registered functions must have drivers with suspend
  434. * and resume methods. Failing that we simply remove the whole card.
  435. */
  436. static int mmc_sdio_suspend(struct mmc_host *host)
  437. {
  438. int i, err = 0;
  439. for (i = 0; i < host->card->sdio_funcs; i++) {
  440. struct sdio_func *func = host->card->sdio_func[i];
  441. if (func && sdio_func_present(func) && func->dev.driver) {
  442. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  443. if (!pmops || !pmops->suspend || !pmops->resume) {
  444. /* force removal of entire card in that case */
  445. err = -ENOSYS;
  446. } else
  447. err = pmops->suspend(&func->dev);
  448. if (err)
  449. break;
  450. }
  451. }
  452. while (err && --i >= 0) {
  453. struct sdio_func *func = host->card->sdio_func[i];
  454. if (func && sdio_func_present(func) && func->dev.driver) {
  455. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  456. pmops->resume(&func->dev);
  457. }
  458. }
  459. if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
  460. mmc_claim_host(host);
  461. sdio_disable_wide(host->card);
  462. mmc_release_host(host);
  463. }
  464. return err;
  465. }
  466. static int mmc_sdio_resume(struct mmc_host *host)
  467. {
  468. int i, err;
  469. BUG_ON(!host);
  470. BUG_ON(!host->card);
  471. /* Basic card reinitialization. */
  472. mmc_claim_host(host);
  473. err = mmc_sdio_init_card(host, host->ocr, host->card,
  474. (host->pm_flags & MMC_PM_KEEP_POWER));
  475. if (!err) {
  476. /* We may have switched to 1-bit mode during suspend. */
  477. err = sdio_enable_4bit_bus(host->card);
  478. if (err > 0) {
  479. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  480. err = 0;
  481. }
  482. }
  483. if (!err && host->sdio_irqs)
  484. mmc_signal_sdio_irq(host);
  485. mmc_release_host(host);
  486. /*
  487. * If the card looked to be the same as before suspending, then
  488. * we proceed to resume all card functions. If one of them returns
  489. * an error then we simply return that error to the core and the
  490. * card will be redetected as new. It is the responsibility of
  491. * the function driver to perform further tests with the extra
  492. * knowledge it has of the card to confirm the card is indeed the
  493. * same as before suspending (same MAC address for network cards,
  494. * etc.) and return an error otherwise.
  495. */
  496. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  497. struct sdio_func *func = host->card->sdio_func[i];
  498. if (func && sdio_func_present(func) && func->dev.driver) {
  499. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  500. err = pmops->resume(&func->dev);
  501. }
  502. }
  503. return err;
  504. }
  505. static const struct mmc_bus_ops mmc_sdio_ops = {
  506. .remove = mmc_sdio_remove,
  507. .detect = mmc_sdio_detect,
  508. .suspend = mmc_sdio_suspend,
  509. .resume = mmc_sdio_resume,
  510. };
  511. /*
  512. * Starting point for SDIO card init.
  513. */
  514. int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
  515. {
  516. int err;
  517. int i, funcs;
  518. struct mmc_card *card;
  519. BUG_ON(!host);
  520. WARN_ON(!host->claimed);
  521. mmc_attach_bus(host, &mmc_sdio_ops);
  522. /*
  523. * Sanity check the voltages that the card claims to
  524. * support.
  525. */
  526. if (ocr & 0x7F) {
  527. printk(KERN_WARNING "%s: card claims to support voltages "
  528. "below the defined range. These will be ignored.\n",
  529. mmc_hostname(host));
  530. ocr &= ~0x7F;
  531. }
  532. host->ocr = mmc_select_voltage(host, ocr);
  533. /*
  534. * Can we support the voltage(s) of the card(s)?
  535. */
  536. if (!host->ocr) {
  537. err = -EINVAL;
  538. goto err;
  539. }
  540. /*
  541. * Detect and init the card.
  542. */
  543. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  544. if (err)
  545. goto err;
  546. card = host->card;
  547. /*
  548. * The number of functions on the card is encoded inside
  549. * the ocr.
  550. */
  551. funcs = (ocr & 0x70000000) >> 28;
  552. card->sdio_funcs = 0;
  553. /*
  554. * Initialize (but don't add) all present functions.
  555. */
  556. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  557. err = sdio_init_func(host->card, i + 1);
  558. if (err)
  559. goto remove;
  560. }
  561. mmc_release_host(host);
  562. /*
  563. * First add the card to the driver model...
  564. */
  565. err = mmc_add_card(host->card);
  566. if (err)
  567. goto remove_added;
  568. /*
  569. * ...then the SDIO functions.
  570. */
  571. for (i = 0;i < funcs;i++) {
  572. err = sdio_add_func(host->card->sdio_func[i]);
  573. if (err)
  574. goto remove_added;
  575. }
  576. return 0;
  577. remove_added:
  578. /* Remove without lock if the device has been added. */
  579. mmc_sdio_remove(host);
  580. mmc_claim_host(host);
  581. remove:
  582. /* And with lock if it hasn't been added. */
  583. if (host->card)
  584. mmc_sdio_remove(host);
  585. err:
  586. mmc_detach_bus(host);
  587. mmc_release_host(host);
  588. printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
  589. mmc_hostname(host), err);
  590. return err;
  591. }