usb: host: xhci-plat: Create platform device for onboard hubs in probe()
[linux-2.6-microblaze.git] / drivers / usb / host / xhci-plat.c
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/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>
23
24 #include "xhci.h"
25 #include "xhci-plat.h"
26 #include "xhci-mvebu.h"
27 #include "xhci-rcar.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
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
48 static 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
58 static 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
68 static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
69 {
70         struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
71
72         if (!priv->suspend_quirk)
73                 return 0;
74
75         return priv->suspend_quirk(hcd);
76 }
77
78 static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
79 {
80         struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
81
82         if (!priv->resume_quirk)
83                 return 0;
84
85         return priv->resume_quirk(hcd);
86 }
87
88 static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
89 {
90         struct xhci_plat_priv *priv = xhci_to_priv(xhci);
91
92         /*
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
96          */
97         xhci->quirks |= XHCI_PLAT | priv->quirks;
98 }
99
100 /* called during probe() after chip reset completes */
101 static int xhci_plat_setup(struct usb_hcd *hcd)
102 {
103         int ret;
104
105
106         ret = xhci_priv_init_quirk(hcd);
107         if (ret)
108                 return ret;
109
110         return xhci_gen_setup(hcd, xhci_plat_quirks);
111 }
112
113 static int xhci_plat_start(struct usb_hcd *hcd)
114 {
115         xhci_priv_plat_start(hcd);
116         return xhci_run(hcd);
117 }
118
119 #ifdef CONFIG_OF
120 static const struct xhci_plat_priv xhci_plat_marvell_armada = {
121         .init_quirk = xhci_mvebu_mbus_init_quirk,
122 };
123
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,
127 };
128
129 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
130         SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
131 };
132
133 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
134         SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
135 };
136
137 static const struct xhci_plat_priv xhci_plat_brcm = {
138         .quirks = XHCI_RESET_ON_RESUME,
139 };
140
141 static const struct of_device_id usb_xhci_of_match[] = {
142         {
143                 .compatible = "generic-xhci",
144         }, {
145                 .compatible = "xhci-platform",
146         }, {
147                 .compatible = "marvell,armada-375-xhci",
148                 .data = &xhci_plat_marvell_armada,
149         }, {
150                 .compatible = "marvell,armada-380-xhci",
151                 .data = &xhci_plat_marvell_armada,
152         }, {
153                 .compatible = "marvell,armada3700-xhci",
154                 .data = &xhci_plat_marvell_armada3700,
155         }, {
156                 .compatible = "renesas,xhci-r8a7790",
157                 .data = &xhci_plat_renesas_rcar_gen2,
158         }, {
159                 .compatible = "renesas,xhci-r8a7791",
160                 .data = &xhci_plat_renesas_rcar_gen2,
161         }, {
162                 .compatible = "renesas,xhci-r8a7793",
163                 .data = &xhci_plat_renesas_rcar_gen2,
164         }, {
165                 .compatible = "renesas,xhci-r8a7795",
166                 .data = &xhci_plat_renesas_rcar_gen3,
167         }, {
168                 .compatible = "renesas,xhci-r8a7796",
169                 .data = &xhci_plat_renesas_rcar_gen3,
170         }, {
171                 .compatible = "renesas,rcar-gen2-xhci",
172                 .data = &xhci_plat_renesas_rcar_gen2,
173         }, {
174                 .compatible = "renesas,rcar-gen3-xhci",
175                 .data = &xhci_plat_renesas_rcar_gen3,
176         }, {
177                 .compatible = "brcm,xhci-brcm-v2",
178                 .data = &xhci_plat_brcm,
179         }, {
180                 .compatible = "brcm,bcm7445-xhci",
181                 .data = &xhci_plat_brcm,
182         },
183         {},
184 };
185 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
186 #endif
187
188 static int xhci_plat_probe(struct platform_device *pdev)
189 {
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;
195         struct usb_hcd          *hcd;
196         int                     ret;
197         int                     irq;
198         struct xhci_plat_priv   *priv = NULL;
199
200
201         if (usb_disabled())
202                 return -ENODEV;
203
204         driver = &xhci_plat_hc_driver;
205
206         irq = platform_get_irq(pdev, 0);
207         if (irq < 0)
208                 return irq;
209
210         /*
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)
216          */
217         for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
218                 if (is_of_node(sysdev->fwnode) ||
219                         is_acpi_device_node(sysdev->fwnode))
220                         break;
221 #ifdef CONFIG_PCI
222                 else if (sysdev->bus == &pci_bus_type)
223                         break;
224 #endif
225         }
226
227         if (!sysdev)
228                 sysdev = &pdev->dev;
229
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,
234                                                    DMA_BIT_MASK(64));
235         else
236                 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
237
238         /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
239         if (ret) {
240                 ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
241                 if (ret)
242                         return ret;
243         }
244
245         pm_runtime_set_active(&pdev->dev);
246         pm_runtime_enable(&pdev->dev);
247         pm_runtime_get_noresume(&pdev->dev);
248
249         hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
250                                dev_name(&pdev->dev), NULL);
251         if (!hcd) {
252                 ret = -ENOMEM;
253                 goto disable_runtime;
254         }
255
256         hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
257         if (IS_ERR(hcd->regs)) {
258                 ret = PTR_ERR(hcd->regs);
259                 goto put_hcd;
260         }
261
262         hcd->rsrc_start = res->start;
263         hcd->rsrc_len = resource_size(res);
264
265         xhci = hcd_to_xhci(hcd);
266
267         /*
268          * Not all platforms have clks so it is not an error if the
269          * clock do not exist.
270          */
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);
274                 goto put_hcd;
275         }
276
277         ret = clk_prepare_enable(xhci->reg_clk);
278         if (ret)
279                 goto put_hcd;
280
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;
285         }
286
287         ret = clk_prepare_enable(xhci->clk);
288         if (ret)
289                 goto disable_reg_clk;
290
291         if (pdev->dev.of_node)
292                 priv_match = of_device_get_match_data(&pdev->dev);
293         else
294                 priv_match = dev_get_platdata(&pdev->dev);
295
296         if (priv_match) {
297                 priv = hcd_to_xhci_priv(hcd);
298                 /* Just copy data for now */
299                 *priv = *priv_match;
300         }
301
302         device_set_wakeup_capable(&pdev->dev, true);
303
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) {
308                 ret = -ENOMEM;
309                 goto disable_clk;
310         }
311
312         /* imod_interval is the interrupt moderation value in nanoseconds. */
313         xhci->imod_interval = 40000;
314
315         /* Iterate over all parent nodes for finding quirks */
316         for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
317
318                 if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
319                         xhci->quirks |= XHCI_HW_LPM_DISABLE;
320
321                 if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
322                         xhci->quirks |= XHCI_LPM_SUPPORT;
323
324                 if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
325                         xhci->quirks |= XHCI_BROKEN_PORT_PED;
326
327                 device_property_read_u32(tmpdev, "imod-interval-ns",
328                                          &xhci->imod_interval);
329         }
330
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)
335                         goto put_usb3_hcd;
336                 hcd->usb_phy = NULL;
337         } else {
338                 ret = usb_phy_init(hcd->usb_phy);
339                 if (ret)
340                         goto put_usb3_hcd;
341         }
342
343         hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
344         xhci->shared_hcd->tpl_support = hcd->tpl_support;
345
346         if (priv) {
347                 ret = xhci_priv_plat_setup(hcd);
348                 if (ret)
349                         goto disable_usb_phy;
350         }
351
352         if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
353                 hcd->skip_phy_initialization = 1;
354
355         if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
356                 xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
357
358         ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
359         if (ret)
360                 goto disable_usb_phy;
361
362         if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
363                 xhci->shared_hcd->can_do_streams = 1;
364
365         ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
366         if (ret)
367                 goto dealloc_usb2_hcd;
368
369         device_enable_async_suspend(&pdev->dev);
370         pm_runtime_put_noidle(&pdev->dev);
371
372         /*
373          * Prevent runtime pm from being on as default, users should enable
374          * runtime pm using power/control in sysfs.
375          */
376         pm_runtime_forbid(&pdev->dev);
377
378         INIT_LIST_HEAD(&xhci->onboard_hub_devs);
379         onboard_hub_create_pdevs(hcd->self.root_hub, &xhci->onboard_hub_devs);
380
381         return 0;
382
383
384 dealloc_usb2_hcd:
385         usb_remove_hcd(hcd);
386
387 disable_usb_phy:
388         usb_phy_shutdown(hcd->usb_phy);
389
390 put_usb3_hcd:
391         usb_put_hcd(xhci->shared_hcd);
392
393 disable_clk:
394         clk_disable_unprepare(xhci->clk);
395
396 disable_reg_clk:
397         clk_disable_unprepare(xhci->reg_clk);
398
399 put_hcd:
400         usb_put_hcd(hcd);
401
402 disable_runtime:
403         pm_runtime_put_noidle(&pdev->dev);
404         pm_runtime_disable(&pdev->dev);
405
406         return ret;
407 }
408
409 static int xhci_plat_remove(struct platform_device *dev)
410 {
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;
416
417         pm_runtime_get_sync(&dev->dev);
418         xhci->xhc_state |= XHCI_STATE_REMOVING;
419
420         usb_remove_hcd(shared_hcd);
421         xhci->shared_hcd = NULL;
422         usb_phy_shutdown(hcd->usb_phy);
423
424         usb_remove_hcd(hcd);
425         usb_put_hcd(shared_hcd);
426
427         onboard_hub_destroy_pdevs(&xhci->onboard_hub_devs);
428
429         clk_disable_unprepare(clk);
430         clk_disable_unprepare(reg_clk);
431         usb_put_hcd(hcd);
432
433         pm_runtime_disable(&dev->dev);
434         pm_runtime_put_noidle(&dev->dev);
435         pm_runtime_set_suspended(&dev->dev);
436
437         return 0;
438 }
439
440 static int __maybe_unused xhci_plat_suspend(struct device *dev)
441 {
442         struct usb_hcd  *hcd = dev_get_drvdata(dev);
443         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
444         int ret;
445
446         ret = xhci_priv_suspend_quirk(hcd);
447         if (ret)
448                 return ret;
449         /*
450          * xhci_suspend() needs `do_wakeup` to know whether host is allowed
451          * to do wakeup during suspend.
452          */
453         return xhci_suspend(xhci, device_may_wakeup(dev));
454 }
455
456 static int __maybe_unused xhci_plat_resume(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         ret = xhci_priv_resume_quirk(hcd);
463         if (ret)
464                 return ret;
465
466         ret = xhci_resume(xhci, 0);
467         if (ret)
468                 return ret;
469
470         pm_runtime_disable(dev);
471         pm_runtime_set_active(dev);
472         pm_runtime_enable(dev);
473
474         return 0;
475 }
476
477 static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
478 {
479         struct usb_hcd  *hcd = dev_get_drvdata(dev);
480         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
481         int ret;
482
483         ret = xhci_priv_suspend_quirk(hcd);
484         if (ret)
485                 return ret;
486
487         return xhci_suspend(xhci, true);
488 }
489
490 static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
491 {
492         struct usb_hcd  *hcd = dev_get_drvdata(dev);
493         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
494
495         return xhci_resume(xhci, 0);
496 }
497
498 static const struct dev_pm_ops xhci_plat_pm_ops = {
499         SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
500
501         SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
502                            xhci_plat_runtime_resume,
503                            NULL)
504 };
505
506 #ifdef CONFIG_ACPI
507 static const struct acpi_device_id usb_xhci_acpi_match[] = {
508         /* XHCI-compliant USB Controller */
509         { "PNP0D10", },
510         { }
511 };
512 MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
513 #endif
514
515 static struct platform_driver usb_xhci_driver = {
516         .probe  = xhci_plat_probe,
517         .remove = xhci_plat_remove,
518         .shutdown = usb_hcd_platform_shutdown,
519         .driver = {
520                 .name = "xhci-hcd",
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),
524         },
525 };
526 MODULE_ALIAS("platform:xhci-hcd");
527
528 static int __init xhci_plat_init(void)
529 {
530         xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
531         return platform_driver_register(&usb_xhci_driver);
532 }
533 module_init(xhci_plat_init);
534
535 static void __exit xhci_plat_exit(void)
536 {
537         platform_driver_unregister(&usb_xhci_driver);
538 }
539 module_exit(xhci_plat_exit);
540
541 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
542 MODULE_LICENSE("GPL");