pci-quirks.c 29 KB

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