xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision 0cc1a0f4)
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 
1001c451c13SAdrian Hunter enum {
1011c451c13SAdrian Hunter 	INTEL_DSM_FNS		=  0,
1021c451c13SAdrian Hunter 	INTEL_DSM_V18_SWITCH	=  3,
1031c451c13SAdrian Hunter 	INTEL_DSM_V33_SWITCH	=  4,
1041c451c13SAdrian Hunter };
1051c451c13SAdrian Hunter 
1061c451c13SAdrian Hunter struct intel_host {
1071c451c13SAdrian Hunter 	u32	dsm_fns;
1081c451c13SAdrian Hunter };
1091c451c13SAdrian Hunter 
1101c451c13SAdrian Hunter static const guid_t intel_dsm_guid =
1111c451c13SAdrian Hunter 	GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
1121c451c13SAdrian Hunter 		  0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
1131c451c13SAdrian Hunter 
1141c451c13SAdrian Hunter static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
1151c451c13SAdrian Hunter 		       unsigned int fn, u32 *result)
1161c451c13SAdrian Hunter {
1171c451c13SAdrian Hunter 	union acpi_object *obj;
1181c451c13SAdrian Hunter 	int err = 0;
1191c451c13SAdrian Hunter 
1201c451c13SAdrian Hunter 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
1211c451c13SAdrian Hunter 	if (!obj)
1221c451c13SAdrian Hunter 		return -EOPNOTSUPP;
1231c451c13SAdrian Hunter 
1241c451c13SAdrian Hunter 	if (obj->type == ACPI_TYPE_INTEGER) {
1251c451c13SAdrian Hunter 		*result = obj->integer.value;
1261c451c13SAdrian Hunter 	} else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
1271c451c13SAdrian Hunter 		size_t len = min_t(size_t, obj->buffer.length, 4);
1281c451c13SAdrian Hunter 
1291c451c13SAdrian Hunter 		*result = 0;
1301c451c13SAdrian Hunter 		memcpy(result, obj->buffer.pointer, len);
1311c451c13SAdrian Hunter 	} else {
1321c451c13SAdrian Hunter 		dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
1331c451c13SAdrian Hunter 			__func__, fn, obj->type, obj->buffer.length);
1341c451c13SAdrian Hunter 		err = -EINVAL;
1351c451c13SAdrian Hunter 	}
1361c451c13SAdrian Hunter 
1371c451c13SAdrian Hunter 	ACPI_FREE(obj);
1381c451c13SAdrian Hunter 
1391c451c13SAdrian Hunter 	return err;
1401c451c13SAdrian Hunter }
1411c451c13SAdrian Hunter 
1421c451c13SAdrian Hunter static int intel_dsm(struct intel_host *intel_host, struct device *dev,
1431c451c13SAdrian Hunter 		     unsigned int fn, u32 *result)
1441c451c13SAdrian Hunter {
1451c451c13SAdrian Hunter 	if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
1461c451c13SAdrian Hunter 		return -EOPNOTSUPP;
1471c451c13SAdrian Hunter 
1481c451c13SAdrian Hunter 	return __intel_dsm(intel_host, dev, fn, result);
1491c451c13SAdrian Hunter }
1501c451c13SAdrian Hunter 
1511c451c13SAdrian Hunter static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
1521c451c13SAdrian Hunter 			   struct mmc_host *mmc)
1531c451c13SAdrian Hunter {
1541c451c13SAdrian Hunter 	int err;
1551c451c13SAdrian Hunter 
1561c451c13SAdrian Hunter 	err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
1571c451c13SAdrian Hunter 	if (err) {
1581c451c13SAdrian Hunter 		pr_debug("%s: DSM not supported, error %d\n",
1591c451c13SAdrian Hunter 			 mmc_hostname(mmc), err);
1601c451c13SAdrian Hunter 		return;
1611c451c13SAdrian Hunter 	}
1621c451c13SAdrian Hunter 
1631c451c13SAdrian Hunter 	pr_debug("%s: DSM function mask %#x\n",
1641c451c13SAdrian Hunter 		 mmc_hostname(mmc), intel_host->dsm_fns);
1651c451c13SAdrian Hunter }
1661c451c13SAdrian Hunter 
1671c451c13SAdrian Hunter static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
1681c451c13SAdrian Hunter 					     struct mmc_ios *ios)
1691c451c13SAdrian Hunter {
1701c451c13SAdrian Hunter 	struct device *dev = mmc_dev(mmc);
1711c451c13SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
1721c451c13SAdrian Hunter 	struct intel_host *intel_host = sdhci_acpi_priv(c);
1731c451c13SAdrian Hunter 	unsigned int fn;
1741c451c13SAdrian Hunter 	u32 result = 0;
1751c451c13SAdrian Hunter 	int err;
1761c451c13SAdrian Hunter 
1771c451c13SAdrian Hunter 	err = sdhci_start_signal_voltage_switch(mmc, ios);
1781c451c13SAdrian Hunter 	if (err)
1791c451c13SAdrian Hunter 		return err;
1801c451c13SAdrian Hunter 
1811c451c13SAdrian Hunter 	switch (ios->signal_voltage) {
1821c451c13SAdrian Hunter 	case MMC_SIGNAL_VOLTAGE_330:
1831c451c13SAdrian Hunter 		fn = INTEL_DSM_V33_SWITCH;
1841c451c13SAdrian Hunter 		break;
1851c451c13SAdrian Hunter 	case MMC_SIGNAL_VOLTAGE_180:
1861c451c13SAdrian Hunter 		fn = INTEL_DSM_V18_SWITCH;
1871c451c13SAdrian Hunter 		break;
1881c451c13SAdrian Hunter 	default:
1891c451c13SAdrian Hunter 		return 0;
1901c451c13SAdrian Hunter 	}
1911c451c13SAdrian Hunter 
1921c451c13SAdrian Hunter 	err = intel_dsm(intel_host, dev, fn, &result);
1931c451c13SAdrian Hunter 	pr_debug("%s: %s DSM fn %u error %d result %u\n",
1941c451c13SAdrian Hunter 		 mmc_hostname(mmc), __func__, fn, err, result);
1951c451c13SAdrian Hunter 
1961c451c13SAdrian Hunter 	return 0;
1971c451c13SAdrian Hunter }
1981c451c13SAdrian Hunter 
199b04fa064SAdrian Hunter static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
200b04fa064SAdrian Hunter {
201b04fa064SAdrian Hunter 	u8 reg;
202b04fa064SAdrian Hunter 
203b04fa064SAdrian Hunter 	reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
204b04fa064SAdrian Hunter 	reg |= 0x10;
205b04fa064SAdrian Hunter 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
206b04fa064SAdrian Hunter 	/* For eMMC, minimum is 1us but give it 9us for good measure */
207b04fa064SAdrian Hunter 	udelay(9);
208b04fa064SAdrian Hunter 	reg &= ~0x10;
209b04fa064SAdrian Hunter 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
210b04fa064SAdrian Hunter 	/* For eMMC, minimum is 200us but give it 300us for good measure */
211b04fa064SAdrian Hunter 	usleep_range(300, 1000);
212b04fa064SAdrian Hunter }
213b04fa064SAdrian Hunter 
214c4e05037SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_dflt = {
2151771059cSRussell King 	.set_clock = sdhci_set_clock,
2162317f56cSRussell King 	.set_bus_width = sdhci_set_bus_width,
21703231f9bSRussell King 	.reset = sdhci_reset,
21896d7b78cSRussell King 	.set_uhs_signaling = sdhci_set_uhs_signaling,
219c4e05037SAdrian Hunter };
220c4e05037SAdrian Hunter 
221b04fa064SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_int = {
2221771059cSRussell King 	.set_clock = sdhci_set_clock,
2232317f56cSRussell King 	.set_bus_width = sdhci_set_bus_width,
22403231f9bSRussell King 	.reset = sdhci_reset,
22596d7b78cSRussell King 	.set_uhs_signaling = sdhci_set_uhs_signaling,
226b04fa064SAdrian Hunter 	.hw_reset   = sdhci_acpi_int_hw_reset,
227b04fa064SAdrian Hunter };
228b04fa064SAdrian Hunter 
229b04fa064SAdrian Hunter static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
230b04fa064SAdrian Hunter 	.ops = &sdhci_acpi_ops_int,
231b04fa064SAdrian Hunter };
232b04fa064SAdrian Hunter 
2336e1c7d61SAdrian Hunter #ifdef CONFIG_X86
2346e1c7d61SAdrian Hunter 
2356e1c7d61SAdrian Hunter static bool sdhci_acpi_byt(void)
2366e1c7d61SAdrian Hunter {
2376e1c7d61SAdrian Hunter 	static const struct x86_cpu_id byt[] = {
2388ba4cb53SDave Hansen 		{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
2396e1c7d61SAdrian Hunter 		{}
2406e1c7d61SAdrian Hunter 	};
2416e1c7d61SAdrian Hunter 
2426e1c7d61SAdrian Hunter 	return x86_match_cpu(byt);
2436e1c7d61SAdrian Hunter }
2446e1c7d61SAdrian Hunter 
24517753d16SAdrian Hunter static bool sdhci_acpi_cht(void)
24617753d16SAdrian Hunter {
24717753d16SAdrian Hunter 	static const struct x86_cpu_id cht[] = {
24817753d16SAdrian Hunter 		{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
24917753d16SAdrian Hunter 		{}
25017753d16SAdrian Hunter 	};
25117753d16SAdrian Hunter 
25217753d16SAdrian Hunter 	return x86_match_cpu(cht);
25317753d16SAdrian Hunter }
25417753d16SAdrian Hunter 
2556e1c7d61SAdrian Hunter #define BYT_IOSF_SCCEP			0x63
2566e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_NETCTRL0		0x1078
2576e1c7d61SAdrian Hunter #define BYT_IOSF_OCP_TIMEOUT_BASE	GENMASK(10, 8)
2586e1c7d61SAdrian Hunter 
2596e1c7d61SAdrian Hunter static void sdhci_acpi_byt_setting(struct device *dev)
2606e1c7d61SAdrian Hunter {
2616e1c7d61SAdrian Hunter 	u32 val = 0;
2626e1c7d61SAdrian Hunter 
2636e1c7d61SAdrian Hunter 	if (!sdhci_acpi_byt())
2646e1c7d61SAdrian Hunter 		return;
2656e1c7d61SAdrian Hunter 
2666e1c7d61SAdrian Hunter 	if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
2676e1c7d61SAdrian Hunter 			  &val)) {
2686e1c7d61SAdrian Hunter 		dev_err(dev, "%s read error\n", __func__);
2696e1c7d61SAdrian Hunter 		return;
2706e1c7d61SAdrian Hunter 	}
2716e1c7d61SAdrian Hunter 
2726e1c7d61SAdrian Hunter 	if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
2736e1c7d61SAdrian Hunter 		return;
2746e1c7d61SAdrian Hunter 
2756e1c7d61SAdrian Hunter 	val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
2766e1c7d61SAdrian Hunter 
2776e1c7d61SAdrian Hunter 	if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
2786e1c7d61SAdrian Hunter 			   val)) {
2796e1c7d61SAdrian Hunter 		dev_err(dev, "%s write error\n", __func__);
2806e1c7d61SAdrian Hunter 		return;
2816e1c7d61SAdrian Hunter 	}
2826e1c7d61SAdrian Hunter 
2836e1c7d61SAdrian Hunter 	dev_dbg(dev, "%s completed\n", __func__);
2846e1c7d61SAdrian Hunter }
2856e1c7d61SAdrian Hunter 
2866e1c7d61SAdrian Hunter static bool sdhci_acpi_byt_defer(struct device *dev)
2876e1c7d61SAdrian Hunter {
2886e1c7d61SAdrian Hunter 	if (!sdhci_acpi_byt())
2896e1c7d61SAdrian Hunter 		return false;
2906e1c7d61SAdrian Hunter 
2916e1c7d61SAdrian Hunter 	if (!iosf_mbi_available())
2926e1c7d61SAdrian Hunter 		return true;
2936e1c7d61SAdrian Hunter 
2946e1c7d61SAdrian Hunter 	sdhci_acpi_byt_setting(dev);
2956e1c7d61SAdrian Hunter 
2966e1c7d61SAdrian Hunter 	return false;
2976e1c7d61SAdrian Hunter }
2986e1c7d61SAdrian Hunter 
29917753d16SAdrian Hunter static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
30017753d16SAdrian Hunter 				    unsigned int slot, unsigned int parent_slot)
30117753d16SAdrian Hunter {
30217753d16SAdrian Hunter 	struct pci_dev *dev, *parent, *from = NULL;
30317753d16SAdrian Hunter 
30417753d16SAdrian Hunter 	while (1) {
30517753d16SAdrian Hunter 		dev = pci_get_device(vendor, device, from);
30617753d16SAdrian Hunter 		pci_dev_put(from);
30717753d16SAdrian Hunter 		if (!dev)
30817753d16SAdrian Hunter 			break;
30917753d16SAdrian Hunter 		parent = pci_upstream_bridge(dev);
31017753d16SAdrian Hunter 		if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
31117753d16SAdrian Hunter 		    parent && PCI_SLOT(parent->devfn) == parent_slot &&
31217753d16SAdrian Hunter 		    !pci_upstream_bridge(parent)) {
31317753d16SAdrian Hunter 			pci_dev_put(dev);
31417753d16SAdrian Hunter 			return true;
31517753d16SAdrian Hunter 		}
31617753d16SAdrian Hunter 		from = dev;
31717753d16SAdrian Hunter 	}
31817753d16SAdrian Hunter 
31917753d16SAdrian Hunter 	return false;
32017753d16SAdrian Hunter }
32117753d16SAdrian Hunter 
32217753d16SAdrian Hunter /*
32317753d16SAdrian Hunter  * GPDwin uses PCI wifi which conflicts with SDIO's use of
32417753d16SAdrian Hunter  * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
32517753d16SAdrian Hunter  * problematic, but since SDIO is only used for wifi, the presence of the PCI
32617753d16SAdrian Hunter  * wifi card in the expected slot with an ACPI companion node, is used to
32717753d16SAdrian Hunter  * indicate that acpi_device_fix_up_power() should be avoided.
32817753d16SAdrian Hunter  */
32917753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
33017753d16SAdrian Hunter 						   const char *uid)
33117753d16SAdrian Hunter {
33217753d16SAdrian Hunter 	return sdhci_acpi_cht() &&
33317753d16SAdrian Hunter 	       !strcmp(hid, "80860F14") &&
33417753d16SAdrian Hunter 	       !strcmp(uid, "2") &&
33517753d16SAdrian Hunter 	       sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
33617753d16SAdrian Hunter }
33717753d16SAdrian Hunter 
3386e1c7d61SAdrian Hunter #else
3396e1c7d61SAdrian Hunter 
3406e1c7d61SAdrian Hunter static inline void sdhci_acpi_byt_setting(struct device *dev)
3416e1c7d61SAdrian Hunter {
3426e1c7d61SAdrian Hunter }
3436e1c7d61SAdrian Hunter 
3446e1c7d61SAdrian Hunter static inline bool sdhci_acpi_byt_defer(struct device *dev)
3456e1c7d61SAdrian Hunter {
3466e1c7d61SAdrian Hunter 	return false;
3476e1c7d61SAdrian Hunter }
3486e1c7d61SAdrian Hunter 
34917753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
35017753d16SAdrian Hunter 						   const char *uid)
35117753d16SAdrian Hunter {
35217753d16SAdrian Hunter 	return false;
35317753d16SAdrian Hunter }
35417753d16SAdrian Hunter 
3556e1c7d61SAdrian Hunter #endif
3566e1c7d61SAdrian Hunter 
3576a645dd8SAdrian Hunter static int bxt_get_cd(struct mmc_host *mmc)
3586a645dd8SAdrian Hunter {
3596a645dd8SAdrian Hunter 	int gpio_cd = mmc_gpio_get_cd(mmc);
3606a645dd8SAdrian Hunter 	struct sdhci_host *host = mmc_priv(mmc);
3616a645dd8SAdrian Hunter 	unsigned long flags;
3626a645dd8SAdrian Hunter 	int ret = 0;
3636a645dd8SAdrian Hunter 
3646a645dd8SAdrian Hunter 	if (!gpio_cd)
3656a645dd8SAdrian Hunter 		return 0;
3666a645dd8SAdrian Hunter 
3676a645dd8SAdrian Hunter 	spin_lock_irqsave(&host->lock, flags);
3686a645dd8SAdrian Hunter 
3696a645dd8SAdrian Hunter 	if (host->flags & SDHCI_DEVICE_DEAD)
3706a645dd8SAdrian Hunter 		goto out;
3716a645dd8SAdrian Hunter 
3726a645dd8SAdrian Hunter 	ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
3736a645dd8SAdrian Hunter out:
3746a645dd8SAdrian Hunter 	spin_unlock_irqrestore(&host->lock, flags);
3756a645dd8SAdrian Hunter 
3766a645dd8SAdrian Hunter 	return ret;
3776a645dd8SAdrian Hunter }
3786a645dd8SAdrian Hunter 
379159cd328SAdrian Hunter static int intel_probe_slot(struct platform_device *pdev, const char *hid,
380159cd328SAdrian Hunter 			    const char *uid)
381578b36b6SGao, Yunpeng {
382578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
3831c451c13SAdrian Hunter 	struct intel_host *intel_host = sdhci_acpi_priv(c);
384159cd328SAdrian Hunter 	struct sdhci_host *host = c->host;
385578b36b6SGao, Yunpeng 
3868024379eSAdrian Hunter 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
3878024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
3888024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
3898024379eSAdrian Hunter 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
3908024379eSAdrian Hunter 
391d3e97407SAzhar Shaikh 	if (hid && !strcmp(hid, "80865ACA"))
3926a645dd8SAdrian Hunter 		host->mmc_host_ops.get_cd = bxt_get_cd;
3936a645dd8SAdrian Hunter 
3941c451c13SAdrian Hunter 	intel_dsm_init(intel_host, &pdev->dev, host->mmc);
3951c451c13SAdrian Hunter 
3961c451c13SAdrian Hunter 	host->mmc_host_ops.start_signal_voltage_switch =
3971c451c13SAdrian Hunter 					intel_start_signal_voltage_switch;
3981c451c13SAdrian Hunter 
399578b36b6SGao, Yunpeng 	return 0;
400578b36b6SGao, Yunpeng }
401578b36b6SGao, Yunpeng 
40207a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
403b04fa064SAdrian Hunter 	.chip    = &sdhci_acpi_chip_int,
404f25c3372SMaurice Petallo 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
4059d65cb88SAdrian Hunter 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
406c80f275fSAdrian Hunter 		   MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
40707a58883SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
408e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
409e839b134SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
410e839b134SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC |
411e839b134SAdrian Hunter 		   SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
412159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4131c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
41407a58883SAdrian Hunter };
41507a58883SAdrian Hunter 
416e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
417e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
418e1f5633aSAdrian Hunter 		   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
419e5571397SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
4209d65cb88SAdrian Hunter 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
421265984b3SAdrian Hunter 		   MMC_CAP_WAIT_WHILE_BUSY,
422e5571397SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
423e5571397SAdrian Hunter 	.pm_caps = MMC_PM_KEEP_POWER,
424159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4251c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
426e5571397SAdrian Hunter };
427e5571397SAdrian Hunter 
42807a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
4294fd4409cSAdrian Hunter 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
4304fd4409cSAdrian Hunter 		   SDHCI_ACPI_RUNTIME_PM,
431e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
432934e31b9SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
433934e31b9SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC,
434d3e97407SAzhar Shaikh 	.caps    = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
435159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4361c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
43707a58883SAdrian Hunter };
43807a58883SAdrian Hunter 
43970cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
44070cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
44170cce2afSPhilip Elcan 	.quirks2 = SDHCI_QUIRK2_NO_1_8_V,
44270cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
44370cce2afSPhilip Elcan };
44470cce2afSPhilip Elcan 
44570cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
44670cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
44770cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
44870cce2afSPhilip Elcan };
44970cce2afSPhilip Elcan 
45034597a3fSShah Nehal-Bakulchandra /* AMD sdhci reset dll register. */
45134597a3fSShah Nehal-Bakulchandra #define SDHCI_AMD_RESET_DLL_REGISTER    0x908
45234597a3fSShah Nehal-Bakulchandra 
45334597a3fSShah Nehal-Bakulchandra static int amd_select_drive_strength(struct mmc_card *card,
45434597a3fSShah Nehal-Bakulchandra 				     unsigned int max_dtr, int host_drv,
45534597a3fSShah Nehal-Bakulchandra 				     int card_drv, int *drv_type)
45634597a3fSShah Nehal-Bakulchandra {
45734597a3fSShah Nehal-Bakulchandra 	return MMC_SET_DRIVER_TYPE_A;
45834597a3fSShah Nehal-Bakulchandra }
45934597a3fSShah Nehal-Bakulchandra 
46034597a3fSShah Nehal-Bakulchandra static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host)
46134597a3fSShah Nehal-Bakulchandra {
46234597a3fSShah Nehal-Bakulchandra 	/* AMD Platform requires dll setting */
46334597a3fSShah Nehal-Bakulchandra 	sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
46434597a3fSShah Nehal-Bakulchandra 	usleep_range(10, 20);
46534597a3fSShah Nehal-Bakulchandra 	sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
46634597a3fSShah Nehal-Bakulchandra }
46734597a3fSShah Nehal-Bakulchandra 
46834597a3fSShah Nehal-Bakulchandra /*
46934597a3fSShah Nehal-Bakulchandra  * For AMD Platform it is required to disable the tuning
47034597a3fSShah Nehal-Bakulchandra  * bit first controller to bring to HS Mode from HS200
47134597a3fSShah Nehal-Bakulchandra  * mode, later enable to tune to HS400 mode.
47234597a3fSShah Nehal-Bakulchandra  */
47334597a3fSShah Nehal-Bakulchandra static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
47434597a3fSShah Nehal-Bakulchandra {
47534597a3fSShah Nehal-Bakulchandra 	struct sdhci_host *host = mmc_priv(mmc);
47634597a3fSShah Nehal-Bakulchandra 	unsigned int old_timing = host->timing;
47734597a3fSShah Nehal-Bakulchandra 
47834597a3fSShah Nehal-Bakulchandra 	sdhci_set_ios(mmc, ios);
47934597a3fSShah Nehal-Bakulchandra 	if (old_timing == MMC_TIMING_MMC_HS200 &&
48034597a3fSShah Nehal-Bakulchandra 	    ios->timing == MMC_TIMING_MMC_HS)
48134597a3fSShah Nehal-Bakulchandra 		sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2);
48234597a3fSShah Nehal-Bakulchandra 	if (old_timing != MMC_TIMING_MMC_HS400 &&
48334597a3fSShah Nehal-Bakulchandra 	    ios->timing == MMC_TIMING_MMC_HS400) {
48434597a3fSShah Nehal-Bakulchandra 		sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2);
48534597a3fSShah Nehal-Bakulchandra 		sdhci_acpi_amd_hs400_dll(host);
48634597a3fSShah Nehal-Bakulchandra 	}
48734597a3fSShah Nehal-Bakulchandra }
48834597a3fSShah Nehal-Bakulchandra 
48934597a3fSShah Nehal-Bakulchandra static const struct sdhci_ops sdhci_acpi_ops_amd = {
49034597a3fSShah Nehal-Bakulchandra 	.set_clock	= sdhci_set_clock,
49134597a3fSShah Nehal-Bakulchandra 	.set_bus_width	= sdhci_set_bus_width,
49234597a3fSShah Nehal-Bakulchandra 	.reset		= sdhci_reset,
49334597a3fSShah Nehal-Bakulchandra 	.set_uhs_signaling = sdhci_set_uhs_signaling,
49434597a3fSShah Nehal-Bakulchandra };
49534597a3fSShah Nehal-Bakulchandra 
49634597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
49734597a3fSShah Nehal-Bakulchandra 	.ops = &sdhci_acpi_ops_amd,
49834597a3fSShah Nehal-Bakulchandra };
49934597a3fSShah Nehal-Bakulchandra 
50034597a3fSShah Nehal-Bakulchandra static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
50134597a3fSShah Nehal-Bakulchandra 					  const char *hid, const char *uid)
50234597a3fSShah Nehal-Bakulchandra {
50334597a3fSShah Nehal-Bakulchandra 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
50434597a3fSShah Nehal-Bakulchandra 	struct sdhci_host *host   = c->host;
50534597a3fSShah Nehal-Bakulchandra 
50634597a3fSShah Nehal-Bakulchandra 	sdhci_read_caps(host);
50734597a3fSShah Nehal-Bakulchandra 	if (host->caps1 & SDHCI_SUPPORT_DDR50)
50834597a3fSShah Nehal-Bakulchandra 		host->mmc->caps = MMC_CAP_1_8V_DDR;
50934597a3fSShah Nehal-Bakulchandra 
51034597a3fSShah Nehal-Bakulchandra 	if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
51134597a3fSShah Nehal-Bakulchandra 	    (host->mmc->caps & MMC_CAP_1_8V_DDR))
51234597a3fSShah Nehal-Bakulchandra 		host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
51334597a3fSShah Nehal-Bakulchandra 
51434597a3fSShah Nehal-Bakulchandra 	host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
51534597a3fSShah Nehal-Bakulchandra 	host->mmc_host_ops.set_ios = amd_set_ios;
51634597a3fSShah Nehal-Bakulchandra 	return 0;
51734597a3fSShah Nehal-Bakulchandra }
51834597a3fSShah Nehal-Bakulchandra 
51934597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
52034597a3fSShah Nehal-Bakulchandra 	.chip   = &sdhci_acpi_chip_amd,
52134597a3fSShah Nehal-Bakulchandra 	.caps   = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
52234597a3fSShah Nehal-Bakulchandra 	.quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE |
52334597a3fSShah Nehal-Bakulchandra 			SDHCI_QUIRK_32BIT_ADMA_SIZE,
52434597a3fSShah Nehal-Bakulchandra 	.probe_slot     = sdhci_acpi_emmc_amd_probe_slot,
52534597a3fSShah Nehal-Bakulchandra };
52634597a3fSShah Nehal-Bakulchandra 
52707a58883SAdrian Hunter struct sdhci_acpi_uid_slot {
52807a58883SAdrian Hunter 	const char *hid;
52907a58883SAdrian Hunter 	const char *uid;
53007a58883SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
53107a58883SAdrian Hunter };
53207a58883SAdrian Hunter 
53307a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
534e839b134SAdrian Hunter 	{ "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
535e839b134SAdrian Hunter 	{ "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
536e839b134SAdrian Hunter 	{ "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
53707a58883SAdrian Hunter 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
538db52d4f8SDaniel Drake 	{ "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
53907a58883SAdrian Hunter 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
540aad95dc4SAdrian Hunter 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
54107a58883SAdrian Hunter 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
5427147eaf3SAdrian Hunter 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
54307a58883SAdrian Hunter 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
54407c001c1SMika Westerberg 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
545d0ed8e6bSAdrian Hunter 	{ "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
5460cd2f044SMichele Curti 	{ "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
54707a58883SAdrian Hunter 	{ "PNP0D40"  },
54870cce2afSPhilip Elcan 	{ "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
54970cce2afSPhilip Elcan 	{ "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
55034597a3fSShah Nehal-Bakulchandra 	{ "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
55107a58883SAdrian Hunter 	{ },
55207a58883SAdrian Hunter };
55307a58883SAdrian Hunter 
554c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = {
555e839b134SAdrian Hunter 	{ "80865ACA" },
556e839b134SAdrian Hunter 	{ "80865ACC" },
557e839b134SAdrian Hunter 	{ "80865AD0" },
55807a58883SAdrian Hunter 	{ "80860F14" },
559aad95dc4SAdrian Hunter 	{ "80860F16" },
56007a58883SAdrian Hunter 	{ "INT33BB"  },
56107a58883SAdrian Hunter 	{ "INT33C6"  },
56207c001c1SMika Westerberg 	{ "INT3436"  },
563d0ed8e6bSAdrian Hunter 	{ "INT344D"  },
564c4e05037SAdrian Hunter 	{ "PNP0D40"  },
56570cce2afSPhilip Elcan 	{ "QCOM8051" },
56670cce2afSPhilip Elcan 	{ "QCOM8052" },
56734597a3fSShah Nehal-Bakulchandra 	{ "AMDI0040" },
568c4e05037SAdrian Hunter 	{ },
569c4e05037SAdrian Hunter };
570c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
571c4e05037SAdrian Hunter 
5723db35251SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
57307a58883SAdrian Hunter 							 const char *uid)
574c4e05037SAdrian Hunter {
57507a58883SAdrian Hunter 	const struct sdhci_acpi_uid_slot *u;
576c4e05037SAdrian Hunter 
57707a58883SAdrian Hunter 	for (u = sdhci_acpi_uids; u->hid; u++) {
57807a58883SAdrian Hunter 		if (strcmp(u->hid, hid))
57907a58883SAdrian Hunter 			continue;
58007a58883SAdrian Hunter 		if (!u->uid)
58107a58883SAdrian Hunter 			return u->slot;
58207a58883SAdrian Hunter 		if (uid && !strcmp(u->uid, uid))
58307a58883SAdrian Hunter 			return u->slot;
58407a58883SAdrian Hunter 	}
585c4e05037SAdrian Hunter 	return NULL;
586c4e05037SAdrian Hunter }
587c4e05037SAdrian Hunter 
5884e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev)
589c4e05037SAdrian Hunter {
590c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
591f07b7952SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
592e5bbf307SAdrian Hunter 	struct acpi_device *device, *child;
593c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c;
594c4e05037SAdrian Hunter 	struct sdhci_host *host;
595c4e05037SAdrian Hunter 	struct resource *iomem;
596c4e05037SAdrian Hunter 	resource_size_t len;
597f07b7952SAdrian Hunter 	size_t priv_size;
598c4e05037SAdrian Hunter 	const char *hid;
5993db35251SAdrian Hunter 	const char *uid;
60087875655SMika Westerberg 	int err;
601c4e05037SAdrian Hunter 
602cd25c7beSAndy Shevchenko 	device = ACPI_COMPANION(dev);
603cd25c7beSAndy Shevchenko 	if (!device)
604c4e05037SAdrian Hunter 		return -ENODEV;
605c4e05037SAdrian Hunter 
60617753d16SAdrian Hunter 	hid = acpi_device_hid(device);
607a2038497SAdrian Hunter 	uid = acpi_device_uid(device);
60817753d16SAdrian Hunter 
609f07b7952SAdrian Hunter 	slot = sdhci_acpi_get_slot(hid, uid);
610f07b7952SAdrian Hunter 
611e5bbf307SAdrian Hunter 	/* Power on the SDHCI controller and its children */
612e5bbf307SAdrian Hunter 	acpi_device_fix_up_power(device);
61317753d16SAdrian Hunter 	if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
614e5bbf307SAdrian Hunter 		list_for_each_entry(child, &device->children, node)
615e1d070c3SHans de Goede 			if (child->status.present && child->status.enabled)
616e5bbf307SAdrian Hunter 				acpi_device_fix_up_power(child);
61717753d16SAdrian Hunter 	}
618e5bbf307SAdrian Hunter 
6196e1c7d61SAdrian Hunter 	if (sdhci_acpi_byt_defer(dev))
6206e1c7d61SAdrian Hunter 		return -EPROBE_DEFER;
6216e1c7d61SAdrian Hunter 
622c4e05037SAdrian Hunter 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
623c4e05037SAdrian Hunter 	if (!iomem)
624c4e05037SAdrian Hunter 		return -ENOMEM;
625c4e05037SAdrian Hunter 
626c4e05037SAdrian Hunter 	len = resource_size(iomem);
627c4e05037SAdrian Hunter 	if (len < 0x100)
628c4e05037SAdrian Hunter 		dev_err(dev, "Invalid iomem size!\n");
629c4e05037SAdrian Hunter 
630c4e05037SAdrian Hunter 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
631c4e05037SAdrian Hunter 		return -ENOMEM;
632c4e05037SAdrian Hunter 
633f07b7952SAdrian Hunter 	priv_size = slot ? slot->priv_size : 0;
634f07b7952SAdrian Hunter 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
635c4e05037SAdrian Hunter 	if (IS_ERR(host))
636c4e05037SAdrian Hunter 		return PTR_ERR(host);
637c4e05037SAdrian Hunter 
638c4e05037SAdrian Hunter 	c = sdhci_priv(host);
639c4e05037SAdrian Hunter 	c->host = host;
640f07b7952SAdrian Hunter 	c->slot = slot;
641c4e05037SAdrian Hunter 	c->pdev = pdev;
642c4e05037SAdrian Hunter 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
643c4e05037SAdrian Hunter 
644c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, c);
645c4e05037SAdrian Hunter 
646c4e05037SAdrian Hunter 	host->hw_name	= "ACPI";
647c4e05037SAdrian Hunter 	host->ops	= &sdhci_acpi_ops_dflt;
648c4e05037SAdrian Hunter 	host->irq	= platform_get_irq(pdev, 0);
649c4e05037SAdrian Hunter 
650c4e05037SAdrian Hunter 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
651c4e05037SAdrian Hunter 					    resource_size(iomem));
652c4e05037SAdrian Hunter 	if (host->ioaddr == NULL) {
653c4e05037SAdrian Hunter 		err = -ENOMEM;
654c4e05037SAdrian Hunter 		goto err_free;
655c4e05037SAdrian Hunter 	}
656c4e05037SAdrian Hunter 
657c4e05037SAdrian Hunter 	if (c->slot) {
658578b36b6SGao, Yunpeng 		if (c->slot->probe_slot) {
6597dafca83SAdrian Hunter 			err = c->slot->probe_slot(pdev, hid, uid);
660578b36b6SGao, Yunpeng 			if (err)
661578b36b6SGao, Yunpeng 				goto err_free;
662578b36b6SGao, Yunpeng 		}
663c4e05037SAdrian Hunter 		if (c->slot->chip) {
664c4e05037SAdrian Hunter 			host->ops            = c->slot->chip->ops;
665c4e05037SAdrian Hunter 			host->quirks        |= c->slot->chip->quirks;
666c4e05037SAdrian Hunter 			host->quirks2       |= c->slot->chip->quirks2;
667c4e05037SAdrian Hunter 			host->mmc->caps     |= c->slot->chip->caps;
668c4e05037SAdrian Hunter 			host->mmc->caps2    |= c->slot->chip->caps2;
669c4e05037SAdrian Hunter 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
670c4e05037SAdrian Hunter 		}
671c4e05037SAdrian Hunter 		host->quirks        |= c->slot->quirks;
672c4e05037SAdrian Hunter 		host->quirks2       |= c->slot->quirks2;
673c4e05037SAdrian Hunter 		host->mmc->caps     |= c->slot->caps;
674c4e05037SAdrian Hunter 		host->mmc->caps2    |= c->slot->caps2;
675c4e05037SAdrian Hunter 		host->mmc->pm_caps  |= c->slot->pm_caps;
676c4e05037SAdrian Hunter 	}
677c4e05037SAdrian Hunter 
6780d3e3350SAdrian Hunter 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
6790d3e3350SAdrian Hunter 
6804fd4409cSAdrian Hunter 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
6814fd4409cSAdrian Hunter 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
6824fd4409cSAdrian Hunter 
683e28d6f04SZhang Rui 		err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
684e28d6f04SZhang Rui 		if (err) {
685e28d6f04SZhang Rui 			if (err == -EPROBE_DEFER)
686e28d6f04SZhang Rui 				goto err_free;
6874fd4409cSAdrian Hunter 			dev_warn(dev, "failed to setup card detect gpio\n");
6884fd4409cSAdrian Hunter 			c->use_runtime_pm = false;
6894fd4409cSAdrian Hunter 		}
6904fd4409cSAdrian Hunter 	}
6914fd4409cSAdrian Hunter 
6920cc1a0f4SAdrian Hunter 	err = sdhci_setup_host(host);
693c4e05037SAdrian Hunter 	if (err)
694c4e05037SAdrian Hunter 		goto err_free;
695c4e05037SAdrian Hunter 
6960cc1a0f4SAdrian Hunter 	if (c->slot && c->slot->setup_host) {
6970cc1a0f4SAdrian Hunter 		err = c->slot->setup_host(pdev);
6980cc1a0f4SAdrian Hunter 		if (err)
6990cc1a0f4SAdrian Hunter 			goto err_cleanup;
7000cc1a0f4SAdrian Hunter 	}
7010cc1a0f4SAdrian Hunter 
7020cc1a0f4SAdrian Hunter 	err = __sdhci_add_host(host);
7030cc1a0f4SAdrian Hunter 	if (err)
7040cc1a0f4SAdrian Hunter 		goto err_cleanup;
7050cc1a0f4SAdrian Hunter 
706c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
7071d1ff458SAdrian Hunter 		pm_runtime_set_active(dev);
708c4e05037SAdrian Hunter 		pm_suspend_ignore_children(dev, 1);
709c4e05037SAdrian Hunter 		pm_runtime_set_autosuspend_delay(dev, 50);
710c4e05037SAdrian Hunter 		pm_runtime_use_autosuspend(dev);
711c4e05037SAdrian Hunter 		pm_runtime_enable(dev);
712c4e05037SAdrian Hunter 	}
713c4e05037SAdrian Hunter 
7144e6a2ef9SFu, Zhonghui 	device_enable_async_suspend(dev);
7154e6a2ef9SFu, Zhonghui 
716c4e05037SAdrian Hunter 	return 0;
717c4e05037SAdrian Hunter 
7180cc1a0f4SAdrian Hunter err_cleanup:
7190cc1a0f4SAdrian Hunter 	sdhci_cleanup_host(c->host);
720c4e05037SAdrian Hunter err_free:
721c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
722c4e05037SAdrian Hunter 	return err;
723c4e05037SAdrian Hunter }
724c4e05037SAdrian Hunter 
7254e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev)
726c4e05037SAdrian Hunter {
727c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
728c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
729c4e05037SAdrian Hunter 	int dead;
730c4e05037SAdrian Hunter 
731c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
732c4e05037SAdrian Hunter 		pm_runtime_get_sync(dev);
733c4e05037SAdrian Hunter 		pm_runtime_disable(dev);
734c4e05037SAdrian Hunter 		pm_runtime_put_noidle(dev);
735c4e05037SAdrian Hunter 	}
736c4e05037SAdrian Hunter 
737578b36b6SGao, Yunpeng 	if (c->slot && c->slot->remove_slot)
738578b36b6SGao, Yunpeng 		c->slot->remove_slot(pdev);
739578b36b6SGao, Yunpeng 
740c4e05037SAdrian Hunter 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
741c4e05037SAdrian Hunter 	sdhci_remove_host(c->host, dead);
742c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
743c4e05037SAdrian Hunter 
744c4e05037SAdrian Hunter 	return 0;
745c4e05037SAdrian Hunter }
746c4e05037SAdrian Hunter 
747c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP
748c4e05037SAdrian Hunter 
749c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev)
750c4e05037SAdrian Hunter {
751c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
752d38dcad4SAdrian Hunter 	struct sdhci_host *host = c->host;
753c4e05037SAdrian Hunter 
754d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
755d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
756d38dcad4SAdrian Hunter 
757d38dcad4SAdrian Hunter 	return sdhci_suspend_host(host);
758c4e05037SAdrian Hunter }
759c4e05037SAdrian Hunter 
760c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev)
761c4e05037SAdrian Hunter {
762c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
763c4e05037SAdrian Hunter 
7646e1c7d61SAdrian Hunter 	sdhci_acpi_byt_setting(&c->pdev->dev);
7656e1c7d61SAdrian Hunter 
766c4e05037SAdrian Hunter 	return sdhci_resume_host(c->host);
767c4e05037SAdrian Hunter }
768c4e05037SAdrian Hunter 
769c4e05037SAdrian Hunter #endif
770c4e05037SAdrian Hunter 
771162d6f98SRafael J. Wysocki #ifdef CONFIG_PM
772c4e05037SAdrian Hunter 
773c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev)
774c4e05037SAdrian Hunter {
775c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
776d38dcad4SAdrian Hunter 	struct sdhci_host *host = c->host;
777c4e05037SAdrian Hunter 
778d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
779d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
780d38dcad4SAdrian Hunter 
781d38dcad4SAdrian Hunter 	return sdhci_runtime_suspend_host(host);
782c4e05037SAdrian Hunter }
783c4e05037SAdrian Hunter 
784c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev)
785c4e05037SAdrian Hunter {
786c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
787c4e05037SAdrian Hunter 
7886e1c7d61SAdrian Hunter 	sdhci_acpi_byt_setting(&c->pdev->dev);
7896e1c7d61SAdrian Hunter 
790c4e05037SAdrian Hunter 	return sdhci_runtime_resume_host(c->host);
791c4e05037SAdrian Hunter }
792c4e05037SAdrian Hunter 
793c4e05037SAdrian Hunter #endif
794c4e05037SAdrian Hunter 
795c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = {
796dafed447SUlf Hansson 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
7971d75f74bSPeter Griffin 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
7989b449e99SUlf Hansson 			sdhci_acpi_runtime_resume, NULL)
799c4e05037SAdrian Hunter };
800c4e05037SAdrian Hunter 
801c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = {
802c4e05037SAdrian Hunter 	.driver = {
803c4e05037SAdrian Hunter 		.name			= "sdhci-acpi",
804c4e05037SAdrian Hunter 		.acpi_match_table	= sdhci_acpi_ids,
805c4e05037SAdrian Hunter 		.pm			= &sdhci_acpi_pm_ops,
806c4e05037SAdrian Hunter 	},
807c4e05037SAdrian Hunter 	.probe	= sdhci_acpi_probe,
8084e608e4eSGreg Kroah-Hartman 	.remove	= sdhci_acpi_remove,
809c4e05037SAdrian Hunter };
810c4e05037SAdrian Hunter 
811c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver);
812c4e05037SAdrian Hunter 
813c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
814c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter");
815c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2");
816