blob: 1292a5b2197ae8d4d1b0955fa736e6f74cb31b78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownelld49d4312005-05-07 13:21:50 -07002 * Copyright (C) 2001-2004 by David Brownell
David Brownell53bd6a62006-08-30 14:50:06 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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/*-------------------------------------------------------------------------*/
30
Alan Stern58a97ff2008-04-14 12:17:10 -040031#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
32
Alan Sternaff6d182008-04-18 11:11:26 -040033#ifdef CONFIG_PM
34
Alan Stern383975d2007-05-04 11:52:40 -040035static int ehci_hub_control(
36 struct usb_hcd *hcd,
37 u16 typeReq,
38 u16 wValue,
39 u16 wIndex,
40 char *buf,
41 u16 wLength
42);
43
44/* After a power loss, ports that were owned by the companion must be
45 * reset so that the companion can still own them.
46 */
47static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
48{
49 u32 __iomem *reg;
50 u32 status;
51 int port;
52 __le32 buf;
53 struct usb_hcd *hcd = ehci_to_hcd(ehci);
54
55 if (!ehci->owned_ports)
56 return;
57
58 /* Give the connections some time to appear */
59 msleep(20);
60
61 port = HCS_N_PORTS(ehci->hcs_params);
62 while (port--) {
63 if (test_bit(port, &ehci->owned_ports)) {
64 reg = &ehci->regs->port_status[port];
Alan Stern3c519b82007-05-04 11:55:31 -040065 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern383975d2007-05-04 11:52:40 -040066
67 /* Port already owned by companion? */
68 if (status & PORT_OWNER)
69 clear_bit(port, &ehci->owned_ports);
Alan Stern3c519b82007-05-04 11:55:31 -040070 else if (test_bit(port, &ehci->companion_ports))
71 ehci_writel(ehci, status & ~PORT_PE, reg);
Alan Stern383975d2007-05-04 11:52:40 -040072 else
73 ehci_hub_control(hcd, SetPortFeature,
74 USB_PORT_FEAT_RESET, port + 1,
75 NULL, 0);
76 }
77 }
78
79 if (!ehci->owned_ports)
80 return;
81 msleep(90); /* Wait for resets to complete */
82
83 port = HCS_N_PORTS(ehci->hcs_params);
84 while (port--) {
85 if (test_bit(port, &ehci->owned_ports)) {
86 ehci_hub_control(hcd, GetPortStatus,
87 0, port + 1,
88 (char *) &buf, sizeof(buf));
89
90 /* The companion should now own the port,
91 * but if something went wrong the port must not
92 * remain enabled.
93 */
94 reg = &ehci->regs->port_status[port];
95 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
96 if (status & PORT_OWNER)
97 ehci_writel(ehci, status | PORT_CSC, reg);
98 else {
99 ehci_dbg(ehci, "failed handover port %d: %x\n",
100 port + 1, status);
101 ehci_writel(ehci, status & ~PORT_PE, reg);
102 }
103 }
104 }
105
106 ehci->owned_ports = 0;
107}
108
Alan Stern16032c42010-05-12 18:21:35 -0400109static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
Alan Stern41472002010-06-25 14:02:14 -0400110 bool suspending, bool do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400111{
112 int port;
113 u32 temp;
114
115 /* If remote wakeup is enabled for the root hub but disabled
116 * for the controller, we must adjust all the port wakeup flags
117 * when the controller is suspended or resumed. In all other
118 * cases they don't need to be changed.
119 */
Alan Stern41472002010-06-25 14:02:14 -0400120 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400121 return;
122
123 /* clear phy low-power mode before changing wakeup flags */
124 if (ehci->has_hostpc) {
125 port = HCS_N_PORTS(ehci->hcs_params);
126 while (port--) {
127 u32 __iomem *hostpc_reg;
128
129 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
130 + HOSTPC0 + 4 * port);
131 temp = ehci_readl(ehci, hostpc_reg);
132 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
133 }
134 msleep(5);
135 }
136
137 port = HCS_N_PORTS(ehci->hcs_params);
138 while (port--) {
139 u32 __iomem *reg = &ehci->regs->port_status[port];
140 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
141 u32 t2 = t1 & ~PORT_WAKE_BITS;
142
143 /* If we are suspending the controller, clear the flags.
144 * If we are resuming the controller, set the wakeup flags.
145 */
146 if (!suspending) {
147 if (t1 & PORT_CONNECT)
148 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
149 else
150 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
151 }
152 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
153 port + 1, t1, t2);
154 ehci_writel(ehci, t2, reg);
155 }
156
157 /* enter phy low-power mode again */
158 if (ehci->has_hostpc) {
159 port = HCS_N_PORTS(ehci->hcs_params);
160 while (port--) {
161 u32 __iomem *hostpc_reg;
162
163 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
164 + HOSTPC0 + 4 * port);
165 temp = ehci_readl(ehci, hostpc_reg);
166 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
167 }
168 }
169}
170
Alan Stern0c0382e2005-10-13 17:08:02 -0400171static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
174 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500175 int mask;
Alan Stern16032c42010-05-12 18:21:35 -0400176 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Alan Stern8c774fe2007-02-01 16:09:59 -0500178 ehci_dbg(ehci, "suspend root hub\n");
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (time_before (jiffies, ehci->next_statechange))
181 msleep(5);
Alan Sternf8fa7572008-01-10 16:43:15 -0500182 del_timer_sync(&ehci->watchdog);
183 del_timer_sync(&ehci->iaa_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_lock_irq (&ehci->lock);
186
Alan Sterncec3a532010-01-08 11:18:20 -0500187 /* Once the controller is stopped, port resumes that are already
188 * in progress won't complete. Hence if remote wakeup is enabled
189 * for the root hub and any ports are in the middle of a resume or
190 * remote wakeup, we must fail the suspend.
191 */
192 if (hcd->self.root_hub->do_remote_wakeup) {
193 port = HCS_N_PORTS(ehci->hcs_params);
194 while (port--) {
195 if (ehci->reset_done[port] != 0) {
196 spin_unlock_irq(&ehci->lock);
197 ehci_dbg(ehci, "suspend failed because "
198 "port %d is resuming\n",
199 port + 1);
200 return -EBUSY;
201 }
202 }
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* stop schedules, clean any completed work */
206 if (HC_IS_RUNNING(hcd->state)) {
207 ehci_quiesce (ehci);
208 hcd->state = HC_STATE_QUIESCING;
209 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100210 ehci->command = ehci_readl(ehci, &ehci->regs->command);
David Howells7d12e782006-10-05 14:55:46 +0100211 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Alan Stern8c033562006-11-09 14:42:16 -0500213 /* Unlike other USB host controller types, EHCI doesn't have
214 * any notion of "global" or bus-wide suspend. The driver has
215 * to manually suspend all the active unsuspended ports, and
216 * then manually resume them in the bus_resume() routine.
217 */
218 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400219 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400220 changed = 0;
Alan Sterncec3a532010-01-08 11:18:20 -0500221 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while (port--) {
223 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100224 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400225 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Stern8c033562006-11-09 14:42:16 -0500227 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400228 if (t1 & PORT_OWNER)
229 set_bit(port, &ehci->owned_ports);
230 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500232 set_bit(port, &ehci->bus_suspended);
233 }
234
Alan Stern16032c42010-05-12 18:21:35 -0400235 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800236 if (hcd->self.root_hub->do_remote_wakeup) {
237 /* only enable appropriate wake bits, otherwise the
238 * hardware can not go phy low power mode. If a race
239 * condition happens here(connection change during bits
240 * set), the port change detection will finally fix it.
241 */
Alan Stern16032c42010-05-12 18:21:35 -0400242 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800243 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400244 else
Alek Du331ac6b2009-07-13 12:41:20 +0800245 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (t1 != t2) {
249 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
250 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100251 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400252 changed = 1;
253 }
254 }
Alek Du331ac6b2009-07-13 12:41:20 +0800255
Alan Stern16032c42010-05-12 18:21:35 -0400256 if (changed && ehci->has_hostpc) {
257 spin_unlock_irq(&ehci->lock);
258 msleep(5); /* 5 ms for HCD to enter low-power mode */
259 spin_lock_irq(&ehci->lock);
260
261 port = HCS_N_PORTS(ehci->hcs_params);
262 while (port--) {
263 u32 __iomem *hostpc_reg;
264 u32 t3;
265
266 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
267 + HOSTPC0 + 4 * port);
268 t3 = ehci_readl(ehci, hostpc_reg);
269 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
270 t3 = ehci_readl(ehci, hostpc_reg);
271 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800272 port, (t3 & HOSTPC_PHCD) ?
273 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 }
276
Alan Sterncd930c92008-01-10 11:14:53 -0500277 /* Apparently some devices need a >= 1-uframe delay here */
278 if (ehci->bus_suspended)
279 udelay(150);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* turn off now-idle HC */
282 ehci_halt (ehci);
283 hcd->state = HC_STATE_SUSPENDED;
284
David Brownellcdc647a2008-04-02 13:40:20 -0700285 if (ehci->reclaim)
286 end_unlink_async(ehci);
287
Alan Stern8c033562006-11-09 14:42:16 -0500288 /* allow remote wakeup */
289 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400290 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500291 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100292 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
293 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
296 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400297
298 /* ehci_work() may have re-enabled the watchdog timer, which we do not
299 * want, and so we must delete any pending watchdog timer events.
300 */
301 del_timer_sync(&ehci->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303}
304
305
306/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400307static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
310 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400311 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int i;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530313 u8 resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (time_before (jiffies, ehci->next_statechange))
316 msleep(5);
317 spin_lock_irq (&ehci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400318 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -0400319 spin_unlock_irq(&ehci->lock);
320 return -ESHUTDOWN;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Jason Wesselad45f1d2009-08-20 15:39:58 -0500323 if (unlikely(ehci->debug)) {
Jason Wessel872d3592009-09-23 18:32:44 -0500324 if (!dbgp_reset_prep())
Jason Wesselad45f1d2009-08-20 15:39:58 -0500325 ehci->debug = NULL;
326 else
327 dbgp_external_startup();
328 }
329
David Brownellf03c17f2005-11-23 15:45:28 -0800330 /* Ideally and we've got a real resume here, and no port's power
331 * was lost. (For PCI, that means Vaux was maintained.) But we
332 * could instead be restoring a swsusp snapshot -- so that BIOS was
333 * the last user of the controller, not reset/pm hardware keeping
334 * state we gave to it.
335 */
Alan Stern383975d2007-05-04 11:52:40 -0400336 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
337 ehci_dbg(ehci, "resume root hub%s\n",
338 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800339
Alan Stern8c033562006-11-09 14:42:16 -0500340 /* at least some APM implementations will try to deliver
341 * IRQs right away, so delay them until we're ready.
342 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100343 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500344
345 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100346 ehci_writel(ehci, 0, &ehci->regs->segment);
347 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
348 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100351 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Alan Sterne198a312007-03-15 15:54:30 -0400353 /* Some controller/firmware combinations need a delay during which
354 * they set up the port statuses. See Bugzilla #8190. */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530355 spin_unlock_irq(&ehci->lock);
356 msleep(8);
357 spin_lock_irq(&ehci->lock);
Alan Sterne198a312007-03-15 15:54:30 -0400358
Alan Stern16032c42010-05-12 18:21:35 -0400359 /* clear phy low-power mode before resume */
360 if (ehci->bus_suspended && ehci->has_hostpc) {
361 i = HCS_N_PORTS(ehci->hcs_params);
362 while (i--) {
363 if (test_bit(i, &ehci->bus_suspended)) {
364 u32 __iomem *hostpc_reg;
365
366 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
367 + HOSTPC0 + 4 * i);
368 temp = ehci_readl(ehci, hostpc_reg);
369 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
370 hostpc_reg);
371 }
372 }
373 spin_unlock_irq(&ehci->lock);
374 msleep(5);
375 spin_lock_irq(&ehci->lock);
376 }
377
Alan Stern8c033562006-11-09 14:42:16 -0500378 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 i = HCS_N_PORTS (ehci->hcs_params);
380 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100381 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400382 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500383 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530384 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 temp |= PORT_RESUME;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530386 resume_needed = 1;
387 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100388 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530390
391 /* msleep for 20ms only if code is trying to resume port */
392 if (resume_needed) {
393 spin_unlock_irq(&ehci->lock);
394 msleep(20);
395 spin_lock_irq(&ehci->lock);
396 }
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100400 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500401 if (test_bit(i, &ehci->bus_suspended) &&
402 (temp & PORT_SUSPEND)) {
403 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100404 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500405 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100408 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* maybe re-activate the schedule(s) */
411 temp = 0;
412 if (ehci->async->qh_next.qh)
413 temp |= CMD_ASE;
414 if (ehci->periodic_sched)
415 temp |= CMD_PSE;
416 if (temp) {
417 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100418 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
422 hcd->state = HC_STATE_RUNNING;
423
424 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100425 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 spin_unlock_irq (&ehci->lock);
Alan Stern3bb1af52008-03-03 15:15:36 -0500428 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432#else
433
Alan Stern0c0382e2005-10-13 17:08:02 -0400434#define ehci_bus_suspend NULL
435#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437#endif /* CONFIG_PM */
438
439/*-------------------------------------------------------------------------*/
440
Alan Stern57e06c12007-01-16 11:59:45 -0500441/* Display the ports dedicated to the companion controller */
Tony Jones5a3201b2007-09-11 14:07:31 -0700442static ssize_t show_companion(struct device *dev,
443 struct device_attribute *attr,
444 char *buf)
Alan Stern57e06c12007-01-16 11:59:45 -0500445{
446 struct ehci_hcd *ehci;
447 int nports, index, n;
448 int count = PAGE_SIZE;
449 char *ptr = buf;
450
Tony Jones5a3201b2007-09-11 14:07:31 -0700451 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
Alan Stern57e06c12007-01-16 11:59:45 -0500452 nports = HCS_N_PORTS(ehci->hcs_params);
453
454 for (index = 0; index < nports; ++index) {
455 if (test_bit(index, &ehci->companion_ports)) {
456 n = scnprintf(ptr, count, "%d\n", index + 1);
457 ptr += n;
458 count -= n;
459 }
460 }
461 return ptr - buf;
462}
463
464/*
Balaji Rao90da0962007-11-22 01:58:14 +0530465 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500466 */
Balaji Rao90da0962007-11-22 01:58:14 +0530467static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500468{
Alan Stern57e06c12007-01-16 11:59:45 -0500469 u32 __iomem *status_reg;
470 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530471 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500472
Balaji Rao90da0962007-11-22 01:58:14 +0530473 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500474
475 /*
476 * The controller won't set the OWNER bit if the port is
477 * enabled, so this loop will sometimes require at least two
478 * iterations: one to disable the port and one to set OWNER.
479 */
Alan Stern57e06c12007-01-16 11:59:45 -0500480 for (try = 4; try > 0; --try) {
481 spin_lock_irq(&ehci->lock);
482 port_status = ehci_readl(ehci, status_reg);
483 if ((port_status & PORT_OWNER) == new_owner
484 || (port_status & (PORT_OWNER | PORT_CONNECT))
485 == 0)
486 try = 0;
487 else {
488 port_status ^= PORT_OWNER;
489 port_status &= ~(PORT_PE | PORT_RWC_BITS);
490 ehci_writel(ehci, port_status, status_reg);
491 }
492 spin_unlock_irq(&ehci->lock);
493 if (try > 1)
494 msleep(5);
495 }
Balaji Rao90da0962007-11-22 01:58:14 +0530496}
497
498/*
499 * Dedicate or undedicate a port to the companion controller.
500 * Syntax is "[-]portnum", where a leading '-' sign means
501 * return control of the port to the EHCI controller.
502 */
503static ssize_t store_companion(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
506{
507 struct ehci_hcd *ehci;
508 int portnum, new_owner;
509
510 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
511 new_owner = PORT_OWNER; /* Owned by companion */
512 if (sscanf(buf, "%d", &portnum) != 1)
513 return -EINVAL;
514 if (portnum < 0) {
515 portnum = - portnum;
516 new_owner = 0; /* Owned by EHCI */
517 }
518 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
519 return -ENOENT;
520 portnum--;
521 if (new_owner)
522 set_bit(portnum, &ehci->companion_ports);
523 else
524 clear_bit(portnum, &ehci->companion_ports);
525 set_owner(ehci, portnum, new_owner);
Alan Stern57e06c12007-01-16 11:59:45 -0500526 return count;
527}
Tony Jones5a3201b2007-09-11 14:07:31 -0700528static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500529
530static inline void create_companion_file(struct ehci_hcd *ehci)
531{
532 int i;
533
534 /* with integrated TT there is no companion! */
535 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700536 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700537 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500538}
539
540static inline void remove_companion_file(struct ehci_hcd *ehci)
541{
542 /* with integrated TT there is no companion! */
543 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700544 device_remove_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700545 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500546}
547
548
549/*-------------------------------------------------------------------------*/
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static int check_reset_complete (
552 struct ehci_hcd *ehci,
553 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500554 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int port_status
556) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800557 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* if reset finished and it's still not enabled -- handoff */
561 if (!(port_status & PORT_PE)) {
562
563 /* with integrated TT, there's nobody to hand it to! */
564 if (ehci_is_TDI(ehci)) {
565 ehci_dbg (ehci,
566 "Failed to enable port %d on root hub TT\n",
567 index+1);
568 return port_status;
569 }
570
571 ehci_dbg (ehci, "port %d full speed --> companion\n",
572 index + 1);
573
574 // what happens if HCS_N_CC(params) == 0 ?
575 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700576 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500577 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100579 /* ensure 440EPX ohci controller state is operational */
580 if (ehci->has_amcc_usb23)
581 set_ohci_hcfs(ehci, 1);
582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ehci_dbg (ehci, "port %d high speed\n", index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100584 /* ensure 440EPx ohci controller state is suspended */
585 if (ehci->has_amcc_usb23)
586 set_ohci_hcfs(ehci, 0);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 return port_status;
590}
591
592/*-------------------------------------------------------------------------*/
593
594
595/* build "status change" packet (one or two bytes) from HC registers */
596
597static int
598ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
599{
600 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
601 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800602 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int ports, i, retval = 1;
604 unsigned long flags;
Alek Du5a9cdf32010-06-04 15:47:56 +0800605 u32 ppcd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
608 if (!HC_IS_RUNNING(hcd->state))
609 return 0;
610
611 /* init status to no-changes */
612 buf [0] = 0;
613 ports = HCS_N_PORTS (ehci->hcs_params);
614 if (ports > 7) {
615 buf [1] = 0;
616 retval++;
617 }
David Brownell53bd6a62006-08-30 14:50:06 -0700618
David Brownell93f1a472006-11-16 23:34:58 -0800619 /* Some boards (mostly VIA?) report bogus overcurrent indications,
620 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200621 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800622 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
623 * PORT_POWER; that's surprising, but maybe within-spec.
624 */
625 if (!ignore_oc)
626 mask = PORT_CSC | PORT_PEC | PORT_OCC;
627 else
628 mask = PORT_CSC | PORT_PEC;
629 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* no hub change reports (bit 0) for now (power, ...) */
632
633 /* port N changes (bit N)? */
634 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800635
636 /* get per-port change detect bits */
637 if (ehci->has_ppcd)
638 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800641 /* leverage per-port change bits feature */
642 if (ehci->has_ppcd && !(ppcd & (1 << i)))
643 continue;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100644 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500645
646 /*
647 * Return status information even for ports with OWNER set.
648 * Otherwise khubd wouldn't see the disconnect event when a
649 * high-speed device is switched over to the companion
650 * controller by the user.
651 */
652
Alan Sterneafe5b92008-10-06 11:25:53 -0400653 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
654 || (ehci->reset_done[i] && time_after_eq(
655 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (i < 7)
657 buf [0] |= 1 << (i + 1);
658 else
659 buf [1] |= 1 << (i - 7);
660 status = STS_PCD;
661 }
662 }
663 /* FIXME autosuspend idle root hubs */
664 spin_unlock_irqrestore (&ehci->lock, flags);
665 return status ? retval : 0;
666}
667
668/*-------------------------------------------------------------------------*/
669
670static void
671ehci_hub_descriptor (
672 struct ehci_hcd *ehci,
673 struct usb_hub_descriptor *desc
674) {
675 int ports = HCS_N_PORTS (ehci->hcs_params);
676 u16 temp;
677
678 desc->bDescriptorType = 0x29;
679 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
680 desc->bHubContrCurrent = 0;
681
682 desc->bNbrPorts = ports;
683 temp = 1 + (ports / 8);
684 desc->bDescLength = 7 + 2 * temp;
685
686 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
687 memset (&desc->bitmap [0], 0, temp);
688 memset (&desc->bitmap [temp], 0xff, temp);
689
690 temp = 0x0008; /* per-port overcurrent reporting */
691 if (HCS_PPC (ehci->hcs_params))
692 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700693 else
694 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#if 0
696// re-enable when we support USB_PORT_FEAT_INDICATOR below.
697 if (HCS_INDICATOR (ehci->hcs_params))
698 temp |= 0x0080; /* per-port indicators (LEDs) */
699#endif
Al Virofd05e722008-04-28 07:00:16 +0100700 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/*-------------------------------------------------------------------------*/
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static int ehci_hub_control (
706 struct usb_hcd *hcd,
707 u16 typeReq,
708 u16 wValue,
709 u16 wIndex,
710 char *buf,
711 u16 wLength
712) {
713 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
714 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400715 u32 __iomem *status_reg = &ehci->regs->port_status[
716 (wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800717 u32 __iomem *hostpc_reg = NULL;
718 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long flags;
720 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800721 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
725 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
726 * (track current state ourselves) ... blink for diagnostics,
727 * power, "this is the one", etc. EHCI spec supports this.
728 */
729
Alek Du331ac6b2009-07-13 12:41:20 +0800730 if (ehci->has_hostpc)
731 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
732 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 spin_lock_irqsave (&ehci->lock, flags);
734 switch (typeReq) {
735 case ClearHubFeature:
736 switch (wValue) {
737 case C_HUB_LOCAL_POWER:
738 case C_HUB_OVER_CURRENT:
739 /* no hub-wide feature/status flags */
740 break;
741 default:
742 goto error;
743 }
744 break;
745 case ClearPortFeature:
746 if (!wIndex || wIndex > ports)
747 goto error;
748 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500749 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500750
751 /*
752 * Even if OWNER is set, so the port is owned by the
753 * companion controller, khubd needs to be able to clear
754 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500755 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 switch (wValue) {
759 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500760 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100763 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500764 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 case USB_PORT_FEAT_SUSPEND:
767 if (temp & PORT_RESET)
768 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800769 if (ehci->no_selective_suspend)
770 break;
Alan Stern16032c42010-05-12 18:21:35 -0400771 if (!(temp & PORT_SUSPEND))
772 break;
773 if ((temp & PORT_PE) == 0)
774 goto error;
775
776 /* clear phy low-power mode before resume */
777 if (hostpc_reg) {
778 temp1 = ehci_readl(ehci, hostpc_reg);
779 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800780 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400781 spin_unlock_irqrestore(&ehci->lock, flags);
782 msleep(5);/* wait to leave low-power mode */
783 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Alan Stern16032c42010-05-12 18:21:35 -0400785 /* resume signaling for 20 msec */
786 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
787 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
788 ehci->reset_done[wIndex] = jiffies
789 + msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
791 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400792 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794 case USB_PORT_FEAT_POWER:
795 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100796 ehci_writel(ehci,
797 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500798 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800 case USB_PORT_FEAT_C_CONNECTION:
Alek Du48f24972010-06-04 15:47:55 +0800801 if (ehci->has_lpm) {
802 /* clear PORTSC bits on disconnect */
803 temp &= ~PORT_LPM;
804 temp &= ~PORT_DEV_ADDR;
805 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100806 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500807 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
809 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100810 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500811 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 case USB_PORT_FEAT_C_RESET:
814 /* GetPortStatus clears reset */
815 break;
816 default:
817 goto error;
818 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100819 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 case GetHubDescriptor:
822 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
823 buf);
824 break;
825 case GetHubStatus:
826 /* no hub-wide feature/status flags */
827 memset (buf, 0, 4);
828 //cpu_to_le32s ((u32 *) buf);
829 break;
830 case GetPortStatus:
831 if (!wIndex || wIndex > ports)
832 goto error;
833 wIndex--;
834 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500835 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 // wPortChange bits
838 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500839 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500841 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700842
843 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500844 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700846 /*
847 * Hubs should disable port power on over-current.
848 * However, not all EHCI implementations do this
849 * automatically, even if they _do_ support per-port
850 * power switching; they're allowed to just limit the
851 * current. khubd will turn the power back on.
852 */
853 if (HCS_PPC (ehci->hcs_params)){
854 ehci_writel(ehci,
855 temp & ~(PORT_RWC_BITS | PORT_POWER),
856 status_reg);
857 }
858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500861 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Alan Stern629e4422007-01-22 16:08:53 -0500863 /* Remote Wakeup received? */
864 if (!ehci->reset_done[wIndex]) {
865 /* resume signaling for 20 msec */
866 ehci->reset_done[wIndex] = jiffies
867 + msecs_to_jiffies(20);
868 /* check the port again */
869 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
870 ehci->reset_done[wIndex]);
871 }
872
873 /* resume completed? */
874 else if (time_after_eq(jiffies,
875 ehci->reset_done[wIndex])) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400876 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sternd1f114d2008-05-20 16:58:58 -0400877 set_bit(wIndex, &ehci->port_c_suspend);
Alan Stern629e4422007-01-22 16:08:53 -0500878 ehci->reset_done[wIndex] = 0;
879
880 /* stop resume signaling */
881 temp = ehci_readl(ehci, status_reg);
882 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500883 temp & ~(PORT_RWC_BITS | PORT_RESUME),
884 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500885 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100886 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500887 if (retval != 0) {
888 ehci_err(ehci,
889 "port %d resume error %d\n",
890 wIndex + 1, retval);
891 goto error;
892 }
893 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
897 /* whoever resets must GetPortStatus to complete it!! */
898 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500899 && time_after_eq(jiffies,
900 ehci->reset_done[wIndex])) {
Alan Stern749da5f2010-03-04 17:05:08 -0500901 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ehci->reset_done [wIndex] = 0;
903
904 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100905 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500906 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700907 /* REVISIT: some hardware needs 550+ usec to clear
908 * this bit; seems too long to spin routinely...
909 */
Alan Sterne6316562007-01-16 11:58:00 -0500910 retval = handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -0500911 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (retval != 0) {
913 ehci_err (ehci, "port %d reset error %d\n",
914 wIndex + 1, retval);
915 goto error;
916 }
917
918 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500919 temp = check_reset_complete (ehci, wIndex, status_reg,
920 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Alan Sterneafe5b92008-10-06 11:25:53 -0400923 if (!(temp & (PORT_RESUME|PORT_RESET)))
924 ehci->reset_done[wIndex] = 0;
925
Alan Stern57e06c12007-01-16 11:59:45 -0500926 /* transfer dedicated ports to the companion hc */
927 if ((temp & PORT_CONNECT) &&
928 test_bit(wIndex, &ehci->companion_ports)) {
929 temp &= ~PORT_RWC_BITS;
930 temp |= PORT_OWNER;
931 ehci_writel(ehci, temp, status_reg);
932 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
933 temp = ehci_readl(ehci, status_reg);
934 }
935
Alan Stern625b5c92007-01-16 11:58:47 -0500936 /*
937 * Even if OWNER is set, there's no harm letting khubd
938 * see the wPortStatus values (they should all be 0 except
939 * for PORT_POWER anyway).
940 */
941
942 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500943 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -0500944 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +0800945 if (ehci->has_hostpc) {
946 temp1 = ehci_readl(ehci, hostpc_reg);
947 status |= ehci_port_speed(ehci, temp1);
948 } else
949 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Alan Stern625b5c92007-01-16 11:58:47 -0500951 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500952 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -0400953
954 /* maybe the port was unsuspended without our knowledge */
955 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -0500956 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -0400957 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
958 clear_bit(wIndex, &ehci->suspended_ports);
959 ehci->reset_done[wIndex] = 0;
960 if (temp & PORT_PE)
961 set_bit(wIndex, &ehci->port_c_suspend);
962 }
963
Alan Stern625b5c92007-01-16 11:58:47 -0500964 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500965 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -0500966 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500967 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -0500968 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500969 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -0400970 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -0500971 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
David Brownell9776afc2008-02-01 11:42:05 -0800973#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (status & ~0xffff) /* only if wPortChange is interesting */
975#endif
976 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700977 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 case SetHubFeature:
980 switch (wValue) {
981 case C_HUB_LOCAL_POWER:
982 case C_HUB_OVER_CURRENT:
983 /* no hub-wide feature/status flags */
984 break;
985 default:
986 goto error;
987 }
988 break;
989 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800990 selector = wIndex >> 8;
991 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -0500992 if (unlikely(ehci->debug)) {
993 /* If the debug port is active any port
994 * feature requests should get denied */
995 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
996 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
997 retval = -ENODEV;
998 goto error_exit;
999 }
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (!wIndex || wIndex > ports)
1002 goto error;
1003 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -05001004 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (temp & PORT_OWNER)
1006 break;
1007
David Brownell10f65242005-08-31 10:55:38 -07001008 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 switch (wValue) {
1010 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -08001011 if (ehci->no_selective_suspend)
1012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if ((temp & PORT_PE) == 0
1014 || (temp & PORT_RESET) != 0)
1015 goto error;
Alek Dub9df79422010-01-19 16:31:31 +08001016
Alek Du331ac6b2009-07-13 12:41:20 +08001017 /* After above check the port must be connected.
1018 * Set appropriate bit thus could put phy into low power
1019 * mode if we have hostpc feature
1020 */
Alek Dub9df79422010-01-19 16:31:31 +08001021 temp &= ~PORT_WKCONN_E;
1022 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1023 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Alek Du331ac6b2009-07-13 12:41:20 +08001024 if (hostpc_reg) {
Alek Dub9df79422010-01-19 16:31:31 +08001025 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001026 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df79422010-01-19 16:31:31 +08001027 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001028 temp1 = ehci_readl(ehci, hostpc_reg);
1029 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1030 hostpc_reg);
1031 temp1 = ehci_readl(ehci, hostpc_reg);
1032 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1033 wIndex, (temp1 & HOSTPC_PHCD) ?
1034 "succeeded" : "failed");
1035 }
Alan Sterneafe5b92008-10-06 11:25:53 -04001036 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
1038 case USB_PORT_FEAT_POWER:
1039 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001040 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -05001041 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
1043 case USB_PORT_FEAT_RESET:
1044 if (temp & PORT_RESUME)
1045 goto error;
1046 /* line status bits may report this as low speed,
1047 * which can be fine if this root hub has a
1048 * transaction translator built in.
1049 */
1050 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1051 && !ehci_is_TDI(ehci)
1052 && PORT_USB11 (temp)) {
1053 ehci_dbg (ehci,
1054 "port %d low speed --> companion\n",
1055 wIndex + 1);
1056 temp |= PORT_OWNER;
1057 } else {
1058 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1059 temp |= PORT_RESET;
1060 temp &= ~PORT_PE;
1061
1062 /*
1063 * caller must wait, then call GetPortStatus
1064 * usb 2.0 spec says 50 ms resets on root
1065 */
1066 ehci->reset_done [wIndex] = jiffies
1067 + msecs_to_jiffies (50);
1068 }
Alan Sterne6316562007-01-16 11:58:00 -05001069 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001071
1072 /* For downstream facing ports (these): one hub port is put
1073 * into test mode according to USB2 11.24.2.13, then the hub
1074 * must be reset (which for root hub now means rmmod+modprobe,
1075 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1076 * about the EHCI-specific stuff.
1077 */
1078 case USB_PORT_FEAT_TEST:
1079 if (!selector || selector > 5)
1080 goto error;
1081 ehci_quiesce(ehci);
1082 ehci_halt(ehci);
1083 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001084 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001085 break;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 default:
1088 goto error;
1089 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001090 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092
1093 default:
1094error:
1095 /* "stall" on error */
1096 retval = -EPIPE;
1097 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001098error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 spin_unlock_irqrestore (&ehci->lock, flags);
1100 return retval;
1101}
Balaji Rao90da0962007-11-22 01:58:14 +05301102
1103static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1104{
1105 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1106
1107 if (ehci_is_TDI(ehci))
1108 return;
1109 set_owner(ehci, --portnum, PORT_OWNER);
1110}
1111
Alan Stern3a311552008-05-20 16:58:29 -04001112static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1113{
1114 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1115 u32 __iomem *reg;
1116
1117 if (ehci_is_TDI(ehci))
1118 return 0;
1119 reg = &ehci->regs->port_status[portnum - 1];
1120 return ehci_readl(ehci, reg) & PORT_OWNER;
1121}