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
RevLine 
[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]64static 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);
[17d1542]102 while ((wait < DEFAULT_WAIT) && (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
[40a5d40]103 async_usleep(WAIT_STEP);
[dcffe95]104 ret = pci_config_space_read_32(parent_sess,
105 eecp + USBLEGSUP_OFFSET, &usblegsup);
[40a5d40]106 wait += WAIT_STEP;
107 }
108
[17d1542]109 if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
[4125b7d]110 usb_log_info("BIOS released control after %zu usec.\n", wait);
[6e5369b]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 */
[dcffe95]141 ret = pci_config_space_write_32(parent_sess,
[6e5369b]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",
[109d55c]148 str_error(ret));
[6e5369b]149 usb_log_debug("Zeroed USBLEGCTLSTS: %" PRIx32 ".\n",
150 usblegctlsts);
[40a5d40]151 }
152
[13927cf]153 /* Read again Legacy Support register */
[dcffe95]154 ret = pci_config_space_read_32(parent_sess,
155 eecp + USBLEGSUP_OFFSET, &usblegsup);
[6e5369b]156 CHECK_RET_HANGUP_RETURN(ret, "Failed to read USBLEGSUP: %s.\n",
157 str_error(ret));
[50340bf]158 usb_log_debug("USBLEGSUP: %" PRIx32 ".\n", usblegsup);
[dcffe95]159 async_hangup(parent_sess);
[6e5369b]160 return EOK;
161#undef CHECK_RET_HANGUP_RETURN
162}
163
[7de1988c]164int disable_legacy(ddf_dev_t *device, addr_range_t *reg_range)
[6e5369b]165{
166 assert(device);
[5b7ba8d]167 usb_log_debug("Disabling EHCI legacy support.\n");
[6e5369b]168
169 /* Map EHCI registers */
170 void *regs = NULL;
[3f03199]171 int ret = pio_enable_range(reg_range, &regs);
172 if (ret != EOK) {
[d930980]173 usb_log_error("Failed to map registers %p: %s.\n",
[3f03199]174 RNGABSPTR(*reg_range), str_error(ret));
175 return ret;
[d930980]176 }
[6e5369b]177
[5b7ba8d]178 usb_log_debug2("Registers mapped at: %p.\n", regs);
179
[d3dd96e2]180 ehci_caps_regs_t *ehci_caps = regs;
181
182 const uint32_t hcc_params = EHCI_RD(ehci_caps->hccparams);
[6e5369b]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 =
[d3dd96e2]188 (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
[6e5369b]189 usb_log_debug("Value of EECP: %x.\n", eecp);
190
191 ret = disable_extended_caps(device, eecp);
[3f03199]192 if (ret != EOK) {
[d930980]193 usb_log_error("Failed to disable extended capabilities: %s.\n",
[3f03199]194 str_error(ret));
195 return ret;
[d930980]196 }
[6e5369b]197
[dcffe95]198
[67352d2]199 /*
[dcffe95]200 * TURN OFF EHCI FOR NOW, DRIVER WILL REINITIALIZE IT IF NEEDED
[67352d2]201 */
[40a5d40]202
[13927cf]203 /* Get size of capability registers in memory space. */
[d3dd96e2]204 const unsigned operation_offset = EHCI_RD8(ehci_caps->caplength);
[40a5d40]205 usb_log_debug("USBCMD offset: %d.\n", operation_offset);
[13927cf]206
[d3dd96e2]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);
[c060090]215 /* Wait until hc is halted */
[d3dd96e2]216 while ((EHCI_RD(ehci_regs->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
[40a5d40]217 usb_log_info("EHCI turned off.\n");
218 } else {
219 usb_log_info("EHCI was not running.\n");
220 }
[17d1542]221 usb_log_debug("Registers: \n"
[5b7ba8d]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",
[d3dd96e2]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));
[40a5d40]230
[4ed80ce8]231 return ret;
[40a5d40]232}
233
234/**
235 * @}
236 */
Note: See TracBrowser for help on using the repository browser.