sdio.c 29 KB

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