pci-quirks.c 20 KB

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