pci-quirks.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /*
  18. * PIIX3 USB: We have to disable USB interrupts that are
  19. * hardwired to PIRQD# and may be shared with an
  20. * external device.
  21. *
  22. * Legacy Support Register (LEGSUP):
  23. * bit13: USB PIRQ Enable (USBPIRQDEN),
  24. * bit4: Trap/SMI On IRQ Enable (USBSMIEN).
  25. *
  26. * We mask out all r/wc bits, too.
  27. */
  28. static void __devinit quirk_piix3_usb(struct pci_dev *dev)
  29. {
  30. u16 legsup;
  31. pci_read_config_word(dev, 0xc0, &legsup);
  32. legsup &= 0x50ef;
  33. pci_write_config_word(dev, 0xc0, legsup);
  34. }
  35. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_2, quirk_piix3_usb );
  36. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_2, quirk_piix3_usb );
  37. /* FIXME these should be the guts of hcd->reset() methods; resolve all
  38. * the differences between this version and the HCD's version.
  39. */
  40. #define UHCI_USBLEGSUP 0xc0 /* legacy support */
  41. #define UHCI_USBCMD 0 /* command register */
  42. #define UHCI_USBSTS 2 /* status register */
  43. #define UHCI_USBINTR 4 /* interrupt register */
  44. #define UHCI_USBLEGSUP_DEFAULT 0x2000 /* only PIRQ enable set */
  45. #define UHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
  46. #define UHCI_USBCMD_GRESET (1 << 2) /* Global reset */
  47. #define UHCI_USBCMD_CONFIGURE (1 << 6) /* config semaphore */
  48. #define UHCI_USBSTS_HALTED (1 << 5) /* HCHalted bit */
  49. #define OHCI_CONTROL 0x04
  50. #define OHCI_CMDSTATUS 0x08
  51. #define OHCI_INTRSTATUS 0x0c
  52. #define OHCI_INTRENABLE 0x10
  53. #define OHCI_INTRDISABLE 0x14
  54. #define OHCI_OCR (1 << 3) /* ownership change request */
  55. #define OHCI_CTRL_IR (1 << 8) /* interrupt routing */
  56. #define OHCI_INTR_OC (1 << 30) /* ownership change */
  57. #define EHCI_HCC_PARAMS 0x08 /* extended capabilities */
  58. #define EHCI_USBCMD 0 /* command register */
  59. #define EHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
  60. #define EHCI_USBSTS 4 /* status register */
  61. #define EHCI_USBSTS_HALTED (1 << 12) /* HCHalted bit */
  62. #define EHCI_USBINTR 8 /* interrupt register */
  63. #define EHCI_USBLEGSUP 0 /* legacy support register */
  64. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  65. #define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
  66. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  67. #define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
  68. int usb_early_handoff __devinitdata = 0;
  69. static int __init usb_handoff_early(char *str)
  70. {
  71. usb_early_handoff = 1;
  72. return 0;
  73. }
  74. __setup("usb-handoff", usb_handoff_early);
  75. static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
  76. {
  77. unsigned long base = 0;
  78. int wait_time, delta;
  79. u16 val, sts;
  80. int i;
  81. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  82. if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  83. base = pci_resource_start(pdev, i);
  84. break;
  85. }
  86. if (!base)
  87. return;
  88. /*
  89. * stop controller
  90. */
  91. sts = inw(base + UHCI_USBSTS);
  92. val = inw(base + UHCI_USBCMD);
  93. val &= ~(u16)(UHCI_USBCMD_RUN | UHCI_USBCMD_CONFIGURE);
  94. outw(val, base + UHCI_USBCMD);
  95. /*
  96. * wait while it stops if it was running
  97. */
  98. if ((sts & UHCI_USBSTS_HALTED) == 0)
  99. {
  100. wait_time = 1000;
  101. delta = 100;
  102. do {
  103. outw(0x1f, base + UHCI_USBSTS);
  104. udelay(delta);
  105. wait_time -= delta;
  106. val = inw(base + UHCI_USBSTS);
  107. if (val & UHCI_USBSTS_HALTED)
  108. break;
  109. } while (wait_time > 0);
  110. }
  111. /*
  112. * disable interrupts & legacy support
  113. */
  114. outw(0, base + UHCI_USBINTR);
  115. outw(0x1f, base + UHCI_USBSTS);
  116. pci_read_config_word(pdev, UHCI_USBLEGSUP, &val);
  117. if (val & 0xbf)
  118. pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_DEFAULT);
  119. }
  120. static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
  121. {
  122. void __iomem *base;
  123. int wait_time;
  124. base = ioremap_nocache(pci_resource_start(pdev, 0),
  125. pci_resource_len(pdev, 0));
  126. if (base == NULL) return;
  127. if (readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
  128. wait_time = 500; /* 0.5 seconds */
  129. writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
  130. writel(OHCI_OCR, base + OHCI_CMDSTATUS);
  131. while (wait_time > 0 &&
  132. readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
  133. wait_time -= 10;
  134. msleep(10);
  135. }
  136. }
  137. /*
  138. * disable interrupts
  139. */
  140. writel(~(u32)0, base + OHCI_INTRDISABLE);
  141. writel(~(u32)0, base + OHCI_INTRSTATUS);
  142. iounmap(base);
  143. }
  144. static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
  145. {
  146. int wait_time, delta;
  147. void __iomem *base, *op_reg_base;
  148. u32 hcc_params, val, temp;
  149. u8 cap_length;
  150. base = ioremap_nocache(pci_resource_start(pdev, 0),
  151. pci_resource_len(pdev, 0));
  152. if (base == NULL) return;
  153. cap_length = readb(base);
  154. op_reg_base = base + cap_length;
  155. hcc_params = readl(base + EHCI_HCC_PARAMS);
  156. hcc_params = (hcc_params >> 8) & 0xff;
  157. if (hcc_params) {
  158. pci_read_config_dword(pdev,
  159. hcc_params + EHCI_USBLEGSUP,
  160. &val);
  161. if (((val & 0xff) == 1) && (val & EHCI_USBLEGSUP_BIOS)) {
  162. /*
  163. * Ok, BIOS is in smm mode, try to hand off...
  164. */
  165. pci_read_config_dword(pdev,
  166. hcc_params + EHCI_USBLEGCTLSTS,
  167. &temp);
  168. pci_write_config_dword(pdev,
  169. hcc_params + EHCI_USBLEGCTLSTS,
  170. temp | EHCI_USBLEGCTLSTS_SOOE);
  171. val |= EHCI_USBLEGSUP_OS;
  172. pci_write_config_dword(pdev,
  173. hcc_params + EHCI_USBLEGSUP,
  174. val);
  175. wait_time = 500;
  176. do {
  177. msleep(10);
  178. wait_time -= 10;
  179. pci_read_config_dword(pdev,
  180. hcc_params + EHCI_USBLEGSUP,
  181. &val);
  182. } while (wait_time && (val & EHCI_USBLEGSUP_BIOS));
  183. if (!wait_time) {
  184. /*
  185. * well, possibly buggy BIOS...
  186. */
  187. printk(KERN_WARNING "EHCI early BIOS handoff "
  188. "failed (BIOS bug ?)\n");
  189. pci_write_config_dword(pdev,
  190. hcc_params + EHCI_USBLEGSUP,
  191. EHCI_USBLEGSUP_OS);
  192. pci_write_config_dword(pdev,
  193. hcc_params + EHCI_USBLEGCTLSTS,
  194. 0);
  195. }
  196. }
  197. }
  198. /*
  199. * halt EHCI & disable its interrupts in any case
  200. */
  201. val = readl(op_reg_base + EHCI_USBSTS);
  202. if ((val & EHCI_USBSTS_HALTED) == 0) {
  203. val = readl(op_reg_base + EHCI_USBCMD);
  204. val &= ~EHCI_USBCMD_RUN;
  205. writel(val, op_reg_base + EHCI_USBCMD);
  206. wait_time = 2000;
  207. delta = 100;
  208. do {
  209. writel(0x3f, op_reg_base + EHCI_USBSTS);
  210. udelay(delta);
  211. wait_time -= delta;
  212. val = readl(op_reg_base + EHCI_USBSTS);
  213. if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
  214. break;
  215. }
  216. } while (wait_time > 0);
  217. }
  218. writel(0, op_reg_base + EHCI_USBINTR);
  219. writel(0x3f, op_reg_base + EHCI_USBSTS);
  220. iounmap(base);
  221. return;
  222. }
  223. static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
  224. {
  225. if (!usb_early_handoff)
  226. return;
  227. if (pdev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x00)) { /* UHCI */
  228. quirk_usb_handoff_uhci(pdev);
  229. } else if (pdev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x10)) { /* OHCI */
  230. quirk_usb_handoff_ohci(pdev);
  231. } else if (pdev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x20)) { /* EHCI */
  232. quirk_usb_disable_ehci(pdev);
  233. }
  234. return;
  235. }
  236. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);