Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// 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 - https://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#include <linux/reset.h>
23#include <linux/usb/xhci-sideband.h>
24
25#include "xhci.h"
26#include "xhci-plat.h"
27#include "xhci-mvebu.h"
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_init_quirk(struct usb_hcd *hcd)
49{
50 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
51
52 if (!priv->init_quirk)
53 return 0;
54
55 return priv->init_quirk(hcd);
56}
57
58static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
59{
60 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
61
62 if (!priv->suspend_quirk)
63 return 0;
64
65 return priv->suspend_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 int xhci_priv_post_resume_quirk(struct usb_hcd *hcd)
79{
80 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
81
82 if (!priv->post_resume_quirk)
83 return 0;
84
85 return priv->post_resume_quirk(hcd);
86}
87
88static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
89{
90 struct xhci_plat_priv *priv = xhci_to_priv(xhci);
91
92 xhci->quirks |= priv->quirks;
93}
94
95/* called during probe() after chip reset completes */
96static int xhci_plat_setup(struct usb_hcd *hcd)
97{
98 int ret;
99
100
101 ret = xhci_priv_init_quirk(hcd);
102 if (ret)
103 return ret;
104
105 return xhci_gen_setup(hcd, xhci_plat_quirks);
106}
107
108static int xhci_plat_start(struct usb_hcd *hcd)
109{
110 xhci_priv_plat_start(hcd);
111 return xhci_run(hcd);
112}
113
114#ifdef CONFIG_OF
115static const struct xhci_plat_priv xhci_plat_marvell_armada = {
116 .init_quirk = xhci_mvebu_mbus_init_quirk,
117};
118
119static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
120 .quirks = XHCI_RESET_ON_RESUME,
121};
122
123static const struct xhci_plat_priv xhci_plat_brcm = {
124 .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
125};
126
127static const struct of_device_id usb_xhci_of_match[] = {
128 {
129 .compatible = "generic-xhci",
130 }, {
131 .compatible = "xhci-platform",
132 }, {
133 .compatible = "marvell,armada-375-xhci",
134 .data = &xhci_plat_marvell_armada,
135 }, {
136 .compatible = "marvell,armada-380-xhci",
137 .data = &xhci_plat_marvell_armada,
138 }, {
139 .compatible = "marvell,armada3700-xhci",
140 .data = &xhci_plat_marvell_armada3700,
141 }, {
142 .compatible = "brcm,xhci-brcm-v2",
143 .data = &xhci_plat_brcm,
144 }, {
145 .compatible = "brcm,bcm2711-xhci",
146 .data = &xhci_plat_brcm,
147 }, {
148 .compatible = "brcm,bcm7445-xhci",
149 .data = &xhci_plat_brcm,
150 },
151 {},
152};
153MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
154#endif
155
156int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const struct xhci_plat_priv *priv_match)
157{
158 const struct hc_driver *driver;
159 struct device *tmpdev;
160 struct xhci_hcd *xhci;
161 struct resource *res;
162 struct usb_hcd *hcd, *usb3_hcd;
163 int ret;
164 int irq;
165 struct xhci_plat_priv *priv = NULL;
166 const struct of_device_id *of_match;
167
168 if (usb_disabled())
169 return -ENODEV;
170
171 driver = &xhci_plat_hc_driver;
172
173 irq = platform_get_irq(pdev, 0);
174 if (irq < 0)
175 return irq;
176
177 if (!sysdev)
178 sysdev = &pdev->dev;
179
180 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
181 if (ret)
182 return ret;
183
184 pm_runtime_set_active(&pdev->dev);
185 pm_runtime_use_autosuspend(&pdev->dev);
186 pm_runtime_enable(&pdev->dev);
187 pm_runtime_get_noresume(&pdev->dev);
188
189 hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
190 dev_name(&pdev->dev), NULL);
191 if (!hcd) {
192 ret = -ENOMEM;
193 goto disable_runtime;
194 }
195
196 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
197 if (IS_ERR(hcd->regs)) {
198 ret = PTR_ERR(hcd->regs);
199 goto put_hcd;
200 }
201
202 hcd->rsrc_start = res->start;
203 hcd->rsrc_len = resource_size(res);
204
205 xhci = hcd_to_xhci(hcd);
206
207 xhci->allow_single_roothub = 1;
208
209 /*
210 * Not all platforms have clks so it is not an error if the
211 * clock do not exist.
212 */
213 xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
214 if (IS_ERR(xhci->reg_clk)) {
215 ret = PTR_ERR(xhci->reg_clk);
216 goto put_hcd;
217 }
218
219 xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
220 if (IS_ERR(xhci->clk)) {
221 ret = PTR_ERR(xhci->clk);
222 goto put_hcd;
223 }
224
225 xhci->reset = devm_reset_control_array_get_optional_shared(&pdev->dev);
226 if (IS_ERR(xhci->reset)) {
227 ret = PTR_ERR(xhci->reset);
228 goto put_hcd;
229 }
230
231 ret = reset_control_deassert(xhci->reset);
232 if (ret)
233 goto put_hcd;
234
235 ret = clk_prepare_enable(xhci->reg_clk);
236 if (ret)
237 goto err_reset;
238
239 ret = clk_prepare_enable(xhci->clk);
240 if (ret)
241 goto disable_reg_clk;
242
243 if (priv_match) {
244 priv = hcd_to_xhci_priv(hcd);
245 /* Just copy data for now */
246 *priv = *priv_match;
247 }
248
249 device_set_wakeup_capable(&pdev->dev, true);
250
251 xhci->main_hcd = hcd;
252
253 /* imod_interval is the interrupt moderation value in nanoseconds. */
254 xhci->imod_interval = 40000;
255
256 /* Iterate over all parent nodes for finding quirks */
257 for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
258
259 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
260 xhci->quirks |= XHCI_HW_LPM_DISABLE;
261
262 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
263 xhci->quirks |= XHCI_LPM_SUPPORT;
264
265 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
266 xhci->quirks |= XHCI_BROKEN_PORT_PED;
267
268 if (device_property_read_bool(tmpdev, "xhci-sg-trb-cache-size-quirk"))
269 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
270
271 if (device_property_read_bool(tmpdev, "write-64-hi-lo-quirk"))
272 xhci->quirks |= XHCI_WRITE_64_HI_LO;
273
274 if (device_property_read_bool(tmpdev, "xhci-missing-cas-quirk"))
275 xhci->quirks |= XHCI_MISSING_CAS;
276
277 if (device_property_read_bool(tmpdev, "xhci-skip-phy-init-quirk"))
278 xhci->quirks |= XHCI_SKIP_PHY_INIT;
279
280 device_property_read_u32(tmpdev, "imod-interval-ns",
281 &xhci->imod_interval);
282 device_property_read_u16(tmpdev, "num-hc-interrupters",
283 &xhci->max_interrupters);
284 }
285
286 /*
287 * Drivers such as dwc3 manages PHYs themself (and rely on driver name
288 * matching for the xhci platform device).
289 */
290 of_match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
291 if (of_match) {
292 hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
293 if (IS_ERR(hcd->usb_phy)) {
294 ret = PTR_ERR(hcd->usb_phy);
295 if (ret == -EPROBE_DEFER)
296 goto disable_clk;
297 hcd->usb_phy = NULL;
298 } else {
299 ret = usb_phy_init(hcd->usb_phy);
300 if (ret)
301 goto disable_clk;
302 }
303 }
304
305 hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
306
307 if ((priv && (priv->quirks & XHCI_SKIP_PHY_INIT)) ||
308 (xhci->quirks & XHCI_SKIP_PHY_INIT))
309 hcd->skip_phy_initialization = 1;
310
311 if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
312 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
313
314 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
315 if (ret)
316 goto disable_usb_phy;
317
318 if (!xhci_has_one_roothub(xhci)) {
319 xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
320 dev_name(&pdev->dev), hcd);
321 if (!xhci->shared_hcd) {
322 ret = -ENOMEM;
323 goto dealloc_usb2_hcd;
324 }
325
326 if (of_match) {
327 xhci->shared_hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev,
328 "usb-phy", 1);
329 if (IS_ERR(xhci->shared_hcd->usb_phy)) {
330 xhci->shared_hcd->usb_phy = NULL;
331 } else {
332 ret = usb_phy_init(xhci->shared_hcd->usb_phy);
333 if (ret)
334 dev_err(sysdev, "%s init usb3phy fail (ret=%d)\n",
335 __func__, ret);
336 }
337 }
338
339 xhci->shared_hcd->tpl_support = hcd->tpl_support;
340 }
341
342 usb3_hcd = xhci_get_usb3_hcd(xhci);
343 if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
344 !(xhci->quirks & XHCI_BROKEN_STREAMS))
345 usb3_hcd->can_do_streams = 1;
346
347 if (xhci->shared_hcd) {
348 xhci->shared_hcd->rsrc_start = hcd->rsrc_start;
349 xhci->shared_hcd->rsrc_len = hcd->rsrc_len;
350 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
351 if (ret)
352 goto put_usb3_hcd;
353 }
354
355 device_enable_async_suspend(&pdev->dev);
356 pm_runtime_put_noidle(&pdev->dev);
357
358 /*
359 * Prevent runtime pm from being on as default, users should enable
360 * runtime pm using power/control in sysfs.
361 */
362 pm_runtime_forbid(&pdev->dev);
363
364 return 0;
365
366
367put_usb3_hcd:
368 usb_put_hcd(xhci->shared_hcd);
369
370dealloc_usb2_hcd:
371 usb_remove_hcd(hcd);
372
373disable_usb_phy:
374 usb_phy_shutdown(hcd->usb_phy);
375
376disable_clk:
377 clk_disable_unprepare(xhci->clk);
378
379disable_reg_clk:
380 clk_disable_unprepare(xhci->reg_clk);
381
382err_reset:
383 reset_control_assert(xhci->reset);
384
385put_hcd:
386 usb_put_hcd(hcd);
387
388disable_runtime:
389 pm_runtime_put_noidle(&pdev->dev);
390 pm_runtime_disable(&pdev->dev);
391
392 return ret;
393}
394EXPORT_SYMBOL_GPL(xhci_plat_probe);
395
396static int xhci_generic_plat_probe(struct platform_device *pdev)
397{
398 const struct xhci_plat_priv *priv_match;
399 struct device *sysdev;
400 int ret;
401
402 /*
403 * sysdev must point to a device that is known to the system firmware
404 * or PCI hardware. We handle these three cases here:
405 * 1. xhci_plat comes from firmware
406 * 2. xhci_plat is child of a device from firmware (dwc3-plat)
407 * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
408 */
409 for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
410 if (is_of_node(sysdev->fwnode) ||
411 is_acpi_device_node(sysdev->fwnode))
412 break;
413 else if (dev_is_pci(sysdev))
414 break;
415 }
416
417 if (!sysdev)
418 sysdev = &pdev->dev;
419
420 if (WARN_ON(!sysdev->dma_mask)) {
421 /* Platform did not initialize dma_mask */
422 ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
423 if (ret)
424 return ret;
425 }
426
427 if (pdev->dev.of_node)
428 priv_match = of_device_get_match_data(&pdev->dev);
429 else
430 priv_match = dev_get_platdata(&pdev->dev);
431
432 return xhci_plat_probe(pdev, sysdev, priv_match);
433}
434
435void xhci_plat_remove(struct platform_device *dev)
436{
437 struct usb_hcd *hcd = platform_get_drvdata(dev);
438 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
439 struct clk *clk = xhci->clk;
440 struct clk *reg_clk = xhci->reg_clk;
441 struct usb_hcd *shared_hcd = xhci->shared_hcd;
442
443 xhci->xhc_state |= XHCI_STATE_REMOVING;
444 pm_runtime_get_sync(&dev->dev);
445
446 if (shared_hcd) {
447 usb_remove_hcd(shared_hcd);
448 xhci->shared_hcd = NULL;
449 }
450
451 usb_phy_shutdown(hcd->usb_phy);
452
453 usb_remove_hcd(hcd);
454
455 if (shared_hcd)
456 usb_put_hcd(shared_hcd);
457
458 clk_disable_unprepare(clk);
459 clk_disable_unprepare(reg_clk);
460 reset_control_assert(xhci->reset);
461 usb_put_hcd(hcd);
462
463 pm_runtime_disable(&dev->dev);
464 pm_runtime_put_noidle(&dev->dev);
465 pm_runtime_set_suspended(&dev->dev);
466}
467EXPORT_SYMBOL_GPL(xhci_plat_remove);
468
469static int xhci_plat_suspend_common(struct device *dev)
470{
471 struct usb_hcd *hcd = dev_get_drvdata(dev);
472 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
473 int ret;
474
475 if (pm_runtime_suspended(dev))
476 pm_runtime_resume(dev);
477
478 ret = xhci_priv_suspend_quirk(hcd);
479 if (ret)
480 return ret;
481 /*
482 * xhci_suspend() needs `do_wakeup` to know whether host is allowed
483 * to do wakeup during suspend.
484 */
485 ret = xhci_suspend(xhci, device_may_wakeup(dev));
486 if (ret)
487 return ret;
488
489 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
490 clk_disable_unprepare(xhci->clk);
491 clk_disable_unprepare(xhci->reg_clk);
492 }
493
494 return 0;
495}
496
497static int xhci_plat_suspend(struct device *dev)
498{
499 struct usb_hcd *hcd = dev_get_drvdata(dev);
500 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
501
502 if (xhci_sideband_check(hcd)) {
503 priv->sideband_at_suspend = 1;
504 dev_dbg(dev, "sideband instance active, skip suspend.\n");
505 return 0;
506 }
507
508 return xhci_plat_suspend_common(dev);
509}
510
511static int xhci_plat_freeze(struct device *dev)
512{
513 return xhci_plat_suspend_common(dev);
514}
515
516static int xhci_plat_resume_common(struct device *dev, bool power_lost)
517{
518 struct usb_hcd *hcd = dev_get_drvdata(dev);
519 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
520 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
521 int ret;
522
523 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
524 ret = clk_prepare_enable(xhci->clk);
525 if (ret)
526 return ret;
527
528 ret = clk_prepare_enable(xhci->reg_clk);
529 if (ret) {
530 clk_disable_unprepare(xhci->clk);
531 return ret;
532 }
533 }
534
535 ret = xhci_priv_resume_quirk(hcd);
536 if (ret)
537 goto disable_clks;
538
539 ret = xhci_resume(xhci, power_lost || priv->power_lost, false);
540 if (ret)
541 goto disable_clks;
542
543 ret = xhci_priv_post_resume_quirk(hcd);
544 if (ret)
545 goto disable_clks;
546
547 pm_runtime_disable(dev);
548 pm_runtime_set_active(dev);
549 pm_runtime_enable(dev);
550
551 return 0;
552
553disable_clks:
554 if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
555 clk_disable_unprepare(xhci->clk);
556 clk_disable_unprepare(xhci->reg_clk);
557 }
558
559 return ret;
560}
561
562static int xhci_plat_resume(struct device *dev)
563{
564 struct usb_hcd *hcd = dev_get_drvdata(dev);
565 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
566
567 if (priv->sideband_at_suspend) {
568 priv->sideband_at_suspend = 0;
569 dev_dbg(dev, "sideband instance active, skip resume.\n");
570 return 0;
571 }
572
573 return xhci_plat_resume_common(dev, false);
574}
575
576static int xhci_plat_thaw(struct device *dev)
577{
578 return xhci_plat_resume_common(dev, false);
579}
580
581static int xhci_plat_restore(struct device *dev)
582{
583 return xhci_plat_resume_common(dev, true);
584}
585
586static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
587{
588 struct usb_hcd *hcd = dev_get_drvdata(dev);
589 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
590 int ret;
591
592 ret = xhci_priv_suspend_quirk(hcd);
593 if (ret)
594 return ret;
595
596 return xhci_suspend(xhci, true);
597}
598
599static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
600{
601 struct usb_hcd *hcd = dev_get_drvdata(dev);
602 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
603
604 return xhci_resume(xhci, false, true);
605}
606
607const struct dev_pm_ops xhci_plat_pm_ops = {
608 .suspend = pm_sleep_ptr(xhci_plat_suspend),
609 .resume = pm_sleep_ptr(xhci_plat_resume),
610 .freeze = pm_sleep_ptr(xhci_plat_freeze),
611 .thaw = pm_sleep_ptr(xhci_plat_thaw),
612 .poweroff = pm_sleep_ptr(xhci_plat_freeze),
613 .restore = pm_sleep_ptr(xhci_plat_restore),
614
615 SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
616 xhci_plat_runtime_resume,
617 NULL)
618};
619EXPORT_SYMBOL_GPL(xhci_plat_pm_ops);
620
621#ifdef CONFIG_ACPI
622static const struct acpi_device_id usb_xhci_acpi_match[] = {
623 /* XHCI-compliant USB Controller */
624 { "PNP0D10", },
625 { "PNP0D15", },
626 { }
627};
628MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
629#endif
630
631static struct platform_driver usb_generic_xhci_driver = {
632 .probe = xhci_generic_plat_probe,
633 .remove = xhci_plat_remove,
634 .shutdown = usb_hcd_platform_shutdown,
635 .driver = {
636 .name = "xhci-hcd",
637 .pm = &xhci_plat_pm_ops,
638 .of_match_table = of_match_ptr(usb_xhci_of_match),
639 .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
640 },
641};
642MODULE_ALIAS("platform:xhci-hcd");
643
644static int __init xhci_plat_init(void)
645{
646 xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
647 return platform_driver_register(&usb_generic_xhci_driver);
648}
649module_init(xhci_plat_init);
650
651static void __exit xhci_plat_exit(void)
652{
653 platform_driver_unregister(&usb_generic_xhci_driver);
654}
655module_exit(xhci_plat_exit);
656
657MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
658MODULE_LICENSE("GPL");