pci-quirks.c 34 KB

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