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
= 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 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 ports
, 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 ports
= roothub_a (ohci
) & RH_A_NDP
;
323 if (ports
> MAX_ROOT_PORTS
) {
324 ohci_err (ohci
, "bogus NDP=%d, rereads as NDP=%d\n", ports
,
325 ohci_readl (ohci
, &ohci
->regs
->roothub
.a
) & RH_A_NDP
);
326 /* retry later; "should not happen" */
331 if (roothub_status (ohci
) & (RH_HS_LPSC
| RH_HS_OCIC
))
332 buf
[0] = changed
= 1;
340 /* look at each port */
341 for (i
= 0; i
< ports
; i
++) {
342 u32 status
= roothub_portstatus (ohci
, i
);
344 if (status
& (RH_PS_CSC
| RH_PS_PESC
| RH_PS_PSSC
345 | RH_PS_OCIC
| RH_PS_PRSC
)) {
348 buf
[0] |= 1 << (i
+ 1);
350 buf
[1] |= 1 << (i
- 7);
354 /* can suspend if no ports are enabled; or if all all
355 * enabled ports are suspended AND remote wakeup is on.
357 if (!(status
& RH_PS_CCS
))
359 if ((status
& RH_PS_PSS
) && hcd
->remote_wakeup
)
364 spin_unlock_irqrestore (&ohci
->lock
, flags
);
367 /* save power by suspending idle root hubs;
368 * INTR_RD wakes us when there's work
369 * NOTE: if we can do this, we don't need a root hub timer!
374 && ((OHCI_CTRL_HCFS
| OHCI_SCHED_ENABLES
)
377 && time_after (jiffies
, ohci
->next_statechange
)
378 && usb_trylock_device (hcd
->self
.root_hub
)
380 ohci_vdbg (ohci
, "autosuspend\n");
381 (void) ohci_hub_suspend (hcd
);
382 hcd
->state
= HC_STATE_RUNNING
;
383 usb_unlock_device (hcd
->self
.root_hub
);
387 return changed
? length
: 0;
390 /*-------------------------------------------------------------------------*/
393 ohci_hub_descriptor (
394 struct ohci_hcd
*ohci
,
395 struct usb_hub_descriptor
*desc
397 u32 rh
= roothub_a (ohci
);
398 int ports
= rh
& RH_A_NDP
;
401 desc
->bDescriptorType
= 0x29;
402 desc
->bPwrOn2PwrGood
= (rh
& RH_A_POTPGT
) >> 24;
403 desc
->bHubContrCurrent
= 0;
405 desc
->bNbrPorts
= ports
;
406 temp
= 1 + (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 desc
->bitmap
[0] = rh
& RH_B_DR
;
424 desc
->bitmap
[1] = (rh
& RH_B_DR
) >> 8;
425 desc
->bitmap
[2] = desc
->bitmap
[3] = 0xff;
427 desc
->bitmap
[1] = 0xff;
430 /*-------------------------------------------------------------------------*/
432 #ifdef CONFIG_USB_OTG
434 static int ohci_start_port_reset (struct usb_hcd
*hcd
, unsigned port
)
436 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
443 /* start port reset before HNP protocol times out */
444 status
= ohci_readl(ohci
, &ohci
->regs
->roothub
.portstatus
[port
]);
445 if (!(status
& RH_PS_CCS
))
448 /* khubd will finish the reset later */
449 ohci_writel(ohci
, RH_PS_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
453 static void start_hnp(struct ohci_hcd
*ohci
);
457 #define ohci_start_port_reset NULL
461 /*-------------------------------------------------------------------------*/
464 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
465 * not necessarily continuous ... to guard against resume signaling.
466 * The short timeout is safe for non-root hubs, and is backward-compatible
467 * with earlier Linux hosts.
469 #ifdef CONFIG_USB_SUSPEND
470 #define PORT_RESET_MSEC 50
472 #define PORT_RESET_MSEC 10
475 /* this timer value might be vendor-specific ... */
476 #define PORT_RESET_HW_MSEC 10
478 /* wrap-aware logic morphed from <linux/jiffies.h> */
479 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
481 /* called from some task, normally khubd */
482 static inline void root_port_reset (struct ohci_hcd
*ohci
, unsigned port
)
484 __hc32 __iomem
*portstat
= &ohci
->regs
->roothub
.portstatus
[port
];
486 u16 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
487 u16 reset_done
= now
+ PORT_RESET_MSEC
;
489 /* build a "continuous enough" reset signal, with up to
490 * 3msec gap between pulses. scheduler HZ==100 must work;
491 * this might need to be deadline-scheduled.
494 /* spin until any current reset finishes */
496 temp
= ohci_readl (ohci
, portstat
);
497 if (!(temp
& RH_PS_PRS
))
502 if (!(temp
& RH_PS_CCS
))
504 if (temp
& RH_PS_PRSC
)
505 ohci_writel (ohci
, RH_PS_PRSC
, portstat
);
507 /* start the next reset, sleep till it's probably done */
508 ohci_writel (ohci
, RH_PS_PRS
, portstat
);
509 msleep(PORT_RESET_HW_MSEC
);
510 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
511 } while (tick_before(now
, reset_done
));
512 /* caller synchronizes using PRSC */
515 static int ohci_hub_control (
523 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
524 int ports
= hcd_to_bus (hcd
)->root_hub
->maxchild
;
529 case ClearHubFeature
:
531 case C_HUB_OVER_CURRENT
:
532 ohci_writel (ohci
, RH_HS_OCIC
,
533 &ohci
->regs
->roothub
.status
);
534 case C_HUB_LOCAL_POWER
:
540 case ClearPortFeature
:
541 if (!wIndex
|| wIndex
> ports
)
546 case USB_PORT_FEAT_ENABLE
:
549 case USB_PORT_FEAT_C_ENABLE
:
552 case USB_PORT_FEAT_SUSPEND
:
554 if ((ohci
->hc_control
& OHCI_CTRL_HCFS
)
556 schedule_work (&ohci
->rh_resume
);
558 case USB_PORT_FEAT_C_SUSPEND
:
561 case USB_PORT_FEAT_POWER
:
564 case USB_PORT_FEAT_C_CONNECTION
:
567 case USB_PORT_FEAT_C_OVER_CURRENT
:
570 case USB_PORT_FEAT_C_RESET
:
576 ohci_writel (ohci
, temp
,
577 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
578 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
580 case GetHubDescriptor
:
581 ohci_hub_descriptor (ohci
, (struct usb_hub_descriptor
*) buf
);
584 temp
= roothub_status (ohci
) & ~(RH_HS_CRWE
| RH_HS_DRWE
);
585 *(__le32
*) buf
= cpu_to_le32 (temp
);
588 if (!wIndex
|| wIndex
> ports
)
591 temp
= roothub_portstatus (ohci
, wIndex
);
592 *(__le32
*) buf
= cpu_to_le32 (temp
);
594 #ifndef OHCI_VERBOSE_DEBUG
595 if (*(u16
*)(buf
+2)) /* only if wPortChange is interesting */
597 dbg_port (ohci
, "GetStatus", wIndex
, temp
);
601 case C_HUB_OVER_CURRENT
:
602 // FIXME: this can be cleared, yes?
603 case C_HUB_LOCAL_POWER
:
610 if (!wIndex
|| wIndex
> ports
)
614 case USB_PORT_FEAT_SUSPEND
:
615 #ifdef CONFIG_USB_OTG
616 if (hcd
->self
.otg_port
== (wIndex
+ 1)
617 && hcd
->self
.b_hnp_enable
)
621 ohci_writel (ohci
, RH_PS_PSS
,
622 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
624 case USB_PORT_FEAT_POWER
:
625 ohci_writel (ohci
, RH_PS_PPS
,
626 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
628 case USB_PORT_FEAT_RESET
:
629 root_port_reset (ohci
, wIndex
);
638 /* "protocol stall" on error */