sdio.c 15 KB

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