1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c4e05037SAdrian Hunter /* 3c4e05037SAdrian Hunter * Secure Digital Host Controller Interface ACPI driver. 4c4e05037SAdrian Hunter * 5c4e05037SAdrian Hunter * Copyright (c) 2012, Intel Corporation. 6c4e05037SAdrian Hunter */ 7c4e05037SAdrian Hunter 8c4e05037SAdrian Hunter #include <linux/init.h> 9c4e05037SAdrian Hunter #include <linux/export.h> 10c4e05037SAdrian Hunter #include <linux/module.h> 11c4e05037SAdrian Hunter #include <linux/device.h> 12c4e05037SAdrian Hunter #include <linux/platform_device.h> 13c4e05037SAdrian Hunter #include <linux/ioport.h> 14c4e05037SAdrian Hunter #include <linux/io.h> 15c4e05037SAdrian Hunter #include <linux/dma-mapping.h> 16c4e05037SAdrian Hunter #include <linux/compiler.h> 17c4e05037SAdrian Hunter #include <linux/stddef.h> 18c4e05037SAdrian Hunter #include <linux/bitops.h> 19c4e05037SAdrian Hunter #include <linux/types.h> 20c4e05037SAdrian Hunter #include <linux/err.h> 21c4e05037SAdrian Hunter #include <linux/interrupt.h> 22c4e05037SAdrian Hunter #include <linux/acpi.h> 23c4e05037SAdrian Hunter #include <linux/pm.h> 24c4e05037SAdrian Hunter #include <linux/pm_runtime.h> 25b04fa064SAdrian Hunter #include <linux/delay.h> 26c4e05037SAdrian Hunter 27c4e05037SAdrian Hunter #include <linux/mmc/host.h> 28c4e05037SAdrian Hunter #include <linux/mmc/pm.h> 294fd4409cSAdrian Hunter #include <linux/mmc/slot-gpio.h> 30c4e05037SAdrian Hunter 316e1c7d61SAdrian Hunter #ifdef CONFIG_X86 326e1c7d61SAdrian Hunter #include <asm/cpu_device_id.h> 338ba4cb53SDave Hansen #include <asm/intel-family.h> 346e1c7d61SAdrian Hunter #include <asm/iosf_mbi.h> 3517753d16SAdrian Hunter #include <linux/pci.h> 366e1c7d61SAdrian Hunter #endif 376e1c7d61SAdrian Hunter 38c4e05037SAdrian Hunter #include "sdhci.h" 39c4e05037SAdrian Hunter 40c4e05037SAdrian Hunter enum { 41c4e05037SAdrian Hunter SDHCI_ACPI_SD_CD = BIT(0), 42c4e05037SAdrian Hunter SDHCI_ACPI_RUNTIME_PM = BIT(1), 434fd4409cSAdrian Hunter SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2), 44c4e05037SAdrian Hunter }; 45c4e05037SAdrian Hunter 46c4e05037SAdrian Hunter struct sdhci_acpi_chip { 47c4e05037SAdrian Hunter const struct sdhci_ops *ops; 48c4e05037SAdrian Hunter unsigned int quirks; 49c4e05037SAdrian Hunter unsigned int quirks2; 50c4e05037SAdrian Hunter unsigned long caps; 51c4e05037SAdrian Hunter unsigned int caps2; 52c4e05037SAdrian Hunter mmc_pm_flag_t pm_caps; 53c4e05037SAdrian Hunter }; 54c4e05037SAdrian Hunter 55c4e05037SAdrian Hunter struct sdhci_acpi_slot { 56c4e05037SAdrian Hunter const struct sdhci_acpi_chip *chip; 57c4e05037SAdrian Hunter unsigned int quirks; 58c4e05037SAdrian Hunter unsigned int quirks2; 59c4e05037SAdrian Hunter unsigned long caps; 60c4e05037SAdrian Hunter unsigned int caps2; 61c4e05037SAdrian Hunter mmc_pm_flag_t pm_caps; 62c4e05037SAdrian Hunter unsigned int flags; 63f07b7952SAdrian Hunter size_t priv_size; 644f3cde3aSAndy Shevchenko int (*probe_slot)(struct platform_device *, struct acpi_device *); 65578b36b6SGao, Yunpeng int (*remove_slot)(struct platform_device *); 66c7eabbeeSWang Dongsheng int (*free_slot)(struct platform_device *pdev); 670cc1a0f4SAdrian Hunter int (*setup_host)(struct platform_device *pdev); 68c4e05037SAdrian Hunter }; 69c4e05037SAdrian Hunter 70c4e05037SAdrian Hunter struct sdhci_acpi_host { 71c4e05037SAdrian Hunter struct sdhci_host *host; 72c4e05037SAdrian Hunter const struct sdhci_acpi_slot *slot; 73c4e05037SAdrian Hunter struct platform_device *pdev; 74c4e05037SAdrian Hunter bool use_runtime_pm; 75f07b7952SAdrian Hunter unsigned long private[0] ____cacheline_aligned; 76c4e05037SAdrian Hunter }; 77c4e05037SAdrian Hunter 78f07b7952SAdrian Hunter static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c) 79f07b7952SAdrian Hunter { 80f07b7952SAdrian Hunter return (void *)c->private; 81f07b7952SAdrian Hunter } 82f07b7952SAdrian Hunter 83c4e05037SAdrian Hunter static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag) 84c4e05037SAdrian Hunter { 85c4e05037SAdrian Hunter return c->slot && (c->slot->flags & flag); 86c4e05037SAdrian Hunter } 87c4e05037SAdrian Hunter 880acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR25 BIT(0) 890acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_DDR50 BIT(1) 900acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR50 BIT(2) 910acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR104 BIT(3) 920acccf41SAdrian Hunter 931c451c13SAdrian Hunter enum { 941c451c13SAdrian Hunter INTEL_DSM_FNS = 0, 951c451c13SAdrian Hunter INTEL_DSM_V18_SWITCH = 3, 961c451c13SAdrian Hunter INTEL_DSM_V33_SWITCH = 4, 970acccf41SAdrian Hunter INTEL_DSM_HS_CAPS = 8, 981c451c13SAdrian Hunter }; 991c451c13SAdrian Hunter 1001c451c13SAdrian Hunter struct intel_host { 1011c451c13SAdrian Hunter u32 dsm_fns; 1020acccf41SAdrian Hunter u32 hs_caps; 1031c451c13SAdrian Hunter }; 1041c451c13SAdrian Hunter 1051c451c13SAdrian Hunter static const guid_t intel_dsm_guid = 1061c451c13SAdrian Hunter GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F, 1071c451c13SAdrian Hunter 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61); 1081c451c13SAdrian Hunter 1091c451c13SAdrian Hunter static int __intel_dsm(struct intel_host *intel_host, struct device *dev, 1101c451c13SAdrian Hunter unsigned int fn, u32 *result) 1111c451c13SAdrian Hunter { 1121c451c13SAdrian Hunter union acpi_object *obj; 1131c451c13SAdrian Hunter int err = 0; 1141c451c13SAdrian Hunter 1151c451c13SAdrian Hunter obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL); 1161c451c13SAdrian Hunter if (!obj) 1171c451c13SAdrian Hunter return -EOPNOTSUPP; 1181c451c13SAdrian Hunter 1191c451c13SAdrian Hunter if (obj->type == ACPI_TYPE_INTEGER) { 1201c451c13SAdrian Hunter *result = obj->integer.value; 1211c451c13SAdrian Hunter } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) { 1221c451c13SAdrian Hunter size_t len = min_t(size_t, obj->buffer.length, 4); 1231c451c13SAdrian Hunter 1241c451c13SAdrian Hunter *result = 0; 1251c451c13SAdrian Hunter memcpy(result, obj->buffer.pointer, len); 1261c451c13SAdrian Hunter } else { 1271c451c13SAdrian Hunter dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n", 1281c451c13SAdrian Hunter __func__, fn, obj->type, obj->buffer.length); 1291c451c13SAdrian Hunter err = -EINVAL; 1301c451c13SAdrian Hunter } 1311c451c13SAdrian Hunter 1321c451c13SAdrian Hunter ACPI_FREE(obj); 1331c451c13SAdrian Hunter 1341c451c13SAdrian Hunter return err; 1351c451c13SAdrian Hunter } 1361c451c13SAdrian Hunter 1371c451c13SAdrian Hunter static int intel_dsm(struct intel_host *intel_host, struct device *dev, 1381c451c13SAdrian Hunter unsigned int fn, u32 *result) 1391c451c13SAdrian Hunter { 1401c451c13SAdrian Hunter if (fn > 31 || !(intel_host->dsm_fns & (1 << fn))) 1411c451c13SAdrian Hunter return -EOPNOTSUPP; 1421c451c13SAdrian Hunter 1431c451c13SAdrian Hunter return __intel_dsm(intel_host, dev, fn, result); 1441c451c13SAdrian Hunter } 1451c451c13SAdrian Hunter 1461c451c13SAdrian Hunter static void intel_dsm_init(struct intel_host *intel_host, struct device *dev, 1471c451c13SAdrian Hunter struct mmc_host *mmc) 1481c451c13SAdrian Hunter { 1491c451c13SAdrian Hunter int err; 1501c451c13SAdrian Hunter 1510acccf41SAdrian Hunter intel_host->hs_caps = ~0; 1520acccf41SAdrian Hunter 1531c451c13SAdrian Hunter err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns); 1541c451c13SAdrian Hunter if (err) { 1551c451c13SAdrian Hunter pr_debug("%s: DSM not supported, error %d\n", 1561c451c13SAdrian Hunter mmc_hostname(mmc), err); 1571c451c13SAdrian Hunter return; 1581c451c13SAdrian Hunter } 1591c451c13SAdrian Hunter 1601c451c13SAdrian Hunter pr_debug("%s: DSM function mask %#x\n", 1611c451c13SAdrian Hunter mmc_hostname(mmc), intel_host->dsm_fns); 1620acccf41SAdrian Hunter 1630acccf41SAdrian Hunter intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps); 1641c451c13SAdrian Hunter } 1651c451c13SAdrian Hunter 1661c451c13SAdrian Hunter static int intel_start_signal_voltage_switch(struct mmc_host *mmc, 1671c451c13SAdrian Hunter struct mmc_ios *ios) 1681c451c13SAdrian Hunter { 1691c451c13SAdrian Hunter struct device *dev = mmc_dev(mmc); 1701c451c13SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 1711c451c13SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 1721c451c13SAdrian Hunter unsigned int fn; 1731c451c13SAdrian Hunter u32 result = 0; 1741c451c13SAdrian Hunter int err; 1751c451c13SAdrian Hunter 1761c451c13SAdrian Hunter err = sdhci_start_signal_voltage_switch(mmc, ios); 1771c451c13SAdrian Hunter if (err) 1781c451c13SAdrian Hunter return err; 1791c451c13SAdrian Hunter 1801c451c13SAdrian Hunter switch (ios->signal_voltage) { 1811c451c13SAdrian Hunter case MMC_SIGNAL_VOLTAGE_330: 1821c451c13SAdrian Hunter fn = INTEL_DSM_V33_SWITCH; 1831c451c13SAdrian Hunter break; 1841c451c13SAdrian Hunter case MMC_SIGNAL_VOLTAGE_180: 1851c451c13SAdrian Hunter fn = INTEL_DSM_V18_SWITCH; 1861c451c13SAdrian Hunter break; 1871c451c13SAdrian Hunter default: 1881c451c13SAdrian Hunter return 0; 1891c451c13SAdrian Hunter } 1901c451c13SAdrian Hunter 1911c451c13SAdrian Hunter err = intel_dsm(intel_host, dev, fn, &result); 1921c451c13SAdrian Hunter pr_debug("%s: %s DSM fn %u error %d result %u\n", 1931c451c13SAdrian Hunter mmc_hostname(mmc), __func__, fn, err, result); 1941c451c13SAdrian Hunter 1951c451c13SAdrian Hunter return 0; 1961c451c13SAdrian Hunter } 1971c451c13SAdrian Hunter 198b04fa064SAdrian Hunter static void sdhci_acpi_int_hw_reset(struct sdhci_host *host) 199b04fa064SAdrian Hunter { 200b04fa064SAdrian Hunter u8 reg; 201b04fa064SAdrian Hunter 202b04fa064SAdrian Hunter reg = sdhci_readb(host, SDHCI_POWER_CONTROL); 203b04fa064SAdrian Hunter reg |= 0x10; 204b04fa064SAdrian Hunter sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 205b04fa064SAdrian Hunter /* For eMMC, minimum is 1us but give it 9us for good measure */ 206b04fa064SAdrian Hunter udelay(9); 207b04fa064SAdrian Hunter reg &= ~0x10; 208b04fa064SAdrian Hunter sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 209b04fa064SAdrian Hunter /* For eMMC, minimum is 200us but give it 300us for good measure */ 210b04fa064SAdrian Hunter usleep_range(300, 1000); 211b04fa064SAdrian Hunter } 212b04fa064SAdrian Hunter 213c4e05037SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_dflt = { 2141771059cSRussell King .set_clock = sdhci_set_clock, 2152317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 21603231f9bSRussell King .reset = sdhci_reset, 21796d7b78cSRussell King .set_uhs_signaling = sdhci_set_uhs_signaling, 218c4e05037SAdrian Hunter }; 219c4e05037SAdrian Hunter 220b04fa064SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_int = { 2211771059cSRussell King .set_clock = sdhci_set_clock, 2222317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 22303231f9bSRussell King .reset = sdhci_reset, 22496d7b78cSRussell King .set_uhs_signaling = sdhci_set_uhs_signaling, 225b04fa064SAdrian Hunter .hw_reset = sdhci_acpi_int_hw_reset, 226b04fa064SAdrian Hunter }; 227b04fa064SAdrian Hunter 228b04fa064SAdrian Hunter static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { 229b04fa064SAdrian Hunter .ops = &sdhci_acpi_ops_int, 230b04fa064SAdrian Hunter }; 231b04fa064SAdrian Hunter 2326e1c7d61SAdrian Hunter #ifdef CONFIG_X86 2336e1c7d61SAdrian Hunter 2346e1c7d61SAdrian Hunter static bool sdhci_acpi_byt(void) 2356e1c7d61SAdrian Hunter { 2366e1c7d61SAdrian Hunter static const struct x86_cpu_id byt[] = { 237f2c4db1bSPeter Zijlstra { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, 2386e1c7d61SAdrian Hunter {} 2396e1c7d61SAdrian Hunter }; 2406e1c7d61SAdrian Hunter 2416e1c7d61SAdrian Hunter return x86_match_cpu(byt); 2426e1c7d61SAdrian Hunter } 2436e1c7d61SAdrian Hunter 24417753d16SAdrian Hunter static bool sdhci_acpi_cht(void) 24517753d16SAdrian Hunter { 24617753d16SAdrian Hunter static const struct x86_cpu_id cht[] = { 24717753d16SAdrian Hunter { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, 24817753d16SAdrian Hunter {} 24917753d16SAdrian Hunter }; 25017753d16SAdrian Hunter 25117753d16SAdrian Hunter return x86_match_cpu(cht); 25217753d16SAdrian Hunter } 25317753d16SAdrian Hunter 2546e1c7d61SAdrian Hunter #define BYT_IOSF_SCCEP 0x63 2556e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_NETCTRL0 0x1078 2566e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8) 2576e1c7d61SAdrian Hunter 2586e1c7d61SAdrian Hunter static void sdhci_acpi_byt_setting(struct device *dev) 2596e1c7d61SAdrian Hunter { 2606e1c7d61SAdrian Hunter u32 val = 0; 2616e1c7d61SAdrian Hunter 2626e1c7d61SAdrian Hunter if (!sdhci_acpi_byt()) 2636e1c7d61SAdrian Hunter return; 2646e1c7d61SAdrian Hunter 2656e1c7d61SAdrian Hunter if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0, 2666e1c7d61SAdrian Hunter &val)) { 2676e1c7d61SAdrian Hunter dev_err(dev, "%s read error\n", __func__); 2686e1c7d61SAdrian Hunter return; 2696e1c7d61SAdrian Hunter } 2706e1c7d61SAdrian Hunter 2716e1c7d61SAdrian Hunter if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE)) 2726e1c7d61SAdrian Hunter return; 2736e1c7d61SAdrian Hunter 2746e1c7d61SAdrian Hunter val &= ~BYT_IOSF_OCP_TIMEOUT_BASE; 2756e1c7d61SAdrian Hunter 2766e1c7d61SAdrian Hunter if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0, 2776e1c7d61SAdrian Hunter val)) { 2786e1c7d61SAdrian Hunter dev_err(dev, "%s write error\n", __func__); 2796e1c7d61SAdrian Hunter return; 2806e1c7d61SAdrian Hunter } 2816e1c7d61SAdrian Hunter 2826e1c7d61SAdrian Hunter dev_dbg(dev, "%s completed\n", __func__); 2836e1c7d61SAdrian Hunter } 2846e1c7d61SAdrian Hunter 2856e1c7d61SAdrian Hunter static bool sdhci_acpi_byt_defer(struct device *dev) 2866e1c7d61SAdrian Hunter { 2876e1c7d61SAdrian Hunter if (!sdhci_acpi_byt()) 2886e1c7d61SAdrian Hunter return false; 2896e1c7d61SAdrian Hunter 2906e1c7d61SAdrian Hunter if (!iosf_mbi_available()) 2916e1c7d61SAdrian Hunter return true; 2926e1c7d61SAdrian Hunter 2936e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(dev); 2946e1c7d61SAdrian Hunter 2956e1c7d61SAdrian Hunter return false; 2966e1c7d61SAdrian Hunter } 2976e1c7d61SAdrian Hunter 29817753d16SAdrian Hunter static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device, 29917753d16SAdrian Hunter unsigned int slot, unsigned int parent_slot) 30017753d16SAdrian Hunter { 30117753d16SAdrian Hunter struct pci_dev *dev, *parent, *from = NULL; 30217753d16SAdrian Hunter 30317753d16SAdrian Hunter while (1) { 30417753d16SAdrian Hunter dev = pci_get_device(vendor, device, from); 30517753d16SAdrian Hunter pci_dev_put(from); 30617753d16SAdrian Hunter if (!dev) 30717753d16SAdrian Hunter break; 30817753d16SAdrian Hunter parent = pci_upstream_bridge(dev); 30917753d16SAdrian Hunter if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot && 31017753d16SAdrian Hunter parent && PCI_SLOT(parent->devfn) == parent_slot && 31117753d16SAdrian Hunter !pci_upstream_bridge(parent)) { 31217753d16SAdrian Hunter pci_dev_put(dev); 31317753d16SAdrian Hunter return true; 31417753d16SAdrian Hunter } 31517753d16SAdrian Hunter from = dev; 31617753d16SAdrian Hunter } 31717753d16SAdrian Hunter 31817753d16SAdrian Hunter return false; 31917753d16SAdrian Hunter } 32017753d16SAdrian Hunter 32117753d16SAdrian Hunter /* 32217753d16SAdrian Hunter * GPDwin uses PCI wifi which conflicts with SDIO's use of 32317753d16SAdrian Hunter * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is 32417753d16SAdrian Hunter * problematic, but since SDIO is only used for wifi, the presence of the PCI 32517753d16SAdrian Hunter * wifi card in the expected slot with an ACPI companion node, is used to 32617753d16SAdrian Hunter * indicate that acpi_device_fix_up_power() should be avoided. 32717753d16SAdrian Hunter */ 3284f3cde3aSAndy Shevchenko static inline bool sdhci_acpi_no_fixup_child_power(struct acpi_device *adev) 32917753d16SAdrian Hunter { 33017753d16SAdrian Hunter return sdhci_acpi_cht() && 3314f3cde3aSAndy Shevchenko acpi_dev_hid_uid_match(adev, "80860F14", "2") && 33217753d16SAdrian Hunter sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28); 33317753d16SAdrian Hunter } 33417753d16SAdrian Hunter 3356e1c7d61SAdrian Hunter #else 3366e1c7d61SAdrian Hunter 3376e1c7d61SAdrian Hunter static inline void sdhci_acpi_byt_setting(struct device *dev) 3386e1c7d61SAdrian Hunter { 3396e1c7d61SAdrian Hunter } 3406e1c7d61SAdrian Hunter 3416e1c7d61SAdrian Hunter static inline bool sdhci_acpi_byt_defer(struct device *dev) 3426e1c7d61SAdrian Hunter { 3436e1c7d61SAdrian Hunter return false; 3446e1c7d61SAdrian Hunter } 3456e1c7d61SAdrian Hunter 3464f3cde3aSAndy Shevchenko static inline bool sdhci_acpi_no_fixup_child_power(struct acpi_device *adev) 34717753d16SAdrian Hunter { 34817753d16SAdrian Hunter return false; 34917753d16SAdrian Hunter } 35017753d16SAdrian Hunter 3516e1c7d61SAdrian Hunter #endif 3526e1c7d61SAdrian Hunter 3536a645dd8SAdrian Hunter static int bxt_get_cd(struct mmc_host *mmc) 3546a645dd8SAdrian Hunter { 3556a645dd8SAdrian Hunter int gpio_cd = mmc_gpio_get_cd(mmc); 3566a645dd8SAdrian Hunter struct sdhci_host *host = mmc_priv(mmc); 3576a645dd8SAdrian Hunter unsigned long flags; 3586a645dd8SAdrian Hunter int ret = 0; 3596a645dd8SAdrian Hunter 3606a645dd8SAdrian Hunter if (!gpio_cd) 3616a645dd8SAdrian Hunter return 0; 3626a645dd8SAdrian Hunter 3636a645dd8SAdrian Hunter spin_lock_irqsave(&host->lock, flags); 3646a645dd8SAdrian Hunter 3656a645dd8SAdrian Hunter if (host->flags & SDHCI_DEVICE_DEAD) 3666a645dd8SAdrian Hunter goto out; 3676a645dd8SAdrian Hunter 3686a645dd8SAdrian Hunter ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 3696a645dd8SAdrian Hunter out: 3706a645dd8SAdrian Hunter spin_unlock_irqrestore(&host->lock, flags); 3716a645dd8SAdrian Hunter 3726a645dd8SAdrian Hunter return ret; 3736a645dd8SAdrian Hunter } 3746a645dd8SAdrian Hunter 3754f3cde3aSAndy Shevchenko static int intel_probe_slot(struct platform_device *pdev, struct acpi_device *adev) 376578b36b6SGao, Yunpeng { 377578b36b6SGao, Yunpeng struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 3781c451c13SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 379159cd328SAdrian Hunter struct sdhci_host *host = c->host; 380578b36b6SGao, Yunpeng 3814f3cde3aSAndy Shevchenko if (acpi_dev_hid_uid_match(adev, "80860F14", "1") && 3828024379eSAdrian Hunter sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 && 3838024379eSAdrian Hunter sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807) 3848024379eSAdrian Hunter host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */ 3858024379eSAdrian Hunter 3864f3cde3aSAndy Shevchenko if (acpi_dev_hid_uid_match(adev, "80865ACA", NULL)) 3876a645dd8SAdrian Hunter host->mmc_host_ops.get_cd = bxt_get_cd; 3886a645dd8SAdrian Hunter 3891c451c13SAdrian Hunter intel_dsm_init(intel_host, &pdev->dev, host->mmc); 3901c451c13SAdrian Hunter 3911c451c13SAdrian Hunter host->mmc_host_ops.start_signal_voltage_switch = 3921c451c13SAdrian Hunter intel_start_signal_voltage_switch; 3931c451c13SAdrian Hunter 394578b36b6SGao, Yunpeng return 0; 395578b36b6SGao, Yunpeng } 396578b36b6SGao, Yunpeng 3970acccf41SAdrian Hunter static int intel_setup_host(struct platform_device *pdev) 3980acccf41SAdrian Hunter { 3990acccf41SAdrian Hunter struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 4000acccf41SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 4010acccf41SAdrian Hunter 4020acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25)) 4030acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25; 4040acccf41SAdrian Hunter 4050acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50)) 4060acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50; 4070acccf41SAdrian Hunter 4080acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50)) 4090acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50; 4100acccf41SAdrian Hunter 4110acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104)) 4120acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104; 4130acccf41SAdrian Hunter 4140acccf41SAdrian Hunter return 0; 4150acccf41SAdrian Hunter } 4160acccf41SAdrian Hunter 41707a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { 418b04fa064SAdrian Hunter .chip = &sdhci_acpi_chip_int, 419f25c3372SMaurice Petallo .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | 4209d65cb88SAdrian Hunter MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR | 421c80f275fSAdrian Hunter MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY, 42207a58883SAdrian Hunter .flags = SDHCI_ACPI_RUNTIME_PM, 423197ce1a5SAdrian Hunter .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | 424197ce1a5SAdrian Hunter SDHCI_QUIRK_NO_LED, 425e839b134SAdrian Hunter .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 426e839b134SAdrian Hunter SDHCI_QUIRK2_STOP_WITH_TC | 427e839b134SAdrian Hunter SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400, 428159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4290acccf41SAdrian Hunter .setup_host = intel_setup_host, 4301c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 43107a58883SAdrian Hunter }; 43207a58883SAdrian Hunter 433e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { 434e1f5633aSAdrian Hunter .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | 435197ce1a5SAdrian Hunter SDHCI_QUIRK_NO_LED | 436e1f5633aSAdrian Hunter SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 437e5571397SAdrian Hunter .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON, 4389d65cb88SAdrian Hunter .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD | 439265984b3SAdrian Hunter MMC_CAP_WAIT_WHILE_BUSY, 440e5571397SAdrian Hunter .flags = SDHCI_ACPI_RUNTIME_PM, 441e5571397SAdrian Hunter .pm_caps = MMC_PM_KEEP_POWER, 442159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4430acccf41SAdrian Hunter .setup_host = intel_setup_host, 4441c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 445e5571397SAdrian Hunter }; 446e5571397SAdrian Hunter 44707a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { 4484fd4409cSAdrian Hunter .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | 4494fd4409cSAdrian Hunter SDHCI_ACPI_RUNTIME_PM, 450197ce1a5SAdrian Hunter .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC | 451197ce1a5SAdrian Hunter SDHCI_QUIRK_NO_LED, 452934e31b9SAdrian Hunter .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | 453934e31b9SAdrian Hunter SDHCI_QUIRK2_STOP_WITH_TC, 454d3e97407SAzhar Shaikh .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM, 455159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4560acccf41SAdrian Hunter .setup_host = intel_setup_host, 4571c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 45807a58883SAdrian Hunter }; 45907a58883SAdrian Hunter 46096ccb858SWang Dongsheng #define VENDOR_SPECIFIC_PWRCTL_CLEAR_REG 0x1a8 46196ccb858SWang Dongsheng #define VENDOR_SPECIFIC_PWRCTL_CTL_REG 0x1ac 46296ccb858SWang Dongsheng static irqreturn_t sdhci_acpi_qcom_handler(int irq, void *ptr) 46396ccb858SWang Dongsheng { 46496ccb858SWang Dongsheng struct sdhci_host *host = ptr; 46596ccb858SWang Dongsheng 46696ccb858SWang Dongsheng sdhci_writel(host, 0x3, VENDOR_SPECIFIC_PWRCTL_CLEAR_REG); 46796ccb858SWang Dongsheng sdhci_writel(host, 0x1, VENDOR_SPECIFIC_PWRCTL_CTL_REG); 46896ccb858SWang Dongsheng 46996ccb858SWang Dongsheng return IRQ_HANDLED; 47096ccb858SWang Dongsheng } 47196ccb858SWang Dongsheng 4724f3cde3aSAndy Shevchenko static int qcom_probe_slot(struct platform_device *pdev, struct acpi_device *adev) 47396ccb858SWang Dongsheng { 47496ccb858SWang Dongsheng struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 47596ccb858SWang Dongsheng struct sdhci_host *host = c->host; 47696ccb858SWang Dongsheng int *irq = sdhci_acpi_priv(c); 47796ccb858SWang Dongsheng 47896ccb858SWang Dongsheng *irq = -EINVAL; 47996ccb858SWang Dongsheng 4804f3cde3aSAndy Shevchenko if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL)) 48196ccb858SWang Dongsheng return 0; 48296ccb858SWang Dongsheng 48396ccb858SWang Dongsheng *irq = platform_get_irq(pdev, 1); 48496ccb858SWang Dongsheng if (*irq < 0) 48596ccb858SWang Dongsheng return 0; 48696ccb858SWang Dongsheng 48796ccb858SWang Dongsheng return request_threaded_irq(*irq, NULL, sdhci_acpi_qcom_handler, 48896ccb858SWang Dongsheng IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 48996ccb858SWang Dongsheng "sdhci_qcom", host); 49096ccb858SWang Dongsheng } 49196ccb858SWang Dongsheng 49296ccb858SWang Dongsheng static int qcom_free_slot(struct platform_device *pdev) 49396ccb858SWang Dongsheng { 49496ccb858SWang Dongsheng struct device *dev = &pdev->dev; 49596ccb858SWang Dongsheng struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 49696ccb858SWang Dongsheng struct sdhci_host *host = c->host; 49796ccb858SWang Dongsheng struct acpi_device *adev; 49896ccb858SWang Dongsheng int *irq = sdhci_acpi_priv(c); 49996ccb858SWang Dongsheng 50096ccb858SWang Dongsheng adev = ACPI_COMPANION(dev); 50196ccb858SWang Dongsheng if (!adev) 50296ccb858SWang Dongsheng return -ENODEV; 50396ccb858SWang Dongsheng 5044f3cde3aSAndy Shevchenko if (!acpi_dev_hid_uid_match(adev, "QCOM8051", NULL)) 50596ccb858SWang Dongsheng return 0; 50696ccb858SWang Dongsheng 50796ccb858SWang Dongsheng if (*irq < 0) 50896ccb858SWang Dongsheng return 0; 50996ccb858SWang Dongsheng 51096ccb858SWang Dongsheng free_irq(*irq, host); 51196ccb858SWang Dongsheng return 0; 51296ccb858SWang Dongsheng } 51396ccb858SWang Dongsheng 51470cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = { 51570cce2afSPhilip Elcan .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 51670cce2afSPhilip Elcan .quirks2 = SDHCI_QUIRK2_NO_1_8_V, 51770cce2afSPhilip Elcan .caps = MMC_CAP_NONREMOVABLE, 51896ccb858SWang Dongsheng .priv_size = sizeof(int), 51996ccb858SWang Dongsheng .probe_slot = qcom_probe_slot, 52096ccb858SWang Dongsheng .free_slot = qcom_free_slot, 52170cce2afSPhilip Elcan }; 52270cce2afSPhilip Elcan 52370cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = { 52470cce2afSPhilip Elcan .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 52570cce2afSPhilip Elcan .caps = MMC_CAP_NONREMOVABLE, 52670cce2afSPhilip Elcan }; 52770cce2afSPhilip Elcan 52834597a3fSShah Nehal-Bakulchandra /* AMD sdhci reset dll register. */ 52934597a3fSShah Nehal-Bakulchandra #define SDHCI_AMD_RESET_DLL_REGISTER 0x908 53034597a3fSShah Nehal-Bakulchandra 53134597a3fSShah Nehal-Bakulchandra static int amd_select_drive_strength(struct mmc_card *card, 53234597a3fSShah Nehal-Bakulchandra unsigned int max_dtr, int host_drv, 53334597a3fSShah Nehal-Bakulchandra int card_drv, int *drv_type) 53434597a3fSShah Nehal-Bakulchandra { 53534597a3fSShah Nehal-Bakulchandra return MMC_SET_DRIVER_TYPE_A; 53634597a3fSShah Nehal-Bakulchandra } 53734597a3fSShah Nehal-Bakulchandra 53834597a3fSShah Nehal-Bakulchandra static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host) 53934597a3fSShah Nehal-Bakulchandra { 54034597a3fSShah Nehal-Bakulchandra /* AMD Platform requires dll setting */ 54134597a3fSShah Nehal-Bakulchandra sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER); 54234597a3fSShah Nehal-Bakulchandra usleep_range(10, 20); 54334597a3fSShah Nehal-Bakulchandra sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER); 54434597a3fSShah Nehal-Bakulchandra } 54534597a3fSShah Nehal-Bakulchandra 54634597a3fSShah Nehal-Bakulchandra /* 54734597a3fSShah Nehal-Bakulchandra * For AMD Platform it is required to disable the tuning 54834597a3fSShah Nehal-Bakulchandra * bit first controller to bring to HS Mode from HS200 54934597a3fSShah Nehal-Bakulchandra * mode, later enable to tune to HS400 mode. 55034597a3fSShah Nehal-Bakulchandra */ 55134597a3fSShah Nehal-Bakulchandra static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 55234597a3fSShah Nehal-Bakulchandra { 55334597a3fSShah Nehal-Bakulchandra struct sdhci_host *host = mmc_priv(mmc); 55434597a3fSShah Nehal-Bakulchandra unsigned int old_timing = host->timing; 55534597a3fSShah Nehal-Bakulchandra 55634597a3fSShah Nehal-Bakulchandra sdhci_set_ios(mmc, ios); 55734597a3fSShah Nehal-Bakulchandra if (old_timing == MMC_TIMING_MMC_HS200 && 55834597a3fSShah Nehal-Bakulchandra ios->timing == MMC_TIMING_MMC_HS) 55934597a3fSShah Nehal-Bakulchandra sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2); 56034597a3fSShah Nehal-Bakulchandra if (old_timing != MMC_TIMING_MMC_HS400 && 56134597a3fSShah Nehal-Bakulchandra ios->timing == MMC_TIMING_MMC_HS400) { 56234597a3fSShah Nehal-Bakulchandra sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2); 56334597a3fSShah Nehal-Bakulchandra sdhci_acpi_amd_hs400_dll(host); 56434597a3fSShah Nehal-Bakulchandra } 56534597a3fSShah Nehal-Bakulchandra } 56634597a3fSShah Nehal-Bakulchandra 56734597a3fSShah Nehal-Bakulchandra static const struct sdhci_ops sdhci_acpi_ops_amd = { 56834597a3fSShah Nehal-Bakulchandra .set_clock = sdhci_set_clock, 56934597a3fSShah Nehal-Bakulchandra .set_bus_width = sdhci_set_bus_width, 57034597a3fSShah Nehal-Bakulchandra .reset = sdhci_reset, 57134597a3fSShah Nehal-Bakulchandra .set_uhs_signaling = sdhci_set_uhs_signaling, 57234597a3fSShah Nehal-Bakulchandra }; 57334597a3fSShah Nehal-Bakulchandra 57434597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = { 57534597a3fSShah Nehal-Bakulchandra .ops = &sdhci_acpi_ops_amd, 57634597a3fSShah Nehal-Bakulchandra }; 57734597a3fSShah Nehal-Bakulchandra 57834597a3fSShah Nehal-Bakulchandra static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev, 5794f3cde3aSAndy Shevchenko struct acpi_device *adev) 58034597a3fSShah Nehal-Bakulchandra { 58134597a3fSShah Nehal-Bakulchandra struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 58234597a3fSShah Nehal-Bakulchandra struct sdhci_host *host = c->host; 58334597a3fSShah Nehal-Bakulchandra 58434597a3fSShah Nehal-Bakulchandra sdhci_read_caps(host); 58534597a3fSShah Nehal-Bakulchandra if (host->caps1 & SDHCI_SUPPORT_DDR50) 58634597a3fSShah Nehal-Bakulchandra host->mmc->caps = MMC_CAP_1_8V_DDR; 58734597a3fSShah Nehal-Bakulchandra 58834597a3fSShah Nehal-Bakulchandra if ((host->caps1 & SDHCI_SUPPORT_SDR104) && 58934597a3fSShah Nehal-Bakulchandra (host->mmc->caps & MMC_CAP_1_8V_DDR)) 59034597a3fSShah Nehal-Bakulchandra host->mmc->caps2 = MMC_CAP2_HS400_1_8V; 59134597a3fSShah Nehal-Bakulchandra 59234597a3fSShah Nehal-Bakulchandra host->mmc_host_ops.select_drive_strength = amd_select_drive_strength; 59334597a3fSShah Nehal-Bakulchandra host->mmc_host_ops.set_ios = amd_set_ios; 59434597a3fSShah Nehal-Bakulchandra return 0; 59534597a3fSShah Nehal-Bakulchandra } 59634597a3fSShah Nehal-Bakulchandra 59734597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = { 59834597a3fSShah Nehal-Bakulchandra .chip = &sdhci_acpi_chip_amd, 59934597a3fSShah Nehal-Bakulchandra .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE, 60034597a3fSShah Nehal-Bakulchandra .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE | 60134597a3fSShah Nehal-Bakulchandra SDHCI_QUIRK_32BIT_ADMA_SIZE, 60234597a3fSShah Nehal-Bakulchandra .probe_slot = sdhci_acpi_emmc_amd_probe_slot, 60334597a3fSShah Nehal-Bakulchandra }; 60434597a3fSShah Nehal-Bakulchandra 60507a58883SAdrian Hunter struct sdhci_acpi_uid_slot { 60607a58883SAdrian Hunter const char *hid; 60707a58883SAdrian Hunter const char *uid; 60807a58883SAdrian Hunter const struct sdhci_acpi_slot *slot; 60907a58883SAdrian Hunter }; 61007a58883SAdrian Hunter 61107a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = { 612e839b134SAdrian Hunter { "80865ACA", NULL, &sdhci_acpi_slot_int_sd }, 613e839b134SAdrian Hunter { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc }, 614e839b134SAdrian Hunter { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio }, 61507a58883SAdrian Hunter { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc }, 616db52d4f8SDaniel Drake { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio }, 61707a58883SAdrian Hunter { "80860F14" , "3" , &sdhci_acpi_slot_int_sd }, 618aad95dc4SAdrian Hunter { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, 61907a58883SAdrian Hunter { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, 6207147eaf3SAdrian Hunter { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd }, 62107a58883SAdrian Hunter { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, 62207c001c1SMika Westerberg { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, 623d0ed8e6bSAdrian Hunter { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio }, 6240cd2f044SMichele Curti { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd }, 62507a58883SAdrian Hunter { "PNP0D40" }, 62670cce2afSPhilip Elcan { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v }, 62770cce2afSPhilip Elcan { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd }, 62834597a3fSShah Nehal-Bakulchandra { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc }, 62907a58883SAdrian Hunter { }, 63007a58883SAdrian Hunter }; 63107a58883SAdrian Hunter 632c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = { 633e839b134SAdrian Hunter { "80865ACA" }, 634e839b134SAdrian Hunter { "80865ACC" }, 635e839b134SAdrian Hunter { "80865AD0" }, 63607a58883SAdrian Hunter { "80860F14" }, 637aad95dc4SAdrian Hunter { "80860F16" }, 63807a58883SAdrian Hunter { "INT33BB" }, 63907a58883SAdrian Hunter { "INT33C6" }, 64007c001c1SMika Westerberg { "INT3436" }, 641d0ed8e6bSAdrian Hunter { "INT344D" }, 642c4e05037SAdrian Hunter { "PNP0D40" }, 64370cce2afSPhilip Elcan { "QCOM8051" }, 64470cce2afSPhilip Elcan { "QCOM8052" }, 64534597a3fSShah Nehal-Bakulchandra { "AMDI0040" }, 646c4e05037SAdrian Hunter { }, 647c4e05037SAdrian Hunter }; 648c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); 649c4e05037SAdrian Hunter 6504f3cde3aSAndy Shevchenko static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(struct acpi_device *adev) 651c4e05037SAdrian Hunter { 65207a58883SAdrian Hunter const struct sdhci_acpi_uid_slot *u; 653c4e05037SAdrian Hunter 65407a58883SAdrian Hunter for (u = sdhci_acpi_uids; u->hid; u++) { 6554f3cde3aSAndy Shevchenko if (acpi_dev_hid_uid_match(adev, u->hid, u->uid)) 65607a58883SAdrian Hunter return u->slot; 65707a58883SAdrian Hunter } 658c4e05037SAdrian Hunter return NULL; 659c4e05037SAdrian Hunter } 660c4e05037SAdrian Hunter 6614e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev) 662c4e05037SAdrian Hunter { 663c4e05037SAdrian Hunter struct device *dev = &pdev->dev; 664f07b7952SAdrian Hunter const struct sdhci_acpi_slot *slot; 665e5bbf307SAdrian Hunter struct acpi_device *device, *child; 666c4e05037SAdrian Hunter struct sdhci_acpi_host *c; 667c4e05037SAdrian Hunter struct sdhci_host *host; 668c4e05037SAdrian Hunter struct resource *iomem; 669c4e05037SAdrian Hunter resource_size_t len; 670f07b7952SAdrian Hunter size_t priv_size; 67187875655SMika Westerberg int err; 672c4e05037SAdrian Hunter 673cd25c7beSAndy Shevchenko device = ACPI_COMPANION(dev); 674cd25c7beSAndy Shevchenko if (!device) 675c4e05037SAdrian Hunter return -ENODEV; 676c4e05037SAdrian Hunter 6774f3cde3aSAndy Shevchenko slot = sdhci_acpi_get_slot(device); 678f07b7952SAdrian Hunter 679e5bbf307SAdrian Hunter /* Power on the SDHCI controller and its children */ 680e5bbf307SAdrian Hunter acpi_device_fix_up_power(device); 6814f3cde3aSAndy Shevchenko if (!sdhci_acpi_no_fixup_child_power(device)) { 682e5bbf307SAdrian Hunter list_for_each_entry(child, &device->children, node) 683e1d070c3SHans de Goede if (child->status.present && child->status.enabled) 684e5bbf307SAdrian Hunter acpi_device_fix_up_power(child); 68517753d16SAdrian Hunter } 686e5bbf307SAdrian Hunter 6876e1c7d61SAdrian Hunter if (sdhci_acpi_byt_defer(dev)) 6886e1c7d61SAdrian Hunter return -EPROBE_DEFER; 6896e1c7d61SAdrian Hunter 690c4e05037SAdrian Hunter iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 691c4e05037SAdrian Hunter if (!iomem) 692c4e05037SAdrian Hunter return -ENOMEM; 693c4e05037SAdrian Hunter 694c4e05037SAdrian Hunter len = resource_size(iomem); 695c4e05037SAdrian Hunter if (len < 0x100) 696c4e05037SAdrian Hunter dev_err(dev, "Invalid iomem size!\n"); 697c4e05037SAdrian Hunter 698c4e05037SAdrian Hunter if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev))) 699c4e05037SAdrian Hunter return -ENOMEM; 700c4e05037SAdrian Hunter 701f07b7952SAdrian Hunter priv_size = slot ? slot->priv_size : 0; 702f07b7952SAdrian Hunter host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size); 703c4e05037SAdrian Hunter if (IS_ERR(host)) 704c4e05037SAdrian Hunter return PTR_ERR(host); 705c4e05037SAdrian Hunter 706c4e05037SAdrian Hunter c = sdhci_priv(host); 707c4e05037SAdrian Hunter c->host = host; 708f07b7952SAdrian Hunter c->slot = slot; 709c4e05037SAdrian Hunter c->pdev = pdev; 710c4e05037SAdrian Hunter c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM); 711c4e05037SAdrian Hunter 712c4e05037SAdrian Hunter platform_set_drvdata(pdev, c); 713c4e05037SAdrian Hunter 714c4e05037SAdrian Hunter host->hw_name = "ACPI"; 715c4e05037SAdrian Hunter host->ops = &sdhci_acpi_ops_dflt; 716c4e05037SAdrian Hunter host->irq = platform_get_irq(pdev, 0); 717d58ac803SAdrian Hunter if (host->irq < 0) { 7181b7ba57eSArvind Yadav err = -EINVAL; 7191b7ba57eSArvind Yadav goto err_free; 7201b7ba57eSArvind Yadav } 721c4e05037SAdrian Hunter 722c4e05037SAdrian Hunter host->ioaddr = devm_ioremap_nocache(dev, iomem->start, 723c4e05037SAdrian Hunter resource_size(iomem)); 724c4e05037SAdrian Hunter if (host->ioaddr == NULL) { 725c4e05037SAdrian Hunter err = -ENOMEM; 726c4e05037SAdrian Hunter goto err_free; 727c4e05037SAdrian Hunter } 728c4e05037SAdrian Hunter 729c4e05037SAdrian Hunter if (c->slot) { 730578b36b6SGao, Yunpeng if (c->slot->probe_slot) { 7314f3cde3aSAndy Shevchenko err = c->slot->probe_slot(pdev, device); 732578b36b6SGao, Yunpeng if (err) 733578b36b6SGao, Yunpeng goto err_free; 734578b36b6SGao, Yunpeng } 735c4e05037SAdrian Hunter if (c->slot->chip) { 736c4e05037SAdrian Hunter host->ops = c->slot->chip->ops; 737c4e05037SAdrian Hunter host->quirks |= c->slot->chip->quirks; 738c4e05037SAdrian Hunter host->quirks2 |= c->slot->chip->quirks2; 739c4e05037SAdrian Hunter host->mmc->caps |= c->slot->chip->caps; 740c4e05037SAdrian Hunter host->mmc->caps2 |= c->slot->chip->caps2; 741c4e05037SAdrian Hunter host->mmc->pm_caps |= c->slot->chip->pm_caps; 742c4e05037SAdrian Hunter } 743c4e05037SAdrian Hunter host->quirks |= c->slot->quirks; 744c4e05037SAdrian Hunter host->quirks2 |= c->slot->quirks2; 745c4e05037SAdrian Hunter host->mmc->caps |= c->slot->caps; 746c4e05037SAdrian Hunter host->mmc->caps2 |= c->slot->caps2; 747c4e05037SAdrian Hunter host->mmc->pm_caps |= c->slot->pm_caps; 748c4e05037SAdrian Hunter } 749c4e05037SAdrian Hunter 7500d3e3350SAdrian Hunter host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; 7510d3e3350SAdrian Hunter 7524fd4409cSAdrian Hunter if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { 7534fd4409cSAdrian Hunter bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); 7544fd4409cSAdrian Hunter 755e28d6f04SZhang Rui err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL); 756e28d6f04SZhang Rui if (err) { 757e28d6f04SZhang Rui if (err == -EPROBE_DEFER) 758e28d6f04SZhang Rui goto err_free; 7594fd4409cSAdrian Hunter dev_warn(dev, "failed to setup card detect gpio\n"); 7604fd4409cSAdrian Hunter c->use_runtime_pm = false; 7614fd4409cSAdrian Hunter } 7624fd4409cSAdrian Hunter } 7634fd4409cSAdrian Hunter 7640cc1a0f4SAdrian Hunter err = sdhci_setup_host(host); 765c4e05037SAdrian Hunter if (err) 766c4e05037SAdrian Hunter goto err_free; 767c4e05037SAdrian Hunter 7680cc1a0f4SAdrian Hunter if (c->slot && c->slot->setup_host) { 7690cc1a0f4SAdrian Hunter err = c->slot->setup_host(pdev); 7700cc1a0f4SAdrian Hunter if (err) 7710cc1a0f4SAdrian Hunter goto err_cleanup; 7720cc1a0f4SAdrian Hunter } 7730cc1a0f4SAdrian Hunter 7740cc1a0f4SAdrian Hunter err = __sdhci_add_host(host); 7750cc1a0f4SAdrian Hunter if (err) 7760cc1a0f4SAdrian Hunter goto err_cleanup; 7770cc1a0f4SAdrian Hunter 778c4e05037SAdrian Hunter if (c->use_runtime_pm) { 7791d1ff458SAdrian Hunter pm_runtime_set_active(dev); 780c4e05037SAdrian Hunter pm_suspend_ignore_children(dev, 1); 781c4e05037SAdrian Hunter pm_runtime_set_autosuspend_delay(dev, 50); 782c4e05037SAdrian Hunter pm_runtime_use_autosuspend(dev); 783c4e05037SAdrian Hunter pm_runtime_enable(dev); 784c4e05037SAdrian Hunter } 785c4e05037SAdrian Hunter 7864e6a2ef9SFu, Zhonghui device_enable_async_suspend(dev); 7874e6a2ef9SFu, Zhonghui 788c4e05037SAdrian Hunter return 0; 789c4e05037SAdrian Hunter 7900cc1a0f4SAdrian Hunter err_cleanup: 7910cc1a0f4SAdrian Hunter sdhci_cleanup_host(c->host); 792c4e05037SAdrian Hunter err_free: 793c7eabbeeSWang Dongsheng if (c->slot && c->slot->free_slot) 794c7eabbeeSWang Dongsheng c->slot->free_slot(pdev); 795c7eabbeeSWang Dongsheng 796c4e05037SAdrian Hunter sdhci_free_host(c->host); 797c4e05037SAdrian Hunter return err; 798c4e05037SAdrian Hunter } 799c4e05037SAdrian Hunter 8004e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev) 801c4e05037SAdrian Hunter { 802c4e05037SAdrian Hunter struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 803c4e05037SAdrian Hunter struct device *dev = &pdev->dev; 804c4e05037SAdrian Hunter int dead; 805c4e05037SAdrian Hunter 806c4e05037SAdrian Hunter if (c->use_runtime_pm) { 807c4e05037SAdrian Hunter pm_runtime_get_sync(dev); 808c4e05037SAdrian Hunter pm_runtime_disable(dev); 809c4e05037SAdrian Hunter pm_runtime_put_noidle(dev); 810c4e05037SAdrian Hunter } 811c4e05037SAdrian Hunter 812578b36b6SGao, Yunpeng if (c->slot && c->slot->remove_slot) 813578b36b6SGao, Yunpeng c->slot->remove_slot(pdev); 814578b36b6SGao, Yunpeng 815c4e05037SAdrian Hunter dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); 816c4e05037SAdrian Hunter sdhci_remove_host(c->host, dead); 817c7eabbeeSWang Dongsheng 818c7eabbeeSWang Dongsheng if (c->slot && c->slot->free_slot) 819c7eabbeeSWang Dongsheng c->slot->free_slot(pdev); 820c7eabbeeSWang Dongsheng 821c4e05037SAdrian Hunter sdhci_free_host(c->host); 822c4e05037SAdrian Hunter 823c4e05037SAdrian Hunter return 0; 824c4e05037SAdrian Hunter } 825c4e05037SAdrian Hunter 826c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP 827c4e05037SAdrian Hunter 828c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev) 829c4e05037SAdrian Hunter { 830c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 831d38dcad4SAdrian Hunter struct sdhci_host *host = c->host; 832c4e05037SAdrian Hunter 833d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 834d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 835d38dcad4SAdrian Hunter 836d38dcad4SAdrian Hunter return sdhci_suspend_host(host); 837c4e05037SAdrian Hunter } 838c4e05037SAdrian Hunter 839c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev) 840c4e05037SAdrian Hunter { 841c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 842c4e05037SAdrian Hunter 8436e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(&c->pdev->dev); 8446e1c7d61SAdrian Hunter 845c4e05037SAdrian Hunter return sdhci_resume_host(c->host); 846c4e05037SAdrian Hunter } 847c4e05037SAdrian Hunter 848c4e05037SAdrian Hunter #endif 849c4e05037SAdrian Hunter 850162d6f98SRafael J. Wysocki #ifdef CONFIG_PM 851c4e05037SAdrian Hunter 852c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev) 853c4e05037SAdrian Hunter { 854c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 855d38dcad4SAdrian Hunter struct sdhci_host *host = c->host; 856c4e05037SAdrian Hunter 857d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 858d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 859d38dcad4SAdrian Hunter 860d38dcad4SAdrian Hunter return sdhci_runtime_suspend_host(host); 861c4e05037SAdrian Hunter } 862c4e05037SAdrian Hunter 863c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev) 864c4e05037SAdrian Hunter { 865c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 866c4e05037SAdrian Hunter 8676e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(&c->pdev->dev); 8686e1c7d61SAdrian Hunter 869c6303c5dSBaolin Wang return sdhci_runtime_resume_host(c->host, 0); 870c4e05037SAdrian Hunter } 871c4e05037SAdrian Hunter 872c4e05037SAdrian Hunter #endif 873c4e05037SAdrian Hunter 874c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = { 875dafed447SUlf Hansson SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume) 8761d75f74bSPeter Griffin SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend, 8779b449e99SUlf Hansson sdhci_acpi_runtime_resume, NULL) 878c4e05037SAdrian Hunter }; 879c4e05037SAdrian Hunter 880c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = { 881c4e05037SAdrian Hunter .driver = { 882c4e05037SAdrian Hunter .name = "sdhci-acpi", 883c4e05037SAdrian Hunter .acpi_match_table = sdhci_acpi_ids, 884c4e05037SAdrian Hunter .pm = &sdhci_acpi_pm_ops, 885c4e05037SAdrian Hunter }, 886c4e05037SAdrian Hunter .probe = sdhci_acpi_probe, 8874e608e4eSGreg Kroah-Hartman .remove = sdhci_acpi_remove, 888c4e05037SAdrian Hunter }; 889c4e05037SAdrian Hunter 890c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver); 891c4e05037SAdrian Hunter 892c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); 893c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter"); 894c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2"); 895