source: mainline/uspace/drv/bus/usb/uhci/hc.c@ babcc423

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since babcc423 was 9b8dac4, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

uhci: do not wait on _locked without the lock

  • Property mode set to 100644
File size: 17.3 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - 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 ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbuhcihc
30 * @{
31 */
32/** @file
33 * @brief UHCI Host controller driver routines
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <async.h>
39#include <ddi.h>
40#include <device/hw_res_parsed.h>
41#include <fibril.h>
42#include <errno.h>
43#include <macros.h>
44#include <mem.h>
45#include <stdlib.h>
46#include <stdint.h>
47#include <str_error.h>
48
49#include <usb/debug.h>
50#include <usb/usb.h>
51#include <usb/host/utils/malloc32.h>
52#include <usb/host/bandwidth.h>
53
54#include "uhci_batch.h"
55#include "transfer_list.h"
56#include "hc.h"
57
58#define UHCI_INTR_ALLOW_INTERRUPTS \
59 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
60#define UHCI_STATUS_USED_INTERRUPTS \
61 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
62
63static const irq_pio_range_t uhci_irq_pio_ranges[] = {
64 {
65 .base = 0,
66 .size = sizeof(uhci_regs_t)
67 }
68};
69
70static const irq_cmd_t uhci_irq_commands[] = {
71 {
72 .cmd = CMD_PIO_READ_16,
73 .dstarg = 1,
74 .addr = NULL
75 },
76 {
77 .cmd = CMD_AND,
78 .srcarg = 1,
79 .dstarg = 2,
80 .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
81 },
82 {
83 .cmd = CMD_PREDICATE,
84 .srcarg = 2,
85 .value = 2
86 },
87 {
88 .cmd = CMD_PIO_WRITE_A_16,
89 .srcarg = 1,
90 .addr = NULL
91 },
92 {
93 .cmd = CMD_ACCEPT
94 }
95};
96
97static void hc_init_hw(const hc_t *instance);
98static int hc_init_mem_structures(hc_t *instance, hc_device_t *);
99static int hc_init_transfer_lists(hc_t *instance);
100
101static int hc_debug_checker(void *arg);
102
103
104/** Generate IRQ code.
105 * @param[out] code IRQ code structure.
106 * @param[in] hw_res Device's resources.
107 *
108 * @return Error code.
109 */
110int hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
111{
112 assert(code);
113 assert(hw_res);
114
115 if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
116 return EINVAL;
117 const addr_range_t regs = hw_res->io_ranges.ranges[0];
118
119 if (RNGSZ(regs) < sizeof(uhci_regs_t))
120 return EOVERFLOW;
121
122 code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
123 if (code->ranges == NULL)
124 return ENOMEM;
125
126 code->cmds = malloc(sizeof(uhci_irq_commands));
127 if (code->cmds == NULL) {
128 free(code->ranges);
129 return ENOMEM;
130 }
131
132 code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
133 code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
134
135 memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
136 code->ranges[0].base = RNGABS(regs);
137
138 memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
139 uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
140 code->cmds[0].addr = (void*)&registers->usbsts;
141 code->cmds[3].addr = (void*)&registers->usbsts;
142
143 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.",
144 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
145
146 return hw_res->irqs.irqs[0];
147}
148
149/** Take action based on the interrupt cause.
150 *
151 * @param[in] hcd HCD structure to use.
152 * @param[in] status Value of the status register at the time of interrupt.
153 *
154 * Interrupt might indicate:
155 * - transaction completed, either by triggering IOC, SPD, or an error
156 * - some kind of device error
157 * - resume from suspend state (not implemented)
158 */
159static void hc_interrupt(bus_t *bus, uint32_t status)
160{
161 hc_t *instance = bus_to_hc(bus);
162
163 /* Lower 2 bits are transaction error and transaction complete */
164 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
165 LIST_INITIALIZE(done);
166 transfer_list_remove_finished(
167 &instance->transfers_interrupt, &done);
168 transfer_list_remove_finished(
169 &instance->transfers_control_slow, &done);
170 transfer_list_remove_finished(
171 &instance->transfers_control_full, &done);
172 transfer_list_remove_finished(
173 &instance->transfers_bulk_full, &done);
174
175 list_foreach_safe(done, current, next) {
176 list_remove(current);
177 uhci_transfer_batch_t *batch =
178 uhci_transfer_batch_from_link(current);
179 usb_transfer_batch_finish(&batch->base);
180 }
181 }
182 /* Resume interrupts are not supported */
183 if (status & UHCI_STATUS_RESUME) {
184 usb_log_error("Resume interrupt!");
185 }
186
187 /* Bits 4 and 5 indicate hc error */
188 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
189 usb_log_error("UHCI hardware failure!.");
190 ++instance->hw_failures;
191 transfer_list_abort_all(&instance->transfers_interrupt);
192 transfer_list_abort_all(&instance->transfers_control_slow);
193 transfer_list_abort_all(&instance->transfers_control_full);
194 transfer_list_abort_all(&instance->transfers_bulk_full);
195
196 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
197 /* reinitialize hw, this triggers virtual disconnect*/
198 hc_init_hw(instance);
199 } else {
200 usb_log_fatal("Too many UHCI hardware failures!.");
201 hc_gone(&instance->base);
202 }
203 }
204}
205
206/** Initialize UHCI hc driver structure
207 *
208 * @param[in] instance Memory place to initialize.
209 * @param[in] regs Range of device's I/O control registers.
210 * @param[in] interrupts True if hw interrupts should be used.
211 * @return Error code.
212 * @note Should be called only once on any structure.
213 *
214 * Initializes memory structures, starts up hw, and launches debugger and
215 * interrupt fibrils.
216 */
217int hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
218{
219 hc_t *instance = hcd_to_hc(hcd);
220 assert(hw_res);
221 if (hw_res->io_ranges.count != 1 ||
222 hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
223 return EINVAL;
224
225 instance->hw_failures = 0;
226
227 /* allow access to hc control registers */
228 int ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
229 (void **) &instance->registers);
230 if (ret != EOK) {
231 usb_log_error("Failed to gain access to registers: %s.",
232 str_error(ret));
233 return ret;
234 }
235
236 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
237 hw_res->io_ranges.ranges[0].address.absolute,
238 hw_res->io_ranges.ranges[0].size);
239
240 ret = hc_init_mem_structures(instance, hcd);
241 if (ret != EOK) {
242 usb_log_error("Failed to init UHCI memory structures: %s.",
243 str_error(ret));
244 // TODO: we should disable pio here
245 return ret;
246 }
247
248 return EOK;
249}
250
251int hc_start(hc_device_t *hcd)
252{
253 hc_t *instance = hcd_to_hc(hcd);
254 hc_init_hw(instance);
255 (void)hc_debug_checker;
256
257 return uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
258}
259
260/** Safely dispose host controller internal structures
261 *
262 * @param[in] instance Host controller structure to use.
263 */
264int hc_gone(hc_device_t *instance)
265{
266 assert(instance);
267 //TODO Implement
268 return ENOTSUP;
269}
270
271/** Initialize UHCI hc hw resources.
272 *
273 * @param[in] instance UHCI structure to use.
274 * For magic values see UHCI Design Guide
275 */
276void hc_init_hw(const hc_t *instance)
277{
278 assert(instance);
279 uhci_regs_t *registers = instance->registers;
280
281 /* Reset everything, who knows what touched it before us */
282 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
283 async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
284 pio_write_16(&registers->usbcmd, 0);
285
286 /* Reset hc, all states and counters. Hope that hw is not broken */
287 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
288 do { async_usleep(10); }
289 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
290
291 /* Set frame to exactly 1ms */
292 pio_write_8(&registers->sofmod, 64);
293
294 /* Set frame list pointer */
295 const uint32_t pa = addr_to_phys(instance->frame_list);
296 pio_write_32(&registers->flbaseadd, pa);
297
298 if (instance->base.irq_cap >= 0) {
299 /* Enable all interrupts, but resume interrupt */
300 pio_write_16(&instance->registers->usbintr,
301 UHCI_INTR_ALLOW_INTERRUPTS);
302 }
303
304 const uint16_t cmd = pio_read_16(&registers->usbcmd);
305 if (cmd != 0)
306 usb_log_warning("Previous command value: %x.", cmd);
307
308 /* Start the hc with large(64B) packet FSBR */
309 pio_write_16(&registers->usbcmd,
310 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
311}
312
313static usb_transfer_batch_t *create_transfer_batch(endpoint_t *ep)
314{
315 uhci_transfer_batch_t *batch = uhci_transfer_batch_create(ep);
316 return &batch->base;
317}
318
319static void destroy_transfer_batch(usb_transfer_batch_t *batch)
320{
321 uhci_transfer_batch_destroy(uhci_transfer_batch_get(batch));
322}
323
324static void endpoint_unregister(endpoint_t *ep)
325{
326 hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
327 usb2_bus_ops.endpoint_unregister(ep);
328
329 uhci_transfer_batch_t *batch = NULL;
330
331 // Check for the roothub, as it does not schedule into lists
332 if (ep->device->speed == USB_SPEED_MAX) {
333 // FIXME: We shall check the roothub for active transfer. But
334 // as it is polling, there is no way to make it stop doing so.
335 // Return after rewriting uhci rh.
336 return;
337 }
338
339 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
340 assert(list);
341
342 // To avoid ABBA deadlock, we need to take the list first
343 fibril_mutex_lock(&list->guard);
344 fibril_mutex_lock(&ep->guard);
345 if (ep->active_batch) {
346 batch = uhci_transfer_batch_get(ep->active_batch);
347 endpoint_deactivate_locked(ep);
348 transfer_list_remove_batch(list, batch);
349 }
350 fibril_mutex_unlock(&ep->guard);
351 fibril_mutex_unlock(&list->guard);
352
353 if (batch) {
354 // The HW could have been looking at the batch.
355 // Better wait two frames before we release the buffers.
356 async_usleep(2000);
357 batch->base.error = EINTR;
358 batch->base.transfered_size = 0;
359 usb_transfer_batch_finish(&batch->base);
360 }
361}
362
363static int hc_status(bus_t *, uint32_t *);
364static int hc_schedule(usb_transfer_batch_t *);
365
366static const bus_ops_t uhci_bus_ops = {
367 .parent = &usb2_bus_ops,
368
369 .interrupt = hc_interrupt,
370 .status = hc_status,
371
372 .endpoint_unregister = endpoint_unregister,
373 .endpoint_count_bw = bandwidth_count_usb11,
374
375 .batch_create = create_transfer_batch,
376 .batch_schedule = hc_schedule,
377 .batch_destroy = destroy_transfer_batch,
378};
379
380/** Initialize UHCI hc memory structures.
381 *
382 * @param[in] instance UHCI structure to use.
383 * @return Error code
384 * @note Should be called only once on any structure.
385 *
386 * Structures:
387 * - transfer lists (queue heads need to be accessible by the hw)
388 * - frame list page (needs to be one UHCI hw accessible 4K page)
389 */
390int hc_init_mem_structures(hc_t *instance, hc_device_t *hcd)
391{
392 assert(instance);
393
394 usb2_bus_init(&instance->bus, BANDWIDTH_AVAILABLE_USB11);
395
396 bus_t *bus = (bus_t *) &instance->bus;
397 bus->ops = &uhci_bus_ops;
398
399 hc_device_setup(&instance->base, bus);
400
401 /* Init USB frame list page */
402 instance->frame_list = get_page();
403 if (!instance->frame_list) {
404 return ENOMEM;
405 }
406 usb_log_debug("Initialized frame list at %p.", instance->frame_list);
407
408 /* Init transfer lists */
409 int ret = hc_init_transfer_lists(instance);
410 if (ret != EOK) {
411 usb_log_error("Failed to initialize transfer lists.");
412 return_page(instance->frame_list);
413 return ENOMEM;
414 }
415 usb_log_debug("Initialized transfer lists.");
416
417
418 /* Set all frames to point to the first queue head */
419 const uint32_t queue = LINK_POINTER_QH(
420 addr_to_phys(instance->transfers_interrupt.queue_head));
421
422 for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
423 instance->frame_list[i] = queue;
424 }
425
426 return EOK;
427}
428
429/** Initialize UHCI hc transfer lists.
430 *
431 * @param[in] instance UHCI structure to use.
432 * @return Error code
433 * @note Should be called only once on any structure.
434 *
435 * Initializes transfer lists and sets them in one chain to support proper
436 * USB scheduling. Sets pointer table for quick access.
437 */
438int hc_init_transfer_lists(hc_t *instance)
439{
440 assert(instance);
441#define SETUP_TRANSFER_LIST(type, name) \
442do { \
443 int ret = transfer_list_init(&instance->transfers_##type, name); \
444 if (ret != EOK) { \
445 usb_log_error("Failed to setup %s transfer list: %s.", \
446 name, str_error(ret)); \
447 transfer_list_fini(&instance->transfers_bulk_full); \
448 transfer_list_fini(&instance->transfers_control_full); \
449 transfer_list_fini(&instance->transfers_control_slow); \
450 transfer_list_fini(&instance->transfers_interrupt); \
451 return ret; \
452 } \
453} while (0)
454
455 SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
456 SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
457 SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
458 SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
459#undef SETUP_TRANSFER_LIST
460 /* Connect lists into one schedule */
461 transfer_list_set_next(&instance->transfers_control_full,
462 &instance->transfers_bulk_full);
463 transfer_list_set_next(&instance->transfers_control_slow,
464 &instance->transfers_control_full);
465 transfer_list_set_next(&instance->transfers_interrupt,
466 &instance->transfers_control_slow);
467
468 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
469 * buggy on certain hw, enable at your own risk. */
470#ifdef FSBR
471 transfer_list_set_next(&instance->transfers_bulk_full,
472 &instance->transfers_control_full);
473#endif
474
475 /* Assign pointers to be used during scheduling */
476 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
477 &instance->transfers_interrupt;
478 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
479 &instance->transfers_interrupt;
480 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
481 &instance->transfers_control_full;
482 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
483 &instance->transfers_control_slow;
484 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
485 &instance->transfers_bulk_full;
486
487 return EOK;
488}
489
490static int hc_status(bus_t *bus, uint32_t *status)
491{
492 hc_t *instance = bus_to_hc(bus);
493 assert(status);
494
495 *status = 0;
496 if (instance->registers) {
497 uint16_t s = pio_read_16(&instance->registers->usbsts);
498 pio_write_16(&instance->registers->usbsts, s);
499 *status = s;
500 }
501 return EOK;
502}
503
504/** Schedule batch for execution.
505 *
506 * @param[in] instance UHCI structure to use.
507 * @param[in] batch Transfer batch to schedule.
508 * @return Error code
509 *
510 * Checks for bandwidth availability and appends the batch to the proper queue.
511 */
512static int hc_schedule(usb_transfer_batch_t *batch)
513{
514 uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
515 endpoint_t *ep = batch->ep;
516 hc_t *hc = bus_to_hc(endpoint_get_bus(ep));
517
518 if (batch->target.address == uhci_rh_get_address(&hc->rh))
519 return uhci_rh_schedule(&hc->rh, batch);
520
521
522 const int err = uhci_transfer_batch_prepare(uhci_batch);
523 if (err)
524 return err;
525
526 transfer_list_t *list = hc->transfers[ep->device->speed][ep->transfer_type];
527 assert(list);
528 transfer_list_add_batch(list, uhci_batch);
529
530 return EOK;
531}
532
533int hc_unschedule_batch(usb_transfer_batch_t *batch)
534{
535
536 return EOK;
537}
538
539/** Debug function, checks consistency of memory structures.
540 *
541 * @param[in] arg UHCI structure to use.
542 * @return EOK (should never return)
543 */
544int hc_debug_checker(void *arg)
545{
546 hc_t *instance = arg;
547 assert(instance);
548
549#define QH(queue) \
550 instance->transfers_##queue.queue_head
551
552 while (1) {
553 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
554 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
555 const uint16_t intr =
556 pio_read_16(&instance->registers->usbintr);
557
558 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
559 usb_log_debug2("Command: %X Status: %X Intr: %x",
560 cmd, sts, intr);
561 }
562
563 const uintptr_t frame_list =
564 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
565 if (frame_list != addr_to_phys(instance->frame_list)) {
566 usb_log_debug("Framelist address: %p vs. %p.",
567 (void *) frame_list,
568 (void *) addr_to_phys(instance->frame_list));
569 }
570
571 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
572
573 uintptr_t expected_pa = instance->frame_list[frnum]
574 & LINK_POINTER_ADDRESS_MASK;
575 uintptr_t real_pa = addr_to_phys(QH(interrupt));
576 if (expected_pa != real_pa) {
577 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.",
578 (void *) expected_pa, frnum, (void *) real_pa);
579 }
580
581 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
582 real_pa = addr_to_phys(QH(control_slow));
583 if (expected_pa != real_pa) {
584 usb_log_debug("Control Slow QH: %p vs. %p.",
585 (void *) expected_pa, (void *) real_pa);
586 }
587
588 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
589 real_pa = addr_to_phys(QH(control_full));
590 if (expected_pa != real_pa) {
591 usb_log_debug("Control Full QH: %p vs. %p.",
592 (void *) expected_pa, (void *) real_pa);
593 }
594
595 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
596 real_pa = addr_to_phys(QH(bulk_full));
597 if (expected_pa != real_pa ) {
598 usb_log_debug("Bulk QH: %p vs. %p.",
599 (void *) expected_pa, (void *) real_pa);
600 }
601 async_usleep(UHCI_DEBUGER_TIMEOUT);
602 }
603 return EOK;
604#undef QH
605}
606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.