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