| [6297465] | 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 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbehcihc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief EHCI Host controller driver routines
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| [8d2dd7f2] | 42 | #include <stdint.h>
|
|---|
| [6297465] | 43 | #include <str_error.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <usb/debug.h>
|
|---|
| 46 | #include <usb/usb.h>
|
|---|
| [45cbf897] | 47 | #include <usb/host/utils/malloc32.h>
|
|---|
| [6297465] | 48 |
|
|---|
| [e9c5bd9] | 49 | #include "ehci_batch.h"
|
|---|
| [6297465] | 50 |
|
|---|
| 51 | #include "hc.h"
|
|---|
| 52 |
|
|---|
| 53 | #define EHCI_USED_INTERRUPTS \
|
|---|
| [bfff7fd] | 54 | (USB_INTR_IRQ_FLAG | USB_INTR_ERR_IRQ_FLAG | USB_INTR_PORT_CHANGE_FLAG | \
|
|---|
| 55 | USB_INTR_ASYNC_ADVANCE_FLAG | USB_INTR_HOST_ERR_FLAG)
|
|---|
| [6297465] | 56 |
|
|---|
| 57 | static const irq_pio_range_t ehci_pio_ranges[] = {
|
|---|
| 58 | {
|
|---|
| 59 | .base = 0,
|
|---|
| 60 | .size = sizeof(ehci_regs_t)
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static const irq_cmd_t ehci_irq_commands[] = {
|
|---|
| 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,
|
|---|
| 74 | .value = 0
|
|---|
| 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 | }
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | static int hc_init_memory(hc_t *instance);
|
|---|
| 92 |
|
|---|
| 93 | /** Generate IRQ code.
|
|---|
| 94 | * @param[out] ranges PIO ranges buffer.
|
|---|
| [ba4a03a5] | 95 | * @param[in] hw_res Device's resources.
|
|---|
| [6297465] | 96 | *
|
|---|
| 97 | * @return Error code.
|
|---|
| 98 | */
|
|---|
| [32fb6bce] | 99 | int hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
|---|
| [6297465] | 100 | {
|
|---|
| 101 | assert(code);
|
|---|
| [ba4a03a5] | 102 | assert(hw_res);
|
|---|
| [32fb6bce] | 103 | hc_t *instance = hcd_to_hc(hcd);
|
|---|
| [e4d7363] | 104 |
|
|---|
| [ba4a03a5] | 105 | if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1)
|
|---|
| 106 | return EINVAL;
|
|---|
| 107 |
|
|---|
| 108 | addr_range_t regs = hw_res->mem_ranges.ranges[0];
|
|---|
| 109 |
|
|---|
| 110 | if (RNGSZ(regs) < sizeof(ehci_regs_t))
|
|---|
| [6297465] | 111 | return EOVERFLOW;
|
|---|
| 112 |
|
|---|
| 113 | code->ranges = malloc(sizeof(ehci_pio_ranges));
|
|---|
| 114 | if (code->ranges == NULL)
|
|---|
| 115 | return ENOMEM;
|
|---|
| 116 |
|
|---|
| 117 | code->cmds = malloc(sizeof(ehci_irq_commands));
|
|---|
| 118 | if (code->cmds == NULL) {
|
|---|
| 119 | free(code->ranges);
|
|---|
| 120 | return ENOMEM;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | code->rangecount = ARRAY_SIZE(ehci_pio_ranges);
|
|---|
| 124 | code->cmdcount = ARRAY_SIZE(ehci_irq_commands);
|
|---|
| 125 |
|
|---|
| 126 | memcpy(code->ranges, ehci_pio_ranges, sizeof(ehci_pio_ranges));
|
|---|
| [ba4a03a5] | 127 | code->ranges[0].base = RNGABS(regs);
|
|---|
| [6297465] | 128 |
|
|---|
| 129 | memcpy(code->cmds, ehci_irq_commands, sizeof(ehci_irq_commands));
|
|---|
| [dca8fe5] | 130 |
|
|---|
| [d09d108] | 131 | ehci_regs_t *registers =
|
|---|
| 132 | (ehci_regs_t *)(RNGABSPTR(regs) + EHCI_RD8(instance->caps->caplength));
|
|---|
| 133 | code->cmds[0].addr = (void *) ®isters->usbsts;
|
|---|
| 134 | code->cmds[3].addr = (void *) ®isters->usbsts;
|
|---|
| [6297465] | 135 | EHCI_WR(code->cmds[1].value, EHCI_USED_INTERRUPTS);
|
|---|
| 136 |
|
|---|
| [ba4a03a5] | 137 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
|---|
| 138 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
|---|
| 139 |
|
|---|
| 140 | return hw_res->irqs.irqs[0];
|
|---|
| [6297465] | 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Initialize EHCI hc driver structure
|
|---|
| 144 | *
|
|---|
| 145 | * @param[in] instance Memory place for the structure.
|
|---|
| 146 | * @param[in] regs Device's I/O registers range.
|
|---|
| 147 | * @param[in] interrupts True if w interrupts should be used
|
|---|
| 148 | * @return Error code
|
|---|
| 149 | */
|
|---|
| [32fb6bce] | 150 | int hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
|
|---|
| [6297465] | 151 | {
|
|---|
| [32fb6bce] | 152 | hc_t *instance = hcd_to_hc(hcd);
|
|---|
| [7813516] | 153 | assert(hw_res);
|
|---|
| 154 | if (hw_res->mem_ranges.count != 1 ||
|
|---|
| 155 | hw_res->mem_ranges.ranges[0].size <
|
|---|
| 156 | (sizeof(ehci_caps_regs_t) + sizeof(ehci_regs_t)))
|
|---|
| 157 | return EINVAL;
|
|---|
| [6297465] | 158 |
|
|---|
| [7813516] | 159 | int ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
|
|---|
| 160 | (void **)&instance->caps);
|
|---|
| [6297465] | 161 | if (ret != EOK) {
|
|---|
| [05b51e37] | 162 | usb_log_error("HC(%p): Failed to gain access to device "
|
|---|
| 163 | "registers: %s.\n", instance, str_error(ret));
|
|---|
| [6297465] | 164 | return ret;
|
|---|
| 165 | }
|
|---|
| [e4d7363] | 166 |
|
|---|
| [05b51e37] | 167 | usb_log_info("HC(%p): Device registers at %"PRIx64" (%zuB) accessible.",
|
|---|
| 168 | instance, hw_res->mem_ranges.ranges[0].address.absolute,
|
|---|
| [7813516] | 169 | hw_res->mem_ranges.ranges[0].size);
|
|---|
| [6297465] | 170 | instance->registers =
|
|---|
| 171 | (void*)instance->caps + EHCI_RD8(instance->caps->caplength);
|
|---|
| [05b51e37] | 172 | usb_log_info("HC(%p): Device control registers at %" PRIx64, instance,
|
|---|
| [d97f91f] | 173 | hw_res->mem_ranges.ranges[0].address.absolute
|
|---|
| 174 | + EHCI_RD8(instance->caps->caplength));
|
|---|
| [6297465] | 175 |
|
|---|
| 176 | list_initialize(&instance->pending_batches);
|
|---|
| 177 | fibril_mutex_initialize(&instance->guard);
|
|---|
| [763dbcb] | 178 | fibril_condvar_initialize(&instance->async_doorbell);
|
|---|
| [6297465] | 179 |
|
|---|
| 180 | ret = hc_init_memory(instance);
|
|---|
| 181 | if (ret != EOK) {
|
|---|
| [05b51e37] | 182 | usb_log_error("HC(%p): Failed to create EHCI memory structures:"
|
|---|
| 183 | " %s.", instance, str_error(ret));
|
|---|
| [6297465] | 184 | return ret;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| [05b51e37] | 187 | usb_log_info("HC(%p): Initializing RH(%p).", instance, &instance->rh);
|
|---|
| [6297465] | 188 | ehci_rh_init(
|
|---|
| 189 | &instance->rh, instance->caps, instance->registers, "ehci rh");
|
|---|
| 190 |
|
|---|
| [32fb6bce] | 191 | ehci_bus_init(&instance->bus, instance);
|
|---|
| 192 | hc_device_setup(hcd, (bus_t *) &instance->bus);
|
|---|
| [6297465] | 193 | return EOK;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| [7813516] | 196 | /** Safely dispose host controller internal structures
|
|---|
| 197 | *
|
|---|
| 198 | * @param[in] instance Host controller structure to use.
|
|---|
| 199 | */
|
|---|
| [32fb6bce] | 200 | int hc_gone(hc_device_t *instance)
|
|---|
| [7813516] | 201 | {
|
|---|
| 202 | assert(instance);
|
|---|
| [32fb6bce] | 203 | return EOK;
|
|---|
| [5f5321ee] | 204 | //TODO: stop the hw
|
|---|
| 205 | #if 0
|
|---|
| 206 | endpoint_list_fini(&instance->async_list);
|
|---|
| 207 | endpoint_list_fini(&instance->int_list);
|
|---|
| 208 | return_page(instance->periodic_list_base);
|
|---|
| 209 | #endif
|
|---|
| [7813516] | 210 | };
|
|---|
| 211 |
|
|---|
| [6297465] | 212 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| 213 | {
|
|---|
| [5f5321ee] | 214 | assert(instance);
|
|---|
| 215 | assert(ep);
|
|---|
| 216 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| [05b51e37] | 217 | usb_log_debug("HC(%p) enqueue EP(%d:%d:%s:%s)\n", instance,
|
|---|
| [a5b3de6] | 218 | ep->device->address, ep->endpoint,
|
|---|
| [92900e2] | 219 | usb_str_transfer_type_short(ep->transfer_type),
|
|---|
| 220 | usb_str_direction(ep->direction));
|
|---|
| [5f5321ee] | 221 | switch (ep->transfer_type)
|
|---|
| 222 | {
|
|---|
| 223 | case USB_TRANSFER_CONTROL:
|
|---|
| 224 | case USB_TRANSFER_BULK:
|
|---|
| 225 | endpoint_list_append_ep(&instance->async_list, ehci_ep);
|
|---|
| 226 | break;
|
|---|
| 227 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 228 | endpoint_list_append_ep(&instance->int_list, ehci_ep);
|
|---|
| 229 | break;
|
|---|
| 230 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 231 | /* NOT SUPPORTED */
|
|---|
| 232 | break;
|
|---|
| 233 | }
|
|---|
| [6297465] | 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| 237 | {
|
|---|
| [763dbcb] | 238 | assert(instance);
|
|---|
| 239 | assert(ep);
|
|---|
| 240 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| [306a36d] | 241 | usb_log_debug("HC(%p) dequeue EP(?:%d:%s:%s)\n", instance,
|
|---|
| 242 | ep->endpoint,
|
|---|
| [92900e2] | 243 | usb_str_transfer_type_short(ep->transfer_type),
|
|---|
| 244 | usb_str_direction(ep->direction));
|
|---|
| [763dbcb] | 245 | switch (ep->transfer_type)
|
|---|
| 246 | {
|
|---|
| 247 | case USB_TRANSFER_INTERRUPT:
|
|---|
| [ce735cc2] | 248 | endpoint_list_remove_ep(&instance->int_list, ehci_ep);
|
|---|
| [763dbcb] | 249 | /* Fall through */
|
|---|
| 250 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 251 | /* NOT SUPPORTED */
|
|---|
| 252 | return;
|
|---|
| 253 | case USB_TRANSFER_CONTROL:
|
|---|
| 254 | case USB_TRANSFER_BULK:
|
|---|
| 255 | endpoint_list_remove_ep(&instance->async_list, ehci_ep);
|
|---|
| 256 | break;
|
|---|
| 257 | }
|
|---|
| 258 | fibril_mutex_lock(&instance->guard);
|
|---|
| [05b51e37] | 259 | usb_log_debug("HC(%p): Waiting for doorbell", instance);
|
|---|
| [1803b7d] | 260 | EHCI_SET(instance->registers->usbcmd, USB_CMD_IRQ_ASYNC_DOORBELL);
|
|---|
| [763dbcb] | 261 | fibril_condvar_wait(&instance->async_doorbell, &instance->guard);
|
|---|
| [05b51e37] | 262 | usb_log_debug2("HC(%p): Got doorbell", instance);
|
|---|
| [763dbcb] | 263 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [6297465] | 264 | }
|
|---|
| 265 |
|
|---|
| [32fb6bce] | 266 | int ehci_hc_status(bus_t *bus_base, uint32_t *status)
|
|---|
| [c9e954c] | 267 | {
|
|---|
| [32fb6bce] | 268 | assert(bus_base);
|
|---|
| [c9e954c] | 269 | assert(status);
|
|---|
| [32fb6bce] | 270 |
|
|---|
| 271 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 272 | hc_t *hc = bus->hc;
|
|---|
| 273 | assert(hc);
|
|---|
| 274 |
|
|---|
| [c9e954c] | 275 | *status = 0;
|
|---|
| [32fb6bce] | 276 | if (hc->registers) {
|
|---|
| 277 | *status = EHCI_RD(hc->registers->usbsts);
|
|---|
| 278 | EHCI_WR(hc->registers->usbsts, *status);
|
|---|
| [c9e954c] | 279 | }
|
|---|
| [32fb6bce] | 280 | usb_log_debug2("HC(%p): Read status: %x", hc, *status);
|
|---|
| [c9e954c] | 281 | return EOK;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| [6297465] | 284 | /** Add USB transfer to the schedule.
|
|---|
| 285 | *
|
|---|
| [c9e954c] | 286 | * @param[in] hcd HCD driver structure.
|
|---|
| [6297465] | 287 | * @param[in] batch Batch representing the transfer.
|
|---|
| 288 | * @return Error code.
|
|---|
| 289 | */
|
|---|
| [32fb6bce] | 290 | int ehci_hc_schedule(usb_transfer_batch_t *batch)
|
|---|
| [6297465] | 291 | {
|
|---|
| [32fb6bce] | 292 | assert(batch);
|
|---|
| 293 |
|
|---|
| 294 | ehci_bus_t *bus = (ehci_bus_t *) endpoint_get_bus(batch->ep);
|
|---|
| 295 | hc_t *hc = bus->hc;
|
|---|
| 296 | assert(hc);
|
|---|
| [6297465] | 297 |
|
|---|
| 298 | /* Check for root hub communication */
|
|---|
| [32fb6bce] | 299 | if (batch->target.address == ehci_rh_get_address(&hc->rh)) {
|
|---|
| [05b51e37] | 300 | usb_log_debug("HC(%p): Scheduling BATCH(%p) for RH(%p)",
|
|---|
| [32fb6bce] | 301 | hc, batch, &hc->rh);
|
|---|
| 302 | return ehci_rh_schedule(&hc->rh, batch);
|
|---|
| [6297465] | 303 | }
|
|---|
| [5fd9c30] | 304 |
|
|---|
| [e9c5bd9] | 305 | ehci_transfer_batch_t *ehci_batch = ehci_transfer_batch_get(batch);
|
|---|
| [5fd9c30] | 306 |
|
|---|
| 307 | const int err = ehci_transfer_batch_prepare(ehci_batch);
|
|---|
| 308 | if (err)
|
|---|
| 309 | return err;
|
|---|
| [e9c5bd9] | 310 |
|
|---|
| [32fb6bce] | 311 | fibril_mutex_lock(&hc->guard);
|
|---|
| 312 | usb_log_debug2("HC(%p): Appending BATCH(%p)", hc, batch);
|
|---|
| 313 | list_append(&ehci_batch->link, &hc->pending_batches);
|
|---|
| 314 | usb_log_debug("HC(%p): Committing BATCH(%p)", hc, batch);
|
|---|
| [e9c5bd9] | 315 | ehci_transfer_batch_commit(ehci_batch);
|
|---|
| 316 |
|
|---|
| [32fb6bce] | 317 | fibril_mutex_unlock(&hc->guard);
|
|---|
| [e9c5bd9] | 318 | return EOK;
|
|---|
| [6297465] | 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /** Interrupt handling routine
|
|---|
| 322 | *
|
|---|
| [c9e954c] | 323 | * @param[in] hcd HCD driver structure.
|
|---|
| [6297465] | 324 | * @param[in] status Value of the status register at the time of interrupt.
|
|---|
| 325 | */
|
|---|
| [32fb6bce] | 326 | void ehci_hc_interrupt(bus_t *bus_base, uint32_t status)
|
|---|
| [6297465] | 327 | {
|
|---|
| [32fb6bce] | 328 | assert(bus_base);
|
|---|
| 329 |
|
|---|
| 330 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
|---|
| 331 | hc_t *hc = bus->hc;
|
|---|
| 332 | assert(hc);
|
|---|
| [07645906] | 333 |
|
|---|
| [32fb6bce] | 334 | usb_log_debug2("HC(%p): Interrupt: %"PRIx32, hc, status);
|
|---|
| [6297465] | 335 | if (status & USB_STS_PORT_CHANGE_FLAG) {
|
|---|
| [32fb6bce] | 336 | ehci_rh_interrupt(&hc->rh);
|
|---|
| [6297465] | 337 | }
|
|---|
| [07645906] | 338 |
|
|---|
| [580b330] | 339 | if (status & USB_STS_IRQ_ASYNC_ADVANCE_FLAG) {
|
|---|
| [32fb6bce] | 340 | fibril_mutex_lock(&hc->guard);
|
|---|
| 341 | usb_log_debug2("HC(%p): Signaling doorbell", hc);
|
|---|
| 342 | fibril_condvar_broadcast(&hc->async_doorbell);
|
|---|
| 343 | fibril_mutex_unlock(&hc->guard);
|
|---|
| [763dbcb] | 344 | }
|
|---|
| [07645906] | 345 |
|
|---|
| [e9c5bd9] | 346 | if (status & (USB_STS_IRQ_FLAG | USB_STS_ERR_IRQ_FLAG)) {
|
|---|
| [32fb6bce] | 347 | fibril_mutex_lock(&hc->guard);
|
|---|
| [e9c5bd9] | 348 |
|
|---|
| [32fb6bce] | 349 | usb_log_debug2("HC(%p): Scanning %lu pending batches", hc,
|
|---|
| 350 | list_count(&hc->pending_batches));
|
|---|
| 351 | list_foreach_safe(hc->pending_batches, current, next) {
|
|---|
| [e9c5bd9] | 352 | ehci_transfer_batch_t *batch =
|
|---|
| 353 | ehci_transfer_batch_from_link(current);
|
|---|
| 354 |
|
|---|
| [5fd9c30] | 355 | if (ehci_transfer_batch_check_completed(batch)) {
|
|---|
| [e9c5bd9] | 356 | list_remove(current);
|
|---|
| [5fd9c30] | 357 | usb_transfer_batch_finish(&batch->base);
|
|---|
| [e9c5bd9] | 358 | }
|
|---|
| 359 | }
|
|---|
| [32fb6bce] | 360 | fibril_mutex_unlock(&hc->guard);
|
|---|
| [e9c5bd9] | 361 | }
|
|---|
| 362 |
|
|---|
| [07645906] | 363 | if (status & USB_STS_HOST_ERROR_FLAG) {
|
|---|
| [32fb6bce] | 364 | usb_log_fatal("HCD(%p): HOST SYSTEM ERROR!", hc);
|
|---|
| [07645906] | 365 | //TODO do something here
|
|---|
| 366 | }
|
|---|
| [6297465] | 367 | }
|
|---|
| 368 |
|
|---|
| 369 | /** EHCI hw initialization routine.
|
|---|
| 370 | *
|
|---|
| 371 | * @param[in] instance EHCI hc driver structure.
|
|---|
| 372 | */
|
|---|
| [32fb6bce] | 373 | int hc_start(hc_device_t *hcd)
|
|---|
| [6297465] | 374 | {
|
|---|
| [32fb6bce] | 375 | hc_t *instance = hcd_to_hc(hcd);
|
|---|
| [e4d7363] | 376 | usb_log_debug("HC(%p): Starting HW.", instance);
|
|---|
| 377 |
|
|---|
| [3eb0c85] | 378 | /* Turn off the HC if it's running, Reseting a running device is
|
|---|
| [478e243] | 379 | * undefined */
|
|---|
| 380 | if (!(EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG)) {
|
|---|
| 381 | /* disable all interrupts */
|
|---|
| 382 | EHCI_WR(instance->registers->usbintr, 0);
|
|---|
| 383 | /* ack all interrupts */
|
|---|
| 384 | EHCI_WR(instance->registers->usbsts, 0x3f);
|
|---|
| 385 | /* Stop HC hw */
|
|---|
| 386 | EHCI_WR(instance->registers->usbcmd, 0);
|
|---|
| 387 | /* Wait until hc is halted */
|
|---|
| 388 | while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0) {
|
|---|
| 389 | async_usleep(1);
|
|---|
| 390 | }
|
|---|
| [05b51e37] | 391 | usb_log_info("HC(%p): EHCI turned off.", instance);
|
|---|
| [478e243] | 392 | } else {
|
|---|
| [05b51e37] | 393 | usb_log_info("HC(%p): EHCI was not running.", instance);
|
|---|
| [478e243] | 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /* Hw initialization sequence, see page 53 (pdf 63) */
|
|---|
| 397 | EHCI_SET(instance->registers->usbcmd, USB_CMD_HC_RESET_FLAG);
|
|---|
| [05b51e37] | 398 | usb_log_info("HC(%p): Waiting for HW reset.", instance);
|
|---|
| [478e243] | 399 | while (EHCI_RD(instance->registers->usbcmd) & USB_CMD_HC_RESET_FLAG) {
|
|---|
| 400 | async_usleep(1);
|
|---|
| 401 | }
|
|---|
| [05b51e37] | 402 | usb_log_debug("HC(%p): HW reset OK.", instance);
|
|---|
| 403 |
|
|---|
| [763dbcb] | 404 | /* Use the lowest 4G segment */
|
|---|
| [478e243] | 405 | EHCI_WR(instance->registers->ctrldssegment, 0);
|
|---|
| [3eb0c85] | 406 |
|
|---|
| 407 | /* Enable periodic list */
|
|---|
| [478e243] | 408 | assert(instance->periodic_list_base);
|
|---|
| [50362c6] | 409 | uintptr_t phys_base =
|
|---|
| [478e243] | 410 | addr_to_phys((void*)instance->periodic_list_base);
|
|---|
| 411 | assert((phys_base & USB_PERIODIC_LIST_BASE_MASK) == phys_base);
|
|---|
| 412 | EHCI_WR(instance->registers->periodiclistbase, phys_base);
|
|---|
| [44b9b44] | 413 | EHCI_SET(instance->registers->usbcmd, USB_CMD_PERIODIC_SCHEDULE_FLAG);
|
|---|
| [05b51e37] | 414 | usb_log_debug("HC(%p): Enabled periodic list.", instance);
|
|---|
| [478e243] | 415 |
|
|---|
| [763dbcb] | 416 |
|
|---|
| [0a751aa] | 417 | /* Enable Async schedule */
|
|---|
| [50362c6] | 418 | phys_base = addr_to_phys((void*)instance->async_list.list_head);
|
|---|
| 419 | assert((phys_base & USB_ASYNCLIST_MASK) == phys_base);
|
|---|
| 420 | EHCI_WR(instance->registers->asynclistaddr, phys_base);
|
|---|
| [0a751aa] | 421 | EHCI_SET(instance->registers->usbcmd, USB_CMD_ASYNC_SCHEDULE_FLAG);
|
|---|
| [05b51e37] | 422 | usb_log_debug("HC(%p): Enabled async list.", instance);
|
|---|
| [44b9b44] | 423 |
|
|---|
| [3eb0c85] | 424 | /* Start hc and get all ports */
|
|---|
| [44b9b44] | 425 | EHCI_SET(instance->registers->usbcmd, USB_CMD_RUN_FLAG);
|
|---|
| 426 | EHCI_SET(instance->registers->configflag, USB_CONFIG_FLAG_FLAG);
|
|---|
| [05b51e37] | 427 | usb_log_debug("HC(%p): HW started.", instance);
|
|---|
| 428 |
|
|---|
| 429 | usb_log_debug2("HC(%p): Registers: \n"
|
|---|
| 430 | "\tUSBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)\n"
|
|---|
| 431 | "\tUSBSTS(%p): %x(0x00001000 = HC halted)\n"
|
|---|
| 432 | "\tUSBINT(%p): %x(0x0 = no interrupts).\n"
|
|---|
| 433 | "\tCONFIG(%p): %x(0x0 = ports controlled by companion hc).\n",
|
|---|
| 434 | instance,
|
|---|
| [615abda] | 435 | &instance->registers->usbcmd, EHCI_RD(instance->registers->usbcmd),
|
|---|
| 436 | &instance->registers->usbsts, EHCI_RD(instance->registers->usbsts),
|
|---|
| 437 | &instance->registers->usbintr, EHCI_RD(instance->registers->usbintr),
|
|---|
| 438 | &instance->registers->configflag, EHCI_RD(instance->registers->configflag));
|
|---|
| [495547d] | 439 | /* Clear and Enable interrupts */
|
|---|
| 440 | EHCI_WR(instance->registers->usbsts, EHCI_RD(instance->registers->usbsts));
|
|---|
| 441 | EHCI_WR(instance->registers->usbintr, EHCI_USED_INTERRUPTS);
|
|---|
| [e4d7363] | 442 |
|
|---|
| 443 | return EOK;
|
|---|
| [6297465] | 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /** Initialize memory structures used by the EHCI hcd.
|
|---|
| 447 | *
|
|---|
| 448 | * @param[in] instance EHCI hc driver structure.
|
|---|
| 449 | * @return Error code.
|
|---|
| 450 | */
|
|---|
| 451 | int hc_init_memory(hc_t *instance)
|
|---|
| 452 | {
|
|---|
| [478e243] | 453 | assert(instance);
|
|---|
| [05b51e37] | 454 | usb_log_debug2("HC(%p): Initializing Async list(%p).", instance,
|
|---|
| 455 | &instance->async_list);
|
|---|
| [5f5321ee] | 456 | int ret = endpoint_list_init(&instance->async_list, "ASYNC");
|
|---|
| 457 | if (ret != EOK) {
|
|---|
| [05b51e37] | 458 | usb_log_error("HC(%p): Failed to setup ASYNC list: %s",
|
|---|
| 459 | instance, str_error(ret));
|
|---|
| [5f5321ee] | 460 | return ret;
|
|---|
| 461 | }
|
|---|
| [50362c6] | 462 | /* Specs say "Software must set queue head horizontal pointer T-bits to
|
|---|
| 463 | * a zero for queue heads in the asynchronous schedule" (4.4.0).
|
|---|
| 464 | * So we must maintain circular buffer (all horizontal pointers
|
|---|
| 465 | * have to be valid */
|
|---|
| 466 | endpoint_list_chain(&instance->async_list, &instance->async_list);
|
|---|
| [5f5321ee] | 467 |
|
|---|
| [05b51e37] | 468 | usb_log_debug2("HC(%p): Initializing Interrupt list (%p).", instance,
|
|---|
| 469 | &instance->int_list);
|
|---|
| [5f5321ee] | 470 | ret = endpoint_list_init(&instance->int_list, "INT");
|
|---|
| 471 | if (ret != EOK) {
|
|---|
| [05b51e37] | 472 | usb_log_error("HC(%p): Failed to setup INT list: %s",
|
|---|
| 473 | instance, str_error(ret));
|
|---|
| [5f5321ee] | 474 | endpoint_list_fini(&instance->async_list);
|
|---|
| 475 | return ret;
|
|---|
| 476 | }
|
|---|
| [478e243] | 477 |
|
|---|
| 478 | /* Take 1024 periodic list heads, we ignore low mem options */
|
|---|
| 479 | instance->periodic_list_base = get_page();
|
|---|
| [5f5321ee] | 480 | if (!instance->periodic_list_base) {
|
|---|
| [05b51e37] | 481 | usb_log_error("HC(%p): Failed to get ISO schedule page.",
|
|---|
| 482 | instance);
|
|---|
| [5f5321ee] | 483 | endpoint_list_fini(&instance->async_list);
|
|---|
| 484 | endpoint_list_fini(&instance->int_list);
|
|---|
| [478e243] | 485 | return ENOMEM;
|
|---|
| [5f5321ee] | 486 | }
|
|---|
| [05b51e37] | 487 |
|
|---|
| 488 | usb_log_debug2("HC(%p): Initializing Periodic list.", instance);
|
|---|
| [478e243] | 489 | for (unsigned i = 0;
|
|---|
| 490 | i < PAGE_SIZE/sizeof(instance->periodic_list_base[0]); ++i)
|
|---|
| 491 | {
|
|---|
| 492 | /* Disable everything for now */
|
|---|
| [5f5321ee] | 493 | instance->periodic_list_base[i] =
|
|---|
| [50362c6] | 494 | LINK_POINTER_QH(addr_to_phys(instance->int_list.list_head));
|
|---|
| [478e243] | 495 | }
|
|---|
| [6297465] | 496 | return EOK;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /**
|
|---|
| 500 | * @}
|
|---|
| 501 | */
|
|---|