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