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 #ifdef CONFIG_X86 45 #include <asm/cpu_device_id.h> 46 #include <asm/intel-family.h> 47 #include <asm/iosf_mbi.h> 48 #include <linux/pci.h> 49 #endif 50 51 #include "sdhci.h" 52 53 enum { 54 SDHCI_ACPI_SD_CD = BIT(0), 55 SDHCI_ACPI_RUNTIME_PM = BIT(1), 56 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2), 57 }; 58 59 struct sdhci_acpi_chip { 60 const struct sdhci_ops *ops; 61 unsigned int quirks; 62 unsigned int quirks2; 63 unsigned long caps; 64 unsigned int caps2; 65 mmc_pm_flag_t pm_caps; 66 }; 67 68 struct sdhci_acpi_slot { 69 const struct sdhci_acpi_chip *chip; 70 unsigned int quirks; 71 unsigned int quirks2; 72 unsigned long caps; 73 unsigned int caps2; 74 mmc_pm_flag_t pm_caps; 75 unsigned int flags; 76 size_t priv_size; 77 int (*probe_slot)(struct platform_device *, const char *, const char *); 78 int (*remove_slot)(struct platform_device *); 79 }; 80 81 struct sdhci_acpi_host { 82 struct sdhci_host *host; 83 const struct sdhci_acpi_slot *slot; 84 struct platform_device *pdev; 85 bool use_runtime_pm; 86 unsigned long private[0] ____cacheline_aligned; 87 }; 88 89 static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c) 90 { 91 return (void *)c->private; 92 } 93 94 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag) 95 { 96 return c->slot && (c->slot->flags & flag); 97 } 98 99 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host) 100 { 101 u8 reg; 102 103 reg = sdhci_readb(host, SDHCI_POWER_CONTROL); 104 reg |= 0x10; 105 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 106 /* For eMMC, minimum is 1us but give it 9us for good measure */ 107 udelay(9); 108 reg &= ~0x10; 109 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); 110 /* For eMMC, minimum is 200us but give it 300us for good measure */ 111 usleep_range(300, 1000); 112 } 113 114 static const struct sdhci_ops sdhci_acpi_ops_dflt = { 115 .set_clock = sdhci_set_clock, 116 .set_bus_width = sdhci_set_bus_width, 117 .reset = sdhci_reset, 118 .set_uhs_signaling = sdhci_set_uhs_signaling, 119 }; 120 121 static const struct sdhci_ops sdhci_acpi_ops_int = { 122 .set_clock = sdhci_set_clock, 123 .set_bus_width = sdhci_set_bus_width, 124 .reset = sdhci_reset, 125 .set_uhs_signaling = sdhci_set_uhs_signaling, 126 .hw_reset = sdhci_acpi_int_hw_reset, 127 }; 128 129 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { 130 .ops = &sdhci_acpi_ops_int, 131 }; 132 133 #ifdef CONFIG_X86 134 135 static bool sdhci_acpi_byt(void) 136 { 137 static const struct x86_cpu_id byt[] = { 138 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 }, 139 {} 140 }; 141 142 return x86_match_cpu(byt); 143 } 144 145 static bool sdhci_acpi_cht(void) 146 { 147 static const struct x86_cpu_id cht[] = { 148 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, 149 {} 150 }; 151 152 return x86_match_cpu(cht); 153 } 154 155 #define BYT_IOSF_SCCEP 0x63 156 #define BYT_IOSF_OCP_NETCTRL0 0x1078 157 #define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8) 158 159 static void sdhci_acpi_byt_setting(struct device *dev) 160 { 161 u32 val = 0; 162 163 if (!sdhci_acpi_byt()) 164 return; 165 166 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0, 167 &val)) { 168 dev_err(dev, "%s read error\n", __func__); 169 return; 170 } 171 172 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE)) 173 return; 174 175 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE; 176 177 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0, 178 val)) { 179 dev_err(dev, "%s write error\n", __func__); 180 return; 181 } 182 183 dev_dbg(dev, "%s completed\n", __func__); 184 } 185 186 static bool sdhci_acpi_byt_defer(struct device *dev) 187 { 188 if (!sdhci_acpi_byt()) 189 return false; 190 191 if (!iosf_mbi_available()) 192 return true; 193 194 sdhci_acpi_byt_setting(dev); 195 196 return false; 197 } 198 199 static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device, 200 unsigned int slot, unsigned int parent_slot) 201 { 202 struct pci_dev *dev, *parent, *from = NULL; 203 204 while (1) { 205 dev = pci_get_device(vendor, device, from); 206 pci_dev_put(from); 207 if (!dev) 208 break; 209 parent = pci_upstream_bridge(dev); 210 if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot && 211 parent && PCI_SLOT(parent->devfn) == parent_slot && 212 !pci_upstream_bridge(parent)) { 213 pci_dev_put(dev); 214 return true; 215 } 216 from = dev; 217 } 218 219 return false; 220 } 221 222 /* 223 * GPDwin uses PCI wifi which conflicts with SDIO's use of 224 * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is 225 * problematic, but since SDIO is only used for wifi, the presence of the PCI 226 * wifi card in the expected slot with an ACPI companion node, is used to 227 * indicate that acpi_device_fix_up_power() should be avoided. 228 */ 229 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid, 230 const char *uid) 231 { 232 return sdhci_acpi_cht() && 233 !strcmp(hid, "80860F14") && 234 !strcmp(uid, "2") && 235 sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28); 236 } 237 238 #else 239 240 static inline void sdhci_acpi_byt_setting(struct device *dev) 241 { 242 } 243 244 static inline bool sdhci_acpi_byt_defer(struct device *dev) 245 { 246 return false; 247 } 248 249 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid, 250 const char *uid) 251 { 252 return false; 253 } 254 255 #endif 256 257 static int bxt_get_cd(struct mmc_host *mmc) 258 { 259 int gpio_cd = mmc_gpio_get_cd(mmc); 260 struct sdhci_host *host = mmc_priv(mmc); 261 unsigned long flags; 262 int ret = 0; 263 264 if (!gpio_cd) 265 return 0; 266 267 spin_lock_irqsave(&host->lock, flags); 268 269 if (host->flags & SDHCI_DEVICE_DEAD) 270 goto out; 271 272 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 273 out: 274 spin_unlock_irqrestore(&host->lock, flags); 275 276 return ret; 277 } 278 279 static int intel_probe_slot(struct platform_device *pdev, const char *hid, 280 const char *uid) 281 { 282 struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 283 struct sdhci_host *host = c->host; 284 285 if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") && 286 sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 && 287 sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807) 288 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */ 289 290 if (hid && !strcmp(hid, "80865ACA")) 291 host->mmc_host_ops.get_cd = bxt_get_cd; 292 293 return 0; 294 } 295 296 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { 297 .chip = &sdhci_acpi_chip_int, 298 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | 299 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR | 300 MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY, 301 .flags = SDHCI_ACPI_RUNTIME_PM, 302 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 303 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | 304 SDHCI_QUIRK2_STOP_WITH_TC | 305 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400, 306 .probe_slot = intel_probe_slot, 307 }; 308 309 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { 310 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | 311 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 312 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON, 313 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD | 314 MMC_CAP_WAIT_WHILE_BUSY, 315 .flags = SDHCI_ACPI_RUNTIME_PM, 316 .pm_caps = MMC_PM_KEEP_POWER, 317 .probe_slot = intel_probe_slot, 318 }; 319 320 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { 321 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | 322 SDHCI_ACPI_RUNTIME_PM, 323 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, 324 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | 325 SDHCI_QUIRK2_STOP_WITH_TC, 326 .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM, 327 .probe_slot = intel_probe_slot, 328 }; 329 330 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = { 331 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 332 .quirks2 = SDHCI_QUIRK2_NO_1_8_V, 333 .caps = MMC_CAP_NONREMOVABLE, 334 }; 335 336 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = { 337 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, 338 .caps = MMC_CAP_NONREMOVABLE, 339 }; 340 341 struct sdhci_acpi_uid_slot { 342 const char *hid; 343 const char *uid; 344 const struct sdhci_acpi_slot *slot; 345 }; 346 347 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = { 348 { "80865ACA", NULL, &sdhci_acpi_slot_int_sd }, 349 { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc }, 350 { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio }, 351 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc }, 352 { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio }, 353 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd }, 354 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, 355 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, 356 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd }, 357 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, 358 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, 359 { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio }, 360 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd }, 361 { "PNP0D40" }, 362 { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v }, 363 { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd }, 364 { }, 365 }; 366 367 static const struct acpi_device_id sdhci_acpi_ids[] = { 368 { "80865ACA" }, 369 { "80865ACC" }, 370 { "80865AD0" }, 371 { "80860F14" }, 372 { "80860F16" }, 373 { "INT33BB" }, 374 { "INT33C6" }, 375 { "INT3436" }, 376 { "INT344D" }, 377 { "PNP0D40" }, 378 { "QCOM8051" }, 379 { "QCOM8052" }, 380 { }, 381 }; 382 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); 383 384 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid, 385 const char *uid) 386 { 387 const struct sdhci_acpi_uid_slot *u; 388 389 for (u = sdhci_acpi_uids; u->hid; u++) { 390 if (strcmp(u->hid, hid)) 391 continue; 392 if (!u->uid) 393 return u->slot; 394 if (uid && !strcmp(u->uid, uid)) 395 return u->slot; 396 } 397 return NULL; 398 } 399 400 static int sdhci_acpi_probe(struct platform_device *pdev) 401 { 402 struct device *dev = &pdev->dev; 403 const struct sdhci_acpi_slot *slot; 404 struct acpi_device *device, *child; 405 struct sdhci_acpi_host *c; 406 struct sdhci_host *host; 407 struct resource *iomem; 408 resource_size_t len; 409 size_t priv_size; 410 const char *hid; 411 const char *uid; 412 int err; 413 414 device = ACPI_COMPANION(dev); 415 if (!device) 416 return -ENODEV; 417 418 hid = acpi_device_hid(device); 419 uid = acpi_device_uid(device); 420 421 slot = sdhci_acpi_get_slot(hid, uid); 422 423 /* Power on the SDHCI controller and its children */ 424 acpi_device_fix_up_power(device); 425 if (!sdhci_acpi_no_fixup_child_power(hid, uid)) { 426 list_for_each_entry(child, &device->children, node) 427 if (child->status.present && child->status.enabled) 428 acpi_device_fix_up_power(child); 429 } 430 431 if (sdhci_acpi_byt_defer(dev)) 432 return -EPROBE_DEFER; 433 434 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 435 if (!iomem) 436 return -ENOMEM; 437 438 len = resource_size(iomem); 439 if (len < 0x100) 440 dev_err(dev, "Invalid iomem size!\n"); 441 442 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev))) 443 return -ENOMEM; 444 445 priv_size = slot ? slot->priv_size : 0; 446 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size); 447 if (IS_ERR(host)) 448 return PTR_ERR(host); 449 450 c = sdhci_priv(host); 451 c->host = host; 452 c->slot = slot; 453 c->pdev = pdev; 454 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM); 455 456 platform_set_drvdata(pdev, c); 457 458 host->hw_name = "ACPI"; 459 host->ops = &sdhci_acpi_ops_dflt; 460 host->irq = platform_get_irq(pdev, 0); 461 462 host->ioaddr = devm_ioremap_nocache(dev, iomem->start, 463 resource_size(iomem)); 464 if (host->ioaddr == NULL) { 465 err = -ENOMEM; 466 goto err_free; 467 } 468 469 if (c->slot) { 470 if (c->slot->probe_slot) { 471 err = c->slot->probe_slot(pdev, hid, uid); 472 if (err) 473 goto err_free; 474 } 475 if (c->slot->chip) { 476 host->ops = c->slot->chip->ops; 477 host->quirks |= c->slot->chip->quirks; 478 host->quirks2 |= c->slot->chip->quirks2; 479 host->mmc->caps |= c->slot->chip->caps; 480 host->mmc->caps2 |= c->slot->chip->caps2; 481 host->mmc->pm_caps |= c->slot->chip->pm_caps; 482 } 483 host->quirks |= c->slot->quirks; 484 host->quirks2 |= c->slot->quirks2; 485 host->mmc->caps |= c->slot->caps; 486 host->mmc->caps2 |= c->slot->caps2; 487 host->mmc->pm_caps |= c->slot->pm_caps; 488 } 489 490 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; 491 492 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { 493 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); 494 495 err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL); 496 if (err) { 497 if (err == -EPROBE_DEFER) 498 goto err_free; 499 dev_warn(dev, "failed to setup card detect gpio\n"); 500 c->use_runtime_pm = false; 501 } 502 } 503 504 err = sdhci_add_host(host); 505 if (err) 506 goto err_free; 507 508 if (c->use_runtime_pm) { 509 pm_runtime_set_active(dev); 510 pm_suspend_ignore_children(dev, 1); 511 pm_runtime_set_autosuspend_delay(dev, 50); 512 pm_runtime_use_autosuspend(dev); 513 pm_runtime_enable(dev); 514 } 515 516 device_enable_async_suspend(dev); 517 518 return 0; 519 520 err_free: 521 sdhci_free_host(c->host); 522 return err; 523 } 524 525 static int sdhci_acpi_remove(struct platform_device *pdev) 526 { 527 struct sdhci_acpi_host *c = platform_get_drvdata(pdev); 528 struct device *dev = &pdev->dev; 529 int dead; 530 531 if (c->use_runtime_pm) { 532 pm_runtime_get_sync(dev); 533 pm_runtime_disable(dev); 534 pm_runtime_put_noidle(dev); 535 } 536 537 if (c->slot && c->slot->remove_slot) 538 c->slot->remove_slot(pdev); 539 540 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); 541 sdhci_remove_host(c->host, dead); 542 sdhci_free_host(c->host); 543 544 return 0; 545 } 546 547 #ifdef CONFIG_PM_SLEEP 548 549 static int sdhci_acpi_suspend(struct device *dev) 550 { 551 struct sdhci_acpi_host *c = dev_get_drvdata(dev); 552 struct sdhci_host *host = c->host; 553 554 if (host->tuning_mode != SDHCI_TUNING_MODE_3) 555 mmc_retune_needed(host->mmc); 556 557 return sdhci_suspend_host(host); 558 } 559 560 static int sdhci_acpi_resume(struct device *dev) 561 { 562 struct sdhci_acpi_host *c = dev_get_drvdata(dev); 563 564 sdhci_acpi_byt_setting(&c->pdev->dev); 565 566 return sdhci_resume_host(c->host); 567 } 568 569 #endif 570 571 #ifdef CONFIG_PM 572 573 static int sdhci_acpi_runtime_suspend(struct device *dev) 574 { 575 struct sdhci_acpi_host *c = dev_get_drvdata(dev); 576 struct sdhci_host *host = c->host; 577 578 if (host->tuning_mode != SDHCI_TUNING_MODE_3) 579 mmc_retune_needed(host->mmc); 580 581 return sdhci_runtime_suspend_host(host); 582 } 583 584 static int sdhci_acpi_runtime_resume(struct device *dev) 585 { 586 struct sdhci_acpi_host *c = dev_get_drvdata(dev); 587 588 sdhci_acpi_byt_setting(&c->pdev->dev); 589 590 return sdhci_runtime_resume_host(c->host); 591 } 592 593 #endif 594 595 static const struct dev_pm_ops sdhci_acpi_pm_ops = { 596 SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume) 597 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend, 598 sdhci_acpi_runtime_resume, NULL) 599 }; 600 601 static struct platform_driver sdhci_acpi_driver = { 602 .driver = { 603 .name = "sdhci-acpi", 604 .acpi_match_table = sdhci_acpi_ids, 605 .pm = &sdhci_acpi_pm_ops, 606 }, 607 .probe = sdhci_acpi_probe, 608 .remove = sdhci_acpi_remove, 609 }; 610 611 module_platform_driver(sdhci_acpi_driver); 612 613 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); 614 MODULE_AUTHOR("Adrian Hunter"); 615 MODULE_LICENSE("GPL v2"); 616