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