OSDN Git Service

ddd5d43093b7f809bd89afea6cf3c2038167e3de
[android-x86/kernel.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
44 #include "sdhci.h"
45
46 #ifdef CONFIG_X86
47 #include <asm/cpu_device_id.h>
48 static bool sdhci_acpi_on_byt(void)
49 {
50         static const struct x86_cpu_id byt[] = {
51                 { X86_VENDOR_INTEL, 6, 0x37 },
52                 {}
53         };
54
55         return x86_match_cpu(byt);
56 }
57 #else
58 static bool sdhci_acpi_on_byt(void)
59 {
60         return false;
61 }
62 #endif
63
64 enum {
65         SDHCI_ACPI_SD_CD                = BIT(0),
66         SDHCI_ACPI_RUNTIME_PM           = BIT(1),
67         SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
68 };
69
70 struct sdhci_acpi_chip {
71         const struct    sdhci_ops *ops;
72         unsigned int    quirks;
73         unsigned int    quirks2;
74         unsigned long   caps;
75         unsigned int    caps2;
76         mmc_pm_flag_t   pm_caps;
77 };
78
79 struct sdhci_acpi_slot {
80         const struct    sdhci_acpi_chip *chip;
81         unsigned int    quirks;
82         unsigned int    quirks2;
83         unsigned long   caps;
84         unsigned int    caps2;
85         mmc_pm_flag_t   pm_caps;
86         unsigned int    flags;
87         int (*probe_slot)(struct platform_device *, const char *, const char *);
88         int (*remove_slot)(struct platform_device *);
89 };
90
91 struct sdhci_acpi_host {
92         struct sdhci_host               *host;
93         const struct sdhci_acpi_slot    *slot;
94         struct platform_device          *pdev;
95         bool                            use_runtime_pm;
96         bool                            dma_setup;
97 };
98
99 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
100 {
101         return c->slot && (c->slot->flags & flag);
102 }
103
104 static int sdhci_acpi_enable_dma(struct sdhci_host *host)
105 {
106         struct sdhci_acpi_host *c = sdhci_priv(host);
107         struct device *dev = &c->pdev->dev;
108         int err = -1;
109
110         if (c->dma_setup)
111                 return 0;
112
113         if (host->flags & SDHCI_USE_64_BIT_DMA) {
114                 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) {
115                         host->flags &= ~SDHCI_USE_64_BIT_DMA;
116                 } else {
117                         err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
118                         if (err)
119                                 dev_warn(dev, "Failed to set 64-bit DMA mask\n");
120                 }
121         }
122
123         if (err)
124                 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
125
126         c->dma_setup = !err;
127
128         return err;
129 }
130
131 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
132 {
133         u8 reg;
134
135         reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
136         reg |= 0x10;
137         sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
138         /* For eMMC, minimum is 1us but give it 9us for good measure */
139         udelay(9);
140         reg &= ~0x10;
141         sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
142         /* For eMMC, minimum is 200us but give it 300us for good measure */
143         usleep_range(300, 1000);
144 }
145
146 static const struct sdhci_ops sdhci_acpi_ops_dflt = {
147         .set_clock = sdhci_set_clock,
148         .enable_dma = sdhci_acpi_enable_dma,
149         .set_bus_width = sdhci_set_bus_width,
150         .reset = sdhci_reset,
151         .set_uhs_signaling = sdhci_set_uhs_signaling,
152 };
153
154 static const struct sdhci_ops sdhci_acpi_ops_int = {
155         .set_clock = sdhci_set_clock,
156         .enable_dma = sdhci_acpi_enable_dma,
157         .set_bus_width = sdhci_set_bus_width,
158         .reset = sdhci_reset,
159         .set_uhs_signaling = sdhci_set_uhs_signaling,
160         .hw_reset   = sdhci_acpi_int_hw_reset,
161 };
162
163 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
164         .ops = &sdhci_acpi_ops_int,
165 };
166
167 static void sdhci_acpi_int_dma_latency(struct sdhci_host *host)
168 {
169         if (sdhci_acpi_on_byt()) {
170                 host->dma_latency = 20;
171                 host->lat_cancel_delay = 275;
172         }
173 }
174
175 static int bxt_get_cd(struct mmc_host *mmc)
176 {
177         int gpio_cd = mmc_gpio_get_cd(mmc);
178         struct sdhci_host *host = mmc_priv(mmc);
179         unsigned long flags;
180         int ret = 0;
181
182         if (!gpio_cd)
183                 return 0;
184
185         pm_runtime_get_sync(mmc->parent);
186
187         spin_lock_irqsave(&host->lock, flags);
188
189         if (host->flags & SDHCI_DEVICE_DEAD)
190                 goto out;
191
192         ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
193 out:
194         spin_unlock_irqrestore(&host->lock, flags);
195
196         pm_runtime_mark_last_busy(mmc->parent);
197         pm_runtime_put_autosuspend(mmc->parent);
198
199         return ret;
200 }
201
202 static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
203                                       const char *hid, const char *uid)
204 {
205         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
206         struct sdhci_host *host;
207
208         if (!c || !c->host)
209                 return 0;
210
211         host = c->host;
212
213         /* Platform specific code during emmc probe slot goes here */
214
215         if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
216             sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
217             sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
218                 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
219
220         sdhci_acpi_int_dma_latency(host);
221
222         return 0;
223 }
224
225 static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
226                                       const char *hid, const char *uid)
227 {
228         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
229         struct sdhci_host *host;
230
231         if (!c || !c->host)
232                 return 0;
233
234         host = c->host;
235
236         sdhci_acpi_int_dma_latency(host);
237
238         /* Platform specific code during sdio probe slot goes here */
239
240         return 0;
241 }
242
243 static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
244                                     const char *hid, const char *uid)
245 {
246         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
247         struct sdhci_host *host;
248
249         if (!c || !c->host || !c->slot)
250                 return 0;
251
252         host = c->host;
253
254         sdhci_acpi_int_dma_latency(host);
255
256         /* Platform specific code during sd probe slot goes here */
257
258         if (hid && !strcmp(hid, "80865ACA"))
259                 host->mmc_host_ops.get_cd = bxt_get_cd;
260
261         return 0;
262 }
263
264 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
265         .chip    = &sdhci_acpi_chip_int,
266         .caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
267                    MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
268                    MMC_CAP_WAIT_WHILE_BUSY,
269         .caps2   = MMC_CAP2_HC_ERASE_SZ,
270         .flags   = SDHCI_ACPI_RUNTIME_PM,
271         .quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
272         .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
273                    SDHCI_QUIRK2_STOP_WITH_TC |
274                    SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
275         .probe_slot     = sdhci_acpi_emmc_probe_slot,
276 };
277
278 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
279         .quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
280                    SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
281         .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
282         .caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
283                    MMC_CAP_WAIT_WHILE_BUSY,
284         .flags   = SDHCI_ACPI_RUNTIME_PM,
285         .pm_caps = MMC_PM_KEEP_POWER,
286         .probe_slot     = sdhci_acpi_sdio_probe_slot,
287 };
288
289 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
290         .flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
291                    SDHCI_ACPI_RUNTIME_PM,
292         .quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
293         .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
294                    SDHCI_QUIRK2_STOP_WITH_TC,
295         .caps    = MMC_CAP_WAIT_WHILE_BUSY,
296         .probe_slot     = sdhci_acpi_sd_probe_slot,
297 };
298
299 struct sdhci_acpi_uid_slot {
300         const char *hid;
301         const char *uid;
302         const struct sdhci_acpi_slot *slot;
303 };
304
305 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
306         { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
307         { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
308         { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
309         { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
310         { "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
311         { "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
312         { "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
313         { "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
314         { "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
315         { "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
316         { "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
317         { "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
318         { "PNP0D40"  },
319         { },
320 };
321
322 static const struct acpi_device_id sdhci_acpi_ids[] = {
323         { "80865ACA" },
324         { "80865ACC" },
325         { "80865AD0" },
326         { "80860F14" },
327         { "80860F16" },
328         { "INT33BB"  },
329         { "INT33C6"  },
330         { "INT3436"  },
331         { "INT344D"  },
332         { "PNP0D40"  },
333         { },
334 };
335 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
336
337 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
338                                                          const char *uid)
339 {
340         const struct sdhci_acpi_uid_slot *u;
341
342         for (u = sdhci_acpi_uids; u->hid; u++) {
343                 if (strcmp(u->hid, hid))
344                         continue;
345                 if (!u->uid)
346                         return u->slot;
347                 if (uid && !strcmp(u->uid, uid))
348                         return u->slot;
349         }
350         return NULL;
351 }
352
353 static int sdhci_acpi_probe(struct platform_device *pdev)
354 {
355         struct device *dev = &pdev->dev;
356         acpi_handle handle = ACPI_HANDLE(dev);
357         struct acpi_device *device, *child;
358         struct sdhci_acpi_host *c;
359         struct sdhci_host *host;
360         struct resource *iomem;
361         resource_size_t len;
362         const char *hid;
363         const char *uid;
364         int err;
365
366         if (acpi_bus_get_device(handle, &device))
367                 return -ENODEV;
368
369         /* Power on the SDHCI controller and its children */
370         acpi_device_fix_up_power(device);
371         list_for_each_entry(child, &device->children, node)
372                 acpi_device_fix_up_power(child);
373
374         if (acpi_bus_get_status(device) || !device->status.present)
375                 return -ENODEV;
376
377         hid = acpi_device_hid(device);
378         uid = device->pnp.unique_id;
379
380         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
381         if (!iomem)
382                 return -ENOMEM;
383
384         len = resource_size(iomem);
385         if (len < 0x100)
386                 dev_err(dev, "Invalid iomem size!\n");
387
388         if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
389                 return -ENOMEM;
390
391         host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
392         if (IS_ERR(host))
393                 return PTR_ERR(host);
394
395         c = sdhci_priv(host);
396         c->host = host;
397         c->slot = sdhci_acpi_get_slot(hid, uid);
398         c->pdev = pdev;
399         c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
400
401         platform_set_drvdata(pdev, c);
402
403         host->hw_name   = "ACPI";
404         host->ops       = &sdhci_acpi_ops_dflt;
405         host->irq       = platform_get_irq(pdev, 0);
406
407         host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
408                                             resource_size(iomem));
409         if (host->ioaddr == NULL) {
410                 err = -ENOMEM;
411                 goto err_free;
412         }
413
414         if (c->slot) {
415                 if (c->slot->probe_slot) {
416                         err = c->slot->probe_slot(pdev, hid, uid);
417                         if (err)
418                                 goto err_free;
419                 }
420                 if (c->slot->chip) {
421                         host->ops            = c->slot->chip->ops;
422                         host->quirks        |= c->slot->chip->quirks;
423                         host->quirks2       |= c->slot->chip->quirks2;
424                         host->mmc->caps     |= c->slot->chip->caps;
425                         host->mmc->caps2    |= c->slot->chip->caps2;
426                         host->mmc->pm_caps  |= c->slot->chip->pm_caps;
427                 }
428                 host->quirks        |= c->slot->quirks;
429                 host->quirks2       |= c->slot->quirks2;
430                 host->mmc->caps     |= c->slot->caps;
431                 host->mmc->caps2    |= c->slot->caps2;
432                 host->mmc->pm_caps  |= c->slot->pm_caps;
433         }
434
435         host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
436
437         if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
438                 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
439
440                 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
441                         dev_warn(dev, "failed to setup card detect gpio\n");
442                         c->use_runtime_pm = false;
443                 }
444         }
445
446         err = sdhci_add_host(host);
447         if (err)
448                 goto err_free;
449
450         if (c->use_runtime_pm) {
451                 pm_runtime_set_active(dev);
452                 pm_suspend_ignore_children(dev, 1);
453                 pm_runtime_set_autosuspend_delay(dev, 50);
454                 pm_runtime_use_autosuspend(dev);
455                 pm_runtime_enable(dev);
456         }
457
458         return 0;
459
460 err_free:
461         sdhci_free_host(c->host);
462         return err;
463 }
464
465 static int sdhci_acpi_remove(struct platform_device *pdev)
466 {
467         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
468         struct device *dev = &pdev->dev;
469         int dead;
470
471         if (c->use_runtime_pm) {
472                 pm_runtime_get_sync(dev);
473                 pm_runtime_disable(dev);
474                 pm_runtime_put_noidle(dev);
475         }
476
477         if (c->slot && c->slot->remove_slot)
478                 c->slot->remove_slot(pdev);
479
480         dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
481         sdhci_remove_host(c->host, dead);
482         sdhci_free_host(c->host);
483
484         return 0;
485 }
486
487 #ifdef CONFIG_PM_SLEEP
488
489 static int sdhci_acpi_suspend(struct device *dev)
490 {
491         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
492
493         return sdhci_suspend_host(c->host);
494 }
495
496 static int sdhci_acpi_resume(struct device *dev)
497 {
498         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
499
500         return sdhci_resume_host(c->host);
501 }
502
503 #else
504
505 #define sdhci_acpi_suspend      NULL
506 #define sdhci_acpi_resume       NULL
507
508 #endif
509
510 #ifdef CONFIG_PM
511
512 static int sdhci_acpi_runtime_suspend(struct device *dev)
513 {
514         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
515
516         return sdhci_runtime_suspend_host(c->host);
517 }
518
519 static int sdhci_acpi_runtime_resume(struct device *dev)
520 {
521         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
522
523         return sdhci_runtime_resume_host(c->host);
524 }
525
526 #endif
527
528 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
529         .suspend                = sdhci_acpi_suspend,
530         .resume                 = sdhci_acpi_resume,
531         SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
532                         sdhci_acpi_runtime_resume, NULL)
533 };
534
535 static struct platform_driver sdhci_acpi_driver = {
536         .driver = {
537                 .name                   = "sdhci-acpi",
538                 .acpi_match_table       = sdhci_acpi_ids,
539                 .pm                     = &sdhci_acpi_pm_ops,
540         },
541         .probe  = sdhci_acpi_probe,
542         .remove = sdhci_acpi_remove,
543 };
544
545 module_platform_driver(sdhci_acpi_driver);
546
547 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
548 MODULE_AUTHOR("Adrian Hunter");
549 MODULE_LICENSE("GPL v2");