sdio.c 27 KB

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