75169b2eee33277647391a755615b15a7247965f
[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
31 struct arm_cti {
32 uint32_t base;
33 struct adiv5_ap *ap;
34 };
35
36 struct arm_cti *arm_cti_create(struct adiv5_ap *ap, uint32_t base)
37 {
38 struct arm_cti *self = calloc(1, sizeof(struct arm_cti));
39 if (!self)
40 return NULL;
41
42 self->base = base;
43 self->ap = ap;
44 return self;
45 }
46
47 static int arm_cti_mod_reg_bits(struct arm_cti *self, unsigned int reg, uint32_t mask, uint32_t value)
48 {
49 uint32_t tmp;
50
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;
55
56 /* clear bitfield */
57 tmp &= ~mask;
58 /* put new value */
59 tmp |= value & mask;
60
61 /* write new value */
62 return mem_ap_write_atomic_u32(self->ap, self->base + reg, tmp);
63 }
64
65 int arm_cti_enable(struct arm_cti *self, bool enable)
66 {
67 uint32_t val = enable ? 1 : 0;
68
69 return mem_ap_write_atomic_u32(self->ap, self->base + CTI_CTR, val);
70 }
71
72 int arm_cti_ack_events(struct arm_cti *self, uint32_t event)
73 {
74 int retval;
75 uint32_t tmp;
76
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;
90 }
91 }
92 }
93
94 return retval;
95 }
96
97 int arm_cti_gate_channel(struct arm_cti *self, uint32_t channel)
98 {
99 if (channel > 31)
100 return ERROR_COMMAND_ARGUMENT_INVALID;
101
102 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0);
103 }
104
105 int arm_cti_ungate_channel(struct arm_cti *self, uint32_t channel)
106 {
107 if (channel > 31)
108 return ERROR_COMMAND_ARGUMENT_INVALID;
109
110 return arm_cti_mod_reg_bits(self, CTI_GATE, CTI_CHNL(channel), 0xFFFFFFFF);
111 }
112
113 int arm_cti_write_reg(struct arm_cti *self, unsigned int reg, uint32_t value)
114 {
115 return mem_ap_write_atomic_u32(self->ap, self->base + reg, value);
116 }
117
118 int arm_cti_read_reg(struct arm_cti *self, unsigned int reg, uint32_t *p_value)
119 {
120 if (p_value == NULL)
121 return ERROR_COMMAND_ARGUMENT_INVALID;
122
123 return mem_ap_read_atomic_u32(self->ap, self->base + reg, p_value);
124 }
125
126 int arm_cti_pulse_channel(struct arm_cti *self, uint32_t channel)
127 {
128 if (channel > 31)
129 return ERROR_COMMAND_ARGUMENT_INVALID;
130
131 return arm_cti_write_reg(self, CTI_APPPULSE, CTI_CHNL(channel));
132 }
133
134 int arm_cti_set_channel(struct arm_cti *self, uint32_t channel)
135 {
136 if (channel > 31)
137 return ERROR_COMMAND_ARGUMENT_INVALID;
138
139 return arm_cti_write_reg(self, CTI_APPSET, CTI_CHNL(channel));
140 }
141
142 int arm_cti_clear_channel(struct arm_cti *self, uint32_t channel)
143 {
144 if (channel > 31)
145 return ERROR_COMMAND_ARGUMENT_INVALID;
146
147 return arm_cti_write_reg(self, CTI_APPCLEAR, CTI_CHNL(channel));
148 }