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 spin_unlock_irqrestore (&ohci
->lock
, flags
);
142 static inline struct ed
*find_head (struct ed
*ed
)
144 /* for bulk and control lists */
150 /* caller has locked the root hub */
151 static int ohci_bus_resume (struct usb_hcd
*hcd
)
153 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
155 int status
= -EINPROGRESS
;
158 if (time_before (jiffies
, ohci
->next_statechange
))
161 spin_lock_irqsave (&ohci
->lock
, flags
);
163 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))) {
164 spin_unlock_irqrestore (&ohci
->lock
, flags
);
169 ohci
->hc_control
= ohci_readl (ohci
, &ohci
->regs
->control
);
171 if (ohci
->hc_control
& (OHCI_CTRL_IR
| OHCI_SCHED_ENABLES
)) {
172 /* this can happen after resuming a swsusp snapshot */
173 if (hcd
->state
== HC_STATE_RESUMING
) {
174 ohci_dbg (ohci
, "BIOS/SMM active, control %03x\n",
177 /* this happens when pmcore resumes HC then root */
179 ohci_dbg (ohci
, "duplicate resume\n");
182 } else switch (ohci
->hc_control
& OHCI_CTRL_HCFS
) {
183 case OHCI_USB_SUSPEND
:
184 ohci
->hc_control
&= ~(OHCI_CTRL_HCFS
|OHCI_SCHED_ENABLES
);
185 ohci
->hc_control
|= OHCI_USB_RESUME
;
186 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
187 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
188 ohci_dbg (ohci
, "resume root hub\n");
190 case OHCI_USB_RESUME
:
191 /* HCFS changes sometime after INTR_RD */
192 ohci_info (ohci
, "wakeup\n");
195 /* this can happen after resuming a swsusp snapshot */
196 ohci_dbg (ohci
, "snapshot resume? reinit\n");
199 default: /* RESET, we lost power */
200 ohci_dbg (ohci
, "lost power\n");
203 spin_unlock_irqrestore (&ohci
->lock
, flags
);
204 if (status
== -EBUSY
) {
205 (void) ohci_init (ohci
);
206 return ohci_restart (ohci
);
208 if (status
!= -EINPROGRESS
)
211 temp
= ohci
->num_ports
;
214 u32 stat
= ohci_readl (ohci
,
215 &ohci
->regs
->roothub
.portstatus
[temp
]);
217 /* force global, not selective, resume */
218 if (!(stat
& RH_PS_PSS
))
220 ohci_writel (ohci
, RH_PS_POCI
,
221 &ohci
->regs
->roothub
.portstatus
[temp
]);
224 /* Some controllers (lucent erratum) need extra-long delays */
225 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
227 temp
= ohci_readl (ohci
, &ohci
->regs
->control
);
228 temp
&= OHCI_CTRL_HCFS
;
229 if (temp
!= OHCI_USB_RESUME
) {
230 ohci_err (ohci
, "controller won't resume\n");
234 /* disable old schedule state, reinit from scratch */
235 ohci_writel (ohci
, 0, &ohci
->regs
->ed_controlhead
);
236 ohci_writel (ohci
, 0, &ohci
->regs
->ed_controlcurrent
);
237 ohci_writel (ohci
, 0, &ohci
->regs
->ed_bulkhead
);
238 ohci_writel (ohci
, 0, &ohci
->regs
->ed_bulkcurrent
);
239 ohci_writel (ohci
, 0, &ohci
->regs
->ed_periodcurrent
);
240 ohci_writel (ohci
, (u32
) ohci
->hcca_dma
, &ohci
->regs
->hcca
);
242 /* Sometimes PCI D3 suspend trashes frame timings ... */
243 periodic_reinit (ohci
);
245 /* interrupts might have been disabled */
246 ohci_writel (ohci
, OHCI_INTR_INIT
, &ohci
->regs
->intrenable
);
247 if (ohci
->ed_rm_list
)
248 ohci_writel (ohci
, OHCI_INTR_SF
, &ohci
->regs
->intrenable
);
249 ohci_writel (ohci
, ohci_readl (ohci
, &ohci
->regs
->intrstatus
),
250 &ohci
->regs
->intrstatus
);
252 /* Then re-enable operations */
253 ohci_writel (ohci
, OHCI_USB_OPER
, &ohci
->regs
->control
);
254 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
257 temp
= ohci
->hc_control
;
258 temp
&= OHCI_CTRL_RWC
;
259 temp
|= OHCI_CONTROL_INIT
| OHCI_USB_OPER
;
260 ohci
->hc_control
= temp
;
261 ohci_writel (ohci
, temp
, &ohci
->regs
->control
);
262 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
267 /* keep it alive for more than ~5x suspend + resume costs */
268 ohci
->next_statechange
= jiffies
+ STATECHANGE_DELAY
;
270 /* maybe turn schedules back on */
273 if (!ohci
->ed_rm_list
) {
274 if (ohci
->ed_controltail
) {
276 find_head (ohci
->ed_controltail
)->dma
,
277 &ohci
->regs
->ed_controlhead
);
278 enables
|= OHCI_CTRL_CLE
;
281 if (ohci
->ed_bulktail
) {
282 ohci_writel (ohci
, find_head (ohci
->ed_bulktail
)->dma
,
283 &ohci
->regs
->ed_bulkhead
);
284 enables
|= OHCI_CTRL_BLE
;
288 if (hcd
->self
.bandwidth_isoc_reqs
|| hcd
->self
.bandwidth_int_reqs
)
289 enables
|= OHCI_CTRL_PLE
|OHCI_CTRL_IE
;
291 ohci_dbg (ohci
, "restarting schedules ... %08x\n", enables
);
292 ohci
->hc_control
|= enables
;
293 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
295 ohci_writel (ohci
, temp
, &ohci
->regs
->cmdstatus
);
296 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
302 #endif /* CONFIG_PM */
304 /*-------------------------------------------------------------------------*/
306 /* build "status change" packet (one or two bytes) from HC registers */
309 ohci_hub_status_data (struct usb_hcd
*hcd
, char *buf
)
311 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
312 int i
, changed
= 0, length
= 1;
316 can_suspend
= device_may_wakeup(&hcd
->self
.root_hub
->dev
);
317 spin_lock_irqsave (&ohci
->lock
, flags
);
319 /* handle autosuspended root: finish resuming before
320 * letting khubd or root hub timer see state changes.
322 if (unlikely((ohci
->hc_control
& OHCI_CTRL_HCFS
) != OHCI_USB_OPER
323 || !HC_IS_RUNNING(hcd
->state
))) {
328 /* undocumented erratum seen on at least rev D */
329 if ((ohci
->flags
& OHCI_QUIRK_AMD756
)
330 && (roothub_a (ohci
) & RH_A_NDP
) > MAX_ROOT_PORTS
) {
331 ohci_warn (ohci
, "bogus NDP, rereads as NDP=%d\n",
332 ohci_readl (ohci
, &ohci
->regs
->roothub
.a
) & RH_A_NDP
);
333 /* retry later; "should not happen" */
338 if (roothub_status (ohci
) & (RH_HS_LPSC
| RH_HS_OCIC
))
339 buf
[0] = changed
= 1;
342 if (ohci
->num_ports
> 7) {
347 /* look at each port */
348 for (i
= 0; i
< ohci
->num_ports
; i
++) {
349 u32 status
= roothub_portstatus (ohci
, i
);
351 /* can't autosuspend with active ports */
352 if ((status
& RH_PS_PES
) && !(status
& RH_PS_PSS
))
355 if (status
& (RH_PS_CSC
| RH_PS_PESC
| RH_PS_PSSC
356 | RH_PS_OCIC
| RH_PS_PRSC
)) {
359 buf
[0] |= 1 << (i
+ 1);
361 buf
[1] |= 1 << (i
- 7);
366 /* after root hub changes, stop polling after debouncing
367 * for a while and maybe kicking in autosuspend
370 ohci
->next_statechange
= jiffies
+ STATECHANGE_DELAY
;
372 } else if (time_before (jiffies
, ohci
->next_statechange
)) {
376 can_suspend
= can_suspend
378 && ((OHCI_CTRL_HCFS
| OHCI_SCHED_ENABLES
)
382 if (hcd
->uses_new_polling
) {
384 /* use INTR_RHSC iff INTR_RD won't apply */
386 ohci_writel (ohci
, OHCI_INTR_RHSC
,
387 &ohci
->regs
->intrenable
);
392 spin_unlock_irqrestore (&ohci
->lock
, flags
);
395 /* save power by autosuspending idle root hubs;
396 * INTR_RD wakes us when there's work
398 if (can_suspend
&& usb_trylock_device (hcd
->self
.root_hub
) == 0) {
399 ohci_vdbg (ohci
, "autosuspend\n");
400 (void) ohci_bus_suspend (hcd
);
401 usb_unlock_device (hcd
->self
.root_hub
);
405 return changed
? length
: 0;
408 /*-------------------------------------------------------------------------*/
411 ohci_hub_descriptor (
412 struct ohci_hcd
*ohci
,
413 struct usb_hub_descriptor
*desc
415 u32 rh
= roothub_a (ohci
);
418 desc
->bDescriptorType
= 0x29;
419 desc
->bPwrOn2PwrGood
= (rh
& RH_A_POTPGT
) >> 24;
420 desc
->bHubContrCurrent
= 0;
422 desc
->bNbrPorts
= ohci
->num_ports
;
423 temp
= 1 + (ohci
->num_ports
/ 8);
424 desc
->bDescLength
= 7 + 2 * temp
;
427 if (rh
& RH_A_NPS
) /* no power switching? */
429 if (rh
& RH_A_PSM
) /* per-port power switching? */
431 if (rh
& RH_A_NOCP
) /* no overcurrent reporting? */
433 else if (rh
& RH_A_OCPM
) /* per-port overcurrent reporting? */
435 desc
->wHubCharacteristics
= (__force __u16
)cpu_to_hc16(ohci
, temp
);
437 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
438 rh
= roothub_b (ohci
);
439 memset(desc
->bitmap
, 0xff, sizeof(desc
->bitmap
));
440 desc
->bitmap
[0] = rh
& RH_B_DR
;
441 if (ohci
->num_ports
> 7) {
442 desc
->bitmap
[1] = (rh
& RH_B_DR
) >> 8;
443 desc
->bitmap
[2] = 0xff;
445 desc
->bitmap
[1] = 0xff;
448 /*-------------------------------------------------------------------------*/
450 #ifdef CONFIG_USB_OTG
452 static int ohci_start_port_reset (struct usb_hcd
*hcd
, unsigned port
)
454 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
461 /* start port reset before HNP protocol times out */
462 status
= ohci_readl(ohci
, &ohci
->regs
->roothub
.portstatus
[port
]);
463 if (!(status
& RH_PS_CCS
))
466 /* khubd will finish the reset later */
467 ohci_writel(ohci
, RH_PS_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
471 static void start_hnp(struct ohci_hcd
*ohci
);
475 #define ohci_start_port_reset NULL
479 /*-------------------------------------------------------------------------*/
482 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
483 * not necessarily continuous ... to guard against resume signaling.
484 * The short timeout is safe for non-root hubs, and is backward-compatible
485 * with earlier Linux hosts.
487 #ifdef CONFIG_USB_SUSPEND
488 #define PORT_RESET_MSEC 50
490 #define PORT_RESET_MSEC 10
493 /* this timer value might be vendor-specific ... */
494 #define PORT_RESET_HW_MSEC 10
496 /* wrap-aware logic morphed from <linux/jiffies.h> */
497 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
499 /* called from some task, normally khubd */
500 static inline void root_port_reset (struct ohci_hcd
*ohci
, unsigned port
)
502 __hc32 __iomem
*portstat
= &ohci
->regs
->roothub
.portstatus
[port
];
504 u16 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
505 u16 reset_done
= now
+ PORT_RESET_MSEC
;
507 /* build a "continuous enough" reset signal, with up to
508 * 3msec gap between pulses. scheduler HZ==100 must work;
509 * this might need to be deadline-scheduled.
512 /* spin until any current reset finishes */
514 temp
= ohci_readl (ohci
, portstat
);
515 if (!(temp
& RH_PS_PRS
))
520 if (!(temp
& RH_PS_CCS
))
522 if (temp
& RH_PS_PRSC
)
523 ohci_writel (ohci
, RH_PS_PRSC
, portstat
);
525 /* start the next reset, sleep till it's probably done */
526 ohci_writel (ohci
, RH_PS_PRS
, portstat
);
527 msleep(PORT_RESET_HW_MSEC
);
528 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
529 } while (tick_before(now
, reset_done
));
530 /* caller synchronizes using PRSC */
533 static int ohci_hub_control (
541 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
542 int ports
= hcd_to_bus (hcd
)->root_hub
->maxchild
;
546 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
)))
550 case ClearHubFeature
:
552 case C_HUB_OVER_CURRENT
:
553 ohci_writel (ohci
, RH_HS_OCIC
,
554 &ohci
->regs
->roothub
.status
);
555 case C_HUB_LOCAL_POWER
:
561 case ClearPortFeature
:
562 if (!wIndex
|| wIndex
> ports
)
567 case USB_PORT_FEAT_ENABLE
:
570 case USB_PORT_FEAT_C_ENABLE
:
573 case USB_PORT_FEAT_SUSPEND
:
575 if ((ohci
->hc_control
& OHCI_CTRL_HCFS
)
577 usb_hcd_resume_root_hub(hcd
);
579 case USB_PORT_FEAT_C_SUSPEND
:
582 case USB_PORT_FEAT_POWER
:
585 case USB_PORT_FEAT_C_CONNECTION
:
588 case USB_PORT_FEAT_C_OVER_CURRENT
:
591 case USB_PORT_FEAT_C_RESET
:
597 ohci_writel (ohci
, temp
,
598 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
599 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
601 case GetHubDescriptor
:
602 ohci_hub_descriptor (ohci
, (struct usb_hub_descriptor
*) buf
);
605 temp
= roothub_status (ohci
) & ~(RH_HS_CRWE
| RH_HS_DRWE
);
606 put_unaligned(cpu_to_le32 (temp
), (__le32
*) buf
);
609 if (!wIndex
|| wIndex
> ports
)
612 temp
= roothub_portstatus (ohci
, wIndex
);
613 put_unaligned(cpu_to_le32 (temp
), (__le32
*) buf
);
615 #ifndef OHCI_VERBOSE_DEBUG
616 if (*(u16
*)(buf
+2)) /* only if wPortChange is interesting */
618 dbg_port (ohci
, "GetStatus", wIndex
, temp
);
622 case C_HUB_OVER_CURRENT
:
623 // FIXME: this can be cleared, yes?
624 case C_HUB_LOCAL_POWER
:
631 if (!wIndex
|| wIndex
> ports
)
635 case USB_PORT_FEAT_SUSPEND
:
636 #ifdef CONFIG_USB_OTG
637 if (hcd
->self
.otg_port
== (wIndex
+ 1)
638 && hcd
->self
.b_hnp_enable
)
642 ohci_writel (ohci
, RH_PS_PSS
,
643 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
645 case USB_PORT_FEAT_POWER
:
646 ohci_writel (ohci
, RH_PS_PPS
,
647 &ohci
->regs
->roothub
.portstatus
[wIndex
]);
649 case USB_PORT_FEAT_RESET
:
650 root_port_reset (ohci
, wIndex
);
659 /* "protocol stall" on error */