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 /* Some controllers (lucent erratum) need extra-long delays */
208 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
210 temp = ohci_readl (ohci, &ohci->regs->control);
211 temp &= OHCI_CTRL_HCFS;
212 if (temp != OHCI_USB_RESUME) {
213 ohci_err (ohci, "controller won't resume\n");
214 spin_lock_irq(&ohci->lock);
218 /* disable old schedule state, reinit from scratch */
219 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
220 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
221 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
222 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
223 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
224 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
226 /* Sometimes PCI D3 suspend trashes frame timings ... */
227 periodic_reinit (ohci);
229 /* the following code is executed with ohci->lock held and
230 * irqs disabled if and only if autostopped is true
234 /* interrupts might have been disabled */
235 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
236 if (ohci->ed_rm_list)
237 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
239 /* Then re-enable operations */
240 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
241 (void) ohci_readl (ohci, &ohci->regs->control);
245 temp = ohci->hc_control;
246 temp &= OHCI_CTRL_RWC;
247 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
248 ohci->hc_control = temp;
249 ohci_writel (ohci, temp, &ohci->regs->control);
250 (void) ohci_readl (ohci, &ohci->regs->control);
255 spin_lock_irq (&ohci->lock);
257 /* now ohci->lock is always held and irqs are always disabled */
259 /* keep it alive for more than ~5x suspend + resume costs */
260 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
262 /* maybe turn schedules back on */
265 if (!ohci->ed_rm_list) {
266 if (ohci->ed_controltail) {
268 find_head (ohci->ed_controltail)->dma,
269 &ohci->regs->ed_controlhead);
270 enables |= OHCI_CTRL_CLE;
273 if (ohci->ed_bulktail) {
274 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
275 &ohci->regs->ed_bulkhead);
276 enables |= OHCI_CTRL_BLE;
280 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
281 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
283 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
284 ohci->hc_control |= enables;
285 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
287 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
288 (void) ohci_readl (ohci, &ohci->regs->control);
296 static int ohci_bus_suspend (struct usb_hcd *hcd)
298 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
301 spin_lock_irq (&ohci->lock);
303 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
306 rc = ohci_rh_suspend (ohci, 0);
307 spin_unlock_irq (&ohci->lock);
311 static int ohci_bus_resume (struct usb_hcd *hcd)
313 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
316 if (time_before (jiffies, ohci->next_statechange))
319 spin_lock_irq (&ohci->lock);
321 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
324 rc = ohci_rh_resume (ohci);
325 spin_unlock_irq (&ohci->lock);
327 /* poll until we know a device is connected or we autostop */
329 usb_hcd_poll_rh_status(hcd);
333 #endif /* CONFIG_PM */
335 /*-------------------------------------------------------------------------*/
337 /* build "status change" packet (one or two bytes) from HC registers */
340 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
342 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
343 int i, changed = 0, length = 1;
344 int any_connected = 0;
347 spin_lock_irqsave (&ohci->lock, flags);
349 /* undocumented erratum seen on at least rev D */
350 if ((ohci->flags & OHCI_QUIRK_AMD756)
351 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
352 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
353 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
354 /* retry later; "should not happen" */
359 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
360 buf [0] = changed = 1;
363 if (ohci->num_ports > 7) {
368 /* look at each port */
369 for (i = 0; i < ohci->num_ports; i++) {
370 u32 status = roothub_portstatus (ohci, i);
372 /* can't autostop if ports are connected */
373 any_connected |= (status & RH_PS_CCS);
375 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
376 | RH_PS_OCIC | RH_PS_PRSC)) {
379 buf [0] |= 1 << (i + 1);
381 buf [1] |= 1 << (i - 7);
387 /* carry out appropriate state changes */
388 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
391 /* keep on polling until we know a device is connected
392 * and RHSC is enabled */
393 if (!ohci->autostop) {
395 if (ohci_readl(ohci, &ohci->regs->intrenable) &
400 ohci->next_statechange = jiffies + HZ;
403 /* if no devices have been attached for one second, autostop */
405 if (changed || any_connected) {
407 ohci->next_statechange = jiffies +
409 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
410 && time_after_eq(jiffies,
411 ohci->next_statechange)
413 && !(ohci->hc_control &
414 OHCI_SCHED_ENABLES)) {
415 ohci_rh_suspend (ohci, 1);
420 /* if there is a port change, autostart or ask to be resumed */
421 case OHCI_USB_SUSPEND:
422 case OHCI_USB_RESUME:
425 ohci_rh_resume (ohci);
427 usb_hcd_resume_root_hub (hcd);
429 /* everything is idle, no need for polling */
436 spin_unlock_irqrestore (&ohci->lock, flags);
438 return changed ? length : 0;
441 /*-------------------------------------------------------------------------*/
444 ohci_hub_descriptor (
445 struct ohci_hcd *ohci,
446 struct usb_hub_descriptor *desc
448 u32 rh = roothub_a (ohci);
451 desc->bDescriptorType = 0x29;
452 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
453 desc->bHubContrCurrent = 0;
455 desc->bNbrPorts = ohci->num_ports;
456 temp = 1 + (ohci->num_ports / 8);
457 desc->bDescLength = 7 + 2 * temp;
460 if (rh & RH_A_NPS) /* no power switching? */
462 if (rh & RH_A_PSM) /* per-port power switching? */
464 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
466 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
468 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
470 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
471 rh = roothub_b (ohci);
472 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
473 desc->bitmap [0] = rh & RH_B_DR;
474 if (ohci->num_ports > 7) {
475 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
476 desc->bitmap [2] = 0xff;
478 desc->bitmap [1] = 0xff;
481 /*-------------------------------------------------------------------------*/
483 #ifdef CONFIG_USB_OTG
485 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
487 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
494 /* start port reset before HNP protocol times out */
495 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
496 if (!(status & RH_PS_CCS))
499 /* khubd will finish the reset later */
500 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
504 static void start_hnp(struct ohci_hcd *ohci);
508 #define ohci_start_port_reset NULL
512 /*-------------------------------------------------------------------------*/
515 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
516 * not necessarily continuous ... to guard against resume signaling.
517 * The short timeout is safe for non-root hubs, and is backward-compatible
518 * with earlier Linux hosts.
520 #ifdef CONFIG_USB_SUSPEND
521 #define PORT_RESET_MSEC 50
523 #define PORT_RESET_MSEC 10
526 /* this timer value might be vendor-specific ... */
527 #define PORT_RESET_HW_MSEC 10
529 /* wrap-aware logic morphed from <linux/jiffies.h> */
530 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
532 /* called from some task, normally khubd */
533 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
535 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
537 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
538 u16 reset_done = now + PORT_RESET_MSEC;
540 /* build a "continuous enough" reset signal, with up to
541 * 3msec gap between pulses. scheduler HZ==100 must work;
542 * this might need to be deadline-scheduled.
545 /* spin until any current reset finishes */
547 temp = ohci_readl (ohci, portstat);
548 if (!(temp & RH_PS_PRS))
553 if (!(temp & RH_PS_CCS))
555 if (temp & RH_PS_PRSC)
556 ohci_writel (ohci, RH_PS_PRSC, portstat);
558 /* start the next reset, sleep till it's probably done */
559 ohci_writel (ohci, RH_PS_PRS, portstat);
560 msleep(PORT_RESET_HW_MSEC);
561 now = ohci_readl(ohci, &ohci->regs->fmnumber);
562 } while (tick_before(now, reset_done));
563 /* caller synchronizes using PRSC */
566 static int ohci_hub_control (
574 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
575 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
579 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
583 case ClearHubFeature:
585 case C_HUB_OVER_CURRENT:
586 ohci_writel (ohci, RH_HS_OCIC,
587 &ohci->regs->roothub.status);
588 case C_HUB_LOCAL_POWER:
594 case ClearPortFeature:
595 if (!wIndex || wIndex > ports)
600 case USB_PORT_FEAT_ENABLE:
603 case USB_PORT_FEAT_C_ENABLE:
606 case USB_PORT_FEAT_SUSPEND:
609 case USB_PORT_FEAT_C_SUSPEND:
612 case USB_PORT_FEAT_POWER:
615 case USB_PORT_FEAT_C_CONNECTION:
618 case USB_PORT_FEAT_C_OVER_CURRENT:
621 case USB_PORT_FEAT_C_RESET:
627 ohci_writel (ohci, temp,
628 &ohci->regs->roothub.portstatus [wIndex]);
629 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
631 case GetHubDescriptor:
632 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
635 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
636 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
639 if (!wIndex || wIndex > ports)
642 temp = roothub_portstatus (ohci, wIndex);
643 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
645 #ifndef OHCI_VERBOSE_DEBUG
646 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
648 dbg_port (ohci, "GetStatus", wIndex, temp);
652 case C_HUB_OVER_CURRENT:
653 // FIXME: this can be cleared, yes?
654 case C_HUB_LOCAL_POWER:
661 if (!wIndex || wIndex > ports)
665 case USB_PORT_FEAT_SUSPEND:
666 #ifdef CONFIG_USB_OTG
667 if (hcd->self.otg_port == (wIndex + 1)
668 && hcd->self.b_hnp_enable)
672 ohci_writel (ohci, RH_PS_PSS,
673 &ohci->regs->roothub.portstatus [wIndex]);
675 case USB_PORT_FEAT_POWER:
676 ohci_writel (ohci, RH_PS_PPS,
677 &ohci->regs->roothub.portstatus [wIndex]);
679 case USB_PORT_FEAT_RESET:
680 root_port_reset (ohci, wIndex);
689 /* "protocol stall" on error */