pci-quirks.c 20 KB

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