pci-quirks.c 35 KB

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