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;
61 INIT_LIST_HEAD(&dap->cmd_journal);
62 INIT_LIST_HEAD(&dap->cmd_pool);
65 const char *adiv5_dap_name(struct adiv5_dap *self)
67 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
71 const struct swd_driver *adiv5_dap_swd_driver(struct adiv5_dap *self)
73 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
77 struct adiv5_dap *adiv5_get_dap(struct arm_dap_object *obj)
81 struct adiv5_dap *dap_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
83 struct arm_dap_object *obj = NULL;
87 name = Jim_GetString(o, NULL);
89 list_for_each_entry(obj, &all_dap, lh) {
90 if (!strcmp(name, obj->name)) {
101 static int dap_init_all(void)
103 struct arm_dap_object *obj;
106 LOG_DEBUG("Initializing all DAPs ...");
108 list_for_each_entry(obj, &all_dap, lh) {
109 struct adiv5_dap *dap = &obj->dap;
111 /* with hla, dap is just a dummy */
112 if (transport_is_hla())
115 /* skip taps that are disabled */
116 if (!dap->tap->enabled)
119 if (transport_is_swd()) {
120 dap->ops = &swd_dap_ops;
121 obj->swd = adapter_driver->swd_ops;
122 } else if (transport_is_dapdirect_swd()) {
123 dap->ops = adapter_driver->dap_swd_ops;
124 } else if (transport_is_dapdirect_jtag()) {
125 dap->ops = adapter_driver->dap_jtag_ops;
127 dap->ops = &jtag_dp_ops;
129 retval = dap->ops->connect(dap);
130 if (retval != ERROR_OK)
137 int dap_cleanup_all(void)
139 struct arm_dap_object *obj, *tmp;
140 struct adiv5_dap *dap;
142 list_for_each_entry_safe(obj, tmp, &all_dap, lh) {
144 if (dap->ops && dap->ops->quit)
156 CFG_IGNORE_SYSPWRUPACK,
159 static const struct jim_nvp nvp_config_opts[] = {
160 { .name = "-chain-position", .value = CFG_CHAIN_POSITION },
161 { .name = "-ignore-syspwrupack", .value = CFG_IGNORE_SYSPWRUPACK },
162 { .name = NULL, .value = -1 }
165 static int dap_configure(struct jim_getopt_info *goi, struct arm_dap_object *dap)
167 struct jtag_tap *tap = NULL;
171 /* parse config or cget options ... */
172 while (goi->argc > 0) {
173 Jim_SetEmptyResult(goi->interp);
175 e = jim_getopt_nvp(goi, nvp_config_opts, &n);
177 jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
181 case CFG_CHAIN_POSITION: {
183 e = jim_getopt_obj(goi, &o_t);
186 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
188 Jim_SetResultString(goi->interp, "-chain-position is invalid", -1);
194 case CFG_IGNORE_SYSPWRUPACK:
195 dap->dap.ignore_syspwrupack = true;
203 Jim_SetResultString(goi->interp, "-chain-position required when creating DAP", -1);
207 dap_instance_init(&dap->dap);
213 static int dap_create(struct jim_getopt_info *goi)
215 struct command_context *cmd_ctx;
216 static struct arm_dap_object *dap;
222 cmd_ctx = current_command_context(goi->interp);
223 assert(cmd_ctx != NULL);
226 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
230 jim_getopt_obj(goi, &new_cmd);
231 /* does this command exist? */
232 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
234 cp = Jim_GetString(new_cmd, NULL);
235 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
240 dap = calloc(1, sizeof(struct arm_dap_object));
244 e = dap_configure(goi, dap);
250 cp = Jim_GetString(new_cmd, NULL);
251 dap->name = strdup(cp);
253 struct command_registration dap_commands[] = {
257 .help = "dap instance command group",
259 .chain = dap_instance_commands,
261 COMMAND_REGISTRATION_DONE
264 /* don't expose the instance commands when using hla */
265 if (transport_is_hla())
266 dap_commands[0].chain = NULL;
268 e = register_commands_with_data(cmd_ctx, NULL, dap_commands, dap);
272 list_add_tail(&dap->lh, &all_dap);
277 static int jim_dap_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
279 struct jim_getopt_info goi;
280 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
282 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
283 "<name> [<dap_options> ...]");
286 return dap_create(&goi);
289 static int jim_dap_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
291 struct arm_dap_object *obj;
294 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
297 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
298 list_for_each_entry(obj, &all_dap, lh) {
299 Jim_ListAppendElement(interp, Jim_GetResult(interp),
300 Jim_NewStringObj(interp, obj->name, -1));
305 COMMAND_HANDLER(handle_dap_init)
307 return dap_init_all();
310 COMMAND_HANDLER(handle_dap_info_command)
312 struct target *target = get_current_target(CMD_CTX);
313 struct arm *arm = target_to_arm(target);
314 struct adiv5_dap *dap = arm->dap;
318 LOG_ERROR("DAP instance not available. Probably a HLA target...");
319 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
327 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
328 if (apsel > DP_APSEL_MAX)
329 return ERROR_COMMAND_SYNTAX_ERROR;
332 return ERROR_COMMAND_SYNTAX_ERROR;
335 return dap_info_command(CMD, &dap->ap[apsel]);
338 static const struct command_registration dap_subcommand_handlers[] = {
342 .jim_handler = jim_dap_create,
343 .usage = "name '-chain-position' name",
344 .help = "Creates a new DAP instance",
349 .jim_handler = jim_dap_names,
351 .help = "Lists all registered DAP instances by name",
356 .handler = handle_dap_init,
358 .help = "Initialize all registered DAP instances"
362 .handler = handle_dap_info_command,
363 .mode = COMMAND_EXEC,
364 .help = "display ROM table for MEM-AP of current target "
365 "(default currently selected AP)",
368 COMMAND_REGISTRATION_DONE
371 static const struct command_registration dap_commands[] = {
374 .mode = COMMAND_CONFIG,
375 .help = "DAP commands",
376 .chain = dap_subcommand_handlers,
379 COMMAND_REGISTRATION_DONE
382 int dap_register_commands(struct command_context *cmd_ctx)
384 return register_commands(cmd_ctx, NULL, dap_commands);