blob: 72b75306265083e932ea901ea880989c2f0134c8 [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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010035 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053036 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010050 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053051 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010065 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053066 */
Mark Kettenisfac410c2023-01-21 20:27:55 +010067static void xhci_segment_free(struct xhci_ctrl *ctrl, struct xhci_segment *seg)
Vivek Gautam4912dcc2013-09-14 14:02:45 +053068{
Mark Kettenisfac410c2023-01-21 20:27:55 +010069 xhci_dma_unmap(ctrl, seg->dma, SEGMENT_SIZE);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053070 free(seg->trbs);
71 seg->trbs = NULL;
72
73 free(seg);
74}
75
76/**
77 * frees the "ring" pointer passed
78 *
79 * @param ptr pointer to "ring" to be freed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010080 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +053081 */
Mark Kettenisfac410c2023-01-21 20:27:55 +010082static void xhci_ring_free(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
Vivek Gautam4912dcc2013-09-14 14:02:45 +053083{
84 struct xhci_segment *seg;
85 struct xhci_segment *first_seg;
86
87 BUG_ON(!ring);
88
89 first_seg = ring->first_seg;
90 seg = first_seg->next;
91 while (seg != first_seg) {
92 struct xhci_segment *next = seg->next;
Mark Kettenisfac410c2023-01-21 20:27:55 +010093 xhci_segment_free(ctrl, seg);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053094 seg = next;
95 }
Mark Kettenisfac410c2023-01-21 20:27:55 +010096 xhci_segment_free(ctrl, first_seg);
Vivek Gautam4912dcc2013-09-14 14:02:45 +053097
98 free(ring);
99}
100
101/**
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800102 * Free the scratchpad buffer array and scratchpad buffers
103 *
104 * @ctrl host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100105 * Return: none
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800106 */
107static void xhci_scratchpad_free(struct xhci_ctrl *ctrl)
108{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100109 struct xhci_hccr *hccr = ctrl->hccr;
110 int num_sp;
111
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800112 if (!ctrl->scratchpad)
113 return;
114
Mark Kettenisfac410c2023-01-21 20:27:55 +0100115 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
116 xhci_dma_unmap(ctrl, ctrl->scratchpad->sp_array[0],
117 num_sp * ctrl->page_size);
118 xhci_dma_unmap(ctrl, ctrl->dcbaa->dev_context_ptrs[0],
119 num_sp * sizeof(u64));
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800120 ctrl->dcbaa->dev_context_ptrs[0] = 0;
121
Mark Kettenisfac410c2023-01-21 20:27:55 +0100122 free(ctrl->scratchpad->scratchpad);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800123 free(ctrl->scratchpad->sp_array);
124 free(ctrl->scratchpad);
125 ctrl->scratchpad = NULL;
126}
127
128/**
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530129 * frees the "xhci_container_ctx" pointer passed
130 *
131 * @param ptr pointer to "xhci_container_ctx" to be freed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100132 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530133 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100134static void xhci_free_container_ctx(struct xhci_ctrl *ctrl,
135 struct xhci_container_ctx *ctx)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530136{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100137 xhci_dma_unmap(ctrl, ctx->dma, ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530138 free(ctx->bytes);
139 free(ctx);
140}
141
142/**
143 * frees the virtual devices for "xhci_ctrl" pointer passed
144 *
145 * @param ptr pointer to "xhci_ctrl" whose virtual devices are to be freed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100146 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530147 */
148static void xhci_free_virt_devices(struct xhci_ctrl *ctrl)
149{
150 int i;
151 int slot_id;
152 struct xhci_virt_device *virt_dev;
153
154 /*
155 * refactored here to loop through all virt_dev
156 * Slot ID 0 is reserved
157 */
158 for (slot_id = 0; slot_id < MAX_HC_SLOTS; slot_id++) {
159 virt_dev = ctrl->devs[slot_id];
160 if (!virt_dev)
161 continue;
162
163 ctrl->dcbaa->dev_context_ptrs[slot_id] = 0;
164
165 for (i = 0; i < 31; ++i)
166 if (virt_dev->eps[i].ring)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100167 xhci_ring_free(ctrl, virt_dev->eps[i].ring);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530168
169 if (virt_dev->in_ctx)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100170 xhci_free_container_ctx(ctrl, virt_dev->in_ctx);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530171 if (virt_dev->out_ctx)
Mark Kettenisfac410c2023-01-21 20:27:55 +0100172 xhci_free_container_ctx(ctrl, virt_dev->out_ctx);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530173
174 free(virt_dev);
175 /* make sure we are pointing to NULL */
176 ctrl->devs[slot_id] = NULL;
177 }
178}
179
180/**
181 * frees all the memory allocated
182 *
183 * @param ptr pointer to "xhci_ctrl" to be cleaned up
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100184 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530185 */
186void xhci_cleanup(struct xhci_ctrl *ctrl)
187{
Mark Kettenisfac410c2023-01-21 20:27:55 +0100188 xhci_ring_free(ctrl, ctrl->event_ring);
189 xhci_ring_free(ctrl, ctrl->cmd_ring);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800190 xhci_scratchpad_free(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530191 xhci_free_virt_devices(ctrl);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100192 xhci_dma_unmap(ctrl, ctrl->erst.erst_dma_addr,
193 sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530194 free(ctrl->erst.entries);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100195 xhci_dma_unmap(ctrl, ctrl->dcbaa->dma,
196 sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530197 free(ctrl->dcbaa);
198 memset(ctrl, '\0', sizeof(struct xhci_ctrl));
199}
200
201/**
202 * Malloc the aligned memory
203 *
204 * @param size size of memory to be allocated
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100205 * Return: allocates the memory and returns the aligned pointer
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530206 */
207static void *xhci_malloc(unsigned int size)
208{
209 void *ptr;
210 size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE);
211
212 ptr = memalign(cacheline_size, ALIGN(size, cacheline_size));
213 BUG_ON(!ptr);
214 memset(ptr, '\0', size);
215
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300216 xhci_flush_cache((uintptr_t)ptr, size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530217
218 return ptr;
219}
220
221/**
222 * Make the prev segment point to the next segment.
223 * Change the last TRB in the prev segment to be a Link TRB which points to the
224 * address of the next segment. The caller needs to set any Link TRB
225 * related flags, such as End TRB, Toggle Cycle, and no snoop.
226 *
227 * @param prev pointer to the previous segment
228 * @param next pointer to the next segment
229 * @param link_trbs flag to indicate whether to link the trbs or NOT
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100230 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530231 */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100232static void xhci_link_segments(struct xhci_ctrl *ctrl, struct xhci_segment *prev,
233 struct xhci_segment *next, bool link_trbs)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530234{
235 u32 val;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530236
237 if (!prev || !next)
238 return;
239 prev->next = next;
240 if (link_trbs) {
Stefan Roesecb570862020-07-21 10:46:02 +0200241 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
Mark Kettenisfac410c2023-01-21 20:27:55 +0100242 cpu_to_le64(next->dma);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530243
244 /*
245 * Set the last TRB in the segment to
246 * have a TRB type ID of Link TRB
247 */
248 val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
249 val &= ~TRB_TYPE_BITMASK;
developer497dcfa2020-09-08 18:59:59 +0200250 val |= TRB_TYPE(TRB_LINK);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530251 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
252 }
253}
254
255/**
256 * Initialises the Ring's enqueue,dequeue,enq_seg pointers
257 *
258 * @param ring pointer to the RING to be intialised
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100259 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530260 */
261static void xhci_initialize_ring_info(struct xhci_ring *ring)
262{
263 /*
264 * The ring is empty, so the enqueue pointer == dequeue pointer
265 */
266 ring->enqueue = ring->first_seg->trbs;
267 ring->enq_seg = ring->first_seg;
268 ring->dequeue = ring->enqueue;
269 ring->deq_seg = ring->first_seg;
270
271 /*
272 * The ring is initialized to 0. The producer must write 1 to the
273 * cycle bit to handover ownership of the TRB, so PCS = 1.
274 * The consumer must compare CCS to the cycle bit to
275 * check ownership, so CCS = 1.
276 */
277 ring->cycle_state = 1;
278}
279
280/**
281 * Allocates a generic ring segment from the ring pool, sets the dma address,
282 * initializes the segment to zero, and sets the private next pointer to NULL.
283 * Section 4.11.1.1:
284 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
285 *
286 * @param none
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100287 * Return: pointer to the newly allocated SEGMENT
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530288 */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100289static struct xhci_segment *xhci_segment_alloc(struct xhci_ctrl *ctrl)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530290{
291 struct xhci_segment *seg;
292
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200293 seg = malloc(sizeof(struct xhci_segment));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530294 BUG_ON(!seg);
295
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200296 seg->trbs = xhci_malloc(SEGMENT_SIZE);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100297 seg->dma = xhci_dma_map(ctrl, seg->trbs, SEGMENT_SIZE);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530298
299 seg->next = NULL;
300
301 return seg;
302}
303
304/**
305 * Create a new ring with zero or more segments.
306 * TODO: current code only uses one-time-allocated single-segment rings
307 * of 1KB anyway, so we might as well get rid of all the segment and
308 * linking code (and maybe increase the size a bit, e.g. 4KB).
309 *
310 *
311 * Link each segment together into a ring.
312 * Set the end flag and the cycle toggle bit on the last segment.
313 * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0.
314 *
315 * @param num_segs number of segments in the ring
316 * @param link_trbs flag to indicate whether to link the trbs or NOT
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100317 * Return: pointer to the newly created RING
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530318 */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100319struct xhci_ring *xhci_ring_alloc(struct xhci_ctrl *ctrl, unsigned int num_segs,
320 bool link_trbs)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530321{
322 struct xhci_ring *ring;
323 struct xhci_segment *prev;
324
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200325 ring = malloc(sizeof(struct xhci_ring));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530326 BUG_ON(!ring);
327
328 if (num_segs == 0)
329 return ring;
330
Mark Kettenisfac410c2023-01-21 20:27:55 +0100331 ring->first_seg = xhci_segment_alloc(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530332 BUG_ON(!ring->first_seg);
333
334 num_segs--;
335
336 prev = ring->first_seg;
337 while (num_segs > 0) {
338 struct xhci_segment *next;
339
Mark Kettenisfac410c2023-01-21 20:27:55 +0100340 next = xhci_segment_alloc(ctrl);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530341 BUG_ON(!next);
342
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100343 xhci_link_segments(ctrl, prev, next, link_trbs);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530344
345 prev = next;
346 num_segs--;
347 }
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100348 xhci_link_segments(ctrl, prev, ring->first_seg, link_trbs);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530349 if (link_trbs) {
350 /* See section 4.9.2.1 and 6.4.4.1 */
351 prev->trbs[TRBS_PER_SEGMENT-1].link.control |=
352 cpu_to_le32(LINK_TOGGLE);
353 }
354 xhci_initialize_ring_info(ring);
355
356 return ring;
357}
358
359/**
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800360 * Set up the scratchpad buffer array and scratchpad buffers
361 *
362 * @ctrl host controller data structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100363 * Return: -ENOMEM if buffer allocation fails, 0 on success
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800364 */
365static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl)
366{
367 struct xhci_hccr *hccr = ctrl->hccr;
368 struct xhci_hcor *hcor = ctrl->hcor;
369 struct xhci_scratchpad *scratchpad;
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100370 uint64_t val_64;
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800371 int num_sp;
372 uint32_t page_size;
373 void *buf;
374 int i;
375
376 num_sp = HCS_MAX_SCRATCHPAD(xhci_readl(&hccr->cr_hcsparams2));
377 if (!num_sp)
378 return 0;
379
380 scratchpad = malloc(sizeof(*scratchpad));
381 if (!scratchpad)
382 goto fail_sp;
383 ctrl->scratchpad = scratchpad;
384
385 scratchpad->sp_array = xhci_malloc(num_sp * sizeof(u64));
386 if (!scratchpad->sp_array)
387 goto fail_sp2;
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100388
Mark Kettenisfac410c2023-01-21 20:27:55 +0100389 val_64 = xhci_dma_map(ctrl, scratchpad->sp_array,
390 num_sp * sizeof(u64));
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100391 ctrl->dcbaa->dev_context_ptrs[0] = cpu_to_le64(val_64);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800392
Ye Li4bda7182019-01-07 02:45:46 +0000393 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0],
394 sizeof(ctrl->dcbaa->dev_context_ptrs[0]));
395
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800396 page_size = xhci_readl(&hcor->or_pagesize) & 0xffff;
397 for (i = 0; i < 16; i++) {
398 if ((0x1 & page_size) != 0)
399 break;
400 page_size = page_size >> 1;
401 }
402 BUG_ON(i == 16);
403
Mark Kettenisfac410c2023-01-21 20:27:55 +0100404 ctrl->page_size = 1 << (i + 12);
405 buf = memalign(ctrl->page_size, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800406 if (!buf)
407 goto fail_sp3;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100408 memset(buf, '\0', num_sp * ctrl->page_size);
409 xhci_flush_cache((uintptr_t)buf, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800410
Mark Kettenisfac410c2023-01-21 20:27:55 +0100411 scratchpad->scratchpad = buf;
412 val_64 = xhci_dma_map(ctrl, buf, num_sp * ctrl->page_size);
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800413 for (i = 0; i < num_sp; i++) {
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100414 scratchpad->sp_array[i] = cpu_to_le64(val_64);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100415 val_64 += ctrl->page_size;
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800416 }
417
Sylwester Nawrockiffedc752020-05-25 13:39:51 +0200418 xhci_flush_cache((uintptr_t)scratchpad->sp_array,
419 sizeof(u64) * num_sp);
420
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800421 return 0;
422
423fail_sp3:
424 free(scratchpad->sp_array);
425
426fail_sp2:
427 free(scratchpad);
428 ctrl->scratchpad = NULL;
429
430fail_sp:
431 return -ENOMEM;
432}
433
434/**
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530435 * Allocates the Container context
436 *
437 * @param ctrl Host controller data structure
438 * @param type type of XHCI Container Context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100439 * Return: NULL if failed else pointer to the context on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530440 */
441static struct xhci_container_ctx
442 *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type)
443{
444 struct xhci_container_ctx *ctx;
445
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200446 ctx = malloc(sizeof(struct xhci_container_ctx));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530447 BUG_ON(!ctx);
448
449 BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT));
450 ctx->type = type;
451 ctx->size = (MAX_EP_CTX_NUM + 1) *
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200452 CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530453 if (type == XHCI_CTX_TYPE_INPUT)
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200454 ctx->size += CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530455
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200456 ctx->bytes = xhci_malloc(ctx->size);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100457 ctx->dma = xhci_dma_map(ctrl, ctx->bytes, ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530458
459 return ctx;
460}
461
462/**
463 * Allocating virtual device
464 *
465 * @param udev pointer to USB deivce structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100466 * Return: 0 on success else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530467 */
Simon Glass88a37842015-03-25 12:22:50 -0600468int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530469{
470 u64 byte_64 = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530471 struct xhci_virt_device *virt_dev;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530472
473 /* Slot ID 0 is reserved */
474 if (ctrl->devs[slot_id]) {
475 printf("Virt dev for slot[%d] already allocated\n", slot_id);
476 return -EEXIST;
477 }
478
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200479 ctrl->devs[slot_id] = malloc(sizeof(struct xhci_virt_device));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530480
481 if (!ctrl->devs[slot_id]) {
482 puts("Failed to allocate virtual device\n");
483 return -ENOMEM;
484 }
485
486 memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device));
487 virt_dev = ctrl->devs[slot_id];
488
489 /* Allocate the (output) device context that will be used in the HC. */
490 virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl,
491 XHCI_CTX_TYPE_DEVICE);
492 if (!virt_dev->out_ctx) {
493 puts("Failed to allocate out context for virt dev\n");
494 return -ENOMEM;
495 }
496
497 /* Allocate the (input) device context for address device command */
498 virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl,
499 XHCI_CTX_TYPE_INPUT);
500 if (!virt_dev->in_ctx) {
501 puts("Failed to allocate in context for virt dev\n");
502 return -ENOMEM;
503 }
504
505 /* Allocate endpoint 0 ring */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100506 virt_dev->eps[0].ring = xhci_ring_alloc(ctrl, 1, true);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530507
Mark Kettenisfac410c2023-01-21 20:27:55 +0100508 byte_64 = virt_dev->out_ctx->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530509
510 /* Point to output device context in dcbaa. */
Stefan Roesecb570862020-07-21 10:46:02 +0200511 ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530512
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300513 xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id],
514 sizeof(__le64));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530515 return 0;
516}
517
518/**
519 * Allocates the necessary data structures
520 * for XHCI host controller
521 *
522 * @param ctrl Host controller data structure
523 * @param hccr pointer to HOST Controller Control Registers
524 * @param hcor pointer to HOST Controller Operational Registers
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100525 * Return: 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530526 */
527int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr,
528 struct xhci_hcor *hcor)
529{
530 uint64_t val_64;
531 uint64_t trb_64;
532 uint32_t val;
Stefan Roesecaf8cae2020-07-21 10:46:05 +0200533 uint64_t deq;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530534 int i;
535 struct xhci_segment *seg;
536
537 /* DCBAA initialization */
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200538 ctrl->dcbaa = xhci_malloc(sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530539 if (ctrl->dcbaa == NULL) {
540 puts("unable to allocate DCBA\n");
541 return -ENOMEM;
542 }
543
Mark Kettenisfac410c2023-01-21 20:27:55 +0100544 ctrl->dcbaa->dma = xhci_dma_map(ctrl, ctrl->dcbaa,
545 sizeof(struct xhci_device_context_array));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530546 /* Set the pointer in DCBAA register */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100547 xhci_writeq(&hcor->or_dcbaap, ctrl->dcbaa->dma);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530548
549 /* Command ring control pointer register initialization */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100550 ctrl->cmd_ring = xhci_ring_alloc(ctrl, 1, true);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530551
552 /* Set the address in the Command Ring Control register */
Mark Kettenisfac410c2023-01-21 20:27:55 +0100553 trb_64 = ctrl->cmd_ring->first_seg->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530554 val_64 = xhci_readq(&hcor->or_crcr);
555 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
556 (trb_64 & (u64) ~CMD_RING_RSVD_BITS) |
557 ctrl->cmd_ring->cycle_state;
558 xhci_writeq(&hcor->or_crcr, val_64);
559
560 /* write the address of db register */
561 val = xhci_readl(&hccr->cr_dboff);
562 val &= DBOFF_MASK;
563 ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val);
564
565 /* write the address of runtime register */
566 val = xhci_readl(&hccr->cr_rtsoff);
567 val &= RTSOFF_MASK;
568 ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val);
569
570 /* writting the address of ir_set structure */
571 ctrl->ir_set = &ctrl->run_regs->ir_set[0];
572
573 /* Event ring does not maintain link TRB */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100574 ctrl->event_ring = xhci_ring_alloc(ctrl, ERST_NUM_SEGS, false);
Heinrich Schuchardt01adeb32020-09-29 22:03:01 +0200575 ctrl->erst.entries = xhci_malloc(sizeof(struct xhci_erst_entry) *
576 ERST_NUM_SEGS);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100577 ctrl->erst.erst_dma_addr = xhci_dma_map(ctrl, ctrl->erst.entries,
578 sizeof(struct xhci_erst_entry) * ERST_NUM_SEGS);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530579
580 ctrl->erst.num_entries = ERST_NUM_SEGS;
581
582 for (val = 0, seg = ctrl->event_ring->first_seg;
583 val < ERST_NUM_SEGS;
584 val++) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530585 struct xhci_erst_entry *entry = &ctrl->erst.entries[val];
Mark Kettenisfac410c2023-01-21 20:27:55 +0100586 trb_64 = seg->dma;
Stefan Roese83b17512020-07-21 10:46:03 +0200587 entry->seg_addr = cpu_to_le64(trb_64);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530588 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
589 entry->rsvd = 0;
590 seg = seg->next;
591 }
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300592 xhci_flush_cache((uintptr_t)ctrl->erst.entries,
593 ERST_NUM_SEGS * sizeof(struct xhci_erst_entry));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530594
Mark Kettenisfac410c2023-01-21 20:27:55 +0100595 deq = xhci_trb_virt_to_dma(ctrl->event_ring->deq_seg,
596 ctrl->event_ring->dequeue);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530597
598 /* Update HC event ring dequeue pointer */
599 xhci_writeq(&ctrl->ir_set->erst_dequeue,
600 (u64)deq & (u64)~ERST_PTR_MASK);
601
602 /* set ERST count with the number of entries in the segment table */
603 val = xhci_readl(&ctrl->ir_set->erst_size);
604 val &= ERST_SIZE_MASK;
605 val |= ERST_NUM_SEGS;
606 xhci_writel(&ctrl->ir_set->erst_size, val);
607
608 /* this is the event ring segment table pointer */
609 val_64 = xhci_readq(&ctrl->ir_set->erst_base);
610 val_64 &= ERST_PTR_MASK;
Mark Kettenisfac410c2023-01-21 20:27:55 +0100611 val_64 |= ctrl->erst.erst_dma_addr & ~ERST_PTR_MASK;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530612
613 xhci_writeq(&ctrl->ir_set->erst_base, val_64);
614
Bin Meng8a3f9cf2017-07-19 21:49:55 +0800615 /* set up the scratchpad buffer array and scratchpad buffers */
616 xhci_scratchpad_alloc(ctrl);
617
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530618 /* initializing the virtual devices to NULL */
619 for (i = 0; i < MAX_HC_SLOTS; ++i)
620 ctrl->devs[i] = NULL;
621
622 /*
623 * Just Zero'ing this register completely,
624 * or some spurious Device Notification Events
625 * might screw things here.
626 */
627 xhci_writel(&hcor->or_dnctrl, 0x0);
628
629 return 0;
630}
631
632/**
633 * Give the input control context for the passed container context
634 *
635 * @param ctx pointer to the context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100636 * Return: pointer to the Input control context data
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530637 */
638struct xhci_input_control_ctx
639 *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx)
640{
641 BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT);
642 return (struct xhci_input_control_ctx *)ctx->bytes;
643}
644
645/**
646 * Give the slot context for the passed container context
647 *
648 * @param ctrl Host controller data structure
649 * @param ctx pointer to the context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100650 * Return: pointer to the slot control context data
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530651 */
652struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl,
653 struct xhci_container_ctx *ctx)
654{
655 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
656 return (struct xhci_slot_ctx *)ctx->bytes;
657
658 return (struct xhci_slot_ctx *)
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200659 (ctx->bytes + CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams)));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530660}
661
662/**
663 * Gets the EP context from based on the ep_index
664 *
665 * @param ctrl Host controller data structure
666 * @param ctx context container
667 * @param ep_index index of the endpoint
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100668 * Return: pointer to the End point context
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530669 */
670struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl,
671 struct xhci_container_ctx *ctx,
672 unsigned int ep_index)
673{
674 /* increment ep index by offset of start of ep ctx array */
675 ep_index++;
676 if (ctx->type == XHCI_CTX_TYPE_INPUT)
677 ep_index++;
678
679 return (struct xhci_ep_ctx *)
680 (ctx->bytes +
Aaron Williamsfd062eb2021-04-06 12:10:17 +0200681 (ep_index * CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams))));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530682}
683
684/**
685 * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
686 * Useful when you want to change one particular aspect of the endpoint
687 * and then issue a configure endpoint command.
688 *
689 * @param ctrl Host controller data structure
690 * @param in_ctx contains the input context
691 * @param out_ctx contains the input context
692 * @param ep_index index of the end point
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100693 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530694 */
695void xhci_endpoint_copy(struct xhci_ctrl *ctrl,
696 struct xhci_container_ctx *in_ctx,
697 struct xhci_container_ctx *out_ctx,
698 unsigned int ep_index)
699{
700 struct xhci_ep_ctx *out_ep_ctx;
701 struct xhci_ep_ctx *in_ep_ctx;
702
703 out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
704 in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
705
706 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
707 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
708 in_ep_ctx->deq = out_ep_ctx->deq;
709 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
710}
711
712/**
713 * Copy output xhci_slot_ctx to the input xhci_slot_ctx.
714 * Useful when you want to change one particular aspect of the endpoint
715 * and then issue a configure endpoint command.
716 * Only the context entries field matters, but
717 * we'll copy the whole thing anyway.
718 *
719 * @param ctrl Host controller data structure
720 * @param in_ctx contains the inpout context
721 * @param out_ctx contains the inpout context
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100722 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530723 */
724void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx,
725 struct xhci_container_ctx *out_ctx)
726{
727 struct xhci_slot_ctx *in_slot_ctx;
728 struct xhci_slot_ctx *out_slot_ctx;
729
730 in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
731 out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx);
732
733 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
734 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
735 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
736 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
737}
738
739/**
740 * Setup an xHCI virtual device for a Set Address command
741 *
742 * @param udev pointer to the Device Data Structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100743 * Return: returns negative value on failure else 0 on success
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530744 */
Bin Meng1459ce62017-07-19 21:51:14 +0800745void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl,
746 struct usb_device *udev, int hop_portnr)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530747{
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530748 struct xhci_virt_device *virt_dev;
749 struct xhci_ep_ctx *ep0_ctx;
750 struct xhci_slot_ctx *slot_ctx;
751 u32 port_num = 0;
752 u64 trb_64 = 0;
Bin Meng1459ce62017-07-19 21:51:14 +0800753 int slot_id = udev->slot_id;
754 int speed = udev->speed;
Bin Meng63c3b3b2017-07-19 21:51:15 +0800755 int route = 0;
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100756#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800757 struct usb_device *dev = udev;
758 struct usb_hub_device *hub;
759#endif
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530760
Simon Glass4ec422c2015-03-25 12:22:51 -0600761 virt_dev = ctrl->devs[slot_id];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530762
763 BUG_ON(!virt_dev);
764
765 /* Extract the EP0 and Slot Ctrl */
766 ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0);
767 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx);
768
769 /* Only the control endpoint is valid - one endpoint context */
Bin Meng63c3b3b2017-07-19 21:51:15 +0800770 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
771
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100772#if CONFIG_IS_ENABLED(DM_USB)
Bin Meng63c3b3b2017-07-19 21:51:15 +0800773 /* Calculate the route string for this device */
774 port_num = dev->portnr;
775 while (!usb_hub_is_root_hub(dev->dev)) {
776 hub = dev_get_uclass_priv(dev->dev);
777 /*
778 * Each hub in the topology is expected to have no more than
779 * 15 ports in order for the route string of a device to be
780 * unique. SuperSpeed hubs are restricted to only having 15
781 * ports, but FS/LS/HS hubs are not. The xHCI specification
782 * says that if the port number the device is greater than 15,
783 * that portion of the route string shall be set to 15.
784 */
785 if (port_num > 15)
786 port_num = 15;
787 route |= port_num << (hub->hub_depth * 4);
788 dev = dev_get_parent_priv(dev->dev);
789 port_num = dev->portnr;
790 dev = dev_get_parent_priv(dev->dev->parent);
791 }
792
793 debug("route string %x\n", route);
794#endif
Stefan Roesecb570862020-07-21 10:46:02 +0200795 slot_ctx->dev_info |= cpu_to_le32(route);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530796
Simon Glass4ec422c2015-03-25 12:22:51 -0600797 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530798 case USB_SPEED_SUPER:
799 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
800 break;
801 case USB_SPEED_HIGH:
802 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
803 break;
804 case USB_SPEED_FULL:
805 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
806 break;
807 case USB_SPEED_LOW:
808 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
809 break;
810 default:
811 /* Speed was set earlier, this shouldn't happen. */
812 BUG();
813 }
814
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100815#if CONFIG_IS_ENABLED(DM_USB)
Bin Mengfe3327f2017-07-19 21:51:21 +0800816 /* Set up TT fields to support FS/LS devices */
817 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
Bin Mengbb4b1052017-09-18 06:40:39 -0700818 struct udevice *parent = udev->dev;
819
820 dev = udev;
821 do {
822 port_num = dev->portnr;
823 dev = dev_get_parent_priv(parent);
824 if (usb_hub_is_root_hub(dev->dev))
825 break;
826 parent = dev->dev->parent;
827 } while (dev->speed != USB_SPEED_HIGH);
828
829 if (!usb_hub_is_root_hub(dev->dev)) {
830 hub = dev_get_uclass_priv(dev->dev);
Bin Mengfe3327f2017-07-19 21:51:21 +0800831 if (hub->tt.multi)
832 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Mengbb4b1052017-09-18 06:40:39 -0700833 slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num));
Bin Mengfe3327f2017-07-19 21:51:21 +0800834 slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id));
835 }
836 }
837#endif
838
Simon Glass4ec422c2015-03-25 12:22:51 -0600839 port_num = hop_portnr;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530840 debug("port_num = %d\n", port_num);
841
842 slot_ctx->dev_info2 |=
843 cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) <<
844 ROOT_HUB_PORT_SHIFT));
845
846 /* Step 4 - ring already allocated */
847 /* Step 5 */
developer99634222020-09-08 19:00:02 +0200848 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
Simon Glass4ec422c2015-03-25 12:22:51 -0600849 debug("SPEED = %d\n", speed);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530850
Simon Glass4ec422c2015-03-25 12:22:51 -0600851 switch (speed) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530852 case USB_SPEED_SUPER:
developer99634222020-09-08 19:00:02 +0200853 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(512));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530854 debug("Setting Packet size = 512bytes\n");
855 break;
856 case USB_SPEED_HIGH:
857 /* USB core guesses at a 64-byte max packet first for FS devices */
858 case USB_SPEED_FULL:
developer99634222020-09-08 19:00:02 +0200859 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(64));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530860 debug("Setting Packet size = 64bytes\n");
861 break;
862 case USB_SPEED_LOW:
developer99634222020-09-08 19:00:02 +0200863 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(8));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530864 debug("Setting Packet size = 8bytes\n");
865 break;
866 default:
867 /* New speed? */
868 BUG();
869 }
870
871 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
developer99634222020-09-08 19:00:02 +0200872 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530873
Mark Kettenisfac410c2023-01-21 20:27:55 +0100874 trb_64 = virt_dev->eps[0].ring->first_seg->dma;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530875 ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state);
876
Bin Mengc03fb202017-09-18 06:40:50 -0700877 /*
878 * xHCI spec 6.2.3:
879 * software shall set 'Average TRB Length' to 8 for control endpoints.
880 */
881 ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8));
882
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530883 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
884
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300885 xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx));
886 xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530887}