1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-plat.c - xHCI host controller driver platform Bus Glue.
5 * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com
8 * A lot of code borrowed from the Linux xHCI driver.
11 #include <linux/clk.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/pci.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>
25 #include "xhci-plat.h"
26 #include "xhci-mvebu.h"
28 static struct hc_driver __read_mostly xhci_plat_hc_driver;
30 static int xhci_plat_setup(struct usb_hcd *hcd);
31 static int xhci_plat_start(struct usb_hcd *hcd);
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,
39 static void xhci_priv_plat_start(struct usb_hcd *hcd)
41 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
44 priv->plat_start(hcd);
47 static int xhci_priv_init_quirk(struct usb_hcd *hcd)
49 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
51 if (!priv->init_quirk)
54 return priv->init_quirk(hcd);
57 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
59 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
61 if (!priv->suspend_quirk)
64 return priv->suspend_quirk(hcd);
67 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
69 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
71 if (!priv->resume_quirk)
74 return priv->resume_quirk(hcd);
77 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
79 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
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
86 xhci->quirks |= XHCI_PLAT | priv->quirks;
89 /* called during probe() after chip reset completes */
90 static int xhci_plat_setup(struct usb_hcd *hcd)
95 ret = xhci_priv_init_quirk(hcd);
99 return xhci_gen_setup(hcd, xhci_plat_quirks);
102 static int xhci_plat_start(struct usb_hcd *hcd)
104 xhci_priv_plat_start(hcd);
105 return xhci_run(hcd);
109 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
110 .init_quirk = xhci_mvebu_mbus_init_quirk,
113 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
114 .init_quirk = xhci_mvebu_a3700_init_quirk,
117 static const struct xhci_plat_priv xhci_plat_brcm = {
118 .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
121 static const struct of_device_id usb_xhci_of_match[] = {
123 .compatible = "generic-xhci",
125 .compatible = "xhci-platform",
127 .compatible = "marvell,armada-375-xhci",
128 .data = &xhci_plat_marvell_armada,
130 .compatible = "marvell,armada-380-xhci",
131 .data = &xhci_plat_marvell_armada,
133 .compatible = "marvell,armada3700-xhci",
134 .data = &xhci_plat_marvell_armada3700,
136 .compatible = "brcm,xhci-brcm-v2",
137 .data = &xhci_plat_brcm,
139 .compatible = "brcm,bcm7445-xhci",
140 .data = &xhci_plat_brcm,
144 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
147 int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match)
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;
156 struct xhci_plat_priv *priv = NULL;
162 driver = &xhci_plat_hc_driver;
164 irq = platform_get_irq(pdev, 0);
171 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
175 pm_runtime_set_active(&pdev->dev);
176 pm_runtime_enable(&pdev->dev);
177 pm_runtime_get_noresume(&pdev->dev);
179 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
180 dev_name(&pdev->dev), NULL);
183 goto disable_runtime;
186 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
187 if (IS_ERR(hcd->regs)) {
188 ret = PTR_ERR(hcd->regs);
192 hcd->rsrc_start = res->start;
193 hcd->rsrc_len = resource_size(res);
195 xhci = hcd_to_xhci(hcd);
197 xhci->allow_single_roothub = 1;
200 * Not all platforms have clks so it is not an error if the
201 * clock do not exist.
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);
209 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
210 if (IS_ERR(xhci->clk)) {
211 ret = PTR_ERR(xhci->clk);
215 xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev);
216 if (IS_ERR(xhci->reset)) {
217 ret = PTR_ERR(xhci->reset);
221 ret = reset_control_deassert(xhci->reset);
225 ret = clk_prepare_enable(xhci->reg_clk);
229 ret = clk_prepare_enable(xhci->clk);
231 goto disable_reg_clk;
234 priv = hcd_to_xhci_priv(hcd);
235 /* Just copy data for now */
239 device_set_wakeup_capable(&pdev->dev, true);
241 xhci->main_hcd = hcd;
243 /* imod_interval is the interrupt moderation value in nanoseconds. */
244 xhci->imod_interval = 40000;
246 /* Iterate over all parent nodes for finding quirks */
247 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
249 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
250 xhci->quirks |= XHCI_HW_LPM_DISABLE;
252 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
253 xhci->quirks |= XHCI_LPM_SUPPORT;
255 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
256 xhci->quirks |= XHCI_BROKEN_PORT_PED;
258 device_property_read_u32(tmpdev, "imod-interval-ns",
259 &xhci->imod_interval);
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)
269 ret = usb_phy_init(hcd->usb_phy);
274 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
276 if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT))
277 hcd->skip_phy_initialization = 1;
279 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
280 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
282 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
284 goto disable_usb_phy;
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) {
291 goto dealloc_usb2_hcd;
294 xhci->shared_hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev,
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",
300 (int)PTR_ERR(xhci->shared_hcd->usb_phy));
301 xhci->shared_hcd->usb_phy = NULL;
303 ret = usb_phy_init(xhci->shared_hcd->usb_phy);
305 dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n",
309 xhci->shared_hcd->tpl_support = hcd->tpl_support;
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;
316 if (xhci->shared_hcd) {
317 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
322 device_enable_async_suspend(&pdev->dev);
323 pm_runtime_put_noidle(&pdev->dev);
326 * Prevent runtime pm from being on as default, users should enable
327 * runtime pm using power/control in sysfs.
329 pm_runtime_forbid(&pdev->dev);
335 usb_put_hcd(xhci->shared_hcd);
341 usb_phy_shutdown(hcd->usb_phy);
344 clk_disable_unprepare(xhci->clk);
347 clk_disable_unprepare(xhci->reg_clk);
350 reset_control_assert(xhci->reset);
356 pm_runtime_put_noidle(&pdev->dev);
357 pm_runtime_disable(&pdev->dev);
361 EXPORT_SYMBOL_GPL(xhci_plat_probe);
363 static int xhci_generic_plat_probe(struct platform_device *pdev)
365 const struct xhci_plat_priv *priv_match;
366 struct device *sysdev;
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)
376 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
377 if (is_of_node(sysdev->fwnode) ||
378 is_acpi_device_node(sysdev->fwnode))
380 else if (dev_is_pci(sysdev))
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));
394 if (pdev->dev.of_node)
395 priv_match = of_device_get_match_data(&pdev->dev);
397 priv_match = dev_get_platdata(&pdev->dev);
399 return xhci_plat_probe(pdev, sysdev, priv_match);
402 int xhci_plat_remove(struct platform_device *dev)
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;
410 pm_runtime_get_sync(&dev->dev);
411 xhci->xhc_state |= XHCI_STATE_REMOVING;
414 usb_remove_hcd(shared_hcd);
415 xhci->shared_hcd = NULL;
418 usb_phy_shutdown(hcd->usb_phy);
423 usb_put_hcd(shared_hcd);
425 clk_disable_unprepare(clk);
426 clk_disable_unprepare(reg_clk);
427 reset_control_assert(xhci->reset);
430 pm_runtime_disable(&dev->dev);
431 pm_runtime_put_noidle(&dev->dev);
432 pm_runtime_set_suspended(&dev->dev);
436 EXPORT_SYMBOL_GPL(xhci_plat_remove);
438 static int __maybe_unused xhci_plat_suspend(struct device *dev)
440 struct usb_hcd *hcd = dev_get_drvdata(dev);
441 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
444 if (pm_runtime_suspended(dev))
445 pm_runtime_resume(dev);
447 ret = xhci_priv_suspend_quirk(hcd);
451 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
452 * to do wakeup during suspend.
454 ret = xhci_suspend(xhci, device_may_wakeup(dev));
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);
466 static int __maybe_unused xhci_plat_resume(struct device *dev)
468 struct usb_hcd *hcd = dev_get_drvdata(dev);
469 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
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);
477 ret = xhci_priv_resume_quirk(hcd);
481 ret = xhci_resume(xhci, 0);
485 pm_runtime_disable(dev);
486 pm_runtime_set_active(dev);
487 pm_runtime_enable(dev);
492 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
494 struct usb_hcd *hcd = dev_get_drvdata(dev);
495 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
498 ret = xhci_priv_suspend_quirk(hcd);
502 return xhci_suspend(xhci, true);
505 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
507 struct usb_hcd *hcd = dev_get_drvdata(dev);
508 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
510 return xhci_resume(xhci, 0);
513 const struct dev_pm_ops xhci_plat_pm_ops = {
514 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
516 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
517 xhci_plat_runtime_resume,
520 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
523 static const struct acpi_device_id usb_xhci_acpi_match[] = {
524 /* XHCI-compliant USB Controller */
528 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
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,
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),
542 MODULE_ALIAS("platform:xhci-hcd");
544 static int __init xhci_plat_init(void)
546 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
547 return platform_driver_register(&usb_generic_xhci_driver);
549 module_init(xhci_plat_init);
551 static void __exit xhci_plat_exit(void)
553 platform_driver_unregister(&usb_generic_xhci_driver);
555 module_exit(xhci_plat_exit);
557 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
558 MODULE_LICENSE("GPL");