blob: d3b496375ddfd198c59e73c5f40ead21acfbf537 [file] [log] [blame]
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
16 */
17
18#include <stddef.h>
19#include <console/console.h>
20
21#include "ehci_debug.h"
22#include "usb_ch9.h"
23#include "ehci.h"
24
25
26#define USB_HUB_PORT_CONNECTION 0
27#define USB_HUB_PORT_ENABLED 1
28#define USB_HUB_PORT_RESET 4
29#define USB_HUB_PORT_POWER 8
30#define USB_HUB_C_PORT_CONNECTION 16
31#define USB_HUB_C_PORT_RESET 20
32
33
34static int hub_port_status(const char * buf, int feature)
35{
36 return !!(buf[feature>>3] & (1<<(feature&0x7)));
37}
38
39/* After USB port reset, treat device number 0 as an USB hub. Assign it with
40 * a device number hub_addr. Then apply enable and reset on downstream port.
41 */
42 static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned char hub_addr,
43 unsigned char port)
44{
45 char status[8];
46 int ret, loop;
47
48 /* Assign a devicenumber for the hub. */
49 ret = dbgp_control_msg(ehci_debug, 0,
50 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
51 USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0);
52 if (ret < 0)
53 goto err;
54
55 /* Enter configured state on hub. */
56 ret = dbgp_control_msg(ehci_debug, hub_addr,
57 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
58 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
59 if (ret < 0)
60 goto err;
61
62 /* Set PORT_POWER, poll for PORT_CONNECTION. */
63 ret = dbgp_control_msg(ehci_debug, hub_addr,
64 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
65 USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0);
66 if (ret < 0)
67 goto err;
68
69 loop = 100;
70 do {
71 dbgp_mdelay(10);
72 ret = dbgp_control_msg(ehci_debug, hub_addr,
73 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
74 USB_REQ_GET_STATUS, 0, port, status, 4);
75 if (ret < 0)
76 goto err;
77 if (hub_port_status(status, USB_HUB_PORT_CONNECTION))
78 break;
79 } while (--loop);
80 if (! loop)
81 goto err;
82
83 ret = dbgp_control_msg(ehci_debug, hub_addr,
84 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
85 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0);
86 if (ret < 0)
87 goto err;
88
89
90 /* Set PORT_RESET, poll for C_PORT_RESET. */
91 ret = dbgp_control_msg(ehci_debug, hub_addr,
92 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
93 USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0);
94 if (ret < 0)
95 goto err;
96
97 loop = 100;
98 do {
99 dbgp_mdelay(10);
100 ret = dbgp_control_msg(ehci_debug, hub_addr,
101 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
102 USB_REQ_GET_STATUS, 0, port, status, 4);
103 if (ret < 0)
104 goto err;
105 if (hub_port_status(status, USB_HUB_C_PORT_RESET))
106 break;
107 } while (--loop);
108 if (! loop)
109 goto err;
110
111 ret = dbgp_control_msg(ehci_debug, hub_addr,
112 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
113 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0);
114 if (ret < 0)
115 goto err;
116
117 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
118 return 0;
119err:
120 return -1;
121}
122
123
124int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
125{
126 int ret;
127
128 if (CONFIG_USBDEBUG_OPTIONAL_HUB_PORT != 0) {
129 ret = dbgp_hub_enable(ehci_debug, USB_DEBUG_DEVNUM-1,
130 CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
131 if (ret < 0) {
132 printk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
133 return ret;
134 }
135 }
136
137 return 0;
138}