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