blob: 29c656e86632e1d44c9accbd8ce3d93969df5470 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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 - http://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
23#include "xhci.h"
24#include "xhci-plat.h"
25#include "xhci-mvebu.h"
26#include "xhci-rcar.h"
27
28
29static struct hc_driver __read_mostly xhci_plat_hc_driver;
30
31static int xhci_plat_setup(struct usb_hcd *hcd);
32static int xhci_plat_start(struct usb_hcd *hcd);
33
34static 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,
38};
39
40static void xhci_priv_plat_start(struct usb_hcd *hcd)
41{
42 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
43
44 if (priv->plat_start)
45 priv->plat_start(hcd);
46}
47
48static int xhci_priv_plat_setup(struct usb_hcd *hcd)
49{
50 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
51
52 if (!priv->plat_setup)
53 return 0;
54
55 return priv->plat_setup(hcd);
56}
57
58static int xhci_priv_init_quirk(struct usb_hcd *hcd)
59{
60 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
61
62 if (!priv->init_quirk)
63 return 0;
64
65 return priv->init_quirk(hcd);
66}
67
68static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
69{
70 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
71
72 if (!priv->resume_quirk)
73 return 0;
74
75 return priv->resume_quirk(hcd);
76}
77
78static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
79{
80 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
81
82 /*
83 * As of now platform drivers don't provide MSI support so we ensure
84 * here that the generic code does not try to make a pci_dev from our
85 * dev struct in order to setup MSI
86 */
87 xhci->quirks |= XHCI_PLAT | priv->quirks;
88}
89
90/* called during probe() after chip reset completes */
91static int xhci_plat_setup(struct usb_hcd *hcd)
92{
93 int ret;
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
102static 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
109static const struct xhci_plat_priv xhci_plat_marvell_armada = {
110 .init_quirk = xhci_mvebu_mbus_init_quirk,
111};
112
113static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
114 .plat_setup = xhci_mvebu_a3700_plat_setup,
115 .init_quirk = xhci_mvebu_a3700_init_quirk,
116};
117
118static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
119 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
120};
121
122static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
123 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
124};
125
126static const struct of_device_id usb_xhci_of_match[] = {
127 {
128 .compatible = "generic-xhci",
129 }, {
130 .compatible = "xhci-platform",
131 }, {
132 .compatible = "marvell,armada-375-xhci",
133 .data = &xhci_plat_marvell_armada,
134 }, {
135 .compatible = "marvell,armada-380-xhci",
136 .data = &xhci_plat_marvell_armada,
137 }, {
138 .compatible = "marvell,armada3700-xhci",
139 .data = &xhci_plat_marvell_armada3700,
140 }, {
141 .compatible = "renesas,xhci-r8a7790",
142 .data = &xhci_plat_renesas_rcar_gen2,
143 }, {
144 .compatible = "renesas,xhci-r8a7791",
145 .data = &xhci_plat_renesas_rcar_gen2,
146 }, {
147 .compatible = "renesas,xhci-r8a7793",
148 .data = &xhci_plat_renesas_rcar_gen2,
149 }, {
150 .compatible = "renesas,xhci-r8a7795",
151 .data = &xhci_plat_renesas_rcar_gen3,
152 }, {
153 .compatible = "renesas,xhci-r8a7796",
154 .data = &xhci_plat_renesas_rcar_gen3,
155 }, {
156 .compatible = "renesas,rcar-gen2-xhci",
157 .data = &xhci_plat_renesas_rcar_gen2,
158 }, {
159 .compatible = "renesas,rcar-gen3-xhci",
160 .data = &xhci_plat_renesas_rcar_gen3,
161 },
162 {},
163};
164MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
165#endif
166
167extern int usb_otg_set_host(struct usb_hcd *hcd, struct usb_hcd *shared_hcd);
168
169static int xhci_plat_probe(struct platform_device *pdev)
170{
171 const struct xhci_plat_priv *priv_match;
172 const struct hc_driver *driver;
173 struct device *sysdev, *tmpdev;
174 struct xhci_hcd *xhci;
175 struct resource *res;
176 struct usb_hcd *hcd;
177 int ret;
178 int irq;
179 struct xhci_plat_priv *priv = NULL;
180
181 if (usb_disabled())
182 return -ENODEV;
183
184 driver = &xhci_plat_hc_driver;
185
186 irq = platform_get_irq(pdev, 0);
187 if (irq < 0) {
188 pr_err("%s: platform_get_irq failed\n", __func__);
189 return irq;
190 }
191 /*
192 * sysdev must point to a device that is known to the system firmware
193 * or PCI hardware. We handle these three cases here:
194 * 1. xhci_plat comes from firmware
195 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
196 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
197 */
198 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
199 if (is_of_node(sysdev->fwnode) ||
200 is_acpi_device_node(sysdev->fwnode))
201 break;
202#ifdef CONFIG_PCI
203 else if (sysdev->bus == &pci_bus_type)
204 break;
205#endif
206 }
207
208 if (!sysdev)
209 sysdev = &pdev->dev;
210
211 /* Try to set 64-bit DMA first */
212 if (WARN_ON(!sysdev->dma_mask))
213 /* Platform did not initialize dma_mask */
214 ret = dma_coerce_mask_and_coherent(sysdev,
215 DMA_BIT_MASK(64));
216 else
217 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
218
219 /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
220 if (ret) {
221 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
222 if (ret)
223 return ret;
224 }
225
226 pm_runtime_set_active(&pdev->dev);
227 pm_runtime_enable(&pdev->dev);
228 pm_runtime_get_noresume(&pdev->dev);
229
230 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
231 dev_name(&pdev->dev), NULL);
232 if (!hcd) {
233 ret = -ENOMEM;
234 goto disable_runtime;
235 }
236
237 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
239 if (IS_ERR(hcd->regs)) {
240 ret = PTR_ERR(hcd->regs);
241 pr_err("%s: devm_ioremap_resource failed\n", __func__);
242 goto put_hcd;
243 }
244
245 hcd->rsrc_start = res->start;
246 hcd->rsrc_len = resource_size(res);
247 hcd->irq = irq;
248 xhci = hcd_to_xhci(hcd);
249
250#ifndef CONFIG_USB_DWC3_ASR_OTG
251 /*
252 * Not all platforms have clks so it is not an error if the
253 * clock do not exist.
254 */
255 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
256 if (IS_ERR(xhci->reg_clk)) {
257 ret = PTR_ERR(xhci->reg_clk);
258 pr_err("%s: devm_clk_get_optional failed\n", __func__);
259 goto put_hcd;
260 }
261
262 ret = clk_prepare_enable(xhci->reg_clk);
263 if (ret)
264 goto put_hcd;
265
266 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
267 if (IS_ERR(xhci->clk)) {
268 ret = PTR_ERR(xhci->clk);
269 goto disable_reg_clk;
270 }
271
272 ret = clk_prepare_enable(xhci->clk);
273 if (ret)
274 goto disable_reg_clk;
275#endif
276
277 priv_match = of_device_get_match_data(&pdev->dev);
278 if (priv_match) {
279 priv = hcd_to_xhci_priv(hcd);
280 /* Just copy data for now */
281 if (priv_match)
282 *priv = *priv_match;
283 }
284
285 device_set_wakeup_capable(&pdev->dev, true);
286
287 xhci->main_hcd = hcd;
288 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
289 dev_name(&pdev->dev), hcd);
290 if (!xhci->shared_hcd) {
291 ret = -ENOMEM;
292 pr_err("%s: !xhci->shared_hcd\n", __func__);
293 goto disable_clk;
294 }
295 xhci->shared_hcd->irq = irq;
296
297 /* imod_interval is the interrupt moderation value in nanoseconds. */
298 xhci->imod_interval = 40000;
299
300 /* Iterate over all parent nodes for finding quirks */
301 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
302
303 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
304 xhci->quirks |= XHCI_HW_LPM_DISABLE;
305
306 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
307 xhci->quirks |= XHCI_LPM_SUPPORT;
308
309 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
310 xhci->quirks |= XHCI_BROKEN_PORT_PED;
311
312 device_property_read_u32(tmpdev, "imod-interval-ns",
313 &xhci->imod_interval);
314 }
315
316#ifndef CONFIG_USB_DWC3_ASR_OTG
317 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
318 if (IS_ERR(hcd->usb_phy)) {
319 ret = PTR_ERR(hcd->usb_phy);
320 if (ret == -EPROBE_DEFER)
321 goto put_usb3_hcd;
322 hcd->usb_phy = NULL;
323 } else {
324 ret = usb_phy_init(hcd->usb_phy);
325 if (ret)
326 goto put_usb3_hcd;
327 }
328#endif
329
330 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
331 xhci->shared_hcd->tpl_support = hcd->tpl_support;
332
333 if (priv) {
334 ret = xhci_priv_plat_setup(hcd);
335 if (ret)
336 goto disable_usb_phy;
337 }
338
339 if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
340 hcd->skip_phy_initialization = 1;
341
342#ifndef CONFIG_USB_DWC3_ASR_OTG
343 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
344 if (ret)
345 goto disable_usb_phy;
346#endif
347
348 if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
349 xhci->shared_hcd->can_do_streams = 1;
350
351#ifndef CONFIG_USB_DWC3_ASR_OTG
352 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
353 if (ret)
354 goto dealloc_usb2_hcd;
355#endif
356
357#ifdef CONFIG_USB_DWC3_ASR_OTG
358 usb_otg_set_host(hcd, xhci->shared_hcd);
359#endif
360
361 device_enable_async_suspend(&pdev->dev);
362 pm_runtime_put_noidle(&pdev->dev);
363
364 /*
365 * Prevent runtime pm from being on as default, users should enable
366 * runtime pm using power/control in sysfs.
367 */
368 pm_runtime_forbid(&pdev->dev);
369
370 return 0;
371
372#ifndef CONFIG_USB_DWC3_ASR_OTG
373dealloc_usb2_hcd:
374 usb_remove_hcd(hcd);
375#endif
376
377disable_usb_phy:
378 usb_phy_shutdown(hcd->usb_phy);
379
380#ifndef CONFIG_USB_DWC3_ASR_OTG
381put_usb3_hcd:
382 usb_put_hcd(xhci->shared_hcd);
383#endif
384
385disable_clk:
386 clk_disable_unprepare(xhci->clk);
387
388#ifndef CONFIG_USB_DWC3_ASR_OTG
389disable_reg_clk:
390 clk_disable_unprepare(xhci->reg_clk);
391#endif
392
393put_hcd:
394 usb_put_hcd(hcd);
395
396disable_runtime:
397 pm_runtime_put_noidle(&pdev->dev);
398 pm_runtime_disable(&pdev->dev);
399
400 return ret;
401}
402
403static int xhci_plat_remove(struct platform_device *dev)
404{
405 struct usb_hcd *hcd = platform_get_drvdata(dev);
406 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
407 struct clk *clk = xhci->clk;
408 struct clk *reg_clk = xhci->reg_clk;
409 struct usb_hcd *shared_hcd = xhci->shared_hcd;
410
411 pm_runtime_get_sync(&dev->dev);
412 xhci->xhc_state |= XHCI_STATE_REMOVING;
413
414 usb_remove_hcd(shared_hcd);
415 xhci->shared_hcd = NULL;
416 usb_phy_shutdown(hcd->usb_phy);
417
418 usb_remove_hcd(hcd);
419 usb_put_hcd(shared_hcd);
420
421 clk_disable_unprepare(clk);
422 clk_disable_unprepare(reg_clk);
423 usb_put_hcd(hcd);
424
425 pm_runtime_disable(&dev->dev);
426 pm_runtime_put_noidle(&dev->dev);
427 pm_runtime_set_suspended(&dev->dev);
428
429 return 0;
430}
431
432static int __maybe_unused xhci_plat_suspend(struct device *dev)
433{
434 struct usb_hcd *hcd = dev_get_drvdata(dev);
435 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
436
437 /*
438 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
439 * to do wakeup during suspend. Since xhci_plat_suspend is currently
440 * only designed for system suspend, device_may_wakeup() is enough
441 * to dertermine whether host is allowed to do wakeup. Need to
442 * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
443 * also applies to runtime suspend.
444 */
445 return xhci_suspend(xhci, device_may_wakeup(dev));
446}
447
448static int __maybe_unused xhci_plat_resume(struct device *dev)
449{
450 struct usb_hcd *hcd = dev_get_drvdata(dev);
451 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
452 int ret;
453
454 ret = xhci_priv_resume_quirk(hcd);
455 if (ret)
456 return ret;
457
458 return xhci_resume(xhci, 0);
459}
460
461static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
462{
463 struct usb_hcd *hcd = dev_get_drvdata(dev);
464 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
465
466 return xhci_suspend(xhci, true);
467}
468
469static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
470{
471 struct usb_hcd *hcd = dev_get_drvdata(dev);
472 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
473
474 return xhci_resume(xhci, 0);
475}
476
477static const struct dev_pm_ops xhci_plat_pm_ops = {
478 SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
479
480 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
481 xhci_plat_runtime_resume,
482 NULL)
483};
484
485static const struct acpi_device_id usb_xhci_acpi_match[] = {
486 /* XHCI-compliant USB Controller */
487 { "PNP0D10", },
488 { }
489};
490MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
491
492static struct platform_driver usb_xhci_driver = {
493 .probe = xhci_plat_probe,
494 .remove = xhci_plat_remove,
495 .shutdown = usb_hcd_platform_shutdown,
496 .driver = {
497 .name = "xhci-hcd",
498 .pm = &xhci_plat_pm_ops,
499 .of_match_table = of_match_ptr(usb_xhci_of_match),
500 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
501 },
502};
503MODULE_ALIAS("platform:xhci-hcd");
504
505static int __init xhci_plat_init(void)
506{
507 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
508 return platform_driver_register(&usb_xhci_driver);
509}
510module_init(xhci_plat_init);
511
512static void __exit xhci_plat_exit(void)
513{
514 platform_driver_unregister(&usb_xhci_driver);
515}
516module_exit(xhci_plat_exit);
517
518MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
519MODULE_LICENSE("GPL");