xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision 4e608e4e)
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>
38c4e05037SAdrian Hunter 
39c4e05037SAdrian Hunter #include <linux/mmc/host.h>
40c4e05037SAdrian Hunter #include <linux/mmc/pm.h>
41c4e05037SAdrian Hunter #include <linux/mmc/sdhci.h>
42c4e05037SAdrian Hunter 
43c4e05037SAdrian Hunter #include "sdhci.h"
44c4e05037SAdrian Hunter 
45c4e05037SAdrian Hunter enum {
46c4e05037SAdrian Hunter 	SDHCI_ACPI_SD_CD	= BIT(0),
47c4e05037SAdrian Hunter 	SDHCI_ACPI_RUNTIME_PM	= BIT(1),
48c4e05037SAdrian Hunter };
49c4e05037SAdrian Hunter 
50c4e05037SAdrian Hunter struct sdhci_acpi_chip {
51c4e05037SAdrian Hunter 	const struct	sdhci_ops *ops;
52c4e05037SAdrian Hunter 	unsigned int	quirks;
53c4e05037SAdrian Hunter 	unsigned int	quirks2;
54c4e05037SAdrian Hunter 	unsigned long	caps;
55c4e05037SAdrian Hunter 	unsigned int	caps2;
56c4e05037SAdrian Hunter 	mmc_pm_flag_t	pm_caps;
57c4e05037SAdrian Hunter };
58c4e05037SAdrian Hunter 
59c4e05037SAdrian Hunter struct sdhci_acpi_slot {
60c4e05037SAdrian Hunter 	const struct	sdhci_acpi_chip *chip;
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 	unsigned int	flags;
67c4e05037SAdrian Hunter };
68c4e05037SAdrian Hunter 
69c4e05037SAdrian Hunter struct sdhci_acpi_host {
70c4e05037SAdrian Hunter 	struct sdhci_host		*host;
71c4e05037SAdrian Hunter 	const struct sdhci_acpi_slot	*slot;
72c4e05037SAdrian Hunter 	struct platform_device		*pdev;
73c4e05037SAdrian Hunter 	bool				use_runtime_pm;
74c4e05037SAdrian Hunter };
75c4e05037SAdrian Hunter 
76c4e05037SAdrian Hunter static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
77c4e05037SAdrian Hunter {
78c4e05037SAdrian Hunter 	return c->slot && (c->slot->flags & flag);
79c4e05037SAdrian Hunter }
80c4e05037SAdrian Hunter 
81c4e05037SAdrian Hunter static int sdhci_acpi_enable_dma(struct sdhci_host *host)
82c4e05037SAdrian Hunter {
83c4e05037SAdrian Hunter 	return 0;
84c4e05037SAdrian Hunter }
85c4e05037SAdrian Hunter 
86c4e05037SAdrian Hunter static const struct sdhci_ops sdhci_acpi_ops_dflt = {
87c4e05037SAdrian Hunter 	.enable_dma = sdhci_acpi_enable_dma,
88c4e05037SAdrian Hunter };
89c4e05037SAdrian Hunter 
90e5571397SAdrian Hunter static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
91e5571397SAdrian Hunter 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
92e5571397SAdrian Hunter 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
93e5571397SAdrian Hunter 	.flags   = SDHCI_ACPI_RUNTIME_PM,
94e5571397SAdrian Hunter 	.pm_caps = MMC_PM_KEEP_POWER,
95e5571397SAdrian Hunter };
96e5571397SAdrian Hunter 
97c4e05037SAdrian Hunter static const struct acpi_device_id sdhci_acpi_ids[] = {
98e5571397SAdrian Hunter 	{ "INT33C6", (kernel_ulong_t)&sdhci_acpi_slot_int_sdio },
99c4e05037SAdrian Hunter 	{ "PNP0D40" },
100c4e05037SAdrian Hunter 	{ },
101c4e05037SAdrian Hunter };
102c4e05037SAdrian Hunter MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
103c4e05037SAdrian Hunter 
104c4e05037SAdrian Hunter static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid)
105c4e05037SAdrian Hunter {
106c4e05037SAdrian Hunter 	const struct acpi_device_id *id;
107c4e05037SAdrian Hunter 
108c4e05037SAdrian Hunter 	for (id = sdhci_acpi_ids; id->id[0]; id++)
109c4e05037SAdrian Hunter 		if (!strcmp(id->id, hid))
110c4e05037SAdrian Hunter 			return (const struct sdhci_acpi_slot *)id->driver_data;
111c4e05037SAdrian Hunter 	return NULL;
112c4e05037SAdrian Hunter }
113c4e05037SAdrian Hunter 
1144e608e4eSGreg Kroah-Hartman static int sdhci_acpi_probe(struct platform_device *pdev)
115c4e05037SAdrian Hunter {
116c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
117c4e05037SAdrian Hunter 	acpi_handle handle = ACPI_HANDLE(dev);
118c4e05037SAdrian Hunter 	struct acpi_device *device;
119c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c;
120c4e05037SAdrian Hunter 	struct sdhci_host *host;
121c4e05037SAdrian Hunter 	struct resource *iomem;
122c4e05037SAdrian Hunter 	resource_size_t len;
123c4e05037SAdrian Hunter 	const char *hid;
124c4e05037SAdrian Hunter 	int err;
125c4e05037SAdrian Hunter 
126c4e05037SAdrian Hunter 	if (acpi_bus_get_device(handle, &device))
127c4e05037SAdrian Hunter 		return -ENODEV;
128c4e05037SAdrian Hunter 
129c4e05037SAdrian Hunter 	if (acpi_bus_get_status(device) || !device->status.present)
130c4e05037SAdrian Hunter 		return -ENODEV;
131c4e05037SAdrian Hunter 
132c4e05037SAdrian Hunter 	hid = acpi_device_hid(device);
133c4e05037SAdrian Hunter 
134c4e05037SAdrian Hunter 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135c4e05037SAdrian Hunter 	if (!iomem)
136c4e05037SAdrian Hunter 		return -ENOMEM;
137c4e05037SAdrian Hunter 
138c4e05037SAdrian Hunter 	len = resource_size(iomem);
139c4e05037SAdrian Hunter 	if (len < 0x100)
140c4e05037SAdrian Hunter 		dev_err(dev, "Invalid iomem size!\n");
141c4e05037SAdrian Hunter 
142c4e05037SAdrian Hunter 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
143c4e05037SAdrian Hunter 		return -ENOMEM;
144c4e05037SAdrian Hunter 
145c4e05037SAdrian Hunter 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
146c4e05037SAdrian Hunter 	if (IS_ERR(host))
147c4e05037SAdrian Hunter 		return PTR_ERR(host);
148c4e05037SAdrian Hunter 
149c4e05037SAdrian Hunter 	c = sdhci_priv(host);
150c4e05037SAdrian Hunter 	c->host = host;
151c4e05037SAdrian Hunter 	c->slot = sdhci_acpi_get_slot(hid);
152c4e05037SAdrian Hunter 	c->pdev = pdev;
153c4e05037SAdrian Hunter 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
154c4e05037SAdrian Hunter 
155c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, c);
156c4e05037SAdrian Hunter 
157c4e05037SAdrian Hunter 	host->hw_name	= "ACPI";
158c4e05037SAdrian Hunter 	host->ops	= &sdhci_acpi_ops_dflt;
159c4e05037SAdrian Hunter 	host->irq	= platform_get_irq(pdev, 0);
160c4e05037SAdrian Hunter 
161c4e05037SAdrian Hunter 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
162c4e05037SAdrian Hunter 					    resource_size(iomem));
163c4e05037SAdrian Hunter 	if (host->ioaddr == NULL) {
164c4e05037SAdrian Hunter 		err = -ENOMEM;
165c4e05037SAdrian Hunter 		goto err_free;
166c4e05037SAdrian Hunter 	}
167c4e05037SAdrian Hunter 
168c4e05037SAdrian Hunter 	if (!dev->dma_mask) {
169c4e05037SAdrian Hunter 		u64 dma_mask;
170c4e05037SAdrian Hunter 
171c4e05037SAdrian Hunter 		if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
172c4e05037SAdrian Hunter 			/* 64-bit DMA is not supported at present */
173c4e05037SAdrian Hunter 			dma_mask = DMA_BIT_MASK(32);
174c4e05037SAdrian Hunter 		} else {
175c4e05037SAdrian Hunter 			dma_mask = DMA_BIT_MASK(32);
176c4e05037SAdrian Hunter 		}
177c4e05037SAdrian Hunter 
178c4e05037SAdrian Hunter 		dev->dma_mask = &dev->coherent_dma_mask;
179c4e05037SAdrian Hunter 		dev->coherent_dma_mask = dma_mask;
180c4e05037SAdrian Hunter 	}
181c4e05037SAdrian Hunter 
182c4e05037SAdrian Hunter 	if (c->slot) {
183c4e05037SAdrian Hunter 		if (c->slot->chip) {
184c4e05037SAdrian Hunter 			host->ops            = c->slot->chip->ops;
185c4e05037SAdrian Hunter 			host->quirks        |= c->slot->chip->quirks;
186c4e05037SAdrian Hunter 			host->quirks2       |= c->slot->chip->quirks2;
187c4e05037SAdrian Hunter 			host->mmc->caps     |= c->slot->chip->caps;
188c4e05037SAdrian Hunter 			host->mmc->caps2    |= c->slot->chip->caps2;
189c4e05037SAdrian Hunter 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
190c4e05037SAdrian Hunter 		}
191c4e05037SAdrian Hunter 		host->quirks        |= c->slot->quirks;
192c4e05037SAdrian Hunter 		host->quirks2       |= c->slot->quirks2;
193c4e05037SAdrian Hunter 		host->mmc->caps     |= c->slot->caps;
194c4e05037SAdrian Hunter 		host->mmc->caps2    |= c->slot->caps2;
195c4e05037SAdrian Hunter 		host->mmc->pm_caps  |= c->slot->pm_caps;
196c4e05037SAdrian Hunter 	}
197c4e05037SAdrian Hunter 
198c4e05037SAdrian Hunter 	err = sdhci_add_host(host);
199c4e05037SAdrian Hunter 	if (err)
200c4e05037SAdrian Hunter 		goto err_free;
201c4e05037SAdrian Hunter 
202c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
203c4e05037SAdrian Hunter 		pm_suspend_ignore_children(dev, 1);
204c4e05037SAdrian Hunter 		pm_runtime_set_autosuspend_delay(dev, 50);
205c4e05037SAdrian Hunter 		pm_runtime_use_autosuspend(dev);
206c4e05037SAdrian Hunter 		pm_runtime_enable(dev);
207c4e05037SAdrian Hunter 	}
208c4e05037SAdrian Hunter 
209c4e05037SAdrian Hunter 	return 0;
210c4e05037SAdrian Hunter 
211c4e05037SAdrian Hunter err_free:
212c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, NULL);
213c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
214c4e05037SAdrian Hunter 	return err;
215c4e05037SAdrian Hunter }
216c4e05037SAdrian Hunter 
2174e608e4eSGreg Kroah-Hartman static int sdhci_acpi_remove(struct platform_device *pdev)
218c4e05037SAdrian Hunter {
219c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
220c4e05037SAdrian Hunter 	struct device *dev = &pdev->dev;
221c4e05037SAdrian Hunter 	int dead;
222c4e05037SAdrian Hunter 
223c4e05037SAdrian Hunter 	if (c->use_runtime_pm) {
224c4e05037SAdrian Hunter 		pm_runtime_get_sync(dev);
225c4e05037SAdrian Hunter 		pm_runtime_disable(dev);
226c4e05037SAdrian Hunter 		pm_runtime_put_noidle(dev);
227c4e05037SAdrian Hunter 	}
228c4e05037SAdrian Hunter 
229c4e05037SAdrian Hunter 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
230c4e05037SAdrian Hunter 	sdhci_remove_host(c->host, dead);
231c4e05037SAdrian Hunter 	platform_set_drvdata(pdev, NULL);
232c4e05037SAdrian Hunter 	sdhci_free_host(c->host);
233c4e05037SAdrian Hunter 
234c4e05037SAdrian Hunter 	return 0;
235c4e05037SAdrian Hunter }
236c4e05037SAdrian Hunter 
237c4e05037SAdrian Hunter #ifdef CONFIG_PM_SLEEP
238c4e05037SAdrian Hunter 
239c4e05037SAdrian Hunter static int sdhci_acpi_suspend(struct device *dev)
240c4e05037SAdrian Hunter {
241c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
242c4e05037SAdrian Hunter 
243c4e05037SAdrian Hunter 	return sdhci_suspend_host(c->host);
244c4e05037SAdrian Hunter }
245c4e05037SAdrian Hunter 
246c4e05037SAdrian Hunter static int sdhci_acpi_resume(struct device *dev)
247c4e05037SAdrian Hunter {
248c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
249c4e05037SAdrian Hunter 
250c4e05037SAdrian Hunter 	return sdhci_resume_host(c->host);
251c4e05037SAdrian Hunter }
252c4e05037SAdrian Hunter 
253c4e05037SAdrian Hunter #else
254c4e05037SAdrian Hunter 
255c4e05037SAdrian Hunter #define sdhci_acpi_suspend	NULL
256c4e05037SAdrian Hunter #define sdhci_acpi_resume	NULL
257c4e05037SAdrian Hunter 
258c4e05037SAdrian Hunter #endif
259c4e05037SAdrian Hunter 
260c4e05037SAdrian Hunter #ifdef CONFIG_PM_RUNTIME
261c4e05037SAdrian Hunter 
262c4e05037SAdrian Hunter static int sdhci_acpi_runtime_suspend(struct device *dev)
263c4e05037SAdrian Hunter {
264c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
265c4e05037SAdrian Hunter 
266c4e05037SAdrian Hunter 	return sdhci_runtime_suspend_host(c->host);
267c4e05037SAdrian Hunter }
268c4e05037SAdrian Hunter 
269c4e05037SAdrian Hunter static int sdhci_acpi_runtime_resume(struct device *dev)
270c4e05037SAdrian Hunter {
271c4e05037SAdrian Hunter 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
272c4e05037SAdrian Hunter 
273c4e05037SAdrian Hunter 	return sdhci_runtime_resume_host(c->host);
274c4e05037SAdrian Hunter }
275c4e05037SAdrian Hunter 
276c4e05037SAdrian Hunter static int sdhci_acpi_runtime_idle(struct device *dev)
277c4e05037SAdrian Hunter {
278c4e05037SAdrian Hunter 	return 0;
279c4e05037SAdrian Hunter }
280c4e05037SAdrian Hunter 
281c4e05037SAdrian Hunter #else
282c4e05037SAdrian Hunter 
283c4e05037SAdrian Hunter #define sdhci_acpi_runtime_suspend	NULL
284c4e05037SAdrian Hunter #define sdhci_acpi_runtime_resume	NULL
285c4e05037SAdrian Hunter #define sdhci_acpi_runtime_idle		NULL
286c4e05037SAdrian Hunter 
287c4e05037SAdrian Hunter #endif
288c4e05037SAdrian Hunter 
289c4e05037SAdrian Hunter static const struct dev_pm_ops sdhci_acpi_pm_ops = {
290c4e05037SAdrian Hunter 	.suspend		= sdhci_acpi_suspend,
291c4e05037SAdrian Hunter 	.resume			= sdhci_acpi_resume,
292c4e05037SAdrian Hunter 	.runtime_suspend	= sdhci_acpi_runtime_suspend,
293c4e05037SAdrian Hunter 	.runtime_resume		= sdhci_acpi_runtime_resume,
294c4e05037SAdrian Hunter 	.runtime_idle		= sdhci_acpi_runtime_idle,
295c4e05037SAdrian Hunter };
296c4e05037SAdrian Hunter 
297c4e05037SAdrian Hunter static struct platform_driver sdhci_acpi_driver = {
298c4e05037SAdrian Hunter 	.driver = {
299c4e05037SAdrian Hunter 		.name			= "sdhci-acpi",
300c4e05037SAdrian Hunter 		.owner			= THIS_MODULE,
301c4e05037SAdrian Hunter 		.acpi_match_table	= sdhci_acpi_ids,
302c4e05037SAdrian Hunter 		.pm			= &sdhci_acpi_pm_ops,
303c4e05037SAdrian Hunter 	},
304c4e05037SAdrian Hunter 	.probe	= sdhci_acpi_probe,
3054e608e4eSGreg Kroah-Hartman 	.remove	= sdhci_acpi_remove,
306c4e05037SAdrian Hunter };
307c4e05037SAdrian Hunter 
308c4e05037SAdrian Hunter module_platform_driver(sdhci_acpi_driver);
309c4e05037SAdrian Hunter 
310c4e05037SAdrian Hunter MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
311c4e05037SAdrian Hunter MODULE_AUTHOR("Adrian Hunter");
312c4e05037SAdrian Hunter MODULE_LICENSE("GPL v2");
313