| 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"
|
|---|
| 49 | //#include "ehci_batch.h"
|
|---|
| 50 | #include "utils/malloc32.h"
|
|---|
| 51 |
|
|---|
| 52 | #include "hc.h"
|
|---|
| 53 |
|
|---|
| 54 | #define EHCI_USED_INTERRUPTS \
|
|---|
| 55 | (USB_INTR_IRQ_FLAG | USB_INTR_ERR_IRQ_FLAG | USB_INTR_PORT_CHANGE_FLAG)
|
|---|
| 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 void hc_start(hc_t *instance);
|
|---|
| 92 | static int hc_init_memory(hc_t *instance);
|
|---|
| 93 |
|
|---|
| 94 | /** Generate IRQ code.
|
|---|
| 95 | * @param[out] ranges PIO ranges buffer.
|
|---|
| 96 | * @param[in] hw_res Device's resources.
|
|---|
| 97 | *
|
|---|
| 98 | * @return Error code.
|
|---|
| 99 | */
|
|---|
| 100 | int ehci_hc_gen_irq_code(irq_code_t *code, const hw_res_list_parsed_t *hw_res)
|
|---|
| 101 | {
|
|---|
| 102 | assert(code);
|
|---|
| 103 | assert(hw_res);
|
|---|
| 104 |
|
|---|
| 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))
|
|---|
| 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));
|
|---|
| 127 | code->ranges[0].base = RNGABS(regs);
|
|---|
| 128 |
|
|---|
| 129 | memcpy(code->cmds, ehci_irq_commands, sizeof(ehci_irq_commands));
|
|---|
| 130 | ehci_caps_regs_t *caps = NULL;
|
|---|
| 131 | int ret = pio_enable_range(®s, (void**)&caps);
|
|---|
| 132 | if (ret != EOK) {
|
|---|
| 133 | return ret;
|
|---|
| 134 | }
|
|---|
| 135 | ehci_regs_t *registers =
|
|---|
| 136 | (ehci_regs_t *)(RNGABSPTR(regs) + EHCI_RD8(caps->caplength));
|
|---|
| 137 | code->cmds[0].addr = (void *) ®isters->usbsts;
|
|---|
| 138 | code->cmds[3].addr = (void *) ®isters->usbsts;
|
|---|
| 139 | EHCI_WR(code->cmds[1].value, EHCI_USED_INTERRUPTS);
|
|---|
| 140 |
|
|---|
| 141 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
|---|
| 142 | RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
|
|---|
| 143 |
|
|---|
| 144 | return hw_res->irqs.irqs[0];
|
|---|
| 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 | */
|
|---|
| 154 | int hc_init(hc_t *instance, const hw_res_list_parsed_t *hw_res, bool interrupts)
|
|---|
| 155 | {
|
|---|
| 156 | assert(instance);
|
|---|
| 157 | assert(hw_res);
|
|---|
| 158 | if (hw_res->mem_ranges.count != 1 ||
|
|---|
| 159 | hw_res->mem_ranges.ranges[0].size <
|
|---|
| 160 | (sizeof(ehci_caps_regs_t) + sizeof(ehci_regs_t)))
|
|---|
| 161 | return EINVAL;
|
|---|
| 162 |
|
|---|
| 163 | int ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
|
|---|
| 164 | (void **)&instance->caps);
|
|---|
| 165 | if (ret != EOK) {
|
|---|
| 166 | usb_log_error("Failed to gain access to device registers: %s.\n",
|
|---|
| 167 | str_error(ret));
|
|---|
| 168 | return ret;
|
|---|
| 169 | }
|
|---|
| 170 | usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.\n",
|
|---|
| 171 | hw_res->mem_ranges.ranges[0].address.absolute,
|
|---|
| 172 | hw_res->mem_ranges.ranges[0].size);
|
|---|
| 173 | instance->registers =
|
|---|
| 174 | (void*)instance->caps + EHCI_RD8(instance->caps->caplength);
|
|---|
| 175 |
|
|---|
| 176 | list_initialize(&instance->pending_batches);
|
|---|
| 177 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 178 |
|
|---|
| 179 | ret = hc_init_memory(instance);
|
|---|
| 180 | if (ret != EOK) {
|
|---|
| 181 | usb_log_error("Failed to create EHCI memory structures: %s.\n",
|
|---|
| 182 | str_error(ret));
|
|---|
| 183 | return ret;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | ehci_rh_init(
|
|---|
| 187 | &instance->rh, instance->caps, instance->registers, "ehci rh");
|
|---|
| 188 | hc_start(instance);
|
|---|
| 189 |
|
|---|
| 190 | return EOK;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /** Safely dispose host controller internal structures
|
|---|
| 194 | *
|
|---|
| 195 | * @param[in] instance Host controller structure to use.
|
|---|
| 196 | */
|
|---|
| 197 | void hc_fini(hc_t *instance)
|
|---|
| 198 | {
|
|---|
| 199 | assert(instance);
|
|---|
| 200 | //TODO: stop the hw
|
|---|
| 201 | #if 0
|
|---|
| 202 | endpoint_list_fini(&instance->async_list);
|
|---|
| 203 | endpoint_list_fini(&instance->int_list);
|
|---|
| 204 | return_page(instance->periodic_list_base);
|
|---|
| 205 | #endif
|
|---|
| 206 | };
|
|---|
| 207 |
|
|---|
| 208 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| 209 | {
|
|---|
| 210 | assert(instance);
|
|---|
| 211 | assert(ep);
|
|---|
| 212 | ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
|
|---|
| 213 | switch (ep->transfer_type)
|
|---|
| 214 | {
|
|---|
| 215 | case USB_TRANSFER_CONTROL:
|
|---|
| 216 | endpoint_list_prepend_ep(&instance->async_list, ehci_ep);
|
|---|
| 217 | break;
|
|---|
| 218 | case USB_TRANSFER_BULK:
|
|---|
| 219 | endpoint_list_append_ep(&instance->async_list, ehci_ep);
|
|---|
| 220 | break;
|
|---|
| 221 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 222 | endpoint_list_append_ep(&instance->int_list, ehci_ep);
|
|---|
| 223 | break;
|
|---|
| 224 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 225 | /* NOT SUPPORTED */
|
|---|
| 226 | break;
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
|---|
| 231 | {
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | int ehci_hc_status(hcd_t *hcd, uint32_t *status)
|
|---|
| 235 | {
|
|---|
| 236 | assert(hcd);
|
|---|
| 237 | hc_t *instance = hcd->driver.data;
|
|---|
| 238 | assert(instance);
|
|---|
| 239 | assert(status);
|
|---|
| 240 | *status = 0;
|
|---|
| 241 | if (instance->registers) {
|
|---|
| 242 | *status = EHCI_RD(instance->registers->usbsts);
|
|---|
| 243 | EHCI_WR(instance->registers->usbsts, *status);
|
|---|
| 244 | }
|
|---|
| 245 | return EOK;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** Add USB transfer to the schedule.
|
|---|
| 249 | *
|
|---|
| 250 | * @param[in] hcd HCD driver structure.
|
|---|
| 251 | * @param[in] batch Batch representing the transfer.
|
|---|
| 252 | * @return Error code.
|
|---|
| 253 | */
|
|---|
| 254 | int ehci_hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
|---|
| 255 | {
|
|---|
| 256 | assert(hcd);
|
|---|
| 257 | hc_t *instance = hcd->driver.data;
|
|---|
| 258 | assert(instance);
|
|---|
| 259 |
|
|---|
| 260 | /* Check for root hub communication */
|
|---|
| 261 | if (batch->ep->address == ehci_rh_get_address(&instance->rh)) {
|
|---|
| 262 | usb_log_debug("EHCI root hub request.\n");
|
|---|
| 263 | return ehci_rh_schedule(&instance->rh, batch);
|
|---|
| 264 | }
|
|---|
| 265 | return ENOTSUP;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** Interrupt handling routine
|
|---|
| 269 | *
|
|---|
| 270 | * @param[in] hcd HCD driver structure.
|
|---|
| 271 | * @param[in] status Value of the status register at the time of interrupt.
|
|---|
| 272 | */
|
|---|
| 273 | void ehci_hc_interrupt(hcd_t *hcd, uint32_t status)
|
|---|
| 274 | {
|
|---|
| 275 | assert(hcd);
|
|---|
| 276 | hc_t *instance = hcd->driver.data;
|
|---|
| 277 | status = EHCI_RD(status);
|
|---|
| 278 | assert(instance);
|
|---|
| 279 | if (status & USB_STS_PORT_CHANGE_FLAG) {
|
|---|
| 280 | ehci_rh_interrupt(&instance->rh);
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** EHCI hw initialization routine.
|
|---|
| 285 | *
|
|---|
| 286 | * @param[in] instance EHCI hc driver structure.
|
|---|
| 287 | */
|
|---|
| 288 | void hc_start(hc_t *instance)
|
|---|
| 289 | {
|
|---|
| 290 | assert(instance);
|
|---|
| 291 | /* Turn of the HC if it's running, Reseting a running device is
|
|---|
| 292 | * undefined */
|
|---|
| 293 | if (!(EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG)) {
|
|---|
| 294 | /* disable all interrupts */
|
|---|
| 295 | EHCI_WR(instance->registers->usbintr, 0);
|
|---|
| 296 | /* ack all interrupts */
|
|---|
| 297 | EHCI_WR(instance->registers->usbsts, 0x3f);
|
|---|
| 298 | /* Stop HC hw */
|
|---|
| 299 | EHCI_WR(instance->registers->usbcmd, 0);
|
|---|
| 300 | /* Wait until hc is halted */
|
|---|
| 301 | while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0) {
|
|---|
| 302 | async_usleep(1);
|
|---|
| 303 | }
|
|---|
| 304 | usb_log_info("EHCI turned off.\n");
|
|---|
| 305 | } else {
|
|---|
| 306 | usb_log_info("EHCI was not running.\n");
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /* Hw initialization sequence, see page 53 (pdf 63) */
|
|---|
| 310 | EHCI_SET(instance->registers->usbcmd, USB_CMD_HC_RESET_FLAG);
|
|---|
| 311 | while (EHCI_RD(instance->registers->usbcmd) & USB_CMD_HC_RESET_FLAG) {
|
|---|
| 312 | async_usleep(1);
|
|---|
| 313 | }
|
|---|
| 314 | /* Enable interrupts */
|
|---|
| 315 | EHCI_WR(instance->registers->usbintr, USB_INTR_PORT_CHANGE_FLAG | USB_INTR_IRQ_FLAG);
|
|---|
| 316 | /* Use lower 4G segment */
|
|---|
| 317 | EHCI_WR(instance->registers->ctrldssegment, 0);
|
|---|
| 318 | /* Set periodic list */
|
|---|
| 319 | assert(instance->periodic_list_base);
|
|---|
| 320 | const uintptr_t phys_base =
|
|---|
| 321 | addr_to_phys((void*)instance->periodic_list_base);
|
|---|
| 322 | assert((phys_base & USB_PERIODIC_LIST_BASE_MASK) == phys_base);
|
|---|
| 323 | EHCI_WR(instance->registers->periodiclistbase, phys_base);
|
|---|
| 324 |
|
|---|
| 325 | /* start hc and get all ports */
|
|---|
| 326 | EHCI_SET(instance->registers->usbcmd, USB_CMD_RUN_FLAG);
|
|---|
| 327 | EHCI_SET(instance->registers->configflag, USB_CONFIG_FLAG_FLAG);
|
|---|
| 328 | #if 0
|
|---|
| 329 | /*
|
|---|
| 330 | * TURN OFF EHCI FOR NOW
|
|---|
| 331 | */
|
|---|
| 332 | usb_log_debug("USBCMD value: %x.\n",
|
|---|
| 333 | EHCI_RD(instance->registers->usbcmd));
|
|---|
| 334 | if (EHCI_RD(instance->registers->usbcmd) & USB_CMD_RUN_FLAG) {
|
|---|
| 335 | /* disable all interrupts */
|
|---|
| 336 | EHCI_WR(instance->registers->usbintr, 0);
|
|---|
| 337 | /* ack all interrupts */
|
|---|
| 338 | EHCI_WR(instance->registers->usbsts, 0x3f);
|
|---|
| 339 | /* release RH ports */
|
|---|
| 340 | EHCI_WR(instance->registers->configflag, 0);
|
|---|
| 341 | EHCI_WR(instance->registers->usbcmd, 0);
|
|---|
| 342 | /* Wait until hc is halted */
|
|---|
| 343 | while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
|
|---|
| 344 | usb_log_info("EHCI turned off.\n");
|
|---|
| 345 | } else {
|
|---|
| 346 | usb_log_info("EHCI was not running.\n");
|
|---|
| 347 | }
|
|---|
| 348 | #endif
|
|---|
| 349 | usb_log_debug("Registers: \n"
|
|---|
| 350 | "\t USBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)\n"
|
|---|
| 351 | "\t USBSTS(%p): %x(0x00001000 = HC halted)\n"
|
|---|
| 352 | "\t USBINT(%p): %x(0x0 = no interrupts).\n"
|
|---|
| 353 | "\t CONFIG(%p): %x(0x0 = ports controlled by companion hc).\n",
|
|---|
| 354 | &instance->registers->usbcmd, EHCI_RD(instance->registers->usbcmd),
|
|---|
| 355 | &instance->registers->usbsts, EHCI_RD(instance->registers->usbsts),
|
|---|
| 356 | &instance->registers->usbintr, EHCI_RD(instance->registers->usbintr),
|
|---|
| 357 | &instance->registers->configflag, EHCI_RD(instance->registers->configflag));
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | /** Initialize memory structures used by the EHCI hcd.
|
|---|
| 361 | *
|
|---|
| 362 | * @param[in] instance EHCI hc driver structure.
|
|---|
| 363 | * @return Error code.
|
|---|
| 364 | */
|
|---|
| 365 | int hc_init_memory(hc_t *instance)
|
|---|
| 366 | {
|
|---|
| 367 | assert(instance);
|
|---|
| 368 | int ret = endpoint_list_init(&instance->async_list, "ASYNC");
|
|---|
| 369 | if (ret != EOK) {
|
|---|
| 370 | usb_log_error("Failed to setup ASYNC list: %s", str_error(ret));
|
|---|
| 371 | return ret;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | ret = endpoint_list_init(&instance->int_list, "INT");
|
|---|
| 375 | if (ret != EOK) {
|
|---|
| 376 | usb_log_error("Failed to setup INT list: %s", str_error(ret));
|
|---|
| 377 | endpoint_list_fini(&instance->async_list);
|
|---|
| 378 | return ret;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /* Take 1024 periodic list heads, we ignore low mem options */
|
|---|
| 382 | instance->periodic_list_base = get_page();
|
|---|
| 383 | if (!instance->periodic_list_base) {
|
|---|
| 384 | usb_log_error("Failed to get ISO schedule page.");
|
|---|
| 385 | endpoint_list_fini(&instance->async_list);
|
|---|
| 386 | endpoint_list_fini(&instance->int_list);
|
|---|
| 387 | return ENOMEM;
|
|---|
| 388 | }
|
|---|
| 389 | for (unsigned i = 0;
|
|---|
| 390 | i < PAGE_SIZE/sizeof(instance->periodic_list_base[0]); ++i)
|
|---|
| 391 | {
|
|---|
| 392 | /* Disable everything for now */
|
|---|
| 393 | instance->periodic_list_base[i] =
|
|---|
| 394 | LINK_POINTER_QH(instance->int_list.list_head_pa);
|
|---|
| 395 | }
|
|---|
| 396 | return EOK;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /**
|
|---|
| 400 | * @}
|
|---|
| 401 | */
|
|---|