1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
7 #include "ehci_debug.h"
11 #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
18 static int hub_port_status(const char *buf
, int feature
)
20 return !!(buf
[feature
>>3] & (1<<(feature
&0x7)));
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.
26 static int dbgp_hub_enable(struct ehci_dbg_port
*ehci_debug
, unsigned char hub_addr
,
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);
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);
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);
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);
61 if (hub_port_status(status
, USB_HUB_PORT_CONNECTION
))
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);
73 /* 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);
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);
88 if (hub_port_status(status
, USB_HUB_C_PORT_RESET
))
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);
100 if (hub_port_status(status
, USB_HUB_PORT_ENABLED
))
106 static void ack_set_configuration(struct dbgp_pipe
*pipe
, u8 devnum
, int timeout
)
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
;
118 static void activate_endpoints(struct dbgp_pipe
*pipe
)
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
;
128 static int probe_for_debug_descriptor(struct ehci_dbg_port
*ehci_debug
, struct dbgp_pipe
*pipe
)
130 struct usb_debug_descriptor dbgp_desc
;
131 int configured
= 0, ret
;
134 /* Find the debug device and make it device number 127 */
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
)) {
142 if (dbgp_desc
.bLength
== sizeof(dbgp_desc
) && dbgp_desc
.bDescriptorType
== USB_DT_DEBUG
)
143 goto debug_dev_found
;
145 printk(BIOS_INFO
, "Invalid debug device descriptor.\n");
148 devnum
= USB_DEBUG_DEVNUM
;
149 goto debug_dev_retry
;
151 printk(BIOS_INFO
, "Could not find attached debug device.\n");
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);
162 printk(BIOS_INFO
, "Could not move attached device to %d.\n",
166 devnum
= USB_DEBUG_DEVNUM
;
167 printk(BIOS_INFO
, "EHCI debug device renamed to 127.\n");
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);
175 printk(BIOS_INFO
, "Could not enable EHCI debug device.\n");
178 printk(BIOS_INFO
, "EHCI debug interface enabled.\n");
180 pipe
[DBGP_CONSOLE_EPOUT
].endpoint
= dbgp_desc
.bDebugOutEndpoint
;
181 pipe
[DBGP_CONSOLE_EPIN
].endpoint
= dbgp_desc
.bDebugInEndpoint
;
183 ack_set_configuration(pipe
, devnum
, 1000);
185 /* Perform a small write. */
188 ret
= dbgp_bulk_write_x(&pipe
[DBGP_CONSOLE_EPOUT
], "USB\r\n",5);
190 printk(BIOS_INFO
, "dbgp_bulk_write failed: %d\n", ret
);
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) {
204 printk(BIOS_INFO
, "Test write done\n");
208 /* Refer to USB CDC PSTN Subclass specification section 6.3 */
209 #define CDC_SET_LINE_CODING_REQUEST 0x20
210 struct cdc_line_coding
{
217 static int probe_for_ch347(struct ehci_dbg_port
*ehci_debug
, struct dbgp_pipe
*pipe
)
221 u8 uart_if
= 2; /* CH347 UART 1 */
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);
228 printk(BIOS_INFO
, "Could not move attached device to %d.\n",
232 devnum
= USB_DEBUG_DEVNUM
;
233 printk(BIOS_INFO
, "EHCI debug device renamed to %d.\n", devnum
);
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);
240 printk(BIOS_INFO
, "CH347 set configuration failed.\n");
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 */
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
));
255 printk(BIOS_INFO
, "CDC SET_LINE_CODING failed.\n");
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;
263 ack_set_configuration(pipe
, devnum
, 1000);
265 /* Perform a small write. */
266 ret
= dbgp_bulk_write_x(&pipe
[DBGP_CONSOLE_EPOUT
], "USB\r\n", 5);
268 printk(BIOS_INFO
, "dbgp_bulk_write failed: %d\n", ret
);
271 printk(BIOS_INFO
, "Test write done\n");
275 /* 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
281 /* Simplified divisor selection for 12MHz base clock only */
282 static void ft232h_baud(u16
*const value
, u16
*const index
, u32 baud
)
284 static const u32 fraction_map
[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
286 /* divisor must fit into 14 bits */
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;
295 /* special case for 12MHz */
298 /* special case for 8MHz */
299 else if (div
== (fraction_map
[4] << 14 | 1))
302 *value
= div
; /* divisor lower 16 bits */
303 *index
= (div
>> 8) & 0x0100; /* divisor bit 16 */
304 *index
|= 0x0200; /* select 120MHz / 10 */
307 static int probe_for_ftdi(struct ehci_dbg_port
*ehci_debug
, struct dbgp_pipe
*pipe
)
311 u8 uart_if
= 1; /* FTDI_INTERFACE_A 1 */
312 u16 baud_value
, baud_index
;
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);
319 printk(BIOS_INFO
, "Could not move attached device to %d.\n",
323 devnum
= USB_DEBUG_DEVNUM
;
324 printk(BIOS_INFO
, "EHCI debug device renamed to %d.\n", devnum
);
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);
331 printk(BIOS_INFO
, "FTDI set configuration failed.\n");
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);
339 printk(BIOS_INFO
, "FTDI SET_BITMODE failed.\n");
342 ft232h_baud(&baud_value
, &baud_index
,
343 CONFIG_USBDEBUG_DONGLE_FTDI_FT232H_BAUD
);
344 ret
= dbgp_control_msg(ehci_debug
, devnum
,
345 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
346 FTDI_SIO_SET_BAUDRATE_REQUEST
,
347 baud_value
, baud_index
| uart_if
, NULL
, 0);
349 printk(BIOS_INFO
, "FTDI SET_BAUDRATE failed.\n");
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);
357 printk(BIOS_INFO
, "FTDI SET_DATA failed.\n");
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);
365 printk(BIOS_INFO
, "FTDI SET_FLOW_CTRL failed.\n");
369 pipe
[DBGP_CONSOLE_EPOUT
].endpoint
= 0x02;
370 pipe
[DBGP_CONSOLE_EPIN
].endpoint
= 0x81;
372 ack_set_configuration(pipe
, devnum
, 1000);
374 /* Perform a small write. */
375 ret
= dbgp_bulk_write_x(&pipe
[DBGP_CONSOLE_EPOUT
], "USB\r\n", 5);
377 printk(BIOS_INFO
, "dbgp_bulk_write failed: %d\n", ret
);
380 printk(BIOS_INFO
, "Test write done\n");
384 int dbgp_probe_gadget(struct ehci_dbg_port
*ehci_debug
, struct dbgp_pipe
*pipe
)
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
);
392 printk(BIOS_INFO
, "Could not enable USB hub on debug port.\n");
397 if (CONFIG(USBDEBUG_DONGLE_FTDI_FT232H
)) {
398 ret
= probe_for_ftdi(ehci_debug
, pipe
);
399 } else if (CONFIG(USBDEBUG_DONGLE_WCH_CH347
)) {
400 ret
= probe_for_ch347(ehci_debug
, pipe
);
402 ret
= probe_for_debug_descriptor(ehci_debug
, pipe
);
405 printk(BIOS_INFO
, "Could not enable debug dongle.\n");
409 activate_endpoints(pipe
);