jtag/hla, jtag/stlink: switch to command 'adapter serial'
[fw/openocd] / src / jtag / hla / hla_interface.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   Copyright (C) 2012 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
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.                                   *
12  *                                                                         *
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.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 /* project specific includes */
27 #include <jtag/interface.h>
28 #include <transport/transport.h>
29 #include <helper/time_support.h>
30
31 #include <jtag/hla/hla_tcl.h>
32 #include <jtag/hla/hla_layout.h>
33 #include <jtag/hla/hla_transport.h>
34 #include <jtag/hla/hla_interface.h>
35
36 #include <target/target.h>
37
38 static struct hl_interface_s hl_if = {
39         .param = {
40                 .device_desc = NULL,
41                 .vid = { 0 },
42                 .pid = { 0 },
43                 .transport = HL_TRANSPORT_UNKNOWN,
44                 .connect_under_reset = false,
45                 .initial_interface_speed = -1,
46                 .use_stlink_tcp = false,
47                 .stlink_tcp_port = 7184,
48         },
49         .layout = NULL,
50         .handle = NULL,
51 };
52
53 int hl_interface_open(enum hl_transports tr)
54 {
55         LOG_DEBUG("hl_interface_open");
56
57         enum reset_types jtag_reset_config = jtag_get_reset_config();
58
59         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
60                 if (jtag_reset_config & RESET_SRST_NO_GATING)
61                         hl_if.param.connect_under_reset = true;
62                 else
63                         LOG_WARNING("\'srst_nogate\' reset_config option is required");
64         }
65
66         /* set transport mode */
67         hl_if.param.transport = tr;
68
69         int result = hl_if.layout->open(&hl_if);
70         if (result != ERROR_OK)
71                 return result;
72
73         return hl_interface_init_reset();
74 }
75
76 int hl_interface_init_target(struct target *t)
77 {
78         int res;
79
80         LOG_DEBUG("hl_interface_init_target");
81
82         /* this is the interface for the current target and we
83          * can setup the private pointer in the tap structure
84          * if the interface match the tap idcode
85          */
86         res = hl_if.layout->api->idcode(hl_if.handle, &t->tap->idcode);
87
88         if (res != ERROR_OK)
89                 return res;
90
91         unsigned ii, limit = t->tap->expected_ids_cnt;
92         int found = 0;
93
94         for (ii = 0; ii < limit; ii++) {
95                 uint32_t expected = t->tap->expected_ids[ii];
96
97                 /* treat "-expected-id 0" as a "don't-warn" wildcard */
98                 if (!expected || !t->tap->idcode ||
99                     (t->tap->idcode == expected)) {
100                         found = 1;
101                         break;
102                 }
103         }
104
105         if (found == 0) {
106                 LOG_WARNING("UNEXPECTED idcode: 0x%08" PRIx32, t->tap->idcode);
107                 for (ii = 0; ii < limit; ii++)
108                         LOG_ERROR("expected %u of %u: 0x%08" PRIx32, ii + 1, limit,
109                                 t->tap->expected_ids[ii]);
110
111                 return ERROR_FAIL;
112         }
113
114         t->tap->priv = &hl_if;
115         t->tap->hasidcode = 1;
116
117         return ERROR_OK;
118 }
119
120 static int hl_interface_init(void)
121 {
122         LOG_DEBUG("hl_interface_init");
123
124         /* here we can initialize the layout */
125         return hl_layout_init(&hl_if);
126 }
127
128 static int hl_interface_quit(void)
129 {
130         LOG_DEBUG("hl_interface_quit");
131
132         if (hl_if.layout->api->close)
133                 hl_if.layout->api->close(hl_if.handle);
134
135         jtag_command_queue_reset();
136
137         free((void *)hl_if.param.device_desc);
138
139         return ERROR_OK;
140 }
141
142 static int hl_interface_reset(int req_trst, int req_srst)
143 {
144         return hl_if.layout->api->assert_srst(hl_if.handle, req_srst ? 0 : 1);
145 }
146
147 int hl_interface_init_reset(void)
148 {
149         /* in case the adapter has not already handled asserting srst
150          * we will attempt it again */
151         if (hl_if.param.connect_under_reset) {
152                 adapter_assert_reset();
153         } else {
154                 adapter_deassert_reset();
155         }
156
157         return ERROR_OK;
158 }
159
160 static int hl_interface_khz(int khz, int *jtag_speed)
161 {
162         if (!hl_if.layout->api->speed)
163                 return ERROR_OK;
164
165         *jtag_speed = hl_if.layout->api->speed(hl_if.handle, khz, true);
166         return ERROR_OK;
167 }
168
169 static int hl_interface_speed_div(int speed, int *khz)
170 {
171         *khz = speed;
172         return ERROR_OK;
173 }
174
175 static int hl_interface_speed(int speed)
176 {
177         if (!hl_if.layout->api->speed)
178                 return ERROR_OK;
179
180         if (!hl_if.handle) {
181                 /* pass speed as initial param as interface not open yet */
182                 hl_if.param.initial_interface_speed = speed;
183                 return ERROR_OK;
184         }
185
186         hl_if.layout->api->speed(hl_if.handle, speed, false);
187
188         return ERROR_OK;
189 }
190
191 int hl_interface_override_target(const char **targetname)
192 {
193         if (hl_if.layout->api->override_target) {
194                 if (hl_if.layout->api->override_target(*targetname)) {
195                         *targetname = "hla_target";
196                         return ERROR_OK;
197                 } else
198                         return ERROR_FAIL;
199         }
200         return ERROR_FAIL;
201 }
202
203 static int hl_interface_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
204                 uint32_t port_size, unsigned int *trace_freq,
205                 unsigned int traceclkin_freq, uint16_t *prescaler)
206 {
207         if (hl_if.layout->api->config_trace)
208                 return hl_if.layout->api->config_trace(hl_if.handle, enabled,
209                         pin_protocol, port_size, trace_freq, traceclkin_freq, prescaler);
210         else if (enabled) {
211                 LOG_ERROR("The selected interface does not support tracing");
212                 return ERROR_FAIL;
213         }
214
215         return ERROR_OK;
216 }
217
218 static int hl_interface_poll_trace(uint8_t *buf, size_t *size)
219 {
220         if (hl_if.layout->api->poll_trace)
221                 return hl_if.layout->api->poll_trace(hl_if.handle, buf, size);
222
223         return ERROR_FAIL;
224 }
225
226 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
227 {
228         LOG_DEBUG("hl_interface_handle_device_desc_command");
229
230         if (CMD_ARGC == 1) {
231                 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
232         } else {
233                 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
234         }
235
236         return ERROR_OK;
237 }
238
239 COMMAND_HANDLER(hl_interface_handle_layout_command)
240 {
241         LOG_DEBUG("hl_interface_handle_layout_command");
242
243         if (CMD_ARGC != 1) {
244                 LOG_ERROR("Need exactly one argument to stlink_layout");
245                 return ERROR_COMMAND_SYNTAX_ERROR;
246         }
247
248         if (hl_if.layout) {
249                 LOG_ERROR("already specified hl_layout %s",
250                                 hl_if.layout->name);
251                 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
252                     ? ERROR_FAIL : ERROR_OK;
253         }
254
255         for (const struct hl_layout *l = hl_layout_get_list(); l->name;
256              l++) {
257                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
258                         hl_if.layout = l;
259                         return ERROR_OK;
260                 }
261         }
262
263         LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
264         return ERROR_FAIL;
265 }
266
267 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
268 {
269         if (CMD_ARGC > HLA_MAX_USB_IDS * 2) {
270                 LOG_WARNING("ignoring extra IDs in hla_vid_pid "
271                         "(maximum is %d pairs)", HLA_MAX_USB_IDS);
272                 CMD_ARGC = HLA_MAX_USB_IDS * 2;
273         }
274         if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
275                 LOG_WARNING("incomplete hla_vid_pid configuration directive");
276                 return ERROR_COMMAND_SYNTAX_ERROR;
277         }
278
279         unsigned i;
280         for (i = 0; i < CMD_ARGC; i += 2) {
281                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], hl_if.param.vid[i / 2]);
282                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], hl_if.param.pid[i / 2]);
283         }
284
285         /*
286          * Explicitly terminate, in case there are multiple instances of
287          * hla_vid_pid.
288          */
289         hl_if.param.vid[i / 2] = hl_if.param.pid[i / 2] = 0;
290
291         return ERROR_OK;
292 }
293
294 COMMAND_HANDLER(hl_interface_handle_stlink_backend_command)
295 {
296         /* default values */
297         bool use_stlink_tcp = false;
298         uint16_t stlink_tcp_port = 7184;
299
300         if (CMD_ARGC == 0 || CMD_ARGC > 2)
301                 return ERROR_COMMAND_SYNTAX_ERROR;
302         else if (strcmp(CMD_ARGV[0], "usb") == 0) {
303                 if (CMD_ARGC > 1)
304                         return ERROR_COMMAND_SYNTAX_ERROR;
305                 /* else use_stlink_tcp = false (already the case ) */
306         } else if (strcmp(CMD_ARGV[0], "tcp") == 0) {
307                 use_stlink_tcp = true;
308                 if (CMD_ARGC == 2)
309                         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_tcp_port);
310         } else
311                 return ERROR_COMMAND_SYNTAX_ERROR;
312
313         hl_if.param.use_stlink_tcp = use_stlink_tcp;
314         hl_if.param.stlink_tcp_port = stlink_tcp_port;
315
316         return ERROR_OK;
317 }
318
319 COMMAND_HANDLER(interface_handle_hla_command)
320 {
321         if (CMD_ARGC != 1)
322                 return ERROR_COMMAND_SYNTAX_ERROR;
323
324         if (!hl_if.layout->api->custom_command) {
325                 LOG_ERROR("The selected adapter doesn't support custom commands");
326                 return ERROR_FAIL;
327         }
328
329         hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
330
331         return ERROR_OK;
332 }
333
334 static const struct command_registration hl_interface_command_handlers[] = {
335         {
336          .name = "hla_device_desc",
337          .handler = &hl_interface_handle_device_desc_command,
338          .mode = COMMAND_CONFIG,
339          .help = "set the device description of the adapter",
340          .usage = "description_string",
341          },
342         {
343          .name = "hla_layout",
344          .handler = &hl_interface_handle_layout_command,
345          .mode = COMMAND_CONFIG,
346          .help = "set the layout of the adapter",
347          .usage = "layout_name",
348          },
349         {
350          .name = "hla_vid_pid",
351          .handler = &hl_interface_handle_vid_pid_command,
352          .mode = COMMAND_CONFIG,
353          .help = "the vendor and product ID of the adapter",
354          .usage = "(vid pid)*",
355          },
356         {
357          .name = "hla_stlink_backend",
358          .handler = &hl_interface_handle_stlink_backend_command,
359          .mode = COMMAND_CONFIG,
360          .help = "select which ST-Link backend to use",
361          .usage = "usb | tcp [port]",
362         },
363          {
364          .name = "hla_command",
365          .handler = &interface_handle_hla_command,
366          .mode = COMMAND_EXEC,
367          .help = "execute a custom adapter-specific command",
368          .usage = "<command>",
369          },
370         COMMAND_REGISTRATION_DONE
371 };
372
373 struct adapter_driver hl_adapter_driver = {
374         .name = "hla",
375         .transports = hl_transports,
376         .commands = hl_interface_command_handlers,
377
378         .init = hl_interface_init,
379         .quit = hl_interface_quit,
380         .reset = hl_interface_reset,
381         .speed = &hl_interface_speed,
382         .khz = &hl_interface_khz,
383         .speed_div = &hl_interface_speed_div,
384         .config_trace = &hl_interface_config_trace,
385         .poll_trace = &hl_interface_poll_trace,
386
387         /* no ops for HLA, targets hla_target and stm8 intercept them all */
388 };