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> 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> 305853e133SVivek Gautam #include <asm-generic/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 */ 525853e133SVivek Gautam {}, /* Device removable */ 535853e133SVivek Gautam {} /* 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 1115853e133SVivek Gautam static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT]; 1125853e133SVivek Gautam 113*7c1deec0SSimon Glass struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev) 114*7c1deec0SSimon Glass { 115*7c1deec0SSimon Glass return udev->controller; 116*7c1deec0SSimon Glass } 117*7c1deec0SSimon Glass 1185853e133SVivek Gautam /** 1195853e133SVivek Gautam * Waits for as per specified amount of time 1205853e133SVivek Gautam * for the "result" to match with "done" 1215853e133SVivek Gautam * 1225853e133SVivek Gautam * @param ptr pointer to the register to be read 1235853e133SVivek Gautam * @param mask mask for the value read 1245853e133SVivek Gautam * @param done value to be campared with result 1255853e133SVivek Gautam * @param usec time to wait till 1265853e133SVivek Gautam * @return 0 if handshake is success else < 0 on failure 1275853e133SVivek Gautam */ 1285853e133SVivek Gautam static int handshake(uint32_t volatile *ptr, uint32_t mask, 1295853e133SVivek Gautam uint32_t done, int usec) 1305853e133SVivek Gautam { 1315853e133SVivek Gautam uint32_t result; 1325853e133SVivek Gautam 1335853e133SVivek Gautam do { 1345853e133SVivek Gautam result = xhci_readl(ptr); 1355853e133SVivek Gautam if (result == ~(uint32_t)0) 1365853e133SVivek Gautam return -ENODEV; 1375853e133SVivek Gautam result &= mask; 1385853e133SVivek Gautam if (result == done) 1395853e133SVivek Gautam return 0; 1405853e133SVivek Gautam usec--; 1415853e133SVivek Gautam udelay(1); 1425853e133SVivek Gautam } while (usec > 0); 1435853e133SVivek Gautam 1445853e133SVivek Gautam return -ETIMEDOUT; 1455853e133SVivek Gautam } 1465853e133SVivek Gautam 1475853e133SVivek Gautam /** 1485853e133SVivek Gautam * Set the run bit and wait for the host to be running. 1495853e133SVivek Gautam * 1505853e133SVivek Gautam * @param hcor pointer to host controller operation registers 1515853e133SVivek Gautam * @return status of the Handshake 1525853e133SVivek Gautam */ 1535853e133SVivek Gautam static int xhci_start(struct xhci_hcor *hcor) 1545853e133SVivek Gautam { 1555853e133SVivek Gautam u32 temp; 1565853e133SVivek Gautam int ret; 1575853e133SVivek Gautam 1585853e133SVivek Gautam puts("Starting the controller\n"); 1595853e133SVivek Gautam temp = xhci_readl(&hcor->or_usbcmd); 1605853e133SVivek Gautam temp |= (CMD_RUN); 1615853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, temp); 1625853e133SVivek Gautam 1635853e133SVivek Gautam /* 1645853e133SVivek Gautam * Wait for the HCHalted Status bit to be 0 to indicate the host is 1655853e133SVivek Gautam * running. 1665853e133SVivek Gautam */ 1675853e133SVivek Gautam ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC); 1685853e133SVivek Gautam if (ret) 1695853e133SVivek Gautam debug("Host took too long to start, " 1705853e133SVivek Gautam "waited %u microseconds.\n", 1715853e133SVivek Gautam XHCI_MAX_HALT_USEC); 1725853e133SVivek Gautam return ret; 1735853e133SVivek Gautam } 1745853e133SVivek Gautam 1755853e133SVivek Gautam /** 1765853e133SVivek Gautam * Resets the XHCI Controller 1775853e133SVivek Gautam * 1785853e133SVivek Gautam * @param hcor pointer to host controller operation registers 1795853e133SVivek Gautam * @return -EBUSY if XHCI Controller is not halted else status of handshake 1805853e133SVivek Gautam */ 1815853e133SVivek Gautam int xhci_reset(struct xhci_hcor *hcor) 1825853e133SVivek Gautam { 1835853e133SVivek Gautam u32 cmd; 1845853e133SVivek Gautam u32 state; 1855853e133SVivek Gautam int ret; 1865853e133SVivek Gautam 1875853e133SVivek Gautam /* Halting the Host first */ 1885853e133SVivek Gautam debug("// Halt the HC\n"); 1895853e133SVivek Gautam state = xhci_readl(&hcor->or_usbsts) & STS_HALT; 1905853e133SVivek Gautam if (!state) { 1915853e133SVivek Gautam cmd = xhci_readl(&hcor->or_usbcmd); 1925853e133SVivek Gautam cmd &= ~CMD_RUN; 1935853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, cmd); 1945853e133SVivek Gautam } 1955853e133SVivek Gautam 1965853e133SVivek Gautam ret = handshake(&hcor->or_usbsts, 1975853e133SVivek Gautam STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); 1985853e133SVivek Gautam if (ret) { 1995853e133SVivek Gautam printf("Host not halted after %u microseconds.\n", 2005853e133SVivek Gautam XHCI_MAX_HALT_USEC); 2015853e133SVivek Gautam return -EBUSY; 2025853e133SVivek Gautam } 2035853e133SVivek Gautam 2045853e133SVivek Gautam debug("// Reset the HC\n"); 2055853e133SVivek Gautam cmd = xhci_readl(&hcor->or_usbcmd); 2065853e133SVivek Gautam cmd |= CMD_RESET; 2075853e133SVivek Gautam xhci_writel(&hcor->or_usbcmd, cmd); 2085853e133SVivek Gautam 2095853e133SVivek Gautam ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC); 2105853e133SVivek Gautam if (ret) 2115853e133SVivek Gautam return ret; 2125853e133SVivek Gautam 2135853e133SVivek Gautam /* 2145853e133SVivek Gautam * xHCI cannot write to any doorbells or operational registers other 2155853e133SVivek Gautam * than status until the "Controller Not Ready" flag is cleared. 2165853e133SVivek Gautam */ 2175853e133SVivek Gautam return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC); 2185853e133SVivek Gautam } 2195853e133SVivek Gautam 2205853e133SVivek Gautam /** 2215853e133SVivek Gautam * Used for passing endpoint bitmasks between the core and HCDs. 2225853e133SVivek Gautam * Find the index for an endpoint given its descriptor. 2235853e133SVivek Gautam * Use the return value to right shift 1 for the bitmask. 2245853e133SVivek Gautam * 2255853e133SVivek Gautam * Index = (epnum * 2) + direction - 1, 2265853e133SVivek Gautam * where direction = 0 for OUT, 1 for IN. 2275853e133SVivek Gautam * For control endpoints, the IN index is used (OUT index is unused), so 2285853e133SVivek Gautam * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) 2295853e133SVivek Gautam * 2305853e133SVivek Gautam * @param desc USB enpdoint Descriptor 2315853e133SVivek Gautam * @return index of the Endpoint 2325853e133SVivek Gautam */ 2335853e133SVivek Gautam static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc) 2345853e133SVivek Gautam { 2355853e133SVivek Gautam unsigned int index; 2365853e133SVivek Gautam 2375853e133SVivek Gautam if (usb_endpoint_xfer_control(desc)) 2385853e133SVivek Gautam index = (unsigned int)(usb_endpoint_num(desc) * 2); 2395853e133SVivek Gautam else 2405853e133SVivek Gautam index = (unsigned int)((usb_endpoint_num(desc) * 2) - 2415853e133SVivek Gautam (usb_endpoint_dir_in(desc) ? 0 : 1)); 2425853e133SVivek Gautam 2435853e133SVivek Gautam return index; 2445853e133SVivek Gautam } 2455853e133SVivek Gautam 2465853e133SVivek Gautam /** 2475853e133SVivek Gautam * Issue a configure endpoint command or evaluate context command 2485853e133SVivek Gautam * and wait for it to finish. 2495853e133SVivek Gautam * 2505853e133SVivek Gautam * @param udev pointer to the Device Data Structure 2515853e133SVivek Gautam * @param ctx_change flag to indicate the Context has changed or NOT 2525853e133SVivek Gautam * @return 0 on success, -1 on failure 2535853e133SVivek Gautam */ 2545853e133SVivek Gautam static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change) 2555853e133SVivek Gautam { 2565853e133SVivek Gautam struct xhci_container_ctx *in_ctx; 2575853e133SVivek Gautam struct xhci_virt_device *virt_dev; 258*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 2595853e133SVivek Gautam union xhci_trb *event; 2605853e133SVivek Gautam 2615853e133SVivek Gautam virt_dev = ctrl->devs[udev->slot_id]; 2625853e133SVivek Gautam in_ctx = virt_dev->in_ctx; 2635853e133SVivek Gautam 264421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size); 2655853e133SVivek Gautam xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0, 2665853e133SVivek Gautam ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP); 2675853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION); 2685853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) 2695853e133SVivek Gautam != udev->slot_id); 2705853e133SVivek Gautam 2715853e133SVivek Gautam switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { 2725853e133SVivek Gautam case COMP_SUCCESS: 2735853e133SVivek Gautam debug("Successful %s command\n", 2745853e133SVivek Gautam ctx_change ? "Evaluate Context" : "Configure Endpoint"); 2755853e133SVivek Gautam break; 2765853e133SVivek Gautam default: 2775853e133SVivek Gautam printf("ERROR: %s command returned completion code %d.\n", 2785853e133SVivek Gautam ctx_change ? "Evaluate Context" : "Configure Endpoint", 2795853e133SVivek Gautam GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); 2805853e133SVivek Gautam return -EINVAL; 2815853e133SVivek Gautam } 2825853e133SVivek Gautam 2835853e133SVivek Gautam xhci_acknowledge_event(ctrl); 2845853e133SVivek Gautam 2855853e133SVivek Gautam return 0; 2865853e133SVivek Gautam } 2875853e133SVivek Gautam 2885853e133SVivek Gautam /** 2895853e133SVivek Gautam * Configure the endpoint, programming the device contexts. 2905853e133SVivek Gautam * 2915853e133SVivek Gautam * @param udev pointer to the USB device structure 2925853e133SVivek Gautam * @return returns the status of the xhci_configure_endpoints 2935853e133SVivek Gautam */ 2945853e133SVivek Gautam static int xhci_set_configuration(struct usb_device *udev) 2955853e133SVivek Gautam { 2965853e133SVivek Gautam struct xhci_container_ctx *in_ctx; 2975853e133SVivek Gautam struct xhci_container_ctx *out_ctx; 2985853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx; 2995853e133SVivek Gautam struct xhci_slot_ctx *slot_ctx; 3005853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM]; 3015853e133SVivek Gautam int cur_ep; 3025853e133SVivek Gautam int max_ep_flag = 0; 3035853e133SVivek Gautam int ep_index; 3045853e133SVivek Gautam unsigned int dir; 3055853e133SVivek Gautam unsigned int ep_type; 306*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 3075853e133SVivek Gautam int num_of_ep; 3085853e133SVivek Gautam int ep_flag = 0; 3095853e133SVivek Gautam u64 trb_64 = 0; 3105853e133SVivek Gautam int slot_id = udev->slot_id; 3115853e133SVivek Gautam struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; 3125853e133SVivek Gautam struct usb_interface *ifdesc; 3135853e133SVivek Gautam 3145853e133SVivek Gautam out_ctx = virt_dev->out_ctx; 3155853e133SVivek Gautam in_ctx = virt_dev->in_ctx; 3165853e133SVivek Gautam 3175853e133SVivek Gautam num_of_ep = udev->config.if_desc[0].no_of_ep; 3185853e133SVivek Gautam ifdesc = &udev->config.if_desc[0]; 3195853e133SVivek Gautam 3205853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(in_ctx); 3215853e133SVivek Gautam /* Zero the input context control */ 3225853e133SVivek Gautam ctrl_ctx->add_flags = 0; 3235853e133SVivek Gautam ctrl_ctx->drop_flags = 0; 3245853e133SVivek Gautam 3255853e133SVivek Gautam /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */ 3265853e133SVivek Gautam for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { 3275853e133SVivek Gautam ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]); 3285853e133SVivek Gautam ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1)); 3295853e133SVivek Gautam if (max_ep_flag < ep_flag) 3305853e133SVivek Gautam max_ep_flag = ep_flag; 3315853e133SVivek Gautam } 3325853e133SVivek Gautam 333421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); 3345853e133SVivek Gautam 3355853e133SVivek Gautam /* slot context */ 3365853e133SVivek Gautam xhci_slot_copy(ctrl, in_ctx, out_ctx); 3375853e133SVivek Gautam slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); 3385853e133SVivek Gautam slot_ctx->dev_info &= ~(LAST_CTX_MASK); 3395853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0); 3405853e133SVivek Gautam 3415853e133SVivek Gautam xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0); 3425853e133SVivek Gautam 3435853e133SVivek Gautam /* filling up ep contexts */ 3445853e133SVivek Gautam for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { 3455853e133SVivek Gautam struct usb_endpoint_descriptor *endpt_desc = NULL; 3465853e133SVivek Gautam 3475853e133SVivek Gautam endpt_desc = &ifdesc->ep_desc[cur_ep]; 3485853e133SVivek Gautam trb_64 = 0; 3495853e133SVivek Gautam 3505853e133SVivek Gautam ep_index = xhci_get_ep_index(endpt_desc); 3515853e133SVivek Gautam ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); 3525853e133SVivek Gautam 3535853e133SVivek Gautam /* Allocate the ep rings */ 3545853e133SVivek Gautam virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true); 3555853e133SVivek Gautam if (!virt_dev->eps[ep_index].ring) 3565853e133SVivek Gautam return -ENOMEM; 3575853e133SVivek Gautam 3585853e133SVivek Gautam /*NOTE: ep_desc[0] actually represents EP1 and so on */ 3595853e133SVivek Gautam dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7); 3605853e133SVivek Gautam ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2)); 3615853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 = 3625853e133SVivek Gautam cpu_to_le32(ep_type << EP_TYPE_SHIFT); 3635853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 |= 3645853e133SVivek Gautam cpu_to_le32(MAX_PACKET 3655853e133SVivek Gautam (get_unaligned(&endpt_desc->wMaxPacketSize))); 3665853e133SVivek Gautam 3675853e133SVivek Gautam ep_ctx[ep_index]->ep_info2 |= 3685853e133SVivek Gautam cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) | 3695853e133SVivek Gautam ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT)); 3705853e133SVivek Gautam 3715853e133SVivek Gautam trb_64 = (uintptr_t) 3725853e133SVivek Gautam virt_dev->eps[ep_index].ring->enqueue; 3735853e133SVivek Gautam ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 | 3745853e133SVivek Gautam virt_dev->eps[ep_index].ring->cycle_state); 3755853e133SVivek Gautam } 3765853e133SVivek Gautam 3775853e133SVivek Gautam return xhci_configure_endpoints(udev, false); 3785853e133SVivek Gautam } 3795853e133SVivek Gautam 3805853e133SVivek Gautam /** 3815853e133SVivek Gautam * Issue an Address Device command (which will issue a SetAddress request to 3825853e133SVivek Gautam * the device). 3835853e133SVivek Gautam * 3845853e133SVivek Gautam * @param udev pointer to the Device Data Structure 3855853e133SVivek Gautam * @return 0 if successful else error code on failure 3865853e133SVivek Gautam */ 3875853e133SVivek Gautam static int xhci_address_device(struct usb_device *udev) 3885853e133SVivek Gautam { 3895853e133SVivek Gautam int ret = 0; 390*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 3915853e133SVivek Gautam struct xhci_slot_ctx *slot_ctx; 3925853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx; 3935853e133SVivek Gautam struct xhci_virt_device *virt_dev; 3945853e133SVivek Gautam int slot_id = udev->slot_id; 3955853e133SVivek Gautam union xhci_trb *event; 3965853e133SVivek Gautam 3975853e133SVivek Gautam virt_dev = ctrl->devs[slot_id]; 3985853e133SVivek Gautam 3995853e133SVivek Gautam /* 4005853e133SVivek Gautam * This is the first Set Address since device plug-in 4015853e133SVivek Gautam * so setting up the slot context. 4025853e133SVivek Gautam */ 4035853e133SVivek Gautam debug("Setting up addressable devices\n"); 4045853e133SVivek Gautam xhci_setup_addressable_virt_dev(udev); 4055853e133SVivek Gautam 4065853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 4075853e133SVivek Gautam ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); 4085853e133SVivek Gautam ctrl_ctx->drop_flags = 0; 4095853e133SVivek Gautam 4105853e133SVivek Gautam xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV); 4115853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION); 4125853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id); 4135853e133SVivek Gautam 4145853e133SVivek Gautam switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { 4155853e133SVivek Gautam case COMP_CTX_STATE: 4165853e133SVivek Gautam case COMP_EBADSLT: 4175853e133SVivek Gautam printf("Setup ERROR: address device command for slot %d.\n", 4185853e133SVivek Gautam slot_id); 4195853e133SVivek Gautam ret = -EINVAL; 4205853e133SVivek Gautam break; 4215853e133SVivek Gautam case COMP_TX_ERR: 4225853e133SVivek Gautam puts("Device not responding to set address.\n"); 4235853e133SVivek Gautam ret = -EPROTO; 4245853e133SVivek Gautam break; 4255853e133SVivek Gautam case COMP_DEV_ERR: 4265853e133SVivek Gautam puts("ERROR: Incompatible device" 4275853e133SVivek Gautam "for address device command.\n"); 4285853e133SVivek Gautam ret = -ENODEV; 4295853e133SVivek Gautam break; 4305853e133SVivek Gautam case COMP_SUCCESS: 4315853e133SVivek Gautam debug("Successful Address Device command\n"); 4325853e133SVivek Gautam udev->status = 0; 4335853e133SVivek Gautam break; 4345853e133SVivek Gautam default: 4355853e133SVivek Gautam printf("ERROR: unexpected command completion code 0x%x.\n", 4365853e133SVivek Gautam GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); 4375853e133SVivek Gautam ret = -EINVAL; 4385853e133SVivek Gautam break; 4395853e133SVivek Gautam } 4405853e133SVivek Gautam 4415853e133SVivek Gautam xhci_acknowledge_event(ctrl); 4425853e133SVivek Gautam 4435853e133SVivek Gautam if (ret < 0) 4445853e133SVivek Gautam /* 4455853e133SVivek Gautam * TODO: Unsuccessful Address Device command shall leave the 4465853e133SVivek Gautam * slot in default state. So, issue Disable Slot command now. 4475853e133SVivek Gautam */ 4485853e133SVivek Gautam return ret; 4495853e133SVivek Gautam 450421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, 4515853e133SVivek Gautam virt_dev->out_ctx->size); 4525853e133SVivek Gautam slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx); 4535853e133SVivek Gautam 4545853e133SVivek Gautam debug("xHC internal address is: %d\n", 4555853e133SVivek Gautam le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 4565853e133SVivek Gautam 4575853e133SVivek Gautam return 0; 4585853e133SVivek Gautam } 4595853e133SVivek Gautam 4605853e133SVivek Gautam /** 4615853e133SVivek Gautam * Issue Enable slot command to the controller to allocate 4625853e133SVivek Gautam * device slot and assign the slot id. It fails if the xHC 4635853e133SVivek Gautam * ran out of device slots, the Enable Slot command timed out, 4645853e133SVivek Gautam * or allocating memory failed. 4655853e133SVivek Gautam * 4665853e133SVivek Gautam * @param udev pointer to the Device Data Structure 4675853e133SVivek Gautam * @return Returns 0 on succes else return error code on failure 4685853e133SVivek Gautam */ 4695853e133SVivek Gautam int usb_alloc_device(struct usb_device *udev) 4705853e133SVivek Gautam { 471*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 4725853e133SVivek Gautam union xhci_trb *event; 4735853e133SVivek Gautam int ret; 4745853e133SVivek Gautam 4755853e133SVivek Gautam /* 4765853e133SVivek Gautam * Root hub will be first device to be initailized. 4775853e133SVivek Gautam * If this device is root-hub, don't do any xHC related 4785853e133SVivek Gautam * stuff. 4795853e133SVivek Gautam */ 4805853e133SVivek Gautam if (ctrl->rootdev == 0) { 4815853e133SVivek Gautam udev->speed = USB_SPEED_SUPER; 4825853e133SVivek Gautam return 0; 4835853e133SVivek Gautam } 4845853e133SVivek Gautam 4855853e133SVivek Gautam xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT); 4865853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION); 4875853e133SVivek Gautam BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)) 4885853e133SVivek Gautam != COMP_SUCCESS); 4895853e133SVivek Gautam 4905853e133SVivek Gautam udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)); 4915853e133SVivek Gautam 4925853e133SVivek Gautam xhci_acknowledge_event(ctrl); 4935853e133SVivek Gautam 4945853e133SVivek Gautam ret = xhci_alloc_virt_device(udev); 4955853e133SVivek Gautam if (ret < 0) { 4965853e133SVivek Gautam /* 4975853e133SVivek Gautam * TODO: Unsuccessful Address Device command shall leave 4985853e133SVivek Gautam * the slot in default. So, issue Disable Slot command now. 4995853e133SVivek Gautam */ 5005853e133SVivek Gautam puts("Could not allocate xHCI USB device data structures\n"); 5015853e133SVivek Gautam return ret; 5025853e133SVivek Gautam } 5035853e133SVivek Gautam 5045853e133SVivek Gautam return 0; 5055853e133SVivek Gautam } 5065853e133SVivek Gautam 5075853e133SVivek Gautam /* 5085853e133SVivek Gautam * Full speed devices may have a max packet size greater than 8 bytes, but the 5095853e133SVivek Gautam * USB core doesn't know that until it reads the first 8 bytes of the 5105853e133SVivek Gautam * descriptor. If the usb_device's max packet size changes after that point, 5115853e133SVivek Gautam * we need to issue an evaluate context command and wait on it. 5125853e133SVivek Gautam * 5135853e133SVivek Gautam * @param udev pointer to the Device Data Structure 5145853e133SVivek Gautam * @return returns the status of the xhci_configure_endpoints 5155853e133SVivek Gautam */ 5165853e133SVivek Gautam int xhci_check_maxpacket(struct usb_device *udev) 5175853e133SVivek Gautam { 518*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 5195853e133SVivek Gautam unsigned int slot_id = udev->slot_id; 5205853e133SVivek Gautam int ep_index = 0; /* control endpoint */ 5215853e133SVivek Gautam struct xhci_container_ctx *in_ctx; 5225853e133SVivek Gautam struct xhci_container_ctx *out_ctx; 5235853e133SVivek Gautam struct xhci_input_control_ctx *ctrl_ctx; 5245853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx; 5255853e133SVivek Gautam int max_packet_size; 5265853e133SVivek Gautam int hw_max_packet_size; 5275853e133SVivek Gautam int ret = 0; 5285853e133SVivek Gautam struct usb_interface *ifdesc; 5295853e133SVivek Gautam 5305853e133SVivek Gautam ifdesc = &udev->config.if_desc[0]; 5315853e133SVivek Gautam 5325853e133SVivek Gautam out_ctx = ctrl->devs[slot_id]->out_ctx; 533421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); 5345853e133SVivek Gautam 5355853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); 5365853e133SVivek Gautam hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); 5375853e133SVivek Gautam max_packet_size = usb_endpoint_maxp(&ifdesc->ep_desc[0]); 5385853e133SVivek Gautam if (hw_max_packet_size != max_packet_size) { 5395853e133SVivek Gautam debug("Max Packet Size for ep 0 changed.\n"); 5405853e133SVivek Gautam debug("Max packet size in usb_device = %d\n", max_packet_size); 5415853e133SVivek Gautam debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size); 5425853e133SVivek Gautam debug("Issuing evaluate context command.\n"); 5435853e133SVivek Gautam 5445853e133SVivek Gautam /* Set up the modified control endpoint 0 */ 5455853e133SVivek Gautam xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx, 5465853e133SVivek Gautam ctrl->devs[slot_id]->out_ctx, ep_index); 5475853e133SVivek Gautam in_ctx = ctrl->devs[slot_id]->in_ctx; 5485853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); 5495853e133SVivek Gautam ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); 5505853e133SVivek Gautam ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); 5515853e133SVivek Gautam 5525853e133SVivek Gautam /* 5535853e133SVivek Gautam * Set up the input context flags for the command 5545853e133SVivek Gautam * FIXME: This won't work if a non-default control endpoint 5555853e133SVivek Gautam * changes max packet sizes. 5565853e133SVivek Gautam */ 5575853e133SVivek Gautam ctrl_ctx = xhci_get_input_control_ctx(in_ctx); 5585853e133SVivek Gautam ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); 5595853e133SVivek Gautam ctrl_ctx->drop_flags = 0; 5605853e133SVivek Gautam 5615853e133SVivek Gautam ret = xhci_configure_endpoints(udev, true); 5625853e133SVivek Gautam } 5635853e133SVivek Gautam return ret; 5645853e133SVivek Gautam } 5655853e133SVivek Gautam 5665853e133SVivek Gautam /** 5675853e133SVivek Gautam * Clears the Change bits of the Port Status Register 5685853e133SVivek Gautam * 5695853e133SVivek Gautam * @param wValue request value 5705853e133SVivek Gautam * @param wIndex request index 5715853e133SVivek Gautam * @param addr address of posrt status register 5725853e133SVivek Gautam * @param port_status state of port status register 5735853e133SVivek Gautam * @return none 5745853e133SVivek Gautam */ 5755853e133SVivek Gautam static void xhci_clear_port_change_bit(u16 wValue, 5765853e133SVivek Gautam u16 wIndex, volatile uint32_t *addr, u32 port_status) 5775853e133SVivek Gautam { 5785853e133SVivek Gautam char *port_change_bit; 5795853e133SVivek Gautam u32 status; 5805853e133SVivek Gautam 5815853e133SVivek Gautam switch (wValue) { 5825853e133SVivek Gautam case USB_PORT_FEAT_C_RESET: 5835853e133SVivek Gautam status = PORT_RC; 5845853e133SVivek Gautam port_change_bit = "reset"; 5855853e133SVivek Gautam break; 5865853e133SVivek Gautam case USB_PORT_FEAT_C_CONNECTION: 5875853e133SVivek Gautam status = PORT_CSC; 5885853e133SVivek Gautam port_change_bit = "connect"; 5895853e133SVivek Gautam break; 5905853e133SVivek Gautam case USB_PORT_FEAT_C_OVER_CURRENT: 5915853e133SVivek Gautam status = PORT_OCC; 5925853e133SVivek Gautam port_change_bit = "over-current"; 5935853e133SVivek Gautam break; 5945853e133SVivek Gautam case USB_PORT_FEAT_C_ENABLE: 5955853e133SVivek Gautam status = PORT_PEC; 5965853e133SVivek Gautam port_change_bit = "enable/disable"; 5975853e133SVivek Gautam break; 5985853e133SVivek Gautam case USB_PORT_FEAT_C_SUSPEND: 5995853e133SVivek Gautam status = PORT_PLC; 6005853e133SVivek Gautam port_change_bit = "suspend/resume"; 6015853e133SVivek Gautam break; 6025853e133SVivek Gautam default: 6035853e133SVivek Gautam /* Should never happen */ 6045853e133SVivek Gautam return; 6055853e133SVivek Gautam } 6065853e133SVivek Gautam 6075853e133SVivek Gautam /* Change bits are all write 1 to clear */ 6085853e133SVivek Gautam xhci_writel(addr, port_status | status); 6095853e133SVivek Gautam 6105853e133SVivek Gautam port_status = xhci_readl(addr); 6115853e133SVivek Gautam debug("clear port %s change, actual port %d status = 0x%x\n", 6125853e133SVivek Gautam port_change_bit, wIndex, port_status); 6135853e133SVivek Gautam } 6145853e133SVivek Gautam 6155853e133SVivek Gautam /** 6165853e133SVivek Gautam * Save Read Only (RO) bits and save read/write bits where 6175853e133SVivek Gautam * writing a 0 clears the bit and writing a 1 sets the bit (RWS). 6185853e133SVivek Gautam * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. 6195853e133SVivek Gautam * 6205853e133SVivek Gautam * @param state state of the Port Status and Control Regsiter 6215853e133SVivek Gautam * @return a value that would result in the port being in the 6225853e133SVivek Gautam * same state, if the value was written to the port 6235853e133SVivek Gautam * status control register. 6245853e133SVivek Gautam */ 6255853e133SVivek Gautam static u32 xhci_port_state_to_neutral(u32 state) 6265853e133SVivek Gautam { 6275853e133SVivek Gautam /* Save read-only status and port state */ 6285853e133SVivek Gautam return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS); 6295853e133SVivek Gautam } 6305853e133SVivek Gautam 6315853e133SVivek Gautam /** 6325853e133SVivek Gautam * Submits the Requests to the XHCI Host Controller 6335853e133SVivek Gautam * 6345853e133SVivek Gautam * @param udev pointer to the USB device structure 6355853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 6365853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 6375853e133SVivek Gautam * @return returns 0 if successful else -1 on failure 6385853e133SVivek Gautam */ 6395853e133SVivek Gautam static int xhci_submit_root(struct usb_device *udev, unsigned long pipe, 6405853e133SVivek Gautam void *buffer, struct devrequest *req) 6415853e133SVivek Gautam { 6425853e133SVivek Gautam uint8_t tmpbuf[4]; 6435853e133SVivek Gautam u16 typeReq; 6445853e133SVivek Gautam void *srcptr = NULL; 6455853e133SVivek Gautam int len, srclen; 6465853e133SVivek Gautam uint32_t reg; 6475853e133SVivek Gautam volatile uint32_t *status_reg; 648*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 6495853e133SVivek Gautam struct xhci_hcor *hcor = ctrl->hcor; 6505853e133SVivek Gautam 65125d1936aSJeroen Hofstee if ((req->requesttype & USB_RT_PORT) && 65225d1936aSJeroen Hofstee le16_to_cpu(req->index) > CONFIG_SYS_USB_XHCI_MAX_ROOT_PORTS) { 6535853e133SVivek Gautam printf("The request port(%d) is not configured\n", 6545853e133SVivek Gautam le16_to_cpu(req->index) - 1); 6555853e133SVivek Gautam return -EINVAL; 6565853e133SVivek Gautam } 6575853e133SVivek Gautam 6585853e133SVivek Gautam status_reg = (volatile uint32_t *) 6595853e133SVivek Gautam (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc); 6605853e133SVivek Gautam srclen = 0; 6615853e133SVivek Gautam 6625853e133SVivek Gautam typeReq = req->request | req->requesttype << 8; 6635853e133SVivek Gautam 6645853e133SVivek Gautam switch (typeReq) { 6655853e133SVivek Gautam case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 6665853e133SVivek Gautam switch (le16_to_cpu(req->value) >> 8) { 6675853e133SVivek Gautam case USB_DT_DEVICE: 6685853e133SVivek Gautam debug("USB_DT_DEVICE request\n"); 6695853e133SVivek Gautam srcptr = &descriptor.device; 6705853e133SVivek Gautam srclen = 0x12; 6715853e133SVivek Gautam break; 6725853e133SVivek Gautam case USB_DT_CONFIG: 6735853e133SVivek Gautam debug("USB_DT_CONFIG config\n"); 6745853e133SVivek Gautam srcptr = &descriptor.config; 6755853e133SVivek Gautam srclen = 0x19; 6765853e133SVivek Gautam break; 6775853e133SVivek Gautam case USB_DT_STRING: 6785853e133SVivek Gautam debug("USB_DT_STRING config\n"); 6795853e133SVivek Gautam switch (le16_to_cpu(req->value) & 0xff) { 6805853e133SVivek Gautam case 0: /* Language */ 6815853e133SVivek Gautam srcptr = "\4\3\11\4"; 6825853e133SVivek Gautam srclen = 4; 6835853e133SVivek Gautam break; 6845853e133SVivek Gautam case 1: /* Vendor String */ 6855853e133SVivek Gautam srcptr = "\16\3u\0-\0b\0o\0o\0t\0"; 6865853e133SVivek Gautam srclen = 14; 6875853e133SVivek Gautam break; 6885853e133SVivek Gautam case 2: /* Product Name */ 6895853e133SVivek Gautam srcptr = "\52\3X\0H\0C\0I\0 " 6905853e133SVivek Gautam "\0H\0o\0s\0t\0 " 6915853e133SVivek Gautam "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; 6925853e133SVivek Gautam srclen = 42; 6935853e133SVivek Gautam break; 6945853e133SVivek Gautam default: 6955853e133SVivek Gautam printf("unknown value DT_STRING %x\n", 6965853e133SVivek Gautam le16_to_cpu(req->value)); 6975853e133SVivek Gautam goto unknown; 6985853e133SVivek Gautam } 6995853e133SVivek Gautam break; 7005853e133SVivek Gautam default: 7015853e133SVivek Gautam printf("unknown value %x\n", le16_to_cpu(req->value)); 7025853e133SVivek Gautam goto unknown; 7035853e133SVivek Gautam } 7045853e133SVivek Gautam break; 7055853e133SVivek Gautam case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): 7065853e133SVivek Gautam switch (le16_to_cpu(req->value) >> 8) { 7075853e133SVivek Gautam case USB_DT_HUB: 7085853e133SVivek Gautam debug("USB_DT_HUB config\n"); 7095853e133SVivek Gautam srcptr = &descriptor.hub; 7105853e133SVivek Gautam srclen = 0x8; 7115853e133SVivek Gautam break; 7125853e133SVivek Gautam default: 7135853e133SVivek Gautam printf("unknown value %x\n", le16_to_cpu(req->value)); 7145853e133SVivek Gautam goto unknown; 7155853e133SVivek Gautam } 7165853e133SVivek Gautam break; 7175853e133SVivek Gautam case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): 7185853e133SVivek Gautam debug("USB_REQ_SET_ADDRESS\n"); 7195853e133SVivek Gautam ctrl->rootdev = le16_to_cpu(req->value); 7205853e133SVivek Gautam break; 7215853e133SVivek Gautam case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 7225853e133SVivek Gautam /* Do nothing */ 7235853e133SVivek Gautam break; 7245853e133SVivek Gautam case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): 7255853e133SVivek Gautam tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ 7265853e133SVivek Gautam tmpbuf[1] = 0; 7275853e133SVivek Gautam srcptr = tmpbuf; 7285853e133SVivek Gautam srclen = 2; 7295853e133SVivek Gautam break; 7305853e133SVivek Gautam case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): 7315853e133SVivek Gautam memset(tmpbuf, 0, 4); 7325853e133SVivek Gautam reg = xhci_readl(status_reg); 7335853e133SVivek Gautam if (reg & PORT_CONNECT) { 7345853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_CONNECTION; 7355853e133SVivek Gautam switch (reg & DEV_SPEED_MASK) { 7365853e133SVivek Gautam case XDEV_FS: 7375853e133SVivek Gautam debug("SPEED = FULLSPEED\n"); 7385853e133SVivek Gautam break; 7395853e133SVivek Gautam case XDEV_LS: 7405853e133SVivek Gautam debug("SPEED = LOWSPEED\n"); 7415853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; 7425853e133SVivek Gautam break; 7435853e133SVivek Gautam case XDEV_HS: 7445853e133SVivek Gautam debug("SPEED = HIGHSPEED\n"); 7455853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; 7465853e133SVivek Gautam break; 7475853e133SVivek Gautam case XDEV_SS: 7485853e133SVivek Gautam debug("SPEED = SUPERSPEED\n"); 7495853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8; 7505853e133SVivek Gautam break; 7515853e133SVivek Gautam } 7525853e133SVivek Gautam } 7535853e133SVivek Gautam if (reg & PORT_PE) 7545853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_ENABLE; 7555853e133SVivek Gautam if ((reg & PORT_PLS_MASK) == XDEV_U3) 7565853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_SUSPEND; 7575853e133SVivek Gautam if (reg & PORT_OC) 7585853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; 7595853e133SVivek Gautam if (reg & PORT_RESET) 7605853e133SVivek Gautam tmpbuf[0] |= USB_PORT_STAT_RESET; 7615853e133SVivek Gautam if (reg & PORT_POWER) 7625853e133SVivek Gautam /* 7635853e133SVivek Gautam * XXX: This Port power bit (for USB 3.0 hub) 7645853e133SVivek Gautam * we are faking in USB 2.0 hub port status; 7655853e133SVivek Gautam * since there's a change in bit positions in 7665853e133SVivek Gautam * two: 7675853e133SVivek Gautam * USB 2.0 port status PP is at position[8] 7685853e133SVivek Gautam * USB 3.0 port status PP is at position[9] 7695853e133SVivek Gautam * So, we are still keeping it at position [8] 7705853e133SVivek Gautam */ 7715853e133SVivek Gautam tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; 7725853e133SVivek Gautam if (reg & PORT_CSC) 7735853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; 7745853e133SVivek Gautam if (reg & PORT_PEC) 7755853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; 7765853e133SVivek Gautam if (reg & PORT_OCC) 7775853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; 7785853e133SVivek Gautam if (reg & PORT_RC) 7795853e133SVivek Gautam tmpbuf[2] |= USB_PORT_STAT_C_RESET; 7805853e133SVivek Gautam 7815853e133SVivek Gautam srcptr = tmpbuf; 7825853e133SVivek Gautam srclen = 4; 7835853e133SVivek Gautam break; 7845853e133SVivek Gautam case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): 7855853e133SVivek Gautam reg = xhci_readl(status_reg); 7865853e133SVivek Gautam reg = xhci_port_state_to_neutral(reg); 7875853e133SVivek Gautam switch (le16_to_cpu(req->value)) { 7885853e133SVivek Gautam case USB_PORT_FEAT_ENABLE: 7895853e133SVivek Gautam reg |= PORT_PE; 7905853e133SVivek Gautam xhci_writel(status_reg, reg); 7915853e133SVivek Gautam break; 7925853e133SVivek Gautam case USB_PORT_FEAT_POWER: 7935853e133SVivek Gautam reg |= PORT_POWER; 7945853e133SVivek Gautam xhci_writel(status_reg, reg); 7955853e133SVivek Gautam break; 7965853e133SVivek Gautam case USB_PORT_FEAT_RESET: 7975853e133SVivek Gautam reg |= PORT_RESET; 7985853e133SVivek Gautam xhci_writel(status_reg, reg); 7995853e133SVivek Gautam break; 8005853e133SVivek Gautam default: 8015853e133SVivek Gautam printf("unknown feature %x\n", le16_to_cpu(req->value)); 8025853e133SVivek Gautam goto unknown; 8035853e133SVivek Gautam } 8045853e133SVivek Gautam break; 8055853e133SVivek Gautam case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): 8065853e133SVivek Gautam reg = xhci_readl(status_reg); 8075853e133SVivek Gautam reg = xhci_port_state_to_neutral(reg); 8085853e133SVivek Gautam switch (le16_to_cpu(req->value)) { 8095853e133SVivek Gautam case USB_PORT_FEAT_ENABLE: 8105853e133SVivek Gautam reg &= ~PORT_PE; 8115853e133SVivek Gautam break; 8125853e133SVivek Gautam case USB_PORT_FEAT_POWER: 8135853e133SVivek Gautam reg &= ~PORT_POWER; 8145853e133SVivek Gautam break; 8155853e133SVivek Gautam case USB_PORT_FEAT_C_RESET: 8165853e133SVivek Gautam case USB_PORT_FEAT_C_CONNECTION: 8175853e133SVivek Gautam case USB_PORT_FEAT_C_OVER_CURRENT: 8185853e133SVivek Gautam case USB_PORT_FEAT_C_ENABLE: 8195853e133SVivek Gautam xhci_clear_port_change_bit((le16_to_cpu(req->value)), 8205853e133SVivek Gautam le16_to_cpu(req->index), 8215853e133SVivek Gautam status_reg, reg); 8225853e133SVivek Gautam break; 8235853e133SVivek Gautam default: 8245853e133SVivek Gautam printf("unknown feature %x\n", le16_to_cpu(req->value)); 8255853e133SVivek Gautam goto unknown; 8265853e133SVivek Gautam } 8275853e133SVivek Gautam xhci_writel(status_reg, reg); 8285853e133SVivek Gautam break; 8295853e133SVivek Gautam default: 8305853e133SVivek Gautam puts("Unknown request\n"); 8315853e133SVivek Gautam goto unknown; 8325853e133SVivek Gautam } 8335853e133SVivek Gautam 8345853e133SVivek Gautam debug("scrlen = %d\n req->length = %d\n", 8355853e133SVivek Gautam srclen, le16_to_cpu(req->length)); 8365853e133SVivek Gautam 837b4141195SMasahiro Yamada len = min(srclen, (int)le16_to_cpu(req->length)); 8385853e133SVivek Gautam 8395853e133SVivek Gautam if (srcptr != NULL && len > 0) 8405853e133SVivek Gautam memcpy(buffer, srcptr, len); 8415853e133SVivek Gautam else 8425853e133SVivek Gautam debug("Len is 0\n"); 8435853e133SVivek Gautam 8445853e133SVivek Gautam udev->act_len = len; 8455853e133SVivek Gautam udev->status = 0; 8465853e133SVivek Gautam 8475853e133SVivek Gautam return 0; 8485853e133SVivek Gautam 8495853e133SVivek Gautam unknown: 8505853e133SVivek Gautam udev->act_len = 0; 8515853e133SVivek Gautam udev->status = USB_ST_STALLED; 8525853e133SVivek Gautam 8535853e133SVivek Gautam return -ENODEV; 8545853e133SVivek Gautam } 8555853e133SVivek Gautam 8565853e133SVivek Gautam /** 8575853e133SVivek Gautam * Submits the INT request to XHCI Host cotroller 8585853e133SVivek Gautam * 8595853e133SVivek Gautam * @param udev pointer to the USB device 8605853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 8615853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 8625853e133SVivek Gautam * @param length length of the buffer 8635853e133SVivek Gautam * @param interval interval of the interrupt 8645853e133SVivek Gautam * @return 0 8655853e133SVivek Gautam */ 8665853e133SVivek Gautam int 8675853e133SVivek Gautam submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, 8685853e133SVivek Gautam int length, int interval) 8695853e133SVivek Gautam { 8705853e133SVivek Gautam /* 8715853e133SVivek Gautam * TODO: Not addressing any interrupt type transfer requests 8725853e133SVivek Gautam * Add support for it later. 8735853e133SVivek Gautam */ 8745853e133SVivek Gautam return -EINVAL; 8755853e133SVivek Gautam } 8765853e133SVivek Gautam 8775853e133SVivek Gautam /** 8785853e133SVivek Gautam * submit the BULK type of request to the USB Device 8795853e133SVivek Gautam * 8805853e133SVivek Gautam * @param udev pointer to the USB device 8815853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 8825853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 8835853e133SVivek Gautam * @param length length of the buffer 8845853e133SVivek Gautam * @return returns 0 if successful else -1 on failure 8855853e133SVivek Gautam */ 8865853e133SVivek Gautam int 8875853e133SVivek Gautam submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, 8885853e133SVivek Gautam int length) 8895853e133SVivek Gautam { 8905853e133SVivek Gautam if (usb_pipetype(pipe) != PIPE_BULK) { 8915853e133SVivek Gautam printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); 8925853e133SVivek Gautam return -EINVAL; 8935853e133SVivek Gautam } 8945853e133SVivek Gautam 8955853e133SVivek Gautam return xhci_bulk_tx(udev, pipe, length, buffer); 8965853e133SVivek Gautam } 8975853e133SVivek Gautam 8985853e133SVivek Gautam /** 8995853e133SVivek Gautam * submit the control type of request to the Root hub/Device based on the devnum 9005853e133SVivek Gautam * 9015853e133SVivek Gautam * @param udev pointer to the USB device 9025853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 9035853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 9045853e133SVivek Gautam * @param length length of the buffer 9055853e133SVivek Gautam * @param setup Request type 9065853e133SVivek Gautam * @return returns 0 if successful else -1 on failure 9075853e133SVivek Gautam */ 9085853e133SVivek Gautam int 9095853e133SVivek Gautam submit_control_msg(struct usb_device *udev, unsigned long pipe, void *buffer, 9105853e133SVivek Gautam int length, struct devrequest *setup) 9115853e133SVivek Gautam { 912*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 9135853e133SVivek Gautam int ret = 0; 9145853e133SVivek Gautam 9155853e133SVivek Gautam if (usb_pipetype(pipe) != PIPE_CONTROL) { 9165853e133SVivek Gautam printf("non-control pipe (type=%lu)", usb_pipetype(pipe)); 9175853e133SVivek Gautam return -EINVAL; 9185853e133SVivek Gautam } 9195853e133SVivek Gautam 9205853e133SVivek Gautam if (usb_pipedevice(pipe) == ctrl->rootdev) 9215853e133SVivek Gautam return xhci_submit_root(udev, pipe, buffer, setup); 9225853e133SVivek Gautam 9235853e133SVivek Gautam if (setup->request == USB_REQ_SET_ADDRESS) 9245853e133SVivek Gautam return xhci_address_device(udev); 9255853e133SVivek Gautam 9265853e133SVivek Gautam if (setup->request == USB_REQ_SET_CONFIGURATION) { 9275853e133SVivek Gautam ret = xhci_set_configuration(udev); 9285853e133SVivek Gautam if (ret) { 9295853e133SVivek Gautam puts("Failed to configure xHCI endpoint\n"); 9305853e133SVivek Gautam return ret; 9315853e133SVivek Gautam } 9325853e133SVivek Gautam } 9335853e133SVivek Gautam 9345853e133SVivek Gautam return xhci_ctrl_tx(udev, pipe, setup, length, buffer); 9355853e133SVivek Gautam } 9365853e133SVivek Gautam 9375853e133SVivek Gautam /** 9385853e133SVivek Gautam * Intialises the XHCI host controller 9395853e133SVivek Gautam * and allocates the necessary data structures 9405853e133SVivek Gautam * 9415853e133SVivek Gautam * @param index index to the host controller data structure 9425853e133SVivek Gautam * @return pointer to the intialised controller 9435853e133SVivek Gautam */ 94406d513ecSTroy Kisky int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) 9455853e133SVivek Gautam { 9465853e133SVivek Gautam uint32_t val; 9475853e133SVivek Gautam uint32_t val2; 9485853e133SVivek Gautam uint32_t reg; 9495853e133SVivek Gautam struct xhci_hccr *hccr; 9505853e133SVivek Gautam struct xhci_hcor *hcor; 9515853e133SVivek Gautam struct xhci_ctrl *ctrl; 9525853e133SVivek Gautam 9535853e133SVivek Gautam if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0) 9545853e133SVivek Gautam return -ENODEV; 9555853e133SVivek Gautam 9565853e133SVivek Gautam if (xhci_reset(hcor) != 0) 9575853e133SVivek Gautam return -ENODEV; 9585853e133SVivek Gautam 9595853e133SVivek Gautam ctrl = &xhcic[index]; 9605853e133SVivek Gautam 9615853e133SVivek Gautam ctrl->hccr = hccr; 9625853e133SVivek Gautam ctrl->hcor = hcor; 9635853e133SVivek Gautam 9645853e133SVivek Gautam /* 9655853e133SVivek Gautam * Program the Number of Device Slots Enabled field in the CONFIG 9665853e133SVivek Gautam * register with the max value of slots the HC can handle. 9675853e133SVivek Gautam */ 9685853e133SVivek Gautam val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK); 9695853e133SVivek Gautam val2 = xhci_readl(&hcor->or_config); 9705853e133SVivek Gautam val |= (val2 & ~HCS_SLOTS_MASK); 9715853e133SVivek Gautam xhci_writel(&hcor->or_config, val); 9725853e133SVivek Gautam 9735853e133SVivek Gautam /* initializing xhci data structures */ 9745853e133SVivek Gautam if (xhci_mem_init(ctrl, hccr, hcor) < 0) 9755853e133SVivek Gautam return -ENOMEM; 9765853e133SVivek Gautam 9775853e133SVivek Gautam reg = xhci_readl(&hccr->cr_hcsparams1); 9785853e133SVivek Gautam descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >> 9795853e133SVivek Gautam HCS_MAX_PORTS_SHIFT); 9805853e133SVivek Gautam printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); 9815853e133SVivek Gautam 9825853e133SVivek Gautam /* Port Indicators */ 9835853e133SVivek Gautam reg = xhci_readl(&hccr->cr_hccparams); 9845853e133SVivek Gautam if (HCS_INDICATOR(reg)) 9855853e133SVivek Gautam put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) 9865853e133SVivek Gautam | 0x80, &descriptor.hub.wHubCharacteristics); 9875853e133SVivek Gautam 9885853e133SVivek Gautam /* Port Power Control */ 9895853e133SVivek Gautam if (HCC_PPC(reg)) 9905853e133SVivek Gautam put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) 9915853e133SVivek Gautam | 0x01, &descriptor.hub.wHubCharacteristics); 9925853e133SVivek Gautam 9935853e133SVivek Gautam if (xhci_start(hcor)) { 9945853e133SVivek Gautam xhci_reset(hcor); 9955853e133SVivek Gautam return -ENODEV; 9965853e133SVivek Gautam } 9975853e133SVivek Gautam 9985853e133SVivek Gautam /* Zero'ing IRQ control register and IRQ pending register */ 9995853e133SVivek Gautam xhci_writel(&ctrl->ir_set->irq_control, 0x0); 10005853e133SVivek Gautam xhci_writel(&ctrl->ir_set->irq_pending, 0x0); 10015853e133SVivek Gautam 10025853e133SVivek Gautam reg = HC_VERSION(xhci_readl(&hccr->cr_capbase)); 10035853e133SVivek Gautam printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff); 10045853e133SVivek Gautam 10055853e133SVivek Gautam *controller = &xhcic[index]; 10065853e133SVivek Gautam 10075853e133SVivek Gautam return 0; 10085853e133SVivek Gautam } 10095853e133SVivek Gautam 10105853e133SVivek Gautam /** 10115853e133SVivek Gautam * Stops the XHCI host controller 10125853e133SVivek Gautam * and cleans up all the related data structures 10135853e133SVivek Gautam * 10145853e133SVivek Gautam * @param index index to the host controller data structure 10155853e133SVivek Gautam * @return none 10165853e133SVivek Gautam */ 10175853e133SVivek Gautam int usb_lowlevel_stop(int index) 10185853e133SVivek Gautam { 10195853e133SVivek Gautam struct xhci_ctrl *ctrl = (xhcic + index); 10205853e133SVivek Gautam u32 temp; 10215853e133SVivek Gautam 10225853e133SVivek Gautam xhci_reset(ctrl->hcor); 10235853e133SVivek Gautam 10245853e133SVivek Gautam debug("// Disabling event ring interrupts\n"); 10255853e133SVivek Gautam temp = xhci_readl(&ctrl->hcor->or_usbsts); 10265853e133SVivek Gautam xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT); 10275853e133SVivek Gautam temp = xhci_readl(&ctrl->ir_set->irq_pending); 10285853e133SVivek Gautam xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp)); 10295853e133SVivek Gautam 10305853e133SVivek Gautam xhci_hcd_stop(index); 10315853e133SVivek Gautam 10325853e133SVivek Gautam xhci_cleanup(ctrl); 10335853e133SVivek Gautam 10345853e133SVivek Gautam return 0; 10355853e133SVivek Gautam } 1036