| [41b96b4] | 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 | */
|
|---|
| [8486c07] | 28 |
|
|---|
| [41b96b4] | 29 | /** @addtogroup drvusbohcihc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief OHCI Host controller driver routines
|
|---|
| 34 | */
|
|---|
| [8486c07] | 35 |
|
|---|
| [0d4b110] | 36 | #include <assert.h>
|
|---|
| 37 | #include <async.h>
|
|---|
| [41b96b4] | 38 | #include <errno.h>
|
|---|
| [0d4b110] | 39 | #include <macros.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| [41b96b4] | 42 | #include <str_error.h>
|
|---|
| [0d4b110] | 43 | #include <sys/types.h>
|
|---|
| [41b96b4] | 44 |
|
|---|
| 45 | #include <usb/debug.h>
|
|---|
| 46 | #include <usb/usb.h>
|
|---|
| 47 |
|
|---|
| [e20eaed] | 48 | #include "ohci_endpoint.h"
|
|---|
| [0d4b110] | 49 | #include "ohci_batch.h"
|
|---|
| 50 | #include "utils/malloc32.h"
|
|---|
| 51 |
|
|---|
| 52 | #include "hc.h"
|
|---|
| [41b96b4] | 53 |
|
|---|
| [561112f] | 54 | #define OHCI_USED_INTERRUPTS \
|
|---|
| 55 | (I_SO | I_WDH | I_UE | I_RHSC)
|
|---|
| [1ecc5de] | 56 |
|
|---|
| [d57122c] | 57 | static const irq_pio_range_t ohci_pio_ranges[] = {
|
|---|
| 58 | {
|
|---|
| [8486c07] | 59 | .base = 0,
|
|---|
| [d57122c] | 60 | .size = sizeof(ohci_regs_t)
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static const irq_cmd_t ohci_irq_commands[] = {
|
|---|
| [8486c07] | 65 | {
|
|---|
| 66 | .cmd = CMD_PIO_READ_32,
|
|---|
| 67 | .dstarg = 1,
|
|---|
| 68 | .addr = NULL
|
|---|
| 69 | },
|
|---|
| 70 | {
|
|---|
| 71 | .cmd = CMD_AND,
|
|---|
| 72 | .srcarg = 1,
|
|---|
| 73 | .dstarg = 2,
|
|---|
| [ea8b91d] | 74 | .value = 0
|
|---|
| [8486c07] | 75 | },
|
|---|
| 76 | {
|
|---|
| 77 | .cmd = CMD_PREDICATE,
|
|---|
| 78 | .srcarg = 2,
|
|---|
| 79 | .value = 2
|
|---|
| 80 | },
|
|---|
| 81 | {
|
|---|
| 82 | .cmd = CMD_PIO_WRITE_A_32,
|
|---|
| 83 | .srcarg = 1,
|
|---|
| 84 | .addr = NULL
|
|---|
| 85 | },
|
|---|
| 86 | {
|
|---|
| 87 | .cmd = CMD_ACCEPT
|
|---|
| 88 | }
|
|---|
| [1ecc5de] | 89 | };
|
|---|
| 90 |
|
|---|
| [2c617b0] | 91 | static void hc_gain_control(hc_t *instance);
|
|---|
| [1cb4f05] | 92 | static void hc_start(hc_t *instance);
|
|---|
| [6b6e3ed3] | 93 | static int hc_init_transfer_lists(hc_t *instance);
|
|---|
| [344925c] | 94 | static int hc_init_memory(hc_t *instance);
|
|---|
| [1cb4f05] | 95 | static int interrupt_emulator(hc_t *instance);
|
|---|
| [76fbd9a] | 96 |
|
|---|
| [d57122c] | 97 | /** Generate IRQ code.
|
|---|
| 98 | * @param[out] ranges PIO ranges buffer.
|
|---|
| 99 | * @param[in] ranges_size Size of the ranges buffer (bytes).
|
|---|
| 100 | * @param[out] cmds Commands buffer.
|
|---|
| 101 | * @param[in] cmds_size Size of the commands buffer (bytes).
|
|---|
| [7de1988c] | 102 | * @param[in] regs Device's register range.
|
|---|
| [1cb4f05] | 103 | *
|
|---|
| 104 | * @return Error code.
|
|---|
| 105 | */
|
|---|
| [6210a333] | 106 | int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs)
|
|---|
| [1cb4f05] | 107 | {
|
|---|
| [6210a333] | 108 | assert(code);
|
|---|
| 109 | if (RNGSZ(*regs) < sizeof(ohci_regs_t))
|
|---|
| [1cb4f05] | 110 | return EOVERFLOW;
|
|---|
| 111 |
|
|---|
| [6210a333] | 112 | code->ranges = malloc(sizeof(ohci_pio_ranges));
|
|---|
| 113 | if (code->ranges == NULL)
|
|---|
| 114 | return ENOMEM;
|
|---|
| [1cb4f05] | 115 |
|
|---|
| [6210a333] | 116 | code->cmds = malloc(sizeof(ohci_irq_commands));
|
|---|
| 117 | if (code->cmds == NULL) {
|
|---|
| 118 | free(code->ranges);
|
|---|
| 119 | return ENOMEM;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | code->rangecount = ARRAY_SIZE(ohci_pio_ranges);
|
|---|
| 123 | code->cmdcount = ARRAY_SIZE(ohci_irq_commands);
|
|---|
| 124 |
|
|---|
| 125 | memcpy(code->ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
|
|---|
| 126 | code->ranges[0].base = RNGABS(*regs);
|
|---|
| 127 |
|
|---|
| 128 | memcpy(code->cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
|
|---|
| [7de1988c] | 129 | ohci_regs_t *registers = (ohci_regs_t *) RNGABSPTR(*regs);
|
|---|
| [6210a333] | 130 | code->cmds[0].addr = (void *) ®isters->interrupt_status;
|
|---|
| 131 | code->cmds[3].addr = (void *) ®isters->interrupt_status;
|
|---|
| 132 | OHCI_WR(code->cmds[1].value, OHCI_USED_INTERRUPTS);
|
|---|
| [1cb4f05] | 133 |
|
|---|
| 134 | return EOK;
|
|---|
| 135 | }
|
|---|
| [76fbd9a] | 136 |
|
|---|
| [02cacce] | 137 | /** Initialize OHCI hc driver structure
|
|---|
| 138 | *
|
|---|
| 139 | * @param[in] instance Memory place for the structure.
|
|---|
| [7de1988c] | 140 | * @param[in] regs Device's I/O registers range.
|
|---|
| [02cacce] | 141 | * @param[in] interrupts True if w interrupts should be used
|
|---|
| 142 | * @return Error code
|
|---|
| 143 | */
|
|---|
| [7de1988c] | 144 | int hc_init(hc_t *instance, addr_range_t *regs, bool interrupts)
|
|---|
| [41b96b4] | 145 | {
|
|---|
| 146 | assert(instance);
|
|---|
| [1cb4f05] | 147 |
|
|---|
| [3f03199] | 148 | int ret = pio_enable_range(regs, (void **) &instance->registers);
|
|---|
| [6340a6ff] | 149 | if (ret != EOK) {
|
|---|
| [f83666c] | 150 | usb_log_error("Failed to gain access to device registers: %s.\n",
|
|---|
| [6340a6ff] | 151 | str_error(ret));
|
|---|
| 152 | return ret;
|
|---|
| 153 | }
|
|---|
| [c2be0e5] | 154 |
|
|---|
| [bba0dc20] | 155 | list_initialize(&instance->pending_batches);
|
|---|
| [6340a6ff] | 156 | fibril_mutex_initialize(&instance->guard);
|
|---|
| [e7bc999] | 157 |
|
|---|
| [8790650] | 158 | ret = hc_init_memory(instance);
|
|---|
| [6340a6ff] | 159 | if (ret != EOK) {
|
|---|
| 160 | usb_log_error("Failed to create OHCI memory structures: %s.\n",
|
|---|
| 161 | str_error(ret));
|
|---|
| 162 | return ret;
|
|---|
| 163 | }
|
|---|
| [2c617b0] | 164 |
|
|---|
| [78ab6d4] | 165 | hc_gain_control(instance);
|
|---|
| [ff582d47] | 166 |
|
|---|
| [ff0e354] | 167 | if (!interrupts) {
|
|---|
| 168 | instance->interrupt_emulator =
|
|---|
| 169 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
|---|
| 170 | fibril_add_ready(instance->interrupt_emulator);
|
|---|
| 171 | }
|
|---|
| [7013b14] | 172 |
|
|---|
| [171e668] | 173 | ohci_rh_init(&instance->rh, instance->registers, "ohci rh");
|
|---|
| [1ef93fa] | 174 | hc_start(instance);
|
|---|
| [78ab6d4] | 175 |
|
|---|
| [8627377] | 176 | return EOK;
|
|---|
| [a6d1bc1] | 177 | }
|
|---|
| [76fbd9a] | 178 |
|
|---|
| [57e06ef] | 179 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| [620c710] | 180 | {
|
|---|
| [57e06ef] | 181 | assert(instance);
|
|---|
| 182 | assert(ep);
|
|---|
| 183 |
|
|---|
| [620c710] | 184 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
|---|
| 185 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
|---|
| [57e06ef] | 186 | assert(list);
|
|---|
| 187 | assert(ohci_ep);
|
|---|
| 188 |
|
|---|
| [620c710] | 189 | /* Enqueue ep */
|
|---|
| 190 | switch (ep->transfer_type) {
|
|---|
| 191 | case USB_TRANSFER_CONTROL:
|
|---|
| [bfc5c9dd] | 192 | OHCI_CLR(instance->registers->control, C_CLE);
|
|---|
| [620c710] | 193 | endpoint_list_add_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 194 | OHCI_WR(instance->registers->control_current, 0);
|
|---|
| 195 | OHCI_SET(instance->registers->control, C_CLE);
|
|---|
| [620c710] | 196 | break;
|
|---|
| 197 | case USB_TRANSFER_BULK:
|
|---|
| [bfc5c9dd] | 198 | OHCI_CLR(instance->registers->control, C_BLE);
|
|---|
| [f974519] | 199 | endpoint_list_add_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 200 | OHCI_WR(instance->registers->bulk_current, 0);
|
|---|
| 201 | OHCI_SET(instance->registers->control, C_BLE);
|
|---|
| [620c710] | 202 | break;
|
|---|
| 203 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 204 | case USB_TRANSFER_INTERRUPT:
|
|---|
| [bfc5c9dd] | 205 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
|---|
| [f974519] | 206 | endpoint_list_add_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 207 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
|---|
| [620c710] | 208 | break;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| [76fbd9a] | 211 |
|
|---|
| [57e06ef] | 212 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| [620c710] | 213 | {
|
|---|
| [57e06ef] | 214 | assert(instance);
|
|---|
| 215 | assert(ep);
|
|---|
| 216 |
|
|---|
| [620c710] | 217 | /* Dequeue ep */
|
|---|
| 218 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
|---|
| 219 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
|---|
| [57e06ef] | 220 |
|
|---|
| 221 | assert(list);
|
|---|
| 222 | assert(ohci_ep);
|
|---|
| [620c710] | 223 | switch (ep->transfer_type) {
|
|---|
| 224 | case USB_TRANSFER_CONTROL:
|
|---|
| [bfc5c9dd] | 225 | OHCI_CLR(instance->registers->control, C_CLE);
|
|---|
| [620c710] | 226 | endpoint_list_remove_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 227 | OHCI_WR(instance->registers->control_current, 0);
|
|---|
| 228 | OHCI_SET(instance->registers->control, C_CLE);
|
|---|
| [620c710] | 229 | break;
|
|---|
| 230 | case USB_TRANSFER_BULK:
|
|---|
| [bfc5c9dd] | 231 | OHCI_CLR(instance->registers->control, C_BLE);
|
|---|
| [620c710] | 232 | endpoint_list_remove_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 233 | OHCI_WR(instance->registers->bulk_current, 0);
|
|---|
| 234 | OHCI_SET(instance->registers->control, C_BLE);
|
|---|
| [620c710] | 235 | break;
|
|---|
| 236 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 237 | case USB_TRANSFER_INTERRUPT:
|
|---|
| [bfc5c9dd] | 238 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
|---|
| [620c710] | 239 | endpoint_list_remove_ep(list, ohci_ep);
|
|---|
| [bfc5c9dd] | 240 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
|---|
| [620c710] | 241 | break;
|
|---|
| 242 | default:
|
|---|
| 243 | break;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| [76fbd9a] | 246 |
|
|---|
| [02cacce] | 247 | /** Add USB transfer to the schedule.
|
|---|
| 248 | *
|
|---|
| 249 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 250 | * @param[in] batch Batch representing the transfer.
|
|---|
| 251 | * @return Error code.
|
|---|
| 252 | */
|
|---|
| [09ace19] | 253 | int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
|---|
| [41b96b4] | 254 | {
|
|---|
| [09ace19] | 255 | assert(hcd);
|
|---|
| [9348862] | 256 | hc_t *instance = hcd->driver.data;
|
|---|
| [41b96b4] | 257 | assert(instance);
|
|---|
| [9ff5ff82] | 258 |
|
|---|
| [02cacce] | 259 | /* Check for root hub communication */
|
|---|
| [171e668] | 260 | if (batch->ep->address == ohci_rh_get_address(&instance->rh)) {
|
|---|
| [ffcc5776] | 261 | usb_log_debug("OHCI root hub request.\n");
|
|---|
| [171e668] | 262 | return ohci_rh_schedule(&instance->rh, batch);
|
|---|
| [41b96b4] | 263 | }
|
|---|
| [9c10e51] | 264 | ohci_transfer_batch_t *ohci_batch = ohci_transfer_batch_get(batch);
|
|---|
| 265 | if (!ohci_batch)
|
|---|
| 266 | return ENOMEM;
|
|---|
| [7013b14] | 267 |
|
|---|
| [aa9ccf7] | 268 | fibril_mutex_lock(&instance->guard);
|
|---|
| [9c10e51] | 269 | list_append(&ohci_batch->link, &instance->pending_batches);
|
|---|
| 270 | ohci_transfer_batch_commit(ohci_batch);
|
|---|
| [02cacce] | 271 |
|
|---|
| 272 | /* Control and bulk schedules need a kick to start working */
|
|---|
| 273 | switch (batch->ep->transfer_type)
|
|---|
| 274 | {
|
|---|
| [9ff5ff82] | 275 | case USB_TRANSFER_CONTROL:
|
|---|
| [bfc5c9dd] | 276 | OHCI_SET(instance->registers->command_status, CS_CLF);
|
|---|
| [9ff5ff82] | 277 | break;
|
|---|
| 278 | case USB_TRANSFER_BULK:
|
|---|
| [bfc5c9dd] | 279 | OHCI_SET(instance->registers->command_status, CS_BLF);
|
|---|
| [9ff5ff82] | 280 | break;
|
|---|
| 281 | default:
|
|---|
| 282 | break;
|
|---|
| 283 | }
|
|---|
| [aa9ccf7] | 284 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [4c28d17] | 285 | return EOK;
|
|---|
| [41b96b4] | 286 | }
|
|---|
| [76fbd9a] | 287 |
|
|---|
| [02cacce] | 288 | /** Interrupt handling routine
|
|---|
| 289 | *
|
|---|
| 290 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 291 | * @param[in] status Value of the status register at the time of interrupt.
|
|---|
| 292 | */
|
|---|
| [7d6a676] | 293 | void hc_interrupt(hc_t *instance, uint32_t status)
|
|---|
| [41b96b4] | 294 | {
|
|---|
| [d1ca752] | 295 | status = OHCI_RD(status);
|
|---|
| [41b96b4] | 296 | assert(instance);
|
|---|
| [561112f] | 297 | if ((status & ~I_SF) == 0) /* ignore sof status */
|
|---|
| [eaf1e3d] | 298 | return;
|
|---|
| [2df648c2] | 299 | usb_log_debug2("OHCI(%p) interrupt: %x.\n", instance, status);
|
|---|
| [561112f] | 300 | if (status & I_RHSC)
|
|---|
| [171e668] | 301 | ohci_rh_interrupt(&instance->rh);
|
|---|
| [7d6a676] | 302 |
|
|---|
| [561112f] | 303 | if (status & I_WDH) {
|
|---|
| [aa9ccf7] | 304 | fibril_mutex_lock(&instance->guard);
|
|---|
| [4125b7d] | 305 | usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).\n", instance->hcca,
|
|---|
| [bfc5c9dd] | 306 | OHCI_RD(instance->registers->hcca),
|
|---|
| [4125b7d] | 307 | (void *) addr_to_phys(instance->hcca));
|
|---|
| 308 | usb_log_debug2("Periodic current: %#" PRIx32 ".\n",
|
|---|
| [bfc5c9dd] | 309 | OHCI_RD(instance->registers->periodic_current));
|
|---|
| [eaf1e3d] | 310 |
|
|---|
| [9c10e51] | 311 | link_t *current = list_first(&instance->pending_batches);
|
|---|
| 312 | while (current && current != &instance->pending_batches.head) {
|
|---|
| [7013b14] | 313 | link_t *next = current->next;
|
|---|
| [9c10e51] | 314 | ohci_transfer_batch_t *batch =
|
|---|
| 315 | ohci_transfer_batch_from_link(current);
|
|---|
| [7013b14] | 316 |
|
|---|
| [9c10e51] | 317 | if (ohci_transfer_batch_is_complete(batch)) {
|
|---|
| [d6522dd] | 318 | list_remove(current);
|
|---|
| [9c10e51] | 319 | ohci_transfer_batch_finish_dispose(batch);
|
|---|
| [7013b14] | 320 | }
|
|---|
| [b72efe8] | 321 |
|
|---|
| [7013b14] | 322 | current = next;
|
|---|
| [eaf1e3d] | 323 | }
|
|---|
| [aa9ccf7] | 324 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [4c28d17] | 325 | }
|
|---|
| [68b9f148] | 326 |
|
|---|
| 327 | if (status & I_UE) {
|
|---|
| [f974519] | 328 | usb_log_fatal("Error like no other!\n");
|
|---|
| [1ef93fa] | 329 | hc_start(instance);
|
|---|
| [68b9f148] | 330 | }
|
|---|
| 331 |
|
|---|
| [41b96b4] | 332 | }
|
|---|
| [76fbd9a] | 333 |
|
|---|
| [02cacce] | 334 | /** Check status register regularly
|
|---|
| 335 | *
|
|---|
| 336 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 337 | * @return Error code
|
|---|
| 338 | */
|
|---|
| [53f1c87] | 339 | int interrupt_emulator(hc_t *instance)
|
|---|
| [7d6a676] | 340 | {
|
|---|
| 341 | assert(instance);
|
|---|
| 342 | usb_log_info("Started interrupt emulator.\n");
|
|---|
| 343 | while (1) {
|
|---|
| [2c617b0] | 344 | const uint32_t status = instance->registers->interrupt_status;
|
|---|
| [7d6a676] | 345 | instance->registers->interrupt_status = status;
|
|---|
| 346 | hc_interrupt(instance, status);
|
|---|
| [02cacce] | 347 | async_usleep(10000);
|
|---|
| [7d6a676] | 348 | }
|
|---|
| 349 | return EOK;
|
|---|
| 350 | }
|
|---|
| [76fbd9a] | 351 |
|
|---|
| [02cacce] | 352 | /** Turn off any (BIOS)driver that might be in control of the device.
|
|---|
| [78ab6d4] | 353 | *
|
|---|
| 354 | * This function implements routines described in chapter 5.1.1.3 of the OHCI
|
|---|
| 355 | * specification (page 40, pdf page 54).
|
|---|
| [02cacce] | 356 | *
|
|---|
| 357 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 358 | */
|
|---|
| [2c617b0] | 359 | void hc_gain_control(hc_t *instance)
|
|---|
| 360 | {
|
|---|
| 361 | assert(instance);
|
|---|
| [78ab6d4] | 362 |
|
|---|
| [c8eddf4] | 363 | usb_log_debug("Requesting OHCI control.\n");
|
|---|
| [bfc5c9dd] | 364 | if (OHCI_RD(instance->registers->revision) & R_LEGACY_FLAG) {
|
|---|
| [78ab6d4] | 365 | /* Turn off legacy emulation, it should be enough to zero
|
|---|
| 366 | * the lowest bit, but it caused problems. Thus clear all
|
|---|
| 367 | * except GateA20 (causes restart on some hw).
|
|---|
| 368 | * See page 145 of the specs for details.
|
|---|
| 369 | */
|
|---|
| 370 | volatile uint32_t *ohci_emulation_reg =
|
|---|
| 371 | (uint32_t*)((char*)instance->registers + LEGACY_REGS_OFFSET);
|
|---|
| 372 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
|---|
| [bfc5c9dd] | 373 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
|---|
| [78ab6d4] | 374 | /* Zero everything but A20State */
|
|---|
| [bfc5c9dd] | 375 | OHCI_CLR(*ohci_emulation_reg, ~0x100);
|
|---|
| [78ab6d4] | 376 | usb_log_debug(
|
|---|
| 377 | "OHCI legacy register (should be 0 or 0x100) %p: %x.\n",
|
|---|
| [bfc5c9dd] | 378 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
|---|
| [78ab6d4] | 379 | }
|
|---|
| [112d159] | 380 |
|
|---|
| [2c617b0] | 381 | /* Interrupt routing enabled => smm driver is active */
|
|---|
| [bfc5c9dd] | 382 | if (OHCI_RD(instance->registers->control) & C_IR) {
|
|---|
| [112d159] | 383 | usb_log_debug("SMM driver: request ownership change.\n");
|
|---|
| [bfc5c9dd] | 384 | OHCI_SET(instance->registers->command_status, CS_OCR);
|
|---|
| [78ab6d4] | 385 | /* Hope that SMM actually knows its stuff or we can hang here */
|
|---|
| [f5bfd98] | 386 | while (OHCI_RD(instance->registers->control) & C_IR) {
|
|---|
| [2c617b0] | 387 | async_usleep(1000);
|
|---|
| 388 | }
|
|---|
| [112d159] | 389 | usb_log_info("SMM driver: Ownership taken.\n");
|
|---|
| [78ab6d4] | 390 | C_HCFS_SET(instance->registers->control, C_HCFS_RESET);
|
|---|
| [5d07f54] | 391 | async_usleep(50000);
|
|---|
| [2c617b0] | 392 | return;
|
|---|
| 393 | }
|
|---|
| [8486c07] | 394 |
|
|---|
| [78ab6d4] | 395 | const unsigned hc_status = C_HCFS_GET(instance->registers->control);
|
|---|
| [2c617b0] | 396 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
|---|
| 397 | if (hc_status != C_HCFS_RESET) {
|
|---|
| [112d159] | 398 | usb_log_debug("BIOS driver found.\n");
|
|---|
| [2c617b0] | 399 | if (hc_status == C_HCFS_OPERATIONAL) {
|
|---|
| [112d159] | 400 | usb_log_info("BIOS driver: HC operational.\n");
|
|---|
| [2c617b0] | 401 | return;
|
|---|
| 402 | }
|
|---|
| [bfc5c9dd] | 403 | /* HC is suspended assert resume for 20ms */
|
|---|
| [78ab6d4] | 404 | C_HCFS_SET(instance->registers->control, C_HCFS_RESUME);
|
|---|
| [2c617b0] | 405 | async_usleep(20000);
|
|---|
| [112d159] | 406 | usb_log_info("BIOS driver: HC resumed.\n");
|
|---|
| [2c617b0] | 407 | return;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /* HC is in reset (hw startup) => no other driver
|
|---|
| 411 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
|---|
| [c4fb5ecd] | 412 | usb_log_debug("Host controller found in reset state.\n");
|
|---|
| [2c617b0] | 413 | async_usleep(50000);
|
|---|
| 414 | }
|
|---|
| [76fbd9a] | 415 |
|
|---|
| [02cacce] | 416 | /** OHCI hw initialization routine.
|
|---|
| 417 | *
|
|---|
| 418 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 419 | */
|
|---|
| [1ef93fa] | 420 | void hc_start(hc_t *instance)
|
|---|
| [2c617b0] | 421 | {
|
|---|
| [112d159] | 422 | /* OHCI guide page 42 */
|
|---|
| [2c617b0] | 423 | assert(instance);
|
|---|
| [112d159] | 424 | usb_log_debug2("Started hc initialization routine.\n");
|
|---|
| 425 |
|
|---|
| 426 | /* Save contents of fm_interval register */
|
|---|
| [bfc5c9dd] | 427 | const uint32_t fm_interval = OHCI_RD(instance->registers->fm_interval);
|
|---|
| [112d159] | 428 | usb_log_debug2("Old value of HcFmInterval: %x.\n", fm_interval);
|
|---|
| [344925c] | 429 |
|
|---|
| [112d159] | 430 | /* Reset hc */
|
|---|
| 431 | usb_log_debug2("HC reset.\n");
|
|---|
| 432 | size_t time = 0;
|
|---|
| [bfc5c9dd] | 433 | OHCI_WR(instance->registers->command_status, CS_HCR);
|
|---|
| 434 | while (OHCI_RD(instance->registers->command_status) & CS_HCR) {
|
|---|
| [112d159] | 435 | async_usleep(10);
|
|---|
| 436 | time += 10;
|
|---|
| 437 | }
|
|---|
| 438 | usb_log_debug2("HC reset complete in %zu us.\n", time);
|
|---|
| [344925c] | 439 |
|
|---|
| [112d159] | 440 | /* Restore fm_interval */
|
|---|
| [bfc5c9dd] | 441 | OHCI_WR(instance->registers->fm_interval, fm_interval);
|
|---|
| 442 | assert((OHCI_RD(instance->registers->command_status) & CS_HCR) == 0);
|
|---|
| [344925c] | 443 |
|
|---|
| [2c617b0] | 444 | /* hc is now in suspend state */
|
|---|
| [112d159] | 445 | usb_log_debug2("HC should be in suspend state(%x).\n",
|
|---|
| [bfc5c9dd] | 446 | OHCI_RD(instance->registers->control));
|
|---|
| [344925c] | 447 |
|
|---|
| [78d4e1f] | 448 | /* Use HCCA */
|
|---|
| [bfc5c9dd] | 449 | OHCI_WR(instance->registers->hcca, addr_to_phys(instance->hcca));
|
|---|
| [78d4e1f] | 450 |
|
|---|
| 451 | /* Use queues */
|
|---|
| [bfc5c9dd] | 452 | OHCI_WR(instance->registers->bulk_head,
|
|---|
| 453 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
|---|
| [4125b7d] | 454 | usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").\n",
|
|---|
| [5a2c42b] | 455 | instance->lists[USB_TRANSFER_BULK].list_head,
|
|---|
| 456 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
|---|
| [78d4e1f] | 457 |
|
|---|
| [bfc5c9dd] | 458 | OHCI_WR(instance->registers->control_head,
|
|---|
| 459 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
|---|
| [4125b7d] | 460 | usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").\n",
|
|---|
| [5a2c42b] | 461 | instance->lists[USB_TRANSFER_CONTROL].list_head,
|
|---|
| 462 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
|---|
| [78d4e1f] | 463 |
|
|---|
| [112d159] | 464 | /* Enable queues */
|
|---|
| [65eac7b] | 465 | OHCI_SET(instance->registers->control, (C_PLE | C_IE | C_CLE | C_BLE));
|
|---|
| 466 | usb_log_debug("Queues enabled(%x).\n",
|
|---|
| 467 | OHCI_RD(instance->registers->control));
|
|---|
| [112d159] | 468 |
|
|---|
| [561112f] | 469 | /* Enable interrupts */
|
|---|
| [bfc5c9dd] | 470 | OHCI_WR(instance->registers->interrupt_enable, OHCI_USED_INTERRUPTS);
|
|---|
| 471 | usb_log_debug("Enabled interrupts: %x.\n",
|
|---|
| 472 | OHCI_RD(instance->registers->interrupt_enable));
|
|---|
| 473 | OHCI_WR(instance->registers->interrupt_enable, I_MI);
|
|---|
| [112d159] | 474 |
|
|---|
| 475 | /* Set periodic start to 90% */
|
|---|
| [bfc5c9dd] | 476 | const uint32_t frame_length =
|
|---|
| 477 | (fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK;
|
|---|
| 478 | OHCI_WR(instance->registers->periodic_start,
|
|---|
| 479 | ((frame_length / 10) * 9) & PS_MASK << PS_SHIFT);
|
|---|
| [112d159] | 480 | usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).\n",
|
|---|
| [bfc5c9dd] | 481 | OHCI_RD(instance->registers->periodic_start),
|
|---|
| 482 | OHCI_RD(instance->registers->periodic_start), frame_length);
|
|---|
| [78ab6d4] | 483 | C_HCFS_SET(instance->registers->control, C_HCFS_OPERATIONAL);
|
|---|
| [c4fb5ecd] | 484 | usb_log_debug("OHCI HC up and running (ctl_reg=0x%x).\n",
|
|---|
| [bfc5c9dd] | 485 | OHCI_RD(instance->registers->control));
|
|---|
| [2c617b0] | 486 | }
|
|---|
| [76fbd9a] | 487 |
|
|---|
| [02cacce] | 488 | /** Initialize schedule queues
|
|---|
| 489 | *
|
|---|
| 490 | * @param[in] instance OHCI hc driver structure
|
|---|
| 491 | * @return Error code
|
|---|
| 492 | */
|
|---|
| [6b6e3ed3] | 493 | int hc_init_transfer_lists(hc_t *instance)
|
|---|
| 494 | {
|
|---|
| 495 | assert(instance);
|
|---|
| [5a2c42b] | 496 | #define SETUP_ENDPOINT_LIST(type) \
|
|---|
| [344925c] | 497 | do { \
|
|---|
| [5a2c42b] | 498 | const char *name = usb_str_transfer_type(type); \
|
|---|
| [6340a6ff] | 499 | const int ret = endpoint_list_init(&instance->lists[type], name); \
|
|---|
| [6b6e3ed3] | 500 | if (ret != EOK) { \
|
|---|
| [1cb4f05] | 501 | usb_log_error("Failed to setup %s endpoint list: %s.\n", \
|
|---|
| 502 | name, str_error(ret)); \
|
|---|
| [68b9f148] | 503 | endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]);\
|
|---|
| [5a2c42b] | 504 | endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
|
|---|
| 505 | endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
|
|---|
| 506 | endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
|
|---|
| [70c85320] | 507 | return ret; \
|
|---|
| [344925c] | 508 | } \
|
|---|
| 509 | } while (0)
|
|---|
| [6b6e3ed3] | 510 |
|
|---|
| [5a2c42b] | 511 | SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
|
|---|
| 512 | SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
|
|---|
| 513 | SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
|
|---|
| 514 | SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
|
|---|
| 515 | #undef SETUP_ENDPOINT_LIST
|
|---|
| 516 | endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
|
|---|
| 517 | &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
|
|---|
| [6b6e3ed3] | 518 |
|
|---|
| 519 | return EOK;
|
|---|
| 520 | }
|
|---|
| [76fbd9a] | 521 |
|
|---|
| [02cacce] | 522 | /** Initialize memory structures used by the OHCI hcd.
|
|---|
| 523 | *
|
|---|
| 524 | * @param[in] instance OHCI hc driver structure.
|
|---|
| 525 | * @return Error code.
|
|---|
| 526 | */
|
|---|
| [344925c] | 527 | int hc_init_memory(hc_t *instance)
|
|---|
| 528 | {
|
|---|
| 529 | assert(instance);
|
|---|
| [5d07f54] | 530 |
|
|---|
| [acdb5bac] | 531 | memset(&instance->rh, 0, sizeof(instance->rh));
|
|---|
| [8790650] | 532 | /* Init queues */
|
|---|
| [8953514] | 533 | const int ret = hc_init_transfer_lists(instance);
|
|---|
| 534 | if (ret != EOK) {
|
|---|
| 535 | return ret;
|
|---|
| 536 | }
|
|---|
| [344925c] | 537 |
|
|---|
| [8790650] | 538 | /*Init HCCA */
|
|---|
| [f8dfb40] | 539 | instance->hcca = hcca_get();
|
|---|
| [344925c] | 540 | if (instance->hcca == NULL)
|
|---|
| 541 | return ENOMEM;
|
|---|
| [78d4e1f] | 542 | usb_log_debug2("OHCI HCCA initialized at %p.\n", instance->hcca);
|
|---|
| [344925c] | 543 |
|
|---|
| [1b90e90] | 544 | for (unsigned i = 0; i < HCCA_INT_EP_COUNT; ++i) {
|
|---|
| 545 | hcca_set_int_ep(instance->hcca, i,
|
|---|
| [65eac7b] | 546 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
|---|
| [344925c] | 547 | }
|
|---|
| [4125b7d] | 548 | usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").\n",
|
|---|
| [5a2c42b] | 549 | instance->lists[USB_TRANSFER_INTERRUPT].list_head,
|
|---|
| 550 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
|---|
| [344925c] | 551 |
|
|---|
| 552 | return EOK;
|
|---|
| 553 | }
|
|---|
| [1ecc5de] | 554 |
|
|---|
| [41b96b4] | 555 | /**
|
|---|
| 556 | * @}
|
|---|
| 557 | */
|
|---|