sdio.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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_warn("%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. err = sdio_enable_wide(card);
  234. else 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. err = sdio_enable_wide(card);
  240. if (err <= 0)
  241. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  242. } else
  243. return 0;
  244. if (err > 0) {
  245. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  246. err = 0;
  247. }
  248. return err;
  249. }
  250. /*
  251. * Test if the card supports high-speed mode and, if so, switch to it.
  252. */
  253. static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
  254. {
  255. int ret;
  256. u8 speed;
  257. if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
  258. return 0;
  259. if (!card->cccr.high_speed)
  260. return 0;
  261. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  262. if (ret)
  263. return ret;
  264. if (enable)
  265. speed |= SDIO_SPEED_EHS;
  266. else
  267. speed &= ~SDIO_SPEED_EHS;
  268. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  269. if (ret)
  270. return ret;
  271. return 1;
  272. }
  273. /*
  274. * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
  275. */
  276. static int sdio_enable_hs(struct mmc_card *card)
  277. {
  278. int ret;
  279. ret = mmc_sdio_switch_hs(card, true);
  280. if (ret <= 0 || card->type == MMC_TYPE_SDIO)
  281. return ret;
  282. ret = mmc_sd_switch_hs(card);
  283. if (ret <= 0)
  284. mmc_sdio_switch_hs(card, false);
  285. return ret;
  286. }
  287. static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
  288. {
  289. unsigned max_dtr;
  290. if (mmc_card_hs(card)) {
  291. /*
  292. * The SDIO specification doesn't mention how
  293. * the CIS transfer speed register relates to
  294. * high-speed, but it seems that 50 MHz is
  295. * mandatory.
  296. */
  297. max_dtr = 50000000;
  298. } else {
  299. max_dtr = card->cis.max_dtr;
  300. }
  301. if (card->type == MMC_TYPE_SD_COMBO)
  302. max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
  303. return max_dtr;
  304. }
  305. static unsigned char host_drive_to_sdio_drive(int host_strength)
  306. {
  307. switch (host_strength) {
  308. case MMC_SET_DRIVER_TYPE_A:
  309. return SDIO_DTSx_SET_TYPE_A;
  310. case MMC_SET_DRIVER_TYPE_B:
  311. return SDIO_DTSx_SET_TYPE_B;
  312. case MMC_SET_DRIVER_TYPE_C:
  313. return SDIO_DTSx_SET_TYPE_C;
  314. case MMC_SET_DRIVER_TYPE_D:
  315. return SDIO_DTSx_SET_TYPE_D;
  316. default:
  317. return SDIO_DTSx_SET_TYPE_B;
  318. }
  319. }
  320. static void sdio_select_driver_type(struct mmc_card *card)
  321. {
  322. int host_drv_type = SD_DRIVER_TYPE_B;
  323. int card_drv_type = SD_DRIVER_TYPE_B;
  324. int drive_strength;
  325. unsigned char card_strength;
  326. int err;
  327. /*
  328. * If the host doesn't support any of the Driver Types A,C or D,
  329. * or there is no board specific handler then default Driver
  330. * Type B is used.
  331. */
  332. if (!(card->host->caps &
  333. (MMC_CAP_DRIVER_TYPE_A |
  334. MMC_CAP_DRIVER_TYPE_C |
  335. MMC_CAP_DRIVER_TYPE_D)))
  336. return;
  337. if (!card->host->ops->select_drive_strength)
  338. return;
  339. if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
  340. host_drv_type |= SD_DRIVER_TYPE_A;
  341. if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
  342. host_drv_type |= SD_DRIVER_TYPE_C;
  343. if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
  344. host_drv_type |= SD_DRIVER_TYPE_D;
  345. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
  346. card_drv_type |= SD_DRIVER_TYPE_A;
  347. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
  348. card_drv_type |= SD_DRIVER_TYPE_C;
  349. if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
  350. card_drv_type |= SD_DRIVER_TYPE_D;
  351. /*
  352. * The drive strength that the hardware can support
  353. * depends on the board design. Pass the appropriate
  354. * information and let the hardware specific code
  355. * return what is possible given the options
  356. */
  357. drive_strength = card->host->ops->select_drive_strength(
  358. card->sw_caps.uhs_max_dtr,
  359. host_drv_type, card_drv_type);
  360. /* if error just use default for drive strength B */
  361. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
  362. &card_strength);
  363. if (err)
  364. return;
  365. card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
  366. card_strength |= host_drive_to_sdio_drive(drive_strength);
  367. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
  368. card_strength, NULL);
  369. /* if error default to drive strength B */
  370. if (!err)
  371. mmc_set_driver_type(card->host, drive_strength);
  372. }
  373. static int sdio_set_bus_speed_mode(struct mmc_card *card)
  374. {
  375. unsigned int bus_speed, timing;
  376. int err;
  377. unsigned char speed;
  378. /*
  379. * If the host doesn't support any of the UHS-I modes, fallback on
  380. * default speed.
  381. */
  382. if (!mmc_host_uhs(card->host))
  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. card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
  392. } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
  393. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
  394. bus_speed = SDIO_SPEED_DDR50;
  395. timing = MMC_TIMING_UHS_DDR50;
  396. card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
  397. card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
  398. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  399. MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
  400. SD_MODE_UHS_SDR50)) {
  401. bus_speed = SDIO_SPEED_SDR50;
  402. timing = MMC_TIMING_UHS_SDR50;
  403. card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
  404. card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
  405. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  406. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
  407. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
  408. bus_speed = SDIO_SPEED_SDR25;
  409. timing = MMC_TIMING_UHS_SDR25;
  410. card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
  411. card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
  412. } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
  413. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
  414. MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
  415. SD_MODE_UHS_SDR12)) {
  416. bus_speed = SDIO_SPEED_SDR12;
  417. timing = MMC_TIMING_UHS_SDR12;
  418. card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
  419. card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
  420. }
  421. err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  422. if (err)
  423. return err;
  424. speed &= ~SDIO_SPEED_BSS_MASK;
  425. speed |= bus_speed;
  426. err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
  427. if (err)
  428. return err;
  429. if (bus_speed) {
  430. mmc_set_timing(card->host, timing);
  431. mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
  432. }
  433. return 0;
  434. }
  435. /*
  436. * UHS-I specific initialization procedure
  437. */
  438. static int mmc_sdio_init_uhs_card(struct mmc_card *card)
  439. {
  440. int err;
  441. if (!card->scr.sda_spec3)
  442. return 0;
  443. /*
  444. * Switch to wider bus (if supported).
  445. */
  446. if (card->host->caps & MMC_CAP_4_BIT_DATA)
  447. err = sdio_enable_4bit_bus(card);
  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. /*
  455. * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
  456. * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
  457. */
  458. if (!mmc_host_is_spi(card->host) &&
  459. ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
  460. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)))
  461. err = mmc_execute_tuning(card);
  462. out:
  463. return err;
  464. }
  465. /*
  466. * Handle the detection and initialisation of a card.
  467. *
  468. * In the case of a resume, "oldcard" will contain the card
  469. * we're trying to reinitialise.
  470. */
  471. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  472. struct mmc_card *oldcard, int powered_resume)
  473. {
  474. struct mmc_card *card;
  475. int err;
  476. int retries = 10;
  477. u32 rocr = 0;
  478. u32 ocr_card = ocr;
  479. BUG_ON(!host);
  480. WARN_ON(!host->claimed);
  481. /* to query card if 1.8V signalling is supported */
  482. if (mmc_host_uhs(host))
  483. ocr |= R4_18V_PRESENT;
  484. try_again:
  485. if (!retries) {
  486. pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
  487. ocr &= ~R4_18V_PRESENT;
  488. }
  489. /*
  490. * Inform the card of the voltage
  491. */
  492. if (!powered_resume) {
  493. err = mmc_send_io_op_cond(host, ocr, &rocr);
  494. if (err)
  495. goto err;
  496. }
  497. /*
  498. * For SPI, enable CRC as appropriate.
  499. */
  500. if (mmc_host_is_spi(host)) {
  501. err = mmc_spi_set_crc(host, use_spi_crc);
  502. if (err)
  503. goto err;
  504. }
  505. /*
  506. * Allocate card structure.
  507. */
  508. card = mmc_alloc_card(host, NULL);
  509. if (IS_ERR(card)) {
  510. err = PTR_ERR(card);
  511. goto err;
  512. }
  513. if ((rocr & R4_MEMORY_PRESENT) &&
  514. mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
  515. card->type = MMC_TYPE_SD_COMBO;
  516. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  517. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  518. mmc_remove_card(card);
  519. return -ENOENT;
  520. }
  521. } else {
  522. card->type = MMC_TYPE_SDIO;
  523. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  524. mmc_remove_card(card);
  525. return -ENOENT;
  526. }
  527. }
  528. /*
  529. * Call the optional HC's init_card function to handle quirks.
  530. */
  531. if (host->ops->init_card)
  532. host->ops->init_card(host, card);
  533. /*
  534. * If the host and card support UHS-I mode request the card
  535. * to switch to 1.8V signaling level. No 1.8v signalling if
  536. * UHS mode is not enabled to maintain compatibility and some
  537. * systems that claim 1.8v signalling in fact do not support
  538. * it.
  539. */
  540. if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
  541. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
  542. ocr);
  543. if (err == -EAGAIN) {
  544. sdio_reset(host);
  545. mmc_go_idle(host);
  546. mmc_send_if_cond(host, host->ocr_avail);
  547. mmc_remove_card(card);
  548. retries--;
  549. goto try_again;
  550. } else if (err) {
  551. ocr &= ~R4_18V_PRESENT;
  552. }
  553. err = 0;
  554. } else {
  555. ocr &= ~R4_18V_PRESENT;
  556. }
  557. /*
  558. * For native busses: set card RCA and quit open drain mode.
  559. */
  560. if (!powered_resume && !mmc_host_is_spi(host)) {
  561. err = mmc_send_relative_addr(host, &card->rca);
  562. if (err)
  563. goto remove;
  564. /*
  565. * Update oldcard with the new RCA received from the SDIO
  566. * device -- we're doing this so that it's updated in the
  567. * "card" struct when oldcard overwrites that later.
  568. */
  569. if (oldcard)
  570. oldcard->rca = card->rca;
  571. }
  572. /*
  573. * Read CSD, before selecting the card
  574. */
  575. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  576. err = mmc_sd_get_csd(host, card);
  577. if (err)
  578. return err;
  579. mmc_decode_cid(card);
  580. }
  581. /*
  582. * Select card, as all following commands rely on that.
  583. */
  584. if (!powered_resume && !mmc_host_is_spi(host)) {
  585. err = mmc_select_card(card);
  586. if (err)
  587. goto remove;
  588. }
  589. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  590. /*
  591. * This is non-standard SDIO device, meaning it doesn't
  592. * have any CIA (Common I/O area) registers present.
  593. * It's host's responsibility to fill cccr and cis
  594. * structures in init_card().
  595. */
  596. mmc_set_clock(host, card->cis.max_dtr);
  597. if (card->cccr.high_speed) {
  598. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  599. }
  600. goto finish;
  601. }
  602. /*
  603. * Read the common registers.
  604. */
  605. err = sdio_read_cccr(card, ocr);
  606. if (err)
  607. goto remove;
  608. /*
  609. * Read the common CIS tuples.
  610. */
  611. err = sdio_read_common_cis(card);
  612. if (err)
  613. goto remove;
  614. if (oldcard) {
  615. int same = (card->cis.vendor == oldcard->cis.vendor &&
  616. card->cis.device == oldcard->cis.device);
  617. mmc_remove_card(card);
  618. if (!same)
  619. return -ENOENT;
  620. card = oldcard;
  621. }
  622. card->ocr = ocr_card;
  623. mmc_fixup_device(card, NULL);
  624. if (card->type == MMC_TYPE_SD_COMBO) {
  625. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  626. /* handle as SDIO-only card if memory init failed */
  627. if (err) {
  628. mmc_go_idle(host);
  629. if (mmc_host_is_spi(host))
  630. /* should not fail, as it worked previously */
  631. mmc_spi_set_crc(host, use_spi_crc);
  632. card->type = MMC_TYPE_SDIO;
  633. } else
  634. card->dev.type = &sd_type;
  635. }
  636. /*
  637. * If needed, disconnect card detection pull-up resistor.
  638. */
  639. err = sdio_disable_cd(card);
  640. if (err)
  641. goto remove;
  642. /* Initialization sequence for UHS-I cards */
  643. /* Only if card supports 1.8v and UHS signaling */
  644. if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  645. err = mmc_sdio_init_uhs_card(card);
  646. if (err)
  647. goto remove;
  648. } else {
  649. /*
  650. * Switch to high-speed (if supported).
  651. */
  652. err = sdio_enable_hs(card);
  653. if (err > 0)
  654. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  655. else if (err)
  656. goto remove;
  657. /*
  658. * Change to the card's maximum speed.
  659. */
  660. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  661. /*
  662. * Switch to wider bus (if supported).
  663. */
  664. err = sdio_enable_4bit_bus(card);
  665. if (err)
  666. goto remove;
  667. }
  668. finish:
  669. if (!oldcard)
  670. host->card = card;
  671. return 0;
  672. remove:
  673. if (!oldcard)
  674. mmc_remove_card(card);
  675. err:
  676. return err;
  677. }
  678. /*
  679. * Host is being removed. Free up the current card.
  680. */
  681. static void mmc_sdio_remove(struct mmc_host *host)
  682. {
  683. int i;
  684. BUG_ON(!host);
  685. BUG_ON(!host->card);
  686. for (i = 0;i < host->card->sdio_funcs;i++) {
  687. if (host->card->sdio_func[i]) {
  688. sdio_remove_func(host->card->sdio_func[i]);
  689. host->card->sdio_func[i] = NULL;
  690. }
  691. }
  692. mmc_remove_card(host->card);
  693. host->card = NULL;
  694. }
  695. /*
  696. * Card detection - card is alive.
  697. */
  698. static int mmc_sdio_alive(struct mmc_host *host)
  699. {
  700. return mmc_select_card(host->card);
  701. }
  702. /*
  703. * Card detection callback from host.
  704. */
  705. static void mmc_sdio_detect(struct mmc_host *host)
  706. {
  707. int err;
  708. BUG_ON(!host);
  709. BUG_ON(!host->card);
  710. /* Make sure card is powered before detecting it */
  711. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  712. err = pm_runtime_get_sync(&host->card->dev);
  713. if (err < 0) {
  714. pm_runtime_put_noidle(&host->card->dev);
  715. goto out;
  716. }
  717. }
  718. mmc_claim_host(host);
  719. /*
  720. * Just check if our card has been removed.
  721. */
  722. err = _mmc_detect_card_removed(host);
  723. mmc_release_host(host);
  724. /*
  725. * Tell PM core it's OK to power off the card now.
  726. *
  727. * The _sync variant is used in order to ensure that the card
  728. * is left powered off in case an error occurred, and the card
  729. * is going to be removed.
  730. *
  731. * Since there is no specific reason to believe a new user
  732. * is about to show up at this point, the _sync variant is
  733. * desirable anyway.
  734. */
  735. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  736. pm_runtime_put_sync(&host->card->dev);
  737. out:
  738. if (err) {
  739. mmc_sdio_remove(host);
  740. mmc_claim_host(host);
  741. mmc_detach_bus(host);
  742. mmc_power_off(host);
  743. mmc_release_host(host);
  744. }
  745. }
  746. /*
  747. * SDIO pre_suspend. We need to suspend all functions separately.
  748. * Therefore all registered functions must have drivers with suspend
  749. * and resume methods. Failing that we simply remove the whole card.
  750. */
  751. static int mmc_sdio_pre_suspend(struct mmc_host *host)
  752. {
  753. int i, err = 0;
  754. for (i = 0; i < host->card->sdio_funcs; i++) {
  755. struct sdio_func *func = host->card->sdio_func[i];
  756. if (func && sdio_func_present(func) && func->dev.driver) {
  757. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  758. if (!pmops || !pmops->suspend || !pmops->resume) {
  759. /* force removal of entire card in that case */
  760. err = -ENOSYS;
  761. break;
  762. }
  763. }
  764. }
  765. return err;
  766. }
  767. /*
  768. * SDIO suspend. Suspend all functions separately.
  769. */
  770. static int mmc_sdio_suspend(struct mmc_host *host)
  771. {
  772. if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  773. mmc_claim_host(host);
  774. sdio_disable_wide(host->card);
  775. mmc_release_host(host);
  776. }
  777. if (!mmc_card_keep_power(host))
  778. mmc_power_off(host);
  779. return 0;
  780. }
  781. static int mmc_sdio_resume(struct mmc_host *host)
  782. {
  783. int err = 0;
  784. BUG_ON(!host);
  785. BUG_ON(!host->card);
  786. /* Basic card reinitialization. */
  787. mmc_claim_host(host);
  788. /* Restore power if needed */
  789. if (!mmc_card_keep_power(host)) {
  790. mmc_power_up(host, host->card->ocr);
  791. /*
  792. * Tell runtime PM core we just powered up the card,
  793. * since it still believes the card is powered off.
  794. * Note that currently runtime PM is only enabled
  795. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  796. */
  797. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  798. pm_runtime_disable(&host->card->dev);
  799. pm_runtime_set_active(&host->card->dev);
  800. pm_runtime_enable(&host->card->dev);
  801. }
  802. }
  803. /* No need to reinitialize powered-resumed nonremovable cards */
  804. if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
  805. sdio_reset(host);
  806. mmc_go_idle(host);
  807. mmc_send_if_cond(host, host->card->ocr);
  808. err = mmc_send_io_op_cond(host, 0, NULL);
  809. if (!err)
  810. err = mmc_sdio_init_card(host, host->card->ocr,
  811. host->card,
  812. mmc_card_keep_power(host));
  813. } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  814. /* We may have switched to 1-bit mode during suspend */
  815. err = sdio_enable_4bit_bus(host->card);
  816. }
  817. if (!err && host->sdio_irqs) {
  818. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  819. wake_up_process(host->sdio_irq_thread);
  820. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  821. mmc_host_clk_hold(host);
  822. host->ops->enable_sdio_irq(host, 1);
  823. mmc_host_clk_release(host);
  824. }
  825. }
  826. mmc_release_host(host);
  827. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  828. return err;
  829. }
  830. static int mmc_sdio_power_restore(struct mmc_host *host)
  831. {
  832. int ret;
  833. BUG_ON(!host);
  834. BUG_ON(!host->card);
  835. mmc_claim_host(host);
  836. /*
  837. * Reset the card by performing the same steps that are taken by
  838. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  839. *
  840. * sdio_reset() is technically not needed. Having just powered up the
  841. * hardware, it should already be in reset state. However, some
  842. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  843. * meaning that a reset is required when restoring power soon after
  844. * powering off. It is harmless in other cases.
  845. *
  846. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  847. * is not necessary for non-removable cards. However, it is required
  848. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  849. * harmless in other situations.
  850. *
  851. */
  852. sdio_reset(host);
  853. mmc_go_idle(host);
  854. mmc_send_if_cond(host, host->card->ocr);
  855. ret = mmc_send_io_op_cond(host, 0, NULL);
  856. if (ret)
  857. goto out;
  858. ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
  859. mmc_card_keep_power(host));
  860. if (!ret && host->sdio_irqs)
  861. mmc_signal_sdio_irq(host);
  862. out:
  863. mmc_release_host(host);
  864. return ret;
  865. }
  866. static int mmc_sdio_runtime_suspend(struct mmc_host *host)
  867. {
  868. /* No references to the card, cut the power to it. */
  869. mmc_power_off(host);
  870. return 0;
  871. }
  872. static int mmc_sdio_runtime_resume(struct mmc_host *host)
  873. {
  874. /* Restore power and re-initialize. */
  875. mmc_power_up(host, host->card->ocr);
  876. return mmc_sdio_power_restore(host);
  877. }
  878. static const struct mmc_bus_ops mmc_sdio_ops = {
  879. .remove = mmc_sdio_remove,
  880. .detect = mmc_sdio_detect,
  881. .pre_suspend = mmc_sdio_pre_suspend,
  882. .suspend = mmc_sdio_suspend,
  883. .resume = mmc_sdio_resume,
  884. .runtime_suspend = mmc_sdio_runtime_suspend,
  885. .runtime_resume = mmc_sdio_runtime_resume,
  886. .power_restore = mmc_sdio_power_restore,
  887. .alive = mmc_sdio_alive,
  888. };
  889. /*
  890. * Starting point for SDIO card init.
  891. */
  892. int mmc_attach_sdio(struct mmc_host *host)
  893. {
  894. int err, i, funcs;
  895. u32 ocr, rocr;
  896. struct mmc_card *card;
  897. BUG_ON(!host);
  898. WARN_ON(!host->claimed);
  899. err = mmc_send_io_op_cond(host, 0, &ocr);
  900. if (err)
  901. return err;
  902. mmc_attach_bus(host, &mmc_sdio_ops);
  903. if (host->ocr_avail_sdio)
  904. host->ocr_avail = host->ocr_avail_sdio;
  905. rocr = mmc_select_voltage(host, ocr);
  906. /*
  907. * Can we support the voltage(s) of the card(s)?
  908. */
  909. if (!rocr) {
  910. err = -EINVAL;
  911. goto err;
  912. }
  913. /*
  914. * Detect and init the card.
  915. */
  916. err = mmc_sdio_init_card(host, rocr, NULL, 0);
  917. if (err)
  918. goto err;
  919. card = host->card;
  920. /*
  921. * Enable runtime PM only if supported by host+card+board
  922. */
  923. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  924. /*
  925. * Let runtime PM core know our card is active
  926. */
  927. err = pm_runtime_set_active(&card->dev);
  928. if (err)
  929. goto remove;
  930. /*
  931. * Enable runtime PM for this card
  932. */
  933. pm_runtime_enable(&card->dev);
  934. }
  935. /*
  936. * The number of functions on the card is encoded inside
  937. * the ocr.
  938. */
  939. funcs = (ocr & 0x70000000) >> 28;
  940. card->sdio_funcs = 0;
  941. /*
  942. * Initialize (but don't add) all present functions.
  943. */
  944. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  945. err = sdio_init_func(host->card, i + 1);
  946. if (err)
  947. goto remove;
  948. /*
  949. * Enable Runtime PM for this func (if supported)
  950. */
  951. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  952. pm_runtime_enable(&card->sdio_func[i]->dev);
  953. }
  954. /*
  955. * First add the card to the driver model...
  956. */
  957. mmc_release_host(host);
  958. err = mmc_add_card(host->card);
  959. if (err)
  960. goto remove_added;
  961. /*
  962. * ...then the SDIO functions.
  963. */
  964. for (i = 0;i < funcs;i++) {
  965. err = sdio_add_func(host->card->sdio_func[i]);
  966. if (err)
  967. goto remove_added;
  968. }
  969. mmc_claim_host(host);
  970. return 0;
  971. remove_added:
  972. /* Remove without lock if the device has been added. */
  973. mmc_sdio_remove(host);
  974. mmc_claim_host(host);
  975. remove:
  976. /* And with lock if it hasn't been added. */
  977. mmc_release_host(host);
  978. if (host->card)
  979. mmc_sdio_remove(host);
  980. mmc_claim_host(host);
  981. err:
  982. mmc_detach_bus(host);
  983. pr_err("%s: error %d whilst initialising SDIO card\n",
  984. mmc_hostname(host), err);
  985. return err;
  986. }