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>
45 #include <asm/cpu_device_id.h>
46 #include <asm/intel-family.h>
47 #include <asm/iosf_mbi.h>
48 #include <linux/pci.h>
54 SDHCI_ACPI_SD_CD = BIT(0),
55 SDHCI_ACPI_RUNTIME_PM = BIT(1),
56 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
59 struct sdhci_acpi_chip {
60 const struct sdhci_ops *ops;
65 mmc_pm_flag_t pm_caps;
68 struct sdhci_acpi_slot {
69 const struct sdhci_acpi_chip *chip;
74 mmc_pm_flag_t pm_caps;
77 int (*probe_slot)(struct platform_device *, const char *, const char *);
78 int (*remove_slot)(struct platform_device *);
81 struct sdhci_acpi_host {
82 struct sdhci_host *host;
83 const struct sdhci_acpi_slot *slot;
84 struct platform_device *pdev;
86 unsigned long private[0] ____cacheline_aligned;
89 static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
91 return (void *)c->private;
94 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
96 return c->slot && (c->slot->flags & flag);
101 INTEL_DSM_V18_SWITCH = 3,
102 INTEL_DSM_V33_SWITCH = 4,
109 static const guid_t intel_dsm_guid =
110 GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
111 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
113 static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
114 unsigned int fn, u32 *result)
116 union acpi_object *obj;
119 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
123 if (obj->type == ACPI_TYPE_INTEGER) {
124 *result = obj->integer.value;
125 } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
126 size_t len = min_t(size_t, obj->buffer.length, 4);
129 memcpy(result, obj->buffer.pointer, len);
131 dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
132 __func__, fn, obj->type, obj->buffer.length);
141 static int intel_dsm(struct intel_host *intel_host, struct device *dev,
142 unsigned int fn, u32 *result)
144 if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
147 return __intel_dsm(intel_host, dev, fn, result);
150 static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
151 struct mmc_host *mmc)
155 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
157 pr_debug("%s: DSM not supported, error %d\n",
158 mmc_hostname(mmc), err);
162 pr_debug("%s: DSM function mask %#x\n",
163 mmc_hostname(mmc), intel_host->dsm_fns);
166 static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
169 struct device *dev = mmc_dev(mmc);
170 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
171 struct intel_host *intel_host = sdhci_acpi_priv(c);
176 err = sdhci_start_signal_voltage_switch(mmc, ios);
180 switch (ios->signal_voltage) {
181 case MMC_SIGNAL_VOLTAGE_330:
182 fn = INTEL_DSM_V33_SWITCH;
184 case MMC_SIGNAL_VOLTAGE_180:
185 fn = INTEL_DSM_V18_SWITCH;
191 err = intel_dsm(intel_host, dev, fn, &result);
192 pr_debug("%s: %s DSM fn %u error %d result %u\n",
193 mmc_hostname(mmc), __func__, fn, err, result);
198 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
202 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
204 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
205 /* For eMMC, minimum is 1us but give it 9us for good measure */
208 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
209 /* For eMMC, minimum is 200us but give it 300us for good measure */
210 usleep_range(300, 1000);
213 static const struct sdhci_ops sdhci_acpi_ops_dflt = {
214 .set_clock = sdhci_set_clock,
215 .set_bus_width = sdhci_set_bus_width,
216 .reset = sdhci_reset,
217 .set_uhs_signaling = sdhci_set_uhs_signaling,
220 static const struct sdhci_ops sdhci_acpi_ops_int = {
221 .set_clock = sdhci_set_clock,
222 .set_bus_width = sdhci_set_bus_width,
223 .reset = sdhci_reset,
224 .set_uhs_signaling = sdhci_set_uhs_signaling,
225 .hw_reset = sdhci_acpi_int_hw_reset,
228 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
229 .ops = &sdhci_acpi_ops_int,
234 static bool sdhci_acpi_byt(void)
236 static const struct x86_cpu_id byt[] = {
237 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
241 return x86_match_cpu(byt);
244 static bool sdhci_acpi_cht(void)
246 static const struct x86_cpu_id cht[] = {
247 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
251 return x86_match_cpu(cht);
254 #define BYT_IOSF_SCCEP 0x63
255 #define BYT_IOSF_OCP_NETCTRL0 0x1078
256 #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
258 static void sdhci_acpi_byt_setting(struct device *dev)
262 if (!sdhci_acpi_byt())
265 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
267 dev_err(dev, "%s read error\n", __func__);
271 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
274 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
276 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
278 dev_err(dev, "%s write error\n", __func__);
282 dev_dbg(dev, "%s completed\n", __func__);
285 static bool sdhci_acpi_byt_defer(struct device *dev)
287 if (!sdhci_acpi_byt())
290 if (!iosf_mbi_available())
293 sdhci_acpi_byt_setting(dev);
298 static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
299 unsigned int slot, unsigned int parent_slot)
301 struct pci_dev *dev, *parent, *from = NULL;
304 dev = pci_get_device(vendor, device, from);
308 parent = pci_upstream_bridge(dev);
309 if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
310 parent && PCI_SLOT(parent->devfn) == parent_slot &&
311 !pci_upstream_bridge(parent)) {
322 * GPDwin uses PCI wifi which conflicts with SDIO's use of
323 * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
324 * problematic, but since SDIO is only used for wifi, the presence of the PCI
325 * wifi card in the expected slot with an ACPI companion node, is used to
326 * indicate that acpi_device_fix_up_power() should be avoided.
328 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
331 return sdhci_acpi_cht() &&
332 !strcmp(hid, "80860F14") &&
334 sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
339 static inline void sdhci_acpi_byt_setting(struct device *dev)
343 static inline bool sdhci_acpi_byt_defer(struct device *dev)
348 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
356 static int bxt_get_cd(struct mmc_host *mmc)
358 int gpio_cd = mmc_gpio_get_cd(mmc);
359 struct sdhci_host *host = mmc_priv(mmc);
366 spin_lock_irqsave(&host->lock, flags);
368 if (host->flags & SDHCI_DEVICE_DEAD)
371 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
373 spin_unlock_irqrestore(&host->lock, flags);
378 static int intel_probe_slot(struct platform_device *pdev, const char *hid,
381 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
382 struct intel_host *intel_host = sdhci_acpi_priv(c);
383 struct sdhci_host *host = c->host;
385 if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
386 sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
387 sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
388 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
390 if (hid && !strcmp(hid, "80865ACA"))
391 host->mmc_host_ops.get_cd = bxt_get_cd;
393 intel_dsm_init(intel_host, &pdev->dev, host->mmc);
395 host->mmc_host_ops.start_signal_voltage_switch =
396 intel_start_signal_voltage_switch;
401 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
402 .chip = &sdhci_acpi_chip_int,
403 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
404 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
405 MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
406 .flags = SDHCI_ACPI_RUNTIME_PM,
407 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
408 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
409 SDHCI_QUIRK2_STOP_WITH_TC |
410 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
411 .probe_slot = intel_probe_slot,
412 .priv_size = sizeof(struct intel_host),
415 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
416 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
417 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
418 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
419 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
420 MMC_CAP_WAIT_WHILE_BUSY,
421 .flags = SDHCI_ACPI_RUNTIME_PM,
422 .pm_caps = MMC_PM_KEEP_POWER,
423 .probe_slot = intel_probe_slot,
424 .priv_size = sizeof(struct intel_host),
427 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
428 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
429 SDHCI_ACPI_RUNTIME_PM,
430 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
431 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
432 SDHCI_QUIRK2_STOP_WITH_TC,
433 .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
434 .probe_slot = intel_probe_slot,
435 .priv_size = sizeof(struct intel_host),
438 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
439 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
440 .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
441 .caps = MMC_CAP_NONREMOVABLE,
444 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
445 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
446 .caps = MMC_CAP_NONREMOVABLE,
449 struct sdhci_acpi_uid_slot {
452 const struct sdhci_acpi_slot *slot;
455 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
456 { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
457 { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
458 { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
459 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
460 { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
461 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
462 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
463 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
464 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
465 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
466 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
467 { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
468 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
470 { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
471 { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
475 static const struct acpi_device_id sdhci_acpi_ids[] = {
490 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
492 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
495 const struct sdhci_acpi_uid_slot *u;
497 for (u = sdhci_acpi_uids; u->hid; u++) {
498 if (strcmp(u->hid, hid))
502 if (uid && !strcmp(u->uid, uid))
508 static int sdhci_acpi_probe(struct platform_device *pdev)
510 struct device *dev = &pdev->dev;
511 const struct sdhci_acpi_slot *slot;
512 struct acpi_device *device, *child;
513 struct sdhci_acpi_host *c;
514 struct sdhci_host *host;
515 struct resource *iomem;
522 device = ACPI_COMPANION(dev);
526 hid = acpi_device_hid(device);
527 uid = acpi_device_uid(device);
529 slot = sdhci_acpi_get_slot(hid, uid);
531 /* Power on the SDHCI controller and its children */
532 acpi_device_fix_up_power(device);
533 if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
534 list_for_each_entry(child, &device->children, node)
535 if (child->status.present && child->status.enabled)
536 acpi_device_fix_up_power(child);
539 if (sdhci_acpi_byt_defer(dev))
540 return -EPROBE_DEFER;
542 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
546 len = resource_size(iomem);
548 dev_err(dev, "Invalid iomem size!\n");
550 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
553 priv_size = slot ? slot->priv_size : 0;
554 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
556 return PTR_ERR(host);
558 c = sdhci_priv(host);
562 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
564 platform_set_drvdata(pdev, c);
566 host->hw_name = "ACPI";
567 host->ops = &sdhci_acpi_ops_dflt;
568 host->irq = platform_get_irq(pdev, 0);
570 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
571 resource_size(iomem));
572 if (host->ioaddr == NULL) {
578 if (c->slot->probe_slot) {
579 err = c->slot->probe_slot(pdev, hid, uid);
584 host->ops = c->slot->chip->ops;
585 host->quirks |= c->slot->chip->quirks;
586 host->quirks2 |= c->slot->chip->quirks2;
587 host->mmc->caps |= c->slot->chip->caps;
588 host->mmc->caps2 |= c->slot->chip->caps2;
589 host->mmc->pm_caps |= c->slot->chip->pm_caps;
591 host->quirks |= c->slot->quirks;
592 host->quirks2 |= c->slot->quirks2;
593 host->mmc->caps |= c->slot->caps;
594 host->mmc->caps2 |= c->slot->caps2;
595 host->mmc->pm_caps |= c->slot->pm_caps;
598 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
600 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
601 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
603 err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
605 if (err == -EPROBE_DEFER)
607 dev_warn(dev, "failed to setup card detect gpio\n");
608 c->use_runtime_pm = false;
612 err = sdhci_add_host(host);
616 if (c->use_runtime_pm) {
617 pm_runtime_set_active(dev);
618 pm_suspend_ignore_children(dev, 1);
619 pm_runtime_set_autosuspend_delay(dev, 50);
620 pm_runtime_use_autosuspend(dev);
621 pm_runtime_enable(dev);
624 device_enable_async_suspend(dev);
629 sdhci_free_host(c->host);
633 static int sdhci_acpi_remove(struct platform_device *pdev)
635 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
636 struct device *dev = &pdev->dev;
639 if (c->use_runtime_pm) {
640 pm_runtime_get_sync(dev);
641 pm_runtime_disable(dev);
642 pm_runtime_put_noidle(dev);
645 if (c->slot && c->slot->remove_slot)
646 c->slot->remove_slot(pdev);
648 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
649 sdhci_remove_host(c->host, dead);
650 sdhci_free_host(c->host);
655 #ifdef CONFIG_PM_SLEEP
657 static int sdhci_acpi_suspend(struct device *dev)
659 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
660 struct sdhci_host *host = c->host;
662 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
663 mmc_retune_needed(host->mmc);
665 return sdhci_suspend_host(host);
668 static int sdhci_acpi_resume(struct device *dev)
670 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
672 sdhci_acpi_byt_setting(&c->pdev->dev);
674 return sdhci_resume_host(c->host);
681 static int sdhci_acpi_runtime_suspend(struct device *dev)
683 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
684 struct sdhci_host *host = c->host;
686 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
687 mmc_retune_needed(host->mmc);
689 return sdhci_runtime_suspend_host(host);
692 static int sdhci_acpi_runtime_resume(struct device *dev)
694 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
696 sdhci_acpi_byt_setting(&c->pdev->dev);
698 return sdhci_runtime_resume_host(c->host);
703 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
704 SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
705 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
706 sdhci_acpi_runtime_resume, NULL)
709 static struct platform_driver sdhci_acpi_driver = {
711 .name = "sdhci-acpi",
712 .acpi_match_table = sdhci_acpi_ids,
713 .pm = &sdhci_acpi_pm_ops,
715 .probe = sdhci_acpi_probe,
716 .remove = sdhci_acpi_remove,
719 module_platform_driver(sdhci_acpi_driver);
721 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
722 MODULE_AUTHOR("Adrian Hunter");
723 MODULE_LICENSE("GPL v2");