2 * This file is part of the libpayload project.
4 * Copyright (C) 2008-2010 coresystems GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
32 #include <libpayload.h>
34 #include "uhci_private.h"
40 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
43 uhci_rh_enable_port (usbdev_t *dev, int port)
46 hci_t *controller = dev->controller;
52 printf("Invalid port %d\n", port);
56 uhci_reg_mask16 (controller, port, ~(1 << 12), 0); /* wakeup */
58 uhci_reg_mask16 (controller, port, ~0, 1 << 9); /* reset */
60 uhci_reg_mask16 (controller, port, ~(1 << 9), 0);
61 mdelay (1); // >5.3us per spec, <3ms because some devices make trouble
63 uhci_reg_mask16 (controller, port, ~0, 1 << 2); /* enable */
65 value = uhci_reg_read16 (controller, port);
67 } while (((value & (1 << 2)) == 0) && (value & 0x01));
70 /* disable root hub */
72 uhci_rh_disable_port (usbdev_t *dev, int port)
74 hci_t *controller = dev->controller;
78 uhci_reg_mask16 (controller, port, ~4, 0);
81 value = uhci_reg_read16 (controller, port);
83 } while ((value & (1 << 2)) != 0);
87 uhci_rh_scanport (usbdev_t *dev, int port)
93 } else if (port == 2) {
97 printf("Invalid port %d\n", port);
100 int devno = RH_INST (dev)->port[offset];
101 if ((dev->controller->devices[devno] != 0) && (devno != -1)) {
102 usb_detach_device(dev->controller, devno);
103 RH_INST (dev)->port[offset] = -1;
105 uhci_reg_mask16 (dev->controller, portsc, ~0, (1 << 3) | (1 << 2)); // clear port state change, enable port
107 mdelay(100); // wait for signal to stabilize
109 if ((uhci_reg_read16 (dev->controller, portsc) & 1) != 0) {
112 uhci_rh_disable_port (dev, port);
113 uhci_rh_enable_port (dev, port);
115 int speed = ((uhci_reg_read16 (dev->controller, portsc) >> 8) & 1);
117 RH_INST (dev)->port[offset] = usb_attach_device(dev->controller, dev->address, portsc, speed);
122 uhci_rh_report_port_changes (usbdev_t *dev)
126 stored = (RH_INST (dev)->port[0] == -1);
127 real = ((uhci_reg_read16 (dev->controller, PORTSC1) & 1) == 0);
128 if (stored != real) {
129 debug("change on port 1\n");
133 stored = (RH_INST (dev)->port[1] == -1);
134 real = ((uhci_reg_read16 (dev->controller, PORTSC2) & 1) == 0);
135 if (stored != real) {
136 debug("change on port 2\n");
140 // maybe detach+attach happened between two scans?
142 if ((uhci_reg_read16 (dev->controller, PORTSC1) & 2) > 0) {
143 debug("possibly re-attached on port 1\n");
146 if ((uhci_reg_read16 (dev->controller, PORTSC2) & 2) > 0) {
147 debug("possibly re-attached on port 2\n");
156 uhci_rh_destroy (usbdev_t *dev)
158 uhci_rh_disable_port (dev, 1);
159 uhci_rh_disable_port (dev, 2);
160 free (RH_INST (dev));
164 uhci_rh_poll (usbdev_t *dev)
167 while ((port = uhci_rh_report_port_changes (dev)) != -1)
168 uhci_rh_scanport (dev, port);
172 uhci_rh_init (usbdev_t *dev)
174 dev->destroy = uhci_rh_destroy;
175 dev->poll = uhci_rh_poll;
177 uhci_rh_enable_port (dev, 1);
178 uhci_rh_enable_port (dev, 2);
179 dev->data = malloc (sizeof (rh_inst_t));
181 usb_fatal ("Not enough memory for UHCI RH.\n");
183 RH_INST (dev)->port[0] = -1;
184 RH_INST (dev)->port[1] = -1;
186 /* we can set them here because a root hub _really_ shouldn't