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