sdio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. BUG_ON(fn > SDIO_MAX_FUNCS);
  55. func = sdio_alloc_func(card);
  56. if (IS_ERR(func))
  57. return PTR_ERR(func);
  58. func->num = fn;
  59. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  60. ret = sdio_read_fbr(func);
  61. if (ret)
  62. goto fail;
  63. ret = sdio_read_func_cis(func);
  64. if (ret)
  65. goto fail;
  66. } else {
  67. func->vendor = func->card->cis.vendor;
  68. func->device = func->card->cis.device;
  69. func->max_blksize = func->card->cis.blksize;
  70. }
  71. card->sdio_func[fn - 1] = func;
  72. return 0;
  73. fail:
  74. /*
  75. * It is okay to remove the function here even though we hold
  76. * the host lock as we haven't registered the device yet.
  77. */
  78. sdio_remove_func(func);
  79. return ret;
  80. }
  81. static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
  82. {
  83. int ret;
  84. int cccr_vsn;
  85. int uhs = ocr & R4_18V_PRESENT;
  86. unsigned char data;
  87. unsigned char speed;
  88. memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  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 (card->host->caps &
  129. (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  130. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  131. MMC_CAP_UHS_DDR50)) {
  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_warning("%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. return sdio_enable_wide(card);
  237. 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. } else
  243. return 0;
  244. err = sdio_enable_wide(card);
  245. if (err <= 0)
  246. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  247. return err;
  248. }
  249. /*
  250. * Test if the card supports high-speed mode and, if so, switch to it.
  251. */
  252. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  253. {
  254. int ret;
  255. u8 speed;
  256. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  257. return 0;
  258. if (!card->cccr.high_speed)
  259. return 0;
  260. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  261. if (ret)
  262. return ret;
  263. if (enable)
  264. speed |= SDIO_SPEED_EHS;
  265. else
  266. speed &= ~SDIO_SPEED_EHS;
  267. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  268. if (ret)
  269. return ret;
  270. return 1;
  271. }
  272. /*
  273. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  274. */
  275. static int sdio_enable_hs(struct mmc_card *card)
  276. {
  277. int ret;
  278. ret = mmc_sdio_switch_hs(card, true);
  279. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  280. return ret;
  281. ret = mmc_sd_switch_hs(card);
  282. if (ret <= 0)
  283. mmc_sdio_switch_hs(card, false);
  284. return ret;
  285. }
  286. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  287. {
  288. unsigned max_dtr;
  289. if (mmc_card_highspeed(card)) {
  290. /*
  291. * The SDIO specification doesn't mention how
  292. * the CIS transfer speed register relates to
  293. * high-speed, but it seems that 50 MHz is
  294. * mandatory.
  295. */
  296. max_dtr = 50000000;
  297. } else {
  298. max_dtr = card->cis.max_dtr;
  299. }
  300. if (card->type == MMC_TYPE_SD_COMBO)
  301. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  302. return max_dtr;
  303. }
  304. static unsigned char host_drive_to_sdio_drive(int host_strength)
  305. {
  306. switch (host_strength) {
  307. case MMC_SET_DRIVER_TYPE_A:
  308. return SDIO_DTSx_SET_TYPE_A;
  309. case MMC_SET_DRIVER_TYPE_B:
  310. return SDIO_DTSx_SET_TYPE_B;
  311. case MMC_SET_DRIVER_TYPE_C:
  312. return SDIO_DTSx_SET_TYPE_C;
  313. case MMC_SET_DRIVER_TYPE_D:
  314. return SDIO_DTSx_SET_TYPE_D;
  315. default:
  316. return SDIO_DTSx_SET_TYPE_B;
  317. }
  318. }
  319. static void sdio_select_driver_type(struct mmc_card *card)
  320. {
  321. int host_drv_type = SD_DRIVER_TYPE_B;
  322. int card_drv_type = SD_DRIVER_TYPE_B;
  323. int drive_strength;
  324. unsigned char card_strength;
  325. int err;
  326. /*
  327. * If the host doesn't support any of the Driver Types A,C or D,
  328. * or there is no board specific handler then default Driver
  329. * Type B is used.
  330. */
  331. if (!(card->host->caps &
  332. (MMC_CAP_DRIVER_TYPE_A |
  333. MMC_CAP_DRIVER_TYPE_C |
  334. MMC_CAP_DRIVER_TYPE_D)))
  335. return;
  336. if (!card->host->ops->select_drive_strength)
  337. return;
  338. if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
  339. host_drv_type |= SD_DRIVER_TYPE_A;
  340. if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
  341. host_drv_type |= SD_DRIVER_TYPE_C;
  342. if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
  343. host_drv_type |= SD_DRIVER_TYPE_D;
  344. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
  345. card_drv_type |= SD_DRIVER_TYPE_A;
  346. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
  347. card_drv_type |= SD_DRIVER_TYPE_C;
  348. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
  349. card_drv_type |= SD_DRIVER_TYPE_D;
  350. /*
  351. * The drive strength that the hardware can support
  352. * depends on the board design. Pass the appropriate
  353. * information and let the hardware specific code
  354. * return what is possible given the options
  355. */
  356. drive_strength = card->host->ops->select_drive_strength(
  357. card->sw_caps.uhs_max_dtr,
  358. host_drv_type, card_drv_type);
  359. /* if error just use default for drive strength B */
  360. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
  361. &card_strength);
  362. if (err)
  363. return;
  364. card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
  365. card_strength |= host_drive_to_sdio_drive(drive_strength);
  366. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
  367. card_strength, NULL);
  368. /* if error default to drive strength B */
  369. if (!err)
  370. mmc_set_driver_type(card->host, drive_strength);
  371. }
  372. static int sdio_set_bus_speed_mode(struct mmc_card *card)
  373. {
  374. unsigned int bus_speed, timing;
  375. int err;
  376. unsigned char speed;
  377. /*
  378. * If the host doesn't support any of the UHS-I modes, fallback on
  379. * default speed.
  380. */
  381. if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  382. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
  383. return 0;
  384. bus_speed = SDIO_SPEED_SDR12;
  385. timing = MMC_TIMING_UHS_SDR12;
  386. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  387. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  388. bus_speed = SDIO_SPEED_SDR104;
  389. timing = MMC_TIMING_UHS_SDR104;
  390. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  391. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  392. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  393. bus_speed = SDIO_SPEED_DDR50;
  394. timing = MMC_TIMING_UHS_DDR50;
  395. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  396. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  397. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  398. SD_MODE_UHS_SDR50)) {
  399. bus_speed = SDIO_SPEED_SDR50;
  400. timing = MMC_TIMING_UHS_SDR50;
  401. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  402. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  403. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  404. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  405. bus_speed = SDIO_SPEED_SDR25;
  406. timing = MMC_TIMING_UHS_SDR25;
  407. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  408. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  409. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  410. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  411. SD_MODE_UHS_SDR12)) {
  412. bus_speed = SDIO_SPEED_SDR12;
  413. timing = MMC_TIMING_UHS_SDR12;
  414. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  415. }
  416. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  417. if (err)
  418. return err;
  419. speed &= ~SDIO_SPEED_BSS_MASK;
  420. speed |= bus_speed;
  421. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  422. if (err)
  423. return err;
  424. if (bus_speed) {
  425. mmc_set_timing(card->host, timing);
  426. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  427. }
  428. return 0;
  429. }
  430. /*
  431. * UHS-I specific initialization procedure
  432. */
  433. static int mmc_sdio_init_uhs_card(struct mmc_card *card)
  434. {
  435. int err;
  436. if (!card->scr.sda_spec3)
  437. return 0;
  438. /*
  439. * Switch to wider bus (if supported).
  440. */
  441. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  442. err = sdio_enable_4bit_bus(card);
  443. if (err > 0) {
  444. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  445. err = 0;
  446. }
  447. }
  448. /* Set the driver strength for the card */
  449. sdio_select_driver_type(card);
  450. /* Set bus speed mode of the card */
  451. err = sdio_set_bus_speed_mode(card);
  452. if (err)
  453. goto out;
  454. /* Initialize and start re-tuning timer */
  455. if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
  456. err = card->host->ops->execute_tuning(card->host,
  457. MMC_SEND_TUNING_BLOCK);
  458. out:
  459. return err;
  460. }
  461. /*
  462. * Handle the detection and initialisation of a card.
  463. *
  464. * In the case of a resume, "oldcard" will contain the card
  465. * we're trying to reinitialise.
  466. */
  467. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  468. struct mmc_card *oldcard, int powered_resume)
  469. {
  470. struct mmc_card *card;
  471. int err;
  472. BUG_ON(!host);
  473. WARN_ON(!host->claimed);
  474. /*
  475. * Inform the card of the voltage
  476. */
  477. if (!powered_resume) {
  478. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  479. if (err)
  480. goto err;
  481. }
  482. /*
  483. * For SPI, enable CRC as appropriate.
  484. */
  485. if (mmc_host_is_spi(host)) {
  486. err = mmc_spi_set_crc(host, use_spi_crc);
  487. if (err)
  488. goto err;
  489. }
  490. /*
  491. * Allocate card structure.
  492. */
  493. card = mmc_alloc_card(host, NULL);
  494. if (IS_ERR(card)) {
  495. err = PTR_ERR(card);
  496. goto err;
  497. }
  498. if ((ocr & R4_MEMORY_PRESENT) &&
  499. mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
  500. card->type = MMC_TYPE_SD_COMBO;
  501. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  502. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  503. mmc_remove_card(card);
  504. return -ENOENT;
  505. }
  506. } else {
  507. card->type = MMC_TYPE_SDIO;
  508. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  509. mmc_remove_card(card);
  510. return -ENOENT;
  511. }
  512. }
  513. /*
  514. * Call the optional HC's init_card function to handle quirks.
  515. */
  516. if (host->ops->init_card)
  517. host->ops->init_card(host, card);
  518. /*
  519. * If the host and card support UHS-I mode request the card
  520. * to switch to 1.8V signaling level. No 1.8v signalling if
  521. * UHS mode is not enabled to maintain compatibilty and some
  522. * systems that claim 1.8v signalling in fact do not support
  523. * it.
  524. */
  525. if ((ocr & R4_18V_PRESENT) &&
  526. (host->caps &
  527. (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  528. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  529. MMC_CAP_UHS_DDR50))) {
  530. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
  531. true);
  532. if (err) {
  533. ocr &= ~R4_18V_PRESENT;
  534. host->ocr &= ~R4_18V_PRESENT;
  535. }
  536. err = 0;
  537. } else {
  538. ocr &= ~R4_18V_PRESENT;
  539. host->ocr &= ~R4_18V_PRESENT;
  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_card_set_highspeed(card);
  583. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  584. }
  585. goto finish;
  586. }
  587. /*
  588. * Read the common registers.
  589. */
  590. err = sdio_read_cccr(card, ocr);
  591. if (err)
  592. goto remove;
  593. /*
  594. * Read the common CIS tuples.
  595. */
  596. err = sdio_read_common_cis(card);
  597. if (err)
  598. goto remove;
  599. if (oldcard) {
  600. int same = (card->cis.vendor == oldcard->cis.vendor &&
  601. card->cis.device == oldcard->cis.device);
  602. mmc_remove_card(card);
  603. if (!same)
  604. return -ENOENT;
  605. card = oldcard;
  606. }
  607. mmc_fixup_device(card, NULL);
  608. if (card->type == MMC_TYPE_SD_COMBO) {
  609. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  610. /* handle as SDIO-only card if memory init failed */
  611. if (err) {
  612. mmc_go_idle(host);
  613. if (mmc_host_is_spi(host))
  614. /* should not fail, as it worked previously */
  615. mmc_spi_set_crc(host, use_spi_crc);
  616. card->type = MMC_TYPE_SDIO;
  617. } else
  618. card->dev.type = &sd_type;
  619. }
  620. /*
  621. * If needed, disconnect card detection pull-up resistor.
  622. */
  623. err = sdio_disable_cd(card);
  624. if (err)
  625. goto remove;
  626. /* Initialization sequence for UHS-I cards */
  627. /* Only if card supports 1.8v and UHS signaling */
  628. if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  629. err = mmc_sdio_init_uhs_card(card);
  630. if (err)
  631. goto remove;
  632. /* Card is an ultra-high-speed card */
  633. mmc_card_set_uhs(card);
  634. } else {
  635. /*
  636. * Switch to high-speed (if supported).
  637. */
  638. err = sdio_enable_hs(card);
  639. if (err > 0)
  640. mmc_sd_go_highspeed(card);
  641. else if (err)
  642. goto remove;
  643. /*
  644. * Change to the card's maximum speed.
  645. */
  646. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  647. /*
  648. * Switch to wider bus (if supported).
  649. */
  650. err = sdio_enable_4bit_bus(card);
  651. if (err > 0)
  652. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  653. else if (err)
  654. goto remove;
  655. }
  656. finish:
  657. if (!oldcard)
  658. host->card = card;
  659. return 0;
  660. remove:
  661. if (!oldcard)
  662. mmc_remove_card(card);
  663. err:
  664. return err;
  665. }
  666. /*
  667. * Host is being removed. Free up the current card.
  668. */
  669. static void mmc_sdio_remove(struct mmc_host *host)
  670. {
  671. int i;
  672. BUG_ON(!host);
  673. BUG_ON(!host->card);
  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. BUG_ON(!host);
  697. BUG_ON(!host->card);
  698. /* Make sure card is powered before detecting it */
  699. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  700. err = pm_runtime_get_sync(&host->card->dev);
  701. if (err < 0)
  702. goto out;
  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 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_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. } else
  748. err = pmops->suspend(&func->dev);
  749. if (err)
  750. break;
  751. }
  752. }
  753. while (err && --i >= 0) {
  754. struct sdio_func *func = host->card->sdio_func[i];
  755. if (func && sdio_func_present(func) && func->dev.driver) {
  756. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  757. pmops->resume(&func->dev);
  758. }
  759. }
  760. if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  761. mmc_claim_host(host);
  762. sdio_disable_wide(host->card);
  763. mmc_release_host(host);
  764. }
  765. return err;
  766. }
  767. static int mmc_sdio_resume(struct mmc_host *host)
  768. {
  769. int i, err = 0;
  770. BUG_ON(!host);
  771. BUG_ON(!host->card);
  772. /* Basic card reinitialization. */
  773. mmc_claim_host(host);
  774. /* No need to reinitialize powered-resumed nonremovable cards */
  775. if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
  776. err = mmc_sdio_init_card(host, host->ocr, host->card,
  777. mmc_card_keep_power(host));
  778. else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  779. /* We may have switched to 1-bit mode during suspend */
  780. err = sdio_enable_4bit_bus(host->card);
  781. if (err > 0) {
  782. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  783. err = 0;
  784. }
  785. }
  786. if (!err && host->sdio_irqs)
  787. wake_up_process(host->sdio_irq_thread);
  788. mmc_release_host(host);
  789. /*
  790. * If the card looked to be the same as before suspending, then
  791. * we proceed to resume all card functions. If one of them returns
  792. * an error then we simply return that error to the core and the
  793. * card will be redetected as new. It is the responsibility of
  794. * the function driver to perform further tests with the extra
  795. * knowledge it has of the card to confirm the card is indeed the
  796. * same as before suspending (same MAC address for network cards,
  797. * etc.) and return an error otherwise.
  798. */
  799. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  800. struct sdio_func *func = host->card->sdio_func[i];
  801. if (func && sdio_func_present(func) && func->dev.driver) {
  802. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  803. err = pmops->resume(&func->dev);
  804. }
  805. }
  806. return err;
  807. }
  808. static int mmc_sdio_power_restore(struct mmc_host *host)
  809. {
  810. int ret;
  811. u32 ocr;
  812. BUG_ON(!host);
  813. BUG_ON(!host->card);
  814. mmc_claim_host(host);
  815. /*
  816. * Reset the card by performing the same steps that are taken by
  817. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  818. *
  819. * sdio_reset() is technically not needed. Having just powered up the
  820. * hardware, it should already be in reset state. However, some
  821. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  822. * meaning that a reset is required when restoring power soon after
  823. * powering off. It is harmless in other cases.
  824. *
  825. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  826. * is not necessary for non-removable cards. However, it is required
  827. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  828. * harmless in other situations.
  829. *
  830. * With these steps taken, mmc_select_voltage() is also required to
  831. * restore the correct voltage setting of the card.
  832. */
  833. sdio_reset(host);
  834. mmc_go_idle(host);
  835. mmc_send_if_cond(host, host->ocr_avail);
  836. ret = mmc_send_io_op_cond(host, 0, &ocr);
  837. if (ret)
  838. goto out;
  839. if (host->ocr_avail_sdio)
  840. host->ocr_avail = host->ocr_avail_sdio;
  841. host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
  842. if (!host->ocr) {
  843. ret = -EINVAL;
  844. goto out;
  845. }
  846. ret = mmc_sdio_init_card(host, host->ocr, host->card,
  847. mmc_card_keep_power(host));
  848. if (!ret && host->sdio_irqs)
  849. mmc_signal_sdio_irq(host);
  850. out:
  851. mmc_release_host(host);
  852. return ret;
  853. }
  854. static const struct mmc_bus_ops mmc_sdio_ops = {
  855. .remove = mmc_sdio_remove,
  856. .detect = mmc_sdio_detect,
  857. .suspend = mmc_sdio_suspend,
  858. .resume = mmc_sdio_resume,
  859. .power_restore = mmc_sdio_power_restore,
  860. .alive = mmc_sdio_alive,
  861. };
  862. /*
  863. * Starting point for SDIO card init.
  864. */
  865. int mmc_attach_sdio(struct mmc_host *host)
  866. {
  867. int err, i, funcs;
  868. u32 ocr;
  869. struct mmc_card *card;
  870. BUG_ON(!host);
  871. WARN_ON(!host->claimed);
  872. err = mmc_send_io_op_cond(host, 0, &ocr);
  873. if (err)
  874. return err;
  875. mmc_attach_bus(host, &mmc_sdio_ops);
  876. if (host->ocr_avail_sdio)
  877. host->ocr_avail = host->ocr_avail_sdio;
  878. /*
  879. * Sanity check the voltages that the card claims to
  880. * support.
  881. */
  882. if (ocr & 0x7F) {
  883. pr_warning("%s: card claims to support voltages "
  884. "below the defined range. These will be ignored.\n",
  885. mmc_hostname(host));
  886. ocr &= ~0x7F;
  887. }
  888. host->ocr = mmc_select_voltage(host, ocr);
  889. /*
  890. * Can we support the voltage(s) of the card(s)?
  891. */
  892. if (!host->ocr) {
  893. err = -EINVAL;
  894. goto err;
  895. }
  896. /*
  897. * Detect and init the card.
  898. */
  899. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  900. if (err) {
  901. if (err == -EAGAIN) {
  902. /*
  903. * Retry initialization with S18R set to 0.
  904. */
  905. host->ocr &= ~R4_18V_PRESENT;
  906. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  907. }
  908. if (err)
  909. goto err;
  910. }
  911. card = host->card;
  912. /*
  913. * Enable runtime PM only if supported by host+card+board
  914. */
  915. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  916. /*
  917. * Let runtime PM core know our card is active
  918. */
  919. err = pm_runtime_set_active(&card->dev);
  920. if (err)
  921. goto remove;
  922. /*
  923. * Enable runtime PM for this card
  924. */
  925. pm_runtime_enable(&card->dev);
  926. }
  927. /*
  928. * The number of functions on the card is encoded inside
  929. * the ocr.
  930. */
  931. funcs = (ocr & 0x70000000) >> 28;
  932. card->sdio_funcs = 0;
  933. /*
  934. * Initialize (but don't add) all present functions.
  935. */
  936. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  937. err = sdio_init_func(host->card, i + 1);
  938. if (err)
  939. goto remove;
  940. /*
  941. * Enable Runtime PM for this func (if supported)
  942. */
  943. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  944. pm_runtime_enable(&card->sdio_func[i]->dev);
  945. }
  946. /*
  947. * First add the card to the driver model...
  948. */
  949. mmc_release_host(host);
  950. err = mmc_add_card(host->card);
  951. if (err)
  952. goto remove_added;
  953. /*
  954. * ...then the SDIO functions.
  955. */
  956. for (i = 0;i < funcs;i++) {
  957. err = sdio_add_func(host->card->sdio_func[i]);
  958. if (err)
  959. goto remove_added;
  960. }
  961. mmc_claim_host(host);
  962. return 0;
  963. remove_added:
  964. /* Remove without lock if the device has been added. */
  965. mmc_sdio_remove(host);
  966. mmc_claim_host(host);
  967. remove:
  968. /* And with lock if it hasn't been added. */
  969. mmc_release_host(host);
  970. if (host->card)
  971. mmc_sdio_remove(host);
  972. mmc_claim_host(host);
  973. err:
  974. mmc_detach_bus(host);
  975. pr_err("%s: error %d whilst initialising SDIO card\n",
  976. mmc_hostname(host), err);
  977. return err;
  978. }