source: mainline/uspace/drv/uhci-hcd/hc.c@ fd3940c1

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

Add interrupt bandwidth checks

  • Property mode set to 100644
File size: 16.8 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/** @addtogroup drvusbuhcihc
29 * @{
30 */
31/** @file
32 * @brief UHCI Host controller driver routines
33 */
34#include <errno.h>
35#include <str_error.h>
36#include <adt/list.h>
37#include <libarch/ddi.h>
38
39#include <usb/debug.h>
40#include <usb/usb.h>
41#include <usb/ddfiface.h>
42#include <usb_iface.h>
43
44#include "hc.h"
45
46static irq_cmd_t uhci_cmds[] = {
47 {
48 .cmd = CMD_PIO_READ_16,
49 .addr = NULL, /* patched for every instance */
50 .dstarg = 1
51 },
52 {
53 .cmd = CMD_PIO_WRITE_16,
54 .addr = NULL, /* pathed for every instance */
55 .value = 0x1f
56 },
57 {
58 .cmd = CMD_ACCEPT
59 }
60};
61/*----------------------------------------------------------------------------*/
62static int hc_init_transfer_lists(hc_t *instance);
63static int hc_init_mem_structures(hc_t *instance);
64static void hc_init_hw(hc_t *instance);
65
66static int hc_interrupt_emulator(void *arg);
67static int hc_debug_checker(void *arg);
68#if 0
69static bool usb_is_allowed(
70 bool low_speed, usb_transfer_type_t transfer, size_t size);
71#endif
72/*----------------------------------------------------------------------------*/
73/** Initialize UHCI hcd driver structure
74 *
75 * @param[in] instance Memory place to initialize.
76 * @param[in] fun DDF function.
77 * @param[in] regs Address of I/O control registers.
78 * @param[in] size Size of I/O control registers.
79 * @return Error code.
80 * @note Should be called only once on any structure.
81 *
82 * Initializes memory structures, starts up hw, and launches debugger and
83 * interrupt fibrils.
84 */
85int hc_init(hc_t *instance, ddf_fun_t *fun,
86 void *regs, size_t reg_size, bool interrupts)
87{
88 assert(reg_size >= sizeof(regs_t));
89 int ret;
90
91#define CHECK_RET_DEST_FUN_RETURN(ret, message...) \
92 if (ret != EOK) { \
93 usb_log_error(message); \
94 if (instance->ddf_instance) \
95 ddf_fun_destroy(instance->ddf_instance); \
96 return ret; \
97 } else (void) 0
98
99 instance->hw_interrupts = interrupts;
100 instance->hw_failures = 0;
101
102 /* Setup UHCI function. */
103 instance->ddf_instance = fun;
104
105 /* allow access to hc control registers */
106 regs_t *io;
107 ret = pio_enable(regs, reg_size, (void**)&io);
108 CHECK_RET_DEST_FUN_RETURN(ret,
109 "Failed(%d) to gain access to registers at %p: %s.\n",
110 ret, str_error(ret), io);
111 instance->registers = io;
112 usb_log_debug("Device registers at %p(%u) accessible.\n",
113 io, reg_size);
114
115 ret = hc_init_mem_structures(instance);
116 CHECK_RET_DEST_FUN_RETURN(ret,
117 "Failed to initialize UHCI memory structures.\n");
118
119 hc_init_hw(instance);
120 if (!interrupts) {
121 instance->cleaner =
122 fibril_create(hc_interrupt_emulator, instance);
123 fibril_add_ready(instance->cleaner);
124 } else {
125 /* TODO: enable interrupts here */
126 }
127
128 instance->debug_checker =
129 fibril_create(hc_debug_checker, instance);
130// fibril_add_ready(instance->debug_checker);
131
132 return EOK;
133#undef CHECK_RET_DEST_FUN_RETURN
134}
135/*----------------------------------------------------------------------------*/
136/** Initialize UHCI hc hw resources.
137 *
138 * @param[in] instance UHCI structure to use.
139 * For magic values see UHCI Design Guide
140 */
141void hc_init_hw(hc_t *instance)
142{
143 assert(instance);
144 regs_t *registers = instance->registers;
145
146 /* Reset everything, who knows what touched it before us */
147 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
148 async_usleep(10000); /* 10ms according to USB spec */
149 pio_write_16(&registers->usbcmd, 0);
150
151 /* Reset hc, all states and counters */
152 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
153 do { async_usleep(10); }
154 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
155
156 /* Set frame to exactly 1ms */
157 pio_write_8(&registers->sofmod, 64);
158
159 /* Set frame list pointer */
160 const uint32_t pa = addr_to_phys(instance->frame_list);
161 pio_write_32(&registers->flbaseadd, pa);
162
163 if (instance->hw_interrupts) {
164 /* Enable all interrupts, but resume interrupt */
165 pio_write_16(&instance->registers->usbintr,
166 UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET);
167 }
168
169 uint16_t status = pio_read_16(&registers->usbcmd);
170 if (status != 0)
171 usb_log_warning("Previous command value: %x.\n", status);
172
173 /* Start the hc with large(64B) packet FSBR */
174 pio_write_16(&registers->usbcmd,
175 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
176}
177/*----------------------------------------------------------------------------*/
178/** Initialize UHCI hc memory structures.
179 *
180 * @param[in] instance UHCI structure to use.
181 * @return Error code
182 * @note Should be called only once on any structure.
183 *
184 * Structures:
185 * - interrupt code (I/O addressses are customized per instance)
186 * - transfer lists (queue heads need to be accessible by the hw)
187 * - frame list page (needs to be one UHCI hw accessible 4K page)
188 */
189int hc_init_mem_structures(hc_t *instance)
190{
191 assert(instance);
192#define CHECK_RET_DEST_CMDS_RETURN(ret, message...) \
193 if (ret != EOK) { \
194 usb_log_error(message); \
195 if (instance->interrupt_code.cmds != NULL) \
196 free(instance->interrupt_code.cmds); \
197 return ret; \
198 } else (void) 0
199
200 /* Init interrupt code */
201 instance->interrupt_code.cmds = malloc(sizeof(uhci_cmds));
202 int ret = (instance->interrupt_code.cmds == NULL) ? ENOMEM : EOK;
203 CHECK_RET_DEST_CMDS_RETURN(ret,
204 "Failed to allocate interrupt cmds space.\n");
205
206 {
207 irq_cmd_t *interrupt_commands = instance->interrupt_code.cmds;
208 memcpy(interrupt_commands, uhci_cmds, sizeof(uhci_cmds));
209 interrupt_commands[0].addr =
210 (void*)&instance->registers->usbsts;
211 interrupt_commands[1].addr =
212 (void*)&instance->registers->usbsts;
213 instance->interrupt_code.cmdcount =
214 sizeof(uhci_cmds) / sizeof(irq_cmd_t);
215 }
216
217 /* Init transfer lists */
218 ret = hc_init_transfer_lists(instance);
219 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to init transfer lists.\n");
220 usb_log_debug("Initialized transfer lists.\n");
221
222 /* Init USB frame list page*/
223 instance->frame_list = get_page();
224 ret = instance ? EOK : ENOMEM;
225 CHECK_RET_DEST_CMDS_RETURN(ret, "Failed to get frame list page.\n");
226 usb_log_debug("Initialized frame list at %p.\n", instance->frame_list);
227
228 /* Set all frames to point to the first queue head */
229 const uint32_t queue =
230 instance->transfers_interrupt.queue_head_pa
231 | LINK_POINTER_QUEUE_HEAD_FLAG;
232
233 unsigned i = 0;
234 for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
235 instance->frame_list[i] = queue;
236 }
237
238 /* Init device keeper*/
239 usb_device_keeper_init(&instance->manager);
240 usb_log_debug("Initialized device manager.\n");
241
242 ret =
243 usb_endpoint_manager_init(&instance->ep_manager,
244 BANDWIDTH_AVAILABLE_USB11);
245 assert(ret == EOK);
246
247 return EOK;
248#undef CHECK_RET_DEST_CMDS_RETURN
249}
250/*----------------------------------------------------------------------------*/
251/** Initialize UHCI hc transfer lists.
252 *
253 * @param[in] instance UHCI structure to use.
254 * @return Error code
255 * @note Should be called only once on any structure.
256 *
257 * Initializes transfer lists and sets them in one chain to support proper
258 * USB scheduling. Sets pointer table for quick access.
259 */
260int hc_init_transfer_lists(hc_t *instance)
261{
262 assert(instance);
263#define CHECK_RET_CLEAR_RETURN(ret, message...) \
264 if (ret != EOK) { \
265 usb_log_error(message); \
266 transfer_list_fini(&instance->transfers_bulk_full); \
267 transfer_list_fini(&instance->transfers_control_full); \
268 transfer_list_fini(&instance->transfers_control_slow); \
269 transfer_list_fini(&instance->transfers_interrupt); \
270 return ret; \
271 } else (void) 0
272
273 /* initialize TODO: check errors */
274 int ret;
275 ret = transfer_list_init(&instance->transfers_bulk_full, "BULK_FULL");
276 CHECK_RET_CLEAR_RETURN(ret, "Failed to init BULK list.");
277
278 ret = transfer_list_init(
279 &instance->transfers_control_full, "CONTROL_FULL");
280 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL FULL list.");
281
282 ret = transfer_list_init(
283 &instance->transfers_control_slow, "CONTROL_SLOW");
284 CHECK_RET_CLEAR_RETURN(ret, "Failed to init CONTROL SLOW list.");
285
286 ret = transfer_list_init(&instance->transfers_interrupt, "INTERRUPT");
287 CHECK_RET_CLEAR_RETURN(ret, "Failed to init INTERRUPT list.");
288
289 transfer_list_set_next(&instance->transfers_control_full,
290 &instance->transfers_bulk_full);
291 transfer_list_set_next(&instance->transfers_control_slow,
292 &instance->transfers_control_full);
293 transfer_list_set_next(&instance->transfers_interrupt,
294 &instance->transfers_control_slow);
295
296 /*FSBR*/
297#ifdef FSBR
298 transfer_list_set_next(&instance->transfers_bulk_full,
299 &instance->transfers_control_full);
300#endif
301
302 /* Assign pointers to be used during scheduling */
303 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
304 &instance->transfers_interrupt;
305 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
306 &instance->transfers_interrupt;
307 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
308 &instance->transfers_control_full;
309 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
310 &instance->transfers_control_slow;
311 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
312 &instance->transfers_bulk_full;
313
314 return EOK;
315#undef CHECK_RET_CLEAR_RETURN
316}
317/*----------------------------------------------------------------------------*/
318/** Schedule batch for execution.
319 *
320 * @param[in] instance UHCI structure to use.
321 * @param[in] batch Transfer batch to schedule.
322 * @return Error code
323 *
324 * Checks for bandwidth availability and appends the batch to the proper queue.
325 */
326int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
327{
328 assert(instance);
329 assert(batch);
330
331 transfer_list_t *list =
332 instance->transfers[batch->speed][batch->transfer_type];
333 assert(list);
334 if (batch->transfer_type == USB_TRANSFER_CONTROL) {
335 usb_device_keeper_use_control(
336 &instance->manager, batch->target);
337 }
338 transfer_list_add_batch(list, batch);
339
340 return EOK;
341}
342/*----------------------------------------------------------------------------*/
343/** Take action based on the interrupt cause.
344 *
345 * @param[in] instance UHCI structure to use.
346 * @param[in] status Value of the status register at the time of interrupt.
347 *
348 * Interrupt might indicate:
349 * - transaction completed, either by triggering IOC, SPD, or an error
350 * - some kind of device error
351 * - resume from suspend state (not implemented)
352 */
353void hc_interrupt(hc_t *instance, uint16_t status)
354{
355 assert(instance);
356// status |= 1; //Uncomment to work around qemu hang
357 /* TODO: Resume interrupts are not supported */
358 /* Lower 2 bits are transaction error and transaction complete */
359 if (status & 0x3) {
360 LIST_INITIALIZE(done);
361 transfer_list_remove_finished(
362 &instance->transfers_interrupt, &done);
363 transfer_list_remove_finished(
364 &instance->transfers_control_slow, &done);
365 transfer_list_remove_finished(
366 &instance->transfers_control_full, &done);
367 transfer_list_remove_finished(
368 &instance->transfers_bulk_full, &done);
369
370 while (!list_empty(&done)) {
371 link_t *item = done.next;
372 list_remove(item);
373 usb_transfer_batch_t *batch =
374 list_get_instance(item, usb_transfer_batch_t, link);
375 switch (batch->transfer_type)
376 {
377 case USB_TRANSFER_CONTROL:
378 usb_device_keeper_release_control(
379 &instance->manager, batch->target);
380 break;
381 case USB_TRANSFER_INTERRUPT:
382 case USB_TRANSFER_ISOCHRONOUS: {
383/*
384 int ret = bandwidth_free(&instance->bandwidth,
385 batch->target.address,
386 batch->target.endpoint,
387 batch->direction);
388 if (ret != EOK)
389 usb_log_warning("Failed(%d) to free "
390 "reserved bw: %s.\n", ret,
391 str_error(ret));
392*/
393 }
394 default:
395 break;
396 }
397 batch->next_step(batch);
398 }
399 }
400 /* bits 4 and 5 indicate hc error */
401 if (status & 0x18) {
402 usb_log_error("UHCI hardware failure!.\n");
403 ++instance->hw_failures;
404 transfer_list_abort_all(&instance->transfers_interrupt);
405 transfer_list_abort_all(&instance->transfers_control_slow);
406 transfer_list_abort_all(&instance->transfers_control_full);
407 transfer_list_abort_all(&instance->transfers_bulk_full);
408
409 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
410 /* reinitialize hw, this triggers virtual disconnect*/
411 hc_init_hw(instance);
412 } else {
413 usb_log_fatal("Too many UHCI hardware failures!.\n");
414 hc_fini(instance);
415 }
416 }
417}
418/*----------------------------------------------------------------------------*/
419/** Polling function, emulates interrupts.
420 *
421 * @param[in] arg UHCI hc structure to use.
422 * @return EOK (should never return)
423 */
424int hc_interrupt_emulator(void* arg)
425{
426 usb_log_debug("Started interrupt emulator.\n");
427 hc_t *instance = (hc_t*)arg;
428 assert(instance);
429
430 while (1) {
431 /* read and ack interrupts */
432 uint16_t status = pio_read_16(&instance->registers->usbsts);
433 pio_write_16(&instance->registers->usbsts, 0x1f);
434 if (status != 0)
435 usb_log_debug2("UHCI status: %x.\n", status);
436 hc_interrupt(instance, status);
437 async_usleep(UHCI_CLEANER_TIMEOUT);
438 }
439 return EOK;
440}
441/*---------------------------------------------------------------------------*/
442/** Debug function, checks consistency of memory structures.
443 *
444 * @param[in] arg UHCI structure to use.
445 * @return EOK (should never return)
446 */
447int hc_debug_checker(void *arg)
448{
449 hc_t *instance = (hc_t*)arg;
450 assert(instance);
451
452#define QH(queue) \
453 instance->transfers_##queue.queue_head
454
455 while (1) {
456 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
457 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
458 const uint16_t intr =
459 pio_read_16(&instance->registers->usbintr);
460
461 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
462 usb_log_debug2("Command: %X Status: %X Intr: %x\n",
463 cmd, sts, intr);
464 }
465
466 uintptr_t frame_list =
467 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
468 if (frame_list != addr_to_phys(instance->frame_list)) {
469 usb_log_debug("Framelist address: %p vs. %p.\n",
470 frame_list, addr_to_phys(instance->frame_list));
471 }
472
473 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
474
475 uintptr_t expected_pa = instance->frame_list[frnum]
476 & LINK_POINTER_ADDRESS_MASK;
477 uintptr_t real_pa = addr_to_phys(QH(interrupt));
478 if (expected_pa != real_pa) {
479 usb_log_debug("Interrupt QH: %p(frame: %d) vs. %p.\n",
480 expected_pa, frnum, real_pa);
481 }
482
483 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
484 real_pa = addr_to_phys(QH(control_slow));
485 if (expected_pa != real_pa) {
486 usb_log_debug("Control Slow QH: %p vs. %p.\n",
487 expected_pa, real_pa);
488 }
489
490 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
491 real_pa = addr_to_phys(QH(control_full));
492 if (expected_pa != real_pa) {
493 usb_log_debug("Control Full QH: %p vs. %p.\n",
494 expected_pa, real_pa);
495 }
496
497 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
498 real_pa = addr_to_phys(QH(bulk_full));
499 if (expected_pa != real_pa ) {
500 usb_log_debug("Bulk QH: %p vs. %p.\n",
501 expected_pa, real_pa);
502 }
503 async_usleep(UHCI_DEBUGER_TIMEOUT);
504 }
505 return EOK;
506#undef QH
507}
508/*----------------------------------------------------------------------------*/
509/** Check transfers for USB validity
510 *
511 * @param[in] low_speed Transfer speed.
512 * @param[in] transfer Transer type
513 * @param[in] size Size of data packets
514 * @return True if transaction is allowed by USB specs, false otherwise
515 */
516#if 0
517bool usb_is_allowed(
518 bool low_speed, usb_transfer_type_t transfer, size_t size)
519{
520 /* see USB specification chapter 5.5-5.8 for magic numbers used here */
521 switch(transfer)
522 {
523 case USB_TRANSFER_ISOCHRONOUS:
524 return (!low_speed && size < 1024);
525 case USB_TRANSFER_INTERRUPT:
526 return size <= (low_speed ? 8 : 64);
527 case USB_TRANSFER_CONTROL: /* device specifies its own max size */
528 return (size <= (low_speed ? 8 : 64));
529 case USB_TRANSFER_BULK: /* device specifies its own max size */
530 return (!low_speed && size <= 64);
531 }
532 return false;
533}
534#endif
535/**
536 * @}
537 */
Note: See TracBrowser for help on using the repository browser.