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 /*-------------------------------------------------------------------------*/
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_bus_suspend (struct usb_hcd
*hcd
)
50 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
54 spin_lock_irqsave (&ohci
->lock
, flags
);
56 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))) {
57 spin_unlock_irqrestore (&ohci
->lock
, flags
);
61 ohci
->hc_control
= ohci_readl (ohci
, &ohci
->regs
->control
);
62 switch (ohci
->hc_control
& OHCI_CTRL_HCFS
) {
64 ohci_dbg (ohci
, "resume/suspend?\n");
65 ohci
->hc_control
&= ~OHCI_CTRL_HCFS
;
66 ohci
->hc_control
|= OHCI_USB_RESET
;
67 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
68 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
72 ohci_dbg (ohci
, "needs reinit!\n");
74 case OHCI_USB_SUSPEND
:
75 ohci_dbg (ohci
, "already suspended\n");
78 ohci_dbg (ohci
, "suspend root hub\n");
80 /* First stop any processing */
81 if (ohci
->hc_control
& OHCI_SCHED_ENABLES
) {
84 ohci
->hc_control
&= ~OHCI_SCHED_ENABLES
;
85 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
86 ohci
->hc_control
= ohci_readl (ohci
, &ohci
->regs
->control
);
87 ohci_writel (ohci
, OHCI_INTR_SF
, &ohci
->regs
->intrstatus
);
89 /* sched disables take effect on the next frame,
90 * then the last WDH could take 6+ msec
92 ohci_dbg (ohci
, "stopping schedules ...\n");
97 if (ohci_readl (ohci
, &ohci
->regs
->intrstatus
)
101 dl_done_list (ohci
, NULL
);
104 dl_done_list (ohci
, NULL
);
105 finish_unlinks (ohci
, ohci_frame_no(ohci
), NULL
);
106 ohci_writel (ohci
, ohci_readl (ohci
, &ohci
->regs
->intrstatus
),
107 &ohci
->regs
->intrstatus
);
109 /* maybe resume can wake root hub */
110 if (device_may_wakeup(&ohci_to_hcd(ohci
)->self
.root_hub
->dev
))
111 ohci
->hc_control
|= OHCI_CTRL_RWE
;
113 ohci
->hc_control
&= ~OHCI_CTRL_RWE
;
115 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
116 * which doesn't imply ports will first be individually suspended.
118 ohci
->hc_control
&= ~OHCI_CTRL_HCFS
;
119 ohci
->hc_control
|= OHCI_USB_SUSPEND
;
120 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
121 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
123 /* no resumes until devices finish suspending */
124 ohci
->next_statechange
= jiffies
+ msecs_to_jiffies (5);
127 /* external suspend vs self autosuspend ... same effect */
129 usb_hcd_suspend_root_hub(hcd
);
130 spin_unlock_irqrestore (&ohci
->lock
, flags
);
134 static inline struct ed
*find_head (struct ed
*ed
)
136 /* for bulk and control lists */
142 /* caller has locked the root hub */
143 static int ohci_bus_resume (struct usb_hcd
*hcd
)
145 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
147 int status
= -EINPROGRESS
;
150 if (time_before (jiffies
, ohci
->next_statechange
))
153 spin_lock_irqsave (&ohci
->lock
, flags
);
155 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
))) {
156 spin_unlock_irqrestore (&ohci
->lock
, flags
);
161 ohci
->hc_control
= ohci_readl (ohci
, &ohci
->regs
->control
);
163 if (ohci
->hc_control
& (OHCI_CTRL_IR
| OHCI_SCHED_ENABLES
)) {
164 /* this can happen after resuming a swsusp snapshot */
165 if (hcd
->state
== HC_STATE_RESUMING
) {
166 ohci_dbg (ohci
, "BIOS/SMM active, control %03x\n",
169 /* this happens when pmcore resumes HC then root */
171 ohci_dbg (ohci
, "duplicate resume\n");
174 } else switch (ohci
->hc_control
& OHCI_CTRL_HCFS
) {
175 case OHCI_USB_SUSPEND
:
176 ohci
->hc_control
&= ~(OHCI_CTRL_HCFS
|OHCI_SCHED_ENABLES
);
177 ohci
->hc_control
|= OHCI_USB_RESUME
;
178 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
179 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
180 ohci_dbg (ohci
, "resume root hub\n");
182 case OHCI_USB_RESUME
:
183 /* HCFS changes sometime after INTR_RD */
184 ohci_info (ohci
, "wakeup\n");
187 /* this can happen after resuming a swsusp snapshot */
188 ohci_dbg (ohci
, "snapshot resume? reinit\n");
191 default: /* RESET, we lost power */
192 ohci_dbg (ohci
, "lost power\n");
195 spin_unlock_irqrestore (&ohci
->lock
, flags
);
196 if (status
== -EBUSY
) {
197 (void) ohci_init (ohci
);
198 return ohci_restart (ohci
);
200 if (status
!= -EINPROGRESS
)
203 temp
= ohci
->num_ports
;
206 u32 stat
= ohci_readl (ohci
,
207 &ohci
->regs
->roothub
.portstatus
[temp
]);
209 /* force global, not selective, resume */
210 if (!(stat
& RH_PS_PSS
))
212 ohci_writel (ohci
, RH_PS_POCI
,
213 &ohci
->regs
->roothub
.portstatus
[temp
]);
216 /* Some controllers (lucent erratum) need extra-long delays */
217 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
219 temp
= ohci_readl (ohci
, &ohci
->regs
->control
);
220 temp
&= OHCI_CTRL_HCFS
;
221 if (temp
!= OHCI_USB_RESUME
) {
222 ohci_err (ohci
, "controller won't resume\n");
226 /* disable old schedule state, reinit from scratch */
227 ohci_writel (ohci
, 0, &ohci
->regs
->ed_controlhead
);
228 ohci_writel (ohci
, 0, &ohci
->regs
->ed_controlcurrent
);
229 ohci_writel (ohci
, 0, &ohci
->regs
->ed_bulkhead
);
230 ohci_writel (ohci
, 0, &ohci
->regs
->ed_bulkcurrent
);
231 ohci_writel (ohci
, 0, &ohci
->regs
->ed_periodcurrent
);
232 ohci_writel (ohci
, (u32
) ohci
->hcca_dma
, &ohci
->regs
->hcca
);
234 /* Sometimes PCI D3 suspend trashes frame timings ... */
235 periodic_reinit (ohci
);
237 /* interrupts might have been disabled */
238 ohci_writel (ohci
, OHCI_INTR_INIT
, &ohci
->regs
->intrenable
);
239 if (ohci
->ed_rm_list
)
240 ohci_writel (ohci
, OHCI_INTR_SF
, &ohci
->regs
->intrenable
);
241 ohci_writel (ohci
, ohci_readl (ohci
, &ohci
->regs
->intrstatus
),
242 &ohci
->regs
->intrstatus
);
244 /* Then re-enable operations */
245 ohci_writel (ohci
, OHCI_USB_OPER
, &ohci
->regs
->control
);
246 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
249 temp
= ohci
->hc_control
;
250 temp
&= OHCI_CTRL_RWC
;
251 temp
|= OHCI_CONTROL_INIT
| OHCI_USB_OPER
;
252 ohci
->hc_control
= temp
;
253 ohci_writel (ohci
, temp
, &ohci
->regs
->control
);
254 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
259 /* keep it alive for ~5x suspend + resume costs */
260 ohci
->next_statechange
= jiffies
+ msecs_to_jiffies (250);
262 /* maybe turn schedules back on */
265 if (!ohci
->ed_rm_list
) {
266 if (ohci
->ed_controltail
) {
268 find_head (ohci
->ed_controltail
)->dma
,
269 &ohci
->regs
->ed_controlhead
);
270 enables
|= OHCI_CTRL_CLE
;
273 if (ohci
->ed_bulktail
) {
274 ohci_writel (ohci
, find_head (ohci
->ed_bulktail
)->dma
,
275 &ohci
->regs
->ed_bulkhead
);
276 enables
|= OHCI_CTRL_BLE
;
280 if (hcd
->self
.bandwidth_isoc_reqs
|| hcd
->self
.bandwidth_int_reqs
)
281 enables
|= OHCI_CTRL_PLE
|OHCI_CTRL_IE
;
283 ohci_dbg (ohci
, "restarting schedules ... %08x\n", enables
);
284 ohci
->hc_control
|= enables
;
285 ohci_writel (ohci
, ohci
->hc_control
, &ohci
->regs
->control
);
287 ohci_writel (ohci
, temp
, &ohci
->regs
->cmdstatus
);
288 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
294 #endif /* CONFIG_PM */
296 /*-------------------------------------------------------------------------*/
298 /* build "status change" packet (one or two bytes) from HC registers */
301 ohci_hub_status_data (struct usb_hcd
*hcd
, char *buf
)
303 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
304 int i
, changed
= 0, length
= 1;
305 int can_suspend
= device_may_wakeup(&hcd
->self
.root_hub
->dev
);
308 spin_lock_irqsave (&ohci
->lock
, flags
);
310 /* handle autosuspended root: finish resuming before
311 * letting khubd or root hub timer see state changes.
313 if (unlikely((ohci
->hc_control
& OHCI_CTRL_HCFS
) != OHCI_USB_OPER
314 || !HC_IS_RUNNING(hcd
->state
))) {
319 /* undocumented erratum seen on at least rev D */
320 if ((ohci
->flags
& OHCI_QUIRK_AMD756
)
321 && (roothub_a (ohci
) & RH_A_NDP
) > MAX_ROOT_PORTS
) {
322 ohci_warn (ohci
, "bogus NDP, rereads as NDP=%d\n",
323 ohci_readl (ohci
, &ohci
->regs
->roothub
.a
) & RH_A_NDP
);
324 /* retry later; "should not happen" */
329 if (roothub_status (ohci
) & (RH_HS_LPSC
| RH_HS_OCIC
))
330 buf
[0] = changed
= 1;
333 if (ohci
->num_ports
> 7) {
338 /* look at each port */
339 for (i
= 0; i
< ohci
->num_ports
; i
++) {
340 u32 status
= roothub_portstatus (ohci
, i
);
342 if (status
& (RH_PS_CSC
| RH_PS_PESC
| RH_PS_PSSC
343 | RH_PS_OCIC
| RH_PS_PRSC
)) {
346 buf
[0] |= 1 << (i
+ 1);
348 buf
[1] |= 1 << (i
- 7);
352 /* can suspend if no ports are enabled; or if all all
353 * enabled ports are suspended AND remote wakeup is on.
355 if (!(status
& RH_PS_CCS
))
357 if ((status
& RH_PS_PSS
) && can_suspend
)
362 spin_unlock_irqrestore (&ohci
->lock
, flags
);
365 /* save power by suspending idle root hubs;
366 * INTR_RD wakes us when there's work
371 && ((OHCI_CTRL_HCFS
| OHCI_SCHED_ENABLES
)
374 && time_after (jiffies
, ohci
->next_statechange
)
375 && usb_trylock_device (hcd
->self
.root_hub
) == 0
377 ohci_vdbg (ohci
, "autosuspend\n");
378 (void) ohci_bus_suspend (hcd
);
379 usb_unlock_device (hcd
->self
.root_hub
);
383 return changed
? length
: 0;
386 /*-------------------------------------------------------------------------*/
389 ohci_hub_descriptor (
390 struct ohci_hcd
*ohci
,
391 struct usb_hub_descriptor
*desc
393 u32 rh
= roothub_a (ohci
);
396 desc
->bDescriptorType
= 0x29;
397 desc
->bPwrOn2PwrGood
= (rh
& RH_A_POTPGT
) >> 24;
398 desc
->bHubContrCurrent
= 0;
400 desc
->bNbrPorts
= ohci
->num_ports
;
401 temp
= 1 + (ohci
->num_ports
/ 8);
402 desc
->bDescLength
= 7 + 2 * temp
;
405 if (rh
& RH_A_NPS
) /* no power switching? */
407 if (rh
& RH_A_PSM
) /* per-port power switching? */
409 if (rh
& RH_A_NOCP
) /* no overcurrent reporting? */
411 else if (rh
& RH_A_OCPM
) /* per-port overcurrent reporting? */
413 desc
->wHubCharacteristics
= (__force __u16
)cpu_to_hc16(ohci
, temp
);
415 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
416 rh
= roothub_b (ohci
);
417 memset(desc
->bitmap
, 0xff, sizeof(desc
->bitmap
));
418 desc
->bitmap
[0] = rh
& RH_B_DR
;
419 if (ohci
->num_ports
> 7) {
420 desc
->bitmap
[1] = (rh
& RH_B_DR
) >> 8;
421 desc
->bitmap
[2] = 0xff;
423 desc
->bitmap
[1] = 0xff;
426 /*-------------------------------------------------------------------------*/
428 #ifdef CONFIG_USB_OTG
430 static int ohci_start_port_reset (struct usb_hcd
*hcd
, unsigned port
)
432 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
439 /* start port reset before HNP protocol times out */
440 status
= ohci_readl(ohci
, &ohci
->regs
->roothub
.portstatus
[port
]);
441 if (!(status
& RH_PS_CCS
))
444 /* khubd will finish the reset later */
445 ohci_writel(ohci
, RH_PS_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
449 static void start_hnp(struct ohci_hcd
*ohci
);
453 #define ohci_start_port_reset NULL
457 /*-------------------------------------------------------------------------*/
460 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
461 * not necessarily continuous ... to guard against resume signaling.
462 * The short timeout is safe for non-root hubs, and is backward-compatible
463 * with earlier Linux hosts.
465 #ifdef CONFIG_USB_SUSPEND
466 #define PORT_RESET_MSEC 50
468 #define PORT_RESET_MSEC 10
471 /* this timer value might be vendor-specific ... */
472 #define PORT_RESET_HW_MSEC 10
474 /* wrap-aware logic morphed from <linux/jiffies.h> */
475 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
477 /* called from some task, normally khubd */
478 static inline void root_port_reset (struct ohci_hcd
*ohci
, unsigned port
)
480 __hc32 __iomem
*portstat
= &ohci
->regs
->roothub
.portstatus
[port
];
482 u16 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
483 u16 reset_done
= now
+ PORT_RESET_MSEC
;
485 /* build a "continuous enough" reset signal, with up to
486 * 3msec gap between pulses. scheduler HZ==100 must work;
487 * this might need to be deadline-scheduled.
490 /* spin until any current reset finishes */
492 temp
= ohci_readl (ohci
, portstat
);
493 if (!(temp
& RH_PS_PRS
))
498 if (!(temp
& RH_PS_CCS
))
500 if (temp
& RH_PS_PRSC
)
501 ohci_writel (ohci
, RH_PS_PRSC
, portstat
);
503 /* start the next reset, sleep till it's probably done */
504 ohci_writel (ohci
, RH_PS_PRS
, portstat
);
505 msleep(PORT_RESET_HW_MSEC
);
506 now
= ohci_readl(ohci
, &ohci
->regs
->fmnumber
);
507 } while (tick_before(now
, reset_done
));
508 /* caller synchronizes using PRSC */
511 static int ohci_hub_control (
519 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
520 int ports
= hcd_to_bus (hcd
)->root_hub
->maxchild
;
524 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
)))
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 usb_hcd_resume_root_hub(hcd
);
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 */