blob: b002d6f16640f3ced4fa8cfa41f5b701fa420c76 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vivek Gautam5853e132013-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 Gautam5853e132013-09-14 14:02:45 +053014 */
15
16#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070017#include <cpu_func.h>
Simon Glassa5762fe2015-03-25 12:22:53 -060018#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <log.h>
Vivek Gautam5853e132013-09-14 14:02:45 +053020#include <asm/byteorder.h>
21#include <usb.h>
22#include <malloc.h>
23#include <asm/cache.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060024#include <linux/bug.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090025#include <linux/errno.h>
Vivek Gautam5853e132013-09-14 14:02:45 +053026
Jean-Jacques Hiblot1708a122019-09-11 11:33:46 +020027#include <usb/xhci.h>
Vivek Gautam5853e132013-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 Temerkhanov421a5a02015-04-01 17:18:45 +030037void xhci_flush_cache(uintptr_t addr, u32 len)
Vivek Gautam5853e132013-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 Temerkhanov421a5a02015-04-01 17:18:45 +030052void xhci_inval_cache(uintptr_t addr, u32 len)
Vivek Gautam5853e132013-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 Meng209b98d2017-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 Roese543eb122020-07-21 10:46:02 +0200113 free((void *)(uintptr_t)le64_to_cpu(ctrl->scratchpad->sp_array[0]));
Bin Meng209b98d2017-07-19 21:49:55 +0800114 free(ctrl->scratchpad->sp_array);
115 free(ctrl->scratchpad);
116 ctrl->scratchpad = NULL;
117}
118
119/**
Vivek Gautam5853e132013-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 Meng209b98d2017-07-19 21:49:55 +0800179 xhci_scratchpad_free(ctrl);
Vivek Gautam5853e132013-09-14 14:02:45 +0530180 xhci_free_virt_devices(ctrl);
181 free(ctrl->erst.entries);
182 free(ctrl->dcbaa);
Nicolas Saenz Julienne0b803712020-06-29 18:37:25 +0200183 if (reset_valid(&ctrl->reset))
184 reset_free(&ctrl->reset);
Vivek Gautam5853e132013-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 Temerkhanov421a5a02015-04-01 17:18:45 +0300203 xhci_flush_cache((uintptr_t)ptr, size);
Vivek Gautam5853e132013-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) {
Stefan Roeseb5152a62020-07-21 10:46:05 +0200229 val_64 = virt_to_phys(next->trbs);
Stefan Roese543eb122020-07-21 10:46:02 +0200230 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
231 cpu_to_le64(val_64);
Vivek Gautam5853e132013-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;
Chunfeng Yuna826d762020-09-08 18:59:59 +0200239 val |= TRB_TYPE(TRB_LINK);
Vivek Gautam5853e132013-09-14 14:02:45 +0530240 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
241 }
242}
243
244/**
245 * Initialises the Ring's enqueue,dequeue,enq_seg pointers
246 *
247 * @param ring pointer to the RING to be intialised
248 * @return none
249 */
250static void xhci_initialize_ring_info(struct xhci_ring *ring)
251{
252 /*
253 * The ring is empty, so the enqueue pointer == dequeue pointer
254 */
255 ring->enqueue = ring->first_seg->trbs;
256 ring->enq_seg = ring->first_seg;
257 ring->dequeue = ring->enqueue;
258 ring->deq_seg = ring->first_seg;
259
260 /*
261 * The ring is initialized to 0. The producer must write 1 to the
262 * cycle bit to handover ownership of the TRB, so PCS = 1.
263 * The consumer must compare CCS to the cycle bit to
264 * check ownership, so CCS = 1.
265 */
266 ring->cycle_state = 1;
267}
268
269/**
270 * Allocates a generic ring segment from the ring pool, sets the dma address,
271 * initializes the segment to zero, and sets the private next pointer to NULL.
272 * Section 4.11.1.1:
273 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
274 *
275 * @param none
276 * @return pointer to the newly allocated SEGMENT
277 */
278static struct xhci_segment *xhci_segment_alloc(void)
279{
280 struct xhci_segment *seg;
281
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200282 seg = malloc(sizeof(struct xhci_segment));
Vivek Gautam5853e132013-09-14 14:02:45 +0530283 BUG_ON(!seg);
284
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200285 seg->trbs = xhci_malloc(SEGMENT_SIZE);
Vivek Gautam5853e132013-09-14 14:02:45 +0530286
287 seg->next = NULL;
288
289 return seg;
290}
291
292/**
293 * Create a new ring with zero or more segments.
294 * TODO: current code only uses one-time-allocated single-segment rings
295 * of 1KB anyway, so we might as well get rid of all the segment and
296 * linking code (and maybe increase the size a bit, e.g. 4KB).
297 *
298 *
299 * Link each segment together into a ring.
300 * Set the end flag and the cycle toggle bit on the last segment.
301 * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
302 *
303 * @param num_segs number of segments in the ring
304 * @param link_trbs flag to indicate whether to link the trbs or NOT
305 * @return pointer to the newly created RING
306 */
307struct xhci_ring *xhci_ring_alloc(unsigned int num_segs, bool link_trbs)
308{
309 struct xhci_ring *ring;
310 struct xhci_segment *prev;
311
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200312 ring = malloc(sizeof(struct xhci_ring));
Vivek Gautam5853e132013-09-14 14:02:45 +0530313 BUG_ON(!ring);
314
315 if (num_segs == 0)
316 return ring;
317
318 ring->first_seg = xhci_segment_alloc();
319 BUG_ON(!ring->first_seg);
320
321 num_segs--;
322
323 prev = ring->first_seg;
324 while (num_segs > 0) {
325 struct xhci_segment *next;
326
327 next = xhci_segment_alloc();
328 BUG_ON(!next);
329
330 xhci_link_segments(prev, next, link_trbs);
331
332 prev = next;
333 num_segs--;
334 }
335 xhci_link_segments(prev, ring->first_seg, link_trbs);
336 if (link_trbs) {
337 /* See section 4.9.2.1 and 6.4.4.1 */
338 prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
339 cpu_to_le32(LINK_TOGGLE);
340 }
341 xhci_initialize_ring_info(ring);
342
343 return ring;
344}
345
346/**
Bin Meng209b98d2017-07-19 21:49:55 +0800347 * Set up the scratchpad buffer array and scratchpad buffers
348 *
349 * @ctrl host controller data structure
350 * @return -ENOMEM if buffer allocation fails, 0 on success
351 */
352static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
353{
354 struct xhci_hccr *hccr = ctrl->hccr;
355 struct xhci_hcor *hcor = ctrl->hcor;
356 struct xhci_scratchpad *scratchpad;
357 int num_sp;
358 uint32_t page_size;
359 void *buf;
360 int i;
361
362 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
363 if (!num_sp)
364 return 0;
365
366 scratchpad = malloc(sizeof(*scratchpad));
367 if (!scratchpad)
368 goto fail_sp;
369 ctrl->scratchpad = scratchpad;
370
371 scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
372 if (!scratchpad->sp_array)
373 goto fail_sp2;
374 ctrl->dcbaa->dev_context_ptrs[0] =
375 cpu_to_le64((uintptr_t)scratchpad->sp_array);
376
Ye Li8c6cc712019-01-07 02:45:46 +0000377 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
378 sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
379
Bin Meng209b98d2017-07-19 21:49:55 +0800380 page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
381 for (i = 0; i < 16; i++) {
382 if ((0x1 & page_size) != 0)
383 break;
384 page_size = page_size >> 1;
385 }
386 BUG_ON(i == 16);
387
388 page_size = 1 << (i + 12);
389 buf = memalign(page_size, num_sp * page_size);
390 if (!buf)
391 goto fail_sp3;
392 memset(buf, '\0', num_sp * page_size);
393 xhci_flush_cache((uintptr_t)buf, num_sp * page_size);
394
395 for (i = 0; i < num_sp; i++) {
396 uintptr_t ptr = (uintptr_t)buf + i * page_size;
397 scratchpad->sp_array[i] = cpu_to_le64(ptr);
398 }
399
Sylwester Nawrocki61293f52020-05-25 13:39:51 +0200400 xhci_flush_cache((uintptr_t)scratchpad->sp_array,
401 sizeof(u64) * num_sp);
402
Bin Meng209b98d2017-07-19 21:49:55 +0800403 return 0;
404
405fail_sp3:
406 free(scratchpad->sp_array);
407
408fail_sp2:
409 free(scratchpad);
410 ctrl->scratchpad = NULL;
411
412fail_sp:
413 return -ENOMEM;
414}
415
416/**
Vivek Gautam5853e132013-09-14 14:02:45 +0530417 * Allocates the Container context
418 *
419 * @param ctrl Host controller data structure
420 * @param type type of XHCI Container Context
421 * @return NULL if failed else pointer to the context on success
422 */
423static struct xhci_container_ctx
424 *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
425{
426 struct xhci_container_ctx *ctx;
427
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200428 ctx = malloc(sizeof(struct xhci_container_ctx));
Vivek Gautam5853e132013-09-14 14:02:45 +0530429 BUG_ON(!ctx);
430
431 BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
432 ctx->type = type;
433 ctx->size = (MAX_EP_CTX_NUM + 1) *
434 CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
435 if (type == XHCI_CTX_TYPE_INPUT)
436 ctx->size += CTX_SIZE(readl(&ctrl->hccr->cr_hccparams));
437
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200438 ctx->bytes = xhci_malloc(ctx->size);
Vivek Gautam5853e132013-09-14 14:02:45 +0530439
440 return ctx;
441}
442
443/**
444 * Allocating virtual device
445 *
446 * @param udev pointer to USB deivce structure
447 * @return 0 on success else -1 on failure
448 */
Simon Glass7e0c5ee2015-03-25 12:22:50 -0600449int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
Vivek Gautam5853e132013-09-14 14:02:45 +0530450{
451 u64 byte_64 = 0;
Vivek Gautam5853e132013-09-14 14:02:45 +0530452 struct xhci_virt_device *virt_dev;
Vivek Gautam5853e132013-09-14 14:02:45 +0530453
454 /* Slot ID 0 is reserved */
455 if (ctrl->devs[slot_id]) {
456 printf("Virt dev for slot[%d] already allocated\n", slot_id);
457 return -EEXIST;
458 }
459
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200460 ctrl->devs[slot_id] = malloc(sizeof(struct xhci_virt_device));
Vivek Gautam5853e132013-09-14 14:02:45 +0530461
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
Stefan Roeseb5152a62020-07-21 10:46:05 +0200489 byte_64 = virt_to_phys(virt_dev->out_ctx->bytes);
Vivek Gautam5853e132013-09-14 14:02:45 +0530490
491 /* Point to output device context in dcbaa. */
Stefan Roese543eb122020-07-21 10:46:02 +0200492 ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
Vivek Gautam5853e132013-09-14 14:02:45 +0530493
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300494 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
495 sizeof(__le64));
Vivek Gautam5853e132013-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;
Stefan Roeseb5152a62020-07-21 10:46:05 +0200514 uint64_t deq;
Vivek Gautam5853e132013-09-14 14:02:45 +0530515 int i;
516 struct xhci_segment *seg;
517
518 /* DCBAA initialization */
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200519 ctrl->dcbaa = xhci_malloc(sizeof(struct xhci_device_context_array));
Vivek Gautam5853e132013-09-14 14:02:45 +0530520 if (ctrl->dcbaa == NULL) {
521 puts("unable to allocate DCBA\n");
522 return -ENOMEM;
523 }
524
Stefan Roeseb5152a62020-07-21 10:46:05 +0200525 val_64 = virt_to_phys(ctrl->dcbaa);
Vivek Gautam5853e132013-09-14 14:02:45 +0530526 /* Set the pointer in DCBAA register */
527 xhci_writeq(&hcor->or_dcbaap, val_64);
528
529 /* Command ring control pointer register initialization */
530 ctrl->cmd_ring = xhci_ring_alloc(1, true);
531
532 /* Set the address in the Command Ring Control register */
Stefan Roeseb5152a62020-07-21 10:46:05 +0200533 trb_64 = virt_to_phys(ctrl->cmd_ring->first_seg->trbs);
Vivek Gautam5853e132013-09-14 14:02:45 +0530534 val_64 = xhci_readq(&hcor->or_crcr);
535 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
536 (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
537 ctrl->cmd_ring->cycle_state;
538 xhci_writeq(&hcor->or_crcr, val_64);
539
540 /* write the address of db register */
541 val = xhci_readl(&hccr->cr_dboff);
542 val &= DBOFF_MASK;
543 ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
544
545 /* write the address of runtime register */
546 val = xhci_readl(&hccr->cr_rtsoff);
547 val &= RTSOFF_MASK;
548 ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
549
550 /* writting the address of ir_set structure */
551 ctrl->ir_set = &ctrl->run_regs->ir_set[0];
552
553 /* Event ring does not maintain link TRB */
554 ctrl->event_ring = xhci_ring_alloc(ERST_NUM_SEGS, false);
Heinrich Schuchardt3fade882020-09-29 22:03:01 +0200555 ctrl->erst.entries = xhci_malloc(sizeof(struct xhci_erst_entry) *
556 ERST_NUM_SEGS);
Vivek Gautam5853e132013-09-14 14:02:45 +0530557
558 ctrl->erst.num_entries = ERST_NUM_SEGS;
559
560 for (val = 0, seg = ctrl->event_ring->first_seg;
561 val < ERST_NUM_SEGS;
562 val++) {
Stefan Roeseb5152a62020-07-21 10:46:05 +0200563 trb_64 = virt_to_phys(seg->trbs);
Vivek Gautam5853e132013-09-14 14:02:45 +0530564 struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
Stefan Roese61a1acb2020-07-21 10:46:03 +0200565 entry->seg_addr = cpu_to_le64(trb_64);
Vivek Gautam5853e132013-09-14 14:02:45 +0530566 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
567 entry->rsvd = 0;
568 seg = seg->next;
569 }
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300570 xhci_flush_cache((uintptr_t)ctrl->erst.entries,
571 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
Vivek Gautam5853e132013-09-14 14:02:45 +0530572
Stefan Roeseb5152a62020-07-21 10:46:05 +0200573 deq = virt_to_phys(ctrl->event_ring->dequeue);
Vivek Gautam5853e132013-09-14 14:02:45 +0530574
575 /* Update HC event ring dequeue pointer */
576 xhci_writeq(&ctrl->ir_set->erst_dequeue,
577 (u64)deq & (u64)~ERST_PTR_MASK);
578
579 /* set ERST count with the number of entries in the segment table */
580 val = xhci_readl(&ctrl->ir_set->erst_size);
581 val &= ERST_SIZE_MASK;
582 val |= ERST_NUM_SEGS;
583 xhci_writel(&ctrl->ir_set->erst_size, val);
584
585 /* this is the event ring segment table pointer */
586 val_64 = xhci_readq(&ctrl->ir_set->erst_base);
587 val_64 &= ERST_PTR_MASK;
Stefan Roeseb5152a62020-07-21 10:46:05 +0200588 val_64 |= virt_to_phys(ctrl->erst.entries) & ~ERST_PTR_MASK;
Vivek Gautam5853e132013-09-14 14:02:45 +0530589
590 xhci_writeq(&ctrl->ir_set->erst_base, val_64);
591
Bin Meng209b98d2017-07-19 21:49:55 +0800592 /* set up the scratchpad buffer array and scratchpad buffers */
593 xhci_scratchpad_alloc(ctrl);
594
Vivek Gautam5853e132013-09-14 14:02:45 +0530595 /* initializing the virtual devices to NULL */
596 for (i = 0; i < MAX_HC_SLOTS; ++i)
597 ctrl->devs[i] = NULL;
598
599 /*
600 * Just Zero'ing this register completely,
601 * or some spurious Device Notification Events
602 * might screw things here.
603 */
604 xhci_writel(&hcor->or_dnctrl, 0x0);
605
606 return 0;
607}
608
609/**
610 * Give the input control context for the passed container context
611 *
612 * @param ctx pointer to the context
613 * @return pointer to the Input control context data
614 */
615struct xhci_input_control_ctx
616 *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
617{
618 BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
619 return (struct xhci_input_control_ctx *)ctx->bytes;
620}
621
622/**
623 * Give the slot context for the passed container context
624 *
625 * @param ctrl Host controller data structure
626 * @param ctx pointer to the context
627 * @return pointer to the slot control context data
628 */
629struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
630 struct xhci_container_ctx *ctx)
631{
632 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
633 return (struct xhci_slot_ctx *)ctx->bytes;
634
635 return (struct xhci_slot_ctx *)
636 (ctx->bytes + CTX_SIZE(readl(&ctrl->hccr->cr_hccparams)));
637}
638
639/**
640 * Gets the EP context from based on the ep_index
641 *
642 * @param ctrl Host controller data structure
643 * @param ctx context container
644 * @param ep_index index of the endpoint
645 * @return pointer to the End point context
646 */
647struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
648 struct xhci_container_ctx *ctx,
649 unsigned int ep_index)
650{
651 /* increment ep index by offset of start of ep ctx array */
652 ep_index++;
653 if (ctx->type == XHCI_CTX_TYPE_INPUT)
654 ep_index++;
655
656 return (struct xhci_ep_ctx *)
657 (ctx->bytes +
658 (ep_index * CTX_SIZE(readl(&ctrl->hccr->cr_hccparams))));
659}
660
661/**
662 * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
663 * Useful when you want to change one particular aspect of the endpoint
664 * and then issue a configure endpoint command.
665 *
666 * @param ctrl Host controller data structure
667 * @param in_ctx contains the input context
668 * @param out_ctx contains the input context
669 * @param ep_index index of the end point
670 * @return none
671 */
672void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
673 struct xhci_container_ctx *in_ctx,
674 struct xhci_container_ctx *out_ctx,
675 unsigned int ep_index)
676{
677 struct xhci_ep_ctx *out_ep_ctx;
678 struct xhci_ep_ctx *in_ep_ctx;
679
680 out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
681 in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
682
683 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
684 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
685 in_ep_ctx->deq = out_ep_ctx->deq;
686 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
687}
688
689/**
690 * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
691 * Useful when you want to change one particular aspect of the endpoint
692 * and then issue a configure endpoint command.
693 * Only the context entries field matters, but
694 * we'll copy the whole thing anyway.
695 *
696 * @param ctrl Host controller data structure
697 * @param in_ctx contains the inpout context
698 * @param out_ctx contains the inpout context
699 * @return none
700 */
701void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
702 struct xhci_container_ctx *out_ctx)
703{
704 struct xhci_slot_ctx *in_slot_ctx;
705 struct xhci_slot_ctx *out_slot_ctx;
706
707 in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
708 out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
709
710 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
711 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
712 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
713 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
714}
715
716/**
717 * Setup an xHCI virtual device for a Set Address command
718 *
719 * @param udev pointer to the Device Data Structure
720 * @return returns negative value on failure else 0 on success
721 */
Bin Mengdaec4692017-07-19 21:51:14 +0800722void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
723 struct usb_device *udev, int hop_portnr)
Vivek Gautam5853e132013-09-14 14:02:45 +0530724{
Vivek Gautam5853e132013-09-14 14:02:45 +0530725 struct xhci_virt_device *virt_dev;
726 struct xhci_ep_ctx *ep0_ctx;
727 struct xhci_slot_ctx *slot_ctx;
728 u32 port_num = 0;
729 u64 trb_64 = 0;
Bin Mengdaec4692017-07-19 21:51:14 +0800730 int slot_id = udev->slot_id;
731 int speed = udev->speed;
Bin Meng493b8dd2017-07-19 21:51:15 +0800732 int route = 0;
Sven Schwermerfd09c202018-11-21 08:43:56 +0100733#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng493b8dd2017-07-19 21:51:15 +0800734 struct usb_device *dev = udev;
735 struct usb_hub_device *hub;
736#endif
Vivek Gautam5853e132013-09-14 14:02:45 +0530737
Simon Glass5dd75e32015-03-25 12:22:51 -0600738 virt_dev = ctrl->devs[slot_id];
Vivek Gautam5853e132013-09-14 14:02:45 +0530739
740 BUG_ON(!virt_dev);
741
742 /* Extract the EP0 and Slot Ctrl */
743 ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
744 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
745
746 /* Only the control endpoint is valid - one endpoint context */
Bin Meng493b8dd2017-07-19 21:51:15 +0800747 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
748
Sven Schwermerfd09c202018-11-21 08:43:56 +0100749#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng493b8dd2017-07-19 21:51:15 +0800750 /* Calculate the route string for this device */
751 port_num = dev->portnr;
752 while (!usb_hub_is_root_hub(dev->dev)) {
753 hub = dev_get_uclass_priv(dev->dev);
754 /*
755 * Each hub in the topology is expected to have no more than
756 * 15 ports in order for the route string of a device to be
757 * unique. SuperSpeed hubs are restricted to only having 15
758 * ports, but FS/LS/HS hubs are not. The xHCI specification
759 * says that if the port number the device is greater than 15,
760 * that portion of the route string shall be set to 15.
761 */
762 if (port_num > 15)
763 port_num = 15;
764 route |= port_num << (hub->hub_depth * 4);
765 dev = dev_get_parent_priv(dev->dev);
766 port_num = dev->portnr;
767 dev = dev_get_parent_priv(dev->dev->parent);
768 }
769
770 debug("route string %x\n", route);
771#endif
Stefan Roese543eb122020-07-21 10:46:02 +0200772 slot_ctx->dev_info |= cpu_to_le32(route);
Vivek Gautam5853e132013-09-14 14:02:45 +0530773
Simon Glass5dd75e32015-03-25 12:22:51 -0600774 switch (speed) {
Vivek Gautam5853e132013-09-14 14:02:45 +0530775 case USB_SPEED_SUPER:
776 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
777 break;
778 case USB_SPEED_HIGH:
779 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
780 break;
781 case USB_SPEED_FULL:
782 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
783 break;
784 case USB_SPEED_LOW:
785 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
786 break;
787 default:
788 /* Speed was set earlier, this shouldn't happen. */
789 BUG();
790 }
791
Sven Schwermerfd09c202018-11-21 08:43:56 +0100792#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng78e30982017-07-19 21:51:21 +0800793 /* Set up TT fields to support FS/LS devices */
794 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
Bin Meng8a0e6d82017-09-18 06:40:39 -0700795 struct udevice *parent = udev->dev;
796
797 dev = udev;
798 do {
799 port_num = dev->portnr;
800 dev = dev_get_parent_priv(parent);
801 if (usb_hub_is_root_hub(dev->dev))
802 break;
803 parent = dev->dev->parent;
804 } while (dev->speed != USB_SPEED_HIGH);
805
806 if (!usb_hub_is_root_hub(dev->dev)) {
807 hub = dev_get_uclass_priv(dev->dev);
Bin Meng78e30982017-07-19 21:51:21 +0800808 if (hub->tt.multi)
809 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Meng8a0e6d82017-09-18 06:40:39 -0700810 slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
Bin Meng78e30982017-07-19 21:51:21 +0800811 slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
812 }
813 }
814#endif
815
Simon Glass5dd75e32015-03-25 12:22:51 -0600816 port_num = hop_portnr;
Vivek Gautam5853e132013-09-14 14:02:45 +0530817 debug("port_num = %d\n", port_num);
818
819 slot_ctx->dev_info2 |=
820 cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
821 ROOT_HUB_PORT_SHIFT));
822
823 /* Step 4 - ring already allocated */
824 /* Step 5 */
Chunfeng Yun23a54cc2020-09-08 19:00:02 +0200825 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
Simon Glass5dd75e32015-03-25 12:22:51 -0600826 debug("SPEED = %d\n", speed);
Vivek Gautam5853e132013-09-14 14:02:45 +0530827
Simon Glass5dd75e32015-03-25 12:22:51 -0600828 switch (speed) {
Vivek Gautam5853e132013-09-14 14:02:45 +0530829 case USB_SPEED_SUPER:
Chunfeng Yun23a54cc2020-09-08 19:00:02 +0200830 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(512));
Vivek Gautam5853e132013-09-14 14:02:45 +0530831 debug("Setting Packet size = 512bytes\n");
832 break;
833 case USB_SPEED_HIGH:
834 /* USB core guesses at a 64-byte max packet first for FS devices */
835 case USB_SPEED_FULL:
Chunfeng Yun23a54cc2020-09-08 19:00:02 +0200836 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(64));
Vivek Gautam5853e132013-09-14 14:02:45 +0530837 debug("Setting Packet size = 64bytes\n");
838 break;
839 case USB_SPEED_LOW:
Chunfeng Yun23a54cc2020-09-08 19:00:02 +0200840 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(8));
Vivek Gautam5853e132013-09-14 14:02:45 +0530841 debug("Setting Packet size = 8bytes\n");
842 break;
843 default:
844 /* New speed? */
845 BUG();
846 }
847
848 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
Chunfeng Yun23a54cc2020-09-08 19:00:02 +0200849 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3));
Vivek Gautam5853e132013-09-14 14:02:45 +0530850
Stefan Roeseb5152a62020-07-21 10:46:05 +0200851 trb_64 = virt_to_phys(virt_dev->eps[0].ring->first_seg->trbs);
Vivek Gautam5853e132013-09-14 14:02:45 +0530852 ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
853
Bin Mengfae35852017-09-18 06:40:50 -0700854 /*
855 * xHCI spec 6.2.3:
856 * software shall set 'Average TRB Length' to 8 for control endpoints.
857 */
858 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
859
Vivek Gautam5853e132013-09-14 14:02:45 +0530860 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
861
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300862 xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
863 xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
Vivek Gautam5853e132013-09-14 14:02:45 +0530864}