blob: d1e68d887f6ee53875452cb560f64e560f3ef9cb [file] [log] [blame]
David Brownell75862692005-09-23 17:14:37 -07001/*
2 * This file contains code to reset and initialize USB host controllers.
3 * Some of it includes work-arounds for PCI hardware and BIOS quirks.
4 * It may need to run early during booting -- before USB would normally
5 * initialize -- to ensure that Linux doesn't use any legacy modes.
6 *
7 * Copyright (c) 1999 Martin Mares <mj@ucw.cz>
8 * (and others)
9 */
10
David Brownell75862692005-09-23 17:14:37 -070011#include <linux/types.h>
Sarah Sharp51c9e6c2012-04-16 10:56:47 -070012#include <linux/kconfig.h>
David Brownell75862692005-09-23 17:14:37 -070013#include <linux/kernel.h>
14#include <linux/pci.h>
15#include <linux/init.h>
16#include <linux/delay.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040017#include <linux/export.h>
David Brownell75862692005-09-23 17:14:37 -070018#include <linux/acpi.h>
Andy Ross3610ea52011-05-11 15:52:38 -070019#include <linux/dmi.h>
Adrian Bunk75e2df62006-03-25 18:01:53 +010020#include "pci-quirks.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070021#include "xhci-ext-caps.h"
David Brownell75862692005-09-23 17:14:37 -070022
23
David Brownell75862692005-09-23 17:14:37 -070024#define UHCI_USBLEGSUP 0xc0 /* legacy support */
25#define UHCI_USBCMD 0 /* command register */
David Brownell75862692005-09-23 17:14:37 -070026#define UHCI_USBINTR 4 /* interrupt register */
Alan Sternbb200f62005-10-03 16:36:29 -040027#define UHCI_USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
28#define UHCI_USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
29#define UHCI_USBCMD_RUN 0x0001 /* RUN/STOP bit */
30#define UHCI_USBCMD_HCRESET 0x0002 /* Host Controller reset */
31#define UHCI_USBCMD_EGSM 0x0008 /* Global Suspend Mode */
32#define UHCI_USBCMD_CONFIGURE 0x0040 /* Config Flag */
33#define UHCI_USBINTR_RESUME 0x0002 /* Resume interrupt enable */
David Brownell75862692005-09-23 17:14:37 -070034
35#define OHCI_CONTROL 0x04
36#define OHCI_CMDSTATUS 0x08
37#define OHCI_INTRSTATUS 0x0c
38#define OHCI_INTRENABLE 0x10
39#define OHCI_INTRDISABLE 0x14
Alan Stern6ea12a02011-07-15 17:22:15 -040040#define OHCI_FMINTERVAL 0x34
Alan Sternc6187592011-11-17 16:41:45 -050041#define OHCI_HCFS (3 << 6) /* hc functional state */
Alan Stern6ea12a02011-07-15 17:22:15 -040042#define OHCI_HCR (1 << 0) /* host controller reset */
David Brownell75862692005-09-23 17:14:37 -070043#define OHCI_OCR (1 << 3) /* ownership change request */
David Brownellf2cb36c2005-09-22 22:43:30 -070044#define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */
David Brownell75862692005-09-23 17:14:37 -070045#define OHCI_CTRL_IR (1 << 8) /* interrupt routing */
46#define OHCI_INTR_OC (1 << 30) /* ownership change */
47
48#define EHCI_HCC_PARAMS 0x08 /* extended capabilities */
49#define EHCI_USBCMD 0 /* command register */
50#define EHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
51#define EHCI_USBSTS 4 /* status register */
52#define EHCI_USBSTS_HALTED (1 << 12) /* HCHalted bit */
53#define EHCI_USBINTR 8 /* interrupt register */
Alan Stern4fe53542007-04-05 16:06:53 -040054#define EHCI_CONFIGFLAG 0x40 /* configured flag register */
David Brownell75862692005-09-23 17:14:37 -070055#define EHCI_USBLEGSUP 0 /* legacy support register */
56#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
57#define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
58#define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
59#define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
60
Andiry Xuad935622011-03-01 14:57:05 +080061/* AMD quirk use */
62#define AB_REG_BAR_LOW 0xe0
63#define AB_REG_BAR_HIGH 0xe1
64#define AB_REG_BAR_SB700 0xf0
65#define AB_INDX(addr) ((addr) + 0x00)
66#define AB_DATA(addr) ((addr) + 0x04)
67#define AX_INDXC 0x30
68#define AX_DATAC 0x34
69
70#define NB_PCIE_INDX_ADDR 0xe0
71#define NB_PCIE_INDX_DATA 0xe4
72#define PCIE_P_CNTL 0x10040
73#define BIF_NB 0x10002
74#define NB_PIF0_PWRDOWN_0 0x01100012
75#define NB_PIF0_PWRDOWN_1 0x01100013
76
Sarah Sharp69e848c2011-02-22 09:57:15 -080077#define USB_INTEL_XUSB2PR 0xD0
Keng-Yu Lina96874a2012-08-10 01:39:23 +080078#define USB_INTEL_USB2PRM 0xD4
Sarah Sharp69e848c2011-02-22 09:57:15 -080079#define USB_INTEL_USB3_PSSEN 0xD8
Keng-Yu Lina96874a2012-08-10 01:39:23 +080080#define USB_INTEL_USB3PRM 0xDC
Sarah Sharp69e848c2011-02-22 09:57:15 -080081
Huang Rui22b4f0c2013-09-16 23:47:27 +080082/*
83 * amd_chipset_gen values represent AMD different chipset generations
84 */
85enum amd_chipset_gen {
86 NOT_AMD_CHIPSET = 0,
87 AMD_CHIPSET_SB600,
88 AMD_CHIPSET_SB700,
89 AMD_CHIPSET_SB800,
90 AMD_CHIPSET_HUDSON2,
91 AMD_CHIPSET_BOLTON,
92 AMD_CHIPSET_YANGTZE,
93 AMD_CHIPSET_UNKNOWN,
94};
95
96struct amd_chipset_type {
97 enum amd_chipset_gen gen;
98 u8 rev;
99};
100
Andiry Xuad935622011-03-01 14:57:05 +0800101static struct amd_chipset_info {
102 struct pci_dev *nb_dev;
103 struct pci_dev *smbus_dev;
104 int nb_type;
Huang Rui22b4f0c2013-09-16 23:47:27 +0800105 struct amd_chipset_type sb_type;
Andiry Xuad935622011-03-01 14:57:05 +0800106 int isoc_reqs;
107 int probe_count;
108 int probe_result;
109} amd_chipset;
110
111static DEFINE_SPINLOCK(amd_lock);
112
Huang Rui22b4f0c2013-09-16 23:47:27 +0800113/*
114 * amd_chipset_sb_type_init - initialize amd chipset southbridge type
115 *
116 * AMD FCH/SB generation and revision is identified by SMBus controller
117 * vendor, device and revision IDs.
118 *
119 * Returns: 1 if it is an AMD chipset, 0 otherwise.
120 */
121int amd_chipset_sb_type_init(struct amd_chipset_info *pinfo)
122{
123 u8 rev = 0;
124 pinfo->sb_type.gen = AMD_CHIPSET_UNKNOWN;
125
126 pinfo->smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI,
127 PCI_DEVICE_ID_ATI_SBX00_SMBUS, NULL);
128 if (pinfo->smbus_dev) {
129 rev = pinfo->smbus_dev->revision;
130 if (rev >= 0x10 && rev <= 0x1f)
131 pinfo->sb_type.gen = AMD_CHIPSET_SB600;
132 else if (rev >= 0x30 && rev <= 0x3f)
133 pinfo->sb_type.gen = AMD_CHIPSET_SB700;
134 else if (rev >= 0x40 && rev <= 0x4f)
135 pinfo->sb_type.gen = AMD_CHIPSET_SB800;
136 } else {
137 pinfo->smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
138 PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
139
140 if (!pinfo->smbus_dev) {
141 pinfo->sb_type.gen = NOT_AMD_CHIPSET;
142 return 0;
143 }
144
145 rev = pinfo->smbus_dev->revision;
146 if (rev >= 0x11 && rev <= 0x14)
147 pinfo->sb_type.gen = AMD_CHIPSET_HUDSON2;
148 else if (rev >= 0x15 && rev <= 0x18)
149 pinfo->sb_type.gen = AMD_CHIPSET_BOLTON;
150 else if (rev >= 0x39 && rev <= 0x3a)
151 pinfo->sb_type.gen = AMD_CHIPSET_YANGTZE;
152 }
153
154 pinfo->sb_type.rev = rev;
155 return 1;
156}
157
Manjunath Goudar2621d012013-05-28 18:34:50 +0530158void sb800_prefetch(struct device *dev, int on)
159{
160 u16 misc;
161 struct pci_dev *pdev = to_pci_dev(dev);
162
163 pci_read_config_word(pdev, 0x50, &misc);
164 if (on == 0)
165 pci_write_config_word(pdev, 0x50, misc & 0xfcff);
166 else
167 pci_write_config_word(pdev, 0x50, misc | 0x0300);
168}
169EXPORT_SYMBOL_GPL(sb800_prefetch);
170
Andiry Xuad935622011-03-01 14:57:05 +0800171int usb_amd_find_chipset_info(void)
172{
Andiry Xuad935622011-03-01 14:57:05 +0800173 unsigned long flags;
Joerg Roedel9ab79272011-04-13 08:38:16 +0200174 struct amd_chipset_info info;
175 int ret;
Andiry Xuad935622011-03-01 14:57:05 +0800176
177 spin_lock_irqsave(&amd_lock, flags);
178
Andiry Xuad935622011-03-01 14:57:05 +0800179 /* probe only once */
Joerg Roedel9ab79272011-04-13 08:38:16 +0200180 if (amd_chipset.probe_count > 0) {
181 amd_chipset.probe_count++;
Andiry Xuad935622011-03-01 14:57:05 +0800182 spin_unlock_irqrestore(&amd_lock, flags);
183 return amd_chipset.probe_result;
184 }
Joerg Roedel9ab79272011-04-13 08:38:16 +0200185 memset(&info, 0, sizeof(info));
186 spin_unlock_irqrestore(&amd_lock, flags);
Andiry Xuad935622011-03-01 14:57:05 +0800187
Huang Rui22b4f0c2013-09-16 23:47:27 +0800188 if (!amd_chipset_sb_type_init(&info)) {
189 ret = 0;
190 goto commit;
Andiry Xuad935622011-03-01 14:57:05 +0800191 }
192
Huang Rui22b4f0c2013-09-16 23:47:27 +0800193 /* Below chipset generations needn't enable AMD PLL quirk */
194 if (info.sb_type.gen == AMD_CHIPSET_UNKNOWN ||
195 info.sb_type.gen == AMD_CHIPSET_SB600 ||
196 info.sb_type.gen == AMD_CHIPSET_YANGTZE ||
197 (info.sb_type.gen == AMD_CHIPSET_SB700 &&
198 info.sb_type.rev > 0x3b)) {
Joerg Roedel9ab79272011-04-13 08:38:16 +0200199 if (info.smbus_dev) {
200 pci_dev_put(info.smbus_dev);
201 info.smbus_dev = NULL;
Andiry Xuad935622011-03-01 14:57:05 +0800202 }
Joerg Roedel9ab79272011-04-13 08:38:16 +0200203 ret = 0;
204 goto commit;
Andiry Xuad935622011-03-01 14:57:05 +0800205 }
206
Joerg Roedel9ab79272011-04-13 08:38:16 +0200207 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
208 if (info.nb_dev) {
209 info.nb_type = 1;
Andiry Xuad935622011-03-01 14:57:05 +0800210 } else {
Joerg Roedel9ab79272011-04-13 08:38:16 +0200211 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
212 if (info.nb_dev) {
213 info.nb_type = 2;
214 } else {
215 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
216 0x9600, NULL);
217 if (info.nb_dev)
218 info.nb_type = 3;
Andiry Xuad935622011-03-01 14:57:05 +0800219 }
220 }
221
Joerg Roedel9ab79272011-04-13 08:38:16 +0200222 ret = info.probe_result = 1;
Andiry Xuad935622011-03-01 14:57:05 +0800223 printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
224
Joerg Roedel9ab79272011-04-13 08:38:16 +0200225commit:
226
227 spin_lock_irqsave(&amd_lock, flags);
228 if (amd_chipset.probe_count > 0) {
229 /* race - someone else was faster - drop devices */
230
231 /* Mark that we where here */
232 amd_chipset.probe_count++;
233 ret = amd_chipset.probe_result;
234
235 spin_unlock_irqrestore(&amd_lock, flags);
236
237 if (info.nb_dev)
238 pci_dev_put(info.nb_dev);
239 if (info.smbus_dev)
240 pci_dev_put(info.smbus_dev);
241
242 } else {
243 /* no race - commit the result */
244 info.probe_count++;
245 amd_chipset = info;
246 spin_unlock_irqrestore(&amd_lock, flags);
247 }
248
249 return ret;
Andiry Xuad935622011-03-01 14:57:05 +0800250}
251EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
252
253/*
254 * The hardware normally enables the A-link power management feature, which
255 * lets the system lower the power consumption in idle states.
256 *
257 * This USB quirk prevents the link going into that lower power state
258 * during isochronous transfers.
259 *
260 * Without this quirk, isochronous stream on OHCI/EHCI/xHCI controllers of
261 * some AMD platforms may stutter or have breaks occasionally.
262 */
263static void usb_amd_quirk_pll(int disable)
264{
265 u32 addr, addr_low, addr_high, val;
266 u32 bit = disable ? 0 : 1;
267 unsigned long flags;
268
269 spin_lock_irqsave(&amd_lock, flags);
270
271 if (disable) {
272 amd_chipset.isoc_reqs++;
273 if (amd_chipset.isoc_reqs > 1) {
274 spin_unlock_irqrestore(&amd_lock, flags);
275 return;
276 }
277 } else {
278 amd_chipset.isoc_reqs--;
279 if (amd_chipset.isoc_reqs > 0) {
280 spin_unlock_irqrestore(&amd_lock, flags);
281 return;
282 }
283 }
284
Huang Rui22b4f0c2013-09-16 23:47:27 +0800285 if (amd_chipset.sb_type.gen == AMD_CHIPSET_SB800 ||
286 amd_chipset.sb_type.gen == AMD_CHIPSET_HUDSON2 ||
287 amd_chipset.sb_type.gen == AMD_CHIPSET_BOLTON) {
Andiry Xuad935622011-03-01 14:57:05 +0800288 outb_p(AB_REG_BAR_LOW, 0xcd6);
289 addr_low = inb_p(0xcd7);
290 outb_p(AB_REG_BAR_HIGH, 0xcd6);
291 addr_high = inb_p(0xcd7);
292 addr = addr_high << 8 | addr_low;
293
294 outl_p(0x30, AB_INDX(addr));
295 outl_p(0x40, AB_DATA(addr));
296 outl_p(0x34, AB_INDX(addr));
297 val = inl_p(AB_DATA(addr));
Huang Rui22b4f0c2013-09-16 23:47:27 +0800298 } else if (amd_chipset.sb_type.gen == AMD_CHIPSET_SB700 &&
299 amd_chipset.sb_type.rev <= 0x3b) {
Andiry Xuad935622011-03-01 14:57:05 +0800300 pci_read_config_dword(amd_chipset.smbus_dev,
301 AB_REG_BAR_SB700, &addr);
302 outl(AX_INDXC, AB_INDX(addr));
303 outl(0x40, AB_DATA(addr));
304 outl(AX_DATAC, AB_INDX(addr));
305 val = inl(AB_DATA(addr));
306 } else {
307 spin_unlock_irqrestore(&amd_lock, flags);
308 return;
309 }
310
311 if (disable) {
312 val &= ~0x08;
313 val |= (1 << 4) | (1 << 9);
314 } else {
315 val |= 0x08;
316 val &= ~((1 << 4) | (1 << 9));
317 }
318 outl_p(val, AB_DATA(addr));
319
320 if (!amd_chipset.nb_dev) {
321 spin_unlock_irqrestore(&amd_lock, flags);
322 return;
323 }
324
325 if (amd_chipset.nb_type == 1 || amd_chipset.nb_type == 3) {
326 addr = PCIE_P_CNTL;
327 pci_write_config_dword(amd_chipset.nb_dev,
328 NB_PCIE_INDX_ADDR, addr);
329 pci_read_config_dword(amd_chipset.nb_dev,
330 NB_PCIE_INDX_DATA, &val);
331
332 val &= ~(1 | (1 << 3) | (1 << 4) | (1 << 9) | (1 << 12));
333 val |= bit | (bit << 3) | (bit << 12);
334 val |= ((!bit) << 4) | ((!bit) << 9);
335 pci_write_config_dword(amd_chipset.nb_dev,
336 NB_PCIE_INDX_DATA, val);
337
338 addr = BIF_NB;
339 pci_write_config_dword(amd_chipset.nb_dev,
340 NB_PCIE_INDX_ADDR, addr);
341 pci_read_config_dword(amd_chipset.nb_dev,
342 NB_PCIE_INDX_DATA, &val);
343 val &= ~(1 << 8);
344 val |= bit << 8;
345
346 pci_write_config_dword(amd_chipset.nb_dev,
347 NB_PCIE_INDX_DATA, val);
348 } else if (amd_chipset.nb_type == 2) {
349 addr = NB_PIF0_PWRDOWN_0;
350 pci_write_config_dword(amd_chipset.nb_dev,
351 NB_PCIE_INDX_ADDR, addr);
352 pci_read_config_dword(amd_chipset.nb_dev,
353 NB_PCIE_INDX_DATA, &val);
354 if (disable)
355 val &= ~(0x3f << 7);
356 else
357 val |= 0x3f << 7;
358
359 pci_write_config_dword(amd_chipset.nb_dev,
360 NB_PCIE_INDX_DATA, val);
361
362 addr = NB_PIF0_PWRDOWN_1;
363 pci_write_config_dword(amd_chipset.nb_dev,
364 NB_PCIE_INDX_ADDR, addr);
365 pci_read_config_dword(amd_chipset.nb_dev,
366 NB_PCIE_INDX_DATA, &val);
367 if (disable)
368 val &= ~(0x3f << 7);
369 else
370 val |= 0x3f << 7;
371
372 pci_write_config_dword(amd_chipset.nb_dev,
373 NB_PCIE_INDX_DATA, val);
374 }
375
376 spin_unlock_irqrestore(&amd_lock, flags);
377 return;
378}
379
380void usb_amd_quirk_pll_disable(void)
381{
382 usb_amd_quirk_pll(1);
383}
384EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_disable);
385
386void usb_amd_quirk_pll_enable(void)
387{
388 usb_amd_quirk_pll(0);
389}
390EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
391
392void usb_amd_dev_put(void)
393{
Joerg Roedel9ab79272011-04-13 08:38:16 +0200394 struct pci_dev *nb, *smbus;
Andiry Xuad935622011-03-01 14:57:05 +0800395 unsigned long flags;
396
397 spin_lock_irqsave(&amd_lock, flags);
398
399 amd_chipset.probe_count--;
400 if (amd_chipset.probe_count > 0) {
401 spin_unlock_irqrestore(&amd_lock, flags);
402 return;
403 }
404
Joerg Roedel9ab79272011-04-13 08:38:16 +0200405 /* save them to pci_dev_put outside of spinlock */
406 nb = amd_chipset.nb_dev;
407 smbus = amd_chipset.smbus_dev;
408
409 amd_chipset.nb_dev = NULL;
410 amd_chipset.smbus_dev = NULL;
Andiry Xuad935622011-03-01 14:57:05 +0800411 amd_chipset.nb_type = 0;
Huang Rui22b4f0c2013-09-16 23:47:27 +0800412 memset(&amd_chipset.sb_type, 0, sizeof(amd_chipset.sb_type));
Andiry Xuad935622011-03-01 14:57:05 +0800413 amd_chipset.isoc_reqs = 0;
414 amd_chipset.probe_result = 0;
415
416 spin_unlock_irqrestore(&amd_lock, flags);
Joerg Roedel9ab79272011-04-13 08:38:16 +0200417
418 if (nb)
419 pci_dev_put(nb);
420 if (smbus)
421 pci_dev_put(smbus);
Andiry Xuad935622011-03-01 14:57:05 +0800422}
423EXPORT_SYMBOL_GPL(usb_amd_dev_put);
David Brownell75862692005-09-23 17:14:37 -0700424
Alan Sternbb200f62005-10-03 16:36:29 -0400425/*
426 * Make sure the controller is completely inactive, unable to
427 * generate interrupts or do DMA.
428 */
429void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
430{
431 /* Turn off PIRQ enable and SMI enable. (This also turns off the
432 * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
433 */
434 pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
435
436 /* Reset the HC - this will force us to get a
437 * new notification of any already connected
438 * ports due to the virtual disconnect that it
439 * implies.
440 */
441 outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
442 mb();
443 udelay(5);
444 if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
445 dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
446
447 /* Just to be safe, disable interrupt requests and
448 * make sure the controller is stopped.
449 */
450 outw(0, base + UHCI_USBINTR);
451 outw(0, base + UHCI_USBCMD);
452}
453EXPORT_SYMBOL_GPL(uhci_reset_hc);
454
455/*
456 * Initialize a controller that was newly discovered or has just been
457 * resumed. In either case we can't be sure of its previous state.
458 *
459 * Returns: 1 if the controller was reset, 0 otherwise.
460 */
461int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
462{
463 u16 legsup;
464 unsigned int cmd, intr;
465
466 /*
467 * When restarting a suspended controller, we expect all the
468 * settings to be the same as we left them:
469 *
470 * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
471 * Controller is stopped and configured with EGSM set;
472 * No interrupts enabled except possibly Resume Detect.
473 *
474 * If any of these conditions are violated we do a complete reset.
475 */
476 pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
477 if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
478 dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800479 __func__, legsup);
Alan Sternbb200f62005-10-03 16:36:29 -0400480 goto reset_needed;
481 }
482
483 cmd = inw(base + UHCI_USBCMD);
484 if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
485 !(cmd & UHCI_USBCMD_EGSM)) {
486 dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800487 __func__, cmd);
Alan Sternbb200f62005-10-03 16:36:29 -0400488 goto reset_needed;
489 }
490
491 intr = inw(base + UHCI_USBINTR);
492 if (intr & (~UHCI_USBINTR_RESUME)) {
493 dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800494 __func__, intr);
Alan Sternbb200f62005-10-03 16:36:29 -0400495 goto reset_needed;
496 }
497 return 0;
498
499reset_needed:
500 dev_dbg(&pdev->dev, "Performing full reset\n");
501 uhci_reset_hc(pdev, base);
502 return 1;
503}
504EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
505
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800506static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
507{
508 u16 cmd;
509 return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
510}
511
512#define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
513#define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
514
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500515static void quirk_usb_handoff_uhci(struct pci_dev *pdev)
David Brownell75862692005-09-23 17:14:37 -0700516{
517 unsigned long base = 0;
David Brownell75862692005-09-23 17:14:37 -0700518 int i;
519
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800520 if (!pio_enabled(pdev))
521 return;
522
David Brownell75862692005-09-23 17:14:37 -0700523 for (i = 0; i < PCI_ROM_RESOURCE; i++)
524 if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
525 base = pci_resource_start(pdev, i);
526 break;
527 }
528
Alan Sternbb200f62005-10-03 16:36:29 -0400529 if (base)
530 uhci_check_and_reset_hc(pdev, base);
David Brownell75862692005-09-23 17:14:37 -0700531}
532
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500533static int mmio_resource_enabled(struct pci_dev *pdev, int idx)
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800534{
535 return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
536}
537
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500538static void quirk_usb_handoff_ohci(struct pci_dev *pdev)
David Brownell75862692005-09-23 17:14:37 -0700539{
540 void __iomem *base;
Alan Stern3df71692010-09-10 16:37:05 -0400541 u32 control;
Alan Sternc6187592011-11-17 16:41:45 -0500542 u32 fminterval;
543 int cnt;
David Brownell75862692005-09-23 17:14:37 -0700544
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800545 if (!mmio_resource_enabled(pdev, 0))
546 return;
547
Arjan van de Ven8e8ce4b2008-10-20 21:46:01 -0700548 base = pci_ioremap_bar(pdev, 0);
549 if (base == NULL)
550 return;
David Brownell75862692005-09-23 17:14:37 -0700551
Alan Stern3df71692010-09-10 16:37:05 -0400552 control = readl(base + OHCI_CONTROL);
553
David Brownellf2cb36c2005-09-22 22:43:30 -0700554/* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
Alan Stern3df71692010-09-10 16:37:05 -0400555#ifdef __hppa__
556#define OHCI_CTRL_MASK (OHCI_CTRL_RWC | OHCI_CTRL_IR)
557#else
558#define OHCI_CTRL_MASK OHCI_CTRL_RWC
559
David Brownellf2cb36c2005-09-22 22:43:30 -0700560 if (control & OHCI_CTRL_IR) {
Kyle McMartinc1b45f22006-06-25 18:45:29 -0400561 int wait_time = 500; /* arbitrary; 5 seconds */
David Brownell75862692005-09-23 17:14:37 -0700562 writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
563 writel(OHCI_OCR, base + OHCI_CMDSTATUS);
564 while (wait_time > 0 &&
565 readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
566 wait_time -= 10;
567 msleep(10);
568 }
David Brownellf2cb36c2005-09-22 22:43:30 -0700569 if (wait_time <= 0)
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700570 dev_warn(&pdev->dev, "OHCI: BIOS handoff failed"
571 " (BIOS bug?) %08x\n",
David Brownella38408c2006-02-09 16:35:31 -0500572 readl(base + OHCI_CONTROL));
David Brownell75862692005-09-23 17:14:37 -0700573 }
David Brownellf2cb36c2005-09-22 22:43:30 -0700574#endif
David Brownell75862692005-09-23 17:14:37 -0700575
Alan Sternc6187592011-11-17 16:41:45 -0500576 /* disable interrupts */
577 writel((u32) ~0, base + OHCI_INTRDISABLE);
Alan Stern6ea12a02011-07-15 17:22:15 -0400578
Alan Sternc6187592011-11-17 16:41:45 -0500579 /* Reset the USB bus, if the controller isn't already in RESET */
580 if (control & OHCI_HCFS) {
581 /* Go into RESET, preserving RWC (and possibly IR) */
582 writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
583 readl(base + OHCI_CONTROL);
Alan Stern6ea12a02011-07-15 17:22:15 -0400584
Alan Sternc6187592011-11-17 16:41:45 -0500585 /* drive bus reset for at least 50 ms (7.1.7.5) */
Alan Stern6ea12a02011-07-15 17:22:15 -0400586 msleep(50);
Alan Stern6ea12a02011-07-15 17:22:15 -0400587 }
Alan Stern3df71692010-09-10 16:37:05 -0400588
Alan Sternc6187592011-11-17 16:41:45 -0500589 /* software reset of the controller, preserving HcFmInterval */
590 fminterval = readl(base + OHCI_FMINTERVAL);
591 writel(OHCI_HCR, base + OHCI_CMDSTATUS);
David Brownell75862692005-09-23 17:14:37 -0700592
Alan Sternc6187592011-11-17 16:41:45 -0500593 /* reset requires max 10 us delay */
594 for (cnt = 30; cnt > 0; --cnt) { /* ... allow extra time */
595 if ((readl(base + OHCI_CMDSTATUS) & OHCI_HCR) == 0)
596 break;
597 udelay(1);
598 }
599 writel(fminterval, base + OHCI_FMINTERVAL);
600
601 /* Now the controller is safely in SUSPEND and nothing can wake it up */
David Brownell75862692005-09-23 17:14:37 -0700602 iounmap(base);
603}
604
Bill Pemberton2f826862012-11-19 13:25:20 -0500605static const struct dmi_system_id ehci_dmi_nohandoff_table[] = {
Anisse Astier03c75362011-07-05 16:38:45 +0200606 {
607 /* Pegatron Lucid (ExoPC) */
608 .matches = {
609 DMI_MATCH(DMI_BOARD_NAME, "EXOPG06411"),
610 DMI_MATCH(DMI_BIOS_VERSION, "Lucid-CE-133"),
611 },
612 },
Anisse Astier0c42a4e2011-07-05 16:38:46 +0200613 {
614 /* Pegatron Lucid (Ordissimo AIRIS) */
615 .matches = {
616 DMI_MATCH(DMI_BOARD_NAME, "M11JB"),
Anisse Astierc323dc02012-10-09 12:22:36 +0200617 DMI_MATCH(DMI_BIOS_VERSION, "Lucid-"),
Anisse Astier0c42a4e2011-07-05 16:38:46 +0200618 },
619 },
Anisse Astier8daf8b62012-10-09 12:22:37 +0200620 {
621 /* Pegatron Lucid (Ordissimo) */
622 .matches = {
623 DMI_MATCH(DMI_BOARD_NAME, "Ordissimo"),
624 DMI_MATCH(DMI_BIOS_VERSION, "Lucid-"),
625 },
626 },
Anisse Astier03c75362011-07-05 16:38:45 +0200627 { }
628};
629
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500630static void ehci_bios_handoff(struct pci_dev *pdev,
Andy Ross5c853012011-05-11 15:15:51 -0700631 void __iomem *op_reg_base,
632 u32 cap, u8 offset)
633{
Andy Ross3610ea52011-05-11 15:52:38 -0700634 int try_handoff = 1, tried_handoff = 0;
Andy Ross5c853012011-05-11 15:15:51 -0700635
Anisse Astier03c75362011-07-05 16:38:45 +0200636 /* The Pegatron Lucid tablet sporadically waits for 98 seconds trying
637 * the handoff on its unused controller. Skip it. */
Andy Ross3610ea52011-05-11 15:52:38 -0700638 if (pdev->vendor == 0x8086 && pdev->device == 0x283a) {
Anisse Astier03c75362011-07-05 16:38:45 +0200639 if (dmi_check_system(ehci_dmi_nohandoff_table))
Andy Ross3610ea52011-05-11 15:52:38 -0700640 try_handoff = 0;
641 }
642
643 if (try_handoff && (cap & EHCI_USBLEGSUP_BIOS)) {
Andy Ross5c853012011-05-11 15:15:51 -0700644 dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
645
646#if 0
647/* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
648 * but that seems dubious in general (the BIOS left it off intentionally)
649 * and is known to prevent some systems from booting. so we won't do this
650 * unless maybe we can determine when we're on a system that needs SMI forced.
651 */
652 /* BIOS workaround (?): be sure the pre-Linux code
653 * receives the SMI
654 */
655 pci_read_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, &val);
656 pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS,
657 val | EHCI_USBLEGCTLSTS_SOOE);
658#endif
659
660 /* some systems get upset if this semaphore is
661 * set for any other reason than forcing a BIOS
662 * handoff..
663 */
664 pci_write_config_byte(pdev, offset + 3, 1);
665 }
666
667 /* if boot firmware now owns EHCI, spin till it hands it over. */
Andy Ross3610ea52011-05-11 15:52:38 -0700668 if (try_handoff) {
669 int msec = 1000;
670 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
671 tried_handoff = 1;
672 msleep(10);
673 msec -= 10;
674 pci_read_config_dword(pdev, offset, &cap);
675 }
Andy Ross5c853012011-05-11 15:15:51 -0700676 }
677
678 if (cap & EHCI_USBLEGSUP_BIOS) {
679 /* well, possibly buggy BIOS... try to shut it down,
680 * and hope nothing goes too wrong
681 */
Andy Ross3610ea52011-05-11 15:52:38 -0700682 if (try_handoff)
683 dev_warn(&pdev->dev, "EHCI: BIOS handoff failed"
684 " (BIOS bug?) %08x\n", cap);
Andy Ross5c853012011-05-11 15:15:51 -0700685 pci_write_config_byte(pdev, offset + 2, 0);
686 }
687
688 /* just in case, always disable EHCI SMIs */
689 pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, 0);
690
691 /* If the BIOS ever owned the controller then we can't expect
692 * any power sessions to remain intact.
693 */
694 if (tried_handoff)
695 writel(0, op_reg_base + EHCI_CONFIGFLAG);
696}
697
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500698static void quirk_usb_disable_ehci(struct pci_dev *pdev)
David Brownell75862692005-09-23 17:14:37 -0700699{
David Brownell75862692005-09-23 17:14:37 -0700700 void __iomem *base, *op_reg_base;
Andy Ross5c853012011-05-11 15:15:51 -0700701 u32 hcc_params, cap, val;
David Brownell401feaf2006-01-24 07:15:30 -0800702 u8 offset, cap_length;
Alan Stern97ff22e2011-10-27 11:20:21 -0400703 int wait_time, count = 256/4;
David Brownell75862692005-09-23 17:14:37 -0700704
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800705 if (!mmio_resource_enabled(pdev, 0))
706 return;
707
Arjan van de Ven8e8ce4b2008-10-20 21:46:01 -0700708 base = pci_ioremap_bar(pdev, 0);
709 if (base == NULL)
710 return;
David Brownell75862692005-09-23 17:14:37 -0700711
712 cap_length = readb(base);
713 op_reg_base = base + cap_length;
David Brownell75862692005-09-23 17:14:37 -0700714
David Brownell401feaf2006-01-24 07:15:30 -0800715 /* EHCI 0.96 and later may have "extended capabilities"
716 * spec section 5.1 explains the bios handoff, e.g. for
717 * booting from USB disk or using a usb keyboard
718 */
719 hcc_params = readl(base + EHCI_HCC_PARAMS);
720 offset = (hcc_params >> 8) & 0xff;
Roel Kluin6e14bda2009-01-31 12:37:04 +0100721 while (offset && --count) {
David Brownell401feaf2006-01-24 07:15:30 -0800722 pci_read_config_dword(pdev, offset, &cap);
Andy Ross5c853012011-05-11 15:15:51 -0700723
David Brownell401feaf2006-01-24 07:15:30 -0800724 switch (cap & 0xff) {
Andy Ross5c853012011-05-11 15:15:51 -0700725 case 1:
726 ehci_bios_handoff(pdev, op_reg_base, cap, offset);
David Brownell401feaf2006-01-24 07:15:30 -0800727 break;
Andy Ross5c853012011-05-11 15:15:51 -0700728 case 0: /* Illegal reserved cap, set cap=0 so we exit */
729 cap = 0; /* then fallthrough... */
David Brownell401feaf2006-01-24 07:15:30 -0800730 default:
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700731 dev_warn(&pdev->dev, "EHCI: unrecognized capability "
Andy Ross5c853012011-05-11 15:15:51 -0700732 "%02x\n", cap & 0xff);
David Brownell75862692005-09-23 17:14:37 -0700733 }
David Brownell401feaf2006-01-24 07:15:30 -0800734 offset = (cap >> 8) & 0xff;
David Brownell75862692005-09-23 17:14:37 -0700735 }
David Brownell401feaf2006-01-24 07:15:30 -0800736 if (!count)
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700737 dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
David Brownell75862692005-09-23 17:14:37 -0700738
739 /*
740 * halt EHCI & disable its interrupts in any case
741 */
742 val = readl(op_reg_base + EHCI_USBSTS);
743 if ((val & EHCI_USBSTS_HALTED) == 0) {
744 val = readl(op_reg_base + EHCI_USBCMD);
745 val &= ~EHCI_USBCMD_RUN;
746 writel(val, op_reg_base + EHCI_USBCMD);
747
748 wait_time = 2000;
David Brownell75862692005-09-23 17:14:37 -0700749 do {
750 writel(0x3f, op_reg_base + EHCI_USBSTS);
Alan Stern97ff22e2011-10-27 11:20:21 -0400751 udelay(100);
752 wait_time -= 100;
David Brownell75862692005-09-23 17:14:37 -0700753 val = readl(op_reg_base + EHCI_USBSTS);
754 if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
755 break;
756 }
757 } while (wait_time > 0);
758 }
759 writel(0, op_reg_base + EHCI_USBINTR);
760 writel(0x3f, op_reg_base + EHCI_USBSTS);
761
762 iounmap(base);
David Brownell75862692005-09-23 17:14:37 -0700763}
764
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700765/*
766 * handshake - spin reading a register until handshake completes
767 * @ptr: address of hc register to be read
768 * @mask: bits to look at in result of read
769 * @done: value of those bits when handshake succeeds
770 * @wait_usec: timeout in microseconds
771 * @delay_usec: delay in microseconds to wait between polling
772 *
773 * Polls a register every delay_usec microseconds.
774 * Returns 0 when the mask bits have the value done.
775 * Returns -ETIMEDOUT if this condition is not true after
776 * wait_usec microseconds have passed.
777 */
778static int handshake(void __iomem *ptr, u32 mask, u32 done,
779 int wait_usec, int delay_usec)
780{
781 u32 result;
David Brownell75862692005-09-23 17:14:37 -0700782
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700783 do {
784 result = readl(ptr);
785 result &= mask;
786 if (result == done)
787 return 0;
788 udelay(delay_usec);
789 wait_usec -= delay_usec;
790 } while (wait_usec > 0);
791 return -ETIMEDOUT;
792}
793
Sarah Sharp69e848c2011-02-22 09:57:15 -0800794/*
795 * Intel's Panther Point chipset has two host controllers (EHCI and xHCI) that
796 * share some number of ports. These ports can be switched between either
797 * controller. Not all of the ports under the EHCI host controller may be
798 * switchable.
799 *
800 * The ports should be switched over to xHCI before PCI probes for any device
801 * start. This avoids active devices under EHCI being disconnected during the
802 * port switchover, which could cause loss of data on USB storage devices, or
803 * failed boot when the root file system is on a USB mass storage device and is
804 * enumerated under EHCI first.
805 *
806 * We write into the xHC's PCI configuration space in some Intel-specific
807 * registers to switch the ports over. The USB 3.0 terminations and the USB
808 * 2.0 data wires are switched separately. We want to enable the SuperSpeed
809 * terminations before switching the USB 2.0 wires over, so that USB 3.0
810 * devices connect at SuperSpeed, rather than at USB 2.0 speeds.
811 */
Mathias Nyman26b76792013-07-23 11:35:47 +0300812void usb_enable_intel_xhci_ports(struct pci_dev *xhci_pdev)
Sarah Sharp69e848c2011-02-22 09:57:15 -0800813{
814 u32 ports_available;
Mathias Nyman26b76792013-07-23 11:35:47 +0300815 bool ehci_found = false;
816 struct pci_dev *companion = NULL;
817
818 /* make sure an intel EHCI controller exists */
819 for_each_pci_dev(companion) {
820 if (companion->class == PCI_CLASS_SERIAL_USB_EHCI &&
821 companion->vendor == PCI_VENDOR_ID_INTEL) {
822 ehci_found = true;
823 break;
824 }
825 }
826
827 if (!ehci_found)
828 return;
Sarah Sharp69e848c2011-02-22 09:57:15 -0800829
Sarah Sharp51c9e6c2012-04-16 10:56:47 -0700830 /* Don't switchover the ports if the user hasn't compiled the xHCI
831 * driver. Otherwise they will see "dead" USB ports that don't power
832 * the devices.
833 */
834 if (!IS_ENABLED(CONFIG_USB_XHCI_HCD)) {
835 dev_warn(&xhci_pdev->dev,
836 "CONFIG_USB_XHCI_HCD is turned off, "
837 "defaulting to EHCI.\n");
838 dev_warn(&xhci_pdev->dev,
839 "USB 3.0 devices will work at USB 2.0 speeds.\n");
David Moore58b29392013-01-23 22:19:49 -0800840 usb_disable_xhci_ports(xhci_pdev);
Sarah Sharp51c9e6c2012-04-16 10:56:47 -0700841 return;
842 }
843
Keng-Yu Lina96874a2012-08-10 01:39:23 +0800844 /* Read USB3PRM, the USB 3.0 Port Routing Mask Register
845 * Indicate the ports that can be changed from OS.
846 */
847 pci_read_config_dword(xhci_pdev, USB_INTEL_USB3PRM,
848 &ports_available);
849
850 dev_dbg(&xhci_pdev->dev, "Configurable ports to enable SuperSpeed: 0x%x\n",
851 ports_available);
852
Sarah Sharp69e848c2011-02-22 09:57:15 -0800853 /* Write USB3_PSSEN, the USB 3.0 Port SuperSpeed Enable
Keng-Yu Lina96874a2012-08-10 01:39:23 +0800854 * Register, to turn on SuperSpeed terminations for the
855 * switchable ports.
Sarah Sharp69e848c2011-02-22 09:57:15 -0800856 */
857 pci_write_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
858 cpu_to_le32(ports_available));
859
860 pci_read_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
861 &ports_available);
862 dev_dbg(&xhci_pdev->dev, "USB 3.0 ports that are now enabled "
863 "under xHCI: 0x%x\n", ports_available);
864
Keng-Yu Lina96874a2012-08-10 01:39:23 +0800865 /* Read XUSB2PRM, xHCI USB 2.0 Port Routing Mask Register
866 * Indicate the USB 2.0 ports to be controlled by the xHCI host.
867 */
868
869 pci_read_config_dword(xhci_pdev, USB_INTEL_USB2PRM,
870 &ports_available);
871
872 dev_dbg(&xhci_pdev->dev, "Configurable USB 2.0 ports to hand over to xCHI: 0x%x\n",
873 ports_available);
874
Sarah Sharp69e848c2011-02-22 09:57:15 -0800875 /* Write XUSB2PR, the xHC USB 2.0 Port Routing Register, to
876 * switch the USB 2.0 power and data lines over to the xHCI
877 * host.
878 */
879 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
880 cpu_to_le32(ports_available));
881
882 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
883 &ports_available);
884 dev_dbg(&xhci_pdev->dev, "USB 2.0 ports that are now switched over "
885 "to xHCI: 0x%x\n", ports_available);
886}
Mathias Nyman26b76792013-07-23 11:35:47 +0300887EXPORT_SYMBOL_GPL(usb_enable_intel_xhci_ports);
Sarah Sharp69e848c2011-02-22 09:57:15 -0800888
Sarah Sharpe95829f2012-07-23 18:59:30 +0300889void usb_disable_xhci_ports(struct pci_dev *xhci_pdev)
890{
891 pci_write_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN, 0x0);
892 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, 0x0);
893}
894EXPORT_SYMBOL_GPL(usb_disable_xhci_ports);
895
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700896/**
897 * PCI Quirks for xHCI.
898 *
899 * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
900 * It signals to the BIOS that the OS wants control of the host controller,
901 * and then waits 5 seconds for the BIOS to hand over control.
902 * If we timeout, assume the BIOS is broken and take control anyway.
903 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500904static void quirk_usb_handoff_xhci(struct pci_dev *pdev)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700905{
906 void __iomem *base;
907 int ext_cap_offset;
908 void __iomem *op_reg_base;
909 u32 val;
910 int timeout;
Matthew Garrette955a1c2012-08-14 16:44:49 -0400911 int len = pci_resource_len(pdev, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700912
913 if (!mmio_resource_enabled(pdev, 0))
914 return;
915
Matthew Garrette955a1c2012-08-14 16:44:49 -0400916 base = ioremap_nocache(pci_resource_start(pdev, 0), len);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700917 if (base == NULL)
918 return;
919
920 /*
921 * Find the Legacy Support Capability register -
922 * this is optional for xHCI host controllers.
923 */
924 ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
925 do {
Matthew Garrette955a1c2012-08-14 16:44:49 -0400926 if ((ext_cap_offset + sizeof(val)) > len) {
927 /* We're reading garbage from the controller */
928 dev_warn(&pdev->dev,
929 "xHCI controller failing to respond");
930 return;
931 }
932
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700933 if (!ext_cap_offset)
934 /* We've reached the end of the extended capabilities */
935 goto hc_init;
Matthew Garrette955a1c2012-08-14 16:44:49 -0400936
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700937 val = readl(base + ext_cap_offset);
938 if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
939 break;
940 ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
941 } while (1);
942
943 /* If the BIOS owns the HC, signal that the OS wants it, and wait */
944 if (val & XHCI_HC_BIOS_OWNED) {
JiSheng Zhang67684582011-07-16 11:04:19 +0800945 writel(val | XHCI_HC_OS_OWNED, base + ext_cap_offset);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700946
947 /* Wait for 5 seconds with 10 microsecond polling interval */
948 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
949 0, 5000, 10);
950
951 /* Assume a buggy BIOS and take HC ownership anyway */
952 if (timeout) {
953 dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
954 " (BIOS bug ?) %08x\n", val);
955 writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
956 }
957 }
958
Alex He95018a52012-03-30 10:21:38 +0800959 val = readl(base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
960 /* Mask off (turn off) any enabled SMIs */
961 val &= XHCI_LEGACY_DISABLE_SMI;
962 /* Mask all SMI events bits, RW1C */
963 val |= XHCI_LEGACY_SMI_EVENTS;
964 /* Disable any BIOS SMIs and clear all SMI events*/
965 writel(val, base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700966
Manoj Iyer29d21452012-08-22 11:53:18 -0500967hc_init:
Mathias Nyman26b76792013-07-23 11:35:47 +0300968 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
969 usb_enable_intel_xhci_ports(pdev);
Manoj Iyer29d21452012-08-22 11:53:18 -0500970
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700971 op_reg_base = base + XHCI_HC_LENGTH(readl(base));
972
973 /* Wait for the host controller to be ready before writing any
974 * operational or runtime registers. Wait 5 seconds and no more.
975 */
976 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
977 5000, 10);
978 /* Assume a buggy HC and start HC initialization anyway */
979 if (timeout) {
980 val = readl(op_reg_base + XHCI_STS_OFFSET);
981 dev_warn(&pdev->dev,
982 "xHCI HW not ready after 5 sec (HC bug?) "
983 "status = 0x%x\n", val);
984 }
985
986 /* Send the halt and disable interrupts command */
987 val = readl(op_reg_base + XHCI_CMD_OFFSET);
988 val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
989 writel(val, op_reg_base + XHCI_CMD_OFFSET);
990
991 /* Wait for the HC to halt - poll every 125 usec (one microframe). */
992 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
993 XHCI_MAX_HALT_USEC, 125);
994 if (timeout) {
995 val = readl(op_reg_base + XHCI_STS_OFFSET);
996 dev_warn(&pdev->dev,
997 "xHCI HW did not halt within %d usec "
998 "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
999 }
1000
1001 iounmap(base);
1002}
David Brownell75862692005-09-23 17:14:37 -07001003
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001004static void quirk_usb_early_handoff(struct pci_dev *pdev)
David Brownell75862692005-09-23 17:14:37 -07001005{
Jayachandran Ce4436a72012-01-27 20:27:32 +05301006 /* Skip Netlogic mips SoC's internal PCI USB controller.
1007 * This device does not need/support EHCI/OHCI handoff
1008 */
1009 if (pdev->vendor == 0x184e) /* vendor Netlogic */
1010 return;
Sarah Sharpcab928ee2012-02-07 15:11:46 -08001011 if (pdev->class != PCI_CLASS_SERIAL_USB_UHCI &&
1012 pdev->class != PCI_CLASS_SERIAL_USB_OHCI &&
1013 pdev->class != PCI_CLASS_SERIAL_USB_EHCI &&
1014 pdev->class != PCI_CLASS_SERIAL_USB_XHCI)
1015 return;
Jayachandran Ce4436a72012-01-27 20:27:32 +05301016
Sarah Sharpcab928ee2012-02-07 15:11:46 -08001017 if (pci_enable_device(pdev) < 0) {
1018 dev_warn(&pdev->dev, "Can't enable PCI device, "
1019 "BIOS handoff failed.\n");
1020 return;
1021 }
Alan Stern478a3ba2005-10-19 12:52:02 -04001022 if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
David Brownell75862692005-09-23 17:14:37 -07001023 quirk_usb_handoff_uhci(pdev);
Alan Stern478a3ba2005-10-19 12:52:02 -04001024 else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
David Brownell75862692005-09-23 17:14:37 -07001025 quirk_usb_handoff_ohci(pdev);
Alan Stern478a3ba2005-10-19 12:52:02 -04001026 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
David Brownell75862692005-09-23 17:14:37 -07001027 quirk_usb_disable_ehci(pdev);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001028 else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
1029 quirk_usb_handoff_xhci(pdev);
Sarah Sharpcab928ee2012-02-07 15:11:46 -08001030 pci_disable_device(pdev);
David Brownell75862692005-09-23 17:14:37 -07001031}
Yinghai Lu8474ecd2012-02-23 23:46:59 -08001032DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
1033 PCI_CLASS_SERIAL_USB, 8, quirk_usb_early_handoff);