ehci-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 during probe() after chip reset completes */
  110. static int ehci_pci_setup(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. /* data structure init */
  126. retval = ehci_init(hcd);
  127. if (retval)
  128. return retval;
  129. /* NOTE: only the parts below this line are PCI-specific */
  130. switch (pdev->vendor) {
  131. case PCI_VENDOR_ID_TDI:
  132. if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
  133. ehci->is_tdi_rh_tt = 1;
  134. tdi_reset(ehci);
  135. }
  136. break;
  137. case PCI_VENDOR_ID_AMD:
  138. /* AMD8111 EHCI doesn't work, according to AMD errata */
  139. if (pdev->device == 0x7463) {
  140. ehci_info(ehci, "ignoring AMD8111 (errata)\n");
  141. retval = -EIO;
  142. goto done;
  143. }
  144. break;
  145. case PCI_VENDOR_ID_NVIDIA:
  146. /* NVidia reports that certain chips don't handle
  147. * QH, ITD, or SITD addresses above 2GB. (But TD,
  148. * data buffer, and periodic schedule are normal.)
  149. */
  150. switch (pdev->device) {
  151. case 0x003c: /* MCP04 */
  152. case 0x005b: /* CK804 */
  153. case 0x00d8: /* CK8 */
  154. case 0x00e8: /* CK8S */
  155. if (pci_set_consistent_dma_mask(pdev,
  156. DMA_31BIT_MASK) < 0)
  157. ehci_warn(ehci, "can't enable NVidia "
  158. "workaround for >2GB RAM\n");
  159. break;
  160. }
  161. break;
  162. }
  163. if (ehci_is_TDI(ehci))
  164. ehci_reset(ehci);
  165. /* at least the Genesys GL880S needs fixup here */
  166. temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
  167. temp &= 0x0f;
  168. if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
  169. ehci_dbg(ehci, "bogus port configuration: "
  170. "cc=%d x pcc=%d < ports=%d\n",
  171. HCS_N_CC(ehci->hcs_params),
  172. HCS_N_PCC(ehci->hcs_params),
  173. HCS_N_PORTS(ehci->hcs_params));
  174. switch (pdev->vendor) {
  175. case 0x17a0: /* GENESYS */
  176. /* GL880S: should be PORTS=2 */
  177. temp |= (ehci->hcs_params & ~0xf);
  178. ehci->hcs_params = temp;
  179. break;
  180. case PCI_VENDOR_ID_NVIDIA:
  181. /* NF4: should be PCC=10 */
  182. break;
  183. }
  184. }
  185. /* Serial Bus Release Number is at PCI 0x60 offset */
  186. pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
  187. /* REVISIT: per-port wake capability (PCI 0x62) currently unused */
  188. retval = ehci_pci_reinit(ehci, pdev);
  189. done:
  190. return retval;
  191. }
  192. /*-------------------------------------------------------------------------*/
  193. #ifdef CONFIG_PM
  194. /* suspend/resume, section 4.3 */
  195. /* These routines rely on the PCI bus glue
  196. * to handle powerdown and wakeup, and currently also on
  197. * transceivers that don't need any software attention to set up
  198. * the right sort of wakeup.
  199. * Also they depend on separate root hub suspend/resume.
  200. */
  201. static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
  202. {
  203. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  204. unsigned long flags;
  205. int rc = 0;
  206. if (time_before(jiffies, ehci->next_statechange))
  207. msleep(10);
  208. /* Root hub was already suspended. Disable irq emission and
  209. * mark HW unaccessible, bail out if RH has been resumed. Use
  210. * the spinlock to properly synchronize with possible pending
  211. * RH suspend or resume activity.
  212. *
  213. * This is still racy as hcd->state is manipulated outside of
  214. * any locks =P But that will be a different fix.
  215. */
  216. spin_lock_irqsave (&ehci->lock, flags);
  217. if (hcd->state != HC_STATE_SUSPENDED) {
  218. rc = -EINVAL;
  219. goto bail;
  220. }
  221. writel (0, &ehci->regs->intr_enable);
  222. (void)readl(&ehci->regs->intr_enable);
  223. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  224. bail:
  225. spin_unlock_irqrestore (&ehci->lock, flags);
  226. // could save FLADJ in case of Vaux power loss
  227. // ... we'd only use it to handle clock skew
  228. return rc;
  229. }
  230. static int ehci_pci_resume(struct usb_hcd *hcd)
  231. {
  232. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  233. unsigned port;
  234. struct usb_device *root = hcd->self.root_hub;
  235. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  236. int retval = -EINVAL;
  237. // maybe restore FLADJ
  238. if (time_before(jiffies, ehci->next_statechange))
  239. msleep(100);
  240. /* Mark hardware accessible again as we are out of D3 state by now */
  241. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  242. /* If CF is clear, we lost PCI Vaux power and need to restart. */
  243. if (readl(&ehci->regs->configured_flag) != FLAG_CF)
  244. goto restart;
  245. /* If any port is suspended (or owned by the companion),
  246. * we know we can/must resume the HC (and mustn't reset it).
  247. * We just defer that to the root hub code.
  248. */
  249. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  250. u32 status;
  251. port--;
  252. status = readl(&ehci->regs->port_status [port]);
  253. if (!(status & PORT_POWER))
  254. continue;
  255. if (status & (PORT_SUSPEND | PORT_RESUME | PORT_OWNER)) {
  256. usb_hcd_resume_root_hub(hcd);
  257. return 0;
  258. }
  259. }
  260. restart:
  261. ehci_dbg(ehci, "lost power, restarting\n");
  262. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  263. port--;
  264. if (!root->children [port])
  265. continue;
  266. usb_set_device_state(root->children[port],
  267. USB_STATE_NOTATTACHED);
  268. }
  269. /* Else reset, to cope with power loss or flush-to-storage
  270. * style "resume" having let BIOS kick in during reboot.
  271. */
  272. (void) ehci_halt(ehci);
  273. (void) ehci_reset(ehci);
  274. (void) ehci_pci_reinit(ehci, pdev);
  275. /* emptying the schedule aborts any urbs */
  276. spin_lock_irq(&ehci->lock);
  277. if (ehci->reclaim)
  278. ehci->reclaim_ready = 1;
  279. ehci_work(ehci, NULL);
  280. spin_unlock_irq(&ehci->lock);
  281. /* restart; khubd will disconnect devices */
  282. retval = ehci_run(hcd);
  283. /* here we "know" root ports should always stay powered */
  284. ehci_port_power(ehci, 1);
  285. return retval;
  286. }
  287. #endif
  288. static const struct hc_driver ehci_pci_hc_driver = {
  289. .description = hcd_name,
  290. .product_desc = "EHCI Host Controller",
  291. .hcd_priv_size = sizeof(struct ehci_hcd),
  292. /*
  293. * generic hardware linkage
  294. */
  295. .irq = ehci_irq,
  296. .flags = HCD_MEMORY | HCD_USB2,
  297. /*
  298. * basic lifecycle operations
  299. */
  300. .reset = ehci_pci_setup,
  301. .start = ehci_run,
  302. #ifdef CONFIG_PM
  303. .suspend = ehci_pci_suspend,
  304. .resume = ehci_pci_resume,
  305. #endif
  306. .stop = ehci_stop,
  307. /*
  308. * managing i/o requests and associated device resources
  309. */
  310. .urb_enqueue = ehci_urb_enqueue,
  311. .urb_dequeue = ehci_urb_dequeue,
  312. .endpoint_disable = ehci_endpoint_disable,
  313. /*
  314. * scheduling support
  315. */
  316. .get_frame_number = ehci_get_frame,
  317. /*
  318. * root hub support
  319. */
  320. .hub_status_data = ehci_hub_status_data,
  321. .hub_control = ehci_hub_control,
  322. .bus_suspend = ehci_bus_suspend,
  323. .bus_resume = ehci_bus_resume,
  324. };
  325. /*-------------------------------------------------------------------------*/
  326. /* PCI driver selection metadata; PCI hotplugging uses this */
  327. static const struct pci_device_id pci_ids [] = { {
  328. /* handle any USB 2.0 EHCI controller */
  329. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
  330. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  331. },
  332. { /* end: all zeroes */ }
  333. };
  334. MODULE_DEVICE_TABLE(pci, pci_ids);
  335. /* pci driver glue; this is a "new style" PCI driver module */
  336. static struct pci_driver ehci_pci_driver = {
  337. .name = (char *) hcd_name,
  338. .id_table = pci_ids,
  339. .probe = usb_hcd_pci_probe,
  340. .remove = usb_hcd_pci_remove,
  341. #ifdef CONFIG_PM
  342. .suspend = usb_hcd_pci_suspend,
  343. .resume = usb_hcd_pci_resume,
  344. #endif
  345. };
  346. static int __init ehci_hcd_pci_init(void)
  347. {
  348. if (usb_disabled())
  349. return -ENODEV;
  350. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  351. hcd_name,
  352. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  353. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  354. return pci_register_driver(&ehci_pci_driver);
  355. }
  356. module_init(ehci_hcd_pci_init);
  357. static void __exit ehci_hcd_pci_cleanup(void)
  358. {
  359. pci_unregister_driver(&ehci_pci_driver);
  360. }
  361. module_exit(ehci_hcd_pci_cleanup);