1 /*
2 * Driver for USB OHCI Root Hubs ported from CoreBoot
3 *
4 * Copyright (C) 2014 BALATON Zoltan
5 *
6 * This file was part of the libpayload project.
7 *
8 * Copyright (C) 2010 Patrick Georgi
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "config.h"
35 #include "timer.h"
36 #include "usbohci_private.h"
37 #include "usbohci.h"
38
39 typedef struct {
40 int numports;
41 int *port;
42 } rh_inst_t;
43
44 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
45
46 static void
ohci_rh_enable_port(usbdev_t * dev,int port)47 ohci_rh_enable_port (usbdev_t *dev, int port)
48 {
49 /* Reset RH port should hold 50ms with pulses of at least 10ms and
50 * gaps of at most 3ms (usb20 spec 7.1.7.5).
51 * After reset, the port will be enabled automatically (ohci spec
52 * 7.4.4).
53 */
54 int total_delay = 100; /* 100 * 500us == 50ms */
55 while (total_delay > 0) {
56 if (!(READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port])
57 & CurrentConnectStatus))
58 return;
59
60 /* start reset */
61 OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] =
62 __cpu_to_le32(SetPortReset);
63 int timeout = 200; /* timeout after 200 * 500us == 100ms */
64 while ((READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port])
65 & PortResetStatus)
66 && timeout--) {
67 udelay(500); total_delay--;
68 }
69 if (READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port])
70 & PortResetStatus) {
71 usb_debug("Warning: root-hub port reset timed out.\n");
72 break;
73 }
74 if ((200-timeout) < 20) {
75 usb_debug("Warning: port reset too short: %dms; "
76 "should be at least 10ms.\n",
77 (200-timeout)/2);
78 total_delay = 0; /* can happen on QEMU */
79 }
80 /* clear reset status change */
81 OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] =
82 __cpu_to_le32(PortResetStatusChange);
83 usb_debug ("rh port reset finished after %dms.\n", (200-timeout)/2);
84 }
85 }
86
87 /* disable root hub */
88 static void
ohci_rh_disable_port(usbdev_t * dev,int port)89 ohci_rh_disable_port (usbdev_t *dev, int port)
90 {
91 OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] =
92 __cpu_to_le32(ClearPortEnable); // disable port
93 int timeout = 50; /* timeout after 50 * 100us == 5ms */
94 while ((READ_OPREG(OHCI_INST (dev->controller), HcRhPortStatus[port])
95 & PortEnableStatus)
96 && timeout--) {
97 udelay(100);
98 }
99 }
100
101 static void
ohci_rh_scanport(usbdev_t * dev,int port)102 ohci_rh_scanport (usbdev_t *dev, int port)
103 {
104 if (port >= RH_INST(dev)->numports) {
105 usb_debug("Invalid port %d\n", port);
106 return;
107 }
108
109 /* device registered, and device change logged, so something must have happened */
110 if (RH_INST (dev)->port[port] != -1) {
111 usb_detach_device(dev->controller, RH_INST (dev)->port[port]);
112 RH_INST (dev)->port[port] = -1;
113 }
114
115 /* no device attached
116 previously registered devices are detached, nothing left to do */
117 if (!(READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port]) & CurrentConnectStatus))
118 return;
119
120 // clear port state change
121 OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = __cpu_to_le32(ConnectStatusChange);
122 ohci_rh_enable_port (dev, port);
123
124 mdelay(100); // wait for signal to stabilize
125
126 if (!(READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port]) & PortEnableStatus)) {
127 usb_debug ("port enable failed\n");
128 return;
129 }
130
131 int speed = (READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port]) & LowSpeedDeviceAttached) != 0;
132 RH_INST (dev)->port[port] = usb_attach_device(dev->controller, dev->address, port, speed);
133 }
134
135 static int
ohci_rh_report_port_changes(usbdev_t * dev)136 ohci_rh_report_port_changes (usbdev_t *dev)
137 {
138 ohci_t *const ohcic = OHCI_INST (dev->controller);
139
140 int i;
141
142 for (i = 0; i < RH_INST(dev)->numports; i++) {
143 // maybe detach+attach happened between two scans?
144 if (READ_OPREG(ohcic, HcRhPortStatus[i]) & ConnectStatusChange) {
145 ohcic->opreg->HcRhPortStatus[i] = __cpu_to_le32(ConnectStatusChange);
146 usb_debug("attachment change on port %d\n", i);
147 return i;
148 }
149 }
150
151 // no change
152 return -1;
153 }
154
155 static void
ohci_rh_destroy(usbdev_t * dev)156 ohci_rh_destroy (usbdev_t *dev)
157 {
158 int i;
159 for (i = 0; i < RH_INST (dev)->numports; i++)
160 ohci_rh_disable_port (dev, i);
161 free (RH_INST (dev));
162 }
163
164 static void
ohci_rh_poll(usbdev_t * dev)165 ohci_rh_poll (usbdev_t *dev)
166 {
167 ohci_t *const ohcic = OHCI_INST (dev->controller);
168
169 int port;
170
171 /* Check if anything changed. */
172 if (!(READ_OPREG(ohcic, HcInterruptStatus) & RootHubStatusChange))
173 return;
174 ohcic->opreg->HcInterruptStatus = __cpu_to_le32(RootHubStatusChange);
175 usb_debug("root hub status change\n");
176
177 /* Scan ports with changed connection status. */
178 while ((port = ohci_rh_report_port_changes (dev)) != -1)
179 ohci_rh_scanport (dev, port);
180 }
181
182 void
ohci_rh_init(usbdev_t * dev)183 ohci_rh_init (usbdev_t *dev)
184 {
185 int i;
186
187 dev->destroy = ohci_rh_destroy;
188 dev->poll = ohci_rh_poll;
189
190 dev->data = malloc (sizeof (rh_inst_t));
191 if (!dev->data) {
192 printk("Not enough memory for OHCI RH.\n");
193 return;
194 }
195
196 RH_INST (dev)->numports = READ_OPREG(OHCI_INST(dev->controller), HcRhDescriptorA) & NumberDownstreamPortsMask;
197 RH_INST (dev)->port = malloc(sizeof(int) * RH_INST (dev)->numports);
198 usb_debug("%d ports registered\n", RH_INST (dev)->numports);
199
200 for (i = 0; i < RH_INST (dev)->numports; i++) {
201 ohci_rh_enable_port (dev, i);
202 RH_INST (dev)->port[i] = -1;
203 }
204
205 /* we can set them here because a root hub _really_ shouldn't
206 appear elsewhere */
207 dev->address = 0;
208 dev->hub = -1;
209 dev->port = -1;
210
211 usb_debug("rh init done\n");
212 }
213