openocd: fix SPDX tag format for files .c
[openocd.git] / src / target / arm_cti.c
index d415eb3a8fa658c09242756b392394f1237d2128..3612874e2a013c143375f32412b3e2fbe64fffd8 100644 (file)
@@ -1,20 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2016 by Matthias Welwarsky                              *
  *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *                                                                         *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 #include "helper/command.h"
 
 struct arm_cti {
-       target_addr_t base;
-       struct adiv5_ap *ap;
-};
-
-struct arm_cti_object {
        struct list_head lh;
-       struct arm_cti cti;
-       int ap_num;
        char *name;
+       struct adiv5_mem_ap_spot spot;
+       struct adiv5_ap *ap;
 };
 
 static LIST_HEAD(all_cti);
 
 const char *arm_cti_name(struct arm_cti *self)
 {
-       struct arm_cti_object *obj = container_of(self, struct arm_cti_object, cti);
-       return obj->name;
+       return self->name;
 }
 
 struct arm_cti *cti_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
 {
-       struct arm_cti_object *obj = NULL;
+       struct arm_cti *obj = NULL;
        const char *name;
        bool found = false;
 
@@ -66,17 +48,18 @@ struct arm_cti *cti_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
        }
 
        if (found)
-               return &obj->cti;
+               return obj;
        return NULL;
 }
 
 static int arm_cti_mod_reg_bits(struct arm_cti *self, unsigned int reg, uint32_t mask, uint32_t value)
 {
+       struct adiv5_ap *ap = self->ap;
        uint32_t tmp;
 
        /* Read register */
-       int retval = mem_ap_read_atomic_u32(self->ap, self->base + reg, &tmp);
-       if (ERROR_OK != retval)
+       int retval = mem_ap_read_atomic_u32(ap, self->spot.base + reg, &tmp);
+       if (retval != ERROR_OK)
                return retval;
 
        /* clear bitfield */
@@ -85,26 +68,27 @@ static int arm_cti_mod_reg_bits(struct arm_cti *self, unsigned int reg, uint32_t
        tmp |= value & mask;
 
        /* write new value */
-       return mem_ap_write_atomic_u32(self->ap, self->base + reg, tmp);
+       return mem_ap_write_atomic_u32(ap, self->spot.base + reg, tmp);
 }
 
 int arm_cti_enable(struct arm_cti *self, bool enable)
 {
        uint32_t val = enable ? 1 : 0;
 
-       return mem_ap_write_atomic_u32(self->ap, self->base + CTI_CTR, val);
+       return mem_ap_write_atomic_u32(self->ap, self->spot.base + CTI_CTR, val);
 }
 
 int arm_cti_ack_events(struct arm_cti *self, uint32_t event)
 {
+       struct adiv5_ap *ap = self->ap;
        int retval;
        uint32_t tmp;
 
-       retval = mem_ap_write_atomic_u32(self->ap, self->base + CTI_INACK, event);
+       retval = mem_ap_write_atomic_u32(ap, self->spot.base + CTI_INACK, event);
        if (retval == ERROR_OK) {
                int64_t then = timeval_ms();
                for (;;) {
-                       retval = mem_ap_read_atomic_u32(self->ap, self->base + CTI_TROUT_STATUS, &tmp);
+                       retval = mem_ap_read_atomic_u32(ap, self->spot.base + CTI_TROUT_STATUS, &tmp);
                        if (retval != ERROR_OK)
                                break;
                        if ((tmp & event) == 0)
@@ -138,15 +122,15 @@ int arm_cti_ungate_channel(struct arm_cti *self, uint32_t channel)
 
 int arm_cti_write_reg(struct arm_cti *self, unsigned int reg, uint32_t value)
 {
-       return mem_ap_write_atomic_u32(self->ap, self->base + reg, value);
+       return mem_ap_write_atomic_u32(self->ap, self->spot.base + reg, value);
 }
 
 int arm_cti_read_reg(struct arm_cti *self, unsigned int reg, uint32_t *p_value)
 {
-       if (p_value == NULL)
+       if (!p_value)
                return ERROR_COMMAND_ARGUMENT_INVALID;
 
-       return mem_ap_read_atomic_u32(self->ap, self->base + reg, p_value);
+       return mem_ap_read_atomic_u32(self->ap, self->spot.base + reg, p_value);
 }
 
 int arm_cti_pulse_channel(struct arm_cti *self, uint32_t channel)
@@ -225,9 +209,11 @@ static int cti_find_reg_offset(const char *name)
 
 int arm_cti_cleanup_all(void)
 {
-       struct arm_cti_object *obj, *tmp;
+       struct arm_cti *obj, *tmp;
 
        list_for_each_entry_safe(obj, tmp, &all_cti, lh) {
+               if (obj->ap)
+                       dap_put_ap(obj->ap);
                free(obj->name);
                free(obj);
        }
@@ -237,16 +223,16 @@ int arm_cti_cleanup_all(void)
 
 COMMAND_HANDLER(handle_cti_dump)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
+       struct adiv5_ap *ap = cti->ap;
        int retval = ERROR_OK;
 
        for (int i = 0; (retval == ERROR_OK) && (i < (int)ARRAY_SIZE(cti_names)); i++)
-               retval = mem_ap_read_u32(cti->ap,
-                               cti->base + cti_names[i].offset, cti_names[i].p_val);
+               retval = mem_ap_read_u32(ap,
+                               cti->spot.base + cti_names[i].offset, cti_names[i].p_val);
 
        if (retval == ERROR_OK)
-               retval = dap_run(cti->ap->dap);
+               retval = dap_run(ap->dap);
 
        if (retval != ERROR_OK)
                return JIM_ERR;
@@ -260,15 +246,11 @@ COMMAND_HANDLER(handle_cti_dump)
 
 COMMAND_HANDLER(handle_cti_enable)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       Jim_Interp *interp = CMD_CTX->interp;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        bool on_off;
 
-       if (CMD_ARGC != 1) {
-               Jim_SetResultString(interp, "wrong number of args", -1);
-               return ERROR_FAIL;
-       }
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
        COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
 
@@ -277,15 +259,11 @@ COMMAND_HANDLER(handle_cti_enable)
 
 COMMAND_HANDLER(handle_cti_testmode)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       Jim_Interp *interp = CMD_CTX->interp;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        bool on_off;
 
-       if (CMD_ARGC != 1) {
-               Jim_SetResultString(interp, "wrong number of args", -1);
-               return ERROR_FAIL;
-       }
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
        COMMAND_PARSE_ON_OFF(CMD_ARGV[0], on_off);
 
@@ -294,16 +272,12 @@ COMMAND_HANDLER(handle_cti_testmode)
 
 COMMAND_HANDLER(handle_cti_write)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       Jim_Interp *interp = CMD_CTX->interp;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        int offset;
        uint32_t value;
 
-       if (CMD_ARGC != 2) {
-               Jim_SetResultString(interp, "Wrong number of args", -1);
-               return ERROR_FAIL;
-       }
+       if (CMD_ARGC != 2)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
        offset = cti_find_reg_offset(CMD_ARGV[0]);
        if (offset < 0)
@@ -316,17 +290,13 @@ COMMAND_HANDLER(handle_cti_write)
 
 COMMAND_HANDLER(handle_cti_read)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       Jim_Interp *interp = CMD_CTX->interp;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        int offset;
        int retval;
        uint32_t value;
 
-       if (CMD_ARGC != 1) {
-               Jim_SetResultString(interp, "Wrong number of args", -1);
-               return ERROR_FAIL;
-       }
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
 
        offset = cti_find_reg_offset(CMD_ARGV[0]);
        if (offset < 0)
@@ -343,8 +313,7 @@ COMMAND_HANDLER(handle_cti_read)
 
 COMMAND_HANDLER(handle_cti_ack)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        uint32_t event;
 
        if (CMD_ARGC != 1)
@@ -363,8 +332,7 @@ COMMAND_HANDLER(handle_cti_ack)
 
 COMMAND_HANDLER(handle_cti_channel)
 {
-       struct arm_cti_object *obj = CMD_DATA;
-       struct arm_cti *cti = &obj->cti;
+       struct arm_cti *cti = CMD_DATA;
        int retval = ERROR_OK;
        uint32_t ch_num;
 
@@ -448,99 +416,47 @@ static const struct command_registration cti_instance_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-enum cti_cfg_param {
-       CFG_DAP,
-       CFG_AP_NUM,
-       CFG_CTIBASE
-};
-
-static const Jim_Nvp nvp_config_opts[] = {
-       { .name = "-dap",     .value = CFG_DAP },
-       { .name = "-ctibase", .value = CFG_CTIBASE },
-       { .name = "-ap-num",  .value = CFG_AP_NUM },
-       { .name = NULL, .value = -1 }
-};
-
-static int cti_configure(Jim_GetOptInfo *goi, struct arm_cti_object *cti)
+static int cti_configure(struct jim_getopt_info *goi, struct arm_cti *cti)
 {
-       struct adiv5_dap *dap = NULL;
-       Jim_Nvp *n;
-       jim_wide w;
-       int e;
-
        /* parse config or cget options ... */
        while (goi->argc > 0) {
-               Jim_SetEmptyResult(goi->interp);
+               int e = adiv5_jim_mem_ap_spot_configure(&cti->spot, goi);
 
-               e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
-               if (e != JIM_OK) {
-                       Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
-                       return e;
-               }
-               switch (n->value) {
-               case CFG_DAP: {
-                       Jim_Obj *o_t;
-                       e = Jim_GetOpt_Obj(goi, &o_t);
-                       if (e != JIM_OK)
-                               return e;
-                       dap = dap_instance_by_jim_obj(goi->interp, o_t);
-                       if (dap == NULL) {
-                               Jim_SetResultString(goi->interp, "-dap is invalid", -1);
-                               return JIM_ERR;
-                       }
-                       /* loop for more */
-                       break;
-               }
-               case CFG_CTIBASE:
-                       e = Jim_GetOpt_Wide(goi, &w);
-                       if (e != JIM_OK)
-                               return e;
-                       cti->cti.base = (uint32_t)w;
-                       /* loop for more */
-                       break;
+               if (e == JIM_CONTINUE)
+                       Jim_SetResultFormatted(goi->interp, "unknown option '%s'",
+                               Jim_String(goi->argv[0]));
 
-               case CFG_AP_NUM:
-                       e = Jim_GetOpt_Wide(goi, &w);
-                       if (e != JIM_OK)
-                               return e;
-                       if (w < 0 || w > DP_APSEL_MAX) {
-                               Jim_SetResultString(goi->interp, "-ap-num is invalid", -1);
-                               return JIM_ERR;
-                       }
-                       cti->ap_num = (uint32_t)w;
-               }
+               if (e != JIM_OK)
+                       return JIM_ERR;
        }
 
-       if (dap == NULL) {
+       if (!cti->spot.dap) {
                Jim_SetResultString(goi->interp, "-dap required when creating CTI", -1);
                return JIM_ERR;
        }
 
-       cti->cti.ap = dap_ap(dap, cti->ap_num);
-
        return JIM_OK;
 }
-
-static int cti_create(Jim_GetOptInfo *goi)
+static int cti_create(struct jim_getopt_info *goi)
 {
        struct command_context *cmd_ctx;
-       static struct arm_cti_object *cti;
+       static struct arm_cti *cti;
        Jim_Obj *new_cmd;
        Jim_Cmd *cmd;
        const char *cp;
        int e;
 
        cmd_ctx = current_command_context(goi->interp);
-       assert(cmd_ctx != NULL);
+       assert(cmd_ctx);
 
        if (goi->argc < 3) {
                Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ..options...");
                return JIM_ERR;
        }
        /* COMMAND */
-       Jim_GetOpt_Obj(goi, &new_cmd);
+       jim_getopt_obj(goi, &new_cmd);
        /* does this command exist? */
-       cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
+       cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_NONE);
        if (cmd) {
                cp = Jim_GetString(new_cmd, NULL);
                Jim_SetResultFormatted(goi->interp, "Command: %s Exists", cp);
@@ -548,10 +464,14 @@ static int cti_create(Jim_GetOptInfo *goi)
        }
 
        /* Create it */
-       cti = calloc(1, sizeof(struct arm_cti_object));
-       if (cti == NULL)
+       cti = calloc(1, sizeof(*cti));
+       if (!cti)
                return JIM_ERR;
 
+       adiv5_mem_ap_spot_init(&cti->spot);
+
+       /* Do the rest as "configure" options */
+       goi->isconfigure = 1;
        e = cti_configure(goi, cti);
        if (e != JIM_OK) {
                free(cti);
@@ -578,23 +498,25 @@ static int cti_create(Jim_GetOptInfo *goi)
                },
                COMMAND_REGISTRATION_DONE
        };
-       e = register_commands(cmd_ctx, NULL, cti_commands);
-       if (ERROR_OK != e)
+       e = register_commands_with_data(cmd_ctx, NULL, cti_commands, cti);
+       if (e != ERROR_OK)
                return JIM_ERR;
 
-       struct command *c = command_find_in_context(cmd_ctx, cp);
-       assert(c);
-       command_set_handler_data(c, cti);
-
        list_add_tail(&cti->lh, &all_cti);
 
-       return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
+       cti->ap = dap_get_ap(cti->spot.dap, cti->spot.ap_num);
+       if (!cti->ap) {
+               Jim_SetResultString(goi->interp, "Cannot get AP", -1);
+               return JIM_ERR;
+       }
+
+       return JIM_OK;
 }
 
 static int jim_cti_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
-       Jim_GetOptInfo goi;
-       Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
+       struct jim_getopt_info goi;
+       jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
        if (goi.argc < 2) {
                Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
                        "<name> [<cti_options> ...]");
@@ -605,7 +527,7 @@ static int jim_cti_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
 static int jim_cti_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 {
-       struct arm_cti_object *obj;
+       struct arm_cti *obj;
 
        if (argc != 1) {
                Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
@@ -653,4 +575,3 @@ int cti_register_commands(struct command_context *cmd_ctx)
 {
        return register_commands(cmd_ctx, NULL, cti_command_handlers);
 }
-

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)