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