libpayload: Reduce verbosity in USB stack
[coreboot.git] / payloads / libpayload / drivers / usb / ehci_rh.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 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 #include <libpayload.h>
31 #include "ehci.h"
32 #include "ehci_private.h"
33
34 typedef struct {
35         int n_ports;
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.
39          */
40         volatile portsc_t *ports;
41         int *devices;
42 } rh_inst_t;
43
44 #define RH_INST(dev) ((rh_inst_t*)(dev)->data)
45
46 static void
47 ehci_rh_destroy (usbdev_t *dev)
48 {
49         free (RH_INST(dev)->devices);
50         free (RH_INST(dev));
51 }
52
53 static void
54 ehci_rh_hand_over_port (usbdev_t *dev, int port)
55 {
56         volatile portsc_t *p = &(RH_INST(dev)->ports[port]);
57         volatile portsc_t tmp;
58
59         debug("giving up port %x, it's USB1\n", port+1);
60
61         /* Lowspeed device. Hand over to companion */
62         tmp = *p;
63         tmp.port_owner = 1;
64         *p = tmp;
65         do {} while (!p->conn_status_change);
66         /* RW/C register, so clear it by writing 1 */
67         tmp = *p;
68         tmp.conn_status_change = 1;
69         *p = tmp;
70         return;
71 }
72
73 static void
74 ehci_rh_scanport (usbdev_t *dev, int port)
75 {
76         volatile portsc_t *p = &(RH_INST(dev)->ports[port]);
77         volatile portsc_t tmp;
78         if (RH_INST(dev)->devices[port]!=-1) {
79                 debug("Unregister device at port %x\n", port+1);
80                 usb_detach_device(dev->controller, RH_INST(dev)->devices[port]);
81                 RH_INST(dev)->devices[port]=-1;
82         }
83         /* device connected, handle */
84         if (p->current_conn_status) {
85                 mdelay(100);
86                 if (p->line_status == 0x1) {
87                         ehci_rh_hand_over_port(dev, port);
88                         return;
89                 }
90
91                 /* Deassert enable, assert reset.  These must change
92                  * atomically.
93                  */
94                 tmp = *p;
95                 tmp.port_enable = 0;
96                 tmp.port_reset = 1;
97                 *p = tmp;
98
99                 /* Wait a bit while reset is active. */
100                 mdelay(50);
101
102                 /* Deassert reset. */
103                 tmp.port_reset = 0;
104                 *p = tmp;
105
106                 /* Wait for flag change to finish. The controller might take a while */
107                 while (p->port_reset) ;
108                 if (!p->port_enable) {
109                         ehci_rh_hand_over_port(dev, port);
110                         return;
111                 }
112                 debug("port %x hosts a USB2 device\n", port+1);
113                 RH_INST(dev)->devices[port] = usb_attach_device(dev->controller, dev->address, port, 2);
114         }
115         /* RW/C register, so clear it by writing 1 */
116         tmp = *p;
117         tmp.conn_status_change = 1;
118         *p = tmp;
119 }
120
121 static int
122 ehci_rh_report_port_changes (usbdev_t *dev)
123 {
124         int i;
125         for (i=0; i<RH_INST(dev)->n_ports; i++) {
126                 if (RH_INST(dev)->ports[i].conn_status_change)
127                         return i;
128         }
129         return -1;
130 }
131
132 static void
133 ehci_rh_poll (usbdev_t *dev)
134 {
135         int port;
136         while ((port = ehci_rh_report_port_changes (dev)) != -1)
137                 ehci_rh_scanport (dev, port);
138 }
139
140
141 void
142 ehci_rh_init (usbdev_t *dev)
143 {
144         int i;
145         volatile portsc_t *p;
146         volatile portsc_t tmp;
147
148         dev->destroy = ehci_rh_destroy;
149         dev->poll = ehci_rh_poll;
150
151         dev->data = malloc(sizeof(rh_inst_t));
152
153         RH_INST(dev)->n_ports = EHCI_INST(dev->controller)->capabilities->n_ports;
154         RH_INST(dev)->ports = EHCI_INST(dev->controller)->operation->portsc;
155
156         debug("root hub has %x ports\n", RH_INST(dev)->n_ports);
157
158         RH_INST(dev)->devices = malloc(RH_INST(dev)->n_ports * sizeof(int));
159         for (i=0; i < RH_INST(dev)->n_ports; i++) {
160                 p = &(RH_INST(dev)->ports[i]);
161                 RH_INST(dev)->devices[i] = -1;
162                 tmp = *p;
163                 tmp.pp = 1;
164                 *p = tmp;
165         }
166
167         dev->address = 0;
168         dev->hub = -1;
169         dev->port = -1;
170 }