blob: 89ff025b41be19552f43e00539dc53d62a65f41b [file] [log] [blame]
Sarah Sharp0f2a7932009-04-27 19:57:12 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <asm/unaligned.h>
24
25#include "xhci.h"
26
Andiry Xu9777e3c2010-10-14 07:23:03 -070027#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
28#define PORT_RWC_BITS (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
29 PORT_RC | PORT_PLC | PORT_PE)
30
Sarah Sharp0f2a7932009-04-27 19:57:12 -070031static void xhci_hub_descriptor(struct xhci_hcd *xhci,
32 struct usb_hub_descriptor *desc)
33{
34 int ports;
35 u16 temp;
36
37 ports = HCS_MAX_PORTS(xhci->hcs_params1);
38
39 /* USB 3.0 hubs have a different descriptor, but we fake this for now */
40 desc->bDescriptorType = 0x29;
41 desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
42 desc->bHubContrCurrent = 0;
43
44 desc->bNbrPorts = ports;
45 temp = 1 + (ports / 8);
46 desc->bDescLength = 7 + 2 * temp;
47
John Youndbe79bb2001-09-17 00:00:00 -070048 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
49 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -070050
51 /* Ugh, these should be #defines, FIXME */
52 /* Using table 11-13 in USB 2.0 spec. */
53 temp = 0;
54 /* Bits 1:0 - support port power switching, or power always on */
55 if (HCC_PPC(xhci->hcc_params))
56 temp |= 0x0001;
57 else
58 temp |= 0x0002;
59 /* Bit 2 - root hubs are not part of a compound device */
60 /* Bits 4:3 - individual port over current protection */
61 temp |= 0x0008;
62 /* Bits 6:5 - no TTs in root ports */
63 /* Bit 7 - no port indicators */
64 desc->wHubCharacteristics = (__force __u16) cpu_to_le16(temp);
65}
66
67static unsigned int xhci_port_speed(unsigned int port_status)
68{
69 if (DEV_LOWSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050070 return USB_PORT_STAT_LOW_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070071 if (DEV_HIGHSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050072 return USB_PORT_STAT_HIGH_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070073 if (DEV_SUPERSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050074 return USB_PORT_STAT_SUPER_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070075 /*
76 * FIXME: Yes, we should check for full speed, but the core uses that as
77 * a default in portspeed() in usb/core/hub.c (which is the only place
Alan Stern288ead42010-03-04 11:32:30 -050078 * USB_PORT_STAT_*_SPEED is used).
Sarah Sharp0f2a7932009-04-27 19:57:12 -070079 */
80 return 0;
81}
82
83/*
84 * These bits are Read Only (RO) and should be saved and written to the
85 * registers: 0, 3, 10:13, 30
86 * connect status, over-current status, port speed, and device removable.
87 * connect status and port speed are also sticky - meaning they're in
88 * the AUX well and they aren't changed by a hot, warm, or cold reset.
89 */
90#define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
91/*
92 * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
93 * bits 5:8, 9, 14:15, 25:27
94 * link state, port power, port indicator state, "wake on" enable state
95 */
96#define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
97/*
98 * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
99 * bit 4 (port reset)
100 */
101#define XHCI_PORT_RW1S ((1<<4))
102/*
103 * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
104 * bits 1, 17, 18, 19, 20, 21, 22, 23
105 * port enable/disable, and
106 * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
107 * over-current, reset, link state, and L1 change
108 */
109#define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
110/*
111 * Bit 16 is RW, and writing a '1' to it causes the link state control to be
112 * latched in
113 */
114#define XHCI_PORT_RW ((1<<16))
115/*
116 * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
117 * bits 2, 24, 28:31
118 */
119#define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
120
121/*
122 * Given a port state, this function returns a value that would result in the
123 * port being in the same state, if the value was written to the port status
124 * control register.
125 * Save Read Only (RO) bits and save read/write bits where
126 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
127 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
128 */
Andiry Xu56192532010-10-14 07:23:00 -0700129u32 xhci_port_state_to_neutral(u32 state)
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700130{
131 /* Save read-only status and port state */
132 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
133}
134
Andiry Xube88fe42010-10-14 07:22:57 -0700135/*
136 * find slot id based on port number.
137 */
Andiry Xu56192532010-10-14 07:23:00 -0700138int xhci_find_slot_id_by_port(struct xhci_hcd *xhci, u16 port)
Andiry Xube88fe42010-10-14 07:22:57 -0700139{
140 int slot_id;
141 int i;
142
143 slot_id = 0;
144 for (i = 0; i < MAX_HC_SLOTS; i++) {
145 if (!xhci->devs[i])
146 continue;
147 if (xhci->devs[i]->port == port) {
148 slot_id = i;
149 break;
150 }
151 }
152
153 return slot_id;
154}
155
156/*
157 * Stop device
158 * It issues stop endpoint command for EP 0 to 30. And wait the last command
159 * to complete.
160 * suspend will set to 1, if suspend bit need to set in command.
161 */
162static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
163{
164 struct xhci_virt_device *virt_dev;
165 struct xhci_command *cmd;
166 unsigned long flags;
167 int timeleft;
168 int ret;
169 int i;
170
171 ret = 0;
172 virt_dev = xhci->devs[slot_id];
173 cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
174 if (!cmd) {
175 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
176 return -ENOMEM;
177 }
178
179 spin_lock_irqsave(&xhci->lock, flags);
180 for (i = LAST_EP_INDEX; i > 0; i--) {
181 if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
182 xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
183 }
184 cmd->command_trb = xhci->cmd_ring->enqueue;
185 list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
186 xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
187 xhci_ring_cmd_db(xhci);
188 spin_unlock_irqrestore(&xhci->lock, flags);
189
190 /* Wait for last stop endpoint command to finish */
191 timeleft = wait_for_completion_interruptible_timeout(
192 cmd->completion,
193 USB_CTRL_SET_TIMEOUT);
194 if (timeleft <= 0) {
195 xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
196 timeleft == 0 ? "Timeout" : "Signal");
197 spin_lock_irqsave(&xhci->lock, flags);
198 /* The timeout might have raced with the event ring handler, so
199 * only delete from the list if the item isn't poisoned.
200 */
201 if (cmd->cmd_list.next != LIST_POISON1)
202 list_del(&cmd->cmd_list);
203 spin_unlock_irqrestore(&xhci->lock, flags);
204 ret = -ETIME;
205 goto command_cleanup;
206 }
207
208command_cleanup:
209 xhci_free_command(xhci, cmd);
210 return ret;
211}
212
213/*
214 * Ring device, it rings the all doorbells unconditionally.
215 */
Andiry Xu56192532010-10-14 07:23:00 -0700216void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
Andiry Xube88fe42010-10-14 07:22:57 -0700217{
218 int i;
219
220 for (i = 0; i < LAST_EP_INDEX + 1; i++)
221 if (xhci->devs[slot_id]->eps[i].ring &&
222 xhci->devs[slot_id]->eps[i].ring->dequeue)
223 xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
224
225 return;
226}
227
Sarah Sharp6219c042009-12-09 15:59:11 -0800228static void xhci_disable_port(struct xhci_hcd *xhci, u16 wIndex,
229 u32 __iomem *addr, u32 port_status)
230{
Sarah Sharp6dd0a3a72010-11-16 15:58:52 -0800231 /* Don't allow the USB core to disable SuperSpeed ports. */
232 if (xhci->port_array[wIndex] == 0x03) {
233 xhci_dbg(xhci, "Ignoring request to disable "
234 "SuperSpeed port.\n");
235 return;
236 }
237
Sarah Sharp6219c042009-12-09 15:59:11 -0800238 /* Write 1 to disable the port */
239 xhci_writel(xhci, port_status | PORT_PE, addr);
240 port_status = xhci_readl(xhci, addr);
241 xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
242 wIndex, port_status);
243}
244
Sarah Sharp34fb5622009-12-09 15:59:08 -0800245static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
246 u16 wIndex, u32 __iomem *addr, u32 port_status)
247{
248 char *port_change_bit;
249 u32 status;
250
251 switch (wValue) {
252 case USB_PORT_FEAT_C_RESET:
253 status = PORT_RC;
254 port_change_bit = "reset";
255 break;
256 case USB_PORT_FEAT_C_CONNECTION:
257 status = PORT_CSC;
258 port_change_bit = "connect";
259 break;
260 case USB_PORT_FEAT_C_OVER_CURRENT:
261 status = PORT_OCC;
262 port_change_bit = "over-current";
263 break;
Sarah Sharp6219c042009-12-09 15:59:11 -0800264 case USB_PORT_FEAT_C_ENABLE:
265 status = PORT_PEC;
266 port_change_bit = "enable/disable";
267 break;
Andiry Xube88fe42010-10-14 07:22:57 -0700268 case USB_PORT_FEAT_C_SUSPEND:
269 status = PORT_PLC;
270 port_change_bit = "suspend/resume";
271 break;
Sarah Sharp34fb5622009-12-09 15:59:08 -0800272 default:
273 /* Should never happen */
274 return;
275 }
276 /* Change bits are all write 1 to clear */
277 xhci_writel(xhci, port_status | status, addr);
278 port_status = xhci_readl(xhci, addr);
279 xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
280 port_change_bit, wIndex, port_status);
281}
282
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700283int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
284 u16 wIndex, char *buf, u16 wLength)
285{
286 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
287 int ports;
288 unsigned long flags;
Andiry Xu56192532010-10-14 07:23:00 -0700289 u32 temp, temp1, status;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700290 int retval = 0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800291 u32 __iomem *port_array[15 + USB_MAXCHILDREN];
292 int i;
Andiry Xube88fe42010-10-14 07:22:57 -0700293 int slot_id;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700294
295 ports = HCS_MAX_PORTS(xhci->hcs_params1);
Sarah Sharp5308a912010-12-01 11:34:59 -0800296 for (i = 0; i < ports; i++) {
297 if (i < xhci->num_usb3_ports)
298 port_array[i] = xhci->usb3_ports[i];
299 else
300 port_array[i] =
301 xhci->usb2_ports[i - xhci->num_usb3_ports];
302 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700303
304 spin_lock_irqsave(&xhci->lock, flags);
305 switch (typeReq) {
306 case GetHubStatus:
307 /* No power source, over-current reported per port */
308 memset(buf, 0, 4);
309 break;
310 case GetHubDescriptor:
311 xhci_hub_descriptor(xhci, (struct usb_hub_descriptor *) buf);
312 break;
313 case GetPortStatus:
314 if (!wIndex || wIndex > ports)
315 goto error;
316 wIndex--;
317 status = 0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800318 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700319 xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
320
321 /* wPortChange bits */
322 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500323 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700324 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500325 status |= USB_PORT_STAT_C_ENABLE << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700326 if ((temp & PORT_OCC))
Alan Stern749da5f2010-03-04 17:05:08 -0500327 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700328 /*
Andiry Xube88fe42010-10-14 07:22:57 -0700329 * FIXME ignoring reset and USB 2.1/3.0 specific
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700330 * changes
331 */
Andiry Xube88fe42010-10-14 07:22:57 -0700332 if ((temp & PORT_PLS_MASK) == XDEV_U3
333 && (temp & PORT_POWER))
334 status |= 1 << USB_PORT_FEAT_SUSPEND;
Andiry Xu56192532010-10-14 07:23:00 -0700335 if ((temp & PORT_PLS_MASK) == XDEV_RESUME) {
336 if ((temp & PORT_RESET) || !(temp & PORT_PE))
337 goto error;
338 if (!DEV_SUPERSPEED(temp) && time_after_eq(jiffies,
339 xhci->resume_done[wIndex])) {
340 xhci_dbg(xhci, "Resume USB2 port %d\n",
341 wIndex + 1);
342 xhci->resume_done[wIndex] = 0;
343 temp1 = xhci_port_state_to_neutral(temp);
344 temp1 &= ~PORT_PLS_MASK;
345 temp1 |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800346 xhci_writel(xhci, temp1, port_array[wIndex]);
Andiry Xu56192532010-10-14 07:23:00 -0700347
348 xhci_dbg(xhci, "set port %d resume\n",
349 wIndex + 1);
350 slot_id = xhci_find_slot_id_by_port(xhci,
351 wIndex + 1);
352 if (!slot_id) {
353 xhci_dbg(xhci, "slot_id is zero\n");
354 goto error;
355 }
356 xhci_ring_device(xhci, slot_id);
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800357 xhci->port_c_suspend |= 1 << wIndex;
358 xhci->suspended_ports &= ~(1 << wIndex);
Andiry Xu56192532010-10-14 07:23:00 -0700359 }
360 }
Andiry Xube88fe42010-10-14 07:22:57 -0700361 if ((temp & PORT_PLS_MASK) == XDEV_U0
362 && (temp & PORT_POWER)
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800363 && (xhci->suspended_ports & (1 << wIndex))) {
364 xhci->suspended_ports &= ~(1 << wIndex);
365 xhci->port_c_suspend |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700366 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700367 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500368 status |= USB_PORT_STAT_CONNECTION;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700369 status |= xhci_port_speed(temp);
370 }
371 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500372 status |= USB_PORT_STAT_ENABLE;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700373 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500374 status |= USB_PORT_STAT_OVERCURRENT;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700375 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500376 status |= USB_PORT_STAT_RESET;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700377 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500378 status |= USB_PORT_STAT_POWER;
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800379 if (xhci->port_c_suspend & (1 << wIndex))
Andiry Xube88fe42010-10-14 07:22:57 -0700380 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700381 xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
382 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
383 break;
384 case SetPortFeature:
385 wIndex &= 0xff;
386 if (!wIndex || wIndex > ports)
387 goto error;
388 wIndex--;
Sarah Sharp5308a912010-12-01 11:34:59 -0800389 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700390 temp = xhci_port_state_to_neutral(temp);
391 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -0700392 case USB_PORT_FEAT_SUSPEND:
Sarah Sharp5308a912010-12-01 11:34:59 -0800393 temp = xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700394 /* In spec software should not attempt to suspend
395 * a port unless the port reports that it is in the
396 * enabled (PED = ‘1’,PLS < ‘3’) state.
397 */
398 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
399 || (temp & PORT_PLS_MASK) >= XDEV_U3) {
400 xhci_warn(xhci, "USB core suspending device "
401 "not in U0/U1/U2.\n");
402 goto error;
403 }
404
405 slot_id = xhci_find_slot_id_by_port(xhci, wIndex + 1);
406 if (!slot_id) {
407 xhci_warn(xhci, "slot_id is zero\n");
408 goto error;
409 }
410 /* unlock to execute stop endpoint commands */
411 spin_unlock_irqrestore(&xhci->lock, flags);
412 xhci_stop_device(xhci, slot_id, 1);
413 spin_lock_irqsave(&xhci->lock, flags);
414
415 temp = xhci_port_state_to_neutral(temp);
416 temp &= ~PORT_PLS_MASK;
417 temp |= PORT_LINK_STROBE | XDEV_U3;
Sarah Sharp5308a912010-12-01 11:34:59 -0800418 xhci_writel(xhci, temp, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700419
420 spin_unlock_irqrestore(&xhci->lock, flags);
421 msleep(10); /* wait device to enter */
422 spin_lock_irqsave(&xhci->lock, flags);
423
Sarah Sharp5308a912010-12-01 11:34:59 -0800424 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800425 xhci->suspended_ports |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700426 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700427 case USB_PORT_FEAT_POWER:
428 /*
429 * Turn on ports, even if there isn't per-port switching.
430 * HC will report connect events even before this is set.
431 * However, khubd will ignore the roothub events until
432 * the roothub is registered.
433 */
Sarah Sharp5308a912010-12-01 11:34:59 -0800434 xhci_writel(xhci, temp | PORT_POWER,
435 port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700436
Sarah Sharp5308a912010-12-01 11:34:59 -0800437 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700438 xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
439 break;
440 case USB_PORT_FEAT_RESET:
441 temp = (temp | PORT_RESET);
Sarah Sharp5308a912010-12-01 11:34:59 -0800442 xhci_writel(xhci, temp, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700443
Sarah Sharp5308a912010-12-01 11:34:59 -0800444 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700445 xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
446 break;
447 default:
448 goto error;
449 }
Sarah Sharp5308a912010-12-01 11:34:59 -0800450 /* unblock any posted writes */
451 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700452 break;
453 case ClearPortFeature:
454 if (!wIndex || wIndex > ports)
455 goto error;
456 wIndex--;
Sarah Sharp5308a912010-12-01 11:34:59 -0800457 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700458 temp = xhci_port_state_to_neutral(temp);
459 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -0700460 case USB_PORT_FEAT_SUSPEND:
Sarah Sharp5308a912010-12-01 11:34:59 -0800461 temp = xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700462 xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
463 xhci_dbg(xhci, "PORTSC %04x\n", temp);
464 if (temp & PORT_RESET)
465 goto error;
466 if (temp & XDEV_U3) {
467 if ((temp & PORT_PE) == 0)
468 goto error;
469 if (DEV_SUPERSPEED(temp)) {
470 temp = xhci_port_state_to_neutral(temp);
471 temp &= ~PORT_PLS_MASK;
472 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800473 xhci_writel(xhci, temp,
474 port_array[wIndex]);
475 xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700476 } else {
477 temp = xhci_port_state_to_neutral(temp);
478 temp &= ~PORT_PLS_MASK;
479 temp |= PORT_LINK_STROBE | XDEV_RESUME;
Sarah Sharp5308a912010-12-01 11:34:59 -0800480 xhci_writel(xhci, temp,
481 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700482
483 spin_unlock_irqrestore(&xhci->lock,
484 flags);
485 msleep(20);
486 spin_lock_irqsave(&xhci->lock, flags);
487
Sarah Sharp5308a912010-12-01 11:34:59 -0800488 temp = xhci_readl(xhci,
489 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700490 temp = xhci_port_state_to_neutral(temp);
491 temp &= ~PORT_PLS_MASK;
492 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800493 xhci_writel(xhci, temp,
494 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700495 }
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800496 xhci->port_c_suspend |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700497 }
498
499 slot_id = xhci_find_slot_id_by_port(xhci, wIndex + 1);
500 if (!slot_id) {
501 xhci_dbg(xhci, "slot_id is zero\n");
502 goto error;
503 }
504 xhci_ring_device(xhci, slot_id);
505 break;
506 case USB_PORT_FEAT_C_SUSPEND:
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800507 xhci->port_c_suspend &= ~(1 << wIndex);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700508 case USB_PORT_FEAT_C_RESET:
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700509 case USB_PORT_FEAT_C_CONNECTION:
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700510 case USB_PORT_FEAT_C_OVER_CURRENT:
Sarah Sharp6219c042009-12-09 15:59:11 -0800511 case USB_PORT_FEAT_C_ENABLE:
Sarah Sharp34fb5622009-12-09 15:59:08 -0800512 xhci_clear_port_change_bit(xhci, wValue, wIndex,
Sarah Sharp5308a912010-12-01 11:34:59 -0800513 port_array[wIndex], temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700514 break;
Sarah Sharp6219c042009-12-09 15:59:11 -0800515 case USB_PORT_FEAT_ENABLE:
Sarah Sharp5308a912010-12-01 11:34:59 -0800516 xhci_disable_port(xhci, wIndex,
517 port_array[wIndex], temp);
Sarah Sharp6219c042009-12-09 15:59:11 -0800518 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700519 default:
520 goto error;
521 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700522 break;
523 default:
524error:
525 /* "stall" on error */
526 retval = -EPIPE;
527 }
528 spin_unlock_irqrestore(&xhci->lock, flags);
529 return retval;
530}
531
532/*
533 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
534 * Ports are 0-indexed from the HCD point of view,
535 * and 1-indexed from the USB core pointer of view.
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700536 *
537 * Note that the status change bits will be cleared as soon as a port status
538 * change event is generated, so we use the saved status from that event.
539 */
540int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
541{
542 unsigned long flags;
543 u32 temp, status;
Andiry Xu56192532010-10-14 07:23:00 -0700544 u32 mask;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700545 int i, retval;
546 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
547 int ports;
Sarah Sharp5308a912010-12-01 11:34:59 -0800548 u32 __iomem *port_array[15 + USB_MAXCHILDREN];
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700549
550 ports = HCS_MAX_PORTS(xhci->hcs_params1);
Sarah Sharp5308a912010-12-01 11:34:59 -0800551 for (i = 0; i < ports; i++) {
552 if (i < xhci->num_usb3_ports)
553 port_array[i] = xhci->usb3_ports[i];
554 else
555 port_array[i] =
556 xhci->usb2_ports[i - xhci->num_usb3_ports];
557 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700558
559 /* Initial status is no changes */
William Gulland419a8e812010-05-12 10:20:34 -0700560 retval = (ports + 8) / 8;
561 memset(buf, 0, retval);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700562 status = 0;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700563
Andiry Xu56192532010-10-14 07:23:00 -0700564 mask = PORT_CSC | PORT_PEC | PORT_OCC;
565
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700566 spin_lock_irqsave(&xhci->lock, flags);
567 /* For each port, did anything change? If so, set that bit in buf. */
568 for (i = 0; i < ports; i++) {
Sarah Sharp5308a912010-12-01 11:34:59 -0800569 temp = xhci_readl(xhci, port_array[i]);
Andiry Xu56192532010-10-14 07:23:00 -0700570 if ((temp & mask) != 0 ||
Sarah Sharp1d5810b2010-12-09 14:52:41 -0800571 (xhci->port_c_suspend & 1 << i) ||
Andiry Xu56192532010-10-14 07:23:00 -0700572 (xhci->resume_done[i] && time_after_eq(
573 jiffies, xhci->resume_done[i]))) {
William Gulland419a8e812010-05-12 10:20:34 -0700574 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700575 status = 1;
576 }
577 }
578 spin_unlock_irqrestore(&xhci->lock, flags);
579 return status ? retval : 0;
580}
Andiry Xu9777e3c2010-10-14 07:23:03 -0700581
582#ifdef CONFIG_PM
583
584int xhci_bus_suspend(struct usb_hcd *hcd)
585{
586 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp518e8482010-12-15 11:56:29 -0800587 int max_ports, port_index;
Sarah Sharp5308a912010-12-01 11:34:59 -0800588 u32 __iomem *port_array[15 + USB_MAXCHILDREN];
589 int i;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700590 unsigned long flags;
591
592 xhci_dbg(xhci, "suspend root hub\n");
Sarah Sharp518e8482010-12-15 11:56:29 -0800593 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
Sarah Sharp5308a912010-12-01 11:34:59 -0800594 for (i = 0; i < max_ports; i++) {
595 if (i < xhci->num_usb3_ports)
596 port_array[i] = xhci->usb3_ports[i];
597 else
598 port_array[i] =
599 xhci->usb2_ports[i - xhci->num_usb3_ports];
600 }
Andiry Xu9777e3c2010-10-14 07:23:03 -0700601
602 spin_lock_irqsave(&xhci->lock, flags);
603
604 if (hcd->self.root_hub->do_remote_wakeup) {
Sarah Sharp518e8482010-12-15 11:56:29 -0800605 port_index = max_ports;
606 while (port_index--) {
607 if (xhci->resume_done[port_index] != 0) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700608 spin_unlock_irqrestore(&xhci->lock, flags);
609 xhci_dbg(xhci, "suspend failed because "
610 "port %d is resuming\n",
Sarah Sharp518e8482010-12-15 11:56:29 -0800611 port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700612 return -EBUSY;
613 }
614 }
615 }
616
Sarah Sharp518e8482010-12-15 11:56:29 -0800617 port_index = max_ports;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700618 xhci->bus_suspended = 0;
Sarah Sharp518e8482010-12-15 11:56:29 -0800619 while (port_index--) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700620 /* suspend the port if the port is not suspended */
Andiry Xu9777e3c2010-10-14 07:23:03 -0700621 u32 t1, t2;
622 int slot_id;
623
Sarah Sharp5308a912010-12-01 11:34:59 -0800624 t1 = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700625 t2 = xhci_port_state_to_neutral(t1);
626
627 if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
Sarah Sharp518e8482010-12-15 11:56:29 -0800628 xhci_dbg(xhci, "port %d not suspended\n", port_index);
629 slot_id = xhci_find_slot_id_by_port(xhci,
630 port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700631 if (slot_id) {
632 spin_unlock_irqrestore(&xhci->lock, flags);
633 xhci_stop_device(xhci, slot_id, 1);
634 spin_lock_irqsave(&xhci->lock, flags);
635 }
636 t2 &= ~PORT_PLS_MASK;
637 t2 |= PORT_LINK_STROBE | XDEV_U3;
Sarah Sharp518e8482010-12-15 11:56:29 -0800638 set_bit(port_index, &xhci->bus_suspended);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700639 }
640 if (hcd->self.root_hub->do_remote_wakeup) {
641 if (t1 & PORT_CONNECT) {
642 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
643 t2 &= ~PORT_WKCONN_E;
644 } else {
645 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
646 t2 &= ~PORT_WKDISC_E;
647 }
648 } else
649 t2 &= ~PORT_WAKE_BITS;
650
651 t1 = xhci_port_state_to_neutral(t1);
652 if (t1 != t2)
Sarah Sharp5308a912010-12-01 11:34:59 -0800653 xhci_writel(xhci, t2, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700654
655 if (DEV_HIGHSPEED(t1)) {
656 /* enable remote wake up for USB 2.0 */
657 u32 __iomem *addr;
658 u32 tmp;
659
Sarah Sharp5308a912010-12-01 11:34:59 -0800660 /* Add one to the port status register address to get
661 * the port power control register address.
662 */
663 addr = port_array[port_index] + 1;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700664 tmp = xhci_readl(xhci, addr);
665 tmp |= PORT_RWE;
666 xhci_writel(xhci, tmp, addr);
667 }
668 }
669 hcd->state = HC_STATE_SUSPENDED;
670 xhci->next_statechange = jiffies + msecs_to_jiffies(10);
671 spin_unlock_irqrestore(&xhci->lock, flags);
672 return 0;
673}
674
675int xhci_bus_resume(struct usb_hcd *hcd)
676{
677 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp518e8482010-12-15 11:56:29 -0800678 int max_ports, port_index;
Sarah Sharp5308a912010-12-01 11:34:59 -0800679 u32 __iomem *port_array[15 + USB_MAXCHILDREN];
680 int i;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700681 u32 temp;
682 unsigned long flags;
683
684 xhci_dbg(xhci, "resume root hub\n");
Sarah Sharp518e8482010-12-15 11:56:29 -0800685 max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
Sarah Sharp5308a912010-12-01 11:34:59 -0800686 for (i = 0; i < max_ports; i++) {
687 if (i < xhci->num_usb3_ports)
688 port_array[i] = xhci->usb3_ports[i];
689 else
690 port_array[i] =
691 xhci->usb2_ports[i - xhci->num_usb3_ports];
692 }
Andiry Xu9777e3c2010-10-14 07:23:03 -0700693
694 if (time_before(jiffies, xhci->next_statechange))
695 msleep(5);
696
697 spin_lock_irqsave(&xhci->lock, flags);
698 if (!HCD_HW_ACCESSIBLE(hcd)) {
699 spin_unlock_irqrestore(&xhci->lock, flags);
700 return -ESHUTDOWN;
701 }
702
703 /* delay the irqs */
704 temp = xhci_readl(xhci, &xhci->op_regs->command);
705 temp &= ~CMD_EIE;
706 xhci_writel(xhci, temp, &xhci->op_regs->command);
707
Sarah Sharp518e8482010-12-15 11:56:29 -0800708 port_index = max_ports;
709 while (port_index--) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700710 /* Check whether need resume ports. If needed
711 resume port and disable remote wakeup */
Andiry Xu9777e3c2010-10-14 07:23:03 -0700712 u32 temp;
713 int slot_id;
714
Sarah Sharp5308a912010-12-01 11:34:59 -0800715 temp = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700716 if (DEV_SUPERSPEED(temp))
717 temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
718 else
719 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Sarah Sharp518e8482010-12-15 11:56:29 -0800720 if (test_bit(port_index, &xhci->bus_suspended) &&
Andiry Xu9777e3c2010-10-14 07:23:03 -0700721 (temp & PORT_PLS_MASK)) {
722 if (DEV_SUPERSPEED(temp)) {
723 temp = xhci_port_state_to_neutral(temp);
724 temp &= ~PORT_PLS_MASK;
725 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800726 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700727 } else {
728 temp = xhci_port_state_to_neutral(temp);
729 temp &= ~PORT_PLS_MASK;
730 temp |= PORT_LINK_STROBE | XDEV_RESUME;
Sarah Sharp5308a912010-12-01 11:34:59 -0800731 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700732
733 spin_unlock_irqrestore(&xhci->lock, flags);
734 msleep(20);
735 spin_lock_irqsave(&xhci->lock, flags);
736
Sarah Sharp5308a912010-12-01 11:34:59 -0800737 temp = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700738 temp = xhci_port_state_to_neutral(temp);
739 temp &= ~PORT_PLS_MASK;
740 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800741 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700742 }
Sarah Sharp518e8482010-12-15 11:56:29 -0800743 slot_id = xhci_find_slot_id_by_port(xhci, port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700744 if (slot_id)
745 xhci_ring_device(xhci, slot_id);
746 } else
Sarah Sharp5308a912010-12-01 11:34:59 -0800747 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700748
749 if (DEV_HIGHSPEED(temp)) {
750 /* disable remote wake up for USB 2.0 */
751 u32 __iomem *addr;
752 u32 tmp;
753
Sarah Sharp5308a912010-12-01 11:34:59 -0800754 /* Add one to the port status register address to get
755 * the port power control register address.
756 */
757 addr = port_array[port_index] + 1;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700758 tmp = xhci_readl(xhci, addr);
759 tmp &= ~PORT_RWE;
760 xhci_writel(xhci, tmp, addr);
761 }
762 }
763
764 (void) xhci_readl(xhci, &xhci->op_regs->command);
765
766 xhci->next_statechange = jiffies + msecs_to_jiffies(5);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700767 /* re-enable irqs */
768 temp = xhci_readl(xhci, &xhci->op_regs->command);
769 temp |= CMD_EIE;
770 xhci_writel(xhci, temp, &xhci->op_regs->command);
771 temp = xhci_readl(xhci, &xhci->op_regs->command);
772
773 spin_unlock_irqrestore(&xhci->lock, flags);
774 return 0;
775}
776
Sarah Sharp436a3892010-10-15 14:59:15 -0700777#endif /* CONFIG_PM */