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 /* hcd->hub_irq_enable() */
40 static void ohci_rhsc_enable (struct usb_hcd *hcd)
42 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
44 spin_lock_irq(&ohci->lock);
46 del_timer(&hcd->rh_timer); /* Prevent next poll */
47 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
48 spin_unlock_irq(&ohci->lock);
51 #define OHCI_SCHED_ENABLES \
52 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
54 static void dl_done_list (struct ohci_hcd *);
55 static void finish_unlinks (struct ohci_hcd *, u16);
58 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
59 __releases(ohci->lock)
60 __acquires(ohci->lock)
64 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
65 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
67 ohci_dbg (ohci, "resume/suspend?\n");
68 ohci->hc_control &= ~OHCI_CTRL_HCFS;
69 ohci->hc_control |= OHCI_USB_RESET;
70 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
71 (void) ohci_readl (ohci, &ohci->regs->control);
75 ohci_dbg (ohci, "needs reinit!\n");
77 case OHCI_USB_SUSPEND:
78 if (!ohci->autostop) {
79 ohci_dbg (ohci, "already suspended\n");
83 ohci_dbg (ohci, "%s root hub\n",
84 autostop ? "auto-stop" : "suspend");
86 /* First stop any processing */
87 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
88 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
89 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
90 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
91 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
93 /* sched disables take effect on the next frame,
94 * then the last WDH could take 6+ msec
96 ohci_dbg (ohci, "stopping schedules ...\n");
98 spin_unlock_irq (&ohci->lock);
100 spin_lock_irq (&ohci->lock);
103 finish_unlinks (ohci, ohci_frame_no(ohci));
105 /* maybe resume can wake root hub */
106 if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
107 ohci->hc_control |= OHCI_CTRL_RWE;
109 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
110 ohci->hc_control &= ~OHCI_CTRL_RWE;
113 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
114 * which doesn't imply ports will first be individually suspended.
116 ohci->hc_control &= ~OHCI_CTRL_HCFS;
117 ohci->hc_control |= OHCI_USB_SUSPEND;
118 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
119 (void) ohci_readl (ohci, &ohci->regs->control);
121 /* no resumes until devices finish suspending */
123 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
131 static inline struct ed *find_head (struct ed *ed)
133 /* for bulk and control lists */
139 /* caller has locked the root hub */
140 static int ohci_rh_resume (struct ohci_hcd *ohci)
141 __releases(ohci->lock)
142 __acquires(ohci->lock)
144 struct usb_hcd *hcd = ohci_to_hcd (ohci);
146 int status = -EINPROGRESS;
147 int autostopped = ohci->autostop;
150 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
152 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
153 /* this can happen after resuming a swsusp snapshot */
154 if (hcd->state == HC_STATE_RESUMING) {
155 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
158 /* this happens when pmcore resumes HC then root */
160 ohci_dbg (ohci, "duplicate resume\n");
163 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
164 case OHCI_USB_SUSPEND:
165 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
166 ohci->hc_control |= OHCI_USB_RESUME;
167 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
168 (void) ohci_readl (ohci, &ohci->regs->control);
169 ohci_dbg (ohci, "%s root hub\n",
170 autostopped ? "auto-start" : "resume");
172 case OHCI_USB_RESUME:
173 /* HCFS changes sometime after INTR_RD */
174 ohci_dbg(ohci, "%swakeup root hub\n",
175 autostopped ? "auto-" : "");
178 /* this can happen after resuming a swsusp snapshot */
179 ohci_dbg (ohci, "snapshot resume? reinit\n");
182 default: /* RESET, we lost power */
183 ohci_dbg (ohci, "lost power\n");
186 if (status == -EBUSY) {
188 spin_unlock_irq (&ohci->lock);
189 (void) ohci_init (ohci);
190 status = ohci_restart (ohci);
192 usb_root_hub_lost_power(hcd->self.root_hub);
194 spin_lock_irq (&ohci->lock);
198 if (status != -EINPROGRESS)
202 spin_unlock_irq (&ohci->lock);
204 /* Some controllers (lucent erratum) need extra-long delays */
205 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
207 temp = ohci_readl (ohci, &ohci->regs->control);
208 temp &= OHCI_CTRL_HCFS;
209 if (temp != OHCI_USB_RESUME) {
210 ohci_err (ohci, "controller won't resume\n");
211 spin_lock_irq(&ohci->lock);
215 /* disable old schedule state, reinit from scratch */
216 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
217 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
218 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
219 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
220 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
221 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
223 /* Sometimes PCI D3 suspend trashes frame timings ... */
224 periodic_reinit (ohci);
226 /* the following code is executed with ohci->lock held and
227 * irqs disabled if and only if autostopped is true
231 /* interrupts might have been disabled */
232 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
233 if (ohci->ed_rm_list)
234 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
236 /* Then re-enable operations */
237 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
238 (void) ohci_readl (ohci, &ohci->regs->control);
242 temp = ohci->hc_control;
243 temp &= OHCI_CTRL_RWC;
244 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
245 ohci->hc_control = temp;
246 ohci_writel (ohci, temp, &ohci->regs->control);
247 (void) ohci_readl (ohci, &ohci->regs->control);
252 spin_lock_irq (&ohci->lock);
254 /* now ohci->lock is always held and irqs are always disabled */
256 /* keep it alive for more than ~5x suspend + resume costs */
257 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
259 /* maybe turn schedules back on */
262 if (!ohci->ed_rm_list) {
263 if (ohci->ed_controltail) {
265 find_head (ohci->ed_controltail)->dma,
266 &ohci->regs->ed_controlhead);
267 enables |= OHCI_CTRL_CLE;
270 if (ohci->ed_bulktail) {
271 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
272 &ohci->regs->ed_bulkhead);
273 enables |= OHCI_CTRL_BLE;
277 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
278 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
280 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
281 ohci->hc_control |= enables;
282 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
284 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
285 (void) ohci_readl (ohci, &ohci->regs->control);
291 static int ohci_bus_suspend (struct usb_hcd *hcd)
293 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
296 spin_lock_irq (&ohci->lock);
298 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
301 rc = ohci_rh_suspend (ohci, 0);
302 spin_unlock_irq (&ohci->lock);
306 static int ohci_bus_resume (struct usb_hcd *hcd)
308 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
311 if (time_before (jiffies, ohci->next_statechange))
314 spin_lock_irq (&ohci->lock);
316 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
319 rc = ohci_rh_resume (ohci);
320 spin_unlock_irq (&ohci->lock);
322 /* poll until we know a device is connected or we autostop */
324 usb_hcd_poll_rh_status(hcd);
328 /* Carry out the final steps of resuming the controller device */
329 static void ohci_finish_controller_resume(struct usb_hcd *hcd)
331 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
333 bool need_reinit = false;
335 /* See if the controller is already running or has been reset */
336 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
337 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
340 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
347 /* If needed, reinitialize and suspend the root hub */
349 spin_lock_irq(&ohci->lock);
350 hcd->state = HC_STATE_RESUMING;
351 ohci_rh_resume(ohci);
352 hcd->state = HC_STATE_QUIESCING;
353 ohci_rh_suspend(ohci, 0);
354 hcd->state = HC_STATE_SUSPENDED;
355 spin_unlock_irq(&ohci->lock);
358 /* Normally just turn on port power and enable interrupts */
360 ohci_dbg(ohci, "powerup ports\n");
361 for (port = 0; port < ohci->num_ports; port++)
362 ohci_writel(ohci, RH_PS_PPS,
363 &ohci->regs->roothub.portstatus[port]);
365 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
366 ohci_readl(ohci, &ohci->regs->intrenable);
371 /* Carry out polling-, autostop-, and autoresume-related state changes */
372 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
377 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
380 /* keep on polling until we know a device is connected
381 * and RHSC is enabled */
382 if (!ohci->autostop) {
384 !device_may_wakeup(&ohci_to_hcd(ohci)
385 ->self.root_hub->dev)) {
386 if (ohci_readl(ohci, &ohci->regs->intrenable) &
391 ohci->next_statechange = jiffies + HZ;
394 /* if no devices have been attached for one second, autostop */
396 if (changed || any_connected) {
398 ohci->next_statechange = jiffies +
400 } else if (time_after_eq(jiffies,
401 ohci->next_statechange)
403 && !(ohci->hc_control &
404 OHCI_SCHED_ENABLES)) {
405 ohci_rh_suspend(ohci, 1);
410 /* if there is a port change, autostart or ask to be resumed */
411 case OHCI_USB_SUSPEND:
412 case OHCI_USB_RESUME:
415 ohci_rh_resume(ohci);
417 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
419 /* everything is idle, no need for polling */
427 #else /* CONFIG_PM */
429 static inline int ohci_rh_resume(struct ohci_hcd *ohci)
434 /* Carry out polling-related state changes.
435 * autostop isn't used when CONFIG_PM is turned off.
437 static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
442 /* keep on polling until RHSC is enabled */
443 if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
448 #endif /* CONFIG_PM */
450 /*-------------------------------------------------------------------------*/
452 /* build "status change" packet (one or two bytes) from HC registers */
455 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
457 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
458 int i, changed = 0, length = 1;
459 int any_connected = 0;
462 spin_lock_irqsave (&ohci->lock, flags);
463 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
466 /* undocumented erratum seen on at least rev D */
467 if ((ohci->flags & OHCI_QUIRK_AMD756)
468 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
469 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
470 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
471 /* retry later; "should not happen" */
476 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
477 buf [0] = changed = 1;
480 if (ohci->num_ports > 7) {
485 /* look at each port */
486 for (i = 0; i < ohci->num_ports; i++) {
487 u32 status = roothub_portstatus (ohci, i);
489 /* can't autostop if ports are connected */
490 any_connected |= (status & RH_PS_CCS);
492 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
493 | RH_PS_OCIC | RH_PS_PRSC)) {
496 buf [0] |= 1 << (i + 1);
498 buf [1] |= 1 << (i - 7);
502 hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed,
506 spin_unlock_irqrestore (&ohci->lock, flags);
508 return changed ? length : 0;
511 /*-------------------------------------------------------------------------*/
514 ohci_hub_descriptor (
515 struct ohci_hcd *ohci,
516 struct usb_hub_descriptor *desc
518 u32 rh = roothub_a (ohci);
521 desc->bDescriptorType = 0x29;
522 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
523 desc->bHubContrCurrent = 0;
525 desc->bNbrPorts = ohci->num_ports;
526 temp = 1 + (ohci->num_ports / 8);
527 desc->bDescLength = 7 + 2 * temp;
530 if (rh & RH_A_NPS) /* no power switching? */
532 if (rh & RH_A_PSM) /* per-port power switching? */
534 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
536 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
538 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
540 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
541 rh = roothub_b (ohci);
542 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
543 desc->bitmap [0] = rh & RH_B_DR;
544 if (ohci->num_ports > 7) {
545 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
546 desc->bitmap [2] = 0xff;
548 desc->bitmap [1] = 0xff;
551 /*-------------------------------------------------------------------------*/
553 #ifdef CONFIG_USB_OTG
555 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
557 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
564 /* start port reset before HNP protocol times out */
565 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
566 if (!(status & RH_PS_CCS))
569 /* khubd will finish the reset later */
570 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
574 static void start_hnp(struct ohci_hcd *ohci);
578 #define ohci_start_port_reset NULL
582 /*-------------------------------------------------------------------------*/
585 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
586 * not necessarily continuous ... to guard against resume signaling.
587 * The short timeout is safe for non-root hubs, and is backward-compatible
588 * with earlier Linux hosts.
590 #ifdef CONFIG_USB_SUSPEND
591 #define PORT_RESET_MSEC 50
593 #define PORT_RESET_MSEC 10
596 /* this timer value might be vendor-specific ... */
597 #define PORT_RESET_HW_MSEC 10
599 /* wrap-aware logic morphed from <linux/jiffies.h> */
600 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
602 /* called from some task, normally khubd */
603 static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
605 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
607 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
608 u16 reset_done = now + PORT_RESET_MSEC;
609 int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
611 /* build a "continuous enough" reset signal, with up to
612 * 3msec gap between pulses. scheduler HZ==100 must work;
613 * this might need to be deadline-scheduled.
618 /* spin until any current reset finishes */
619 limit_2 = PORT_RESET_HW_MSEC * 2;
620 while (--limit_2 >= 0) {
621 temp = ohci_readl (ohci, portstat);
622 /* handle e.g. CardBus eject */
625 if (!(temp & RH_PS_PRS))
630 /* timeout (a hardware error) has been observed when
631 * EHCI sets CF while this driver is resetting a port;
632 * presumably other disconnect paths might do it too.
636 "port[%d] reset timeout, stat %08x\n",
641 if (!(temp & RH_PS_CCS))
643 if (temp & RH_PS_PRSC)
644 ohci_writel (ohci, RH_PS_PRSC, portstat);
646 /* start the next reset, sleep till it's probably done */
647 ohci_writel (ohci, RH_PS_PRS, portstat);
648 msleep(PORT_RESET_HW_MSEC);
649 now = ohci_readl(ohci, &ohci->regs->fmnumber);
650 } while (tick_before(now, reset_done) && --limit_1 >= 0);
652 /* caller synchronizes using PRSC ... and handles PRS
653 * still being set when this returns.
659 static int ohci_hub_control (
667 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
668 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
672 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
676 case ClearHubFeature:
678 case C_HUB_OVER_CURRENT:
679 ohci_writel (ohci, RH_HS_OCIC,
680 &ohci->regs->roothub.status);
681 case C_HUB_LOCAL_POWER:
687 case ClearPortFeature:
688 if (!wIndex || wIndex > ports)
693 case USB_PORT_FEAT_ENABLE:
696 case USB_PORT_FEAT_C_ENABLE:
699 case USB_PORT_FEAT_SUSPEND:
702 case USB_PORT_FEAT_C_SUSPEND:
705 case USB_PORT_FEAT_POWER:
708 case USB_PORT_FEAT_C_CONNECTION:
711 case USB_PORT_FEAT_C_OVER_CURRENT:
714 case USB_PORT_FEAT_C_RESET:
720 ohci_writel (ohci, temp,
721 &ohci->regs->roothub.portstatus [wIndex]);
722 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
724 case GetHubDescriptor:
725 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
728 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
729 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
732 if (!wIndex || wIndex > ports)
735 temp = roothub_portstatus (ohci, wIndex);
736 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
738 #ifndef OHCI_VERBOSE_DEBUG
739 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
741 dbg_port (ohci, "GetStatus", wIndex, temp);
745 case C_HUB_OVER_CURRENT:
746 // FIXME: this can be cleared, yes?
747 case C_HUB_LOCAL_POWER:
754 if (!wIndex || wIndex > ports)
758 case USB_PORT_FEAT_SUSPEND:
759 #ifdef CONFIG_USB_OTG
760 if (hcd->self.otg_port == (wIndex + 1)
761 && hcd->self.b_hnp_enable)
765 ohci_writel (ohci, RH_PS_PSS,
766 &ohci->regs->roothub.portstatus [wIndex]);
768 case USB_PORT_FEAT_POWER:
769 ohci_writel (ohci, RH_PS_PPS,
770 &ohci->regs->roothub.portstatus [wIndex]);
772 case USB_PORT_FEAT_RESET:
773 retval = root_port_reset (ohci, wIndex);
782 /* "protocol stall" on error */