sdio.c 26 KB

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