pci-quirks.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #define UHCI_USBLEGSUP 0xc0 /* legacy support */
  18. #define UHCI_USBCMD 0 /* command register */
  19. #define UHCI_USBINTR 4 /* interrupt register */
  20. #define UHCI_USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
  21. #define UHCI_USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
  22. #define UHCI_USBCMD_RUN 0x0001 /* RUN/STOP bit */
  23. #define UHCI_USBCMD_HCRESET 0x0002 /* Host Controller reset */
  24. #define UHCI_USBCMD_EGSM 0x0008 /* Global Suspend Mode */
  25. #define UHCI_USBCMD_CONFIGURE 0x0040 /* Config Flag */
  26. #define UHCI_USBINTR_RESUME 0x0002 /* Resume interrupt enable */
  27. #define OHCI_CONTROL 0x04
  28. #define OHCI_CMDSTATUS 0x08
  29. #define OHCI_INTRSTATUS 0x0c
  30. #define OHCI_INTRENABLE 0x10
  31. #define OHCI_INTRDISABLE 0x14
  32. #define OHCI_OCR (1 << 3) /* ownership change request */
  33. #define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */
  34. #define OHCI_CTRL_IR (1 << 8) /* interrupt routing */
  35. #define OHCI_INTR_OC (1 << 30) /* ownership change */
  36. #define EHCI_HCC_PARAMS 0x08 /* extended capabilities */
  37. #define EHCI_USBCMD 0 /* command register */
  38. #define EHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
  39. #define EHCI_USBSTS 4 /* status register */
  40. #define EHCI_USBSTS_HALTED (1 << 12) /* HCHalted bit */
  41. #define EHCI_USBINTR 8 /* interrupt register */
  42. #define EHCI_USBLEGSUP 0 /* legacy support register */
  43. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  44. #define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
  45. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  46. #define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
  47. /*
  48. * Make sure the controller is completely inactive, unable to
  49. * generate interrupts or do DMA.
  50. */
  51. void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
  52. {
  53. /* Turn off PIRQ enable and SMI enable. (This also turns off the
  54. * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
  55. */
  56. pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
  57. /* Reset the HC - this will force us to get a
  58. * new notification of any already connected
  59. * ports due to the virtual disconnect that it
  60. * implies.
  61. */
  62. outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
  63. mb();
  64. udelay(5);
  65. if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
  66. dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
  67. /* Just to be safe, disable interrupt requests and
  68. * make sure the controller is stopped.
  69. */
  70. outw(0, base + UHCI_USBINTR);
  71. outw(0, base + UHCI_USBCMD);
  72. }
  73. EXPORT_SYMBOL_GPL(uhci_reset_hc);
  74. /*
  75. * Initialize a controller that was newly discovered or has just been
  76. * resumed. In either case we can't be sure of its previous state.
  77. *
  78. * Returns: 1 if the controller was reset, 0 otherwise.
  79. */
  80. int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
  81. {
  82. u16 legsup;
  83. unsigned int cmd, intr;
  84. /*
  85. * When restarting a suspended controller, we expect all the
  86. * settings to be the same as we left them:
  87. *
  88. * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
  89. * Controller is stopped and configured with EGSM set;
  90. * No interrupts enabled except possibly Resume Detect.
  91. *
  92. * If any of these conditions are violated we do a complete reset.
  93. */
  94. pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
  95. if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
  96. dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
  97. __FUNCTION__, legsup);
  98. goto reset_needed;
  99. }
  100. cmd = inw(base + UHCI_USBCMD);
  101. if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
  102. !(cmd & UHCI_USBCMD_EGSM)) {
  103. dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
  104. __FUNCTION__, cmd);
  105. goto reset_needed;
  106. }
  107. intr = inw(base + UHCI_USBINTR);
  108. if (intr & (~UHCI_USBINTR_RESUME)) {
  109. dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
  110. __FUNCTION__, intr);
  111. goto reset_needed;
  112. }
  113. return 0;
  114. reset_needed:
  115. dev_dbg(&pdev->dev, "Performing full reset\n");
  116. uhci_reset_hc(pdev, base);
  117. return 1;
  118. }
  119. EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
  120. static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
  121. {
  122. u16 cmd;
  123. return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
  124. }
  125. #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
  126. #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
  127. static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
  128. {
  129. unsigned long base = 0;
  130. int i;
  131. if (!pio_enabled(pdev))
  132. return;
  133. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  134. if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  135. base = pci_resource_start(pdev, i);
  136. break;
  137. }
  138. if (base)
  139. uhci_check_and_reset_hc(pdev, base);
  140. }
  141. static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
  142. {
  143. return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
  144. }
  145. static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
  146. {
  147. void __iomem *base;
  148. int wait_time;
  149. u32 control;
  150. if (!mmio_resource_enabled(pdev, 0))
  151. return;
  152. base = ioremap_nocache(pci_resource_start(pdev, 0),
  153. pci_resource_len(pdev, 0));
  154. if (base == NULL) return;
  155. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  156. #ifndef __hppa__
  157. control = readl(base + OHCI_CONTROL);
  158. if (control & OHCI_CTRL_IR) {
  159. wait_time = 500; /* arbitrary; 5 seconds */
  160. writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
  161. writel(OHCI_OCR, base + OHCI_CMDSTATUS);
  162. while (wait_time > 0 &&
  163. readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
  164. wait_time -= 10;
  165. msleep(10);
  166. }
  167. if (wait_time <= 0)
  168. printk(KERN_WARNING "%s %s: BIOS handoff "
  169. "failed (BIOS bug ?)\n",
  170. pdev->dev.bus_id, "OHCI");
  171. /* reset controller, preserving RWC */
  172. writel(control & OHCI_CTRL_RWC, base + OHCI_CONTROL);
  173. }
  174. #endif
  175. /*
  176. * disable interrupts
  177. */
  178. writel(~(u32)0, base + OHCI_INTRDISABLE);
  179. writel(~(u32)0, base + OHCI_INTRSTATUS);
  180. iounmap(base);
  181. }
  182. static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
  183. {
  184. int wait_time, delta;
  185. void __iomem *base, *op_reg_base;
  186. u32 hcc_params, val;
  187. u8 offset, cap_length;
  188. int count = 256/4;
  189. if (!mmio_resource_enabled(pdev, 0))
  190. return;
  191. base = ioremap_nocache(pci_resource_start(pdev, 0),
  192. pci_resource_len(pdev, 0));
  193. if (base == NULL) return;
  194. cap_length = readb(base);
  195. op_reg_base = base + cap_length;
  196. /* EHCI 0.96 and later may have "extended capabilities"
  197. * spec section 5.1 explains the bios handoff, e.g. for
  198. * booting from USB disk or using a usb keyboard
  199. */
  200. hcc_params = readl(base + EHCI_HCC_PARAMS);
  201. offset = (hcc_params >> 8) & 0xff;
  202. while (offset && count--) {
  203. u32 cap;
  204. int msec;
  205. pci_read_config_dword(pdev, offset, &cap);
  206. switch (cap & 0xff) {
  207. case 1: /* BIOS/SMM/... handoff support */
  208. if ((cap & EHCI_USBLEGSUP_BIOS)) {
  209. pr_debug("%s %s: BIOS handoff\n",
  210. pdev->dev.bus_id, "EHCI");
  211. /* BIOS workaround (?): be sure the
  212. * pre-Linux code receives the SMI
  213. */
  214. pci_read_config_dword(pdev,
  215. offset + EHCI_USBLEGCTLSTS,
  216. &val);
  217. pci_write_config_dword(pdev,
  218. offset + EHCI_USBLEGCTLSTS,
  219. val | EHCI_USBLEGCTLSTS_SOOE);
  220. }
  221. /* always say Linux will own the hardware
  222. * by setting EHCI_USBLEGSUP_OS.
  223. */
  224. pci_write_config_byte(pdev, offset + 3, 1);
  225. /* if boot firmware now owns EHCI, spin till
  226. * it hands it over.
  227. */
  228. msec = 5000;
  229. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  230. msleep(10);
  231. msec -= 10;
  232. pci_read_config_dword(pdev, offset, &cap);
  233. }
  234. if (cap & EHCI_USBLEGSUP_BIOS) {
  235. /* well, possibly buggy BIOS... try to shut
  236. * it down, and hope nothing goes too wrong
  237. */
  238. printk(KERN_WARNING "%s %s: BIOS handoff "
  239. "failed (BIOS bug ?)\n",
  240. pdev->dev.bus_id, "EHCI");
  241. pci_write_config_byte(pdev, offset + 2, 0);
  242. }
  243. /* just in case, always disable EHCI SMIs */
  244. pci_write_config_dword(pdev,
  245. offset + EHCI_USBLEGCTLSTS,
  246. 0);
  247. break;
  248. case 0: /* illegal reserved capability */
  249. cap = 0;
  250. /* FALLTHROUGH */
  251. default:
  252. printk(KERN_WARNING "%s %s: unrecognized "
  253. "capability %02x\n",
  254. pdev->dev.bus_id, "EHCI",
  255. cap & 0xff);
  256. break;
  257. }
  258. offset = (cap >> 8) & 0xff;
  259. }
  260. if (!count)
  261. printk(KERN_DEBUG "%s %s: capability loop?\n",
  262. pdev->dev.bus_id, "EHCI");
  263. /*
  264. * halt EHCI & disable its interrupts in any case
  265. */
  266. val = readl(op_reg_base + EHCI_USBSTS);
  267. if ((val & EHCI_USBSTS_HALTED) == 0) {
  268. val = readl(op_reg_base + EHCI_USBCMD);
  269. val &= ~EHCI_USBCMD_RUN;
  270. writel(val, op_reg_base + EHCI_USBCMD);
  271. wait_time = 2000;
  272. delta = 100;
  273. do {
  274. writel(0x3f, op_reg_base + EHCI_USBSTS);
  275. udelay(delta);
  276. wait_time -= delta;
  277. val = readl(op_reg_base + EHCI_USBSTS);
  278. if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
  279. break;
  280. }
  281. } while (wait_time > 0);
  282. }
  283. writel(0, op_reg_base + EHCI_USBINTR);
  284. writel(0x3f, op_reg_base + EHCI_USBSTS);
  285. iounmap(base);
  286. return;
  287. }
  288. static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
  289. {
  290. if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
  291. quirk_usb_handoff_uhci(pdev);
  292. else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
  293. quirk_usb_handoff_ohci(pdev);
  294. else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
  295. quirk_usb_disable_ehci(pdev);
  296. }
  297. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);