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/platform_device.h>
17 #include <linux/usb/phy.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 #include <linux/usb/of.h>
21 #include <linux/reset.h>
24 #include "xhci-plat.h"
25 #include "xhci-mvebu.h"
27 static struct hc_driver __read_mostly xhci_plat_hc_driver;
29 static int xhci_plat_setup(struct usb_hcd *hcd);
30 static int xhci_plat_start(struct usb_hcd *hcd);
32 static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
33 .extra_priv_size = sizeof(struct xhci_plat_priv),
34 .reset = xhci_plat_setup,
35 .start = xhci_plat_start,
38 static void xhci_priv_plat_start(struct usb_hcd *hcd)
40 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
43 priv->plat_start(hcd);
46 static int xhci_priv_init_quirk(struct usb_hcd *hcd)
48 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
50 if (!priv->init_quirk)
53 return priv->init_quirk(hcd);
56 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
58 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
60 if (!priv->suspend_quirk)
63 return priv->suspend_quirk(hcd);
66 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
68 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
70 if (!priv->resume_quirk)
73 return priv->resume_quirk(hcd);
76 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
78 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
80 xhci->quirks |= priv->quirks;
83 /* called during probe() after chip reset completes */
84 static int xhci_plat_setup(struct usb_hcd *hcd)
89 ret = xhci_priv_init_quirk(hcd);
93 return xhci_gen_setup(hcd, xhci_plat_quirks);
96 static int xhci_plat_start(struct usb_hcd *hcd)
98 xhci_priv_plat_start(hcd);
103 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
104 .init_quirk = xhci_mvebu_mbus_init_quirk,
107 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
108 .init_quirk = xhci_mvebu_a3700_init_quirk,
111 static const struct xhci_plat_priv xhci_plat_brcm = {
112 .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
115 static const struct of_device_id usb_xhci_of_match[] = {
117 .compatible = "generic-xhci",
119 .compatible = "xhci-platform",
121 .compatible = "marvell,armada-375-xhci",
122 .data = &xhci_plat_marvell_armada,
124 .compatible = "marvell,armada-380-xhci",
125 .data = &xhci_plat_marvell_armada,
127 .compatible = "marvell,armada3700-xhci",
128 .data = &xhci_plat_marvell_armada3700,
130 .compatible = "brcm,xhci-brcm-v2",
131 .data = &xhci_plat_brcm,
133 .compatible = "brcm,bcm7445-xhci",
134 .data = &xhci_plat_brcm,
138 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
141 int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match)
143 const struct hc_driver *driver;
144 struct device *tmpdev;
145 struct xhci_hcd *xhci;
146 struct resource *res;
147 struct usb_hcd *hcd, *usb3_hcd;
150 struct xhci_plat_priv *priv = NULL;
156 driver = &xhci_plat_hc_driver;
158 irq = platform_get_irq(pdev, 0);
165 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
169 pm_runtime_set_active(&pdev->dev);
170 pm_runtime_enable(&pdev->dev);
171 pm_runtime_get_noresume(&pdev->dev);
173 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
174 dev_name(&pdev->dev), NULL);
177 goto disable_runtime;
180 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
181 if (IS_ERR(hcd->regs)) {
182 ret = PTR_ERR(hcd->regs);
186 hcd->rsrc_start = res->start;
187 hcd->rsrc_len = resource_size(res);
189 xhci = hcd_to_xhci(hcd);
191 xhci->allow_single_roothub = 1;
194 * Not all platforms have clks so it is not an error if the
195 * clock do not exist.
197 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
198 if (IS_ERR(xhci->reg_clk)) {
199 ret = PTR_ERR(xhci->reg_clk);
203 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
204 if (IS_ERR(xhci->clk)) {
205 ret = PTR_ERR(xhci->clk);
209 xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev);
210 if (IS_ERR(xhci->reset)) {
211 ret = PTR_ERR(xhci->reset);
215 ret = reset_control_deassert(xhci->reset);
219 ret = clk_prepare_enable(xhci->reg_clk);
223 ret = clk_prepare_enable(xhci->clk);
225 goto disable_reg_clk;
228 priv = hcd_to_xhci_priv(hcd);
229 /* Just copy data for now */
233 device_set_wakeup_capable(&pdev->dev, true);
235 xhci->main_hcd = hcd;
237 /* imod_interval is the interrupt moderation value in nanoseconds. */
238 xhci->imod_interval = 40000;
240 /* Iterate over all parent nodes for finding quirks */
241 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
243 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
244 xhci->quirks |= XHCI_HW_LPM_DISABLE;
246 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
247 xhci->quirks |= XHCI_LPM_SUPPORT;
249 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
250 xhci->quirks |= XHCI_BROKEN_PORT_PED;
252 device_property_read_u32(tmpdev, "imod-interval-ns",
253 &xhci->imod_interval);
256 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
257 if (IS_ERR(hcd->usb_phy)) {
258 ret = PTR_ERR(hcd->usb_phy);
259 if (ret == -EPROBE_DEFER)
263 ret = usb_phy_init(hcd->usb_phy);
268 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
270 if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT))
271 hcd->skip_phy_initialization = 1;
273 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
274 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
276 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
278 goto disable_usb_phy;
280 if (!xhci_has_one_roothub(xhci)) {
281 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
282 dev_name(&pdev->dev), hcd);
283 if (!xhci->shared_hcd) {
285 goto dealloc_usb2_hcd;
288 xhci->shared_hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev,
290 if (IS_ERR(xhci->shared_hcd->usb_phy)) {
291 xhci->shared_hcd->usb_phy = NULL;
293 ret = usb_phy_init(xhci->shared_hcd->usb_phy);
295 dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n",
299 xhci->shared_hcd->tpl_support = hcd->tpl_support;
302 usb3_hcd = xhci_get_usb3_hcd(xhci);
303 if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4)
304 usb3_hcd->can_do_streams = 1;
306 if (xhci->shared_hcd) {
307 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
312 device_enable_async_suspend(&pdev->dev);
313 pm_runtime_put_noidle(&pdev->dev);
316 * Prevent runtime pm from being on as default, users should enable
317 * runtime pm using power/control in sysfs.
319 pm_runtime_forbid(&pdev->dev);
325 usb_put_hcd(xhci->shared_hcd);
331 usb_phy_shutdown(hcd->usb_phy);
334 clk_disable_unprepare(xhci->clk);
337 clk_disable_unprepare(xhci->reg_clk);
340 reset_control_assert(xhci->reset);
346 pm_runtime_put_noidle(&pdev->dev);
347 pm_runtime_disable(&pdev->dev);
351 EXPORT_SYMBOL_GPL(xhci_plat_probe);
353 static int xhci_generic_plat_probe(struct platform_device *pdev)
355 const struct xhci_plat_priv *priv_match;
356 struct device *sysdev;
360 * sysdev must point to a device that is known to the system firmware
361 * or PCI hardware. We handle these three cases here:
362 * 1. xhci_plat comes from firmware
363 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
364 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
366 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
367 if (is_of_node(sysdev->fwnode) ||
368 is_acpi_device_node(sysdev->fwnode))
370 else if (dev_is_pci(sysdev))
377 if (WARN_ON(!sysdev->dma_mask)) {
378 /* Platform did not initialize dma_mask */
379 ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
384 if (pdev->dev.of_node)
385 priv_match = of_device_get_match_data(&pdev->dev);
387 priv_match = dev_get_platdata(&pdev->dev);
389 return xhci_plat_probe(pdev, sysdev, priv_match);
392 void xhci_plat_remove(struct platform_device *dev)
394 struct usb_hcd *hcd = platform_get_drvdata(dev);
395 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
396 struct clk *clk = xhci->clk;
397 struct clk *reg_clk = xhci->reg_clk;
398 struct usb_hcd *shared_hcd = xhci->shared_hcd;
400 xhci->xhc_state |= XHCI_STATE_REMOVING;
401 pm_runtime_get_sync(&dev->dev);
404 usb_remove_hcd(shared_hcd);
405 xhci->shared_hcd = NULL;
408 usb_phy_shutdown(hcd->usb_phy);
413 usb_put_hcd(shared_hcd);
415 clk_disable_unprepare(clk);
416 clk_disable_unprepare(reg_clk);
417 reset_control_assert(xhci->reset);
420 pm_runtime_disable(&dev->dev);
421 pm_runtime_put_noidle(&dev->dev);
422 pm_runtime_set_suspended(&dev->dev);
424 EXPORT_SYMBOL_GPL(xhci_plat_remove);
426 static int __maybe_unused xhci_plat_suspend(struct device *dev)
428 struct usb_hcd *hcd = dev_get_drvdata(dev);
429 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
432 if (pm_runtime_suspended(dev))
433 pm_runtime_resume(dev);
435 ret = xhci_priv_suspend_quirk(hcd);
439 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
440 * to do wakeup during suspend.
442 ret = xhci_suspend(xhci, device_may_wakeup(dev));
446 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
447 clk_disable_unprepare(xhci->clk);
448 clk_disable_unprepare(xhci->reg_clk);
454 static int __maybe_unused xhci_plat_resume(struct device *dev)
456 struct usb_hcd *hcd = dev_get_drvdata(dev);
457 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
460 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
461 clk_prepare_enable(xhci->clk);
462 clk_prepare_enable(xhci->reg_clk);
465 ret = xhci_priv_resume_quirk(hcd);
469 ret = xhci_resume(xhci, PMSG_RESUME);
473 pm_runtime_disable(dev);
474 pm_runtime_set_active(dev);
475 pm_runtime_enable(dev);
480 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
482 struct usb_hcd *hcd = dev_get_drvdata(dev);
483 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
486 ret = xhci_priv_suspend_quirk(hcd);
490 return xhci_suspend(xhci, true);
493 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
495 struct usb_hcd *hcd = dev_get_drvdata(dev);
496 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
498 return xhci_resume(xhci, PMSG_AUTO_RESUME);
501 const struct dev_pm_ops xhci_plat_pm_ops = {
502 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
504 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
505 xhci_plat_runtime_resume,
508 EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
511 static const struct acpi_device_id usb_xhci_acpi_match[] = {
512 /* XHCI-compliant USB Controller */
516 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
519 static struct platform_driver usb_generic_xhci_driver = {
520 .probe = xhci_generic_plat_probe,
521 .remove_new = xhci_plat_remove,
522 .shutdown = usb_hcd_platform_shutdown,
525 .pm = &xhci_plat_pm_ops,
526 .of_match_table = of_match_ptr(usb_xhci_of_match),
527 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
530 MODULE_ALIAS("platform:xhci-hcd");
532 static int __init xhci_plat_init(void)
534 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
535 return platform_driver_register(&usb_generic_xhci_driver);
537 module_init(xhci_plat_init);
539 static void __exit xhci_plat_exit(void)
541 platform_driver_unregister(&usb_generic_xhci_driver);
543 module_exit(xhci_plat_exit);
545 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
546 MODULE_LICENSE("GPL");