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, "%swakeup\n",
173 autostopped ? "auto-" : "");
176 /* this can happen after resuming a swsusp snapshot */
177 ohci_dbg (ohci, "snapshot resume? reinit\n");
180 default: /* RESET, we lost power */
181 ohci_dbg (ohci, "lost power\n");
185 if (status == -EBUSY) {
187 spin_unlock_irq (&ohci->lock);
188 (void) ohci_init (ohci);
189 status = ohci_restart (ohci);
190 spin_lock_irq (&ohci->lock);
195 if (status != -EINPROGRESS)
199 spin_unlock_irq (&ohci->lock);
201 temp = ohci->num_ports;
203 u32 stat = ohci_readl (ohci,
204 &ohci->regs->roothub.portstatus [temp]);
206 /* force global, not selective, resume */
207 if (!(stat & RH_PS_PSS))
209 ohci_writel (ohci, RH_PS_POCI,
210 &ohci->regs->roothub.portstatus [temp]);
213 /* Some controllers (lucent erratum) need extra-long delays */
214 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
216 temp = ohci_readl (ohci, &ohci->regs->control);
217 temp &= OHCI_CTRL_HCFS;
218 if (temp != OHCI_USB_RESUME) {
219 ohci_err (ohci, "controller won't resume\n");
223 /* disable old schedule state, reinit from scratch */
224 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
225 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
226 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
227 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
228 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
229 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
231 /* Sometimes PCI D3 suspend trashes frame timings ... */
232 periodic_reinit (ohci);
234 /* the following code is executed with ohci->lock held and
235 * irqs disabled if and only if autostopped is true
239 /* interrupts might have been disabled */
240 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
241 if (ohci->ed_rm_list)
242 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
244 /* Then re-enable operations */
245 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
246 (void) ohci_readl (ohci, &ohci->regs->control);
250 temp = ohci->hc_control;
251 temp &= OHCI_CTRL_RWC;
252 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
253 ohci->hc_control = temp;
254 ohci_writel (ohci, temp, &ohci->regs->control);
255 (void) ohci_readl (ohci, &ohci->regs->control);
260 spin_lock_irq (&ohci->lock);
262 /* now ohci->lock is always held and irqs are always disabled */
264 /* keep it alive for more than ~5x suspend + resume costs */
265 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
267 /* maybe turn schedules back on */
270 if (!ohci->ed_rm_list) {
271 if (ohci->ed_controltail) {
273 find_head (ohci->ed_controltail)->dma,
274 &ohci->regs->ed_controlhead);
275 enables |= OHCI_CTRL_CLE;
278 if (ohci->ed_bulktail) {
279 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
280 &ohci->regs->ed_bulkhead);
281 enables |= OHCI_CTRL_BLE;
285 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
286 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
288 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
289 ohci->hc_control |= enables;
290 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
292 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
293 (void) ohci_readl (ohci, &ohci->regs->control);
301 static int ohci_bus_suspend (struct usb_hcd *hcd)
303 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
306 spin_lock_irq (&ohci->lock);
308 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
311 rc = ohci_rh_suspend (ohci, 0);
312 spin_unlock_irq (&ohci->lock);
316 static int ohci_bus_resume (struct usb_hcd *hcd)
318 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
321 if (time_before (jiffies, ohci->next_statechange))
324 spin_lock_irq (&ohci->lock);
326 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
329 rc = ohci_rh_resume (ohci);
330 spin_unlock_irq (&ohci->lock);
332 /* poll until we know a device is connected or we autostop */
334 usb_hcd_poll_rh_status(hcd);
338 #endif /* CONFIG_PM */
340 /*-------------------------------------------------------------------------*/
342 /* build "status change" packet (one or two bytes) from HC registers */
345 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
347 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
348 int i, changed = 0, length = 1;
349 int any_connected = 0, rhsc_enabled = 1;
352 spin_lock_irqsave (&ohci->lock, flags);
354 /* undocumented erratum seen on at least rev D */
355 if ((ohci->flags & OHCI_QUIRK_AMD756)
356 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
357 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
358 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
359 /* retry later; "should not happen" */
364 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
365 buf [0] = changed = 1;
368 if (ohci->num_ports > 7) {
373 /* look at each port */
374 for (i = 0; i < ohci->num_ports; i++) {
375 u32 status = roothub_portstatus (ohci, i);
377 /* can't autostop if ports are connected */
378 any_connected |= (status & RH_PS_CCS);
380 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
381 | RH_PS_OCIC | RH_PS_PRSC)) {
384 buf [0] |= 1 << (i + 1);
386 buf [1] |= 1 << (i - 7);
390 /* NOTE: vendors didn't always make the same implementation
391 * choices for RHSC. Sometimes it triggers on an edge (like
392 * setting and maybe clearing a port status change bit); and
393 * it's level-triggered on other silicon, active until khubd
394 * clears all active port status change bits. If it's still
395 * set (level-triggered) we must disable it and rely on
396 * polling until khubd re-enables it.
398 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
399 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
400 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
405 /* carry out appropriate state changes */
406 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
409 /* keep on polling until we know a device is connected
410 * and RHSC is enabled */
411 if (!ohci->autostop) {
417 ohci->next_statechange = jiffies + HZ;
420 /* if no devices have been attached for one second, autostop */
422 if (changed || any_connected) {
424 ohci->next_statechange = jiffies +
426 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
427 && time_after_eq(jiffies,
428 ohci->next_statechange)
430 && !(ohci->hc_control &
431 OHCI_SCHED_ENABLES)) {
432 ohci_rh_suspend (ohci, 1);
437 /* if there is a port change, autostart or ask to be resumed */
438 case OHCI_USB_SUSPEND:
439 case OHCI_USB_RESUME:
442 ohci_rh_resume (ohci);
444 usb_hcd_resume_root_hub (hcd);
446 /* everything is idle, no need for polling */
453 spin_unlock_irqrestore (&ohci->lock, flags);
455 return changed ? length : 0;
458 /*-------------------------------------------------------------------------*/
461 ohci_hub_descriptor (
462 struct ohci_hcd *ohci,
463 struct usb_hub_descriptor *desc
465 u32 rh = roothub_a (ohci);
468 desc->bDescriptorType = 0x29;
469 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
470 desc->bHubContrCurrent = 0;
472 desc->bNbrPorts = ohci->num_ports;
473 temp = 1 + (ohci->num_ports / 8);
474 desc->bDescLength = 7 + 2 * temp;
477 if (rh & RH_A_NPS) /* no power switching? */
479 if (rh & RH_A_PSM) /* per-port power switching? */
481 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
483 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
485 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
487 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
488 rh = roothub_b (ohci);
489 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
490 desc->bitmap [0] = rh & RH_B_DR;
491 if (ohci->num_ports > 7) {
492 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
493 desc->bitmap [2] = 0xff;
495 desc->bitmap [1] = 0xff;
498 /*-------------------------------------------------------------------------*/
500 #ifdef CONFIG_USB_OTG
502 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
504 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
511 /* start port reset before HNP protocol times out */
512 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
513 if (!(status & RH_PS_CCS))
516 /* khubd will finish the reset later */
517 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
521 static void start_hnp(struct ohci_hcd *ohci);
525 #define ohci_start_port_reset NULL
529 /*-------------------------------------------------------------------------*/
532 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
533 * not necessarily continuous ... to guard against resume signaling.
534 * The short timeout is safe for non-root hubs, and is backward-compatible
535 * with earlier Linux hosts.
537 #ifdef CONFIG_USB_SUSPEND
538 #define PORT_RESET_MSEC 50
540 #define PORT_RESET_MSEC 10
543 /* this timer value might be vendor-specific ... */
544 #define PORT_RESET_HW_MSEC 10
546 /* wrap-aware logic morphed from <linux/jiffies.h> */
547 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
549 /* called from some task, normally khubd */
550 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
552 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
554 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
555 u16 reset_done = now + PORT_RESET_MSEC;
557 /* build a "continuous enough" reset signal, with up to
558 * 3msec gap between pulses. scheduler HZ==100 must work;
559 * this might need to be deadline-scheduled.
562 /* spin until any current reset finishes */
564 temp = ohci_readl (ohci, portstat);
565 if (!(temp & RH_PS_PRS))
570 if (!(temp & RH_PS_CCS))
572 if (temp & RH_PS_PRSC)
573 ohci_writel (ohci, RH_PS_PRSC, portstat);
575 /* start the next reset, sleep till it's probably done */
576 ohci_writel (ohci, RH_PS_PRS, portstat);
577 msleep(PORT_RESET_HW_MSEC);
578 now = ohci_readl(ohci, &ohci->regs->fmnumber);
579 } while (tick_before(now, reset_done));
580 /* caller synchronizes using PRSC */
583 static int ohci_hub_control (
591 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
592 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
596 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
600 case ClearHubFeature:
602 case C_HUB_OVER_CURRENT:
603 ohci_writel (ohci, RH_HS_OCIC,
604 &ohci->regs->roothub.status);
605 case C_HUB_LOCAL_POWER:
611 case ClearPortFeature:
612 if (!wIndex || wIndex > ports)
617 case USB_PORT_FEAT_ENABLE:
620 case USB_PORT_FEAT_C_ENABLE:
623 case USB_PORT_FEAT_SUSPEND:
626 case USB_PORT_FEAT_C_SUSPEND:
629 case USB_PORT_FEAT_POWER:
632 case USB_PORT_FEAT_C_CONNECTION:
635 case USB_PORT_FEAT_C_OVER_CURRENT:
638 case USB_PORT_FEAT_C_RESET:
644 ohci_writel (ohci, temp,
645 &ohci->regs->roothub.portstatus [wIndex]);
646 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
648 case GetHubDescriptor:
649 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
652 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
653 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
656 if (!wIndex || wIndex > ports)
659 temp = roothub_portstatus (ohci, wIndex);
660 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
662 #ifndef OHCI_VERBOSE_DEBUG
663 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
665 dbg_port (ohci, "GetStatus", wIndex, temp);
669 case C_HUB_OVER_CURRENT:
670 // FIXME: this can be cleared, yes?
671 case C_HUB_LOCAL_POWER:
678 if (!wIndex || wIndex > ports)
682 case USB_PORT_FEAT_SUSPEND:
683 #ifdef CONFIG_USB_OTG
684 if (hcd->self.otg_port == (wIndex + 1)
685 && hcd->self.b_hnp_enable)
689 ohci_writel (ohci, RH_PS_PSS,
690 &ohci->regs->roothub.portstatus [wIndex]);
692 case USB_PORT_FEAT_POWER:
693 ohci_writel (ohci, RH_PS_PPS,
694 &ohci->regs->roothub.portstatus [wIndex]);
696 case USB_PORT_FEAT_RESET:
697 root_port_reset (ohci, wIndex);
706 /* "protocol stall" on error */