xref: /aosp_15_r20/external/coreboot/src/drivers/usb/gadget.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 #include <console/console.h>
5 #include <string.h>
6 
7 #include "ehci_debug.h"
8 #include "usb_ch9.h"
9 #include "ehci.h"
10 
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
17 
hub_port_status(const char * buf,int feature)18 static int hub_port_status(const char *buf, int feature)
19 {
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  */
dbgp_hub_enable(struct ehci_dbg_port * ehci_debug,unsigned char hub_addr,unsigned char port)26 static int dbgp_hub_enable(struct ehci_dbg_port *ehci_debug, unsigned char hub_addr,
27 	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 
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);
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;
102 err:
103 	return -1;
104 }
105 
ack_set_configuration(struct dbgp_pipe * pipe,u8 devnum,int timeout)106 static 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 
activate_endpoints(struct dbgp_pipe * pipe)118 static 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 
probe_for_debug_descriptor(struct ehci_dbg_port * ehci_debug,struct dbgp_pipe * pipe)128 static 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 */
135 debug_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)) {
142 		if (dbgp_desc.bLength == sizeof(dbgp_desc) && dbgp_desc.bDescriptorType == USB_DT_DEBUG)
143 			goto debug_dev_found;
144 		else
145 			printk(BIOS_INFO, "Invalid debug device descriptor.\n");
146 	}
147 	if (devnum == 0) {
148 		devnum = USB_DEBUG_DEVNUM;
149 		goto debug_dev_retry;
150 	} else {
151 		printk(BIOS_INFO, "Could not find attached debug device.\n");
152 		return -1;
153 	}
154 debug_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) {
162 			printk(BIOS_INFO, "Could not move attached device to %d.\n",
163 				USB_DEBUG_DEVNUM);
164 			return -2;
165 		}
166 		devnum = USB_DEBUG_DEVNUM;
167 		printk(BIOS_INFO, "EHCI debug device renamed to 127.\n");
168 	}
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) {
175 		printk(BIOS_INFO, "Could not enable EHCI debug device.\n");
176 		return -3;
177 	}
178 	printk(BIOS_INFO, "EHCI debug interface enabled.\n");
179 
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;
187 small_write:
188 	ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n",5);
189 	if (ret < 0) {
190 		printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
191 		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 	}
204 	printk(BIOS_INFO, "Test write done\n");
205 	return 0;
206 }
207 
208 /* Refer to USB CDC PSTN Subclass specification section 6.3 */
209 #define CDC_SET_LINE_CODING_REQUEST 0x20
210 struct cdc_line_coding {
211 	u32 baudrate;
212 	u8 stop_bits;
213 	u8 parity;
214 	u8 data_bits;
215 } __packed;
216 
probe_for_ch347(struct ehci_dbg_port * ehci_debug,struct dbgp_pipe * pipe)217 static 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 
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
280 
281 /* Simplified divisor selection for 12MHz base clock only */
ft232h_baud(u16 * const value,u16 * const index,u32 baud)282 static 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 
probe_for_ftdi(struct ehci_dbg_port * ehci_debug,struct dbgp_pipe * pipe)307 static 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 */
312 	u16 baud_value, baud_index;
313 
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) {
319 		printk(BIOS_INFO, "Could not move attached device to %d.\n",
320 			USB_DEBUG_DEVNUM);
321 			return -2;
322 	}
323 	devnum = USB_DEBUG_DEVNUM;
324 	printk(BIOS_INFO, "EHCI debug device renamed to %d.\n", devnum);
325 
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) {
331 		printk(BIOS_INFO, "FTDI set configuration failed.\n");
332 		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) {
339 		printk(BIOS_INFO, "FTDI SET_BITMODE failed.\n");
340 		return -3;
341 	}
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);
348 	if (ret < 0) {
349 		printk(BIOS_INFO, "FTDI SET_BAUDRATE failed.\n");
350 		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) {
357 		printk(BIOS_INFO, "FTDI SET_DATA failed.\n");
358 		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) {
365 		printk(BIOS_INFO, "FTDI SET_FLOW_CTRL failed.\n");
366 		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) {
377 		printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
378 		return -4;
379 	}
380 	printk(BIOS_INFO, "Test write done\n");
381 	return 0;
382 }
383 
dbgp_probe_gadget(struct ehci_dbg_port * ehci_debug,struct dbgp_pipe * pipe)384 int 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 
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);
401 	} else {
402 		ret = probe_for_debug_descriptor(ehci_debug, pipe);
403 	}
404 	if (ret < 0) {
405 		printk(BIOS_INFO, "Could not enable debug dongle.\n");
406 		return ret;
407 	}
408 
409 	activate_endpoints(pipe);
410 	return 0;
411 }
412