xref: /rk3399_rockchip-uboot/drivers/usb/host/xhci-mem.c (revision 1a4f6af8bfd44c8ae6e87a81ff125eed47042cc5)
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>
18a5762fe0SSimon 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>
235d97dff0SMasahiro Yamada #include <linux/errno.h>
245853e133SVivek Gautam 
25*143fc13bSJean-Jacques Hiblot #include <usb/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  */
xhci_flush_cache(uintptr_t addr,u32 len)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  */
xhci_inval_cache(uintptr_t addr,u32 len)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  */
xhci_segment_free(struct xhci_segment * seg)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  */
xhci_ring_free(struct xhci_ring * ring)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 /**
99209b98deSBin Meng  * Free the scratchpad buffer array and scratchpad buffers
100209b98deSBin Meng  *
101209b98deSBin Meng  * @ctrl	host controller data structure
102209b98deSBin Meng  * @return	none
103209b98deSBin Meng  */
xhci_scratchpad_free(struct xhci_ctrl * ctrl)104209b98deSBin Meng static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
105209b98deSBin Meng {
106209b98deSBin Meng 	if (!ctrl->scratchpad)
107209b98deSBin Meng 		return;
108209b98deSBin Meng 
109209b98deSBin Meng 	ctrl->dcbaa->dev_context_ptrs[0] = 0;
110209b98deSBin Meng 
111209b98deSBin Meng 	free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
112209b98deSBin Meng 	free(ctrl->scratchpad->sp_array);
113209b98deSBin Meng 	free(ctrl->scratchpad);
114209b98deSBin Meng 	ctrl->scratchpad = NULL;
115209b98deSBin Meng }
116209b98deSBin Meng 
117209b98deSBin Meng /**
1185853e133SVivek Gautam  * frees the "xhci_container_ctx" pointer passed
1195853e133SVivek Gautam  *
1205853e133SVivek Gautam  * @param ptr	pointer to "xhci_container_ctx" to be freed
1215853e133SVivek Gautam  * @return none
1225853e133SVivek Gautam  */
xhci_free_container_ctx(struct xhci_container_ctx * ctx)1235853e133SVivek Gautam static void xhci_free_container_ctx(struct xhci_container_ctx *ctx)
1245853e133SVivek Gautam {
1255853e133SVivek Gautam 	free(ctx->bytes);
1265853e133SVivek Gautam 	free(ctx);
1275853e133SVivek Gautam }
1285853e133SVivek Gautam 
1295853e133SVivek Gautam /**
1305853e133SVivek Gautam  * frees the virtual devices for "xhci_ctrl" pointer passed
1315853e133SVivek Gautam  *
1325853e133SVivek Gautam  * @param ptr	pointer to "xhci_ctrl" whose virtual devices are to be freed
1335853e133SVivek Gautam  * @return none
1345853e133SVivek Gautam  */
xhci_free_virt_devices(struct xhci_ctrl * ctrl)1355853e133SVivek Gautam static void xhci_free_virt_devices(struct xhci_ctrl *ctrl)
1365853e133SVivek Gautam {
1375853e133SVivek Gautam 	int i;
1385853e133SVivek Gautam 	int slot_id;
1395853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
1405853e133SVivek Gautam 
1415853e133SVivek Gautam 	/*
1425853e133SVivek Gautam 	 * refactored here to loop through all virt_dev
1435853e133SVivek Gautam 	 * Slot ID 0 is reserved
1445853e133SVivek Gautam 	 */
1455853e133SVivek Gautam 	for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) {
1465853e133SVivek Gautam 		virt_dev = ctrl->devs[slot_id];
1475853e133SVivek Gautam 		if (!virt_dev)
1485853e133SVivek Gautam 			continue;
1495853e133SVivek Gautam 
1505853e133SVivek Gautam 		ctrl->dcbaa->dev_context_ptrs[slot_id] = 0;
1515853e133SVivek Gautam 
1525853e133SVivek Gautam 		for (i = 0; i < 31; ++i)
1535853e133SVivek Gautam 			if (virt_dev->eps[i].ring)
1545853e133SVivek Gautam 				xhci_ring_free(virt_dev->eps[i].ring);
1555853e133SVivek Gautam 
1565853e133SVivek Gautam 		if (virt_dev->in_ctx)
1575853e133SVivek Gautam 			xhci_free_container_ctx(virt_dev->in_ctx);
1585853e133SVivek Gautam 		if (virt_dev->out_ctx)
1595853e133SVivek Gautam 			xhci_free_container_ctx(virt_dev->out_ctx);
1605853e133SVivek Gautam 
1615853e133SVivek Gautam 		free(virt_dev);
1625853e133SVivek Gautam 		/* make sure we are pointing to NULL */
1635853e133SVivek Gautam 		ctrl->devs[slot_id] = NULL;
1645853e133SVivek Gautam 	}
1655853e133SVivek Gautam }
1665853e133SVivek Gautam 
1675853e133SVivek Gautam /**
1685853e133SVivek Gautam  * frees all the memory allocated
1695853e133SVivek Gautam  *
1705853e133SVivek Gautam  * @param ptr	pointer to "xhci_ctrl" to be cleaned up
1715853e133SVivek Gautam  * @return none
1725853e133SVivek Gautam  */
xhci_cleanup(struct xhci_ctrl * ctrl)1735853e133SVivek Gautam void xhci_cleanup(struct xhci_ctrl *ctrl)
1745853e133SVivek Gautam {
1755853e133SVivek Gautam 	xhci_ring_free(ctrl->event_ring);
1765853e133SVivek Gautam 	xhci_ring_free(ctrl->cmd_ring);
177209b98deSBin Meng 	xhci_scratchpad_free(ctrl);
1785853e133SVivek Gautam 	xhci_free_virt_devices(ctrl);
1795853e133SVivek Gautam 	free(ctrl->erst.entries);
1805853e133SVivek Gautam 	free(ctrl->dcbaa);
1815853e133SVivek Gautam 	memset(ctrl, '\0', sizeof(struct xhci_ctrl));
1825853e133SVivek Gautam }
1835853e133SVivek Gautam 
1845853e133SVivek Gautam /**
1855853e133SVivek Gautam  * Malloc the aligned memory
1865853e133SVivek Gautam  *
1875853e133SVivek Gautam  * @param size	size of memory to be allocated
1885853e133SVivek Gautam  * @return allocates the memory and returns the aligned pointer
1895853e133SVivek Gautam  */
xhci_malloc(unsigned int size)1905853e133SVivek Gautam static void *xhci_malloc(unsigned int size)
1915853e133SVivek Gautam {
1925853e133SVivek Gautam 	void *ptr;
1935853e133SVivek Gautam 	size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
1945853e133SVivek Gautam 
1955853e133SVivek Gautam 	ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
1965853e133SVivek Gautam 	BUG_ON(!ptr);
1975853e133SVivek Gautam 	memset(ptr, '\0', size);
1985853e133SVivek Gautam 
199421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)ptr, size);
2005853e133SVivek Gautam 
2015853e133SVivek Gautam 	return ptr;
2025853e133SVivek Gautam }
2035853e133SVivek Gautam 
2045853e133SVivek Gautam /**
2055853e133SVivek Gautam  * Make the prev segment point to the next segment.
2065853e133SVivek Gautam  * Change the last TRB in the prev segment to be a Link TRB which points to the
2075853e133SVivek Gautam  * address of the next segment.  The caller needs to set any Link TRB
2085853e133SVivek Gautam  * related flags, such as End TRB, Toggle Cycle, and no snoop.
2095853e133SVivek Gautam  *
2105853e133SVivek Gautam  * @param prev	pointer to the previous segment
2115853e133SVivek Gautam  * @param next	pointer to the next segment
2125853e133SVivek Gautam  * @param link_trbs	flag to indicate whether to link the trbs or NOT
2135853e133SVivek Gautam  * @return none
2145853e133SVivek Gautam  */
xhci_link_segments(struct xhci_segment * prev,struct xhci_segment * next,bool link_trbs)2155853e133SVivek Gautam static void xhci_link_segments(struct xhci_segment *prev,
2165853e133SVivek Gautam 				struct xhci_segment *next, bool link_trbs)
2175853e133SVivek Gautam {
2185853e133SVivek Gautam 	u32 val;
2195853e133SVivek Gautam 	u64 val_64 = 0;
2205853e133SVivek Gautam 
2215853e133SVivek Gautam 	if (!prev || !next)
2225853e133SVivek Gautam 		return;
2235853e133SVivek Gautam 	prev->next = next;
2245853e133SVivek Gautam 	if (link_trbs) {
2255853e133SVivek Gautam 		val_64 = (uintptr_t)next->trbs;
2265853e133SVivek Gautam 		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
2275853e133SVivek Gautam 
2285853e133SVivek Gautam 		/*
2295853e133SVivek Gautam 		 * Set the last TRB in the segment to
2305853e133SVivek Gautam 		 * have a TRB type ID of Link TRB
2315853e133SVivek Gautam 		 */
2325853e133SVivek Gautam 		val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
2335853e133SVivek Gautam 		val &= ~TRB_TYPE_BITMASK;
2345853e133SVivek Gautam 		val |= (TRB_LINK << TRB_TYPE_SHIFT);
2355853e133SVivek Gautam 
2365853e133SVivek Gautam 		prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
2375853e133SVivek Gautam 	}
2385853e133SVivek Gautam }
2395853e133SVivek Gautam 
2405853e133SVivek Gautam /**
2415853e133SVivek Gautam  * Initialises the Ring's enqueue,dequeue,enq_seg pointers
2425853e133SVivek Gautam  *
2435853e133SVivek Gautam  * @param ring	pointer to the RING to be intialised
2445853e133SVivek Gautam  * @return none
2455853e133SVivek Gautam  */
xhci_initialize_ring_info(struct xhci_ring * ring)2465853e133SVivek Gautam static void xhci_initialize_ring_info(struct xhci_ring *ring)
2475853e133SVivek Gautam {
2485853e133SVivek Gautam 	/*
2495853e133SVivek Gautam 	 * The ring is empty, so the enqueue pointer == dequeue pointer
2505853e133SVivek Gautam 	 */
2515853e133SVivek Gautam 	ring->enqueue = ring->first_seg->trbs;
2525853e133SVivek Gautam 	ring->enq_seg = ring->first_seg;
2535853e133SVivek Gautam 	ring->dequeue = ring->enqueue;
2545853e133SVivek Gautam 	ring->deq_seg = ring->first_seg;
2555853e133SVivek Gautam 
2565853e133SVivek Gautam 	/*
2575853e133SVivek Gautam 	 * The ring is initialized to 0. The producer must write 1 to the
2585853e133SVivek Gautam 	 * cycle bit to handover ownership of the TRB, so PCS = 1.
2595853e133SVivek Gautam 	 * The consumer must compare CCS to the cycle bit to
2605853e133SVivek Gautam 	 * check ownership, so CCS = 1.
2615853e133SVivek Gautam 	 */
2625853e133SVivek Gautam 	ring->cycle_state = 1;
2635853e133SVivek Gautam }
2645853e133SVivek Gautam 
2655853e133SVivek Gautam /**
2665853e133SVivek Gautam  * Allocates a generic ring segment from the ring pool, sets the dma address,
2675853e133SVivek Gautam  * initializes the segment to zero, and sets the private next pointer to NULL.
2685853e133SVivek Gautam  * Section 4.11.1.1:
2695853e133SVivek Gautam  * "All components of all Command and Transfer TRBs shall be initialized to '0'"
2705853e133SVivek Gautam  *
2715853e133SVivek Gautam  * @param	none
2725853e133SVivek Gautam  * @return pointer to the newly allocated SEGMENT
2735853e133SVivek Gautam  */
xhci_segment_alloc(void)2745853e133SVivek Gautam static struct xhci_segment *xhci_segment_alloc(void)
2755853e133SVivek Gautam {
2765853e133SVivek Gautam 	struct xhci_segment *seg;
2775853e133SVivek Gautam 
2785853e133SVivek Gautam 	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
2795853e133SVivek Gautam 	BUG_ON(!seg);
2805853e133SVivek Gautam 
2815853e133SVivek Gautam 	seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE);
2825853e133SVivek Gautam 
2835853e133SVivek Gautam 	seg->next = NULL;
2845853e133SVivek Gautam 
2855853e133SVivek Gautam 	return seg;
2865853e133SVivek Gautam }
2875853e133SVivek Gautam 
2885853e133SVivek Gautam /**
2895853e133SVivek Gautam  * Create a new ring with zero or more segments.
2905853e133SVivek Gautam  * TODO: current code only uses one-time-allocated single-segment rings
2915853e133SVivek Gautam  * of 1KB anyway, so we might as well get rid of all the segment and
2925853e133SVivek Gautam  * linking code (and maybe increase the size a bit, e.g. 4KB).
2935853e133SVivek Gautam  *
2945853e133SVivek Gautam  *
2955853e133SVivek Gautam  * Link each segment together into a ring.
2965853e133SVivek Gautam  * Set the end flag and the cycle toggle bit on the last segment.
2975853e133SVivek Gautam  * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
2985853e133SVivek Gautam  *
2995853e133SVivek Gautam  * @param num_segs	number of segments in the ring
3005853e133SVivek Gautam  * @param link_trbs	flag to indicate whether to link the trbs or NOT
3015853e133SVivek Gautam  * @return pointer to the newly created RING
3025853e133SVivek Gautam  */
xhci_ring_alloc(unsigned int num_segs,bool link_trbs)3035853e133SVivek Gautam struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
3045853e133SVivek Gautam {
3055853e133SVivek Gautam 	struct xhci_ring *ring;
3065853e133SVivek Gautam 	struct xhci_segment *prev;
3075853e133SVivek Gautam 
3085853e133SVivek Gautam 	ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring));
3095853e133SVivek Gautam 	BUG_ON(!ring);
3105853e133SVivek Gautam 
3115853e133SVivek Gautam 	if (num_segs == 0)
3125853e133SVivek Gautam 		return ring;
3135853e133SVivek Gautam 
3145853e133SVivek Gautam 	ring->first_seg = xhci_segment_alloc();
3155853e133SVivek Gautam 	BUG_ON(!ring->first_seg);
3165853e133SVivek Gautam 
3175853e133SVivek Gautam 	num_segs--;
3185853e133SVivek Gautam 
3195853e133SVivek Gautam 	prev = ring->first_seg;
3205853e133SVivek Gautam 	while (num_segs > 0) {
3215853e133SVivek Gautam 		struct xhci_segment *next;
3225853e133SVivek Gautam 
3235853e133SVivek Gautam 		next = xhci_segment_alloc();
3245853e133SVivek Gautam 		BUG_ON(!next);
3255853e133SVivek Gautam 
3265853e133SVivek Gautam 		xhci_link_segments(prev, next, link_trbs);
3275853e133SVivek Gautam 
3285853e133SVivek Gautam 		prev = next;
3295853e133SVivek Gautam 		num_segs--;
3305853e133SVivek Gautam 	}
3315853e133SVivek Gautam 	xhci_link_segments(prev, ring->first_seg, link_trbs);
3325853e133SVivek Gautam 	if (link_trbs) {
3335853e133SVivek Gautam 		/* See section 4.9.2.1 and 6.4.4.1 */
3345853e133SVivek Gautam 		prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
3355853e133SVivek Gautam 					cpu_to_le32(LINK_TOGGLE);
3365853e133SVivek Gautam 	}
3375853e133SVivek Gautam 	xhci_initialize_ring_info(ring);
3385853e133SVivek Gautam 
3395853e133SVivek Gautam 	return ring;
3405853e133SVivek Gautam }
3415853e133SVivek Gautam 
3425853e133SVivek Gautam /**
343209b98deSBin Meng  * Set up the scratchpad buffer array and scratchpad buffers
344209b98deSBin Meng  *
345209b98deSBin Meng  * @ctrl	host controller data structure
346209b98deSBin Meng  * @return	-ENOMEM if buffer allocation fails, 0 on success
347209b98deSBin Meng  */
xhci_scratchpad_alloc(struct xhci_ctrl * ctrl)348209b98deSBin Meng static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
349209b98deSBin Meng {
350209b98deSBin Meng 	struct xhci_hccr *hccr = ctrl->hccr;
351209b98deSBin Meng 	struct xhci_hcor *hcor = ctrl->hcor;
352209b98deSBin Meng 	struct xhci_scratchpad *scratchpad;
353209b98deSBin Meng 	int num_sp;
354209b98deSBin Meng 	uint32_t page_size;
355209b98deSBin Meng 	void *buf;
356209b98deSBin Meng 	int i;
357209b98deSBin Meng 
358209b98deSBin Meng 	num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
359209b98deSBin Meng 	if (!num_sp)
360209b98deSBin Meng 		return 0;
361209b98deSBin Meng 
362209b98deSBin Meng 	scratchpad = malloc(sizeof(*scratchpad));
363209b98deSBin Meng 	if (!scratchpad)
364209b98deSBin Meng 		goto fail_sp;
365209b98deSBin Meng 	ctrl->scratchpad = scratchpad;
366209b98deSBin Meng 
367209b98deSBin Meng 	scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
368209b98deSBin Meng 	if (!scratchpad->sp_array)
369209b98deSBin Meng 		goto fail_sp2;
370209b98deSBin Meng 	ctrl->dcbaa->dev_context_ptrs[0] =
371209b98deSBin Meng 		cpu_to_le64((uintptr_t)scratchpad->sp_array);
372209b98deSBin Meng 
3733403786cSYe Li 	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
3743403786cSYe Li 		sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
3753403786cSYe Li 
376209b98deSBin Meng 	page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
377209b98deSBin Meng 	for (i = 0; i < 16; i++) {
378209b98deSBin Meng 		if ((0x1 & page_size) != 0)
379209b98deSBin Meng 			break;
380209b98deSBin Meng 		page_size = page_size >> 1;
381209b98deSBin Meng 	}
382209b98deSBin Meng 	BUG_ON(i == 16);
383209b98deSBin Meng 
384209b98deSBin Meng 	page_size = 1 << (i + 12);
385209b98deSBin Meng 	buf = memalign(page_size, num_sp * page_size);
386209b98deSBin Meng 	if (!buf)
387209b98deSBin Meng 		goto fail_sp3;
388209b98deSBin Meng 	memset(buf, '\0', num_sp * page_size);
389209b98deSBin Meng 	xhci_flush_cache((uintptr_t)buf, num_sp * page_size);
390209b98deSBin Meng 
391209b98deSBin Meng 	for (i = 0; i < num_sp; i++) {
392209b98deSBin Meng 		uintptr_t ptr = (uintptr_t)buf + i * page_size;
393209b98deSBin Meng 		scratchpad->sp_array[i] = cpu_to_le64(ptr);
394209b98deSBin Meng 	}
395209b98deSBin Meng 
396209b98deSBin Meng 	return 0;
397209b98deSBin Meng 
398209b98deSBin Meng fail_sp3:
399209b98deSBin Meng 	free(scratchpad->sp_array);
400209b98deSBin Meng 
401209b98deSBin Meng fail_sp2:
402209b98deSBin Meng 	free(scratchpad);
403209b98deSBin Meng 	ctrl->scratchpad = NULL;
404209b98deSBin Meng 
405209b98deSBin Meng fail_sp:
406209b98deSBin Meng 	return -ENOMEM;
407209b98deSBin Meng }
408209b98deSBin Meng 
409209b98deSBin Meng /**
4105853e133SVivek Gautam  * Allocates the Container context
4115853e133SVivek Gautam  *
4125853e133SVivek Gautam  * @param ctrl	Host controller data structure
4135853e133SVivek Gautam  * @param type type of XHCI Container Context
4145853e133SVivek Gautam  * @return NULL if failed else pointer to the context on success
4155853e133SVivek Gautam  */
4165853e133SVivek Gautam static struct xhci_container_ctx
xhci_alloc_container_ctx(struct xhci_ctrl * ctrl,int type)4175853e133SVivek Gautam 		*xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
4185853e133SVivek Gautam {
4195853e133SVivek Gautam 	struct xhci_container_ctx *ctx;
4205853e133SVivek Gautam 
4215853e133SVivek Gautam 	ctx = (struct xhci_container_ctx *)
4225853e133SVivek Gautam 		malloc(sizeof(struct xhci_container_ctx));
4235853e133SVivek Gautam 	BUG_ON(!ctx);
4245853e133SVivek Gautam 
4255853e133SVivek Gautam 	BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
4265853e133SVivek Gautam 	ctx->type = type;
4275853e133SVivek Gautam 	ctx->size = (MAX_EP_CTX_NUM + 1) *
4285853e133SVivek Gautam 			CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
4295853e133SVivek Gautam 	if (type == XHCI_CTX_TYPE_INPUT)
4305853e133SVivek Gautam 		ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
4315853e133SVivek Gautam 
4325853e133SVivek Gautam 	ctx->bytes = (u8 *)xhci_malloc(ctx->size);
4335853e133SVivek Gautam 
4345853e133SVivek Gautam 	return ctx;
4355853e133SVivek Gautam }
4365853e133SVivek Gautam 
4375853e133SVivek Gautam /**
4385853e133SVivek Gautam  * Allocating virtual device
4395853e133SVivek Gautam  *
4405853e133SVivek Gautam  * @param udev	pointer to USB deivce structure
4415853e133SVivek Gautam  * @return 0 on success else -1 on failure
4425853e133SVivek Gautam  */
xhci_alloc_virt_device(struct xhci_ctrl * ctrl,unsigned int slot_id)4437e0c5ee8SSimon Glass int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
4445853e133SVivek Gautam {
4455853e133SVivek Gautam 	u64 byte_64 = 0;
4465853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
4475853e133SVivek Gautam 
4485853e133SVivek Gautam 	/* Slot ID 0 is reserved */
4495853e133SVivek Gautam 	if (ctrl->devs[slot_id]) {
4505853e133SVivek Gautam 		printf("Virt dev for slot[%d] already allocated\n", slot_id);
4515853e133SVivek Gautam 		return -EEXIST;
4525853e133SVivek Gautam 	}
4535853e133SVivek Gautam 
4545853e133SVivek Gautam 	ctrl->devs[slot_id] = (struct xhci_virt_device *)
4555853e133SVivek Gautam 					malloc(sizeof(struct xhci_virt_device));
4565853e133SVivek Gautam 
4575853e133SVivek Gautam 	if (!ctrl->devs[slot_id]) {
4585853e133SVivek Gautam 		puts("Failed to allocate virtual device\n");
4595853e133SVivek Gautam 		return -ENOMEM;
4605853e133SVivek Gautam 	}
4615853e133SVivek Gautam 
4625853e133SVivek Gautam 	memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
4635853e133SVivek Gautam 	virt_dev = ctrl->devs[slot_id];
4645853e133SVivek Gautam 
4655853e133SVivek Gautam 	/* Allocate the (output) device context that will be used in the HC. */
4665853e133SVivek Gautam 	virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
4675853e133SVivek Gautam 					XHCI_CTX_TYPE_DEVICE);
4685853e133SVivek Gautam 	if (!virt_dev->out_ctx) {
4695853e133SVivek Gautam 		puts("Failed to allocate out context for virt dev\n");
4705853e133SVivek Gautam 		return -ENOMEM;
4715853e133SVivek Gautam 	}
4725853e133SVivek Gautam 
4735853e133SVivek Gautam 	/* Allocate the (input) device context for address device command */
4745853e133SVivek Gautam 	virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
4755853e133SVivek Gautam 					XHCI_CTX_TYPE_INPUT);
4765853e133SVivek Gautam 	if (!virt_dev->in_ctx) {
4775853e133SVivek Gautam 		puts("Failed to allocate in context for virt dev\n");
4785853e133SVivek Gautam 		return -ENOMEM;
4795853e133SVivek Gautam 	}
4805853e133SVivek Gautam 
4815853e133SVivek Gautam 	/* Allocate endpoint 0 ring */
4825853e133SVivek Gautam 	virt_dev->eps[0].ring = xhci_ring_alloc(1, true);
4835853e133SVivek Gautam 
4845853e133SVivek Gautam 	byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
4855853e133SVivek Gautam 
4865853e133SVivek Gautam 	/* Point to output device context in dcbaa. */
4875853e133SVivek Gautam 	ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
4885853e133SVivek Gautam 
489421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
4905853e133SVivek Gautam 			 sizeof(__le64));
4915853e133SVivek Gautam 	return 0;
4925853e133SVivek Gautam }
4935853e133SVivek Gautam 
4945853e133SVivek Gautam /**
4955853e133SVivek Gautam  * Allocates the necessary data structures
4965853e133SVivek Gautam  * for XHCI host controller
4975853e133SVivek Gautam  *
4985853e133SVivek Gautam  * @param ctrl	Host controller data structure
4995853e133SVivek Gautam  * @param hccr	pointer to HOST Controller Control Registers
5005853e133SVivek Gautam  * @param hcor	pointer to HOST Controller Operational Registers
5015853e133SVivek Gautam  * @return 0 if successful else -1 on failure
5025853e133SVivek Gautam  */
xhci_mem_init(struct xhci_ctrl * ctrl,struct xhci_hccr * hccr,struct xhci_hcor * hcor)5035853e133SVivek Gautam int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
5045853e133SVivek Gautam 					struct xhci_hcor *hcor)
5055853e133SVivek Gautam {
5065853e133SVivek Gautam 	uint64_t val_64;
5075853e133SVivek Gautam 	uint64_t trb_64;
5085853e133SVivek Gautam 	uint32_t val;
5095853e133SVivek Gautam 	unsigned long deq;
5105853e133SVivek Gautam 	int i;
5115853e133SVivek Gautam 	struct xhci_segment *seg;
5125853e133SVivek Gautam 
5135853e133SVivek Gautam 	/* DCBAA initialization */
5145853e133SVivek Gautam 	ctrl->dcbaa = (struct xhci_device_context_array *)
5155853e133SVivek Gautam 			xhci_malloc(sizeof(struct xhci_device_context_array));
5165853e133SVivek Gautam 	if (ctrl->dcbaa == NULL) {
5175853e133SVivek Gautam 		puts("unable to allocate DCBA\n");
5185853e133SVivek Gautam 		return -ENOMEM;
5195853e133SVivek Gautam 	}
5205853e133SVivek Gautam 
5215853e133SVivek Gautam 	val_64 = (uintptr_t)ctrl->dcbaa;
5225853e133SVivek Gautam 	/* Set the pointer in DCBAA register */
5235853e133SVivek Gautam 	xhci_writeq(&hcor->or_dcbaap, val_64);
5245853e133SVivek Gautam 
5255853e133SVivek Gautam 	/* Command ring control pointer register initialization */
5265853e133SVivek Gautam 	ctrl->cmd_ring = xhci_ring_alloc(1, true);
5275853e133SVivek Gautam 
5285853e133SVivek Gautam 	/* Set the address in the Command Ring Control register */
5295853e133SVivek Gautam 	trb_64 = (uintptr_t)ctrl->cmd_ring->first_seg->trbs;
5305853e133SVivek Gautam 	val_64 = xhci_readq(&hcor->or_crcr);
5315853e133SVivek Gautam 	val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
5325853e133SVivek Gautam 		(trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
5335853e133SVivek Gautam 		ctrl->cmd_ring->cycle_state;
5345853e133SVivek Gautam 	xhci_writeq(&hcor->or_crcr, val_64);
5355853e133SVivek Gautam 
5365853e133SVivek Gautam 	/* write the address of db register */
5375853e133SVivek Gautam 	val = xhci_readl(&hccr->cr_dboff);
5385853e133SVivek Gautam 	val &= DBOFF_MASK;
5395853e133SVivek Gautam 	ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
5405853e133SVivek Gautam 
5415853e133SVivek Gautam 	/* write the address of runtime register */
5425853e133SVivek Gautam 	val = xhci_readl(&hccr->cr_rtsoff);
5435853e133SVivek Gautam 	val &= RTSOFF_MASK;
5445853e133SVivek Gautam 	ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
5455853e133SVivek Gautam 
5465853e133SVivek Gautam 	/* writting the address of ir_set structure */
5475853e133SVivek Gautam 	ctrl->ir_set = &ctrl->run_regs->ir_set[0];
5485853e133SVivek Gautam 
5495853e133SVivek Gautam 	/* Event ring does not maintain link TRB */
5505853e133SVivek Gautam 	ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
5515853e133SVivek Gautam 	ctrl->erst.entries = (struct xhci_erst_entry *)
5525853e133SVivek Gautam 		xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
5535853e133SVivek Gautam 
5545853e133SVivek Gautam 	ctrl->erst.num_entries = ERST_NUM_SEGS;
5555853e133SVivek Gautam 
5565853e133SVivek Gautam 	for (val = 0, seg = ctrl->event_ring->first_seg;
5575853e133SVivek Gautam 			val < ERST_NUM_SEGS;
5585853e133SVivek Gautam 			val++) {
5595853e133SVivek Gautam 		trb_64 = 0;
5605853e133SVivek Gautam 		trb_64 = (uintptr_t)seg->trbs;
5615853e133SVivek Gautam 		struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
5625853e133SVivek Gautam 		xhci_writeq(&entry->seg_addr, trb_64);
5635853e133SVivek Gautam 		entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
5645853e133SVivek Gautam 		entry->rsvd = 0;
5655853e133SVivek Gautam 		seg = seg->next;
5665853e133SVivek Gautam 	}
567421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)ctrl->erst.entries,
5685853e133SVivek Gautam 			 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
5695853e133SVivek Gautam 
5705853e133SVivek Gautam 	deq = (unsigned long)ctrl->event_ring->dequeue;
5715853e133SVivek Gautam 
5725853e133SVivek Gautam 	/* Update HC event ring dequeue pointer */
5735853e133SVivek Gautam 	xhci_writeq(&ctrl->ir_set->erst_dequeue,
5745853e133SVivek Gautam 				(u64)deq & (u64)~ERST_PTR_MASK);
5755853e133SVivek Gautam 
5765853e133SVivek Gautam 	/* set ERST count with the number of entries in the segment table */
5775853e133SVivek Gautam 	val = xhci_readl(&ctrl->ir_set->erst_size);
5785853e133SVivek Gautam 	val &= ERST_SIZE_MASK;
5795853e133SVivek Gautam 	val |= ERST_NUM_SEGS;
5805853e133SVivek Gautam 	xhci_writel(&ctrl->ir_set->erst_size, val);
5815853e133SVivek Gautam 
5825853e133SVivek Gautam 	/* this is the event ring segment table pointer */
5835853e133SVivek Gautam 	val_64 = xhci_readq(&ctrl->ir_set->erst_base);
5845853e133SVivek Gautam 	val_64 &= ERST_PTR_MASK;
585421a5a0cSSergey Temerkhanov 	val_64 |= ((uintptr_t)(ctrl->erst.entries) & ~ERST_PTR_MASK);
5865853e133SVivek Gautam 
5875853e133SVivek Gautam 	xhci_writeq(&ctrl->ir_set->erst_base, val_64);
5885853e133SVivek Gautam 
589209b98deSBin Meng 	/* set up the scratchpad buffer array and scratchpad buffers */
590209b98deSBin Meng 	xhci_scratchpad_alloc(ctrl);
591209b98deSBin Meng 
5925853e133SVivek Gautam 	/* initializing the virtual devices to NULL */
5935853e133SVivek Gautam 	for (i = 0; i < MAX_HC_SLOTS; ++i)
5945853e133SVivek Gautam 		ctrl->devs[i] = NULL;
5955853e133SVivek Gautam 
5965853e133SVivek Gautam 	/*
5975853e133SVivek Gautam 	 * Just Zero'ing this register completely,
5985853e133SVivek Gautam 	 * or some spurious Device Notification Events
5995853e133SVivek Gautam 	 * might screw things here.
6005853e133SVivek Gautam 	 */
6015853e133SVivek Gautam 	xhci_writel(&hcor->or_dnctrl, 0x0);
6025853e133SVivek Gautam 
6035853e133SVivek Gautam 	return 0;
6045853e133SVivek Gautam }
6055853e133SVivek Gautam 
6065853e133SVivek Gautam /**
6075853e133SVivek Gautam  * Give the input control context for the passed container context
6085853e133SVivek Gautam  *
6095853e133SVivek Gautam  * @param ctx	pointer to the context
6105853e133SVivek Gautam  * @return pointer to the Input control context data
6115853e133SVivek Gautam  */
6125853e133SVivek Gautam struct xhci_input_control_ctx
xhci_get_input_control_ctx(struct xhci_container_ctx * ctx)6135853e133SVivek Gautam 		*xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
6145853e133SVivek Gautam {
6155853e133SVivek Gautam 	BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
6165853e133SVivek Gautam 	return (struct xhci_input_control_ctx *)ctx->bytes;
6175853e133SVivek Gautam }
6185853e133SVivek Gautam 
6195853e133SVivek Gautam /**
6205853e133SVivek Gautam  * Give the slot context for the passed container context
6215853e133SVivek Gautam  *
6225853e133SVivek Gautam  * @param ctrl	Host controller data structure
6235853e133SVivek Gautam  * @param ctx	pointer to the context
6245853e133SVivek Gautam  * @return pointer to the slot control context data
6255853e133SVivek Gautam  */
xhci_get_slot_ctx(struct xhci_ctrl * ctrl,struct xhci_container_ctx * ctx)6265853e133SVivek Gautam struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
6275853e133SVivek Gautam 				struct xhci_container_ctx *ctx)
6285853e133SVivek Gautam {
6295853e133SVivek Gautam 	if (ctx->type == XHCI_CTX_TYPE_DEVICE)
6305853e133SVivek Gautam 		return (struct xhci_slot_ctx *)ctx->bytes;
6315853e133SVivek Gautam 
6325853e133SVivek Gautam 	return (struct xhci_slot_ctx *)
6335853e133SVivek Gautam 		(ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)));
6345853e133SVivek Gautam }
6355853e133SVivek Gautam 
6365853e133SVivek Gautam /**
6375853e133SVivek Gautam  * Gets the EP context from based on the ep_index
6385853e133SVivek Gautam  *
6395853e133SVivek Gautam  * @param ctrl	Host controller data structure
6405853e133SVivek Gautam  * @param ctx	context container
6415853e133SVivek Gautam  * @param ep_index	index of the endpoint
6425853e133SVivek Gautam  * @return pointer to the End point context
6435853e133SVivek Gautam  */
xhci_get_ep_ctx(struct xhci_ctrl * ctrl,struct xhci_container_ctx * ctx,unsigned int ep_index)6445853e133SVivek Gautam struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
6455853e133SVivek Gautam 				    struct xhci_container_ctx *ctx,
6465853e133SVivek Gautam 				    unsigned int ep_index)
6475853e133SVivek Gautam {
6485853e133SVivek Gautam 	/* increment ep index by offset of start of ep ctx array */
6495853e133SVivek Gautam 	ep_index++;
6505853e133SVivek Gautam 	if (ctx->type == XHCI_CTX_TYPE_INPUT)
6515853e133SVivek Gautam 		ep_index++;
6525853e133SVivek Gautam 
6535853e133SVivek Gautam 	return (struct xhci_ep_ctx *)
6545853e133SVivek Gautam 		(ctx->bytes +
6555853e133SVivek Gautam 		(ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))));
6565853e133SVivek Gautam }
6575853e133SVivek Gautam 
6585853e133SVivek Gautam /**
6595853e133SVivek Gautam  * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
6605853e133SVivek Gautam  * Useful when you want to change one particular aspect of the endpoint
6615853e133SVivek Gautam  * and then issue a configure endpoint command.
6625853e133SVivek Gautam  *
6635853e133SVivek Gautam  * @param ctrl	Host controller data structure
6645853e133SVivek Gautam  * @param in_ctx contains the input context
6655853e133SVivek Gautam  * @param out_ctx contains the input context
6665853e133SVivek Gautam  * @param ep_index index of the end point
6675853e133SVivek Gautam  * @return none
6685853e133SVivek Gautam  */
xhci_endpoint_copy(struct xhci_ctrl * ctrl,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx,unsigned int ep_index)6695853e133SVivek Gautam void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
6705853e133SVivek Gautam 			struct xhci_container_ctx *in_ctx,
6715853e133SVivek Gautam 			struct xhci_container_ctx *out_ctx,
6725853e133SVivek Gautam 			unsigned int ep_index)
6735853e133SVivek Gautam {
6745853e133SVivek Gautam 	struct xhci_ep_ctx *out_ep_ctx;
6755853e133SVivek Gautam 	struct xhci_ep_ctx *in_ep_ctx;
6765853e133SVivek Gautam 
6775853e133SVivek Gautam 	out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
6785853e133SVivek Gautam 	in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
6795853e133SVivek Gautam 
6805853e133SVivek Gautam 	in_ep_ctx->ep_info = out_ep_ctx->ep_info;
6815853e133SVivek Gautam 	in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
6825853e133SVivek Gautam 	in_ep_ctx->deq = out_ep_ctx->deq;
6835853e133SVivek Gautam 	in_ep_ctx->tx_info = out_ep_ctx->tx_info;
6845853e133SVivek Gautam }
6855853e133SVivek Gautam 
6865853e133SVivek Gautam /**
6875853e133SVivek Gautam  * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
6885853e133SVivek Gautam  * Useful when you want to change one particular aspect of the endpoint
6895853e133SVivek Gautam  * and then issue a configure endpoint command.
6905853e133SVivek Gautam  * Only the context entries field matters, but
6915853e133SVivek Gautam  * we'll copy the whole thing anyway.
6925853e133SVivek Gautam  *
6935853e133SVivek Gautam  * @param ctrl	Host controller data structure
6945853e133SVivek Gautam  * @param in_ctx contains the inpout context
6955853e133SVivek Gautam  * @param out_ctx contains the inpout context
6965853e133SVivek Gautam  * @return none
6975853e133SVivek Gautam  */
xhci_slot_copy(struct xhci_ctrl * ctrl,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx)6985853e133SVivek Gautam void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
6995853e133SVivek Gautam 					struct xhci_container_ctx *out_ctx)
7005853e133SVivek Gautam {
7015853e133SVivek Gautam 	struct xhci_slot_ctx *in_slot_ctx;
7025853e133SVivek Gautam 	struct xhci_slot_ctx *out_slot_ctx;
7035853e133SVivek Gautam 
7045853e133SVivek Gautam 	in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
7055853e133SVivek Gautam 	out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
7065853e133SVivek Gautam 
7075853e133SVivek Gautam 	in_slot_ctx->dev_info = out_slot_ctx->dev_info;
7085853e133SVivek Gautam 	in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
7095853e133SVivek Gautam 	in_slot_ctx->tt_info = out_slot_ctx->tt_info;
7105853e133SVivek Gautam 	in_slot_ctx->dev_state = out_slot_ctx->dev_state;
7115853e133SVivek Gautam }
7125853e133SVivek Gautam 
7135853e133SVivek Gautam /**
7145853e133SVivek Gautam  * Setup an xHCI virtual device for a Set Address command
7155853e133SVivek Gautam  *
7165853e133SVivek Gautam  * @param udev pointer to the Device Data Structure
7175853e133SVivek Gautam  * @return returns negative value on failure else 0 on success
7185853e133SVivek Gautam  */
xhci_setup_addressable_virt_dev(struct xhci_ctrl * ctrl,struct usb_device * udev,int hop_portnr)719daec4691SBin Meng void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
720daec4691SBin Meng 				     struct usb_device *udev, int hop_portnr)
7215853e133SVivek Gautam {
7225853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
7235853e133SVivek Gautam 	struct xhci_ep_ctx *ep0_ctx;
7245853e133SVivek Gautam 	struct xhci_slot_ctx *slot_ctx;
7255853e133SVivek Gautam 	u32 port_num = 0;
7265853e133SVivek Gautam 	u64 trb_64 = 0;
727daec4691SBin Meng 	int slot_id = udev->slot_id;
728daec4691SBin Meng 	int speed = udev->speed;
729493b8dd0SBin Meng 	int route = 0;
7303739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
731493b8dd0SBin Meng 	struct usb_device *dev = udev;
732493b8dd0SBin Meng 	struct usb_hub_device *hub;
733493b8dd0SBin Meng #endif
7345853e133SVivek Gautam 
7355dd75e3bSSimon Glass 	virt_dev = ctrl->devs[slot_id];
7365853e133SVivek Gautam 
7375853e133SVivek Gautam 	BUG_ON(!virt_dev);
7385853e133SVivek Gautam 
7395853e133SVivek Gautam 	/* Extract the EP0 and Slot Ctrl */
7405853e133SVivek Gautam 	ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
7415853e133SVivek Gautam 	slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
7425853e133SVivek Gautam 
7435853e133SVivek Gautam 	/* Only the control endpoint is valid - one endpoint context */
744493b8dd0SBin Meng 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
745493b8dd0SBin Meng 
7463739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
747493b8dd0SBin Meng 	/* Calculate the route string for this device */
748493b8dd0SBin Meng 	port_num = dev->portnr;
749493b8dd0SBin Meng 	while (!usb_hub_is_root_hub(dev->dev)) {
750493b8dd0SBin Meng 		hub = dev_get_uclass_priv(dev->dev);
751493b8dd0SBin Meng 		/*
752493b8dd0SBin Meng 		 * Each hub in the topology is expected to have no more than
753493b8dd0SBin Meng 		 * 15 ports in order for the route string of a device to be
754493b8dd0SBin Meng 		 * unique. SuperSpeed hubs are restricted to only having 15
755493b8dd0SBin Meng 		 * ports, but FS/LS/HS hubs are not. The xHCI specification
756493b8dd0SBin Meng 		 * says that if the port number the device is greater than 15,
757493b8dd0SBin Meng 		 * that portion of the route string shall be set to 15.
758493b8dd0SBin Meng 		 */
759493b8dd0SBin Meng 		if (port_num > 15)
760493b8dd0SBin Meng 			port_num = 15;
761493b8dd0SBin Meng 		route |= port_num << (hub->hub_depth * 4);
762493b8dd0SBin Meng 		dev = dev_get_parent_priv(dev->dev);
763493b8dd0SBin Meng 		port_num = dev->portnr;
764493b8dd0SBin Meng 		dev = dev_get_parent_priv(dev->dev->parent);
765493b8dd0SBin Meng 	}
766493b8dd0SBin Meng 
767493b8dd0SBin Meng 	debug("route string %x\n", route);
768493b8dd0SBin Meng #endif
769493b8dd0SBin Meng 	slot_ctx->dev_info |= route;
7705853e133SVivek Gautam 
7715dd75e3bSSimon Glass 	switch (speed) {
7725853e133SVivek Gautam 	case USB_SPEED_SUPER:
7735853e133SVivek Gautam 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
7745853e133SVivek Gautam 		break;
7755853e133SVivek Gautam 	case USB_SPEED_HIGH:
7765853e133SVivek Gautam 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
7775853e133SVivek Gautam 		break;
7785853e133SVivek Gautam 	case USB_SPEED_FULL:
7795853e133SVivek Gautam 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
7805853e133SVivek Gautam 		break;
7815853e133SVivek Gautam 	case USB_SPEED_LOW:
7825853e133SVivek Gautam 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
7835853e133SVivek Gautam 		break;
7845853e133SVivek Gautam 	default:
7855853e133SVivek Gautam 		/* Speed was set earlier, this shouldn't happen. */
7865853e133SVivek Gautam 		BUG();
7875853e133SVivek Gautam 	}
7885853e133SVivek Gautam 
7893739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
79078e30987SBin Meng 	/* Set up TT fields to support FS/LS devices */
79178e30987SBin Meng 	if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
792d17dd8c5SBin Meng 		struct udevice *parent = udev->dev;
793d17dd8c5SBin Meng 
794d17dd8c5SBin Meng 		dev = udev;
795d17dd8c5SBin Meng 		do {
796d17dd8c5SBin Meng 			port_num = dev->portnr;
797d17dd8c5SBin Meng 			dev = dev_get_parent_priv(parent);
798d17dd8c5SBin Meng 			if (usb_hub_is_root_hub(dev->dev))
799d17dd8c5SBin Meng 				break;
800d17dd8c5SBin Meng 			parent = dev->dev->parent;
801d17dd8c5SBin Meng 		} while (dev->speed != USB_SPEED_HIGH);
802d17dd8c5SBin Meng 
803d17dd8c5SBin Meng 		if (!usb_hub_is_root_hub(dev->dev)) {
804d17dd8c5SBin Meng 			hub = dev_get_uclass_priv(dev->dev);
80578e30987SBin Meng 			if (hub->tt.multi)
80678e30987SBin Meng 				slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
807d17dd8c5SBin Meng 			slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
80878e30987SBin Meng 			slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
80978e30987SBin Meng 		}
81078e30987SBin Meng 	}
81178e30987SBin Meng #endif
81278e30987SBin Meng 
8135dd75e3bSSimon Glass 	port_num = hop_portnr;
8145853e133SVivek Gautam 	debug("port_num = %d\n", port_num);
8155853e133SVivek Gautam 
8165853e133SVivek Gautam 	slot_ctx->dev_info2 |=
8175853e133SVivek Gautam 			cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
8185853e133SVivek Gautam 				ROOT_HUB_PORT_SHIFT));
8195853e133SVivek Gautam 
8205853e133SVivek Gautam 	/* Step 4 - ring already allocated */
8215853e133SVivek Gautam 	/* Step 5 */
8225853e133SVivek Gautam 	ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT);
8235dd75e3bSSimon Glass 	debug("SPEED = %d\n", speed);
8245853e133SVivek Gautam 
8255dd75e3bSSimon Glass 	switch (speed) {
8265853e133SVivek Gautam 	case USB_SPEED_SUPER:
8275853e133SVivek Gautam 		ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) <<
8285853e133SVivek Gautam 					MAX_PACKET_SHIFT));
8295853e133SVivek Gautam 		debug("Setting Packet size = 512bytes\n");
8305853e133SVivek Gautam 		break;
8315853e133SVivek Gautam 	case USB_SPEED_HIGH:
8325853e133SVivek Gautam 	/* USB core guesses at a 64-byte max packet first for FS devices */
8335853e133SVivek Gautam 	case USB_SPEED_FULL:
8345853e133SVivek Gautam 		ep0_ctx->ep_info2 |= cpu_to_le32(((64 & MAX_PACKET_MASK) <<
8355853e133SVivek Gautam 					MAX_PACKET_SHIFT));
8365853e133SVivek Gautam 		debug("Setting Packet size = 64bytes\n");
8375853e133SVivek Gautam 		break;
8385853e133SVivek Gautam 	case USB_SPEED_LOW:
8395853e133SVivek Gautam 		ep0_ctx->ep_info2 |= cpu_to_le32(((8 & MAX_PACKET_MASK) <<
8405853e133SVivek Gautam 					MAX_PACKET_SHIFT));
8415853e133SVivek Gautam 		debug("Setting Packet size = 8bytes\n");
8425853e133SVivek Gautam 		break;
8435853e133SVivek Gautam 	default:
8445853e133SVivek Gautam 		/* New speed? */
8455853e133SVivek Gautam 		BUG();
8465853e133SVivek Gautam 	}
8475853e133SVivek Gautam 
8485853e133SVivek Gautam 	/* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
8495853e133SVivek Gautam 	ep0_ctx->ep_info2 |=
8505853e133SVivek Gautam 			cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) |
8515853e133SVivek Gautam 			((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT));
8525853e133SVivek Gautam 
8535853e133SVivek Gautam 	trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs;
8545853e133SVivek Gautam 	ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
8555853e133SVivek Gautam 
856f018b53dSBin Meng 	/*
857f018b53dSBin Meng 	 * xHCI spec 6.2.3:
858f018b53dSBin Meng 	 * software shall set 'Average TRB Length' to 8 for control endpoints.
859f018b53dSBin Meng 	 */
860f018b53dSBin Meng 	ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
861f018b53dSBin Meng 
8625853e133SVivek Gautam 	/* Steps 7 and 8 were done in xhci_alloc_virt_device() */
8635853e133SVivek Gautam 
864421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
865421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
8665853e133SVivek Gautam }
867