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