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 (time_after_eq (jiffies,
426 ohci->next_statechange)
428 && !(ohci->hc_control &
429 OHCI_SCHED_ENABLES)) {
430 ohci_rh_suspend (ohci, 1);
435 /* if there is a port change, autostart or ask to be resumed */
436 case OHCI_USB_SUSPEND:
437 case OHCI_USB_RESUME:
440 ohci_rh_resume (ohci);
442 usb_hcd_resume_root_hub (hcd);
444 /* everything is idle, no need for polling */
451 spin_unlock_irqrestore (&ohci->lock, flags);
453 return changed ? length : 0;
456 /*-------------------------------------------------------------------------*/
459 ohci_hub_descriptor (
460 struct ohci_hcd *ohci,
461 struct usb_hub_descriptor *desc
463 u32 rh = roothub_a (ohci);
466 desc->bDescriptorType = 0x29;
467 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
468 desc->bHubContrCurrent = 0;
470 desc->bNbrPorts = ohci->num_ports;
471 temp = 1 + (ohci->num_ports / 8);
472 desc->bDescLength = 7 + 2 * temp;
475 if (rh & RH_A_NPS) /* no power switching? */
477 if (rh & RH_A_PSM) /* per-port power switching? */
479 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
481 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
483 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
485 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
486 rh = roothub_b (ohci);
487 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
488 desc->bitmap [0] = rh & RH_B_DR;
489 if (ohci->num_ports > 7) {
490 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
491 desc->bitmap [2] = 0xff;
493 desc->bitmap [1] = 0xff;
496 /*-------------------------------------------------------------------------*/
498 #ifdef CONFIG_USB_OTG
500 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
502 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
509 /* start port reset before HNP protocol times out */
510 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
511 if (!(status & RH_PS_CCS))
514 /* khubd will finish the reset later */
515 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
519 static void start_hnp(struct ohci_hcd *ohci);
523 #define ohci_start_port_reset NULL
527 /*-------------------------------------------------------------------------*/
530 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
531 * not necessarily continuous ... to guard against resume signaling.
532 * The short timeout is safe for non-root hubs, and is backward-compatible
533 * with earlier Linux hosts.
535 #ifdef CONFIG_USB_SUSPEND
536 #define PORT_RESET_MSEC 50
538 #define PORT_RESET_MSEC 10
541 /* this timer value might be vendor-specific ... */
542 #define PORT_RESET_HW_MSEC 10
544 /* wrap-aware logic morphed from <linux/jiffies.h> */
545 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
547 /* called from some task, normally khubd */
548 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
550 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
552 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
553 u16 reset_done = now + PORT_RESET_MSEC;
555 /* build a "continuous enough" reset signal, with up to
556 * 3msec gap between pulses. scheduler HZ==100 must work;
557 * this might need to be deadline-scheduled.
560 /* spin until any current reset finishes */
562 temp = ohci_readl (ohci, portstat);
563 if (!(temp & RH_PS_PRS))
568 if (!(temp & RH_PS_CCS))
570 if (temp & RH_PS_PRSC)
571 ohci_writel (ohci, RH_PS_PRSC, portstat);
573 /* start the next reset, sleep till it's probably done */
574 ohci_writel (ohci, RH_PS_PRS, portstat);
575 msleep(PORT_RESET_HW_MSEC);
576 now = ohci_readl(ohci, &ohci->regs->fmnumber);
577 } while (tick_before(now, reset_done));
578 /* caller synchronizes using PRSC */
581 static int ohci_hub_control (
589 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
590 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
594 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
598 case ClearHubFeature:
600 case C_HUB_OVER_CURRENT:
601 ohci_writel (ohci, RH_HS_OCIC,
602 &ohci->regs->roothub.status);
603 case C_HUB_LOCAL_POWER:
609 case ClearPortFeature:
610 if (!wIndex || wIndex > ports)
615 case USB_PORT_FEAT_ENABLE:
618 case USB_PORT_FEAT_C_ENABLE:
621 case USB_PORT_FEAT_SUSPEND:
624 case USB_PORT_FEAT_C_SUSPEND:
627 case USB_PORT_FEAT_POWER:
630 case USB_PORT_FEAT_C_CONNECTION:
633 case USB_PORT_FEAT_C_OVER_CURRENT:
636 case USB_PORT_FEAT_C_RESET:
642 ohci_writel (ohci, temp,
643 &ohci->regs->roothub.portstatus [wIndex]);
644 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
646 case GetHubDescriptor:
647 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
650 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
651 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
654 if (!wIndex || wIndex > ports)
657 temp = roothub_portstatus (ohci, wIndex);
658 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
660 #ifndef OHCI_VERBOSE_DEBUG
661 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
663 dbg_port (ohci, "GetStatus", wIndex, temp);
667 case C_HUB_OVER_CURRENT:
668 // FIXME: this can be cleared, yes?
669 case C_HUB_LOCAL_POWER:
676 if (!wIndex || wIndex > ports)
680 case USB_PORT_FEAT_SUSPEND:
681 #ifdef CONFIG_USB_OTG
682 if (hcd->self.otg_port == (wIndex + 1)
683 && hcd->self.b_hnp_enable)
687 ohci_writel (ohci, RH_PS_PSS,
688 &ohci->regs->roothub.portstatus [wIndex]);
690 case USB_PORT_FEAT_POWER:
691 ohci_writel (ohci, RH_PS_PPS,
692 &ohci->regs->roothub.portstatus [wIndex]);
694 case USB_PORT_FEAT_RESET:
695 root_port_reset (ohci, wIndex);
704 /* "protocol stall" on error */