blob: 71272c71330b71fb83d41fbe7d984b2171f93211 [file] [log] [blame]
Patrick Georgi6615ef32010-08-13 09:18:58 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2010 Patrick Georgi
Nico Huber90292652013-06-13 14:37:15 +02005 * Copyright (C) 2013 secunet Security Networks AG
Patrick Georgi6615ef32010-08-13 09:18:58 +00006 *
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 Huber90292652013-06-13 14:37:15 +020031//#define XHCI_SPEW_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +000032
Nico Huber90292652013-06-13 14:37:15 +020033#include <inttypes.h>
Patrick Georgi6615ef32010-08-13 09:18:58 +000034#include <arch/virtual.h>
Patrick Georgi6615ef32010-08-13 09:18:58 +000035#include "xhci_private.h"
Nico Huber90292652013-06-13 14:37:15 +020036#include "xhci.h"
Patrick Georgi6615ef32010-08-13 09:18:58 +000037
38static void xhci_start (hci_t *controller);
39static void xhci_stop (hci_t *controller);
40static void xhci_reset (hci_t *controller);
Nico Huber90292652013-06-13 14:37:15 +020041static void xhci_reinit (hci_t *controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +000042static void xhci_shutdown (hci_t *controller);
43static int xhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize);
44static int xhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
45 int dalen, u8 *data);
46static void* xhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
47static void xhci_destroy_intr_queue (endpoint_t *ep, void *queue);
48static u8* xhci_poll_intr_queue (void *queue);
49
Nico Huber90292652013-06-13 14:37:15 +020050/*
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 */
54void *
55xhci_align(const size_t min_align, const size_t size)
Patrick Georgi6615ef32010-08-13 09:18:58 +000056{
Nico Huber90292652013-06-13 14:37:15 +020057 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 Werner1f864342013-09-03 17:15:31 -070065 return dma_memalign(align, size);
Nico Huber90292652013-06-13 14:37:15 +020066}
67
68void
69xhci_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
77void
78xhci_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 Reinauer1b4d3942015-06-29 15:47:34 -070091#if IS_ENABLED(CONFIG_LP_USB_PCI)
Nico Huber90292652013-06-13 14:37:15 +020092static void
93xhci_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 Jones86127c72014-12-29 22:07:04 -0700113#endif
Nico Huber90292652013-06-13 14:37:15 +0200114
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700115#if IS_ENABLED(CONFIG_LP_USB_PCI)
Nico Huberc3714422013-07-19 14:03:47 +0200116/* On Panther Point: switch all ports back to EHCI */
117static void
118xhci_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 Georgi22045392015-05-04 19:05:41 +0200128#endif
Nico Huberc3714422013-07-19 14:03:47 +0200129
Nico Huber90292652013-06-13 14:37:15 +0200130static long
131xhci_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
137static int
138xhci_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
149hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200150xhci_init (unsigned long physical_bar)
Nico Huber90292652013-06-13 14:37:15 +0200151{
152 int i;
153
154 /* First, allocate and initialize static controller structures */
155
156 hci_t *const controller = new_controller();
Nico Huber90292652013-06-13 14:37:15 +0200157 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 Huber8b8e9632014-07-07 17:20:53 +0200171 controller->pcidev = 0;
Nico Huber90292652013-06-13 14:37:15 +0200172
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700173 controller->reg_base = (uintptr_t)physical_bar;
Julius Werner7234d602014-04-08 12:54:25 -0700174 controller->instance = xzalloc(sizeof(xhci_t));
Nico Huber90292652013-06-13 14:37:15 +0200175 xhci_t *const xhci = (xhci_t *)controller->instance;
Nico Huber90292652013-06-13 14:37:15 +0200176
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 Huber5b9e6f12014-07-10 12:56:34 +0200188 xhci->capreg = phys_to_virt(physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200189 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 Huber5b9e6f12014-07-10 12:56:34 +0200192 xhci_debug("regbase: 0x%"PRIx32"\n", physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200193 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 Werner1f864342013-09-03 17:15:31 -0700205 xhci_debug("context size: %dB\n", CTXSIZE(xhci));
Nico Huber90292652013-06-13 14:37:15 +0200206 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 Werner1f864342013-09-03 17:15:31 -0700216 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 Huber90292652013-06-13 14:37:15 +0200220 xhci_debug("Out of memory\n");
221 goto _free_xhci;
222 }
Julius Werner1f864342013-09-03 17:15:31 -0700223 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 Huber90292652013-06-13 14:37:15 +0200225
226 /*
227 * Let dcbaa[0] point to another array of pointers, sp_ptrs.
228 * The pointers therein point to scratchpad buffers (pages).
229 */
Julius Werner57ddd9a2015-08-07 13:14:20 -0700230 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs_Hi << 5 |
231 xhci->capreg->Max_Scratchpad_Bufs_Lo;
Nico Huber90292652013-06-13 14:37:15 +0200232 xhci_debug("max scratchpad bufs: 0x%zx\n", max_sp_bufs);
233 if (max_sp_bufs) {
234 const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64);
235 xhci->sp_ptrs = xhci_align(64, sp_ptrs_size);
236 if (!xhci->sp_ptrs) {
237 xhci_debug("Out of memory\n");
238 goto _free_xhci_structs;
239 }
240 memset(xhci->sp_ptrs, 0x00, sp_ptrs_size);
241 for (i = 0; i < max_sp_bufs; ++i) {
242 /* Could use mmap() here if we had it.
243 Maybe there is another way. */
244 void *const page = memalign(pagesize, pagesize);
245 if (!page) {
246 xhci_debug("Out of memory\n");
247 goto _free_xhci_structs;
248 }
249 xhci->sp_ptrs[i] = virt_to_phys(page);
250 }
251 xhci->dcbaa[0] = virt_to_phys(xhci->sp_ptrs);
252 }
253
Julius Werner1f864342013-09-03 17:15:31 -0700254 if (dma_initialized()) {
255 xhci->dma_buffer = dma_memalign(64 * 1024, DMA_SIZE);
256 if (!xhci->dma_buffer) {
257 xhci_debug("Not enough memory for DMA bounce buffer\n");
258 goto _free_xhci_structs;
259 }
260 }
261
Nico Huber90292652013-06-13 14:37:15 +0200262 /* Now start working on the hardware */
Nico Huber90292652013-06-13 14:37:15 +0200263 if (xhci_wait_ready(xhci))
Julius Werner1f864342013-09-03 17:15:31 -0700264 goto _free_xhci_structs;
Nico Huber90292652013-06-13 14:37:15 +0200265
266 /* TODO: Check if BIOS claims ownership (and hand over) */
267
268 xhci_reset(controller);
269 xhci_reinit(controller);
270
Nico Huber90292652013-06-13 14:37:15 +0200271 xhci->roothub->controller = controller;
272 xhci->roothub->init = xhci_rh_init;
273 xhci->roothub->init(xhci->roothub);
274
275 return controller;
276
277_free_xhci_structs:
Julius Werner7234d602014-04-08 12:54:25 -0700278 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200279 if (xhci->sp_ptrs) {
280 for (i = 0; i < max_sp_bufs; ++i) {
281 if (xhci->sp_ptrs[i])
282 free(phys_to_virt(xhci->sp_ptrs[i]));
283 }
284 }
285 free(xhci->sp_ptrs);
286 free(xhci->dcbaa);
287_free_xhci:
288 free((void *)xhci->ev_ring_table);
289 free((void *)xhci->er.ring);
290 free((void *)xhci->cr.ring);
291 free(xhci->roothub);
Julius Werner1f864342013-09-03 17:15:31 -0700292 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200293 free(xhci);
Marc Jones86127c72014-12-29 22:07:04 -0700294/* _free_controller: */
Nico Huber90292652013-06-13 14:37:15 +0200295 detach_controller(controller);
296 free(controller);
297 return NULL;
298}
299
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700300#if IS_ENABLED(CONFIG_LP_USB_PCI)
Stefan Reinauer8992e532013-05-02 16:16:41 -0700301hci_t *
302xhci_pci_init (pcidev_t addr)
303{
304 u32 reg_addr;
Patrick Georgifdb348a2013-12-21 11:41:22 +0100305 hci_t *controller;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700306
Nico Huber6a058902014-07-04 18:17:39 +0200307 reg_addr = pci_read_config32 (addr, 0x10) & ~0xf;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700308 if (pci_read_config32 (addr, 0x14) > 0) {
309 fatal("We don't do 64bit addressing.\n");
310 }
311
Nico Huber6e230662014-07-07 16:33:59 +0200312 controller = xhci_init((unsigned long)reg_addr);
Nico Huberf4316f82014-07-07 17:11:53 +0200313 if (controller) {
314 controller->pcidev = addr;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700315
Nico Huberf4316f82014-07-07 17:11:53 +0200316 xhci_switch_ppt_ports(addr);
317 }
Stefan Reinauer8992e532013-05-02 16:16:41 -0700318
319 return controller;
320}
321#endif
322
Nico Huber90292652013-06-13 14:37:15 +0200323static void
324xhci_reset(hci_t *const controller)
325{
326 xhci_t *const xhci = XHCI_INST(controller);
327
328 xhci_stop(controller);
329
330 xhci->opreg->usbcmd |= USBCMD_HCRST;
331 xhci_debug("Resetting controller... ");
332 if (!xhci_handshake(&xhci->opreg->usbcmd, USBCMD_HCRST, 0, 1000000L))
333 usb_debug("timeout!\n");
334 else
335 usb_debug("ok.\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000336}
337
Nico Huber6e711c62012-11-12 16:20:32 +0100338static void
339xhci_reinit (hci_t *controller)
340{
Nico Huber90292652013-06-13 14:37:15 +0200341 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber6e711c62012-11-12 16:20:32 +0100342
Nico Huber90292652013-06-13 14:37:15 +0200343 if (xhci_wait_ready(xhci))
344 return;
345
346 /* Enable all available slots */
Julius Werner1f864342013-09-03 17:15:31 -0700347 xhci->opreg->config = xhci->max_slots_en;
Nico Huber90292652013-06-13 14:37:15 +0200348
349 /* Set DCBAA */
350 xhci->opreg->dcbaap_lo = virt_to_phys(xhci->dcbaa);
351 xhci->opreg->dcbaap_hi = 0;
352
353 /* Initialize command ring */
354 xhci_init_cycle_ring(&xhci->cr, COMMAND_RING_SIZE);
355 xhci_debug("command ring @%p (0x%08x)\n",
356 xhci->cr.ring, virt_to_phys(xhci->cr.ring));
357 xhci->opreg->crcr_lo = virt_to_phys(xhci->cr.ring) | CRCR_RCS;
358 xhci->opreg->crcr_hi = 0;
359
360 /* Make sure interrupts are disabled */
361 xhci->opreg->usbcmd &= ~USBCMD_INTE;
362
363 /* Initialize event ring */
364 xhci_reset_event_ring(&xhci->er);
365 xhci_debug("event ring @%p (0x%08x)\n",
366 xhci->er.ring, virt_to_phys(xhci->er.ring));
367 xhci_debug("ERST Max: 0x%lx -> 0x%lx entries\n",
368 xhci->capreg->ERST_Max, 1 << xhci->capreg->ERST_Max);
369 memset((void*)xhci->ev_ring_table, 0x00, sizeof(erst_entry_t));
370 xhci->ev_ring_table[0].seg_base_lo = virt_to_phys(xhci->er.ring);
371 xhci->ev_ring_table[0].seg_base_hi = 0;
372 xhci->ev_ring_table[0].seg_size = EVENT_RING_SIZE;
373
Yidi Lind42ee152015-05-07 15:36:04 +0800374 /* pass event ring table to hardware */
375 wmb();
Nico Huber90292652013-06-13 14:37:15 +0200376 /* Initialize primary interrupter */
377 xhci->hcrreg->intrrs[0].erstsz = 1;
378 xhci_update_event_dq(xhci);
379 /* erstba has to be written at last */
380 xhci->hcrreg->intrrs[0].erstba_lo = virt_to_phys(xhci->ev_ring_table);
381 xhci->hcrreg->intrrs[0].erstba_hi = 0;
382
383 xhci_start(controller);
384
385#ifdef USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +0000386 int i;
Nico Huber90292652013-06-13 14:37:15 +0200387 for (i = 0; i < 32; ++i) {
388 xhci_debug("NOOP run #%d\n", i);
389 trb_t *const cmd = xhci_next_command_trb(xhci);
390 TRB_SET(TT, cmd, TRB_CMD_NOOP);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000391
Nico Huber90292652013-06-13 14:37:15 +0200392 xhci_post_command(xhci);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000393
Nico Huber90292652013-06-13 14:37:15 +0200394 /* Wait for result in event ring */
395 xhci_wait_for_command_done(xhci, cmd, 1);
396 xhci_debug("Command ring is %srunning\n",
397 (xhci->opreg->crcr_lo & CRCR_CRR) ? "" : "not ");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000398 }
Nico Huber90292652013-06-13 14:37:15 +0200399#endif
Patrick Georgi6615ef32010-08-13 09:18:58 +0000400}
401
402static void
Nico Huber90292652013-06-13 14:37:15 +0200403xhci_shutdown(hci_t *const controller)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000404{
Nico Huber90292652013-06-13 14:37:15 +0200405 int i;
406
Patrick Georgi6615ef32010-08-13 09:18:58 +0000407 if (controller == 0)
408 return;
Nico Huber90292652013-06-13 14:37:15 +0200409
410 detach_controller(controller);
411
Julius Werner7234d602014-04-08 12:54:25 -0700412 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber90292652013-06-13 14:37:15 +0200413 xhci_stop(controller);
414
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700415#if IS_ENABLED(CONFIG_LP_USB_PCI)
Patrick Georgifdb348a2013-12-21 11:41:22 +0100416 if (controller->pcidev)
417 xhci_switchback_ppt_ports(controller->pcidev);
Patrick Georgi22045392015-05-04 19:05:41 +0200418#endif
Nico Huberc3714422013-07-19 14:03:47 +0200419
Nico Huber90292652013-06-13 14:37:15 +0200420 if (xhci->sp_ptrs) {
Julius Werner57ddd9a2015-08-07 13:14:20 -0700421 size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs_Hi << 5 |
422 xhci->capreg->Max_Scratchpad_Bufs_Lo;
Nico Huber90292652013-06-13 14:37:15 +0200423 for (i = 0; i < max_sp_bufs; ++i) {
424 if (xhci->sp_ptrs[i])
425 free(phys_to_virt(xhci->sp_ptrs[i]));
426 }
427 }
428 free(xhci->sp_ptrs);
Julius Werner7234d602014-04-08 12:54:25 -0700429 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200430 free(xhci->dcbaa);
Julius Werner1f864342013-09-03 17:15:31 -0700431 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200432 free((void *)xhci->ev_ring_table);
433 free((void *)xhci->er.ring);
434 free((void *)xhci->cr.ring);
435 free(xhci);
436 free(controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000437}
438
439static void
440xhci_start (hci_t *controller)
441{
Nico Huber90292652013-06-13 14:37:15 +0200442 xhci_t *const xhci = XHCI_INST(controller);
443
444 xhci->opreg->usbcmd |= USBCMD_RS;
445 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_HCH, 0, 1000000L))
446 xhci_debug("Controller didn't start within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000447}
448
449static void
450xhci_stop (hci_t *controller)
451{
Nico Huber90292652013-06-13 14:37:15 +0200452 xhci_t *const xhci = XHCI_INST(controller);
453
454 xhci->opreg->usbcmd &= ~USBCMD_RS;
455 if (!xhci_handshake(&xhci->opreg->usbsts,
456 USBSTS_HCH, USBSTS_HCH, 1000000L))
457 xhci_debug("Controller didn't halt within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000458}
459
460static int
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700461xhci_reset_endpoint(usbdev_t *const dev, endpoint_t *const ep)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000462{
Nico Huber90292652013-06-13 14:37:15 +0200463 xhci_t *const xhci = XHCI_INST(dev->controller);
Nico Huber90292652013-06-13 14:37:15 +0200464 const int slot_id = dev->address;
465 const int ep_id = ep ? xhci_ep_id(ep) : 1;
Julius Werner1f864342013-09-03 17:15:31 -0700466 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200467
468 xhci_debug("Resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700469 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200470
471 /* Run Reset Endpoint Command if the EP is in Halted state */
Julius Werner1f864342013-09-03 17:15:31 -0700472 if (EC_GET(STATE, epctx) == 2) {
Nico Huber90292652013-06-13 14:37:15 +0200473 const int cc = xhci_cmd_reset_endpoint(xhci, slot_id, ep_id);
474 if (cc != CC_SUCCESS) {
475 xhci_debug("Reset Endpoint Command failed: %d\n", cc);
476 return 1;
477 }
478 }
479
480 /* Clear TT buffer for bulk and control endpoints behind a TT */
481 const int hub = dev->hub;
482 if (hub && dev->speed < HIGH_SPEED &&
483 dev->controller->devices[hub]->speed == HIGH_SPEED)
484 /* TODO */;
485
Nico Huber90292652013-06-13 14:37:15 +0200486 /* Reset transfer ring if the endpoint is in the right state */
Julius Werner1f864342013-09-03 17:15:31 -0700487 const unsigned ep_state = EC_GET(STATE, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200488 if (ep_state == 3 || ep_state == 4) {
Julius Werner1f864342013-09-03 17:15:31 -0700489 transfer_ring_t *const tr =
490 xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200491 const int cc = xhci_cmd_set_tr_dq(xhci, slot_id, ep_id,
492 tr->ring, 1);
493 if (cc != CC_SUCCESS) {
494 xhci_debug("Set TR Dequeue Command failed: %d\n", cc);
495 return 1;
496 }
497 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
498 }
499
500 xhci_debug("Finished resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700501 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200502
503 return 0;
504}
505
506static void
507xhci_enqueue_trb(transfer_ring_t *const tr)
508{
509 const int chain = TRB_GET(CH, tr->cur);
510 TRB_SET(C, tr->cur, tr->pcs);
511 ++tr->cur;
512
513 while (TRB_GET(TT, tr->cur) == TRB_LINK) {
514 xhci_spew("Handling LINK pointer\n");
515 const int tc = TRB_GET(TC, tr->cur);
516 TRB_SET(CH, tr->cur, chain);
Yidi Lind42ee152015-05-07 15:36:04 +0800517 wmb();
Nico Huber90292652013-06-13 14:37:15 +0200518 TRB_SET(C, tr->cur, tr->pcs);
519 tr->cur = phys_to_virt(tr->cur->ptr_low);
520 if (tc)
521 tr->pcs ^= 1;
522 }
523}
524
525static void
526xhci_enqueue_td(transfer_ring_t *const tr, const int ep, const size_t mps,
527 const int dalen, void *const data, const int dir)
528{
529 trb_t *trb = NULL; /* cur TRB */
530 u8 *cur_start = data; /* cur data pointer */
531 size_t length = dalen; /* remaining bytes */
532 size_t packets = (length + mps - 1) / mps; /* remaining packets */
533 size_t residue = 0; /* residue from last TRB */
534 size_t trb_count = 0; /* TRBs added so far */
535
536 while (length || !trb_count /* enqueue at least one */) {
537 const size_t cur_end = ((size_t)cur_start + 0x10000) & ~0xffff;
538 size_t cur_length = cur_end - (size_t)cur_start;
539 if (length < cur_length) {
540 cur_length = length;
541 packets = 0;
542 length = 0;
Chunfeng Yunb4a24992015-08-04 14:33:58 +0800543 } else if (!IS_ENABLED(CONFIG_LP_USB_XHCI_MTK_QUIRK)) {
Nico Huber90292652013-06-13 14:37:15 +0200544 packets -= (residue + cur_length) / mps;
545 residue = (residue + cur_length) % mps;
546 length -= cur_length;
547 }
548
549 trb = tr->cur;
550 xhci_clear_trb(trb, tr->pcs);
551 trb->ptr_low = virt_to_phys(cur_start);
552 TRB_SET(TL, trb, cur_length);
Rajmohan Manid6fb32b2014-05-30 13:06:01 -0700553 TRB_SET(TDS, trb, MIN(TRB_MAX_TD_SIZE, packets));
Julius Werner83da5012013-09-27 12:45:11 -0700554 TRB_SET(CH, trb, 1);
Nico Huber90292652013-06-13 14:37:15 +0200555
Chunfeng Yunb4a24992015-08-04 14:33:58 +0800556 if (length && IS_ENABLED(CONFIG_LP_USB_XHCI_MTK_QUIRK)) {
Yidi Lind42ee152015-05-07 15:36:04 +0800557 /*
558 * For MTK's xHCI controller, TDS defines a number of
559 * packets that remain to be transferred for a TD after
560 * processing all Max packets in all previous TRBs, that
561 * means don't include the current TRB's.
562 */
563 packets -= (residue + cur_length) / mps;
564 residue = (residue + cur_length) % mps;
565 length -= cur_length;
566 }
567
Nico Huber90292652013-06-13 14:37:15 +0200568 /* Check for first, data stage TRB */
569 if (!trb_count && ep == 1) {
570 TRB_SET(DIR, trb, dir);
571 TRB_SET(TT, trb, TRB_DATA_STAGE);
572 } else {
573 TRB_SET(TT, trb, TRB_NORMAL);
574 }
Sourabh Banerjeee73335c2014-09-24 16:14:45 +0530575 /*
576 * This is a workaround for Synopsys DWC3. If the ENT flag is
577 * not set for the Normal and Data Stage TRBs. We get Event TRB
578 * with length 0x20d from the controller when we enqueue a TRB
579 * for the IN endpoint with length 0x200.
580 */
581 if (!length)
582 TRB_SET(ENT, trb, 1);
Nico Huber90292652013-06-13 14:37:15 +0200583
Nico Huber90292652013-06-13 14:37:15 +0200584 xhci_enqueue_trb(tr);
585
586 cur_start += cur_length;
587 ++trb_count;
588 }
Julius Werner83da5012013-09-27 12:45:11 -0700589
590 trb = tr->cur;
591 xhci_clear_trb(trb, tr->pcs);
592 trb->ptr_low = virt_to_phys(trb); /* for easier debugging only */
593 TRB_SET(TT, trb, TRB_EVENT_DATA);
594 TRB_SET(IOC, trb, 1);
595
596 xhci_enqueue_trb(tr);
Nico Huber90292652013-06-13 14:37:15 +0200597}
598
599static int
600xhci_control(usbdev_t *const dev, const direction_t dir,
601 const int drlen, void *const devreq,
Julius Werner1f864342013-09-03 17:15:31 -0700602 const int dalen, unsigned char *const src)
Nico Huber90292652013-06-13 14:37:15 +0200603{
Julius Werner1f864342013-09-03 17:15:31 -0700604 unsigned char *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200605 xhci_t *const xhci = XHCI_INST(dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700606 epctx_t *const epctx = xhci->dev[dev->address].ctx.ep0;
607 transfer_ring_t *const tr = xhci->dev[dev->address].transfer_rings[1];
Nico Huber90292652013-06-13 14:37:15 +0200608
609 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700610 if ((off + dalen) > ((TRANSFER_RING_SIZE - 4) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200611 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800612 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200613 }
614
Julius Werner49ba2832013-09-26 15:13:44 -0700615 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700616 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700617 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700618 if (xhci_reset_endpoint(dev, NULL))
Julius Wernere9738db2013-02-21 13:41:40 -0800619 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200620 }
621
Julius Werner1f864342013-09-03 17:15:31 -0700622 if (dalen && !dma_coherent(src)) {
623 data = xhci->dma_buffer;
624 if (dalen > DMA_SIZE) {
625 xhci_debug("Control transfer too large: %d\n", dalen);
626 return -1;
627 }
628 if (dir == OUT)
629 memcpy(data, src, dalen);
630 }
631
Nico Huber90292652013-06-13 14:37:15 +0200632 /* Fill and enqueue setup TRB */
633 trb_t *const setup = tr->cur;
634 xhci_clear_trb(setup, tr->pcs);
635 setup->ptr_low = ((u32 *)devreq)[0];
636 setup->ptr_high = ((u32 *)devreq)[1];
637 TRB_SET(TL, setup, 8);
638 TRB_SET(TRT, setup, (dalen)
639 ? ((dir == OUT) ? TRB_TRT_OUT_DATA : TRB_TRT_IN_DATA)
640 : TRB_TRT_NO_DATA);
641 TRB_SET(TT, setup, TRB_SETUP_STAGE);
642 TRB_SET(IDT, setup, 1);
643 TRB_SET(IOC, setup, 1);
644 xhci_enqueue_trb(tr);
645
646 /* Fill and enqueue data TRBs (if any) */
647 if (dalen) {
Julius Werner1f864342013-09-03 17:15:31 -0700648 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200649 const unsigned dt_dir = (dir == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
650 xhci_enqueue_td(tr, 1, mps, dalen, data, dt_dir);
651 }
652
653 /* Fill status TRB */
654 trb_t *const status = tr->cur;
655 xhci_clear_trb(status, tr->pcs);
656 TRB_SET(DIR, status, (dir == OUT) ? TRB_DIR_IN : TRB_DIR_OUT);
657 TRB_SET(TT, status, TRB_STATUS_STAGE);
658 TRB_SET(IOC, status, 1);
659 xhci_enqueue_trb(tr);
660
661 /* Ring doorbell for EP0 */
662 xhci->dbreg[dev->address] = 1;
663
664 /* Wait for transfer events */
Julius Wernere9738db2013-02-21 13:41:40 -0800665 int i, transferred = 0;
Nico Huber90292652013-06-13 14:37:15 +0200666 const int n_stages = 2 + !!dalen;
667 for (i = 0; i < n_stages; ++i) {
668 const int ret = xhci_wait_for_transfer(xhci, dev->address, 1);
Julius Wernere9738db2013-02-21 13:41:40 -0800669 transferred += ret;
670 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200671 if (ret == TIMEOUT) {
672 xhci_debug("Stopping ID %d EP 1\n",
673 dev->address);
674 xhci_cmd_stop_endpoint(xhci, dev->address, 1);
675 }
676 xhci_debug("Stage %d/%d failed: %d\n"
677 " trb ring: @%p\n"
678 " setup trb: @%p\n"
679 " status trb: @%p\n"
680 " ep state: %d -> %d\n"
681 " usbsts: 0x%08"PRIx32"\n",
682 i, n_stages, ret,
683 tr->ring, setup, status,
Julius Werner1f864342013-09-03 17:15:31 -0700684 ep_state, EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200685 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800686 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200687 }
688 }
689
Julius Werner1f864342013-09-03 17:15:31 -0700690 if (dir == IN && data != src)
691 memcpy(src, data, transferred);
Julius Wernere9738db2013-02-21 13:41:40 -0800692 return transferred;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000693}
694
695/* finalize == 1: if data is of packet aligned size, add a zero length packet */
696static int
Julius Werner1f864342013-09-03 17:15:31 -0700697xhci_bulk(endpoint_t *const ep, const int size, u8 *const src,
Nico Huber90292652013-06-13 14:37:15 +0200698 const int finalize)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000699{
Nico Huber90292652013-06-13 14:37:15 +0200700 /* finalize: Hopefully the xHCI controller always does this.
701 We have no control over the packets. */
702
Julius Werner1f864342013-09-03 17:15:31 -0700703 u8 *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200704 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700705 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200706 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700707 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
708 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200709
710 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700711 if ((off + size) > ((TRANSFER_RING_SIZE - 2) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200712 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800713 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200714 }
715
Julius Werner1f864342013-09-03 17:15:31 -0700716 if (!dma_coherent(src)) {
717 data = xhci->dma_buffer;
718 if (size > DMA_SIZE) {
719 xhci_debug("Bulk transfer too large: %d\n", size);
720 return -1;
721 }
722 if (ep->direction == OUT)
723 memcpy(data, src, size);
724 }
725
Julius Werner49ba2832013-09-26 15:13:44 -0700726 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700727 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700728 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700729 if (xhci_reset_endpoint(ep->dev, ep))
Julius Wernere9738db2013-02-21 13:41:40 -0800730 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200731 }
732
733 /* Enqueue transfer and ring doorbell */
Julius Werner1f864342013-09-03 17:15:31 -0700734 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200735 const unsigned dir = (ep->direction == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
736 xhci_enqueue_td(tr, ep_id, mps, size, data, dir);
737 xhci->dbreg[ep->dev->address] = ep_id;
738
739 /* Wait for transfer event */
740 const int ret = xhci_wait_for_transfer(xhci, ep->dev->address, ep_id);
Julius Wernere9738db2013-02-21 13:41:40 -0800741 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200742 if (ret == TIMEOUT) {
743 xhci_debug("Stopping ID %d EP %d\n",
744 ep->dev->address, ep_id);
745 xhci_cmd_stop_endpoint(xhci, ep->dev->address, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200746 }
747 xhci_debug("Bulk transfer failed: %d\n"
748 " ep state: %d -> %d\n"
749 " usbsts: 0x%08"PRIx32"\n",
750 ret, ep_state,
Julius Werner1f864342013-09-03 17:15:31 -0700751 EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200752 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800753 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200754 }
755
Julius Werner1f864342013-09-03 17:15:31 -0700756 if (ep->direction == IN && data != src)
757 memcpy(src, data, ret);
Julius Wernere9738db2013-02-21 13:41:40 -0800758 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200759}
760
761static trb_t *
762xhci_next_trb(trb_t *cur, int *const pcs)
763{
764 ++cur;
765 while (TRB_GET(TT, cur) == TRB_LINK) {
766 if (pcs && TRB_GET(TC, cur))
767 *pcs ^= 1;
768 cur = phys_to_virt(cur->ptr_low);
769 }
770 return cur;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000771}
772
773/* create and hook-up an intr queue into device schedule */
Nico Huber90292652013-06-13 14:37:15 +0200774static void *
775xhci_create_intr_queue(endpoint_t *const ep,
776 const int reqsize, const int reqcount,
777 const int reqtiming)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000778{
Nico Huber90292652013-06-13 14:37:15 +0200779 /* reqtiming: We ignore it and use the interval from the
780 endpoint descriptor configured earlier. */
781
782 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700783 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200784 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700785 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200786
787 if (reqcount > (TRANSFER_RING_SIZE - 2)) {
788 xhci_debug("reqcount is too high, at most %d supported\n",
789 TRANSFER_RING_SIZE - 2);
790 return NULL;
791 }
792 if (reqsize > 0x10000) {
793 xhci_debug("reqsize is too large, at most 64KiB supported\n");
794 return NULL;
795 }
Julius Werner1f864342013-09-03 17:15:31 -0700796 if (xhci->dev[slot_id].interrupt_queues[ep_id]) {
Nico Huber90292652013-06-13 14:37:15 +0200797 xhci_debug("Only one interrupt queue per endpoint supported\n");
798 return NULL;
799 }
800
801 /* Allocate intrq structure and reqdata chunks */
802
803 intrq_t *const intrq = malloc(sizeof(*intrq));
804 if (!intrq) {
805 xhci_debug("Out of memory\n");
806 return NULL;
807 }
808
809 int i;
810 int pcs = tr->pcs;
811 trb_t *cur = tr->cur;
812 for (i = 0; i < reqcount; ++i) {
813 if (TRB_GET(C, cur) == pcs) {
814 xhci_debug("Not enough empty TRBs\n");
815 goto _free_return;
816 }
817 void *const reqdata = xhci_align(1, reqsize);
818 if (!reqdata) {
819 xhci_debug("Out of memory\n");
820 goto _free_return;
821 }
822 xhci_clear_trb(cur, pcs);
823 cur->ptr_low = virt_to_phys(reqdata);
824 cur->ptr_high = 0;
825 TRB_SET(TL, cur, reqsize);
826 TRB_SET(TT, cur, TRB_NORMAL);
827 TRB_SET(ISP, cur, 1);
828 TRB_SET(IOC, cur, 1);
829
830 cur = xhci_next_trb(cur, &pcs);
831 }
832
833 intrq->size = reqsize;
834 intrq->count = reqcount;
835 intrq->next = tr->cur;
836 intrq->ready = NULL;
837 intrq->ep = ep;
Julius Werner1f864342013-09-03 17:15:31 -0700838 xhci->dev[slot_id].interrupt_queues[ep_id] = intrq;
Nico Huber90292652013-06-13 14:37:15 +0200839
840 /* Now enqueue all the prepared TRBs but the last
841 and ring the doorbell. */
842 for (i = 0; i < (reqcount - 1); ++i)
843 xhci_enqueue_trb(tr);
Julius Werner1f864342013-09-03 17:15:31 -0700844 xhci->dbreg[slot_id] = ep_id;
Nico Huber90292652013-06-13 14:37:15 +0200845
846 return intrq;
847
848_free_return:
849 cur = tr->cur;
850 for (--i; i >= 0; --i) {
851 free(phys_to_virt(cur->ptr_low));
852 cur = xhci_next_trb(cur, NULL);
853 }
854 free(intrq);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000855 return NULL;
856}
857
858/* remove queue from device schedule, dropping all data that came in */
859static void
Nico Huber90292652013-06-13 14:37:15 +0200860xhci_destroy_intr_queue(endpoint_t *const ep, void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000861{
Nico Huber90292652013-06-13 14:37:15 +0200862 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700863 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200864 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700865 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200866
867 intrq_t *const intrq = (intrq_t *)q;
868
869 /* Make sure the endpoint is stopped */
Julius Werner1f864342013-09-03 17:15:31 -0700870 if (EC_GET(STATE, xhci->dev[slot_id].ctx.ep[ep_id]) == 1) {
871 const int cc = xhci_cmd_stop_endpoint(xhci, slot_id, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200872 if (cc != CC_SUCCESS)
873 xhci_debug("Warning: Failed to stop endpoint\n");
874 }
875
876 /* Process all remaining transfer events */
877 xhci_handle_events(xhci);
878
879 /* Free all pending transfers and the interrupt queue structure */
880 int i;
881 for (i = 0; i < intrq->count; ++i) {
882 free(phys_to_virt(intrq->next->ptr_low));
883 intrq->next = xhci_next_trb(intrq->next, NULL);
884 }
Julius Werner1f864342013-09-03 17:15:31 -0700885 xhci->dev[slot_id].interrupt_queues[ep_id] = NULL;
Nico Huber90292652013-06-13 14:37:15 +0200886 free((void *)intrq);
887
888 /* Reset the controller's dequeue pointer and reinitialize the ring */
Julius Werner1f864342013-09-03 17:15:31 -0700889 xhci_cmd_set_tr_dq(xhci, slot_id, ep_id, tr->ring, 1);
Nico Huber90292652013-06-13 14:37:15 +0200890 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000891}
892
893/* read one intr-packet from queue, if available. extend the queue for new input.
894 return NULL if nothing new available.
895 Recommended use: while (data=poll_intr_queue(q)) process(data);
896 */
Nico Huber90292652013-06-13 14:37:15 +0200897static u8 *
898xhci_poll_intr_queue(void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000899{
Nico Huber90292652013-06-13 14:37:15 +0200900 if (!q)
901 return NULL;
902
903 intrq_t *const intrq = (intrq_t *)q;
904 endpoint_t *const ep = intrq->ep;
905 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
906
907 /* TODO: Reset interrupt queue if it gets halted? */
908
909 xhci_handle_events(xhci);
910
911 u8 *reqdata = NULL;
912 while (!reqdata && intrq->ready) {
913 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700914 transfer_ring_t *const tr =
915 xhci->dev[ep->dev->address].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200916
917 /* Fetch the request's buffer */
918 reqdata = phys_to_virt(intrq->next->ptr_low);
919
920 /* Enqueue the last (spare) TRB and ring doorbell */
921 xhci_enqueue_trb(tr);
922 xhci->dbreg[ep->dev->address] = ep_id;
923
924 /* Reuse the current buffer for the next spare TRB */
925 xhci_clear_trb(tr->cur, tr->pcs);
926 tr->cur->ptr_low = virt_to_phys(reqdata);
927 tr->cur->ptr_high = 0;
928 TRB_SET(TL, tr->cur, intrq->size);
929 TRB_SET(TT, tr->cur, TRB_NORMAL);
930 TRB_SET(ISP, tr->cur, 1);
931 TRB_SET(IOC, tr->cur, 1);
932
933 /* Check if anything was transferred */
934 const size_t read = TRB_GET(TL, intrq->next);
935 if (!read)
936 reqdata = NULL;
937 else if (read < intrq->size)
938 /* At least zero it, poll interface is rather limited */
939 memset(reqdata + read, 0x00, intrq->size - read);
940
941 /* Advance the interrupt queue */
942 if (intrq->ready == intrq->next)
943 /* This was last TRB being ready */
944 intrq->ready = NULL;
945 intrq->next = xhci_next_trb(intrq->next, NULL);
946 }
947
948 return reqdata;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000949}