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 = USB_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 = HCD_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 == USB_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, "odd resume\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 = roothub_a (ohci) & RH_A_NDP;
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) need extra-long delays */
201 hcd->state = USB_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 periodic_reinit (ohci);
221 /* interrupts might have been disabled */
222 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
223 if (ohci->ed_rm_list)
224 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
225 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
226 &ohci->regs->intrstatus);
228 /* Then re-enable operations */
229 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
230 (void) ohci_readl (ohci, &ohci->regs->control);
233 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
235 temp |= OHCI_CTRL_RWC;
236 ohci->hc_control = temp;
237 ohci_writel (ohci, temp, &ohci->regs->control);
238 (void) ohci_readl (ohci, &ohci->regs->control);
243 /* keep it alive for ~5x suspend + resume costs */
244 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
246 /* maybe turn schedules back on */
249 if (!ohci->ed_rm_list) {
250 if (ohci->ed_controltail) {
252 find_head (ohci->ed_controltail)->dma,
253 &ohci->regs->ed_controlhead);
254 enables |= OHCI_CTRL_CLE;
257 if (ohci->ed_bulktail) {
258 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
259 &ohci->regs->ed_bulkhead);
260 enables |= OHCI_CTRL_BLE;
264 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
265 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
267 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
268 ohci->hc_control |= enables;
269 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
271 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
272 (void) ohci_readl (ohci, &ohci->regs->control);
275 hcd->state = USB_STATE_RUNNING;
279 static void ohci_rh_resume (void *_hcd)
281 struct usb_hcd *hcd = _hcd;
283 usb_lock_device (hcd->self.root_hub);
284 (void) ohci_hub_resume (hcd);
285 usb_unlock_device (hcd->self.root_hub);
290 static void ohci_rh_resume (void *_hcd)
292 struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
293 ohci_dbg(ohci, "rh_resume ??\n");
296 #endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
298 /*-------------------------------------------------------------------------*/
300 /* build "status change" packet (one or two bytes) from HC registers */
303 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
305 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
306 int ports, i, changed = 0, length = 1;
307 int can_suspend = hcd->can_wakeup;
310 spin_lock_irqsave (&ohci->lock, flags);
312 /* handle autosuspended root: finish resuming before
313 * letting khubd or root hub timer see state changes.
315 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
316 || !HCD_IS_RUNNING(hcd->state)) {
321 ports = roothub_a (ohci) & RH_A_NDP;
322 if (ports > MAX_ROOT_PORTS) {
323 ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n", ports,
324 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
325 /* retry later; "should not happen" */
330 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
331 buf [0] = changed = 1;
339 /* look at each port */
340 for (i = 0; i < ports; i++) {
341 u32 status = roothub_portstatus (ohci, i);
343 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
344 | RH_PS_OCIC | RH_PS_PRSC)) {
347 buf [0] |= 1 << (i + 1);
349 buf [1] |= 1 << (i - 7);
353 /* can suspend if no ports are enabled; or if all all
354 * enabled ports are suspended AND remote wakeup is on.
356 if (!(status & RH_PS_CCS))
358 if ((status & RH_PS_PSS) && hcd->remote_wakeup)
363 spin_unlock_irqrestore (&ohci->lock, flags);
366 /* save power by suspending idle root hubs;
367 * INTR_RD wakes us when there's work
368 * NOTE: if we can do this, we don't need a root hub timer!
373 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
376 && time_after (jiffies, ohci->next_statechange)
377 && usb_trylock_device (hcd->self.root_hub)
379 ohci_vdbg (ohci, "autosuspend\n");
380 (void) ohci_hub_suspend (hcd);
381 hcd->state = USB_STATE_RUNNING;
382 usb_unlock_device (hcd->self.root_hub);
386 return changed ? length : 0;
389 /*-------------------------------------------------------------------------*/
392 ohci_hub_descriptor (
393 struct ohci_hcd *ohci,
394 struct usb_hub_descriptor *desc
396 u32 rh = roothub_a (ohci);
397 int ports = rh & RH_A_NDP;
400 desc->bDescriptorType = 0x29;
401 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
402 desc->bHubContrCurrent = 0;
404 desc->bNbrPorts = ports;
405 temp = 1 + (ports / 8);
406 desc->bDescLength = 7 + 2 * temp;
409 if (rh & RH_A_NPS) /* no power switching? */
411 if (rh & RH_A_PSM) /* per-port power switching? */
413 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
415 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
417 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
419 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
420 rh = roothub_b (ohci);
421 desc->bitmap [0] = rh & RH_B_DR;
423 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
424 desc->bitmap [2] = desc->bitmap [3] = 0xff;
426 desc->bitmap [1] = 0xff;
429 /*-------------------------------------------------------------------------*/
431 #ifdef CONFIG_USB_OTG
433 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
435 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
442 /* start port reset before HNP protocol times out */
443 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
444 if (!(status & RH_PS_CCS))
447 /* khubd will finish the reset later */
448 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
452 static void start_hnp(struct ohci_hcd *ohci);
456 #define ohci_start_port_reset NULL
460 /*-------------------------------------------------------------------------*/
463 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
464 * not necessarily continuous ... to guard against resume signaling.
465 * The short timeout is safe for non-root hubs, and is backward-compatible
466 * with earlier Linux hosts.
468 #ifdef CONFIG_USB_SUSPEND
469 #define PORT_RESET_MSEC 50
471 #define PORT_RESET_MSEC 10
474 /* this timer value might be vendor-specific ... */
475 #define PORT_RESET_HW_MSEC 10
477 /* wrap-aware logic morphed from <linux/jiffies.h> */
478 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
480 /* called from some task, normally khubd */
481 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
483 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
485 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
486 u16 reset_done = now + PORT_RESET_MSEC;
488 /* build a "continuous enough" reset signal, with up to
489 * 3msec gap between pulses. scheduler HZ==100 must work;
490 * this might need to be deadline-scheduled.
493 /* spin until any current reset finishes */
495 temp = ohci_readl (ohci, portstat);
496 if (!(temp & RH_PS_PRS))
501 if (!(temp & RH_PS_CCS))
503 if (temp & RH_PS_PRSC)
504 ohci_writel (ohci, RH_PS_PRSC, portstat);
506 /* start the next reset, sleep till it's probably done */
507 ohci_writel (ohci, RH_PS_PRS, portstat);
508 msleep(PORT_RESET_HW_MSEC);
509 now = ohci_readl(ohci, &ohci->regs->fmnumber);
510 } while (tick_before(now, reset_done));
511 /* caller synchronizes using PRSC */
514 static int ohci_hub_control (
522 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
523 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
528 case ClearHubFeature:
530 case C_HUB_OVER_CURRENT:
531 ohci_writel (ohci, RH_HS_OCIC,
532 &ohci->regs->roothub.status);
533 case C_HUB_LOCAL_POWER:
539 case ClearPortFeature:
540 if (!wIndex || wIndex > ports)
545 case USB_PORT_FEAT_ENABLE:
548 case USB_PORT_FEAT_C_ENABLE:
551 case USB_PORT_FEAT_SUSPEND:
553 if ((ohci->hc_control & OHCI_CTRL_HCFS)
555 schedule_work (&ohci->rh_resume);
557 case USB_PORT_FEAT_C_SUSPEND:
560 case USB_PORT_FEAT_POWER:
563 case USB_PORT_FEAT_C_CONNECTION:
566 case USB_PORT_FEAT_C_OVER_CURRENT:
569 case USB_PORT_FEAT_C_RESET:
575 ohci_writel (ohci, temp,
576 &ohci->regs->roothub.portstatus [wIndex]);
577 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
579 case GetHubDescriptor:
580 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
583 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
584 *(__le32 *) buf = cpu_to_le32 (temp);
587 if (!wIndex || wIndex > ports)
590 temp = roothub_portstatus (ohci, wIndex);
591 *(__le32 *) buf = cpu_to_le32 (temp);
593 #ifndef OHCI_VERBOSE_DEBUG
594 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
596 dbg_port (ohci, "GetStatus", wIndex, temp);
600 case C_HUB_OVER_CURRENT:
601 // FIXME: this can be cleared, yes?
602 case C_HUB_LOCAL_POWER:
609 if (!wIndex || wIndex > ports)
613 case USB_PORT_FEAT_SUSPEND:
614 #ifdef CONFIG_USB_OTG
615 if (hcd->self.otg_port == (wIndex + 1)
616 && hcd->self.b_hnp_enable)
620 ohci_writel (ohci, RH_PS_PSS,
621 &ohci->regs->roothub.portstatus [wIndex]);
623 case USB_PORT_FEAT_POWER:
624 ohci_writel (ohci, RH_PS_PPS,
625 &ohci->regs->roothub.portstatus [wIndex]);
627 case USB_PORT_FEAT_RESET:
628 root_port_reset (ohci, wIndex);
637 /* "protocol stall" on error */