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