Debian Codegraph @

payloads/libpayload/drivers/usb/uhci_rh.c

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2013 secunet Security Networks AG
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 //#define USB_DEBUG
30
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <usb/usb.h>
34
35 #include "generic_hub.h"
36 #include "uhci_private.h"
37 #include "uhci.h"
38
39 #define PORTSC(p) (PORTSC1 + ((p) - 1) * sizeof(uint16_t))
40 #define PORTSC_CCS (1 << 0)
41 #define PORTSC_CSC (1 << 1)
42 #define PORTSC_PED (1 << 2)
43 #define PORTSC_PEDC (1 << 3)
44 #define PORTSC_LSDA (1 << 8)
45 #define PORTSC_PR (1 << 9)
46 #define PORTSC_SUSP (1 << 12)
47 #define PORTSC_SC_BITS (PORTSC_CSC | PORTSC_PEDC)
48
49 static int
50 uhci_rh_port_status_changed(usbdev_t *const dev, const int port)
51 {
52 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port));
53 const bool changed = portsc & (PORTSC_CSC | PORTSC_PEDC);
54
55 /* always clear all the status change bits */
56 uhci_reg_write16(dev->controller, PORTSC(port), portsc);
57
58 return changed;
59 }
60
61 static int
62 uhci_rh_port_connected(usbdev_t *const dev, const int port)
63 {
64 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port));
65 return !!(portsc & PORTSC_CCS);
66 }
67
68 static int
69 uhci_rh_port_enabled(usbdev_t *const dev, const int port)
70 {
71 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port));
72 return !(portsc & PORTSC_SUSP) && (portsc & PORTSC_PED);
73 }
74
75 static usb_speed
76 uhci_rh_port_speed(usbdev_t *const dev, const int port)
77 {
78 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port));
79 const bool low_speed = portsc & PORTSC_LSDA;
80
81 return low_speed ? LOW_SPEED : FULL_SPEED;
82 }
83
84 static int
85 uhci_rh_enable_port(usbdev_t *const dev, const int port)
86 {
87 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)) & ~PORTSC_SUSP;
88 uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~(PORTSC_SC_BITS | PORTSC_SUSP));
89 uhci_reg_write16(dev->controller, PORTSC(port), (portsc & ~PORTSC_SC_BITS) | PORTSC_PED);
90 return 0;
91 }
92
93 static int
94 uhci_rh_disable_port(usbdev_t *const dev, const int port)
95 {
96 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port));
97 uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~(PORTSC_SC_BITS | PORTSC_PED));
98 return 0;
99 }
100
101 static int
102 uhci_rh_reset_port(usbdev_t *const dev, const int port)
103 {
104 const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)) & ~PORTSC_PR;
105
106 /* Trigger port reset. */
107 uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PR);
108
109 /* Wait for 15ms (usb20 spec 11.5.1.5: reset should take 10 to 20ms). */
110 mdelay(15);
111
112 /* Clear port reset. */
113 uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~PORTSC_PR);
114
115 udelay(10); /* Linux waits this long. */
116
117 /* Enable port. */
118 uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PED);
119
120 /* Clear status-change bits one last time. */
121 uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PED | PORTSC_SC_BITS);
122
123 return 0;
124 }
125
126 static const generic_hub_ops_t uhci_rh_ops = {
127 .hub_status_changed = NULL,
128 .port_status_changed = uhci_rh_port_status_changed,
129 .port_connected = uhci_rh_port_connected,
130 .port_in_reset = NULL,
131 .port_enabled = uhci_rh_port_enabled,
132 .port_speed = uhci_rh_port_speed,
133 .enable_port = uhci_rh_enable_port,
134 .disable_port = uhci_rh_disable_port,
135 .start_port_reset = NULL,
136 .reset_port = uhci_rh_reset_port,
137 };
138
139 void
140 uhci_rh_init(usbdev_t *dev)
141 {
142 /* we can set them here because a root hub _really_ shouldn't appear elsewhere */
143 dev->address = 0;
144 dev->hub = -1;
145 dev->port = -1;
146
147 generic_hub_init(dev, 2, &uhci_rh_ops);
148
149 usb_debug("UHCI: root hub init done\n");
150 }