1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
33 #include <jtag/hla/hla_tcl.h>
34 #include <jtag/hla/hla_layout.h>
35 #include <jtag/hla/hla_transport.h>
36 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
40 static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, false, NULL, 0, -1}, 0, 0 };
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.param.trace_f) {
122 fclose(hl_if.param.trace_f);
123 hl_if.param.trace_f = NULL;
125 hl_if.param.trace_source_hz = 0;
127 if (hl_if.layout->api->close)
128 hl_if.layout->api->close(hl_if.handle);
133 static int hl_interface_execute_queue(void)
135 LOG_DEBUG("hl_interface_execute_queue: ignored");
140 int hl_interface_init_reset(void)
142 /* incase the adapter has not already handled asserting srst
143 * we will attempt it again */
144 if (hl_if.param.connect_under_reset) {
145 jtag_add_reset(0, 1);
146 hl_if.layout->api->assert_srst(hl_if.handle, 0);
148 jtag_add_reset(0, 0);
154 static int hl_interface_khz(int khz, int *jtag_speed)
156 if (hl_if.layout->api->speed == NULL)
159 *jtag_speed = hl_if.layout->api->speed(hl_if.handle, khz, true);
163 static int hl_interface_speed_div(int speed, int *khz)
169 static int hl_interface_speed(int speed)
171 if (hl_if.layout->api->speed == NULL)
174 if (hl_if.handle == NULL) {
175 /* pass speed as initial param as interface not open yet */
176 hl_if.param.initial_interface_speed = speed;
180 hl_if.layout->api->speed(hl_if.handle, speed, false);
185 int hl_interface_override_target(const char **targetname)
187 if (hl_if.layout->api->override_target) {
188 if (hl_if.layout->api->override_target(*targetname)) {
189 *targetname = "hla_target";
197 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
199 LOG_DEBUG("hl_interface_handle_device_desc_command");
202 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
204 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
210 COMMAND_HANDLER(hl_interface_handle_serial_command)
212 LOG_DEBUG("hl_interface_handle_serial_command");
215 hl_if.param.serial = strdup(CMD_ARGV[0]);
217 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
223 COMMAND_HANDLER(hl_interface_handle_layout_command)
225 LOG_DEBUG("hl_interface_handle_layout_command");
228 LOG_ERROR("Need exactly one argument to stlink_layout");
229 return ERROR_COMMAND_SYNTAX_ERROR;
233 LOG_ERROR("already specified hl_layout %s",
235 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
236 ? ERROR_FAIL : ERROR_OK;
239 for (const struct hl_layout *l = hl_layout_get_list(); l->name;
241 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
247 LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
251 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
253 LOG_DEBUG("hl_interface_handle_vid_pid_command");
256 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
257 return ERROR_COMMAND_SYNTAX_ERROR;
260 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], hl_if.param.vid);
261 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], hl_if.param.pid);
266 COMMAND_HANDLER(interface_handle_trace_command)
271 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
272 return ERROR_COMMAND_SYNTAX_ERROR;
274 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], source_hz);
275 if (source_hz == 0) {
276 return ERROR_COMMAND_SYNTAX_ERROR;
280 f = fopen(CMD_ARGV[1], "a");
282 return ERROR_COMMAND_SYNTAX_ERROR;
285 hl_if.param.trace_f = f;
286 hl_if.param.trace_source_hz = source_hz;
291 COMMAND_HANDLER(interface_handle_hla_command)
294 return ERROR_COMMAND_SYNTAX_ERROR;
296 if (!hl_if.layout->api->custom_command) {
297 LOG_ERROR("The selected adapter doesn't support custom commands");
301 hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
306 static const struct command_registration hl_interface_command_handlers[] = {
308 .name = "hla_device_desc",
309 .handler = &hl_interface_handle_device_desc_command,
310 .mode = COMMAND_CONFIG,
311 .help = "set the a device description of the adapter",
312 .usage = "description_string",
315 .name = "hla_serial",
316 .handler = &hl_interface_handle_serial_command,
317 .mode = COMMAND_CONFIG,
318 .help = "set the serial number of the adapter",
319 .usage = "serial_string",
322 .name = "hla_layout",
323 .handler = &hl_interface_handle_layout_command,
324 .mode = COMMAND_CONFIG,
325 .help = "set the layout of the adapter",
326 .usage = "layout_name",
329 .name = "hla_vid_pid",
330 .handler = &hl_interface_handle_vid_pid_command,
331 .mode = COMMAND_CONFIG,
332 .help = "the vendor and product ID of the adapter",
333 .usage = "(vid pid)* ",
337 .handler = &interface_handle_trace_command,
338 .mode = COMMAND_CONFIG,
339 .help = "configure trace reception",
340 .usage = "source_lock_hz [destination_path]",
343 .name = "hla_command",
344 .handler = &interface_handle_hla_command,
345 .mode = COMMAND_EXEC,
346 .help = "execute a custom adapter-specific command",
347 .usage = "hla_command <command>",
349 COMMAND_REGISTRATION_DONE
352 struct jtag_interface hl_interface = {
355 .commands = hl_interface_command_handlers,
356 .transports = hl_transports,
357 .init = hl_interface_init,
358 .quit = hl_interface_quit,
359 .execute_queue = hl_interface_execute_queue,
360 .speed = &hl_interface_speed,
361 .khz = &hl_interface_khz,
362 .speed_div = &hl_interface_speed_div,