2 * Copyright (c) 2001-2002 by David Brownell
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* this file is part of ehci-hcd.c */
21 /*-------------------------------------------------------------------------*/
24 * EHCI Root Hub ... the nonsharable stuff
26 * Registers don't need cpu_to_le32, that happens transparently
29 /*-------------------------------------------------------------------------*/
33 static int ehci_hub_suspend (struct usb_hcd
*hcd
)
35 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
38 if (time_before (jiffies
, ehci
->next_statechange
))
41 port
= HCS_N_PORTS (ehci
->hcs_params
);
42 spin_lock_irq (&ehci
->lock
);
44 /* stop schedules, clean any completed work */
45 if (HC_IS_RUNNING(hcd
->state
)) {
47 hcd
->state
= HC_STATE_QUIESCING
;
49 ehci
->command
= readl (&ehci
->regs
->command
);
51 ehci
->reclaim_ready
= 1;
52 ehci_work(ehci
, NULL
);
54 /* suspend any active/unsuspended ports, maybe allow wakeup */
56 u32 __iomem
*reg
= &ehci
->regs
->port_status
[port
];
60 if ((t1
& PORT_PE
) && !(t1
& PORT_OWNER
))
62 if (hcd
->remote_wakeup
)
63 t2
|= PORT_WKOC_E
|PORT_WKDISC_E
|PORT_WKCONN_E
;
65 t2
&= ~(PORT_WKOC_E
|PORT_WKDISC_E
|PORT_WKCONN_E
);
68 ehci_vdbg (ehci
, "port %d, %08x -> %08x\n",
74 /* turn off now-idle HC */
75 del_timer_sync (&ehci
->watchdog
);
77 hcd
->state
= HC_STATE_SUSPENDED
;
79 ehci
->next_statechange
= jiffies
+ msecs_to_jiffies(10);
80 spin_unlock_irq (&ehci
->lock
);
85 /* caller has locked the root hub, and should reset/reinit on error */
86 static int ehci_hub_resume (struct usb_hcd
*hcd
)
88 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
93 if (time_before (jiffies
, ehci
->next_statechange
))
95 spin_lock_irq (&ehci
->lock
);
97 /* re-init operational registers in case we lost power */
98 if (readl (&ehci
->regs
->intr_enable
) == 0) {
99 /* at least some APM implementations will try to deliver
100 * IRQs right away, so delay them until we're ready.
103 writel (0, &ehci
->regs
->segment
);
104 writel (ehci
->periodic_dma
, &ehci
->regs
->frame_list
);
105 writel ((u32
)ehci
->async
->qh_dma
, &ehci
->regs
->async_next
);
108 ehci_dbg(ehci
, "resume root hub%s\n",
109 intr_enable
? " after power loss" : "");
111 /* restore CMD_RUN, framelist size, and irq threshold */
112 writel (ehci
->command
, &ehci
->regs
->command
);
114 /* take ports out of suspend */
115 i
= HCS_N_PORTS (ehci
->hcs_params
);
117 temp
= readl (&ehci
->regs
->port_status
[i
]);
118 temp
&= ~(PORT_WKOC_E
|PORT_WKDISC_E
|PORT_WKCONN_E
);
119 if (temp
& PORT_SUSPEND
) {
120 ehci
->reset_done
[i
] = jiffies
+ msecs_to_jiffies (20);
123 writel (temp
, &ehci
->regs
->port_status
[i
]);
125 i
= HCS_N_PORTS (ehci
->hcs_params
);
128 temp
= readl (&ehci
->regs
->port_status
[i
]);
129 if ((temp
& PORT_SUSPEND
) == 0)
131 temp
&= ~PORT_RESUME
;
132 writel (temp
, &ehci
->regs
->port_status
[i
]);
133 ehci_vdbg (ehci
, "resumed port %d\n", i
+ 1);
135 (void) readl (&ehci
->regs
->command
);
137 /* maybe re-activate the schedule(s) */
139 if (ehci
->async
->qh_next
.qh
)
141 if (ehci
->periodic_sched
)
144 ehci
->command
|= temp
;
145 writel (ehci
->command
, &ehci
->regs
->command
);
148 ehci
->next_statechange
= jiffies
+ msecs_to_jiffies(5);
149 hcd
->state
= HC_STATE_RUNNING
;
151 /* Now we can safely re-enable irqs */
153 writel (INTR_MASK
, &ehci
->regs
->intr_enable
);
155 spin_unlock_irq (&ehci
->lock
);
161 #define ehci_hub_suspend NULL
162 #define ehci_hub_resume NULL
164 #endif /* CONFIG_PM */
166 /*-------------------------------------------------------------------------*/
168 static int check_reset_complete (
169 struct ehci_hcd
*ehci
,
173 if (!(port_status
& PORT_CONNECT
)) {
174 ehci
->reset_done
[index
] = 0;
178 /* if reset finished and it's still not enabled -- handoff */
179 if (!(port_status
& PORT_PE
)) {
181 /* with integrated TT, there's nobody to hand it to! */
182 if (ehci_is_TDI(ehci
)) {
184 "Failed to enable port %d on root hub TT\n",
189 ehci_dbg (ehci
, "port %d full speed --> companion\n",
192 // what happens if HCS_N_CC(params) == 0 ?
193 port_status
|= PORT_OWNER
;
194 writel (port_status
, &ehci
->regs
->port_status
[index
]);
197 ehci_dbg (ehci
, "port %d high speed\n", index
+ 1);
202 /*-------------------------------------------------------------------------*/
205 /* build "status change" packet (one or two bytes) from HC registers */
208 ehci_hub_status_data (struct usb_hcd
*hcd
, char *buf
)
210 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
211 u32 temp
, status
= 0;
212 int ports
, i
, retval
= 1;
215 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
216 if (!HC_IS_RUNNING(hcd
->state
))
219 /* init status to no-changes */
221 ports
= HCS_N_PORTS (ehci
->hcs_params
);
227 /* no hub change reports (bit 0) for now (power, ...) */
229 /* port N changes (bit N)? */
230 spin_lock_irqsave (&ehci
->lock
, flags
);
231 for (i
= 0; i
< ports
; i
++) {
232 temp
= readl (&ehci
->regs
->port_status
[i
]);
233 if (temp
& PORT_OWNER
) {
234 /* don't report this in GetPortStatus */
235 if (temp
& PORT_CSC
) {
237 writel (temp
, &ehci
->regs
->port_status
[i
]);
241 if (!(temp
& PORT_CONNECT
))
242 ehci
->reset_done
[i
] = 0;
243 if ((temp
& (PORT_CSC
| PORT_PEC
| PORT_OCC
)) != 0
244 // PORT_STAT_C_SUSPEND?
245 || ((temp
& PORT_RESUME
) != 0
246 && time_after (jiffies
,
247 ehci
->reset_done
[i
]))) {
249 buf
[0] |= 1 << (i
+ 1);
251 buf
[1] |= 1 << (i
- 7);
255 /* FIXME autosuspend idle root hubs */
256 spin_unlock_irqrestore (&ehci
->lock
, flags
);
257 return status
? retval
: 0;
260 /*-------------------------------------------------------------------------*/
263 ehci_hub_descriptor (
264 struct ehci_hcd
*ehci
,
265 struct usb_hub_descriptor
*desc
267 int ports
= HCS_N_PORTS (ehci
->hcs_params
);
270 desc
->bDescriptorType
= 0x29;
271 desc
->bPwrOn2PwrGood
= 10; /* ehci 1.0, 2.3.9 says 20ms max */
272 desc
->bHubContrCurrent
= 0;
274 desc
->bNbrPorts
= ports
;
275 temp
= 1 + (ports
/ 8);
276 desc
->bDescLength
= 7 + 2 * temp
;
278 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
279 memset (&desc
->bitmap
[0], 0, temp
);
280 memset (&desc
->bitmap
[temp
], 0xff, temp
);
282 temp
= 0x0008; /* per-port overcurrent reporting */
283 if (HCS_PPC (ehci
->hcs_params
))
284 temp
|= 0x0001; /* per-port power control */
286 temp
|= 0x0002; /* no power switching */
288 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
289 if (HCS_INDICATOR (ehci
->hcs_params
))
290 temp
|= 0x0080; /* per-port indicators (LEDs) */
292 desc
->wHubCharacteristics
= (__force __u16
)cpu_to_le16 (temp
);
295 /*-------------------------------------------------------------------------*/
297 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
299 static int ehci_hub_control (
307 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
308 int ports
= HCS_N_PORTS (ehci
->hcs_params
);
314 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
315 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
316 * (track current state ourselves) ... blink for diagnostics,
317 * power, "this is the one", etc. EHCI spec supports this.
320 spin_lock_irqsave (&ehci
->lock
, flags
);
322 case ClearHubFeature
:
324 case C_HUB_LOCAL_POWER
:
325 case C_HUB_OVER_CURRENT
:
326 /* no hub-wide feature/status flags */
332 case ClearPortFeature
:
333 if (!wIndex
|| wIndex
> ports
)
336 temp
= readl (&ehci
->regs
->port_status
[wIndex
]);
337 if (temp
& PORT_OWNER
)
341 case USB_PORT_FEAT_ENABLE
:
342 writel (temp
& ~PORT_PE
,
343 &ehci
->regs
->port_status
[wIndex
]);
345 case USB_PORT_FEAT_C_ENABLE
:
346 writel (temp
| PORT_PEC
,
347 &ehci
->regs
->port_status
[wIndex
]);
349 case USB_PORT_FEAT_SUSPEND
:
350 if (temp
& PORT_RESET
)
352 if (temp
& PORT_SUSPEND
) {
353 if ((temp
& PORT_PE
) == 0)
355 /* resume signaling for 20 msec */
356 writel ((temp
& ~PORT_WAKE_BITS
) | PORT_RESUME
,
357 &ehci
->regs
->port_status
[wIndex
]);
358 ehci
->reset_done
[wIndex
] = jiffies
359 + msecs_to_jiffies (20);
362 case USB_PORT_FEAT_C_SUSPEND
:
363 /* we auto-clear this feature */
365 case USB_PORT_FEAT_POWER
:
366 if (HCS_PPC (ehci
->hcs_params
))
367 writel (temp
& ~PORT_POWER
,
368 &ehci
->regs
->port_status
[wIndex
]);
370 case USB_PORT_FEAT_C_CONNECTION
:
371 writel (temp
| PORT_CSC
,
372 &ehci
->regs
->port_status
[wIndex
]);
374 case USB_PORT_FEAT_C_OVER_CURRENT
:
375 writel (temp
| PORT_OCC
,
376 &ehci
->regs
->port_status
[wIndex
]);
378 case USB_PORT_FEAT_C_RESET
:
379 /* GetPortStatus clears reset */
384 readl (&ehci
->regs
->command
); /* unblock posted write */
386 case GetHubDescriptor
:
387 ehci_hub_descriptor (ehci
, (struct usb_hub_descriptor
*)
391 /* no hub-wide feature/status flags */
393 //cpu_to_le32s ((u32 *) buf);
396 if (!wIndex
|| wIndex
> ports
)
400 temp
= readl (&ehci
->regs
->port_status
[wIndex
]);
404 status
|= 1 << USB_PORT_FEAT_C_CONNECTION
;
406 status
|= 1 << USB_PORT_FEAT_C_ENABLE
;
408 status
|= 1 << USB_PORT_FEAT_C_OVER_CURRENT
;
410 /* whoever resumes must GetPortStatus to complete it!! */
411 if ((temp
& PORT_RESUME
)
412 && time_after (jiffies
,
413 ehci
->reset_done
[wIndex
])) {
414 status
|= 1 << USB_PORT_FEAT_C_SUSPEND
;
415 ehci
->reset_done
[wIndex
] = 0;
417 /* stop resume signaling */
418 temp
= readl (&ehci
->regs
->port_status
[wIndex
]);
419 writel (temp
& ~PORT_RESUME
,
420 &ehci
->regs
->port_status
[wIndex
]);
422 &ehci
->regs
->port_status
[wIndex
],
423 PORT_RESUME
, 0, 2000 /* 2msec */);
425 ehci_err (ehci
, "port %d resume error %d\n",
429 temp
&= ~(PORT_SUSPEND
|PORT_RESUME
|(3<<10));
432 /* whoever resets must GetPortStatus to complete it!! */
433 if ((temp
& PORT_RESET
)
434 && time_after (jiffies
,
435 ehci
->reset_done
[wIndex
])) {
436 status
|= 1 << USB_PORT_FEAT_C_RESET
;
437 ehci
->reset_done
[wIndex
] = 0;
439 /* force reset to complete */
440 writel (temp
& ~PORT_RESET
,
441 &ehci
->regs
->port_status
[wIndex
]);
443 &ehci
->regs
->port_status
[wIndex
],
446 ehci_err (ehci
, "port %d reset error %d\n",
451 /* see what we found out */
452 temp
= check_reset_complete (ehci
, wIndex
,
453 readl (&ehci
->regs
->port_status
[wIndex
]));
456 // don't show wPortStatus if it's owned by a companion hc
457 if (!(temp
& PORT_OWNER
)) {
458 if (temp
& PORT_CONNECT
) {
459 status
|= 1 << USB_PORT_FEAT_CONNECTION
;
460 // status may be from integrated TT
461 status
|= ehci_port_speed(ehci
, temp
);
464 status
|= 1 << USB_PORT_FEAT_ENABLE
;
465 if (temp
& (PORT_SUSPEND
|PORT_RESUME
))
466 status
|= 1 << USB_PORT_FEAT_SUSPEND
;
468 status
|= 1 << USB_PORT_FEAT_OVER_CURRENT
;
469 if (temp
& PORT_RESET
)
470 status
|= 1 << USB_PORT_FEAT_RESET
;
471 if (temp
& PORT_POWER
)
472 status
|= 1 << USB_PORT_FEAT_POWER
;
475 #ifndef EHCI_VERBOSE_DEBUG
476 if (status
& ~0xffff) /* only if wPortChange is interesting */
478 dbg_port (ehci
, "GetStatus", wIndex
+ 1, temp
);
479 // we "know" this alignment is good, caller used kmalloc()...
480 *((__le32
*) buf
) = cpu_to_le32 (status
);
484 case C_HUB_LOCAL_POWER
:
485 case C_HUB_OVER_CURRENT
:
486 /* no hub-wide feature/status flags */
493 if (!wIndex
|| wIndex
> ports
)
496 temp
= readl (&ehci
->regs
->port_status
[wIndex
]);
497 if (temp
& PORT_OWNER
)
501 case USB_PORT_FEAT_SUSPEND
:
502 if ((temp
& PORT_PE
) == 0
503 || (temp
& PORT_RESET
) != 0)
505 if (hcd
->remote_wakeup
)
506 temp
|= PORT_WAKE_BITS
;
507 writel (temp
| PORT_SUSPEND
,
508 &ehci
->regs
->port_status
[wIndex
]);
510 case USB_PORT_FEAT_POWER
:
511 if (HCS_PPC (ehci
->hcs_params
))
512 writel (temp
| PORT_POWER
,
513 &ehci
->regs
->port_status
[wIndex
]);
515 case USB_PORT_FEAT_RESET
:
516 if (temp
& PORT_RESUME
)
518 /* line status bits may report this as low speed,
519 * which can be fine if this root hub has a
520 * transaction translator built in.
522 if ((temp
& (PORT_PE
|PORT_CONNECT
)) == PORT_CONNECT
523 && !ehci_is_TDI(ehci
)
524 && PORT_USB11 (temp
)) {
526 "port %d low speed --> companion\n",
530 ehci_vdbg (ehci
, "port %d reset\n", wIndex
+ 1);
535 * caller must wait, then call GetPortStatus
536 * usb 2.0 spec says 50 ms resets on root
538 ehci
->reset_done
[wIndex
] = jiffies
539 + msecs_to_jiffies (50);
541 writel (temp
, &ehci
->regs
->port_status
[wIndex
]);
546 readl (&ehci
->regs
->command
); /* unblock posted writes */
551 /* "stall" on error */
554 spin_unlock_irqrestore (&ehci
->lock
, flags
);