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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e200736 was 3e200736, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

uhci,ohci, ehci: Move interrupt replacement fibril to libusbhost

  • Property mode set to 100644
File size: 15.2 KB
RevLine 
[9351353]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 */
[17ceb72]28/** @addtogroup drvusbuhcihc
[9351353]29 * @{
30 */
31/** @file
[17ceb72]32 * @brief UHCI Host controller driver routines
[9351353]33 */
[8064c2f6]34
[9351353]35#include <adt/list.h>
[8064c2f6]36#include <assert.h>
37#include <async.h>
[1ae74c6]38#include <ddi.h>
[8064c2f6]39#include <device/hw_res_parsed.h>
40#include <fibril.h>
41#include <errno.h>
42#include <macros.h>
43#include <mem.h>
44#include <stdlib.h>
45#include <str_error.h>
46#include <sys/types.h>
[9351353]47
48#include <usb/debug.h>
49#include <usb/usb.h>
50
[07f49ae]51#include "uhci_batch.h"
[8064c2f6]52#include "utils/malloc32.h"
53#include "hc.h"
[9351353]54
[8986412]55#define UHCI_INTR_ALLOW_INTERRUPTS \
[af81980]56 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
[8986412]57#define UHCI_STATUS_USED_INTERRUPTS \
58 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
[af81980]59
[d57122c]60static const irq_pio_range_t uhci_irq_pio_ranges[] = {
61 {
[8486c07]62 .base = 0,
[d57122c]63 .size = sizeof(uhci_regs_t)
64 }
65};
[5fe0a697]66
[d57122c]67static const irq_cmd_t uhci_irq_commands[] = {
[8486c07]68 {
69 .cmd = CMD_PIO_READ_16,
70 .dstarg = 1,
71 .addr = NULL
72 },
73 {
74 .cmd = CMD_AND,
75 .srcarg = 1,
76 .dstarg = 2,
77 .value = UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS
78 },
79 {
80 .cmd = CMD_PREDICATE,
81 .srcarg = 2,
82 .value = 2
83 },
84 {
85 .cmd = CMD_PIO_WRITE_A_16,
86 .srcarg = 1,
87 .addr = NULL
88 },
89 {
90 .cmd = CMD_ACCEPT
91 }
[dfe4955]92};
[302a4b6]93
[3afb758]94static void hc_init_hw(const hc_t *instance);
[c01cd32]95static int hc_init_mem_structures(hc_t *instance);
[3afb758]96static int hc_init_transfer_lists(hc_t *instance);
[9351353]97
[c01cd32]98static int hc_debug_checker(void *arg);
[dfe4955]99
[76fbd9a]100
[d57122c]101/** Generate IRQ code.
[6210a333]102 * @param[out] code IRQ code structure.
[ba4a03a5]103 * @param[in] hw_res Device's resources.
[dfe4955]104 *
105 * @return Error code.
106 */
[ba4a03a5]107int hc_gen_irq_code(irq_code_t *code, const hw_res_list_parsed_t *hw_res)
[dfe4955]108{
[6210a333]109 assert(code);
[ba4a03a5]110 assert(hw_res);
[6210a333]111
[ba4a03a5]112 if (hw_res->irqs.count != 1 || hw_res->io_ranges.count != 1)
113 return EINVAL;
114 const addr_range_t regs = hw_res->io_ranges.ranges[0];
115
116 if (RNGSZ(regs) < sizeof(uhci_regs_t))
[dfe4955]117 return EOVERFLOW;
118
[6210a333]119 code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
120 if (code->ranges == NULL)
121 return ENOMEM;
122
123 code->cmds = malloc(sizeof(uhci_irq_commands));
124 if (code->cmds == NULL) {
125 free(code->ranges);
126 return ENOMEM;
127 }
128
129 code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
130 code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
[dfe4955]131
[6210a333]132 memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
[ba4a03a5]133 code->ranges[0].base = RNGABS(regs);
[6210a333]134
135 memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
[ba4a03a5]136 uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(regs);
[6210a333]137 code->cmds[0].addr = (void*)&registers->usbsts;
138 code->cmds[3].addr = (void*)&registers->usbsts;
[dfe4955]139
[ba4a03a5]140 usb_log_debug("I/O regs at %p (size %zu), IRQ %d.\n",
141 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
142
143 return hw_res->irqs.irqs[0];
[dfe4955]144}
[76fbd9a]145
[3afb758]146/** Take action based on the interrupt cause.
147 *
148 * @param[in] instance UHCI structure to use.
149 * @param[in] status Value of the status register at the time of interrupt.
150 *
151 * Interrupt might indicate:
152 * - transaction completed, either by triggering IOC, SPD, or an error
153 * - some kind of device error
154 * - resume from suspend state (not implemented)
155 */
156void hc_interrupt(hc_t *instance, uint16_t status)
157{
158 assert(instance);
159 /* Lower 2 bits are transaction error and transaction complete */
160 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
161 LIST_INITIALIZE(done);
162 transfer_list_remove_finished(
163 &instance->transfers_interrupt, &done);
164 transfer_list_remove_finished(
165 &instance->transfers_control_slow, &done);
166 transfer_list_remove_finished(
167 &instance->transfers_control_full, &done);
168 transfer_list_remove_finished(
169 &instance->transfers_bulk_full, &done);
170
[ad5f149]171 list_foreach_safe(done, current, next) {
172 list_remove(current);
[b991d37]173 uhci_transfer_batch_t *batch =
[ad5f149]174 uhci_transfer_batch_from_link(current);
[6bba41d]175 uhci_transfer_batch_finish_dispose(batch);
[3afb758]176 }
177 }
178 /* Resume interrupts are not supported */
179 if (status & UHCI_STATUS_RESUME) {
180 usb_log_error("Resume interrupt!\n");
181 }
182
183 /* Bits 4 and 5 indicate hc error */
184 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
185 usb_log_error("UHCI hardware failure!.\n");
186 ++instance->hw_failures;
187 transfer_list_abort_all(&instance->transfers_interrupt);
188 transfer_list_abort_all(&instance->transfers_control_slow);
189 transfer_list_abort_all(&instance->transfers_control_full);
190 transfer_list_abort_all(&instance->transfers_bulk_full);
191
192 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
193 /* reinitialize hw, this triggers virtual disconnect*/
194 hc_init_hw(instance);
195 } else {
196 usb_log_fatal("Too many UHCI hardware failures!.\n");
197 hc_fini(instance);
198 }
199 }
200}
[76fbd9a]201
[02cacce]202/** Initialize UHCI hc driver structure
[9351353]203 *
204 * @param[in] instance Memory place to initialize.
[7de1988c]205 * @param[in] regs Range of device's I/O control registers.
[23f40280]206 * @param[in] interrupts True if hw interrupts should be used.
[9351353]207 * @return Error code.
208 * @note Should be called only once on any structure.
[17ceb72]209 *
210 * Initializes memory structures, starts up hw, and launches debugger and
211 * interrupt fibrils.
[9351353]212 */
[7813516]213int hc_init(hc_t *instance, const hw_res_list_parsed_t *hw_res, bool interrupts)
[9351353]214{
[3f03199]215 assert(instance);
[7813516]216 assert(hw_res);
217 if (hw_res->io_ranges.count != 1 ||
218 hw_res->io_ranges.ranges[0].size < sizeof(uhci_regs_t))
219 return EINVAL;
[9351353]220
[ff34e5a]221 instance->hw_interrupts = interrupts;
[fcc525d]222 instance->hw_failures = 0;
223
[9351353]224 /* allow access to hc control registers */
[7813516]225 int ret = pio_enable_range(&hw_res->io_ranges.ranges[0],
226 (void **) &instance->registers);
[e0d8b740]227 if (ret != EOK) {
[7813516]228 usb_log_error("Failed to gain access to registers: %s.\n",
229 str_error(ret));
[e0d8b740]230 return ret;
231 }
232
[7813516]233 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.\n",
234 hw_res->io_ranges.ranges[0].address.absolute,
235 hw_res->io_ranges.ranges[0].size);
[3afb758]236
[7265558]237 ret = hc_init_mem_structures(instance);
[e0d8b740]238 if (ret != EOK) {
239 usb_log_error("Failed to init UHCI memory structures: %s.\n",
240 str_error(ret));
241 // TODO: we should disable pio here
242 return ret;
243 }
[7265558]244
[c01cd32]245 hc_init_hw(instance);
[ea993d18]246 (void)hc_debug_checker;
[9351353]247
[e646c61]248 uhci_rh_init(&instance->rh, instance->registers->ports, "uhci");
[c95c00e]249
[9351353]250 return EOK;
251}
[76fbd9a]252
[7813516]253/** Safely dispose host controller internal structures
254 *
255 * @param[in] instance Host controller structure to use.
256 */
257void hc_fini(hc_t *instance)
258{
259 assert(instance);
260 //TODO Implement
261}
262
[17ceb72]263/** Initialize UHCI hc hw resources.
[9351353]264 *
265 * @param[in] instance UHCI structure to use.
[17ceb72]266 * For magic values see UHCI Design Guide
[9351353]267 */
[3afb758]268void hc_init_hw(const hc_t *instance)
[9351353]269{
270 assert(instance);
[dfe4955]271 uhci_regs_t *registers = instance->registers;
[9351353]272
273 /* Reset everything, who knows what touched it before us */
274 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
[26858040]275 async_usleep(50000); /* 50ms according to USB spec(root hub reset) */
[9351353]276 pio_write_16(&registers->usbcmd, 0);
277
[26858040]278 /* Reset hc, all states and counters. Hope that hw is not broken */
[9351353]279 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
280 do { async_usleep(10); }
281 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
282
[eb2a48a]283 /* Set frame to exactly 1ms */
284 pio_write_8(&registers->sofmod, 64);
285
286 /* Set frame list pointer */
[9351353]287 const uint32_t pa = addr_to_phys(instance->frame_list);
288 pio_write_32(&registers->flbaseadd, pa);
289
[ff34e5a]290 if (instance->hw_interrupts) {
291 /* Enable all interrupts, but resume interrupt */
292 pio_write_16(&instance->registers->usbintr,
[8986412]293 UHCI_INTR_ALLOW_INTERRUPTS);
[ff34e5a]294 }
[9351353]295
[26858040]296 const uint16_t cmd = pio_read_16(&registers->usbcmd);
297 if (cmd != 0)
298 usb_log_warning("Previous command value: %x.\n", cmd);
[9351353]299
300 /* Start the hc with large(64B) packet FSBR */
301 pio_write_16(&registers->usbcmd,
302 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
303}
[76fbd9a]304
[17ceb72]305/** Initialize UHCI hc memory structures.
[9351353]306 *
307 * @param[in] instance UHCI structure to use.
308 * @return Error code
309 * @note Should be called only once on any structure.
[17ceb72]310 *
311 * Structures:
312 * - transfer lists (queue heads need to be accessible by the hw)
313 * - frame list page (needs to be one UHCI hw accessible 4K page)
[9351353]314 */
[c01cd32]315int hc_init_mem_structures(hc_t *instance)
[9351353]316{
317 assert(instance);
318
[3afb758]319 /* Init USB frame list page */
[9351353]320 instance->frame_list = get_page();
[26858040]321 if (!instance->frame_list) {
322 return ENOMEM;
323 }
[001b152]324 usb_log_debug("Initialized frame list at %p.\n", instance->frame_list);
[9351353]325
[3afb758]326 /* Init transfer lists */
327 int ret = hc_init_transfer_lists(instance);
328 if (ret != EOK) {
329 usb_log_error("Failed to initialize transfer lists.\n");
330 return_page(instance->frame_list);
331 return ENOMEM;
332 }
333 usb_log_debug("Initialized transfer lists.\n");
334
335
[9351353]336 /* Set all frames to point to the first queue head */
[302a4b6]337 const uint32_t queue = LINK_POINTER_QH(
338 addr_to_phys(instance->transfers_interrupt.queue_head));
[75f9dcd]339
340 for (unsigned i = 0; i < UHCI_FRAME_LIST_COUNT; ++i) {
[9351353]341 instance->frame_list[i] = queue;
342 }
343
344 return EOK;
345}
[76fbd9a]346
[17ceb72]347/** Initialize UHCI hc transfer lists.
[9351353]348 *
349 * @param[in] instance UHCI structure to use.
350 * @return Error code
351 * @note Should be called only once on any structure.
[17ceb72]352 *
353 * Initializes transfer lists and sets them in one chain to support proper
354 * USB scheduling. Sets pointer table for quick access.
[9351353]355 */
[c01cd32]356int hc_init_transfer_lists(hc_t *instance)
[9351353]357{
358 assert(instance);
[27205841]359#define SETUP_TRANSFER_LIST(type, name) \
360do { \
361 int ret = transfer_list_init(&instance->transfers_##type, name); \
[9351353]362 if (ret != EOK) { \
[26858040]363 usb_log_error("Failed to setup %s transfer list: %s.\n", \
364 name, str_error(ret)); \
[9351353]365 transfer_list_fini(&instance->transfers_bulk_full); \
366 transfer_list_fini(&instance->transfers_control_full); \
367 transfer_list_fini(&instance->transfers_control_slow); \
368 transfer_list_fini(&instance->transfers_interrupt); \
369 return ret; \
[27205841]370 } \
371} while (0)
372
373 SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
374 SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
375 SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
376 SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
377#undef SETUP_TRANSFER_LIST
378 /* Connect lists into one schedule */
[9351353]379 transfer_list_set_next(&instance->transfers_control_full,
380 &instance->transfers_bulk_full);
381 transfer_list_set_next(&instance->transfers_control_slow,
382 &instance->transfers_control_full);
383 transfer_list_set_next(&instance->transfers_interrupt,
384 &instance->transfers_control_slow);
385
[e247d83]386 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
387 * buggy on certain hw, enable at your own risk. */
[9351353]388#ifdef FSBR
389 transfer_list_set_next(&instance->transfers_bulk_full,
[302a4b6]390 &instance->transfers_control_full);
[9351353]391#endif
392
393 /* Assign pointers to be used during scheduling */
394 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
395 &instance->transfers_interrupt;
396 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
397 &instance->transfers_interrupt;
398 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
399 &instance->transfers_control_full;
400 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
401 &instance->transfers_control_slow;
402 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
403 &instance->transfers_bulk_full;
404
405 return EOK;
406}
[76fbd9a]407
[e26a9d95]408int hc_status(hcd_t *hcd, uint32_t *status)
409{
410 assert(hcd);
411 assert(status);
412 hc_t *instance = hcd->driver.data;
413 assert(instance);
414
415 *status = 0;
416 if (instance->registers) {
417 uint16_t s = pio_read_16(&instance->registers->usbsts);
418 pio_write_16(&instance->registers->usbsts, s);
419 *status = s;
420 }
421 return EOK;
422}
423
[17ceb72]424/** Schedule batch for execution.
[9351353]425 *
426 * @param[in] instance UHCI structure to use.
427 * @param[in] batch Transfer batch to schedule.
428 * @return Error code
[17ceb72]429 *
430 * Checks for bandwidth availability and appends the batch to the proper queue.
[9351353]431 */
[3afb758]432int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
[9351353]433{
[3afb758]434 assert(hcd);
[9348862]435 hc_t *instance = hcd->driver.data;
[9351353]436 assert(instance);
437 assert(batch);
[c95c00e]438
[3848fec]439 if (batch->ep->address == uhci_rh_get_address(&instance->rh))
440 return uhci_rh_schedule(&instance->rh, batch);
[c95c00e]441
[b991d37]442 uhci_transfer_batch_t *uhci_batch = uhci_transfer_batch_get(batch);
443 if (!uhci_batch) {
444 usb_log_error("Failed to create UHCI transfer structures.\n");
445 return ENOMEM;
[23b0fe8]446 }
[9351353]447
448 transfer_list_t *list =
[d017cea]449 instance->transfers[batch->ep->speed][batch->ep->transfer_type];
[9351353]450 assert(list);
[b991d37]451 transfer_list_add_batch(list, uhci_batch);
[9351353]452
453 return EOK;
454}
[76fbd9a]455
[9351353]456/** Debug function, checks consistency of memory structures.
457 *
458 * @param[in] arg UHCI structure to use.
[17ceb72]459 * @return EOK (should never return)
[9351353]460 */
[c01cd32]461int hc_debug_checker(void *arg)
[9351353]462{
[6f122df]463 hc_t *instance = arg;
[9351353]464 assert(instance);
465
466#define QH(queue) \
467 instance->transfers_##queue.queue_head
468
469 while (1) {
470 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
471 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
472 const uint16_t intr =
473 pio_read_16(&instance->registers->usbintr);
474
475 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
476 usb_log_debug2("Command: %X Status: %X Intr: %x\n",
477 cmd, sts, intr);
478 }
479
[e247d83]480 const uintptr_t frame_list =
[9351353]481 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
482 if (frame_list != addr_to_phys(instance->frame_list)) {
483 usb_log_debug("Framelist address: %p vs. %p.\n",
[4125b7d]484 (void *) frame_list,
485 (void *) addr_to_phys(instance->frame_list));
[9351353]486 }
487
488 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
489
490 uintptr_t expected_pa = instance->frame_list[frnum]
491 & LINK_POINTER_ADDRESS_MASK;
492 uintptr_t real_pa = addr_to_phys(QH(interrupt));
493 if (expected_pa != real_pa) {
[4125b7d]494 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.\n",
495 (void *) expected_pa, frnum, (void *) real_pa);
[9351353]496 }
497
498 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
499 real_pa = addr_to_phys(QH(control_slow));
500 if (expected_pa != real_pa) {
501 usb_log_debug("Control Slow QH: %p vs. %p.\n",
[4125b7d]502 (void *) expected_pa, (void *) real_pa);
[9351353]503 }
504
505 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
506 real_pa = addr_to_phys(QH(control_full));
507 if (expected_pa != real_pa) {
508 usb_log_debug("Control Full QH: %p vs. %p.\n",
[4125b7d]509 (void *) expected_pa, (void *) real_pa);
[9351353]510 }
511
512 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
513 real_pa = addr_to_phys(QH(bulk_full));
514 if (expected_pa != real_pa ) {
515 usb_log_debug("Bulk QH: %p vs. %p.\n",
[4125b7d]516 (void *) expected_pa, (void *) real_pa);
[9351353]517 }
518 async_usleep(UHCI_DEBUGER_TIMEOUT);
519 }
520 return EOK;
521#undef QH
522}
523/**
524 * @}
525 */
Note: See TracBrowser for help on using the repository browser.