sdio.c 26 KB

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