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