1 /*
2 * Copyright (c) 2020, Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/devicetree.h>
8 #if DT_HAS_COMPAT_STATUS_OKAY(zephyr_native_posix_counter)
9 #define DT_DRV_COMPAT zephyr_native_posix_counter
10 #warning "zephyr,native-posix-counter is deprecated in favor of zephyr,native-sim-counter"
11 #else
12 #define DT_DRV_COMPAT zephyr_native_sim_counter
13 #endif
14
15 #include <string.h>
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/counter.h>
18 #include <zephyr/irq.h>
19 #include <soc.h>
20 #include <hw_counter.h>
21 #include <limits.h>
22
23 #define DRIVER_CONFIG_INFO_FLAGS (COUNTER_CONFIG_INFO_COUNT_UP)
24 #define DRIVER_CONFIG_INFO_CHANNELS CONFIG_COUNTER_NATIVE_SIM_NBR_CHANNELS
25 #define COUNTER_NATIVE_SIM_IRQ_FLAGS (0)
26 #define COUNTER_NATIVE_SIM_IRQ_PRIORITY (2)
27
28 #define COUNTER_PERIOD (USEC_PER_SEC / CONFIG_COUNTER_NATIVE_SIM_FREQUENCY)
29 #define TOP_VALUE (UINT_MAX)
30
31 static struct counter_alarm_cfg pending_alarm[DRIVER_CONFIG_INFO_CHANNELS];
32 static bool is_alarm_pending[DRIVER_CONFIG_INFO_CHANNELS];
33 static struct counter_top_cfg top;
34 static bool is_top_set;
35 static const struct device *dev_p;
36
schedule_next_isr(void)37 static void schedule_next_isr(void)
38 {
39 int64_t current_value = hw_counter_get_value();
40 uint32_t next_time = top.ticks; /* top.ticks is TOP_VALUE if is_top_set == false */
41
42 if (current_value == top.ticks) {
43 current_value = -1;
44 }
45
46 for (int i = 0; i < DRIVER_CONFIG_INFO_CHANNELS; i++) {
47 if (is_alarm_pending[i]) {
48 if (pending_alarm[i].ticks > current_value) {
49 /* If the alarm is not after a wrap */
50 next_time = MIN(pending_alarm[i].ticks, next_time);
51 }
52 }
53 }
54
55 /* We will at least get an interrupt at top.ticks even if is_top_set == false,
56 * which is fine. We may use that to set the next alarm if needed
57 */
58 hw_counter_set_target(next_time);
59 }
60
counter_isr(const void * arg)61 static void counter_isr(const void *arg)
62 {
63 ARG_UNUSED(arg);
64 uint32_t current_value = hw_counter_get_value();
65
66 for (int i = 0; i < DRIVER_CONFIG_INFO_CHANNELS; i++) {
67 if (is_alarm_pending[i] && (current_value == pending_alarm[i].ticks)) {
68 is_alarm_pending[i] = false;
69 if (pending_alarm[i].callback) {
70 pending_alarm[i].callback(dev_p, i, current_value,
71 pending_alarm[i].user_data);
72 }
73 }
74 }
75
76 if (is_top_set && (current_value == top.ticks) && top.callback) {
77 top.callback(dev_p, top.user_data);
78 }
79
80 schedule_next_isr();
81 }
82
ctr_init(const struct device * dev)83 static int ctr_init(const struct device *dev)
84 {
85 dev_p = dev;
86 memset(is_alarm_pending, 0, sizeof(is_alarm_pending));
87 is_top_set = false;
88 top.ticks = TOP_VALUE;
89
90 IRQ_CONNECT(COUNTER_EVENT_IRQ, COUNTER_NATIVE_SIM_IRQ_PRIORITY,
91 counter_isr, NULL, COUNTER_NATIVE_SIM_IRQ_FLAGS);
92 irq_enable(COUNTER_EVENT_IRQ);
93 hw_counter_set_period(COUNTER_PERIOD);
94 hw_counter_set_wrap_value((uint64_t)top.ticks + 1);
95 hw_counter_reset();
96
97 return 0;
98 }
99
ctr_start(const struct device * dev)100 static int ctr_start(const struct device *dev)
101 {
102 ARG_UNUSED(dev);
103
104 schedule_next_isr();
105 hw_counter_start();
106 return 0;
107 }
108
ctr_stop(const struct device * dev)109 static int ctr_stop(const struct device *dev)
110 {
111 ARG_UNUSED(dev);
112
113 hw_counter_stop();
114 return 0;
115 }
116
ctr_get_value(const struct device * dev,uint32_t * ticks)117 static int ctr_get_value(const struct device *dev, uint32_t *ticks)
118 {
119 ARG_UNUSED(dev);
120
121 *ticks = hw_counter_get_value();
122 return 0;
123 }
124
ctr_reset(const struct device * dev)125 static int ctr_reset(const struct device *dev)
126 {
127 ARG_UNUSED(dev);
128
129 hw_counter_reset();
130 return 0;
131 }
132
ctr_get_pending_int(const struct device * dev)133 static uint32_t ctr_get_pending_int(const struct device *dev)
134 {
135 ARG_UNUSED(dev);
136 return 0;
137 }
138
is_any_alarm_pending(void)139 static bool is_any_alarm_pending(void)
140 {
141 for (int i = 0; i < DRIVER_CONFIG_INFO_CHANNELS; i++) {
142 if (is_alarm_pending[i]) {
143 return true;
144 }
145 }
146 return false;
147 }
148
ctr_set_top_value(const struct device * dev,const struct counter_top_cfg * cfg)149 static int ctr_set_top_value(const struct device *dev,
150 const struct counter_top_cfg *cfg)
151 {
152 ARG_UNUSED(dev);
153
154 if (is_any_alarm_pending()) {
155 posix_print_warning("Can't set top value while alarm is active\n");
156 return -EBUSY;
157 }
158
159 uint32_t current_value = hw_counter_get_value();
160
161 if (cfg->flags & COUNTER_TOP_CFG_DONT_RESET) {
162 if (current_value >= cfg->ticks) {
163 if (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE) {
164 hw_counter_reset();
165 }
166 return -ETIME;
167 }
168 } else {
169 hw_counter_reset();
170 }
171
172 top = *cfg;
173 hw_counter_set_wrap_value((uint64_t)top.ticks + 1);
174
175 if ((cfg->ticks == TOP_VALUE) && !cfg->callback) {
176 is_top_set = false;
177 } else {
178 is_top_set = true;
179 }
180
181 schedule_next_isr();
182
183 return 0;
184 }
185
ctr_get_top_value(const struct device * dev)186 static uint32_t ctr_get_top_value(const struct device *dev)
187 {
188 return top.ticks;
189 }
190
ctr_set_alarm(const struct device * dev,uint8_t chan_id,const struct counter_alarm_cfg * alarm_cfg)191 static int ctr_set_alarm(const struct device *dev, uint8_t chan_id,
192 const struct counter_alarm_cfg *alarm_cfg)
193 {
194 ARG_UNUSED(dev);
195
196 if (is_alarm_pending[chan_id]) {
197 return -EBUSY;
198 }
199
200 uint32_t ticks = alarm_cfg->ticks;
201
202 if (ticks > top.ticks) {
203 posix_print_warning("Alarm ticks %u exceed top ticks %u\n", ticks,
204 top.ticks);
205 return -EINVAL;
206 }
207
208 if (!(alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE)) {
209 uint32_t current_value = hw_counter_get_value();
210
211 ticks += current_value;
212 if (ticks > top.ticks) { /* Handle wrap arounds */
213 ticks -= (top.ticks + 1); /* The count period is top.ticks + 1 */
214 }
215 }
216
217 pending_alarm[chan_id] = *alarm_cfg;
218 pending_alarm[chan_id].ticks = ticks;
219 is_alarm_pending[chan_id] = true;
220
221 schedule_next_isr();
222
223 return 0;
224 }
225
ctr_cancel_alarm(const struct device * dev,uint8_t chan_id)226 static int ctr_cancel_alarm(const struct device *dev, uint8_t chan_id)
227 {
228 ARG_UNUSED(dev);
229
230 if (!hw_counter_is_started()) {
231 posix_print_warning("Counter not started\n");
232 return -ENOTSUP;
233 }
234
235 is_alarm_pending[chan_id] = false;
236
237 schedule_next_isr();
238
239 return 0;
240 }
241
242 static DEVICE_API(counter, ctr_api) = {
243 .start = ctr_start,
244 .stop = ctr_stop,
245 .get_value = ctr_get_value,
246 .reset = ctr_reset,
247 .set_alarm = ctr_set_alarm,
248 .cancel_alarm = ctr_cancel_alarm,
249 .set_top_value = ctr_set_top_value,
250 .get_pending_int = ctr_get_pending_int,
251 .get_top_value = ctr_get_top_value,
252 };
253
254 static const struct counter_config_info ctr_config = {
255 .max_top_value = UINT_MAX,
256 .freq = CONFIG_COUNTER_NATIVE_SIM_FREQUENCY,
257 .channels = DRIVER_CONFIG_INFO_CHANNELS,
258 .flags = DRIVER_CONFIG_INFO_FLAGS
259 };
260
261 DEVICE_DT_INST_DEFINE(0, ctr_init,
262 NULL, NULL, &ctr_config, PRE_KERNEL_1,
263 CONFIG_COUNTER_INIT_PRIORITY, &ctr_api);
264