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 #include <common.h> 18*a5762fe0SSimon Glass #include <dm.h> 195853e133SVivek Gautam #include <asm/byteorder.h> 205853e133SVivek Gautam #include <usb.h> 215853e133SVivek Gautam #include <malloc.h> 225853e133SVivek Gautam #include <asm/cache.h> 235853e133SVivek Gautam #include <asm-generic/errno.h> 245853e133SVivek Gautam 255853e133SVivek Gautam #include "xhci.h" 265853e133SVivek Gautam 275853e133SVivek Gautam #define CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE 285853e133SVivek Gautam /** 295853e133SVivek Gautam * flushes the address passed till the length 305853e133SVivek Gautam * 315853e133SVivek Gautam * @param addr pointer to memory region to be flushed 325853e133SVivek Gautam * @param len the length of the cache line to be flushed 335853e133SVivek Gautam * @return none 345853e133SVivek Gautam */ 35421a5a0cSSergey Temerkhanov void xhci_flush_cache(uintptr_t addr, u32 len) 365853e133SVivek Gautam { 375853e133SVivek Gautam BUG_ON((void *)addr == NULL || len == 0); 385853e133SVivek Gautam 395853e133SVivek Gautam flush_dcache_range(addr & ~(CACHELINE_SIZE - 1), 405853e133SVivek Gautam ALIGN(addr + len, CACHELINE_SIZE)); 415853e133SVivek Gautam } 425853e133SVivek Gautam 435853e133SVivek Gautam /** 445853e133SVivek Gautam * invalidates the address passed till the length 455853e133SVivek Gautam * 465853e133SVivek Gautam * @param addr pointer to memory region to be invalidates 475853e133SVivek Gautam * @param len the length of the cache line to be invalidated 485853e133SVivek Gautam * @return none 495853e133SVivek Gautam */ 50421a5a0cSSergey Temerkhanov void xhci_inval_cache(uintptr_t addr, u32 len) 515853e133SVivek Gautam { 525853e133SVivek Gautam BUG_ON((void *)addr == NULL || len == 0); 535853e133SVivek Gautam 545853e133SVivek Gautam invalidate_dcache_range(addr & ~(CACHELINE_SIZE - 1), 555853e133SVivek Gautam ALIGN(addr + len, CACHELINE_SIZE)); 565853e133SVivek Gautam } 575853e133SVivek Gautam 585853e133SVivek Gautam 595853e133SVivek Gautam /** 605853e133SVivek Gautam * frees the "segment" pointer passed 615853e133SVivek Gautam * 625853e133SVivek Gautam * @param ptr pointer to "segement" to be freed 635853e133SVivek Gautam * @return none 645853e133SVivek Gautam */ 655853e133SVivek Gautam static void xhci_segment_free(struct xhci_segment *seg) 665853e133SVivek Gautam { 675853e133SVivek Gautam free(seg->trbs); 685853e133SVivek Gautam seg->trbs = NULL; 695853e133SVivek Gautam 705853e133SVivek Gautam free(seg); 715853e133SVivek Gautam } 725853e133SVivek Gautam 735853e133SVivek Gautam /** 745853e133SVivek Gautam * frees the "ring" pointer passed 755853e133SVivek Gautam * 765853e133SVivek Gautam * @param ptr pointer to "ring" to be freed 775853e133SVivek Gautam * @return none 785853e133SVivek Gautam */ 795853e133SVivek Gautam static void xhci_ring_free(struct xhci_ring *ring) 805853e133SVivek Gautam { 815853e133SVivek Gautam struct xhci_segment *seg; 825853e133SVivek Gautam struct xhci_segment *first_seg; 835853e133SVivek Gautam 845853e133SVivek Gautam BUG_ON(!ring); 855853e133SVivek Gautam 865853e133SVivek Gautam first_seg = ring->first_seg; 875853e133SVivek Gautam seg = first_seg->next; 885853e133SVivek Gautam while (seg != first_seg) { 895853e133SVivek Gautam struct xhci_segment *next = seg->next; 905853e133SVivek Gautam xhci_segment_free(seg); 915853e133SVivek Gautam seg = next; 925853e133SVivek Gautam } 935853e133SVivek Gautam xhci_segment_free(first_seg); 945853e133SVivek Gautam 955853e133SVivek Gautam free(ring); 965853e133SVivek Gautam } 975853e133SVivek Gautam 985853e133SVivek Gautam /** 995853e133SVivek Gautam * frees the "xhci_container_ctx" pointer passed 1005853e133SVivek Gautam * 1015853e133SVivek Gautam * @param ptr pointer to "xhci_container_ctx" to be freed 1025853e133SVivek Gautam * @return none 1035853e133SVivek Gautam */ 1045853e133SVivek Gautam static void xhci_free_container_ctx(struct xhci_container_ctx *ctx) 1055853e133SVivek Gautam { 1065853e133SVivek Gautam free(ctx->bytes); 1075853e133SVivek Gautam free(ctx); 1085853e133SVivek Gautam } 1095853e133SVivek Gautam 1105853e133SVivek Gautam /** 1115853e133SVivek Gautam * frees the virtual devices for "xhci_ctrl" pointer passed 1125853e133SVivek Gautam * 1135853e133SVivek Gautam * @param ptr pointer to "xhci_ctrl" whose virtual devices are to be freed 1145853e133SVivek Gautam * @return none 1155853e133SVivek Gautam */ 1165853e133SVivek Gautam static void xhci_free_virt_devices(struct xhci_ctrl *ctrl) 1175853e133SVivek Gautam { 1185853e133SVivek Gautam int i; 1195853e133SVivek Gautam int slot_id; 1205853e133SVivek Gautam struct xhci_virt_device *virt_dev; 1215853e133SVivek Gautam 1225853e133SVivek Gautam /* 1235853e133SVivek Gautam * refactored here to loop through all virt_dev 1245853e133SVivek Gautam * Slot ID 0 is reserved 1255853e133SVivek Gautam */ 1265853e133SVivek Gautam for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) { 1275853e133SVivek Gautam virt_dev = ctrl->devs[slot_id]; 1285853e133SVivek Gautam if (!virt_dev) 1295853e133SVivek Gautam continue; 1305853e133SVivek Gautam 1315853e133SVivek Gautam ctrl->dcbaa->dev_context_ptrs[slot_id] = 0; 1325853e133SVivek Gautam 1335853e133SVivek Gautam for (i = 0; i < 31; ++i) 1345853e133SVivek Gautam if (virt_dev->eps[i].ring) 1355853e133SVivek Gautam xhci_ring_free(virt_dev->eps[i].ring); 1365853e133SVivek Gautam 1375853e133SVivek Gautam if (virt_dev->in_ctx) 1385853e133SVivek Gautam xhci_free_container_ctx(virt_dev->in_ctx); 1395853e133SVivek Gautam if (virt_dev->out_ctx) 1405853e133SVivek Gautam xhci_free_container_ctx(virt_dev->out_ctx); 1415853e133SVivek Gautam 1425853e133SVivek Gautam free(virt_dev); 1435853e133SVivek Gautam /* make sure we are pointing to NULL */ 1445853e133SVivek Gautam ctrl->devs[slot_id] = NULL; 1455853e133SVivek Gautam } 1465853e133SVivek Gautam } 1475853e133SVivek Gautam 1485853e133SVivek Gautam /** 1495853e133SVivek Gautam * frees all the memory allocated 1505853e133SVivek Gautam * 1515853e133SVivek Gautam * @param ptr pointer to "xhci_ctrl" to be cleaned up 1525853e133SVivek Gautam * @return none 1535853e133SVivek Gautam */ 1545853e133SVivek Gautam void xhci_cleanup(struct xhci_ctrl *ctrl) 1555853e133SVivek Gautam { 1565853e133SVivek Gautam xhci_ring_free(ctrl->event_ring); 1575853e133SVivek Gautam xhci_ring_free(ctrl->cmd_ring); 1585853e133SVivek Gautam xhci_free_virt_devices(ctrl); 1595853e133SVivek Gautam free(ctrl->erst.entries); 1605853e133SVivek Gautam free(ctrl->dcbaa); 1615853e133SVivek Gautam memset(ctrl, '\0', sizeof(struct xhci_ctrl)); 1625853e133SVivek Gautam } 1635853e133SVivek Gautam 1645853e133SVivek Gautam /** 1655853e133SVivek Gautam * Malloc the aligned memory 1665853e133SVivek Gautam * 1675853e133SVivek Gautam * @param size size of memory to be allocated 1685853e133SVivek Gautam * @return allocates the memory and returns the aligned pointer 1695853e133SVivek Gautam */ 1705853e133SVivek Gautam static void *xhci_malloc(unsigned int size) 1715853e133SVivek Gautam { 1725853e133SVivek Gautam void *ptr; 1735853e133SVivek Gautam size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE); 1745853e133SVivek Gautam 1755853e133SVivek Gautam ptr = memalign(cacheline_size, ALIGN(size, cacheline_size)); 1765853e133SVivek Gautam BUG_ON(!ptr); 1775853e133SVivek Gautam memset(ptr, '\0', size); 1785853e133SVivek Gautam 179421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)ptr, size); 1805853e133SVivek Gautam 1815853e133SVivek Gautam return ptr; 1825853e133SVivek Gautam } 1835853e133SVivek Gautam 1845853e133SVivek Gautam /** 1855853e133SVivek Gautam * Make the prev segment point to the next segment. 1865853e133SVivek Gautam * Change the last TRB in the prev segment to be a Link TRB which points to the 1875853e133SVivek Gautam * address of the next segment. The caller needs to set any Link TRB 1885853e133SVivek Gautam * related flags, such as End TRB, Toggle Cycle, and no snoop. 1895853e133SVivek Gautam * 1905853e133SVivek Gautam * @param prev pointer to the previous segment 1915853e133SVivek Gautam * @param next pointer to the next segment 1925853e133SVivek Gautam * @param link_trbs flag to indicate whether to link the trbs or NOT 1935853e133SVivek Gautam * @return none 1945853e133SVivek Gautam */ 1955853e133SVivek Gautam static void xhci_link_segments(struct xhci_segment *prev, 1965853e133SVivek Gautam struct xhci_segment *next, bool link_trbs) 1975853e133SVivek Gautam { 1985853e133SVivek Gautam u32 val; 1995853e133SVivek Gautam u64 val_64 = 0; 2005853e133SVivek Gautam 2015853e133SVivek Gautam if (!prev || !next) 2025853e133SVivek Gautam return; 2035853e133SVivek Gautam prev->next = next; 2045853e133SVivek Gautam if (link_trbs) { 2055853e133SVivek Gautam val_64 = (uintptr_t)next->trbs; 2065853e133SVivek Gautam prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64; 2075853e133SVivek Gautam 2085853e133SVivek Gautam /* 2095853e133SVivek Gautam * Set the last TRB in the segment to 2105853e133SVivek Gautam * have a TRB type ID of Link TRB 2115853e133SVivek Gautam */ 2125853e133SVivek Gautam val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control); 2135853e133SVivek Gautam val &= ~TRB_TYPE_BITMASK; 2145853e133SVivek Gautam val |= (TRB_LINK << TRB_TYPE_SHIFT); 2155853e133SVivek Gautam 2165853e133SVivek Gautam prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val); 2175853e133SVivek Gautam } 2185853e133SVivek Gautam } 2195853e133SVivek Gautam 2205853e133SVivek Gautam /** 2215853e133SVivek Gautam * Initialises the Ring's enqueue,dequeue,enq_seg pointers 2225853e133SVivek Gautam * 2235853e133SVivek Gautam * @param ring pointer to the RING to be intialised 2245853e133SVivek Gautam * @return none 2255853e133SVivek Gautam */ 2265853e133SVivek Gautam static void xhci_initialize_ring_info(struct xhci_ring *ring) 2275853e133SVivek Gautam { 2285853e133SVivek Gautam /* 2295853e133SVivek Gautam * The ring is empty, so the enqueue pointer == dequeue pointer 2305853e133SVivek Gautam */ 2315853e133SVivek Gautam ring->enqueue = ring->first_seg->trbs; 2325853e133SVivek Gautam ring->enq_seg = ring->first_seg; 2335853e133SVivek Gautam ring->dequeue = ring->enqueue; 2345853e133SVivek Gautam ring->deq_seg = ring->first_seg; 2355853e133SVivek Gautam 2365853e133SVivek Gautam /* 2375853e133SVivek Gautam * The ring is initialized to 0. The producer must write 1 to the 2385853e133SVivek Gautam * cycle bit to handover ownership of the TRB, so PCS = 1. 2395853e133SVivek Gautam * The consumer must compare CCS to the cycle bit to 2405853e133SVivek Gautam * check ownership, so CCS = 1. 2415853e133SVivek Gautam */ 2425853e133SVivek Gautam ring->cycle_state = 1; 2435853e133SVivek Gautam } 2445853e133SVivek Gautam 2455853e133SVivek Gautam /** 2465853e133SVivek Gautam * Allocates a generic ring segment from the ring pool, sets the dma address, 2475853e133SVivek Gautam * initializes the segment to zero, and sets the private next pointer to NULL. 2485853e133SVivek Gautam * Section 4.11.1.1: 2495853e133SVivek Gautam * "All components of all Command and Transfer TRBs shall be initialized to '0'" 2505853e133SVivek Gautam * 2515853e133SVivek Gautam * @param none 2525853e133SVivek Gautam * @return pointer to the newly allocated SEGMENT 2535853e133SVivek Gautam */ 2545853e133SVivek Gautam static struct xhci_segment *xhci_segment_alloc(void) 2555853e133SVivek Gautam { 2565853e133SVivek Gautam struct xhci_segment *seg; 2575853e133SVivek Gautam 2585853e133SVivek Gautam seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment)); 2595853e133SVivek Gautam BUG_ON(!seg); 2605853e133SVivek Gautam 2615853e133SVivek Gautam seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE); 2625853e133SVivek Gautam 2635853e133SVivek Gautam seg->next = NULL; 2645853e133SVivek Gautam 2655853e133SVivek Gautam return seg; 2665853e133SVivek Gautam } 2675853e133SVivek Gautam 2685853e133SVivek Gautam /** 2695853e133SVivek Gautam * Create a new ring with zero or more segments. 2705853e133SVivek Gautam * TODO: current code only uses one-time-allocated single-segment rings 2715853e133SVivek Gautam * of 1KB anyway, so we might as well get rid of all the segment and 2725853e133SVivek Gautam * linking code (and maybe increase the size a bit, e.g. 4KB). 2735853e133SVivek Gautam * 2745853e133SVivek Gautam * 2755853e133SVivek Gautam * Link each segment together into a ring. 2765853e133SVivek Gautam * Set the end flag and the cycle toggle bit on the last segment. 2775853e133SVivek Gautam * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0. 2785853e133SVivek Gautam * 2795853e133SVivek Gautam * @param num_segs number of segments in the ring 2805853e133SVivek Gautam * @param link_trbs flag to indicate whether to link the trbs or NOT 2815853e133SVivek Gautam * @return pointer to the newly created RING 2825853e133SVivek Gautam */ 2835853e133SVivek Gautam struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs) 2845853e133SVivek Gautam { 2855853e133SVivek Gautam struct xhci_ring *ring; 2865853e133SVivek Gautam struct xhci_segment *prev; 2875853e133SVivek Gautam 2885853e133SVivek Gautam ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring)); 2895853e133SVivek Gautam BUG_ON(!ring); 2905853e133SVivek Gautam 2915853e133SVivek Gautam if (num_segs == 0) 2925853e133SVivek Gautam return ring; 2935853e133SVivek Gautam 2945853e133SVivek Gautam ring->first_seg = xhci_segment_alloc(); 2955853e133SVivek Gautam BUG_ON(!ring->first_seg); 2965853e133SVivek Gautam 2975853e133SVivek Gautam num_segs--; 2985853e133SVivek Gautam 2995853e133SVivek Gautam prev = ring->first_seg; 3005853e133SVivek Gautam while (num_segs > 0) { 3015853e133SVivek Gautam struct xhci_segment *next; 3025853e133SVivek Gautam 3035853e133SVivek Gautam next = xhci_segment_alloc(); 3045853e133SVivek Gautam BUG_ON(!next); 3055853e133SVivek Gautam 3065853e133SVivek Gautam xhci_link_segments(prev, next, link_trbs); 3075853e133SVivek Gautam 3085853e133SVivek Gautam prev = next; 3095853e133SVivek Gautam num_segs--; 3105853e133SVivek Gautam } 3115853e133SVivek Gautam xhci_link_segments(prev, ring->first_seg, link_trbs); 3125853e133SVivek Gautam if (link_trbs) { 3135853e133SVivek Gautam /* See section 4.9.2.1 and 6.4.4.1 */ 3145853e133SVivek Gautam prev->trbs[TRBS_PER_SEGMENT-1].link.control |= 3155853e133SVivek Gautam cpu_to_le32(LINK_TOGGLE); 3165853e133SVivek Gautam } 3175853e133SVivek Gautam xhci_initialize_ring_info(ring); 3185853e133SVivek Gautam 3195853e133SVivek Gautam return ring; 3205853e133SVivek Gautam } 3215853e133SVivek Gautam 3225853e133SVivek Gautam /** 3235853e133SVivek Gautam * Allocates the Container context 3245853e133SVivek Gautam * 3255853e133SVivek Gautam * @param ctrl Host controller data structure 3265853e133SVivek Gautam * @param type type of XHCI Container Context 3275853e133SVivek Gautam * @return NULL if failed else pointer to the context on success 3285853e133SVivek Gautam */ 3295853e133SVivek Gautam static struct xhci_container_ctx 3305853e133SVivek Gautam *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type) 3315853e133SVivek Gautam { 3325853e133SVivek Gautam struct xhci_container_ctx *ctx; 3335853e133SVivek Gautam 3345853e133SVivek Gautam ctx = (struct xhci_container_ctx *) 3355853e133SVivek Gautam malloc(sizeof(struct xhci_container_ctx)); 3365853e133SVivek Gautam BUG_ON(!ctx); 3375853e133SVivek Gautam 3385853e133SVivek Gautam BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT)); 3395853e133SVivek Gautam ctx->type = type; 3405853e133SVivek Gautam ctx->size = (MAX_EP_CTX_NUM + 1) * 3415853e133SVivek Gautam CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)); 3425853e133SVivek Gautam if (type == XHCI_CTX_TYPE_INPUT) 3435853e133SVivek Gautam ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)); 3445853e133SVivek Gautam 3455853e133SVivek Gautam ctx->bytes = (u8 *)xhci_malloc(ctx->size); 3465853e133SVivek Gautam 3475853e133SVivek Gautam return ctx; 3485853e133SVivek Gautam } 3495853e133SVivek Gautam 3505853e133SVivek Gautam /** 3515853e133SVivek Gautam * Allocating virtual device 3525853e133SVivek Gautam * 3535853e133SVivek Gautam * @param udev pointer to USB deivce structure 3545853e133SVivek Gautam * @return 0 on success else -1 on failure 3555853e133SVivek Gautam */ 3567e0c5ee8SSimon Glass int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id) 3575853e133SVivek Gautam { 3585853e133SVivek Gautam u64 byte_64 = 0; 3595853e133SVivek Gautam struct xhci_virt_device *virt_dev; 3605853e133SVivek Gautam 3615853e133SVivek Gautam /* Slot ID 0 is reserved */ 3625853e133SVivek Gautam if (ctrl->devs[slot_id]) { 3635853e133SVivek Gautam printf("Virt dev for slot[%d] already allocated\n", slot_id); 3645853e133SVivek Gautam return -EEXIST; 3655853e133SVivek Gautam } 3665853e133SVivek Gautam 3675853e133SVivek Gautam ctrl->devs[slot_id] = (struct xhci_virt_device *) 3685853e133SVivek Gautam malloc(sizeof(struct xhci_virt_device)); 3695853e133SVivek Gautam 3705853e133SVivek Gautam if (!ctrl->devs[slot_id]) { 3715853e133SVivek Gautam puts("Failed to allocate virtual device\n"); 3725853e133SVivek Gautam return -ENOMEM; 3735853e133SVivek Gautam } 3745853e133SVivek Gautam 3755853e133SVivek Gautam memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device)); 3765853e133SVivek Gautam virt_dev = ctrl->devs[slot_id]; 3775853e133SVivek Gautam 3785853e133SVivek Gautam /* Allocate the (output) device context that will be used in the HC. */ 3795853e133SVivek Gautam virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl, 3805853e133SVivek Gautam XHCI_CTX_TYPE_DEVICE); 3815853e133SVivek Gautam if (!virt_dev->out_ctx) { 3825853e133SVivek Gautam puts("Failed to allocate out context for virt dev\n"); 3835853e133SVivek Gautam return -ENOMEM; 3845853e133SVivek Gautam } 3855853e133SVivek Gautam 3865853e133SVivek Gautam /* Allocate the (input) device context for address device command */ 3875853e133SVivek Gautam virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl, 3885853e133SVivek Gautam XHCI_CTX_TYPE_INPUT); 3895853e133SVivek Gautam if (!virt_dev->in_ctx) { 3905853e133SVivek Gautam puts("Failed to allocate in context for virt dev\n"); 3915853e133SVivek Gautam return -ENOMEM; 3925853e133SVivek Gautam } 3935853e133SVivek Gautam 3945853e133SVivek Gautam /* Allocate endpoint 0 ring */ 3955853e133SVivek Gautam virt_dev->eps[0].ring = xhci_ring_alloc(1, true); 3965853e133SVivek Gautam 3975853e133SVivek Gautam byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes); 3985853e133SVivek Gautam 3995853e133SVivek Gautam /* Point to output device context in dcbaa. */ 4005853e133SVivek Gautam ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64; 4015853e133SVivek Gautam 402421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id], 4035853e133SVivek Gautam sizeof(__le64)); 4045853e133SVivek Gautam return 0; 4055853e133SVivek Gautam } 4065853e133SVivek Gautam 4075853e133SVivek Gautam /** 4085853e133SVivek Gautam * Allocates the necessary data structures 4095853e133SVivek Gautam * for XHCI host controller 4105853e133SVivek Gautam * 4115853e133SVivek Gautam * @param ctrl Host controller data structure 4125853e133SVivek Gautam * @param hccr pointer to HOST Controller Control Registers 4135853e133SVivek Gautam * @param hcor pointer to HOST Controller Operational Registers 4145853e133SVivek Gautam * @return 0 if successful else -1 on failure 4155853e133SVivek Gautam */ 4165853e133SVivek Gautam int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr, 4175853e133SVivek Gautam struct xhci_hcor *hcor) 4185853e133SVivek Gautam { 4195853e133SVivek Gautam uint64_t val_64; 4205853e133SVivek Gautam uint64_t trb_64; 4215853e133SVivek Gautam uint32_t val; 4225853e133SVivek Gautam unsigned long deq; 4235853e133SVivek Gautam int i; 4245853e133SVivek Gautam struct xhci_segment *seg; 4255853e133SVivek Gautam 4265853e133SVivek Gautam /* DCBAA initialization */ 4275853e133SVivek Gautam ctrl->dcbaa = (struct xhci_device_context_array *) 4285853e133SVivek Gautam xhci_malloc(sizeof(struct xhci_device_context_array)); 4295853e133SVivek Gautam if (ctrl->dcbaa == NULL) { 4305853e133SVivek Gautam puts("unable to allocate DCBA\n"); 4315853e133SVivek Gautam return -ENOMEM; 4325853e133SVivek Gautam } 4335853e133SVivek Gautam 4345853e133SVivek Gautam val_64 = (uintptr_t)ctrl->dcbaa; 4355853e133SVivek Gautam /* Set the pointer in DCBAA register */ 4365853e133SVivek Gautam xhci_writeq(&hcor->or_dcbaap, val_64); 4375853e133SVivek Gautam 4385853e133SVivek Gautam /* Command ring control pointer register initialization */ 4395853e133SVivek Gautam ctrl->cmd_ring = xhci_ring_alloc(1, true); 4405853e133SVivek Gautam 4415853e133SVivek Gautam /* Set the address in the Command Ring Control register */ 4425853e133SVivek Gautam trb_64 = (uintptr_t)ctrl->cmd_ring->first_seg->trbs; 4435853e133SVivek Gautam val_64 = xhci_readq(&hcor->or_crcr); 4445853e133SVivek Gautam val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) | 4455853e133SVivek Gautam (trb_64 & (u64) ~CMD_RING_RSVD_BITS) | 4465853e133SVivek Gautam ctrl->cmd_ring->cycle_state; 4475853e133SVivek Gautam xhci_writeq(&hcor->or_crcr, val_64); 4485853e133SVivek Gautam 4495853e133SVivek Gautam /* write the address of db register */ 4505853e133SVivek Gautam val = xhci_readl(&hccr->cr_dboff); 4515853e133SVivek Gautam val &= DBOFF_MASK; 4525853e133SVivek Gautam ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val); 4535853e133SVivek Gautam 4545853e133SVivek Gautam /* write the address of runtime register */ 4555853e133SVivek Gautam val = xhci_readl(&hccr->cr_rtsoff); 4565853e133SVivek Gautam val &= RTSOFF_MASK; 4575853e133SVivek Gautam ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val); 4585853e133SVivek Gautam 4595853e133SVivek Gautam /* writting the address of ir_set structure */ 4605853e133SVivek Gautam ctrl->ir_set = &ctrl->run_regs->ir_set[0]; 4615853e133SVivek Gautam 4625853e133SVivek Gautam /* Event ring does not maintain link TRB */ 4635853e133SVivek Gautam ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false); 4645853e133SVivek Gautam ctrl->erst.entries = (struct xhci_erst_entry *) 4655853e133SVivek Gautam xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS); 4665853e133SVivek Gautam 4675853e133SVivek Gautam ctrl->erst.num_entries = ERST_NUM_SEGS; 4685853e133SVivek Gautam 4695853e133SVivek Gautam for (val = 0, seg = ctrl->event_ring->first_seg; 4705853e133SVivek Gautam val < ERST_NUM_SEGS; 4715853e133SVivek Gautam val++) { 4725853e133SVivek Gautam trb_64 = 0; 4735853e133SVivek Gautam trb_64 = (uintptr_t)seg->trbs; 4745853e133SVivek Gautam struct xhci_erst_entry *entry = &ctrl->erst.entries[val]; 4755853e133SVivek Gautam xhci_writeq(&entry->seg_addr, trb_64); 4765853e133SVivek Gautam entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT); 4775853e133SVivek Gautam entry->rsvd = 0; 4785853e133SVivek Gautam seg = seg->next; 4795853e133SVivek Gautam } 480421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)ctrl->erst.entries, 4815853e133SVivek Gautam ERST_NUM_SEGS * sizeof(struct xhci_erst_entry)); 4825853e133SVivek Gautam 4835853e133SVivek Gautam deq = (unsigned long)ctrl->event_ring->dequeue; 4845853e133SVivek Gautam 4855853e133SVivek Gautam /* Update HC event ring dequeue pointer */ 4865853e133SVivek Gautam xhci_writeq(&ctrl->ir_set->erst_dequeue, 4875853e133SVivek Gautam (u64)deq & (u64)~ERST_PTR_MASK); 4885853e133SVivek Gautam 4895853e133SVivek Gautam /* set ERST count with the number of entries in the segment table */ 4905853e133SVivek Gautam val = xhci_readl(&ctrl->ir_set->erst_size); 4915853e133SVivek Gautam val &= ERST_SIZE_MASK; 4925853e133SVivek Gautam val |= ERST_NUM_SEGS; 4935853e133SVivek Gautam xhci_writel(&ctrl->ir_set->erst_size, val); 4945853e133SVivek Gautam 4955853e133SVivek Gautam /* this is the event ring segment table pointer */ 4965853e133SVivek Gautam val_64 = xhci_readq(&ctrl->ir_set->erst_base); 4975853e133SVivek Gautam val_64 &= ERST_PTR_MASK; 498421a5a0cSSergey Temerkhanov val_64 |= ((uintptr_t)(ctrl->erst.entries) & ~ERST_PTR_MASK); 4995853e133SVivek Gautam 5005853e133SVivek Gautam xhci_writeq(&ctrl->ir_set->erst_base, val_64); 5015853e133SVivek Gautam 5025853e133SVivek Gautam /* initializing the virtual devices to NULL */ 5035853e133SVivek Gautam for (i = 0; i < MAX_HC_SLOTS; ++i) 5045853e133SVivek Gautam ctrl->devs[i] = NULL; 5055853e133SVivek Gautam 5065853e133SVivek Gautam /* 5075853e133SVivek Gautam * Just Zero'ing this register completely, 5085853e133SVivek Gautam * or some spurious Device Notification Events 5095853e133SVivek Gautam * might screw things here. 5105853e133SVivek Gautam */ 5115853e133SVivek Gautam xhci_writel(&hcor->or_dnctrl, 0x0); 5125853e133SVivek Gautam 5135853e133SVivek Gautam return 0; 5145853e133SVivek Gautam } 5155853e133SVivek Gautam 5165853e133SVivek Gautam /** 5175853e133SVivek Gautam * Give the input control context for the passed container context 5185853e133SVivek Gautam * 5195853e133SVivek Gautam * @param ctx pointer to the context 5205853e133SVivek Gautam * @return pointer to the Input control context data 5215853e133SVivek Gautam */ 5225853e133SVivek Gautam struct xhci_input_control_ctx 5235853e133SVivek Gautam *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx) 5245853e133SVivek Gautam { 5255853e133SVivek Gautam BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT); 5265853e133SVivek Gautam return (struct xhci_input_control_ctx *)ctx->bytes; 5275853e133SVivek Gautam } 5285853e133SVivek Gautam 5295853e133SVivek Gautam /** 5305853e133SVivek Gautam * Give the slot context for the passed container context 5315853e133SVivek Gautam * 5325853e133SVivek Gautam * @param ctrl Host controller data structure 5335853e133SVivek Gautam * @param ctx pointer to the context 5345853e133SVivek Gautam * @return pointer to the slot control context data 5355853e133SVivek Gautam */ 5365853e133SVivek Gautam struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl, 5375853e133SVivek Gautam struct xhci_container_ctx *ctx) 5385853e133SVivek Gautam { 5395853e133SVivek Gautam if (ctx->type == XHCI_CTX_TYPE_DEVICE) 5405853e133SVivek Gautam return (struct xhci_slot_ctx *)ctx->bytes; 5415853e133SVivek Gautam 5425853e133SVivek Gautam return (struct xhci_slot_ctx *) 5435853e133SVivek Gautam (ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))); 5445853e133SVivek Gautam } 5455853e133SVivek Gautam 5465853e133SVivek Gautam /** 5475853e133SVivek Gautam * Gets the EP context from based on the ep_index 5485853e133SVivek Gautam * 5495853e133SVivek Gautam * @param ctrl Host controller data structure 5505853e133SVivek Gautam * @param ctx context container 5515853e133SVivek Gautam * @param ep_index index of the endpoint 5525853e133SVivek Gautam * @return pointer to the End point context 5535853e133SVivek Gautam */ 5545853e133SVivek Gautam struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl, 5555853e133SVivek Gautam struct xhci_container_ctx *ctx, 5565853e133SVivek Gautam unsigned int ep_index) 5575853e133SVivek Gautam { 5585853e133SVivek Gautam /* increment ep index by offset of start of ep ctx array */ 5595853e133SVivek Gautam ep_index++; 5605853e133SVivek Gautam if (ctx->type == XHCI_CTX_TYPE_INPUT) 5615853e133SVivek Gautam ep_index++; 5625853e133SVivek Gautam 5635853e133SVivek Gautam return (struct xhci_ep_ctx *) 5645853e133SVivek Gautam (ctx->bytes + 5655853e133SVivek Gautam (ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)))); 5665853e133SVivek Gautam } 5675853e133SVivek Gautam 5685853e133SVivek Gautam /** 5695853e133SVivek Gautam * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy. 5705853e133SVivek Gautam * Useful when you want to change one particular aspect of the endpoint 5715853e133SVivek Gautam * and then issue a configure endpoint command. 5725853e133SVivek Gautam * 5735853e133SVivek Gautam * @param ctrl Host controller data structure 5745853e133SVivek Gautam * @param in_ctx contains the input context 5755853e133SVivek Gautam * @param out_ctx contains the input context 5765853e133SVivek Gautam * @param ep_index index of the end point 5775853e133SVivek Gautam * @return none 5785853e133SVivek Gautam */ 5795853e133SVivek Gautam void xhci_endpoint_copy(struct xhci_ctrl *ctrl, 5805853e133SVivek Gautam struct xhci_container_ctx *in_ctx, 5815853e133SVivek Gautam struct xhci_container_ctx *out_ctx, 5825853e133SVivek Gautam unsigned int ep_index) 5835853e133SVivek Gautam { 5845853e133SVivek Gautam struct xhci_ep_ctx *out_ep_ctx; 5855853e133SVivek Gautam struct xhci_ep_ctx *in_ep_ctx; 5865853e133SVivek Gautam 5875853e133SVivek Gautam out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); 5885853e133SVivek Gautam in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); 5895853e133SVivek Gautam 5905853e133SVivek Gautam in_ep_ctx->ep_info = out_ep_ctx->ep_info; 5915853e133SVivek Gautam in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2; 5925853e133SVivek Gautam in_ep_ctx->deq = out_ep_ctx->deq; 5935853e133SVivek Gautam in_ep_ctx->tx_info = out_ep_ctx->tx_info; 5945853e133SVivek Gautam } 5955853e133SVivek Gautam 5965853e133SVivek Gautam /** 5975853e133SVivek Gautam * Copy output xhci_slot_ctx to the input xhci_slot_ctx. 5985853e133SVivek Gautam * Useful when you want to change one particular aspect of the endpoint 5995853e133SVivek Gautam * and then issue a configure endpoint command. 6005853e133SVivek Gautam * Only the context entries field matters, but 6015853e133SVivek Gautam * we'll copy the whole thing anyway. 6025853e133SVivek Gautam * 6035853e133SVivek Gautam * @param ctrl Host controller data structure 6045853e133SVivek Gautam * @param in_ctx contains the inpout context 6055853e133SVivek Gautam * @param out_ctx contains the inpout context 6065853e133SVivek Gautam * @return none 6075853e133SVivek Gautam */ 6085853e133SVivek Gautam void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx, 6095853e133SVivek Gautam struct xhci_container_ctx *out_ctx) 6105853e133SVivek Gautam { 6115853e133SVivek Gautam struct xhci_slot_ctx *in_slot_ctx; 6125853e133SVivek Gautam struct xhci_slot_ctx *out_slot_ctx; 6135853e133SVivek Gautam 6145853e133SVivek Gautam in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); 6155853e133SVivek Gautam out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx); 6165853e133SVivek Gautam 6175853e133SVivek Gautam in_slot_ctx->dev_info = out_slot_ctx->dev_info; 6185853e133SVivek Gautam in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2; 6195853e133SVivek Gautam in_slot_ctx->tt_info = out_slot_ctx->tt_info; 6205853e133SVivek Gautam in_slot_ctx->dev_state = out_slot_ctx->dev_state; 6215853e133SVivek Gautam } 6225853e133SVivek Gautam 6235853e133SVivek Gautam /** 6245853e133SVivek Gautam * Setup an xHCI virtual device for a Set Address command 6255853e133SVivek Gautam * 6265853e133SVivek Gautam * @param udev pointer to the Device Data Structure 6275853e133SVivek Gautam * @return returns negative value on failure else 0 on success 6285853e133SVivek Gautam */ 6295dd75e3bSSimon Glass void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl, int slot_id, 6305dd75e3bSSimon Glass int speed, int hop_portnr) 6315853e133SVivek Gautam { 6325853e133SVivek Gautam struct xhci_virt_device *virt_dev; 6335853e133SVivek Gautam struct xhci_ep_ctx *ep0_ctx; 6345853e133SVivek Gautam struct xhci_slot_ctx *slot_ctx; 6355853e133SVivek Gautam u32 port_num = 0; 6365853e133SVivek Gautam u64 trb_64 = 0; 6375853e133SVivek Gautam 6385dd75e3bSSimon Glass virt_dev = ctrl->devs[slot_id]; 6395853e133SVivek Gautam 6405853e133SVivek Gautam BUG_ON(!virt_dev); 6415853e133SVivek Gautam 6425853e133SVivek Gautam /* Extract the EP0 and Slot Ctrl */ 6435853e133SVivek Gautam ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0); 6445853e133SVivek Gautam slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx); 6455853e133SVivek Gautam 6465853e133SVivek Gautam /* Only the control endpoint is valid - one endpoint context */ 6475853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | 0); 6485853e133SVivek Gautam 6495dd75e3bSSimon Glass switch (speed) { 6505853e133SVivek Gautam case USB_SPEED_SUPER: 6515853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS); 6525853e133SVivek Gautam break; 6535853e133SVivek Gautam case USB_SPEED_HIGH: 6545853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS); 6555853e133SVivek Gautam break; 6565853e133SVivek Gautam case USB_SPEED_FULL: 6575853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS); 6585853e133SVivek Gautam break; 6595853e133SVivek Gautam case USB_SPEED_LOW: 6605853e133SVivek Gautam slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS); 6615853e133SVivek Gautam break; 6625853e133SVivek Gautam default: 6635853e133SVivek Gautam /* Speed was set earlier, this shouldn't happen. */ 6645853e133SVivek Gautam BUG(); 6655853e133SVivek Gautam } 6665853e133SVivek Gautam 6675dd75e3bSSimon Glass port_num = hop_portnr; 6685853e133SVivek Gautam debug("port_num = %d\n", port_num); 6695853e133SVivek Gautam 6705853e133SVivek Gautam slot_ctx->dev_info2 |= 6715853e133SVivek Gautam cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) << 6725853e133SVivek Gautam ROOT_HUB_PORT_SHIFT)); 6735853e133SVivek Gautam 6745853e133SVivek Gautam /* Step 4 - ring already allocated */ 6755853e133SVivek Gautam /* Step 5 */ 6765853e133SVivek Gautam ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT); 6775dd75e3bSSimon Glass debug("SPEED = %d\n", speed); 6785853e133SVivek Gautam 6795dd75e3bSSimon Glass switch (speed) { 6805853e133SVivek Gautam case USB_SPEED_SUPER: 6815853e133SVivek Gautam ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) << 6825853e133SVivek Gautam MAX_PACKET_SHIFT)); 6835853e133SVivek Gautam debug("Setting Packet size = 512bytes\n"); 6845853e133SVivek Gautam break; 6855853e133SVivek Gautam case USB_SPEED_HIGH: 6865853e133SVivek Gautam /* USB core guesses at a 64-byte max packet first for FS devices */ 6875853e133SVivek Gautam case USB_SPEED_FULL: 6885853e133SVivek Gautam ep0_ctx->ep_info2 |= cpu_to_le32(((64 & MAX_PACKET_MASK) << 6895853e133SVivek Gautam MAX_PACKET_SHIFT)); 6905853e133SVivek Gautam debug("Setting Packet size = 64bytes\n"); 6915853e133SVivek Gautam break; 6925853e133SVivek Gautam case USB_SPEED_LOW: 6935853e133SVivek Gautam ep0_ctx->ep_info2 |= cpu_to_le32(((8 & MAX_PACKET_MASK) << 6945853e133SVivek Gautam MAX_PACKET_SHIFT)); 6955853e133SVivek Gautam debug("Setting Packet size = 8bytes\n"); 6965853e133SVivek Gautam break; 6975853e133SVivek Gautam default: 6985853e133SVivek Gautam /* New speed? */ 6995853e133SVivek Gautam BUG(); 7005853e133SVivek Gautam } 7015853e133SVivek Gautam 7025853e133SVivek Gautam /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */ 7035853e133SVivek Gautam ep0_ctx->ep_info2 |= 7045853e133SVivek Gautam cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) | 7055853e133SVivek Gautam ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT)); 7065853e133SVivek Gautam 7075853e133SVivek Gautam trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs; 7085853e133SVivek Gautam ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state); 7095853e133SVivek Gautam 7105853e133SVivek Gautam /* Steps 7 and 8 were done in xhci_alloc_virt_device() */ 7115853e133SVivek Gautam 712421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx)); 713421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx)); 7145853e133SVivek Gautam } 715