]> git.gag.com Git - fw/openocd/blob - src/jtag/hla/hla_interface.c
44a7fc69f8917b821a0ed5a3a10beb1d6a637b88
[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, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
22  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
32
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>
37
38 #include <target/target.h>
39
40 static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, false, NULL, 0}, 0, 0 };
41
42 int hl_interface_open(enum hl_transports tr)
43 {
44         LOG_DEBUG("hl_interface_open");
45
46         enum reset_types jtag_reset_config = jtag_get_reset_config();
47
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;
51                 else
52                         LOG_WARNING("\'srst_nogate\' reset_config option is required");
53         }
54
55         /* set transport mode */
56         hl_if.param.transport = tr;
57
58         int result = hl_if.layout->open(&hl_if);
59         if (result != ERROR_OK)
60                 return result;
61
62         return hl_interface_init_reset();
63 }
64
65 int hl_interface_init_target(struct target *t)
66 {
67         int res;
68
69         LOG_DEBUG("hl_interface_init_target");
70
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
74          */
75         res = hl_if.layout->api->idcode(hl_if.handle, &t->tap->idcode);
76
77         if (res != ERROR_OK)
78                 return res;
79
80         unsigned ii, limit = t->tap->expected_ids_cnt;
81         int found = 0;
82
83         for (ii = 0; ii < limit; ii++) {
84                 uint32_t expected = t->tap->expected_ids[ii];
85
86                 /* treat "-expected-id 0" as a "don't-warn" wildcard */
87                 if (!expected || !t->tap->idcode ||
88                     (t->tap->idcode == expected)) {
89                         found = 1;
90                         break;
91                 }
92         }
93
94         if (found == 0) {
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]);
99
100                 return ERROR_FAIL;
101         }
102
103         t->tap->priv = &hl_if;
104         t->tap->hasidcode = 1;
105
106         return ERROR_OK;
107 }
108
109 static int hl_interface_init(void)
110 {
111         LOG_DEBUG("hl_interface_init");
112
113         /* here we can initialize the layout */
114         return hl_layout_init(&hl_if);
115 }
116
117 static int hl_interface_quit(void)
118 {
119         LOG_DEBUG("hl_interface_quit");
120
121         if (hl_if.param.trace_f) {
122                 fclose(hl_if.param.trace_f);
123                 hl_if.param.trace_f = NULL;
124         }
125         hl_if.param.trace_source_hz = 0;
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 static int dummy_khz(int khz, int *jtag_speed)
152 {
153         *jtag_speed = khz;
154         return ERROR_OK;
155 }
156
157 static int dummy_speed_div(int speed, int *khz)
158 {
159         *khz = speed;
160         return ERROR_OK;
161 }
162
163 static int dummy_speed(int speed)
164 {
165         return ERROR_OK;
166 }
167
168 int hl_interface_override_target(const char **targetname)
169 {
170         if (hl_if.layout->api->override_target) {
171                 if (hl_if.layout->api->override_target(*targetname)) {
172                         *targetname = "hla_target";
173                         return ERROR_OK;
174                 } else
175                         return ERROR_FAIL;
176         }
177         return ERROR_FAIL;
178 }
179
180 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
181 {
182         LOG_DEBUG("hl_interface_handle_device_desc_command");
183
184         if (CMD_ARGC == 1) {
185                 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
186         } else {
187                 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
188         }
189
190         return ERROR_OK;
191 }
192
193 COMMAND_HANDLER(hl_interface_handle_serial_command)
194 {
195         LOG_DEBUG("hl_interface_handle_serial_command");
196
197         if (CMD_ARGC == 1) {
198                 hl_if.param.serial = strdup(CMD_ARGV[0]);
199         } else {
200                 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
201         }
202
203         return ERROR_OK;
204 }
205
206 COMMAND_HANDLER(hl_interface_handle_layout_command)
207 {
208         LOG_DEBUG("hl_interface_handle_layout_command");
209
210         if (CMD_ARGC != 1) {
211                 LOG_ERROR("Need exactly one argument to stlink_layout");
212                 return ERROR_COMMAND_SYNTAX_ERROR;
213         }
214
215         if (hl_if.layout) {
216                 LOG_ERROR("already specified hl_layout %s",
217                                 hl_if.layout->name);
218                 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
219                     ? ERROR_FAIL : ERROR_OK;
220         }
221
222         for (const struct hl_layout *l = hl_layout_get_list(); l->name;
223              l++) {
224                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
225                         hl_if.layout = l;
226                         return ERROR_OK;
227                 }
228         }
229
230         LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
231         return ERROR_FAIL;
232 }
233
234 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
235 {
236         LOG_DEBUG("hl_interface_handle_vid_pid_command");
237
238         if (CMD_ARGC != 2) {
239                 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
240                 return ERROR_COMMAND_SYNTAX_ERROR;
241         }
242
243         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], hl_if.param.vid);
244         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], hl_if.param.pid);
245
246         return ERROR_OK;
247 }
248
249 COMMAND_HANDLER(interface_handle_trace_command)
250 {
251         FILE *f = NULL;
252         unsigned source_hz;
253
254         if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
255                 return ERROR_COMMAND_SYNTAX_ERROR;
256
257         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], source_hz);
258         if (source_hz == 0) {
259                 return ERROR_COMMAND_SYNTAX_ERROR;
260         }
261
262         if (CMD_ARGC == 2) {
263                 f = fopen(CMD_ARGV[1], "a");
264                 if (!f)
265                         return ERROR_COMMAND_SYNTAX_ERROR;
266         }
267
268         hl_if.param.trace_f = f;
269         hl_if.param.trace_source_hz = source_hz;
270
271         return ERROR_OK;
272 }
273
274 COMMAND_HANDLER(interface_handle_hla_command)
275 {
276         if (CMD_ARGC != 1)
277                 return ERROR_COMMAND_SYNTAX_ERROR;
278
279         if (!hl_if.layout->api->custom_command) {
280                 LOG_ERROR("The selected adapter doesn't support custom commands");
281                 return ERROR_FAIL;
282         }
283
284         hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
285
286         return ERROR_OK;
287 }
288
289 static const struct command_registration hl_interface_command_handlers[] = {
290         {
291          .name = "hla_device_desc",
292          .handler = &hl_interface_handle_device_desc_command,
293          .mode = COMMAND_CONFIG,
294          .help = "set the a device description of the adapter",
295          .usage = "description_string",
296          },
297         {
298          .name = "hla_serial",
299          .handler = &hl_interface_handle_serial_command,
300          .mode = COMMAND_CONFIG,
301          .help = "set the serial number of the adapter",
302          .usage = "serial_string",
303          },
304         {
305          .name = "hla_layout",
306          .handler = &hl_interface_handle_layout_command,
307          .mode = COMMAND_CONFIG,
308          .help = "set the layout of the adapter",
309          .usage = "layout_name",
310          },
311         {
312          .name = "hla_vid_pid",
313          .handler = &hl_interface_handle_vid_pid_command,
314          .mode = COMMAND_CONFIG,
315          .help = "the vendor and product ID of the adapter",
316          .usage = "(vid pid)* ",
317          },
318          {
319          .name = "trace",
320          .handler = &interface_handle_trace_command,
321          .mode = COMMAND_CONFIG,
322          .help = "configure trace reception",
323          .usage = "source_lock_hz [destination_path]",
324          },
325          {
326          .name = "hla_command",
327          .handler = &interface_handle_hla_command,
328          .mode = COMMAND_EXEC,
329          .help = "execute a custom adapter-specific command",
330          .usage = "hla_command <command>",
331          },
332         COMMAND_REGISTRATION_DONE
333 };
334
335 struct jtag_interface hl_interface = {
336         .name = "hla",
337         .supported = 0,
338         .commands = hl_interface_command_handlers,
339         .transports = hl_transports,
340         .init = hl_interface_init,
341         .quit = hl_interface_quit,
342         .execute_queue = hl_interface_execute_queue,
343         .speed = &dummy_speed,
344         .khz = &dummy_khz,
345         .speed_div = &dummy_speed_div,
346 };