sdio.c 26 KB

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