[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>
|
---|
[d15797d] | 41 | #include <ddf/driver.h>
|
---|
[40a5d40] | 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 |
|
---|
[32fb6bce] | 47 | #include "hc.h"
|
---|
[dcffe95] | 48 | #include "res.h"
|
---|
[d3dd96e2] | 49 | #include "ehci_regs.h"
|
---|
[40a5d40] | 50 |
|
---|
| 51 | #define USBLEGSUP_OFFSET 0
|
---|
| 52 | #define USBLEGSUP_BIOS_CONTROL (1 << 16)
|
---|
| 53 | #define USBLEGSUP_OS_CONTROL (1 << 24)
|
---|
| 54 | #define USBLEGCTLSTS_OFFSET 4
|
---|
| 55 |
|
---|
[17d1542] | 56 | #define DEFAULT_WAIT 1000
|
---|
[40a5d40] | 57 | #define WAIT_STEP 10
|
---|
| 58 |
|
---|
[6e5369b] | 59 | /** Implements BIOS hands-off routine as described in EHCI spec
|
---|
[13927cf] | 60 | *
|
---|
[6e5369b] | 61 | * @param device EHCI device
|
---|
| 62 | * @param eecp Value of EHCI Extended Capabilities pointer.
|
---|
[13927cf] | 63 | * @return Error code.
|
---|
| 64 | */
|
---|
[615abda] | 65 | static int disable_extended_caps(async_sess_t *parent_sess, unsigned eecp)
|
---|
[40a5d40] | 66 | {
|
---|
[6e5369b] | 67 | /* nothing to do */
|
---|
| 68 | if (eecp == 0)
|
---|
| 69 | return EOK;
|
---|
| 70 |
|
---|
[13927cf] | 71 | /* Read the first EEC. i.e. Legacy Support register */
|
---|
[17d1542] | 72 | uint32_t usblegsup;
|
---|
[6e5369b] | 73 | int ret = pci_config_space_read_32(parent_sess,
|
---|
[dcffe95] | 74 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[615abda] | 75 | if (ret != EOK) {
|
---|
[a1732929] | 76 | usb_log_error("Failed to read USBLEGSUP: %s.", str_error(ret));
|
---|
[615abda] | 77 | return ret;
|
---|
[d930980] | 78 | }
|
---|
[a1732929] | 79 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
---|
[40a5d40] | 80 |
|
---|
[6e5369b] | 81 | /* Request control from firmware/BIOS by writing 1 to highest
|
---|
| 82 | * byte. (OS Control semaphore)*/
|
---|
[a1732929] | 83 | usb_log_debug("Requesting OS control.");
|
---|
[dcffe95] | 84 | ret = pci_config_space_write_8(parent_sess,
|
---|
| 85 | eecp + USBLEGSUP_OFFSET + 3, 1);
|
---|
[615abda] | 86 | if (ret != EOK) {
|
---|
[a1732929] | 87 | usb_log_error("Failed to request OS EHCI control: %s.",
|
---|
[615abda] | 88 | str_error(ret));
|
---|
| 89 | return ret;
|
---|
[d930980] | 90 | }
|
---|
[40a5d40] | 91 |
|
---|
[4ed80ce8] | 92 | size_t wait = 0;
|
---|
[13927cf] | 93 | /* Wait for BIOS to release control. */
|
---|
[6e5369b] | 94 | ret = pci_config_space_read_32(
|
---|
| 95 | parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[fddffb2] | 96 | while ((ret == EOK) && (wait < DEFAULT_WAIT)
|
---|
| 97 | && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
|
---|
[40a5d40] | 98 | async_usleep(WAIT_STEP);
|
---|
[dcffe95] | 99 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 100 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[40a5d40] | 101 | wait += WAIT_STEP;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[17d1542] | 104 | if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
|
---|
[a1732929] | 105 | usb_log_info("BIOS released control after %zu usec.", wait);
|
---|
[6e5369b] | 106 | return EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* BIOS failed to hand over control, this should not happen. */
|
---|
| 110 | usb_log_warning( "BIOS failed to release control after "
|
---|
[a1732929] | 111 | "%zu usecs, force it.", wait);
|
---|
[6e5369b] | 112 | ret = pci_config_space_write_32(parent_sess,
|
---|
| 113 | eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
|
---|
[615abda] | 114 | if (ret != EOK) {
|
---|
[a1732929] | 115 | usb_log_error("Failed to force OS control: %s.",
|
---|
[615abda] | 116 | str_error(ret));
|
---|
| 117 | return ret;
|
---|
[d930980] | 118 | }
|
---|
| 119 |
|
---|
[6e5369b] | 120 | /*
|
---|
| 121 | * Check capability type here, value of 01h identifies the capability
|
---|
| 122 | * as Legacy Support. This extended capability requires one additional
|
---|
| 123 | * 32-bit register for control/status information and this register is
|
---|
| 124 | * located at offset EECP+04h
|
---|
| 125 | */
|
---|
| 126 | if ((usblegsup & 0xff) == 1) {
|
---|
| 127 | /* Read the second EEC Legacy Support and Control register */
|
---|
| 128 | uint32_t usblegctlsts;
|
---|
| 129 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 130 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
---|
[615abda] | 131 | if (ret != EOK) {
|
---|
[a1732929] | 132 | usb_log_error("Failed to get USBLEGCTLSTS: %s.",
|
---|
[615abda] | 133 | str_error(ret));
|
---|
| 134 | return ret;
|
---|
[d930980] | 135 | }
|
---|
[a1732929] | 136 | usb_log_debug2("USBLEGCTLSTS: %" PRIx32 ".", usblegctlsts);
|
---|
[6e5369b] | 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);
|
---|
[615abda] | 144 | if (ret != EOK) {
|
---|
[a1732929] | 145 | usb_log_error("Failed to zero USBLEGCTLSTS: %s",
|
---|
[615abda] | 146 | str_error(ret));
|
---|
| 147 | return ret;
|
---|
[d930980] | 148 | }
|
---|
| 149 |
|
---|
[6e5369b] | 150 | udelay(10);
|
---|
[615abda] | 151 | /* read again to amke sure it's zeroed */
|
---|
[6e5369b] | 152 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 153 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
---|
[615abda] | 154 | if (ret != EOK) {
|
---|
[a1732929] | 155 | usb_log_error("Failed to get USBLEGCTLSTS 2: %s.",
|
---|
[615abda] | 156 | str_error(ret));
|
---|
| 157 | return ret;
|
---|
[d930980] | 158 | }
|
---|
[a1732929] | 159 | usb_log_debug2("Zeroed USBLEGCTLSTS: %" PRIx32 ".",
|
---|
[6e5369b] | 160 | usblegctlsts);
|
---|
[40a5d40] | 161 | }
|
---|
| 162 |
|
---|
[13927cf] | 163 | /* Read again Legacy Support register */
|
---|
[dcffe95] | 164 | ret = pci_config_space_read_32(parent_sess,
|
---|
| 165 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
[615abda] | 166 | if (ret != EOK) {
|
---|
[a1732929] | 167 | usb_log_error("Failed to read USBLEGSUP: %s.",
|
---|
[615abda] | 168 | str_error(ret));
|
---|
| 169 | return ret;
|
---|
[d930980] | 170 | }
|
---|
[a1732929] | 171 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
---|
[615abda] | 172 | return ret;
|
---|
[6e5369b] | 173 | }
|
---|
| 174 |
|
---|
[32fb6bce] | 175 | int disable_legacy(hc_device_t *hcd)
|
---|
[6e5369b] | 176 | {
|
---|
[32fb6bce] | 177 | hc_t *hc = hcd_to_hc(hcd);
|
---|
[615abda] | 178 |
|
---|
[32fb6bce] | 179 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
[d15797d] | 180 | if (parent_sess == NULL)
|
---|
[615abda] | 181 | return ENOMEM;
|
---|
| 182 |
|
---|
[a1732929] | 183 | usb_log_debug("Disabling EHCI legacy support.");
|
---|
[6e5369b] | 184 |
|
---|
[d3dd96e2] | 185 |
|
---|
[e4d7363] | 186 | const uint32_t hcc_params = EHCI_RD(hc->caps->hccparams);
|
---|
[a1732929] | 187 | usb_log_debug2("Value of hcc params register: %x.", hcc_params);
|
---|
[6e5369b] | 188 |
|
---|
| 189 | /* Read value of EHCI Extended Capabilities Pointer
|
---|
| 190 | * position of EEC registers (points to PCI config space) */
|
---|
| 191 | const uint32_t eecp =
|
---|
[d3dd96e2] | 192 | (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
|
---|
[a1732929] | 193 | usb_log_debug2("Value of EECP: %x.", eecp);
|
---|
[6e5369b] | 194 |
|
---|
[e4d7363] | 195 | int ret = disable_extended_caps(parent_sess, eecp);
|
---|
[3f03199] | 196 | if (ret != EOK) {
|
---|
[a1732929] | 197 | usb_log_error("Failed to disable extended capabilities: %s.",
|
---|
[3f03199] | 198 | str_error(ret));
|
---|
[615abda] | 199 | goto clean;
|
---|
[d930980] | 200 | }
|
---|
[615abda] | 201 | clean:
|
---|
| 202 | async_hangup(parent_sess);
|
---|
[4ed80ce8] | 203 | return ret;
|
---|
[40a5d40] | 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * @}
|
---|
| 208 | */
|
---|