pci-quirks.c 24 KB

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