blob: 840788ebdc7fde2a91d0938f20d9025d0ed3031c [file] [log] [blame]
Adrian Hunterc4e05032012-11-23 21:17:34 +01001/*
2 * Secure Digital Host Controller Interface ACPI driver.
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <linux/init.h>
22#include <linux/export.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/dma-mapping.h>
29#include <linux/compiler.h>
30#include <linux/stddef.h>
31#include <linux/bitops.h>
32#include <linux/types.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/acpi.h>
36#include <linux/pm.h>
37#include <linux/pm_runtime.h>
Adrian Hunterb04fa062013-06-13 11:50:27 +030038#include <linux/delay.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010039
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
Adrian Hunter4fd44092014-03-10 15:02:42 +020042#include <linux/mmc/slot-gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010043#include <linux/mmc/sdhci.h>
44
45#include "sdhci.h"
46
47enum {
Adrian Hunter4fd44092014-03-10 15:02:42 +020048 SDHCI_ACPI_SD_CD = BIT(0),
49 SDHCI_ACPI_RUNTIME_PM = BIT(1),
50 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
Adrian Hunterc4e05032012-11-23 21:17:34 +010051};
52
53struct sdhci_acpi_chip {
54 const struct sdhci_ops *ops;
55 unsigned int quirks;
56 unsigned int quirks2;
57 unsigned long caps;
58 unsigned int caps2;
59 mmc_pm_flag_t pm_caps;
60};
61
62struct sdhci_acpi_slot {
63 const struct sdhci_acpi_chip *chip;
64 unsigned int quirks;
65 unsigned int quirks2;
66 unsigned long caps;
67 unsigned int caps2;
68 mmc_pm_flag_t pm_caps;
69 unsigned int flags;
70};
71
72struct sdhci_acpi_host {
73 struct sdhci_host *host;
74 const struct sdhci_acpi_slot *slot;
75 struct platform_device *pdev;
76 bool use_runtime_pm;
77};
78
79static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
80{
81 return c->slot && (c->slot->flags & flag);
82}
83
84static int sdhci_acpi_enable_dma(struct sdhci_host *host)
85{
86 return 0;
87}
88
Adrian Hunterb04fa062013-06-13 11:50:27 +030089static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
90{
91 u8 reg;
92
93 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
94 reg |= 0x10;
95 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
96 /* For eMMC, minimum is 1us but give it 9us for good measure */
97 udelay(9);
98 reg &= ~0x10;
99 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
100 /* For eMMC, minimum is 200us but give it 300us for good measure */
101 usleep_range(300, 1000);
102}
103
Adrian Hunterc4e05032012-11-23 21:17:34 +0100104static const struct sdhci_ops sdhci_acpi_ops_dflt = {
Russell King17710592014-04-25 12:58:55 +0100105 .set_clock = sdhci_set_clock,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100106 .enable_dma = sdhci_acpi_enable_dma,
Russell King2317f562014-04-25 12:57:07 +0100107 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100108 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100109 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100110};
111
Adrian Hunterb04fa062013-06-13 11:50:27 +0300112static const struct sdhci_ops sdhci_acpi_ops_int = {
Russell King17710592014-04-25 12:58:55 +0100113 .set_clock = sdhci_set_clock,
Adrian Hunterb04fa062013-06-13 11:50:27 +0300114 .enable_dma = sdhci_acpi_enable_dma,
Russell King2317f562014-04-25 12:57:07 +0100115 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100116 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100117 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterb04fa062013-06-13 11:50:27 +0300118 .hw_reset = sdhci_acpi_int_hw_reset,
119};
120
121static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
122 .ops = &sdhci_acpi_ops_int,
123};
124
Adrian Hunter07a58882013-04-26 11:27:22 +0300125static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
Adrian Hunterb04fa062013-06-13 11:50:27 +0300126 .chip = &sdhci_acpi_chip_int,
127 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET,
Adrian Hunter07a58882013-04-26 11:27:22 +0300128 .caps2 = MMC_CAP2_HC_ERASE_SZ,
129 .flags = SDHCI_ACPI_RUNTIME_PM,
Maurice Petallod61b5942014-07-08 19:11:00 +0800130 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Adrian Hunter07a58882013-04-26 11:27:22 +0300131};
132
Adrian Huntere5571392012-12-10 21:18:48 +0100133static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
Adrian Hunterc6748012014-04-03 14:58:39 +0300134 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Adrian Huntere5571392012-12-10 21:18:48 +0100135 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
136 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
137 .flags = SDHCI_ACPI_RUNTIME_PM,
138 .pm_caps = MMC_PM_KEEP_POWER,
139};
140
Adrian Hunter07a58882013-04-26 11:27:22 +0300141static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Hunter4fd44092014-03-10 15:02:42 +0200142 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
143 SDHCI_ACPI_RUNTIME_PM,
Adrian Huntera61abe62013-05-06 12:17:33 +0300144 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
Adrian Hunter07a58882013-04-26 11:27:22 +0300145};
146
147struct sdhci_acpi_uid_slot {
148 const char *hid;
149 const char *uid;
150 const struct sdhci_acpi_slot *slot;
151};
152
153static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
154 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
155 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200156 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
Adrian Hunter07a58882013-04-26 11:27:22 +0300157 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
158 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200159 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
Adrian Hunter07a58882013-04-26 11:27:22 +0300160 { "PNP0D40" },
161 { },
162};
163
Adrian Hunterc4e05032012-11-23 21:17:34 +0100164static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Hunter07a58882013-04-26 11:27:22 +0300165 { "80860F14" },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200166 { "80860F16" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300167 { "INT33BB" },
168 { "INT33C6" },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200169 { "INT3436" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300170 { "PNP0D40" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100171 { },
172};
173MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
174
Adrian Hunter07a58882013-04-26 11:27:22 +0300175static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
176 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100177{
Adrian Hunter07a58882013-04-26 11:27:22 +0300178 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100179
Adrian Hunter07a58882013-04-26 11:27:22 +0300180 for (u = sdhci_acpi_uids; u->hid; u++) {
181 if (strcmp(u->hid, hid))
182 continue;
183 if (!u->uid)
184 return u->slot;
185 if (uid && !strcmp(u->uid, uid))
186 return u->slot;
187 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100188 return NULL;
189}
190
Adrian Hunter07a58882013-04-26 11:27:22 +0300191static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
192 const char *hid)
193{
194 const struct sdhci_acpi_slot *slot;
195 struct acpi_device_info *info;
196 const char *uid = NULL;
197 acpi_status status;
198
199 status = acpi_get_object_info(handle, &info);
200 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
201 uid = info->unique_id.string;
202
203 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
204
205 kfree(info);
206 return slot;
207}
208
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800209static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100210{
211 struct device *dev = &pdev->dev;
212 acpi_handle handle = ACPI_HANDLE(dev);
213 struct acpi_device *device;
214 struct sdhci_acpi_host *c;
215 struct sdhci_host *host;
216 struct resource *iomem;
217 resource_size_t len;
218 const char *hid;
Mika Westerberg87875652014-01-08 12:40:55 +0200219 int err;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100220
221 if (acpi_bus_get_device(handle, &device))
222 return -ENODEV;
223
224 if (acpi_bus_get_status(device) || !device->status.present)
225 return -ENODEV;
226
227 hid = acpi_device_hid(device);
228
229 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
230 if (!iomem)
231 return -ENOMEM;
232
233 len = resource_size(iomem);
234 if (len < 0x100)
235 dev_err(dev, "Invalid iomem size!\n");
236
237 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
238 return -ENOMEM;
239
240 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
241 if (IS_ERR(host))
242 return PTR_ERR(host);
243
244 c = sdhci_priv(host);
245 c->host = host;
Adrian Hunter07a58882013-04-26 11:27:22 +0300246 c->slot = sdhci_acpi_get_slot(handle, hid);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100247 c->pdev = pdev;
248 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
249
250 platform_set_drvdata(pdev, c);
251
252 host->hw_name = "ACPI";
253 host->ops = &sdhci_acpi_ops_dflt;
254 host->irq = platform_get_irq(pdev, 0);
255
256 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
257 resource_size(iomem));
258 if (host->ioaddr == NULL) {
259 err = -ENOMEM;
260 goto err_free;
261 }
262
263 if (!dev->dma_mask) {
264 u64 dma_mask;
265
266 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
267 /* 64-bit DMA is not supported at present */
268 dma_mask = DMA_BIT_MASK(32);
269 } else {
270 dma_mask = DMA_BIT_MASK(32);
271 }
272
Russell King07f44502013-06-27 14:06:28 +0100273 err = dma_coerce_mask_and_coherent(dev, dma_mask);
274 if (err)
275 goto err_free;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100276 }
277
278 if (c->slot) {
279 if (c->slot->chip) {
280 host->ops = c->slot->chip->ops;
281 host->quirks |= c->slot->chip->quirks;
282 host->quirks2 |= c->slot->chip->quirks2;
283 host->mmc->caps |= c->slot->chip->caps;
284 host->mmc->caps2 |= c->slot->chip->caps2;
285 host->mmc->pm_caps |= c->slot->chip->pm_caps;
286 }
287 host->quirks |= c->slot->quirks;
288 host->quirks2 |= c->slot->quirks2;
289 host->mmc->caps |= c->slot->caps;
290 host->mmc->caps2 |= c->slot->caps2;
291 host->mmc->pm_caps |= c->slot->pm_caps;
292 }
293
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300294 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
295
Adrian Hunter4fd44092014-03-10 15:02:42 +0200296 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
297 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
298
299 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) {
300 dev_warn(dev, "failed to setup card detect gpio\n");
301 c->use_runtime_pm = false;
302 }
303 }
304
Adrian Hunterc4e05032012-11-23 21:17:34 +0100305 err = sdhci_add_host(host);
306 if (err)
307 goto err_free;
308
309 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300310 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100311 pm_suspend_ignore_children(dev, 1);
312 pm_runtime_set_autosuspend_delay(dev, 50);
313 pm_runtime_use_autosuspend(dev);
314 pm_runtime_enable(dev);
315 }
316
317 return 0;
318
319err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100320 sdhci_free_host(c->host);
321 return err;
322}
323
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800324static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100325{
326 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
327 struct device *dev = &pdev->dev;
328 int dead;
329
330 if (c->use_runtime_pm) {
331 pm_runtime_get_sync(dev);
332 pm_runtime_disable(dev);
333 pm_runtime_put_noidle(dev);
334 }
335
336 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
337 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100338 sdhci_free_host(c->host);
339
340 return 0;
341}
342
343#ifdef CONFIG_PM_SLEEP
344
345static int sdhci_acpi_suspend(struct device *dev)
346{
347 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
348
349 return sdhci_suspend_host(c->host);
350}
351
352static int sdhci_acpi_resume(struct device *dev)
353{
354 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
355
356 return sdhci_resume_host(c->host);
357}
358
359#else
360
361#define sdhci_acpi_suspend NULL
362#define sdhci_acpi_resume NULL
363
364#endif
365
366#ifdef CONFIG_PM_RUNTIME
367
368static int sdhci_acpi_runtime_suspend(struct device *dev)
369{
370 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
371
372 return sdhci_runtime_suspend_host(c->host);
373}
374
375static int sdhci_acpi_runtime_resume(struct device *dev)
376{
377 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
378
379 return sdhci_runtime_resume_host(c->host);
380}
381
382static int sdhci_acpi_runtime_idle(struct device *dev)
383{
384 return 0;
385}
386
387#else
388
389#define sdhci_acpi_runtime_suspend NULL
390#define sdhci_acpi_runtime_resume NULL
391#define sdhci_acpi_runtime_idle NULL
392
393#endif
394
395static const struct dev_pm_ops sdhci_acpi_pm_ops = {
396 .suspend = sdhci_acpi_suspend,
397 .resume = sdhci_acpi_resume,
398 .runtime_suspend = sdhci_acpi_runtime_suspend,
399 .runtime_resume = sdhci_acpi_runtime_resume,
400 .runtime_idle = sdhci_acpi_runtime_idle,
401};
402
403static struct platform_driver sdhci_acpi_driver = {
404 .driver = {
405 .name = "sdhci-acpi",
406 .owner = THIS_MODULE,
407 .acpi_match_table = sdhci_acpi_ids,
408 .pm = &sdhci_acpi_pm_ops,
409 },
410 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800411 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100412};
413
414module_platform_driver(sdhci_acpi_driver);
415
416MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
417MODULE_AUTHOR("Adrian Hunter");
418MODULE_LICENSE("GPL v2");