blob: 184a370936d33ac16a59b4fad499fd02b7555a72 [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
Nico Huberc3714422013-07-19 14:03:47 +0200113/* On Panther Point: switch all ports back to EHCI */
114static void
115xhci_switchback_ppt_ports(pcidev_t addr)
116{
117 if (pci_read_config32(addr, 0x00) == 0x1e318086) {
118 u32 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
119 xhci_debug("Switching ports back: 0x%"PRIx32"\n", reg32);
120 pci_write_config32(addr, 0xd0, 0x00000000);
121 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
122 xhci_debug("Still switched to xHCI: 0x%"PRIx32"\n", reg32);
123 }
124}
125
Nico Huber90292652013-06-13 14:37:15 +0200126static long
127xhci_handshake(volatile u32 *const reg, u32 mask, u32 wait_for, long timeout_us)
128{
129 while ((*reg & mask) != wait_for && timeout_us--) udelay(1);
130 return timeout_us;
131}
132
133static int
134xhci_wait_ready(xhci_t *const xhci)
135{
136 xhci_debug("Waiting for controller to be ready... ");
137 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_CNR, 0, 100000L)) {
138 usb_debug("timeout!\n");
139 return -1;
140 }
141 usb_debug("ok.\n");
142 return 0;
143}
144
145hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200146xhci_init (unsigned long physical_bar)
Nico Huber90292652013-06-13 14:37:15 +0200147{
148 int i;
149
150 /* First, allocate and initialize static controller structures */
151
152 hci_t *const controller = new_controller();
153 if (!controller) {
154 xhci_debug("Could not create USB controller instance\n");
155 return controller;
156 }
157
158 controller->type = XHCI;
159 controller->start = xhci_start;
160 controller->stop = xhci_stop;
161 controller->reset = xhci_reset;
162 controller->init = xhci_reinit;
163 controller->shutdown = xhci_shutdown;
164 controller->bulk = xhci_bulk;
165 controller->control = xhci_control;
166 controller->set_address = xhci_set_address;
167 controller->finish_device_config= xhci_finish_device_config;
168 controller->destroy_device = xhci_destroy_dev;
169 controller->create_intr_queue = xhci_create_intr_queue;
170 controller->destroy_intr_queue = xhci_destroy_intr_queue;
171 controller->poll_intr_queue = xhci_poll_intr_queue;
Nico Huber8b8e9632014-07-07 17:20:53 +0200172 controller->pcidev = 0;
Nico Huber90292652013-06-13 14:37:15 +0200173 for (i = 0; i < 128; ++i) {
174 controller->devices[i] = NULL;
175 }
176
177 controller->instance = malloc(sizeof(xhci_t));
178 if (!controller->instance) {
179 xhci_debug("Out of memory creating xHCI controller instance\n");
180 goto _free_controller;
181 }
182 xhci_t *const xhci = (xhci_t *)controller->instance;
183 memset(xhci, 0x00, sizeof(*xhci));
184
185 init_device_entry(controller, 0);
186 xhci->roothub = controller->devices[0];
187 xhci->cr.ring = xhci_align(64, COMMAND_RING_SIZE * sizeof(trb_t));
188 xhci->er.ring = xhci_align(64, EVENT_RING_SIZE * sizeof(trb_t));
189 xhci->ev_ring_table = xhci_align(64, sizeof(erst_entry_t));
190 if (!xhci->roothub || !xhci->cr.ring ||
191 !xhci->er.ring || !xhci->ev_ring_table) {
192 xhci_debug("Out of memory\n");
193 goto _free_xhci;
194 }
195
Nico Huber5b9e6f12014-07-10 12:56:34 +0200196 xhci->capreg = phys_to_virt(physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200197 xhci->opreg = ((void *)xhci->capreg) + xhci->capreg->caplength;
198 xhci->hcrreg = ((void *)xhci->capreg) + xhci->capreg->rtsoff;
199 xhci->dbreg = ((void *)xhci->capreg) + xhci->capreg->dboff;
Nico Huber5b9e6f12014-07-10 12:56:34 +0200200 xhci_debug("regbase: 0x%"PRIx32"\n", physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200201 xhci_debug("caplen: 0x%"PRIx32"\n", xhci->capreg->caplength);
202 xhci_debug("rtsoff: 0x%"PRIx32"\n", xhci->capreg->rtsoff);
203 xhci_debug("dboff: 0x%"PRIx32"\n", xhci->capreg->dboff);
204
205 xhci_debug("hciversion: %"PRIx8".%"PRIx8"\n",
206 xhci->capreg->hciver_hi, xhci->capreg->hciver_lo);
207 if ((xhci->capreg->hciversion < 0x96) ||
208 (xhci->capreg->hciversion > 0x100)) {
209 xhci_debug("Unsupported xHCI version\n");
210 goto _free_xhci;
211 }
212
213 xhci_debug("context size: %dB\n", xhci->capreg->csz ? 64 : 32);
214 if (xhci->capreg->csz) {
215 xhci_debug("Only 32B contexts are supported\n");
216 goto _free_xhci;
217 }
218
219 xhci_debug("maxslots: 0x%02lx\n", xhci->capreg->MaxSlots);
220 xhci_debug("maxports: 0x%02lx\n", xhci->capreg->MaxPorts);
221 const unsigned pagesize = xhci->opreg->pagesize << 12;
222 xhci_debug("pagesize: 0x%04x\n", pagesize);
223
224 /*
225 * We haven't touched the hardware yet. So we allocate all dynamic
226 * structures at first and can still chicken out easily if we run out
227 * of memory.
228 */
229 const size_t dcbaa_size = (xhci->capreg->MaxSlots + 1) * sizeof(u64);
230 xhci->dcbaa = xhci_align(64, dcbaa_size);
231 if (!xhci->dcbaa) {
232 xhci_debug("Out of memory\n");
233 goto _free_xhci;
234 }
235 memset((void*)xhci->dcbaa, 0x00, dcbaa_size);
236
237 /*
238 * Let dcbaa[0] point to another array of pointers, sp_ptrs.
239 * The pointers therein point to scratchpad buffers (pages).
240 */
241 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
242 xhci_debug("max scratchpad bufs: 0x%zx\n", max_sp_bufs);
243 if (max_sp_bufs) {
244 const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64);
245 xhci->sp_ptrs = xhci_align(64, sp_ptrs_size);
246 if (!xhci->sp_ptrs) {
247 xhci_debug("Out of memory\n");
248 goto _free_xhci_structs;
249 }
250 memset(xhci->sp_ptrs, 0x00, sp_ptrs_size);
251 for (i = 0; i < max_sp_bufs; ++i) {
252 /* Could use mmap() here if we had it.
253 Maybe there is another way. */
254 void *const page = memalign(pagesize, pagesize);
255 if (!page) {
256 xhci_debug("Out of memory\n");
257 goto _free_xhci_structs;
258 }
259 xhci->sp_ptrs[i] = virt_to_phys(page);
260 }
261 xhci->dcbaa[0] = virt_to_phys(xhci->sp_ptrs);
262 }
263
264 /* Now start working on the hardware */
Nico Huber90292652013-06-13 14:37:15 +0200265 if (xhci_wait_ready(xhci))
266 goto _free_xhci;
267
268 /* TODO: Check if BIOS claims ownership (and hand over) */
269
270 xhci_reset(controller);
271 xhci_reinit(controller);
272
Nico Huber90292652013-06-13 14:37:15 +0200273 xhci->roothub->controller = controller;
274 xhci->roothub->init = xhci_rh_init;
275 xhci->roothub->init(xhci->roothub);
276
277 return controller;
278
279_free_xhci_structs:
280 if (xhci->sp_ptrs) {
281 for (i = 0; i < max_sp_bufs; ++i) {
282 if (xhci->sp_ptrs[i])
283 free(phys_to_virt(xhci->sp_ptrs[i]));
284 }
285 }
286 free(xhci->sp_ptrs);
287 free(xhci->dcbaa);
288_free_xhci:
289 free((void *)xhci->ev_ring_table);
290 free((void *)xhci->er.ring);
291 free((void *)xhci->cr.ring);
292 free(xhci->roothub);
293 free(xhci);
294_free_controller:
295 detach_controller(controller);
296 free(controller);
297 return NULL;
298}
299
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700300#ifdef 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 */
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700347 xhci->opreg->config = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn;
348 xhci->max_slots_en = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn;
Nico Huber90292652013-06-13 14:37:15 +0200349
350 /* Set DCBAA */
351 xhci->opreg->dcbaap_lo = virt_to_phys(xhci->dcbaa);
352 xhci->opreg->dcbaap_hi = 0;
353
354 /* Initialize command ring */
355 xhci_init_cycle_ring(&xhci->cr, COMMAND_RING_SIZE);
356 xhci_debug("command ring @%p (0x%08x)\n",
357 xhci->cr.ring, virt_to_phys(xhci->cr.ring));
358 xhci->opreg->crcr_lo = virt_to_phys(xhci->cr.ring) | CRCR_RCS;
359 xhci->opreg->crcr_hi = 0;
360
361 /* Make sure interrupts are disabled */
362 xhci->opreg->usbcmd &= ~USBCMD_INTE;
363
364 /* Initialize event ring */
365 xhci_reset_event_ring(&xhci->er);
366 xhci_debug("event ring @%p (0x%08x)\n",
367 xhci->er.ring, virt_to_phys(xhci->er.ring));
368 xhci_debug("ERST Max: 0x%lx -> 0x%lx entries\n",
369 xhci->capreg->ERST_Max, 1 << xhci->capreg->ERST_Max);
370 memset((void*)xhci->ev_ring_table, 0x00, sizeof(erst_entry_t));
371 xhci->ev_ring_table[0].seg_base_lo = virt_to_phys(xhci->er.ring);
372 xhci->ev_ring_table[0].seg_base_hi = 0;
373 xhci->ev_ring_table[0].seg_size = EVENT_RING_SIZE;
374
375 /* Initialize primary interrupter */
376 xhci->hcrreg->intrrs[0].erstsz = 1;
377 xhci_update_event_dq(xhci);
378 /* erstba has to be written at last */
379 xhci->hcrreg->intrrs[0].erstba_lo = virt_to_phys(xhci->ev_ring_table);
380 xhci->hcrreg->intrrs[0].erstba_hi = 0;
381
382 xhci_start(controller);
383
384#ifdef USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +0000385 int i;
Nico Huber90292652013-06-13 14:37:15 +0200386 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 Georgi6615ef32010-08-13 09:18:58 +0000390
Nico Huber90292652013-06-13 14:37:15 +0200391 xhci_post_command(xhci);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000392
Nico Huber90292652013-06-13 14:37:15 +0200393 /* 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 Georgi6615ef32010-08-13 09:18:58 +0000397 }
Nico Huber90292652013-06-13 14:37:15 +0200398#endif
Patrick Georgi6615ef32010-08-13 09:18:58 +0000399}
400
401static void
Nico Huber90292652013-06-13 14:37:15 +0200402xhci_shutdown(hci_t *const controller)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000403{
Nico Huber90292652013-06-13 14:37:15 +0200404 int i;
405
Patrick Georgi6615ef32010-08-13 09:18:58 +0000406 if (controller == 0)
407 return;
Nico Huber90292652013-06-13 14:37:15 +0200408 xhci_t *const xhci = XHCI_INST(controller);
409
410 detach_controller(controller);
411
412 /* Detach device hierarchy (starting at root hub) */
413 usb_detach_device(controller, 0);
414
415 xhci_stop(controller);
416
Patrick Georgifdb348a2013-12-21 11:41:22 +0100417 if (controller->pcidev)
418 xhci_switchback_ppt_ports(controller->pcidev);
Nico Huberc3714422013-07-19 14:03:47 +0200419
Nico Huber90292652013-06-13 14:37:15 +0200420 if (xhci->sp_ptrs) {
421 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
422 for (i = 0; i < max_sp_bufs; ++i) {
423 if (xhci->sp_ptrs[i])
424 free(phys_to_virt(xhci->sp_ptrs[i]));
425 }
426 }
427 free(xhci->sp_ptrs);
428 free(xhci->dcbaa);
429 free((void *)xhci->ev_ring_table);
430 free((void *)xhci->er.ring);
431 free((void *)xhci->cr.ring);
432 free(xhci);
433 free(controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000434}
435
436static void
437xhci_start (hci_t *controller)
438{
Nico Huber90292652013-06-13 14:37:15 +0200439 xhci_t *const xhci = XHCI_INST(controller);
440
441 xhci->opreg->usbcmd |= USBCMD_RS;
442 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_HCH, 0, 1000000L))
443 xhci_debug("Controller didn't start within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000444}
445
446static void
447xhci_stop (hci_t *controller)
448{
Nico Huber90292652013-06-13 14:37:15 +0200449 xhci_t *const xhci = XHCI_INST(controller);
450
451 xhci->opreg->usbcmd &= ~USBCMD_RS;
452 if (!xhci_handshake(&xhci->opreg->usbsts,
453 USBSTS_HCH, USBSTS_HCH, 1000000L))
454 xhci_debug("Controller didn't halt within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000455}
456
457static int
Nico Huber90292652013-06-13 14:37:15 +0200458xhci_reset_endpoint(usbdev_t *const dev, endpoint_t *const ep,
459 const int clear_halt)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000460{
Nico Huber90292652013-06-13 14:37:15 +0200461 xhci_t *const xhci = XHCI_INST(dev->controller);
462 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, dev->address);
463 const int slot_id = dev->address;
464 const int ep_id = ep ? xhci_ep_id(ep) : 1;
465
466 xhci_debug("Resetting ID %d EP %d (ep state: %d)\n",
467 slot_id, ep_id, EC_GET(STATE, di->devctx.eps[ep_id]));
468
469 /* Run Reset Endpoint Command if the EP is in Halted state */
470 if (EC_GET(STATE, di->devctx.eps[ep_id]) == 2) {
471 const int cc = xhci_cmd_reset_endpoint(xhci, slot_id, ep_id);
472 if (cc != CC_SUCCESS) {
473 xhci_debug("Reset Endpoint Command failed: %d\n", cc);
474 return 1;
475 }
476 }
477
478 /* Clear TT buffer for bulk and control endpoints behind a TT */
479 const int hub = dev->hub;
480 if (hub && dev->speed < HIGH_SPEED &&
481 dev->controller->devices[hub]->speed == HIGH_SPEED)
482 /* TODO */;
483
484 /* Try clearing the device' halt condition on non-control endpoints */
485 if (clear_halt && ep)
486 clear_stall(ep);
487
488 /* Reset transfer ring if the endpoint is in the right state */
489 const unsigned ep_state = EC_GET(STATE, di->devctx.eps[ep_id]);
490 if (ep_state == 3 || ep_state == 4) {
491 transfer_ring_t *const tr = di->transfer_rings[ep_id];
492 const int cc = xhci_cmd_set_tr_dq(xhci, slot_id, ep_id,
493 tr->ring, 1);
494 if (cc != CC_SUCCESS) {
495 xhci_debug("Set TR Dequeue Command failed: %d\n", cc);
496 return 1;
497 }
498 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
499 }
500
501 xhci_debug("Finished resetting ID %d EP %d (ep state: %d)\n",
502 slot_id, ep_id, EC_GET(STATE, di->devctx.eps[ep_id]));
503
504 return 0;
505}
506
507static void
508xhci_enqueue_trb(transfer_ring_t *const tr)
509{
510 const int chain = TRB_GET(CH, tr->cur);
511 TRB_SET(C, tr->cur, tr->pcs);
512 ++tr->cur;
513
514 while (TRB_GET(TT, tr->cur) == TRB_LINK) {
515 xhci_spew("Handling LINK pointer\n");
516 const int tc = TRB_GET(TC, tr->cur);
517 TRB_SET(CH, tr->cur, chain);
518 TRB_SET(C, tr->cur, tr->pcs);
519 tr->cur = phys_to_virt(tr->cur->ptr_low);
520 if (tc)
521 tr->pcs ^= 1;
522 }
523}
524
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;
543 } else {
544 packets -= (residue + cur_length) / mps;
545 residue = (residue + cur_length) % mps;
546 length -= cur_length;
547 }
548
549 trb = tr->cur;
550 xhci_clear_trb(trb, tr->pcs);
551 trb->ptr_low = virt_to_phys(cur_start);
552 TRB_SET(TL, trb, cur_length);
553 TRB_SET(TDS, trb, packets);
Julius Werner83da5012013-09-27 12:45:11 -0700554 TRB_SET(CH, trb, 1);
Nico Huber90292652013-06-13 14:37:15 +0200555
556 /* Check for first, data stage TRB */
557 if (!trb_count && ep == 1) {
558 TRB_SET(DIR, trb, dir);
559 TRB_SET(TT, trb, TRB_DATA_STAGE);
560 } else {
561 TRB_SET(TT, trb, TRB_NORMAL);
562 }
563
Nico Huber90292652013-06-13 14:37:15 +0200564 xhci_enqueue_trb(tr);
565
566 cur_start += cur_length;
567 ++trb_count;
568 }
Julius Werner83da5012013-09-27 12:45:11 -0700569
570 trb = tr->cur;
571 xhci_clear_trb(trb, tr->pcs);
572 trb->ptr_low = virt_to_phys(trb); /* for easier debugging only */
573 TRB_SET(TT, trb, TRB_EVENT_DATA);
574 TRB_SET(IOC, trb, 1);
575
576 xhci_enqueue_trb(tr);
Nico Huber90292652013-06-13 14:37:15 +0200577}
578
579static int
580xhci_control(usbdev_t *const dev, const direction_t dir,
581 const int drlen, void *const devreq,
582 const int dalen, unsigned char *const data)
583{
584 xhci_t *const xhci = XHCI_INST(dev->controller);
585 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, dev->address);
586 transfer_ring_t *const tr = di->transfer_rings[1];
587
588 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700589 if ((off + dalen) > ((TRANSFER_RING_SIZE - 4) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200590 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800591 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200592 }
593
594 /* Reset endpoint if it's halted */
595 const unsigned ep_state = EC_GET(STATE, di->devctx.ep0);
596 if (ep_state == 2 || ep_state == 4) {
597 if (xhci_reset_endpoint(dev, NULL, 0))
Julius Wernere9738db2013-02-21 13:41:40 -0800598 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200599 }
600
601 /* Fill and enqueue setup TRB */
602 trb_t *const setup = tr->cur;
603 xhci_clear_trb(setup, tr->pcs);
604 setup->ptr_low = ((u32 *)devreq)[0];
605 setup->ptr_high = ((u32 *)devreq)[1];
606 TRB_SET(TL, setup, 8);
607 TRB_SET(TRT, setup, (dalen)
608 ? ((dir == OUT) ? TRB_TRT_OUT_DATA : TRB_TRT_IN_DATA)
609 : TRB_TRT_NO_DATA);
610 TRB_SET(TT, setup, TRB_SETUP_STAGE);
611 TRB_SET(IDT, setup, 1);
612 TRB_SET(IOC, setup, 1);
613 xhci_enqueue_trb(tr);
614
615 /* Fill and enqueue data TRBs (if any) */
616 if (dalen) {
617 const unsigned mps = EC_GET(MPS, di->devctx.ep0);
618 const unsigned dt_dir = (dir == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
619 xhci_enqueue_td(tr, 1, mps, dalen, data, dt_dir);
620 }
621
622 /* Fill status TRB */
623 trb_t *const status = tr->cur;
624 xhci_clear_trb(status, tr->pcs);
625 TRB_SET(DIR, status, (dir == OUT) ? TRB_DIR_IN : TRB_DIR_OUT);
626 TRB_SET(TT, status, TRB_STATUS_STAGE);
627 TRB_SET(IOC, status, 1);
628 xhci_enqueue_trb(tr);
629
630 /* Ring doorbell for EP0 */
631 xhci->dbreg[dev->address] = 1;
632
633 /* Wait for transfer events */
Julius Wernere9738db2013-02-21 13:41:40 -0800634 int i, transferred = 0;
Nico Huber90292652013-06-13 14:37:15 +0200635 const int n_stages = 2 + !!dalen;
636 for (i = 0; i < n_stages; ++i) {
637 const int ret = xhci_wait_for_transfer(xhci, dev->address, 1);
Julius Wernere9738db2013-02-21 13:41:40 -0800638 transferred += ret;
639 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200640 if (ret == TIMEOUT) {
641 xhci_debug("Stopping ID %d EP 1\n",
642 dev->address);
643 xhci_cmd_stop_endpoint(xhci, dev->address, 1);
644 }
645 xhci_debug("Stage %d/%d failed: %d\n"
646 " trb ring: @%p\n"
647 " setup trb: @%p\n"
648 " status trb: @%p\n"
649 " ep state: %d -> %d\n"
650 " usbsts: 0x%08"PRIx32"\n",
651 i, n_stages, ret,
652 tr->ring, setup, status,
653 ep_state, EC_GET(STATE, di->devctx.ep0),
654 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800655 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200656 }
657 }
658
Julius Wernere9738db2013-02-21 13:41:40 -0800659 return transferred;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000660}
661
662/* finalize == 1: if data is of packet aligned size, add a zero length packet */
663static int
Nico Huber90292652013-06-13 14:37:15 +0200664xhci_bulk(endpoint_t *const ep,
665 const int size, u8 *const data,
666 const int finalize)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000667{
Nico Huber90292652013-06-13 14:37:15 +0200668 /* finalize: Hopefully the xHCI controller always does this.
669 We have no control over the packets. */
670
671 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
672 const int ep_id = xhci_ep_id(ep);
673 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address);
674 transfer_ring_t *const tr = di->transfer_rings[ep_id];
675
676 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700677 if ((off + size) > ((TRANSFER_RING_SIZE - 2) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200678 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800679 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200680 }
681
682 /* Reset endpoint if it's halted */
683 const unsigned ep_state = EC_GET(STATE, di->devctx.eps[ep_id]);
684 if (ep_state == 2 || ep_state == 4) {
685 if (xhci_reset_endpoint(ep->dev, ep, 0))
Julius Wernere9738db2013-02-21 13:41:40 -0800686 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200687 }
688
689 /* Enqueue transfer and ring doorbell */
690 const unsigned mps = EC_GET(MPS, di->devctx.eps[ep_id]);
691 const unsigned dir = (ep->direction == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
692 xhci_enqueue_td(tr, ep_id, mps, size, data, dir);
693 xhci->dbreg[ep->dev->address] = ep_id;
694
695 /* Wait for transfer event */
696 const int ret = xhci_wait_for_transfer(xhci, ep->dev->address, ep_id);
Julius Wernere9738db2013-02-21 13:41:40 -0800697 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200698 if (ret == TIMEOUT) {
699 xhci_debug("Stopping ID %d EP %d\n",
700 ep->dev->address, ep_id);
701 xhci_cmd_stop_endpoint(xhci, ep->dev->address, ep_id);
Julius Wernere9738db2013-02-21 13:41:40 -0800702 } else if (ret == -CC_STALL_ERROR) {
Nico Huber90292652013-06-13 14:37:15 +0200703 xhci_reset_endpoint(ep->dev, ep, 1);
704 }
705 xhci_debug("Bulk transfer failed: %d\n"
706 " ep state: %d -> %d\n"
707 " usbsts: 0x%08"PRIx32"\n",
708 ret, ep_state,
709 EC_GET(STATE, di->devctx.eps[ep_id]),
710 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800711 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200712 }
713
Julius Wernere9738db2013-02-21 13:41:40 -0800714 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200715}
716
717static trb_t *
718xhci_next_trb(trb_t *cur, int *const pcs)
719{
720 ++cur;
721 while (TRB_GET(TT, cur) == TRB_LINK) {
722 if (pcs && TRB_GET(TC, cur))
723 *pcs ^= 1;
724 cur = phys_to_virt(cur->ptr_low);
725 }
726 return cur;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000727}
728
729/* create and hook-up an intr queue into device schedule */
Nico Huber90292652013-06-13 14:37:15 +0200730static void *
731xhci_create_intr_queue(endpoint_t *const ep,
732 const int reqsize, const int reqcount,
733 const int reqtiming)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000734{
Nico Huber90292652013-06-13 14:37:15 +0200735 /* reqtiming: We ignore it and use the interval from the
736 endpoint descriptor configured earlier. */
737
738 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
739 const int ep_id = xhci_ep_id(ep);
740 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address);
741 transfer_ring_t *const tr = di->transfer_rings[ep_id];
742
743 if (reqcount > (TRANSFER_RING_SIZE - 2)) {
744 xhci_debug("reqcount is too high, at most %d supported\n",
745 TRANSFER_RING_SIZE - 2);
746 return NULL;
747 }
748 if (reqsize > 0x10000) {
749 xhci_debug("reqsize is too large, at most 64KiB supported\n");
750 return NULL;
751 }
752 if (di->interrupt_queues[ep_id]) {
753 xhci_debug("Only one interrupt queue per endpoint supported\n");
754 return NULL;
755 }
756
757 /* Allocate intrq structure and reqdata chunks */
758
759 intrq_t *const intrq = malloc(sizeof(*intrq));
760 if (!intrq) {
761 xhci_debug("Out of memory\n");
762 return NULL;
763 }
764
765 int i;
766 int pcs = tr->pcs;
767 trb_t *cur = tr->cur;
768 for (i = 0; i < reqcount; ++i) {
769 if (TRB_GET(C, cur) == pcs) {
770 xhci_debug("Not enough empty TRBs\n");
771 goto _free_return;
772 }
773 void *const reqdata = xhci_align(1, reqsize);
774 if (!reqdata) {
775 xhci_debug("Out of memory\n");
776 goto _free_return;
777 }
778 xhci_clear_trb(cur, pcs);
779 cur->ptr_low = virt_to_phys(reqdata);
780 cur->ptr_high = 0;
781 TRB_SET(TL, cur, reqsize);
782 TRB_SET(TT, cur, TRB_NORMAL);
783 TRB_SET(ISP, cur, 1);
784 TRB_SET(IOC, cur, 1);
785
786 cur = xhci_next_trb(cur, &pcs);
787 }
788
789 intrq->size = reqsize;
790 intrq->count = reqcount;
791 intrq->next = tr->cur;
792 intrq->ready = NULL;
793 intrq->ep = ep;
794 di->interrupt_queues[ep_id] = intrq;
795
796 /* Now enqueue all the prepared TRBs but the last
797 and ring the doorbell. */
798 for (i = 0; i < (reqcount - 1); ++i)
799 xhci_enqueue_trb(tr);
800 xhci->dbreg[ep->dev->address] = ep_id;
801
802 return intrq;
803
804_free_return:
805 cur = tr->cur;
806 for (--i; i >= 0; --i) {
807 free(phys_to_virt(cur->ptr_low));
808 cur = xhci_next_trb(cur, NULL);
809 }
810 free(intrq);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000811 return NULL;
812}
813
814/* remove queue from device schedule, dropping all data that came in */
815static void
Nico Huber90292652013-06-13 14:37:15 +0200816xhci_destroy_intr_queue(endpoint_t *const ep, void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000817{
Nico Huber90292652013-06-13 14:37:15 +0200818 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
819 const int ep_id = xhci_ep_id(ep);
820 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address);
821 transfer_ring_t *const tr = di->transfer_rings[ep_id];
822
823 intrq_t *const intrq = (intrq_t *)q;
824
825 /* Make sure the endpoint is stopped */
826 if (EC_GET(STATE, di->devctx.eps[ep_id]) == 1) {
827 const int cc = xhci_cmd_stop_endpoint(
828 xhci, ep->dev->address, ep_id);
829 if (cc != CC_SUCCESS)
830 xhci_debug("Warning: Failed to stop endpoint\n");
831 }
832
833 /* Process all remaining transfer events */
834 xhci_handle_events(xhci);
835
836 /* Free all pending transfers and the interrupt queue structure */
837 int i;
838 for (i = 0; i < intrq->count; ++i) {
839 free(phys_to_virt(intrq->next->ptr_low));
840 intrq->next = xhci_next_trb(intrq->next, NULL);
841 }
842 di->interrupt_queues[ep_id] = NULL;
843 free((void *)intrq);
844
845 /* Reset the controller's dequeue pointer and reinitialize the ring */
846 xhci_cmd_set_tr_dq(xhci, ep->dev->address, ep_id, tr->ring, 1);
847 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000848}
849
850/* read one intr-packet from queue, if available. extend the queue for new input.
851 return NULL if nothing new available.
852 Recommended use: while (data=poll_intr_queue(q)) process(data);
853 */
Nico Huber90292652013-06-13 14:37:15 +0200854static u8 *
855xhci_poll_intr_queue(void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000856{
Nico Huber90292652013-06-13 14:37:15 +0200857 if (!q)
858 return NULL;
859
860 intrq_t *const intrq = (intrq_t *)q;
861 endpoint_t *const ep = intrq->ep;
862 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
863
864 /* TODO: Reset interrupt queue if it gets halted? */
865
866 xhci_handle_events(xhci);
867
868 u8 *reqdata = NULL;
869 while (!reqdata && intrq->ready) {
870 const int ep_id = xhci_ep_id(ep);
871 devinfo_t *const di = DEVINFO_FROM_XHCI(xhci, ep->dev->address);
872 transfer_ring_t *const tr = di->transfer_rings[ep_id];
873
874 /* Fetch the request's buffer */
875 reqdata = phys_to_virt(intrq->next->ptr_low);
876
877 /* Enqueue the last (spare) TRB and ring doorbell */
878 xhci_enqueue_trb(tr);
879 xhci->dbreg[ep->dev->address] = ep_id;
880
881 /* Reuse the current buffer for the next spare TRB */
882 xhci_clear_trb(tr->cur, tr->pcs);
883 tr->cur->ptr_low = virt_to_phys(reqdata);
884 tr->cur->ptr_high = 0;
885 TRB_SET(TL, tr->cur, intrq->size);
886 TRB_SET(TT, tr->cur, TRB_NORMAL);
887 TRB_SET(ISP, tr->cur, 1);
888 TRB_SET(IOC, tr->cur, 1);
889
890 /* Check if anything was transferred */
891 const size_t read = TRB_GET(TL, intrq->next);
892 if (!read)
893 reqdata = NULL;
894 else if (read < intrq->size)
895 /* At least zero it, poll interface is rather limited */
896 memset(reqdata + read, 0x00, intrq->size - read);
897
898 /* Advance the interrupt queue */
899 if (intrq->ready == intrq->next)
900 /* This was last TRB being ready */
901 intrq->ready = NULL;
902 intrq->next = xhci_next_trb(intrq->next, NULL);
903 }
904
905 return reqdata;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000906}