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 #if defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
41 #define OHCI_SCHED_ENABLES \
42 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
44 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
45 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
46 static int ohci_restart (struct ohci_hcd *ohci);
48 static int ohci_hub_suspend (struct usb_hcd *hcd)
50 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
54 spin_lock_irqsave (&ohci->lock, flags);
56 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
57 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
59 ohci_dbg (ohci, "resume/suspend?\n");
60 ohci->hc_control &= ~OHCI_CTRL_HCFS;
61 ohci->hc_control |= OHCI_USB_RESET;
62 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
63 (void) ohci_readl (ohci, &ohci->regs->control);
67 ohci_dbg (ohci, "needs reinit!\n");
69 case OHCI_USB_SUSPEND:
70 ohci_dbg (ohci, "already suspended\n");
73 ohci_dbg (ohci, "suspend root hub\n");
75 /* First stop any processing */
76 hcd->state = HC_STATE_QUIESCING;
77 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
80 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
81 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
82 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
83 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
85 /* sched disables take effect on the next frame,
86 * then the last WDH could take 6+ msec
88 ohci_dbg (ohci, "stopping schedules ...\n");
93 if (ohci_readl (ohci, &ohci->regs->intrstatus)
97 dl_done_list (ohci, NULL);
100 dl_done_list (ohci, NULL);
101 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
102 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
103 &ohci->regs->intrstatus);
105 /* maybe resume can wake root hub */
106 if (hcd->remote_wakeup)
107 ohci->hc_control |= OHCI_CTRL_RWE;
109 ohci->hc_control &= ~OHCI_CTRL_RWE;
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 */
118 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
122 hcd->state = HC_STATE_SUSPENDED;
123 spin_unlock_irqrestore (&ohci->lock, flags);
127 static inline struct ed *find_head (struct ed *ed)
129 /* for bulk and control lists */
135 /* caller has locked the root hub */
136 static int ohci_hub_resume (struct usb_hcd *hcd)
138 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
140 int status = -EINPROGRESS;
142 if (time_before (jiffies, ohci->next_statechange))
145 spin_lock_irq (&ohci->lock);
146 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
148 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
149 /* this can happen after suspend-to-disk */
150 if (hcd->state == HC_STATE_RESUMING) {
151 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
154 /* this happens when pmcore resumes HC then root */
156 ohci_dbg (ohci, "duplicate resume\n");
159 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
160 case OHCI_USB_SUSPEND:
161 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
162 ohci->hc_control |= OHCI_USB_RESUME;
163 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
164 (void) ohci_readl (ohci, &ohci->regs->control);
165 ohci_dbg (ohci, "resume root hub\n");
167 case OHCI_USB_RESUME:
168 /* HCFS changes sometime after INTR_RD */
169 ohci_info (ohci, "wakeup\n");
172 ohci_dbg (ohci, "already resumed\n");
175 default: /* RESET, we lost power */
176 ohci_dbg (ohci, "root hub hardware reset\n");
179 spin_unlock_irq (&ohci->lock);
180 if (status == -EBUSY) {
181 (void) ohci_init (ohci);
182 return ohci_restart (ohci);
184 if (status != -EINPROGRESS)
187 temp = ohci->num_ports;
190 u32 stat = ohci_readl (ohci,
191 &ohci->regs->roothub.portstatus [temp]);
193 /* force global, not selective, resume */
194 if (!(stat & RH_PS_PSS))
196 ohci_writel (ohci, RH_PS_POCI,
197 &ohci->regs->roothub.portstatus [temp]);
200 /* Some controllers (lucent erratum) need extra-long delays */
201 hcd->state = HC_STATE_RESUMING;
202 mdelay (20 /* usb 11.5.1.10 */ + 15);
204 temp = ohci_readl (ohci, &ohci->regs->control);
205 temp &= OHCI_CTRL_HCFS;
206 if (temp != OHCI_USB_RESUME) {
207 ohci_err (ohci, "controller won't resume\n");
211 /* disable old schedule state, reinit from scratch */
212 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
213 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
214 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
215 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
216 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
217 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
219 /* Sometimes PCI D3 suspend trashes frame timings ... */
220 periodic_reinit (ohci);
222 /* interrupts might have been disabled */
223 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
224 if (ohci->ed_rm_list)
225 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
226 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
227 &ohci->regs->intrstatus);
229 /* Then re-enable operations */
230 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
231 (void) ohci_readl (ohci, &ohci->regs->control);
234 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
236 temp |= OHCI_CTRL_RWC;
237 ohci->hc_control = temp;
238 ohci_writel (ohci, temp, &ohci->regs->control);
239 (void) ohci_readl (ohci, &ohci->regs->control);
244 /* keep it alive for ~5x suspend + resume costs */
245 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
247 /* maybe turn schedules back on */
250 if (!ohci->ed_rm_list) {
251 if (ohci->ed_controltail) {
253 find_head (ohci->ed_controltail)->dma,
254 &ohci->regs->ed_controlhead);
255 enables |= OHCI_CTRL_CLE;
258 if (ohci->ed_bulktail) {
259 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
260 &ohci->regs->ed_bulkhead);
261 enables |= OHCI_CTRL_BLE;
265 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
266 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
268 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
269 ohci->hc_control |= enables;
270 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
272 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
273 (void) ohci_readl (ohci, &ohci->regs->control);
276 hcd->state = HC_STATE_RUNNING;
280 static void ohci_rh_resume (void *_hcd)
282 struct usb_hcd *hcd = _hcd;
284 usb_lock_device (hcd->self.root_hub);
285 (void) ohci_hub_resume (hcd);
286 usb_unlock_device (hcd->self.root_hub);
291 static void ohci_rh_resume (void *_hcd)
293 struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
294 ohci_dbg(ohci, "rh_resume ??\n");
297 #endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
299 /*-------------------------------------------------------------------------*/
301 /* build "status change" packet (one or two bytes) from HC registers */
304 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
306 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
307 int i, changed = 0, length = 1;
308 int can_suspend = hcd->can_wakeup;
311 spin_lock_irqsave (&ohci->lock, flags);
313 /* handle autosuspended root: finish resuming before
314 * letting khubd or root hub timer see state changes.
316 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
317 || !HC_IS_RUNNING(hcd->state)) {
322 /* undocumented erratum seen on at least rev D */
323 if ((ohci->flags & OHCI_QUIRK_AMD756)
324 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
325 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
326 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
327 /* retry later; "should not happen" */
332 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
333 buf [0] = changed = 1;
336 if (ohci->num_ports > 7) {
341 /* look at each port */
342 for (i = 0; i < ohci->num_ports; i++) {
343 u32 status = roothub_portstatus (ohci, i);
345 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
346 | RH_PS_OCIC | RH_PS_PRSC)) {
349 buf [0] |= 1 << (i + 1);
351 buf [1] |= 1 << (i - 7);
355 /* can suspend if no ports are enabled; or if all all
356 * enabled ports are suspended AND remote wakeup is on.
358 if (!(status & RH_PS_CCS))
360 if ((status & RH_PS_PSS) && hcd->remote_wakeup)
365 spin_unlock_irqrestore (&ohci->lock, flags);
368 /* save power by suspending idle root hubs;
369 * INTR_RD wakes us when there's work
370 * NOTE: if we can do this, we don't need a root hub timer!
375 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
378 && time_after (jiffies, ohci->next_statechange)
379 && usb_trylock_device (hcd->self.root_hub)
381 ohci_vdbg (ohci, "autosuspend\n");
382 (void) ohci_hub_suspend (hcd);
383 hcd->state = HC_STATE_RUNNING;
384 usb_unlock_device (hcd->self.root_hub);
388 return changed ? length : 0;
391 /*-------------------------------------------------------------------------*/
394 ohci_hub_descriptor (
395 struct ohci_hcd *ohci,
396 struct usb_hub_descriptor *desc
398 u32 rh = roothub_a (ohci);
401 desc->bDescriptorType = 0x29;
402 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
403 desc->bHubContrCurrent = 0;
405 desc->bNbrPorts = ohci->num_ports;
406 temp = 1 + (ohci->num_ports / 8);
407 desc->bDescLength = 7 + 2 * temp;
410 if (rh & RH_A_NPS) /* no power switching? */
412 if (rh & RH_A_PSM) /* per-port power switching? */
414 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
416 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
418 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
420 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
421 rh = roothub_b (ohci);
422 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
423 desc->bitmap [0] = rh & RH_B_DR;
424 if (ohci->num_ports > 7) {
425 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
426 desc->bitmap [2] = 0xff;
428 desc->bitmap [1] = 0xff;
431 /*-------------------------------------------------------------------------*/
433 #ifdef CONFIG_USB_OTG
435 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
437 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
444 /* start port reset before HNP protocol times out */
445 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
446 if (!(status & RH_PS_CCS))
449 /* khubd will finish the reset later */
450 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
454 static void start_hnp(struct ohci_hcd *ohci);
458 #define ohci_start_port_reset NULL
462 /*-------------------------------------------------------------------------*/
465 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
466 * not necessarily continuous ... to guard against resume signaling.
467 * The short timeout is safe for non-root hubs, and is backward-compatible
468 * with earlier Linux hosts.
470 #ifdef CONFIG_USB_SUSPEND
471 #define PORT_RESET_MSEC 50
473 #define PORT_RESET_MSEC 10
476 /* this timer value might be vendor-specific ... */
477 #define PORT_RESET_HW_MSEC 10
479 /* wrap-aware logic morphed from <linux/jiffies.h> */
480 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
482 /* called from some task, normally khubd */
483 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
485 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
487 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
488 u16 reset_done = now + PORT_RESET_MSEC;
490 /* build a "continuous enough" reset signal, with up to
491 * 3msec gap between pulses. scheduler HZ==100 must work;
492 * this might need to be deadline-scheduled.
495 /* spin until any current reset finishes */
497 temp = ohci_readl (ohci, portstat);
498 if (!(temp & RH_PS_PRS))
503 if (!(temp & RH_PS_CCS))
505 if (temp & RH_PS_PRSC)
506 ohci_writel (ohci, RH_PS_PRSC, portstat);
508 /* start the next reset, sleep till it's probably done */
509 ohci_writel (ohci, RH_PS_PRS, portstat);
510 msleep(PORT_RESET_HW_MSEC);
511 now = ohci_readl(ohci, &ohci->regs->fmnumber);
512 } while (tick_before(now, reset_done));
513 /* caller synchronizes using PRSC */
516 static int ohci_hub_control (
524 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
525 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
530 case ClearHubFeature:
532 case C_HUB_OVER_CURRENT:
533 ohci_writel (ohci, RH_HS_OCIC,
534 &ohci->regs->roothub.status);
535 case C_HUB_LOCAL_POWER:
541 case ClearPortFeature:
542 if (!wIndex || wIndex > ports)
547 case USB_PORT_FEAT_ENABLE:
550 case USB_PORT_FEAT_C_ENABLE:
553 case USB_PORT_FEAT_SUSPEND:
555 if ((ohci->hc_control & OHCI_CTRL_HCFS)
557 schedule_work (&ohci->rh_resume);
559 case USB_PORT_FEAT_C_SUSPEND:
562 case USB_PORT_FEAT_POWER:
565 case USB_PORT_FEAT_C_CONNECTION:
568 case USB_PORT_FEAT_C_OVER_CURRENT:
571 case USB_PORT_FEAT_C_RESET:
577 ohci_writel (ohci, temp,
578 &ohci->regs->roothub.portstatus [wIndex]);
579 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
581 case GetHubDescriptor:
582 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
585 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
586 *(__le32 *) buf = cpu_to_le32 (temp);
589 if (!wIndex || wIndex > ports)
592 temp = roothub_portstatus (ohci, wIndex);
593 *(__le32 *) buf = cpu_to_le32 (temp);
595 #ifndef OHCI_VERBOSE_DEBUG
596 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
598 dbg_port (ohci, "GetStatus", wIndex, temp);
602 case C_HUB_OVER_CURRENT:
603 // FIXME: this can be cleared, yes?
604 case C_HUB_LOCAL_POWER:
611 if (!wIndex || wIndex > ports)
615 case USB_PORT_FEAT_SUSPEND:
616 #ifdef CONFIG_USB_OTG
617 if (hcd->self.otg_port == (wIndex + 1)
618 && hcd->self.b_hnp_enable)
622 ohci_writel (ohci, RH_PS_PSS,
623 &ohci->regs->roothub.portstatus [wIndex]);
625 case USB_PORT_FEAT_POWER:
626 ohci_writel (ohci, RH_PS_PPS,
627 &ohci->regs->roothub.portstatus [wIndex]);
629 case USB_PORT_FEAT_RESET:
630 root_port_reset (ohci, wIndex);
639 /* "protocol stall" on error */