pci-quirks.c 26 KB

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