blob: f4465205284584bb0c175dbdf172245a7ec3ebfb [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
113 free((void *)(uintptr_t)ctrl->scratchpad->sp_array[0]);
114 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);
183 memset(ctrl, '\0', sizeof(struct xhci_ctrl));
184}
185
186/**
187 * Malloc the aligned memory
188 *
189 * @param size size of memory to be allocated
190 * @return allocates the memory and returns the aligned pointer
191 */
192static void *xhci_malloc(unsigned int size)
193{
194 void *ptr;
195 size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
196
197 ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
198 BUG_ON(!ptr);
199 memset(ptr, '\0', size);
200
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300201 xhci_flush_cache((uintptr_t)ptr, size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530202
203 return ptr;
204}
205
206/**
207 * Make the prev segment point to the next segment.
208 * Change the last TRB in the prev segment to be a Link TRB which points to the
209 * address of the next segment. The caller needs to set any Link TRB
210 * related flags, such as End TRB, Toggle Cycle, and no snoop.
211 *
212 * @param prev pointer to the previous segment
213 * @param next pointer to the next segment
214 * @param link_trbs flag to indicate whether to link the trbs or NOT
215 * @return none
216 */
217static void xhci_link_segments(struct xhci_segment *prev,
218 struct xhci_segment *next, bool link_trbs)
219{
220 u32 val;
221 u64 val_64 = 0;
222
223 if (!prev || !next)
224 return;
225 prev->next = next;
226 if (link_trbs) {
227 val_64 = (uintptr_t)next->trbs;
228 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = val_64;
229
230 /*
231 * Set the last TRB in the segment to
232 * have a TRB type ID of Link TRB
233 */
234 val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
235 val &= ~TRB_TYPE_BITMASK;
236 val |= (TRB_LINK << TRB_TYPE_SHIFT);
237
238 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
239 }
240}
241
242/**
243 * Initialises the Ring's enqueue,dequeue,enq_seg pointers
244 *
245 * @param ring pointer to the RING to be intialised
246 * @return none
247 */
248static void xhci_initialize_ring_info(struct xhci_ring *ring)
249{
250 /*
251 * The ring is empty, so the enqueue pointer == dequeue pointer
252 */
253 ring->enqueue = ring->first_seg->trbs;
254 ring->enq_seg = ring->first_seg;
255 ring->dequeue = ring->enqueue;
256 ring->deq_seg = ring->first_seg;
257
258 /*
259 * The ring is initialized to 0. The producer must write 1 to the
260 * cycle bit to handover ownership of the TRB, so PCS = 1.
261 * The consumer must compare CCS to the cycle bit to
262 * check ownership, so CCS = 1.
263 */
264 ring->cycle_state = 1;
265}
266
267/**
268 * Allocates a generic ring segment from the ring pool, sets the dma address,
269 * initializes the segment to zero, and sets the private next pointer to NULL.
270 * Section 4.11.1.1:
271 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
272 *
273 * @param none
274 * @return pointer to the newly allocated SEGMENT
275 */
276static struct xhci_segment *xhci_segment_alloc(void)
277{
278 struct xhci_segment *seg;
279
280 seg = (struct xhci_segment *)malloc(sizeof(struct xhci_segment));
281 BUG_ON(!seg);
282
283 seg->trbs = (union xhci_trb *)xhci_malloc(SEGMENT_SIZE);
284
285 seg->next = NULL;
286
287 return seg;
288}
289
290/**
291 * Create a new ring with zero or more segments.
292 * TODO: current code only uses one-time-allocated single-segment rings
293 * of 1KB anyway, so we might as well get rid of all the segment and
294 * linking code (and maybe increase the size a bit, e.g. 4KB).
295 *
296 *
297 * Link each segment together into a ring.
298 * Set the end flag and the cycle toggle bit on the last segment.
299 * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
300 *
301 * @param num_segs number of segments in the ring
302 * @param link_trbs flag to indicate whether to link the trbs or NOT
303 * @return pointer to the newly created RING
304 */
305struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
306{
307 struct xhci_ring *ring;
308 struct xhci_segment *prev;
309
310 ring = (struct xhci_ring *)malloc(sizeof(struct xhci_ring));
311 BUG_ON(!ring);
312
313 if (num_segs == 0)
314 return ring;
315
316 ring->first_seg = xhci_segment_alloc();
317 BUG_ON(!ring->first_seg);
318
319 num_segs--;
320
321 prev = ring->first_seg;
322 while (num_segs > 0) {
323 struct xhci_segment *next;
324
325 next = xhci_segment_alloc();
326 BUG_ON(!next);
327
328 xhci_link_segments(prev, next, link_trbs);
329
330 prev = next;
331 num_segs--;
332 }
333 xhci_link_segments(prev, ring->first_seg, link_trbs);
334 if (link_trbs) {
335 /* See section 4.9.2.1 and 6.4.4.1 */
336 prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
337 cpu_to_le32(LINK_TOGGLE);
338 }
339 xhci_initialize_ring_info(ring);
340
341 return ring;
342}
343
344/**
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800345 * Set up the scratchpad buffer array and scratchpad buffers
346 *
347 * @ctrl host controller data structure
348 * @return -ENOMEM if buffer allocation fails, 0 on success
349 */
350static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
351{
352 struct xhci_hccr *hccr = ctrl->hccr;
353 struct xhci_hcor *hcor = ctrl->hcor;
354 struct xhci_scratchpad *scratchpad;
355 int num_sp;
356 uint32_t page_size;
357 void *buf;
358 int i;
359
360 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
361 if (!num_sp)
362 return 0;
363
364 scratchpad = malloc(sizeof(*scratchpad));
365 if (!scratchpad)
366 goto fail_sp;
367 ctrl->scratchpad = scratchpad;
368
369 scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
370 if (!scratchpad->sp_array)
371 goto fail_sp2;
372 ctrl->dcbaa->dev_context_ptrs[0] =
373 cpu_to_le64((uintptr_t)scratchpad->sp_array);
374
Ye Li4bda7182019-01-07 02:45:46 +0000375 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
376 sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
377
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800378 page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
379 for (i = 0; i < 16; i++) {
380 if ((0x1 & page_size) != 0)
381 break;
382 page_size = page_size >> 1;
383 }
384 BUG_ON(i == 16);
385
386 page_size = 1 << (i + 12);
387 buf = memalign(page_size, num_sp * page_size);
388 if (!buf)
389 goto fail_sp3;
390 memset(buf, '\0', num_sp * page_size);
391 xhci_flush_cache((uintptr_t)buf, num_sp * page_size);
392
393 for (i = 0; i < num_sp; i++) {
394 uintptr_t ptr = (uintptr_t)buf + i * page_size;
395 scratchpad->sp_array[i] = cpu_to_le64(ptr);
396 }
397
Sylwester Nawrockiffedc752020-05-25 13:39:51 +0200398 xhci_flush_cache((uintptr_t)scratchpad->sp_array,
399 sizeof(u64) * num_sp);
400
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800401 return 0;
402
403fail_sp3:
404 free(scratchpad->sp_array);
405
406fail_sp2:
407 free(scratchpad);
408 ctrl->scratchpad = NULL;
409
410fail_sp:
411 return -ENOMEM;
412}
413
414/**
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530415 * Allocates the Container context
416 *
417 * @param ctrl Host controller data structure
418 * @param type type of XHCI Container Context
419 * @return NULL if failed else pointer to the context on success
420 */
421static struct xhci_container_ctx
422 *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
423{
424 struct xhci_container_ctx *ctx;
425
426 ctx = (struct xhci_container_ctx *)
427 malloc(sizeof(struct xhci_container_ctx));
428 BUG_ON(!ctx);
429
430 BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
431 ctx->type = type;
432 ctx->size = (MAX_EP_CTX_NUM + 1) *
433 CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
434 if (type == XHCI_CTX_TYPE_INPUT)
435 ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
436
437 ctx->bytes = (u8 *)xhci_malloc(ctx->size);
438
439 return ctx;
440}
441
442/**
443 * Allocating virtual device
444 *
445 * @param udev pointer to USB deivce structure
446 * @return 0 on success else -1 on failure
447 */
Simon Glass88a37842015-03-25 12:22:50 -0600448int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530449{
450 u64 byte_64 = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530451 struct xhci_virt_device *virt_dev;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530452
453 /* Slot ID 0 is reserved */
454 if (ctrl->devs[slot_id]) {
455 printf("Virt dev for slot[%d] already allocated\n", slot_id);
456 return -EEXIST;
457 }
458
459 ctrl->devs[slot_id] = (struct xhci_virt_device *)
460 malloc(sizeof(struct xhci_virt_device));
461
462 if (!ctrl->devs[slot_id]) {
463 puts("Failed to allocate virtual device\n");
464 return -ENOMEM;
465 }
466
467 memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
468 virt_dev = ctrl->devs[slot_id];
469
470 /* Allocate the (output) device context that will be used in the HC. */
471 virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
472 XHCI_CTX_TYPE_DEVICE);
473 if (!virt_dev->out_ctx) {
474 puts("Failed to allocate out context for virt dev\n");
475 return -ENOMEM;
476 }
477
478 /* Allocate the (input) device context for address device command */
479 virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
480 XHCI_CTX_TYPE_INPUT);
481 if (!virt_dev->in_ctx) {
482 puts("Failed to allocate in context for virt dev\n");
483 return -ENOMEM;
484 }
485
486 /* Allocate endpoint 0 ring */
487 virt_dev->eps[0].ring = xhci_ring_alloc(1, true);
488
489 byte_64 = (uintptr_t)(virt_dev->out_ctx->bytes);
490
491 /* Point to output device context in dcbaa. */
492 ctrl->dcbaa->dev_context_ptrs[slot_id] = byte_64;
493
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300494 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
495 sizeof(__le64));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530496 return 0;
497}
498
499/**
500 * Allocates the necessary data structures
501 * for XHCI host controller
502 *
503 * @param ctrl Host controller data structure
504 * @param hccr pointer to HOST Controller Control Registers
505 * @param hcor pointer to HOST Controller Operational Registers
506 * @return 0 if successful else -1 on failure
507 */
508int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
509 struct xhci_hcor *hcor)
510{
511 uint64_t val_64;
512 uint64_t trb_64;
513 uint32_t val;
514 unsigned long deq;
515 int i;
516 struct xhci_segment *seg;
517
518 /* DCBAA initialization */
519 ctrl->dcbaa = (struct xhci_device_context_array *)
520 xhci_malloc(sizeof(struct xhci_device_context_array));
521 if (ctrl->dcbaa == NULL) {
522 puts("unable to allocate DCBA\n");
523 return -ENOMEM;
524 }
525
526 val_64 = (uintptr_t)ctrl->dcbaa;
527 /* Set the pointer in DCBAA register */
528 xhci_writeq(&hcor->or_dcbaap, val_64);
529
530 /* Command ring control pointer register initialization */
531 ctrl->cmd_ring = xhci_ring_alloc(1, true);
532
533 /* Set the address in the Command Ring Control register */
534 trb_64 = (uintptr_t)ctrl->cmd_ring->first_seg->trbs;
535 val_64 = xhci_readq(&hcor->or_crcr);
536 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
537 (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
538 ctrl->cmd_ring->cycle_state;
539 xhci_writeq(&hcor->or_crcr, val_64);
540
541 /* write the address of db register */
542 val = xhci_readl(&hccr->cr_dboff);
543 val &= DBOFF_MASK;
544 ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
545
546 /* write the address of runtime register */
547 val = xhci_readl(&hccr->cr_rtsoff);
548 val &= RTSOFF_MASK;
549 ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
550
551 /* writting the address of ir_set structure */
552 ctrl->ir_set = &ctrl->run_regs->ir_set[0];
553
554 /* Event ring does not maintain link TRB */
555 ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
556 ctrl->erst.entries = (struct xhci_erst_entry *)
557 xhci_malloc(sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
558
559 ctrl->erst.num_entries = ERST_NUM_SEGS;
560
561 for (val = 0, seg = ctrl->event_ring->first_seg;
562 val < ERST_NUM_SEGS;
563 val++) {
564 trb_64 = 0;
565 trb_64 = (uintptr_t)seg->trbs;
566 struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
567 xhci_writeq(&entry->seg_addr, trb_64);
568 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
569 entry->rsvd = 0;
570 seg = seg->next;
571 }
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300572 xhci_flush_cache((uintptr_t)ctrl->erst.entries,
573 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530574
575 deq = (unsigned long)ctrl->event_ring->dequeue;
576
577 /* Update HC event ring dequeue pointer */
578 xhci_writeq(&ctrl->ir_set->erst_dequeue,
579 (u64)deq & (u64)~ERST_PTR_MASK);
580
581 /* set ERST count with the number of entries in the segment table */
582 val = xhci_readl(&ctrl->ir_set->erst_size);
583 val &= ERST_SIZE_MASK;
584 val |= ERST_NUM_SEGS;
585 xhci_writel(&ctrl->ir_set->erst_size, val);
586
587 /* this is the event ring segment table pointer */
588 val_64 = xhci_readq(&ctrl->ir_set->erst_base);
589 val_64 &= ERST_PTR_MASK;
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300590 val_64 |= ((uintptr_t)(ctrl->erst.entries) & ~ERST_PTR_MASK);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530591
592 xhci_writeq(&ctrl->ir_set->erst_base, val_64);
593
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800594 /* set up the scratchpad buffer array and scratchpad buffers */
595 xhci_scratchpad_alloc(ctrl);
596
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530597 /* initializing the virtual devices to NULL */
598 for (i = 0; i < MAX_HC_SLOTS; ++i)
599 ctrl->devs[i] = NULL;
600
601 /*
602 * Just Zero'ing this register completely,
603 * or some spurious Device Notification Events
604 * might screw things here.
605 */
606 xhci_writel(&hcor->or_dnctrl, 0x0);
607
608 return 0;
609}
610
611/**
612 * Give the input control context for the passed container context
613 *
614 * @param ctx pointer to the context
615 * @return pointer to the Input control context data
616 */
617struct xhci_input_control_ctx
618 *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
619{
620 BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
621 return (struct xhci_input_control_ctx *)ctx->bytes;
622}
623
624/**
625 * Give the slot context for the passed container context
626 *
627 * @param ctrl Host controller data structure
628 * @param ctx pointer to the context
629 * @return pointer to the slot control context data
630 */
631struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
632 struct xhci_container_ctx *ctx)
633{
634 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
635 return (struct xhci_slot_ctx *)ctx->bytes;
636
637 return (struct xhci_slot_ctx *)
638 (ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)));
639}
640
641/**
642 * Gets the EP context from based on the ep_index
643 *
644 * @param ctrl Host controller data structure
645 * @param ctx context container
646 * @param ep_index index of the endpoint
647 * @return pointer to the End point context
648 */
649struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
650 struct xhci_container_ctx *ctx,
651 unsigned int ep_index)
652{
653 /* increment ep index by offset of start of ep ctx array */
654 ep_index++;
655 if (ctx->type == XHCI_CTX_TYPE_INPUT)
656 ep_index++;
657
658 return (struct xhci_ep_ctx *)
659 (ctx->bytes +
660 (ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))));
661}
662
663/**
664 * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
665 * Useful when you want to change one particular aspect of the endpoint
666 * and then issue a configure endpoint command.
667 *
668 * @param ctrl Host controller data structure
669 * @param in_ctx contains the input context
670 * @param out_ctx contains the input context
671 * @param ep_index index of the end point
672 * @return none
673 */
674void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
675 struct xhci_container_ctx *in_ctx,
676 struct xhci_container_ctx *out_ctx,
677 unsigned int ep_index)
678{
679 struct xhci_ep_ctx *out_ep_ctx;
680 struct xhci_ep_ctx *in_ep_ctx;
681
682 out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
683 in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
684
685 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
686 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
687 in_ep_ctx->deq = out_ep_ctx->deq;
688 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
689}
690
691/**
692 * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
693 * Useful when you want to change one particular aspect of the endpoint
694 * and then issue a configure endpoint command.
695 * Only the context entries field matters, but
696 * we'll copy the whole thing anyway.
697 *
698 * @param ctrl Host controller data structure
699 * @param in_ctx contains the inpout context
700 * @param out_ctx contains the inpout context
701 * @return none
702 */
703void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
704 struct xhci_container_ctx *out_ctx)
705{
706 struct xhci_slot_ctx *in_slot_ctx;
707 struct xhci_slot_ctx *out_slot_ctx;
708
709 in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
710 out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
711
712 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
713 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
714 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
715 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
716}
717
718/**
719 * Setup an xHCI virtual device for a Set Address command
720 *
721 * @param udev pointer to the Device Data Structure
722 * @return returns negative value on failure else 0 on success
723 */
Bin Meng1459ce62017-07-19 21:51:14 +0800724void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
725 struct usb_device *udev, int hop_portnr)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530726{
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530727 struct xhci_virt_device *virt_dev;
728 struct xhci_ep_ctx *ep0_ctx;
729 struct xhci_slot_ctx *slot_ctx;
730 u32 port_num = 0;
731 u64 trb_64 = 0;
Bin Meng1459ce62017-07-19 21:51:14 +0800732 int slot_id = udev->slot_id;
733 int speed = udev->speed;
Bin Meng63c3b3b2017-07-19 21:51:15 +0800734 int route = 0;
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100735#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800736 struct usb_device *dev = udev;
737 struct usb_hub_device *hub;
738#endif
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530739
Simon Glass4ec422c2015-03-25 12:22:51 -0600740 virt_dev = ctrl->devs[slot_id];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530741
742 BUG_ON(!virt_dev);
743
744 /* Extract the EP0 and Slot Ctrl */
745 ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
746 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
747
748 /* Only the control endpoint is valid - one endpoint context */
Bin Meng63c3b3b2017-07-19 21:51:15 +0800749 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
750
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100751#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800752 /* Calculate the route string for this device */
753 port_num = dev->portnr;
754 while (!usb_hub_is_root_hub(dev->dev)) {
755 hub = dev_get_uclass_priv(dev->dev);
756 /*
757 * Each hub in the topology is expected to have no more than
758 * 15 ports in order for the route string of a device to be
759 * unique. SuperSpeed hubs are restricted to only having 15
760 * ports, but FS/LS/HS hubs are not. The xHCI specification
761 * says that if the port number the device is greater than 15,
762 * that portion of the route string shall be set to 15.
763 */
764 if (port_num > 15)
765 port_num = 15;
766 route |= port_num << (hub->hub_depth * 4);
767 dev = dev_get_parent_priv(dev->dev);
768 port_num = dev->portnr;
769 dev = dev_get_parent_priv(dev->dev->parent);
770 }
771
772 debug("route string %x\n", route);
773#endif
774 slot_ctx->dev_info |= route;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530775
Simon Glass4ec422c2015-03-25 12:22:51 -0600776 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530777 case USB_SPEED_SUPER:
778 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
779 break;
780 case USB_SPEED_HIGH:
781 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
782 break;
783 case USB_SPEED_FULL:
784 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
785 break;
786 case USB_SPEED_LOW:
787 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
788 break;
789 default:
790 /* Speed was set earlier, this shouldn't happen. */
791 BUG();
792 }
793
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100794#if CONFIG_IS_ENABLED(DM_USB)
Bin Mengfe3327f2017-07-19 21:51:21 +0800795 /* Set up TT fields to support FS/LS devices */
796 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
Bin Mengbb4b1052017-09-18 06:40:39 -0700797 struct udevice *parent = udev->dev;
798
799 dev = udev;
800 do {
801 port_num = dev->portnr;
802 dev = dev_get_parent_priv(parent);
803 if (usb_hub_is_root_hub(dev->dev))
804 break;
805 parent = dev->dev->parent;
806 } while (dev->speed != USB_SPEED_HIGH);
807
808 if (!usb_hub_is_root_hub(dev->dev)) {
809 hub = dev_get_uclass_priv(dev->dev);
Bin Mengfe3327f2017-07-19 21:51:21 +0800810 if (hub->tt.multi)
811 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Mengbb4b1052017-09-18 06:40:39 -0700812 slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
Bin Mengfe3327f2017-07-19 21:51:21 +0800813 slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
814 }
815 }
816#endif
817
Simon Glass4ec422c2015-03-25 12:22:51 -0600818 port_num = hop_portnr;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530819 debug("port_num = %d\n", port_num);
820
821 slot_ctx->dev_info2 |=
822 cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
823 ROOT_HUB_PORT_SHIFT));
824
825 /* Step 4 - ring already allocated */
826 /* Step 5 */
827 ep0_ctx->ep_info2 = cpu_to_le32(CTRL_EP << EP_TYPE_SHIFT);
Simon Glass4ec422c2015-03-25 12:22:51 -0600828 debug("SPEED = %d\n", speed);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530829
Simon Glass4ec422c2015-03-25 12:22:51 -0600830 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530831 case USB_SPEED_SUPER:
832 ep0_ctx->ep_info2 |= cpu_to_le32(((512 & MAX_PACKET_MASK) <<
833 MAX_PACKET_SHIFT));
834 debug("Setting Packet size = 512bytes\n");
835 break;
836 case USB_SPEED_HIGH:
837 /* USB core guesses at a 64-byte max packet first for FS devices */
838 case USB_SPEED_FULL:
839 ep0_ctx->ep_info2 |= cpu_to_le32(((64 & MAX_PACKET_MASK) <<
840 MAX_PACKET_SHIFT));
841 debug("Setting Packet size = 64bytes\n");
842 break;
843 case USB_SPEED_LOW:
844 ep0_ctx->ep_info2 |= cpu_to_le32(((8 & MAX_PACKET_MASK) <<
845 MAX_PACKET_SHIFT));
846 debug("Setting Packet size = 8bytes\n");
847 break;
848 default:
849 /* New speed? */
850 BUG();
851 }
852
853 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
854 ep0_ctx->ep_info2 |=
855 cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) |
856 ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT));
857
858 trb_64 = (uintptr_t)virt_dev->eps[0].ring->first_seg->trbs;
859 ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
860
Bin Mengc03fb202017-09-18 06:40:50 -0700861 /*
862 * xHCI spec 6.2.3:
863 * software shall set 'Average TRB Length' to 8 for control endpoints.
864 */
865 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
866
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530867 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
868
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300869 xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
870 xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530871}