ehci-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. u32 temp;
  59. unsigned count = 256/4;
  60. spin_lock_init (&ehci->lock);
  61. ehci->caps = hcd->regs;
  62. ehci->regs = hcd->regs + HC_LENGTH (readl (&ehci->caps->hc_capbase));
  63. dbg_hcs_params (ehci, "reset");
  64. dbg_hcc_params (ehci, "reset");
  65. /* cache this readonly data; minimize chip reads */
  66. ehci->hcs_params = readl (&ehci->caps->hcs_params);
  67. if (hcd->self.controller->bus == &pci_bus_type) {
  68. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  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. } else
  121. temp = 0;
  122. /* EHCI 0.96 and later may have "extended capabilities" */
  123. while (temp && count--) {
  124. u32 cap;
  125. pci_read_config_dword (to_pci_dev(hcd->self.controller),
  126. temp, &cap);
  127. ehci_dbg (ehci, "capability %04x at %02x\n", cap, temp);
  128. switch (cap & 0xff) {
  129. case 1: /* BIOS/SMM/... handoff */
  130. if (bios_handoff (ehci, temp, cap) != 0)
  131. return -EOPNOTSUPP;
  132. break;
  133. case 0: /* illegal reserved capability */
  134. ehci_warn (ehci, "illegal capability!\n");
  135. cap = 0;
  136. /* FALLTHROUGH */
  137. default: /* unknown */
  138. break;
  139. }
  140. temp = (cap >> 8) & 0xff;
  141. }
  142. if (!count) {
  143. ehci_err (ehci, "bogus capabilities ... PCI problems!\n");
  144. return -EIO;
  145. }
  146. if (ehci_is_TDI(ehci))
  147. ehci_reset (ehci);
  148. ehci_port_power (ehci, 0);
  149. /* at least the Genesys GL880S needs fixup here */
  150. temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
  151. temp &= 0x0f;
  152. if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
  153. ehci_dbg (ehci, "bogus port configuration: "
  154. "cc=%d x pcc=%d < ports=%d\n",
  155. HCS_N_CC(ehci->hcs_params),
  156. HCS_N_PCC(ehci->hcs_params),
  157. HCS_N_PORTS(ehci->hcs_params));
  158. if (hcd->self.controller->bus == &pci_bus_type) {
  159. struct pci_dev *pdev;
  160. pdev = to_pci_dev(hcd->self.controller);
  161. switch (pdev->vendor) {
  162. case 0x17a0: /* GENESYS */
  163. /* GL880S: should be PORTS=2 */
  164. temp |= (ehci->hcs_params & ~0xf);
  165. ehci->hcs_params = temp;
  166. break;
  167. case PCI_VENDOR_ID_NVIDIA:
  168. /* NF4: should be PCC=10 */
  169. break;
  170. }
  171. }
  172. }
  173. /* force HC to halt state */
  174. return ehci_halt (ehci);
  175. }
  176. static int ehci_pci_start (struct usb_hcd *hcd)
  177. {
  178. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  179. int result = 0;
  180. if (hcd->self.controller->bus == &pci_bus_type) {
  181. struct pci_dev *pdev;
  182. u16 port_wake;
  183. pdev = to_pci_dev(hcd->self.controller);
  184. /* Serial Bus Release Number is at PCI 0x60 offset */
  185. pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
  186. /* port wake capability, reported by boot firmware */
  187. pci_read_config_word(pdev, 0x62, &port_wake);
  188. hcd->can_wakeup = (port_wake & 1) != 0;
  189. /* help hc dma work well with cachelines */
  190. result = pci_set_mwi(pdev);
  191. if (result)
  192. ehci_dbg(ehci, "unable to enable MWI - not fatal.\n");
  193. }
  194. return ehci_run (hcd);
  195. }
  196. /* always called by thread; normally rmmod */
  197. static void ehci_pci_stop (struct usb_hcd *hcd)
  198. {
  199. ehci_stop (hcd);
  200. }
  201. /*-------------------------------------------------------------------------*/
  202. #ifdef CONFIG_PM
  203. /* suspend/resume, section 4.3 */
  204. /* These routines rely on the bus (pci, platform, etc)
  205. * to handle powerdown and wakeup, and currently also on
  206. * transceivers that don't need any software attention to set up
  207. * the right sort of wakeup.
  208. */
  209. static int ehci_pci_suspend (struct usb_hcd *hcd, pm_message_t message)
  210. {
  211. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  212. if (time_before (jiffies, ehci->next_statechange))
  213. msleep (100);
  214. #ifdef CONFIG_USB_SUSPEND
  215. (void) usb_suspend_device (hcd->self.root_hub);
  216. #else
  217. usb_lock_device (hcd->self.root_hub);
  218. (void) ehci_bus_suspend (hcd);
  219. usb_unlock_device (hcd->self.root_hub);
  220. #endif
  221. // save (PCI) FLADJ in case of Vaux power loss
  222. // ... we'd only use it to handle clock skew
  223. return 0;
  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. int retval = -EINVAL;
  231. // maybe restore (PCI) FLADJ
  232. if (time_before (jiffies, ehci->next_statechange))
  233. msleep (100);
  234. /* If any port is suspended (or owned by the companion),
  235. * we know we can/must resume the HC (and mustn't reset it).
  236. */
  237. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; ) {
  238. u32 status;
  239. port--;
  240. status = readl (&ehci->regs->port_status [port]);
  241. if (!(status & PORT_POWER))
  242. continue;
  243. if (status & (PORT_SUSPEND | PORT_OWNER)) {
  244. down (&hcd->self.root_hub->serialize);
  245. retval = ehci_bus_resume (hcd);
  246. up (&hcd->self.root_hub->serialize);
  247. break;
  248. }
  249. if (!root->children [port])
  250. continue;
  251. dbg_port (ehci, __FUNCTION__, port + 1, status);
  252. usb_set_device_state (root->children[port],
  253. USB_STATE_NOTATTACHED);
  254. }
  255. /* Else reset, to cope with power loss or flush-to-storage
  256. * style "resume" having activated BIOS during reboot.
  257. */
  258. if (port == 0) {
  259. (void) ehci_halt (ehci);
  260. (void) ehci_reset (ehci);
  261. (void) ehci_pci_reset (hcd);
  262. /* emptying the schedule aborts any urbs */
  263. spin_lock_irq (&ehci->lock);
  264. if (ehci->reclaim)
  265. ehci->reclaim_ready = 1;
  266. ehci_work (ehci, NULL);
  267. spin_unlock_irq (&ehci->lock);
  268. /* restart; khubd will disconnect devices */
  269. retval = ehci_run (hcd);
  270. /* here we "know" root ports should always stay powered;
  271. * but some controllers may lose all power.
  272. */
  273. ehci_port_power (ehci, 1);
  274. }
  275. return retval;
  276. }
  277. #endif
  278. static const struct hc_driver ehci_pci_hc_driver = {
  279. .description = hcd_name,
  280. .product_desc = "EHCI Host Controller",
  281. .hcd_priv_size = sizeof(struct ehci_hcd),
  282. /*
  283. * generic hardware linkage
  284. */
  285. .irq = ehci_irq,
  286. .flags = HCD_MEMORY | HCD_USB2,
  287. /*
  288. * basic lifecycle operations
  289. */
  290. .reset = ehci_pci_reset,
  291. .start = ehci_pci_start,
  292. #ifdef CONFIG_PM
  293. .suspend = ehci_pci_suspend,
  294. .resume = ehci_pci_resume,
  295. #endif
  296. .stop = ehci_pci_stop,
  297. /*
  298. * managing i/o requests and associated device resources
  299. */
  300. .urb_enqueue = ehci_urb_enqueue,
  301. .urb_dequeue = ehci_urb_dequeue,
  302. .endpoint_disable = ehci_endpoint_disable,
  303. /*
  304. * scheduling support
  305. */
  306. .get_frame_number = ehci_get_frame,
  307. /*
  308. * root hub support
  309. */
  310. .hub_status_data = ehci_hub_status_data,
  311. .hub_control = ehci_hub_control,
  312. .bus_suspend = ehci_bus_suspend,
  313. .bus_resume = ehci_bus_resume,
  314. };
  315. /*-------------------------------------------------------------------------*/
  316. /* PCI driver selection metadata; PCI hotplugging uses this */
  317. static const struct pci_device_id pci_ids [] = { {
  318. /* handle any USB 2.0 EHCI controller */
  319. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
  320. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  321. },
  322. { /* end: all zeroes */ }
  323. };
  324. MODULE_DEVICE_TABLE (pci, pci_ids);
  325. /* pci driver glue; this is a "new style" PCI driver module */
  326. static struct pci_driver ehci_pci_driver = {
  327. .name = (char *) hcd_name,
  328. .id_table = pci_ids,
  329. .probe = usb_hcd_pci_probe,
  330. .remove = usb_hcd_pci_remove,
  331. #ifdef CONFIG_PM
  332. .suspend = usb_hcd_pci_suspend,
  333. .resume = usb_hcd_pci_resume,
  334. #endif
  335. };
  336. static int __init ehci_hcd_pci_init (void)
  337. {
  338. if (usb_disabled())
  339. return -ENODEV;
  340. pr_debug ("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  341. hcd_name,
  342. sizeof (struct ehci_qh), sizeof (struct ehci_qtd),
  343. sizeof (struct ehci_itd), sizeof (struct ehci_sitd));
  344. return pci_register_driver (&ehci_pci_driver);
  345. }
  346. module_init (ehci_hcd_pci_init);
  347. static void __exit ehci_hcd_pci_cleanup (void)
  348. {
  349. pci_unregister_driver (&ehci_pci_driver);
  350. }
  351. module_exit (ehci_hcd_pci_cleanup);