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