adiv6: add dap flags -adiv5 and -adiv6
[openocd.git] / src / target / arm_dap.c
1 /***************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * *
18 ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include "target/arm_adi_v5.h"
27 #include "target/arm.h"
28 #include "helper/list.h"
29 #include "helper/command.h"
30 #include "transport/transport.h"
31 #include "jtag/interface.h"
32
33 static LIST_HEAD(all_dap);
34
35 extern const struct dap_ops swd_dap_ops;
36 extern const struct dap_ops jtag_dp_ops;
37 extern struct adapter_driver *adapter_driver;
38
39 /* DAP command support */
40 struct arm_dap_object {
41 struct list_head lh;
42 struct adiv5_dap dap;
43 char *name;
44 const struct swd_driver *swd;
45 };
46
47 static void dap_instance_init(struct adiv5_dap *dap)
48 {
49 int i;
50 /* Set up with safe defaults */
51 for (i = 0; i <= DP_APSEL_MAX; i++) {
52 dap->ap[i].dap = dap;
53 dap->ap[i].ap_num = i;
54 /* memaccess_tck max is 255 */
55 dap->ap[i].memaccess_tck = 255;
56 /* Number of bits for tar autoincrement, impl. dep. at least 10 */
57 dap->ap[i].tar_autoincr_block = (1<<10);
58 /* default CSW value */
59 dap->ap[i].csw_default = CSW_AHB_DEFAULT;
60 dap->ap[i].cfg_reg = MEM_AP_REG_CFG_INVALID; /* mem_ap configuration reg (large physical addr, etc.) */
61 dap->ap[i].refcount = 0;
62 dap->ap[i].config_ap_never_release = false;
63 }
64 INIT_LIST_HEAD(&dap->cmd_journal);
65 INIT_LIST_HEAD(&dap->cmd_pool);
66 }
67
68 const char *adiv5_dap_name(struct adiv5_dap *self)
69 {
70 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
71 return obj->name;
72 }
73
74 const struct swd_driver *adiv5_dap_swd_driver(struct adiv5_dap *self)
75 {
76 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
77 return obj->swd;
78 }
79
80 struct adiv5_dap *adiv5_get_dap(struct arm_dap_object *obj)
81 {
82 return &obj->dap;
83 }
84 struct adiv5_dap *dap_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
85 {
86 struct arm_dap_object *obj = NULL;
87 const char *name;
88 bool found = false;
89
90 name = Jim_GetString(o, NULL);
91
92 list_for_each_entry(obj, &all_dap, lh) {
93 if (!strcmp(name, obj->name)) {
94 found = true;
95 break;
96 }
97 }
98
99 if (found)
100 return &obj->dap;
101 return NULL;
102 }
103
104 static int dap_init_all(void)
105 {
106 struct arm_dap_object *obj;
107 int retval;
108
109 LOG_DEBUG("Initializing all DAPs ...");
110
111 list_for_each_entry(obj, &all_dap, lh) {
112 struct adiv5_dap *dap = &obj->dap;
113
114 /* with hla, dap is just a dummy */
115 if (transport_is_hla())
116 continue;
117
118 /* skip taps that are disabled */
119 if (!dap->tap->enabled)
120 continue;
121
122 if (transport_is_swd()) {
123 dap->ops = &swd_dap_ops;
124 obj->swd = adapter_driver->swd_ops;
125 } else if (transport_is_dapdirect_swd()) {
126 dap->ops = adapter_driver->dap_swd_ops;
127 } else if (transport_is_dapdirect_jtag()) {
128 dap->ops = adapter_driver->dap_jtag_ops;
129 } else
130 dap->ops = &jtag_dp_ops;
131
132 if (dap->adi_version == 0) {
133 LOG_DEBUG("DAP %s configured by default to use ADIv5 protocol", jtag_tap_name(dap->tap));
134 dap->adi_version = 5;
135 } else {
136 LOG_DEBUG("DAP %s configured to use %s protocol by user cfg file", jtag_tap_name(dap->tap),
137 is_adiv6(dap) ? "ADIv6" : "ADIv5");
138 }
139
140 retval = dap->ops->connect(dap);
141 if (retval != ERROR_OK)
142 return retval;
143 }
144
145 return ERROR_OK;
146 }
147
148 int dap_cleanup_all(void)
149 {
150 struct arm_dap_object *obj, *tmp;
151 struct adiv5_dap *dap;
152
153 list_for_each_entry_safe(obj, tmp, &all_dap, lh) {
154 dap = &obj->dap;
155 for (unsigned int i = 0; i <= DP_APSEL_MAX; i++) {
156 if (dap->ap[i].refcount != 0)
157 LOG_ERROR("BUG: refcount AP#%u still %u at exit", i, dap->ap[i].refcount);
158 }
159 if (dap->ops && dap->ops->quit)
160 dap->ops->quit(dap);
161
162 free(obj->name);
163 free(obj);
164 }
165
166 return ERROR_OK;
167 }
168
169 enum dap_cfg_param {
170 CFG_CHAIN_POSITION,
171 CFG_IGNORE_SYSPWRUPACK,
172 CFG_DP_ID,
173 CFG_INSTANCE_ID,
174 CFG_ADIV6,
175 CFG_ADIV5,
176 };
177
178 static const struct jim_nvp nvp_config_opts[] = {
179 { .name = "-chain-position", .value = CFG_CHAIN_POSITION },
180 { .name = "-ignore-syspwrupack", .value = CFG_IGNORE_SYSPWRUPACK },
181 { .name = "-dp-id", .value = CFG_DP_ID },
182 { .name = "-instance-id", .value = CFG_INSTANCE_ID },
183 { .name = "-adiv6", .value = CFG_ADIV6 },
184 { .name = "-adiv5", .value = CFG_ADIV5 },
185 { .name = NULL, .value = -1 }
186 };
187
188 static int dap_configure(struct jim_getopt_info *goi, struct arm_dap_object *dap)
189 {
190 struct jim_nvp *n;
191 int e;
192
193 /* parse config ... */
194 while (goi->argc > 0) {
195 Jim_SetEmptyResult(goi->interp);
196
197 e = jim_getopt_nvp(goi, nvp_config_opts, &n);
198 if (e != JIM_OK) {
199 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
200 return e;
201 }
202 switch (n->value) {
203 case CFG_CHAIN_POSITION: {
204 Jim_Obj *o_t;
205 e = jim_getopt_obj(goi, &o_t);
206 if (e != JIM_OK)
207 return e;
208
209 struct jtag_tap *tap;
210 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
211 if (!tap) {
212 Jim_SetResultString(goi->interp, "-chain-position is invalid", -1);
213 return JIM_ERR;
214 }
215 dap->dap.tap = tap;
216 /* loop for more */
217 break;
218 }
219 case CFG_IGNORE_SYSPWRUPACK:
220 dap->dap.ignore_syspwrupack = true;
221 break;
222 case CFG_DP_ID: {
223 jim_wide w;
224 e = jim_getopt_wide(goi, &w);
225 if (e != JIM_OK) {
226 Jim_SetResultFormatted(goi->interp,
227 "create %s: bad parameter %s",
228 dap->name, n->name);
229 return JIM_ERR;
230 }
231 if (w < 0 || w > DP_TARGETSEL_DPID_MASK) {
232 Jim_SetResultFormatted(goi->interp,
233 "create %s: %s out of range",
234 dap->name, n->name);
235 return JIM_ERR;
236 }
237 dap->dap.multidrop_targetsel =
238 (dap->dap.multidrop_targetsel & DP_TARGETSEL_INSTANCEID_MASK)
239 | (w & DP_TARGETSEL_DPID_MASK);
240 dap->dap.multidrop_dp_id_valid = true;
241 break;
242 }
243 case CFG_INSTANCE_ID: {
244 jim_wide w;
245 e = jim_getopt_wide(goi, &w);
246 if (e != JIM_OK) {
247 Jim_SetResultFormatted(goi->interp,
248 "create %s: bad parameter %s",
249 dap->name, n->name);
250 return JIM_ERR;
251 }
252 if (w < 0 || w > 15) {
253 Jim_SetResultFormatted(goi->interp,
254 "create %s: %s out of range",
255 dap->name, n->name);
256 return JIM_ERR;
257 }
258 dap->dap.multidrop_targetsel =
259 (dap->dap.multidrop_targetsel & DP_TARGETSEL_DPID_MASK)
260 | ((w << DP_TARGETSEL_INSTANCEID_SHIFT) & DP_TARGETSEL_INSTANCEID_MASK);
261 dap->dap.multidrop_instance_id_valid = true;
262 break;
263 }
264 case CFG_ADIV6:
265 dap->dap.adi_version = 6;
266 break;
267 case CFG_ADIV5:
268 dap->dap.adi_version = 5;
269 break;
270 default:
271 break;
272 }
273 }
274
275 return JIM_OK;
276 }
277
278 static int dap_check_config(struct adiv5_dap *dap)
279 {
280 if (transport_is_jtag() || transport_is_dapdirect_jtag() || transport_is_hla())
281 return ERROR_OK;
282
283 struct arm_dap_object *obj;
284 bool new_multidrop = dap_is_multidrop(dap);
285 bool had_multidrop = new_multidrop;
286 uint32_t targetsel = dap->multidrop_targetsel;
287 unsigned int non_multidrop_count = had_multidrop ? 0 : 1;
288
289 list_for_each_entry(obj, &all_dap, lh) {
290 struct adiv5_dap *dap_it = &obj->dap;
291
292 if (transport_is_swd()) {
293 if (dap_is_multidrop(dap_it)) {
294 had_multidrop = true;
295 if (new_multidrop && dap_it->multidrop_targetsel == targetsel) {
296 uint32_t dp_id = targetsel & DP_TARGETSEL_DPID_MASK;
297 uint32_t instance_id = targetsel >> DP_TARGETSEL_INSTANCEID_SHIFT;
298 LOG_ERROR("%s and %s have the same multidrop selectors -dp-id 0x%08"
299 PRIx32 " and -instance-id 0x%" PRIx32,
300 obj->name, adiv5_dap_name(dap),
301 dp_id, instance_id);
302 return ERROR_FAIL;
303 }
304 } else {
305 non_multidrop_count++;
306 }
307 } else if (transport_is_dapdirect_swd()) {
308 non_multidrop_count++;
309 }
310 }
311
312 if (non_multidrop_count > 1) {
313 LOG_ERROR("Two or more SWD non multidrop DAPs are not supported");
314 return ERROR_FAIL;
315 }
316 if (had_multidrop && non_multidrop_count) {
317 LOG_ERROR("Mixing of SWD multidrop DAPs and non multidrop DAPs is not supported");
318 return ERROR_FAIL;
319 }
320
321 return ERROR_OK;
322 }
323
324 static int dap_create(struct jim_getopt_info *goi)
325 {
326 struct command_context *cmd_ctx;
327 static struct arm_dap_object *dap;
328 Jim_Obj *new_cmd;
329 Jim_Cmd *cmd;
330 const char *cp;
331 int e;
332
333 cmd_ctx = current_command_context(goi->interp);
334 assert(cmd_ctx);
335
336 if (goi->argc < 3) {
337 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
338 return JIM_ERR;
339 }
340 /* COMMAND */
341 jim_getopt_obj(goi, &new_cmd);
342 /* does this command exist? */
343 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_NONE);
344 if (cmd) {
345 cp = Jim_GetString(new_cmd, NULL);
346 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
347 return JIM_ERR;
348 }
349
350 /* Create it */
351 dap = calloc(1, sizeof(struct arm_dap_object));
352 if (!dap)
353 return JIM_ERR;
354
355 dap_instance_init(&dap->dap);
356
357 cp = Jim_GetString(new_cmd, NULL);
358 dap->name = strdup(cp);
359
360 e = dap_configure(goi, dap);
361 if (e != JIM_OK)
362 goto err;
363
364 if (!dap->dap.tap) {
365 Jim_SetResultString(goi->interp, "-chain-position required when creating DAP", -1);
366 e = JIM_ERR;
367 goto err;
368 }
369
370 e = dap_check_config(&dap->dap);
371 if (e != ERROR_OK) {
372 e = JIM_ERR;
373 goto err;
374 }
375
376 struct command_registration dap_create_commands[] = {
377 {
378 .name = cp,
379 .mode = COMMAND_ANY,
380 .help = "dap instance command group",
381 .usage = "",
382 .chain = dap_instance_commands,
383 },
384 COMMAND_REGISTRATION_DONE
385 };
386
387 /* don't expose the instance commands when using hla */
388 if (transport_is_hla())
389 dap_create_commands[0].chain = NULL;
390
391 e = register_commands_with_data(cmd_ctx, NULL, dap_create_commands, dap);
392 if (e != ERROR_OK) {
393 e = JIM_ERR;
394 goto err;
395 }
396
397 list_add_tail(&dap->lh, &all_dap);
398
399 return JIM_OK;
400
401 err:
402 free(dap->name);
403 free(dap);
404 return e;
405 }
406
407 static int jim_dap_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
408 {
409 struct jim_getopt_info goi;
410 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
411 if (goi.argc < 2) {
412 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
413 "<name> [<dap_options> ...]");
414 return JIM_ERR;
415 }
416 return dap_create(&goi);
417 }
418
419 static int jim_dap_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
420 {
421 struct arm_dap_object *obj;
422
423 if (argc != 1) {
424 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
425 return JIM_ERR;
426 }
427 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
428 list_for_each_entry(obj, &all_dap, lh) {
429 Jim_ListAppendElement(interp, Jim_GetResult(interp),
430 Jim_NewStringObj(interp, obj->name, -1));
431 }
432 return JIM_OK;
433 }
434
435 COMMAND_HANDLER(handle_dap_init)
436 {
437 return dap_init_all();
438 }
439
440 COMMAND_HANDLER(handle_dap_info_command)
441 {
442 struct target *target = get_current_target(CMD_CTX);
443 struct arm *arm = target_to_arm(target);
444 struct adiv5_dap *dap = arm->dap;
445 uint32_t apsel;
446
447 if (!dap) {
448 LOG_ERROR("DAP instance not available. Probably a HLA target...");
449 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
450 }
451
452 switch (CMD_ARGC) {
453 case 0:
454 apsel = dap->apsel;
455 break;
456 case 1:
457 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
458 if (apsel > DP_APSEL_MAX)
459 return ERROR_COMMAND_SYNTAX_ERROR;
460 break;
461 default:
462 return ERROR_COMMAND_SYNTAX_ERROR;
463 }
464
465 struct adiv5_ap *ap = dap_get_ap(dap, apsel);
466 if (!ap) {
467 command_print(CMD, "Cannot get AP");
468 return ERROR_FAIL;
469 }
470 int retval = dap_info_command(CMD, ap);
471 dap_put_ap(ap);
472 return retval;
473 }
474
475 static const struct command_registration dap_subcommand_handlers[] = {
476 {
477 .name = "create",
478 .mode = COMMAND_ANY,
479 .jim_handler = jim_dap_create,
480 .usage = "name '-chain-position' name",
481 .help = "Creates a new DAP instance",
482 },
483 {
484 .name = "names",
485 .mode = COMMAND_ANY,
486 .jim_handler = jim_dap_names,
487 .usage = "",
488 .help = "Lists all registered DAP instances by name",
489 },
490 {
491 .name = "init",
492 .mode = COMMAND_ANY,
493 .handler = handle_dap_init,
494 .usage = "",
495 .help = "Initialize all registered DAP instances"
496 },
497 {
498 .name = "info",
499 .handler = handle_dap_info_command,
500 .mode = COMMAND_EXEC,
501 .help = "display ROM table for MEM-AP of current target "
502 "(default currently selected AP)",
503 .usage = "[ap_num]",
504 },
505 COMMAND_REGISTRATION_DONE
506 };
507
508 static const struct command_registration dap_commands[] = {
509 {
510 .name = "dap",
511 .mode = COMMAND_CONFIG,
512 .help = "DAP commands",
513 .chain = dap_subcommand_handlers,
514 .usage = "",
515 },
516 COMMAND_REGISTRATION_DONE
517 };
518
519 int dap_register_commands(struct command_context *cmd_ctx)
520 {
521 return register_commands(cmd_ctx, NULL, dap_commands);
522 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)