1 /*
2  * Copyright (c) 2014-2015 Wind River Systems, Inc.
3  * Copyright (c) 2018 Synopsys Inc, Inc.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 #include <zephyr/init.h>
8 #include <zephyr/drivers/timer/system_timer.h>
9 #include <zephyr/sys_clock.h>
10 #include <zephyr/spinlock.h>
11 #include <zephyr/arch/arc/v2/aux_regs.h>
12 #include <zephyr/irq.h>
13 /*
14  * note: This implementation assumes Timer0 is present. Be sure
15  * to build the ARC CPU with Timer0.
16  *
17  * If secureshield is present and secure firmware is configured,
18  * use secure Timer 0
19  */
20 
21 #ifdef CONFIG_ARC_SECURE_FIRMWARE
22 
23 #undef _ARC_V2_TMR0_COUNT
24 #undef _ARC_V2_TMR0_CONTROL
25 #undef _ARC_V2_TMR0_LIMIT
26 
27 #define _ARC_V2_TMR0_COUNT _ARC_V2_S_TMR0_COUNT
28 #define _ARC_V2_TMR0_CONTROL _ARC_V2_S_TMR0_CONTROL
29 #define _ARC_V2_TMR0_LIMIT _ARC_V2_S_TMR0_LIMIT
30 #define IRQ_TIMER0 DT_IRQN(DT_NODELABEL(sectimer0))
31 
32 #else
33 #define IRQ_TIMER0 DT_IRQN(DT_NODELABEL(timer0))
34 #endif
35 
36 #define _ARC_V2_TMR_CTRL_IE 0x1 /* interrupt enable */
37 #define _ARC_V2_TMR_CTRL_NH 0x2 /* count only while not halted */
38 #define _ARC_V2_TMR_CTRL_W  0x4 /* watchdog mode enable */
39 #define _ARC_V2_TMR_CTRL_IP 0x8 /* interrupt pending flag */
40 
41 /* Minimum cycles in the future to try to program. */
42 #define MIN_DELAY 1024
43 /* arc timer has 32 bit, here use 31 bit to avoid the possible
44  * overflow,e.g, 0xffffffff + any value will cause overflow
45  */
46 #define COUNTER_MAX 0x7fffffff
47 #define TIMER_STOPPED 0x0
48 #define CYC_PER_TICK (sys_clock_hw_cycles_per_sec()	\
49 		      / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
50 
51 #define MAX_TICKS ((COUNTER_MAX / CYC_PER_TICK) - 1)
52 #define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
53 
54 #define TICKLESS (IS_ENABLED(CONFIG_TICKLESS_KERNEL))
55 
56 #define SMP_TIMER_DRIVER (CONFIG_SMP && CONFIG_MP_MAX_NUM_CPUS > 1)
57 
58 #if defined(CONFIG_TEST)
59 const int32_t z_sys_timer_irq_for_test = IRQ_TIMER0;
60 #endif
61 static struct k_spinlock lock;
62 
63 
64 #if SMP_TIMER_DRIVER
65 volatile static uint64_t last_time;
66 volatile static uint64_t start_time;
67 
68 #else
69 static uint32_t last_load;
70 
71 
72 /*
73  * This local variable holds the amount of timer cycles elapsed
74  * and it is updated in timer_int_handler and sys_clock_set_timeout().
75  *
76  * Note:
77  *  At an arbitrary point in time the "current" value of the
78  *  HW timer is calculated as:
79  *
80  * t = cycle_counter + elapsed();
81  */
82 static uint32_t cycle_count;
83 
84 /*
85  * This local variable holds the amount of elapsed HW cycles
86  * that have been announced to the kernel.
87  */
88 static uint32_t announced_cycles;
89 
90 
91 /*
92  * This local variable holds the amount of elapsed HW cycles due to
93  * timer wraps ('overflows') and is used in the calculation
94  * in elapsed() function, as well as in the updates to cycle_count.
95  *
96  * Note:
97  * Each time cycle_count is updated with the value from overflow_cycles,
98  * the overflow_cycles must be reset to zero.
99  */
100 static volatile uint32_t overflow_cycles;
101 #endif
102 
103 /**
104  * @brief Get contents of Timer0 count register
105  *
106  * @return Current Timer0 count
107  */
timer0_count_register_get(void)108 static ALWAYS_INLINE uint32_t timer0_count_register_get(void)
109 {
110 	return z_arc_v2_aux_reg_read(_ARC_V2_TMR0_COUNT);
111 }
112 
113 /**
114  * @brief Set Timer0 count register to the specified value
115  */
timer0_count_register_set(uint32_t value)116 static ALWAYS_INLINE void timer0_count_register_set(uint32_t value)
117 {
118 	z_arc_v2_aux_reg_write(_ARC_V2_TMR0_COUNT, value);
119 }
120 
121 /**
122  * @brief Get contents of Timer0 control register
123  *
124  * @return Contents of Timer0 control register.
125  */
timer0_control_register_get(void)126 static ALWAYS_INLINE uint32_t timer0_control_register_get(void)
127 {
128 	return z_arc_v2_aux_reg_read(_ARC_V2_TMR0_CONTROL);
129 }
130 
131 /**
132  * @brief Set Timer0 control register to the specified value
133  */
timer0_control_register_set(uint32_t value)134 static ALWAYS_INLINE void timer0_control_register_set(uint32_t value)
135 {
136 	z_arc_v2_aux_reg_write(_ARC_V2_TMR0_CONTROL, value);
137 }
138 
139 /**
140  * @brief Get contents of Timer0 limit register
141  *
142  * @return Contents of Timer0 limit register.
143  */
timer0_limit_register_get(void)144 static ALWAYS_INLINE uint32_t timer0_limit_register_get(void)
145 {
146 	return z_arc_v2_aux_reg_read(_ARC_V2_TMR0_LIMIT);
147 }
148 
149 /**
150  * @brief Set Timer0 limit register to the specified value
151  */
timer0_limit_register_set(uint32_t count)152 static ALWAYS_INLINE void timer0_limit_register_set(uint32_t count)
153 {
154 	z_arc_v2_aux_reg_write(_ARC_V2_TMR0_LIMIT, count);
155 }
156 
157 #if !SMP_TIMER_DRIVER
158 /* This internal function calculates the amount of HW cycles that have
159  * elapsed since the last time the absolute HW cycles counter has been
160  * updated. 'cycle_count' may be updated either by the ISR, or
161  * in sys_clock_set_timeout().
162  *
163  * Additionally, the function updates the 'overflow_cycles' counter, that
164  * holds the amount of elapsed HW cycles due to (possibly) multiple
165  * timer wraps (overflows).
166  *
167  * Prerequisites:
168  * - reprogramming of LIMIT must be clearing the COUNT
169  * - ISR must be clearing the 'overflow_cycles' counter.
170  * - no more than one counter-wrap has occurred between
171  *     - the timer reset or the last time the function was called
172  *     - and until the current call of the function is completed.
173  * - the function is invoked with interrupts disabled.
174  */
elapsed(void)175 static uint32_t elapsed(void)
176 {
177 	uint32_t val, ctrl;
178 
179 	do {
180 		val =  timer0_count_register_get();
181 		ctrl = timer0_control_register_get();
182 	} while (timer0_count_register_get() < val);
183 
184 	if (ctrl & _ARC_V2_TMR_CTRL_IP) {
185 		overflow_cycles += last_load;
186 		/* clear the IP bit of the control register */
187 		timer0_control_register_set(_ARC_V2_TMR_CTRL_NH |
188 					    _ARC_V2_TMR_CTRL_IE);
189 		/* use sw triggered irq to remember the timer irq request
190 		 * which may be cleared by the above operation. when elapsed ()
191 		 * is called in timer_int_handler, no need to do this.
192 		 */
193 		if (!z_arc_v2_irq_unit_is_in_isr() ||
194 		    z_arc_v2_aux_reg_read(_ARC_V2_ICAUSE) != IRQ_TIMER0) {
195 			z_arc_v2_aux_reg_write(_ARC_V2_AUX_IRQ_HINT,
196 					       IRQ_TIMER0);
197 		}
198 	}
199 
200 	return val + overflow_cycles;
201 }
202 #endif
203 
204 /**
205  * @brief System clock periodic tick handler
206  *
207  * This routine handles the system clock tick interrupt. It always
208  * announces one tick when TICKLESS is not enabled, or multiple ticks
209  * when TICKLESS is enabled.
210  */
timer_int_handler(const void * unused)211 static void timer_int_handler(const void *unused)
212 {
213 	ARG_UNUSED(unused);
214 	uint32_t dticks;
215 
216 #if defined(CONFIG_SMP) && CONFIG_MP_MAX_NUM_CPUS > 1
217 	uint64_t curr_time;
218 	k_spinlock_key_t key;
219 
220 	/* clear the IP bit of the control register */
221 	timer0_control_register_set(_ARC_V2_TMR_CTRL_NH |
222 				    _ARC_V2_TMR_CTRL_IE);
223 	key = k_spin_lock(&lock);
224 	/* gfrc is the wall clock */
225 	curr_time = z_arc_connect_gfrc_read();
226 
227 	dticks = (curr_time - last_time) / CYC_PER_TICK;
228 	/* last_time should be aligned to ticks */
229 	last_time += dticks * CYC_PER_TICK;
230 
231 	k_spin_unlock(&lock, key);
232 
233 	sys_clock_announce(dticks);
234 #else
235 	/* timer_int_handler may be triggered by timer irq or
236 	 * software helper irq
237 	 */
238 
239 	/* irq with higher priority may call sys_clock_set_timeout
240 	 * so need a lock here
241 	 */
242 	uint32_t key;
243 
244 	key = arch_irq_lock();
245 
246 	elapsed();
247 	cycle_count += overflow_cycles;
248 	overflow_cycles = 0;
249 
250 	arch_irq_unlock(key);
251 
252 	dticks = (cycle_count - announced_cycles) / CYC_PER_TICK;
253 	announced_cycles += dticks * CYC_PER_TICK;
254 	sys_clock_announce(TICKLESS ? dticks : 1);
255 #endif
256 
257 }
258 
sys_clock_set_timeout(int32_t ticks,bool idle)259 void sys_clock_set_timeout(int32_t ticks, bool idle)
260 {
261 	/* If the kernel allows us to miss tick announcements in idle,
262 	 * then shut off the counter. (Note: we can assume if idle==true
263 	 * that interrupts are already disabled)
264 	 */
265 #if SMP_TIMER_DRIVER
266 	/* as 64-bits GFRC is used as wall clock, it's ok to ignore idle
267 	 * systick will not be missed.
268 	 * However for single core using 32-bits arc timer, idle cannot
269 	 * be ignored, as 32-bits timer will overflow in a not-long time.
270 	 */
271 	if (IS_ENABLED(CONFIG_TICKLESS_KERNEL) && ticks == K_TICKS_FOREVER) {
272 		timer0_control_register_set(0);
273 		timer0_count_register_set(0);
274 		timer0_limit_register_set(0);
275 		return;
276 	}
277 
278 #if defined(CONFIG_TICKLESS_KERNEL)
279 	uint32_t delay;
280 	uint32_t key;
281 
282 	ticks = MIN(MAX_TICKS, ticks);
283 
284 	/* Desired delay in the future
285 	 * use MIN_DEALY here can trigger the timer
286 	 * irq more soon, no need to go to CYC_PER_TICK
287 	 * later.
288 	 */
289 	delay = MAX(ticks * CYC_PER_TICK, MIN_DELAY);
290 
291 	key = arch_irq_lock();
292 
293 	timer0_limit_register_set(delay - 1);
294 	timer0_count_register_set(0);
295 	timer0_control_register_set(_ARC_V2_TMR_CTRL_NH |
296 						_ARC_V2_TMR_CTRL_IE);
297 
298 	arch_irq_unlock(key);
299 #endif
300 #else
301 	if (IS_ENABLED(CONFIG_TICKLESS_KERNEL) && idle && ticks == K_TICKS_FOREVER) {
302 		timer0_control_register_set(0);
303 		timer0_count_register_set(0);
304 		timer0_limit_register_set(0);
305 		last_load = TIMER_STOPPED;
306 		return;
307 	}
308 
309 #if defined(CONFIG_TICKLESS_KERNEL)
310 	uint32_t delay;
311 	uint32_t unannounced;
312 
313 	ticks = MIN(MAX_TICKS, (uint32_t)(MAX((int32_t)(ticks - 1), 0)));
314 
315 	k_spinlock_key_t key = k_spin_lock(&lock);
316 
317 
318 	cycle_count += elapsed();
319 	/* clear counter early to avoid cycle loss as few as possible,
320 	 * between cycle_count and clearing 0, few cycles are possible
321 	 * to loss
322 	 */
323 	timer0_count_register_set(0);
324 	overflow_cycles = 0U;
325 
326 
327 	/* normal case */
328 	unannounced = cycle_count - announced_cycles;
329 
330 	if ((int32_t)unannounced < 0) {
331 		/* We haven't announced for more than half the 32-bit
332 		 * wrap duration, because new timeouts keep being set
333 		 * before the existing one fires. Force an announce
334 		 * to avoid loss of a wrap event, making sure the
335 		 * delay is at least the minimum delay possible.
336 		 */
337 		last_load = MIN_DELAY;
338 	} else {
339 		/* Desired delay in the future */
340 		delay = ticks * CYC_PER_TICK;
341 
342 		/* Round delay up to next tick boundary */
343 		delay += unannounced;
344 		delay = DIV_ROUND_UP(delay, CYC_PER_TICK) * CYC_PER_TICK;
345 
346 		delay -= unannounced;
347 		delay = MAX(delay, MIN_DELAY);
348 
349 		last_load = MIN(delay, MAX_CYCLES);
350 	}
351 
352 	timer0_limit_register_set(last_load - 1);
353 	timer0_control_register_set(_ARC_V2_TMR_CTRL_NH | _ARC_V2_TMR_CTRL_IE);
354 
355 	k_spin_unlock(&lock, key);
356 #endif
357 #endif
358 }
359 
sys_clock_elapsed(void)360 uint32_t sys_clock_elapsed(void)
361 {
362 	if (!TICKLESS) {
363 		return 0;
364 	}
365 
366 	uint32_t cyc;
367 	k_spinlock_key_t key = k_spin_lock(&lock);
368 
369 #if SMP_TIMER_DRIVER
370 	cyc = (z_arc_connect_gfrc_read() - last_time);
371 #else
372 	cyc =  elapsed() + cycle_count - announced_cycles;
373 #endif
374 
375 	k_spin_unlock(&lock, key);
376 
377 	return cyc / CYC_PER_TICK;
378 }
379 
sys_clock_cycle_get_32(void)380 uint32_t sys_clock_cycle_get_32(void)
381 {
382 #if SMP_TIMER_DRIVER
383 	return z_arc_connect_gfrc_read() - start_time;
384 #else
385 	k_spinlock_key_t key = k_spin_lock(&lock);
386 	uint32_t ret = elapsed() + cycle_count;
387 
388 	k_spin_unlock(&lock, key);
389 	return ret;
390 #endif
391 }
392 
393 #if SMP_TIMER_DRIVER
smp_timer_init(void)394 void smp_timer_init(void)
395 {
396 	/* set the initial status of timer0 of each slave core
397 	 */
398 	timer0_control_register_set(0);
399 	timer0_count_register_set(0);
400 	timer0_limit_register_set(0);
401 
402 	z_irq_priority_set(IRQ_TIMER0, CONFIG_ARCV2_TIMER_IRQ_PRIORITY, 0);
403 	irq_enable(IRQ_TIMER0);
404 }
405 #endif
406 
407 /**
408  *
409  * @brief Initialize and enable the system clock
410  *
411  * This routine is used to program the ARCv2 timer to deliver interrupts at the
412  * rate specified via the CYC_PER_TICK.
413  *
414  * @return 0
415  */
sys_clock_driver_init(void)416 static int sys_clock_driver_init(void)
417 {
418 
419 	/* ensure that the timer will not generate interrupts */
420 	timer0_control_register_set(0);
421 
422 #if SMP_TIMER_DRIVER
423 	IRQ_CONNECT(IRQ_TIMER0, CONFIG_ARCV2_TIMER_IRQ_PRIORITY,
424 		    timer_int_handler, NULL, 0);
425 
426 	timer0_limit_register_set(CYC_PER_TICK - 1);
427 	last_time = z_arc_connect_gfrc_read();
428 	start_time = last_time;
429 #else
430 	last_load = CYC_PER_TICK;
431 	overflow_cycles = 0;
432 	announced_cycles = 0;
433 
434 	IRQ_CONNECT(IRQ_TIMER0, CONFIG_ARCV2_TIMER_IRQ_PRIORITY,
435 		    timer_int_handler, NULL, 0);
436 
437 	timer0_limit_register_set(last_load - 1);
438 #endif
439 	timer0_count_register_set(0);
440 	timer0_control_register_set(_ARC_V2_TMR_CTRL_NH | _ARC_V2_TMR_CTRL_IE);
441 
442 	/* everything has been configured: safe to enable the interrupt */
443 
444 	irq_enable(IRQ_TIMER0);
445 
446 	return 0;
447 }
448 
449 SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
450 	 CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
451