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