[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);
|
---|
[a6d1bc1] | 49 | /*----------------------------------------------------------------------------*/
|
---|
[53f1c87] | 50 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
---|
| 51 | {
|
---|
| 52 | assert(instance);
|
---|
| 53 | assert(hub_fun);
|
---|
| 54 |
|
---|
| 55 | usb_address_t hub_address =
|
---|
| 56 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
---|
| 57 | instance->rh.address = hub_address;
|
---|
| 58 | usb_device_keeper_bind(
|
---|
| 59 | &instance->manager, hub_address, hub_fun->handle);
|
---|
| 60 |
|
---|
| 61 | char *match_str = NULL;
|
---|
[9b640c42] | 62 | int ret = asprintf(&match_str, "usb&class=hub");
|
---|
[53f1c87] | 63 | ret = (match_str == NULL) ? ret : EOK;
|
---|
| 64 | if (ret < 0) {
|
---|
| 65 | usb_log_error("Failed to create root hub match-id string.\n");
|
---|
| 66 | return ret;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
---|
| 70 | if (ret != EOK) {
|
---|
| 71 | usb_log_error("Failed add create root hub match-id.\n");
|
---|
| 72 | }
|
---|
| 73 | return ret;
|
---|
| 74 | }
|
---|
| 75 | /*----------------------------------------------------------------------------*/
|
---|
[a6d1bc1] | 76 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
---|
[e7bc999] | 77 | uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
[41b96b4] | 78 | {
|
---|
| 79 | assert(instance);
|
---|
[ff582d47] | 80 | int ret = EOK;
|
---|
[c2be0e5] | 81 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
| 82 | if (ret != EOK) { \
|
---|
| 83 | usb_log_error(message); \
|
---|
| 84 | return ret; \
|
---|
| 85 | } else (void)0
|
---|
[ff582d47] | 86 |
|
---|
| 87 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
[c2be0e5] | 88 | CHECK_RET_RETURN(ret,
|
---|
| 89 | "Failed(%d) to gain access to device registers: %s.\n",
|
---|
| 90 | ret, str_error(ret));
|
---|
| 91 |
|
---|
[ff582d47] | 92 | instance->ddf_instance = fun;
|
---|
[68b5ed6e] | 93 | usb_device_keeper_init(&instance->manager);
|
---|
[5876d36] | 94 | ret = usb_endpoint_manager_init(&instance->ep_manager,
|
---|
| 95 | BANDWIDTH_AVAILABLE_USB11);
|
---|
| 96 | CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
|
---|
[c2be0e5] | 97 | ret, str_error(ret));
|
---|
[e7bc999] | 98 |
|
---|
[7d6a676] | 99 | if (!interrupts) {
|
---|
| 100 | instance->interrupt_emulator =
|
---|
| 101 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
| 102 | fibril_add_ready(instance->interrupt_emulator);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2c617b0] | 105 | hc_gain_control(instance);
|
---|
| 106 |
|
---|
[a6d1bc1] | 107 | rh_init(&instance->rh, dev, instance->registers);
|
---|
[ff582d47] | 108 |
|
---|
[2c617b0] | 109 | hc_init_hw(instance);
|
---|
| 110 |
|
---|
[53f1c87] | 111 | /* TODO: implement */
|
---|
[8627377] | 112 | return EOK;
|
---|
[a6d1bc1] | 113 | }
|
---|
| 114 | /*----------------------------------------------------------------------------*/
|
---|
[1387692] | 115 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
---|
[41b96b4] | 116 | {
|
---|
| 117 | assert(instance);
|
---|
| 118 | assert(batch);
|
---|
| 119 | if (batch->target.address == instance->rh.address) {
|
---|
[2bf8f8c] | 120 | return rh_request(&instance->rh, batch);
|
---|
[41b96b4] | 121 | }
|
---|
| 122 | /* TODO: implement */
|
---|
[a6d1bc1] | 123 | return ENOTSUP;
|
---|
[41b96b4] | 124 | }
|
---|
| 125 | /*----------------------------------------------------------------------------*/
|
---|
[7d6a676] | 126 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
[41b96b4] | 127 | {
|
---|
| 128 | assert(instance);
|
---|
[7d6a676] | 129 | if (status == 0)
|
---|
| 130 | return;
|
---|
| 131 | if (status & IS_RHSC)
|
---|
| 132 | rh_interrupt(&instance->rh);
|
---|
| 133 |
|
---|
[2c617b0] | 134 | usb_log_info("OHCI interrupt: %x.\n", status);
|
---|
| 135 |
|
---|
[7d6a676] | 136 | /* TODO: Check for further interrupt causes */
|
---|
[41b96b4] | 137 | /* TODO: implement */
|
---|
| 138 | }
|
---|
[7d6a676] | 139 | /*----------------------------------------------------------------------------*/
|
---|
[53f1c87] | 140 | int interrupt_emulator(hc_t *instance)
|
---|
[7d6a676] | 141 | {
|
---|
| 142 | assert(instance);
|
---|
| 143 | usb_log_info("Started interrupt emulator.\n");
|
---|
| 144 | while (1) {
|
---|
[2c617b0] | 145 | const uint32_t status = instance->registers->interrupt_status;
|
---|
[7d6a676] | 146 | instance->registers->interrupt_status = status;
|
---|
| 147 | hc_interrupt(instance, status);
|
---|
| 148 | async_usleep(1000);
|
---|
| 149 | }
|
---|
| 150 | return EOK;
|
---|
| 151 | }
|
---|
[2c617b0] | 152 | /*----------------------------------------------------------------------------*/
|
---|
| 153 | void hc_gain_control(hc_t *instance)
|
---|
| 154 | {
|
---|
| 155 | assert(instance);
|
---|
| 156 | /* Interrupt routing enabled => smm driver is active */
|
---|
| 157 | if (instance->registers->control & C_IR) {
|
---|
| 158 | usb_log_info("Found SMM driver requesting ownership change.\n");
|
---|
| 159 | instance->registers->command_status |= CS_OCR;
|
---|
| 160 | while (instance->registers->control & C_IR) {
|
---|
| 161 | async_usleep(1000);
|
---|
| 162 | }
|
---|
| 163 | usb_log_info("Ownership taken from SMM driver.\n");
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | const unsigned hc_status =
|
---|
| 168 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
---|
| 169 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
| 170 | if (hc_status != C_HCFS_RESET) {
|
---|
| 171 | usb_log_info("Found BIOS driver.\n");
|
---|
| 172 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
| 173 | usb_log_info("HC operational(BIOS).\n");
|
---|
| 174 | return;
|
---|
| 175 | }
|
---|
| 176 | /* HC is suspended assert resume for 20ms */
|
---|
| 177 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
---|
| 178 | async_usleep(20000);
|
---|
| 179 | return;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /* HC is in reset (hw startup) => no other driver
|
---|
| 183 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
| 184 | async_usleep(50000);
|
---|
| 185 | }
|
---|
| 186 | /*----------------------------------------------------------------------------*/
|
---|
| 187 | void hc_init_hw(hc_t *instance)
|
---|
| 188 | {
|
---|
| 189 | assert(instance);
|
---|
| 190 | const uint32_t fm_interval = instance->registers->fm_interval;
|
---|
| 191 | instance->registers->command_status = CS_HCR;
|
---|
| 192 | async_usleep(10);
|
---|
| 193 | instance->registers->fm_interval = fm_interval;
|
---|
| 194 | assert((instance->registers->command_status & CS_HCR) == 0);
|
---|
| 195 | /* hc is now in suspend state */
|
---|
[c2be0e5] | 196 | /* TODO: init HCCA block */
|
---|
| 197 | /* TODO: init queues */
|
---|
| 198 | /* TODO: enable queues */
|
---|
| 199 | /* TODO: enable interrupts */
|
---|
| 200 | /* TODO: set periodic start to 90% */
|
---|
[2c617b0] | 201 |
|
---|
| 202 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
---|
| 203 | usb_log_info("OHCI HC up and running.\n");
|
---|
| 204 | }
|
---|
[41b96b4] | 205 | /**
|
---|
| 206 | * @}
|
---|
| 207 | */
|
---|