pci-quirks.c 35 KB

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