[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);
|
---|
| 131 | if (batch->target.address == instance->rh.address) {
|
---|
[2bf8f8c] | 132 | return rh_request(&instance->rh, batch);
|
---|
[41b96b4] | 133 | }
|
---|
| 134 | /* TODO: implement */
|
---|
[a6d1bc1] | 135 | return ENOTSUP;
|
---|
[41b96b4] | 136 | }
|
---|
| 137 | /*----------------------------------------------------------------------------*/
|
---|
[7d6a676] | 138 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
[41b96b4] | 139 | {
|
---|
| 140 | assert(instance);
|
---|
[7d6a676] | 141 | if (status == 0)
|
---|
| 142 | return;
|
---|
| 143 | if (status & IS_RHSC)
|
---|
| 144 | rh_interrupt(&instance->rh);
|
---|
| 145 |
|
---|
[2c617b0] | 146 | usb_log_info("OHCI interrupt: %x.\n", status);
|
---|
| 147 |
|
---|
[7d6a676] | 148 | /* TODO: Check for further interrupt causes */
|
---|
[41b96b4] | 149 | /* TODO: implement */
|
---|
| 150 | }
|
---|
[7d6a676] | 151 | /*----------------------------------------------------------------------------*/
|
---|
[53f1c87] | 152 | int interrupt_emulator(hc_t *instance)
|
---|
[7d6a676] | 153 | {
|
---|
| 154 | assert(instance);
|
---|
| 155 | usb_log_info("Started interrupt emulator.\n");
|
---|
| 156 | while (1) {
|
---|
[2c617b0] | 157 | const uint32_t status = instance->registers->interrupt_status;
|
---|
[7d6a676] | 158 | instance->registers->interrupt_status = status;
|
---|
| 159 | hc_interrupt(instance, status);
|
---|
| 160 | async_usleep(1000);
|
---|
| 161 | }
|
---|
| 162 | return EOK;
|
---|
| 163 | }
|
---|
[2c617b0] | 164 | /*----------------------------------------------------------------------------*/
|
---|
| 165 | void hc_gain_control(hc_t *instance)
|
---|
| 166 | {
|
---|
| 167 | assert(instance);
|
---|
| 168 | /* Interrupt routing enabled => smm driver is active */
|
---|
| 169 | if (instance->registers->control & C_IR) {
|
---|
| 170 | usb_log_info("Found SMM driver requesting ownership change.\n");
|
---|
| 171 | instance->registers->command_status |= CS_OCR;
|
---|
| 172 | while (instance->registers->control & C_IR) {
|
---|
| 173 | async_usleep(1000);
|
---|
| 174 | }
|
---|
| 175 | usb_log_info("Ownership taken from SMM driver.\n");
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | const unsigned hc_status =
|
---|
| 180 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
---|
| 181 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
| 182 | if (hc_status != C_HCFS_RESET) {
|
---|
| 183 | usb_log_info("Found BIOS driver.\n");
|
---|
| 184 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
| 185 | usb_log_info("HC operational(BIOS).\n");
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 | /* HC is suspended assert resume for 20ms */
|
---|
| 189 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
---|
| 190 | async_usleep(20000);
|
---|
| 191 | return;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* HC is in reset (hw startup) => no other driver
|
---|
| 195 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
| 196 | async_usleep(50000);
|
---|
[049eb87] | 197 |
|
---|
| 198 | /* turn off legacy emulation */
|
---|
| 199 | volatile uint32_t *ohci_emulation_reg =
|
---|
| 200 | (uint32_t*)((char*)instance->registers + 0x100);
|
---|
| 201 | usb_log_info("OHCI legacy register status %p: %x.\n",
|
---|
| 202 | ohci_emulation_reg, *ohci_emulation_reg);
|
---|
| 203 | *ohci_emulation_reg = 0;
|
---|
| 204 |
|
---|
[2c617b0] | 205 | }
|
---|
| 206 | /*----------------------------------------------------------------------------*/
|
---|
| 207 | void hc_init_hw(hc_t *instance)
|
---|
| 208 | {
|
---|
| 209 | assert(instance);
|
---|
| 210 | const uint32_t fm_interval = instance->registers->fm_interval;
|
---|
[344925c] | 211 |
|
---|
| 212 | /* reset hc */
|
---|
[2c617b0] | 213 | instance->registers->command_status = CS_HCR;
|
---|
| 214 | async_usleep(10);
|
---|
[344925c] | 215 |
|
---|
| 216 | /* restore fm_interval */
|
---|
[2c617b0] | 217 | instance->registers->fm_interval = fm_interval;
|
---|
| 218 | assert((instance->registers->command_status & CS_HCR) == 0);
|
---|
[344925c] | 219 |
|
---|
[2c617b0] | 220 | /* hc is now in suspend state */
|
---|
[344925c] | 221 |
|
---|
| 222 | /* enable queues */
|
---|
| 223 | instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
|
---|
[c2be0e5] | 224 | /* TODO: enable interrupts */
|
---|
[344925c] | 225 | /* set periodic start to 90% */
|
---|
| 226 | instance->registers->periodic_start = (fm_interval / 10) * 9;
|
---|
[2c617b0] | 227 |
|
---|
| 228 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
---|
| 229 | usb_log_info("OHCI HC up and running.\n");
|
---|
| 230 | }
|
---|
[6b6e3ed3] | 231 | /*----------------------------------------------------------------------------*/
|
---|
| 232 | int hc_init_transfer_lists(hc_t *instance)
|
---|
| 233 | {
|
---|
| 234 | assert(instance);
|
---|
[344925c] | 235 |
|
---|
| 236 | #define SETUP_TRANSFER_LIST(type, name) \
|
---|
| 237 | do { \
|
---|
| 238 | int ret = transfer_list_init(&instance->type, name); \
|
---|
[6b6e3ed3] | 239 | if (ret != EOK) { \
|
---|
[344925c] | 240 | usb_log_error("Failed(%d) to setup %s transfer list.\n", \
|
---|
| 241 | ret, name); \
|
---|
[6b6e3ed3] | 242 | transfer_list_fini(&instance->transfers_isochronous); \
|
---|
| 243 | transfer_list_fini(&instance->transfers_interrupt); \
|
---|
| 244 | transfer_list_fini(&instance->transfers_control); \
|
---|
| 245 | transfer_list_fini(&instance->transfers_bulk); \
|
---|
[344925c] | 246 | } \
|
---|
| 247 | } while (0)
|
---|
[6b6e3ed3] | 248 |
|
---|
[344925c] | 249 | SETUP_TRANSFER_LIST(transfers_isochronous, "ISOCHRONOUS");
|
---|
| 250 | SETUP_TRANSFER_LIST(transfers_interrupt, "INTERRUPT");
|
---|
| 251 | SETUP_TRANSFER_LIST(transfers_control, "CONTROL");
|
---|
| 252 | SETUP_TRANSFER_LIST(transfers_bulk, "BULK");
|
---|
[6b6e3ed3] | 253 |
|
---|
| 254 | transfer_list_set_next(&instance->transfers_interrupt,
|
---|
[344925c] | 255 | &instance->transfers_isochronous);
|
---|
[6b6e3ed3] | 256 |
|
---|
| 257 | /* Assign pointers to be used during scheduling */
|
---|
| 258 | instance->transfers[USB_TRANSFER_INTERRUPT] =
|
---|
| 259 | &instance->transfers_interrupt;
|
---|
| 260 | instance->transfers[USB_TRANSFER_ISOCHRONOUS] =
|
---|
| 261 | &instance->transfers_interrupt;
|
---|
| 262 | instance->transfers[USB_TRANSFER_CONTROL] =
|
---|
| 263 | &instance->transfers_control;
|
---|
| 264 | instance->transfers[USB_TRANSFER_BULK] =
|
---|
| 265 | &instance->transfers_bulk;
|
---|
| 266 |
|
---|
| 267 | return EOK;
|
---|
| 268 | #undef CHECK_RET_CLEAR_RETURN
|
---|
| 269 | }
|
---|
[344925c] | 270 | /*----------------------------------------------------------------------------*/
|
---|
| 271 | int hc_init_memory(hc_t *instance)
|
---|
| 272 | {
|
---|
| 273 | assert(instance);
|
---|
| 274 | /* init queues */
|
---|
| 275 | hc_init_transfer_lists(instance);
|
---|
| 276 |
|
---|
| 277 | /* init HCCA */
|
---|
| 278 | instance->hcca = malloc32(sizeof(hcca_t));
|
---|
| 279 | if (instance->hcca == NULL)
|
---|
| 280 | return ENOMEM;
|
---|
| 281 | bzero(instance->hcca, sizeof(hcca_t));
|
---|
| 282 | instance->registers->hcca = addr_to_phys(instance->hcca);
|
---|
| 283 |
|
---|
| 284 | /* use queues */
|
---|
| 285 | instance->registers->bulk_head = instance->transfers_bulk.list_head_pa;
|
---|
| 286 | instance->registers->control_head =
|
---|
| 287 | instance->transfers_control.list_head_pa;
|
---|
| 288 |
|
---|
| 289 | unsigned i = 0;
|
---|
| 290 | for (; i < 32; ++i) {
|
---|
| 291 | instance->hcca->int_ep[i] =
|
---|
| 292 | instance->transfers_interrupt.list_head_pa;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | return EOK;
|
---|
| 296 | }
|
---|
[41b96b4] | 297 | /**
|
---|
| 298 | * @}
|
---|
| 299 | */
|
---|