blob: 441c26064b44b3266ac055e4c708548f465183ce [file] [log] [blame]
Matt Porter7ff71d62005-09-22 22:31:15 -07001/*
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
21#ifndef CONFIG_PCI
22#error "This file is PCI bus glue. CONFIG_PCI must be defined."
23#endif
24
25/*-------------------------------------------------------------------------*/
26
27/* EHCI 0.96 (and later) section 5.1 says how to kick BIOS/SMM/...
28 * off the controller (maybe it can boot from highspeed USB disks).
29 */
David Brownellabcc9442005-11-23 15:45:32 -080030static int bios_handoff(struct ehci_hcd *ehci, int where, u32 cap)
Matt Porter7ff71d62005-09-22 22:31:15 -070031{
32 struct pci_dev *pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
33
34 /* always say Linux will own the hardware */
35 pci_write_config_byte(pdev, where + 3, 1);
36
37 /* maybe wait a while for BIOS to respond */
38 if (cap & (1 << 16)) {
39 int msec = 5000;
40
41 do {
42 msleep(10);
43 msec -= 10;
44 pci_read_config_dword(pdev, where, &cap);
45 } while ((cap & (1 << 16)) && msec);
46 if (cap & (1 << 16)) {
47 ehci_err(ehci, "BIOS handoff failed (%d, %08x)\n",
48 where, cap);
49 // some BIOS versions seem buggy...
50 // return 1;
David Brownellabcc9442005-11-23 15:45:32 -080051 ehci_warn(ehci, "continuing after BIOS bug...\n");
Matt Porter7ff71d62005-09-22 22:31:15 -070052 /* disable all SMIs, and clear "BIOS owns" flag */
53 pci_write_config_dword(pdev, where + 4, 0);
54 pci_write_config_byte(pdev, where + 2, 0);
55 } else
56 ehci_dbg(ehci, "BIOS handoff succeeded\n");
57 }
58 return 0;
59}
60
David Brownell18807522005-11-23 15:45:37 -080061/* called after powerup, by probe or system-pm "wakeup" */
62static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
63{
64 u32 temp;
65 int retval;
66 unsigned count = 256/4;
67
68 /* optional debug port, normally in the first BAR */
69 temp = pci_find_capability(pdev, 0x0a);
70 if (temp) {
71 pci_read_config_dword(pdev, temp, &temp);
72 temp >>= 16;
73 if ((temp & (3 << 13)) == (1 << 13)) {
74 temp &= 0x1fff;
75 ehci->debug = ehci_to_hcd(ehci)->regs + temp;
76 temp = readl(&ehci->debug->control);
77 ehci_info(ehci, "debug port %d%s\n",
78 HCS_DEBUG_PORT(ehci->hcs_params),
79 (temp & DBGP_ENABLED)
80 ? " IN USE"
81 : "");
82 if (!(temp & DBGP_ENABLED))
83 ehci->debug = NULL;
84 }
85 }
86
87 temp = HCC_EXT_CAPS(readl(&ehci->caps->hcc_params));
88
89 /* EHCI 0.96 and later may have "extended capabilities" */
90 while (temp && count--) {
91 u32 cap;
92
93 pci_read_config_dword(pdev, temp, &cap);
94 ehci_dbg(ehci, "capability %04x at %02x\n", cap, temp);
95 switch (cap & 0xff) {
96 case 1: /* BIOS/SMM/... handoff */
97 if (bios_handoff(ehci, temp, cap) != 0)
98 return -EOPNOTSUPP;
99 break;
100 case 0: /* illegal reserved capability */
101 ehci_dbg(ehci, "illegal capability!\n");
102 cap = 0;
103 /* FALLTHROUGH */
104 default: /* unknown */
105 break;
106 }
107 temp = (cap >> 8) & 0xff;
108 }
109 if (!count) {
110 ehci_err(ehci, "bogus capabilities ... PCI problems!\n");
111 return -EIO;
112 }
113
114 /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
115 retval = pci_set_mwi(pdev);
116 if (!retval)
117 ehci_dbg(ehci, "MWI active\n");
118
119 ehci_port_power(ehci, 0);
120
121 return 0;
122}
123
124/* called by khubd or root hub (re)init threads; leaves HC in halt state */
David Brownellabcc9442005-11-23 15:45:32 -0800125static int ehci_pci_reset(struct usb_hcd *hcd)
Matt Porter7ff71d62005-09-22 22:31:15 -0700126{
David Brownellabcc9442005-11-23 15:45:32 -0800127 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
128 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Matt Porter7ff71d62005-09-22 22:31:15 -0700129 u32 temp;
David Brownell18807522005-11-23 15:45:37 -0800130 int retval;
Matt Porter7ff71d62005-09-22 22:31:15 -0700131
132 ehci->caps = hcd->regs;
David Brownellabcc9442005-11-23 15:45:32 -0800133 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
134 dbg_hcs_params(ehci, "reset");
135 dbg_hcc_params(ehci, "reset");
Matt Porter7ff71d62005-09-22 22:31:15 -0700136
137 /* cache this readonly data; minimize chip reads */
David Brownellabcc9442005-11-23 15:45:32 -0800138 ehci->hcs_params = readl(&ehci->caps->hcs_params);
Matt Porter7ff71d62005-09-22 22:31:15 -0700139
David Brownell18807522005-11-23 15:45:37 -0800140 retval = ehci_halt(ehci);
141 if (retval)
142 return retval;
143
David Brownellabcc9442005-11-23 15:45:32 -0800144 /* NOTE: only the parts below this line are PCI-specific */
Matt Porter7ff71d62005-09-22 22:31:15 -0700145
David Brownellabcc9442005-11-23 15:45:32 -0800146 switch (pdev->vendor) {
147 case PCI_VENDOR_ID_TDI:
148 if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
149 ehci->is_tdi_rh_tt = 1;
150 tdi_reset(ehci);
151 }
152 break;
153 case PCI_VENDOR_ID_AMD:
154 /* AMD8111 EHCI doesn't work, according to AMD errata */
155 if (pdev->device == 0x7463) {
156 ehci_info(ehci, "ignoring AMD8111 (errata)\n");
157 return -EIO;
158 }
159 break;
160 case PCI_VENDOR_ID_NVIDIA:
161 /* NVidia reports that certain chips don't handle
162 * QH, ITD, or SITD addresses above 2GB. (But TD,
163 * data buffer, and periodic schedule are normal.)
164 */
165 switch (pdev->device) {
166 case 0x003c: /* MCP04 */
167 case 0x005b: /* CK804 */
168 case 0x00d8: /* CK8 */
169 case 0x00e8: /* CK8S */
170 if (pci_set_consistent_dma_mask(pdev,
171 DMA_31BIT_MASK) < 0)
172 ehci_warn(ehci, "can't enable NVidia "
173 "workaround for >2GB RAM\n");
Matt Porter7ff71d62005-09-22 22:31:15 -0700174 break;
175 }
David Brownellabcc9442005-11-23 15:45:32 -0800176 break;
177 }
Matt Porter7ff71d62005-09-22 22:31:15 -0700178
Matt Porter7ff71d62005-09-22 22:31:15 -0700179 if (ehci_is_TDI(ehci))
David Brownellabcc9442005-11-23 15:45:32 -0800180 ehci_reset(ehci);
Matt Porter7ff71d62005-09-22 22:31:15 -0700181
Matt Porter7ff71d62005-09-22 22:31:15 -0700182 /* at least the Genesys GL880S needs fixup here */
183 temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
184 temp &= 0x0f;
185 if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
David Brownellabcc9442005-11-23 15:45:32 -0800186 ehci_dbg(ehci, "bogus port configuration: "
Matt Porter7ff71d62005-09-22 22:31:15 -0700187 "cc=%d x pcc=%d < ports=%d\n",
188 HCS_N_CC(ehci->hcs_params),
189 HCS_N_PCC(ehci->hcs_params),
190 HCS_N_PORTS(ehci->hcs_params));
191
David Brownellabcc9442005-11-23 15:45:32 -0800192 switch (pdev->vendor) {
193 case 0x17a0: /* GENESYS */
194 /* GL880S: should be PORTS=2 */
195 temp |= (ehci->hcs_params & ~0xf);
196 ehci->hcs_params = temp;
197 break;
198 case PCI_VENDOR_ID_NVIDIA:
199 /* NF4: should be PCC=10 */
200 break;
Matt Porter7ff71d62005-09-22 22:31:15 -0700201 }
202 }
203
David Brownellabcc9442005-11-23 15:45:32 -0800204 /* Serial Bus Release Number is at PCI 0x60 offset */
205 pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
Matt Porter7ff71d62005-09-22 22:31:15 -0700206
David Brownell18807522005-11-23 15:45:37 -0800207 /* REVISIT: per-port wake capability (PCI 0x62) currently unused */
Matt Porter7ff71d62005-09-22 22:31:15 -0700208
David Brownell18807522005-11-23 15:45:37 -0800209 retval = ehci_pci_reinit(ehci, pdev);
Matt Porter7ff71d62005-09-22 22:31:15 -0700210
David Brownell18807522005-11-23 15:45:37 -0800211 /* finish init */
212 return ehci_init(hcd);
Matt Porter7ff71d62005-09-22 22:31:15 -0700213}
214
215/*-------------------------------------------------------------------------*/
216
217#ifdef CONFIG_PM
218
219/* suspend/resume, section 4.3 */
220
David Brownellf03c17f2005-11-23 15:45:28 -0800221/* These routines rely on the PCI bus glue
Matt Porter7ff71d62005-09-22 22:31:15 -0700222 * to handle powerdown and wakeup, and currently also on
223 * transceivers that don't need any software attention to set up
224 * the right sort of wakeup.
David Brownellf03c17f2005-11-23 15:45:28 -0800225 * Also they depend on separate root hub suspend/resume.
Matt Porter7ff71d62005-09-22 22:31:15 -0700226 */
227
David Brownellabcc9442005-11-23 15:45:32 -0800228static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
Matt Porter7ff71d62005-09-22 22:31:15 -0700229{
David Brownellabcc9442005-11-23 15:45:32 -0800230 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Matt Porter7ff71d62005-09-22 22:31:15 -0700231
David Brownellabcc9442005-11-23 15:45:32 -0800232 if (time_before(jiffies, ehci->next_statechange))
233 msleep(10);
Matt Porter7ff71d62005-09-22 22:31:15 -0700234
David Brownellf03c17f2005-11-23 15:45:28 -0800235 // could save FLADJ in case of Vaux power loss
Matt Porter7ff71d62005-09-22 22:31:15 -0700236 // ... we'd only use it to handle clock skew
237
238 return 0;
239}
240
David Brownellabcc9442005-11-23 15:45:32 -0800241static int ehci_pci_resume(struct usb_hcd *hcd)
Matt Porter7ff71d62005-09-22 22:31:15 -0700242{
David Brownellabcc9442005-11-23 15:45:32 -0800243 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Matt Porter7ff71d62005-09-22 22:31:15 -0700244 unsigned port;
245 struct usb_device *root = hcd->self.root_hub;
David Brownell18807522005-11-23 15:45:37 -0800246 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Matt Porter7ff71d62005-09-22 22:31:15 -0700247 int retval = -EINVAL;
248
David Brownellf03c17f2005-11-23 15:45:28 -0800249 // maybe restore FLADJ
Matt Porter7ff71d62005-09-22 22:31:15 -0700250
David Brownellabcc9442005-11-23 15:45:32 -0800251 if (time_before(jiffies, ehci->next_statechange))
252 msleep(100);
Matt Porter7ff71d62005-09-22 22:31:15 -0700253
David Brownellf03c17f2005-11-23 15:45:28 -0800254 /* If CF is clear, we lost PCI Vaux power and need to restart. */
David Brownell18807522005-11-23 15:45:37 -0800255 if (readl(&ehci->regs->configured_flag) != FLAG_CF)
David Brownellf03c17f2005-11-23 15:45:28 -0800256 goto restart;
257
Matt Porter7ff71d62005-09-22 22:31:15 -0700258 /* If any port is suspended (or owned by the companion),
259 * we know we can/must resume the HC (and mustn't reset it).
David Brownellf03c17f2005-11-23 15:45:28 -0800260 * We just defer that to the root hub code.
Matt Porter7ff71d62005-09-22 22:31:15 -0700261 */
David Brownellabcc9442005-11-23 15:45:32 -0800262 for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
Matt Porter7ff71d62005-09-22 22:31:15 -0700263 u32 status;
264 port--;
David Brownellabcc9442005-11-23 15:45:32 -0800265 status = readl(&ehci->regs->port_status [port]);
Matt Porter7ff71d62005-09-22 22:31:15 -0700266 if (!(status & PORT_POWER))
267 continue;
David Brownellf03c17f2005-11-23 15:45:28 -0800268 if (status & (PORT_SUSPEND | PORT_RESUME | PORT_OWNER)) {
269 usb_hcd_resume_root_hub(hcd);
270 return 0;
Matt Porter7ff71d62005-09-22 22:31:15 -0700271 }
David Brownellf03c17f2005-11-23 15:45:28 -0800272 }
273
274restart:
275 ehci_dbg(ehci, "lost power, restarting\n");
David Brownellabcc9442005-11-23 15:45:32 -0800276 for (port = HCS_N_PORTS(ehci->hcs_params); port > 0; ) {
David Brownellf03c17f2005-11-23 15:45:28 -0800277 port--;
Matt Porter7ff71d62005-09-22 22:31:15 -0700278 if (!root->children [port])
279 continue;
David Brownellabcc9442005-11-23 15:45:32 -0800280 usb_set_device_state(root->children[port],
Matt Porter7ff71d62005-09-22 22:31:15 -0700281 USB_STATE_NOTATTACHED);
282 }
283
284 /* Else reset, to cope with power loss or flush-to-storage
David Brownellf03c17f2005-11-23 15:45:28 -0800285 * style "resume" having let BIOS kick in during reboot.
Matt Porter7ff71d62005-09-22 22:31:15 -0700286 */
David Brownellabcc9442005-11-23 15:45:32 -0800287 (void) ehci_halt(ehci);
288 (void) ehci_reset(ehci);
David Brownell18807522005-11-23 15:45:37 -0800289 (void) ehci_pci_reinit(ehci, pdev);
Matt Porter7ff71d62005-09-22 22:31:15 -0700290
David Brownellf03c17f2005-11-23 15:45:28 -0800291 /* emptying the schedule aborts any urbs */
David Brownellabcc9442005-11-23 15:45:32 -0800292 spin_lock_irq(&ehci->lock);
David Brownellf03c17f2005-11-23 15:45:28 -0800293 if (ehci->reclaim)
294 ehci->reclaim_ready = 1;
David Brownellabcc9442005-11-23 15:45:32 -0800295 ehci_work(ehci, NULL);
296 spin_unlock_irq(&ehci->lock);
Matt Porter7ff71d62005-09-22 22:31:15 -0700297
David Brownellf03c17f2005-11-23 15:45:28 -0800298 /* restart; khubd will disconnect devices */
David Brownellabcc9442005-11-23 15:45:32 -0800299 retval = ehci_run(hcd);
Matt Porter7ff71d62005-09-22 22:31:15 -0700300
David Brownell18807522005-11-23 15:45:37 -0800301 /* here we "know" root ports should always stay powered */
David Brownellabcc9442005-11-23 15:45:32 -0800302 ehci_port_power(ehci, 1);
Matt Porter7ff71d62005-09-22 22:31:15 -0700303
304 return retval;
305}
306#endif
307
308static const struct hc_driver ehci_pci_hc_driver = {
309 .description = hcd_name,
310 .product_desc = "EHCI Host Controller",
311 .hcd_priv_size = sizeof(struct ehci_hcd),
312
313 /*
314 * generic hardware linkage
315 */
316 .irq = ehci_irq,
317 .flags = HCD_MEMORY | HCD_USB2,
318
319 /*
320 * basic lifecycle operations
321 */
322 .reset = ehci_pci_reset,
David Brownell18807522005-11-23 15:45:37 -0800323 .start = ehci_run,
Matt Porter7ff71d62005-09-22 22:31:15 -0700324#ifdef CONFIG_PM
325 .suspend = ehci_pci_suspend,
326 .resume = ehci_pci_resume,
327#endif
David Brownell18807522005-11-23 15:45:37 -0800328 .stop = ehci_stop,
Matt Porter7ff71d62005-09-22 22:31:15 -0700329
330 /*
331 * managing i/o requests and associated device resources
332 */
333 .urb_enqueue = ehci_urb_enqueue,
334 .urb_dequeue = ehci_urb_dequeue,
335 .endpoint_disable = ehci_endpoint_disable,
336
337 /*
338 * scheduling support
339 */
340 .get_frame_number = ehci_get_frame,
341
342 /*
343 * root hub support
344 */
345 .hub_status_data = ehci_hub_status_data,
346 .hub_control = ehci_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -0400347 .bus_suspend = ehci_bus_suspend,
348 .bus_resume = ehci_bus_resume,
Matt Porter7ff71d62005-09-22 22:31:15 -0700349};
350
351/*-------------------------------------------------------------------------*/
352
353/* PCI driver selection metadata; PCI hotplugging uses this */
354static const struct pci_device_id pci_ids [] = { {
355 /* handle any USB 2.0 EHCI controller */
356 PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB << 8) | 0x20), ~0),
357 .driver_data = (unsigned long) &ehci_pci_hc_driver,
358 },
359 { /* end: all zeroes */ }
360};
David Brownellabcc9442005-11-23 15:45:32 -0800361MODULE_DEVICE_TABLE(pci, pci_ids);
Matt Porter7ff71d62005-09-22 22:31:15 -0700362
363/* pci driver glue; this is a "new style" PCI driver module */
364static struct pci_driver ehci_pci_driver = {
365 .name = (char *) hcd_name,
366 .id_table = pci_ids,
Matt Porter7ff71d62005-09-22 22:31:15 -0700367
368 .probe = usb_hcd_pci_probe,
369 .remove = usb_hcd_pci_remove,
370
371#ifdef CONFIG_PM
372 .suspend = usb_hcd_pci_suspend,
373 .resume = usb_hcd_pci_resume,
374#endif
375};
376
David Brownellabcc9442005-11-23 15:45:32 -0800377static int __init ehci_hcd_pci_init(void)
Matt Porter7ff71d62005-09-22 22:31:15 -0700378{
379 if (usb_disabled())
380 return -ENODEV;
381
David Brownellabcc9442005-11-23 15:45:32 -0800382 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
Matt Porter7ff71d62005-09-22 22:31:15 -0700383 hcd_name,
David Brownellabcc9442005-11-23 15:45:32 -0800384 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
385 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
Matt Porter7ff71d62005-09-22 22:31:15 -0700386
David Brownellabcc9442005-11-23 15:45:32 -0800387 return pci_register_driver(&ehci_pci_driver);
Matt Porter7ff71d62005-09-22 22:31:15 -0700388}
David Brownellabcc9442005-11-23 15:45:32 -0800389module_init(ehci_hcd_pci_init);
Matt Porter7ff71d62005-09-22 22:31:15 -0700390
David Brownellabcc9442005-11-23 15:45:32 -0800391static void __exit ehci_hcd_pci_cleanup(void)
Matt Porter7ff71d62005-09-22 22:31:15 -0700392{
David Brownellabcc9442005-11-23 15:45:32 -0800393 pci_unregister_driver(&ehci_pci_driver);
Matt Porter7ff71d62005-09-22 22:31:15 -0700394}
David Brownellabcc9442005-11-23 15:45:32 -0800395module_exit(ehci_hcd_pci_cleanup);