USB updates from our internal tree
[coreboot.git] / payloads / libpayload / drivers / usb / uhci_rh.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008-2010 coresystems GmbH
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 "uhci.h"
34
35 typedef struct {
36         int port[2];
37 } rh_inst_t;
38
39 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
40
41 static void
42 uhci_rh_enable_port (usbdev_t *dev, int port)
43 {
44         u16 value;
45         hci_t *controller = dev->controller;
46         if (port == 1)
47                 port = PORTSC1;
48         else if (port == 2)
49                 port = PORTSC2;
50         else {
51                 printf("Invalid port %d\n", port);
52                 return;
53         }
54
55         uhci_reg_mask16 (controller, port, ~(1 << 12), 0);      /* wakeup */
56
57         uhci_reg_mask16 (controller, port, ~0, 1 << 9); /* reset */
58         mdelay (30);            // >10ms
59         uhci_reg_mask16 (controller, port, ~(1 << 9), 0);
60         mdelay (1);             // >5.3us per spec, <3ms because some devices make trouble
61
62         uhci_reg_mask16 (controller, port, ~0, 1 << 2); /* enable */
63         do {
64                 value = uhci_reg_read16 (controller, port);
65                 mdelay (1);
66         } while (((value & (1 << 2)) == 0) && (value & 0x01));
67 }
68
69 /* disable root hub */
70 static void
71 uhci_rh_disable_port (usbdev_t *dev, int port)
72 {
73         hci_t *controller = dev->controller;
74         port = PORTSC2;
75         if (port == 1)
76                 port = PORTSC1;
77         uhci_reg_mask16 (controller, port, ~4, 0);
78         int value;
79         do {
80                 value = uhci_reg_read16 (controller, port);
81                 mdelay (1);
82         } while ((value & (1 << 2)) != 0);
83 }
84
85 static void
86 uhci_rh_scanport (usbdev_t *dev, int port)
87 {
88         int portsc, offset;
89         if (port == 1) {
90                 portsc = PORTSC1;
91                 offset = 0;
92         } else if (port == 2) {
93                 portsc = PORTSC2;
94                 offset = 1;
95         } else {
96                 printf("Invalid port %d\n", port);
97                 return;
98         }
99         int devno = RH_INST (dev)->port[offset];
100         if ((dev->controller->devices[devno] != 0) && (devno != -1)) {
101                 usb_detach_device(dev->controller, devno);
102                 RH_INST (dev)->port[offset] = -1;
103         }
104         uhci_reg_mask16 (dev->controller, portsc, ~0, (1 << 3) | (1 << 2));     // clear port state change, enable port
105
106         mdelay(100); // wait for signal to stabilize
107
108         if ((uhci_reg_read16 (dev->controller, portsc) & 1) != 0) {
109                 // device attached
110
111                 uhci_rh_disable_port (dev, port);
112                 uhci_rh_enable_port (dev, port);
113
114                 int speed = ((uhci_reg_read16 (dev->controller, portsc) >> 8) & 1);
115
116                 RH_INST (dev)->port[offset] = usb_attach_device(dev->controller, dev->address, portsc, speed);
117         }
118 }
119
120 static int
121 uhci_rh_report_port_changes (usbdev_t *dev)
122 {
123         int stored, real;
124
125         stored = (RH_INST (dev)->port[0] == -1);
126         real = ((uhci_reg_read16 (dev->controller, PORTSC1) & 1) == 0);
127         if (stored != real) {
128                 debug("change on port 1\n");
129                 return 1;
130         }
131
132         stored = (RH_INST (dev)->port[1] == -1);
133         real = ((uhci_reg_read16 (dev->controller, PORTSC2) & 1) == 0);
134         if (stored != real) {
135                 debug("change on port 2\n");
136                 return 2;
137         }
138
139         // maybe detach+attach happened between two scans?
140
141         if ((uhci_reg_read16 (dev->controller, PORTSC1) & 2) > 0) {
142                 debug("possibly re-attached on port 1\n");
143                 return 1;
144         }
145         if ((uhci_reg_read16 (dev->controller, PORTSC2) & 2) > 0) {
146                 debug("possibly re-attached on port 2\n");
147                 return 2;
148         }
149
150         // no change
151         return -1;
152 }
153
154 static void
155 uhci_rh_destroy (usbdev_t *dev)
156 {
157         uhci_rh_disable_port (dev, 1);
158         uhci_rh_disable_port (dev, 2);
159         free (RH_INST (dev));
160 }
161
162 static void
163 uhci_rh_poll (usbdev_t *dev)
164 {
165         int port;
166         while ((port = uhci_rh_report_port_changes (dev)) != -1)
167                 uhci_rh_scanport (dev, port);
168 }
169
170 void
171 uhci_rh_init (usbdev_t *dev)
172 {
173         dev->destroy = uhci_rh_destroy;
174         dev->poll = uhci_rh_poll;
175
176         uhci_rh_enable_port (dev, 1);
177         uhci_rh_enable_port (dev, 2);
178         dev->data = malloc (sizeof (rh_inst_t));
179         if (!dev->data)
180                 usb_fatal ("Not enough memory for UHCI RH.\n");
181
182         RH_INST (dev)->port[0] = -1;
183         RH_INST (dev)->port[1] = -1;
184
185         /* we can set them here because a root hub _really_ shouldn't
186            appear elsewhere */
187         dev->address = 0;
188         dev->hub = -1;
189         dev->port = -1;
190 }