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 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
47 #define OHCI_SCHED_ENABLES \
48 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
50 static void dl_done_list (struct ohci_hcd *);
51 static void finish_unlinks (struct ohci_hcd *, u16);
53 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
54 __releases(ohci->lock)
55 __acquires(ohci->lock)
59 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
60 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
62 ohci_dbg (ohci, "resume/suspend?\n");
63 ohci->hc_control &= ~OHCI_CTRL_HCFS;
64 ohci->hc_control |= OHCI_USB_RESET;
65 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
66 (void) ohci_readl (ohci, &ohci->regs->control);
70 ohci_dbg (ohci, "needs reinit!\n");
72 case OHCI_USB_SUSPEND:
73 if (!ohci->autostop) {
74 ohci_dbg (ohci, "already suspended\n");
78 ohci_dbg (ohci, "%s root hub\n",
79 autostop ? "auto-stop" : "suspend");
81 /* First stop any processing */
82 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
83 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
84 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
85 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
86 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
88 /* sched disables take effect on the next frame,
89 * then the last WDH could take 6+ msec
91 ohci_dbg (ohci, "stopping schedules ...\n");
93 spin_unlock_irq (&ohci->lock);
95 spin_lock_irq (&ohci->lock);
98 finish_unlinks (ohci, ohci_frame_no(ohci));
100 /* maybe resume can wake root hub */
101 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) ||
103 ohci->hc_control |= OHCI_CTRL_RWE;
105 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
106 ohci->hc_control &= ~OHCI_CTRL_RWE;
109 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
110 * which doesn't imply ports will first be individually suspended.
112 ohci->hc_control &= ~OHCI_CTRL_HCFS;
113 ohci->hc_control |= OHCI_USB_SUSPEND;
114 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
115 (void) ohci_readl (ohci, &ohci->regs->control);
117 /* no resumes until devices finish suspending */
119 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
127 static inline struct ed *find_head (struct ed *ed)
129 /* for bulk and control lists */
135 static int ohci_restart (struct ohci_hcd *ohci);
137 /* caller has locked the root hub */
138 static int ohci_rh_resume (struct ohci_hcd *ohci)
139 __releases(ohci->lock)
140 __acquires(ohci->lock)
142 struct usb_hcd *hcd = ohci_to_hcd (ohci);
144 int status = -EINPROGRESS;
145 int autostopped = ohci->autostop;
148 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
150 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
151 /* this can happen after resuming a swsusp snapshot */
152 if (hcd->state == HC_STATE_RESUMING) {
153 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
156 /* this happens when pmcore resumes HC then root */
158 ohci_dbg (ohci, "duplicate resume\n");
161 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
162 case OHCI_USB_SUSPEND:
163 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
164 ohci->hc_control |= OHCI_USB_RESUME;
165 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
166 (void) ohci_readl (ohci, &ohci->regs->control);
167 ohci_dbg (ohci, "%s root hub\n",
168 autostopped ? "auto-start" : "resume");
170 case OHCI_USB_RESUME:
171 /* HCFS changes sometime after INTR_RD */
172 ohci_info (ohci, "wakeup\n");
175 /* this can happen after resuming a swsusp snapshot */
176 ohci_dbg (ohci, "snapshot resume? reinit\n");
179 default: /* RESET, we lost power */
180 ohci_dbg (ohci, "lost power\n");
184 if (status == -EBUSY) {
186 spin_unlock_irq (&ohci->lock);
187 (void) ohci_init (ohci);
188 status = ohci_restart (ohci);
189 spin_lock_irq (&ohci->lock);
194 if (status != -EINPROGRESS)
198 spin_unlock_irq (&ohci->lock);
200 temp = ohci->num_ports;
202 u32 stat = ohci_readl (ohci,
203 &ohci->regs->roothub.portstatus [temp]);
205 /* force global, not selective, resume */
206 if (!(stat & RH_PS_PSS))
208 ohci_writel (ohci, RH_PS_POCI,
209 &ohci->regs->roothub.portstatus [temp]);
212 /* Some controllers (lucent erratum) need extra-long delays */
213 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
215 temp = ohci_readl (ohci, &ohci->regs->control);
216 temp &= OHCI_CTRL_HCFS;
217 if (temp != OHCI_USB_RESUME) {
218 ohci_err (ohci, "controller won't resume\n");
222 /* disable old schedule state, reinit from scratch */
223 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
224 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
225 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
226 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
227 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
228 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
230 /* Sometimes PCI D3 suspend trashes frame timings ... */
231 periodic_reinit (ohci);
233 /* the following code is executed with ohci->lock held and
234 * irqs disabled if and only if autostopped is true
238 /* interrupts might have been disabled */
239 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
240 if (ohci->ed_rm_list)
241 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
243 /* Then re-enable operations */
244 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
245 (void) ohci_readl (ohci, &ohci->regs->control);
249 temp = ohci->hc_control;
250 temp &= OHCI_CTRL_RWC;
251 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
252 ohci->hc_control = temp;
253 ohci_writel (ohci, temp, &ohci->regs->control);
254 (void) ohci_readl (ohci, &ohci->regs->control);
259 spin_lock_irq (&ohci->lock);
261 /* now ohci->lock is always held and irqs are always disabled */
263 /* keep it alive for more than ~5x suspend + resume costs */
264 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
266 /* maybe turn schedules back on */
269 if (!ohci->ed_rm_list) {
270 if (ohci->ed_controltail) {
272 find_head (ohci->ed_controltail)->dma,
273 &ohci->regs->ed_controlhead);
274 enables |= OHCI_CTRL_CLE;
277 if (ohci->ed_bulktail) {
278 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
279 &ohci->regs->ed_bulkhead);
280 enables |= OHCI_CTRL_BLE;
284 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
285 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
287 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
288 ohci->hc_control |= enables;
289 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
291 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
292 (void) ohci_readl (ohci, &ohci->regs->control);
300 static int ohci_bus_suspend (struct usb_hcd *hcd)
302 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
305 spin_lock_irq (&ohci->lock);
307 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
310 rc = ohci_rh_suspend (ohci, 0);
311 spin_unlock_irq (&ohci->lock);
315 static int ohci_bus_resume (struct usb_hcd *hcd)
317 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
320 if (time_before (jiffies, ohci->next_statechange))
323 spin_lock_irq (&ohci->lock);
325 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
328 rc = ohci_rh_resume (ohci);
329 spin_unlock_irq (&ohci->lock);
331 /* poll until we know a device is connected or we autostop */
333 usb_hcd_poll_rh_status(hcd);
337 #endif /* CONFIG_PM */
339 /*-------------------------------------------------------------------------*/
341 /* build "status change" packet (one or two bytes) from HC registers */
344 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
346 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
347 int i, changed = 0, length = 1;
348 int any_connected = 0, rhsc_enabled = 1;
351 spin_lock_irqsave (&ohci->lock, flags);
353 /* undocumented erratum seen on at least rev D */
354 if ((ohci->flags & OHCI_QUIRK_AMD756)
355 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
356 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
357 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
358 /* retry later; "should not happen" */
363 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
364 buf [0] = changed = 1;
367 if (ohci->num_ports > 7) {
372 /* look at each port */
373 for (i = 0; i < ohci->num_ports; i++) {
374 u32 status = roothub_portstatus (ohci, i);
376 /* can't autostop if ports are connected */
377 any_connected |= (status & RH_PS_CCS);
379 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
380 | RH_PS_OCIC | RH_PS_PRSC)) {
383 buf [0] |= 1 << (i + 1);
385 buf [1] |= 1 << (i - 7);
389 /* NOTE: vendors didn't always make the same implementation
390 * choices for RHSC. Sometimes it triggers on an edge (like
391 * setting and maybe clearing a port status change bit); and
392 * it's level-triggered on other silicon, active until khubd
393 * clears all active port status change bits. If it's still
394 * set (level-triggered) we must disable it and rely on
395 * polling until khubd re-enables it.
397 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
398 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
399 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
404 /* carry out appropriate state changes */
405 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
408 /* keep on polling until we know a device is connected
409 * and RHSC is enabled */
410 if (!ohci->autostop) {
416 ohci->next_statechange = jiffies + HZ;
419 /* if no devices have been attached for one second, autostop */
421 if (changed || any_connected) {
423 ohci->next_statechange = jiffies +
425 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
426 && time_after_eq(jiffies,
427 ohci->next_statechange)
429 && !(ohci->hc_control &
430 OHCI_SCHED_ENABLES)) {
431 ohci_rh_suspend (ohci, 1);
436 /* if there is a port change, autostart or ask to be resumed */
437 case OHCI_USB_SUSPEND:
438 case OHCI_USB_RESUME:
441 ohci_rh_resume (ohci);
443 usb_hcd_resume_root_hub (hcd);
445 /* everything is idle, no need for polling */
452 spin_unlock_irqrestore (&ohci->lock, flags);
454 return changed ? length : 0;
457 /*-------------------------------------------------------------------------*/
460 ohci_hub_descriptor (
461 struct ohci_hcd *ohci,
462 struct usb_hub_descriptor *desc
464 u32 rh = roothub_a (ohci);
467 desc->bDescriptorType = 0x29;
468 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
469 desc->bHubContrCurrent = 0;
471 desc->bNbrPorts = ohci->num_ports;
472 temp = 1 + (ohci->num_ports / 8);
473 desc->bDescLength = 7 + 2 * temp;
476 if (rh & RH_A_NPS) /* no power switching? */
478 if (rh & RH_A_PSM) /* per-port power switching? */
480 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
482 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
484 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
486 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
487 rh = roothub_b (ohci);
488 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
489 desc->bitmap [0] = rh & RH_B_DR;
490 if (ohci->num_ports > 7) {
491 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
492 desc->bitmap [2] = 0xff;
494 desc->bitmap [1] = 0xff;
497 /*-------------------------------------------------------------------------*/
499 #ifdef CONFIG_USB_OTG
501 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
503 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
510 /* start port reset before HNP protocol times out */
511 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
512 if (!(status & RH_PS_CCS))
515 /* khubd will finish the reset later */
516 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
520 static void start_hnp(struct ohci_hcd *ohci);
524 #define ohci_start_port_reset NULL
528 /*-------------------------------------------------------------------------*/
531 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
532 * not necessarily continuous ... to guard against resume signaling.
533 * The short timeout is safe for non-root hubs, and is backward-compatible
534 * with earlier Linux hosts.
536 #ifdef CONFIG_USB_SUSPEND
537 #define PORT_RESET_MSEC 50
539 #define PORT_RESET_MSEC 10
542 /* this timer value might be vendor-specific ... */
543 #define PORT_RESET_HW_MSEC 10
545 /* wrap-aware logic morphed from <linux/jiffies.h> */
546 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
548 /* called from some task, normally khubd */
549 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
551 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
553 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
554 u16 reset_done = now + PORT_RESET_MSEC;
556 /* build a "continuous enough" reset signal, with up to
557 * 3msec gap between pulses. scheduler HZ==100 must work;
558 * this might need to be deadline-scheduled.
561 /* spin until any current reset finishes */
563 temp = ohci_readl (ohci, portstat);
564 if (!(temp & RH_PS_PRS))
569 if (!(temp & RH_PS_CCS))
571 if (temp & RH_PS_PRSC)
572 ohci_writel (ohci, RH_PS_PRSC, portstat);
574 /* start the next reset, sleep till it's probably done */
575 ohci_writel (ohci, RH_PS_PRS, portstat);
576 msleep(PORT_RESET_HW_MSEC);
577 now = ohci_readl(ohci, &ohci->regs->fmnumber);
578 } while (tick_before(now, reset_done));
579 /* caller synchronizes using PRSC */
582 static int ohci_hub_control (
590 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
591 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
595 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
599 case ClearHubFeature:
601 case C_HUB_OVER_CURRENT:
602 ohci_writel (ohci, RH_HS_OCIC,
603 &ohci->regs->roothub.status);
604 case C_HUB_LOCAL_POWER:
610 case ClearPortFeature:
611 if (!wIndex || wIndex > ports)
616 case USB_PORT_FEAT_ENABLE:
619 case USB_PORT_FEAT_C_ENABLE:
622 case USB_PORT_FEAT_SUSPEND:
625 case USB_PORT_FEAT_C_SUSPEND:
628 case USB_PORT_FEAT_POWER:
631 case USB_PORT_FEAT_C_CONNECTION:
634 case USB_PORT_FEAT_C_OVER_CURRENT:
637 case USB_PORT_FEAT_C_RESET:
643 ohci_writel (ohci, temp,
644 &ohci->regs->roothub.portstatus [wIndex]);
645 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
647 case GetHubDescriptor:
648 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
651 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
652 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
655 if (!wIndex || wIndex > ports)
658 temp = roothub_portstatus (ohci, wIndex);
659 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
661 #ifndef OHCI_VERBOSE_DEBUG
662 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
664 dbg_port (ohci, "GetStatus", wIndex, temp);
668 case C_HUB_OVER_CURRENT:
669 // FIXME: this can be cleared, yes?
670 case C_HUB_LOCAL_POWER:
677 if (!wIndex || wIndex > ports)
681 case USB_PORT_FEAT_SUSPEND:
682 #ifdef CONFIG_USB_OTG
683 if (hcd->self.otg_port == (wIndex + 1)
684 && hcd->self.b_hnp_enable)
688 ohci_writel (ohci, RH_PS_PSS,
689 &ohci->regs->roothub.portstatus [wIndex]);
691 case USB_PORT_FEAT_POWER:
692 ohci_writel (ohci, RH_PS_PPS,
693 &ohci->regs->roothub.portstatus [wIndex]);
695 case USB_PORT_FEAT_RESET:
696 root_port_reset (ohci, wIndex);
705 /* "protocol stall" on error */