[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 | */
|
---|
[79ae36dd] | 28 |
|
---|
[40a5d40] | 29 | /**
|
---|
[0969e45e] | 30 | * @addtogroup drvusbehci
|
---|
[40a5d40] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
[0969e45e] | 35 | * PCI related functions needed by the EHCI driver.
|
---|
[40a5d40] | 36 | */
|
---|
[79ae36dd] | 37 |
|
---|
[40a5d40] | 38 | #include <errno.h>
|
---|
[109d55c] | 39 | #include <str_error.h>
|
---|
[40a5d40] | 40 | #include <assert.h>
|
---|
| 41 | #include <devman.h>
|
---|
| 42 | #include <ddi.h>
|
---|
| 43 | #include <usb/debug.h>
|
---|
[dcffe95] | 44 | #include <device/hw_res_parsed.h>
|
---|
[99e8fb7b] | 45 | #include <pci_dev_iface.h>
|
---|
[40a5d40] | 46 |
|
---|
[dcffe95] | 47 | #include "res.h"
|
---|
[d3dd96e2] | 48 | #include "ehci_regs.h"
|
---|
[40a5d40] | 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 |
|
---|
[17d1542] | 55 | #define DEFAULT_WAIT 1000
|
---|
[40a5d40] | 56 | #define WAIT_STEP 10
|
---|
| 57 |
|
---|
[6e5369b] | 58 | /** Implements BIOS hands-off routine as described in EHCI spec
|
---|
[13927cf] | 59 | *
|
---|
[6e5369b] | 60 | * @param device EHCI device
|
---|
| 61 | * @param eecp Value of EHCI Extended Capabilities pointer.
|
---|
[13927cf] | 62 | * @return Error code.
|
---|
| 63 | */
|
---|
[56fd7cf] | 64 | static int disable_extended_caps(ddf_dev_t *device, unsigned eecp)
|
---|
[40a5d40] | 65 | {
|
---|
[6e5369b] | 66 | /* nothing to do */
|
---|
| 67 | if (eecp == 0)
|
---|
| 68 | return EOK;
|
---|
| 69 |
|
---|
[dcffe95] | 70 | async_sess_t *parent_sess = devman_parent_device_connect(
|
---|
[56fd7cf] | 71 | EXCHANGE_SERIALIZE, ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
---|
[dcffe95] | 72 | if (!parent_sess)
|
---|
| 73 | return ENOMEM;
|
---|
[40a5d40] | 74 |
|
---|
[6e5369b] | 75 | #define CHECK_RET_HANGUP_RETURN(ret, message...) \
|
---|
[4ed80ce8] | 76 | if (ret != EOK) { \
|
---|
| 77 | usb_log_error(message); \
|
---|
[dcffe95] | 78 | async_hangup(parent_sess); \
|
---|
[4ed80ce8] | 79 | return ret; \
|
---|
| 80 | } else (void)0
|
---|
| 81 |
|
---|
[13927cf] | 82 | /* Read the first EEC. i.e. Legacy Support register */
|
---|
[17d1542] | 83 | uint32_t usblegsup;
|
---|
[6e5369b] | 84 | int ret = pci_config_space_read_32(parent_sess,
|
---|
[dcffe95] | 85 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[6e5369b] | 86 | CHECK_RET_HANGUP_RETURN(ret,
|
---|
| 87 | "Failed to read USBLEGSUP: %s.\n", str_error(ret));
|
---|
[50340bf] | 88 | usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
|
---|
[40a5d40] | 89 |
|
---|
[6e5369b] | 90 | /* Request control from firmware/BIOS by writing 1 to highest
|
---|
| 91 | * byte. (OS Control semaphore)*/
|
---|
[17d1542] | 92 | usb_log_debug("Requesting OS control.\n");
|
---|
[dcffe95] | 93 | ret = pci_config_space_write_8(parent_sess,
|
---|
| 94 | eecp + USBLEGSUP_OFFSET + 3, 1);
|
---|
[6e5369b] | 95 | CHECK_RET_HANGUP_RETURN(ret, "Failed to request OS EHCI control: %s.\n",
|
---|
[109d55c] | 96 | str_error(ret));
|
---|
[40a5d40] | 97 |
|
---|
[4ed80ce8] | 98 | size_t wait = 0;
|
---|
[13927cf] | 99 | /* Wait for BIOS to release control. */
|
---|
[6e5369b] | 100 | ret = pci_config_space_read_32(
|
---|
| 101 | parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[fddffb2] | 102 | while ((ret == EOK) && (wait < DEFAULT_WAIT)
|
---|
| 103 | && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
|
---|
[40a5d40] | 104 | async_usleep(WAIT_STEP);
|
---|
[dcffe95] | 105 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 106 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[40a5d40] | 107 | wait += WAIT_STEP;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[17d1542] | 110 | if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
|
---|
[4125b7d] | 111 | usb_log_info("BIOS released control after %zu usec.\n", wait);
|
---|
[6e5369b] | 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 | */
|
---|
[dcffe95] | 142 | ret = pci_config_space_write_32(parent_sess,
|
---|
[6e5369b] | 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",
|
---|
[109d55c] | 149 | str_error(ret));
|
---|
[6e5369b] | 150 | usb_log_debug("Zeroed USBLEGCTLSTS: %" PRIx32 ".\n",
|
---|
| 151 | usblegctlsts);
|
---|
[40a5d40] | 152 | }
|
---|
| 153 |
|
---|
[13927cf] | 154 | /* Read again Legacy Support register */
|
---|
[dcffe95] | 155 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 156 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[6e5369b] | 157 | CHECK_RET_HANGUP_RETURN(ret, "Failed to read USBLEGSUP: %s.\n",
|
---|
| 158 | str_error(ret));
|
---|
[50340bf] | 159 | usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
|
---|
[dcffe95] | 160 | async_hangup(parent_sess);
|
---|
[6e5369b] | 161 | return EOK;
|
---|
| 162 | #undef CHECK_RET_HANGUP_RETURN
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[7de1988c] | 165 | int disable_legacy(ddf_dev_t *device, addr_range_t *reg_range)
|
---|
[6e5369b] | 166 | {
|
---|
| 167 | assert(device);
|
---|
[5b7ba8d] | 168 | usb_log_debug("Disabling EHCI legacy support.\n");
|
---|
[6e5369b] | 169 |
|
---|
| 170 | /* Map EHCI registers */
|
---|
| 171 | void *regs = NULL;
|
---|
[3f03199] | 172 | int ret = pio_enable_range(reg_range, ®s);
|
---|
| 173 | if (ret != EOK) {
|
---|
[d930980] | 174 | usb_log_error("Failed to map registers %p: %s.\n",
|
---|
[3f03199] | 175 | RNGABSPTR(*reg_range), str_error(ret));
|
---|
| 176 | return ret;
|
---|
[d930980] | 177 | }
|
---|
[6e5369b] | 178 |
|
---|
[5b7ba8d] | 179 | usb_log_debug2("Registers mapped at: %p.\n", regs);
|
---|
| 180 |
|
---|
[d3dd96e2] | 181 | ehci_caps_regs_t *ehci_caps = regs;
|
---|
| 182 |
|
---|
| 183 | const uint32_t hcc_params = EHCI_RD(ehci_caps->hccparams);
|
---|
[6e5369b] | 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 =
|
---|
[d3dd96e2] | 189 | (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
|
---|
[6e5369b] | 190 | usb_log_debug("Value of EECP: %x.\n", eecp);
|
---|
| 191 |
|
---|
| 192 | ret = disable_extended_caps(device, eecp);
|
---|
[3f03199] | 193 | if (ret != EOK) {
|
---|
[d930980] | 194 | usb_log_error("Failed to disable extended capabilities: %s.\n",
|
---|
[3f03199] | 195 | str_error(ret));
|
---|
| 196 | return ret;
|
---|
[d930980] | 197 | }
|
---|
[6e5369b] | 198 |
|
---|
[dcffe95] | 199 |
|
---|
[67352d2] | 200 | /*
|
---|
[dcffe95] | 201 | * TURN OFF EHCI FOR NOW, DRIVER WILL REINITIALIZE IT IF NEEDED
|
---|
[67352d2] | 202 | */
|
---|
[40a5d40] | 203 |
|
---|
[13927cf] | 204 | /* Get size of capability registers in memory space. */
|
---|
[d3dd96e2] | 205 | const unsigned operation_offset = EHCI_RD8(ehci_caps->caplength);
|
---|
[40a5d40] | 206 | usb_log_debug("USBCMD offset: %d.\n", operation_offset);
|
---|
[13927cf] | 207 |
|
---|
[d3dd96e2] | 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);
|
---|
[c060090] | 216 | /* Wait until hc is halted */
|
---|
[d3dd96e2] | 217 | while ((EHCI_RD(ehci_regs->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
|
---|
[40a5d40] | 218 | usb_log_info("EHCI turned off.\n");
|
---|
| 219 | } else {
|
---|
| 220 | usb_log_info("EHCI was not running.\n");
|
---|
| 221 | }
|
---|
[17d1542] | 222 | usb_log_debug("Registers: \n"
|
---|
[5b7ba8d] | 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",
|
---|
[d3dd96e2] | 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));
|
---|
[40a5d40] | 231 |
|
---|
[4ed80ce8] | 232 | return ret;
|
---|
[40a5d40] | 233 | }
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * @}
|
---|
| 237 | */
|
---|