1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Generic platform ehci driver 4 * 5 * Copyright 2007 Steven Brown <sbrown@cortland.com> 6 * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de> 7 * Copyright 2014 Hans de Goede <hdegoede@redhat.com> 8 * 9 * Derived from the ohci-ssb driver 10 * Copyright 2007 Michael Buesch <m@bues.ch> 11 * 12 * Derived from the EHCI-PCI driver 13 * Copyright (c) 2000-2004 by David Brownell 14 * 15 * Derived from the ohci-pci driver 16 * Copyright 1999 Roman Weissgaerber 17 * Copyright 2000-2002 David Brownell 18 * Copyright 1999 Linus Torvalds 19 * Copyright 1999 Gregory P. Smith 20 */ 21 #include <linux/acpi.h> 22 #include <linux/clk.h> 23 #include <linux/dma-mapping.h> 24 #include <linux/err.h> 25 #include <linux/kernel.h> 26 #include <linux/hrtimer.h> 27 #include <linux/io.h> 28 #include <linux/module.h> 29 #include <linux/of.h> 30 #include <linux/of_device.h> 31 #include <linux/platform_device.h> 32 #include <linux/reset.h> 33 #include <linux/sys_soc.h> 34 #include <linux/timer.h> 35 #include <linux/usb.h> 36 #include <linux/usb/hcd.h> 37 #include <linux/usb/ehci_pdriver.h> 38 #include <linux/usb/of.h> 39 40 #include "ehci.h" 41 42 #define DRIVER_DESC "EHCI generic platform driver" 43 #define EHCI_MAX_CLKS 4 44 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv) 45 46 #define BCM_USB_FIFO_THRESHOLD 0x00800040 47 48 struct ehci_platform_priv { 49 struct clk *clks[EHCI_MAX_CLKS]; 50 struct reset_control *rsts; 51 bool reset_on_resume; 52 bool quirk_poll; 53 struct timer_list poll_timer; 54 struct delayed_work poll_work; 55 }; 56 57 static int ehci_platform_reset(struct usb_hcd *hcd) 58 { 59 struct platform_device *pdev = to_platform_device(hcd->self.controller); 60 struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev); 61 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 62 int retval; 63 64 ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug; 65 66 if (pdata->pre_setup) { 67 retval = pdata->pre_setup(hcd); 68 if (retval < 0) 69 return retval; 70 } 71 72 ehci->caps = hcd->regs + pdata->caps_offset; 73 retval = ehci_setup(hcd); 74 if (retval) 75 return retval; 76 77 if (pdata->no_io_watchdog) 78 ehci->need_io_watchdog = 0; 79 80 if (of_device_is_compatible(pdev->dev.of_node, "brcm,xgs-iproc-ehci")) 81 ehci_writel(ehci, BCM_USB_FIFO_THRESHOLD, 82 &ehci->regs->brcm_insnreg[1]); 83 84 return 0; 85 } 86 87 static int ehci_platform_power_on(struct platform_device *dev) 88 { 89 struct usb_hcd *hcd = platform_get_drvdata(dev); 90 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd); 91 int clk, ret; 92 93 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) { 94 ret = clk_prepare_enable(priv->clks[clk]); 95 if (ret) 96 goto err_disable_clks; 97 } 98 99 return 0; 100 101 err_disable_clks: 102 while (--clk >= 0) 103 clk_disable_unprepare(priv->clks[clk]); 104 105 return ret; 106 } 107 108 static void ehci_platform_power_off(struct platform_device *dev) 109 { 110 struct usb_hcd *hcd = platform_get_drvdata(dev); 111 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd); 112 int clk; 113 114 for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--) 115 clk_disable_unprepare(priv->clks[clk]); 116 } 117 118 static struct hc_driver __read_mostly ehci_platform_hc_driver; 119 120 static const struct ehci_driver_overrides platform_overrides __initconst = { 121 .reset = ehci_platform_reset, 122 .extra_priv_size = sizeof(struct ehci_platform_priv), 123 }; 124 125 static struct usb_ehci_pdata ehci_platform_defaults = { 126 .power_on = ehci_platform_power_on, 127 .power_suspend = ehci_platform_power_off, 128 .power_off = ehci_platform_power_off, 129 }; 130 131 /** 132 * quirk_poll_check_port_status - Poll port_status if the device sticks 133 * @ehci: the ehci hcd pointer 134 * 135 * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting 136 * stuck very rarely after a full/low usb device was disconnected. To 137 * detect such a situation, the controllers require a special way which poll 138 * the EHCI PORTSC register. 139 * 140 * Return: true if the controller's port_status indicated getting stuck 141 */ 142 static bool quirk_poll_check_port_status(struct ehci_hcd *ehci) 143 { 144 u32 port_status = ehci_readl(ehci, &ehci->regs->port_status[0]); 145 146 if (!(port_status & PORT_OWNER) && 147 (port_status & PORT_POWER) && 148 !(port_status & PORT_CONNECT) && 149 (port_status & PORT_LS_MASK)) 150 return true; 151 152 return false; 153 } 154 155 /** 156 * quirk_poll_rebind_companion - rebind comanion device to recover 157 * @ehci: the ehci hcd pointer 158 * 159 * Since EHCI/OHCI controllers on R-Car Gen3 SoCs are possible to be getting 160 * stuck very rarely after a full/low usb device was disconnected. To 161 * recover from such a situation, the controllers require changing the OHCI 162 * functional state. 163 */ 164 static void quirk_poll_rebind_companion(struct ehci_hcd *ehci) 165 { 166 struct device *companion_dev; 167 struct usb_hcd *hcd = ehci_to_hcd(ehci); 168 169 companion_dev = usb_of_get_companion_dev(hcd->self.controller); 170 if (!companion_dev) 171 return; 172 173 device_release_driver(companion_dev); 174 if (device_attach(companion_dev) < 0) 175 ehci_err(ehci, "%s: failed\n", __func__); 176 177 put_device(companion_dev); 178 } 179 180 static void quirk_poll_work(struct work_struct *work) 181 { 182 struct ehci_platform_priv *priv = 183 container_of(to_delayed_work(work), struct ehci_platform_priv, 184 poll_work); 185 struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd, 186 priv); 187 188 /* check the status twice to reduce misdetection rate */ 189 if (!quirk_poll_check_port_status(ehci)) 190 return; 191 udelay(10); 192 if (!quirk_poll_check_port_status(ehci)) 193 return; 194 195 ehci_dbg(ehci, "%s: detected getting stuck. rebind now!\n", __func__); 196 quirk_poll_rebind_companion(ehci); 197 } 198 199 static void quirk_poll_timer(struct timer_list *t) 200 { 201 struct ehci_platform_priv *priv = timer_container_of(priv, t, 202 poll_timer); 203 struct ehci_hcd *ehci = container_of((void *)priv, struct ehci_hcd, 204 priv); 205 206 if (quirk_poll_check_port_status(ehci)) { 207 /* 208 * Now scheduling the work for testing the port more. Note that 209 * updating the status is possible to be delayed when 210 * reconnection. So, this uses delayed work with 5 ms delay 211 * to avoid misdetection. 212 */ 213 schedule_delayed_work(&priv->poll_work, msecs_to_jiffies(5)); 214 } 215 216 mod_timer(&priv->poll_timer, jiffies + HZ); 217 } 218 219 static void quirk_poll_init(struct ehci_platform_priv *priv) 220 { 221 INIT_DELAYED_WORK(&priv->poll_work, quirk_poll_work); 222 timer_setup(&priv->poll_timer, quirk_poll_timer, 0); 223 mod_timer(&priv->poll_timer, jiffies + HZ); 224 } 225 226 static void quirk_poll_end(struct ehci_platform_priv *priv) 227 { 228 timer_delete_sync(&priv->poll_timer); 229 cancel_delayed_work(&priv->poll_work); 230 } 231 232 static const struct soc_device_attribute quirk_poll_match[] = { 233 { .family = "R-Car Gen3" }, 234 { /* sentinel*/ } 235 }; 236 237 static int ehci_platform_probe(struct platform_device *dev) 238 { 239 struct usb_hcd *hcd; 240 struct resource *res_mem; 241 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev); 242 const struct of_device_id *match; 243 struct ehci_platform_priv *priv; 244 struct ehci_hcd *ehci; 245 int err, irq, clk = 0; 246 bool dma_mask_64; 247 248 if (usb_disabled()) 249 return -ENODEV; 250 251 /* 252 * Use reasonable defaults so platforms don't have to provide these 253 * with DT probing on ARM. 254 */ 255 if (!pdata) 256 pdata = &ehci_platform_defaults; 257 258 dma_mask_64 = pdata->dma_mask_64; 259 match = of_match_device(dev->dev.driver->of_match_table, &dev->dev); 260 if (match && match->data) 261 dma_mask_64 = true; 262 263 err = dma_coerce_mask_and_coherent(&dev->dev, 264 dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32)); 265 if (err) { 266 dev_err(&dev->dev, "Error: DMA mask configuration failed\n"); 267 return err; 268 } 269 270 irq = platform_get_irq(dev, 0); 271 if (irq < 0) 272 return irq; 273 274 hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev, 275 dev_name(&dev->dev)); 276 if (!hcd) 277 return -ENOMEM; 278 279 platform_set_drvdata(dev, hcd); 280 dev->dev.platform_data = pdata; 281 priv = hcd_to_ehci_priv(hcd); 282 ehci = hcd_to_ehci(hcd); 283 284 if (pdata == &ehci_platform_defaults && dev->dev.of_node) { 285 if (of_property_read_bool(dev->dev.of_node, "big-endian-regs")) 286 ehci->big_endian_mmio = 1; 287 288 if (of_property_read_bool(dev->dev.of_node, "big-endian-desc")) 289 ehci->big_endian_desc = 1; 290 291 if (of_property_read_bool(dev->dev.of_node, "big-endian")) 292 ehci->big_endian_mmio = ehci->big_endian_desc = 1; 293 294 if (of_property_read_bool(dev->dev.of_node, "spurious-oc")) 295 ehci->spurious_oc = 1; 296 297 if (of_property_read_bool(dev->dev.of_node, 298 "needs-reset-on-resume")) 299 priv->reset_on_resume = true; 300 301 if (of_property_read_bool(dev->dev.of_node, 302 "has-transaction-translator")) 303 hcd->has_tt = 1; 304 305 if (of_device_is_compatible(dev->dev.of_node, 306 "aspeed,ast2500-ehci") || 307 of_device_is_compatible(dev->dev.of_node, 308 "aspeed,ast2600-ehci") || 309 of_device_is_compatible(dev->dev.of_node, 310 "aspeed,ast2700-ehci")) 311 ehci->is_aspeed = 1; 312 313 if (soc_device_match(quirk_poll_match)) 314 priv->quirk_poll = true; 315 316 for (clk = 0; clk < EHCI_MAX_CLKS; clk++) { 317 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk); 318 if (IS_ERR(priv->clks[clk])) { 319 err = PTR_ERR(priv->clks[clk]); 320 if (err == -EPROBE_DEFER) 321 goto err_put_clks; 322 priv->clks[clk] = NULL; 323 break; 324 } 325 } 326 } 327 328 priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev); 329 if (IS_ERR(priv->rsts)) { 330 err = PTR_ERR(priv->rsts); 331 goto err_put_clks; 332 } 333 334 err = reset_control_deassert(priv->rsts); 335 if (err) 336 goto err_put_clks; 337 338 if (pdata->big_endian_desc) 339 ehci->big_endian_desc = 1; 340 if (pdata->big_endian_mmio) 341 ehci->big_endian_mmio = 1; 342 if (pdata->has_tt) 343 hcd->has_tt = 1; 344 if (pdata->reset_on_resume) 345 priv->reset_on_resume = true; 346 if (pdata->spurious_oc) 347 ehci->spurious_oc = 1; 348 349 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO 350 if (ehci->big_endian_mmio) { 351 dev_err(&dev->dev, 352 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n"); 353 err = -EINVAL; 354 goto err_reset; 355 } 356 #endif 357 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC 358 if (ehci->big_endian_desc) { 359 dev_err(&dev->dev, 360 "Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n"); 361 err = -EINVAL; 362 goto err_reset; 363 } 364 #endif 365 366 if (pdata->power_on) { 367 err = pdata->power_on(dev); 368 if (err < 0) 369 goto err_reset; 370 } 371 372 hcd->regs = devm_platform_get_and_ioremap_resource(dev, 0, &res_mem); 373 if (IS_ERR(hcd->regs)) { 374 err = PTR_ERR(hcd->regs); 375 goto err_power; 376 } 377 hcd->rsrc_start = res_mem->start; 378 hcd->rsrc_len = resource_size(res_mem); 379 380 hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node); 381 382 err = usb_add_hcd(hcd, irq, IRQF_SHARED); 383 if (err) 384 goto err_power; 385 386 device_wakeup_enable(hcd->self.controller); 387 device_enable_async_suspend(hcd->self.controller); 388 platform_set_drvdata(dev, hcd); 389 390 if (priv->quirk_poll) 391 quirk_poll_init(priv); 392 393 return err; 394 395 err_power: 396 if (pdata->power_off) 397 pdata->power_off(dev); 398 err_reset: 399 reset_control_assert(priv->rsts); 400 err_put_clks: 401 while (--clk >= 0) 402 clk_put(priv->clks[clk]); 403 404 if (pdata == &ehci_platform_defaults) 405 dev->dev.platform_data = NULL; 406 407 usb_put_hcd(hcd); 408 409 return err; 410 } 411 412 static void ehci_platform_remove(struct platform_device *dev) 413 { 414 struct usb_hcd *hcd = platform_get_drvdata(dev); 415 struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev); 416 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd); 417 int clk; 418 419 if (priv->quirk_poll) 420 quirk_poll_end(priv); 421 422 usb_remove_hcd(hcd); 423 424 if (pdata->power_off) 425 pdata->power_off(dev); 426 427 reset_control_assert(priv->rsts); 428 429 for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) 430 clk_put(priv->clks[clk]); 431 432 usb_put_hcd(hcd); 433 434 if (pdata == &ehci_platform_defaults) 435 dev->dev.platform_data = NULL; 436 } 437 438 static int __maybe_unused ehci_platform_suspend(struct device *dev) 439 { 440 struct usb_hcd *hcd = dev_get_drvdata(dev); 441 struct usb_ehci_pdata *pdata = dev_get_platdata(dev); 442 struct platform_device *pdev = to_platform_device(dev); 443 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd); 444 bool do_wakeup = device_may_wakeup(dev); 445 int ret; 446 447 if (priv->quirk_poll) 448 quirk_poll_end(priv); 449 450 ret = ehci_suspend(hcd, do_wakeup); 451 if (ret) 452 return ret; 453 454 if (pdata->power_suspend) 455 pdata->power_suspend(pdev); 456 457 ret = reset_control_assert(priv->rsts); 458 if (ret) { 459 if (pdata->power_on) 460 pdata->power_on(pdev); 461 462 ehci_resume(hcd, false); 463 464 if (priv->quirk_poll) 465 quirk_poll_init(priv); 466 } 467 468 return ret; 469 } 470 471 static int __maybe_unused ehci_platform_resume(struct device *dev) 472 { 473 struct usb_hcd *hcd = dev_get_drvdata(dev); 474 struct usb_ehci_pdata *pdata = dev_get_platdata(dev); 475 struct platform_device *pdev = to_platform_device(dev); 476 struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd); 477 struct device *companion_dev; 478 int err; 479 480 err = reset_control_deassert(priv->rsts); 481 if (err) 482 return err; 483 484 if (pdata->power_on) { 485 err = pdata->power_on(pdev); 486 if (err < 0) { 487 reset_control_assert(priv->rsts); 488 return err; 489 } 490 } 491 492 companion_dev = usb_of_get_companion_dev(hcd->self.controller); 493 if (companion_dev) { 494 device_pm_wait_for_dev(hcd->self.controller, companion_dev); 495 put_device(companion_dev); 496 } 497 498 ehci_resume(hcd, priv->reset_on_resume); 499 500 pm_runtime_disable(dev); 501 pm_runtime_set_active(dev); 502 pm_runtime_enable(dev); 503 504 if (priv->quirk_poll) 505 quirk_poll_init(priv); 506 507 return 0; 508 } 509 510 static const struct of_device_id vt8500_ehci_ids[] = { 511 { .compatible = "via,vt8500-ehci", }, 512 { .compatible = "wm,prizm-ehci", }, 513 { .compatible = "generic-ehci", }, 514 { .compatible = "cavium,octeon-6335-ehci", }, 515 { .compatible = "aspeed,ast2700-ehci", .data = (void *)1 }, 516 {} 517 }; 518 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids); 519 520 #ifdef CONFIG_ACPI 521 static const struct acpi_device_id ehci_acpi_match[] = { 522 { "PNP0D20", 0 }, /* EHCI controller without debug */ 523 { } 524 }; 525 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match); 526 #endif 527 528 static const struct platform_device_id ehci_platform_table[] = { 529 { "ehci-platform", 0 }, 530 { } 531 }; 532 MODULE_DEVICE_TABLE(platform, ehci_platform_table); 533 534 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend, 535 ehci_platform_resume); 536 537 static struct platform_driver ehci_platform_driver = { 538 .id_table = ehci_platform_table, 539 .probe = ehci_platform_probe, 540 .remove = ehci_platform_remove, 541 .shutdown = usb_hcd_platform_shutdown, 542 .driver = { 543 .name = "ehci-platform", 544 .pm = pm_ptr(&ehci_platform_pm_ops), 545 .of_match_table = vt8500_ehci_ids, 546 .acpi_match_table = ACPI_PTR(ehci_acpi_match), 547 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 548 } 549 }; 550 551 static int __init ehci_platform_init(void) 552 { 553 if (usb_disabled()) 554 return -ENODEV; 555 556 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides); 557 return platform_driver_register(&ehci_platform_driver); 558 } 559 module_init(ehci_platform_init); 560 561 static void __exit ehci_platform_cleanup(void) 562 { 563 platform_driver_unregister(&ehci_platform_driver); 564 } 565 module_exit(ehci_platform_cleanup); 566 567 MODULE_DESCRIPTION(DRIVER_DESC); 568 MODULE_AUTHOR("Hauke Mehrtens"); 569 MODULE_AUTHOR("Alan Stern"); 570 MODULE_LICENSE("GPL"); 571