sdio.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/mmc/sdio.h>
  18. #include <linux/mmc/sdio_func.h>
  19. #include <linux/mmc/sdio_ids.h>
  20. #include "core.h"
  21. #include "bus.h"
  22. #include "sd.h"
  23. #include "sdio_bus.h"
  24. #include "mmc_ops.h"
  25. #include "sd_ops.h"
  26. #include "sdio_ops.h"
  27. #include "sdio_cis.h"
  28. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  29. #include <linux/mmc/sdio_ids.h>
  30. #endif
  31. static int sdio_read_fbr(struct sdio_func *func)
  32. {
  33. int ret;
  34. unsigned char data;
  35. if (mmc_card_nonstd_func_interface(func->card)) {
  36. func->class = SDIO_CLASS_NONE;
  37. return 0;
  38. }
  39. ret = mmc_io_rw_direct(func->card, 0, 0,
  40. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  41. if (ret)
  42. goto out;
  43. data &= 0x0f;
  44. if (data == 0x0f) {
  45. ret = mmc_io_rw_direct(func->card, 0, 0,
  46. SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  47. if (ret)
  48. goto out;
  49. }
  50. func->class = data;
  51. out:
  52. return ret;
  53. }
  54. static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  55. {
  56. int ret;
  57. struct sdio_func *func;
  58. BUG_ON(fn > SDIO_MAX_FUNCS);
  59. func = sdio_alloc_func(card);
  60. if (IS_ERR(func))
  61. return PTR_ERR(func);
  62. func->num = fn;
  63. if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  64. ret = sdio_read_fbr(func);
  65. if (ret)
  66. goto fail;
  67. ret = sdio_read_func_cis(func);
  68. if (ret)
  69. goto fail;
  70. } else {
  71. func->vendor = func->card->cis.vendor;
  72. func->device = func->card->cis.device;
  73. func->max_blksize = func->card->cis.blksize;
  74. }
  75. card->sdio_func[fn - 1] = func;
  76. return 0;
  77. fail:
  78. /*
  79. * It is okay to remove the function here even though we hold
  80. * the host lock as we haven't registered the device yet.
  81. */
  82. sdio_remove_func(func);
  83. return ret;
  84. }
  85. static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
  86. {
  87. int ret;
  88. int cccr_vsn;
  89. int uhs = ocr & R4_18V_PRESENT;
  90. unsigned char data;
  91. unsigned char speed;
  92. memset(&card->cccr, 0, sizeof(struct sdio_cccr));
  93. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
  94. if (ret)
  95. goto out;
  96. cccr_vsn = data & 0x0f;
  97. if (cccr_vsn > SDIO_CCCR_REV_3_00) {
  98. pr_err("%s: unrecognised CCCR structure version %d\n",
  99. mmc_hostname(card->host), cccr_vsn);
  100. return -EINVAL;
  101. }
  102. card->cccr.sdio_vsn = (data & 0xf0) >> 4;
  103. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
  104. if (ret)
  105. goto out;
  106. if (data & SDIO_CCCR_CAP_SMB)
  107. card->cccr.multi_block = 1;
  108. if (data & SDIO_CCCR_CAP_LSC)
  109. card->cccr.low_speed = 1;
  110. if (data & SDIO_CCCR_CAP_4BLS)
  111. card->cccr.wide_bus = 1;
  112. if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
  113. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
  114. if (ret)
  115. goto out;
  116. if (data & SDIO_POWER_SMPC)
  117. card->cccr.high_power = 1;
  118. }
  119. if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
  120. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
  121. if (ret)
  122. goto out;
  123. card->scr.sda_spec3 = 0;
  124. card->sw_caps.sd3_bus_mode = 0;
  125. card->sw_caps.sd3_drv_type = 0;
  126. if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
  127. card->scr.sda_spec3 = 1;
  128. ret = mmc_io_rw_direct(card, 0, 0,
  129. SDIO_CCCR_UHS, 0, &data);
  130. if (ret)
  131. goto out;
  132. if (mmc_host_uhs(card->host)) {
  133. if (data & SDIO_UHS_DDR50)
  134. card->sw_caps.sd3_bus_mode
  135. |= SD_MODE_UHS_DDR50;
  136. if (data & SDIO_UHS_SDR50)
  137. card->sw_caps.sd3_bus_mode
  138. |= SD_MODE_UHS_SDR50;
  139. if (data & SDIO_UHS_SDR104)
  140. card->sw_caps.sd3_bus_mode
  141. |= SD_MODE_UHS_SDR104;
  142. }
  143. ret = mmc_io_rw_direct(card, 0, 0,
  144. SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
  145. if (ret)
  146. goto out;
  147. if (data & SDIO_DRIVE_SDTA)
  148. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
  149. if (data & SDIO_DRIVE_SDTC)
  150. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
  151. if (data & SDIO_DRIVE_SDTD)
  152. card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
  153. }
  154. /* if no uhs mode ensure we check for high speed */
  155. if (!card->sw_caps.sd3_bus_mode) {
  156. if (speed & SDIO_SPEED_SHS) {
  157. card->cccr.high_speed = 1;
  158. card->sw_caps.hs_max_dtr = 50000000;
  159. } else {
  160. card->cccr.high_speed = 0;
  161. card->sw_caps.hs_max_dtr = 25000000;
  162. }
  163. }
  164. }
  165. out:
  166. return ret;
  167. }
  168. static int sdio_enable_wide(struct mmc_card *card)
  169. {
  170. int ret;
  171. u8 ctrl;
  172. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  173. return 0;
  174. if (card->cccr.low_speed && !card->cccr.wide_bus)
  175. return 0;
  176. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  177. if (ret)
  178. return ret;
  179. if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
  180. pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
  181. mmc_hostname(card->host), ctrl);
  182. /* set as 4-bit bus width */
  183. ctrl &= ~SDIO_BUS_WIDTH_MASK;
  184. ctrl |= SDIO_BUS_WIDTH_4BIT;
  185. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  186. if (ret)
  187. return ret;
  188. return 1;
  189. }
  190. /*
  191. * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
  192. * of the card. This may be required on certain setups of boards,
  193. * controllers and embedded sdio device which do not need the card's
  194. * pull-up. As a result, card detection is disabled and power is saved.
  195. */
  196. static int sdio_disable_cd(struct mmc_card *card)
  197. {
  198. int ret;
  199. u8 ctrl;
  200. if (!mmc_card_disable_cd(card))
  201. return 0;
  202. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  203. if (ret)
  204. return ret;
  205. ctrl |= SDIO_BUS_CD_DISABLE;
  206. return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  207. }
  208. /*
  209. * Devices that remain active during a system suspend are
  210. * put back into 1-bit mode.
  211. */
  212. static int sdio_disable_wide(struct mmc_card *card)
  213. {
  214. int ret;
  215. u8 ctrl;
  216. if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
  217. return 0;
  218. if (card->cccr.low_speed && !card->cccr.wide_bus)
  219. return 0;
  220. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
  221. if (ret)
  222. return ret;
  223. if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
  224. return 0;
  225. ctrl &= ~SDIO_BUS_WIDTH_4BIT;
  226. ctrl |= SDIO_BUS_ASYNC_INT;
  227. ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
  228. if (ret)
  229. return ret;
  230. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
  231. return 0;
  232. }
  233. static int sdio_enable_4bit_bus(struct mmc_card *card)
  234. {
  235. int err;
  236. if (card->type == MMC_TYPE_SDIO)
  237. return sdio_enable_wide(card);
  238. if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
  239. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  240. err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
  241. if (err)
  242. return err;
  243. } else
  244. return 0;
  245. err = sdio_enable_wide(card);
  246. if (err <= 0)
  247. mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
  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. if (err > 0) {
  449. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  450. err = 0;
  451. }
  452. }
  453. /* Set the driver strength for the card */
  454. sdio_select_driver_type(card);
  455. /* Set bus speed mode of the card */
  456. err = sdio_set_bus_speed_mode(card);
  457. if (err)
  458. goto out;
  459. /*
  460. * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
  461. * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
  462. */
  463. if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
  464. ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
  465. (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104))) {
  466. mmc_host_clk_hold(card->host);
  467. err = card->host->ops->execute_tuning(card->host,
  468. MMC_SEND_TUNING_BLOCK);
  469. mmc_host_clk_release(card->host);
  470. }
  471. out:
  472. return err;
  473. }
  474. /*
  475. * Handle the detection and initialisation of a card.
  476. *
  477. * In the case of a resume, "oldcard" will contain the card
  478. * we're trying to reinitialise.
  479. */
  480. static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
  481. struct mmc_card *oldcard, int powered_resume)
  482. {
  483. struct mmc_card *card;
  484. int err;
  485. int retries = 10;
  486. u32 rocr = 0;
  487. u32 ocr_card = ocr;
  488. BUG_ON(!host);
  489. WARN_ON(!host->claimed);
  490. /* to query card if 1.8V signalling is supported */
  491. if (mmc_host_uhs(host))
  492. ocr |= R4_18V_PRESENT;
  493. try_again:
  494. if (!retries) {
  495. pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
  496. ocr &= ~R4_18V_PRESENT;
  497. }
  498. /*
  499. * Inform the card of the voltage
  500. */
  501. if (!powered_resume) {
  502. err = mmc_send_io_op_cond(host, ocr, &rocr);
  503. if (err)
  504. goto err;
  505. }
  506. /*
  507. * For SPI, enable CRC as appropriate.
  508. */
  509. if (mmc_host_is_spi(host)) {
  510. err = mmc_spi_set_crc(host, use_spi_crc);
  511. if (err)
  512. goto err;
  513. }
  514. /*
  515. * Allocate card structure.
  516. */
  517. card = mmc_alloc_card(host, NULL);
  518. if (IS_ERR(card)) {
  519. err = PTR_ERR(card);
  520. goto err;
  521. }
  522. if ((rocr & R4_MEMORY_PRESENT) &&
  523. mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
  524. card->type = MMC_TYPE_SD_COMBO;
  525. if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
  526. memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
  527. mmc_remove_card(card);
  528. return -ENOENT;
  529. }
  530. } else {
  531. card->type = MMC_TYPE_SDIO;
  532. if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
  533. mmc_remove_card(card);
  534. return -ENOENT;
  535. }
  536. }
  537. /*
  538. * Call the optional HC's init_card function to handle quirks.
  539. */
  540. if (host->ops->init_card)
  541. host->ops->init_card(host, card);
  542. /*
  543. * If the host and card support UHS-I mode request the card
  544. * to switch to 1.8V signaling level. No 1.8v signalling if
  545. * UHS mode is not enabled to maintain compatibility and some
  546. * systems that claim 1.8v signalling in fact do not support
  547. * it.
  548. */
  549. if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
  550. err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
  551. ocr);
  552. if (err == -EAGAIN) {
  553. sdio_reset(host);
  554. mmc_go_idle(host);
  555. mmc_send_if_cond(host, host->ocr_avail);
  556. mmc_remove_card(card);
  557. retries--;
  558. goto try_again;
  559. } else if (err) {
  560. ocr &= ~R4_18V_PRESENT;
  561. }
  562. err = 0;
  563. } else {
  564. ocr &= ~R4_18V_PRESENT;
  565. }
  566. /*
  567. * For native busses: set card RCA and quit open drain mode.
  568. */
  569. if (!powered_resume && !mmc_host_is_spi(host)) {
  570. err = mmc_send_relative_addr(host, &card->rca);
  571. if (err)
  572. goto remove;
  573. /*
  574. * Update oldcard with the new RCA received from the SDIO
  575. * device -- we're doing this so that it's updated in the
  576. * "card" struct when oldcard overwrites that later.
  577. */
  578. if (oldcard)
  579. oldcard->rca = card->rca;
  580. }
  581. /*
  582. * Read CSD, before selecting the card
  583. */
  584. if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
  585. err = mmc_sd_get_csd(host, card);
  586. if (err)
  587. return err;
  588. mmc_decode_cid(card);
  589. }
  590. /*
  591. * Select card, as all following commands rely on that.
  592. */
  593. if (!powered_resume && !mmc_host_is_spi(host)) {
  594. err = mmc_select_card(card);
  595. if (err)
  596. goto remove;
  597. }
  598. if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
  599. /*
  600. * This is non-standard SDIO device, meaning it doesn't
  601. * have any CIA (Common I/O area) registers present.
  602. * It's host's responsibility to fill cccr and cis
  603. * structures in init_card().
  604. */
  605. mmc_set_clock(host, card->cis.max_dtr);
  606. if (card->cccr.high_speed) {
  607. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  608. }
  609. goto finish;
  610. }
  611. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  612. if (host->embedded_sdio_data.cccr)
  613. memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
  614. else {
  615. #endif
  616. /*
  617. * Read the common registers.
  618. */
  619. err = sdio_read_cccr(card, ocr);
  620. if (err)
  621. goto remove;
  622. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  623. }
  624. #endif
  625. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  626. if (host->embedded_sdio_data.cis)
  627. memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
  628. else {
  629. #endif
  630. /*
  631. * Read the common CIS tuples.
  632. */
  633. err = sdio_read_common_cis(card);
  634. if (err)
  635. goto remove;
  636. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  637. }
  638. #endif
  639. if (oldcard) {
  640. int same = (card->cis.vendor == oldcard->cis.vendor &&
  641. card->cis.device == oldcard->cis.device);
  642. mmc_remove_card(card);
  643. if (!same)
  644. return -ENOENT;
  645. card = oldcard;
  646. }
  647. card->ocr = ocr_card;
  648. mmc_fixup_device(card, NULL);
  649. if (card->type == MMC_TYPE_SD_COMBO) {
  650. err = mmc_sd_setup_card(host, card, oldcard != NULL);
  651. /* handle as SDIO-only card if memory init failed */
  652. if (err) {
  653. mmc_go_idle(host);
  654. if (mmc_host_is_spi(host))
  655. /* should not fail, as it worked previously */
  656. mmc_spi_set_crc(host, use_spi_crc);
  657. card->type = MMC_TYPE_SDIO;
  658. } else
  659. card->dev.type = &sd_type;
  660. }
  661. /*
  662. * If needed, disconnect card detection pull-up resistor.
  663. */
  664. err = sdio_disable_cd(card);
  665. if (err)
  666. goto remove;
  667. /* Initialization sequence for UHS-I cards */
  668. /* Only if card supports 1.8v and UHS signaling */
  669. if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
  670. err = mmc_sdio_init_uhs_card(card);
  671. if (err)
  672. goto remove;
  673. } else {
  674. /*
  675. * Switch to high-speed (if supported).
  676. */
  677. err = sdio_enable_hs(card);
  678. if (err > 0)
  679. mmc_set_timing(card->host, MMC_TIMING_SD_HS);
  680. else if (err)
  681. goto remove;
  682. /*
  683. * Change to the card's maximum speed.
  684. */
  685. mmc_set_clock(host, mmc_sdio_get_max_clock(card));
  686. /*
  687. * Switch to wider bus (if supported).
  688. */
  689. err = sdio_enable_4bit_bus(card);
  690. if (err > 0)
  691. mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
  692. else if (err)
  693. goto remove;
  694. }
  695. finish:
  696. if (!oldcard)
  697. host->card = card;
  698. return 0;
  699. remove:
  700. if (!oldcard)
  701. mmc_remove_card(card);
  702. err:
  703. return err;
  704. }
  705. /*
  706. * Host is being removed. Free up the current card.
  707. */
  708. static void mmc_sdio_remove(struct mmc_host *host)
  709. {
  710. int i;
  711. BUG_ON(!host);
  712. BUG_ON(!host->card);
  713. for (i = 0;i < host->card->sdio_funcs;i++) {
  714. if (host->card->sdio_func[i]) {
  715. sdio_remove_func(host->card->sdio_func[i]);
  716. host->card->sdio_func[i] = NULL;
  717. }
  718. }
  719. mmc_remove_card(host->card);
  720. host->card = NULL;
  721. }
  722. /*
  723. * Card detection - card is alive.
  724. */
  725. static int mmc_sdio_alive(struct mmc_host *host)
  726. {
  727. return mmc_select_card(host->card);
  728. }
  729. /*
  730. * Card detection callback from host.
  731. */
  732. static void mmc_sdio_detect(struct mmc_host *host)
  733. {
  734. int err;
  735. BUG_ON(!host);
  736. BUG_ON(!host->card);
  737. /* Make sure card is powered before detecting it */
  738. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  739. err = pm_runtime_get_sync(&host->card->dev);
  740. if (err < 0) {
  741. pm_runtime_put_noidle(&host->card->dev);
  742. goto out;
  743. }
  744. }
  745. mmc_claim_host(host);
  746. /*
  747. * Just check if our card has been removed.
  748. */
  749. err = _mmc_detect_card_removed(host);
  750. mmc_release_host(host);
  751. /*
  752. * Tell PM core it's OK to power off the card now.
  753. *
  754. * The _sync variant is used in order to ensure that the card
  755. * is left powered off in case an error occurred, and the card
  756. * is going to be removed.
  757. *
  758. * Since there is no specific reason to believe a new user
  759. * is about to show up at this point, the _sync variant is
  760. * desirable anyway.
  761. */
  762. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  763. pm_runtime_put_sync(&host->card->dev);
  764. out:
  765. if (err) {
  766. mmc_sdio_remove(host);
  767. mmc_claim_host(host);
  768. mmc_detach_bus(host);
  769. mmc_power_off(host);
  770. mmc_release_host(host);
  771. }
  772. }
  773. /*
  774. * SDIO pre_suspend. We need to suspend all functions separately.
  775. * Therefore all registered functions must have drivers with suspend
  776. * and resume methods. Failing that we simply remove the whole card.
  777. */
  778. static int mmc_sdio_pre_suspend(struct mmc_host *host)
  779. {
  780. int i, err = 0;
  781. for (i = 0; i < host->card->sdio_funcs; i++) {
  782. struct sdio_func *func = host->card->sdio_func[i];
  783. if (func && sdio_func_present(func) && func->dev.driver) {
  784. const struct dev_pm_ops *pmops = func->dev.driver->pm;
  785. if (!pmops || !pmops->suspend || !pmops->resume) {
  786. /* force removal of entire card in that case */
  787. err = -ENOSYS;
  788. break;
  789. }
  790. }
  791. }
  792. return err;
  793. }
  794. /*
  795. * SDIO suspend. Suspend all functions separately.
  796. */
  797. static int mmc_sdio_suspend(struct mmc_host *host)
  798. {
  799. if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  800. mmc_claim_host(host);
  801. sdio_disable_wide(host->card);
  802. mmc_release_host(host);
  803. }
  804. if (!mmc_card_keep_power(host))
  805. mmc_power_off(host);
  806. return 0;
  807. }
  808. static int mmc_sdio_resume(struct mmc_host *host)
  809. {
  810. int err = 0;
  811. BUG_ON(!host);
  812. BUG_ON(!host->card);
  813. /* Basic card reinitialization. */
  814. mmc_claim_host(host);
  815. /* Restore power if needed */
  816. if (!mmc_card_keep_power(host)) {
  817. mmc_power_up(host, host->card->ocr);
  818. /*
  819. * Tell runtime PM core we just powered up the card,
  820. * since it still believes the card is powered off.
  821. * Note that currently runtime PM is only enabled
  822. * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
  823. */
  824. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  825. pm_runtime_disable(&host->card->dev);
  826. pm_runtime_set_active(&host->card->dev);
  827. pm_runtime_enable(&host->card->dev);
  828. }
  829. }
  830. /* No need to reinitialize powered-resumed nonremovable cards */
  831. if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
  832. sdio_reset(host);
  833. mmc_go_idle(host);
  834. err = mmc_sdio_init_card(host, host->card->ocr, host->card,
  835. mmc_card_keep_power(host));
  836. } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
  837. /* We may have switched to 1-bit mode during suspend */
  838. err = sdio_enable_4bit_bus(host->card);
  839. if (err > 0) {
  840. mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
  841. err = 0;
  842. }
  843. }
  844. if (!err && host->sdio_irqs) {
  845. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  846. wake_up_process(host->sdio_irq_thread);
  847. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  848. mmc_host_clk_hold(host);
  849. host->ops->enable_sdio_irq(host, 1);
  850. mmc_host_clk_release(host);
  851. }
  852. }
  853. mmc_release_host(host);
  854. host->pm_flags &= ~MMC_PM_KEEP_POWER;
  855. return err;
  856. }
  857. static int mmc_sdio_power_restore(struct mmc_host *host)
  858. {
  859. int ret;
  860. BUG_ON(!host);
  861. BUG_ON(!host->card);
  862. mmc_claim_host(host);
  863. /*
  864. * Reset the card by performing the same steps that are taken by
  865. * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
  866. *
  867. * sdio_reset() is technically not needed. Having just powered up the
  868. * hardware, it should already be in reset state. However, some
  869. * platforms (such as SD8686 on OLPC) do not instantly cut power,
  870. * meaning that a reset is required when restoring power soon after
  871. * powering off. It is harmless in other cases.
  872. *
  873. * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
  874. * is not necessary for non-removable cards. However, it is required
  875. * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
  876. * harmless in other situations.
  877. *
  878. */
  879. sdio_reset(host);
  880. mmc_go_idle(host);
  881. mmc_send_if_cond(host, host->ocr_avail);
  882. ret = mmc_send_io_op_cond(host, 0, NULL);
  883. if (ret)
  884. goto out;
  885. ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
  886. mmc_card_keep_power(host));
  887. if (!ret && host->sdio_irqs)
  888. mmc_signal_sdio_irq(host);
  889. out:
  890. mmc_release_host(host);
  891. return ret;
  892. }
  893. static int mmc_sdio_runtime_suspend(struct mmc_host *host)
  894. {
  895. /* No references to the card, cut the power to it. */
  896. mmc_power_off(host);
  897. return 0;
  898. }
  899. static int mmc_sdio_runtime_resume(struct mmc_host *host)
  900. {
  901. /* Restore power and re-initialize. */
  902. mmc_power_up(host, host->card->ocr);
  903. return mmc_sdio_power_restore(host);
  904. }
  905. static const struct mmc_bus_ops mmc_sdio_ops = {
  906. .remove = mmc_sdio_remove,
  907. .detect = mmc_sdio_detect,
  908. .pre_suspend = mmc_sdio_pre_suspend,
  909. .suspend = mmc_sdio_suspend,
  910. .resume = mmc_sdio_resume,
  911. .runtime_suspend = mmc_sdio_runtime_suspend,
  912. .runtime_resume = mmc_sdio_runtime_resume,
  913. .power_restore = mmc_sdio_power_restore,
  914. .alive = mmc_sdio_alive,
  915. };
  916. /*
  917. * Starting point for SDIO card init.
  918. */
  919. int mmc_attach_sdio(struct mmc_host *host)
  920. {
  921. int err, i, funcs;
  922. u32 ocr, rocr;
  923. struct mmc_card *card;
  924. BUG_ON(!host);
  925. WARN_ON(!host->claimed);
  926. err = mmc_send_io_op_cond(host, 0, &ocr);
  927. if (err)
  928. return err;
  929. mmc_attach_bus(host, &mmc_sdio_ops);
  930. if (host->ocr_avail_sdio)
  931. host->ocr_avail = host->ocr_avail_sdio;
  932. rocr = mmc_select_voltage(host, ocr);
  933. /*
  934. * Can we support the voltage(s) of the card(s)?
  935. */
  936. if (!rocr) {
  937. err = -EINVAL;
  938. goto err;
  939. }
  940. /*
  941. * Detect and init the card.
  942. */
  943. err = mmc_sdio_init_card(host, rocr, NULL, 0);
  944. if (err)
  945. goto err;
  946. card = host->card;
  947. /*
  948. * Enable runtime PM only if supported by host+card+board
  949. */
  950. if (host->caps & MMC_CAP_POWER_OFF_CARD) {
  951. /*
  952. * Let runtime PM core know our card is active
  953. */
  954. err = pm_runtime_set_active(&card->dev);
  955. if (err)
  956. goto remove;
  957. /*
  958. * Enable runtime PM for this card
  959. */
  960. pm_runtime_enable(&card->dev);
  961. }
  962. /*
  963. * The number of functions on the card is encoded inside
  964. * the ocr.
  965. */
  966. funcs = (ocr & 0x70000000) >> 28;
  967. card->sdio_funcs = 0;
  968. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  969. if (host->embedded_sdio_data.funcs)
  970. card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
  971. #endif
  972. /*
  973. * Initialize (but don't add) all present functions.
  974. */
  975. for (i = 0; i < funcs; i++, card->sdio_funcs++) {
  976. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  977. if (host->embedded_sdio_data.funcs) {
  978. struct sdio_func *tmp;
  979. tmp = sdio_alloc_func(host->card);
  980. if (IS_ERR(tmp))
  981. goto remove;
  982. tmp->num = (i + 1);
  983. card->sdio_func[i] = tmp;
  984. tmp->class = host->embedded_sdio_data.funcs[i].f_class;
  985. tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
  986. tmp->vendor = card->cis.vendor;
  987. tmp->device = card->cis.device;
  988. } else {
  989. #endif
  990. err = sdio_init_func(host->card, i + 1);
  991. if (err)
  992. goto remove;
  993. #ifdef CONFIG_MMC_EMBEDDED_SDIO
  994. }
  995. #endif
  996. /*
  997. * Enable Runtime PM for this func (if supported)
  998. */
  999. if (host->caps & MMC_CAP_POWER_OFF_CARD)
  1000. pm_runtime_enable(&card->sdio_func[i]->dev);
  1001. }
  1002. /*
  1003. * First add the card to the driver model...
  1004. */
  1005. mmc_release_host(host);
  1006. err = mmc_add_card(host->card);
  1007. if (err)
  1008. goto remove_added;
  1009. /*
  1010. * ...then the SDIO functions.
  1011. */
  1012. for (i = 0;i < funcs;i++) {
  1013. err = sdio_add_func(host->card->sdio_func[i]);
  1014. if (err)
  1015. goto remove_added;
  1016. }
  1017. mmc_claim_host(host);
  1018. return 0;
  1019. remove_added:
  1020. /* Remove without lock if the device has been added. */
  1021. mmc_sdio_remove(host);
  1022. mmc_claim_host(host);
  1023. remove:
  1024. /* And with lock if it hasn't been added. */
  1025. mmc_release_host(host);
  1026. if (host->card)
  1027. mmc_sdio_remove(host);
  1028. mmc_claim_host(host);
  1029. err:
  1030. mmc_detach_bus(host);
  1031. pr_err("%s: error %d whilst initialising SDIO card\n",
  1032. mmc_hostname(host), err);
  1033. return err;
  1034. }
  1035. int sdio_reset_comm(struct mmc_card *card)
  1036. {
  1037. struct mmc_host *host = card->host;
  1038. u32 ocr;
  1039. u32 rocr;
  1040. int err;
  1041. printk("%s():\n", __func__);
  1042. mmc_claim_host(host);
  1043. mmc_go_idle(host);
  1044. mmc_set_clock(host, host->f_min);
  1045. err = mmc_send_io_op_cond(host, 0, &ocr);
  1046. if (err)
  1047. goto err;
  1048. rocr = mmc_select_voltage(host, ocr);
  1049. if (!rocr) {
  1050. err = -EINVAL;
  1051. goto err;
  1052. }
  1053. err = mmc_sdio_init_card(host, rocr, card, 0);
  1054. if (err)
  1055. goto err;
  1056. mmc_release_host(host);
  1057. return 0;
  1058. err:
  1059. printk("%s: Error resetting SDIO communications (%d)\n",
  1060. mmc_hostname(host), err);
  1061. mmc_release_host(host);
  1062. return err;
  1063. }
  1064. EXPORT_SYMBOL(sdio_reset_comm);