| 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 | /**
|
|---|
| 30 | * @addtogroup drvusbehci
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * PCI related functions needed by the EHCI driver.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <devman.h>
|
|---|
| 42 | #include <ddi.h>
|
|---|
| 43 | #include <usb/debug.h>
|
|---|
| 44 | #include <device/hw_res_parsed.h>
|
|---|
| 45 | #include <pci_dev_iface.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "res.h"
|
|---|
| 48 | #include "ehci_regs.h"
|
|---|
| 49 |
|
|---|
| 50 | #define USBLEGSUP_OFFSET 0
|
|---|
| 51 | #define USBLEGSUP_BIOS_CONTROL (1 << 16)
|
|---|
| 52 | #define USBLEGSUP_OS_CONTROL (1 << 24)
|
|---|
| 53 | #define USBLEGCTLSTS_OFFSET 4
|
|---|
| 54 |
|
|---|
| 55 | #define DEFAULT_WAIT 1000
|
|---|
| 56 | #define WAIT_STEP 10
|
|---|
| 57 |
|
|---|
| 58 | /** Implements BIOS hands-off routine as described in EHCI spec
|
|---|
| 59 | *
|
|---|
| 60 | * @param device EHCI device
|
|---|
| 61 | * @param eecp Value of EHCI Extended Capabilities pointer.
|
|---|
| 62 | * @return Error code.
|
|---|
| 63 | */
|
|---|
| 64 | static int disable_extended_caps(ddf_dev_t *device, unsigned eecp)
|
|---|
| 65 | {
|
|---|
| 66 | /* nothing to do */
|
|---|
| 67 | if (eecp == 0)
|
|---|
| 68 | return EOK;
|
|---|
| 69 |
|
|---|
| 70 | async_sess_t *parent_sess = devman_parent_device_connect(
|
|---|
| 71 | EXCHANGE_SERIALIZE, ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
|---|
| 72 | if (!parent_sess)
|
|---|
| 73 | return ENOMEM;
|
|---|
| 74 |
|
|---|
| 75 | #define CHECK_RET_HANGUP_RETURN(ret, message...) \
|
|---|
| 76 | if (ret != EOK) { \
|
|---|
| 77 | usb_log_error(message); \
|
|---|
| 78 | async_hangup(parent_sess); \
|
|---|
| 79 | return ret; \
|
|---|
| 80 | } else (void)0
|
|---|
| 81 |
|
|---|
| 82 | /* Read the first EEC. i.e. Legacy Support register */
|
|---|
| 83 | uint32_t usblegsup;
|
|---|
| 84 | int ret = pci_config_space_read_32(parent_sess,
|
|---|
| 85 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| 86 | CHECK_RET_HANGUP_RETURN(ret,
|
|---|
| 87 | "Failed to read USBLEGSUP: %s.\n", str_error(ret));
|
|---|
| 88 | usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
|
|---|
| 89 |
|
|---|
| 90 | /* Request control from firmware/BIOS by writing 1 to highest
|
|---|
| 91 | * byte. (OS Control semaphore)*/
|
|---|
| 92 | usb_log_debug("Requesting OS control.\n");
|
|---|
| 93 | ret = pci_config_space_write_8(parent_sess,
|
|---|
| 94 | eecp + USBLEGSUP_OFFSET + 3, 1);
|
|---|
| 95 | CHECK_RET_HANGUP_RETURN(ret, "Failed to request OS EHCI control: %s.\n",
|
|---|
| 96 | str_error(ret));
|
|---|
| 97 |
|
|---|
| 98 | size_t wait = 0;
|
|---|
| 99 | /* Wait for BIOS to release control. */
|
|---|
| 100 | ret = pci_config_space_read_32(
|
|---|
| 101 | parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| 102 | while ((ret == EOK) && (wait < DEFAULT_WAIT)
|
|---|
| 103 | && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
|
|---|
| 104 | async_usleep(WAIT_STEP);
|
|---|
| 105 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 106 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| 107 | wait += WAIT_STEP;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
|
|---|
| 111 | usb_log_info("BIOS released control after %zu usec.\n", wait);
|
|---|
| 112 | async_hangup(parent_sess);
|
|---|
| 113 | return EOK;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* BIOS failed to hand over control, this should not happen. */
|
|---|
| 117 | usb_log_warning( "BIOS failed to release control after "
|
|---|
| 118 | "%zu usecs, force it.\n", wait);
|
|---|
| 119 | ret = pci_config_space_write_32(parent_sess,
|
|---|
| 120 | eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
|
|---|
| 121 | CHECK_RET_HANGUP_RETURN(ret, "Failed to force OS control: "
|
|---|
| 122 | "%s.\n", str_error(ret));
|
|---|
| 123 | /*
|
|---|
| 124 | * Check capability type here, value of 01h identifies the capability
|
|---|
| 125 | * as Legacy Support. This extended capability requires one additional
|
|---|
| 126 | * 32-bit register for control/status information and this register is
|
|---|
| 127 | * located at offset EECP+04h
|
|---|
| 128 | */
|
|---|
| 129 | if ((usblegsup & 0xff) == 1) {
|
|---|
| 130 | /* Read the second EEC Legacy Support and Control register */
|
|---|
| 131 | uint32_t usblegctlsts;
|
|---|
| 132 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 133 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
|---|
| 134 | CHECK_RET_HANGUP_RETURN(ret, "Failed to get USBLEGCTLSTS: %s.\n",
|
|---|
| 135 | str_error(ret));
|
|---|
| 136 | usb_log_debug("USBLEGCTLSTS: %" PRIx32 ".\n", usblegctlsts);
|
|---|
| 137 | /*
|
|---|
| 138 | * Zero SMI enables in legacy control register.
|
|---|
| 139 | * It should prevent pre-OS code from
|
|---|
| 140 | * interfering. NOTE: Three upper bits are WC
|
|---|
| 141 | */
|
|---|
| 142 | ret = pci_config_space_write_32(parent_sess,
|
|---|
| 143 | eecp + USBLEGCTLSTS_OFFSET, 0xe0000000);
|
|---|
| 144 | CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) zero USBLEGCTLSTS.\n", ret);
|
|---|
| 145 | udelay(10);
|
|---|
| 146 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 147 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
|---|
| 148 | CHECK_RET_HANGUP_RETURN(ret, "Failed to get USBLEGCTLSTS 2: %s.\n",
|
|---|
| 149 | str_error(ret));
|
|---|
| 150 | usb_log_debug("Zeroed USBLEGCTLSTS: %" PRIx32 ".\n",
|
|---|
| 151 | usblegctlsts);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /* Read again Legacy Support register */
|
|---|
| 155 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 156 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| 157 | CHECK_RET_HANGUP_RETURN(ret, "Failed to read USBLEGSUP: %s.\n",
|
|---|
| 158 | str_error(ret));
|
|---|
| 159 | usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
|
|---|
| 160 | async_hangup(parent_sess);
|
|---|
| 161 | return EOK;
|
|---|
| 162 | #undef CHECK_RET_HANGUP_RETURN
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | int disable_legacy(ddf_dev_t *device, addr_range_t *reg_range)
|
|---|
| 166 | {
|
|---|
| 167 | assert(device);
|
|---|
| 168 | usb_log_debug("Disabling EHCI legacy support.\n");
|
|---|
| 169 |
|
|---|
| 170 | /* Map EHCI registers */
|
|---|
| 171 | void *regs = NULL;
|
|---|
| 172 | int ret = pio_enable_range(reg_range, ®s);
|
|---|
| 173 | if (ret != EOK) {
|
|---|
| 174 | usb_log_error("Failed to map registers %p: %s.\n",
|
|---|
| 175 | RNGABSPTR(*reg_range), str_error(ret));
|
|---|
| 176 | return ret;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | usb_log_debug2("Registers mapped at: %p.\n", regs);
|
|---|
| 180 |
|
|---|
| 181 | ehci_caps_regs_t *ehci_caps = regs;
|
|---|
| 182 |
|
|---|
| 183 | const uint32_t hcc_params = EHCI_RD(ehci_caps->hccparams);
|
|---|
| 184 | usb_log_debug("Value of hcc params register: %x.\n", hcc_params);
|
|---|
| 185 |
|
|---|
| 186 | /* Read value of EHCI Extended Capabilities Pointer
|
|---|
| 187 | * position of EEC registers (points to PCI config space) */
|
|---|
| 188 | const uint32_t eecp =
|
|---|
| 189 | (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
|
|---|
| 190 | usb_log_debug("Value of EECP: %x.\n", eecp);
|
|---|
| 191 |
|
|---|
| 192 | ret = disable_extended_caps(device, eecp);
|
|---|
| 193 | if (ret != EOK) {
|
|---|
| 194 | usb_log_error("Failed to disable extended capabilities: %s.\n",
|
|---|
| 195 | str_error(ret));
|
|---|
| 196 | return ret;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | /*
|
|---|
| 201 | * TURN OFF EHCI FOR NOW, DRIVER WILL REINITIALIZE IT IF NEEDED
|
|---|
| 202 | */
|
|---|
| 203 |
|
|---|
| 204 | /* Get size of capability registers in memory space. */
|
|---|
| 205 | const unsigned operation_offset = EHCI_RD8(ehci_caps->caplength);
|
|---|
| 206 | usb_log_debug("USBCMD offset: %d.\n", operation_offset);
|
|---|
| 207 |
|
|---|
| 208 | ehci_regs_t *ehci_regs = regs + operation_offset;
|
|---|
| 209 |
|
|---|
| 210 | usb_log_debug("USBCMD value: %x.\n", EHCI_RD(ehci_regs->usbcmd));
|
|---|
| 211 | if (EHCI_RD(ehci_regs->usbcmd) & USB_CMD_RUN_FLAG) {
|
|---|
| 212 | EHCI_WR(ehci_regs->usbintr, 0); /* disable all interrupts */
|
|---|
| 213 | EHCI_WR(ehci_regs->usbsts, 0x3f); /* ack all interrupts */
|
|---|
| 214 | EHCI_WR(ehci_regs->configflag, 0); /* release RH ports */
|
|---|
| 215 | EHCI_WR(ehci_regs->usbcmd, 0);
|
|---|
| 216 | /* Wait until hc is halted */
|
|---|
| 217 | while ((EHCI_RD(ehci_regs->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
|
|---|
| 218 | usb_log_info("EHCI turned off.\n");
|
|---|
| 219 | } else {
|
|---|
| 220 | usb_log_info("EHCI was not running.\n");
|
|---|
| 221 | }
|
|---|
| 222 | usb_log_debug("Registers: \n"
|
|---|
| 223 | "\t USBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)\n"
|
|---|
| 224 | "\t USBSTS(%p): %x(0x00001000 = HC halted)\n"
|
|---|
| 225 | "\t USBINT(%p): %x(0x0 = no interrupts).\n"
|
|---|
| 226 | "\t CONFIG(%p): %x(0x0 = ports controlled by companion hc).\n",
|
|---|
| 227 | &ehci_regs->usbcmd, EHCI_RD(ehci_regs->usbcmd),
|
|---|
| 228 | &ehci_regs->usbsts, EHCI_RD(ehci_regs->usbsts),
|
|---|
| 229 | &ehci_regs->usbintr, EHCI_RD(ehci_regs->usbintr),
|
|---|
| 230 | &ehci_regs->configflag, EHCI_RD(ehci_regs->configflag));
|
|---|
| 231 |
|
|---|
| 232 | return ret;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /**
|
|---|
| 236 | * @}
|
|---|
| 237 | */
|
|---|