sdio.c 26 KB

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