sdhci-acpi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * Secure Digital Host Controller Interface ACPI driver.
  3. *
  4. * Copyright (c) 2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/export.h>
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/ioport.h>
  26. #include <linux/io.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/compiler.h>
  29. #include <linux/stddef.h>
  30. #include <linux/bitops.h>
  31. #include <linux/types.h>
  32. #include <linux/err.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/acpi.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/delay.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/pm.h>
  40. #include <linux/mmc/slot-gpio.h>
  41. #ifdef CONFIG_X86
  42. #include <asm/cpu_device_id.h>
  43. #include <asm/intel-family.h>
  44. #include <asm/iosf_mbi.h>
  45. #include <linux/pci.h>
  46. #endif
  47. #include "sdhci.h"
  48. enum {
  49. SDHCI_ACPI_SD_CD = BIT(0),
  50. SDHCI_ACPI_RUNTIME_PM = BIT(1),
  51. SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
  52. };
  53. struct sdhci_acpi_chip {
  54. const struct sdhci_ops *ops;
  55. unsigned int quirks;
  56. unsigned int quirks2;
  57. unsigned long caps;
  58. unsigned int caps2;
  59. mmc_pm_flag_t pm_caps;
  60. };
  61. struct sdhci_acpi_slot {
  62. const struct sdhci_acpi_chip *chip;
  63. unsigned int quirks;
  64. unsigned int quirks2;
  65. unsigned long caps;
  66. unsigned int caps2;
  67. mmc_pm_flag_t pm_caps;
  68. unsigned int flags;
  69. size_t priv_size;
  70. int (*probe_slot)(struct platform_device *, const char *, const char *);
  71. int (*remove_slot)(struct platform_device *);
  72. int (*free_slot)(struct platform_device *pdev);
  73. int (*setup_host)(struct platform_device *pdev);
  74. };
  75. struct sdhci_acpi_host {
  76. struct sdhci_host *host;
  77. const struct sdhci_acpi_slot *slot;
  78. struct platform_device *pdev;
  79. bool use_runtime_pm;
  80. unsigned long private[0] ____cacheline_aligned;
  81. };
  82. static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
  83. {
  84. return (void *)c->private;
  85. }
  86. static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
  87. {
  88. return c->slot && (c->slot->flags & flag);
  89. }
  90. #define INTEL_DSM_HS_CAPS_SDR25 BIT(0)
  91. #define INTEL_DSM_HS_CAPS_DDR50 BIT(1)
  92. #define INTEL_DSM_HS_CAPS_SDR50 BIT(2)
  93. #define INTEL_DSM_HS_CAPS_SDR104 BIT(3)
  94. enum {
  95. INTEL_DSM_FNS = 0,
  96. INTEL_DSM_V18_SWITCH = 3,
  97. INTEL_DSM_V33_SWITCH = 4,
  98. INTEL_DSM_HS_CAPS = 8,
  99. };
  100. struct intel_host {
  101. u32 dsm_fns;
  102. u32 hs_caps;
  103. };
  104. static const guid_t intel_dsm_guid =
  105. GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
  106. 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
  107. static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
  108. unsigned int fn, u32 *result)
  109. {
  110. union acpi_object *obj;
  111. int err = 0;
  112. obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
  113. if (!obj)
  114. return -EOPNOTSUPP;
  115. if (obj->type == ACPI_TYPE_INTEGER) {
  116. *result = obj->integer.value;
  117. } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
  118. size_t len = min_t(size_t, obj->buffer.length, 4);
  119. *result = 0;
  120. memcpy(result, obj->buffer.pointer, len);
  121. } else {
  122. dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
  123. __func__, fn, obj->type, obj->buffer.length);
  124. err = -EINVAL;
  125. }
  126. ACPI_FREE(obj);
  127. return err;
  128. }
  129. static int intel_dsm(struct intel_host *intel_host, struct device *dev,
  130. unsigned int fn, u32 *result)
  131. {
  132. if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
  133. return -EOPNOTSUPP;
  134. return __intel_dsm(intel_host, dev, fn, result);
  135. }
  136. static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
  137. struct mmc_host *mmc)
  138. {
  139. int err;
  140. intel_host->hs_caps = ~0;
  141. err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
  142. if (err) {
  143. pr_debug("%s: DSM not supported, error %d\n",
  144. mmc_hostname(mmc), err);
  145. return;
  146. }
  147. pr_debug("%s: DSM function mask %#x\n",
  148. mmc_hostname(mmc), intel_host->dsm_fns);
  149. intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps);
  150. }
  151. static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
  152. struct mmc_ios *ios)
  153. {
  154. struct device *dev = mmc_dev(mmc);
  155. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  156. struct intel_host *intel_host = sdhci_acpi_priv(c);
  157. unsigned int fn;
  158. u32 result = 0;
  159. int err;
  160. err = sdhci_start_signal_voltage_switch(mmc, ios);
  161. if (err)
  162. return err;
  163. switch (ios->signal_voltage) {
  164. case MMC_SIGNAL_VOLTAGE_330:
  165. fn = INTEL_DSM_V33_SWITCH;
  166. break;
  167. case MMC_SIGNAL_VOLTAGE_180:
  168. fn = INTEL_DSM_V18_SWITCH;
  169. break;
  170. default:
  171. return 0;
  172. }
  173. err = intel_dsm(intel_host, dev, fn, &result);
  174. pr_debug("%s: %s DSM fn %u error %d result %u\n",
  175. mmc_hostname(mmc), __func__, fn, err, result);
  176. return 0;
  177. }
  178. static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
  179. {
  180. u8 reg;
  181. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  182. reg |= 0x10;
  183. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  184. /* For eMMC, minimum is 1us but give it 9us for good measure */
  185. udelay(9);
  186. reg &= ~0x10;
  187. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  188. /* For eMMC, minimum is 200us but give it 300us for good measure */
  189. usleep_range(300, 1000);
  190. }
  191. static const struct sdhci_ops sdhci_acpi_ops_dflt = {
  192. .set_clock = sdhci_set_clock,
  193. .set_bus_width = sdhci_set_bus_width,
  194. .reset = sdhci_reset,
  195. .set_uhs_signaling = sdhci_set_uhs_signaling,
  196. };
  197. static const struct sdhci_ops sdhci_acpi_ops_int = {
  198. .set_clock = sdhci_set_clock,
  199. .set_bus_width = sdhci_set_bus_width,
  200. .reset = sdhci_reset,
  201. .set_uhs_signaling = sdhci_set_uhs_signaling,
  202. .hw_reset = sdhci_acpi_int_hw_reset,
  203. };
  204. static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
  205. .ops = &sdhci_acpi_ops_int,
  206. };
  207. #ifdef CONFIG_X86
  208. static bool sdhci_acpi_byt(void)
  209. {
  210. static const struct x86_cpu_id byt[] = {
  211. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT },
  212. {}
  213. };
  214. return x86_match_cpu(byt);
  215. }
  216. static bool sdhci_acpi_cht(void)
  217. {
  218. static const struct x86_cpu_id cht[] = {
  219. { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
  220. {}
  221. };
  222. return x86_match_cpu(cht);
  223. }
  224. #define BYT_IOSF_SCCEP 0x63
  225. #define BYT_IOSF_OCP_NETCTRL0 0x1078
  226. #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
  227. static void sdhci_acpi_byt_setting(struct device *dev)
  228. {
  229. u32 val = 0;
  230. if (!sdhci_acpi_byt())
  231. return;
  232. if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
  233. &val)) {
  234. dev_err(dev, "%s read error\n", __func__);
  235. return;
  236. }
  237. if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
  238. return;
  239. val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
  240. if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
  241. val)) {
  242. dev_err(dev, "%s write error\n", __func__);
  243. return;
  244. }
  245. dev_dbg(dev, "%s completed\n", __func__);
  246. }
  247. static bool sdhci_acpi_byt_defer(struct device *dev)
  248. {
  249. if (!sdhci_acpi_byt())
  250. return false;
  251. if (!iosf_mbi_available())
  252. return true;
  253. sdhci_acpi_byt_setting(dev);
  254. return false;
  255. }
  256. static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
  257. unsigned int slot, unsigned int parent_slot)
  258. {
  259. struct pci_dev *dev, *parent, *from = NULL;
  260. while (1) {
  261. dev = pci_get_device(vendor, device, from);
  262. pci_dev_put(from);
  263. if (!dev)
  264. break;
  265. parent = pci_upstream_bridge(dev);
  266. if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
  267. parent && PCI_SLOT(parent->devfn) == parent_slot &&
  268. !pci_upstream_bridge(parent)) {
  269. pci_dev_put(dev);
  270. return true;
  271. }
  272. from = dev;
  273. }
  274. return false;
  275. }
  276. /*
  277. * GPDwin uses PCI wifi which conflicts with SDIO's use of
  278. * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
  279. * problematic, but since SDIO is only used for wifi, the presence of the PCI
  280. * wifi card in the expected slot with an ACPI companion node, is used to
  281. * indicate that acpi_device_fix_up_power() should be avoided.
  282. */
  283. static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
  284. const char *uid)
  285. {
  286. return sdhci_acpi_cht() &&
  287. !strcmp(hid, "80860F14") &&
  288. !strcmp(uid, "2") &&
  289. sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
  290. }
  291. #else
  292. static inline void sdhci_acpi_byt_setting(struct device *dev)
  293. {
  294. }
  295. static inline bool sdhci_acpi_byt_defer(struct device *dev)
  296. {
  297. return false;
  298. }
  299. static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
  300. const char *uid)
  301. {
  302. return false;
  303. }
  304. #endif
  305. static int bxt_get_cd(struct mmc_host *mmc)
  306. {
  307. int gpio_cd = mmc_gpio_get_cd(mmc);
  308. struct sdhci_host *host = mmc_priv(mmc);
  309. unsigned long flags;
  310. int ret = 0;
  311. if (!gpio_cd)
  312. return 0;
  313. spin_lock_irqsave(&host->lock, flags);
  314. if (host->flags & SDHCI_DEVICE_DEAD)
  315. goto out;
  316. ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  317. out:
  318. spin_unlock_irqrestore(&host->lock, flags);
  319. return ret;
  320. }
  321. static int intel_probe_slot(struct platform_device *pdev, const char *hid,
  322. const char *uid)
  323. {
  324. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  325. struct intel_host *intel_host = sdhci_acpi_priv(c);
  326. struct sdhci_host *host = c->host;
  327. if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
  328. sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
  329. sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
  330. host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  331. if (hid && !strcmp(hid, "80865ACA"))
  332. host->mmc_host_ops.get_cd = bxt_get_cd;
  333. intel_dsm_init(intel_host, &pdev->dev, host->mmc);
  334. host->mmc_host_ops.start_signal_voltage_switch =
  335. intel_start_signal_voltage_switch;
  336. return 0;
  337. }
  338. static int intel_setup_host(struct platform_device *pdev)
  339. {
  340. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  341. struct intel_host *intel_host = sdhci_acpi_priv(c);
  342. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25))
  343. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25;
  344. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50))
  345. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50;
  346. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50))
  347. c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50;
  348. if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104))
  349. c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
  350. return 0;
  351. }
  352. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
  353. .chip = &sdhci_acpi_chip_int,
  354. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  355. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
  356. MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
  357. .flags = SDHCI_ACPI_RUNTIME_PM,
  358. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  359. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
  360. SDHCI_QUIRK2_STOP_WITH_TC |
  361. SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
  362. .probe_slot = intel_probe_slot,
  363. .setup_host = intel_setup_host,
  364. .priv_size = sizeof(struct intel_host),
  365. };
  366. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  367. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  368. SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  369. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  370. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
  371. MMC_CAP_WAIT_WHILE_BUSY,
  372. .flags = SDHCI_ACPI_RUNTIME_PM,
  373. .pm_caps = MMC_PM_KEEP_POWER,
  374. .probe_slot = intel_probe_slot,
  375. .setup_host = intel_setup_host,
  376. .priv_size = sizeof(struct intel_host),
  377. };
  378. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
  379. .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
  380. SDHCI_ACPI_RUNTIME_PM,
  381. .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
  382. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  383. SDHCI_QUIRK2_STOP_WITH_TC,
  384. .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
  385. .probe_slot = intel_probe_slot,
  386. .setup_host = intel_setup_host,
  387. .priv_size = sizeof(struct intel_host),
  388. };
  389. #define VENDOR_SPECIFIC_PWRCTL_CLEAR_REG 0x1a8
  390. #define VENDOR_SPECIFIC_PWRCTL_CTL_REG 0x1ac
  391. static irqreturn_t sdhci_acpi_qcom_handler(int irq, void *ptr)
  392. {
  393. struct sdhci_host *host = ptr;
  394. sdhci_writel(host, 0x3, VENDOR_SPECIFIC_PWRCTL_CLEAR_REG);
  395. sdhci_writel(host, 0x1, VENDOR_SPECIFIC_PWRCTL_CTL_REG);
  396. return IRQ_HANDLED;
  397. }
  398. static int qcom_probe_slot(struct platform_device *pdev, const char *hid,
  399. const char *uid)
  400. {
  401. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  402. struct sdhci_host *host = c->host;
  403. int *irq = sdhci_acpi_priv(c);
  404. *irq = -EINVAL;
  405. if (strcmp(hid, "QCOM8051"))
  406. return 0;
  407. *irq = platform_get_irq(pdev, 1);
  408. if (*irq < 0)
  409. return 0;
  410. return request_threaded_irq(*irq, NULL, sdhci_acpi_qcom_handler,
  411. IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
  412. "sdhci_qcom", host);
  413. }
  414. static int qcom_free_slot(struct platform_device *pdev)
  415. {
  416. struct device *dev = &pdev->dev;
  417. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  418. struct sdhci_host *host = c->host;
  419. struct acpi_device *adev;
  420. int *irq = sdhci_acpi_priv(c);
  421. const char *hid;
  422. adev = ACPI_COMPANION(dev);
  423. if (!adev)
  424. return -ENODEV;
  425. hid = acpi_device_hid(adev);
  426. if (strcmp(hid, "QCOM8051"))
  427. return 0;
  428. if (*irq < 0)
  429. return 0;
  430. free_irq(*irq, host);
  431. return 0;
  432. }
  433. static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
  434. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  435. .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
  436. .caps = MMC_CAP_NONREMOVABLE,
  437. .priv_size = sizeof(int),
  438. .probe_slot = qcom_probe_slot,
  439. .free_slot = qcom_free_slot,
  440. };
  441. static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
  442. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  443. .caps = MMC_CAP_NONREMOVABLE,
  444. };
  445. /* AMD sdhci reset dll register. */
  446. #define SDHCI_AMD_RESET_DLL_REGISTER 0x908
  447. static int amd_select_drive_strength(struct mmc_card *card,
  448. unsigned int max_dtr, int host_drv,
  449. int card_drv, int *drv_type)
  450. {
  451. return MMC_SET_DRIVER_TYPE_A;
  452. }
  453. static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host)
  454. {
  455. /* AMD Platform requires dll setting */
  456. sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
  457. usleep_range(10, 20);
  458. sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
  459. }
  460. /*
  461. * For AMD Platform it is required to disable the tuning
  462. * bit first controller to bring to HS Mode from HS200
  463. * mode, later enable to tune to HS400 mode.
  464. */
  465. static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  466. {
  467. struct sdhci_host *host = mmc_priv(mmc);
  468. unsigned int old_timing = host->timing;
  469. sdhci_set_ios(mmc, ios);
  470. if (old_timing == MMC_TIMING_MMC_HS200 &&
  471. ios->timing == MMC_TIMING_MMC_HS)
  472. sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2);
  473. if (old_timing != MMC_TIMING_MMC_HS400 &&
  474. ios->timing == MMC_TIMING_MMC_HS400) {
  475. sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2);
  476. sdhci_acpi_amd_hs400_dll(host);
  477. }
  478. }
  479. static const struct sdhci_ops sdhci_acpi_ops_amd = {
  480. .set_clock = sdhci_set_clock,
  481. .set_bus_width = sdhci_set_bus_width,
  482. .reset = sdhci_reset,
  483. .set_uhs_signaling = sdhci_set_uhs_signaling,
  484. };
  485. static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
  486. .ops = &sdhci_acpi_ops_amd,
  487. };
  488. static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
  489. const char *hid, const char *uid)
  490. {
  491. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  492. struct sdhci_host *host = c->host;
  493. sdhci_read_caps(host);
  494. if (host->caps1 & SDHCI_SUPPORT_DDR50)
  495. host->mmc->caps = MMC_CAP_1_8V_DDR;
  496. if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
  497. (host->mmc->caps & MMC_CAP_1_8V_DDR))
  498. host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
  499. host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
  500. host->mmc_host_ops.set_ios = amd_set_ios;
  501. return 0;
  502. }
  503. static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
  504. .chip = &sdhci_acpi_chip_amd,
  505. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
  506. .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE |
  507. SDHCI_QUIRK_32BIT_ADMA_SIZE,
  508. .probe_slot = sdhci_acpi_emmc_amd_probe_slot,
  509. };
  510. struct sdhci_acpi_uid_slot {
  511. const char *hid;
  512. const char *uid;
  513. const struct sdhci_acpi_slot *slot;
  514. };
  515. static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
  516. { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
  517. { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
  518. { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
  519. { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
  520. { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
  521. { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
  522. { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
  523. { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
  524. { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
  525. { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
  526. { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
  527. { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
  528. { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
  529. { "PNP0D40" },
  530. { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
  531. { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
  532. { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
  533. { },
  534. };
  535. static const struct acpi_device_id sdhci_acpi_ids[] = {
  536. { "80865ACA" },
  537. { "80865ACC" },
  538. { "80865AD0" },
  539. { "80860F14" },
  540. { "80860F16" },
  541. { "INT33BB" },
  542. { "INT33C6" },
  543. { "INT3436" },
  544. { "INT344D" },
  545. { "PNP0D40" },
  546. { "QCOM8051" },
  547. { "QCOM8052" },
  548. { "AMDI0040" },
  549. { },
  550. };
  551. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  552. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
  553. const char *uid)
  554. {
  555. const struct sdhci_acpi_uid_slot *u;
  556. for (u = sdhci_acpi_uids; u->hid; u++) {
  557. if (strcmp(u->hid, hid))
  558. continue;
  559. if (!u->uid)
  560. return u->slot;
  561. if (uid && !strcmp(u->uid, uid))
  562. return u->slot;
  563. }
  564. return NULL;
  565. }
  566. static int sdhci_acpi_probe(struct platform_device *pdev)
  567. {
  568. struct device *dev = &pdev->dev;
  569. const struct sdhci_acpi_slot *slot;
  570. struct acpi_device *device, *child;
  571. struct sdhci_acpi_host *c;
  572. struct sdhci_host *host;
  573. struct resource *iomem;
  574. resource_size_t len;
  575. size_t priv_size;
  576. const char *hid;
  577. const char *uid;
  578. int err;
  579. device = ACPI_COMPANION(dev);
  580. if (!device)
  581. return -ENODEV;
  582. hid = acpi_device_hid(device);
  583. uid = acpi_device_uid(device);
  584. slot = sdhci_acpi_get_slot(hid, uid);
  585. /* Power on the SDHCI controller and its children */
  586. acpi_device_fix_up_power(device);
  587. if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
  588. list_for_each_entry(child, &device->children, node)
  589. if (child->status.present && child->status.enabled)
  590. acpi_device_fix_up_power(child);
  591. }
  592. if (sdhci_acpi_byt_defer(dev))
  593. return -EPROBE_DEFER;
  594. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  595. if (!iomem)
  596. return -ENOMEM;
  597. len = resource_size(iomem);
  598. if (len < 0x100)
  599. dev_err(dev, "Invalid iomem size!\n");
  600. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  601. return -ENOMEM;
  602. priv_size = slot ? slot->priv_size : 0;
  603. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
  604. if (IS_ERR(host))
  605. return PTR_ERR(host);
  606. c = sdhci_priv(host);
  607. c->host = host;
  608. c->slot = slot;
  609. c->pdev = pdev;
  610. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  611. platform_set_drvdata(pdev, c);
  612. host->hw_name = "ACPI";
  613. host->ops = &sdhci_acpi_ops_dflt;
  614. host->irq = platform_get_irq(pdev, 0);
  615. if (host->irq < 0) {
  616. err = -EINVAL;
  617. goto err_free;
  618. }
  619. host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
  620. resource_size(iomem));
  621. if (host->ioaddr == NULL) {
  622. err = -ENOMEM;
  623. goto err_free;
  624. }
  625. if (c->slot) {
  626. if (c->slot->probe_slot) {
  627. err = c->slot->probe_slot(pdev, hid, uid);
  628. if (err)
  629. goto err_free;
  630. }
  631. if (c->slot->chip) {
  632. host->ops = c->slot->chip->ops;
  633. host->quirks |= c->slot->chip->quirks;
  634. host->quirks2 |= c->slot->chip->quirks2;
  635. host->mmc->caps |= c->slot->chip->caps;
  636. host->mmc->caps2 |= c->slot->chip->caps2;
  637. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  638. }
  639. host->quirks |= c->slot->quirks;
  640. host->quirks2 |= c->slot->quirks2;
  641. host->mmc->caps |= c->slot->caps;
  642. host->mmc->caps2 |= c->slot->caps2;
  643. host->mmc->pm_caps |= c->slot->pm_caps;
  644. }
  645. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  646. if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
  647. bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
  648. err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
  649. if (err) {
  650. if (err == -EPROBE_DEFER)
  651. goto err_free;
  652. dev_warn(dev, "failed to setup card detect gpio\n");
  653. c->use_runtime_pm = false;
  654. }
  655. }
  656. err = sdhci_setup_host(host);
  657. if (err)
  658. goto err_free;
  659. if (c->slot && c->slot->setup_host) {
  660. err = c->slot->setup_host(pdev);
  661. if (err)
  662. goto err_cleanup;
  663. }
  664. err = __sdhci_add_host(host);
  665. if (err)
  666. goto err_cleanup;
  667. if (c->use_runtime_pm) {
  668. pm_runtime_set_active(dev);
  669. pm_suspend_ignore_children(dev, 1);
  670. pm_runtime_set_autosuspend_delay(dev, 50);
  671. pm_runtime_use_autosuspend(dev);
  672. pm_runtime_enable(dev);
  673. }
  674. device_enable_async_suspend(dev);
  675. return 0;
  676. err_cleanup:
  677. sdhci_cleanup_host(c->host);
  678. err_free:
  679. if (c->slot && c->slot->free_slot)
  680. c->slot->free_slot(pdev);
  681. sdhci_free_host(c->host);
  682. return err;
  683. }
  684. static int sdhci_acpi_remove(struct platform_device *pdev)
  685. {
  686. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  687. struct device *dev = &pdev->dev;
  688. int dead;
  689. if (c->use_runtime_pm) {
  690. pm_runtime_get_sync(dev);
  691. pm_runtime_disable(dev);
  692. pm_runtime_put_noidle(dev);
  693. }
  694. if (c->slot && c->slot->remove_slot)
  695. c->slot->remove_slot(pdev);
  696. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  697. sdhci_remove_host(c->host, dead);
  698. if (c->slot && c->slot->free_slot)
  699. c->slot->free_slot(pdev);
  700. sdhci_free_host(c->host);
  701. return 0;
  702. }
  703. #ifdef CONFIG_PM_SLEEP
  704. static int sdhci_acpi_suspend(struct device *dev)
  705. {
  706. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  707. struct sdhci_host *host = c->host;
  708. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  709. mmc_retune_needed(host->mmc);
  710. return sdhci_suspend_host(host);
  711. }
  712. static int sdhci_acpi_resume(struct device *dev)
  713. {
  714. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  715. sdhci_acpi_byt_setting(&c->pdev->dev);
  716. return sdhci_resume_host(c->host);
  717. }
  718. #endif
  719. #ifdef CONFIG_PM
  720. static int sdhci_acpi_runtime_suspend(struct device *dev)
  721. {
  722. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  723. struct sdhci_host *host = c->host;
  724. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  725. mmc_retune_needed(host->mmc);
  726. return sdhci_runtime_suspend_host(host);
  727. }
  728. static int sdhci_acpi_runtime_resume(struct device *dev)
  729. {
  730. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  731. sdhci_acpi_byt_setting(&c->pdev->dev);
  732. return sdhci_runtime_resume_host(c->host);
  733. }
  734. #endif
  735. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  736. SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
  737. SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
  738. sdhci_acpi_runtime_resume, NULL)
  739. };
  740. static struct platform_driver sdhci_acpi_driver = {
  741. .driver = {
  742. .name = "sdhci-acpi",
  743. .acpi_match_table = sdhci_acpi_ids,
  744. .pm = &sdhci_acpi_pm_ops,
  745. },
  746. .probe = sdhci_acpi_probe,
  747. .remove = sdhci_acpi_remove,
  748. };
  749. module_platform_driver(sdhci_acpi_driver);
  750. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  751. MODULE_AUTHOR("Adrian Hunter");
  752. MODULE_LICENSE("GPL v2");