ehci-pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. /* called after powerup, by probe or system-pm "wakeup" */
  25. static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
  26. {
  27. u32 temp;
  28. int retval;
  29. /* optional debug port, normally in the first BAR */
  30. temp = pci_find_capability(pdev, 0x0a);
  31. if (temp) {
  32. pci_read_config_dword(pdev, temp, &temp);
  33. temp >>= 16;
  34. if ((temp & (3 << 13)) == (1 << 13)) {
  35. temp &= 0x1fff;
  36. ehci->debug = ehci_to_hcd(ehci)->regs + temp;
  37. temp = ehci_readl(ehci, &ehci->debug->control);
  38. ehci_info(ehci, "debug port %d%s\n",
  39. HCS_DEBUG_PORT(ehci->hcs_params),
  40. (temp & DBGP_ENABLED)
  41. ? " IN USE"
  42. : "");
  43. if (!(temp & DBGP_ENABLED))
  44. ehci->debug = NULL;
  45. }
  46. }
  47. /* we expect static quirk code to handle the "extended capabilities"
  48. * (currently just BIOS handoff) allowed starting with EHCI 0.96
  49. */
  50. /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
  51. retval = pci_set_mwi(pdev);
  52. if (!retval)
  53. ehci_dbg(ehci, "MWI active\n");
  54. return 0;
  55. }
  56. /* called during probe() after chip reset completes */
  57. static int ehci_pci_setup(struct usb_hcd *hcd)
  58. {
  59. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  60. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  61. struct pci_dev *p_smbus;
  62. u8 rev;
  63. u32 temp;
  64. int retval;
  65. switch (pdev->vendor) {
  66. case PCI_VENDOR_ID_TOSHIBA_2:
  67. /* celleb's companion chip */
  68. if (pdev->device == 0x01b5) {
  69. #ifdef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
  70. ehci->big_endian_mmio = 1;
  71. #else
  72. ehci_warn(ehci,
  73. "unsupported big endian Toshiba quirk\n");
  74. #endif
  75. }
  76. break;
  77. }
  78. ehci->caps = hcd->regs;
  79. ehci->regs = hcd->regs +
  80. HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
  81. dbg_hcs_params(ehci, "reset");
  82. dbg_hcc_params(ehci, "reset");
  83. /* ehci_init() causes memory for DMA transfers to be
  84. * allocated. Thus, any vendor-specific workarounds based on
  85. * limiting the type of memory used for DMA transfers must
  86. * happen before ehci_init() is called. */
  87. switch (pdev->vendor) {
  88. case PCI_VENDOR_ID_NVIDIA:
  89. /* NVidia reports that certain chips don't handle
  90. * QH, ITD, or SITD addresses above 2GB. (But TD,
  91. * data buffer, and periodic schedule are normal.)
  92. */
  93. switch (pdev->device) {
  94. case 0x003c: /* MCP04 */
  95. case 0x005b: /* CK804 */
  96. case 0x00d8: /* CK8 */
  97. case 0x00e8: /* CK8S */
  98. if (pci_set_consistent_dma_mask(pdev,
  99. DMA_BIT_MASK(31)) < 0)
  100. ehci_warn(ehci, "can't enable NVidia "
  101. "workaround for >2GB RAM\n");
  102. break;
  103. }
  104. break;
  105. }
  106. /* cache this readonly data; minimize chip reads */
  107. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  108. retval = ehci_halt(ehci);
  109. if (retval)
  110. return retval;
  111. /* data structure init */
  112. retval = ehci_init(hcd);
  113. if (retval)
  114. return retval;
  115. switch (pdev->vendor) {
  116. case PCI_VENDOR_ID_TDI:
  117. if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
  118. hcd->has_tt = 1;
  119. tdi_reset(ehci);
  120. }
  121. break;
  122. case PCI_VENDOR_ID_AMD:
  123. /* AMD8111 EHCI doesn't work, according to AMD errata */
  124. if (pdev->device == 0x7463) {
  125. ehci_info(ehci, "ignoring AMD8111 (errata)\n");
  126. retval = -EIO;
  127. goto done;
  128. }
  129. break;
  130. case PCI_VENDOR_ID_NVIDIA:
  131. switch (pdev->device) {
  132. /* Some NForce2 chips have problems with selective suspend;
  133. * fixed in newer silicon.
  134. */
  135. case 0x0068:
  136. if (pdev->revision < 0xa4)
  137. ehci->no_selective_suspend = 1;
  138. break;
  139. }
  140. break;
  141. case PCI_VENDOR_ID_VIA:
  142. if (pdev->device == 0x3104 && (pdev->revision & 0xf0) == 0x60) {
  143. u8 tmp;
  144. /* The VT6212 defaults to a 1 usec EHCI sleep time which
  145. * hogs the PCI bus *badly*. Setting bit 5 of 0x4B makes
  146. * that sleep time use the conventional 10 usec.
  147. */
  148. pci_read_config_byte(pdev, 0x4b, &tmp);
  149. if (tmp & 0x20)
  150. break;
  151. pci_write_config_byte(pdev, 0x4b, tmp | 0x20);
  152. }
  153. break;
  154. case PCI_VENDOR_ID_ATI:
  155. /* SB600 and old version of SB700 have a bug in EHCI controller,
  156. * which causes usb devices lose response in some cases.
  157. */
  158. if ((pdev->device == 0x4386) || (pdev->device == 0x4396)) {
  159. p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
  160. PCI_DEVICE_ID_ATI_SBX00_SMBUS,
  161. NULL);
  162. if (!p_smbus)
  163. break;
  164. rev = p_smbus->revision;
  165. if ((pdev->device == 0x4386) || (rev == 0x3a)
  166. || (rev == 0x3b)) {
  167. u8 tmp;
  168. ehci_info(ehci, "applying AMD SB600/SB700 USB "
  169. "freeze workaround\n");
  170. pci_read_config_byte(pdev, 0x53, &tmp);
  171. pci_write_config_byte(pdev, 0x53, tmp | (1<<3));
  172. }
  173. pci_dev_put(p_smbus);
  174. }
  175. break;
  176. }
  177. ehci_reset(ehci);
  178. /* at least the Genesys GL880S needs fixup here */
  179. temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
  180. temp &= 0x0f;
  181. if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
  182. ehci_dbg(ehci, "bogus port configuration: "
  183. "cc=%d x pcc=%d < ports=%d\n",
  184. HCS_N_CC(ehci->hcs_params),
  185. HCS_N_PCC(ehci->hcs_params),
  186. HCS_N_PORTS(ehci->hcs_params));
  187. switch (pdev->vendor) {
  188. case 0x17a0: /* GENESYS */
  189. /* GL880S: should be PORTS=2 */
  190. temp |= (ehci->hcs_params & ~0xf);
  191. ehci->hcs_params = temp;
  192. break;
  193. case PCI_VENDOR_ID_NVIDIA:
  194. /* NF4: should be PCC=10 */
  195. break;
  196. }
  197. }
  198. /* Serial Bus Release Number is at PCI 0x60 offset */
  199. pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
  200. /* Keep this around for a while just in case some EHCI
  201. * implementation uses legacy PCI PM support. This test
  202. * can be removed on 17 Dec 2009 if the dev_warn() hasn't
  203. * been triggered by then.
  204. */
  205. if (!device_can_wakeup(&pdev->dev)) {
  206. u16 port_wake;
  207. pci_read_config_word(pdev, 0x62, &port_wake);
  208. if (port_wake & 0x0001) {
  209. dev_warn(&pdev->dev, "Enabling legacy PCI PM\n");
  210. device_set_wakeup_capable(&pdev->dev, 1);
  211. }
  212. }
  213. #ifdef CONFIG_USB_SUSPEND
  214. /* REVISIT: the controller works fine for wakeup iff the root hub
  215. * itself is "globally" suspended, but usbcore currently doesn't
  216. * understand such things.
  217. *
  218. * System suspend currently expects to be able to suspend the entire
  219. * device tree, device-at-a-time. If we failed selective suspend
  220. * reports, system suspend would fail; so the root hub code must claim
  221. * success. That's lying to usbcore, and it matters for for runtime
  222. * PM scenarios with selective suspend and remote wakeup...
  223. */
  224. if (ehci->no_selective_suspend && device_can_wakeup(&pdev->dev))
  225. ehci_warn(ehci, "selective suspend/wakeup unavailable\n");
  226. #endif
  227. ehci_port_power(ehci, 1);
  228. retval = ehci_pci_reinit(ehci, pdev);
  229. done:
  230. return retval;
  231. }
  232. /*-------------------------------------------------------------------------*/
  233. #ifdef CONFIG_PM
  234. /* suspend/resume, section 4.3 */
  235. /* These routines rely on the PCI bus glue
  236. * to handle powerdown and wakeup, and currently also on
  237. * transceivers that don't need any software attention to set up
  238. * the right sort of wakeup.
  239. * Also they depend on separate root hub suspend/resume.
  240. */
  241. static int ehci_pci_suspend(struct usb_hcd *hcd)
  242. {
  243. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  244. unsigned long flags;
  245. int rc = 0;
  246. if (time_before(jiffies, ehci->next_statechange))
  247. msleep(10);
  248. /* Root hub was already suspended. Disable irq emission and
  249. * mark HW unaccessible, bail out if RH has been resumed. Use
  250. * the spinlock to properly synchronize with possible pending
  251. * RH suspend or resume activity.
  252. *
  253. * This is still racy as hcd->state is manipulated outside of
  254. * any locks =P But that will be a different fix.
  255. */
  256. spin_lock_irqsave (&ehci->lock, flags);
  257. if (hcd->state != HC_STATE_SUSPENDED) {
  258. rc = -EINVAL;
  259. goto bail;
  260. }
  261. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  262. (void)ehci_readl(ehci, &ehci->regs->intr_enable);
  263. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  264. bail:
  265. spin_unlock_irqrestore (&ehci->lock, flags);
  266. // could save FLADJ in case of Vaux power loss
  267. // ... we'd only use it to handle clock skew
  268. return rc;
  269. }
  270. static int ehci_pci_resume(struct usb_hcd *hcd, bool hibernated)
  271. {
  272. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  273. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  274. // maybe restore FLADJ
  275. if (time_before(jiffies, ehci->next_statechange))
  276. msleep(100);
  277. /* Mark hardware accessible again as we are out of D3 state by now */
  278. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  279. /* If CF is still set and we aren't resuming from hibernation
  280. * then we maintained PCI Vaux power.
  281. * Just undo the effect of ehci_pci_suspend().
  282. */
  283. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
  284. !hibernated) {
  285. int mask = INTR_MASK;
  286. if (!hcd->self.root_hub->do_remote_wakeup)
  287. mask &= ~STS_PCD;
  288. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  289. ehci_readl(ehci, &ehci->regs->intr_enable);
  290. return 0;
  291. }
  292. usb_root_hub_lost_power(hcd->self.root_hub);
  293. /* Else reset, to cope with power loss or flush-to-storage
  294. * style "resume" having let BIOS kick in during reboot.
  295. */
  296. (void) ehci_halt(ehci);
  297. (void) ehci_reset(ehci);
  298. (void) ehci_pci_reinit(ehci, pdev);
  299. /* emptying the schedule aborts any urbs */
  300. spin_lock_irq(&ehci->lock);
  301. if (ehci->reclaim)
  302. end_unlink_async(ehci);
  303. ehci_work(ehci);
  304. spin_unlock_irq(&ehci->lock);
  305. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  306. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  307. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  308. /* here we "know" root ports should always stay powered */
  309. ehci_port_power(ehci, 1);
  310. hcd->state = HC_STATE_SUSPENDED;
  311. return 0;
  312. }
  313. #endif
  314. static const struct hc_driver ehci_pci_hc_driver = {
  315. .description = hcd_name,
  316. .product_desc = "EHCI Host Controller",
  317. .hcd_priv_size = sizeof(struct ehci_hcd),
  318. /*
  319. * generic hardware linkage
  320. */
  321. .irq = ehci_irq,
  322. .flags = HCD_MEMORY | HCD_USB2,
  323. /*
  324. * basic lifecycle operations
  325. */
  326. .reset = ehci_pci_setup,
  327. .start = ehci_run,
  328. #ifdef CONFIG_PM
  329. .pci_suspend = ehci_pci_suspend,
  330. .pci_resume = ehci_pci_resume,
  331. #endif
  332. .stop = ehci_stop,
  333. .shutdown = ehci_shutdown,
  334. /*
  335. * managing i/o requests and associated device resources
  336. */
  337. .urb_enqueue = ehci_urb_enqueue,
  338. .urb_dequeue = ehci_urb_dequeue,
  339. .endpoint_disable = ehci_endpoint_disable,
  340. .endpoint_reset = ehci_endpoint_reset,
  341. /*
  342. * scheduling support
  343. */
  344. .get_frame_number = ehci_get_frame,
  345. /*
  346. * root hub support
  347. */
  348. .hub_status_data = ehci_hub_status_data,
  349. .hub_control = ehci_hub_control,
  350. .bus_suspend = ehci_bus_suspend,
  351. .bus_resume = ehci_bus_resume,
  352. .relinquish_port = ehci_relinquish_port,
  353. .port_handed_over = ehci_port_handed_over,
  354. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  355. };
  356. /*-------------------------------------------------------------------------*/
  357. /* PCI driver selection metadata; PCI hotplugging uses this */
  358. static const struct pci_device_id pci_ids [] = { {
  359. /* handle any USB 2.0 EHCI controller */
  360. PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_EHCI, ~0),
  361. .driver_data = (unsigned long) &ehci_pci_hc_driver,
  362. },
  363. { /* end: all zeroes */ }
  364. };
  365. MODULE_DEVICE_TABLE(pci, pci_ids);
  366. /* pci driver glue; this is a "new style" PCI driver module */
  367. static struct pci_driver ehci_pci_driver = {
  368. .name = (char *) hcd_name,
  369. .id_table = pci_ids,
  370. .probe = usb_hcd_pci_probe,
  371. .remove = usb_hcd_pci_remove,
  372. .shutdown = usb_hcd_pci_shutdown,
  373. #ifdef CONFIG_PM_SLEEP
  374. .driver = {
  375. .pm = &usb_hcd_pci_pm_ops
  376. },
  377. #endif
  378. };