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
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
46#define UHCI_INTR_ALLOW_INTERRUPTS \
47 (UHCI_INTR_CRC | UHCI_INTR_COMPLETE | UHCI_INTR_SHORT_PACKET)
48#define UHCI_STATUS_USED_INTERRUPTS \
49 (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)
50
51
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);
55
56static int hc_interrupt_emulator(void *arg);
57static int hc_debug_checker(void *arg);
58/*----------------------------------------------------------------------------*/
59/** Initialize UHCI hc driver structure
60 *
61 * @param[in] instance Memory place to initialize.
62 * @param[in] regs Address of I/O control registers.
63 * @param[in] reg_size Size of I/O control registers.
64 * @param[in] interrupts True if hw interrupts should be used.
65 * @return Error code.
66 * @note Should be called only once on any structure.
67 *
68 * Initializes memory structures, starts up hw, and launches debugger and
69 * interrupt fibrils.
70 */
71int hc_init(hc_t *instance, void *regs, size_t reg_size, bool interrupts)
72{
73 assert(reg_size >= sizeof(regs_t));
74 int ret;
75
76#define CHECK_RET_RETURN(ret, message...) \
77 if (ret != EOK) { \
78 usb_log_error(message); \
79 return ret; \
80 } else (void) 0
81
82 instance->hw_interrupts = interrupts;
83 instance->hw_failures = 0;
84
85 /* allow access to hc control registers */
86 regs_t *io;
87 ret = pio_enable(regs, reg_size, (void **)&io);
88 CHECK_RET_RETURN(ret,
89 "Failed(%d) to gain access to registers at %p: %s.\n",
90 ret, io, str_error(ret));
91 instance->registers = io;
92 usb_log_debug("Device registers at %p (%zuB) accessible.\n",
93 io, reg_size);
94
95 ret = hc_init_mem_structures(instance);
96 CHECK_RET_RETURN(ret,
97 "Failed(%d) to initialize UHCI memory structures: %s.\n",
98 ret, str_error(ret));
99
100 hc_init_hw(instance);
101 if (!interrupts) {
102 instance->interrupt_emulator =
103 fibril_create(hc_interrupt_emulator, instance);
104 fibril_add_ready(instance->interrupt_emulator);
105 }
106 (void)hc_debug_checker;
107
108 return EOK;
109#undef CHECK_RET_DEST_FUN_RETURN
110}
111/*----------------------------------------------------------------------------*/
112/** Initialize UHCI hc hw resources.
113 *
114 * @param[in] instance UHCI structure to use.
115 * For magic values see UHCI Design Guide
116 */
117void hc_init_hw(hc_t *instance)
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
132 /* Set frame to exactly 1ms */
133 pio_write_8(&registers->sofmod, 64);
134
135 /* Set frame list pointer */
136 const uint32_t pa = addr_to_phys(instance->frame_list);
137 pio_write_32(&registers->flbaseadd, pa);
138
139 if (instance->hw_interrupts) {
140 /* Enable all interrupts, but resume interrupt */
141 pio_write_16(&instance->registers->usbintr,
142 UHCI_INTR_ALLOW_INTERRUPTS);
143 }
144
145 const uint16_t status = pio_read_16(&registers->usbcmd);
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/*----------------------------------------------------------------------------*/
154/** Initialize UHCI hc memory structures.
155 *
156 * @param[in] instance UHCI structure to use.
157 * @return Error code
158 * @note Should be called only once on any structure.
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)
164 */
165int hc_init_mem_structures(hc_t *instance)
166{
167 assert(instance);
168#define CHECK_RET_RETURN(ret, message...) \
169 if (ret != EOK) { \
170 usb_log_error(message); \
171 return ret; \
172 } else (void) 0
173
174 /* Init interrupt code */
175 instance->interrupt_code.cmds = instance->interrupt_commands;
176 {
177 /* Read status register */
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
183 /* Test whether we are the interrupt cause */
184 instance->interrupt_commands[1].cmd = CMD_BTEST;
185 instance->interrupt_commands[1].value =
186 UHCI_STATUS_USED_INTERRUPTS | UHCI_STATUS_NM_INTERRUPTS;
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 =
199 &instance->registers->usbsts;
200
201 /* Accept interrupt */
202 instance->interrupt_commands[4].cmd = CMD_ACCEPT;
203
204 instance->interrupt_code.cmdcount = UHCI_NEEDED_IRQ_COMMANDS;
205 }
206
207 /* Init transfer lists */
208 int ret = hc_init_transfer_lists(instance);
209 CHECK_RET_RETURN(ret, "Failed to init transfer lists.\n");
210 usb_log_debug("Initialized transfer lists.\n");
211
212 /* Init USB frame list page*/
213 instance->frame_list = get_page();
214 ret = instance->frame_list ? EOK : ENOMEM;
215 CHECK_RET_RETURN(ret, "Failed to get frame list page.\n");
216 usb_log_debug("Initialized frame list at %p.\n", instance->frame_list);
217
218 /* Set all frames to point to the first queue head */
219 const uint32_t queue = LINK_POINTER_QH(
220 addr_to_phys(instance->transfers_interrupt.queue_head));
221
222 unsigned i = 0;
223 for(; i < UHCI_FRAME_LIST_COUNT; ++i) {
224 instance->frame_list[i] = queue;
225 }
226
227 /* Init device keeper */
228 usb_device_keeper_init(&instance->manager);
229 usb_log_debug("Initialized device manager.\n");
230
231 ret = usb_endpoint_manager_init(&instance->ep_manager,
232 BANDWIDTH_AVAILABLE_USB11);
233 CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
234 str_error(ret));
235
236 return EOK;
237#undef CHECK_RET_RETURN
238}
239/*----------------------------------------------------------------------------*/
240/** Initialize UHCI hc transfer lists.
241 *
242 * @param[in] instance UHCI structure to use.
243 * @return Error code
244 * @note Should be called only once on any structure.
245 *
246 * Initializes transfer lists and sets them in one chain to support proper
247 * USB scheduling. Sets pointer table for quick access.
248 */
249int hc_init_transfer_lists(hc_t *instance)
250{
251 assert(instance);
252#define SETUP_TRANSFER_LIST(type, name) \
253do { \
254 int ret = transfer_list_init(&instance->transfers_##type, name); \
255 if (ret != EOK) { \
256 usb_log_error("Failed(%d) to setup %s transfer list: %s.\n", \
257 ret, name, str_error(ret)); \
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; \
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 */
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
279 /*FSBR, This feature is not needed (adds no benefit) and is supposedly
280 * buggy on certain hw, enable at your own risk. */
281#ifdef FSBR
282 transfer_list_set_next(&instance->transfers_bulk_full,
283 &instance->transfers_control_full);
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/*----------------------------------------------------------------------------*/
302/** Schedule batch for execution.
303 *
304 * @param[in] instance UHCI structure to use.
305 * @param[in] batch Transfer batch to schedule.
306 * @return Error code
307 *
308 * Checks for bandwidth availability and appends the batch to the proper queue.
309 */
310int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
311{
312 assert(instance);
313 assert(batch);
314
315 transfer_list_t *list =
316 instance->transfers[batch->ep->speed][batch->ep->transfer_type];
317 assert(list);
318 transfer_list_add_batch(list, batch);
319
320 return EOK;
321}
322/*----------------------------------------------------------------------------*/
323/** Take action based on the interrupt cause.
324 *
325 * @param[in] instance UHCI structure to use.
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)
332 */
333void hc_interrupt(hc_t *instance, uint16_t status)
334{
335 assert(instance);
336 /* Lower 2 bits are transaction error and transaction complete */
337 if (status & (UHCI_STATUS_INTERRUPT | UHCI_STATUS_ERROR_INTERRUPT)) {
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);
353 usb_transfer_batch_finish(batch);
354 }
355 }
356 /* Resume interrupts are not supported */
357 if (status & UHCI_STATUS_RESUME) {
358 usb_log_error("Resume interrupt!\n");
359 }
360
361 /* Bits 4 and 5 indicate hc error */
362 if (status & (UHCI_STATUS_PROCESS_ERROR | UHCI_STATUS_SYSTEM_ERROR)) {
363 usb_log_error("UHCI hardware failure!.\n");
364 ++instance->hw_failures;
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);
369
370 if (instance->hw_failures < UHCI_ALLOWED_HW_FAIL) {
371 /* reinitialize hw, this triggers virtual disconnect*/
372 hc_init_hw(instance);
373 } else {
374 usb_log_fatal("Too many UHCI hardware failures!.\n");
375 hc_fini(instance);
376 }
377 }
378}
379/*----------------------------------------------------------------------------*/
380/** Polling function, emulates interrupts.
381 *
382 * @param[in] arg UHCI hc structure to use.
383 * @return EOK (should never return)
384 */
385int hc_interrupt_emulator(void* arg)
386{
387 usb_log_debug("Started interrupt emulator.\n");
388 hc_t *instance = arg;
389 assert(instance);
390
391 while (1) {
392 /* Read and clear status register */
393 uint16_t status = pio_read_16(&instance->registers->usbsts);
394 pio_write_16(&instance->registers->usbsts, status);
395 if (status != 0)
396 usb_log_debug2("UHCI status: %x.\n", status);
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;
401 hc_interrupt(instance, status);
402 async_usleep(UHCI_INT_EMULATOR_TIMEOUT);
403 }
404 return EOK;
405}
406/*---------------------------------------------------------------------------*/
407/** Debug function, checks consistency of memory structures.
408 *
409 * @param[in] arg UHCI structure to use.
410 * @return EOK (should never return)
411 */
412int hc_debug_checker(void *arg)
413{
414 hc_t *instance = arg;
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
431 const uintptr_t frame_list =
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",
435 (void *) frame_list,
436 (void *) addr_to_phys(instance->frame_list));
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) {
445 usb_log_debug("Interrupt QH: %p (frame %d) vs. %p.\n",
446 (void *) expected_pa, frnum, (void *) real_pa);
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",
453 (void *) expected_pa, (void *) real_pa);
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",
460 (void *) expected_pa, (void *) real_pa);
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",
467 (void *) expected_pa, (void *) real_pa);
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.