pci-quirks.c 8.7 KB

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