sdhci-acpi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Secure Digital Host Controller Interface ACPI driver.
  4. *
  5. * Copyright (c) 2012, Intel Corporation.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/init.h>
  9. #include <linux/export.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/pinctrl/pinconf-generic.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/ioport.h>
  15. #include <linux/io.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/compiler.h>
  18. #include <linux/stddef.h>
  19. #include <linux/bitops.h>
  20. #include <linux/types.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/acpi.h>
  24. #include <linux/pm.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/delay.h>
  27. #include <linux/dmi.h>
  28. #include <linux/mmc/host.h>
  29. #include <linux/mmc/pm.h>
  30. #include <linux/mmc/slot-gpio.h>
  31. #ifdef CONFIG_X86
  32. #include <linux/platform_data/x86/soc.h>
  33. #include <asm/iosf_mbi.h>
  34. #endif
  35. #include "sdhci.h"
  36. enum {
  37. SDHCI_ACPI_SD_CD = BIT(0),
  38. SDHCI_ACPI_RUNTIME_PM = BIT(1),
  39. SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
  40. };
  41. struct sdhci_acpi_chip {
  42. const struct sdhci_ops *ops;
  43. unsigned int quirks;
  44. unsigned int quirks2;
  45. unsigned long caps;
  46. unsigned int caps2;
  47. mmc_pm_flag_t pm_caps;
  48. };
  49. struct sdhci_acpi_slot {
  50. const struct sdhci_acpi_chip *chip;
  51. unsigned int quirks;
  52. unsigned int quirks2;
  53. unsigned long caps;
  54. unsigned int caps2;
  55. mmc_pm_flag_t pm_caps;
  56. unsigned int flags;
  57. size_t priv_size;
  58. int (*probe_slot)(struct platform_device *, struct acpi_device *);
  59. int (*remove_slot)(struct platform_device *);
  60. int (*free_slot)(struct platform_device *pdev);
  61. int (*setup_host)(struct platform_device *pdev);
  62. };
  63. struct sdhci_acpi_host {
  64. struct sdhci_host *host;
  65. const struct sdhci_acpi_slot *slot;
  66. struct platform_device *pdev;
  67. bool use_runtime_pm;
  68. bool is_intel;
  69. bool reset_signal_volt_on_suspend;
  70. unsigned long private[] ____cacheline_aligned;
  71. };
  72. enum {
  73. DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP = BIT(0),
  74. DMI_QUIRK_SD_NO_WRITE_PROTECT = BIT(1),
  75. DMI_QUIRK_SD_CD_ACTIVE_HIGH = BIT(2),
  76. DMI_QUIRK_SD_CD_ENABLE_PULL_UP = BIT(3),
  77. };
  78. static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
  79. {
  80. return (void *)c->private;
  81. }
  82. static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
  83. {
  84. return c->slot && (c->slot->flags & flag);
  85. }
  86. #define INTEL_DSM_HS_CAPS_SDR25 BIT(0)
  87. #define INTEL_DSM_HS_CAPS_DDR50 BIT(1)
  88. #define INTEL_DSM_HS_CAPS_SDR50 BIT(2)
  89. #define INTEL_DSM_HS_CAPS_SDR104 BIT(3)
  90. enum {
  91. INTEL_DSM_FNS = 0,
  92. INTEL_DSM_V18_SWITCH = 3,
  93. INTEL_DSM_V33_SWITCH = 4,
  94. INTEL_DSM_HS_CAPS = 8,
  95. };
  96. struct intel_host {
  97. u32 dsm_fns;
  98. u32 hs_caps;
  99. };
  100. static const guid_t intel_dsm_guid =
  101. GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
  102. 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
  103. static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
  104. unsigned int fn, u32 *result)
  105. {
  106. union acpi_object *obj;
  107. int err = 0;
  108. obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
  109. if (!obj)
  110. return -EOPNOTSUPP;
  111. if (obj->type == ACPI_TYPE_INTEGER) {
  112. *result = obj->integer.value;
  113. } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
  114. size_t len = min_t(size_t, obj->buffer.length, 4);
  115. *result = 0;
  116. memcpy(result, obj->buffer.pointer, len);
  117. } else {
  118. dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
  119. __func__, fn, obj->type, obj->buffer.length);
  120. err = -EINVAL;
  121. }
  122. ACPI_FREE(obj);
  123. return err;
  124. }
  125. static int intel_dsm(struct intel_host *intel_host, struct device *dev,
  126. unsigned int fn, u32 *result)
  127. {
  128. if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
  129. return -EOPNOTSUPP;
  130. return __intel_dsm(intel_host, dev, fn, result);
  131. }
  132. static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
  133. struct mmc_host *mmc)
  134. {
  135. int err;
  136. intel_host->hs_caps = ~0;
  137. err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
  138. if (err) {
  139. pr_debug("%s: DSM not supported, error %d\n",
  140. mmc_hostname(mmc), err);
  141. return;
  142. }
  143. pr_debug("%s: DSM function mask %#x\n",
  144. mmc_hostname(mmc), intel_host->dsm_fns);
  145. intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps);
  146. }
  147. static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
  148. struct mmc_ios *ios)
  149. {
  150. struct device *dev = mmc_dev(mmc);
  151. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  152. struct intel_host *intel_host = sdhci_acpi_priv(c);
  153. unsigned int fn;
  154. u32 result = 0;
  155. int err;
  156. err = sdhci_start_signal_voltage_switch(mmc, ios);
  157. if (err)
  158. return err;
  159. switch (ios->signal_voltage) {
  160. case MMC_SIGNAL_VOLTAGE_330:
  161. fn = INTEL_DSM_V33_SWITCH;
  162. break;
  163. case MMC_SIGNAL_VOLTAGE_180:
  164. fn = INTEL_DSM_V18_SWITCH;
  165. break;
  166. default:
  167. return 0;
  168. }
  169. err = intel_dsm(intel_host, dev, fn, &result);
  170. pr_debug("%s: %s DSM fn %u error %d result %u\n",
  171. mmc_hostname(mmc), __func__, fn, err, result);
  172. return 0;
  173. }
  174. static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
  175. {
  176. u8 reg;
  177. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  178. reg |= 0x10;
  179. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  180. /* For eMMC, minimum is 1us but give it 9us for good measure */
  181. udelay(9);
  182. reg &= ~0x10;
  183. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  184. /* For eMMC, minimum is 200us but give it 300us for good measure */
  185. usleep_range(300, 1000);
  186. }
  187. static const struct sdhci_ops sdhci_acpi_ops_dflt = {
  188. .set_clock = sdhci_set_clock,
  189. .set_bus_width = sdhci_set_bus_width,
  190. .reset = sdhci_reset,
  191. .set_uhs_signaling = sdhci_set_uhs_signaling,
  192. };
  193. static const struct sdhci_ops sdhci_acpi_ops_int = {
  194. .set_clock = sdhci_set_clock,
  195. .set_bus_width = sdhci_set_bus_width,
  196. .reset = sdhci_reset,
  197. .set_uhs_signaling = sdhci_set_uhs_signaling,
  198. .hw_reset = sdhci_acpi_int_hw_reset,
  199. };
  200. static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
  201. .ops = &sdhci_acpi_ops_int,
  202. };
  203. #ifdef CONFIG_X86
  204. #define BYT_IOSF_SCCEP 0x63
  205. #define BYT_IOSF_OCP_NETCTRL0 0x1078
  206. #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
  207. static void sdhci_acpi_byt_setting(struct device *dev)
  208. {
  209. u32 val = 0;
  210. if (!soc_intel_is_byt())
  211. return;
  212. if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
  213. &val)) {
  214. dev_err(dev, "%s read error\n", __func__);
  215. return;
  216. }
  217. if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
  218. return;
  219. val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
  220. if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
  221. val)) {
  222. dev_err(dev, "%s write error\n", __func__);
  223. return;
  224. }
  225. dev_dbg(dev, "%s completed\n", __func__);
  226. }
  227. static bool sdhci_acpi_byt_defer(struct device *dev)
  228. {
  229. if (!soc_intel_is_byt())
  230. return false;
  231. if (!iosf_mbi_available())
  232. return true;
  233. sdhci_acpi_byt_setting(dev);
  234. return false;
  235. }
  236. #else
  237. static inline void sdhci_acpi_byt_setting(struct device *dev)
  238. {
  239. }
  240. static inline bool sdhci_acpi_byt_defer(struct device *dev)
  241. {
  242. return false;
  243. }
  244. #endif
  245. static int bxt_get_cd(struct mmc_host *mmc)
  246. {
  247. int gpio_cd = mmc_gpio_get_cd(mmc);
  248. if (!gpio_cd)
  249. return 0;
  250. return sdhci_get_cd_nogpio(mmc);
  251. }
  252. static int intel_probe_slot(struct platform_device *pdev, struct acpi_device *adev)
  253. {
  254. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  255. struct intel_host *intel_host = sdhci_acpi_priv(c);
  256. struct sdhci_host *host = c->host;
  257. if (acpi_dev_hid_uid_match(adev, "80860F14", "1") &&
  258. sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
  259. sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
  260. host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  261. if (acpi_dev_hid_uid_match(adev, "80865ACA", NULL))
  262. host->mmc_host_ops.get_cd = bxt_get_cd;
  263. intel_dsm_init(intel_host, &pdev->dev, host->mmc);
  264. host->mmc_host_ops.start_signal_voltage_switch =
  265. intel_start_signal_voltage_switch;
  266. c->is_intel = true;
  267. return 0;
  268. }
  269. static int intel_setup_host(struct platform_device *pdev)
  270. {
  271. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  272. struct intel_host *intel_host = sdhci_acpi_priv(c);
  273. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25))
  274. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25;
  275. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50))
  276. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50;
  277. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50))
  278. c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50;
  279. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104))
  280. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
  281. return 0;
  282. }
  283. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
  284. .chip = &sdhci_acpi_chip_int,
  285. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  286. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
  287. MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
  288. .flags = SDHCI_ACPI_RUNTIME_PM,
  289. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  290. SDHCI_QUIRK_NO_LED,
  291. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  292. SDHCI_QUIRK2_STOP_WITH_TC |
  293. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
  294. .probe_slot = intel_probe_slot,
  295. .setup_host = intel_setup_host,
  296. .priv_size = sizeof(struct intel_host),
  297. };
  298. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  299. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  300. SDHCI_QUIRK_NO_LED |
  301. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  302. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  303. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
  304. MMC_CAP_WAIT_WHILE_BUSY,
  305. .flags = SDHCI_ACPI_RUNTIME_PM,
  306. .pm_caps = MMC_PM_KEEP_POWER,
  307. .probe_slot = intel_probe_slot,
  308. .setup_host = intel_setup_host,
  309. .priv_size = sizeof(struct intel_host),
  310. };
  311. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
  312. .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
  313. SDHCI_ACPI_RUNTIME_PM,
  314. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
  315. SDHCI_QUIRK_NO_LED,
  316. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  317. SDHCI_QUIRK2_STOP_WITH_TC,
  318. .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
  319. .probe_slot = intel_probe_slot,
  320. .setup_host = intel_setup_host,
  321. .priv_size = sizeof(struct intel_host),
  322. };
  323. #define VENDOR_SPECIFIC_PWRCTL_CLEAR_REG 0x1a8
  324. #define VENDOR_SPECIFIC_PWRCTL_CTL_REG 0x1ac
  325. static irqreturn_t sdhci_acpi_qcom_handler(int irq, void *ptr)
  326. {
  327. struct sdhci_host *host = ptr;
  328. sdhci_writel(host, 0x3, VENDOR_SPECIFIC_PWRCTL_CLEAR_REG);
  329. sdhci_writel(host, 0x1, VENDOR_SPECIFIC_PWRCTL_CTL_REG);
  330. return IRQ_HANDLED;
  331. }
  332. static int qcom_probe_slot(struct platform_device *pdev, struct acpi_device *adev)
  333. {
  334. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  335. struct sdhci_host *host = c->host;
  336. int *irq = sdhci_acpi_priv(c);
  337. *irq = -EINVAL;
  338. if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL))
  339. return 0;
  340. *irq = platform_get_irq(pdev, 1);
  341. if (*irq < 0)
  342. return 0;
  343. return request_threaded_irq(*irq, NULL, sdhci_acpi_qcom_handler,
  344. IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
  345. "sdhci_qcom", host);
  346. }
  347. static int qcom_free_slot(struct platform_device *pdev)
  348. {
  349. struct device *dev = &pdev->dev;
  350. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  351. struct sdhci_host *host = c->host;
  352. struct acpi_device *adev;
  353. int *irq = sdhci_acpi_priv(c);
  354. adev = ACPI_COMPANION(dev);
  355. if (!adev)
  356. return -ENODEV;
  357. if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL))
  358. return 0;
  359. if (*irq < 0)
  360. return 0;
  361. free_irq(*irq, host);
  362. return 0;
  363. }
  364. static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
  365. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  366. .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
  367. .caps = MMC_CAP_NONREMOVABLE,
  368. .priv_size = sizeof(int),
  369. .probe_slot = qcom_probe_slot,
  370. .free_slot = qcom_free_slot,
  371. };
  372. static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
  373. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  374. .caps = MMC_CAP_NONREMOVABLE,
  375. };
  376. struct amd_sdhci_host {
  377. bool tuned_clock;
  378. bool dll_enabled;
  379. };
  380. /* AMD sdhci reset dll register. */
  381. #define SDHCI_AMD_RESET_DLL_REGISTER 0x908
  382. static int amd_select_drive_strength(struct mmc_card *card,
  383. unsigned int max_dtr, int host_drv,
  384. int card_drv, int *host_driver_strength)
  385. {
  386. struct sdhci_host *host = mmc_priv(card->host);
  387. u16 preset, preset_driver_strength;
  388. /*
  389. * This method is only called by mmc_select_hs200 so we only need to
  390. * read from the HS200 (SDR104) preset register.
  391. *
  392. * Firmware that has "invalid/default" presets return a driver strength
  393. * of A. This matches the previously hard coded value.
  394. */
  395. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
  396. preset_driver_strength = FIELD_GET(SDHCI_PRESET_DRV_MASK, preset);
  397. /*
  398. * We want the controller driver strength to match the card's driver
  399. * strength so they have similar rise/fall times.
  400. *
  401. * The controller driver strength set by this method is sticky for all
  402. * timings after this method is called. This unfortunately means that
  403. * while HS400 tuning is in progress we end up with mismatched driver
  404. * strengths between the controller and the card. HS400 tuning requires
  405. * switching from HS400->DDR52->HS->HS200->HS400. So the driver mismatch
  406. * happens while in DDR52 and HS modes. This has not been observed to
  407. * cause problems. Enabling presets would fix this issue.
  408. */
  409. *host_driver_strength = preset_driver_strength;
  410. /*
  411. * The resulting card driver strength is only set when switching the
  412. * card's timing to HS200 or HS400. The card will use the default driver
  413. * strength (B) for any other mode.
  414. */
  415. return preset_driver_strength;
  416. }
  417. static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host, bool enable)
  418. {
  419. struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
  420. struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
  421. /* AMD Platform requires dll setting */
  422. sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
  423. usleep_range(10, 20);
  424. if (enable)
  425. sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
  426. amd_host->dll_enabled = enable;
  427. }
  428. /*
  429. * The initialization sequence for HS400 is:
  430. * HS->HS200->Perform Tuning->HS->HS400
  431. *
  432. * The re-tuning sequence is:
  433. * HS400->DDR52->HS->HS200->Perform Tuning->HS->HS400
  434. *
  435. * The AMD eMMC Controller can only use the tuned clock while in HS200 and HS400
  436. * mode. If we switch to a different mode, we need to disable the tuned clock.
  437. * If we have previously performed tuning and switch back to HS200 or
  438. * HS400, we can re-enable the tuned clock.
  439. *
  440. */
  441. static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  442. {
  443. struct sdhci_host *host = mmc_priv(mmc);
  444. struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
  445. struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
  446. unsigned int old_timing = host->timing;
  447. u16 val;
  448. sdhci_set_ios(mmc, ios);
  449. if (old_timing != host->timing && amd_host->tuned_clock) {
  450. if (host->timing == MMC_TIMING_MMC_HS400 ||
  451. host->timing == MMC_TIMING_MMC_HS200) {
  452. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  453. val |= SDHCI_CTRL_TUNED_CLK;
  454. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  455. } else {
  456. val = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  457. val &= ~SDHCI_CTRL_TUNED_CLK;
  458. sdhci_writew(host, val, SDHCI_HOST_CONTROL2);
  459. }
  460. /* DLL is only required for HS400 */
  461. if (host->timing == MMC_TIMING_MMC_HS400 &&
  462. !amd_host->dll_enabled)
  463. sdhci_acpi_amd_hs400_dll(host, true);
  464. }
  465. }
  466. static int amd_sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  467. {
  468. int err;
  469. struct sdhci_host *host = mmc_priv(mmc);
  470. struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
  471. struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
  472. amd_host->tuned_clock = false;
  473. err = sdhci_execute_tuning(mmc, opcode);
  474. if (!err && !host->tuning_err)
  475. amd_host->tuned_clock = true;
  476. return err;
  477. }
  478. static void amd_sdhci_reset(struct sdhci_host *host, u8 mask)
  479. {
  480. struct sdhci_acpi_host *acpi_host = sdhci_priv(host);
  481. struct amd_sdhci_host *amd_host = sdhci_acpi_priv(acpi_host);
  482. if (mask & SDHCI_RESET_ALL) {
  483. amd_host->tuned_clock = false;
  484. sdhci_acpi_amd_hs400_dll(host, false);
  485. }
  486. sdhci_reset(host, mask);
  487. }
  488. static const struct sdhci_ops sdhci_acpi_ops_amd = {
  489. .set_clock = sdhci_set_clock,
  490. .set_bus_width = sdhci_set_bus_width,
  491. .reset = amd_sdhci_reset,
  492. .set_uhs_signaling = sdhci_set_uhs_signaling,
  493. };
  494. static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
  495. .ops = &sdhci_acpi_ops_amd,
  496. };
  497. static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
  498. struct acpi_device *adev)
  499. {
  500. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  501. struct sdhci_host *host = c->host;
  502. sdhci_read_caps(host);
  503. if (host->caps1 & SDHCI_SUPPORT_DDR50)
  504. host->mmc->caps = MMC_CAP_1_8V_DDR;
  505. if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
  506. (host->mmc->caps & MMC_CAP_1_8V_DDR))
  507. host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
  508. /*
  509. * There are two types of presets out in the wild:
  510. * 1) Default/broken presets.
  511. * These presets have two sets of problems:
  512. * a) The clock divisor for SDR12, SDR25, and SDR50 is too small.
  513. * This results in clock frequencies that are 2x higher than
  514. * acceptable. i.e., SDR12 = 25 MHz, SDR25 = 50 MHz, SDR50 =
  515. * 100 MHz.x
  516. * b) The HS200 and HS400 driver strengths don't match.
  517. * By default, the SDR104 preset register has a driver strength of
  518. * A, but the (internal) HS400 preset register has a driver
  519. * strength of B. As part of initializing HS400, HS200 tuning
  520. * needs to be performed. Having different driver strengths
  521. * between tuning and operation is wrong. It results in different
  522. * rise/fall times that lead to incorrect sampling.
  523. * 2) Firmware with properly initialized presets.
  524. * These presets have proper clock divisors. i.e., SDR12 => 12MHz,
  525. * SDR25 => 25 MHz, SDR50 => 50 MHz. Additionally the HS200 and
  526. * HS400 preset driver strengths match.
  527. *
  528. * Enabling presets for HS400 doesn't work for the following reasons:
  529. * 1) sdhci_set_ios has a hard coded list of timings that are used
  530. * to determine if presets should be enabled.
  531. * 2) sdhci_get_preset_value is using a non-standard register to
  532. * read out HS400 presets. The AMD controller doesn't support this
  533. * non-standard register. In fact, it doesn't expose the HS400
  534. * preset register anywhere in the SDHCI memory map. This results
  535. * in reading a garbage value and using the wrong presets.
  536. *
  537. * Since HS400 and HS200 presets must be identical, we could
  538. * instead use the SDR104 preset register.
  539. *
  540. * If the above issues are resolved we could remove this quirk for
  541. * firmware that has valid presets (i.e., SDR12 <= 12 MHz).
  542. */
  543. host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
  544. host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
  545. host->mmc_host_ops.set_ios = amd_set_ios;
  546. host->mmc_host_ops.execute_tuning = amd_sdhci_execute_tuning;
  547. return 0;
  548. }
  549. static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
  550. .chip = &sdhci_acpi_chip_amd,
  551. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
  552. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
  553. SDHCI_QUIRK_32BIT_DMA_SIZE |
  554. SDHCI_QUIRK_32BIT_ADMA_SIZE,
  555. .quirks2 = SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
  556. .probe_slot = sdhci_acpi_emmc_amd_probe_slot,
  557. .priv_size = sizeof(struct amd_sdhci_host),
  558. };
  559. struct sdhci_acpi_uid_slot {
  560. const char *hid;
  561. const char *uid;
  562. const struct sdhci_acpi_slot *slot;
  563. };
  564. static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
  565. { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
  566. { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
  567. { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
  568. { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
  569. { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
  570. { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
  571. { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
  572. { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
  573. { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
  574. { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
  575. { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
  576. { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
  577. { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
  578. { "PNP0D40" },
  579. { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
  580. { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
  581. { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
  582. { "AMDI0041", NULL, &sdhci_acpi_slot_amd_emmc },
  583. { },
  584. };
  585. static const struct acpi_device_id sdhci_acpi_ids[] = {
  586. { "80865ACA" },
  587. { "80865ACC" },
  588. { "80865AD0" },
  589. { "80860F14" },
  590. { "80860F16" },
  591. { "INT33BB" },
  592. { "INT33C6" },
  593. { "INT3436" },
  594. { "INT344D" },
  595. { "PNP0D40" },
  596. { "QCOM8051" },
  597. { "QCOM8052" },
  598. { "AMDI0040" },
  599. { "AMDI0041" },
  600. { },
  601. };
  602. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  603. /* Please keep this list sorted alphabetically */
  604. static const struct dmi_system_id sdhci_acpi_quirks[] = {
  605. {
  606. /*
  607. * The Acer Aspire Switch 10 (SW5-012) microSD slot always
  608. * reports the card being write-protected even though microSD
  609. * cards do not have a write-protect switch at all.
  610. */
  611. .matches = {
  612. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  613. DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
  614. },
  615. .driver_data = (void *)DMI_QUIRK_SD_NO_WRITE_PROTECT,
  616. },
  617. {
  618. /* Asus T100TA, needs pull-up for cd but DSDT GpioInt has NoPull set */
  619. .matches = {
  620. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  621. DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
  622. },
  623. .driver_data = (void *)DMI_QUIRK_SD_CD_ENABLE_PULL_UP,
  624. },
  625. {
  626. /*
  627. * The Lenovo Miix 320-10ICR has a bug in the _PS0 method of
  628. * the SHC1 ACPI device, this bug causes it to reprogram the
  629. * wrong LDO (DLDO3) to 1.8V if 1.8V modes are used and the
  630. * card is (runtime) suspended + resumed. DLDO3 is used for
  631. * the LCD and setting it to 1.8V causes the LCD to go black.
  632. */
  633. .matches = {
  634. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  635. DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
  636. },
  637. .driver_data = (void *)DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP,
  638. },
  639. {
  640. /*
  641. * Lenovo Yoga Tablet 2 Pro 1380F/L (13" Android version) this
  642. * has broken WP reporting and an inverted CD signal.
  643. * Note this has more or less the same BIOS as the Lenovo Yoga
  644. * Tablet 2 830F/L or 1050F/L (8" and 10" Android), but unlike
  645. * the 830 / 1050 models which share the same mainboard this
  646. * model has a different mainboard and the inverted CD and
  647. * broken WP are unique to this board.
  648. */
  649. .matches = {
  650. DMI_MATCH(DMI_SYS_VENDOR, "Intel Corp."),
  651. DMI_MATCH(DMI_PRODUCT_NAME, "VALLEYVIEW C0 PLATFORM"),
  652. DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
  653. /* Full match so as to NOT match the 830/1050 BIOS */
  654. DMI_MATCH(DMI_BIOS_VERSION, "BLADE_21.X64.0005.R00.1504101516"),
  655. },
  656. .driver_data = (void *)(DMI_QUIRK_SD_NO_WRITE_PROTECT |
  657. DMI_QUIRK_SD_CD_ACTIVE_HIGH),
  658. },
  659. {
  660. /*
  661. * The Toshiba WT8-B's microSD slot always reports the card being
  662. * write-protected.
  663. */
  664. .matches = {
  665. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  666. DMI_MATCH(DMI_PRODUCT_NAME, "TOSHIBA ENCORE 2 WT8-B"),
  667. },
  668. .driver_data = (void *)DMI_QUIRK_SD_NO_WRITE_PROTECT,
  669. },
  670. {
  671. /*
  672. * The Toshiba WT10-A's microSD slot always reports the card being
  673. * write-protected.
  674. */
  675. .matches = {
  676. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  677. DMI_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A"),
  678. },
  679. .driver_data = (void *)DMI_QUIRK_SD_NO_WRITE_PROTECT,
  680. },
  681. {} /* Terminating entry */
  682. };
  683. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(struct acpi_device *adev)
  684. {
  685. const struct sdhci_acpi_uid_slot *u;
  686. for (u = sdhci_acpi_uids; u->hid; u++) {
  687. if (acpi_dev_hid_uid_match(adev, u->hid, u->uid))
  688. return u->slot;
  689. }
  690. return NULL;
  691. }
  692. static int sdhci_acpi_probe(struct platform_device *pdev)
  693. {
  694. struct device *dev = &pdev->dev;
  695. const struct sdhci_acpi_slot *slot;
  696. const struct dmi_system_id *id;
  697. struct acpi_device *device;
  698. struct sdhci_acpi_host *c;
  699. struct sdhci_host *host;
  700. struct resource *iomem;
  701. resource_size_t len;
  702. size_t priv_size;
  703. int quirks = 0;
  704. int err;
  705. device = ACPI_COMPANION(dev);
  706. if (!device)
  707. return -ENODEV;
  708. id = dmi_first_match(sdhci_acpi_quirks);
  709. if (id)
  710. quirks = (long)id->driver_data;
  711. slot = sdhci_acpi_get_slot(device);
  712. /* Power on the SDHCI controller and its children */
  713. acpi_device_fix_up_power_extended(device);
  714. if (sdhci_acpi_byt_defer(dev))
  715. return -EPROBE_DEFER;
  716. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  717. if (!iomem)
  718. return -ENOMEM;
  719. len = resource_size(iomem);
  720. if (len < 0x100)
  721. dev_err(dev, "Invalid iomem size!\n");
  722. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  723. return -ENOMEM;
  724. priv_size = slot ? slot->priv_size : 0;
  725. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
  726. if (IS_ERR(host))
  727. return PTR_ERR(host);
  728. c = sdhci_priv(host);
  729. c->host = host;
  730. c->slot = slot;
  731. c->pdev = pdev;
  732. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  733. platform_set_drvdata(pdev, c);
  734. host->hw_name = "ACPI";
  735. host->ops = &sdhci_acpi_ops_dflt;
  736. host->irq = platform_get_irq(pdev, 0);
  737. if (host->irq < 0) {
  738. err = host->irq;
  739. goto err_free;
  740. }
  741. host->ioaddr = devm_ioremap(dev, iomem->start,
  742. resource_size(iomem));
  743. if (host->ioaddr == NULL) {
  744. err = -ENOMEM;
  745. goto err_free;
  746. }
  747. if (c->slot) {
  748. if (c->slot->probe_slot) {
  749. err = c->slot->probe_slot(pdev, device);
  750. if (err)
  751. goto err_free;
  752. }
  753. if (c->slot->chip) {
  754. host->ops = c->slot->chip->ops;
  755. host->quirks |= c->slot->chip->quirks;
  756. host->quirks2 |= c->slot->chip->quirks2;
  757. host->mmc->caps |= c->slot->chip->caps;
  758. host->mmc->caps2 |= c->slot->chip->caps2;
  759. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  760. }
  761. host->quirks |= c->slot->quirks;
  762. host->quirks2 |= c->slot->quirks2;
  763. host->mmc->caps |= c->slot->caps;
  764. host->mmc->caps2 |= c->slot->caps2;
  765. host->mmc->pm_caps |= c->slot->pm_caps;
  766. }
  767. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  768. if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
  769. bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
  770. if (quirks & DMI_QUIRK_SD_CD_ACTIVE_HIGH)
  771. host->mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  772. err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0);
  773. if (err) {
  774. if (err == -EPROBE_DEFER)
  775. goto err_free;
  776. dev_warn(dev, "failed to setup card detect gpio\n");
  777. c->use_runtime_pm = false;
  778. } else if (quirks & DMI_QUIRK_SD_CD_ENABLE_PULL_UP) {
  779. mmc_gpiod_set_cd_config(host->mmc,
  780. PIN_CONF_PACKED(PIN_CONFIG_BIAS_PULL_UP, 20000));
  781. }
  782. if (quirks & DMI_QUIRK_RESET_SD_SIGNAL_VOLT_ON_SUSP)
  783. c->reset_signal_volt_on_suspend = true;
  784. if (quirks & DMI_QUIRK_SD_NO_WRITE_PROTECT)
  785. host->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  786. }
  787. err = sdhci_setup_host(host);
  788. if (err)
  789. goto err_free;
  790. if (c->slot && c->slot->setup_host) {
  791. err = c->slot->setup_host(pdev);
  792. if (err)
  793. goto err_cleanup;
  794. }
  795. err = __sdhci_add_host(host);
  796. if (err)
  797. goto err_cleanup;
  798. if (c->use_runtime_pm) {
  799. pm_runtime_set_active(dev);
  800. pm_suspend_ignore_children(dev, 1);
  801. pm_runtime_set_autosuspend_delay(dev, 50);
  802. pm_runtime_use_autosuspend(dev);
  803. pm_runtime_enable(dev);
  804. }
  805. device_enable_async_suspend(dev);
  806. return 0;
  807. err_cleanup:
  808. sdhci_cleanup_host(c->host);
  809. err_free:
  810. if (c->slot && c->slot->free_slot)
  811. c->slot->free_slot(pdev);
  812. sdhci_free_host(c->host);
  813. return err;
  814. }
  815. static void sdhci_acpi_remove(struct platform_device *pdev)
  816. {
  817. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  818. struct device *dev = &pdev->dev;
  819. int dead;
  820. if (c->use_runtime_pm) {
  821. pm_runtime_get_sync(dev);
  822. pm_runtime_disable(dev);
  823. pm_runtime_put_noidle(dev);
  824. }
  825. if (c->slot && c->slot->remove_slot)
  826. c->slot->remove_slot(pdev);
  827. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  828. sdhci_remove_host(c->host, dead);
  829. if (c->slot && c->slot->free_slot)
  830. c->slot->free_slot(pdev);
  831. sdhci_free_host(c->host);
  832. }
  833. static void __maybe_unused sdhci_acpi_reset_signal_voltage_if_needed(
  834. struct device *dev)
  835. {
  836. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  837. struct sdhci_host *host = c->host;
  838. if (c->is_intel && c->reset_signal_volt_on_suspend &&
  839. host->mmc->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_330) {
  840. struct intel_host *intel_host = sdhci_acpi_priv(c);
  841. unsigned int fn = INTEL_DSM_V33_SWITCH;
  842. u32 result = 0;
  843. intel_dsm(intel_host, dev, fn, &result);
  844. }
  845. }
  846. #ifdef CONFIG_PM_SLEEP
  847. static int sdhci_acpi_suspend(struct device *dev)
  848. {
  849. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  850. struct sdhci_host *host = c->host;
  851. int ret;
  852. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  853. mmc_retune_needed(host->mmc);
  854. ret = sdhci_suspend_host(host);
  855. if (ret)
  856. return ret;
  857. sdhci_acpi_reset_signal_voltage_if_needed(dev);
  858. return 0;
  859. }
  860. static int sdhci_acpi_resume(struct device *dev)
  861. {
  862. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  863. sdhci_acpi_byt_setting(&c->pdev->dev);
  864. return sdhci_resume_host(c->host);
  865. }
  866. #endif
  867. #ifdef CONFIG_PM
  868. static int sdhci_acpi_runtime_suspend(struct device *dev)
  869. {
  870. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  871. struct sdhci_host *host = c->host;
  872. int ret;
  873. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  874. mmc_retune_needed(host->mmc);
  875. ret = sdhci_runtime_suspend_host(host);
  876. if (ret)
  877. return ret;
  878. sdhci_acpi_reset_signal_voltage_if_needed(dev);
  879. return 0;
  880. }
  881. static int sdhci_acpi_runtime_resume(struct device *dev)
  882. {
  883. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  884. sdhci_acpi_byt_setting(&c->pdev->dev);
  885. return sdhci_runtime_resume_host(c->host, 0);
  886. }
  887. #endif
  888. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  889. SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
  890. SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
  891. sdhci_acpi_runtime_resume, NULL)
  892. };
  893. static struct platform_driver sdhci_acpi_driver = {
  894. .driver = {
  895. .name = "sdhci-acpi",
  896. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  897. .acpi_match_table = sdhci_acpi_ids,
  898. .pm = &sdhci_acpi_pm_ops,
  899. },
  900. .probe = sdhci_acpi_probe,
  901. .remove_new = sdhci_acpi_remove,
  902. };
  903. module_platform_driver(sdhci_acpi_driver);
  904. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  905. MODULE_AUTHOR("Adrian Hunter");
  906. MODULE_LICENSE("GPL v2");