1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * USB HOST XHCI Controller stack
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Based on xHCI host controller driver in linux-kernel
5*4882a593Smuzhiyun * by Sarah Sharp.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2008 Intel Corp.
8*4882a593Smuzhiyun * Author: Sarah Sharp
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Copyright (C) 2013 Samsung Electronics Co.Ltd
11*4882a593Smuzhiyun * Authors: Vivek Gautam <gautam.vivek@samsung.com>
12*4882a593Smuzhiyun * Vikas Sajjan <vikas.sajjan@samsung.com>
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /**
18*4882a593Smuzhiyun * This file gives the xhci stack for usb3.0 looking into
19*4882a593Smuzhiyun * xhci specification Rev1.0 (5/21/10).
20*4882a593Smuzhiyun * The quirk devices support hasn't been given yet.
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include <common.h>
24*4882a593Smuzhiyun #include <dm.h>
25*4882a593Smuzhiyun #include <asm/byteorder.h>
26*4882a593Smuzhiyun #include <usb.h>
27*4882a593Smuzhiyun #include <malloc.h>
28*4882a593Smuzhiyun #include <watchdog.h>
29*4882a593Smuzhiyun #include <asm/cache.h>
30*4882a593Smuzhiyun #include <asm/unaligned.h>
31*4882a593Smuzhiyun #include <linux/errno.h>
32*4882a593Smuzhiyun #include <usb/xhci.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
35*4882a593Smuzhiyun #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static struct descriptor {
39*4882a593Smuzhiyun struct usb_hub_descriptor hub;
40*4882a593Smuzhiyun struct usb_device_descriptor device;
41*4882a593Smuzhiyun struct usb_config_descriptor config;
42*4882a593Smuzhiyun struct usb_interface_descriptor interface;
43*4882a593Smuzhiyun struct usb_endpoint_descriptor endpoint;
44*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor ep_companion;
45*4882a593Smuzhiyun } __attribute__ ((packed)) descriptor = {
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 0xc, /* bDescLength */
48*4882a593Smuzhiyun 0x2a, /* bDescriptorType: hub descriptor */
49*4882a593Smuzhiyun 2, /* bNrPorts -- runtime modified */
50*4882a593Smuzhiyun cpu_to_le16(0x8), /* wHubCharacteristics */
51*4882a593Smuzhiyun 10, /* bPwrOn2PwrGood */
52*4882a593Smuzhiyun 0, /* bHubCntrCurrent */
53*4882a593Smuzhiyun { /* Device removable */
54*4882a593Smuzhiyun } /* at most 7 ports! XXX */
55*4882a593Smuzhiyun },
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 0x12, /* bLength */
58*4882a593Smuzhiyun 1, /* bDescriptorType: UDESC_DEVICE */
59*4882a593Smuzhiyun cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
60*4882a593Smuzhiyun 9, /* bDeviceClass: UDCLASS_HUB */
61*4882a593Smuzhiyun 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
62*4882a593Smuzhiyun 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
63*4882a593Smuzhiyun 9, /* bMaxPacketSize: 512 bytes 2^9 */
64*4882a593Smuzhiyun 0x0000, /* idVendor */
65*4882a593Smuzhiyun 0x0000, /* idProduct */
66*4882a593Smuzhiyun cpu_to_le16(0x0100), /* bcdDevice */
67*4882a593Smuzhiyun 1, /* iManufacturer */
68*4882a593Smuzhiyun 2, /* iProduct */
69*4882a593Smuzhiyun 0, /* iSerialNumber */
70*4882a593Smuzhiyun 1 /* bNumConfigurations: 1 */
71*4882a593Smuzhiyun },
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 0x9,
74*4882a593Smuzhiyun 2, /* bDescriptorType: UDESC_CONFIG */
75*4882a593Smuzhiyun cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
76*4882a593Smuzhiyun 1, /* bNumInterface */
77*4882a593Smuzhiyun 1, /* bConfigurationValue */
78*4882a593Smuzhiyun 0, /* iConfiguration */
79*4882a593Smuzhiyun 0x40, /* bmAttributes: UC_SELF_POWER */
80*4882a593Smuzhiyun 0 /* bMaxPower */
81*4882a593Smuzhiyun },
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 0x9, /* bLength */
84*4882a593Smuzhiyun 4, /* bDescriptorType: UDESC_INTERFACE */
85*4882a593Smuzhiyun 0, /* bInterfaceNumber */
86*4882a593Smuzhiyun 0, /* bAlternateSetting */
87*4882a593Smuzhiyun 1, /* bNumEndpoints */
88*4882a593Smuzhiyun 9, /* bInterfaceClass: UICLASS_HUB */
89*4882a593Smuzhiyun 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
90*4882a593Smuzhiyun 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
91*4882a593Smuzhiyun 0 /* iInterface */
92*4882a593Smuzhiyun },
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 0x7, /* bLength */
95*4882a593Smuzhiyun 5, /* bDescriptorType: UDESC_ENDPOINT */
96*4882a593Smuzhiyun 0x81, /* bEndpointAddress: IN endpoint 1 */
97*4882a593Smuzhiyun 3, /* bmAttributes: UE_INTERRUPT */
98*4882a593Smuzhiyun 8, /* wMaxPacketSize */
99*4882a593Smuzhiyun 255 /* bInterval */
100*4882a593Smuzhiyun },
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 0x06, /* ss_bLength */
103*4882a593Smuzhiyun 0x30, /* ss_bDescriptorType: SS EP Companion */
104*4882a593Smuzhiyun 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
105*4882a593Smuzhiyun /* ss_bmAttributes: 1 packet per service interval */
106*4882a593Smuzhiyun 0x00,
107*4882a593Smuzhiyun /* ss_wBytesPerInterval: 15 bits for max 15 ports */
108*4882a593Smuzhiyun cpu_to_le16(0x02),
109*4882a593Smuzhiyun },
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
113*4882a593Smuzhiyun static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun
xhci_get_ctrl(struct usb_device * udev)116*4882a593Smuzhiyun struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
119*4882a593Smuzhiyun struct udevice *dev;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Find the USB controller */
122*4882a593Smuzhiyun for (dev = udev->dev;
123*4882a593Smuzhiyun device_get_uclass_id(dev) != UCLASS_USB;
124*4882a593Smuzhiyun dev = dev->parent)
125*4882a593Smuzhiyun ;
126*4882a593Smuzhiyun return dev_get_priv(dev);
127*4882a593Smuzhiyun #else
128*4882a593Smuzhiyun return udev->controller;
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun * Waits for as per specified amount of time
134*4882a593Smuzhiyun * for the "result" to match with "done"
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * @param ptr pointer to the register to be read
137*4882a593Smuzhiyun * @param mask mask for the value read
138*4882a593Smuzhiyun * @param done value to be campared with result
139*4882a593Smuzhiyun * @param usec time to wait till
140*4882a593Smuzhiyun * @return 0 if handshake is success else < 0 on failure
141*4882a593Smuzhiyun */
handshake(uint32_t volatile * ptr,uint32_t mask,uint32_t done,int usec)142*4882a593Smuzhiyun static int handshake(uint32_t volatile *ptr, uint32_t mask,
143*4882a593Smuzhiyun uint32_t done, int usec)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun uint32_t result;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun do {
148*4882a593Smuzhiyun result = xhci_readl(ptr);
149*4882a593Smuzhiyun if (result == ~(uint32_t)0)
150*4882a593Smuzhiyun return -ENODEV;
151*4882a593Smuzhiyun result &= mask;
152*4882a593Smuzhiyun if (result == done)
153*4882a593Smuzhiyun return 0;
154*4882a593Smuzhiyun usec--;
155*4882a593Smuzhiyun udelay(1);
156*4882a593Smuzhiyun } while (usec > 0);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return -ETIMEDOUT;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun * Set the run bit and wait for the host to be running.
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * @param hcor pointer to host controller operation registers
165*4882a593Smuzhiyun * @return status of the Handshake
166*4882a593Smuzhiyun */
xhci_start(struct xhci_hcor * hcor)167*4882a593Smuzhiyun static int xhci_start(struct xhci_hcor *hcor)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun u32 temp;
170*4882a593Smuzhiyun int ret;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun puts("Starting the controller\n");
173*4882a593Smuzhiyun temp = xhci_readl(&hcor->or_usbcmd);
174*4882a593Smuzhiyun temp |= (CMD_RUN);
175*4882a593Smuzhiyun xhci_writel(&hcor->or_usbcmd, temp);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * Wait for the HCHalted Status bit to be 0 to indicate the host is
179*4882a593Smuzhiyun * running.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
182*4882a593Smuzhiyun if (ret)
183*4882a593Smuzhiyun debug("Host took too long to start, "
184*4882a593Smuzhiyun "waited %u microseconds.\n",
185*4882a593Smuzhiyun XHCI_MAX_HALT_USEC);
186*4882a593Smuzhiyun return ret;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /**
190*4882a593Smuzhiyun * Resets the XHCI Controller
191*4882a593Smuzhiyun *
192*4882a593Smuzhiyun * @param hcor pointer to host controller operation registers
193*4882a593Smuzhiyun * @return -EBUSY if XHCI Controller is not halted else status of handshake
194*4882a593Smuzhiyun */
xhci_reset(struct xhci_hcor * hcor)195*4882a593Smuzhiyun static int xhci_reset(struct xhci_hcor *hcor)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun u32 cmd;
198*4882a593Smuzhiyun u32 state;
199*4882a593Smuzhiyun int ret;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /* Halting the Host first */
202*4882a593Smuzhiyun debug("// Halt the HC: %p\n", hcor);
203*4882a593Smuzhiyun state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
204*4882a593Smuzhiyun if (!state) {
205*4882a593Smuzhiyun cmd = xhci_readl(&hcor->or_usbcmd);
206*4882a593Smuzhiyun cmd &= ~CMD_RUN;
207*4882a593Smuzhiyun xhci_writel(&hcor->or_usbcmd, cmd);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun ret = handshake(&hcor->or_usbsts,
211*4882a593Smuzhiyun STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
212*4882a593Smuzhiyun if (ret) {
213*4882a593Smuzhiyun printf("Host not halted after %u microseconds.\n",
214*4882a593Smuzhiyun XHCI_MAX_HALT_USEC);
215*4882a593Smuzhiyun return -EBUSY;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun debug("// Reset the HC\n");
219*4882a593Smuzhiyun cmd = xhci_readl(&hcor->or_usbcmd);
220*4882a593Smuzhiyun cmd |= CMD_RESET;
221*4882a593Smuzhiyun xhci_writel(&hcor->or_usbcmd, cmd);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
224*4882a593Smuzhiyun if (ret)
225*4882a593Smuzhiyun return ret;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * xHCI cannot write to any doorbells or operational registers other
229*4882a593Smuzhiyun * than status until the "Controller Not Ready" flag is cleared.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun * Used for passing endpoint bitmasks between the core and HCDs.
236*4882a593Smuzhiyun * Find the index for an endpoint given its descriptor.
237*4882a593Smuzhiyun * Use the return value to right shift 1 for the bitmask.
238*4882a593Smuzhiyun *
239*4882a593Smuzhiyun * Index = (epnum * 2) + direction - 1,
240*4882a593Smuzhiyun * where direction = 0 for OUT, 1 for IN.
241*4882a593Smuzhiyun * For control endpoints, the IN index is used (OUT index is unused), so
242*4882a593Smuzhiyun * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
243*4882a593Smuzhiyun *
244*4882a593Smuzhiyun * @param desc USB enpdoint Descriptor
245*4882a593Smuzhiyun * @return index of the Endpoint
246*4882a593Smuzhiyun */
xhci_get_ep_index(struct usb_endpoint_descriptor * desc)247*4882a593Smuzhiyun static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun unsigned int index;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (usb_endpoint_xfer_control(desc))
252*4882a593Smuzhiyun index = (unsigned int)(usb_endpoint_num(desc) * 2);
253*4882a593Smuzhiyun else
254*4882a593Smuzhiyun index = (unsigned int)((usb_endpoint_num(desc) * 2) -
255*4882a593Smuzhiyun (usb_endpoint_dir_in(desc) ? 0 : 1));
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun return index;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
262*4882a593Smuzhiyun * microframes, rounded down to nearest power of 2.
263*4882a593Smuzhiyun */
xhci_microframes_to_exponent(unsigned int desc_interval,unsigned int min_exponent,unsigned int max_exponent)264*4882a593Smuzhiyun static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
265*4882a593Smuzhiyun unsigned int min_exponent,
266*4882a593Smuzhiyun unsigned int max_exponent)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun unsigned int interval;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun interval = fls(desc_interval) - 1;
271*4882a593Smuzhiyun interval = clamp_val(interval, min_exponent, max_exponent);
272*4882a593Smuzhiyun if ((1 << interval) != desc_interval)
273*4882a593Smuzhiyun debug("rounding interval to %d microframes, "\
274*4882a593Smuzhiyun "ep desc says %d microframes\n",
275*4882a593Smuzhiyun 1 << interval, desc_interval);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun return interval;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
xhci_parse_microframe_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)280*4882a593Smuzhiyun static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
281*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun if (endpt_desc->bInterval == 0)
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
xhci_parse_frame_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)289*4882a593Smuzhiyun static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
290*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /*
296*4882a593Smuzhiyun * Convert interval expressed as 2^(bInterval - 1) == interval into
297*4882a593Smuzhiyun * straight exponent value 2^n == interval.
298*4882a593Smuzhiyun */
xhci_parse_exponent_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)299*4882a593Smuzhiyun static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
300*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun unsigned int interval;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
305*4882a593Smuzhiyun if (interval != endpt_desc->bInterval - 1)
306*4882a593Smuzhiyun debug("ep %#x - rounding interval to %d %sframes\n",
307*4882a593Smuzhiyun endpt_desc->bEndpointAddress, 1 << interval,
308*4882a593Smuzhiyun udev->speed == USB_SPEED_FULL ? "" : "micro");
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (udev->speed == USB_SPEED_FULL) {
311*4882a593Smuzhiyun /*
312*4882a593Smuzhiyun * Full speed isoc endpoints specify interval in frames,
313*4882a593Smuzhiyun * not microframes. We are using microframes everywhere,
314*4882a593Smuzhiyun * so adjust accordingly.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun interval += 3; /* 1 frame = 2^3 uframes */
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun return interval;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /*
323*4882a593Smuzhiyun * Return the polling or NAK interval.
324*4882a593Smuzhiyun *
325*4882a593Smuzhiyun * The polling interval is expressed in "microframes". If xHCI's Interval field
326*4882a593Smuzhiyun * is set to N, it will service the endpoint every 2^(Interval)*125us.
327*4882a593Smuzhiyun *
328*4882a593Smuzhiyun * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
329*4882a593Smuzhiyun * is set to 0.
330*4882a593Smuzhiyun */
xhci_get_endpoint_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)331*4882a593Smuzhiyun static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
332*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun unsigned int interval = 0;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun switch (udev->speed) {
337*4882a593Smuzhiyun case USB_SPEED_HIGH:
338*4882a593Smuzhiyun /* Max NAK rate */
339*4882a593Smuzhiyun if (usb_endpoint_xfer_control(endpt_desc) ||
340*4882a593Smuzhiyun usb_endpoint_xfer_bulk(endpt_desc)) {
341*4882a593Smuzhiyun interval = xhci_parse_microframe_interval(udev,
342*4882a593Smuzhiyun endpt_desc);
343*4882a593Smuzhiyun break;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun /* Fall through - SS and HS isoc/int have same decoding */
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun case USB_SPEED_SUPER:
348*4882a593Smuzhiyun if (usb_endpoint_xfer_int(endpt_desc) ||
349*4882a593Smuzhiyun usb_endpoint_xfer_isoc(endpt_desc)) {
350*4882a593Smuzhiyun interval = xhci_parse_exponent_interval(udev,
351*4882a593Smuzhiyun endpt_desc);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun break;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun case USB_SPEED_FULL:
356*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(endpt_desc)) {
357*4882a593Smuzhiyun interval = xhci_parse_exponent_interval(udev,
358*4882a593Smuzhiyun endpt_desc);
359*4882a593Smuzhiyun break;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun * Fall through for interrupt endpoint interval decoding
363*4882a593Smuzhiyun * since it uses the same rules as low speed interrupt
364*4882a593Smuzhiyun * endpoints.
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun case USB_SPEED_LOW:
368*4882a593Smuzhiyun if (usb_endpoint_xfer_int(endpt_desc) ||
369*4882a593Smuzhiyun usb_endpoint_xfer_isoc(endpt_desc)) {
370*4882a593Smuzhiyun interval = xhci_parse_frame_interval(udev, endpt_desc);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun break;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun default:
375*4882a593Smuzhiyun BUG();
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun return interval;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
383*4882a593Smuzhiyun * High speed endpoint descriptors can define "the number of additional
384*4882a593Smuzhiyun * transaction opportunities per microframe", but that goes in the Max Burst
385*4882a593Smuzhiyun * endpoint context field.
386*4882a593Smuzhiyun */
xhci_get_endpoint_mult(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc,struct usb_ss_ep_comp_descriptor * ss_ep_comp_desc)387*4882a593Smuzhiyun static u32 xhci_get_endpoint_mult(struct usb_device *udev,
388*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc,
389*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun if (udev->speed < USB_SPEED_SUPER ||
392*4882a593Smuzhiyun !usb_endpoint_xfer_isoc(endpt_desc))
393*4882a593Smuzhiyun return 0;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun return ss_ep_comp_desc->bmAttributes;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
xhci_get_endpoint_max_burst(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc,struct usb_ss_ep_comp_descriptor * ss_ep_comp_desc)398*4882a593Smuzhiyun static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
399*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc,
400*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun /* Super speed and Plus have max burst in ep companion desc */
403*4882a593Smuzhiyun if (udev->speed >= USB_SPEED_SUPER)
404*4882a593Smuzhiyun return ss_ep_comp_desc->bMaxBurst;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun if (udev->speed == USB_SPEED_HIGH &&
407*4882a593Smuzhiyun (usb_endpoint_xfer_isoc(endpt_desc) ||
408*4882a593Smuzhiyun usb_endpoint_xfer_int(endpt_desc)))
409*4882a593Smuzhiyun return usb_endpoint_maxp_mult(endpt_desc) - 1;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun return 0;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /*
415*4882a593Smuzhiyun * Return the maximum endpoint service interval time (ESIT) payload.
416*4882a593Smuzhiyun * Basically, this is the maxpacket size, multiplied by the burst size
417*4882a593Smuzhiyun * and mult size.
418*4882a593Smuzhiyun */
xhci_get_max_esit_payload(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc,struct usb_ss_ep_comp_descriptor * ss_ep_comp_desc)419*4882a593Smuzhiyun static u32 xhci_get_max_esit_payload(struct usb_device *udev,
420*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc,
421*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun int max_burst;
424*4882a593Smuzhiyun int max_packet;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun /* Only applies for interrupt or isochronous endpoints */
427*4882a593Smuzhiyun if (usb_endpoint_xfer_control(endpt_desc) ||
428*4882a593Smuzhiyun usb_endpoint_xfer_bulk(endpt_desc))
429*4882a593Smuzhiyun return 0;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* SuperSpeed Isoc ep with less than 48k per esit */
432*4882a593Smuzhiyun if (udev->speed >= USB_SPEED_SUPER)
433*4882a593Smuzhiyun return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun max_packet = usb_endpoint_maxp(endpt_desc);
436*4882a593Smuzhiyun max_burst = usb_endpoint_maxp_mult(endpt_desc);
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /* A 0 in max burst means 1 transfer per ESIT */
439*4882a593Smuzhiyun return max_packet * max_burst;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /**
443*4882a593Smuzhiyun * Issue a configure endpoint command or evaluate context command
444*4882a593Smuzhiyun * and wait for it to finish.
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * @param udev pointer to the Device Data Structure
447*4882a593Smuzhiyun * @param ctx_change flag to indicate the Context has changed or NOT
448*4882a593Smuzhiyun * @return 0 on success, -1 on failure
449*4882a593Smuzhiyun */
xhci_configure_endpoints(struct usb_device * udev,bool ctx_change)450*4882a593Smuzhiyun static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx;
453*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
454*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
455*4882a593Smuzhiyun union xhci_trb *event;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun virt_dev = ctrl->devs[udev->slot_id];
458*4882a593Smuzhiyun in_ctx = virt_dev->in_ctx;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
461*4882a593Smuzhiyun xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
462*4882a593Smuzhiyun ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
463*4882a593Smuzhiyun event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
464*4882a593Smuzhiyun BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
465*4882a593Smuzhiyun != udev->slot_id);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
468*4882a593Smuzhiyun case COMP_SUCCESS:
469*4882a593Smuzhiyun debug("Successful %s command\n",
470*4882a593Smuzhiyun ctx_change ? "Evaluate Context" : "Configure Endpoint");
471*4882a593Smuzhiyun break;
472*4882a593Smuzhiyun default:
473*4882a593Smuzhiyun printf("ERROR: %s command returned completion code %d.\n",
474*4882a593Smuzhiyun ctx_change ? "Evaluate Context" : "Configure Endpoint",
475*4882a593Smuzhiyun GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
476*4882a593Smuzhiyun return -EINVAL;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun xhci_acknowledge_event(ctrl);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /**
485*4882a593Smuzhiyun * Configure the endpoint, programming the device contexts.
486*4882a593Smuzhiyun *
487*4882a593Smuzhiyun * @param udev pointer to the USB device structure
488*4882a593Smuzhiyun * @return returns the status of the xhci_configure_endpoints
489*4882a593Smuzhiyun */
xhci_set_configuration(struct usb_device * udev)490*4882a593Smuzhiyun static int xhci_set_configuration(struct usb_device *udev)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx;
493*4882a593Smuzhiyun struct xhci_container_ctx *out_ctx;
494*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
495*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
496*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
497*4882a593Smuzhiyun int cur_ep;
498*4882a593Smuzhiyun int max_ep_flag = 0;
499*4882a593Smuzhiyun int ep_index;
500*4882a593Smuzhiyun unsigned int dir;
501*4882a593Smuzhiyun unsigned int ep_type;
502*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
503*4882a593Smuzhiyun int num_of_ep;
504*4882a593Smuzhiyun int ep_flag = 0;
505*4882a593Smuzhiyun u64 trb_64 = 0;
506*4882a593Smuzhiyun int slot_id = udev->slot_id;
507*4882a593Smuzhiyun struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
508*4882a593Smuzhiyun struct usb_interface *ifdesc;
509*4882a593Smuzhiyun u32 max_esit_payload;
510*4882a593Smuzhiyun unsigned int interval;
511*4882a593Smuzhiyun unsigned int mult;
512*4882a593Smuzhiyun unsigned int max_burst;
513*4882a593Smuzhiyun unsigned int avg_trb_len;
514*4882a593Smuzhiyun unsigned int err_count = 0;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun out_ctx = virt_dev->out_ctx;
517*4882a593Smuzhiyun in_ctx = virt_dev->in_ctx;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun num_of_ep = udev->config.if_desc[0].no_of_ep;
520*4882a593Smuzhiyun ifdesc = &udev->config.if_desc[0];
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
523*4882a593Smuzhiyun /* Initialize the input context control */
524*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
525*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
528*4882a593Smuzhiyun for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
529*4882a593Smuzhiyun ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
530*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
531*4882a593Smuzhiyun if (max_ep_flag < ep_flag)
532*4882a593Smuzhiyun max_ep_flag = ep_flag;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /* slot context */
538*4882a593Smuzhiyun xhci_slot_copy(ctrl, in_ctx, out_ctx);
539*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
540*4882a593Smuzhiyun slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
541*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* filling up ep contexts */
546*4882a593Smuzhiyun for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
547*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpt_desc = NULL;
548*4882a593Smuzhiyun struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun endpt_desc = &ifdesc->ep_desc[cur_ep];
551*4882a593Smuzhiyun ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
552*4882a593Smuzhiyun trb_64 = 0;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /*
555*4882a593Smuzhiyun * Get values to fill the endpoint context, mostly from ep
556*4882a593Smuzhiyun * descriptor. The average TRB buffer lengt for bulk endpoints
557*4882a593Smuzhiyun * is unclear as we have no clue on scatter gather list entry
558*4882a593Smuzhiyun * size. For Isoc and Int, set it to max available.
559*4882a593Smuzhiyun * See xHCI 1.1 spec 4.14.1.1 for details.
560*4882a593Smuzhiyun */
561*4882a593Smuzhiyun max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
562*4882a593Smuzhiyun ss_ep_comp_desc);
563*4882a593Smuzhiyun interval = xhci_get_endpoint_interval(udev, endpt_desc);
564*4882a593Smuzhiyun mult = xhci_get_endpoint_mult(udev, endpt_desc,
565*4882a593Smuzhiyun ss_ep_comp_desc);
566*4882a593Smuzhiyun max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
567*4882a593Smuzhiyun ss_ep_comp_desc);
568*4882a593Smuzhiyun avg_trb_len = max_esit_payload;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun ep_index = xhci_get_ep_index(endpt_desc);
571*4882a593Smuzhiyun ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /* Allocate the ep rings */
574*4882a593Smuzhiyun virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
575*4882a593Smuzhiyun if (!virt_dev->eps[ep_index].ring)
576*4882a593Smuzhiyun return -ENOMEM;
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /*NOTE: ep_desc[0] actually represents EP1 and so on */
579*4882a593Smuzhiyun dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
580*4882a593Smuzhiyun ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun ep_ctx[ep_index]->ep_info =
583*4882a593Smuzhiyun cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
584*4882a593Smuzhiyun EP_INTERVAL(interval) | EP_MULT(mult));
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun ep_ctx[ep_index]->ep_info2 =
587*4882a593Smuzhiyun cpu_to_le32(ep_type << EP_TYPE_SHIFT);
588*4882a593Smuzhiyun ep_ctx[ep_index]->ep_info2 |=
589*4882a593Smuzhiyun cpu_to_le32(MAX_PACKET
590*4882a593Smuzhiyun (get_unaligned(&endpt_desc->wMaxPacketSize)));
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /* Allow 3 retries for everything but isoc, set CErr = 3 */
593*4882a593Smuzhiyun if (!usb_endpoint_xfer_isoc(endpt_desc))
594*4882a593Smuzhiyun err_count = 3;
595*4882a593Smuzhiyun ep_ctx[ep_index]->ep_info2 |=
596*4882a593Smuzhiyun cpu_to_le32(MAX_BURST(max_burst) |
597*4882a593Smuzhiyun ERROR_COUNT(err_count));
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun trb_64 = (uintptr_t)
600*4882a593Smuzhiyun virt_dev->eps[ep_index].ring->enqueue;
601*4882a593Smuzhiyun ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
602*4882a593Smuzhiyun virt_dev->eps[ep_index].ring->cycle_state);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /*
605*4882a593Smuzhiyun * xHCI spec 6.2.3:
606*4882a593Smuzhiyun * 'Average TRB Length' should be 8 for control endpoints.
607*4882a593Smuzhiyun */
608*4882a593Smuzhiyun if (usb_endpoint_xfer_control(endpt_desc))
609*4882a593Smuzhiyun avg_trb_len = 8;
610*4882a593Smuzhiyun ep_ctx[ep_index]->tx_info =
611*4882a593Smuzhiyun cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
612*4882a593Smuzhiyun EP_AVG_TRB_LENGTH(avg_trb_len));
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun return xhci_configure_endpoints(udev, false);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /**
619*4882a593Smuzhiyun * Issue an Address Device command (which will issue a SetAddress request to
620*4882a593Smuzhiyun * the device).
621*4882a593Smuzhiyun *
622*4882a593Smuzhiyun * @param udev pointer to the Device Data Structure
623*4882a593Smuzhiyun * @return 0 if successful else error code on failure
624*4882a593Smuzhiyun */
xhci_address_device(struct usb_device * udev,int root_portnr)625*4882a593Smuzhiyun static int xhci_address_device(struct usb_device *udev, int root_portnr)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun int ret = 0;
628*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
629*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
630*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
631*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
632*4882a593Smuzhiyun int slot_id = udev->slot_id;
633*4882a593Smuzhiyun union xhci_trb *event;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun virt_dev = ctrl->devs[slot_id];
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun /*
638*4882a593Smuzhiyun * This is the first Set Address since device plug-in
639*4882a593Smuzhiyun * so setting up the slot context.
640*4882a593Smuzhiyun */
641*4882a593Smuzhiyun debug("Setting up addressable devices %p\n", ctrl->dcbaa);
642*4882a593Smuzhiyun xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
645*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
646*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
649*4882a593Smuzhiyun event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
650*4882a593Smuzhiyun BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
653*4882a593Smuzhiyun case COMP_CTX_STATE:
654*4882a593Smuzhiyun case COMP_EBADSLT:
655*4882a593Smuzhiyun printf("Setup ERROR: address device command for slot %d.\n",
656*4882a593Smuzhiyun slot_id);
657*4882a593Smuzhiyun ret = -EINVAL;
658*4882a593Smuzhiyun break;
659*4882a593Smuzhiyun case COMP_TX_ERR:
660*4882a593Smuzhiyun puts("Device not responding to set address.\n");
661*4882a593Smuzhiyun ret = -EPROTO;
662*4882a593Smuzhiyun break;
663*4882a593Smuzhiyun case COMP_DEV_ERR:
664*4882a593Smuzhiyun puts("ERROR: Incompatible device"
665*4882a593Smuzhiyun "for address device command.\n");
666*4882a593Smuzhiyun ret = -ENODEV;
667*4882a593Smuzhiyun break;
668*4882a593Smuzhiyun case COMP_SUCCESS:
669*4882a593Smuzhiyun debug("Successful Address Device command\n");
670*4882a593Smuzhiyun udev->status = 0;
671*4882a593Smuzhiyun break;
672*4882a593Smuzhiyun default:
673*4882a593Smuzhiyun printf("ERROR: unexpected command completion code 0x%x.\n",
674*4882a593Smuzhiyun GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
675*4882a593Smuzhiyun ret = -EINVAL;
676*4882a593Smuzhiyun break;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun xhci_acknowledge_event(ctrl);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (ret < 0)
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun * TODO: Unsuccessful Address Device command shall leave the
684*4882a593Smuzhiyun * slot in default state. So, issue Disable Slot command now.
685*4882a593Smuzhiyun */
686*4882a593Smuzhiyun return ret;
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
689*4882a593Smuzhiyun virt_dev->out_ctx->size);
690*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun debug("xHC internal address is: %d\n",
693*4882a593Smuzhiyun le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun return 0;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /**
699*4882a593Smuzhiyun * Issue Enable slot command to the controller to allocate
700*4882a593Smuzhiyun * device slot and assign the slot id. It fails if the xHC
701*4882a593Smuzhiyun * ran out of device slots, the Enable Slot command timed out,
702*4882a593Smuzhiyun * or allocating memory failed.
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * @param udev pointer to the Device Data Structure
705*4882a593Smuzhiyun * @return Returns 0 on succes else return error code on failure
706*4882a593Smuzhiyun */
_xhci_alloc_device(struct usb_device * udev)707*4882a593Smuzhiyun static int _xhci_alloc_device(struct usb_device *udev)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
710*4882a593Smuzhiyun union xhci_trb *event;
711*4882a593Smuzhiyun int ret;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun /*
714*4882a593Smuzhiyun * Root hub will be first device to be initailized.
715*4882a593Smuzhiyun * If this device is root-hub, don't do any xHC related
716*4882a593Smuzhiyun * stuff.
717*4882a593Smuzhiyun */
718*4882a593Smuzhiyun if (ctrl->rootdev == 0) {
719*4882a593Smuzhiyun udev->speed = USB_SPEED_SUPER;
720*4882a593Smuzhiyun return 0;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
724*4882a593Smuzhiyun event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
725*4882a593Smuzhiyun BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
726*4882a593Smuzhiyun != COMP_SUCCESS);
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun xhci_acknowledge_event(ctrl);
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
733*4882a593Smuzhiyun if (ret < 0) {
734*4882a593Smuzhiyun /*
735*4882a593Smuzhiyun * TODO: Unsuccessful Address Device command shall leave
736*4882a593Smuzhiyun * the slot in default. So, issue Disable Slot command now.
737*4882a593Smuzhiyun */
738*4882a593Smuzhiyun puts("Could not allocate xHCI USB device data structures\n");
739*4882a593Smuzhiyun return ret;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun return 0;
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
usb_alloc_device(struct usb_device * udev)746*4882a593Smuzhiyun int usb_alloc_device(struct usb_device *udev)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun return _xhci_alloc_device(udev);
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun #endif
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /*
753*4882a593Smuzhiyun * Full speed devices may have a max packet size greater than 8 bytes, but the
754*4882a593Smuzhiyun * USB core doesn't know that until it reads the first 8 bytes of the
755*4882a593Smuzhiyun * descriptor. If the usb_device's max packet size changes after that point,
756*4882a593Smuzhiyun * we need to issue an evaluate context command and wait on it.
757*4882a593Smuzhiyun *
758*4882a593Smuzhiyun * @param udev pointer to the Device Data Structure
759*4882a593Smuzhiyun * @return returns the status of the xhci_configure_endpoints
760*4882a593Smuzhiyun */
xhci_check_maxpacket(struct usb_device * udev)761*4882a593Smuzhiyun int xhci_check_maxpacket(struct usb_device *udev)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
764*4882a593Smuzhiyun unsigned int slot_id = udev->slot_id;
765*4882a593Smuzhiyun int ep_index = 0; /* control endpoint */
766*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx;
767*4882a593Smuzhiyun struct xhci_container_ctx *out_ctx;
768*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
769*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
770*4882a593Smuzhiyun int max_packet_size;
771*4882a593Smuzhiyun int hw_max_packet_size;
772*4882a593Smuzhiyun int ret = 0;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun out_ctx = ctrl->devs[slot_id]->out_ctx;
775*4882a593Smuzhiyun xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
778*4882a593Smuzhiyun hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
779*4882a593Smuzhiyun max_packet_size = udev->epmaxpacketin[0];
780*4882a593Smuzhiyun if (hw_max_packet_size != max_packet_size) {
781*4882a593Smuzhiyun debug("Max Packet Size for ep 0 changed.\n");
782*4882a593Smuzhiyun debug("Max packet size in usb_device = %d\n", max_packet_size);
783*4882a593Smuzhiyun debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
784*4882a593Smuzhiyun debug("Issuing evaluate context command.\n");
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun /* Set up the modified control endpoint 0 */
787*4882a593Smuzhiyun xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
788*4882a593Smuzhiyun ctrl->devs[slot_id]->out_ctx, ep_index);
789*4882a593Smuzhiyun in_ctx = ctrl->devs[slot_id]->in_ctx;
790*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
791*4882a593Smuzhiyun ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
792*4882a593Smuzhiyun << MAX_PACKET_SHIFT));
793*4882a593Smuzhiyun ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun * Set up the input context flags for the command
797*4882a593Smuzhiyun * FIXME: This won't work if a non-default control endpoint
798*4882a593Smuzhiyun * changes max packet sizes.
799*4882a593Smuzhiyun */
800*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
801*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
802*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun ret = xhci_configure_endpoints(udev, true);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun return ret;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /**
810*4882a593Smuzhiyun * Clears the Change bits of the Port Status Register
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * @param wValue request value
813*4882a593Smuzhiyun * @param wIndex request index
814*4882a593Smuzhiyun * @param addr address of posrt status register
815*4882a593Smuzhiyun * @param port_status state of port status register
816*4882a593Smuzhiyun * @return none
817*4882a593Smuzhiyun */
xhci_clear_port_change_bit(u16 wValue,u16 wIndex,volatile uint32_t * addr,u32 port_status)818*4882a593Smuzhiyun static void xhci_clear_port_change_bit(u16 wValue,
819*4882a593Smuzhiyun u16 wIndex, volatile uint32_t *addr, u32 port_status)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun char *port_change_bit;
822*4882a593Smuzhiyun u32 status;
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun switch (wValue) {
825*4882a593Smuzhiyun case USB_PORT_FEAT_C_RESET:
826*4882a593Smuzhiyun status = PORT_RC;
827*4882a593Smuzhiyun port_change_bit = "reset";
828*4882a593Smuzhiyun break;
829*4882a593Smuzhiyun case USB_PORT_FEAT_C_CONNECTION:
830*4882a593Smuzhiyun status = PORT_CSC;
831*4882a593Smuzhiyun port_change_bit = "connect";
832*4882a593Smuzhiyun break;
833*4882a593Smuzhiyun case USB_PORT_FEAT_C_OVER_CURRENT:
834*4882a593Smuzhiyun status = PORT_OCC;
835*4882a593Smuzhiyun port_change_bit = "over-current";
836*4882a593Smuzhiyun break;
837*4882a593Smuzhiyun case USB_PORT_FEAT_C_ENABLE:
838*4882a593Smuzhiyun status = PORT_PEC;
839*4882a593Smuzhiyun port_change_bit = "enable/disable";
840*4882a593Smuzhiyun break;
841*4882a593Smuzhiyun case USB_PORT_FEAT_C_SUSPEND:
842*4882a593Smuzhiyun status = PORT_PLC;
843*4882a593Smuzhiyun port_change_bit = "suspend/resume";
844*4882a593Smuzhiyun break;
845*4882a593Smuzhiyun default:
846*4882a593Smuzhiyun /* Should never happen */
847*4882a593Smuzhiyun return;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun /* Change bits are all write 1 to clear */
851*4882a593Smuzhiyun xhci_writel(addr, port_status | status);
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun port_status = xhci_readl(addr);
854*4882a593Smuzhiyun debug("clear port %s change, actual port %d status = 0x%x\n",
855*4882a593Smuzhiyun port_change_bit, wIndex, port_status);
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun /**
859*4882a593Smuzhiyun * Save Read Only (RO) bits and save read/write bits where
860*4882a593Smuzhiyun * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
861*4882a593Smuzhiyun * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
862*4882a593Smuzhiyun *
863*4882a593Smuzhiyun * @param state state of the Port Status and Control Regsiter
864*4882a593Smuzhiyun * @return a value that would result in the port being in the
865*4882a593Smuzhiyun * same state, if the value was written to the port
866*4882a593Smuzhiyun * status control register.
867*4882a593Smuzhiyun */
xhci_port_state_to_neutral(u32 state)868*4882a593Smuzhiyun static u32 xhci_port_state_to_neutral(u32 state)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun /* Save read-only status and port state */
871*4882a593Smuzhiyun return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun /**
875*4882a593Smuzhiyun * Submits the Requests to the XHCI Host Controller
876*4882a593Smuzhiyun *
877*4882a593Smuzhiyun * @param udev pointer to the USB device structure
878*4882a593Smuzhiyun * @param pipe contains the DIR_IN or OUT , devnum
879*4882a593Smuzhiyun * @param buffer buffer to be read/written based on the request
880*4882a593Smuzhiyun * @return returns 0 if successful else -1 on failure
881*4882a593Smuzhiyun */
xhci_submit_root(struct usb_device * udev,unsigned long pipe,void * buffer,struct devrequest * req)882*4882a593Smuzhiyun static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
883*4882a593Smuzhiyun void *buffer, struct devrequest *req)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun uint8_t tmpbuf[4];
886*4882a593Smuzhiyun u16 typeReq;
887*4882a593Smuzhiyun void *srcptr = NULL;
888*4882a593Smuzhiyun int len, srclen;
889*4882a593Smuzhiyun uint32_t reg;
890*4882a593Smuzhiyun volatile uint32_t *status_reg;
891*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
892*4882a593Smuzhiyun struct xhci_hccr *hccr = ctrl->hccr;
893*4882a593Smuzhiyun struct xhci_hcor *hcor = ctrl->hcor;
894*4882a593Smuzhiyun int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun if ((req->requesttype & USB_RT_PORT) &&
897*4882a593Smuzhiyun le16_to_cpu(req->index) > max_ports) {
898*4882a593Smuzhiyun printf("The request port(%d) exceeds maximum port number\n",
899*4882a593Smuzhiyun le16_to_cpu(req->index) - 1);
900*4882a593Smuzhiyun return -EINVAL;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun status_reg = (volatile uint32_t *)
904*4882a593Smuzhiyun (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
905*4882a593Smuzhiyun srclen = 0;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun typeReq = req->request | req->requesttype << 8;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun switch (typeReq) {
910*4882a593Smuzhiyun case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
911*4882a593Smuzhiyun switch (le16_to_cpu(req->value) >> 8) {
912*4882a593Smuzhiyun case USB_DT_DEVICE:
913*4882a593Smuzhiyun debug("USB_DT_DEVICE request\n");
914*4882a593Smuzhiyun srcptr = &descriptor.device;
915*4882a593Smuzhiyun srclen = 0x12;
916*4882a593Smuzhiyun break;
917*4882a593Smuzhiyun case USB_DT_CONFIG:
918*4882a593Smuzhiyun debug("USB_DT_CONFIG config\n");
919*4882a593Smuzhiyun srcptr = &descriptor.config;
920*4882a593Smuzhiyun srclen = 0x19;
921*4882a593Smuzhiyun break;
922*4882a593Smuzhiyun case USB_DT_STRING:
923*4882a593Smuzhiyun debug("USB_DT_STRING config\n");
924*4882a593Smuzhiyun switch (le16_to_cpu(req->value) & 0xff) {
925*4882a593Smuzhiyun case 0: /* Language */
926*4882a593Smuzhiyun srcptr = "\4\3\11\4";
927*4882a593Smuzhiyun srclen = 4;
928*4882a593Smuzhiyun break;
929*4882a593Smuzhiyun case 1: /* Vendor String */
930*4882a593Smuzhiyun srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
931*4882a593Smuzhiyun srclen = 14;
932*4882a593Smuzhiyun break;
933*4882a593Smuzhiyun case 2: /* Product Name */
934*4882a593Smuzhiyun srcptr = "\52\3X\0H\0C\0I\0 "
935*4882a593Smuzhiyun "\0H\0o\0s\0t\0 "
936*4882a593Smuzhiyun "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
937*4882a593Smuzhiyun srclen = 42;
938*4882a593Smuzhiyun break;
939*4882a593Smuzhiyun default:
940*4882a593Smuzhiyun printf("unknown value DT_STRING %x\n",
941*4882a593Smuzhiyun le16_to_cpu(req->value));
942*4882a593Smuzhiyun goto unknown;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun break;
945*4882a593Smuzhiyun default:
946*4882a593Smuzhiyun printf("unknown value %x\n", le16_to_cpu(req->value));
947*4882a593Smuzhiyun goto unknown;
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun break;
950*4882a593Smuzhiyun case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
951*4882a593Smuzhiyun switch (le16_to_cpu(req->value) >> 8) {
952*4882a593Smuzhiyun case USB_DT_HUB:
953*4882a593Smuzhiyun case USB_DT_SS_HUB:
954*4882a593Smuzhiyun debug("USB_DT_HUB config\n");
955*4882a593Smuzhiyun srcptr = &ctrl->hub;
956*4882a593Smuzhiyun srclen = 0x8;
957*4882a593Smuzhiyun break;
958*4882a593Smuzhiyun default:
959*4882a593Smuzhiyun printf("unknown value %x\n", le16_to_cpu(req->value));
960*4882a593Smuzhiyun goto unknown;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun break;
963*4882a593Smuzhiyun case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
964*4882a593Smuzhiyun debug("USB_REQ_SET_ADDRESS\n");
965*4882a593Smuzhiyun ctrl->rootdev = le16_to_cpu(req->value);
966*4882a593Smuzhiyun break;
967*4882a593Smuzhiyun case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
968*4882a593Smuzhiyun /* Do nothing */
969*4882a593Smuzhiyun break;
970*4882a593Smuzhiyun case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
971*4882a593Smuzhiyun tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
972*4882a593Smuzhiyun tmpbuf[1] = 0;
973*4882a593Smuzhiyun srcptr = tmpbuf;
974*4882a593Smuzhiyun srclen = 2;
975*4882a593Smuzhiyun break;
976*4882a593Smuzhiyun case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
977*4882a593Smuzhiyun memset(tmpbuf, 0, 4);
978*4882a593Smuzhiyun reg = xhci_readl(status_reg);
979*4882a593Smuzhiyun if (reg & PORT_CONNECT) {
980*4882a593Smuzhiyun tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
981*4882a593Smuzhiyun switch (reg & DEV_SPEED_MASK) {
982*4882a593Smuzhiyun case XDEV_FS:
983*4882a593Smuzhiyun debug("SPEED = FULLSPEED\n");
984*4882a593Smuzhiyun break;
985*4882a593Smuzhiyun case XDEV_LS:
986*4882a593Smuzhiyun debug("SPEED = LOWSPEED\n");
987*4882a593Smuzhiyun tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
988*4882a593Smuzhiyun break;
989*4882a593Smuzhiyun case XDEV_HS:
990*4882a593Smuzhiyun debug("SPEED = HIGHSPEED\n");
991*4882a593Smuzhiyun tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
992*4882a593Smuzhiyun break;
993*4882a593Smuzhiyun case XDEV_SS:
994*4882a593Smuzhiyun debug("SPEED = SUPERSPEED\n");
995*4882a593Smuzhiyun tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
996*4882a593Smuzhiyun break;
997*4882a593Smuzhiyun }
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun if (reg & PORT_PE)
1000*4882a593Smuzhiyun tmpbuf[0] |= USB_PORT_STAT_ENABLE;
1001*4882a593Smuzhiyun if ((reg & PORT_PLS_MASK) == XDEV_U3)
1002*4882a593Smuzhiyun tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
1003*4882a593Smuzhiyun if (reg & PORT_OC)
1004*4882a593Smuzhiyun tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
1005*4882a593Smuzhiyun if (reg & PORT_RESET)
1006*4882a593Smuzhiyun tmpbuf[0] |= USB_PORT_STAT_RESET;
1007*4882a593Smuzhiyun if (reg & PORT_POWER)
1008*4882a593Smuzhiyun /*
1009*4882a593Smuzhiyun * XXX: This Port power bit (for USB 3.0 hub)
1010*4882a593Smuzhiyun * we are faking in USB 2.0 hub port status;
1011*4882a593Smuzhiyun * since there's a change in bit positions in
1012*4882a593Smuzhiyun * two:
1013*4882a593Smuzhiyun * USB 2.0 port status PP is at position[8]
1014*4882a593Smuzhiyun * USB 3.0 port status PP is at position[9]
1015*4882a593Smuzhiyun * So, we are still keeping it at position [8]
1016*4882a593Smuzhiyun */
1017*4882a593Smuzhiyun tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
1018*4882a593Smuzhiyun if (reg & PORT_CSC)
1019*4882a593Smuzhiyun tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
1020*4882a593Smuzhiyun if (reg & PORT_PEC)
1021*4882a593Smuzhiyun tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
1022*4882a593Smuzhiyun if (reg & PORT_OCC)
1023*4882a593Smuzhiyun tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
1024*4882a593Smuzhiyun if (reg & PORT_RC)
1025*4882a593Smuzhiyun tmpbuf[2] |= USB_PORT_STAT_C_RESET;
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun srcptr = tmpbuf;
1028*4882a593Smuzhiyun srclen = 4;
1029*4882a593Smuzhiyun break;
1030*4882a593Smuzhiyun case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1031*4882a593Smuzhiyun reg = xhci_readl(status_reg);
1032*4882a593Smuzhiyun reg = xhci_port_state_to_neutral(reg);
1033*4882a593Smuzhiyun switch (le16_to_cpu(req->value)) {
1034*4882a593Smuzhiyun case USB_PORT_FEAT_ENABLE:
1035*4882a593Smuzhiyun reg |= PORT_PE;
1036*4882a593Smuzhiyun xhci_writel(status_reg, reg);
1037*4882a593Smuzhiyun break;
1038*4882a593Smuzhiyun case USB_PORT_FEAT_POWER:
1039*4882a593Smuzhiyun reg |= PORT_POWER;
1040*4882a593Smuzhiyun xhci_writel(status_reg, reg);
1041*4882a593Smuzhiyun break;
1042*4882a593Smuzhiyun case USB_PORT_FEAT_RESET:
1043*4882a593Smuzhiyun reg |= PORT_RESET;
1044*4882a593Smuzhiyun xhci_writel(status_reg, reg);
1045*4882a593Smuzhiyun break;
1046*4882a593Smuzhiyun default:
1047*4882a593Smuzhiyun printf("unknown feature %x\n", le16_to_cpu(req->value));
1048*4882a593Smuzhiyun goto unknown;
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun break;
1051*4882a593Smuzhiyun case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1052*4882a593Smuzhiyun reg = xhci_readl(status_reg);
1053*4882a593Smuzhiyun reg = xhci_port_state_to_neutral(reg);
1054*4882a593Smuzhiyun switch (le16_to_cpu(req->value)) {
1055*4882a593Smuzhiyun case USB_PORT_FEAT_ENABLE:
1056*4882a593Smuzhiyun reg &= ~PORT_PE;
1057*4882a593Smuzhiyun break;
1058*4882a593Smuzhiyun case USB_PORT_FEAT_POWER:
1059*4882a593Smuzhiyun reg &= ~PORT_POWER;
1060*4882a593Smuzhiyun break;
1061*4882a593Smuzhiyun case USB_PORT_FEAT_C_RESET:
1062*4882a593Smuzhiyun case USB_PORT_FEAT_C_CONNECTION:
1063*4882a593Smuzhiyun case USB_PORT_FEAT_C_OVER_CURRENT:
1064*4882a593Smuzhiyun case USB_PORT_FEAT_C_ENABLE:
1065*4882a593Smuzhiyun xhci_clear_port_change_bit((le16_to_cpu(req->value)),
1066*4882a593Smuzhiyun le16_to_cpu(req->index),
1067*4882a593Smuzhiyun status_reg, reg);
1068*4882a593Smuzhiyun break;
1069*4882a593Smuzhiyun default:
1070*4882a593Smuzhiyun printf("unknown feature %x\n", le16_to_cpu(req->value));
1071*4882a593Smuzhiyun goto unknown;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun xhci_writel(status_reg, reg);
1074*4882a593Smuzhiyun break;
1075*4882a593Smuzhiyun default:
1076*4882a593Smuzhiyun puts("Unknown request\n");
1077*4882a593Smuzhiyun goto unknown;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun debug("scrlen = %d\n req->length = %d\n",
1081*4882a593Smuzhiyun srclen, le16_to_cpu(req->length));
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun len = min(srclen, (int)le16_to_cpu(req->length));
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun if (srcptr != NULL && len > 0)
1086*4882a593Smuzhiyun memcpy(buffer, srcptr, len);
1087*4882a593Smuzhiyun else
1088*4882a593Smuzhiyun debug("Len is 0\n");
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun udev->act_len = len;
1091*4882a593Smuzhiyun udev->status = 0;
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun return 0;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun unknown:
1096*4882a593Smuzhiyun udev->act_len = 0;
1097*4882a593Smuzhiyun udev->status = USB_ST_STALLED;
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun return -ENODEV;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun /**
1103*4882a593Smuzhiyun * Submits the INT request to XHCI Host cotroller
1104*4882a593Smuzhiyun *
1105*4882a593Smuzhiyun * @param udev pointer to the USB device
1106*4882a593Smuzhiyun * @param pipe contains the DIR_IN or OUT , devnum
1107*4882a593Smuzhiyun * @param buffer buffer to be read/written based on the request
1108*4882a593Smuzhiyun * @param length length of the buffer
1109*4882a593Smuzhiyun * @param interval interval of the interrupt
1110*4882a593Smuzhiyun * @return 0
1111*4882a593Smuzhiyun */
_xhci_submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1112*4882a593Smuzhiyun static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
1113*4882a593Smuzhiyun void *buffer, int length, int interval,
1114*4882a593Smuzhiyun bool nonblock)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1117*4882a593Smuzhiyun printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1118*4882a593Smuzhiyun return -EINVAL;
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun /*
1122*4882a593Smuzhiyun * xHCI uses normal TRBs for both bulk and interrupt. When the
1123*4882a593Smuzhiyun * interrupt endpoint is to be serviced, the xHC will consume
1124*4882a593Smuzhiyun * (at most) one TD. A TD (comprised of sg list entries) can
1125*4882a593Smuzhiyun * take several service intervals to transmit.
1126*4882a593Smuzhiyun */
1127*4882a593Smuzhiyun return xhci_bulk_tx(udev, pipe, length, buffer);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun /**
1131*4882a593Smuzhiyun * submit the BULK type of request to the USB Device
1132*4882a593Smuzhiyun *
1133*4882a593Smuzhiyun * @param udev pointer to the USB device
1134*4882a593Smuzhiyun * @param pipe contains the DIR_IN or OUT , devnum
1135*4882a593Smuzhiyun * @param buffer buffer to be read/written based on the request
1136*4882a593Smuzhiyun * @param length length of the buffer
1137*4882a593Smuzhiyun * @return returns 0 if successful else -1 on failure
1138*4882a593Smuzhiyun */
_xhci_submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1139*4882a593Smuzhiyun static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1140*4882a593Smuzhiyun void *buffer, int length)
1141*4882a593Smuzhiyun {
1142*4882a593Smuzhiyun if (usb_pipetype(pipe) != PIPE_BULK) {
1143*4882a593Smuzhiyun printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1144*4882a593Smuzhiyun return -EINVAL;
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun return xhci_bulk_tx(udev, pipe, length, buffer);
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun * submit the control type of request to the Root hub/Device based on the devnum
1152*4882a593Smuzhiyun *
1153*4882a593Smuzhiyun * @param udev pointer to the USB device
1154*4882a593Smuzhiyun * @param pipe contains the DIR_IN or OUT , devnum
1155*4882a593Smuzhiyun * @param buffer buffer to be read/written based on the request
1156*4882a593Smuzhiyun * @param length length of the buffer
1157*4882a593Smuzhiyun * @param setup Request type
1158*4882a593Smuzhiyun * @param root_portnr Root port number that this device is on
1159*4882a593Smuzhiyun * @return returns 0 if successful else -1 on failure
1160*4882a593Smuzhiyun */
_xhci_submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup,int root_portnr)1161*4882a593Smuzhiyun static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
1162*4882a593Smuzhiyun void *buffer, int length,
1163*4882a593Smuzhiyun struct devrequest *setup, int root_portnr)
1164*4882a593Smuzhiyun {
1165*4882a593Smuzhiyun struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
1166*4882a593Smuzhiyun int ret = 0;
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun if (usb_pipetype(pipe) != PIPE_CONTROL) {
1169*4882a593Smuzhiyun printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
1170*4882a593Smuzhiyun return -EINVAL;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun if (usb_pipedevice(pipe) == ctrl->rootdev)
1174*4882a593Smuzhiyun return xhci_submit_root(udev, pipe, buffer, setup);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun if (setup->request == USB_REQ_SET_ADDRESS &&
1177*4882a593Smuzhiyun (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1178*4882a593Smuzhiyun return xhci_address_device(udev, root_portnr);
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun if (setup->request == USB_REQ_SET_CONFIGURATION &&
1181*4882a593Smuzhiyun (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1182*4882a593Smuzhiyun ret = xhci_set_configuration(udev);
1183*4882a593Smuzhiyun if (ret) {
1184*4882a593Smuzhiyun puts("Failed to configure xHCI endpoint\n");
1185*4882a593Smuzhiyun return ret;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun
xhci_lowlevel_init(struct xhci_ctrl * ctrl)1192*4882a593Smuzhiyun static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
1193*4882a593Smuzhiyun {
1194*4882a593Smuzhiyun struct xhci_hccr *hccr;
1195*4882a593Smuzhiyun struct xhci_hcor *hcor;
1196*4882a593Smuzhiyun uint32_t val;
1197*4882a593Smuzhiyun uint32_t val2;
1198*4882a593Smuzhiyun uint32_t reg;
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun hccr = ctrl->hccr;
1201*4882a593Smuzhiyun hcor = ctrl->hcor;
1202*4882a593Smuzhiyun /*
1203*4882a593Smuzhiyun * Program the Number of Device Slots Enabled field in the CONFIG
1204*4882a593Smuzhiyun * register with the max value of slots the HC can handle.
1205*4882a593Smuzhiyun */
1206*4882a593Smuzhiyun val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
1207*4882a593Smuzhiyun val2 = xhci_readl(&hcor->or_config);
1208*4882a593Smuzhiyun val |= (val2 & ~HCS_SLOTS_MASK);
1209*4882a593Smuzhiyun xhci_writel(&hcor->or_config, val);
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun /* initializing xhci data structures */
1212*4882a593Smuzhiyun if (xhci_mem_init(ctrl, hccr, hcor) < 0)
1213*4882a593Smuzhiyun return -ENOMEM;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun reg = xhci_readl(&hccr->cr_hcsparams1);
1216*4882a593Smuzhiyun descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
1217*4882a593Smuzhiyun HCS_MAX_PORTS_SHIFT);
1218*4882a593Smuzhiyun printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun /* Port Indicators */
1221*4882a593Smuzhiyun reg = xhci_readl(&hccr->cr_hccparams);
1222*4882a593Smuzhiyun if (HCS_INDICATOR(reg))
1223*4882a593Smuzhiyun put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1224*4882a593Smuzhiyun | 0x80, &descriptor.hub.wHubCharacteristics);
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun /* Port Power Control */
1227*4882a593Smuzhiyun if (HCC_PPC(reg))
1228*4882a593Smuzhiyun put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1229*4882a593Smuzhiyun | 0x01, &descriptor.hub.wHubCharacteristics);
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun memcpy(&ctrl->hub, &descriptor, sizeof(struct usb_hub_descriptor));
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun if (xhci_start(hcor)) {
1234*4882a593Smuzhiyun xhci_reset(hcor);
1235*4882a593Smuzhiyun return -ENODEV;
1236*4882a593Smuzhiyun }
1237*4882a593Smuzhiyun
1238*4882a593Smuzhiyun /* Zero'ing IRQ control register and IRQ pending register */
1239*4882a593Smuzhiyun xhci_writel(&ctrl->ir_set->irq_control, 0x0);
1240*4882a593Smuzhiyun xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
1243*4882a593Smuzhiyun printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun return 0;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun
xhci_lowlevel_stop(struct xhci_ctrl * ctrl)1248*4882a593Smuzhiyun static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1249*4882a593Smuzhiyun {
1250*4882a593Smuzhiyun u32 temp;
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun xhci_reset(ctrl->hcor);
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun debug("// Disabling event ring interrupts\n");
1255*4882a593Smuzhiyun temp = xhci_readl(&ctrl->hcor->or_usbsts);
1256*4882a593Smuzhiyun xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1257*4882a593Smuzhiyun temp = xhci_readl(&ctrl->ir_set->irq_pending);
1258*4882a593Smuzhiyun xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun return 0;
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)1264*4882a593Smuzhiyun int submit_control_msg(struct usb_device *udev, unsigned long pipe,
1265*4882a593Smuzhiyun void *buffer, int length, struct devrequest *setup)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun struct usb_device *hop = udev;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun if (hop->parent)
1270*4882a593Smuzhiyun while (hop->parent->parent)
1271*4882a593Smuzhiyun hop = hop->parent;
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1274*4882a593Smuzhiyun hop->portnr);
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun
submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1277*4882a593Smuzhiyun int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1278*4882a593Smuzhiyun int length)
1279*4882a593Smuzhiyun {
1280*4882a593Smuzhiyun return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun
submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1283*4882a593Smuzhiyun int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1284*4882a593Smuzhiyun int length, int interval, bool nonblock)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1287*4882a593Smuzhiyun nonblock);
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun /**
1291*4882a593Smuzhiyun * Intialises the XHCI host controller
1292*4882a593Smuzhiyun * and allocates the necessary data structures
1293*4882a593Smuzhiyun *
1294*4882a593Smuzhiyun * @param index index to the host controller data structure
1295*4882a593Smuzhiyun * @return pointer to the intialised controller
1296*4882a593Smuzhiyun */
usb_lowlevel_init(int index,enum usb_init_type init,void ** controller)1297*4882a593Smuzhiyun int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1298*4882a593Smuzhiyun {
1299*4882a593Smuzhiyun struct xhci_hccr *hccr;
1300*4882a593Smuzhiyun struct xhci_hcor *hcor;
1301*4882a593Smuzhiyun struct xhci_ctrl *ctrl;
1302*4882a593Smuzhiyun int ret;
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun *controller = NULL;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1307*4882a593Smuzhiyun return -ENODEV;
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun if (xhci_reset(hcor) != 0)
1310*4882a593Smuzhiyun return -ENODEV;
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun ctrl = &xhcic[index];
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun ctrl->hccr = hccr;
1315*4882a593Smuzhiyun ctrl->hcor = hcor;
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun ret = xhci_lowlevel_init(ctrl);
1318*4882a593Smuzhiyun
1319*4882a593Smuzhiyun if (ret) {
1320*4882a593Smuzhiyun ctrl->hccr = NULL;
1321*4882a593Smuzhiyun ctrl->hcor = NULL;
1322*4882a593Smuzhiyun } else {
1323*4882a593Smuzhiyun *controller = &xhcic[index];
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun return ret;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun /**
1330*4882a593Smuzhiyun * Stops the XHCI host controller
1331*4882a593Smuzhiyun * and cleans up all the related data structures
1332*4882a593Smuzhiyun *
1333*4882a593Smuzhiyun * @param index index to the host controller data structure
1334*4882a593Smuzhiyun * @return none
1335*4882a593Smuzhiyun */
usb_lowlevel_stop(int index)1336*4882a593Smuzhiyun int usb_lowlevel_stop(int index)
1337*4882a593Smuzhiyun {
1338*4882a593Smuzhiyun struct xhci_ctrl *ctrl = (xhcic + index);
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun if (ctrl->hcor) {
1341*4882a593Smuzhiyun xhci_lowlevel_stop(ctrl);
1342*4882a593Smuzhiyun xhci_hcd_stop(index);
1343*4882a593Smuzhiyun xhci_cleanup(ctrl);
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun return 0;
1347*4882a593Smuzhiyun }
1348*4882a593Smuzhiyun #endif /* CONFIG_IS_ENABLED(DM_USB) */
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
1351*4882a593Smuzhiyun
xhci_submit_control_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)1352*4882a593Smuzhiyun static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1353*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length,
1354*4882a593Smuzhiyun struct devrequest *setup)
1355*4882a593Smuzhiyun {
1356*4882a593Smuzhiyun struct usb_device *uhop;
1357*4882a593Smuzhiyun struct udevice *hub;
1358*4882a593Smuzhiyun int root_portnr = 0;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1361*4882a593Smuzhiyun dev->name, udev, udev->dev->name, udev->portnr);
1362*4882a593Smuzhiyun hub = udev->dev;
1363*4882a593Smuzhiyun if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1364*4882a593Smuzhiyun /* Figure out our port number on the root hub */
1365*4882a593Smuzhiyun if (usb_hub_is_root_hub(hub)) {
1366*4882a593Smuzhiyun root_portnr = udev->portnr;
1367*4882a593Smuzhiyun } else {
1368*4882a593Smuzhiyun while (!usb_hub_is_root_hub(hub->parent))
1369*4882a593Smuzhiyun hub = hub->parent;
1370*4882a593Smuzhiyun uhop = dev_get_parent_priv(hub);
1371*4882a593Smuzhiyun root_portnr = uhop->portnr;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun /*
1375*4882a593Smuzhiyun struct usb_device *hop = udev;
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun if (hop->parent)
1378*4882a593Smuzhiyun while (hop->parent->parent)
1379*4882a593Smuzhiyun hop = hop->parent;
1380*4882a593Smuzhiyun */
1381*4882a593Smuzhiyun return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1382*4882a593Smuzhiyun root_portnr);
1383*4882a593Smuzhiyun }
1384*4882a593Smuzhiyun
xhci_submit_bulk_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length)1385*4882a593Smuzhiyun static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1386*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length)
1387*4882a593Smuzhiyun {
1388*4882a593Smuzhiyun debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1389*4882a593Smuzhiyun return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun
xhci_submit_int_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1392*4882a593Smuzhiyun static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1393*4882a593Smuzhiyun unsigned long pipe, void *buffer, int length,
1394*4882a593Smuzhiyun int interval, bool nonblock)
1395*4882a593Smuzhiyun {
1396*4882a593Smuzhiyun debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1397*4882a593Smuzhiyun return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1398*4882a593Smuzhiyun nonblock);
1399*4882a593Smuzhiyun }
1400*4882a593Smuzhiyun
xhci_alloc_device(struct udevice * dev,struct usb_device * udev)1401*4882a593Smuzhiyun static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1402*4882a593Smuzhiyun {
1403*4882a593Smuzhiyun debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1404*4882a593Smuzhiyun return _xhci_alloc_device(udev);
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun
xhci_update_hub_device(struct udevice * dev,struct usb_device * udev)1407*4882a593Smuzhiyun static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun struct xhci_ctrl *ctrl = dev_get_priv(dev);
1410*4882a593Smuzhiyun struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1411*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
1412*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
1413*4882a593Smuzhiyun struct xhci_container_ctx *out_ctx;
1414*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx;
1415*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
1416*4882a593Smuzhiyun int slot_id = udev->slot_id;
1417*4882a593Smuzhiyun unsigned think_time;
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun /* Ignore root hubs */
1422*4882a593Smuzhiyun if (usb_hub_is_root_hub(udev->dev))
1423*4882a593Smuzhiyun return 0;
1424*4882a593Smuzhiyun
1425*4882a593Smuzhiyun virt_dev = ctrl->devs[slot_id];
1426*4882a593Smuzhiyun BUG_ON(!virt_dev);
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun out_ctx = virt_dev->out_ctx;
1429*4882a593Smuzhiyun in_ctx = virt_dev->in_ctx;
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1432*4882a593Smuzhiyun /* Initialize the input context control */
1433*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1434*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /* slot context */
1439*4882a593Smuzhiyun xhci_slot_copy(ctrl, in_ctx, out_ctx);
1440*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun /* Update hub related fields */
1443*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
1444*4882a593Smuzhiyun /*
1445*4882a593Smuzhiyun * refer to section 6.2.2: MTT should be 0 for full speed hub,
1446*4882a593Smuzhiyun * but it may be already set to 1 when setup an xHCI virtual
1447*4882a593Smuzhiyun * device, so clear it anyway.
1448*4882a593Smuzhiyun */
1449*4882a593Smuzhiyun if (hub->tt.multi)
1450*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1451*4882a593Smuzhiyun else if (udev->speed == USB_SPEED_FULL)
1452*4882a593Smuzhiyun slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
1453*4882a593Smuzhiyun slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1454*4882a593Smuzhiyun /*
1455*4882a593Smuzhiyun * Set TT think time - convert from ns to FS bit times.
1456*4882a593Smuzhiyun * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1457*4882a593Smuzhiyun *
1458*4882a593Smuzhiyun * 0 = 8 FS bit times, 1 = 16 FS bit times,
1459*4882a593Smuzhiyun * 2 = 24 FS bit times, 3 = 32 FS bit times.
1460*4882a593Smuzhiyun *
1461*4882a593Smuzhiyun * This field shall be 0 if the device is not a high-spped hub.
1462*4882a593Smuzhiyun */
1463*4882a593Smuzhiyun think_time = hub->tt.think_time;
1464*4882a593Smuzhiyun if (think_time != 0)
1465*4882a593Smuzhiyun think_time = (think_time / 666) - 1;
1466*4882a593Smuzhiyun if (udev->speed == USB_SPEED_HIGH)
1467*4882a593Smuzhiyun slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
1468*4882a593Smuzhiyun slot_ctx->dev_state = 0;
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun return xhci_configure_endpoints(udev, false);
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun
xhci_get_max_xfer_size(struct udevice * dev,size_t * size)1473*4882a593Smuzhiyun static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1474*4882a593Smuzhiyun {
1475*4882a593Smuzhiyun /*
1476*4882a593Smuzhiyun * xHCD allocates one segment which includes 64 TRBs for each endpoint
1477*4882a593Smuzhiyun * and the last TRB in this segment is configured as a link TRB to form
1478*4882a593Smuzhiyun * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1479*4882a593Smuzhiyun * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1480*4882a593Smuzhiyun * Hence the maximum number of TRBs we can use in one transfer is 62.
1481*4882a593Smuzhiyun */
1482*4882a593Smuzhiyun *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun return 0;
1485*4882a593Smuzhiyun }
1486*4882a593Smuzhiyun
xhci_register(struct udevice * dev,struct xhci_hccr * hccr,struct xhci_hcor * hcor)1487*4882a593Smuzhiyun int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1488*4882a593Smuzhiyun struct xhci_hcor *hcor)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun struct xhci_ctrl *ctrl = dev_get_priv(dev);
1491*4882a593Smuzhiyun struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1492*4882a593Smuzhiyun int ret;
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1495*4882a593Smuzhiyun ctrl, hccr, hcor);
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun ctrl->dev = dev;
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun /*
1500*4882a593Smuzhiyun * XHCI needs to issue a Address device command to setup
1501*4882a593Smuzhiyun * proper device context structures, before it can interact
1502*4882a593Smuzhiyun * with the device. So a get_descriptor will fail before any
1503*4882a593Smuzhiyun * of that is done for XHCI unlike EHCI.
1504*4882a593Smuzhiyun */
1505*4882a593Smuzhiyun priv->desc_before_addr = false;
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun ret = xhci_reset(hcor);
1508*4882a593Smuzhiyun if (ret)
1509*4882a593Smuzhiyun goto err;
1510*4882a593Smuzhiyun
1511*4882a593Smuzhiyun ctrl->hccr = hccr;
1512*4882a593Smuzhiyun ctrl->hcor = hcor;
1513*4882a593Smuzhiyun ret = xhci_lowlevel_init(ctrl);
1514*4882a593Smuzhiyun if (ret)
1515*4882a593Smuzhiyun goto err;
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun return 0;
1518*4882a593Smuzhiyun err:
1519*4882a593Smuzhiyun free(ctrl);
1520*4882a593Smuzhiyun debug("%s: failed, ret=%d\n", __func__, ret);
1521*4882a593Smuzhiyun return ret;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun
xhci_deregister(struct udevice * dev)1524*4882a593Smuzhiyun int xhci_deregister(struct udevice *dev)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun struct xhci_ctrl *ctrl = dev_get_priv(dev);
1527*4882a593Smuzhiyun
1528*4882a593Smuzhiyun xhci_lowlevel_stop(ctrl);
1529*4882a593Smuzhiyun xhci_cleanup(ctrl);
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun return 0;
1532*4882a593Smuzhiyun }
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun struct dm_usb_ops xhci_usb_ops = {
1535*4882a593Smuzhiyun .control = xhci_submit_control_msg,
1536*4882a593Smuzhiyun .bulk = xhci_submit_bulk_msg,
1537*4882a593Smuzhiyun .interrupt = xhci_submit_int_msg,
1538*4882a593Smuzhiyun .alloc_device = xhci_alloc_device,
1539*4882a593Smuzhiyun .update_hub_device = xhci_update_hub_device,
1540*4882a593Smuzhiyun .get_max_xfer_size = xhci_get_max_xfer_size,
1541*4882a593Smuzhiyun };
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun #endif
1544