| [40a5d40] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| [e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
|---|
| [40a5d40] | 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| [79ae36dd] | 29 |
|
|---|
| [40a5d40] | 30 | /**
|
|---|
| [0969e45e] | 31 | * @addtogroup drvusbehci
|
|---|
| [40a5d40] | 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /**
|
|---|
| 35 | * @file
|
|---|
| [0969e45e] | 36 | * PCI related functions needed by the EHCI driver.
|
|---|
| [40a5d40] | 37 | */
|
|---|
| [79ae36dd] | 38 |
|
|---|
| [40a5d40] | 39 | #include <errno.h>
|
|---|
| [109d55c] | 40 | #include <str_error.h>
|
|---|
| [40a5d40] | 41 | #include <assert.h>
|
|---|
| [d15797d] | 42 | #include <ddf/driver.h>
|
|---|
| [40a5d40] | 43 | #include <ddi.h>
|
|---|
| 44 | #include <usb/debug.h>
|
|---|
| [dcffe95] | 45 | #include <device/hw_res_parsed.h>
|
|---|
| [99e8fb7b] | 46 | #include <pci_dev_iface.h>
|
|---|
| [40a5d40] | 47 |
|
|---|
| [32fb6bce] | 48 | #include "hc.h"
|
|---|
| [dcffe95] | 49 | #include "res.h"
|
|---|
| [d3dd96e2] | 50 | #include "ehci_regs.h"
|
|---|
| [40a5d40] | 51 |
|
|---|
| 52 | #define USBLEGSUP_OFFSET 0
|
|---|
| 53 | #define USBLEGSUP_BIOS_CONTROL (1 << 16)
|
|---|
| 54 | #define USBLEGSUP_OS_CONTROL (1 << 24)
|
|---|
| 55 | #define USBLEGCTLSTS_OFFSET 4
|
|---|
| 56 |
|
|---|
| [17d1542] | 57 | #define DEFAULT_WAIT 1000
|
|---|
| [40a5d40] | 58 | #define WAIT_STEP 10
|
|---|
| 59 |
|
|---|
| [6e5369b] | 60 | /** Implements BIOS hands-off routine as described in EHCI spec
|
|---|
| [13927cf] | 61 | *
|
|---|
| [6e5369b] | 62 | * @param device EHCI device
|
|---|
| 63 | * @param eecp Value of EHCI Extended Capabilities pointer.
|
|---|
| [13927cf] | 64 | * @return Error code.
|
|---|
| 65 | */
|
|---|
| [5a6cc679] | 66 | static errno_t disable_extended_caps(async_sess_t *parent_sess, unsigned eecp)
|
|---|
| [40a5d40] | 67 | {
|
|---|
| [6e5369b] | 68 | /* nothing to do */
|
|---|
| 69 | if (eecp == 0)
|
|---|
| 70 | return EOK;
|
|---|
| 71 |
|
|---|
| [13927cf] | 72 | /* Read the first EEC. i.e. Legacy Support register */
|
|---|
| [17d1542] | 73 | uint32_t usblegsup;
|
|---|
| [5a6cc679] | 74 | errno_t ret = pci_config_space_read_32(parent_sess,
|
|---|
| [dcffe95] | 75 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| [615abda] | 76 | if (ret != EOK) {
|
|---|
| [a1732929] | 77 | usb_log_error("Failed to read USBLEGSUP: %s.", str_error(ret));
|
|---|
| [615abda] | 78 | return ret;
|
|---|
| [d930980] | 79 | }
|
|---|
| [a1732929] | 80 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
|---|
| [40a5d40] | 81 |
|
|---|
| [6e5369b] | 82 | /* Request control from firmware/BIOS by writing 1 to highest
|
|---|
| 83 | * byte. (OS Control semaphore)*/
|
|---|
| [a1732929] | 84 | usb_log_debug("Requesting OS control.");
|
|---|
| [dcffe95] | 85 | ret = pci_config_space_write_8(parent_sess,
|
|---|
| 86 | eecp + USBLEGSUP_OFFSET + 3, 1);
|
|---|
| [615abda] | 87 | if (ret != EOK) {
|
|---|
| [a1732929] | 88 | usb_log_error("Failed to request OS EHCI control: %s.",
|
|---|
| [615abda] | 89 | str_error(ret));
|
|---|
| 90 | return ret;
|
|---|
| [d930980] | 91 | }
|
|---|
| [40a5d40] | 92 |
|
|---|
| [4ed80ce8] | 93 | size_t wait = 0;
|
|---|
| [13927cf] | 94 | /* Wait for BIOS to release control. */
|
|---|
| [6e5369b] | 95 | ret = pci_config_space_read_32(
|
|---|
| 96 | parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| [fddffb2] | 97 | while ((ret == EOK) && (wait < DEFAULT_WAIT)
|
|---|
| 98 | && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
|
|---|
| [40a5d40] | 99 | async_usleep(WAIT_STEP);
|
|---|
| [dcffe95] | 100 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 101 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| [40a5d40] | 102 | wait += WAIT_STEP;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| [17d1542] | 105 | if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
|
|---|
| [a1732929] | 106 | usb_log_info("BIOS released control after %zu usec.", wait);
|
|---|
| [6e5369b] | 107 | return EOK;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* BIOS failed to hand over control, this should not happen. */
|
|---|
| 111 | usb_log_warning( "BIOS failed to release control after "
|
|---|
| [a1732929] | 112 | "%zu usecs, force it.", wait);
|
|---|
| [6e5369b] | 113 | ret = pci_config_space_write_32(parent_sess,
|
|---|
| 114 | eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
|
|---|
| [615abda] | 115 | if (ret != EOK) {
|
|---|
| [a1732929] | 116 | usb_log_error("Failed to force OS control: %s.",
|
|---|
| [615abda] | 117 | str_error(ret));
|
|---|
| 118 | return ret;
|
|---|
| [d930980] | 119 | }
|
|---|
| 120 |
|
|---|
| [6e5369b] | 121 | /*
|
|---|
| 122 | * Check capability type here, value of 01h identifies the capability
|
|---|
| 123 | * as Legacy Support. This extended capability requires one additional
|
|---|
| 124 | * 32-bit register for control/status information and this register is
|
|---|
| 125 | * located at offset EECP+04h
|
|---|
| 126 | */
|
|---|
| 127 | if ((usblegsup & 0xff) == 1) {
|
|---|
| 128 | /* Read the second EEC Legacy Support and Control register */
|
|---|
| 129 | uint32_t usblegctlsts;
|
|---|
| 130 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 131 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
|---|
| [615abda] | 132 | if (ret != EOK) {
|
|---|
| [a1732929] | 133 | usb_log_error("Failed to get USBLEGCTLSTS: %s.",
|
|---|
| [615abda] | 134 | str_error(ret));
|
|---|
| 135 | return ret;
|
|---|
| [d930980] | 136 | }
|
|---|
| [a1732929] | 137 | usb_log_debug2("USBLEGCTLSTS: %" PRIx32 ".", usblegctlsts);
|
|---|
| [6e5369b] | 138 | /*
|
|---|
| 139 | * Zero SMI enables in legacy control register.
|
|---|
| 140 | * It should prevent pre-OS code from
|
|---|
| 141 | * interfering. NOTE: Three upper bits are WC
|
|---|
| 142 | */
|
|---|
| [dcffe95] | 143 | ret = pci_config_space_write_32(parent_sess,
|
|---|
| [6e5369b] | 144 | eecp + USBLEGCTLSTS_OFFSET, 0xe0000000);
|
|---|
| [615abda] | 145 | if (ret != EOK) {
|
|---|
| [a1732929] | 146 | usb_log_error("Failed to zero USBLEGCTLSTS: %s",
|
|---|
| [615abda] | 147 | str_error(ret));
|
|---|
| 148 | return ret;
|
|---|
| [d930980] | 149 | }
|
|---|
| 150 |
|
|---|
| [6e5369b] | 151 | udelay(10);
|
|---|
| [615abda] | 152 | /* read again to amke sure it's zeroed */
|
|---|
| [6e5369b] | 153 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 154 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
|---|
| [615abda] | 155 | if (ret != EOK) {
|
|---|
| [a1732929] | 156 | usb_log_error("Failed to get USBLEGCTLSTS 2: %s.",
|
|---|
| [615abda] | 157 | str_error(ret));
|
|---|
| 158 | return ret;
|
|---|
| [d930980] | 159 | }
|
|---|
| [a1732929] | 160 | usb_log_debug2("Zeroed USBLEGCTLSTS: %" PRIx32 ".",
|
|---|
| [6e5369b] | 161 | usblegctlsts);
|
|---|
| [40a5d40] | 162 | }
|
|---|
| 163 |
|
|---|
| [13927cf] | 164 | /* Read again Legacy Support register */
|
|---|
| [dcffe95] | 165 | ret = pci_config_space_read_32(parent_sess,
|
|---|
| 166 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
|---|
| [615abda] | 167 | if (ret != EOK) {
|
|---|
| [a1732929] | 168 | usb_log_error("Failed to read USBLEGSUP: %s.",
|
|---|
| [615abda] | 169 | str_error(ret));
|
|---|
| 170 | return ret;
|
|---|
| [d930980] | 171 | }
|
|---|
| [a1732929] | 172 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
|---|
| [615abda] | 173 | return ret;
|
|---|
| [6e5369b] | 174 | }
|
|---|
| 175 |
|
|---|
| [5a6cc679] | 176 | errno_t disable_legacy(hc_device_t *hcd)
|
|---|
| [6e5369b] | 177 | {
|
|---|
| [32fb6bce] | 178 | hc_t *hc = hcd_to_hc(hcd);
|
|---|
| [615abda] | 179 |
|
|---|
| [32fb6bce] | 180 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
|---|
| [d15797d] | 181 | if (parent_sess == NULL)
|
|---|
| [615abda] | 182 | return ENOMEM;
|
|---|
| 183 |
|
|---|
| [a1732929] | 184 | usb_log_debug("Disabling EHCI legacy support.");
|
|---|
| [6e5369b] | 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 | */
|
|---|