sdio.c 27 KB

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