1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * xhci-plat.c - xHCI host controller driver platform Bus Glue.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com
6*4882a593Smuzhiyun * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * A lot of code borrowed from the Linux xHCI driver.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/clk.h>
12*4882a593Smuzhiyun #include <linux/dma-mapping.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/pci.h>
15*4882a593Smuzhiyun #include <linux/of.h>
16*4882a593Smuzhiyun #include <linux/of_device.h>
17*4882a593Smuzhiyun #include <linux/platform_device.h>
18*4882a593Smuzhiyun #include <linux/usb/phy.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/acpi.h>
21*4882a593Smuzhiyun #include <linux/usb/of.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "xhci.h"
24*4882a593Smuzhiyun #include "xhci-plat.h"
25*4882a593Smuzhiyun #include "xhci-mvebu.h"
26*4882a593Smuzhiyun #include "xhci-rcar.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun static struct hc_driver __read_mostly xhci_plat_hc_driver;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun static int xhci_plat_setup(struct usb_hcd *hcd);
31*4882a593Smuzhiyun static int xhci_plat_start(struct usb_hcd *hcd);
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
34*4882a593Smuzhiyun .extra_priv_size = sizeof(struct xhci_plat_priv),
35*4882a593Smuzhiyun .reset = xhci_plat_setup,
36*4882a593Smuzhiyun .start = xhci_plat_start,
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
xhci_priv_plat_start(struct usb_hcd * hcd)39*4882a593Smuzhiyun static void xhci_priv_plat_start(struct usb_hcd *hcd)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (priv->plat_start)
44*4882a593Smuzhiyun priv->plat_start(hcd);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
xhci_priv_plat_setup(struct usb_hcd * hcd)47*4882a593Smuzhiyun static int xhci_priv_plat_setup(struct usb_hcd *hcd)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (!priv->plat_setup)
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun return priv->plat_setup(hcd);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
xhci_priv_init_quirk(struct usb_hcd * hcd)57*4882a593Smuzhiyun static int xhci_priv_init_quirk(struct usb_hcd *hcd)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun if (!priv->init_quirk)
62*4882a593Smuzhiyun return 0;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return priv->init_quirk(hcd);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
xhci_priv_suspend_quirk(struct usb_hcd * hcd)67*4882a593Smuzhiyun static int xhci_priv_suspend_quirk(struct usb_hcd *hcd)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (!priv->suspend_quirk)
72*4882a593Smuzhiyun return 0;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun return priv->suspend_quirk(hcd);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
xhci_priv_resume_quirk(struct usb_hcd * hcd)77*4882a593Smuzhiyun static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (!priv->resume_quirk)
82*4882a593Smuzhiyun return 0;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return priv->resume_quirk(hcd);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
xhci_plat_quirks(struct device * dev,struct xhci_hcd * xhci)87*4882a593Smuzhiyun static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun struct xhci_plat_priv *priv = xhci_to_priv(xhci);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * As of now platform drivers don't provide MSI support so we ensure
93*4882a593Smuzhiyun * here that the generic code does not try to make a pci_dev from our
94*4882a593Smuzhiyun * dev struct in order to setup MSI
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun xhci->quirks |= XHCI_PLAT | priv->quirks;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* called during probe() after chip reset completes */
xhci_plat_setup(struct usb_hcd * hcd)100*4882a593Smuzhiyun static int xhci_plat_setup(struct usb_hcd *hcd)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun int ret;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun ret = xhci_priv_init_quirk(hcd);
106*4882a593Smuzhiyun if (ret)
107*4882a593Smuzhiyun return ret;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return xhci_gen_setup(hcd, xhci_plat_quirks);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
xhci_plat_start(struct usb_hcd * hcd)112*4882a593Smuzhiyun static int xhci_plat_start(struct usb_hcd *hcd)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun xhci_priv_plat_start(hcd);
115*4882a593Smuzhiyun return xhci_run(hcd);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #ifdef CONFIG_OF
119*4882a593Smuzhiyun static const struct xhci_plat_priv xhci_plat_marvell_armada = {
120*4882a593Smuzhiyun .init_quirk = xhci_mvebu_mbus_init_quirk,
121*4882a593Smuzhiyun };
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
124*4882a593Smuzhiyun .plat_setup = xhci_mvebu_a3700_plat_setup,
125*4882a593Smuzhiyun .init_quirk = xhci_mvebu_a3700_init_quirk,
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
129*4882a593Smuzhiyun SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
133*4882a593Smuzhiyun SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun static const struct xhci_plat_priv xhci_plat_brcm = {
137*4882a593Smuzhiyun .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun static const struct of_device_id usb_xhci_of_match[] = {
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun .compatible = "generic-xhci",
143*4882a593Smuzhiyun }, {
144*4882a593Smuzhiyun .compatible = "xhci-platform",
145*4882a593Smuzhiyun #ifndef CONFIG_ARCH_ROCKCHIP
146*4882a593Smuzhiyun }, {
147*4882a593Smuzhiyun .compatible = "marvell,armada-375-xhci",
148*4882a593Smuzhiyun .data = &xhci_plat_marvell_armada,
149*4882a593Smuzhiyun }, {
150*4882a593Smuzhiyun .compatible = "marvell,armada-380-xhci",
151*4882a593Smuzhiyun .data = &xhci_plat_marvell_armada,
152*4882a593Smuzhiyun }, {
153*4882a593Smuzhiyun .compatible = "marvell,armada3700-xhci",
154*4882a593Smuzhiyun .data = &xhci_plat_marvell_armada3700,
155*4882a593Smuzhiyun }, {
156*4882a593Smuzhiyun .compatible = "renesas,xhci-r8a7790",
157*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen2,
158*4882a593Smuzhiyun }, {
159*4882a593Smuzhiyun .compatible = "renesas,xhci-r8a7791",
160*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen2,
161*4882a593Smuzhiyun }, {
162*4882a593Smuzhiyun .compatible = "renesas,xhci-r8a7793",
163*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen2,
164*4882a593Smuzhiyun }, {
165*4882a593Smuzhiyun .compatible = "renesas,xhci-r8a7795",
166*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen3,
167*4882a593Smuzhiyun }, {
168*4882a593Smuzhiyun .compatible = "renesas,xhci-r8a7796",
169*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen3,
170*4882a593Smuzhiyun }, {
171*4882a593Smuzhiyun .compatible = "renesas,rcar-gen2-xhci",
172*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen2,
173*4882a593Smuzhiyun }, {
174*4882a593Smuzhiyun .compatible = "renesas,rcar-gen3-xhci",
175*4882a593Smuzhiyun .data = &xhci_plat_renesas_rcar_gen3,
176*4882a593Smuzhiyun }, {
177*4882a593Smuzhiyun .compatible = "brcm,xhci-brcm-v2",
178*4882a593Smuzhiyun .data = &xhci_plat_brcm,
179*4882a593Smuzhiyun }, {
180*4882a593Smuzhiyun .compatible = "brcm,bcm7445-xhci",
181*4882a593Smuzhiyun .data = &xhci_plat_brcm,
182*4882a593Smuzhiyun #endif
183*4882a593Smuzhiyun },
184*4882a593Smuzhiyun {},
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
187*4882a593Smuzhiyun #endif
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun static struct xhci_plat_priv_overwrite xhci_plat_vendor_overwrite;
190*4882a593Smuzhiyun
xhci_plat_register_vendor_ops(struct xhci_vendor_ops * vendor_ops)191*4882a593Smuzhiyun int xhci_plat_register_vendor_ops(struct xhci_vendor_ops *vendor_ops)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun if (vendor_ops == NULL)
194*4882a593Smuzhiyun return -EINVAL;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun xhci_plat_vendor_overwrite.vendor_ops = vendor_ops;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_plat_register_vendor_ops);
201*4882a593Smuzhiyun
xhci_vendor_init(struct xhci_hcd * xhci)202*4882a593Smuzhiyun static int xhci_vendor_init(struct xhci_hcd *xhci)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun struct xhci_vendor_ops *ops = NULL;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (xhci_plat_vendor_overwrite.vendor_ops)
207*4882a593Smuzhiyun ops = xhci->vendor_ops = xhci_plat_vendor_overwrite.vendor_ops;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (ops && ops->vendor_init)
210*4882a593Smuzhiyun return ops->vendor_init(xhci);
211*4882a593Smuzhiyun return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
xhci_vendor_cleanup(struct xhci_hcd * xhci)214*4882a593Smuzhiyun static void xhci_vendor_cleanup(struct xhci_hcd *xhci)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun if (ops && ops->vendor_cleanup)
219*4882a593Smuzhiyun ops->vendor_cleanup(xhci);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun xhci->vendor_ops = NULL;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
xhci_plat_probe(struct platform_device * pdev)224*4882a593Smuzhiyun static int xhci_plat_probe(struct platform_device *pdev)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun const struct xhci_plat_priv *priv_match;
227*4882a593Smuzhiyun const struct hc_driver *driver;
228*4882a593Smuzhiyun struct device *sysdev, *tmpdev;
229*4882a593Smuzhiyun struct xhci_hcd *xhci;
230*4882a593Smuzhiyun struct resource *res;
231*4882a593Smuzhiyun struct usb_hcd *hcd;
232*4882a593Smuzhiyun int ret;
233*4882a593Smuzhiyun int irq;
234*4882a593Smuzhiyun struct xhci_plat_priv *priv = NULL;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (usb_disabled())
238*4882a593Smuzhiyun return -ENODEV;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun driver = &xhci_plat_hc_driver;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
243*4882a593Smuzhiyun if (irq < 0)
244*4882a593Smuzhiyun return irq;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * sysdev must point to a device that is known to the system firmware
248*4882a593Smuzhiyun * or PCI hardware. We handle these three cases here:
249*4882a593Smuzhiyun * 1. xhci_plat comes from firmware
250*4882a593Smuzhiyun * 2. xhci_plat is child of a device from firmware (dwc3-plat)
251*4882a593Smuzhiyun * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
252*4882a593Smuzhiyun */
253*4882a593Smuzhiyun for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
254*4882a593Smuzhiyun if (is_of_node(sysdev->fwnode) ||
255*4882a593Smuzhiyun is_acpi_device_node(sysdev->fwnode))
256*4882a593Smuzhiyun break;
257*4882a593Smuzhiyun #ifdef CONFIG_PCI
258*4882a593Smuzhiyun else if (sysdev->bus == &pci_bus_type)
259*4882a593Smuzhiyun break;
260*4882a593Smuzhiyun #endif
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (!sysdev)
264*4882a593Smuzhiyun sysdev = &pdev->dev;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* Try to set 64-bit DMA first */
267*4882a593Smuzhiyun if (WARN_ON(!sysdev->dma_mask))
268*4882a593Smuzhiyun /* Platform did not initialize dma_mask */
269*4882a593Smuzhiyun ret = dma_coerce_mask_and_coherent(sysdev,
270*4882a593Smuzhiyun DMA_BIT_MASK(64));
271*4882a593Smuzhiyun else
272*4882a593Smuzhiyun ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
275*4882a593Smuzhiyun if (ret) {
276*4882a593Smuzhiyun ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
277*4882a593Smuzhiyun if (ret)
278*4882a593Smuzhiyun return ret;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun pm_runtime_set_active(&pdev->dev);
282*4882a593Smuzhiyun pm_runtime_enable(&pdev->dev);
283*4882a593Smuzhiyun pm_runtime_get_noresume(&pdev->dev);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
286*4882a593Smuzhiyun dev_name(&pdev->dev), NULL);
287*4882a593Smuzhiyun if (!hcd) {
288*4882a593Smuzhiyun ret = -ENOMEM;
289*4882a593Smuzhiyun goto disable_runtime;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
293*4882a593Smuzhiyun if (IS_ERR(hcd->regs)) {
294*4882a593Smuzhiyun ret = PTR_ERR(hcd->regs);
295*4882a593Smuzhiyun goto put_hcd;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun hcd->rsrc_start = res->start;
299*4882a593Smuzhiyun hcd->rsrc_len = resource_size(res);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * Not all platforms have clks so it is not an error if the
305*4882a593Smuzhiyun * clock do not exist.
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
308*4882a593Smuzhiyun if (IS_ERR(xhci->reg_clk)) {
309*4882a593Smuzhiyun ret = PTR_ERR(xhci->reg_clk);
310*4882a593Smuzhiyun goto put_hcd;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun ret = clk_prepare_enable(xhci->reg_clk);
314*4882a593Smuzhiyun if (ret)
315*4882a593Smuzhiyun goto put_hcd;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
318*4882a593Smuzhiyun if (IS_ERR(xhci->clk)) {
319*4882a593Smuzhiyun ret = PTR_ERR(xhci->clk);
320*4882a593Smuzhiyun goto disable_reg_clk;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun ret = clk_prepare_enable(xhci->clk);
324*4882a593Smuzhiyun if (ret)
325*4882a593Smuzhiyun goto disable_reg_clk;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun if (pdev->dev.of_node)
328*4882a593Smuzhiyun priv_match = of_device_get_match_data(&pdev->dev);
329*4882a593Smuzhiyun else
330*4882a593Smuzhiyun priv_match = dev_get_platdata(&pdev->dev);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun if (priv_match) {
333*4882a593Smuzhiyun priv = hcd_to_xhci_priv(hcd);
334*4882a593Smuzhiyun /* Just copy data for now */
335*4882a593Smuzhiyun *priv = *priv_match;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun device_set_wakeup_capable(&pdev->dev, true);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun xhci->main_hcd = hcd;
341*4882a593Smuzhiyun xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
342*4882a593Smuzhiyun dev_name(&pdev->dev), hcd);
343*4882a593Smuzhiyun if (!xhci->shared_hcd) {
344*4882a593Smuzhiyun ret = -ENOMEM;
345*4882a593Smuzhiyun goto disable_clk;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /* imod_interval is the interrupt moderation value in nanoseconds. */
349*4882a593Smuzhiyun xhci->imod_interval = 40000;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Iterate over all parent nodes for finding quirks */
352*4882a593Smuzhiyun for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
355*4882a593Smuzhiyun xhci->quirks |= XHCI_HW_LPM_DISABLE;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
358*4882a593Smuzhiyun xhci->quirks |= XHCI_LPM_SUPPORT;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
361*4882a593Smuzhiyun xhci->quirks |= XHCI_BROKEN_PORT_PED;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (device_property_read_bool(tmpdev, "quirk-skip-phy-init"))
364*4882a593Smuzhiyun xhci->quirks |= XHCI_SKIP_PHY_INIT;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun if (device_property_read_bool(tmpdev,
367*4882a593Smuzhiyun "xhci-u2-broken-suspend"))
368*4882a593Smuzhiyun xhci->quirks |= XHCI_U2_BROKEN_SUSPEND;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun device_property_read_u32(tmpdev, "imod-interval-ns",
371*4882a593Smuzhiyun &xhci->imod_interval);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
375*4882a593Smuzhiyun if (IS_ERR(hcd->usb_phy)) {
376*4882a593Smuzhiyun ret = PTR_ERR(hcd->usb_phy);
377*4882a593Smuzhiyun if (ret == -EPROBE_DEFER)
378*4882a593Smuzhiyun goto put_usb3_hcd;
379*4882a593Smuzhiyun hcd->usb_phy = NULL;
380*4882a593Smuzhiyun } else {
381*4882a593Smuzhiyun ret = usb_phy_init(hcd->usb_phy);
382*4882a593Smuzhiyun if (ret)
383*4882a593Smuzhiyun goto put_usb3_hcd;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun ret = xhci_vendor_init(xhci);
387*4882a593Smuzhiyun if (ret)
388*4882a593Smuzhiyun goto disable_usb_phy;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
391*4882a593Smuzhiyun xhci->shared_hcd->tpl_support = hcd->tpl_support;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun if (priv) {
394*4882a593Smuzhiyun ret = xhci_priv_plat_setup(hcd);
395*4882a593Smuzhiyun if (ret)
396*4882a593Smuzhiyun goto disable_usb_phy;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
400*4882a593Smuzhiyun hcd->skip_phy_initialization = 1;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
403*4882a593Smuzhiyun xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
406*4882a593Smuzhiyun if (ret)
407*4882a593Smuzhiyun goto disable_usb_phy;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
410*4882a593Smuzhiyun xhci->shared_hcd->can_do_streams = 1;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
413*4882a593Smuzhiyun if (ret)
414*4882a593Smuzhiyun goto dealloc_usb2_hcd;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun device_enable_async_suspend(&pdev->dev);
417*4882a593Smuzhiyun pm_runtime_put_noidle(&pdev->dev);
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /*
420*4882a593Smuzhiyun * Prevent runtime pm from being on as default, users should enable
421*4882a593Smuzhiyun * runtime pm using power/control in sysfs.
422*4882a593Smuzhiyun */
423*4882a593Smuzhiyun pm_runtime_forbid(&pdev->dev);
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun return 0;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun dealloc_usb2_hcd:
429*4882a593Smuzhiyun usb_remove_hcd(hcd);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun disable_usb_phy:
432*4882a593Smuzhiyun usb_phy_shutdown(hcd->usb_phy);
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun put_usb3_hcd:
435*4882a593Smuzhiyun usb_put_hcd(xhci->shared_hcd);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun disable_clk:
438*4882a593Smuzhiyun clk_disable_unprepare(xhci->clk);
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun disable_reg_clk:
441*4882a593Smuzhiyun clk_disable_unprepare(xhci->reg_clk);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun put_hcd:
444*4882a593Smuzhiyun usb_put_hcd(hcd);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun disable_runtime:
447*4882a593Smuzhiyun pm_runtime_put_noidle(&pdev->dev);
448*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun return ret;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
xhci_plat_remove(struct platform_device * dev)453*4882a593Smuzhiyun static int xhci_plat_remove(struct platform_device *dev)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(dev);
456*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
457*4882a593Smuzhiyun struct clk *clk = xhci->clk;
458*4882a593Smuzhiyun struct clk *reg_clk = xhci->reg_clk;
459*4882a593Smuzhiyun struct usb_hcd *shared_hcd = xhci->shared_hcd;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun pm_runtime_get_sync(&dev->dev);
462*4882a593Smuzhiyun xhci->xhc_state |= XHCI_STATE_REMOVING;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun usb_remove_hcd(shared_hcd);
465*4882a593Smuzhiyun xhci->shared_hcd = NULL;
466*4882a593Smuzhiyun usb_phy_shutdown(hcd->usb_phy);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun usb_remove_hcd(hcd);
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun xhci_vendor_cleanup(xhci);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun usb_put_hcd(shared_hcd);
473*4882a593Smuzhiyun clk_disable_unprepare(clk);
474*4882a593Smuzhiyun clk_disable_unprepare(reg_clk);
475*4882a593Smuzhiyun usb_put_hcd(hcd);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun pm_runtime_disable(&dev->dev);
478*4882a593Smuzhiyun pm_runtime_put_noidle(&dev->dev);
479*4882a593Smuzhiyun pm_runtime_set_suspended(&dev->dev);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
xhci_plat_suspend(struct device * dev)484*4882a593Smuzhiyun static int __maybe_unused xhci_plat_suspend(struct device *dev)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
487*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
488*4882a593Smuzhiyun int ret;
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun if (pm_runtime_suspended(dev))
491*4882a593Smuzhiyun pm_runtime_resume(dev);
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun ret = xhci_priv_suspend_quirk(hcd);
494*4882a593Smuzhiyun if (ret)
495*4882a593Smuzhiyun return ret;
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun * xhci_suspend() needs `do_wakeup` to know whether host is allowed
498*4882a593Smuzhiyun * to do wakeup during suspend.
499*4882a593Smuzhiyun */
500*4882a593Smuzhiyun ret = xhci_suspend(xhci, device_may_wakeup(dev));
501*4882a593Smuzhiyun if (ret)
502*4882a593Smuzhiyun return ret;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
505*4882a593Smuzhiyun clk_disable_unprepare(xhci->clk);
506*4882a593Smuzhiyun clk_disable_unprepare(xhci->reg_clk);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun return 0;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
xhci_plat_resume(struct device * dev)512*4882a593Smuzhiyun static int __maybe_unused xhci_plat_resume(struct device *dev)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
515*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
516*4882a593Smuzhiyun int ret;
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
519*4882a593Smuzhiyun clk_prepare_enable(xhci->clk);
520*4882a593Smuzhiyun clk_prepare_enable(xhci->reg_clk);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun ret = xhci_priv_resume_quirk(hcd);
524*4882a593Smuzhiyun if (ret)
525*4882a593Smuzhiyun return ret;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun ret = xhci_resume(xhci, 0);
528*4882a593Smuzhiyun if (ret)
529*4882a593Smuzhiyun return ret;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun pm_runtime_disable(dev);
532*4882a593Smuzhiyun pm_runtime_set_active(dev);
533*4882a593Smuzhiyun pm_runtime_enable(dev);
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun return 0;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
xhci_plat_runtime_suspend(struct device * dev)538*4882a593Smuzhiyun static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
541*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
542*4882a593Smuzhiyun int ret;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun ret = xhci_priv_suspend_quirk(hcd);
545*4882a593Smuzhiyun if (ret)
546*4882a593Smuzhiyun return ret;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun return xhci_suspend(xhci, true);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
xhci_plat_runtime_resume(struct device * dev)551*4882a593Smuzhiyun static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
554*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun return xhci_resume(xhci, 0);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun static const struct dev_pm_ops xhci_plat_pm_ops = {
560*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
563*4882a593Smuzhiyun xhci_plat_runtime_resume,
564*4882a593Smuzhiyun NULL)
565*4882a593Smuzhiyun };
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun #ifdef CONFIG_ACPI
568*4882a593Smuzhiyun static const struct acpi_device_id usb_xhci_acpi_match[] = {
569*4882a593Smuzhiyun /* XHCI-compliant USB Controller */
570*4882a593Smuzhiyun { "PNP0D10", },
571*4882a593Smuzhiyun { }
572*4882a593Smuzhiyun };
573*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
574*4882a593Smuzhiyun #endif
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun static struct platform_driver usb_xhci_driver = {
577*4882a593Smuzhiyun .probe = xhci_plat_probe,
578*4882a593Smuzhiyun .remove = xhci_plat_remove,
579*4882a593Smuzhiyun .shutdown = usb_hcd_platform_shutdown,
580*4882a593Smuzhiyun .driver = {
581*4882a593Smuzhiyun .name = "xhci-hcd",
582*4882a593Smuzhiyun .pm = &xhci_plat_pm_ops,
583*4882a593Smuzhiyun .of_match_table = of_match_ptr(usb_xhci_of_match),
584*4882a593Smuzhiyun .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
585*4882a593Smuzhiyun },
586*4882a593Smuzhiyun };
587*4882a593Smuzhiyun MODULE_ALIAS("platform:xhci-hcd");
588*4882a593Smuzhiyun
xhci_plat_init(void)589*4882a593Smuzhiyun static int __init xhci_plat_init(void)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
592*4882a593Smuzhiyun return platform_driver_register(&usb_xhci_driver);
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun module_init(xhci_plat_init);
595*4882a593Smuzhiyun
xhci_plat_exit(void)596*4882a593Smuzhiyun static void __exit xhci_plat_exit(void)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun platform_driver_unregister(&usb_xhci_driver);
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun module_exit(xhci_plat_exit);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
603*4882a593Smuzhiyun MODULE_LICENSE("GPL");
604