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