1 /*
2  * xHCI host controller driver PCI Bus Glue.
3  *
4  * Copyright (C) 2008 Intel Corp.
5  *
6  * Author: Sarah Sharp
7  * Some code borrowed from the Linux EHCI driver.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/acpi.h>
27 
28 #include "xhci.h"
29 #include "xhci-trace.h"
30 
31 #define SSIC_PORT_NUM		2
32 #define SSIC_PORT_CFG2		0x880c
33 #define SSIC_PORT_CFG2_OFFSET	0x30
34 #define PROG_DONE		(1 << 30)
35 #define SSIC_PORT_UNUSED	(1 << 31)
36 
37 /* Device for a quirk */
38 #define PCI_VENDOR_ID_FRESCO_LOGIC	0x1b73
39 #define PCI_DEVICE_ID_FRESCO_LOGIC_PDK	0x1000
40 #define PCI_DEVICE_ID_FRESCO_LOGIC_FL1400	0x1400
41 
42 #define PCI_VENDOR_ID_ETRON		0x1b6f
43 #define PCI_DEVICE_ID_EJ168		0x7023
44 
45 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_XHCI	0x8c31
46 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
47 #define PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI		0x22b5
48 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI		0xa12f
49 #define PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI	0x9d2f
50 #define PCI_DEVICE_ID_INTEL_BROXTON_M_XHCI		0x0aa8
51 #define PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI		0x1aa8
52 
53 static const char hcd_name[] = "xhci_hcd";
54 
55 static struct hc_driver __read_mostly xhci_pci_hc_driver;
56 
57 static int xhci_pci_setup(struct usb_hcd *hcd);
58 
59 static const struct xhci_driver_overrides xhci_pci_overrides __initconst = {
60 	.extra_priv_size = sizeof(struct xhci_hcd),
61 	.reset = xhci_pci_setup,
62 };
63 
64 /* called after powerup, by probe or system-pm "wakeup" */
xhci_pci_reinit(struct xhci_hcd * xhci,struct pci_dev * pdev)65 static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
66 {
67 	/*
68 	 * TODO: Implement finding debug ports later.
69 	 * TODO: see if there are any quirks that need to be added to handle
70 	 * new extended capabilities.
71 	 */
72 
73 	/* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
74 	if (!pci_set_mwi(pdev))
75 		xhci_dbg(xhci, "MWI active\n");
76 
77 	xhci_dbg(xhci, "Finished xhci_pci_reinit\n");
78 	return 0;
79 }
80 
xhci_pci_quirks(struct device * dev,struct xhci_hcd * xhci)81 static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
82 {
83 	struct pci_dev		*pdev = to_pci_dev(dev);
84 
85 	/* Look for vendor-specific quirks */
86 	if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
87 			(pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK ||
88 			 pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_FL1400)) {
89 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK &&
90 				pdev->revision == 0x0) {
91 			xhci->quirks |= XHCI_RESET_EP_QUIRK;
92 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
93 				"QUIRK: Fresco Logic xHC needs configure"
94 				" endpoint cmd after reset endpoint");
95 		}
96 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK &&
97 				pdev->revision == 0x4) {
98 			xhci->quirks |= XHCI_SLOW_SUSPEND;
99 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
100 				"QUIRK: Fresco Logic xHC revision %u"
101 				"must be suspended extra slowly",
102 				pdev->revision);
103 		}
104 		if (pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK)
105 			xhci->quirks |= XHCI_BROKEN_STREAMS;
106 		/* Fresco Logic confirms: all revisions of this chip do not
107 		 * support MSI, even though some of them claim to in their PCI
108 		 * capabilities.
109 		 */
110 		xhci->quirks |= XHCI_BROKEN_MSI;
111 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
112 				"QUIRK: Fresco Logic revision %u "
113 				"has broken MSI implementation",
114 				pdev->revision);
115 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
116 	}
117 
118 	if (pdev->vendor == PCI_VENDOR_ID_NEC)
119 		xhci->quirks |= XHCI_NEC_HOST;
120 
121 	if (pdev->vendor == PCI_VENDOR_ID_AMD && xhci->hci_version == 0x96)
122 		xhci->quirks |= XHCI_AMD_0x96_HOST;
123 
124 	/* AMD PLL quirk */
125 	if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_find_chipset_info())
126 		xhci->quirks |= XHCI_AMD_PLL_FIX;
127 
128 	if (pdev->vendor == PCI_VENDOR_ID_AMD)
129 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
130 
131 	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
132 		xhci->quirks |= XHCI_LPM_SUPPORT;
133 		xhci->quirks |= XHCI_INTEL_HOST;
134 		xhci->quirks |= XHCI_AVOID_BEI;
135 	}
136 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
137 			pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI) {
138 		xhci->quirks |= XHCI_EP_LIMIT_QUIRK;
139 		xhci->limit_active_eps = 64;
140 		xhci->quirks |= XHCI_SW_BW_CHECKING;
141 		/*
142 		 * PPT desktop boards DH77EB and DH77DF will power back on after
143 		 * a few seconds of being shutdown.  The fix for this is to
144 		 * switch the ports from xHCI to EHCI on shutdown.  We can't use
145 		 * DMI information to find those particular boards (since each
146 		 * vendor will change the board name), so we have to key off all
147 		 * PPT chipsets.
148 		 */
149 		xhci->quirks |= XHCI_SPURIOUS_REBOOT;
150 	}
151 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
152 		pdev->device == PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI) {
153 		xhci->quirks |= XHCI_SPURIOUS_REBOOT;
154 		xhci->quirks |= XHCI_SPURIOUS_WAKEUP;
155 	}
156 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
157 		(pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_LP_XHCI ||
158 		 pdev->device == PCI_DEVICE_ID_INTEL_SUNRISEPOINT_H_XHCI ||
159 		 pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI ||
160 		 pdev->device == PCI_DEVICE_ID_INTEL_BROXTON_M_XHCI ||
161 		 pdev->device == PCI_DEVICE_ID_INTEL_BROXTON_B_XHCI)) {
162 		xhci->quirks |= XHCI_PME_STUCK_QUIRK;
163 	}
164 	if (pdev->vendor == PCI_VENDOR_ID_ETRON &&
165 			pdev->device == PCI_DEVICE_ID_EJ168) {
166 		xhci->quirks |= XHCI_RESET_ON_RESUME;
167 		xhci->quirks |= XHCI_TRUST_TX_LENGTH;
168 		xhci->quirks |= XHCI_BROKEN_STREAMS;
169 	}
170 	if (pdev->vendor == PCI_VENDOR_ID_RENESAS &&
171 			pdev->device == 0x0015)
172 		xhci->quirks |= XHCI_RESET_ON_RESUME;
173 	if (pdev->vendor == PCI_VENDOR_ID_VIA)
174 		xhci->quirks |= XHCI_RESET_ON_RESUME;
175 
176 	/* See https://bugzilla.kernel.org/show_bug.cgi?id=79511 */
177 	if (pdev->vendor == PCI_VENDOR_ID_VIA &&
178 			pdev->device == 0x3432)
179 		xhci->quirks |= XHCI_BROKEN_STREAMS;
180 
181 	if (pdev->vendor == PCI_VENDOR_ID_ASMEDIA &&
182 			pdev->device == 0x1042)
183 		xhci->quirks |= XHCI_BROKEN_STREAMS;
184 
185 	if (xhci->quirks & XHCI_RESET_ON_RESUME)
186 		xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
187 				"QUIRK: Resetting on resume");
188 }
189 
190 #ifdef CONFIG_ACPI
xhci_pme_acpi_rtd3_enable(struct pci_dev * dev)191 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev)
192 {
193 	static const u8 intel_dsm_uuid[] = {
194 		0xb7, 0x0c, 0x34, 0xac,	0x01, 0xe9, 0xbf, 0x45,
195 		0xb7, 0xe6, 0x2b, 0x34, 0xec, 0x93, 0x1e, 0x23,
196 	};
197 	union acpi_object *obj;
198 
199 	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dev->dev), intel_dsm_uuid, 3, 1,
200 				NULL);
201 	ACPI_FREE(obj);
202 }
203 #else
xhci_pme_acpi_rtd3_enable(struct pci_dev * dev)204 static void xhci_pme_acpi_rtd3_enable(struct pci_dev *dev) { }
205 #endif /* CONFIG_ACPI */
206 
207 /* called during probe() after chip reset completes */
xhci_pci_setup(struct usb_hcd * hcd)208 static int xhci_pci_setup(struct usb_hcd *hcd)
209 {
210 	struct xhci_hcd		*xhci;
211 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
212 	int			retval;
213 
214 	xhci = hcd_to_xhci(hcd);
215 	if (!xhci->sbrn)
216 		pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
217 
218 	retval = xhci_gen_setup(hcd, xhci_pci_quirks);
219 	if (retval)
220 		return retval;
221 
222 	if (!usb_hcd_is_primary_hcd(hcd))
223 		return 0;
224 
225 	xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
226 
227 	/* Find any debug ports */
228 	retval = xhci_pci_reinit(xhci, pdev);
229 	if (!retval)
230 		return retval;
231 
232 	return retval;
233 }
234 
235 /*
236  * We need to register our own PCI probe function (instead of the USB core's
237  * function) in order to create a second roothub under xHCI.
238  */
xhci_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)239 static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
240 {
241 	int retval;
242 	struct xhci_hcd *xhci;
243 	struct hc_driver *driver;
244 	struct usb_hcd *hcd;
245 
246 	driver = (struct hc_driver *)id->driver_data;
247 
248 	/* Prevent runtime suspending between USB-2 and USB-3 initialization */
249 	pm_runtime_get_noresume(&dev->dev);
250 
251 	/* Register the USB 2.0 roothub.
252 	 * FIXME: USB core must know to register the USB 2.0 roothub first.
253 	 * This is sort of silly, because we could just set the HCD driver flags
254 	 * to say USB 2.0, but I'm not sure what the implications would be in
255 	 * the other parts of the HCD code.
256 	 */
257 	retval = usb_hcd_pci_probe(dev, id);
258 
259 	if (retval)
260 		goto put_runtime_pm;
261 
262 	/* USB 2.0 roothub is stored in the PCI device now. */
263 	hcd = dev_get_drvdata(&dev->dev);
264 	xhci = hcd_to_xhci(hcd);
265 	xhci->shared_hcd = usb_create_shared_hcd(driver, &dev->dev,
266 				pci_name(dev), hcd);
267 	if (!xhci->shared_hcd) {
268 		retval = -ENOMEM;
269 		goto dealloc_usb2_hcd;
270 	}
271 
272 	retval = usb_add_hcd(xhci->shared_hcd, dev->irq,
273 			IRQF_SHARED);
274 	if (retval)
275 		goto put_usb3_hcd;
276 	/* Roothub already marked as USB 3.0 speed */
277 
278 	if (!(xhci->quirks & XHCI_BROKEN_STREAMS) &&
279 			HCC_MAX_PSA(xhci->hcc_params) >= 4)
280 		xhci->shared_hcd->can_do_streams = 1;
281 
282 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
283 		xhci_pme_acpi_rtd3_enable(dev);
284 
285 	/* USB-2 and USB-3 roothubs initialized, allow runtime pm suspend */
286 	pm_runtime_put_noidle(&dev->dev);
287 
288 	return 0;
289 
290 put_usb3_hcd:
291 	usb_put_hcd(xhci->shared_hcd);
292 dealloc_usb2_hcd:
293 	usb_hcd_pci_remove(dev);
294 put_runtime_pm:
295 	pm_runtime_put_noidle(&dev->dev);
296 	return retval;
297 }
298 
xhci_pci_remove(struct pci_dev * dev)299 static void xhci_pci_remove(struct pci_dev *dev)
300 {
301 	struct xhci_hcd *xhci;
302 
303 	xhci = hcd_to_xhci(pci_get_drvdata(dev));
304 	xhci->xhc_state |= XHCI_STATE_REMOVING;
305 	if (xhci->shared_hcd) {
306 		usb_remove_hcd(xhci->shared_hcd);
307 		usb_put_hcd(xhci->shared_hcd);
308 	}
309 	usb_hcd_pci_remove(dev);
310 
311 	/* Workaround for spurious wakeups at shutdown with HSW */
312 	if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
313 		pci_set_power_state(dev, PCI_D3hot);
314 }
315 
316 #ifdef CONFIG_PM
317 /*
318  * In some Intel xHCI controllers, in order to get D3 working,
319  * through a vendor specific SSIC CONFIG register at offset 0x883c,
320  * SSIC PORT need to be marked as "unused" before putting xHCI
321  * into D3. After D3 exit, the SSIC port need to be marked as "used".
322  * Without this change, xHCI might not enter D3 state.
323  * Make sure PME works on some Intel xHCI controllers by writing 1 to clear
324  * the Internal PME flag bit in vendor specific PMCTRL register at offset 0x80a4
325  */
xhci_pme_quirk(struct usb_hcd * hcd,bool suspend)326 static void xhci_pme_quirk(struct usb_hcd *hcd, bool suspend)
327 {
328 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
329 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
330 	u32 val;
331 	void __iomem *reg;
332 	int i;
333 
334 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
335 		 pdev->device == PCI_DEVICE_ID_INTEL_CHERRYVIEW_XHCI) {
336 
337 		for (i = 0; i < SSIC_PORT_NUM; i++) {
338 			reg = (void __iomem *) xhci->cap_regs +
339 					SSIC_PORT_CFG2 +
340 					i * SSIC_PORT_CFG2_OFFSET;
341 
342 			/*
343 			 * Notify SSIC that SSIC profile programming
344 			 * is not done.
345 			 */
346 			val = readl(reg) & ~PROG_DONE;
347 			writel(val, reg);
348 
349 			/* Mark SSIC port as unused(suspend) or used(resume) */
350 			val = readl(reg);
351 			if (suspend)
352 				val |= SSIC_PORT_UNUSED;
353 			else
354 				val &= ~SSIC_PORT_UNUSED;
355 			writel(val, reg);
356 
357 			/* Notify SSIC that SSIC profile programming is done */
358 			val = readl(reg) | PROG_DONE;
359 			writel(val, reg);
360 			readl(reg);
361 		}
362 	}
363 
364 	reg = (void __iomem *) xhci->cap_regs + 0x80a4;
365 	val = readl(reg);
366 	writel(val | BIT(28), reg);
367 	readl(reg);
368 }
369 
xhci_pci_suspend(struct usb_hcd * hcd,bool do_wakeup)370 static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
371 {
372 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
373 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
374 
375 	/*
376 	 * Systems with the TI redriver that loses port status change events
377 	 * need to have the registers polled during D3, so avoid D3cold.
378 	 */
379 	if (xhci->quirks & XHCI_COMP_MODE_QUIRK)
380 		pdev->no_d3cold = true;
381 
382 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
383 		xhci_pme_quirk(hcd, true);
384 
385 	return xhci_suspend(xhci, do_wakeup);
386 }
387 
xhci_pci_resume(struct usb_hcd * hcd,bool hibernated)388 static int xhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
389 {
390 	struct xhci_hcd		*xhci = hcd_to_xhci(hcd);
391 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
392 	int			retval = 0;
393 
394 	/* The BIOS on systems with the Intel Panther Point chipset may or may
395 	 * not support xHCI natively.  That means that during system resume, it
396 	 * may switch the ports back to EHCI so that users can use their
397 	 * keyboard to select a kernel from GRUB after resume from hibernate.
398 	 *
399 	 * The BIOS is supposed to remember whether the OS had xHCI ports
400 	 * enabled before resume, and switch the ports back to xHCI when the
401 	 * BIOS/OS semaphore is written, but we all know we can't trust BIOS
402 	 * writers.
403 	 *
404 	 * Unconditionally switch the ports back to xHCI after a system resume.
405 	 * It should not matter whether the EHCI or xHCI controller is
406 	 * resumed first. It's enough to do the switchover in xHCI because
407 	 * USB core won't notice anything as the hub driver doesn't start
408 	 * running again until after all the devices (including both EHCI and
409 	 * xHCI host controllers) have been resumed.
410 	 */
411 
412 	if (pdev->vendor == PCI_VENDOR_ID_INTEL)
413 		usb_enable_intel_xhci_ports(pdev);
414 
415 	if (xhci->quirks & XHCI_PME_STUCK_QUIRK)
416 		xhci_pme_quirk(hcd, false);
417 
418 	retval = xhci_resume(xhci, hibernated);
419 	return retval;
420 }
421 #endif /* CONFIG_PM */
422 
423 /*-------------------------------------------------------------------------*/
424 
425 /* PCI driver selection metadata; PCI hotplugging uses this */
426 static const struct pci_device_id pci_ids[] = { {
427 	/* handle any USB 3.0 xHCI controller */
428 	PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
429 	.driver_data =	(unsigned long) &xhci_pci_hc_driver,
430 	},
431 	{ /* end: all zeroes */ }
432 };
433 MODULE_DEVICE_TABLE(pci, pci_ids);
434 
435 /* pci driver glue; this is a "new style" PCI driver module */
436 static struct pci_driver xhci_pci_driver = {
437 	.name =		(char *) hcd_name,
438 	.id_table =	pci_ids,
439 
440 	.probe =	xhci_pci_probe,
441 	.remove =	xhci_pci_remove,
442 	/* suspend and resume implemented later */
443 
444 	.shutdown = 	usb_hcd_pci_shutdown,
445 #ifdef CONFIG_PM
446 	.driver = {
447 		.pm = &usb_hcd_pci_pm_ops
448 	},
449 #endif
450 };
451 
xhci_pci_init(void)452 static int __init xhci_pci_init(void)
453 {
454 	xhci_init_driver(&xhci_pci_hc_driver, &xhci_pci_overrides);
455 #ifdef CONFIG_PM
456 	xhci_pci_hc_driver.pci_suspend = xhci_pci_suspend;
457 	xhci_pci_hc_driver.pci_resume = xhci_pci_resume;
458 #endif
459 	return pci_register_driver(&xhci_pci_driver);
460 }
461 module_init(xhci_pci_init);
462 
xhci_pci_exit(void)463 static void __exit xhci_pci_exit(void)
464 {
465 	pci_unregister_driver(&xhci_pci_driver);
466 }
467 module_exit(xhci_pci_exit);
468 
469 MODULE_DESCRIPTION("xHCI PCI Host Controller Driver");
470 MODULE_LICENSE("GPL");
471