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