sdio.c 26 KB

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