2 * Copyright (C) 2001-2004 by David Brownell
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* this file is part of ehci-hcd.c */
21 /*-------------------------------------------------------------------------*/
24 * EHCI Root Hub ... the nonsharable stuff
26 * Registers don't need cpu_to_le32, that happens transparently
29 /*-------------------------------------------------------------------------*/
33 static int ehci_bus_suspend (struct usb_hcd *hcd)
35 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
39 if (time_before (jiffies, ehci->next_statechange))
42 port = HCS_N_PORTS (ehci->hcs_params);
43 spin_lock_irq (&ehci->lock);
45 /* stop schedules, clean any completed work */
46 if (HC_IS_RUNNING(hcd->state)) {
48 hcd->state = HC_STATE_QUIESCING;
50 ehci->command = ehci_readl(ehci, &ehci->regs->command);
52 ehci->reclaim_ready = 1;
55 /* Unlike other USB host controller types, EHCI doesn't have
56 * any notion of "global" or bus-wide suspend. The driver has
57 * to manually suspend all the active unsuspended ports, and
58 * then manually resume them in the bus_resume() routine.
60 ehci->bus_suspended = 0;
62 u32 __iomem *reg = &ehci->regs->port_status [port];
63 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
66 /* keep track of which ports we suspend */
67 if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
68 !(t1 & PORT_SUSPEND)) {
70 set_bit(port, &ehci->bus_suspended);
73 /* enable remote wakeup on all ports */
74 if (device_may_wakeup(&hcd->self.root_hub->dev))
75 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
77 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
80 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
82 ehci_writel(ehci, t2, reg);
86 /* turn off now-idle HC */
87 del_timer_sync (&ehci->watchdog);
89 hcd->state = HC_STATE_SUSPENDED;
91 /* allow remote wakeup */
93 if (!device_may_wakeup(&hcd->self.root_hub->dev))
95 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
96 ehci_readl(ehci, &ehci->regs->intr_enable);
98 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
99 spin_unlock_irq (&ehci->lock);
104 /* caller has locked the root hub, and should reset/reinit on error */
105 static int ehci_bus_resume (struct usb_hcd *hcd)
107 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
111 if (time_before (jiffies, ehci->next_statechange))
113 spin_lock_irq (&ehci->lock);
115 /* Ideally and we've got a real resume here, and no port's power
116 * was lost. (For PCI, that means Vaux was maintained.) But we
117 * could instead be restoring a swsusp snapshot -- so that BIOS was
118 * the last user of the controller, not reset/pm hardware keeping
119 * state we gave to it.
121 temp = ehci_readl(ehci, &ehci->regs->intr_enable);
122 ehci_dbg(ehci, "resume root hub%s\n", temp ? "" : " after power loss");
124 /* at least some APM implementations will try to deliver
125 * IRQs right away, so delay them until we're ready.
127 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
129 /* re-init operational registers */
130 ehci_writel(ehci, 0, &ehci->regs->segment);
131 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
132 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
134 /* restore CMD_RUN, framelist size, and irq threshold */
135 ehci_writel(ehci, ehci->command, &ehci->regs->command);
137 /* manually resume the ports we suspended during bus_suspend() */
138 i = HCS_N_PORTS (ehci->hcs_params);
140 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
141 temp &= ~(PORT_RWC_BITS
142 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
143 if (test_bit(i, &ehci->bus_suspended) &&
144 (temp & PORT_SUSPEND)) {
145 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
148 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
150 i = HCS_N_PORTS (ehci->hcs_params);
153 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
154 if (test_bit(i, &ehci->bus_suspended) &&
155 (temp & PORT_SUSPEND)) {
156 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
157 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
158 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
161 (void) ehci_readl(ehci, &ehci->regs->command);
163 /* maybe re-activate the schedule(s) */
165 if (ehci->async->qh_next.qh)
167 if (ehci->periodic_sched)
170 ehci->command |= temp;
171 ehci_writel(ehci, ehci->command, &ehci->regs->command);
174 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
175 hcd->state = HC_STATE_RUNNING;
177 /* Now we can safely re-enable irqs */
178 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
180 spin_unlock_irq (&ehci->lock);
186 #define ehci_bus_suspend NULL
187 #define ehci_bus_resume NULL
189 #endif /* CONFIG_PM */
191 /*-------------------------------------------------------------------------*/
193 static int check_reset_complete (
194 struct ehci_hcd *ehci,
198 if (!(port_status & PORT_CONNECT)) {
199 ehci->reset_done [index] = 0;
203 /* if reset finished and it's still not enabled -- handoff */
204 if (!(port_status & PORT_PE)) {
206 /* with integrated TT, there's nobody to hand it to! */
207 if (ehci_is_TDI(ehci)) {
209 "Failed to enable port %d on root hub TT\n",
214 ehci_dbg (ehci, "port %d full speed --> companion\n",
217 // what happens if HCS_N_CC(params) == 0 ?
218 port_status |= PORT_OWNER;
219 port_status &= ~PORT_RWC_BITS;
220 ehci_writel(ehci, port_status,
221 &ehci->regs->port_status [index]);
224 ehci_dbg (ehci, "port %d high speed\n", index + 1);
229 /*-------------------------------------------------------------------------*/
232 /* build "status change" packet (one or two bytes) from HC registers */
235 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
237 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
238 u32 temp, status = 0;
240 int ports, i, retval = 1;
243 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
244 if (!HC_IS_RUNNING(hcd->state))
247 /* init status to no-changes */
249 ports = HCS_N_PORTS (ehci->hcs_params);
255 /* Some boards (mostly VIA?) report bogus overcurrent indications,
256 * causing massive log spam unless we completely ignore them. It
257 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
258 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
259 * PORT_POWER; that's surprising, but maybe within-spec.
262 mask = PORT_CSC | PORT_PEC | PORT_OCC;
264 mask = PORT_CSC | PORT_PEC;
265 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
267 /* no hub change reports (bit 0) for now (power, ...) */
269 /* port N changes (bit N)? */
270 spin_lock_irqsave (&ehci->lock, flags);
271 for (i = 0; i < ports; i++) {
272 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
273 if (temp & PORT_OWNER) {
274 /* don't report this in GetPortStatus */
275 if (temp & PORT_CSC) {
276 temp &= ~PORT_RWC_BITS;
278 ehci_writel(ehci, temp,
279 &ehci->regs->port_status [i]);
283 if (!(temp & PORT_CONNECT))
284 ehci->reset_done [i] = 0;
285 if ((temp & mask) != 0
286 || ((temp & PORT_RESUME) != 0
287 && time_after (jiffies,
288 ehci->reset_done [i]))) {
290 buf [0] |= 1 << (i + 1);
292 buf [1] |= 1 << (i - 7);
296 /* FIXME autosuspend idle root hubs */
297 spin_unlock_irqrestore (&ehci->lock, flags);
298 return status ? retval : 0;
301 /*-------------------------------------------------------------------------*/
304 ehci_hub_descriptor (
305 struct ehci_hcd *ehci,
306 struct usb_hub_descriptor *desc
308 int ports = HCS_N_PORTS (ehci->hcs_params);
311 desc->bDescriptorType = 0x29;
312 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
313 desc->bHubContrCurrent = 0;
315 desc->bNbrPorts = ports;
316 temp = 1 + (ports / 8);
317 desc->bDescLength = 7 + 2 * temp;
319 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
320 memset (&desc->bitmap [0], 0, temp);
321 memset (&desc->bitmap [temp], 0xff, temp);
323 temp = 0x0008; /* per-port overcurrent reporting */
324 if (HCS_PPC (ehci->hcs_params))
325 temp |= 0x0001; /* per-port power control */
327 temp |= 0x0002; /* no power switching */
329 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
330 if (HCS_INDICATOR (ehci->hcs_params))
331 temp |= 0x0080; /* per-port indicators (LEDs) */
333 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
336 /*-------------------------------------------------------------------------*/
338 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
340 static int ehci_hub_control (
348 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
349 int ports = HCS_N_PORTS (ehci->hcs_params);
356 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
357 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
358 * (track current state ourselves) ... blink for diagnostics,
359 * power, "this is the one", etc. EHCI spec supports this.
362 spin_lock_irqsave (&ehci->lock, flags);
364 case ClearHubFeature:
366 case C_HUB_LOCAL_POWER:
367 case C_HUB_OVER_CURRENT:
368 /* no hub-wide feature/status flags */
374 case ClearPortFeature:
375 if (!wIndex || wIndex > ports)
378 temp = ehci_readl(ehci, &ehci->regs->port_status [wIndex]);
379 if (temp & PORT_OWNER)
383 case USB_PORT_FEAT_ENABLE:
384 ehci_writel(ehci, temp & ~PORT_PE,
385 &ehci->regs->port_status [wIndex]);
387 case USB_PORT_FEAT_C_ENABLE:
388 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
389 &ehci->regs->port_status [wIndex]);
391 case USB_PORT_FEAT_SUSPEND:
392 if (temp & PORT_RESET)
394 if (ehci->no_selective_suspend)
396 if (temp & PORT_SUSPEND) {
397 if ((temp & PORT_PE) == 0)
399 /* resume signaling for 20 msec */
400 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
401 ehci_writel(ehci, temp | PORT_RESUME,
402 &ehci->regs->port_status [wIndex]);
403 ehci->reset_done [wIndex] = jiffies
404 + msecs_to_jiffies (20);
407 case USB_PORT_FEAT_C_SUSPEND:
408 /* we auto-clear this feature */
410 case USB_PORT_FEAT_POWER:
411 if (HCS_PPC (ehci->hcs_params))
413 temp & ~(PORT_RWC_BITS | PORT_POWER),
414 &ehci->regs->port_status [wIndex]);
416 case USB_PORT_FEAT_C_CONNECTION:
417 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
418 &ehci->regs->port_status [wIndex]);
420 case USB_PORT_FEAT_C_OVER_CURRENT:
421 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
422 &ehci->regs->port_status [wIndex]);
424 case USB_PORT_FEAT_C_RESET:
425 /* GetPortStatus clears reset */
430 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
432 case GetHubDescriptor:
433 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
437 /* no hub-wide feature/status flags */
439 //cpu_to_le32s ((u32 *) buf);
442 if (!wIndex || wIndex > ports)
446 temp = ehci_readl(ehci, &ehci->regs->port_status [wIndex]);
450 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
452 status |= 1 << USB_PORT_FEAT_C_ENABLE;
453 if ((temp & PORT_OCC) && !ignore_oc)
454 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
456 /* whoever resumes must GetPortStatus to complete it!! */
457 if ((temp & PORT_RESUME)
458 && time_after (jiffies,
459 ehci->reset_done [wIndex])) {
460 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
461 ehci->reset_done [wIndex] = 0;
463 /* stop resume signaling */
464 temp = ehci_readl(ehci,
465 &ehci->regs->port_status [wIndex]);
467 temp & ~(PORT_RWC_BITS | PORT_RESUME),
468 &ehci->regs->port_status [wIndex]);
469 retval = handshake(ehci,
470 &ehci->regs->port_status [wIndex],
471 PORT_RESUME, 0, 2000 /* 2msec */);
473 ehci_err (ehci, "port %d resume error %d\n",
477 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
480 /* whoever resets must GetPortStatus to complete it!! */
481 if ((temp & PORT_RESET)
482 && time_after (jiffies,
483 ehci->reset_done [wIndex])) {
484 status |= 1 << USB_PORT_FEAT_C_RESET;
485 ehci->reset_done [wIndex] = 0;
487 /* force reset to complete */
488 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
489 &ehci->regs->port_status [wIndex]);
490 /* REVISIT: some hardware needs 550+ usec to clear
491 * this bit; seems too long to spin routinely...
493 retval = handshake(ehci,
494 &ehci->regs->port_status [wIndex],
497 ehci_err (ehci, "port %d reset error %d\n",
502 /* see what we found out */
503 temp = check_reset_complete (ehci, wIndex,
505 &ehci->regs->port_status [wIndex]));
508 // don't show wPortStatus if it's owned by a companion hc
509 if (!(temp & PORT_OWNER)) {
510 if (temp & PORT_CONNECT) {
511 status |= 1 << USB_PORT_FEAT_CONNECTION;
512 // status may be from integrated TT
513 status |= ehci_port_speed(ehci, temp);
516 status |= 1 << USB_PORT_FEAT_ENABLE;
517 if (temp & (PORT_SUSPEND|PORT_RESUME))
518 status |= 1 << USB_PORT_FEAT_SUSPEND;
520 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
521 if (temp & PORT_RESET)
522 status |= 1 << USB_PORT_FEAT_RESET;
523 if (temp & PORT_POWER)
524 status |= 1 << USB_PORT_FEAT_POWER;
527 #ifndef EHCI_VERBOSE_DEBUG
528 if (status & ~0xffff) /* only if wPortChange is interesting */
530 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
531 // we "know" this alignment is good, caller used kmalloc()...
532 *((__le32 *) buf) = cpu_to_le32 (status);
536 case C_HUB_LOCAL_POWER:
537 case C_HUB_OVER_CURRENT:
538 /* no hub-wide feature/status flags */
545 selector = wIndex >> 8;
547 if (!wIndex || wIndex > ports)
550 temp = ehci_readl(ehci, &ehci->regs->port_status [wIndex]);
551 if (temp & PORT_OWNER)
554 temp &= ~PORT_RWC_BITS;
556 case USB_PORT_FEAT_SUSPEND:
557 if (ehci->no_selective_suspend)
559 if ((temp & PORT_PE) == 0
560 || (temp & PORT_RESET) != 0)
562 if (device_may_wakeup(&hcd->self.root_hub->dev))
563 temp |= PORT_WAKE_BITS;
564 ehci_writel(ehci, temp | PORT_SUSPEND,
565 &ehci->regs->port_status [wIndex]);
567 case USB_PORT_FEAT_POWER:
568 if (HCS_PPC (ehci->hcs_params))
569 ehci_writel(ehci, temp | PORT_POWER,
570 &ehci->regs->port_status [wIndex]);
572 case USB_PORT_FEAT_RESET:
573 if (temp & PORT_RESUME)
575 /* line status bits may report this as low speed,
576 * which can be fine if this root hub has a
577 * transaction translator built in.
579 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
580 && !ehci_is_TDI(ehci)
581 && PORT_USB11 (temp)) {
583 "port %d low speed --> companion\n",
587 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
592 * caller must wait, then call GetPortStatus
593 * usb 2.0 spec says 50 ms resets on root
595 ehci->reset_done [wIndex] = jiffies
596 + msecs_to_jiffies (50);
598 ehci_writel(ehci, temp,
599 &ehci->regs->port_status [wIndex]);
602 /* For downstream facing ports (these): one hub port is put
603 * into test mode according to USB2 11.24.2.13, then the hub
604 * must be reset (which for root hub now means rmmod+modprobe,
605 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
606 * about the EHCI-specific stuff.
608 case USB_PORT_FEAT_TEST:
609 if (!selector || selector > 5)
613 temp |= selector << 16;
614 ehci_writel(ehci, temp,
615 &ehci->regs->port_status [wIndex]);
621 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
626 /* "stall" on error */
629 spin_unlock_irqrestore (&ehci->lock, flags);