xref: /openbmc/linux/drivers/usb/dwc3/host.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * host.c - DesignWare USB3 DRD Controller Host Glue
4  *
5  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com
6  *
7  * Authors: Felipe Balbi <balbi@ti.com>,
8  */
9 
10 #include <linux/irq.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/usb.h>
14 #include <linux/usb/hcd.h>
15 
16 #include "../host/xhci-plat.h"
17 #include "core.h"
18 
19 static void dwc3_xhci_plat_start(struct usb_hcd *hcd)
20 {
21 	struct platform_device *pdev;
22 	struct dwc3 *dwc;
23 
24 	if (!usb_hcd_is_primary_hcd(hcd))
25 		return;
26 
27 	pdev = to_platform_device(hcd->self.controller);
28 	dwc = dev_get_drvdata(pdev->dev.parent);
29 
30 	dwc3_enable_susphy(dwc, true);
31 }
32 
33 static const struct xhci_plat_priv dwc3_xhci_plat_quirk = {
34 	.plat_start = dwc3_xhci_plat_start,
35 };
36 
37 static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
38 					int irq, char *name)
39 {
40 	struct platform_device *pdev = to_platform_device(dwc->dev);
41 	struct device_node *np = dev_of_node(&pdev->dev);
42 
43 	dwc->xhci_resources[1].start = irq;
44 	dwc->xhci_resources[1].end = irq;
45 	dwc->xhci_resources[1].flags = IORESOURCE_IRQ | irq_get_trigger_type(irq);
46 	if (!name && np)
47 		dwc->xhci_resources[1].name = of_node_full_name(pdev->dev.of_node);
48 	else
49 		dwc->xhci_resources[1].name = name;
50 }
51 
52 static int dwc3_host_get_irq(struct dwc3 *dwc)
53 {
54 	struct platform_device	*dwc3_pdev = to_platform_device(dwc->dev);
55 	int irq;
56 
57 	irq = platform_get_irq_byname_optional(dwc3_pdev, "host");
58 	if (irq > 0) {
59 		dwc3_host_fill_xhci_irq_res(dwc, irq, "host");
60 		goto out;
61 	}
62 
63 	if (irq == -EPROBE_DEFER)
64 		goto out;
65 
66 	irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
67 	if (irq > 0) {
68 		dwc3_host_fill_xhci_irq_res(dwc, irq, "dwc_usb3");
69 		goto out;
70 	}
71 
72 	if (irq == -EPROBE_DEFER)
73 		goto out;
74 
75 	irq = platform_get_irq(dwc3_pdev, 0);
76 	if (irq > 0)
77 		dwc3_host_fill_xhci_irq_res(dwc, irq, NULL);
78 
79 out:
80 	return irq;
81 }
82 
83 int dwc3_host_init(struct dwc3 *dwc)
84 {
85 	struct property_entry	props[5];
86 	struct platform_device	*xhci;
87 	int			ret, irq;
88 	int			prop_idx = 0;
89 
90 	irq = dwc3_host_get_irq(dwc);
91 	if (irq < 0)
92 		return irq;
93 
94 	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
95 	if (!xhci) {
96 		dev_err(dwc->dev, "couldn't allocate xHCI device\n");
97 		return -ENOMEM;
98 	}
99 
100 	xhci->dev.parent	= dwc->dev;
101 
102 	dwc->xhci = xhci;
103 
104 	ret = platform_device_add_resources(xhci, dwc->xhci_resources,
105 						DWC3_XHCI_RESOURCES_NUM);
106 	if (ret) {
107 		dev_err(dwc->dev, "couldn't add resources to xHCI device\n");
108 		goto err;
109 	}
110 
111 	memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
112 
113 	props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-sg-trb-cache-size-quirk");
114 
115 	if (dwc->usb3_lpm_capable)
116 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb3-lpm-capable");
117 
118 	if (dwc->usb2_lpm_disable)
119 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb2-lpm-disable");
120 
121 	/**
122 	 * WORKAROUND: dwc3 revisions <=3.00a have a limitation
123 	 * where Port Disable command doesn't work.
124 	 *
125 	 * The suggested workaround is that we avoid Port Disable
126 	 * completely.
127 	 *
128 	 * This following flag tells XHCI to do just that.
129 	 */
130 	if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A))
131 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("quirk-broken-port-ped");
132 
133 	if (prop_idx) {
134 		ret = device_create_managed_software_node(&xhci->dev, props, NULL);
135 		if (ret) {
136 			dev_err(dwc->dev, "failed to add properties to xHCI\n");
137 			goto err;
138 		}
139 	}
140 
141 	ret = platform_device_add_data(xhci, &dwc3_xhci_plat_quirk,
142 				       sizeof(struct xhci_plat_priv));
143 	if (ret)
144 		goto err;
145 
146 	ret = platform_device_add(xhci);
147 	if (ret) {
148 		dev_err(dwc->dev, "failed to register xHCI device\n");
149 		goto err;
150 	}
151 
152 	if (dwc->sys_wakeup) {
153 		/* Restore wakeup setting if switched from device */
154 		device_wakeup_enable(dwc->sysdev);
155 
156 		/* Pass on wakeup setting to the new xhci platform device */
157 		device_init_wakeup(&xhci->dev, true);
158 	}
159 
160 	return 0;
161 err:
162 	platform_device_put(xhci);
163 	return ret;
164 }
165 
166 void dwc3_host_exit(struct dwc3 *dwc)
167 {
168 	if (dwc->sys_wakeup)
169 		device_init_wakeup(&dwc->xhci->dev, false);
170 
171 	dwc3_enable_susphy(dwc, false);
172 	platform_device_unregister(dwc->xhci);
173 	dwc->xhci = NULL;
174 }
175