| 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 | /** @addtogroup drvusbohcihc
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI Host controller driver routines
|
|---|
| 33 | */
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <str_error.h>
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <libarch/ddi.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 | #include <usb/usb.h>
|
|---|
| 41 | #include <usb/ddfiface.h>
|
|---|
| 42 | #include <usb/usbdevice.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "hc.h"
|
|---|
| 45 | #include "hcd_endpoint.h"
|
|---|
| 46 |
|
|---|
| 47 | #define OHCI_USED_INTERRUPTS \
|
|---|
| 48 | (I_SO | I_WDH | I_UE | I_RHSC)
|
|---|
| 49 | static int interrupt_emulator(hc_t *instance);
|
|---|
| 50 | static void hc_gain_control(hc_t *instance);
|
|---|
| 51 | static int hc_init_transfer_lists(hc_t *instance);
|
|---|
| 52 | static int hc_init_memory(hc_t *instance);
|
|---|
| 53 | /*----------------------------------------------------------------------------*/
|
|---|
| 54 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
|---|
| 55 | {
|
|---|
| 56 | assert(instance);
|
|---|
| 57 | assert(hub_fun);
|
|---|
| 58 |
|
|---|
| 59 | int ret;
|
|---|
| 60 |
|
|---|
| 61 | usb_address_t hub_address =
|
|---|
| 62 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
|---|
| 63 | if (hub_address <= 0) {
|
|---|
| 64 | usb_log_error("Failed to get OHCI root hub address.\n");
|
|---|
| 65 | return hub_address;
|
|---|
| 66 | }
|
|---|
| 67 | instance->rh.address = hub_address;
|
|---|
| 68 | usb_device_keeper_bind(
|
|---|
| 69 | &instance->manager, hub_address, hub_fun->handle);
|
|---|
| 70 |
|
|---|
| 71 | ret = hc_add_endpoint(instance, hub_address, 0, USB_SPEED_FULL,
|
|---|
| 72 | USB_TRANSFER_CONTROL, USB_DIRECTION_BOTH, 64, 0, 0);
|
|---|
| 73 | if (ret != EOK) {
|
|---|
| 74 | usb_log_error("Failed to add OHCI rh endpoint 0.\n");
|
|---|
| 75 | usb_device_keeper_release(&instance->manager, hub_address);
|
|---|
| 76 | return ret;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | char *match_str = NULL;
|
|---|
| 80 | /* DDF needs heap allocated string */
|
|---|
| 81 | ret = asprintf(&match_str, "usb&class=hub");
|
|---|
| 82 | if (ret < 0) {
|
|---|
| 83 | usb_log_error(
|
|---|
| 84 | "Failed(%d) to create root hub match-id string.\n", ret);
|
|---|
| 85 | usb_device_keeper_release(&instance->manager, hub_address);
|
|---|
| 86 | return ret;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
|---|
| 90 | if (ret != EOK) {
|
|---|
| 91 | usb_log_error("Failed add root hub match-id.\n");
|
|---|
| 92 | }
|
|---|
| 93 | ret = ddf_fun_bind(hub_fun);
|
|---|
| 94 | return ret;
|
|---|
| 95 | }
|
|---|
| 96 | /*----------------------------------------------------------------------------*/
|
|---|
| 97 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
|---|
| 98 | uintptr_t regs, size_t reg_size, bool interrupts)
|
|---|
| 99 | {
|
|---|
| 100 | assert(instance);
|
|---|
| 101 | int ret = EOK;
|
|---|
| 102 | #define CHECK_RET_RETURN(ret, message...) \
|
|---|
| 103 | if (ret != EOK) { \
|
|---|
| 104 | usb_log_error(message); \
|
|---|
| 105 | return ret; \
|
|---|
| 106 | } else (void)0
|
|---|
| 107 |
|
|---|
| 108 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
|---|
| 109 | CHECK_RET_RETURN(ret,
|
|---|
| 110 | "Failed(%d) to gain access to device registers: %s.\n",
|
|---|
| 111 | ret, str_error(ret));
|
|---|
| 112 |
|
|---|
| 113 | list_initialize(&instance->pending_batches);
|
|---|
| 114 | usb_device_keeper_init(&instance->manager);
|
|---|
| 115 | ret = usb_endpoint_manager_init(&instance->ep_manager,
|
|---|
| 116 | BANDWIDTH_AVAILABLE_USB11);
|
|---|
| 117 | CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
|
|---|
| 118 | str_error(ret));
|
|---|
| 119 |
|
|---|
| 120 | ret = hc_init_memory(instance);
|
|---|
| 121 | CHECK_RET_RETURN(ret, "Failed to create OHCI memory structures: %s.\n",
|
|---|
| 122 | str_error(ret));
|
|---|
| 123 | #undef CHECK_RET_RETURN
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | // hc_init_hw(instance);
|
|---|
| 127 | hc_gain_control(instance);
|
|---|
| 128 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 129 |
|
|---|
| 130 | rh_init(&instance->rh, instance->registers);
|
|---|
| 131 |
|
|---|
| 132 | if (!interrupts) {
|
|---|
| 133 | instance->interrupt_emulator =
|
|---|
| 134 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
|---|
| 135 | fibril_add_ready(instance->interrupt_emulator);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | return EOK;
|
|---|
| 139 | }
|
|---|
| 140 | /*----------------------------------------------------------------------------*/
|
|---|
| 141 | int hc_add_endpoint(
|
|---|
| 142 | hc_t *instance, usb_address_t address, usb_endpoint_t endpoint,
|
|---|
| 143 | usb_speed_t speed, usb_transfer_type_t type, usb_direction_t direction,
|
|---|
| 144 | size_t mps, size_t size, unsigned interval)
|
|---|
| 145 | {
|
|---|
| 146 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
|---|
| 147 | if (ep == NULL)
|
|---|
| 148 | return ENOMEM;
|
|---|
| 149 | int ret =
|
|---|
| 150 | endpoint_init(ep, address, endpoint, direction, type, speed, mps);
|
|---|
| 151 | if (ret != EOK) {
|
|---|
| 152 | free(ep);
|
|---|
| 153 | return ret;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | hcd_endpoint_t *hcd_ep = hcd_endpoint_assign(ep);
|
|---|
| 157 | if (hcd_ep == NULL) {
|
|---|
| 158 | endpoint_destroy(ep);
|
|---|
| 159 | return ENOMEM;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | ret = usb_endpoint_manager_register_ep(&instance->ep_manager, ep, size);
|
|---|
| 163 | if (ret != EOK) {
|
|---|
| 164 | hcd_endpoint_clear(ep);
|
|---|
| 165 | endpoint_destroy(ep);
|
|---|
| 166 | return ret;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /* Enqueue hcd_ep */
|
|---|
| 170 | switch (ep->transfer_type) {
|
|---|
| 171 | case USB_TRANSFER_CONTROL:
|
|---|
| 172 | instance->registers->control &= ~C_CLE;
|
|---|
| 173 | endpoint_list_add_ep(
|
|---|
| 174 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 175 | instance->registers->control_current = 0;
|
|---|
| 176 | instance->registers->control |= C_CLE;
|
|---|
| 177 | break;
|
|---|
| 178 | case USB_TRANSFER_BULK:
|
|---|
| 179 | instance->registers->control &= ~C_BLE;
|
|---|
| 180 | endpoint_list_add_ep(
|
|---|
| 181 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 182 | instance->registers->control |= C_BLE;
|
|---|
| 183 | break;
|
|---|
| 184 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 185 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 186 | instance->registers->control &= (~C_PLE & ~C_IE);
|
|---|
| 187 | endpoint_list_add_ep(
|
|---|
| 188 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 189 | instance->registers->control |= C_PLE | C_IE;
|
|---|
| 190 | break;
|
|---|
| 191 | default:
|
|---|
| 192 | break;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | return EOK;
|
|---|
| 196 | }
|
|---|
| 197 | /*----------------------------------------------------------------------------*/
|
|---|
| 198 | int hc_remove_endpoint(hc_t *instance, usb_address_t address,
|
|---|
| 199 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 200 | {
|
|---|
| 201 | assert(instance);
|
|---|
| 202 | fibril_mutex_lock(&instance->guard);
|
|---|
| 203 | endpoint_t *ep = usb_endpoint_manager_get_ep(&instance->ep_manager,
|
|---|
| 204 | address, endpoint, direction, NULL);
|
|---|
| 205 | if (ep == NULL) {
|
|---|
| 206 | usb_log_error("Endpoint unregister failed: No such EP.\n");
|
|---|
| 207 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 208 | return ENOENT;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
|
|---|
| 212 | if (hcd_ep) {
|
|---|
| 213 | /* Dequeue hcd_ep */
|
|---|
| 214 | switch (ep->transfer_type) {
|
|---|
| 215 | case USB_TRANSFER_CONTROL:
|
|---|
| 216 | instance->registers->control &= ~C_CLE;
|
|---|
| 217 | endpoint_list_remove_ep(
|
|---|
| 218 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 219 | instance->registers->control_current = 0;
|
|---|
| 220 | instance->registers->control |= C_CLE;
|
|---|
| 221 | break;
|
|---|
| 222 | case USB_TRANSFER_BULK:
|
|---|
| 223 | instance->registers->control &= ~C_BLE;
|
|---|
| 224 | endpoint_list_remove_ep(
|
|---|
| 225 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 226 | instance->registers->control |= C_BLE;
|
|---|
| 227 | break;
|
|---|
| 228 | case USB_TRANSFER_ISOCHRONOUS:
|
|---|
| 229 | case USB_TRANSFER_INTERRUPT:
|
|---|
| 230 | instance->registers->control &= (~C_PLE & ~C_IE);
|
|---|
| 231 | endpoint_list_remove_ep(
|
|---|
| 232 | &instance->lists[ep->transfer_type], hcd_ep);
|
|---|
| 233 | instance->registers->control |= C_PLE | C_IE;
|
|---|
| 234 | break;
|
|---|
| 235 | default:
|
|---|
| 236 | break;
|
|---|
| 237 | }
|
|---|
| 238 | hcd_endpoint_clear(ep);
|
|---|
| 239 | } else {
|
|---|
| 240 | usb_log_warning("Endpoint without hcd equivalent structure.\n");
|
|---|
| 241 | }
|
|---|
| 242 | int ret = usb_endpoint_manager_unregister_ep(&instance->ep_manager,
|
|---|
| 243 | address, endpoint, direction);
|
|---|
| 244 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 245 | return ret;
|
|---|
| 246 | }
|
|---|
| 247 | /*----------------------------------------------------------------------------*/
|
|---|
| 248 | endpoint_t * hc_get_endpoint(hc_t *instance, usb_address_t address,
|
|---|
| 249 | usb_endpoint_t endpoint, usb_direction_t direction, size_t *bw)
|
|---|
| 250 | {
|
|---|
| 251 | assert(instance);
|
|---|
| 252 | fibril_mutex_lock(&instance->guard);
|
|---|
| 253 | endpoint_t *ep = usb_endpoint_manager_get_ep(&instance->ep_manager,
|
|---|
| 254 | address, endpoint, direction, bw);
|
|---|
| 255 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 256 | return ep;
|
|---|
| 257 | }
|
|---|
| 258 | /*----------------------------------------------------------------------------*/
|
|---|
| 259 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
|---|
| 260 | {
|
|---|
| 261 | assert(instance);
|
|---|
| 262 | assert(batch);
|
|---|
| 263 | assert(batch->ep);
|
|---|
| 264 |
|
|---|
| 265 | /* check for root hub communication */
|
|---|
| 266 | if (batch->ep->address == instance->rh.address) {
|
|---|
| 267 | return rh_request(&instance->rh, batch);
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | fibril_mutex_lock(&instance->guard);
|
|---|
| 271 | list_append(&batch->link, &instance->pending_batches);
|
|---|
| 272 | batch_commit(batch);
|
|---|
| 273 | switch (batch->ep->transfer_type) {
|
|---|
| 274 | case USB_TRANSFER_CONTROL:
|
|---|
| 275 | instance->registers->command_status |= CS_CLF;
|
|---|
| 276 | break;
|
|---|
| 277 | case USB_TRANSFER_BULK:
|
|---|
| 278 | instance->registers->command_status |= CS_BLF;
|
|---|
| 279 | break;
|
|---|
| 280 | default:
|
|---|
| 281 | break;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 285 | return EOK;
|
|---|
| 286 | }
|
|---|
| 287 | /*----------------------------------------------------------------------------*/
|
|---|
| 288 | void hc_interrupt(hc_t *instance, uint32_t status)
|
|---|
| 289 | {
|
|---|
| 290 | assert(instance);
|
|---|
| 291 | usb_log_debug("OHCI(%p) interrupt: %x.\n", instance, status);
|
|---|
| 292 | if ((status & ~I_SF) == 0) /* ignore sof status */
|
|---|
| 293 | return;
|
|---|
| 294 | if (status & I_RHSC)
|
|---|
| 295 | rh_interrupt(&instance->rh);
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 | if (status & I_WDH) {
|
|---|
| 299 | fibril_mutex_lock(&instance->guard);
|
|---|
| 300 | usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).\n", instance->hcca,
|
|---|
| 301 | instance->registers->hcca,
|
|---|
| 302 | (void *) addr_to_phys(instance->hcca));
|
|---|
| 303 | usb_log_debug2("Periodic current: %#" PRIx32 ".\n",
|
|---|
| 304 | instance->registers->periodic_current);
|
|---|
| 305 |
|
|---|
| 306 | link_t *current = instance->pending_batches.next;
|
|---|
| 307 | while (current != &instance->pending_batches) {
|
|---|
| 308 | link_t *next = current->next;
|
|---|
| 309 | usb_transfer_batch_t *batch =
|
|---|
| 310 | usb_transfer_batch_from_link(current);
|
|---|
| 311 |
|
|---|
| 312 | if (batch_is_complete(batch)) {
|
|---|
| 313 | list_remove(current);
|
|---|
| 314 | usb_transfer_batch_finish(batch);
|
|---|
| 315 | }
|
|---|
| 316 | current = next;
|
|---|
| 317 | }
|
|---|
| 318 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | /*----------------------------------------------------------------------------*/
|
|---|
| 322 | int interrupt_emulator(hc_t *instance)
|
|---|
| 323 | {
|
|---|
| 324 | assert(instance);
|
|---|
| 325 | usb_log_info("Started interrupt emulator.\n");
|
|---|
| 326 | while (1) {
|
|---|
| 327 | const uint32_t status = instance->registers->interrupt_status;
|
|---|
| 328 | instance->registers->interrupt_status = status;
|
|---|
| 329 | hc_interrupt(instance, status);
|
|---|
| 330 | async_usleep(50000);
|
|---|
| 331 | }
|
|---|
| 332 | return EOK;
|
|---|
| 333 | }
|
|---|
| 334 | /*----------------------------------------------------------------------------*/
|
|---|
| 335 | void hc_gain_control(hc_t *instance)
|
|---|
| 336 | {
|
|---|
| 337 | assert(instance);
|
|---|
| 338 | usb_log_debug("Requesting OHCI control.\n");
|
|---|
| 339 | /* Turn off legacy emulation */
|
|---|
| 340 | volatile uint32_t *ohci_emulation_reg =
|
|---|
| 341 | (uint32_t*)((char*)instance->registers + 0x100);
|
|---|
| 342 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
|---|
| 343 | ohci_emulation_reg, *ohci_emulation_reg);
|
|---|
| 344 | /* Do not change A20 state */
|
|---|
| 345 | *ohci_emulation_reg &= 0x100;
|
|---|
| 346 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
|---|
| 347 | ohci_emulation_reg, *ohci_emulation_reg);
|
|---|
| 348 |
|
|---|
| 349 | /* Interrupt routing enabled => smm driver is active */
|
|---|
| 350 | if (instance->registers->control & C_IR) {
|
|---|
| 351 | usb_log_debug("SMM driver: request ownership change.\n");
|
|---|
| 352 | instance->registers->command_status |= CS_OCR;
|
|---|
| 353 | while (instance->registers->control & C_IR) {
|
|---|
| 354 | async_usleep(1000);
|
|---|
| 355 | }
|
|---|
| 356 | usb_log_info("SMM driver: Ownership taken.\n");
|
|---|
| 357 | instance->registers->control &= (C_HCFS_RESET << C_HCFS_SHIFT);
|
|---|
| 358 | async_usleep(50000);
|
|---|
| 359 | return;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | const unsigned hc_status =
|
|---|
| 363 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
|---|
| 364 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
|---|
| 365 | if (hc_status != C_HCFS_RESET) {
|
|---|
| 366 | usb_log_debug("BIOS driver found.\n");
|
|---|
| 367 | if (hc_status == C_HCFS_OPERATIONAL) {
|
|---|
| 368 | usb_log_info("BIOS driver: HC operational.\n");
|
|---|
| 369 | return;
|
|---|
| 370 | }
|
|---|
| 371 | /* HC is suspended assert resume for 20ms */
|
|---|
| 372 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
|---|
| 373 | async_usleep(20000);
|
|---|
| 374 | usb_log_info("BIOS driver: HC resumed.\n");
|
|---|
| 375 | return;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /* HC is in reset (hw startup) => no other driver
|
|---|
| 379 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
|---|
| 380 | usb_log_info("HC found in reset.\n");
|
|---|
| 381 | async_usleep(50000);
|
|---|
| 382 | }
|
|---|
| 383 | /*----------------------------------------------------------------------------*/
|
|---|
| 384 | void hc_start_hw(hc_t *instance)
|
|---|
| 385 | {
|
|---|
| 386 | /* OHCI guide page 42 */
|
|---|
| 387 | assert(instance);
|
|---|
| 388 | usb_log_debug2("Started hc initialization routine.\n");
|
|---|
| 389 |
|
|---|
| 390 | /* Save contents of fm_interval register */
|
|---|
| 391 | const uint32_t fm_interval = instance->registers->fm_interval;
|
|---|
| 392 | usb_log_debug2("Old value of HcFmInterval: %x.\n", fm_interval);
|
|---|
| 393 |
|
|---|
| 394 | /* Reset hc */
|
|---|
| 395 | usb_log_debug2("HC reset.\n");
|
|---|
| 396 | size_t time = 0;
|
|---|
| 397 | instance->registers->command_status = CS_HCR;
|
|---|
| 398 | while (instance->registers->command_status & CS_HCR) {
|
|---|
| 399 | async_usleep(10);
|
|---|
| 400 | time += 10;
|
|---|
| 401 | }
|
|---|
| 402 | usb_log_debug2("HC reset complete in %zu us.\n", time);
|
|---|
| 403 |
|
|---|
| 404 | /* Restore fm_interval */
|
|---|
| 405 | instance->registers->fm_interval = fm_interval;
|
|---|
| 406 | assert((instance->registers->command_status & CS_HCR) == 0);
|
|---|
| 407 |
|
|---|
| 408 | /* hc is now in suspend state */
|
|---|
| 409 | usb_log_debug2("HC should be in suspend state(%x).\n",
|
|---|
| 410 | instance->registers->control);
|
|---|
| 411 |
|
|---|
| 412 | /* Use HCCA */
|
|---|
| 413 | instance->registers->hcca = addr_to_phys(instance->hcca);
|
|---|
| 414 |
|
|---|
| 415 | /* Use queues */
|
|---|
| 416 | instance->registers->bulk_head =
|
|---|
| 417 | instance->lists[USB_TRANSFER_BULK].list_head_pa;
|
|---|
| 418 | usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").\n",
|
|---|
| 419 | instance->lists[USB_TRANSFER_BULK].list_head,
|
|---|
| 420 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
|---|
| 421 |
|
|---|
| 422 | instance->registers->control_head =
|
|---|
| 423 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa;
|
|---|
| 424 | usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").\n",
|
|---|
| 425 | instance->lists[USB_TRANSFER_CONTROL].list_head,
|
|---|
| 426 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
|---|
| 427 |
|
|---|
| 428 | /* Enable queues */
|
|---|
| 429 | instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
|
|---|
| 430 | usb_log_debug2("All queues enabled(%x).\n",
|
|---|
| 431 | instance->registers->control);
|
|---|
| 432 |
|
|---|
| 433 | /* Enable interrupts */
|
|---|
| 434 | instance->registers->interrupt_enable = OHCI_USED_INTERRUPTS;
|
|---|
| 435 | usb_log_debug2("Enabled interrupts: %x.\n",
|
|---|
| 436 | instance->registers->interrupt_enable);
|
|---|
| 437 | instance->registers->interrupt_enable = I_MI;
|
|---|
| 438 |
|
|---|
| 439 | /* Set periodic start to 90% */
|
|---|
| 440 | uint32_t frame_length = ((fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK);
|
|---|
| 441 | instance->registers->periodic_start = (frame_length / 10) * 9;
|
|---|
| 442 | usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).\n",
|
|---|
| 443 | instance->registers->periodic_start,
|
|---|
| 444 | instance->registers->periodic_start, frame_length);
|
|---|
| 445 |
|
|---|
| 446 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
|---|
| 447 | usb_log_info("OHCI HC up and running(%x).\n",
|
|---|
| 448 | instance->registers->control);
|
|---|
| 449 | }
|
|---|
| 450 | /*----------------------------------------------------------------------------*/
|
|---|
| 451 | int hc_init_transfer_lists(hc_t *instance)
|
|---|
| 452 | {
|
|---|
| 453 | assert(instance);
|
|---|
| 454 |
|
|---|
| 455 | #define SETUP_ENDPOINT_LIST(type) \
|
|---|
| 456 | do { \
|
|---|
| 457 | const char *name = usb_str_transfer_type(type); \
|
|---|
| 458 | int ret = endpoint_list_init(&instance->lists[type], name); \
|
|---|
| 459 | if (ret != EOK) { \
|
|---|
| 460 | usb_log_error("Failed(%d) to setup %s endpoint list.\n", \
|
|---|
| 461 | ret, name); \
|
|---|
| 462 | endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]); \
|
|---|
| 463 | endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
|
|---|
| 464 | endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
|
|---|
| 465 | endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
|
|---|
| 466 | } \
|
|---|
| 467 | } while (0)
|
|---|
| 468 |
|
|---|
| 469 | SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
|
|---|
| 470 | SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
|
|---|
| 471 | SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
|
|---|
| 472 | SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
|
|---|
| 473 | #undef SETUP_ENDPOINT_LIST
|
|---|
| 474 | endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
|
|---|
| 475 | &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
|
|---|
| 476 |
|
|---|
| 477 | return EOK;
|
|---|
| 478 | }
|
|---|
| 479 | /*----------------------------------------------------------------------------*/
|
|---|
| 480 | int hc_init_memory(hc_t *instance)
|
|---|
| 481 | {
|
|---|
| 482 | assert(instance);
|
|---|
| 483 |
|
|---|
| 484 | bzero(&instance->rh, sizeof(instance->rh));
|
|---|
| 485 | /* Init queues */
|
|---|
| 486 | hc_init_transfer_lists(instance);
|
|---|
| 487 |
|
|---|
| 488 | /*Init HCCA */
|
|---|
| 489 | instance->hcca = malloc32(sizeof(hcca_t));
|
|---|
| 490 | if (instance->hcca == NULL)
|
|---|
| 491 | return ENOMEM;
|
|---|
| 492 | bzero(instance->hcca, sizeof(hcca_t));
|
|---|
| 493 | usb_log_debug2("OHCI HCCA initialized at %p.\n", instance->hcca);
|
|---|
| 494 |
|
|---|
| 495 | unsigned i = 0;
|
|---|
| 496 | for (; i < 32; ++i) {
|
|---|
| 497 | instance->hcca->int_ep[i] =
|
|---|
| 498 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa;
|
|---|
| 499 | }
|
|---|
| 500 | usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").\n",
|
|---|
| 501 | instance->lists[USB_TRANSFER_INTERRUPT].list_head,
|
|---|
| 502 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
|---|
| 503 |
|
|---|
| 504 | /* Init interrupt code */
|
|---|
| 505 | instance->interrupt_code.cmds = instance->interrupt_commands;
|
|---|
| 506 | {
|
|---|
| 507 | /* Read status register */
|
|---|
| 508 | instance->interrupt_commands[0].cmd = CMD_MEM_READ_32;
|
|---|
| 509 | instance->interrupt_commands[0].dstarg = 1;
|
|---|
| 510 | instance->interrupt_commands[0].addr =
|
|---|
| 511 | (void*)&instance->registers->interrupt_status;
|
|---|
| 512 |
|
|---|
| 513 | /* Test whether we are the interrupt cause */
|
|---|
| 514 | instance->interrupt_commands[1].cmd = CMD_BTEST;
|
|---|
| 515 | instance->interrupt_commands[1].value =
|
|---|
| 516 | OHCI_USED_INTERRUPTS;
|
|---|
| 517 | instance->interrupt_commands[1].srcarg = 1;
|
|---|
| 518 | instance->interrupt_commands[1].dstarg = 2;
|
|---|
| 519 |
|
|---|
| 520 | /* Predicate cleaning and accepting */
|
|---|
| 521 | instance->interrupt_commands[2].cmd = CMD_PREDICATE;
|
|---|
| 522 | instance->interrupt_commands[2].value = 2;
|
|---|
| 523 | instance->interrupt_commands[2].srcarg = 2;
|
|---|
| 524 |
|
|---|
| 525 | /* Write clean status register */
|
|---|
| 526 | instance->interrupt_commands[3].cmd = CMD_MEM_WRITE_A_32;
|
|---|
| 527 | instance->interrupt_commands[3].srcarg = 1;
|
|---|
| 528 | instance->interrupt_commands[3].addr =
|
|---|
| 529 | (void*)&instance->registers->interrupt_status;
|
|---|
| 530 |
|
|---|
| 531 | /* Accept interrupt */
|
|---|
| 532 | instance->interrupt_commands[4].cmd = CMD_ACCEPT;
|
|---|
| 533 |
|
|---|
| 534 | instance->interrupt_code.cmdcount = OHCI_NEEDED_IRQ_COMMANDS;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | return EOK;
|
|---|
| 538 | }
|
|---|
| 539 | /**
|
|---|
| 540 | * @}
|
|---|
| 541 | */
|
|---|