[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"
|
---|
[41b96b4] | 45 |
|
---|
[7d6a676] | 46 | static int interrupt_emulator(hc_t *instance);
|
---|
[2c617b0] | 47 | static void hc_gain_control(hc_t *instance);
|
---|
| 48 | static void hc_init_hw(hc_t *instance);
|
---|
[6b6e3ed3] | 49 | static int hc_init_transfer_lists(hc_t *instance);
|
---|
[344925c] | 50 | static int hc_init_memory(hc_t *instance);
|
---|
[a6d1bc1] | 51 | /*----------------------------------------------------------------------------*/
|
---|
[53f1c87] | 52 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
---|
| 53 | {
|
---|
| 54 | assert(instance);
|
---|
| 55 | assert(hub_fun);
|
---|
| 56 |
|
---|
| 57 | usb_address_t hub_address =
|
---|
| 58 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
---|
| 59 | instance->rh.address = hub_address;
|
---|
| 60 | usb_device_keeper_bind(
|
---|
| 61 | &instance->manager, hub_address, hub_fun->handle);
|
---|
| 62 |
|
---|
[6bec59b] | 63 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
| 64 | assert(ep);
|
---|
| 65 | int ret = endpoint_init(ep, hub_address, 0, USB_DIRECTION_BOTH,
|
---|
| 66 | USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64);
|
---|
| 67 | assert(ret == EOK);
|
---|
| 68 | ret = usb_endpoint_manager_register_ep(&instance->ep_manager, ep, 0);
|
---|
| 69 | assert(ret == EOK);
|
---|
| 70 |
|
---|
[53f1c87] | 71 | char *match_str = NULL;
|
---|
[6bec59b] | 72 | ret = asprintf(&match_str, "usb&class=hub");
|
---|
| 73 | // ret = (match_str == NULL) ? ret : EOK;
|
---|
[53f1c87] | 74 | if (ret < 0) {
|
---|
[6bec59b] | 75 | usb_log_error(
|
---|
| 76 | "Failed(%d) to create root hub match-id string.\n", ret);
|
---|
[53f1c87] | 77 | return ret;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
---|
| 81 | if (ret != EOK) {
|
---|
| 82 | usb_log_error("Failed add create root hub match-id.\n");
|
---|
| 83 | }
|
---|
| 84 | return ret;
|
---|
| 85 | }
|
---|
| 86 | /*----------------------------------------------------------------------------*/
|
---|
[a6d1bc1] | 87 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
---|
[e7bc999] | 88 | uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
[41b96b4] | 89 | {
|
---|
| 90 | assert(instance);
|
---|
[ff582d47] | 91 | int ret = EOK;
|
---|
[c2be0e5] | 92 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
| 93 | if (ret != EOK) { \
|
---|
| 94 | usb_log_error(message); \
|
---|
| 95 | return ret; \
|
---|
| 96 | } else (void)0
|
---|
[ff582d47] | 97 |
|
---|
| 98 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
[c2be0e5] | 99 | CHECK_RET_RETURN(ret,
|
---|
| 100 | "Failed(%d) to gain access to device registers: %s.\n",
|
---|
| 101 | ret, str_error(ret));
|
---|
| 102 |
|
---|
[ff582d47] | 103 | instance->ddf_instance = fun;
|
---|
[68b5ed6e] | 104 | usb_device_keeper_init(&instance->manager);
|
---|
[5876d36] | 105 | ret = usb_endpoint_manager_init(&instance->ep_manager,
|
---|
| 106 | BANDWIDTH_AVAILABLE_USB11);
|
---|
| 107 | CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
|
---|
[c2be0e5] | 108 | ret, str_error(ret));
|
---|
[e7bc999] | 109 |
|
---|
[7d6a676] | 110 | if (!interrupts) {
|
---|
| 111 | instance->interrupt_emulator =
|
---|
| 112 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
| 113 | fibril_add_ready(instance->interrupt_emulator);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[2c617b0] | 116 | hc_gain_control(instance);
|
---|
| 117 |
|
---|
[a6d1bc1] | 118 | rh_init(&instance->rh, dev, instance->registers);
|
---|
[ff582d47] | 119 |
|
---|
[344925c] | 120 | hc_init_memory(instance);
|
---|
[2c617b0] | 121 | hc_init_hw(instance);
|
---|
| 122 |
|
---|
[53f1c87] | 123 | /* TODO: implement */
|
---|
[8627377] | 124 | return EOK;
|
---|
[a6d1bc1] | 125 | }
|
---|
| 126 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 127 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
---|
[41b96b4] | 128 | {
|
---|
| 129 | assert(instance);
|
---|
| 130 | assert(batch);
|
---|
[9ff5ff82] | 131 |
|
---|
| 132 | /* check for root hub communication */
|
---|
[41b96b4] | 133 | if (batch->target.address == instance->rh.address) {
|
---|
[2bf8f8c] | 134 | return rh_request(&instance->rh, batch);
|
---|
[41b96b4] | 135 | }
|
---|
[9ff5ff82] | 136 |
|
---|
[4c28d17] | 137 | transfer_list_add_batch(
|
---|
| 138 | instance->transfers[batch->transfer_type], batch);
|
---|
[9ff5ff82] | 139 |
|
---|
| 140 | switch (batch->transfer_type) {
|
---|
| 141 | case USB_TRANSFER_CONTROL:
|
---|
| 142 | instance->registers->command_status |= CS_CLF;
|
---|
| 143 | break;
|
---|
| 144 | case USB_TRANSFER_BULK:
|
---|
| 145 | instance->registers->command_status |= CS_BLF;
|
---|
| 146 | break;
|
---|
| 147 | default:
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
[4c28d17] | 150 | return EOK;
|
---|
[41b96b4] | 151 | }
|
---|
| 152 | /*----------------------------------------------------------------------------*/
|
---|
[7d6a676] | 153 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
[41b96b4] | 154 | {
|
---|
| 155 | assert(instance);
|
---|
[7d6a676] | 156 | if (status == 0)
|
---|
| 157 | return;
|
---|
| 158 | if (status & IS_RHSC)
|
---|
| 159 | rh_interrupt(&instance->rh);
|
---|
| 160 |
|
---|
[2c617b0] | 161 | usb_log_info("OHCI interrupt: %x.\n", status);
|
---|
| 162 |
|
---|
[4c28d17] | 163 | LIST_INITIALIZE(done);
|
---|
| 164 | transfer_list_remove_finished(&instance->transfers_interrupt, &done);
|
---|
| 165 | transfer_list_remove_finished(&instance->transfers_isochronous, &done);
|
---|
| 166 | transfer_list_remove_finished(&instance->transfers_control, &done);
|
---|
| 167 | transfer_list_remove_finished(&instance->transfers_bulk, &done);
|
---|
| 168 |
|
---|
| 169 | while (!list_empty(&done)) {
|
---|
| 170 | link_t *item = done.next;
|
---|
| 171 | list_remove(item);
|
---|
| 172 | usb_transfer_batch_t *batch =
|
---|
| 173 | list_get_instance(item, usb_transfer_batch_t, link);
|
---|
| 174 | usb_transfer_batch_finish(batch);
|
---|
| 175 | }
|
---|
[41b96b4] | 176 | }
|
---|
[7d6a676] | 177 | /*----------------------------------------------------------------------------*/
|
---|
[53f1c87] | 178 | int interrupt_emulator(hc_t *instance)
|
---|
[7d6a676] | 179 | {
|
---|
| 180 | assert(instance);
|
---|
| 181 | usb_log_info("Started interrupt emulator.\n");
|
---|
| 182 | while (1) {
|
---|
[2c617b0] | 183 | const uint32_t status = instance->registers->interrupt_status;
|
---|
[7d6a676] | 184 | instance->registers->interrupt_status = status;
|
---|
| 185 | hc_interrupt(instance, status);
|
---|
| 186 | async_usleep(1000);
|
---|
| 187 | }
|
---|
| 188 | return EOK;
|
---|
| 189 | }
|
---|
[2c617b0] | 190 | /*----------------------------------------------------------------------------*/
|
---|
| 191 | void hc_gain_control(hc_t *instance)
|
---|
| 192 | {
|
---|
| 193 | assert(instance);
|
---|
| 194 | /* Interrupt routing enabled => smm driver is active */
|
---|
| 195 | if (instance->registers->control & C_IR) {
|
---|
| 196 | usb_log_info("Found SMM driver requesting ownership change.\n");
|
---|
| 197 | instance->registers->command_status |= CS_OCR;
|
---|
| 198 | while (instance->registers->control & C_IR) {
|
---|
| 199 | async_usleep(1000);
|
---|
| 200 | }
|
---|
| 201 | usb_log_info("Ownership taken from SMM driver.\n");
|
---|
| 202 | return;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | const unsigned hc_status =
|
---|
| 206 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
---|
| 207 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
| 208 | if (hc_status != C_HCFS_RESET) {
|
---|
| 209 | usb_log_info("Found BIOS driver.\n");
|
---|
| 210 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
| 211 | usb_log_info("HC operational(BIOS).\n");
|
---|
| 212 | return;
|
---|
| 213 | }
|
---|
| 214 | /* HC is suspended assert resume for 20ms */
|
---|
| 215 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
---|
| 216 | async_usleep(20000);
|
---|
| 217 | return;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /* HC is in reset (hw startup) => no other driver
|
---|
| 221 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
| 222 | async_usleep(50000);
|
---|
[049eb87] | 223 |
|
---|
| 224 | /* turn off legacy emulation */
|
---|
| 225 | volatile uint32_t *ohci_emulation_reg =
|
---|
| 226 | (uint32_t*)((char*)instance->registers + 0x100);
|
---|
| 227 | usb_log_info("OHCI legacy register status %p: %x.\n",
|
---|
| 228 | ohci_emulation_reg, *ohci_emulation_reg);
|
---|
| 229 | *ohci_emulation_reg = 0;
|
---|
| 230 |
|
---|
[2c617b0] | 231 | }
|
---|
| 232 | /*----------------------------------------------------------------------------*/
|
---|
| 233 | void hc_init_hw(hc_t *instance)
|
---|
| 234 | {
|
---|
| 235 | assert(instance);
|
---|
| 236 | const uint32_t fm_interval = instance->registers->fm_interval;
|
---|
[344925c] | 237 |
|
---|
| 238 | /* reset hc */
|
---|
[2c617b0] | 239 | instance->registers->command_status = CS_HCR;
|
---|
| 240 | async_usleep(10);
|
---|
[344925c] | 241 |
|
---|
| 242 | /* restore fm_interval */
|
---|
[2c617b0] | 243 | instance->registers->fm_interval = fm_interval;
|
---|
| 244 | assert((instance->registers->command_status & CS_HCR) == 0);
|
---|
[344925c] | 245 |
|
---|
[2c617b0] | 246 | /* hc is now in suspend state */
|
---|
[344925c] | 247 |
|
---|
| 248 | /* enable queues */
|
---|
| 249 | instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
|
---|
[c2be0e5] | 250 | /* TODO: enable interrupts */
|
---|
[344925c] | 251 | /* set periodic start to 90% */
|
---|
| 252 | instance->registers->periodic_start = (fm_interval / 10) * 9;
|
---|
[2c617b0] | 253 |
|
---|
| 254 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
---|
| 255 | usb_log_info("OHCI HC up and running.\n");
|
---|
| 256 | }
|
---|
[6b6e3ed3] | 257 | /*----------------------------------------------------------------------------*/
|
---|
| 258 | int hc_init_transfer_lists(hc_t *instance)
|
---|
| 259 | {
|
---|
| 260 | assert(instance);
|
---|
[344925c] | 261 |
|
---|
| 262 | #define SETUP_TRANSFER_LIST(type, name) \
|
---|
| 263 | do { \
|
---|
| 264 | int ret = transfer_list_init(&instance->type, name); \
|
---|
[6b6e3ed3] | 265 | if (ret != EOK) { \
|
---|
[344925c] | 266 | usb_log_error("Failed(%d) to setup %s transfer list.\n", \
|
---|
| 267 | ret, name); \
|
---|
[6b6e3ed3] | 268 | transfer_list_fini(&instance->transfers_isochronous); \
|
---|
| 269 | transfer_list_fini(&instance->transfers_interrupt); \
|
---|
| 270 | transfer_list_fini(&instance->transfers_control); \
|
---|
| 271 | transfer_list_fini(&instance->transfers_bulk); \
|
---|
[344925c] | 272 | } \
|
---|
| 273 | } while (0)
|
---|
[6b6e3ed3] | 274 |
|
---|
[344925c] | 275 | SETUP_TRANSFER_LIST(transfers_isochronous, "ISOCHRONOUS");
|
---|
| 276 | SETUP_TRANSFER_LIST(transfers_interrupt, "INTERRUPT");
|
---|
| 277 | SETUP_TRANSFER_LIST(transfers_control, "CONTROL");
|
---|
| 278 | SETUP_TRANSFER_LIST(transfers_bulk, "BULK");
|
---|
[6b6e3ed3] | 279 |
|
---|
| 280 | transfer_list_set_next(&instance->transfers_interrupt,
|
---|
[344925c] | 281 | &instance->transfers_isochronous);
|
---|
[6b6e3ed3] | 282 |
|
---|
| 283 | /* Assign pointers to be used during scheduling */
|
---|
| 284 | instance->transfers[USB_TRANSFER_INTERRUPT] =
|
---|
| 285 | &instance->transfers_interrupt;
|
---|
| 286 | instance->transfers[USB_TRANSFER_ISOCHRONOUS] =
|
---|
| 287 | &instance->transfers_interrupt;
|
---|
| 288 | instance->transfers[USB_TRANSFER_CONTROL] =
|
---|
| 289 | &instance->transfers_control;
|
---|
| 290 | instance->transfers[USB_TRANSFER_BULK] =
|
---|
| 291 | &instance->transfers_bulk;
|
---|
| 292 |
|
---|
| 293 | return EOK;
|
---|
| 294 | #undef CHECK_RET_CLEAR_RETURN
|
---|
| 295 | }
|
---|
[344925c] | 296 | /*----------------------------------------------------------------------------*/
|
---|
| 297 | int hc_init_memory(hc_t *instance)
|
---|
| 298 | {
|
---|
| 299 | assert(instance);
|
---|
| 300 | /* init queues */
|
---|
| 301 | hc_init_transfer_lists(instance);
|
---|
| 302 |
|
---|
| 303 | /* init HCCA */
|
---|
| 304 | instance->hcca = malloc32(sizeof(hcca_t));
|
---|
| 305 | if (instance->hcca == NULL)
|
---|
| 306 | return ENOMEM;
|
---|
| 307 | bzero(instance->hcca, sizeof(hcca_t));
|
---|
| 308 | instance->registers->hcca = addr_to_phys(instance->hcca);
|
---|
| 309 |
|
---|
| 310 | /* use queues */
|
---|
| 311 | instance->registers->bulk_head = instance->transfers_bulk.list_head_pa;
|
---|
| 312 | instance->registers->control_head =
|
---|
| 313 | instance->transfers_control.list_head_pa;
|
---|
| 314 |
|
---|
| 315 | unsigned i = 0;
|
---|
| 316 | for (; i < 32; ++i) {
|
---|
| 317 | instance->hcca->int_ep[i] =
|
---|
| 318 | instance->transfers_interrupt.list_head_pa;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | return EOK;
|
---|
| 322 | }
|
---|
[41b96b4] | 323 | /**
|
---|
| 324 | * @}
|
---|
| 325 | */
|
---|