USB: EHCI: remove unused Link Power Management code
[deliverable/linux.git] / drivers / usb / host / ehci-hub.c
CommitLineData
1da177e4 1/*
d49d4317 2 * Copyright (C) 2001-2004 by David Brownell
53bd6a60 3 *
1da177e4
LT
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.
8 *
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
12 * for more details.
13 *
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.
17 */
18
19/* this file is part of ehci-hcd.c */
20
21/*-------------------------------------------------------------------------*/
22
23/*
24 * EHCI Root Hub ... the nonsharable stuff
25 *
26 * Registers don't need cpu_to_le32, that happens transparently
27 */
28
29/*-------------------------------------------------------------------------*/
83722bc9 30#include <linux/usb/otg.h>
1da177e4 31
58a97ffe
AS
32#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33
aff6d18f
AS
34#ifdef CONFIG_PM
35
383975d7
AS
36static int ehci_hub_control(
37 struct usb_hcd *hcd,
38 u16 typeReq,
39 u16 wValue,
40 u16 wIndex,
41 char *buf,
42 u16 wLength
43);
44
45/* After a power loss, ports that were owned by the companion must be
46 * reset so that the companion can still own them.
47 */
48static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
49{
50 u32 __iomem *reg;
51 u32 status;
52 int port;
53 __le32 buf;
54 struct usb_hcd *hcd = ehci_to_hcd(ehci);
55
56 if (!ehci->owned_ports)
57 return;
58
59 /* Give the connections some time to appear */
60 msleep(20);
61
c4f34764 62 spin_lock_irq(&ehci->lock);
383975d7
AS
63 port = HCS_N_PORTS(ehci->hcs_params);
64 while (port--) {
65 if (test_bit(port, &ehci->owned_ports)) {
66 reg = &ehci->regs->port_status[port];
3c519b84 67 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
383975d7
AS
68
69 /* Port already owned by companion? */
70 if (status & PORT_OWNER)
71 clear_bit(port, &ehci->owned_ports);
3c519b84
AS
72 else if (test_bit(port, &ehci->companion_ports))
73 ehci_writel(ehci, status & ~PORT_PE, reg);
c4f34764
AS
74 else {
75 spin_unlock_irq(&ehci->lock);
383975d7
AS
76 ehci_hub_control(hcd, SetPortFeature,
77 USB_PORT_FEAT_RESET, port + 1,
78 NULL, 0);
c4f34764
AS
79 spin_lock_irq(&ehci->lock);
80 }
383975d7
AS
81 }
82 }
c4f34764 83 spin_unlock_irq(&ehci->lock);
383975d7
AS
84
85 if (!ehci->owned_ports)
86 return;
87 msleep(90); /* Wait for resets to complete */
88
c4f34764 89 spin_lock_irq(&ehci->lock);
383975d7
AS
90 port = HCS_N_PORTS(ehci->hcs_params);
91 while (port--) {
92 if (test_bit(port, &ehci->owned_ports)) {
c4f34764 93 spin_unlock_irq(&ehci->lock);
383975d7
AS
94 ehci_hub_control(hcd, GetPortStatus,
95 0, port + 1,
96 (char *) &buf, sizeof(buf));
c4f34764 97 spin_lock_irq(&ehci->lock);
383975d7
AS
98
99 /* The companion should now own the port,
100 * but if something went wrong the port must not
101 * remain enabled.
102 */
103 reg = &ehci->regs->port_status[port];
104 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
105 if (status & PORT_OWNER)
106 ehci_writel(ehci, status | PORT_CSC, reg);
107 else {
108 ehci_dbg(ehci, "failed handover port %d: %x\n",
109 port + 1, status);
110 ehci_writel(ehci, status & ~PORT_PE, reg);
111 }
112 }
113 }
114
115 ehci->owned_ports = 0;
c4f34764 116 spin_unlock_irq(&ehci->lock);
383975d7
AS
117}
118
c5cf9212 119static int ehci_port_change(struct ehci_hcd *ehci)
294d95f2
MG
120{
121 int i = HCS_N_PORTS(ehci->hcs_params);
122
123 /* First check if the controller indicates a change event */
124
125 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
126 return 1;
127
128 /*
129 * Not all controllers appear to update this while going from D3 to D0,
130 * so check the individual port status registers as well
131 */
132
133 while (i--)
134 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
135 return 1;
136
137 return 0;
138}
139
c5cf9212 140static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
4147200d 141 bool suspending, bool do_wakeup)
16032c4f
AS
142{
143 int port;
144 u32 temp;
145
146 /* If remote wakeup is enabled for the root hub but disabled
147 * for the controller, we must adjust all the port wakeup flags
148 * when the controller is suspended or resumed. In all other
149 * cases they don't need to be changed.
150 */
4147200d 151 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
16032c4f
AS
152 return;
153
c4f34764 154 spin_lock_irq(&ehci->lock);
148fc55f 155
16032c4f
AS
156 /* clear phy low-power mode before changing wakeup flags */
157 if (ehci->has_hostpc) {
158 port = HCS_N_PORTS(ehci->hcs_params);
159 while (port--) {
a46af4eb 160 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f 161
16032c4f
AS
162 temp = ehci_readl(ehci, hostpc_reg);
163 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
164 }
c4f34764 165 spin_unlock_irq(&ehci->lock);
16032c4f 166 msleep(5);
c4f34764 167 spin_lock_irq(&ehci->lock);
16032c4f
AS
168 }
169
170 port = HCS_N_PORTS(ehci->hcs_params);
171 while (port--) {
172 u32 __iomem *reg = &ehci->regs->port_status[port];
173 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
174 u32 t2 = t1 & ~PORT_WAKE_BITS;
175
176 /* If we are suspending the controller, clear the flags.
177 * If we are resuming the controller, set the wakeup flags.
178 */
179 if (!suspending) {
180 if (t1 & PORT_CONNECT)
181 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
182 else
183 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
184 }
185 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
186 port + 1, t1, t2);
187 ehci_writel(ehci, t2, reg);
188 }
189
190 /* enter phy low-power mode again */
191 if (ehci->has_hostpc) {
192 port = HCS_N_PORTS(ehci->hcs_params);
193 while (port--) {
a46af4eb 194 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f 195
16032c4f
AS
196 temp = ehci_readl(ehci, hostpc_reg);
197 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
198 }
199 }
ee0b9be8
AS
200
201 /* Does the root hub have a port wakeup pending? */
294d95f2 202 if (!suspending && ehci_port_change(ehci))
ee0b9be8 203 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
148fc55f 204
c4f34764 205 spin_unlock_irq(&ehci->lock);
16032c4f
AS
206}
207
0c0382e3 208static int ehci_bus_suspend (struct usb_hcd *hcd)
1da177e4
LT
209{
210 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
211 int port;
8c03356a 212 int mask;
16032c4f 213 int changed;
1da177e4 214
8c774fe8
AS
215 ehci_dbg(ehci, "suspend root hub\n");
216
1da177e4
LT
217 if (time_before (jiffies, ehci->next_statechange))
218 msleep(5);
219
c4f34764
AS
220 /* stop the schedules */
221 ehci_quiesce(ehci);
222
1da177e4 223 spin_lock_irq (&ehci->lock);
43fe3a99
AS
224 if (ehci->rh_state < EHCI_RH_RUNNING)
225 goto done;
1da177e4 226
cec3a53c
AS
227 /* Once the controller is stopped, port resumes that are already
228 * in progress won't complete. Hence if remote wakeup is enabled
229 * for the root hub and any ports are in the middle of a resume or
230 * remote wakeup, we must fail the suspend.
231 */
232 if (hcd->self.root_hub->do_remote_wakeup) {
a448e4dc
AS
233 if (ehci->resuming_ports) {
234 spin_unlock_irq(&ehci->lock);
235 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
236 return -EBUSY;
cec3a53c
AS
237 }
238 }
239
8c03356a
AS
240 /* Unlike other USB host controller types, EHCI doesn't have
241 * any notion of "global" or bus-wide suspend. The driver has
242 * to manually suspend all the active unsuspended ports, and
243 * then manually resume them in the bus_resume() routine.
244 */
245 ehci->bus_suspended = 0;
383975d7 246 ehci->owned_ports = 0;
16032c4f 247 changed = 0;
cec3a53c 248 port = HCS_N_PORTS(ehci->hcs_params);
1da177e4
LT
249 while (port--) {
250 u32 __iomem *reg = &ehci->regs->port_status [port];
083522d7 251 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
16032c4f 252 u32 t2 = t1 & ~PORT_WAKE_BITS;
1da177e4 253
8c03356a 254 /* keep track of which ports we suspend */
383975d7
AS
255 if (t1 & PORT_OWNER)
256 set_bit(port, &ehci->owned_ports);
257 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
1da177e4 258 t2 |= PORT_SUSPEND;
8c03356a
AS
259 set_bit(port, &ehci->bus_suspended);
260 }
261
16032c4f 262 /* enable remote wakeup on all ports, if told to do so */
331ac6b2
AD
263 if (hcd->self.root_hub->do_remote_wakeup) {
264 /* only enable appropriate wake bits, otherwise the
265 * hardware can not go phy low power mode. If a race
266 * condition happens here(connection change during bits
267 * set), the port change detection will finally fix it.
268 */
16032c4f 269 if (t1 & PORT_CONNECT)
331ac6b2 270 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
16032c4f 271 else
331ac6b2 272 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
16032c4f 273 }
1da177e4
LT
274
275 if (t1 != t2) {
276 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
277 port + 1, t1, t2);
083522d7 278 ehci_writel(ehci, t2, reg);
16032c4f
AS
279 changed = 1;
280 }
281 }
331ac6b2 282
16032c4f
AS
283 if (changed && ehci->has_hostpc) {
284 spin_unlock_irq(&ehci->lock);
285 msleep(5); /* 5 ms for HCD to enter low-power mode */
286 spin_lock_irq(&ehci->lock);
287
288 port = HCS_N_PORTS(ehci->hcs_params);
289 while (port--) {
a46af4eb 290 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
16032c4f
AS
291 u32 t3;
292
16032c4f
AS
293 t3 = ehci_readl(ehci, hostpc_reg);
294 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
295 t3 = ehci_readl(ehci, hostpc_reg);
296 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
331ac6b2
AD
297 port, (t3 & HOSTPC_PHCD) ?
298 "succeeded" : "failed");
1da177e4
LT
299 }
300 }
c4f34764 301 spin_unlock_irq(&ehci->lock);
1da177e4 302
cd930c93
AS
303 /* Apparently some devices need a >= 1-uframe delay here */
304 if (ehci->bus_suspended)
305 udelay(150);
306
1da177e4
LT
307 /* turn off now-idle HC */
308 ehci_halt (ehci);
c4f34764
AS
309
310 spin_lock_irq(&ehci->lock);
43fe3a99
AS
311 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
312 ehci_handle_controller_death(ehci);
313 if (ehci->rh_state != EHCI_RH_RUNNING)
314 goto done;
e8799906 315 ehci->rh_state = EHCI_RH_SUSPENDED;
1da177e4 316
3c273a05 317 end_unlink_async(ehci);
32830f20 318 unlink_empty_async(ehci);
df202255 319 ehci_handle_intr_unlinks(ehci);
55934eb3 320 end_free_itds(ehci);
cdc647a9 321
8c03356a
AS
322 /* allow remote wakeup */
323 mask = INTR_MASK;
58a97ffe 324 if (!hcd->self.root_hub->do_remote_wakeup)
8c03356a 325 mask &= ~STS_PCD;
083522d7
BH
326 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
327 ehci_readl(ehci, &ehci->regs->intr_enable);
8c03356a 328
43fe3a99 329 done:
1da177e4 330 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
d58b4bcc
AS
331 ehci->enabled_hrtimer_events = 0;
332 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
1da177e4 333 spin_unlock_irq (&ehci->lock);
015798b2 334
d58b4bcc 335 hrtimer_cancel(&ehci->hrtimer);
1da177e4
LT
336 return 0;
337}
338
339
340/* caller has locked the root hub, and should reset/reinit on error */
0c0382e3 341static int ehci_bus_resume (struct usb_hcd *hcd)
1da177e4
LT
342{
343 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
344 u32 temp;
383975d7 345 u32 power_okay;
1da177e4 346 int i;
d0f2fb25 347 unsigned long resume_needed = 0;
1da177e4
LT
348
349 if (time_before (jiffies, ehci->next_statechange))
350 msleep(5);
351 spin_lock_irq (&ehci->lock);
43fe3a99
AS
352 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
353 goto shutdown;
1da177e4 354
ad45f1dc 355 if (unlikely(ehci->debug)) {
9fa5780b 356 if (!dbgp_reset_prep(hcd))
ad45f1dc
JW
357 ehci->debug = NULL;
358 else
9fa5780b 359 dbgp_external_startup(hcd);
ad45f1dc
JW
360 }
361
f03c17fc
DB
362 /* Ideally and we've got a real resume here, and no port's power
363 * was lost. (For PCI, that means Vaux was maintained.) But we
364 * could instead be restoring a swsusp snapshot -- so that BIOS was
365 * the last user of the controller, not reset/pm hardware keeping
366 * state we gave to it.
367 */
383975d7
AS
368 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
369 ehci_dbg(ehci, "resume root hub%s\n",
370 power_okay ? "" : " after power loss");
f03c17fc 371
8c03356a
AS
372 /* at least some APM implementations will try to deliver
373 * IRQs right away, so delay them until we're ready.
374 */
083522d7 375 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
8c03356a
AS
376
377 /* re-init operational registers */
083522d7
BH
378 ehci_writel(ehci, 0, &ehci->regs->segment);
379 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
380 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1da177e4
LT
381
382 /* restore CMD_RUN, framelist size, and irq threshold */
3d9545cc 383 ehci->command |= CMD_RUN;
083522d7 384 ehci_writel(ehci, ehci->command, &ehci->regs->command);
e8799906 385 ehci->rh_state = EHCI_RH_RUNNING;
1da177e4 386
6273f181
PC
387 /*
388 * According to Bugzilla #8190, the port status for some controllers
389 * will be wrong without a delay. At their wrong status, the port
390 * is enabled, but not suspended neither resumed.
391 */
392 i = HCS_N_PORTS(ehci->hcs_params);
393 while (i--) {
394 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
395 if ((temp & PORT_PE) &&
396 !(temp & (PORT_SUSPEND | PORT_RESUME))) {
397 ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
398 spin_unlock_irq(&ehci->lock);
399 msleep(8);
400 spin_lock_irq(&ehci->lock);
401 break;
402 }
403 }
404
43fe3a99
AS
405 if (ehci->shutdown)
406 goto shutdown;
e198a314 407
16032c4f
AS
408 /* clear phy low-power mode before resume */
409 if (ehci->bus_suspended && ehci->has_hostpc) {
410 i = HCS_N_PORTS(ehci->hcs_params);
411 while (i--) {
412 if (test_bit(i, &ehci->bus_suspended)) {
a46af4eb
AS
413 u32 __iomem *hostpc_reg =
414 &ehci->regs->hostpc[i];
16032c4f 415
16032c4f
AS
416 temp = ehci_readl(ehci, hostpc_reg);
417 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
418 hostpc_reg);
419 }
420 }
421 spin_unlock_irq(&ehci->lock);
422 msleep(5);
423 spin_lock_irq(&ehci->lock);
43fe3a99
AS
424 if (ehci->shutdown)
425 goto shutdown;
16032c4f
AS
426 }
427
8c03356a 428 /* manually resume the ports we suspended during bus_suspend() */
1da177e4
LT
429 i = HCS_N_PORTS (ehci->hcs_params);
430 while (i--) {
083522d7 431 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
58a97ffe 432 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
8c03356a 433 if (test_bit(i, &ehci->bus_suspended) &&
3a4e72cb 434 (temp & PORT_SUSPEND)) {
1da177e4 435 temp |= PORT_RESUME;
d0f2fb25 436 set_bit(i, &resume_needed);
3a4e72cb 437 }
083522d7 438 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
1da177e4 439 }
3a4e72cb
VP
440
441 /* msleep for 20ms only if code is trying to resume port */
442 if (resume_needed) {
443 spin_unlock_irq(&ehci->lock);
444 msleep(20);
445 spin_lock_irq(&ehci->lock);
43fe3a99
AS
446 if (ehci->shutdown)
447 goto shutdown;
3a4e72cb
VP
448 }
449
1da177e4 450 i = HCS_N_PORTS (ehci->hcs_params);
1da177e4 451 while (i--) {
083522d7 452 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
d0f2fb25 453 if (test_bit(i, &resume_needed)) {
8c03356a 454 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
083522d7 455 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
8c03356a
AS
456 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
457 }
1da177e4 458 }
1da177e4
LT
459
460 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
c4f34764
AS
461 spin_unlock_irq(&ehci->lock);
462
463 ehci_handover_companion_ports(ehci);
1da177e4
LT
464
465 /* Now we can safely re-enable irqs */
43fe3a99
AS
466 spin_lock_irq(&ehci->lock);
467 if (ehci->shutdown)
468 goto shutdown;
083522d7 469 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
15be105b 470 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
43fe3a99 471 spin_unlock_irq(&ehci->lock);
1da177e4 472
1da177e4 473 return 0;
43fe3a99
AS
474
475 shutdown:
476 spin_unlock_irq(&ehci->lock);
477 return -ESHUTDOWN;
1da177e4
LT
478}
479
480#else
481
0c0382e3
AS
482#define ehci_bus_suspend NULL
483#define ehci_bus_resume NULL
1da177e4
LT
484
485#endif /* CONFIG_PM */
486
57e06c11
AS
487/*-------------------------------------------------------------------------*/
488
57e06c11 489/*
90da096e 490 * Sets the owner of a port
57e06c11 491 */
90da096e 492static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
57e06c11 493{
57e06c11
AS
494 u32 __iomem *status_reg;
495 u32 port_status;
90da096e 496 int try;
57e06c11 497
90da096e 498 status_reg = &ehci->regs->port_status[portnum];
57e06c11
AS
499
500 /*
501 * The controller won't set the OWNER bit if the port is
502 * enabled, so this loop will sometimes require at least two
503 * iterations: one to disable the port and one to set OWNER.
504 */
57e06c11
AS
505 for (try = 4; try > 0; --try) {
506 spin_lock_irq(&ehci->lock);
507 port_status = ehci_readl(ehci, status_reg);
508 if ((port_status & PORT_OWNER) == new_owner
509 || (port_status & (PORT_OWNER | PORT_CONNECT))
510 == 0)
511 try = 0;
512 else {
513 port_status ^= PORT_OWNER;
514 port_status &= ~(PORT_PE | PORT_RWC_BITS);
515 ehci_writel(ehci, port_status, status_reg);
516 }
517 spin_unlock_irq(&ehci->lock);
518 if (try > 1)
519 msleep(5);
520 }
90da096e
BR
521}
522
1da177e4
LT
523/*-------------------------------------------------------------------------*/
524
525static int check_reset_complete (
526 struct ehci_hcd *ehci,
527 int index,
e6316565 528 u32 __iomem *status_reg,
1da177e4
LT
529 int port_status
530) {
cd4cdc93 531 if (!(port_status & PORT_CONNECT))
1da177e4 532 return port_status;
1da177e4
LT
533
534 /* if reset finished and it's still not enabled -- handoff */
535 if (!(port_status & PORT_PE)) {
536
537 /* with integrated TT, there's nobody to hand it to! */
538 if (ehci_is_TDI(ehci)) {
539 ehci_dbg (ehci,
540 "Failed to enable port %d on root hub TT\n",
541 index+1);
542 return port_status;
543 }
544
545 ehci_dbg (ehci, "port %d full speed --> companion\n",
546 index + 1);
547
548 // what happens if HCS_N_CC(params) == 0 ?
549 port_status |= PORT_OWNER;
10f6524a 550 port_status &= ~PORT_RWC_BITS;
e6316565 551 ehci_writel(ehci, port_status, status_reg);
1da177e4 552
796bcae7
VB
553 /* ensure 440EPX ohci controller state is operational */
554 if (ehci->has_amcc_usb23)
555 set_ohci_hcfs(ehci, 1);
556 } else {
0ca7eb23
PC
557 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
558 index + 1);
796bcae7
VB
559 /* ensure 440EPx ohci controller state is suspended */
560 if (ehci->has_amcc_usb23)
561 set_ohci_hcfs(ehci, 0);
562 }
1da177e4
LT
563
564 return port_status;
565}
566
567/*-------------------------------------------------------------------------*/
568
569
570/* build "status change" packet (one or two bytes) from HC registers */
571
572static int
573ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
574{
575 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
a448e4dc 576 u32 temp, status;
93f1a47c 577 u32 mask;
1da177e4
LT
578 int ports, i, retval = 1;
579 unsigned long flags;
5a9cdf33 580 u32 ppcd = 0;
1da177e4 581
1da177e4
LT
582 /* init status to no-changes */
583 buf [0] = 0;
584 ports = HCS_N_PORTS (ehci->hcs_params);
585 if (ports > 7) {
586 buf [1] = 0;
587 retval++;
588 }
53bd6a60 589
a448e4dc
AS
590 /* Inform the core about resumes-in-progress by returning
591 * a non-zero value even if there are no status changes.
592 */
593 status = ehci->resuming_ports;
594
93f1a47c
DB
595 /* Some boards (mostly VIA?) report bogus overcurrent indications,
596 * causing massive log spam unless we completely ignore them. It
3a4fa0a2 597 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
93f1a47c
DB
598 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
599 * PORT_POWER; that's surprising, but maybe within-spec.
600 */
601 if (!ignore_oc)
602 mask = PORT_CSC | PORT_PEC | PORT_OCC;
603 else
604 mask = PORT_CSC | PORT_PEC;
605 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
606
1da177e4
LT
607 /* no hub change reports (bit 0) for now (power, ...) */
608
609 /* port N changes (bit N)? */
610 spin_lock_irqsave (&ehci->lock, flags);
5a9cdf33
AD
611
612 /* get per-port change detect bits */
613 if (ehci->has_ppcd)
614 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
615
1da177e4 616 for (i = 0; i < ports; i++) {
5a9cdf33
AD
617 /* leverage per-port change bits feature */
618 if (ehci->has_ppcd && !(ppcd & (1 << i)))
619 continue;
083522d7 620 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
625b5c9a
AS
621
622 /*
623 * Return status information even for ports with OWNER set.
624 * Otherwise khubd wouldn't see the disconnect event when a
625 * high-speed device is switched over to the companion
626 * controller by the user.
627 */
628
eafe5b99
AS
629 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
630 || (ehci->reset_done[i] && time_after_eq(
631 jiffies, ehci->reset_done[i]))) {
1da177e4
LT
632 if (i < 7)
633 buf [0] |= 1 << (i + 1);
634 else
635 buf [1] |= 1 << (i - 7);
636 status = STS_PCD;
637 }
638 }
639 /* FIXME autosuspend idle root hubs */
640 spin_unlock_irqrestore (&ehci->lock, flags);
641 return status ? retval : 0;
642}
643
644/*-------------------------------------------------------------------------*/
645
646static void
647ehci_hub_descriptor (
648 struct ehci_hcd *ehci,
649 struct usb_hub_descriptor *desc
650) {
651 int ports = HCS_N_PORTS (ehci->hcs_params);
652 u16 temp;
653
654 desc->bDescriptorType = 0x29;
655 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
656 desc->bHubContrCurrent = 0;
657
658 desc->bNbrPorts = ports;
659 temp = 1 + (ports / 8);
660 desc->bDescLength = 7 + 2 * temp;
661
662 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
dbe79bbe
JY
663 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
664 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
1da177e4
LT
665
666 temp = 0x0008; /* per-port overcurrent reporting */
667 if (HCS_PPC (ehci->hcs_params))
668 temp |= 0x0001; /* per-port power control */
56c1e26d
DB
669 else
670 temp |= 0x0002; /* no power switching */
1da177e4
LT
671#if 0
672// re-enable when we support USB_PORT_FEAT_INDICATOR below.
673 if (HCS_INDICATOR (ehci->hcs_params))
674 temp |= 0x0080; /* per-port indicators (LEDs) */
675#endif
fd05e720 676 desc->wHubCharacteristics = cpu_to_le16(temp);
1da177e4
LT
677}
678
679/*-------------------------------------------------------------------------*/
680
1da177e4
LT
681static int ehci_hub_control (
682 struct usb_hcd *hcd,
683 u16 typeReq,
684 u16 wValue,
685 u16 wIndex,
686 char *buf,
687 u16 wLength
688) {
689 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
690 int ports = HCS_N_PORTS (ehci->hcs_params);
383975d7
AS
691 u32 __iomem *status_reg = &ehci->regs->port_status[
692 (wIndex & 0xff) - 1];
a46af4eb 693 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
331ac6b2 694 u32 temp, temp1, status;
1da177e4
LT
695 unsigned long flags;
696 int retval = 0;
f0d7f273 697 unsigned selector;
1da177e4
LT
698
699 /*
700 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
701 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
702 * (track current state ourselves) ... blink for diagnostics,
703 * power, "this is the one", etc. EHCI spec supports this.
704 */
705
706 spin_lock_irqsave (&ehci->lock, flags);
707 switch (typeReq) {
708 case ClearHubFeature:
709 switch (wValue) {
710 case C_HUB_LOCAL_POWER:
711 case C_HUB_OVER_CURRENT:
712 /* no hub-wide feature/status flags */
713 break;
714 default:
715 goto error;
716 }
717 break;
718 case ClearPortFeature:
719 if (!wIndex || wIndex > ports)
720 goto error;
721 wIndex--;
e6316565 722 temp = ehci_readl(ehci, status_reg);
6d5f89c7 723 temp &= ~PORT_RWC_BITS;
625b5c9a
AS
724
725 /*
726 * Even if OWNER is set, so the port is owned by the
727 * companion controller, khubd needs to be able to clear
728 * the port-change status bits (especially
749da5f8 729 * USB_PORT_STAT_C_CONNECTION).
625b5c9a 730 */
1da177e4
LT
731
732 switch (wValue) {
733 case USB_PORT_FEAT_ENABLE:
e6316565 734 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
1da177e4
LT
735 break;
736 case USB_PORT_FEAT_C_ENABLE:
6d5f89c7 737 ehci_writel(ehci, temp | PORT_PEC, status_reg);
1da177e4
LT
738 break;
739 case USB_PORT_FEAT_SUSPEND:
740 if (temp & PORT_RESET)
741 goto error;
f8aeb3bb
DB
742 if (ehci->no_selective_suspend)
743 break;
83722bc9
AG
744#ifdef CONFIG_USB_OTG
745 if ((hcd->self.otg_port == (wIndex + 1))
746 && hcd->self.b_hnp_enable) {
c2e935a7 747 otg_start_hnp(hcd->phy->otg);
83722bc9
AG
748 break;
749 }
750#endif
16032c4f
AS
751 if (!(temp & PORT_SUSPEND))
752 break;
753 if ((temp & PORT_PE) == 0)
754 goto error;
755
756 /* clear phy low-power mode before resume */
a46af4eb 757 if (ehci->has_hostpc) {
16032c4f
AS
758 temp1 = ehci_readl(ehci, hostpc_reg);
759 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
eab80de0 760 hostpc_reg);
16032c4f
AS
761 spin_unlock_irqrestore(&ehci->lock, flags);
762 msleep(5);/* wait to leave low-power mode */
763 spin_lock_irqsave(&ehci->lock, flags);
1da177e4 764 }
16032c4f 765 /* resume signaling for 20 msec */
6d5f89c7 766 temp &= ~PORT_WAKE_BITS;
16032c4f
AS
767 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
768 ehci->reset_done[wIndex] = jiffies
769 + msecs_to_jiffies(20);
1da177e4
LT
770 break;
771 case USB_PORT_FEAT_C_SUSPEND:
d1f114d1 772 clear_bit(wIndex, &ehci->port_c_suspend);
1da177e4
LT
773 break;
774 case USB_PORT_FEAT_POWER:
775 if (HCS_PPC (ehci->hcs_params))
6d5f89c7
SW
776 ehci_writel(ehci, temp & ~PORT_POWER,
777 status_reg);
1da177e4
LT
778 break;
779 case USB_PORT_FEAT_C_CONNECTION:
6d5f89c7 780 ehci_writel(ehci, temp | PORT_CSC, status_reg);
1da177e4
LT
781 break;
782 case USB_PORT_FEAT_C_OVER_CURRENT:
6d5f89c7 783 ehci_writel(ehci, temp | PORT_OCC, status_reg);
1da177e4
LT
784 break;
785 case USB_PORT_FEAT_C_RESET:
786 /* GetPortStatus clears reset */
787 break;
788 default:
789 goto error;
790 }
083522d7 791 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
1da177e4
LT
792 break;
793 case GetHubDescriptor:
794 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
795 buf);
796 break;
797 case GetHubStatus:
798 /* no hub-wide feature/status flags */
799 memset (buf, 0, 4);
800 //cpu_to_le32s ((u32 *) buf);
801 break;
802 case GetPortStatus:
803 if (!wIndex || wIndex > ports)
804 goto error;
805 wIndex--;
806 status = 0;
e6316565 807 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
808
809 // wPortChange bits
810 if (temp & PORT_CSC)
749da5f8 811 status |= USB_PORT_STAT_C_CONNECTION << 16;
1da177e4 812 if (temp & PORT_PEC)
749da5f8 813 status |= USB_PORT_STAT_C_ENABLE << 16;
756aa6b3
CE
814
815 if ((temp & PORT_OCC) && !ignore_oc){
749da5f8 816 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1da177e4 817
756aa6b3
CE
818 /*
819 * Hubs should disable port power on over-current.
820 * However, not all EHCI implementations do this
821 * automatically, even if they _do_ support per-port
822 * power switching; they're allowed to just limit the
823 * current. khubd will turn the power back on.
824 */
81463c1d 825 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
756aa6b3
CE
826 ehci_writel(ehci,
827 temp & ~(PORT_RWC_BITS | PORT_POWER),
828 status_reg);
81463c1d 829 temp = ehci_readl(ehci, status_reg);
756aa6b3
CE
830 }
831 }
832
1da177e4 833 /* whoever resumes must GetPortStatus to complete it!! */
629e4427
AS
834 if (temp & PORT_RESUME) {
835
836 /* Remote Wakeup received? */
837 if (!ehci->reset_done[wIndex]) {
838 /* resume signaling for 20 msec */
839 ehci->reset_done[wIndex] = jiffies
840 + msecs_to_jiffies(20);
841 /* check the port again */
842 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
843 ehci->reset_done[wIndex]);
844 }
1da177e4 845
629e4427
AS
846 /* resume completed? */
847 else if (time_after_eq(jiffies,
848 ehci->reset_done[wIndex])) {
eafe5b99 849 clear_bit(wIndex, &ehci->suspended_ports);
d1f114d1 850 set_bit(wIndex, &ehci->port_c_suspend);
629e4427
AS
851 ehci->reset_done[wIndex] = 0;
852
853 /* stop resume signaling */
854 temp = ehci_readl(ehci, status_reg);
855 ehci_writel(ehci,
e6316565
AS
856 temp & ~(PORT_RWC_BITS | PORT_RESUME),
857 status_reg);
a448e4dc 858 clear_bit(wIndex, &ehci->resuming_ports);
629e4427 859 retval = handshake(ehci, status_reg,
083522d7 860 PORT_RESUME, 0, 2000 /* 2msec */);
629e4427
AS
861 if (retval != 0) {
862 ehci_err(ehci,
863 "port %d resume error %d\n",
864 wIndex + 1, retval);
865 goto error;
866 }
867 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1da177e4 868 }
1da177e4
LT
869 }
870
871 /* whoever resets must GetPortStatus to complete it!! */
872 if ((temp & PORT_RESET)
629e4427
AS
873 && time_after_eq(jiffies,
874 ehci->reset_done[wIndex])) {
749da5f8 875 status |= USB_PORT_STAT_C_RESET << 16;
1da177e4 876 ehci->reset_done [wIndex] = 0;
a448e4dc 877 clear_bit(wIndex, &ehci->resuming_ports);
1da177e4
LT
878
879 /* force reset to complete */
083522d7 880 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
e6316565 881 status_reg);
c22fa3ac
DB
882 /* REVISIT: some hardware needs 550+ usec to clear
883 * this bit; seems too long to spin routinely...
884 */
e6316565 885 retval = handshake(ehci, status_reg,
6307e096 886 PORT_RESET, 0, 1000);
1da177e4
LT
887 if (retval != 0) {
888 ehci_err (ehci, "port %d reset error %d\n",
889 wIndex + 1, retval);
890 goto error;
891 }
892
893 /* see what we found out */
e6316565
AS
894 temp = check_reset_complete (ehci, wIndex, status_reg,
895 ehci_readl(ehci, status_reg));
1da177e4
LT
896 }
897
a448e4dc 898 if (!(temp & (PORT_RESUME|PORT_RESET))) {
eafe5b99 899 ehci->reset_done[wIndex] = 0;
a448e4dc
AS
900 clear_bit(wIndex, &ehci->resuming_ports);
901 }
eafe5b99 902
57e06c11
AS
903 /* transfer dedicated ports to the companion hc */
904 if ((temp & PORT_CONNECT) &&
905 test_bit(wIndex, &ehci->companion_ports)) {
906 temp &= ~PORT_RWC_BITS;
907 temp |= PORT_OWNER;
908 ehci_writel(ehci, temp, status_reg);
909 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
910 temp = ehci_readl(ehci, status_reg);
911 }
912
625b5c9a
AS
913 /*
914 * Even if OWNER is set, there's no harm letting khubd
915 * see the wPortStatus values (they should all be 0 except
916 * for PORT_POWER anyway).
917 */
918
919 if (temp & PORT_CONNECT) {
749da5f8 920 status |= USB_PORT_STAT_CONNECTION;
625b5c9a 921 // status may be from integrated TT
331ac6b2
AD
922 if (ehci->has_hostpc) {
923 temp1 = ehci_readl(ehci, hostpc_reg);
924 status |= ehci_port_speed(ehci, temp1);
925 } else
926 status |= ehci_port_speed(ehci, temp);
1da177e4 927 }
625b5c9a 928 if (temp & PORT_PE)
749da5f8 929 status |= USB_PORT_STAT_ENABLE;
eafe5b99
AS
930
931 /* maybe the port was unsuspended without our knowledge */
932 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
749da5f8 933 status |= USB_PORT_STAT_SUSPEND;
eafe5b99
AS
934 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
935 clear_bit(wIndex, &ehci->suspended_ports);
a448e4dc 936 clear_bit(wIndex, &ehci->resuming_ports);
eafe5b99
AS
937 ehci->reset_done[wIndex] = 0;
938 if (temp & PORT_PE)
939 set_bit(wIndex, &ehci->port_c_suspend);
940 }
941
625b5c9a 942 if (temp & PORT_OC)
749da5f8 943 status |= USB_PORT_STAT_OVERCURRENT;
625b5c9a 944 if (temp & PORT_RESET)
749da5f8 945 status |= USB_PORT_STAT_RESET;
625b5c9a 946 if (temp & PORT_POWER)
749da5f8 947 status |= USB_PORT_STAT_POWER;
d1f114d1 948 if (test_bit(wIndex, &ehci->port_c_suspend))
749da5f8 949 status |= USB_PORT_STAT_C_SUSPEND << 16;
1da177e4 950
9776afc8 951#ifndef VERBOSE_DEBUG
1da177e4
LT
952 if (status & ~0xffff) /* only if wPortChange is interesting */
953#endif
954 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
a5abdeaf 955 put_unaligned_le32(status, buf);
1da177e4
LT
956 break;
957 case SetHubFeature:
958 switch (wValue) {
959 case C_HUB_LOCAL_POWER:
960 case C_HUB_OVER_CURRENT:
961 /* no hub-wide feature/status flags */
962 break;
963 default:
964 goto error;
965 }
966 break;
967 case SetPortFeature:
f0d7f273
DB
968 selector = wIndex >> 8;
969 wIndex &= 0xff;
8d053c79
JW
970 if (unlikely(ehci->debug)) {
971 /* If the debug port is active any port
972 * feature requests should get denied */
973 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
974 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
975 retval = -ENODEV;
976 goto error_exit;
977 }
978 }
1da177e4
LT
979 if (!wIndex || wIndex > ports)
980 goto error;
981 wIndex--;
e6316565 982 temp = ehci_readl(ehci, status_reg);
1da177e4
LT
983 if (temp & PORT_OWNER)
984 break;
985
10f6524a 986 temp &= ~PORT_RWC_BITS;
1da177e4
LT
987 switch (wValue) {
988 case USB_PORT_FEAT_SUSPEND:
f8aeb3bb
DB
989 if (ehci->no_selective_suspend)
990 break;
1da177e4
LT
991 if ((temp & PORT_PE) == 0
992 || (temp & PORT_RESET) != 0)
993 goto error;
b9df7942 994
331ac6b2
AD
995 /* After above check the port must be connected.
996 * Set appropriate bit thus could put phy into low power
997 * mode if we have hostpc feature
998 */
b9df7942
AD
999 temp &= ~PORT_WKCONN_E;
1000 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1001 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
a46af4eb 1002 if (ehci->has_hostpc) {
b9df7942 1003 spin_unlock_irqrestore(&ehci->lock, flags);
331ac6b2 1004 msleep(5);/* 5ms for HCD enter low pwr mode */
b9df7942 1005 spin_lock_irqsave(&ehci->lock, flags);
331ac6b2
AD
1006 temp1 = ehci_readl(ehci, hostpc_reg);
1007 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1008 hostpc_reg);
1009 temp1 = ehci_readl(ehci, hostpc_reg);
1010 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1011 wIndex, (temp1 & HOSTPC_PHCD) ?
1012 "succeeded" : "failed");
1013 }
eafe5b99 1014 set_bit(wIndex, &ehci->suspended_ports);
1da177e4
LT
1015 break;
1016 case USB_PORT_FEAT_POWER:
1017 if (HCS_PPC (ehci->hcs_params))
083522d7 1018 ehci_writel(ehci, temp | PORT_POWER,
e6316565 1019 status_reg);
1da177e4
LT
1020 break;
1021 case USB_PORT_FEAT_RESET:
1022 if (temp & PORT_RESUME)
1023 goto error;
1024 /* line status bits may report this as low speed,
1025 * which can be fine if this root hub has a
1026 * transaction translator built in.
1027 */
1028 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1029 && !ehci_is_TDI(ehci)
1030 && PORT_USB11 (temp)) {
1031 ehci_dbg (ehci,
1032 "port %d low speed --> companion\n",
1033 wIndex + 1);
1034 temp |= PORT_OWNER;
1035 } else {
1036 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1037 temp |= PORT_RESET;
1038 temp &= ~PORT_PE;
1039
1040 /*
1041 * caller must wait, then call GetPortStatus
1042 * usb 2.0 spec says 50 ms resets on root
1043 */
1044 ehci->reset_done [wIndex] = jiffies
1045 + msecs_to_jiffies (50);
1046 }
e6316565 1047 ehci_writel(ehci, temp, status_reg);
1da177e4 1048 break;
f0d7f273
DB
1049
1050 /* For downstream facing ports (these): one hub port is put
1051 * into test mode according to USB2 11.24.2.13, then the hub
1052 * must be reset (which for root hub now means rmmod+modprobe,
1053 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1054 * about the EHCI-specific stuff.
1055 */
1056 case USB_PORT_FEAT_TEST:
1057 if (!selector || selector > 5)
1058 goto error;
c4f34764 1059 spin_unlock_irqrestore(&ehci->lock, flags);
f0d7f273 1060 ehci_quiesce(ehci);
c4f34764 1061 spin_lock_irqsave(&ehci->lock, flags);
77636c86
BT
1062
1063 /* Put all enabled ports into suspend */
1064 while (ports--) {
1065 u32 __iomem *sreg =
1066 &ehci->regs->port_status[ports];
1067
1068 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1069 if (temp & PORT_PE)
1070 ehci_writel(ehci, temp | PORT_SUSPEND,
1071 sreg);
1072 }
c4f34764
AS
1073
1074 spin_unlock_irqrestore(&ehci->lock, flags);
f0d7f273 1075 ehci_halt(ehci);
c4f34764
AS
1076 spin_lock_irqsave(&ehci->lock, flags);
1077
77636c86 1078 temp = ehci_readl(ehci, status_reg);
f0d7f273 1079 temp |= selector << 16;
e6316565 1080 ehci_writel(ehci, temp, status_reg);
f0d7f273
DB
1081 break;
1082
1da177e4
LT
1083 default:
1084 goto error;
1085 }
083522d7 1086 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1da177e4
LT
1087 break;
1088
1089 default:
1090error:
1091 /* "stall" on error */
1092 retval = -EPIPE;
1093 }
8d053c79 1094error_exit:
1da177e4
LT
1095 spin_unlock_irqrestore (&ehci->lock, flags);
1096 return retval;
1097}
90da096e 1098
5407a3c3
FB
1099static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
1100 int portnum)
90da096e
BR
1101{
1102 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1103
1104 if (ehci_is_TDI(ehci))
1105 return;
1106 set_owner(ehci, --portnum, PORT_OWNER);
1107}
1108
5407a3c3
FB
1109static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
1110 int portnum)
3a31155c
AS
1111{
1112 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1113 u32 __iomem *reg;
1114
1115 if (ehci_is_TDI(ehci))
1116 return 0;
1117 reg = &ehci->regs->port_status[portnum - 1];
1118 return ehci_readl(ehci, reg) & PORT_OWNER;
1119}
This page took 0.989051 seconds and 5 git commands to generate.