sdio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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/pm_runtime.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/mmc.h>
  16. #include <linux/mmc/sdio.h>
  17. #include <linux/mmc/sdio_func.h>
  18. #include <linux/mmc/sdio_ids.h>
  19. #include "core.h"
  20. #include "bus.h"
  21. #include "sd.h"
  22. #include "sdio_bus.h"
  23. #include "mmc_ops.h"
  24. #include "sd_ops.h"
  25. #include "sdio_ops.h"
  26. #include "sdio_cis.h"
  27. static int sdio_read_fbr(struct sdio_func *func)
  28. {
  29. int ret;
  30. unsigned char data;
  31. if (mmc_card_nonstd_func_interface(func->card)) {
  32. func->class = SDIO_CLASS_NONE;
  33. return 0;
  34. }
  35. ret = mmc_io_rw_direct(func->card, 0, 0,
  36. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  37. if (ret)
  38. goto out;
  39. data &= 0x0f;
  40. if (data == 0x0f) {
  41. ret = mmc_io_rw_direct(func->card, 0, 0,
  42. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  43. if (ret)
  44. goto out;
  45. }
  46. func->class = data;
  47. out:
  48. return ret;
  49. }
  50. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  51. {
  52. int ret;
  53. struct sdio_func *func;
  54. if (WARN_ON(fn > SDIO_MAX_FUNCS))
  55. return -EINVAL;
  56. func = sdio_alloc_func(card);
  57. if (IS_ERR(func))
  58. return PTR_ERR(func);
  59. func->num = fn;
  60. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  61. ret = sdio_read_fbr(func);
  62. if (ret)
  63. goto fail;
  64. ret = sdio_read_func_cis(func);
  65. if (ret)
  66. goto fail;
  67. } else {
  68. func->vendor = func->card->cis.vendor;
  69. func->device = func->card->cis.device;
  70. func->max_blksize = func->card->cis.blksize;
  71. }
  72. card->sdio_func[fn - 1] = func;
  73. return 0;
  74. fail:
  75. /*
  76. * It is okay to remove the function here even though we hold
  77. * the host lock as we haven't registered the device yet.
  78. */
  79. sdio_remove_func(func);
  80. return ret;
  81. }
  82. static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
  83. {
  84. int ret;
  85. int cccr_vsn;
  86. int uhs = ocr & R4_18V_PRESENT;
  87. unsigned char data;
  88. unsigned char speed;
  89. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
  90. if (ret)
  91. goto out;
  92. cccr_vsn = data & 0x0f;
  93. if (cccr_vsn > SDIO_CCCR_REV_3_00) {
  94. pr_err("%s: unrecognised CCCR structure version %d\n",
  95. mmc_hostname(card->host), cccr_vsn);
  96. return -EINVAL;
  97. }
  98. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  99. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
  100. if (ret)
  101. goto out;
  102. if (data & SDIO_CCCR_CAP_SMB)
  103. card->cccr.multi_block = 1;
  104. if (data & SDIO_CCCR_CAP_LSC)
  105. card->cccr.low_speed = 1;
  106. if (data & SDIO_CCCR_CAP_4BLS)
  107. card->cccr.wide_bus = 1;
  108. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  109. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
  110. if (ret)
  111. goto out;
  112. if (data & SDIO_POWER_SMPC)
  113. card->cccr.high_power = 1;
  114. }
  115. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  116. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  117. if (ret)
  118. goto out;
  119. card->scr.sda_spec3 = 0;
  120. card->sw_caps.sd3_bus_mode = 0;
  121. card->sw_caps.sd3_drv_type = 0;
  122. if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
  123. card->scr.sda_spec3 = 1;
  124. ret = mmc_io_rw_direct(card, 0, 0,
  125. SDIO_CCCR_UHS, 0, &data);
  126. if (ret)
  127. goto out;
  128. if (mmc_host_uhs(card->host)) {
  129. if (data & SDIO_UHS_DDR50)
  130. card->sw_caps.sd3_bus_mode
  131. |= SD_MODE_UHS_DDR50;
  132. if (data & SDIO_UHS_SDR50)
  133. card->sw_caps.sd3_bus_mode
  134. |= SD_MODE_UHS_SDR50;
  135. if (data & SDIO_UHS_SDR104)
  136. card->sw_caps.sd3_bus_mode
  137. |= SD_MODE_UHS_SDR104;
  138. }
  139. ret = mmc_io_rw_direct(card, 0, 0,
  140. SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
  141. if (ret)
  142. goto out;
  143. if (data & SDIO_DRIVE_SDTA)
  144. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
  145. if (data & SDIO_DRIVE_SDTC)
  146. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
  147. if (data & SDIO_DRIVE_SDTD)
  148. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
  149. }
  150. /* if no uhs mode ensure we check for high speed */
  151. if (!card->sw_caps.sd3_bus_mode) {
  152. if (speed & SDIO_SPEED_SHS) {
  153. card->cccr.high_speed = 1;
  154. card->sw_caps.hs_max_dtr = 50000000;
  155. } else {
  156. card->cccr.high_speed = 0;
  157. card->sw_caps.hs_max_dtr = 25000000;
  158. }
  159. }
  160. }
  161. out:
  162. return ret;
  163. }
  164. static int sdio_enable_wide(struct mmc_card *card)
  165. {
  166. int ret;
  167. u8 ctrl;
  168. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  169. return 0;
  170. if (card->cccr.low_speed && !card->cccr.wide_bus)
  171. return 0;
  172. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  173. if (ret)
  174. return ret;
  175. if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
  176. pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
  177. mmc_hostname(card->host), ctrl);
  178. /* set as 4-bit bus width */
  179. ctrl &= ~SDIO_BUS_WIDTH_MASK;
  180. ctrl |= SDIO_BUS_WIDTH_4BIT;
  181. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  182. if (ret)
  183. return ret;
  184. return 1;
  185. }
  186. /*
  187. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  188. * of the card. This may be required on certain setups of boards,
  189. * controllers and embedded sdio device which do not need the card's
  190. * pull-up. As a result, card detection is disabled and power is saved.
  191. */
  192. static int sdio_disable_cd(struct mmc_card *card)
  193. {
  194. int ret;
  195. u8 ctrl;
  196. if (!mmc_card_disable_cd(card))
  197. return 0;
  198. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  199. if (ret)
  200. return ret;
  201. ctrl |= SDIO_BUS_CD_DISABLE;
  202. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  203. }
  204. /*
  205. * Devices that remain active during a system suspend are
  206. * put back into 1-bit mode.
  207. */
  208. static int sdio_disable_wide(struct mmc_card *card)
  209. {
  210. int ret;
  211. u8 ctrl;
  212. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  213. return 0;
  214. if (card->cccr.low_speed && !card->cccr.wide_bus)
  215. return 0;
  216. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  217. if (ret)
  218. return ret;
  219. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  220. return 0;
  221. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  222. ctrl |= SDIO_BUS_ASYNC_INT;
  223. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  224. if (ret)
  225. return ret;
  226. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  227. return 0;
  228. }
  229. static int sdio_enable_4bit_bus(struct mmc_card *card)
  230. {
  231. int err;
  232. if (card->type == MMC_TYPE_SDIO)
  233. err = sdio_enable_wide(card);
  234. else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  235. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  236. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  237. if (err)
  238. return err;
  239. err = sdio_enable_wide(card);
  240. if (err <= 0)
  241. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  242. } else
  243. return 0;
  244. if (err > 0) {
  245. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  246. err = 0;
  247. }
  248. return err;
  249. }
  250. /*
  251. * Test if the card supports high-speed mode and, if so, switch to it.
  252. */
  253. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  254. {
  255. int ret;
  256. u8 speed;
  257. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  258. return 0;
  259. if (!card->cccr.high_speed)
  260. return 0;
  261. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  262. if (ret)
  263. return ret;
  264. if (enable)
  265. speed |= SDIO_SPEED_EHS;
  266. else
  267. speed &= ~SDIO_SPEED_EHS;
  268. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  269. if (ret)
  270. return ret;
  271. return 1;
  272. }
  273. /*
  274. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  275. */
  276. static int sdio_enable_hs(struct mmc_card *card)
  277. {
  278. int ret;
  279. ret = mmc_sdio_switch_hs(card, true);
  280. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  281. return ret;
  282. ret = mmc_sd_switch_hs(card);
  283. if (ret <= 0)
  284. mmc_sdio_switch_hs(card, false);
  285. return ret;
  286. }
  287. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  288. {
  289. unsigned max_dtr;
  290. if (mmc_card_hs(card)) {
  291. /*
  292. * The SDIO specification doesn't mention how
  293. * the CIS transfer speed register relates to
  294. * high-speed, but it seems that 50 MHz is
  295. * mandatory.
  296. */
  297. max_dtr = 50000000;
  298. } else {
  299. max_dtr = card->cis.max_dtr;
  300. }
  301. if (card->type == MMC_TYPE_SD_COMBO)
  302. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  303. return max_dtr;
  304. }
  305. static unsigned char host_drive_to_sdio_drive(int host_strength)
  306. {
  307. switch (host_strength) {
  308. case MMC_SET_DRIVER_TYPE_A:
  309. return SDIO_DTSx_SET_TYPE_A;
  310. case MMC_SET_DRIVER_TYPE_B:
  311. return SDIO_DTSx_SET_TYPE_B;
  312. case MMC_SET_DRIVER_TYPE_C:
  313. return SDIO_DTSx_SET_TYPE_C;
  314. case MMC_SET_DRIVER_TYPE_D:
  315. return SDIO_DTSx_SET_TYPE_D;
  316. default:
  317. return SDIO_DTSx_SET_TYPE_B;
  318. }
  319. }
  320. static void sdio_select_driver_type(struct mmc_card *card)
  321. {
  322. int card_drv_type, drive_strength, drv_type;
  323. unsigned char card_strength;
  324. int err;
  325. card->drive_strength = 0;
  326. card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
  327. drive_strength = mmc_select_drive_strength(card,
  328. card->sw_caps.uhs_max_dtr,
  329. card_drv_type, &drv_type);
  330. if (drive_strength) {
  331. /* if error just use default for drive strength B */
  332. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
  333. &card_strength);
  334. if (err)
  335. return;
  336. card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
  337. card_strength |= host_drive_to_sdio_drive(drive_strength);
  338. /* if error default to drive strength B */
  339. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
  340. card_strength, NULL);
  341. if (err)
  342. return;
  343. card->drive_strength = drive_strength;
  344. }
  345. if (drv_type)
  346. mmc_set_driver_type(card->host, drv_type);
  347. }
  348. static int sdio_set_bus_speed_mode(struct mmc_card *card)
  349. {
  350. unsigned int bus_speed, timing;
  351. int err;
  352. unsigned char speed;
  353. /*
  354. * If the host doesn't support any of the UHS-I modes, fallback on
  355. * default speed.
  356. */
  357. if (!mmc_host_uhs(card->host))
  358. return 0;
  359. bus_speed = SDIO_SPEED_SDR12;
  360. timing = MMC_TIMING_UHS_SDR12;
  361. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  362. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  363. bus_speed = SDIO_SPEED_SDR104;
  364. timing = MMC_TIMING_UHS_SDR104;
  365. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  366. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  367. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  368. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  369. bus_speed = SDIO_SPEED_DDR50;
  370. timing = MMC_TIMING_UHS_DDR50;
  371. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  372. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  373. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  374. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  375. SD_MODE_UHS_SDR50)) {
  376. bus_speed = SDIO_SPEED_SDR50;
  377. timing = MMC_TIMING_UHS_SDR50;
  378. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  379. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  380. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  381. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  382. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  383. bus_speed = SDIO_SPEED_SDR25;
  384. timing = MMC_TIMING_UHS_SDR25;
  385. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  386. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  387. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  388. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  389. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  390. SD_MODE_UHS_SDR12)) {
  391. bus_speed = SDIO_SPEED_SDR12;
  392. timing = MMC_TIMING_UHS_SDR12;
  393. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  394. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  395. }
  396. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  397. if (err)
  398. return err;
  399. speed &= ~SDIO_SPEED_BSS_MASK;
  400. speed |= bus_speed;
  401. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  402. if (err)
  403. return err;
  404. if (bus_speed) {
  405. mmc_set_timing(card->host, timing);
  406. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  407. }
  408. return 0;
  409. }
  410. /*
  411. * UHS-I specific initialization procedure
  412. */
  413. static int mmc_sdio_init_uhs_card(struct mmc_card *card)
  414. {
  415. int err;
  416. if (!card->scr.sda_spec3)
  417. return 0;
  418. /*
  419. * Switch to wider bus (if supported).
  420. */
  421. if (card->host->caps & MMC_CAP_4_BIT_DATA)
  422. err = sdio_enable_4bit_bus(card);
  423. /* Set the driver strength for the card */
  424. sdio_select_driver_type(card);
  425. /* Set bus speed mode of the card */
  426. err = sdio_set_bus_speed_mode(card);
  427. if (err)
  428. goto out;
  429. /*
  430. * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
  431. * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
  432. */
  433. if (!mmc_host_is_spi(card->host) &&
  434. ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
  435. (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
  436. err = mmc_execute_tuning(card);
  437. out:
  438. return err;
  439. }
  440. /*
  441. * Handle the detection and initialisation of a card.
  442. *
  443. * In the case of a resume, "oldcard" will contain the card
  444. * we're trying to reinitialise.
  445. */
  446. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  447. struct mmc_card *oldcard, int powered_resume)
  448. {
  449. struct mmc_card *card;
  450. int err;
  451. int retries = 10;
  452. u32 rocr = 0;
  453. u32 ocr_card = ocr;
  454. WARN_ON(!host->claimed);
  455. /* to query card if 1.8V signalling is supported */
  456. if (mmc_host_uhs(host))
  457. ocr |= R4_18V_PRESENT;
  458. try_again:
  459. if (!retries) {
  460. pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
  461. ocr &= ~R4_18V_PRESENT;
  462. }
  463. /*
  464. * Inform the card of the voltage
  465. */
  466. if (!powered_resume) {
  467. err = mmc_send_io_op_cond(host, ocr, &rocr);
  468. if (err)
  469. goto err;
  470. }
  471. /*
  472. * For SPI, enable CRC as appropriate.
  473. */
  474. if (mmc_host_is_spi(host)) {
  475. err = mmc_spi_set_crc(host, use_spi_crc);
  476. if (err)
  477. goto err;
  478. }
  479. /*
  480. * Allocate card structure.
  481. */
  482. card = mmc_alloc_card(host, NULL);
  483. if (IS_ERR(card)) {
  484. err = PTR_ERR(card);
  485. goto err;
  486. }
  487. if ((rocr & R4_MEMORY_PRESENT) &&
  488. mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
  489. card->type = MMC_TYPE_SD_COMBO;
  490. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  491. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  492. mmc_remove_card(card);
  493. return -ENOENT;
  494. }
  495. } else {
  496. card->type = MMC_TYPE_SDIO;
  497. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  498. mmc_remove_card(card);
  499. return -ENOENT;
  500. }
  501. }
  502. /*
  503. * Call the optional HC's init_card function to handle quirks.
  504. */
  505. if (host->ops->init_card)
  506. host->ops->init_card(host, card);
  507. /*
  508. * If the host and card support UHS-I mode request the card
  509. * to switch to 1.8V signaling level. No 1.8v signalling if
  510. * UHS mode is not enabled to maintain compatibility and some
  511. * systems that claim 1.8v signalling in fact do not support
  512. * it.
  513. */
  514. if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
  515. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
  516. ocr_card);
  517. if (err == -EAGAIN) {
  518. sdio_reset(host);
  519. mmc_go_idle(host);
  520. mmc_send_if_cond(host, host->ocr_avail);
  521. mmc_remove_card(card);
  522. retries--;
  523. goto try_again;
  524. } else if (err) {
  525. ocr &= ~R4_18V_PRESENT;
  526. }
  527. err = 0;
  528. } else {
  529. ocr &= ~R4_18V_PRESENT;
  530. }
  531. /*
  532. * For native busses: set card RCA and quit open drain mode.
  533. */
  534. if (!powered_resume && !mmc_host_is_spi(host)) {
  535. err = mmc_send_relative_addr(host, &card->rca);
  536. if (err)
  537. goto remove;
  538. /*
  539. * Update oldcard with the new RCA received from the SDIO
  540. * device -- we're doing this so that it's updated in the
  541. * "card" struct when oldcard overwrites that later.
  542. */
  543. if (oldcard)
  544. oldcard->rca = card->rca;
  545. }
  546. /*
  547. * Read CSD, before selecting the card
  548. */
  549. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  550. err = mmc_sd_get_csd(host, card);
  551. if (err)
  552. return err;
  553. mmc_decode_cid(card);
  554. }
  555. /*
  556. * Select card, as all following commands rely on that.
  557. */
  558. if (!powered_resume && !mmc_host_is_spi(host)) {
  559. err = mmc_select_card(card);
  560. if (err)
  561. goto remove;
  562. }
  563. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  564. /*
  565. * This is non-standard SDIO device, meaning it doesn't
  566. * have any CIA (Common I/O area) registers present.
  567. * It's host's responsibility to fill cccr and cis
  568. * structures in init_card().
  569. */
  570. mmc_set_clock(host, card->cis.max_dtr);
  571. if (card->cccr.high_speed) {
  572. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  573. }
  574. goto finish;
  575. }
  576. /*
  577. * Read the common registers.
  578. */
  579. err = sdio_read_cccr(card, ocr);
  580. if (err)
  581. goto remove;
  582. /*
  583. * Read the common CIS tuples.
  584. */
  585. err = sdio_read_common_cis(card);
  586. if (err)
  587. goto remove;
  588. if (oldcard) {
  589. int same = (card->cis.vendor == oldcard->cis.vendor &&
  590. card->cis.device == oldcard->cis.device);
  591. mmc_remove_card(card);
  592. if (!same)
  593. return -ENOENT;
  594. card = oldcard;
  595. }
  596. card->ocr = ocr_card;
  597. mmc_fixup_device(card, NULL);
  598. if (card->type == MMC_TYPE_SD_COMBO) {
  599. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  600. /* handle as SDIO-only card if memory init failed */
  601. if (err) {
  602. mmc_go_idle(host);
  603. if (mmc_host_is_spi(host))
  604. /* should not fail, as it worked previously */
  605. mmc_spi_set_crc(host, use_spi_crc);
  606. card->type = MMC_TYPE_SDIO;
  607. } else
  608. card->dev.type = &sd_type;
  609. }
  610. /*
  611. * If needed, disconnect card detection pull-up resistor.
  612. */
  613. err = sdio_disable_cd(card);
  614. if (err)
  615. goto remove;
  616. /* Initialization sequence for UHS-I cards */
  617. /* Only if card supports 1.8v and UHS signaling */
  618. if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  619. err = mmc_sdio_init_uhs_card(card);
  620. if (err)
  621. goto remove;
  622. } else {
  623. /*
  624. * Switch to high-speed (if supported).
  625. */
  626. err = sdio_enable_hs(card);
  627. if (err > 0)
  628. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  629. else if (err)
  630. goto remove;
  631. /*
  632. * Change to the card's maximum speed.
  633. */
  634. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  635. /*
  636. * Switch to wider bus (if supported).
  637. */
  638. err = sdio_enable_4bit_bus(card);
  639. if (err)
  640. goto remove;
  641. }
  642. finish:
  643. if (!oldcard)
  644. host->card = card;
  645. return 0;
  646. remove:
  647. if (!oldcard)
  648. mmc_remove_card(card);
  649. err:
  650. return err;
  651. }
  652. /*
  653. * Host is being removed. Free up the current card.
  654. */
  655. static void mmc_sdio_remove(struct mmc_host *host)
  656. {
  657. int i;
  658. for (i = 0;i < host->card->sdio_funcs;i++) {
  659. if (host->card->sdio_func[i]) {
  660. sdio_remove_func(host->card->sdio_func[i]);
  661. host->card->sdio_func[i] = NULL;
  662. }
  663. }
  664. mmc_remove_card(host->card);
  665. host->card = NULL;
  666. }
  667. /*
  668. * Card detection - card is alive.
  669. */
  670. static int mmc_sdio_alive(struct mmc_host *host)
  671. {
  672. return mmc_select_card(host->card);
  673. }
  674. /*
  675. * Card detection callback from host.
  676. */
  677. static void mmc_sdio_detect(struct mmc_host *host)
  678. {
  679. int err;
  680. /* Make sure card is powered before detecting it */
  681. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  682. err = pm_runtime_get_sync(&host->card->dev);
  683. if (err < 0) {
  684. pm_runtime_put_noidle(&host->card->dev);
  685. goto out;
  686. }
  687. }
  688. mmc_claim_host(host);
  689. /*
  690. * Just check if our card has been removed.
  691. */
  692. err = _mmc_detect_card_removed(host);
  693. mmc_release_host(host);
  694. /*
  695. * Tell PM core it's OK to power off the card now.
  696. *
  697. * The _sync variant is used in order to ensure that the card
  698. * is left powered off in case an error occurred, and the card
  699. * is going to be removed.
  700. *
  701. * Since there is no specific reason to believe a new user
  702. * is about to show up at this point, the _sync variant is
  703. * desirable anyway.
  704. */
  705. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  706. pm_runtime_put_sync(&host->card->dev);
  707. out:
  708. if (err) {
  709. mmc_sdio_remove(host);
  710. mmc_claim_host(host);
  711. mmc_detach_bus(host);
  712. mmc_power_off(host);
  713. mmc_release_host(host);
  714. }
  715. }
  716. /*
  717. * SDIO pre_suspend. We need to suspend all functions separately.
  718. * Therefore all registered functions must have drivers with suspend
  719. * and resume methods. Failing that we simply remove the whole card.
  720. */
  721. static int mmc_sdio_pre_suspend(struct mmc_host *host)
  722. {
  723. int i, err = 0;
  724. for (i = 0; i < host->card->sdio_funcs; i++) {
  725. struct sdio_func *func = host->card->sdio_func[i];
  726. if (func && sdio_func_present(func) && func->dev.driver) {
  727. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  728. if (!pmops || !pmops->suspend || !pmops->resume) {
  729. /* force removal of entire card in that case */
  730. err = -ENOSYS;
  731. break;
  732. }
  733. }
  734. }
  735. return err;
  736. }
  737. /*
  738. * SDIO suspend. Suspend all functions separately.
  739. */
  740. static int mmc_sdio_suspend(struct mmc_host *host)
  741. {
  742. mmc_claim_host(host);
  743. if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
  744. sdio_disable_wide(host->card);
  745. if (!mmc_card_keep_power(host)) {
  746. mmc_power_off(host);
  747. } else if (host->retune_period) {
  748. mmc_retune_timer_stop(host);
  749. mmc_retune_needed(host);
  750. }
  751. mmc_release_host(host);
  752. return 0;
  753. }
  754. static int mmc_sdio_resume(struct mmc_host *host)
  755. {
  756. int err = 0;
  757. /* Basic card reinitialization. */
  758. mmc_claim_host(host);
  759. /* Restore power if needed */
  760. if (!mmc_card_keep_power(host)) {
  761. mmc_power_up(host, host->card->ocr);
  762. /*
  763. * Tell runtime PM core we just powered up the card,
  764. * since it still believes the card is powered off.
  765. * Note that currently runtime PM is only enabled
  766. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  767. */
  768. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  769. pm_runtime_disable(&host->card->dev);
  770. pm_runtime_set_active(&host->card->dev);
  771. pm_runtime_enable(&host->card->dev);
  772. }
  773. }
  774. /* No need to reinitialize powered-resumed nonremovable cards */
  775. if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
  776. sdio_reset(host);
  777. mmc_go_idle(host);
  778. mmc_send_if_cond(host, host->card->ocr);
  779. err = mmc_send_io_op_cond(host, 0, NULL);
  780. if (!err)
  781. err = mmc_sdio_init_card(host, host->card->ocr,
  782. host->card,
  783. mmc_card_keep_power(host));
  784. } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  785. /* We may have switched to 1-bit mode during suspend */
  786. err = sdio_enable_4bit_bus(host->card);
  787. }
  788. if (!err && host->sdio_irqs) {
  789. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
  790. wake_up_process(host->sdio_irq_thread);
  791. else if (host->caps & MMC_CAP_SDIO_IRQ)
  792. host->ops->enable_sdio_irq(host, 1);
  793. }
  794. mmc_release_host(host);
  795. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  796. return err;
  797. }
  798. static int mmc_sdio_power_restore(struct mmc_host *host)
  799. {
  800. int ret;
  801. mmc_claim_host(host);
  802. /*
  803. * Reset the card by performing the same steps that are taken by
  804. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  805. *
  806. * sdio_reset() is technically not needed. Having just powered up the
  807. * hardware, it should already be in reset state. However, some
  808. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  809. * meaning that a reset is required when restoring power soon after
  810. * powering off. It is harmless in other cases.
  811. *
  812. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  813. * is not necessary for non-removable cards. However, it is required
  814. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  815. * harmless in other situations.
  816. *
  817. */
  818. sdio_reset(host);
  819. mmc_go_idle(host);
  820. mmc_send_if_cond(host, host->card->ocr);
  821. ret = mmc_send_io_op_cond(host, 0, NULL);
  822. if (ret)
  823. goto out;
  824. ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
  825. mmc_card_keep_power(host));
  826. if (!ret && host->sdio_irqs)
  827. mmc_signal_sdio_irq(host);
  828. out:
  829. mmc_release_host(host);
  830. return ret;
  831. }
  832. static int mmc_sdio_runtime_suspend(struct mmc_host *host)
  833. {
  834. /* No references to the card, cut the power to it. */
  835. mmc_claim_host(host);
  836. mmc_power_off(host);
  837. mmc_release_host(host);
  838. return 0;
  839. }
  840. static int mmc_sdio_runtime_resume(struct mmc_host *host)
  841. {
  842. int ret;
  843. /* Restore power and re-initialize. */
  844. mmc_claim_host(host);
  845. mmc_power_up(host, host->card->ocr);
  846. ret = mmc_sdio_power_restore(host);
  847. mmc_release_host(host);
  848. return ret;
  849. }
  850. static int mmc_sdio_reset(struct mmc_host *host)
  851. {
  852. mmc_power_cycle(host, host->card->ocr);
  853. return mmc_sdio_power_restore(host);
  854. }
  855. static const struct mmc_bus_ops mmc_sdio_ops = {
  856. .remove = mmc_sdio_remove,
  857. .detect = mmc_sdio_detect,
  858. .pre_suspend = mmc_sdio_pre_suspend,
  859. .suspend = mmc_sdio_suspend,
  860. .resume = mmc_sdio_resume,
  861. .runtime_suspend = mmc_sdio_runtime_suspend,
  862. .runtime_resume = mmc_sdio_runtime_resume,
  863. .power_restore = mmc_sdio_power_restore,
  864. .alive = mmc_sdio_alive,
  865. .reset = mmc_sdio_reset,
  866. };
  867. /*
  868. * Starting point for SDIO card init.
  869. */
  870. int mmc_attach_sdio(struct mmc_host *host)
  871. {
  872. int err, i, funcs;
  873. u32 ocr, rocr;
  874. struct mmc_card *card;
  875. WARN_ON(!host->claimed);
  876. err = mmc_send_io_op_cond(host, 0, &ocr);
  877. if (err)
  878. return err;
  879. mmc_attach_bus(host, &mmc_sdio_ops);
  880. if (host->ocr_avail_sdio)
  881. host->ocr_avail = host->ocr_avail_sdio;
  882. rocr = mmc_select_voltage(host, ocr);
  883. /*
  884. * Can we support the voltage(s) of the card(s)?
  885. */
  886. if (!rocr) {
  887. err = -EINVAL;
  888. goto err;
  889. }
  890. /*
  891. * Detect and init the card.
  892. */
  893. err = mmc_sdio_init_card(host, rocr, NULL, 0);
  894. if (err)
  895. goto err;
  896. card = host->card;
  897. /*
  898. * Enable runtime PM only if supported by host+card+board
  899. */
  900. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  901. /*
  902. * Let runtime PM core know our card is active
  903. */
  904. err = pm_runtime_set_active(&card->dev);
  905. if (err)
  906. goto remove;
  907. /*
  908. * Enable runtime PM for this card
  909. */
  910. pm_runtime_enable(&card->dev);
  911. }
  912. /*
  913. * The number of functions on the card is encoded inside
  914. * the ocr.
  915. */
  916. funcs = (ocr & 0x70000000) >> 28;
  917. card->sdio_funcs = 0;
  918. /*
  919. * Initialize (but don't add) all present functions.
  920. */
  921. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  922. err = sdio_init_func(host->card, i + 1);
  923. if (err)
  924. goto remove;
  925. /*
  926. * Enable Runtime PM for this func (if supported)
  927. */
  928. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  929. pm_runtime_enable(&card->sdio_func[i]->dev);
  930. }
  931. /*
  932. * First add the card to the driver model...
  933. */
  934. mmc_release_host(host);
  935. err = mmc_add_card(host->card);
  936. if (err)
  937. goto remove_added;
  938. /*
  939. * ...then the SDIO functions.
  940. */
  941. for (i = 0;i < funcs;i++) {
  942. err = sdio_add_func(host->card->sdio_func[i]);
  943. if (err)
  944. goto remove_added;
  945. }
  946. mmc_claim_host(host);
  947. return 0;
  948. remove_added:
  949. /* Remove without lock if the device has been added. */
  950. mmc_sdio_remove(host);
  951. mmc_claim_host(host);
  952. remove:
  953. /* And with lock if it hasn't been added. */
  954. mmc_release_host(host);
  955. if (host->card)
  956. mmc_sdio_remove(host);
  957. mmc_claim_host(host);
  958. err:
  959. mmc_detach_bus(host);
  960. pr_err("%s: error %d whilst initialising SDIO card\n",
  961. mmc_hostname(host), err);
  962. return err;
  963. }