sdio.c 26 KB

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