Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 2 | /* |
| 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 Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 17 | #include <cpu_func.h> |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 18 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 20 | #include <asm/byteorder.h> |
| 21 | #include <usb.h> |
| 22 | #include <malloc.h> |
| 23 | #include <asm/cache.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 24 | #include <linux/bug.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 25 | #include <linux/errno.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 26 | |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 27 | #include <usb/xhci.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 28 | |
| 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 Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 37 | void xhci_flush_cache(uintptr_t addr, u32 len) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 38 | { |
| 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 Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 52 | void xhci_inval_cache(uintptr_t addr, u32 len) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 53 | { |
| 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 | */ |
| 67 | static 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 | */ |
| 81 | static 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 Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 101 | * Free the scratchpad buffer array and scratchpad buffers |
| 102 | * |
| 103 | * @ctrl host controller data structure |
| 104 | * @return none |
| 105 | */ |
| 106 | static 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 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 113 | free(xhci_bus_to_virt(ctrl, le64_to_cpu(ctrl->scratchpad->sp_array[0]))); |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 114 | free(ctrl->scratchpad->sp_array); |
| 115 | free(ctrl->scratchpad); |
| 116 | ctrl->scratchpad = NULL; |
| 117 | } |
| 118 | |
| 119 | /** |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 120 | * frees the "xhci_container_ctx" pointer passed |
| 121 | * |
| 122 | * @param ptr pointer to "xhci_container_ctx" to be freed |
| 123 | * @return none |
| 124 | */ |
| 125 | static 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 | */ |
| 137 | static 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 | */ |
| 175 | void xhci_cleanup(struct xhci_ctrl *ctrl) |
| 176 | { |
| 177 | xhci_ring_free(ctrl->event_ring); |
| 178 | xhci_ring_free(ctrl->cmd_ring); |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 179 | xhci_scratchpad_free(ctrl); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 180 | xhci_free_virt_devices(ctrl); |
| 181 | free(ctrl->erst.entries); |
| 182 | free(ctrl->dcbaa); |
| 183 | memset(ctrl, '\0', sizeof(struct xhci_ctrl)); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Malloc the aligned memory |
| 188 | * |
| 189 | * @param size size of memory to be allocated |
| 190 | * @return allocates the memory and returns the aligned pointer |
| 191 | */ |
| 192 | static void *xhci_malloc(unsigned int size) |
| 193 | { |
| 194 | void *ptr; |
| 195 | size_t cacheline_size = max(XHCI_ALIGNMENT, CACHELINE_SIZE); |
| 196 | |
| 197 | ptr = memalign(cacheline_size, ALIGN(size, cacheline_size)); |
| 198 | BUG_ON(!ptr); |
| 199 | memset(ptr, '\0', size); |
| 200 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 201 | xhci_flush_cache((uintptr_t)ptr, size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 202 | |
| 203 | return ptr; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Make the prev segment point to the next segment. |
| 208 | * Change the last TRB in the prev segment to be a Link TRB which points to the |
| 209 | * address of the next segment. The caller needs to set any Link TRB |
| 210 | * related flags, such as End TRB, Toggle Cycle, and no snoop. |
| 211 | * |
| 212 | * @param prev pointer to the previous segment |
| 213 | * @param next pointer to the next segment |
| 214 | * @param link_trbs flag to indicate whether to link the trbs or NOT |
| 215 | * @return none |
| 216 | */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 217 | static void xhci_link_segments(struct xhci_ctrl *ctrl, struct xhci_segment *prev, |
| 218 | struct xhci_segment *next, bool link_trbs) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 219 | { |
| 220 | u32 val; |
| 221 | u64 val_64 = 0; |
| 222 | |
| 223 | if (!prev || !next) |
| 224 | return; |
| 225 | prev->next = next; |
| 226 | if (link_trbs) { |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 227 | val_64 = xhci_virt_to_bus(ctrl, next->trbs); |
Stefan Roese | cb57086 | 2020-07-21 10:46:02 +0200 | [diff] [blame] | 228 | prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr = |
| 229 | cpu_to_le64(val_64); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Set the last TRB in the segment to |
| 233 | * have a TRB type ID of Link TRB |
| 234 | */ |
| 235 | val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control); |
| 236 | val &= ~TRB_TYPE_BITMASK; |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 237 | val |= TRB_TYPE(TRB_LINK); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 238 | prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Initialises the Ring's enqueue,dequeue,enq_seg pointers |
| 244 | * |
| 245 | * @param ring pointer to the RING to be intialised |
| 246 | * @return none |
| 247 | */ |
| 248 | static void xhci_initialize_ring_info(struct xhci_ring *ring) |
| 249 | { |
| 250 | /* |
| 251 | * The ring is empty, so the enqueue pointer == dequeue pointer |
| 252 | */ |
| 253 | ring->enqueue = ring->first_seg->trbs; |
| 254 | ring->enq_seg = ring->first_seg; |
| 255 | ring->dequeue = ring->enqueue; |
| 256 | ring->deq_seg = ring->first_seg; |
| 257 | |
| 258 | /* |
| 259 | * The ring is initialized to 0. The producer must write 1 to the |
| 260 | * cycle bit to handover ownership of the TRB, so PCS = 1. |
| 261 | * The consumer must compare CCS to the cycle bit to |
| 262 | * check ownership, so CCS = 1. |
| 263 | */ |
| 264 | ring->cycle_state = 1; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Allocates a generic ring segment from the ring pool, sets the dma address, |
| 269 | * initializes the segment to zero, and sets the private next pointer to NULL. |
| 270 | * Section 4.11.1.1: |
| 271 | * "All components of all Command and Transfer TRBs shall be initialized to '0'" |
| 272 | * |
| 273 | * @param none |
| 274 | * @return pointer to the newly allocated SEGMENT |
| 275 | */ |
| 276 | static struct xhci_segment *xhci_segment_alloc(void) |
| 277 | { |
| 278 | struct xhci_segment *seg; |
| 279 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 280 | seg = malloc(sizeof(struct xhci_segment)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 281 | BUG_ON(!seg); |
| 282 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 283 | seg->trbs = xhci_malloc(SEGMENT_SIZE); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 284 | |
| 285 | seg->next = NULL; |
| 286 | |
| 287 | return seg; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Create a new ring with zero or more segments. |
| 292 | * TODO: current code only uses one-time-allocated single-segment rings |
| 293 | * of 1KB anyway, so we might as well get rid of all the segment and |
| 294 | * linking code (and maybe increase the size a bit, e.g. 4KB). |
| 295 | * |
| 296 | * |
| 297 | * Link each segment together into a ring. |
| 298 | * Set the end flag and the cycle toggle bit on the last segment. |
| 299 | * See section 4.9.2 and figures 15 and 16 of XHCI spec rev1.0. |
| 300 | * |
| 301 | * @param num_segs number of segments in the ring |
| 302 | * @param link_trbs flag to indicate whether to link the trbs or NOT |
| 303 | * @return pointer to the newly created RING |
| 304 | */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 305 | struct xhci_ring *xhci_ring_alloc(struct xhci_ctrl *ctrl, unsigned int num_segs, |
| 306 | bool link_trbs) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 307 | { |
| 308 | struct xhci_ring *ring; |
| 309 | struct xhci_segment *prev; |
| 310 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 311 | ring = malloc(sizeof(struct xhci_ring)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 312 | BUG_ON(!ring); |
| 313 | |
| 314 | if (num_segs == 0) |
| 315 | return ring; |
| 316 | |
| 317 | ring->first_seg = xhci_segment_alloc(); |
| 318 | BUG_ON(!ring->first_seg); |
| 319 | |
| 320 | num_segs--; |
| 321 | |
| 322 | prev = ring->first_seg; |
| 323 | while (num_segs > 0) { |
| 324 | struct xhci_segment *next; |
| 325 | |
| 326 | next = xhci_segment_alloc(); |
| 327 | BUG_ON(!next); |
| 328 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 329 | xhci_link_segments(ctrl, prev, next, link_trbs); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 330 | |
| 331 | prev = next; |
| 332 | num_segs--; |
| 333 | } |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 334 | xhci_link_segments(ctrl, prev, ring->first_seg, link_trbs); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 335 | if (link_trbs) { |
| 336 | /* See section 4.9.2.1 and 6.4.4.1 */ |
| 337 | prev->trbs[TRBS_PER_SEGMENT-1].link.control |= |
| 338 | cpu_to_le32(LINK_TOGGLE); |
| 339 | } |
| 340 | xhci_initialize_ring_info(ring); |
| 341 | |
| 342 | return ring; |
| 343 | } |
| 344 | |
| 345 | /** |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 346 | * Set up the scratchpad buffer array and scratchpad buffers |
| 347 | * |
| 348 | * @ctrl host controller data structure |
| 349 | * @return -ENOMEM if buffer allocation fails, 0 on success |
| 350 | */ |
| 351 | static int xhci_scratchpad_alloc(struct xhci_ctrl *ctrl) |
| 352 | { |
| 353 | struct xhci_hccr *hccr = ctrl->hccr; |
| 354 | struct xhci_hcor *hcor = ctrl->hcor; |
| 355 | struct xhci_scratchpad *scratchpad; |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 356 | uint64_t val_64; |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 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; |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 374 | |
| 375 | val_64 = xhci_virt_to_bus(ctrl, scratchpad->sp_array); |
| 376 | ctrl->dcbaa->dev_context_ptrs[0] = cpu_to_le64(val_64); |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 377 | |
Ye Li | 4bda718 | 2019-01-07 02:45:46 +0000 | [diff] [blame] | 378 | xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[0], |
| 379 | sizeof(ctrl->dcbaa->dev_context_ptrs[0])); |
| 380 | |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 381 | page_size = xhci_readl(&hcor->or_pagesize) & 0xffff; |
| 382 | for (i = 0; i < 16; i++) { |
| 383 | if ((0x1 & page_size) != 0) |
| 384 | break; |
| 385 | page_size = page_size >> 1; |
| 386 | } |
| 387 | BUG_ON(i == 16); |
| 388 | |
| 389 | page_size = 1 << (i + 12); |
| 390 | buf = memalign(page_size, num_sp * page_size); |
| 391 | if (!buf) |
| 392 | goto fail_sp3; |
| 393 | memset(buf, '\0', num_sp * page_size); |
| 394 | xhci_flush_cache((uintptr_t)buf, num_sp * page_size); |
| 395 | |
| 396 | for (i = 0; i < num_sp; i++) { |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 397 | val_64 = xhci_virt_to_bus(ctrl, buf + i * page_size); |
| 398 | scratchpad->sp_array[i] = cpu_to_le64(val_64); |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Sylwester Nawrocki | ffedc75 | 2020-05-25 13:39:51 +0200 | [diff] [blame] | 401 | xhci_flush_cache((uintptr_t)scratchpad->sp_array, |
| 402 | sizeof(u64) * num_sp); |
| 403 | |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 404 | return 0; |
| 405 | |
| 406 | fail_sp3: |
| 407 | free(scratchpad->sp_array); |
| 408 | |
| 409 | fail_sp2: |
| 410 | free(scratchpad); |
| 411 | ctrl->scratchpad = NULL; |
| 412 | |
| 413 | fail_sp: |
| 414 | return -ENOMEM; |
| 415 | } |
| 416 | |
| 417 | /** |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 418 | * Allocates the Container context |
| 419 | * |
| 420 | * @param ctrl Host controller data structure |
| 421 | * @param type type of XHCI Container Context |
| 422 | * @return NULL if failed else pointer to the context on success |
| 423 | */ |
| 424 | static struct xhci_container_ctx |
| 425 | *xhci_alloc_container_ctx(struct xhci_ctrl *ctrl, int type) |
| 426 | { |
| 427 | struct xhci_container_ctx *ctx; |
| 428 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 429 | ctx = malloc(sizeof(struct xhci_container_ctx)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 430 | BUG_ON(!ctx); |
| 431 | |
| 432 | BUG_ON((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT)); |
| 433 | ctx->type = type; |
| 434 | ctx->size = (MAX_EP_CTX_NUM + 1) * |
Aaron Williams | fd062eb | 2021-04-06 12:10:17 +0200 | [diff] [blame] | 435 | CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 436 | if (type == XHCI_CTX_TYPE_INPUT) |
Aaron Williams | fd062eb | 2021-04-06 12:10:17 +0200 | [diff] [blame] | 437 | ctx->size += CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 438 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 439 | ctx->bytes = xhci_malloc(ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 440 | |
| 441 | return ctx; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Allocating virtual device |
| 446 | * |
| 447 | * @param udev pointer to USB deivce structure |
| 448 | * @return 0 on success else -1 on failure |
| 449 | */ |
Simon Glass | 88a3784 | 2015-03-25 12:22:50 -0600 | [diff] [blame] | 450 | int xhci_alloc_virt_device(struct xhci_ctrl *ctrl, unsigned int slot_id) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 451 | { |
| 452 | u64 byte_64 = 0; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 453 | struct xhci_virt_device *virt_dev; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 454 | |
| 455 | /* Slot ID 0 is reserved */ |
| 456 | if (ctrl->devs[slot_id]) { |
| 457 | printf("Virt dev for slot[%d] already allocated\n", slot_id); |
| 458 | return -EEXIST; |
| 459 | } |
| 460 | |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 461 | ctrl->devs[slot_id] = malloc(sizeof(struct xhci_virt_device)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 462 | |
| 463 | if (!ctrl->devs[slot_id]) { |
| 464 | puts("Failed to allocate virtual device\n"); |
| 465 | return -ENOMEM; |
| 466 | } |
| 467 | |
| 468 | memset(ctrl->devs[slot_id], 0, sizeof(struct xhci_virt_device)); |
| 469 | virt_dev = ctrl->devs[slot_id]; |
| 470 | |
| 471 | /* Allocate the (output) device context that will be used in the HC. */ |
| 472 | virt_dev->out_ctx = xhci_alloc_container_ctx(ctrl, |
| 473 | XHCI_CTX_TYPE_DEVICE); |
| 474 | if (!virt_dev->out_ctx) { |
| 475 | puts("Failed to allocate out context for virt dev\n"); |
| 476 | return -ENOMEM; |
| 477 | } |
| 478 | |
| 479 | /* Allocate the (input) device context for address device command */ |
| 480 | virt_dev->in_ctx = xhci_alloc_container_ctx(ctrl, |
| 481 | XHCI_CTX_TYPE_INPUT); |
| 482 | if (!virt_dev->in_ctx) { |
| 483 | puts("Failed to allocate in context for virt dev\n"); |
| 484 | return -ENOMEM; |
| 485 | } |
| 486 | |
| 487 | /* Allocate endpoint 0 ring */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 488 | virt_dev->eps[0].ring = xhci_ring_alloc(ctrl, 1, true); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 489 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 490 | byte_64 = xhci_virt_to_bus(ctrl, virt_dev->out_ctx->bytes); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 491 | |
| 492 | /* Point to output device context in dcbaa. */ |
Stefan Roese | cb57086 | 2020-07-21 10:46:02 +0200 | [diff] [blame] | 493 | ctrl->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(byte_64); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 494 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 495 | xhci_flush_cache((uintptr_t)&ctrl->dcbaa->dev_context_ptrs[slot_id], |
| 496 | sizeof(__le64)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Allocates the necessary data structures |
| 502 | * for XHCI host controller |
| 503 | * |
| 504 | * @param ctrl Host controller data structure |
| 505 | * @param hccr pointer to HOST Controller Control Registers |
| 506 | * @param hcor pointer to HOST Controller Operational Registers |
| 507 | * @return 0 if successful else -1 on failure |
| 508 | */ |
| 509 | int xhci_mem_init(struct xhci_ctrl *ctrl, struct xhci_hccr *hccr, |
| 510 | struct xhci_hcor *hcor) |
| 511 | { |
| 512 | uint64_t val_64; |
| 513 | uint64_t trb_64; |
| 514 | uint32_t val; |
Stefan Roese | caf8cae | 2020-07-21 10:46:05 +0200 | [diff] [blame] | 515 | uint64_t deq; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 516 | int i; |
| 517 | struct xhci_segment *seg; |
| 518 | |
| 519 | /* DCBAA initialization */ |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 520 | ctrl->dcbaa = xhci_malloc(sizeof(struct xhci_device_context_array)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 521 | if (ctrl->dcbaa == NULL) { |
| 522 | puts("unable to allocate DCBA\n"); |
| 523 | return -ENOMEM; |
| 524 | } |
| 525 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 526 | val_64 = xhci_virt_to_bus(ctrl, ctrl->dcbaa); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 527 | /* Set the pointer in DCBAA register */ |
| 528 | xhci_writeq(&hcor->or_dcbaap, val_64); |
| 529 | |
| 530 | /* Command ring control pointer register initialization */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 531 | ctrl->cmd_ring = xhci_ring_alloc(ctrl, 1, true); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 532 | |
| 533 | /* Set the address in the Command Ring Control register */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 534 | trb_64 = xhci_virt_to_bus(ctrl, ctrl->cmd_ring->first_seg->trbs); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 535 | val_64 = xhci_readq(&hcor->or_crcr); |
| 536 | val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) | |
| 537 | (trb_64 & (u64) ~CMD_RING_RSVD_BITS) | |
| 538 | ctrl->cmd_ring->cycle_state; |
| 539 | xhci_writeq(&hcor->or_crcr, val_64); |
| 540 | |
| 541 | /* write the address of db register */ |
| 542 | val = xhci_readl(&hccr->cr_dboff); |
| 543 | val &= DBOFF_MASK; |
| 544 | ctrl->dba = (struct xhci_doorbell_array *)((char *)hccr + val); |
| 545 | |
| 546 | /* write the address of runtime register */ |
| 547 | val = xhci_readl(&hccr->cr_rtsoff); |
| 548 | val &= RTSOFF_MASK; |
| 549 | ctrl->run_regs = (struct xhci_run_regs *)((char *)hccr + val); |
| 550 | |
| 551 | /* writting the address of ir_set structure */ |
| 552 | ctrl->ir_set = &ctrl->run_regs->ir_set[0]; |
| 553 | |
| 554 | /* Event ring does not maintain link TRB */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 555 | ctrl->event_ring = xhci_ring_alloc(ctrl, ERST_NUM_SEGS, false); |
Heinrich Schuchardt | 01adeb3 | 2020-09-29 22:03:01 +0200 | [diff] [blame] | 556 | ctrl->erst.entries = xhci_malloc(sizeof(struct xhci_erst_entry) * |
| 557 | ERST_NUM_SEGS); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 558 | |
| 559 | ctrl->erst.num_entries = ERST_NUM_SEGS; |
| 560 | |
| 561 | for (val = 0, seg = ctrl->event_ring->first_seg; |
| 562 | val < ERST_NUM_SEGS; |
| 563 | val++) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 564 | struct xhci_erst_entry *entry = &ctrl->erst.entries[val]; |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 565 | trb_64 = xhci_virt_to_bus(ctrl, seg->trbs); |
Stefan Roese | 83b1751 | 2020-07-21 10:46:03 +0200 | [diff] [blame] | 566 | entry->seg_addr = cpu_to_le64(trb_64); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 567 | entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT); |
| 568 | entry->rsvd = 0; |
| 569 | seg = seg->next; |
| 570 | } |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 571 | xhci_flush_cache((uintptr_t)ctrl->erst.entries, |
| 572 | ERST_NUM_SEGS * sizeof(struct xhci_erst_entry)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 573 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 574 | deq = xhci_virt_to_bus(ctrl, ctrl->event_ring->dequeue); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 575 | |
| 576 | /* Update HC event ring dequeue pointer */ |
| 577 | xhci_writeq(&ctrl->ir_set->erst_dequeue, |
| 578 | (u64)deq & (u64)~ERST_PTR_MASK); |
| 579 | |
| 580 | /* set ERST count with the number of entries in the segment table */ |
| 581 | val = xhci_readl(&ctrl->ir_set->erst_size); |
| 582 | val &= ERST_SIZE_MASK; |
| 583 | val |= ERST_NUM_SEGS; |
| 584 | xhci_writel(&ctrl->ir_set->erst_size, val); |
| 585 | |
| 586 | /* this is the event ring segment table pointer */ |
| 587 | val_64 = xhci_readq(&ctrl->ir_set->erst_base); |
| 588 | val_64 &= ERST_PTR_MASK; |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 589 | val_64 |= xhci_virt_to_bus(ctrl, ctrl->erst.entries) & ~ERST_PTR_MASK; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 590 | |
| 591 | xhci_writeq(&ctrl->ir_set->erst_base, val_64); |
| 592 | |
Bin Meng | 8a3f9cf | 2017-07-19 21:49:55 +0800 | [diff] [blame] | 593 | /* set up the scratchpad buffer array and scratchpad buffers */ |
| 594 | xhci_scratchpad_alloc(ctrl); |
| 595 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 596 | /* initializing the virtual devices to NULL */ |
| 597 | for (i = 0; i < MAX_HC_SLOTS; ++i) |
| 598 | ctrl->devs[i] = NULL; |
| 599 | |
| 600 | /* |
| 601 | * Just Zero'ing this register completely, |
| 602 | * or some spurious Device Notification Events |
| 603 | * might screw things here. |
| 604 | */ |
| 605 | xhci_writel(&hcor->or_dnctrl, 0x0); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Give the input control context for the passed container context |
| 612 | * |
| 613 | * @param ctx pointer to the context |
| 614 | * @return pointer to the Input control context data |
| 615 | */ |
| 616 | struct xhci_input_control_ctx |
| 617 | *xhci_get_input_control_ctx(struct xhci_container_ctx *ctx) |
| 618 | { |
| 619 | BUG_ON(ctx->type != XHCI_CTX_TYPE_INPUT); |
| 620 | return (struct xhci_input_control_ctx *)ctx->bytes; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Give the slot context for the passed container context |
| 625 | * |
| 626 | * @param ctrl Host controller data structure |
| 627 | * @param ctx pointer to the context |
| 628 | * @return pointer to the slot control context data |
| 629 | */ |
| 630 | struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctrl *ctrl, |
| 631 | struct xhci_container_ctx *ctx) |
| 632 | { |
| 633 | if (ctx->type == XHCI_CTX_TYPE_DEVICE) |
| 634 | return (struct xhci_slot_ctx *)ctx->bytes; |
| 635 | |
| 636 | return (struct xhci_slot_ctx *) |
Aaron Williams | fd062eb | 2021-04-06 12:10:17 +0200 | [diff] [blame] | 637 | (ctx->bytes + CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams))); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Gets the EP context from based on the ep_index |
| 642 | * |
| 643 | * @param ctrl Host controller data structure |
| 644 | * @param ctx context container |
| 645 | * @param ep_index index of the endpoint |
| 646 | * @return pointer to the End point context |
| 647 | */ |
| 648 | struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctrl *ctrl, |
| 649 | struct xhci_container_ctx *ctx, |
| 650 | unsigned int ep_index) |
| 651 | { |
| 652 | /* increment ep index by offset of start of ep ctx array */ |
| 653 | ep_index++; |
| 654 | if (ctx->type == XHCI_CTX_TYPE_INPUT) |
| 655 | ep_index++; |
| 656 | |
| 657 | return (struct xhci_ep_ctx *) |
| 658 | (ctx->bytes + |
Aaron Williams | fd062eb | 2021-04-06 12:10:17 +0200 | [diff] [blame] | 659 | (ep_index * CTX_SIZE(xhci_readl(&ctrl->hccr->cr_hccparams)))); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Copy output xhci_ep_ctx to the input xhci_ep_ctx copy. |
| 664 | * Useful when you want to change one particular aspect of the endpoint |
| 665 | * and then issue a configure endpoint command. |
| 666 | * |
| 667 | * @param ctrl Host controller data structure |
| 668 | * @param in_ctx contains the input context |
| 669 | * @param out_ctx contains the input context |
| 670 | * @param ep_index index of the end point |
| 671 | * @return none |
| 672 | */ |
| 673 | void xhci_endpoint_copy(struct xhci_ctrl *ctrl, |
| 674 | struct xhci_container_ctx *in_ctx, |
| 675 | struct xhci_container_ctx *out_ctx, |
| 676 | unsigned int ep_index) |
| 677 | { |
| 678 | struct xhci_ep_ctx *out_ep_ctx; |
| 679 | struct xhci_ep_ctx *in_ep_ctx; |
| 680 | |
| 681 | out_ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); |
| 682 | in_ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); |
| 683 | |
| 684 | in_ep_ctx->ep_info = out_ep_ctx->ep_info; |
| 685 | in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2; |
| 686 | in_ep_ctx->deq = out_ep_ctx->deq; |
| 687 | in_ep_ctx->tx_info = out_ep_ctx->tx_info; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Copy output xhci_slot_ctx to the input xhci_slot_ctx. |
| 692 | * Useful when you want to change one particular aspect of the endpoint |
| 693 | * and then issue a configure endpoint command. |
| 694 | * Only the context entries field matters, but |
| 695 | * we'll copy the whole thing anyway. |
| 696 | * |
| 697 | * @param ctrl Host controller data structure |
| 698 | * @param in_ctx contains the inpout context |
| 699 | * @param out_ctx contains the inpout context |
| 700 | * @return none |
| 701 | */ |
| 702 | void xhci_slot_copy(struct xhci_ctrl *ctrl, struct xhci_container_ctx *in_ctx, |
| 703 | struct xhci_container_ctx *out_ctx) |
| 704 | { |
| 705 | struct xhci_slot_ctx *in_slot_ctx; |
| 706 | struct xhci_slot_ctx *out_slot_ctx; |
| 707 | |
| 708 | in_slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); |
| 709 | out_slot_ctx = xhci_get_slot_ctx(ctrl, out_ctx); |
| 710 | |
| 711 | in_slot_ctx->dev_info = out_slot_ctx->dev_info; |
| 712 | in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2; |
| 713 | in_slot_ctx->tt_info = out_slot_ctx->tt_info; |
| 714 | in_slot_ctx->dev_state = out_slot_ctx->dev_state; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Setup an xHCI virtual device for a Set Address command |
| 719 | * |
| 720 | * @param udev pointer to the Device Data Structure |
| 721 | * @return returns negative value on failure else 0 on success |
| 722 | */ |
Bin Meng | 1459ce6 | 2017-07-19 21:51:14 +0800 | [diff] [blame] | 723 | void xhci_setup_addressable_virt_dev(struct xhci_ctrl *ctrl, |
| 724 | struct usb_device *udev, int hop_portnr) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 725 | { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 726 | struct xhci_virt_device *virt_dev; |
| 727 | struct xhci_ep_ctx *ep0_ctx; |
| 728 | struct xhci_slot_ctx *slot_ctx; |
| 729 | u32 port_num = 0; |
| 730 | u64 trb_64 = 0; |
Bin Meng | 1459ce6 | 2017-07-19 21:51:14 +0800 | [diff] [blame] | 731 | int slot_id = udev->slot_id; |
| 732 | int speed = udev->speed; |
Bin Meng | 63c3b3b | 2017-07-19 21:51:15 +0800 | [diff] [blame] | 733 | int route = 0; |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 734 | #if CONFIG_IS_ENABLED(DM_USB) |
Bin Meng | 63c3b3b | 2017-07-19 21:51:15 +0800 | [diff] [blame] | 735 | struct usb_device *dev = udev; |
| 736 | struct usb_hub_device *hub; |
| 737 | #endif |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 738 | |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 739 | virt_dev = ctrl->devs[slot_id]; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 740 | |
| 741 | BUG_ON(!virt_dev); |
| 742 | |
| 743 | /* Extract the EP0 and Slot Ctrl */ |
| 744 | ep0_ctx = xhci_get_ep_ctx(ctrl, virt_dev->in_ctx, 0); |
| 745 | slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->in_ctx); |
| 746 | |
| 747 | /* Only the control endpoint is valid - one endpoint context */ |
Bin Meng | 63c3b3b | 2017-07-19 21:51:15 +0800 | [diff] [blame] | 748 | slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); |
| 749 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 750 | #if CONFIG_IS_ENABLED(DM_USB) |
Bin Meng | 63c3b3b | 2017-07-19 21:51:15 +0800 | [diff] [blame] | 751 | /* Calculate the route string for this device */ |
| 752 | port_num = dev->portnr; |
| 753 | while (!usb_hub_is_root_hub(dev->dev)) { |
| 754 | hub = dev_get_uclass_priv(dev->dev); |
| 755 | /* |
| 756 | * Each hub in the topology is expected to have no more than |
| 757 | * 15 ports in order for the route string of a device to be |
| 758 | * unique. SuperSpeed hubs are restricted to only having 15 |
| 759 | * ports, but FS/LS/HS hubs are not. The xHCI specification |
| 760 | * says that if the port number the device is greater than 15, |
| 761 | * that portion of the route string shall be set to 15. |
| 762 | */ |
| 763 | if (port_num > 15) |
| 764 | port_num = 15; |
| 765 | route |= port_num << (hub->hub_depth * 4); |
| 766 | dev = dev_get_parent_priv(dev->dev); |
| 767 | port_num = dev->portnr; |
| 768 | dev = dev_get_parent_priv(dev->dev->parent); |
| 769 | } |
| 770 | |
| 771 | debug("route string %x\n", route); |
| 772 | #endif |
Stefan Roese | cb57086 | 2020-07-21 10:46:02 +0200 | [diff] [blame] | 773 | slot_ctx->dev_info |= cpu_to_le32(route); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 774 | |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 775 | switch (speed) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 776 | case USB_SPEED_SUPER: |
| 777 | slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS); |
| 778 | break; |
| 779 | case USB_SPEED_HIGH: |
| 780 | slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS); |
| 781 | break; |
| 782 | case USB_SPEED_FULL: |
| 783 | slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS); |
| 784 | break; |
| 785 | case USB_SPEED_LOW: |
| 786 | slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS); |
| 787 | break; |
| 788 | default: |
| 789 | /* Speed was set earlier, this shouldn't happen. */ |
| 790 | BUG(); |
| 791 | } |
| 792 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 793 | #if CONFIG_IS_ENABLED(DM_USB) |
Bin Meng | fe3327f | 2017-07-19 21:51:21 +0800 | [diff] [blame] | 794 | /* Set up TT fields to support FS/LS devices */ |
| 795 | if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) { |
Bin Meng | bb4b105 | 2017-09-18 06:40:39 -0700 | [diff] [blame] | 796 | struct udevice *parent = udev->dev; |
| 797 | |
| 798 | dev = udev; |
| 799 | do { |
| 800 | port_num = dev->portnr; |
| 801 | dev = dev_get_parent_priv(parent); |
| 802 | if (usb_hub_is_root_hub(dev->dev)) |
| 803 | break; |
| 804 | parent = dev->dev->parent; |
| 805 | } while (dev->speed != USB_SPEED_HIGH); |
| 806 | |
| 807 | if (!usb_hub_is_root_hub(dev->dev)) { |
| 808 | hub = dev_get_uclass_priv(dev->dev); |
Bin Meng | fe3327f | 2017-07-19 21:51:21 +0800 | [diff] [blame] | 809 | if (hub->tt.multi) |
| 810 | slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); |
Bin Meng | bb4b105 | 2017-09-18 06:40:39 -0700 | [diff] [blame] | 811 | slot_ctx->tt_info |= cpu_to_le32(TT_PORT(port_num)); |
Bin Meng | fe3327f | 2017-07-19 21:51:21 +0800 | [diff] [blame] | 812 | slot_ctx->tt_info |= cpu_to_le32(TT_SLOT(dev->slot_id)); |
| 813 | } |
| 814 | } |
| 815 | #endif |
| 816 | |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 817 | port_num = hop_portnr; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 818 | debug("port_num = %d\n", port_num); |
| 819 | |
| 820 | slot_ctx->dev_info2 |= |
| 821 | cpu_to_le32(((port_num & ROOT_HUB_PORT_MASK) << |
| 822 | ROOT_HUB_PORT_SHIFT)); |
| 823 | |
| 824 | /* Step 4 - ring already allocated */ |
| 825 | /* Step 5 */ |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 826 | ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP)); |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 827 | debug("SPEED = %d\n", speed); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 828 | |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 829 | switch (speed) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 830 | case USB_SPEED_SUPER: |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 831 | ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(512)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 832 | debug("Setting Packet size = 512bytes\n"); |
| 833 | break; |
| 834 | case USB_SPEED_HIGH: |
| 835 | /* USB core guesses at a 64-byte max packet first for FS devices */ |
| 836 | case USB_SPEED_FULL: |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 837 | ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(64)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 838 | debug("Setting Packet size = 64bytes\n"); |
| 839 | break; |
| 840 | case USB_SPEED_LOW: |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 841 | ep0_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(8)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 842 | debug("Setting Packet size = 8bytes\n"); |
| 843 | break; |
| 844 | default: |
| 845 | /* New speed? */ |
| 846 | BUG(); |
| 847 | } |
| 848 | |
| 849 | /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */ |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 850 | ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 851 | |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 852 | trb_64 = xhci_virt_to_bus(ctrl, virt_dev->eps[0].ring->first_seg->trbs); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 853 | ep0_ctx->deq = cpu_to_le64(trb_64 | virt_dev->eps[0].ring->cycle_state); |
| 854 | |
Bin Meng | c03fb20 | 2017-09-18 06:40:50 -0700 | [diff] [blame] | 855 | /* |
| 856 | * xHCI spec 6.2.3: |
| 857 | * software shall set 'Average TRB Length' to 8 for control endpoints. |
| 858 | */ |
| 859 | ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(8)); |
| 860 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 861 | /* Steps 7 and 8 were done in xhci_alloc_virt_device() */ |
| 862 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 863 | xhci_flush_cache((uintptr_t)ep0_ctx, sizeof(struct xhci_ep_ctx)); |
| 864 | xhci_flush_cache((uintptr_t)slot_ctx, sizeof(struct xhci_slot_ctx)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 865 | } |