pci-quirks.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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/export.h>
  16. #include <linux/acpi.h>
  17. #include <linux/dmi.h>
  18. #include "pci-quirks.h"
  19. #include "xhci-ext-caps.h"
  20. #define UHCI_USBLEGSUP 0xc0 /* legacy support */
  21. #define UHCI_USBCMD 0 /* command register */
  22. #define UHCI_USBINTR 4 /* interrupt register */
  23. #define UHCI_USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
  24. #define UHCI_USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
  25. #define UHCI_USBCMD_RUN 0x0001 /* RUN/STOP bit */
  26. #define UHCI_USBCMD_HCRESET 0x0002 /* Host Controller reset */
  27. #define UHCI_USBCMD_EGSM 0x0008 /* Global Suspend Mode */
  28. #define UHCI_USBCMD_CONFIGURE 0x0040 /* Config Flag */
  29. #define UHCI_USBINTR_RESUME 0x0002 /* Resume interrupt enable */
  30. #define OHCI_CONTROL 0x04
  31. #define OHCI_CMDSTATUS 0x08
  32. #define OHCI_INTRSTATUS 0x0c
  33. #define OHCI_INTRENABLE 0x10
  34. #define OHCI_INTRDISABLE 0x14
  35. #define OHCI_FMINTERVAL 0x34
  36. #define OHCI_HCFS (3 << 6) /* hc functional state */
  37. #define OHCI_HCR (1 << 0) /* host controller reset */
  38. #define OHCI_OCR (1 << 3) /* ownership change request */
  39. #define OHCI_CTRL_RWC (1 << 9) /* remote wakeup connected */
  40. #define OHCI_CTRL_IR (1 << 8) /* interrupt routing */
  41. #define OHCI_INTR_OC (1 << 30) /* ownership change */
  42. #define EHCI_HCC_PARAMS 0x08 /* extended capabilities */
  43. #define EHCI_USBCMD 0 /* command register */
  44. #define EHCI_USBCMD_RUN (1 << 0) /* RUN/STOP bit */
  45. #define EHCI_USBSTS 4 /* status register */
  46. #define EHCI_USBSTS_HALTED (1 << 12) /* HCHalted bit */
  47. #define EHCI_USBINTR 8 /* interrupt register */
  48. #define EHCI_CONFIGFLAG 0x40 /* configured flag register */
  49. #define EHCI_USBLEGSUP 0 /* legacy support register */
  50. #define EHCI_USBLEGSUP_BIOS (1 << 16) /* BIOS semaphore */
  51. #define EHCI_USBLEGSUP_OS (1 << 24) /* OS semaphore */
  52. #define EHCI_USBLEGCTLSTS 4 /* legacy control/status */
  53. #define EHCI_USBLEGCTLSTS_SOOE (1 << 13) /* SMI on ownership change */
  54. /* AMD quirk use */
  55. #define AB_REG_BAR_LOW 0xe0
  56. #define AB_REG_BAR_HIGH 0xe1
  57. #define AB_REG_BAR_SB700 0xf0
  58. #define AB_INDX(addr) ((addr) + 0x00)
  59. #define AB_DATA(addr) ((addr) + 0x04)
  60. #define AX_INDXC 0x30
  61. #define AX_DATAC 0x34
  62. #define NB_PCIE_INDX_ADDR 0xe0
  63. #define NB_PCIE_INDX_DATA 0xe4
  64. #define PCIE_P_CNTL 0x10040
  65. #define BIF_NB 0x10002
  66. #define NB_PIF0_PWRDOWN_0 0x01100012
  67. #define NB_PIF0_PWRDOWN_1 0x01100013
  68. #define USB_INTEL_XUSB2PR 0xD0
  69. #define USB_INTEL_USB3_PSSEN 0xD8
  70. static struct amd_chipset_info {
  71. struct pci_dev *nb_dev;
  72. struct pci_dev *smbus_dev;
  73. int nb_type;
  74. int sb_type;
  75. int isoc_reqs;
  76. int probe_count;
  77. int probe_result;
  78. } amd_chipset;
  79. static DEFINE_SPINLOCK(amd_lock);
  80. int usb_amd_find_chipset_info(void)
  81. {
  82. u8 rev = 0;
  83. unsigned long flags;
  84. struct amd_chipset_info info;
  85. int ret;
  86. spin_lock_irqsave(&amd_lock, flags);
  87. /* probe only once */
  88. if (amd_chipset.probe_count > 0) {
  89. amd_chipset.probe_count++;
  90. spin_unlock_irqrestore(&amd_lock, flags);
  91. return amd_chipset.probe_result;
  92. }
  93. memset(&info, 0, sizeof(info));
  94. spin_unlock_irqrestore(&amd_lock, flags);
  95. info.smbus_dev = pci_get_device(PCI_VENDOR_ID_ATI, 0x4385, NULL);
  96. if (info.smbus_dev) {
  97. rev = info.smbus_dev->revision;
  98. if (rev >= 0x40)
  99. info.sb_type = 1;
  100. else if (rev >= 0x30 && rev <= 0x3b)
  101. info.sb_type = 3;
  102. } else {
  103. info.smbus_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  104. 0x780b, NULL);
  105. if (!info.smbus_dev) {
  106. ret = 0;
  107. goto commit;
  108. }
  109. rev = info.smbus_dev->revision;
  110. if (rev >= 0x11 && rev <= 0x18)
  111. info.sb_type = 2;
  112. }
  113. if (info.sb_type == 0) {
  114. if (info.smbus_dev) {
  115. pci_dev_put(info.smbus_dev);
  116. info.smbus_dev = NULL;
  117. }
  118. ret = 0;
  119. goto commit;
  120. }
  121. info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x9601, NULL);
  122. if (info.nb_dev) {
  123. info.nb_type = 1;
  124. } else {
  125. info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1510, NULL);
  126. if (info.nb_dev) {
  127. info.nb_type = 2;
  128. } else {
  129. info.nb_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  130. 0x9600, NULL);
  131. if (info.nb_dev)
  132. info.nb_type = 3;
  133. }
  134. }
  135. ret = info.probe_result = 1;
  136. printk(KERN_DEBUG "QUIRK: Enable AMD PLL fix\n");
  137. commit:
  138. spin_lock_irqsave(&amd_lock, flags);
  139. if (amd_chipset.probe_count > 0) {
  140. /* race - someone else was faster - drop devices */
  141. /* Mark that we where here */
  142. amd_chipset.probe_count++;
  143. ret = amd_chipset.probe_result;
  144. spin_unlock_irqrestore(&amd_lock, flags);
  145. if (info.nb_dev)
  146. pci_dev_put(info.nb_dev);
  147. if (info.smbus_dev)
  148. pci_dev_put(info.smbus_dev);
  149. } else {
  150. /* no race - commit the result */
  151. info.probe_count++;
  152. amd_chipset = info;
  153. spin_unlock_irqrestore(&amd_lock, flags);
  154. }
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(usb_amd_find_chipset_info);
  158. /*
  159. * The hardware normally enables the A-link power management feature, which
  160. * lets the system lower the power consumption in idle states.
  161. *
  162. * This USB quirk prevents the link going into that lower power state
  163. * during isochronous transfers.
  164. *
  165. * Without this quirk, isochronous stream on OHCI/EHCI/xHCI controllers of
  166. * some AMD platforms may stutter or have breaks occasionally.
  167. */
  168. static void usb_amd_quirk_pll(int disable)
  169. {
  170. u32 addr, addr_low, addr_high, val;
  171. u32 bit = disable ? 0 : 1;
  172. unsigned long flags;
  173. spin_lock_irqsave(&amd_lock, flags);
  174. if (disable) {
  175. amd_chipset.isoc_reqs++;
  176. if (amd_chipset.isoc_reqs > 1) {
  177. spin_unlock_irqrestore(&amd_lock, flags);
  178. return;
  179. }
  180. } else {
  181. amd_chipset.isoc_reqs--;
  182. if (amd_chipset.isoc_reqs > 0) {
  183. spin_unlock_irqrestore(&amd_lock, flags);
  184. return;
  185. }
  186. }
  187. if (amd_chipset.sb_type == 1 || amd_chipset.sb_type == 2) {
  188. outb_p(AB_REG_BAR_LOW, 0xcd6);
  189. addr_low = inb_p(0xcd7);
  190. outb_p(AB_REG_BAR_HIGH, 0xcd6);
  191. addr_high = inb_p(0xcd7);
  192. addr = addr_high << 8 | addr_low;
  193. outl_p(0x30, AB_INDX(addr));
  194. outl_p(0x40, AB_DATA(addr));
  195. outl_p(0x34, AB_INDX(addr));
  196. val = inl_p(AB_DATA(addr));
  197. } else if (amd_chipset.sb_type == 3) {
  198. pci_read_config_dword(amd_chipset.smbus_dev,
  199. AB_REG_BAR_SB700, &addr);
  200. outl(AX_INDXC, AB_INDX(addr));
  201. outl(0x40, AB_DATA(addr));
  202. outl(AX_DATAC, AB_INDX(addr));
  203. val = inl(AB_DATA(addr));
  204. } else {
  205. spin_unlock_irqrestore(&amd_lock, flags);
  206. return;
  207. }
  208. if (disable) {
  209. val &= ~0x08;
  210. val |= (1 << 4) | (1 << 9);
  211. } else {
  212. val |= 0x08;
  213. val &= ~((1 << 4) | (1 << 9));
  214. }
  215. outl_p(val, AB_DATA(addr));
  216. if (!amd_chipset.nb_dev) {
  217. spin_unlock_irqrestore(&amd_lock, flags);
  218. return;
  219. }
  220. if (amd_chipset.nb_type == 1 || amd_chipset.nb_type == 3) {
  221. addr = PCIE_P_CNTL;
  222. pci_write_config_dword(amd_chipset.nb_dev,
  223. NB_PCIE_INDX_ADDR, addr);
  224. pci_read_config_dword(amd_chipset.nb_dev,
  225. NB_PCIE_INDX_DATA, &val);
  226. val &= ~(1 | (1 << 3) | (1 << 4) | (1 << 9) | (1 << 12));
  227. val |= bit | (bit << 3) | (bit << 12);
  228. val |= ((!bit) << 4) | ((!bit) << 9);
  229. pci_write_config_dword(amd_chipset.nb_dev,
  230. NB_PCIE_INDX_DATA, val);
  231. addr = BIF_NB;
  232. pci_write_config_dword(amd_chipset.nb_dev,
  233. NB_PCIE_INDX_ADDR, addr);
  234. pci_read_config_dword(amd_chipset.nb_dev,
  235. NB_PCIE_INDX_DATA, &val);
  236. val &= ~(1 << 8);
  237. val |= bit << 8;
  238. pci_write_config_dword(amd_chipset.nb_dev,
  239. NB_PCIE_INDX_DATA, val);
  240. } else if (amd_chipset.nb_type == 2) {
  241. addr = NB_PIF0_PWRDOWN_0;
  242. pci_write_config_dword(amd_chipset.nb_dev,
  243. NB_PCIE_INDX_ADDR, addr);
  244. pci_read_config_dword(amd_chipset.nb_dev,
  245. NB_PCIE_INDX_DATA, &val);
  246. if (disable)
  247. val &= ~(0x3f << 7);
  248. else
  249. val |= 0x3f << 7;
  250. pci_write_config_dword(amd_chipset.nb_dev,
  251. NB_PCIE_INDX_DATA, val);
  252. addr = NB_PIF0_PWRDOWN_1;
  253. pci_write_config_dword(amd_chipset.nb_dev,
  254. NB_PCIE_INDX_ADDR, addr);
  255. pci_read_config_dword(amd_chipset.nb_dev,
  256. NB_PCIE_INDX_DATA, &val);
  257. if (disable)
  258. val &= ~(0x3f << 7);
  259. else
  260. val |= 0x3f << 7;
  261. pci_write_config_dword(amd_chipset.nb_dev,
  262. NB_PCIE_INDX_DATA, val);
  263. }
  264. spin_unlock_irqrestore(&amd_lock, flags);
  265. return;
  266. }
  267. void usb_amd_quirk_pll_disable(void)
  268. {
  269. usb_amd_quirk_pll(1);
  270. }
  271. EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_disable);
  272. void usb_amd_quirk_pll_enable(void)
  273. {
  274. usb_amd_quirk_pll(0);
  275. }
  276. EXPORT_SYMBOL_GPL(usb_amd_quirk_pll_enable);
  277. void usb_amd_dev_put(void)
  278. {
  279. struct pci_dev *nb, *smbus;
  280. unsigned long flags;
  281. spin_lock_irqsave(&amd_lock, flags);
  282. amd_chipset.probe_count--;
  283. if (amd_chipset.probe_count > 0) {
  284. spin_unlock_irqrestore(&amd_lock, flags);
  285. return;
  286. }
  287. /* save them to pci_dev_put outside of spinlock */
  288. nb = amd_chipset.nb_dev;
  289. smbus = amd_chipset.smbus_dev;
  290. amd_chipset.nb_dev = NULL;
  291. amd_chipset.smbus_dev = NULL;
  292. amd_chipset.nb_type = 0;
  293. amd_chipset.sb_type = 0;
  294. amd_chipset.isoc_reqs = 0;
  295. amd_chipset.probe_result = 0;
  296. spin_unlock_irqrestore(&amd_lock, flags);
  297. if (nb)
  298. pci_dev_put(nb);
  299. if (smbus)
  300. pci_dev_put(smbus);
  301. }
  302. EXPORT_SYMBOL_GPL(usb_amd_dev_put);
  303. /*
  304. * Make sure the controller is completely inactive, unable to
  305. * generate interrupts or do DMA.
  306. */
  307. void uhci_reset_hc(struct pci_dev *pdev, unsigned long base)
  308. {
  309. /* Turn off PIRQ enable and SMI enable. (This also turns off the
  310. * BIOS's USB Legacy Support.) Turn off all the R/WC bits too.
  311. */
  312. pci_write_config_word(pdev, UHCI_USBLEGSUP, UHCI_USBLEGSUP_RWC);
  313. /* Reset the HC - this will force us to get a
  314. * new notification of any already connected
  315. * ports due to the virtual disconnect that it
  316. * implies.
  317. */
  318. outw(UHCI_USBCMD_HCRESET, base + UHCI_USBCMD);
  319. mb();
  320. udelay(5);
  321. if (inw(base + UHCI_USBCMD) & UHCI_USBCMD_HCRESET)
  322. dev_warn(&pdev->dev, "HCRESET not completed yet!\n");
  323. /* Just to be safe, disable interrupt requests and
  324. * make sure the controller is stopped.
  325. */
  326. outw(0, base + UHCI_USBINTR);
  327. outw(0, base + UHCI_USBCMD);
  328. }
  329. EXPORT_SYMBOL_GPL(uhci_reset_hc);
  330. /*
  331. * Initialize a controller that was newly discovered or has just been
  332. * resumed. In either case we can't be sure of its previous state.
  333. *
  334. * Returns: 1 if the controller was reset, 0 otherwise.
  335. */
  336. int uhci_check_and_reset_hc(struct pci_dev *pdev, unsigned long base)
  337. {
  338. u16 legsup;
  339. unsigned int cmd, intr;
  340. /*
  341. * When restarting a suspended controller, we expect all the
  342. * settings to be the same as we left them:
  343. *
  344. * PIRQ and SMI disabled, no R/W bits set in USBLEGSUP;
  345. * Controller is stopped and configured with EGSM set;
  346. * No interrupts enabled except possibly Resume Detect.
  347. *
  348. * If any of these conditions are violated we do a complete reset.
  349. */
  350. pci_read_config_word(pdev, UHCI_USBLEGSUP, &legsup);
  351. if (legsup & ~(UHCI_USBLEGSUP_RO | UHCI_USBLEGSUP_RWC)) {
  352. dev_dbg(&pdev->dev, "%s: legsup = 0x%04x\n",
  353. __func__, legsup);
  354. goto reset_needed;
  355. }
  356. cmd = inw(base + UHCI_USBCMD);
  357. if ((cmd & UHCI_USBCMD_RUN) || !(cmd & UHCI_USBCMD_CONFIGURE) ||
  358. !(cmd & UHCI_USBCMD_EGSM)) {
  359. dev_dbg(&pdev->dev, "%s: cmd = 0x%04x\n",
  360. __func__, cmd);
  361. goto reset_needed;
  362. }
  363. intr = inw(base + UHCI_USBINTR);
  364. if (intr & (~UHCI_USBINTR_RESUME)) {
  365. dev_dbg(&pdev->dev, "%s: intr = 0x%04x\n",
  366. __func__, intr);
  367. goto reset_needed;
  368. }
  369. return 0;
  370. reset_needed:
  371. dev_dbg(&pdev->dev, "Performing full reset\n");
  372. uhci_reset_hc(pdev, base);
  373. return 1;
  374. }
  375. EXPORT_SYMBOL_GPL(uhci_check_and_reset_hc);
  376. static inline int io_type_enabled(struct pci_dev *pdev, unsigned int mask)
  377. {
  378. u16 cmd;
  379. return !pci_read_config_word(pdev, PCI_COMMAND, &cmd) && (cmd & mask);
  380. }
  381. #define pio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_IO)
  382. #define mmio_enabled(dev) io_type_enabled(dev, PCI_COMMAND_MEMORY)
  383. static void __devinit quirk_usb_handoff_uhci(struct pci_dev *pdev)
  384. {
  385. unsigned long base = 0;
  386. int i;
  387. if (!pio_enabled(pdev))
  388. return;
  389. for (i = 0; i < PCI_ROM_RESOURCE; i++)
  390. if ((pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  391. base = pci_resource_start(pdev, i);
  392. break;
  393. }
  394. if (base)
  395. uhci_check_and_reset_hc(pdev, base);
  396. }
  397. static int __devinit mmio_resource_enabled(struct pci_dev *pdev, int idx)
  398. {
  399. return pci_resource_start(pdev, idx) && mmio_enabled(pdev);
  400. }
  401. static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
  402. {
  403. void __iomem *base;
  404. u32 control;
  405. u32 fminterval;
  406. int cnt;
  407. if (!mmio_resource_enabled(pdev, 0))
  408. return;
  409. base = pci_ioremap_bar(pdev, 0);
  410. if (base == NULL)
  411. return;
  412. control = readl(base + OHCI_CONTROL);
  413. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  414. #ifdef __hppa__
  415. #define OHCI_CTRL_MASK (OHCI_CTRL_RWC | OHCI_CTRL_IR)
  416. #else
  417. #define OHCI_CTRL_MASK OHCI_CTRL_RWC
  418. if (control & OHCI_CTRL_IR) {
  419. int wait_time = 500; /* arbitrary; 5 seconds */
  420. writel(OHCI_INTR_OC, base + OHCI_INTRENABLE);
  421. writel(OHCI_OCR, base + OHCI_CMDSTATUS);
  422. while (wait_time > 0 &&
  423. readl(base + OHCI_CONTROL) & OHCI_CTRL_IR) {
  424. wait_time -= 10;
  425. msleep(10);
  426. }
  427. if (wait_time <= 0)
  428. dev_warn(&pdev->dev, "OHCI: BIOS handoff failed"
  429. " (BIOS bug?) %08x\n",
  430. readl(base + OHCI_CONTROL));
  431. }
  432. #endif
  433. /* disable interrupts */
  434. writel((u32) ~0, base + OHCI_INTRDISABLE);
  435. /* Reset the USB bus, if the controller isn't already in RESET */
  436. if (control & OHCI_HCFS) {
  437. /* Go into RESET, preserving RWC (and possibly IR) */
  438. writel(control & OHCI_CTRL_MASK, base + OHCI_CONTROL);
  439. readl(base + OHCI_CONTROL);
  440. /* drive bus reset for at least 50 ms (7.1.7.5) */
  441. msleep(50);
  442. }
  443. /* software reset of the controller, preserving HcFmInterval */
  444. fminterval = readl(base + OHCI_FMINTERVAL);
  445. writel(OHCI_HCR, base + OHCI_CMDSTATUS);
  446. /* reset requires max 10 us delay */
  447. for (cnt = 30; cnt > 0; --cnt) { /* ... allow extra time */
  448. if ((readl(base + OHCI_CMDSTATUS) & OHCI_HCR) == 0)
  449. break;
  450. udelay(1);
  451. }
  452. writel(fminterval, base + OHCI_FMINTERVAL);
  453. /* Now the controller is safely in SUSPEND and nothing can wake it up */
  454. iounmap(base);
  455. }
  456. static const struct dmi_system_id __devinitconst ehci_dmi_nohandoff_table[] = {
  457. {
  458. /* Pegatron Lucid (ExoPC) */
  459. .matches = {
  460. DMI_MATCH(DMI_BOARD_NAME, "EXOPG06411"),
  461. DMI_MATCH(DMI_BIOS_VERSION, "Lucid-CE-133"),
  462. },
  463. },
  464. {
  465. /* Pegatron Lucid (Ordissimo AIRIS) */
  466. .matches = {
  467. DMI_MATCH(DMI_BOARD_NAME, "M11JB"),
  468. DMI_MATCH(DMI_BIOS_VERSION, "Lucid-GE-133"),
  469. },
  470. },
  471. { }
  472. };
  473. static void __devinit ehci_bios_handoff(struct pci_dev *pdev,
  474. void __iomem *op_reg_base,
  475. u32 cap, u8 offset)
  476. {
  477. int try_handoff = 1, tried_handoff = 0;
  478. /* The Pegatron Lucid tablet sporadically waits for 98 seconds trying
  479. * the handoff on its unused controller. Skip it. */
  480. if (pdev->vendor == 0x8086 && pdev->device == 0x283a) {
  481. if (dmi_check_system(ehci_dmi_nohandoff_table))
  482. try_handoff = 0;
  483. }
  484. if (try_handoff && (cap & EHCI_USBLEGSUP_BIOS)) {
  485. dev_dbg(&pdev->dev, "EHCI: BIOS handoff\n");
  486. #if 0
  487. /* aleksey_gorelov@phoenix.com reports that some systems need SMI forced on,
  488. * but that seems dubious in general (the BIOS left it off intentionally)
  489. * and is known to prevent some systems from booting. so we won't do this
  490. * unless maybe we can determine when we're on a system that needs SMI forced.
  491. */
  492. /* BIOS workaround (?): be sure the pre-Linux code
  493. * receives the SMI
  494. */
  495. pci_read_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, &val);
  496. pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS,
  497. val | EHCI_USBLEGCTLSTS_SOOE);
  498. #endif
  499. /* some systems get upset if this semaphore is
  500. * set for any other reason than forcing a BIOS
  501. * handoff..
  502. */
  503. pci_write_config_byte(pdev, offset + 3, 1);
  504. }
  505. /* if boot firmware now owns EHCI, spin till it hands it over. */
  506. if (try_handoff) {
  507. int msec = 1000;
  508. while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
  509. tried_handoff = 1;
  510. msleep(10);
  511. msec -= 10;
  512. pci_read_config_dword(pdev, offset, &cap);
  513. }
  514. }
  515. if (cap & EHCI_USBLEGSUP_BIOS) {
  516. /* well, possibly buggy BIOS... try to shut it down,
  517. * and hope nothing goes too wrong
  518. */
  519. if (try_handoff)
  520. dev_warn(&pdev->dev, "EHCI: BIOS handoff failed"
  521. " (BIOS bug?) %08x\n", cap);
  522. pci_write_config_byte(pdev, offset + 2, 0);
  523. }
  524. /* just in case, always disable EHCI SMIs */
  525. pci_write_config_dword(pdev, offset + EHCI_USBLEGCTLSTS, 0);
  526. /* If the BIOS ever owned the controller then we can't expect
  527. * any power sessions to remain intact.
  528. */
  529. if (tried_handoff)
  530. writel(0, op_reg_base + EHCI_CONFIGFLAG);
  531. }
  532. static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
  533. {
  534. void __iomem *base, *op_reg_base;
  535. u32 hcc_params, cap, val;
  536. u8 offset, cap_length;
  537. int wait_time, count = 256/4;
  538. if (!mmio_resource_enabled(pdev, 0))
  539. return;
  540. base = pci_ioremap_bar(pdev, 0);
  541. if (base == NULL)
  542. return;
  543. cap_length = readb(base);
  544. op_reg_base = base + cap_length;
  545. /* EHCI 0.96 and later may have "extended capabilities"
  546. * spec section 5.1 explains the bios handoff, e.g. for
  547. * booting from USB disk or using a usb keyboard
  548. */
  549. hcc_params = readl(base + EHCI_HCC_PARAMS);
  550. offset = (hcc_params >> 8) & 0xff;
  551. while (offset && --count) {
  552. pci_read_config_dword(pdev, offset, &cap);
  553. switch (cap & 0xff) {
  554. case 1:
  555. ehci_bios_handoff(pdev, op_reg_base, cap, offset);
  556. break;
  557. case 0: /* Illegal reserved cap, set cap=0 so we exit */
  558. cap = 0; /* then fallthrough... */
  559. default:
  560. dev_warn(&pdev->dev, "EHCI: unrecognized capability "
  561. "%02x\n", cap & 0xff);
  562. }
  563. offset = (cap >> 8) & 0xff;
  564. }
  565. if (!count)
  566. dev_printk(KERN_DEBUG, &pdev->dev, "EHCI: capability loop?\n");
  567. /*
  568. * halt EHCI & disable its interrupts in any case
  569. */
  570. val = readl(op_reg_base + EHCI_USBSTS);
  571. if ((val & EHCI_USBSTS_HALTED) == 0) {
  572. val = readl(op_reg_base + EHCI_USBCMD);
  573. val &= ~EHCI_USBCMD_RUN;
  574. writel(val, op_reg_base + EHCI_USBCMD);
  575. wait_time = 2000;
  576. do {
  577. writel(0x3f, op_reg_base + EHCI_USBSTS);
  578. udelay(100);
  579. wait_time -= 100;
  580. val = readl(op_reg_base + EHCI_USBSTS);
  581. if ((val == ~(u32)0) || (val & EHCI_USBSTS_HALTED)) {
  582. break;
  583. }
  584. } while (wait_time > 0);
  585. }
  586. writel(0, op_reg_base + EHCI_USBINTR);
  587. writel(0x3f, op_reg_base + EHCI_USBSTS);
  588. iounmap(base);
  589. }
  590. /*
  591. * handshake - spin reading a register until handshake completes
  592. * @ptr: address of hc register to be read
  593. * @mask: bits to look at in result of read
  594. * @done: value of those bits when handshake succeeds
  595. * @wait_usec: timeout in microseconds
  596. * @delay_usec: delay in microseconds to wait between polling
  597. *
  598. * Polls a register every delay_usec microseconds.
  599. * Returns 0 when the mask bits have the value done.
  600. * Returns -ETIMEDOUT if this condition is not true after
  601. * wait_usec microseconds have passed.
  602. */
  603. static int handshake(void __iomem *ptr, u32 mask, u32 done,
  604. int wait_usec, int delay_usec)
  605. {
  606. u32 result;
  607. do {
  608. result = readl(ptr);
  609. result &= mask;
  610. if (result == done)
  611. return 0;
  612. udelay(delay_usec);
  613. wait_usec -= delay_usec;
  614. } while (wait_usec > 0);
  615. return -ETIMEDOUT;
  616. }
  617. bool usb_is_intel_switchable_xhci(struct pci_dev *pdev)
  618. {
  619. return pdev->class == PCI_CLASS_SERIAL_USB_XHCI &&
  620. pdev->vendor == PCI_VENDOR_ID_INTEL &&
  621. pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI;
  622. }
  623. EXPORT_SYMBOL_GPL(usb_is_intel_switchable_xhci);
  624. /*
  625. * Intel's Panther Point chipset has two host controllers (EHCI and xHCI) that
  626. * share some number of ports. These ports can be switched between either
  627. * controller. Not all of the ports under the EHCI host controller may be
  628. * switchable.
  629. *
  630. * The ports should be switched over to xHCI before PCI probes for any device
  631. * start. This avoids active devices under EHCI being disconnected during the
  632. * port switchover, which could cause loss of data on USB storage devices, or
  633. * failed boot when the root file system is on a USB mass storage device and is
  634. * enumerated under EHCI first.
  635. *
  636. * We write into the xHC's PCI configuration space in some Intel-specific
  637. * registers to switch the ports over. The USB 3.0 terminations and the USB
  638. * 2.0 data wires are switched separately. We want to enable the SuperSpeed
  639. * terminations before switching the USB 2.0 wires over, so that USB 3.0
  640. * devices connect at SuperSpeed, rather than at USB 2.0 speeds.
  641. */
  642. void usb_enable_xhci_ports(struct pci_dev *xhci_pdev)
  643. {
  644. u32 ports_available;
  645. ports_available = 0xffffffff;
  646. /* Write USB3_PSSEN, the USB 3.0 Port SuperSpeed Enable
  647. * Register, to turn on SuperSpeed terminations for all
  648. * available ports.
  649. */
  650. pci_write_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
  651. cpu_to_le32(ports_available));
  652. pci_read_config_dword(xhci_pdev, USB_INTEL_USB3_PSSEN,
  653. &ports_available);
  654. dev_dbg(&xhci_pdev->dev, "USB 3.0 ports that are now enabled "
  655. "under xHCI: 0x%x\n", ports_available);
  656. ports_available = 0xffffffff;
  657. /* Write XUSB2PR, the xHC USB 2.0 Port Routing Register, to
  658. * switch the USB 2.0 power and data lines over to the xHCI
  659. * host.
  660. */
  661. pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  662. cpu_to_le32(ports_available));
  663. pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
  664. &ports_available);
  665. dev_dbg(&xhci_pdev->dev, "USB 2.0 ports that are now switched over "
  666. "to xHCI: 0x%x\n", ports_available);
  667. }
  668. EXPORT_SYMBOL_GPL(usb_enable_xhci_ports);
  669. /**
  670. * PCI Quirks for xHCI.
  671. *
  672. * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
  673. * It signals to the BIOS that the OS wants control of the host controller,
  674. * and then waits 5 seconds for the BIOS to hand over control.
  675. * If we timeout, assume the BIOS is broken and take control anyway.
  676. */
  677. static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
  678. {
  679. void __iomem *base;
  680. int ext_cap_offset;
  681. void __iomem *op_reg_base;
  682. u32 val;
  683. int timeout;
  684. if (!mmio_resource_enabled(pdev, 0))
  685. return;
  686. base = ioremap_nocache(pci_resource_start(pdev, 0),
  687. pci_resource_len(pdev, 0));
  688. if (base == NULL)
  689. return;
  690. /*
  691. * Find the Legacy Support Capability register -
  692. * this is optional for xHCI host controllers.
  693. */
  694. ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
  695. do {
  696. if (!ext_cap_offset)
  697. /* We've reached the end of the extended capabilities */
  698. goto hc_init;
  699. val = readl(base + ext_cap_offset);
  700. if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
  701. break;
  702. ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
  703. } while (1);
  704. /* If the BIOS owns the HC, signal that the OS wants it, and wait */
  705. if (val & XHCI_HC_BIOS_OWNED) {
  706. writel(val | XHCI_HC_OS_OWNED, base + ext_cap_offset);
  707. /* Wait for 5 seconds with 10 microsecond polling interval */
  708. timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
  709. 0, 5000, 10);
  710. /* Assume a buggy BIOS and take HC ownership anyway */
  711. if (timeout) {
  712. dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
  713. " (BIOS bug ?) %08x\n", val);
  714. writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
  715. }
  716. }
  717. /* Disable any BIOS SMIs */
  718. writel(XHCI_LEGACY_DISABLE_SMI,
  719. base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
  720. if (usb_is_intel_switchable_xhci(pdev))
  721. usb_enable_xhci_ports(pdev);
  722. hc_init:
  723. op_reg_base = base + XHCI_HC_LENGTH(readl(base));
  724. /* Wait for the host controller to be ready before writing any
  725. * operational or runtime registers. Wait 5 seconds and no more.
  726. */
  727. timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
  728. 5000, 10);
  729. /* Assume a buggy HC and start HC initialization anyway */
  730. if (timeout) {
  731. val = readl(op_reg_base + XHCI_STS_OFFSET);
  732. dev_warn(&pdev->dev,
  733. "xHCI HW not ready after 5 sec (HC bug?) "
  734. "status = 0x%x\n", val);
  735. }
  736. /* Send the halt and disable interrupts command */
  737. val = readl(op_reg_base + XHCI_CMD_OFFSET);
  738. val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
  739. writel(val, op_reg_base + XHCI_CMD_OFFSET);
  740. /* Wait for the HC to halt - poll every 125 usec (one microframe). */
  741. timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
  742. XHCI_MAX_HALT_USEC, 125);
  743. if (timeout) {
  744. val = readl(op_reg_base + XHCI_STS_OFFSET);
  745. dev_warn(&pdev->dev,
  746. "xHCI HW did not halt within %d usec "
  747. "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
  748. }
  749. iounmap(base);
  750. }
  751. static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
  752. {
  753. if (pdev->class == PCI_CLASS_SERIAL_USB_UHCI)
  754. quirk_usb_handoff_uhci(pdev);
  755. else if (pdev->class == PCI_CLASS_SERIAL_USB_OHCI)
  756. quirk_usb_handoff_ohci(pdev);
  757. else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
  758. quirk_usb_disable_ehci(pdev);
  759. else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
  760. quirk_usb_handoff_xhci(pdev);
  761. }
  762. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);