pci-quirks.c 30 KB

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