[40a5d40] | 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 | /**
|
---|
[0969e45e] | 29 | * @addtogroup drvusbehci
|
---|
[40a5d40] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[0969e45e] | 34 | * PCI related functions needed by the EHCI driver.
|
---|
[40a5d40] | 35 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <assert.h>
|
---|
| 38 | #include <as.h>
|
---|
| 39 | #include <devman.h>
|
---|
| 40 | #include <ddi.h>
|
---|
| 41 | #include <libarch/ddi.h>
|
---|
| 42 | #include <device/hw_res.h>
|
---|
| 43 |
|
---|
| 44 | #include <usb/debug.h>
|
---|
| 45 | #include <pci_dev_iface.h>
|
---|
| 46 |
|
---|
| 47 | #include "pci.h"
|
---|
| 48 |
|
---|
| 49 | #define PAGE_SIZE_MASK 0xfffff000
|
---|
[13927cf] | 50 |
|
---|
[40a5d40] | 51 | #define HCC_PARAMS_OFFSET 0x8
|
---|
| 52 | #define HCC_PARAMS_EECP_MASK 0xff
|
---|
| 53 | #define HCC_PARAMS_EECP_OFFSET 8
|
---|
| 54 |
|
---|
[0d3167e] | 55 | #define CMD_OFFSET 0x0
|
---|
[a948c23] | 56 | #define STS_OFFSET 0x4
|
---|
| 57 | #define CFG_OFFSET 0x40
|
---|
[0d3167e] | 58 |
|
---|
[40a5d40] | 59 | #define USBCMD_RUN 1
|
---|
| 60 |
|
---|
| 61 | #define USBLEGSUP_OFFSET 0
|
---|
| 62 | #define USBLEGSUP_BIOS_CONTROL (1 << 16)
|
---|
| 63 | #define USBLEGSUP_OS_CONTROL (1 << 24)
|
---|
| 64 | #define USBLEGCTLSTS_OFFSET 4
|
---|
| 65 |
|
---|
| 66 | #define DEFAULT_WAIT 10000
|
---|
| 67 | #define WAIT_STEP 10
|
---|
| 68 |
|
---|
| 69 | /** Get address of registers and IRQ for given device.
|
---|
| 70 | *
|
---|
| 71 | * @param[in] dev Device asking for the addresses.
|
---|
[13927cf] | 72 | * @param[out] mem_reg_address Base address of the memory range.
|
---|
| 73 | * @param[out] mem_reg_size Size of the memory range.
|
---|
[40a5d40] | 74 | * @param[out] irq_no IRQ assigned to the device.
|
---|
| 75 | * @return Error code.
|
---|
| 76 | */
|
---|
| 77 | int pci_get_my_registers(ddf_dev_t *dev,
|
---|
| 78 | uintptr_t *mem_reg_address, size_t *mem_reg_size, int *irq_no)
|
---|
| 79 | {
|
---|
| 80 | assert(dev != NULL);
|
---|
| 81 |
|
---|
| 82 | int parent_phone = devman_parent_device_connect(dev->handle,
|
---|
| 83 | IPC_FLAG_BLOCKING);
|
---|
| 84 | if (parent_phone < 0) {
|
---|
| 85 | return parent_phone;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | int rc;
|
---|
| 89 |
|
---|
| 90 | hw_resource_list_t hw_resources;
|
---|
| 91 | rc = hw_res_get_resource_list(parent_phone, &hw_resources);
|
---|
| 92 | if (rc != EOK) {
|
---|
[4ed80ce8] | 93 | async_hangup(parent_phone);
|
---|
| 94 | return rc;
|
---|
[40a5d40] | 95 | }
|
---|
| 96 |
|
---|
| 97 | uintptr_t mem_address = 0;
|
---|
| 98 | size_t mem_size = 0;
|
---|
| 99 | bool mem_found = false;
|
---|
| 100 |
|
---|
| 101 | int irq = 0;
|
---|
| 102 | bool irq_found = false;
|
---|
| 103 |
|
---|
| 104 | size_t i;
|
---|
| 105 | for (i = 0; i < hw_resources.count; i++) {
|
---|
| 106 | hw_resource_t *res = &hw_resources.resources[i];
|
---|
[4ed80ce8] | 107 | switch (res->type)
|
---|
| 108 | {
|
---|
| 109 | case INTERRUPT:
|
---|
| 110 | irq = res->res.interrupt.irq;
|
---|
| 111 | irq_found = true;
|
---|
| 112 | usb_log_debug2("Found interrupt: %d.\n", irq);
|
---|
| 113 | break;
|
---|
| 114 |
|
---|
| 115 | case MEM_RANGE:
|
---|
| 116 | if (res->res.mem_range.address != 0
|
---|
| 117 | && res->res.mem_range.size != 0 ) {
|
---|
| 118 | mem_address = res->res.mem_range.address;
|
---|
| 119 | mem_size = res->res.mem_range.size;
|
---|
[4125b7d] | 120 | usb_log_debug2("Found mem: %" PRIxn" %zu.\n",
|
---|
[4ed80ce8] | 121 | mem_address, mem_size);
|
---|
| 122 | mem_found = true;
|
---|
[40a5d40] | 123 | }
|
---|
[4ed80ce8] | 124 | default:
|
---|
| 125 | break;
|
---|
[40a5d40] | 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[4ed80ce8] | 129 | if (mem_found && irq_found) {
|
---|
| 130 | *mem_reg_address = mem_address;
|
---|
| 131 | *mem_reg_size = mem_size;
|
---|
| 132 | *irq_no = irq;
|
---|
| 133 | rc = EOK;
|
---|
| 134 | } else {
|
---|
[40a5d40] | 135 | rc = ENOENT;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | async_hangup(parent_phone);
|
---|
| 139 | return rc;
|
---|
| 140 | }
|
---|
| 141 | /*----------------------------------------------------------------------------*/
|
---|
[13927cf] | 142 | /** Calls the PCI driver with a request to enable interrupts
|
---|
| 143 | *
|
---|
| 144 | * @param[in] device Device asking for interrupts
|
---|
| 145 | * @return Error code.
|
---|
| 146 | */
|
---|
[40a5d40] | 147 | int pci_enable_interrupts(ddf_dev_t *device)
|
---|
| 148 | {
|
---|
[4ed80ce8] | 149 | int parent_phone =
|
---|
| 150 | devman_parent_device_connect(device->handle, IPC_FLAG_BLOCKING);
|
---|
| 151 | if (parent_phone < 0) {
|
---|
| 152 | return parent_phone;
|
---|
| 153 | }
|
---|
[40a5d40] | 154 | bool enabled = hw_res_enable_interrupt(parent_phone);
|
---|
| 155 | async_hangup(parent_phone);
|
---|
| 156 | return enabled ? EOK : EIO;
|
---|
| 157 | }
|
---|
| 158 | /*----------------------------------------------------------------------------*/
|
---|
[13927cf] | 159 | /** Implements BIOS handoff routine as decribed in EHCI spec
|
---|
| 160 | *
|
---|
| 161 | * @param[in] device Device asking for interrupts
|
---|
| 162 | * @return Error code.
|
---|
| 163 | */
|
---|
[40a5d40] | 164 | int pci_disable_legacy(ddf_dev_t *device)
|
---|
| 165 | {
|
---|
| 166 | assert(device);
|
---|
| 167 | int parent_phone = devman_parent_device_connect(device->handle,
|
---|
| 168 | IPC_FLAG_BLOCKING);
|
---|
| 169 | if (parent_phone < 0) {
|
---|
| 170 | return parent_phone;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[4ed80ce8] | 173 | #define CHECK_RET_HANGUP_RETURN(ret, message...) \
|
---|
| 174 | if (ret != EOK) { \
|
---|
| 175 | usb_log_error(message); \
|
---|
| 176 | async_hangup(parent_phone); \
|
---|
| 177 | return ret; \
|
---|
| 178 | } else (void)0
|
---|
| 179 |
|
---|
| 180 |
|
---|
[13927cf] | 181 | /* read register space BASE BAR */
|
---|
[40a5d40] | 182 | sysarg_t address = 0x10;
|
---|
| 183 | sysarg_t value;
|
---|
| 184 |
|
---|
[4ed80ce8] | 185 | int ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
[40a5d40] | 186 | IPC_M_CONFIG_SPACE_READ_32, address, &value);
|
---|
[4ed80ce8] | 187 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to read PCI config space.\n",
|
---|
| 188 | ret);
|
---|
[561112f] | 189 | usb_log_info("Register space BAR at %p:%" PRIxn ".\n",
|
---|
| 190 | (void *) address, value);
|
---|
[40a5d40] | 191 |
|
---|
[13927cf] | 192 | /* clear lower byte, it's not part of the BASE address */
|
---|
[40a5d40] | 193 | uintptr_t registers = (value & 0xffffff00);
|
---|
[4125b7d] | 194 | usb_log_info("Memory registers BASE address:%p.\n", (void *) registers);
|
---|
[40a5d40] | 195 |
|
---|
[13927cf] | 196 | /* if nothing setup the hc, we don't need to turn it off */
|
---|
[40a5d40] | 197 | if (registers == 0)
|
---|
| 198 | return ENOTSUP;
|
---|
| 199 |
|
---|
[4ed80ce8] | 200 | /* map EHCI registers */
|
---|
[40a5d40] | 201 | void *regs = as_get_mappable_page(4096);
|
---|
| 202 | ret = physmem_map((void*)(registers & PAGE_SIZE_MASK), regs, 1,
|
---|
| 203 | AS_AREA_READ | AS_AREA_WRITE);
|
---|
[4ed80ce8] | 204 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to map registers %p:%p.\n",
|
---|
[4125b7d] | 205 | ret, regs, (void *) registers);
|
---|
[4ed80ce8] | 206 |
|
---|
[40a5d40] | 207 | /* calculate value of BASE */
|
---|
| 208 | registers = (registers & 0xf00) | (uintptr_t)regs;
|
---|
| 209 |
|
---|
[4ed80ce8] | 210 | const uint32_t hcc_params =
|
---|
| 211 | *(uint32_t*)(registers + HCC_PARAMS_OFFSET);
|
---|
[40a5d40] | 212 | usb_log_debug("Value of hcc params register: %x.\n", hcc_params);
|
---|
[13927cf] | 213 |
|
---|
| 214 | /* Read value of EHCI Extended Capabilities Pointer
|
---|
| 215 | * (points to PCI config space) */
|
---|
[4ed80ce8] | 216 | uint32_t eecp =
|
---|
| 217 | (hcc_params >> HCC_PARAMS_EECP_OFFSET) & HCC_PARAMS_EECP_MASK;
|
---|
[40a5d40] | 218 | usb_log_debug("Value of EECP: %x.\n", eecp);
|
---|
| 219 |
|
---|
[13927cf] | 220 | /* Read the second EEC. i.e. Legacy Support and Control register */
|
---|
| 221 | /* TODO: Check capability type here */
|
---|
[40a5d40] | 222 | ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
| 223 | IPC_M_CONFIG_SPACE_READ_32, eecp + USBLEGCTLSTS_OFFSET, &value);
|
---|
[4ed80ce8] | 224 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to read USBLEGCTLSTS.\n", ret);
|
---|
[4125b7d] | 225 | usb_log_debug("USBLEGCTLSTS: %" PRIxn ".\n", value);
|
---|
[40a5d40] | 226 |
|
---|
[13927cf] | 227 | /* Read the first EEC. i.e. Legacy Support register */
|
---|
| 228 | /* TODO: Check capability type here */
|
---|
[40a5d40] | 229 | ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
| 230 | IPC_M_CONFIG_SPACE_READ_32, eecp + USBLEGSUP_OFFSET, &value);
|
---|
[4ed80ce8] | 231 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to read USBLEGSUP.\n", ret);
|
---|
[4125b7d] | 232 | usb_log_debug2("USBLEGSUP: %" PRIxn ".\n", value);
|
---|
[40a5d40] | 233 |
|
---|
[13927cf] | 234 | /* Request control from firmware/BIOS, by writing 1 to highest byte.
|
---|
| 235 | * (OS Control semaphore)*/
|
---|
[40a5d40] | 236 | ret = async_req_3_0(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
| 237 | IPC_M_CONFIG_SPACE_WRITE_8, eecp + USBLEGSUP_OFFSET + 3, 1);
|
---|
[13927cf] | 238 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to request OS EHCI control.\n",
|
---|
[4ed80ce8] | 239 | ret);
|
---|
[40a5d40] | 240 |
|
---|
[4ed80ce8] | 241 | size_t wait = 0;
|
---|
[13927cf] | 242 | /* Wait for BIOS to release control. */
|
---|
[40a5d40] | 243 | while ((wait < DEFAULT_WAIT) && (value & USBLEGSUP_BIOS_CONTROL)) {
|
---|
| 244 | async_usleep(WAIT_STEP);
|
---|
| 245 | ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
[4ed80ce8] | 246 | IPC_M_CONFIG_SPACE_READ_32, eecp + USBLEGSUP_OFFSET, &value);
|
---|
[40a5d40] | 247 | wait += WAIT_STEP;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[13927cf] | 250 |
|
---|
[67352d2] | 251 | if ((value & USBLEGSUP_BIOS_CONTROL) == 0) {
|
---|
[4125b7d] | 252 | usb_log_info("BIOS released control after %zu usec.\n", wait);
|
---|
[4ed80ce8] | 253 | } else {
|
---|
[13927cf] | 254 | /* BIOS failed to hand over control, this should not happen. */
|
---|
[67352d2] | 255 | usb_log_warning( "BIOS failed to release control after "
|
---|
[4125b7d] | 256 | "%zu usecs, force it.\n", wait);
|
---|
[40a5d40] | 257 | ret = async_req_3_0(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
[4ed80ce8] | 258 | IPC_M_CONFIG_SPACE_WRITE_32, eecp + USBLEGSUP_OFFSET,
|
---|
[40a5d40] | 259 | USBLEGSUP_OS_CONTROL);
|
---|
[4ed80ce8] | 260 | CHECK_RET_HANGUP_RETURN(ret,
|
---|
| 261 | "Failed(%d) to force OS EHCI control.\n", ret);
|
---|
[40a5d40] | 262 | }
|
---|
| 263 |
|
---|
[13927cf] | 264 | /* Zero SMI enables in legacy control register.
|
---|
| 265 | * It would prevent pre-OS code from interfering. */
|
---|
[40a5d40] | 266 | ret = async_req_3_0(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
[a948c23] | 267 | IPC_M_CONFIG_SPACE_WRITE_32, eecp + USBLEGCTLSTS_OFFSET,
|
---|
| 268 | 0xe0000000);
|
---|
[4ed80ce8] | 269 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) zero USBLEGCTLSTS.\n", ret);
|
---|
[40a5d40] | 270 |
|
---|
[13927cf] | 271 | /* Read again Legacy Support and Control register */
|
---|
[40a5d40] | 272 | ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
| 273 | IPC_M_CONFIG_SPACE_READ_32, eecp + USBLEGCTLSTS_OFFSET, &value);
|
---|
[4ed80ce8] | 274 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to read USBLEGCTLSTS.\n", ret);
|
---|
[4125b7d] | 275 | usb_log_debug2("USBLEGCTLSTS: %" PRIxn ".\n", value);
|
---|
[40a5d40] | 276 |
|
---|
[13927cf] | 277 | /* Read again Legacy Support register */
|
---|
[40a5d40] | 278 | ret = async_req_2_1(parent_phone, DEV_IFACE_ID(PCI_DEV_IFACE),
|
---|
| 279 | IPC_M_CONFIG_SPACE_READ_32, eecp + USBLEGSUP_OFFSET, &value);
|
---|
[4ed80ce8] | 280 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) to read USBLEGSUP.\n", ret);
|
---|
[4125b7d] | 281 | usb_log_debug2("USBLEGSUP: %" PRIxn ".\n", value);
|
---|
[40a5d40] | 282 |
|
---|
[67352d2] | 283 | /*
|
---|
| 284 | * TURN OFF EHCI FOR NOW, DRIVER WILL REINITIALIZE IT
|
---|
| 285 | */
|
---|
[40a5d40] | 286 |
|
---|
[13927cf] | 287 | /* Get size of capability registers in memory space. */
|
---|
[40a5d40] | 288 | uint8_t operation_offset = *(uint8_t*)registers;
|
---|
| 289 | usb_log_debug("USBCMD offset: %d.\n", operation_offset);
|
---|
[13927cf] | 290 |
|
---|
| 291 | /* Zero USBCMD register. */
|
---|
[40a5d40] | 292 | volatile uint32_t *usbcmd =
|
---|
[0d3167e] | 293 | (uint32_t*)((uint8_t*)registers + operation_offset + CMD_OFFSET);
|
---|
[a948c23] | 294 | volatile uint32_t *usbsts =
|
---|
| 295 | (uint32_t*)((uint8_t*)registers + operation_offset + STS_OFFSET);
|
---|
[0d3167e] | 296 | volatile uint32_t *usbconfigured =
|
---|
[a948c23] | 297 | (uint32_t*)((uint8_t*)registers + operation_offset + CFG_OFFSET);
|
---|
[40a5d40] | 298 | usb_log_debug("USBCMD value: %x.\n", *usbcmd);
|
---|
| 299 | if (*usbcmd & USBCMD_RUN) {
|
---|
| 300 | *usbcmd = 0;
|
---|
[a948c23] | 301 | while (!(*usbsts & (1 << 12))); /*wait until hc is halted */
|
---|
| 302 | *usbcmd = 0x2; /* reset */
|
---|
| 303 | while (*usbcmd & 0x2); /* wait for reset to complete */
|
---|
[40a5d40] | 304 | usb_log_info("EHCI turned off.\n");
|
---|
| 305 | } else {
|
---|
| 306 | usb_log_info("EHCI was not running.\n");
|
---|
| 307 | }
|
---|
[a948c23] | 308 | usb_log_debug("Registers: %x(0x00080000):%x(0x00001000):%x(0x0).\n",
|
---|
| 309 | *usbcmd, *usbsts, *usbconfigured);
|
---|
[40a5d40] | 310 |
|
---|
| 311 | async_hangup(parent_phone);
|
---|
[4ed80ce8] | 312 | return ret;
|
---|
| 313 | #undef CHECK_RET_HANGUP_RETURN
|
---|
[40a5d40] | 314 | }
|
---|
| 315 | /*----------------------------------------------------------------------------*/
|
---|
| 316 | /**
|
---|
| 317 | * @}
|
---|
| 318 | */
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * @}
|
---|
| 322 | */
|
---|