blob: 18ba4e810499b8d74987c80966888016ffddcf48 [file] [log] [blame]
Nico Huber90292652013-06-13 14:37:15 +02001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2013 secunet Security Networks AG
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30//#define XHCI_SPEW_DEBUG
31
32#include <arch/virtual.h>
33#include "xhci_private.h"
34
35static u32
36xhci_gen_route(xhci_t *const xhci, const int hubport, const int hubaddr)
37{
38 if (!hubaddr)
39 return 0;
Julius Werner1f864342013-09-03 17:15:31 -070040 u32 route_string = SC_GET(ROUTE, xhci->dev[hubaddr].ctx.slot);
Nico Huber90292652013-06-13 14:37:15 +020041 int i;
42 for (i = 0; i < 20; i += 4) {
43 if (!(route_string & (0xf << i))) {
44 route_string |= (hubport & 0xf) << i;
45 break;
46 }
47 }
48 return route_string;
49}
50
51static int
52xhci_get_rh_port(xhci_t *const xhci, const int hubport, const int hubaddr)
53{
54 if (!hubaddr)
55 return hubport;
Julius Werner1f864342013-09-03 17:15:31 -070056 return SC_GET(RHPORT, xhci->dev[hubaddr].ctx.slot);
Nico Huber90292652013-06-13 14:37:15 +020057}
58
59static int
60xhci_get_tt(xhci_t *const xhci, const int xhci_speed,
61 const int hubport, const int hubaddr,
62 int *const tt, int *const tt_port)
63{
64 if (!hubaddr)
65 return 0;
Julius Werner1f864342013-09-03 17:15:31 -070066 const slotctx_t *const slot = xhci->dev[hubaddr].ctx.slot;
67 if ((*tt = SC_GET(TTID, slot))) {
68 *tt_port = SC_GET(TTPORT, slot);
Nico Huber90292652013-06-13 14:37:15 +020069 } else if (xhci_speed < XHCI_HIGH_SPEED &&
Julius Werner1f864342013-09-03 17:15:31 -070070 SC_GET(SPEED, slot) == XHCI_HIGH_SPEED) {
Nico Huber90292652013-06-13 14:37:15 +020071 *tt = hubaddr;
72 *tt_port = hubport;
73 }
74 return *tt != 0;
75}
76
77static long
78xhci_decode_mps0(const int xhci_speed, const u8 b_mps)
79{
80 switch (xhci_speed) {
81 case XHCI_LOW_SPEED:
82 case XHCI_FULL_SPEED:
83 case XHCI_HIGH_SPEED:
84 switch (b_mps) {
85 case 8: case 16: case 32: case 64:
86 return b_mps;
87 default:
88 xhci_debug("Invalid MPS0: 0x%02x\n", b_mps);
89 return 8;
90 }
91 break;
92 case XHCI_SUPER_SPEED:
93 if (b_mps == 9) {
94 return 2 << b_mps;
95 } else {
96 xhci_debug("Invalid MPS0: 0x%02x\n", b_mps);
97 return 2 << 9;
98 }
99 break;
100 default:
101 xhci_debug("Invalid speed for MPS0: %d\n", xhci_speed);
102 return 8;
103 }
104}
105
106
107static long
108xhci_get_mps0(usbdev_t *const dev, const int xhci_speed)
109{
110 u8 buf[8];
111 dev_req_t dr = {
112 .bmRequestType = gen_bmRequestType(
113 device_to_host, standard_type, dev_recp),
114 .data_dir = device_to_host,
115 .bRequest = GET_DESCRIPTOR,
116 .wValue = (1 << 8),
117 .wIndex = 0,
118 .wLength = 8,
119 };
120 if (dev->controller->control(dev, IN, sizeof(dr), &dr, 8, buf)) {
121 xhci_debug("Failed to read MPS0\n");
122 return COMMUNICATION_ERROR;
123 } else {
124 return xhci_decode_mps0(xhci_speed, buf[7]);
125 }
126}
127
Julius Werner1f864342013-09-03 17:15:31 -0700128static inputctx_t *
129xhci_make_inputctx(const size_t ctxsize)
130{
131 int i;
132 const size_t size = (1 + NUM_EPS) * ctxsize;
133 inputctx_t *const ic = malloc(sizeof(*ic));
134 void *dma_buffer = dma_memalign(64, size);
135
136 if (!ic || !dma_buffer) {
137 free(ic);
138 free(dma_buffer);
139 return NULL;
140 }
141
142 memset(dma_buffer, 0, size);
143 ic->drop = dma_buffer + 0;
144 ic->add = dma_buffer + 4;
145 dma_buffer += ctxsize;
146 for (i = 0; i < NUM_EPS; i++, dma_buffer += ctxsize)
147 ic->dev.ep[i] = dma_buffer;
148
149 return ic;
150}
151
Nico Huber90292652013-06-13 14:37:15 +0200152int
153xhci_set_address (hci_t *controller, int speed, int hubport, int hubaddr)
154{
155 xhci_t *const xhci = XHCI_INST(controller);
156 const int xhci_speed = speed + 1;
Julius Werner1f864342013-09-03 17:15:31 -0700157 const size_t ctxsize = CTXSIZE(xhci);
158 devinfo_t *di = NULL;
Nico Huber90292652013-06-13 14:37:15 +0200159
Julius Werner1f864342013-09-03 17:15:31 -0700160 int i, ret = -1;
Nico Huber90292652013-06-13 14:37:15 +0200161
Julius Werner1f864342013-09-03 17:15:31 -0700162 inputctx_t *const ic = xhci_make_inputctx(ctxsize);
Nico Huber90292652013-06-13 14:37:15 +0200163 transfer_ring_t *const tr = malloc(sizeof(*tr));
164 if (tr)
165 tr->ring = xhci_align(16, TRANSFER_RING_SIZE * sizeof(trb_t));
Julius Werner1f864342013-09-03 17:15:31 -0700166 if (!ic || !tr || !tr->ring) {
Nico Huber90292652013-06-13 14:37:15 +0200167 xhci_debug("Out of memory\n");
168 goto _free_return;
169 }
170
171 int slot_id;
172 int cc = xhci_cmd_enable_slot(xhci, &slot_id);
173 if (cc != CC_SUCCESS) {
174 xhci_debug("Enable slot failed: %d\n", cc);
175 goto _free_return;
176 } else {
177 xhci_debug("Enabled slot %d\n", slot_id);
178 }
179
Julius Werner1f864342013-09-03 17:15:31 -0700180 di = &xhci->dev[slot_id];
181 void *dma_buffer = dma_memalign(64, NUM_EPS * ctxsize);
182 if (!dma_buffer)
183 goto _free_return;
184 memset(dma_buffer, 0, NUM_EPS * ctxsize);
185 for (i = 0; i < NUM_EPS; i++, dma_buffer += ctxsize)
186 di->ctx.ep[i] = dma_buffer;
187
188 *ic->add = (1 << 0) /* Slot Context */ | (1 << 1) /* EP0 Context */ ;
Nico Huber90292652013-06-13 14:37:15 +0200189
190 SC_SET(ROUTE, ic->dev.slot, xhci_gen_route(xhci, hubport, hubaddr));
191 SC_SET(SPEED, ic->dev.slot, xhci_speed);
192 SC_SET(CTXENT, ic->dev.slot, 1); /* the endpoint 0 context */
193 SC_SET(RHPORT, ic->dev.slot, xhci_get_rh_port(xhci, hubport, hubaddr));
194
195 int tt, tt_port;
196 if (xhci_get_tt(xhci, xhci_speed, hubport, hubaddr, &tt, &tt_port)) {
197 xhci_debug("TT for %d: %d[%d]\n", slot_id, tt, tt_port);
Julius Werner1f864342013-09-03 17:15:31 -0700198 SC_SET(MTT, ic->dev.slot, SC_GET(MTT, xhci->dev[tt].ctx.slot));
Nico Huber90292652013-06-13 14:37:15 +0200199 SC_SET(TTID, ic->dev.slot, tt);
200 SC_SET(TTPORT, ic->dev.slot, tt_port);
201 }
202
Nico Huber90292652013-06-13 14:37:15 +0200203 di->transfer_rings[1] = tr;
204 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
205
Julius Werner1f864342013-09-03 17:15:31 -0700206 ic->dev.ep0->tr_dq_low = virt_to_phys(tr->ring);
207 ic->dev.ep0->tr_dq_high = 0;
Nico Huber90292652013-06-13 14:37:15 +0200208 EC_SET(TYPE, ic->dev.ep0, EP_CONTROL);
209 EC_SET(AVRTRB, ic->dev.ep0, 8);
210 EC_SET(MPS, ic->dev.ep0, 8);
211 EC_SET(CERR, ic->dev.ep0, 3);
212 EC_SET(DCS, ic->dev.ep0, 1);
213
Julius Werner1f864342013-09-03 17:15:31 -0700214 xhci->dcbaa[slot_id] = virt_to_phys(di->ctx.raw);
Nico Huber90292652013-06-13 14:37:15 +0200215
216 cc = xhci_cmd_address_device(xhci, slot_id, ic);
217 if (cc != CC_SUCCESS) {
218 xhci_debug("Address device failed: %d\n", cc);
219 goto _disable_return;
220 } else {
221 xhci_debug("Addressed device %d (USB: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700222 slot_id, SC_GET(UADDR, di->ctx.slot));
Nico Huber90292652013-06-13 14:37:15 +0200223 }
224 mdelay(2); /* SetAddress() recovery interval (usb20 spec 9.2.6.3) */
225
226 init_device_entry(controller, slot_id);
227 controller->devices[slot_id]->address = slot_id;
228
229 const long mps0 = xhci_get_mps0(
230 controller->devices[slot_id], xhci_speed);
231 if (mps0 < 0) {
232 goto _disable_return;
233 } else if (mps0 != 8) {
Julius Werner1f864342013-09-03 17:15:31 -0700234 memset((void *)ic->dev.ep0, 0x00, ctxsize);
235 *ic->add = (1 << 1); /* EP0 Context */
Nico Huber90292652013-06-13 14:37:15 +0200236 EC_SET(MPS, ic->dev.ep0, mps0);
237 cc = xhci_cmd_evaluate_context(xhci, slot_id, ic);
238 if (cc != CC_SUCCESS) {
239 xhci_debug("Context evaluation failed: %d\n", cc);
240 goto _disable_return;
241 } else {
242 xhci_debug("Set MPS0 to %dB\n", mps0);
243 }
244 }
245
246 ret = slot_id;
247 goto _free_ic_return;
248
249_disable_return:
250 xhci_cmd_disable_slot(xhci, slot_id);
251 xhci->dcbaa[slot_id] = 0;
252_free_return:
253 if (tr)
254 free((void *)tr->ring);
255 free(tr);
Julius Werner1f864342013-09-03 17:15:31 -0700256 if (di)
257 free(di->ctx.raw);
Nico Huber90292652013-06-13 14:37:15 +0200258 free((void *)di);
259_free_ic_return:
Julius Werner1f864342013-09-03 17:15:31 -0700260 if (ic)
261 free(ic->raw);
Nico Huber90292652013-06-13 14:37:15 +0200262 free(ic);
263 return ret;
264}
265
266static int
267xhci_finish_hub_config(usbdev_t *const dev, inputctx_t *const ic)
268{
269 hub_descriptor_t *const descriptor = (hub_descriptor_t *)
270 get_descriptor(
271 dev,
272 gen_bmRequestType(device_to_host, class_type, dev_recp),
273 0x29, 0, 0);
274 if (!descriptor) {
275 xhci_debug("Failed to fetch hub descriptor\n");
276 return COMMUNICATION_ERROR;
277 }
278
279 SC_SET(HUB, ic->dev.slot, 1);
280 SC_SET(MTT, ic->dev.slot, 0); /* No support for Multi-TT */
281 SC_SET(NPORTS, ic->dev.slot, descriptor->bNbrPorts);
282 if (dev->speed == HIGH_SPEED)
283 SC_SET(TTT, ic->dev.slot,
284 (descriptor->wHubCharacteristics >> 5) & 0x0003);
285
286 free(descriptor);
287 return 0;
288}
289
290static size_t
291xhci_bound_interval(const endpoint_t *const ep)
292{
293 if ( (ep->dev->speed == LOW_SPEED &&
294 (ep->type == ISOCHRONOUS ||
295 ep->type == INTERRUPT)) ||
296 (ep->dev->speed == FULL_SPEED &&
297 ep->type == INTERRUPT))
298 {
299 if (ep->interval < 3)
300 return 3;
301 else if (ep->interval > 11)
302 return 11;
303 else
304 return ep->interval;
305 } else {
306 if (ep->interval < 0)
307 return 0;
308 else if (ep->interval > 15)
309 return 15;
310 else
311 return ep->interval;
312 }
313}
314
315static int
316xhci_finish_ep_config(const endpoint_t *const ep, inputctx_t *const ic)
317{
318 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Nico Huber90292652013-06-13 14:37:15 +0200319 const int ep_id = xhci_ep_id(ep);
320 xhci_debug("ep_id: %d\n", ep_id);
321 if (ep_id <= 1 || 32 <= ep_id)
322 return DRIVER_ERROR;
323
324 transfer_ring_t *const tr = malloc(sizeof(*tr));
325 if (tr)
326 tr->ring = xhci_align(16, TRANSFER_RING_SIZE * sizeof(trb_t));
327 if (!tr || !tr->ring) {
328 free(tr);
329 xhci_debug("Out of memory\n");
330 return OUT_OF_MEMORY;
331 }
Julius Werner1f864342013-09-03 17:15:31 -0700332 xhci->dev[ep->dev->address].transfer_rings[ep_id] = tr;
Nico Huber90292652013-06-13 14:37:15 +0200333 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
334
Julius Werner1f864342013-09-03 17:15:31 -0700335 *ic->add |= (1 << ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200336 if (SC_GET(CTXENT, ic->dev.slot) < ep_id)
337 SC_SET(CTXENT, ic->dev.slot, ep_id);
338
Julius Werner1f864342013-09-03 17:15:31 -0700339 epctx_t *const epctx = ic->dev.ep[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200340 xhci_debug("Filling epctx (@%p)\n", epctx);
341 epctx->tr_dq_low = virt_to_phys(tr->ring);
342 epctx->tr_dq_high = 0;
Julius Werner1f864342013-09-03 17:15:31 -0700343 EC_SET(INTVAL, epctx, xhci_bound_interval(ep));
344 EC_SET(CERR, epctx, 3);
345 EC_SET(TYPE, epctx, ep->type | ((ep->direction != OUT) << 2));
346 EC_SET(MPS, epctx, ep->maxpacketsize);
347 EC_SET(DCS, epctx, 1);
Nico Huber90292652013-06-13 14:37:15 +0200348 size_t avrtrb;
349 switch (ep->type) {
350 case BULK: case ISOCHRONOUS: avrtrb = 3 * 1024; break;
351 case INTERRUPT: avrtrb = 1024; break;
352 default: avrtrb = 8; break;
353 }
Julius Werner1f864342013-09-03 17:15:31 -0700354 EC_SET(AVRTRB, epctx, avrtrb);
355 EC_SET(MXESIT, epctx, EC_GET(MPS, epctx) * EC_GET(MBS, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200356
357 return 0;
358}
359
360int
361xhci_finish_device_config(usbdev_t *const dev)
362{
363 xhci_t *const xhci = XHCI_INST(dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700364 devinfo_t *const di = &xhci->dev[dev->address];
Nico Huber90292652013-06-13 14:37:15 +0200365
366 int i, ret = 0;
367
Julius Werner1f864342013-09-03 17:15:31 -0700368 inputctx_t *const ic = xhci_make_inputctx(CTXSIZE(xhci));
Nico Huber90292652013-06-13 14:37:15 +0200369 if (!ic) {
370 xhci_debug("Out of memory\n");
371 return OUT_OF_MEMORY;
372 }
Nico Huber90292652013-06-13 14:37:15 +0200373
Julius Werner1f864342013-09-03 17:15:31 -0700374 *ic->add = (1 << 0); /* Slot Context */
Nico Huber90292652013-06-13 14:37:15 +0200375
Julius Werner1f864342013-09-03 17:15:31 -0700376 xhci_dump_slotctx(di->ctx.slot);
377 ic->dev.slot->f1 = di->ctx.slot->f1;
378 ic->dev.slot->f2 = di->ctx.slot->f2;
379 ic->dev.slot->f3 = di->ctx.slot->f3;
Nico Huber90292652013-06-13 14:37:15 +0200380
381 if (((device_descriptor_t *)dev->descriptor)->bDeviceClass == 0x09) {
382 ret = xhci_finish_hub_config(dev, ic);
383 if (ret)
384 goto _free_return;
385 }
386
387 for (i = 1; i < dev->num_endp; ++i) {
388 ret = xhci_finish_ep_config(&dev->endpoints[i], ic);
389 if (ret)
390 goto _free_ep_ctx_return;
391 }
392
393 xhci_dump_inputctx(ic);
394
395 const int config_id = ((configuration_descriptor_t *)
396 dev->configuration)->bConfigurationValue;
397 xhci_debug("config_id: %d\n", config_id);
398 const int cc =
399 xhci_cmd_configure_endpoint(xhci, dev->address, config_id, ic);
400 if (cc != CC_SUCCESS) {
401 xhci_debug("Configure endpoint failed: %d\n", cc);
402 ret = CONTROLLER_ERROR;
403 goto _free_ep_ctx_return;
404 } else {
405 xhci_debug("Endpoints configured\n");
406 }
407
408 goto _free_return;
409
410_free_ep_ctx_return:
411 for (i = 2; i < 31; ++i) {
412 if (di->transfer_rings[i])
413 free((void *)di->transfer_rings[i]->ring);
414 free(di->transfer_rings[i]);
415 di->transfer_rings[i] = NULL;
416 }
417_free_return:
Julius Werner1f864342013-09-03 17:15:31 -0700418 free(ic->raw);
Nico Huber90292652013-06-13 14:37:15 +0200419 free(ic);
420 return ret;
421}
422
423void
424xhci_destroy_dev(hci_t *const controller, const int slot_id)
425{
426 xhci_t *const xhci = XHCI_INST(controller);
427
428 if (slot_id <= 0 || xhci->max_slots_en > slot_id)
429 return;
430
431 int i;
432
433 const int cc = xhci_cmd_disable_slot(xhci, slot_id);
434 if (cc != CC_SUCCESS)
435 xhci_debug("Failed to disable slot %d: %d\n", slot_id, cc);
436
Julius Werner1f864342013-09-03 17:15:31 -0700437 devinfo_t *const di = &xhci->dev[slot_id];
Nico Huber90292652013-06-13 14:37:15 +0200438 for (i = 1; i < 31; ++i) {
439 if (di->transfer_rings[i])
440 free((void *)di->transfer_rings[i]->ring);
441 free(di->transfer_rings[i]);
442
443 free(di->interrupt_queues[i]);
444 }
Nico Huber90292652013-06-13 14:37:15 +0200445 xhci->dcbaa[slot_id] = 0;
446}