ehci-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. /* EHCI 0.96 (and later) section 5.1 says how to kick BIOS/SMM/...
  25. * off the controller (maybe it can boot from highspeed USB disks).
  26. */
  27. static int bios_handoff(struct ehci_hcd *ehci, int where, u32 cap)
  28. {
  29. struct pci_dev *pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
  30. /* always say Linux will own the hardware */
  31. pci_write_config_byte(pdev, where + 3, 1);
  32. /* maybe wait a while for BIOS to respond */
  33. if (cap & (1 << 16)) {
  34. int msec = 5000;
  35. do {
  36. msleep(10);
  37. msec -= 10;
  38. pci_read_config_dword(pdev, where, &cap);
  39. } while ((cap & (1 << 16)) && msec);
  40. if (cap & (1 << 16)) {
  41. ehci_err(ehci, "BIOS handoff failed (%d, %08x)\n",
  42. where, cap);
  43. // some BIOS versions seem buggy...
  44. // return 1;
  45. ehci_warn(ehci, "continuing after BIOS bug...\n");
  46. /* disable all SMIs, and clear "BIOS owns" flag */
  47. pci_write_config_dword(pdev, where + 4, 0);
  48. pci_write_config_byte(pdev, where + 2, 0);
  49. } else
  50. ehci_dbg(ehci, "BIOS handoff succeeded\n");
  51. }
  52. return 0;
  53. }
  54. /* called after powerup, by probe or system-pm "wakeup" */
  55. static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
  56. {
  57. u32 temp;
  58. int retval;
  59. unsigned count = 256/4;
  60. /* optional debug port, normally in the first BAR */
  61. temp = pci_find_capability(pdev, 0x0a);
  62. if (temp) {
  63. pci_read_config_dword(pdev, temp, &temp);
  64. temp >>= 16;
  65. if ((temp & (3 << 13)) == (1 << 13)) {
  66. temp &= 0x1fff;
  67. ehci->debug = ehci_to_hcd(ehci)->regs + temp;
  68. temp = readl(&ehci->debug->control);
  69. ehci_info(ehci, "debug port %d%s\n",
  70. HCS_DEBUG_PORT(ehci->hcs_params),
  71. (temp & DBGP_ENABLED)
  72. ? " IN USE"
  73. : "");
  74. if (!(temp & DBGP_ENABLED))
  75. ehci->debug = NULL;
  76. }
  77. }
  78. temp = HCC_EXT_CAPS(readl(&ehci->caps->hcc_params));
  79. /* EHCI 0.96 and later may have "extended capabilities" */
  80. while (temp && count--) {
  81. u32 cap;
  82. pci_read_config_dword(pdev, temp, &cap);
  83. ehci_dbg(ehci, "capability %04x at %02x\n", cap, temp);
  84. switch (cap & 0xff) {
  85. case 1: /* BIOS/SMM/... handoff */
  86. if (bios_handoff(ehci, temp, cap) != 0)
  87. return -EOPNOTSUPP;
  88. break;
  89. case 0: /* illegal reserved capability */
  90. ehci_dbg(ehci, "illegal capability!\n");
  91. cap = 0;
  92. /* FALLTHROUGH */
  93. default: /* unknown */
  94. break;
  95. }
  96. temp = (cap >> 8) & 0xff;
  97. }
  98. if (!count) {
  99. ehci_err(ehci, "bogus capabilities ... PCI problems!\n");
  100. return -EIO;
  101. }
  102. /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
  103. retval = pci_set_mwi(pdev);
  104. if (!retval)
  105. ehci_dbg(ehci, "MWI active\n");
  106. ehci_port_power(ehci, 0);
  107. return 0;
  108. }
  109. /* called by khubd or root hub (re)init threads; leaves HC in halt state */
  110. static int ehci_pci_reset(struct usb_hcd *hcd)
  111. {
  112. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  113. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  114. u32 temp;
  115. int retval;
  116. ehci->caps = hcd->regs;
  117. ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
  118. dbg_hcs_params(ehci, "reset");
  119. dbg_hcc_params(ehci, "reset");
  120. /* cache this readonly data; minimize chip reads */
  121. ehci->hcs_params = readl(&ehci->caps->hcs_params);
  122. retval = ehci_halt(ehci);
  123. if (retval)
  124. return retval;
  125. /* NOTE: only the parts below this line are PCI-specific */
  126. switch (pdev->vendor) {
  127. case PCI_VENDOR_ID_TDI:
  128. if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
  129. ehci->is_tdi_rh_tt = 1;
  130. tdi_reset(ehci);
  131. }
  132. break;
  133. case PCI_VENDOR_ID_AMD:
  134. /* AMD8111 EHCI doesn't work, according to AMD errata */
  135. if (pdev->device == 0x7463) {
  136. ehci_info(ehci, "ignoring AMD8111 (errata)\n");
  137. return -EIO;
  138. }
  139. break;
  140. case PCI_VENDOR_ID_NVIDIA:
  141. /* NVidia reports that certain chips don't handle
  142. * QH, ITD, or SITD addresses above 2GB. (But TD,
  143. * data buffer, and periodic schedule are normal.)
  144. */
  145. switch (pdev->device) {
  146. case 0x003c: /* MCP04 */
  147. case 0x005b: /* CK804 */
  148. case 0x00d8: /* CK8 */
  149. case 0x00e8: /* CK8S */
  150. if (pci_set_consistent_dma_mask(pdev,
  151. DMA_31BIT_MASK) < 0)
  152. ehci_warn(ehci, "can't enable NVidia "
  153. "workaround for >2GB RAM\n");
  154. break;
  155. }
  156. break;
  157. }
  158. if (ehci_is_TDI(ehci))
  159. ehci_reset(ehci);
  160. /* at least the Genesys GL880S needs fixup here */
  161. temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
  162. temp &= 0x0f;
  163. if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
  164. ehci_dbg(ehci, "bogus port configuration: "
  165. "cc=%d x pcc=%d < ports=%d\n",
  166. HCS_N_CC(ehci->hcs_params),
  167. HCS_N_PCC(ehci->hcs_params),
  168. HCS_N_PORTS(ehci->hcs_params));
  169. switch (pdev->vendor) {
  170. case 0x17a0: /* GENESYS */
  171. /* GL880S: should be PORTS=2 */
  172. temp |= (ehci->hcs_params & ~0xf);
  173. ehci->hcs_params = temp;
  174. break;
  175. case PCI_VENDOR_ID_NVIDIA:
  176. /* NF4: should be PCC=10 */
  177. break;
  178. }
  179. }
  180. /* Serial Bus Release Number is at PCI 0x60 offset */
  181. pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
  182. /* REVISIT: per-port wake capability (PCI 0x62) currently unused */
  183. retval = ehci_pci_reinit(ehci, pdev);
  184. /* finish init */
  185. return ehci_init(hcd);
  186. }
  187. /*-------------------------------------------------------------------------*/
  188. #ifdef CONFIG_PM
  189. /* suspend/resume, section 4.3 */
  190. /* These routines rely on the PCI bus glue
  191. * to handle powerdown and wakeup, and currently also on
  192. * transceivers that don't need any software attention to set up
  193. * the right sort of wakeup.
  194. * Also they depend on separate root hub suspend/resume.
  195. */
  196. static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
  197. {
  198. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  199. unsigned long flags;
  200. int rc = 0;
  201. if (time_before(jiffies, ehci->next_statechange))
  202. msleep(10);
  203. /* Root hub was already suspended. Disable irq emission and
  204. * mark HW unaccessible, bail out if RH has been resumed. Use
  205. * the spinlock to properly synchronize with possible pending
  206. * RH suspend or resume activity.
  207. *
  208. * This is still racy as hcd->state is manipulated outside of
  209. * any locks =P But that will be a different fix.
  210. */
  211. spin_lock_irqsave (&ehci->lock, flags);
  212. if (hcd->state != HC_STATE_SUSPENDED) {
  213. rc = -EINVAL;
  214. goto bail;
  215. }
  216. writel (0, &ehci->regs->intr_enable);
  217. (void)readl(&ehci->regs->intr_enable);
  218. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  219. bail:
  220. spin_unlock_irqrestore (&ehci->lock, flags);
  221. // could save FLADJ in case of Vaux power loss
  222. // ... we'd only use it to handle clock skew
  223. return rc;
  224. }
  225. static int ehci_pci_resume(struct usb_hcd *hcd)
  226. {
  227. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  228. unsigned port;
  229. struct usb_device *root = hcd->self.root_hub;
  230. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  231. int retval = -EINVAL;
  232. // maybe restore FLADJ
  233. if (time_before(jiffies, ehci->next_statechange))
  234. msleep(100);
  235. /* Mark hardware accessible again as we are out of D3 state by now */
  236. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  237. /* If CF is clear, we lost PCI Vaux power and need to restart. */
  238. if (readl(&ehci->regs->configured_flag) != FLAG_CF)
  239. goto restart;
  240. /* If any port is suspended (or owned by the companion),
  241. * we know we can/must resume the HC (and mustn't reset it).
  242. * We just defer that to the root hub code.
  243. */
  244. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  245. u32 status;
  246. port--;
  247. status = readl(&ehci->regs->port_status [port]);
  248. if (!(status & PORT_POWER))
  249. continue;
  250. if (status & (PORT_SUSPEND | PORT_RESUME | PORT_OWNER)) {
  251. usb_hcd_resume_root_hub(hcd);
  252. return 0;
  253. }
  254. }
  255. restart:
  256. ehci_dbg(ehci, "lost power, restarting\n");
  257. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  258. port--;
  259. if (!root->children [port])
  260. continue;
  261. usb_set_device_state(root->children[port],
  262. USB_STATE_NOTATTACHED);
  263. }
  264. /* Else reset, to cope with power loss or flush-to-storage
  265. * style "resume" having let BIOS kick in during reboot.
  266. */
  267. (void) ehci_halt(ehci);
  268. (void) ehci_reset(ehci);
  269. (void) ehci_pci_reinit(ehci, pdev);
  270. /* emptying the schedule aborts any urbs */
  271. spin_lock_irq(&ehci->lock);
  272. if (ehci->reclaim)
  273. ehci->reclaim_ready = 1;
  274. ehci_work(ehci, NULL);
  275. spin_unlock_irq(&ehci->lock);
  276. /* restart; khubd will disconnect devices */
  277. retval = ehci_run(hcd);
  278. /* here we "know" root ports should always stay powered */
  279. ehci_port_power(ehci, 1);
  280. return retval;
  281. }
  282. #endif
  283. static const struct hc_driver ehci_pci_hc_driver = {
  284. .description = hcd_name,
  285. .product_desc = "EHCI Host Controller",
  286. .hcd_priv_size = sizeof(struct ehci_hcd),
  287. /*
  288. * generic hardware linkage
  289. */
  290. .irq = ehci_irq,
  291. .flags = HCD_MEMORY | HCD_USB2,
  292. /*
  293. * basic lifecycle operations
  294. */
  295. .reset = ehci_pci_reset,
  296. .start = ehci_run,
  297. #ifdef CONFIG_PM
  298. .suspend = ehci_pci_suspend,
  299. .resume = ehci_pci_resume,
  300. #endif
  301. .stop = ehci_stop,
  302. /*
  303. * managing i/o requests and associated device resources
  304. */
  305. .urb_enqueue = ehci_urb_enqueue,
  306. .urb_dequeue = ehci_urb_dequeue,
  307. .endpoint_disable = ehci_endpoint_disable,
  308. /*
  309. * scheduling support
  310. */
  311. .get_frame_number = ehci_get_frame,
  312. /*
  313. * root hub support
  314. */
  315. .hub_status_data = ehci_hub_status_data,
  316. .hub_control = ehci_hub_control,
  317. .bus_suspend = ehci_bus_suspend,
  318. .bus_resume = ehci_bus_resume,
  319. };
  320. /*-------------------------------------------------------------------------*/
  321. /* PCI driver selection metadata; PCI hotplugging uses this */
  322. static const struct pci_device_id pci_ids [] = { {
  323. /* handle any USB 2.0 EHCI controller */
  324. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
  325. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  326. },
  327. { /* end: all zeroes */ }
  328. };
  329. MODULE_DEVICE_TABLE(pci, pci_ids);
  330. /* pci driver glue; this is a "new style" PCI driver module */
  331. static struct pci_driver ehci_pci_driver = {
  332. .name = (char *) hcd_name,
  333. .id_table = pci_ids,
  334. .probe = usb_hcd_pci_probe,
  335. .remove = usb_hcd_pci_remove,
  336. #ifdef CONFIG_PM
  337. .suspend = usb_hcd_pci_suspend,
  338. .resume = usb_hcd_pci_resume,
  339. #endif
  340. };
  341. static int __init ehci_hcd_pci_init(void)
  342. {
  343. if (usb_disabled())
  344. return -ENODEV;
  345. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  346. hcd_name,
  347. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  348. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  349. return pci_register_driver(&ehci_pci_driver);
  350. }
  351. module_init(ehci_hcd_pci_init);
  352. static void __exit ehci_hcd_pci_cleanup(void)
  353. {
  354. pci_unregister_driver(&ehci_pci_driver);
  355. }
  356. module_exit(ehci_hcd_pci_cleanup);