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_restart(struct ohci_hcd *ohci);
61 static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
62 __releases(ohci->lock)
63 __acquires(ohci->lock)
67 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
68 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
70 ohci_dbg (ohci, "resume/suspend?\n");
71 ohci->hc_control &= ~OHCI_CTRL_HCFS;
72 ohci->hc_control |= OHCI_USB_RESET;
73 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
74 (void) ohci_readl (ohci, &ohci->regs->control);
78 ohci_dbg (ohci, "needs reinit!\n");
80 case OHCI_USB_SUSPEND:
81 if (!ohci->autostop) {
82 ohci_dbg (ohci, "already suspended\n");
86 ohci_dbg (ohci, "%s root hub\n",
87 autostop ? "auto-stop" : "suspend");
89 /* First stop any processing */
90 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
91 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
92 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
93 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
94 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
96 /* sched disables take effect on the next frame,
97 * then the last WDH could take 6+ msec
99 ohci_dbg (ohci, "stopping schedules ...\n");
101 spin_unlock_irq (&ohci->lock);
103 spin_lock_irq (&ohci->lock);
106 finish_unlinks (ohci, ohci_frame_no(ohci));
108 /* maybe resume can wake root hub */
109 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) ||
111 ohci->hc_control |= OHCI_CTRL_RWE;
113 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
114 ohci->hc_control &= ~OHCI_CTRL_RWE;
117 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
118 * which doesn't imply ports will first be individually suspended.
120 ohci->hc_control &= ~OHCI_CTRL_HCFS;
121 ohci->hc_control |= OHCI_USB_SUSPEND;
122 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
123 (void) ohci_readl (ohci, &ohci->regs->control);
125 /* no resumes until devices finish suspending */
127 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
135 static inline struct ed *find_head (struct ed *ed)
137 /* for bulk and control lists */
143 /* caller has locked the root hub */
144 static int ohci_rh_resume (struct ohci_hcd *ohci)
145 __releases(ohci->lock)
146 __acquires(ohci->lock)
148 struct usb_hcd *hcd = ohci_to_hcd (ohci);
150 int status = -EINPROGRESS;
151 int autostopped = ohci->autostop;
154 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
156 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
157 /* this can happen after resuming a swsusp snapshot */
158 if (hcd->state == HC_STATE_RESUMING) {
159 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
162 /* this happens when pmcore resumes HC then root */
164 ohci_dbg (ohci, "duplicate resume\n");
167 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
168 case OHCI_USB_SUSPEND:
169 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
170 ohci->hc_control |= OHCI_USB_RESUME;
171 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
172 (void) ohci_readl (ohci, &ohci->regs->control);
173 ohci_dbg (ohci, "%s root hub\n",
174 autostopped ? "auto-start" : "resume");
176 case OHCI_USB_RESUME:
177 /* HCFS changes sometime after INTR_RD */
178 ohci_info(ohci, "%swakeup\n",
179 autostopped ? "auto-" : "");
182 /* this can happen after resuming a swsusp snapshot */
183 ohci_dbg (ohci, "snapshot resume? reinit\n");
186 default: /* RESET, we lost power */
187 ohci_dbg (ohci, "lost power\n");
191 if (status == -EBUSY) {
193 spin_unlock_irq (&ohci->lock);
194 (void) ohci_init (ohci);
195 status = ohci_restart (ohci);
196 spin_lock_irq (&ohci->lock);
201 if (status != -EINPROGRESS)
205 spin_unlock_irq (&ohci->lock);
207 temp = ohci->num_ports;
209 u32 stat = ohci_readl (ohci,
210 &ohci->regs->roothub.portstatus [temp]);
212 /* force global, not selective, resume */
213 if (!(stat & RH_PS_PSS))
215 ohci_writel (ohci, RH_PS_POCI,
216 &ohci->regs->roothub.portstatus [temp]);
219 /* Some controllers (lucent erratum) need extra-long delays */
220 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
222 temp = ohci_readl (ohci, &ohci->regs->control);
223 temp &= OHCI_CTRL_HCFS;
224 if (temp != OHCI_USB_RESUME) {
225 ohci_err (ohci, "controller won't resume\n");
229 /* disable old schedule state, reinit from scratch */
230 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
231 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
232 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
233 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
234 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
235 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
237 /* Sometimes PCI D3 suspend trashes frame timings ... */
238 periodic_reinit (ohci);
240 /* the following code is executed with ohci->lock held and
241 * irqs disabled if and only if autostopped is true
245 /* interrupts might have been disabled */
246 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
247 if (ohci->ed_rm_list)
248 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
250 /* Then re-enable operations */
251 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
252 (void) ohci_readl (ohci, &ohci->regs->control);
256 temp = ohci->hc_control;
257 temp &= OHCI_CTRL_RWC;
258 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
259 ohci->hc_control = temp;
260 ohci_writel (ohci, temp, &ohci->regs->control);
261 (void) ohci_readl (ohci, &ohci->regs->control);
266 spin_lock_irq (&ohci->lock);
268 /* now ohci->lock is always held and irqs are always disabled */
270 /* keep it alive for more than ~5x suspend + resume costs */
271 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
273 /* maybe turn schedules back on */
276 if (!ohci->ed_rm_list) {
277 if (ohci->ed_controltail) {
279 find_head (ohci->ed_controltail)->dma,
280 &ohci->regs->ed_controlhead);
281 enables |= OHCI_CTRL_CLE;
284 if (ohci->ed_bulktail) {
285 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
286 &ohci->regs->ed_bulkhead);
287 enables |= OHCI_CTRL_BLE;
291 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
292 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
294 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
295 ohci->hc_control |= enables;
296 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
298 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
299 (void) ohci_readl (ohci, &ohci->regs->control);
307 static int ohci_bus_suspend (struct usb_hcd *hcd)
309 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
312 spin_lock_irq (&ohci->lock);
314 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
317 rc = ohci_rh_suspend (ohci, 0);
318 spin_unlock_irq (&ohci->lock);
322 static int ohci_bus_resume (struct usb_hcd *hcd)
324 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
327 if (time_before (jiffies, ohci->next_statechange))
330 spin_lock_irq (&ohci->lock);
332 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
335 rc = ohci_rh_resume (ohci);
336 spin_unlock_irq (&ohci->lock);
338 /* poll until we know a device is connected or we autostop */
340 usb_hcd_poll_rh_status(hcd);
344 #endif /* CONFIG_PM */
346 /*-------------------------------------------------------------------------*/
348 /* build "status change" packet (one or two bytes) from HC registers */
351 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
353 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
354 int i, changed = 0, length = 1;
355 int any_connected = 0;
358 spin_lock_irqsave (&ohci->lock, flags);
360 /* undocumented erratum seen on at least rev D */
361 if ((ohci->flags & OHCI_QUIRK_AMD756)
362 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
363 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
364 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
365 /* retry later; "should not happen" */
370 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
371 buf [0] = changed = 1;
374 if (ohci->num_ports > 7) {
379 /* look at each port */
380 for (i = 0; i < ohci->num_ports; i++) {
381 u32 status = roothub_portstatus (ohci, i);
383 /* can't autostop if ports are connected */
384 any_connected |= (status & RH_PS_CCS);
386 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
387 | RH_PS_OCIC | RH_PS_PRSC)) {
390 buf [0] |= 1 << (i + 1);
392 buf [1] |= 1 << (i - 7);
398 /* carry out appropriate state changes */
399 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
402 /* keep on polling until we know a device is connected
403 * and RHSC is enabled */
404 if (!ohci->autostop) {
406 if (ohci_readl(ohci, &ohci->regs->intrenable) &
411 ohci->next_statechange = jiffies + HZ;
414 /* if no devices have been attached for one second, autostop */
416 if (changed || any_connected) {
418 ohci->next_statechange = jiffies +
420 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
421 && time_after_eq(jiffies,
422 ohci->next_statechange)
424 && !(ohci->hc_control &
425 OHCI_SCHED_ENABLES)) {
426 ohci_rh_suspend (ohci, 1);
431 /* if there is a port change, autostart or ask to be resumed */
432 case OHCI_USB_SUSPEND:
433 case OHCI_USB_RESUME:
436 ohci_rh_resume (ohci);
438 usb_hcd_resume_root_hub (hcd);
440 /* everything is idle, no need for polling */
447 spin_unlock_irqrestore (&ohci->lock, flags);
449 return changed ? length : 0;
452 /*-------------------------------------------------------------------------*/
455 ohci_hub_descriptor (
456 struct ohci_hcd *ohci,
457 struct usb_hub_descriptor *desc
459 u32 rh = roothub_a (ohci);
462 desc->bDescriptorType = 0x29;
463 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
464 desc->bHubContrCurrent = 0;
466 desc->bNbrPorts = ohci->num_ports;
467 temp = 1 + (ohci->num_ports / 8);
468 desc->bDescLength = 7 + 2 * temp;
471 if (rh & RH_A_NPS) /* no power switching? */
473 if (rh & RH_A_PSM) /* per-port power switching? */
475 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
477 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
479 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
481 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
482 rh = roothub_b (ohci);
483 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
484 desc->bitmap [0] = rh & RH_B_DR;
485 if (ohci->num_ports > 7) {
486 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
487 desc->bitmap [2] = 0xff;
489 desc->bitmap [1] = 0xff;
492 /*-------------------------------------------------------------------------*/
494 #ifdef CONFIG_USB_OTG
496 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
498 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
505 /* start port reset before HNP protocol times out */
506 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
507 if (!(status & RH_PS_CCS))
510 /* khubd will finish the reset later */
511 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
515 static void start_hnp(struct ohci_hcd *ohci);
519 #define ohci_start_port_reset NULL
523 /*-------------------------------------------------------------------------*/
526 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
527 * not necessarily continuous ... to guard against resume signaling.
528 * The short timeout is safe for non-root hubs, and is backward-compatible
529 * with earlier Linux hosts.
531 #ifdef CONFIG_USB_SUSPEND
532 #define PORT_RESET_MSEC 50
534 #define PORT_RESET_MSEC 10
537 /* this timer value might be vendor-specific ... */
538 #define PORT_RESET_HW_MSEC 10
540 /* wrap-aware logic morphed from <linux/jiffies.h> */
541 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
543 /* called from some task, normally khubd */
544 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
546 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
548 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
549 u16 reset_done = now + PORT_RESET_MSEC;
551 /* build a "continuous enough" reset signal, with up to
552 * 3msec gap between pulses. scheduler HZ==100 must work;
553 * this might need to be deadline-scheduled.
556 /* spin until any current reset finishes */
558 temp = ohci_readl (ohci, portstat);
559 if (!(temp & RH_PS_PRS))
564 if (!(temp & RH_PS_CCS))
566 if (temp & RH_PS_PRSC)
567 ohci_writel (ohci, RH_PS_PRSC, portstat);
569 /* start the next reset, sleep till it's probably done */
570 ohci_writel (ohci, RH_PS_PRS, portstat);
571 msleep(PORT_RESET_HW_MSEC);
572 now = ohci_readl(ohci, &ohci->regs->fmnumber);
573 } while (tick_before(now, reset_done));
574 /* caller synchronizes using PRSC */
577 static int ohci_hub_control (
585 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
586 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
590 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
594 case ClearHubFeature:
596 case C_HUB_OVER_CURRENT:
597 ohci_writel (ohci, RH_HS_OCIC,
598 &ohci->regs->roothub.status);
599 case C_HUB_LOCAL_POWER:
605 case ClearPortFeature:
606 if (!wIndex || wIndex > ports)
611 case USB_PORT_FEAT_ENABLE:
614 case USB_PORT_FEAT_C_ENABLE:
617 case USB_PORT_FEAT_SUSPEND:
620 case USB_PORT_FEAT_C_SUSPEND:
623 case USB_PORT_FEAT_POWER:
626 case USB_PORT_FEAT_C_CONNECTION:
629 case USB_PORT_FEAT_C_OVER_CURRENT:
632 case USB_PORT_FEAT_C_RESET:
638 ohci_writel (ohci, temp,
639 &ohci->regs->roothub.portstatus [wIndex]);
640 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
642 case GetHubDescriptor:
643 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
646 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
647 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
650 if (!wIndex || wIndex > ports)
653 temp = roothub_portstatus (ohci, wIndex);
654 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
656 #ifndef OHCI_VERBOSE_DEBUG
657 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
659 dbg_port (ohci, "GetStatus", wIndex, temp);
663 case C_HUB_OVER_CURRENT:
664 // FIXME: this can be cleared, yes?
665 case C_HUB_LOCAL_POWER:
672 if (!wIndex || wIndex > ports)
676 case USB_PORT_FEAT_SUSPEND:
677 #ifdef CONFIG_USB_OTG
678 if (hcd->self.otg_port == (wIndex + 1)
679 && hcd->self.b_hnp_enable)
683 ohci_writel (ohci, RH_PS_PSS,
684 &ohci->regs->roothub.portstatus [wIndex]);
686 case USB_PORT_FEAT_POWER:
687 ohci_writel (ohci, RH_PS_PPS,
688 &ohci->regs->roothub.portstatus [wIndex]);
690 case USB_PORT_FEAT_RESET:
691 root_port_reset (ohci, wIndex);
700 /* "protocol stall" on error */