xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision 70cce2af)
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 
44c4e05037SAdrian Hunter #include "sdhci.h"
45c4e05037SAdrian Hunter 
46c4e05037SAdrian Hunter enum {
47c4e05037SAdrian Hunter 	SDHCI_ACPI_SD_CD		= BIT(0),
48c4e05037SAdrian Hunter 	SDHCI_ACPI_RUNTIME_PM		= BIT(1),
494fd4409cSAdrian Hunter 	SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL	= BIT(2),
50c4e05037SAdrian Hunter };
51c4e05037SAdrian Hunter 
52c4e05037SAdrian Hunter struct sdhci_acpi_chip {
53c4e05037SAdrian Hunter 	const struct	sdhci_ops *ops;
54c4e05037SAdrian Hunter 	unsigned int	quirks;
55c4e05037SAdrian Hunter 	unsigned int	quirks2;
56c4e05037SAdrian Hunter 	unsigned long	caps;
57c4e05037SAdrian Hunter 	unsigned int	caps2;
58c4e05037SAdrian Hunter 	mmc_pm_flag_t	pm_caps;
59c4e05037SAdrian Hunter };
60c4e05037SAdrian Hunter 
61c4e05037SAdrian Hunter struct sdhci_acpi_slot {
62c4e05037SAdrian Hunter 	const struct	sdhci_acpi_chip *chip;
63c4e05037SAdrian Hunter 	unsigned int	quirks;
64c4e05037SAdrian Hunter 	unsigned int	quirks2;
65c4e05037SAdrian Hunter 	unsigned long	caps;
66c4e05037SAdrian Hunter 	unsigned int	caps2;
67c4e05037SAdrian Hunter 	mmc_pm_flag_t	pm_caps;
68c4e05037SAdrian Hunter 	unsigned int	flags;
697dafca83SAdrian Hunter 	int (*probe_slot)(struct platform_device *, const char *, const char *);
70578b36b6SGao, Yunpeng 	int (*remove_slot)(struct platform_device *);
71c4e05037SAdrian Hunter };
72c4e05037SAdrian Hunter 
73c4e05037SAdrian Hunter struct sdhci_acpi_host {
74c4e05037SAdrian Hunter 	struct sdhci_host		*host;
75c4e05037SAdrian Hunter 	const struct sdhci_acpi_slot	*slot;
76c4e05037SAdrian Hunter 	struct platform_device		*pdev;
77c4e05037SAdrian Hunter 	bool				use_runtime_pm;
784f64f843SAdrian Hunter 	bool				dma_setup;
79c4e05037SAdrian Hunter };
80c4e05037SAdrian Hunter 
81c4e05037SAdrian Hunter static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
82c4e05037SAdrian Hunter {
83c4e05037SAdrian Hunter 	return c->slot && (c->slot->flags & flag);
84c4e05037SAdrian Hunter }
85c4e05037SAdrian Hunter 
86c4e05037SAdrian Hunter static int sdhci_acpi_enable_dma(struct sdhci_host *host)
87c4e05037SAdrian Hunter {
884f64f843SAdrian Hunter 	struct sdhci_acpi_host *c = sdhci_priv(host);
894f64f843SAdrian Hunter 	struct device *dev = &c->pdev->dev;
904f64f843SAdrian Hunter 	int err = -1;
914f64f843SAdrian Hunter 
924f64f843SAdrian Hunter 	if (c->dma_setup)
93c4e05037SAdrian Hunter 		return 0;
944f64f843SAdrian Hunter 
954f64f843SAdrian Hunter 	if (host->flags & SDHCI_USE_64_BIT_DMA) {
964f64f843SAdrian Hunter 		if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) {
974f64f843SAdrian Hunter 			host->flags &= ~SDHCI_USE_64_BIT_DMA;
984f64f843SAdrian Hunter 		} else {
994f64f843SAdrian Hunter 			err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
1004f64f843SAdrian Hunter 			if (err)
1014f64f843SAdrian Hunter 				dev_warn(dev, "Failed to set 64-bit DMA mask\n");
1024f64f843SAdrian Hunter 		}
1034f64f843SAdrian Hunter 	}
1044f64f843SAdrian Hunter 
1054f64f843SAdrian Hunter 	if (err)
1064f64f843SAdrian Hunter 		err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1074f64f843SAdrian Hunter 
1084f64f843SAdrian Hunter 	c->dma_setup = !err;
1094f64f843SAdrian Hunter 
1104f64f843SAdrian Hunter 	return err;
111c4e05037SAdrian Hunter }
112c4e05037SAdrian Hunter 
113b04fa064SAdrian Hunter static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
114b04fa064SAdrian Hunter {
115b04fa064SAdrian Hunter 	u8 reg;
116b04fa064SAdrian Hunter 
117b04fa064SAdrian Hunter 	reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
118b04fa064SAdrian Hunter 	reg |= 0x10;
119b04fa064SAdrian Hunter 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
120b04fa064SAdrian Hunter 	/* For eMMC, minimum is 1us but give it 9us for good measure */
121b04fa064SAdrian Hunter 	udelay(9);
122b04fa064SAdrian Hunter 	reg &= ~0x10;
123b04fa064SAdrian Hunter 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
124b04fa064SAdrian Hunter 	/* For eMMC, minimum is 200us but give it 300us for good measure */
125b04fa064SAdrian Hunter 	usleep_range(300, 1000);
126b04fa064SAdrian Hunter }
127b04fa064SAdrian Hunter 
128c4e05037SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_dflt = {
1291771059cSRussell King 	.set_clock = sdhci_set_clock,
130c4e05037SAdrian Hunter 	.enable_dma = sdhci_acpi_enable_dma,
1312317f56cSRussell King 	.set_bus_width = sdhci_set_bus_width,
13203231f9bSRussell King 	.reset = sdhci_reset,
13396d7b78cSRussell King 	.set_uhs_signaling = sdhci_set_uhs_signaling,
134c4e05037SAdrian Hunter };
135c4e05037SAdrian Hunter 
136b04fa064SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_int = {
1371771059cSRussell King 	.set_clock = sdhci_set_clock,
138b04fa064SAdrian Hunter 	.enable_dma = sdhci_acpi_enable_dma,
1392317f56cSRussell King 	.set_bus_width = sdhci_set_bus_width,
14003231f9bSRussell King 	.reset = sdhci_reset,
14196d7b78cSRussell King 	.set_uhs_signaling = sdhci_set_uhs_signaling,
142b04fa064SAdrian Hunter 	.hw_reset   = sdhci_acpi_int_hw_reset,
143b04fa064SAdrian Hunter };
144b04fa064SAdrian Hunter 
145b04fa064SAdrian Hunter static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
146b04fa064SAdrian Hunter 	.ops = &sdhci_acpi_ops_int,
147b04fa064SAdrian Hunter };
148b04fa064SAdrian Hunter 
1496a645dd8SAdrian Hunter static int bxt_get_cd(struct mmc_host *mmc)
1506a645dd8SAdrian Hunter {
1516a645dd8SAdrian Hunter 	int gpio_cd = mmc_gpio_get_cd(mmc);
1526a645dd8SAdrian Hunter 	struct sdhci_host *host = mmc_priv(mmc);
1536a645dd8SAdrian Hunter 	unsigned long flags;
1546a645dd8SAdrian Hunter 	int ret = 0;
1556a645dd8SAdrian Hunter 
1566a645dd8SAdrian Hunter 	if (!gpio_cd)
1576a645dd8SAdrian Hunter 		return 0;
1586a645dd8SAdrian Hunter 
1596a645dd8SAdrian Hunter 	pm_runtime_get_sync(mmc->parent);
1606a645dd8SAdrian Hunter 
1616a645dd8SAdrian Hunter 	spin_lock_irqsave(&host->lock, flags);
1626a645dd8SAdrian Hunter 
1636a645dd8SAdrian Hunter 	if (host->flags & SDHCI_DEVICE_DEAD)
1646a645dd8SAdrian Hunter 		goto out;
1656a645dd8SAdrian Hunter 
1666a645dd8SAdrian Hunter 	ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1676a645dd8SAdrian Hunter out:
1686a645dd8SAdrian Hunter 	spin_unlock_irqrestore(&host->lock, flags);
1696a645dd8SAdrian Hunter 
1706a645dd8SAdrian Hunter 	pm_runtime_mark_last_busy(mmc->parent);
1716a645dd8SAdrian Hunter 	pm_runtime_put_autosuspend(mmc->parent);
1726a645dd8SAdrian Hunter 
1736a645dd8SAdrian Hunter 	return ret;
1746a645dd8SAdrian Hunter }
1756a645dd8SAdrian Hunter 
1767dafca83SAdrian Hunter static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
1777dafca83SAdrian Hunter 				      const char *hid, const char *uid)
178578b36b6SGao, Yunpeng {
179578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
180578b36b6SGao, Yunpeng 	struct sdhci_host *host;
181578b36b6SGao, Yunpeng 
182578b36b6SGao, Yunpeng 	if (!c || !c->host)
183578b36b6SGao, Yunpeng 		return 0;
184578b36b6SGao, Yunpeng 
185578b36b6SGao, Yunpeng 	host = c->host;
186578b36b6SGao, Yunpeng 
18794203042SAndy Shevchenko 	/* Platform specific code during emmc probe slot goes here */
188578b36b6SGao, Yunpeng 
1898024379eSAdrian Hunter 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
1908024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
1918024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
1928024379eSAdrian Hunter 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
1938024379eSAdrian Hunter 
194578b36b6SGao, Yunpeng 	return 0;
195578b36b6SGao, Yunpeng }
196578b36b6SGao, Yunpeng 
1977dafca83SAdrian Hunter static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
1987dafca83SAdrian Hunter 				      const char *hid, const char *uid)
199578b36b6SGao, Yunpeng {
200578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
201578b36b6SGao, Yunpeng 	struct sdhci_host *host;
202578b36b6SGao, Yunpeng 
203578b36b6SGao, Yunpeng 	if (!c || !c->host)
204578b36b6SGao, Yunpeng 		return 0;
205578b36b6SGao, Yunpeng 
206578b36b6SGao, Yunpeng 	host = c->host;
207578b36b6SGao, Yunpeng 
20894203042SAndy Shevchenko 	/* Platform specific code during sdio probe slot goes here */
209578b36b6SGao, Yunpeng 
210578b36b6SGao, Yunpeng 	return 0;
211578b36b6SGao, Yunpeng }
212578b36b6SGao, Yunpeng 
2137dafca83SAdrian Hunter static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
2147dafca83SAdrian Hunter 				    const char *hid, const char *uid)
215578b36b6SGao, Yunpeng {
216578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
217578b36b6SGao, Yunpeng 	struct sdhci_host *host;
218578b36b6SGao, Yunpeng 
219578b36b6SGao, Yunpeng 	if (!c || !c->host || !c->slot)
220578b36b6SGao, Yunpeng 		return 0;
221578b36b6SGao, Yunpeng 
222578b36b6SGao, Yunpeng 	host = c->host;
223578b36b6SGao, Yunpeng 
22494203042SAndy Shevchenko 	/* Platform specific code during sd probe slot goes here */
225578b36b6SGao, Yunpeng 
2266a645dd8SAdrian Hunter 	if (hid && !strcmp(hid, "80865ACA"))
2276a645dd8SAdrian Hunter 		host->mmc_host_ops.get_cd = bxt_get_cd;
2286a645dd8SAdrian Hunter 
229578b36b6SGao, Yunpeng 	return 0;
230578b36b6SGao, Yunpeng }
231578b36b6SGao, Yunpeng 
23207a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
233b04fa064SAdrian Hunter 	.chip    = &sdhci_acpi_chip_int,
234f25c3372SMaurice Petallo 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
2359d65cb88SAdrian Hunter 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
2369d65cb88SAdrian Hunter 		   MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
23707a58883SAdrian Hunter 	.caps2   = MMC_CAP2_HC_ERASE_SZ,
23807a58883SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
239e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
240e839b134SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
241e839b134SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC |
242e839b134SAdrian Hunter 		   SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
243578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_emmc_probe_slot,
24407a58883SAdrian Hunter };
24507a58883SAdrian Hunter 
246e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
247e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
248e1f5633aSAdrian Hunter 		   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
249e5571397SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
2509d65cb88SAdrian Hunter 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
2519d65cb88SAdrian Hunter 		   MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
252e5571397SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
253e5571397SAdrian Hunter 	.pm_caps = MMC_PM_KEEP_POWER,
254578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_sdio_probe_slot,
255e5571397SAdrian Hunter };
256e5571397SAdrian Hunter 
25707a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
2584fd4409cSAdrian Hunter 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
2594fd4409cSAdrian Hunter 		   SDHCI_ACPI_RUNTIME_PM,
260e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
261934e31b9SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
262934e31b9SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC,
2639d65cb88SAdrian Hunter 	.caps    = MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
264578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_sd_probe_slot,
26507a58883SAdrian Hunter };
26607a58883SAdrian Hunter 
26770cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
26870cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
26970cce2afSPhilip Elcan 	.quirks2 = SDHCI_QUIRK2_NO_1_8_V,
27070cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
27170cce2afSPhilip Elcan };
27270cce2afSPhilip Elcan 
27370cce2afSPhilip Elcan static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
27470cce2afSPhilip Elcan 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
27570cce2afSPhilip Elcan 	.caps    = MMC_CAP_NONREMOVABLE,
27670cce2afSPhilip Elcan };
27770cce2afSPhilip Elcan 
27807a58883SAdrian Hunter struct sdhci_acpi_uid_slot {
27907a58883SAdrian Hunter 	const char *hid;
28007a58883SAdrian Hunter 	const char *uid;
28107a58883SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
28207a58883SAdrian Hunter };
28307a58883SAdrian Hunter 
28407a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
285e839b134SAdrian Hunter 	{ "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
286e839b134SAdrian Hunter 	{ "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
287e839b134SAdrian Hunter 	{ "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
28807a58883SAdrian Hunter 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
28907a58883SAdrian Hunter 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
290aad95dc4SAdrian Hunter 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
29107a58883SAdrian Hunter 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
2927147eaf3SAdrian Hunter 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
29307a58883SAdrian Hunter 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
29407c001c1SMika Westerberg 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
295d0ed8e6bSAdrian Hunter 	{ "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
2960cd2f044SMichele Curti 	{ "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
29707a58883SAdrian Hunter 	{ "PNP0D40"  },
29870cce2afSPhilip Elcan 	{ "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
29970cce2afSPhilip Elcan 	{ "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
30007a58883SAdrian Hunter 	{ },
30107a58883SAdrian Hunter };
30207a58883SAdrian Hunter 
303c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = {
304e839b134SAdrian Hunter 	{ "80865ACA" },
305e839b134SAdrian Hunter 	{ "80865ACC" },
306e839b134SAdrian Hunter 	{ "80865AD0" },
30707a58883SAdrian Hunter 	{ "80860F14" },
308aad95dc4SAdrian Hunter 	{ "80860F16" },
30907a58883SAdrian Hunter 	{ "INT33BB"  },
31007a58883SAdrian Hunter 	{ "INT33C6"  },
31107c001c1SMika Westerberg 	{ "INT3436"  },
312d0ed8e6bSAdrian Hunter 	{ "INT344D"  },
313c4e05037SAdrian Hunter 	{ "PNP0D40"  },
31470cce2afSPhilip Elcan 	{ "QCOM8051" },
31570cce2afSPhilip Elcan 	{ "QCOM8052" },
316c4e05037SAdrian Hunter 	{ },
317c4e05037SAdrian Hunter };
318c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
319c4e05037SAdrian Hunter 
3203db35251SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
32107a58883SAdrian Hunter 							 const char *uid)
322c4e05037SAdrian Hunter {
32307a58883SAdrian Hunter 	const struct sdhci_acpi_uid_slot *u;
324c4e05037SAdrian Hunter 
32507a58883SAdrian Hunter 	for (u = sdhci_acpi_uids; u->hid; u++) {
32607a58883SAdrian Hunter 		if (strcmp(u->hid, hid))
32707a58883SAdrian Hunter 			continue;
32807a58883SAdrian Hunter 		if (!u->uid)
32907a58883SAdrian Hunter 			return u->slot;
33007a58883SAdrian Hunter 		if (uid && !strcmp(u->uid, uid))
33107a58883SAdrian Hunter 			return u->slot;
33207a58883SAdrian Hunter 	}
333c4e05037SAdrian Hunter 	return NULL;
334c4e05037SAdrian Hunter }
335c4e05037SAdrian Hunter 
3364e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev)
337c4e05037SAdrian Hunter {
338c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
339c4e05037SAdrian Hunter 	acpi_handle handle = ACPI_HANDLE(dev);
340c4e05037SAdrian Hunter 	struct acpi_device *device;
341c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c;
342c4e05037SAdrian Hunter 	struct sdhci_host *host;
343c4e05037SAdrian Hunter 	struct resource *iomem;
344c4e05037SAdrian Hunter 	resource_size_t len;
345c4e05037SAdrian Hunter 	const char *hid;
3463db35251SAdrian Hunter 	const char *uid;
34787875655SMika Westerberg 	int err;
348c4e05037SAdrian Hunter 
349c4e05037SAdrian Hunter 	if (acpi_bus_get_device(handle, &device))
350c4e05037SAdrian Hunter 		return -ENODEV;
351c4e05037SAdrian Hunter 
352c4e05037SAdrian Hunter 	if (acpi_bus_get_status(device) || !device->status.present)
353c4e05037SAdrian Hunter 		return -ENODEV;
354c4e05037SAdrian Hunter 
355c4e05037SAdrian Hunter 	hid = acpi_device_hid(device);
3563db35251SAdrian Hunter 	uid = device->pnp.unique_id;
357c4e05037SAdrian Hunter 
358c4e05037SAdrian Hunter 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
359c4e05037SAdrian Hunter 	if (!iomem)
360c4e05037SAdrian Hunter 		return -ENOMEM;
361c4e05037SAdrian Hunter 
362c4e05037SAdrian Hunter 	len = resource_size(iomem);
363c4e05037SAdrian Hunter 	if (len < 0x100)
364c4e05037SAdrian Hunter 		dev_err(dev, "Invalid iomem size!\n");
365c4e05037SAdrian Hunter 
366c4e05037SAdrian Hunter 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
367c4e05037SAdrian Hunter 		return -ENOMEM;
368c4e05037SAdrian Hunter 
369c4e05037SAdrian Hunter 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
370c4e05037SAdrian Hunter 	if (IS_ERR(host))
371c4e05037SAdrian Hunter 		return PTR_ERR(host);
372c4e05037SAdrian Hunter 
373c4e05037SAdrian Hunter 	c = sdhci_priv(host);
374c4e05037SAdrian Hunter 	c->host = host;
3753db35251SAdrian Hunter 	c->slot = sdhci_acpi_get_slot(hid, uid);
376c4e05037SAdrian Hunter 	c->pdev = pdev;
377c4e05037SAdrian Hunter 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
378c4e05037SAdrian Hunter 
379c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, c);
380c4e05037SAdrian Hunter 
381c4e05037SAdrian Hunter 	host->hw_name	= "ACPI";
382c4e05037SAdrian Hunter 	host->ops	= &sdhci_acpi_ops_dflt;
383c4e05037SAdrian Hunter 	host->irq	= platform_get_irq(pdev, 0);
384c4e05037SAdrian Hunter 
385c4e05037SAdrian Hunter 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
386c4e05037SAdrian Hunter 					    resource_size(iomem));
387c4e05037SAdrian Hunter 	if (host->ioaddr == NULL) {
388c4e05037SAdrian Hunter 		err = -ENOMEM;
389c4e05037SAdrian Hunter 		goto err_free;
390c4e05037SAdrian Hunter 	}
391c4e05037SAdrian Hunter 
392c4e05037SAdrian Hunter 	if (c->slot) {
393578b36b6SGao, Yunpeng 		if (c->slot->probe_slot) {
3947dafca83SAdrian Hunter 			err = c->slot->probe_slot(pdev, hid, uid);
395578b36b6SGao, Yunpeng 			if (err)
396578b36b6SGao, Yunpeng 				goto err_free;
397578b36b6SGao, Yunpeng 		}
398c4e05037SAdrian Hunter 		if (c->slot->chip) {
399c4e05037SAdrian Hunter 			host->ops            = c->slot->chip->ops;
400c4e05037SAdrian Hunter 			host->quirks        |= c->slot->chip->quirks;
401c4e05037SAdrian Hunter 			host->quirks2       |= c->slot->chip->quirks2;
402c4e05037SAdrian Hunter 			host->mmc->caps     |= c->slot->chip->caps;
403c4e05037SAdrian Hunter 			host->mmc->caps2    |= c->slot->chip->caps2;
404c4e05037SAdrian Hunter 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
405c4e05037SAdrian Hunter 		}
406c4e05037SAdrian Hunter 		host->quirks        |= c->slot->quirks;
407c4e05037SAdrian Hunter 		host->quirks2       |= c->slot->quirks2;
408c4e05037SAdrian Hunter 		host->mmc->caps     |= c->slot->caps;
409c4e05037SAdrian Hunter 		host->mmc->caps2    |= c->slot->caps2;
410c4e05037SAdrian Hunter 		host->mmc->pm_caps  |= c->slot->pm_caps;
411c4e05037SAdrian Hunter 	}
412c4e05037SAdrian Hunter 
4130d3e3350SAdrian Hunter 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
4140d3e3350SAdrian Hunter 
4154fd4409cSAdrian Hunter 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
4164fd4409cSAdrian Hunter 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
4174fd4409cSAdrian Hunter 
41889168b48SLinus Walleij 		if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
4194fd4409cSAdrian Hunter 			dev_warn(dev, "failed to setup card detect gpio\n");
4204fd4409cSAdrian Hunter 			c->use_runtime_pm = false;
4214fd4409cSAdrian Hunter 		}
4224fd4409cSAdrian Hunter 	}
4234fd4409cSAdrian Hunter 
424c4e05037SAdrian Hunter 	err = sdhci_add_host(host);
425c4e05037SAdrian Hunter 	if (err)
426c4e05037SAdrian Hunter 		goto err_free;
427c4e05037SAdrian Hunter 
428c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
4291d1ff458SAdrian Hunter 		pm_runtime_set_active(dev);
430c4e05037SAdrian Hunter 		pm_suspend_ignore_children(dev, 1);
431c4e05037SAdrian Hunter 		pm_runtime_set_autosuspend_delay(dev, 50);
432c4e05037SAdrian Hunter 		pm_runtime_use_autosuspend(dev);
433c4e05037SAdrian Hunter 		pm_runtime_enable(dev);
434c4e05037SAdrian Hunter 	}
435c4e05037SAdrian Hunter 
4364e6a2ef9SFu, Zhonghui 	device_enable_async_suspend(dev);
4374e6a2ef9SFu, Zhonghui 
438c4e05037SAdrian Hunter 	return 0;
439c4e05037SAdrian Hunter 
440c4e05037SAdrian Hunter err_free:
441c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
442c4e05037SAdrian Hunter 	return err;
443c4e05037SAdrian Hunter }
444c4e05037SAdrian Hunter 
4454e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev)
446c4e05037SAdrian Hunter {
447c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
448c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
449c4e05037SAdrian Hunter 	int dead;
450c4e05037SAdrian Hunter 
451c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
452c4e05037SAdrian Hunter 		pm_runtime_get_sync(dev);
453c4e05037SAdrian Hunter 		pm_runtime_disable(dev);
454c4e05037SAdrian Hunter 		pm_runtime_put_noidle(dev);
455c4e05037SAdrian Hunter 	}
456c4e05037SAdrian Hunter 
457578b36b6SGao, Yunpeng 	if (c->slot && c->slot->remove_slot)
458578b36b6SGao, Yunpeng 		c->slot->remove_slot(pdev);
459578b36b6SGao, Yunpeng 
460c4e05037SAdrian Hunter 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
461c4e05037SAdrian Hunter 	sdhci_remove_host(c->host, dead);
462c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
463c4e05037SAdrian Hunter 
464c4e05037SAdrian Hunter 	return 0;
465c4e05037SAdrian Hunter }
466c4e05037SAdrian Hunter 
467c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP
468c4e05037SAdrian Hunter 
469c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev)
470c4e05037SAdrian Hunter {
471c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
472c4e05037SAdrian Hunter 
473c4e05037SAdrian Hunter 	return sdhci_suspend_host(c->host);
474c4e05037SAdrian Hunter }
475c4e05037SAdrian Hunter 
476c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev)
477c4e05037SAdrian Hunter {
478c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
479c4e05037SAdrian Hunter 
480c4e05037SAdrian Hunter 	return sdhci_resume_host(c->host);
481c4e05037SAdrian Hunter }
482c4e05037SAdrian Hunter 
483c4e05037SAdrian Hunter #else
484c4e05037SAdrian Hunter 
485c4e05037SAdrian Hunter #define sdhci_acpi_suspend	NULL
486c4e05037SAdrian Hunter #define sdhci_acpi_resume	NULL
487c4e05037SAdrian Hunter 
488c4e05037SAdrian Hunter #endif
489c4e05037SAdrian Hunter 
490162d6f98SRafael J. Wysocki #ifdef CONFIG_PM
491c4e05037SAdrian Hunter 
492c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev)
493c4e05037SAdrian Hunter {
494c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
495c4e05037SAdrian Hunter 
496c4e05037SAdrian Hunter 	return sdhci_runtime_suspend_host(c->host);
497c4e05037SAdrian Hunter }
498c4e05037SAdrian Hunter 
499c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev)
500c4e05037SAdrian Hunter {
501c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
502c4e05037SAdrian Hunter 
503c4e05037SAdrian Hunter 	return sdhci_runtime_resume_host(c->host);
504c4e05037SAdrian Hunter }
505c4e05037SAdrian Hunter 
506c4e05037SAdrian Hunter #endif
507c4e05037SAdrian Hunter 
508c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = {
509c4e05037SAdrian Hunter 	.suspend		= sdhci_acpi_suspend,
510c4e05037SAdrian Hunter 	.resume			= sdhci_acpi_resume,
5111d75f74bSPeter Griffin 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
5129b449e99SUlf Hansson 			sdhci_acpi_runtime_resume, NULL)
513c4e05037SAdrian Hunter };
514c4e05037SAdrian Hunter 
515c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = {
516c4e05037SAdrian Hunter 	.driver = {
517c4e05037SAdrian Hunter 		.name			= "sdhci-acpi",
518c4e05037SAdrian Hunter 		.acpi_match_table	= sdhci_acpi_ids,
519c4e05037SAdrian Hunter 		.pm			= &sdhci_acpi_pm_ops,
520c4e05037SAdrian Hunter 	},
521c4e05037SAdrian Hunter 	.probe	= sdhci_acpi_probe,
5224e608e4eSGreg Kroah-Hartman 	.remove	= sdhci_acpi_remove,
523c4e05037SAdrian Hunter };
524c4e05037SAdrian Hunter 
525c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver);
526c4e05037SAdrian Hunter 
527c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
528c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter");
529c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2");
530