Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux.git] / drivers / mmc / host / sdhci-acpi.c
1 /*
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>
38 #include <linux/delay.h>
39
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/pm.h>
42 #include <linux/mmc/slot-gpio.h>
43 #include <linux/mmc/sdhci.h>
44
45 #include "sdhci.h"
46
47 enum {
48         SDHCI_ACPI_SD_CD                = BIT(0),
49         SDHCI_ACPI_RUNTIME_PM           = BIT(1),
50         SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
51 };
52
53 struct 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
62 struct 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
72 struct 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
79 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
80 {
81         return c->slot && (c->slot->flags & flag);
82 }
83
84 static int sdhci_acpi_enable_dma(struct sdhci_host *host)
85 {
86         return 0;
87 }
88
89 static 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
104 static const struct sdhci_ops sdhci_acpi_ops_dflt = {
105         .enable_dma = sdhci_acpi_enable_dma,
106 };
107
108 static const struct sdhci_ops sdhci_acpi_ops_int = {
109         .enable_dma = sdhci_acpi_enable_dma,
110         .hw_reset   = sdhci_acpi_int_hw_reset,
111 };
112
113 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
114         .ops = &sdhci_acpi_ops_int,
115 };
116
117 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
118         .chip    = &sdhci_acpi_chip_int,
119         .caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET,
120         .caps2   = MMC_CAP2_HC_ERASE_SZ,
121         .flags   = SDHCI_ACPI_RUNTIME_PM,
122 };
123
124 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
125         .quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
126         .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
127         .caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
128         .flags   = SDHCI_ACPI_RUNTIME_PM,
129         .pm_caps = MMC_PM_KEEP_POWER,
130 };
131
132 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
133         .flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
134                    SDHCI_ACPI_RUNTIME_PM,
135         .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
136 };
137
138 struct sdhci_acpi_uid_slot {
139         const char *hid;
140         const char *uid;
141         const struct sdhci_acpi_slot *slot;
142 };
143
144 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
145         { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
146         { "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
147         { "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
148         { "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
149         { "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
150         { "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
151         { "PNP0D40"  },
152         { },
153 };
154
155 static const struct acpi_device_id sdhci_acpi_ids[] = {
156         { "80860F14" },
157         { "80860F16" },
158         { "INT33BB"  },
159         { "INT33C6"  },
160         { "INT3436"  },
161         { "PNP0D40"  },
162         { },
163 };
164 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
165
166 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
167                                                                 const char *uid)
168 {
169         const struct sdhci_acpi_uid_slot *u;
170
171         for (u = sdhci_acpi_uids; u->hid; u++) {
172                 if (strcmp(u->hid, hid))
173                         continue;
174                 if (!u->uid)
175                         return u->slot;
176                 if (uid && !strcmp(u->uid, uid))
177                         return u->slot;
178         }
179         return NULL;
180 }
181
182 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
183                                                          const char *hid)
184 {
185         const struct sdhci_acpi_slot *slot;
186         struct acpi_device_info *info;
187         const char *uid = NULL;
188         acpi_status status;
189
190         status = acpi_get_object_info(handle, &info);
191         if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
192                 uid = info->unique_id.string;
193
194         slot = sdhci_acpi_get_slot_by_ids(hid, uid);
195
196         kfree(info);
197         return slot;
198 }
199
200 static int sdhci_acpi_probe(struct platform_device *pdev)
201 {
202         struct device *dev = &pdev->dev;
203         acpi_handle handle = ACPI_HANDLE(dev);
204         struct acpi_device *device;
205         struct sdhci_acpi_host *c;
206         struct sdhci_host *host;
207         struct resource *iomem;
208         resource_size_t len;
209         const char *hid;
210         int err;
211
212         if (acpi_bus_get_device(handle, &device))
213                 return -ENODEV;
214
215         if (acpi_bus_get_status(device) || !device->status.present)
216                 return -ENODEV;
217
218         hid = acpi_device_hid(device);
219
220         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
221         if (!iomem)
222                 return -ENOMEM;
223
224         len = resource_size(iomem);
225         if (len < 0x100)
226                 dev_err(dev, "Invalid iomem size!\n");
227
228         if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
229                 return -ENOMEM;
230
231         host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
232         if (IS_ERR(host))
233                 return PTR_ERR(host);
234
235         c = sdhci_priv(host);
236         c->host = host;
237         c->slot = sdhci_acpi_get_slot(handle, hid);
238         c->pdev = pdev;
239         c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
240
241         platform_set_drvdata(pdev, c);
242
243         host->hw_name   = "ACPI";
244         host->ops       = &sdhci_acpi_ops_dflt;
245         host->irq       = platform_get_irq(pdev, 0);
246
247         host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
248                                             resource_size(iomem));
249         if (host->ioaddr == NULL) {
250                 err = -ENOMEM;
251                 goto err_free;
252         }
253
254         if (!dev->dma_mask) {
255                 u64 dma_mask;
256
257                 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
258                         /* 64-bit DMA is not supported at present */
259                         dma_mask = DMA_BIT_MASK(32);
260                 } else {
261                         dma_mask = DMA_BIT_MASK(32);
262                 }
263
264                 err = dma_coerce_mask_and_coherent(dev, dma_mask);
265                 if (err)
266                         goto err_free;
267         }
268
269         if (c->slot) {
270                 if (c->slot->chip) {
271                         host->ops            = c->slot->chip->ops;
272                         host->quirks        |= c->slot->chip->quirks;
273                         host->quirks2       |= c->slot->chip->quirks2;
274                         host->mmc->caps     |= c->slot->chip->caps;
275                         host->mmc->caps2    |= c->slot->chip->caps2;
276                         host->mmc->pm_caps  |= c->slot->chip->pm_caps;
277                 }
278                 host->quirks        |= c->slot->quirks;
279                 host->quirks2       |= c->slot->quirks2;
280                 host->mmc->caps     |= c->slot->caps;
281                 host->mmc->caps2    |= c->slot->caps2;
282                 host->mmc->pm_caps  |= c->slot->pm_caps;
283         }
284
285         host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
286
287         if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
288                 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
289
290                 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) {
291                         dev_warn(dev, "failed to setup card detect gpio\n");
292                         c->use_runtime_pm = false;
293                 }
294         }
295
296         err = sdhci_add_host(host);
297         if (err)
298                 goto err_free;
299
300         if (c->use_runtime_pm) {
301                 pm_runtime_set_active(dev);
302                 pm_suspend_ignore_children(dev, 1);
303                 pm_runtime_set_autosuspend_delay(dev, 50);
304                 pm_runtime_use_autosuspend(dev);
305                 pm_runtime_enable(dev);
306         }
307
308         return 0;
309
310 err_free:
311         sdhci_free_host(c->host);
312         return err;
313 }
314
315 static int sdhci_acpi_remove(struct platform_device *pdev)
316 {
317         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
318         struct device *dev = &pdev->dev;
319         int dead;
320
321         if (c->use_runtime_pm) {
322                 pm_runtime_get_sync(dev);
323                 pm_runtime_disable(dev);
324                 pm_runtime_put_noidle(dev);
325         }
326
327         dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
328         sdhci_remove_host(c->host, dead);
329         sdhci_free_host(c->host);
330
331         return 0;
332 }
333
334 #ifdef CONFIG_PM_SLEEP
335
336 static int sdhci_acpi_suspend(struct device *dev)
337 {
338         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
339
340         return sdhci_suspend_host(c->host);
341 }
342
343 static int sdhci_acpi_resume(struct device *dev)
344 {
345         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
346
347         return sdhci_resume_host(c->host);
348 }
349
350 #else
351
352 #define sdhci_acpi_suspend      NULL
353 #define sdhci_acpi_resume       NULL
354
355 #endif
356
357 #ifdef CONFIG_PM_RUNTIME
358
359 static int sdhci_acpi_runtime_suspend(struct device *dev)
360 {
361         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
362
363         return sdhci_runtime_suspend_host(c->host);
364 }
365
366 static int sdhci_acpi_runtime_resume(struct device *dev)
367 {
368         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
369
370         return sdhci_runtime_resume_host(c->host);
371 }
372
373 static int sdhci_acpi_runtime_idle(struct device *dev)
374 {
375         return 0;
376 }
377
378 #else
379
380 #define sdhci_acpi_runtime_suspend      NULL
381 #define sdhci_acpi_runtime_resume       NULL
382 #define sdhci_acpi_runtime_idle         NULL
383
384 #endif
385
386 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
387         .suspend                = sdhci_acpi_suspend,
388         .resume                 = sdhci_acpi_resume,
389         .runtime_suspend        = sdhci_acpi_runtime_suspend,
390         .runtime_resume         = sdhci_acpi_runtime_resume,
391         .runtime_idle           = sdhci_acpi_runtime_idle,
392 };
393
394 static struct platform_driver sdhci_acpi_driver = {
395         .driver = {
396                 .name                   = "sdhci-acpi",
397                 .owner                  = THIS_MODULE,
398                 .acpi_match_table       = sdhci_acpi_ids,
399                 .pm                     = &sdhci_acpi_pm_ops,
400         },
401         .probe  = sdhci_acpi_probe,
402         .remove = sdhci_acpi_remove,
403 };
404
405 module_platform_driver(sdhci_acpi_driver);
406
407 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
408 MODULE_AUTHOR("Adrian Hunter");
409 MODULE_LICENSE("GPL v2");