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
6 * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
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/onboard_hub.h>
19 #include <linux/usb/phy.h>
20 #include <linux/slab.h>
21 #include <linux/acpi.h>
22 #include <linux/usb/of.h>
25 #include "xhci-plat.h"
26 #include "xhci-mvebu.h"
27 #include "xhci-rcar.h"
29 static struct hc_driver __read_mostly xhci_plat_hc_driver;
31 static int xhci_plat_setup(struct usb_hcd *hcd);
32 static int xhci_plat_start(struct usb_hcd *hcd);
34 static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
35 .extra_priv_size = sizeof(struct xhci_plat_priv),
36 .reset = xhci_plat_setup,
37 .start = xhci_plat_start,
40 static void xhci_priv_plat_start(struct usb_hcd *hcd)
42 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
45 priv->plat_start(hcd);
48 static int xhci_priv_plat_setup(struct usb_hcd *hcd)
50 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
52 if (!priv->plat_setup)
55 return priv->plat_setup(hcd);
58 static int xhci_priv_init_quirk(struct usb_hcd *hcd)
60 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
62 if (!priv->init_quirk)
65 return priv->init_quirk(hcd);
68 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
70 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
72 if (!priv->suspend_quirk)
75 return priv->suspend_quirk(hcd);
78 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
80 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
82 if (!priv->resume_quirk)
85 return priv->resume_quirk(hcd);
88 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
90 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
93 * As of now platform drivers don't provide MSI support so we ensure
94 * here that the generic code does not try to make a pci_dev from our
95 * dev struct in order to setup MSI
97 xhci->quirks |= XHCI_PLAT | priv->quirks;
100 /* called during probe() after chip reset completes */
101 static int xhci_plat_setup(struct usb_hcd *hcd)
106 ret = xhci_priv_init_quirk(hcd);
110 return xhci_gen_setup(hcd, xhci_plat_quirks);
113 static int xhci_plat_start(struct usb_hcd *hcd)
115 xhci_priv_plat_start(hcd);
116 return xhci_run(hcd);
120 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
121 .init_quirk = xhci_mvebu_mbus_init_quirk,
124 static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
125 .plat_setup = xhci_mvebu_a3700_plat_setup,
126 .init_quirk = xhci_mvebu_a3700_init_quirk,
129 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
130 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
133 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
134 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
137 static const struct xhci_plat_priv xhci_plat_brcm = {
138 .quirks = XHCI_RESET_ON_RESUME,
141 static const struct of_device_id usb_xhci_of_match[] = {
143 .compatible = "generic-xhci",
145 .compatible = "xhci-platform",
147 .compatible = "marvell,armada-375-xhci",
148 .data = &xhci_plat_marvell_armada,
150 .compatible = "marvell,armada-380-xhci",
151 .data = &xhci_plat_marvell_armada,
153 .compatible = "marvell,armada3700-xhci",
154 .data = &xhci_plat_marvell_armada3700,
156 .compatible = "renesas,xhci-r8a7790",
157 .data = &xhci_plat_renesas_rcar_gen2,
159 .compatible = "renesas,xhci-r8a7791",
160 .data = &xhci_plat_renesas_rcar_gen2,
162 .compatible = "renesas,xhci-r8a7793",
163 .data = &xhci_plat_renesas_rcar_gen2,
165 .compatible = "renesas,xhci-r8a7795",
166 .data = &xhci_plat_renesas_rcar_gen3,
168 .compatible = "renesas,xhci-r8a7796",
169 .data = &xhci_plat_renesas_rcar_gen3,
171 .compatible = "renesas,rcar-gen2-xhci",
172 .data = &xhci_plat_renesas_rcar_gen2,
174 .compatible = "renesas,rcar-gen3-xhci",
175 .data = &xhci_plat_renesas_rcar_gen3,
177 .compatible = "brcm,xhci-brcm-v2",
178 .data = &xhci_plat_brcm,
180 .compatible = "brcm,bcm7445-xhci",
181 .data = &xhci_plat_brcm,
185 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
188 static int xhci_plat_probe(struct platform_device *pdev)
190 const struct xhci_plat_priv *priv_match;
191 const struct hc_driver *driver;
192 struct device *sysdev, *tmpdev;
193 struct xhci_hcd *xhci;
194 struct resource *res;
198 struct xhci_plat_priv *priv = NULL;
204 driver = &xhci_plat_hc_driver;
206 irq = platform_get_irq(pdev, 0);
211 * sysdev must point to a device that is known to the system firmware
212 * or PCI hardware. We handle these three cases here:
213 * 1. xhci_plat comes from firmware
214 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
215 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
217 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
218 if (is_of_node(sysdev->fwnode) ||
219 is_acpi_device_node(sysdev->fwnode))
222 else if (sysdev->bus == &pci_bus_type)
230 /* Try to set 64-bit DMA first */
231 if (WARN_ON(!sysdev->dma_mask))
232 /* Platform did not initialize dma_mask */
233 ret = dma_coerce_mask_and_coherent(sysdev,
236 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
238 /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
240 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
245 pm_runtime_set_active(&pdev->dev);
246 pm_runtime_enable(&pdev->dev);
247 pm_runtime_get_noresume(&pdev->dev);
249 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
250 dev_name(&pdev->dev), NULL);
253 goto disable_runtime;
256 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
257 if (IS_ERR(hcd->regs)) {
258 ret = PTR_ERR(hcd->regs);
262 hcd->rsrc_start = res->start;
263 hcd->rsrc_len = resource_size(res);
265 xhci = hcd_to_xhci(hcd);
268 * Not all platforms have clks so it is not an error if the
269 * clock do not exist.
271 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
272 if (IS_ERR(xhci->reg_clk)) {
273 ret = PTR_ERR(xhci->reg_clk);
277 ret = clk_prepare_enable(xhci->reg_clk);
281 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
282 if (IS_ERR(xhci->clk)) {
283 ret = PTR_ERR(xhci->clk);
284 goto disable_reg_clk;
287 ret = clk_prepare_enable(xhci->clk);
289 goto disable_reg_clk;
291 if (pdev->dev.of_node)
292 priv_match = of_device_get_match_data(&pdev->dev);
294 priv_match = dev_get_platdata(&pdev->dev);
297 priv = hcd_to_xhci_priv(hcd);
298 /* Just copy data for now */
302 device_set_wakeup_capable(&pdev->dev, true);
304 xhci->main_hcd = hcd;
305 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
306 dev_name(&pdev->dev), hcd);
307 if (!xhci->shared_hcd) {
312 /* imod_interval is the interrupt moderation value in nanoseconds. */
313 xhci->imod_interval = 40000;
315 /* Iterate over all parent nodes for finding quirks */
316 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
318 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
319 xhci->quirks |= XHCI_HW_LPM_DISABLE;
321 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
322 xhci->quirks |= XHCI_LPM_SUPPORT;
324 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
325 xhci->quirks |= XHCI_BROKEN_PORT_PED;
327 device_property_read_u32(tmpdev, "imod-interval-ns",
328 &xhci->imod_interval);
331 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
332 if (IS_ERR(hcd->usb_phy)) {
333 ret = PTR_ERR(hcd->usb_phy);
334 if (ret == -EPROBE_DEFER)
338 ret = usb_phy_init(hcd->usb_phy);
343 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
344 xhci->shared_hcd->tpl_support = hcd->tpl_support;
347 ret = xhci_priv_plat_setup(hcd);
349 goto disable_usb_phy;
352 if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
353 hcd->skip_phy_initialization = 1;
355 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
356 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
358 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
360 goto disable_usb_phy;
362 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
363 xhci->shared_hcd->can_do_streams = 1;
365 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
367 goto dealloc_usb2_hcd;
369 device_enable_async_suspend(&pdev->dev);
370 pm_runtime_put_noidle(&pdev->dev);
373 * Prevent runtime pm from being on as default, users should enable
374 * runtime pm using power/control in sysfs.
376 pm_runtime_forbid(&pdev->dev);
378 INIT_LIST_HEAD(&xhci->onboard_hub_devs);
379 onboard_hub_create_pdevs(hcd->self.root_hub, &xhci->onboard_hub_devs);
388 usb_phy_shutdown(hcd->usb_phy);
391 usb_put_hcd(xhci->shared_hcd);
394 clk_disable_unprepare(xhci->clk);
397 clk_disable_unprepare(xhci->reg_clk);
403 pm_runtime_put_noidle(&pdev->dev);
404 pm_runtime_disable(&pdev->dev);
409 static int xhci_plat_remove(struct platform_device *dev)
411 struct usb_hcd *hcd = platform_get_drvdata(dev);
412 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
413 struct clk *clk = xhci->clk;
414 struct clk *reg_clk = xhci->reg_clk;
415 struct usb_hcd *shared_hcd = xhci->shared_hcd;
417 pm_runtime_get_sync(&dev->dev);
418 xhci->xhc_state |= XHCI_STATE_REMOVING;
420 usb_remove_hcd(shared_hcd);
421 xhci->shared_hcd = NULL;
422 usb_phy_shutdown(hcd->usb_phy);
425 usb_put_hcd(shared_hcd);
427 onboard_hub_destroy_pdevs(&xhci->onboard_hub_devs);
429 clk_disable_unprepare(clk);
430 clk_disable_unprepare(reg_clk);
433 pm_runtime_disable(&dev->dev);
434 pm_runtime_put_noidle(&dev->dev);
435 pm_runtime_set_suspended(&dev->dev);
440 static int __maybe_unused xhci_plat_suspend(struct device *dev)
442 struct usb_hcd *hcd = dev_get_drvdata(dev);
443 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
446 ret = xhci_priv_suspend_quirk(hcd);
450 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
451 * to do wakeup during suspend.
453 return xhci_suspend(xhci, device_may_wakeup(dev));
456 static int __maybe_unused xhci_plat_resume(struct device *dev)
458 struct usb_hcd *hcd = dev_get_drvdata(dev);
459 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
462 ret = xhci_priv_resume_quirk(hcd);
466 ret = xhci_resume(xhci, 0);
470 pm_runtime_disable(dev);
471 pm_runtime_set_active(dev);
472 pm_runtime_enable(dev);
477 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
479 struct usb_hcd *hcd = dev_get_drvdata(dev);
480 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
483 ret = xhci_priv_suspend_quirk(hcd);
487 return xhci_suspend(xhci, true);
490 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
492 struct usb_hcd *hcd = dev_get_drvdata(dev);
493 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
495 return xhci_resume(xhci, 0);
498 static const struct dev_pm_ops xhci_plat_pm_ops = {
499 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
501 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
502 xhci_plat_runtime_resume,
507 static const struct acpi_device_id usb_xhci_acpi_match[] = {
508 /* XHCI-compliant USB Controller */
512 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
515 static struct platform_driver usb_xhci_driver = {
516 .probe = xhci_plat_probe,
517 .remove = xhci_plat_remove,
518 .shutdown = usb_hcd_platform_shutdown,
521 .pm = &xhci_plat_pm_ops,
522 .of_match_table = of_match_ptr(usb_xhci_of_match),
523 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
526 MODULE_ALIAS("platform:xhci-hcd");
528 static int __init xhci_plat_init(void)
530 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
531 return platform_driver_register(&usb_xhci_driver);
533 module_init(xhci_plat_init);
535 static void __exit xhci_plat_exit(void)
537 platform_driver_unregister(&usb_xhci_driver);
539 module_exit(xhci_plat_exit);
541 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
542 MODULE_LICENSE("GPL");