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