blob: ffc5f27df725bff5edc12df99bd05098dadff9b0 [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);
224
Alan Sterncec3a532010-01-08 11:18:20 -0500225 /* Once the controller is stopped, port resumes that are already
226 * in progress won't complete. Hence if remote wakeup is enabled
227 * for the root hub and any ports are in the middle of a resume or
228 * remote wakeup, we must fail the suspend.
229 */
230 if (hcd->self.root_hub->do_remote_wakeup) {
Alan Sterna448e4d2012-04-03 15:24:30 -0400231 if (ehci->resuming_ports) {
232 spin_unlock_irq(&ehci->lock);
233 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
234 return -EBUSY;
Alan Sterncec3a532010-01-08 11:18:20 -0500235 }
236 }
237
Alan Stern8c033562006-11-09 14:42:16 -0500238 /* Unlike other USB host controller types, EHCI doesn't have
239 * any notion of "global" or bus-wide suspend. The driver has
240 * to manually suspend all the active unsuspended ports, and
241 * then manually resume them in the bus_resume() routine.
242 */
243 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400244 ehci->owned_ports = 0;
Alan Stern16032c42010-05-12 18:21:35 -0400245 changed = 0;
Alan Sterncec3a532010-01-08 11:18:20 -0500246 port = HCS_N_PORTS(ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 while (port--) {
248 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100249 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400250 u32 t2 = t1 & ~PORT_WAKE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Alan Stern8c033562006-11-09 14:42:16 -0500252 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400253 if (t1 & PORT_OWNER)
254 set_bit(port, &ehci->owned_ports);
255 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500257 set_bit(port, &ehci->bus_suspended);
258 }
259
Alan Stern16032c42010-05-12 18:21:35 -0400260 /* enable remote wakeup on all ports, if told to do so */
Alek Du331ac6b2009-07-13 12:41:20 +0800261 if (hcd->self.root_hub->do_remote_wakeup) {
262 /* only enable appropriate wake bits, otherwise the
263 * hardware can not go phy low power mode. If a race
264 * condition happens here(connection change during bits
265 * set), the port change detection will finally fix it.
266 */
Alan Stern16032c42010-05-12 18:21:35 -0400267 if (t1 & PORT_CONNECT)
Alek Du331ac6b2009-07-13 12:41:20 +0800268 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
Alan Stern16032c42010-05-12 18:21:35 -0400269 else
Alek Du331ac6b2009-07-13 12:41:20 +0800270 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
Alan Stern16032c42010-05-12 18:21:35 -0400271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (t1 != t2) {
274 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
275 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100276 ehci_writel(ehci, t2, reg);
Alan Stern16032c42010-05-12 18:21:35 -0400277 changed = 1;
278 }
279 }
Alek Du331ac6b2009-07-13 12:41:20 +0800280
Alan Stern16032c42010-05-12 18:21:35 -0400281 if (changed && ehci->has_hostpc) {
282 spin_unlock_irq(&ehci->lock);
283 msleep(5); /* 5 ms for HCD to enter low-power mode */
284 spin_lock_irq(&ehci->lock);
285
286 port = HCS_N_PORTS(ehci->hcs_params);
287 while (port--) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400288 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
Alan Stern16032c42010-05-12 18:21:35 -0400289 u32 t3;
290
Alan Stern16032c42010-05-12 18:21:35 -0400291 t3 = ehci_readl(ehci, hostpc_reg);
292 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
293 t3 = ehci_readl(ehci, hostpc_reg);
294 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
Alek Du331ac6b2009-07-13 12:41:20 +0800295 port, (t3 & HOSTPC_PHCD) ?
296 "succeeded" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 }
Alan Sternc4f34762012-07-11 11:23:10 -0400299 spin_unlock_irq(&ehci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Alan Sterncd930c92008-01-10 11:14:53 -0500301 /* Apparently some devices need a >= 1-uframe delay here */
302 if (ehci->bus_suspended)
303 udelay(150);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* turn off now-idle HC */
306 ehci_halt (ehci);
Alan Sternc4f34762012-07-11 11:23:10 -0400307
308 spin_lock_irq(&ehci->lock);
Alan Sterne8799902011-08-18 16:31:30 -0400309 ehci->rh_state = EHCI_RH_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alan Stern3c273a02012-07-11 11:22:49 -0400311 end_unlink_async(ehci);
Alan Stern32830f22012-07-11 11:22:53 -0400312 unlink_empty_async(ehci);
Alan Sterndf202252012-07-11 11:22:26 -0400313 ehci_handle_intr_unlinks(ehci);
Alan Stern55934eb2012-07-11 11:22:35 -0400314 end_free_itds(ehci);
David Brownellcdc647a2008-04-02 13:40:20 -0700315
Alan Stern8c033562006-11-09 14:42:16 -0500316 /* allow remote wakeup */
317 mask = INTR_MASK;
Alan Stern58a97ff2008-04-14 12:17:10 -0400318 if (!hcd->self.root_hub->do_remote_wakeup)
Alan Stern8c033562006-11-09 14:42:16 -0500319 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100320 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
321 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
Alan Sternd58b4bc2012-07-11 11:21:54 -0400324 ehci->enabled_hrtimer_events = 0;
325 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_unlock_irq (&ehci->lock);
Jon Hunter015798b2009-08-12 11:57:59 -0400327
Alan Sternd58b4bc2012-07-11 11:21:54 -0400328 hrtimer_cancel(&ehci->hrtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
332
333/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400334static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
337 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400338 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int i;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800340 unsigned long resume_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (time_before (jiffies, ehci->next_statechange))
343 msleep(5);
344 spin_lock_irq (&ehci->lock);
Alan Stern541c7d42010-06-22 16:39:10 -0400345 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -0400346 spin_unlock_irq(&ehci->lock);
347 return -ESHUTDOWN;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jason Wesselad45f1d2009-08-20 15:39:58 -0500350 if (unlikely(ehci->debug)) {
Jason Wessel872d3592009-09-23 18:32:44 -0500351 if (!dbgp_reset_prep())
Jason Wesselad45f1d2009-08-20 15:39:58 -0500352 ehci->debug = NULL;
353 else
354 dbgp_external_startup();
355 }
356
David Brownellf03c17f2005-11-23 15:45:28 -0800357 /* Ideally and we've got a real resume here, and no port's power
358 * was lost. (For PCI, that means Vaux was maintained.) But we
359 * could instead be restoring a swsusp snapshot -- so that BIOS was
360 * the last user of the controller, not reset/pm hardware keeping
361 * state we gave to it.
362 */
Alan Stern383975d2007-05-04 11:52:40 -0400363 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
364 ehci_dbg(ehci, "resume root hub%s\n",
365 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800366
Alan Stern8c033562006-11-09 14:42:16 -0500367 /* at least some APM implementations will try to deliver
368 * IRQs right away, so delay them until we're ready.
369 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100370 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500371
372 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100373 ehci_writel(ehci, 0, &ehci->regs->segment);
374 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
375 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* restore CMD_RUN, framelist size, and irq threshold */
Alan Stern3d9545c2012-04-23 13:54:36 -0400378 ehci->command |= CMD_RUN;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100379 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Alan Sterne8799902011-08-18 16:31:30 -0400380 ehci->rh_state = EHCI_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alan Sterne198a312007-03-15 15:54:30 -0400382 /* Some controller/firmware combinations need a delay during which
383 * they set up the port statuses. See Bugzilla #8190. */
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530384 spin_unlock_irq(&ehci->lock);
385 msleep(8);
386 spin_lock_irq(&ehci->lock);
Alan Sterne198a312007-03-15 15:54:30 -0400387
Alan Stern16032c42010-05-12 18:21:35 -0400388 /* clear phy low-power mode before resume */
389 if (ehci->bus_suspended && ehci->has_hostpc) {
390 i = HCS_N_PORTS(ehci->hcs_params);
391 while (i--) {
392 if (test_bit(i, &ehci->bus_suspended)) {
Alan Sterna46af4e2012-06-25 12:19:03 -0400393 u32 __iomem *hostpc_reg =
394 &ehci->regs->hostpc[i];
Alan Stern16032c42010-05-12 18:21:35 -0400395
Alan Stern16032c42010-05-12 18:21:35 -0400396 temp = ehci_readl(ehci, hostpc_reg);
397 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
398 hostpc_reg);
399 }
400 }
401 spin_unlock_irq(&ehci->lock);
402 msleep(5);
403 spin_lock_irq(&ehci->lock);
404 }
405
Alan Stern8c033562006-11-09 14:42:16 -0500406 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 i = HCS_N_PORTS (ehci->hcs_params);
408 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100409 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern58a97ff2008-04-14 12:17:10 -0400410 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Alan Stern8c033562006-11-09 14:42:16 -0500411 if (test_bit(i, &ehci->bus_suspended) &&
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530412 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 temp |= PORT_RESUME;
Wang Zhid0f2fb22011-08-17 10:39:31 +0800414 set_bit(i, &resume_needed);
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530415 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100416 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Vikram Pandita3a4e72c2008-10-24 23:41:30 +0530418
419 /* msleep for 20ms only if code is trying to resume port */
420 if (resume_needed) {
421 spin_unlock_irq(&ehci->lock);
422 msleep(20);
423 spin_lock_irq(&ehci->lock);
424 }
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 i = HCS_N_PORTS (ehci->hcs_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100428 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Wang Zhid0f2fb22011-08-17 10:39:31 +0800429 if (test_bit(i, &resume_needed)) {
Alan Stern8c033562006-11-09 14:42:16 -0500430 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100431 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500432 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
Alan Sternc4f34762012-07-11 11:23:10 -0400437 spin_unlock_irq(&ehci->lock);
438
439 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100442 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Alan Stern15be1052012-07-11 11:21:17 -0400443 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
448#else
449
Alan Stern0c0382e2005-10-13 17:08:02 -0400450#define ehci_bus_suspend NULL
451#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453#endif /* CONFIG_PM */
454
455/*-------------------------------------------------------------------------*/
456
Alan Stern57e06c12007-01-16 11:59:45 -0500457/*
Balaji Rao90da0962007-11-22 01:58:14 +0530458 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500459 */
Balaji Rao90da0962007-11-22 01:58:14 +0530460static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500461{
Alan Stern57e06c12007-01-16 11:59:45 -0500462 u32 __iomem *status_reg;
463 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530464 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500465
Balaji Rao90da0962007-11-22 01:58:14 +0530466 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500467
468 /*
469 * The controller won't set the OWNER bit if the port is
470 * enabled, so this loop will sometimes require at least two
471 * iterations: one to disable the port and one to set OWNER.
472 */
Alan Stern57e06c12007-01-16 11:59:45 -0500473 for (try = 4; try > 0; --try) {
474 spin_lock_irq(&ehci->lock);
475 port_status = ehci_readl(ehci, status_reg);
476 if ((port_status & PORT_OWNER) == new_owner
477 || (port_status & (PORT_OWNER | PORT_CONNECT))
478 == 0)
479 try = 0;
480 else {
481 port_status ^= PORT_OWNER;
482 port_status &= ~(PORT_PE | PORT_RWC_BITS);
483 ehci_writel(ehci, port_status, status_reg);
484 }
485 spin_unlock_irq(&ehci->lock);
486 if (try > 1)
487 msleep(5);
488 }
Balaji Rao90da0962007-11-22 01:58:14 +0530489}
490
Alan Stern57e06c12007-01-16 11:59:45 -0500491/*-------------------------------------------------------------------------*/
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int check_reset_complete (
494 struct ehci_hcd *ehci,
495 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500496 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int port_status
498) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800499 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* if reset finished and it's still not enabled -- handoff */
503 if (!(port_status & PORT_PE)) {
504
505 /* with integrated TT, there's nobody to hand it to! */
506 if (ehci_is_TDI(ehci)) {
507 ehci_dbg (ehci,
508 "Failed to enable port %d on root hub TT\n",
509 index+1);
510 return port_status;
511 }
512
513 ehci_dbg (ehci, "port %d full speed --> companion\n",
514 index + 1);
515
516 // what happens if HCS_N_CC(params) == 0 ?
517 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700518 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500519 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100521 /* ensure 440EPX ohci controller state is operational */
522 if (ehci->has_amcc_usb23)
523 set_ohci_hcfs(ehci, 1);
524 } else {
Peter Chen0ca7eb22012-03-29 09:50:06 +0800525 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
526 index + 1);
Vitaly Bordug796bcae2008-11-09 19:43:30 +0100527 /* ensure 440EPx ohci controller state is suspended */
528 if (ehci->has_amcc_usb23)
529 set_ohci_hcfs(ehci, 0);
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return port_status;
533}
534
535/*-------------------------------------------------------------------------*/
536
537
538/* build "status change" packet (one or two bytes) from HC registers */
539
540static int
541ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
542{
543 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
Alan Sterna448e4d2012-04-03 15:24:30 -0400544 u32 temp, status;
David Brownell93f1a472006-11-16 23:34:58 -0800545 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int ports, i, retval = 1;
547 unsigned long flags;
Alek Du5a9cdf32010-06-04 15:47:56 +0800548 u32 ppcd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* init status to no-changes */
551 buf [0] = 0;
552 ports = HCS_N_PORTS (ehci->hcs_params);
553 if (ports > 7) {
554 buf [1] = 0;
555 retval++;
556 }
David Brownell53bd6a62006-08-30 14:50:06 -0700557
Alan Sterna448e4d2012-04-03 15:24:30 -0400558 /* Inform the core about resumes-in-progress by returning
559 * a non-zero value even if there are no status changes.
560 */
561 status = ehci->resuming_ports;
562
David Brownell93f1a472006-11-16 23:34:58 -0800563 /* Some boards (mostly VIA?) report bogus overcurrent indications,
564 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200565 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800566 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
567 * PORT_POWER; that's surprising, but maybe within-spec.
568 */
569 if (!ignore_oc)
570 mask = PORT_CSC | PORT_PEC | PORT_OCC;
571 else
572 mask = PORT_CSC | PORT_PEC;
573 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* no hub change reports (bit 0) for now (power, ...) */
576
577 /* port N changes (bit N)? */
578 spin_lock_irqsave (&ehci->lock, flags);
Alek Du5a9cdf32010-06-04 15:47:56 +0800579
580 /* get per-port change detect bits */
581 if (ehci->has_ppcd)
582 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 for (i = 0; i < ports; i++) {
Alek Du5a9cdf32010-06-04 15:47:56 +0800585 /* leverage per-port change bits feature */
586 if (ehci->has_ppcd && !(ppcd & (1 << i)))
587 continue;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100588 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500589
590 /*
591 * Return status information even for ports with OWNER set.
592 * Otherwise khubd wouldn't see the disconnect event when a
593 * high-speed device is switched over to the companion
594 * controller by the user.
595 */
596
Alan Sterneafe5b92008-10-06 11:25:53 -0400597 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
598 || (ehci->reset_done[i] && time_after_eq(
599 jiffies, ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (i < 7)
601 buf [0] |= 1 << (i + 1);
602 else
603 buf [1] |= 1 << (i - 7);
604 status = STS_PCD;
605 }
606 }
607 /* FIXME autosuspend idle root hubs */
608 spin_unlock_irqrestore (&ehci->lock, flags);
609 return status ? retval : 0;
610}
611
612/*-------------------------------------------------------------------------*/
613
614static void
615ehci_hub_descriptor (
616 struct ehci_hcd *ehci,
617 struct usb_hub_descriptor *desc
618) {
619 int ports = HCS_N_PORTS (ehci->hcs_params);
620 u16 temp;
621
622 desc->bDescriptorType = 0x29;
623 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
624 desc->bHubContrCurrent = 0;
625
626 desc->bNbrPorts = ports;
627 temp = 1 + (ports / 8);
628 desc->bDescLength = 7 + 2 * temp;
629
630 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
John Youndbe79bb2001-09-17 00:00:00 -0700631 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
632 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 temp = 0x0008; /* per-port overcurrent reporting */
635 if (HCS_PPC (ehci->hcs_params))
636 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700637 else
638 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#if 0
640// re-enable when we support USB_PORT_FEAT_INDICATOR below.
641 if (HCS_INDICATOR (ehci->hcs_params))
642 temp |= 0x0080; /* per-port indicators (LEDs) */
643#endif
Al Virofd05e722008-04-28 07:00:16 +0100644 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647/*-------------------------------------------------------------------------*/
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static int ehci_hub_control (
650 struct usb_hcd *hcd,
651 u16 typeReq,
652 u16 wValue,
653 u16 wIndex,
654 char *buf,
655 u16 wLength
656) {
657 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
658 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400659 u32 __iomem *status_reg = &ehci->regs->port_status[
660 (wIndex & 0xff) - 1];
Alan Sterna46af4e2012-06-25 12:19:03 -0400661 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
Alek Du331ac6b2009-07-13 12:41:20 +0800662 u32 temp, temp1, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned long flags;
664 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800665 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
668 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
669 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
670 * (track current state ourselves) ... blink for diagnostics,
671 * power, "this is the one", etc. EHCI spec supports this.
672 */
673
674 spin_lock_irqsave (&ehci->lock, flags);
675 switch (typeReq) {
676 case ClearHubFeature:
677 switch (wValue) {
678 case C_HUB_LOCAL_POWER:
679 case C_HUB_OVER_CURRENT:
680 /* no hub-wide feature/status flags */
681 break;
682 default:
683 goto error;
684 }
685 break;
686 case ClearPortFeature:
687 if (!wIndex || wIndex > ports)
688 goto error;
689 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500690 temp = ehci_readl(ehci, status_reg);
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600691 temp &= ~PORT_RWC_BITS;
Alan Stern625b5c92007-01-16 11:58:47 -0500692
693 /*
694 * Even if OWNER is set, so the port is owned by the
695 * companion controller, khubd needs to be able to clear
696 * the port-change status bits (especially
Alan Stern749da5f2010-03-04 17:05:08 -0500697 * USB_PORT_STAT_C_CONNECTION).
Alan Stern625b5c92007-01-16 11:58:47 -0500698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 switch (wValue) {
701 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500702 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 case USB_PORT_FEAT_C_ENABLE:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600705 ehci_writel(ehci, temp | PORT_PEC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 case USB_PORT_FEAT_SUSPEND:
708 if (temp & PORT_RESET)
709 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800710 if (ehci->no_selective_suspend)
711 break;
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200712#ifdef CONFIG_USB_OTG
713 if ((hcd->self.otg_port == (wIndex + 1))
714 && hcd->self.b_hnp_enable) {
Richard Zhaoc2e935a2012-06-13 20:34:12 +0800715 otg_start_hnp(hcd->phy->otg);
Anatolij Gustschin83722bc2011-04-18 22:02:00 +0200716 break;
717 }
718#endif
Alan Stern16032c42010-05-12 18:21:35 -0400719 if (!(temp & PORT_SUSPEND))
720 break;
721 if ((temp & PORT_PE) == 0)
722 goto error;
723
724 /* clear phy low-power mode before resume */
Alan Sterna46af4e2012-06-25 12:19:03 -0400725 if (ehci->has_hostpc) {
Alan Stern16032c42010-05-12 18:21:35 -0400726 temp1 = ehci_readl(ehci, hostpc_reg);
727 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
Alek Dueab80de2010-05-10 11:17:49 +0800728 hostpc_reg);
Alan Stern16032c42010-05-12 18:21:35 -0400729 spin_unlock_irqrestore(&ehci->lock, flags);
730 msleep(5);/* wait to leave low-power mode */
731 spin_lock_irqsave(&ehci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Alan Stern16032c42010-05-12 18:21:35 -0400733 /* resume signaling for 20 msec */
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600734 temp &= ~PORT_WAKE_BITS;
Alan Stern16032c42010-05-12 18:21:35 -0400735 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
736 ehci->reset_done[wIndex] = jiffies
737 + msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739 case USB_PORT_FEAT_C_SUSPEND:
Alan Sternd1f114d2008-05-20 16:58:58 -0400740 clear_bit(wIndex, &ehci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 case USB_PORT_FEAT_POWER:
743 if (HCS_PPC (ehci->hcs_params))
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600744 ehci_writel(ehci, temp & ~PORT_POWER,
745 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 case USB_PORT_FEAT_C_CONNECTION:
Alek Du48f24972010-06-04 15:47:55 +0800748 if (ehci->has_lpm) {
749 /* clear PORTSC bits on disconnect */
750 temp &= ~PORT_LPM;
751 temp &= ~PORT_DEV_ADDR;
752 }
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600753 ehci_writel(ehci, temp | PORT_CSC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 case USB_PORT_FEAT_C_OVER_CURRENT:
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600756 ehci_writel(ehci, temp | PORT_OCC, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 case USB_PORT_FEAT_C_RESET:
759 /* GetPortStatus clears reset */
760 break;
761 default:
762 goto error;
763 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100764 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 case GetHubDescriptor:
767 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
768 buf);
769 break;
770 case GetHubStatus:
771 /* no hub-wide feature/status flags */
772 memset (buf, 0, 4);
773 //cpu_to_le32s ((u32 *) buf);
774 break;
775 case GetPortStatus:
776 if (!wIndex || wIndex > ports)
777 goto error;
778 wIndex--;
779 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500780 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 // wPortChange bits
783 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500784 status |= USB_PORT_STAT_C_CONNECTION << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500786 status |= USB_PORT_STAT_C_ENABLE << 16;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700787
788 if ((temp & PORT_OCC) && !ignore_oc){
Alan Stern749da5f2010-03-04 17:05:08 -0500789 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700791 /*
792 * Hubs should disable port power on over-current.
793 * However, not all EHCI implementations do this
794 * automatically, even if they _do_ support per-port
795 * power switching; they're allowed to just limit the
796 * current. khubd will turn the power back on.
797 */
Sergei Shtylyov81463c12011-07-06 23:19:38 +0400798 if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700799 ehci_writel(ehci,
800 temp & ~(PORT_RWC_BITS | PORT_POWER),
801 status_reg);
Sergei Shtylyov81463c12011-07-06 23:19:38 +0400802 temp = ehci_readl(ehci, status_reg);
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700803 }
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500807 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Alan Stern629e4422007-01-22 16:08:53 -0500809 /* Remote Wakeup received? */
810 if (!ehci->reset_done[wIndex]) {
811 /* resume signaling for 20 msec */
812 ehci->reset_done[wIndex] = jiffies
813 + msecs_to_jiffies(20);
814 /* check the port again */
815 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
816 ehci->reset_done[wIndex]);
817 }
818
819 /* resume completed? */
820 else if (time_after_eq(jiffies,
821 ehci->reset_done[wIndex])) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400822 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sternd1f114d2008-05-20 16:58:58 -0400823 set_bit(wIndex, &ehci->port_c_suspend);
Alan Stern629e4422007-01-22 16:08:53 -0500824 ehci->reset_done[wIndex] = 0;
825
826 /* stop resume signaling */
827 temp = ehci_readl(ehci, status_reg);
828 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500829 temp & ~(PORT_RWC_BITS | PORT_RESUME),
830 status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400831 clear_bit(wIndex, &ehci->resuming_ports);
Alan Stern629e4422007-01-22 16:08:53 -0500832 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100833 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500834 if (retval != 0) {
835 ehci_err(ehci,
836 "port %d resume error %d\n",
837 wIndex + 1, retval);
838 goto error;
839 }
840 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
844 /* whoever resets must GetPortStatus to complete it!! */
845 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500846 && time_after_eq(jiffies,
847 ehci->reset_done[wIndex])) {
Alan Stern749da5f2010-03-04 17:05:08 -0500848 status |= USB_PORT_STAT_C_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ehci->reset_done [wIndex] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400850 clear_bit(wIndex, &ehci->resuming_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100853 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500854 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700855 /* REVISIT: some hardware needs 550+ usec to clear
856 * this bit; seems too long to spin routinely...
857 */
Alan Sterne6316562007-01-16 11:58:00 -0500858 retval = handshake(ehci, status_reg,
Dinh Nguyen6307e092010-04-13 11:13:15 -0500859 PORT_RESET, 0, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (retval != 0) {
861 ehci_err (ehci, "port %d reset error %d\n",
862 wIndex + 1, retval);
863 goto error;
864 }
865
866 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500867 temp = check_reset_complete (ehci, wIndex, status_reg,
868 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Alan Sterna448e4d2012-04-03 15:24:30 -0400871 if (!(temp & (PORT_RESUME|PORT_RESET))) {
Alan Sterneafe5b92008-10-06 11:25:53 -0400872 ehci->reset_done[wIndex] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400873 clear_bit(wIndex, &ehci->resuming_ports);
874 }
Alan Sterneafe5b92008-10-06 11:25:53 -0400875
Alan Stern57e06c12007-01-16 11:59:45 -0500876 /* transfer dedicated ports to the companion hc */
877 if ((temp & PORT_CONNECT) &&
878 test_bit(wIndex, &ehci->companion_ports)) {
879 temp &= ~PORT_RWC_BITS;
880 temp |= PORT_OWNER;
881 ehci_writel(ehci, temp, status_reg);
882 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
883 temp = ehci_readl(ehci, status_reg);
884 }
885
Alan Stern625b5c92007-01-16 11:58:47 -0500886 /*
887 * Even if OWNER is set, there's no harm letting khubd
888 * see the wPortStatus values (they should all be 0 except
889 * for PORT_POWER anyway).
890 */
891
892 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500893 status |= USB_PORT_STAT_CONNECTION;
Alan Stern625b5c92007-01-16 11:58:47 -0500894 // status may be from integrated TT
Alek Du331ac6b2009-07-13 12:41:20 +0800895 if (ehci->has_hostpc) {
896 temp1 = ehci_readl(ehci, hostpc_reg);
897 status |= ehci_port_speed(ehci, temp1);
898 } else
899 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Alan Stern625b5c92007-01-16 11:58:47 -0500901 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500902 status |= USB_PORT_STAT_ENABLE;
Alan Sterneafe5b92008-10-06 11:25:53 -0400903
904 /* maybe the port was unsuspended without our knowledge */
905 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
Alan Stern749da5f2010-03-04 17:05:08 -0500906 status |= USB_PORT_STAT_SUSPEND;
Alan Sterneafe5b92008-10-06 11:25:53 -0400907 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
908 clear_bit(wIndex, &ehci->suspended_ports);
Alan Sterna448e4d2012-04-03 15:24:30 -0400909 clear_bit(wIndex, &ehci->resuming_ports);
Alan Sterneafe5b92008-10-06 11:25:53 -0400910 ehci->reset_done[wIndex] = 0;
911 if (temp & PORT_PE)
912 set_bit(wIndex, &ehci->port_c_suspend);
913 }
914
Alan Stern625b5c92007-01-16 11:58:47 -0500915 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500916 status |= USB_PORT_STAT_OVERCURRENT;
Alan Stern625b5c92007-01-16 11:58:47 -0500917 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500918 status |= USB_PORT_STAT_RESET;
Alan Stern625b5c92007-01-16 11:58:47 -0500919 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500920 status |= USB_PORT_STAT_POWER;
Alan Sternd1f114d2008-05-20 16:58:58 -0400921 if (test_bit(wIndex, &ehci->port_c_suspend))
Alan Stern749da5f2010-03-04 17:05:08 -0500922 status |= USB_PORT_STAT_C_SUSPEND << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
David Brownell9776afc2008-02-01 11:42:05 -0800924#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (status & ~0xffff) /* only if wPortChange is interesting */
926#endif
927 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700928 put_unaligned_le32(status, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 case SetHubFeature:
931 switch (wValue) {
932 case C_HUB_LOCAL_POWER:
933 case C_HUB_OVER_CURRENT:
934 /* no hub-wide feature/status flags */
935 break;
936 default:
937 goto error;
938 }
939 break;
940 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800941 selector = wIndex >> 8;
942 wIndex &= 0xff;
Jason Wessel8d053c72009-08-20 15:39:54 -0500943 if (unlikely(ehci->debug)) {
944 /* If the debug port is active any port
945 * feature requests should get denied */
946 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
947 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
948 retval = -ENODEV;
949 goto error_exit;
950 }
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!wIndex || wIndex > ports)
953 goto error;
954 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500955 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (temp & PORT_OWNER)
957 break;
958
David Brownell10f65242005-08-31 10:55:38 -0700959 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 switch (wValue) {
961 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800962 if (ehci->no_selective_suspend)
963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if ((temp & PORT_PE) == 0
965 || (temp & PORT_RESET) != 0)
966 goto error;
Alek Dub9df79422010-01-19 16:31:31 +0800967
Alek Du331ac6b2009-07-13 12:41:20 +0800968 /* After above check the port must be connected.
969 * Set appropriate bit thus could put phy into low power
970 * mode if we have hostpc feature
971 */
Alek Dub9df79422010-01-19 16:31:31 +0800972 temp &= ~PORT_WKCONN_E;
973 temp |= PORT_WKDISC_E | PORT_WKOC_E;
974 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Alan Sterna46af4e2012-06-25 12:19:03 -0400975 if (ehci->has_hostpc) {
Alek Dub9df79422010-01-19 16:31:31 +0800976 spin_unlock_irqrestore(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +0800977 msleep(5);/* 5ms for HCD enter low pwr mode */
Alek Dub9df79422010-01-19 16:31:31 +0800978 spin_lock_irqsave(&ehci->lock, flags);
Alek Du331ac6b2009-07-13 12:41:20 +0800979 temp1 = ehci_readl(ehci, hostpc_reg);
980 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
981 hostpc_reg);
982 temp1 = ehci_readl(ehci, hostpc_reg);
983 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
984 wIndex, (temp1 & HOSTPC_PHCD) ?
985 "succeeded" : "failed");
986 }
Alan Sterneafe5b92008-10-06 11:25:53 -0400987 set_bit(wIndex, &ehci->suspended_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989 case USB_PORT_FEAT_POWER:
990 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100991 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -0500992 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
994 case USB_PORT_FEAT_RESET:
995 if (temp & PORT_RESUME)
996 goto error;
997 /* line status bits may report this as low speed,
998 * which can be fine if this root hub has a
999 * transaction translator built in.
1000 */
1001 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1002 && !ehci_is_TDI(ehci)
1003 && PORT_USB11 (temp)) {
1004 ehci_dbg (ehci,
1005 "port %d low speed --> companion\n",
1006 wIndex + 1);
1007 temp |= PORT_OWNER;
1008 } else {
1009 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
1010 temp |= PORT_RESET;
1011 temp &= ~PORT_PE;
1012
1013 /*
1014 * caller must wait, then call GetPortStatus
1015 * usb 2.0 spec says 50 ms resets on root
1016 */
1017 ehci->reset_done [wIndex] = jiffies
1018 + msecs_to_jiffies (50);
1019 }
Alan Sterne6316562007-01-16 11:58:00 -05001020 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
David Brownellf0d7f272006-11-16 23:56:15 -08001022
1023 /* For downstream facing ports (these): one hub port is put
1024 * into test mode according to USB2 11.24.2.13, then the hub
1025 * must be reset (which for root hub now means rmmod+modprobe,
1026 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1027 * about the EHCI-specific stuff.
1028 */
1029 case USB_PORT_FEAT_TEST:
1030 if (!selector || selector > 5)
1031 goto error;
Alan Sternc4f34762012-07-11 11:23:10 -04001032 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001033 ehci_quiesce(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001034 spin_lock_irqsave(&ehci->lock, flags);
Boris Todorov77636c82011-07-11 12:03:33 +03001035
1036 /* Put all enabled ports into suspend */
1037 while (ports--) {
1038 u32 __iomem *sreg =
1039 &ehci->regs->port_status[ports];
1040
1041 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1042 if (temp & PORT_PE)
1043 ehci_writel(ehci, temp | PORT_SUSPEND,
1044 sreg);
1045 }
Alan Sternc4f34762012-07-11 11:23:10 -04001046
1047 spin_unlock_irqrestore(&ehci->lock, flags);
David Brownellf0d7f272006-11-16 23:56:15 -08001048 ehci_halt(ehci);
Alan Sternc4f34762012-07-11 11:23:10 -04001049 spin_lock_irqsave(&ehci->lock, flags);
1050
Boris Todorov77636c82011-07-11 12:03:33 +03001051 temp = ehci_readl(ehci, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001052 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -05001053 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -08001054 break;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 default:
1057 goto error;
1058 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +11001059 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061
1062 default:
1063error:
1064 /* "stall" on error */
1065 retval = -EPIPE;
1066 }
Jason Wessel8d053c72009-08-20 15:39:54 -05001067error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_unlock_irqrestore (&ehci->lock, flags);
1069 return retval;
1070}
Balaji Rao90da0962007-11-22 01:58:14 +05301071
Felipe Balbi5407a3c2012-02-15 09:34:26 +02001072static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
1073 int portnum)
Balaji Rao90da0962007-11-22 01:58:14 +05301074{
1075 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1076
1077 if (ehci_is_TDI(ehci))
1078 return;
1079 set_owner(ehci, --portnum, PORT_OWNER);
1080}
1081
Felipe Balbi5407a3c2012-02-15 09:34:26 +02001082static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
1083 int portnum)
Alan Stern3a311552008-05-20 16:58:29 -04001084{
1085 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1086 u32 __iomem *reg;
1087
1088 if (ehci_is_TDI(ehci))
1089 return 0;
1090 reg = &ehci->regs->port_status[portnum - 1];
1091 return ehci_readl(ehci, reg) & PORT_OWNER;
1092}