GNU Linux-libre 4.14.290-gnu1
[releases.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/delay.h>
15 #include <linux/export.h>
16 #include <linux/acpi.h>
17 #include <linux/dmi.h>
18 #include "pci-quirks.h"
19 #include "xhci-ext-caps.h"
20
21
22 #define UHCI_USBLEGSUP          0xc0            /* legacy support */
23 #define UHCI_USBCMD             0               /* command register */
24 #define UHCI_USBINTR            4               /* interrupt register */
25 #define UHCI_USBLEGSUP_RWC      0x8f00          /* the R/WC bits */
26 #define UHCI_USBLEGSUP_RO       0x5040          /* R/O and reserved bits */
27 #define UHCI_USBCMD_RUN         0x0001          /* RUN/STOP bit */
28 #define UHCI_USBCMD_HCRESET     0x0002          /* Host Controller reset */
29 #define UHCI_USBCMD_EGSM        0x0008          /* Global Suspend Mode */
30 #define UHCI_USBCMD_CONFIGURE   0x0040          /* Config Flag */
31 #define UHCI_USBINTR_RESUME     0x0002          /* Resume interrupt enable */
32
33 #define OHCI_CONTROL            0x04
34 #define OHCI_CMDSTATUS          0x08
35 #define OHCI_INTRSTATUS         0x0c
36 #define OHCI_INTRENABLE         0x10
37 #define OHCI_INTRDISABLE        0x14
38 #define OHCI_FMINTERVAL         0x34
39 #define OHCI_HCFS               (3 << 6)        /* hc functional state */
40 #define OHCI_HCR                (1 << 0)        /* host controller reset */
41 #define OHCI_OCR                (1 << 3)        /* ownership change request */
42 #define OHCI_CTRL_RWC           (1 << 9)        /* remote wakeup connected */
43 #define OHCI_CTRL_IR            (1 << 8)        /* interrupt routing */
44 #define OHCI_INTR_OC            (1 << 30)       /* ownership change */
45
46 #define EHCI_HCC_PARAMS         0x08            /* extended capabilities */
47 #define EHCI_USBCMD             0               /* command register */
48 #define EHCI_USBCMD_RUN         (1 << 0)        /* RUN/STOP bit */
49 #define EHCI_USBSTS             4               /* status register */
50 #define EHCI_USBSTS_HALTED      (1 << 12)       /* HCHalted bit */
51 #define EHCI_USBINTR            8               /* interrupt register */
52 #define EHCI_CONFIGFLAG         0x40            /* configured flag register */
53 #define EHCI_USBLEGSUP          0               /* legacy support register */
54 #define EHCI_USBLEGSUP_BIOS     (1 << 16)       /* BIOS semaphore */
55 #define EHCI_USBLEGSUP_OS       (1 << 24)       /* OS semaphore */
56 #define EHCI_USBLEGCTLSTS       4               /* legacy control/status */
57 #define EHCI_USBLEGCTLSTS_SOOE  (1 << 13)       /* SMI on ownership change */
58
59 /* AMD quirk use */
60 #define AB_REG_BAR_LOW          0xe0
61 #define AB_REG_BAR_HIGH         0xe1
62 #define AB_REG_BAR_SB700        0xf0
63 #define AB_INDX(addr)           ((addr) + 0x00)
64 #define AB_DATA(addr)           ((addr) + 0x04)
65 #define AX_INDXC                0x30
66 #define AX_DATAC                0x34
67
68 #define PT_ADDR_INDX            0xE8
69 #define PT_READ_INDX            0xE4
70 #define PT_SIG_1_ADDR           0xA520
71 #define PT_SIG_2_ADDR           0xA521
72 #define PT_SIG_3_ADDR           0xA522
73 #define PT_SIG_4_ADDR           0xA523
74 #define PT_SIG_1_DATA           0x78
75 #define PT_SIG_2_DATA           0x56
76 #define PT_SIG_3_DATA           0x34
77 #define PT_SIG_4_DATA           0x12
78 #define PT4_P1_REG              0xB521
79 #define PT4_P2_REG              0xB522
80 #define PT2_P1_REG              0xD520
81 #define PT2_P2_REG              0xD521
82 #define PT1_P1_REG              0xD522
83 #define PT1_P2_REG              0xD523
84
85 #define NB_PCIE_INDX_ADDR       0xe0
86 #define NB_PCIE_INDX_DATA       0xe4
87 #define PCIE_P_CNTL             0x10040
88 #define BIF_NB                  0x10002
89 #define NB_PIF0_PWRDOWN_0       0x01100012
90 #define NB_PIF0_PWRDOWN_1       0x01100013
91
92 #define USB_INTEL_XUSB2PR      0xD0
93 #define USB_INTEL_USB2PRM      0xD4
94 #define USB_INTEL_USB3_PSSEN   0xD8
95 #define USB_INTEL_USB3PRM      0xDC
96
97 /* ASMEDIA quirk use */
98 #define ASMT_DATA_WRITE0_REG    0xF8
99 #define ASMT_DATA_WRITE1_REG    0xFC
100 #define ASMT_CONTROL_REG        0xE0
101 #define ASMT_CONTROL_WRITE_BIT  0x02
102 #define ASMT_WRITEREG_CMD       0x10423
103 #define ASMT_FLOWCTL_ADDR       0xFA30
104 #define ASMT_FLOWCTL_DATA       0xBA
105 #define ASMT_PSEUDO_DATA        0
106
107 /*
108  * amd_chipset_gen values represent AMD different chipset generations
109  */
110 enum amd_chipset_gen {
111         NOT_AMD_CHIPSET = 0,
112         AMD_CHIPSET_SB600,
113         AMD_CHIPSET_SB700,
114         AMD_CHIPSET_SB800,
115         AMD_CHIPSET_HUDSON2,
116         AMD_CHIPSET_BOLTON,
117         AMD_CHIPSET_YANGTZE,
118         AMD_CHIPSET_TAISHAN,
119         AMD_CHIPSET_UNKNOWN,
120 };
121
122 struct amd_chipset_type {
123         enum amd_chipset_gen gen;
124         u8 rev;
125 };
126
127 static struct amd_chipset_info {
128         struct pci_dev  *nb_dev;
129         struct pci_dev  *smbus_dev;
130         int nb_type;
131         struct amd_chipset_type sb_type;
132         int isoc_reqs;
133         int probe_count;
134         int probe_result;
135 } amd_chipset;
136
137 static DEFINE_SPINLOCK(amd_lock);
138
139 /*
140  * amd_chipset_sb_type_init - initialize amd chipset southbridge type
141  *
142  * AMD FCH/SB generation and revision is identified by SMBus controller
143  * vendor, device and revision IDs.
144  *
145  * Returns: 1 if it is an AMD chipset, 0 otherwise.
146  */
147 static int amd_chipset_sb_type_init(struct amd_chipset_info *pinfo)
148 {
149         u8 rev = 0;
150         pinfo->sb_type.gen = AMD_CHIPSET_UNKNOWN;
151
152         pinfo->smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI,
153                         PCI_DEVICE_ID_ATI_SBX00_SMBUS, NULL);
154         if (pinfo->smbus_dev) {
155                 rev = pinfo->smbus_dev->revision;
156                 if (rev >= 0x10 && rev <= 0x1f)
157                         pinfo->sb_type.gen = AMD_CHIPSET_SB600;
158                 else if (rev >= 0x30 && rev <= 0x3f)
159                         pinfo->sb_type.gen = AMD_CHIPSET_SB700;
160                 else if (rev >= 0x40 && rev <= 0x4f)
161                         pinfo->sb_type.gen = AMD_CHIPSET_SB800;
162         } else {
163                 pinfo->smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
164                                 PCI_DEVICE_ID_AMD_HUDSON2_SMBUS, NULL);
165
166                 if (pinfo->smbus_dev) {
167                         rev = pinfo->smbus_dev->revision;
168                         if (rev >= 0x11 && rev <= 0x14)
169                                 pinfo->sb_type.gen = AMD_CHIPSET_HUDSON2;
170                         else if (rev >= 0x15 && rev <= 0x18)
171                                 pinfo->sb_type.gen = AMD_CHIPSET_BOLTON;
172                         else if (rev >= 0x39 && rev <= 0x3a)
173                                 pinfo->sb_type.gen = AMD_CHIPSET_YANGTZE;
174                 } else {
175                         pinfo->smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
176                                                           0x145c, NULL);
177                         if (pinfo->smbus_dev) {
178                                 rev = pinfo->smbus_dev->revision;
179                                 pinfo->sb_type.gen = AMD_CHIPSET_TAISHAN;
180                         } else {
181                                 pinfo->sb_type.gen = NOT_AMD_CHIPSET;
182                                 return 0;
183                         }
184                 }
185         }
186         pinfo->sb_type.rev = rev;
187         return 1;
188 }
189
190 void sb800_prefetch(struct device *dev, int on)
191 {
192         u16 misc;
193         struct pci_dev *pdev = to_pci_dev(dev);
194
195         pci_read_config_word(pdev, 0x50, &misc);
196         if (on == 0)
197                 pci_write_config_word(pdev, 0x50, misc & 0xfcff);
198         else
199                 pci_write_config_word(pdev, 0x50, misc | 0x0300);
200 }
201 EXPORT_SYMBOL_GPL(sb800_prefetch);
202
203 int usb_amd_find_chipset_info(void)
204 {
205         unsigned long flags;
206         struct amd_chipset_info info;
207         int need_pll_quirk = 0;
208
209         spin_lock_irqsave(&amd_lock, flags);
210
211         /* probe only once */
212         if (amd_chipset.probe_count > 0) {
213                 amd_chipset.probe_count++;
214                 spin_unlock_irqrestore(&amd_lock, flags);
215                 return amd_chipset.probe_result;
216         }
217         memset(&info, 0, sizeof(info));
218         spin_unlock_irqrestore(&amd_lock, flags);
219
220         if (!amd_chipset_sb_type_init(&info)) {
221                 goto commit;
222         }
223
224         switch (info.sb_type.gen) {
225         case AMD_CHIPSET_SB700:
226                 need_pll_quirk = info.sb_type.rev <= 0x3B;
227                 break;
228         case AMD_CHIPSET_SB800:
229         case AMD_CHIPSET_HUDSON2:
230         case AMD_CHIPSET_BOLTON:
231                 need_pll_quirk = 1;
232                 break;
233         default:
234                 need_pll_quirk = 0;
235                 break;
236         }
237
238         if (!need_pll_quirk) {
239                 if (info.smbus_dev) {
240                         pci_dev_put(info.smbus_dev);
241                         info.smbus_dev = NULL;
242                 }
243                 goto commit;
244         }
245
246         info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
247         if (info.nb_dev) {
248                 info.nb_type = 1;
249         } else {
250                 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
251                 if (info.nb_dev) {
252                         info.nb_type = 2;
253                 } else {
254                         info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
255                                                      0x9600, NULL);
256                         if (info.nb_dev)
257                                 info.nb_type = 3;
258                 }
259         }
260
261         need_pll_quirk = info.probe_result = 1;
262         printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
263
264 commit:
265
266         spin_lock_irqsave(&amd_lock, flags);
267         if (amd_chipset.probe_count > 0) {
268                 /* race - someone else was faster - drop devices */
269
270                 /* Mark that we where here */
271                 amd_chipset.probe_count++;
272                 need_pll_quirk = amd_chipset.probe_result;
273
274                 spin_unlock_irqrestore(&amd_lock, flags);
275
276                 pci_dev_put(info.nb_dev);
277                 pci_dev_put(info.smbus_dev);
278
279         } else {
280                 /* no race - commit the result */
281                 info.probe_count++;
282                 amd_chipset = info;
283                 spin_unlock_irqrestore(&amd_lock, flags);
284         }
285
286         return need_pll_quirk;
287 }
288 EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
289
290 int usb_hcd_amd_remote_wakeup_quirk(struct pci_dev *pdev)
291 {
292         /* Make sure amd chipset type has already been initialized */
293         usb_amd_find_chipset_info();
294         if (amd_chipset.sb_type.gen == AMD_CHIPSET_YANGTZE ||
295             amd_chipset.sb_type.gen == AMD_CHIPSET_TAISHAN) {
296                 dev_dbg(&pdev->dev, "QUIRK: Enable AMD remote wakeup fix\n");
297                 return 1;
298         }
299         return 0;
300 }
301 EXPORT_SYMBOL_GPL(usb_hcd_amd_remote_wakeup_quirk);
302
303 bool usb_amd_hang_symptom_quirk(void)
304 {
305         u8 rev;
306
307         usb_amd_find_chipset_info();
308         rev = amd_chipset.sb_type.rev;
309         /* SB600 and old version of SB700 have hang symptom bug */
310         return amd_chipset.sb_type.gen == AMD_CHIPSET_SB600 ||
311                         (amd_chipset.sb_type.gen == AMD_CHIPSET_SB700 &&
312                          rev >= 0x3a && rev <= 0x3b);
313 }
314 EXPORT_SYMBOL_GPL(usb_amd_hang_symptom_quirk);
315
316 bool usb_amd_prefetch_quirk(void)
317 {
318         usb_amd_find_chipset_info();
319         /* SB800 needs pre-fetch fix */
320         return amd_chipset.sb_type.gen == AMD_CHIPSET_SB800;
321 }
322 EXPORT_SYMBOL_GPL(usb_amd_prefetch_quirk);
323
324 /*
325  * The hardware normally enables the A-link power management feature, which
326  * lets the system lower the power consumption in idle states.
327  *
328  * This USB quirk prevents the link going into that lower power state
329  * during isochronous transfers.
330  *
331  * Without this quirk, isochronous stream on OHCI/EHCI/xHCI controllers of
332  * some AMD platforms may stutter or have breaks occasionally.
333  */
334 static void usb_amd_quirk_pll(int disable)
335 {
336         u32 addr, addr_low, addr_high, val;
337         u32 bit = disable ? 0 : 1;
338         unsigned long flags;
339
340         spin_lock_irqsave(&amd_lock, flags);
341
342         if (disable) {
343                 amd_chipset.isoc_reqs++;
344                 if (amd_chipset.isoc_reqs > 1) {
345                         spin_unlock_irqrestore(&amd_lock, flags);
346                         return;
347                 }
348         } else {
349                 amd_chipset.isoc_reqs--;
350                 if (amd_chipset.isoc_reqs > 0) {
351                         spin_unlock_irqrestore(&amd_lock, flags);
352                         return;
353                 }
354         }
355
356         if (amd_chipset.sb_type.gen == AMD_CHIPSET_SB800 ||
357                         amd_chipset.sb_type.gen == AMD_CHIPSET_HUDSON2 ||
358                         amd_chipset.sb_type.gen == AMD_CHIPSET_BOLTON) {
359                 outb_p(AB_REG_BAR_LOW, 0xcd6);
360                 addr_low = inb_p(0xcd7);
361                 outb_p(AB_REG_BAR_HIGH, 0xcd6);
362                 addr_high = inb_p(0xcd7);
363                 addr = addr_high << 8 | addr_low;
364
365                 outl_p(0x30, AB_INDX(addr));
366                 outl_p(0x40, AB_DATA(addr));
367                 outl_p(0x34, AB_INDX(addr));
368                 val = inl_p(AB_DATA(addr));
369         } else if (amd_chipset.sb_type.gen == AMD_CHIPSET_SB700 &&
370                         amd_chipset.sb_type.rev <= 0x3b) {
371                 pci_read_config_dword(amd_chipset.smbus_dev,
372                                         AB_REG_BAR_SB700, &addr);
373                 outl(AX_INDXC, AB_INDX(addr));
374                 outl(0x40, AB_DATA(addr));
375                 outl(AX_DATAC, AB_INDX(addr));
376                 val = inl(AB_DATA(addr));
377         } else {
378                 spin_unlock_irqrestore(&amd_lock, flags);
379                 return;
380         }
381
382         if (disable) {
383                 val &= ~0x08;
384                 val |= (1 << 4) | (1 << 9);
385         } else {
386                 val |= 0x08;
387                 val &= ~((1 << 4) | (1 << 9));
388         }
389         outl_p(val, AB_DATA(addr));
390
391         if (!amd_chipset.nb_dev) {
392                 spin_unlock_irqrestore(&amd_lock, flags);
393                 return;
394         }
395
396         if (amd_chipset.nb_type == 1 || amd_chipset.nb_type == 3) {
397                 addr = PCIE_P_CNTL;
398                 pci_write_config_dword(amd_chipset.nb_dev,
399                                         NB_PCIE_INDX_ADDR, addr);
400                 pci_read_config_dword(amd_chipset.nb_dev,
401                                         NB_PCIE_INDX_DATA, &val);
402
403                 val &= ~(1 | (1 << 3) | (1 << 4) | (1 << 9) | (1 << 12));
404                 val |= bit | (bit << 3) | (bit << 12);
405                 val |= ((!bit) << 4) | ((!bit) << 9);
406                 pci_write_config_dword(amd_chipset.nb_dev,
407                                         NB_PCIE_INDX_DATA, val);
408
409                 addr = BIF_NB;
410                 pci_write_config_dword(amd_chipset.nb_dev,
411                                         NB_PCIE_INDX_ADDR, addr);
412                 pci_read_config_dword(amd_chipset.nb_dev,
413                                         NB_PCIE_INDX_DATA, &val);
414                 val &= ~(1 << 8);
415                 val |= bit << 8;
416
417                 pci_write_config_dword(amd_chipset.nb_dev,
418                                         NB_PCIE_INDX_DATA, val);
419         } else if (amd_chipset.nb_type == 2) {
420                 addr = NB_PIF0_PWRDOWN_0;
421                 pci_write_config_dword(amd_chipset.nb_dev,
422                                         NB_PCIE_INDX_ADDR, addr);
423                 pci_read_config_dword(amd_chipset.nb_dev,
424                                         NB_PCIE_INDX_DATA, &val);
425                 if (disable)
426                         val &= ~(0x3f << 7);
427                 else
428                         val |= 0x3f << 7;
429
430                 pci_write_config_dword(amd_chipset.nb_dev,
431                                         NB_PCIE_INDX_DATA, val);
432
433                 addr = NB_PIF0_PWRDOWN_1;
434                 pci_write_config_dword(amd_chipset.nb_dev,
435                                         NB_PCIE_INDX_ADDR, addr);
436                 pci_read_config_dword(amd_chipset.nb_dev,
437                                         NB_PCIE_INDX_DATA, &val);
438                 if (disable)
439                         val &= ~(0x3f << 7);
440                 else
441                         val |= 0x3f << 7;
442
443                 pci_write_config_dword(amd_chipset.nb_dev,
444                                         NB_PCIE_INDX_DATA, val);
445         }
446
447         spin_unlock_irqrestore(&amd_lock, flags);
448         return;
449 }
450
451 void usb_amd_quirk_pll_disable(void)
452 {
453         usb_amd_quirk_pll(1);
454 }
455 EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_disable);
456
457 static int usb_asmedia_wait_write(struct pci_dev *pdev)
458 {
459         unsigned long retry_count;
460         unsigned char value;
461
462         for (retry_count = 1000; retry_count > 0; --retry_count) {
463
464                 pci_read_config_byte(pdev, ASMT_CONTROL_REG, &value);
465
466                 if (value == 0xff) {
467                         dev_err(&pdev->dev, "%s: check_ready ERROR", __func__);
468                         return -EIO;
469                 }
470
471                 if ((value & ASMT_CONTROL_WRITE_BIT) == 0)
472                         return 0;
473
474                 udelay(50);
475         }
476
477         dev_warn(&pdev->dev, "%s: check_write_ready timeout", __func__);
478         return -ETIMEDOUT;
479 }
480
481 void usb_asmedia_modifyflowcontrol(struct pci_dev *pdev)
482 {
483         if (usb_asmedia_wait_write(pdev) != 0)
484                 return;
485
486         /* send command and address to device */
487         pci_write_config_dword(pdev, ASMT_DATA_WRITE0_REG, ASMT_WRITEREG_CMD);
488         pci_write_config_dword(pdev, ASMT_DATA_WRITE1_REG, ASMT_FLOWCTL_ADDR);
489         pci_write_config_byte(pdev, ASMT_CONTROL_REG, ASMT_CONTROL_WRITE_BIT);
490
491         if (usb_asmedia_wait_write(pdev) != 0)
492                 return;
493
494         /* send data to device */
495         pci_write_config_dword(pdev, ASMT_DATA_WRITE0_REG, ASMT_FLOWCTL_DATA);
496         pci_write_config_dword(pdev, ASMT_DATA_WRITE1_REG, ASMT_PSEUDO_DATA);
497         pci_write_config_byte(pdev, ASMT_CONTROL_REG, ASMT_CONTROL_WRITE_BIT);
498 }
499 EXPORT_SYMBOL_GPL(usb_asmedia_modifyflowcontrol);
500
501 void usb_amd_quirk_pll_enable(void)
502 {
503         usb_amd_quirk_pll(0);
504 }
505 EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
506
507 void usb_amd_dev_put(void)
508 {
509         struct pci_dev *nb, *smbus;
510         unsigned long flags;
511
512         spin_lock_irqsave(&amd_lock, flags);
513
514         amd_chipset.probe_count--;
515         if (amd_chipset.probe_count > 0) {
516                 spin_unlock_irqrestore(&amd_lock, flags);
517                 return;
518         }
519
520         /* save them to pci_dev_put outside of spinlock */
521         nb    = amd_chipset.nb_dev;
522         smbus = amd_chipset.smbus_dev;
523
524         amd_chipset.nb_dev = NULL;
525         amd_chipset.smbus_dev = NULL;
526         amd_chipset.nb_type = 0;
527         memset(&amd_chipset.sb_type, 0, sizeof(amd_chipset.sb_type));
528         amd_chipset.isoc_reqs = 0;
529         amd_chipset.probe_result = 0;
530
531         spin_unlock_irqrestore(&amd_lock, flags);
532
533         pci_dev_put(nb);
534         pci_dev_put(smbus);
535 }
536 EXPORT_SYMBOL_GPL(usb_amd_dev_put);
537
538 /*
539  * Check if port is disabled in BIOS on AMD Promontory host.
540  * BIOS Disabled ports may wake on connect/disconnect and need
541  * driver workaround to keep them disabled.
542  * Returns true if port is marked disabled.
543  */
544 bool usb_amd_pt_check_port(struct device *device, int port)
545 {
546         unsigned char value, port_shift;
547         struct pci_dev *pdev;
548         u16 reg;
549
550         pdev = to_pci_dev(device);
551         pci_write_config_word(pdev, PT_ADDR_INDX, PT_SIG_1_ADDR);
552
553         pci_read_config_byte(pdev, PT_READ_INDX, &value);
554         if (value != PT_SIG_1_DATA)
555                 return false;
556
557         pci_write_config_word(pdev, PT_ADDR_INDX, PT_SIG_2_ADDR);
558
559         pci_read_config_byte(pdev, PT_READ_INDX, &value);
560         if (value != PT_SIG_2_DATA)
561                 return false;
562
563         pci_write_config_word(pdev, PT_ADDR_INDX, PT_SIG_3_ADDR);
564
565         pci_read_config_byte(pdev, PT_READ_INDX, &value);
566         if (value != PT_SIG_3_DATA)
567                 return false;
568
569         pci_write_config_word(pdev, PT_ADDR_INDX, PT_SIG_4_ADDR);
570
571         pci_read_config_byte(pdev, PT_READ_INDX, &value);
572         if (value != PT_SIG_4_DATA)
573                 return false;
574
575         /* Check disabled port setting, if bit is set port is enabled */
576         switch (pdev->device) {
577         case 0x43b9:
578         case 0x43ba:
579         /*
580          * device is AMD_PROMONTORYA_4(0x43b9) or PROMONTORYA_3(0x43ba)
581          * PT4_P1_REG bits[7..1] represents USB2.0 ports 6 to 0
582          * PT4_P2_REG bits[6..0] represents ports 13 to 7
583          */
584                 if (port > 6) {
585                         reg = PT4_P2_REG;
586                         port_shift = port - 7;
587                 } else {
588                         reg = PT4_P1_REG;
589                         port_shift = port + 1;
590                 }
591                 break;
592         case 0x43bb:
593         /*
594          * device is AMD_PROMONTORYA_2(0x43bb)
595          * PT2_P1_REG bits[7..5] represents USB2.0 ports 2 to 0
596          * PT2_P2_REG bits[5..0] represents ports 9 to 3
597          */
598                 if (port > 2) {
599                         reg = PT2_P2_REG;
600                         port_shift = port - 3;
601                 } else {
602                         reg = PT2_P1_REG;
603                         port_shift = port + 5;
604                 }
605                 break;
606         case 0x43bc:
607         /*
608          * device is AMD_PROMONTORYA_1(0x43bc)
609          * PT1_P1_REG[7..4] represents USB2.0 ports 3 to 0
610          * PT1_P2_REG[5..0] represents ports 9 to 4
611          */
612                 if (port > 3) {
613                         reg = PT1_P2_REG;
614                         port_shift = port - 4;
615                 } else {
616                         reg = PT1_P1_REG;
617                         port_shift = port + 4;
618                 }
619                 break;
620         default:
621                 return false;
622         }
623         pci_write_config_word(pdev, PT_ADDR_INDX, reg);
624         pci_read_config_byte(pdev, PT_READ_INDX, &value);
625
626         return !(value & BIT(port_shift));
627 }
628 EXPORT_SYMBOL_GPL(usb_amd_pt_check_port);
629
630 /*
631  * Make sure the controller is completely inactive, unable to
632  * generate interrupts or do DMA.
633  */
634 void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
635 {
636         /* Turn off PIRQ enable and SMI enable.  (This also turns off the
637          * BIOS's USB Legacy Support.)  Turn off all the R/WC bits too.
638          */
639         pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
640
641         /* Reset the HC - this will force us to get a
642          * new notification of any already connected
643          * ports due to the virtual disconnect that it
644          * implies.
645          */
646         outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
647         mb();
648         udelay(5);
649         if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
650                 dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
651
652         /* Just to be safe, disable interrupt requests and
653          * make sure the controller is stopped.
654          */
655         outw(0, base + UHCI_USBINTR);
656         outw(0, base + UHCI_USBCMD);
657 }
658 EXPORT_SYMBOL_GPL(uhci_reset_hc);
659
660 /*
661  * Initialize a controller that was newly discovered or has just been
662  * resumed.  In either case we can't be sure of its previous state.
663  *
664  * Returns: 1 if the controller was reset, 0 otherwise.
665  */
666 int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
667 {
668         u16 legsup;
669         unsigned int cmd, intr;
670
671         /*
672          * When restarting a suspended controller, we expect all the
673          * settings to be the same as we left them:
674          *
675          *      PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
676          *      Controller is stopped and configured with EGSM set;
677          *      No interrupts enabled except possibly Resume Detect.
678          *
679          * If any of these conditions are violated we do a complete reset.
680          */
681         pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
682         if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
683                 dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
684                                 __func__, legsup);
685                 goto reset_needed;
686         }
687
688         cmd = inw(base + UHCI_USBCMD);
689         if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
690                         !(cmd & UHCI_USBCMD_EGSM)) {
691                 dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
692                                 __func__, cmd);
693                 goto reset_needed;
694         }
695
696         intr = inw(base + UHCI_USBINTR);
697         if (intr & (~UHCI_USBINTR_RESUME)) {
698                 dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
699                                 __func__, intr);
700                 goto reset_needed;
701         }
702         return 0;
703
704 reset_needed:
705         dev_dbg(&pdev->dev, "Performing full reset\n");
706         uhci_reset_hc(pdev, base);
707         return 1;
708 }
709 EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
710
711 static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
712 {
713         u16 cmd;
714         return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
715 }
716
717 #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
718 #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
719
720 static void quirk_usb_handoff_uhci(struct pci_dev *pdev)
721 {
722         unsigned long base = 0;
723         int i;
724
725         if (!pio_enabled(pdev))
726                 return;
727
728         for (i = 0; i < PCI_ROM_RESOURCE; i++)
729                 if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
730                         base = pci_resource_start(pdev, i);
731                         break;
732                 }
733
734         if (base)
735                 uhci_check_and_reset_hc(pdev, base);
736 }
737
738 static int mmio_resource_enabled(struct pci_dev *pdev, int idx)
739 {
740         return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
741 }
742
743 static void quirk_usb_handoff_ohci(struct pci_dev *pdev)
744 {
745         void __iomem *base;
746         u32 control;
747         u32 fminterval = 0;
748         bool no_fminterval = false;
749         int cnt;
750
751         if (!mmio_resource_enabled(pdev, 0))
752                 return;
753
754         base = pci_ioremap_bar(pdev, 0);
755         if (base == NULL)
756                 return;
757
758         /*
759          * ULi M5237 OHCI controller locks the whole system when accessing
760          * the OHCI_FMINTERVAL offset.
761          */
762         if (pdev->vendor == PCI_VENDOR_ID_AL && pdev->device == 0x5237)
763                 no_fminterval = true;
764
765         control = readl(base + OHCI_CONTROL);
766
767 /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
768 #ifdef __hppa__
769 #define OHCI_CTRL_MASK          (OHCI_CTRL_RWC | OHCI_CTRL_IR)
770 #else
771 #define OHCI_CTRL_MASK          OHCI_CTRL_RWC
772
773         if (control & OHCI_CTRL_IR) {
774                 int wait_time = 500; /* arbitrary; 5 seconds */
775                 writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
776                 writel(OHCI_OCR, base + OHCI_CMDSTATUS);
777                 while (wait_time > 0 &&
778                                 readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
779                         wait_time -= 10;
780                         msleep(10);
781                 }
782                 if (wait_time <= 0)
783                         dev_warn(&pdev->dev,
784                                  "OHCI: BIOS handoff failed (BIOS bug?) %08x\n",
785                                  readl(base + OHCI_CONTROL));
786         }
787 #endif
788
789         /* disable interrupts */
790         writel((u32) ~0, base + OHCI_INTRDISABLE);
791
792         /* Reset the USB bus, if the controller isn't already in RESET */
793         if (control & OHCI_HCFS) {
794                 /* Go into RESET, preserving RWC (and possibly IR) */
795                 writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
796                 readl(base + OHCI_CONTROL);
797
798                 /* drive bus reset for at least 50 ms (7.1.7.5) */
799                 msleep(50);
800         }
801
802         /* software reset of the controller, preserving HcFmInterval */
803         if (!no_fminterval)
804                 fminterval = readl(base + OHCI_FMINTERVAL);
805
806         writel(OHCI_HCR, base + OHCI_CMDSTATUS);
807
808         /* reset requires max 10 us delay */
809         for (cnt = 30; cnt > 0; --cnt) {        /* ... allow extra time */
810                 if ((readl(base + OHCI_CMDSTATUS) & OHCI_HCR) == 0)
811                         break;
812                 udelay(1);
813         }
814
815         if (!no_fminterval)
816                 writel(fminterval, base + OHCI_FMINTERVAL);
817
818         /* Now the controller is safely in SUSPEND and nothing can wake it up */
819         iounmap(base);
820 }
821
822 static const struct dmi_system_id ehci_dmi_nohandoff_table[] = {
823         {
824                 /*  Pegatron Lucid (ExoPC) */
825                 .matches = {
826                         DMI_MATCH(DMI_BOARD_NAME, "EXOPG06411"),
827                         DMI_MATCH(DMI_BIOS_VERSION, "Lucid-CE-133"),
828                 },
829         },
830         {
831                 /*  Pegatron Lucid (Ordissimo AIRIS) */
832                 .matches = {
833                         DMI_MATCH(DMI_BOARD_NAME, "M11JB"),
834                         DMI_MATCH(DMI_BIOS_VERSION, "Lucid-"),
835                 },
836         },
837         {
838                 /*  Pegatron Lucid (Ordissimo) */
839                 .matches = {
840                         DMI_MATCH(DMI_BOARD_NAME, "Ordissimo"),
841                         DMI_MATCH(DMI_BIOS_VERSION, "Lucid-"),
842                 },
843         },
844         {
845                 /* HASEE E200 */
846                 .matches = {
847                         DMI_MATCH(DMI_BOARD_VENDOR, "HASEE"),
848                         DMI_MATCH(DMI_BOARD_NAME, "E210"),
849                         DMI_MATCH(DMI_BIOS_VERSION, "6.00"),
850                 },
851         },
852         { }
853 };
854
855 static void ehci_bios_handoff(struct pci_dev *pdev,
856                                         void __iomem *op_reg_base,
857                                         u32 cap, u8 offset)
858 {
859         int try_handoff = 1, tried_handoff = 0;
860
861         /*
862          * The Pegatron Lucid tablet sporadically waits for 98 seconds trying
863          * the handoff on its unused controller.  Skip it.
864          *
865          * The HASEE E200 hangs when the semaphore is set (bugzilla #77021).
866          */
867         if (pdev->vendor == 0x8086 && (pdev->device == 0x283a ||
868                         pdev->device == 0x27cc)) {
869                 if (dmi_check_system(ehci_dmi_nohandoff_table))
870                         try_handoff = 0;
871         }
872
873         if (try_handoff && (cap & EHCI_USBLEGSUP_BIOS)) {
874                 dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
875
876 #if 0
877 /* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
878  * but that seems dubious in general (the BIOS left it off intentionally)
879  * and is known to prevent some systems from booting.  so we won't do this
880  * unless maybe we can determine when we're on a system that needs SMI forced.
881  */
882                 /* BIOS workaround (?): be sure the pre-Linux code
883                  * receives the SMI
884                  */
885                 pci_read_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, &val);
886                 pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS,
887                                        val | EHCI_USBLEGCTLSTS_SOOE);
888 #endif
889
890                 /* some systems get upset if this semaphore is
891                  * set for any other reason than forcing a BIOS
892                  * handoff..
893                  */
894                 pci_write_config_byte(pdev, offset + 3, 1);
895         }
896
897         /* if boot firmware now owns EHCI, spin till it hands it over. */
898         if (try_handoff) {
899                 int msec = 1000;
900                 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
901                         tried_handoff = 1;
902                         msleep(10);
903                         msec -= 10;
904                         pci_read_config_dword(pdev, offset, &cap);
905                 }
906         }
907
908         if (cap & EHCI_USBLEGSUP_BIOS) {
909                 /* well, possibly buggy BIOS... try to shut it down,
910                  * and hope nothing goes too wrong
911                  */
912                 if (try_handoff)
913                         dev_warn(&pdev->dev,
914                                  "EHCI: BIOS handoff failed (BIOS bug?) %08x\n",
915                                  cap);
916                 pci_write_config_byte(pdev, offset + 2, 0);
917         }
918
919         /* just in case, always disable EHCI SMIs */
920         pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, 0);
921
922         /* If the BIOS ever owned the controller then we can't expect
923          * any power sessions to remain intact.
924          */
925         if (tried_handoff)
926                 writel(0, op_reg_base + EHCI_CONFIGFLAG);
927 }
928
929 static void quirk_usb_disable_ehci(struct pci_dev *pdev)
930 {
931         void __iomem *base, *op_reg_base;
932         u32     hcc_params, cap, val;
933         u8      offset, cap_length;
934         int     wait_time, count = 256/4;
935
936         if (!mmio_resource_enabled(pdev, 0))
937                 return;
938
939         base = pci_ioremap_bar(pdev, 0);
940         if (base == NULL)
941                 return;
942
943         cap_length = readb(base);
944         op_reg_base = base + cap_length;
945
946         /* EHCI 0.96 and later may have "extended capabilities"
947          * spec section 5.1 explains the bios handoff, e.g. for
948          * booting from USB disk or using a usb keyboard
949          */
950         hcc_params = readl(base + EHCI_HCC_PARAMS);
951         offset = (hcc_params >> 8) & 0xff;
952         while (offset && --count) {
953                 pci_read_config_dword(pdev, offset, &cap);
954
955                 switch (cap & 0xff) {
956                 case 1:
957                         ehci_bios_handoff(pdev, op_reg_base, cap, offset);
958                         break;
959                 case 0: /* Illegal reserved cap, set cap=0 so we exit */
960                         cap = 0; /* then fallthrough... */
961                 default:
962                         dev_warn(&pdev->dev,
963                                  "EHCI: unrecognized capability %02x\n",
964                                  cap & 0xff);
965                 }
966                 offset = (cap >> 8) & 0xff;
967         }
968         if (!count)
969                 dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
970
971         /*
972          * halt EHCI & disable its interrupts in any case
973          */
974         val = readl(op_reg_base + EHCI_USBSTS);
975         if ((val & EHCI_USBSTS_HALTED) == 0) {
976                 val = readl(op_reg_base + EHCI_USBCMD);
977                 val &= ~EHCI_USBCMD_RUN;
978                 writel(val, op_reg_base + EHCI_USBCMD);
979
980                 wait_time = 2000;
981                 do {
982                         writel(0x3f, op_reg_base + EHCI_USBSTS);
983                         udelay(100);
984                         wait_time -= 100;
985                         val = readl(op_reg_base + EHCI_USBSTS);
986                         if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
987                                 break;
988                         }
989                 } while (wait_time > 0);
990         }
991         writel(0, op_reg_base + EHCI_USBINTR);
992         writel(0x3f, op_reg_base + EHCI_USBSTS);
993
994         iounmap(base);
995 }
996
997 /*
998  * handshake - spin reading a register until handshake completes
999  * @ptr: address of hc register to be read
1000  * @mask: bits to look at in result of read
1001  * @done: value of those bits when handshake succeeds
1002  * @wait_usec: timeout in microseconds
1003  * @delay_usec: delay in microseconds to wait between polling
1004  *
1005  * Polls a register every delay_usec microseconds.
1006  * Returns 0 when the mask bits have the value done.
1007  * Returns -ETIMEDOUT if this condition is not true after
1008  * wait_usec microseconds have passed.
1009  */
1010 static int handshake(void __iomem *ptr, u32 mask, u32 done,
1011                 int wait_usec, int delay_usec)
1012 {
1013         u32     result;
1014
1015         do {
1016                 result = readl(ptr);
1017                 result &= mask;
1018                 if (result == done)
1019                         return 0;
1020                 udelay(delay_usec);
1021                 wait_usec -= delay_usec;
1022         } while (wait_usec > 0);
1023         return -ETIMEDOUT;
1024 }
1025
1026 /*
1027  * Intel's Panther Point chipset has two host controllers (EHCI and xHCI) that
1028  * share some number of ports.  These ports can be switched between either
1029  * controller.  Not all of the ports under the EHCI host controller may be
1030  * switchable.
1031  *
1032  * The ports should be switched over to xHCI before PCI probes for any device
1033  * start.  This avoids active devices under EHCI being disconnected during the
1034  * port switchover, which could cause loss of data on USB storage devices, or
1035  * failed boot when the root file system is on a USB mass storage device and is
1036  * enumerated under EHCI first.
1037  *
1038  * We write into the xHC's PCI configuration space in some Intel-specific
1039  * registers to switch the ports over.  The USB 3.0 terminations and the USB
1040  * 2.0 data wires are switched separately.  We want to enable the SuperSpeed
1041  * terminations before switching the USB 2.0 wires over, so that USB 3.0
1042  * devices connect at SuperSpeed, rather than at USB 2.0 speeds.
1043  */
1044 void usb_enable_intel_xhci_ports(struct pci_dev *xhci_pdev)
1045 {
1046         u32             ports_available;
1047         bool            ehci_found = false;
1048         struct pci_dev  *companion = NULL;
1049
1050         /* Sony VAIO t-series with subsystem device ID 90a8 is not capable of
1051          * switching ports from EHCI to xHCI
1052          */
1053         if (xhci_pdev->subsystem_vendor == PCI_VENDOR_ID_SONY &&
1054             xhci_pdev->subsystem_device == 0x90a8)
1055                 return;
1056
1057         /* make sure an intel EHCI controller exists */
1058         for_each_pci_dev(companion) {
1059                 if (companion->class == PCI_CLASS_SERIAL_USB_EHCI &&
1060                     companion->vendor == PCI_VENDOR_ID_INTEL) {
1061                         ehci_found = true;
1062                         break;
1063                 }
1064         }
1065
1066         if (!ehci_found)
1067                 return;
1068
1069         /* Don't switchover the ports if the user hasn't compiled the xHCI
1070          * driver.  Otherwise they will see "dead" USB ports that don't power
1071          * the devices.
1072          */
1073         if (!IS_ENABLED(CONFIG_USB_XHCI_HCD)) {
1074                 dev_warn(&xhci_pdev->dev,
1075                          "CONFIG_USB_XHCI_HCD is turned off, defaulting to EHCI.\n");
1076                 dev_warn(&xhci_pdev->dev,
1077                                 "USB 3.0 devices will work at USB 2.0 speeds.\n");
1078                 usb_disable_xhci_ports(xhci_pdev);
1079                 return;
1080         }
1081
1082         /* Read USB3PRM, the USB 3.0 Port Routing Mask Register
1083          * Indicate the ports that can be changed from OS.
1084          */
1085         pci_read_config_dword(xhci_pdev, USB_INTEL_USB3PRM,
1086                         &ports_available);
1087
1088         dev_dbg(&xhci_pdev->dev, "Configurable ports to enable SuperSpeed: 0x%x\n",
1089                         ports_available);
1090
1091         /* Write USB3_PSSEN, the USB 3.0 Port SuperSpeed Enable
1092          * Register, to turn on SuperSpeed terminations for the
1093          * switchable ports.
1094          */
1095         pci_write_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
1096                         ports_available);
1097
1098         pci_read_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
1099                         &ports_available);
1100         dev_dbg(&xhci_pdev->dev,
1101                 "USB 3.0 ports that are now enabled under xHCI: 0x%x\n",
1102                 ports_available);
1103
1104         /* Read XUSB2PRM, xHCI USB 2.0 Port Routing Mask Register
1105          * Indicate the USB 2.0 ports to be controlled by the xHCI host.
1106          */
1107
1108         pci_read_config_dword(xhci_pdev, USB_INTEL_USB2PRM,
1109                         &ports_available);
1110
1111         dev_dbg(&xhci_pdev->dev, "Configurable USB 2.0 ports to hand over to xCHI: 0x%x\n",
1112                         ports_available);
1113
1114         /* Write XUSB2PR, the xHC USB 2.0 Port Routing Register, to
1115          * switch the USB 2.0 power and data lines over to the xHCI
1116          * host.
1117          */
1118         pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1119                         ports_available);
1120
1121         pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1122                         &ports_available);
1123         dev_dbg(&xhci_pdev->dev,
1124                 "USB 2.0 ports that are now switched over to xHCI: 0x%x\n",
1125                 ports_available);
1126 }
1127 EXPORT_SYMBOL_GPL(usb_enable_intel_xhci_ports);
1128
1129 void usb_disable_xhci_ports(struct pci_dev *xhci_pdev)
1130 {
1131         pci_write_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN, 0x0);
1132         pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, 0x0);
1133 }
1134 EXPORT_SYMBOL_GPL(usb_disable_xhci_ports);
1135
1136 /**
1137  * PCI Quirks for xHCI.
1138  *
1139  * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
1140  * It signals to the BIOS that the OS wants control of the host controller,
1141  * and then waits 1 second for the BIOS to hand over control.
1142  * If we timeout, assume the BIOS is broken and take control anyway.
1143  */
1144 static void quirk_usb_handoff_xhci(struct pci_dev *pdev)
1145 {
1146         void __iomem *base;
1147         int ext_cap_offset;
1148         void __iomem *op_reg_base;
1149         u32 val;
1150         int timeout;
1151         int len = pci_resource_len(pdev, 0);
1152
1153         if (!mmio_resource_enabled(pdev, 0))
1154                 return;
1155
1156         base = ioremap_nocache(pci_resource_start(pdev, 0), len);
1157         if (base == NULL)
1158                 return;
1159
1160         /*
1161          * Find the Legacy Support Capability register -
1162          * this is optional for xHCI host controllers.
1163          */
1164         ext_cap_offset = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_LEGACY);
1165
1166         if (!ext_cap_offset)
1167                 goto hc_init;
1168
1169         if ((ext_cap_offset + sizeof(val)) > len) {
1170                 /* We're reading garbage from the controller */
1171                 dev_warn(&pdev->dev, "xHCI controller failing to respond");
1172                 goto iounmap;
1173         }
1174         val = readl(base + ext_cap_offset);
1175
1176         /* Auto handoff never worked for these devices. Force it and continue */
1177         if ((pdev->vendor == PCI_VENDOR_ID_TI && pdev->device == 0x8241) ||
1178                         (pdev->vendor == PCI_VENDOR_ID_RENESAS
1179                          && pdev->device == 0x0014)) {
1180                 val = (val | XHCI_HC_OS_OWNED) & ~XHCI_HC_BIOS_OWNED;
1181                 writel(val, base + ext_cap_offset);
1182         }
1183
1184         /* If the BIOS owns the HC, signal that the OS wants it, and wait */
1185         if (val & XHCI_HC_BIOS_OWNED) {
1186                 writel(val | XHCI_HC_OS_OWNED, base + ext_cap_offset);
1187
1188                 /* Wait for 1 second with 10 microsecond polling interval */
1189                 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
1190                                 0, 1000000, 10);
1191
1192                 /* Assume a buggy BIOS and take HC ownership anyway */
1193                 if (timeout) {
1194                         dev_warn(&pdev->dev,
1195                                  "xHCI BIOS handoff failed (BIOS bug ?) %08x\n",
1196                                  val);
1197                         writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
1198                 }
1199         }
1200
1201         val = readl(base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
1202         /* Mask off (turn off) any enabled SMIs */
1203         val &= XHCI_LEGACY_DISABLE_SMI;
1204         /* Mask all SMI events bits, RW1C */
1205         val |= XHCI_LEGACY_SMI_EVENTS;
1206         /* Disable any BIOS SMIs and clear all SMI events*/
1207         writel(val, base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
1208
1209 hc_init:
1210         if (pdev->vendor == PCI_VENDOR_ID_INTEL)
1211                 usb_enable_intel_xhci_ports(pdev);
1212
1213         op_reg_base = base + XHCI_HC_LENGTH(readl(base));
1214
1215         /* Wait for the host controller to be ready before writing any
1216          * operational or runtime registers.  Wait 5 seconds and no more.
1217          */
1218         timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
1219                         5000000, 10);
1220         /* Assume a buggy HC and start HC initialization anyway */
1221         if (timeout) {
1222                 val = readl(op_reg_base + XHCI_STS_OFFSET);
1223                 dev_warn(&pdev->dev,
1224                          "xHCI HW not ready after 5 sec (HC bug?) status = 0x%x\n",
1225                          val);
1226         }
1227
1228         /* Send the halt and disable interrupts command */
1229         val = readl(op_reg_base + XHCI_CMD_OFFSET);
1230         val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
1231         writel(val, op_reg_base + XHCI_CMD_OFFSET);
1232
1233         /* Wait for the HC to halt - poll every 125 usec (one microframe). */
1234         timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
1235                         XHCI_MAX_HALT_USEC, 125);
1236         if (timeout) {
1237                 val = readl(op_reg_base + XHCI_STS_OFFSET);
1238                 dev_warn(&pdev->dev,
1239                          "xHCI HW did not halt within %d usec status = 0x%x\n",
1240                          XHCI_MAX_HALT_USEC, val);
1241         }
1242
1243 iounmap:
1244         iounmap(base);
1245 }
1246
1247 static void quirk_usb_early_handoff(struct pci_dev *pdev)
1248 {
1249         /* Skip Netlogic mips SoC's internal PCI USB controller.
1250          * This device does not need/support EHCI/OHCI handoff
1251          */
1252         if (pdev->vendor == 0x184e)     /* vendor Netlogic */
1253                 return;
1254         if (pdev->class != PCI_CLASS_SERIAL_USB_UHCI &&
1255                         pdev->class != PCI_CLASS_SERIAL_USB_OHCI &&
1256                         pdev->class != PCI_CLASS_SERIAL_USB_EHCI &&
1257                         pdev->class != PCI_CLASS_SERIAL_USB_XHCI)
1258                 return;
1259
1260         if (pci_enable_device(pdev) < 0) {
1261                 dev_warn(&pdev->dev,
1262                          "Can't enable PCI device, BIOS handoff failed.\n");
1263                 return;
1264         }
1265         if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
1266                 quirk_usb_handoff_uhci(pdev);
1267         else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
1268                 quirk_usb_handoff_ohci(pdev);
1269         else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
1270                 quirk_usb_disable_ehci(pdev);
1271         else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
1272                 quirk_usb_handoff_xhci(pdev);
1273         pci_disable_device(pdev);
1274 }
1275 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
1276                         PCI_CLASS_SERIAL_USB, 8, quirk_usb_early_handoff);
1277
1278 bool usb_xhci_needs_pci_reset(struct pci_dev *pdev)
1279 {
1280         /*
1281          * Our dear uPD72020{1,2} friend only partially resets when
1282          * asked to via the XHCI interface, and may end up doing DMA
1283          * at the wrong addresses, as it keeps the top 32bit of some
1284          * addresses from its previous programming under obscure
1285          * circumstances.
1286          * Give it a good wack at probe time. Unfortunately, this
1287          * needs to happen before we've had a chance to discover any
1288          * quirk, or the system will be in a rather bad state.
1289          */
1290         if (pdev->vendor == PCI_VENDOR_ID_RENESAS &&
1291             (pdev->device == 0x0014 || pdev->device == 0x0015))
1292                 return true;
1293
1294         return false;
1295 }
1296 EXPORT_SYMBOL_GPL(usb_xhci_needs_pci_reset);