[41b96b4] | 1 | /*
|
---|
[9dfb034] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[41b96b4] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
[e0a5d4c] | 4 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[41b96b4] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
[8486c07] | 30 |
|
---|
[c8ea6eca] | 31 | /** @addtogroup drvusbohci
|
---|
[41b96b4] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | * @brief OHCI Host controller driver routines
|
---|
| 36 | */
|
---|
[8486c07] | 37 |
|
---|
[0d4b110] | 38 | #include <assert.h>
|
---|
| 39 | #include <async.h>
|
---|
[41b96b4] | 40 | #include <errno.h>
|
---|
[0d4b110] | 41 | #include <macros.h>
|
---|
| 42 | #include <mem.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
[41b96b4] | 44 | #include <str_error.h>
|
---|
[8d2dd7f2] | 45 | #include <stddef.h>
|
---|
| 46 | #include <stdint.h>
|
---|
[41b96b4] | 47 |
|
---|
| 48 | #include <usb/debug.h>
|
---|
[c6f82e5] | 49 | #include <usb/host/utility.h>
|
---|
[41b96b4] | 50 | #include <usb/usb.h>
|
---|
| 51 |
|
---|
[e6b9182] | 52 | #include "ohci_bus.h"
|
---|
[0d4b110] | 53 | #include "ohci_batch.h"
|
---|
| 54 |
|
---|
| 55 | #include "hc.h"
|
---|
[41b96b4] | 56 |
|
---|
[561112f] | 57 | #define OHCI_USED_INTERRUPTS \
|
---|
| 58 | (I_SO | I_WDH | I_UE | I_RHSC)
|
---|
[1ecc5de] | 59 |
|
---|
[d57122c] | 60 | static const irq_pio_range_t ohci_pio_ranges[] = {
|
---|
| 61 | {
|
---|
[8486c07] | 62 | .base = 0,
|
---|
[d57122c] | 63 | .size = sizeof(ohci_regs_t)
|
---|
| 64 | }
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | static const irq_cmd_t ohci_irq_commands[] = {
|
---|
[8486c07] | 68 | {
|
---|
| 69 | .cmd = CMD_PIO_READ_32,
|
---|
| 70 | .dstarg = 1,
|
---|
| 71 | .addr = NULL
|
---|
| 72 | },
|
---|
| 73 | {
|
---|
| 74 | .cmd = CMD_AND,
|
---|
| 75 | .srcarg = 1,
|
---|
| 76 | .dstarg = 2,
|
---|
[ea8b91d] | 77 | .value = 0
|
---|
[8486c07] | 78 | },
|
---|
| 79 | {
|
---|
| 80 | .cmd = CMD_PREDICATE,
|
---|
| 81 | .srcarg = 2,
|
---|
| 82 | .value = 2
|
---|
| 83 | },
|
---|
| 84 | {
|
---|
| 85 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
| 86 | .srcarg = 1,
|
---|
| 87 | .addr = NULL
|
---|
| 88 | },
|
---|
| 89 | {
|
---|
| 90 | .cmd = CMD_ACCEPT
|
---|
| 91 | }
|
---|
[1ecc5de] | 92 | };
|
---|
| 93 |
|
---|
[5a6cc679] | 94 | static errno_t hc_init_transfer_lists(hc_t *instance);
|
---|
| 95 | static errno_t hc_init_memory(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).
|
---|
[ba4a03a5] | 102 | * @param[in] hw_res Device's resources.
|
---|
[1cb4f05] | 103 | *
|
---|
| 104 | * @return Error code.
|
---|
| 105 | */
|
---|
[5a6cc679] | 106 | errno_t hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res, int *irq)
|
---|
[1cb4f05] | 107 | {
|
---|
[6210a333] | 108 | assert(code);
|
---|
[ba4a03a5] | 109 | assert(hw_res);
|
---|
| 110 |
|
---|
| 111 | if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1)
|
---|
| 112 | return EINVAL;
|
---|
| 113 |
|
---|
| 114 | const addr_range_t regs = hw_res->mem_ranges.ranges[0];
|
---|
| 115 |
|
---|
| 116 | if (RNGSZ(regs) < sizeof(ohci_regs_t))
|
---|
[1cb4f05] | 117 | return EOVERFLOW;
|
---|
| 118 |
|
---|
[6210a333] | 119 | code->ranges = malloc(sizeof(ohci_pio_ranges));
|
---|
| 120 | if (code->ranges == NULL)
|
---|
| 121 | return ENOMEM;
|
---|
[1cb4f05] | 122 |
|
---|
[6210a333] | 123 | code->cmds = malloc(sizeof(ohci_irq_commands));
|
---|
| 124 | if (code->cmds == NULL) {
|
---|
| 125 | free(code->ranges);
|
---|
| 126 | return ENOMEM;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | code->rangecount = ARRAY_SIZE(ohci_pio_ranges);
|
---|
| 130 | code->cmdcount = ARRAY_SIZE(ohci_irq_commands);
|
---|
| 131 |
|
---|
| 132 | memcpy(code->ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
|
---|
[ba4a03a5] | 133 | code->ranges[0].base = RNGABS(regs);
|
---|
[6210a333] | 134 |
|
---|
| 135 | memcpy(code->cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
|
---|
[ba4a03a5] | 136 | ohci_regs_t *registers = (ohci_regs_t *) RNGABSPTR(regs);
|
---|
[6210a333] | 137 | code->cmds[0].addr = (void *) ®isters->interrupt_status;
|
---|
| 138 | code->cmds[3].addr = (void *) ®isters->interrupt_status;
|
---|
| 139 | OHCI_WR(code->cmds[1].value, OHCI_USED_INTERRUPTS);
|
---|
[1cb4f05] | 140 |
|
---|
[a1732929] | 141 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.",
|
---|
[ba4a03a5] | 142 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
---|
| 143 |
|
---|
[68e5406] | 144 | *irq = hw_res->irqs.irqs[0];
|
---|
| 145 | return EOK;
|
---|
[1cb4f05] | 146 | }
|
---|
[76fbd9a] | 147 |
|
---|
[02cacce] | 148 | /** Initialize OHCI hc driver structure
|
---|
| 149 | *
|
---|
| 150 | * @param[in] instance Memory place for the structure.
|
---|
[7813516] | 151 | * @param[in] regs Device's resources
|
---|
[02cacce] | 152 | * @param[in] interrupts True if w interrupts should be used
|
---|
| 153 | * @return Error code
|
---|
| 154 | */
|
---|
[5a6cc679] | 155 | errno_t hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
---|
[41b96b4] | 156 | {
|
---|
[32fb6bce] | 157 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[7813516] | 158 | assert(hw_res);
|
---|
| 159 | if (hw_res->mem_ranges.count != 1 ||
|
---|
| 160 | hw_res->mem_ranges.ranges[0].size < sizeof(ohci_regs_t))
|
---|
[3bacee1] | 161 | return EINVAL;
|
---|
[1cb4f05] | 162 |
|
---|
[5a6cc679] | 163 | errno_t ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
|
---|
[7813516] | 164 | (void **) &instance->registers);
|
---|
[6340a6ff] | 165 | if (ret != EOK) {
|
---|
[a1732929] | 166 | usb_log_error("Failed to gain access to registers: %s.",
|
---|
[6340a6ff] | 167 | str_error(ret));
|
---|
| 168 | return ret;
|
---|
| 169 | }
|
---|
[a1732929] | 170 | usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
|
---|
[7813516] | 171 | hw_res->mem_ranges.ranges[0].address.absolute,
|
---|
| 172 | hw_res->mem_ranges.ranges[0].size);
|
---|
[c2be0e5] | 173 |
|
---|
[d60115a] | 174 | list_initialize(&instance->pending_endpoints);
|
---|
[6340a6ff] | 175 | fibril_mutex_initialize(&instance->guard);
|
---|
[e7bc999] | 176 |
|
---|
[8790650] | 177 | ret = hc_init_memory(instance);
|
---|
[6340a6ff] | 178 | if (ret != EOK) {
|
---|
[a1732929] | 179 | usb_log_error("Failed to create OHCI memory structures: %s.",
|
---|
[6340a6ff] | 180 | str_error(ret));
|
---|
[58563585] | 181 | // TODO: We should disable pio access here
|
---|
[6340a6ff] | 182 | return ret;
|
---|
| 183 | }
|
---|
[2c617b0] | 184 |
|
---|
[8627377] | 185 | return EOK;
|
---|
[a6d1bc1] | 186 | }
|
---|
[76fbd9a] | 187 |
|
---|
[7813516] | 188 | /** Safely dispose host controller internal structures
|
---|
| 189 | *
|
---|
| 190 | * @param[in] instance Host controller structure to use.
|
---|
| 191 | */
|
---|
[32fb6bce] | 192 | int hc_gone(hc_device_t *instance)
|
---|
[7813516] | 193 | {
|
---|
| 194 | assert(instance);
|
---|
[d1582b50] | 195 | /* TODO: implement */
|
---|
[32fb6bce] | 196 | return ENOTSUP;
|
---|
| 197 | }
|
---|
[7813516] | 198 |
|
---|
[9dfb034] | 199 | /** Quiesce host controller
|
---|
| 200 | *
|
---|
| 201 | * @param hcd Host controller device
|
---|
| 202 | */
|
---|
| 203 | int hc_quiesce(hc_device_t *hcd)
|
---|
| 204 | {
|
---|
| 205 | hc_t *instance = hcd_to_hc(hcd);
|
---|
| 206 |
|
---|
| 207 | /* OHCI guide page 42 */
|
---|
| 208 | usb_log_debug2("Started hc initialization routine.");
|
---|
| 209 |
|
---|
| 210 | /* Reset hc */
|
---|
| 211 | usb_log_debug2("HC reset.");
|
---|
| 212 | size_t time = 0;
|
---|
| 213 | OHCI_WR(instance->registers->command_status, CS_HCR);
|
---|
| 214 | while (OHCI_RD(instance->registers->command_status) & CS_HCR) {
|
---|
| 215 | fibril_usleep(10);
|
---|
| 216 | time += 10;
|
---|
| 217 | }
|
---|
| 218 | usb_log_debug2("HC reset complete in %zu us.", time);
|
---|
| 219 |
|
---|
| 220 | return EOK;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[57e06ef] | 223 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
[620c710] | 224 | {
|
---|
[57e06ef] | 225 | assert(instance);
|
---|
| 226 | assert(ep);
|
---|
| 227 |
|
---|
[620c710] | 228 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
---|
| 229 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
[57e06ef] | 230 | assert(list);
|
---|
| 231 | assert(ohci_ep);
|
---|
| 232 |
|
---|
[620c710] | 233 | /* Enqueue ep */
|
---|
| 234 | switch (ep->transfer_type) {
|
---|
| 235 | case USB_TRANSFER_CONTROL:
|
---|
[bfc5c9dd] | 236 | OHCI_CLR(instance->registers->control, C_CLE);
|
---|
[620c710] | 237 | endpoint_list_add_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 238 | OHCI_WR(instance->registers->control_current, 0);
|
---|
| 239 | OHCI_SET(instance->registers->control, C_CLE);
|
---|
[620c710] | 240 | break;
|
---|
| 241 | case USB_TRANSFER_BULK:
|
---|
[bfc5c9dd] | 242 | OHCI_CLR(instance->registers->control, C_BLE);
|
---|
[f974519] | 243 | endpoint_list_add_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 244 | OHCI_WR(instance->registers->bulk_current, 0);
|
---|
| 245 | OHCI_SET(instance->registers->control, C_BLE);
|
---|
[620c710] | 246 | break;
|
---|
| 247 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
| 248 | case USB_TRANSFER_INTERRUPT:
|
---|
[bfc5c9dd] | 249 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
---|
[f974519] | 250 | endpoint_list_add_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 251 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
---|
[620c710] | 252 | break;
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
[76fbd9a] | 255 |
|
---|
[57e06ef] | 256 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
[620c710] | 257 | {
|
---|
[57e06ef] | 258 | assert(instance);
|
---|
| 259 | assert(ep);
|
---|
| 260 |
|
---|
[620c710] | 261 | /* Dequeue ep */
|
---|
| 262 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
---|
| 263 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
[57e06ef] | 264 |
|
---|
| 265 | assert(list);
|
---|
| 266 | assert(ohci_ep);
|
---|
[620c710] | 267 | switch (ep->transfer_type) {
|
---|
| 268 | case USB_TRANSFER_CONTROL:
|
---|
[bfc5c9dd] | 269 | OHCI_CLR(instance->registers->control, C_CLE);
|
---|
[620c710] | 270 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 271 | OHCI_WR(instance->registers->control_current, 0);
|
---|
| 272 | OHCI_SET(instance->registers->control, C_CLE);
|
---|
[620c710] | 273 | break;
|
---|
| 274 | case USB_TRANSFER_BULK:
|
---|
[bfc5c9dd] | 275 | OHCI_CLR(instance->registers->control, C_BLE);
|
---|
[620c710] | 276 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 277 | OHCI_WR(instance->registers->bulk_current, 0);
|
---|
| 278 | OHCI_SET(instance->registers->control, C_BLE);
|
---|
[620c710] | 279 | break;
|
---|
| 280 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
| 281 | case USB_TRANSFER_INTERRUPT:
|
---|
[bfc5c9dd] | 282 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
---|
[620c710] | 283 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
[bfc5c9dd] | 284 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
---|
[620c710] | 285 | break;
|
---|
| 286 | default:
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
[76fbd9a] | 290 |
|
---|
[5a6cc679] | 291 | errno_t ohci_hc_status(bus_t *bus_base, uint32_t *status)
|
---|
[e26a9d95] | 292 | {
|
---|
[32fb6bce] | 293 | assert(bus_base);
|
---|
[e26a9d95] | 294 | assert(status);
|
---|
| 295 |
|
---|
[32fb6bce] | 296 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
| 297 | hc_t *hc = bus->hc;
|
---|
| 298 | assert(hc);
|
---|
| 299 |
|
---|
[3bacee1] | 300 | if (hc->registers) {
|
---|
[32fb6bce] | 301 | *status = OHCI_RD(hc->registers->interrupt_status);
|
---|
| 302 | OHCI_WR(hc->registers->interrupt_status, *status);
|
---|
[e26a9d95] | 303 | }
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[02cacce] | 307 | /** Add USB transfer to the schedule.
|
---|
| 308 | *
|
---|
[fccf289] | 309 | * @param[in] hcd HCD driver structure.
|
---|
[02cacce] | 310 | * @param[in] batch Batch representing the transfer.
|
---|
| 311 | * @return Error code.
|
---|
| 312 | */
|
---|
[5a6cc679] | 313 | errno_t ohci_hc_schedule(usb_transfer_batch_t *batch)
|
---|
[41b96b4] | 314 | {
|
---|
[32fb6bce] | 315 | assert(batch);
|
---|
| 316 |
|
---|
| 317 | ohci_bus_t *bus = (ohci_bus_t *) endpoint_get_bus(batch->ep);
|
---|
| 318 | hc_t *hc = bus->hc;
|
---|
| 319 | assert(hc);
|
---|
[9ff5ff82] | 320 |
|
---|
[02cacce] | 321 | /* Check for root hub communication */
|
---|
[32fb6bce] | 322 | if (batch->target.address == ohci_rh_get_address(&hc->rh)) {
|
---|
[a1732929] | 323 | usb_log_debug("OHCI root hub request.");
|
---|
[32fb6bce] | 324 | return ohci_rh_schedule(&hc->rh, batch);
|
---|
[41b96b4] | 325 | }
|
---|
[5fd9c30] | 326 |
|
---|
[d60115a] | 327 | endpoint_t *ep = batch->ep;
|
---|
[3bacee1] | 328 | ohci_endpoint_t *const ohci_ep = ohci_endpoint_get(ep);
|
---|
[4db49344] | 329 | ohci_transfer_batch_t *ohci_batch = ohci_transfer_batch_get(batch);
|
---|
| 330 | int err;
|
---|
| 331 |
|
---|
[0539c14] | 332 | if ((err = ohci_transfer_batch_prepare(ohci_batch)))
|
---|
| 333 | return err;
|
---|
| 334 |
|
---|
[4db49344] | 335 | fibril_mutex_lock(&hc->guard);
|
---|
| 336 | if ((err = endpoint_activate_locked(ep, batch))) {
|
---|
| 337 | fibril_mutex_unlock(&hc->guard);
|
---|
| 338 | return err;
|
---|
| 339 | }
|
---|
[d60115a] | 340 |
|
---|
[9c10e51] | 341 | ohci_transfer_batch_commit(ohci_batch);
|
---|
[4db49344] | 342 | list_append(&ohci_ep->pending_link, &hc->pending_endpoints);
|
---|
| 343 | fibril_mutex_unlock(&hc->guard);
|
---|
[02cacce] | 344 |
|
---|
| 345 | /* Control and bulk schedules need a kick to start working */
|
---|
[3bacee1] | 346 | switch (batch->ep->transfer_type) {
|
---|
[9ff5ff82] | 347 | case USB_TRANSFER_CONTROL:
|
---|
[32fb6bce] | 348 | OHCI_SET(hc->registers->command_status, CS_CLF);
|
---|
[9ff5ff82] | 349 | break;
|
---|
| 350 | case USB_TRANSFER_BULK:
|
---|
[32fb6bce] | 351 | OHCI_SET(hc->registers->command_status, CS_BLF);
|
---|
[9ff5ff82] | 352 | break;
|
---|
| 353 | default:
|
---|
| 354 | break;
|
---|
| 355 | }
|
---|
[d60115a] | 356 |
|
---|
[4c28d17] | 357 | return EOK;
|
---|
[41b96b4] | 358 | }
|
---|
[76fbd9a] | 359 |
|
---|
[02cacce] | 360 | /** Interrupt handling routine
|
---|
| 361 | *
|
---|
[fccf289] | 362 | * @param[in] hcd HCD driver structure.
|
---|
[02cacce] | 363 | * @param[in] status Value of the status register at the time of interrupt.
|
---|
| 364 | */
|
---|
[32fb6bce] | 365 | void ohci_hc_interrupt(bus_t *bus_base, uint32_t status)
|
---|
[41b96b4] | 366 | {
|
---|
[32fb6bce] | 367 | assert(bus_base);
|
---|
| 368 |
|
---|
| 369 | ohci_bus_t *bus = (ohci_bus_t *) bus_base;
|
---|
| 370 | hc_t *hc = bus->hc;
|
---|
| 371 | assert(hc);
|
---|
| 372 |
|
---|
[d1ca752] | 373 | status = OHCI_RD(status);
|
---|
[32fb6bce] | 374 | assert(hc);
|
---|
[561112f] | 375 | if ((status & ~I_SF) == 0) /* ignore sof status */
|
---|
[eaf1e3d] | 376 | return;
|
---|
[a1732929] | 377 | usb_log_debug2("OHCI(%p) interrupt: %x.", hc, status);
|
---|
[561112f] | 378 | if (status & I_RHSC)
|
---|
[32fb6bce] | 379 | ohci_rh_interrupt(&hc->rh);
|
---|
[7d6a676] | 380 |
|
---|
[561112f] | 381 | if (status & I_WDH) {
|
---|
[32fb6bce] | 382 | fibril_mutex_lock(&hc->guard);
|
---|
[a1732929] | 383 | usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).", hc->hcca,
|
---|
[32fb6bce] | 384 | OHCI_RD(hc->registers->hcca),
|
---|
| 385 | (void *) addr_to_phys(hc->hcca));
|
---|
[a1732929] | 386 | usb_log_debug2("Periodic current: %#" PRIx32 ".",
|
---|
[32fb6bce] | 387 | OHCI_RD(hc->registers->periodic_current));
|
---|
[eaf1e3d] | 388 |
|
---|
[d60115a] | 389 | list_foreach_safe(hc->pending_endpoints, current, next) {
|
---|
[3bacee1] | 390 | ohci_endpoint_t *ep =
|
---|
| 391 | list_get_instance(current, ohci_endpoint_t, pending_link);
|
---|
[d60115a] | 392 |
|
---|
[3bacee1] | 393 | ohci_transfer_batch_t *batch =
|
---|
| 394 | ohci_transfer_batch_get(ep->base.active_batch);
|
---|
[d60115a] | 395 | assert(batch);
|
---|
[7013b14] | 396 |
|
---|
[5fd9c30] | 397 | if (ohci_transfer_batch_check_completed(batch)) {
|
---|
[d60115a] | 398 | endpoint_deactivate_locked(&ep->base);
|
---|
[d6522dd] | 399 | list_remove(current);
|
---|
[c6f82e5] | 400 | hc_reset_toggles(&batch->base, &ohci_ep_toggle_reset);
|
---|
[5fd9c30] | 401 | usb_transfer_batch_finish(&batch->base);
|
---|
[7013b14] | 402 | }
|
---|
[eaf1e3d] | 403 | }
|
---|
[32fb6bce] | 404 | fibril_mutex_unlock(&hc->guard);
|
---|
[4c28d17] | 405 | }
|
---|
[68b9f148] | 406 |
|
---|
| 407 | if (status & I_UE) {
|
---|
[a1732929] | 408 | usb_log_fatal("Error like no other!");
|
---|
[32fb6bce] | 409 | hc_start(&hc->base);
|
---|
[68b9f148] | 410 | }
|
---|
| 411 |
|
---|
[41b96b4] | 412 | }
|
---|
[76fbd9a] | 413 |
|
---|
[02cacce] | 414 | /** Turn off any (BIOS)driver that might be in control of the device.
|
---|
[78ab6d4] | 415 | *
|
---|
| 416 | * This function implements routines described in chapter 5.1.1.3 of the OHCI
|
---|
| 417 | * specification (page 40, pdf page 54).
|
---|
[02cacce] | 418 | *
|
---|
| 419 | * @param[in] instance OHCI hc driver structure.
|
---|
| 420 | */
|
---|
[32fb6bce] | 421 | int hc_gain_control(hc_device_t *hcd)
|
---|
[2c617b0] | 422 | {
|
---|
[32fb6bce] | 423 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[78ab6d4] | 424 |
|
---|
[a1732929] | 425 | usb_log_debug("Requesting OHCI control.");
|
---|
[bfc5c9dd] | 426 | if (OHCI_RD(instance->registers->revision) & R_LEGACY_FLAG) {
|
---|
[7c3fb9b] | 427 | /*
|
---|
| 428 | * Turn off legacy emulation, it should be enough to zero
|
---|
[78ab6d4] | 429 | * the lowest bit, but it caused problems. Thus clear all
|
---|
| 430 | * except GateA20 (causes restart on some hw).
|
---|
| 431 | * See page 145 of the specs for details.
|
---|
| 432 | */
|
---|
| 433 | volatile uint32_t *ohci_emulation_reg =
|
---|
[3bacee1] | 434 | (uint32_t *)((char *)instance->registers + LEGACY_REGS_OFFSET);
|
---|
[a1732929] | 435 | usb_log_debug("OHCI legacy register %p: %x.",
|
---|
[bfc5c9dd] | 436 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
---|
[78ab6d4] | 437 | /* Zero everything but A20State */
|
---|
[58563585] | 438 | // TODO: should we ack interrupts before doing this?
|
---|
[bfc5c9dd] | 439 | OHCI_CLR(*ohci_emulation_reg, ~0x100);
|
---|
[78ab6d4] | 440 | usb_log_debug(
|
---|
[a1732929] | 441 | "OHCI legacy register (should be 0 or 0x100) %p: %x.",
|
---|
[bfc5c9dd] | 442 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
---|
[78ab6d4] | 443 | }
|
---|
[112d159] | 444 |
|
---|
[2c617b0] | 445 | /* Interrupt routing enabled => smm driver is active */
|
---|
[bfc5c9dd] | 446 | if (OHCI_RD(instance->registers->control) & C_IR) {
|
---|
[a1732929] | 447 | usb_log_debug("SMM driver: request ownership change.");
|
---|
[58563585] | 448 | // TODO: should we ack interrupts before doing this?
|
---|
[bfc5c9dd] | 449 | OHCI_SET(instance->registers->command_status, CS_OCR);
|
---|
[78ab6d4] | 450 | /* Hope that SMM actually knows its stuff or we can hang here */
|
---|
[f5bfd98] | 451 | while (OHCI_RD(instance->registers->control) & C_IR) {
|
---|
[5f97ef44] | 452 | fibril_usleep(1000);
|
---|
[2c617b0] | 453 | }
|
---|
[a1732929] | 454 | usb_log_info("SMM driver: Ownership taken.");
|
---|
[78ab6d4] | 455 | C_HCFS_SET(instance->registers->control, C_HCFS_RESET);
|
---|
[5f97ef44] | 456 | fibril_usleep(50000);
|
---|
[32fb6bce] | 457 | return EOK;
|
---|
[2c617b0] | 458 | }
|
---|
[8486c07] | 459 |
|
---|
[78ab6d4] | 460 | const unsigned hc_status = C_HCFS_GET(instance->registers->control);
|
---|
[2c617b0] | 461 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
| 462 | if (hc_status != C_HCFS_RESET) {
|
---|
[a1732929] | 463 | usb_log_debug("BIOS driver found.");
|
---|
[2c617b0] | 464 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
[a1732929] | 465 | usb_log_info("BIOS driver: HC operational.");
|
---|
[32fb6bce] | 466 | return EOK;
|
---|
[2c617b0] | 467 | }
|
---|
[bfc5c9dd] | 468 | /* HC is suspended assert resume for 20ms */
|
---|
[78ab6d4] | 469 | C_HCFS_SET(instance->registers->control, C_HCFS_RESUME);
|
---|
[5f97ef44] | 470 | fibril_usleep(20000);
|
---|
[a1732929] | 471 | usb_log_info("BIOS driver: HC resumed.");
|
---|
[32fb6bce] | 472 | return EOK;
|
---|
[2c617b0] | 473 | }
|
---|
| 474 |
|
---|
[7c3fb9b] | 475 | /*
|
---|
| 476 | * HC is in reset (hw startup) => no other driver
|
---|
| 477 | * maintain reset for at least the time specified in USB spec (50 ms)
|
---|
| 478 | */
|
---|
[a1732929] | 479 | usb_log_debug("Host controller found in reset state.");
|
---|
[5f97ef44] | 480 | fibril_usleep(50000);
|
---|
[32fb6bce] | 481 | return EOK;
|
---|
[2c617b0] | 482 | }
|
---|
[76fbd9a] | 483 |
|
---|
[02cacce] | 484 | /** OHCI hw initialization routine.
|
---|
| 485 | *
|
---|
| 486 | * @param[in] instance OHCI hc driver structure.
|
---|
| 487 | */
|
---|
[32fb6bce] | 488 | int hc_start(hc_device_t *hcd)
|
---|
[2c617b0] | 489 | {
|
---|
[32fb6bce] | 490 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[ee0ffa6] | 491 | ohci_rh_init(&instance->rh, instance->registers, &instance->guard, "ohci rh");
|
---|
[e4d7363] | 492 |
|
---|
[112d159] | 493 | /* OHCI guide page 42 */
|
---|
[2c617b0] | 494 | assert(instance);
|
---|
[a1732929] | 495 | usb_log_debug2("Started hc initialization routine.");
|
---|
[112d159] | 496 |
|
---|
| 497 | /* Save contents of fm_interval register */
|
---|
[bfc5c9dd] | 498 | const uint32_t fm_interval = OHCI_RD(instance->registers->fm_interval);
|
---|
[a1732929] | 499 | usb_log_debug2("Old value of HcFmInterval: %x.", fm_interval);
|
---|
[344925c] | 500 |
|
---|
[112d159] | 501 | /* Reset hc */
|
---|
[a1732929] | 502 | usb_log_debug2("HC reset.");
|
---|
[112d159] | 503 | size_t time = 0;
|
---|
[bfc5c9dd] | 504 | OHCI_WR(instance->registers->command_status, CS_HCR);
|
---|
| 505 | while (OHCI_RD(instance->registers->command_status) & CS_HCR) {
|
---|
[5f97ef44] | 506 | fibril_usleep(10);
|
---|
[112d159] | 507 | time += 10;
|
---|
| 508 | }
|
---|
[a1732929] | 509 | usb_log_debug2("HC reset complete in %zu us.", time);
|
---|
[344925c] | 510 |
|
---|
[112d159] | 511 | /* Restore fm_interval */
|
---|
[bfc5c9dd] | 512 | OHCI_WR(instance->registers->fm_interval, fm_interval);
|
---|
| 513 | assert((OHCI_RD(instance->registers->command_status) & CS_HCR) == 0);
|
---|
[344925c] | 514 |
|
---|
[2c617b0] | 515 | /* hc is now in suspend state */
|
---|
[a1732929] | 516 | usb_log_debug2("HC should be in suspend state(%x).",
|
---|
[bfc5c9dd] | 517 | OHCI_RD(instance->registers->control));
|
---|
[344925c] | 518 |
|
---|
[78d4e1f] | 519 | /* Use HCCA */
|
---|
[bfc5c9dd] | 520 | OHCI_WR(instance->registers->hcca, addr_to_phys(instance->hcca));
|
---|
[78d4e1f] | 521 |
|
---|
| 522 | /* Use queues */
|
---|
[bfc5c9dd] | 523 | OHCI_WR(instance->registers->bulk_head,
|
---|
| 524 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
---|
[a1732929] | 525 | usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").",
|
---|
[5a2c42b] | 526 | instance->lists[USB_TRANSFER_BULK].list_head,
|
---|
| 527 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
---|
[78d4e1f] | 528 |
|
---|
[bfc5c9dd] | 529 | OHCI_WR(instance->registers->control_head,
|
---|
| 530 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
---|
[a1732929] | 531 | usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").",
|
---|
[5a2c42b] | 532 | instance->lists[USB_TRANSFER_CONTROL].list_head,
|
---|
| 533 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
---|
[78d4e1f] | 534 |
|
---|
[112d159] | 535 | /* Enable queues */
|
---|
[65eac7b] | 536 | OHCI_SET(instance->registers->control, (C_PLE | C_IE | C_CLE | C_BLE));
|
---|
[a1732929] | 537 | usb_log_debug("Queues enabled(%x).",
|
---|
[65eac7b] | 538 | OHCI_RD(instance->registers->control));
|
---|
[112d159] | 539 |
|
---|
[561112f] | 540 | /* Enable interrupts */
|
---|
[bb97118] | 541 | if (cap_handle_valid(instance->base.irq_handle)) {
|
---|
[a5361fb] | 542 | OHCI_WR(instance->registers->interrupt_enable,
|
---|
| 543 | OHCI_USED_INTERRUPTS);
|
---|
[a1732929] | 544 | usb_log_debug("Enabled interrupts: %x.",
|
---|
[a5361fb] | 545 | OHCI_RD(instance->registers->interrupt_enable));
|
---|
| 546 | OHCI_WR(instance->registers->interrupt_enable, I_MI);
|
---|
| 547 | }
|
---|
[112d159] | 548 |
|
---|
| 549 | /* Set periodic start to 90% */
|
---|
[bfc5c9dd] | 550 | const uint32_t frame_length =
|
---|
| 551 | (fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK;
|
---|
| 552 | OHCI_WR(instance->registers->periodic_start,
|
---|
| 553 | ((frame_length / 10) * 9) & PS_MASK << PS_SHIFT);
|
---|
[a1732929] | 554 | usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).",
|
---|
[bfc5c9dd] | 555 | OHCI_RD(instance->registers->periodic_start),
|
---|
| 556 | OHCI_RD(instance->registers->periodic_start), frame_length);
|
---|
[78ab6d4] | 557 | C_HCFS_SET(instance->registers->control, C_HCFS_OPERATIONAL);
|
---|
[a1732929] | 558 | usb_log_debug("OHCI HC up and running (ctl_reg=0x%x).",
|
---|
[bfc5c9dd] | 559 | OHCI_RD(instance->registers->control));
|
---|
[32fb6bce] | 560 |
|
---|
| 561 | return EOK;
|
---|
[2c617b0] | 562 | }
|
---|
[76fbd9a] | 563 |
|
---|
[129b821f] | 564 | /**
|
---|
| 565 | * Setup roothub as a virtual hub.
|
---|
| 566 | */
|
---|
| 567 | int hc_setup_roothub(hc_device_t *hcd)
|
---|
| 568 | {
|
---|
| 569 | return hc_setup_virtual_root_hub(hcd, USB_SPEED_FULL);
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[02cacce] | 572 | /** Initialize schedule queues
|
---|
| 573 | *
|
---|
| 574 | * @param[in] instance OHCI hc driver structure
|
---|
| 575 | * @return Error code
|
---|
| 576 | */
|
---|
[5a6cc679] | 577 | errno_t hc_init_transfer_lists(hc_t *instance)
|
---|
[6b6e3ed3] | 578 | {
|
---|
| 579 | assert(instance);
|
---|
[5a2c42b] | 580 | #define SETUP_ENDPOINT_LIST(type) \
|
---|
[344925c] | 581 | do { \
|
---|
[5a2c42b] | 582 | const char *name = usb_str_transfer_type(type); \
|
---|
[5a6cc679] | 583 | const errno_t ret = endpoint_list_init(&instance->lists[type], name); \
|
---|
[6b6e3ed3] | 584 | if (ret != EOK) { \
|
---|
[a1732929] | 585 | usb_log_error("Failed to setup %s endpoint list: %s.", \
|
---|
[1cb4f05] | 586 | name, str_error(ret)); \
|
---|
[68b9f148] | 587 | endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]);\
|
---|
[5a2c42b] | 588 | endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
|
---|
| 589 | endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
|
---|
| 590 | endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
|
---|
[70c85320] | 591 | return ret; \
|
---|
[344925c] | 592 | } \
|
---|
| 593 | } while (0)
|
---|
[6b6e3ed3] | 594 |
|
---|
[5a2c42b] | 595 | SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
|
---|
| 596 | SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
|
---|
| 597 | SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
|
---|
| 598 | SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
|
---|
| 599 | #undef SETUP_ENDPOINT_LIST
|
---|
| 600 | endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
|
---|
| 601 | &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
|
---|
[6b6e3ed3] | 602 |
|
---|
| 603 | return EOK;
|
---|
| 604 | }
|
---|
[76fbd9a] | 605 |
|
---|
[02cacce] | 606 | /** Initialize memory structures used by the OHCI hcd.
|
---|
| 607 | *
|
---|
| 608 | * @param[in] instance OHCI hc driver structure.
|
---|
| 609 | * @return Error code.
|
---|
| 610 | */
|
---|
[5a6cc679] | 611 | errno_t hc_init_memory(hc_t *instance)
|
---|
[344925c] | 612 | {
|
---|
| 613 | assert(instance);
|
---|
[5d07f54] | 614 |
|
---|
[acdb5bac] | 615 | memset(&instance->rh, 0, sizeof(instance->rh));
|
---|
[8790650] | 616 | /* Init queues */
|
---|
[5a6cc679] | 617 | errno_t ret = hc_init_transfer_lists(instance);
|
---|
[8953514] | 618 | if (ret != EOK) {
|
---|
| 619 | return ret;
|
---|
| 620 | }
|
---|
[344925c] | 621 |
|
---|
[d1582b50] | 622 | /* Init HCCA */
|
---|
[f8dfb40] | 623 | instance->hcca = hcca_get();
|
---|
[344925c] | 624 | if (instance->hcca == NULL)
|
---|
| 625 | return ENOMEM;
|
---|
[a1732929] | 626 | usb_log_debug2("OHCI HCCA initialized at %p.", instance->hcca);
|
---|
[344925c] | 627 |
|
---|
[1b90e90] | 628 | for (unsigned i = 0; i < HCCA_INT_EP_COUNT; ++i) {
|
---|
| 629 | hcca_set_int_ep(instance->hcca, i,
|
---|
[65eac7b] | 630 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
---|
[344925c] | 631 | }
|
---|
[a1732929] | 632 | usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").",
|
---|
[5a2c42b] | 633 | instance->lists[USB_TRANSFER_INTERRUPT].list_head,
|
---|
| 634 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
---|
[344925c] | 635 |
|
---|
[32fb6bce] | 636 | if ((ret = ohci_bus_init(&instance->bus, instance))) {
|
---|
| 637 | usb_log_error("HC(%p): Failed to setup bus : %s",
|
---|
| 638 | instance, str_error(ret));
|
---|
| 639 | return ret;
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | hc_device_setup(&instance->base, (bus_t *) &instance->bus);
|
---|
| 643 |
|
---|
[344925c] | 644 | return EOK;
|
---|
| 645 | }
|
---|
[1ecc5de] | 646 |
|
---|
[41b96b4] | 647 | /**
|
---|
| 648 | * @}
|
---|
| 649 | */
|
---|