blob: 914ce9370e70f4a1d9a956ccf50fae9b97973b21 [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/*-------------------------------------------------------------------------*/
Anatolij Gustschin83722bc2011-04-18 22:02:00 +020030#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alan Stern58a97ff2008-04-14 12:17:10 -040032#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33
Alan Sternaff6d182008-04-18 11:11:26 -040034#ifdef CONFIG_PM
35
Alan Stern383975d2007-05-04 11:52:40 -040036static int ehci_hub_control(
37 struct usb_hcd *hcd,
38 u16 typeReq,
39 u16 wValue,
40 u16 wIndex,
41 char *buf,
42 u16 wLength
43);
44
45/* After a power loss, ports that were owned by the companion must be
46 * reset so that the companion can still own them.
47 */
48static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
49{
50 u32 __iomem *reg;
51 u32 status;
52 int port;
53 __le32 buf;
54 struct usb_hcd *hcd = ehci_to_hcd(ehci);
55
56 if (!ehci->owned_ports)
57 return;
58
59 /* Give the connections some time to appear */
60 msleep(20);
61
Alan Sternc4f34762012-07-11 11:23:10 -040062 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040063 port = HCS_N_PORTS(ehci->hcs_params);
64 while (port--) {
65 if (test_bit(port, &ehci->owned_ports)) {
66 reg = &ehci->regs->port_status[port];
Alan Stern3c519b82007-05-04 11:55:31 -040067 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern383975d2007-05-04 11:52:40 -040068
69 /* Port already owned by companion? */
70 if (status & PORT_OWNER)
71 clear_bit(port, &ehci->owned_ports);
Alan Stern3c519b82007-05-04 11:55:31 -040072 else if (test_bit(port, &ehci->companion_ports))
73 ehci_writel(ehci, status & ~PORT_PE, reg);
Alan Sternc4f34762012-07-11 11:23:10 -040074 else {
75 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040076 ehci_hub_control(hcd, SetPortFeature,
77 USB_PORT_FEAT_RESET, port + 1,
78 NULL, 0);
Alan Sternc4f34762012-07-11 11:23:10 -040079 spin_lock_irq(&ehci->lock);
80 }
Alan Stern383975d2007-05-04 11:52:40 -040081 }
82 }
Alan Sternc4f34762012-07-11 11:23:10 -040083 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040084
85 if (!ehci->owned_ports)
86 return;
87 msleep(90); /* Wait for resets to complete */
88
Alan Sternc4f34762012-07-11 11:23:10 -040089 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040090 port = HCS_N_PORTS(ehci->hcs_params);
91 while (port--) {
92 if (test_bit(port, &ehci->owned_ports)) {
Alan Sternc4f34762012-07-11 11:23:10 -040093 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040094 ehci_hub_control(hcd, GetPortStatus,
95 0, port + 1,
96 (char *) &buf, sizeof(buf));
Alan Sternc4f34762012-07-11 11:23:10 -040097 spin_lock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -040098
99 /* The companion should now own the port,
100 * but if something went wrong the port must not
101 * remain enabled.
102 */
103 reg = &ehci->regs->port_status[port];
104 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
105 if (status & PORT_OWNER)
106 ehci_writel(ehci, status | PORT_CSC, reg);
107 else {
108 ehci_dbg(ehci, "failed handover port %d: %x\n",
109 port + 1, status);
110 ehci_writel(ehci, status & ~PORT_PE, reg);
111 }
112 }
113 }
114
115 ehci->owned_ports = 0;
Alan Sternc4f34762012-07-11 11:23:10 -0400116 spin_unlock_irq(&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400117}
118
Alan Sternc5cf9212012-06-28 11:19:02 -0400119static int ehci_port_change(struct ehci_hcd *ehci)
Matthew Garrett294d95f2011-01-11 12:26:48 -0500120{
121 int i = HCS_N_PORTS(ehci->hcs_params);
122
123 /* First check if the controller indicates a change event */
124
125 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
126 return 1;
127
128 /*
129 * Not all controllers appear to update this while going from D3 to D0,
130 * so check the individual port status registers as well
131 */
132
133 while (i--)
134 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
135 return 1;
136
137 return 0;
138}
139
Alan Sternc5cf9212012-06-28 11:19:02 -0400140static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
Alan Stern41472002010-06-25 14:02:14 -0400141 bool suspending, bool do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400142{
143 int port;
144 u32 temp;
145
146 /* If remote wakeup is enabled for the root hub but disabled
147 * for the controller, we must adjust all the port wakeup flags
148 * when the controller is suspended or resumed. In all other
149 * cases they don't need to be changed.
150 */
Alan Stern41472002010-06-25 14:02:14 -0400151 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
Alan Stern16032c42010-05-12 18:21:35 -0400152 return;
153
Alan Sternc4f34762012-07-11 11:23:10 -0400154 spin_lock_irq(&ehci->lock);
Yin Kangkai148fc552011-01-28 12:04:35 +0800155
Alan Stern16032c42010-05-12 18:21:35 -0400156 /* clear phy low-power mode before changing wakeup flags */
157 if (ehci->has_hostpc) {
158 port = HCS_N_PORTS(ehci->hcs_params);
159 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400160 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400161
Alan Stern16032c42010-05-12 18:21:35 -0400162 temp = ehci_readl(ehci, hostpc_reg);
163 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
164 }
Alan Sternc4f34762012-07-11 11:23:10 -0400165 spin_unlock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400166 msleep(5);
Alan Sternc4f34762012-07-11 11:23:10 -0400167 spin_lock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400168 }
169
170 port = HCS_N_PORTS(ehci->hcs_params);
171 while (port--) {
172 u32 __iomem *reg = &ehci->regs->port_status[port];
173 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
174 u32 t2 = t1 & ~PORT_WAKE_BITS;
175
176 /* If we are suspending the controller, clear the flags.
177 * If we are resuming the controller, set the wakeup flags.
178 */
179 if (!suspending) {
180 if (t1 & PORT_CONNECT)
181 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
182 else
183 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
184 }
185 ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
186 port + 1, t1, t2);
187 ehci_writel(ehci, t2, reg);
188 }
189
190 /* enter phy low-power mode again */
191 if (ehci->has_hostpc) {
192 port = HCS_N_PORTS(ehci->hcs_params);
193 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400194 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400195
Alan Stern16032c42010-05-12 18:21:35 -0400196 temp = ehci_readl(ehci, hostpc_reg);
197 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
198 }
199 }
Alan Sternee0b9be2010-06-25 14:02:24 -0400200
201 /* Does the root hub have a port wakeup pending? */
Matthew Garrett294d95f2011-01-11 12:26:48 -0500202 if (!suspending && ehci_port_change(ehci))
Alan Sternee0b9be2010-06-25 14:02:24 -0400203 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
Yin Kangkai148fc552011-01-28 12:04:35 +0800204
Alan Sternc4f34762012-07-11 11:23:10 -0400205 spin_unlock_irq(&ehci->lock);
Alan Stern16032c42010-05-12 18:21:35 -0400206}
207
Alan Stern0c0382e2005-10-13 17:08:02 -0400208static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
211 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500212 int mask;
Alan Stern16032c42010-05-12 18:21:35 -0400213 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Alan Stern8c774fe2007-02-01 16:09:59 -0500215 ehci_dbg(ehci, "suspend root hub\n");
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (time_before (jiffies, ehci->next_statechange))
218 msleep(5);
219
Alan Sternc4f34762012-07-11 11:23:10 -0400220 /* stop the schedules */
221 ehci_quiesce(ehci);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_lock_irq (&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400224 if (ehci->rh_state < EHCI_RH_RUNNING)
225 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Sterncec3a532010-01-08 11:18:20 -0500227 /* Once the controller is stopped, port resumes that are already
228 * in progress won't complete. Hence if remote wakeup is enabled
229 * for the root hub and any ports are in the middle of a resume or
230 * remote wakeup, we must fail the suspend.
231 */
232 if (hcd->self.root_hub->do_remote_wakeup) {
Alan Sterna448e4d2012-04-03 15:24:30 -0400233 if (ehci->resuming_ports) {
234 spin_unlock_irq(&ehci->lock);
235 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
236 return -EBUSY;
Alan Sterncec3a532010-01-08 11:18:20 -0500237 }
238 }
239
Alan Stern8c033562006-11-09 14:42:16 -0500240 /* Unlike other USB host controller types, EHCI doesn't have
241 * any notion of "global" or bus-wide suspend. The driver has
242 * to manually suspend all the active unsuspended ports, and
243 * then manually resume them in the bus_resume() routine.
244 */
245 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400246 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400247 changed = 0;
Alan Sterncec3a532010-01-08 11:18:20 -0500248 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 while (port--) {
250 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100251 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400252 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alan Stern8c033562006-11-09 14:42:16 -0500254 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400255 if (t1 & PORT_OWNER)
256 set_bit(port, &ehci->owned_ports);
257 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500259 set_bit(port, &ehci->bus_suspended);
260 }
261
Alan Stern16032c42010-05-12 18:21:35 -0400262 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800263 if (hcd->self.root_hub->do_remote_wakeup) {
264 /* only enable appropriate wake bits, otherwise the
265 * hardware can not go phy low power mode. If a race
266 * condition happens here(connection change during bits
267 * set), the port change detection will finally fix it.
268 */
Alan Stern16032c42010-05-12 18:21:35 -0400269 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800270 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400271 else
Alek Du331ac6b2009-07-13 12:41:20 +0800272 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 if (t1 != t2) {
276 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
277 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100278 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400279 changed = 1;
280 }
281 }
Alek Du331ac6b2009-07-13 12:41:20 +0800282
Alan Stern16032c42010-05-12 18:21:35 -0400283 if (changed && ehci->has_hostpc) {
284 spin_unlock_irq(&ehci->lock);
285 msleep(5); /* 5 ms for HCD to enter low-power mode */
286 spin_lock_irq(&ehci->lock);
287
288 port = HCS_N_PORTS(ehci->hcs_params);
289 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400290 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400291 u32 t3;
292
Alan Stern16032c42010-05-12 18:21:35 -0400293 t3 = ehci_readl(ehci, hostpc_reg);
294 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
295 t3 = ehci_readl(ehci, hostpc_reg);
296 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800297 port, (t3 & HOSTPC_PHCD) ?
298 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 }
Alan Sternc4f34762012-07-11 11:23:10 -0400301 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Alan Sterncd930c92008-01-10 11:14:53 -0500303 /* Apparently some devices need a >= 1-uframe delay here */
304 if (ehci->bus_suspended)
305 udelay(150);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* turn off now-idle HC */
308 ehci_halt (ehci);
Alan Sternc4f34762012-07-11 11:23:10 -0400309
310 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400311 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
312 ehci_handle_controller_death(ehci);
313 if (ehci->rh_state != EHCI_RH_RUNNING)
314 goto done;
Alan Sterne8799902011-08-18 16:31:30 -0400315 ehci->rh_state = EHCI_RH_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Alan Stern3c273a02012-07-11 11:22:49 -0400317 end_unlink_async(ehci);
Alan Stern32830f22012-07-11 11:22:53 -0400318 unlink_empty_async(ehci);
Alan Sterndf202252012-07-11 11:22:26 -0400319 ehci_handle_intr_unlinks(ehci);
Alan Stern55934eb2012-07-11 11:22:35 -0400320 end_free_itds(ehci);
David Brownellcdc647a2008-04-02 13:40:20 -0700321
Alan Stern8c033562006-11-09 14:42:16 -0500322 /* allow remote wakeup */
323 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400324 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500325 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100326 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
327 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500328
Alan Stern43fe3a92012-07-11 11:23:16 -0400329 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
Alan Sternd58b4bc2012-07-11 11:21:54 -0400331 ehci->enabled_hrtimer_events = 0;
332 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400334
Alan Sternd58b4bc2012-07-11 11:21:54 -0400335 hrtimer_cancel(&ehci->hrtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
339
340/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400341static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
344 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400345 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int i;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800347 unsigned long resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (time_before (jiffies, ehci->next_statechange))
350 msleep(5);
351 spin_lock_irq (&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400352 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
353 goto shutdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jason Wesselad45f1d2009-08-20 15:39:58 -0500355 if (unlikely(ehci->debug)) {
Jan Beulich9fa57802012-09-18 12:23:02 +0100356 if (!dbgp_reset_prep(hcd))
Jason Wesselad45f1d2009-08-20 15:39:58 -0500357 ehci->debug = NULL;
358 else
Jan Beulich9fa57802012-09-18 12:23:02 +0100359 dbgp_external_startup(hcd);
Jason Wesselad45f1d2009-08-20 15:39:58 -0500360 }
361
David Brownellf03c17f2005-11-23 15:45:28 -0800362 /* Ideally and we've got a real resume here, and no port's power
363 * was lost. (For PCI, that means Vaux was maintained.) But we
364 * could instead be restoring a swsusp snapshot -- so that BIOS was
365 * the last user of the controller, not reset/pm hardware keeping
366 * state we gave to it.
367 */
Alan Stern383975d2007-05-04 11:52:40 -0400368 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
369 ehci_dbg(ehci, "resume root hub%s\n",
370 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800371
Alan Stern8c033562006-11-09 14:42:16 -0500372 /* at least some APM implementations will try to deliver
373 * IRQs right away, so delay them until we're ready.
374 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100375 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500376
377 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100378 ehci_writel(ehci, 0, &ehci->regs->segment);
379 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
380 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* restore CMD_RUN, framelist size, and irq threshold */
Alan Stern3d9545c2012-04-23 13:54:36 -0400383 ehci->command |= CMD_RUN;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100384 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Alan Sterne8799902011-08-18 16:31:30 -0400385 ehci->rh_state = EHCI_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Sterne198a312007-03-15 15:54:30 -0400387 /* Some controller/firmware combinations need a delay during which
388 * they set up the port statuses. See Bugzilla #8190. */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530389 spin_unlock_irq(&ehci->lock);
390 msleep(8);
391 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400392 if (ehci->shutdown)
393 goto shutdown;
Alan Sterne198a312007-03-15 15:54:30 -0400394
Alan Stern16032c42010-05-12 18:21:35 -0400395 /* clear phy low-power mode before resume */
396 if (ehci->bus_suspended && ehci->has_hostpc) {
397 i = HCS_N_PORTS(ehci->hcs_params);
398 while (i--) {
399 if (test_bit(i, &ehci->bus_suspended)) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400400 u32 __iomem *hostpc_reg =
401 &ehci->regs->hostpc[i];
Alan Stern16032c42010-05-12 18:21:35 -0400402
Alan Stern16032c42010-05-12 18:21:35 -0400403 temp = ehci_readl(ehci, hostpc_reg);
404 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
405 hostpc_reg);
406 }
407 }
408 spin_unlock_irq(&ehci->lock);
409 msleep(5);
410 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400411 if (ehci->shutdown)
412 goto shutdown;
Alan Stern16032c42010-05-12 18:21:35 -0400413 }
414
Alan Stern8c033562006-11-09 14:42:16 -0500415 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 i = HCS_N_PORTS (ehci->hcs_params);
417 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100418 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400419 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500420 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530421 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 temp |= PORT_RESUME;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800423 set_bit(i, &resume_needed);
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530424 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100425 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530427
428 /* msleep for 20ms only if code is trying to resume port */
429 if (resume_needed) {
430 spin_unlock_irq(&ehci->lock);
431 msleep(20);
432 spin_lock_irq(&ehci->lock);
Alan Stern43fe3a92012-07-11 11:23:16 -0400433 if (ehci->shutdown)
434 goto shutdown;
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100439 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Wang Zhid0f2fb22011-08-17 10:39:31 +0800440 if (test_bit(i, &resume_needed)) {
Alan Stern8c033562006-11-09 14:42:16 -0500441 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100442 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500443 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
Alan Sternc4f34762012-07-11 11:23:10 -0400448 spin_unlock_irq(&ehci->lock);
449
450 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Now we can safely re-enable irqs */
Alan Stern43fe3a92012-07-11 11:23:16 -0400453 spin_lock_irq(&ehci->lock);
454 if (ehci->shutdown)
455 goto shutdown;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100456 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Alan Stern15be1052012-07-11 11:21:17 -0400457 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern43fe3a92012-07-11 11:23:16 -0400458 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
Alan Stern43fe3a92012-07-11 11:23:16 -0400461
462 shutdown:
463 spin_unlock_irq(&ehci->lock);
464 return -ESHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467#else
468
Alan Stern0c0382e2005-10-13 17:08:02 -0400469#define ehci_bus_suspend NULL
470#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472#endif /* CONFIG_PM */
473
474/*-------------------------------------------------------------------------*/
475
Alan Stern57e06c12007-01-16 11:59:45 -0500476/*
Balaji Rao90da0962007-11-22 01:58:14 +0530477 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500478 */
Balaji Rao90da0962007-11-22 01:58:14 +0530479static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500480{
Alan Stern57e06c12007-01-16 11:59:45 -0500481 u32 __iomem *status_reg;
482 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530483 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500484
Balaji Rao90da0962007-11-22 01:58:14 +0530485 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500486
487 /*
488 * The controller won't set the OWNER bit if the port is
489 * enabled, so this loop will sometimes require at least two
490 * iterations: one to disable the port and one to set OWNER.
491 */
Alan Stern57e06c12007-01-16 11:59:45 -0500492 for (try = 4; try > 0; --try) {
493 spin_lock_irq(&ehci->lock);
494 port_status = ehci_readl(ehci, status_reg);
495 if ((port_status & PORT_OWNER) == new_owner
496 || (port_status & (PORT_OWNER | PORT_CONNECT))
497 == 0)
498 try = 0;
499 else {
500 port_status ^= PORT_OWNER;
501 port_status &= ~(PORT_PE | PORT_RWC_BITS);
502 ehci_writel(ehci, port_status, status_reg);
503 }
504 spin_unlock_irq(&ehci->lock);
505 if (try > 1)
506 msleep(5);
507 }
Balaji Rao90da0962007-11-22 01:58:14 +0530508}
509
Alan Stern57e06c12007-01-16 11:59:45 -0500510/*-------------------------------------------------------------------------*/
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static int check_reset_complete (
513 struct ehci_hcd *ehci,
514 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500515 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int port_status
517) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800518 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* if reset finished and it's still not enabled -- handoff */
522 if (!(port_status & PORT_PE)) {
523
524 /* with integrated TT, there's nobody to hand it to! */
525 if (ehci_is_TDI(ehci)) {
526 ehci_dbg (ehci,
527 "Failed to enable port %d on root hub TT\n",
528 index+1);
529 return port_status;
530 }
531
532 ehci_dbg (ehci, "port %d full speed --> companion\n",
533 index + 1);
534
535 // what happens if HCS_N_CC(params) == 0 ?
536 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700537 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500538 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100540 /* ensure 440EPX ohci controller state is operational */
541 if (ehci->has_amcc_usb23)
542 set_ohci_hcfs(ehci, 1);
543 } else {
Peter Chen0ca7eb22012-03-29 09:50:06 +0800544 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
545 index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100546 /* ensure 440EPx ohci controller state is suspended */
547 if (ehci->has_amcc_usb23)
548 set_ohci_hcfs(ehci, 0);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return port_status;
552}
553
554/*-------------------------------------------------------------------------*/
555
556
557/* build "status change" packet (one or two bytes) from HC registers */
558
559static int
560ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
561{
562 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
Alan Sterna448e4d2012-04-03 15:24:30 -0400563 u32 temp, status;
David Brownell93f1a472006-11-16 23:34:58 -0800564 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int ports, i, retval = 1;
566 unsigned long flags;
Alek Du5a9cdf32010-06-04 15:47:56 +0800567 u32 ppcd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* init status to no-changes */
570 buf [0] = 0;
571 ports = HCS_N_PORTS (ehci->hcs_params);
572 if (ports > 7) {
573 buf [1] = 0;
574 retval++;
575 }
David Brownell53bd6a62006-08-30 14:50:06 -0700576
Alan Sterna448e4d2012-04-03 15:24:30 -0400577 /* Inform the core about resumes-in-progress by returning
578 * a non-zero value even if there are no status changes.
579 */
580 status = ehci->resuming_ports;
581
David Brownell93f1a472006-11-16 23:34:58 -0800582 /* Some boards (mostly VIA?) report bogus overcurrent indications,
583 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200584 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800585 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
586 * PORT_POWER; that's surprising, but maybe within-spec.
587 */
588 if (!ignore_oc)
589 mask = PORT_CSC | PORT_PEC | PORT_OCC;
590 else
591 mask = PORT_CSC | PORT_PEC;
592 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* no hub change reports (bit 0) for now (power, ...) */
595
596 /* port N changes (bit N)? */
597 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800598
599 /* get per-port change detect bits */
600 if (ehci->has_ppcd)
601 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800604 /* leverage per-port change bits feature */
605 if (ehci->has_ppcd && !(ppcd & (1 << i)))
606 continue;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100607 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500608
609 /*
610 * Return status information even for ports with OWNER set.
611 * Otherwise khubd wouldn't see the disconnect event when a
612 * high-speed device is switched over to the companion
613 * controller by the user.
614 */
615
Alan Sterneafe5b92008-10-06 11:25:53 -0400616 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
617 || (ehci->reset_done[i] && time_after_eq(
618 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (i < 7)
620 buf [0] |= 1 << (i + 1);
621 else
622 buf [1] |= 1 << (i - 7);
623 status = STS_PCD;
624 }
625 }
626 /* FIXME autosuspend idle root hubs */
627 spin_unlock_irqrestore (&ehci->lock, flags);
628 return status ? retval : 0;
629}
630
631/*-------------------------------------------------------------------------*/
632
633static void
634ehci_hub_descriptor (
635 struct ehci_hcd *ehci,
636 struct usb_hub_descriptor *desc
637) {
638 int ports = HCS_N_PORTS (ehci->hcs_params);
639 u16 temp;
640
641 desc->bDescriptorType = 0x29;
642 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
643 desc->bHubContrCurrent = 0;
644
645 desc->bNbrPorts = ports;
646 temp = 1 + (ports / 8);
647 desc->bDescLength = 7 + 2 * temp;
648
649 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -0700650 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
651 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 temp = 0x0008; /* per-port overcurrent reporting */
654 if (HCS_PPC (ehci->hcs_params))
655 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700656 else
657 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#if 0
659// re-enable when we support USB_PORT_FEAT_INDICATOR below.
660 if (HCS_INDICATOR (ehci->hcs_params))
661 temp |= 0x0080; /* per-port indicators (LEDs) */
662#endif
Al Virofd05e722008-04-28 07:00:16 +0100663 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*-------------------------------------------------------------------------*/
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int ehci_hub_control (
669 struct usb_hcd *hcd,
670 u16 typeReq,
671 u16 wValue,
672 u16 wIndex,
673 char *buf,
674 u16 wLength
675) {
676 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
677 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400678 u32 __iomem *status_reg = &ehci->regs->port_status[
679 (wIndex & 0xff) - 1];
Alan Sterna46af4e2012-06-25 12:19:03 -0400680 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800681 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned long flags;
683 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800684 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /*
687 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
688 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
689 * (track current state ourselves) ... blink for diagnostics,
690 * power, "this is the one", etc. EHCI spec supports this.
691 */
692
693 spin_lock_irqsave (&ehci->lock, flags);
694 switch (typeReq) {
695 case ClearHubFeature:
696 switch (wValue) {
697 case C_HUB_LOCAL_POWER:
698 case C_HUB_OVER_CURRENT:
699 /* no hub-wide feature/status flags */
700 break;
701 default:
702 goto error;
703 }
704 break;
705 case ClearPortFeature:
706 if (!wIndex || wIndex > ports)
707 goto error;
708 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500709 temp = ehci_readl(ehci, status_reg);
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600710 temp &= ~PORT_RWC_BITS;
Alan Stern625b5c92007-01-16 11:58:47 -0500711
712 /*
713 * Even if OWNER is set, so the port is owned by the
714 * companion controller, khubd needs to be able to clear
715 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500716 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 switch (wValue) {
720 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500721 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 case USB_PORT_FEAT_C_ENABLE:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600724 ehci_writel(ehci, temp | PORT_PEC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 case USB_PORT_FEAT_SUSPEND:
727 if (temp & PORT_RESET)
728 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800729 if (ehci->no_selective_suspend)
730 break;
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200731#ifdef CONFIG_USB_OTG
732 if ((hcd->self.otg_port == (wIndex + 1))
733 && hcd->self.b_hnp_enable) {
Richard Zhaoc2e935a2012-06-13 20:34:12 +0800734 otg_start_hnp(hcd->phy->otg);
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200735 break;
736 }
737#endif
Alan Stern16032c42010-05-12 18:21:35 -0400738 if (!(temp & PORT_SUSPEND))
739 break;
740 if ((temp & PORT_PE) == 0)
741 goto error;
742
743 /* clear phy low-power mode before resume */
Alan Sterna46af4e2012-06-25 12:19:03 -0400744 if (ehci->has_hostpc) {
Alan Stern16032c42010-05-12 18:21:35 -0400745 temp1 = ehci_readl(ehci, hostpc_reg);
746 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800747 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400748 spin_unlock_irqrestore(&ehci->lock, flags);
749 msleep(5);/* wait to leave low-power mode */
750 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Alan Stern16032c42010-05-12 18:21:35 -0400752 /* resume signaling for 20 msec */
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600753 temp &= ~PORT_WAKE_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400754 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
755 ehci->reset_done[wIndex] = jiffies
756 + msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400759 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 case USB_PORT_FEAT_POWER:
762 if (HCS_PPC (ehci->hcs_params))
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600763 ehci_writel(ehci, temp & ~PORT_POWER,
764 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 case USB_PORT_FEAT_C_CONNECTION:
Alek Du48f24972010-06-04 15:47:55 +0800767 if (ehci->has_lpm) {
768 /* clear PORTSC bits on disconnect */
769 temp &= ~PORT_LPM;
770 temp &= ~PORT_DEV_ADDR;
771 }
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600772 ehci_writel(ehci, temp | PORT_CSC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 case USB_PORT_FEAT_C_OVER_CURRENT:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600775 ehci_writel(ehci, temp | PORT_OCC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case USB_PORT_FEAT_C_RESET:
778 /* GetPortStatus clears reset */
779 break;
780 default:
781 goto error;
782 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100783 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 case GetHubDescriptor:
786 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
787 buf);
788 break;
789 case GetHubStatus:
790 /* no hub-wide feature/status flags */
791 memset (buf, 0, 4);
792 //cpu_to_le32s ((u32 *) buf);
793 break;
794 case GetPortStatus:
795 if (!wIndex || wIndex > ports)
796 goto error;
797 wIndex--;
798 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500799 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 // wPortChange bits
802 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500803 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500805 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700806
807 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500808 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700810 /*
811 * Hubs should disable port power on over-current.
812 * However, not all EHCI implementations do this
813 * automatically, even if they _do_ support per-port
814 * power switching; they're allowed to just limit the
815 * current. khubd will turn the power back on.
816 */
Sergei Shtylyov81463c12011-07-06 23:19:38 +0400817 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700818 ehci_writel(ehci,
819 temp & ~(PORT_RWC_BITS | PORT_POWER),
820 status_reg);
Sergei Shtylyov81463c12011-07-06 23:19:38 +0400821 temp = ehci_readl(ehci, status_reg);
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700822 }
823 }
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500826 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alan Stern629e4422007-01-22 16:08:53 -0500828 /* Remote Wakeup received? */
829 if (!ehci->reset_done[wIndex]) {
830 /* resume signaling for 20 msec */
831 ehci->reset_done[wIndex] = jiffies
832 + msecs_to_jiffies(20);
833 /* check the port again */
834 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
835 ehci->reset_done[wIndex]);
836 }
837
838 /* resume completed? */
839 else if (time_after_eq(jiffies,
840 ehci->reset_done[wIndex])) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400841 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sternd1f114d2008-05-20 16:58:58 -0400842 set_bit(wIndex, &ehci->port_c_suspend);
Alan Stern629e4422007-01-22 16:08:53 -0500843 ehci->reset_done[wIndex] = 0;
844
845 /* stop resume signaling */
846 temp = ehci_readl(ehci, status_reg);
847 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500848 temp & ~(PORT_RWC_BITS | PORT_RESUME),
849 status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400850 clear_bit(wIndex, &ehci->resuming_ports);
Alan Stern629e4422007-01-22 16:08:53 -0500851 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100852 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500853 if (retval != 0) {
854 ehci_err(ehci,
855 "port %d resume error %d\n",
856 wIndex + 1, retval);
857 goto error;
858 }
859 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 /* whoever resets must GetPortStatus to complete it!! */
864 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500865 && time_after_eq(jiffies,
866 ehci->reset_done[wIndex])) {
Alan Stern749da5f2010-03-04 17:05:08 -0500867 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 ehci->reset_done [wIndex] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400869 clear_bit(wIndex, &ehci->resuming_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100872 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500873 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700874 /* REVISIT: some hardware needs 550+ usec to clear
875 * this bit; seems too long to spin routinely...
876 */
Alan Sterne6316562007-01-16 11:58:00 -0500877 retval = handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -0500878 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (retval != 0) {
880 ehci_err (ehci, "port %d reset error %d\n",
881 wIndex + 1, retval);
882 goto error;
883 }
884
885 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500886 temp = check_reset_complete (ehci, wIndex, status_reg,
887 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Alan Sterna448e4d2012-04-03 15:24:30 -0400890 if (!(temp & (PORT_RESUME|PORT_RESET))) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400891 ehci->reset_done[wIndex] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400892 clear_bit(wIndex, &ehci->resuming_ports);
893 }
Alan Sterneafe5b92008-10-06 11:25:53 -0400894
Alan Stern57e06c12007-01-16 11:59:45 -0500895 /* transfer dedicated ports to the companion hc */
896 if ((temp & PORT_CONNECT) &&
897 test_bit(wIndex, &ehci->companion_ports)) {
898 temp &= ~PORT_RWC_BITS;
899 temp |= PORT_OWNER;
900 ehci_writel(ehci, temp, status_reg);
901 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
902 temp = ehci_readl(ehci, status_reg);
903 }
904
Alan Stern625b5c92007-01-16 11:58:47 -0500905 /*
906 * Even if OWNER is set, there's no harm letting khubd
907 * see the wPortStatus values (they should all be 0 except
908 * for PORT_POWER anyway).
909 */
910
911 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500912 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -0500913 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +0800914 if (ehci->has_hostpc) {
915 temp1 = ehci_readl(ehci, hostpc_reg);
916 status |= ehci_port_speed(ehci, temp1);
917 } else
918 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Alan Stern625b5c92007-01-16 11:58:47 -0500920 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500921 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -0400922
923 /* maybe the port was unsuspended without our knowledge */
924 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -0500925 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -0400926 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
927 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sterna448e4d2012-04-03 15:24:30 -0400928 clear_bit(wIndex, &ehci->resuming_ports);
Alan Sterneafe5b92008-10-06 11:25:53 -0400929 ehci->reset_done[wIndex] = 0;
930 if (temp & PORT_PE)
931 set_bit(wIndex, &ehci->port_c_suspend);
932 }
933
Alan Stern625b5c92007-01-16 11:58:47 -0500934 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500935 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -0500936 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500937 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -0500938 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500939 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -0400940 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -0500941 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
David Brownell9776afc2008-02-01 11:42:05 -0800943#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (status & ~0xffff) /* only if wPortChange is interesting */
945#endif
946 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700947 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
949 case SetHubFeature:
950 switch (wValue) {
951 case C_HUB_LOCAL_POWER:
952 case C_HUB_OVER_CURRENT:
953 /* no hub-wide feature/status flags */
954 break;
955 default:
956 goto error;
957 }
958 break;
959 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800960 selector = wIndex >> 8;
961 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -0500962 if (unlikely(ehci->debug)) {
963 /* If the debug port is active any port
964 * feature requests should get denied */
965 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
966 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
967 retval = -ENODEV;
968 goto error_exit;
969 }
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!wIndex || wIndex > ports)
972 goto error;
973 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500974 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (temp & PORT_OWNER)
976 break;
977
David Brownell10f65242005-08-31 10:55:38 -0700978 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 switch (wValue) {
980 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800981 if (ehci->no_selective_suspend)
982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if ((temp & PORT_PE) == 0
984 || (temp & PORT_RESET) != 0)
985 goto error;
Alek Dub9df79422010-01-19 16:31:31 +0800986
Alek Du331ac6b2009-07-13 12:41:20 +0800987 /* After above check the port must be connected.
988 * Set appropriate bit thus could put phy into low power
989 * mode if we have hostpc feature
990 */
Alek Dub9df79422010-01-19 16:31:31 +0800991 temp &= ~PORT_WKCONN_E;
992 temp |= PORT_WKDISC_E | PORT_WKOC_E;
993 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Alan Sterna46af4e2012-06-25 12:19:03 -0400994 if (ehci->has_hostpc) {
Alek Dub9df79422010-01-19 16:31:31 +0800995 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +0800996 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df79422010-01-19 16:31:31 +0800997 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +0800998 temp1 = ehci_readl(ehci, hostpc_reg);
999 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1000 hostpc_reg);
1001 temp1 = ehci_readl(ehci, hostpc_reg);
1002 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1003 wIndex, (temp1 & HOSTPC_PHCD) ?
1004 "succeeded" : "failed");
1005 }
Alan Sterneafe5b92008-10-06 11:25:53 -04001006 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 case USB_PORT_FEAT_POWER:
1009 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001010 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -05001011 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013 case USB_PORT_FEAT_RESET:
1014 if (temp & PORT_RESUME)
1015 goto error;
1016 /* line status bits may report this as low speed,
1017 * which can be fine if this root hub has a
1018 * transaction translator built in.
1019 */
1020 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1021 && !ehci_is_TDI(ehci)
1022 && PORT_USB11 (temp)) {
1023 ehci_dbg (ehci,
1024 "port %d low speed --> companion\n",
1025 wIndex + 1);
1026 temp |= PORT_OWNER;
1027 } else {
1028 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1029 temp |= PORT_RESET;
1030 temp &= ~PORT_PE;
1031
1032 /*
1033 * caller must wait, then call GetPortStatus
1034 * usb 2.0 spec says 50 ms resets on root
1035 */
1036 ehci->reset_done [wIndex] = jiffies
1037 + msecs_to_jiffies (50);
1038 }
Alan Sterne6316562007-01-16 11:58:00 -05001039 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001041
1042 /* For downstream facing ports (these): one hub port is put
1043 * into test mode according to USB2 11.24.2.13, then the hub
1044 * must be reset (which for root hub now means rmmod+modprobe,
1045 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1046 * about the EHCI-specific stuff.
1047 */
1048 case USB_PORT_FEAT_TEST:
1049 if (!selector || selector > 5)
1050 goto error;
Alan Sternc4f34762012-07-11 11:23:10 -04001051 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001052 ehci_quiesce(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001053 spin_lock_irqsave(&ehci->lock, flags);
Boris Todorov77636c82011-07-11 12:03:33 +03001054
1055 /* Put all enabled ports into suspend */
1056 while (ports--) {
1057 u32 __iomem *sreg =
1058 &ehci->regs->port_status[ports];
1059
1060 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1061 if (temp & PORT_PE)
1062 ehci_writel(ehci, temp | PORT_SUSPEND,
1063 sreg);
1064 }
Alan Sternc4f34762012-07-11 11:23:10 -04001065
1066 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001067 ehci_halt(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001068 spin_lock_irqsave(&ehci->lock, flags);
1069
Boris Todorov77636c82011-07-11 12:03:33 +03001070 temp = ehci_readl(ehci, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001071 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001072 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001073 break;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 default:
1076 goto error;
1077 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001078 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
1080
1081 default:
1082error:
1083 /* "stall" on error */
1084 retval = -EPIPE;
1085 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001086error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock_irqrestore (&ehci->lock, flags);
1088 return retval;
1089}
Balaji Rao90da0962007-11-22 01:58:14 +05301090
Felipe Balbi5407a3c2012-02-15 09:34:26 +02001091static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
1092 int portnum)
Balaji Rao90da0962007-11-22 01:58:14 +05301093{
1094 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1095
1096 if (ehci_is_TDI(ehci))
1097 return;
1098 set_owner(ehci, --portnum, PORT_OWNER);
1099}
1100
Felipe Balbi5407a3c2012-02-15 09:34:26 +02001101static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
1102 int portnum)
Alan Stern3a311552008-05-20 16:58:29 -04001103{
1104 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1105 u32 __iomem *reg;
1106
1107 if (ehci_is_TDI(ehci))
1108 return 0;
1109 reg = &ehci->regs->port_status[portnum - 1];
1110 return ehci_readl(ehci, reg) & PORT_OWNER;
1111}