pci-quirks.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/config.h>
  11. #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>
  17. #include "pci-quirks.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_USBLEGSUP 0 /* legacy support register */
  44. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  45. #define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
  46. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  47. #define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
  48. /*
  49. * Make sure the controller is completely inactive, unable to
  50. * generate interrupts or do DMA.
  51. */
  52. void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
  53. {
  54. /* Turn off PIRQ enable and SMI enable. (This also turns off the
  55. * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
  56. */
  57. pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
  58. /* Reset the HC - this will force us to get a
  59. * new notification of any already connected
  60. * ports due to the virtual disconnect that it
  61. * implies.
  62. */
  63. outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
  64. mb();
  65. udelay(5);
  66. if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
  67. dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
  68. /* Just to be safe, disable interrupt requests and
  69. * make sure the controller is stopped.
  70. */
  71. outw(0, base + UHCI_USBINTR);
  72. outw(0, base + UHCI_USBCMD);
  73. }
  74. EXPORT_SYMBOL_GPL(uhci_reset_hc);
  75. /*
  76. * Initialize a controller that was newly discovered or has just been
  77. * resumed. In either case we can't be sure of its previous state.
  78. *
  79. * Returns: 1 if the controller was reset, 0 otherwise.
  80. */
  81. int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
  82. {
  83. u16 legsup;
  84. unsigned int cmd, intr;
  85. /*
  86. * When restarting a suspended controller, we expect all the
  87. * settings to be the same as we left them:
  88. *
  89. * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
  90. * Controller is stopped and configured with EGSM set;
  91. * No interrupts enabled except possibly Resume Detect.
  92. *
  93. * If any of these conditions are violated we do a complete reset.
  94. */
  95. pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
  96. if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
  97. dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
  98. __FUNCTION__, legsup);
  99. goto reset_needed;
  100. }
  101. cmd = inw(base + UHCI_USBCMD);
  102. if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
  103. !(cmd & UHCI_USBCMD_EGSM)) {
  104. dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
  105. __FUNCTION__, cmd);
  106. goto reset_needed;
  107. }
  108. intr = inw(base + UHCI_USBINTR);
  109. if (intr & (~UHCI_USBINTR_RESUME)) {
  110. dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
  111. __FUNCTION__, intr);
  112. goto reset_needed;
  113. }
  114. return 0;
  115. reset_needed:
  116. dev_dbg(&pdev->dev, "Performing full reset\n");
  117. uhci_reset_hc(pdev, base);
  118. return 1;
  119. }
  120. EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
  121. static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
  122. {
  123. u16 cmd;
  124. return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
  125. }
  126. #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
  127. #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
  128. static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
  129. {
  130. unsigned long base = 0;
  131. int i;
  132. if (!pio_enabled(pdev))
  133. return;
  134. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  135. if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  136. base = pci_resource_start(pdev, i);
  137. break;
  138. }
  139. if (base)
  140. uhci_check_and_reset_hc(pdev, base);
  141. }
  142. static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
  143. {
  144. return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
  145. }
  146. static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
  147. {
  148. void __iomem *base;
  149. int wait_time;
  150. u32 control;
  151. if (!mmio_resource_enabled(pdev, 0))
  152. return;
  153. base = ioremap_nocache(pci_resource_start(pdev, 0),
  154. pci_resource_len(pdev, 0));
  155. if (base == NULL) return;
  156. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  157. #ifndef __hppa__
  158. control = readl(base + OHCI_CONTROL);
  159. if (control & OHCI_CTRL_IR) {
  160. 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. printk(KERN_WARNING "%s %s: BIOS handoff "
  170. "failed (BIOS bug ?) %08x\n",
  171. pdev->dev.bus_id, "OHCI",
  172. readl(base + OHCI_CONTROL));
  173. /* reset controller, preserving RWC */
  174. writel(control & OHCI_CTRL_RWC, base + OHCI_CONTROL);
  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. if (!mmio_resource_enabled(pdev, 0))
  192. return;
  193. base = ioremap_nocache(pci_resource_start(pdev, 0),
  194. pci_resource_len(pdev, 0));
  195. if (base == NULL) return;
  196. cap_length = readb(base);
  197. op_reg_base = base + cap_length;
  198. /* EHCI 0.96 and later may have "extended capabilities"
  199. * spec section 5.1 explains the bios handoff, e.g. for
  200. * booting from USB disk or using a usb keyboard
  201. */
  202. hcc_params = readl(base + EHCI_HCC_PARAMS);
  203. offset = (hcc_params >> 8) & 0xff;
  204. while (offset && count--) {
  205. u32 cap;
  206. int msec;
  207. pci_read_config_dword(pdev, offset, &cap);
  208. switch (cap & 0xff) {
  209. case 1: /* BIOS/SMM/... handoff support */
  210. if ((cap & EHCI_USBLEGSUP_BIOS)) {
  211. pr_debug("%s %s: BIOS handoff\n",
  212. pdev->dev.bus_id, "EHCI");
  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 = 5000;
  239. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  240. msleep(10);
  241. msec -= 10;
  242. pci_read_config_dword(pdev, offset, &cap);
  243. }
  244. if (cap & EHCI_USBLEGSUP_BIOS) {
  245. /* well, possibly buggy BIOS... try to shut
  246. * it down, and hope nothing goes too wrong
  247. */
  248. printk(KERN_WARNING "%s %s: BIOS handoff "
  249. "failed (BIOS bug ?) %08x\n",
  250. pdev->dev.bus_id, "EHCI", 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. break;
  258. case 0: /* illegal reserved capability */
  259. cap = 0;
  260. /* FALLTHROUGH */
  261. default:
  262. printk(KERN_WARNING "%s %s: unrecognized "
  263. "capability %02x\n",
  264. pdev->dev.bus_id, "EHCI",
  265. cap & 0xff);
  266. break;
  267. }
  268. offset = (cap >> 8) & 0xff;
  269. }
  270. if (!count)
  271. printk(KERN_DEBUG "%s %s: capability loop?\n",
  272. pdev->dev.bus_id, "EHCI");
  273. /*
  274. * halt EHCI & disable its interrupts in any case
  275. */
  276. val = readl(op_reg_base + EHCI_USBSTS);
  277. if ((val & EHCI_USBSTS_HALTED) == 0) {
  278. val = readl(op_reg_base + EHCI_USBCMD);
  279. val &= ~EHCI_USBCMD_RUN;
  280. writel(val, op_reg_base + EHCI_USBCMD);
  281. wait_time = 2000;
  282. delta = 100;
  283. do {
  284. writel(0x3f, op_reg_base + EHCI_USBSTS);
  285. udelay(delta);
  286. wait_time -= delta;
  287. val = readl(op_reg_base + EHCI_USBSTS);
  288. if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
  289. break;
  290. }
  291. } while (wait_time > 0);
  292. }
  293. writel(0, op_reg_base + EHCI_USBINTR);
  294. writel(0x3f, op_reg_base + EHCI_USBSTS);
  295. iounmap(base);
  296. return;
  297. }
  298. static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
  299. {
  300. if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
  301. quirk_usb_handoff_uhci(pdev);
  302. else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
  303. quirk_usb_handoff_ohci(pdev);
  304. else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
  305. quirk_usb_disable_ehci(pdev);
  306. }
  307. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);