xref: /OK3568_Linux_fs/u-boot/drivers/usb/host/xhci-mem.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * USB HOST XHCI Controller stack
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Based on xHCI host controller driver in linux-kernel
5*4882a593Smuzhiyun  * by Sarah Sharp.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2008 Intel Corp.
8*4882a593Smuzhiyun  * Author: Sarah Sharp
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Copyright (C) 2013 Samsung Electronics Co.Ltd
11*4882a593Smuzhiyun  * Authors: Vivek Gautam <gautam.vivek@samsung.com>
12*4882a593Smuzhiyun  *	    Vikas Sajjan <vikas.sajjan@samsung.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <common.h>
18*4882a593Smuzhiyun #include <dm.h>
19*4882a593Smuzhiyun #include <asm/byteorder.h>
20*4882a593Smuzhiyun #include <usb.h>
21*4882a593Smuzhiyun #include <malloc.h>
22*4882a593Smuzhiyun #include <asm/cache.h>
23*4882a593Smuzhiyun #include <linux/errno.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <usb/xhci.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define CACHELINE_SIZE		CONFIG_SYS_CACHELINE_SIZE
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun  * flushes the address passed till the length
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * @param addr	pointer to memory region to be flushed
32*4882a593Smuzhiyun  * @param len	the length of the cache line to be flushed
33*4882a593Smuzhiyun  * @return none
34*4882a593Smuzhiyun  */
xhci_flush_cache(uintptr_t addr,u32 len)35*4882a593Smuzhiyun void xhci_flush_cache(uintptr_t addr, u32 len)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	BUG_ON((void *)addr == NULL || len == 0);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	flush_dcache_range(addr & ~(CACHELINE_SIZE - 1),
40*4882a593Smuzhiyun 				ALIGN(addr + len, CACHELINE_SIZE));
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun  * invalidates the address passed till the length
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * @param addr	pointer to memory region to be invalidates
47*4882a593Smuzhiyun  * @param len	the length of the cache line to be invalidated
48*4882a593Smuzhiyun  * @return none
49*4882a593Smuzhiyun  */
xhci_inval_cache(uintptr_t addr,u32 len)50*4882a593Smuzhiyun void xhci_inval_cache(uintptr_t addr, u32 len)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	BUG_ON((void *)addr == NULL || len == 0);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	invalidate_dcache_range(addr & ~(CACHELINE_SIZE - 1),
55*4882a593Smuzhiyun 				ALIGN(addr + len, CACHELINE_SIZE));
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun  * frees the "segment" pointer passed
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * @param ptr	pointer to "segement" to be freed
63*4882a593Smuzhiyun  * @return none
64*4882a593Smuzhiyun  */
xhci_segment_free(struct xhci_segment * seg)65*4882a593Smuzhiyun static void xhci_segment_free(struct xhci_segment *seg)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	free(seg->trbs);
68*4882a593Smuzhiyun 	seg->trbs = NULL;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	free(seg);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * frees the "ring" pointer passed
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * @param ptr	pointer to "ring" to be freed
77*4882a593Smuzhiyun  * @return none
78*4882a593Smuzhiyun  */
xhci_ring_free(struct xhci_ring * ring)79*4882a593Smuzhiyun static void xhci_ring_free(struct xhci_ring *ring)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	struct xhci_segment *seg;
82*4882a593Smuzhiyun 	struct xhci_segment *first_seg;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	BUG_ON(!ring);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	first_seg = ring->first_seg;
87*4882a593Smuzhiyun 	seg = first_seg->next;
88*4882a593Smuzhiyun 	while (seg != first_seg) {
89*4882a593Smuzhiyun 		struct xhci_segment *next = seg->next;
90*4882a593Smuzhiyun 		xhci_segment_free(seg);
91*4882a593Smuzhiyun 		seg = next;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 	xhci_segment_free(first_seg);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	free(ring);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * Free the scratchpad buffer array and scratchpad buffers
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * @ctrl	host controller data structure
102*4882a593Smuzhiyun  * @return	none
103*4882a593Smuzhiyun  */
xhci_scratchpad_free(struct xhci_ctrl * ctrl)104*4882a593Smuzhiyun static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	if (!ctrl->scratchpad)
107*4882a593Smuzhiyun 		return;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	ctrl->dcbaa->dev_context_ptrs[0] = 0;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
112*4882a593Smuzhiyun 	free(ctrl->scratchpad->sp_array);
113*4882a593Smuzhiyun 	free(ctrl->scratchpad);
114*4882a593Smuzhiyun 	ctrl->scratchpad = NULL;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun  * frees the "xhci_container_ctx" pointer passed
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * @param ptr	pointer to "xhci_container_ctx" to be freed
121*4882a593Smuzhiyun  * @return none
122*4882a593Smuzhiyun  */
xhci_free_container_ctx(struct xhci_container_ctx * ctx)123*4882a593Smuzhiyun static void xhci_free_container_ctx(struct xhci_container_ctx *ctx)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	free(ctx->bytes);
126*4882a593Smuzhiyun 	free(ctx);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /**
130*4882a593Smuzhiyun  * frees the virtual devices for "xhci_ctrl" pointer passed
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  * @param ptr	pointer to "xhci_ctrl" whose virtual devices are to be freed
133*4882a593Smuzhiyun  * @return none
134*4882a593Smuzhiyun  */
xhci_free_virt_devices(struct xhci_ctrl * ctrl)135*4882a593Smuzhiyun static void xhci_free_virt_devices(struct xhci_ctrl *ctrl)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	int i;
138*4882a593Smuzhiyun 	int slot_id;
139*4882a593Smuzhiyun 	struct xhci_virt_device *virt_dev;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/*
142*4882a593Smuzhiyun 	 * refactored here to loop through all virt_dev
143*4882a593Smuzhiyun 	 * Slot ID 0 is reserved
144*4882a593Smuzhiyun 	 */
145*4882a593Smuzhiyun 	for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) {
146*4882a593Smuzhiyun 		virt_dev = ctrl->devs[slot_id];
147*4882a593Smuzhiyun 		if (!virt_dev)
148*4882a593Smuzhiyun 			continue;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		ctrl->dcbaa->dev_context_ptrs[slot_id] = 0;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		for (i = 0; i < 31; ++i)
153*4882a593Smuzhiyun 			if (virt_dev->eps[i].ring)
154*4882a593Smuzhiyun 				xhci_ring_free(virt_dev->eps[i].ring);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		if (virt_dev->in_ctx)
157*4882a593Smuzhiyun 			xhci_free_container_ctx(virt_dev->in_ctx);
158*4882a593Smuzhiyun 		if (virt_dev->out_ctx)
159*4882a593Smuzhiyun 			xhci_free_container_ctx(virt_dev->out_ctx);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		free(virt_dev);
162*4882a593Smuzhiyun 		/* make sure we are pointing to NULL */
163*4882a593Smuzhiyun 		ctrl->devs[slot_id] = NULL;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * frees all the memory allocated
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * @param ptr	pointer to "xhci_ctrl" to be cleaned up
171*4882a593Smuzhiyun  * @return none
172*4882a593Smuzhiyun  */
xhci_cleanup(struct xhci_ctrl * ctrl)173*4882a593Smuzhiyun void xhci_cleanup(struct xhci_ctrl *ctrl)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	xhci_ring_free(ctrl->event_ring);
176*4882a593Smuzhiyun 	xhci_ring_free(ctrl->cmd_ring);
177*4882a593Smuzhiyun 	xhci_scratchpad_free(ctrl);
178*4882a593Smuzhiyun 	xhci_free_virt_devices(ctrl);
179*4882a593Smuzhiyun 	free(ctrl->erst.entries);
180*4882a593Smuzhiyun 	free(ctrl->dcbaa);
181*4882a593Smuzhiyun 	memset(ctrl, '\0', sizeof(struct xhci_ctrl));
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /**
185*4882a593Smuzhiyun  * Malloc the aligned memory
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * @param size	size of memory to be allocated
188*4882a593Smuzhiyun  * @return allocates the memory and returns the aligned pointer
189*4882a593Smuzhiyun  */
xhci_malloc(unsigned int size)190*4882a593Smuzhiyun static void *xhci_malloc(unsigned int size)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	void *ptr;
193*4882a593Smuzhiyun 	size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
196*4882a593Smuzhiyun 	BUG_ON(!ptr);
197*4882a593Smuzhiyun 	memset(ptr, '\0', size);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)ptr, size);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return ptr;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /**
205*4882a593Smuzhiyun  * Make the prev segment point to the next segment.
206*4882a593Smuzhiyun  * Change the last TRB in the prev segment to be a Link TRB which points to the
207*4882a593Smuzhiyun  * address of the next segment.  The caller needs to set any Link TRB
208*4882a593Smuzhiyun  * related flags, such as End TRB, Toggle Cycle, and no snoop.
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * @param prev	pointer to the previous segment
211*4882a593Smuzhiyun  * @param next	pointer to the next segment
212*4882a593Smuzhiyun  * @param link_trbs	flag to indicate whether to link the trbs or NOT
213*4882a593Smuzhiyun  * @return none
214*4882a593Smuzhiyun  */
xhci_link_segments(struct xhci_segment * prev,struct xhci_segment * next,bool link_trbs)215*4882a593Smuzhiyun static void xhci_link_segments(struct xhci_segment *prev,
216*4882a593Smuzhiyun 				struct xhci_segment *next, bool link_trbs)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	u32 val;
219*4882a593Smuzhiyun 	u64 val_64 = 0;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (!prev || !next)
222*4882a593Smuzhiyun 		return;
223*4882a593Smuzhiyun 	prev->next = next;
224*4882a593Smuzhiyun 	if (link_trbs) {
225*4882a593Smuzhiyun 		val_64 = (uintptr_t)next->trbs;
226*4882a593Smuzhiyun 		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		/*
229*4882a593Smuzhiyun 		 * Set the last TRB in the segment to
230*4882a593Smuzhiyun 		 * have a TRB type ID of Link TRB
231*4882a593Smuzhiyun 		 */
232*4882a593Smuzhiyun 		val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
233*4882a593Smuzhiyun 		val &= ~TRB_TYPE_BITMASK;
234*4882a593Smuzhiyun 		val |= (TRB_LINK << TRB_TYPE_SHIFT);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun  * Initialises the Ring's enqueue,dequeue,enq_seg pointers
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * @param ring	pointer to the RING to be intialised
244*4882a593Smuzhiyun  * @return none
245*4882a593Smuzhiyun  */
xhci_initialize_ring_info(struct xhci_ring * ring)246*4882a593Smuzhiyun static void xhci_initialize_ring_info(struct xhci_ring *ring)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	/*
249*4882a593Smuzhiyun 	 * The ring is empty, so the enqueue pointer == dequeue pointer
250*4882a593Smuzhiyun 	 */
251*4882a593Smuzhiyun 	ring->enqueue = ring->first_seg->trbs;
252*4882a593Smuzhiyun 	ring->enq_seg = ring->first_seg;
253*4882a593Smuzhiyun 	ring->dequeue = ring->enqueue;
254*4882a593Smuzhiyun 	ring->deq_seg = ring->first_seg;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	/*
257*4882a593Smuzhiyun 	 * The ring is initialized to 0. The producer must write 1 to the
258*4882a593Smuzhiyun 	 * cycle bit to handover ownership of the TRB, so PCS = 1.
259*4882a593Smuzhiyun 	 * The consumer must compare CCS to the cycle bit to
260*4882a593Smuzhiyun 	 * check ownership, so CCS = 1.
261*4882a593Smuzhiyun 	 */
262*4882a593Smuzhiyun 	ring->cycle_state = 1;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /**
266*4882a593Smuzhiyun  * Allocates a generic ring segment from the ring pool, sets the dma address,
267*4882a593Smuzhiyun  * initializes the segment to zero, and sets the private next pointer to NULL.
268*4882a593Smuzhiyun  * Section 4.11.1.1:
269*4882a593Smuzhiyun  * "All components of all Command and Transfer TRBs shall be initialized to '0'"
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * @param	none
272*4882a593Smuzhiyun  * @return pointer to the newly allocated SEGMENT
273*4882a593Smuzhiyun  */
xhci_segment_alloc(void)274*4882a593Smuzhiyun static struct xhci_segment *xhci_segment_alloc(void)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	struct xhci_segment *seg;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
279*4882a593Smuzhiyun 	BUG_ON(!seg);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	seg->next = NULL;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return seg;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  * Create a new ring with zero or more segments.
290*4882a593Smuzhiyun  * TODO: current code only uses one-time-allocated single-segment rings
291*4882a593Smuzhiyun  * of 1KB anyway, so we might as well get rid of all the segment and
292*4882a593Smuzhiyun  * linking code (and maybe increase the size a bit, e.g. 4KB).
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  *
295*4882a593Smuzhiyun  * Link each segment together into a ring.
296*4882a593Smuzhiyun  * Set the end flag and the cycle toggle bit on the last segment.
297*4882a593Smuzhiyun  * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * @param num_segs	number of segments in the ring
300*4882a593Smuzhiyun  * @param link_trbs	flag to indicate whether to link the trbs or NOT
301*4882a593Smuzhiyun  * @return pointer to the newly created RING
302*4882a593Smuzhiyun  */
xhci_ring_alloc(unsigned int num_segs,bool link_trbs)303*4882a593Smuzhiyun struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	struct xhci_ring *ring;
306*4882a593Smuzhiyun 	struct xhci_segment *prev;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring));
309*4882a593Smuzhiyun 	BUG_ON(!ring);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (num_segs == 0)
312*4882a593Smuzhiyun 		return ring;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	ring->first_seg = xhci_segment_alloc();
315*4882a593Smuzhiyun 	BUG_ON(!ring->first_seg);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	num_segs--;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	prev = ring->first_seg;
320*4882a593Smuzhiyun 	while (num_segs > 0) {
321*4882a593Smuzhiyun 		struct xhci_segment *next;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		next = xhci_segment_alloc();
324*4882a593Smuzhiyun 		BUG_ON(!next);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		xhci_link_segments(prev, next, link_trbs);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 		prev = next;
329*4882a593Smuzhiyun 		num_segs--;
330*4882a593Smuzhiyun 	}
331*4882a593Smuzhiyun 	xhci_link_segments(prev, ring->first_seg, link_trbs);
332*4882a593Smuzhiyun 	if (link_trbs) {
333*4882a593Smuzhiyun 		/* See section 4.9.2.1 and 6.4.4.1 */
334*4882a593Smuzhiyun 		prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
335*4882a593Smuzhiyun 					cpu_to_le32(LINK_TOGGLE);
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 	xhci_initialize_ring_info(ring);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	return ring;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /**
343*4882a593Smuzhiyun  * Set up the scratchpad buffer array and scratchpad buffers
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  * @ctrl	host controller data structure
346*4882a593Smuzhiyun  * @return	-ENOMEM if buffer allocation fails, 0 on success
347*4882a593Smuzhiyun  */
xhci_scratchpad_alloc(struct xhci_ctrl * ctrl)348*4882a593Smuzhiyun static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	struct xhci_hccr *hccr = ctrl->hccr;
351*4882a593Smuzhiyun 	struct xhci_hcor *hcor = ctrl->hcor;
352*4882a593Smuzhiyun 	struct xhci_scratchpad *scratchpad;
353*4882a593Smuzhiyun 	int num_sp;
354*4882a593Smuzhiyun 	uint32_t page_size;
355*4882a593Smuzhiyun 	void *buf;
356*4882a593Smuzhiyun 	int i;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
359*4882a593Smuzhiyun 	if (!num_sp)
360*4882a593Smuzhiyun 		return 0;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	scratchpad = malloc(sizeof(*scratchpad));
363*4882a593Smuzhiyun 	if (!scratchpad)
364*4882a593Smuzhiyun 		goto fail_sp;
365*4882a593Smuzhiyun 	ctrl->scratchpad = scratchpad;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
368*4882a593Smuzhiyun 	if (!scratchpad->sp_array)
369*4882a593Smuzhiyun 		goto fail_sp2;
370*4882a593Smuzhiyun 	ctrl->dcbaa->dev_context_ptrs[0] =
371*4882a593Smuzhiyun 		cpu_to_le64((uintptr_t)scratchpad->sp_array);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
374*4882a593Smuzhiyun 		sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
377*4882a593Smuzhiyun 	for (i = 0; i < 16; i++) {
378*4882a593Smuzhiyun 		if ((0x1 & page_size) != 0)
379*4882a593Smuzhiyun 			break;
380*4882a593Smuzhiyun 		page_size = page_size >> 1;
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 	BUG_ON(i == 16);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	page_size = 1 << (i + 12);
385*4882a593Smuzhiyun 	buf = memalign(page_size, num_sp * page_size);
386*4882a593Smuzhiyun 	if (!buf)
387*4882a593Smuzhiyun 		goto fail_sp3;
388*4882a593Smuzhiyun 	memset(buf, '\0', num_sp * page_size);
389*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)buf, num_sp * page_size);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	for (i = 0; i < num_sp; i++) {
392*4882a593Smuzhiyun 		uintptr_t ptr = (uintptr_t)buf + i * page_size;
393*4882a593Smuzhiyun 		scratchpad->sp_array[i] = cpu_to_le64(ptr);
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	return 0;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun fail_sp3:
399*4882a593Smuzhiyun 	free(scratchpad->sp_array);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun fail_sp2:
402*4882a593Smuzhiyun 	free(scratchpad);
403*4882a593Smuzhiyun 	ctrl->scratchpad = NULL;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun fail_sp:
406*4882a593Smuzhiyun 	return -ENOMEM;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun /**
410*4882a593Smuzhiyun  * Allocates the Container context
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
413*4882a593Smuzhiyun  * @param type type of XHCI Container Context
414*4882a593Smuzhiyun  * @return NULL if failed else pointer to the context on success
415*4882a593Smuzhiyun  */
416*4882a593Smuzhiyun static struct xhci_container_ctx
xhci_alloc_container_ctx(struct xhci_ctrl * ctrl,int type)417*4882a593Smuzhiyun 		*xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	struct xhci_container_ctx *ctx;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	ctx = (struct xhci_container_ctx *)
422*4882a593Smuzhiyun 		malloc(sizeof(struct xhci_container_ctx));
423*4882a593Smuzhiyun 	BUG_ON(!ctx);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
426*4882a593Smuzhiyun 	ctx->type = type;
427*4882a593Smuzhiyun 	ctx->size = (MAX_EP_CTX_NUM + 1) *
428*4882a593Smuzhiyun 			CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
429*4882a593Smuzhiyun 	if (type == XHCI_CTX_TYPE_INPUT)
430*4882a593Smuzhiyun 		ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	ctx->bytes = (u8 *)xhci_malloc(ctx->size);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	return ctx;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /**
438*4882a593Smuzhiyun  * Allocating virtual device
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * @param udev	pointer to USB deivce structure
441*4882a593Smuzhiyun  * @return 0 on success else -1 on failure
442*4882a593Smuzhiyun  */
xhci_alloc_virt_device(struct xhci_ctrl * ctrl,unsigned int slot_id)443*4882a593Smuzhiyun int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	u64 byte_64 = 0;
446*4882a593Smuzhiyun 	struct xhci_virt_device *virt_dev;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	/* Slot ID 0 is reserved */
449*4882a593Smuzhiyun 	if (ctrl->devs[slot_id]) {
450*4882a593Smuzhiyun 		printf("Virt dev for slot[%d] already allocated\n", slot_id);
451*4882a593Smuzhiyun 		return -EEXIST;
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	ctrl->devs[slot_id] = (struct xhci_virt_device *)
455*4882a593Smuzhiyun 					malloc(sizeof(struct xhci_virt_device));
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (!ctrl->devs[slot_id]) {
458*4882a593Smuzhiyun 		puts("Failed to allocate virtual device\n");
459*4882a593Smuzhiyun 		return -ENOMEM;
460*4882a593Smuzhiyun 	}
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
463*4882a593Smuzhiyun 	virt_dev = ctrl->devs[slot_id];
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	/* Allocate the (output) device context that will be used in the HC. */
466*4882a593Smuzhiyun 	virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
467*4882a593Smuzhiyun 					XHCI_CTX_TYPE_DEVICE);
468*4882a593Smuzhiyun 	if (!virt_dev->out_ctx) {
469*4882a593Smuzhiyun 		puts("Failed to allocate out context for virt dev\n");
470*4882a593Smuzhiyun 		return -ENOMEM;
471*4882a593Smuzhiyun 	}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* Allocate the (input) device context for address device command */
474*4882a593Smuzhiyun 	virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
475*4882a593Smuzhiyun 					XHCI_CTX_TYPE_INPUT);
476*4882a593Smuzhiyun 	if (!virt_dev->in_ctx) {
477*4882a593Smuzhiyun 		puts("Failed to allocate in context for virt dev\n");
478*4882a593Smuzhiyun 		return -ENOMEM;
479*4882a593Smuzhiyun 	}
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* Allocate endpoint 0 ring */
482*4882a593Smuzhiyun 	virt_dev->eps[0].ring = xhci_ring_alloc(1, true);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/* Point to output device context in dcbaa. */
487*4882a593Smuzhiyun 	ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
490*4882a593Smuzhiyun 			 sizeof(__le64));
491*4882a593Smuzhiyun 	return 0;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun /**
495*4882a593Smuzhiyun  * Allocates the necessary data structures
496*4882a593Smuzhiyun  * for XHCI host controller
497*4882a593Smuzhiyun  *
498*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
499*4882a593Smuzhiyun  * @param hccr	pointer to HOST Controller Control Registers
500*4882a593Smuzhiyun  * @param hcor	pointer to HOST Controller Operational Registers
501*4882a593Smuzhiyun  * @return 0 if successful else -1 on failure
502*4882a593Smuzhiyun  */
xhci_mem_init(struct xhci_ctrl * ctrl,struct xhci_hccr * hccr,struct xhci_hcor * hcor)503*4882a593Smuzhiyun int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
504*4882a593Smuzhiyun 					struct xhci_hcor *hcor)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	uint64_t val_64;
507*4882a593Smuzhiyun 	uint64_t trb_64;
508*4882a593Smuzhiyun 	uint32_t val;
509*4882a593Smuzhiyun 	unsigned long deq;
510*4882a593Smuzhiyun 	int i;
511*4882a593Smuzhiyun 	struct xhci_segment *seg;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/* DCBAA initialization */
514*4882a593Smuzhiyun 	ctrl->dcbaa = (struct xhci_device_context_array *)
515*4882a593Smuzhiyun 			xhci_malloc(sizeof(struct xhci_device_context_array));
516*4882a593Smuzhiyun 	if (ctrl->dcbaa == NULL) {
517*4882a593Smuzhiyun 		puts("unable to allocate DCBA\n");
518*4882a593Smuzhiyun 		return -ENOMEM;
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	val_64 = (uintptr_t)ctrl->dcbaa;
522*4882a593Smuzhiyun 	/* Set the pointer in DCBAA register */
523*4882a593Smuzhiyun 	xhci_writeq(&hcor->or_dcbaap, val_64);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	/* Command ring control pointer register initialization */
526*4882a593Smuzhiyun 	ctrl->cmd_ring = xhci_ring_alloc(1, true);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	/* Set the address in the Command Ring Control register */
529*4882a593Smuzhiyun 	trb_64 = (uintptr_t)ctrl->cmd_ring->first_seg->trbs;
530*4882a593Smuzhiyun 	val_64 = xhci_readq(&hcor->or_crcr);
531*4882a593Smuzhiyun 	val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
532*4882a593Smuzhiyun 		(trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
533*4882a593Smuzhiyun 		ctrl->cmd_ring->cycle_state;
534*4882a593Smuzhiyun 	xhci_writeq(&hcor->or_crcr, val_64);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	/* write the address of db register */
537*4882a593Smuzhiyun 	val = xhci_readl(&hccr->cr_dboff);
538*4882a593Smuzhiyun 	val &= DBOFF_MASK;
539*4882a593Smuzhiyun 	ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	/* write the address of runtime register */
542*4882a593Smuzhiyun 	val = xhci_readl(&hccr->cr_rtsoff);
543*4882a593Smuzhiyun 	val &= RTSOFF_MASK;
544*4882a593Smuzhiyun 	ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	/* writting the address of ir_set structure */
547*4882a593Smuzhiyun 	ctrl->ir_set = &ctrl->run_regs->ir_set[0];
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	/* Event ring does not maintain link TRB */
550*4882a593Smuzhiyun 	ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
551*4882a593Smuzhiyun 	ctrl->erst.entries = (struct xhci_erst_entry *)
552*4882a593Smuzhiyun 		xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	ctrl->erst.num_entries = ERST_NUM_SEGS;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	for (val = 0, seg = ctrl->event_ring->first_seg;
557*4882a593Smuzhiyun 			val < ERST_NUM_SEGS;
558*4882a593Smuzhiyun 			val++) {
559*4882a593Smuzhiyun 		trb_64 = 0;
560*4882a593Smuzhiyun 		trb_64 = (uintptr_t)seg->trbs;
561*4882a593Smuzhiyun 		struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
562*4882a593Smuzhiyun 		xhci_writeq(&entry->seg_addr, trb_64);
563*4882a593Smuzhiyun 		entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
564*4882a593Smuzhiyun 		entry->rsvd = 0;
565*4882a593Smuzhiyun 		seg = seg->next;
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)ctrl->erst.entries,
568*4882a593Smuzhiyun 			 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	deq = (unsigned long)ctrl->event_ring->dequeue;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	/* Update HC event ring dequeue pointer */
573*4882a593Smuzhiyun 	xhci_writeq(&ctrl->ir_set->erst_dequeue,
574*4882a593Smuzhiyun 				(u64)deq & (u64)~ERST_PTR_MASK);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/* set ERST count with the number of entries in the segment table */
577*4882a593Smuzhiyun 	val = xhci_readl(&ctrl->ir_set->erst_size);
578*4882a593Smuzhiyun 	val &= ERST_SIZE_MASK;
579*4882a593Smuzhiyun 	val |= ERST_NUM_SEGS;
580*4882a593Smuzhiyun 	xhci_writel(&ctrl->ir_set->erst_size, val);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	/* this is the event ring segment table pointer */
583*4882a593Smuzhiyun 	val_64 = xhci_readq(&ctrl->ir_set->erst_base);
584*4882a593Smuzhiyun 	val_64 &= ERST_PTR_MASK;
585*4882a593Smuzhiyun 	val_64 |= ((uintptr_t)(ctrl->erst.entries) & ~ERST_PTR_MASK);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	xhci_writeq(&ctrl->ir_set->erst_base, val_64);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	/* set up the scratchpad buffer array and scratchpad buffers */
590*4882a593Smuzhiyun 	xhci_scratchpad_alloc(ctrl);
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	/* initializing the virtual devices to NULL */
593*4882a593Smuzhiyun 	for (i = 0; i < MAX_HC_SLOTS; ++i)
594*4882a593Smuzhiyun 		ctrl->devs[i] = NULL;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	/*
597*4882a593Smuzhiyun 	 * Just Zero'ing this register completely,
598*4882a593Smuzhiyun 	 * or some spurious Device Notification Events
599*4882a593Smuzhiyun 	 * might screw things here.
600*4882a593Smuzhiyun 	 */
601*4882a593Smuzhiyun 	xhci_writel(&hcor->or_dnctrl, 0x0);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 	return 0;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun /**
607*4882a593Smuzhiyun  * Give the input control context for the passed container context
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * @param ctx	pointer to the context
610*4882a593Smuzhiyun  * @return pointer to the Input control context data
611*4882a593Smuzhiyun  */
612*4882a593Smuzhiyun struct xhci_input_control_ctx
xhci_get_input_control_ctx(struct xhci_container_ctx * ctx)613*4882a593Smuzhiyun 		*xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun 	BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
616*4882a593Smuzhiyun 	return (struct xhci_input_control_ctx *)ctx->bytes;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun /**
620*4882a593Smuzhiyun  * Give the slot context for the passed container context
621*4882a593Smuzhiyun  *
622*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
623*4882a593Smuzhiyun  * @param ctx	pointer to the context
624*4882a593Smuzhiyun  * @return pointer to the slot control context data
625*4882a593Smuzhiyun  */
xhci_get_slot_ctx(struct xhci_ctrl * ctrl,struct xhci_container_ctx * ctx)626*4882a593Smuzhiyun struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
627*4882a593Smuzhiyun 				struct xhci_container_ctx *ctx)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	if (ctx->type == XHCI_CTX_TYPE_DEVICE)
630*4882a593Smuzhiyun 		return (struct xhci_slot_ctx *)ctx->bytes;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	return (struct xhci_slot_ctx *)
633*4882a593Smuzhiyun 		(ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)));
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun /**
637*4882a593Smuzhiyun  * Gets the EP context from based on the ep_index
638*4882a593Smuzhiyun  *
639*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
640*4882a593Smuzhiyun  * @param ctx	context container
641*4882a593Smuzhiyun  * @param ep_index	index of the endpoint
642*4882a593Smuzhiyun  * @return pointer to the End point context
643*4882a593Smuzhiyun  */
xhci_get_ep_ctx(struct xhci_ctrl * ctrl,struct xhci_container_ctx * ctx,unsigned int ep_index)644*4882a593Smuzhiyun struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
645*4882a593Smuzhiyun 				    struct xhci_container_ctx *ctx,
646*4882a593Smuzhiyun 				    unsigned int ep_index)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	/* increment ep index by offset of start of ep ctx array */
649*4882a593Smuzhiyun 	ep_index++;
650*4882a593Smuzhiyun 	if (ctx->type == XHCI_CTX_TYPE_INPUT)
651*4882a593Smuzhiyun 		ep_index++;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	return (struct xhci_ep_ctx *)
654*4882a593Smuzhiyun 		(ctx->bytes +
655*4882a593Smuzhiyun 		(ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))));
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /**
659*4882a593Smuzhiyun  * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
660*4882a593Smuzhiyun  * Useful when you want to change one particular aspect of the endpoint
661*4882a593Smuzhiyun  * and then issue a configure endpoint command.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
664*4882a593Smuzhiyun  * @param in_ctx contains the input context
665*4882a593Smuzhiyun  * @param out_ctx contains the input context
666*4882a593Smuzhiyun  * @param ep_index index of the end point
667*4882a593Smuzhiyun  * @return none
668*4882a593Smuzhiyun  */
xhci_endpoint_copy(struct xhci_ctrl * ctrl,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx,unsigned int ep_index)669*4882a593Smuzhiyun void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
670*4882a593Smuzhiyun 			struct xhci_container_ctx *in_ctx,
671*4882a593Smuzhiyun 			struct xhci_container_ctx *out_ctx,
672*4882a593Smuzhiyun 			unsigned int ep_index)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun 	struct xhci_ep_ctx *out_ep_ctx;
675*4882a593Smuzhiyun 	struct xhci_ep_ctx *in_ep_ctx;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
678*4882a593Smuzhiyun 	in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	in_ep_ctx->ep_info = out_ep_ctx->ep_info;
681*4882a593Smuzhiyun 	in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
682*4882a593Smuzhiyun 	in_ep_ctx->deq = out_ep_ctx->deq;
683*4882a593Smuzhiyun 	in_ep_ctx->tx_info = out_ep_ctx->tx_info;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /**
687*4882a593Smuzhiyun  * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
688*4882a593Smuzhiyun  * Useful when you want to change one particular aspect of the endpoint
689*4882a593Smuzhiyun  * and then issue a configure endpoint command.
690*4882a593Smuzhiyun  * Only the context entries field matters, but
691*4882a593Smuzhiyun  * we'll copy the whole thing anyway.
692*4882a593Smuzhiyun  *
693*4882a593Smuzhiyun  * @param ctrl	Host controller data structure
694*4882a593Smuzhiyun  * @param in_ctx contains the inpout context
695*4882a593Smuzhiyun  * @param out_ctx contains the inpout context
696*4882a593Smuzhiyun  * @return none
697*4882a593Smuzhiyun  */
xhci_slot_copy(struct xhci_ctrl * ctrl,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx)698*4882a593Smuzhiyun void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
699*4882a593Smuzhiyun 					struct xhci_container_ctx *out_ctx)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct xhci_slot_ctx *in_slot_ctx;
702*4882a593Smuzhiyun 	struct xhci_slot_ctx *out_slot_ctx;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
705*4882a593Smuzhiyun 	out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	in_slot_ctx->dev_info = out_slot_ctx->dev_info;
708*4882a593Smuzhiyun 	in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
709*4882a593Smuzhiyun 	in_slot_ctx->tt_info = out_slot_ctx->tt_info;
710*4882a593Smuzhiyun 	in_slot_ctx->dev_state = out_slot_ctx->dev_state;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun /**
714*4882a593Smuzhiyun  * Setup an xHCI virtual device for a Set Address command
715*4882a593Smuzhiyun  *
716*4882a593Smuzhiyun  * @param udev pointer to the Device Data Structure
717*4882a593Smuzhiyun  * @return returns negative value on failure else 0 on success
718*4882a593Smuzhiyun  */
xhci_setup_addressable_virt_dev(struct xhci_ctrl * ctrl,struct usb_device * udev,int hop_portnr)719*4882a593Smuzhiyun void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
720*4882a593Smuzhiyun 				     struct usb_device *udev, int hop_portnr)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct xhci_virt_device *virt_dev;
723*4882a593Smuzhiyun 	struct xhci_ep_ctx *ep0_ctx;
724*4882a593Smuzhiyun 	struct xhci_slot_ctx *slot_ctx;
725*4882a593Smuzhiyun 	u32 port_num = 0;
726*4882a593Smuzhiyun 	u64 trb_64 = 0;
727*4882a593Smuzhiyun 	int slot_id = udev->slot_id;
728*4882a593Smuzhiyun 	int speed = udev->speed;
729*4882a593Smuzhiyun 	int route = 0;
730*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
731*4882a593Smuzhiyun 	struct usb_device *dev = udev;
732*4882a593Smuzhiyun 	struct usb_hub_device *hub;
733*4882a593Smuzhiyun #endif
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	virt_dev = ctrl->devs[slot_id];
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	BUG_ON(!virt_dev);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* Extract the EP0 and Slot Ctrl */
740*4882a593Smuzhiyun 	ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
741*4882a593Smuzhiyun 	slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* Only the control endpoint is valid - one endpoint context */
744*4882a593Smuzhiyun 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
747*4882a593Smuzhiyun 	/* Calculate the route string for this device */
748*4882a593Smuzhiyun 	port_num = dev->portnr;
749*4882a593Smuzhiyun 	while (!usb_hub_is_root_hub(dev->dev)) {
750*4882a593Smuzhiyun 		hub = dev_get_uclass_priv(dev->dev);
751*4882a593Smuzhiyun 		/*
752*4882a593Smuzhiyun 		 * Each hub in the topology is expected to have no more than
753*4882a593Smuzhiyun 		 * 15 ports in order for the route string of a device to be
754*4882a593Smuzhiyun 		 * unique. SuperSpeed hubs are restricted to only having 15
755*4882a593Smuzhiyun 		 * ports, but FS/LS/HS hubs are not. The xHCI specification
756*4882a593Smuzhiyun 		 * says that if the port number the device is greater than 15,
757*4882a593Smuzhiyun 		 * that portion of the route string shall be set to 15.
758*4882a593Smuzhiyun 		 */
759*4882a593Smuzhiyun 		if (port_num > 15)
760*4882a593Smuzhiyun 			port_num = 15;
761*4882a593Smuzhiyun 		route |= port_num << (hub->hub_depth * 4);
762*4882a593Smuzhiyun 		dev = dev_get_parent_priv(dev->dev);
763*4882a593Smuzhiyun 		port_num = dev->portnr;
764*4882a593Smuzhiyun 		dev = dev_get_parent_priv(dev->dev->parent);
765*4882a593Smuzhiyun 	}
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	debug("route string %x\n", route);
768*4882a593Smuzhiyun #endif
769*4882a593Smuzhiyun 	slot_ctx->dev_info |= route;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	switch (speed) {
772*4882a593Smuzhiyun 	case USB_SPEED_SUPER:
773*4882a593Smuzhiyun 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
774*4882a593Smuzhiyun 		break;
775*4882a593Smuzhiyun 	case USB_SPEED_HIGH:
776*4882a593Smuzhiyun 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
777*4882a593Smuzhiyun 		break;
778*4882a593Smuzhiyun 	case USB_SPEED_FULL:
779*4882a593Smuzhiyun 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
780*4882a593Smuzhiyun 		break;
781*4882a593Smuzhiyun 	case USB_SPEED_LOW:
782*4882a593Smuzhiyun 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
783*4882a593Smuzhiyun 		break;
784*4882a593Smuzhiyun 	default:
785*4882a593Smuzhiyun 		/* Speed was set earlier, this shouldn't happen. */
786*4882a593Smuzhiyun 		BUG();
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
790*4882a593Smuzhiyun 	/* Set up TT fields to support FS/LS devices */
791*4882a593Smuzhiyun 	if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
792*4882a593Smuzhiyun 		struct udevice *parent = udev->dev;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 		dev = udev;
795*4882a593Smuzhiyun 		do {
796*4882a593Smuzhiyun 			port_num = dev->portnr;
797*4882a593Smuzhiyun 			dev = dev_get_parent_priv(parent);
798*4882a593Smuzhiyun 			if (usb_hub_is_root_hub(dev->dev))
799*4882a593Smuzhiyun 				break;
800*4882a593Smuzhiyun 			parent = dev->dev->parent;
801*4882a593Smuzhiyun 		} while (dev->speed != USB_SPEED_HIGH);
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 		if (!usb_hub_is_root_hub(dev->dev)) {
804*4882a593Smuzhiyun 			hub = dev_get_uclass_priv(dev->dev);
805*4882a593Smuzhiyun 			if (hub->tt.multi)
806*4882a593Smuzhiyun 				slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
807*4882a593Smuzhiyun 			slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
808*4882a593Smuzhiyun 			slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
809*4882a593Smuzhiyun 		}
810*4882a593Smuzhiyun 	}
811*4882a593Smuzhiyun #endif
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	port_num = hop_portnr;
814*4882a593Smuzhiyun 	debug("port_num = %d\n", port_num);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	slot_ctx->dev_info2 |=
817*4882a593Smuzhiyun 			cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
818*4882a593Smuzhiyun 				ROOT_HUB_PORT_SHIFT));
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	/* Step 4 - ring already allocated */
821*4882a593Smuzhiyun 	/* Step 5 */
822*4882a593Smuzhiyun 	ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT);
823*4882a593Smuzhiyun 	debug("SPEED = %d\n", speed);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	switch (speed) {
826*4882a593Smuzhiyun 	case USB_SPEED_SUPER:
827*4882a593Smuzhiyun 		ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) <<
828*4882a593Smuzhiyun 					MAX_PACKET_SHIFT));
829*4882a593Smuzhiyun 		debug("Setting Packet size = 512bytes\n");
830*4882a593Smuzhiyun 		break;
831*4882a593Smuzhiyun 	case USB_SPEED_HIGH:
832*4882a593Smuzhiyun 	/* USB core guesses at a 64-byte max packet first for FS devices */
833*4882a593Smuzhiyun 	case USB_SPEED_FULL:
834*4882a593Smuzhiyun 		ep0_ctx->ep_info2 |= cpu_to_le32(((64 & MAX_PACKET_MASK) <<
835*4882a593Smuzhiyun 					MAX_PACKET_SHIFT));
836*4882a593Smuzhiyun 		debug("Setting Packet size = 64bytes\n");
837*4882a593Smuzhiyun 		break;
838*4882a593Smuzhiyun 	case USB_SPEED_LOW:
839*4882a593Smuzhiyun 		ep0_ctx->ep_info2 |= cpu_to_le32(((8 & MAX_PACKET_MASK) <<
840*4882a593Smuzhiyun 					MAX_PACKET_SHIFT));
841*4882a593Smuzhiyun 		debug("Setting Packet size = 8bytes\n");
842*4882a593Smuzhiyun 		break;
843*4882a593Smuzhiyun 	default:
844*4882a593Smuzhiyun 		/* New speed? */
845*4882a593Smuzhiyun 		BUG();
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	/* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
849*4882a593Smuzhiyun 	ep0_ctx->ep_info2 |=
850*4882a593Smuzhiyun 			cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) |
851*4882a593Smuzhiyun 			((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT));
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs;
854*4882a593Smuzhiyun 	ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	/*
857*4882a593Smuzhiyun 	 * xHCI spec 6.2.3:
858*4882a593Smuzhiyun 	 * software shall set 'Average TRB Length' to 8 for control endpoints.
859*4882a593Smuzhiyun 	 */
860*4882a593Smuzhiyun 	ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* Steps 7 and 8 were done in xhci_alloc_virt_device() */
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
865*4882a593Smuzhiyun 	xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
866*4882a593Smuzhiyun }
867