1c4e05037SAdrian Hunter /* 2c4e05037SAdrian Hunter * Secure Digital Host Controller Interface ACPI driver. 3c4e05037SAdrian Hunter * 4c4e05037SAdrian Hunter * Copyright (c) 2012, Intel Corporation. 5c4e05037SAdrian Hunter * 6c4e05037SAdrian Hunter * This program is free software; you can redistribute it and/or modify it 7c4e05037SAdrian Hunter * under the terms and conditions of the GNU General Public License, 8c4e05037SAdrian Hunter * version 2, as published by the Free Software Foundation. 9c4e05037SAdrian Hunter * 10c4e05037SAdrian Hunter * This program is distributed in the hope it will be useful, but WITHOUT 11c4e05037SAdrian Hunter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12c4e05037SAdrian Hunter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13c4e05037SAdrian Hunter * more details. 14c4e05037SAdrian Hunter * 15c4e05037SAdrian Hunter * You should have received a copy of the GNU General Public License along with 16c4e05037SAdrian Hunter * this program; if not, write to the Free Software Foundation, Inc., 17c4e05037SAdrian Hunter * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 18c4e05037SAdrian Hunter * 19c4e05037SAdrian Hunter */ 20c4e05037SAdrian Hunter 21c4e05037SAdrian Hunter #include <linux/init.h> 22c4e05037SAdrian Hunter #include <linux/export.h> 23c4e05037SAdrian Hunter #include <linux/module.h> 24c4e05037SAdrian Hunter #include <linux/device.h> 25c4e05037SAdrian Hunter #include <linux/platform_device.h> 26c4e05037SAdrian Hunter #include <linux/ioport.h> 27c4e05037SAdrian Hunter #include <linux/io.h> 28c4e05037SAdrian Hunter #include <linux/dma-mapping.h> 29c4e05037SAdrian Hunter #include <linux/compiler.h> 30c4e05037SAdrian Hunter #include <linux/stddef.h> 31c4e05037SAdrian Hunter #include <linux/bitops.h> 32c4e05037SAdrian Hunter #include <linux/types.h> 33c4e05037SAdrian Hunter #include <linux/err.h> 34c4e05037SAdrian Hunter #include <linux/interrupt.h> 35c4e05037SAdrian Hunter #include <linux/acpi.h> 36c4e05037SAdrian Hunter #include <linux/pm.h> 37c4e05037SAdrian Hunter #include <linux/pm_runtime.h> 38b04fa064SAdrian Hunter #include <linux/delay.h> 39c4e05037SAdrian Hunter 40c4e05037SAdrian Hunter #include <linux/mmc/host.h> 41c4e05037SAdrian Hunter #include <linux/mmc/pm.h> 424fd4409cSAdrian Hunter #include <linux/mmc/slot-gpio.h> 43c4e05037SAdrian Hunter 446e1c7d61SAdrian Hunter #ifdef CONFIG_X86 456e1c7d61SAdrian Hunter #include <asm/cpu_device_id.h> 468ba4cb53SDave Hansen #include <asm/intel-family.h> 476e1c7d61SAdrian Hunter #include <asm/iosf_mbi.h> 4817753d16SAdrian Hunter #include <linux/pci.h> 496e1c7d61SAdrian Hunter #endif 506e1c7d61SAdrian Hunter 51c4e05037SAdrian Hunter #include "sdhci.h" 52c4e05037SAdrian Hunter 53c4e05037SAdrian Hunter enum { 54c4e05037SAdrian Hunter SDHCI_ACPI_SD_CD = BIT(0), 55c4e05037SAdrian Hunter SDHCI_ACPI_RUNTIME_PM = BIT(1), 564fd4409cSAdrian Hunter SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2), 57c4e05037SAdrian Hunter }; 58c4e05037SAdrian Hunter 59c4e05037SAdrian Hunter struct sdhci_acpi_chip { 60c4e05037SAdrian Hunter const struct sdhci_ops *ops; 61c4e05037SAdrian Hunter unsigned int quirks; 62c4e05037SAdrian Hunter unsigned int quirks2; 63c4e05037SAdrian Hunter unsigned long caps; 64c4e05037SAdrian Hunter unsigned int caps2; 65c4e05037SAdrian Hunter mmc_pm_flag_t pm_caps; 66c4e05037SAdrian Hunter }; 67c4e05037SAdrian Hunter 68c4e05037SAdrian Hunter struct sdhci_acpi_slot { 69c4e05037SAdrian Hunter const struct sdhci_acpi_chip *chip; 70c4e05037SAdrian Hunter unsigned int quirks; 71c4e05037SAdrian Hunter unsigned int quirks2; 72c4e05037SAdrian Hunter unsigned long caps; 73c4e05037SAdrian Hunter unsigned int caps2; 74c4e05037SAdrian Hunter mmc_pm_flag_t pm_caps; 75c4e05037SAdrian Hunter unsigned int flags; 76f07b7952SAdrian Hunter size_t priv_size; 777dafca83SAdrian Hunter int (*probe_slot)(struct platform_device *, const char *, const char *); 78578b36b6SGao, Yunpeng int (*remove_slot)(struct platform_device *); 790cc1a0f4SAdrian Hunter int (*setup_host)(struct platform_device *pdev); 80c4e05037SAdrian Hunter }; 81c4e05037SAdrian Hunter 82c4e05037SAdrian Hunter struct sdhci_acpi_host { 83c4e05037SAdrian Hunter struct sdhci_host *host; 84c4e05037SAdrian Hunter const struct sdhci_acpi_slot *slot; 85c4e05037SAdrian Hunter struct platform_device *pdev; 86c4e05037SAdrian Hunter bool use_runtime_pm; 87f07b7952SAdrian Hunter unsigned long private[0] ____cacheline_aligned; 88c4e05037SAdrian Hunter }; 89c4e05037SAdrian Hunter 90f07b7952SAdrian Hunter static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c) 91f07b7952SAdrian Hunter { 92f07b7952SAdrian Hunter return (void *)c->private; 93f07b7952SAdrian Hunter } 94f07b7952SAdrian Hunter 95c4e05037SAdrian Hunter static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag) 96c4e05037SAdrian Hunter { 97c4e05037SAdrian Hunter return c->slot && (c->slot->flags & flag); 98c4e05037SAdrian Hunter } 99c4e05037SAdrian Hunter 1000acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR25 BIT(0) 1010acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_DDR50 BIT(1) 1020acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR50 BIT(2) 1030acccf41SAdrian Hunter #define INTEL_DSM_HS_CAPS_SDR104 BIT(3) 1040acccf41SAdrian Hunter 1051c451c13SAdrian Hunter enum { 1061c451c13SAdrian Hunter INTEL_DSM_FNS = 0, 1071c451c13SAdrian Hunter INTEL_DSM_V18_SWITCH = 3, 1081c451c13SAdrian Hunter INTEL_DSM_V33_SWITCH = 4, 1090acccf41SAdrian Hunter INTEL_DSM_HS_CAPS = 8, 1101c451c13SAdrian Hunter }; 1111c451c13SAdrian Hunter 1121c451c13SAdrian Hunter struct intel_host { 1131c451c13SAdrian Hunter u32 dsm_fns; 1140acccf41SAdrian Hunter u32 hs_caps; 1151c451c13SAdrian Hunter }; 1161c451c13SAdrian Hunter 1171c451c13SAdrian Hunter static const guid_t intel_dsm_guid = 1181c451c13SAdrian Hunter GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F, 1191c451c13SAdrian Hunter 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61); 1201c451c13SAdrian Hunter 1211c451c13SAdrian Hunter static int __intel_dsm(struct intel_host *intel_host, struct device *dev, 1221c451c13SAdrian Hunter unsigned int fn, u32 *result) 1231c451c13SAdrian Hunter { 1241c451c13SAdrian Hunter union acpi_object *obj; 1251c451c13SAdrian Hunter int err = 0; 1261c451c13SAdrian Hunter 1271c451c13SAdrian Hunter obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL); 1281c451c13SAdrian Hunter if (!obj) 1291c451c13SAdrian Hunter return -EOPNOTSUPP; 1301c451c13SAdrian Hunter 1311c451c13SAdrian Hunter if (obj->type == ACPI_TYPE_INTEGER) { 1321c451c13SAdrian Hunter *result = obj->integer.value; 1331c451c13SAdrian Hunter } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) { 1341c451c13SAdrian Hunter size_t len = min_t(size_t, obj->buffer.length, 4); 1351c451c13SAdrian Hunter 1361c451c13SAdrian Hunter *result = 0; 1371c451c13SAdrian Hunter memcpy(result, obj->buffer.pointer, len); 1381c451c13SAdrian Hunter } else { 1391c451c13SAdrian Hunter dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n", 1401c451c13SAdrian Hunter __func__, fn, obj->type, obj->buffer.length); 1411c451c13SAdrian Hunter err = -EINVAL; 1421c451c13SAdrian Hunter } 1431c451c13SAdrian Hunter 1441c451c13SAdrian Hunter ACPI_FREE(obj); 1451c451c13SAdrian Hunter 1461c451c13SAdrian Hunter return err; 1471c451c13SAdrian Hunter } 1481c451c13SAdrian Hunter 1491c451c13SAdrian Hunter static int intel_dsm(struct intel_host *intel_host, struct device *dev, 1501c451c13SAdrian Hunter unsigned int fn, u32 *result) 1511c451c13SAdrian Hunter { 1521c451c13SAdrian Hunter if (fn > 31 || !(intel_host->dsm_fns & (1 << fn))) 1531c451c13SAdrian Hunter return -EOPNOTSUPP; 1541c451c13SAdrian Hunter 1551c451c13SAdrian Hunter return __intel_dsm(intel_host, dev, fn, result); 1561c451c13SAdrian Hunter } 1571c451c13SAdrian Hunter 1581c451c13SAdrian Hunter static void intel_dsm_init(struct intel_host *intel_host, struct device *dev, 1591c451c13SAdrian Hunter struct mmc_host *mmc) 1601c451c13SAdrian Hunter { 1611c451c13SAdrian Hunter int err; 1621c451c13SAdrian Hunter 1630acccf41SAdrian Hunter intel_host->hs_caps = ~0; 1640acccf41SAdrian Hunter 1651c451c13SAdrian Hunter err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns); 1661c451c13SAdrian Hunter if (err) { 1671c451c13SAdrian Hunter pr_debug("%s: DSM not supported, error %d\n", 1681c451c13SAdrian Hunter mmc_hostname(mmc), err); 1691c451c13SAdrian Hunter return; 1701c451c13SAdrian Hunter } 1711c451c13SAdrian Hunter 1721c451c13SAdrian Hunter pr_debug("%s: DSM function mask %#x\n", 1731c451c13SAdrian Hunter mmc_hostname(mmc), intel_host->dsm_fns); 1740acccf41SAdrian Hunter 1750acccf41SAdrian Hunter intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps); 1761c451c13SAdrian Hunter } 1771c451c13SAdrian Hunter 1781c451c13SAdrian Hunter static int intel_start_signal_voltage_switch(struct mmc_host *mmc, 1791c451c13SAdrian Hunter struct mmc_ios *ios) 1801c451c13SAdrian Hunter { 1811c451c13SAdrian Hunter struct device *dev = mmc_dev(mmc); 1821c451c13SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 1831c451c13SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 1841c451c13SAdrian Hunter unsigned int fn; 1851c451c13SAdrian Hunter u32 result = 0; 1861c451c13SAdrian Hunter int err; 1871c451c13SAdrian Hunter 1881c451c13SAdrian Hunter err = sdhci_start_signal_voltage_switch(mmc, ios); 1891c451c13SAdrian Hunter if (err) 1901c451c13SAdrian Hunter return err; 1911c451c13SAdrian Hunter 1921c451c13SAdrian Hunter switch (ios->signal_voltage) { 1931c451c13SAdrian Hunter case MMC_SIGNAL_VOLTAGE_330: 1941c451c13SAdrian Hunter fn = INTEL_DSM_V33_SWITCH; 1951c451c13SAdrian Hunter break; 1961c451c13SAdrian Hunter case MMC_SIGNAL_VOLTAGE_180: 1971c451c13SAdrian Hunter fn = INTEL_DSM_V18_SWITCH; 1981c451c13SAdrian Hunter break; 1991c451c13SAdrian Hunter default: 2001c451c13SAdrian Hunter return 0; 2011c451c13SAdrian Hunter } 2021c451c13SAdrian Hunter 2031c451c13SAdrian Hunter err = intel_dsm(intel_host, dev, fn, &result); 2041c451c13SAdrian Hunter pr_debug("%s: %s DSM fn %u error %d result %u\n", 2051c451c13SAdrian Hunter mmc_hostname(mmc), __func__, fn, err, result); 2061c451c13SAdrian Hunter 2071c451c13SAdrian Hunter return 0; 2081c451c13SAdrian Hunter } 2091c451c13SAdrian Hunter 210b04fa064SAdrian Hunter static void sdhci_acpi_int_hw_reset(struct sdhci_host *host) 211b04fa064SAdrian Hunter { 212b04fa064SAdrian Hunter u8 reg; 213b04fa064SAdrian Hunter 214b04fa064SAdrian Hunter reg = sdhci_readb(host, SDHCI_POWER_CONTROL); 215b04fa064SAdrian Hunter reg |= 0x10; 216b04fa064SAdrian Hunter sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 217b04fa064SAdrian Hunter /* For eMMC, minimum is 1us but give it 9us for good measure */ 218b04fa064SAdrian Hunter udelay(9); 219b04fa064SAdrian Hunter reg &= ~0x10; 220b04fa064SAdrian Hunter sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 221b04fa064SAdrian Hunter /* For eMMC, minimum is 200us but give it 300us for good measure */ 222b04fa064SAdrian Hunter usleep_range(300, 1000); 223b04fa064SAdrian Hunter } 224b04fa064SAdrian Hunter 225c4e05037SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_dflt = { 2261771059cSRussell King .set_clock = sdhci_set_clock, 2272317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 22803231f9bSRussell King .reset = sdhci_reset, 22996d7b78cSRussell King .set_uhs_signaling = sdhci_set_uhs_signaling, 230c4e05037SAdrian Hunter }; 231c4e05037SAdrian Hunter 232b04fa064SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_int = { 2331771059cSRussell King .set_clock = sdhci_set_clock, 2342317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 23503231f9bSRussell King .reset = sdhci_reset, 23696d7b78cSRussell King .set_uhs_signaling = sdhci_set_uhs_signaling, 237b04fa064SAdrian Hunter .hw_reset = sdhci_acpi_int_hw_reset, 238b04fa064SAdrian Hunter }; 239b04fa064SAdrian Hunter 240b04fa064SAdrian Hunter static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { 241b04fa064SAdrian Hunter .ops = &sdhci_acpi_ops_int, 242b04fa064SAdrian Hunter }; 243b04fa064SAdrian Hunter 2446e1c7d61SAdrian Hunter #ifdef CONFIG_X86 2456e1c7d61SAdrian Hunter 2466e1c7d61SAdrian Hunter static bool sdhci_acpi_byt(void) 2476e1c7d61SAdrian Hunter { 2486e1c7d61SAdrian Hunter static const struct x86_cpu_id byt[] = { 2498ba4cb53SDave Hansen { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 }, 2506e1c7d61SAdrian Hunter {} 2516e1c7d61SAdrian Hunter }; 2526e1c7d61SAdrian Hunter 2536e1c7d61SAdrian Hunter return x86_match_cpu(byt); 2546e1c7d61SAdrian Hunter } 2556e1c7d61SAdrian Hunter 25617753d16SAdrian Hunter static bool sdhci_acpi_cht(void) 25717753d16SAdrian Hunter { 25817753d16SAdrian Hunter static const struct x86_cpu_id cht[] = { 25917753d16SAdrian Hunter { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, 26017753d16SAdrian Hunter {} 26117753d16SAdrian Hunter }; 26217753d16SAdrian Hunter 26317753d16SAdrian Hunter return x86_match_cpu(cht); 26417753d16SAdrian Hunter } 26517753d16SAdrian Hunter 2666e1c7d61SAdrian Hunter #define BYT_IOSF_SCCEP 0x63 2676e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_NETCTRL0 0x1078 2686e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8) 2696e1c7d61SAdrian Hunter 2706e1c7d61SAdrian Hunter static void sdhci_acpi_byt_setting(struct device *dev) 2716e1c7d61SAdrian Hunter { 2726e1c7d61SAdrian Hunter u32 val = 0; 2736e1c7d61SAdrian Hunter 2746e1c7d61SAdrian Hunter if (!sdhci_acpi_byt()) 2756e1c7d61SAdrian Hunter return; 2766e1c7d61SAdrian Hunter 2776e1c7d61SAdrian Hunter if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0, 2786e1c7d61SAdrian Hunter &val)) { 2796e1c7d61SAdrian Hunter dev_err(dev, "%s read error\n", __func__); 2806e1c7d61SAdrian Hunter return; 2816e1c7d61SAdrian Hunter } 2826e1c7d61SAdrian Hunter 2836e1c7d61SAdrian Hunter if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE)) 2846e1c7d61SAdrian Hunter return; 2856e1c7d61SAdrian Hunter 2866e1c7d61SAdrian Hunter val &= ~BYT_IOSF_OCP_TIMEOUT_BASE; 2876e1c7d61SAdrian Hunter 2886e1c7d61SAdrian Hunter if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0, 2896e1c7d61SAdrian Hunter val)) { 2906e1c7d61SAdrian Hunter dev_err(dev, "%s write error\n", __func__); 2916e1c7d61SAdrian Hunter return; 2926e1c7d61SAdrian Hunter } 2936e1c7d61SAdrian Hunter 2946e1c7d61SAdrian Hunter dev_dbg(dev, "%s completed\n", __func__); 2956e1c7d61SAdrian Hunter } 2966e1c7d61SAdrian Hunter 2976e1c7d61SAdrian Hunter static bool sdhci_acpi_byt_defer(struct device *dev) 2986e1c7d61SAdrian Hunter { 2996e1c7d61SAdrian Hunter if (!sdhci_acpi_byt()) 3006e1c7d61SAdrian Hunter return false; 3016e1c7d61SAdrian Hunter 3026e1c7d61SAdrian Hunter if (!iosf_mbi_available()) 3036e1c7d61SAdrian Hunter return true; 3046e1c7d61SAdrian Hunter 3056e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(dev); 3066e1c7d61SAdrian Hunter 3076e1c7d61SAdrian Hunter return false; 3086e1c7d61SAdrian Hunter } 3096e1c7d61SAdrian Hunter 31017753d16SAdrian Hunter static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device, 31117753d16SAdrian Hunter unsigned int slot, unsigned int parent_slot) 31217753d16SAdrian Hunter { 31317753d16SAdrian Hunter struct pci_dev *dev, *parent, *from = NULL; 31417753d16SAdrian Hunter 31517753d16SAdrian Hunter while (1) { 31617753d16SAdrian Hunter dev = pci_get_device(vendor, device, from); 31717753d16SAdrian Hunter pci_dev_put(from); 31817753d16SAdrian Hunter if (!dev) 31917753d16SAdrian Hunter break; 32017753d16SAdrian Hunter parent = pci_upstream_bridge(dev); 32117753d16SAdrian Hunter if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot && 32217753d16SAdrian Hunter parent && PCI_SLOT(parent->devfn) == parent_slot && 32317753d16SAdrian Hunter !pci_upstream_bridge(parent)) { 32417753d16SAdrian Hunter pci_dev_put(dev); 32517753d16SAdrian Hunter return true; 32617753d16SAdrian Hunter } 32717753d16SAdrian Hunter from = dev; 32817753d16SAdrian Hunter } 32917753d16SAdrian Hunter 33017753d16SAdrian Hunter return false; 33117753d16SAdrian Hunter } 33217753d16SAdrian Hunter 33317753d16SAdrian Hunter /* 33417753d16SAdrian Hunter * GPDwin uses PCI wifi which conflicts with SDIO's use of 33517753d16SAdrian Hunter * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is 33617753d16SAdrian Hunter * problematic, but since SDIO is only used for wifi, the presence of the PCI 33717753d16SAdrian Hunter * wifi card in the expected slot with an ACPI companion node, is used to 33817753d16SAdrian Hunter * indicate that acpi_device_fix_up_power() should be avoided. 33917753d16SAdrian Hunter */ 34017753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid, 34117753d16SAdrian Hunter const char *uid) 34217753d16SAdrian Hunter { 34317753d16SAdrian Hunter return sdhci_acpi_cht() && 34417753d16SAdrian Hunter !strcmp(hid, "80860F14") && 34517753d16SAdrian Hunter !strcmp(uid, "2") && 34617753d16SAdrian Hunter sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28); 34717753d16SAdrian Hunter } 34817753d16SAdrian Hunter 3496e1c7d61SAdrian Hunter #else 3506e1c7d61SAdrian Hunter 3516e1c7d61SAdrian Hunter static inline void sdhci_acpi_byt_setting(struct device *dev) 3526e1c7d61SAdrian Hunter { 3536e1c7d61SAdrian Hunter } 3546e1c7d61SAdrian Hunter 3556e1c7d61SAdrian Hunter static inline bool sdhci_acpi_byt_defer(struct device *dev) 3566e1c7d61SAdrian Hunter { 3576e1c7d61SAdrian Hunter return false; 3586e1c7d61SAdrian Hunter } 3596e1c7d61SAdrian Hunter 36017753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid, 36117753d16SAdrian Hunter const char *uid) 36217753d16SAdrian Hunter { 36317753d16SAdrian Hunter return false; 36417753d16SAdrian Hunter } 36517753d16SAdrian Hunter 3666e1c7d61SAdrian Hunter #endif 3676e1c7d61SAdrian Hunter 3686a645dd8SAdrian Hunter static int bxt_get_cd(struct mmc_host *mmc) 3696a645dd8SAdrian Hunter { 3706a645dd8SAdrian Hunter int gpio_cd = mmc_gpio_get_cd(mmc); 3716a645dd8SAdrian Hunter struct sdhci_host *host = mmc_priv(mmc); 3726a645dd8SAdrian Hunter unsigned long flags; 3736a645dd8SAdrian Hunter int ret = 0; 3746a645dd8SAdrian Hunter 3756a645dd8SAdrian Hunter if (!gpio_cd) 3766a645dd8SAdrian Hunter return 0; 3776a645dd8SAdrian Hunter 3786a645dd8SAdrian Hunter spin_lock_irqsave(&host->lock, flags); 3796a645dd8SAdrian Hunter 3806a645dd8SAdrian Hunter if (host->flags & SDHCI_DEVICE_DEAD) 3816a645dd8SAdrian Hunter goto out; 3826a645dd8SAdrian Hunter 3836a645dd8SAdrian Hunter ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 3846a645dd8SAdrian Hunter out: 3856a645dd8SAdrian Hunter spin_unlock_irqrestore(&host->lock, flags); 3866a645dd8SAdrian Hunter 3876a645dd8SAdrian Hunter return ret; 3886a645dd8SAdrian Hunter } 3896a645dd8SAdrian Hunter 390159cd328SAdrian Hunter static int intel_probe_slot(struct platform_device *pdev, const char *hid, 391159cd328SAdrian Hunter const char *uid) 392578b36b6SGao, Yunpeng { 393578b36b6SGao, Yunpeng struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 3941c451c13SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 395159cd328SAdrian Hunter struct sdhci_host *host = c->host; 396578b36b6SGao, Yunpeng 3978024379eSAdrian Hunter if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") && 3988024379eSAdrian Hunter sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 && 3998024379eSAdrian Hunter sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807) 4008024379eSAdrian Hunter host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */ 4018024379eSAdrian Hunter 402d3e97407SAzhar Shaikh if (hid && !strcmp(hid, "80865ACA")) 4036a645dd8SAdrian Hunter host->mmc_host_ops.get_cd = bxt_get_cd; 4046a645dd8SAdrian Hunter 4051c451c13SAdrian Hunter intel_dsm_init(intel_host, &pdev->dev, host->mmc); 4061c451c13SAdrian Hunter 4071c451c13SAdrian Hunter host->mmc_host_ops.start_signal_voltage_switch = 4081c451c13SAdrian Hunter intel_start_signal_voltage_switch; 4091c451c13SAdrian Hunter 410578b36b6SGao, Yunpeng return 0; 411578b36b6SGao, Yunpeng } 412578b36b6SGao, Yunpeng 4130acccf41SAdrian Hunter static int intel_setup_host(struct platform_device *pdev) 4140acccf41SAdrian Hunter { 4150acccf41SAdrian Hunter struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 4160acccf41SAdrian Hunter struct intel_host *intel_host = sdhci_acpi_priv(c); 4170acccf41SAdrian Hunter 4180acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25)) 4190acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25; 4200acccf41SAdrian Hunter 4210acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50)) 4220acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50; 4230acccf41SAdrian Hunter 4240acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50)) 4250acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50; 4260acccf41SAdrian Hunter 4270acccf41SAdrian Hunter if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104)) 4280acccf41SAdrian Hunter c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104; 4290acccf41SAdrian Hunter 4300acccf41SAdrian Hunter return 0; 4310acccf41SAdrian Hunter } 4320acccf41SAdrian Hunter 43307a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { 434b04fa064SAdrian Hunter .chip = &sdhci_acpi_chip_int, 435f25c3372SMaurice Petallo .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | 4369d65cb88SAdrian Hunter MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR | 437c80f275fSAdrian Hunter MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY, 43807a58883SAdrian Hunter .flags = SDHCI_ACPI_RUNTIME_PM, 439e1f5633aSAdrian Hunter .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 440e839b134SAdrian Hunter .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 441e839b134SAdrian Hunter SDHCI_QUIRK2_STOP_WITH_TC | 442e839b134SAdrian Hunter SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400, 443159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4440acccf41SAdrian Hunter .setup_host = intel_setup_host, 4451c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 44607a58883SAdrian Hunter }; 44707a58883SAdrian Hunter 448e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { 449e1f5633aSAdrian Hunter .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | 450e1f5633aSAdrian Hunter SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 451e5571397SAdrian Hunter .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON, 4529d65cb88SAdrian Hunter .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD | 453265984b3SAdrian Hunter MMC_CAP_WAIT_WHILE_BUSY, 454e5571397SAdrian Hunter .flags = SDHCI_ACPI_RUNTIME_PM, 455e5571397SAdrian Hunter .pm_caps = MMC_PM_KEEP_POWER, 456159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4570acccf41SAdrian Hunter .setup_host = intel_setup_host, 4581c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 459e5571397SAdrian Hunter }; 460e5571397SAdrian Hunter 46107a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { 4624fd4409cSAdrian Hunter .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | 4634fd4409cSAdrian Hunter SDHCI_ACPI_RUNTIME_PM, 464e1f5633aSAdrian Hunter .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 465934e31b9SAdrian Hunter .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | 466934e31b9SAdrian Hunter SDHCI_QUIRK2_STOP_WITH_TC, 467d3e97407SAzhar Shaikh .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM, 468159cd328SAdrian Hunter .probe_slot = intel_probe_slot, 4690acccf41SAdrian Hunter .setup_host = intel_setup_host, 4701c451c13SAdrian Hunter .priv_size = sizeof(struct intel_host), 47107a58883SAdrian Hunter }; 47207a58883SAdrian Hunter 47370cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = { 47470cce2afSPhilip Elcan .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 47570cce2afSPhilip Elcan .quirks2 = SDHCI_QUIRK2_NO_1_8_V, 47670cce2afSPhilip Elcan .caps = MMC_CAP_NONREMOVABLE, 47770cce2afSPhilip Elcan }; 47870cce2afSPhilip Elcan 47970cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = { 48070cce2afSPhilip Elcan .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 48170cce2afSPhilip Elcan .caps = MMC_CAP_NONREMOVABLE, 48270cce2afSPhilip Elcan }; 48370cce2afSPhilip Elcan 48434597a3fSShah Nehal-Bakulchandra /* AMD sdhci reset dll register. */ 48534597a3fSShah Nehal-Bakulchandra #define SDHCI_AMD_RESET_DLL_REGISTER 0x908 48634597a3fSShah Nehal-Bakulchandra 48734597a3fSShah Nehal-Bakulchandra static int amd_select_drive_strength(struct mmc_card *card, 48834597a3fSShah Nehal-Bakulchandra unsigned int max_dtr, int host_drv, 48934597a3fSShah Nehal-Bakulchandra int card_drv, int *drv_type) 49034597a3fSShah Nehal-Bakulchandra { 49134597a3fSShah Nehal-Bakulchandra return MMC_SET_DRIVER_TYPE_A; 49234597a3fSShah Nehal-Bakulchandra } 49334597a3fSShah Nehal-Bakulchandra 49434597a3fSShah Nehal-Bakulchandra static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host) 49534597a3fSShah Nehal-Bakulchandra { 49634597a3fSShah Nehal-Bakulchandra /* AMD Platform requires dll setting */ 49734597a3fSShah Nehal-Bakulchandra sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER); 49834597a3fSShah Nehal-Bakulchandra usleep_range(10, 20); 49934597a3fSShah Nehal-Bakulchandra sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER); 50034597a3fSShah Nehal-Bakulchandra } 50134597a3fSShah Nehal-Bakulchandra 50234597a3fSShah Nehal-Bakulchandra /* 50334597a3fSShah Nehal-Bakulchandra * For AMD Platform it is required to disable the tuning 50434597a3fSShah Nehal-Bakulchandra * bit first controller to bring to HS Mode from HS200 50534597a3fSShah Nehal-Bakulchandra * mode, later enable to tune to HS400 mode. 50634597a3fSShah Nehal-Bakulchandra */ 50734597a3fSShah Nehal-Bakulchandra static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 50834597a3fSShah Nehal-Bakulchandra { 50934597a3fSShah Nehal-Bakulchandra struct sdhci_host *host = mmc_priv(mmc); 51034597a3fSShah Nehal-Bakulchandra unsigned int old_timing = host->timing; 51134597a3fSShah Nehal-Bakulchandra 51234597a3fSShah Nehal-Bakulchandra sdhci_set_ios(mmc, ios); 51334597a3fSShah Nehal-Bakulchandra if (old_timing == MMC_TIMING_MMC_HS200 && 51434597a3fSShah Nehal-Bakulchandra ios->timing == MMC_TIMING_MMC_HS) 51534597a3fSShah Nehal-Bakulchandra sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2); 51634597a3fSShah Nehal-Bakulchandra if (old_timing != MMC_TIMING_MMC_HS400 && 51734597a3fSShah Nehal-Bakulchandra ios->timing == MMC_TIMING_MMC_HS400) { 51834597a3fSShah Nehal-Bakulchandra sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2); 51934597a3fSShah Nehal-Bakulchandra sdhci_acpi_amd_hs400_dll(host); 52034597a3fSShah Nehal-Bakulchandra } 52134597a3fSShah Nehal-Bakulchandra } 52234597a3fSShah Nehal-Bakulchandra 52334597a3fSShah Nehal-Bakulchandra static const struct sdhci_ops sdhci_acpi_ops_amd = { 52434597a3fSShah Nehal-Bakulchandra .set_clock = sdhci_set_clock, 52534597a3fSShah Nehal-Bakulchandra .set_bus_width = sdhci_set_bus_width, 52634597a3fSShah Nehal-Bakulchandra .reset = sdhci_reset, 52734597a3fSShah Nehal-Bakulchandra .set_uhs_signaling = sdhci_set_uhs_signaling, 52834597a3fSShah Nehal-Bakulchandra }; 52934597a3fSShah Nehal-Bakulchandra 53034597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = { 53134597a3fSShah Nehal-Bakulchandra .ops = &sdhci_acpi_ops_amd, 53234597a3fSShah Nehal-Bakulchandra }; 53334597a3fSShah Nehal-Bakulchandra 53434597a3fSShah Nehal-Bakulchandra static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev, 53534597a3fSShah Nehal-Bakulchandra const char *hid, const char *uid) 53634597a3fSShah Nehal-Bakulchandra { 53734597a3fSShah Nehal-Bakulchandra struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 53834597a3fSShah Nehal-Bakulchandra struct sdhci_host *host = c->host; 53934597a3fSShah Nehal-Bakulchandra 54034597a3fSShah Nehal-Bakulchandra sdhci_read_caps(host); 54134597a3fSShah Nehal-Bakulchandra if (host->caps1 & SDHCI_SUPPORT_DDR50) 54234597a3fSShah Nehal-Bakulchandra host->mmc->caps = MMC_CAP_1_8V_DDR; 54334597a3fSShah Nehal-Bakulchandra 54434597a3fSShah Nehal-Bakulchandra if ((host->caps1 & SDHCI_SUPPORT_SDR104) && 54534597a3fSShah Nehal-Bakulchandra (host->mmc->caps & MMC_CAP_1_8V_DDR)) 54634597a3fSShah Nehal-Bakulchandra host->mmc->caps2 = MMC_CAP2_HS400_1_8V; 54734597a3fSShah Nehal-Bakulchandra 54834597a3fSShah Nehal-Bakulchandra host->mmc_host_ops.select_drive_strength = amd_select_drive_strength; 54934597a3fSShah Nehal-Bakulchandra host->mmc_host_ops.set_ios = amd_set_ios; 55034597a3fSShah Nehal-Bakulchandra return 0; 55134597a3fSShah Nehal-Bakulchandra } 55234597a3fSShah Nehal-Bakulchandra 55334597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = { 55434597a3fSShah Nehal-Bakulchandra .chip = &sdhci_acpi_chip_amd, 55534597a3fSShah Nehal-Bakulchandra .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE, 55634597a3fSShah Nehal-Bakulchandra .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE | 55734597a3fSShah Nehal-Bakulchandra SDHCI_QUIRK_32BIT_ADMA_SIZE, 55834597a3fSShah Nehal-Bakulchandra .probe_slot = sdhci_acpi_emmc_amd_probe_slot, 55934597a3fSShah Nehal-Bakulchandra }; 56034597a3fSShah Nehal-Bakulchandra 56107a58883SAdrian Hunter struct sdhci_acpi_uid_slot { 56207a58883SAdrian Hunter const char *hid; 56307a58883SAdrian Hunter const char *uid; 56407a58883SAdrian Hunter const struct sdhci_acpi_slot *slot; 56507a58883SAdrian Hunter }; 56607a58883SAdrian Hunter 56707a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = { 568e839b134SAdrian Hunter { "80865ACA", NULL, &sdhci_acpi_slot_int_sd }, 569e839b134SAdrian Hunter { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc }, 570e839b134SAdrian Hunter { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio }, 57107a58883SAdrian Hunter { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc }, 572db52d4f8SDaniel Drake { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio }, 57307a58883SAdrian Hunter { "80860F14" , "3" , &sdhci_acpi_slot_int_sd }, 574aad95dc4SAdrian Hunter { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, 57507a58883SAdrian Hunter { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, 5767147eaf3SAdrian Hunter { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd }, 57707a58883SAdrian Hunter { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, 57807c001c1SMika Westerberg { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, 579d0ed8e6bSAdrian Hunter { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio }, 5800cd2f044SMichele Curti { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd }, 58107a58883SAdrian Hunter { "PNP0D40" }, 58270cce2afSPhilip Elcan { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v }, 58370cce2afSPhilip Elcan { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd }, 58434597a3fSShah Nehal-Bakulchandra { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc }, 58507a58883SAdrian Hunter { }, 58607a58883SAdrian Hunter }; 58707a58883SAdrian Hunter 588c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = { 589e839b134SAdrian Hunter { "80865ACA" }, 590e839b134SAdrian Hunter { "80865ACC" }, 591e839b134SAdrian Hunter { "80865AD0" }, 59207a58883SAdrian Hunter { "80860F14" }, 593aad95dc4SAdrian Hunter { "80860F16" }, 59407a58883SAdrian Hunter { "INT33BB" }, 59507a58883SAdrian Hunter { "INT33C6" }, 59607c001c1SMika Westerberg { "INT3436" }, 597d0ed8e6bSAdrian Hunter { "INT344D" }, 598c4e05037SAdrian Hunter { "PNP0D40" }, 59970cce2afSPhilip Elcan { "QCOM8051" }, 60070cce2afSPhilip Elcan { "QCOM8052" }, 60134597a3fSShah Nehal-Bakulchandra { "AMDI0040" }, 602c4e05037SAdrian Hunter { }, 603c4e05037SAdrian Hunter }; 604c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); 605c4e05037SAdrian Hunter 6063db35251SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid, 60707a58883SAdrian Hunter const char *uid) 608c4e05037SAdrian Hunter { 60907a58883SAdrian Hunter const struct sdhci_acpi_uid_slot *u; 610c4e05037SAdrian Hunter 61107a58883SAdrian Hunter for (u = sdhci_acpi_uids; u->hid; u++) { 61207a58883SAdrian Hunter if (strcmp(u->hid, hid)) 61307a58883SAdrian Hunter continue; 61407a58883SAdrian Hunter if (!u->uid) 61507a58883SAdrian Hunter return u->slot; 61607a58883SAdrian Hunter if (uid && !strcmp(u->uid, uid)) 61707a58883SAdrian Hunter return u->slot; 61807a58883SAdrian Hunter } 619c4e05037SAdrian Hunter return NULL; 620c4e05037SAdrian Hunter } 621c4e05037SAdrian Hunter 6224e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev) 623c4e05037SAdrian Hunter { 624c4e05037SAdrian Hunter struct device *dev = &pdev->dev; 625f07b7952SAdrian Hunter const struct sdhci_acpi_slot *slot; 626e5bbf307SAdrian Hunter struct acpi_device *device, *child; 627c4e05037SAdrian Hunter struct sdhci_acpi_host *c; 628c4e05037SAdrian Hunter struct sdhci_host *host; 629c4e05037SAdrian Hunter struct resource *iomem; 630c4e05037SAdrian Hunter resource_size_t len; 631f07b7952SAdrian Hunter size_t priv_size; 632c4e05037SAdrian Hunter const char *hid; 6333db35251SAdrian Hunter const char *uid; 63487875655SMika Westerberg int err; 635c4e05037SAdrian Hunter 636cd25c7beSAndy Shevchenko device = ACPI_COMPANION(dev); 637cd25c7beSAndy Shevchenko if (!device) 638c4e05037SAdrian Hunter return -ENODEV; 639c4e05037SAdrian Hunter 64017753d16SAdrian Hunter hid = acpi_device_hid(device); 641a2038497SAdrian Hunter uid = acpi_device_uid(device); 64217753d16SAdrian Hunter 643f07b7952SAdrian Hunter slot = sdhci_acpi_get_slot(hid, uid); 644f07b7952SAdrian Hunter 645e5bbf307SAdrian Hunter /* Power on the SDHCI controller and its children */ 646e5bbf307SAdrian Hunter acpi_device_fix_up_power(device); 64717753d16SAdrian Hunter if (!sdhci_acpi_no_fixup_child_power(hid, uid)) { 648e5bbf307SAdrian Hunter list_for_each_entry(child, &device->children, node) 649e1d070c3SHans de Goede if (child->status.present && child->status.enabled) 650e5bbf307SAdrian Hunter acpi_device_fix_up_power(child); 65117753d16SAdrian Hunter } 652e5bbf307SAdrian Hunter 6536e1c7d61SAdrian Hunter if (sdhci_acpi_byt_defer(dev)) 6546e1c7d61SAdrian Hunter return -EPROBE_DEFER; 6556e1c7d61SAdrian Hunter 656c4e05037SAdrian Hunter iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 657c4e05037SAdrian Hunter if (!iomem) 658c4e05037SAdrian Hunter return -ENOMEM; 659c4e05037SAdrian Hunter 660c4e05037SAdrian Hunter len = resource_size(iomem); 661c4e05037SAdrian Hunter if (len < 0x100) 662c4e05037SAdrian Hunter dev_err(dev, "Invalid iomem size!\n"); 663c4e05037SAdrian Hunter 664c4e05037SAdrian Hunter if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev))) 665c4e05037SAdrian Hunter return -ENOMEM; 666c4e05037SAdrian Hunter 667f07b7952SAdrian Hunter priv_size = slot ? slot->priv_size : 0; 668f07b7952SAdrian Hunter host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size); 669c4e05037SAdrian Hunter if (IS_ERR(host)) 670c4e05037SAdrian Hunter return PTR_ERR(host); 671c4e05037SAdrian Hunter 672c4e05037SAdrian Hunter c = sdhci_priv(host); 673c4e05037SAdrian Hunter c->host = host; 674f07b7952SAdrian Hunter c->slot = slot; 675c4e05037SAdrian Hunter c->pdev = pdev; 676c4e05037SAdrian Hunter c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM); 677c4e05037SAdrian Hunter 678c4e05037SAdrian Hunter platform_set_drvdata(pdev, c); 679c4e05037SAdrian Hunter 680c4e05037SAdrian Hunter host->hw_name = "ACPI"; 681c4e05037SAdrian Hunter host->ops = &sdhci_acpi_ops_dflt; 682c4e05037SAdrian Hunter host->irq = platform_get_irq(pdev, 0); 683d58ac803SAdrian Hunter if (host->irq < 0) { 6841b7ba57eSArvind Yadav err = -EINVAL; 6851b7ba57eSArvind Yadav goto err_free; 6861b7ba57eSArvind Yadav } 687c4e05037SAdrian Hunter 688c4e05037SAdrian Hunter host->ioaddr = devm_ioremap_nocache(dev, iomem->start, 689c4e05037SAdrian Hunter resource_size(iomem)); 690c4e05037SAdrian Hunter if (host->ioaddr == NULL) { 691c4e05037SAdrian Hunter err = -ENOMEM; 692c4e05037SAdrian Hunter goto err_free; 693c4e05037SAdrian Hunter } 694c4e05037SAdrian Hunter 695c4e05037SAdrian Hunter if (c->slot) { 696578b36b6SGao, Yunpeng if (c->slot->probe_slot) { 6977dafca83SAdrian Hunter err = c->slot->probe_slot(pdev, hid, uid); 698578b36b6SGao, Yunpeng if (err) 699578b36b6SGao, Yunpeng goto err_free; 700578b36b6SGao, Yunpeng } 701c4e05037SAdrian Hunter if (c->slot->chip) { 702c4e05037SAdrian Hunter host->ops = c->slot->chip->ops; 703c4e05037SAdrian Hunter host->quirks |= c->slot->chip->quirks; 704c4e05037SAdrian Hunter host->quirks2 |= c->slot->chip->quirks2; 705c4e05037SAdrian Hunter host->mmc->caps |= c->slot->chip->caps; 706c4e05037SAdrian Hunter host->mmc->caps2 |= c->slot->chip->caps2; 707c4e05037SAdrian Hunter host->mmc->pm_caps |= c->slot->chip->pm_caps; 708c4e05037SAdrian Hunter } 709c4e05037SAdrian Hunter host->quirks |= c->slot->quirks; 710c4e05037SAdrian Hunter host->quirks2 |= c->slot->quirks2; 711c4e05037SAdrian Hunter host->mmc->caps |= c->slot->caps; 712c4e05037SAdrian Hunter host->mmc->caps2 |= c->slot->caps2; 713c4e05037SAdrian Hunter host->mmc->pm_caps |= c->slot->pm_caps; 714c4e05037SAdrian Hunter } 715c4e05037SAdrian Hunter 7160d3e3350SAdrian Hunter host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; 7170d3e3350SAdrian Hunter 7184fd4409cSAdrian Hunter if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { 7194fd4409cSAdrian Hunter bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); 7204fd4409cSAdrian Hunter 721e28d6f04SZhang Rui err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL); 722e28d6f04SZhang Rui if (err) { 723e28d6f04SZhang Rui if (err == -EPROBE_DEFER) 724e28d6f04SZhang Rui goto err_free; 7254fd4409cSAdrian Hunter dev_warn(dev, "failed to setup card detect gpio\n"); 7264fd4409cSAdrian Hunter c->use_runtime_pm = false; 7274fd4409cSAdrian Hunter } 7284fd4409cSAdrian Hunter } 7294fd4409cSAdrian Hunter 7300cc1a0f4SAdrian Hunter err = sdhci_setup_host(host); 731c4e05037SAdrian Hunter if (err) 732c4e05037SAdrian Hunter goto err_free; 733c4e05037SAdrian Hunter 7340cc1a0f4SAdrian Hunter if (c->slot && c->slot->setup_host) { 7350cc1a0f4SAdrian Hunter err = c->slot->setup_host(pdev); 7360cc1a0f4SAdrian Hunter if (err) 7370cc1a0f4SAdrian Hunter goto err_cleanup; 7380cc1a0f4SAdrian Hunter } 7390cc1a0f4SAdrian Hunter 7400cc1a0f4SAdrian Hunter err = __sdhci_add_host(host); 7410cc1a0f4SAdrian Hunter if (err) 7420cc1a0f4SAdrian Hunter goto err_cleanup; 7430cc1a0f4SAdrian Hunter 744c4e05037SAdrian Hunter if (c->use_runtime_pm) { 7451d1ff458SAdrian Hunter pm_runtime_set_active(dev); 746c4e05037SAdrian Hunter pm_suspend_ignore_children(dev, 1); 747c4e05037SAdrian Hunter pm_runtime_set_autosuspend_delay(dev, 50); 748c4e05037SAdrian Hunter pm_runtime_use_autosuspend(dev); 749c4e05037SAdrian Hunter pm_runtime_enable(dev); 750c4e05037SAdrian Hunter } 751c4e05037SAdrian Hunter 7524e6a2ef9SFu, Zhonghui device_enable_async_suspend(dev); 7534e6a2ef9SFu, Zhonghui 754c4e05037SAdrian Hunter return 0; 755c4e05037SAdrian Hunter 7560cc1a0f4SAdrian Hunter err_cleanup: 7570cc1a0f4SAdrian Hunter sdhci_cleanup_host(c->host); 758c4e05037SAdrian Hunter err_free: 759c4e05037SAdrian Hunter sdhci_free_host(c->host); 760c4e05037SAdrian Hunter return err; 761c4e05037SAdrian Hunter } 762c4e05037SAdrian Hunter 7634e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev) 764c4e05037SAdrian Hunter { 765c4e05037SAdrian Hunter struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 766c4e05037SAdrian Hunter struct device *dev = &pdev->dev; 767c4e05037SAdrian Hunter int dead; 768c4e05037SAdrian Hunter 769c4e05037SAdrian Hunter if (c->use_runtime_pm) { 770c4e05037SAdrian Hunter pm_runtime_get_sync(dev); 771c4e05037SAdrian Hunter pm_runtime_disable(dev); 772c4e05037SAdrian Hunter pm_runtime_put_noidle(dev); 773c4e05037SAdrian Hunter } 774c4e05037SAdrian Hunter 775578b36b6SGao, Yunpeng if (c->slot && c->slot->remove_slot) 776578b36b6SGao, Yunpeng c->slot->remove_slot(pdev); 777578b36b6SGao, Yunpeng 778c4e05037SAdrian Hunter dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); 779c4e05037SAdrian Hunter sdhci_remove_host(c->host, dead); 780c4e05037SAdrian Hunter sdhci_free_host(c->host); 781c4e05037SAdrian Hunter 782c4e05037SAdrian Hunter return 0; 783c4e05037SAdrian Hunter } 784c4e05037SAdrian Hunter 785c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP 786c4e05037SAdrian Hunter 787c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev) 788c4e05037SAdrian Hunter { 789c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 790d38dcad4SAdrian Hunter struct sdhci_host *host = c->host; 791c4e05037SAdrian Hunter 792d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 793d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 794d38dcad4SAdrian Hunter 795d38dcad4SAdrian Hunter return sdhci_suspend_host(host); 796c4e05037SAdrian Hunter } 797c4e05037SAdrian Hunter 798c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev) 799c4e05037SAdrian Hunter { 800c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 801c4e05037SAdrian Hunter 8026e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(&c->pdev->dev); 8036e1c7d61SAdrian Hunter 804c4e05037SAdrian Hunter return sdhci_resume_host(c->host); 805c4e05037SAdrian Hunter } 806c4e05037SAdrian Hunter 807c4e05037SAdrian Hunter #endif 808c4e05037SAdrian Hunter 809162d6f98SRafael J. Wysocki #ifdef CONFIG_PM 810c4e05037SAdrian Hunter 811c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev) 812c4e05037SAdrian Hunter { 813c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 814d38dcad4SAdrian Hunter struct sdhci_host *host = c->host; 815c4e05037SAdrian Hunter 816d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 817d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 818d38dcad4SAdrian Hunter 819d38dcad4SAdrian Hunter return sdhci_runtime_suspend_host(host); 820c4e05037SAdrian Hunter } 821c4e05037SAdrian Hunter 822c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev) 823c4e05037SAdrian Hunter { 824c4e05037SAdrian Hunter struct sdhci_acpi_host *c = dev_get_drvdata(dev); 825c4e05037SAdrian Hunter 8266e1c7d61SAdrian Hunter sdhci_acpi_byt_setting(&c->pdev->dev); 8276e1c7d61SAdrian Hunter 828c4e05037SAdrian Hunter return sdhci_runtime_resume_host(c->host); 829c4e05037SAdrian Hunter } 830c4e05037SAdrian Hunter 831c4e05037SAdrian Hunter #endif 832c4e05037SAdrian Hunter 833c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = { 834dafed447SUlf Hansson SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume) 8351d75f74bSPeter Griffin SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend, 8369b449e99SUlf Hansson sdhci_acpi_runtime_resume, NULL) 837c4e05037SAdrian Hunter }; 838c4e05037SAdrian Hunter 839c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = { 840c4e05037SAdrian Hunter .driver = { 841c4e05037SAdrian Hunter .name = "sdhci-acpi", 842c4e05037SAdrian Hunter .acpi_match_table = sdhci_acpi_ids, 843c4e05037SAdrian Hunter .pm = &sdhci_acpi_pm_ops, 844c4e05037SAdrian Hunter }, 845c4e05037SAdrian Hunter .probe = sdhci_acpi_probe, 8464e608e4eSGreg Kroah-Hartman .remove = sdhci_acpi_remove, 847c4e05037SAdrian Hunter }; 848c4e05037SAdrian Hunter 849c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver); 850c4e05037SAdrian Hunter 851c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); 852c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter"); 853c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2"); 854