pci-quirks.c 34 KB

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