mips32, add option to avoid check in last instruction
[openocd.git] / src / target / arm_cti.c
blob75169b2eee33277647391a755615b15a7247965f
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 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
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"
31 struct arm_cti {
32 uint32_t base;
33 struct adiv5_ap *ap;
36 struct arm_cti *arm_cti_create(struct adiv5_ap *ap, uint32_t base)
38 struct arm_cti *self = calloc(1, sizeof(struct arm_cti));
39 if (!self)
40 return NULL;
42 self->base = base;
43 self->ap = ap;
44 return self;
47 static int arm_cti_mod_reg_bits(struct arm_cti *self, unsigned int reg, uint32_t mask, uint32_t value)
49 uint32_t tmp;
51 /* Read register */
52 int retval = mem_ap_read_atomic_u32(self->ap, self->base + reg, &tmp);
53 if (ERROR_OK != retval)
54 return retval;
56 /* clear bitfield */
57 tmp &= ~mask;
58 /* put new value */
59 tmp |= value & mask;
61 /* write new value */
62 return mem_ap_write_atomic_u32(self->ap, self->base + reg, tmp);
65 int arm_cti_enable(struct arm_cti *self, bool enable)
67 uint32_t val = enable ? 1 : 0;
69 return mem_ap_write_atomic_u32(self->ap, self->base + CTI_CTR, val);
72 int arm_cti_ack_events(struct arm_cti *self, uint32_t event)
74 int retval;
75 uint32_t tmp;
77 retval = mem_ap_write_atomic_u32(self->ap, self->base + CTI_INACK, event);
78 if (retval == ERROR_OK) {
79 int64_t then = timeval_ms();
80 for (;;) {
81 retval = mem_ap_read_atomic_u32(self->ap, self->base + CTI_TROUT_STATUS, &tmp);
82 if (retval != ERROR_OK)
83 break;
84 if ((tmp & event) == 0)
85 break;
86 if (timeval_ms() > then + 1000) {
87 LOG_ERROR("timeout waiting for target");
88 retval = ERROR_TARGET_TIMEOUT;
89 break;
94 return retval;
97 int arm_cti_gate_channel(struct arm_cti *self, uint32_t channel)
99 if (channel > 31)
100 return ERROR_COMMAND_ARGUMENT_INVALID;
102 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0);
105 int arm_cti_ungate_channel(struct arm_cti *self, uint32_t channel)
107 if (channel > 31)
108 return ERROR_COMMAND_ARGUMENT_INVALID;
110 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0xFFFFFFFF);
113 int arm_cti_write_reg(struct arm_cti *self, unsigned int reg, uint32_t value)
115 return mem_ap_write_atomic_u32(self->ap, self->base + reg, value);
118 int arm_cti_read_reg(struct arm_cti *self, unsigned int reg, uint32_t *p_value)
120 if (p_value == NULL)
121 return ERROR_COMMAND_ARGUMENT_INVALID;
123 return mem_ap_read_atomic_u32(self->ap, self->base + reg, p_value);
126 int arm_cti_pulse_channel(struct arm_cti *self, uint32_t channel)
128 if (channel > 31)
129 return ERROR_COMMAND_ARGUMENT_INVALID;
131 return arm_cti_write_reg(self, CTI_APPPULSE, CTI_CHNL(channel));
134 int arm_cti_set_channel(struct arm_cti *self, uint32_t channel)
136 if (channel > 31)
137 return ERROR_COMMAND_ARGUMENT_INVALID;
139 return arm_cti_write_reg(self, CTI_APPSET, CTI_CHNL(channel));
142 int arm_cti_clear_channel(struct arm_cti *self, uint32_t channel)
144 if (channel > 31)
145 return ERROR_COMMAND_ARGUMENT_INVALID;
147 return arm_cti_write_reg(self, CTI_APPCLEAR, CTI_CHNL(channel));