1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
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 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @addtogroup drvusbehci
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * PCI related functions needed by the EHCI driver.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <errno.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include <assert.h>
|
---|
42 | #include <ddf/driver.h>
|
---|
43 | #include <ddi.h>
|
---|
44 | #include <usb/debug.h>
|
---|
45 | #include <device/hw_res_parsed.h>
|
---|
46 | #include <pci_dev_iface.h>
|
---|
47 |
|
---|
48 | #include "hc.h"
|
---|
49 | #include "res.h"
|
---|
50 | #include "ehci_regs.h"
|
---|
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 |
|
---|
57 | #define DEFAULT_WAIT 1000
|
---|
58 | #define WAIT_STEP 10
|
---|
59 |
|
---|
60 | /** Implements BIOS hands-off routine as described in EHCI spec
|
---|
61 | *
|
---|
62 | * @param device EHCI device
|
---|
63 | * @param eecp Value of EHCI Extended Capabilities pointer.
|
---|
64 | * @return Error code.
|
---|
65 | */
|
---|
66 | static errno_t disable_extended_caps(async_sess_t *parent_sess, unsigned eecp)
|
---|
67 | {
|
---|
68 | /* nothing to do */
|
---|
69 | if (eecp == 0)
|
---|
70 | return EOK;
|
---|
71 |
|
---|
72 | /* Read the first EEC. i.e. Legacy Support register */
|
---|
73 | uint32_t usblegsup;
|
---|
74 | errno_t ret = pci_config_space_read_32(parent_sess,
|
---|
75 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
76 | if (ret != EOK) {
|
---|
77 | usb_log_error("Failed to read USBLEGSUP: %s.", str_error(ret));
|
---|
78 | return ret;
|
---|
79 | }
|
---|
80 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Request control from firmware/BIOS by writing 1 to highest
|
---|
84 | * byte. (OS Control semaphore)
|
---|
85 | */
|
---|
86 | usb_log_debug("Requesting OS control.");
|
---|
87 | ret = pci_config_space_write_8(parent_sess,
|
---|
88 | eecp + USBLEGSUP_OFFSET + 3, 1);
|
---|
89 | if (ret != EOK) {
|
---|
90 | usb_log_error("Failed to request OS EHCI control: %s.",
|
---|
91 | str_error(ret));
|
---|
92 | return ret;
|
---|
93 | }
|
---|
94 |
|
---|
95 | size_t wait = 0;
|
---|
96 | /* Wait for BIOS to release control. */
|
---|
97 | ret = pci_config_space_read_32(
|
---|
98 | parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
99 | while ((ret == EOK) && (wait < DEFAULT_WAIT) &&
|
---|
100 | (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
|
---|
101 | fibril_usleep(WAIT_STEP);
|
---|
102 | ret = pci_config_space_read_32(parent_sess,
|
---|
103 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
104 | wait += WAIT_STEP;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
|
---|
108 | usb_log_info("BIOS released control after %zu usec.", wait);
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* BIOS failed to hand over control, this should not happen. */
|
---|
113 | usb_log_warning("BIOS failed to release control after "
|
---|
114 | "%zu usecs, force it.", wait);
|
---|
115 | ret = pci_config_space_write_32(parent_sess,
|
---|
116 | eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
|
---|
117 | if (ret != EOK) {
|
---|
118 | usb_log_error("Failed to force OS control: %s.",
|
---|
119 | str_error(ret));
|
---|
120 | return ret;
|
---|
121 | }
|
---|
122 |
|
---|
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 | if (ret != EOK) {
|
---|
135 | usb_log_error("Failed to get USBLEGCTLSTS: %s.",
|
---|
136 | str_error(ret));
|
---|
137 | return ret;
|
---|
138 | }
|
---|
139 | usb_log_debug2("USBLEGCTLSTS: %" PRIx32 ".", usblegctlsts);
|
---|
140 | /*
|
---|
141 | * Zero SMI enables in legacy control register.
|
---|
142 | * It should prevent pre-OS code from
|
---|
143 | * interfering. NOTE: Three upper bits are WC
|
---|
144 | */
|
---|
145 | ret = pci_config_space_write_32(parent_sess,
|
---|
146 | eecp + USBLEGCTLSTS_OFFSET, 0xe0000000);
|
---|
147 | if (ret != EOK) {
|
---|
148 | usb_log_error("Failed to zero USBLEGCTLSTS: %s",
|
---|
149 | str_error(ret));
|
---|
150 | return ret;
|
---|
151 | }
|
---|
152 |
|
---|
153 | udelay(10);
|
---|
154 | /* read again to amke sure it's zeroed */
|
---|
155 | ret = pci_config_space_read_32(parent_sess,
|
---|
156 | eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
|
---|
157 | if (ret != EOK) {
|
---|
158 | usb_log_error("Failed to get USBLEGCTLSTS 2: %s.",
|
---|
159 | str_error(ret));
|
---|
160 | return ret;
|
---|
161 | }
|
---|
162 | usb_log_debug2("Zeroed USBLEGCTLSTS: %" PRIx32 ".",
|
---|
163 | usblegctlsts);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* Read again Legacy Support register */
|
---|
167 | ret = pci_config_space_read_32(parent_sess,
|
---|
168 | eecp + USBLEGSUP_OFFSET, &usblegsup);
|
---|
169 | if (ret != EOK) {
|
---|
170 | usb_log_error("Failed to read USBLEGSUP: %s.",
|
---|
171 | str_error(ret));
|
---|
172 | return ret;
|
---|
173 | }
|
---|
174 | usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
|
---|
175 | return ret;
|
---|
176 | }
|
---|
177 |
|
---|
178 | errno_t disable_legacy(hc_device_t *hcd)
|
---|
179 | {
|
---|
180 | hc_t *hc = hcd_to_hc(hcd);
|
---|
181 |
|
---|
182 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
183 | if (parent_sess == NULL)
|
---|
184 | return ENOMEM;
|
---|
185 |
|
---|
186 | usb_log_debug("Disabling EHCI legacy support.");
|
---|
187 |
|
---|
188 | const uint32_t hcc_params = EHCI_RD(hc->caps->hccparams);
|
---|
189 | usb_log_debug2("Value of hcc params register: %x.", hcc_params);
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Read value of EHCI Extended Capabilities Pointer
|
---|
193 | * position of EEC registers (points to PCI config space)
|
---|
194 | */
|
---|
195 | const uint32_t eecp =
|
---|
196 | (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
|
---|
197 | usb_log_debug2("Value of EECP: %x.", eecp);
|
---|
198 |
|
---|
199 | int ret = disable_extended_caps(parent_sess, eecp);
|
---|
200 | if (ret != EOK) {
|
---|
201 | usb_log_error("Failed to disable extended capabilities: %s.",
|
---|
202 | str_error(ret));
|
---|
203 | goto clean;
|
---|
204 | }
|
---|
205 | clean:
|
---|
206 | async_hangup(parent_sess);
|
---|
207 | return ret;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * @}
|
---|
212 | */
|
---|