pci-quirks.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /*
  50. * Make sure the controller is completely inactive, unable to
  51. * generate interrupts or do DMA.
  52. */
  53. void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
  54. {
  55. /* Turn off PIRQ enable and SMI enable. (This also turns off the
  56. * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
  57. */
  58. pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
  59. /* Reset the HC - this will force us to get a
  60. * new notification of any already connected
  61. * ports due to the virtual disconnect that it
  62. * implies.
  63. */
  64. outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
  65. mb();
  66. udelay(5);
  67. if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
  68. dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
  69. /* Just to be safe, disable interrupt requests and
  70. * make sure the controller is stopped.
  71. */
  72. outw(0, base + UHCI_USBINTR);
  73. outw(0, base + UHCI_USBCMD);
  74. }
  75. EXPORT_SYMBOL_GPL(uhci_reset_hc);
  76. /*
  77. * Initialize a controller that was newly discovered or has just been
  78. * resumed. In either case we can't be sure of its previous state.
  79. *
  80. * Returns: 1 if the controller was reset, 0 otherwise.
  81. */
  82. int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
  83. {
  84. u16 legsup;
  85. unsigned int cmd, intr;
  86. /*
  87. * When restarting a suspended controller, we expect all the
  88. * settings to be the same as we left them:
  89. *
  90. * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
  91. * Controller is stopped and configured with EGSM set;
  92. * No interrupts enabled except possibly Resume Detect.
  93. *
  94. * If any of these conditions are violated we do a complete reset.
  95. */
  96. pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
  97. if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
  98. dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
  99. __func__, legsup);
  100. goto reset_needed;
  101. }
  102. cmd = inw(base + UHCI_USBCMD);
  103. if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
  104. !(cmd & UHCI_USBCMD_EGSM)) {
  105. dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
  106. __func__, cmd);
  107. goto reset_needed;
  108. }
  109. intr = inw(base + UHCI_USBINTR);
  110. if (intr & (~UHCI_USBINTR_RESUME)) {
  111. dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
  112. __func__, intr);
  113. goto reset_needed;
  114. }
  115. return 0;
  116. reset_needed:
  117. dev_dbg(&pdev->dev, "Performing full reset\n");
  118. uhci_reset_hc(pdev, base);
  119. return 1;
  120. }
  121. EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
  122. static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
  123. {
  124. u16 cmd;
  125. return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
  126. }
  127. #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
  128. #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
  129. static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
  130. {
  131. unsigned long base = 0;
  132. int i;
  133. if (!pio_enabled(pdev))
  134. return;
  135. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  136. if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  137. base = pci_resource_start(pdev, i);
  138. break;
  139. }
  140. if (base)
  141. uhci_check_and_reset_hc(pdev, base);
  142. }
  143. static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
  144. {
  145. return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
  146. }
  147. static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
  148. {
  149. void __iomem *base;
  150. if (!mmio_resource_enabled(pdev, 0))
  151. return;
  152. base = pci_ioremap_bar(pdev, 0);
  153. if (base == NULL)
  154. return;
  155. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  156. #ifndef __hppa__
  157. {
  158. u32 control = readl(base + OHCI_CONTROL);
  159. if (control & OHCI_CTRL_IR) {
  160. int wait_time = 500; /* arbitrary; 5 seconds */
  161. writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
  162. writel(OHCI_OCR, base + OHCI_CMDSTATUS);
  163. while (wait_time > 0 &&
  164. readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
  165. wait_time -= 10;
  166. msleep(10);
  167. }
  168. if (wait_time <= 0)
  169. dev_warn(&pdev->dev, "OHCI: BIOS handoff failed"
  170. " (BIOS bug?) %08x\n",
  171. readl(base + OHCI_CONTROL));
  172. /* reset controller, preserving RWC */
  173. writel(control & OHCI_CTRL_RWC, base + OHCI_CONTROL);
  174. }
  175. }
  176. #endif
  177. /*
  178. * disable interrupts
  179. */
  180. writel(~(u32)0, base + OHCI_INTRDISABLE);
  181. writel(~(u32)0, base + OHCI_INTRSTATUS);
  182. iounmap(base);
  183. }
  184. static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
  185. {
  186. int wait_time, delta;
  187. void __iomem *base, *op_reg_base;
  188. u32 hcc_params, val;
  189. u8 offset, cap_length;
  190. int count = 256/4;
  191. int tried_handoff = 0;
  192. if (!mmio_resource_enabled(pdev, 0))
  193. return;
  194. base = pci_ioremap_bar(pdev, 0);
  195. if (base == NULL)
  196. return;
  197. cap_length = readb(base);
  198. op_reg_base = base + cap_length;
  199. /* EHCI 0.96 and later may have "extended capabilities"
  200. * spec section 5.1 explains the bios handoff, e.g. for
  201. * booting from USB disk or using a usb keyboard
  202. */
  203. hcc_params = readl(base + EHCI_HCC_PARAMS);
  204. offset = (hcc_params >> 8) & 0xff;
  205. while (offset && --count) {
  206. u32 cap;
  207. int msec;
  208. pci_read_config_dword(pdev, offset, &cap);
  209. switch (cap & 0xff) {
  210. case 1: /* BIOS/SMM/... handoff support */
  211. if ((cap & EHCI_USBLEGSUP_BIOS)) {
  212. dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
  213. #if 0
  214. /* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
  215. * but that seems dubious in general (the BIOS left it off intentionally)
  216. * and is known to prevent some systems from booting. so we won't do this
  217. * unless maybe we can determine when we're on a system that needs SMI forced.
  218. */
  219. /* BIOS workaround (?): be sure the
  220. * pre-Linux code receives the SMI
  221. */
  222. pci_read_config_dword(pdev,
  223. offset + EHCI_USBLEGCTLSTS,
  224. &val);
  225. pci_write_config_dword(pdev,
  226. offset + EHCI_USBLEGCTLSTS,
  227. val | EHCI_USBLEGCTLSTS_SOOE);
  228. #endif
  229. /* some systems get upset if this semaphore is
  230. * set for any other reason than forcing a BIOS
  231. * handoff..
  232. */
  233. pci_write_config_byte(pdev, offset + 3, 1);
  234. }
  235. /* if boot firmware now owns EHCI, spin till
  236. * it hands it over.
  237. */
  238. msec = 1000;
  239. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  240. tried_handoff = 1;
  241. msleep(10);
  242. msec -= 10;
  243. pci_read_config_dword(pdev, offset, &cap);
  244. }
  245. if (cap & EHCI_USBLEGSUP_BIOS) {
  246. /* well, possibly buggy BIOS... try to shut
  247. * it down, and hope nothing goes too wrong
  248. */
  249. dev_warn(&pdev->dev, "EHCI: BIOS handoff failed"
  250. " (BIOS bug?) %08x\n", cap);
  251. pci_write_config_byte(pdev, offset + 2, 0);
  252. }
  253. /* just in case, always disable EHCI SMIs */
  254. pci_write_config_dword(pdev,
  255. offset + EHCI_USBLEGCTLSTS,
  256. 0);
  257. /* If the BIOS ever owned the controller then we
  258. * can't expect any power sessions to remain intact.
  259. */
  260. if (tried_handoff)
  261. writel(0, op_reg_base + EHCI_CONFIGFLAG);
  262. break;
  263. case 0: /* illegal reserved capability */
  264. cap = 0;
  265. /* FALLTHROUGH */
  266. default:
  267. dev_warn(&pdev->dev, "EHCI: unrecognized capability "
  268. "%02x\n", cap & 0xff);
  269. break;
  270. }
  271. offset = (cap >> 8) & 0xff;
  272. }
  273. if (!count)
  274. dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
  275. /*
  276. * halt EHCI & disable its interrupts in any case
  277. */
  278. val = readl(op_reg_base + EHCI_USBSTS);
  279. if ((val & EHCI_USBSTS_HALTED) == 0) {
  280. val = readl(op_reg_base + EHCI_USBCMD);
  281. val &= ~EHCI_USBCMD_RUN;
  282. writel(val, op_reg_base + EHCI_USBCMD);
  283. wait_time = 2000;
  284. delta = 100;
  285. do {
  286. writel(0x3f, op_reg_base + EHCI_USBSTS);
  287. udelay(delta);
  288. wait_time -= delta;
  289. val = readl(op_reg_base + EHCI_USBSTS);
  290. if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
  291. break;
  292. }
  293. } while (wait_time > 0);
  294. }
  295. writel(0, op_reg_base + EHCI_USBINTR);
  296. writel(0x3f, op_reg_base + EHCI_USBSTS);
  297. iounmap(base);
  298. return;
  299. }
  300. /*
  301. * handshake - spin reading a register until handshake completes
  302. * @ptr: address of hc register to be read
  303. * @mask: bits to look at in result of read
  304. * @done: value of those bits when handshake succeeds
  305. * @wait_usec: timeout in microseconds
  306. * @delay_usec: delay in microseconds to wait between polling
  307. *
  308. * Polls a register every delay_usec microseconds.
  309. * Returns 0 when the mask bits have the value done.
  310. * Returns -ETIMEDOUT if this condition is not true after
  311. * wait_usec microseconds have passed.
  312. */
  313. static int handshake(void __iomem *ptr, u32 mask, u32 done,
  314. int wait_usec, int delay_usec)
  315. {
  316. u32 result;
  317. do {
  318. result = readl(ptr);
  319. result &= mask;
  320. if (result == done)
  321. return 0;
  322. udelay(delay_usec);
  323. wait_usec -= delay_usec;
  324. } while (wait_usec > 0);
  325. return -ETIMEDOUT;
  326. }
  327. /**
  328. * PCI Quirks for xHCI.
  329. *
  330. * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
  331. * It signals to the BIOS that the OS wants control of the host controller,
  332. * and then waits 5 seconds for the BIOS to hand over control.
  333. * If we timeout, assume the BIOS is broken and take control anyway.
  334. */
  335. static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
  336. {
  337. void __iomem *base;
  338. int ext_cap_offset;
  339. void __iomem *op_reg_base;
  340. u32 val;
  341. int timeout;
  342. if (!mmio_resource_enabled(pdev, 0))
  343. return;
  344. base = ioremap_nocache(pci_resource_start(pdev, 0),
  345. pci_resource_len(pdev, 0));
  346. if (base == NULL)
  347. return;
  348. /*
  349. * Find the Legacy Support Capability register -
  350. * this is optional for xHCI host controllers.
  351. */
  352. ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
  353. do {
  354. if (!ext_cap_offset)
  355. /* We've reached the end of the extended capabilities */
  356. goto hc_init;
  357. val = readl(base + ext_cap_offset);
  358. if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
  359. break;
  360. ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
  361. } while (1);
  362. /* If the BIOS owns the HC, signal that the OS wants it, and wait */
  363. if (val & XHCI_HC_BIOS_OWNED) {
  364. writel(val & XHCI_HC_OS_OWNED, base + ext_cap_offset);
  365. /* Wait for 5 seconds with 10 microsecond polling interval */
  366. timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
  367. 0, 5000, 10);
  368. /* Assume a buggy BIOS and take HC ownership anyway */
  369. if (timeout) {
  370. dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
  371. " (BIOS bug ?) %08x\n", val);
  372. writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
  373. }
  374. }
  375. /* Disable any BIOS SMIs */
  376. writel(XHCI_LEGACY_DISABLE_SMI,
  377. base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
  378. hc_init:
  379. op_reg_base = base + XHCI_HC_LENGTH(readl(base));
  380. /* Wait for the host controller to be ready before writing any
  381. * operational or runtime registers. Wait 5 seconds and no more.
  382. */
  383. timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
  384. 5000, 10);
  385. /* Assume a buggy HC and start HC initialization anyway */
  386. if (timeout) {
  387. val = readl(op_reg_base + XHCI_STS_OFFSET);
  388. dev_warn(&pdev->dev,
  389. "xHCI HW not ready after 5 sec (HC bug?) "
  390. "status = 0x%x\n", val);
  391. }
  392. /* Send the halt and disable interrupts command */
  393. val = readl(op_reg_base + XHCI_CMD_OFFSET);
  394. val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
  395. writel(val, op_reg_base + XHCI_CMD_OFFSET);
  396. /* Wait for the HC to halt - poll every 125 usec (one microframe). */
  397. timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
  398. XHCI_MAX_HALT_USEC, 125);
  399. if (timeout) {
  400. val = readl(op_reg_base + XHCI_STS_OFFSET);
  401. dev_warn(&pdev->dev,
  402. "xHCI HW did not halt within %d usec "
  403. "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
  404. }
  405. iounmap(base);
  406. }
  407. static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
  408. {
  409. if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
  410. quirk_usb_handoff_uhci(pdev);
  411. else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
  412. quirk_usb_handoff_ohci(pdev);
  413. else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
  414. quirk_usb_disable_ehci(pdev);
  415. else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
  416. quirk_usb_handoff_xhci(pdev);
  417. }
  418. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);