2 * This file is part of the libpayload project.
4 * Copyright (C) 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
30 #include <libpayload.h>
32 #include "ehci_private.h"
36 /* typical C, n_ports is the number
37 * of ports, while ports[] spans [0,n_ports-1],
38 * even though the spec counts from 1.
40 volatile portsc_t *ports;
44 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
47 ehci_rh_destroy (usbdev_t *dev)
49 free (RH_INST(dev)->devices);
54 ehci_rh_hand_over_port (usbdev_t *dev, int port)
56 debug("giving up port %x, it's USB1\n", port+1);
58 /* Lowspeed device. Hand over to companion */
59 RH_INST(dev)->ports[port] |= P_PORT_OWNER;
60 do {} while (!(RH_INST(dev)->ports[port] & P_CONN_STATUS_CHANGE));
61 /* RW/C register, so clear it by writing 1 */
62 RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
67 ehci_rh_scanport (usbdev_t *dev, int port)
69 if (RH_INST(dev)->devices[port]!=-1) {
70 debug("Unregister device at port %x\n", port+1);
71 usb_detach_device(dev->controller, RH_INST(dev)->devices[port]);
72 RH_INST(dev)->devices[port]=-1;
74 /* device connected, handle */
75 if (RH_INST(dev)->ports[port] & P_CURR_CONN_STATUS) {
77 if ((RH_INST(dev)->ports[port] & P_LINE_STATUS) == P_LINE_STATUS_LOWSPEED) {
78 ehci_rh_hand_over_port(dev, port);
82 /* Deassert enable, assert reset. These must change
85 RH_INST(dev)->ports[port] = (RH_INST(dev)->ports[port] & ~P_PORT_ENABLE) | P_PORT_RESET;
87 /* Wait a bit while reset is active. */
91 RH_INST(dev)->ports[port] &= ~P_PORT_RESET;
93 /* Wait for flag change to finish. The controller might take a while */
94 while (RH_INST(dev)->ports[port] & P_PORT_RESET) ;
95 if (!(RH_INST(dev)->ports[port] & P_PORT_ENABLE)) {
96 ehci_rh_hand_over_port(dev, port);
99 debug("port %x hosts a USB2 device\n", port+1);
100 RH_INST(dev)->devices[port] = usb_attach_device(dev->controller, dev->address, port, 2);
102 /* RW/C register, so clear it by writing 1 */
103 RH_INST(dev)->ports[port] |= P_CONN_STATUS_CHANGE;
107 ehci_rh_report_port_changes (usbdev_t *dev)
110 for (i=0; i<RH_INST(dev)->n_ports; i++) {
111 if (RH_INST(dev)->ports[i] & P_CONN_STATUS_CHANGE)
118 ehci_rh_poll (usbdev_t *dev)
121 while ((port = ehci_rh_report_port_changes (dev)) != -1)
122 ehci_rh_scanport (dev, port);
127 ehci_rh_init (usbdev_t *dev)
130 dev->destroy = ehci_rh_destroy;
131 dev->poll = ehci_rh_poll;
133 dev->data = malloc(sizeof(rh_inst_t));
135 RH_INST(dev)->n_ports = EHCI_INST(dev->controller)->capabilities->hcsparams & HCS_NPORTS_MASK;
136 RH_INST(dev)->ports = EHCI_INST(dev->controller)->operation->portsc;
138 debug("root hub has %x ports\n", RH_INST(dev)->n_ports);
140 RH_INST(dev)->devices = malloc(RH_INST(dev)->n_ports * sizeof(int));
141 for (i=0; i < RH_INST(dev)->n_ports; i++) {
142 RH_INST(dev)->devices[i] = -1;
143 RH_INST(dev)->ports[i] |= P_PP;
144 ehci_rh_scanport(dev, i);