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

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

Const, type-casting and other minor fixes

  • Property mode set to 100644
File size: 15.6 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 */
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
[c01cd32]44#include "hc.h"
[9351353]45
[8986412]46#define UHCI_INTR_ALLOW_INTERRUPTS \
[af81980]47 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
[8986412]48#define UHCI_STATUS_USED_INTERRUPTS \
49 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
[af81980]50
[302a4b6]51
[c01cd32]52static int hc_init_transfer_lists(hc_t *instance);
53static int hc_init_mem_structures(hc_t *instance);
54static void hc_init_hw(hc_t *instance);
[9351353]55
[c01cd32]56static int hc_interrupt_emulator(void *arg);
57static int hc_debug_checker(void *arg);
[9351353]58/*----------------------------------------------------------------------------*/
[02cacce]59/** Initialize UHCI hc driver structure
[9351353]60 *
61 * @param[in] instance Memory place to initialize.
62 * @param[in] regs Address of I/O control registers.
[23f40280]63 * @param[in] reg_size Size of I/O control registers.
64 * @param[in] interrupts True if hw interrupts should be used.
[9351353]65 * @return Error code.
66 * @note Should be called only once on any structure.
[17ceb72]67 *
68 * Initializes memory structures, starts up hw, and launches debugger and
69 * interrupt fibrils.
[9351353]70 */
[d2bff2f]71int hc_init(hc_t *instance, void *regs, size_t reg_size, bool interrupts)
[9351353]72{
73 assert(reg_size >= sizeof(regs_t));
74 int ret;
75
[ea993d18]76#define CHECK_RET_RETURN(ret, message...) \
[9351353]77 if (ret != EOK) { \
78 usb_log_error(message); \
79 return ret; \
80 } else (void) 0
81
[ff34e5a]82 instance->hw_interrupts = interrupts;
[fcc525d]83 instance->hw_failures = 0;
84
[9351353]85 /* allow access to hc control registers */
86 regs_t *io;
[e247d83]87 ret = pio_enable(regs, reg_size, (void **)&io);
[ea993d18]88 CHECK_RET_RETURN(ret,
[9351353]89 "Failed(%d) to gain access to registers at %p: %s.\n",
[ea993d18]90 ret, io, str_error(ret));
[9351353]91 instance->registers = io;
[4125b7d]92 usb_log_debug("Device registers at %p (%zuB) accessible.\n",
[9351353]93 io, reg_size);
94
[c01cd32]95 ret = hc_init_mem_structures(instance);
[ea993d18]96 CHECK_RET_RETURN(ret,
97 "Failed(%d) to initialize UHCI memory structures: %s.\n",
98 ret, str_error(ret));
[9351353]99
[c01cd32]100 hc_init_hw(instance);
[ff34e5a]101 if (!interrupts) {
[ea993d18]102 instance->interrupt_emulator =
[c01cd32]103 fibril_create(hc_interrupt_emulator, instance);
[ea993d18]104 fibril_add_ready(instance->interrupt_emulator);
[ff34e5a]105 }
[ea993d18]106 (void)hc_debug_checker;
[9351353]107
108 return EOK;
109#undef CHECK_RET_DEST_FUN_RETURN
110}
111/*----------------------------------------------------------------------------*/
[17ceb72]112/** Initialize UHCI hc hw resources.
[9351353]113 *
114 * @param[in] instance UHCI structure to use.
[17ceb72]115 * For magic values see UHCI Design Guide
[9351353]116 */
[c01cd32]117void hc_init_hw(hc_t *instance)
[9351353]118{
119 assert(instance);
120 regs_t *registers = instance->registers;
121
122 /* Reset everything, who knows what touched it before us */
123 pio_write_16(&registers->usbcmd, UHCI_CMD_GLOBAL_RESET);
124 async_usleep(10000); /* 10ms according to USB spec */
125 pio_write_16(&registers->usbcmd, 0);
126
127 /* Reset hc, all states and counters */
128 pio_write_16(&registers->usbcmd, UHCI_CMD_HCRESET);
129 do { async_usleep(10); }
130 while ((pio_read_16(&registers->usbcmd) & UHCI_CMD_HCRESET) != 0);
131
[eb2a48a]132 /* Set frame to exactly 1ms */
133 pio_write_8(&registers->sofmod, 64);
134
135 /* Set frame list pointer */
[9351353]136 const uint32_t pa = addr_to_phys(instance->frame_list);
137 pio_write_32(&registers->flbaseadd, pa);
138
[ff34e5a]139 if (instance->hw_interrupts) {
140 /* Enable all interrupts, but resume interrupt */
141 pio_write_16(&instance->registers->usbintr,
[8986412]142 UHCI_INTR_ALLOW_INTERRUPTS);
[ff34e5a]143 }
[9351353]144
[e247d83]145 const uint16_t status = pio_read_16(&registers->usbcmd);
[9351353]146 if (status != 0)
147 usb_log_warning("Previous command value: %x.\n", status);
148
149 /* Start the hc with large(64B) packet FSBR */
150 pio_write_16(&registers->usbcmd,
151 UHCI_CMD_RUN_STOP | UHCI_CMD_MAX_PACKET | UHCI_CMD_CONFIGURE);
152}
153/*----------------------------------------------------------------------------*/
[17ceb72]154/** Initialize UHCI hc memory structures.
[9351353]155 *
156 * @param[in] instance UHCI structure to use.
157 * @return Error code
158 * @note Should be called only once on any structure.
[17ceb72]159 *
160 * Structures:
161 * - interrupt code (I/O addressses are customized per instance)
162 * - transfer lists (queue heads need to be accessible by the hw)
163 * - frame list page (needs to be one UHCI hw accessible 4K page)
[9351353]164 */
[c01cd32]165int hc_init_mem_structures(hc_t *instance)
[9351353]166{
167 assert(instance);
[77d10f0]168#define CHECK_RET_RETURN(ret, message...) \
[9351353]169 if (ret != EOK) { \
170 usb_log_error(message); \
171 return ret; \
172 } else (void) 0
173
174 /* Init interrupt code */
[77d10f0]175 instance->interrupt_code.cmds = instance->interrupt_commands;
[9351353]176 {
[af81980]177 /* Read status register */
[77d10f0]178 instance->interrupt_commands[0].cmd = CMD_PIO_READ_16;
179 instance->interrupt_commands[0].dstarg = 1;
180 instance->interrupt_commands[0].addr =
181 &instance->registers->usbsts;
182
[af81980]183 /* Test whether we are the interrupt cause */
184 instance->interrupt_commands[1].cmd = CMD_BTEST;
185 instance->interrupt_commands[1].value =
[8986412]186 UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS;
[af81980]187 instance->interrupt_commands[1].srcarg = 1;
188 instance->interrupt_commands[1].dstarg = 2;
189
190 /* Predicate cleaning and accepting */
191 instance->interrupt_commands[2].cmd = CMD_PREDICATE;
192 instance->interrupt_commands[2].value = 2;
193 instance->interrupt_commands[2].srcarg = 2;
194
195 /* Write clean status register */
196 instance->interrupt_commands[3].cmd = CMD_PIO_WRITE_A_16;
197 instance->interrupt_commands[3].srcarg = 1;
198 instance->interrupt_commands[3].addr =
[77d10f0]199 &instance->registers->usbsts;
200
[af81980]201 /* Accept interrupt */
202 instance->interrupt_commands[4].cmd = CMD_ACCEPT;
203
[77d10f0]204 instance->interrupt_code.cmdcount = UHCI_NEEDED_IRQ_COMMANDS;
[9351353]205 }
206
207 /* Init transfer lists */
[77d10f0]208 int ret = hc_init_transfer_lists(instance);
209 CHECK_RET_RETURN(ret, "Failed to init transfer lists.\n");
[9351353]210 usb_log_debug("Initialized transfer lists.\n");
211
212 /* Init USB frame list page*/
213 instance->frame_list = get_page();
[e247d83]214 ret = instance->frame_list ? EOK : ENOMEM;
[77d10f0]215 CHECK_RET_RETURN(ret, "Failed to get frame list page.\n");
[001b152]216 usb_log_debug("Initialized frame list at %p.\n", instance->frame_list);
[9351353]217
218 /* Set all frames to point to the first queue head */
[302a4b6]219 const uint32_t queue = LINK_POINTER_QH(
220 addr_to_phys(instance->transfers_interrupt.queue_head));
[9351353]221
222 unsigned i = 0;
223 for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
224 instance->frame_list[i] = queue;
225 }
226
[4c70554]227 /* Init device keeper */
[62ed5bc]228 usb_device_keeper_init(&instance->manager);
[9351353]229 usb_log_debug("Initialized device manager.\n");
230
[4c28d17]231 ret = usb_endpoint_manager_init(&instance->ep_manager,
232 BANDWIDTH_AVAILABLE_USB11);
[77d10f0]233 CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
234 str_error(ret));
[a1313b8c]235
[9351353]236 return EOK;
[77d10f0]237#undef CHECK_RET_RETURN
[9351353]238}
239/*----------------------------------------------------------------------------*/
[17ceb72]240/** Initialize UHCI hc transfer lists.
[9351353]241 *
242 * @param[in] instance UHCI structure to use.
243 * @return Error code
244 * @note Should be called only once on any structure.
[17ceb72]245 *
246 * Initializes transfer lists and sets them in one chain to support proper
247 * USB scheduling. Sets pointer table for quick access.
[9351353]248 */
[c01cd32]249int hc_init_transfer_lists(hc_t *instance)
[9351353]250{
251 assert(instance);
[27205841]252#define SETUP_TRANSFER_LIST(type, name) \
253do { \
254 int ret = transfer_list_init(&instance->transfers_##type, name); \
[9351353]255 if (ret != EOK) { \
[27205841]256 usb_log_error("Failed(%d) to setup %s transfer list: %s.\n", \
257 ret, name, str_error(ret)); \
[9351353]258 transfer_list_fini(&instance->transfers_bulk_full); \
259 transfer_list_fini(&instance->transfers_control_full); \
260 transfer_list_fini(&instance->transfers_control_slow); \
261 transfer_list_fini(&instance->transfers_interrupt); \
262 return ret; \
[27205841]263 } \
264} while (0)
265
266 SETUP_TRANSFER_LIST(bulk_full, "BULK FULL");
267 SETUP_TRANSFER_LIST(control_full, "CONTROL FULL");
268 SETUP_TRANSFER_LIST(control_slow, "CONTROL LOW");
269 SETUP_TRANSFER_LIST(interrupt, "INTERRUPT");
270#undef SETUP_TRANSFER_LIST
271 /* Connect lists into one schedule */
[9351353]272 transfer_list_set_next(&instance->transfers_control_full,
273 &instance->transfers_bulk_full);
274 transfer_list_set_next(&instance->transfers_control_slow,
275 &instance->transfers_control_full);
276 transfer_list_set_next(&instance->transfers_interrupt,
277 &instance->transfers_control_slow);
278
[e247d83]279 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
280 * buggy on certain hw, enable at your own risk. */
[9351353]281#ifdef FSBR
282 transfer_list_set_next(&instance->transfers_bulk_full,
[302a4b6]283 &instance->transfers_control_full);
[9351353]284#endif
285
286 /* Assign pointers to be used during scheduling */
287 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_INTERRUPT] =
288 &instance->transfers_interrupt;
289 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_INTERRUPT] =
290 &instance->transfers_interrupt;
291 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_CONTROL] =
292 &instance->transfers_control_full;
293 instance->transfers[USB_SPEED_LOW][USB_TRANSFER_CONTROL] =
294 &instance->transfers_control_slow;
295 instance->transfers[USB_SPEED_FULL][USB_TRANSFER_BULK] =
296 &instance->transfers_bulk_full;
297
298 return EOK;
299#undef CHECK_RET_CLEAR_RETURN
300}
301/*----------------------------------------------------------------------------*/
[17ceb72]302/** Schedule batch for execution.
[9351353]303 *
304 * @param[in] instance UHCI structure to use.
305 * @param[in] batch Transfer batch to schedule.
306 * @return Error code
[17ceb72]307 *
308 * Checks for bandwidth availability and appends the batch to the proper queue.
[9351353]309 */
[c01cd32]310int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
[9351353]311{
312 assert(instance);
313 assert(batch);
314
315 transfer_list_t *list =
[d017cea]316 instance->transfers[batch->ep->speed][batch->ep->transfer_type];
[9351353]317 assert(list);
318 transfer_list_add_batch(list, batch);
319
320 return EOK;
321}
322/*----------------------------------------------------------------------------*/
[17ceb72]323/** Take action based on the interrupt cause.
[9351353]324 *
325 * @param[in] instance UHCI structure to use.
[17ceb72]326 * @param[in] status Value of the status register at the time of interrupt.
327 *
328 * Interrupt might indicate:
329 * - transaction completed, either by triggering IOC, SPD, or an error
330 * - some kind of device error
331 * - resume from suspend state (not implemented)
[9351353]332 */
[c01cd32]333void hc_interrupt(hc_t *instance, uint16_t status)
[9351353]334{
335 assert(instance);
336 /* Lower 2 bits are transaction error and transaction complete */
[27205841]337 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
[1585c7e]338 LIST_INITIALIZE(done);
339 transfer_list_remove_finished(
340 &instance->transfers_interrupt, &done);
341 transfer_list_remove_finished(
342 &instance->transfers_control_slow, &done);
343 transfer_list_remove_finished(
344 &instance->transfers_control_full, &done);
345 transfer_list_remove_finished(
346 &instance->transfers_bulk_full, &done);
347
348 while (!list_empty(&done)) {
349 link_t *item = done.next;
350 list_remove(item);
351 usb_transfer_batch_t *batch =
352 list_get_instance(item, usb_transfer_batch_t, link);
[cd1cec3b]353 usb_transfer_batch_finish(batch);
[1585c7e]354 }
[9351353]355 }
[27205841]356 /* Resume interrupts are not supported */
[302a4b6]357 if (status & UHCI_STATUS_RESUME) {
358 usb_log_error("Resume interrupt!\n");
359 }
[27205841]360
361 /* Bits 4 and 5 indicate hc error */
362 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
[fcc525d]363 usb_log_error("UHCI hardware failure!.\n");
364 ++instance->hw_failures;
[a963a68]365 transfer_list_abort_all(&instance->transfers_interrupt);
366 transfer_list_abort_all(&instance->transfers_control_slow);
367 transfer_list_abort_all(&instance->transfers_control_full);
368 transfer_list_abort_all(&instance->transfers_bulk_full);
[fcc525d]369
370 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
371 /* reinitialize hw, this triggers virtual disconnect*/
[c01cd32]372 hc_init_hw(instance);
[fcc525d]373 } else {
374 usb_log_fatal("Too many UHCI hardware failures!.\n");
[c01cd32]375 hc_fini(instance);
[fcc525d]376 }
[a963a68]377 }
[9351353]378}
379/*----------------------------------------------------------------------------*/
380/** Polling function, emulates interrupts.
381 *
[17ceb72]382 * @param[in] arg UHCI hc structure to use.
383 * @return EOK (should never return)
[9351353]384 */
[c01cd32]385int hc_interrupt_emulator(void* arg)
[9351353]386{
387 usb_log_debug("Started interrupt emulator.\n");
[6f122df]388 hc_t *instance = arg;
[9351353]389 assert(instance);
390
391 while (1) {
[6f122df]392 /* Read and clear status register */
[9351353]393 uint16_t status = pio_read_16(&instance->registers->usbsts);
[27205841]394 pio_write_16(&instance->registers->usbsts, status);
[9351353]395 if (status != 0)
396 usb_log_debug2("UHCI status: %x.\n", status);
[6f122df]397// Qemu fails to report stalled communication
398// see https://bugs.launchpad.net/qemu/+bug/757654
399// This is a simple workaround to force queue processing every time
400 // status |= 1;
[c01cd32]401 hc_interrupt(instance, status);
[27205841]402 async_usleep(UHCI_INT_EMULATOR_TIMEOUT);
[9351353]403 }
404 return EOK;
405}
406/*---------------------------------------------------------------------------*/
407/** Debug function, checks consistency of memory structures.
408 *
409 * @param[in] arg UHCI structure to use.
[17ceb72]410 * @return EOK (should never return)
[9351353]411 */
[c01cd32]412int hc_debug_checker(void *arg)
[9351353]413{
[6f122df]414 hc_t *instance = arg;
[9351353]415 assert(instance);
416
417#define QH(queue) \
418 instance->transfers_##queue.queue_head
419
420 while (1) {
421 const uint16_t cmd = pio_read_16(&instance->registers->usbcmd);
422 const uint16_t sts = pio_read_16(&instance->registers->usbsts);
423 const uint16_t intr =
424 pio_read_16(&instance->registers->usbintr);
425
426 if (((cmd & UHCI_CMD_RUN_STOP) != 1) || (sts != 0)) {
427 usb_log_debug2("Command: %X Status: %X Intr: %x\n",
428 cmd, sts, intr);
429 }
430
[e247d83]431 const uintptr_t frame_list =
[9351353]432 pio_read_32(&instance->registers->flbaseadd) & ~0xfff;
433 if (frame_list != addr_to_phys(instance->frame_list)) {
434 usb_log_debug("Framelist address: %p vs. %p.\n",
[4125b7d]435 (void *) frame_list,
436 (void *) addr_to_phys(instance->frame_list));
[9351353]437 }
438
439 int frnum = pio_read_16(&instance->registers->frnum) & 0x3ff;
440
441 uintptr_t expected_pa = instance->frame_list[frnum]
442 & LINK_POINTER_ADDRESS_MASK;
443 uintptr_t real_pa = addr_to_phys(QH(interrupt));
444 if (expected_pa != real_pa) {
[4125b7d]445 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.\n",
446 (void *) expected_pa, frnum, (void *) real_pa);
[9351353]447 }
448
449 expected_pa = QH(interrupt)->next & LINK_POINTER_ADDRESS_MASK;
450 real_pa = addr_to_phys(QH(control_slow));
451 if (expected_pa != real_pa) {
452 usb_log_debug("Control Slow QH: %p vs. %p.\n",
[4125b7d]453 (void *) expected_pa, (void *) real_pa);
[9351353]454 }
455
456 expected_pa = QH(control_slow)->next & LINK_POINTER_ADDRESS_MASK;
457 real_pa = addr_to_phys(QH(control_full));
458 if (expected_pa != real_pa) {
459 usb_log_debug("Control Full QH: %p vs. %p.\n",
[4125b7d]460 (void *) expected_pa, (void *) real_pa);
[9351353]461 }
462
463 expected_pa = QH(control_full)->next & LINK_POINTER_ADDRESS_MASK;
464 real_pa = addr_to_phys(QH(bulk_full));
465 if (expected_pa != real_pa ) {
466 usb_log_debug("Bulk QH: %p vs. %p.\n",
[4125b7d]467 (void *) expected_pa, (void *) real_pa);
[9351353]468 }
469 async_usleep(UHCI_DEBUGER_TIMEOUT);
470 }
471 return EOK;
472#undef QH
473}
474/**
475 * @}
476 */
Note: See TracBrowser for help on using the repository browser.