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