sdio.c 28 KB

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