[5cbccd4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty
|
---|
| 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 drvusbxhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief The host controller data bookkeeping.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
[cb89430] | 37 | #include <str_error.h>
|
---|
[5cbccd4] | 38 | #include <usb/debug.h>
|
---|
[cb89430] | 39 | #include <usb/host/utils/malloc32.h>
|
---|
[5cbccd4] | 40 | #include "debug.h"
|
---|
| 41 | #include "hc.h"
|
---|
[7bd99bf] | 42 | #include "rh.h"
|
---|
[cb89430] | 43 | #include "hw_struct/trb.h"
|
---|
[c9c0e41] | 44 | #include "commands.h"
|
---|
[e9e24f2] | 45 | #include "transfers.h"
|
---|
| 46 | #include "trb_ring.h"
|
---|
[5cbccd4] | 47 |
|
---|
[91ca111] | 48 | /**
|
---|
| 49 | * Default USB Speed ID mapping: Table 157
|
---|
| 50 | */
|
---|
| 51 | #define PSI_TO_BPS(psie, psim) (((uint64_t) psim) << (10 * psie))
|
---|
[816335c] | 52 | #define PORT_SPEED(mjr, psie, psim) { \
|
---|
| 53 | .name = "USB ", \
|
---|
| 54 | .major = mjr, \
|
---|
| 55 | .minor = 0, \
|
---|
[91ca111] | 56 | .rx_bps = PSI_TO_BPS(psie, psim), \
|
---|
| 57 | .tx_bps = PSI_TO_BPS(psie, psim) \
|
---|
| 58 | }
|
---|
[816335c] | 59 | static const xhci_port_speed_t ps_default_full = PORT_SPEED(2, 2, 12);
|
---|
| 60 | static const xhci_port_speed_t ps_default_low = PORT_SPEED(2, 1, 1500);
|
---|
| 61 | static const xhci_port_speed_t ps_default_high = PORT_SPEED(2, 2, 480);
|
---|
| 62 | static const xhci_port_speed_t ps_default_super = PORT_SPEED(3, 3, 5);
|
---|
[91ca111] | 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Walk the list of extended capabilities.
|
---|
| 66 | */
|
---|
| 67 | static int hc_parse_ec(xhci_hc_t *hc)
|
---|
| 68 | {
|
---|
[816335c] | 69 | unsigned psic, major, minor;
|
---|
| 70 | xhci_sp_name_t name;
|
---|
| 71 |
|
---|
| 72 | xhci_port_speed_t *speeds = hc->rh.speeds;
|
---|
[91ca111] | 73 |
|
---|
| 74 | for (xhci_extcap_t *ec = hc->xecp; ec; ec = xhci_extcap_next(ec)) {
|
---|
| 75 | xhci_dump_extcap(ec);
|
---|
| 76 | switch (XHCI_REG_RD(ec, XHCI_EC_CAP_ID)) {
|
---|
| 77 | case XHCI_EC_USB_LEGACY:
|
---|
| 78 | assert(hc->legsup == NULL);
|
---|
| 79 | hc->legsup = (xhci_legsup_t *) ec;
|
---|
| 80 | break;
|
---|
| 81 | case XHCI_EC_SUPPORTED_PROTOCOL:
|
---|
| 82 | psic = XHCI_REG_RD(ec, XHCI_EC_SP_PSIC);
|
---|
| 83 | major = XHCI_REG_RD(ec, XHCI_EC_SP_MAJOR);
|
---|
[816335c] | 84 | minor = XHCI_REG_RD(ec, XHCI_EC_SP_MINOR);
|
---|
| 85 | name.packed = host2uint32_t_le(XHCI_REG_RD(ec, XHCI_EC_SP_NAME));
|
---|
| 86 |
|
---|
| 87 | if (name.packed != xhci_name_usb.packed) {
|
---|
| 88 | /**
|
---|
| 89 | * The detection of such protocol would work,
|
---|
| 90 | * but the rest of the implementation is made
|
---|
| 91 | * for the USB protocol only.
|
---|
| 92 | */
|
---|
| 93 | usb_log_error("Unknown protocol %.4s.", name.str);
|
---|
| 94 | return ENOTSUP;
|
---|
| 95 | }
|
---|
[91ca111] | 96 |
|
---|
| 97 | // "Implied" speed
|
---|
| 98 | if (psic == 0) {
|
---|
[816335c] | 99 | assert(minor == 0);
|
---|
[370a1c8] | 100 |
|
---|
[91ca111] | 101 | if (major == 2) {
|
---|
[816335c] | 102 | speeds[1] = ps_default_full;
|
---|
| 103 | speeds[2] = ps_default_low;
|
---|
| 104 | speeds[3] = ps_default_high;
|
---|
[91ca111] | 105 | } else if (major == 3) {
|
---|
[816335c] | 106 | speeds[4] = ps_default_super;
|
---|
[91ca111] | 107 | } else {
|
---|
| 108 | return EINVAL;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[816335c] | 111 | usb_log_debug2("Implied speed of USB %u.0 set up.", major);
|
---|
[91ca111] | 112 | } else {
|
---|
| 113 | for (unsigned i = 0; i < psic; i++) {
|
---|
| 114 | xhci_psi_t *psi = xhci_extcap_psi(ec, i);
|
---|
| 115 | unsigned sim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
| 116 | unsigned psiv = XHCI_REG_RD(psi, XHCI_PSI_PSIV);
|
---|
| 117 | unsigned psie = XHCI_REG_RD(psi, XHCI_PSI_PSIE);
|
---|
| 118 | unsigned psim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
| 119 |
|
---|
[816335c] | 120 | speeds[psiv].major = major;
|
---|
| 121 | speeds[psiv].minor = minor;
|
---|
| 122 | str_ncpy(speeds[psiv].name, 4, name.str, 4);
|
---|
| 123 |
|
---|
[91ca111] | 124 | uint64_t bps = PSI_TO_BPS(psie, psim);
|
---|
| 125 |
|
---|
| 126 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_RX)
|
---|
[816335c] | 127 | speeds[psiv].rx_bps = bps;
|
---|
[91ca111] | 128 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_TX) {
|
---|
[816335c] | 129 | speeds[psiv].tx_bps = bps;
|
---|
| 130 | usb_log_debug2("Speed %u set up for bps %" PRIu64 " / %" PRIu64 ".", psiv, speeds[psiv].rx_bps, speeds[psiv].tx_bps);
|
---|
[91ca111] | 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | return EOK;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[e4d7363] | 139 | int hc_init_mmio(xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
| 140 | {
|
---|
| 141 | int err;
|
---|
| 142 |
|
---|
| 143 | if (hw_res->mem_ranges.count != 1) {
|
---|
| 144 | usb_log_error("Unexpected MMIO area, bailing out.");
|
---|
| 145 | return EINVAL;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | hc->mmio_range = hw_res->mem_ranges.ranges[0];
|
---|
| 149 |
|
---|
| 150 | usb_log_debug("MMIO area at %p (size %zu), IRQ %d.\n",
|
---|
| 151 | RNGABSPTR(hc->mmio_range), RNGSZ(hc->mmio_range), hw_res->irqs.irqs[0]);
|
---|
| 152 |
|
---|
| 153 | if (RNGSZ(hc->mmio_range) < sizeof(xhci_cap_regs_t))
|
---|
| 154 | return EOVERFLOW;
|
---|
| 155 |
|
---|
| 156 | void *base;
|
---|
| 157 | if ((err = pio_enable_range(&hc->mmio_range, &base)))
|
---|
| 158 | return err;
|
---|
| 159 |
|
---|
[91ca111] | 160 | hc->base = base;
|
---|
[e4d7363] | 161 | hc->cap_regs = (xhci_cap_regs_t *) base;
|
---|
| 162 | hc->op_regs = (xhci_op_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH));
|
---|
| 163 | hc->rt_regs = (xhci_rt_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF));
|
---|
| 164 | hc->db_arry = (xhci_doorbell_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_DBOFF));
|
---|
| 165 |
|
---|
[91ca111] | 166 | uintptr_t xec_offset = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_XECP) * sizeof(xhci_dword_t);
|
---|
| 167 | if (xec_offset > 0)
|
---|
| 168 | hc->xecp = (xhci_extcap_t *) (base + xec_offset);
|
---|
| 169 |
|
---|
[e4d7363] | 170 | usb_log_debug2("Initialized MMIO reg areas:");
|
---|
| 171 | usb_log_debug2("\tCapability regs: %p", hc->cap_regs);
|
---|
| 172 | usb_log_debug2("\tOperational regs: %p", hc->op_regs);
|
---|
| 173 | usb_log_debug2("\tRuntime regs: %p", hc->rt_regs);
|
---|
| 174 | usb_log_debug2("\tDoorbell array base: %p", hc->db_arry);
|
---|
| 175 |
|
---|
| 176 | xhci_dump_cap_regs(hc->cap_regs);
|
---|
| 177 |
|
---|
| 178 | hc->ac64 = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_AC64);
|
---|
| 179 | hc->max_slots = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SLOTS);
|
---|
| 180 |
|
---|
[91ca111] | 181 | if ((err = hc_parse_ec(hc))) {
|
---|
| 182 | pio_disable(hc->base, RNGSZ(hc->mmio_range));
|
---|
| 183 | return err;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[e4d7363] | 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | int hc_init_memory(xhci_hc_t *hc)
|
---|
| 190 | {
|
---|
| 191 | int err;
|
---|
| 192 |
|
---|
[73e5b62] | 193 | hc->dcbaa = malloc32((1 + hc->max_slots) * sizeof(uint64_t));
|
---|
[e4d7363] | 194 | if (!hc->dcbaa)
|
---|
| 195 | return ENOMEM;
|
---|
| 196 |
|
---|
[078e0e6] | 197 | hc->dcbaa_virt = malloc((1 + hc->max_slots) * sizeof(xhci_virt_device_ctx_t));
|
---|
[73e5b62] | 198 | if (!hc->dcbaa_virt) {
|
---|
| 199 | err = ENOMEM;
|
---|
[e4d7363] | 200 | goto err_dcbaa;
|
---|
[73e5b62] | 201 | }
|
---|
| 202 |
|
---|
| 203 | if ((err = xhci_trb_ring_init(&hc->command_ring, hc)))
|
---|
| 204 | goto err_dcbaa_virt;
|
---|
[e4d7363] | 205 |
|
---|
| 206 | if ((err = xhci_event_ring_init(&hc->event_ring, hc)))
|
---|
| 207 | goto err_cmd_ring;
|
---|
| 208 |
|
---|
[b19131c5] | 209 | if ((err = xhci_scratchpad_alloc(hc)))
|
---|
[5a9ae994] | 210 | goto err_event_ring;
|
---|
[e4d7363] | 211 |
|
---|
[aee352c] | 212 | if ((err = xhci_init_commands(hc)))
|
---|
[ee28ae66] | 213 | goto err_scratch;
|
---|
[aee352c] | 214 |
|
---|
[816335c] | 215 | if ((err = xhci_rh_init(&hc->rh, hc)))
|
---|
[ee28ae66] | 216 | goto err_cmd;
|
---|
[d32d51d] | 217 |
|
---|
[e6b9182] | 218 | if ((err = xhci_bus_init(&hc->bus)))
|
---|
| 219 | goto err_rh;
|
---|
| 220 |
|
---|
| 221 |
|
---|
[e4d7363] | 222 | return EOK;
|
---|
| 223 |
|
---|
[e6b9182] | 224 | err_rh:
|
---|
| 225 | xhci_rh_fini(&hc->rh);
|
---|
[ee28ae66] | 226 | err_cmd:
|
---|
[d271f78] | 227 | xhci_fini_commands(hc);
|
---|
[ee28ae66] | 228 | err_scratch:
|
---|
| 229 | xhci_scratchpad_free(hc);
|
---|
[5a9ae994] | 230 | err_event_ring:
|
---|
[e4d7363] | 231 | xhci_event_ring_fini(&hc->event_ring);
|
---|
| 232 | err_cmd_ring:
|
---|
| 233 | xhci_trb_ring_fini(&hc->command_ring);
|
---|
[73e5b62] | 234 | err_dcbaa_virt:
|
---|
| 235 | free32(hc->dcbaa_virt);
|
---|
[e4d7363] | 236 | err_dcbaa:
|
---|
| 237 | free32(hc->dcbaa);
|
---|
| 238 | return err;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[ab5a0830] | 241 | /*
|
---|
| 242 | * Pseudocode:
|
---|
| 243 | * ip = read(intr[0].iman)
|
---|
| 244 | * if (ip) {
|
---|
| 245 | * status = read(usbsts)
|
---|
| 246 | * assert status
|
---|
| 247 | * assert ip
|
---|
| 248 | * accept (passing status)
|
---|
| 249 | * }
|
---|
| 250 | * decline
|
---|
| 251 | */
|
---|
| 252 | static const irq_cmd_t irq_commands[] = {
|
---|
| 253 | {
|
---|
| 254 | .cmd = CMD_PIO_READ_32,
|
---|
| 255 | .dstarg = 3,
|
---|
| 256 | .addr = NULL /* intr[0].iman */
|
---|
| 257 | },
|
---|
| 258 | {
|
---|
| 259 | .cmd = CMD_AND,
|
---|
| 260 | .srcarg = 3,
|
---|
| 261 | .dstarg = 4,
|
---|
| 262 | .value = 0 /* host2xhci(32, 1) */
|
---|
| 263 | },
|
---|
| 264 | {
|
---|
| 265 | .cmd = CMD_PREDICATE,
|
---|
| 266 | .srcarg = 4,
|
---|
| 267 | .value = 5
|
---|
| 268 | },
|
---|
| 269 | {
|
---|
| 270 | .cmd = CMD_PIO_READ_32,
|
---|
| 271 | .dstarg = 1,
|
---|
| 272 | .addr = NULL /* usbsts */
|
---|
| 273 | },
|
---|
| 274 | {
|
---|
| 275 | .cmd = CMD_AND,
|
---|
| 276 | .srcarg = 1,
|
---|
| 277 | .dstarg = 2,
|
---|
| 278 | .value = 0 /* host2xhci(32, XHCI_STATUS_ACK_MASK) */
|
---|
| 279 | },
|
---|
| 280 | {
|
---|
| 281 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
| 282 | .srcarg = 2,
|
---|
| 283 | .addr = NULL /* usbsts */
|
---|
| 284 | },
|
---|
| 285 | {
|
---|
| 286 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
[efe9463] | 287 | .srcarg = 3,
|
---|
[ab5a0830] | 288 | .addr = NULL /* intr[0].iman */
|
---|
| 289 | },
|
---|
| 290 | {
|
---|
| 291 | .cmd = CMD_ACCEPT
|
---|
| 292 | },
|
---|
| 293 | {
|
---|
| 294 | .cmd = CMD_DECLINE
|
---|
| 295 | }
|
---|
| 296 | };
|
---|
| 297 |
|
---|
[e4d7363] | 298 |
|
---|
[cb89430] | 299 | /**
|
---|
| 300 | * Generates code to accept interrupts. The xHCI is designed primarily for
|
---|
| 301 | * MSI/MSI-X, but we use PCI Interrupt Pin. In this mode, all the Interrupters
|
---|
| 302 | * (except 0) are disabled.
|
---|
| 303 | */
|
---|
[e4d7363] | 304 | int hc_irq_code_gen(irq_code_t *code, xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
[cb89430] | 305 | {
|
---|
| 306 | assert(code);
|
---|
| 307 | assert(hw_res);
|
---|
| 308 |
|
---|
[e4d7363] | 309 | if (hw_res->irqs.count != 1) {
|
---|
[cb89430] | 310 | usb_log_info("Unexpected HW resources to enable interrupts.");
|
---|
| 311 | return EINVAL;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | code->ranges = malloc(sizeof(irq_pio_range_t));
|
---|
| 315 | if (code->ranges == NULL)
|
---|
| 316 | return ENOMEM;
|
---|
| 317 |
|
---|
| 318 | code->cmds = malloc(sizeof(irq_commands));
|
---|
| 319 | if (code->cmds == NULL) {
|
---|
| 320 | free(code->ranges);
|
---|
| 321 | return ENOMEM;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | code->rangecount = 1;
|
---|
| 325 | code->ranges[0] = (irq_pio_range_t) {
|
---|
[91ca111] | 326 | .base = RNGABS(hc->mmio_range),
|
---|
| 327 | .size = RNGSZ(hc->mmio_range),
|
---|
[cb89430] | 328 | };
|
---|
| 329 |
|
---|
| 330 | code->cmdcount = ARRAY_SIZE(irq_commands);
|
---|
| 331 | memcpy(code->cmds, irq_commands, sizeof(irq_commands));
|
---|
| 332 |
|
---|
[91ca111] | 333 | void *intr0_iman = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF) + offsetof(xhci_rt_regs_t, ir[0]);
|
---|
[ab5a0830] | 334 | void *usbsts = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH) + offsetof(xhci_op_regs_t, usbsts);
|
---|
[cb89430] | 335 | code->cmds[0].addr = intr0_iman;
|
---|
| 336 | code->cmds[1].value = host2xhci(32, 1);
|
---|
[ab5a0830] | 337 | code->cmds[3].addr = usbsts;
|
---|
| 338 | code->cmds[4].value = host2xhci(32, XHCI_STATUS_ACK_MASK);
|
---|
| 339 | code->cmds[5].addr = usbsts;
|
---|
| 340 | code->cmds[6].addr = intr0_iman;
|
---|
[cb89430] | 341 |
|
---|
| 342 | return hw_res->irqs.irqs[0];
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[e4d7363] | 345 | int hc_claim(xhci_hc_t *hc, ddf_dev_t *dev)
|
---|
[cb89430] | 346 | {
|
---|
[91ca111] | 347 | /* No legacy support capability, the controller is solely for us */
|
---|
| 348 | if (!hc->legsup)
|
---|
| 349 | return EOK;
|
---|
| 350 |
|
---|
[e6b0dba] | 351 | /* Section 4.22.1 */
|
---|
| 352 | /* TODO: Test this with USB3-aware BIOS */
|
---|
| 353 | usb_log_debug2("LEGSUP: bios: %x, os: %x", hc->legsup->sem_bios, hc->legsup->sem_os);
|
---|
| 354 | XHCI_REG_WR(hc->legsup, XHCI_LEGSUP_SEM_OS, 1);
|
---|
[4d28d86] | 355 | for (int i = 0; i <= (XHCI_LEGSUP_BIOS_TIMEOUT_US / XHCI_LEGSUP_POLLING_DELAY_1MS); i++) {
|
---|
[e6b0dba] | 356 | usb_log_debug2("LEGSUP: elapsed: %i ms, bios: %x, os: %x", i,
|
---|
| 357 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS),
|
---|
| 358 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS));
|
---|
| 359 | if (XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS) == 0) {
|
---|
| 360 | assert(XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS) == 1);
|
---|
| 361 | return EOK;
|
---|
| 362 | }
|
---|
[c9d905f] | 363 | async_usleep(XHCI_LEGSUP_POLLING_DELAY_1MS);
|
---|
[e6b0dba] | 364 | }
|
---|
| 365 | usb_log_error("BIOS did not release XHCI legacy hold!\n");
|
---|
| 366 |
|
---|
[91ca111] | 367 | return ENOTSUP;
|
---|
[cb89430] | 368 | }
|
---|
| 369 |
|
---|
| 370 | static int hc_reset(xhci_hc_t *hc)
|
---|
| 371 | {
|
---|
| 372 | /* Stop the HC: set R/S to 0 */
|
---|
| 373 | XHCI_REG_CLR(hc->op_regs, XHCI_OP_RS, 1);
|
---|
| 374 |
|
---|
| 375 | /* Wait 16 ms until the HC is halted */
|
---|
| 376 | async_usleep(16000);
|
---|
| 377 | assert(XHCI_REG_RD(hc->op_regs, XHCI_OP_HCH));
|
---|
| 378 |
|
---|
| 379 | /* Reset */
|
---|
| 380 | XHCI_REG_SET(hc->op_regs, XHCI_OP_HCRST, 1);
|
---|
| 381 |
|
---|
| 382 | /* Wait until the reset is complete */
|
---|
| 383 | while (XHCI_REG_RD(hc->op_regs, XHCI_OP_HCRST))
|
---|
| 384 | async_usleep(1000);
|
---|
| 385 |
|
---|
| 386 | return EOK;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /**
|
---|
| 390 | * Initialize the HC: section 4.2
|
---|
| 391 | */
|
---|
[e4d7363] | 392 | int hc_start(xhci_hc_t *hc, bool irq)
|
---|
[cb89430] | 393 | {
|
---|
| 394 | int err;
|
---|
| 395 |
|
---|
| 396 | if ((err = hc_reset(hc)))
|
---|
| 397 | return err;
|
---|
| 398 |
|
---|
| 399 | while (XHCI_REG_RD(hc->op_regs, XHCI_OP_CNR))
|
---|
| 400 | async_usleep(1000);
|
---|
| 401 |
|
---|
[37789b5f] | 402 | uint64_t dcbaaptr = addr_to_phys(hc->dcbaa);
|
---|
[cb89430] | 403 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
|
---|
| 404 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
|
---|
| 405 | XHCI_REG_WR(hc->op_regs, XHCI_OP_MAX_SLOTS_EN, 0);
|
---|
| 406 |
|
---|
| 407 | uint64_t crptr = xhci_trb_ring_get_dequeue_ptr(&hc->command_ring);
|
---|
| 408 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_LO, LOWER32(crptr) >> 6);
|
---|
| 409 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crptr));
|
---|
| 410 |
|
---|
| 411 | uint64_t erstptr = addr_to_phys(hc->event_ring.erst);
|
---|
[12fba858] | 412 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
[cb89430] | 413 | xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
|
---|
| 414 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
|
---|
[12fba858] | 415 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
| 416 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
[cb89430] | 417 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
|
---|
| 418 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
|
---|
| 419 |
|
---|
| 420 | if (irq) {
|
---|
| 421 | XHCI_REG_SET(intr0, XHCI_INTR_IE, 1);
|
---|
| 422 | XHCI_REG_SET(hc->op_regs, XHCI_OP_INTE, 1);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | XHCI_REG_SET(hc->op_regs, XHCI_OP_RS, 1);
|
---|
| 426 |
|
---|
[dcf0597] | 427 | /* The reset changed status of all ports, and SW originated reason does
|
---|
| 428 | * not cause an interrupt.
|
---|
| 429 | */
|
---|
| 430 | xhci_rh_handle_port_change(&hc->rh);
|
---|
| 431 |
|
---|
[cb89430] | 432 | return EOK;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[ab5a0830] | 435 | /**
|
---|
| 436 | * Used only when polling. Shall supplement the irq_commands.
|
---|
| 437 | */
|
---|
[e4d7363] | 438 | int hc_status(xhci_hc_t *hc, uint32_t *status)
|
---|
[5cbccd4] | 439 | {
|
---|
[ab5a0830] | 440 | int ip = XHCI_REG_RD(hc->rt_regs->ir, XHCI_INTR_IP);
|
---|
| 441 | if (ip) {
|
---|
| 442 | *status = XHCI_REG_RD(hc->op_regs, XHCI_OP_STATUS);
|
---|
| 443 | XHCI_REG_WR(hc->op_regs, XHCI_OP_STATUS, *status & XHCI_STATUS_ACK_MASK);
|
---|
| 444 | XHCI_REG_WR(hc->rt_regs->ir, XHCI_INTR_IP, 1);
|
---|
| 445 |
|
---|
| 446 | /* interrupt handler expects status from irq_commands, which is
|
---|
| 447 | * in xhci order. */
|
---|
| 448 | *status = host2xhci(32, *status);
|
---|
| 449 | }
|
---|
[62ba2cbe] | 450 |
|
---|
[ab5a0830] | 451 | usb_log_debug2("HC(%p): Polled status: %x", hc, *status);
|
---|
[cb89430] | 452 | return EOK;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[e4d7363] | 455 | int hc_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
|
---|
[5cbccd4] | 456 | {
|
---|
[275f529] | 457 | assert(batch);
|
---|
| 458 |
|
---|
| 459 | usb_log_debug2("EP(%d:%d) started %s transfer of size %lu.",
|
---|
[41924f30] | 460 | batch->ep->target.address, batch->ep->target.endpoint,
|
---|
[275f529] | 461 | usb_str_transfer_type(batch->ep->transfer_type),
|
---|
| 462 | batch->buffer_size);
|
---|
| 463 |
|
---|
[41924f30] | 464 | if (!batch->ep->target.address) {
|
---|
[a0be5d0] | 465 | usb_log_error("Attempted to schedule transfer to address 0.");
|
---|
| 466 | return EINVAL;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[275f529] | 469 | switch (batch->ep->transfer_type) {
|
---|
| 470 | case USB_TRANSFER_CONTROL:
|
---|
[42bc933] | 471 | return xhci_schedule_control_transfer(hc, batch);
|
---|
[275f529] | 472 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
| 473 | /* TODO: Implement me. */
|
---|
| 474 | break;
|
---|
| 475 | case USB_TRANSFER_BULK:
|
---|
[42bc933] | 476 | return xhci_schedule_bulk_transfer(hc, batch);
|
---|
[275f529] | 477 | case USB_TRANSFER_INTERRUPT:
|
---|
| 478 | /* TODO: Implement me. */
|
---|
| 479 | break;
|
---|
| 480 | }
|
---|
[e50bdd92] | 481 |
|
---|
[d32d51d] | 482 | return EOK;
|
---|
[5cbccd4] | 483 | }
|
---|
| 484 |
|
---|
[472235a] | 485 | typedef int (*event_handler) (xhci_hc_t *, xhci_trb_t *trb);
|
---|
| 486 |
|
---|
| 487 | static event_handler event_handlers [] = {
|
---|
| 488 | [XHCI_TRB_TYPE_COMMAND_COMPLETION_EVENT] = &xhci_handle_command_completion,
|
---|
[dcf0597] | 489 | [XHCI_TRB_TYPE_PORT_STATUS_CHANGE_EVENT] = &xhci_rh_handle_port_status_change_event,
|
---|
[e9e24f2] | 490 | [XHCI_TRB_TYPE_TRANSFER_EVENT] = &xhci_handle_transfer_event,
|
---|
[472235a] | 491 | };
|
---|
| 492 |
|
---|
| 493 | static int hc_handle_event(xhci_hc_t *hc, xhci_trb_t *trb, xhci_interrupter_regs_t *intr)
|
---|
[7ee5408] | 494 | {
|
---|
[472235a] | 495 | unsigned type = TRB_TYPE(*trb);
|
---|
| 496 | if (type >= ARRAY_SIZE(event_handlers) || !event_handlers[type])
|
---|
| 497 | return ENOTSUP;
|
---|
| 498 |
|
---|
| 499 | return event_handlers[type](hc, trb);
|
---|
[7ee5408] | 500 | }
|
---|
| 501 |
|
---|
[cb89430] | 502 | static void hc_run_event_ring(xhci_hc_t *hc, xhci_event_ring_t *event_ring, xhci_interrupter_regs_t *intr)
|
---|
[62ba2cbe] | 503 | {
|
---|
[cb89430] | 504 | int err;
|
---|
[472235a] | 505 | ssize_t size = 16;
|
---|
| 506 | xhci_trb_t *queue = malloc(sizeof(xhci_trb_t) * size);
|
---|
| 507 | if (!queue) {
|
---|
| 508 | usb_log_error("Not enough memory to run the event ring.");
|
---|
| 509 | return;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | xhci_trb_t *head = queue;
|
---|
| 513 |
|
---|
| 514 | while ((err = xhci_event_ring_dequeue(event_ring, head)) != ENOENT) {
|
---|
| 515 | if (err != EOK) {
|
---|
[e50bdd92] | 516 | usb_log_warning("Error while accessing event ring: %s", str_error(err));
|
---|
| 517 | break;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[472235a] | 520 | usb_log_debug2("Dequeued trb from event ring: %s", xhci_trb_str_type(TRB_TYPE(*head)));
|
---|
| 521 | head++;
|
---|
[adb4e683] | 522 |
|
---|
| 523 | /* Expand the array if needed. */
|
---|
[472235a] | 524 | if (head - queue >= size) {
|
---|
[adb4e683] | 525 | size *= 2;
|
---|
[472235a] | 526 | xhci_trb_t *new_queue = realloc(queue, size);
|
---|
| 527 | if (new_queue == NULL)
|
---|
| 528 | break; /* Will process only those TRBs we have memory for. */
|
---|
[adb4e683] | 529 |
|
---|
[472235a] | 530 | head = new_queue + (head - queue);
|
---|
[adb4e683] | 531 | }
|
---|
[cb89430] | 532 | }
|
---|
| 533 |
|
---|
[adb4e683] | 534 | /* Update the ERDP to make room in the ring. */
|
---|
[472235a] | 535 | usb_log_debug2("Copying from ring finished, updating ERDP.");
|
---|
[a06fd64] | 536 | hc->event_ring.dequeue_ptr = host2xhci(64, addr_to_phys(hc->event_ring.dequeue_trb));
|
---|
[12fba858] | 537 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
| 538 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
| 539 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
[a06fd64] | 540 | XHCI_REG_SET(intr, XHCI_INTR_ERDP_EHB, 1);
|
---|
[adb4e683] | 541 |
|
---|
| 542 | /* Handle all of the collected events if possible. */
|
---|
[472235a] | 543 | if (head == queue)
|
---|
[adb4e683] | 544 | usb_log_warning("No events to be handled!");
|
---|
[472235a] | 545 |
|
---|
| 546 | for (xhci_trb_t *tail = queue; tail != head; tail++) {
|
---|
| 547 | if ((err = hc_handle_event(hc, tail, intr)) != EOK) {
|
---|
| 548 | usb_log_error("Failed to handle event: %s", str_error(err));
|
---|
| 549 | }
|
---|
[adb4e683] | 550 | }
|
---|
| 551 |
|
---|
[472235a] | 552 | free(queue);
|
---|
| 553 | usb_log_debug2("Event ring run finished.");
|
---|
[cb89430] | 554 | }
|
---|
| 555 |
|
---|
[e4d7363] | 556 | void hc_interrupt(xhci_hc_t *hc, uint32_t status)
|
---|
[cb89430] | 557 | {
|
---|
[ab5a0830] | 558 | status = xhci2host(32, status);
|
---|
[aee352c] | 559 |
|
---|
[8b415cc] | 560 | if (status & XHCI_REG_MASK(XHCI_OP_PCD)) {
|
---|
[07c08ea] | 561 | usb_log_debug2("Root hub interrupt.");
|
---|
[dcf0597] | 562 | xhci_rh_handle_port_change(&hc->rh);
|
---|
[ab5a0830] | 563 | status &= ~XHCI_REG_MASK(XHCI_OP_PCD);
|
---|
[07c08ea] | 564 | }
|
---|
| 565 |
|
---|
[cb89430] | 566 | if (status & XHCI_REG_MASK(XHCI_OP_HSE)) {
|
---|
| 567 | usb_log_error("Host controller error occured. Bad things gonna happen...");
|
---|
[ab5a0830] | 568 | status &= ~XHCI_REG_MASK(XHCI_OP_HSE);
|
---|
[cb89430] | 569 | }
|
---|
| 570 |
|
---|
| 571 | if (status & XHCI_REG_MASK(XHCI_OP_EINT)) {
|
---|
[472235a] | 572 | usb_log_debug2("Event interrupt, running the event ring.");
|
---|
[ab5a0830] | 573 | hc_run_event_ring(hc, &hc->event_ring, &hc->rt_regs->ir[0]);
|
---|
| 574 | status &= ~XHCI_REG_MASK(XHCI_OP_EINT);
|
---|
[cb89430] | 575 | }
|
---|
[275f529] | 576 |
|
---|
[cb89430] | 577 | if (status & XHCI_REG_MASK(XHCI_OP_SRE)) {
|
---|
| 578 | usb_log_error("Save/Restore error occured. WTF, S/R mechanism not implemented!");
|
---|
[ab5a0830] | 579 | status &= ~XHCI_REG_MASK(XHCI_OP_SRE);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | if (status) {
|
---|
| 583 | usb_log_error("Non-zero status after interrupt handling (%08x) - missing something?", status);
|
---|
[cb89430] | 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
[3256a6c] | 587 | static void hc_dcbaa_fini(xhci_hc_t *hc)
|
---|
| 588 | {
|
---|
[706a3e2] | 589 | xhci_trb_ring_t* trb_ring;
|
---|
[3256a6c] | 590 | xhci_scratchpad_free(hc);
|
---|
| 591 |
|
---|
[5a9ae994] | 592 | /* Idx 0 already deallocated by xhci_scratchpad_free. */
|
---|
| 593 | for (unsigned i = 1; i < hc->max_slots + 1; ++i) {
|
---|
[decfc8d1] | 594 | if (hc->dcbaa_virt[i].dev_ctx) {
|
---|
| 595 | free32(hc->dcbaa_virt[i].dev_ctx);
|
---|
| 596 | hc->dcbaa_virt[i].dev_ctx = NULL;
|
---|
| 597 | }
|
---|
[706a3e2] | 598 |
|
---|
[decfc8d1] | 599 | for (unsigned i = 0; i < XHCI_EP_COUNT; ++i) {
|
---|
| 600 | trb_ring = hc->dcbaa_virt[i].trs[i];
|
---|
| 601 | if (trb_ring) {
|
---|
| 602 | hc->dcbaa_virt[i].trs[i] = NULL;
|
---|
| 603 | xhci_trb_ring_fini(trb_ring);
|
---|
| 604 | free32(trb_ring);
|
---|
| 605 | }
|
---|
[3256a6c] | 606 | }
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | free32(hc->dcbaa);
|
---|
[73e5b62] | 610 | free32(hc->dcbaa_virt);
|
---|
[3256a6c] | 611 | }
|
---|
| 612 |
|
---|
[e4d7363] | 613 | void hc_fini(xhci_hc_t *hc)
|
---|
[cb89430] | 614 | {
|
---|
[e6b9182] | 615 | xhci_bus_fini(&hc->bus);
|
---|
[cb89430] | 616 | xhci_trb_ring_fini(&hc->command_ring);
|
---|
| 617 | xhci_event_ring_fini(&hc->event_ring);
|
---|
[3256a6c] | 618 | hc_dcbaa_fini(hc);
|
---|
[c46c356] | 619 | xhci_fini_commands(hc);
|
---|
[d32d51d] | 620 | xhci_rh_fini(&hc->rh);
|
---|
[91ca111] | 621 | pio_disable(hc->base, RNGSZ(hc->mmio_range));
|
---|
[e4d7363] | 622 | usb_log_info("HC(%p): Finalized.", hc);
|
---|
[62ba2cbe] | 623 | }
|
---|
| 624 |
|
---|
[a0be5d0] | 625 | int hc_ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
| 626 | {
|
---|
| 627 | assert(hc);
|
---|
| 628 | uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
|
---|
| 629 | pio_write_32(&hc->db_arry[doorbell], v);
|
---|
| 630 | return EOK;
|
---|
| 631 | }
|
---|
[5cbccd4] | 632 |
|
---|
| 633 | /**
|
---|
| 634 | * @}
|
---|
| 635 | */
|
---|