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