2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under GPL
10 /*-------------------------------------------------------------------------*/
13 * OHCI Root Hub ... the nonsharable stuff
16 #define dbg_port(hc,label,num,value) \
18 "%s roothub.portstatus [%d] " \
19 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
21 (temp & RH_PS_PRSC) ? " PRSC" : "", \
22 (temp & RH_PS_OCIC) ? " OCIC" : "", \
23 (temp & RH_PS_PSSC) ? " PSSC" : "", \
24 (temp & RH_PS_PESC) ? " PESC" : "", \
25 (temp & RH_PS_CSC) ? " CSC" : "", \
27 (temp & RH_PS_LSDA) ? " LSDA" : "", \
28 (temp & RH_PS_PPS) ? " PPS" : "", \
29 (temp & RH_PS_PRS) ? " PRS" : "", \
30 (temp & RH_PS_POCI) ? " POCI" : "", \
31 (temp & RH_PS_PSS) ? " PSS" : "", \
33 (temp & RH_PS_PES) ? " PES" : "", \
34 (temp & RH_PS_CCS) ? " CCS" : "" \
37 /*-------------------------------------------------------------------------*/
39 #define OHCI_SCHED_ENABLES \
40 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
42 static void update_done_list(struct ohci_hcd *);
43 static void process_done_list(struct ohci_hcd *);
44 static void finish_unlinks (struct ohci_hcd *, u16);
47 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
48 __releases(ohci->lock)
49 __acquires(ohci->lock)
53 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
54 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
56 ohci_dbg (ohci, "resume/suspend?\n");
57 ohci->hc_control &= ~OHCI_CTRL_HCFS;
58 ohci->hc_control |= OHCI_USB_RESET;
59 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
60 (void) ohci_readl (ohci, &ohci->regs->control);
64 ohci_dbg (ohci, "needs reinit!\n");
66 case OHCI_USB_SUSPEND:
67 if (!ohci->autostop) {
68 ohci_dbg (ohci, "already suspended\n");
72 ohci_dbg (ohci, "%s root hub\n",
73 autostop ? "auto-stop" : "suspend");
75 /* First stop any processing */
76 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
77 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
78 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
79 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
80 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
82 /* sched disables take effect on the next frame,
83 * then the last WDH could take 6+ msec
85 ohci_dbg (ohci, "stopping schedules ...\n");
87 spin_unlock_irq (&ohci->lock);
89 spin_lock_irq (&ohci->lock);
91 update_done_list(ohci);
92 process_done_list(ohci);
93 finish_unlinks (ohci, ohci_frame_no(ohci));
96 * Some controllers don't handle "global" suspend properly if
97 * there are unsuspended ports. For these controllers, put all
98 * the enabled ports into suspend before suspending the root hub.
100 if (ohci->flags & OHCI_QUIRK_GLOBAL_SUSPEND) {
101 __hc32 __iomem *portstat = ohci->regs->roothub.portstatus;
105 for (i = 0; i < ohci->num_ports; (++i, ++portstat)) {
106 temp = ohci_readl(ohci, portstat);
107 if ((temp & (RH_PS_PES | RH_PS_PSS)) ==
109 ohci_writel(ohci, RH_PS_PSS, portstat);
113 /* maybe resume can wake root hub */
114 if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
115 ohci->hc_control |= OHCI_CTRL_RWE;
117 ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
118 &ohci->regs->intrdisable);
119 ohci->hc_control &= ~OHCI_CTRL_RWE;
122 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
123 * which doesn't imply ports will first be individually suspended.
125 ohci->hc_control &= ~OHCI_CTRL_HCFS;
126 ohci->hc_control |= OHCI_USB_SUSPEND;
127 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
128 (void) ohci_readl (ohci, &ohci->regs->control);
130 /* no resumes until devices finish suspending */
132 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
134 ohci->rh_state = OHCI_RH_SUSPENDED;
141 static inline struct ed *find_head (struct ed *ed)
143 /* for bulk and control lists */
149 /* caller has locked the root hub */
150 static int ohci_rh_resume (struct ohci_hcd *ohci)
151 __releases(ohci->lock)
152 __acquires(ohci->lock)
154 struct usb_hcd *hcd = ohci_to_hcd (ohci);
156 int status = -EINPROGRESS;
157 int autostopped = ohci->autostop;
160 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
162 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
163 /* this can happen after resuming a swsusp snapshot */
164 if (ohci->rh_state != OHCI_RH_RUNNING) {
165 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
168 /* this happens when pmcore resumes HC then root */
170 ohci_dbg (ohci, "duplicate resume\n");
173 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
174 case OHCI_USB_SUSPEND:
175 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
176 ohci->hc_control |= OHCI_USB_RESUME;
177 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
178 (void) ohci_readl (ohci, &ohci->regs->control);
179 ohci_dbg (ohci, "%s root hub\n",
180 autostopped ? "auto-start" : "resume");
182 case OHCI_USB_RESUME:
183 /* HCFS changes sometime after INTR_RD */
184 ohci_dbg(ohci, "%swakeup root hub\n",
185 autostopped ? "auto-" : "");
188 /* this can happen after resuming a swsusp snapshot */
189 ohci_dbg (ohci, "snapshot resume? reinit\n");
192 default: /* RESET, we lost power */
193 ohci_dbg (ohci, "lost power\n");
196 if (status == -EBUSY) {
198 spin_unlock_irq (&ohci->lock);
199 status = ohci_restart (ohci);
201 usb_root_hub_lost_power(hcd->self.root_hub);
203 spin_lock_irq (&ohci->lock);
207 if (status != -EINPROGRESS)
211 spin_unlock_irq (&ohci->lock);
213 /* Some controllers (lucent erratum) need extra-long delays */
214 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
216 temp = ohci_readl (ohci, &ohci->regs->control);
217 temp &= OHCI_CTRL_HCFS;
218 if (temp != OHCI_USB_RESUME) {
219 ohci_err (ohci, "controller won't resume\n");
220 spin_lock_irq(&ohci->lock);
224 /* disable old schedule state, reinit from scratch */
225 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
226 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
227 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
228 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
229 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
230 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
232 /* Sometimes PCI D3 suspend trashes frame timings ... */
233 periodic_reinit (ohci);
236 * The following code is executed with ohci->lock held and
237 * irqs disabled if and only if autostopped is true. This
238 * will cause sparse to warn about a "context imbalance".
241 /* interrupts might have been disabled */
242 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
243 if (ohci->ed_rm_list)
244 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
246 /* Then re-enable operations */
247 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
248 (void) ohci_readl (ohci, &ohci->regs->control);
252 temp = ohci->hc_control;
253 temp &= OHCI_CTRL_RWC;
254 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
255 ohci->hc_control = temp;
256 ohci_writel (ohci, temp, &ohci->regs->control);
257 (void) ohci_readl (ohci, &ohci->regs->control);
262 spin_lock_irq (&ohci->lock);
264 /* now ohci->lock is always held and irqs are always disabled */
266 /* keep it alive for more than ~5x suspend + resume costs */
267 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
269 /* maybe turn schedules back on */
272 if (!ohci->ed_rm_list) {
273 if (ohci->ed_controltail) {
275 find_head (ohci->ed_controltail)->dma,
276 &ohci->regs->ed_controlhead);
277 enables |= OHCI_CTRL_CLE;
280 if (ohci->ed_bulktail) {
281 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
282 &ohci->regs->ed_bulkhead);
283 enables |= OHCI_CTRL_BLE;
287 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
288 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
290 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
291 ohci->hc_control |= enables;
292 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
294 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
295 (void) ohci_readl (ohci, &ohci->regs->control);
298 ohci->rh_state = OHCI_RH_RUNNING;
302 static int ohci_bus_suspend (struct usb_hcd *hcd)
304 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
307 spin_lock_irq (&ohci->lock);
309 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
312 rc = ohci_rh_suspend (ohci, 0);
313 spin_unlock_irq (&ohci->lock);
317 static int ohci_bus_resume (struct usb_hcd *hcd)
319 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
322 if (time_before (jiffies, ohci->next_statechange))
325 spin_lock_irq (&ohci->lock);
327 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
330 rc = ohci_rh_resume (ohci);
331 spin_unlock_irq (&ohci->lock);
333 /* poll until we know a device is connected or we autostop */
335 usb_hcd_poll_rh_status(hcd);
339 /* Carry out polling-, autostop-, and autoresume-related state changes */
340 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
341 int any_connected, int rhsc_status)
346 /* Some broken controllers never turn off RHSC in the interrupt
347 * status register. For their sake we won't re-enable RHSC
348 * interrupts if the interrupt bit is already active.
350 rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
353 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
355 /* If no status changes are pending, enable RHSC interrupts. */
356 if (!rhsc_enable && !rhsc_status && !changed) {
357 rhsc_enable = OHCI_INTR_RHSC;
358 ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
361 /* Keep on polling until we know a device is connected
362 * and RHSC is enabled, or until we autostop.
364 if (!ohci->autostop) {
366 !device_may_wakeup(&ohci_to_hcd(ohci)
367 ->self.root_hub->dev)) {
372 ohci->next_statechange = jiffies + HZ;
375 /* if no devices have been attached for one second, autostop */
377 if (changed || any_connected) {
379 ohci->next_statechange = jiffies +
381 } else if (time_after_eq(jiffies,
382 ohci->next_statechange)
384 && !(ohci->hc_control &
385 OHCI_SCHED_ENABLES)) {
386 ohci_rh_suspend(ohci, 1);
393 case OHCI_USB_SUSPEND:
394 case OHCI_USB_RESUME:
395 /* if there is a port change, autostart or ask to be resumed */
398 ohci_rh_resume(ohci);
400 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
402 /* If remote wakeup is disabled, stop polling */
403 } else if (!ohci->autostop &&
404 !ohci_to_hcd(ohci)->self.root_hub->
409 /* If no status changes are pending,
410 * enable RHSC interrupts
412 if (!rhsc_enable && !rhsc_status) {
413 rhsc_enable = OHCI_INTR_RHSC;
414 ohci_writel(ohci, rhsc_enable,
415 &ohci->regs->intrenable);
417 /* Keep polling until RHSC is enabled */
426 #else /* CONFIG_PM */
428 static inline int ohci_rh_resume(struct ohci_hcd *ohci)
433 /* Carry out polling-related state changes.
434 * autostop isn't used when CONFIG_PM is turned off.
436 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
437 int any_connected, int rhsc_status)
439 /* If RHSC is enabled, don't poll */
440 if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
443 /* If status changes are pending, continue polling.
444 * Conversely, if no status changes are pending but the RHSC
445 * status bit was set, then RHSC may be broken so continue polling.
447 if (changed || rhsc_status)
450 /* It's safe to re-enable RHSC interrupts */
451 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
455 #endif /* CONFIG_PM */
457 /*-------------------------------------------------------------------------*/
459 /* build "status change" packet (one or two bytes) from HC registers */
461 int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
463 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
464 int i, changed = 0, length = 1;
465 int any_connected = 0;
469 spin_lock_irqsave (&ohci->lock, flags);
470 if (!HCD_HW_ACCESSIBLE(hcd))
473 /* undocumented erratum seen on at least rev D */
474 if ((ohci->flags & OHCI_QUIRK_AMD756)
475 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
476 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
477 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
478 /* retry later; "should not happen" */
483 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
484 buf [0] = changed = 1;
487 if (ohci->num_ports > 7) {
492 /* Clear the RHSC status flag before reading the port statuses */
493 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
494 rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
497 /* look at each port */
498 for (i = 0; i < ohci->num_ports; i++) {
499 u32 status = roothub_portstatus (ohci, i);
501 /* can't autostop if ports are connected */
502 any_connected |= (status & RH_PS_CCS);
504 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
505 | RH_PS_OCIC | RH_PS_PRSC)) {
508 buf [0] |= 1 << (i + 1);
510 buf [1] |= 1 << (i - 7);
514 if (ohci_root_hub_state_changes(ohci, changed,
515 any_connected, rhsc_status))
516 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
518 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
522 spin_unlock_irqrestore (&ohci->lock, flags);
524 return changed ? length : 0;
526 EXPORT_SYMBOL_GPL(ohci_hub_status_data);
528 /*-------------------------------------------------------------------------*/
531 ohci_hub_descriptor (
532 struct ohci_hcd *ohci,
533 struct usb_hub_descriptor *desc
535 u32 rh = roothub_a (ohci);
538 desc->bDescriptorType = 0x29;
539 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
540 desc->bHubContrCurrent = 0;
542 desc->bNbrPorts = ohci->num_ports;
543 temp = 1 + (ohci->num_ports / 8);
544 desc->bDescLength = 7 + 2 * temp;
547 if (rh & RH_A_NPS) /* no power switching? */
549 if (rh & RH_A_PSM) /* per-port power switching? */
551 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
553 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
555 desc->wHubCharacteristics = cpu_to_le16(temp);
557 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
558 rh = roothub_b (ohci);
559 memset(desc->u.hs.DeviceRemovable, 0xff,
560 sizeof(desc->u.hs.DeviceRemovable));
561 desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
562 if (ohci->num_ports > 7) {
563 desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
564 desc->u.hs.DeviceRemovable[2] = 0xff;
566 desc->u.hs.DeviceRemovable[1] = 0xff;
569 /*-------------------------------------------------------------------------*/
571 #ifdef CONFIG_USB_OTG
573 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
575 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
582 /* start port reset before HNP protocol times out */
583 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
584 if (!(status & RH_PS_CCS))
587 /* khubd will finish the reset later */
588 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
594 #define ohci_start_port_reset NULL
598 /*-------------------------------------------------------------------------*/
601 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
602 * not necessarily continuous ... to guard against resume signaling.
604 #define PORT_RESET_MSEC 50
606 /* this timer value might be vendor-specific ... */
607 #define PORT_RESET_HW_MSEC 10
609 /* wrap-aware logic morphed from <linux/jiffies.h> */
610 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
612 /* called from some task, normally khubd */
613 static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
615 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
617 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
618 u16 reset_done = now + PORT_RESET_MSEC;
619 int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
621 /* build a "continuous enough" reset signal, with up to
622 * 3msec gap between pulses. scheduler HZ==100 must work;
623 * this might need to be deadline-scheduled.
628 /* spin until any current reset finishes */
629 limit_2 = PORT_RESET_HW_MSEC * 2;
630 while (--limit_2 >= 0) {
631 temp = ohci_readl (ohci, portstat);
632 /* handle e.g. CardBus eject */
635 if (!(temp & RH_PS_PRS))
640 /* timeout (a hardware error) has been observed when
641 * EHCI sets CF while this driver is resetting a port;
642 * presumably other disconnect paths might do it too.
646 "port[%d] reset timeout, stat %08x\n",
651 if (!(temp & RH_PS_CCS))
653 if (temp & RH_PS_PRSC)
654 ohci_writel (ohci, RH_PS_PRSC, portstat);
656 /* start the next reset, sleep till it's probably done */
657 ohci_writel (ohci, RH_PS_PRS, portstat);
658 msleep(PORT_RESET_HW_MSEC);
659 now = ohci_readl(ohci, &ohci->regs->fmnumber);
660 } while (tick_before(now, reset_done) && --limit_1 >= 0);
662 /* caller synchronizes using PRSC ... and handles PRS
663 * still being set when this returns.
669 int ohci_hub_control(
677 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
678 int ports = ohci->num_ports;
682 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
686 case ClearHubFeature:
688 case C_HUB_OVER_CURRENT:
689 ohci_writel (ohci, RH_HS_OCIC,
690 &ohci->regs->roothub.status);
691 case C_HUB_LOCAL_POWER:
697 case ClearPortFeature:
698 if (!wIndex || wIndex > ports)
703 case USB_PORT_FEAT_ENABLE:
706 case USB_PORT_FEAT_C_ENABLE:
709 case USB_PORT_FEAT_SUSPEND:
712 case USB_PORT_FEAT_C_SUSPEND:
715 case USB_PORT_FEAT_POWER:
718 case USB_PORT_FEAT_C_CONNECTION:
721 case USB_PORT_FEAT_C_OVER_CURRENT:
724 case USB_PORT_FEAT_C_RESET:
730 ohci_writel (ohci, temp,
731 &ohci->regs->roothub.portstatus [wIndex]);
732 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
734 case GetHubDescriptor:
735 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
738 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
739 put_unaligned_le32(temp, buf);
742 if (!wIndex || wIndex > ports)
745 temp = roothub_portstatus (ohci, wIndex);
746 put_unaligned_le32(temp, buf);
748 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
749 dbg_port(ohci, "GetStatus", wIndex, temp);
753 case C_HUB_OVER_CURRENT:
754 // FIXME: this can be cleared, yes?
755 case C_HUB_LOCAL_POWER:
762 if (!wIndex || wIndex > ports)
766 case USB_PORT_FEAT_SUSPEND:
767 #ifdef CONFIG_USB_OTG
768 if (hcd->self.otg_port == (wIndex + 1)
769 && hcd->self.b_hnp_enable)
770 ohci->start_hnp(ohci);
773 ohci_writel (ohci, RH_PS_PSS,
774 &ohci->regs->roothub.portstatus [wIndex]);
776 case USB_PORT_FEAT_POWER:
777 ohci_writel (ohci, RH_PS_PPS,
778 &ohci->regs->roothub.portstatus [wIndex]);
780 case USB_PORT_FEAT_RESET:
781 retval = root_port_reset (ohci, wIndex);
790 /* "protocol stall" on error */
795 EXPORT_SYMBOL_GPL(ohci_hub_control);