xref: /OK3568_Linux_fs/kernel/drivers/usb/host/xhci-hub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * xHCI host controller driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2008 Intel Corp.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Sarah Sharp
8*4882a593Smuzhiyun  * Some code borrowed from the Linux EHCI driver.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <asm/unaligned.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "xhci.h"
16*4882a593Smuzhiyun #include "xhci-trace.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define	PORT_WAKE_BITS	(PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
19*4882a593Smuzhiyun #define	PORT_RWC_BITS	(PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | \
20*4882a593Smuzhiyun 			 PORT_RC | PORT_PLC | PORT_PE)
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* USB 3 BOS descriptor and a capability descriptors, combined.
23*4882a593Smuzhiyun  * Fields will be adjusted and added later in xhci_create_usb3_bos_desc()
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun static u8 usb_bos_descriptor [] = {
26*4882a593Smuzhiyun 	USB_DT_BOS_SIZE,		/*  __u8 bLength, 5 bytes */
27*4882a593Smuzhiyun 	USB_DT_BOS,			/*  __u8 bDescriptorType */
28*4882a593Smuzhiyun 	0x0F, 0x00,			/*  __le16 wTotalLength, 15 bytes */
29*4882a593Smuzhiyun 	0x1,				/*  __u8 bNumDeviceCaps */
30*4882a593Smuzhiyun 	/* First device capability, SuperSpeed */
31*4882a593Smuzhiyun 	USB_DT_USB_SS_CAP_SIZE,		/*  __u8 bLength, 10 bytes */
32*4882a593Smuzhiyun 	USB_DT_DEVICE_CAPABILITY,	/* Device Capability */
33*4882a593Smuzhiyun 	USB_SS_CAP_TYPE,		/* bDevCapabilityType, SUPERSPEED_USB */
34*4882a593Smuzhiyun 	0x00,				/* bmAttributes, LTM off by default */
35*4882a593Smuzhiyun 	USB_5GBPS_OPERATION, 0x00,	/* wSpeedsSupported, 5Gbps only */
36*4882a593Smuzhiyun 	0x03,				/* bFunctionalitySupport,
37*4882a593Smuzhiyun 					   USB 3.0 speed only */
38*4882a593Smuzhiyun 	0x00,				/* bU1DevExitLat, set later. */
39*4882a593Smuzhiyun 	0x00, 0x00,			/* __le16 bU2DevExitLat, set later. */
40*4882a593Smuzhiyun 	/* Second device capability, SuperSpeedPlus */
41*4882a593Smuzhiyun 	0x1c,				/* bLength 28, will be adjusted later */
42*4882a593Smuzhiyun 	USB_DT_DEVICE_CAPABILITY,	/* Device Capability */
43*4882a593Smuzhiyun 	USB_SSP_CAP_TYPE,		/* bDevCapabilityType SUPERSPEED_PLUS */
44*4882a593Smuzhiyun 	0x00,				/* bReserved 0 */
45*4882a593Smuzhiyun 	0x23, 0x00, 0x00, 0x00,		/* bmAttributes, SSAC=3 SSIC=1 */
46*4882a593Smuzhiyun 	0x01, 0x00,			/* wFunctionalitySupport */
47*4882a593Smuzhiyun 	0x00, 0x00,			/* wReserved 0 */
48*4882a593Smuzhiyun 	/* Default Sublink Speed Attributes, overwrite if custom PSI exists */
49*4882a593Smuzhiyun 	0x34, 0x00, 0x05, 0x00,		/* 5Gbps, symmetric, rx, ID = 4 */
50*4882a593Smuzhiyun 	0xb4, 0x00, 0x05, 0x00,		/* 5Gbps, symmetric, tx, ID = 4 */
51*4882a593Smuzhiyun 	0x35, 0x40, 0x0a, 0x00,		/* 10Gbps, SSP, symmetric, rx, ID = 5 */
52*4882a593Smuzhiyun 	0xb5, 0x40, 0x0a, 0x00,		/* 10Gbps, SSP, symmetric, tx, ID = 5 */
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
xhci_create_usb3_bos_desc(struct xhci_hcd * xhci,char * buf,u16 wLength)55*4882a593Smuzhiyun static int xhci_create_usb3_bos_desc(struct xhci_hcd *xhci, char *buf,
56*4882a593Smuzhiyun 				     u16 wLength)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct xhci_port_cap *port_cap = NULL;
59*4882a593Smuzhiyun 	int i, ssa_count;
60*4882a593Smuzhiyun 	u32 temp;
61*4882a593Smuzhiyun 	u16 desc_size, ssp_cap_size, ssa_size = 0;
62*4882a593Smuzhiyun 	bool usb3_1 = false;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	desc_size = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
65*4882a593Smuzhiyun 	ssp_cap_size = sizeof(usb_bos_descriptor) - desc_size;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	/* does xhci support USB 3.1 Enhanced SuperSpeed */
68*4882a593Smuzhiyun 	for (i = 0; i < xhci->num_port_caps; i++) {
69*4882a593Smuzhiyun 		if (xhci->port_caps[i].maj_rev == 0x03 &&
70*4882a593Smuzhiyun 		    xhci->port_caps[i].min_rev >= 0x01) {
71*4882a593Smuzhiyun 			usb3_1 = true;
72*4882a593Smuzhiyun 			port_cap = &xhci->port_caps[i];
73*4882a593Smuzhiyun 			break;
74*4882a593Smuzhiyun 		}
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (usb3_1) {
78*4882a593Smuzhiyun 		/* does xhci provide a PSI table for SSA speed attributes? */
79*4882a593Smuzhiyun 		if (port_cap->psi_count) {
80*4882a593Smuzhiyun 			/* two SSA entries for each unique PSI ID, RX and TX */
81*4882a593Smuzhiyun 			ssa_count = port_cap->psi_uid_count * 2;
82*4882a593Smuzhiyun 			ssa_size = ssa_count * sizeof(u32);
83*4882a593Smuzhiyun 			ssp_cap_size -= 16; /* skip copying the default SSA */
84*4882a593Smuzhiyun 		}
85*4882a593Smuzhiyun 		desc_size += ssp_cap_size;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 	memcpy(buf, &usb_bos_descriptor, min(desc_size, wLength));
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (usb3_1) {
90*4882a593Smuzhiyun 		/* modify bos descriptor bNumDeviceCaps and wTotalLength */
91*4882a593Smuzhiyun 		buf[4] += 1;
92*4882a593Smuzhiyun 		put_unaligned_le16(desc_size + ssa_size, &buf[2]);
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (wLength < USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE)
96*4882a593Smuzhiyun 		return wLength;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* Indicate whether the host has LTM support. */
99*4882a593Smuzhiyun 	temp = readl(&xhci->cap_regs->hcc_params);
100*4882a593Smuzhiyun 	if (HCC_LTC(temp))
101*4882a593Smuzhiyun 		buf[8] |= USB_LTM_SUPPORT;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/* Set the U1 and U2 exit latencies. */
104*4882a593Smuzhiyun 	if ((xhci->quirks & XHCI_LPM_SUPPORT)) {
105*4882a593Smuzhiyun 		temp = readl(&xhci->cap_regs->hcs_params3);
106*4882a593Smuzhiyun 		buf[12] = HCS_U1_LATENCY(temp);
107*4882a593Smuzhiyun 		put_unaligned_le16(HCS_U2_LATENCY(temp), &buf[13]);
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* If PSI table exists, add the custom speed attributes from it */
111*4882a593Smuzhiyun 	if (usb3_1 && port_cap->psi_count) {
112*4882a593Smuzhiyun 		u32 ssp_cap_base, bm_attrib, psi, psi_mant, psi_exp;
113*4882a593Smuzhiyun 		int offset;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		ssp_cap_base = USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 		if (wLength < desc_size)
118*4882a593Smuzhiyun 			return wLength;
119*4882a593Smuzhiyun 		buf[ssp_cap_base] = ssp_cap_size + ssa_size;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 		/* attribute count SSAC bits 4:0 and ID count SSIC bits 8:5 */
122*4882a593Smuzhiyun 		bm_attrib = (ssa_count - 1) & 0x1f;
123*4882a593Smuzhiyun 		bm_attrib |= (port_cap->psi_uid_count - 1) << 5;
124*4882a593Smuzhiyun 		put_unaligned_le32(bm_attrib, &buf[ssp_cap_base + 4]);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 		if (wLength < desc_size + ssa_size)
127*4882a593Smuzhiyun 			return wLength;
128*4882a593Smuzhiyun 		/*
129*4882a593Smuzhiyun 		 * Create the Sublink Speed Attributes (SSA) array.
130*4882a593Smuzhiyun 		 * The xhci PSI field and USB 3.1 SSA fields are very similar,
131*4882a593Smuzhiyun 		 * but link type bits 7:6 differ for values 01b and 10b.
132*4882a593Smuzhiyun 		 * xhci has also only one PSI entry for a symmetric link when
133*4882a593Smuzhiyun 		 * USB 3.1 requires two SSA entries (RX and TX) for every link
134*4882a593Smuzhiyun 		 */
135*4882a593Smuzhiyun 		offset = desc_size;
136*4882a593Smuzhiyun 		for (i = 0; i < port_cap->psi_count; i++) {
137*4882a593Smuzhiyun 			psi = port_cap->psi[i];
138*4882a593Smuzhiyun 			psi &= ~USB_SSP_SUBLINK_SPEED_RSVD;
139*4882a593Smuzhiyun 			psi_exp = XHCI_EXT_PORT_PSIE(psi);
140*4882a593Smuzhiyun 			psi_mant = XHCI_EXT_PORT_PSIM(psi);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 			/* Shift to Gbps and set SSP Link BIT(14) if 10Gpbs */
143*4882a593Smuzhiyun 			for (; psi_exp < 3; psi_exp++)
144*4882a593Smuzhiyun 				psi_mant /= 1000;
145*4882a593Smuzhiyun 			if (psi_mant >= 10)
146*4882a593Smuzhiyun 				psi |= BIT(14);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 			if ((psi & PLT_MASK) == PLT_SYM) {
149*4882a593Smuzhiyun 			/* Symmetric, create SSA RX and TX from one PSI entry */
150*4882a593Smuzhiyun 				put_unaligned_le32(psi, &buf[offset]);
151*4882a593Smuzhiyun 				psi |= 1 << 7;  /* turn entry to TX */
152*4882a593Smuzhiyun 				offset += 4;
153*4882a593Smuzhiyun 				if (offset >= desc_size + ssa_size)
154*4882a593Smuzhiyun 					return desc_size + ssa_size;
155*4882a593Smuzhiyun 			} else if ((psi & PLT_MASK) == PLT_ASYM_RX) {
156*4882a593Smuzhiyun 				/* Asymetric RX, flip bits 7:6 for SSA */
157*4882a593Smuzhiyun 				psi ^= PLT_MASK;
158*4882a593Smuzhiyun 			}
159*4882a593Smuzhiyun 			put_unaligned_le32(psi, &buf[offset]);
160*4882a593Smuzhiyun 			offset += 4;
161*4882a593Smuzhiyun 			if (offset >= desc_size + ssa_size)
162*4882a593Smuzhiyun 				return desc_size + ssa_size;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 	/* ssa_size is 0 for other than usb 3.1 hosts */
166*4882a593Smuzhiyun 	return desc_size + ssa_size;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
xhci_common_hub_descriptor(struct xhci_hcd * xhci,struct usb_hub_descriptor * desc,int ports)169*4882a593Smuzhiyun static void xhci_common_hub_descriptor(struct xhci_hcd *xhci,
170*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc, int ports)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	u16 temp;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	desc->bHubContrCurrent = 0;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	desc->bNbrPorts = ports;
177*4882a593Smuzhiyun 	temp = 0;
178*4882a593Smuzhiyun 	/* Bits 1:0 - support per-port power switching, or power always on */
179*4882a593Smuzhiyun 	if (HCC_PPC(xhci->hcc_params))
180*4882a593Smuzhiyun 		temp |= HUB_CHAR_INDV_PORT_LPSM;
181*4882a593Smuzhiyun 	else
182*4882a593Smuzhiyun 		temp |= HUB_CHAR_NO_LPSM;
183*4882a593Smuzhiyun 	/* Bit  2 - root hubs are not part of a compound device */
184*4882a593Smuzhiyun 	/* Bits 4:3 - individual port over current protection */
185*4882a593Smuzhiyun 	temp |= HUB_CHAR_INDV_PORT_OCPM;
186*4882a593Smuzhiyun 	/* Bits 6:5 - no TTs in root ports */
187*4882a593Smuzhiyun 	/* Bit  7 - no port indicators */
188*4882a593Smuzhiyun 	desc->wHubCharacteristics = cpu_to_le16(temp);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /* Fill in the USB 2.0 roothub descriptor */
xhci_usb2_hub_descriptor(struct usb_hcd * hcd,struct xhci_hcd * xhci,struct usb_hub_descriptor * desc)192*4882a593Smuzhiyun static void xhci_usb2_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
193*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	int ports;
196*4882a593Smuzhiyun 	u16 temp;
197*4882a593Smuzhiyun 	__u8 port_removable[(USB_MAXCHILDREN + 1 + 7) / 8];
198*4882a593Smuzhiyun 	u32 portsc;
199*4882a593Smuzhiyun 	unsigned int i;
200*4882a593Smuzhiyun 	struct xhci_hub *rhub;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	rhub = &xhci->usb2_rhub;
203*4882a593Smuzhiyun 	ports = rhub->num_ports;
204*4882a593Smuzhiyun 	xhci_common_hub_descriptor(xhci, desc, ports);
205*4882a593Smuzhiyun 	desc->bDescriptorType = USB_DT_HUB;
206*4882a593Smuzhiyun 	temp = 1 + (ports / 8);
207*4882a593Smuzhiyun 	desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
208*4882a593Smuzhiyun 	desc->bPwrOn2PwrGood = 10;	/* xhci section 5.4.8 says 20ms */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* The Device Removable bits are reported on a byte granularity.
211*4882a593Smuzhiyun 	 * If the port doesn't exist within that byte, the bit is set to 0.
212*4882a593Smuzhiyun 	 */
213*4882a593Smuzhiyun 	memset(port_removable, 0, sizeof(port_removable));
214*4882a593Smuzhiyun 	for (i = 0; i < ports; i++) {
215*4882a593Smuzhiyun 		portsc = readl(rhub->ports[i]->addr);
216*4882a593Smuzhiyun 		/* If a device is removable, PORTSC reports a 0, same as in the
217*4882a593Smuzhiyun 		 * hub descriptor DeviceRemovable bits.
218*4882a593Smuzhiyun 		 */
219*4882a593Smuzhiyun 		if (portsc & PORT_DEV_REMOVE)
220*4882a593Smuzhiyun 			/* This math is hairy because bit 0 of DeviceRemovable
221*4882a593Smuzhiyun 			 * is reserved, and bit 1 is for port 1, etc.
222*4882a593Smuzhiyun 			 */
223*4882a593Smuzhiyun 			port_removable[(i + 1) / 8] |= 1 << ((i + 1) % 8);
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* ch11.h defines a hub descriptor that has room for USB_MAXCHILDREN
227*4882a593Smuzhiyun 	 * ports on it.  The USB 2.0 specification says that there are two
228*4882a593Smuzhiyun 	 * variable length fields at the end of the hub descriptor:
229*4882a593Smuzhiyun 	 * DeviceRemovable and PortPwrCtrlMask.  But since we can have less than
230*4882a593Smuzhiyun 	 * USB_MAXCHILDREN ports, we may need to use the DeviceRemovable array
231*4882a593Smuzhiyun 	 * to set PortPwrCtrlMask bits.  PortPwrCtrlMask must always be set to
232*4882a593Smuzhiyun 	 * 0xFF, so we initialize the both arrays (DeviceRemovable and
233*4882a593Smuzhiyun 	 * PortPwrCtrlMask) to 0xFF.  Then we set the DeviceRemovable for each
234*4882a593Smuzhiyun 	 * set of ports that actually exist.
235*4882a593Smuzhiyun 	 */
236*4882a593Smuzhiyun 	memset(desc->u.hs.DeviceRemovable, 0xff,
237*4882a593Smuzhiyun 			sizeof(desc->u.hs.DeviceRemovable));
238*4882a593Smuzhiyun 	memset(desc->u.hs.PortPwrCtrlMask, 0xff,
239*4882a593Smuzhiyun 			sizeof(desc->u.hs.PortPwrCtrlMask));
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	for (i = 0; i < (ports + 1 + 7) / 8; i++)
242*4882a593Smuzhiyun 		memset(&desc->u.hs.DeviceRemovable[i], port_removable[i],
243*4882a593Smuzhiyun 				sizeof(__u8));
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /* Fill in the USB 3.0 roothub descriptor */
xhci_usb3_hub_descriptor(struct usb_hcd * hcd,struct xhci_hcd * xhci,struct usb_hub_descriptor * desc)247*4882a593Smuzhiyun static void xhci_usb3_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
248*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	int ports;
251*4882a593Smuzhiyun 	u16 port_removable;
252*4882a593Smuzhiyun 	u32 portsc;
253*4882a593Smuzhiyun 	unsigned int i;
254*4882a593Smuzhiyun 	struct xhci_hub *rhub;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	rhub = &xhci->usb3_rhub;
257*4882a593Smuzhiyun 	ports = rhub->num_ports;
258*4882a593Smuzhiyun 	xhci_common_hub_descriptor(xhci, desc, ports);
259*4882a593Smuzhiyun 	desc->bDescriptorType = USB_DT_SS_HUB;
260*4882a593Smuzhiyun 	desc->bDescLength = USB_DT_SS_HUB_SIZE;
261*4882a593Smuzhiyun 	desc->bPwrOn2PwrGood = 50;	/* usb 3.1 may fail if less than 100ms */
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	/* header decode latency should be zero for roothubs,
264*4882a593Smuzhiyun 	 * see section 4.23.5.2.
265*4882a593Smuzhiyun 	 */
266*4882a593Smuzhiyun 	desc->u.ss.bHubHdrDecLat = 0;
267*4882a593Smuzhiyun 	desc->u.ss.wHubDelay = 0;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	port_removable = 0;
270*4882a593Smuzhiyun 	/* bit 0 is reserved, bit 1 is for port 1, etc. */
271*4882a593Smuzhiyun 	for (i = 0; i < ports; i++) {
272*4882a593Smuzhiyun 		portsc = readl(rhub->ports[i]->addr);
273*4882a593Smuzhiyun 		if (portsc & PORT_DEV_REMOVE)
274*4882a593Smuzhiyun 			port_removable |= 1 << (i + 1);
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
xhci_hub_descriptor(struct usb_hcd * hcd,struct xhci_hcd * xhci,struct usb_hub_descriptor * desc)280*4882a593Smuzhiyun static void xhci_hub_descriptor(struct usb_hcd *hcd, struct xhci_hcd *xhci,
281*4882a593Smuzhiyun 		struct usb_hub_descriptor *desc)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (hcd->speed >= HCD_USB3)
285*4882a593Smuzhiyun 		xhci_usb3_hub_descriptor(hcd, xhci, desc);
286*4882a593Smuzhiyun 	else
287*4882a593Smuzhiyun 		xhci_usb2_hub_descriptor(hcd, xhci, desc);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
xhci_port_speed(unsigned int port_status)291*4882a593Smuzhiyun static unsigned int xhci_port_speed(unsigned int port_status)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	if (DEV_LOWSPEED(port_status))
294*4882a593Smuzhiyun 		return USB_PORT_STAT_LOW_SPEED;
295*4882a593Smuzhiyun 	if (DEV_HIGHSPEED(port_status))
296*4882a593Smuzhiyun 		return USB_PORT_STAT_HIGH_SPEED;
297*4882a593Smuzhiyun 	/*
298*4882a593Smuzhiyun 	 * FIXME: Yes, we should check for full speed, but the core uses that as
299*4882a593Smuzhiyun 	 * a default in portspeed() in usb/core/hub.c (which is the only place
300*4882a593Smuzhiyun 	 * USB_PORT_STAT_*_SPEED is used).
301*4882a593Smuzhiyun 	 */
302*4882a593Smuzhiyun 	return 0;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * These bits are Read Only (RO) and should be saved and written to the
307*4882a593Smuzhiyun  * registers: 0, 3, 10:13, 30
308*4882a593Smuzhiyun  * connect status, over-current status, port speed, and device removable.
309*4882a593Smuzhiyun  * connect status and port speed are also sticky - meaning they're in
310*4882a593Smuzhiyun  * the AUX well and they aren't changed by a hot, warm, or cold reset.
311*4882a593Smuzhiyun  */
312*4882a593Smuzhiyun #define	XHCI_PORT_RO	((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
313*4882a593Smuzhiyun /*
314*4882a593Smuzhiyun  * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
315*4882a593Smuzhiyun  * bits 5:8, 9, 14:15, 25:27
316*4882a593Smuzhiyun  * link state, port power, port indicator state, "wake on" enable state
317*4882a593Smuzhiyun  */
318*4882a593Smuzhiyun #define XHCI_PORT_RWS	((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
319*4882a593Smuzhiyun /*
320*4882a593Smuzhiyun  * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
321*4882a593Smuzhiyun  * bit 4 (port reset)
322*4882a593Smuzhiyun  */
323*4882a593Smuzhiyun #define	XHCI_PORT_RW1S	((1<<4))
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun  * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
326*4882a593Smuzhiyun  * bits 1, 17, 18, 19, 20, 21, 22, 23
327*4882a593Smuzhiyun  * port enable/disable, and
328*4882a593Smuzhiyun  * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
329*4882a593Smuzhiyun  * over-current, reset, link state, and L1 change
330*4882a593Smuzhiyun  */
331*4882a593Smuzhiyun #define XHCI_PORT_RW1CS	((1<<1) | (0x7f<<17))
332*4882a593Smuzhiyun /*
333*4882a593Smuzhiyun  * Bit 16 is RW, and writing a '1' to it causes the link state control to be
334*4882a593Smuzhiyun  * latched in
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun #define	XHCI_PORT_RW	((1<<16))
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun  * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
339*4882a593Smuzhiyun  * bits 2, 24, 28:31
340*4882a593Smuzhiyun  */
341*4882a593Smuzhiyun #define	XHCI_PORT_RZ	((1<<2) | (1<<24) | (0xf<<28))
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun  * Given a port state, this function returns a value that would result in the
345*4882a593Smuzhiyun  * port being in the same state, if the value was written to the port status
346*4882a593Smuzhiyun  * control register.
347*4882a593Smuzhiyun  * Save Read Only (RO) bits and save read/write bits where
348*4882a593Smuzhiyun  * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
349*4882a593Smuzhiyun  * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
350*4882a593Smuzhiyun  */
xhci_port_state_to_neutral(u32 state)351*4882a593Smuzhiyun u32 xhci_port_state_to_neutral(u32 state)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	/* Save read-only status and port state */
354*4882a593Smuzhiyun 	return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun  * find slot id based on port number.
359*4882a593Smuzhiyun  * @port: The one-based port number from one of the two split roothubs.
360*4882a593Smuzhiyun  */
xhci_find_slot_id_by_port(struct usb_hcd * hcd,struct xhci_hcd * xhci,u16 port)361*4882a593Smuzhiyun int xhci_find_slot_id_by_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
362*4882a593Smuzhiyun 		u16 port)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	int slot_id;
365*4882a593Smuzhiyun 	int i;
366*4882a593Smuzhiyun 	enum usb_device_speed speed;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	slot_id = 0;
369*4882a593Smuzhiyun 	for (i = 0; i < MAX_HC_SLOTS; i++) {
370*4882a593Smuzhiyun 		if (!xhci->devs[i] || !xhci->devs[i]->udev)
371*4882a593Smuzhiyun 			continue;
372*4882a593Smuzhiyun 		speed = xhci->devs[i]->udev->speed;
373*4882a593Smuzhiyun 		if (((speed >= USB_SPEED_SUPER) == (hcd->speed >= HCD_USB3))
374*4882a593Smuzhiyun 				&& xhci->devs[i]->fake_port == port) {
375*4882a593Smuzhiyun 			slot_id = i;
376*4882a593Smuzhiyun 			break;
377*4882a593Smuzhiyun 		}
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	return slot_id;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /*
384*4882a593Smuzhiyun  * Stop device
385*4882a593Smuzhiyun  * It issues stop endpoint command for EP 0 to 30. And wait the last command
386*4882a593Smuzhiyun  * to complete.
387*4882a593Smuzhiyun  * suspend will set to 1, if suspend bit need to set in command.
388*4882a593Smuzhiyun  */
xhci_stop_device(struct xhci_hcd * xhci,int slot_id,int suspend)389*4882a593Smuzhiyun static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	struct xhci_virt_device *virt_dev;
392*4882a593Smuzhiyun 	struct xhci_command *cmd;
393*4882a593Smuzhiyun 	unsigned long flags;
394*4882a593Smuzhiyun 	int ret;
395*4882a593Smuzhiyun 	int i;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	ret = 0;
398*4882a593Smuzhiyun 	virt_dev = xhci->devs[slot_id];
399*4882a593Smuzhiyun 	if (!virt_dev)
400*4882a593Smuzhiyun 		return -ENODEV;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	trace_xhci_stop_device(virt_dev);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
405*4882a593Smuzhiyun 	if (!cmd)
406*4882a593Smuzhiyun 		return -ENOMEM;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, flags);
409*4882a593Smuzhiyun 	for (i = LAST_EP_INDEX; i > 0; i--) {
410*4882a593Smuzhiyun 		if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
411*4882a593Smuzhiyun 			struct xhci_ep_ctx *ep_ctx;
412*4882a593Smuzhiyun 			struct xhci_command *command;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 			ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, i);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 			/* Check ep is running, required by AMD SNPS 3.1 xHC */
417*4882a593Smuzhiyun 			if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_RUNNING)
418*4882a593Smuzhiyun 				continue;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 			command = xhci_alloc_command(xhci, false, GFP_NOWAIT);
421*4882a593Smuzhiyun 			if (!command) {
422*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
423*4882a593Smuzhiyun 				ret = -ENOMEM;
424*4882a593Smuzhiyun 				goto cmd_cleanup;
425*4882a593Smuzhiyun 			}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 			ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
428*4882a593Smuzhiyun 						       i, suspend);
429*4882a593Smuzhiyun 			if (ret) {
430*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
431*4882a593Smuzhiyun 				xhci_free_command(xhci, command);
432*4882a593Smuzhiyun 				goto cmd_cleanup;
433*4882a593Smuzhiyun 			}
434*4882a593Smuzhiyun 		}
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 	ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
437*4882a593Smuzhiyun 	if (ret) {
438*4882a593Smuzhiyun 		spin_unlock_irqrestore(&xhci->lock, flags);
439*4882a593Smuzhiyun 		goto cmd_cleanup;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	xhci_ring_cmd_db(xhci);
443*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, flags);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	/* Wait for last stop endpoint command to finish */
446*4882a593Smuzhiyun 	wait_for_completion(cmd->completion);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	if (cmd->status == COMP_COMMAND_ABORTED ||
449*4882a593Smuzhiyun 	    cmd->status == COMP_COMMAND_RING_STOPPED) {
450*4882a593Smuzhiyun 		xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
451*4882a593Smuzhiyun 		ret = -ETIME;
452*4882a593Smuzhiyun 		goto cmd_cleanup;
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
456*4882a593Smuzhiyun 	if (ret)
457*4882a593Smuzhiyun 		xhci_warn(xhci, "Sync device context failed, ret=%d\n", ret);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun cmd_cleanup:
460*4882a593Smuzhiyun 	xhci_free_command(xhci, cmd);
461*4882a593Smuzhiyun 	return ret;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun  * Ring device, it rings the all doorbells unconditionally.
466*4882a593Smuzhiyun  */
xhci_ring_device(struct xhci_hcd * xhci,int slot_id)467*4882a593Smuzhiyun void xhci_ring_device(struct xhci_hcd *xhci, int slot_id)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	int i, s;
470*4882a593Smuzhiyun 	struct xhci_virt_ep *ep;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	for (i = 0; i < LAST_EP_INDEX + 1; i++) {
473*4882a593Smuzhiyun 		ep = &xhci->devs[slot_id]->eps[i];
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		if (ep->ep_state & EP_HAS_STREAMS) {
476*4882a593Smuzhiyun 			for (s = 1; s < ep->stream_info->num_streams; s++)
477*4882a593Smuzhiyun 				xhci_ring_ep_doorbell(xhci, slot_id, i, s);
478*4882a593Smuzhiyun 		} else if (ep->ring && ep->ring->dequeue) {
479*4882a593Smuzhiyun 			xhci_ring_ep_doorbell(xhci, slot_id, i, 0);
480*4882a593Smuzhiyun 		}
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	return;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun 
xhci_disable_port(struct usb_hcd * hcd,struct xhci_hcd * xhci,u16 wIndex,__le32 __iomem * addr,u32 port_status)486*4882a593Smuzhiyun static void xhci_disable_port(struct usb_hcd *hcd, struct xhci_hcd *xhci,
487*4882a593Smuzhiyun 		u16 wIndex, __le32 __iomem *addr, u32 port_status)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	/* Don't allow the USB core to disable SuperSpeed ports. */
490*4882a593Smuzhiyun 	if (hcd->speed >= HCD_USB3) {
491*4882a593Smuzhiyun 		xhci_dbg(xhci, "Ignoring request to disable "
492*4882a593Smuzhiyun 				"SuperSpeed port.\n");
493*4882a593Smuzhiyun 		return;
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	if (xhci->quirks & XHCI_BROKEN_PORT_PED) {
497*4882a593Smuzhiyun 		xhci_dbg(xhci,
498*4882a593Smuzhiyun 			 "Broken Port Enabled/Disabled, ignoring port disable request.\n");
499*4882a593Smuzhiyun 		return;
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	/* Write 1 to disable the port */
503*4882a593Smuzhiyun 	writel(port_status | PORT_PE, addr);
504*4882a593Smuzhiyun 	port_status = readl(addr);
505*4882a593Smuzhiyun 	xhci_dbg(xhci, "disable port %d-%d, portsc: 0x%x\n",
506*4882a593Smuzhiyun 		 hcd->self.busnum, wIndex + 1, port_status);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
xhci_clear_port_change_bit(struct xhci_hcd * xhci,u16 wValue,u16 wIndex,__le32 __iomem * addr,u32 port_status)509*4882a593Smuzhiyun static void xhci_clear_port_change_bit(struct xhci_hcd *xhci, u16 wValue,
510*4882a593Smuzhiyun 		u16 wIndex, __le32 __iomem *addr, u32 port_status)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	char *port_change_bit;
513*4882a593Smuzhiyun 	u32 status;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	switch (wValue) {
516*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_RESET:
517*4882a593Smuzhiyun 		status = PORT_RC;
518*4882a593Smuzhiyun 		port_change_bit = "reset";
519*4882a593Smuzhiyun 		break;
520*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_BH_PORT_RESET:
521*4882a593Smuzhiyun 		status = PORT_WRC;
522*4882a593Smuzhiyun 		port_change_bit = "warm(BH) reset";
523*4882a593Smuzhiyun 		break;
524*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_CONNECTION:
525*4882a593Smuzhiyun 		status = PORT_CSC;
526*4882a593Smuzhiyun 		port_change_bit = "connect";
527*4882a593Smuzhiyun 		break;
528*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_OVER_CURRENT:
529*4882a593Smuzhiyun 		status = PORT_OCC;
530*4882a593Smuzhiyun 		port_change_bit = "over-current";
531*4882a593Smuzhiyun 		break;
532*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_ENABLE:
533*4882a593Smuzhiyun 		status = PORT_PEC;
534*4882a593Smuzhiyun 		port_change_bit = "enable/disable";
535*4882a593Smuzhiyun 		break;
536*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_SUSPEND:
537*4882a593Smuzhiyun 		status = PORT_PLC;
538*4882a593Smuzhiyun 		port_change_bit = "suspend/resume";
539*4882a593Smuzhiyun 		break;
540*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_PORT_LINK_STATE:
541*4882a593Smuzhiyun 		status = PORT_PLC;
542*4882a593Smuzhiyun 		port_change_bit = "link state";
543*4882a593Smuzhiyun 		break;
544*4882a593Smuzhiyun 	case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
545*4882a593Smuzhiyun 		status = PORT_CEC;
546*4882a593Smuzhiyun 		port_change_bit = "config error";
547*4882a593Smuzhiyun 		break;
548*4882a593Smuzhiyun 	default:
549*4882a593Smuzhiyun 		/* Should never happen */
550*4882a593Smuzhiyun 		return;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 	/* Change bits are all write 1 to clear */
553*4882a593Smuzhiyun 	writel(port_status | status, addr);
554*4882a593Smuzhiyun 	port_status = readl(addr);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	xhci_dbg(xhci, "clear port%d %s change, portsc: 0x%x\n",
557*4882a593Smuzhiyun 		 wIndex + 1, port_change_bit, port_status);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
xhci_get_rhub(struct usb_hcd * hcd)560*4882a593Smuzhiyun struct xhci_hub *xhci_get_rhub(struct usb_hcd *hcd)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	if (hcd->speed >= HCD_USB3)
565*4882a593Smuzhiyun 		return &xhci->usb3_rhub;
566*4882a593Smuzhiyun 	return &xhci->usb2_rhub;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun /*
570*4882a593Smuzhiyun  * xhci_set_port_power() must be called with xhci->lock held.
571*4882a593Smuzhiyun  * It will release and re-aquire the lock while calling ACPI
572*4882a593Smuzhiyun  * method.
573*4882a593Smuzhiyun  */
xhci_set_port_power(struct xhci_hcd * xhci,struct usb_hcd * hcd,u16 index,bool on,unsigned long * flags)574*4882a593Smuzhiyun static void xhci_set_port_power(struct xhci_hcd *xhci, struct usb_hcd *hcd,
575*4882a593Smuzhiyun 				u16 index, bool on, unsigned long *flags)
576*4882a593Smuzhiyun 	__must_hold(&xhci->lock)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	struct xhci_hub *rhub;
579*4882a593Smuzhiyun 	struct xhci_port *port;
580*4882a593Smuzhiyun 	u32 temp;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
583*4882a593Smuzhiyun 	port = rhub->ports[index];
584*4882a593Smuzhiyun 	temp = readl(port->addr);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	xhci_dbg(xhci, "set port power %d-%d %s, portsc: 0x%x\n",
587*4882a593Smuzhiyun 		 hcd->self.busnum, index + 1, on ? "ON" : "OFF", temp);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	temp = xhci_port_state_to_neutral(temp);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	if (on) {
592*4882a593Smuzhiyun 		/* Power on */
593*4882a593Smuzhiyun 		writel(temp | PORT_POWER, port->addr);
594*4882a593Smuzhiyun 		readl(port->addr);
595*4882a593Smuzhiyun 	} else {
596*4882a593Smuzhiyun 		/* Power off */
597*4882a593Smuzhiyun 		writel(temp & ~PORT_POWER, port->addr);
598*4882a593Smuzhiyun 	}
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, *flags);
601*4882a593Smuzhiyun 	temp = usb_acpi_power_manageable(hcd->self.root_hub,
602*4882a593Smuzhiyun 					index);
603*4882a593Smuzhiyun 	if (temp)
604*4882a593Smuzhiyun 		usb_acpi_set_power_state(hcd->self.root_hub,
605*4882a593Smuzhiyun 			index, on);
606*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, *flags);
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
xhci_port_set_test_mode(struct xhci_hcd * xhci,u16 test_mode,u16 wIndex)609*4882a593Smuzhiyun static void xhci_port_set_test_mode(struct xhci_hcd *xhci,
610*4882a593Smuzhiyun 	u16 test_mode, u16 wIndex)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	u32 temp;
613*4882a593Smuzhiyun 	struct xhci_port *port;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	/* xhci only supports test mode for usb2 ports */
616*4882a593Smuzhiyun 	port = xhci->usb2_rhub.ports[wIndex];
617*4882a593Smuzhiyun 	temp = readl(port->addr + PORTPMSC);
618*4882a593Smuzhiyun 	temp |= test_mode << PORT_TEST_MODE_SHIFT;
619*4882a593Smuzhiyun 	writel(temp, port->addr + PORTPMSC);
620*4882a593Smuzhiyun 	xhci->test_mode = test_mode;
621*4882a593Smuzhiyun 	if (test_mode == USB_TEST_FORCE_ENABLE)
622*4882a593Smuzhiyun 		xhci_start(xhci);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
xhci_enter_test_mode(struct xhci_hcd * xhci,u16 test_mode,u16 wIndex,unsigned long * flags)625*4882a593Smuzhiyun static int xhci_enter_test_mode(struct xhci_hcd *xhci,
626*4882a593Smuzhiyun 				u16 test_mode, u16 wIndex, unsigned long *flags)
627*4882a593Smuzhiyun 	__must_hold(&xhci->lock)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	int i, retval;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	/* Disable all Device Slots */
632*4882a593Smuzhiyun 	xhci_dbg(xhci, "Disable all slots\n");
633*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, *flags);
634*4882a593Smuzhiyun 	for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
635*4882a593Smuzhiyun 		if (!xhci->devs[i])
636*4882a593Smuzhiyun 			continue;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 		retval = xhci_disable_slot(xhci, i);
639*4882a593Smuzhiyun 		xhci_free_virt_device(xhci, i);
640*4882a593Smuzhiyun 		if (retval)
641*4882a593Smuzhiyun 			xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
642*4882a593Smuzhiyun 				 i, retval);
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, *flags);
645*4882a593Smuzhiyun 	/* Put all ports to the Disable state by clear PP */
646*4882a593Smuzhiyun 	xhci_dbg(xhci, "Disable all port (PP = 0)\n");
647*4882a593Smuzhiyun 	/* Power off USB3 ports*/
648*4882a593Smuzhiyun 	for (i = 0; i < xhci->usb3_rhub.num_ports; i++)
649*4882a593Smuzhiyun 		xhci_set_port_power(xhci, xhci->shared_hcd, i, false, flags);
650*4882a593Smuzhiyun 	/* Power off USB2 ports*/
651*4882a593Smuzhiyun 	for (i = 0; i < xhci->usb2_rhub.num_ports; i++)
652*4882a593Smuzhiyun 		xhci_set_port_power(xhci, xhci->main_hcd, i, false, flags);
653*4882a593Smuzhiyun 	/* Stop the controller */
654*4882a593Smuzhiyun 	xhci_dbg(xhci, "Stop controller\n");
655*4882a593Smuzhiyun 	retval = xhci_halt(xhci);
656*4882a593Smuzhiyun 	if (retval)
657*4882a593Smuzhiyun 		return retval;
658*4882a593Smuzhiyun 	/* Disable runtime PM for test mode */
659*4882a593Smuzhiyun 	pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
660*4882a593Smuzhiyun 	/* Set PORTPMSC.PTC field to enter selected test mode */
661*4882a593Smuzhiyun 	/* Port is selected by wIndex. port_id = wIndex + 1 */
662*4882a593Smuzhiyun 	xhci_dbg(xhci, "Enter Test Mode: %d, Port_id=%d\n",
663*4882a593Smuzhiyun 					test_mode, wIndex + 1);
664*4882a593Smuzhiyun 	xhci_port_set_test_mode(xhci, test_mode, wIndex);
665*4882a593Smuzhiyun 	return retval;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
xhci_exit_test_mode(struct xhci_hcd * xhci)668*4882a593Smuzhiyun static int xhci_exit_test_mode(struct xhci_hcd *xhci)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	int retval;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	if (!xhci->test_mode) {
673*4882a593Smuzhiyun 		xhci_err(xhci, "Not in test mode, do nothing.\n");
674*4882a593Smuzhiyun 		return 0;
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun 	if (xhci->test_mode == USB_TEST_FORCE_ENABLE &&
677*4882a593Smuzhiyun 		!(xhci->xhc_state & XHCI_STATE_HALTED)) {
678*4882a593Smuzhiyun 		retval = xhci_halt(xhci);
679*4882a593Smuzhiyun 		if (retval)
680*4882a593Smuzhiyun 			return retval;
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 	pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
683*4882a593Smuzhiyun 	xhci->test_mode = 0;
684*4882a593Smuzhiyun 	return xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
xhci_set_link_state(struct xhci_hcd * xhci,struct xhci_port * port,u32 link_state)687*4882a593Smuzhiyun void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,
688*4882a593Smuzhiyun 			 u32 link_state)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	u32 temp;
691*4882a593Smuzhiyun 	u32 portsc;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	portsc = readl(port->addr);
694*4882a593Smuzhiyun 	temp = xhci_port_state_to_neutral(portsc);
695*4882a593Smuzhiyun 	temp &= ~PORT_PLS_MASK;
696*4882a593Smuzhiyun 	temp |= PORT_LINK_STROBE | link_state;
697*4882a593Smuzhiyun 	writel(temp, port->addr);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	xhci_dbg(xhci, "Set port %d-%d link state, portsc: 0x%x, write 0x%x",
700*4882a593Smuzhiyun 		 port->rhub->hcd->self.busnum, port->hcd_portnum + 1,
701*4882a593Smuzhiyun 		 portsc, temp);
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
xhci_set_remote_wake_mask(struct xhci_hcd * xhci,struct xhci_port * port,u16 wake_mask)704*4882a593Smuzhiyun static void xhci_set_remote_wake_mask(struct xhci_hcd *xhci,
705*4882a593Smuzhiyun 				      struct xhci_port *port, u16 wake_mask)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	u32 temp;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	temp = readl(port->addr);
710*4882a593Smuzhiyun 	temp = xhci_port_state_to_neutral(temp);
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_CONNECT)
713*4882a593Smuzhiyun 		temp |= PORT_WKCONN_E;
714*4882a593Smuzhiyun 	else
715*4882a593Smuzhiyun 		temp &= ~PORT_WKCONN_E;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT)
718*4882a593Smuzhiyun 		temp |= PORT_WKDISC_E;
719*4882a593Smuzhiyun 	else
720*4882a593Smuzhiyun 		temp &= ~PORT_WKDISC_E;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	if (wake_mask & USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT)
723*4882a593Smuzhiyun 		temp |= PORT_WKOC_E;
724*4882a593Smuzhiyun 	else
725*4882a593Smuzhiyun 		temp &= ~PORT_WKOC_E;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	writel(temp, port->addr);
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /* Test and clear port RWC bit */
xhci_test_and_clear_bit(struct xhci_hcd * xhci,struct xhci_port * port,u32 port_bit)731*4882a593Smuzhiyun void xhci_test_and_clear_bit(struct xhci_hcd *xhci, struct xhci_port *port,
732*4882a593Smuzhiyun 			     u32 port_bit)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	u32 temp;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	temp = readl(port->addr);
737*4882a593Smuzhiyun 	if (temp & port_bit) {
738*4882a593Smuzhiyun 		temp = xhci_port_state_to_neutral(temp);
739*4882a593Smuzhiyun 		temp |= port_bit;
740*4882a593Smuzhiyun 		writel(temp, port->addr);
741*4882a593Smuzhiyun 	}
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun /* Updates Link Status for super Speed port */
xhci_hub_report_usb3_link_state(struct xhci_hcd * xhci,u32 * status,u32 status_reg)745*4882a593Smuzhiyun static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
746*4882a593Smuzhiyun 		u32 *status, u32 status_reg)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	u32 pls = status_reg & PORT_PLS_MASK;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* When the CAS bit is set then warm reset
751*4882a593Smuzhiyun 	 * should be performed on port
752*4882a593Smuzhiyun 	 */
753*4882a593Smuzhiyun 	if (status_reg & PORT_CAS) {
754*4882a593Smuzhiyun 		/* The CAS bit can be set while the port is
755*4882a593Smuzhiyun 		 * in any link state.
756*4882a593Smuzhiyun 		 * Only roothubs have CAS bit, so we
757*4882a593Smuzhiyun 		 * pretend to be in compliance mode
758*4882a593Smuzhiyun 		 * unless we're already in compliance
759*4882a593Smuzhiyun 		 * or the inactive state.
760*4882a593Smuzhiyun 		 */
761*4882a593Smuzhiyun 		if (pls != USB_SS_PORT_LS_COMP_MOD &&
762*4882a593Smuzhiyun 		    pls != USB_SS_PORT_LS_SS_INACTIVE) {
763*4882a593Smuzhiyun 			pls = USB_SS_PORT_LS_COMP_MOD;
764*4882a593Smuzhiyun 		}
765*4882a593Smuzhiyun 		/* Return also connection bit -
766*4882a593Smuzhiyun 		 * hub state machine resets port
767*4882a593Smuzhiyun 		 * when this bit is set.
768*4882a593Smuzhiyun 		 */
769*4882a593Smuzhiyun 		pls |= USB_PORT_STAT_CONNECTION;
770*4882a593Smuzhiyun 	} else {
771*4882a593Smuzhiyun 		/*
772*4882a593Smuzhiyun 		 * Resume state is an xHCI internal state.  Do not report it to
773*4882a593Smuzhiyun 		 * usb core, instead, pretend to be U3, thus usb core knows
774*4882a593Smuzhiyun 		 * it's not ready for transfer.
775*4882a593Smuzhiyun 		 */
776*4882a593Smuzhiyun 		if (pls == XDEV_RESUME) {
777*4882a593Smuzhiyun 			*status |= USB_SS_PORT_LS_U3;
778*4882a593Smuzhiyun 			return;
779*4882a593Smuzhiyun 		}
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		/*
782*4882a593Smuzhiyun 		 * If CAS bit isn't set but the Port is already at
783*4882a593Smuzhiyun 		 * Compliance Mode, fake a connection so the USB core
784*4882a593Smuzhiyun 		 * notices the Compliance state and resets the port.
785*4882a593Smuzhiyun 		 * This resolves an issue generated by the SN65LVPE502CP
786*4882a593Smuzhiyun 		 * in which sometimes the port enters compliance mode
787*4882a593Smuzhiyun 		 * caused by a delay on the host-device negotiation.
788*4882a593Smuzhiyun 		 */
789*4882a593Smuzhiyun 		if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
790*4882a593Smuzhiyun 				(pls == USB_SS_PORT_LS_COMP_MOD))
791*4882a593Smuzhiyun 			pls |= USB_PORT_STAT_CONNECTION;
792*4882a593Smuzhiyun 	}
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/* update status field */
795*4882a593Smuzhiyun 	*status |= pls;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /*
799*4882a593Smuzhiyun  * Function for Compliance Mode Quirk.
800*4882a593Smuzhiyun  *
801*4882a593Smuzhiyun  * This Function verifies if all xhc USB3 ports have entered U0, if so,
802*4882a593Smuzhiyun  * the compliance mode timer is deleted. A port won't enter
803*4882a593Smuzhiyun  * compliance mode if it has previously entered U0.
804*4882a593Smuzhiyun  */
xhci_del_comp_mod_timer(struct xhci_hcd * xhci,u32 status,u16 wIndex)805*4882a593Smuzhiyun static void xhci_del_comp_mod_timer(struct xhci_hcd *xhci, u32 status,
806*4882a593Smuzhiyun 				    u16 wIndex)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	u32 all_ports_seen_u0 = ((1 << xhci->usb3_rhub.num_ports) - 1);
809*4882a593Smuzhiyun 	bool port_in_u0 = ((status & PORT_PLS_MASK) == XDEV_U0);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	if (!(xhci->quirks & XHCI_COMP_MODE_QUIRK))
812*4882a593Smuzhiyun 		return;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	if ((xhci->port_status_u0 != all_ports_seen_u0) && port_in_u0) {
815*4882a593Smuzhiyun 		xhci->port_status_u0 |= 1 << wIndex;
816*4882a593Smuzhiyun 		if (xhci->port_status_u0 == all_ports_seen_u0) {
817*4882a593Smuzhiyun 			del_timer_sync(&xhci->comp_mode_recovery_timer);
818*4882a593Smuzhiyun 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
819*4882a593Smuzhiyun 				"All USB3 ports have entered U0 already!");
820*4882a593Smuzhiyun 			xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
821*4882a593Smuzhiyun 				"Compliance Mode Recovery Timer Deleted.");
822*4882a593Smuzhiyun 		}
823*4882a593Smuzhiyun 	}
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun 
xhci_handle_usb2_port_link_resume(struct xhci_port * port,u32 * status,u32 portsc,unsigned long * flags)826*4882a593Smuzhiyun static int xhci_handle_usb2_port_link_resume(struct xhci_port *port,
827*4882a593Smuzhiyun 					     u32 *status, u32 portsc,
828*4882a593Smuzhiyun 					     unsigned long *flags)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
831*4882a593Smuzhiyun 	struct xhci_hcd	*xhci;
832*4882a593Smuzhiyun 	struct usb_hcd *hcd;
833*4882a593Smuzhiyun 	int slot_id;
834*4882a593Smuzhiyun 	u32 wIndex;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	hcd = port->rhub->hcd;
837*4882a593Smuzhiyun 	bus_state = &port->rhub->bus_state;
838*4882a593Smuzhiyun 	xhci = hcd_to_xhci(hcd);
839*4882a593Smuzhiyun 	wIndex = port->hcd_portnum;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	if ((portsc & PORT_RESET) || !(portsc & PORT_PE)) {
842*4882a593Smuzhiyun 		*status = 0xffffffff;
843*4882a593Smuzhiyun 		return -EINVAL;
844*4882a593Smuzhiyun 	}
845*4882a593Smuzhiyun 	/* did port event handler already start resume timing? */
846*4882a593Smuzhiyun 	if (!bus_state->resume_done[wIndex]) {
847*4882a593Smuzhiyun 		/* If not, maybe we are in a host initated resume? */
848*4882a593Smuzhiyun 		if (test_bit(wIndex, &bus_state->resuming_ports)) {
849*4882a593Smuzhiyun 			/* Host initated resume doesn't time the resume
850*4882a593Smuzhiyun 			 * signalling using resume_done[].
851*4882a593Smuzhiyun 			 * It manually sets RESUME state, sleeps 20ms
852*4882a593Smuzhiyun 			 * and sets U0 state. This should probably be
853*4882a593Smuzhiyun 			 * changed, but not right now.
854*4882a593Smuzhiyun 			 */
855*4882a593Smuzhiyun 		} else {
856*4882a593Smuzhiyun 			/* port resume was discovered now and here,
857*4882a593Smuzhiyun 			 * start resume timing
858*4882a593Smuzhiyun 			 */
859*4882a593Smuzhiyun 			unsigned long timeout = jiffies +
860*4882a593Smuzhiyun 				msecs_to_jiffies(USB_RESUME_TIMEOUT);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 			set_bit(wIndex, &bus_state->resuming_ports);
863*4882a593Smuzhiyun 			bus_state->resume_done[wIndex] = timeout;
864*4882a593Smuzhiyun 			mod_timer(&hcd->rh_timer, timeout);
865*4882a593Smuzhiyun 			usb_hcd_start_port_resume(&hcd->self, wIndex);
866*4882a593Smuzhiyun 		}
867*4882a593Smuzhiyun 	/* Has resume been signalled for USB_RESUME_TIME yet? */
868*4882a593Smuzhiyun 	} else if (time_after_eq(jiffies, bus_state->resume_done[wIndex])) {
869*4882a593Smuzhiyun 		int time_left;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 		xhci_dbg(xhci, "resume USB2 port %d-%d\n",
872*4882a593Smuzhiyun 			 hcd->self.busnum, wIndex + 1);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 		bus_state->resume_done[wIndex] = 0;
875*4882a593Smuzhiyun 		clear_bit(wIndex, &bus_state->resuming_ports);
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 		set_bit(wIndex, &bus_state->rexit_ports);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 		xhci_test_and_clear_bit(xhci, port, PORT_PLC);
880*4882a593Smuzhiyun 		xhci_set_link_state(xhci, port, XDEV_U0);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 		spin_unlock_irqrestore(&xhci->lock, *flags);
883*4882a593Smuzhiyun 		time_left = wait_for_completion_timeout(
884*4882a593Smuzhiyun 			&bus_state->rexit_done[wIndex],
885*4882a593Smuzhiyun 			msecs_to_jiffies(XHCI_MAX_REXIT_TIMEOUT_MS));
886*4882a593Smuzhiyun 		spin_lock_irqsave(&xhci->lock, *flags);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		if (time_left) {
889*4882a593Smuzhiyun 			slot_id = xhci_find_slot_id_by_port(hcd, xhci,
890*4882a593Smuzhiyun 							    wIndex + 1);
891*4882a593Smuzhiyun 			if (!slot_id) {
892*4882a593Smuzhiyun 				xhci_dbg(xhci, "slot_id is zero\n");
893*4882a593Smuzhiyun 				*status = 0xffffffff;
894*4882a593Smuzhiyun 				return -ENODEV;
895*4882a593Smuzhiyun 			}
896*4882a593Smuzhiyun 			xhci_ring_device(xhci, slot_id);
897*4882a593Smuzhiyun 		} else {
898*4882a593Smuzhiyun 			int port_status = readl(port->addr);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 			xhci_warn(xhci, "Port resume timed out, port %d-%d: 0x%x\n",
901*4882a593Smuzhiyun 				  hcd->self.busnum, wIndex + 1, port_status);
902*4882a593Smuzhiyun 			*status |= USB_PORT_STAT_SUSPEND;
903*4882a593Smuzhiyun 			clear_bit(wIndex, &bus_state->rexit_ports);
904*4882a593Smuzhiyun 		}
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 		usb_hcd_end_port_resume(&hcd->self, wIndex);
907*4882a593Smuzhiyun 		bus_state->port_c_suspend |= 1 << wIndex;
908*4882a593Smuzhiyun 		bus_state->suspended_ports &= ~(1 << wIndex);
909*4882a593Smuzhiyun 	} else {
910*4882a593Smuzhiyun 		/*
911*4882a593Smuzhiyun 		 * The resume has been signaling for less than
912*4882a593Smuzhiyun 		 * USB_RESUME_TIME. Report the port status as SUSPEND,
913*4882a593Smuzhiyun 		 * let the usbcore check port status again and clear
914*4882a593Smuzhiyun 		 * resume signaling later.
915*4882a593Smuzhiyun 		 */
916*4882a593Smuzhiyun 		*status |= USB_PORT_STAT_SUSPEND;
917*4882a593Smuzhiyun 	}
918*4882a593Smuzhiyun 	return 0;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
xhci_get_ext_port_status(u32 raw_port_status,u32 port_li)921*4882a593Smuzhiyun static u32 xhci_get_ext_port_status(u32 raw_port_status, u32 port_li)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	u32 ext_stat = 0;
924*4882a593Smuzhiyun 	int speed_id;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* only support rx and tx lane counts of 1 in usb3.1 spec */
927*4882a593Smuzhiyun 	speed_id = DEV_PORT_SPEED(raw_port_status);
928*4882a593Smuzhiyun 	ext_stat |= speed_id;		/* bits 3:0, RX speed id */
929*4882a593Smuzhiyun 	ext_stat |= speed_id << 4;	/* bits 7:4, TX speed id */
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	ext_stat |= PORT_RX_LANES(port_li) << 8;  /* bits 11:8 Rx lane count */
932*4882a593Smuzhiyun 	ext_stat |= PORT_TX_LANES(port_li) << 12; /* bits 15:12 Tx lane count */
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	return ext_stat;
935*4882a593Smuzhiyun }
936*4882a593Smuzhiyun 
xhci_get_usb3_port_status(struct xhci_port * port,u32 * status,u32 portsc)937*4882a593Smuzhiyun static void xhci_get_usb3_port_status(struct xhci_port *port, u32 *status,
938*4882a593Smuzhiyun 				      u32 portsc)
939*4882a593Smuzhiyun {
940*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
941*4882a593Smuzhiyun 	struct xhci_hcd	*xhci;
942*4882a593Smuzhiyun 	struct usb_hcd *hcd;
943*4882a593Smuzhiyun 	u32 link_state;
944*4882a593Smuzhiyun 	u32 portnum;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	bus_state = &port->rhub->bus_state;
947*4882a593Smuzhiyun 	xhci = hcd_to_xhci(port->rhub->hcd);
948*4882a593Smuzhiyun 	hcd = port->rhub->hcd;
949*4882a593Smuzhiyun 	link_state = portsc & PORT_PLS_MASK;
950*4882a593Smuzhiyun 	portnum = port->hcd_portnum;
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	/* USB3 specific wPortChange bits
953*4882a593Smuzhiyun 	 *
954*4882a593Smuzhiyun 	 * Port link change with port in resume state should not be
955*4882a593Smuzhiyun 	 * reported to usbcore, as this is an internal state to be
956*4882a593Smuzhiyun 	 * handled by xhci driver. Reporting PLC to usbcore may
957*4882a593Smuzhiyun 	 * cause usbcore clearing PLC first and port change event
958*4882a593Smuzhiyun 	 * irq won't be generated.
959*4882a593Smuzhiyun 	 */
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	if (portsc & PORT_PLC && (link_state != XDEV_RESUME))
962*4882a593Smuzhiyun 		*status |= USB_PORT_STAT_C_LINK_STATE << 16;
963*4882a593Smuzhiyun 	if (portsc & PORT_WRC)
964*4882a593Smuzhiyun 		*status |= USB_PORT_STAT_C_BH_RESET << 16;
965*4882a593Smuzhiyun 	if (portsc & PORT_CEC)
966*4882a593Smuzhiyun 		*status |= USB_PORT_STAT_C_CONFIG_ERROR << 16;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	/* USB3 specific wPortStatus bits */
969*4882a593Smuzhiyun 	if (portsc & PORT_POWER) {
970*4882a593Smuzhiyun 		*status |= USB_SS_PORT_STAT_POWER;
971*4882a593Smuzhiyun 		/* link state handling */
972*4882a593Smuzhiyun 		if (link_state == XDEV_U0)
973*4882a593Smuzhiyun 			bus_state->suspended_ports &= ~(1 << portnum);
974*4882a593Smuzhiyun 	}
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	/* remote wake resume signaling complete */
977*4882a593Smuzhiyun 	if (bus_state->port_remote_wakeup & (1 << portnum) &&
978*4882a593Smuzhiyun 	    link_state != XDEV_RESUME &&
979*4882a593Smuzhiyun 	    link_state != XDEV_RECOVERY) {
980*4882a593Smuzhiyun 		bus_state->port_remote_wakeup &= ~(1 << portnum);
981*4882a593Smuzhiyun 		usb_hcd_end_port_resume(&hcd->self, portnum);
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	xhci_hub_report_usb3_link_state(xhci, status, portsc);
985*4882a593Smuzhiyun 	xhci_del_comp_mod_timer(xhci, portsc, portnum);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
xhci_get_usb2_port_status(struct xhci_port * port,u32 * status,u32 portsc,unsigned long * flags)988*4882a593Smuzhiyun static void xhci_get_usb2_port_status(struct xhci_port *port, u32 *status,
989*4882a593Smuzhiyun 				      u32 portsc, unsigned long *flags)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
992*4882a593Smuzhiyun 	u32 link_state;
993*4882a593Smuzhiyun 	u32 portnum;
994*4882a593Smuzhiyun 	int ret;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	bus_state = &port->rhub->bus_state;
997*4882a593Smuzhiyun 	link_state = portsc & PORT_PLS_MASK;
998*4882a593Smuzhiyun 	portnum = port->hcd_portnum;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	/* USB2 wPortStatus bits */
1001*4882a593Smuzhiyun 	if (portsc & PORT_POWER) {
1002*4882a593Smuzhiyun 		*status |= USB_PORT_STAT_POWER;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 		/* link state is only valid if port is powered */
1005*4882a593Smuzhiyun 		if (link_state == XDEV_U3)
1006*4882a593Smuzhiyun 			*status |= USB_PORT_STAT_SUSPEND;
1007*4882a593Smuzhiyun 		if (link_state == XDEV_U2)
1008*4882a593Smuzhiyun 			*status |= USB_PORT_STAT_L1;
1009*4882a593Smuzhiyun 		if (link_state == XDEV_U0) {
1010*4882a593Smuzhiyun 			if (bus_state->resume_done[portnum])
1011*4882a593Smuzhiyun 				usb_hcd_end_port_resume(&port->rhub->hcd->self,
1012*4882a593Smuzhiyun 							portnum);
1013*4882a593Smuzhiyun 			bus_state->resume_done[portnum] = 0;
1014*4882a593Smuzhiyun 			clear_bit(portnum, &bus_state->resuming_ports);
1015*4882a593Smuzhiyun 			if (bus_state->suspended_ports & (1 << portnum)) {
1016*4882a593Smuzhiyun 				bus_state->suspended_ports &= ~(1 << portnum);
1017*4882a593Smuzhiyun 				bus_state->port_c_suspend |= 1 << portnum;
1018*4882a593Smuzhiyun 			}
1019*4882a593Smuzhiyun 		}
1020*4882a593Smuzhiyun 		if (link_state == XDEV_RESUME) {
1021*4882a593Smuzhiyun 			ret = xhci_handle_usb2_port_link_resume(port, status,
1022*4882a593Smuzhiyun 								portsc, flags);
1023*4882a593Smuzhiyun 			if (ret)
1024*4882a593Smuzhiyun 				return;
1025*4882a593Smuzhiyun 		}
1026*4882a593Smuzhiyun 	}
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun /*
1030*4882a593Smuzhiyun  * Converts a raw xHCI port status into the format that external USB 2.0 or USB
1031*4882a593Smuzhiyun  * 3.0 hubs use.
1032*4882a593Smuzhiyun  *
1033*4882a593Smuzhiyun  * Possible side effects:
1034*4882a593Smuzhiyun  *  - Mark a port as being done with device resume,
1035*4882a593Smuzhiyun  *    and ring the endpoint doorbells.
1036*4882a593Smuzhiyun  *  - Stop the Synopsys redriver Compliance Mode polling.
1037*4882a593Smuzhiyun  *  - Drop and reacquire the xHCI lock, in order to wait for port resume.
1038*4882a593Smuzhiyun  */
xhci_get_port_status(struct usb_hcd * hcd,struct xhci_bus_state * bus_state,u16 wIndex,u32 raw_port_status,unsigned long * flags)1039*4882a593Smuzhiyun static u32 xhci_get_port_status(struct usb_hcd *hcd,
1040*4882a593Smuzhiyun 		struct xhci_bus_state *bus_state,
1041*4882a593Smuzhiyun 	u16 wIndex, u32 raw_port_status,
1042*4882a593Smuzhiyun 		unsigned long *flags)
1043*4882a593Smuzhiyun 	__releases(&xhci->lock)
1044*4882a593Smuzhiyun 	__acquires(&xhci->lock)
1045*4882a593Smuzhiyun {
1046*4882a593Smuzhiyun 	u32 status = 0;
1047*4882a593Smuzhiyun 	struct xhci_hub *rhub;
1048*4882a593Smuzhiyun 	struct xhci_port *port;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
1051*4882a593Smuzhiyun 	port = rhub->ports[wIndex];
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	/* common wPortChange bits */
1054*4882a593Smuzhiyun 	if (raw_port_status & PORT_CSC)
1055*4882a593Smuzhiyun 		status |= USB_PORT_STAT_C_CONNECTION << 16;
1056*4882a593Smuzhiyun 	if (raw_port_status & PORT_PEC)
1057*4882a593Smuzhiyun 		status |= USB_PORT_STAT_C_ENABLE << 16;
1058*4882a593Smuzhiyun 	if ((raw_port_status & PORT_OCC))
1059*4882a593Smuzhiyun 		status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1060*4882a593Smuzhiyun 	if ((raw_port_status & PORT_RC))
1061*4882a593Smuzhiyun 		status |= USB_PORT_STAT_C_RESET << 16;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	/* common wPortStatus bits */
1064*4882a593Smuzhiyun 	if (raw_port_status & PORT_CONNECT) {
1065*4882a593Smuzhiyun 		status |= USB_PORT_STAT_CONNECTION;
1066*4882a593Smuzhiyun 		status |= xhci_port_speed(raw_port_status);
1067*4882a593Smuzhiyun 	}
1068*4882a593Smuzhiyun 	if (raw_port_status & PORT_PE)
1069*4882a593Smuzhiyun 		status |= USB_PORT_STAT_ENABLE;
1070*4882a593Smuzhiyun 	if (raw_port_status & PORT_OC)
1071*4882a593Smuzhiyun 		status |= USB_PORT_STAT_OVERCURRENT;
1072*4882a593Smuzhiyun 	if (raw_port_status & PORT_RESET)
1073*4882a593Smuzhiyun 		status |= USB_PORT_STAT_RESET;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	/* USB2 and USB3 specific bits, including Port Link State */
1076*4882a593Smuzhiyun 	if (hcd->speed >= HCD_USB3)
1077*4882a593Smuzhiyun 		xhci_get_usb3_port_status(port, &status, raw_port_status);
1078*4882a593Smuzhiyun 	else
1079*4882a593Smuzhiyun 		xhci_get_usb2_port_status(port, &status, raw_port_status,
1080*4882a593Smuzhiyun 					  flags);
1081*4882a593Smuzhiyun 	/*
1082*4882a593Smuzhiyun 	 * Clear stale usb2 resume signalling variables in case port changed
1083*4882a593Smuzhiyun 	 * state during resume signalling. For example on error
1084*4882a593Smuzhiyun 	 */
1085*4882a593Smuzhiyun 	if ((bus_state->resume_done[wIndex] ||
1086*4882a593Smuzhiyun 	     test_bit(wIndex, &bus_state->resuming_ports)) &&
1087*4882a593Smuzhiyun 	    (raw_port_status & PORT_PLS_MASK) != XDEV_U3 &&
1088*4882a593Smuzhiyun 	    (raw_port_status & PORT_PLS_MASK) != XDEV_RESUME) {
1089*4882a593Smuzhiyun 		bus_state->resume_done[wIndex] = 0;
1090*4882a593Smuzhiyun 		clear_bit(wIndex, &bus_state->resuming_ports);
1091*4882a593Smuzhiyun 		usb_hcd_end_port_resume(&hcd->self, wIndex);
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	if (bus_state->port_c_suspend & (1 << wIndex))
1095*4882a593Smuzhiyun 		status |= USB_PORT_STAT_C_SUSPEND << 16;
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	return status;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
xhci_hub_control(struct usb_hcd * hcd,u16 typeReq,u16 wValue,u16 wIndex,char * buf,u16 wLength)1100*4882a593Smuzhiyun int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
1101*4882a593Smuzhiyun 		u16 wIndex, char *buf, u16 wLength)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
1104*4882a593Smuzhiyun 	int max_ports;
1105*4882a593Smuzhiyun 	unsigned long flags;
1106*4882a593Smuzhiyun 	u32 temp, status;
1107*4882a593Smuzhiyun 	int retval = 0;
1108*4882a593Smuzhiyun 	int slot_id;
1109*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
1110*4882a593Smuzhiyun 	u16 link_state = 0;
1111*4882a593Smuzhiyun 	u16 wake_mask = 0;
1112*4882a593Smuzhiyun 	u16 timeout = 0;
1113*4882a593Smuzhiyun 	u16 test_mode = 0;
1114*4882a593Smuzhiyun 	struct xhci_hub *rhub;
1115*4882a593Smuzhiyun 	struct xhci_port **ports;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
1118*4882a593Smuzhiyun 	ports = rhub->ports;
1119*4882a593Smuzhiyun 	max_ports = rhub->num_ports;
1120*4882a593Smuzhiyun 	bus_state = &rhub->bus_state;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, flags);
1123*4882a593Smuzhiyun 	switch (typeReq) {
1124*4882a593Smuzhiyun 	case GetHubStatus:
1125*4882a593Smuzhiyun 		/* No power source, over-current reported per port */
1126*4882a593Smuzhiyun 		memset(buf, 0, 4);
1127*4882a593Smuzhiyun 		break;
1128*4882a593Smuzhiyun 	case GetHubDescriptor:
1129*4882a593Smuzhiyun 		/* Check to make sure userspace is asking for the USB 3.0 hub
1130*4882a593Smuzhiyun 		 * descriptor for the USB 3.0 roothub.  If not, we stall the
1131*4882a593Smuzhiyun 		 * endpoint, like external hubs do.
1132*4882a593Smuzhiyun 		 */
1133*4882a593Smuzhiyun 		if (hcd->speed >= HCD_USB3 &&
1134*4882a593Smuzhiyun 				(wLength < USB_DT_SS_HUB_SIZE ||
1135*4882a593Smuzhiyun 				 wValue != (USB_DT_SS_HUB << 8))) {
1136*4882a593Smuzhiyun 			xhci_dbg(xhci, "Wrong hub descriptor type for "
1137*4882a593Smuzhiyun 					"USB 3.0 roothub.\n");
1138*4882a593Smuzhiyun 			goto error;
1139*4882a593Smuzhiyun 		}
1140*4882a593Smuzhiyun 		xhci_hub_descriptor(hcd, xhci,
1141*4882a593Smuzhiyun 				(struct usb_hub_descriptor *) buf);
1142*4882a593Smuzhiyun 		break;
1143*4882a593Smuzhiyun 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
1144*4882a593Smuzhiyun 		if ((wValue & 0xff00) != (USB_DT_BOS << 8))
1145*4882a593Smuzhiyun 			goto error;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 		if (hcd->speed < HCD_USB3)
1148*4882a593Smuzhiyun 			goto error;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 		retval = xhci_create_usb3_bos_desc(xhci, buf, wLength);
1151*4882a593Smuzhiyun 		spin_unlock_irqrestore(&xhci->lock, flags);
1152*4882a593Smuzhiyun 		return retval;
1153*4882a593Smuzhiyun 	case GetPortStatus:
1154*4882a593Smuzhiyun 		if (!wIndex || wIndex > max_ports)
1155*4882a593Smuzhiyun 			goto error;
1156*4882a593Smuzhiyun 		wIndex--;
1157*4882a593Smuzhiyun 		temp = readl(ports[wIndex]->addr);
1158*4882a593Smuzhiyun 		if (temp == ~(u32)0) {
1159*4882a593Smuzhiyun 			xhci_hc_died(xhci);
1160*4882a593Smuzhiyun 			retval = -ENODEV;
1161*4882a593Smuzhiyun 			break;
1162*4882a593Smuzhiyun 		}
1163*4882a593Smuzhiyun 		trace_xhci_get_port_status(wIndex, temp);
1164*4882a593Smuzhiyun 		status = xhci_get_port_status(hcd, bus_state, wIndex, temp,
1165*4882a593Smuzhiyun 					      &flags);
1166*4882a593Smuzhiyun 		if (status == 0xffffffff)
1167*4882a593Smuzhiyun 			goto error;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 		xhci_dbg(xhci, "Get port status %d-%d read: 0x%x, return 0x%x",
1170*4882a593Smuzhiyun 			 hcd->self.busnum, wIndex + 1, temp, status);
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 		put_unaligned(cpu_to_le32(status), (__le32 *) buf);
1173*4882a593Smuzhiyun 		/* if USB 3.1 extended port status return additional 4 bytes */
1174*4882a593Smuzhiyun 		if (wValue == 0x02) {
1175*4882a593Smuzhiyun 			u32 port_li;
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 			if (hcd->speed < HCD_USB31 || wLength != 8) {
1178*4882a593Smuzhiyun 				xhci_err(xhci, "get ext port status invalid parameter\n");
1179*4882a593Smuzhiyun 				retval = -EINVAL;
1180*4882a593Smuzhiyun 				break;
1181*4882a593Smuzhiyun 			}
1182*4882a593Smuzhiyun 			port_li = readl(ports[wIndex]->addr + PORTLI);
1183*4882a593Smuzhiyun 			status = xhci_get_ext_port_status(temp, port_li);
1184*4882a593Smuzhiyun 			put_unaligned_le32(status, &buf[4]);
1185*4882a593Smuzhiyun 		}
1186*4882a593Smuzhiyun 		break;
1187*4882a593Smuzhiyun 	case SetPortFeature:
1188*4882a593Smuzhiyun 		if (wValue == USB_PORT_FEAT_LINK_STATE)
1189*4882a593Smuzhiyun 			link_state = (wIndex & 0xff00) >> 3;
1190*4882a593Smuzhiyun 		if (wValue == USB_PORT_FEAT_REMOTE_WAKE_MASK)
1191*4882a593Smuzhiyun 			wake_mask = wIndex & 0xff00;
1192*4882a593Smuzhiyun 		if (wValue == USB_PORT_FEAT_TEST)
1193*4882a593Smuzhiyun 			test_mode = (wIndex & 0xff00) >> 8;
1194*4882a593Smuzhiyun 		/* The MSB of wIndex is the U1/U2 timeout */
1195*4882a593Smuzhiyun 		timeout = (wIndex & 0xff00) >> 8;
1196*4882a593Smuzhiyun 		wIndex &= 0xff;
1197*4882a593Smuzhiyun 		if (!wIndex || wIndex > max_ports)
1198*4882a593Smuzhiyun 			goto error;
1199*4882a593Smuzhiyun 		wIndex--;
1200*4882a593Smuzhiyun 		temp = readl(ports[wIndex]->addr);
1201*4882a593Smuzhiyun 		if (temp == ~(u32)0) {
1202*4882a593Smuzhiyun 			xhci_hc_died(xhci);
1203*4882a593Smuzhiyun 			retval = -ENODEV;
1204*4882a593Smuzhiyun 			break;
1205*4882a593Smuzhiyun 		}
1206*4882a593Smuzhiyun 		temp = xhci_port_state_to_neutral(temp);
1207*4882a593Smuzhiyun 		/* FIXME: What new port features do we need to support? */
1208*4882a593Smuzhiyun 		switch (wValue) {
1209*4882a593Smuzhiyun 		case USB_PORT_FEAT_SUSPEND:
1210*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1211*4882a593Smuzhiyun 			if ((temp & PORT_PLS_MASK) != XDEV_U0) {
1212*4882a593Smuzhiyun 				/* Resume the port to U0 first */
1213*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex],
1214*4882a593Smuzhiyun 							XDEV_U0);
1215*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1216*4882a593Smuzhiyun 				msleep(10);
1217*4882a593Smuzhiyun 				spin_lock_irqsave(&xhci->lock, flags);
1218*4882a593Smuzhiyun 			}
1219*4882a593Smuzhiyun 			/* In spec software should not attempt to suspend
1220*4882a593Smuzhiyun 			 * a port unless the port reports that it is in the
1221*4882a593Smuzhiyun 			 * enabled (PED = ‘1’,PLS < ‘3’) state.
1222*4882a593Smuzhiyun 			 */
1223*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1224*4882a593Smuzhiyun 			if ((temp & PORT_PE) == 0 || (temp & PORT_RESET)
1225*4882a593Smuzhiyun 				|| (temp & PORT_PLS_MASK) >= XDEV_U3) {
1226*4882a593Smuzhiyun 				xhci_warn(xhci, "USB core suspending port %d-%d not in U0/U1/U2\n",
1227*4882a593Smuzhiyun 					  hcd->self.busnum, wIndex + 1);
1228*4882a593Smuzhiyun 				goto error;
1229*4882a593Smuzhiyun 			}
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 			slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1232*4882a593Smuzhiyun 					wIndex + 1);
1233*4882a593Smuzhiyun 			if (!slot_id) {
1234*4882a593Smuzhiyun 				xhci_warn(xhci, "slot_id is zero\n");
1235*4882a593Smuzhiyun 				goto error;
1236*4882a593Smuzhiyun 			}
1237*4882a593Smuzhiyun 			/* unlock to execute stop endpoint commands */
1238*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1239*4882a593Smuzhiyun 			xhci_stop_device(xhci, slot_id, 1);
1240*4882a593Smuzhiyun 			spin_lock_irqsave(&xhci->lock, flags);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 			xhci_set_link_state(xhci, ports[wIndex], XDEV_U3);
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1245*4882a593Smuzhiyun 			msleep(10); /* wait device to enter */
1246*4882a593Smuzhiyun 			spin_lock_irqsave(&xhci->lock, flags);
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1249*4882a593Smuzhiyun 			bus_state->suspended_ports |= 1 << wIndex;
1250*4882a593Smuzhiyun 			break;
1251*4882a593Smuzhiyun 		case USB_PORT_FEAT_LINK_STATE:
1252*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1253*4882a593Smuzhiyun 			/* Disable port */
1254*4882a593Smuzhiyun 			if (link_state == USB_SS_PORT_LS_SS_DISABLED) {
1255*4882a593Smuzhiyun 				xhci_dbg(xhci, "Disable port %d-%d\n",
1256*4882a593Smuzhiyun 					 hcd->self.busnum, wIndex + 1);
1257*4882a593Smuzhiyun 				temp = xhci_port_state_to_neutral(temp);
1258*4882a593Smuzhiyun 				/*
1259*4882a593Smuzhiyun 				 * Clear all change bits, so that we get a new
1260*4882a593Smuzhiyun 				 * connection event.
1261*4882a593Smuzhiyun 				 */
1262*4882a593Smuzhiyun 				temp |= PORT_CSC | PORT_PEC | PORT_WRC |
1263*4882a593Smuzhiyun 					PORT_OCC | PORT_RC | PORT_PLC |
1264*4882a593Smuzhiyun 					PORT_CEC;
1265*4882a593Smuzhiyun 				writel(temp | PORT_PE, ports[wIndex]->addr);
1266*4882a593Smuzhiyun 				temp = readl(ports[wIndex]->addr);
1267*4882a593Smuzhiyun 				break;
1268*4882a593Smuzhiyun 			}
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 			/* Put link in RxDetect (enable port) */
1271*4882a593Smuzhiyun 			if (link_state == USB_SS_PORT_LS_RX_DETECT) {
1272*4882a593Smuzhiyun 				xhci_dbg(xhci, "Enable port %d-%d\n",
1273*4882a593Smuzhiyun 					 hcd->self.busnum, wIndex + 1);
1274*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex],
1275*4882a593Smuzhiyun 							link_state);
1276*4882a593Smuzhiyun 				temp = readl(ports[wIndex]->addr);
1277*4882a593Smuzhiyun 				break;
1278*4882a593Smuzhiyun 			}
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 			/*
1281*4882a593Smuzhiyun 			 * For xHCI 1.1 according to section 4.19.1.2.4.1 a
1282*4882a593Smuzhiyun 			 * root hub port's transition to compliance mode upon
1283*4882a593Smuzhiyun 			 * detecting LFPS timeout may be controlled by an
1284*4882a593Smuzhiyun 			 * Compliance Transition Enabled (CTE) flag (not
1285*4882a593Smuzhiyun 			 * software visible). This flag is set by writing 0xA
1286*4882a593Smuzhiyun 			 * to PORTSC PLS field which will allow transition to
1287*4882a593Smuzhiyun 			 * compliance mode the next time LFPS timeout is
1288*4882a593Smuzhiyun 			 * encountered. A warm reset will clear it.
1289*4882a593Smuzhiyun 			 *
1290*4882a593Smuzhiyun 			 * The CTE flag is only supported if the HCCPARAMS2 CTC
1291*4882a593Smuzhiyun 			 * flag is set, otherwise, the compliance substate is
1292*4882a593Smuzhiyun 			 * automatically entered as on 1.0 and prior.
1293*4882a593Smuzhiyun 			 */
1294*4882a593Smuzhiyun 			if (link_state == USB_SS_PORT_LS_COMP_MOD) {
1295*4882a593Smuzhiyun 				if (!HCC2_CTC(xhci->hcc_params2)) {
1296*4882a593Smuzhiyun 					xhci_dbg(xhci, "CTC flag is 0, port already supports entering compliance mode\n");
1297*4882a593Smuzhiyun 					break;
1298*4882a593Smuzhiyun 				}
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 				if ((temp & PORT_CONNECT)) {
1301*4882a593Smuzhiyun 					xhci_warn(xhci, "Can't set compliance mode when port is connected\n");
1302*4882a593Smuzhiyun 					goto error;
1303*4882a593Smuzhiyun 				}
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 				xhci_dbg(xhci, "Enable compliance mode transition for port %d-%d\n",
1306*4882a593Smuzhiyun 					 hcd->self.busnum, wIndex + 1);
1307*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex],
1308*4882a593Smuzhiyun 						link_state);
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 				temp = readl(ports[wIndex]->addr);
1311*4882a593Smuzhiyun 				break;
1312*4882a593Smuzhiyun 			}
1313*4882a593Smuzhiyun 			/* Port must be enabled */
1314*4882a593Smuzhiyun 			if (!(temp & PORT_PE)) {
1315*4882a593Smuzhiyun 				retval = -ENODEV;
1316*4882a593Smuzhiyun 				break;
1317*4882a593Smuzhiyun 			}
1318*4882a593Smuzhiyun 			/* Can't set port link state above '3' (U3) */
1319*4882a593Smuzhiyun 			if (link_state > USB_SS_PORT_LS_U3) {
1320*4882a593Smuzhiyun 				xhci_warn(xhci, "Cannot set port %d-%d link state %d\n",
1321*4882a593Smuzhiyun 					  hcd->self.busnum, wIndex + 1,
1322*4882a593Smuzhiyun 					  link_state);
1323*4882a593Smuzhiyun 				goto error;
1324*4882a593Smuzhiyun 			}
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 			/*
1327*4882a593Smuzhiyun 			 * set link to U0, steps depend on current link state.
1328*4882a593Smuzhiyun 			 * U3: set link to U0 and wait for u3exit completion.
1329*4882a593Smuzhiyun 			 * U1/U2:  no PLC complete event, only set link to U0.
1330*4882a593Smuzhiyun 			 * Resume/Recovery: device initiated U0, only wait for
1331*4882a593Smuzhiyun 			 * completion
1332*4882a593Smuzhiyun 			 */
1333*4882a593Smuzhiyun 			if (link_state == USB_SS_PORT_LS_U0) {
1334*4882a593Smuzhiyun 				u32 pls = temp & PORT_PLS_MASK;
1335*4882a593Smuzhiyun 				bool wait_u0 = false;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 				/* already in U0 */
1338*4882a593Smuzhiyun 				if (pls == XDEV_U0)
1339*4882a593Smuzhiyun 					break;
1340*4882a593Smuzhiyun 				if (pls == XDEV_U3 ||
1341*4882a593Smuzhiyun 				    pls == XDEV_RESUME ||
1342*4882a593Smuzhiyun 				    pls == XDEV_RECOVERY) {
1343*4882a593Smuzhiyun 					wait_u0 = true;
1344*4882a593Smuzhiyun 					reinit_completion(&bus_state->u3exit_done[wIndex]);
1345*4882a593Smuzhiyun 				}
1346*4882a593Smuzhiyun 				if (pls <= XDEV_U3) /* U1, U2, U3 */
1347*4882a593Smuzhiyun 					xhci_set_link_state(xhci, ports[wIndex],
1348*4882a593Smuzhiyun 							    USB_SS_PORT_LS_U0);
1349*4882a593Smuzhiyun 				if (!wait_u0) {
1350*4882a593Smuzhiyun 					if (pls > XDEV_U3)
1351*4882a593Smuzhiyun 						goto error;
1352*4882a593Smuzhiyun 					break;
1353*4882a593Smuzhiyun 				}
1354*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1355*4882a593Smuzhiyun 				if (!wait_for_completion_timeout(&bus_state->u3exit_done[wIndex],
1356*4882a593Smuzhiyun 								 msecs_to_jiffies(500)))
1357*4882a593Smuzhiyun 					xhci_dbg(xhci, "missing U0 port change event for port %d-%d\n",
1358*4882a593Smuzhiyun 						 hcd->self.busnum, wIndex + 1);
1359*4882a593Smuzhiyun 				spin_lock_irqsave(&xhci->lock, flags);
1360*4882a593Smuzhiyun 				temp = readl(ports[wIndex]->addr);
1361*4882a593Smuzhiyun 				break;
1362*4882a593Smuzhiyun 			}
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 			if (link_state == USB_SS_PORT_LS_U3) {
1365*4882a593Smuzhiyun 				int retries = 16;
1366*4882a593Smuzhiyun 				slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1367*4882a593Smuzhiyun 						wIndex + 1);
1368*4882a593Smuzhiyun 				if (slot_id) {
1369*4882a593Smuzhiyun 					/* unlock to execute stop endpoint
1370*4882a593Smuzhiyun 					 * commands */
1371*4882a593Smuzhiyun 					spin_unlock_irqrestore(&xhci->lock,
1372*4882a593Smuzhiyun 								flags);
1373*4882a593Smuzhiyun 					xhci_stop_device(xhci, slot_id, 1);
1374*4882a593Smuzhiyun 					spin_lock_irqsave(&xhci->lock, flags);
1375*4882a593Smuzhiyun 				}
1376*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex], USB_SS_PORT_LS_U3);
1377*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1378*4882a593Smuzhiyun 				while (retries--) {
1379*4882a593Smuzhiyun 					usleep_range(4000, 8000);
1380*4882a593Smuzhiyun 					temp = readl(ports[wIndex]->addr);
1381*4882a593Smuzhiyun 					if ((temp & PORT_PLS_MASK) == XDEV_U3)
1382*4882a593Smuzhiyun 						break;
1383*4882a593Smuzhiyun 				}
1384*4882a593Smuzhiyun 				spin_lock_irqsave(&xhci->lock, flags);
1385*4882a593Smuzhiyun 				temp = readl(ports[wIndex]->addr);
1386*4882a593Smuzhiyun 				bus_state->suspended_ports |= 1 << wIndex;
1387*4882a593Smuzhiyun 			}
1388*4882a593Smuzhiyun 			break;
1389*4882a593Smuzhiyun 		case USB_PORT_FEAT_POWER:
1390*4882a593Smuzhiyun 			/*
1391*4882a593Smuzhiyun 			 * Turn on ports, even if there isn't per-port switching.
1392*4882a593Smuzhiyun 			 * HC will report connect events even before this is set.
1393*4882a593Smuzhiyun 			 * However, hub_wq will ignore the roothub events until
1394*4882a593Smuzhiyun 			 * the roothub is registered.
1395*4882a593Smuzhiyun 			 */
1396*4882a593Smuzhiyun 			xhci_set_port_power(xhci, hcd, wIndex, true, &flags);
1397*4882a593Smuzhiyun 			break;
1398*4882a593Smuzhiyun 		case USB_PORT_FEAT_RESET:
1399*4882a593Smuzhiyun 			temp = (temp | PORT_RESET);
1400*4882a593Smuzhiyun 			writel(temp, ports[wIndex]->addr);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1403*4882a593Smuzhiyun 			xhci_dbg(xhci, "set port reset, actual port %d-%d status  = 0x%x\n",
1404*4882a593Smuzhiyun 				 hcd->self.busnum, wIndex + 1, temp);
1405*4882a593Smuzhiyun 			break;
1406*4882a593Smuzhiyun 		case USB_PORT_FEAT_REMOTE_WAKE_MASK:
1407*4882a593Smuzhiyun 			xhci_set_remote_wake_mask(xhci, ports[wIndex],
1408*4882a593Smuzhiyun 						  wake_mask);
1409*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1410*4882a593Smuzhiyun 			xhci_dbg(xhci, "set port remote wake mask, actual port %d-%d status  = 0x%x\n",
1411*4882a593Smuzhiyun 				 hcd->self.busnum, wIndex + 1, temp);
1412*4882a593Smuzhiyun 			break;
1413*4882a593Smuzhiyun 		case USB_PORT_FEAT_BH_PORT_RESET:
1414*4882a593Smuzhiyun 			temp |= PORT_WR;
1415*4882a593Smuzhiyun 			writel(temp, ports[wIndex]->addr);
1416*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1417*4882a593Smuzhiyun 			break;
1418*4882a593Smuzhiyun 		case USB_PORT_FEAT_U1_TIMEOUT:
1419*4882a593Smuzhiyun 			if (hcd->speed < HCD_USB3)
1420*4882a593Smuzhiyun 				goto error;
1421*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr + PORTPMSC);
1422*4882a593Smuzhiyun 			temp &= ~PORT_U1_TIMEOUT_MASK;
1423*4882a593Smuzhiyun 			temp |= PORT_U1_TIMEOUT(timeout);
1424*4882a593Smuzhiyun 			writel(temp, ports[wIndex]->addr + PORTPMSC);
1425*4882a593Smuzhiyun 			break;
1426*4882a593Smuzhiyun 		case USB_PORT_FEAT_U2_TIMEOUT:
1427*4882a593Smuzhiyun 			if (hcd->speed < HCD_USB3)
1428*4882a593Smuzhiyun 				goto error;
1429*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr + PORTPMSC);
1430*4882a593Smuzhiyun 			temp &= ~PORT_U2_TIMEOUT_MASK;
1431*4882a593Smuzhiyun 			temp |= PORT_U2_TIMEOUT(timeout);
1432*4882a593Smuzhiyun 			writel(temp, ports[wIndex]->addr + PORTPMSC);
1433*4882a593Smuzhiyun 			break;
1434*4882a593Smuzhiyun 		case USB_PORT_FEAT_TEST:
1435*4882a593Smuzhiyun 			/* 4.19.6 Port Test Modes (USB2 Test Mode) */
1436*4882a593Smuzhiyun 			if (hcd->speed != HCD_USB2)
1437*4882a593Smuzhiyun 				goto error;
1438*4882a593Smuzhiyun 			if (test_mode > USB_TEST_FORCE_ENABLE ||
1439*4882a593Smuzhiyun 			    test_mode < USB_TEST_J)
1440*4882a593Smuzhiyun 				goto error;
1441*4882a593Smuzhiyun 			retval = xhci_enter_test_mode(xhci, test_mode, wIndex,
1442*4882a593Smuzhiyun 						      &flags);
1443*4882a593Smuzhiyun 			break;
1444*4882a593Smuzhiyun 		default:
1445*4882a593Smuzhiyun 			goto error;
1446*4882a593Smuzhiyun 		}
1447*4882a593Smuzhiyun 		/* unblock any posted writes */
1448*4882a593Smuzhiyun 		temp = readl(ports[wIndex]->addr);
1449*4882a593Smuzhiyun 		break;
1450*4882a593Smuzhiyun 	case ClearPortFeature:
1451*4882a593Smuzhiyun 		if (!wIndex || wIndex > max_ports)
1452*4882a593Smuzhiyun 			goto error;
1453*4882a593Smuzhiyun 		wIndex--;
1454*4882a593Smuzhiyun 		temp = readl(ports[wIndex]->addr);
1455*4882a593Smuzhiyun 		if (temp == ~(u32)0) {
1456*4882a593Smuzhiyun 			xhci_hc_died(xhci);
1457*4882a593Smuzhiyun 			retval = -ENODEV;
1458*4882a593Smuzhiyun 			break;
1459*4882a593Smuzhiyun 		}
1460*4882a593Smuzhiyun 		/* FIXME: What new port features do we need to support? */
1461*4882a593Smuzhiyun 		temp = xhci_port_state_to_neutral(temp);
1462*4882a593Smuzhiyun 		switch (wValue) {
1463*4882a593Smuzhiyun 		case USB_PORT_FEAT_SUSPEND:
1464*4882a593Smuzhiyun 			temp = readl(ports[wIndex]->addr);
1465*4882a593Smuzhiyun 			xhci_dbg(xhci, "clear USB_PORT_FEAT_SUSPEND\n");
1466*4882a593Smuzhiyun 			xhci_dbg(xhci, "PORTSC %04x\n", temp);
1467*4882a593Smuzhiyun 			if (temp & PORT_RESET)
1468*4882a593Smuzhiyun 				goto error;
1469*4882a593Smuzhiyun 			if ((temp & PORT_PLS_MASK) == XDEV_U3) {
1470*4882a593Smuzhiyun 				if ((temp & PORT_PE) == 0)
1471*4882a593Smuzhiyun 					goto error;
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 				set_bit(wIndex, &bus_state->resuming_ports);
1474*4882a593Smuzhiyun 				usb_hcd_start_port_resume(&hcd->self, wIndex);
1475*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex],
1476*4882a593Smuzhiyun 						    XDEV_RESUME);
1477*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1478*4882a593Smuzhiyun 				msleep(USB_RESUME_TIMEOUT);
1479*4882a593Smuzhiyun 				spin_lock_irqsave(&xhci->lock, flags);
1480*4882a593Smuzhiyun 				xhci_set_link_state(xhci, ports[wIndex],
1481*4882a593Smuzhiyun 							XDEV_U0);
1482*4882a593Smuzhiyun 				clear_bit(wIndex, &bus_state->resuming_ports);
1483*4882a593Smuzhiyun 				usb_hcd_end_port_resume(&hcd->self, wIndex);
1484*4882a593Smuzhiyun 			}
1485*4882a593Smuzhiyun 			bus_state->port_c_suspend |= 1 << wIndex;
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 			slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1488*4882a593Smuzhiyun 					wIndex + 1);
1489*4882a593Smuzhiyun 			if (!slot_id) {
1490*4882a593Smuzhiyun 				xhci_dbg(xhci, "slot_id is zero\n");
1491*4882a593Smuzhiyun 				goto error;
1492*4882a593Smuzhiyun 			}
1493*4882a593Smuzhiyun 			xhci_ring_device(xhci, slot_id);
1494*4882a593Smuzhiyun 			break;
1495*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_SUSPEND:
1496*4882a593Smuzhiyun 			bus_state->port_c_suspend &= ~(1 << wIndex);
1497*4882a593Smuzhiyun 			fallthrough;
1498*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_RESET:
1499*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_BH_PORT_RESET:
1500*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_CONNECTION:
1501*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_OVER_CURRENT:
1502*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_ENABLE:
1503*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_PORT_LINK_STATE:
1504*4882a593Smuzhiyun 		case USB_PORT_FEAT_C_PORT_CONFIG_ERROR:
1505*4882a593Smuzhiyun 			xhci_clear_port_change_bit(xhci, wValue, wIndex,
1506*4882a593Smuzhiyun 					ports[wIndex]->addr, temp);
1507*4882a593Smuzhiyun 			break;
1508*4882a593Smuzhiyun 		case USB_PORT_FEAT_ENABLE:
1509*4882a593Smuzhiyun 			xhci_disable_port(hcd, xhci, wIndex,
1510*4882a593Smuzhiyun 					ports[wIndex]->addr, temp);
1511*4882a593Smuzhiyun 			break;
1512*4882a593Smuzhiyun 		case USB_PORT_FEAT_POWER:
1513*4882a593Smuzhiyun 			xhci_set_port_power(xhci, hcd, wIndex, false, &flags);
1514*4882a593Smuzhiyun 			break;
1515*4882a593Smuzhiyun 		case USB_PORT_FEAT_TEST:
1516*4882a593Smuzhiyun 			retval = xhci_exit_test_mode(xhci);
1517*4882a593Smuzhiyun 			break;
1518*4882a593Smuzhiyun 		default:
1519*4882a593Smuzhiyun 			goto error;
1520*4882a593Smuzhiyun 		}
1521*4882a593Smuzhiyun 		break;
1522*4882a593Smuzhiyun 	default:
1523*4882a593Smuzhiyun error:
1524*4882a593Smuzhiyun 		/* "stall" on error */
1525*4882a593Smuzhiyun 		retval = -EPIPE;
1526*4882a593Smuzhiyun 	}
1527*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, flags);
1528*4882a593Smuzhiyun 	return retval;
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun /*
1532*4882a593Smuzhiyun  * Returns 0 if the status hasn't changed, or the number of bytes in buf.
1533*4882a593Smuzhiyun  * Ports are 0-indexed from the HCD point of view,
1534*4882a593Smuzhiyun  * and 1-indexed from the USB core pointer of view.
1535*4882a593Smuzhiyun  *
1536*4882a593Smuzhiyun  * Note that the status change bits will be cleared as soon as a port status
1537*4882a593Smuzhiyun  * change event is generated, so we use the saved status from that event.
1538*4882a593Smuzhiyun  */
xhci_hub_status_data(struct usb_hcd * hcd,char * buf)1539*4882a593Smuzhiyun int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun 	unsigned long flags;
1542*4882a593Smuzhiyun 	u32 temp, status;
1543*4882a593Smuzhiyun 	u32 mask;
1544*4882a593Smuzhiyun 	int i, retval;
1545*4882a593Smuzhiyun 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
1546*4882a593Smuzhiyun 	int max_ports;
1547*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
1548*4882a593Smuzhiyun 	bool reset_change = false;
1549*4882a593Smuzhiyun 	struct xhci_hub *rhub;
1550*4882a593Smuzhiyun 	struct xhci_port **ports;
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
1553*4882a593Smuzhiyun 	ports = rhub->ports;
1554*4882a593Smuzhiyun 	max_ports = rhub->num_ports;
1555*4882a593Smuzhiyun 	bus_state = &rhub->bus_state;
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	/* Initial status is no changes */
1558*4882a593Smuzhiyun 	retval = (max_ports + 8) / 8;
1559*4882a593Smuzhiyun 	memset(buf, 0, retval);
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	/*
1562*4882a593Smuzhiyun 	 * Inform the usbcore about resume-in-progress by returning
1563*4882a593Smuzhiyun 	 * a non-zero value even if there are no status changes.
1564*4882a593Smuzhiyun 	 */
1565*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, flags);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	status = bus_state->resuming_ports;
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun 	/* For each port, did anything change?  If so, set that bit in buf. */
1572*4882a593Smuzhiyun 	for (i = 0; i < max_ports; i++) {
1573*4882a593Smuzhiyun 		temp = readl(ports[i]->addr);
1574*4882a593Smuzhiyun 		if (temp == ~(u32)0) {
1575*4882a593Smuzhiyun 			xhci_hc_died(xhci);
1576*4882a593Smuzhiyun 			retval = -ENODEV;
1577*4882a593Smuzhiyun 			break;
1578*4882a593Smuzhiyun 		}
1579*4882a593Smuzhiyun 		trace_xhci_hub_status_data(i, temp);
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun 		if ((temp & mask) != 0 ||
1582*4882a593Smuzhiyun 			(bus_state->port_c_suspend & 1 << i) ||
1583*4882a593Smuzhiyun 			(bus_state->resume_done[i] && time_after_eq(
1584*4882a593Smuzhiyun 			    jiffies, bus_state->resume_done[i]))) {
1585*4882a593Smuzhiyun 			buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
1586*4882a593Smuzhiyun 			status = 1;
1587*4882a593Smuzhiyun 		}
1588*4882a593Smuzhiyun 		if ((temp & PORT_RC))
1589*4882a593Smuzhiyun 			reset_change = true;
1590*4882a593Smuzhiyun 		if (temp & PORT_OC)
1591*4882a593Smuzhiyun 			status = 1;
1592*4882a593Smuzhiyun 	}
1593*4882a593Smuzhiyun 	if (!status && !reset_change) {
1594*4882a593Smuzhiyun 		xhci_dbg(xhci, "%s: stopping usb%d port polling\n",
1595*4882a593Smuzhiyun 			 __func__, hcd->self.busnum);
1596*4882a593Smuzhiyun 		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1597*4882a593Smuzhiyun 	}
1598*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, flags);
1599*4882a593Smuzhiyun 	return status ? retval : 0;
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun #ifdef CONFIG_PM
1603*4882a593Smuzhiyun 
xhci_bus_suspend(struct usb_hcd * hcd)1604*4882a593Smuzhiyun int xhci_bus_suspend(struct usb_hcd *hcd)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
1607*4882a593Smuzhiyun 	int max_ports, port_index;
1608*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
1609*4882a593Smuzhiyun 	unsigned long flags;
1610*4882a593Smuzhiyun 	struct xhci_hub *rhub;
1611*4882a593Smuzhiyun 	struct xhci_port **ports;
1612*4882a593Smuzhiyun 	u32 portsc_buf[USB_MAXCHILDREN];
1613*4882a593Smuzhiyun 	bool wake_enabled;
1614*4882a593Smuzhiyun 
1615*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
1616*4882a593Smuzhiyun 	ports = rhub->ports;
1617*4882a593Smuzhiyun 	max_ports = rhub->num_ports;
1618*4882a593Smuzhiyun 	bus_state = &rhub->bus_state;
1619*4882a593Smuzhiyun 	wake_enabled = hcd->self.root_hub->do_remote_wakeup;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, flags);
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	if (wake_enabled) {
1624*4882a593Smuzhiyun 		if (bus_state->resuming_ports ||	/* USB2 */
1625*4882a593Smuzhiyun 		    bus_state->port_remote_wakeup) {	/* USB3 */
1626*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1627*4882a593Smuzhiyun 			xhci_dbg(xhci, "usb%d bus suspend to fail because a port is resuming\n",
1628*4882a593Smuzhiyun 				 hcd->self.busnum);
1629*4882a593Smuzhiyun 			return -EBUSY;
1630*4882a593Smuzhiyun 		}
1631*4882a593Smuzhiyun 	}
1632*4882a593Smuzhiyun 	/*
1633*4882a593Smuzhiyun 	 * Prepare ports for suspend, but don't write anything before all ports
1634*4882a593Smuzhiyun 	 * are checked and we know bus suspend can proceed
1635*4882a593Smuzhiyun 	 */
1636*4882a593Smuzhiyun 	bus_state->bus_suspended = 0;
1637*4882a593Smuzhiyun 	port_index = max_ports;
1638*4882a593Smuzhiyun 	while (port_index--) {
1639*4882a593Smuzhiyun 		u32 t1, t2;
1640*4882a593Smuzhiyun 		int retries = 10;
1641*4882a593Smuzhiyun retry:
1642*4882a593Smuzhiyun 		t1 = readl(ports[port_index]->addr);
1643*4882a593Smuzhiyun 		t2 = xhci_port_state_to_neutral(t1);
1644*4882a593Smuzhiyun 		portsc_buf[port_index] = 0;
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 		/*
1647*4882a593Smuzhiyun 		 * Give a USB3 port in link training time to finish, but don't
1648*4882a593Smuzhiyun 		 * prevent suspend as port might be stuck
1649*4882a593Smuzhiyun 		 */
1650*4882a593Smuzhiyun 		if ((hcd->speed >= HCD_USB3) && retries-- &&
1651*4882a593Smuzhiyun 		    (t1 & PORT_PLS_MASK) == XDEV_POLLING) {
1652*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1653*4882a593Smuzhiyun 			msleep(XHCI_PORT_POLLING_LFPS_TIME);
1654*4882a593Smuzhiyun 			spin_lock_irqsave(&xhci->lock, flags);
1655*4882a593Smuzhiyun 			xhci_dbg(xhci, "port %d-%d polling in bus suspend, waiting\n",
1656*4882a593Smuzhiyun 				 hcd->self.busnum, port_index + 1);
1657*4882a593Smuzhiyun 			goto retry;
1658*4882a593Smuzhiyun 		}
1659*4882a593Smuzhiyun 		/* bail out if port detected a over-current condition */
1660*4882a593Smuzhiyun 		if (t1 & PORT_OC) {
1661*4882a593Smuzhiyun 			bus_state->bus_suspended = 0;
1662*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1663*4882a593Smuzhiyun 			xhci_dbg(xhci, "Bus suspend bailout, port over-current detected\n");
1664*4882a593Smuzhiyun 			return -EBUSY;
1665*4882a593Smuzhiyun 		}
1666*4882a593Smuzhiyun 		/* suspend ports in U0, or bail out for new connect changes */
1667*4882a593Smuzhiyun 		if ((t1 & PORT_PE) && (t1 & PORT_PLS_MASK) == XDEV_U0) {
1668*4882a593Smuzhiyun 			if ((t1 & PORT_CSC) && wake_enabled) {
1669*4882a593Smuzhiyun 				bus_state->bus_suspended = 0;
1670*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1671*4882a593Smuzhiyun 				xhci_dbg(xhci, "Bus suspend bailout, port connect change\n");
1672*4882a593Smuzhiyun 				return -EBUSY;
1673*4882a593Smuzhiyun 			}
1674*4882a593Smuzhiyun 			xhci_dbg(xhci, "port %d-%d not suspended\n",
1675*4882a593Smuzhiyun 				 hcd->self.busnum, port_index + 1);
1676*4882a593Smuzhiyun 			t2 &= ~PORT_PLS_MASK;
1677*4882a593Smuzhiyun 			t2 |= PORT_LINK_STROBE | XDEV_U3;
1678*4882a593Smuzhiyun 			set_bit(port_index, &bus_state->bus_suspended);
1679*4882a593Smuzhiyun 		} else if ((xhci->quirks & XHCI_U2_BROKEN_SUSPEND) &&
1680*4882a593Smuzhiyun 			   (hcd->speed < HCD_USB3) &&
1681*4882a593Smuzhiyun 			   (t1 & PORT_PLS_MASK) == XDEV_U3) {
1682*4882a593Smuzhiyun 			/*
1683*4882a593Smuzhiyun 			 * Rockchip SNPS xHC 3.0 set USB 2.0 PHY enter
1684*4882a593Smuzhiyun 			 * suspend mode from DWC3 core if the suspend
1685*4882a593Smuzhiyun 			 * conditions are valid. In this case, it need
1686*4882a593Smuzhiyun 			 * to set the bus_suspended bit for USB 2.0, so
1687*4882a593Smuzhiyun 			 * that in xhci_bus_resume, it can set the xHC
1688*4882a593Smuzhiyun 			 * link state to XDEV_RESUME and send USB resume
1689*4882a593Smuzhiyun 			 * signal to USB 2.0 device.
1690*4882a593Smuzhiyun 			 */
1691*4882a593Smuzhiyun 			set_bit(port_index, &bus_state->bus_suspended);
1692*4882a593Smuzhiyun 		}
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 		/* USB core sets remote wake mask for USB 3.0 hubs,
1695*4882a593Smuzhiyun 		 * including the USB 3.0 roothub, but only if CONFIG_PM
1696*4882a593Smuzhiyun 		 * is enabled, so also enable remote wake here.
1697*4882a593Smuzhiyun 		 */
1698*4882a593Smuzhiyun 		if (wake_enabled) {
1699*4882a593Smuzhiyun 			if (t1 & PORT_CONNECT) {
1700*4882a593Smuzhiyun 				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
1701*4882a593Smuzhiyun 				t2 &= ~PORT_WKCONN_E;
1702*4882a593Smuzhiyun 			} else {
1703*4882a593Smuzhiyun 				t2 |= PORT_WKOC_E | PORT_WKCONN_E;
1704*4882a593Smuzhiyun 				t2 &= ~PORT_WKDISC_E;
1705*4882a593Smuzhiyun 			}
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 			if ((xhci->quirks & XHCI_U2_DISABLE_WAKE) &&
1708*4882a593Smuzhiyun 			    (hcd->speed < HCD_USB3)) {
1709*4882a593Smuzhiyun 				if (usb_amd_pt_check_port(hcd->self.controller,
1710*4882a593Smuzhiyun 							  port_index))
1711*4882a593Smuzhiyun 					t2 &= ~PORT_WAKE_BITS;
1712*4882a593Smuzhiyun 			}
1713*4882a593Smuzhiyun 		} else
1714*4882a593Smuzhiyun 			t2 &= ~PORT_WAKE_BITS;
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 		t1 = xhci_port_state_to_neutral(t1);
1717*4882a593Smuzhiyun 		if (t1 != t2)
1718*4882a593Smuzhiyun 			portsc_buf[port_index] = t2;
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	/* write port settings, stopping and suspending ports if needed */
1722*4882a593Smuzhiyun 	port_index = max_ports;
1723*4882a593Smuzhiyun 	while (port_index--) {
1724*4882a593Smuzhiyun 		if (!portsc_buf[port_index])
1725*4882a593Smuzhiyun 			continue;
1726*4882a593Smuzhiyun 		if (test_bit(port_index, &bus_state->bus_suspended)) {
1727*4882a593Smuzhiyun 			int slot_id;
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 			slot_id = xhci_find_slot_id_by_port(hcd, xhci,
1730*4882a593Smuzhiyun 							    port_index + 1);
1731*4882a593Smuzhiyun 			if (slot_id) {
1732*4882a593Smuzhiyun 				spin_unlock_irqrestore(&xhci->lock, flags);
1733*4882a593Smuzhiyun 				xhci_stop_device(xhci, slot_id, 1);
1734*4882a593Smuzhiyun 				spin_lock_irqsave(&xhci->lock, flags);
1735*4882a593Smuzhiyun 			}
1736*4882a593Smuzhiyun 		}
1737*4882a593Smuzhiyun 		writel(portsc_buf[port_index], ports[port_index]->addr);
1738*4882a593Smuzhiyun 	}
1739*4882a593Smuzhiyun 	hcd->state = HC_STATE_SUSPENDED;
1740*4882a593Smuzhiyun 	bus_state->next_statechange = jiffies + msecs_to_jiffies(10);
1741*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, flags);
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	if (bus_state->bus_suspended)
1744*4882a593Smuzhiyun 		usleep_range(5000, 10000);
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	return 0;
1747*4882a593Smuzhiyun }
1748*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_bus_suspend);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun /*
1751*4882a593Smuzhiyun  * Workaround for missing Cold Attach Status (CAS) if device re-plugged in S3.
1752*4882a593Smuzhiyun  * warm reset a USB3 device stuck in polling or compliance mode after resume.
1753*4882a593Smuzhiyun  * See Intel 100/c230 series PCH specification update Doc #332692-006 Errata #8
1754*4882a593Smuzhiyun  */
xhci_port_missing_cas_quirk(struct xhci_port * port)1755*4882a593Smuzhiyun static bool xhci_port_missing_cas_quirk(struct xhci_port *port)
1756*4882a593Smuzhiyun {
1757*4882a593Smuzhiyun 	u32 portsc;
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	portsc = readl(port->addr);
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun 	/* if any of these are set we are not stuck */
1762*4882a593Smuzhiyun 	if (portsc & (PORT_CONNECT | PORT_CAS))
1763*4882a593Smuzhiyun 		return false;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	if (((portsc & PORT_PLS_MASK) != XDEV_POLLING) &&
1766*4882a593Smuzhiyun 	    ((portsc & PORT_PLS_MASK) != XDEV_COMP_MODE))
1767*4882a593Smuzhiyun 		return false;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	/* clear wakeup/change bits, and do a warm port reset */
1770*4882a593Smuzhiyun 	portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
1771*4882a593Smuzhiyun 	portsc |= PORT_WR;
1772*4882a593Smuzhiyun 	writel(portsc, port->addr);
1773*4882a593Smuzhiyun 	/* flush write */
1774*4882a593Smuzhiyun 	readl(port->addr);
1775*4882a593Smuzhiyun 	return true;
1776*4882a593Smuzhiyun }
1777*4882a593Smuzhiyun 
xhci_bus_resume(struct usb_hcd * hcd)1778*4882a593Smuzhiyun int xhci_bus_resume(struct usb_hcd *hcd)
1779*4882a593Smuzhiyun {
1780*4882a593Smuzhiyun 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
1781*4882a593Smuzhiyun 	struct xhci_bus_state *bus_state;
1782*4882a593Smuzhiyun 	unsigned long flags;
1783*4882a593Smuzhiyun 	int max_ports, port_index;
1784*4882a593Smuzhiyun 	int slot_id;
1785*4882a593Smuzhiyun 	int sret;
1786*4882a593Smuzhiyun 	u32 next_state;
1787*4882a593Smuzhiyun 	u32 temp, portsc;
1788*4882a593Smuzhiyun 	struct xhci_hub *rhub;
1789*4882a593Smuzhiyun 	struct xhci_port **ports;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	rhub = xhci_get_rhub(hcd);
1792*4882a593Smuzhiyun 	ports = rhub->ports;
1793*4882a593Smuzhiyun 	max_ports = rhub->num_ports;
1794*4882a593Smuzhiyun 	bus_state = &rhub->bus_state;
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	if (time_before(jiffies, bus_state->next_statechange))
1797*4882a593Smuzhiyun 		msleep(5);
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	spin_lock_irqsave(&xhci->lock, flags);
1800*4882a593Smuzhiyun 	if (!HCD_HW_ACCESSIBLE(hcd)) {
1801*4882a593Smuzhiyun 		spin_unlock_irqrestore(&xhci->lock, flags);
1802*4882a593Smuzhiyun 		return -ESHUTDOWN;
1803*4882a593Smuzhiyun 	}
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun 	/* delay the irqs */
1806*4882a593Smuzhiyun 	temp = readl(&xhci->op_regs->command);
1807*4882a593Smuzhiyun 	temp &= ~CMD_EIE;
1808*4882a593Smuzhiyun 	writel(temp, &xhci->op_regs->command);
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun 	/* bus specific resume for ports we suspended at bus_suspend */
1811*4882a593Smuzhiyun 	if (hcd->speed >= HCD_USB3)
1812*4882a593Smuzhiyun 		next_state = XDEV_U0;
1813*4882a593Smuzhiyun 	else
1814*4882a593Smuzhiyun 		next_state = XDEV_RESUME;
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	port_index = max_ports;
1817*4882a593Smuzhiyun 	while (port_index--) {
1818*4882a593Smuzhiyun 		portsc = readl(ports[port_index]->addr);
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 		/* warm reset CAS limited ports stuck in polling/compliance */
1821*4882a593Smuzhiyun 		if ((xhci->quirks & XHCI_MISSING_CAS) &&
1822*4882a593Smuzhiyun 		    (hcd->speed >= HCD_USB3) &&
1823*4882a593Smuzhiyun 		    xhci_port_missing_cas_quirk(ports[port_index])) {
1824*4882a593Smuzhiyun 			xhci_dbg(xhci, "reset stuck port %d-%d\n",
1825*4882a593Smuzhiyun 				 hcd->self.busnum, port_index + 1);
1826*4882a593Smuzhiyun 			clear_bit(port_index, &bus_state->bus_suspended);
1827*4882a593Smuzhiyun 			continue;
1828*4882a593Smuzhiyun 		}
1829*4882a593Smuzhiyun 		/* resume if we suspended the link, and it is still suspended */
1830*4882a593Smuzhiyun 		if (test_bit(port_index, &bus_state->bus_suspended))
1831*4882a593Smuzhiyun 			switch (portsc & PORT_PLS_MASK) {
1832*4882a593Smuzhiyun 			case XDEV_U3:
1833*4882a593Smuzhiyun 				portsc = xhci_port_state_to_neutral(portsc);
1834*4882a593Smuzhiyun 				portsc &= ~PORT_PLS_MASK;
1835*4882a593Smuzhiyun 				portsc |= PORT_LINK_STROBE | next_state;
1836*4882a593Smuzhiyun 				break;
1837*4882a593Smuzhiyun 			case XDEV_RESUME:
1838*4882a593Smuzhiyun 				/* resume already initiated */
1839*4882a593Smuzhiyun 				break;
1840*4882a593Smuzhiyun 			default:
1841*4882a593Smuzhiyun 				/* not in a resumeable state, ignore it */
1842*4882a593Smuzhiyun 				clear_bit(port_index,
1843*4882a593Smuzhiyun 					  &bus_state->bus_suspended);
1844*4882a593Smuzhiyun 				break;
1845*4882a593Smuzhiyun 			}
1846*4882a593Smuzhiyun 		/* disable wake for all ports, write new link state if needed */
1847*4882a593Smuzhiyun 		portsc &= ~(PORT_RWC_BITS | PORT_CEC | PORT_WAKE_BITS);
1848*4882a593Smuzhiyun 		writel(portsc, ports[port_index]->addr);
1849*4882a593Smuzhiyun 	}
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	/* USB2 specific resume signaling delay and U0 link state transition */
1852*4882a593Smuzhiyun 	if (hcd->speed < HCD_USB3) {
1853*4882a593Smuzhiyun 		if (bus_state->bus_suspended) {
1854*4882a593Smuzhiyun 			spin_unlock_irqrestore(&xhci->lock, flags);
1855*4882a593Smuzhiyun 			msleep(USB_RESUME_TIMEOUT);
1856*4882a593Smuzhiyun 			spin_lock_irqsave(&xhci->lock, flags);
1857*4882a593Smuzhiyun 		}
1858*4882a593Smuzhiyun 		for_each_set_bit(port_index, &bus_state->bus_suspended,
1859*4882a593Smuzhiyun 				 BITS_PER_LONG) {
1860*4882a593Smuzhiyun 			/* Clear PLC to poll it later for U0 transition */
1861*4882a593Smuzhiyun 			xhci_test_and_clear_bit(xhci, ports[port_index],
1862*4882a593Smuzhiyun 						PORT_PLC);
1863*4882a593Smuzhiyun 			xhci_set_link_state(xhci, ports[port_index], XDEV_U0);
1864*4882a593Smuzhiyun 		}
1865*4882a593Smuzhiyun 	}
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	/* poll for U0 link state complete, both USB2 and USB3 */
1868*4882a593Smuzhiyun 	for_each_set_bit(port_index, &bus_state->bus_suspended, BITS_PER_LONG) {
1869*4882a593Smuzhiyun 		sret = xhci_handshake(ports[port_index]->addr, PORT_PLC,
1870*4882a593Smuzhiyun 				      PORT_PLC, 10 * 1000);
1871*4882a593Smuzhiyun 		if (sret) {
1872*4882a593Smuzhiyun 			xhci_warn(xhci, "port %d-%d resume PLC timeout\n",
1873*4882a593Smuzhiyun 				  hcd->self.busnum, port_index + 1);
1874*4882a593Smuzhiyun 			continue;
1875*4882a593Smuzhiyun 		}
1876*4882a593Smuzhiyun 		xhci_test_and_clear_bit(xhci, ports[port_index], PORT_PLC);
1877*4882a593Smuzhiyun 		slot_id = xhci_find_slot_id_by_port(hcd, xhci, port_index + 1);
1878*4882a593Smuzhiyun 		if (slot_id)
1879*4882a593Smuzhiyun 			xhci_ring_device(xhci, slot_id);
1880*4882a593Smuzhiyun 	}
1881*4882a593Smuzhiyun 	(void) readl(&xhci->op_regs->command);
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	bus_state->next_statechange = jiffies + msecs_to_jiffies(5);
1884*4882a593Smuzhiyun 	/* re-enable irqs */
1885*4882a593Smuzhiyun 	temp = readl(&xhci->op_regs->command);
1886*4882a593Smuzhiyun 	temp |= CMD_EIE;
1887*4882a593Smuzhiyun 	writel(temp, &xhci->op_regs->command);
1888*4882a593Smuzhiyun 	temp = readl(&xhci->op_regs->command);
1889*4882a593Smuzhiyun 
1890*4882a593Smuzhiyun 	spin_unlock_irqrestore(&xhci->lock, flags);
1891*4882a593Smuzhiyun 	return 0;
1892*4882a593Smuzhiyun }
1893*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_bus_resume);
1894*4882a593Smuzhiyun 
xhci_get_resuming_ports(struct usb_hcd * hcd)1895*4882a593Smuzhiyun unsigned long xhci_get_resuming_ports(struct usb_hcd *hcd)
1896*4882a593Smuzhiyun {
1897*4882a593Smuzhiyun 	struct xhci_hub *rhub = xhci_get_rhub(hcd);
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	/* USB3 port wakeups are reported via usb_wakeup_notification() */
1900*4882a593Smuzhiyun 	return rhub->bus_state.resuming_ports;	/* USB2 ports only */
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun #endif	/* CONFIG_PM */
1904