sdio.c 26 KB

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