• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xHCI host controller driver PCI Bus Glue.
4  *
5  * Copyright (C) 2008 Intel Corp.
6  *
7  * Author: Sarah Sharp
8  * Some code borrowed from the Linux EHCI driver.
9  */
10 
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/reset.h>
16 
17 #include "xhci.h"
18 #include "xhci-trace.h"
19 #include "xhci-pci.h"
20 
21 #define SSIC_PORT_NUM		2
22 #define SSIC_PORT_CFG2		0x880c
23 #define SSIC_PORT_CFG2_OFFSET	0x30
24 #define PROG_DONE		(1 << 30)
25 #define SSIC_PORT_UNUSED	(1 << 31)
26 #define SPARSE_DISABLE_BIT	17
27 #define SPARSE_CNTL_ENABLE	0xC12C
28 
29 /* Device for a quirk */
30 #define PCI_VENDOR_ID_FRESCO_LOGIC	0x1b73
31 #define PCI_DEVICE_ID_FRESCO_LOGIC_PDK	0x1000
32 #define PCI_DEVICE_ID_FRESCO_LOGIC_FL1009	0x1009
33 #define PCI_DEVICE_ID_FRESCO_LOGIC_FL1100	0x1100
34 #define PCI_DEVICE_ID_FRESCO_LOGIC_FL1400	0x1400
35 
36 #define PCI_VENDOR_ID_ETRON		0x1b6f
37 #define PCI_DEVICE_ID_EJ168		0x7023
38 
39 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_XHCI	0x8c31
40 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
41 #define PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_XHCI	0x9cb1
42 #define PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI		0x22b5
43 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI		0xa12f
44 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI	0x9d2f
45 #define PCI_DEVICE_ID_INTEL_BROXTON_M_XHCI		0x0aa8
46 #define PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI		0x1aa8
47 #define PCI_DEVICE_ID_INTEL_APL_XHCI			0x5aa8
48 #define PCI_DEVICE_ID_INTEL_DNV_XHCI			0x19d0
49 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_XHCI	0x15b5
50 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_XHCI	0x15b6
51 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_XHCI	0x15c1
52 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_XHCI	0x15db
53 #define PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_XHCI	0x15d4
54 #define PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_XHCI		0x15e9
55 #define PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_XHCI		0x15ec
56 #define PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_XHCI		0x15f0
57 #define PCI_DEVICE_ID_INTEL_ICE_LAKE_XHCI		0x8a13
58 #define PCI_DEVICE_ID_INTEL_CML_XHCI			0xa3af
59 #define PCI_DEVICE_ID_INTEL_TIGER_LAKE_XHCI		0x9a13
60 #define PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_XHCI		0x1138
61 #define PCI_DEVICE_ID_INTEL_ALDER_LAKE_PCH_XHCI		0x51ed
62 #define PCI_DEVICE_ID_INTEL_ALDER_LAKE_N_PCH_XHCI	0x54ed
63 
64 #define PCI_DEVICE_ID_AMD_PROMONTORYA_4			0x43b9
65 #define PCI_DEVICE_ID_AMD_PROMONTORYA_3			0x43ba
66 #define PCI_DEVICE_ID_AMD_PROMONTORYA_2			0x43bb
67 #define PCI_DEVICE_ID_AMD_PROMONTORYA_1			0x43bc
68 
69 #define PCI_DEVICE_ID_ASMEDIA_1042_XHCI			0x1042
70 #define PCI_DEVICE_ID_ASMEDIA_1042A_XHCI		0x1142
71 #define PCI_DEVICE_ID_ASMEDIA_1142_XHCI			0x1242
72 #define PCI_DEVICE_ID_ASMEDIA_2142_XHCI			0x2142
73 #define PCI_DEVICE_ID_ASMEDIA_3242_XHCI			0x3242
74 
75 static const char hcd_name[] = "xhci_hcd";
76 
77 static struct hc_driver __read_mostly xhci_pci_hc_driver;
78 
79 static int xhci_pci_setup(struct usb_hcd *hcd);
80 static int xhci_pci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
81 				      struct usb_tt *tt, gfp_t mem_flags);
82 
83 static const struct xhci_driver_overrides xhci_pci_overrides __initconst = {
84 	.reset = xhci_pci_setup,
85 	.update_hub_device = xhci_pci_update_hub_device,
86 };
87 
88 /* called after powerup, by probe or system-pm "wakeup" */
xhci_pci_reinit(struct xhci_hcd * xhci,struct pci_dev * pdev)89 static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
90 {
91 	/*
92 	 * TODO: Implement finding debug ports later.
93 	 * TODO: see if there are any quirks that need to be added to handle
94 	 * new extended capabilities.
95 	 */
96 
97 	/* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
98 	if (!pci_set_mwi(pdev))
99 		xhci_dbg(xhci, "MWI active\n");
100 
101 	xhci_dbg(xhci, "Finished xhci_pci_reinit\n");
102 	return 0;
103 }
104 
xhci_pci_quirks(struct device * dev,struct xhci_hcd * xhci)105 static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
106 {
107 	struct pci_dev                  *pdev = to_pci_dev(dev);
108 	struct xhci_driver_data         *driver_data;
109 	const struct pci_device_id      *id;
110 
111 	id = pci_match_id(pdev->driver->id_table, pdev);
112 
113 	if (id && id->driver_data) {
114 		driver_data = (struct xhci_driver_data *)id->driver_data;
115 		xhci->quirks |= driver_data->quirks;
116 	}
117 
118 	/* Look for vendor-specific quirks */
119 	if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
120 			(pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK ||
121 			 pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_FL1400)) {
122 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK &&
123 				pdev->revision == 0x0) {
124 			xhci->quirks |= XHCI_RESET_EP_QUIRK;
125 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
126 				"QUIRK: Fresco Logic xHC needs configure"
127 				" endpoint cmd after reset endpoint");
128 		}
129 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK &&
130 				pdev->revision == 0x4) {
131 			xhci->quirks |= XHCI_SLOW_SUSPEND;
132 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
133 				"QUIRK: Fresco Logic xHC revision %u"
134 				"must be suspended extra slowly",
135 				pdev->revision);
136 		}
137 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK)
138 			xhci->quirks |= XHCI_BROKEN_STREAMS;
139 		/* Fresco Logic confirms: all revisions of this chip do not
140 		 * support MSI, even though some of them claim to in their PCI
141 		 * capabilities.
142 		 */
143 		xhci->quirks |= XHCI_BROKEN_MSI;
144 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
145 				"QUIRK: Fresco Logic revision %u "
146 				"has broken MSI implementation",
147 				pdev->revision);
148 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
149 	}
150 
151 	if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
152 			pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_FL1009)
153 		xhci->quirks |= XHCI_BROKEN_STREAMS;
154 
155 	if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
156 			pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_FL1100)
157 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
158 
159 	if (pdev->vendor == PCI_VENDOR_ID_NEC)
160 		xhci->quirks |= XHCI_NEC_HOST;
161 
162 	if (pdev->vendor == PCI_VENDOR_ID_AMD && xhci->hci_version == 0x96)
163 		xhci->quirks |= XHCI_AMD_0x96_HOST;
164 
165 	/* AMD PLL quirk */
166 	if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_quirk_pll_check())
167 		xhci->quirks |= XHCI_AMD_PLL_FIX;
168 
169 	if (pdev->vendor == PCI_VENDOR_ID_AMD &&
170 		(pdev->device == 0x145c ||
171 		 pdev->device == 0x15e0 ||
172 		 pdev->device == 0x15e1 ||
173 		 pdev->device == 0x43bb))
174 		xhci->quirks |= XHCI_SUSPEND_DELAY;
175 
176 	if (pdev->vendor == PCI_VENDOR_ID_AMD &&
177 	    (pdev->device == 0x15e0 || pdev->device == 0x15e1))
178 		xhci->quirks |= XHCI_SNPS_BROKEN_SUSPEND;
179 
180 	if (pdev->vendor == PCI_VENDOR_ID_AMD && pdev->device == 0x15e5) {
181 		xhci->quirks |= XHCI_DISABLE_SPARSE;
182 		xhci->quirks |= XHCI_RESET_ON_RESUME;
183 	}
184 
185 	if (pdev->vendor == PCI_VENDOR_ID_AMD)
186 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
187 
188 	if ((pdev->vendor == PCI_VENDOR_ID_AMD) &&
189 		((pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_4) ||
190 		(pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_3) ||
191 		(pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_2) ||
192 		(pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_1)))
193 		xhci->quirks |= XHCI_U2_DISABLE_WAKE;
194 
195 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
196 		xhci->quirks |= XHCI_LPM_SUPPORT;
197 		xhci->quirks |= XHCI_INTEL_HOST;
198 		xhci->quirks |= XHCI_AVOID_BEI;
199 	}
200 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
201 			pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI) {
202 		xhci->quirks |= XHCI_EP_LIMIT_QUIRK;
203 		xhci->limit_active_eps = 64;
204 		xhci->quirks |= XHCI_SW_BW_CHECKING;
205 		/*
206 		 * PPT desktop boards DH77EB and DH77DF will power back on after
207 		 * a few seconds of being shutdown.  The fix for this is to
208 		 * switch the ports from xHCI to EHCI on shutdown.  We can't use
209 		 * DMI information to find those particular boards (since each
210 		 * vendor will change the board name), so we have to key off all
211 		 * PPT chipsets.
212 		 */
213 		xhci->quirks |= XHCI_SPURIOUS_REBOOT;
214 	}
215 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
216 		(pdev->device == PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI ||
217 		 pdev->device == PCI_DEVICE_ID_INTEL_WILDCATPOINT_LP_XHCI)) {
218 		xhci->quirks |= XHCI_SPURIOUS_REBOOT;
219 		xhci->quirks |= XHCI_SPURIOUS_WAKEUP;
220 	}
221 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
222 		(pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI ||
223 		 pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI ||
224 		 pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI ||
225 		 pdev->device == PCI_DEVICE_ID_INTEL_BROXTON_M_XHCI ||
226 		 pdev->device == PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI ||
227 		 pdev->device == PCI_DEVICE_ID_INTEL_APL_XHCI ||
228 		 pdev->device == PCI_DEVICE_ID_INTEL_DNV_XHCI ||
229 		 pdev->device == PCI_DEVICE_ID_INTEL_CML_XHCI)) {
230 		xhci->quirks |= XHCI_PME_STUCK_QUIRK;
231 	}
232 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
233 	    pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI)
234 		xhci->quirks |= XHCI_SSIC_PORT_UNUSED;
235 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
236 	    (pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI ||
237 	     pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI ||
238 	     pdev->device == PCI_DEVICE_ID_INTEL_APL_XHCI))
239 		xhci->quirks |= XHCI_INTEL_USB_ROLE_SW;
240 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
241 	    (pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI ||
242 	     pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI ||
243 	     pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI ||
244 	     pdev->device == PCI_DEVICE_ID_INTEL_APL_XHCI ||
245 	     pdev->device == PCI_DEVICE_ID_INTEL_DNV_XHCI))
246 		xhci->quirks |= XHCI_MISSING_CAS;
247 
248 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
249 	    (pdev->device == PCI_DEVICE_ID_INTEL_ALDER_LAKE_PCH_XHCI ||
250 	     pdev->device == PCI_DEVICE_ID_INTEL_ALDER_LAKE_N_PCH_XHCI))
251 		xhci->quirks |= XHCI_RESET_TO_DEFAULT;
252 
253 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
254 	    (pdev->device == PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_XHCI ||
255 	     pdev->device == PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_XHCI ||
256 	     pdev->device == PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_XHCI ||
257 	     pdev->device == PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_XHCI ||
258 	     pdev->device == PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_XHCI ||
259 	     pdev->device == PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_XHCI ||
260 	     pdev->device == PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_XHCI ||
261 	     pdev->device == PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_XHCI ||
262 	     pdev->device == PCI_DEVICE_ID_INTEL_ICE_LAKE_XHCI ||
263 	     pdev->device == PCI_DEVICE_ID_INTEL_TIGER_LAKE_XHCI ||
264 	     pdev->device == PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_XHCI))
265 		xhci->quirks |= XHCI_DEFAULT_PM_RUNTIME_ALLOW;
266 
267 	if (pdev->vendor == PCI_VENDOR_ID_ETRON &&
268 			pdev->device == PCI_DEVICE_ID_EJ168) {
269 		xhci->quirks |= XHCI_RESET_ON_RESUME;
270 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
271 		xhci->quirks |= XHCI_BROKEN_STREAMS;
272 	}
273 	if (pdev->vendor == PCI_VENDOR_ID_RENESAS &&
274 	    pdev->device == 0x0014) {
275 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
276 		xhci->quirks |= XHCI_ZERO_64B_REGS;
277 	}
278 	if (pdev->vendor == PCI_VENDOR_ID_RENESAS &&
279 	    pdev->device == 0x0015) {
280 		xhci->quirks |= XHCI_RESET_ON_RESUME;
281 		xhci->quirks |= XHCI_ZERO_64B_REGS;
282 	}
283 	if (pdev->vendor == PCI_VENDOR_ID_VIA)
284 		xhci->quirks |= XHCI_RESET_ON_RESUME;
285 
286 	/* See https://bugzilla.kernel.org/show_bug.cgi?id=79511 */
287 	if (pdev->vendor == PCI_VENDOR_ID_VIA &&
288 			pdev->device == 0x3432)
289 		xhci->quirks |= XHCI_BROKEN_STREAMS;
290 
291 	if (pdev->vendor == PCI_VENDOR_ID_VIA && pdev->device == 0x3483) {
292 		xhci->quirks |= XHCI_LPM_SUPPORT;
293 		xhci->quirks |= XHCI_EP_CTX_BROKEN_DCS;
294 	}
295 
296 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
297 		pdev->device == PCI_DEVICE_ID_ASMEDIA_1042_XHCI) {
298 		/*
299 		 * try to tame the ASMedia 1042 controller which reports 0.96
300 		 * but appears to behave more like 1.0
301 		 */
302 		xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
303 		xhci->quirks |= XHCI_BROKEN_STREAMS;
304 	}
305 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
306 		pdev->device == PCI_DEVICE_ID_ASMEDIA_1042A_XHCI) {
307 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
308 		xhci->quirks |= XHCI_NO_64BIT_SUPPORT;
309 	}
310 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
311 	    (pdev->device == PCI_DEVICE_ID_ASMEDIA_1142_XHCI ||
312 	     pdev->device == PCI_DEVICE_ID_ASMEDIA_2142_XHCI ||
313 	     pdev->device == PCI_DEVICE_ID_ASMEDIA_3242_XHCI))
314 		xhci->quirks |= XHCI_NO_64BIT_SUPPORT;
315 
316 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
317 		pdev->device == PCI_DEVICE_ID_ASMEDIA_1042A_XHCI)
318 		xhci->quirks |= XHCI_ASMEDIA_MODIFY_FLOWCONTROL;
319 
320 	if (pdev->vendor == PCI_VENDOR_ID_TI && pdev->device == 0x8241)
321 		xhci->quirks |= XHCI_LIMIT_ENDPOINT_INTERVAL_7;
322 
323 	if ((pdev->vendor == PCI_VENDOR_ID_BROADCOM ||
324 	     pdev->vendor == PCI_VENDOR_ID_CAVIUM) &&
325 	     pdev->device == 0x9026)
326 		xhci->quirks |= XHCI_RESET_PLL_ON_DISCONNECT;
327 
328 	if (pdev->vendor == PCI_VENDOR_ID_AMD &&
329 	    (pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_2 ||
330 	     pdev->device == PCI_DEVICE_ID_AMD_PROMONTORYA_4))
331 		xhci->quirks |= XHCI_NO_SOFT_RETRY;
332 
333 	/* xHC spec requires PCI devices to support D3hot and D3cold */
334 	if (xhci->hci_version >= 0x120)
335 		xhci->quirks |= XHCI_DEFAULT_PM_RUNTIME_ALLOW;
336 
337 	if (xhci->quirks & XHCI_RESET_ON_RESUME)
338 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
339 				"QUIRK: Resetting on resume");
340 }
341 
342 #ifdef CONFIG_ACPI
xhci_pme_acpi_rtd3_enable(struct pci_dev * dev)343 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev)
344 {
345 	static const guid_t intel_dsm_guid =
346 		GUID_INIT(0xac340cb7, 0xe901, 0x45bf,
347 			  0xb7, 0xe6, 0x2b, 0x34, 0xec, 0x93, 0x1e, 0x23);
348 	union acpi_object *obj;
349 
350 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dev->dev), &intel_dsm_guid, 3, 1,
351 				NULL);
352 	ACPI_FREE(obj);
353 }
354 
xhci_find_lpm_incapable_ports(struct usb_hcd * hcd,struct usb_device * hdev)355 static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device *hdev)
356 {
357 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
358 	struct xhci_hub *rhub = &xhci->usb3_rhub;
359 	int ret;
360 	int i;
361 
362 	/* This is not the usb3 roothub we are looking for */
363 	if (hcd != rhub->hcd)
364 		return;
365 
366 	if (hdev->maxchild > rhub->num_ports) {
367 		dev_err(&hdev->dev, "USB3 roothub port number mismatch\n");
368 		return;
369 	}
370 
371 	for (i = 0; i < hdev->maxchild; i++) {
372 		ret = usb_acpi_port_lpm_incapable(hdev, i);
373 
374 		dev_dbg(&hdev->dev, "port-%d disable U1/U2 _DSM: %d\n", i + 1, ret);
375 
376 		if (ret >= 0) {
377 			rhub->ports[i]->lpm_incapable = ret;
378 			continue;
379 		}
380 	}
381 }
382 
383 #else
xhci_pme_acpi_rtd3_enable(struct pci_dev * dev)384 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
xhci_find_lpm_incapable_ports(struct usb_hcd * hcd,struct usb_device * hdev)385 static void xhci_find_lpm_incapable_ports(struct usb_hcd *hcd, struct usb_device *hdev) { }
386 #endif /* CONFIG_ACPI */
387 
388 /* called during probe() after chip reset completes */
xhci_pci_setup(struct usb_hcd * hcd)389 static int xhci_pci_setup(struct usb_hcd *hcd)
390 {
391 	struct xhci_hcd		*xhci;
392 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
393 	int			retval;
394 
395 	xhci = hcd_to_xhci(hcd);
396 	if (!xhci->sbrn)
397 		pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
398 
399 	/* imod_interval is the interrupt moderation value in nanoseconds. */
400 	xhci->imod_interval = 40000;
401 
402 	retval = xhci_gen_setup(hcd, xhci_pci_quirks);
403 	if (retval)
404 		return retval;
405 
406 	if (!usb_hcd_is_primary_hcd(hcd))
407 		return 0;
408 
409 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
410 		xhci_pme_acpi_rtd3_enable(pdev);
411 
412 	xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
413 
414 	/* Find any debug ports */
415 	return xhci_pci_reinit(xhci, pdev);
416 }
417 
xhci_pci_update_hub_device(struct usb_hcd * hcd,struct usb_device * hdev,struct usb_tt * tt,gfp_t mem_flags)418 static int xhci_pci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
419 				      struct usb_tt *tt, gfp_t mem_flags)
420 {
421 	/* Check if acpi claims some USB3 roothub ports are lpm incapable */
422 	if (!hdev->parent)
423 		xhci_find_lpm_incapable_ports(hcd, hdev);
424 
425 	return xhci_update_hub_device(hcd, hdev, tt, mem_flags);
426 }
427 
428 /*
429  * We need to register our own PCI probe function (instead of the USB core's
430  * function) in order to create a second roothub under xHCI.
431  */
xhci_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)432 static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
433 {
434 	int retval;
435 	struct xhci_hcd *xhci;
436 	struct usb_hcd *hcd;
437 	struct xhci_driver_data *driver_data;
438 	struct reset_control *reset;
439 
440 	driver_data = (struct xhci_driver_data *)id->driver_data;
441 	if (driver_data && driver_data->quirks & XHCI_RENESAS_FW_QUIRK) {
442 		retval = renesas_xhci_check_request_fw(dev, id);
443 		if (retval)
444 			return retval;
445 	}
446 
447 	reset = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
448 	if (IS_ERR(reset))
449 		return PTR_ERR(reset);
450 	reset_control_reset(reset);
451 
452 	/* Prevent runtime suspending between USB-2 and USB-3 initialization */
453 	pm_runtime_get_noresume(&dev->dev);
454 
455 	/* Register the USB 2.0 roothub.
456 	 * FIXME: USB core must know to register the USB 2.0 roothub first.
457 	 * This is sort of silly, because we could just set the HCD driver flags
458 	 * to say USB 2.0, but I'm not sure what the implications would be in
459 	 * the other parts of the HCD code.
460 	 */
461 	retval = usb_hcd_pci_probe(dev, id, &xhci_pci_hc_driver);
462 
463 	if (retval)
464 		goto put_runtime_pm;
465 
466 	/* USB 2.0 roothub is stored in the PCI device now. */
467 	hcd = dev_get_drvdata(&dev->dev);
468 	xhci = hcd_to_xhci(hcd);
469 	xhci->reset = reset;
470 	xhci->shared_hcd = usb_create_shared_hcd(&xhci_pci_hc_driver, &dev->dev,
471 						 pci_name(dev), hcd);
472 	if (!xhci->shared_hcd) {
473 		retval = -ENOMEM;
474 		goto dealloc_usb2_hcd;
475 	}
476 
477 	retval = xhci_ext_cap_init(xhci);
478 	if (retval)
479 		goto put_usb3_hcd;
480 
481 	retval = usb_add_hcd(xhci->shared_hcd, dev->irq,
482 			IRQF_SHARED);
483 	if (retval)
484 		goto put_usb3_hcd;
485 	/* Roothub already marked as USB 3.0 speed */
486 
487 	if (!(xhci->quirks & XHCI_BROKEN_STREAMS) &&
488 			HCC_MAX_PSA(xhci->hcc_params) >= 4)
489 		xhci->shared_hcd->can_do_streams = 1;
490 
491 	/* USB-2 and USB-3 roothubs initialized, allow runtime pm suspend */
492 	pm_runtime_put_noidle(&dev->dev);
493 
494 	if (xhci->quirks & XHCI_DEFAULT_PM_RUNTIME_ALLOW)
495 		pm_runtime_allow(&dev->dev);
496 
497 	dma_set_max_seg_size(&dev->dev, UINT_MAX);
498 
499 	return 0;
500 
501 put_usb3_hcd:
502 	usb_put_hcd(xhci->shared_hcd);
503 dealloc_usb2_hcd:
504 	usb_hcd_pci_remove(dev);
505 put_runtime_pm:
506 	pm_runtime_put_noidle(&dev->dev);
507 	return retval;
508 }
509 
xhci_pci_remove(struct pci_dev * dev)510 static void xhci_pci_remove(struct pci_dev *dev)
511 {
512 	struct xhci_hcd *xhci;
513 
514 	xhci = hcd_to_xhci(pci_get_drvdata(dev));
515 	if (xhci->quirks & XHCI_RENESAS_FW_QUIRK)
516 		renesas_xhci_pci_exit(dev);
517 
518 	xhci->xhc_state |= XHCI_STATE_REMOVING;
519 
520 	if (xhci->quirks & XHCI_DEFAULT_PM_RUNTIME_ALLOW)
521 		pm_runtime_forbid(&dev->dev);
522 
523 	if (xhci->shared_hcd) {
524 		usb_remove_hcd(xhci->shared_hcd);
525 		usb_put_hcd(xhci->shared_hcd);
526 		xhci->shared_hcd = NULL;
527 	}
528 
529 	/* Workaround for spurious wakeups at shutdown with HSW */
530 	if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
531 		pci_set_power_state(dev, PCI_D3hot);
532 
533 	usb_hcd_pci_remove(dev);
534 }
535 
536 #ifdef CONFIG_PM
537 /*
538  * In some Intel xHCI controllers, in order to get D3 working,
539  * through a vendor specific SSIC CONFIG register at offset 0x883c,
540  * SSIC PORT need to be marked as "unused" before putting xHCI
541  * into D3. After D3 exit, the SSIC port need to be marked as "used".
542  * Without this change, xHCI might not enter D3 state.
543  */
xhci_ssic_port_unused_quirk(struct usb_hcd * hcd,bool suspend)544 static void xhci_ssic_port_unused_quirk(struct usb_hcd *hcd, bool suspend)
545 {
546 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
547 	u32 val;
548 	void __iomem *reg;
549 	int i;
550 
551 	for (i = 0; i < SSIC_PORT_NUM; i++) {
552 		reg = (void __iomem *) xhci->cap_regs +
553 				SSIC_PORT_CFG2 +
554 				i * SSIC_PORT_CFG2_OFFSET;
555 
556 		/* Notify SSIC that SSIC profile programming is not done. */
557 		val = readl(reg) & ~PROG_DONE;
558 		writel(val, reg);
559 
560 		/* Mark SSIC port as unused(suspend) or used(resume) */
561 		val = readl(reg);
562 		if (suspend)
563 			val |= SSIC_PORT_UNUSED;
564 		else
565 			val &= ~SSIC_PORT_UNUSED;
566 		writel(val, reg);
567 
568 		/* Notify SSIC that SSIC profile programming is done */
569 		val = readl(reg) | PROG_DONE;
570 		writel(val, reg);
571 		readl(reg);
572 	}
573 }
574 
575 /*
576  * Make sure PME works on some Intel xHCI controllers by writing 1 to clear
577  * the Internal PME flag bit in vendor specific PMCTRL register at offset 0x80a4
578  */
xhci_pme_quirk(struct usb_hcd * hcd)579 static void xhci_pme_quirk(struct usb_hcd *hcd)
580 {
581 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
582 	void __iomem *reg;
583 	u32 val;
584 
585 	reg = (void __iomem *) xhci->cap_regs + 0x80a4;
586 	val = readl(reg);
587 	writel(val | BIT(28), reg);
588 	readl(reg);
589 }
590 
xhci_sparse_control_quirk(struct usb_hcd * hcd)591 static void xhci_sparse_control_quirk(struct usb_hcd *hcd)
592 {
593 	u32 reg;
594 
595 	reg = readl(hcd->regs + SPARSE_CNTL_ENABLE);
596 	reg &= ~BIT(SPARSE_DISABLE_BIT);
597 	writel(reg, hcd->regs + SPARSE_CNTL_ENABLE);
598 }
599 
xhci_pci_suspend(struct usb_hcd * hcd,bool do_wakeup)600 static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
601 {
602 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
603 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
604 	int			ret;
605 
606 	/*
607 	 * Systems with the TI redriver that loses port status change events
608 	 * need to have the registers polled during D3, so avoid D3cold.
609 	 */
610 	if (xhci->quirks & XHCI_COMP_MODE_QUIRK)
611 		pci_d3cold_disable(pdev);
612 
613 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
614 		xhci_pme_quirk(hcd);
615 
616 	if (xhci->quirks & XHCI_SSIC_PORT_UNUSED)
617 		xhci_ssic_port_unused_quirk(hcd, true);
618 
619 	if (xhci->quirks & XHCI_DISABLE_SPARSE)
620 		xhci_sparse_control_quirk(hcd);
621 
622 	ret = xhci_suspend(xhci, do_wakeup);
623 	if (ret && (xhci->quirks & XHCI_SSIC_PORT_UNUSED))
624 		xhci_ssic_port_unused_quirk(hcd, false);
625 
626 	return ret;
627 }
628 
xhci_pci_resume(struct usb_hcd * hcd,bool hibernated)629 static int xhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
630 {
631 	struct xhci_hcd		*xhci = hcd_to_xhci(hcd);
632 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
633 	int			retval = 0;
634 
635 	reset_control_reset(xhci->reset);
636 
637 	/* The BIOS on systems with the Intel Panther Point chipset may or may
638 	 * not support xHCI natively.  That means that during system resume, it
639 	 * may switch the ports back to EHCI so that users can use their
640 	 * keyboard to select a kernel from GRUB after resume from hibernate.
641 	 *
642 	 * The BIOS is supposed to remember whether the OS had xHCI ports
643 	 * enabled before resume, and switch the ports back to xHCI when the
644 	 * BIOS/OS semaphore is written, but we all know we can't trust BIOS
645 	 * writers.
646 	 *
647 	 * Unconditionally switch the ports back to xHCI after a system resume.
648 	 * It should not matter whether the EHCI or xHCI controller is
649 	 * resumed first. It's enough to do the switchover in xHCI because
650 	 * USB core won't notice anything as the hub driver doesn't start
651 	 * running again until after all the devices (including both EHCI and
652 	 * xHCI host controllers) have been resumed.
653 	 */
654 
655 	if (pdev->vendor == PCI_VENDOR_ID_INTEL)
656 		usb_enable_intel_xhci_ports(pdev);
657 
658 	if (xhci->quirks & XHCI_SSIC_PORT_UNUSED)
659 		xhci_ssic_port_unused_quirk(hcd, false);
660 
661 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
662 		xhci_pme_quirk(hcd);
663 
664 	retval = xhci_resume(xhci, hibernated);
665 	return retval;
666 }
667 
xhci_pci_shutdown(struct usb_hcd * hcd)668 static void xhci_pci_shutdown(struct usb_hcd *hcd)
669 {
670 	struct xhci_hcd		*xhci = hcd_to_xhci(hcd);
671 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
672 
673 	xhci_shutdown(hcd);
674 
675 	/* Yet another workaround for spurious wakeups at shutdown with HSW */
676 	if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
677 		pci_set_power_state(pdev, PCI_D3hot);
678 }
679 #endif /* CONFIG_PM */
680 
681 /*-------------------------------------------------------------------------*/
682 
683 static const struct xhci_driver_data reneses_data = {
684 	.quirks  = XHCI_RENESAS_FW_QUIRK,
685 	.firmware = "renesas_usb_fw.mem",
686 };
687 
688 /* PCI driver selection metadata; PCI hotplugging uses this */
689 static const struct pci_device_id pci_ids[] = {
690 	{ PCI_DEVICE(0x1912, 0x0014),
691 		.driver_data =  (unsigned long)&reneses_data,
692 	},
693 	{ PCI_DEVICE(0x1912, 0x0015),
694 		.driver_data =  (unsigned long)&reneses_data,
695 	},
696 	/* handle any USB 3.0 xHCI controller */
697 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
698 	},
699 	{ /* end: all zeroes */ }
700 };
701 MODULE_DEVICE_TABLE(pci, pci_ids);
702 
703 /*
704  * Without CONFIG_USB_XHCI_PCI_RENESAS renesas_xhci_check_request_fw() won't
705  * load firmware, so don't encumber the xhci-pci driver with it.
706  */
707 #if IS_ENABLED(CONFIG_USB_XHCI_PCI_RENESAS)
708 MODULE_FIRMWARE("renesas_usb_fw.mem");
709 #endif
710 
711 /* pci driver glue; this is a "new style" PCI driver module */
712 static struct pci_driver xhci_pci_driver = {
713 	.name =		hcd_name,
714 	.id_table =	pci_ids,
715 
716 	.probe =	xhci_pci_probe,
717 	.remove =	xhci_pci_remove,
718 	/* suspend and resume implemented later */
719 
720 	.shutdown = 	usb_hcd_pci_shutdown,
721 #ifdef CONFIG_PM
722 	.driver = {
723 		.pm = &usb_hcd_pci_pm_ops
724 	},
725 #endif
726 };
727 
xhci_pci_init(void)728 static int __init xhci_pci_init(void)
729 {
730 	xhci_init_driver(&xhci_pci_hc_driver, &xhci_pci_overrides);
731 #ifdef CONFIG_PM
732 	xhci_pci_hc_driver.pci_suspend = xhci_pci_suspend;
733 	xhci_pci_hc_driver.pci_resume = xhci_pci_resume;
734 	xhci_pci_hc_driver.shutdown = xhci_pci_shutdown;
735 #endif
736 	return pci_register_driver(&xhci_pci_driver);
737 }
738 module_init(xhci_pci_init);
739 
xhci_pci_exit(void)740 static void __exit xhci_pci_exit(void)
741 {
742 	pci_unregister_driver(&xhci_pci_driver);
743 }
744 module_exit(xhci_pci_exit);
745 
746 MODULE_DESCRIPTION("xHCI PCI Host Controller Driver");
747 MODULE_LICENSE("GPL");
748