1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2011 by Mathias Kuester *
5 * Mathias Kuester <kesmtp@freenet.de> *
7 * Copyright (C) 2012 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
9 ***************************************************************************/
15 /* project specific includes */
16 #include <jtag/interface.h>
17 #include <transport/transport.h>
18 #include <helper/time_support.h>
20 #include <jtag/hla/hla_tcl.h>
21 #include <jtag/hla/hla_layout.h>
22 #include <jtag/hla/hla_transport.h>
23 #include <jtag/hla/hla_interface.h>
25 #include <target/target.h>
27 static struct hl_interface_s hl_if = {
32 .transport = HL_TRANSPORT_UNKNOWN,
33 .connect_under_reset = false,
34 .initial_interface_speed = -1,
35 .use_stlink_tcp = false,
36 .stlink_tcp_port = 7184,
42 int hl_interface_open(enum hl_transports tr)
44 LOG_DEBUG("hl_interface_open");
46 enum reset_types jtag_reset_config = jtag_get_reset_config();
48 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
49 if (jtag_reset_config & RESET_SRST_NO_GATING)
50 hl_if.param.connect_under_reset = true;
52 LOG_WARNING("\'srst_nogate\' reset_config option is required");
55 /* set transport mode */
56 hl_if.param.transport = tr;
58 int result = hl_if.layout->open(&hl_if);
59 if (result != ERROR_OK)
62 return hl_interface_init_reset();
65 int hl_interface_init_target(struct target *t)
69 LOG_DEBUG("hl_interface_init_target");
71 /* this is the interface for the current target and we
72 * can setup the private pointer in the tap structure
73 * if the interface match the tap idcode
75 res = hl_if.layout->api->idcode(hl_if.handle, &t->tap->idcode);
80 unsigned ii, limit = t->tap->expected_ids_cnt;
83 for (ii = 0; ii < limit; ii++) {
84 uint32_t expected = t->tap->expected_ids[ii];
86 /* treat "-expected-id 0" as a "don't-warn" wildcard */
87 if (!expected || !t->tap->idcode ||
88 (t->tap->idcode == expected)) {
95 LOG_WARNING("UNEXPECTED idcode: 0x%08" PRIx32, t->tap->idcode);
96 for (ii = 0; ii < limit; ii++)
97 LOG_ERROR("expected %u of %u: 0x%08" PRIx32, ii + 1, limit,
98 t->tap->expected_ids[ii]);
103 t->tap->priv = &hl_if;
104 t->tap->hasidcode = 1;
109 static int hl_interface_init(void)
111 LOG_DEBUG("hl_interface_init");
113 /* here we can initialize the layout */
114 return hl_layout_init(&hl_if);
117 static int hl_interface_quit(void)
119 LOG_DEBUG("hl_interface_quit");
121 if (hl_if.layout->api->close)
122 hl_if.layout->api->close(hl_if.handle);
124 jtag_command_queue_reset();
126 free((void *)hl_if.param.device_desc);
131 static int hl_interface_reset(int req_trst, int req_srst)
133 return hl_if.layout->api->assert_srst(hl_if.handle, req_srst ? 0 : 1);
136 int hl_interface_init_reset(void)
138 /* in case the adapter has not already handled asserting srst
139 * we will attempt it again */
140 if (hl_if.param.connect_under_reset) {
141 adapter_assert_reset();
143 adapter_deassert_reset();
149 static int hl_interface_khz(int khz, int *jtag_speed)
151 if (!hl_if.layout->api->speed)
154 *jtag_speed = hl_if.layout->api->speed(hl_if.handle, khz, true);
158 static int hl_interface_speed_div(int speed, int *khz)
164 static int hl_interface_speed(int speed)
166 if (!hl_if.layout->api->speed)
170 /* pass speed as initial param as interface not open yet */
171 hl_if.param.initial_interface_speed = speed;
175 hl_if.layout->api->speed(hl_if.handle, speed, false);
180 int hl_interface_override_target(const char **targetname)
182 if (hl_if.layout->api->override_target) {
183 if (hl_if.layout->api->override_target(*targetname)) {
184 *targetname = "hla_target";
192 static int hl_interface_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
193 uint32_t port_size, unsigned int *trace_freq,
194 unsigned int traceclkin_freq, uint16_t *prescaler)
196 if (hl_if.layout->api->config_trace)
197 return hl_if.layout->api->config_trace(hl_if.handle, enabled,
198 pin_protocol, port_size, trace_freq, traceclkin_freq, prescaler);
200 LOG_ERROR("The selected interface does not support tracing");
207 static int hl_interface_poll_trace(uint8_t *buf, size_t *size)
209 if (hl_if.layout->api->poll_trace)
210 return hl_if.layout->api->poll_trace(hl_if.handle, buf, size);
215 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
217 LOG_DEBUG("hl_interface_handle_device_desc_command");
220 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
222 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
228 COMMAND_HANDLER(hl_interface_handle_layout_command)
230 LOG_DEBUG("hl_interface_handle_layout_command");
233 LOG_ERROR("Need exactly one argument to stlink_layout");
234 return ERROR_COMMAND_SYNTAX_ERROR;
238 LOG_ERROR("already specified hl_layout %s",
240 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
241 ? ERROR_FAIL : ERROR_OK;
244 for (const struct hl_layout *l = hl_layout_get_list(); l->name;
246 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
252 LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
256 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
258 if (CMD_ARGC > HLA_MAX_USB_IDS * 2) {
259 LOG_WARNING("ignoring extra IDs in hla_vid_pid "
260 "(maximum is %d pairs)", HLA_MAX_USB_IDS);
261 CMD_ARGC = HLA_MAX_USB_IDS * 2;
263 if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
264 LOG_WARNING("incomplete hla_vid_pid configuration directive");
265 return ERROR_COMMAND_SYNTAX_ERROR;
269 for (i = 0; i < CMD_ARGC; i += 2) {
270 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], hl_if.param.vid[i / 2]);
271 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], hl_if.param.pid[i / 2]);
275 * Explicitly terminate, in case there are multiple instances of
278 hl_if.param.vid[i / 2] = hl_if.param.pid[i / 2] = 0;
283 COMMAND_HANDLER(hl_interface_handle_stlink_backend_command)
286 bool use_stlink_tcp = false;
287 uint16_t stlink_tcp_port = 7184;
289 if (CMD_ARGC == 0 || CMD_ARGC > 2)
290 return ERROR_COMMAND_SYNTAX_ERROR;
291 else if (strcmp(CMD_ARGV[0], "usb") == 0) {
293 return ERROR_COMMAND_SYNTAX_ERROR;
294 /* else use_stlink_tcp = false (already the case ) */
295 } else if (strcmp(CMD_ARGV[0], "tcp") == 0) {
296 use_stlink_tcp = true;
298 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_tcp_port);
300 return ERROR_COMMAND_SYNTAX_ERROR;
302 hl_if.param.use_stlink_tcp = use_stlink_tcp;
303 hl_if.param.stlink_tcp_port = stlink_tcp_port;
308 COMMAND_HANDLER(interface_handle_hla_command)
311 return ERROR_COMMAND_SYNTAX_ERROR;
313 if (!hl_if.layout->api->custom_command) {
314 LOG_ERROR("The selected adapter doesn't support custom commands");
318 hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
323 static const struct command_registration hl_interface_command_handlers[] = {
325 .name = "hla_device_desc",
326 .handler = &hl_interface_handle_device_desc_command,
327 .mode = COMMAND_CONFIG,
328 .help = "set the device description of the adapter",
329 .usage = "description_string",
332 .name = "hla_layout",
333 .handler = &hl_interface_handle_layout_command,
334 .mode = COMMAND_CONFIG,
335 .help = "set the layout of the adapter",
336 .usage = "layout_name",
339 .name = "hla_vid_pid",
340 .handler = &hl_interface_handle_vid_pid_command,
341 .mode = COMMAND_CONFIG,
342 .help = "the vendor and product ID of the adapter",
343 .usage = "(vid pid)*",
346 .name = "hla_stlink_backend",
347 .handler = &hl_interface_handle_stlink_backend_command,
348 .mode = COMMAND_CONFIG,
349 .help = "select which ST-Link backend to use",
350 .usage = "usb | tcp [port]",
353 .name = "hla_command",
354 .handler = &interface_handle_hla_command,
355 .mode = COMMAND_EXEC,
356 .help = "execute a custom adapter-specific command",
357 .usage = "<command>",
359 COMMAND_REGISTRATION_DONE
362 struct adapter_driver hl_adapter_driver = {
364 .transports = hl_transports,
365 .commands = hl_interface_command_handlers,
367 .init = hl_interface_init,
368 .quit = hl_interface_quit,
369 .reset = hl_interface_reset,
370 .speed = &hl_interface_speed,
371 .khz = &hl_interface_khz,
372 .speed_div = &hl_interface_speed_div,
373 .config_trace = &hl_interface_config_trace,
374 .poll_trace = &hl_interface_poll_trace,
376 /* no ops for HLA, targets hla_target and stm8 intercept them all */