2 * Driver for USB OHCI Root Hubs ported from CoreBoot
4 * Copyright (C) 2014 BALATON Zoltan
6 * This file was part of the libpayload project.
8 * Copyright (C) 2010 Patrick Georgi
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 #include "usbohci_private.h"
44 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
47 ohci_rh_enable_port (usbdev_t *dev, int port)
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
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))
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])
67 udelay(500); total_delay--;
69 if (READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port])
71 usb_debug("Warning: root-hub port reset timed out.\n");
74 if ((200-timeout) < 20) {
75 usb_debug("Warning: port reset too short: %dms; "
76 "should be at least 10ms.\n",
78 total_delay = 0; /* can happen on QEMU */
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);
87 /* disable root hub */
89 ohci_rh_disable_port (usbdev_t *dev, int port)
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])
102 ohci_rh_scanport (usbdev_t *dev, int port)
104 if (port >= RH_INST(dev)->numports) {
105 usb_debug("Invalid port %d\n", port);
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;
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))
120 // clear port state change
121 OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] = __cpu_to_le32(ConnectStatusChange);
122 ohci_rh_enable_port (dev, port);
124 mdelay(100); // wait for signal to stabilize
126 if (!(READ_OPREG(OHCI_INST(dev->controller), HcRhPortStatus[port]) & PortEnableStatus)) {
127 usb_debug ("port enable failed\n");
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);
136 ohci_rh_report_port_changes (usbdev_t *dev)
138 ohci_t *const ohcic = OHCI_INST (dev->controller);
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);
156 ohci_rh_destroy (usbdev_t *dev)
159 for (i = 0; i < RH_INST (dev)->numports; i++)
160 ohci_rh_disable_port (dev, i);
161 free (RH_INST (dev));
165 ohci_rh_poll (usbdev_t *dev)
167 ohci_t *const ohcic = OHCI_INST (dev->controller);
171 /* Check if anything changed. */
172 if (!(READ_OPREG(ohcic, HcInterruptStatus) & RootHubStatusChange))
174 ohcic->opreg->HcInterruptStatus = __cpu_to_le32(RootHubStatusChange);
175 usb_debug("root hub status change\n");
177 /* Scan ports with changed connection status. */
178 while ((port = ohci_rh_report_port_changes (dev)) != -1)
179 ohci_rh_scanport (dev, port);
183 ohci_rh_init (usbdev_t *dev)
187 dev->destroy = ohci_rh_destroy;
188 dev->poll = ohci_rh_poll;
190 dev->data = malloc (sizeof (rh_inst_t));
192 printk("Not enough memory for OHCI RH.\n");
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);
200 for (i = 0; i < RH_INST (dev)->numports; i++) {
201 ohci_rh_enable_port (dev, i);
202 RH_INST (dev)->port[i] = -1;
205 /* we can set them here because a root hub _really_ shouldn't
211 usb_debug("rh init done\n");