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 /* Display the ports dedicated to the companion controller */
194 static ssize_t show_companion(struct class_device *class_dev, char *buf)
196 struct ehci_hcd *ehci;
197 int nports, index, n;
198 int count = PAGE_SIZE;
201 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
202 nports = HCS_N_PORTS(ehci->hcs_params);
204 for (index = 0; index < nports; ++index) {
205 if (test_bit(index, &ehci->companion_ports)) {
206 n = scnprintf(ptr, count, "%d\n", index + 1);
215 * Dedicate or undedicate a port to the companion controller.
216 * Syntax is "[-]portnum", where a leading '-' sign means
217 * return control of the port to the EHCI controller.
219 static ssize_t store_companion(struct class_device *class_dev,
220 const char *buf, size_t count)
222 struct ehci_hcd *ehci;
223 int portnum, new_owner, try;
224 u32 __iomem *status_reg;
227 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
228 new_owner = PORT_OWNER; /* Owned by companion */
229 if (sscanf(buf, "%d", &portnum) != 1)
233 new_owner = 0; /* Owned by EHCI */
235 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
237 status_reg = &ehci->regs->port_status[--portnum];
239 set_bit(portnum, &ehci->companion_ports);
241 clear_bit(portnum, &ehci->companion_ports);
244 * The controller won't set the OWNER bit if the port is
245 * enabled, so this loop will sometimes require at least two
246 * iterations: one to disable the port and one to set OWNER.
249 for (try = 4; try > 0; --try) {
250 spin_lock_irq(&ehci->lock);
251 port_status = ehci_readl(ehci, status_reg);
252 if ((port_status & PORT_OWNER) == new_owner
253 || (port_status & (PORT_OWNER | PORT_CONNECT))
257 port_status ^= PORT_OWNER;
258 port_status &= ~(PORT_PE | PORT_RWC_BITS);
259 ehci_writel(ehci, port_status, status_reg);
261 spin_unlock_irq(&ehci->lock);
267 static CLASS_DEVICE_ATTR(companion, 0644, show_companion, store_companion);
269 static inline void create_companion_file(struct ehci_hcd *ehci)
273 /* with integrated TT there is no companion! */
274 if (!ehci_is_TDI(ehci))
275 i = class_device_create_file(ehci_to_hcd(ehci)->self.class_dev,
276 &class_device_attr_companion);
279 static inline void remove_companion_file(struct ehci_hcd *ehci)
281 /* with integrated TT there is no companion! */
282 if (!ehci_is_TDI(ehci))
283 class_device_remove_file(ehci_to_hcd(ehci)->self.class_dev,
284 &class_device_attr_companion);
288 /*-------------------------------------------------------------------------*/
290 static int check_reset_complete (
291 struct ehci_hcd *ehci,
293 u32 __iomem *status_reg,
296 if (!(port_status & PORT_CONNECT)) {
297 ehci->reset_done [index] = 0;
301 /* if reset finished and it's still not enabled -- handoff */
302 if (!(port_status & PORT_PE)) {
304 /* with integrated TT, there's nobody to hand it to! */
305 if (ehci_is_TDI(ehci)) {
307 "Failed to enable port %d on root hub TT\n",
312 ehci_dbg (ehci, "port %d full speed --> companion\n",
315 // what happens if HCS_N_CC(params) == 0 ?
316 port_status |= PORT_OWNER;
317 port_status &= ~PORT_RWC_BITS;
318 ehci_writel(ehci, port_status, status_reg);
321 ehci_dbg (ehci, "port %d high speed\n", index + 1);
326 /*-------------------------------------------------------------------------*/
329 /* build "status change" packet (one or two bytes) from HC registers */
332 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
334 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
335 u32 temp, status = 0;
337 int ports, i, retval = 1;
340 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
341 if (!HC_IS_RUNNING(hcd->state))
344 /* init status to no-changes */
346 ports = HCS_N_PORTS (ehci->hcs_params);
352 /* Some boards (mostly VIA?) report bogus overcurrent indications,
353 * causing massive log spam unless we completely ignore them. It
354 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
355 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
356 * PORT_POWER; that's surprising, but maybe within-spec.
359 mask = PORT_CSC | PORT_PEC | PORT_OCC;
361 mask = PORT_CSC | PORT_PEC;
362 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
364 /* no hub change reports (bit 0) for now (power, ...) */
366 /* port N changes (bit N)? */
367 spin_lock_irqsave (&ehci->lock, flags);
368 for (i = 0; i < ports; i++) {
369 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
372 * Return status information even for ports with OWNER set.
373 * Otherwise khubd wouldn't see the disconnect event when a
374 * high-speed device is switched over to the companion
375 * controller by the user.
378 if (!(temp & PORT_CONNECT))
379 ehci->reset_done [i] = 0;
380 if ((temp & mask) != 0
381 || ((temp & PORT_RESUME) != 0
382 && time_after_eq(jiffies,
383 ehci->reset_done[i]))) {
385 buf [0] |= 1 << (i + 1);
387 buf [1] |= 1 << (i - 7);
391 /* FIXME autosuspend idle root hubs */
392 spin_unlock_irqrestore (&ehci->lock, flags);
393 return status ? retval : 0;
396 /*-------------------------------------------------------------------------*/
399 ehci_hub_descriptor (
400 struct ehci_hcd *ehci,
401 struct usb_hub_descriptor *desc
403 int ports = HCS_N_PORTS (ehci->hcs_params);
406 desc->bDescriptorType = 0x29;
407 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
408 desc->bHubContrCurrent = 0;
410 desc->bNbrPorts = ports;
411 temp = 1 + (ports / 8);
412 desc->bDescLength = 7 + 2 * temp;
414 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
415 memset (&desc->bitmap [0], 0, temp);
416 memset (&desc->bitmap [temp], 0xff, temp);
418 temp = 0x0008; /* per-port overcurrent reporting */
419 if (HCS_PPC (ehci->hcs_params))
420 temp |= 0x0001; /* per-port power control */
422 temp |= 0x0002; /* no power switching */
424 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
425 if (HCS_INDICATOR (ehci->hcs_params))
426 temp |= 0x0080; /* per-port indicators (LEDs) */
428 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
431 /*-------------------------------------------------------------------------*/
433 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
435 static int ehci_hub_control (
443 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
444 int ports = HCS_N_PORTS (ehci->hcs_params);
445 u32 __iomem *status_reg = &ehci->regs->port_status[wIndex - 1];
452 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
453 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
454 * (track current state ourselves) ... blink for diagnostics,
455 * power, "this is the one", etc. EHCI spec supports this.
458 spin_lock_irqsave (&ehci->lock, flags);
460 case ClearHubFeature:
462 case C_HUB_LOCAL_POWER:
463 case C_HUB_OVER_CURRENT:
464 /* no hub-wide feature/status flags */
470 case ClearPortFeature:
471 if (!wIndex || wIndex > ports)
474 temp = ehci_readl(ehci, status_reg);
477 * Even if OWNER is set, so the port is owned by the
478 * companion controller, khubd needs to be able to clear
479 * the port-change status bits (especially
480 * USB_PORT_FEAT_C_CONNECTION).
484 case USB_PORT_FEAT_ENABLE:
485 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
487 case USB_PORT_FEAT_C_ENABLE:
488 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
491 case USB_PORT_FEAT_SUSPEND:
492 if (temp & PORT_RESET)
494 if (ehci->no_selective_suspend)
496 if (temp & PORT_SUSPEND) {
497 if ((temp & PORT_PE) == 0)
499 /* resume signaling for 20 msec */
500 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
501 ehci_writel(ehci, temp | PORT_RESUME,
503 ehci->reset_done [wIndex] = jiffies
504 + msecs_to_jiffies (20);
507 case USB_PORT_FEAT_C_SUSPEND:
508 /* we auto-clear this feature */
510 case USB_PORT_FEAT_POWER:
511 if (HCS_PPC (ehci->hcs_params))
513 temp & ~(PORT_RWC_BITS | PORT_POWER),
516 case USB_PORT_FEAT_C_CONNECTION:
517 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
520 case USB_PORT_FEAT_C_OVER_CURRENT:
521 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
524 case USB_PORT_FEAT_C_RESET:
525 /* GetPortStatus clears reset */
530 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
532 case GetHubDescriptor:
533 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
537 /* no hub-wide feature/status flags */
539 //cpu_to_le32s ((u32 *) buf);
542 if (!wIndex || wIndex > ports)
546 temp = ehci_readl(ehci, status_reg);
550 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
552 status |= 1 << USB_PORT_FEAT_C_ENABLE;
553 if ((temp & PORT_OCC) && !ignore_oc)
554 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
556 /* whoever resumes must GetPortStatus to complete it!! */
557 if (temp & PORT_RESUME) {
559 /* Remote Wakeup received? */
560 if (!ehci->reset_done[wIndex]) {
561 /* resume signaling for 20 msec */
562 ehci->reset_done[wIndex] = jiffies
563 + msecs_to_jiffies(20);
564 /* check the port again */
565 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
566 ehci->reset_done[wIndex]);
569 /* resume completed? */
570 else if (time_after_eq(jiffies,
571 ehci->reset_done[wIndex])) {
572 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
573 ehci->reset_done[wIndex] = 0;
575 /* stop resume signaling */
576 temp = ehci_readl(ehci, status_reg);
578 temp & ~(PORT_RWC_BITS | PORT_RESUME),
580 retval = handshake(ehci, status_reg,
581 PORT_RESUME, 0, 2000 /* 2msec */);
584 "port %d resume error %d\n",
588 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
592 /* whoever resets must GetPortStatus to complete it!! */
593 if ((temp & PORT_RESET)
594 && time_after_eq(jiffies,
595 ehci->reset_done[wIndex])) {
596 status |= 1 << USB_PORT_FEAT_C_RESET;
597 ehci->reset_done [wIndex] = 0;
599 /* force reset to complete */
600 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
602 /* REVISIT: some hardware needs 550+ usec to clear
603 * this bit; seems too long to spin routinely...
605 retval = handshake(ehci, status_reg,
608 ehci_err (ehci, "port %d reset error %d\n",
613 /* see what we found out */
614 temp = check_reset_complete (ehci, wIndex, status_reg,
615 ehci_readl(ehci, status_reg));
618 /* transfer dedicated ports to the companion hc */
619 if ((temp & PORT_CONNECT) &&
620 test_bit(wIndex, &ehci->companion_ports)) {
621 temp &= ~PORT_RWC_BITS;
623 ehci_writel(ehci, temp, status_reg);
624 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
625 temp = ehci_readl(ehci, status_reg);
629 * Even if OWNER is set, there's no harm letting khubd
630 * see the wPortStatus values (they should all be 0 except
631 * for PORT_POWER anyway).
634 if (temp & PORT_CONNECT) {
635 status |= 1 << USB_PORT_FEAT_CONNECTION;
636 // status may be from integrated TT
637 status |= ehci_port_speed(ehci, temp);
640 status |= 1 << USB_PORT_FEAT_ENABLE;
641 if (temp & (PORT_SUSPEND|PORT_RESUME))
642 status |= 1 << USB_PORT_FEAT_SUSPEND;
644 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
645 if (temp & PORT_RESET)
646 status |= 1 << USB_PORT_FEAT_RESET;
647 if (temp & PORT_POWER)
648 status |= 1 << USB_PORT_FEAT_POWER;
650 #ifndef EHCI_VERBOSE_DEBUG
651 if (status & ~0xffff) /* only if wPortChange is interesting */
653 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
654 // we "know" this alignment is good, caller used kmalloc()...
655 *((__le32 *) buf) = cpu_to_le32 (status);
659 case C_HUB_LOCAL_POWER:
660 case C_HUB_OVER_CURRENT:
661 /* no hub-wide feature/status flags */
668 selector = wIndex >> 8;
670 if (!wIndex || wIndex > ports)
673 temp = ehci_readl(ehci, status_reg);
674 if (temp & PORT_OWNER)
677 temp &= ~PORT_RWC_BITS;
679 case USB_PORT_FEAT_SUSPEND:
680 if (ehci->no_selective_suspend)
682 if ((temp & PORT_PE) == 0
683 || (temp & PORT_RESET) != 0)
685 if (device_may_wakeup(&hcd->self.root_hub->dev))
686 temp |= PORT_WAKE_BITS;
687 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
689 case USB_PORT_FEAT_POWER:
690 if (HCS_PPC (ehci->hcs_params))
691 ehci_writel(ehci, temp | PORT_POWER,
694 case USB_PORT_FEAT_RESET:
695 if (temp & PORT_RESUME)
697 /* line status bits may report this as low speed,
698 * which can be fine if this root hub has a
699 * transaction translator built in.
701 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
702 && !ehci_is_TDI(ehci)
703 && PORT_USB11 (temp)) {
705 "port %d low speed --> companion\n",
709 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
714 * caller must wait, then call GetPortStatus
715 * usb 2.0 spec says 50 ms resets on root
717 ehci->reset_done [wIndex] = jiffies
718 + msecs_to_jiffies (50);
720 ehci_writel(ehci, temp, status_reg);
723 /* For downstream facing ports (these): one hub port is put
724 * into test mode according to USB2 11.24.2.13, then the hub
725 * must be reset (which for root hub now means rmmod+modprobe,
726 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
727 * about the EHCI-specific stuff.
729 case USB_PORT_FEAT_TEST:
730 if (!selector || selector > 5)
734 temp |= selector << 16;
735 ehci_writel(ehci, temp, status_reg);
741 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
746 /* "stall" on error */
749 spin_unlock_irqrestore (&ehci->lock, flags);