target/adiv5: Large Physical Address Extension
[fw/openocd] / 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 #define ADI_BAD_CFG 0xBAD00000
40
41 /* DAP command support */
42 struct arm_dap_object {
43         struct list_head lh;
44         struct adiv5_dap dap;
45         char *name;
46         const struct swd_driver *swd;
47 };
48
49 static void dap_instance_init(struct adiv5_dap *dap)
50 {
51         int i;
52         /* Set up with safe defaults */
53         for (i = 0; i <= DP_APSEL_MAX; i++) {
54                 dap->ap[i].dap = dap;
55                 dap->ap[i].ap_num = i;
56                 /* memaccess_tck max is 255 */
57                 dap->ap[i].memaccess_tck = 255;
58                 /* Number of bits for tar autoincrement, impl. dep. at least 10 */
59                 dap->ap[i].tar_autoincr_block = (1<<10);
60                 /* default CSW value */
61                 dap->ap[i].csw_default = CSW_AHB_DEFAULT;
62                 dap->ap[i].cfg_reg = ADI_BAD_CFG; /* mem_ap configuration reg (large physical addr, etc.) */
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                 retval = dap->ops->connect(dap);
133                 if (retval != ERROR_OK)
134                         return retval;
135         }
136
137         return ERROR_OK;
138 }
139
140 int dap_cleanup_all(void)
141 {
142         struct arm_dap_object *obj, *tmp;
143         struct adiv5_dap *dap;
144
145         list_for_each_entry_safe(obj, tmp, &all_dap, lh) {
146                 dap = &obj->dap;
147                 if (dap->ops && dap->ops->quit)
148                         dap->ops->quit(dap);
149
150                 free(obj->name);
151                 free(obj);
152         }
153
154         return ERROR_OK;
155 }
156
157 enum dap_cfg_param {
158         CFG_CHAIN_POSITION,
159         CFG_IGNORE_SYSPWRUPACK,
160 };
161
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 = NULL, .value = -1 }
166 };
167
168 static int dap_configure(struct jim_getopt_info *goi, struct arm_dap_object *dap)
169 {
170         struct jtag_tap *tap = NULL;
171         struct jim_nvp *n;
172         int e;
173
174         /* parse config or cget options ... */
175         while (goi->argc > 0) {
176                 Jim_SetEmptyResult(goi->interp);
177
178                 e = jim_getopt_nvp(goi, nvp_config_opts, &n);
179                 if (e != JIM_OK) {
180                         jim_getopt_nvp_unknown(goi, nvp_config_opts, 0);
181                         return e;
182                 }
183                 switch (n->value) {
184                 case CFG_CHAIN_POSITION: {
185                         Jim_Obj *o_t;
186                         e = jim_getopt_obj(goi, &o_t);
187                         if (e != JIM_OK)
188                                 return e;
189                         tap = jtag_tap_by_jim_obj(goi->interp, o_t);
190                         if (tap == NULL) {
191                                 Jim_SetResultString(goi->interp, "-chain-position is invalid", -1);
192                                 return JIM_ERR;
193                         }
194                         /* loop for more */
195                         break;
196                 }
197                 case CFG_IGNORE_SYSPWRUPACK:
198                         dap->dap.ignore_syspwrupack = true;
199                         break;
200                 default:
201                         break;
202                 }
203         }
204
205         if (tap == NULL) {
206                 Jim_SetResultString(goi->interp, "-chain-position required when creating DAP", -1);
207                 return JIM_ERR;
208         }
209
210         dap_instance_init(&dap->dap);
211         dap->dap.tap = tap;
212
213         return JIM_OK;
214 }
215
216 static int dap_create(struct jim_getopt_info *goi)
217 {
218         struct command_context *cmd_ctx;
219         static struct arm_dap_object *dap;
220         Jim_Obj *new_cmd;
221         Jim_Cmd *cmd;
222         const char *cp;
223         int e;
224
225         cmd_ctx = current_command_context(goi->interp);
226         assert(cmd_ctx != NULL);
227
228         if (goi->argc < 3) {
229                 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
230                 return JIM_ERR;
231         }
232         /* COMMAND */
233         jim_getopt_obj(goi, &new_cmd);
234         /* does this command exist? */
235         cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
236         if (cmd) {
237                 cp = Jim_GetString(new_cmd, NULL);
238                 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
239                 return JIM_ERR;
240         }
241
242         /* Create it */
243         dap = calloc(1, sizeof(struct arm_dap_object));
244         if (dap == NULL)
245                 return JIM_ERR;
246
247         e = dap_configure(goi, dap);
248         if (e != JIM_OK) {
249                 free(dap);
250                 return e;
251         }
252
253         cp = Jim_GetString(new_cmd, NULL);
254         dap->name = strdup(cp);
255
256         struct command_registration dap_commands[] = {
257                 {
258                         .name = cp,
259                         .mode = COMMAND_ANY,
260                         .help = "dap instance command group",
261                         .usage = "",
262                         .chain = dap_instance_commands,
263                 },
264                 COMMAND_REGISTRATION_DONE
265         };
266
267         /* don't expose the instance commands when using hla */
268         if (transport_is_hla())
269                 dap_commands[0].chain = NULL;
270
271         e = register_commands_with_data(cmd_ctx, NULL, dap_commands, dap);
272         if (ERROR_OK != e)
273                 return JIM_ERR;
274
275         list_add_tail(&dap->lh, &all_dap);
276
277         return JIM_OK;
278 }
279
280 static int jim_dap_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
281 {
282         struct jim_getopt_info goi;
283         jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
284         if (goi.argc < 2) {
285                 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
286                         "<name> [<dap_options> ...]");
287                 return JIM_ERR;
288         }
289         return dap_create(&goi);
290 }
291
292 static int jim_dap_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
293 {
294         struct arm_dap_object *obj;
295
296         if (argc != 1) {
297                 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
298                 return JIM_ERR;
299         }
300         Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
301         list_for_each_entry(obj, &all_dap, lh) {
302                 Jim_ListAppendElement(interp, Jim_GetResult(interp),
303                         Jim_NewStringObj(interp, obj->name, -1));
304         }
305         return JIM_OK;
306 }
307
308 COMMAND_HANDLER(handle_dap_init)
309 {
310         return dap_init_all();
311 }
312
313 COMMAND_HANDLER(handle_dap_info_command)
314 {
315         struct target *target = get_current_target(CMD_CTX);
316         struct arm *arm = target_to_arm(target);
317         struct adiv5_dap *dap = arm->dap;
318         uint32_t apsel;
319
320         if (dap == NULL) {
321                 LOG_ERROR("DAP instance not available. Probably a HLA target...");
322                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
323         }
324
325         switch (CMD_ARGC) {
326                 case 0:
327                         apsel = dap->apsel;
328                         break;
329                 case 1:
330                         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
331                         if (apsel > DP_APSEL_MAX)
332                                 return ERROR_COMMAND_SYNTAX_ERROR;
333                         break;
334                 default:
335                         return ERROR_COMMAND_SYNTAX_ERROR;
336         }
337
338         return dap_info_command(CMD, &dap->ap[apsel]);
339 }
340
341 static const struct command_registration dap_subcommand_handlers[] = {
342         {
343                 .name = "create",
344                 .mode = COMMAND_ANY,
345                 .jim_handler = jim_dap_create,
346                 .usage = "name '-chain-position' name",
347                 .help = "Creates a new DAP instance",
348         },
349         {
350                 .name = "names",
351                 .mode = COMMAND_ANY,
352                 .jim_handler = jim_dap_names,
353                 .usage = "",
354                 .help = "Lists all registered DAP instances by name",
355         },
356         {
357                 .name = "init",
358                 .mode = COMMAND_ANY,
359                 .handler = handle_dap_init,
360                 .usage = "",
361                 .help = "Initialize all registered DAP instances"
362         },
363         {
364                 .name = "info",
365                 .handler = handle_dap_info_command,
366                 .mode = COMMAND_EXEC,
367                 .help = "display ROM table for MEM-AP of current target "
368                 "(default currently selected AP)",
369                 .usage = "[ap_num]",
370         },
371         COMMAND_REGISTRATION_DONE
372 };
373
374 static const struct command_registration dap_commands[] = {
375         {
376                 .name = "dap",
377                 .mode = COMMAND_CONFIG,
378                 .help = "DAP commands",
379                 .chain = dap_subcommand_handlers,
380                 .usage = "",
381         },
382         COMMAND_REGISTRATION_DONE
383 };
384
385 int dap_register_commands(struct command_context *cmd_ctx)
386 {
387         return register_commands(cmd_ctx, NULL, dap_commands);
388 }