libpayload: Drop usb_fatal()
[coreboot.git] / payloads / libpayload / drivers / usb / ohci_rh.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2010 Patrick Georgi
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 //#define USB_DEBUG
31
32 #include <libpayload.h>
33 #include "ohci_private.h"
34 #include "ohci.h"
35
36 typedef struct {
37         int numports;
38         int *port;
39 } rh_inst_t;
40
41 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
42
43 static void
44 ohci_rh_enable_port (usbdev_t *dev, int port)
45 {
46         if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & CurrentConnectStatus))
47                 return;
48
49         OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] = SetPortEnable; // enable port
50         mdelay(10);
51         while (!(OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] & PortEnableStatus)) mdelay(1);
52         OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] = SetPortReset; // reset port
53         while (OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] & PortResetStatus) mdelay(1);
54         OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] = PortResetStatusChange;
55 }
56
57 /* disable root hub */
58 static void
59 ohci_rh_disable_port (usbdev_t *dev, int port)
60 {
61         OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] = ClearPortEnable; // disable port
62         while (OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] & PortEnableStatus) mdelay(1);
63 }
64
65 static void
66 ohci_rh_scanport (usbdev_t *dev, int port)
67 {
68         if (port >= RH_INST(dev)->numports) {
69                 debug("Invalid port %d\n", port);
70                 return;
71         }
72
73         /* device registered, and device change logged, so something must have happened */
74         if (RH_INST (dev)->port[port] != -1) {
75                 usb_detach_device(dev->controller, RH_INST (dev)->port[port]);
76                 RH_INST (dev)->port[port] = -1;
77         }
78
79         /* no device attached
80            previously registered devices are detached, nothing left to do */
81         if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & CurrentConnectStatus))
82                 return;
83
84         OHCI_INST (dev->controller)->opreg->HcRhPortStatus[port] = ConnectStatusChange; // clear port state change
85         ohci_rh_enable_port (dev, port);
86
87         mdelay(100); // wait for signal to stabilize
88
89         if (!(OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & PortEnableStatus)) {
90                 debug ("port enable failed\n");
91                 return;
92         }
93
94         int speed = (OHCI_INST(dev->controller)->opreg->HcRhPortStatus[port] & LowSpeedDeviceAttached) != 0;
95         RH_INST (dev)->port[port] = usb_attach_device(dev->controller, dev->address, port, speed);
96 }
97
98 static int
99 ohci_rh_report_port_changes (usbdev_t *dev)
100 {
101         int i;
102
103         if (!(OHCI_INST (dev->controller)->opreg->HcInterruptStatus & RootHubStatusChange)) return -1;
104         OHCI_INST (dev->controller)->opreg->HcInterruptStatus = RootHubStatusChange;
105         debug("port change\n");
106
107         for (i = 0; i < RH_INST(dev)->numports; i++) {
108                 // maybe detach+attach happened between two scans?
109                 if (OHCI_INST (dev->controller)->opreg->HcRhPortStatus[i] & ConnectStatusChange) {
110                         debug("attachment change on port %d\n", i);
111                         return i;
112                 }
113         }
114
115         // no change
116         return -1;
117 }
118
119 static void
120 ohci_rh_destroy (usbdev_t *dev)
121 {
122         int i;
123         for (i = 0; i < RH_INST (dev)->numports; i++)
124                 ohci_rh_disable_port (dev, i);
125         free (RH_INST (dev));
126 }
127
128 static void
129 ohci_rh_poll (usbdev_t *dev)
130 {
131         int port;
132         while ((port = ohci_rh_report_port_changes (dev)) != -1)
133                 ohci_rh_scanport (dev, port);
134 }
135
136 void
137 ohci_rh_init (usbdev_t *dev)
138 {
139         int i;
140
141         dev->destroy = ohci_rh_destroy;
142         dev->poll = ohci_rh_poll;
143
144         dev->data = malloc (sizeof (rh_inst_t));
145         if (!dev->data)
146                 fatal("Not enough memory for OHCI RH.\n");
147
148         RH_INST (dev)->numports = OHCI_INST (dev->controller)->opreg->HcRhDescriptorA & NumberDownstreamPortsMask;
149         RH_INST (dev)->port = malloc(sizeof(int) * RH_INST (dev)->numports);
150         debug("%d ports registered\n", RH_INST (dev)->numports);
151
152         for (i = 0; i < RH_INST (dev)->numports; i++) {
153                 ohci_rh_enable_port (dev, i);
154                 RH_INST (dev)->port[i] = -1;
155         }
156
157         /* we can set them here because a root hub _really_ shouldn't
158            appear elsewhere */
159         dev->address = 0;
160         dev->hub = -1;
161         dev->port = -1;
162
163         debug("rh init done\n");
164 }