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);
49 #define OHCI_SCHED_ENABLES \
50 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
52 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
53 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
54 static int ohci_restart (struct ohci_hcd *ohci);
56 static int ohci_bus_suspend (struct usb_hcd *hcd)
58 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
62 spin_lock_irqsave (&ohci->lock, flags);
64 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
65 spin_unlock_irqrestore (&ohci->lock, flags);
69 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
70 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
72 ohci_dbg (ohci, "resume/suspend?\n");
73 ohci->hc_control &= ~OHCI_CTRL_HCFS;
74 ohci->hc_control |= OHCI_USB_RESET;
75 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
76 (void) ohci_readl (ohci, &ohci->regs->control);
80 ohci_dbg (ohci, "needs reinit!\n");
82 case OHCI_USB_SUSPEND:
83 ohci_dbg (ohci, "already suspended\n");
86 ohci_dbg (ohci, "suspend root hub\n");
88 /* First stop any processing */
89 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
92 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
93 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
94 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
95 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
97 /* sched disables take effect on the next frame,
98 * then the last WDH could take 6+ msec
100 ohci_dbg (ohci, "stopping schedules ...\n");
105 if (ohci_readl (ohci, &ohci->regs->intrstatus)
109 dl_done_list (ohci, NULL);
112 dl_done_list (ohci, NULL);
113 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
114 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
115 &ohci->regs->intrstatus);
117 /* maybe resume can wake root hub */
118 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
119 ohci->hc_control |= OHCI_CTRL_RWE;
121 ohci->hc_control &= ~OHCI_CTRL_RWE;
123 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
124 * which doesn't imply ports will first be individually suspended.
126 ohci->hc_control &= ~OHCI_CTRL_HCFS;
127 ohci->hc_control |= OHCI_USB_SUSPEND;
128 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
129 (void) ohci_readl (ohci, &ohci->regs->control);
131 /* no resumes until devices finish suspending */
132 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
134 /* no timer polling */
138 /* external suspend vs self autosuspend ... same effect */
140 usb_hcd_suspend_root_hub(hcd);
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;
319 can_suspend = device_may_wakeup(&hcd->self.root_hub->dev);
320 spin_lock_irqsave (&ohci->lock, flags);
322 /* handle autosuspended root: finish resuming before
323 * letting khubd or root hub timer see state changes.
325 if (unlikely((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
326 || !HC_IS_RUNNING(hcd->state))) {
331 /* undocumented erratum seen on at least rev D */
332 if ((ohci->flags & OHCI_QUIRK_AMD756)
333 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
334 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
335 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
336 /* retry later; "should not happen" */
341 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
342 buf [0] = changed = 1;
345 if (ohci->num_ports > 7) {
350 /* look at each port */
351 for (i = 0; i < ohci->num_ports; i++) {
352 u32 status = roothub_portstatus (ohci, i);
354 /* can't autosuspend with active ports */
355 if ((status & RH_PS_PES) && !(status & RH_PS_PSS))
358 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
359 | RH_PS_OCIC | RH_PS_PRSC)) {
362 buf [0] |= 1 << (i + 1);
364 buf [1] |= 1 << (i - 7);
369 /* after root hub changes, stop polling after debouncing
370 * for a while and maybe kicking in autosuspend
373 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
375 } else if (time_before (jiffies, ohci->next_statechange)) {
379 can_suspend = can_suspend
381 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
385 if (hcd->uses_new_polling) {
387 /* use INTR_RHSC iff INTR_RD won't apply */
389 ohci_writel (ohci, OHCI_INTR_RHSC,
390 &ohci->regs->intrenable);
395 spin_unlock_irqrestore (&ohci->lock, flags);
398 /* save power by autosuspending idle root hubs;
399 * INTR_RD wakes us when there's work
401 if (can_suspend && usb_trylock_device (hcd->self.root_hub) == 0) {
402 ohci_vdbg (ohci, "autosuspend\n");
403 (void) ohci_bus_suspend (hcd);
404 usb_unlock_device (hcd->self.root_hub);
408 return changed ? length : 0;
411 /*-------------------------------------------------------------------------*/
414 ohci_hub_descriptor (
415 struct ohci_hcd *ohci,
416 struct usb_hub_descriptor *desc
418 u32 rh = roothub_a (ohci);
421 desc->bDescriptorType = 0x29;
422 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
423 desc->bHubContrCurrent = 0;
425 desc->bNbrPorts = ohci->num_ports;
426 temp = 1 + (ohci->num_ports / 8);
427 desc->bDescLength = 7 + 2 * temp;
430 if (rh & RH_A_NPS) /* no power switching? */
432 if (rh & RH_A_PSM) /* per-port power switching? */
434 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
436 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
438 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
440 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
441 rh = roothub_b (ohci);
442 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
443 desc->bitmap [0] = rh & RH_B_DR;
444 if (ohci->num_ports > 7) {
445 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
446 desc->bitmap [2] = 0xff;
448 desc->bitmap [1] = 0xff;
451 /*-------------------------------------------------------------------------*/
453 #ifdef CONFIG_USB_OTG
455 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
457 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
464 /* start port reset before HNP protocol times out */
465 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
466 if (!(status & RH_PS_CCS))
469 /* khubd will finish the reset later */
470 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
474 static void start_hnp(struct ohci_hcd *ohci);
478 #define ohci_start_port_reset NULL
482 /*-------------------------------------------------------------------------*/
485 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
486 * not necessarily continuous ... to guard against resume signaling.
487 * The short timeout is safe for non-root hubs, and is backward-compatible
488 * with earlier Linux hosts.
490 #ifdef CONFIG_USB_SUSPEND
491 #define PORT_RESET_MSEC 50
493 #define PORT_RESET_MSEC 10
496 /* this timer value might be vendor-specific ... */
497 #define PORT_RESET_HW_MSEC 10
499 /* wrap-aware logic morphed from <linux/jiffies.h> */
500 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
502 /* called from some task, normally khubd */
503 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
505 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
507 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
508 u16 reset_done = now + PORT_RESET_MSEC;
510 /* build a "continuous enough" reset signal, with up to
511 * 3msec gap between pulses. scheduler HZ==100 must work;
512 * this might need to be deadline-scheduled.
515 /* spin until any current reset finishes */
517 temp = ohci_readl (ohci, portstat);
518 if (!(temp & RH_PS_PRS))
523 if (!(temp & RH_PS_CCS))
525 if (temp & RH_PS_PRSC)
526 ohci_writel (ohci, RH_PS_PRSC, portstat);
528 /* start the next reset, sleep till it's probably done */
529 ohci_writel (ohci, RH_PS_PRS, portstat);
530 msleep(PORT_RESET_HW_MSEC);
531 now = ohci_readl(ohci, &ohci->regs->fmnumber);
532 } while (tick_before(now, reset_done));
533 /* caller synchronizes using PRSC */
536 static int ohci_hub_control (
544 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
545 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
549 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
553 case ClearHubFeature:
555 case C_HUB_OVER_CURRENT:
556 ohci_writel (ohci, RH_HS_OCIC,
557 &ohci->regs->roothub.status);
558 case C_HUB_LOCAL_POWER:
564 case ClearPortFeature:
565 if (!wIndex || wIndex > ports)
570 case USB_PORT_FEAT_ENABLE:
573 case USB_PORT_FEAT_C_ENABLE:
576 case USB_PORT_FEAT_SUSPEND:
578 if ((ohci->hc_control & OHCI_CTRL_HCFS)
580 usb_hcd_resume_root_hub(hcd);
582 case USB_PORT_FEAT_C_SUSPEND:
585 case USB_PORT_FEAT_POWER:
588 case USB_PORT_FEAT_C_CONNECTION:
591 case USB_PORT_FEAT_C_OVER_CURRENT:
594 case USB_PORT_FEAT_C_RESET:
600 ohci_writel (ohci, temp,
601 &ohci->regs->roothub.portstatus [wIndex]);
602 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
604 case GetHubDescriptor:
605 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
608 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
609 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
612 if (!wIndex || wIndex > ports)
615 temp = roothub_portstatus (ohci, wIndex);
616 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
618 #ifndef OHCI_VERBOSE_DEBUG
619 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
621 dbg_port (ohci, "GetStatus", wIndex, temp);
625 case C_HUB_OVER_CURRENT:
626 // FIXME: this can be cleared, yes?
627 case C_HUB_LOCAL_POWER:
634 if (!wIndex || wIndex > ports)
638 case USB_PORT_FEAT_SUSPEND:
639 #ifdef CONFIG_USB_OTG
640 if (hcd->self.otg_port == (wIndex + 1)
641 && hcd->self.b_hnp_enable)
645 ohci_writel (ohci, RH_PS_PSS,
646 &ohci->regs->roothub.portstatus [wIndex]);
648 case USB_PORT_FEAT_POWER:
649 ohci_writel (ohci, RH_PS_PPS,
650 &ohci->regs->roothub.portstatus [wIndex]);
652 case USB_PORT_FEAT_RESET:
653 root_port_reset (ohci, wIndex);
662 /* "protocol stall" on error */