sdio.c 27 KB

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