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