xhci-pci.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * xHCI host controller driver PCI Bus Glue.
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include "xhci.h"
  26. /* Device for a quirk */
  27. #define PCI_VENDOR_ID_FRESCO_LOGIC 0x1b73
  28. #define PCI_DEVICE_ID_FRESCO_LOGIC_PDK 0x1000
  29. #define PCI_VENDOR_ID_ETRON 0x1b6f
  30. #define PCI_DEVICE_ID_ASROCK_P67 0x7023
  31. static const char hcd_name[] = "xhci_hcd";
  32. /* called after powerup, by probe or system-pm "wakeup" */
  33. static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
  34. {
  35. /*
  36. * TODO: Implement finding debug ports later.
  37. * TODO: see if there are any quirks that need to be added to handle
  38. * new extended capabilities.
  39. */
  40. /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
  41. if (!pci_set_mwi(pdev))
  42. xhci_dbg(xhci, "MWI active\n");
  43. xhci_dbg(xhci, "Finished xhci_pci_reinit\n");
  44. return 0;
  45. }
  46. static void xhci_pci_quirks(struct device *dev, struct xhci_hcd *xhci)
  47. {
  48. struct pci_dev *pdev = to_pci_dev(dev);
  49. /* Look for vendor-specific quirks */
  50. if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
  51. pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK) {
  52. if (pdev->revision == 0x0) {
  53. xhci->quirks |= XHCI_RESET_EP_QUIRK;
  54. xhci_dbg(xhci, "QUIRK: Fresco Logic xHC needs configure"
  55. " endpoint cmd after reset endpoint\n");
  56. }
  57. /* Fresco Logic confirms: all revisions of this chip do not
  58. * support MSI, even though some of them claim to in their PCI
  59. * capabilities.
  60. */
  61. xhci->quirks |= XHCI_BROKEN_MSI;
  62. xhci_dbg(xhci, "QUIRK: Fresco Logic revision %u "
  63. "has broken MSI implementation\n",
  64. pdev->revision);
  65. xhci->quirks |= XHCI_TRUST_TX_LENGTH;
  66. }
  67. if (pdev->vendor == PCI_VENDOR_ID_NEC)
  68. xhci->quirks |= XHCI_NEC_HOST;
  69. if (pdev->vendor == PCI_VENDOR_ID_AMD && xhci->hci_version == 0x96)
  70. xhci->quirks |= XHCI_AMD_0x96_HOST;
  71. /* AMD PLL quirk */
  72. if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_find_chipset_info())
  73. xhci->quirks |= XHCI_AMD_PLL_FIX;
  74. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  75. xhci->quirks |= XHCI_LPM_SUPPORT;
  76. xhci->quirks |= XHCI_INTEL_HOST;
  77. }
  78. if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
  79. pdev->device == PCI_DEVICE_ID_INTEL_PANTHERPOINT_XHCI) {
  80. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  81. xhci->quirks |= XHCI_EP_LIMIT_QUIRK;
  82. xhci->limit_active_eps = 64;
  83. xhci->quirks |= XHCI_SW_BW_CHECKING;
  84. }
  85. if (pdev->vendor == PCI_VENDOR_ID_ETRON &&
  86. pdev->device == PCI_DEVICE_ID_ASROCK_P67) {
  87. xhci->quirks |= XHCI_RESET_ON_RESUME;
  88. xhci_dbg(xhci, "QUIRK: Resetting on resume\n");
  89. }
  90. if (pdev->vendor == PCI_VENDOR_ID_VIA)
  91. xhci->quirks |= XHCI_RESET_ON_RESUME;
  92. }
  93. /* called during probe() after chip reset completes */
  94. static int xhci_pci_setup(struct usb_hcd *hcd)
  95. {
  96. struct xhci_hcd *xhci;
  97. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  98. int retval;
  99. retval = xhci_gen_setup(hcd, xhci_pci_quirks);
  100. if (retval)
  101. return retval;
  102. xhci = hcd_to_xhci(hcd);
  103. if (!usb_hcd_is_primary_hcd(hcd))
  104. return 0;
  105. pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
  106. xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
  107. /* Find any debug ports */
  108. retval = xhci_pci_reinit(xhci, pdev);
  109. if (!retval)
  110. return retval;
  111. kfree(xhci);
  112. return retval;
  113. }
  114. /*
  115. * We need to register our own PCI probe function (instead of the USB core's
  116. * function) in order to create a second roothub under xHCI.
  117. */
  118. static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  119. {
  120. int retval;
  121. struct xhci_hcd *xhci;
  122. struct hc_driver *driver;
  123. struct usb_hcd *hcd;
  124. driver = (struct hc_driver *)id->driver_data;
  125. /* Register the USB 2.0 roothub.
  126. * FIXME: USB core must know to register the USB 2.0 roothub first.
  127. * This is sort of silly, because we could just set the HCD driver flags
  128. * to say USB 2.0, but I'm not sure what the implications would be in
  129. * the other parts of the HCD code.
  130. */
  131. retval = usb_hcd_pci_probe(dev, id);
  132. if (retval)
  133. return retval;
  134. /* USB 2.0 roothub is stored in the PCI device now. */
  135. hcd = dev_get_drvdata(&dev->dev);
  136. xhci = hcd_to_xhci(hcd);
  137. xhci->shared_hcd = usb_create_shared_hcd(driver, &dev->dev,
  138. pci_name(dev), hcd);
  139. if (!xhci->shared_hcd) {
  140. retval = -ENOMEM;
  141. goto dealloc_usb2_hcd;
  142. }
  143. /* Set the xHCI pointer before xhci_pci_setup() (aka hcd_driver.reset)
  144. * is called by usb_add_hcd().
  145. */
  146. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  147. retval = usb_add_hcd(xhci->shared_hcd, dev->irq,
  148. IRQF_SHARED);
  149. if (retval)
  150. goto put_usb3_hcd;
  151. /* Roothub already marked as USB 3.0 speed */
  152. /* We know the LPM timeout algorithms for this host, let the USB core
  153. * enable and disable LPM for devices under the USB 3.0 roothub.
  154. */
  155. if (xhci->quirks & XHCI_LPM_SUPPORT)
  156. hcd_to_bus(xhci->shared_hcd)->root_hub->lpm_capable = 1;
  157. return 0;
  158. put_usb3_hcd:
  159. usb_put_hcd(xhci->shared_hcd);
  160. dealloc_usb2_hcd:
  161. usb_hcd_pci_remove(dev);
  162. return retval;
  163. }
  164. static void xhci_pci_remove(struct pci_dev *dev)
  165. {
  166. struct xhci_hcd *xhci;
  167. xhci = hcd_to_xhci(pci_get_drvdata(dev));
  168. if (xhci->shared_hcd) {
  169. usb_remove_hcd(xhci->shared_hcd);
  170. usb_put_hcd(xhci->shared_hcd);
  171. }
  172. usb_hcd_pci_remove(dev);
  173. kfree(xhci);
  174. }
  175. #ifdef CONFIG_PM
  176. static int xhci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  177. {
  178. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  179. int retval = 0;
  180. if (hcd->state != HC_STATE_SUSPENDED ||
  181. xhci->shared_hcd->state != HC_STATE_SUSPENDED)
  182. return -EINVAL;
  183. retval = xhci_suspend(xhci);
  184. return retval;
  185. }
  186. static int xhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
  187. {
  188. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  189. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  190. int retval = 0;
  191. /* The BIOS on systems with the Intel Panther Point chipset may or may
  192. * not support xHCI natively. That means that during system resume, it
  193. * may switch the ports back to EHCI so that users can use their
  194. * keyboard to select a kernel from GRUB after resume from hibernate.
  195. *
  196. * The BIOS is supposed to remember whether the OS had xHCI ports
  197. * enabled before resume, and switch the ports back to xHCI when the
  198. * BIOS/OS semaphore is written, but we all know we can't trust BIOS
  199. * writers.
  200. *
  201. * Unconditionally switch the ports back to xHCI after a system resume.
  202. * We can't tell whether the EHCI or xHCI controller will be resumed
  203. * first, so we have to do the port switchover in both drivers. Writing
  204. * a '1' to the port switchover registers should have no effect if the
  205. * port was already switched over.
  206. */
  207. if (usb_is_intel_switchable_xhci(pdev))
  208. usb_enable_xhci_ports(pdev);
  209. retval = xhci_resume(xhci, hibernated);
  210. return retval;
  211. }
  212. #endif /* CONFIG_PM */
  213. static const struct hc_driver xhci_pci_hc_driver = {
  214. .description = hcd_name,
  215. .product_desc = "xHCI Host Controller",
  216. .hcd_priv_size = sizeof(struct xhci_hcd *),
  217. /*
  218. * generic hardware linkage
  219. */
  220. .irq = xhci_irq,
  221. .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
  222. /*
  223. * basic lifecycle operations
  224. */
  225. .reset = xhci_pci_setup,
  226. .start = xhci_run,
  227. #ifdef CONFIG_PM
  228. .pci_suspend = xhci_pci_suspend,
  229. .pci_resume = xhci_pci_resume,
  230. #endif
  231. .stop = xhci_stop,
  232. .shutdown = xhci_shutdown,
  233. /*
  234. * managing i/o requests and associated device resources
  235. */
  236. .urb_enqueue = xhci_urb_enqueue,
  237. .urb_dequeue = xhci_urb_dequeue,
  238. .alloc_dev = xhci_alloc_dev,
  239. .free_dev = xhci_free_dev,
  240. .alloc_streams = xhci_alloc_streams,
  241. .free_streams = xhci_free_streams,
  242. .add_endpoint = xhci_add_endpoint,
  243. .drop_endpoint = xhci_drop_endpoint,
  244. .endpoint_reset = xhci_endpoint_reset,
  245. .check_bandwidth = xhci_check_bandwidth,
  246. .reset_bandwidth = xhci_reset_bandwidth,
  247. .address_device = xhci_address_device,
  248. .update_hub_device = xhci_update_hub_device,
  249. .reset_device = xhci_discover_or_reset_device,
  250. /*
  251. * scheduling support
  252. */
  253. .get_frame_number = xhci_get_frame,
  254. /* Root hub support */
  255. .hub_control = xhci_hub_control,
  256. .hub_status_data = xhci_hub_status_data,
  257. .bus_suspend = xhci_bus_suspend,
  258. .bus_resume = xhci_bus_resume,
  259. /*
  260. * call back when device connected and addressed
  261. */
  262. .update_device = xhci_update_device,
  263. .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
  264. .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
  265. .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
  266. };
  267. /*-------------------------------------------------------------------------*/
  268. /* PCI driver selection metadata; PCI hotplugging uses this */
  269. static const struct pci_device_id pci_ids[] = { {
  270. /* handle any USB 3.0 xHCI controller */
  271. PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
  272. .driver_data = (unsigned long) &xhci_pci_hc_driver,
  273. },
  274. { /* end: all zeroes */ }
  275. };
  276. MODULE_DEVICE_TABLE(pci, pci_ids);
  277. /* pci driver glue; this is a "new style" PCI driver module */
  278. static struct pci_driver xhci_pci_driver = {
  279. .name = (char *) hcd_name,
  280. .id_table = pci_ids,
  281. .probe = xhci_pci_probe,
  282. .remove = xhci_pci_remove,
  283. /* suspend and resume implemented later */
  284. .shutdown = usb_hcd_pci_shutdown,
  285. #ifdef CONFIG_PM_SLEEP
  286. .driver = {
  287. .pm = &usb_hcd_pci_pm_ops
  288. },
  289. #endif
  290. };
  291. int __init xhci_register_pci(void)
  292. {
  293. return pci_register_driver(&xhci_pci_driver);
  294. }
  295. void xhci_unregister_pci(void)
  296. {
  297. pci_unregister_driver(&xhci_pci_driver);
  298. }