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

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

Further cleanup, includes, const, …

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