sdio.c 30 KB

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