mb/amb/birman*/gpio: remove configuration for VDD_MEM_VID[0,1]
[coreboot2.git] / payloads / libpayload / drivers / usb / ehci_rh.c
blobc5bcd5df953d2ea1995e62516fb2f962ad4a12aa
1 /*
3 * Copyright (C) 2010 coresystems GmbH
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 //#define USB_DEBUG
31 #include <libpayload.h>
32 #include <kconfig.h>
33 #include "ehci.h"
34 #include "ehci_private.h"
36 typedef struct {
37 int n_ports;
38 /* typical C, n_ports is the number
39 * of ports, while ports[] spans [0,n_ports-1],
40 * even though the spec counts from 1.
42 volatile portsc_t *ports;
43 int *devices;
44 } rh_inst_t;
46 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
48 static void
49 ehci_rh_destroy(usbdev_t *dev)
51 int port;
53 /* Tear down all devices below the root hub (in bottom-up order). */
54 for (port = 0; port < RH_INST(dev)->n_ports; port++) {
55 if (RH_INST(dev)->devices[port] != -1) {
56 usb_detach_device(dev->controller,
57 RH_INST(dev)->devices[port]);
58 RH_INST(dev)->devices[port] = -1;
62 free(RH_INST(dev)->devices);
63 free(RH_INST(dev));
66 static void
67 ehci_rh_hand_over_port(usbdev_t *dev, int port)
69 usb_debug("giving up port %x, it's USB1\n", port+1);
71 /* Clear ConnectStatusChange before evaluation */
72 /* RW/C register, so clear it by writing 1 */
73 RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
75 /* Lowspeed device. Hand over to companion */
76 RH_INST(dev)->ports[port] |= P_PORT_OWNER;
78 /* TOTEST: how long to wait? trying 100ms for now */
79 int timeout = 10; /* timeout after 10 * 10ms == 100ms */
80 while (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE) && timeout--)
81 mdelay(10);
82 if (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE)) {
83 usb_debug("Warning: Handing port over to companion timed out.\n");
86 /* RW/C register, so clear it by writing 1 */
87 RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
88 return;
91 static void
92 ehci_rh_scanport(usbdev_t *dev, int port)
94 usb_speed port_speed;
96 if (RH_INST(dev)->devices[port] != -1) {
97 usb_debug("Unregister device at port %x\n", port+1);
98 usb_detach_device(dev->controller, RH_INST(dev)->devices[port]);
99 RH_INST(dev)->devices[port] = -1;
101 /* device connected, handle */
102 if (RH_INST(dev)->ports[port] & P_CURR_CONN_STATUS) {
103 mdelay(100); // usb20 spec 9.1.2
104 if (!CONFIG(LP_USB_EHCI_HOSTPC_ROOT_HUB_TT) &&
105 (RH_INST(dev)->ports[port] & P_LINE_STATUS) ==
106 P_LINE_STATUS_LOWSPEED) {
107 ehci_rh_hand_over_port(dev, port);
108 return;
111 /* Deassert enable, assert reset. These must change
112 * atomically.
114 RH_INST(dev)->ports[port] = (RH_INST(dev)->ports[port] & ~P_PORT_ENABLE) | P_PORT_RESET;
116 /* Wait a bit while reset is active (+1 to avoid Tegra race). */
117 mdelay(50 + 1); // usb20 spec 7.1.7.5 (TDRSTR)
119 /* Deassert reset. */
120 RH_INST(dev)->ports[port] &= ~P_PORT_RESET;
122 /* Wait max. 2ms (ehci spec 2.3.9) for flag change to finish. */
123 int timeout = 20; /* time out after 20 * 100us == 2ms */
124 while ((RH_INST(dev)->ports[port] & P_PORT_RESET) && timeout--)
125 udelay(100);
126 if (RH_INST(dev)->ports[port] & P_PORT_RESET) {
127 usb_debug("Error: ehci_rh: port reset timed out.\n");
128 return;
131 mdelay(10); /* TRSTRCY (USB 2.0 spec 7.1.7.5) */
133 /* If the host controller enabled the port, it's a high-speed
134 * device, otherwise it's full-speed.
136 if (!(RH_INST(dev)->ports[port] & P_PORT_ENABLE)) {
137 ehci_rh_hand_over_port(dev, port);
138 return;
140 if (CONFIG(LP_USB_EHCI_HOSTPC_ROOT_HUB_TT)) {
141 port_speed = (usb_speed)
142 ((EHCI_INST(dev->controller)->operation->hostpc
143 >> 25) & 0x03);
144 } else {
145 usb_debug("port %x hosts a USB2 device\n", port+1);
146 port_speed = HIGH_SPEED;
148 RH_INST(dev)->devices[port] = usb_attach_device(dev->controller
149 , dev->address, port, port_speed);
151 /* RW/C register, so clear it by writing 1 */
152 RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
155 static int
156 ehci_rh_report_port_changes(usbdev_t *dev)
158 int i;
159 for (i = 0; i < RH_INST(dev)->n_ports; i++) {
160 if (RH_INST(dev)->ports[i] & P_CONN_STATUS_CHANGE)
161 return i;
163 return -1;
166 static void
167 ehci_rh_poll(usbdev_t *dev)
169 int port;
170 while ((port = ehci_rh_report_port_changes(dev)) != -1)
171 ehci_rh_scanport(dev, port);
174 void
175 ehci_rh_init(usbdev_t *dev)
177 int i;
178 dev->destroy = ehci_rh_destroy;
179 dev->poll = ehci_rh_poll;
181 dev->data = xmalloc(sizeof(rh_inst_t));
182 RH_INST(dev)->n_ports = EHCI_INST(dev->controller)->capabilities->hcsparams & HCS_NPORTS_MASK;
183 RH_INST(dev)->ports = EHCI_INST(dev->controller)->operation->portsc;
184 RH_INST(dev)->devices = xmalloc(RH_INST(dev)->n_ports * sizeof(int));
186 usb_debug("root hub has %x ports\n", RH_INST(dev)->n_ports);
188 /* If the host controller has port power control, enable power on
189 * all ports and wait 20ms.
191 if (EHCI_INST(dev->controller)->capabilities->hcsparams
192 & HCS_PORT_POWER_CONTROL) {
193 usb_debug("host controller has port power control, "
194 "giving power to all ports.\n");
195 for (i = 0; i < RH_INST(dev)->n_ports; i++)
196 RH_INST(dev)->ports[i] |= P_PP;
198 mdelay(20); // ehci spec 2.3.9
200 dev->speed = HIGH_SPEED;
201 dev->address = 0;
202 dev->hub = -1;
203 dev->port = -1;
204 for (i = 0; i < RH_INST(dev)->n_ports; i++) {
205 RH_INST(dev)->devices[i] = -1;
206 ehci_rh_scanport(dev, i);