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 /*-------------------------------------------------------------------------*/
30 #include <linux/usb/otg.h>
32 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
36 static int ehci_hub_control(
46 static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
48 return !udev->maxchild && udev->persist_enabled &&
49 udev->bus->root_hub->speed < USB_SPEED_HIGH;
52 /* After a power loss, ports that were owned by the companion must be
53 * reset so that the companion can still own them.
55 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
61 struct usb_hcd *hcd = ehci_to_hcd(ehci);
63 if (!ehci->owned_ports)
67 * USB 1.1 devices are mostly HIDs, which don't need to persist across
68 * suspends. If we ensure that none of our companion's devices have
69 * persist_enabled (by looking through all USB 1.1 buses in the system),
70 * we can skip this and avoid slowing resume down. Devices without
71 * persist will just get reenumerated shortly after resume anyway.
73 if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
76 /* Give the connections some time to appear */
79 port = HCS_N_PORTS(ehci->hcs_params);
81 if (test_bit(port, &ehci->owned_ports)) {
82 reg = &ehci->regs->port_status[port];
83 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
85 /* Port already owned by companion? */
86 if (status & PORT_OWNER)
87 clear_bit(port, &ehci->owned_ports);
88 else if (test_bit(port, &ehci->companion_ports))
89 ehci_writel(ehci, status & ~PORT_PE, reg);
91 ehci_hub_control(hcd, SetPortFeature,
92 USB_PORT_FEAT_RESET, port + 1,
97 if (!ehci->owned_ports)
99 msleep(90); /* Wait for resets to complete */
101 port = HCS_N_PORTS(ehci->hcs_params);
103 if (test_bit(port, &ehci->owned_ports)) {
104 ehci_hub_control(hcd, GetPortStatus,
106 (char *) &buf, sizeof(buf));
108 /* The companion should now own the port,
109 * but if something went wrong the port must not
112 reg = &ehci->regs->port_status[port];
113 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
114 if (status & PORT_OWNER)
115 ehci_writel(ehci, status | PORT_CSC, reg);
117 ehci_dbg(ehci, "failed handover port %d: %x\n",
119 ehci_writel(ehci, status & ~PORT_PE, reg);
124 ehci->owned_ports = 0;
127 static int __maybe_unused ehci_port_change(struct ehci_hcd *ehci)
129 int i = HCS_N_PORTS(ehci->hcs_params);
131 /* First check if the controller indicates a change event */
133 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
137 * Not all controllers appear to update this while going from D3 to D0,
138 * so check the individual port status registers as well
142 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
148 static __maybe_unused void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
149 bool suspending, bool do_wakeup)
155 /* If remote wakeup is enabled for the root hub but disabled
156 * for the controller, we must adjust all the port wakeup flags
157 * when the controller is suspended or resumed. In all other
158 * cases they don't need to be changed.
160 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
163 spin_lock_irqsave(&ehci->lock, flags);
165 /* clear phy low-power mode before changing wakeup flags */
166 if (ehci->has_hostpc) {
167 port = HCS_N_PORTS(ehci->hcs_params);
169 u32 __iomem *hostpc_reg;
171 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
172 + HOSTPC0 + 4 * port);
173 temp = ehci_readl(ehci, hostpc_reg);
174 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
176 spin_unlock_irqrestore(&ehci->lock, flags);
178 spin_lock_irqsave(&ehci->lock, flags);
181 port = HCS_N_PORTS(ehci->hcs_params);
183 u32 __iomem *reg = &ehci->regs->port_status[port];
184 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
185 u32 t2 = t1 & ~PORT_WAKE_BITS;
187 /* If we are suspending the controller, clear the flags.
188 * If we are resuming the controller, set the wakeup flags.
191 if (t1 & PORT_CONNECT)
192 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
194 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
196 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
198 ehci_writel(ehci, t2, reg);
201 /* enter phy low-power mode again */
202 if (ehci->has_hostpc) {
203 port = HCS_N_PORTS(ehci->hcs_params);
205 u32 __iomem *hostpc_reg;
207 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
208 + HOSTPC0 + 4 * port);
209 temp = ehci_readl(ehci, hostpc_reg);
210 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
214 /* Does the root hub have a port wakeup pending? */
215 if (!suspending && ehci_port_change(ehci))
216 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
218 spin_unlock_irqrestore(&ehci->lock, flags);
221 static int ehci_bus_suspend (struct usb_hcd *hcd)
223 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
228 ehci_dbg(ehci, "suspend root hub\n");
230 if (time_before (jiffies, ehci->next_statechange))
232 del_timer_sync(&ehci->watchdog);
233 del_timer_sync(&ehci->iaa_watchdog);
235 spin_lock_irq (&ehci->lock);
237 /* Once the controller is stopped, port resumes that are already
238 * in progress won't complete. Hence if remote wakeup is enabled
239 * for the root hub and any ports are in the middle of a resume or
240 * remote wakeup, we must fail the suspend.
242 if (hcd->self.root_hub->do_remote_wakeup) {
243 if (ehci->resuming_ports) {
244 spin_unlock_irq(&ehci->lock);
245 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
250 /* stop schedules, clean any completed work */
251 if (ehci->rh_state == EHCI_RH_RUNNING)
253 ehci->command = ehci_readl(ehci, &ehci->regs->command);
256 /* Unlike other USB host controller types, EHCI doesn't have
257 * any notion of "global" or bus-wide suspend. The driver has
258 * to manually suspend all the active unsuspended ports, and
259 * then manually resume them in the bus_resume() routine.
261 ehci->bus_suspended = 0;
262 ehci->owned_ports = 0;
264 port = HCS_N_PORTS(ehci->hcs_params);
266 u32 __iomem *reg = &ehci->regs->port_status [port];
267 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
268 u32 t2 = t1 & ~PORT_WAKE_BITS;
270 /* keep track of which ports we suspend */
272 set_bit(port, &ehci->owned_ports);
273 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
275 set_bit(port, &ehci->bus_suspended);
278 /* enable remote wakeup on all ports, if told to do so */
279 if (hcd->self.root_hub->do_remote_wakeup) {
280 /* only enable appropriate wake bits, otherwise the
281 * hardware can not go phy low power mode. If a race
282 * condition happens here(connection change during bits
283 * set), the port change detection will finally fix it.
285 if (t1 & PORT_CONNECT)
286 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
288 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
292 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
294 ehci_writel(ehci, t2, reg);
299 if (changed && ehci->has_hostpc) {
300 spin_unlock_irq(&ehci->lock);
301 msleep(5); /* 5 ms for HCD to enter low-power mode */
302 spin_lock_irq(&ehci->lock);
304 port = HCS_N_PORTS(ehci->hcs_params);
306 u32 __iomem *hostpc_reg;
309 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
310 + HOSTPC0 + 4 * port);
311 t3 = ehci_readl(ehci, hostpc_reg);
312 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
313 t3 = ehci_readl(ehci, hostpc_reg);
314 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
315 port, (t3 & HOSTPC_PHCD) ?
316 "succeeded" : "failed");
320 /* Apparently some devices need a >= 1-uframe delay here */
321 if (ehci->bus_suspended)
324 /* turn off now-idle HC */
326 ehci->rh_state = EHCI_RH_SUSPENDED;
329 end_unlink_async(ehci);
331 /* allow remote wakeup */
333 if (!hcd->self.root_hub->do_remote_wakeup)
335 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
336 ehci_readl(ehci, &ehci->regs->intr_enable);
338 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
339 spin_unlock_irq (&ehci->lock);
341 /* ehci_work() may have re-enabled the watchdog timer, which we do not
342 * want, and so we must delete any pending watchdog timer events.
344 del_timer_sync(&ehci->watchdog);
349 /* caller has locked the root hub, and should reset/reinit on error */
350 static int ehci_bus_resume (struct usb_hcd *hcd)
352 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
356 unsigned long resume_needed = 0;
358 if (time_before (jiffies, ehci->next_statechange))
360 spin_lock_irq (&ehci->lock);
361 if (!HCD_HW_ACCESSIBLE(hcd)) {
362 spin_unlock_irq(&ehci->lock);
366 if (unlikely(ehci->debug)) {
367 if (!dbgp_reset_prep())
370 dbgp_external_startup();
373 /* Ideally and we've got a real resume here, and no port's power
374 * was lost. (For PCI, that means Vaux was maintained.) But we
375 * could instead be restoring a swsusp snapshot -- so that BIOS was
376 * the last user of the controller, not reset/pm hardware keeping
377 * state we gave to it.
379 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
380 ehci_dbg(ehci, "resume root hub%s\n",
381 power_okay ? "" : " after power loss");
383 /* at least some APM implementations will try to deliver
384 * IRQs right away, so delay them until we're ready.
386 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
388 /* re-init operational registers */
389 ehci_writel(ehci, 0, &ehci->regs->segment);
390 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
391 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
393 /* restore CMD_RUN, framelist size, and irq threshold */
394 ehci_writel(ehci, ehci->command, &ehci->regs->command);
395 ehci->rh_state = EHCI_RH_RUNNING;
397 /* Some controller/firmware combinations need a delay during which
398 * they set up the port statuses. See Bugzilla #8190. */
399 spin_unlock_irq(&ehci->lock);
401 spin_lock_irq(&ehci->lock);
403 /* clear phy low-power mode before resume */
404 if (ehci->bus_suspended && ehci->has_hostpc) {
405 i = HCS_N_PORTS(ehci->hcs_params);
407 if (test_bit(i, &ehci->bus_suspended)) {
408 u32 __iomem *hostpc_reg;
410 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
412 temp = ehci_readl(ehci, hostpc_reg);
413 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
417 spin_unlock_irq(&ehci->lock);
419 spin_lock_irq(&ehci->lock);
422 /* manually resume the ports we suspended during bus_suspend() */
423 i = HCS_N_PORTS (ehci->hcs_params);
425 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
426 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
427 if (test_bit(i, &ehci->bus_suspended) &&
428 (temp & PORT_SUSPEND)) {
430 set_bit(i, &resume_needed);
432 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
435 /* msleep for 20ms only if code is trying to resume port */
437 spin_unlock_irq(&ehci->lock);
439 spin_lock_irq(&ehci->lock);
442 i = HCS_N_PORTS (ehci->hcs_params);
444 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
445 if (test_bit(i, &resume_needed)) {
446 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
447 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
448 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
451 (void) ehci_readl(ehci, &ehci->regs->command);
453 /* maybe re-activate the schedule(s) */
455 if (ehci->async->qh_next.qh)
457 if (ehci->periodic_sched)
460 ehci->command |= temp;
461 ehci_writel(ehci, ehci->command, &ehci->regs->command);
464 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
466 /* Now we can safely re-enable irqs */
467 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
469 spin_unlock_irq (&ehci->lock);
470 ehci_handover_companion_ports(ehci);
476 #define ehci_bus_suspend NULL
477 #define ehci_bus_resume NULL
479 #endif /* CONFIG_PM */
481 /*-------------------------------------------------------------------------*/
484 * Sets the owner of a port
486 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
488 u32 __iomem *status_reg;
492 status_reg = &ehci->regs->port_status[portnum];
495 * The controller won't set the OWNER bit if the port is
496 * enabled, so this loop will sometimes require at least two
497 * iterations: one to disable the port and one to set OWNER.
499 for (try = 4; try > 0; --try) {
500 spin_lock_irq(&ehci->lock);
501 port_status = ehci_readl(ehci, status_reg);
502 if ((port_status & PORT_OWNER) == new_owner
503 || (port_status & (PORT_OWNER | PORT_CONNECT))
507 port_status ^= PORT_OWNER;
508 port_status &= ~(PORT_PE | PORT_RWC_BITS);
509 ehci_writel(ehci, port_status, status_reg);
511 spin_unlock_irq(&ehci->lock);
517 /*-------------------------------------------------------------------------*/
519 static int check_reset_complete (
520 struct ehci_hcd *ehci,
522 u32 __iomem *status_reg,
525 if (!(port_status & PORT_CONNECT))
528 /* if reset finished and it's still not enabled -- handoff */
529 if (!(port_status & PORT_PE)) {
531 /* with integrated TT, there's nobody to hand it to! */
532 if (ehci_is_TDI(ehci)) {
534 "Failed to enable port %d on root hub TT\n",
539 ehci_dbg (ehci, "port %d full speed --> companion\n",
542 // what happens if HCS_N_CC(params) == 0 ?
543 port_status |= PORT_OWNER;
544 port_status &= ~PORT_RWC_BITS;
545 ehci_writel(ehci, port_status, status_reg);
547 /* ensure 440EPX ohci controller state is operational */
548 if (ehci->has_amcc_usb23)
549 set_ohci_hcfs(ehci, 1);
551 ehci_dbg (ehci, "port %d high speed\n", index + 1);
552 /* ensure 440EPx ohci controller state is suspended */
553 if (ehci->has_amcc_usb23)
554 set_ohci_hcfs(ehci, 0);
560 /*-------------------------------------------------------------------------*/
563 /* build "status change" packet (one or two bytes) from HC registers */
566 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
568 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
571 int ports, i, retval = 1;
575 /* init status to no-changes */
577 ports = HCS_N_PORTS (ehci->hcs_params);
583 /* Inform the core about resumes-in-progress by returning
584 * a non-zero value even if there are no status changes.
586 status = ehci->resuming_ports;
588 /* Some boards (mostly VIA?) report bogus overcurrent indications,
589 * causing massive log spam unless we completely ignore them. It
590 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
591 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
592 * PORT_POWER; that's surprising, but maybe within-spec.
595 mask = PORT_CSC | PORT_PEC | PORT_OCC;
597 mask = PORT_CSC | PORT_PEC;
598 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
600 /* no hub change reports (bit 0) for now (power, ...) */
602 /* port N changes (bit N)? */
603 spin_lock_irqsave (&ehci->lock, flags);
605 /* get per-port change detect bits */
607 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
609 for (i = 0; i < ports; i++) {
610 /* leverage per-port change bits feature */
611 if (ehci->has_ppcd && !(ppcd & (1 << i)))
613 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
616 * Return status information even for ports with OWNER set.
617 * Otherwise khubd wouldn't see the disconnect event when a
618 * high-speed device is switched over to the companion
619 * controller by the user.
622 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
623 || (ehci->reset_done[i] && time_after_eq(
624 jiffies, ehci->reset_done[i]))) {
626 buf [0] |= 1 << (i + 1);
628 buf [1] |= 1 << (i - 7);
633 /* If a resume is in progress, make sure it can finish */
634 if (ehci->resuming_ports)
635 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
637 spin_unlock_irqrestore (&ehci->lock, flags);
638 return status ? retval : 0;
641 /*-------------------------------------------------------------------------*/
644 ehci_hub_descriptor (
645 struct ehci_hcd *ehci,
646 struct usb_hub_descriptor *desc
648 int ports = HCS_N_PORTS (ehci->hcs_params);
651 desc->bDescriptorType = 0x29;
652 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
653 desc->bHubContrCurrent = 0;
655 desc->bNbrPorts = ports;
656 temp = 1 + (ports / 8);
657 desc->bDescLength = 7 + 2 * temp;
659 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
660 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
661 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
663 temp = 0x0008; /* per-port overcurrent reporting */
664 if (HCS_PPC (ehci->hcs_params))
665 temp |= 0x0001; /* per-port power control */
667 temp |= 0x0002; /* no power switching */
669 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
670 if (HCS_INDICATOR (ehci->hcs_params))
671 temp |= 0x0080; /* per-port indicators (LEDs) */
673 desc->wHubCharacteristics = cpu_to_le16(temp);
676 /*-------------------------------------------------------------------------*/
678 static int ehci_hub_control (
686 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
687 int ports = HCS_N_PORTS (ehci->hcs_params);
688 u32 __iomem *status_reg = &ehci->regs->port_status[
689 (wIndex & 0xff) - 1];
690 u32 __iomem *hostpc_reg = NULL;
691 u32 temp, temp1, status;
697 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
698 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
699 * (track current state ourselves) ... blink for diagnostics,
700 * power, "this is the one", etc. EHCI spec supports this.
703 if (ehci->has_hostpc)
704 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
705 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
706 spin_lock_irqsave (&ehci->lock, flags);
708 case ClearHubFeature:
710 case C_HUB_LOCAL_POWER:
711 case C_HUB_OVER_CURRENT:
712 /* no hub-wide feature/status flags */
718 case ClearPortFeature:
719 if (!wIndex || wIndex > ports)
722 temp = ehci_readl(ehci, status_reg);
725 * Even if OWNER is set, so the port is owned by the
726 * companion controller, khubd needs to be able to clear
727 * the port-change status bits (especially
728 * USB_PORT_STAT_C_CONNECTION).
732 case USB_PORT_FEAT_ENABLE:
733 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
735 case USB_PORT_FEAT_C_ENABLE:
736 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
739 case USB_PORT_FEAT_SUSPEND:
740 if (temp & PORT_RESET)
742 if (ehci->no_selective_suspend)
744 #ifdef CONFIG_USB_OTG
745 if ((hcd->self.otg_port == (wIndex + 1))
746 && hcd->self.b_hnp_enable) {
747 otg_start_hnp(ehci->transceiver->otg);
751 if (!(temp & PORT_SUSPEND))
753 if ((temp & PORT_PE) == 0)
756 /* clear phy low-power mode before resume */
758 temp1 = ehci_readl(ehci, hostpc_reg);
759 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
761 spin_unlock_irqrestore(&ehci->lock, flags);
762 msleep(5);/* wait to leave low-power mode */
763 spin_lock_irqsave(&ehci->lock, flags);
765 /* resume signaling for 20 msec */
766 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
767 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
768 ehci->reset_done[wIndex] = jiffies
769 + msecs_to_jiffies(20);
771 case USB_PORT_FEAT_C_SUSPEND:
772 clear_bit(wIndex, &ehci->port_c_suspend);
774 case USB_PORT_FEAT_POWER:
775 if (HCS_PPC (ehci->hcs_params))
777 temp & ~(PORT_RWC_BITS | PORT_POWER),
780 case USB_PORT_FEAT_C_CONNECTION:
782 /* clear PORTSC bits on disconnect */
784 temp &= ~PORT_DEV_ADDR;
786 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
789 case USB_PORT_FEAT_C_OVER_CURRENT:
790 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
793 case USB_PORT_FEAT_C_RESET:
794 /* GetPortStatus clears reset */
799 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
801 case GetHubDescriptor:
802 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
806 /* no hub-wide feature/status flags */
808 //cpu_to_le32s ((u32 *) buf);
811 if (!wIndex || wIndex > ports)
815 temp = ehci_readl(ehci, status_reg);
819 status |= USB_PORT_STAT_C_CONNECTION << 16;
821 status |= USB_PORT_STAT_C_ENABLE << 16;
823 if ((temp & PORT_OCC) && !ignore_oc){
824 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
827 * Hubs should disable port power on over-current.
828 * However, not all EHCI implementations do this
829 * automatically, even if they _do_ support per-port
830 * power switching; they're allowed to just limit the
831 * current. khubd will turn the power back on.
833 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
835 temp & ~(PORT_RWC_BITS | PORT_POWER),
837 temp = ehci_readl(ehci, status_reg);
841 /* whoever resumes must GetPortStatus to complete it!! */
842 if (temp & PORT_RESUME) {
844 /* Remote Wakeup received? */
845 if (!ehci->reset_done[wIndex]) {
846 /* resume signaling for 20 msec */
847 ehci->reset_done[wIndex] = jiffies
848 + msecs_to_jiffies(20);
849 /* check the port again */
850 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
851 ehci->reset_done[wIndex]);
854 /* resume completed? */
855 else if (time_after_eq(jiffies,
856 ehci->reset_done[wIndex])) {
857 clear_bit(wIndex, &ehci->suspended_ports);
858 set_bit(wIndex, &ehci->port_c_suspend);
859 ehci->reset_done[wIndex] = 0;
861 /* stop resume signaling */
862 temp = ehci_readl(ehci, status_reg);
864 temp & ~(PORT_RWC_BITS | PORT_RESUME),
866 clear_bit(wIndex, &ehci->resuming_ports);
867 retval = handshake(ehci, status_reg,
868 PORT_RESUME, 0, 2000 /* 2msec */);
871 "port %d resume error %d\n",
875 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
879 /* whoever resets must GetPortStatus to complete it!! */
880 if ((temp & PORT_RESET)
881 && time_after_eq(jiffies,
882 ehci->reset_done[wIndex])) {
883 status |= USB_PORT_STAT_C_RESET << 16;
884 ehci->reset_done [wIndex] = 0;
885 clear_bit(wIndex, &ehci->resuming_ports);
887 /* force reset to complete */
888 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
890 /* REVISIT: some hardware needs 550+ usec to clear
891 * this bit; seems too long to spin routinely...
893 retval = handshake(ehci, status_reg,
894 PORT_RESET, 0, 1000);
896 ehci_err (ehci, "port %d reset error %d\n",
901 /* see what we found out */
902 temp = check_reset_complete (ehci, wIndex, status_reg,
903 ehci_readl(ehci, status_reg));
906 if (!(temp & (PORT_RESUME|PORT_RESET))) {
907 ehci->reset_done[wIndex] = 0;
908 clear_bit(wIndex, &ehci->resuming_ports);
911 /* transfer dedicated ports to the companion hc */
912 if ((temp & PORT_CONNECT) &&
913 test_bit(wIndex, &ehci->companion_ports)) {
914 temp &= ~PORT_RWC_BITS;
916 ehci_writel(ehci, temp, status_reg);
917 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
918 temp = ehci_readl(ehci, status_reg);
922 * Even if OWNER is set, there's no harm letting khubd
923 * see the wPortStatus values (they should all be 0 except
924 * for PORT_POWER anyway).
927 if (temp & PORT_CONNECT) {
928 status |= USB_PORT_STAT_CONNECTION;
929 // status may be from integrated TT
930 if (ehci->has_hostpc) {
931 temp1 = ehci_readl(ehci, hostpc_reg);
932 status |= ehci_port_speed(ehci, temp1);
934 status |= ehci_port_speed(ehci, temp);
937 status |= USB_PORT_STAT_ENABLE;
939 /* maybe the port was unsuspended without our knowledge */
940 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
941 status |= USB_PORT_STAT_SUSPEND;
942 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
943 clear_bit(wIndex, &ehci->suspended_ports);
944 clear_bit(wIndex, &ehci->resuming_ports);
945 ehci->reset_done[wIndex] = 0;
947 set_bit(wIndex, &ehci->port_c_suspend);
951 status |= USB_PORT_STAT_OVERCURRENT;
952 if (temp & PORT_RESET)
953 status |= USB_PORT_STAT_RESET;
954 if (temp & PORT_POWER)
955 status |= USB_PORT_STAT_POWER;
956 if (test_bit(wIndex, &ehci->port_c_suspend))
957 status |= USB_PORT_STAT_C_SUSPEND << 16;
959 #ifndef VERBOSE_DEBUG
960 if (status & ~0xffff) /* only if wPortChange is interesting */
962 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
963 put_unaligned_le32(status, buf);
967 case C_HUB_LOCAL_POWER:
968 case C_HUB_OVER_CURRENT:
969 /* no hub-wide feature/status flags */
976 selector = wIndex >> 8;
978 if (unlikely(ehci->debug)) {
979 /* If the debug port is active any port
980 * feature requests should get denied */
981 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
982 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
987 if (!wIndex || wIndex > ports)
991 /* test if the port is configured to be ignored */
992 if (ehci->port_used_bitmap &&
993 !(ehci->port_used_bitmap & (1 << wIndex)))
996 temp = ehci_readl(ehci, status_reg);
997 if (temp & PORT_OWNER)
1000 temp &= ~PORT_RWC_BITS;
1002 case USB_PORT_FEAT_SUSPEND:
1003 if (ehci->no_selective_suspend)
1005 if ((temp & PORT_PE) == 0
1006 || (temp & PORT_RESET) != 0)
1009 /* After above check the port must be connected.
1010 * Set appropriate bit thus could put phy into low power
1011 * mode if we have hostpc feature
1013 temp &= ~PORT_WKCONN_E;
1014 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1015 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1017 spin_unlock_irqrestore(&ehci->lock, flags);
1018 msleep(5);/* 5ms for HCD enter low pwr mode */
1019 spin_lock_irqsave(&ehci->lock, flags);
1020 temp1 = ehci_readl(ehci, hostpc_reg);
1021 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1023 temp1 = ehci_readl(ehci, hostpc_reg);
1024 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1025 wIndex, (temp1 & HOSTPC_PHCD) ?
1026 "succeeded" : "failed");
1028 set_bit(wIndex, &ehci->suspended_ports);
1030 case USB_PORT_FEAT_POWER:
1031 if (HCS_PPC (ehci->hcs_params))
1032 ehci_writel(ehci, temp | PORT_POWER,
1035 case USB_PORT_FEAT_RESET:
1036 if (temp & PORT_RESUME)
1038 /* line status bits may report this as low speed,
1039 * which can be fine if this root hub has a
1040 * transaction translator built in.
1042 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1043 && !ehci_is_TDI(ehci)
1044 && PORT_USB11 (temp)) {
1046 "port %d low speed --> companion\n",
1050 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1055 * caller must wait, then call GetPortStatus
1056 * usb 2.0 spec says 50 ms resets on root
1058 ehci->reset_done [wIndex] = jiffies
1059 + msecs_to_jiffies (50);
1061 ehci_writel(ehci, temp, status_reg);
1064 /* For downstream facing ports (these): one hub port is put
1065 * into test mode according to USB2 11.24.2.13, then the hub
1066 * must be reset (which for root hub now means rmmod+modprobe,
1067 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1068 * about the EHCI-specific stuff.
1070 case USB_PORT_FEAT_TEST:
1071 if (!selector || selector > 5)
1075 /* Put all enabled ports into suspend */
1078 &ehci->regs->port_status[ports];
1080 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1082 ehci_writel(ehci, temp | PORT_SUSPEND,
1086 temp = ehci_readl(ehci, status_reg);
1087 temp |= selector << 16;
1088 ehci_writel(ehci, temp, status_reg);
1094 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1099 /* "stall" on error */
1103 spin_unlock_irqrestore (&ehci->lock, flags);
1107 static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
1110 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1112 if (ehci_is_TDI(ehci))
1114 set_owner(ehci, --portnum, PORT_OWNER);
1117 static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
1120 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1123 if (ehci_is_TDI(ehci))
1125 reg = &ehci->regs->port_status[portnum - 1];
1126 return ehci_readl(ehci, reg) & PORT_OWNER;