[9351353] | 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 |
|
---|
| 29 | /** @addtogroup drvusbuhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief UHCI driver
|
---|
| 34 | */
|
---|
[56fd7cf] | 35 |
|
---|
| 36 | /* XXX Fix this */
|
---|
| 37 | #define _DDF_DATA_IMPLANT
|
---|
| 38 |
|
---|
[9351353] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <ddf/interrupt.h>
|
---|
| 42 | #include <usb_iface.h>
|
---|
| 43 | #include <usb/ddfiface.h>
|
---|
| 44 | #include <usb/debug.h>
|
---|
| 45 |
|
---|
| 46 | #include "uhci.h"
|
---|
| 47 |
|
---|
[795448f] | 48 | #include "res.h"
|
---|
[d2bff2f] | 49 | #include "hc.h"
|
---|
| 50 | #include "root_hub.h"
|
---|
| 51 |
|
---|
| 52 | /** Structure representing both functions of UHCI hc, USB host controller
|
---|
| 53 | * and USB root hub */
|
---|
| 54 | typedef struct uhci {
|
---|
[795448f] | 55 | /** Pointer to DDF representation of UHCI host controller */
|
---|
[d2bff2f] | 56 | ddf_fun_t *hc_fun;
|
---|
[795448f] | 57 | /** Pointer to DDF representation of UHCI root hub */
|
---|
[d2bff2f] | 58 | ddf_fun_t *rh_fun;
|
---|
| 59 |
|
---|
[795448f] | 60 | /** Internal driver's representation of UHCI host controller */
|
---|
[d2bff2f] | 61 | hc_t hc;
|
---|
[795448f] | 62 | /** Internal driver's representation of UHCI root hub */
|
---|
[d2bff2f] | 63 | rh_t rh;
|
---|
| 64 | } uhci_t;
|
---|
| 65 |
|
---|
[56fd7cf] | 66 | static inline uhci_t *dev_to_uhci(ddf_dev_t *dev)
|
---|
[d2bff2f] | 67 | {
|
---|
[56fd7cf] | 68 | return ddf_dev_data_get(dev);
|
---|
[d2bff2f] | 69 | }
|
---|
[76fbd9a] | 70 |
|
---|
[9d2d444] | 71 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
[9351353] | 72 | *
|
---|
| 73 | * @param[in] dev DDF instance of the device to use.
|
---|
| 74 | * @param[in] iid (Unused).
|
---|
[9d2d444] | 75 | * @param[in] call Pointer to the call from kernel.
|
---|
[9351353] | 76 | */
|
---|
| 77 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
| 78 | {
|
---|
| 79 | assert(dev);
|
---|
[2df648c2] | 80 | uhci_t *uhci = dev_to_uhci(dev);
|
---|
[dfe4955] | 81 | if (!uhci) {
|
---|
| 82 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
[2df648c2] | 85 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
[fc6e607] | 86 | hc_interrupt(&uhci->hc, status);
|
---|
[9351353] | 87 | }
|
---|
[76fbd9a] | 88 |
|
---|
[2df648c2] | 89 | /** Operations supported by the HC driver */
|
---|
| 90 | static ddf_dev_ops_t hc_ops = {
|
---|
[5fe0a697] | 91 | .interfaces[USBHC_DEV_IFACE] = &hcd_iface, /* see iface.h/c */
|
---|
[2df648c2] | 92 | };
|
---|
[76fbd9a] | 93 |
|
---|
[9d2d444] | 94 | /** Gets handle of the respective hc.
|
---|
[9351353] | 95 | *
|
---|
[9d2d444] | 96 | * @param[in] fun DDF function of uhci device.
|
---|
[ea993d18] | 97 | * @param[out] handle Host cotnroller handle.
|
---|
[9351353] | 98 | * @return Error code.
|
---|
| 99 | */
|
---|
[2df648c2] | 100 | static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
[9351353] | 101 | {
|
---|
[56fd7cf] | 102 | ddf_fun_t *hc_fun = dev_to_uhci(ddf_fun_get_dev(fun))->hc_fun;
|
---|
[d2bff2f] | 103 | assert(hc_fun);
|
---|
[9351353] | 104 |
|
---|
[d2bff2f] | 105 | if (handle != NULL)
|
---|
[56fd7cf] | 106 | *handle = ddf_fun_get_handle(hc_fun);
|
---|
[9351353] | 107 | return EOK;
|
---|
| 108 | }
|
---|
[76fbd9a] | 109 |
|
---|
[9d2d444] | 110 | /** USB interface implementation used by RH */
|
---|
[9351353] | 111 | static usb_iface_t usb_iface = {
|
---|
| 112 | .get_hc_handle = usb_iface_get_hc_handle,
|
---|
| 113 | };
|
---|
[76fbd9a] | 114 |
|
---|
[17ceb72] | 115 | /** Get root hub hw resources (I/O registers).
|
---|
[9351353] | 116 | *
|
---|
| 117 | * @param[in] fun Root hub function.
|
---|
| 118 | * @return Pointer to the resource list used by the root hub.
|
---|
| 119 | */
|
---|
| 120 | static hw_resource_list_t *get_resource_list(ddf_fun_t *fun)
|
---|
| 121 | {
|
---|
[56fd7cf] | 122 | rh_t *rh = ddf_fun_data_get(fun);
|
---|
[e247d83] | 123 | assert(rh);
|
---|
| 124 | return &rh->resource_list;
|
---|
[9351353] | 125 | }
|
---|
[76fbd9a] | 126 |
|
---|
[9d2d444] | 127 | /** Interface to provide the root hub driver with hw info */
|
---|
[9351353] | 128 | static hw_res_ops_t hw_res_iface = {
|
---|
| 129 | .get_resource_list = get_resource_list,
|
---|
[d2bff2f] | 130 | .enable_interrupt = NULL,
|
---|
[9351353] | 131 | };
|
---|
[76fbd9a] | 132 |
|
---|
[8d6c1f1] | 133 | /** RH function support for uhci_rhd */
|
---|
[33fbe95] | 134 | static ddf_dev_ops_t rh_ops = {
|
---|
[9351353] | 135 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 136 | .interfaces[HW_RES_DEV_IFACE] = &hw_res_iface
|
---|
| 137 | };
|
---|
[76fbd9a] | 138 |
|
---|
[9d2d444] | 139 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
[17ceb72] | 140 | *
|
---|
| 141 | * @param[in] device DDF instance of the device to use.
|
---|
| 142 | *
|
---|
| 143 | * This function does all the preparatory work for hc and rh drivers:
|
---|
[9d2d444] | 144 | * - gets device's hw resources
|
---|
| 145 | * - disables UHCI legacy support (PCI config space)
|
---|
[23f40280] | 146 | * - attempts to enable interrupts
|
---|
[17ceb72] | 147 | * - registers interrupt handler
|
---|
| 148 | */
|
---|
[d2bff2f] | 149 | int device_setup_uhci(ddf_dev_t *device)
|
---|
[9351353] | 150 | {
|
---|
[1664a26] | 151 | if (!device)
|
---|
| 152 | return EBADMEM;
|
---|
| 153 |
|
---|
| 154 | uhci_t *instance = ddf_dev_data_alloc(device, sizeof(uhci_t));
|
---|
[d2bff2f] | 155 | if (instance == NULL) {
|
---|
| 156 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
| 157 | return ENOMEM;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
[9351353] | 161 | if (ret != EOK) { \
|
---|
| 162 | if (instance->hc_fun) \
|
---|
| 163 | ddf_fun_destroy(instance->hc_fun); \
|
---|
[d2bff2f] | 164 | if (instance->rh_fun) {\
|
---|
[9351353] | 165 | ddf_fun_destroy(instance->rh_fun); \
|
---|
[d2bff2f] | 166 | } \
|
---|
| 167 | usb_log_error(message); \
|
---|
[9351353] | 168 | return ret; \
|
---|
[d2bff2f] | 169 | } else (void)0
|
---|
| 170 |
|
---|
| 171 | instance->rh_fun = NULL;
|
---|
[8d6c1f1] | 172 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "uhci_hc");
|
---|
[d2bff2f] | 173 | int ret = (instance->hc_fun == NULL) ? ENOMEM : EOK;
|
---|
| 174 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI HC function.\n");
|
---|
[56fd7cf] | 175 | ddf_fun_set_ops(instance->hc_fun, &hc_ops);
|
---|
| 176 | ddf_fun_data_implant(instance->hc_fun, &instance->hc.generic);
|
---|
[d2bff2f] | 177 |
|
---|
[8d6c1f1] | 178 | instance->rh_fun = ddf_fun_create(device, fun_inner, "uhci_rh");
|
---|
[d2bff2f] | 179 | ret = (instance->rh_fun == NULL) ? ENOMEM : EOK;
|
---|
| 180 | CHECK_RET_DEST_FREE_RETURN(ret, "Failed to create UHCI RH function.\n");
|
---|
[56fd7cf] | 181 | ddf_fun_set_ops(instance->rh_fun, &rh_ops);
|
---|
| 182 | ddf_fun_data_implant(instance->rh_fun, &instance->rh);
|
---|
[9351353] | 183 |
|
---|
[d2bff2f] | 184 | uintptr_t reg_base = 0;
|
---|
| 185 | size_t reg_size = 0;
|
---|
[9351353] | 186 | int irq = 0;
|
---|
| 187 |
|
---|
[795448f] | 188 | ret = get_my_registers(device, ®_base, ®_size, &irq);
|
---|
[d2bff2f] | 189 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[4125b7d] | 190 | "Failed to get I/O addresses for %" PRIun ": %s.\n",
|
---|
[56fd7cf] | 191 | ddf_dev_get_handle(device), str_error(ret));
|
---|
[4125b7d] | 192 | usb_log_debug("I/O regs at 0x%p (size %zu), IRQ %d.\n",
|
---|
[d2bff2f] | 193 | (void *) reg_base, reg_size, irq);
|
---|
[9351353] | 194 |
|
---|
[795448f] | 195 | ret = disable_legacy(device);
|
---|
[d2bff2f] | 196 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[dfe4955] | 197 | "Failed to disable legacy USB: %s.\n", str_error(ret));
|
---|
| 198 |
|
---|
[d57122c] | 199 | const size_t ranges_count = hc_irq_pio_range_count();
|
---|
| 200 | const size_t cmds_count = hc_irq_cmd_count();
|
---|
| 201 | irq_pio_range_t irq_ranges[ranges_count];
|
---|
| 202 | irq_cmd_t irq_cmds[cmds_count];
|
---|
| 203 | ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
|
---|
| 204 | sizeof(irq_cmds), reg_base, reg_size);
|
---|
[dfe4955] | 205 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
| 206 | "Failed to generate IRQ commands: %s.\n", str_error(ret));
|
---|
| 207 |
|
---|
[d57122c] | 208 | irq_code_t irq_code = {
|
---|
| 209 | .rangecount = ranges_count,
|
---|
| 210 | .ranges = irq_ranges,
|
---|
| 211 | .cmdcount = cmds_count,
|
---|
| 212 | .cmds = irq_cmds
|
---|
| 213 | };
|
---|
[dfe4955] | 214 |
|
---|
| 215 | /* Register handler to avoid interrupt lockup */
|
---|
| 216 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
| 217 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
| 218 | "Failed to register interrupt handler: %s.\n", str_error(ret));
|
---|
[9351353] | 219 |
|
---|
[ff34e5a] | 220 | bool interrupts = false;
|
---|
[795448f] | 221 | ret = enable_interrupts(device);
|
---|
[9351353] | 222 | if (ret != EOK) {
|
---|
[dfe4955] | 223 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
| 224 | " Falling back to polling.\n", str_error(ret));
|
---|
[ff34e5a] | 225 | } else {
|
---|
| 226 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
| 227 | interrupts = true;
|
---|
[9351353] | 228 | }
|
---|
| 229 |
|
---|
[d2bff2f] | 230 | ret = hc_init(&instance->hc, (void*)reg_base, reg_size, interrupts);
|
---|
| 231 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
[408d3c9] | 232 | "Failed to init uhci_hcd: %s.\n", str_error(ret));
|
---|
[9d2d444] | 233 |
|
---|
[9351353] | 234 | #define CHECK_RET_FINI_RETURN(ret, message...) \
|
---|
| 235 | if (ret != EOK) { \
|
---|
[c01cd32] | 236 | hc_fini(&instance->hc); \
|
---|
[d2bff2f] | 237 | CHECK_RET_DEST_FREE_RETURN(ret, message); \
|
---|
[9351353] | 238 | return ret; \
|
---|
[d2bff2f] | 239 | } else (void)0
|
---|
[9351353] | 240 |
|
---|
[d2bff2f] | 241 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
[408d3c9] | 242 | CHECK_RET_FINI_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
|
---|
| 243 | str_error(ret));
|
---|
[9351353] | 244 |
|
---|
[1dc4a5e] | 245 | ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
|
---|
[3ae5ca8] | 246 | CHECK_RET_FINI_RETURN(ret,
|
---|
[76ef94e] | 247 | "Failed to add UHCI to HC class: %s.\n", str_error(ret));
|
---|
| 248 |
|
---|
[33fbe95] | 249 | ret = rh_init(&instance->rh, instance->rh_fun,
|
---|
[9351353] | 250 | (uintptr_t)instance->hc.registers + 0x10, 4);
|
---|
| 251 | CHECK_RET_FINI_RETURN(ret,
|
---|
[408d3c9] | 252 | "Failed to setup UHCI root hub: %s.\n", str_error(ret));
|
---|
[9351353] | 253 |
|
---|
| 254 | ret = ddf_fun_bind(instance->rh_fun);
|
---|
| 255 | CHECK_RET_FINI_RETURN(ret,
|
---|
[408d3c9] | 256 | "Failed to register UHCI root hub: %s.\n", str_error(ret));
|
---|
[9351353] | 257 |
|
---|
| 258 | return EOK;
|
---|
| 259 | #undef CHECK_RET_FINI_RETURN
|
---|
| 260 | }
|
---|
| 261 | /**
|
---|
| 262 | * @}
|
---|
| 263 | */
|
---|