sdio.c 30 KB

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