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