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