xref: /openbmc/u-boot/drivers/usb/host/xhci.c (revision 9450ab2b)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
25853e133SVivek Gautam /*
35853e133SVivek Gautam  * USB HOST XHCI Controller stack
45853e133SVivek Gautam  *
55853e133SVivek Gautam  * Based on xHCI host controller driver in linux-kernel
65853e133SVivek Gautam  * by Sarah Sharp.
75853e133SVivek Gautam  *
85853e133SVivek Gautam  * Copyright (C) 2008 Intel Corp.
95853e133SVivek Gautam  * Author: Sarah Sharp
105853e133SVivek Gautam  *
115853e133SVivek Gautam  * Copyright (C) 2013 Samsung Electronics Co.Ltd
125853e133SVivek Gautam  * Authors: Vivek Gautam <gautam.vivek@samsung.com>
135853e133SVivek Gautam  *	    Vikas Sajjan <vikas.sajjan@samsung.com>
145853e133SVivek Gautam  */
155853e133SVivek Gautam 
165853e133SVivek Gautam /**
175853e133SVivek Gautam  * This file gives the xhci stack for usb3.0 looking into
185853e133SVivek Gautam  * xhci specification Rev1.0 (5/21/10).
195853e133SVivek Gautam  * The quirk devices support hasn't been given yet.
205853e133SVivek Gautam  */
215853e133SVivek Gautam 
225853e133SVivek Gautam #include <common.h>
23a5762fe0SSimon Glass #include <dm.h>
245853e133SVivek Gautam #include <asm/byteorder.h>
255853e133SVivek Gautam #include <usb.h>
265853e133SVivek Gautam #include <malloc.h>
275853e133SVivek Gautam #include <watchdog.h>
285853e133SVivek Gautam #include <asm/cache.h>
295853e133SVivek Gautam #include <asm/unaligned.h>
305d97dff0SMasahiro Yamada #include <linux/errno.h>
315853e133SVivek Gautam #include "xhci.h"
325853e133SVivek Gautam 
335853e133SVivek Gautam #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
345853e133SVivek Gautam #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
355853e133SVivek Gautam #endif
365853e133SVivek Gautam 
375853e133SVivek Gautam static struct descriptor {
385853e133SVivek Gautam 	struct usb_hub_descriptor hub;
395853e133SVivek Gautam 	struct usb_device_descriptor device;
405853e133SVivek Gautam 	struct usb_config_descriptor config;
415853e133SVivek Gautam 	struct usb_interface_descriptor interface;
425853e133SVivek Gautam 	struct usb_endpoint_descriptor endpoint;
435853e133SVivek Gautam 	struct usb_ss_ep_comp_descriptor ep_companion;
445853e133SVivek Gautam } __attribute__ ((packed)) descriptor = {
455853e133SVivek Gautam 	{
465853e133SVivek Gautam 		0xc,		/* bDescLength */
475853e133SVivek Gautam 		0x2a,		/* bDescriptorType: hub descriptor */
485853e133SVivek Gautam 		2,		/* bNrPorts -- runtime modified */
495853e133SVivek Gautam 		cpu_to_le16(0x8), /* wHubCharacteristics */
505853e133SVivek Gautam 		10,		/* bPwrOn2PwrGood */
515853e133SVivek Gautam 		0,		/* bHubCntrCurrent */
52337fc7e6SBin Meng 		{		/* Device removable */
53337fc7e6SBin Meng 		}		/* at most 7 ports! XXX */
545853e133SVivek Gautam 	},
555853e133SVivek Gautam 	{
565853e133SVivek Gautam 		0x12,		/* bLength */
575853e133SVivek Gautam 		1,		/* bDescriptorType: UDESC_DEVICE */
585853e133SVivek Gautam 		cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
595853e133SVivek Gautam 		9,		/* bDeviceClass: UDCLASS_HUB */
605853e133SVivek Gautam 		0,		/* bDeviceSubClass: UDSUBCLASS_HUB */
615853e133SVivek Gautam 		3,		/* bDeviceProtocol: UDPROTO_SSHUBSTT */
625853e133SVivek Gautam 		9,		/* bMaxPacketSize: 512 bytes  2^9 */
635853e133SVivek Gautam 		0x0000,		/* idVendor */
645853e133SVivek Gautam 		0x0000,		/* idProduct */
655853e133SVivek Gautam 		cpu_to_le16(0x0100), /* bcdDevice */
665853e133SVivek Gautam 		1,		/* iManufacturer */
675853e133SVivek Gautam 		2,		/* iProduct */
685853e133SVivek Gautam 		0,		/* iSerialNumber */
695853e133SVivek Gautam 		1		/* bNumConfigurations: 1 */
705853e133SVivek Gautam 	},
715853e133SVivek Gautam 	{
725853e133SVivek Gautam 		0x9,
735853e133SVivek Gautam 		2,		/* bDescriptorType: UDESC_CONFIG */
745853e133SVivek Gautam 		cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
755853e133SVivek Gautam 		1,		/* bNumInterface */
765853e133SVivek Gautam 		1,		/* bConfigurationValue */
775853e133SVivek Gautam 		0,		/* iConfiguration */
785853e133SVivek Gautam 		0x40,		/* bmAttributes: UC_SELF_POWER */
795853e133SVivek Gautam 		0		/* bMaxPower */
805853e133SVivek Gautam 	},
815853e133SVivek Gautam 	{
825853e133SVivek Gautam 		0x9,		/* bLength */
835853e133SVivek Gautam 		4,		/* bDescriptorType: UDESC_INTERFACE */
845853e133SVivek Gautam 		0,		/* bInterfaceNumber */
855853e133SVivek Gautam 		0,		/* bAlternateSetting */
865853e133SVivek Gautam 		1,		/* bNumEndpoints */
875853e133SVivek Gautam 		9,		/* bInterfaceClass: UICLASS_HUB */
885853e133SVivek Gautam 		0,		/* bInterfaceSubClass: UISUBCLASS_HUB */
895853e133SVivek Gautam 		0,		/* bInterfaceProtocol: UIPROTO_HSHUBSTT */
905853e133SVivek Gautam 		0		/* iInterface */
915853e133SVivek Gautam 	},
925853e133SVivek Gautam 	{
935853e133SVivek Gautam 		0x7,		/* bLength */
945853e133SVivek Gautam 		5,		/* bDescriptorType: UDESC_ENDPOINT */
955853e133SVivek Gautam 		0x81,		/* bEndpointAddress: IN endpoint 1 */
965853e133SVivek Gautam 		3,		/* bmAttributes: UE_INTERRUPT */
975853e133SVivek Gautam 		8,		/* wMaxPacketSize */
985853e133SVivek Gautam 		255		/* bInterval */
995853e133SVivek Gautam 	},
1005853e133SVivek Gautam 	{
1015853e133SVivek Gautam 		0x06,		/* ss_bLength */
1025853e133SVivek Gautam 		0x30,		/* ss_bDescriptorType: SS EP Companion */
1035853e133SVivek Gautam 		0x00,		/* ss_bMaxBurst: allows 1 TX between ACKs */
1045853e133SVivek Gautam 		/* ss_bmAttributes: 1 packet per service interval */
1055853e133SVivek Gautam 		0x00,
1065853e133SVivek Gautam 		/* ss_wBytesPerInterval: 15 bits for max 15 ports */
1075853e133SVivek Gautam 		cpu_to_le16(0x02),
1085853e133SVivek Gautam 	},
1095853e133SVivek Gautam };
1105853e133SVivek Gautam 
111*fd09c205SSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
1125853e133SVivek Gautam static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
113a5762fe0SSimon Glass #endif
1145853e133SVivek Gautam 
xhci_get_ctrl(struct usb_device * udev)1157c1deec0SSimon Glass struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
1167c1deec0SSimon Glass {
117*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
118a5762fe0SSimon Glass 	struct udevice *dev;
119a5762fe0SSimon Glass 
120a5762fe0SSimon Glass 	/* Find the USB controller */
121a5762fe0SSimon Glass 	for (dev = udev->dev;
122a5762fe0SSimon Glass 	     device_get_uclass_id(dev) != UCLASS_USB;
123a5762fe0SSimon Glass 	     dev = dev->parent)
124a5762fe0SSimon Glass 		;
125a5762fe0SSimon Glass 	return dev_get_priv(dev);
126a5762fe0SSimon Glass #else
1277c1deec0SSimon Glass 	return udev->controller;
128a5762fe0SSimon Glass #endif
1297c1deec0SSimon Glass }
1307c1deec0SSimon Glass 
1315853e133SVivek Gautam /**
1325853e133SVivek Gautam  * Waits for as per specified amount of time
1335853e133SVivek Gautam  * for the "result" to match with "done"
1345853e133SVivek Gautam  *
1355853e133SVivek Gautam  * @param ptr	pointer to the register to be read
1365853e133SVivek Gautam  * @param mask	mask for the value read
1375853e133SVivek Gautam  * @param done	value to be campared with result
1385853e133SVivek Gautam  * @param usec	time to wait till
1395853e133SVivek Gautam  * @return 0 if handshake is success else < 0 on failure
1405853e133SVivek Gautam  */
handshake(uint32_t volatile * ptr,uint32_t mask,uint32_t done,int usec)1415853e133SVivek Gautam static int handshake(uint32_t volatile *ptr, uint32_t mask,
1425853e133SVivek Gautam 					uint32_t done, int usec)
1435853e133SVivek Gautam {
1445853e133SVivek Gautam 	uint32_t result;
1455853e133SVivek Gautam 
1465853e133SVivek Gautam 	do {
1475853e133SVivek Gautam 		result = xhci_readl(ptr);
1485853e133SVivek Gautam 		if (result == ~(uint32_t)0)
1495853e133SVivek Gautam 			return -ENODEV;
1505853e133SVivek Gautam 		result &= mask;
1515853e133SVivek Gautam 		if (result == done)
1525853e133SVivek Gautam 			return 0;
1535853e133SVivek Gautam 		usec--;
1545853e133SVivek Gautam 		udelay(1);
1555853e133SVivek Gautam 	} while (usec > 0);
1565853e133SVivek Gautam 
1575853e133SVivek Gautam 	return -ETIMEDOUT;
1585853e133SVivek Gautam }
1595853e133SVivek Gautam 
1605853e133SVivek Gautam /**
1615853e133SVivek Gautam  * Set the run bit and wait for the host to be running.
1625853e133SVivek Gautam  *
1635853e133SVivek Gautam  * @param hcor	pointer to host controller operation registers
1645853e133SVivek Gautam  * @return status of the Handshake
1655853e133SVivek Gautam  */
xhci_start(struct xhci_hcor * hcor)1665853e133SVivek Gautam static int xhci_start(struct xhci_hcor *hcor)
1675853e133SVivek Gautam {
1685853e133SVivek Gautam 	u32 temp;
1695853e133SVivek Gautam 	int ret;
1705853e133SVivek Gautam 
1715853e133SVivek Gautam 	puts("Starting the controller\n");
1725853e133SVivek Gautam 	temp = xhci_readl(&hcor->or_usbcmd);
1735853e133SVivek Gautam 	temp |= (CMD_RUN);
1745853e133SVivek Gautam 	xhci_writel(&hcor->or_usbcmd, temp);
1755853e133SVivek Gautam 
1765853e133SVivek Gautam 	/*
1775853e133SVivek Gautam 	 * Wait for the HCHalted Status bit to be 0 to indicate the host is
1785853e133SVivek Gautam 	 * running.
1795853e133SVivek Gautam 	 */
1805853e133SVivek Gautam 	ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
1815853e133SVivek Gautam 	if (ret)
1825853e133SVivek Gautam 		debug("Host took too long to start, "
1835853e133SVivek Gautam 				"waited %u microseconds.\n",
1845853e133SVivek Gautam 				XHCI_MAX_HALT_USEC);
1855853e133SVivek Gautam 	return ret;
1865853e133SVivek Gautam }
1875853e133SVivek Gautam 
1885853e133SVivek Gautam /**
1895853e133SVivek Gautam  * Resets the XHCI Controller
1905853e133SVivek Gautam  *
1915853e133SVivek Gautam  * @param hcor	pointer to host controller operation registers
1925853e133SVivek Gautam  * @return -EBUSY if XHCI Controller is not halted else status of handshake
1935853e133SVivek Gautam  */
xhci_reset(struct xhci_hcor * hcor)194121a4d13SMasahiro Yamada static int xhci_reset(struct xhci_hcor *hcor)
1955853e133SVivek Gautam {
1965853e133SVivek Gautam 	u32 cmd;
1975853e133SVivek Gautam 	u32 state;
1985853e133SVivek Gautam 	int ret;
1995853e133SVivek Gautam 
2005853e133SVivek Gautam 	/* Halting the Host first */
201a5ccda47SSergey Temerkhanov 	debug("// Halt the HC: %p\n", hcor);
2025853e133SVivek Gautam 	state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
2035853e133SVivek Gautam 	if (!state) {
2045853e133SVivek Gautam 		cmd = xhci_readl(&hcor->or_usbcmd);
2055853e133SVivek Gautam 		cmd &= ~CMD_RUN;
2065853e133SVivek Gautam 		xhci_writel(&hcor->or_usbcmd, cmd);
2075853e133SVivek Gautam 	}
2085853e133SVivek Gautam 
2095853e133SVivek Gautam 	ret = handshake(&hcor->or_usbsts,
2105853e133SVivek Gautam 			STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
2115853e133SVivek Gautam 	if (ret) {
2125853e133SVivek Gautam 		printf("Host not halted after %u microseconds.\n",
2135853e133SVivek Gautam 				XHCI_MAX_HALT_USEC);
2145853e133SVivek Gautam 		return -EBUSY;
2155853e133SVivek Gautam 	}
2165853e133SVivek Gautam 
2175853e133SVivek Gautam 	debug("// Reset the HC\n");
2185853e133SVivek Gautam 	cmd = xhci_readl(&hcor->or_usbcmd);
2195853e133SVivek Gautam 	cmd |= CMD_RESET;
2205853e133SVivek Gautam 	xhci_writel(&hcor->or_usbcmd, cmd);
2215853e133SVivek Gautam 
2225853e133SVivek Gautam 	ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
2235853e133SVivek Gautam 	if (ret)
2245853e133SVivek Gautam 		return ret;
2255853e133SVivek Gautam 
2265853e133SVivek Gautam 	/*
2275853e133SVivek Gautam 	 * xHCI cannot write to any doorbells or operational registers other
2285853e133SVivek Gautam 	 * than status until the "Controller Not Ready" flag is cleared.
2295853e133SVivek Gautam 	 */
2305853e133SVivek Gautam 	return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
2315853e133SVivek Gautam }
2325853e133SVivek Gautam 
2335853e133SVivek Gautam /**
2345853e133SVivek Gautam  * Used for passing endpoint bitmasks between the core and HCDs.
2355853e133SVivek Gautam  * Find the index for an endpoint given its descriptor.
2365853e133SVivek Gautam  * Use the return value to right shift 1 for the bitmask.
2375853e133SVivek Gautam  *
2385853e133SVivek Gautam  * Index  = (epnum * 2) + direction - 1,
2395853e133SVivek Gautam  * where direction = 0 for OUT, 1 for IN.
2405853e133SVivek Gautam  * For control endpoints, the IN index is used (OUT index is unused), so
2415853e133SVivek Gautam  * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
2425853e133SVivek Gautam  *
2435853e133SVivek Gautam  * @param desc	USB enpdoint Descriptor
2445853e133SVivek Gautam  * @return index of the Endpoint
2455853e133SVivek Gautam  */
xhci_get_ep_index(struct usb_endpoint_descriptor * desc)2465853e133SVivek Gautam static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
2475853e133SVivek Gautam {
2485853e133SVivek Gautam 	unsigned int index;
2495853e133SVivek Gautam 
2505853e133SVivek Gautam 	if (usb_endpoint_xfer_control(desc))
2515853e133SVivek Gautam 		index = (unsigned int)(usb_endpoint_num(desc) * 2);
2525853e133SVivek Gautam 	else
2535853e133SVivek Gautam 		index = (unsigned int)((usb_endpoint_num(desc) * 2) -
2545853e133SVivek Gautam 				(usb_endpoint_dir_in(desc) ? 0 : 1));
2555853e133SVivek Gautam 
2565853e133SVivek Gautam 	return index;
2575853e133SVivek Gautam }
2585853e133SVivek Gautam 
259f51966bfSBin Meng /*
260f51966bfSBin Meng  * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
261f51966bfSBin Meng  * microframes, rounded down to nearest power of 2.
262f51966bfSBin Meng  */
xhci_microframes_to_exponent(unsigned int desc_interval,unsigned int min_exponent,unsigned int max_exponent)263f51966bfSBin Meng static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
264f51966bfSBin Meng 						 unsigned int min_exponent,
265f51966bfSBin Meng 						 unsigned int max_exponent)
266f51966bfSBin Meng {
267f51966bfSBin Meng 	unsigned int interval;
268f51966bfSBin Meng 
269f51966bfSBin Meng 	interval = fls(desc_interval) - 1;
270f51966bfSBin Meng 	interval = clamp_val(interval, min_exponent, max_exponent);
271f51966bfSBin Meng 	if ((1 << interval) != desc_interval)
272f51966bfSBin Meng 		debug("rounding interval to %d microframes, "\
273f51966bfSBin Meng 		      "ep desc says %d microframes\n",
274f51966bfSBin Meng 		      1 << interval, desc_interval);
275f51966bfSBin Meng 
276f51966bfSBin Meng 	return interval;
277f51966bfSBin Meng }
278f51966bfSBin Meng 
xhci_parse_microframe_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)279f51966bfSBin Meng static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
280f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc)
281f51966bfSBin Meng {
282f51966bfSBin Meng 	if (endpt_desc->bInterval == 0)
283f51966bfSBin Meng 		return 0;
284f51966bfSBin Meng 
285f51966bfSBin Meng 	return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
286f51966bfSBin Meng }
287f51966bfSBin Meng 
xhci_parse_frame_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)288f51966bfSBin Meng static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
289f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc)
290f51966bfSBin Meng {
291f51966bfSBin Meng 	return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
292f51966bfSBin Meng }
293f51966bfSBin Meng 
294f51966bfSBin Meng /*
295f51966bfSBin Meng  * Convert interval expressed as 2^(bInterval - 1) == interval into
296f51966bfSBin Meng  * straight exponent value 2^n == interval.
297f51966bfSBin Meng  */
xhci_parse_exponent_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)298f51966bfSBin Meng static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
299f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc)
300f51966bfSBin Meng {
301f51966bfSBin Meng 	unsigned int interval;
302f51966bfSBin Meng 
303f51966bfSBin Meng 	interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
304f51966bfSBin Meng 	if (interval != endpt_desc->bInterval - 1)
305f51966bfSBin Meng 		debug("ep %#x - rounding interval to %d %sframes\n",
306f51966bfSBin Meng 		      endpt_desc->bEndpointAddress, 1 << interval,
307f51966bfSBin Meng 		      udev->speed == USB_SPEED_FULL ? "" : "micro");
308f51966bfSBin Meng 
309f51966bfSBin Meng 	if (udev->speed == USB_SPEED_FULL) {
310f51966bfSBin Meng 		/*
311f51966bfSBin Meng 		 * Full speed isoc endpoints specify interval in frames,
312f51966bfSBin Meng 		 * not microframes. We are using microframes everywhere,
313f51966bfSBin Meng 		 * so adjust accordingly.
314f51966bfSBin Meng 		 */
315f51966bfSBin Meng 		interval += 3;	/* 1 frame = 2^3 uframes */
316f51966bfSBin Meng 	}
317f51966bfSBin Meng 
318f51966bfSBin Meng 	return interval;
319f51966bfSBin Meng }
320f51966bfSBin Meng 
321f51966bfSBin Meng /*
322f51966bfSBin Meng  * Return the polling or NAK interval.
323f51966bfSBin Meng  *
324f51966bfSBin Meng  * The polling interval is expressed in "microframes". If xHCI's Interval field
325f51966bfSBin Meng  * is set to N, it will service the endpoint every 2^(Interval)*125us.
326f51966bfSBin Meng  *
327f51966bfSBin Meng  * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
328f51966bfSBin Meng  * is set to 0.
329f51966bfSBin Meng  */
xhci_get_endpoint_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)330f51966bfSBin Meng static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
331f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc)
332f51966bfSBin Meng {
333f51966bfSBin Meng 	unsigned int interval = 0;
334f51966bfSBin Meng 
335f51966bfSBin Meng 	switch (udev->speed) {
336f51966bfSBin Meng 	case USB_SPEED_HIGH:
337f51966bfSBin Meng 		/* Max NAK rate */
338f51966bfSBin Meng 		if (usb_endpoint_xfer_control(endpt_desc) ||
339f51966bfSBin Meng 		    usb_endpoint_xfer_bulk(endpt_desc)) {
340f51966bfSBin Meng 			interval = xhci_parse_microframe_interval(udev,
341f51966bfSBin Meng 								  endpt_desc);
342f51966bfSBin Meng 			break;
343f51966bfSBin Meng 		}
344f51966bfSBin Meng 		/* Fall through - SS and HS isoc/int have same decoding */
345f51966bfSBin Meng 
346f51966bfSBin Meng 	case USB_SPEED_SUPER:
347f51966bfSBin Meng 		if (usb_endpoint_xfer_int(endpt_desc) ||
348f51966bfSBin Meng 		    usb_endpoint_xfer_isoc(endpt_desc)) {
349f51966bfSBin Meng 			interval = xhci_parse_exponent_interval(udev,
350f51966bfSBin Meng 								endpt_desc);
351f51966bfSBin Meng 		}
352f51966bfSBin Meng 		break;
353f51966bfSBin Meng 
354f51966bfSBin Meng 	case USB_SPEED_FULL:
355f51966bfSBin Meng 		if (usb_endpoint_xfer_isoc(endpt_desc)) {
356f51966bfSBin Meng 			interval = xhci_parse_exponent_interval(udev,
357f51966bfSBin Meng 								endpt_desc);
358f51966bfSBin Meng 			break;
359f51966bfSBin Meng 		}
360f51966bfSBin Meng 		/*
361f51966bfSBin Meng 		 * Fall through for interrupt endpoint interval decoding
362f51966bfSBin Meng 		 * since it uses the same rules as low speed interrupt
363f51966bfSBin Meng 		 * endpoints.
364f51966bfSBin Meng 		 */
365f51966bfSBin Meng 
366f51966bfSBin Meng 	case USB_SPEED_LOW:
367f51966bfSBin Meng 		if (usb_endpoint_xfer_int(endpt_desc) ||
368f51966bfSBin Meng 		    usb_endpoint_xfer_isoc(endpt_desc)) {
369f51966bfSBin Meng 			interval = xhci_parse_frame_interval(udev, endpt_desc);
370f51966bfSBin Meng 		}
371f51966bfSBin Meng 		break;
372f51966bfSBin Meng 
373f51966bfSBin Meng 	default:
374f51966bfSBin Meng 		BUG();
375f51966bfSBin Meng 	}
376f51966bfSBin Meng 
377f51966bfSBin Meng 	return interval;
378f51966bfSBin Meng }
379f51966bfSBin Meng 
380f51966bfSBin Meng /*
381f51966bfSBin Meng  * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
382f51966bfSBin Meng  * High speed endpoint descriptors can define "the number of additional
383f51966bfSBin Meng  * transaction opportunities per microframe", but that goes in the Max Burst
384f51966bfSBin Meng  * endpoint context field.
385f51966bfSBin Meng  */
xhci_get_endpoint_mult(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc,struct usb_ss_ep_comp_descriptor * ss_ep_comp_desc)386f51966bfSBin Meng static u32 xhci_get_endpoint_mult(struct usb_device *udev,
387f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc,
388f51966bfSBin Meng 	struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
389f51966bfSBin Meng {
390f51966bfSBin Meng 	if (udev->speed < USB_SPEED_SUPER ||
391f51966bfSBin Meng 	    !usb_endpoint_xfer_isoc(endpt_desc))
392f51966bfSBin Meng 		return 0;
393f51966bfSBin Meng 
394f51966bfSBin Meng 	return ss_ep_comp_desc->bmAttributes;
395f51966bfSBin Meng }
396f51966bfSBin Meng 
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)397fa483b2cSBin Meng static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
398fa483b2cSBin Meng 	struct usb_endpoint_descriptor *endpt_desc,
399fa483b2cSBin Meng 	struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
400fa483b2cSBin Meng {
401fa483b2cSBin Meng 	/* Super speed and Plus have max burst in ep companion desc */
402fa483b2cSBin Meng 	if (udev->speed >= USB_SPEED_SUPER)
403fa483b2cSBin Meng 		return ss_ep_comp_desc->bMaxBurst;
404fa483b2cSBin Meng 
405fa483b2cSBin Meng 	if (udev->speed == USB_SPEED_HIGH &&
406fa483b2cSBin Meng 	    (usb_endpoint_xfer_isoc(endpt_desc) ||
407fa483b2cSBin Meng 	     usb_endpoint_xfer_int(endpt_desc)))
408fa483b2cSBin Meng 		return usb_endpoint_maxp_mult(endpt_desc) - 1;
409fa483b2cSBin Meng 
410fa483b2cSBin Meng 	return 0;
411fa483b2cSBin Meng }
412fa483b2cSBin Meng 
413f51966bfSBin Meng /*
414f51966bfSBin Meng  * Return the maximum endpoint service interval time (ESIT) payload.
415f51966bfSBin Meng  * Basically, this is the maxpacket size, multiplied by the burst size
416f51966bfSBin Meng  * and mult size.
417f51966bfSBin Meng  */
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)418f51966bfSBin Meng static u32 xhci_get_max_esit_payload(struct usb_device *udev,
419f51966bfSBin Meng 	struct usb_endpoint_descriptor *endpt_desc,
420f51966bfSBin Meng 	struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
421f51966bfSBin Meng {
422f51966bfSBin Meng 	int max_burst;
423f51966bfSBin Meng 	int max_packet;
424f51966bfSBin Meng 
425f51966bfSBin Meng 	/* Only applies for interrupt or isochronous endpoints */
426f51966bfSBin Meng 	if (usb_endpoint_xfer_control(endpt_desc) ||
427f51966bfSBin Meng 	    usb_endpoint_xfer_bulk(endpt_desc))
428f51966bfSBin Meng 		return 0;
429f51966bfSBin Meng 
430f51966bfSBin Meng 	/* SuperSpeed Isoc ep with less than 48k per esit */
431f51966bfSBin Meng 	if (udev->speed >= USB_SPEED_SUPER)
432f51966bfSBin Meng 		return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
433f51966bfSBin Meng 
434f51966bfSBin Meng 	max_packet = usb_endpoint_maxp(endpt_desc);
435f51966bfSBin Meng 	max_burst = usb_endpoint_maxp_mult(endpt_desc);
436f51966bfSBin Meng 
437f51966bfSBin Meng 	/* A 0 in max burst means 1 transfer per ESIT */
438f51966bfSBin Meng 	return max_packet * max_burst;
439f51966bfSBin Meng }
440f51966bfSBin Meng 
4415853e133SVivek Gautam /**
4425853e133SVivek Gautam  * Issue a configure endpoint command or evaluate context command
4435853e133SVivek Gautam  * and wait for it to finish.
4445853e133SVivek Gautam  *
4455853e133SVivek Gautam  * @param udev	pointer to the Device Data Structure
4465853e133SVivek Gautam  * @param ctx_change	flag to indicate the Context has changed or NOT
4475853e133SVivek Gautam  * @return 0 on success, -1 on failure
4485853e133SVivek Gautam  */
xhci_configure_endpoints(struct usb_device * udev,bool ctx_change)4495853e133SVivek Gautam static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
4505853e133SVivek Gautam {
4515853e133SVivek Gautam 	struct xhci_container_ctx *in_ctx;
4525853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
4537c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
4545853e133SVivek Gautam 	union xhci_trb *event;
4555853e133SVivek Gautam 
4565853e133SVivek Gautam 	virt_dev = ctrl->devs[udev->slot_id];
4575853e133SVivek Gautam 	in_ctx = virt_dev->in_ctx;
4585853e133SVivek Gautam 
459421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
4605853e133SVivek Gautam 	xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
4615853e133SVivek Gautam 			   ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
4625853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
4635853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
4645853e133SVivek Gautam 		!= udev->slot_id);
4655853e133SVivek Gautam 
4665853e133SVivek Gautam 	switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
4675853e133SVivek Gautam 	case COMP_SUCCESS:
4685853e133SVivek Gautam 		debug("Successful %s command\n",
4695853e133SVivek Gautam 			ctx_change ? "Evaluate Context" : "Configure Endpoint");
4705853e133SVivek Gautam 		break;
4715853e133SVivek Gautam 	default:
4725853e133SVivek Gautam 		printf("ERROR: %s command returned completion code %d.\n",
4735853e133SVivek Gautam 			ctx_change ? "Evaluate Context" : "Configure Endpoint",
4745853e133SVivek Gautam 			GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
4755853e133SVivek Gautam 		return -EINVAL;
4765853e133SVivek Gautam 	}
4775853e133SVivek Gautam 
4785853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
4795853e133SVivek Gautam 
4805853e133SVivek Gautam 	return 0;
4815853e133SVivek Gautam }
4825853e133SVivek Gautam 
4835853e133SVivek Gautam /**
4845853e133SVivek Gautam  * Configure the endpoint, programming the device contexts.
4855853e133SVivek Gautam  *
4865853e133SVivek Gautam  * @param udev	pointer to the USB device structure
4875853e133SVivek Gautam  * @return returns the status of the xhci_configure_endpoints
4885853e133SVivek Gautam  */
xhci_set_configuration(struct usb_device * udev)4895853e133SVivek Gautam static int xhci_set_configuration(struct usb_device *udev)
4905853e133SVivek Gautam {
4915853e133SVivek Gautam 	struct xhci_container_ctx *in_ctx;
4925853e133SVivek Gautam 	struct xhci_container_ctx *out_ctx;
4935853e133SVivek Gautam 	struct xhci_input_control_ctx *ctrl_ctx;
4945853e133SVivek Gautam 	struct xhci_slot_ctx *slot_ctx;
4955853e133SVivek Gautam 	struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
4965853e133SVivek Gautam 	int cur_ep;
4975853e133SVivek Gautam 	int max_ep_flag = 0;
4985853e133SVivek Gautam 	int ep_index;
4995853e133SVivek Gautam 	unsigned int dir;
5005853e133SVivek Gautam 	unsigned int ep_type;
5017c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
5025853e133SVivek Gautam 	int num_of_ep;
5035853e133SVivek Gautam 	int ep_flag = 0;
5045853e133SVivek Gautam 	u64 trb_64 = 0;
5055853e133SVivek Gautam 	int slot_id = udev->slot_id;
5065853e133SVivek Gautam 	struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
5075853e133SVivek Gautam 	struct usb_interface *ifdesc;
508f51966bfSBin Meng 	u32 max_esit_payload;
509f51966bfSBin Meng 	unsigned int interval;
510f51966bfSBin Meng 	unsigned int mult;
511fa483b2cSBin Meng 	unsigned int max_burst;
512f51966bfSBin Meng 	unsigned int avg_trb_len;
513ab2b727dSBin Meng 	unsigned int err_count = 0;
5145853e133SVivek Gautam 
5155853e133SVivek Gautam 	out_ctx = virt_dev->out_ctx;
5165853e133SVivek Gautam 	in_ctx = virt_dev->in_ctx;
5175853e133SVivek Gautam 
5185853e133SVivek Gautam 	num_of_ep = udev->config.if_desc[0].no_of_ep;
5195853e133SVivek Gautam 	ifdesc = &udev->config.if_desc[0];
5205853e133SVivek Gautam 
5215853e133SVivek Gautam 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
522aab0db08SBin Meng 	/* Initialize the input context control */
523aab0db08SBin Meng 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
5245853e133SVivek Gautam 	ctrl_ctx->drop_flags = 0;
5255853e133SVivek Gautam 
5265853e133SVivek Gautam 	/* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
5275853e133SVivek Gautam 	for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
5285853e133SVivek Gautam 		ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
5295853e133SVivek Gautam 		ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
5305853e133SVivek Gautam 		if (max_ep_flag < ep_flag)
5315853e133SVivek Gautam 			max_ep_flag = ep_flag;
5325853e133SVivek Gautam 	}
5335853e133SVivek Gautam 
534421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
5355853e133SVivek Gautam 
5365853e133SVivek Gautam 	/* slot context */
5375853e133SVivek Gautam 	xhci_slot_copy(ctrl, in_ctx, out_ctx);
5385853e133SVivek Gautam 	slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
539e4040660SBin Meng 	slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
5405853e133SVivek Gautam 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
5415853e133SVivek Gautam 
5425853e133SVivek Gautam 	xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
5435853e133SVivek Gautam 
5445853e133SVivek Gautam 	/* filling up ep contexts */
5455853e133SVivek Gautam 	for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
5465853e133SVivek Gautam 		struct usb_endpoint_descriptor *endpt_desc = NULL;
547f51966bfSBin Meng 		struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
5485853e133SVivek Gautam 
5495853e133SVivek Gautam 		endpt_desc = &ifdesc->ep_desc[cur_ep];
550f51966bfSBin Meng 		ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
5515853e133SVivek Gautam 		trb_64 = 0;
5525853e133SVivek Gautam 
553f51966bfSBin Meng 		/*
554f51966bfSBin Meng 		 * Get values to fill the endpoint context, mostly from ep
555f51966bfSBin Meng 		 * descriptor. The average TRB buffer lengt for bulk endpoints
556f51966bfSBin Meng 		 * is unclear as we have no clue on scatter gather list entry
557f51966bfSBin Meng 		 * size. For Isoc and Int, set it to max available.
558f51966bfSBin Meng 		 * See xHCI 1.1 spec 4.14.1.1 for details.
559f51966bfSBin Meng 		 */
560f51966bfSBin Meng 		max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
561f51966bfSBin Meng 							     ss_ep_comp_desc);
562f51966bfSBin Meng 		interval = xhci_get_endpoint_interval(udev, endpt_desc);
563f51966bfSBin Meng 		mult = xhci_get_endpoint_mult(udev, endpt_desc,
564f51966bfSBin Meng 					      ss_ep_comp_desc);
565fa483b2cSBin Meng 		max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
566fa483b2cSBin Meng 							ss_ep_comp_desc);
567f51966bfSBin Meng 		avg_trb_len = max_esit_payload;
568f51966bfSBin Meng 
5695853e133SVivek Gautam 		ep_index = xhci_get_ep_index(endpt_desc);
5705853e133SVivek Gautam 		ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
5715853e133SVivek Gautam 
5725853e133SVivek Gautam 		/* Allocate the ep rings */
5735853e133SVivek Gautam 		virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
5745853e133SVivek Gautam 		if (!virt_dev->eps[ep_index].ring)
5755853e133SVivek Gautam 			return -ENOMEM;
5765853e133SVivek Gautam 
5775853e133SVivek Gautam 		/*NOTE: ep_desc[0] actually represents EP1 and so on */
5785853e133SVivek Gautam 		dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
5795853e133SVivek Gautam 		ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
580f51966bfSBin Meng 
581f51966bfSBin Meng 		ep_ctx[ep_index]->ep_info =
582f51966bfSBin Meng 			cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
583f51966bfSBin Meng 			EP_INTERVAL(interval) | EP_MULT(mult));
584f51966bfSBin Meng 
5855853e133SVivek Gautam 		ep_ctx[ep_index]->ep_info2 =
5865853e133SVivek Gautam 			cpu_to_le32(ep_type << EP_TYPE_SHIFT);
5875853e133SVivek Gautam 		ep_ctx[ep_index]->ep_info2 |=
5885853e133SVivek Gautam 			cpu_to_le32(MAX_PACKET
5895853e133SVivek Gautam 			(get_unaligned(&endpt_desc->wMaxPacketSize)));
5905853e133SVivek Gautam 
591ab2b727dSBin Meng 		/* Allow 3 retries for everything but isoc, set CErr = 3 */
592ab2b727dSBin Meng 		if (!usb_endpoint_xfer_isoc(endpt_desc))
593ab2b727dSBin Meng 			err_count = 3;
5945853e133SVivek Gautam 		ep_ctx[ep_index]->ep_info2 |=
595fa483b2cSBin Meng 			cpu_to_le32(MAX_BURST(max_burst) |
596ab2b727dSBin Meng 			ERROR_COUNT(err_count));
5975853e133SVivek Gautam 
5985853e133SVivek Gautam 		trb_64 = (uintptr_t)
5995853e133SVivek Gautam 				virt_dev->eps[ep_index].ring->enqueue;
6005853e133SVivek Gautam 		ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
6015853e133SVivek Gautam 				virt_dev->eps[ep_index].ring->cycle_state);
602f51966bfSBin Meng 
603fae35857SBin Meng 		/*
604fae35857SBin Meng 		 * xHCI spec 6.2.3:
605fae35857SBin Meng 		 * 'Average TRB Length' should be 8 for control endpoints.
606fae35857SBin Meng 		 */
607fae35857SBin Meng 		if (usb_endpoint_xfer_control(endpt_desc))
608fae35857SBin Meng 			avg_trb_len = 8;
609f51966bfSBin Meng 		ep_ctx[ep_index]->tx_info =
610f51966bfSBin Meng 			cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
611f51966bfSBin Meng 			EP_AVG_TRB_LENGTH(avg_trb_len));
6125853e133SVivek Gautam 	}
6135853e133SVivek Gautam 
6145853e133SVivek Gautam 	return xhci_configure_endpoints(udev, false);
6155853e133SVivek Gautam }
6165853e133SVivek Gautam 
6175853e133SVivek Gautam /**
6185853e133SVivek Gautam  * Issue an Address Device command (which will issue a SetAddress request to
6195853e133SVivek Gautam  * the device).
6205853e133SVivek Gautam  *
6215853e133SVivek Gautam  * @param udev pointer to the Device Data Structure
6225853e133SVivek Gautam  * @return 0 if successful else error code on failure
6235853e133SVivek Gautam  */
xhci_address_device(struct usb_device * udev,int root_portnr)6245dd75e3bSSimon Glass static int xhci_address_device(struct usb_device *udev, int root_portnr)
6255853e133SVivek Gautam {
6265853e133SVivek Gautam 	int ret = 0;
6277c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
6285853e133SVivek Gautam 	struct xhci_slot_ctx *slot_ctx;
6295853e133SVivek Gautam 	struct xhci_input_control_ctx *ctrl_ctx;
6305853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
6315853e133SVivek Gautam 	int slot_id = udev->slot_id;
6325853e133SVivek Gautam 	union xhci_trb *event;
6335853e133SVivek Gautam 
6345853e133SVivek Gautam 	virt_dev = ctrl->devs[slot_id];
6355853e133SVivek Gautam 
6365853e133SVivek Gautam 	/*
6375853e133SVivek Gautam 	 * This is the first Set Address since device plug-in
6385853e133SVivek Gautam 	 * so setting up the slot context.
6395853e133SVivek Gautam 	 */
6405dd75e3bSSimon Glass 	debug("Setting up addressable devices %p\n", ctrl->dcbaa);
641daec4691SBin Meng 	xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
6425853e133SVivek Gautam 
6435853e133SVivek Gautam 	ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
6445853e133SVivek Gautam 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
6455853e133SVivek Gautam 	ctrl_ctx->drop_flags = 0;
6465853e133SVivek Gautam 
6475853e133SVivek Gautam 	xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
6485853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
6495853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
6505853e133SVivek Gautam 
6515853e133SVivek Gautam 	switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
6525853e133SVivek Gautam 	case COMP_CTX_STATE:
6535853e133SVivek Gautam 	case COMP_EBADSLT:
6545853e133SVivek Gautam 		printf("Setup ERROR: address device command for slot %d.\n",
6555853e133SVivek Gautam 								slot_id);
6565853e133SVivek Gautam 		ret = -EINVAL;
6575853e133SVivek Gautam 		break;
6585853e133SVivek Gautam 	case COMP_TX_ERR:
6595853e133SVivek Gautam 		puts("Device not responding to set address.\n");
6605853e133SVivek Gautam 		ret = -EPROTO;
6615853e133SVivek Gautam 		break;
6625853e133SVivek Gautam 	case COMP_DEV_ERR:
6635853e133SVivek Gautam 		puts("ERROR: Incompatible device"
6645853e133SVivek Gautam 					"for address device command.\n");
6655853e133SVivek Gautam 		ret = -ENODEV;
6665853e133SVivek Gautam 		break;
6675853e133SVivek Gautam 	case COMP_SUCCESS:
6685853e133SVivek Gautam 		debug("Successful Address Device command\n");
6695853e133SVivek Gautam 		udev->status = 0;
6705853e133SVivek Gautam 		break;
6715853e133SVivek Gautam 	default:
6725853e133SVivek Gautam 		printf("ERROR: unexpected command completion code 0x%x.\n",
6735853e133SVivek Gautam 			GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
6745853e133SVivek Gautam 		ret = -EINVAL;
6755853e133SVivek Gautam 		break;
6765853e133SVivek Gautam 	}
6775853e133SVivek Gautam 
6785853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
6795853e133SVivek Gautam 
6805853e133SVivek Gautam 	if (ret < 0)
6815853e133SVivek Gautam 		/*
6825853e133SVivek Gautam 		 * TODO: Unsuccessful Address Device command shall leave the
6835853e133SVivek Gautam 		 * slot in default state. So, issue Disable Slot command now.
6845853e133SVivek Gautam 		 */
6855853e133SVivek Gautam 		return ret;
6865853e133SVivek Gautam 
687421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
6885853e133SVivek Gautam 			 virt_dev->out_ctx->size);
6895853e133SVivek Gautam 	slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
6905853e133SVivek Gautam 
6915853e133SVivek Gautam 	debug("xHC internal address is: %d\n",
6925853e133SVivek Gautam 		le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
6935853e133SVivek Gautam 
6945853e133SVivek Gautam 	return 0;
6955853e133SVivek Gautam }
6965853e133SVivek Gautam 
6975853e133SVivek Gautam /**
6985853e133SVivek Gautam  * Issue Enable slot command to the controller to allocate
6995853e133SVivek Gautam  * device slot and assign the slot id. It fails if the xHC
7005853e133SVivek Gautam  * ran out of device slots, the Enable Slot command timed out,
7015853e133SVivek Gautam  * or allocating memory failed.
7025853e133SVivek Gautam  *
7035853e133SVivek Gautam  * @param udev	pointer to the Device Data Structure
7045853e133SVivek Gautam  * @return Returns 0 on succes else return error code on failure
7055853e133SVivek Gautam  */
_xhci_alloc_device(struct usb_device * udev)706121a4d13SMasahiro Yamada static int _xhci_alloc_device(struct usb_device *udev)
7075853e133SVivek Gautam {
7087c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
7095853e133SVivek Gautam 	union xhci_trb *event;
7105853e133SVivek Gautam 	int ret;
7115853e133SVivek Gautam 
7125853e133SVivek Gautam 	/*
7135853e133SVivek Gautam 	 * Root hub will be first device to be initailized.
7145853e133SVivek Gautam 	 * If this device is root-hub, don't do any xHC related
7155853e133SVivek Gautam 	 * stuff.
7165853e133SVivek Gautam 	 */
7175853e133SVivek Gautam 	if (ctrl->rootdev == 0) {
7185853e133SVivek Gautam 		udev->speed = USB_SPEED_SUPER;
7195853e133SVivek Gautam 		return 0;
7205853e133SVivek Gautam 	}
7215853e133SVivek Gautam 
7225853e133SVivek Gautam 	xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
7235853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
7245853e133SVivek Gautam 	BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
7255853e133SVivek Gautam 		!= COMP_SUCCESS);
7265853e133SVivek Gautam 
7275853e133SVivek Gautam 	udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
7285853e133SVivek Gautam 
7295853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
7305853e133SVivek Gautam 
7317e0c5ee8SSimon Glass 	ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
7325853e133SVivek Gautam 	if (ret < 0) {
7335853e133SVivek Gautam 		/*
7345853e133SVivek Gautam 		 * TODO: Unsuccessful Address Device command shall leave
7355853e133SVivek Gautam 		 * the slot in default. So, issue Disable Slot command now.
7365853e133SVivek Gautam 		 */
7375853e133SVivek Gautam 		puts("Could not allocate xHCI USB device data structures\n");
7385853e133SVivek Gautam 		return ret;
7395853e133SVivek Gautam 	}
7405853e133SVivek Gautam 
7415853e133SVivek Gautam 	return 0;
7425853e133SVivek Gautam }
7435853e133SVivek Gautam 
744*fd09c205SSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
usb_alloc_device(struct usb_device * udev)745a5762fe0SSimon Glass int usb_alloc_device(struct usb_device *udev)
746a5762fe0SSimon Glass {
747a5762fe0SSimon Glass 	return _xhci_alloc_device(udev);
748a5762fe0SSimon Glass }
749a5762fe0SSimon Glass #endif
750a5762fe0SSimon Glass 
7515853e133SVivek Gautam /*
7525853e133SVivek Gautam  * Full speed devices may have a max packet size greater than 8 bytes, but the
7535853e133SVivek Gautam  * USB core doesn't know that until it reads the first 8 bytes of the
7545853e133SVivek Gautam  * descriptor.  If the usb_device's max packet size changes after that point,
7555853e133SVivek Gautam  * we need to issue an evaluate context command and wait on it.
7565853e133SVivek Gautam  *
7575853e133SVivek Gautam  * @param udev	pointer to the Device Data Structure
7585853e133SVivek Gautam  * @return returns the status of the xhci_configure_endpoints
7595853e133SVivek Gautam  */
xhci_check_maxpacket(struct usb_device * udev)7605853e133SVivek Gautam int xhci_check_maxpacket(struct usb_device *udev)
7615853e133SVivek Gautam {
7627c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
7635853e133SVivek Gautam 	unsigned int slot_id = udev->slot_id;
7645853e133SVivek Gautam 	int ep_index = 0;	/* control endpoint */
7655853e133SVivek Gautam 	struct xhci_container_ctx *in_ctx;
7665853e133SVivek Gautam 	struct xhci_container_ctx *out_ctx;
7675853e133SVivek Gautam 	struct xhci_input_control_ctx *ctrl_ctx;
7685853e133SVivek Gautam 	struct xhci_ep_ctx *ep_ctx;
7695853e133SVivek Gautam 	int max_packet_size;
7705853e133SVivek Gautam 	int hw_max_packet_size;
7715853e133SVivek Gautam 	int ret = 0;
7725853e133SVivek Gautam 
7735853e133SVivek Gautam 	out_ctx = ctrl->devs[slot_id]->out_ctx;
774421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
7755853e133SVivek Gautam 
7765853e133SVivek Gautam 	ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
7775853e133SVivek Gautam 	hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
778b5aa857bSBin Meng 	max_packet_size = udev->epmaxpacketin[0];
7795853e133SVivek Gautam 	if (hw_max_packet_size != max_packet_size) {
7805853e133SVivek Gautam 		debug("Max Packet Size for ep 0 changed.\n");
7815853e133SVivek Gautam 		debug("Max packet size in usb_device = %d\n", max_packet_size);
7825853e133SVivek Gautam 		debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
7835853e133SVivek Gautam 		debug("Issuing evaluate context command.\n");
7845853e133SVivek Gautam 
7855853e133SVivek Gautam 		/* Set up the modified control endpoint 0 */
7865853e133SVivek Gautam 		xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
7875853e133SVivek Gautam 				ctrl->devs[slot_id]->out_ctx, ep_index);
7885853e133SVivek Gautam 		in_ctx = ctrl->devs[slot_id]->in_ctx;
7895853e133SVivek Gautam 		ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
790b5aa857bSBin Meng 		ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
791b5aa857bSBin Meng 						<< MAX_PACKET_SHIFT));
7925853e133SVivek Gautam 		ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
7935853e133SVivek Gautam 
7945853e133SVivek Gautam 		/*
7955853e133SVivek Gautam 		 * Set up the input context flags for the command
7965853e133SVivek Gautam 		 * FIXME: This won't work if a non-default control endpoint
7975853e133SVivek Gautam 		 * changes max packet sizes.
7985853e133SVivek Gautam 		 */
7995853e133SVivek Gautam 		ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
8005853e133SVivek Gautam 		ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
8015853e133SVivek Gautam 		ctrl_ctx->drop_flags = 0;
8025853e133SVivek Gautam 
8035853e133SVivek Gautam 		ret = xhci_configure_endpoints(udev, true);
8045853e133SVivek Gautam 	}
8055853e133SVivek Gautam 	return ret;
8065853e133SVivek Gautam }
8075853e133SVivek Gautam 
8085853e133SVivek Gautam /**
8095853e133SVivek Gautam  * Clears the Change bits of the Port Status Register
8105853e133SVivek Gautam  *
8115853e133SVivek Gautam  * @param wValue	request value
8125853e133SVivek Gautam  * @param wIndex	request index
8135853e133SVivek Gautam  * @param addr		address of posrt status register
8145853e133SVivek Gautam  * @param port_status	state of port status register
8155853e133SVivek Gautam  * @return none
8165853e133SVivek Gautam  */
xhci_clear_port_change_bit(u16 wValue,u16 wIndex,volatile uint32_t * addr,u32 port_status)8175853e133SVivek Gautam static void xhci_clear_port_change_bit(u16 wValue,
8185853e133SVivek Gautam 		u16 wIndex, volatile uint32_t *addr, u32 port_status)
8195853e133SVivek Gautam {
8205853e133SVivek Gautam 	char *port_change_bit;
8215853e133SVivek Gautam 	u32 status;
8225853e133SVivek Gautam 
8235853e133SVivek Gautam 	switch (wValue) {
8245853e133SVivek Gautam 	case USB_PORT_FEAT_C_RESET:
8255853e133SVivek Gautam 		status = PORT_RC;
8265853e133SVivek Gautam 		port_change_bit = "reset";
8275853e133SVivek Gautam 		break;
8285853e133SVivek Gautam 	case USB_PORT_FEAT_C_CONNECTION:
8295853e133SVivek Gautam 		status = PORT_CSC;
8305853e133SVivek Gautam 		port_change_bit = "connect";
8315853e133SVivek Gautam 		break;
8325853e133SVivek Gautam 	case USB_PORT_FEAT_C_OVER_CURRENT:
8335853e133SVivek Gautam 		status = PORT_OCC;
8345853e133SVivek Gautam 		port_change_bit = "over-current";
8355853e133SVivek Gautam 		break;
8365853e133SVivek Gautam 	case USB_PORT_FEAT_C_ENABLE:
8375853e133SVivek Gautam 		status = PORT_PEC;
8385853e133SVivek Gautam 		port_change_bit = "enable/disable";
8395853e133SVivek Gautam 		break;
8405853e133SVivek Gautam 	case USB_PORT_FEAT_C_SUSPEND:
8415853e133SVivek Gautam 		status = PORT_PLC;
8425853e133SVivek Gautam 		port_change_bit = "suspend/resume";
8435853e133SVivek Gautam 		break;
8445853e133SVivek Gautam 	default:
8455853e133SVivek Gautam 		/* Should never happen */
8465853e133SVivek Gautam 		return;
8475853e133SVivek Gautam 	}
8485853e133SVivek Gautam 
8495853e133SVivek Gautam 	/* Change bits are all write 1 to clear */
8505853e133SVivek Gautam 	xhci_writel(addr, port_status | status);
8515853e133SVivek Gautam 
8525853e133SVivek Gautam 	port_status = xhci_readl(addr);
8535853e133SVivek Gautam 	debug("clear port %s change, actual port %d status  = 0x%x\n",
8545853e133SVivek Gautam 			port_change_bit, wIndex, port_status);
8555853e133SVivek Gautam }
8565853e133SVivek Gautam 
8575853e133SVivek Gautam /**
8585853e133SVivek Gautam  * Save Read Only (RO) bits and save read/write bits where
8595853e133SVivek Gautam  * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
8605853e133SVivek Gautam  * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
8615853e133SVivek Gautam  *
8625853e133SVivek Gautam  * @param state	state of the Port Status and Control Regsiter
8635853e133SVivek Gautam  * @return a value that would result in the port being in the
8645853e133SVivek Gautam  *	   same state, if the value was written to the port
8655853e133SVivek Gautam  *	   status control register.
8665853e133SVivek Gautam  */
xhci_port_state_to_neutral(u32 state)8675853e133SVivek Gautam static u32 xhci_port_state_to_neutral(u32 state)
8685853e133SVivek Gautam {
8695853e133SVivek Gautam 	/* Save read-only status and port state */
8705853e133SVivek Gautam 	return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
8715853e133SVivek Gautam }
8725853e133SVivek Gautam 
8735853e133SVivek Gautam /**
8745853e133SVivek Gautam  * Submits the Requests to the XHCI Host Controller
8755853e133SVivek Gautam  *
8765853e133SVivek Gautam  * @param udev pointer to the USB device structure
8775853e133SVivek Gautam  * @param pipe contains the DIR_IN or OUT , devnum
8785853e133SVivek Gautam  * @param buffer buffer to be read/written based on the request
8795853e133SVivek Gautam  * @return returns 0 if successful else -1 on failure
8805853e133SVivek Gautam  */
xhci_submit_root(struct usb_device * udev,unsigned long pipe,void * buffer,struct devrequest * req)8815853e133SVivek Gautam static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
8825853e133SVivek Gautam 			void *buffer, struct devrequest *req)
8835853e133SVivek Gautam {
8845853e133SVivek Gautam 	uint8_t tmpbuf[4];
8855853e133SVivek Gautam 	u16 typeReq;
8865853e133SVivek Gautam 	void *srcptr = NULL;
8875853e133SVivek Gautam 	int len, srclen;
8885853e133SVivek Gautam 	uint32_t reg;
8895853e133SVivek Gautam 	volatile uint32_t *status_reg;
8907c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
8917274671eSBin Meng 	struct xhci_hccr *hccr = ctrl->hccr;
8925853e133SVivek Gautam 	struct xhci_hcor *hcor = ctrl->hcor;
8937274671eSBin Meng 	int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
8945853e133SVivek Gautam 
89525d1936aSJeroen Hofstee 	if ((req->requesttype & USB_RT_PORT) &&
8967274671eSBin Meng 	    le16_to_cpu(req->index) > max_ports) {
8977274671eSBin Meng 		printf("The request port(%d) exceeds maximum port number\n",
8985853e133SVivek Gautam 		       le16_to_cpu(req->index) - 1);
8995853e133SVivek Gautam 		return -EINVAL;
9005853e133SVivek Gautam 	}
9015853e133SVivek Gautam 
9025853e133SVivek Gautam 	status_reg = (volatile uint32_t *)
9035853e133SVivek Gautam 		     (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
9045853e133SVivek Gautam 	srclen = 0;
9055853e133SVivek Gautam 
9065853e133SVivek Gautam 	typeReq = req->request | req->requesttype << 8;
9075853e133SVivek Gautam 
9085853e133SVivek Gautam 	switch (typeReq) {
9095853e133SVivek Gautam 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
9105853e133SVivek Gautam 		switch (le16_to_cpu(req->value) >> 8) {
9115853e133SVivek Gautam 		case USB_DT_DEVICE:
9125853e133SVivek Gautam 			debug("USB_DT_DEVICE request\n");
9135853e133SVivek Gautam 			srcptr = &descriptor.device;
9145853e133SVivek Gautam 			srclen = 0x12;
9155853e133SVivek Gautam 			break;
9165853e133SVivek Gautam 		case USB_DT_CONFIG:
9175853e133SVivek Gautam 			debug("USB_DT_CONFIG config\n");
9185853e133SVivek Gautam 			srcptr = &descriptor.config;
9195853e133SVivek Gautam 			srclen = 0x19;
9205853e133SVivek Gautam 			break;
9215853e133SVivek Gautam 		case USB_DT_STRING:
9225853e133SVivek Gautam 			debug("USB_DT_STRING config\n");
9235853e133SVivek Gautam 			switch (le16_to_cpu(req->value) & 0xff) {
9245853e133SVivek Gautam 			case 0:	/* Language */
9255853e133SVivek Gautam 				srcptr = "\4\3\11\4";
9265853e133SVivek Gautam 				srclen = 4;
9275853e133SVivek Gautam 				break;
9285853e133SVivek Gautam 			case 1:	/* Vendor String  */
929f161c178SSimon Glass 				srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
9305853e133SVivek Gautam 				srclen = 14;
9315853e133SVivek Gautam 				break;
9325853e133SVivek Gautam 			case 2:	/* Product Name */
9335853e133SVivek Gautam 				srcptr = "\52\3X\0H\0C\0I\0 "
9345853e133SVivek Gautam 					 "\0H\0o\0s\0t\0 "
9355853e133SVivek Gautam 					 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
9365853e133SVivek Gautam 				srclen = 42;
9375853e133SVivek Gautam 				break;
9385853e133SVivek Gautam 			default:
9395853e133SVivek Gautam 				printf("unknown value DT_STRING %x\n",
9405853e133SVivek Gautam 					le16_to_cpu(req->value));
9415853e133SVivek Gautam 				goto unknown;
9425853e133SVivek Gautam 			}
9435853e133SVivek Gautam 			break;
9445853e133SVivek Gautam 		default:
9455853e133SVivek Gautam 			printf("unknown value %x\n", le16_to_cpu(req->value));
9465853e133SVivek Gautam 			goto unknown;
9475853e133SVivek Gautam 		}
9485853e133SVivek Gautam 		break;
9495853e133SVivek Gautam 	case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
9505853e133SVivek Gautam 		switch (le16_to_cpu(req->value) >> 8) {
9515853e133SVivek Gautam 		case USB_DT_HUB:
952f3421196SBin Meng 		case USB_DT_SS_HUB:
9535853e133SVivek Gautam 			debug("USB_DT_HUB config\n");
9545853e133SVivek Gautam 			srcptr = &descriptor.hub;
9555853e133SVivek Gautam 			srclen = 0x8;
9565853e133SVivek Gautam 			break;
9575853e133SVivek Gautam 		default:
9585853e133SVivek Gautam 			printf("unknown value %x\n", le16_to_cpu(req->value));
9595853e133SVivek Gautam 			goto unknown;
9605853e133SVivek Gautam 		}
9615853e133SVivek Gautam 		break;
9625853e133SVivek Gautam 	case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
9635853e133SVivek Gautam 		debug("USB_REQ_SET_ADDRESS\n");
9645853e133SVivek Gautam 		ctrl->rootdev = le16_to_cpu(req->value);
9655853e133SVivek Gautam 		break;
9665853e133SVivek Gautam 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
9675853e133SVivek Gautam 		/* Do nothing */
9685853e133SVivek Gautam 		break;
9695853e133SVivek Gautam 	case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
9705853e133SVivek Gautam 		tmpbuf[0] = 1;	/* USB_STATUS_SELFPOWERED */
9715853e133SVivek Gautam 		tmpbuf[1] = 0;
9725853e133SVivek Gautam 		srcptr = tmpbuf;
9735853e133SVivek Gautam 		srclen = 2;
9745853e133SVivek Gautam 		break;
9755853e133SVivek Gautam 	case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
9765853e133SVivek Gautam 		memset(tmpbuf, 0, 4);
9775853e133SVivek Gautam 		reg = xhci_readl(status_reg);
9785853e133SVivek Gautam 		if (reg & PORT_CONNECT) {
9795853e133SVivek Gautam 			tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
9805853e133SVivek Gautam 			switch (reg & DEV_SPEED_MASK) {
9815853e133SVivek Gautam 			case XDEV_FS:
9825853e133SVivek Gautam 				debug("SPEED = FULLSPEED\n");
9835853e133SVivek Gautam 				break;
9845853e133SVivek Gautam 			case XDEV_LS:
9855853e133SVivek Gautam 				debug("SPEED = LOWSPEED\n");
9865853e133SVivek Gautam 				tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
9875853e133SVivek Gautam 				break;
9885853e133SVivek Gautam 			case XDEV_HS:
9895853e133SVivek Gautam 				debug("SPEED = HIGHSPEED\n");
9905853e133SVivek Gautam 				tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
9915853e133SVivek Gautam 				break;
9925853e133SVivek Gautam 			case XDEV_SS:
9935853e133SVivek Gautam 				debug("SPEED = SUPERSPEED\n");
9945853e133SVivek Gautam 				tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
9955853e133SVivek Gautam 				break;
9965853e133SVivek Gautam 			}
9975853e133SVivek Gautam 		}
9985853e133SVivek Gautam 		if (reg & PORT_PE)
9995853e133SVivek Gautam 			tmpbuf[0] |= USB_PORT_STAT_ENABLE;
10005853e133SVivek Gautam 		if ((reg & PORT_PLS_MASK) == XDEV_U3)
10015853e133SVivek Gautam 			tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
10025853e133SVivek Gautam 		if (reg & PORT_OC)
10035853e133SVivek Gautam 			tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
10045853e133SVivek Gautam 		if (reg & PORT_RESET)
10055853e133SVivek Gautam 			tmpbuf[0] |= USB_PORT_STAT_RESET;
10065853e133SVivek Gautam 		if (reg & PORT_POWER)
10075853e133SVivek Gautam 			/*
10085853e133SVivek Gautam 			 * XXX: This Port power bit (for USB 3.0 hub)
10095853e133SVivek Gautam 			 * we are faking in USB 2.0 hub port status;
10105853e133SVivek Gautam 			 * since there's a change in bit positions in
10115853e133SVivek Gautam 			 * two:
10125853e133SVivek Gautam 			 * USB 2.0 port status PP is at position[8]
10135853e133SVivek Gautam 			 * USB 3.0 port status PP is at position[9]
10145853e133SVivek Gautam 			 * So, we are still keeping it at position [8]
10155853e133SVivek Gautam 			 */
10165853e133SVivek Gautam 			tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
10175853e133SVivek Gautam 		if (reg & PORT_CSC)
10185853e133SVivek Gautam 			tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
10195853e133SVivek Gautam 		if (reg & PORT_PEC)
10205853e133SVivek Gautam 			tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
10215853e133SVivek Gautam 		if (reg & PORT_OCC)
10225853e133SVivek Gautam 			tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
10235853e133SVivek Gautam 		if (reg & PORT_RC)
10245853e133SVivek Gautam 			tmpbuf[2] |= USB_PORT_STAT_C_RESET;
10255853e133SVivek Gautam 
10265853e133SVivek Gautam 		srcptr = tmpbuf;
10275853e133SVivek Gautam 		srclen = 4;
10285853e133SVivek Gautam 		break;
10295853e133SVivek Gautam 	case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
10305853e133SVivek Gautam 		reg = xhci_readl(status_reg);
10315853e133SVivek Gautam 		reg = xhci_port_state_to_neutral(reg);
10325853e133SVivek Gautam 		switch (le16_to_cpu(req->value)) {
10335853e133SVivek Gautam 		case USB_PORT_FEAT_ENABLE:
10345853e133SVivek Gautam 			reg |= PORT_PE;
10355853e133SVivek Gautam 			xhci_writel(status_reg, reg);
10365853e133SVivek Gautam 			break;
10375853e133SVivek Gautam 		case USB_PORT_FEAT_POWER:
10385853e133SVivek Gautam 			reg |= PORT_POWER;
10395853e133SVivek Gautam 			xhci_writel(status_reg, reg);
10405853e133SVivek Gautam 			break;
10415853e133SVivek Gautam 		case USB_PORT_FEAT_RESET:
10425853e133SVivek Gautam 			reg |= PORT_RESET;
10435853e133SVivek Gautam 			xhci_writel(status_reg, reg);
10445853e133SVivek Gautam 			break;
10455853e133SVivek Gautam 		default:
10465853e133SVivek Gautam 			printf("unknown feature %x\n", le16_to_cpu(req->value));
10475853e133SVivek Gautam 			goto unknown;
10485853e133SVivek Gautam 		}
10495853e133SVivek Gautam 		break;
10505853e133SVivek Gautam 	case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
10515853e133SVivek Gautam 		reg = xhci_readl(status_reg);
10525853e133SVivek Gautam 		reg = xhci_port_state_to_neutral(reg);
10535853e133SVivek Gautam 		switch (le16_to_cpu(req->value)) {
10545853e133SVivek Gautam 		case USB_PORT_FEAT_ENABLE:
10555853e133SVivek Gautam 			reg &= ~PORT_PE;
10565853e133SVivek Gautam 			break;
10575853e133SVivek Gautam 		case USB_PORT_FEAT_POWER:
10585853e133SVivek Gautam 			reg &= ~PORT_POWER;
10595853e133SVivek Gautam 			break;
10605853e133SVivek Gautam 		case USB_PORT_FEAT_C_RESET:
10615853e133SVivek Gautam 		case USB_PORT_FEAT_C_CONNECTION:
10625853e133SVivek Gautam 		case USB_PORT_FEAT_C_OVER_CURRENT:
10635853e133SVivek Gautam 		case USB_PORT_FEAT_C_ENABLE:
10645853e133SVivek Gautam 			xhci_clear_port_change_bit((le16_to_cpu(req->value)),
10655853e133SVivek Gautam 							le16_to_cpu(req->index),
10665853e133SVivek Gautam 							status_reg, reg);
10675853e133SVivek Gautam 			break;
10685853e133SVivek Gautam 		default:
10695853e133SVivek Gautam 			printf("unknown feature %x\n", le16_to_cpu(req->value));
10705853e133SVivek Gautam 			goto unknown;
10715853e133SVivek Gautam 		}
10725853e133SVivek Gautam 		xhci_writel(status_reg, reg);
10735853e133SVivek Gautam 		break;
10745853e133SVivek Gautam 	default:
10755853e133SVivek Gautam 		puts("Unknown request\n");
10765853e133SVivek Gautam 		goto unknown;
10775853e133SVivek Gautam 	}
10785853e133SVivek Gautam 
10795853e133SVivek Gautam 	debug("scrlen = %d\n req->length = %d\n",
10805853e133SVivek Gautam 		srclen, le16_to_cpu(req->length));
10815853e133SVivek Gautam 
1082b4141195SMasahiro Yamada 	len = min(srclen, (int)le16_to_cpu(req->length));
10835853e133SVivek Gautam 
10845853e133SVivek Gautam 	if (srcptr != NULL && len > 0)
10855853e133SVivek Gautam 		memcpy(buffer, srcptr, len);
10865853e133SVivek Gautam 	else
10875853e133SVivek Gautam 		debug("Len is 0\n");
10885853e133SVivek Gautam 
10895853e133SVivek Gautam 	udev->act_len = len;
10905853e133SVivek Gautam 	udev->status = 0;
10915853e133SVivek Gautam 
10925853e133SVivek Gautam 	return 0;
10935853e133SVivek Gautam 
10945853e133SVivek Gautam unknown:
10955853e133SVivek Gautam 	udev->act_len = 0;
10965853e133SVivek Gautam 	udev->status = USB_ST_STALLED;
10975853e133SVivek Gautam 
10985853e133SVivek Gautam 	return -ENODEV;
10995853e133SVivek Gautam }
11005853e133SVivek Gautam 
11015853e133SVivek Gautam /**
11025853e133SVivek Gautam  * Submits the INT request to XHCI Host cotroller
11035853e133SVivek Gautam  *
11045853e133SVivek Gautam  * @param udev	pointer to the USB device
11055853e133SVivek Gautam  * @param pipe		contains the DIR_IN or OUT , devnum
11065853e133SVivek Gautam  * @param buffer	buffer to be read/written based on the request
11075853e133SVivek Gautam  * @param length	length of the buffer
11085853e133SVivek Gautam  * @param interval	interval of the interrupt
11095853e133SVivek Gautam  * @return 0
11105853e133SVivek Gautam  */
_xhci_submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval)1111a5762fe0SSimon Glass static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
1112a5762fe0SSimon Glass 				void *buffer, int length, int interval)
11135853e133SVivek Gautam {
11141897d601SBin Meng 	if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
11151897d601SBin Meng 		printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
11165853e133SVivek Gautam 		return -EINVAL;
11175853e133SVivek Gautam 	}
11185853e133SVivek Gautam 
11191897d601SBin Meng 	/*
11201897d601SBin Meng 	 * xHCI uses normal TRBs for both bulk and interrupt. When the
11211897d601SBin Meng 	 * interrupt endpoint is to be serviced, the xHC will consume
11221897d601SBin Meng 	 * (at most) one TD. A TD (comprised of sg list entries) can
11231897d601SBin Meng 	 * take several service intervals to transmit.
11241897d601SBin Meng 	 */
11251897d601SBin Meng 	return xhci_bulk_tx(udev, pipe, length, buffer);
11261897d601SBin Meng }
11271897d601SBin Meng 
11285853e133SVivek Gautam /**
11295853e133SVivek Gautam  * submit the BULK type of request to the USB Device
11305853e133SVivek Gautam  *
11315853e133SVivek Gautam  * @param udev	pointer to the USB device
11325853e133SVivek Gautam  * @param pipe		contains the DIR_IN or OUT , devnum
11335853e133SVivek Gautam  * @param buffer	buffer to be read/written based on the request
11345853e133SVivek Gautam  * @param length	length of the buffer
11355853e133SVivek Gautam  * @return returns 0 if successful else -1 on failure
11365853e133SVivek Gautam  */
_xhci_submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1137a5762fe0SSimon Glass static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1138a5762fe0SSimon Glass 				 void *buffer, int length)
11395853e133SVivek Gautam {
11405853e133SVivek Gautam 	if (usb_pipetype(pipe) != PIPE_BULK) {
11415853e133SVivek Gautam 		printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11425853e133SVivek Gautam 		return -EINVAL;
11435853e133SVivek Gautam 	}
11445853e133SVivek Gautam 
11455853e133SVivek Gautam 	return xhci_bulk_tx(udev, pipe, length, buffer);
11465853e133SVivek Gautam }
11475853e133SVivek Gautam 
11485853e133SVivek Gautam /**
11495853e133SVivek Gautam  * submit the control type of request to the Root hub/Device based on the devnum
11505853e133SVivek Gautam  *
11515853e133SVivek Gautam  * @param udev	pointer to the USB device
11525853e133SVivek Gautam  * @param pipe		contains the DIR_IN or OUT , devnum
11535853e133SVivek Gautam  * @param buffer	buffer to be read/written based on the request
11545853e133SVivek Gautam  * @param length	length of the buffer
11555853e133SVivek Gautam  * @param setup		Request type
11565dd75e3bSSimon Glass  * @param root_portnr	Root port number that this device is on
11575853e133SVivek Gautam  * @return returns 0 if successful else -1 on failure
11585853e133SVivek Gautam  */
_xhci_submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup,int root_portnr)11595dd75e3bSSimon Glass static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
11605dd75e3bSSimon Glass 				    void *buffer, int length,
11615dd75e3bSSimon Glass 				    struct devrequest *setup, int root_portnr)
11625853e133SVivek Gautam {
11637c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
11645853e133SVivek Gautam 	int ret = 0;
11655853e133SVivek Gautam 
11665853e133SVivek Gautam 	if (usb_pipetype(pipe) != PIPE_CONTROL) {
11675853e133SVivek Gautam 		printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
11685853e133SVivek Gautam 		return -EINVAL;
11695853e133SVivek Gautam 	}
11705853e133SVivek Gautam 
11715853e133SVivek Gautam 	if (usb_pipedevice(pipe) == ctrl->rootdev)
11725853e133SVivek Gautam 		return xhci_submit_root(udev, pipe, buffer, setup);
11735853e133SVivek Gautam 
11741b108880STed Chen 	if (setup->request == USB_REQ_SET_ADDRESS &&
11751b108880STed Chen 	   (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
11765dd75e3bSSimon Glass 		return xhci_address_device(udev, root_portnr);
11775853e133SVivek Gautam 
11781b108880STed Chen 	if (setup->request == USB_REQ_SET_CONFIGURATION &&
11791b108880STed Chen 	   (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
11805853e133SVivek Gautam 		ret = xhci_set_configuration(udev);
11815853e133SVivek Gautam 		if (ret) {
11825853e133SVivek Gautam 			puts("Failed to configure xHCI endpoint\n");
11835853e133SVivek Gautam 			return ret;
11845853e133SVivek Gautam 		}
11855853e133SVivek Gautam 	}
11865853e133SVivek Gautam 
11875853e133SVivek Gautam 	return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
11885853e133SVivek Gautam }
11895853e133SVivek Gautam 
xhci_lowlevel_init(struct xhci_ctrl * ctrl)1190779d1263SSimon Glass static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
11915853e133SVivek Gautam {
1192779d1263SSimon Glass 	struct xhci_hccr *hccr;
1193779d1263SSimon Glass 	struct xhci_hcor *hcor;
11945853e133SVivek Gautam 	uint32_t val;
11955853e133SVivek Gautam 	uint32_t val2;
11965853e133SVivek Gautam 	uint32_t reg;
11975853e133SVivek Gautam 
1198779d1263SSimon Glass 	hccr = ctrl->hccr;
1199779d1263SSimon Glass 	hcor = ctrl->hcor;
12005853e133SVivek Gautam 	/*
12015853e133SVivek Gautam 	 * Program the Number of Device Slots Enabled field in the CONFIG
12025853e133SVivek Gautam 	 * register with the max value of slots the HC can handle.
12035853e133SVivek Gautam 	 */
12045853e133SVivek Gautam 	val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
12055853e133SVivek Gautam 	val2 = xhci_readl(&hcor->or_config);
12065853e133SVivek Gautam 	val |= (val2 & ~HCS_SLOTS_MASK);
12075853e133SVivek Gautam 	xhci_writel(&hcor->or_config, val);
12085853e133SVivek Gautam 
12095853e133SVivek Gautam 	/* initializing xhci data structures */
12105853e133SVivek Gautam 	if (xhci_mem_init(ctrl, hccr, hcor) < 0)
12115853e133SVivek Gautam 		return -ENOMEM;
12125853e133SVivek Gautam 
12135853e133SVivek Gautam 	reg = xhci_readl(&hccr->cr_hcsparams1);
12145853e133SVivek Gautam 	descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
12155853e133SVivek Gautam 						HCS_MAX_PORTS_SHIFT);
12165853e133SVivek Gautam 	printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
12175853e133SVivek Gautam 
12185853e133SVivek Gautam 	/* Port Indicators */
12195853e133SVivek Gautam 	reg = xhci_readl(&hccr->cr_hccparams);
12205853e133SVivek Gautam 	if (HCS_INDICATOR(reg))
12215853e133SVivek Gautam 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
12225853e133SVivek Gautam 				| 0x80, &descriptor.hub.wHubCharacteristics);
12235853e133SVivek Gautam 
12245853e133SVivek Gautam 	/* Port Power Control */
12255853e133SVivek Gautam 	if (HCC_PPC(reg))
12265853e133SVivek Gautam 		put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
12275853e133SVivek Gautam 				| 0x01, &descriptor.hub.wHubCharacteristics);
12285853e133SVivek Gautam 
12295853e133SVivek Gautam 	if (xhci_start(hcor)) {
12305853e133SVivek Gautam 		xhci_reset(hcor);
12315853e133SVivek Gautam 		return -ENODEV;
12325853e133SVivek Gautam 	}
12335853e133SVivek Gautam 
12345853e133SVivek Gautam 	/* Zero'ing IRQ control register and IRQ pending register */
12355853e133SVivek Gautam 	xhci_writel(&ctrl->ir_set->irq_control, 0x0);
12365853e133SVivek Gautam 	xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
12375853e133SVivek Gautam 
12385853e133SVivek Gautam 	reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
12395853e133SVivek Gautam 	printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
12405853e133SVivek Gautam 
1241779d1263SSimon Glass 	return 0;
1242779d1263SSimon Glass }
1243779d1263SSimon Glass 
xhci_lowlevel_stop(struct xhci_ctrl * ctrl)1244779d1263SSimon Glass static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1245779d1263SSimon Glass {
1246779d1263SSimon Glass 	u32 temp;
1247779d1263SSimon Glass 
1248779d1263SSimon Glass 	xhci_reset(ctrl->hcor);
1249779d1263SSimon Glass 
1250779d1263SSimon Glass 	debug("// Disabling event ring interrupts\n");
1251779d1263SSimon Glass 	temp = xhci_readl(&ctrl->hcor->or_usbsts);
1252779d1263SSimon Glass 	xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1253779d1263SSimon Glass 	temp = xhci_readl(&ctrl->ir_set->irq_pending);
1254779d1263SSimon Glass 	xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
12555853e133SVivek Gautam 
12565853e133SVivek Gautam 	return 0;
12575853e133SVivek Gautam }
12585853e133SVivek Gautam 
1259*fd09c205SSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)12605dd75e3bSSimon Glass int submit_control_msg(struct usb_device *udev, unsigned long pipe,
12615dd75e3bSSimon Glass 		       void *buffer, int length, struct devrequest *setup)
12625dd75e3bSSimon Glass {
12635dd75e3bSSimon Glass 	struct usb_device *hop = udev;
12645dd75e3bSSimon Glass 
12655dd75e3bSSimon Glass 	if (hop->parent)
12665dd75e3bSSimon Glass 		while (hop->parent->parent)
12675dd75e3bSSimon Glass 			hop = hop->parent;
12685dd75e3bSSimon Glass 
12695dd75e3bSSimon Glass 	return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
12705dd75e3bSSimon Glass 					hop->portnr);
12715dd75e3bSSimon Glass }
12725dd75e3bSSimon Glass 
submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1273a5762fe0SSimon Glass int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1274a5762fe0SSimon Glass 		    int length)
1275a5762fe0SSimon Glass {
1276a5762fe0SSimon Glass 	return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1277a5762fe0SSimon Glass }
1278a5762fe0SSimon Glass 
submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval)1279a5762fe0SSimon Glass int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1280a5762fe0SSimon Glass 		   int length, int interval)
1281a5762fe0SSimon Glass {
1282a5762fe0SSimon Glass 	return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
1283a5762fe0SSimon Glass }
1284a5762fe0SSimon Glass 
12855853e133SVivek Gautam /**
1286779d1263SSimon Glass  * Intialises the XHCI host controller
1287779d1263SSimon Glass  * and allocates the necessary data structures
1288779d1263SSimon Glass  *
1289779d1263SSimon Glass  * @param index	index to the host controller data structure
1290779d1263SSimon Glass  * @return pointer to the intialised controller
1291779d1263SSimon Glass  */
usb_lowlevel_init(int index,enum usb_init_type init,void ** controller)1292779d1263SSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1293779d1263SSimon Glass {
1294779d1263SSimon Glass 	struct xhci_hccr *hccr;
1295779d1263SSimon Glass 	struct xhci_hcor *hcor;
1296779d1263SSimon Glass 	struct xhci_ctrl *ctrl;
1297779d1263SSimon Glass 	int ret;
1298779d1263SSimon Glass 
1299a5ccda47SSergey Temerkhanov 	*controller = NULL;
1300a5ccda47SSergey Temerkhanov 
1301779d1263SSimon Glass 	if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1302779d1263SSimon Glass 		return -ENODEV;
1303779d1263SSimon Glass 
1304779d1263SSimon Glass 	if (xhci_reset(hcor) != 0)
1305779d1263SSimon Glass 		return -ENODEV;
1306779d1263SSimon Glass 
1307779d1263SSimon Glass 	ctrl = &xhcic[index];
1308779d1263SSimon Glass 
1309779d1263SSimon Glass 	ctrl->hccr = hccr;
1310779d1263SSimon Glass 	ctrl->hcor = hcor;
1311779d1263SSimon Glass 
1312779d1263SSimon Glass 	ret = xhci_lowlevel_init(ctrl);
1313779d1263SSimon Glass 
1314a5ccda47SSergey Temerkhanov 	if (ret) {
1315a5ccda47SSergey Temerkhanov 		ctrl->hccr = NULL;
1316a5ccda47SSergey Temerkhanov 		ctrl->hcor = NULL;
1317a5ccda47SSergey Temerkhanov 	} else {
1318779d1263SSimon Glass 		*controller = &xhcic[index];
1319a5ccda47SSergey Temerkhanov 	}
1320779d1263SSimon Glass 
1321779d1263SSimon Glass 	return ret;
1322779d1263SSimon Glass }
1323779d1263SSimon Glass 
1324779d1263SSimon Glass /**
13255853e133SVivek Gautam  * Stops the XHCI host controller
13265853e133SVivek Gautam  * and cleans up all the related data structures
13275853e133SVivek Gautam  *
13285853e133SVivek Gautam  * @param index	index to the host controller data structure
13295853e133SVivek Gautam  * @return none
13305853e133SVivek Gautam  */
usb_lowlevel_stop(int index)13315853e133SVivek Gautam int usb_lowlevel_stop(int index)
13325853e133SVivek Gautam {
13335853e133SVivek Gautam 	struct xhci_ctrl *ctrl = (xhcic + index);
13345853e133SVivek Gautam 
1335a5ccda47SSergey Temerkhanov 	if (ctrl->hcor) {
1336779d1263SSimon Glass 		xhci_lowlevel_stop(ctrl);
13375853e133SVivek Gautam 		xhci_hcd_stop(index);
13385853e133SVivek Gautam 		xhci_cleanup(ctrl);
1339a5ccda47SSergey Temerkhanov 	}
13405853e133SVivek Gautam 
13415853e133SVivek Gautam 	return 0;
13425853e133SVivek Gautam }
1343*fd09c205SSven Schwermer #endif /* CONFIG_IS_ENABLED(DM_USB) */
1344a5762fe0SSimon Glass 
1345*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
1346a5762fe0SSimon Glass 
xhci_submit_control_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)1347a5762fe0SSimon Glass static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1348a5762fe0SSimon Glass 				   unsigned long pipe, void *buffer, int length,
1349a5762fe0SSimon Glass 				   struct devrequest *setup)
1350a5762fe0SSimon Glass {
1351a5762fe0SSimon Glass 	struct usb_device *uhop;
1352a5762fe0SSimon Glass 	struct udevice *hub;
1353a5762fe0SSimon Glass 	int root_portnr = 0;
1354a5762fe0SSimon Glass 
1355a5762fe0SSimon Glass 	debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1356a5762fe0SSimon Glass 	      dev->name, udev, udev->dev->name, udev->portnr);
1357a5762fe0SSimon Glass 	hub = udev->dev;
1358a5762fe0SSimon Glass 	if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1359a5762fe0SSimon Glass 		/* Figure out our port number on the root hub */
136046c1d493SBin Meng 		if (usb_hub_is_root_hub(hub)) {
1361a5762fe0SSimon Glass 			root_portnr = udev->portnr;
1362a5762fe0SSimon Glass 		} else {
136346c1d493SBin Meng 			while (!usb_hub_is_root_hub(hub->parent))
1364a5762fe0SSimon Glass 				hub = hub->parent;
1365bcbe3d15SSimon Glass 			uhop = dev_get_parent_priv(hub);
1366a5762fe0SSimon Glass 			root_portnr = uhop->portnr;
1367a5762fe0SSimon Glass 		}
1368a5762fe0SSimon Glass 	}
1369a5762fe0SSimon Glass /*
1370a5762fe0SSimon Glass 	struct usb_device *hop = udev;
1371a5762fe0SSimon Glass 
1372a5762fe0SSimon Glass 	if (hop->parent)
1373a5762fe0SSimon Glass 		while (hop->parent->parent)
1374a5762fe0SSimon Glass 			hop = hop->parent;
1375a5762fe0SSimon Glass */
1376a5762fe0SSimon Glass 	return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1377a5762fe0SSimon Glass 					root_portnr);
1378a5762fe0SSimon Glass }
1379a5762fe0SSimon Glass 
xhci_submit_bulk_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length)1380a5762fe0SSimon Glass static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1381a5762fe0SSimon Glass 				unsigned long pipe, void *buffer, int length)
1382a5762fe0SSimon Glass {
1383a5762fe0SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1384a5762fe0SSimon Glass 	return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1385a5762fe0SSimon Glass }
1386a5762fe0SSimon Glass 
xhci_submit_int_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval)1387a5762fe0SSimon Glass static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1388a5762fe0SSimon Glass 			       unsigned long pipe, void *buffer, int length,
1389a5762fe0SSimon Glass 			       int interval)
1390a5762fe0SSimon Glass {
1391a5762fe0SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1392a5762fe0SSimon Glass 	return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
1393a5762fe0SSimon Glass }
1394a5762fe0SSimon Glass 
xhci_alloc_device(struct udevice * dev,struct usb_device * udev)1395a5762fe0SSimon Glass static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1396a5762fe0SSimon Glass {
1397a5762fe0SSimon Glass 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1398a5762fe0SSimon Glass 	return _xhci_alloc_device(udev);
1399a5762fe0SSimon Glass }
1400a5762fe0SSimon Glass 
xhci_update_hub_device(struct udevice * dev,struct usb_device * udev)1401d228ca36SBin Meng static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1402d228ca36SBin Meng {
1403d228ca36SBin Meng 	struct xhci_ctrl *ctrl = dev_get_priv(dev);
1404d228ca36SBin Meng 	struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1405d228ca36SBin Meng 	struct xhci_virt_device *virt_dev;
1406d228ca36SBin Meng 	struct xhci_input_control_ctx *ctrl_ctx;
1407d228ca36SBin Meng 	struct xhci_container_ctx *out_ctx;
1408d228ca36SBin Meng 	struct xhci_container_ctx *in_ctx;
1409d228ca36SBin Meng 	struct xhci_slot_ctx *slot_ctx;
1410d228ca36SBin Meng 	int slot_id = udev->slot_id;
1411d228ca36SBin Meng 	unsigned think_time;
1412d228ca36SBin Meng 
1413d228ca36SBin Meng 	debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1414d228ca36SBin Meng 
1415d228ca36SBin Meng 	/* Ignore root hubs */
1416d228ca36SBin Meng 	if (usb_hub_is_root_hub(udev->dev))
1417d228ca36SBin Meng 		return 0;
1418d228ca36SBin Meng 
1419d228ca36SBin Meng 	virt_dev = ctrl->devs[slot_id];
1420d228ca36SBin Meng 	BUG_ON(!virt_dev);
1421d228ca36SBin Meng 
1422d228ca36SBin Meng 	out_ctx = virt_dev->out_ctx;
1423d228ca36SBin Meng 	in_ctx = virt_dev->in_ctx;
1424d228ca36SBin Meng 
1425d228ca36SBin Meng 	ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1426d228ca36SBin Meng 	/* Initialize the input context control */
1427793c819cSBin Meng 	ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1428d228ca36SBin Meng 	ctrl_ctx->drop_flags = 0;
1429d228ca36SBin Meng 
1430d228ca36SBin Meng 	xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1431d228ca36SBin Meng 
1432d228ca36SBin Meng 	/* slot context */
1433d228ca36SBin Meng 	xhci_slot_copy(ctrl, in_ctx, out_ctx);
1434d228ca36SBin Meng 	slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1435d228ca36SBin Meng 
1436d228ca36SBin Meng 	/* Update hub related fields */
1437d228ca36SBin Meng 	slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
1438eaaefb06SBin Meng 	/*
1439eaaefb06SBin Meng 	 * refer to section 6.2.2: MTT should be 0 for full speed hub,
1440eaaefb06SBin Meng 	 * but it may be already set to 1 when setup an xHCI virtual
1441eaaefb06SBin Meng 	 * device, so clear it anyway.
1442eaaefb06SBin Meng 	 */
1443eaaefb06SBin Meng 	if (hub->tt.multi)
1444d228ca36SBin Meng 		slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1445eaaefb06SBin Meng 	else if (udev->speed == USB_SPEED_FULL)
1446eaaefb06SBin Meng 		slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
1447d228ca36SBin Meng 	slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1448d228ca36SBin Meng 	/*
1449d228ca36SBin Meng 	 * Set TT think time - convert from ns to FS bit times.
1450d228ca36SBin Meng 	 * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1451d228ca36SBin Meng 	 *
1452d228ca36SBin Meng 	 * 0 =  8 FS bit times, 1 = 16 FS bit times,
1453d228ca36SBin Meng 	 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1454d228ca36SBin Meng 	 *
1455d228ca36SBin Meng 	 * This field shall be 0 if the device is not a high-spped hub.
1456d228ca36SBin Meng 	 */
1457d228ca36SBin Meng 	think_time = hub->tt.think_time;
1458d228ca36SBin Meng 	if (think_time != 0)
1459d228ca36SBin Meng 		think_time = (think_time / 666) - 1;
1460d228ca36SBin Meng 	if (udev->speed == USB_SPEED_HIGH)
1461d228ca36SBin Meng 		slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
1462ae751b06SBin Meng 	slot_ctx->dev_state = 0;
1463d228ca36SBin Meng 
1464d228ca36SBin Meng 	return xhci_configure_endpoints(udev, false);
1465d228ca36SBin Meng }
1466d228ca36SBin Meng 
xhci_get_max_xfer_size(struct udevice * dev,size_t * size)1467022ceacaSBin Meng static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1468022ceacaSBin Meng {
1469022ceacaSBin Meng 	/*
1470022ceacaSBin Meng 	 * xHCD allocates one segment which includes 64 TRBs for each endpoint
1471022ceacaSBin Meng 	 * and the last TRB in this segment is configured as a link TRB to form
1472022ceacaSBin Meng 	 * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1473022ceacaSBin Meng 	 * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1474022ceacaSBin Meng 	 * Hence the maximum number of TRBs we can use in one transfer is 62.
1475022ceacaSBin Meng 	 */
1476022ceacaSBin Meng 	*size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1477022ceacaSBin Meng 
1478022ceacaSBin Meng 	return 0;
1479022ceacaSBin Meng }
1480022ceacaSBin Meng 
xhci_register(struct udevice * dev,struct xhci_hccr * hccr,struct xhci_hcor * hcor)1481a5762fe0SSimon Glass int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1482a5762fe0SSimon Glass 		  struct xhci_hcor *hcor)
1483a5762fe0SSimon Glass {
1484a5762fe0SSimon Glass 	struct xhci_ctrl *ctrl = dev_get_priv(dev);
1485a5762fe0SSimon Glass 	struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1486a5762fe0SSimon Glass 	int ret;
1487a5762fe0SSimon Glass 
1488a5762fe0SSimon Glass 	debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1489a5762fe0SSimon Glass 	      ctrl, hccr, hcor);
1490a5762fe0SSimon Glass 
1491a5762fe0SSimon Glass 	ctrl->dev = dev;
1492a5762fe0SSimon Glass 
1493a5762fe0SSimon Glass 	/*
1494a5762fe0SSimon Glass 	 * XHCI needs to issue a Address device command to setup
1495a5762fe0SSimon Glass 	 * proper device context structures, before it can interact
1496a5762fe0SSimon Glass 	 * with the device. So a get_descriptor will fail before any
1497a5762fe0SSimon Glass 	 * of that is done for XHCI unlike EHCI.
1498a5762fe0SSimon Glass 	 */
1499a5762fe0SSimon Glass 	priv->desc_before_addr = false;
1500a5762fe0SSimon Glass 
1501a5762fe0SSimon Glass 	ret = xhci_reset(hcor);
1502a5762fe0SSimon Glass 	if (ret)
1503a5762fe0SSimon Glass 		goto err;
1504a5762fe0SSimon Glass 
1505a5762fe0SSimon Glass 	ctrl->hccr = hccr;
1506a5762fe0SSimon Glass 	ctrl->hcor = hcor;
1507a5762fe0SSimon Glass 	ret = xhci_lowlevel_init(ctrl);
1508a5762fe0SSimon Glass 	if (ret)
1509a5762fe0SSimon Glass 		goto err;
1510a5762fe0SSimon Glass 
1511a5762fe0SSimon Glass 	return 0;
1512a5762fe0SSimon Glass err:
1513a5762fe0SSimon Glass 	free(ctrl);
1514a5762fe0SSimon Glass 	debug("%s: failed, ret=%d\n", __func__, ret);
1515a5762fe0SSimon Glass 	return ret;
1516a5762fe0SSimon Glass }
1517a5762fe0SSimon Glass 
xhci_deregister(struct udevice * dev)1518a5762fe0SSimon Glass int xhci_deregister(struct udevice *dev)
1519a5762fe0SSimon Glass {
1520a5762fe0SSimon Glass 	struct xhci_ctrl *ctrl = dev_get_priv(dev);
1521a5762fe0SSimon Glass 
1522a5762fe0SSimon Glass 	xhci_lowlevel_stop(ctrl);
1523a5762fe0SSimon Glass 	xhci_cleanup(ctrl);
1524a5762fe0SSimon Glass 
1525a5762fe0SSimon Glass 	return 0;
1526a5762fe0SSimon Glass }
1527a5762fe0SSimon Glass 
1528a5762fe0SSimon Glass struct dm_usb_ops xhci_usb_ops = {
1529a5762fe0SSimon Glass 	.control = xhci_submit_control_msg,
1530a5762fe0SSimon Glass 	.bulk = xhci_submit_bulk_msg,
1531a5762fe0SSimon Glass 	.interrupt = xhci_submit_int_msg,
1532a5762fe0SSimon Glass 	.alloc_device = xhci_alloc_device,
1533d228ca36SBin Meng 	.update_hub_device = xhci_update_hub_device,
1534022ceacaSBin Meng 	.get_max_xfer_size  = xhci_get_max_xfer_size,
1535a5762fe0SSimon Glass };
1536a5762fe0SSimon Glass 
1537a5762fe0SSimon Glass #endif
1538