target: restructure dap support
[openocd.git] / src / target / arm_cti.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_cti.h"
28 #include "target/target.h"
29 #include "helper/time_support.h"
30 #include "helper/list.h"
31 #include "helper/command.h"
32
33 struct arm_cti {
34 target_addr_t base;
35 struct adiv5_ap *ap;
36 };
37
38 struct arm_cti_object {
39 struct list_head lh;
40 struct arm_cti cti;
41 int ap_num;
42 char *name;
43 };
44
45 static LIST_HEAD(all_cti);
46
47 const char *arm_cti_name(struct arm_cti *self)
48 {
49 struct arm_cti_object *obj = container_of(self, struct arm_cti_object, cti);
50 return obj->name;
51 }
52
53 struct arm_cti *cti_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
54 {
55 struct arm_cti_object *obj = NULL;
56 const char *name;
57 bool found = false;
58
59 name = Jim_GetString(o, NULL);
60
61 list_for_each_entry(obj, &all_cti, lh) {
62 if (!strcmp(name, obj->name)) {
63 found = true;
64 break;
65 }
66 }
67
68 if (found)
69 return &obj->cti;
70 return NULL;
71 }
72
73 static int arm_cti_mod_reg_bits(struct arm_cti *self, unsigned int reg, uint32_t mask, uint32_t value)
74 {
75 uint32_t tmp;
76
77 /* Read register */
78 int retval = mem_ap_read_atomic_u32(self->ap, self->base + reg, &tmp);
79 if (ERROR_OK != retval)
80 return retval;
81
82 /* clear bitfield */
83 tmp &= ~mask;
84 /* put new value */
85 tmp |= value & mask;
86
87 /* write new value */
88 return mem_ap_write_atomic_u32(self->ap, self->base + reg, tmp);
89 }
90
91 int arm_cti_enable(struct arm_cti *self, bool enable)
92 {
93 uint32_t val = enable ? 1 : 0;
94
95 return mem_ap_write_atomic_u32(self->ap, self->base + CTI_CTR, val);
96 }
97
98 int arm_cti_ack_events(struct arm_cti *self, uint32_t event)
99 {
100 int retval;
101 uint32_t tmp;
102
103 retval = mem_ap_write_atomic_u32(self->ap, self->base + CTI_INACK, event);
104 if (retval == ERROR_OK) {
105 int64_t then = timeval_ms();
106 for (;;) {
107 retval = mem_ap_read_atomic_u32(self->ap, self->base + CTI_TROUT_STATUS, &tmp);
108 if (retval != ERROR_OK)
109 break;
110 if ((tmp & event) == 0)
111 break;
112 if (timeval_ms() > then + 1000) {
113 LOG_ERROR("timeout waiting for target");
114 retval = ERROR_TARGET_TIMEOUT;
115 break;
116 }
117 }
118 }
119
120 return retval;
121 }
122
123 int arm_cti_gate_channel(struct arm_cti *self, uint32_t channel)
124 {
125 if (channel > 31)
126 return ERROR_COMMAND_ARGUMENT_INVALID;
127
128 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0);
129 }
130
131 int arm_cti_ungate_channel(struct arm_cti *self, uint32_t channel)
132 {
133 if (channel > 31)
134 return ERROR_COMMAND_ARGUMENT_INVALID;
135
136 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0xFFFFFFFF);
137 }
138
139 int arm_cti_write_reg(struct arm_cti *self, unsigned int reg, uint32_t value)
140 {
141 return mem_ap_write_atomic_u32(self->ap, self->base + reg, value);
142 }
143
144 int arm_cti_read_reg(struct arm_cti *self, unsigned int reg, uint32_t *p_value)
145 {
146 if (p_value == NULL)
147 return ERROR_COMMAND_ARGUMENT_INVALID;
148
149 return mem_ap_read_atomic_u32(self->ap, self->base + reg, p_value);
150 }
151
152 int arm_cti_pulse_channel(struct arm_cti *self, uint32_t channel)
153 {
154 if (channel > 31)
155 return ERROR_COMMAND_ARGUMENT_INVALID;
156
157 return arm_cti_write_reg(self, CTI_APPPULSE, CTI_CHNL(channel));
158 }
159
160 int arm_cti_set_channel(struct arm_cti *self, uint32_t channel)
161 {
162 if (channel > 31)
163 return ERROR_COMMAND_ARGUMENT_INVALID;
164
165 return arm_cti_write_reg(self, CTI_APPSET, CTI_CHNL(channel));
166 }
167
168 int arm_cti_clear_channel(struct arm_cti *self, uint32_t channel)
169 {
170 if (channel > 31)
171 return ERROR_COMMAND_ARGUMENT_INVALID;
172
173 return arm_cti_write_reg(self, CTI_APPCLEAR, CTI_CHNL(channel));
174 }
175
176 static uint32_t cti_regs[26];
177
178 static const struct {
179 uint32_t offset;
180 const char *label;
181 uint32_t *p_val;
182 } cti_names[] = {
183 { CTI_CTR, "CTR", &cti_regs[0] },
184 { CTI_GATE, "GATE", &cti_regs[1] },
185 { CTI_INEN0, "INEN0", &cti_regs[2] },
186 { CTI_INEN1, "INEN1", &cti_regs[3] },
187 { CTI_INEN2, "INEN2", &cti_regs[4] },
188 { CTI_INEN3, "INEN3", &cti_regs[5] },
189 { CTI_INEN4, "INEN4", &cti_regs[6] },
190 { CTI_INEN5, "INEN5", &cti_regs[7] },
191 { CTI_INEN6, "INEN6", &cti_regs[8] },
192 { CTI_INEN7, "INEN7", &cti_regs[9] },
193 { CTI_INEN8, "INEN8", &cti_regs[10] },
194 { CTI_OUTEN0, "OUTEN0", &cti_regs[11] },
195 { CTI_OUTEN1, "OUTEN1", &cti_regs[12] },
196 { CTI_OUTEN2, "OUTEN2", &cti_regs[13] },
197 { CTI_OUTEN3, "OUTEN3", &cti_regs[14] },
198 { CTI_OUTEN4, "OUTEN4", &cti_regs[15] },
199 { CTI_OUTEN5, "OUTEN5", &cti_regs[16] },
200 { CTI_OUTEN6, "OUTEN6", &cti_regs[17] },
201 { CTI_OUTEN7, "OUTEN7", &cti_regs[18] },
202 { CTI_OUTEN8, "OUTEN8", &cti_regs[19] },
203 { CTI_TRIN_STATUS, "TRIN", &cti_regs[20] },
204 { CTI_TROUT_STATUS, "TROUT", &cti_regs[21] },
205 { CTI_CHIN_STATUS, "CHIN", &cti_regs[22] },
206 { CTI_CHOU_STATUS, "CHOUT", &cti_regs[23] },
207 { CTI_APPSET, "APPSET", &cti_regs[24] },
208 { CTI_APPCLEAR, "APPCLR", &cti_regs[25] },
209 };
210
211 static int cti_find_reg_offset(const char *name)
212 {
213 unsigned int i;
214
215 for (i = 0; i < ARRAY_SIZE(cti_names); i++) {
216 if (!strcmp(name, cti_names[i].label))
217 return cti_names[i].offset;
218 }
219 return -1;
220 }
221
222 COMMAND_HANDLER(handle_cti_dump)
223 {
224 struct arm_cti_object *obj = CMD_DATA;
225 struct arm_cti *cti = &obj->cti;
226 int retval = ERROR_OK;
227
228 for (int i = 0; (retval == ERROR_OK) && (i < (int)ARRAY_SIZE(cti_names)); i++)
229 retval = mem_ap_read_u32(cti->ap,
230 cti->base + cti_names[i].offset, cti_names[i].p_val);
231
232 if (retval == ERROR_OK)
233 retval = dap_run(cti->ap->dap);
234
235 if (retval != ERROR_OK)
236 return JIM_ERR;
237
238 for (int i = 0; i < (int)ARRAY_SIZE(cti_names); i++)
239 command_print(CMD_CTX, "%8.8s (0x%04"PRIx32") 0x%08"PRIx32,
240 cti_names[i].label, cti_names[i].offset, *cti_names[i].p_val);
241
242 return JIM_OK;
243 }
244
245 COMMAND_HANDLER(handle_cti_enable)
246 {
247 struct arm_cti_object *obj = CMD_DATA;
248 Jim_Interp *interp = CMD_CTX->interp;
249 struct arm_cti *cti = &obj->cti;
250 bool on_off;
251
252 if (CMD_ARGC != 1) {
253 Jim_SetResultString(interp, "wrong number of args", -1);
254 return ERROR_FAIL;
255 }
256
257 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
258
259 return arm_cti_enable(cti, on_off);
260 }
261
262 COMMAND_HANDLER(handle_cti_testmode)
263 {
264 struct arm_cti_object *obj = CMD_DATA;
265 Jim_Interp *interp = CMD_CTX->interp;
266 struct arm_cti *cti = &obj->cti;
267 bool on_off;
268
269 if (CMD_ARGC != 1) {
270 Jim_SetResultString(interp, "wrong number of args", -1);
271 return ERROR_FAIL;
272 }
273
274 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
275
276 return arm_cti_write_reg(cti, 0xf00, on_off ? 0x1 : 0x0);
277 }
278
279 COMMAND_HANDLER(handle_cti_write)
280 {
281 struct arm_cti_object *obj = CMD_DATA;
282 Jim_Interp *interp = CMD_CTX->interp;
283 struct arm_cti *cti = &obj->cti;
284 int offset;
285 uint32_t value;
286
287 if (CMD_ARGC != 2) {
288 Jim_SetResultString(interp, "Wrong numer of args", -1);
289 return ERROR_FAIL;
290 }
291
292 offset = cti_find_reg_offset(CMD_ARGV[0]);
293 if (offset < 0)
294 return ERROR_FAIL;
295
296 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
297
298 return arm_cti_write_reg(cti, offset, value);
299 }
300
301 COMMAND_HANDLER(handle_cti_read)
302 {
303 struct arm_cti_object *obj = CMD_DATA;
304 Jim_Interp *interp = CMD_CTX->interp;
305 struct arm_cti *cti = &obj->cti;
306 int offset;
307 int retval;
308 uint32_t value;
309
310 if (CMD_ARGC != 1) {
311 Jim_SetResultString(interp, "Wrong numer of args", -1);
312 return ERROR_FAIL;
313 }
314
315 offset = cti_find_reg_offset(CMD_ARGV[0]);
316 if (offset < 0)
317 return ERROR_FAIL;
318
319 retval = arm_cti_read_reg(cti, offset, &value);
320 if (retval != ERROR_OK)
321 return retval;
322
323 command_print(CMD_CTX, "0x%08"PRIx32, value);
324
325 return ERROR_OK;
326 }
327
328 static const struct command_registration cti_instance_command_handlers[] = {
329 {
330 .name = "dump",
331 .mode = COMMAND_EXEC,
332 .handler = handle_cti_dump,
333 .help = "dump CTI registers",
334 .usage = "",
335 },
336 {
337 .name = "enable",
338 .mode = COMMAND_EXEC,
339 .handler = handle_cti_enable,
340 .help = "enable or disable the CTI",
341 .usage = "'on'|'off'",
342 },
343 {
344 .name = "testmode",
345 .mode = COMMAND_EXEC,
346 .handler = handle_cti_testmode,
347 .help = "enable or disable integration test mode",
348 .usage = "'on'|'off'",
349 },
350 {
351 .name = "write",
352 .mode = COMMAND_EXEC,
353 .handler = handle_cti_write,
354 .help = "write to a CTI register",
355 .usage = "register_name value",
356 },
357 {
358 .name = "read",
359 .mode = COMMAND_EXEC,
360 .handler = handle_cti_read,
361 .help = "read a CTI register",
362 .usage = "register_name",
363 },
364 COMMAND_REGISTRATION_DONE
365 };
366
367 enum cti_cfg_param {
368 CFG_DAP,
369 CFG_AP_NUM,
370 CFG_CTIBASE
371 };
372
373 static const Jim_Nvp nvp_config_opts[] = {
374 { .name = "-dap", .value = CFG_DAP },
375 { .name = "-ctibase", .value = CFG_CTIBASE },
376 { .name = "-ap-num", .value = CFG_AP_NUM },
377 { .name = NULL, .value = -1 }
378 };
379
380 static int cti_configure(Jim_GetOptInfo *goi, struct arm_cti_object *cti)
381 {
382 struct adiv5_dap *dap = NULL;
383 Jim_Nvp *n;
384 jim_wide w;
385 int e;
386
387 /* parse config or cget options ... */
388 while (goi->argc > 0) {
389 Jim_SetEmptyResult(goi->interp);
390
391 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
392 if (e != JIM_OK) {
393 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
394 return e;
395 }
396 switch (n->value) {
397 case CFG_DAP: {
398 Jim_Obj *o_t;
399 e = Jim_GetOpt_Obj(goi, &o_t);
400 if (e != JIM_OK)
401 return e;
402 dap = dap_instance_by_jim_obj(goi->interp, o_t);
403 if (dap == NULL) {
404 Jim_SetResultString(goi->interp, "-dap is invalid", -1);
405 return JIM_ERR;
406 }
407 /* loop for more */
408 break;
409 }
410 case CFG_CTIBASE:
411 e = Jim_GetOpt_Wide(goi, &w);
412 if (e != JIM_OK)
413 return e;
414 cti->cti.base = (uint32_t)w;
415 /* loop for more */
416 break;
417
418 case CFG_AP_NUM:
419 e = Jim_GetOpt_Wide(goi, &w);
420 if (e != JIM_OK)
421 return e;
422 cti->ap_num = (uint32_t)w;
423 }
424 }
425
426 if (dap == NULL) {
427 Jim_SetResultString(goi->interp, "-dap required when creating CTI", -1);
428 return JIM_ERR;
429 }
430
431 cti->cti.ap = dap_ap(dap, cti->ap_num);
432
433 return JIM_OK;
434 }
435
436 static int cti_create(Jim_GetOptInfo *goi)
437 {
438 struct command_context *cmd_ctx;
439 static struct arm_cti_object *cti;
440 Jim_Obj *new_cmd;
441 Jim_Cmd *cmd;
442 const char *cp;
443 int e;
444
445 cmd_ctx = current_command_context(goi->interp);
446 assert(cmd_ctx != NULL);
447
448 if (goi->argc < 3) {
449 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
450 return JIM_ERR;
451 }
452 /* COMMAND */
453 Jim_GetOpt_Obj(goi, &new_cmd);
454 /* does this command exist? */
455 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
456 if (cmd) {
457 cp = Jim_GetString(new_cmd, NULL);
458 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
459 return JIM_ERR;
460 }
461
462 /* Create it */
463 cti = calloc(1, sizeof(struct arm_cti_object));
464 if (cti == NULL)
465 return JIM_ERR;
466
467 e = cti_configure(goi, cti);
468 if (e != JIM_OK) {
469 free(cti);
470 return e;
471 }
472
473 cp = Jim_GetString(new_cmd, NULL);
474 cti->name = strdup(cp);
475
476 /* now - create the new cti name command */
477 const struct command_registration cti_subcommands[] = {
478 {
479 .chain = cti_instance_command_handlers,
480 },
481 COMMAND_REGISTRATION_DONE
482 };
483 const struct command_registration cti_commands[] = {
484 {
485 .name = cp,
486 .mode = COMMAND_ANY,
487 .help = "cti instance command group",
488 .usage = "",
489 .chain = cti_subcommands,
490 },
491 COMMAND_REGISTRATION_DONE
492 };
493 e = register_commands(cmd_ctx, NULL, cti_commands);
494 if (ERROR_OK != e)
495 return JIM_ERR;
496
497 struct command *c = command_find_in_context(cmd_ctx, cp);
498 assert(c);
499 command_set_handler_data(c, cti);
500
501 list_add_tail(&cti->lh, &all_cti);
502
503 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
504 }
505
506 static int jim_cti_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
507 {
508 Jim_GetOptInfo goi;
509 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
510 if (goi.argc < 2) {
511 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
512 "<name> [<cti_options> ...]");
513 return JIM_ERR;
514 }
515 return cti_create(&goi);
516 }
517
518 static int jim_cti_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
519 {
520 struct arm_cti_object *obj;
521
522 if (argc != 1) {
523 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
524 return JIM_ERR;
525 }
526 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
527 list_for_each_entry(obj, &all_cti, lh) {
528 Jim_ListAppendElement(interp, Jim_GetResult(interp),
529 Jim_NewStringObj(interp, obj->name, -1));
530 }
531 return JIM_OK;
532 }
533
534
535 static const struct command_registration cti_subcommand_handlers[] = {
536 {
537 .name = "create",
538 .mode = COMMAND_ANY,
539 .jim_handler = jim_cti_create,
540 .usage = "name '-chain-position' name [options ...]",
541 .help = "Creates a new CTI object",
542 },
543 {
544 .name = "names",
545 .mode = COMMAND_ANY,
546 .jim_handler = jim_cti_names,
547 .usage = "",
548 .help = "Lists all registered CTI objects by name",
549 },
550 COMMAND_REGISTRATION_DONE
551 };
552
553 static const struct command_registration cti_command_handlers[] = {
554 {
555 .name = "cti",
556 .mode = COMMAND_CONFIG,
557 .help = "CTI commands",
558 .chain = cti_subcommand_handlers,
559 },
560 COMMAND_REGISTRATION_DONE
561 };
562
563 int cti_register_commands(struct command_context *cmd_ctx)
564 {
565 return register_commands(cmd_ctx, NULL, cti_command_handlers);
566 }
567