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

Last change on this file was 5f97ef44, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Sleep is more natural as part of the fibril API.
(the implementation will move later)

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[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]66static 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
[7c3fb9b]82 /*
83 * Request control from firmware/BIOS by writing 1 to highest
84 * byte. (OS Control semaphore)
85 */
[a1732929]86 usb_log_debug("Requesting OS control.");
[dcffe95]87 ret = pci_config_space_write_8(parent_sess,
88 eecp + USBLEGSUP_OFFSET + 3, 1);
[615abda]89 if (ret != EOK) {
[a1732929]90 usb_log_error("Failed to request OS EHCI control: %s.",
[615abda]91 str_error(ret));
92 return ret;
[d930980]93 }
[40a5d40]94
[4ed80ce8]95 size_t wait = 0;
[13927cf]96 /* Wait for BIOS to release control. */
[6e5369b]97 ret = pci_config_space_read_32(
98 parent_sess, eecp + USBLEGSUP_OFFSET, &usblegsup);
[3bacee1]99 while ((ret == EOK) && (wait < DEFAULT_WAIT) &&
100 (usblegsup & USBLEGSUP_BIOS_CONTROL)) {
[5f97ef44]101 fibril_usleep(WAIT_STEP);
[dcffe95]102 ret = pci_config_space_read_32(parent_sess,
103 eecp + USBLEGSUP_OFFSET, &usblegsup);
[40a5d40]104 wait += WAIT_STEP;
105 }
106
[17d1542]107 if ((usblegsup & USBLEGSUP_BIOS_CONTROL) == 0) {
[a1732929]108 usb_log_info("BIOS released control after %zu usec.", wait);
[6e5369b]109 return EOK;
110 }
111
112 /* BIOS failed to hand over control, this should not happen. */
[3bacee1]113 usb_log_warning("BIOS failed to release control after "
[a1732929]114 "%zu usecs, force it.", wait);
[6e5369b]115 ret = pci_config_space_write_32(parent_sess,
116 eecp + USBLEGSUP_OFFSET, USBLEGSUP_OS_CONTROL);
[615abda]117 if (ret != EOK) {
[a1732929]118 usb_log_error("Failed to force OS control: %s.",
[615abda]119 str_error(ret));
120 return ret;
[d930980]121 }
122
[6e5369b]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);
[615abda]134 if (ret != EOK) {
[a1732929]135 usb_log_error("Failed to get USBLEGCTLSTS: %s.",
[615abda]136 str_error(ret));
137 return ret;
[d930980]138 }
[a1732929]139 usb_log_debug2("USBLEGCTLSTS: %" PRIx32 ".", usblegctlsts);
[6e5369b]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 */
[dcffe95]145 ret = pci_config_space_write_32(parent_sess,
[6e5369b]146 eecp + USBLEGCTLSTS_OFFSET, 0xe0000000);
[615abda]147 if (ret != EOK) {
[a1732929]148 usb_log_error("Failed to zero USBLEGCTLSTS: %s",
[615abda]149 str_error(ret));
150 return ret;
[d930980]151 }
152
[6e5369b]153 udelay(10);
[615abda]154 /* read again to amke sure it's zeroed */
[6e5369b]155 ret = pci_config_space_read_32(parent_sess,
156 eecp + USBLEGCTLSTS_OFFSET, &usblegctlsts);
[615abda]157 if (ret != EOK) {
[a1732929]158 usb_log_error("Failed to get USBLEGCTLSTS 2: %s.",
[615abda]159 str_error(ret));
160 return ret;
[d930980]161 }
[a1732929]162 usb_log_debug2("Zeroed USBLEGCTLSTS: %" PRIx32 ".",
[6e5369b]163 usblegctlsts);
[40a5d40]164 }
165
[13927cf]166 /* Read again Legacy Support register */
[dcffe95]167 ret = pci_config_space_read_32(parent_sess,
168 eecp + USBLEGSUP_OFFSET, &usblegsup);
[615abda]169 if (ret != EOK) {
[a1732929]170 usb_log_error("Failed to read USBLEGSUP: %s.",
[615abda]171 str_error(ret));
172 return ret;
[d930980]173 }
[a1732929]174 usb_log_debug2("USBLEGSUP: %" PRIx32 ".", usblegsup);
[615abda]175 return ret;
[6e5369b]176}
177
[5a6cc679]178errno_t disable_legacy(hc_device_t *hcd)
[6e5369b]179{
[32fb6bce]180 hc_t *hc = hcd_to_hc(hcd);
[615abda]181
[32fb6bce]182 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
[d15797d]183 if (parent_sess == NULL)
[615abda]184 return ENOMEM;
185
[a1732929]186 usb_log_debug("Disabling EHCI legacy support.");
[6e5369b]187
[e4d7363]188 const uint32_t hcc_params = EHCI_RD(hc->caps->hccparams);
[a1732929]189 usb_log_debug2("Value of hcc params register: %x.", hcc_params);
[6e5369b]190
[7c3fb9b]191 /*
192 * Read value of EHCI Extended Capabilities Pointer
193 * position of EEC registers (points to PCI config space)
194 */
[6e5369b]195 const uint32_t eecp =
[d3dd96e2]196 (hcc_params >> EHCI_CAPS_HCC_EECP_SHIFT) & EHCI_CAPS_HCC_EECP_MASK;
[a1732929]197 usb_log_debug2("Value of EECP: %x.", eecp);
[6e5369b]198
[e4d7363]199 int ret = disable_extended_caps(parent_sess, eecp);
[3f03199]200 if (ret != EOK) {
[a1732929]201 usb_log_error("Failed to disable extended capabilities: %s.",
[3f03199]202 str_error(ret));
[3bacee1]203 goto clean;
[d930980]204 }
[615abda]205clean:
206 async_hangup(parent_sess);
[4ed80ce8]207 return ret;
[40a5d40]208}
209
210/**
211 * @}
212 */
Note: See TracBrowser for help on using the repository browser.