Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Patrick Georgi |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 5 | * Copyright (C) 2013 secunet Security Networks AG |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 31 | //#define XHCI_SPEW_DEBUG |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 32 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 33 | #include <inttypes.h> |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 34 | #include <arch/virtual.h> |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 35 | #include "xhci_private.h" |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 36 | #include "xhci.h" |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 37 | |
| 38 | static void xhci_start (hci_t *controller); |
| 39 | static void xhci_stop (hci_t *controller); |
| 40 | static void xhci_reset (hci_t *controller); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 41 | static void xhci_reinit (hci_t *controller); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 42 | static void xhci_shutdown (hci_t *controller); |
| 43 | static int xhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize); |
| 44 | static int xhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, |
| 45 | int dalen, u8 *data); |
| 46 | static void* xhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming); |
| 47 | static void xhci_destroy_intr_queue (endpoint_t *ep, void *queue); |
| 48 | static u8* xhci_poll_intr_queue (void *queue); |
| 49 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 50 | /* |
| 51 | * Some structures must not cross page boundaries. To get this, |
| 52 | * we align them by their size (or the next greater power of 2). |
| 53 | */ |
| 54 | void * |
| 55 | xhci_align(const size_t min_align, const size_t size) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 56 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 57 | size_t align; |
| 58 | if (!(size & (size - 1))) |
| 59 | align = size; /* It's a power of 2 */ |
| 60 | else |
| 61 | align = 1 << ((sizeof(unsigned) << 3) - __builtin_clz(size)); |
| 62 | if (align < min_align) |
| 63 | align = min_align; |
| 64 | xhci_spew("Aligning %zu to %zu\n", size, align); |
| 65 | return memalign(align, size); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | xhci_clear_trb(trb_t *const trb, const int pcs) |
| 70 | { |
| 71 | trb->ptr_low = 0; |
| 72 | trb->ptr_high = 0; |
| 73 | trb->status = 0; |
| 74 | trb->control = !pcs; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | xhci_init_cycle_ring(transfer_ring_t *const tr, const size_t ring_size) |
| 79 | { |
| 80 | memset((void *)tr->ring, 0, ring_size * sizeof(*tr->ring)); |
| 81 | TRB_SET(TT, &tr->ring[ring_size - 1], TRB_LINK); |
| 82 | TRB_SET(TC, &tr->ring[ring_size - 1], 1); |
| 83 | /* only one segment that points to itself */ |
| 84 | tr->ring[ring_size - 1].ptr_low = virt_to_phys(tr->ring); |
| 85 | |
| 86 | tr->pcs = 1; |
| 87 | tr->cur = tr->ring; |
| 88 | } |
| 89 | |
| 90 | /* On Panther Point: switch ports shared with EHCI to xHCI */ |
| 91 | static void |
| 92 | xhci_switch_ppt_ports(pcidev_t addr) |
| 93 | { |
| 94 | if (pci_read_config32(addr, 0x00) == 0x1e318086) { |
| 95 | u32 reg32 = pci_read_config32(addr, 0xdc) & 0xf; |
| 96 | xhci_debug("Ports capable of SuperSpeed: 0x%"PRIx32"\n", reg32); |
| 97 | |
| 98 | /* For now, do not enable SuperSpeed on any ports */ |
| 99 | //pci_write_config32(addr, 0xd8, reg32); |
| 100 | pci_write_config32(addr, 0xd8, 0x00000000); |
| 101 | reg32 = pci_read_config32(addr, 0xd8) & 0xf; |
| 102 | xhci_debug("Configured for SuperSpeed: 0x%"PRIx32"\n", reg32); |
| 103 | |
| 104 | reg32 = pci_read_config32(addr, 0xd4) & 0xf; |
| 105 | xhci_debug("Trying to switch over: 0x%"PRIx32"\n", reg32); |
| 106 | |
| 107 | pci_write_config32(addr, 0xd0, reg32); |
| 108 | reg32 = pci_read_config32(addr, 0xd0) & 0xf; |
| 109 | xhci_debug("Actually switched over: 0x%"PRIx32"\n", reg32); |
| 110 | } |
| 111 | } |
| 112 | |
Nico Huber | c371442 | 2013-07-19 14:03:47 +0200 | [diff] [blame] | 113 | /* On Panther Point: switch all ports back to EHCI */ |
| 114 | static void |
| 115 | xhci_switchback_ppt_ports(pcidev_t addr) |
| 116 | { |
| 117 | if (pci_read_config32(addr, 0x00) == 0x1e318086) { |
| 118 | u32 reg32 = pci_read_config32(addr, 0xd0) & 0xf; |
| 119 | xhci_debug("Switching ports back: 0x%"PRIx32"\n", reg32); |
| 120 | pci_write_config32(addr, 0xd0, 0x00000000); |
| 121 | reg32 = pci_read_config32(addr, 0xd0) & 0xf; |
| 122 | xhci_debug("Still switched to xHCI: 0x%"PRIx32"\n", reg32); |
| 123 | } |
| 124 | } |
| 125 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 126 | static long |
| 127 | xhci_handshake(volatile u32 *const reg, u32 mask, u32 wait_for, long timeout_us) |
| 128 | { |
| 129 | while ((*reg & mask) != wait_for && timeout_us--) udelay(1); |
| 130 | return timeout_us; |
| 131 | } |
| 132 | |
| 133 | static int |
| 134 | xhci_wait_ready(xhci_t *const xhci) |
| 135 | { |
| 136 | xhci_debug("Waiting for controller to be ready... "); |
| 137 | if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_CNR, 0, 100000L)) { |
| 138 | usb_debug("timeout!\n"); |
| 139 | return -1; |
| 140 | } |
| 141 | usb_debug("ok.\n"); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | hci_t * |
Nico Huber | 6e23066 | 2014-07-07 16:33:59 +0200 | [diff] [blame] | 146 | xhci_init (unsigned long physical_bar) |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 147 | { |
| 148 | int i; |
| 149 | |
| 150 | /* First, allocate and initialize static controller structures */ |
| 151 | |
| 152 | hci_t *const controller = new_controller(); |
| 153 | if (!controller) { |
| 154 | xhci_debug("Could not create USB controller instance\n"); |
| 155 | return controller; |
| 156 | } |
| 157 | |
| 158 | controller->type = XHCI; |
| 159 | controller->start = xhci_start; |
| 160 | controller->stop = xhci_stop; |
| 161 | controller->reset = xhci_reset; |
| 162 | controller->init = xhci_reinit; |
| 163 | controller->shutdown = xhci_shutdown; |
| 164 | controller->bulk = xhci_bulk; |
| 165 | controller->control = xhci_control; |
| 166 | controller->set_address = xhci_set_address; |
| 167 | controller->finish_device_config= xhci_finish_device_config; |
| 168 | controller->destroy_device = xhci_destroy_dev; |
| 169 | controller->create_intr_queue = xhci_create_intr_queue; |
| 170 | controller->destroy_intr_queue = xhci_destroy_intr_queue; |
| 171 | controller->poll_intr_queue = xhci_poll_intr_queue; |
Nico Huber | 8b8e963 | 2014-07-07 17:20:53 +0200 | [diff] [blame] | 172 | controller->pcidev = 0; |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 173 | for (i = 0; i < 128; ++i) { |
| 174 | controller->devices[i] = NULL; |
| 175 | } |
| 176 | |
| 177 | controller->instance = malloc(sizeof(xhci_t)); |
| 178 | if (!controller->instance) { |
| 179 | xhci_debug("Out of memory creating xHCI controller instance\n"); |
| 180 | goto _free_controller; |
| 181 | } |
| 182 | xhci_t *const xhci = (xhci_t *)controller->instance; |
| 183 | memset(xhci, 0x00, sizeof(*xhci)); |
| 184 | |
| 185 | init_device_entry(controller, 0); |
| 186 | xhci->roothub = controller->devices[0]; |
| 187 | xhci->cr.ring = xhci_align(64, COMMAND_RING_SIZE * sizeof(trb_t)); |
| 188 | xhci->er.ring = xhci_align(64, EVENT_RING_SIZE * sizeof(trb_t)); |
| 189 | xhci->ev_ring_table = xhci_align(64, sizeof(erst_entry_t)); |
| 190 | if (!xhci->roothub || !xhci->cr.ring || |
| 191 | !xhci->er.ring || !xhci->ev_ring_table) { |
| 192 | xhci_debug("Out of memory\n"); |
| 193 | goto _free_xhci; |
| 194 | } |
| 195 | |
Nico Huber | 5b9e6f1 | 2014-07-10 12:56:34 +0200 | [diff] [blame] | 196 | xhci->capreg = phys_to_virt(physical_bar); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 197 | xhci->opreg = ((void *)xhci->capreg) + xhci->capreg->caplength; |
| 198 | xhci->hcrreg = ((void *)xhci->capreg) + xhci->capreg->rtsoff; |
| 199 | xhci->dbreg = ((void *)xhci->capreg) + xhci->capreg->dboff; |
Nico Huber | 5b9e6f1 | 2014-07-10 12:56:34 +0200 | [diff] [blame] | 200 | xhci_debug("regbase: 0x%"PRIx32"\n", physical_bar); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 201 | xhci_debug("caplen: 0x%"PRIx32"\n", xhci->capreg->caplength); |
| 202 | xhci_debug("rtsoff: 0x%"PRIx32"\n", xhci->capreg->rtsoff); |
| 203 | xhci_debug("dboff: 0x%"PRIx32"\n", xhci->capreg->dboff); |
| 204 | |
| 205 | xhci_debug("hciversion: %"PRIx8".%"PRIx8"\n", |
| 206 | xhci->capreg->hciver_hi, xhci->capreg->hciver_lo); |
| 207 | if ((xhci->capreg->hciversion < 0x96) || |
| 208 | (xhci->capreg->hciversion > 0x100)) { |
| 209 | xhci_debug("Unsupported xHCI version\n"); |
| 210 | goto _free_xhci; |
| 211 | } |
| 212 | |
| 213 | xhci_debug("context size: %dB\n", xhci->capreg->csz ? 64 : 32); |
| 214 | if (xhci->capreg->csz) { |
| 215 | xhci_debug("Only 32B contexts are supported\n"); |
| 216 | goto _free_xhci; |
| 217 | } |
| 218 | |
| 219 | xhci_debug("maxslots: 0x%02lx\n", xhci->capreg->MaxSlots); |
| 220 | xhci_debug("maxports: 0x%02lx\n", xhci->capreg->MaxPorts); |
| 221 | const unsigned pagesize = xhci->opreg->pagesize << 12; |
| 222 | xhci_debug("pagesize: 0x%04x\n", pagesize); |
| 223 | |
| 224 | /* |
| 225 | * We haven't touched the hardware yet. So we allocate all dynamic |
| 226 | * structures at first and can still chicken out easily if we run out |
| 227 | * of memory. |
| 228 | */ |
| 229 | const size_t dcbaa_size = (xhci->capreg->MaxSlots + 1) * sizeof(u64); |
| 230 | xhci->dcbaa = xhci_align(64, dcbaa_size); |
| 231 | if (!xhci->dcbaa) { |
| 232 | xhci_debug("Out of memory\n"); |
| 233 | goto _free_xhci; |
| 234 | } |
| 235 | memset((void*)xhci->dcbaa, 0x00, dcbaa_size); |
| 236 | |
| 237 | /* |
| 238 | * Let dcbaa[0] point to another array of pointers, sp_ptrs. |
| 239 | * The pointers therein point to scratchpad buffers (pages). |
| 240 | */ |
| 241 | const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs; |
| 242 | xhci_debug("max scratchpad bufs: 0x%zx\n", max_sp_bufs); |
| 243 | if (max_sp_bufs) { |
| 244 | const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64); |
| 245 | xhci->sp_ptrs = xhci_align(64, sp_ptrs_size); |
| 246 | if (!xhci->sp_ptrs) { |
| 247 | xhci_debug("Out of memory\n"); |
| 248 | goto _free_xhci_structs; |
| 249 | } |
| 250 | memset(xhci->sp_ptrs, 0x00, sp_ptrs_size); |
| 251 | for (i = 0; i < max_sp_bufs; ++i) { |
| 252 | /* Could use mmap() here if we had it. |
| 253 | Maybe there is another way. */ |
| 254 | void *const page = memalign(pagesize, pagesize); |
| 255 | if (!page) { |
| 256 | xhci_debug("Out of memory\n"); |
| 257 | goto _free_xhci_structs; |
| 258 | } |
| 259 | xhci->sp_ptrs[i] = virt_to_phys(page); |
| 260 | } |
| 261 | xhci->dcbaa[0] = virt_to_phys(xhci->sp_ptrs); |
| 262 | } |
| 263 | |
| 264 | /* Now start working on the hardware */ |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 265 | if (xhci_wait_ready(xhci)) |
| 266 | goto _free_xhci; |
| 267 | |
| 268 | /* TODO: Check if BIOS claims ownership (and hand over) */ |
| 269 | |
| 270 | xhci_reset(controller); |
| 271 | xhci_reinit(controller); |
| 272 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 273 | xhci->roothub->controller = controller; |
| 274 | xhci->roothub->init = xhci_rh_init; |
| 275 | xhci->roothub->init(xhci->roothub); |
| 276 | |
| 277 | return controller; |
| 278 | |
| 279 | _free_xhci_structs: |
| 280 | if (xhci->sp_ptrs) { |
| 281 | for (i = 0; i < max_sp_bufs; ++i) { |
| 282 | if (xhci->sp_ptrs[i]) |
| 283 | free(phys_to_virt(xhci->sp_ptrs[i])); |
| 284 | } |
| 285 | } |
| 286 | free(xhci->sp_ptrs); |
| 287 | free(xhci->dcbaa); |
| 288 | _free_xhci: |
| 289 | free((void *)xhci->ev_ring_table); |
| 290 | free((void *)xhci->er.ring); |
| 291 | free((void *)xhci->cr.ring); |
| 292 | free(xhci->roothub); |
| 293 | free(xhci); |
| 294 | _free_controller: |
| 295 | detach_controller(controller); |
| 296 | free(controller); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 300 | #ifdef CONFIG_LP_USB_PCI |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 301 | hci_t * |
| 302 | xhci_pci_init (pcidev_t addr) |
| 303 | { |
| 304 | u32 reg_addr; |
Patrick Georgi | fdb348a | 2013-12-21 11:41:22 +0100 | [diff] [blame] | 305 | hci_t *controller; |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 306 | |
Nico Huber | 6a05890 | 2014-07-04 18:17:39 +0200 | [diff] [blame] | 307 | reg_addr = pci_read_config32 (addr, 0x10) & ~0xf; |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 308 | if (pci_read_config32 (addr, 0x14) > 0) { |
| 309 | fatal("We don't do 64bit addressing.\n"); |
| 310 | } |
| 311 | |
Nico Huber | 6e23066 | 2014-07-07 16:33:59 +0200 | [diff] [blame] | 312 | controller = xhci_init((unsigned long)reg_addr); |
Nico Huber | f4316f8 | 2014-07-07 17:11:53 +0200 | [diff] [blame] | 313 | if (controller) { |
| 314 | controller->pcidev = addr; |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 315 | |
Nico Huber | f4316f8 | 2014-07-07 17:11:53 +0200 | [diff] [blame] | 316 | xhci_switch_ppt_ports(addr); |
| 317 | } |
Stefan Reinauer | 8992e53 | 2013-05-02 16:16:41 -0700 | [diff] [blame] | 318 | |
| 319 | return controller; |
| 320 | } |
| 321 | #endif |
| 322 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 323 | static void |
| 324 | xhci_reset(hci_t *const controller) |
| 325 | { |
| 326 | xhci_t *const xhci = XHCI_INST(controller); |
| 327 | |
| 328 | xhci_stop(controller); |
| 329 | |
| 330 | xhci->opreg->usbcmd |= USBCMD_HCRST; |
| 331 | xhci_debug("Resetting controller... "); |
| 332 | if (!xhci_handshake(&xhci->opreg->usbcmd, USBCMD_HCRST, 0, 1000000L)) |
| 333 | usb_debug("timeout!\n"); |
| 334 | else |
| 335 | usb_debug("ok.\n"); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 338 | static void |
| 339 | xhci_reinit (hci_t *controller) |
| 340 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 341 | xhci_t *const xhci = XHCI_INST(controller); |
Nico Huber | 6e711c6 | 2012-11-12 16:20:32 +0100 | [diff] [blame] | 342 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 343 | if (xhci_wait_ready(xhci)) |
| 344 | return; |
| 345 | |
| 346 | /* Enable all available slots */ |
Gabe Black | 1ee2c6d | 2013-08-09 04:27:35 -0700 | [diff] [blame] | 347 | xhci->opreg->config = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn; |
| 348 | xhci->max_slots_en = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn; |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 349 | |
| 350 | /* Set DCBAA */ |
| 351 | xhci->opreg->dcbaap_lo = virt_to_phys(xhci->dcbaa); |
| 352 | xhci->opreg->dcbaap_hi = 0; |
| 353 | |
| 354 | /* Initialize command ring */ |
| 355 | xhci_init_cycle_ring(&xhci->cr, COMMAND_RING_SIZE); |
| 356 | xhci_debug("command ring @%p (0x%08x)\n", |
| 357 | xhci->cr.ring, virt_to_phys(xhci->cr.ring)); |
| 358 | xhci->opreg->crcr_lo = virt_to_phys(xhci->cr.ring) | CRCR_RCS; |
| 359 | xhci->opreg->crcr_hi = 0; |
| 360 | |
| 361 | /* Make sure interrupts are disabled */ |
| 362 | xhci->opreg->usbcmd &= ~USBCMD_INTE; |
| 363 | |
| 364 | /* Initialize event ring */ |
| 365 | xhci_reset_event_ring(&xhci->er); |
| 366 | xhci_debug("event ring @%p (0x%08x)\n", |
| 367 | xhci->er.ring, virt_to_phys(xhci->er.ring)); |
| 368 | xhci_debug("ERST Max: 0x%lx -> 0x%lx entries\n", |
| 369 | xhci->capreg->ERST_Max, 1 << xhci->capreg->ERST_Max); |
| 370 | memset((void*)xhci->ev_ring_table, 0x00, sizeof(erst_entry_t)); |
| 371 | xhci->ev_ring_table[0].seg_base_lo = virt_to_phys(xhci->er.ring); |
| 372 | xhci->ev_ring_table[0].seg_base_hi = 0; |
| 373 | xhci->ev_ring_table[0].seg_size = EVENT_RING_SIZE; |
| 374 | |
| 375 | /* Initialize primary interrupter */ |
| 376 | xhci->hcrreg->intrrs[0].erstsz = 1; |
| 377 | xhci_update_event_dq(xhci); |
| 378 | /* erstba has to be written at last */ |
| 379 | xhci->hcrreg->intrrs[0].erstba_lo = virt_to_phys(xhci->ev_ring_table); |
| 380 | xhci->hcrreg->intrrs[0].erstba_hi = 0; |
| 381 | |
| 382 | xhci_start(controller); |
| 383 | |
| 384 | #ifdef USB_DEBUG |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 385 | int i; |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 386 | for (i = 0; i < 32; ++i) { |
| 387 | xhci_debug("NOOP run #%d\n", i); |
| 388 | trb_t *const cmd = xhci_next_command_trb(xhci); |
| 389 | TRB_SET(TT, cmd, TRB_CMD_NOOP); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 390 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 391 | xhci_post_command(xhci); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 392 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 393 | /* Wait for result in event ring */ |
| 394 | xhci_wait_for_command_done(xhci, cmd, 1); |
| 395 | xhci_debug("Command ring is %srunning\n", |
| 396 | (xhci->opreg->crcr_lo & CRCR_CRR) ? "" : "not "); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 397 | } |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 398 | #endif |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 402 | xhci_shutdown(hci_t *const controller) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 403 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 404 | int i; |
| 405 | |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 406 | if (controller == 0) |
| 407 | return; |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 408 | xhci_t *const xhci = XHCI_INST(controller); |
| 409 | |
| 410 | detach_controller(controller); |
| 411 | |
| 412 | /* Detach device hierarchy (starting at root hub) */ |
| 413 | usb_detach_device(controller, 0); |
| 414 | |
| 415 | xhci_stop(controller); |
| 416 | |
Patrick Georgi | fdb348a | 2013-12-21 11:41:22 +0100 | [diff] [blame] | 417 | if (controller->pcidev) |
| 418 | xhci_switchback_ppt_ports(controller->pcidev); |
Nico Huber | c371442 | 2013-07-19 14:03:47 +0200 | [diff] [blame] | 419 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 420 | if (xhci->sp_ptrs) { |
| 421 | const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs; |
| 422 | for (i = 0; i < max_sp_bufs; ++i) { |
| 423 | if (xhci->sp_ptrs[i]) |
| 424 | free(phys_to_virt(xhci->sp_ptrs[i])); |
| 425 | } |
| 426 | } |
| 427 | free(xhci->sp_ptrs); |
| 428 | free(xhci->dcbaa); |
| 429 | free((void *)xhci->ev_ring_table); |
| 430 | free((void *)xhci->er.ring); |
| 431 | free((void *)xhci->cr.ring); |
| 432 | free(xhci); |
| 433 | free(controller); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void |
| 437 | xhci_start (hci_t *controller) |
| 438 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 439 | xhci_t *const xhci = XHCI_INST(controller); |
| 440 | |
| 441 | xhci->opreg->usbcmd |= USBCMD_RS; |
| 442 | if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_HCH, 0, 1000000L)) |
| 443 | xhci_debug("Controller didn't start within 1s\n"); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void |
| 447 | xhci_stop (hci_t *controller) |
| 448 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 449 | xhci_t *const xhci = XHCI_INST(controller); |
| 450 | |
| 451 | xhci->opreg->usbcmd &= ~USBCMD_RS; |
| 452 | if (!xhci_handshake(&xhci->opreg->usbsts, |
| 453 | USBSTS_HCH, USBSTS_HCH, 1000000L)) |
| 454 | xhci_debug("Controller didn't halt within 1s\n"); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 458 | xhci_reset_endpoint(usbdev_t *const dev, endpoint_t *const ep, |
| 459 | const int clear_halt) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 460 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 461 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 462 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, dev->address); |
| 463 | const int slot_id = dev->address; |
| 464 | const int ep_id = ep ? xhci_ep_id(ep) : 1; |
| 465 | |
| 466 | xhci_debug("Resetting ID %d EP %d (ep state: %d)\n", |
| 467 | slot_id, ep_id, EC_GET(STATE, di->devctx.eps[ep_id])); |
| 468 | |
| 469 | /* Run Reset Endpoint Command if the EP is in Halted state */ |
| 470 | if (EC_GET(STATE, di->devctx.eps[ep_id]) == 2) { |
| 471 | const int cc = xhci_cmd_reset_endpoint(xhci, slot_id, ep_id); |
| 472 | if (cc != CC_SUCCESS) { |
| 473 | xhci_debug("Reset Endpoint Command failed: %d\n", cc); |
| 474 | return 1; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /* Clear TT buffer for bulk and control endpoints behind a TT */ |
| 479 | const int hub = dev->hub; |
| 480 | if (hub && dev->speed < HIGH_SPEED && |
| 481 | dev->controller->devices[hub]->speed == HIGH_SPEED) |
| 482 | /* TODO */; |
| 483 | |
| 484 | /* Try clearing the device' halt condition on non-control endpoints */ |
| 485 | if (clear_halt && ep) |
| 486 | clear_stall(ep); |
| 487 | |
| 488 | /* Reset transfer ring if the endpoint is in the right state */ |
| 489 | const unsigned ep_state = EC_GET(STATE, di->devctx.eps[ep_id]); |
| 490 | if (ep_state == 3 || ep_state == 4) { |
| 491 | transfer_ring_t *const tr = di->transfer_rings[ep_id]; |
| 492 | const int cc = xhci_cmd_set_tr_dq(xhci, slot_id, ep_id, |
| 493 | tr->ring, 1); |
| 494 | if (cc != CC_SUCCESS) { |
| 495 | xhci_debug("Set TR Dequeue Command failed: %d\n", cc); |
| 496 | return 1; |
| 497 | } |
| 498 | xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE); |
| 499 | } |
| 500 | |
| 501 | xhci_debug("Finished resetting ID %d EP %d (ep state: %d)\n", |
| 502 | slot_id, ep_id, EC_GET(STATE, di->devctx.eps[ep_id])); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static void |
| 508 | xhci_enqueue_trb(transfer_ring_t *const tr) |
| 509 | { |
| 510 | const int chain = TRB_GET(CH, tr->cur); |
| 511 | TRB_SET(C, tr->cur, tr->pcs); |
| 512 | ++tr->cur; |
| 513 | |
| 514 | while (TRB_GET(TT, tr->cur) == TRB_LINK) { |
| 515 | xhci_spew("Handling LINK pointer\n"); |
| 516 | const int tc = TRB_GET(TC, tr->cur); |
| 517 | TRB_SET(CH, tr->cur, chain); |
| 518 | TRB_SET(C, tr->cur, tr->pcs); |
| 519 | tr->cur = phys_to_virt(tr->cur->ptr_low); |
| 520 | if (tc) |
| 521 | tr->pcs ^= 1; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | static void |
| 526 | xhci_enqueue_td(transfer_ring_t *const tr, const int ep, const size_t mps, |
| 527 | const int dalen, void *const data, const int dir) |
| 528 | { |
| 529 | trb_t *trb = NULL; /* cur TRB */ |
| 530 | u8 *cur_start = data; /* cur data pointer */ |
| 531 | size_t length = dalen; /* remaining bytes */ |
| 532 | size_t packets = (length + mps - 1) / mps; /* remaining packets */ |
| 533 | size_t residue = 0; /* residue from last TRB */ |
| 534 | size_t trb_count = 0; /* TRBs added so far */ |
| 535 | |
| 536 | while (length || !trb_count /* enqueue at least one */) { |
| 537 | const size_t cur_end = ((size_t)cur_start + 0x10000) & ~0xffff; |
| 538 | size_t cur_length = cur_end - (size_t)cur_start; |
| 539 | if (length < cur_length) { |
| 540 | cur_length = length; |
| 541 | packets = 0; |
| 542 | length = 0; |
| 543 | } else { |
| 544 | packets -= (residue + cur_length) / mps; |
| 545 | residue = (residue + cur_length) % mps; |
| 546 | length -= cur_length; |
| 547 | } |
| 548 | |
| 549 | trb = tr->cur; |
| 550 | xhci_clear_trb(trb, tr->pcs); |
| 551 | trb->ptr_low = virt_to_phys(cur_start); |
| 552 | TRB_SET(TL, trb, cur_length); |
| 553 | TRB_SET(TDS, trb, packets); |
Julius Werner | 83da501 | 2013-09-27 12:45:11 -0700 | [diff] [blame^] | 554 | TRB_SET(CH, trb, 1); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 555 | |
| 556 | /* Check for first, data stage TRB */ |
| 557 | if (!trb_count && ep == 1) { |
| 558 | TRB_SET(DIR, trb, dir); |
| 559 | TRB_SET(TT, trb, TRB_DATA_STAGE); |
| 560 | } else { |
| 561 | TRB_SET(TT, trb, TRB_NORMAL); |
| 562 | } |
| 563 | |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 564 | xhci_enqueue_trb(tr); |
| 565 | |
| 566 | cur_start += cur_length; |
| 567 | ++trb_count; |
| 568 | } |
Julius Werner | 83da501 | 2013-09-27 12:45:11 -0700 | [diff] [blame^] | 569 | |
| 570 | trb = tr->cur; |
| 571 | xhci_clear_trb(trb, tr->pcs); |
| 572 | trb->ptr_low = virt_to_phys(trb); /* for easier debugging only */ |
| 573 | TRB_SET(TT, trb, TRB_EVENT_DATA); |
| 574 | TRB_SET(IOC, trb, 1); |
| 575 | |
| 576 | xhci_enqueue_trb(tr); |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static int |
| 580 | xhci_control(usbdev_t *const dev, const direction_t dir, |
| 581 | const int drlen, void *const devreq, |
| 582 | const int dalen, unsigned char *const data) |
| 583 | { |
| 584 | xhci_t *const xhci = XHCI_INST(dev->controller); |
| 585 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, dev->address); |
| 586 | transfer_ring_t *const tr = di->transfer_rings[1]; |
| 587 | |
| 588 | const size_t off = (size_t)data & 0xffff; |
Julius Werner | 83da501 | 2013-09-27 12:45:11 -0700 | [diff] [blame^] | 589 | if ((off + dalen) > ((TRANSFER_RING_SIZE - 4) << 16)) { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 590 | xhci_debug("Unsupported transfer size\n"); |
| 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | /* Reset endpoint if it's halted */ |
| 595 | const unsigned ep_state = EC_GET(STATE, di->devctx.ep0); |
| 596 | if (ep_state == 2 || ep_state == 4) { |
| 597 | if (xhci_reset_endpoint(dev, NULL, 0)) |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | /* Fill and enqueue setup TRB */ |
| 602 | trb_t *const setup = tr->cur; |
| 603 | xhci_clear_trb(setup, tr->pcs); |
| 604 | setup->ptr_low = ((u32 *)devreq)[0]; |
| 605 | setup->ptr_high = ((u32 *)devreq)[1]; |
| 606 | TRB_SET(TL, setup, 8); |
| 607 | TRB_SET(TRT, setup, (dalen) |
| 608 | ? ((dir == OUT) ? TRB_TRT_OUT_DATA : TRB_TRT_IN_DATA) |
| 609 | : TRB_TRT_NO_DATA); |
| 610 | TRB_SET(TT, setup, TRB_SETUP_STAGE); |
| 611 | TRB_SET(IDT, setup, 1); |
| 612 | TRB_SET(IOC, setup, 1); |
| 613 | xhci_enqueue_trb(tr); |
| 614 | |
| 615 | /* Fill and enqueue data TRBs (if any) */ |
| 616 | if (dalen) { |
| 617 | const unsigned mps = EC_GET(MPS, di->devctx.ep0); |
| 618 | const unsigned dt_dir = (dir == OUT) ? TRB_DIR_OUT : TRB_DIR_IN; |
| 619 | xhci_enqueue_td(tr, 1, mps, dalen, data, dt_dir); |
| 620 | } |
| 621 | |
| 622 | /* Fill status TRB */ |
| 623 | trb_t *const status = tr->cur; |
| 624 | xhci_clear_trb(status, tr->pcs); |
| 625 | TRB_SET(DIR, status, (dir == OUT) ? TRB_DIR_IN : TRB_DIR_OUT); |
| 626 | TRB_SET(TT, status, TRB_STATUS_STAGE); |
| 627 | TRB_SET(IOC, status, 1); |
| 628 | xhci_enqueue_trb(tr); |
| 629 | |
| 630 | /* Ring doorbell for EP0 */ |
| 631 | xhci->dbreg[dev->address] = 1; |
| 632 | |
| 633 | /* Wait for transfer events */ |
| 634 | int i; |
| 635 | const int n_stages = 2 + !!dalen; |
| 636 | for (i = 0; i < n_stages; ++i) { |
| 637 | const int ret = xhci_wait_for_transfer(xhci, dev->address, 1); |
| 638 | if (ret != CC_SUCCESS) { |
| 639 | if (ret == TIMEOUT) { |
| 640 | xhci_debug("Stopping ID %d EP 1\n", |
| 641 | dev->address); |
| 642 | xhci_cmd_stop_endpoint(xhci, dev->address, 1); |
| 643 | } |
| 644 | xhci_debug("Stage %d/%d failed: %d\n" |
| 645 | " trb ring: @%p\n" |
| 646 | " setup trb: @%p\n" |
| 647 | " status trb: @%p\n" |
| 648 | " ep state: %d -> %d\n" |
| 649 | " usbsts: 0x%08"PRIx32"\n", |
| 650 | i, n_stages, ret, |
| 651 | tr->ring, setup, status, |
| 652 | ep_state, EC_GET(STATE, di->devctx.ep0), |
| 653 | xhci->opreg->usbsts); |
| 654 | return 1; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | return 0; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* finalize == 1: if data is of packet aligned size, add a zero length packet */ |
| 662 | static int |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 663 | xhci_bulk(endpoint_t *const ep, |
| 664 | const int size, u8 *const data, |
| 665 | const int finalize) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 666 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 667 | /* finalize: Hopefully the xHCI controller always does this. |
| 668 | We have no control over the packets. */ |
| 669 | |
| 670 | xhci_t *const xhci = XHCI_INST(ep->dev->controller); |
| 671 | const int ep_id = xhci_ep_id(ep); |
| 672 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address); |
| 673 | transfer_ring_t *const tr = di->transfer_rings[ep_id]; |
| 674 | |
| 675 | const size_t off = (size_t)data & 0xffff; |
Julius Werner | 83da501 | 2013-09-27 12:45:11 -0700 | [diff] [blame^] | 676 | if ((off + size) > ((TRANSFER_RING_SIZE - 2) << 16)) { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 677 | xhci_debug("Unsupported transfer size\n"); |
| 678 | return 1; |
| 679 | } |
| 680 | |
| 681 | /* Reset endpoint if it's halted */ |
| 682 | const unsigned ep_state = EC_GET(STATE, di->devctx.eps[ep_id]); |
| 683 | if (ep_state == 2 || ep_state == 4) { |
| 684 | if (xhci_reset_endpoint(ep->dev, ep, 0)) |
| 685 | return 1; |
| 686 | } |
| 687 | |
| 688 | /* Enqueue transfer and ring doorbell */ |
| 689 | const unsigned mps = EC_GET(MPS, di->devctx.eps[ep_id]); |
| 690 | const unsigned dir = (ep->direction == OUT) ? TRB_DIR_OUT : TRB_DIR_IN; |
| 691 | xhci_enqueue_td(tr, ep_id, mps, size, data, dir); |
| 692 | xhci->dbreg[ep->dev->address] = ep_id; |
| 693 | |
| 694 | /* Wait for transfer event */ |
| 695 | const int ret = xhci_wait_for_transfer(xhci, ep->dev->address, ep_id); |
| 696 | if (ret != CC_SUCCESS) { |
| 697 | if (ret == TIMEOUT) { |
| 698 | xhci_debug("Stopping ID %d EP %d\n", |
| 699 | ep->dev->address, ep_id); |
| 700 | xhci_cmd_stop_endpoint(xhci, ep->dev->address, ep_id); |
| 701 | } else if (ret == CC_STALL_ERROR) { |
| 702 | xhci_reset_endpoint(ep->dev, ep, 1); |
| 703 | } |
| 704 | xhci_debug("Bulk transfer failed: %d\n" |
| 705 | " ep state: %d -> %d\n" |
| 706 | " usbsts: 0x%08"PRIx32"\n", |
| 707 | ret, ep_state, |
| 708 | EC_GET(STATE, di->devctx.eps[ep_id]), |
| 709 | xhci->opreg->usbsts); |
| 710 | return 1; |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static trb_t * |
| 717 | xhci_next_trb(trb_t *cur, int *const pcs) |
| 718 | { |
| 719 | ++cur; |
| 720 | while (TRB_GET(TT, cur) == TRB_LINK) { |
| 721 | if (pcs && TRB_GET(TC, cur)) |
| 722 | *pcs ^= 1; |
| 723 | cur = phys_to_virt(cur->ptr_low); |
| 724 | } |
| 725 | return cur; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* create and hook-up an intr queue into device schedule */ |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 729 | static void * |
| 730 | xhci_create_intr_queue(endpoint_t *const ep, |
| 731 | const int reqsize, const int reqcount, |
| 732 | const int reqtiming) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 733 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 734 | /* reqtiming: We ignore it and use the interval from the |
| 735 | endpoint descriptor configured earlier. */ |
| 736 | |
| 737 | xhci_t *const xhci = XHCI_INST(ep->dev->controller); |
| 738 | const int ep_id = xhci_ep_id(ep); |
| 739 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address); |
| 740 | transfer_ring_t *const tr = di->transfer_rings[ep_id]; |
| 741 | |
| 742 | if (reqcount > (TRANSFER_RING_SIZE - 2)) { |
| 743 | xhci_debug("reqcount is too high, at most %d supported\n", |
| 744 | TRANSFER_RING_SIZE - 2); |
| 745 | return NULL; |
| 746 | } |
| 747 | if (reqsize > 0x10000) { |
| 748 | xhci_debug("reqsize is too large, at most 64KiB supported\n"); |
| 749 | return NULL; |
| 750 | } |
| 751 | if (di->interrupt_queues[ep_id]) { |
| 752 | xhci_debug("Only one interrupt queue per endpoint supported\n"); |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | /* Allocate intrq structure and reqdata chunks */ |
| 757 | |
| 758 | intrq_t *const intrq = malloc(sizeof(*intrq)); |
| 759 | if (!intrq) { |
| 760 | xhci_debug("Out of memory\n"); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | int i; |
| 765 | int pcs = tr->pcs; |
| 766 | trb_t *cur = tr->cur; |
| 767 | for (i = 0; i < reqcount; ++i) { |
| 768 | if (TRB_GET(C, cur) == pcs) { |
| 769 | xhci_debug("Not enough empty TRBs\n"); |
| 770 | goto _free_return; |
| 771 | } |
| 772 | void *const reqdata = xhci_align(1, reqsize); |
| 773 | if (!reqdata) { |
| 774 | xhci_debug("Out of memory\n"); |
| 775 | goto _free_return; |
| 776 | } |
| 777 | xhci_clear_trb(cur, pcs); |
| 778 | cur->ptr_low = virt_to_phys(reqdata); |
| 779 | cur->ptr_high = 0; |
| 780 | TRB_SET(TL, cur, reqsize); |
| 781 | TRB_SET(TT, cur, TRB_NORMAL); |
| 782 | TRB_SET(ISP, cur, 1); |
| 783 | TRB_SET(IOC, cur, 1); |
| 784 | |
| 785 | cur = xhci_next_trb(cur, &pcs); |
| 786 | } |
| 787 | |
| 788 | intrq->size = reqsize; |
| 789 | intrq->count = reqcount; |
| 790 | intrq->next = tr->cur; |
| 791 | intrq->ready = NULL; |
| 792 | intrq->ep = ep; |
| 793 | di->interrupt_queues[ep_id] = intrq; |
| 794 | |
| 795 | /* Now enqueue all the prepared TRBs but the last |
| 796 | and ring the doorbell. */ |
| 797 | for (i = 0; i < (reqcount - 1); ++i) |
| 798 | xhci_enqueue_trb(tr); |
| 799 | xhci->dbreg[ep->dev->address] = ep_id; |
| 800 | |
| 801 | return intrq; |
| 802 | |
| 803 | _free_return: |
| 804 | cur = tr->cur; |
| 805 | for (--i; i >= 0; --i) { |
| 806 | free(phys_to_virt(cur->ptr_low)); |
| 807 | cur = xhci_next_trb(cur, NULL); |
| 808 | } |
| 809 | free(intrq); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 810 | return NULL; |
| 811 | } |
| 812 | |
| 813 | /* remove queue from device schedule, dropping all data that came in */ |
| 814 | static void |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 815 | xhci_destroy_intr_queue(endpoint_t *const ep, void *const q) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 816 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 817 | xhci_t *const xhci = XHCI_INST(ep->dev->controller); |
| 818 | const int ep_id = xhci_ep_id(ep); |
| 819 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address); |
| 820 | transfer_ring_t *const tr = di->transfer_rings[ep_id]; |
| 821 | |
| 822 | intrq_t *const intrq = (intrq_t *)q; |
| 823 | |
| 824 | /* Make sure the endpoint is stopped */ |
| 825 | if (EC_GET(STATE, di->devctx.eps[ep_id]) == 1) { |
| 826 | const int cc = xhci_cmd_stop_endpoint( |
| 827 | xhci, ep->dev->address, ep_id); |
| 828 | if (cc != CC_SUCCESS) |
| 829 | xhci_debug("Warning: Failed to stop endpoint\n"); |
| 830 | } |
| 831 | |
| 832 | /* Process all remaining transfer events */ |
| 833 | xhci_handle_events(xhci); |
| 834 | |
| 835 | /* Free all pending transfers and the interrupt queue structure */ |
| 836 | int i; |
| 837 | for (i = 0; i < intrq->count; ++i) { |
| 838 | free(phys_to_virt(intrq->next->ptr_low)); |
| 839 | intrq->next = xhci_next_trb(intrq->next, NULL); |
| 840 | } |
| 841 | di->interrupt_queues[ep_id] = NULL; |
| 842 | free((void *)intrq); |
| 843 | |
| 844 | /* Reset the controller's dequeue pointer and reinitialize the ring */ |
| 845 | xhci_cmd_set_tr_dq(xhci, ep->dev->address, ep_id, tr->ring, 1); |
| 846 | xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE); |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* read one intr-packet from queue, if available. extend the queue for new input. |
| 850 | return NULL if nothing new available. |
| 851 | Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 852 | */ |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 853 | static u8 * |
| 854 | xhci_poll_intr_queue(void *const q) |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 855 | { |
Nico Huber | 9029265 | 2013-06-13 14:37:15 +0200 | [diff] [blame] | 856 | if (!q) |
| 857 | return NULL; |
| 858 | |
| 859 | intrq_t *const intrq = (intrq_t *)q; |
| 860 | endpoint_t *const ep = intrq->ep; |
| 861 | xhci_t *const xhci = XHCI_INST(ep->dev->controller); |
| 862 | |
| 863 | /* TODO: Reset interrupt queue if it gets halted? */ |
| 864 | |
| 865 | xhci_handle_events(xhci); |
| 866 | |
| 867 | u8 *reqdata = NULL; |
| 868 | while (!reqdata && intrq->ready) { |
| 869 | const int ep_id = xhci_ep_id(ep); |
| 870 | devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address); |
| 871 | transfer_ring_t *const tr = di->transfer_rings[ep_id]; |
| 872 | |
| 873 | /* Fetch the request's buffer */ |
| 874 | reqdata = phys_to_virt(intrq->next->ptr_low); |
| 875 | |
| 876 | /* Enqueue the last (spare) TRB and ring doorbell */ |
| 877 | xhci_enqueue_trb(tr); |
| 878 | xhci->dbreg[ep->dev->address] = ep_id; |
| 879 | |
| 880 | /* Reuse the current buffer for the next spare TRB */ |
| 881 | xhci_clear_trb(tr->cur, tr->pcs); |
| 882 | tr->cur->ptr_low = virt_to_phys(reqdata); |
| 883 | tr->cur->ptr_high = 0; |
| 884 | TRB_SET(TL, tr->cur, intrq->size); |
| 885 | TRB_SET(TT, tr->cur, TRB_NORMAL); |
| 886 | TRB_SET(ISP, tr->cur, 1); |
| 887 | TRB_SET(IOC, tr->cur, 1); |
| 888 | |
| 889 | /* Check if anything was transferred */ |
| 890 | const size_t read = TRB_GET(TL, intrq->next); |
| 891 | if (!read) |
| 892 | reqdata = NULL; |
| 893 | else if (read < intrq->size) |
| 894 | /* At least zero it, poll interface is rather limited */ |
| 895 | memset(reqdata + read, 0x00, intrq->size - read); |
| 896 | |
| 897 | /* Advance the interrupt queue */ |
| 898 | if (intrq->ready == intrq->next) |
| 899 | /* This was last TRB being ready */ |
| 900 | intrq->ready = NULL; |
| 901 | intrq->next = xhci_next_trb(intrq->next, NULL); |
| 902 | } |
| 903 | |
| 904 | return reqdata; |
Patrick Georgi | 6615ef3 | 2010-08-13 09:18:58 +0000 | [diff] [blame] | 905 | } |