target/arm_cti: add new 'ack' and 'channel' commands
[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[28];
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 { CTI_APPPULSE, "APPPULSE", &cti_regs[26] },
210 { CTI_INACK, "INACK", &cti_regs[27] },
211 };
212
213 static int cti_find_reg_offset(const char *name)
214 {
215 unsigned int i;
216
217 for (i = 0; i < ARRAY_SIZE(cti_names); i++) {
218 if (!strcmp(name, cti_names[i].label))
219 return cti_names[i].offset;
220 }
221
222 LOG_ERROR("unknown CTI register %s", name);
223 return -1;
224 }
225
226 int arm_cti_cleanup_all(void)
227 {
228 struct arm_cti_object *obj, *tmp;
229
230 list_for_each_entry_safe(obj, tmp, &all_cti, lh) {
231 free(obj->name);
232 free(obj);
233 }
234
235 return ERROR_OK;
236 }
237
238 COMMAND_HANDLER(handle_cti_dump)
239 {
240 struct arm_cti_object *obj = CMD_DATA;
241 struct arm_cti *cti = &obj->cti;
242 int retval = ERROR_OK;
243
244 for (int i = 0; (retval == ERROR_OK) && (i < (int)ARRAY_SIZE(cti_names)); i++)
245 retval = mem_ap_read_u32(cti->ap,
246 cti->base + cti_names[i].offset, cti_names[i].p_val);
247
248 if (retval == ERROR_OK)
249 retval = dap_run(cti->ap->dap);
250
251 if (retval != ERROR_OK)
252 return JIM_ERR;
253
254 for (int i = 0; i < (int)ARRAY_SIZE(cti_names); i++)
255 command_print(CMD, "%8.8s (0x%04"PRIx32") 0x%08"PRIx32,
256 cti_names[i].label, cti_names[i].offset, *cti_names[i].p_val);
257
258 return JIM_OK;
259 }
260
261 COMMAND_HANDLER(handle_cti_enable)
262 {
263 struct arm_cti_object *obj = CMD_DATA;
264 Jim_Interp *interp = CMD_CTX->interp;
265 struct arm_cti *cti = &obj->cti;
266 bool on_off;
267
268 if (CMD_ARGC != 1) {
269 Jim_SetResultString(interp, "wrong number of args", -1);
270 return ERROR_FAIL;
271 }
272
273 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
274
275 return arm_cti_enable(cti, on_off);
276 }
277
278 COMMAND_HANDLER(handle_cti_testmode)
279 {
280 struct arm_cti_object *obj = CMD_DATA;
281 Jim_Interp *interp = CMD_CTX->interp;
282 struct arm_cti *cti = &obj->cti;
283 bool on_off;
284
285 if (CMD_ARGC != 1) {
286 Jim_SetResultString(interp, "wrong number of args", -1);
287 return ERROR_FAIL;
288 }
289
290 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
291
292 return arm_cti_write_reg(cti, 0xf00, on_off ? 0x1 : 0x0);
293 }
294
295 COMMAND_HANDLER(handle_cti_write)
296 {
297 struct arm_cti_object *obj = CMD_DATA;
298 Jim_Interp *interp = CMD_CTX->interp;
299 struct arm_cti *cti = &obj->cti;
300 int offset;
301 uint32_t value;
302
303 if (CMD_ARGC != 2) {
304 Jim_SetResultString(interp, "Wrong number of args", -1);
305 return ERROR_FAIL;
306 }
307
308 offset = cti_find_reg_offset(CMD_ARGV[0]);
309 if (offset < 0)
310 return ERROR_FAIL;
311
312 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
313
314 return arm_cti_write_reg(cti, offset, value);
315 }
316
317 COMMAND_HANDLER(handle_cti_read)
318 {
319 struct arm_cti_object *obj = CMD_DATA;
320 Jim_Interp *interp = CMD_CTX->interp;
321 struct arm_cti *cti = &obj->cti;
322 int offset;
323 int retval;
324 uint32_t value;
325
326 if (CMD_ARGC != 1) {
327 Jim_SetResultString(interp, "Wrong number of args", -1);
328 return ERROR_FAIL;
329 }
330
331 offset = cti_find_reg_offset(CMD_ARGV[0]);
332 if (offset < 0)
333 return ERROR_FAIL;
334
335 retval = arm_cti_read_reg(cti, offset, &value);
336 if (retval != ERROR_OK)
337 return retval;
338
339 command_print(CMD, "0x%08"PRIx32, value);
340
341 return ERROR_OK;
342 }
343
344 COMMAND_HANDLER(handle_cti_ack)
345 {
346 struct arm_cti_object *obj = CMD_DATA;
347 struct arm_cti *cti = &obj->cti;
348 uint32_t event;
349
350 if (CMD_ARGC != 1)
351 return ERROR_COMMAND_SYNTAX_ERROR;
352
353 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], event);
354
355 int retval = arm_cti_ack_events(cti, 1 << event);
356
357
358 if (retval != ERROR_OK)
359 return retval;
360
361 return ERROR_OK;
362 }
363
364 COMMAND_HANDLER(handle_cti_channel)
365 {
366 struct arm_cti_object *obj = CMD_DATA;
367 struct arm_cti *cti = &obj->cti;
368 int retval = ERROR_OK;
369 uint32_t ch_num;
370
371 if (CMD_ARGC != 2)
372 return ERROR_COMMAND_SYNTAX_ERROR;
373
374 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], ch_num);
375
376 if (!strcmp(CMD_ARGV[1], "gate"))
377 retval = arm_cti_gate_channel(cti, ch_num);
378 else if (!strcmp(CMD_ARGV[1], "ungate"))
379 retval = arm_cti_ungate_channel(cti, ch_num);
380 else if (!strcmp(CMD_ARGV[1], "pulse"))
381 retval = arm_cti_pulse_channel(cti, ch_num);
382 else if (!strcmp(CMD_ARGV[1], "set"))
383 retval = arm_cti_set_channel(cti, ch_num);
384 else if (!strcmp(CMD_ARGV[1], "clear"))
385 retval = arm_cti_clear_channel(cti, ch_num);
386 else {
387 command_print(CMD, "Possible channel operations: gate|ungate|set|clear|pulse");
388 return ERROR_COMMAND_ARGUMENT_INVALID;
389 }
390
391 if (retval != ERROR_OK)
392 return retval;
393
394 return ERROR_OK;
395 }
396
397 static const struct command_registration cti_instance_command_handlers[] = {
398 {
399 .name = "dump",
400 .mode = COMMAND_EXEC,
401 .handler = handle_cti_dump,
402 .help = "dump CTI registers",
403 .usage = "",
404 },
405 {
406 .name = "enable",
407 .mode = COMMAND_EXEC,
408 .handler = handle_cti_enable,
409 .help = "enable or disable the CTI",
410 .usage = "'on'|'off'",
411 },
412 {
413 .name = "testmode",
414 .mode = COMMAND_EXEC,
415 .handler = handle_cti_testmode,
416 .help = "enable or disable integration test mode",
417 .usage = "'on'|'off'",
418 },
419 {
420 .name = "write",
421 .mode = COMMAND_EXEC,
422 .handler = handle_cti_write,
423 .help = "write to a CTI register",
424 .usage = "register_name value",
425 },
426 {
427 .name = "read",
428 .mode = COMMAND_EXEC,
429 .handler = handle_cti_read,
430 .help = "read a CTI register",
431 .usage = "register_name",
432 },
433 {
434 .name = "ack",
435 .mode = COMMAND_EXEC,
436 .handler = handle_cti_ack,
437 .help = "acknowledge a CTI event",
438 .usage = "event",
439 },
440 {
441 .name = "channel",
442 .mode = COMMAND_EXEC,
443 .handler = handle_cti_channel,
444 .help = "do an operation on one CTI channel, possible operations: "
445 "gate, ungate, set, clear and pulse",
446 .usage = "channel_number operation",
447 },
448 COMMAND_REGISTRATION_DONE
449 };
450
451 enum cti_cfg_param {
452 CFG_DAP,
453 CFG_AP_NUM,
454 CFG_CTIBASE
455 };
456
457 static const Jim_Nvp nvp_config_opts[] = {
458 { .name = "-dap", .value = CFG_DAP },
459 { .name = "-ctibase", .value = CFG_CTIBASE },
460 { .name = "-ap-num", .value = CFG_AP_NUM },
461 { .name = NULL, .value = -1 }
462 };
463
464 static int cti_configure(Jim_GetOptInfo *goi, struct arm_cti_object *cti)
465 {
466 struct adiv5_dap *dap = NULL;
467 Jim_Nvp *n;
468 jim_wide w;
469 int e;
470
471 /* parse config or cget options ... */
472 while (goi->argc > 0) {
473 Jim_SetEmptyResult(goi->interp);
474
475 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
476 if (e != JIM_OK) {
477 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
478 return e;
479 }
480 switch (n->value) {
481 case CFG_DAP: {
482 Jim_Obj *o_t;
483 e = Jim_GetOpt_Obj(goi, &o_t);
484 if (e != JIM_OK)
485 return e;
486 dap = dap_instance_by_jim_obj(goi->interp, o_t);
487 if (dap == NULL) {
488 Jim_SetResultString(goi->interp, "-dap is invalid", -1);
489 return JIM_ERR;
490 }
491 /* loop for more */
492 break;
493 }
494 case CFG_CTIBASE:
495 e = Jim_GetOpt_Wide(goi, &w);
496 if (e != JIM_OK)
497 return e;
498 cti->cti.base = (uint32_t)w;
499 /* loop for more */
500 break;
501
502 case CFG_AP_NUM:
503 e = Jim_GetOpt_Wide(goi, &w);
504 if (e != JIM_OK)
505 return e;
506 if (w < 0 || w > DP_APSEL_MAX) {
507 Jim_SetResultString(goi->interp, "-ap-num is invalid", -1);
508 return JIM_ERR;
509 }
510 cti->ap_num = (uint32_t)w;
511 }
512 }
513
514 if (dap == NULL) {
515 Jim_SetResultString(goi->interp, "-dap required when creating CTI", -1);
516 return JIM_ERR;
517 }
518
519 cti->cti.ap = dap_ap(dap, cti->ap_num);
520
521 return JIM_OK;
522 }
523
524 static int cti_create(Jim_GetOptInfo *goi)
525 {
526 struct command_context *cmd_ctx;
527 static struct arm_cti_object *cti;
528 Jim_Obj *new_cmd;
529 Jim_Cmd *cmd;
530 const char *cp;
531 int e;
532
533 cmd_ctx = current_command_context(goi->interp);
534 assert(cmd_ctx != NULL);
535
536 if (goi->argc < 3) {
537 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
538 return JIM_ERR;
539 }
540 /* COMMAND */
541 Jim_GetOpt_Obj(goi, &new_cmd);
542 /* does this command exist? */
543 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
544 if (cmd) {
545 cp = Jim_GetString(new_cmd, NULL);
546 Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
547 return JIM_ERR;
548 }
549
550 /* Create it */
551 cti = calloc(1, sizeof(struct arm_cti_object));
552 if (cti == NULL)
553 return JIM_ERR;
554
555 e = cti_configure(goi, cti);
556 if (e != JIM_OK) {
557 free(cti);
558 return e;
559 }
560
561 cp = Jim_GetString(new_cmd, NULL);
562 cti->name = strdup(cp);
563
564 /* now - create the new cti name command */
565 const struct command_registration cti_subcommands[] = {
566 {
567 .chain = cti_instance_command_handlers,
568 },
569 COMMAND_REGISTRATION_DONE
570 };
571 const struct command_registration cti_commands[] = {
572 {
573 .name = cp,
574 .mode = COMMAND_ANY,
575 .help = "cti instance command group",
576 .usage = "",
577 .chain = cti_subcommands,
578 },
579 COMMAND_REGISTRATION_DONE
580 };
581 e = register_commands(cmd_ctx, NULL, cti_commands);
582 if (ERROR_OK != e)
583 return JIM_ERR;
584
585 struct command *c = command_find_in_context(cmd_ctx, cp);
586 assert(c);
587 command_set_handler_data(c, cti);
588
589 list_add_tail(&cti->lh, &all_cti);
590
591 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
592 }
593
594 static int jim_cti_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
595 {
596 Jim_GetOptInfo goi;
597 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
598 if (goi.argc < 2) {
599 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
600 "<name> [<cti_options> ...]");
601 return JIM_ERR;
602 }
603 return cti_create(&goi);
604 }
605
606 static int jim_cti_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
607 {
608 struct arm_cti_object *obj;
609
610 if (argc != 1) {
611 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
612 return JIM_ERR;
613 }
614 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
615 list_for_each_entry(obj, &all_cti, lh) {
616 Jim_ListAppendElement(interp, Jim_GetResult(interp),
617 Jim_NewStringObj(interp, obj->name, -1));
618 }
619 return JIM_OK;
620 }
621
622
623 static const struct command_registration cti_subcommand_handlers[] = {
624 {
625 .name = "create",
626 .mode = COMMAND_ANY,
627 .jim_handler = jim_cti_create,
628 .usage = "name '-chain-position' name [options ...]",
629 .help = "Creates a new CTI object",
630 },
631 {
632 .name = "names",
633 .mode = COMMAND_ANY,
634 .jim_handler = jim_cti_names,
635 .usage = "",
636 .help = "Lists all registered CTI objects by name",
637 },
638 COMMAND_REGISTRATION_DONE
639 };
640
641 static const struct command_registration cti_command_handlers[] = {
642 {
643 .name = "cti",
644 .mode = COMMAND_CONFIG,
645 .help = "CTI commands",
646 .chain = cti_subcommand_handlers,
647 .usage = "",
648 },
649 COMMAND_REGISTRATION_DONE
650 };
651
652 int cti_register_commands(struct command_context *cmd_ctx)
653 {
654 return register_commands(cmd_ctx, NULL, cti_command_handlers);
655 }
656

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)