[6297465] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[6297465] | 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 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup drvusbehcihc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * @brief EHCI Host controller driver routines
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <assert.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 | #include <mem.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 43 | #include <stdint.h>
|
---|
[6297465] | 44 | #include <str_error.h>
|
---|
| 45 |
|
---|
| 46 | #include <usb/debug.h>
|
---|
| 47 | #include <usb/usb.h>
|
---|
[c6f82e5] | 48 | #include <usb/host/utility.h>
|
---|
[6297465] | 49 |
|
---|
[e9c5bd9] | 50 | #include "ehci_batch.h"
|
---|
[6297465] | 51 |
|
---|
| 52 | #include "hc.h"
|
---|
| 53 |
|
---|
| 54 | #define EHCI_USED_INTERRUPTS \
|
---|
[bfff7fd] | 55 | (USB_INTR_IRQ_FLAG | USB_INTR_ERR_IRQ_FLAG | USB_INTR_PORT_CHANGE_FLAG | \
|
---|
| 56 | USB_INTR_ASYNC_ADVANCE_FLAG | USB_INTR_HOST_ERR_FLAG)
|
---|
[6297465] | 57 |
|
---|
| 58 | static const irq_pio_range_t ehci_pio_ranges[] = {
|
---|
| 59 | {
|
---|
| 60 | .base = 0,
|
---|
| 61 | .size = sizeof(ehci_regs_t)
|
---|
| 62 | }
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | static const irq_cmd_t ehci_irq_commands[] = {
|
---|
| 66 | {
|
---|
| 67 | .cmd = CMD_PIO_READ_32,
|
---|
| 68 | .dstarg = 1,
|
---|
| 69 | .addr = NULL
|
---|
| 70 | },
|
---|
| 71 | {
|
---|
| 72 | .cmd = CMD_AND,
|
---|
| 73 | .srcarg = 1,
|
---|
| 74 | .dstarg = 2,
|
---|
| 75 | .value = 0
|
---|
| 76 | },
|
---|
| 77 | {
|
---|
| 78 | .cmd = CMD_PREDICATE,
|
---|
| 79 | .srcarg = 2,
|
---|
| 80 | .value = 2
|
---|
| 81 | },
|
---|
| 82 | {
|
---|
| 83 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
| 84 | .srcarg = 1,
|
---|
| 85 | .addr = NULL
|
---|
| 86 | },
|
---|
| 87 | {
|
---|
| 88 | .cmd = CMD_ACCEPT
|
---|
| 89 | }
|
---|
| 90 | };
|
---|
| 91 |
|
---|
[5a6cc679] | 92 | static errno_t hc_init_memory(hc_t *instance);
|
---|
[6297465] | 93 |
|
---|
| 94 | /** Generate IRQ code.
|
---|
| 95 | * @param[out] ranges PIO ranges buffer.
|
---|
[ba4a03a5] | 96 | * @param[in] hw_res Device's resources.
|
---|
[6297465] | 97 | *
|
---|
[68e5406] | 98 | * @param[out] irq
|
---|
| 99 | *
|
---|
[6297465] | 100 | * @return Error code.
|
---|
| 101 | */
|
---|
[5a6cc679] | 102 | errno_t hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res, int *irq)
|
---|
[6297465] | 103 | {
|
---|
| 104 | assert(code);
|
---|
[ba4a03a5] | 105 | assert(hw_res);
|
---|
[32fb6bce] | 106 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[e4d7363] | 107 |
|
---|
[ba4a03a5] | 108 | if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1)
|
---|
| 109 | return EINVAL;
|
---|
| 110 |
|
---|
| 111 | addr_range_t regs = hw_res->mem_ranges.ranges[0];
|
---|
| 112 |
|
---|
| 113 | if (RNGSZ(regs) < sizeof(ehci_regs_t))
|
---|
[6297465] | 114 | return EOVERFLOW;
|
---|
| 115 |
|
---|
| 116 | code->ranges = malloc(sizeof(ehci_pio_ranges));
|
---|
| 117 | if (code->ranges == NULL)
|
---|
| 118 | return ENOMEM;
|
---|
| 119 |
|
---|
| 120 | code->cmds = malloc(sizeof(ehci_irq_commands));
|
---|
| 121 | if (code->cmds == NULL) {
|
---|
| 122 | free(code->ranges);
|
---|
| 123 | return ENOMEM;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | code->rangecount = ARRAY_SIZE(ehci_pio_ranges);
|
---|
| 127 | code->cmdcount = ARRAY_SIZE(ehci_irq_commands);
|
---|
| 128 |
|
---|
| 129 | memcpy(code->ranges, ehci_pio_ranges, sizeof(ehci_pio_ranges));
|
---|
[ba4a03a5] | 130 | code->ranges[0].base = RNGABS(regs);
|
---|
[6297465] | 131 |
|
---|
| 132 | memcpy(code->cmds, ehci_irq_commands, sizeof(ehci_irq_commands));
|
---|
[dca8fe5] | 133 |
|
---|
[d09d108] | 134 | ehci_regs_t *registers =
|
---|
[3bacee1] | 135 | (ehci_regs_t *)(RNGABSPTR(regs) + EHCI_RD8(instance->caps->caplength));
|
---|
[d09d108] | 136 | code->cmds[0].addr = (void *) ®isters->usbsts;
|
---|
| 137 | code->cmds[3].addr = (void *) ®isters->usbsts;
|
---|
[6297465] | 138 | EHCI_WR(code->cmds[1].value, EHCI_USED_INTERRUPTS);
|
---|
| 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;
|
---|
[6297465] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /** Initialize EHCI hc driver structure
|
---|
| 148 | *
|
---|
| 149 | * @param[in] instance Memory place for the structure.
|
---|
| 150 | * @param[in] regs Device's I/O registers range.
|
---|
| 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)
|
---|
[6297465] | 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 <
|
---|
[3bacee1] | 160 | (sizeof(ehci_caps_regs_t) + sizeof(ehci_regs_t)))
|
---|
| 161 | return EINVAL;
|
---|
[6297465] | 162 |
|
---|
[5a6cc679] | 163 | errno_t ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
|
---|
[7813516] | 164 | (void **)&instance->caps);
|
---|
[6297465] | 165 | if (ret != EOK) {
|
---|
[05b51e37] | 166 | usb_log_error("HC(%p): Failed to gain access to device "
|
---|
[a1732929] | 167 | "registers: %s.", instance, str_error(ret));
|
---|
[6297465] | 168 | return ret;
|
---|
| 169 | }
|
---|
[e4d7363] | 170 |
|
---|
[3bacee1] | 171 | usb_log_info("HC(%p): Device registers at %" PRIx64 " (%zuB) accessible.",
|
---|
[05b51e37] | 172 | instance, hw_res->mem_ranges.ranges[0].address.absolute,
|
---|
[7813516] | 173 | hw_res->mem_ranges.ranges[0].size);
|
---|
[6297465] | 174 | instance->registers =
|
---|
[3bacee1] | 175 | (void *)instance->caps + EHCI_RD8(instance->caps->caplength);
|
---|
[05b51e37] | 176 | usb_log_info("HC(%p): Device control registers at %" PRIx64, instance,
|
---|
[3bacee1] | 177 | hw_res->mem_ranges.ranges[0].address.absolute +
|
---|
| 178 | EHCI_RD8(instance->caps->caplength));
|
---|
[6297465] | 179 |
|
---|
[8ad2b0a] | 180 | list_initialize(&instance->pending_endpoints);
|
---|
[6297465] | 181 | fibril_mutex_initialize(&instance->guard);
|
---|
[763dbcb] | 182 | fibril_condvar_initialize(&instance->async_doorbell);
|
---|
[6297465] | 183 |
|
---|
| 184 | ret = hc_init_memory(instance);
|
---|
| 185 | if (ret != EOK) {
|
---|
[05b51e37] | 186 | usb_log_error("HC(%p): Failed to create EHCI memory structures:"
|
---|
| 187 | " %s.", instance, str_error(ret));
|
---|
[6297465] | 188 | return ret;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[05b51e37] | 191 | usb_log_info("HC(%p): Initializing RH(%p).", instance, &instance->rh);
|
---|
[6297465] | 192 | ehci_rh_init(
|
---|
[ddbd088] | 193 | &instance->rh, instance->caps, instance->registers, &instance->guard,
|
---|
| 194 | "ehci rh");
|
---|
[6297465] | 195 |
|
---|
[32fb6bce] | 196 | ehci_bus_init(&instance->bus, instance);
|
---|
| 197 | hc_device_setup(hcd, (bus_t *) &instance->bus);
|
---|
[6297465] | 198 | return EOK;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[7813516] | 201 | /** Safely dispose host controller internal structures
|
---|
| 202 | *
|
---|
| 203 | * @param[in] instance Host controller structure to use.
|
---|
| 204 | */
|
---|
[35c37fc] | 205 | int hc_gone(hc_device_t *hcd)
|
---|
[7813516] | 206 | {
|
---|
[35c37fc] | 207 | hc_t *hc = hcd_to_hc(hcd);
|
---|
| 208 | endpoint_list_fini(&hc->async_list);
|
---|
| 209 | endpoint_list_fini(&hc->int_list);
|
---|
| 210 | dma_buffer_free(&hc->dma_buffer);
|
---|
[32fb6bce] | 211 | return EOK;
|
---|
[84239b1] | 212 | }
|
---|
[7813516] | 213 |
|
---|
[6297465] | 214 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
| 215 | {
|
---|
[5f5321ee] | 216 | assert(instance);
|
---|
| 217 | assert(ep);
|
---|
| 218 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
---|
[a1732929] | 219 | usb_log_debug("HC(%p) enqueue EP(%d:%d:%s:%s)", instance,
|
---|
[a5b3de6] | 220 | ep->device->address, ep->endpoint,
|
---|
[92900e2] | 221 | usb_str_transfer_type_short(ep->transfer_type),
|
---|
| 222 | usb_str_direction(ep->direction));
|
---|
[3bacee1] | 223 | switch (ep->transfer_type) {
|
---|
[5f5321ee] | 224 | case USB_TRANSFER_CONTROL:
|
---|
| 225 | case USB_TRANSFER_BULK:
|
---|
| 226 | endpoint_list_append_ep(&instance->async_list, ehci_ep);
|
---|
| 227 | break;
|
---|
| 228 | case USB_TRANSFER_INTERRUPT:
|
---|
| 229 | endpoint_list_append_ep(&instance->int_list, ehci_ep);
|
---|
| 230 | break;
|
---|
| 231 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
| 232 | /* NOT SUPPORTED */
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
[6297465] | 235 | }
|
---|
| 236 |
|
---|
| 237 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
| 238 | {
|
---|
[763dbcb] | 239 | assert(instance);
|
---|
| 240 | assert(ep);
|
---|
| 241 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
---|
[7278cbc9] | 242 | usb_log_debug("HC(%p) dequeue EP(%d:%d:%s:%s)", instance,
|
---|
| 243 | ep->device->address, ep->endpoint,
|
---|
[92900e2] | 244 | usb_str_transfer_type_short(ep->transfer_type),
|
---|
| 245 | usb_str_direction(ep->direction));
|
---|
[3bacee1] | 246 | switch (ep->transfer_type) {
|
---|
[763dbcb] | 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 |
|
---|
[5a6cc679] | 266 | errno_t 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 | */
|
---|
[5a6cc679] | 290 | errno_t 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 |
|
---|
[3bacee1] | 305 | endpoint_t *const ep = batch->ep;
|
---|
| 306 | ehci_endpoint_t *const ehci_ep = ehci_endpoint_get(ep);
|
---|
[4db49344] | 307 | ehci_transfer_batch_t *ehci_batch = ehci_transfer_batch_get(batch);
|
---|
[8ad2b0a] | 308 |
|
---|
[4db49344] | 309 | int err;
|
---|
[5fd9c30] | 310 |
|
---|
[4db49344] | 311 | if ((err = ehci_transfer_batch_prepare(ehci_batch)))
|
---|
| 312 | return err;
|
---|
[8ad2b0a] | 313 |
|
---|
[4db49344] | 314 | fibril_mutex_lock(&hc->guard);
|
---|
| 315 |
|
---|
| 316 | if ((err = endpoint_activate_locked(ep, batch))) {
|
---|
| 317 | fibril_mutex_unlock(&hc->guard);
|
---|
[5fd9c30] | 318 | return err;
|
---|
[8ad2b0a] | 319 | }
|
---|
[e9c5bd9] | 320 |
|
---|
[32fb6bce] | 321 | usb_log_debug("HC(%p): Committing BATCH(%p)", hc, batch);
|
---|
[e9c5bd9] | 322 | ehci_transfer_batch_commit(ehci_batch);
|
---|
[8ad2b0a] | 323 |
|
---|
| 324 | /* Enqueue endpoint to the checked list */
|
---|
| 325 | usb_log_debug2("HC(%p): Appending BATCH(%p)", hc, batch);
|
---|
| 326 | list_append(&ehci_ep->pending_link, &hc->pending_endpoints);
|
---|
| 327 |
|
---|
[4db49344] | 328 | fibril_mutex_unlock(&hc->guard);
|
---|
[e9c5bd9] | 329 | return EOK;
|
---|
[6297465] | 330 | }
|
---|
| 331 |
|
---|
| 332 | /** Interrupt handling routine
|
---|
| 333 | *
|
---|
[c9e954c] | 334 | * @param[in] hcd HCD driver structure.
|
---|
[6297465] | 335 | * @param[in] status Value of the status register at the time of interrupt.
|
---|
| 336 | */
|
---|
[32fb6bce] | 337 | void ehci_hc_interrupt(bus_t *bus_base, uint32_t status)
|
---|
[6297465] | 338 | {
|
---|
[32fb6bce] | 339 | assert(bus_base);
|
---|
| 340 |
|
---|
| 341 | ehci_bus_t *bus = (ehci_bus_t *) bus_base;
|
---|
| 342 | hc_t *hc = bus->hc;
|
---|
| 343 | assert(hc);
|
---|
[07645906] | 344 |
|
---|
[3bacee1] | 345 | usb_log_debug2("HC(%p): Interrupt: %" PRIx32, hc, status);
|
---|
[6297465] | 346 | if (status & USB_STS_PORT_CHANGE_FLAG) {
|
---|
[32fb6bce] | 347 | ehci_rh_interrupt(&hc->rh);
|
---|
[6297465] | 348 | }
|
---|
[07645906] | 349 |
|
---|
[580b330] | 350 | if (status & USB_STS_IRQ_ASYNC_ADVANCE_FLAG) {
|
---|
[32fb6bce] | 351 | fibril_mutex_lock(&hc->guard);
|
---|
| 352 | usb_log_debug2("HC(%p): Signaling doorbell", hc);
|
---|
| 353 | fibril_condvar_broadcast(&hc->async_doorbell);
|
---|
| 354 | fibril_mutex_unlock(&hc->guard);
|
---|
[763dbcb] | 355 | }
|
---|
[07645906] | 356 |
|
---|
[e9c5bd9] | 357 | if (status & (USB_STS_IRQ_FLAG | USB_STS_ERR_IRQ_FLAG)) {
|
---|
[32fb6bce] | 358 | fibril_mutex_lock(&hc->guard);
|
---|
[e9c5bd9] | 359 |
|
---|
[8ad2b0a] | 360 | usb_log_debug2("HC(%p): Scanning %lu pending endpoints", hc,
|
---|
[3bacee1] | 361 | list_count(&hc->pending_endpoints));
|
---|
[8ad2b0a] | 362 | list_foreach_safe(hc->pending_endpoints, current, next) {
|
---|
[3bacee1] | 363 | ehci_endpoint_t *ep =
|
---|
| 364 | list_get_instance(current, ehci_endpoint_t, pending_link);
|
---|
[8ad2b0a] | 365 |
|
---|
[3bacee1] | 366 | ehci_transfer_batch_t *batch =
|
---|
| 367 | ehci_transfer_batch_get(ep->base.active_batch);
|
---|
[8ad2b0a] | 368 | assert(batch);
|
---|
[e9c5bd9] | 369 |
|
---|
[5fd9c30] | 370 | if (ehci_transfer_batch_check_completed(batch)) {
|
---|
[8ad2b0a] | 371 | endpoint_deactivate_locked(&ep->base);
|
---|
[e9c5bd9] | 372 | list_remove(current);
|
---|
[c6f82e5] | 373 | hc_reset_toggles(&batch->base, &ehci_ep_toggle_reset);
|
---|
[5fd9c30] | 374 | usb_transfer_batch_finish(&batch->base);
|
---|
[e9c5bd9] | 375 | }
|
---|
| 376 | }
|
---|
[32fb6bce] | 377 | fibril_mutex_unlock(&hc->guard);
|
---|
[8ad2b0a] | 378 |
|
---|
| 379 |
|
---|
[e9c5bd9] | 380 | }
|
---|
| 381 |
|
---|
[07645906] | 382 | if (status & USB_STS_HOST_ERROR_FLAG) {
|
---|
[32fb6bce] | 383 | usb_log_fatal("HCD(%p): HOST SYSTEM ERROR!", hc);
|
---|
[07645906] | 384 | //TODO do something here
|
---|
| 385 | }
|
---|
[6297465] | 386 | }
|
---|
| 387 |
|
---|
| 388 | /** EHCI hw initialization routine.
|
---|
| 389 | *
|
---|
| 390 | * @param[in] instance EHCI hc driver structure.
|
---|
| 391 | */
|
---|
[32fb6bce] | 392 | int hc_start(hc_device_t *hcd)
|
---|
[6297465] | 393 | {
|
---|
[32fb6bce] | 394 | hc_t *instance = hcd_to_hc(hcd);
|
---|
[e4d7363] | 395 | usb_log_debug("HC(%p): Starting HW.", instance);
|
---|
| 396 |
|
---|
[7c3fb9b] | 397 | /*
|
---|
| 398 | * Turn off the HC if it's running, Reseting a running device is
|
---|
| 399 | * undefined
|
---|
| 400 | */
|
---|
[478e243] | 401 | if (!(EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG)) {
|
---|
| 402 | /* disable all interrupts */
|
---|
| 403 | EHCI_WR(instance->registers->usbintr, 0);
|
---|
| 404 | /* ack all interrupts */
|
---|
| 405 | EHCI_WR(instance->registers->usbsts, 0x3f);
|
---|
| 406 | /* Stop HC hw */
|
---|
| 407 | EHCI_WR(instance->registers->usbcmd, 0);
|
---|
| 408 | /* Wait until hc is halted */
|
---|
| 409 | while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0) {
|
---|
| 410 | async_usleep(1);
|
---|
| 411 | }
|
---|
[05b51e37] | 412 | usb_log_info("HC(%p): EHCI turned off.", instance);
|
---|
[478e243] | 413 | } else {
|
---|
[05b51e37] | 414 | usb_log_info("HC(%p): EHCI was not running.", instance);
|
---|
[478e243] | 415 | }
|
---|
| 416 |
|
---|
| 417 | /* Hw initialization sequence, see page 53 (pdf 63) */
|
---|
| 418 | EHCI_SET(instance->registers->usbcmd, USB_CMD_HC_RESET_FLAG);
|
---|
[05b51e37] | 419 | usb_log_info("HC(%p): Waiting for HW reset.", instance);
|
---|
[478e243] | 420 | while (EHCI_RD(instance->registers->usbcmd) & USB_CMD_HC_RESET_FLAG) {
|
---|
| 421 | async_usleep(1);
|
---|
| 422 | }
|
---|
[05b51e37] | 423 | usb_log_debug("HC(%p): HW reset OK.", instance);
|
---|
| 424 |
|
---|
[763dbcb] | 425 | /* Use the lowest 4G segment */
|
---|
[478e243] | 426 | EHCI_WR(instance->registers->ctrldssegment, 0);
|
---|
[3eb0c85] | 427 |
|
---|
| 428 | /* Enable periodic list */
|
---|
[35c37fc] | 429 | assert(instance->periodic_list);
|
---|
[50362c6] | 430 | uintptr_t phys_base =
|
---|
[3bacee1] | 431 | addr_to_phys((void *)instance->periodic_list);
|
---|
[478e243] | 432 | assert((phys_base & USB_PERIODIC_LIST_BASE_MASK) == phys_base);
|
---|
| 433 | EHCI_WR(instance->registers->periodiclistbase, phys_base);
|
---|
[44b9b44] | 434 | EHCI_SET(instance->registers->usbcmd, USB_CMD_PERIODIC_SCHEDULE_FLAG);
|
---|
[05b51e37] | 435 | usb_log_debug("HC(%p): Enabled periodic list.", instance);
|
---|
[478e243] | 436 |
|
---|
[763dbcb] | 437 |
|
---|
[0a751aa] | 438 | /* Enable Async schedule */
|
---|
[3bacee1] | 439 | phys_base = addr_to_phys((void *)instance->async_list.list_head);
|
---|
[50362c6] | 440 | assert((phys_base & USB_ASYNCLIST_MASK) == phys_base);
|
---|
| 441 | EHCI_WR(instance->registers->asynclistaddr, phys_base);
|
---|
[0a751aa] | 442 | EHCI_SET(instance->registers->usbcmd, USB_CMD_ASYNC_SCHEDULE_FLAG);
|
---|
[05b51e37] | 443 | usb_log_debug("HC(%p): Enabled async list.", instance);
|
---|
[44b9b44] | 444 |
|
---|
[3eb0c85] | 445 | /* Start hc and get all ports */
|
---|
[44b9b44] | 446 | EHCI_SET(instance->registers->usbcmd, USB_CMD_RUN_FLAG);
|
---|
| 447 | EHCI_SET(instance->registers->configflag, USB_CONFIG_FLAG_FLAG);
|
---|
[05b51e37] | 448 | usb_log_debug("HC(%p): HW started.", instance);
|
---|
| 449 |
|
---|
[a1732929] | 450 | usb_log_debug2("HC(%p): Registers: "
|
---|
| 451 | "\tUSBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)"
|
---|
| 452 | "\tUSBSTS(%p): %x(0x00001000 = HC halted)"
|
---|
| 453 | "\tUSBINT(%p): %x(0x0 = no interrupts)."
|
---|
| 454 | "\tCONFIG(%p): %x(0x0 = ports controlled by companion hc).",
|
---|
[05b51e37] | 455 | instance,
|
---|
[615abda] | 456 | &instance->registers->usbcmd, EHCI_RD(instance->registers->usbcmd),
|
---|
| 457 | &instance->registers->usbsts, EHCI_RD(instance->registers->usbsts),
|
---|
| 458 | &instance->registers->usbintr, EHCI_RD(instance->registers->usbintr),
|
---|
| 459 | &instance->registers->configflag, EHCI_RD(instance->registers->configflag));
|
---|
[495547d] | 460 | /* Clear and Enable interrupts */
|
---|
| 461 | EHCI_WR(instance->registers->usbsts, EHCI_RD(instance->registers->usbsts));
|
---|
| 462 | EHCI_WR(instance->registers->usbintr, EHCI_USED_INTERRUPTS);
|
---|
[e4d7363] | 463 |
|
---|
| 464 | return EOK;
|
---|
[6297465] | 465 | }
|
---|
| 466 |
|
---|
[129b821f] | 467 | /**
|
---|
| 468 | * Setup roothub as a virtual hub.
|
---|
| 469 | */
|
---|
| 470 | int hc_setup_roothub(hc_device_t *hcd)
|
---|
| 471 | {
|
---|
| 472 | return hc_setup_virtual_root_hub(hcd, USB_SPEED_HIGH);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[6297465] | 475 | /** Initialize memory structures used by the EHCI hcd.
|
---|
| 476 | *
|
---|
| 477 | * @param[in] instance EHCI hc driver structure.
|
---|
| 478 | * @return Error code.
|
---|
| 479 | */
|
---|
[5a6cc679] | 480 | errno_t hc_init_memory(hc_t *instance)
|
---|
[6297465] | 481 | {
|
---|
[478e243] | 482 | assert(instance);
|
---|
[05b51e37] | 483 | usb_log_debug2("HC(%p): Initializing Async list(%p).", instance,
|
---|
| 484 | &instance->async_list);
|
---|
[5a6cc679] | 485 | errno_t ret = endpoint_list_init(&instance->async_list, "ASYNC");
|
---|
[5f5321ee] | 486 | if (ret != EOK) {
|
---|
[05b51e37] | 487 | usb_log_error("HC(%p): Failed to setup ASYNC list: %s",
|
---|
| 488 | instance, str_error(ret));
|
---|
[5f5321ee] | 489 | return ret;
|
---|
| 490 | }
|
---|
[7c3fb9b] | 491 | /*
|
---|
| 492 | * Specs say "Software must set queue head horizontal pointer T-bits to
|
---|
[50362c6] | 493 | * a zero for queue heads in the asynchronous schedule" (4.4.0).
|
---|
| 494 | * So we must maintain circular buffer (all horizontal pointers
|
---|
[7c3fb9b] | 495 | * have to be valid
|
---|
| 496 | */
|
---|
[50362c6] | 497 | endpoint_list_chain(&instance->async_list, &instance->async_list);
|
---|
[5f5321ee] | 498 |
|
---|
[05b51e37] | 499 | usb_log_debug2("HC(%p): Initializing Interrupt list (%p).", instance,
|
---|
| 500 | &instance->int_list);
|
---|
[5f5321ee] | 501 | ret = endpoint_list_init(&instance->int_list, "INT");
|
---|
| 502 | if (ret != EOK) {
|
---|
[05b51e37] | 503 | usb_log_error("HC(%p): Failed to setup INT list: %s",
|
---|
| 504 | instance, str_error(ret));
|
---|
[5f5321ee] | 505 | endpoint_list_fini(&instance->async_list);
|
---|
| 506 | return ret;
|
---|
| 507 | }
|
---|
[478e243] | 508 |
|
---|
| 509 | /* Take 1024 periodic list heads, we ignore low mem options */
|
---|
[35c37fc] | 510 | if (dma_buffer_alloc(&instance->dma_buffer, PAGE_SIZE)) {
|
---|
[05b51e37] | 511 | usb_log_error("HC(%p): Failed to get ISO schedule page.",
|
---|
| 512 | instance);
|
---|
[5f5321ee] | 513 | endpoint_list_fini(&instance->async_list);
|
---|
| 514 | endpoint_list_fini(&instance->int_list);
|
---|
[478e243] | 515 | return ENOMEM;
|
---|
[5f5321ee] | 516 | }
|
---|
[35c37fc] | 517 | instance->periodic_list = instance->dma_buffer.virt;
|
---|
[05b51e37] | 518 |
|
---|
| 519 | usb_log_debug2("HC(%p): Initializing Periodic list.", instance);
|
---|
[3bacee1] | 520 | for (unsigned i = 0; i < PAGE_SIZE / sizeof(link_pointer_t); ++i) {
|
---|
[478e243] | 521 | /* Disable everything for now */
|
---|
[35c37fc] | 522 | instance->periodic_list[i] =
|
---|
[50362c6] | 523 | LINK_POINTER_QH(addr_to_phys(instance->int_list.list_head));
|
---|
[478e243] | 524 | }
|
---|
[6297465] | 525 | return EOK;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /**
|
---|
| 529 | * @}
|
---|
| 530 | */
|
---|