pci-quirks.c 30 KB

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