1 /***************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky *
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. *
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. *
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., *
18 ***************************************************************************/
26 #include "target/arm_adi_v5.h"
27 #include "target/arm_cti.h"
28 #include "target/target.h"
29 #include "helper/time_support.h"
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
));
47 static int arm_cti_mod_reg_bits(struct arm_cti
*self
, unsigned int reg
, uint32_t mask
, uint32_t value
)
52 int retval
= mem_ap_read_atomic_u32(self
->ap
, self
->base
+ reg
, &tmp
);
53 if (ERROR_OK
!= retval
)
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
)
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();
81 retval
= mem_ap_read_atomic_u32(self
->ap
, self
->base
+ CTI_TROUT_STATUS
, &tmp
);
82 if (retval
!= ERROR_OK
)
84 if ((tmp
& event
) == 0)
86 if (timeval_ms() > then
+ 1000) {
87 LOG_ERROR("timeout waiting for target");
88 retval
= ERROR_TARGET_TIMEOUT
;
97 int arm_cti_gate_channel(struct arm_cti
*self
, uint32_t channel
)
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
)
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
)
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
)
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
)
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
)
145 return ERROR_COMMAND_ARGUMENT_INVALID
;
147 return arm_cti_write_reg(self
, CTI_APPCLEAR
, CTI_CHNL(channel
));