blob: fcb361cc8318760c40416faf0e59734d4cca5216 [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
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300208/* FTDI FT232H UART programming. */
209#define FTDI_SIO_SET_FLOW_CTRL_REQUEST 0x02
210#define FTDI_SIO_SET_BAUDRATE_REQUEST 0x03
211#define FTDI_SIO_SET_DATA_REQUEST 0x04
212#define FTDI_SIO_SET_BITMODE_REQUEST 0x0b
213
Nico Huberc868fd12016-11-18 20:11:43 +0100214/* Simplified divisor selection for 12MHz base clock only */
215static void ft232h_baud(u16 *const value, u16 *const index, u32 baud)
216{
217 static const u32 fraction_map[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
218
219 /* divisor must fit into 14 bits */
220 if (baud < 733)
221 baud = 733;
222
223 /* divisor is given as a fraction of 8 */
224 const u32 div8 = ((12 * 1000 * 1000) * 8) / baud;
225 /* upper 3 bits fractional part, lower 14 bits integer */
226 u32 div = fraction_map[div8 & 7] << 14 | div8 / 8;
227
228 /* special case for 12MHz */
229 if (div == 1)
230 div = 0;
231 /* special case for 8MHz */
232 else if (div == (fraction_map[4] << 14 | 1))
233 div = 1;
234
235 *value = div; /* divisor lower 16 bits */
236 *index = (div >> 8) & 0x0100; /* divisor bit 16 */
237 *index |= 0x0200; /* select 120MHz / 10 */
238}
239
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300240static int probe_for_ftdi(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
241{
242 int ret;
243 u8 devnum = 0;
244 u8 uart_if = 1; /* FTDI_INTERFACE_A 1 */
Nico Huberc868fd12016-11-18 20:11:43 +0100245 u16 baud_value, baud_index;
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300246
247 /* Move the device to 127 if it isn't already there */
248 ret = dbgp_control_msg(ehci_debug, devnum,
249 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
250 USB_REQ_SET_ADDRESS, USB_DEBUG_DEVNUM, 0, NULL, 0);
251 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100252 printk(BIOS_INFO, "Could not move attached device to %d.\n",
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300253 USB_DEBUG_DEVNUM);
254 return -2;
255 }
256 devnum = USB_DEBUG_DEVNUM;
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100257 printk(BIOS_INFO, "EHCI debug device renamed to %d.\n", devnum);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300258
259 /* Send Set Configure request to device. */
260 ret = dbgp_control_msg(ehci_debug, devnum,
261 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
262 USB_REQ_SET_CONFIGURATION, 1, 0, NULL, 0);
263 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100264 printk(BIOS_INFO, "FTDI set configuration failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300265 return -2;
266 }
267
268 ret = dbgp_control_msg(ehci_debug, devnum,
269 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
270 FTDI_SIO_SET_BITMODE_REQUEST, 0, uart_if, NULL, 0);
271 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100272 printk(BIOS_INFO, "FTDI SET_BITMODE failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300273 return -3;
274 }
Nico Huberc868fd12016-11-18 20:11:43 +0100275 ft232h_baud(&baud_value, &baud_index,
276 CONFIG_USBDEBUG_DONGLE_FTDI_FT232H_BAUD);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300277 ret = dbgp_control_msg(ehci_debug, devnum,
278 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
279 FTDI_SIO_SET_BAUDRATE_REQUEST,
Nico Huberc868fd12016-11-18 20:11:43 +0100280 baud_value, baud_index | uart_if, NULL, 0);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300281 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100282 printk(BIOS_INFO, "FTDI SET_BAUDRATE failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300283 return -3;
284 }
285 ret = dbgp_control_msg(ehci_debug, devnum,
286 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
287 FTDI_SIO_SET_DATA_REQUEST,
288 0x0008, uart_if, NULL, 0);
289 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100290 printk(BIOS_INFO, "FTDI SET_DATA failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300291 return -3;
292 }
293 ret = dbgp_control_msg(ehci_debug, devnum,
294 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
295 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
296 0x0000, uart_if, NULL, 0);
297 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100298 printk(BIOS_INFO, "FTDI SET_FLOW_CTRL failed.\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300299 return -3;
300 }
301
302 pipe[DBGP_CONSOLE_EPOUT].endpoint = 0x02;
303 pipe[DBGP_CONSOLE_EPIN].endpoint = 0x81;
304
305 ack_set_configuration(pipe, devnum, 1000);
306
307 /* Perform a small write. */
308 ret = dbgp_bulk_write_x(&pipe[DBGP_CONSOLE_EPOUT], "USB\r\n", 5);
309 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100310 printk(BIOS_INFO, "dbgp_bulk_write failed: %d\n", ret);
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300311 return -4;
312 }
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100313 printk(BIOS_INFO, "Test write done\n");
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300314 return 0;
315}
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200316
317int dbgp_probe_gadget(struct ehci_dbg_port *ehci_debug, struct dbgp_pipe *pipe)
318{
319 int ret;
320
321 if (CONFIG_USBDEBUG_OPTIONAL_HUB_PORT != 0) {
322 ret = dbgp_hub_enable(ehci_debug, USB_DEBUG_DEVNUM-1,
323 CONFIG_USBDEBUG_OPTIONAL_HUB_PORT);
324 if (ret < 0) {
325 printk(BIOS_INFO, "Could not enable USB hub on debug port.\n");
326 return ret;
327 }
328 }
329
Julius Wernercd49cce2019-03-05 16:53:33 -0800330 if (CONFIG(USBDEBUG_DONGLE_FTDI_FT232H)) {
Kyösti Mälkkicc4d3092015-05-03 06:27:11 +0300331 ret = probe_for_ftdi(ehci_debug, pipe);
332 } else {
333 ret = probe_for_debug_descriptor(ehci_debug, pipe);
334 }
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200335 if (ret < 0) {
Elyes HAOUASe7b96c32022-01-26 14:33:47 +0100336 printk(BIOS_INFO, "Could not enable debug dongle.\n");
Kyösti Mälkki46249be22014-10-27 14:07:28 +0200337 return ret;
338 }
339
340 activate_endpoints(pipe);
Kyösti Mälkki83fe6d72014-02-25 20:11:52 +0200341 return 0;
342}