source: mainline/uspace/drv/bus/usb/ehci/res.c@ 1b973dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b973dc was 1b973dc, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

merge libdrv cleanup

  • Property mode set to 100644
File size: 7.9 KB
Line 
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 */
64static 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 ((wait < DEFAULT_WAIT) && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
103 async_usleep(WAIT_STEP);
104 ret = pci_config_space_read_32(parent_sess,
105 eecp + USBLEGSUP_OFFSET, &usblegsup);
106 wait += WAIT_STEP;
107 }
108
109 if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
110 usb_log_info("BIOS released control after %zu usec.\n", wait);
111 async_hangup(parent_sess);
112 return EOK;
113 }
114
115 /* BIOS failed to hand over control, this should not happen. */
116 usb_log_warning( "BIOS failed to release control after "
117 "%zu usecs, force it.\n", wait);
118 ret = pci_config_space_write_32(parent_sess,
119 eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
120 CHECK_RET_HANGUP_RETURN(ret, "Failed to force OS control: "
121 "%s.\n", str_error(ret));
122 /*
123 * Check capability type here, value of 01h identifies the capability
124 * as Legacy Support. This extended capability requires one additional
125 * 32-bit register for control/status information and this register is
126 * located at offset EECP+04h
127 */
128 if ((usblegsup & 0xff) == 1) {
129 /* Read the second EEC Legacy Support and Control register */
130 uint32_t usblegctlsts;
131 ret = pci_config_space_read_32(parent_sess,
132 eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
133 CHECK_RET_HANGUP_RETURN(ret, "Failed to get USBLEGCTLSTS: %s.\n",
134 str_error(ret));
135 usb_log_debug("USBLEGCTLSTS: %" PRIx32 ".\n", usblegctlsts);
136 /*
137 * Zero SMI enables in legacy control register.
138 * It should prevent pre-OS code from
139 * interfering. NOTE: Three upper bits are WC
140 */
141 ret = pci_config_space_write_32(parent_sess,
142 eecp + USBLEGCTLSTS_OFFSET, 0xe0000000);
143 CHECK_RET_HANGUP_RETURN(ret, "Failed(%d) zero USBLEGCTLSTS.\n", ret);
144 udelay(10);
145 ret = pci_config_space_read_32(parent_sess,
146 eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
147 CHECK_RET_HANGUP_RETURN(ret, "Failed to get USBLEGCTLSTS 2: %s.\n",
148 str_error(ret));
149 usb_log_debug("Zeroed USBLEGCTLSTS: %" PRIx32 ".\n",
150 usblegctlsts);
151 }
152
153 /* Read again Legacy Support register */
154 ret = pci_config_space_read_32(parent_sess,
155 eecp + USBLEGSUP_OFFSET, &usblegsup);
156 CHECK_RET_HANGUP_RETURN(ret, "Failed to read USBLEGSUP: %s.\n",
157 str_error(ret));
158 usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
159 async_hangup(parent_sess);
160 return EOK;
161#undef CHECK_RET_HANGUP_RETURN
162}
163
164int disable_legacy(ddf_dev_t *device, addr_range_t *reg_range)
165{
166 assert(device);
167 usb_log_debug("Disabling EHCI legacy support.\n");
168
169 /* Map EHCI registers */
170 void *regs = NULL;
171 int ret = pio_enable_range(reg_range, &regs);
172 if (ret != EOK) {
173 usb_log_error("Failed to map registers %p: %s.\n",
174 RNGABSPTR(*reg_range), str_error(ret));
175 return ret;
176 }
177
178 usb_log_debug2("Registers mapped at: %p.\n", regs);
179
180 ehci_caps_regs_t *ehci_caps = regs;
181
182 const uint32_t hcc_params = EHCI_RD(ehci_caps->hccparams);
183 usb_log_debug("Value of hcc params register: %x.\n", hcc_params);
184
185 /* Read value of EHCI Extended Capabilities Pointer
186 * position of EEC registers (points to PCI config space) */
187 const uint32_t eecp =
188 (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
189 usb_log_debug("Value of EECP: %x.\n", eecp);
190
191 ret = disable_extended_caps(device, eecp);
192 if (ret != EOK) {
193 usb_log_error("Failed to disable extended capabilities: %s.\n",
194 str_error(ret));
195 return ret;
196 }
197
198
199 /*
200 * TURN OFF EHCI FOR NOW, DRIVER WILL REINITIALIZE IT IF NEEDED
201 */
202
203 /* Get size of capability registers in memory space. */
204 const unsigned operation_offset = EHCI_RD8(ehci_caps->caplength);
205 usb_log_debug("USBCMD offset: %d.\n", operation_offset);
206
207 ehci_regs_t *ehci_regs = regs + operation_offset;
208
209 usb_log_debug("USBCMD value: %x.\n", EHCI_RD(ehci_regs->usbcmd));
210 if (EHCI_RD(ehci_regs->usbcmd) & USB_CMD_RUN_FLAG) {
211 EHCI_WR(ehci_regs->usbintr, 0); /* disable all interrupts */
212 EHCI_WR(ehci_regs->usbsts, 0x3f); /* ack all interrupts */
213 EHCI_WR(ehci_regs->configflag, 0); /* release RH ports */
214 EHCI_WR(ehci_regs->usbcmd, 0);
215 /* Wait until hc is halted */
216 while ((EHCI_RD(ehci_regs->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
217 usb_log_info("EHCI turned off.\n");
218 } else {
219 usb_log_info("EHCI was not running.\n");
220 }
221 usb_log_debug("Registers: \n"
222 "\t USBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)\n"
223 "\t USBSTS(%p): %x(0x00001000 = HC halted)\n"
224 "\t USBINT(%p): %x(0x0 = no interrupts).\n"
225 "\t CONFIG(%p): %x(0x0 = ports controlled by companion hc).\n",
226 &ehci_regs->usbcmd, EHCI_RD(ehci_regs->usbcmd),
227 &ehci_regs->usbsts, EHCI_RD(ehci_regs->usbsts),
228 &ehci_regs->usbintr, EHCI_RD(ehci_regs->usbintr),
229 &ehci_regs->configflag, EHCI_RD(ehci_regs->configflag));
230
231 return ret;
232}
233
234/**
235 * @}
236 */
Note: See TracBrowser for help on using the repository browser.