15853e133SVivek Gautam /*
25853e133SVivek Gautam * USB HOST XHCI Controller stack
35853e133SVivek Gautam *
45853e133SVivek Gautam * Based on xHCI host controller driver in linux-kernel
55853e133SVivek Gautam * by Sarah Sharp.
65853e133SVivek Gautam *
75853e133SVivek Gautam * Copyright (C) 2008 Intel Corp.
85853e133SVivek Gautam * Author: Sarah Sharp
95853e133SVivek Gautam *
105853e133SVivek Gautam * Copyright (C) 2013 Samsung Electronics Co.Ltd
115853e133SVivek Gautam * Authors: Vivek Gautam <gautam.vivek@samsung.com>
125853e133SVivek Gautam * Vikas Sajjan <vikas.sajjan@samsung.com>
135853e133SVivek Gautam *
145853e133SVivek Gautam * SPDX-License-Identifier: GPL-2.0+
155853e133SVivek Gautam */
165853e133SVivek Gautam
175853e133SVivek Gautam /**
185853e133SVivek Gautam * This file gives the xhci stack for usb3.0 looking into
195853e133SVivek Gautam * xhci specification Rev1.0 (5/21/10).
205853e133SVivek Gautam * The quirk devices support hasn't been given yet.
215853e133SVivek Gautam */
225853e133SVivek Gautam
235853e133SVivek Gautam #include <common.h>
24a5762fe0SSimon Glass #include <dm.h>
255853e133SVivek Gautam #include <asm/byteorder.h>
265853e133SVivek Gautam #include <usb.h>
275853e133SVivek Gautam #include <malloc.h>
285853e133SVivek Gautam #include <watchdog.h>
295853e133SVivek Gautam #include <asm/cache.h>
305853e133SVivek Gautam #include <asm/unaligned.h>
315d97dff0SMasahiro Yamada #include <linux/errno.h>
32143fc13bSJean-Jacques Hiblot #include <usb/xhci.h>
335853e133SVivek Gautam
345853e133SVivek Gautam #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
355853e133SVivek Gautam #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
365853e133SVivek Gautam #endif
375853e133SVivek Gautam
385853e133SVivek Gautam static struct descriptor {
395853e133SVivek Gautam struct usb_hub_descriptor hub;
405853e133SVivek Gautam struct usb_device_descriptor device;
415853e133SVivek Gautam struct usb_config_descriptor config;
425853e133SVivek Gautam struct usb_interface_descriptor interface;
435853e133SVivek Gautam struct usb_endpoint_descriptor endpoint;
445853e133SVivek Gautam struct usb_ss_ep_comp_descriptor ep_companion;
455853e133SVivek Gautam } __attribute__ ((packed)) descriptor = {
465853e133SVivek Gautam {
475853e133SVivek Gautam 0xc, /* bDescLength */
485853e133SVivek Gautam 0x2a, /* bDescriptorType: hub descriptor */
495853e133SVivek Gautam 2, /* bNrPorts -- runtime modified */
505853e133SVivek Gautam cpu_to_le16(0x8), /* wHubCharacteristics */
515853e133SVivek Gautam 10, /* bPwrOn2PwrGood */
525853e133SVivek Gautam 0, /* bHubCntrCurrent */
53337fc7e6SBin Meng { /* Device removable */
54337fc7e6SBin Meng } /* at most 7 ports! XXX */
555853e133SVivek Gautam },
565853e133SVivek Gautam {
575853e133SVivek Gautam 0x12, /* bLength */
585853e133SVivek Gautam 1, /* bDescriptorType: UDESC_DEVICE */
595853e133SVivek Gautam cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
605853e133SVivek Gautam 9, /* bDeviceClass: UDCLASS_HUB */
615853e133SVivek Gautam 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
625853e133SVivek Gautam 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
635853e133SVivek Gautam 9, /* bMaxPacketSize: 512 bytes 2^9 */
645853e133SVivek Gautam 0x0000, /* idVendor */
655853e133SVivek Gautam 0x0000, /* idProduct */
665853e133SVivek Gautam cpu_to_le16(0x0100), /* bcdDevice */
675853e133SVivek Gautam 1, /* iManufacturer */
685853e133SVivek Gautam 2, /* iProduct */
695853e133SVivek Gautam 0, /* iSerialNumber */
705853e133SVivek Gautam 1 /* bNumConfigurations: 1 */
715853e133SVivek Gautam },
725853e133SVivek Gautam {
735853e133SVivek Gautam 0x9,
745853e133SVivek Gautam 2, /* bDescriptorType: UDESC_CONFIG */
755853e133SVivek Gautam cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
765853e133SVivek Gautam 1, /* bNumInterface */
775853e133SVivek Gautam 1, /* bConfigurationValue */
785853e133SVivek Gautam 0, /* iConfiguration */
795853e133SVivek Gautam 0x40, /* bmAttributes: UC_SELF_POWER */
805853e133SVivek Gautam 0 /* bMaxPower */
815853e133SVivek Gautam },
825853e133SVivek Gautam {
835853e133SVivek Gautam 0x9, /* bLength */
845853e133SVivek Gautam 4, /* bDescriptorType: UDESC_INTERFACE */
855853e133SVivek Gautam 0, /* bInterfaceNumber */
865853e133SVivek Gautam 0, /* bAlternateSetting */
875853e133SVivek Gautam 1, /* bNumEndpoints */
885853e133SVivek Gautam 9, /* bInterfaceClass: UICLASS_HUB */
895853e133SVivek Gautam 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
905853e133SVivek Gautam 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
915853e133SVivek Gautam 0 /* iInterface */
925853e133SVivek Gautam },
935853e133SVivek Gautam {
945853e133SVivek Gautam 0x7, /* bLength */
955853e133SVivek Gautam 5, /* bDescriptorType: UDESC_ENDPOINT */
965853e133SVivek Gautam 0x81, /* bEndpointAddress: IN endpoint 1 */
975853e133SVivek Gautam 3, /* bmAttributes: UE_INTERRUPT */
985853e133SVivek Gautam 8, /* wMaxPacketSize */
995853e133SVivek Gautam 255 /* bInterval */
1005853e133SVivek Gautam },
1015853e133SVivek Gautam {
1025853e133SVivek Gautam 0x06, /* ss_bLength */
1035853e133SVivek Gautam 0x30, /* ss_bDescriptorType: SS EP Companion */
1045853e133SVivek Gautam 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
1055853e133SVivek Gautam /* ss_bmAttributes: 1 packet per service interval */
1065853e133SVivek Gautam 0x00,
1075853e133SVivek Gautam /* ss_wBytesPerInterval: 15 bits for max 15 ports */
1085853e133SVivek Gautam cpu_to_le16(0x02),
1095853e133SVivek Gautam },
1105853e133SVivek Gautam };
1115853e133SVivek Gautam
1123739bf7eSSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
1135853e133SVivek Gautam static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
114a5762fe0SSimon Glass #endif
1155853e133SVivek Gautam
xhci_get_ctrl(struct usb_device * udev)1167c1deec0SSimon Glass struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
1177c1deec0SSimon Glass {
1183739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
119a5762fe0SSimon Glass struct udevice *dev;
120a5762fe0SSimon Glass
121a5762fe0SSimon Glass /* Find the USB controller */
122a5762fe0SSimon Glass for (dev = udev->dev;
123a5762fe0SSimon Glass device_get_uclass_id(dev) != UCLASS_USB;
124a5762fe0SSimon Glass dev = dev->parent)
125a5762fe0SSimon Glass ;
126a5762fe0SSimon Glass return dev_get_priv(dev);
127a5762fe0SSimon Glass #else
1287c1deec0SSimon Glass return udev->controller;
129a5762fe0SSimon Glass #endif
1307c1deec0SSimon Glass }
1317c1deec0SSimon Glass
1325853e133SVivek Gautam /**
1335853e133SVivek Gautam * Waits for as per specified amount of time
1345853e133SVivek Gautam * for the "result" to match with "done"
1355853e133SVivek Gautam *
1365853e133SVivek Gautam * @param ptr pointer to the register to be read
1375853e133SVivek Gautam * @param mask mask for the value read
1385853e133SVivek Gautam * @param done value to be campared with result
1395853e133SVivek Gautam * @param usec time to wait till
1405853e133SVivek Gautam * @return 0 if handshake is success else < 0 on failure
1415853e133SVivek Gautam */
handshake(uint32_t volatile * ptr,uint32_t mask,uint32_t done,int usec)1425853e133SVivek Gautam static int handshake(uint32_t volatile *ptr, uint32_t mask,
1435853e133SVivek Gautam uint32_t done, int usec)
1445853e133SVivek Gautam {
1455853e133SVivek Gautam uint32_t result;
1465853e133SVivek Gautam
1475853e133SVivek Gautam do {
1485853e133SVivek Gautam result = xhci_readl(ptr);
1495853e133SVivek Gautam if (result == ~(uint32_t)0)
1505853e133SVivek Gautam return -ENODEV;
1515853e133SVivek Gautam result &= mask;
1525853e133SVivek Gautam if (result == done)
1535853e133SVivek Gautam return 0;
1545853e133SVivek Gautam usec--;
1555853e133SVivek Gautam udelay(1);
1565853e133SVivek Gautam } while (usec > 0);
1575853e133SVivek Gautam
1585853e133SVivek Gautam return -ETIMEDOUT;
1595853e133SVivek Gautam }
1605853e133SVivek Gautam
1615853e133SVivek Gautam /**
1625853e133SVivek Gautam * Set the run bit and wait for the host to be running.
1635853e133SVivek Gautam *
1645853e133SVivek Gautam * @param hcor pointer to host controller operation registers
1655853e133SVivek Gautam * @return status of the Handshake
1665853e133SVivek Gautam */
xhci_start(struct xhci_hcor * hcor)1675853e133SVivek Gautam static int xhci_start(struct xhci_hcor *hcor)
1685853e133SVivek Gautam {
1695853e133SVivek Gautam u32 temp;
1705853e133SVivek Gautam int ret;
1715853e133SVivek Gautam
1725853e133SVivek Gautam puts("Starting the controller\n");
1735853e133SVivek Gautam temp = xhci_readl(&hcor->or_usbcmd);
1745853e133SVivek Gautam temp |= (CMD_RUN);
1755853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, temp);
1765853e133SVivek Gautam
1775853e133SVivek Gautam /*
1785853e133SVivek Gautam * Wait for the HCHalted Status bit to be 0 to indicate the host is
1795853e133SVivek Gautam * running.
1805853e133SVivek Gautam */
1815853e133SVivek Gautam ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
1825853e133SVivek Gautam if (ret)
1835853e133SVivek Gautam debug("Host took too long to start, "
1845853e133SVivek Gautam "waited %u microseconds.\n",
1855853e133SVivek Gautam XHCI_MAX_HALT_USEC);
1865853e133SVivek Gautam return ret;
1875853e133SVivek Gautam }
1885853e133SVivek Gautam
1895853e133SVivek Gautam /**
1905853e133SVivek Gautam * Resets the XHCI Controller
1915853e133SVivek Gautam *
1925853e133SVivek Gautam * @param hcor pointer to host controller operation registers
1935853e133SVivek Gautam * @return -EBUSY if XHCI Controller is not halted else status of handshake
1945853e133SVivek Gautam */
xhci_reset(struct xhci_hcor * hcor)195121a4d13SMasahiro Yamada static int xhci_reset(struct xhci_hcor *hcor)
1965853e133SVivek Gautam {
1975853e133SVivek Gautam u32 cmd;
1985853e133SVivek Gautam u32 state;
1995853e133SVivek Gautam int ret;
2005853e133SVivek Gautam
2015853e133SVivek Gautam /* Halting the Host first */
202a5ccda47SSergey Temerkhanov debug("// Halt the HC: %p\n", hcor);
2035853e133SVivek Gautam state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
2045853e133SVivek Gautam if (!state) {
2055853e133SVivek Gautam cmd = xhci_readl(&hcor->or_usbcmd);
2065853e133SVivek Gautam cmd &= ~CMD_RUN;
2075853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, cmd);
2085853e133SVivek Gautam }
2095853e133SVivek Gautam
2105853e133SVivek Gautam ret = handshake(&hcor->or_usbsts,
2115853e133SVivek Gautam STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
2125853e133SVivek Gautam if (ret) {
2135853e133SVivek Gautam printf("Host not halted after %u microseconds.\n",
2145853e133SVivek Gautam XHCI_MAX_HALT_USEC);
2155853e133SVivek Gautam return -EBUSY;
2165853e133SVivek Gautam }
2175853e133SVivek Gautam
2185853e133SVivek Gautam debug("// Reset the HC\n");
2195853e133SVivek Gautam cmd = xhci_readl(&hcor->or_usbcmd);
2205853e133SVivek Gautam cmd |= CMD_RESET;
2215853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, cmd);
2225853e133SVivek Gautam
2235853e133SVivek Gautam ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
2245853e133SVivek Gautam if (ret)
2255853e133SVivek Gautam return ret;
2265853e133SVivek Gautam
2275853e133SVivek Gautam /*
2285853e133SVivek Gautam * xHCI cannot write to any doorbells or operational registers other
2295853e133SVivek Gautam * than status until the "Controller Not Ready" flag is cleared.
2305853e133SVivek Gautam */
2315853e133SVivek Gautam return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
2325853e133SVivek Gautam }
2335853e133SVivek Gautam
2345853e133SVivek Gautam /**
2355853e133SVivek Gautam * Used for passing endpoint bitmasks between the core and HCDs.
2365853e133SVivek Gautam * Find the index for an endpoint given its descriptor.
2375853e133SVivek Gautam * Use the return value to right shift 1 for the bitmask.
2385853e133SVivek Gautam *
2395853e133SVivek Gautam * Index = (epnum * 2) + direction - 1,
2405853e133SVivek Gautam * where direction = 0 for OUT, 1 for IN.
2415853e133SVivek Gautam * For control endpoints, the IN index is used (OUT index is unused), so
2425853e133SVivek Gautam * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
2435853e133SVivek Gautam *
2445853e133SVivek Gautam * @param desc USB enpdoint Descriptor
2455853e133SVivek Gautam * @return index of the Endpoint
2465853e133SVivek Gautam */
xhci_get_ep_index(struct usb_endpoint_descriptor * desc)2475853e133SVivek Gautam static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
2485853e133SVivek Gautam {
2495853e133SVivek Gautam unsigned int index;
2505853e133SVivek Gautam
2515853e133SVivek Gautam if (usb_endpoint_xfer_control(desc))
2525853e133SVivek Gautam index = (unsigned int)(usb_endpoint_num(desc) * 2);
2535853e133SVivek Gautam else
2545853e133SVivek Gautam index = (unsigned int)((usb_endpoint_num(desc) * 2) -
2555853e133SVivek Gautam (usb_endpoint_dir_in(desc) ? 0 : 1));
2565853e133SVivek Gautam
2575853e133SVivek Gautam return index;
2585853e133SVivek Gautam }
2595853e133SVivek Gautam
2601fa1b460SBin Meng /*
2611fa1b460SBin Meng * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
2621fa1b460SBin Meng * microframes, rounded down to nearest power of 2.
2631fa1b460SBin Meng */
xhci_microframes_to_exponent(unsigned int desc_interval,unsigned int min_exponent,unsigned int max_exponent)2641fa1b460SBin Meng static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
2651fa1b460SBin Meng unsigned int min_exponent,
2661fa1b460SBin Meng unsigned int max_exponent)
2671fa1b460SBin Meng {
2681fa1b460SBin Meng unsigned int interval;
2691fa1b460SBin Meng
2701fa1b460SBin Meng interval = fls(desc_interval) - 1;
2711fa1b460SBin Meng interval = clamp_val(interval, min_exponent, max_exponent);
2721fa1b460SBin Meng if ((1 << interval) != desc_interval)
2731fa1b460SBin Meng debug("rounding interval to %d microframes, "\
2741fa1b460SBin Meng "ep desc says %d microframes\n",
2751fa1b460SBin Meng 1 << interval, desc_interval);
2761fa1b460SBin Meng
2771fa1b460SBin Meng return interval;
2781fa1b460SBin Meng }
2791fa1b460SBin Meng
xhci_parse_microframe_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)2801fa1b460SBin Meng static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
2811fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc)
2821fa1b460SBin Meng {
2831fa1b460SBin Meng if (endpt_desc->bInterval == 0)
2841fa1b460SBin Meng return 0;
2851fa1b460SBin Meng
2861fa1b460SBin Meng return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
2871fa1b460SBin Meng }
2881fa1b460SBin Meng
xhci_parse_frame_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)2891fa1b460SBin Meng static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
2901fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc)
2911fa1b460SBin Meng {
2921fa1b460SBin Meng return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
2931fa1b460SBin Meng }
2941fa1b460SBin Meng
2951fa1b460SBin Meng /*
2961fa1b460SBin Meng * Convert interval expressed as 2^(bInterval - 1) == interval into
2971fa1b460SBin Meng * straight exponent value 2^n == interval.
2981fa1b460SBin Meng */
xhci_parse_exponent_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)2991fa1b460SBin Meng static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
3001fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc)
3011fa1b460SBin Meng {
3021fa1b460SBin Meng unsigned int interval;
3031fa1b460SBin Meng
3041fa1b460SBin Meng interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
3051fa1b460SBin Meng if (interval != endpt_desc->bInterval - 1)
3061fa1b460SBin Meng debug("ep %#x - rounding interval to %d %sframes\n",
3071fa1b460SBin Meng endpt_desc->bEndpointAddress, 1 << interval,
3081fa1b460SBin Meng udev->speed == USB_SPEED_FULL ? "" : "micro");
3091fa1b460SBin Meng
3101fa1b460SBin Meng if (udev->speed == USB_SPEED_FULL) {
3111fa1b460SBin Meng /*
3121fa1b460SBin Meng * Full speed isoc endpoints specify interval in frames,
3131fa1b460SBin Meng * not microframes. We are using microframes everywhere,
3141fa1b460SBin Meng * so adjust accordingly.
3151fa1b460SBin Meng */
3161fa1b460SBin Meng interval += 3; /* 1 frame = 2^3 uframes */
3171fa1b460SBin Meng }
3181fa1b460SBin Meng
3191fa1b460SBin Meng return interval;
3201fa1b460SBin Meng }
3211fa1b460SBin Meng
3221fa1b460SBin Meng /*
3231fa1b460SBin Meng * Return the polling or NAK interval.
3241fa1b460SBin Meng *
3251fa1b460SBin Meng * The polling interval is expressed in "microframes". If xHCI's Interval field
3261fa1b460SBin Meng * is set to N, it will service the endpoint every 2^(Interval)*125us.
3271fa1b460SBin Meng *
3281fa1b460SBin Meng * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
3291fa1b460SBin Meng * is set to 0.
3301fa1b460SBin Meng */
xhci_get_endpoint_interval(struct usb_device * udev,struct usb_endpoint_descriptor * endpt_desc)3311fa1b460SBin Meng static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
3321fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc)
3331fa1b460SBin Meng {
3341fa1b460SBin Meng unsigned int interval = 0;
3351fa1b460SBin Meng
3361fa1b460SBin Meng switch (udev->speed) {
3371fa1b460SBin Meng case USB_SPEED_HIGH:
3381fa1b460SBin Meng /* Max NAK rate */
3391fa1b460SBin Meng if (usb_endpoint_xfer_control(endpt_desc) ||
3401fa1b460SBin Meng usb_endpoint_xfer_bulk(endpt_desc)) {
3411fa1b460SBin Meng interval = xhci_parse_microframe_interval(udev,
3421fa1b460SBin Meng endpt_desc);
3431fa1b460SBin Meng break;
3441fa1b460SBin Meng }
3451fa1b460SBin Meng /* Fall through - SS and HS isoc/int have same decoding */
3461fa1b460SBin Meng
3471fa1b460SBin Meng case USB_SPEED_SUPER:
3481fa1b460SBin Meng if (usb_endpoint_xfer_int(endpt_desc) ||
3491fa1b460SBin Meng usb_endpoint_xfer_isoc(endpt_desc)) {
3501fa1b460SBin Meng interval = xhci_parse_exponent_interval(udev,
3511fa1b460SBin Meng endpt_desc);
3521fa1b460SBin Meng }
3531fa1b460SBin Meng break;
3541fa1b460SBin Meng
3551fa1b460SBin Meng case USB_SPEED_FULL:
3561fa1b460SBin Meng if (usb_endpoint_xfer_isoc(endpt_desc)) {
3571fa1b460SBin Meng interval = xhci_parse_exponent_interval(udev,
3581fa1b460SBin Meng endpt_desc);
3591fa1b460SBin Meng break;
3601fa1b460SBin Meng }
3611fa1b460SBin Meng /*
3621fa1b460SBin Meng * Fall through for interrupt endpoint interval decoding
3631fa1b460SBin Meng * since it uses the same rules as low speed interrupt
3641fa1b460SBin Meng * endpoints.
3651fa1b460SBin Meng */
3661fa1b460SBin Meng
3671fa1b460SBin Meng case USB_SPEED_LOW:
3681fa1b460SBin Meng if (usb_endpoint_xfer_int(endpt_desc) ||
3691fa1b460SBin Meng usb_endpoint_xfer_isoc(endpt_desc)) {
3701fa1b460SBin Meng interval = xhci_parse_frame_interval(udev, endpt_desc);
3711fa1b460SBin Meng }
3721fa1b460SBin Meng break;
3731fa1b460SBin Meng
3741fa1b460SBin Meng default:
3751fa1b460SBin Meng BUG();
3761fa1b460SBin Meng }
3771fa1b460SBin Meng
3781fa1b460SBin Meng return interval;
3791fa1b460SBin Meng }
3801fa1b460SBin Meng
3811fa1b460SBin Meng /*
3821fa1b460SBin Meng * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
3831fa1b460SBin Meng * High speed endpoint descriptors can define "the number of additional
3841fa1b460SBin Meng * transaction opportunities per microframe", but that goes in the Max Burst
3851fa1b460SBin Meng * endpoint context field.
3861fa1b460SBin 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)3871fa1b460SBin Meng static u32 xhci_get_endpoint_mult(struct usb_device *udev,
3881fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc,
3891fa1b460SBin Meng struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
3901fa1b460SBin Meng {
3911fa1b460SBin Meng if (udev->speed < USB_SPEED_SUPER ||
3921fa1b460SBin Meng !usb_endpoint_xfer_isoc(endpt_desc))
3931fa1b460SBin Meng return 0;
3941fa1b460SBin Meng
3951fa1b460SBin Meng return ss_ep_comp_desc->bmAttributes;
3961fa1b460SBin Meng }
3971fa1b460SBin 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)3982874e912SBin Meng static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
3992874e912SBin Meng struct usb_endpoint_descriptor *endpt_desc,
4002874e912SBin Meng struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
4012874e912SBin Meng {
4022874e912SBin Meng /* Super speed and Plus have max burst in ep companion desc */
4032874e912SBin Meng if (udev->speed >= USB_SPEED_SUPER)
4042874e912SBin Meng return ss_ep_comp_desc->bMaxBurst;
4052874e912SBin Meng
4062874e912SBin Meng if (udev->speed == USB_SPEED_HIGH &&
4072874e912SBin Meng (usb_endpoint_xfer_isoc(endpt_desc) ||
4082874e912SBin Meng usb_endpoint_xfer_int(endpt_desc)))
4092874e912SBin Meng return usb_endpoint_maxp_mult(endpt_desc) - 1;
4102874e912SBin Meng
4112874e912SBin Meng return 0;
4122874e912SBin Meng }
4132874e912SBin Meng
4141fa1b460SBin Meng /*
4151fa1b460SBin Meng * Return the maximum endpoint service interval time (ESIT) payload.
4161fa1b460SBin Meng * Basically, this is the maxpacket size, multiplied by the burst size
4171fa1b460SBin Meng * and mult size.
4181fa1b460SBin 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)4191fa1b460SBin Meng static u32 xhci_get_max_esit_payload(struct usb_device *udev,
4201fa1b460SBin Meng struct usb_endpoint_descriptor *endpt_desc,
4211fa1b460SBin Meng struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
4221fa1b460SBin Meng {
4231fa1b460SBin Meng int max_burst;
4241fa1b460SBin Meng int max_packet;
4251fa1b460SBin Meng
4261fa1b460SBin Meng /* Only applies for interrupt or isochronous endpoints */
4271fa1b460SBin Meng if (usb_endpoint_xfer_control(endpt_desc) ||
4281fa1b460SBin Meng usb_endpoint_xfer_bulk(endpt_desc))
4291fa1b460SBin Meng return 0;
4301fa1b460SBin Meng
4311fa1b460SBin Meng /* SuperSpeed Isoc ep with less than 48k per esit */
4321fa1b460SBin Meng if (udev->speed >= USB_SPEED_SUPER)
4331fa1b460SBin Meng return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
4341fa1b460SBin Meng
4351fa1b460SBin Meng max_packet = usb_endpoint_maxp(endpt_desc);
4361fa1b460SBin Meng max_burst = usb_endpoint_maxp_mult(endpt_desc);
4371fa1b460SBin Meng
4381fa1b460SBin Meng /* A 0 in max burst means 1 transfer per ESIT */
4391fa1b460SBin Meng return max_packet * max_burst;
4401fa1b460SBin Meng }
4411fa1b460SBin Meng
4425853e133SVivek Gautam /**
4435853e133SVivek Gautam * Issue a configure endpoint command or evaluate context command
4445853e133SVivek Gautam * and wait for it to finish.
4455853e133SVivek Gautam *
4465853e133SVivek Gautam * @param udev pointer to the Device Data Structure
4475853e133SVivek Gautam * @param ctx_change flag to indicate the Context has changed or NOT
4485853e133SVivek Gautam * @return 0 on success, -1 on failure
4495853e133SVivek Gautam */
xhci_configure_endpoints(struct usb_device * udev,bool ctx_change)4505853e133SVivek Gautam static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
4515853e133SVivek Gautam {
4525853e133SVivek Gautam struct xhci_container_ctx *in_ctx;
4535853e133SVivek Gautam struct xhci_virt_device *virt_dev;
4547c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
4555853e133SVivek Gautam union xhci_trb *event;
4565853e133SVivek Gautam
4575853e133SVivek Gautam virt_dev = ctrl->devs[udev->slot_id];
4585853e133SVivek Gautam in_ctx = virt_dev->in_ctx;
4595853e133SVivek Gautam
460421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
4615853e133SVivek Gautam xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
4625853e133SVivek Gautam ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
4635853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
464*1422d140SHector Martin if (!event)
465*1422d140SHector Martin return -ETIMEDOUT;
466*1422d140SHector Martin
4675853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
4685853e133SVivek Gautam != udev->slot_id);
4695853e133SVivek Gautam
4705853e133SVivek Gautam switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
4715853e133SVivek Gautam case COMP_SUCCESS:
4725853e133SVivek Gautam debug("Successful %s command\n",
4735853e133SVivek Gautam ctx_change ? "Evaluate Context" : "Configure Endpoint");
4745853e133SVivek Gautam break;
4755853e133SVivek Gautam default:
4765853e133SVivek Gautam printf("ERROR: %s command returned completion code %d.\n",
4775853e133SVivek Gautam ctx_change ? "Evaluate Context" : "Configure Endpoint",
4785853e133SVivek Gautam GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
4795853e133SVivek Gautam return -EINVAL;
4805853e133SVivek Gautam }
4815853e133SVivek Gautam
4825853e133SVivek Gautam xhci_acknowledge_event(ctrl);
4835853e133SVivek Gautam
4845853e133SVivek Gautam return 0;
4855853e133SVivek Gautam }
4865853e133SVivek Gautam
4875853e133SVivek Gautam /**
4885853e133SVivek Gautam * Configure the endpoint, programming the device contexts.
4895853e133SVivek Gautam *
4905853e133SVivek Gautam * @param udev pointer to the USB device structure
4915853e133SVivek Gautam * @return returns the status of the xhci_configure_endpoints
4925853e133SVivek Gautam */
xhci_set_configuration(struct usb_device * udev)4935853e133SVivek Gautam static int xhci_set_configuration(struct usb_device *udev)
4945853e133SVivek Gautam {
4955853e133SVivek Gautam struct xhci_container_ctx *in_ctx;
4965853e133SVivek Gautam struct xhci_container_ctx *out_ctx;
4975853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx;
4985853e133SVivek Gautam struct xhci_slot_ctx *slot_ctx;
4995853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
5005853e133SVivek Gautam int cur_ep;
5015853e133SVivek Gautam int max_ep_flag = 0;
5025853e133SVivek Gautam int ep_index;
5035853e133SVivek Gautam unsigned int dir;
5045853e133SVivek Gautam unsigned int ep_type;
5057c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
5065853e133SVivek Gautam int num_of_ep;
5075853e133SVivek Gautam int ep_flag = 0;
5085853e133SVivek Gautam u64 trb_64 = 0;
5095853e133SVivek Gautam int slot_id = udev->slot_id;
5105853e133SVivek Gautam struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
5115853e133SVivek Gautam struct usb_interface *ifdesc;
5121fa1b460SBin Meng u32 max_esit_payload;
5131fa1b460SBin Meng unsigned int interval;
5141fa1b460SBin Meng unsigned int mult;
5152874e912SBin Meng unsigned int max_burst;
5161fa1b460SBin Meng unsigned int avg_trb_len;
517dd5c0fa3SBin Meng unsigned int err_count = 0;
5185853e133SVivek Gautam
5195853e133SVivek Gautam out_ctx = virt_dev->out_ctx;
5205853e133SVivek Gautam in_ctx = virt_dev->in_ctx;
5215853e133SVivek Gautam
5225853e133SVivek Gautam num_of_ep = udev->config.if_desc[0].no_of_ep;
5235853e133SVivek Gautam ifdesc = &udev->config.if_desc[0];
5245853e133SVivek Gautam
5255853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
526aab0db08SBin Meng /* Initialize the input context control */
527aab0db08SBin Meng ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
5285853e133SVivek Gautam ctrl_ctx->drop_flags = 0;
5295853e133SVivek Gautam
5305853e133SVivek Gautam /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
5315853e133SVivek Gautam for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
5325853e133SVivek Gautam ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
5335853e133SVivek Gautam ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
5345853e133SVivek Gautam if (max_ep_flag < ep_flag)
5355853e133SVivek Gautam max_ep_flag = ep_flag;
5365853e133SVivek Gautam }
5375853e133SVivek Gautam
538421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
5395853e133SVivek Gautam
5405853e133SVivek Gautam /* slot context */
5415853e133SVivek Gautam xhci_slot_copy(ctrl, in_ctx, out_ctx);
5425853e133SVivek Gautam slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
543affcda62SBin Meng slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
5445853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
5455853e133SVivek Gautam
5465853e133SVivek Gautam xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
5475853e133SVivek Gautam
5485853e133SVivek Gautam /* filling up ep contexts */
5495853e133SVivek Gautam for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
5505853e133SVivek Gautam struct usb_endpoint_descriptor *endpt_desc = NULL;
5511fa1b460SBin Meng struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
5525853e133SVivek Gautam
5535853e133SVivek Gautam endpt_desc = &ifdesc->ep_desc[cur_ep];
5541fa1b460SBin Meng ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
5555853e133SVivek Gautam trb_64 = 0;
5565853e133SVivek Gautam
5571fa1b460SBin Meng /*
5581fa1b460SBin Meng * Get values to fill the endpoint context, mostly from ep
5591fa1b460SBin Meng * descriptor. The average TRB buffer lengt for bulk endpoints
5601fa1b460SBin Meng * is unclear as we have no clue on scatter gather list entry
5611fa1b460SBin Meng * size. For Isoc and Int, set it to max available.
5621fa1b460SBin Meng * See xHCI 1.1 spec 4.14.1.1 for details.
5631fa1b460SBin Meng */
5641fa1b460SBin Meng max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
5651fa1b460SBin Meng ss_ep_comp_desc);
5661fa1b460SBin Meng interval = xhci_get_endpoint_interval(udev, endpt_desc);
5671fa1b460SBin Meng mult = xhci_get_endpoint_mult(udev, endpt_desc,
5681fa1b460SBin Meng ss_ep_comp_desc);
5692874e912SBin Meng max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
5702874e912SBin Meng ss_ep_comp_desc);
5711fa1b460SBin Meng avg_trb_len = max_esit_payload;
5721fa1b460SBin Meng
5735853e133SVivek Gautam ep_index = xhci_get_ep_index(endpt_desc);
5745853e133SVivek Gautam ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
5755853e133SVivek Gautam
5765853e133SVivek Gautam /* Allocate the ep rings */
5775853e133SVivek Gautam virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
5785853e133SVivek Gautam if (!virt_dev->eps[ep_index].ring)
5795853e133SVivek Gautam return -ENOMEM;
5805853e133SVivek Gautam
5815853e133SVivek Gautam /*NOTE: ep_desc[0] actually represents EP1 and so on */
5825853e133SVivek Gautam dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
5835853e133SVivek Gautam ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
5841fa1b460SBin Meng
5851fa1b460SBin Meng ep_ctx[ep_index]->ep_info =
5861fa1b460SBin Meng cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
5871fa1b460SBin Meng EP_INTERVAL(interval) | EP_MULT(mult));
5881fa1b460SBin Meng
5895853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 =
5905853e133SVivek Gautam cpu_to_le32(ep_type << EP_TYPE_SHIFT);
5915853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 |=
5925853e133SVivek Gautam cpu_to_le32(MAX_PACKET
5935853e133SVivek Gautam (get_unaligned(&endpt_desc->wMaxPacketSize)));
5945853e133SVivek Gautam
595dd5c0fa3SBin Meng /* Allow 3 retries for everything but isoc, set CErr = 3 */
596dd5c0fa3SBin Meng if (!usb_endpoint_xfer_isoc(endpt_desc))
597dd5c0fa3SBin Meng err_count = 3;
5985853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 |=
5992874e912SBin Meng cpu_to_le32(MAX_BURST(max_burst) |
600dd5c0fa3SBin Meng ERROR_COUNT(err_count));
6015853e133SVivek Gautam
6025853e133SVivek Gautam trb_64 = (uintptr_t)
6035853e133SVivek Gautam virt_dev->eps[ep_index].ring->enqueue;
6045853e133SVivek Gautam ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
6055853e133SVivek Gautam virt_dev->eps[ep_index].ring->cycle_state);
6061fa1b460SBin Meng
607f018b53dSBin Meng /*
608f018b53dSBin Meng * xHCI spec 6.2.3:
609f018b53dSBin Meng * 'Average TRB Length' should be 8 for control endpoints.
610f018b53dSBin Meng */
611f018b53dSBin Meng if (usb_endpoint_xfer_control(endpt_desc))
612f018b53dSBin Meng avg_trb_len = 8;
6131fa1b460SBin Meng ep_ctx[ep_index]->tx_info =
6141fa1b460SBin Meng cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
6151fa1b460SBin Meng EP_AVG_TRB_LENGTH(avg_trb_len));
6165853e133SVivek Gautam }
6175853e133SVivek Gautam
6185853e133SVivek Gautam return xhci_configure_endpoints(udev, false);
6195853e133SVivek Gautam }
6205853e133SVivek Gautam
6215853e133SVivek Gautam /**
6225853e133SVivek Gautam * Issue an Address Device command (which will issue a SetAddress request to
6235853e133SVivek Gautam * the device).
6245853e133SVivek Gautam *
6255853e133SVivek Gautam * @param udev pointer to the Device Data Structure
6265853e133SVivek Gautam * @return 0 if successful else error code on failure
6275853e133SVivek Gautam */
xhci_address_device(struct usb_device * udev,int root_portnr)6285dd75e3bSSimon Glass static int xhci_address_device(struct usb_device *udev, int root_portnr)
6295853e133SVivek Gautam {
6305853e133SVivek Gautam int ret = 0;
6317c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
6325853e133SVivek Gautam struct xhci_slot_ctx *slot_ctx;
6335853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx;
6345853e133SVivek Gautam struct xhci_virt_device *virt_dev;
6355853e133SVivek Gautam int slot_id = udev->slot_id;
6365853e133SVivek Gautam union xhci_trb *event;
6375853e133SVivek Gautam
6385853e133SVivek Gautam virt_dev = ctrl->devs[slot_id];
6395853e133SVivek Gautam
6405853e133SVivek Gautam /*
6415853e133SVivek Gautam * This is the first Set Address since device plug-in
6425853e133SVivek Gautam * so setting up the slot context.
6435853e133SVivek Gautam */
6445dd75e3bSSimon Glass debug("Setting up addressable devices %p\n", ctrl->dcbaa);
645daec4691SBin Meng xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
6465853e133SVivek Gautam
6475853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
6485853e133SVivek Gautam ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
6495853e133SVivek Gautam ctrl_ctx->drop_flags = 0;
6505853e133SVivek Gautam
6515853e133SVivek Gautam xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
6525853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
653*1422d140SHector Martin if (!event)
654*1422d140SHector Martin return -ETIMEDOUT;
655*1422d140SHector Martin
6565853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
6575853e133SVivek Gautam
6585853e133SVivek Gautam switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
6595853e133SVivek Gautam case COMP_CTX_STATE:
6605853e133SVivek Gautam case COMP_EBADSLT:
6615853e133SVivek Gautam printf("Setup ERROR: address device command for slot %d.\n",
6625853e133SVivek Gautam slot_id);
6635853e133SVivek Gautam ret = -EINVAL;
6645853e133SVivek Gautam break;
6655853e133SVivek Gautam case COMP_TX_ERR:
6665853e133SVivek Gautam puts("Device not responding to set address.\n");
6675853e133SVivek Gautam ret = -EPROTO;
6685853e133SVivek Gautam break;
6695853e133SVivek Gautam case COMP_DEV_ERR:
6705853e133SVivek Gautam puts("ERROR: Incompatible device"
6715853e133SVivek Gautam "for address device command.\n");
6725853e133SVivek Gautam ret = -ENODEV;
6735853e133SVivek Gautam break;
6745853e133SVivek Gautam case COMP_SUCCESS:
6755853e133SVivek Gautam debug("Successful Address Device command\n");
6765853e133SVivek Gautam udev->status = 0;
6775853e133SVivek Gautam break;
6785853e133SVivek Gautam default:
6795853e133SVivek Gautam printf("ERROR: unexpected command completion code 0x%x.\n",
6805853e133SVivek Gautam GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
6815853e133SVivek Gautam ret = -EINVAL;
6825853e133SVivek Gautam break;
6835853e133SVivek Gautam }
6845853e133SVivek Gautam
6855853e133SVivek Gautam xhci_acknowledge_event(ctrl);
6865853e133SVivek Gautam
6875853e133SVivek Gautam if (ret < 0)
6885853e133SVivek Gautam /*
6895853e133SVivek Gautam * TODO: Unsuccessful Address Device command shall leave the
6905853e133SVivek Gautam * slot in default state. So, issue Disable Slot command now.
6915853e133SVivek Gautam */
6925853e133SVivek Gautam return ret;
6935853e133SVivek Gautam
694421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
6955853e133SVivek Gautam virt_dev->out_ctx->size);
6965853e133SVivek Gautam slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
6975853e133SVivek Gautam
6985853e133SVivek Gautam debug("xHC internal address is: %d\n",
6995853e133SVivek Gautam le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
7005853e133SVivek Gautam
7015853e133SVivek Gautam return 0;
7025853e133SVivek Gautam }
7035853e133SVivek Gautam
7045853e133SVivek Gautam /**
7055853e133SVivek Gautam * Issue Enable slot command to the controller to allocate
7065853e133SVivek Gautam * device slot and assign the slot id. It fails if the xHC
7075853e133SVivek Gautam * ran out of device slots, the Enable Slot command timed out,
7085853e133SVivek Gautam * or allocating memory failed.
7095853e133SVivek Gautam *
7105853e133SVivek Gautam * @param udev pointer to the Device Data Structure
7115853e133SVivek Gautam * @return Returns 0 on succes else return error code on failure
7125853e133SVivek Gautam */
_xhci_alloc_device(struct usb_device * udev)713121a4d13SMasahiro Yamada static int _xhci_alloc_device(struct usb_device *udev)
7145853e133SVivek Gautam {
7157c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
7165853e133SVivek Gautam union xhci_trb *event;
7175853e133SVivek Gautam int ret;
7185853e133SVivek Gautam
7195853e133SVivek Gautam /*
7205853e133SVivek Gautam * Root hub will be first device to be initailized.
7215853e133SVivek Gautam * If this device is root-hub, don't do any xHC related
7225853e133SVivek Gautam * stuff.
7235853e133SVivek Gautam */
7245853e133SVivek Gautam if (ctrl->rootdev == 0) {
7255853e133SVivek Gautam udev->speed = USB_SPEED_SUPER;
7265853e133SVivek Gautam return 0;
7275853e133SVivek Gautam }
7285853e133SVivek Gautam
7295853e133SVivek Gautam xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
7305853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
731*1422d140SHector Martin if (!event)
732*1422d140SHector Martin return -ETIMEDOUT;
733*1422d140SHector Martin
7345853e133SVivek Gautam BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
7355853e133SVivek Gautam != COMP_SUCCESS);
7365853e133SVivek Gautam
7375853e133SVivek Gautam udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
7385853e133SVivek Gautam
7395853e133SVivek Gautam xhci_acknowledge_event(ctrl);
7405853e133SVivek Gautam
7417e0c5ee8SSimon Glass ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
7425853e133SVivek Gautam if (ret < 0) {
7435853e133SVivek Gautam /*
7445853e133SVivek Gautam * TODO: Unsuccessful Address Device command shall leave
7455853e133SVivek Gautam * the slot in default. So, issue Disable Slot command now.
7465853e133SVivek Gautam */
7475853e133SVivek Gautam puts("Could not allocate xHCI USB device data structures\n");
7485853e133SVivek Gautam return ret;
7495853e133SVivek Gautam }
7505853e133SVivek Gautam
7515853e133SVivek Gautam return 0;
7525853e133SVivek Gautam }
7535853e133SVivek Gautam
7543739bf7eSSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
usb_alloc_device(struct usb_device * udev)755a5762fe0SSimon Glass int usb_alloc_device(struct usb_device *udev)
756a5762fe0SSimon Glass {
757a5762fe0SSimon Glass return _xhci_alloc_device(udev);
758a5762fe0SSimon Glass }
759a5762fe0SSimon Glass #endif
760a5762fe0SSimon Glass
7615853e133SVivek Gautam /*
7625853e133SVivek Gautam * Full speed devices may have a max packet size greater than 8 bytes, but the
7635853e133SVivek Gautam * USB core doesn't know that until it reads the first 8 bytes of the
7645853e133SVivek Gautam * descriptor. If the usb_device's max packet size changes after that point,
7655853e133SVivek Gautam * we need to issue an evaluate context command and wait on it.
7665853e133SVivek Gautam *
7675853e133SVivek Gautam * @param udev pointer to the Device Data Structure
7685853e133SVivek Gautam * @return returns the status of the xhci_configure_endpoints
7695853e133SVivek Gautam */
xhci_check_maxpacket(struct usb_device * udev)7705853e133SVivek Gautam int xhci_check_maxpacket(struct usb_device *udev)
7715853e133SVivek Gautam {
7727c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
7735853e133SVivek Gautam unsigned int slot_id = udev->slot_id;
7745853e133SVivek Gautam int ep_index = 0; /* control endpoint */
7755853e133SVivek Gautam struct xhci_container_ctx *in_ctx;
7765853e133SVivek Gautam struct xhci_container_ctx *out_ctx;
7775853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx;
7785853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx;
7795853e133SVivek Gautam int max_packet_size;
7805853e133SVivek Gautam int hw_max_packet_size;
7815853e133SVivek Gautam int ret = 0;
7825853e133SVivek Gautam
7835853e133SVivek Gautam out_ctx = ctrl->devs[slot_id]->out_ctx;
784421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
7855853e133SVivek Gautam
7865853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
7875853e133SVivek Gautam hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
78824acdf7aSBin Meng max_packet_size = udev->epmaxpacketin[0];
7895853e133SVivek Gautam if (hw_max_packet_size != max_packet_size) {
7905853e133SVivek Gautam debug("Max Packet Size for ep 0 changed.\n");
7915853e133SVivek Gautam debug("Max packet size in usb_device = %d\n", max_packet_size);
7925853e133SVivek Gautam debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
7935853e133SVivek Gautam debug("Issuing evaluate context command.\n");
7945853e133SVivek Gautam
7955853e133SVivek Gautam /* Set up the modified control endpoint 0 */
7965853e133SVivek Gautam xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
7975853e133SVivek Gautam ctrl->devs[slot_id]->out_ctx, ep_index);
7985853e133SVivek Gautam in_ctx = ctrl->devs[slot_id]->in_ctx;
7995853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
80024acdf7aSBin Meng ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
80124acdf7aSBin Meng << MAX_PACKET_SHIFT));
8025853e133SVivek Gautam ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
8035853e133SVivek Gautam
8045853e133SVivek Gautam /*
8055853e133SVivek Gautam * Set up the input context flags for the command
8065853e133SVivek Gautam * FIXME: This won't work if a non-default control endpoint
8075853e133SVivek Gautam * changes max packet sizes.
8085853e133SVivek Gautam */
8095853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
8105853e133SVivek Gautam ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
8115853e133SVivek Gautam ctrl_ctx->drop_flags = 0;
8125853e133SVivek Gautam
8135853e133SVivek Gautam ret = xhci_configure_endpoints(udev, true);
8145853e133SVivek Gautam }
8155853e133SVivek Gautam return ret;
8165853e133SVivek Gautam }
8175853e133SVivek Gautam
8185853e133SVivek Gautam /**
8195853e133SVivek Gautam * Clears the Change bits of the Port Status Register
8205853e133SVivek Gautam *
8215853e133SVivek Gautam * @param wValue request value
8225853e133SVivek Gautam * @param wIndex request index
8235853e133SVivek Gautam * @param addr address of posrt status register
8245853e133SVivek Gautam * @param port_status state of port status register
8255853e133SVivek Gautam * @return none
8265853e133SVivek Gautam */
xhci_clear_port_change_bit(u16 wValue,u16 wIndex,volatile uint32_t * addr,u32 port_status)8275853e133SVivek Gautam static void xhci_clear_port_change_bit(u16 wValue,
8285853e133SVivek Gautam u16 wIndex, volatile uint32_t *addr, u32 port_status)
8295853e133SVivek Gautam {
8305853e133SVivek Gautam char *port_change_bit;
8315853e133SVivek Gautam u32 status;
8325853e133SVivek Gautam
8335853e133SVivek Gautam switch (wValue) {
8345853e133SVivek Gautam case USB_PORT_FEAT_C_RESET:
8355853e133SVivek Gautam status = PORT_RC;
8365853e133SVivek Gautam port_change_bit = "reset";
8375853e133SVivek Gautam break;
8385853e133SVivek Gautam case USB_PORT_FEAT_C_CONNECTION:
8395853e133SVivek Gautam status = PORT_CSC;
8405853e133SVivek Gautam port_change_bit = "connect";
8415853e133SVivek Gautam break;
8425853e133SVivek Gautam case USB_PORT_FEAT_C_OVER_CURRENT:
8435853e133SVivek Gautam status = PORT_OCC;
8445853e133SVivek Gautam port_change_bit = "over-current";
8455853e133SVivek Gautam break;
8465853e133SVivek Gautam case USB_PORT_FEAT_C_ENABLE:
8475853e133SVivek Gautam status = PORT_PEC;
8485853e133SVivek Gautam port_change_bit = "enable/disable";
8495853e133SVivek Gautam break;
8505853e133SVivek Gautam case USB_PORT_FEAT_C_SUSPEND:
8515853e133SVivek Gautam status = PORT_PLC;
8525853e133SVivek Gautam port_change_bit = "suspend/resume";
8535853e133SVivek Gautam break;
8545853e133SVivek Gautam default:
8555853e133SVivek Gautam /* Should never happen */
8565853e133SVivek Gautam return;
8575853e133SVivek Gautam }
8585853e133SVivek Gautam
8595853e133SVivek Gautam /* Change bits are all write 1 to clear */
8605853e133SVivek Gautam xhci_writel(addr, port_status | status);
8615853e133SVivek Gautam
8625853e133SVivek Gautam port_status = xhci_readl(addr);
8635853e133SVivek Gautam debug("clear port %s change, actual port %d status = 0x%x\n",
8645853e133SVivek Gautam port_change_bit, wIndex, port_status);
8655853e133SVivek Gautam }
8665853e133SVivek Gautam
8675853e133SVivek Gautam /**
8685853e133SVivek Gautam * Save Read Only (RO) bits and save read/write bits where
8695853e133SVivek Gautam * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
8705853e133SVivek Gautam * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
8715853e133SVivek Gautam *
8725853e133SVivek Gautam * @param state state of the Port Status and Control Regsiter
8735853e133SVivek Gautam * @return a value that would result in the port being in the
8745853e133SVivek Gautam * same state, if the value was written to the port
8755853e133SVivek Gautam * status control register.
8765853e133SVivek Gautam */
xhci_port_state_to_neutral(u32 state)8775853e133SVivek Gautam static u32 xhci_port_state_to_neutral(u32 state)
8785853e133SVivek Gautam {
8795853e133SVivek Gautam /* Save read-only status and port state */
8805853e133SVivek Gautam return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
8815853e133SVivek Gautam }
8825853e133SVivek Gautam
8835853e133SVivek Gautam /**
8845853e133SVivek Gautam * Submits the Requests to the XHCI Host Controller
8855853e133SVivek Gautam *
8865853e133SVivek Gautam * @param udev pointer to the USB device structure
8875853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum
8885853e133SVivek Gautam * @param buffer buffer to be read/written based on the request
8895853e133SVivek Gautam * @return returns 0 if successful else -1 on failure
8905853e133SVivek Gautam */
xhci_submit_root(struct usb_device * udev,unsigned long pipe,void * buffer,struct devrequest * req)8915853e133SVivek Gautam static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
8925853e133SVivek Gautam void *buffer, struct devrequest *req)
8935853e133SVivek Gautam {
8945853e133SVivek Gautam uint8_t tmpbuf[4];
8955853e133SVivek Gautam u16 typeReq;
8965853e133SVivek Gautam void *srcptr = NULL;
8975853e133SVivek Gautam int len, srclen;
8985853e133SVivek Gautam uint32_t reg;
8995853e133SVivek Gautam volatile uint32_t *status_reg;
9007c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
9017274671eSBin Meng struct xhci_hccr *hccr = ctrl->hccr;
9025853e133SVivek Gautam struct xhci_hcor *hcor = ctrl->hcor;
9037274671eSBin Meng int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
9045853e133SVivek Gautam
90525d1936aSJeroen Hofstee if ((req->requesttype & USB_RT_PORT) &&
9067274671eSBin Meng le16_to_cpu(req->index) > max_ports) {
9077274671eSBin Meng printf("The request port(%d) exceeds maximum port number\n",
9085853e133SVivek Gautam le16_to_cpu(req->index) - 1);
9095853e133SVivek Gautam return -EINVAL;
9105853e133SVivek Gautam }
9115853e133SVivek Gautam
9125853e133SVivek Gautam status_reg = (volatile uint32_t *)
9135853e133SVivek Gautam (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
9145853e133SVivek Gautam srclen = 0;
9155853e133SVivek Gautam
9165853e133SVivek Gautam typeReq = req->request | req->requesttype << 8;
9175853e133SVivek Gautam
9185853e133SVivek Gautam switch (typeReq) {
9195853e133SVivek Gautam case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
9205853e133SVivek Gautam switch (le16_to_cpu(req->value) >> 8) {
9215853e133SVivek Gautam case USB_DT_DEVICE:
9225853e133SVivek Gautam debug("USB_DT_DEVICE request\n");
9235853e133SVivek Gautam srcptr = &descriptor.device;
9245853e133SVivek Gautam srclen = 0x12;
9255853e133SVivek Gautam break;
9265853e133SVivek Gautam case USB_DT_CONFIG:
9275853e133SVivek Gautam debug("USB_DT_CONFIG config\n");
9285853e133SVivek Gautam srcptr = &descriptor.config;
9295853e133SVivek Gautam srclen = 0x19;
9305853e133SVivek Gautam break;
9315853e133SVivek Gautam case USB_DT_STRING:
9325853e133SVivek Gautam debug("USB_DT_STRING config\n");
9335853e133SVivek Gautam switch (le16_to_cpu(req->value) & 0xff) {
9345853e133SVivek Gautam case 0: /* Language */
9355853e133SVivek Gautam srcptr = "\4\3\11\4";
9365853e133SVivek Gautam srclen = 4;
9375853e133SVivek Gautam break;
9385853e133SVivek Gautam case 1: /* Vendor String */
939f161c178SSimon Glass srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
9405853e133SVivek Gautam srclen = 14;
9415853e133SVivek Gautam break;
9425853e133SVivek Gautam case 2: /* Product Name */
9435853e133SVivek Gautam srcptr = "\52\3X\0H\0C\0I\0 "
9445853e133SVivek Gautam "\0H\0o\0s\0t\0 "
9455853e133SVivek Gautam "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
9465853e133SVivek Gautam srclen = 42;
9475853e133SVivek Gautam break;
9485853e133SVivek Gautam default:
9495853e133SVivek Gautam printf("unknown value DT_STRING %x\n",
9505853e133SVivek Gautam le16_to_cpu(req->value));
9515853e133SVivek Gautam goto unknown;
9525853e133SVivek Gautam }
9535853e133SVivek Gautam break;
9545853e133SVivek Gautam default:
9555853e133SVivek Gautam printf("unknown value %x\n", le16_to_cpu(req->value));
9565853e133SVivek Gautam goto unknown;
9575853e133SVivek Gautam }
9585853e133SVivek Gautam break;
9595853e133SVivek Gautam case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
9605853e133SVivek Gautam switch (le16_to_cpu(req->value) >> 8) {
9615853e133SVivek Gautam case USB_DT_HUB:
962f3421196SBin Meng case USB_DT_SS_HUB:
9635853e133SVivek Gautam debug("USB_DT_HUB config\n");
964ce2f4ca4SFrank Wang srcptr = &ctrl->hub;
9655853e133SVivek Gautam srclen = 0x8;
9665853e133SVivek Gautam break;
9675853e133SVivek Gautam default:
9685853e133SVivek Gautam printf("unknown value %x\n", le16_to_cpu(req->value));
9695853e133SVivek Gautam goto unknown;
9705853e133SVivek Gautam }
9715853e133SVivek Gautam break;
9725853e133SVivek Gautam case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
9735853e133SVivek Gautam debug("USB_REQ_SET_ADDRESS\n");
9745853e133SVivek Gautam ctrl->rootdev = le16_to_cpu(req->value);
9755853e133SVivek Gautam break;
9765853e133SVivek Gautam case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
9775853e133SVivek Gautam /* Do nothing */
9785853e133SVivek Gautam break;
9795853e133SVivek Gautam case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
9805853e133SVivek Gautam tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
9815853e133SVivek Gautam tmpbuf[1] = 0;
9825853e133SVivek Gautam srcptr = tmpbuf;
9835853e133SVivek Gautam srclen = 2;
9845853e133SVivek Gautam break;
9855853e133SVivek Gautam case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
9865853e133SVivek Gautam memset(tmpbuf, 0, 4);
9875853e133SVivek Gautam reg = xhci_readl(status_reg);
9885853e133SVivek Gautam if (reg & PORT_CONNECT) {
9895853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
9905853e133SVivek Gautam switch (reg & DEV_SPEED_MASK) {
9915853e133SVivek Gautam case XDEV_FS:
9925853e133SVivek Gautam debug("SPEED = FULLSPEED\n");
9935853e133SVivek Gautam break;
9945853e133SVivek Gautam case XDEV_LS:
9955853e133SVivek Gautam debug("SPEED = LOWSPEED\n");
9965853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
9975853e133SVivek Gautam break;
9985853e133SVivek Gautam case XDEV_HS:
9995853e133SVivek Gautam debug("SPEED = HIGHSPEED\n");
10005853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
10015853e133SVivek Gautam break;
10025853e133SVivek Gautam case XDEV_SS:
10035853e133SVivek Gautam debug("SPEED = SUPERSPEED\n");
10045853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
10055853e133SVivek Gautam break;
10065853e133SVivek Gautam }
10075853e133SVivek Gautam }
10085853e133SVivek Gautam if (reg & PORT_PE)
10095853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_ENABLE;
10105853e133SVivek Gautam if ((reg & PORT_PLS_MASK) == XDEV_U3)
10115853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
10125853e133SVivek Gautam if (reg & PORT_OC)
10135853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
10145853e133SVivek Gautam if (reg & PORT_RESET)
10155853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_RESET;
10165853e133SVivek Gautam if (reg & PORT_POWER)
10175853e133SVivek Gautam /*
10185853e133SVivek Gautam * XXX: This Port power bit (for USB 3.0 hub)
10195853e133SVivek Gautam * we are faking in USB 2.0 hub port status;
10205853e133SVivek Gautam * since there's a change in bit positions in
10215853e133SVivek Gautam * two:
10225853e133SVivek Gautam * USB 2.0 port status PP is at position[8]
10235853e133SVivek Gautam * USB 3.0 port status PP is at position[9]
10245853e133SVivek Gautam * So, we are still keeping it at position [8]
10255853e133SVivek Gautam */
10265853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
10275853e133SVivek Gautam if (reg & PORT_CSC)
10285853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
10295853e133SVivek Gautam if (reg & PORT_PEC)
10305853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
10315853e133SVivek Gautam if (reg & PORT_OCC)
10325853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
10335853e133SVivek Gautam if (reg & PORT_RC)
10345853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_RESET;
10355853e133SVivek Gautam
10365853e133SVivek Gautam srcptr = tmpbuf;
10375853e133SVivek Gautam srclen = 4;
10385853e133SVivek Gautam break;
10395853e133SVivek Gautam case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
10405853e133SVivek Gautam reg = xhci_readl(status_reg);
10415853e133SVivek Gautam reg = xhci_port_state_to_neutral(reg);
10425853e133SVivek Gautam switch (le16_to_cpu(req->value)) {
10435853e133SVivek Gautam case USB_PORT_FEAT_ENABLE:
10445853e133SVivek Gautam reg |= PORT_PE;
10455853e133SVivek Gautam xhci_writel(status_reg, reg);
10465853e133SVivek Gautam break;
10475853e133SVivek Gautam case USB_PORT_FEAT_POWER:
10485853e133SVivek Gautam reg |= PORT_POWER;
10495853e133SVivek Gautam xhci_writel(status_reg, reg);
10505853e133SVivek Gautam break;
10515853e133SVivek Gautam case USB_PORT_FEAT_RESET:
10525853e133SVivek Gautam reg |= PORT_RESET;
10535853e133SVivek Gautam xhci_writel(status_reg, reg);
10545853e133SVivek Gautam break;
10555853e133SVivek Gautam default:
10565853e133SVivek Gautam printf("unknown feature %x\n", le16_to_cpu(req->value));
10575853e133SVivek Gautam goto unknown;
10585853e133SVivek Gautam }
10595853e133SVivek Gautam break;
10605853e133SVivek Gautam case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
10615853e133SVivek Gautam reg = xhci_readl(status_reg);
10625853e133SVivek Gautam reg = xhci_port_state_to_neutral(reg);
10635853e133SVivek Gautam switch (le16_to_cpu(req->value)) {
10645853e133SVivek Gautam case USB_PORT_FEAT_ENABLE:
10655853e133SVivek Gautam reg &= ~PORT_PE;
10665853e133SVivek Gautam break;
10675853e133SVivek Gautam case USB_PORT_FEAT_POWER:
10685853e133SVivek Gautam reg &= ~PORT_POWER;
10695853e133SVivek Gautam break;
10705853e133SVivek Gautam case USB_PORT_FEAT_C_RESET:
10715853e133SVivek Gautam case USB_PORT_FEAT_C_CONNECTION:
10725853e133SVivek Gautam case USB_PORT_FEAT_C_OVER_CURRENT:
10735853e133SVivek Gautam case USB_PORT_FEAT_C_ENABLE:
10745853e133SVivek Gautam xhci_clear_port_change_bit((le16_to_cpu(req->value)),
10755853e133SVivek Gautam le16_to_cpu(req->index),
10765853e133SVivek Gautam status_reg, reg);
10775853e133SVivek Gautam break;
10785853e133SVivek Gautam default:
10795853e133SVivek Gautam printf("unknown feature %x\n", le16_to_cpu(req->value));
10805853e133SVivek Gautam goto unknown;
10815853e133SVivek Gautam }
10825853e133SVivek Gautam xhci_writel(status_reg, reg);
10835853e133SVivek Gautam break;
10845853e133SVivek Gautam default:
10855853e133SVivek Gautam puts("Unknown request\n");
10865853e133SVivek Gautam goto unknown;
10875853e133SVivek Gautam }
10885853e133SVivek Gautam
10895853e133SVivek Gautam debug("scrlen = %d\n req->length = %d\n",
10905853e133SVivek Gautam srclen, le16_to_cpu(req->length));
10915853e133SVivek Gautam
1092b4141195SMasahiro Yamada len = min(srclen, (int)le16_to_cpu(req->length));
10935853e133SVivek Gautam
10945853e133SVivek Gautam if (srcptr != NULL && len > 0)
10955853e133SVivek Gautam memcpy(buffer, srcptr, len);
10965853e133SVivek Gautam else
10975853e133SVivek Gautam debug("Len is 0\n");
10985853e133SVivek Gautam
10995853e133SVivek Gautam udev->act_len = len;
11005853e133SVivek Gautam udev->status = 0;
11015853e133SVivek Gautam
11025853e133SVivek Gautam return 0;
11035853e133SVivek Gautam
11045853e133SVivek Gautam unknown:
11055853e133SVivek Gautam udev->act_len = 0;
11065853e133SVivek Gautam udev->status = USB_ST_STALLED;
11075853e133SVivek Gautam
11085853e133SVivek Gautam return -ENODEV;
11095853e133SVivek Gautam }
11105853e133SVivek Gautam
11115853e133SVivek Gautam /**
11125853e133SVivek Gautam * Submits the INT request to XHCI Host cotroller
11135853e133SVivek Gautam *
11145853e133SVivek Gautam * @param udev pointer to the USB device
11155853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum
11165853e133SVivek Gautam * @param buffer buffer to be read/written based on the request
11175853e133SVivek Gautam * @param length length of the buffer
11185853e133SVivek Gautam * @param interval interval of the interrupt
11195853e133SVivek Gautam * @return 0
11205853e133SVivek Gautam */
_xhci_submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1121a5762fe0SSimon Glass static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
112292937b1fSMichal Suchanek void *buffer, int length, int interval,
112392937b1fSMichal Suchanek bool nonblock)
11245853e133SVivek Gautam {
112583e13f0fSBin Meng if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
112683e13f0fSBin Meng printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
11275853e133SVivek Gautam return -EINVAL;
11285853e133SVivek Gautam }
11295853e133SVivek Gautam
113083e13f0fSBin Meng /*
113183e13f0fSBin Meng * xHCI uses normal TRBs for both bulk and interrupt. When the
113283e13f0fSBin Meng * interrupt endpoint is to be serviced, the xHC will consume
113383e13f0fSBin Meng * (at most) one TD. A TD (comprised of sg list entries) can
113483e13f0fSBin Meng * take several service intervals to transmit.
113583e13f0fSBin Meng */
113683e13f0fSBin Meng return xhci_bulk_tx(udev, pipe, length, buffer);
113783e13f0fSBin Meng }
113883e13f0fSBin Meng
11395853e133SVivek Gautam /**
11405853e133SVivek Gautam * submit the BULK type of request to the USB Device
11415853e133SVivek Gautam *
11425853e133SVivek Gautam * @param udev pointer to the USB device
11435853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum
11445853e133SVivek Gautam * @param buffer buffer to be read/written based on the request
11455853e133SVivek Gautam * @param length length of the buffer
11465853e133SVivek Gautam * @return returns 0 if successful else -1 on failure
11475853e133SVivek Gautam */
_xhci_submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1148a5762fe0SSimon Glass static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1149a5762fe0SSimon Glass void *buffer, int length)
11505853e133SVivek Gautam {
11515853e133SVivek Gautam if (usb_pipetype(pipe) != PIPE_BULK) {
11525853e133SVivek Gautam printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
11535853e133SVivek Gautam return -EINVAL;
11545853e133SVivek Gautam }
11555853e133SVivek Gautam
11565853e133SVivek Gautam return xhci_bulk_tx(udev, pipe, length, buffer);
11575853e133SVivek Gautam }
11585853e133SVivek Gautam
11595853e133SVivek Gautam /**
11605853e133SVivek Gautam * submit the control type of request to the Root hub/Device based on the devnum
11615853e133SVivek Gautam *
11625853e133SVivek Gautam * @param udev pointer to the USB device
11635853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum
11645853e133SVivek Gautam * @param buffer buffer to be read/written based on the request
11655853e133SVivek Gautam * @param length length of the buffer
11665853e133SVivek Gautam * @param setup Request type
11675dd75e3bSSimon Glass * @param root_portnr Root port number that this device is on
11685853e133SVivek Gautam * @return returns 0 if successful else -1 on failure
11695853e133SVivek Gautam */
_xhci_submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup,int root_portnr)11705dd75e3bSSimon Glass static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
11715dd75e3bSSimon Glass void *buffer, int length,
11725dd75e3bSSimon Glass struct devrequest *setup, int root_portnr)
11735853e133SVivek Gautam {
11747c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
11755853e133SVivek Gautam int ret = 0;
11765853e133SVivek Gautam
11775853e133SVivek Gautam if (usb_pipetype(pipe) != PIPE_CONTROL) {
11785853e133SVivek Gautam printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
11795853e133SVivek Gautam return -EINVAL;
11805853e133SVivek Gautam }
11815853e133SVivek Gautam
11825853e133SVivek Gautam if (usb_pipedevice(pipe) == ctrl->rootdev)
11835853e133SVivek Gautam return xhci_submit_root(udev, pipe, buffer, setup);
11845853e133SVivek Gautam
11851b108880STed Chen if (setup->request == USB_REQ_SET_ADDRESS &&
11861b108880STed Chen (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
11875dd75e3bSSimon Glass return xhci_address_device(udev, root_portnr);
11885853e133SVivek Gautam
11891b108880STed Chen if (setup->request == USB_REQ_SET_CONFIGURATION &&
11901b108880STed Chen (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
11915853e133SVivek Gautam ret = xhci_set_configuration(udev);
11925853e133SVivek Gautam if (ret) {
11935853e133SVivek Gautam puts("Failed to configure xHCI endpoint\n");
11945853e133SVivek Gautam return ret;
11955853e133SVivek Gautam }
11965853e133SVivek Gautam }
11975853e133SVivek Gautam
11985853e133SVivek Gautam return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
11995853e133SVivek Gautam }
12005853e133SVivek Gautam
xhci_lowlevel_init(struct xhci_ctrl * ctrl)1201779d1263SSimon Glass static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
12025853e133SVivek Gautam {
1203779d1263SSimon Glass struct xhci_hccr *hccr;
1204779d1263SSimon Glass struct xhci_hcor *hcor;
12055853e133SVivek Gautam uint32_t val;
12065853e133SVivek Gautam uint32_t val2;
12075853e133SVivek Gautam uint32_t reg;
12085853e133SVivek Gautam
1209779d1263SSimon Glass hccr = ctrl->hccr;
1210779d1263SSimon Glass hcor = ctrl->hcor;
12115853e133SVivek Gautam /*
12125853e133SVivek Gautam * Program the Number of Device Slots Enabled field in the CONFIG
12135853e133SVivek Gautam * register with the max value of slots the HC can handle.
12145853e133SVivek Gautam */
12155853e133SVivek Gautam val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
12165853e133SVivek Gautam val2 = xhci_readl(&hcor->or_config);
12175853e133SVivek Gautam val |= (val2 & ~HCS_SLOTS_MASK);
12185853e133SVivek Gautam xhci_writel(&hcor->or_config, val);
12195853e133SVivek Gautam
12205853e133SVivek Gautam /* initializing xhci data structures */
12215853e133SVivek Gautam if (xhci_mem_init(ctrl, hccr, hcor) < 0)
12225853e133SVivek Gautam return -ENOMEM;
12235853e133SVivek Gautam
12245853e133SVivek Gautam reg = xhci_readl(&hccr->cr_hcsparams1);
12255853e133SVivek Gautam descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
12265853e133SVivek Gautam HCS_MAX_PORTS_SHIFT);
12275853e133SVivek Gautam printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
12285853e133SVivek Gautam
12295853e133SVivek Gautam /* Port Indicators */
12305853e133SVivek Gautam reg = xhci_readl(&hccr->cr_hccparams);
12315853e133SVivek Gautam if (HCS_INDICATOR(reg))
12325853e133SVivek Gautam put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
12335853e133SVivek Gautam | 0x80, &descriptor.hub.wHubCharacteristics);
12345853e133SVivek Gautam
12355853e133SVivek Gautam /* Port Power Control */
12365853e133SVivek Gautam if (HCC_PPC(reg))
12375853e133SVivek Gautam put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
12385853e133SVivek Gautam | 0x01, &descriptor.hub.wHubCharacteristics);
12395853e133SVivek Gautam
1240ce2f4ca4SFrank Wang memcpy(&ctrl->hub, &descriptor, sizeof(struct usb_hub_descriptor));
1241ce2f4ca4SFrank Wang
12425853e133SVivek Gautam if (xhci_start(hcor)) {
12435853e133SVivek Gautam xhci_reset(hcor);
12445853e133SVivek Gautam return -ENODEV;
12455853e133SVivek Gautam }
12465853e133SVivek Gautam
12475853e133SVivek Gautam /* Zero'ing IRQ control register and IRQ pending register */
12485853e133SVivek Gautam xhci_writel(&ctrl->ir_set->irq_control, 0x0);
12495853e133SVivek Gautam xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
12505853e133SVivek Gautam
12515853e133SVivek Gautam reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
12525853e133SVivek Gautam printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
12535853e133SVivek Gautam
1254779d1263SSimon Glass return 0;
1255779d1263SSimon Glass }
1256779d1263SSimon Glass
xhci_lowlevel_stop(struct xhci_ctrl * ctrl)1257779d1263SSimon Glass static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1258779d1263SSimon Glass {
1259779d1263SSimon Glass u32 temp;
1260779d1263SSimon Glass
1261779d1263SSimon Glass xhci_reset(ctrl->hcor);
1262779d1263SSimon Glass
1263779d1263SSimon Glass debug("// Disabling event ring interrupts\n");
1264779d1263SSimon Glass temp = xhci_readl(&ctrl->hcor->or_usbsts);
1265779d1263SSimon Glass xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1266779d1263SSimon Glass temp = xhci_readl(&ctrl->ir_set->irq_pending);
1267779d1263SSimon Glass xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
12685853e133SVivek Gautam
12695853e133SVivek Gautam return 0;
12705853e133SVivek Gautam }
12715853e133SVivek Gautam
12723739bf7eSSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)12735dd75e3bSSimon Glass int submit_control_msg(struct usb_device *udev, unsigned long pipe,
12745dd75e3bSSimon Glass void *buffer, int length, struct devrequest *setup)
12755dd75e3bSSimon Glass {
12765dd75e3bSSimon Glass struct usb_device *hop = udev;
12775dd75e3bSSimon Glass
12785dd75e3bSSimon Glass if (hop->parent)
12795dd75e3bSSimon Glass while (hop->parent->parent)
12805dd75e3bSSimon Glass hop = hop->parent;
12815dd75e3bSSimon Glass
12825dd75e3bSSimon Glass return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
12835dd75e3bSSimon Glass hop->portnr);
12845dd75e3bSSimon Glass }
12855dd75e3bSSimon Glass
submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)1286a5762fe0SSimon Glass int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1287a5762fe0SSimon Glass int length)
1288a5762fe0SSimon Glass {
1289a5762fe0SSimon Glass return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1290a5762fe0SSimon Glass }
1291a5762fe0SSimon Glass
submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1292a5762fe0SSimon Glass int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
129392937b1fSMichal Suchanek int length, int interval, bool nonblock)
1294a5762fe0SSimon Glass {
129592937b1fSMichal Suchanek return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
129692937b1fSMichal Suchanek nonblock);
1297a5762fe0SSimon Glass }
1298a5762fe0SSimon Glass
12995853e133SVivek Gautam /**
1300779d1263SSimon Glass * Intialises the XHCI host controller
1301779d1263SSimon Glass * and allocates the necessary data structures
1302779d1263SSimon Glass *
1303779d1263SSimon Glass * @param index index to the host controller data structure
1304779d1263SSimon Glass * @return pointer to the intialised controller
1305779d1263SSimon Glass */
usb_lowlevel_init(int index,enum usb_init_type init,void ** controller)1306779d1263SSimon Glass int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1307779d1263SSimon Glass {
1308779d1263SSimon Glass struct xhci_hccr *hccr;
1309779d1263SSimon Glass struct xhci_hcor *hcor;
1310779d1263SSimon Glass struct xhci_ctrl *ctrl;
1311779d1263SSimon Glass int ret;
1312779d1263SSimon Glass
1313a5ccda47SSergey Temerkhanov *controller = NULL;
1314a5ccda47SSergey Temerkhanov
1315779d1263SSimon Glass if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1316779d1263SSimon Glass return -ENODEV;
1317779d1263SSimon Glass
1318779d1263SSimon Glass if (xhci_reset(hcor) != 0)
1319779d1263SSimon Glass return -ENODEV;
1320779d1263SSimon Glass
1321779d1263SSimon Glass ctrl = &xhcic[index];
1322779d1263SSimon Glass
1323779d1263SSimon Glass ctrl->hccr = hccr;
1324779d1263SSimon Glass ctrl->hcor = hcor;
1325779d1263SSimon Glass
1326779d1263SSimon Glass ret = xhci_lowlevel_init(ctrl);
1327779d1263SSimon Glass
1328a5ccda47SSergey Temerkhanov if (ret) {
1329a5ccda47SSergey Temerkhanov ctrl->hccr = NULL;
1330a5ccda47SSergey Temerkhanov ctrl->hcor = NULL;
1331a5ccda47SSergey Temerkhanov } else {
1332779d1263SSimon Glass *controller = &xhcic[index];
1333a5ccda47SSergey Temerkhanov }
1334779d1263SSimon Glass
1335779d1263SSimon Glass return ret;
1336779d1263SSimon Glass }
1337779d1263SSimon Glass
1338779d1263SSimon Glass /**
13395853e133SVivek Gautam * Stops the XHCI host controller
13405853e133SVivek Gautam * and cleans up all the related data structures
13415853e133SVivek Gautam *
13425853e133SVivek Gautam * @param index index to the host controller data structure
13435853e133SVivek Gautam * @return none
13445853e133SVivek Gautam */
usb_lowlevel_stop(int index)13455853e133SVivek Gautam int usb_lowlevel_stop(int index)
13465853e133SVivek Gautam {
13475853e133SVivek Gautam struct xhci_ctrl *ctrl = (xhcic + index);
13485853e133SVivek Gautam
1349a5ccda47SSergey Temerkhanov if (ctrl->hcor) {
1350779d1263SSimon Glass xhci_lowlevel_stop(ctrl);
13515853e133SVivek Gautam xhci_hcd_stop(index);
13525853e133SVivek Gautam xhci_cleanup(ctrl);
1353a5ccda47SSergey Temerkhanov }
13545853e133SVivek Gautam
13555853e133SVivek Gautam return 0;
13565853e133SVivek Gautam }
13573739bf7eSSven Schwermer #endif /* CONFIG_IS_ENABLED(DM_USB) */
1358a5762fe0SSimon Glass
13593739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
1360a5762fe0SSimon Glass
xhci_submit_control_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)1361a5762fe0SSimon Glass static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1362a5762fe0SSimon Glass unsigned long pipe, void *buffer, int length,
1363a5762fe0SSimon Glass struct devrequest *setup)
1364a5762fe0SSimon Glass {
1365a5762fe0SSimon Glass struct usb_device *uhop;
1366a5762fe0SSimon Glass struct udevice *hub;
1367a5762fe0SSimon Glass int root_portnr = 0;
1368a5762fe0SSimon Glass
1369a5762fe0SSimon Glass debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1370a5762fe0SSimon Glass dev->name, udev, udev->dev->name, udev->portnr);
1371a5762fe0SSimon Glass hub = udev->dev;
1372a5762fe0SSimon Glass if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1373a5762fe0SSimon Glass /* Figure out our port number on the root hub */
137446c1d493SBin Meng if (usb_hub_is_root_hub(hub)) {
1375a5762fe0SSimon Glass root_portnr = udev->portnr;
1376a5762fe0SSimon Glass } else {
137746c1d493SBin Meng while (!usb_hub_is_root_hub(hub->parent))
1378a5762fe0SSimon Glass hub = hub->parent;
1379bcbe3d15SSimon Glass uhop = dev_get_parent_priv(hub);
1380a5762fe0SSimon Glass root_portnr = uhop->portnr;
1381a5762fe0SSimon Glass }
1382a5762fe0SSimon Glass }
1383a5762fe0SSimon Glass /*
1384a5762fe0SSimon Glass struct usb_device *hop = udev;
1385a5762fe0SSimon Glass
1386a5762fe0SSimon Glass if (hop->parent)
1387a5762fe0SSimon Glass while (hop->parent->parent)
1388a5762fe0SSimon Glass hop = hop->parent;
1389a5762fe0SSimon Glass */
1390a5762fe0SSimon Glass return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1391a5762fe0SSimon Glass root_portnr);
1392a5762fe0SSimon Glass }
1393a5762fe0SSimon Glass
xhci_submit_bulk_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length)1394a5762fe0SSimon Glass static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1395a5762fe0SSimon Glass unsigned long pipe, void *buffer, int length)
1396a5762fe0SSimon Glass {
1397a5762fe0SSimon Glass debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1398a5762fe0SSimon Glass return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1399a5762fe0SSimon Glass }
1400a5762fe0SSimon Glass
xhci_submit_int_msg(struct udevice * dev,struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)1401a5762fe0SSimon Glass static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1402a5762fe0SSimon Glass unsigned long pipe, void *buffer, int length,
140392937b1fSMichal Suchanek int interval, bool nonblock)
1404a5762fe0SSimon Glass {
1405a5762fe0SSimon Glass debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
140692937b1fSMichal Suchanek return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
140792937b1fSMichal Suchanek nonblock);
1408a5762fe0SSimon Glass }
1409a5762fe0SSimon Glass
xhci_alloc_device(struct udevice * dev,struct usb_device * udev)1410a5762fe0SSimon Glass static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1411a5762fe0SSimon Glass {
1412a5762fe0SSimon Glass debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1413a5762fe0SSimon Glass return _xhci_alloc_device(udev);
1414a5762fe0SSimon Glass }
1415a5762fe0SSimon Glass
xhci_update_hub_device(struct udevice * dev,struct usb_device * udev)1416d228ca36SBin Meng static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1417d228ca36SBin Meng {
1418d228ca36SBin Meng struct xhci_ctrl *ctrl = dev_get_priv(dev);
1419d228ca36SBin Meng struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1420d228ca36SBin Meng struct xhci_virt_device *virt_dev;
1421d228ca36SBin Meng struct xhci_input_control_ctx *ctrl_ctx;
1422d228ca36SBin Meng struct xhci_container_ctx *out_ctx;
1423d228ca36SBin Meng struct xhci_container_ctx *in_ctx;
1424d228ca36SBin Meng struct xhci_slot_ctx *slot_ctx;
1425d228ca36SBin Meng int slot_id = udev->slot_id;
1426d228ca36SBin Meng unsigned think_time;
1427d228ca36SBin Meng
1428d228ca36SBin Meng debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1429d228ca36SBin Meng
1430d228ca36SBin Meng /* Ignore root hubs */
1431d228ca36SBin Meng if (usb_hub_is_root_hub(udev->dev))
1432d228ca36SBin Meng return 0;
1433d228ca36SBin Meng
1434d228ca36SBin Meng virt_dev = ctrl->devs[slot_id];
1435d228ca36SBin Meng BUG_ON(!virt_dev);
1436d228ca36SBin Meng
1437d228ca36SBin Meng out_ctx = virt_dev->out_ctx;
1438d228ca36SBin Meng in_ctx = virt_dev->in_ctx;
1439d228ca36SBin Meng
1440d228ca36SBin Meng ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1441d228ca36SBin Meng /* Initialize the input context control */
1442d222f63dSBin Meng ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1443d228ca36SBin Meng ctrl_ctx->drop_flags = 0;
1444d228ca36SBin Meng
1445d228ca36SBin Meng xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1446d228ca36SBin Meng
1447d228ca36SBin Meng /* slot context */
1448d228ca36SBin Meng xhci_slot_copy(ctrl, in_ctx, out_ctx);
1449d228ca36SBin Meng slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1450d228ca36SBin Meng
1451d228ca36SBin Meng /* Update hub related fields */
1452d228ca36SBin Meng slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
1453fc48a70cSBin Meng /*
1454fc48a70cSBin Meng * refer to section 6.2.2: MTT should be 0 for full speed hub,
1455fc48a70cSBin Meng * but it may be already set to 1 when setup an xHCI virtual
1456fc48a70cSBin Meng * device, so clear it anyway.
1457fc48a70cSBin Meng */
1458fc48a70cSBin Meng if (hub->tt.multi)
1459d228ca36SBin Meng slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1460fc48a70cSBin Meng else if (udev->speed == USB_SPEED_FULL)
1461fc48a70cSBin Meng slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
1462d228ca36SBin Meng slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1463d228ca36SBin Meng /*
1464d228ca36SBin Meng * Set TT think time - convert from ns to FS bit times.
1465d228ca36SBin Meng * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1466d228ca36SBin Meng *
1467d228ca36SBin Meng * 0 = 8 FS bit times, 1 = 16 FS bit times,
1468d228ca36SBin Meng * 2 = 24 FS bit times, 3 = 32 FS bit times.
1469d228ca36SBin Meng *
1470d228ca36SBin Meng * This field shall be 0 if the device is not a high-spped hub.
1471d228ca36SBin Meng */
1472d228ca36SBin Meng think_time = hub->tt.think_time;
1473d228ca36SBin Meng if (think_time != 0)
1474d228ca36SBin Meng think_time = (think_time / 666) - 1;
1475d228ca36SBin Meng if (udev->speed == USB_SPEED_HIGH)
1476d228ca36SBin Meng slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
147702d23558SBin Meng slot_ctx->dev_state = 0;
1478d228ca36SBin Meng
1479d228ca36SBin Meng return xhci_configure_endpoints(udev, false);
1480d228ca36SBin Meng }
1481d228ca36SBin Meng
xhci_get_max_xfer_size(struct udevice * dev,size_t * size)1482dbdd0140SBin Meng static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1483dbdd0140SBin Meng {
1484dbdd0140SBin Meng /*
1485dbdd0140SBin Meng * xHCD allocates one segment which includes 64 TRBs for each endpoint
1486dbdd0140SBin Meng * and the last TRB in this segment is configured as a link TRB to form
1487dbdd0140SBin Meng * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1488dbdd0140SBin Meng * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1489dbdd0140SBin Meng * Hence the maximum number of TRBs we can use in one transfer is 62.
1490dbdd0140SBin Meng */
1491dbdd0140SBin Meng *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1492dbdd0140SBin Meng
1493dbdd0140SBin Meng return 0;
1494dbdd0140SBin Meng }
1495dbdd0140SBin Meng
xhci_register(struct udevice * dev,struct xhci_hccr * hccr,struct xhci_hcor * hcor)1496a5762fe0SSimon Glass int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1497a5762fe0SSimon Glass struct xhci_hcor *hcor)
1498a5762fe0SSimon Glass {
1499a5762fe0SSimon Glass struct xhci_ctrl *ctrl = dev_get_priv(dev);
1500a5762fe0SSimon Glass struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1501a5762fe0SSimon Glass int ret;
1502a5762fe0SSimon Glass
1503a5762fe0SSimon Glass debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1504a5762fe0SSimon Glass ctrl, hccr, hcor);
1505a5762fe0SSimon Glass
1506a5762fe0SSimon Glass ctrl->dev = dev;
1507a5762fe0SSimon Glass
1508a5762fe0SSimon Glass /*
1509a5762fe0SSimon Glass * XHCI needs to issue a Address device command to setup
1510a5762fe0SSimon Glass * proper device context structures, before it can interact
1511a5762fe0SSimon Glass * with the device. So a get_descriptor will fail before any
1512a5762fe0SSimon Glass * of that is done for XHCI unlike EHCI.
1513a5762fe0SSimon Glass */
1514a5762fe0SSimon Glass priv->desc_before_addr = false;
1515a5762fe0SSimon Glass
1516a5762fe0SSimon Glass ret = xhci_reset(hcor);
1517a5762fe0SSimon Glass if (ret)
1518a5762fe0SSimon Glass goto err;
1519a5762fe0SSimon Glass
1520a5762fe0SSimon Glass ctrl->hccr = hccr;
1521a5762fe0SSimon Glass ctrl->hcor = hcor;
1522a5762fe0SSimon Glass ret = xhci_lowlevel_init(ctrl);
1523a5762fe0SSimon Glass if (ret)
1524a5762fe0SSimon Glass goto err;
1525a5762fe0SSimon Glass
1526a5762fe0SSimon Glass return 0;
1527a5762fe0SSimon Glass err:
1528a5762fe0SSimon Glass free(ctrl);
1529a5762fe0SSimon Glass debug("%s: failed, ret=%d\n", __func__, ret);
1530a5762fe0SSimon Glass return ret;
1531a5762fe0SSimon Glass }
1532a5762fe0SSimon Glass
xhci_deregister(struct udevice * dev)1533a5762fe0SSimon Glass int xhci_deregister(struct udevice *dev)
1534a5762fe0SSimon Glass {
1535a5762fe0SSimon Glass struct xhci_ctrl *ctrl = dev_get_priv(dev);
1536a5762fe0SSimon Glass
1537a5762fe0SSimon Glass xhci_lowlevel_stop(ctrl);
1538a5762fe0SSimon Glass xhci_cleanup(ctrl);
1539a5762fe0SSimon Glass
1540a5762fe0SSimon Glass return 0;
1541a5762fe0SSimon Glass }
1542a5762fe0SSimon Glass
1543a5762fe0SSimon Glass struct dm_usb_ops xhci_usb_ops = {
1544a5762fe0SSimon Glass .control = xhci_submit_control_msg,
1545a5762fe0SSimon Glass .bulk = xhci_submit_bulk_msg,
1546a5762fe0SSimon Glass .interrupt = xhci_submit_int_msg,
1547a5762fe0SSimon Glass .alloc_device = xhci_alloc_device,
1548d228ca36SBin Meng .update_hub_device = xhci_update_hub_device,
1549dbdd0140SBin Meng .get_max_xfer_size = xhci_get_max_xfer_size,
1550a5762fe0SSimon Glass };
1551a5762fe0SSimon Glass
1552a5762fe0SSimon Glass #endif
1553