pci-quirks.c 30 KB

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