2 * Secure Digital Host Controller Interface ACPI driver.
4 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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>
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>
37 #include <linux/pm_runtime.h>
38 #include <linux/delay.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/pm.h>
42 #include <linux/mmc/slot-gpio.h>
47 #include <asm/cpu_device_id.h>
48 static bool sdhci_acpi_on_byt(void)
50 static const struct x86_cpu_id byt[] = {
51 { X86_VENDOR_INTEL, 6, 0x37 },
55 return x86_match_cpu(byt);
58 static bool sdhci_acpi_on_byt(void)
65 SDHCI_ACPI_SD_CD = BIT(0),
66 SDHCI_ACPI_RUNTIME_PM = BIT(1),
67 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
70 struct sdhci_acpi_chip {
71 const struct sdhci_ops *ops;
76 mmc_pm_flag_t pm_caps;
79 struct sdhci_acpi_slot {
80 const struct sdhci_acpi_chip *chip;
85 mmc_pm_flag_t pm_caps;
87 int (*probe_slot)(struct platform_device *, const char *, const char *);
88 int (*remove_slot)(struct platform_device *);
91 struct sdhci_acpi_host {
92 struct sdhci_host *host;
93 const struct sdhci_acpi_slot *slot;
94 struct platform_device *pdev;
99 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
101 return c->slot && (c->slot->flags & flag);
104 static int sdhci_acpi_enable_dma(struct sdhci_host *host)
106 struct sdhci_acpi_host *c = sdhci_priv(host);
107 struct device *dev = &c->pdev->dev;
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;
117 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
119 dev_warn(dev, "Failed to set 64-bit DMA mask\n");
124 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
131 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
135 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
137 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
138 /* For eMMC, minimum is 1us but give it 9us for good measure */
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);
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,
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,
163 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
164 .ops = &sdhci_acpi_ops_int,
167 static void sdhci_acpi_int_dma_latency(struct sdhci_host *host)
169 if (sdhci_acpi_on_byt()) {
170 host->dma_latency = 20;
171 host->lat_cancel_delay = 275;
175 static int bxt_get_cd(struct mmc_host *mmc)
177 int gpio_cd = mmc_gpio_get_cd(mmc);
178 struct sdhci_host *host = mmc_priv(mmc);
185 pm_runtime_get_sync(mmc->parent);
187 spin_lock_irqsave(&host->lock, flags);
189 if (host->flags & SDHCI_DEVICE_DEAD)
192 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
194 spin_unlock_irqrestore(&host->lock, flags);
196 pm_runtime_mark_last_busy(mmc->parent);
197 pm_runtime_put_autosuspend(mmc->parent);
202 static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
203 const char *hid, const char *uid)
205 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
206 struct sdhci_host *host;
213 /* Platform specific code during emmc probe slot goes here */
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 */
220 sdhci_acpi_int_dma_latency(host);
225 static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
226 const char *hid, const char *uid)
228 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
229 struct sdhci_host *host;
236 sdhci_acpi_int_dma_latency(host);
238 /* Platform specific code during sdio probe slot goes here */
243 static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
244 const char *hid, const char *uid)
246 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
247 struct sdhci_host *host;
249 if (!c || !c->host || !c->slot)
254 sdhci_acpi_int_dma_latency(host);
256 /* Platform specific code during sd probe slot goes here */
258 if (hid && !strcmp(hid, "80865ACA"))
259 host->mmc_host_ops.get_cd = bxt_get_cd;
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,
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,
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,
299 struct sdhci_acpi_uid_slot {
302 const struct sdhci_acpi_slot *slot;
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 },
322 static const struct acpi_device_id sdhci_acpi_ids[] = {
335 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
337 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
340 const struct sdhci_acpi_uid_slot *u;
342 for (u = sdhci_acpi_uids; u->hid; u++) {
343 if (strcmp(u->hid, hid))
347 if (uid && !strcmp(u->uid, uid))
353 static int sdhci_acpi_probe(struct platform_device *pdev)
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;
366 if (acpi_bus_get_device(handle, &device))
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);
374 if (acpi_bus_get_status(device) || !device->status.present)
377 hid = acpi_device_hid(device);
378 uid = device->pnp.unique_id;
380 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 len = resource_size(iomem);
386 dev_err(dev, "Invalid iomem size!\n");
388 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
391 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
393 return PTR_ERR(host);
395 c = sdhci_priv(host);
397 c->slot = sdhci_acpi_get_slot(hid, uid);
399 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
401 platform_set_drvdata(pdev, c);
403 host->hw_name = "ACPI";
404 host->ops = &sdhci_acpi_ops_dflt;
405 host->irq = platform_get_irq(pdev, 0);
407 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
408 resource_size(iomem));
409 if (host->ioaddr == NULL) {
415 if (c->slot->probe_slot) {
416 err = c->slot->probe_slot(pdev, hid, uid);
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;
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;
435 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
437 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
438 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
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;
446 err = sdhci_add_host(host);
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);
461 sdhci_free_host(c->host);
465 static int sdhci_acpi_remove(struct platform_device *pdev)
467 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
468 struct device *dev = &pdev->dev;
471 if (c->use_runtime_pm) {
472 pm_runtime_get_sync(dev);
473 pm_runtime_disable(dev);
474 pm_runtime_put_noidle(dev);
477 if (c->slot && c->slot->remove_slot)
478 c->slot->remove_slot(pdev);
480 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
481 sdhci_remove_host(c->host, dead);
482 sdhci_free_host(c->host);
487 #ifdef CONFIG_PM_SLEEP
489 static int sdhci_acpi_suspend(struct device *dev)
491 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
493 return sdhci_suspend_host(c->host);
496 static int sdhci_acpi_resume(struct device *dev)
498 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
500 return sdhci_resume_host(c->host);
505 #define sdhci_acpi_suspend NULL
506 #define sdhci_acpi_resume NULL
512 static int sdhci_acpi_runtime_suspend(struct device *dev)
514 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
516 return sdhci_runtime_suspend_host(c->host);
519 static int sdhci_acpi_runtime_resume(struct device *dev)
521 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
523 return sdhci_runtime_resume_host(c->host);
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)
535 static struct platform_driver sdhci_acpi_driver = {
537 .name = "sdhci-acpi",
538 .acpi_match_table = sdhci_acpi_ids,
539 .pm = &sdhci_acpi_pm_ops,
541 .probe = sdhci_acpi_probe,
542 .remove = sdhci_acpi_remove,
545 module_platform_driver(sdhci_acpi_driver);
547 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
548 MODULE_AUTHOR("Adrian Hunter");
549 MODULE_LICENSE("GPL v2");