blob: f16c59d5f48702cb4abb4c7bab1cc6e811ffda19 [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>
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>
Andy Ross3610ea52011-05-11 15:52:38 -070017#include <linux/dmi.h>
Adrian Bunk75e2df62006-03-25 18:01:53 +010018#include "pci-quirks.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070019#include "xhci-ext-caps.h"
David Brownell75862692005-09-23 17:14:37 -070020
21
David Brownell75862692005-09-23 17:14:37 -070022#define UHCI_USBLEGSUP 0xc0 /* legacy support */
23#define UHCI_USBCMD 0 /* command register */
David Brownell75862692005-09-23 17:14:37 -070024#define UHCI_USBINTR 4 /* interrupt register */
Alan Sternbb200f62005-10-03 16:36:29 -040025#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 */
David Brownell75862692005-09-23 17:14:37 -070032
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_OCR (1 << 3) /* ownership change request */
David Brownellf2cb36c2005-09-22 22:43:30 -070039#define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */
David Brownell75862692005-09-23 17:14:37 -070040#define OHCI_CTRL_IR (1 << 8) /* interrupt routing */
41#define OHCI_INTR_OC (1 << 30) /* ownership change */
42
43#define EHCI_HCC_PARAMS 0x08 /* extended capabilities */
44#define EHCI_USBCMD 0 /* command register */
45#define EHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
46#define EHCI_USBSTS 4 /* status register */
47#define EHCI_USBSTS_HALTED (1 << 12) /* HCHalted bit */
48#define EHCI_USBINTR 8 /* interrupt register */
Alan Stern4fe53542007-04-05 16:06:53 -040049#define EHCI_CONFIGFLAG 0x40 /* configured flag register */
David Brownell75862692005-09-23 17:14:37 -070050#define EHCI_USBLEGSUP 0 /* legacy support register */
51#define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
52#define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
53#define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
54#define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
55
Andiry Xuad935622011-03-01 14:57:05 +080056/* AMD quirk use */
57#define AB_REG_BAR_LOW 0xe0
58#define AB_REG_BAR_HIGH 0xe1
59#define AB_REG_BAR_SB700 0xf0
60#define AB_INDX(addr) ((addr) + 0x00)
61#define AB_DATA(addr) ((addr) + 0x04)
62#define AX_INDXC 0x30
63#define AX_DATAC 0x34
64
65#define NB_PCIE_INDX_ADDR 0xe0
66#define NB_PCIE_INDX_DATA 0xe4
67#define PCIE_P_CNTL 0x10040
68#define BIF_NB 0x10002
69#define NB_PIF0_PWRDOWN_0 0x01100012
70#define NB_PIF0_PWRDOWN_1 0x01100013
71
72static struct amd_chipset_info {
73 struct pci_dev *nb_dev;
74 struct pci_dev *smbus_dev;
75 int nb_type;
76 int sb_type;
77 int isoc_reqs;
78 int probe_count;
79 int probe_result;
80} amd_chipset;
81
82static DEFINE_SPINLOCK(amd_lock);
83
84int usb_amd_find_chipset_info(void)
85{
86 u8 rev = 0;
87 unsigned long flags;
Joerg Roedel9ab79272011-04-13 08:38:16 +020088 struct amd_chipset_info info;
89 int ret;
Andiry Xuad935622011-03-01 14:57:05 +080090
91 spin_lock_irqsave(&amd_lock, flags);
92
Andiry Xuad935622011-03-01 14:57:05 +080093 /* probe only once */
Joerg Roedel9ab79272011-04-13 08:38:16 +020094 if (amd_chipset.probe_count > 0) {
95 amd_chipset.probe_count++;
Andiry Xuad935622011-03-01 14:57:05 +080096 spin_unlock_irqrestore(&amd_lock, flags);
97 return amd_chipset.probe_result;
98 }
Joerg Roedel9ab79272011-04-13 08:38:16 +020099 memset(&info, 0, sizeof(info));
100 spin_unlock_irqrestore(&amd_lock, flags);
Andiry Xuad935622011-03-01 14:57:05 +0800101
Joerg Roedel9ab79272011-04-13 08:38:16 +0200102 info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
103 if (info.smbus_dev) {
104 rev = info.smbus_dev->revision;
Andiry Xuad935622011-03-01 14:57:05 +0800105 if (rev >= 0x40)
Joerg Roedel9ab79272011-04-13 08:38:16 +0200106 info.sb_type = 1;
Andiry Xuad935622011-03-01 14:57:05 +0800107 else if (rev >= 0x30 && rev <= 0x3b)
Joerg Roedel9ab79272011-04-13 08:38:16 +0200108 info.sb_type = 3;
Andiry Xuad935622011-03-01 14:57:05 +0800109 } else {
Joerg Roedel9ab79272011-04-13 08:38:16 +0200110 info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
111 0x780b, NULL);
112 if (!info.smbus_dev) {
113 ret = 0;
114 goto commit;
Andiry Xuad935622011-03-01 14:57:05 +0800115 }
Joerg Roedel9ab79272011-04-13 08:38:16 +0200116
117 rev = info.smbus_dev->revision;
Andiry Xuad935622011-03-01 14:57:05 +0800118 if (rev >= 0x11 && rev <= 0x18)
Joerg Roedel9ab79272011-04-13 08:38:16 +0200119 info.sb_type = 2;
Andiry Xuad935622011-03-01 14:57:05 +0800120 }
121
Joerg Roedel9ab79272011-04-13 08:38:16 +0200122 if (info.sb_type == 0) {
123 if (info.smbus_dev) {
124 pci_dev_put(info.smbus_dev);
125 info.smbus_dev = NULL;
Andiry Xuad935622011-03-01 14:57:05 +0800126 }
Joerg Roedel9ab79272011-04-13 08:38:16 +0200127 ret = 0;
128 goto commit;
Andiry Xuad935622011-03-01 14:57:05 +0800129 }
130
Joerg Roedel9ab79272011-04-13 08:38:16 +0200131 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
132 if (info.nb_dev) {
133 info.nb_type = 1;
Andiry Xuad935622011-03-01 14:57:05 +0800134 } else {
Joerg Roedel9ab79272011-04-13 08:38:16 +0200135 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
136 if (info.nb_dev) {
137 info.nb_type = 2;
138 } else {
139 info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
140 0x9600, NULL);
141 if (info.nb_dev)
142 info.nb_type = 3;
Andiry Xuad935622011-03-01 14:57:05 +0800143 }
144 }
145
Joerg Roedel9ab79272011-04-13 08:38:16 +0200146 ret = info.probe_result = 1;
Andiry Xuad935622011-03-01 14:57:05 +0800147 printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
148
Joerg Roedel9ab79272011-04-13 08:38:16 +0200149commit:
150
151 spin_lock_irqsave(&amd_lock, flags);
152 if (amd_chipset.probe_count > 0) {
153 /* race - someone else was faster - drop devices */
154
155 /* Mark that we where here */
156 amd_chipset.probe_count++;
157 ret = amd_chipset.probe_result;
158
159 spin_unlock_irqrestore(&amd_lock, flags);
160
161 if (info.nb_dev)
162 pci_dev_put(info.nb_dev);
163 if (info.smbus_dev)
164 pci_dev_put(info.smbus_dev);
165
166 } else {
167 /* no race - commit the result */
168 info.probe_count++;
169 amd_chipset = info;
170 spin_unlock_irqrestore(&amd_lock, flags);
171 }
172
173 return ret;
Andiry Xuad935622011-03-01 14:57:05 +0800174}
175EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
176
177/*
178 * The hardware normally enables the A-link power management feature, which
179 * lets the system lower the power consumption in idle states.
180 *
181 * This USB quirk prevents the link going into that lower power state
182 * during isochronous transfers.
183 *
184 * Without this quirk, isochronous stream on OHCI/EHCI/xHCI controllers of
185 * some AMD platforms may stutter or have breaks occasionally.
186 */
187static void usb_amd_quirk_pll(int disable)
188{
189 u32 addr, addr_low, addr_high, val;
190 u32 bit = disable ? 0 : 1;
191 unsigned long flags;
192
193 spin_lock_irqsave(&amd_lock, flags);
194
195 if (disable) {
196 amd_chipset.isoc_reqs++;
197 if (amd_chipset.isoc_reqs > 1) {
198 spin_unlock_irqrestore(&amd_lock, flags);
199 return;
200 }
201 } else {
202 amd_chipset.isoc_reqs--;
203 if (amd_chipset.isoc_reqs > 0) {
204 spin_unlock_irqrestore(&amd_lock, flags);
205 return;
206 }
207 }
208
209 if (amd_chipset.sb_type == 1 || amd_chipset.sb_type == 2) {
210 outb_p(AB_REG_BAR_LOW, 0xcd6);
211 addr_low = inb_p(0xcd7);
212 outb_p(AB_REG_BAR_HIGH, 0xcd6);
213 addr_high = inb_p(0xcd7);
214 addr = addr_high << 8 | addr_low;
215
216 outl_p(0x30, AB_INDX(addr));
217 outl_p(0x40, AB_DATA(addr));
218 outl_p(0x34, AB_INDX(addr));
219 val = inl_p(AB_DATA(addr));
220 } else if (amd_chipset.sb_type == 3) {
221 pci_read_config_dword(amd_chipset.smbus_dev,
222 AB_REG_BAR_SB700, &addr);
223 outl(AX_INDXC, AB_INDX(addr));
224 outl(0x40, AB_DATA(addr));
225 outl(AX_DATAC, AB_INDX(addr));
226 val = inl(AB_DATA(addr));
227 } else {
228 spin_unlock_irqrestore(&amd_lock, flags);
229 return;
230 }
231
232 if (disable) {
233 val &= ~0x08;
234 val |= (1 << 4) | (1 << 9);
235 } else {
236 val |= 0x08;
237 val &= ~((1 << 4) | (1 << 9));
238 }
239 outl_p(val, AB_DATA(addr));
240
241 if (!amd_chipset.nb_dev) {
242 spin_unlock_irqrestore(&amd_lock, flags);
243 return;
244 }
245
246 if (amd_chipset.nb_type == 1 || amd_chipset.nb_type == 3) {
247 addr = PCIE_P_CNTL;
248 pci_write_config_dword(amd_chipset.nb_dev,
249 NB_PCIE_INDX_ADDR, addr);
250 pci_read_config_dword(amd_chipset.nb_dev,
251 NB_PCIE_INDX_DATA, &val);
252
253 val &= ~(1 | (1 << 3) | (1 << 4) | (1 << 9) | (1 << 12));
254 val |= bit | (bit << 3) | (bit << 12);
255 val |= ((!bit) << 4) | ((!bit) << 9);
256 pci_write_config_dword(amd_chipset.nb_dev,
257 NB_PCIE_INDX_DATA, val);
258
259 addr = BIF_NB;
260 pci_write_config_dword(amd_chipset.nb_dev,
261 NB_PCIE_INDX_ADDR, addr);
262 pci_read_config_dword(amd_chipset.nb_dev,
263 NB_PCIE_INDX_DATA, &val);
264 val &= ~(1 << 8);
265 val |= bit << 8;
266
267 pci_write_config_dword(amd_chipset.nb_dev,
268 NB_PCIE_INDX_DATA, val);
269 } else if (amd_chipset.nb_type == 2) {
270 addr = NB_PIF0_PWRDOWN_0;
271 pci_write_config_dword(amd_chipset.nb_dev,
272 NB_PCIE_INDX_ADDR, addr);
273 pci_read_config_dword(amd_chipset.nb_dev,
274 NB_PCIE_INDX_DATA, &val);
275 if (disable)
276 val &= ~(0x3f << 7);
277 else
278 val |= 0x3f << 7;
279
280 pci_write_config_dword(amd_chipset.nb_dev,
281 NB_PCIE_INDX_DATA, val);
282
283 addr = NB_PIF0_PWRDOWN_1;
284 pci_write_config_dword(amd_chipset.nb_dev,
285 NB_PCIE_INDX_ADDR, addr);
286 pci_read_config_dword(amd_chipset.nb_dev,
287 NB_PCIE_INDX_DATA, &val);
288 if (disable)
289 val &= ~(0x3f << 7);
290 else
291 val |= 0x3f << 7;
292
293 pci_write_config_dword(amd_chipset.nb_dev,
294 NB_PCIE_INDX_DATA, val);
295 }
296
297 spin_unlock_irqrestore(&amd_lock, flags);
298 return;
299}
300
301void usb_amd_quirk_pll_disable(void)
302{
303 usb_amd_quirk_pll(1);
304}
305EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_disable);
306
307void usb_amd_quirk_pll_enable(void)
308{
309 usb_amd_quirk_pll(0);
310}
311EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
312
313void usb_amd_dev_put(void)
314{
Joerg Roedel9ab79272011-04-13 08:38:16 +0200315 struct pci_dev *nb, *smbus;
Andiry Xuad935622011-03-01 14:57:05 +0800316 unsigned long flags;
317
318 spin_lock_irqsave(&amd_lock, flags);
319
320 amd_chipset.probe_count--;
321 if (amd_chipset.probe_count > 0) {
322 spin_unlock_irqrestore(&amd_lock, flags);
323 return;
324 }
325
Joerg Roedel9ab79272011-04-13 08:38:16 +0200326 /* save them to pci_dev_put outside of spinlock */
327 nb = amd_chipset.nb_dev;
328 smbus = amd_chipset.smbus_dev;
329
330 amd_chipset.nb_dev = NULL;
331 amd_chipset.smbus_dev = NULL;
Andiry Xuad935622011-03-01 14:57:05 +0800332 amd_chipset.nb_type = 0;
333 amd_chipset.sb_type = 0;
334 amd_chipset.isoc_reqs = 0;
335 amd_chipset.probe_result = 0;
336
337 spin_unlock_irqrestore(&amd_lock, flags);
Joerg Roedel9ab79272011-04-13 08:38:16 +0200338
339 if (nb)
340 pci_dev_put(nb);
341 if (smbus)
342 pci_dev_put(smbus);
Andiry Xuad935622011-03-01 14:57:05 +0800343}
344EXPORT_SYMBOL_GPL(usb_amd_dev_put);
David Brownell75862692005-09-23 17:14:37 -0700345
Alan Sternbb200f62005-10-03 16:36:29 -0400346/*
347 * Make sure the controller is completely inactive, unable to
348 * generate interrupts or do DMA.
349 */
350void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
351{
352 /* Turn off PIRQ enable and SMI enable. (This also turns off the
353 * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
354 */
355 pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
356
357 /* Reset the HC - this will force us to get a
358 * new notification of any already connected
359 * ports due to the virtual disconnect that it
360 * implies.
361 */
362 outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
363 mb();
364 udelay(5);
365 if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
366 dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
367
368 /* Just to be safe, disable interrupt requests and
369 * make sure the controller is stopped.
370 */
371 outw(0, base + UHCI_USBINTR);
372 outw(0, base + UHCI_USBCMD);
373}
374EXPORT_SYMBOL_GPL(uhci_reset_hc);
375
376/*
377 * Initialize a controller that was newly discovered or has just been
378 * resumed. In either case we can't be sure of its previous state.
379 *
380 * Returns: 1 if the controller was reset, 0 otherwise.
381 */
382int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
383{
384 u16 legsup;
385 unsigned int cmd, intr;
386
387 /*
388 * When restarting a suspended controller, we expect all the
389 * settings to be the same as we left them:
390 *
391 * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
392 * Controller is stopped and configured with EGSM set;
393 * No interrupts enabled except possibly Resume Detect.
394 *
395 * If any of these conditions are violated we do a complete reset.
396 */
397 pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
398 if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
399 dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800400 __func__, legsup);
Alan Sternbb200f62005-10-03 16:36:29 -0400401 goto reset_needed;
402 }
403
404 cmd = inw(base + UHCI_USBCMD);
405 if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
406 !(cmd & UHCI_USBCMD_EGSM)) {
407 dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800408 __func__, cmd);
Alan Sternbb200f62005-10-03 16:36:29 -0400409 goto reset_needed;
410 }
411
412 intr = inw(base + UHCI_USBINTR);
413 if (intr & (~UHCI_USBINTR_RESUME)) {
414 dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800415 __func__, intr);
Alan Sternbb200f62005-10-03 16:36:29 -0400416 goto reset_needed;
417 }
418 return 0;
419
420reset_needed:
421 dev_dbg(&pdev->dev, "Performing full reset\n");
422 uhci_reset_hc(pdev, base);
423 return 1;
424}
425EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
426
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800427static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
428{
429 u16 cmd;
430 return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
431}
432
433#define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
434#define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
435
David Brownell75862692005-09-23 17:14:37 -0700436static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
437{
438 unsigned long base = 0;
David Brownell75862692005-09-23 17:14:37 -0700439 int i;
440
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800441 if (!pio_enabled(pdev))
442 return;
443
David Brownell75862692005-09-23 17:14:37 -0700444 for (i = 0; i < PCI_ROM_RESOURCE; i++)
445 if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
446 base = pci_resource_start(pdev, i);
447 break;
448 }
449
Alan Sternbb200f62005-10-03 16:36:29 -0400450 if (base)
451 uhci_check_and_reset_hc(pdev, base);
David Brownell75862692005-09-23 17:14:37 -0700452}
453
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800454static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
455{
456 return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
457}
458
David Brownell75862692005-09-23 17:14:37 -0700459static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
460{
461 void __iomem *base;
Alan Stern3df71692010-09-10 16:37:05 -0400462 u32 control;
David Brownell75862692005-09-23 17:14:37 -0700463
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800464 if (!mmio_resource_enabled(pdev, 0))
465 return;
466
Arjan van de Ven8e8ce4b2008-10-20 21:46:01 -0700467 base = pci_ioremap_bar(pdev, 0);
468 if (base == NULL)
469 return;
David Brownell75862692005-09-23 17:14:37 -0700470
Alan Stern3df71692010-09-10 16:37:05 -0400471 control = readl(base + OHCI_CONTROL);
472
David Brownellf2cb36c2005-09-22 22:43:30 -0700473/* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
Alan Stern3df71692010-09-10 16:37:05 -0400474#ifdef __hppa__
475#define OHCI_CTRL_MASK (OHCI_CTRL_RWC | OHCI_CTRL_IR)
476#else
477#define OHCI_CTRL_MASK OHCI_CTRL_RWC
478
David Brownellf2cb36c2005-09-22 22:43:30 -0700479 if (control & OHCI_CTRL_IR) {
Kyle McMartinc1b45f22006-06-25 18:45:29 -0400480 int wait_time = 500; /* arbitrary; 5 seconds */
David Brownell75862692005-09-23 17:14:37 -0700481 writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
482 writel(OHCI_OCR, base + OHCI_CMDSTATUS);
483 while (wait_time > 0 &&
484 readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
485 wait_time -= 10;
486 msleep(10);
487 }
David Brownellf2cb36c2005-09-22 22:43:30 -0700488 if (wait_time <= 0)
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700489 dev_warn(&pdev->dev, "OHCI: BIOS handoff failed"
490 " (BIOS bug?) %08x\n",
David Brownella38408c2006-02-09 16:35:31 -0500491 readl(base + OHCI_CONTROL));
David Brownell75862692005-09-23 17:14:37 -0700492 }
David Brownellf2cb36c2005-09-22 22:43:30 -0700493#endif
David Brownell75862692005-09-23 17:14:37 -0700494
Alan Stern3df71692010-09-10 16:37:05 -0400495 /* reset controller, preserving RWC (and possibly IR) */
496 writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
497
David Brownell75862692005-09-23 17:14:37 -0700498 /*
499 * disable interrupts
500 */
501 writel(~(u32)0, base + OHCI_INTRDISABLE);
502 writel(~(u32)0, base + OHCI_INTRSTATUS);
503
504 iounmap(base);
505}
506
Andy Ross5c853012011-05-11 15:15:51 -0700507static void __devinit ehci_bios_handoff(struct pci_dev *pdev,
508 void __iomem *op_reg_base,
509 u32 cap, u8 offset)
510{
Andy Ross3610ea52011-05-11 15:52:38 -0700511 int try_handoff = 1, tried_handoff = 0;
Andy Ross5c853012011-05-11 15:15:51 -0700512
Andy Ross3610ea52011-05-11 15:52:38 -0700513 /* The Pegatron Lucid (ExoPC) tablet sporadically waits for 90
514 * seconds trying the handoff on its unused controller. Skip
515 * it. */
516 if (pdev->vendor == 0x8086 && pdev->device == 0x283a) {
517 const char *dmi_bn = dmi_get_system_info(DMI_BOARD_NAME);
518 const char *dmi_bv = dmi_get_system_info(DMI_BIOS_VERSION);
519 if (dmi_bn && !strcmp(dmi_bn, "EXOPG06411") &&
520 dmi_bv && !strcmp(dmi_bv, "Lucid-CE-133"))
521 try_handoff = 0;
522 }
523
524 if (try_handoff && (cap & EHCI_USBLEGSUP_BIOS)) {
Andy Ross5c853012011-05-11 15:15:51 -0700525 dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
526
527#if 0
528/* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
529 * but that seems dubious in general (the BIOS left it off intentionally)
530 * and is known to prevent some systems from booting. so we won't do this
531 * unless maybe we can determine when we're on a system that needs SMI forced.
532 */
533 /* BIOS workaround (?): be sure the pre-Linux code
534 * receives the SMI
535 */
536 pci_read_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, &val);
537 pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS,
538 val | EHCI_USBLEGCTLSTS_SOOE);
539#endif
540
541 /* some systems get upset if this semaphore is
542 * set for any other reason than forcing a BIOS
543 * handoff..
544 */
545 pci_write_config_byte(pdev, offset + 3, 1);
546 }
547
548 /* if boot firmware now owns EHCI, spin till it hands it over. */
Andy Ross3610ea52011-05-11 15:52:38 -0700549 if (try_handoff) {
550 int msec = 1000;
551 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
552 tried_handoff = 1;
553 msleep(10);
554 msec -= 10;
555 pci_read_config_dword(pdev, offset, &cap);
556 }
Andy Ross5c853012011-05-11 15:15:51 -0700557 }
558
559 if (cap & EHCI_USBLEGSUP_BIOS) {
560 /* well, possibly buggy BIOS... try to shut it down,
561 * and hope nothing goes too wrong
562 */
Andy Ross3610ea52011-05-11 15:52:38 -0700563 if (try_handoff)
564 dev_warn(&pdev->dev, "EHCI: BIOS handoff failed"
565 " (BIOS bug?) %08x\n", cap);
Andy Ross5c853012011-05-11 15:15:51 -0700566 pci_write_config_byte(pdev, offset + 2, 0);
567 }
568
569 /* just in case, always disable EHCI SMIs */
570 pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, 0);
571
572 /* If the BIOS ever owned the controller then we can't expect
573 * any power sessions to remain intact.
574 */
575 if (tried_handoff)
576 writel(0, op_reg_base + EHCI_CONFIGFLAG);
577}
578
David Brownell75862692005-09-23 17:14:37 -0700579static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
580{
David Brownell75862692005-09-23 17:14:37 -0700581 void __iomem *base, *op_reg_base;
Andy Ross5c853012011-05-11 15:15:51 -0700582 u32 hcc_params, cap, val;
David Brownell401feaf2006-01-24 07:15:30 -0800583 u8 offset, cap_length;
Andy Ross5c853012011-05-11 15:15:51 -0700584 int wait_time, delta, count = 256/4;
David Brownell75862692005-09-23 17:14:37 -0700585
Linus Torvalds541ab4a2005-10-31 21:12:40 -0800586 if (!mmio_resource_enabled(pdev, 0))
587 return;
588
Arjan van de Ven8e8ce4b2008-10-20 21:46:01 -0700589 base = pci_ioremap_bar(pdev, 0);
590 if (base == NULL)
591 return;
David Brownell75862692005-09-23 17:14:37 -0700592
593 cap_length = readb(base);
594 op_reg_base = base + cap_length;
David Brownell75862692005-09-23 17:14:37 -0700595
David Brownell401feaf2006-01-24 07:15:30 -0800596 /* EHCI 0.96 and later may have "extended capabilities"
597 * spec section 5.1 explains the bios handoff, e.g. for
598 * booting from USB disk or using a usb keyboard
599 */
600 hcc_params = readl(base + EHCI_HCC_PARAMS);
601 offset = (hcc_params >> 8) & 0xff;
Roel Kluin6e14bda2009-01-31 12:37:04 +0100602 while (offset && --count) {
David Brownell401feaf2006-01-24 07:15:30 -0800603 pci_read_config_dword(pdev, offset, &cap);
Andy Ross5c853012011-05-11 15:15:51 -0700604
David Brownell401feaf2006-01-24 07:15:30 -0800605 switch (cap & 0xff) {
Andy Ross5c853012011-05-11 15:15:51 -0700606 case 1:
607 ehci_bios_handoff(pdev, op_reg_base, cap, offset);
David Brownell401feaf2006-01-24 07:15:30 -0800608 break;
Andy Ross5c853012011-05-11 15:15:51 -0700609 case 0: /* Illegal reserved cap, set cap=0 so we exit */
610 cap = 0; /* then fallthrough... */
David Brownell401feaf2006-01-24 07:15:30 -0800611 default:
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700612 dev_warn(&pdev->dev, "EHCI: unrecognized capability "
Andy Ross5c853012011-05-11 15:15:51 -0700613 "%02x\n", cap & 0xff);
David Brownell75862692005-09-23 17:14:37 -0700614 }
David Brownell401feaf2006-01-24 07:15:30 -0800615 offset = (cap >> 8) & 0xff;
David Brownell75862692005-09-23 17:14:37 -0700616 }
David Brownell401feaf2006-01-24 07:15:30 -0800617 if (!count)
bjorn.helgaas@hp.comf0fda802007-12-17 14:09:39 -0700618 dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
David Brownell75862692005-09-23 17:14:37 -0700619
620 /*
621 * halt EHCI & disable its interrupts in any case
622 */
623 val = readl(op_reg_base + EHCI_USBSTS);
624 if ((val & EHCI_USBSTS_HALTED) == 0) {
625 val = readl(op_reg_base + EHCI_USBCMD);
626 val &= ~EHCI_USBCMD_RUN;
627 writel(val, op_reg_base + EHCI_USBCMD);
628
629 wait_time = 2000;
630 delta = 100;
631 do {
632 writel(0x3f, op_reg_base + EHCI_USBSTS);
633 udelay(delta);
634 wait_time -= delta;
635 val = readl(op_reg_base + EHCI_USBSTS);
636 if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
637 break;
638 }
639 } while (wait_time > 0);
640 }
641 writel(0, op_reg_base + EHCI_USBINTR);
642 writel(0x3f, op_reg_base + EHCI_USBSTS);
643
644 iounmap(base);
David Brownell75862692005-09-23 17:14:37 -0700645}
646
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700647/*
648 * handshake - spin reading a register until handshake completes
649 * @ptr: address of hc register to be read
650 * @mask: bits to look at in result of read
651 * @done: value of those bits when handshake succeeds
652 * @wait_usec: timeout in microseconds
653 * @delay_usec: delay in microseconds to wait between polling
654 *
655 * Polls a register every delay_usec microseconds.
656 * Returns 0 when the mask bits have the value done.
657 * Returns -ETIMEDOUT if this condition is not true after
658 * wait_usec microseconds have passed.
659 */
660static int handshake(void __iomem *ptr, u32 mask, u32 done,
661 int wait_usec, int delay_usec)
662{
663 u32 result;
David Brownell75862692005-09-23 17:14:37 -0700664
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700665 do {
666 result = readl(ptr);
667 result &= mask;
668 if (result == done)
669 return 0;
670 udelay(delay_usec);
671 wait_usec -= delay_usec;
672 } while (wait_usec > 0);
673 return -ETIMEDOUT;
674}
675
676/**
677 * PCI Quirks for xHCI.
678 *
679 * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
680 * It signals to the BIOS that the OS wants control of the host controller,
681 * and then waits 5 seconds for the BIOS to hand over control.
682 * If we timeout, assume the BIOS is broken and take control anyway.
683 */
684static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
685{
686 void __iomem *base;
687 int ext_cap_offset;
688 void __iomem *op_reg_base;
689 u32 val;
690 int timeout;
691
692 if (!mmio_resource_enabled(pdev, 0))
693 return;
694
695 base = ioremap_nocache(pci_resource_start(pdev, 0),
696 pci_resource_len(pdev, 0));
697 if (base == NULL)
698 return;
699
700 /*
701 * Find the Legacy Support Capability register -
702 * this is optional for xHCI host controllers.
703 */
704 ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
705 do {
706 if (!ext_cap_offset)
707 /* We've reached the end of the extended capabilities */
708 goto hc_init;
709 val = readl(base + ext_cap_offset);
710 if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
711 break;
712 ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
713 } while (1);
714
715 /* If the BIOS owns the HC, signal that the OS wants it, and wait */
716 if (val & XHCI_HC_BIOS_OWNED) {
717 writel(val & XHCI_HC_OS_OWNED, base + ext_cap_offset);
718
719 /* Wait for 5 seconds with 10 microsecond polling interval */
720 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
721 0, 5000, 10);
722
723 /* Assume a buggy BIOS and take HC ownership anyway */
724 if (timeout) {
725 dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
726 " (BIOS bug ?) %08x\n", val);
727 writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
728 }
729 }
730
731 /* Disable any BIOS SMIs */
732 writel(XHCI_LEGACY_DISABLE_SMI,
733 base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
734
735hc_init:
736 op_reg_base = base + XHCI_HC_LENGTH(readl(base));
737
738 /* Wait for the host controller to be ready before writing any
739 * operational or runtime registers. Wait 5 seconds and no more.
740 */
741 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
742 5000, 10);
743 /* Assume a buggy HC and start HC initialization anyway */
744 if (timeout) {
745 val = readl(op_reg_base + XHCI_STS_OFFSET);
746 dev_warn(&pdev->dev,
747 "xHCI HW not ready after 5 sec (HC bug?) "
748 "status = 0x%x\n", val);
749 }
750
751 /* Send the halt and disable interrupts command */
752 val = readl(op_reg_base + XHCI_CMD_OFFSET);
753 val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
754 writel(val, op_reg_base + XHCI_CMD_OFFSET);
755
756 /* Wait for the HC to halt - poll every 125 usec (one microframe). */
757 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
758 XHCI_MAX_HALT_USEC, 125);
759 if (timeout) {
760 val = readl(op_reg_base + XHCI_STS_OFFSET);
761 dev_warn(&pdev->dev,
762 "xHCI HW did not halt within %d usec "
763 "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
764 }
765
766 iounmap(base);
767}
David Brownell75862692005-09-23 17:14:37 -0700768
769static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
770{
Alan Stern478a3ba2005-10-19 12:52:02 -0400771 if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
David Brownell75862692005-09-23 17:14:37 -0700772 quirk_usb_handoff_uhci(pdev);
Alan Stern478a3ba2005-10-19 12:52:02 -0400773 else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
David Brownell75862692005-09-23 17:14:37 -0700774 quirk_usb_handoff_ohci(pdev);
Alan Stern478a3ba2005-10-19 12:52:02 -0400775 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
David Brownell75862692005-09-23 17:14:37 -0700776 quirk_usb_disable_ehci(pdev);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700777 else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
778 quirk_usb_handoff_xhci(pdev);
David Brownell75862692005-09-23 17:14:37 -0700779}
Linus Torvaldsd93a8f82009-10-11 15:57:57 -0700780DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);