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);
45 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
50 #define OHCI_SCHED_ENABLES \
51 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
53 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
54 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
55 static int ohci_restart (struct ohci_hcd *ohci);
57 static int ohci_bus_suspend (struct usb_hcd *hcd)
59 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
63 spin_lock_irqsave (&ohci->lock, flags);
65 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
66 spin_unlock_irqrestore (&ohci->lock, flags);
70 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
71 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
73 ohci_dbg (ohci, "resume/suspend?\n");
74 ohci->hc_control &= ~OHCI_CTRL_HCFS;
75 ohci->hc_control |= OHCI_USB_RESET;
76 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
77 (void) ohci_readl (ohci, &ohci->regs->control);
81 ohci_dbg (ohci, "needs reinit!\n");
83 case OHCI_USB_SUSPEND:
84 ohci_dbg (ohci, "already suspended\n");
87 ohci_dbg (ohci, "suspend root hub\n");
89 /* First stop any processing */
90 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
93 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
94 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
95 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
96 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
98 /* sched disables take effect on the next frame,
99 * then the last WDH could take 6+ msec
101 ohci_dbg (ohci, "stopping schedules ...\n");
106 if (ohci_readl (ohci, &ohci->regs->intrstatus)
110 dl_done_list (ohci, NULL);
113 dl_done_list (ohci, NULL);
114 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
115 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
116 &ohci->regs->intrstatus);
118 /* maybe resume can wake root hub */
119 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
120 ohci->hc_control |= OHCI_CTRL_RWE;
122 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
123 ohci->hc_control &= ~OHCI_CTRL_RWE;
126 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
127 * which doesn't imply ports will first be individually suspended.
129 ohci->hc_control &= ~OHCI_CTRL_HCFS;
130 ohci->hc_control |= OHCI_USB_SUSPEND;
131 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
132 (void) ohci_readl (ohci, &ohci->regs->control);
134 /* no resumes until devices finish suspending */
135 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
137 /* no timer polling */
141 spin_unlock_irqrestore (&ohci->lock, flags);
145 static inline struct ed *find_head (struct ed *ed)
147 /* for bulk and control lists */
153 /* caller has locked the root hub */
154 static int ohci_bus_resume (struct usb_hcd *hcd)
156 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
158 int status = -EINPROGRESS;
161 if (time_before (jiffies, ohci->next_statechange))
164 spin_lock_irqsave (&ohci->lock, flags);
166 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
167 spin_unlock_irqrestore (&ohci->lock, flags);
172 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
174 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
175 /* this can happen after resuming a swsusp snapshot */
176 if (hcd->state == HC_STATE_RESUMING) {
177 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
180 /* this happens when pmcore resumes HC then root */
182 ohci_dbg (ohci, "duplicate resume\n");
185 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
186 case OHCI_USB_SUSPEND:
187 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
188 ohci->hc_control |= OHCI_USB_RESUME;
189 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
190 (void) ohci_readl (ohci, &ohci->regs->control);
191 ohci_dbg (ohci, "resume root hub\n");
193 case OHCI_USB_RESUME:
194 /* HCFS changes sometime after INTR_RD */
195 ohci_info (ohci, "wakeup\n");
198 /* this can happen after resuming a swsusp snapshot */
199 ohci_dbg (ohci, "snapshot resume? reinit\n");
202 default: /* RESET, we lost power */
203 ohci_dbg (ohci, "lost power\n");
206 spin_unlock_irqrestore (&ohci->lock, flags);
207 if (status == -EBUSY) {
208 (void) ohci_init (ohci);
209 return ohci_restart (ohci);
211 if (status != -EINPROGRESS)
214 temp = ohci->num_ports;
217 u32 stat = ohci_readl (ohci,
218 &ohci->regs->roothub.portstatus [temp]);
220 /* force global, not selective, resume */
221 if (!(stat & RH_PS_PSS))
223 ohci_writel (ohci, RH_PS_POCI,
224 &ohci->regs->roothub.portstatus [temp]);
227 /* Some controllers (lucent erratum) need extra-long delays */
228 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
230 temp = ohci_readl (ohci, &ohci->regs->control);
231 temp &= OHCI_CTRL_HCFS;
232 if (temp != OHCI_USB_RESUME) {
233 ohci_err (ohci, "controller won't resume\n");
237 /* disable old schedule state, reinit from scratch */
238 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
239 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
240 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
241 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
242 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
243 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
245 /* Sometimes PCI D3 suspend trashes frame timings ... */
246 periodic_reinit (ohci);
248 /* interrupts might have been disabled */
249 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
250 if (ohci->ed_rm_list)
251 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
252 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
253 &ohci->regs->intrstatus);
255 /* Then re-enable operations */
256 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
257 (void) ohci_readl (ohci, &ohci->regs->control);
260 temp = ohci->hc_control;
261 temp &= OHCI_CTRL_RWC;
262 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
263 ohci->hc_control = temp;
264 ohci_writel (ohci, temp, &ohci->regs->control);
265 (void) ohci_readl (ohci, &ohci->regs->control);
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);
305 #endif /* CONFIG_PM */
307 /*-------------------------------------------------------------------------*/
309 /* build "status change" packet (one or two bytes) from HC registers */
312 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
314 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
315 int i, changed = 0, length = 1;
318 spin_lock_irqsave (&ohci->lock, flags);
320 /* handle autosuspended root: finish resuming before
321 * letting khubd or root hub timer see state changes.
323 if (unlikely((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
324 || !HC_IS_RUNNING(hcd->state)))
327 /* undocumented erratum seen on at least rev D */
328 if ((ohci->flags & OHCI_QUIRK_AMD756)
329 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
330 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
331 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
332 /* retry later; "should not happen" */
337 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
338 buf [0] = changed = 1;
341 if (ohci->num_ports > 7) {
346 /* look at each port */
347 for (i = 0; i < ohci->num_ports; i++) {
348 u32 status = roothub_portstatus (ohci, i);
350 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
351 | RH_PS_OCIC | RH_PS_PRSC)) {
354 buf [0] |= 1 << (i + 1);
356 buf [1] |= 1 << (i - 7);
361 /* after root hub changes, stop polling after debouncing
362 * for a while and maybe kicking in autosuspend
365 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
368 spin_unlock_irqrestore (&ohci->lock, flags);
370 return changed ? length : 0;
373 /*-------------------------------------------------------------------------*/
376 ohci_hub_descriptor (
377 struct ohci_hcd *ohci,
378 struct usb_hub_descriptor *desc
380 u32 rh = roothub_a (ohci);
383 desc->bDescriptorType = 0x29;
384 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
385 desc->bHubContrCurrent = 0;
387 desc->bNbrPorts = ohci->num_ports;
388 temp = 1 + (ohci->num_ports / 8);
389 desc->bDescLength = 7 + 2 * temp;
392 if (rh & RH_A_NPS) /* no power switching? */
394 if (rh & RH_A_PSM) /* per-port power switching? */
396 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
398 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
400 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
402 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
403 rh = roothub_b (ohci);
404 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
405 desc->bitmap [0] = rh & RH_B_DR;
406 if (ohci->num_ports > 7) {
407 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
408 desc->bitmap [2] = 0xff;
410 desc->bitmap [1] = 0xff;
413 /*-------------------------------------------------------------------------*/
415 #ifdef CONFIG_USB_OTG
417 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
419 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
426 /* start port reset before HNP protocol times out */
427 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
428 if (!(status & RH_PS_CCS))
431 /* khubd will finish the reset later */
432 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
436 static void start_hnp(struct ohci_hcd *ohci);
440 #define ohci_start_port_reset NULL
444 /*-------------------------------------------------------------------------*/
447 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
448 * not necessarily continuous ... to guard against resume signaling.
449 * The short timeout is safe for non-root hubs, and is backward-compatible
450 * with earlier Linux hosts.
452 #ifdef CONFIG_USB_SUSPEND
453 #define PORT_RESET_MSEC 50
455 #define PORT_RESET_MSEC 10
458 /* this timer value might be vendor-specific ... */
459 #define PORT_RESET_HW_MSEC 10
461 /* wrap-aware logic morphed from <linux/jiffies.h> */
462 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
464 /* called from some task, normally khubd */
465 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
467 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
469 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
470 u16 reset_done = now + PORT_RESET_MSEC;
472 /* build a "continuous enough" reset signal, with up to
473 * 3msec gap between pulses. scheduler HZ==100 must work;
474 * this might need to be deadline-scheduled.
477 /* spin until any current reset finishes */
479 temp = ohci_readl (ohci, portstat);
480 if (!(temp & RH_PS_PRS))
485 if (!(temp & RH_PS_CCS))
487 if (temp & RH_PS_PRSC)
488 ohci_writel (ohci, RH_PS_PRSC, portstat);
490 /* start the next reset, sleep till it's probably done */
491 ohci_writel (ohci, RH_PS_PRS, portstat);
492 msleep(PORT_RESET_HW_MSEC);
493 now = ohci_readl(ohci, &ohci->regs->fmnumber);
494 } while (tick_before(now, reset_done));
495 /* caller synchronizes using PRSC */
498 static int ohci_hub_control (
506 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
507 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
511 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
515 case ClearHubFeature:
517 case C_HUB_OVER_CURRENT:
518 ohci_writel (ohci, RH_HS_OCIC,
519 &ohci->regs->roothub.status);
520 case C_HUB_LOCAL_POWER:
526 case ClearPortFeature:
527 if (!wIndex || wIndex > ports)
532 case USB_PORT_FEAT_ENABLE:
535 case USB_PORT_FEAT_C_ENABLE:
538 case USB_PORT_FEAT_SUSPEND:
541 case USB_PORT_FEAT_C_SUSPEND:
544 case USB_PORT_FEAT_POWER:
547 case USB_PORT_FEAT_C_CONNECTION:
550 case USB_PORT_FEAT_C_OVER_CURRENT:
553 case USB_PORT_FEAT_C_RESET:
559 ohci_writel (ohci, temp,
560 &ohci->regs->roothub.portstatus [wIndex]);
561 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
563 case GetHubDescriptor:
564 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
567 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
568 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
571 if (!wIndex || wIndex > ports)
574 temp = roothub_portstatus (ohci, wIndex);
575 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
577 #ifndef OHCI_VERBOSE_DEBUG
578 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
580 dbg_port (ohci, "GetStatus", wIndex, temp);
584 case C_HUB_OVER_CURRENT:
585 // FIXME: this can be cleared, yes?
586 case C_HUB_LOCAL_POWER:
593 if (!wIndex || wIndex > ports)
597 case USB_PORT_FEAT_SUSPEND:
598 #ifdef CONFIG_USB_OTG
599 if (hcd->self.otg_port == (wIndex + 1)
600 && hcd->self.b_hnp_enable)
604 ohci_writel (ohci, RH_PS_PSS,
605 &ohci->regs->roothub.portstatus [wIndex]);
607 case USB_PORT_FEAT_POWER:
608 ohci_writel (ohci, RH_PS_PPS,
609 &ohci->regs->roothub.portstatus [wIndex]);
611 case USB_PORT_FEAT_RESET:
612 root_port_reset (ohci, wIndex);
621 /* "protocol stall" on error */