blob: d7614474ced0ecb3d3bfc4b1df54d827ba808af5 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +02002
Elyes HAOUASd970e392020-07-10 10:58:10 +02003#include <stdint.h>
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +02004#include <console/console.h>
Kyösti Mälkki46249be22014-10-27 14:07:28 +02005#include <string.h>
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +02006
7#include "ehci_debug.h"
8#include "usb_ch9.h"
9#include "ehci.h"
10
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020011#define USB_HUB_PORT_CONNECTION 0
12#define USB_HUB_PORT_ENABLED 1
13#define USB_HUB_PORT_RESET 4
14#define USB_HUB_PORT_POWER 8
15#define USB_HUB_C_PORT_CONNECTION 16
16#define USB_HUB_C_PORT_RESET 20
17
Elyes HAOUASb0b0c8c2018-07-08 12:33:47 +020018static int hub_port_status(const char *buf, int feature)
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020019{
20 return !!(buf[feature>>3] & (1<<(feature&0x7)));
21}
22
23/* After USB port reset, treat device number 0 as an USB hub. Assign it with
24 * a device number hub_addr. Then apply enable and reset on downstream port.
25 */
Martin Rothb9810a42017-07-23 20:00:04 -060026static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned char hub_addr,
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020027 unsigned char port)
28{
29 char status[8];
30 int ret, loop;
31
32 /* Assign a devicenumber for the hub. */
33 ret = dbgp_control_msg(ehci_debug, 0,
34 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
35 USB_REQ_SET_ADDRESS, hub_addr, 0, NULL, 0);
36 if (ret < 0)
37 goto err;
38
39 /* Enter configured state on hub. */
40 ret = dbgp_control_msg(ehci_debug, hub_addr,
41 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
42 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
43 if (ret < 0)
44 goto err;
45
46 /* Set PORT_POWER, poll for PORT_CONNECTION. */
47 ret = dbgp_control_msg(ehci_debug, hub_addr,
48 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
49 USB_REQ_SET_FEATURE, USB_HUB_PORT_POWER, port, NULL, 0);
50 if (ret < 0)
51 goto err;
52
53 loop = 100;
54 do {
55 dbgp_mdelay(10);
56 ret = dbgp_control_msg(ehci_debug, hub_addr,
57 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
58 USB_REQ_GET_STATUS, 0, port, status, 4);
59 if (ret < 0)
60 goto err;
61 if (hub_port_status(status, USB_HUB_PORT_CONNECTION))
62 break;
63 } while (--loop);
64 if (! loop)
65 goto err;
66
67 ret = dbgp_control_msg(ehci_debug, hub_addr,
68 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
69 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_CONNECTION, port, NULL, 0);
70 if (ret < 0)
71 goto err;
72
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +020073 /* Set PORT_RESET, poll for C_PORT_RESET. */
74 ret = dbgp_control_msg(ehci_debug, hub_addr,
75 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
76 USB_REQ_SET_FEATURE, USB_HUB_PORT_RESET, port, NULL, 0);
77 if (ret < 0)
78 goto err;
79
80 loop = 100;
81 do {
82 dbgp_mdelay(10);
83 ret = dbgp_control_msg(ehci_debug, hub_addr,
84 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER,
85 USB_REQ_GET_STATUS, 0, port, status, 4);
86 if (ret < 0)
87 goto err;
88 if (hub_port_status(status, USB_HUB_C_PORT_RESET))
89 break;
90 } while (--loop);
91 if (! loop)
92 goto err;
93
94 ret = dbgp_control_msg(ehci_debug, hub_addr,
95 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER,
96 USB_REQ_CLEAR_FEATURE, USB_HUB_C_PORT_RESET, port, NULL, 0);
97 if (ret < 0)
98 goto err;
99
100 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
101 return 0;
102err:
103 return -1;
104}
105
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200106static void ack_set_configuration(struct dbgp_pipe *pipe, u8 devnum, int timeout)
107{
108 int i = DBGP_SETUP_EP0;
109 while (++i < DBGP_MAX_ENDPOINTS) {
110 if (pipe[i].endpoint != 0) {
111 pipe[i].devnum = devnum;
112 pipe[i].pid = USB_PID_DATA0;
113 pipe[i].timeout = timeout;
114 }
115 }
116}
117
118static void activate_endpoints(struct dbgp_pipe *pipe)
119{
120 int i = DBGP_SETUP_EP0;
121 pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
122 while (++i < DBGP_MAX_ENDPOINTS) {
123 if (pipe[i].endpoint != 0)
124 pipe[i].status |= DBGP_EP_ENABLED | DBGP_EP_VALID;
125 }
126}
127
128static int probe_for_debug_descriptor(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
129{
130 struct usb_debug_descriptor dbgp_desc;
131 int configured = 0, ret;
132 u8 devnum = 0;
133
134 /* Find the debug device and make it device number 127 */
135debug_dev_retry:
136 memset(&dbgp_desc, 0, sizeof(dbgp_desc));
137 ret = dbgp_control_msg(ehci_debug, devnum,
138 USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
139 USB_REQ_GET_DESCRIPTOR, (USB_DT_DEBUG << 8), 0,
140 &dbgp_desc, sizeof(dbgp_desc));
141 if (ret == sizeof(dbgp_desc)) {
Elyes HAOUAS9b41bae2018-04-26 10:29:10 +0200142 if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType == USB_DT_DEBUG)
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200143 goto debug_dev_found;
144 else
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100145 printk(BIOS_INFO, "Invalid debug device descriptor.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200146 }
147 if (devnum == 0) {
148 devnum = USB_DEBUG_DEVNUM;
149 goto debug_dev_retry;
150 } else {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100151 printk(BIOS_INFO, "Could not find attached debug device.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200152 return -1;
153 }
154debug_dev_found:
155
156 /* Move the device to 127 if it isn't already there */
157 if (devnum != USB_DEBUG_DEVNUM) {
158 ret = dbgp_control_msg(ehci_debug, devnum,
159 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
160 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
161 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100162 printk(BIOS_INFO, "Could not move attached device to %d.\n",
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200163 USB_DEBUG_DEVNUM);
164 return -2;
165 }
166 devnum = USB_DEBUG_DEVNUM;
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100167 printk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200168 }
169
170 /* Enable the debug interface */
171 ret = dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
172 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
173 USB_REQ_SET_FEATURE, USB_DEVICE_DEBUG_MODE, 0, NULL, 0);
174 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100175 printk(BIOS_INFO, "Could not enable EHCI debug device.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200176 return -3;
177 }
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100178 printk(BIOS_INFO, "EHCI debug interface enabled.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200179
180 pipe[DBGP_CONSOLE_EPOUT].endpoint = dbgp_desc.bDebugOutEndpoint;
181 pipe[DBGP_CONSOLE_EPIN].endpoint = dbgp_desc.bDebugInEndpoint;
182
183 ack_set_configuration(pipe, devnum, 1000);
184
185 /* Perform a small write. */
186 configured = 0;
187small_write:
188 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5);
189 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100190 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200191 if (!configured) {
192 /* Send Set Configure request to device. This is required for FX2
193 (CY7C68013) to transfer from USB state Addressed to Configured,
194 only then endpoints other than 0 are enabled. */
195 if (dbgp_control_msg(ehci_debug, USB_DEBUG_DEVNUM,
196 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
197 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0) >= 0) {
198 configured = 1;
199 goto small_write;
200 }
201 }
202 return -4;
203 }
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100204 printk(BIOS_INFO, "Test write done\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200205 return 0;
206}
207
Nicholas Chin81827aa2022-10-10 20:39:03 -0600208/* Refer to USB CDC PSTN Subclass specification section 6.3 */
209#define CDC_SET_LINE_CODING_REQUEST 0x20
210struct cdc_line_coding {
211 u32 baudrate;
212 u8 stop_bits;
213 u8 parity;
214 u8 data_bits;
215} __packed;
216
217static int probe_for_ch347(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
218{
219 int ret;
220 u8 devnum = 0;
221 u8 uart_if = 2; /* CH347 UART 1 */
222
223 /* Move the device to 127 if it isn't already there */
224 ret = dbgp_control_msg(ehci_debug, devnum,
225 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
226 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
227 if (ret < 0) {
228 printk(BIOS_INFO, "Could not move attached device to %d.\n",
229 USB_DEBUG_DEVNUM);
230 return -2;
231 }
232 devnum = USB_DEBUG_DEVNUM;
233 printk(BIOS_INFO, "EHCI debug device renamed to %d.\n", devnum);
234
235 /* Send Set Configure request to device. */
236 ret = dbgp_control_msg(ehci_debug, devnum,
237 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
238 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
239 if (ret < 0) {
240 printk(BIOS_INFO, "CH347 set configuration failed.\n");
241 return -2;
242 }
243
244 struct cdc_line_coding line_coding = {
245 .baudrate = CONFIG_USBDEBUG_DONGLE_WCH_CH347_BAUD,
246 .stop_bits = 0, /* 1 stop bit */
247 .parity = 0, /* No parity */
248 .data_bits = 8
249 };
250
251 ret = dbgp_control_msg(ehci_debug, devnum,
252 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
253 CDC_SET_LINE_CODING_REQUEST, 0, uart_if, &line_coding, sizeof(line_coding));
254 if (ret < 0) {
255 printk(BIOS_INFO, "CDC SET_LINE_CODING failed.\n");
256 return -3;
257 }
258
259 /* Modes 0, 1, and 3 all have UART 1 on endpoint 4 in common */
260 pipe[DBGP_CONSOLE_EPOUT].endpoint = 0x04;
261 pipe[DBGP_CONSOLE_EPIN].endpoint = 0x84;
262
263 ack_set_configuration(pipe, devnum, 1000);
264
265 /* Perform a small write. */
266 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n", 5);
267 if (ret < 0) {
268 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
269 return -4;
270 }
271 printk(BIOS_INFO, "Test write done\n");
272 return 0;
273}
274
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300275/* FTDI FT232H UART programming. */
276#define FTDI_SIO_SET_FLOW_CTRL_REQUEST 0x02
277#define FTDI_SIO_SET_BAUDRATE_REQUEST 0x03
278#define FTDI_SIO_SET_DATA_REQUEST 0x04
279#define FTDI_SIO_SET_BITMODE_REQUEST 0x0b
280
Nico Huberc868fd12016-11-18 20:11:43 +0100281/* Simplified divisor selection for 12MHz base clock only */
282static void ft232h_baud(u16 *const value, u16 *const index, u32 baud)
283{
284 static const u32 fraction_map[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
285
286 /* divisor must fit into 14 bits */
287 if (baud < 733)
288 baud = 733;
289
290 /* divisor is given as a fraction of 8 */
291 const u32 div8 = ((12 * 1000 * 1000) * 8) / baud;
292 /* upper 3 bits fractional part, lower 14 bits integer */
293 u32 div = fraction_map[div8 & 7] << 14 | div8 / 8;
294
295 /* special case for 12MHz */
296 if (div == 1)
297 div = 0;
298 /* special case for 8MHz */
299 else if (div == (fraction_map[4] << 14 | 1))
300 div = 1;
301
302 *value = div; /* divisor lower 16 bits */
303 *index = (div >> 8) & 0x0100; /* divisor bit 16 */
304 *index |= 0x0200; /* select 120MHz / 10 */
305}
306
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300307static int probe_for_ftdi(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
308{
309 int ret;
310 u8 devnum = 0;
311 u8 uart_if = 1; /* FTDI_INTERFACE_A 1 */
Nico Huberc868fd12016-11-18 20:11:43 +0100312 u16 baud_value, baud_index;
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300313
314 /* Move the device to 127 if it isn't already there */
315 ret = dbgp_control_msg(ehci_debug, devnum,
316 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
317 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
318 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100319 printk(BIOS_INFO, "Could not move attached device to %d.\n",
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300320 USB_DEBUG_DEVNUM);
321 return -2;
322 }
323 devnum = USB_DEBUG_DEVNUM;
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100324 printk(BIOS_INFO, "EHCI debug device renamed to %d.\n", devnum);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300325
326 /* Send Set Configure request to device. */
327 ret = dbgp_control_msg(ehci_debug, devnum,
328 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
329 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
330 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100331 printk(BIOS_INFO, "FTDI set configuration failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300332 return -2;
333 }
334
335 ret = dbgp_control_msg(ehci_debug, devnum,
336 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
337 FTDI_SIO_SET_BITMODE_REQUEST, 0, uart_if, NULL, 0);
338 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100339 printk(BIOS_INFO, "FTDI SET_BITMODE failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300340 return -3;
341 }
Nico Huberc868fd12016-11-18 20:11:43 +0100342 ft232h_baud(&baud_value, &baud_index,
343 CONFIG_USBDEBUG_DONGLE_FTDI_FT232H_BAUD);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300344 ret = dbgp_control_msg(ehci_debug, devnum,
345 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
346 FTDI_SIO_SET_BAUDRATE_REQUEST,
Nico Huberc868fd12016-11-18 20:11:43 +0100347 baud_value, baud_index | uart_if, NULL, 0);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300348 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100349 printk(BIOS_INFO, "FTDI SET_BAUDRATE failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300350 return -3;
351 }
352 ret = dbgp_control_msg(ehci_debug, devnum,
353 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
354 FTDI_SIO_SET_DATA_REQUEST,
355 0x0008, uart_if, NULL, 0);
356 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100357 printk(BIOS_INFO, "FTDI SET_DATA failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300358 return -3;
359 }
360 ret = dbgp_control_msg(ehci_debug, devnum,
361 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
362 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
363 0x0000, uart_if, NULL, 0);
364 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100365 printk(BIOS_INFO, "FTDI SET_FLOW_CTRL failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300366 return -3;
367 }
368
369 pipe[DBGP_CONSOLE_EPOUT].endpoint = 0x02;
370 pipe[DBGP_CONSOLE_EPIN].endpoint = 0x81;
371
372 ack_set_configuration(pipe, devnum, 1000);
373
374 /* Perform a small write. */
375 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n", 5);
376 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100377 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300378 return -4;
379 }
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100380 printk(BIOS_INFO, "Test write done\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300381 return 0;
382}
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200383
384int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
385{
386 int ret;
387
388 if (CONFIG_USBDEBUG_OPTIONAL_HUB_PORT != 0) {
389 ret = dbgp_hub_enable(ehci_debug, USB_DEBUG_DEVNUM-1,
390 CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
391 if (ret < 0) {
392 printk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
393 return ret;
394 }
395 }
396
Julius Wernercd49cce2019-03-05 16:53:33 -0800397 if (CONFIG(USBDEBUG_DONGLE_FTDI_FT232H)) {
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300398 ret = probe_for_ftdi(ehci_debug, pipe);
Nicholas Chin81827aa2022-10-10 20:39:03 -0600399 } else if (CONFIG(USBDEBUG_DONGLE_WCH_CH347)) {
400 ret = probe_for_ch347(ehci_debug, pipe);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300401 } else {
402 ret = probe_for_debug_descriptor(ehci_debug, pipe);
403 }
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200404 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100405 printk(BIOS_INFO, "Could not enable debug dongle.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200406 return ret;
407 }
408
409 activate_endpoints(pipe);
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200410 return 0;
411}