blob: 045b0fba81281b5ae36e8e12db2abc82f3ea6bce [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
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Simon Glass49b41832015-03-25 12:22:53 -060017#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053019#include <asm/byteorder.h>
20#include <usb.h>
21#include <malloc.h>
22#include <asm/cache.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060023#include <linux/bug.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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010034 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053035 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010049 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053050 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010064 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053065 */
Mark Kettenisfac410c2023-01-21 20:27:55 +010066static void xhci_segment_free(struct xhci_ctrl *ctrl, struct xhci_segment *seg)
Vivek Gautam4912dcc2013-09-14 14:02:45 +053067{
Mark Kettenisfac410c2023-01-21 20:27:55 +010068 xhci_dma_unmap(ctrl, seg->dma, SEGMENT_SIZE);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053069 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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010079 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053080 */
Mark Kettenisfac410c2023-01-21 20:27:55 +010081static void xhci_ring_free(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
Vivek Gautam4912dcc2013-09-14 14:02:45 +053082{
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;
Mark Kettenisfac410c2023-01-21 20:27:55 +010092 xhci_segment_free(ctrl, seg);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053093 seg = next;
94 }
Mark Kettenisfac410c2023-01-21 20:27:55 +010095 xhci_segment_free(ctrl, first_seg);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053096
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100104 * Return: none
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800105 */
106static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
107{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100108 struct xhci_hccr *hccr = ctrl->hccr;
109 int num_sp;
110
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800111 if (!ctrl->scratchpad)
112 return;
113
Mark Kettenisfac410c2023-01-21 20:27:55 +0100114 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
115 xhci_dma_unmap(ctrl, ctrl->scratchpad->sp_array[0],
116 num_sp * ctrl->page_size);
117 xhci_dma_unmap(ctrl, ctrl->dcbaa->dev_context_ptrs[0],
118 num_sp * sizeof(u64));
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800119 ctrl->dcbaa->dev_context_ptrs[0] = 0;
120
Mark Kettenisfac410c2023-01-21 20:27:55 +0100121 free(ctrl->scratchpad->scratchpad);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800122 free(ctrl->scratchpad->sp_array);
123 free(ctrl->scratchpad);
124 ctrl->scratchpad = NULL;
125}
126
127/**
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530128 * frees the "xhci_container_ctx" pointer passed
129 *
130 * @param ptr pointer to "xhci_container_ctx" to be freed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100131 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530132 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100133static void xhci_free_container_ctx(struct xhci_ctrl *ctrl,
134 struct xhci_container_ctx *ctx)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530135{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100136 xhci_dma_unmap(ctrl, ctx->dma, ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530137 free(ctx->bytes);
138 free(ctx);
139}
140
141/**
142 * frees the virtual devices for "xhci_ctrl" pointer passed
143 *
144 * @param ptr pointer to "xhci_ctrl" whose virtual devices are to be freed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100145 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530146 */
147static void xhci_free_virt_devices(struct xhci_ctrl *ctrl)
148{
149 int i;
150 int slot_id;
151 struct xhci_virt_device *virt_dev;
152
153 /*
154 * refactored here to loop through all virt_dev
155 * Slot ID 0 is reserved
156 */
157 for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) {
158 virt_dev = ctrl->devs[slot_id];
159 if (!virt_dev)
160 continue;
161
162 ctrl->dcbaa->dev_context_ptrs[slot_id] = 0;
163
164 for (i = 0; i < 31; ++i)
165 if (virt_dev->eps[i].ring)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100166 xhci_ring_free(ctrl, virt_dev->eps[i].ring);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530167
168 if (virt_dev->in_ctx)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100169 xhci_free_container_ctx(ctrl, virt_dev->in_ctx);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530170 if (virt_dev->out_ctx)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100171 xhci_free_container_ctx(ctrl, virt_dev->out_ctx);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530172
173 free(virt_dev);
174 /* make sure we are pointing to NULL */
175 ctrl->devs[slot_id] = NULL;
176 }
177}
178
179/**
180 * frees all the memory allocated
181 *
182 * @param ptr pointer to "xhci_ctrl" to be cleaned up
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100183 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530184 */
185void xhci_cleanup(struct xhci_ctrl *ctrl)
186{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100187 xhci_ring_free(ctrl, ctrl->event_ring);
188 xhci_ring_free(ctrl, ctrl->cmd_ring);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800189 xhci_scratchpad_free(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530190 xhci_free_virt_devices(ctrl);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100191 xhci_dma_unmap(ctrl, ctrl->erst.erst_dma_addr,
192 sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530193 free(ctrl->erst.entries);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100194 xhci_dma_unmap(ctrl, ctrl->dcbaa->dma,
195 sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530196 free(ctrl->dcbaa);
197 memset(ctrl, '\0', sizeof(struct xhci_ctrl));
198}
199
200/**
201 * Malloc the aligned memory
202 *
203 * @param size size of memory to be allocated
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100204 * Return: allocates the memory and returns the aligned pointer
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530205 */
206static void *xhci_malloc(unsigned int size)
207{
208 void *ptr;
209 size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
210
211 ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
212 BUG_ON(!ptr);
213 memset(ptr, '\0', size);
214
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300215 xhci_flush_cache((uintptr_t)ptr, size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530216
217 return ptr;
218}
219
220/**
221 * Make the prev segment point to the next segment.
222 * Change the last TRB in the prev segment to be a Link TRB which points to the
223 * address of the next segment. The caller needs to set any Link TRB
224 * related flags, such as End TRB, Toggle Cycle, and no snoop.
225 *
226 * @param prev pointer to the previous segment
227 * @param next pointer to the next segment
228 * @param link_trbs flag to indicate whether to link the trbs or NOT
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100229 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530230 */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100231static void xhci_link_segments(struct xhci_ctrl *ctrl, struct xhci_segment *prev,
232 struct xhci_segment *next, bool link_trbs)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530233{
234 u32 val;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530235
236 if (!prev || !next)
237 return;
238 prev->next = next;
239 if (link_trbs) {
Stefan Roesecb570862020-07-21 10:46:02 +0200240 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
Mark Kettenisfac410c2023-01-21 20:27:55 +0100241 cpu_to_le64(next->dma);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530242
243 /*
244 * Set the last TRB in the segment to
245 * have a TRB type ID of Link TRB
246 */
247 val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
248 val &= ~TRB_TYPE_BITMASK;
developer497dcfa2020-09-08 18:59:59 +0200249 val |= TRB_TYPE(TRB_LINK);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530250 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
251 }
252}
253
254/**
255 * Initialises the Ring's enqueue,dequeue,enq_seg pointers
256 *
257 * @param ring pointer to the RING to be intialised
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100258 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530259 */
260static void xhci_initialize_ring_info(struct xhci_ring *ring)
261{
262 /*
263 * The ring is empty, so the enqueue pointer == dequeue pointer
264 */
265 ring->enqueue = ring->first_seg->trbs;
266 ring->enq_seg = ring->first_seg;
267 ring->dequeue = ring->enqueue;
268 ring->deq_seg = ring->first_seg;
269
270 /*
271 * The ring is initialized to 0. The producer must write 1 to the
272 * cycle bit to handover ownership of the TRB, so PCS = 1.
273 * The consumer must compare CCS to the cycle bit to
274 * check ownership, so CCS = 1.
275 */
276 ring->cycle_state = 1;
277}
278
279/**
280 * Allocates a generic ring segment from the ring pool, sets the dma address,
281 * initializes the segment to zero, and sets the private next pointer to NULL.
282 * Section 4.11.1.1:
283 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
284 *
285 * @param none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100286 * Return: pointer to the newly allocated SEGMENT
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530287 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100288static struct xhci_segment *xhci_segment_alloc(struct xhci_ctrl *ctrl)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530289{
290 struct xhci_segment *seg;
291
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200292 seg = malloc(sizeof(struct xhci_segment));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530293 BUG_ON(!seg);
294
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200295 seg->trbs = xhci_malloc(SEGMENT_SIZE);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100296 seg->dma = xhci_dma_map(ctrl, seg->trbs, SEGMENT_SIZE);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530297
298 seg->next = NULL;
299
300 return seg;
301}
302
303/**
304 * Create a new ring with zero or more segments.
305 * TODO: current code only uses one-time-allocated single-segment rings
306 * of 1KB anyway, so we might as well get rid of all the segment and
307 * linking code (and maybe increase the size a bit, e.g. 4KB).
308 *
309 *
310 * Link each segment together into a ring.
311 * Set the end flag and the cycle toggle bit on the last segment.
312 * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
313 *
314 * @param num_segs number of segments in the ring
315 * @param link_trbs flag to indicate whether to link the trbs or NOT
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100316 * Return: pointer to the newly created RING
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530317 */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100318struct xhci_ring *xhci_ring_alloc(struct xhci_ctrl *ctrl, unsigned int num_segs,
319 bool link_trbs)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530320{
321 struct xhci_ring *ring;
322 struct xhci_segment *prev;
323
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200324 ring = malloc(sizeof(struct xhci_ring));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530325 BUG_ON(!ring);
326
327 if (num_segs == 0)
328 return ring;
329
Mark Kettenisfac410c2023-01-21 20:27:55 +0100330 ring->first_seg = xhci_segment_alloc(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530331 BUG_ON(!ring->first_seg);
332
333 num_segs--;
334
335 prev = ring->first_seg;
336 while (num_segs > 0) {
337 struct xhci_segment *next;
338
Mark Kettenisfac410c2023-01-21 20:27:55 +0100339 next = xhci_segment_alloc(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530340 BUG_ON(!next);
341
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100342 xhci_link_segments(ctrl, prev, next, link_trbs);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530343
344 prev = next;
345 num_segs--;
346 }
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100347 xhci_link_segments(ctrl, prev, ring->first_seg, link_trbs);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530348 if (link_trbs) {
349 /* See section 4.9.2.1 and 6.4.4.1 */
350 prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
351 cpu_to_le32(LINK_TOGGLE);
352 }
353 xhci_initialize_ring_info(ring);
354
355 return ring;
356}
357
358/**
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800359 * Set up the scratchpad buffer array and scratchpad buffers
360 *
361 * @ctrl host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100362 * Return: -ENOMEM if buffer allocation fails, 0 on success
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800363 */
364static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
365{
366 struct xhci_hccr *hccr = ctrl->hccr;
367 struct xhci_hcor *hcor = ctrl->hcor;
368 struct xhci_scratchpad *scratchpad;
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100369 uint64_t val_64;
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800370 int num_sp;
371 uint32_t page_size;
372 void *buf;
373 int i;
374
375 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
376 if (!num_sp)
377 return 0;
378
379 scratchpad = malloc(sizeof(*scratchpad));
380 if (!scratchpad)
381 goto fail_sp;
382 ctrl->scratchpad = scratchpad;
383
384 scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
385 if (!scratchpad->sp_array)
386 goto fail_sp2;
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100387
Mark Kettenisfac410c2023-01-21 20:27:55 +0100388 val_64 = xhci_dma_map(ctrl, scratchpad->sp_array,
389 num_sp * sizeof(u64));
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100390 ctrl->dcbaa->dev_context_ptrs[0] = cpu_to_le64(val_64);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800391
Ye Li4bda7182019-01-07 02:45:46 +0000392 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
393 sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
394
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800395 page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
396 for (i = 0; i < 16; i++) {
397 if ((0x1 & page_size) != 0)
398 break;
399 page_size = page_size >> 1;
400 }
401 BUG_ON(i == 16);
402
Mark Kettenisfac410c2023-01-21 20:27:55 +0100403 ctrl->page_size = 1 << (i + 12);
404 buf = memalign(ctrl->page_size, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800405 if (!buf)
406 goto fail_sp3;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100407 memset(buf, '\0', num_sp * ctrl->page_size);
408 xhci_flush_cache((uintptr_t)buf, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800409
Mark Kettenisfac410c2023-01-21 20:27:55 +0100410 scratchpad->scratchpad = buf;
411 val_64 = xhci_dma_map(ctrl, buf, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800412 for (i = 0; i < num_sp; i++) {
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100413 scratchpad->sp_array[i] = cpu_to_le64(val_64);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100414 val_64 += ctrl->page_size;
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800415 }
416
Sylwester Nawrockiffedc752020-05-25 13:39:51 +0200417 xhci_flush_cache((uintptr_t)scratchpad->sp_array,
418 sizeof(u64) * num_sp);
419
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800420 return 0;
421
422fail_sp3:
423 free(scratchpad->sp_array);
424
425fail_sp2:
426 free(scratchpad);
427 ctrl->scratchpad = NULL;
428
429fail_sp:
430 return -ENOMEM;
431}
432
433/**
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530434 * Allocates the Container context
435 *
436 * @param ctrl Host controller data structure
437 * @param type type of XHCI Container Context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100438 * Return: NULL if failed else pointer to the context on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530439 */
440static struct xhci_container_ctx
441 *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
442{
443 struct xhci_container_ctx *ctx;
444
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200445 ctx = malloc(sizeof(struct xhci_container_ctx));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530446 BUG_ON(!ctx);
447
448 BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
449 ctx->type = type;
450 ctx->size = (MAX_EP_CTX_NUM + 1) *
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200451 CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530452 if (type == XHCI_CTX_TYPE_INPUT)
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200453 ctx->size += CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530454
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200455 ctx->bytes = xhci_malloc(ctx->size);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100456 ctx->dma = xhci_dma_map(ctrl, ctx->bytes, ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530457
458 return ctx;
459}
460
461/**
462 * Allocating virtual device
463 *
464 * @param udev pointer to USB deivce structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100465 * Return: 0 on success else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530466 */
Simon Glass88a37842015-03-25 12:22:50 -0600467int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530468{
469 u64 byte_64 = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530470 struct xhci_virt_device *virt_dev;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530471
472 /* Slot ID 0 is reserved */
473 if (ctrl->devs[slot_id]) {
474 printf("Virt dev for slot[%d] already allocated\n", slot_id);
475 return -EEXIST;
476 }
477
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200478 ctrl->devs[slot_id] = malloc(sizeof(struct xhci_virt_device));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530479
480 if (!ctrl->devs[slot_id]) {
481 puts("Failed to allocate virtual device\n");
482 return -ENOMEM;
483 }
484
485 memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
486 virt_dev = ctrl->devs[slot_id];
487
488 /* Allocate the (output) device context that will be used in the HC. */
489 virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
490 XHCI_CTX_TYPE_DEVICE);
491 if (!virt_dev->out_ctx) {
492 puts("Failed to allocate out context for virt dev\n");
493 return -ENOMEM;
494 }
495
496 /* Allocate the (input) device context for address device command */
497 virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
498 XHCI_CTX_TYPE_INPUT);
499 if (!virt_dev->in_ctx) {
500 puts("Failed to allocate in context for virt dev\n");
501 return -ENOMEM;
502 }
503
504 /* Allocate endpoint 0 ring */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100505 virt_dev->eps[0].ring = xhci_ring_alloc(ctrl, 1, true);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530506
Mark Kettenisfac410c2023-01-21 20:27:55 +0100507 byte_64 = virt_dev->out_ctx->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530508
509 /* Point to output device context in dcbaa. */
Stefan Roesecb570862020-07-21 10:46:02 +0200510 ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530511
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300512 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
513 sizeof(__le64));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530514 return 0;
515}
516
517/**
518 * Allocates the necessary data structures
519 * for XHCI host controller
520 *
521 * @param ctrl Host controller data structure
522 * @param hccr pointer to HOST Controller Control Registers
523 * @param hcor pointer to HOST Controller Operational Registers
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100524 * Return: 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530525 */
526int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
527 struct xhci_hcor *hcor)
528{
529 uint64_t val_64;
530 uint64_t trb_64;
531 uint32_t val;
Stefan Roesecaf8cae2020-07-21 10:46:05 +0200532 uint64_t deq;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530533 int i;
534 struct xhci_segment *seg;
535
536 /* DCBAA initialization */
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200537 ctrl->dcbaa = xhci_malloc(sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530538 if (ctrl->dcbaa == NULL) {
539 puts("unable to allocate DCBA\n");
540 return -ENOMEM;
541 }
542
Mark Kettenisfac410c2023-01-21 20:27:55 +0100543 ctrl->dcbaa->dma = xhci_dma_map(ctrl, ctrl->dcbaa,
544 sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530545 /* Set the pointer in DCBAA register */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100546 xhci_writeq(&hcor->or_dcbaap, ctrl->dcbaa->dma);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530547
548 /* Command ring control pointer register initialization */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100549 ctrl->cmd_ring = xhci_ring_alloc(ctrl, 1, true);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530550
551 /* Set the address in the Command Ring Control register */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100552 trb_64 = ctrl->cmd_ring->first_seg->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530553 val_64 = xhci_readq(&hcor->or_crcr);
554 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
555 (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
556 ctrl->cmd_ring->cycle_state;
557 xhci_writeq(&hcor->or_crcr, val_64);
558
559 /* write the address of db register */
560 val = xhci_readl(&hccr->cr_dboff);
561 val &= DBOFF_MASK;
562 ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
563
564 /* write the address of runtime register */
565 val = xhci_readl(&hccr->cr_rtsoff);
566 val &= RTSOFF_MASK;
567 ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
568
569 /* writting the address of ir_set structure */
570 ctrl->ir_set = &ctrl->run_regs->ir_set[0];
571
572 /* Event ring does not maintain link TRB */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100573 ctrl->event_ring = xhci_ring_alloc(ctrl, ERST_NUM_SEGS, false);
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200574 ctrl->erst.entries = xhci_malloc(sizeof(struct xhci_erst_entry) *
575 ERST_NUM_SEGS);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100576 ctrl->erst.erst_dma_addr = xhci_dma_map(ctrl, ctrl->erst.entries,
577 sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530578
579 ctrl->erst.num_entries = ERST_NUM_SEGS;
580
581 for (val = 0, seg = ctrl->event_ring->first_seg;
582 val < ERST_NUM_SEGS;
583 val++) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530584 struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
Mark Kettenisfac410c2023-01-21 20:27:55 +0100585 trb_64 = seg->dma;
Stefan Roese83b17512020-07-21 10:46:03 +0200586 entry->seg_addr = cpu_to_le64(trb_64);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530587 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
588 entry->rsvd = 0;
589 seg = seg->next;
590 }
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300591 xhci_flush_cache((uintptr_t)ctrl->erst.entries,
592 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530593
Mark Kettenisfac410c2023-01-21 20:27:55 +0100594 deq = xhci_trb_virt_to_dma(ctrl->event_ring->deq_seg,
595 ctrl->event_ring->dequeue);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530596
597 /* Update HC event ring dequeue pointer */
598 xhci_writeq(&ctrl->ir_set->erst_dequeue,
599 (u64)deq & (u64)~ERST_PTR_MASK);
600
601 /* set ERST count with the number of entries in the segment table */
602 val = xhci_readl(&ctrl->ir_set->erst_size);
603 val &= ERST_SIZE_MASK;
604 val |= ERST_NUM_SEGS;
605 xhci_writel(&ctrl->ir_set->erst_size, val);
606
607 /* this is the event ring segment table pointer */
608 val_64 = xhci_readq(&ctrl->ir_set->erst_base);
609 val_64 &= ERST_PTR_MASK;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100610 val_64 |= ctrl->erst.erst_dma_addr & ~ERST_PTR_MASK;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530611
612 xhci_writeq(&ctrl->ir_set->erst_base, val_64);
613
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800614 /* set up the scratchpad buffer array and scratchpad buffers */
615 xhci_scratchpad_alloc(ctrl);
616
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530617 /* initializing the virtual devices to NULL */
618 for (i = 0; i < MAX_HC_SLOTS; ++i)
619 ctrl->devs[i] = NULL;
620
621 /*
622 * Just Zero'ing this register completely,
623 * or some spurious Device Notification Events
624 * might screw things here.
625 */
626 xhci_writel(&hcor->or_dnctrl, 0x0);
627
628 return 0;
629}
630
631/**
632 * Give the input control context for the passed container context
633 *
634 * @param ctx pointer to the context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100635 * Return: pointer to the Input control context data
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530636 */
637struct xhci_input_control_ctx
638 *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
639{
640 BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
641 return (struct xhci_input_control_ctx *)ctx->bytes;
642}
643
644/**
645 * Give the slot context for the passed container context
646 *
647 * @param ctrl Host controller data structure
648 * @param ctx pointer to the context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100649 * Return: pointer to the slot control context data
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530650 */
651struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
652 struct xhci_container_ctx *ctx)
653{
654 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
655 return (struct xhci_slot_ctx *)ctx->bytes;
656
657 return (struct xhci_slot_ctx *)
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200658 (ctx->bytes + CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams)));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530659}
660
661/**
662 * Gets the EP context from based on the ep_index
663 *
664 * @param ctrl Host controller data structure
665 * @param ctx context container
666 * @param ep_index index of the endpoint
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100667 * Return: pointer to the End point context
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530668 */
669struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
670 struct xhci_container_ctx *ctx,
671 unsigned int ep_index)
672{
673 /* increment ep index by offset of start of ep ctx array */
674 ep_index++;
675 if (ctx->type == XHCI_CTX_TYPE_INPUT)
676 ep_index++;
677
678 return (struct xhci_ep_ctx *)
679 (ctx->bytes +
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200680 (ep_index * CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams))));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530681}
682
683/**
684 * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
685 * Useful when you want to change one particular aspect of the endpoint
686 * and then issue a configure endpoint command.
687 *
688 * @param ctrl Host controller data structure
689 * @param in_ctx contains the input context
690 * @param out_ctx contains the input context
691 * @param ep_index index of the end point
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100692 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530693 */
694void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
695 struct xhci_container_ctx *in_ctx,
696 struct xhci_container_ctx *out_ctx,
697 unsigned int ep_index)
698{
699 struct xhci_ep_ctx *out_ep_ctx;
700 struct xhci_ep_ctx *in_ep_ctx;
701
702 out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
703 in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
704
705 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
706 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
707 in_ep_ctx->deq = out_ep_ctx->deq;
708 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
709}
710
711/**
712 * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
713 * Useful when you want to change one particular aspect of the endpoint
714 * and then issue a configure endpoint command.
715 * Only the context entries field matters, but
716 * we'll copy the whole thing anyway.
717 *
718 * @param ctrl Host controller data structure
719 * @param in_ctx contains the inpout context
720 * @param out_ctx contains the inpout context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100721 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530722 */
723void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
724 struct xhci_container_ctx *out_ctx)
725{
726 struct xhci_slot_ctx *in_slot_ctx;
727 struct xhci_slot_ctx *out_slot_ctx;
728
729 in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
730 out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
731
732 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
733 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
734 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
735 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
736}
737
738/**
739 * Setup an xHCI virtual device for a Set Address command
740 *
741 * @param udev pointer to the Device Data Structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100742 * Return: returns negative value on failure else 0 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530743 */
Bin Meng1459ce62017-07-19 21:51:14 +0800744void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
745 struct usb_device *udev, int hop_portnr)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530746{
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530747 struct xhci_virt_device *virt_dev;
748 struct xhci_ep_ctx *ep0_ctx;
749 struct xhci_slot_ctx *slot_ctx;
750 u32 port_num = 0;
751 u64 trb_64 = 0;
Bin Meng1459ce62017-07-19 21:51:14 +0800752 int slot_id = udev->slot_id;
753 int speed = udev->speed;
Bin Meng63c3b3b2017-07-19 21:51:15 +0800754 int route = 0;
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100755#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800756 struct usb_device *dev = udev;
757 struct usb_hub_device *hub;
758#endif
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530759
Simon Glass4ec422c2015-03-25 12:22:51 -0600760 virt_dev = ctrl->devs[slot_id];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530761
762 BUG_ON(!virt_dev);
763
764 /* Extract the EP0 and Slot Ctrl */
765 ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
766 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
767
768 /* Only the control endpoint is valid - one endpoint context */
Bin Meng63c3b3b2017-07-19 21:51:15 +0800769 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
770
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100771#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800772 /* Calculate the route string for this device */
773 port_num = dev->portnr;
774 while (!usb_hub_is_root_hub(dev->dev)) {
775 hub = dev_get_uclass_priv(dev->dev);
776 /*
777 * Each hub in the topology is expected to have no more than
778 * 15 ports in order for the route string of a device to be
779 * unique. SuperSpeed hubs are restricted to only having 15
780 * ports, but FS/LS/HS hubs are not. The xHCI specification
781 * says that if the port number the device is greater than 15,
782 * that portion of the route string shall be set to 15.
783 */
784 if (port_num > 15)
785 port_num = 15;
786 route |= port_num << (hub->hub_depth * 4);
787 dev = dev_get_parent_priv(dev->dev);
788 port_num = dev->portnr;
789 dev = dev_get_parent_priv(dev->dev->parent);
790 }
791
792 debug("route string %x\n", route);
793#endif
Stefan Roesecb570862020-07-21 10:46:02 +0200794 slot_ctx->dev_info |= cpu_to_le32(route);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530795
Simon Glass4ec422c2015-03-25 12:22:51 -0600796 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530797 case USB_SPEED_SUPER:
798 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
799 break;
800 case USB_SPEED_HIGH:
801 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
802 break;
803 case USB_SPEED_FULL:
804 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
805 break;
806 case USB_SPEED_LOW:
807 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
808 break;
809 default:
810 /* Speed was set earlier, this shouldn't happen. */
811 BUG();
812 }
813
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100814#if CONFIG_IS_ENABLED(DM_USB)
Bin Mengfe3327f2017-07-19 21:51:21 +0800815 /* Set up TT fields to support FS/LS devices */
816 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
Bin Mengbb4b1052017-09-18 06:40:39 -0700817 struct udevice *parent = udev->dev;
818
819 dev = udev;
820 do {
821 port_num = dev->portnr;
822 dev = dev_get_parent_priv(parent);
823 if (usb_hub_is_root_hub(dev->dev))
824 break;
825 parent = dev->dev->parent;
826 } while (dev->speed != USB_SPEED_HIGH);
827
828 if (!usb_hub_is_root_hub(dev->dev)) {
829 hub = dev_get_uclass_priv(dev->dev);
Bin Mengfe3327f2017-07-19 21:51:21 +0800830 if (hub->tt.multi)
831 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Mengbb4b1052017-09-18 06:40:39 -0700832 slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
Bin Mengfe3327f2017-07-19 21:51:21 +0800833 slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
834 }
835 }
836#endif
837
Simon Glass4ec422c2015-03-25 12:22:51 -0600838 port_num = hop_portnr;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530839 debug("port_num = %d\n", port_num);
840
841 slot_ctx->dev_info2 |=
842 cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
843 ROOT_HUB_PORT_SHIFT));
844
845 /* Step 4 - ring already allocated */
846 /* Step 5 */
developer99634222020-09-08 19:00:02 +0200847 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
Simon Glass4ec422c2015-03-25 12:22:51 -0600848 debug("SPEED = %d\n", speed);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530849
Simon Glass4ec422c2015-03-25 12:22:51 -0600850 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530851 case USB_SPEED_SUPER:
developer99634222020-09-08 19:00:02 +0200852 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(512));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530853 debug("Setting Packet size = 512bytes\n");
854 break;
855 case USB_SPEED_HIGH:
856 /* USB core guesses at a 64-byte max packet first for FS devices */
857 case USB_SPEED_FULL:
developer99634222020-09-08 19:00:02 +0200858 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(64));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530859 debug("Setting Packet size = 64bytes\n");
860 break;
861 case USB_SPEED_LOW:
developer99634222020-09-08 19:00:02 +0200862 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(8));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530863 debug("Setting Packet size = 8bytes\n");
864 break;
865 default:
866 /* New speed? */
867 BUG();
868 }
869
870 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
developer99634222020-09-08 19:00:02 +0200871 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530872
Mark Kettenisfac410c2023-01-21 20:27:55 +0100873 trb_64 = virt_dev->eps[0].ring->first_seg->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530874 ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
875
Bin Mengc03fb202017-09-18 06:40:50 -0700876 /*
877 * xHCI spec 6.2.3:
878 * software shall set 'Average TRB Length' to 8 for control endpoints.
879 */
880 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
881
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530882 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
883
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300884 xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
885 xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530886}