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 ehci_dbg(ehci, "suspend root hub\n");
41 if (time_before (jiffies, ehci->next_statechange))
44 port = HCS_N_PORTS (ehci->hcs_params);
45 spin_lock_irq (&ehci->lock);
47 /* stop schedules, clean any completed work */
48 if (HC_IS_RUNNING(hcd->state)) {
50 hcd->state = HC_STATE_QUIESCING;
52 ehci->command = ehci_readl(ehci, &ehci->regs->command);
54 ehci->reclaim_ready = 1;
57 /* Unlike other USB host controller types, EHCI doesn't have
58 * any notion of "global" or bus-wide suspend. The driver has
59 * to manually suspend all the active unsuspended ports, and
60 * then manually resume them in the bus_resume() routine.
62 ehci->bus_suspended = 0;
64 u32 __iomem *reg = &ehci->regs->port_status [port];
65 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
68 /* keep track of which ports we suspend */
69 if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
70 !(t1 & PORT_SUSPEND)) {
72 set_bit(port, &ehci->bus_suspended);
75 /* enable remote wakeup on all ports */
76 if (device_may_wakeup(&hcd->self.root_hub->dev))
77 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
79 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
82 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
84 ehci_writel(ehci, t2, reg);
88 /* turn off now-idle HC */
89 del_timer_sync (&ehci->watchdog);
91 hcd->state = HC_STATE_SUSPENDED;
93 /* allow remote wakeup */
95 if (!device_may_wakeup(&hcd->self.root_hub->dev))
97 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
98 ehci_readl(ehci, &ehci->regs->intr_enable);
100 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
101 spin_unlock_irq (&ehci->lock);
106 /* caller has locked the root hub, and should reset/reinit on error */
107 static int ehci_bus_resume (struct usb_hcd *hcd)
109 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
113 if (time_before (jiffies, ehci->next_statechange))
115 spin_lock_irq (&ehci->lock);
117 /* Ideally and we've got a real resume here, and no port's power
118 * was lost. (For PCI, that means Vaux was maintained.) But we
119 * could instead be restoring a swsusp snapshot -- so that BIOS was
120 * the last user of the controller, not reset/pm hardware keeping
121 * state we gave to it.
123 temp = ehci_readl(ehci, &ehci->regs->intr_enable);
124 ehci_dbg(ehci, "resume root hub%s\n", temp ? "" : " after power loss");
126 /* at least some APM implementations will try to deliver
127 * IRQs right away, so delay them until we're ready.
129 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
131 /* re-init operational registers */
132 ehci_writel(ehci, 0, &ehci->regs->segment);
133 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
134 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
136 /* restore CMD_RUN, framelist size, and irq threshold */
137 ehci_writel(ehci, ehci->command, &ehci->regs->command);
139 /* manually resume the ports we suspended during bus_suspend() */
140 i = HCS_N_PORTS (ehci->hcs_params);
142 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
143 temp &= ~(PORT_RWC_BITS
144 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
145 if (test_bit(i, &ehci->bus_suspended) &&
146 (temp & PORT_SUSPEND)) {
147 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
150 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
152 i = HCS_N_PORTS (ehci->hcs_params);
155 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
156 if (test_bit(i, &ehci->bus_suspended) &&
157 (temp & PORT_SUSPEND)) {
158 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
159 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
160 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
163 (void) ehci_readl(ehci, &ehci->regs->command);
165 /* maybe re-activate the schedule(s) */
167 if (ehci->async->qh_next.qh)
169 if (ehci->periodic_sched)
172 ehci->command |= temp;
173 ehci_writel(ehci, ehci->command, &ehci->regs->command);
176 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
177 hcd->state = HC_STATE_RUNNING;
179 /* Now we can safely re-enable irqs */
180 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
182 spin_unlock_irq (&ehci->lock);
188 #define ehci_bus_suspend NULL
189 #define ehci_bus_resume NULL
191 #endif /* CONFIG_PM */
193 /*-------------------------------------------------------------------------*/
195 /* Display the ports dedicated to the companion controller */
196 static ssize_t show_companion(struct class_device *class_dev, char *buf)
198 struct ehci_hcd *ehci;
199 int nports, index, n;
200 int count = PAGE_SIZE;
203 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
204 nports = HCS_N_PORTS(ehci->hcs_params);
206 for (index = 0; index < nports; ++index) {
207 if (test_bit(index, &ehci->companion_ports)) {
208 n = scnprintf(ptr, count, "%d\n", index + 1);
217 * Dedicate or undedicate a port to the companion controller.
218 * Syntax is "[-]portnum", where a leading '-' sign means
219 * return control of the port to the EHCI controller.
221 static ssize_t store_companion(struct class_device *class_dev,
222 const char *buf, size_t count)
224 struct ehci_hcd *ehci;
225 int portnum, new_owner, try;
226 u32 __iomem *status_reg;
229 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
230 new_owner = PORT_OWNER; /* Owned by companion */
231 if (sscanf(buf, "%d", &portnum) != 1)
235 new_owner = 0; /* Owned by EHCI */
237 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
239 status_reg = &ehci->regs->port_status[--portnum];
241 set_bit(portnum, &ehci->companion_ports);
243 clear_bit(portnum, &ehci->companion_ports);
246 * The controller won't set the OWNER bit if the port is
247 * enabled, so this loop will sometimes require at least two
248 * iterations: one to disable the port and one to set OWNER.
251 for (try = 4; try > 0; --try) {
252 spin_lock_irq(&ehci->lock);
253 port_status = ehci_readl(ehci, status_reg);
254 if ((port_status & PORT_OWNER) == new_owner
255 || (port_status & (PORT_OWNER | PORT_CONNECT))
259 port_status ^= PORT_OWNER;
260 port_status &= ~(PORT_PE | PORT_RWC_BITS);
261 ehci_writel(ehci, port_status, status_reg);
263 spin_unlock_irq(&ehci->lock);
269 static CLASS_DEVICE_ATTR(companion, 0644, show_companion, store_companion);
271 static inline void create_companion_file(struct ehci_hcd *ehci)
275 /* with integrated TT there is no companion! */
276 if (!ehci_is_TDI(ehci))
277 i = class_device_create_file(ehci_to_hcd(ehci)->self.class_dev,
278 &class_device_attr_companion);
281 static inline void remove_companion_file(struct ehci_hcd *ehci)
283 /* with integrated TT there is no companion! */
284 if (!ehci_is_TDI(ehci))
285 class_device_remove_file(ehci_to_hcd(ehci)->self.class_dev,
286 &class_device_attr_companion);
290 /*-------------------------------------------------------------------------*/
292 static int check_reset_complete (
293 struct ehci_hcd *ehci,
295 u32 __iomem *status_reg,
298 if (!(port_status & PORT_CONNECT)) {
299 ehci->reset_done [index] = 0;
303 /* if reset finished and it's still not enabled -- handoff */
304 if (!(port_status & PORT_PE)) {
306 /* with integrated TT, there's nobody to hand it to! */
307 if (ehci_is_TDI(ehci)) {
309 "Failed to enable port %d on root hub TT\n",
314 ehci_dbg (ehci, "port %d full speed --> companion\n",
317 // what happens if HCS_N_CC(params) == 0 ?
318 port_status |= PORT_OWNER;
319 port_status &= ~PORT_RWC_BITS;
320 ehci_writel(ehci, port_status, status_reg);
323 ehci_dbg (ehci, "port %d high speed\n", index + 1);
328 /*-------------------------------------------------------------------------*/
331 /* build "status change" packet (one or two bytes) from HC registers */
334 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
336 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
337 u32 temp, status = 0;
339 int ports, i, retval = 1;
342 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
343 if (!HC_IS_RUNNING(hcd->state))
346 /* init status to no-changes */
348 ports = HCS_N_PORTS (ehci->hcs_params);
354 /* Some boards (mostly VIA?) report bogus overcurrent indications,
355 * causing massive log spam unless we completely ignore them. It
356 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
357 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
358 * PORT_POWER; that's surprising, but maybe within-spec.
361 mask = PORT_CSC | PORT_PEC | PORT_OCC;
363 mask = PORT_CSC | PORT_PEC;
364 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
366 /* no hub change reports (bit 0) for now (power, ...) */
368 /* port N changes (bit N)? */
369 spin_lock_irqsave (&ehci->lock, flags);
370 for (i = 0; i < ports; i++) {
371 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
374 * Return status information even for ports with OWNER set.
375 * Otherwise khubd wouldn't see the disconnect event when a
376 * high-speed device is switched over to the companion
377 * controller by the user.
380 if (!(temp & PORT_CONNECT))
381 ehci->reset_done [i] = 0;
382 if ((temp & mask) != 0
383 || ((temp & PORT_RESUME) != 0
384 && time_after_eq(jiffies,
385 ehci->reset_done[i]))) {
387 buf [0] |= 1 << (i + 1);
389 buf [1] |= 1 << (i - 7);
393 /* FIXME autosuspend idle root hubs */
394 spin_unlock_irqrestore (&ehci->lock, flags);
395 return status ? retval : 0;
398 /*-------------------------------------------------------------------------*/
401 ehci_hub_descriptor (
402 struct ehci_hcd *ehci,
403 struct usb_hub_descriptor *desc
405 int ports = HCS_N_PORTS (ehci->hcs_params);
408 desc->bDescriptorType = 0x29;
409 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
410 desc->bHubContrCurrent = 0;
412 desc->bNbrPorts = ports;
413 temp = 1 + (ports / 8);
414 desc->bDescLength = 7 + 2 * temp;
416 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
417 memset (&desc->bitmap [0], 0, temp);
418 memset (&desc->bitmap [temp], 0xff, temp);
420 temp = 0x0008; /* per-port overcurrent reporting */
421 if (HCS_PPC (ehci->hcs_params))
422 temp |= 0x0001; /* per-port power control */
424 temp |= 0x0002; /* no power switching */
426 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
427 if (HCS_INDICATOR (ehci->hcs_params))
428 temp |= 0x0080; /* per-port indicators (LEDs) */
430 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
433 /*-------------------------------------------------------------------------*/
435 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
437 static int ehci_hub_control (
445 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
446 int ports = HCS_N_PORTS (ehci->hcs_params);
447 u32 __iomem *status_reg = &ehci->regs->port_status[wIndex - 1];
454 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
455 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
456 * (track current state ourselves) ... blink for diagnostics,
457 * power, "this is the one", etc. EHCI spec supports this.
460 spin_lock_irqsave (&ehci->lock, flags);
462 case ClearHubFeature:
464 case C_HUB_LOCAL_POWER:
465 case C_HUB_OVER_CURRENT:
466 /* no hub-wide feature/status flags */
472 case ClearPortFeature:
473 if (!wIndex || wIndex > ports)
476 temp = ehci_readl(ehci, status_reg);
479 * Even if OWNER is set, so the port is owned by the
480 * companion controller, khubd needs to be able to clear
481 * the port-change status bits (especially
482 * USB_PORT_FEAT_C_CONNECTION).
486 case USB_PORT_FEAT_ENABLE:
487 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
489 case USB_PORT_FEAT_C_ENABLE:
490 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
493 case USB_PORT_FEAT_SUSPEND:
494 if (temp & PORT_RESET)
496 if (ehci->no_selective_suspend)
498 if (temp & PORT_SUSPEND) {
499 if ((temp & PORT_PE) == 0)
501 /* resume signaling for 20 msec */
502 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
503 ehci_writel(ehci, temp | PORT_RESUME,
505 ehci->reset_done [wIndex] = jiffies
506 + msecs_to_jiffies (20);
509 case USB_PORT_FEAT_C_SUSPEND:
510 /* we auto-clear this feature */
512 case USB_PORT_FEAT_POWER:
513 if (HCS_PPC (ehci->hcs_params))
515 temp & ~(PORT_RWC_BITS | PORT_POWER),
518 case USB_PORT_FEAT_C_CONNECTION:
519 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
522 case USB_PORT_FEAT_C_OVER_CURRENT:
523 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
526 case USB_PORT_FEAT_C_RESET:
527 /* GetPortStatus clears reset */
532 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
534 case GetHubDescriptor:
535 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
539 /* no hub-wide feature/status flags */
541 //cpu_to_le32s ((u32 *) buf);
544 if (!wIndex || wIndex > ports)
548 temp = ehci_readl(ehci, status_reg);
552 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
554 status |= 1 << USB_PORT_FEAT_C_ENABLE;
555 if ((temp & PORT_OCC) && !ignore_oc)
556 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
558 /* whoever resumes must GetPortStatus to complete it!! */
559 if (temp & PORT_RESUME) {
561 /* Remote Wakeup received? */
562 if (!ehci->reset_done[wIndex]) {
563 /* resume signaling for 20 msec */
564 ehci->reset_done[wIndex] = jiffies
565 + msecs_to_jiffies(20);
566 /* check the port again */
567 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
568 ehci->reset_done[wIndex]);
571 /* resume completed? */
572 else if (time_after_eq(jiffies,
573 ehci->reset_done[wIndex])) {
574 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
575 ehci->reset_done[wIndex] = 0;
577 /* stop resume signaling */
578 temp = ehci_readl(ehci, status_reg);
580 temp & ~(PORT_RWC_BITS | PORT_RESUME),
582 retval = handshake(ehci, status_reg,
583 PORT_RESUME, 0, 2000 /* 2msec */);
586 "port %d resume error %d\n",
590 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
594 /* whoever resets must GetPortStatus to complete it!! */
595 if ((temp & PORT_RESET)
596 && time_after_eq(jiffies,
597 ehci->reset_done[wIndex])) {
598 status |= 1 << USB_PORT_FEAT_C_RESET;
599 ehci->reset_done [wIndex] = 0;
601 /* force reset to complete */
602 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
604 /* REVISIT: some hardware needs 550+ usec to clear
605 * this bit; seems too long to spin routinely...
607 retval = handshake(ehci, status_reg,
610 ehci_err (ehci, "port %d reset error %d\n",
615 /* see what we found out */
616 temp = check_reset_complete (ehci, wIndex, status_reg,
617 ehci_readl(ehci, status_reg));
620 /* transfer dedicated ports to the companion hc */
621 if ((temp & PORT_CONNECT) &&
622 test_bit(wIndex, &ehci->companion_ports)) {
623 temp &= ~PORT_RWC_BITS;
625 ehci_writel(ehci, temp, status_reg);
626 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
627 temp = ehci_readl(ehci, status_reg);
631 * Even if OWNER is set, there's no harm letting khubd
632 * see the wPortStatus values (they should all be 0 except
633 * for PORT_POWER anyway).
636 if (temp & PORT_CONNECT) {
637 status |= 1 << USB_PORT_FEAT_CONNECTION;
638 // status may be from integrated TT
639 status |= ehci_port_speed(ehci, temp);
642 status |= 1 << USB_PORT_FEAT_ENABLE;
643 if (temp & (PORT_SUSPEND|PORT_RESUME))
644 status |= 1 << USB_PORT_FEAT_SUSPEND;
646 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
647 if (temp & PORT_RESET)
648 status |= 1 << USB_PORT_FEAT_RESET;
649 if (temp & PORT_POWER)
650 status |= 1 << USB_PORT_FEAT_POWER;
652 #ifndef EHCI_VERBOSE_DEBUG
653 if (status & ~0xffff) /* only if wPortChange is interesting */
655 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
656 // we "know" this alignment is good, caller used kmalloc()...
657 *((__le32 *) buf) = cpu_to_le32 (status);
661 case C_HUB_LOCAL_POWER:
662 case C_HUB_OVER_CURRENT:
663 /* no hub-wide feature/status flags */
670 selector = wIndex >> 8;
672 if (!wIndex || wIndex > ports)
675 temp = ehci_readl(ehci, status_reg);
676 if (temp & PORT_OWNER)
679 temp &= ~PORT_RWC_BITS;
681 case USB_PORT_FEAT_SUSPEND:
682 if (ehci->no_selective_suspend)
684 if ((temp & PORT_PE) == 0
685 || (temp & PORT_RESET) != 0)
687 if (device_may_wakeup(&hcd->self.root_hub->dev))
688 temp |= PORT_WAKE_BITS;
689 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
691 case USB_PORT_FEAT_POWER:
692 if (HCS_PPC (ehci->hcs_params))
693 ehci_writel(ehci, temp | PORT_POWER,
696 case USB_PORT_FEAT_RESET:
697 if (temp & PORT_RESUME)
699 /* line status bits may report this as low speed,
700 * which can be fine if this root hub has a
701 * transaction translator built in.
703 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
704 && !ehci_is_TDI(ehci)
705 && PORT_USB11 (temp)) {
707 "port %d low speed --> companion\n",
711 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
716 * caller must wait, then call GetPortStatus
717 * usb 2.0 spec says 50 ms resets on root
719 ehci->reset_done [wIndex] = jiffies
720 + msecs_to_jiffies (50);
722 ehci_writel(ehci, temp, status_reg);
725 /* For downstream facing ports (these): one hub port is put
726 * into test mode according to USB2 11.24.2.13, then the hub
727 * must be reset (which for root hub now means rmmod+modprobe,
728 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
729 * about the EHCI-specific stuff.
731 case USB_PORT_FEAT_TEST:
732 if (!selector || selector > 5)
736 temp |= selector << 16;
737 ehci_writel(ehci, temp, status_reg);
743 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
748 /* "stall" on error */
751 spin_unlock_irqrestore (&ehci->lock, flags);