blob: ee4af076a8fe5c5846cd03ed83a9d2ab73046a32 [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 Sharpf6ff0ac2010-12-16 11:21:10 -080031static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
Sarah Sharp0f2a7932009-04-27 19:57:12 -070032 struct usb_hub_descriptor *desc)
33{
34 int ports;
35 u16 temp;
36
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -080037 if (hcd->speed == HCD_USB3)
38 ports = xhci->num_usb3_ports;
39 else
40 ports = xhci->num_usb2_ports;
41
42 /* FIXME: return a USB 3.0 hub descriptor if this request was for the
43 * USB3 roothub.
44 */
Sarah Sharp0f2a7932009-04-27 19:57:12 -070045
46 /* USB 3.0 hubs have a different descriptor, but we fake this for now */
47 desc->bDescriptorType = 0x29;
48 desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
49 desc->bHubContrCurrent = 0;
50
51 desc->bNbrPorts = ports;
52 temp = 1 + (ports / 8);
53 desc->bDescLength = 7 + 2 * temp;
54
John Youndbe79bb2001-09-17 00:00:00 -070055 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
56 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -070057
58 /* Ugh, these should be #defines, FIXME */
59 /* Using table 11-13 in USB 2.0 spec. */
60 temp = 0;
61 /* Bits 1:0 - support port power switching, or power always on */
62 if (HCC_PPC(xhci->hcc_params))
63 temp |= 0x0001;
64 else
65 temp |= 0x0002;
66 /* Bit 2 - root hubs are not part of a compound device */
67 /* Bits 4:3 - individual port over current protection */
68 temp |= 0x0008;
69 /* Bits 6:5 - no TTs in root ports */
70 /* Bit 7 - no port indicators */
71 desc->wHubCharacteristics = (__force __u16) cpu_to_le16(temp);
72}
73
74static unsigned int xhci_port_speed(unsigned int port_status)
75{
76 if (DEV_LOWSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050077 return USB_PORT_STAT_LOW_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070078 if (DEV_HIGHSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050079 return USB_PORT_STAT_HIGH_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070080 if (DEV_SUPERSPEED(port_status))
Alan Stern288ead42010-03-04 11:32:30 -050081 return USB_PORT_STAT_SUPER_SPEED;
Sarah Sharp0f2a7932009-04-27 19:57:12 -070082 /*
83 * FIXME: Yes, we should check for full speed, but the core uses that as
84 * a default in portspeed() in usb/core/hub.c (which is the only place
Alan Stern288ead42010-03-04 11:32:30 -050085 * USB_PORT_STAT_*_SPEED is used).
Sarah Sharp0f2a7932009-04-27 19:57:12 -070086 */
87 return 0;
88}
89
90/*
91 * These bits are Read Only (RO) and should be saved and written to the
92 * registers: 0, 3, 10:13, 30
93 * connect status, over-current status, port speed, and device removable.
94 * connect status and port speed are also sticky - meaning they're in
95 * the AUX well and they aren't changed by a hot, warm, or cold reset.
96 */
97#define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
98/*
99 * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
100 * bits 5:8, 9, 14:15, 25:27
101 * link state, port power, port indicator state, "wake on" enable state
102 */
103#define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
104/*
105 * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
106 * bit 4 (port reset)
107 */
108#define XHCI_PORT_RW1S ((1<<4))
109/*
110 * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
111 * bits 1, 17, 18, 19, 20, 21, 22, 23
112 * port enable/disable, and
113 * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
114 * over-current, reset, link state, and L1 change
115 */
116#define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
117/*
118 * Bit 16 is RW, and writing a '1' to it causes the link state control to be
119 * latched in
120 */
121#define XHCI_PORT_RW ((1<<16))
122/*
123 * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
124 * bits 2, 24, 28:31
125 */
126#define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
127
128/*
129 * Given a port state, this function returns a value that would result in the
130 * port being in the same state, if the value was written to the port status
131 * control register.
132 * Save Read Only (RO) bits and save read/write bits where
133 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
134 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
135 */
Andiry Xu56192532010-10-14 07:23:00 -0700136u32 xhci_port_state_to_neutral(u32 state)
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700137{
138 /* Save read-only status and port state */
139 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
140}
141
Andiry Xube88fe42010-10-14 07:22:57 -0700142/*
143 * find slot id based on port number.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800144 * @port: The one-based port number from one of the two split roothubs.
Andiry Xube88fe42010-10-14 07:22:57 -0700145 */
Sarah Sharp52336302010-12-16 10:49:09 -0800146int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
147 u16 port)
Andiry Xube88fe42010-10-14 07:22:57 -0700148{
149 int slot_id;
150 int i;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800151 enum usb_device_speed speed;
Andiry Xube88fe42010-10-14 07:22:57 -0700152
153 slot_id = 0;
154 for (i = 0; i < MAX_HC_SLOTS; i++) {
155 if (!xhci->devs[i])
156 continue;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800157 speed = xhci->devs[i]->udev->speed;
158 if (((speed == USB_SPEED_SUPER) == (hcd->speed == HCD_USB3))
159 && xhci->devs[i]->port == port) {
Andiry Xube88fe42010-10-14 07:22:57 -0700160 slot_id = i;
161 break;
162 }
163 }
164
165 return slot_id;
166}
167
168/*
169 * Stop device
170 * It issues stop endpoint command for EP 0 to 30. And wait the last command
171 * to complete.
172 * suspend will set to 1, if suspend bit need to set in command.
173 */
174static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
175{
176 struct xhci_virt_device *virt_dev;
177 struct xhci_command *cmd;
178 unsigned long flags;
179 int timeleft;
180 int ret;
181 int i;
182
183 ret = 0;
184 virt_dev = xhci->devs[slot_id];
185 cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
186 if (!cmd) {
187 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
188 return -ENOMEM;
189 }
190
191 spin_lock_irqsave(&xhci->lock, flags);
192 for (i = LAST_EP_INDEX; i > 0; i--) {
193 if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue)
194 xhci_queue_stop_endpoint(xhci, slot_id, i, suspend);
195 }
196 cmd->command_trb = xhci->cmd_ring->enqueue;
197 list_add_tail(&cmd->cmd_list, &virt_dev->cmd_list);
198 xhci_queue_stop_endpoint(xhci, slot_id, 0, suspend);
199 xhci_ring_cmd_db(xhci);
200 spin_unlock_irqrestore(&xhci->lock, flags);
201
202 /* Wait for last stop endpoint command to finish */
203 timeleft = wait_for_completion_interruptible_timeout(
204 cmd->completion,
205 USB_CTRL_SET_TIMEOUT);
206 if (timeleft <= 0) {
207 xhci_warn(xhci, "%s while waiting for stop endpoint command\n",
208 timeleft == 0 ? "Timeout" : "Signal");
209 spin_lock_irqsave(&xhci->lock, flags);
210 /* The timeout might have raced with the event ring handler, so
211 * only delete from the list if the item isn't poisoned.
212 */
213 if (cmd->cmd_list.next != LIST_POISON1)
214 list_del(&cmd->cmd_list);
215 spin_unlock_irqrestore(&xhci->lock, flags);
216 ret = -ETIME;
217 goto command_cleanup;
218 }
219
220command_cleanup:
221 xhci_free_command(xhci, cmd);
222 return ret;
223}
224
225/*
226 * Ring device, it rings the all doorbells unconditionally.
227 */
Andiry Xu56192532010-10-14 07:23:00 -0700228void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
Andiry Xube88fe42010-10-14 07:22:57 -0700229{
230 int i;
231
232 for (i = 0; i < LAST_EP_INDEX + 1; i++)
233 if (xhci->devs[slot_id]->eps[i].ring &&
234 xhci->devs[slot_id]->eps[i].ring->dequeue)
235 xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
236
237 return;
238}
239
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800240static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
241 u16 wIndex, u32 __iomem *addr, u32 port_status)
Sarah Sharp6219c0472009-12-09 15:59:11 -0800242{
Sarah Sharp6dd0a3a72010-11-16 15:58:52 -0800243 /* Don't allow the USB core to disable SuperSpeed ports. */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800244 if (hcd->speed == HCD_USB3) {
Sarah Sharp6dd0a3a72010-11-16 15:58:52 -0800245 xhci_dbg(xhci, "Ignoring request to disable "
246 "SuperSpeed port.\n");
247 return;
248 }
249
Sarah Sharp6219c0472009-12-09 15:59:11 -0800250 /* Write 1 to disable the port */
251 xhci_writel(xhci, port_status | PORT_PE, addr);
252 port_status = xhci_readl(xhci, addr);
253 xhci_dbg(xhci, "disable port, actual port %d status = 0x%x\n",
254 wIndex, port_status);
255}
256
Sarah Sharp34fb5622009-12-09 15:59:08 -0800257static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
258 u16 wIndex, u32 __iomem *addr, u32 port_status)
259{
260 char *port_change_bit;
261 u32 status;
262
263 switch (wValue) {
264 case USB_PORT_FEAT_C_RESET:
265 status = PORT_RC;
266 port_change_bit = "reset";
267 break;
268 case USB_PORT_FEAT_C_CONNECTION:
269 status = PORT_CSC;
270 port_change_bit = "connect";
271 break;
272 case USB_PORT_FEAT_C_OVER_CURRENT:
273 status = PORT_OCC;
274 port_change_bit = "over-current";
275 break;
Sarah Sharp6219c0472009-12-09 15:59:11 -0800276 case USB_PORT_FEAT_C_ENABLE:
277 status = PORT_PEC;
278 port_change_bit = "enable/disable";
279 break;
Andiry Xube88fe42010-10-14 07:22:57 -0700280 case USB_PORT_FEAT_C_SUSPEND:
281 status = PORT_PLC;
282 port_change_bit = "suspend/resume";
283 break;
Sarah Sharp34fb5622009-12-09 15:59:08 -0800284 default:
285 /* Should never happen */
286 return;
287 }
288 /* Change bits are all write 1 to clear */
289 xhci_writel(xhci, port_status | status, addr);
290 port_status = xhci_readl(xhci, addr);
291 xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
292 port_change_bit, wIndex, port_status);
293}
294
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700295int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
296 u16 wIndex, char *buf, u16 wLength)
297{
298 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
299 int ports;
300 unsigned long flags;
Andiry Xu56192532010-10-14 07:23:00 -0700301 u32 temp, temp1, status;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700302 int retval = 0;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800303 u32 __iomem **port_array;
Andiry Xube88fe42010-10-14 07:22:57 -0700304 int slot_id;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800305 struct xhci_bus_state *bus_state;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700306
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800307 if (hcd->speed == HCD_USB3) {
308 ports = xhci->num_usb3_ports;
309 port_array = xhci->usb3_ports;
310 } else {
311 ports = xhci->num_usb2_ports;
312 port_array = xhci->usb2_ports;
Sarah Sharp5308a912010-12-01 11:34:59 -0800313 }
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800314 bus_state = &xhci->bus_state[hcd_index(hcd)];
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700315
316 spin_lock_irqsave(&xhci->lock, flags);
317 switch (typeReq) {
318 case GetHubStatus:
319 /* No power source, over-current reported per port */
320 memset(buf, 0, 4);
321 break;
322 case GetHubDescriptor:
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800323 xhci_hub_descriptor(hcd, xhci,
324 (struct usb_hub_descriptor *) buf);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700325 break;
326 case GetPortStatus:
327 if (!wIndex || wIndex > ports)
328 goto error;
329 wIndex--;
330 status = 0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800331 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700332 xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
333
334 /* wPortChange bits */
335 if (temp & PORT_CSC)
Alan Stern749da5f2010-03-04 17:05:08 -0500336 status |= USB_PORT_STAT_C_CONNECTION << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700337 if (temp & PORT_PEC)
Alan Stern749da5f2010-03-04 17:05:08 -0500338 status |= USB_PORT_STAT_C_ENABLE << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700339 if ((temp & PORT_OCC))
Alan Stern749da5f2010-03-04 17:05:08 -0500340 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700341 /*
Andiry Xube88fe42010-10-14 07:22:57 -0700342 * FIXME ignoring reset and USB 2.1/3.0 specific
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700343 * changes
344 */
Andiry Xube88fe42010-10-14 07:22:57 -0700345 if ((temp & PORT_PLS_MASK) == XDEV_U3
346 && (temp & PORT_POWER))
347 status |= 1 << USB_PORT_FEAT_SUSPEND;
Andiry Xu56192532010-10-14 07:23:00 -0700348 if ((temp & PORT_PLS_MASK) == XDEV_RESUME) {
349 if ((temp & PORT_RESET) || !(temp & PORT_PE))
350 goto error;
351 if (!DEV_SUPERSPEED(temp) && time_after_eq(jiffies,
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800352 bus_state->resume_done[wIndex])) {
Andiry Xu56192532010-10-14 07:23:00 -0700353 xhci_dbg(xhci, "Resume USB2 port %d\n",
354 wIndex + 1);
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800355 bus_state->resume_done[wIndex] = 0;
Andiry Xu56192532010-10-14 07:23:00 -0700356 temp1 = xhci_port_state_to_neutral(temp);
357 temp1 &= ~PORT_PLS_MASK;
358 temp1 |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800359 xhci_writel(xhci, temp1, port_array[wIndex]);
Andiry Xu56192532010-10-14 07:23:00 -0700360
361 xhci_dbg(xhci, "set port %d resume\n",
362 wIndex + 1);
Sarah Sharp52336302010-12-16 10:49:09 -0800363 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
Andiry Xu56192532010-10-14 07:23:00 -0700364 wIndex + 1);
365 if (!slot_id) {
366 xhci_dbg(xhci, "slot_id is zero\n");
367 goto error;
368 }
369 xhci_ring_device(xhci, slot_id);
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800370 bus_state->port_c_suspend |= 1 << wIndex;
371 bus_state->suspended_ports &= ~(1 << wIndex);
Andiry Xu56192532010-10-14 07:23:00 -0700372 }
373 }
Andiry Xube88fe42010-10-14 07:22:57 -0700374 if ((temp & PORT_PLS_MASK) == XDEV_U0
375 && (temp & PORT_POWER)
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800376 && (bus_state->suspended_ports & (1 << wIndex))) {
377 bus_state->suspended_ports &= ~(1 << wIndex);
378 bus_state->port_c_suspend |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700379 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700380 if (temp & PORT_CONNECT) {
Alan Stern749da5f2010-03-04 17:05:08 -0500381 status |= USB_PORT_STAT_CONNECTION;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700382 status |= xhci_port_speed(temp);
383 }
384 if (temp & PORT_PE)
Alan Stern749da5f2010-03-04 17:05:08 -0500385 status |= USB_PORT_STAT_ENABLE;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700386 if (temp & PORT_OC)
Alan Stern749da5f2010-03-04 17:05:08 -0500387 status |= USB_PORT_STAT_OVERCURRENT;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700388 if (temp & PORT_RESET)
Alan Stern749da5f2010-03-04 17:05:08 -0500389 status |= USB_PORT_STAT_RESET;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700390 if (temp & PORT_POWER)
Alan Stern749da5f2010-03-04 17:05:08 -0500391 status |= USB_PORT_STAT_POWER;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800392 if (bus_state->port_c_suspend & (1 << wIndex))
Andiry Xube88fe42010-10-14 07:22:57 -0700393 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700394 xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
395 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
396 break;
397 case SetPortFeature:
398 wIndex &= 0xff;
399 if (!wIndex || wIndex > ports)
400 goto error;
401 wIndex--;
Sarah Sharp5308a912010-12-01 11:34:59 -0800402 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700403 temp = xhci_port_state_to_neutral(temp);
404 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -0700405 case USB_PORT_FEAT_SUSPEND:
Sarah Sharp5308a912010-12-01 11:34:59 -0800406 temp = xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700407 /* In spec software should not attempt to suspend
408 * a port unless the port reports that it is in the
409 * enabled (PED = ‘1’,PLS < ‘3’) state.
410 */
411 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
412 || (temp & PORT_PLS_MASK) >= XDEV_U3) {
413 xhci_warn(xhci, "USB core suspending device "
414 "not in U0/U1/U2.\n");
415 goto error;
416 }
417
Sarah Sharp52336302010-12-16 10:49:09 -0800418 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
419 wIndex + 1);
Andiry Xube88fe42010-10-14 07:22:57 -0700420 if (!slot_id) {
421 xhci_warn(xhci, "slot_id is zero\n");
422 goto error;
423 }
424 /* unlock to execute stop endpoint commands */
425 spin_unlock_irqrestore(&xhci->lock, flags);
426 xhci_stop_device(xhci, slot_id, 1);
427 spin_lock_irqsave(&xhci->lock, flags);
428
429 temp = xhci_port_state_to_neutral(temp);
430 temp &= ~PORT_PLS_MASK;
431 temp |= PORT_LINK_STROBE | XDEV_U3;
Sarah Sharp5308a912010-12-01 11:34:59 -0800432 xhci_writel(xhci, temp, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700433
434 spin_unlock_irqrestore(&xhci->lock, flags);
435 msleep(10); /* wait device to enter */
436 spin_lock_irqsave(&xhci->lock, flags);
437
Sarah Sharp5308a912010-12-01 11:34:59 -0800438 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800439 bus_state->suspended_ports |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700440 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700441 case USB_PORT_FEAT_POWER:
442 /*
443 * Turn on ports, even if there isn't per-port switching.
444 * HC will report connect events even before this is set.
445 * However, khubd will ignore the roothub events until
446 * the roothub is registered.
447 */
Sarah Sharp5308a912010-12-01 11:34:59 -0800448 xhci_writel(xhci, temp | PORT_POWER,
449 port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700450
Sarah Sharp5308a912010-12-01 11:34:59 -0800451 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700452 xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
453 break;
454 case USB_PORT_FEAT_RESET:
455 temp = (temp | PORT_RESET);
Sarah Sharp5308a912010-12-01 11:34:59 -0800456 xhci_writel(xhci, temp, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700457
Sarah Sharp5308a912010-12-01 11:34:59 -0800458 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700459 xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
460 break;
461 default:
462 goto error;
463 }
Sarah Sharp5308a912010-12-01 11:34:59 -0800464 /* unblock any posted writes */
465 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700466 break;
467 case ClearPortFeature:
468 if (!wIndex || wIndex > ports)
469 goto error;
470 wIndex--;
Sarah Sharp5308a912010-12-01 11:34:59 -0800471 temp = xhci_readl(xhci, port_array[wIndex]);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700472 temp = xhci_port_state_to_neutral(temp);
473 switch (wValue) {
Andiry Xube88fe42010-10-14 07:22:57 -0700474 case USB_PORT_FEAT_SUSPEND:
Sarah Sharp5308a912010-12-01 11:34:59 -0800475 temp = xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700476 xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
477 xhci_dbg(xhci, "PORTSC %04x\n", temp);
478 if (temp & PORT_RESET)
479 goto error;
480 if (temp & XDEV_U3) {
481 if ((temp & PORT_PE) == 0)
482 goto error;
483 if (DEV_SUPERSPEED(temp)) {
484 temp = xhci_port_state_to_neutral(temp);
485 temp &= ~PORT_PLS_MASK;
486 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800487 xhci_writel(xhci, temp,
488 port_array[wIndex]);
489 xhci_readl(xhci, port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700490 } else {
491 temp = xhci_port_state_to_neutral(temp);
492 temp &= ~PORT_PLS_MASK;
493 temp |= PORT_LINK_STROBE | XDEV_RESUME;
Sarah Sharp5308a912010-12-01 11:34:59 -0800494 xhci_writel(xhci, temp,
495 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700496
497 spin_unlock_irqrestore(&xhci->lock,
498 flags);
499 msleep(20);
500 spin_lock_irqsave(&xhci->lock, flags);
501
Sarah Sharp5308a912010-12-01 11:34:59 -0800502 temp = xhci_readl(xhci,
503 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700504 temp = xhci_port_state_to_neutral(temp);
505 temp &= ~PORT_PLS_MASK;
506 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800507 xhci_writel(xhci, temp,
508 port_array[wIndex]);
Andiry Xube88fe42010-10-14 07:22:57 -0700509 }
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800510 bus_state->port_c_suspend |= 1 << wIndex;
Andiry Xube88fe42010-10-14 07:22:57 -0700511 }
512
Sarah Sharp52336302010-12-16 10:49:09 -0800513 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
514 wIndex + 1);
Andiry Xube88fe42010-10-14 07:22:57 -0700515 if (!slot_id) {
516 xhci_dbg(xhci, "slot_id is zero\n");
517 goto error;
518 }
519 xhci_ring_device(xhci, slot_id);
520 break;
521 case USB_PORT_FEAT_C_SUSPEND:
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800522 bus_state->port_c_suspend &= ~(1 << wIndex);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700523 case USB_PORT_FEAT_C_RESET:
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700524 case USB_PORT_FEAT_C_CONNECTION:
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700525 case USB_PORT_FEAT_C_OVER_CURRENT:
Sarah Sharp6219c0472009-12-09 15:59:11 -0800526 case USB_PORT_FEAT_C_ENABLE:
Sarah Sharp34fb5622009-12-09 15:59:08 -0800527 xhci_clear_port_change_bit(xhci, wValue, wIndex,
Sarah Sharp5308a912010-12-01 11:34:59 -0800528 port_array[wIndex], temp);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700529 break;
Sarah Sharp6219c0472009-12-09 15:59:11 -0800530 case USB_PORT_FEAT_ENABLE:
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800531 xhci_disable_port(hcd, xhci, wIndex,
Sarah Sharp5308a912010-12-01 11:34:59 -0800532 port_array[wIndex], temp);
Sarah Sharp6219c0472009-12-09 15:59:11 -0800533 break;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700534 default:
535 goto error;
536 }
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700537 break;
538 default:
539error:
540 /* "stall" on error */
541 retval = -EPIPE;
542 }
543 spin_unlock_irqrestore(&xhci->lock, flags);
544 return retval;
545}
546
547/*
548 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
549 * Ports are 0-indexed from the HCD point of view,
550 * and 1-indexed from the USB core pointer of view.
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700551 *
552 * Note that the status change bits will be cleared as soon as a port status
553 * change event is generated, so we use the saved status from that event.
554 */
555int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
556{
557 unsigned long flags;
558 u32 temp, status;
Andiry Xu56192532010-10-14 07:23:00 -0700559 u32 mask;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700560 int i, retval;
561 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
562 int ports;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800563 u32 __iomem **port_array;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800564 struct xhci_bus_state *bus_state;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700565
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800566 if (hcd->speed == HCD_USB3) {
567 ports = xhci->num_usb3_ports;
568 port_array = xhci->usb3_ports;
569 } else {
570 ports = xhci->num_usb2_ports;
571 port_array = xhci->usb2_ports;
Sarah Sharp5308a912010-12-01 11:34:59 -0800572 }
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800573 bus_state = &xhci->bus_state[hcd_index(hcd)];
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700574
575 /* Initial status is no changes */
William Gulland419a8e812010-05-12 10:20:34 -0700576 retval = (ports + 8) / 8;
577 memset(buf, 0, retval);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700578 status = 0;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700579
Andiry Xu56192532010-10-14 07:23:00 -0700580 mask = PORT_CSC | PORT_PEC | PORT_OCC;
581
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700582 spin_lock_irqsave(&xhci->lock, flags);
583 /* For each port, did anything change? If so, set that bit in buf. */
584 for (i = 0; i < ports; i++) {
Sarah Sharp5308a912010-12-01 11:34:59 -0800585 temp = xhci_readl(xhci, port_array[i]);
Andiry Xu56192532010-10-14 07:23:00 -0700586 if ((temp & mask) != 0 ||
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800587 (bus_state->port_c_suspend & 1 << i) ||
588 (bus_state->resume_done[i] && time_after_eq(
589 jiffies, bus_state->resume_done[i]))) {
William Gulland419a8e812010-05-12 10:20:34 -0700590 buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700591 status = 1;
592 }
593 }
594 spin_unlock_irqrestore(&xhci->lock, flags);
595 return status ? retval : 0;
596}
Andiry Xu9777e3c2010-10-14 07:23:03 -0700597
598#ifdef CONFIG_PM
599
600int xhci_bus_suspend(struct usb_hcd *hcd)
601{
602 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp518e8482010-12-15 11:56:29 -0800603 int max_ports, port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800604 u32 __iomem **port_array;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800605 struct xhci_bus_state *bus_state;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700606 unsigned long flags;
607
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800608 if (hcd->speed == HCD_USB3) {
609 max_ports = xhci->num_usb3_ports;
610 port_array = xhci->usb3_ports;
611 xhci_dbg(xhci, "suspend USB 3.0 root hub\n");
612 } else {
613 max_ports = xhci->num_usb2_ports;
614 port_array = xhci->usb2_ports;
615 xhci_dbg(xhci, "suspend USB 2.0 root hub\n");
Sarah Sharp5308a912010-12-01 11:34:59 -0800616 }
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800617 bus_state = &xhci->bus_state[hcd_index(hcd)];
Andiry Xu9777e3c2010-10-14 07:23:03 -0700618
619 spin_lock_irqsave(&xhci->lock, flags);
620
621 if (hcd->self.root_hub->do_remote_wakeup) {
Sarah Sharp518e8482010-12-15 11:56:29 -0800622 port_index = max_ports;
623 while (port_index--) {
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800624 if (bus_state->resume_done[port_index] != 0) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700625 spin_unlock_irqrestore(&xhci->lock, flags);
626 xhci_dbg(xhci, "suspend failed because "
627 "port %d is resuming\n",
Sarah Sharp518e8482010-12-15 11:56:29 -0800628 port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700629 return -EBUSY;
630 }
631 }
632 }
633
Sarah Sharp518e8482010-12-15 11:56:29 -0800634 port_index = max_ports;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800635 bus_state->bus_suspended = 0;
Sarah Sharp518e8482010-12-15 11:56:29 -0800636 while (port_index--) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700637 /* suspend the port if the port is not suspended */
Andiry Xu9777e3c2010-10-14 07:23:03 -0700638 u32 t1, t2;
639 int slot_id;
640
Sarah Sharp5308a912010-12-01 11:34:59 -0800641 t1 = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700642 t2 = xhci_port_state_to_neutral(t1);
643
644 if ((t1 & PORT_PE) && !(t1 & PORT_PLS_MASK)) {
Sarah Sharp518e8482010-12-15 11:56:29 -0800645 xhci_dbg(xhci, "port %d not suspended\n", port_index);
Sarah Sharp52336302010-12-16 10:49:09 -0800646 slot_id = xhci_find_slot_id_by_port(hcd, xhci,
Sarah Sharp518e8482010-12-15 11:56:29 -0800647 port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700648 if (slot_id) {
649 spin_unlock_irqrestore(&xhci->lock, flags);
650 xhci_stop_device(xhci, slot_id, 1);
651 spin_lock_irqsave(&xhci->lock, flags);
652 }
653 t2 &= ~PORT_PLS_MASK;
654 t2 |= PORT_LINK_STROBE | XDEV_U3;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800655 set_bit(port_index, &bus_state->bus_suspended);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700656 }
657 if (hcd->self.root_hub->do_remote_wakeup) {
658 if (t1 & PORT_CONNECT) {
659 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
660 t2 &= ~PORT_WKCONN_E;
661 } else {
662 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
663 t2 &= ~PORT_WKDISC_E;
664 }
665 } else
666 t2 &= ~PORT_WAKE_BITS;
667
668 t1 = xhci_port_state_to_neutral(t1);
669 if (t1 != t2)
Sarah Sharp5308a912010-12-01 11:34:59 -0800670 xhci_writel(xhci, t2, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700671
672 if (DEV_HIGHSPEED(t1)) {
673 /* enable remote wake up for USB 2.0 */
674 u32 __iomem *addr;
675 u32 tmp;
676
Sarah Sharp5308a912010-12-01 11:34:59 -0800677 /* Add one to the port status register address to get
678 * the port power control register address.
679 */
680 addr = port_array[port_index] + 1;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700681 tmp = xhci_readl(xhci, addr);
682 tmp |= PORT_RWE;
683 xhci_writel(xhci, tmp, addr);
684 }
685 }
686 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800687 bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700688 spin_unlock_irqrestore(&xhci->lock, flags);
689 return 0;
690}
691
692int xhci_bus_resume(struct usb_hcd *hcd)
693{
694 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp518e8482010-12-15 11:56:29 -0800695 int max_ports, port_index;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800696 u32 __iomem **port_array;
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800697 struct xhci_bus_state *bus_state;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700698 u32 temp;
699 unsigned long flags;
700
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800701 if (hcd->speed == HCD_USB3) {
702 max_ports = xhci->num_usb3_ports;
703 port_array = xhci->usb3_ports;
704 xhci_dbg(xhci, "resume USB 3.0 root hub\n");
705 } else {
706 max_ports = xhci->num_usb2_ports;
707 port_array = xhci->usb2_ports;
708 xhci_dbg(xhci, "resume USB 2.0 root hub\n");
Sarah Sharp5308a912010-12-01 11:34:59 -0800709 }
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800710 bus_state = &xhci->bus_state[hcd_index(hcd)];
Andiry Xu9777e3c2010-10-14 07:23:03 -0700711
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800712 if (time_before(jiffies, bus_state->next_statechange))
Andiry Xu9777e3c2010-10-14 07:23:03 -0700713 msleep(5);
714
715 spin_lock_irqsave(&xhci->lock, flags);
716 if (!HCD_HW_ACCESSIBLE(hcd)) {
717 spin_unlock_irqrestore(&xhci->lock, flags);
718 return -ESHUTDOWN;
719 }
720
721 /* delay the irqs */
722 temp = xhci_readl(xhci, &xhci->op_regs->command);
723 temp &= ~CMD_EIE;
724 xhci_writel(xhci, temp, &xhci->op_regs->command);
725
Sarah Sharp518e8482010-12-15 11:56:29 -0800726 port_index = max_ports;
727 while (port_index--) {
Andiry Xu9777e3c2010-10-14 07:23:03 -0700728 /* Check whether need resume ports. If needed
729 resume port and disable remote wakeup */
Andiry Xu9777e3c2010-10-14 07:23:03 -0700730 u32 temp;
731 int slot_id;
732
Sarah Sharp5308a912010-12-01 11:34:59 -0800733 temp = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700734 if (DEV_SUPERSPEED(temp))
735 temp &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
736 else
737 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800738 if (test_bit(port_index, &bus_state->bus_suspended) &&
Andiry Xu9777e3c2010-10-14 07:23:03 -0700739 (temp & PORT_PLS_MASK)) {
740 if (DEV_SUPERSPEED(temp)) {
741 temp = xhci_port_state_to_neutral(temp);
742 temp &= ~PORT_PLS_MASK;
743 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800744 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700745 } else {
746 temp = xhci_port_state_to_neutral(temp);
747 temp &= ~PORT_PLS_MASK;
748 temp |= PORT_LINK_STROBE | XDEV_RESUME;
Sarah Sharp5308a912010-12-01 11:34:59 -0800749 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700750
751 spin_unlock_irqrestore(&xhci->lock, flags);
752 msleep(20);
753 spin_lock_irqsave(&xhci->lock, flags);
754
Sarah Sharp5308a912010-12-01 11:34:59 -0800755 temp = xhci_readl(xhci, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700756 temp = xhci_port_state_to_neutral(temp);
757 temp &= ~PORT_PLS_MASK;
758 temp |= PORT_LINK_STROBE | XDEV_U0;
Sarah Sharp5308a912010-12-01 11:34:59 -0800759 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700760 }
Sarah Sharp52336302010-12-16 10:49:09 -0800761 slot_id = xhci_find_slot_id_by_port(hcd,
762 xhci, port_index + 1);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700763 if (slot_id)
764 xhci_ring_device(xhci, slot_id);
765 } else
Sarah Sharp5308a912010-12-01 11:34:59 -0800766 xhci_writel(xhci, temp, port_array[port_index]);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700767
768 if (DEV_HIGHSPEED(temp)) {
769 /* disable remote wake up for USB 2.0 */
770 u32 __iomem *addr;
771 u32 tmp;
772
Sarah Sharp5308a912010-12-01 11:34:59 -0800773 /* Add one to the port status register address to get
774 * the port power control register address.
775 */
776 addr = port_array[port_index] + 1;
Andiry Xu9777e3c2010-10-14 07:23:03 -0700777 tmp = xhci_readl(xhci, addr);
778 tmp &= ~PORT_RWE;
779 xhci_writel(xhci, tmp, addr);
780 }
781 }
782
783 (void) xhci_readl(xhci, &xhci->op_regs->command);
784
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800785 bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
Andiry Xu9777e3c2010-10-14 07:23:03 -0700786 /* re-enable irqs */
787 temp = xhci_readl(xhci, &xhci->op_regs->command);
788 temp |= CMD_EIE;
789 xhci_writel(xhci, temp, &xhci->op_regs->command);
790 temp = xhci_readl(xhci, &xhci->op_regs->command);
791
792 spin_unlock_irqrestore(&xhci->lock, flags);
793 return 0;
794}
795
Sarah Sharp436a3892010-10-15 14:59:15 -0700796#endif /* CONFIG_PM */