ehci-pci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * EHCI HCD (Host Controller Driver) PCI Bus Glue.
  3. *
  4. * Copyright (c) 2000-2004 by David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #ifndef CONFIG_PCI
  21. #error "This file is PCI bus glue. CONFIG_PCI must be defined."
  22. #endif
  23. /*-------------------------------------------------------------------------*/
  24. /* called after powerup, by probe or system-pm "wakeup" */
  25. static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
  26. {
  27. u32 temp;
  28. int retval;
  29. /* optional debug port, normally in the first BAR */
  30. temp = pci_find_capability(pdev, 0x0a);
  31. if (temp) {
  32. pci_read_config_dword(pdev, temp, &temp);
  33. temp >>= 16;
  34. if ((temp & (3 << 13)) == (1 << 13)) {
  35. temp &= 0x1fff;
  36. ehci->debug = ehci_to_hcd(ehci)->regs + temp;
  37. temp = readl(&ehci->debug->control);
  38. ehci_info(ehci, "debug port %d%s\n",
  39. HCS_DEBUG_PORT(ehci->hcs_params),
  40. (temp & DBGP_ENABLED)
  41. ? " IN USE"
  42. : "");
  43. if (!(temp & DBGP_ENABLED))
  44. ehci->debug = NULL;
  45. }
  46. }
  47. /* we expect static quirk code to handle the "extended capabilities"
  48. * (currently just BIOS handoff) allowed starting with EHCI 0.96
  49. */
  50. /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
  51. retval = pci_set_mwi(pdev);
  52. if (!retval)
  53. ehci_dbg(ehci, "MWI active\n");
  54. ehci_port_power(ehci, 0);
  55. return 0;
  56. }
  57. /* called during probe() after chip reset completes */
  58. static int ehci_pci_setup(struct usb_hcd *hcd)
  59. {
  60. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  61. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  62. u32 temp;
  63. int retval;
  64. ehci->caps = hcd->regs;
  65. ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
  66. dbg_hcs_params(ehci, "reset");
  67. dbg_hcc_params(ehci, "reset");
  68. /* cache this readonly data; minimize chip reads */
  69. ehci->hcs_params = readl(&ehci->caps->hcs_params);
  70. retval = ehci_halt(ehci);
  71. if (retval)
  72. return retval;
  73. /* data structure init */
  74. retval = ehci_init(hcd);
  75. if (retval)
  76. return retval;
  77. /* NOTE: only the parts below this line are PCI-specific */
  78. switch (pdev->vendor) {
  79. case PCI_VENDOR_ID_TDI:
  80. if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
  81. ehci->is_tdi_rh_tt = 1;
  82. tdi_reset(ehci);
  83. }
  84. break;
  85. case PCI_VENDOR_ID_AMD:
  86. /* AMD8111 EHCI doesn't work, according to AMD errata */
  87. if (pdev->device == 0x7463) {
  88. ehci_info(ehci, "ignoring AMD8111 (errata)\n");
  89. retval = -EIO;
  90. goto done;
  91. }
  92. break;
  93. case PCI_VENDOR_ID_NVIDIA:
  94. switch (pdev->device) {
  95. /* NVidia reports that certain chips don't handle
  96. * QH, ITD, or SITD addresses above 2GB. (But TD,
  97. * data buffer, and periodic schedule are normal.)
  98. */
  99. case 0x003c: /* MCP04 */
  100. case 0x005b: /* CK804 */
  101. case 0x00d8: /* CK8 */
  102. case 0x00e8: /* CK8S */
  103. if (pci_set_consistent_dma_mask(pdev,
  104. DMA_31BIT_MASK) < 0)
  105. ehci_warn(ehci, "can't enable NVidia "
  106. "workaround for >2GB RAM\n");
  107. break;
  108. /* Some NForce2 chips have problems with selective suspend;
  109. * fixed in newer silicon.
  110. */
  111. case 0x0068:
  112. pci_read_config_dword(pdev, PCI_REVISION_ID, &temp);
  113. if ((temp & 0xff) < 0xa4)
  114. ehci->no_selective_suspend = 1;
  115. break;
  116. }
  117. break;
  118. }
  119. if (ehci_is_TDI(ehci))
  120. ehci_reset(ehci);
  121. /* at least the Genesys GL880S needs fixup here */
  122. temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
  123. temp &= 0x0f;
  124. if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
  125. ehci_dbg(ehci, "bogus port configuration: "
  126. "cc=%d x pcc=%d < ports=%d\n",
  127. HCS_N_CC(ehci->hcs_params),
  128. HCS_N_PCC(ehci->hcs_params),
  129. HCS_N_PORTS(ehci->hcs_params));
  130. switch (pdev->vendor) {
  131. case 0x17a0: /* GENESYS */
  132. /* GL880S: should be PORTS=2 */
  133. temp |= (ehci->hcs_params & ~0xf);
  134. ehci->hcs_params = temp;
  135. break;
  136. case PCI_VENDOR_ID_NVIDIA:
  137. /* NF4: should be PCC=10 */
  138. break;
  139. }
  140. }
  141. /* Serial Bus Release Number is at PCI 0x60 offset */
  142. pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
  143. /* Workaround current PCI init glitch: wakeup bits aren't
  144. * being set from PCI PM capability.
  145. */
  146. if (!device_can_wakeup(&pdev->dev)) {
  147. u16 port_wake;
  148. pci_read_config_word(pdev, 0x62, &port_wake);
  149. if (port_wake & 0x0001)
  150. device_init_wakeup(&pdev->dev, 1);
  151. }
  152. #ifdef CONFIG_USB_SUSPEND
  153. /* REVISIT: the controller works fine for wakeup iff the root hub
  154. * itself is "globally" suspended, but usbcore currently doesn't
  155. * understand such things.
  156. *
  157. * System suspend currently expects to be able to suspend the entire
  158. * device tree, device-at-a-time. If we failed selective suspend
  159. * reports, system suspend would fail; so the root hub code must claim
  160. * success. That's lying to usbcore, and it matters for for runtime
  161. * PM scenarios with selective suspend and remote wakeup...
  162. */
  163. if (ehci->no_selective_suspend && device_can_wakeup(&pdev->dev))
  164. ehci_warn(ehci, "selective suspend/wakeup unavailable\n");
  165. #endif
  166. retval = ehci_pci_reinit(ehci, pdev);
  167. done:
  168. return retval;
  169. }
  170. /*-------------------------------------------------------------------------*/
  171. #ifdef CONFIG_PM
  172. /* suspend/resume, section 4.3 */
  173. /* These routines rely on the PCI bus glue
  174. * to handle powerdown and wakeup, and currently also on
  175. * transceivers that don't need any software attention to set up
  176. * the right sort of wakeup.
  177. * Also they depend on separate root hub suspend/resume.
  178. */
  179. static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
  180. {
  181. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  182. unsigned long flags;
  183. int rc = 0;
  184. if (time_before(jiffies, ehci->next_statechange))
  185. msleep(10);
  186. /* Root hub was already suspended. Disable irq emission and
  187. * mark HW unaccessible, bail out if RH has been resumed. Use
  188. * the spinlock to properly synchronize with possible pending
  189. * RH suspend or resume activity.
  190. *
  191. * This is still racy as hcd->state is manipulated outside of
  192. * any locks =P But that will be a different fix.
  193. */
  194. spin_lock_irqsave (&ehci->lock, flags);
  195. if (hcd->state != HC_STATE_SUSPENDED) {
  196. rc = -EINVAL;
  197. goto bail;
  198. }
  199. writel (0, &ehci->regs->intr_enable);
  200. (void)readl(&ehci->regs->intr_enable);
  201. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  202. bail:
  203. spin_unlock_irqrestore (&ehci->lock, flags);
  204. // could save FLADJ in case of Vaux power loss
  205. // ... we'd only use it to handle clock skew
  206. return rc;
  207. }
  208. static int ehci_pci_resume(struct usb_hcd *hcd)
  209. {
  210. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  211. unsigned port;
  212. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  213. int retval = -EINVAL;
  214. // maybe restore FLADJ
  215. if (time_before(jiffies, ehci->next_statechange))
  216. msleep(100);
  217. /* Mark hardware accessible again as we are out of D3 state by now */
  218. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  219. /* If CF is clear, we lost PCI Vaux power and need to restart. */
  220. if (readl(&ehci->regs->configured_flag) != FLAG_CF)
  221. goto restart;
  222. /* If any port is suspended (or owned by the companion),
  223. * we know we can/must resume the HC (and mustn't reset it).
  224. * We just defer that to the root hub code.
  225. */
  226. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  227. u32 status;
  228. port--;
  229. status = readl(&ehci->regs->port_status [port]);
  230. if (!(status & PORT_POWER))
  231. continue;
  232. if (status & (PORT_SUSPEND | PORT_RESUME | PORT_OWNER)) {
  233. usb_hcd_resume_root_hub(hcd);
  234. return 0;
  235. }
  236. }
  237. restart:
  238. ehci_dbg(ehci, "lost power, restarting\n");
  239. usb_root_hub_lost_power(hcd->self.root_hub);
  240. /* Else reset, to cope with power loss or flush-to-storage
  241. * style "resume" having let BIOS kick in during reboot.
  242. */
  243. (void) ehci_halt(ehci);
  244. (void) ehci_reset(ehci);
  245. (void) ehci_pci_reinit(ehci, pdev);
  246. /* emptying the schedule aborts any urbs */
  247. spin_lock_irq(&ehci->lock);
  248. if (ehci->reclaim)
  249. ehci->reclaim_ready = 1;
  250. ehci_work(ehci, NULL);
  251. spin_unlock_irq(&ehci->lock);
  252. /* restart; khubd will disconnect devices */
  253. retval = ehci_run(hcd);
  254. /* here we "know" root ports should always stay powered */
  255. ehci_port_power(ehci, 1);
  256. return retval;
  257. }
  258. #endif
  259. static const struct hc_driver ehci_pci_hc_driver = {
  260. .description = hcd_name,
  261. .product_desc = "EHCI Host Controller",
  262. .hcd_priv_size = sizeof(struct ehci_hcd),
  263. /*
  264. * generic hardware linkage
  265. */
  266. .irq = ehci_irq,
  267. .flags = HCD_MEMORY | HCD_USB2,
  268. /*
  269. * basic lifecycle operations
  270. */
  271. .reset = ehci_pci_setup,
  272. .start = ehci_run,
  273. #ifdef CONFIG_PM
  274. .suspend = ehci_pci_suspend,
  275. .resume = ehci_pci_resume,
  276. #endif
  277. .stop = ehci_stop,
  278. /*
  279. * managing i/o requests and associated device resources
  280. */
  281. .urb_enqueue = ehci_urb_enqueue,
  282. .urb_dequeue = ehci_urb_dequeue,
  283. .endpoint_disable = ehci_endpoint_disable,
  284. /*
  285. * scheduling support
  286. */
  287. .get_frame_number = ehci_get_frame,
  288. /*
  289. * root hub support
  290. */
  291. .hub_status_data = ehci_hub_status_data,
  292. .hub_control = ehci_hub_control,
  293. .bus_suspend = ehci_bus_suspend,
  294. .bus_resume = ehci_bus_resume,
  295. };
  296. /*-------------------------------------------------------------------------*/
  297. /* PCI driver selection metadata; PCI hotplugging uses this */
  298. static const struct pci_device_id pci_ids [] = { {
  299. /* handle any USB 2.0 EHCI controller */
  300. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
  301. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  302. },
  303. { /* end: all zeroes */ }
  304. };
  305. MODULE_DEVICE_TABLE(pci, pci_ids);
  306. /* pci driver glue; this is a "new style" PCI driver module */
  307. static struct pci_driver ehci_pci_driver = {
  308. .name = (char *) hcd_name,
  309. .id_table = pci_ids,
  310. .probe = usb_hcd_pci_probe,
  311. .remove = usb_hcd_pci_remove,
  312. #ifdef CONFIG_PM
  313. .suspend = usb_hcd_pci_suspend,
  314. .resume = usb_hcd_pci_resume,
  315. #endif
  316. };
  317. static int __init ehci_hcd_pci_init(void)
  318. {
  319. if (usb_disabled())
  320. return -ENODEV;
  321. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  322. hcd_name,
  323. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  324. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  325. return pci_register_driver(&ehci_pci_driver);
  326. }
  327. module_init(ehci_hcd_pci_init);
  328. static void __exit ehci_hcd_pci_cleanup(void)
  329. {
  330. pci_unregister_driver(&ehci_pci_driver);
  331. }
  332. module_exit(ehci_hcd_pci_cleanup);