blob: 7f881494731b824532f3a5e7a44a47b92d7c406c [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>
Kyösti Mälkki46249be22014-10-27 14:07:28 +020020#include <string.h>
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020021
22#include "ehci_debug.h"
23#include "usb_ch9.h"
24#include "ehci.h"
25
Kyösti Mälkki46249be22014-10-27 14:07:28 +020026#define dprintk printk
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020027
28#define USB_HUB_PORT_CONNECTION 0
29#define USB_HUB_PORT_ENABLED 1
30#define USB_HUB_PORT_RESET 4
31#define USB_HUB_PORT_POWER 8
32#define USB_HUB_C_PORT_CONNECTION 16
33#define USB_HUB_C_PORT_RESET 20
34
35
36static int hub_port_status(const char * buf, int feature)
37{
38 return !!(buf[feature>>3] & (1<<(feature&0x7)));
39}
40
41/* After USB port reset, treat device number 0 as an USB hub. Assign it with
42 * a device number hub_addr. Then apply enable and reset on downstream port.
43 */
44 static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned char hub_addr,
45 unsigned char port)
46{
47 char status[8];
48 int ret, loop;
49
50 /* Assign a devicenumber for the hub. */
51 ret = dbgp_control_msg(ehci_debug, 0,
52 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
53 USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0);
54 if (ret < 0)
55 goto err;
56
57 /* Enter configured state on hub. */
58 ret = dbgp_control_msg(ehci_debug, hub_addr,
59 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
60 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
61 if (ret < 0)
62 goto err;
63
64 /* Set PORT_POWER, poll for PORT_CONNECTION. */
65 ret = dbgp_control_msg(ehci_debug, hub_addr,
66 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
67 USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0);
68 if (ret < 0)
69 goto err;
70
71 loop = 100;
72 do {
73 dbgp_mdelay(10);
74 ret = dbgp_control_msg(ehci_debug, hub_addr,
75 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
76 USB_REQ_GET_STATUS, 0, port, status, 4);
77 if (ret < 0)
78 goto err;
79 if (hub_port_status(status, USB_HUB_PORT_CONNECTION))
80 break;
81 } while (--loop);
82 if (! loop)
83 goto err;
84
85 ret = dbgp_control_msg(ehci_debug, hub_addr,
86 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
87 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0);
88 if (ret < 0)
89 goto err;
90
91
92 /* Set PORT_RESET, poll for C_PORT_RESET. */
93 ret = dbgp_control_msg(ehci_debug, hub_addr,
94 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
95 USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0);
96 if (ret < 0)
97 goto err;
98
99 loop = 100;
100 do {
101 dbgp_mdelay(10);
102 ret = dbgp_control_msg(ehci_debug, hub_addr,
103 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
104 USB_REQ_GET_STATUS, 0, port, status, 4);
105 if (ret < 0)
106 goto err;
107 if (hub_port_status(status, USB_HUB_C_PORT_RESET))
108 break;
109 } while (--loop);
110 if (! loop)
111 goto err;
112
113 ret = dbgp_control_msg(ehci_debug, hub_addr,
114 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
115 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0);
116 if (ret < 0)
117 goto err;
118
119 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
120 return 0;
121err:
122 return -1;
123}
124
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200125static void ack_set_configuration(struct dbgp_pipe *pipe, u8 devnum, int timeout)
126{
127 int i = DBGP_SETUP_EP0;
128 while (++i < DBGP_MAX_ENDPOINTS) {
129 if (pipe[i].endpoint != 0) {
130 pipe[i].devnum = devnum;
131 pipe[i].pid = USB_PID_DATA0;
132 pipe[i].timeout = timeout;
133 }
134 }
135}
136
137static void activate_endpoints(struct dbgp_pipe *pipe)
138{
139 int i = DBGP_SETUP_EP0;
140 pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
141 while (++i < DBGP_MAX_ENDPOINTS) {
142 if (pipe[i].endpoint != 0)
143 pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
144 }
145}
146
147static int probe_for_debug_descriptor(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
148{
149 struct usb_debug_descriptor dbgp_desc;
150 int configured = 0, ret;
151 u8 devnum = 0;
152
153 /* Find the debug device and make it device number 127 */
154debug_dev_retry:
155 memset(&dbgp_desc, 0, sizeof(dbgp_desc));
156 ret = dbgp_control_msg(ehci_debug, devnum,
157 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
158 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
159 &dbgp_desc, sizeof(dbgp_desc));
160 if (ret == sizeof(dbgp_desc)) {
161 if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType==USB_DT_DEBUG)
162 goto debug_dev_found;
163 else
164 dprintk(BIOS_INFO, "Invalid debug device descriptor.\n");
165 }
166 if (devnum == 0) {
167 devnum = USB_DEBUG_DEVNUM;
168 goto debug_dev_retry;
169 } else {
170 dprintk(BIOS_INFO, "Could not find attached debug device.\n");
171 return -1;
172 }
173debug_dev_found:
174
175 /* Move the device to 127 if it isn't already there */
176 if (devnum != USB_DEBUG_DEVNUM) {
177 ret = dbgp_control_msg(ehci_debug, devnum,
178 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
179 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
180 if (ret < 0) {
181 dprintk(BIOS_INFO, "Could not move attached device to %d.\n",
182 USB_DEBUG_DEVNUM);
183 return -2;
184 }
185 devnum = USB_DEBUG_DEVNUM;
186 dprintk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
187 }
188
189 /* Enable the debug interface */
190 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
191 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
192 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
193 if (ret < 0) {
194 dprintk(BIOS_INFO, "Could not enable EHCI debug device.\n");
195 return -3;
196 }
197 dprintk(BIOS_INFO, "EHCI debug interface enabled.\n");
198
199 pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint;
200 pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint;
201
202 ack_set_configuration(pipe, devnum, 1000);
203
204 /* Perform a small write. */
205 configured = 0;
206small_write:
207 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5);
208 if (ret < 0) {
209 dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
210 if (!configured) {
211 /* Send Set Configure request to device. This is required for FX2
212 (CY7C68013) to transfer from USB state Addressed to Configured,
213 only then endpoints other than 0 are enabled. */
214 if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
215 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
216 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) {
217 configured = 1;
218 goto small_write;
219 }
220 }
221 return -4;
222 }
223 dprintk(BIOS_INFO, "Test write done\n");
224 return 0;
225}
226
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300227/* FTDI FT232H UART programming. */
228#define FTDI_SIO_SET_FLOW_CTRL_REQUEST 0x02
229#define FTDI_SIO_SET_BAUDRATE_REQUEST 0x03
230#define FTDI_SIO_SET_DATA_REQUEST 0x04
231#define FTDI_SIO_SET_BITMODE_REQUEST 0x0b
232
233static int probe_for_ftdi(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
234{
235 int ret;
236 u8 devnum = 0;
237 u8 uart_if = 1; /* FTDI_INTERFACE_A 1 */
238
239 /* Move the device to 127 if it isn't already there */
240 ret = dbgp_control_msg(ehci_debug, devnum,
241 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
242 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
243 if (ret < 0) {
244 dprintk(BIOS_INFO, "Could not move attached device to %d.\n",
245 USB_DEBUG_DEVNUM);
246 return -2;
247 }
248 devnum = USB_DEBUG_DEVNUM;
249 dprintk(BIOS_INFO, "EHCI debug device renamed to %d.\n", devnum);
250
251 /* Send Set Configure request to device. */
252 ret = dbgp_control_msg(ehci_debug, devnum,
253 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
254 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
255 if (ret < 0) {
256 dprintk(BIOS_INFO, "FTDI set configuration failed.\n");
257 return -2;
258 }
259
260 ret = dbgp_control_msg(ehci_debug, devnum,
261 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
262 FTDI_SIO_SET_BITMODE_REQUEST, 0, uart_if, NULL, 0);
263 if (ret < 0) {
264 dprintk(BIOS_INFO, "FTDI SET_BITMODE failed.\n");
265 return -3;
266 }
267 ret = dbgp_control_msg(ehci_debug, devnum,
268 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
269 FTDI_SIO_SET_BAUDRATE_REQUEST,
270 0xc068, 0x0200 | uart_if, NULL, 0);
271 if (ret < 0) {
272 dprintk(BIOS_INFO, "FTDI SET_BAUDRATE failed.\n");
273 return -3;
274 }
275 ret = dbgp_control_msg(ehci_debug, devnum,
276 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
277 FTDI_SIO_SET_DATA_REQUEST,
278 0x0008, uart_if, NULL, 0);
279 if (ret < 0) {
280 dprintk(BIOS_INFO, "FTDI SET_DATA failed.\n");
281 return -3;
282 }
283 ret = dbgp_control_msg(ehci_debug, devnum,
284 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
285 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
286 0x0000, uart_if, NULL, 0);
287 if (ret < 0) {
288 dprintk(BIOS_INFO, "FTDI SET_FLOW_CTRL failed.\n");
289 return -3;
290 }
291
292 pipe[DBGP_CONSOLE_EPOUT].endpoint = 0x02;
293 pipe[DBGP_CONSOLE_EPIN].endpoint = 0x81;
294
295 ack_set_configuration(pipe, devnum, 1000);
296
297 /* Perform a small write. */
298 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n", 5);
299 if (ret < 0) {
300 dprintk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
301 return -4;
302 }
303 dprintk(BIOS_INFO, "Test write done\n");
304 return 0;
305}
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200306
307int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
308{
309 int ret;
310
311 if (CONFIG_USBDEBUG_OPTIONAL_HUB_PORT != 0) {
312 ret = dbgp_hub_enable(ehci_debug, USB_DEBUG_DEVNUM-1,
313 CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
314 if (ret < 0) {
315 printk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
316 return ret;
317 }
318 }
319
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300320 if (IS_ENABLED(CONFIG_USBDEBUG_DONGLE_FTDI_FT232H)) {
321 ret = probe_for_ftdi(ehci_debug, pipe);
322 } else {
323 ret = probe_for_debug_descriptor(ehci_debug, pipe);
324 }
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200325 if (ret < 0) {
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300326 dprintk(BIOS_INFO, "Could not enable debug dongle.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200327 return ret;
328 }
329
330 activate_endpoints(pipe);
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200331 return 0;
332}