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 jtag_interface *jtag_interface;
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 <= 255; 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_DEFAULT;
61 INIT_LIST_HEAD(&dap->cmd_journal);
64 const char *adiv5_dap_name(struct adiv5_dap *self)
66 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
70 const struct swd_driver *adiv5_dap_swd_driver(struct adiv5_dap *self)
72 struct arm_dap_object *obj = container_of(self, struct arm_dap_object, dap);
76 struct adiv5_dap *adiv5_get_dap(struct arm_dap_object *obj)
80 struct adiv5_dap *dap_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
82 struct arm_dap_object *obj = NULL;
86 name = Jim_GetString(o, NULL);
88 list_for_each_entry(obj, &all_dap, lh) {
89 if (!strcmp(name, obj->name)) {
100 static int dap_init_all(void)
102 struct arm_dap_object *obj;
105 LOG_DEBUG("Initializing all DAPs ...");
107 list_for_each_entry(obj, &all_dap, lh) {
108 struct adiv5_dap *dap = &obj->dap;
110 /* with hla, dap is just a dummy */
111 if (transport_is_hla())
114 /* skip taps that are disabled */
115 if (!dap->tap->enabled)
118 if (transport_is_swd()) {
119 dap->ops = &swd_dap_ops;
120 obj->swd = jtag_interface->swd;
122 dap->ops = &jtag_dp_ops;
124 retval = dap->ops->connect(dap);
125 if (retval != ERROR_OK)
132 int dap_cleanup_all(void)
134 struct arm_dap_object *obj, *tmp;
135 struct adiv5_dap *dap;
137 list_for_each_entry_safe(obj, tmp, &all_dap, lh) {
139 if (dap->ops && dap->ops->quit)
151 CFG_IGNORE_SYSPWRUPACK,
154 static const Jim_Nvp nvp_config_opts[] = {
155 { .name = "-chain-position", .value = CFG_CHAIN_POSITION },
156 { .name = "-ignore-syspwrupack", .value = CFG_IGNORE_SYSPWRUPACK },
157 { .name = NULL, .value = -1 }
160 static int dap_configure(Jim_GetOptInfo *goi, struct arm_dap_object *dap)
162 struct jtag_tap *tap = NULL;
166 /* parse config or cget options ... */
167 while (goi->argc > 0) {
168 Jim_SetEmptyResult(goi->interp);
170 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
172 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
176 case CFG_CHAIN_POSITION: {
178 e = Jim_GetOpt_Obj(goi, &o_t);
181 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
183 Jim_SetResultString(goi->interp, "-chain-position is invalid", -1);
189 case CFG_IGNORE_SYSPWRUPACK:
190 dap->dap.ignore_syspwrupack = true;
198 Jim_SetResultString(goi->interp, "-chain-position required when creating DAP", -1);
202 dap_instance_init(&dap->dap);
208 static int dap_create(Jim_GetOptInfo *goi)
210 struct command_context *cmd_ctx;
211 static struct arm_dap_object *dap;
217 cmd_ctx = current_command_context(goi->interp);
218 assert(cmd_ctx != NULL);
221 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
225 Jim_GetOpt_Obj(goi, &new_cmd);
226 /* does this command exist? */
227 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
229 cp = Jim_GetString(new_cmd, NULL);
230 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
235 dap = calloc(1, sizeof(struct arm_dap_object));
239 e = dap_configure(goi, dap);
245 cp = Jim_GetString(new_cmd, NULL);
246 dap->name = strdup(cp);
248 struct command_registration dap_commands[] = {
252 .help = "dap instance command group",
254 .chain = dap_instance_commands,
256 COMMAND_REGISTRATION_DONE
259 /* don't expose the instance commands when using hla */
260 if (transport_is_hla())
261 dap_commands[0].chain = NULL;
263 e = register_commands(cmd_ctx, NULL, dap_commands);
267 struct command *c = command_find_in_context(cmd_ctx, cp);
269 command_set_handler_data(c, dap);
271 list_add_tail(&dap->lh, &all_dap);
273 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
276 static int jim_dap_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
279 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
281 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
282 "<name> [<dap_options> ...]");
285 return dap_create(&goi);
288 static int jim_dap_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
290 struct arm_dap_object *obj;
293 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
296 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
297 list_for_each_entry(obj, &all_dap, lh) {
298 Jim_ListAppendElement(interp, Jim_GetResult(interp),
299 Jim_NewStringObj(interp, obj->name, -1));
304 COMMAND_HANDLER(handle_dap_init)
306 return dap_init_all();
309 COMMAND_HANDLER(handle_dap_info_command)
311 struct target *target = get_current_target(CMD_CTX);
312 struct arm *arm = target_to_arm(target);
313 struct adiv5_dap *dap = arm->dap;
321 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
323 return ERROR_COMMAND_SYNTAX_ERROR;
326 return ERROR_COMMAND_SYNTAX_ERROR;
329 return dap_info_command(CMD_CTX, &dap->ap[apsel]);
332 static const struct command_registration dap_subcommand_handlers[] = {
336 .jim_handler = jim_dap_create,
337 .usage = "name '-chain-position' name",
338 .help = "Creates a new DAP instance",
343 .jim_handler = jim_dap_names,
345 .help = "Lists all registered DAP instances by name",
350 .handler = handle_dap_init,
352 .help = "Initialize all registered DAP instances"
356 .handler = handle_dap_info_command,
357 .mode = COMMAND_EXEC,
358 .help = "display ROM table for MEM-AP of current target "
359 "(default currently selected AP)",
362 COMMAND_REGISTRATION_DONE
365 static const struct command_registration dap_commands[] = {
368 .mode = COMMAND_CONFIG,
369 .help = "DAP commands",
370 .chain = dap_subcommand_handlers,
372 COMMAND_REGISTRATION_DONE
375 int dap_register_commands(struct command_context *cmd_ctx)
377 return register_commands(cmd_ctx, NULL, dap_commands);