]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/usb/host/pci-quirks.c
USB host: Move AMD PLL quirk to pci-quirks.c
[mv-sheeva.git] / drivers / usb / host / pci-quirks.c
1 /*
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
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/acpi.h>
17 #include "pci-quirks.h"
18 #include "xhci-ext-caps.h"
19
20
21 #define UHCI_USBLEGSUP          0xc0            /* legacy support */
22 #define UHCI_USBCMD             0               /* command register */
23 #define UHCI_USBINTR            4               /* interrupt register */
24 #define UHCI_USBLEGSUP_RWC      0x8f00          /* the R/WC bits */
25 #define UHCI_USBLEGSUP_RO       0x5040          /* R/O and reserved bits */
26 #define UHCI_USBCMD_RUN         0x0001          /* RUN/STOP bit */
27 #define UHCI_USBCMD_HCRESET     0x0002          /* Host Controller reset */
28 #define UHCI_USBCMD_EGSM        0x0008          /* Global Suspend Mode */
29 #define UHCI_USBCMD_CONFIGURE   0x0040          /* Config Flag */
30 #define UHCI_USBINTR_RESUME     0x0002          /* Resume interrupt enable */
31
32 #define OHCI_CONTROL            0x04
33 #define OHCI_CMDSTATUS          0x08
34 #define OHCI_INTRSTATUS         0x0c
35 #define OHCI_INTRENABLE         0x10
36 #define OHCI_INTRDISABLE        0x14
37 #define OHCI_OCR                (1 << 3)        /* ownership change request */
38 #define OHCI_CTRL_RWC           (1 << 9)        /* remote wakeup connected */
39 #define OHCI_CTRL_IR            (1 << 8)        /* interrupt routing */
40 #define OHCI_INTR_OC            (1 << 30)       /* ownership change */
41
42 #define EHCI_HCC_PARAMS         0x08            /* extended capabilities */
43 #define EHCI_USBCMD             0               /* command register */
44 #define EHCI_USBCMD_RUN         (1 << 0)        /* RUN/STOP bit */
45 #define EHCI_USBSTS             4               /* status register */
46 #define EHCI_USBSTS_HALTED      (1 << 12)       /* HCHalted bit */
47 #define EHCI_USBINTR            8               /* interrupt register */
48 #define EHCI_CONFIGFLAG         0x40            /* configured flag register */
49 #define EHCI_USBLEGSUP          0               /* legacy support register */
50 #define EHCI_USBLEGSUP_BIOS     (1 << 16)       /* BIOS semaphore */
51 #define EHCI_USBLEGSUP_OS       (1 << 24)       /* OS semaphore */
52 #define EHCI_USBLEGCTLSTS       4               /* legacy control/status */
53 #define EHCI_USBLEGCTLSTS_SOOE  (1 << 13)       /* SMI on ownership change */
54
55 /* AMD quirk use */
56 #define AB_REG_BAR_LOW          0xe0
57 #define AB_REG_BAR_HIGH         0xe1
58 #define AB_REG_BAR_SB700        0xf0
59 #define AB_INDX(addr)           ((addr) + 0x00)
60 #define AB_DATA(addr)           ((addr) + 0x04)
61 #define AX_INDXC                0x30
62 #define AX_DATAC                0x34
63
64 #define NB_PCIE_INDX_ADDR       0xe0
65 #define NB_PCIE_INDX_DATA       0xe4
66 #define PCIE_P_CNTL             0x10040
67 #define BIF_NB                  0x10002
68 #define NB_PIF0_PWRDOWN_0       0x01100012
69 #define NB_PIF0_PWRDOWN_1       0x01100013
70
71 static struct amd_chipset_info {
72         struct pci_dev  *nb_dev;
73         struct pci_dev  *smbus_dev;
74         int nb_type;
75         int sb_type;
76         int isoc_reqs;
77         int probe_count;
78         int probe_result;
79 } amd_chipset;
80
81 static DEFINE_SPINLOCK(amd_lock);
82
83 int usb_amd_find_chipset_info(void)
84 {
85         u8 rev = 0;
86         unsigned long flags;
87
88         spin_lock_irqsave(&amd_lock, flags);
89
90         amd_chipset.probe_count++;
91         /* probe only once */
92         if (amd_chipset.probe_count > 1) {
93                 spin_unlock_irqrestore(&amd_lock, flags);
94                 return amd_chipset.probe_result;
95         }
96
97         amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
98         if (amd_chipset.smbus_dev) {
99                 pci_read_config_byte(amd_chipset.smbus_dev,
100                                         PCI_REVISION_ID, &rev);
101                 if (rev >= 0x40)
102                         amd_chipset.sb_type = 1;
103                 else if (rev >= 0x30 && rev <= 0x3b)
104                         amd_chipset.sb_type = 3;
105         } else {
106                 amd_chipset.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
107                                                         0x780b, NULL);
108                 if (!amd_chipset.smbus_dev) {
109                         spin_unlock_irqrestore(&amd_lock, flags);
110                         return 0;
111                 }
112                 pci_read_config_byte(amd_chipset.smbus_dev,
113                                         PCI_REVISION_ID, &rev);
114                 if (rev >= 0x11 && rev <= 0x18)
115                         amd_chipset.sb_type = 2;
116         }
117
118         if (amd_chipset.sb_type == 0) {
119                 if (amd_chipset.smbus_dev) {
120                         pci_dev_put(amd_chipset.smbus_dev);
121                         amd_chipset.smbus_dev = NULL;
122                 }
123                 spin_unlock_irqrestore(&amd_lock, flags);
124                 return 0;
125         }
126
127         amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
128         if (amd_chipset.nb_dev) {
129                 amd_chipset.nb_type = 1;
130         } else {
131                 amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
132                                                         0x1510, NULL);
133                 if (amd_chipset.nb_dev) {
134                         amd_chipset.nb_type = 2;
135                 } else  {
136                         amd_chipset.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
137                                                                 0x9600, NULL);
138                         if (amd_chipset.nb_dev)
139                                 amd_chipset.nb_type = 3;
140                 }
141         }
142
143         amd_chipset.probe_result = 1;
144         printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
145
146         spin_unlock_irqrestore(&amd_lock, flags);
147         return amd_chipset.probe_result;
148 }
149 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
150
151 /*
152  * The hardware normally enables the A-link power management feature, which
153  * lets the system lower the power consumption in idle states.
154  *
155  * This USB quirk prevents the link going into that lower power state
156  * during isochronous transfers.
157  *
158  * Without this quirk, isochronous stream on OHCI/EHCI/xHCI controllers of
159  * some AMD platforms may stutter or have breaks occasionally.
160  */
161 static void usb_amd_quirk_pll(int disable)
162 {
163         u32 addr, addr_low, addr_high, val;
164         u32 bit = disable ? 0 : 1;
165         unsigned long flags;
166
167         spin_lock_irqsave(&amd_lock, flags);
168
169         if (disable) {
170                 amd_chipset.isoc_reqs++;
171                 if (amd_chipset.isoc_reqs > 1) {
172                         spin_unlock_irqrestore(&amd_lock, flags);
173                         return;
174                 }
175         } else {
176                 amd_chipset.isoc_reqs--;
177                 if (amd_chipset.isoc_reqs > 0) {
178                         spin_unlock_irqrestore(&amd_lock, flags);
179                         return;
180                 }
181         }
182
183         if (amd_chipset.sb_type == 1 || amd_chipset.sb_type == 2) {
184                 outb_p(AB_REG_BAR_LOW, 0xcd6);
185                 addr_low = inb_p(0xcd7);
186                 outb_p(AB_REG_BAR_HIGH, 0xcd6);
187                 addr_high = inb_p(0xcd7);
188                 addr = addr_high << 8 | addr_low;
189
190                 outl_p(0x30, AB_INDX(addr));
191                 outl_p(0x40, AB_DATA(addr));
192                 outl_p(0x34, AB_INDX(addr));
193                 val = inl_p(AB_DATA(addr));
194         } else if (amd_chipset.sb_type == 3) {
195                 pci_read_config_dword(amd_chipset.smbus_dev,
196                                         AB_REG_BAR_SB700, &addr);
197                 outl(AX_INDXC, AB_INDX(addr));
198                 outl(0x40, AB_DATA(addr));
199                 outl(AX_DATAC, AB_INDX(addr));
200                 val = inl(AB_DATA(addr));
201         } else {
202                 spin_unlock_irqrestore(&amd_lock, flags);
203                 return;
204         }
205
206         if (disable) {
207                 val &= ~0x08;
208                 val |= (1 << 4) | (1 << 9);
209         } else {
210                 val |= 0x08;
211                 val &= ~((1 << 4) | (1 << 9));
212         }
213         outl_p(val, AB_DATA(addr));
214
215         if (!amd_chipset.nb_dev) {
216                 spin_unlock_irqrestore(&amd_lock, flags);
217                 return;
218         }
219
220         if (amd_chipset.nb_type == 1 || amd_chipset.nb_type == 3) {
221                 addr = PCIE_P_CNTL;
222                 pci_write_config_dword(amd_chipset.nb_dev,
223                                         NB_PCIE_INDX_ADDR, addr);
224                 pci_read_config_dword(amd_chipset.nb_dev,
225                                         NB_PCIE_INDX_DATA, &val);
226
227                 val &= ~(1 | (1 << 3) | (1 << 4) | (1 << 9) | (1 << 12));
228                 val |= bit | (bit << 3) | (bit << 12);
229                 val |= ((!bit) << 4) | ((!bit) << 9);
230                 pci_write_config_dword(amd_chipset.nb_dev,
231                                         NB_PCIE_INDX_DATA, val);
232
233                 addr = BIF_NB;
234                 pci_write_config_dword(amd_chipset.nb_dev,
235                                         NB_PCIE_INDX_ADDR, addr);
236                 pci_read_config_dword(amd_chipset.nb_dev,
237                                         NB_PCIE_INDX_DATA, &val);
238                 val &= ~(1 << 8);
239                 val |= bit << 8;
240
241                 pci_write_config_dword(amd_chipset.nb_dev,
242                                         NB_PCIE_INDX_DATA, val);
243         } else if (amd_chipset.nb_type == 2) {
244                 addr = NB_PIF0_PWRDOWN_0;
245                 pci_write_config_dword(amd_chipset.nb_dev,
246                                         NB_PCIE_INDX_ADDR, addr);
247                 pci_read_config_dword(amd_chipset.nb_dev,
248                                         NB_PCIE_INDX_DATA, &val);
249                 if (disable)
250                         val &= ~(0x3f << 7);
251                 else
252                         val |= 0x3f << 7;
253
254                 pci_write_config_dword(amd_chipset.nb_dev,
255                                         NB_PCIE_INDX_DATA, val);
256
257                 addr = NB_PIF0_PWRDOWN_1;
258                 pci_write_config_dword(amd_chipset.nb_dev,
259                                         NB_PCIE_INDX_ADDR, addr);
260                 pci_read_config_dword(amd_chipset.nb_dev,
261                                         NB_PCIE_INDX_DATA, &val);
262                 if (disable)
263                         val &= ~(0x3f << 7);
264                 else
265                         val |= 0x3f << 7;
266
267                 pci_write_config_dword(amd_chipset.nb_dev,
268                                         NB_PCIE_INDX_DATA, val);
269         }
270
271         spin_unlock_irqrestore(&amd_lock, flags);
272         return;
273 }
274
275 void usb_amd_quirk_pll_disable(void)
276 {
277         usb_amd_quirk_pll(1);
278 }
279 EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_disable);
280
281 void usb_amd_quirk_pll_enable(void)
282 {
283         usb_amd_quirk_pll(0);
284 }
285 EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
286
287 void usb_amd_dev_put(void)
288 {
289         unsigned long flags;
290
291         spin_lock_irqsave(&amd_lock, flags);
292
293         amd_chipset.probe_count--;
294         if (amd_chipset.probe_count > 0) {
295                 spin_unlock_irqrestore(&amd_lock, flags);
296                 return;
297         }
298
299         if (amd_chipset.nb_dev) {
300                 pci_dev_put(amd_chipset.nb_dev);
301                 amd_chipset.nb_dev = NULL;
302         }
303         if (amd_chipset.smbus_dev) {
304                 pci_dev_put(amd_chipset.smbus_dev);
305                 amd_chipset.smbus_dev = NULL;
306         }
307         amd_chipset.nb_type = 0;
308         amd_chipset.sb_type = 0;
309         amd_chipset.isoc_reqs = 0;
310         amd_chipset.probe_result = 0;
311
312         spin_unlock_irqrestore(&amd_lock, flags);
313 }
314 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
315
316 /*
317  * Make sure the controller is completely inactive, unable to
318  * generate interrupts or do DMA.
319  */
320 void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
321 {
322         /* Turn off PIRQ enable and SMI enable.  (This also turns off the
323          * BIOS's USB Legacy Support.)  Turn off all the R/WC bits too.
324          */
325         pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
326
327         /* Reset the HC - this will force us to get a
328          * new notification of any already connected
329          * ports due to the virtual disconnect that it
330          * implies.
331          */
332         outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
333         mb();
334         udelay(5);
335         if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
336                 dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
337
338         /* Just to be safe, disable interrupt requests and
339          * make sure the controller is stopped.
340          */
341         outw(0, base + UHCI_USBINTR);
342         outw(0, base + UHCI_USBCMD);
343 }
344 EXPORT_SYMBOL_GPL(uhci_reset_hc);
345
346 /*
347  * Initialize a controller that was newly discovered or has just been
348  * resumed.  In either case we can't be sure of its previous state.
349  *
350  * Returns: 1 if the controller was reset, 0 otherwise.
351  */
352 int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
353 {
354         u16 legsup;
355         unsigned int cmd, intr;
356
357         /*
358          * When restarting a suspended controller, we expect all the
359          * settings to be the same as we left them:
360          *
361          *      PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
362          *      Controller is stopped and configured with EGSM set;
363          *      No interrupts enabled except possibly Resume Detect.
364          *
365          * If any of these conditions are violated we do a complete reset.
366          */
367         pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
368         if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
369                 dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
370                                 __func__, legsup);
371                 goto reset_needed;
372         }
373
374         cmd = inw(base + UHCI_USBCMD);
375         if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
376                         !(cmd & UHCI_USBCMD_EGSM)) {
377                 dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
378                                 __func__, cmd);
379                 goto reset_needed;
380         }
381
382         intr = inw(base + UHCI_USBINTR);
383         if (intr & (~UHCI_USBINTR_RESUME)) {
384                 dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
385                                 __func__, intr);
386                 goto reset_needed;
387         }
388         return 0;
389
390 reset_needed:
391         dev_dbg(&pdev->dev, "Performing full reset\n");
392         uhci_reset_hc(pdev, base);
393         return 1;
394 }
395 EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
396
397 static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
398 {
399         u16 cmd;
400         return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
401 }
402
403 #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
404 #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
405
406 static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
407 {
408         unsigned long base = 0;
409         int i;
410
411         if (!pio_enabled(pdev))
412                 return;
413
414         for (i = 0; i < PCI_ROM_RESOURCE; i++)
415                 if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
416                         base = pci_resource_start(pdev, i);
417                         break;
418                 }
419
420         if (base)
421                 uhci_check_and_reset_hc(pdev, base);
422 }
423
424 static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
425 {
426         return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
427 }
428
429 static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
430 {
431         void __iomem *base;
432         u32 control;
433
434         if (!mmio_resource_enabled(pdev, 0))
435                 return;
436
437         base = pci_ioremap_bar(pdev, 0);
438         if (base == NULL)
439                 return;
440
441         control = readl(base + OHCI_CONTROL);
442
443 /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
444 #ifdef __hppa__
445 #define OHCI_CTRL_MASK          (OHCI_CTRL_RWC | OHCI_CTRL_IR)
446 #else
447 #define OHCI_CTRL_MASK          OHCI_CTRL_RWC
448
449         if (control & OHCI_CTRL_IR) {
450                 int wait_time = 500; /* arbitrary; 5 seconds */
451                 writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
452                 writel(OHCI_OCR, base + OHCI_CMDSTATUS);
453                 while (wait_time > 0 &&
454                                 readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
455                         wait_time -= 10;
456                         msleep(10);
457                 }
458                 if (wait_time <= 0)
459                         dev_warn(&pdev->dev, "OHCI: BIOS handoff failed"
460                                         " (BIOS bug?) %08x\n",
461                                         readl(base + OHCI_CONTROL));
462         }
463 #endif
464
465         /* reset controller, preserving RWC (and possibly IR) */
466         writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
467
468         /*
469          * disable interrupts
470          */
471         writel(~(u32)0, base + OHCI_INTRDISABLE);
472         writel(~(u32)0, base + OHCI_INTRSTATUS);
473
474         iounmap(base);
475 }
476
477 static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
478 {
479         int wait_time, delta;
480         void __iomem *base, *op_reg_base;
481         u32     hcc_params, val;
482         u8      offset, cap_length;
483         int     count = 256/4;
484         int     tried_handoff = 0;
485
486         if (!mmio_resource_enabled(pdev, 0))
487                 return;
488
489         base = pci_ioremap_bar(pdev, 0);
490         if (base == NULL)
491                 return;
492
493         cap_length = readb(base);
494         op_reg_base = base + cap_length;
495
496         /* EHCI 0.96 and later may have "extended capabilities"
497          * spec section 5.1 explains the bios handoff, e.g. for
498          * booting from USB disk or using a usb keyboard
499          */
500         hcc_params = readl(base + EHCI_HCC_PARAMS);
501         offset = (hcc_params >> 8) & 0xff;
502         while (offset && --count) {
503                 u32             cap;
504                 int             msec;
505
506                 pci_read_config_dword(pdev, offset, &cap);
507                 switch (cap & 0xff) {
508                 case 1:                 /* BIOS/SMM/... handoff support */
509                         if ((cap & EHCI_USBLEGSUP_BIOS)) {
510                                 dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
511
512 #if 0
513 /* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
514  * but that seems dubious in general (the BIOS left it off intentionally)
515  * and is known to prevent some systems from booting.  so we won't do this
516  * unless maybe we can determine when we're on a system that needs SMI forced.
517  */
518                                 /* BIOS workaround (?): be sure the
519                                  * pre-Linux code receives the SMI
520                                  */
521                                 pci_read_config_dword(pdev,
522                                                 offset + EHCI_USBLEGCTLSTS,
523                                                 &val);
524                                 pci_write_config_dword(pdev,
525                                                 offset + EHCI_USBLEGCTLSTS,
526                                                 val | EHCI_USBLEGCTLSTS_SOOE);
527 #endif
528
529                                 /* some systems get upset if this semaphore is
530                                  * set for any other reason than forcing a BIOS
531                                  * handoff..
532                                  */
533                                 pci_write_config_byte(pdev, offset + 3, 1);
534                         }
535
536                         /* if boot firmware now owns EHCI, spin till
537                          * it hands it over.
538                          */
539                         msec = 1000;
540                         while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
541                                 tried_handoff = 1;
542                                 msleep(10);
543                                 msec -= 10;
544                                 pci_read_config_dword(pdev, offset, &cap);
545                         }
546
547                         if (cap & EHCI_USBLEGSUP_BIOS) {
548                                 /* well, possibly buggy BIOS... try to shut
549                                  * it down, and hope nothing goes too wrong
550                                  */
551                                 dev_warn(&pdev->dev, "EHCI: BIOS handoff failed"
552                                                 " (BIOS bug?) %08x\n", cap);
553                                 pci_write_config_byte(pdev, offset + 2, 0);
554                         }
555
556                         /* just in case, always disable EHCI SMIs */
557                         pci_write_config_dword(pdev,
558                                         offset + EHCI_USBLEGCTLSTS,
559                                         0);
560
561                         /* If the BIOS ever owned the controller then we
562                          * can't expect any power sessions to remain intact.
563                          */
564                         if (tried_handoff)
565                                 writel(0, op_reg_base + EHCI_CONFIGFLAG);
566                         break;
567                 case 0:                 /* illegal reserved capability */
568                         cap = 0;
569                         /* FALLTHROUGH */
570                 default:
571                         dev_warn(&pdev->dev, "EHCI: unrecognized capability "
572                                         "%02x\n", cap & 0xff);
573                         break;
574                 }
575                 offset = (cap >> 8) & 0xff;
576         }
577         if (!count)
578                 dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
579
580         /*
581          * halt EHCI & disable its interrupts in any case
582          */
583         val = readl(op_reg_base + EHCI_USBSTS);
584         if ((val & EHCI_USBSTS_HALTED) == 0) {
585                 val = readl(op_reg_base + EHCI_USBCMD);
586                 val &= ~EHCI_USBCMD_RUN;
587                 writel(val, op_reg_base + EHCI_USBCMD);
588
589                 wait_time = 2000;
590                 delta = 100;
591                 do {
592                         writel(0x3f, op_reg_base + EHCI_USBSTS);
593                         udelay(delta);
594                         wait_time -= delta;
595                         val = readl(op_reg_base + EHCI_USBSTS);
596                         if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
597                                 break;
598                         }
599                 } while (wait_time > 0);
600         }
601         writel(0, op_reg_base + EHCI_USBINTR);
602         writel(0x3f, op_reg_base + EHCI_USBSTS);
603
604         iounmap(base);
605 }
606
607 /*
608  * handshake - spin reading a register until handshake completes
609  * @ptr: address of hc register to be read
610  * @mask: bits to look at in result of read
611  * @done: value of those bits when handshake succeeds
612  * @wait_usec: timeout in microseconds
613  * @delay_usec: delay in microseconds to wait between polling
614  *
615  * Polls a register every delay_usec microseconds.
616  * Returns 0 when the mask bits have the value done.
617  * Returns -ETIMEDOUT if this condition is not true after
618  * wait_usec microseconds have passed.
619  */
620 static int handshake(void __iomem *ptr, u32 mask, u32 done,
621                 int wait_usec, int delay_usec)
622 {
623         u32     result;
624
625         do {
626                 result = readl(ptr);
627                 result &= mask;
628                 if (result == done)
629                         return 0;
630                 udelay(delay_usec);
631                 wait_usec -= delay_usec;
632         } while (wait_usec > 0);
633         return -ETIMEDOUT;
634 }
635
636 /**
637  * PCI Quirks for xHCI.
638  *
639  * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
640  * It signals to the BIOS that the OS wants control of the host controller,
641  * and then waits 5 seconds for the BIOS to hand over control.
642  * If we timeout, assume the BIOS is broken and take control anyway.
643  */
644 static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
645 {
646         void __iomem *base;
647         int ext_cap_offset;
648         void __iomem *op_reg_base;
649         u32 val;
650         int timeout;
651
652         if (!mmio_resource_enabled(pdev, 0))
653                 return;
654
655         base = ioremap_nocache(pci_resource_start(pdev, 0),
656                                 pci_resource_len(pdev, 0));
657         if (base == NULL)
658                 return;
659
660         /*
661          * Find the Legacy Support Capability register -
662          * this is optional for xHCI host controllers.
663          */
664         ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
665         do {
666                 if (!ext_cap_offset)
667                         /* We've reached the end of the extended capabilities */
668                         goto hc_init;
669                 val = readl(base + ext_cap_offset);
670                 if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
671                         break;
672                 ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
673         } while (1);
674
675         /* If the BIOS owns the HC, signal that the OS wants it, and wait */
676         if (val & XHCI_HC_BIOS_OWNED) {
677                 writel(val & XHCI_HC_OS_OWNED, base + ext_cap_offset);
678
679                 /* Wait for 5 seconds with 10 microsecond polling interval */
680                 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
681                                 0, 5000, 10);
682
683                 /* Assume a buggy BIOS and take HC ownership anyway */
684                 if (timeout) {
685                         dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
686                                         " (BIOS bug ?) %08x\n", val);
687                         writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
688                 }
689         }
690
691         /* Disable any BIOS SMIs */
692         writel(XHCI_LEGACY_DISABLE_SMI,
693                         base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
694
695 hc_init:
696         op_reg_base = base + XHCI_HC_LENGTH(readl(base));
697
698         /* Wait for the host controller to be ready before writing any
699          * operational or runtime registers.  Wait 5 seconds and no more.
700          */
701         timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
702                         5000, 10);
703         /* Assume a buggy HC and start HC initialization anyway */
704         if (timeout) {
705                 val = readl(op_reg_base + XHCI_STS_OFFSET);
706                 dev_warn(&pdev->dev,
707                                 "xHCI HW not ready after 5 sec (HC bug?) "
708                                 "status = 0x%x\n", val);
709         }
710
711         /* Send the halt and disable interrupts command */
712         val = readl(op_reg_base + XHCI_CMD_OFFSET);
713         val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
714         writel(val, op_reg_base + XHCI_CMD_OFFSET);
715
716         /* Wait for the HC to halt - poll every 125 usec (one microframe). */
717         timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
718                         XHCI_MAX_HALT_USEC, 125);
719         if (timeout) {
720                 val = readl(op_reg_base + XHCI_STS_OFFSET);
721                 dev_warn(&pdev->dev,
722                                 "xHCI HW did not halt within %d usec "
723                                 "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
724         }
725
726         iounmap(base);
727 }
728
729 static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
730 {
731         if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
732                 quirk_usb_handoff_uhci(pdev);
733         else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
734                 quirk_usb_handoff_ohci(pdev);
735         else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
736                 quirk_usb_disable_ehci(pdev);
737         else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
738                 quirk_usb_handoff_xhci(pdev);
739 }
740 DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);