xref: /linux/drivers/usb/dwc3/host.c (revision 955abe0a1b41de5ba61fe4cd614ebc123084d499)
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-port.h"
17 #include "../host/xhci-ext-caps.h"
18 #include "../host/xhci-caps.h"
19 #include "../host/xhci-plat.h"
20 #include "core.h"
21 
22 #define XHCI_HCSPARAMS1		0x4
23 #define XHCI_PORTSC_BASE	0x400
24 
25 /**
26  * dwc3_power_off_all_roothub_ports - Power off all Root hub ports
27  * @dwc: Pointer to our controller context structure
28  */
29 static void dwc3_power_off_all_roothub_ports(struct dwc3 *dwc)
30 {
31 	void __iomem *xhci_regs;
32 	u32 op_regs_base;
33 	int port_num;
34 	u32 offset;
35 	u32 reg;
36 	int i;
37 
38 	/* xhci regs is not mapped yet, do it temperary here */
39 	if (dwc->xhci_resources[0].start) {
40 		xhci_regs = ioremap(dwc->xhci_resources[0].start, DWC3_XHCI_REGS_END);
41 		if (!xhci_regs) {
42 			dev_err(dwc->dev, "Failed to ioremap xhci_regs\n");
43 			return;
44 		}
45 
46 		op_regs_base = HC_LENGTH(readl(xhci_regs));
47 		reg = readl(xhci_regs + XHCI_HCSPARAMS1);
48 		port_num = HCS_MAX_PORTS(reg);
49 
50 		for (i = 1; i <= port_num; i++) {
51 			offset = op_regs_base + XHCI_PORTSC_BASE + 0x10 * (i - 1);
52 			reg = readl(xhci_regs + offset);
53 			reg &= ~PORT_POWER;
54 			writel(reg, xhci_regs + offset);
55 		}
56 
57 		iounmap(xhci_regs);
58 	} else {
59 		dev_err(dwc->dev, "xhci base reg invalid\n");
60 	}
61 }
62 
63 static void dwc3_xhci_plat_start(struct usb_hcd *hcd)
64 {
65 	struct platform_device *pdev;
66 	struct dwc3 *dwc;
67 
68 	if (!usb_hcd_is_primary_hcd(hcd))
69 		return;
70 
71 	pdev = to_platform_device(hcd->self.controller);
72 	dwc = dev_get_drvdata(pdev->dev.parent);
73 
74 	dwc3_enable_susphy(dwc, true);
75 }
76 
77 static const struct xhci_plat_priv dwc3_xhci_plat_quirk = {
78 	.plat_start = dwc3_xhci_plat_start,
79 };
80 
81 static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
82 					int irq, char *name)
83 {
84 	struct platform_device *pdev = to_platform_device(dwc->dev);
85 	struct device_node *np = dev_of_node(&pdev->dev);
86 
87 	dwc->xhci_resources[1].start = irq;
88 	dwc->xhci_resources[1].end = irq;
89 	dwc->xhci_resources[1].flags = IORESOURCE_IRQ | irq_get_trigger_type(irq);
90 	if (!name && np)
91 		dwc->xhci_resources[1].name = of_node_full_name(pdev->dev.of_node);
92 	else
93 		dwc->xhci_resources[1].name = name;
94 }
95 
96 static int dwc3_host_get_irq(struct dwc3 *dwc)
97 {
98 	struct platform_device	*dwc3_pdev = to_platform_device(dwc->dev);
99 	int irq;
100 
101 	irq = platform_get_irq_byname_optional(dwc3_pdev, "host");
102 	if (irq > 0) {
103 		dwc3_host_fill_xhci_irq_res(dwc, irq, "host");
104 		goto out;
105 	}
106 
107 	if (irq == -EPROBE_DEFER)
108 		goto out;
109 
110 	irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
111 	if (irq > 0) {
112 		dwc3_host_fill_xhci_irq_res(dwc, irq, "dwc_usb3");
113 		goto out;
114 	}
115 
116 	if (irq == -EPROBE_DEFER)
117 		goto out;
118 
119 	irq = platform_get_irq(dwc3_pdev, 0);
120 	if (irq > 0)
121 		dwc3_host_fill_xhci_irq_res(dwc, irq, NULL);
122 
123 out:
124 	return irq;
125 }
126 
127 int dwc3_host_init(struct dwc3 *dwc)
128 {
129 	struct property_entry	props[6];
130 	struct platform_device	*xhci;
131 	int			ret, irq;
132 	int			prop_idx = 0;
133 
134 	/*
135 	 * Some platforms need to power off all Root hub ports immediately after DWC3 set to host
136 	 * mode to avoid VBUS glitch happen when xhci get reset later.
137 	 */
138 	dwc3_power_off_all_roothub_ports(dwc);
139 
140 	irq = dwc3_host_get_irq(dwc);
141 	if (irq < 0)
142 		return irq;
143 
144 	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
145 	if (!xhci) {
146 		dev_err(dwc->dev, "couldn't allocate xHCI device\n");
147 		return -ENOMEM;
148 	}
149 
150 	xhci->dev.parent	= dwc->dev;
151 
152 	dwc->xhci = xhci;
153 
154 	ret = platform_device_add_resources(xhci, dwc->xhci_resources,
155 						DWC3_XHCI_RESOURCES_NUM);
156 	if (ret) {
157 		dev_err(dwc->dev, "couldn't add resources to xHCI device\n");
158 		goto err;
159 	}
160 
161 	memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
162 
163 	props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-sg-trb-cache-size-quirk");
164 
165 	props[prop_idx++] = PROPERTY_ENTRY_BOOL("write-64-hi-lo-quirk");
166 
167 	if (dwc->usb3_lpm_capable)
168 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb3-lpm-capable");
169 
170 	if (dwc->usb2_lpm_disable)
171 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb2-lpm-disable");
172 
173 	/**
174 	 * WORKAROUND: dwc3 revisions <=3.00a have a limitation
175 	 * where Port Disable command doesn't work.
176 	 *
177 	 * The suggested workaround is that we avoid Port Disable
178 	 * completely.
179 	 *
180 	 * This following flag tells XHCI to do just that.
181 	 */
182 	if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A))
183 		props[prop_idx++] = PROPERTY_ENTRY_BOOL("quirk-broken-port-ped");
184 
185 	if (prop_idx) {
186 		ret = device_create_managed_software_node(&xhci->dev, props, NULL);
187 		if (ret) {
188 			dev_err(dwc->dev, "failed to add properties to xHCI\n");
189 			goto err;
190 		}
191 	}
192 
193 	ret = platform_device_add_data(xhci, &dwc3_xhci_plat_quirk,
194 				       sizeof(struct xhci_plat_priv));
195 	if (ret)
196 		goto err;
197 
198 	ret = platform_device_add(xhci);
199 	if (ret) {
200 		dev_err(dwc->dev, "failed to register xHCI device\n");
201 		goto err;
202 	}
203 
204 	if (dwc->sys_wakeup) {
205 		/* Restore wakeup setting if switched from device */
206 		device_wakeup_enable(dwc->sysdev);
207 
208 		/* Pass on wakeup setting to the new xhci platform device */
209 		device_init_wakeup(&xhci->dev, true);
210 	}
211 
212 	return 0;
213 err:
214 	platform_device_put(xhci);
215 	return ret;
216 }
217 
218 void dwc3_host_exit(struct dwc3 *dwc)
219 {
220 	if (dwc->sys_wakeup)
221 		device_init_wakeup(&dwc->xhci->dev, false);
222 
223 	dwc3_enable_susphy(dwc, false);
224 	platform_device_unregister(dwc->xhci);
225 	dwc->xhci = NULL;
226 }
227