pci-quirks.c 34 KB

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