xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision 34597a3f)
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 *);
79c4e05037SAdrian Hunter };
80c4e05037SAdrian Hunter 
81c4e05037SAdrian Hunter struct sdhci_acpi_host {
82c4e05037SAdrian Hunter 	struct sdhci_host		*host;
83c4e05037SAdrian Hunter 	const struct sdhci_acpi_slot	*slot;
84c4e05037SAdrian Hunter 	struct platform_device		*pdev;
85c4e05037SAdrian Hunter 	bool				use_runtime_pm;
86f07b7952SAdrian Hunter 	unsigned long			private[0] ____cacheline_aligned;
87c4e05037SAdrian Hunter };
88c4e05037SAdrian Hunter 
89f07b7952SAdrian Hunter static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
90f07b7952SAdrian Hunter {
91f07b7952SAdrian Hunter 	return (void *)c->private;
92f07b7952SAdrian Hunter }
93f07b7952SAdrian Hunter 
94c4e05037SAdrian Hunter static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
95c4e05037SAdrian Hunter {
96c4e05037SAdrian Hunter 	return c->slot && (c->slot->flags & flag);
97c4e05037SAdrian Hunter }
98c4e05037SAdrian Hunter 
991c451c13SAdrian Hunter enum {
1001c451c13SAdrian Hunter 	INTEL_DSM_FNS		=  0,
1011c451c13SAdrian Hunter 	INTEL_DSM_V18_SWITCH	=  3,
1021c451c13SAdrian Hunter 	INTEL_DSM_V33_SWITCH	=  4,
1031c451c13SAdrian Hunter };
1041c451c13SAdrian Hunter 
1051c451c13SAdrian Hunter struct intel_host {
1061c451c13SAdrian Hunter 	u32	dsm_fns;
1071c451c13SAdrian Hunter };
1081c451c13SAdrian Hunter 
1091c451c13SAdrian Hunter static const guid_t intel_dsm_guid =
1101c451c13SAdrian Hunter 	GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
1111c451c13SAdrian Hunter 		  0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
1121c451c13SAdrian Hunter 
1131c451c13SAdrian Hunter static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
1141c451c13SAdrian Hunter 		       unsigned int fn, u32 *result)
1151c451c13SAdrian Hunter {
1161c451c13SAdrian Hunter 	union acpi_object *obj;
1171c451c13SAdrian Hunter 	int err = 0;
1181c451c13SAdrian Hunter 
1191c451c13SAdrian Hunter 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
1201c451c13SAdrian Hunter 	if (!obj)
1211c451c13SAdrian Hunter 		return -EOPNOTSUPP;
1221c451c13SAdrian Hunter 
1231c451c13SAdrian Hunter 	if (obj->type == ACPI_TYPE_INTEGER) {
1241c451c13SAdrian Hunter 		*result = obj->integer.value;
1251c451c13SAdrian Hunter 	} else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
1261c451c13SAdrian Hunter 		size_t len = min_t(size_t, obj->buffer.length, 4);
1271c451c13SAdrian Hunter 
1281c451c13SAdrian Hunter 		*result = 0;
1291c451c13SAdrian Hunter 		memcpy(result, obj->buffer.pointer, len);
1301c451c13SAdrian Hunter 	} else {
1311c451c13SAdrian Hunter 		dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
1321c451c13SAdrian Hunter 			__func__, fn, obj->type, obj->buffer.length);
1331c451c13SAdrian Hunter 		err = -EINVAL;
1341c451c13SAdrian Hunter 	}
1351c451c13SAdrian Hunter 
1361c451c13SAdrian Hunter 	ACPI_FREE(obj);
1371c451c13SAdrian Hunter 
1381c451c13SAdrian Hunter 	return err;
1391c451c13SAdrian Hunter }
1401c451c13SAdrian Hunter 
1411c451c13SAdrian Hunter static int intel_dsm(struct intel_host *intel_host, struct device *dev,
1421c451c13SAdrian Hunter 		     unsigned int fn, u32 *result)
1431c451c13SAdrian Hunter {
1441c451c13SAdrian Hunter 	if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
1451c451c13SAdrian Hunter 		return -EOPNOTSUPP;
1461c451c13SAdrian Hunter 
1471c451c13SAdrian Hunter 	return __intel_dsm(intel_host, dev, fn, result);
1481c451c13SAdrian Hunter }
1491c451c13SAdrian Hunter 
1501c451c13SAdrian Hunter static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
1511c451c13SAdrian Hunter 			   struct mmc_host *mmc)
1521c451c13SAdrian Hunter {
1531c451c13SAdrian Hunter 	int err;
1541c451c13SAdrian Hunter 
1551c451c13SAdrian Hunter 	err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
1561c451c13SAdrian Hunter 	if (err) {
1571c451c13SAdrian Hunter 		pr_debug("%s: DSM not supported, error %d\n",
1581c451c13SAdrian Hunter 			 mmc_hostname(mmc), err);
1591c451c13SAdrian Hunter 		return;
1601c451c13SAdrian Hunter 	}
1611c451c13SAdrian Hunter 
1621c451c13SAdrian Hunter 	pr_debug("%s: DSM function mask %#x\n",
1631c451c13SAdrian Hunter 		 mmc_hostname(mmc), intel_host->dsm_fns);
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[] = {
2378ba4cb53SDave Hansen 		{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
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  */
32817753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
32917753d16SAdrian Hunter 						   const char *uid)
33017753d16SAdrian Hunter {
33117753d16SAdrian Hunter 	return sdhci_acpi_cht() &&
33217753d16SAdrian Hunter 	       !strcmp(hid, "80860F14") &&
33317753d16SAdrian Hunter 	       !strcmp(uid, "2") &&
33417753d16SAdrian Hunter 	       sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
33517753d16SAdrian Hunter }
33617753d16SAdrian Hunter 
3376e1c7d61SAdrian Hunter #else
3386e1c7d61SAdrian Hunter 
3396e1c7d61SAdrian Hunter static inline void sdhci_acpi_byt_setting(struct device *dev)
3406e1c7d61SAdrian Hunter {
3416e1c7d61SAdrian Hunter }
3426e1c7d61SAdrian Hunter 
3436e1c7d61SAdrian Hunter static inline bool sdhci_acpi_byt_defer(struct device *dev)
3446e1c7d61SAdrian Hunter {
3456e1c7d61SAdrian Hunter 	return false;
3466e1c7d61SAdrian Hunter }
3476e1c7d61SAdrian Hunter 
34817753d16SAdrian Hunter static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
34917753d16SAdrian Hunter 						   const char *uid)
35017753d16SAdrian Hunter {
35117753d16SAdrian Hunter 	return false;
35217753d16SAdrian Hunter }
35317753d16SAdrian Hunter 
3546e1c7d61SAdrian Hunter #endif
3556e1c7d61SAdrian Hunter 
3566a645dd8SAdrian Hunter static int bxt_get_cd(struct mmc_host *mmc)
3576a645dd8SAdrian Hunter {
3586a645dd8SAdrian Hunter 	int gpio_cd = mmc_gpio_get_cd(mmc);
3596a645dd8SAdrian Hunter 	struct sdhci_host *host = mmc_priv(mmc);
3606a645dd8SAdrian Hunter 	unsigned long flags;
3616a645dd8SAdrian Hunter 	int ret = 0;
3626a645dd8SAdrian Hunter 
3636a645dd8SAdrian Hunter 	if (!gpio_cd)
3646a645dd8SAdrian Hunter 		return 0;
3656a645dd8SAdrian Hunter 
3666a645dd8SAdrian Hunter 	spin_lock_irqsave(&host->lock, flags);
3676a645dd8SAdrian Hunter 
3686a645dd8SAdrian Hunter 	if (host->flags & SDHCI_DEVICE_DEAD)
3696a645dd8SAdrian Hunter 		goto out;
3706a645dd8SAdrian Hunter 
3716a645dd8SAdrian Hunter 	ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
3726a645dd8SAdrian Hunter out:
3736a645dd8SAdrian Hunter 	spin_unlock_irqrestore(&host->lock, flags);
3746a645dd8SAdrian Hunter 
3756a645dd8SAdrian Hunter 	return ret;
3766a645dd8SAdrian Hunter }
3776a645dd8SAdrian Hunter 
378159cd328SAdrian Hunter static int intel_probe_slot(struct platform_device *pdev, const char *hid,
379159cd328SAdrian Hunter 			    const char *uid)
380578b36b6SGao, Yunpeng {
381578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
3821c451c13SAdrian Hunter 	struct intel_host *intel_host = sdhci_acpi_priv(c);
383159cd328SAdrian Hunter 	struct sdhci_host *host = c->host;
384578b36b6SGao, Yunpeng 
3858024379eSAdrian Hunter 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
3868024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
3878024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
3888024379eSAdrian Hunter 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
3898024379eSAdrian Hunter 
390d3e97407SAzhar Shaikh 	if (hid && !strcmp(hid, "80865ACA"))
3916a645dd8SAdrian Hunter 		host->mmc_host_ops.get_cd = bxt_get_cd;
3926a645dd8SAdrian Hunter 
3931c451c13SAdrian Hunter 	intel_dsm_init(intel_host, &pdev->dev, host->mmc);
3941c451c13SAdrian Hunter 
3951c451c13SAdrian Hunter 	host->mmc_host_ops.start_signal_voltage_switch =
3961c451c13SAdrian Hunter 					intel_start_signal_voltage_switch;
3971c451c13SAdrian Hunter 
398578b36b6SGao, Yunpeng 	return 0;
399578b36b6SGao, Yunpeng }
400578b36b6SGao, Yunpeng 
40107a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
402b04fa064SAdrian Hunter 	.chip    = &sdhci_acpi_chip_int,
403f25c3372SMaurice Petallo 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
4049d65cb88SAdrian Hunter 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
405c80f275fSAdrian Hunter 		   MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
40607a58883SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
407e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
408e839b134SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
409e839b134SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC |
410e839b134SAdrian Hunter 		   SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
411159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4121c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
41307a58883SAdrian Hunter };
41407a58883SAdrian Hunter 
415e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
416e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
417e1f5633aSAdrian Hunter 		   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
418e5571397SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
4199d65cb88SAdrian Hunter 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
420265984b3SAdrian Hunter 		   MMC_CAP_WAIT_WHILE_BUSY,
421e5571397SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
422e5571397SAdrian Hunter 	.pm_caps = MMC_PM_KEEP_POWER,
423159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4241c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
425e5571397SAdrian Hunter };
426e5571397SAdrian Hunter 
42707a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
4284fd4409cSAdrian Hunter 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
4294fd4409cSAdrian Hunter 		   SDHCI_ACPI_RUNTIME_PM,
430e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
431934e31b9SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
432934e31b9SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC,
433d3e97407SAzhar Shaikh 	.caps    = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
434159cd328SAdrian Hunter 	.probe_slot	= intel_probe_slot,
4351c451c13SAdrian Hunter 	.priv_size	= sizeof(struct intel_host),
43607a58883SAdrian Hunter };
43707a58883SAdrian Hunter 
43870cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
43970cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
44070cce2afSPhilip Elcan 	.quirks2 = SDHCI_QUIRK2_NO_1_8_V,
44170cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
44270cce2afSPhilip Elcan };
44370cce2afSPhilip Elcan 
44470cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
44570cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
44670cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
44770cce2afSPhilip Elcan };
44870cce2afSPhilip Elcan 
44934597a3fSShah Nehal-Bakulchandra /* AMD sdhci reset dll register. */
45034597a3fSShah Nehal-Bakulchandra #define SDHCI_AMD_RESET_DLL_REGISTER    0x908
45134597a3fSShah Nehal-Bakulchandra 
45234597a3fSShah Nehal-Bakulchandra static int amd_select_drive_strength(struct mmc_card *card,
45334597a3fSShah Nehal-Bakulchandra 				     unsigned int max_dtr, int host_drv,
45434597a3fSShah Nehal-Bakulchandra 				     int card_drv, int *drv_type)
45534597a3fSShah Nehal-Bakulchandra {
45634597a3fSShah Nehal-Bakulchandra 	return MMC_SET_DRIVER_TYPE_A;
45734597a3fSShah Nehal-Bakulchandra }
45834597a3fSShah Nehal-Bakulchandra 
45934597a3fSShah Nehal-Bakulchandra static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host)
46034597a3fSShah Nehal-Bakulchandra {
46134597a3fSShah Nehal-Bakulchandra 	/* AMD Platform requires dll setting */
46234597a3fSShah Nehal-Bakulchandra 	sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
46334597a3fSShah Nehal-Bakulchandra 	usleep_range(10, 20);
46434597a3fSShah Nehal-Bakulchandra 	sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
46534597a3fSShah Nehal-Bakulchandra }
46634597a3fSShah Nehal-Bakulchandra 
46734597a3fSShah Nehal-Bakulchandra /*
46834597a3fSShah Nehal-Bakulchandra  * For AMD Platform it is required to disable the tuning
46934597a3fSShah Nehal-Bakulchandra  * bit first controller to bring to HS Mode from HS200
47034597a3fSShah Nehal-Bakulchandra  * mode, later enable to tune to HS400 mode.
47134597a3fSShah Nehal-Bakulchandra  */
47234597a3fSShah Nehal-Bakulchandra static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
47334597a3fSShah Nehal-Bakulchandra {
47434597a3fSShah Nehal-Bakulchandra 	struct sdhci_host *host = mmc_priv(mmc);
47534597a3fSShah Nehal-Bakulchandra 	unsigned int old_timing = host->timing;
47634597a3fSShah Nehal-Bakulchandra 
47734597a3fSShah Nehal-Bakulchandra 	sdhci_set_ios(mmc, ios);
47834597a3fSShah Nehal-Bakulchandra 	if (old_timing == MMC_TIMING_MMC_HS200 &&
47934597a3fSShah Nehal-Bakulchandra 	    ios->timing == MMC_TIMING_MMC_HS)
48034597a3fSShah Nehal-Bakulchandra 		sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2);
48134597a3fSShah Nehal-Bakulchandra 	if (old_timing != MMC_TIMING_MMC_HS400 &&
48234597a3fSShah Nehal-Bakulchandra 	    ios->timing == MMC_TIMING_MMC_HS400) {
48334597a3fSShah Nehal-Bakulchandra 		sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2);
48434597a3fSShah Nehal-Bakulchandra 		sdhci_acpi_amd_hs400_dll(host);
48534597a3fSShah Nehal-Bakulchandra 	}
48634597a3fSShah Nehal-Bakulchandra }
48734597a3fSShah Nehal-Bakulchandra 
48834597a3fSShah Nehal-Bakulchandra static const struct sdhci_ops sdhci_acpi_ops_amd = {
48934597a3fSShah Nehal-Bakulchandra 	.set_clock	= sdhci_set_clock,
49034597a3fSShah Nehal-Bakulchandra 	.set_bus_width	= sdhci_set_bus_width,
49134597a3fSShah Nehal-Bakulchandra 	.reset		= sdhci_reset,
49234597a3fSShah Nehal-Bakulchandra 	.set_uhs_signaling = sdhci_set_uhs_signaling,
49334597a3fSShah Nehal-Bakulchandra };
49434597a3fSShah Nehal-Bakulchandra 
49534597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
49634597a3fSShah Nehal-Bakulchandra 	.ops = &sdhci_acpi_ops_amd,
49734597a3fSShah Nehal-Bakulchandra };
49834597a3fSShah Nehal-Bakulchandra 
49934597a3fSShah Nehal-Bakulchandra static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
50034597a3fSShah Nehal-Bakulchandra 					  const char *hid, const char *uid)
50134597a3fSShah Nehal-Bakulchandra {
50234597a3fSShah Nehal-Bakulchandra 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
50334597a3fSShah Nehal-Bakulchandra 	struct sdhci_host *host   = c->host;
50434597a3fSShah Nehal-Bakulchandra 
50534597a3fSShah Nehal-Bakulchandra 	sdhci_read_caps(host);
50634597a3fSShah Nehal-Bakulchandra 	if (host->caps1 & SDHCI_SUPPORT_DDR50)
50734597a3fSShah Nehal-Bakulchandra 		host->mmc->caps = MMC_CAP_1_8V_DDR;
50834597a3fSShah Nehal-Bakulchandra 
50934597a3fSShah Nehal-Bakulchandra 	if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
51034597a3fSShah Nehal-Bakulchandra 	    (host->mmc->caps & MMC_CAP_1_8V_DDR))
51134597a3fSShah Nehal-Bakulchandra 		host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
51234597a3fSShah Nehal-Bakulchandra 
51334597a3fSShah Nehal-Bakulchandra 	host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
51434597a3fSShah Nehal-Bakulchandra 	host->mmc_host_ops.set_ios = amd_set_ios;
51534597a3fSShah Nehal-Bakulchandra 	return 0;
51634597a3fSShah Nehal-Bakulchandra }
51734597a3fSShah Nehal-Bakulchandra 
51834597a3fSShah Nehal-Bakulchandra static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
51934597a3fSShah Nehal-Bakulchandra 	.chip   = &sdhci_acpi_chip_amd,
52034597a3fSShah Nehal-Bakulchandra 	.caps   = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
52134597a3fSShah Nehal-Bakulchandra 	.quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE |
52234597a3fSShah Nehal-Bakulchandra 			SDHCI_QUIRK_32BIT_ADMA_SIZE,
52334597a3fSShah Nehal-Bakulchandra 	.probe_slot     = sdhci_acpi_emmc_amd_probe_slot,
52434597a3fSShah Nehal-Bakulchandra };
52534597a3fSShah Nehal-Bakulchandra 
52607a58883SAdrian Hunter struct sdhci_acpi_uid_slot {
52707a58883SAdrian Hunter 	const char *hid;
52807a58883SAdrian Hunter 	const char *uid;
52907a58883SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
53007a58883SAdrian Hunter };
53107a58883SAdrian Hunter 
53207a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
533e839b134SAdrian Hunter 	{ "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
534e839b134SAdrian Hunter 	{ "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
535e839b134SAdrian Hunter 	{ "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
53607a58883SAdrian Hunter 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
537db52d4f8SDaniel Drake 	{ "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
53807a58883SAdrian Hunter 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
539aad95dc4SAdrian Hunter 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
54007a58883SAdrian Hunter 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
5417147eaf3SAdrian Hunter 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
54207a58883SAdrian Hunter 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
54307c001c1SMika Westerberg 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
544d0ed8e6bSAdrian Hunter 	{ "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
5450cd2f044SMichele Curti 	{ "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
54607a58883SAdrian Hunter 	{ "PNP0D40"  },
54770cce2afSPhilip Elcan 	{ "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
54870cce2afSPhilip Elcan 	{ "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
54934597a3fSShah Nehal-Bakulchandra 	{ "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
55007a58883SAdrian Hunter 	{ },
55107a58883SAdrian Hunter };
55207a58883SAdrian Hunter 
553c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = {
554e839b134SAdrian Hunter 	{ "80865ACA" },
555e839b134SAdrian Hunter 	{ "80865ACC" },
556e839b134SAdrian Hunter 	{ "80865AD0" },
55707a58883SAdrian Hunter 	{ "80860F14" },
558aad95dc4SAdrian Hunter 	{ "80860F16" },
55907a58883SAdrian Hunter 	{ "INT33BB"  },
56007a58883SAdrian Hunter 	{ "INT33C6"  },
56107c001c1SMika Westerberg 	{ "INT3436"  },
562d0ed8e6bSAdrian Hunter 	{ "INT344D"  },
563c4e05037SAdrian Hunter 	{ "PNP0D40"  },
56470cce2afSPhilip Elcan 	{ "QCOM8051" },
56570cce2afSPhilip Elcan 	{ "QCOM8052" },
56634597a3fSShah Nehal-Bakulchandra 	{ "AMDI0040" },
567c4e05037SAdrian Hunter 	{ },
568c4e05037SAdrian Hunter };
569c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
570c4e05037SAdrian Hunter 
5713db35251SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
57207a58883SAdrian Hunter 							 const char *uid)
573c4e05037SAdrian Hunter {
57407a58883SAdrian Hunter 	const struct sdhci_acpi_uid_slot *u;
575c4e05037SAdrian Hunter 
57607a58883SAdrian Hunter 	for (u = sdhci_acpi_uids; u->hid; u++) {
57707a58883SAdrian Hunter 		if (strcmp(u->hid, hid))
57807a58883SAdrian Hunter 			continue;
57907a58883SAdrian Hunter 		if (!u->uid)
58007a58883SAdrian Hunter 			return u->slot;
58107a58883SAdrian Hunter 		if (uid && !strcmp(u->uid, uid))
58207a58883SAdrian Hunter 			return u->slot;
58307a58883SAdrian Hunter 	}
584c4e05037SAdrian Hunter 	return NULL;
585c4e05037SAdrian Hunter }
586c4e05037SAdrian Hunter 
5874e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev)
588c4e05037SAdrian Hunter {
589c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
590f07b7952SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
591e5bbf307SAdrian Hunter 	struct acpi_device *device, *child;
592c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c;
593c4e05037SAdrian Hunter 	struct sdhci_host *host;
594c4e05037SAdrian Hunter 	struct resource *iomem;
595c4e05037SAdrian Hunter 	resource_size_t len;
596f07b7952SAdrian Hunter 	size_t priv_size;
597c4e05037SAdrian Hunter 	const char *hid;
5983db35251SAdrian Hunter 	const char *uid;
59987875655SMika Westerberg 	int err;
600c4e05037SAdrian Hunter 
601cd25c7beSAndy Shevchenko 	device = ACPI_COMPANION(dev);
602cd25c7beSAndy Shevchenko 	if (!device)
603c4e05037SAdrian Hunter 		return -ENODEV;
604c4e05037SAdrian Hunter 
60517753d16SAdrian Hunter 	hid = acpi_device_hid(device);
606a2038497SAdrian Hunter 	uid = acpi_device_uid(device);
60717753d16SAdrian Hunter 
608f07b7952SAdrian Hunter 	slot = sdhci_acpi_get_slot(hid, uid);
609f07b7952SAdrian Hunter 
610e5bbf307SAdrian Hunter 	/* Power on the SDHCI controller and its children */
611e5bbf307SAdrian Hunter 	acpi_device_fix_up_power(device);
61217753d16SAdrian Hunter 	if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
613e5bbf307SAdrian Hunter 		list_for_each_entry(child, &device->children, node)
614e1d070c3SHans de Goede 			if (child->status.present && child->status.enabled)
615e5bbf307SAdrian Hunter 				acpi_device_fix_up_power(child);
61617753d16SAdrian Hunter 	}
617e5bbf307SAdrian Hunter 
6186e1c7d61SAdrian Hunter 	if (sdhci_acpi_byt_defer(dev))
6196e1c7d61SAdrian Hunter 		return -EPROBE_DEFER;
6206e1c7d61SAdrian Hunter 
621c4e05037SAdrian Hunter 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
622c4e05037SAdrian Hunter 	if (!iomem)
623c4e05037SAdrian Hunter 		return -ENOMEM;
624c4e05037SAdrian Hunter 
625c4e05037SAdrian Hunter 	len = resource_size(iomem);
626c4e05037SAdrian Hunter 	if (len < 0x100)
627c4e05037SAdrian Hunter 		dev_err(dev, "Invalid iomem size!\n");
628c4e05037SAdrian Hunter 
629c4e05037SAdrian Hunter 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
630c4e05037SAdrian Hunter 		return -ENOMEM;
631c4e05037SAdrian Hunter 
632f07b7952SAdrian Hunter 	priv_size = slot ? slot->priv_size : 0;
633f07b7952SAdrian Hunter 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
634c4e05037SAdrian Hunter 	if (IS_ERR(host))
635c4e05037SAdrian Hunter 		return PTR_ERR(host);
636c4e05037SAdrian Hunter 
637c4e05037SAdrian Hunter 	c = sdhci_priv(host);
638c4e05037SAdrian Hunter 	c->host = host;
639f07b7952SAdrian Hunter 	c->slot = slot;
640c4e05037SAdrian Hunter 	c->pdev = pdev;
641c4e05037SAdrian Hunter 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
642c4e05037SAdrian Hunter 
643c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, c);
644c4e05037SAdrian Hunter 
645c4e05037SAdrian Hunter 	host->hw_name	= "ACPI";
646c4e05037SAdrian Hunter 	host->ops	= &sdhci_acpi_ops_dflt;
647c4e05037SAdrian Hunter 	host->irq	= platform_get_irq(pdev, 0);
648c4e05037SAdrian Hunter 
649c4e05037SAdrian Hunter 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
650c4e05037SAdrian Hunter 					    resource_size(iomem));
651c4e05037SAdrian Hunter 	if (host->ioaddr == NULL) {
652c4e05037SAdrian Hunter 		err = -ENOMEM;
653c4e05037SAdrian Hunter 		goto err_free;
654c4e05037SAdrian Hunter 	}
655c4e05037SAdrian Hunter 
656c4e05037SAdrian Hunter 	if (c->slot) {
657578b36b6SGao, Yunpeng 		if (c->slot->probe_slot) {
6587dafca83SAdrian Hunter 			err = c->slot->probe_slot(pdev, hid, uid);
659578b36b6SGao, Yunpeng 			if (err)
660578b36b6SGao, Yunpeng 				goto err_free;
661578b36b6SGao, Yunpeng 		}
662c4e05037SAdrian Hunter 		if (c->slot->chip) {
663c4e05037SAdrian Hunter 			host->ops            = c->slot->chip->ops;
664c4e05037SAdrian Hunter 			host->quirks        |= c->slot->chip->quirks;
665c4e05037SAdrian Hunter 			host->quirks2       |= c->slot->chip->quirks2;
666c4e05037SAdrian Hunter 			host->mmc->caps     |= c->slot->chip->caps;
667c4e05037SAdrian Hunter 			host->mmc->caps2    |= c->slot->chip->caps2;
668c4e05037SAdrian Hunter 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
669c4e05037SAdrian Hunter 		}
670c4e05037SAdrian Hunter 		host->quirks        |= c->slot->quirks;
671c4e05037SAdrian Hunter 		host->quirks2       |= c->slot->quirks2;
672c4e05037SAdrian Hunter 		host->mmc->caps     |= c->slot->caps;
673c4e05037SAdrian Hunter 		host->mmc->caps2    |= c->slot->caps2;
674c4e05037SAdrian Hunter 		host->mmc->pm_caps  |= c->slot->pm_caps;
675c4e05037SAdrian Hunter 	}
676c4e05037SAdrian Hunter 
6770d3e3350SAdrian Hunter 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
6780d3e3350SAdrian Hunter 
6794fd4409cSAdrian Hunter 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
6804fd4409cSAdrian Hunter 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
6814fd4409cSAdrian Hunter 
682e28d6f04SZhang Rui 		err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
683e28d6f04SZhang Rui 		if (err) {
684e28d6f04SZhang Rui 			if (err == -EPROBE_DEFER)
685e28d6f04SZhang Rui 				goto err_free;
6864fd4409cSAdrian Hunter 			dev_warn(dev, "failed to setup card detect gpio\n");
6874fd4409cSAdrian Hunter 			c->use_runtime_pm = false;
6884fd4409cSAdrian Hunter 		}
6894fd4409cSAdrian Hunter 	}
6904fd4409cSAdrian Hunter 
691c4e05037SAdrian Hunter 	err = sdhci_add_host(host);
692c4e05037SAdrian Hunter 	if (err)
693c4e05037SAdrian Hunter 		goto err_free;
694c4e05037SAdrian Hunter 
695c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
6961d1ff458SAdrian Hunter 		pm_runtime_set_active(dev);
697c4e05037SAdrian Hunter 		pm_suspend_ignore_children(dev, 1);
698c4e05037SAdrian Hunter 		pm_runtime_set_autosuspend_delay(dev, 50);
699c4e05037SAdrian Hunter 		pm_runtime_use_autosuspend(dev);
700c4e05037SAdrian Hunter 		pm_runtime_enable(dev);
701c4e05037SAdrian Hunter 	}
702c4e05037SAdrian Hunter 
7034e6a2ef9SFu, Zhonghui 	device_enable_async_suspend(dev);
7044e6a2ef9SFu, Zhonghui 
705c4e05037SAdrian Hunter 	return 0;
706c4e05037SAdrian Hunter 
707c4e05037SAdrian Hunter err_free:
708c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
709c4e05037SAdrian Hunter 	return err;
710c4e05037SAdrian Hunter }
711c4e05037SAdrian Hunter 
7124e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev)
713c4e05037SAdrian Hunter {
714c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
715c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
716c4e05037SAdrian Hunter 	int dead;
717c4e05037SAdrian Hunter 
718c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
719c4e05037SAdrian Hunter 		pm_runtime_get_sync(dev);
720c4e05037SAdrian Hunter 		pm_runtime_disable(dev);
721c4e05037SAdrian Hunter 		pm_runtime_put_noidle(dev);
722c4e05037SAdrian Hunter 	}
723c4e05037SAdrian Hunter 
724578b36b6SGao, Yunpeng 	if (c->slot && c->slot->remove_slot)
725578b36b6SGao, Yunpeng 		c->slot->remove_slot(pdev);
726578b36b6SGao, Yunpeng 
727c4e05037SAdrian Hunter 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
728c4e05037SAdrian Hunter 	sdhci_remove_host(c->host, dead);
729c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
730c4e05037SAdrian Hunter 
731c4e05037SAdrian Hunter 	return 0;
732c4e05037SAdrian Hunter }
733c4e05037SAdrian Hunter 
734c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP
735c4e05037SAdrian Hunter 
736c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev)
737c4e05037SAdrian Hunter {
738c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
739d38dcad4SAdrian Hunter 	struct sdhci_host *host = c->host;
740c4e05037SAdrian Hunter 
741d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
742d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
743d38dcad4SAdrian Hunter 
744d38dcad4SAdrian Hunter 	return sdhci_suspend_host(host);
745c4e05037SAdrian Hunter }
746c4e05037SAdrian Hunter 
747c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev)
748c4e05037SAdrian Hunter {
749c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
750c4e05037SAdrian Hunter 
7516e1c7d61SAdrian Hunter 	sdhci_acpi_byt_setting(&c->pdev->dev);
7526e1c7d61SAdrian Hunter 
753c4e05037SAdrian Hunter 	return sdhci_resume_host(c->host);
754c4e05037SAdrian Hunter }
755c4e05037SAdrian Hunter 
756c4e05037SAdrian Hunter #endif
757c4e05037SAdrian Hunter 
758162d6f98SRafael J. Wysocki #ifdef CONFIG_PM
759c4e05037SAdrian Hunter 
760c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev)
761c4e05037SAdrian Hunter {
762c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
763d38dcad4SAdrian Hunter 	struct sdhci_host *host = c->host;
764c4e05037SAdrian Hunter 
765d38dcad4SAdrian Hunter 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
766d38dcad4SAdrian Hunter 		mmc_retune_needed(host->mmc);
767d38dcad4SAdrian Hunter 
768d38dcad4SAdrian Hunter 	return sdhci_runtime_suspend_host(host);
769c4e05037SAdrian Hunter }
770c4e05037SAdrian Hunter 
771c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev)
772c4e05037SAdrian Hunter {
773c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
774c4e05037SAdrian Hunter 
7756e1c7d61SAdrian Hunter 	sdhci_acpi_byt_setting(&c->pdev->dev);
7766e1c7d61SAdrian Hunter 
777c4e05037SAdrian Hunter 	return sdhci_runtime_resume_host(c->host);
778c4e05037SAdrian Hunter }
779c4e05037SAdrian Hunter 
780c4e05037SAdrian Hunter #endif
781c4e05037SAdrian Hunter 
782c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = {
783dafed447SUlf Hansson 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
7841d75f74bSPeter Griffin 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
7859b449e99SUlf Hansson 			sdhci_acpi_runtime_resume, NULL)
786c4e05037SAdrian Hunter };
787c4e05037SAdrian Hunter 
788c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = {
789c4e05037SAdrian Hunter 	.driver = {
790c4e05037SAdrian Hunter 		.name			= "sdhci-acpi",
791c4e05037SAdrian Hunter 		.acpi_match_table	= sdhci_acpi_ids,
792c4e05037SAdrian Hunter 		.pm			= &sdhci_acpi_pm_ops,
793c4e05037SAdrian Hunter 	},
794c4e05037SAdrian Hunter 	.probe	= sdhci_acpi_probe,
7954e608e4eSGreg Kroah-Hartman 	.remove	= sdhci_acpi_remove,
796c4e05037SAdrian Hunter };
797c4e05037SAdrian Hunter 
798c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver);
799c4e05037SAdrian Hunter 
800c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
801c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter");
802c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2");
803