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 *device;
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(device, i, current_value,
71 pending_alarm[i].user_data);
72 }
73 }
74 }
75
76 if (is_top_set && (current_value == top.ticks)) {
77 if (top.callback) {
78 top.callback(device, top.user_data);
79 }
80 }
81
82 schedule_next_isr();
83 }
84
ctr_init(const struct device * dev)85 static int ctr_init(const struct device *dev)
86 {
87 device = dev;
88 memset(is_alarm_pending, 0, sizeof(is_alarm_pending));
89 is_top_set = false;
90 top.ticks = TOP_VALUE;
91
92 IRQ_CONNECT(COUNTER_EVENT_IRQ, COUNTER_NATIVE_SIM_IRQ_PRIORITY,
93 counter_isr, NULL, COUNTER_NATIVE_SIM_IRQ_FLAGS);
94 irq_enable(COUNTER_EVENT_IRQ);
95 hw_counter_set_period(COUNTER_PERIOD);
96 hw_counter_set_wrap_value((uint64_t)top.ticks + 1);
97 hw_counter_reset();
98
99 return 0;
100 }
101
ctr_start(const struct device * dev)102 static int ctr_start(const struct device *dev)
103 {
104 ARG_UNUSED(dev);
105
106 schedule_next_isr();
107 hw_counter_start();
108 return 0;
109 }
110
ctr_stop(const struct device * dev)111 static int ctr_stop(const struct device *dev)
112 {
113 ARG_UNUSED(dev);
114
115 hw_counter_stop();
116 return 0;
117 }
118
ctr_get_value(const struct device * dev,uint32_t * ticks)119 static int ctr_get_value(const struct device *dev, uint32_t *ticks)
120 {
121 ARG_UNUSED(dev);
122
123 *ticks = hw_counter_get_value();
124 return 0;
125 }
126
ctr_get_pending_int(const struct device * dev)127 static uint32_t ctr_get_pending_int(const struct device *dev)
128 {
129 ARG_UNUSED(dev);
130 return 0;
131 }
132
is_any_alarm_pending(void)133 static bool is_any_alarm_pending(void)
134 {
135 for (int i = 0; i < DRIVER_CONFIG_INFO_CHANNELS; i++) {
136 if (is_alarm_pending[i]) {
137 return true;
138 }
139 }
140 return false;
141 }
142
ctr_set_top_value(const struct device * dev,const struct counter_top_cfg * cfg)143 static int ctr_set_top_value(const struct device *dev,
144 const struct counter_top_cfg *cfg)
145 {
146 ARG_UNUSED(dev);
147
148 if (is_any_alarm_pending()) {
149 posix_print_warning("Can't set top value while alarm is active\n");
150 return -EBUSY;
151 }
152
153 uint32_t current_value = hw_counter_get_value();
154
155 if (cfg->flags & COUNTER_TOP_CFG_DONT_RESET) {
156 if (current_value >= cfg->ticks) {
157 if (cfg->flags & COUNTER_TOP_CFG_RESET_WHEN_LATE) {
158 hw_counter_reset();
159 }
160 return -ETIME;
161 }
162 } else {
163 hw_counter_reset();
164 }
165
166 top = *cfg;
167 hw_counter_set_wrap_value((uint64_t)top.ticks + 1);
168
169 if ((cfg->ticks == TOP_VALUE) && !cfg->callback) {
170 is_top_set = false;
171 } else {
172 is_top_set = true;
173 }
174
175 schedule_next_isr();
176
177 return 0;
178 }
179
ctr_get_top_value(const struct device * dev)180 static uint32_t ctr_get_top_value(const struct device *dev)
181 {
182 return top.ticks;
183 }
184
ctr_set_alarm(const struct device * dev,uint8_t chan_id,const struct counter_alarm_cfg * alarm_cfg)185 static int ctr_set_alarm(const struct device *dev, uint8_t chan_id,
186 const struct counter_alarm_cfg *alarm_cfg)
187 {
188 ARG_UNUSED(dev);
189
190 if (is_alarm_pending[chan_id]) {
191 return -EBUSY;
192 }
193
194 uint32_t ticks = alarm_cfg->ticks;
195
196 if (ticks > top.ticks) {
197 posix_print_warning("Alarm ticks %u exceed top ticks %u\n", ticks,
198 top.ticks);
199 return -EINVAL;
200 }
201
202 if (!(alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE)) {
203 uint32_t current_value = hw_counter_get_value();
204
205 ticks += current_value;
206 if (ticks > top.ticks) { /* Handle wrap arounds */
207 ticks -= (top.ticks + 1); /* The count period is top.ticks + 1 */
208 }
209 }
210
211 pending_alarm[chan_id] = *alarm_cfg;
212 pending_alarm[chan_id].ticks = ticks;
213 is_alarm_pending[chan_id] = true;
214
215 schedule_next_isr();
216
217 return 0;
218 }
219
ctr_cancel_alarm(const struct device * dev,uint8_t chan_id)220 static int ctr_cancel_alarm(const struct device *dev, uint8_t chan_id)
221 {
222 ARG_UNUSED(dev);
223
224 if (!hw_counter_is_started()) {
225 posix_print_warning("Counter not started\n");
226 return -ENOTSUP;
227 }
228
229 is_alarm_pending[chan_id] = false;
230
231 schedule_next_isr();
232
233 return 0;
234 }
235
236 static DEVICE_API(counter, ctr_api) = {
237 .start = ctr_start,
238 .stop = ctr_stop,
239 .get_value = ctr_get_value,
240 .set_alarm = ctr_set_alarm,
241 .cancel_alarm = ctr_cancel_alarm,
242 .set_top_value = ctr_set_top_value,
243 .get_pending_int = ctr_get_pending_int,
244 .get_top_value = ctr_get_top_value,
245 };
246
247 static const struct counter_config_info ctr_config = {
248 .max_top_value = UINT_MAX,
249 .freq = CONFIG_COUNTER_NATIVE_SIM_FREQUENCY,
250 .channels = DRIVER_CONFIG_INFO_CHANNELS,
251 .flags = DRIVER_CONFIG_INFO_FLAGS
252 };
253
254 DEVICE_DT_INST_DEFINE(0, ctr_init,
255 NULL, NULL, &ctr_config, PRE_KERNEL_1,
256 CONFIG_COUNTER_INIT_PRIORITY, &ctr_api);
257