xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision e839b134)
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 
1497dafca83SAdrian Hunter static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
1507dafca83SAdrian Hunter 				      const char *hid, const char *uid)
151578b36b6SGao, Yunpeng {
152578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
153578b36b6SGao, Yunpeng 	struct sdhci_host *host;
154578b36b6SGao, Yunpeng 
155578b36b6SGao, Yunpeng 	if (!c || !c->host)
156578b36b6SGao, Yunpeng 		return 0;
157578b36b6SGao, Yunpeng 
158578b36b6SGao, Yunpeng 	host = c->host;
159578b36b6SGao, Yunpeng 
16094203042SAndy Shevchenko 	/* Platform specific code during emmc probe slot goes here */
161578b36b6SGao, Yunpeng 
1628024379eSAdrian Hunter 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
1638024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
1648024379eSAdrian Hunter 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
1658024379eSAdrian Hunter 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
1668024379eSAdrian Hunter 
167578b36b6SGao, Yunpeng 	return 0;
168578b36b6SGao, Yunpeng }
169578b36b6SGao, Yunpeng 
1707dafca83SAdrian Hunter static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
1717dafca83SAdrian Hunter 				      const char *hid, const char *uid)
172578b36b6SGao, Yunpeng {
173578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
174578b36b6SGao, Yunpeng 	struct sdhci_host *host;
175578b36b6SGao, Yunpeng 
176578b36b6SGao, Yunpeng 	if (!c || !c->host)
177578b36b6SGao, Yunpeng 		return 0;
178578b36b6SGao, Yunpeng 
179578b36b6SGao, Yunpeng 	host = c->host;
180578b36b6SGao, Yunpeng 
18194203042SAndy Shevchenko 	/* Platform specific code during sdio probe slot goes here */
182578b36b6SGao, Yunpeng 
183578b36b6SGao, Yunpeng 	return 0;
184578b36b6SGao, Yunpeng }
185578b36b6SGao, Yunpeng 
1867dafca83SAdrian Hunter static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
1877dafca83SAdrian Hunter 				    const char *hid, const char *uid)
188578b36b6SGao, Yunpeng {
189578b36b6SGao, Yunpeng 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
190578b36b6SGao, Yunpeng 	struct sdhci_host *host;
191578b36b6SGao, Yunpeng 
192578b36b6SGao, Yunpeng 	if (!c || !c->host || !c->slot)
193578b36b6SGao, Yunpeng 		return 0;
194578b36b6SGao, Yunpeng 
195578b36b6SGao, Yunpeng 	host = c->host;
196578b36b6SGao, Yunpeng 
19794203042SAndy Shevchenko 	/* Platform specific code during sd probe slot goes here */
198578b36b6SGao, Yunpeng 
199578b36b6SGao, Yunpeng 	return 0;
200578b36b6SGao, Yunpeng }
201578b36b6SGao, Yunpeng 
20207a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
203b04fa064SAdrian Hunter 	.chip    = &sdhci_acpi_chip_int,
204f25c3372SMaurice Petallo 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
2059d65cb88SAdrian Hunter 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
2069d65cb88SAdrian Hunter 		   MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
20707a58883SAdrian Hunter 	.caps2   = MMC_CAP2_HC_ERASE_SZ,
20807a58883SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
209e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
210e839b134SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
211e839b134SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC |
212e839b134SAdrian Hunter 		   SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
213578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_emmc_probe_slot,
21407a58883SAdrian Hunter };
21507a58883SAdrian Hunter 
216e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
217e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
218e1f5633aSAdrian Hunter 		   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
219e5571397SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
2209d65cb88SAdrian Hunter 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
2219d65cb88SAdrian Hunter 		   MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
222e5571397SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
223e5571397SAdrian Hunter 	.pm_caps = MMC_PM_KEEP_POWER,
224578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_sdio_probe_slot,
225e5571397SAdrian Hunter };
226e5571397SAdrian Hunter 
22707a58883SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
2284fd4409cSAdrian Hunter 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
2294fd4409cSAdrian Hunter 		   SDHCI_ACPI_RUNTIME_PM,
230e1f5633aSAdrian Hunter 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
231934e31b9SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
232934e31b9SAdrian Hunter 		   SDHCI_QUIRK2_STOP_WITH_TC,
2339d65cb88SAdrian Hunter 	.caps    = MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY,
234578b36b6SGao, Yunpeng 	.probe_slot	= sdhci_acpi_sd_probe_slot,
23507a58883SAdrian Hunter };
23607a58883SAdrian Hunter 
23707a58883SAdrian Hunter struct sdhci_acpi_uid_slot {
23807a58883SAdrian Hunter 	const char *hid;
23907a58883SAdrian Hunter 	const char *uid;
24007a58883SAdrian Hunter 	const struct sdhci_acpi_slot *slot;
24107a58883SAdrian Hunter };
24207a58883SAdrian Hunter 
24307a58883SAdrian Hunter static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
244e839b134SAdrian Hunter 	{ "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
245e839b134SAdrian Hunter 	{ "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
246e839b134SAdrian Hunter 	{ "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
24707a58883SAdrian Hunter 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
24807a58883SAdrian Hunter 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
249aad95dc4SAdrian Hunter 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
25007a58883SAdrian Hunter 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
2517147eaf3SAdrian Hunter 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
25207a58883SAdrian Hunter 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
25307c001c1SMika Westerberg 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
254d0ed8e6bSAdrian Hunter 	{ "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
2550cd2f044SMichele Curti 	{ "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
25607a58883SAdrian Hunter 	{ "PNP0D40"  },
25707a58883SAdrian Hunter 	{ },
25807a58883SAdrian Hunter };
25907a58883SAdrian Hunter 
260c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = {
261e839b134SAdrian Hunter 	{ "80865ACA" },
262e839b134SAdrian Hunter 	{ "80865ACC" },
263e839b134SAdrian Hunter 	{ "80865AD0" },
26407a58883SAdrian Hunter 	{ "80860F14" },
265aad95dc4SAdrian Hunter 	{ "80860F16" },
26607a58883SAdrian Hunter 	{ "INT33BB"  },
26707a58883SAdrian Hunter 	{ "INT33C6"  },
26807c001c1SMika Westerberg 	{ "INT3436"  },
269d0ed8e6bSAdrian Hunter 	{ "INT344D"  },
270c4e05037SAdrian Hunter 	{ "PNP0D40"  },
271c4e05037SAdrian Hunter 	{ },
272c4e05037SAdrian Hunter };
273c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
274c4e05037SAdrian Hunter 
2753db35251SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
27607a58883SAdrian Hunter 							 const char *uid)
277c4e05037SAdrian Hunter {
27807a58883SAdrian Hunter 	const struct sdhci_acpi_uid_slot *u;
279c4e05037SAdrian Hunter 
28007a58883SAdrian Hunter 	for (u = sdhci_acpi_uids; u->hid; u++) {
28107a58883SAdrian Hunter 		if (strcmp(u->hid, hid))
28207a58883SAdrian Hunter 			continue;
28307a58883SAdrian Hunter 		if (!u->uid)
28407a58883SAdrian Hunter 			return u->slot;
28507a58883SAdrian Hunter 		if (uid && !strcmp(u->uid, uid))
28607a58883SAdrian Hunter 			return u->slot;
28707a58883SAdrian Hunter 	}
288c4e05037SAdrian Hunter 	return NULL;
289c4e05037SAdrian Hunter }
290c4e05037SAdrian Hunter 
2914e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev)
292c4e05037SAdrian Hunter {
293c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
294c4e05037SAdrian Hunter 	acpi_handle handle = ACPI_HANDLE(dev);
295c4e05037SAdrian Hunter 	struct acpi_device *device;
296c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c;
297c4e05037SAdrian Hunter 	struct sdhci_host *host;
298c4e05037SAdrian Hunter 	struct resource *iomem;
299c4e05037SAdrian Hunter 	resource_size_t len;
300c4e05037SAdrian Hunter 	const char *hid;
3013db35251SAdrian Hunter 	const char *uid;
30287875655SMika Westerberg 	int err;
303c4e05037SAdrian Hunter 
304c4e05037SAdrian Hunter 	if (acpi_bus_get_device(handle, &device))
305c4e05037SAdrian Hunter 		return -ENODEV;
306c4e05037SAdrian Hunter 
307c4e05037SAdrian Hunter 	if (acpi_bus_get_status(device) || !device->status.present)
308c4e05037SAdrian Hunter 		return -ENODEV;
309c4e05037SAdrian Hunter 
310c4e05037SAdrian Hunter 	hid = acpi_device_hid(device);
3113db35251SAdrian Hunter 	uid = device->pnp.unique_id;
312c4e05037SAdrian Hunter 
313c4e05037SAdrian Hunter 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
314c4e05037SAdrian Hunter 	if (!iomem)
315c4e05037SAdrian Hunter 		return -ENOMEM;
316c4e05037SAdrian Hunter 
317c4e05037SAdrian Hunter 	len = resource_size(iomem);
318c4e05037SAdrian Hunter 	if (len < 0x100)
319c4e05037SAdrian Hunter 		dev_err(dev, "Invalid iomem size!\n");
320c4e05037SAdrian Hunter 
321c4e05037SAdrian Hunter 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
322c4e05037SAdrian Hunter 		return -ENOMEM;
323c4e05037SAdrian Hunter 
324c4e05037SAdrian Hunter 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
325c4e05037SAdrian Hunter 	if (IS_ERR(host))
326c4e05037SAdrian Hunter 		return PTR_ERR(host);
327c4e05037SAdrian Hunter 
328c4e05037SAdrian Hunter 	c = sdhci_priv(host);
329c4e05037SAdrian Hunter 	c->host = host;
3303db35251SAdrian Hunter 	c->slot = sdhci_acpi_get_slot(hid, uid);
331c4e05037SAdrian Hunter 	c->pdev = pdev;
332c4e05037SAdrian Hunter 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
333c4e05037SAdrian Hunter 
334c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, c);
335c4e05037SAdrian Hunter 
336c4e05037SAdrian Hunter 	host->hw_name	= "ACPI";
337c4e05037SAdrian Hunter 	host->ops	= &sdhci_acpi_ops_dflt;
338c4e05037SAdrian Hunter 	host->irq	= platform_get_irq(pdev, 0);
339c4e05037SAdrian Hunter 
340c4e05037SAdrian Hunter 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
341c4e05037SAdrian Hunter 					    resource_size(iomem));
342c4e05037SAdrian Hunter 	if (host->ioaddr == NULL) {
343c4e05037SAdrian Hunter 		err = -ENOMEM;
344c4e05037SAdrian Hunter 		goto err_free;
345c4e05037SAdrian Hunter 	}
346c4e05037SAdrian Hunter 
347c4e05037SAdrian Hunter 	if (c->slot) {
348578b36b6SGao, Yunpeng 		if (c->slot->probe_slot) {
3497dafca83SAdrian Hunter 			err = c->slot->probe_slot(pdev, hid, uid);
350578b36b6SGao, Yunpeng 			if (err)
351578b36b6SGao, Yunpeng 				goto err_free;
352578b36b6SGao, Yunpeng 		}
353c4e05037SAdrian Hunter 		if (c->slot->chip) {
354c4e05037SAdrian Hunter 			host->ops            = c->slot->chip->ops;
355c4e05037SAdrian Hunter 			host->quirks        |= c->slot->chip->quirks;
356c4e05037SAdrian Hunter 			host->quirks2       |= c->slot->chip->quirks2;
357c4e05037SAdrian Hunter 			host->mmc->caps     |= c->slot->chip->caps;
358c4e05037SAdrian Hunter 			host->mmc->caps2    |= c->slot->chip->caps2;
359c4e05037SAdrian Hunter 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
360c4e05037SAdrian Hunter 		}
361c4e05037SAdrian Hunter 		host->quirks        |= c->slot->quirks;
362c4e05037SAdrian Hunter 		host->quirks2       |= c->slot->quirks2;
363c4e05037SAdrian Hunter 		host->mmc->caps     |= c->slot->caps;
364c4e05037SAdrian Hunter 		host->mmc->caps2    |= c->slot->caps2;
365c4e05037SAdrian Hunter 		host->mmc->pm_caps  |= c->slot->pm_caps;
366c4e05037SAdrian Hunter 	}
367c4e05037SAdrian Hunter 
3680d3e3350SAdrian Hunter 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
3690d3e3350SAdrian Hunter 
3704fd4409cSAdrian Hunter 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
3714fd4409cSAdrian Hunter 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
3724fd4409cSAdrian Hunter 
37389168b48SLinus Walleij 		if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
3744fd4409cSAdrian Hunter 			dev_warn(dev, "failed to setup card detect gpio\n");
3754fd4409cSAdrian Hunter 			c->use_runtime_pm = false;
3764fd4409cSAdrian Hunter 		}
3774fd4409cSAdrian Hunter 	}
3784fd4409cSAdrian Hunter 
379c4e05037SAdrian Hunter 	err = sdhci_add_host(host);
380c4e05037SAdrian Hunter 	if (err)
381c4e05037SAdrian Hunter 		goto err_free;
382c4e05037SAdrian Hunter 
383c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
3841d1ff458SAdrian Hunter 		pm_runtime_set_active(dev);
385c4e05037SAdrian Hunter 		pm_suspend_ignore_children(dev, 1);
386c4e05037SAdrian Hunter 		pm_runtime_set_autosuspend_delay(dev, 50);
387c4e05037SAdrian Hunter 		pm_runtime_use_autosuspend(dev);
388c4e05037SAdrian Hunter 		pm_runtime_enable(dev);
389c4e05037SAdrian Hunter 	}
390c4e05037SAdrian Hunter 
391c4e05037SAdrian Hunter 	return 0;
392c4e05037SAdrian Hunter 
393c4e05037SAdrian Hunter err_free:
394c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
395c4e05037SAdrian Hunter 	return err;
396c4e05037SAdrian Hunter }
397c4e05037SAdrian Hunter 
3984e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev)
399c4e05037SAdrian Hunter {
400c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
401c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
402c4e05037SAdrian Hunter 	int dead;
403c4e05037SAdrian Hunter 
404c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
405c4e05037SAdrian Hunter 		pm_runtime_get_sync(dev);
406c4e05037SAdrian Hunter 		pm_runtime_disable(dev);
407c4e05037SAdrian Hunter 		pm_runtime_put_noidle(dev);
408c4e05037SAdrian Hunter 	}
409c4e05037SAdrian Hunter 
410578b36b6SGao, Yunpeng 	if (c->slot && c->slot->remove_slot)
411578b36b6SGao, Yunpeng 		c->slot->remove_slot(pdev);
412578b36b6SGao, Yunpeng 
413c4e05037SAdrian Hunter 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
414c4e05037SAdrian Hunter 	sdhci_remove_host(c->host, dead);
415c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
416c4e05037SAdrian Hunter 
417c4e05037SAdrian Hunter 	return 0;
418c4e05037SAdrian Hunter }
419c4e05037SAdrian Hunter 
420c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP
421c4e05037SAdrian Hunter 
422c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev)
423c4e05037SAdrian Hunter {
424c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
425c4e05037SAdrian Hunter 
426c4e05037SAdrian Hunter 	return sdhci_suspend_host(c->host);
427c4e05037SAdrian Hunter }
428c4e05037SAdrian Hunter 
429c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev)
430c4e05037SAdrian Hunter {
431c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
432c4e05037SAdrian Hunter 
433c4e05037SAdrian Hunter 	return sdhci_resume_host(c->host);
434c4e05037SAdrian Hunter }
435c4e05037SAdrian Hunter 
436c4e05037SAdrian Hunter #else
437c4e05037SAdrian Hunter 
438c4e05037SAdrian Hunter #define sdhci_acpi_suspend	NULL
439c4e05037SAdrian Hunter #define sdhci_acpi_resume	NULL
440c4e05037SAdrian Hunter 
441c4e05037SAdrian Hunter #endif
442c4e05037SAdrian Hunter 
443162d6f98SRafael J. Wysocki #ifdef CONFIG_PM
444c4e05037SAdrian Hunter 
445c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev)
446c4e05037SAdrian Hunter {
447c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
448c4e05037SAdrian Hunter 
449c4e05037SAdrian Hunter 	return sdhci_runtime_suspend_host(c->host);
450c4e05037SAdrian Hunter }
451c4e05037SAdrian Hunter 
452c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev)
453c4e05037SAdrian Hunter {
454c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
455c4e05037SAdrian Hunter 
456c4e05037SAdrian Hunter 	return sdhci_runtime_resume_host(c->host);
457c4e05037SAdrian Hunter }
458c4e05037SAdrian Hunter 
459c4e05037SAdrian Hunter #endif
460c4e05037SAdrian Hunter 
461c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = {
462c4e05037SAdrian Hunter 	.suspend		= sdhci_acpi_suspend,
463c4e05037SAdrian Hunter 	.resume			= sdhci_acpi_resume,
4641d75f74bSPeter Griffin 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
4659b449e99SUlf Hansson 			sdhci_acpi_runtime_resume, NULL)
466c4e05037SAdrian Hunter };
467c4e05037SAdrian Hunter 
468c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = {
469c4e05037SAdrian Hunter 	.driver = {
470c4e05037SAdrian Hunter 		.name			= "sdhci-acpi",
471c4e05037SAdrian Hunter 		.acpi_match_table	= sdhci_acpi_ids,
472c4e05037SAdrian Hunter 		.pm			= &sdhci_acpi_pm_ops,
473c4e05037SAdrian Hunter 	},
474c4e05037SAdrian Hunter 	.probe	= sdhci_acpi_probe,
4754e608e4eSGreg Kroah-Hartman 	.remove	= sdhci_acpi_remove,
476c4e05037SAdrian Hunter };
477c4e05037SAdrian Hunter 
478c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver);
479c4e05037SAdrian Hunter 
480c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
481c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter");
482c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2");
483