1 /***************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky *
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. *
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. *
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., *
18 ***************************************************************************/
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"
33 static LIST_HEAD(all_dap);
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;
39 /* DAP command support */
40 struct arm_dap_object {
44 const struct swd_driver *swd;
47 static void dap_instance_init(struct adiv5_dap *dap)
50 /* Set up with safe defaults */
51 for (i = 0; i <= DP_APSEL_MAX; i++) {
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.) */
62 INIT_LIST_HEAD(&dap->cmd_journal);
63 INIT_LIST_HEAD(&dap->cmd_pool);
66 const char *adiv5_dap_name(struct adiv5_dap *self)
68 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
72 const struct swd_driver *adiv5_dap_swd_driver(struct adiv5_dap *self)
74 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
78 struct adiv5_dap *adiv5_get_dap(struct arm_dap_object *obj)
82 struct adiv5_dap *dap_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
84 struct arm_dap_object *obj = NULL;
88 name = Jim_GetString(o, NULL);
90 list_for_each_entry(obj, &all_dap, lh) {
91 if (!strcmp(name, obj->name)) {
102 static int dap_init_all(void)
104 struct arm_dap_object *obj;
107 LOG_DEBUG("Initializing all DAPs ...");
109 list_for_each_entry(obj, &all_dap, lh) {
110 struct adiv5_dap *dap = &obj->dap;
112 /* with hla, dap is just a dummy */
113 if (transport_is_hla())
116 /* skip taps that are disabled */
117 if (!dap->tap->enabled)
120 if (transport_is_swd()) {
121 dap->ops = &swd_dap_ops;
122 obj->swd = adapter_driver->swd_ops;
123 } else if (transport_is_dapdirect_swd()) {
124 dap->ops = adapter_driver->dap_swd_ops;
125 } else if (transport_is_dapdirect_jtag()) {
126 dap->ops = adapter_driver->dap_jtag_ops;
128 dap->ops = &jtag_dp_ops;
130 retval = dap->ops->connect(dap);
131 if (retval != ERROR_OK)
138 int dap_cleanup_all(void)
140 struct arm_dap_object *obj, *tmp;
141 struct adiv5_dap *dap;
143 list_for_each_entry_safe(obj, tmp, &all_dap, lh) {
145 if (dap->ops && dap->ops->quit)
157 CFG_IGNORE_SYSPWRUPACK,
162 static const struct jim_nvp nvp_config_opts[] = {
163 { .name = "-chain-position", .value = CFG_CHAIN_POSITION },
164 { .name = "-ignore-syspwrupack", .value = CFG_IGNORE_SYSPWRUPACK },
165 { .name = "-dp-id", .value = CFG_DP_ID },
166 { .name = "-instance-id", .value = CFG_INSTANCE_ID },
167 { .name = NULL, .value = -1 }
170 static int dap_configure(struct jim_getopt_info *goi, struct arm_dap_object *dap)
175 /* parse config ... */
176 while (goi->argc > 0) {
177 Jim_SetEmptyResult(goi->interp);
179 e = jim_getopt_nvp(goi, nvp_config_opts, &n);
181 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
185 case CFG_CHAIN_POSITION: {
187 e = jim_getopt_obj(goi, &o_t);
191 struct jtag_tap *tap;
192 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
194 Jim_SetResultString(goi->interp, "-chain-position is invalid", -1);
201 case CFG_IGNORE_SYSPWRUPACK:
202 dap->dap.ignore_syspwrupack = true;
206 e = jim_getopt_wide(goi, &w);
208 Jim_SetResultFormatted(goi->interp,
209 "create %s: bad parameter %s",
213 if (w < 0 || w > DP_TARGETSEL_DPID_MASK) {
214 Jim_SetResultFormatted(goi->interp,
215 "create %s: %s out of range",
219 dap->dap.multidrop_targetsel =
220 (dap->dap.multidrop_targetsel & DP_TARGETSEL_INSTANCEID_MASK)
221 | (w & DP_TARGETSEL_DPID_MASK);
222 dap->dap.multidrop_dp_id_valid = true;
225 case CFG_INSTANCE_ID: {
227 e = jim_getopt_wide(goi, &w);
229 Jim_SetResultFormatted(goi->interp,
230 "create %s: bad parameter %s",
234 if (w < 0 || w > 15) {
235 Jim_SetResultFormatted(goi->interp,
236 "create %s: %s out of range",
240 dap->dap.multidrop_targetsel =
241 (dap->dap.multidrop_targetsel & DP_TARGETSEL_DPID_MASK)
242 | ((w << DP_TARGETSEL_INSTANCEID_SHIFT) & DP_TARGETSEL_INSTANCEID_MASK);
243 dap->dap.multidrop_instance_id_valid = true;
254 static int dap_create(struct jim_getopt_info *goi)
256 struct command_context *cmd_ctx;
257 static struct arm_dap_object *dap;
263 cmd_ctx = current_command_context(goi->interp);
267 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
271 jim_getopt_obj(goi, &new_cmd);
272 /* does this command exist? */
273 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_NONE);
275 cp = Jim_GetString(new_cmd, NULL);
276 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
281 dap = calloc(1, sizeof(struct arm_dap_object));
285 dap_instance_init(&dap->dap);
287 cp = Jim_GetString(new_cmd, NULL);
288 dap->name = strdup(cp);
290 e = dap_configure(goi, dap);
295 Jim_SetResultString(goi->interp, "-chain-position required when creating DAP", -1);
300 struct command_registration dap_commands[] = {
304 .help = "dap instance command group",
306 .chain = dap_instance_commands,
308 COMMAND_REGISTRATION_DONE
311 /* don't expose the instance commands when using hla */
312 if (transport_is_hla())
313 dap_commands[0].chain = NULL;
315 e = register_commands_with_data(cmd_ctx, NULL, dap_commands, dap);
321 list_add_tail(&dap->lh, &all_dap);
331 static int jim_dap_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
333 struct jim_getopt_info goi;
334 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
336 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
337 "<name> [<dap_options> ...]");
340 return dap_create(&goi);
343 static int jim_dap_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
345 struct arm_dap_object *obj;
348 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
351 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
352 list_for_each_entry(obj, &all_dap, lh) {
353 Jim_ListAppendElement(interp, Jim_GetResult(interp),
354 Jim_NewStringObj(interp, obj->name, -1));
359 COMMAND_HANDLER(handle_dap_init)
361 return dap_init_all();
364 COMMAND_HANDLER(handle_dap_info_command)
366 struct target *target = get_current_target(CMD_CTX);
367 struct arm *arm = target_to_arm(target);
368 struct adiv5_dap *dap = arm->dap;
372 LOG_ERROR("DAP instance not available. Probably a HLA target...");
373 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
381 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
382 if (apsel > DP_APSEL_MAX)
383 return ERROR_COMMAND_SYNTAX_ERROR;
386 return ERROR_COMMAND_SYNTAX_ERROR;
389 return dap_info_command(CMD, &dap->ap[apsel]);
392 static const struct command_registration dap_subcommand_handlers[] = {
396 .jim_handler = jim_dap_create,
397 .usage = "name '-chain-position' name",
398 .help = "Creates a new DAP instance",
403 .jim_handler = jim_dap_names,
405 .help = "Lists all registered DAP instances by name",
410 .handler = handle_dap_init,
412 .help = "Initialize all registered DAP instances"
416 .handler = handle_dap_info_command,
417 .mode = COMMAND_EXEC,
418 .help = "display ROM table for MEM-AP of current target "
419 "(default currently selected AP)",
422 COMMAND_REGISTRATION_DONE
425 static const struct command_registration dap_commands[] = {
428 .mode = COMMAND_CONFIG,
429 .help = "DAP commands",
430 .chain = dap_subcommand_handlers,
433 COMMAND_REGISTRATION_DONE
436 int dap_register_commands(struct command_context *cmd_ctx)
438 return register_commands(cmd_ctx, NULL, dap_commands);