1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * xhci-plat.c - xHCI host controller driver platform Bus Glue. 4 * 5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com 6 * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de> 7 * 8 * A lot of code borrowed from the Linux xHCI driver. 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/module.h> 14 #include <linux/pci.h> 15 #include <linux/of.h> 16 #include <linux/of_device.h> 17 #include <linux/platform_device.h> 18 #include <linux/usb/phy.h> 19 #include <linux/slab.h> 20 #include <linux/acpi.h> 21 #include <linux/usb/of.h> 22 #include <linux/reset.h> 23 24 #include "xhci.h" 25 #include "xhci-plat.h" 26 #include "xhci-mvebu.h" 27 28 static struct hc_driver __read_mostly xhci_plat_hc_driver; 29 30 static int xhci_plat_setup(struct usb_hcd *hcd); 31 static int xhci_plat_start(struct usb_hcd *hcd); 32 33 static const struct xhci_driver_overrides xhci_plat_overrides __initconst = { 34 .extra_priv_size = sizeof(struct xhci_plat_priv), 35 .reset = xhci_plat_setup, 36 .start = xhci_plat_start, 37 }; 38 39 static void xhci_priv_plat_start(struct usb_hcd *hcd) 40 { 41 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd); 42 43 if (priv->plat_start) 44 priv->plat_start(hcd); 45 } 46 47 static int xhci_priv_init_quirk(struct usb_hcd *hcd) 48 { 49 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd); 50 51 if (!priv->init_quirk) 52 return 0; 53 54 return priv->init_quirk(hcd); 55 } 56 57 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd) 58 { 59 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd); 60 61 if (!priv->suspend_quirk) 62 return 0; 63 64 return priv->suspend_quirk(hcd); 65 } 66 67 static int xhci_priv_resume_quirk(struct usb_hcd *hcd) 68 { 69 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd); 70 71 if (!priv->resume_quirk) 72 return 0; 73 74 return priv->resume_quirk(hcd); 75 } 76 77 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci) 78 { 79 struct xhci_plat_priv *priv = xhci_to_priv(xhci); 80 81 /* 82 * As of now platform drivers don't provide MSI support so we ensure 83 * here that the generic code does not try to make a pci_dev from our 84 * dev struct in order to setup MSI 85 */ 86 xhci->quirks |= XHCI_PLAT | priv->quirks; 87 } 88 89 /* called during probe() after chip reset completes */ 90 static int xhci_plat_setup(struct usb_hcd *hcd) 91 { 92 int ret; 93 94 95 ret = xhci_priv_init_quirk(hcd); 96 if (ret) 97 return ret; 98 99 return xhci_gen_setup(hcd, xhci_plat_quirks); 100 } 101 102 static int xhci_plat_start(struct usb_hcd *hcd) 103 { 104 xhci_priv_plat_start(hcd); 105 return xhci_run(hcd); 106 } 107 108 #ifdef CONFIG_OF 109 static const struct xhci_plat_priv xhci_plat_marvell_armada = { 110 .init_quirk = xhci_mvebu_mbus_init_quirk, 111 }; 112 113 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = { 114 .init_quirk = xhci_mvebu_a3700_init_quirk, 115 }; 116 117 static const struct xhci_plat_priv xhci_plat_brcm = { 118 .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS, 119 }; 120 121 static const struct of_device_id usb_xhci_of_match[] = { 122 { 123 .compatible = "generic-xhci", 124 }, { 125 .compatible = "xhci-platform", 126 }, { 127 .compatible = "marvell,armada-375-xhci", 128 .data = &xhci_plat_marvell_armada, 129 }, { 130 .compatible = "marvell,armada-380-xhci", 131 .data = &xhci_plat_marvell_armada, 132 }, { 133 .compatible = "marvell,armada3700-xhci", 134 .data = &xhci_plat_marvell_armada3700, 135 }, { 136 .compatible = "brcm,xhci-brcm-v2", 137 .data = &xhci_plat_brcm, 138 }, { 139 .compatible = "brcm,bcm7445-xhci", 140 .data = &xhci_plat_brcm, 141 }, 142 {}, 143 }; 144 MODULE_DEVICE_TABLE(of, usb_xhci_of_match); 145 #endif 146 147 int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match) 148 { 149 const struct hc_driver *driver; 150 struct device *tmpdev; 151 struct xhci_hcd *xhci; 152 struct resource *res; 153 struct usb_hcd *hcd, *usb3_hcd; 154 int ret; 155 int irq; 156 struct xhci_plat_priv *priv = NULL; 157 158 159 if (usb_disabled()) 160 return -ENODEV; 161 162 driver = &xhci_plat_hc_driver; 163 164 irq = platform_get_irq(pdev, 0); 165 if (irq < 0) 166 return irq; 167 168 if (!sysdev) 169 sysdev = &pdev->dev; 170 171 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64)); 172 if (ret) 173 return ret; 174 175 pm_runtime_set_active(&pdev->dev); 176 pm_runtime_enable(&pdev->dev); 177 pm_runtime_get_noresume(&pdev->dev); 178 179 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev, 180 dev_name(&pdev->dev), NULL); 181 if (!hcd) { 182 ret = -ENOMEM; 183 goto disable_runtime; 184 } 185 186 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 187 if (IS_ERR(hcd->regs)) { 188 ret = PTR_ERR(hcd->regs); 189 goto put_hcd; 190 } 191 192 hcd->rsrc_start = res->start; 193 hcd->rsrc_len = resource_size(res); 194 195 xhci = hcd_to_xhci(hcd); 196 197 xhci->allow_single_roothub = 1; 198 199 /* 200 * Not all platforms have clks so it is not an error if the 201 * clock do not exist. 202 */ 203 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg"); 204 if (IS_ERR(xhci->reg_clk)) { 205 ret = PTR_ERR(xhci->reg_clk); 206 goto put_hcd; 207 } 208 209 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL); 210 if (IS_ERR(xhci->clk)) { 211 ret = PTR_ERR(xhci->clk); 212 goto put_hcd; 213 } 214 215 xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev); 216 if (IS_ERR(xhci->reset)) { 217 ret = PTR_ERR(xhci->reset); 218 goto put_hcd; 219 } 220 221 ret = reset_control_deassert(xhci->reset); 222 if (ret) 223 goto put_hcd; 224 225 ret = clk_prepare_enable(xhci->reg_clk); 226 if (ret) 227 goto err_reset; 228 229 ret = clk_prepare_enable(xhci->clk); 230 if (ret) 231 goto disable_reg_clk; 232 233 if (priv_match) { 234 priv = hcd_to_xhci_priv(hcd); 235 /* Just copy data for now */ 236 *priv = *priv_match; 237 } 238 239 device_set_wakeup_capable(&pdev->dev, true); 240 241 xhci->main_hcd = hcd; 242 243 /* imod_interval is the interrupt moderation value in nanoseconds. */ 244 xhci->imod_interval = 40000; 245 246 /* Iterate over all parent nodes for finding quirks */ 247 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) { 248 249 if (device_property_read_bool(tmpdev, "usb2-lpm-disable")) 250 xhci->quirks |= XHCI_HW_LPM_DISABLE; 251 252 if (device_property_read_bool(tmpdev, "usb3-lpm-capable")) 253 xhci->quirks |= XHCI_LPM_SUPPORT; 254 255 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped")) 256 xhci->quirks |= XHCI_BROKEN_PORT_PED; 257 258 device_property_read_u32(tmpdev, "imod-interval-ns", 259 &xhci->imod_interval); 260 } 261 262 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0); 263 if (IS_ERR(hcd->usb_phy)) { 264 ret = PTR_ERR(hcd->usb_phy); 265 if (ret == -EPROBE_DEFER) 266 goto disable_clk; 267 hcd->usb_phy = NULL; 268 } else { 269 ret = usb_phy_init(hcd->usb_phy); 270 if (ret) 271 goto disable_clk; 272 } 273 274 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node); 275 276 if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)) 277 hcd->skip_phy_initialization = 1; 278 279 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK)) 280 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK; 281 282 ret = usb_add_hcd(hcd, irq, IRQF_SHARED); 283 if (ret) 284 goto disable_usb_phy; 285 286 if (!xhci_has_one_roothub(xhci)) { 287 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev, 288 dev_name(&pdev->dev), hcd); 289 if (!xhci->shared_hcd) { 290 ret = -ENOMEM; 291 goto dealloc_usb2_hcd; 292 } 293 294 xhci->shared_hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, 295 "usb-phy", 1); 296 if (IS_ERR(xhci->shared_hcd->usb_phy)) { 297 if (PTR_ERR(xhci->shared_hcd->usb_phy) != -ENODEV) 298 dev_err(sysdev, "%s get usb3phy fail (ret=%d)\n", 299 __func__, 300 (int)PTR_ERR(xhci->shared_hcd->usb_phy)); 301 xhci->shared_hcd->usb_phy = NULL; 302 } else { 303 ret = usb_phy_init(xhci->shared_hcd->usb_phy); 304 if (ret) 305 dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n", 306 __func__, ret); 307 } 308 309 xhci->shared_hcd->tpl_support = hcd->tpl_support; 310 } 311 312 usb3_hcd = xhci_get_usb3_hcd(xhci); 313 if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4) 314 usb3_hcd->can_do_streams = 1; 315 316 if (xhci->shared_hcd) { 317 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); 318 if (ret) 319 goto put_usb3_hcd; 320 } 321 322 device_enable_async_suspend(&pdev->dev); 323 pm_runtime_put_noidle(&pdev->dev); 324 325 /* 326 * Prevent runtime pm from being on as default, users should enable 327 * runtime pm using power/control in sysfs. 328 */ 329 pm_runtime_forbid(&pdev->dev); 330 331 return 0; 332 333 334 put_usb3_hcd: 335 usb_put_hcd(xhci->shared_hcd); 336 337 dealloc_usb2_hcd: 338 usb_remove_hcd(hcd); 339 340 disable_usb_phy: 341 usb_phy_shutdown(hcd->usb_phy); 342 343 disable_clk: 344 clk_disable_unprepare(xhci->clk); 345 346 disable_reg_clk: 347 clk_disable_unprepare(xhci->reg_clk); 348 349 err_reset: 350 reset_control_assert(xhci->reset); 351 352 put_hcd: 353 usb_put_hcd(hcd); 354 355 disable_runtime: 356 pm_runtime_put_noidle(&pdev->dev); 357 pm_runtime_disable(&pdev->dev); 358 359 return ret; 360 } 361 EXPORT_SYMBOL_GPL(xhci_plat_probe); 362 363 static int xhci_generic_plat_probe(struct platform_device *pdev) 364 { 365 const struct xhci_plat_priv *priv_match; 366 struct device *sysdev; 367 int ret; 368 369 /* 370 * sysdev must point to a device that is known to the system firmware 371 * or PCI hardware. We handle these three cases here: 372 * 1. xhci_plat comes from firmware 373 * 2. xhci_plat is child of a device from firmware (dwc3-plat) 374 * 3. xhci_plat is grandchild of a pci device (dwc3-pci) 375 */ 376 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) { 377 if (is_of_node(sysdev->fwnode) || 378 is_acpi_device_node(sysdev->fwnode)) 379 break; 380 else if (dev_is_pci(sysdev)) 381 break; 382 } 383 384 if (!sysdev) 385 sysdev = &pdev->dev; 386 387 if (WARN_ON(!sysdev->dma_mask)) { 388 /* Platform did not initialize dma_mask */ 389 ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64)); 390 if (ret) 391 return ret; 392 } 393 394 if (pdev->dev.of_node) 395 priv_match = of_device_get_match_data(&pdev->dev); 396 else 397 priv_match = dev_get_platdata(&pdev->dev); 398 399 return xhci_plat_probe(pdev, sysdev, priv_match); 400 } 401 402 int xhci_plat_remove(struct platform_device *dev) 403 { 404 struct usb_hcd *hcd = platform_get_drvdata(dev); 405 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 406 struct clk *clk = xhci->clk; 407 struct clk *reg_clk = xhci->reg_clk; 408 struct usb_hcd *shared_hcd = xhci->shared_hcd; 409 410 pm_runtime_get_sync(&dev->dev); 411 xhci->xhc_state |= XHCI_STATE_REMOVING; 412 413 if (shared_hcd) { 414 usb_remove_hcd(shared_hcd); 415 xhci->shared_hcd = NULL; 416 } 417 418 usb_phy_shutdown(hcd->usb_phy); 419 420 usb_remove_hcd(hcd); 421 422 if (shared_hcd) 423 usb_put_hcd(shared_hcd); 424 425 clk_disable_unprepare(clk); 426 clk_disable_unprepare(reg_clk); 427 reset_control_assert(xhci->reset); 428 usb_put_hcd(hcd); 429 430 pm_runtime_disable(&dev->dev); 431 pm_runtime_put_noidle(&dev->dev); 432 pm_runtime_set_suspended(&dev->dev); 433 434 return 0; 435 } 436 EXPORT_SYMBOL_GPL(xhci_plat_remove); 437 438 static int __maybe_unused xhci_plat_suspend(struct device *dev) 439 { 440 struct usb_hcd *hcd = dev_get_drvdata(dev); 441 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 442 int ret; 443 444 if (pm_runtime_suspended(dev)) 445 pm_runtime_resume(dev); 446 447 ret = xhci_priv_suspend_quirk(hcd); 448 if (ret) 449 return ret; 450 /* 451 * xhci_suspend() needs `do_wakeup` to know whether host is allowed 452 * to do wakeup during suspend. 453 */ 454 ret = xhci_suspend(xhci, device_may_wakeup(dev)); 455 if (ret) 456 return ret; 457 458 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) { 459 clk_disable_unprepare(xhci->clk); 460 clk_disable_unprepare(xhci->reg_clk); 461 } 462 463 return 0; 464 } 465 466 static int __maybe_unused xhci_plat_resume(struct device *dev) 467 { 468 struct usb_hcd *hcd = dev_get_drvdata(dev); 469 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 470 int ret; 471 472 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) { 473 clk_prepare_enable(xhci->clk); 474 clk_prepare_enable(xhci->reg_clk); 475 } 476 477 ret = xhci_priv_resume_quirk(hcd); 478 if (ret) 479 return ret; 480 481 ret = xhci_resume(xhci, 0); 482 if (ret) 483 return ret; 484 485 pm_runtime_disable(dev); 486 pm_runtime_set_active(dev); 487 pm_runtime_enable(dev); 488 489 return 0; 490 } 491 492 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev) 493 { 494 struct usb_hcd *hcd = dev_get_drvdata(dev); 495 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 496 int ret; 497 498 ret = xhci_priv_suspend_quirk(hcd); 499 if (ret) 500 return ret; 501 502 return xhci_suspend(xhci, true); 503 } 504 505 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev) 506 { 507 struct usb_hcd *hcd = dev_get_drvdata(dev); 508 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 509 510 return xhci_resume(xhci, 0); 511 } 512 513 const struct dev_pm_ops xhci_plat_pm_ops = { 514 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume) 515 516 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend, 517 xhci_plat_runtime_resume, 518 NULL) 519 }; 520 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops); 521 522 #ifdef CONFIG_ACPI 523 static const struct acpi_device_id usb_xhci_acpi_match[] = { 524 /* XHCI-compliant USB Controller */ 525 { "PNP0D10", }, 526 { } 527 }; 528 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match); 529 #endif 530 531 static struct platform_driver usb_generic_xhci_driver = { 532 .probe = xhci_generic_plat_probe, 533 .remove = xhci_plat_remove, 534 .shutdown = usb_hcd_platform_shutdown, 535 .driver = { 536 .name = "xhci-hcd", 537 .pm = &xhci_plat_pm_ops, 538 .of_match_table = of_match_ptr(usb_xhci_of_match), 539 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match), 540 }, 541 }; 542 MODULE_ALIAS("platform:xhci-hcd"); 543 544 static int __init xhci_plat_init(void) 545 { 546 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides); 547 return platform_driver_register(&usb_generic_xhci_driver); 548 } 549 module_init(xhci_plat_init); 550 551 static void __exit xhci_plat_exit(void) 552 { 553 platform_driver_unregister(&usb_generic_xhci_driver); 554 } 555 module_exit(xhci_plat_exit); 556 557 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver"); 558 MODULE_LICENSE("GPL"); 559