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