blob: c0b37fedfbc2b8ffaadfee08b72adf9b39abe56b [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;
Yin Kangkai148fc552011-01-28 12:04:35 +0800114 unsigned long flags;
Alan Stern16032c42010-05-12 18:21:35 -0400115
116 /* If remote wakeup is enabled for the root hub but disabled
117 * for the controller, we must adjust all the port wakeup flags
118 * when the controller is suspended or resumed. In all other
119 * cases they don't need to be changed.
120 */
Alan Stern41472002010-06-25 14:02:14 -0400121 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400122 return;
123
Yin Kangkai148fc552011-01-28 12:04:35 +0800124 spin_lock_irqsave(&ehci->lock, flags);
125
Alan Stern16032c42010-05-12 18:21:35 -0400126 /* clear phy low-power mode before changing wakeup flags */
127 if (ehci->has_hostpc) {
128 port = HCS_N_PORTS(ehci->hcs_params);
129 while (port--) {
130 u32 __iomem *hostpc_reg;
131
132 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
133 + HOSTPC0 + 4 * port);
134 temp = ehci_readl(ehci, hostpc_reg);
135 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
136 }
Yin Kangkai148fc552011-01-28 12:04:35 +0800137 spin_unlock_irqrestore(&ehci->lock, flags);
Alan Stern16032c42010-05-12 18:21:35 -0400138 msleep(5);
Yin Kangkai148fc552011-01-28 12:04:35 +0800139 spin_lock_irqsave(&ehci->lock, flags);
Alan Stern16032c42010-05-12 18:21:35 -0400140 }
141
142 port = HCS_N_PORTS(ehci->hcs_params);
143 while (port--) {
144 u32 __iomem *reg = &ehci->regs->port_status[port];
145 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
146 u32 t2 = t1 & ~PORT_WAKE_BITS;
147
148 /* If we are suspending the controller, clear the flags.
149 * If we are resuming the controller, set the wakeup flags.
150 */
151 if (!suspending) {
152 if (t1 & PORT_CONNECT)
153 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
154 else
155 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
156 }
157 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
158 port + 1, t1, t2);
159 ehci_writel(ehci, t2, reg);
160 }
161
162 /* enter phy low-power mode again */
163 if (ehci->has_hostpc) {
164 port = HCS_N_PORTS(ehci->hcs_params);
165 while (port--) {
166 u32 __iomem *hostpc_reg;
167
168 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
169 + HOSTPC0 + 4 * port);
170 temp = ehci_readl(ehci, hostpc_reg);
171 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
172 }
173 }
Alan Sternee0b9be2010-06-25 14:02:24 -0400174
175 /* Does the root hub have a port wakeup pending? */
176 if (!suspending && (ehci_readl(ehci, &ehci->regs->status) & STS_PCD))
177 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
Yin Kangkai148fc552011-01-28 12:04:35 +0800178
179 spin_unlock_irqrestore(&ehci->lock, flags);
Alan Stern16032c42010-05-12 18:21:35 -0400180}
181
Alan Stern0c0382e2005-10-13 17:08:02 -0400182static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
185 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500186 int mask;
Alan Stern16032c42010-05-12 18:21:35 -0400187 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Alan Stern8c774fe2007-02-01 16:09:59 -0500189 ehci_dbg(ehci, "suspend root hub\n");
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (time_before (jiffies, ehci->next_statechange))
192 msleep(5);
Alan Sternf8fa7572008-01-10 16:43:15 -0500193 del_timer_sync(&ehci->watchdog);
194 del_timer_sync(&ehci->iaa_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_lock_irq (&ehci->lock);
197
Alan Sterncec3a532010-01-08 11:18:20 -0500198 /* Once the controller is stopped, port resumes that are already
199 * in progress won't complete. Hence if remote wakeup is enabled
200 * for the root hub and any ports are in the middle of a resume or
201 * remote wakeup, we must fail the suspend.
202 */
203 if (hcd->self.root_hub->do_remote_wakeup) {
204 port = HCS_N_PORTS(ehci->hcs_params);
205 while (port--) {
206 if (ehci->reset_done[port] != 0) {
207 spin_unlock_irq(&ehci->lock);
208 ehci_dbg(ehci, "suspend failed because "
209 "port %d is resuming\n",
210 port + 1);
211 return -EBUSY;
212 }
213 }
214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* stop schedules, clean any completed work */
217 if (HC_IS_RUNNING(hcd->state)) {
218 ehci_quiesce (ehci);
219 hcd->state = HC_STATE_QUIESCING;
220 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100221 ehci->command = ehci_readl(ehci, &ehci->regs->command);
David Howells7d12e782006-10-05 14:55:46 +0100222 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Alan Stern8c033562006-11-09 14:42:16 -0500224 /* Unlike other USB host controller types, EHCI doesn't have
225 * any notion of "global" or bus-wide suspend. The driver has
226 * to manually suspend all the active unsuspended ports, and
227 * then manually resume them in the bus_resume() routine.
228 */
229 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400230 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400231 changed = 0;
Alan Sterncec3a532010-01-08 11:18:20 -0500232 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 while (port--) {
234 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100235 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400236 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alan Stern8c033562006-11-09 14:42:16 -0500238 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400239 if (t1 & PORT_OWNER)
240 set_bit(port, &ehci->owned_ports);
241 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500243 set_bit(port, &ehci->bus_suspended);
244 }
245
Alan Stern16032c42010-05-12 18:21:35 -0400246 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800247 if (hcd->self.root_hub->do_remote_wakeup) {
248 /* only enable appropriate wake bits, otherwise the
249 * hardware can not go phy low power mode. If a race
250 * condition happens here(connection change during bits
251 * set), the port change detection will finally fix it.
252 */
Alan Stern16032c42010-05-12 18:21:35 -0400253 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800254 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400255 else
Alek Du331ac6b2009-07-13 12:41:20 +0800256 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (t1 != t2) {
260 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
261 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100262 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400263 changed = 1;
264 }
265 }
Alek Du331ac6b2009-07-13 12:41:20 +0800266
Alan Stern16032c42010-05-12 18:21:35 -0400267 if (changed && ehci->has_hostpc) {
268 spin_unlock_irq(&ehci->lock);
269 msleep(5); /* 5 ms for HCD to enter low-power mode */
270 spin_lock_irq(&ehci->lock);
271
272 port = HCS_N_PORTS(ehci->hcs_params);
273 while (port--) {
274 u32 __iomem *hostpc_reg;
275 u32 t3;
276
277 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
278 + HOSTPC0 + 4 * port);
279 t3 = ehci_readl(ehci, hostpc_reg);
280 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
281 t3 = ehci_readl(ehci, hostpc_reg);
282 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800283 port, (t3 & HOSTPC_PHCD) ?
284 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
287
Alan Sterncd930c92008-01-10 11:14:53 -0500288 /* Apparently some devices need a >= 1-uframe delay here */
289 if (ehci->bus_suspended)
290 udelay(150);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* turn off now-idle HC */
293 ehci_halt (ehci);
294 hcd->state = HC_STATE_SUSPENDED;
295
David Brownellcdc647a2008-04-02 13:40:20 -0700296 if (ehci->reclaim)
297 end_unlink_async(ehci);
298
Alan Stern8c033562006-11-09 14:42:16 -0500299 /* allow remote wakeup */
300 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400301 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500302 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100303 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
304 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
307 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400308
309 /* ehci_work() may have re-enabled the watchdog timer, which we do not
310 * want, and so we must delete any pending watchdog timer events.
311 */
312 del_timer_sync(&ehci->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314}
315
316
317/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400318static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
321 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400322 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int i;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530324 u8 resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (time_before (jiffies, ehci->next_statechange))
327 msleep(5);
328 spin_lock_irq (&ehci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400329 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -0400330 spin_unlock_irq(&ehci->lock);
331 return -ESHUTDOWN;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jason Wesselad45f1d2009-08-20 15:39:58 -0500334 if (unlikely(ehci->debug)) {
Jason Wessel872d3592009-09-23 18:32:44 -0500335 if (!dbgp_reset_prep())
Jason Wesselad45f1d2009-08-20 15:39:58 -0500336 ehci->debug = NULL;
337 else
338 dbgp_external_startup();
339 }
340
David Brownellf03c17f2005-11-23 15:45:28 -0800341 /* Ideally and we've got a real resume here, and no port's power
342 * was lost. (For PCI, that means Vaux was maintained.) But we
343 * could instead be restoring a swsusp snapshot -- so that BIOS was
344 * the last user of the controller, not reset/pm hardware keeping
345 * state we gave to it.
346 */
Alan Stern383975d2007-05-04 11:52:40 -0400347 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
348 ehci_dbg(ehci, "resume root hub%s\n",
349 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800350
Alan Stern8c033562006-11-09 14:42:16 -0500351 /* at least some APM implementations will try to deliver
352 * IRQs right away, so delay them until we're ready.
353 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100354 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500355
356 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100357 ehci_writel(ehci, 0, &ehci->regs->segment);
358 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
359 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100362 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Alan Sterne198a312007-03-15 15:54:30 -0400364 /* Some controller/firmware combinations need a delay during which
365 * they set up the port statuses. See Bugzilla #8190. */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530366 spin_unlock_irq(&ehci->lock);
367 msleep(8);
368 spin_lock_irq(&ehci->lock);
Alan Sterne198a312007-03-15 15:54:30 -0400369
Alan Stern16032c42010-05-12 18:21:35 -0400370 /* clear phy low-power mode before resume */
371 if (ehci->bus_suspended && ehci->has_hostpc) {
372 i = HCS_N_PORTS(ehci->hcs_params);
373 while (i--) {
374 if (test_bit(i, &ehci->bus_suspended)) {
375 u32 __iomem *hostpc_reg;
376
377 hostpc_reg = (u32 __iomem *)((u8 *) ehci->regs
378 + HOSTPC0 + 4 * i);
379 temp = ehci_readl(ehci, hostpc_reg);
380 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
381 hostpc_reg);
382 }
383 }
384 spin_unlock_irq(&ehci->lock);
385 msleep(5);
386 spin_lock_irq(&ehci->lock);
387 }
388
Alan Stern8c033562006-11-09 14:42:16 -0500389 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 i = HCS_N_PORTS (ehci->hcs_params);
391 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100392 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400393 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500394 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530395 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 temp |= PORT_RESUME;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530397 resume_needed = 1;
398 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100399 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530401
402 /* msleep for 20ms only if code is trying to resume port */
403 if (resume_needed) {
404 spin_unlock_irq(&ehci->lock);
405 msleep(20);
406 spin_lock_irq(&ehci->lock);
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100411 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500412 if (test_bit(i, &ehci->bus_suspended) &&
413 (temp & PORT_SUSPEND)) {
414 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100415 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500416 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100419 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* maybe re-activate the schedule(s) */
422 temp = 0;
423 if (ehci->async->qh_next.qh)
424 temp |= CMD_ASE;
425 if (ehci->periodic_sched)
426 temp |= CMD_PSE;
427 if (temp) {
428 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100429 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
433 hcd->state = HC_STATE_RUNNING;
434
435 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100436 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 spin_unlock_irq (&ehci->lock);
Alan Stern3bb1af52008-03-03 15:15:36 -0500439 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441}
442
443#else
444
Alan Stern0c0382e2005-10-13 17:08:02 -0400445#define ehci_bus_suspend NULL
446#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448#endif /* CONFIG_PM */
449
450/*-------------------------------------------------------------------------*/
451
Alan Stern57e06c12007-01-16 11:59:45 -0500452/* Display the ports dedicated to the companion controller */
Tony Jones5a3201b2007-09-11 14:07:31 -0700453static ssize_t show_companion(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
Alan Stern57e06c12007-01-16 11:59:45 -0500456{
457 struct ehci_hcd *ehci;
458 int nports, index, n;
459 int count = PAGE_SIZE;
460 char *ptr = buf;
461
Tony Jones5a3201b2007-09-11 14:07:31 -0700462 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
Alan Stern57e06c12007-01-16 11:59:45 -0500463 nports = HCS_N_PORTS(ehci->hcs_params);
464
465 for (index = 0; index < nports; ++index) {
466 if (test_bit(index, &ehci->companion_ports)) {
467 n = scnprintf(ptr, count, "%d\n", index + 1);
468 ptr += n;
469 count -= n;
470 }
471 }
472 return ptr - buf;
473}
474
475/*
Balaji Rao90da0962007-11-22 01:58:14 +0530476 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500477 */
Balaji Rao90da0962007-11-22 01:58:14 +0530478static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500479{
Alan Stern57e06c12007-01-16 11:59:45 -0500480 u32 __iomem *status_reg;
481 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530482 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500483
Balaji Rao90da0962007-11-22 01:58:14 +0530484 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500485
486 /*
487 * The controller won't set the OWNER bit if the port is
488 * enabled, so this loop will sometimes require at least two
489 * iterations: one to disable the port and one to set OWNER.
490 */
Alan Stern57e06c12007-01-16 11:59:45 -0500491 for (try = 4; try > 0; --try) {
492 spin_lock_irq(&ehci->lock);
493 port_status = ehci_readl(ehci, status_reg);
494 if ((port_status & PORT_OWNER) == new_owner
495 || (port_status & (PORT_OWNER | PORT_CONNECT))
496 == 0)
497 try = 0;
498 else {
499 port_status ^= PORT_OWNER;
500 port_status &= ~(PORT_PE | PORT_RWC_BITS);
501 ehci_writel(ehci, port_status, status_reg);
502 }
503 spin_unlock_irq(&ehci->lock);
504 if (try > 1)
505 msleep(5);
506 }
Balaji Rao90da0962007-11-22 01:58:14 +0530507}
508
509/*
510 * Dedicate or undedicate a port to the companion controller.
511 * Syntax is "[-]portnum", where a leading '-' sign means
512 * return control of the port to the EHCI controller.
513 */
514static ssize_t store_companion(struct device *dev,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
517{
518 struct ehci_hcd *ehci;
519 int portnum, new_owner;
520
521 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
522 new_owner = PORT_OWNER; /* Owned by companion */
523 if (sscanf(buf, "%d", &portnum) != 1)
524 return -EINVAL;
525 if (portnum < 0) {
526 portnum = - portnum;
527 new_owner = 0; /* Owned by EHCI */
528 }
529 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
530 return -ENOENT;
531 portnum--;
532 if (new_owner)
533 set_bit(portnum, &ehci->companion_ports);
534 else
535 clear_bit(portnum, &ehci->companion_ports);
536 set_owner(ehci, portnum, new_owner);
Alan Stern57e06c12007-01-16 11:59:45 -0500537 return count;
538}
Tony Jones5a3201b2007-09-11 14:07:31 -0700539static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500540
David Daneyac8d6742011-01-25 09:59:37 -0800541static inline int create_companion_file(struct ehci_hcd *ehci)
Alan Stern57e06c12007-01-16 11:59:45 -0500542{
David Daneyac8d6742011-01-25 09:59:37 -0800543 int i = 0;
Alan Stern57e06c12007-01-16 11:59:45 -0500544
545 /* with integrated TT there is no companion! */
546 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700547 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700548 &dev_attr_companion);
David Daneyac8d6742011-01-25 09:59:37 -0800549 return i;
Alan Stern57e06c12007-01-16 11:59:45 -0500550}
551
552static inline void remove_companion_file(struct ehci_hcd *ehci)
553{
554 /* with integrated TT there is no companion! */
555 if (!ehci_is_TDI(ehci))
Greg Kroah-Hartmaned14f032009-04-27 13:15:38 -0700556 device_remove_file(ehci_to_hcd(ehci)->self.controller,
Tony Jones5a3201b2007-09-11 14:07:31 -0700557 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500558}
559
560
561/*-------------------------------------------------------------------------*/
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int check_reset_complete (
564 struct ehci_hcd *ehci,
565 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500566 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int port_status
568) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800569 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* if reset finished and it's still not enabled -- handoff */
573 if (!(port_status & PORT_PE)) {
574
575 /* with integrated TT, there's nobody to hand it to! */
576 if (ehci_is_TDI(ehci)) {
577 ehci_dbg (ehci,
578 "Failed to enable port %d on root hub TT\n",
579 index+1);
580 return port_status;
581 }
582
583 ehci_dbg (ehci, "port %d full speed --> companion\n",
584 index + 1);
585
586 // what happens if HCS_N_CC(params) == 0 ?
587 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700588 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500589 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100591 /* ensure 440EPX ohci controller state is operational */
592 if (ehci->has_amcc_usb23)
593 set_ohci_hcfs(ehci, 1);
594 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ehci_dbg (ehci, "port %d high speed\n", index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100596 /* ensure 440EPx ohci controller state is suspended */
597 if (ehci->has_amcc_usb23)
598 set_ohci_hcfs(ehci, 0);
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 return port_status;
602}
603
604/*-------------------------------------------------------------------------*/
605
606
607/* build "status change" packet (one or two bytes) from HC registers */
608
609static int
610ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
611{
612 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
613 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800614 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int ports, i, retval = 1;
616 unsigned long flags;
Alek Du5a9cdf32010-06-04 15:47:56 +0800617 u32 ppcd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
620 if (!HC_IS_RUNNING(hcd->state))
621 return 0;
622
623 /* init status to no-changes */
624 buf [0] = 0;
625 ports = HCS_N_PORTS (ehci->hcs_params);
626 if (ports > 7) {
627 buf [1] = 0;
628 retval++;
629 }
David Brownell53bd6a62006-08-30 14:50:06 -0700630
David Brownell93f1a472006-11-16 23:34:58 -0800631 /* Some boards (mostly VIA?) report bogus overcurrent indications,
632 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200633 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800634 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
635 * PORT_POWER; that's surprising, but maybe within-spec.
636 */
637 if (!ignore_oc)
638 mask = PORT_CSC | PORT_PEC | PORT_OCC;
639 else
640 mask = PORT_CSC | PORT_PEC;
641 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* no hub change reports (bit 0) for now (power, ...) */
644
645 /* port N changes (bit N)? */
646 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800647
648 /* get per-port change detect bits */
649 if (ehci->has_ppcd)
650 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800653 /* leverage per-port change bits feature */
654 if (ehci->has_ppcd && !(ppcd & (1 << i)))
655 continue;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100656 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500657
658 /*
659 * Return status information even for ports with OWNER set.
660 * Otherwise khubd wouldn't see the disconnect event when a
661 * high-speed device is switched over to the companion
662 * controller by the user.
663 */
664
Alan Sterneafe5b92008-10-06 11:25:53 -0400665 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
666 || (ehci->reset_done[i] && time_after_eq(
667 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (i < 7)
669 buf [0] |= 1 << (i + 1);
670 else
671 buf [1] |= 1 << (i - 7);
672 status = STS_PCD;
673 }
674 }
675 /* FIXME autosuspend idle root hubs */
676 spin_unlock_irqrestore (&ehci->lock, flags);
677 return status ? retval : 0;
678}
679
680/*-------------------------------------------------------------------------*/
681
682static void
683ehci_hub_descriptor (
684 struct ehci_hcd *ehci,
685 struct usb_hub_descriptor *desc
686) {
687 int ports = HCS_N_PORTS (ehci->hcs_params);
688 u16 temp;
689
690 desc->bDescriptorType = 0x29;
691 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
692 desc->bHubContrCurrent = 0;
693
694 desc->bNbrPorts = ports;
695 temp = 1 + (ports / 8);
696 desc->bDescLength = 7 + 2 * temp;
697
698 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
699 memset (&desc->bitmap [0], 0, temp);
700 memset (&desc->bitmap [temp], 0xff, temp);
701
702 temp = 0x0008; /* per-port overcurrent reporting */
703 if (HCS_PPC (ehci->hcs_params))
704 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700705 else
706 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#if 0
708// re-enable when we support USB_PORT_FEAT_INDICATOR below.
709 if (HCS_INDICATOR (ehci->hcs_params))
710 temp |= 0x0080; /* per-port indicators (LEDs) */
711#endif
Al Virofd05e722008-04-28 07:00:16 +0100712 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*-------------------------------------------------------------------------*/
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static int ehci_hub_control (
718 struct usb_hcd *hcd,
719 u16 typeReq,
720 u16 wValue,
721 u16 wIndex,
722 char *buf,
723 u16 wLength
724) {
725 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
726 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400727 u32 __iomem *status_reg = &ehci->regs->port_status[
728 (wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800729 u32 __iomem *hostpc_reg = NULL;
730 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned long flags;
732 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800733 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /*
736 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
737 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
738 * (track current state ourselves) ... blink for diagnostics,
739 * power, "this is the one", etc. EHCI spec supports this.
740 */
741
Alek Du331ac6b2009-07-13 12:41:20 +0800742 if (ehci->has_hostpc)
743 hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
744 + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_lock_irqsave (&ehci->lock, flags);
746 switch (typeReq) {
747 case ClearHubFeature:
748 switch (wValue) {
749 case C_HUB_LOCAL_POWER:
750 case C_HUB_OVER_CURRENT:
751 /* no hub-wide feature/status flags */
752 break;
753 default:
754 goto error;
755 }
756 break;
757 case ClearPortFeature:
758 if (!wIndex || wIndex > ports)
759 goto error;
760 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500761 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500762
763 /*
764 * Even if OWNER is set, so the port is owned by the
765 * companion controller, khubd needs to be able to clear
766 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500767 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 switch (wValue) {
771 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500772 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100775 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500776 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 case USB_PORT_FEAT_SUSPEND:
779 if (temp & PORT_RESET)
780 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800781 if (ehci->no_selective_suspend)
782 break;
Alan Stern16032c42010-05-12 18:21:35 -0400783 if (!(temp & PORT_SUSPEND))
784 break;
785 if ((temp & PORT_PE) == 0)
786 goto error;
787
788 /* clear phy low-power mode before resume */
789 if (hostpc_reg) {
790 temp1 = ehci_readl(ehci, hostpc_reg);
791 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800792 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400793 spin_unlock_irqrestore(&ehci->lock, flags);
794 msleep(5);/* wait to leave low-power mode */
795 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Alan Stern16032c42010-05-12 18:21:35 -0400797 /* resume signaling for 20 msec */
798 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
799 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
800 ehci->reset_done[wIndex] = jiffies
801 + msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400804 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 case USB_PORT_FEAT_POWER:
807 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100808 ehci_writel(ehci,
809 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500810 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812 case USB_PORT_FEAT_C_CONNECTION:
Alek Du48f24972010-06-04 15:47:55 +0800813 if (ehci->has_lpm) {
814 /* clear PORTSC bits on disconnect */
815 temp &= ~PORT_LPM;
816 temp &= ~PORT_DEV_ADDR;
817 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100818 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500819 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100822 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500823 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 break;
825 case USB_PORT_FEAT_C_RESET:
826 /* GetPortStatus clears reset */
827 break;
828 default:
829 goto error;
830 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100831 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
833 case GetHubDescriptor:
834 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
835 buf);
836 break;
837 case GetHubStatus:
838 /* no hub-wide feature/status flags */
839 memset (buf, 0, 4);
840 //cpu_to_le32s ((u32 *) buf);
841 break;
842 case GetPortStatus:
843 if (!wIndex || wIndex > ports)
844 goto error;
845 wIndex--;
846 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500847 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 // wPortChange bits
850 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500851 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500853 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700854
855 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500856 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700858 /*
859 * Hubs should disable port power on over-current.
860 * However, not all EHCI implementations do this
861 * automatically, even if they _do_ support per-port
862 * power switching; they're allowed to just limit the
863 * current. khubd will turn the power back on.
864 */
865 if (HCS_PPC (ehci->hcs_params)){
866 ehci_writel(ehci,
867 temp & ~(PORT_RWC_BITS | PORT_POWER),
868 status_reg);
869 }
870 }
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500873 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Alan Stern629e4422007-01-22 16:08:53 -0500875 /* Remote Wakeup received? */
876 if (!ehci->reset_done[wIndex]) {
877 /* resume signaling for 20 msec */
878 ehci->reset_done[wIndex] = jiffies
879 + msecs_to_jiffies(20);
880 /* check the port again */
881 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
882 ehci->reset_done[wIndex]);
883 }
884
885 /* resume completed? */
886 else if (time_after_eq(jiffies,
887 ehci->reset_done[wIndex])) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400888 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sternd1f114d2008-05-20 16:58:58 -0400889 set_bit(wIndex, &ehci->port_c_suspend);
Alan Stern629e4422007-01-22 16:08:53 -0500890 ehci->reset_done[wIndex] = 0;
891
892 /* stop resume signaling */
893 temp = ehci_readl(ehci, status_reg);
894 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500895 temp & ~(PORT_RWC_BITS | PORT_RESUME),
896 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500897 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100898 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500899 if (retval != 0) {
900 ehci_err(ehci,
901 "port %d resume error %d\n",
902 wIndex + 1, retval);
903 goto error;
904 }
905 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 /* whoever resets must GetPortStatus to complete it!! */
910 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500911 && time_after_eq(jiffies,
912 ehci->reset_done[wIndex])) {
Alan Stern749da5f2010-03-04 17:05:08 -0500913 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 ehci->reset_done [wIndex] = 0;
915
916 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100917 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500918 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700919 /* REVISIT: some hardware needs 550+ usec to clear
920 * this bit; seems too long to spin routinely...
921 */
Alan Sterne6316562007-01-16 11:58:00 -0500922 retval = handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -0500923 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (retval != 0) {
925 ehci_err (ehci, "port %d reset error %d\n",
926 wIndex + 1, retval);
927 goto error;
928 }
929
930 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500931 temp = check_reset_complete (ehci, wIndex, status_reg,
932 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Alan Sterneafe5b92008-10-06 11:25:53 -0400935 if (!(temp & (PORT_RESUME|PORT_RESET)))
936 ehci->reset_done[wIndex] = 0;
937
Alan Stern57e06c12007-01-16 11:59:45 -0500938 /* transfer dedicated ports to the companion hc */
939 if ((temp & PORT_CONNECT) &&
940 test_bit(wIndex, &ehci->companion_ports)) {
941 temp &= ~PORT_RWC_BITS;
942 temp |= PORT_OWNER;
943 ehci_writel(ehci, temp, status_reg);
944 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
945 temp = ehci_readl(ehci, status_reg);
946 }
947
Alan Stern625b5c92007-01-16 11:58:47 -0500948 /*
949 * Even if OWNER is set, there's no harm letting khubd
950 * see the wPortStatus values (they should all be 0 except
951 * for PORT_POWER anyway).
952 */
953
954 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500955 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -0500956 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +0800957 if (ehci->has_hostpc) {
958 temp1 = ehci_readl(ehci, hostpc_reg);
959 status |= ehci_port_speed(ehci, temp1);
960 } else
961 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Alan Stern625b5c92007-01-16 11:58:47 -0500963 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500964 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -0400965
966 /* maybe the port was unsuspended without our knowledge */
967 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -0500968 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -0400969 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
970 clear_bit(wIndex, &ehci->suspended_ports);
971 ehci->reset_done[wIndex] = 0;
972 if (temp & PORT_PE)
973 set_bit(wIndex, &ehci->port_c_suspend);
974 }
975
Alan Stern625b5c92007-01-16 11:58:47 -0500976 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500977 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -0500978 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500979 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -0500980 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500981 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -0400982 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -0500983 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
David Brownell9776afc2008-02-01 11:42:05 -0800985#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (status & ~0xffff) /* only if wPortChange is interesting */
987#endif
988 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700989 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 case SetHubFeature:
992 switch (wValue) {
993 case C_HUB_LOCAL_POWER:
994 case C_HUB_OVER_CURRENT:
995 /* no hub-wide feature/status flags */
996 break;
997 default:
998 goto error;
999 }
1000 break;
1001 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -08001002 selector = wIndex >> 8;
1003 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -05001004 if (unlikely(ehci->debug)) {
1005 /* If the debug port is active any port
1006 * feature requests should get denied */
1007 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1008 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1009 retval = -ENODEV;
1010 goto error_exit;
1011 }
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!wIndex || wIndex > ports)
1014 goto error;
1015 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -05001016 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (temp & PORT_OWNER)
1018 break;
1019
David Brownell10f65242005-08-31 10:55:38 -07001020 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 switch (wValue) {
1022 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -08001023 if (ehci->no_selective_suspend)
1024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if ((temp & PORT_PE) == 0
1026 || (temp & PORT_RESET) != 0)
1027 goto error;
Alek Dub9df7942010-01-19 16:31:31 +08001028
Alek Du331ac6b2009-07-13 12:41:20 +08001029 /* After above check the port must be connected.
1030 * Set appropriate bit thus could put phy into low power
1031 * mode if we have hostpc feature
1032 */
Alek Dub9df7942010-01-19 16:31:31 +08001033 temp &= ~PORT_WKCONN_E;
1034 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1035 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Alek Du331ac6b2009-07-13 12:41:20 +08001036 if (hostpc_reg) {
Alek Dub9df7942010-01-19 16:31:31 +08001037 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001038 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df7942010-01-19 16:31:31 +08001039 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +08001040 temp1 = ehci_readl(ehci, hostpc_reg);
1041 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1042 hostpc_reg);
1043 temp1 = ehci_readl(ehci, hostpc_reg);
1044 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1045 wIndex, (temp1 & HOSTPC_PHCD) ?
1046 "succeeded" : "failed");
1047 }
Alan Sterneafe5b92008-10-06 11:25:53 -04001048 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case USB_PORT_FEAT_POWER:
1051 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001052 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -05001053 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
1055 case USB_PORT_FEAT_RESET:
1056 if (temp & PORT_RESUME)
1057 goto error;
1058 /* line status bits may report this as low speed,
1059 * which can be fine if this root hub has a
1060 * transaction translator built in.
1061 */
1062 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1063 && !ehci_is_TDI(ehci)
1064 && PORT_USB11 (temp)) {
1065 ehci_dbg (ehci,
1066 "port %d low speed --> companion\n",
1067 wIndex + 1);
1068 temp |= PORT_OWNER;
1069 } else {
1070 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1071 temp |= PORT_RESET;
1072 temp &= ~PORT_PE;
1073
1074 /*
1075 * caller must wait, then call GetPortStatus
1076 * usb 2.0 spec says 50 ms resets on root
1077 */
1078 ehci->reset_done [wIndex] = jiffies
1079 + msecs_to_jiffies (50);
1080 }
Alan Sterne6316562007-01-16 11:58:00 -05001081 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001083
1084 /* For downstream facing ports (these): one hub port is put
1085 * into test mode according to USB2 11.24.2.13, then the hub
1086 * must be reset (which for root hub now means rmmod+modprobe,
1087 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1088 * about the EHCI-specific stuff.
1089 */
1090 case USB_PORT_FEAT_TEST:
1091 if (!selector || selector > 5)
1092 goto error;
1093 ehci_quiesce(ehci);
1094 ehci_halt(ehci);
1095 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001096 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001097 break;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 default:
1100 goto error;
1101 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001102 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 break;
1104
1105 default:
1106error:
1107 /* "stall" on error */
1108 retval = -EPIPE;
1109 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001110error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 spin_unlock_irqrestore (&ehci->lock, flags);
1112 return retval;
1113}
Balaji Rao90da0962007-11-22 01:58:14 +05301114
1115static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1116{
1117 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1118
1119 if (ehci_is_TDI(ehci))
1120 return;
1121 set_owner(ehci, --portnum, PORT_OWNER);
1122}
1123
Alan Stern3a311552008-05-20 16:58:29 -04001124static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1125{
1126 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1127 u32 __iomem *reg;
1128
1129 if (ehci_is_TDI(ehci))
1130 return 0;
1131 reg = &ehci->regs->port_status[portnum - 1];
1132 return ehci_readl(ehci, reg) & PORT_OWNER;
1133}