sdio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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_warning("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
  177. mmc_hostname(card->host), ctrl);
  178. /* set as 4-bit bus width */
  179. ctrl &= ~SDIO_BUS_WIDTH_MASK;
  180. ctrl |= SDIO_BUS_WIDTH_4BIT;
  181. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  182. if (ret)
  183. return ret;
  184. return 1;
  185. }
  186. /*
  187. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  188. * of the card. This may be required on certain setups of boards,
  189. * controllers and embedded sdio device which do not need the card's
  190. * pull-up. As a result, card detection is disabled and power is saved.
  191. */
  192. static int sdio_disable_cd(struct mmc_card *card)
  193. {
  194. int ret;
  195. u8 ctrl;
  196. if (!mmc_card_disable_cd(card))
  197. return 0;
  198. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  199. if (ret)
  200. return ret;
  201. ctrl |= SDIO_BUS_CD_DISABLE;
  202. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  203. }
  204. /*
  205. * Devices that remain active during a system suspend are
  206. * put back into 1-bit mode.
  207. */
  208. static int sdio_disable_wide(struct mmc_card *card)
  209. {
  210. int ret;
  211. u8 ctrl;
  212. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  213. return 0;
  214. if (card->cccr.low_speed && !card->cccr.wide_bus)
  215. return 0;
  216. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  217. if (ret)
  218. return ret;
  219. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  220. return 0;
  221. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  222. ctrl |= SDIO_BUS_ASYNC_INT;
  223. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  224. if (ret)
  225. return ret;
  226. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  227. return 0;
  228. }
  229. static int sdio_enable_4bit_bus(struct mmc_card *card)
  230. {
  231. int err;
  232. if (card->type == MMC_TYPE_SDIO)
  233. return sdio_enable_wide(card);
  234. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  235. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  236. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  237. if (err)
  238. return err;
  239. } else
  240. return 0;
  241. err = sdio_enable_wide(card);
  242. if (err <= 0)
  243. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  244. return err;
  245. }
  246. /*
  247. * Test if the card supports high-speed mode and, if so, switch to it.
  248. */
  249. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  250. {
  251. int ret;
  252. u8 speed;
  253. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  254. return 0;
  255. if (!card->cccr.high_speed)
  256. return 0;
  257. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  258. if (ret)
  259. return ret;
  260. if (enable)
  261. speed |= SDIO_SPEED_EHS;
  262. else
  263. speed &= ~SDIO_SPEED_EHS;
  264. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  265. if (ret)
  266. return ret;
  267. return 1;
  268. }
  269. /*
  270. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  271. */
  272. static int sdio_enable_hs(struct mmc_card *card)
  273. {
  274. int ret;
  275. ret = mmc_sdio_switch_hs(card, true);
  276. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  277. return ret;
  278. ret = mmc_sd_switch_hs(card);
  279. if (ret <= 0)
  280. mmc_sdio_switch_hs(card, false);
  281. return ret;
  282. }
  283. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  284. {
  285. unsigned max_dtr;
  286. if (mmc_card_highspeed(card)) {
  287. /*
  288. * The SDIO specification doesn't mention how
  289. * the CIS transfer speed register relates to
  290. * high-speed, but it seems that 50 MHz is
  291. * mandatory.
  292. */
  293. max_dtr = 50000000;
  294. } else {
  295. max_dtr = card->cis.max_dtr;
  296. }
  297. if (card->type == MMC_TYPE_SD_COMBO)
  298. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  299. return max_dtr;
  300. }
  301. static unsigned char host_drive_to_sdio_drive(int host_strength)
  302. {
  303. switch (host_strength) {
  304. case MMC_SET_DRIVER_TYPE_A:
  305. return SDIO_DTSx_SET_TYPE_A;
  306. case MMC_SET_DRIVER_TYPE_B:
  307. return SDIO_DTSx_SET_TYPE_B;
  308. case MMC_SET_DRIVER_TYPE_C:
  309. return SDIO_DTSx_SET_TYPE_C;
  310. case MMC_SET_DRIVER_TYPE_D:
  311. return SDIO_DTSx_SET_TYPE_D;
  312. default:
  313. return SDIO_DTSx_SET_TYPE_B;
  314. }
  315. }
  316. static void sdio_select_driver_type(struct mmc_card *card)
  317. {
  318. int host_drv_type = SD_DRIVER_TYPE_B;
  319. int card_drv_type = SD_DRIVER_TYPE_B;
  320. int drive_strength;
  321. unsigned char card_strength;
  322. int err;
  323. /*
  324. * If the host doesn't support any of the Driver Types A,C or D,
  325. * or there is no board specific handler then default Driver
  326. * Type B is used.
  327. */
  328. if (!(card->host->caps &
  329. (MMC_CAP_DRIVER_TYPE_A |
  330. MMC_CAP_DRIVER_TYPE_C |
  331. MMC_CAP_DRIVER_TYPE_D)))
  332. return;
  333. if (!card->host->ops->select_drive_strength)
  334. return;
  335. if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
  336. host_drv_type |= SD_DRIVER_TYPE_A;
  337. if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
  338. host_drv_type |= SD_DRIVER_TYPE_C;
  339. if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
  340. host_drv_type |= SD_DRIVER_TYPE_D;
  341. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
  342. card_drv_type |= SD_DRIVER_TYPE_A;
  343. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
  344. card_drv_type |= SD_DRIVER_TYPE_C;
  345. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
  346. card_drv_type |= SD_DRIVER_TYPE_D;
  347. /*
  348. * The drive strength that the hardware can support
  349. * depends on the board design. Pass the appropriate
  350. * information and let the hardware specific code
  351. * return what is possible given the options
  352. */
  353. drive_strength = card->host->ops->select_drive_strength(
  354. card->sw_caps.uhs_max_dtr,
  355. host_drv_type, card_drv_type);
  356. /* if error just use default for drive strength B */
  357. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
  358. &card_strength);
  359. if (err)
  360. return;
  361. card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
  362. card_strength |= host_drive_to_sdio_drive(drive_strength);
  363. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
  364. card_strength, NULL);
  365. /* if error default to drive strength B */
  366. if (!err)
  367. mmc_set_driver_type(card->host, drive_strength);
  368. }
  369. static int sdio_set_bus_speed_mode(struct mmc_card *card)
  370. {
  371. unsigned int bus_speed, timing;
  372. int err;
  373. unsigned char speed;
  374. /*
  375. * If the host doesn't support any of the UHS-I modes, fallback on
  376. * default speed.
  377. */
  378. if (!mmc_host_uhs(card->host))
  379. return 0;
  380. bus_speed = SDIO_SPEED_SDR12;
  381. timing = MMC_TIMING_UHS_SDR12;
  382. if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
  383. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
  384. bus_speed = SDIO_SPEED_SDR104;
  385. timing = MMC_TIMING_UHS_SDR104;
  386. card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
  387. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  388. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  389. bus_speed = SDIO_SPEED_DDR50;
  390. timing = MMC_TIMING_UHS_DDR50;
  391. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  392. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  393. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  394. SD_MODE_UHS_SDR50)) {
  395. bus_speed = SDIO_SPEED_SDR50;
  396. timing = MMC_TIMING_UHS_SDR50;
  397. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  398. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  399. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  400. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  401. bus_speed = SDIO_SPEED_SDR25;
  402. timing = MMC_TIMING_UHS_SDR25;
  403. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  404. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  405. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  406. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  407. SD_MODE_UHS_SDR12)) {
  408. bus_speed = SDIO_SPEED_SDR12;
  409. timing = MMC_TIMING_UHS_SDR12;
  410. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  411. }
  412. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  413. if (err)
  414. return err;
  415. speed &= ~SDIO_SPEED_BSS_MASK;
  416. speed |= bus_speed;
  417. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  418. if (err)
  419. return err;
  420. if (bus_speed) {
  421. mmc_set_timing(card->host, timing);
  422. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  423. }
  424. return 0;
  425. }
  426. /*
  427. * UHS-I specific initialization procedure
  428. */
  429. static int mmc_sdio_init_uhs_card(struct mmc_card *card)
  430. {
  431. int err;
  432. if (!card->scr.sda_spec3)
  433. return 0;
  434. /*
  435. * Switch to wider bus (if supported).
  436. */
  437. if (card->host->caps & MMC_CAP_4_BIT_DATA) {
  438. err = sdio_enable_4bit_bus(card);
  439. if (err > 0) {
  440. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  441. err = 0;
  442. }
  443. }
  444. /* Set the driver strength for the card */
  445. sdio_select_driver_type(card);
  446. /* Set bus speed mode of the card */
  447. err = sdio_set_bus_speed_mode(card);
  448. if (err)
  449. goto out;
  450. /* Initialize and start re-tuning timer */
  451. if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
  452. err = card->host->ops->execute_tuning(card->host,
  453. MMC_SEND_TUNING_BLOCK);
  454. out:
  455. return err;
  456. }
  457. /*
  458. * Handle the detection and initialisation of a card.
  459. *
  460. * In the case of a resume, "oldcard" will contain the card
  461. * we're trying to reinitialise.
  462. */
  463. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  464. struct mmc_card *oldcard, int powered_resume)
  465. {
  466. struct mmc_card *card;
  467. int err;
  468. BUG_ON(!host);
  469. WARN_ON(!host->claimed);
  470. /*
  471. * Inform the card of the voltage
  472. */
  473. if (!powered_resume) {
  474. err = mmc_send_io_op_cond(host, host->ocr, &ocr);
  475. if (err)
  476. goto err;
  477. }
  478. /*
  479. * For SPI, enable CRC as appropriate.
  480. */
  481. if (mmc_host_is_spi(host)) {
  482. err = mmc_spi_set_crc(host, use_spi_crc);
  483. if (err)
  484. goto err;
  485. }
  486. /*
  487. * Allocate card structure.
  488. */
  489. card = mmc_alloc_card(host, NULL);
  490. if (IS_ERR(card)) {
  491. err = PTR_ERR(card);
  492. goto err;
  493. }
  494. if ((ocr & R4_MEMORY_PRESENT) &&
  495. mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
  496. card->type = MMC_TYPE_SD_COMBO;
  497. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  498. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  499. mmc_remove_card(card);
  500. return -ENOENT;
  501. }
  502. } else {
  503. card->type = MMC_TYPE_SDIO;
  504. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  505. mmc_remove_card(card);
  506. return -ENOENT;
  507. }
  508. }
  509. /*
  510. * Call the optional HC's init_card function to handle quirks.
  511. */
  512. if (host->ops->init_card)
  513. host->ops->init_card(host, card);
  514. /*
  515. * If the host and card support UHS-I mode request the card
  516. * to switch to 1.8V signaling level. No 1.8v signalling if
  517. * UHS mode is not enabled to maintain compatibility and some
  518. * systems that claim 1.8v signalling in fact do not support
  519. * it.
  520. */
  521. if ((ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) {
  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, ocr);
  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. sdio_reset(host);
  769. mmc_go_idle(host);
  770. err = mmc_sdio_init_card(host, host->ocr, host->card,
  771. mmc_card_keep_power(host));
  772. } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  773. /* We may have switched to 1-bit mode during suspend */
  774. err = sdio_enable_4bit_bus(host->card);
  775. if (err > 0) {
  776. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  777. err = 0;
  778. }
  779. }
  780. if (!err && host->sdio_irqs)
  781. wake_up_process(host->sdio_irq_thread);
  782. mmc_release_host(host);
  783. /*
  784. * If the card looked to be the same as before suspending, then
  785. * we proceed to resume all card functions. If one of them returns
  786. * an error then we simply return that error to the core and the
  787. * card will be redetected as new. It is the responsibility of
  788. * the function driver to perform further tests with the extra
  789. * knowledge it has of the card to confirm the card is indeed the
  790. * same as before suspending (same MAC address for network cards,
  791. * etc.) and return an error otherwise.
  792. */
  793. for (i = 0; !err && i < host->card->sdio_funcs; i++) {
  794. struct sdio_func *func = host->card->sdio_func[i];
  795. if (func && sdio_func_present(func) && func->dev.driver) {
  796. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  797. err = pmops->resume(&func->dev);
  798. }
  799. }
  800. return err;
  801. }
  802. static int mmc_sdio_power_restore(struct mmc_host *host)
  803. {
  804. int ret;
  805. u32 ocr;
  806. BUG_ON(!host);
  807. BUG_ON(!host->card);
  808. mmc_claim_host(host);
  809. /*
  810. * Reset the card by performing the same steps that are taken by
  811. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  812. *
  813. * sdio_reset() is technically not needed. Having just powered up the
  814. * hardware, it should already be in reset state. However, some
  815. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  816. * meaning that a reset is required when restoring power soon after
  817. * powering off. It is harmless in other cases.
  818. *
  819. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  820. * is not necessary for non-removable cards. However, it is required
  821. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  822. * harmless in other situations.
  823. *
  824. * With these steps taken, mmc_select_voltage() is also required to
  825. * restore the correct voltage setting of the card.
  826. */
  827. sdio_reset(host);
  828. mmc_go_idle(host);
  829. mmc_send_if_cond(host, host->ocr_avail);
  830. ret = mmc_send_io_op_cond(host, 0, &ocr);
  831. if (ret)
  832. goto out;
  833. if (host->ocr_avail_sdio)
  834. host->ocr_avail = host->ocr_avail_sdio;
  835. host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
  836. if (!host->ocr) {
  837. ret = -EINVAL;
  838. goto out;
  839. }
  840. if (mmc_host_uhs(host))
  841. /* to query card if 1.8V signalling is supported */
  842. host->ocr |= R4_18V_PRESENT;
  843. ret = mmc_sdio_init_card(host, host->ocr, host->card,
  844. mmc_card_keep_power(host));
  845. if (!ret && host->sdio_irqs)
  846. mmc_signal_sdio_irq(host);
  847. out:
  848. mmc_release_host(host);
  849. return ret;
  850. }
  851. static const struct mmc_bus_ops mmc_sdio_ops = {
  852. .remove = mmc_sdio_remove,
  853. .detect = mmc_sdio_detect,
  854. .suspend = mmc_sdio_suspend,
  855. .resume = mmc_sdio_resume,
  856. .power_restore = mmc_sdio_power_restore,
  857. .alive = mmc_sdio_alive,
  858. };
  859. /*
  860. * Starting point for SDIO card init.
  861. */
  862. int mmc_attach_sdio(struct mmc_host *host)
  863. {
  864. int err, i, funcs;
  865. u32 ocr;
  866. struct mmc_card *card;
  867. BUG_ON(!host);
  868. WARN_ON(!host->claimed);
  869. err = mmc_send_io_op_cond(host, 0, &ocr);
  870. if (err)
  871. return err;
  872. mmc_attach_bus(host, &mmc_sdio_ops);
  873. if (host->ocr_avail_sdio)
  874. host->ocr_avail = host->ocr_avail_sdio;
  875. /*
  876. * Sanity check the voltages that the card claims to
  877. * support.
  878. */
  879. if (ocr & 0x7F) {
  880. pr_warning("%s: card claims to support voltages "
  881. "below the defined range. These will be ignored.\n",
  882. mmc_hostname(host));
  883. ocr &= ~0x7F;
  884. }
  885. host->ocr = mmc_select_voltage(host, ocr);
  886. /*
  887. * Can we support the voltage(s) of the card(s)?
  888. */
  889. if (!host->ocr) {
  890. err = -EINVAL;
  891. goto err;
  892. }
  893. /*
  894. * Detect and init the card.
  895. */
  896. if (mmc_host_uhs(host))
  897. /* to query card if 1.8V signalling is supported */
  898. host->ocr |= R4_18V_PRESENT;
  899. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  900. if (err) {
  901. if (err == -EAGAIN) {
  902. /*
  903. * Retry initialization with S18R set to 0.
  904. */
  905. host->ocr &= ~R4_18V_PRESENT;
  906. err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
  907. }
  908. if (err)
  909. goto err;
  910. }
  911. card = host->card;
  912. /*
  913. * Enable runtime PM only if supported by host+card+board
  914. */
  915. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  916. /*
  917. * Let runtime PM core know our card is active
  918. */
  919. err = pm_runtime_set_active(&card->dev);
  920. if (err)
  921. goto remove;
  922. /*
  923. * Enable runtime PM for this card
  924. */
  925. pm_runtime_enable(&card->dev);
  926. }
  927. /*
  928. * The number of functions on the card is encoded inside
  929. * the ocr.
  930. */
  931. funcs = (ocr & 0x70000000) >> 28;
  932. card->sdio_funcs = 0;
  933. /*
  934. * Initialize (but don't add) all present functions.
  935. */
  936. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  937. err = sdio_init_func(host->card, i + 1);
  938. if (err)
  939. goto remove;
  940. /*
  941. * Enable Runtime PM for this func (if supported)
  942. */
  943. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  944. pm_runtime_enable(&card->sdio_func[i]->dev);
  945. }
  946. /*
  947. * First add the card to the driver model...
  948. */
  949. mmc_release_host(host);
  950. err = mmc_add_card(host->card);
  951. if (err)
  952. goto remove_added;
  953. /*
  954. * ...then the SDIO functions.
  955. */
  956. for (i = 0;i < funcs;i++) {
  957. err = sdio_add_func(host->card->sdio_func[i]);
  958. if (err)
  959. goto remove_added;
  960. }
  961. mmc_claim_host(host);
  962. return 0;
  963. remove_added:
  964. /* Remove without lock if the device has been added. */
  965. mmc_sdio_remove(host);
  966. mmc_claim_host(host);
  967. remove:
  968. /* And with lock if it hasn't been added. */
  969. mmc_release_host(host);
  970. if (host->card)
  971. mmc_sdio_remove(host);
  972. mmc_claim_host(host);
  973. err:
  974. mmc_detach_bus(host);
  975. pr_err("%s: error %d whilst initialising SDIO card\n",
  976. mmc_hostname(host), err);
  977. return err;
  978. }