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