ehci-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. /* Workaround current PCI init glitch: wakeup bits aren't
  188. * being set from PCI PM capability.
  189. */
  190. if (!device_can_wakeup(&pdev->dev)) {
  191. u16 port_wake;
  192. pci_read_config_word(pdev, 0x62, &port_wake);
  193. if (port_wake & 0x0001)
  194. device_init_wakeup(&pdev->dev, 1);
  195. }
  196. retval = ehci_pci_reinit(ehci, pdev);
  197. done:
  198. return retval;
  199. }
  200. /*-------------------------------------------------------------------------*/
  201. #ifdef CONFIG_PM
  202. /* suspend/resume, section 4.3 */
  203. /* These routines rely on the PCI bus glue
  204. * to handle powerdown and wakeup, and currently also on
  205. * transceivers that don't need any software attention to set up
  206. * the right sort of wakeup.
  207. * Also they depend on separate root hub suspend/resume.
  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. unsigned long flags;
  213. int rc = 0;
  214. if (time_before(jiffies, ehci->next_statechange))
  215. msleep(10);
  216. /* Root hub was already suspended. Disable irq emission and
  217. * mark HW unaccessible, bail out if RH has been resumed. Use
  218. * the spinlock to properly synchronize with possible pending
  219. * RH suspend or resume activity.
  220. *
  221. * This is still racy as hcd->state is manipulated outside of
  222. * any locks =P But that will be a different fix.
  223. */
  224. spin_lock_irqsave (&ehci->lock, flags);
  225. if (hcd->state != HC_STATE_SUSPENDED) {
  226. rc = -EINVAL;
  227. goto bail;
  228. }
  229. writel (0, &ehci->regs->intr_enable);
  230. (void)readl(&ehci->regs->intr_enable);
  231. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  232. bail:
  233. spin_unlock_irqrestore (&ehci->lock, flags);
  234. // could save FLADJ in case of Vaux power loss
  235. // ... we'd only use it to handle clock skew
  236. return rc;
  237. }
  238. static int ehci_pci_resume(struct usb_hcd *hcd)
  239. {
  240. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  241. unsigned port;
  242. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  243. int retval = -EINVAL;
  244. // maybe restore FLADJ
  245. if (time_before(jiffies, ehci->next_statechange))
  246. msleep(100);
  247. /* Mark hardware accessible again as we are out of D3 state by now */
  248. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  249. /* If CF is clear, we lost PCI Vaux power and need to restart. */
  250. if (readl(&ehci->regs->configured_flag) != FLAG_CF)
  251. goto restart;
  252. /* If any port is suspended (or owned by the companion),
  253. * we know we can/must resume the HC (and mustn't reset it).
  254. * We just defer that to the root hub code.
  255. */
  256. for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
  257. u32 status;
  258. port--;
  259. status = readl(&ehci->regs->port_status [port]);
  260. if (!(status & PORT_POWER))
  261. continue;
  262. if (status & (PORT_SUSPEND | PORT_RESUME | PORT_OWNER)) {
  263. usb_hcd_resume_root_hub(hcd);
  264. return 0;
  265. }
  266. }
  267. restart:
  268. ehci_dbg(ehci, "lost power, restarting\n");
  269. usb_root_hub_lost_power(hcd->self.root_hub);
  270. /* Else reset, to cope with power loss or flush-to-storage
  271. * style "resume" having let BIOS kick in during reboot.
  272. */
  273. (void) ehci_halt(ehci);
  274. (void) ehci_reset(ehci);
  275. (void) ehci_pci_reinit(ehci, pdev);
  276. /* emptying the schedule aborts any urbs */
  277. spin_lock_irq(&ehci->lock);
  278. if (ehci->reclaim)
  279. ehci->reclaim_ready = 1;
  280. ehci_work(ehci, NULL);
  281. spin_unlock_irq(&ehci->lock);
  282. /* restart; khubd will disconnect devices */
  283. retval = ehci_run(hcd);
  284. /* here we "know" root ports should always stay powered */
  285. ehci_port_power(ehci, 1);
  286. return retval;
  287. }
  288. #endif
  289. static const struct hc_driver ehci_pci_hc_driver = {
  290. .description = hcd_name,
  291. .product_desc = "EHCI Host Controller",
  292. .hcd_priv_size = sizeof(struct ehci_hcd),
  293. /*
  294. * generic hardware linkage
  295. */
  296. .irq = ehci_irq,
  297. .flags = HCD_MEMORY | HCD_USB2,
  298. /*
  299. * basic lifecycle operations
  300. */
  301. .reset = ehci_pci_setup,
  302. .start = ehci_run,
  303. #ifdef CONFIG_PM
  304. .suspend = ehci_pci_suspend,
  305. .resume = ehci_pci_resume,
  306. #endif
  307. .stop = ehci_stop,
  308. /*
  309. * managing i/o requests and associated device resources
  310. */
  311. .urb_enqueue = ehci_urb_enqueue,
  312. .urb_dequeue = ehci_urb_dequeue,
  313. .endpoint_disable = ehci_endpoint_disable,
  314. /*
  315. * scheduling support
  316. */
  317. .get_frame_number = ehci_get_frame,
  318. /*
  319. * root hub support
  320. */
  321. .hub_status_data = ehci_hub_status_data,
  322. .hub_control = ehci_hub_control,
  323. .bus_suspend = ehci_bus_suspend,
  324. .bus_resume = ehci_bus_resume,
  325. };
  326. /*-------------------------------------------------------------------------*/
  327. /* PCI driver selection metadata; PCI hotplugging uses this */
  328. static const struct pci_device_id pci_ids [] = { {
  329. /* handle any USB 2.0 EHCI controller */
  330. PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
  331. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  332. },
  333. { /* end: all zeroes */ }
  334. };
  335. MODULE_DEVICE_TABLE(pci, pci_ids);
  336. /* pci driver glue; this is a "new style" PCI driver module */
  337. static struct pci_driver ehci_pci_driver = {
  338. .name = (char *) hcd_name,
  339. .id_table = pci_ids,
  340. .probe = usb_hcd_pci_probe,
  341. .remove = usb_hcd_pci_remove,
  342. #ifdef CONFIG_PM
  343. .suspend = usb_hcd_pci_suspend,
  344. .resume = usb_hcd_pci_resume,
  345. #endif
  346. };
  347. static int __init ehci_hcd_pci_init(void)
  348. {
  349. if (usb_disabled())
  350. return -ENODEV;
  351. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  352. hcd_name,
  353. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  354. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  355. return pci_register_driver(&ehci_pci_driver);
  356. }
  357. module_init(ehci_hcd_pci_init);
  358. static void __exit ehci_hcd_pci_cleanup(void)
  359. {
  360. pci_unregister_driver(&ehci_pci_driver);
  361. }
  362. module_exit(ehci_hcd_pci_cleanup);