blob: d7ba82c56a2180611ae02e708020e548b9038f6b [file] [log] [blame]
Patrick Georgi6615ef32010-08-13 09:18:58 +00001/*
2 * This file is part of the libpayload project.
3 *
Nico Huber90292652013-06-13 14:37:15 +02004 * Copyright (C) 2013 secunet Security Networks AG
Patrick Georgi6615ef32010-08-13 09:18:58 +00005 *
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 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. 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 * 3. 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 AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Julius Werner1f864342013-09-03 17:15:31 -070030//#define USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +000031
Nico Huber90292652013-06-13 14:37:15 +020032#include <usb/usb.h>
33#include "generic_hub.h"
Patrick Georgi6615ef32010-08-13 09:18:58 +000034#include "xhci_private.h"
35#include "xhci.h"
36
Nico Huber90292652013-06-13 14:37:15 +020037static int
38xhci_rh_hub_status_changed(usbdev_t *const dev)
Patrick Georgi6615ef32010-08-13 09:18:58 +000039{
Nico Huber90292652013-06-13 14:37:15 +020040 xhci_t *const xhci = XHCI_INST(dev->controller);
41 const int changed = !!(xhci->opreg->usbsts & USBSTS_PCD);
42 if (changed)
43 xhci->opreg->usbsts =
44 (xhci->opreg->usbsts & USBSTS_PRSRV_MASK) | USBSTS_PCD;
45 return changed;
Patrick Georgi6615ef32010-08-13 09:18:58 +000046}
47
48static int
Nico Huber90292652013-06-13 14:37:15 +020049xhci_rh_port_status_changed(usbdev_t *const dev, const int port)
Patrick Georgi6615ef32010-08-13 09:18:58 +000050{
Nico Huber90292652013-06-13 14:37:15 +020051 xhci_t *const xhci = XHCI_INST(dev->controller);
52 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
53
Shawn Nematbakhshe1dad0c2014-03-10 14:46:06 -070054 const int changed = !!(*portsc & (PORTSC_CSC | PORTSC_PRC));
Nico Huber90292652013-06-13 14:37:15 +020055 /* always clear all the status change bits */
Julius Werner6f4c7a62015-08-07 15:29:41 -070056 *portsc = (*portsc & PORTSC_RW_MASK) | 0x00fe0000;
Nico Huber90292652013-06-13 14:37:15 +020057 return changed;
58}
59
60static int
61xhci_rh_port_connected(usbdev_t *const dev, const int port)
62{
63 xhci_t *const xhci = XHCI_INST(dev->controller);
64 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
65
66 return *portsc & PORTSC_CCS;
67}
68
69static int
70xhci_rh_port_in_reset(usbdev_t *const dev, const int port)
71{
72 xhci_t *const xhci = XHCI_INST(dev->controller);
73 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
74
75 return !!(*portsc & PORTSC_PR);
76}
77
78static int
79xhci_rh_port_enabled(usbdev_t *const dev, const int port)
80{
81 xhci_t *const xhci = XHCI_INST(dev->controller);
82 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
83
84 return !!(*portsc & PORTSC_PED);
85}
86
Julius Wernere00ba212013-09-24 20:03:54 -070087static usb_speed
Nico Huber90292652013-06-13 14:37:15 +020088xhci_rh_port_speed(usbdev_t *const dev, const int port)
89{
90 xhci_t *const xhci = XHCI_INST(dev->controller);
91 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
92
93 if (*portsc & PORTSC_PED) {
94 return ((*portsc & PORTSC_PORT_SPEED_MASK)
95 >> PORTSC_PORT_SPEED_START)
96 - 1;
97 } else {
Patrick Georgi6615ef32010-08-13 09:18:58 +000098 return -1;
Patrick Georgi6615ef32010-08-13 09:18:58 +000099 }
Patrick Georgi6615ef32010-08-13 09:18:58 +0000100}
101
Nico Huber90292652013-06-13 14:37:15 +0200102static int
Shawn Nematbakhsha16029a2014-03-10 14:12:29 -0700103xhci_rh_reset_port(usbdev_t *const dev, const int port)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000104{
Nico Huber90292652013-06-13 14:37:15 +0200105 xhci_t *const xhci = XHCI_INST(dev->controller);
106 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
107
Shawn Nematbakhsha16029a2014-03-10 14:12:29 -0700108 /* Trigger port reset. */
Nico Huber90292652013-06-13 14:37:15 +0200109 *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PR;
Shawn Nematbakhsha16029a2014-03-10 14:12:29 -0700110
111 /* Wait for port_in_reset == 0, up to 150 * 1000us = 150ms */
112 if (generic_hub_wait_for_port(dev, port, 0, xhci_rh_port_in_reset,
113 150, 1000) == 0)
114 usb_debug("xhci_rh: Reset timed out at port %d\n", port);
115 else
116 /* Clear reset status bits, since port is out of reset. */
117 *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PRC | PORTSC_WRC;
118
Nico Huber90292652013-06-13 14:37:15 +0200119 return 0;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000120}
121
Yidi Lind42ee152015-05-07 15:36:04 +0800122static int
123xhci_rh_enable_port(usbdev_t *const dev, int port)
124{
125 if (IS_ENABLED(CONFIG_LP_USB_XHCI_MTK_QUIRK)) {
126 xhci_t *const xhci = XHCI_INST(dev->controller);
127 volatile u32 *const portsc =
128 &xhci->opreg->prs[port - 1].portsc;
129
130 /*
131 * Before sending commands to a port, the Port Power in
132 * PORTSC register should be enabled on MTK's xHCI.
133 */
134 *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PP;
135 }
136 return 0;
137}
138
139
Nico Huber90292652013-06-13 14:37:15 +0200140static const generic_hub_ops_t xhci_rh_ops = {
141 .hub_status_changed = xhci_rh_hub_status_changed,
142 .port_status_changed = xhci_rh_port_status_changed,
143 .port_connected = xhci_rh_port_connected,
144 .port_in_reset = xhci_rh_port_in_reset,
145 .port_enabled = xhci_rh_port_enabled,
146 .port_speed = xhci_rh_port_speed,
Yidi Lind42ee152015-05-07 15:36:04 +0800147 .enable_port = xhci_rh_enable_port,
Nico Huber90292652013-06-13 14:37:15 +0200148 .disable_port = NULL,
Shawn Nematbakhsha16029a2014-03-10 14:12:29 -0700149 .start_port_reset = NULL,
150 .reset_port = xhci_rh_reset_port,
Nico Huber90292652013-06-13 14:37:15 +0200151};
Patrick Georgi6615ef32010-08-13 09:18:58 +0000152
153void
154xhci_rh_init (usbdev_t *dev)
155{
Patrick Georgi6615ef32010-08-13 09:18:58 +0000156 /* we can set them here because a root hub _really_ shouldn't
157 appear elsewhere */
158 dev->address = 0;
159 dev->hub = -1;
160 dev->port = -1;
161
Nico Huber90292652013-06-13 14:37:15 +0200162 const int num_ports = /* TODO: maybe we need to read extended caps */
163 (XHCI_INST(dev->controller)->capreg->hcsparams1 >> 24) & 0xff;
164 generic_hub_init(dev, num_ports, &xhci_rh_ops);
165
166 usb_debug("xHCI: root hub init done\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000167}