sdio.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. unsigned int max_rate;
  357. /*
  358. * If the host doesn't support any of the UHS-I modes, fallback on
  359. * default speed.
  360. */
  361. if (!mmc_host_uhs(card->host))
  362. return 0;
  363. bus_speed = SDIO_SPEED_SDR12;
  364. timing = MMC_TIMING_UHS_SDR12;
  365. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  366. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  367. bus_speed = SDIO_SPEED_SDR104;
  368. timing = MMC_TIMING_UHS_SDR104;
  369. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  370. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  371. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  372. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  373. bus_speed = SDIO_SPEED_DDR50;
  374. timing = MMC_TIMING_UHS_DDR50;
  375. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  376. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  377. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  378. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  379. SD_MODE_UHS_SDR50)) {
  380. bus_speed = SDIO_SPEED_SDR50;
  381. timing = MMC_TIMING_UHS_SDR50;
  382. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  383. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  384. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  385. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  386. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  387. bus_speed = SDIO_SPEED_SDR25;
  388. timing = MMC_TIMING_UHS_SDR25;
  389. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  390. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  391. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  392. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  393. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  394. SD_MODE_UHS_SDR12)) {
  395. bus_speed = SDIO_SPEED_SDR12;
  396. timing = MMC_TIMING_UHS_SDR12;
  397. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  398. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  399. }
  400. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  401. if (err)
  402. return err;
  403. speed &= ~SDIO_SPEED_BSS_MASK;
  404. speed |= bus_speed;
  405. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  406. if (err)
  407. return err;
  408. max_rate = min_not_zero(card->quirk_max_rate,
  409. card->sw_caps.uhs_max_dtr);
  410. if (bus_speed) {
  411. mmc_set_timing(card->host, timing);
  412. mmc_set_clock(card->host, max_rate);
  413. }
  414. return 0;
  415. }
  416. /*
  417. * UHS-I specific initialization procedure
  418. */
  419. static int mmc_sdio_init_uhs_card(struct mmc_card *card)
  420. {
  421. int err;
  422. if (!card->scr.sda_spec3)
  423. return 0;
  424. /* Switch to wider bus */
  425. err = sdio_enable_4bit_bus(card);
  426. if (err)
  427. goto out;
  428. /* Set the driver strength for the card */
  429. sdio_select_driver_type(card);
  430. /* Set bus speed mode of the card */
  431. err = sdio_set_bus_speed_mode(card);
  432. if (err)
  433. goto out;
  434. /*
  435. * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
  436. * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
  437. */
  438. if (!mmc_host_is_spi(card->host) &&
  439. ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
  440. (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
  441. err = mmc_execute_tuning(card);
  442. out:
  443. return err;
  444. }
  445. static void mmc_sdio_resend_if_cond(struct mmc_host *host,
  446. struct mmc_card *card)
  447. {
  448. sdio_reset(host);
  449. mmc_go_idle(host);
  450. mmc_send_if_cond(host, host->ocr_avail);
  451. mmc_remove_card(card);
  452. }
  453. /*
  454. * Handle the detection and initialisation of a card.
  455. *
  456. * In the case of a resume, "oldcard" will contain the card
  457. * we're trying to reinitialise.
  458. */
  459. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  460. struct mmc_card *oldcard, int powered_resume)
  461. {
  462. struct mmc_card *card;
  463. int err;
  464. int retries = 10;
  465. u32 rocr = 0;
  466. u32 ocr_card = ocr;
  467. WARN_ON(!host->claimed);
  468. /* to query card if 1.8V signalling is supported */
  469. if (mmc_host_uhs(host))
  470. ocr |= R4_18V_PRESENT;
  471. try_again:
  472. if (!retries) {
  473. pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
  474. ocr &= ~R4_18V_PRESENT;
  475. }
  476. /*
  477. * Inform the card of the voltage
  478. */
  479. if (!powered_resume) {
  480. err = mmc_send_io_op_cond(host, ocr, &rocr);
  481. if (err)
  482. goto err;
  483. }
  484. /*
  485. * For SPI, enable CRC as appropriate.
  486. */
  487. if (mmc_host_is_spi(host)) {
  488. err = mmc_spi_set_crc(host, use_spi_crc);
  489. if (err)
  490. goto err;
  491. }
  492. /*
  493. * Allocate card structure.
  494. */
  495. card = mmc_alloc_card(host, NULL);
  496. if (IS_ERR(card)) {
  497. err = PTR_ERR(card);
  498. goto err;
  499. }
  500. if ((rocr & R4_MEMORY_PRESENT) &&
  501. mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
  502. card->type = MMC_TYPE_SD_COMBO;
  503. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  504. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  505. mmc_remove_card(card);
  506. return -ENOENT;
  507. }
  508. } else {
  509. card->type = MMC_TYPE_SDIO;
  510. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  511. mmc_remove_card(card);
  512. return -ENOENT;
  513. }
  514. }
  515. /*
  516. * Call the optional HC's init_card function to handle quirks.
  517. */
  518. if (host->ops->init_card)
  519. host->ops->init_card(host, card);
  520. /*
  521. * If the host and card support UHS-I mode request the card
  522. * to switch to 1.8V signaling level. No 1.8v signalling if
  523. * UHS mode is not enabled to maintain compatibility and some
  524. * systems that claim 1.8v signalling in fact do not support
  525. * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
  526. * 1.8v, the card sets S18A to 0 in the R4 response. So it will
  527. * fails to check rocr & R4_18V_PRESENT, but we still need to
  528. * try to init uhs card. sdio_read_cccr will take over this task
  529. * to make sure which speed mode should work.
  530. */
  531. if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
  532. err = mmc_set_uhs_voltage(host, ocr_card);
  533. if (err == -EAGAIN) {
  534. mmc_sdio_resend_if_cond(host, card);
  535. retries--;
  536. goto try_again;
  537. } else if (err) {
  538. ocr &= ~R4_18V_PRESENT;
  539. }
  540. }
  541. /*
  542. * For native busses: set card RCA and quit open drain mode.
  543. */
  544. if (!powered_resume && !mmc_host_is_spi(host)) {
  545. err = mmc_send_relative_addr(host, &card->rca);
  546. if (err)
  547. goto remove;
  548. /*
  549. * Update oldcard with the new RCA received from the SDIO
  550. * device -- we're doing this so that it's updated in the
  551. * "card" struct when oldcard overwrites that later.
  552. */
  553. if (oldcard)
  554. oldcard->rca = card->rca;
  555. }
  556. /*
  557. * Read CSD, before selecting the card
  558. */
  559. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  560. err = mmc_sd_get_csd(host, card);
  561. if (err)
  562. return err;
  563. mmc_decode_cid(card);
  564. }
  565. /*
  566. * Select card, as all following commands rely on that.
  567. */
  568. if (!powered_resume && !mmc_host_is_spi(host)) {
  569. err = mmc_select_card(card);
  570. if (err)
  571. goto remove;
  572. }
  573. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  574. /*
  575. * This is non-standard SDIO device, meaning it doesn't
  576. * have any CIA (Common I/O area) registers present.
  577. * It's host's responsibility to fill cccr and cis
  578. * structures in init_card().
  579. */
  580. mmc_set_clock(host, card->cis.max_dtr);
  581. if (card->cccr.high_speed) {
  582. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  583. }
  584. goto finish;
  585. }
  586. /*
  587. * Read the common registers. Note that we should try to
  588. * validate whether UHS would work or not.
  589. */
  590. err = sdio_read_cccr(card, ocr);
  591. if (err) {
  592. mmc_sdio_resend_if_cond(host, card);
  593. if (ocr & R4_18V_PRESENT) {
  594. /* Retry init sequence, but without R4_18V_PRESENT. */
  595. retries = 0;
  596. goto try_again;
  597. } else {
  598. goto remove;
  599. }
  600. }
  601. /*
  602. * Read the common CIS tuples.
  603. */
  604. err = sdio_read_common_cis(card);
  605. if (err)
  606. goto remove;
  607. if (oldcard) {
  608. int same = (card->cis.vendor == oldcard->cis.vendor &&
  609. card->cis.device == oldcard->cis.device);
  610. mmc_remove_card(card);
  611. if (!same)
  612. return -ENOENT;
  613. card = oldcard;
  614. }
  615. card->ocr = ocr_card;
  616. mmc_fixup_device(card, sdio_fixup_methods);
  617. if (card->type == MMC_TYPE_SD_COMBO) {
  618. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  619. /* handle as SDIO-only card if memory init failed */
  620. if (err) {
  621. mmc_go_idle(host);
  622. if (mmc_host_is_spi(host))
  623. /* should not fail, as it worked previously */
  624. mmc_spi_set_crc(host, use_spi_crc);
  625. card->type = MMC_TYPE_SDIO;
  626. } else
  627. card->dev.type = &sd_type;
  628. }
  629. /*
  630. * If needed, disconnect card detection pull-up resistor.
  631. */
  632. err = sdio_disable_cd(card);
  633. if (err)
  634. goto remove;
  635. /* Initialization sequence for UHS-I cards */
  636. /* Only if card supports 1.8v and UHS signaling */
  637. if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  638. err = mmc_sdio_init_uhs_card(card);
  639. if (err)
  640. goto remove;
  641. } else {
  642. /*
  643. * Switch to high-speed (if supported).
  644. */
  645. err = sdio_enable_hs(card);
  646. if (err > 0)
  647. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  648. else if (err)
  649. goto remove;
  650. /*
  651. * Change to the card's maximum speed.
  652. */
  653. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  654. /*
  655. * Switch to wider bus (if supported).
  656. */
  657. err = sdio_enable_4bit_bus(card);
  658. if (err)
  659. goto remove;
  660. }
  661. if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
  662. host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  663. pr_err("%s: Host failed to negotiate down from 3.3V\n",
  664. mmc_hostname(host));
  665. err = -EINVAL;
  666. goto remove;
  667. }
  668. finish:
  669. if (!oldcard)
  670. host->card = card;
  671. return 0;
  672. remove:
  673. if (!oldcard)
  674. mmc_remove_card(card);
  675. err:
  676. return err;
  677. }
  678. static int mmc_sdio_reinit_card(struct mmc_host *host, bool powered_resume)
  679. {
  680. int ret;
  681. sdio_reset(host);
  682. mmc_go_idle(host);
  683. mmc_send_if_cond(host, host->card->ocr);
  684. ret = mmc_send_io_op_cond(host, 0, NULL);
  685. if (ret)
  686. return ret;
  687. return mmc_sdio_init_card(host, host->card->ocr, host->card,
  688. powered_resume);
  689. }
  690. /*
  691. * Host is being removed. Free up the current card.
  692. */
  693. static void mmc_sdio_remove(struct mmc_host *host)
  694. {
  695. int i;
  696. for (i = 0;i < host->card->sdio_funcs;i++) {
  697. if (host->card->sdio_func[i]) {
  698. sdio_remove_func(host->card->sdio_func[i]);
  699. host->card->sdio_func[i] = NULL;
  700. }
  701. }
  702. mmc_remove_card(host->card);
  703. host->card = NULL;
  704. }
  705. /*
  706. * Card detection - card is alive.
  707. */
  708. static int mmc_sdio_alive(struct mmc_host *host)
  709. {
  710. return mmc_select_card(host->card);
  711. }
  712. /*
  713. * Card detection callback from host.
  714. */
  715. static void mmc_sdio_detect(struct mmc_host *host)
  716. {
  717. int err;
  718. /* Make sure card is powered before detecting it */
  719. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  720. err = pm_runtime_get_sync(&host->card->dev);
  721. if (err < 0) {
  722. pm_runtime_put_noidle(&host->card->dev);
  723. goto out;
  724. }
  725. }
  726. mmc_claim_host(host);
  727. /*
  728. * Just check if our card has been removed.
  729. */
  730. err = _mmc_detect_card_removed(host);
  731. mmc_release_host(host);
  732. /*
  733. * Tell PM core it's OK to power off the card now.
  734. *
  735. * The _sync variant is used in order to ensure that the card
  736. * is left powered off in case an error occurred, and the card
  737. * is going to be removed.
  738. *
  739. * Since there is no specific reason to believe a new user
  740. * is about to show up at this point, the _sync variant is
  741. * desirable anyway.
  742. */
  743. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  744. pm_runtime_put_sync(&host->card->dev);
  745. out:
  746. if (err) {
  747. mmc_sdio_remove(host);
  748. mmc_claim_host(host);
  749. mmc_detach_bus(host);
  750. mmc_power_off(host);
  751. mmc_release_host(host);
  752. }
  753. }
  754. /*
  755. * SDIO pre_suspend. We need to suspend all functions separately.
  756. * Therefore all registered functions must have drivers with suspend
  757. * and resume methods. Failing that we simply remove the whole card.
  758. */
  759. static int mmc_sdio_pre_suspend(struct mmc_host *host)
  760. {
  761. int i, err = 0;
  762. for (i = 0; i < host->card->sdio_funcs; i++) {
  763. struct sdio_func *func = host->card->sdio_func[i];
  764. if (func && sdio_func_present(func) && func->dev.driver) {
  765. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  766. if (!pmops || !pmops->suspend || !pmops->resume) {
  767. /* force removal of entire card in that case */
  768. err = -ENOSYS;
  769. break;
  770. }
  771. }
  772. }
  773. return err;
  774. }
  775. /*
  776. * SDIO suspend. Suspend all functions separately.
  777. */
  778. static int mmc_sdio_suspend(struct mmc_host *host)
  779. {
  780. mmc_claim_host(host);
  781. if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
  782. sdio_disable_wide(host->card);
  783. if (!mmc_card_keep_power(host)) {
  784. mmc_power_off(host);
  785. } else if (host->retune_period) {
  786. mmc_retune_timer_stop(host);
  787. mmc_retune_needed(host);
  788. }
  789. mmc_release_host(host);
  790. return 0;
  791. }
  792. static int mmc_sdio_resume(struct mmc_host *host)
  793. {
  794. int err = 0;
  795. /* Basic card reinitialization. */
  796. mmc_claim_host(host);
  797. /* Restore power if needed */
  798. if (!mmc_card_keep_power(host)) {
  799. mmc_power_up(host, host->card->ocr);
  800. /*
  801. * Tell runtime PM core we just powered up the card,
  802. * since it still believes the card is powered off.
  803. * Note that currently runtime PM is only enabled
  804. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  805. */
  806. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  807. pm_runtime_disable(&host->card->dev);
  808. pm_runtime_set_active(&host->card->dev);
  809. pm_runtime_enable(&host->card->dev);
  810. }
  811. }
  812. /* No need to reinitialize powered-resumed nonremovable cards */
  813. if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
  814. err = mmc_sdio_reinit_card(host, mmc_card_keep_power(host));
  815. } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  816. /* We may have switched to 1-bit mode during suspend */
  817. err = sdio_enable_4bit_bus(host->card);
  818. }
  819. if (!err && host->sdio_irqs) {
  820. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
  821. wake_up_process(host->sdio_irq_thread);
  822. else if (host->caps & MMC_CAP_SDIO_IRQ)
  823. host->ops->enable_sdio_irq(host, 1);
  824. }
  825. mmc_release_host(host);
  826. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  827. return err;
  828. }
  829. static int mmc_sdio_power_restore(struct mmc_host *host)
  830. {
  831. int ret;
  832. /*
  833. * Reset the card by performing the same steps that are taken by
  834. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  835. *
  836. * sdio_reset() is technically not needed. Having just powered up the
  837. * hardware, it should already be in reset state. However, some
  838. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  839. * meaning that a reset is required when restoring power soon after
  840. * powering off. It is harmless in other cases.
  841. *
  842. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  843. * is not necessary for non-removable cards. However, it is required
  844. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  845. * harmless in other situations.
  846. *
  847. */
  848. mmc_claim_host(host);
  849. ret = mmc_sdio_reinit_card(host, mmc_card_keep_power(host));
  850. if (!ret && host->sdio_irqs)
  851. mmc_signal_sdio_irq(host);
  852. mmc_release_host(host);
  853. return ret;
  854. }
  855. static int mmc_sdio_runtime_suspend(struct mmc_host *host)
  856. {
  857. /* No references to the card, cut the power to it. */
  858. mmc_claim_host(host);
  859. mmc_power_off(host);
  860. mmc_release_host(host);
  861. return 0;
  862. }
  863. static int mmc_sdio_runtime_resume(struct mmc_host *host)
  864. {
  865. int ret;
  866. /* Restore power and re-initialize. */
  867. mmc_claim_host(host);
  868. mmc_power_up(host, host->card->ocr);
  869. ret = mmc_sdio_power_restore(host);
  870. mmc_release_host(host);
  871. return ret;
  872. }
  873. static int mmc_sdio_hw_reset(struct mmc_host *host)
  874. {
  875. mmc_power_cycle(host, host->card->ocr);
  876. return mmc_sdio_power_restore(host);
  877. }
  878. static int mmc_sdio_sw_reset(struct mmc_host *host)
  879. {
  880. mmc_set_clock(host, host->f_init);
  881. sdio_reset(host);
  882. mmc_go_idle(host);
  883. mmc_set_initial_state(host);
  884. mmc_set_initial_signal_voltage(host);
  885. return mmc_sdio_reinit_card(host, 0);
  886. }
  887. static const struct mmc_bus_ops mmc_sdio_ops = {
  888. .remove = mmc_sdio_remove,
  889. .detect = mmc_sdio_detect,
  890. .pre_suspend = mmc_sdio_pre_suspend,
  891. .suspend = mmc_sdio_suspend,
  892. .resume = mmc_sdio_resume,
  893. .runtime_suspend = mmc_sdio_runtime_suspend,
  894. .runtime_resume = mmc_sdio_runtime_resume,
  895. .power_restore = mmc_sdio_power_restore,
  896. .alive = mmc_sdio_alive,
  897. .hw_reset = mmc_sdio_hw_reset,
  898. .sw_reset = mmc_sdio_sw_reset,
  899. };
  900. /*
  901. * Starting point for SDIO card init.
  902. */
  903. int mmc_attach_sdio(struct mmc_host *host)
  904. {
  905. int err, i, funcs;
  906. u32 ocr, rocr;
  907. struct mmc_card *card;
  908. WARN_ON(!host->claimed);
  909. err = mmc_send_io_op_cond(host, 0, &ocr);
  910. if (err)
  911. return err;
  912. mmc_attach_bus(host, &mmc_sdio_ops);
  913. if (host->ocr_avail_sdio)
  914. host->ocr_avail = host->ocr_avail_sdio;
  915. rocr = mmc_select_voltage(host, ocr);
  916. /*
  917. * Can we support the voltage(s) of the card(s)?
  918. */
  919. if (!rocr) {
  920. err = -EINVAL;
  921. goto err;
  922. }
  923. /*
  924. * Detect and init the card.
  925. */
  926. err = mmc_sdio_init_card(host, rocr, NULL, 0);
  927. if (err)
  928. goto err;
  929. card = host->card;
  930. /*
  931. * Enable runtime PM only if supported by host+card+board
  932. */
  933. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  934. /*
  935. * Do not allow runtime suspend until after SDIO function
  936. * devices are added.
  937. */
  938. pm_runtime_get_noresume(&card->dev);
  939. /*
  940. * Let runtime PM core know our card is active
  941. */
  942. err = pm_runtime_set_active(&card->dev);
  943. if (err)
  944. goto remove;
  945. /*
  946. * Enable runtime PM for this card
  947. */
  948. pm_runtime_enable(&card->dev);
  949. }
  950. /*
  951. * The number of functions on the card is encoded inside
  952. * the ocr.
  953. */
  954. funcs = (ocr & 0x70000000) >> 28;
  955. card->sdio_funcs = 0;
  956. /*
  957. * Initialize (but don't add) all present functions.
  958. */
  959. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  960. err = sdio_init_func(host->card, i + 1);
  961. if (err)
  962. goto remove;
  963. /*
  964. * Enable Runtime PM for this func (if supported)
  965. */
  966. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  967. pm_runtime_enable(&card->sdio_func[i]->dev);
  968. }
  969. /*
  970. * First add the card to the driver model...
  971. */
  972. mmc_release_host(host);
  973. err = mmc_add_card(host->card);
  974. if (err)
  975. goto remove_added;
  976. /*
  977. * ...then the SDIO functions.
  978. */
  979. for (i = 0;i < funcs;i++) {
  980. err = sdio_add_func(host->card->sdio_func[i]);
  981. if (err)
  982. goto remove_added;
  983. }
  984. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  985. pm_runtime_put(&card->dev);
  986. mmc_claim_host(host);
  987. return 0;
  988. remove:
  989. mmc_release_host(host);
  990. remove_added:
  991. /*
  992. * The devices are being deleted so it is not necessary to disable
  993. * runtime PM. Similarly we also don't pm_runtime_put() the SDIO card
  994. * because it needs to be active to remove any function devices that
  995. * were probed, and after that it gets deleted.
  996. */
  997. mmc_sdio_remove(host);
  998. mmc_claim_host(host);
  999. err:
  1000. mmc_detach_bus(host);
  1001. pr_err("%s: error %d whilst initialising SDIO card\n",
  1002. mmc_hostname(host), err);
  1003. return err;
  1004. }