mb/google/brya: Create rull variant
[coreboot2.git] / src / drivers / usb / gadget.c
blobd7614474ced0ecb3d3bfc4b1df54d827ba808af5
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <stdint.h>
4 #include <console/console.h>
5 #include <string.h>
7 #include "ehci_debug.h"
8 #include "usb_ch9.h"
9 #include "ehci.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,
27 unsigned char port)
29 char status[8];
30 int ret, loop;
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;
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;
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;
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;
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;
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;
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;
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;
100 if (hub_port_status(status, USB_HUB_PORT_ENABLED))
101 return 0;
102 err:
103 return -1;
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;
132 u8 devnum = 0;
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");
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;
154 debug_dev_found:
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;
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);
174 if (ret < 0) {
175 printk(BIOS_INFO, "Could not enable EHCI debug device.\n");
176 return -3;
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. */
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;
202 return -4;
204 printk(BIOS_INFO, "Test write done\n");
205 return 0;
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;
217 static int probe_for_ch347(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
219 int ret;
220 u8 devnum = 0;
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);
227 if (ret < 0) {
228 printk(BIOS_INFO, "Could not move attached device to %d.\n",
229 USB_DEBUG_DEVNUM);
230 return -2;
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);
239 if (ret < 0) {
240 printk(BIOS_INFO, "CH347 set configuration failed.\n");
241 return -2;
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
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;
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);
267 if (ret < 0) {
268 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
269 return -4;
271 printk(BIOS_INFO, "Test write done\n");
272 return 0;
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 */
287 if (baud < 733)
288 baud = 733;
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 */
296 if (div == 1)
297 div = 0;
298 /* special case for 8MHz */
299 else if (div == (fraction_map[4] << 14 | 1))
300 div = 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)
309 int ret;
310 u8 devnum = 0;
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);
318 if (ret < 0) {
319 printk(BIOS_INFO, "Could not move attached device to %d.\n",
320 USB_DEBUG_DEVNUM);
321 return -2;
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);
330 if (ret < 0) {
331 printk(BIOS_INFO, "FTDI set configuration failed.\n");
332 return -2;
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;
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;
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;
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;
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);
376 if (ret < 0) {
377 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
378 return -4;
380 printk(BIOS_INFO, "Test write done\n");
381 return 0;
384 int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
386 int ret;
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;
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);
404 if (ret < 0) {
405 printk(BIOS_INFO, "Could not enable debug dongle.\n");
406 return ret;
409 activate_endpoints(pipe);
410 return 0;