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);
54 static int ohci_restart(struct ohci_hcd *ohci);
57 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
58 __releases(ohci->lock)
59 __acquires(ohci->lock)
63 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
64 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
66 ohci_dbg (ohci, "resume/suspend?\n");
67 ohci->hc_control &= ~OHCI_CTRL_HCFS;
68 ohci->hc_control |= OHCI_USB_RESET;
69 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
70 (void) ohci_readl (ohci, &ohci->regs->control);
74 ohci_dbg (ohci, "needs reinit!\n");
76 case OHCI_USB_SUSPEND:
77 if (!ohci->autostop) {
78 ohci_dbg (ohci, "already suspended\n");
82 ohci_dbg (ohci, "%s root hub\n",
83 autostop ? "auto-stop" : "suspend");
85 /* First stop any processing */
86 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
87 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
88 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
89 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
90 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
92 /* sched disables take effect on the next frame,
93 * then the last WDH could take 6+ msec
95 ohci_dbg (ohci, "stopping schedules ...\n");
97 spin_unlock_irq (&ohci->lock);
99 spin_lock_irq (&ohci->lock);
102 finish_unlinks (ohci, ohci_frame_no(ohci));
104 /* maybe resume can wake root hub */
105 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) ||
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_info(ohci, "%swakeup\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");
187 if (status == -EBUSY) {
189 spin_unlock_irq (&ohci->lock);
190 (void) ohci_init (ohci);
191 status = ohci_restart (ohci);
192 spin_lock_irq (&ohci->lock);
197 if (status != -EINPROGRESS)
201 spin_unlock_irq (&ohci->lock);
203 temp = ohci->num_ports;
205 u32 stat = ohci_readl (ohci,
206 &ohci->regs->roothub.portstatus [temp]);
208 /* force global, not selective, resume */
209 if (!(stat & RH_PS_PSS))
211 ohci_writel (ohci, RH_PS_POCI,
212 &ohci->regs->roothub.portstatus [temp]);
215 /* Some controllers (lucent erratum) need extra-long delays */
216 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
218 temp = ohci_readl (ohci, &ohci->regs->control);
219 temp &= OHCI_CTRL_HCFS;
220 if (temp != OHCI_USB_RESUME) {
221 ohci_err (ohci, "controller won't resume\n");
225 /* disable old schedule state, reinit from scratch */
226 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
227 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
228 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
229 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
230 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
231 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
233 /* Sometimes PCI D3 suspend trashes frame timings ... */
234 periodic_reinit (ohci);
236 /* the following code is executed with ohci->lock held and
237 * irqs disabled if and only if autostopped is true
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);
303 static int ohci_bus_suspend (struct usb_hcd *hcd)
305 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
308 spin_lock_irq (&ohci->lock);
310 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
313 rc = ohci_rh_suspend (ohci, 0);
314 spin_unlock_irq (&ohci->lock);
318 static int ohci_bus_resume (struct usb_hcd *hcd)
320 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
323 if (time_before (jiffies, ohci->next_statechange))
326 spin_lock_irq (&ohci->lock);
328 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
331 rc = ohci_rh_resume (ohci);
332 spin_unlock_irq (&ohci->lock);
334 /* poll until we know a device is connected or we autostop */
336 usb_hcd_poll_rh_status(hcd);
340 #endif /* CONFIG_PM */
342 /*-------------------------------------------------------------------------*/
344 /* build "status change" packet (one or two bytes) from HC registers */
347 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
349 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
350 int i, changed = 0, length = 1;
351 int any_connected = 0, rhsc_enabled = 1;
354 spin_lock_irqsave (&ohci->lock, flags);
356 /* undocumented erratum seen on at least rev D */
357 if ((ohci->flags & OHCI_QUIRK_AMD756)
358 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
359 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
360 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
361 /* retry later; "should not happen" */
366 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
367 buf [0] = changed = 1;
370 if (ohci->num_ports > 7) {
375 /* look at each port */
376 for (i = 0; i < ohci->num_ports; i++) {
377 u32 status = roothub_portstatus (ohci, i);
379 /* can't autostop if ports are connected */
380 any_connected |= (status & RH_PS_CCS);
382 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
383 | RH_PS_OCIC | RH_PS_PRSC)) {
386 buf [0] |= 1 << (i + 1);
388 buf [1] |= 1 << (i - 7);
392 /* NOTE: vendors didn't always make the same implementation
393 * choices for RHSC. Sometimes it triggers on an edge (like
394 * setting and maybe clearing a port status change bit); and
395 * it's level-triggered on other silicon, active until khubd
396 * clears all active port status change bits. If it's still
397 * set (level-triggered) we must disable it and rely on
398 * polling until khubd re-enables it.
400 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
401 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
402 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
407 /* carry out appropriate state changes */
408 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
411 /* keep on polling until we know a device is connected
412 * and RHSC is enabled */
413 if (!ohci->autostop) {
419 ohci->next_statechange = jiffies + HZ;
422 /* if no devices have been attached for one second, autostop */
424 if (changed || any_connected) {
426 ohci->next_statechange = jiffies +
428 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
429 && time_after_eq(jiffies,
430 ohci->next_statechange)
432 && !(ohci->hc_control &
433 OHCI_SCHED_ENABLES)) {
434 ohci_rh_suspend (ohci, 1);
439 /* if there is a port change, autostart or ask to be resumed */
440 case OHCI_USB_SUSPEND:
441 case OHCI_USB_RESUME:
444 ohci_rh_resume (ohci);
446 usb_hcd_resume_root_hub (hcd);
448 /* everything is idle, no need for polling */
455 spin_unlock_irqrestore (&ohci->lock, flags);
457 return changed ? length : 0;
460 /*-------------------------------------------------------------------------*/
463 ohci_hub_descriptor (
464 struct ohci_hcd *ohci,
465 struct usb_hub_descriptor *desc
467 u32 rh = roothub_a (ohci);
470 desc->bDescriptorType = 0x29;
471 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
472 desc->bHubContrCurrent = 0;
474 desc->bNbrPorts = ohci->num_ports;
475 temp = 1 + (ohci->num_ports / 8);
476 desc->bDescLength = 7 + 2 * temp;
479 if (rh & RH_A_NPS) /* no power switching? */
481 if (rh & RH_A_PSM) /* per-port power switching? */
483 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
485 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
487 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
489 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
490 rh = roothub_b (ohci);
491 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
492 desc->bitmap [0] = rh & RH_B_DR;
493 if (ohci->num_ports > 7) {
494 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
495 desc->bitmap [2] = 0xff;
497 desc->bitmap [1] = 0xff;
500 /*-------------------------------------------------------------------------*/
502 #ifdef CONFIG_USB_OTG
504 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
506 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
513 /* start port reset before HNP protocol times out */
514 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
515 if (!(status & RH_PS_CCS))
518 /* khubd will finish the reset later */
519 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
523 static void start_hnp(struct ohci_hcd *ohci);
527 #define ohci_start_port_reset NULL
531 /*-------------------------------------------------------------------------*/
534 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
535 * not necessarily continuous ... to guard against resume signaling.
536 * The short timeout is safe for non-root hubs, and is backward-compatible
537 * with earlier Linux hosts.
539 #ifdef CONFIG_USB_SUSPEND
540 #define PORT_RESET_MSEC 50
542 #define PORT_RESET_MSEC 10
545 /* this timer value might be vendor-specific ... */
546 #define PORT_RESET_HW_MSEC 10
548 /* wrap-aware logic morphed from <linux/jiffies.h> */
549 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
551 /* called from some task, normally khubd */
552 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
554 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
556 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
557 u16 reset_done = now + PORT_RESET_MSEC;
559 /* build a "continuous enough" reset signal, with up to
560 * 3msec gap between pulses. scheduler HZ==100 must work;
561 * this might need to be deadline-scheduled.
564 /* spin until any current reset finishes */
566 temp = ohci_readl (ohci, portstat);
567 if (!(temp & RH_PS_PRS))
572 if (!(temp & RH_PS_CCS))
574 if (temp & RH_PS_PRSC)
575 ohci_writel (ohci, RH_PS_PRSC, portstat);
577 /* start the next reset, sleep till it's probably done */
578 ohci_writel (ohci, RH_PS_PRS, portstat);
579 msleep(PORT_RESET_HW_MSEC);
580 now = ohci_readl(ohci, &ohci->regs->fmnumber);
581 } while (tick_before(now, reset_done));
582 /* caller synchronizes using PRSC */
585 static int ohci_hub_control (
593 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
594 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
598 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
602 case ClearHubFeature:
604 case C_HUB_OVER_CURRENT:
605 ohci_writel (ohci, RH_HS_OCIC,
606 &ohci->regs->roothub.status);
607 case C_HUB_LOCAL_POWER:
613 case ClearPortFeature:
614 if (!wIndex || wIndex > ports)
619 case USB_PORT_FEAT_ENABLE:
622 case USB_PORT_FEAT_C_ENABLE:
625 case USB_PORT_FEAT_SUSPEND:
628 case USB_PORT_FEAT_C_SUSPEND:
631 case USB_PORT_FEAT_POWER:
634 case USB_PORT_FEAT_C_CONNECTION:
637 case USB_PORT_FEAT_C_OVER_CURRENT:
640 case USB_PORT_FEAT_C_RESET:
646 ohci_writel (ohci, temp,
647 &ohci->regs->roothub.portstatus [wIndex]);
648 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
650 case GetHubDescriptor:
651 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
654 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
655 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
658 if (!wIndex || wIndex > ports)
661 temp = roothub_portstatus (ohci, wIndex);
662 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
664 #ifndef OHCI_VERBOSE_DEBUG
665 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
667 dbg_port (ohci, "GetStatus", wIndex, temp);
671 case C_HUB_OVER_CURRENT:
672 // FIXME: this can be cleared, yes?
673 case C_HUB_LOCAL_POWER:
680 if (!wIndex || wIndex > ports)
684 case USB_PORT_FEAT_SUSPEND:
685 #ifdef CONFIG_USB_OTG
686 if (hcd->self.otg_port == (wIndex + 1)
687 && hcd->self.b_hnp_enable)
691 ohci_writel (ohci, RH_PS_PSS,
692 &ohci->regs->roothub.portstatus [wIndex]);
694 case USB_PORT_FEAT_POWER:
695 ohci_writel (ohci, RH_PS_PPS,
696 &ohci->regs->roothub.portstatus [wIndex]);
698 case USB_PORT_FEAT_RESET:
699 root_port_reset (ohci, wIndex);
708 /* "protocol stall" on error */