ehci-pci.c 10 KB

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