Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2017-2019 Oticon A/S
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : /**
8 : : * Driver for the native_sim board timer
9 : : * It provides the same API required by the kernel as any other timer driver
10 : : */
11 : : #include <zephyr/types.h>
12 : : #include <zephyr/irq.h>
13 : : #include <zephyr/init.h>
14 : : #include <zephyr/drivers/timer/system_timer.h>
15 : : #include <zephyr/sys/clock.h>
16 : : #include "nsi_hw_scheduler.h"
17 : : #include "nsi_timer_model.h"
18 : : #include "soc.h"
19 : :
20 : : /**
21 : : * Return the current HW cycle counter. This corresponds to the number of
22 : : * microseconds since boot, in 64 bits.
23 : : */
24 : 435 : static uint64_t timer_driver_cycle_get(void)
25 : : {
26 : 435 : return nsi_hws_get_time();
27 : : }
28 : :
29 : : /* The model fires at once for a deadline already passed, hence ORDERED. */
30 : 126 : static void timer_driver_set_compare(uint64_t cycles)
31 : : {
32 : 126 : hwtimer_set_tick_one_shot(cycles);
33 : 126 : }
34 : :
35 : : /*
36 : : * Knobs for system_timer_generic.h
37 : : */
38 : : #define TIMER_CORE_BACKEND_COMPARE_ORDERED
39 : : #define TIMER_CORE_COUNTER_WIDTH 64
40 : : #define TIMER_CORE_ALARM_MAX_CYCLES NSI_NEVER
41 : :
42 : : #include "system_timer_generic.h"
43 : :
44 : : /**
45 : : * Interrupt handler for the timer interrupt
46 : : * Announce to the kernel that a number of ticks have passed
47 : : */
48 : 0 : static void np_timer_isr(const void *arg)
49 : : {
50 : 0 : ARG_UNUSED(arg);
51 : :
52 : 0 : timer_core_announce();
53 : 0 : }
54 : :
55 : : /**
56 : : * This function exists only to enable tests to call into the timer ISR
57 : : */
58 : 0 : void np_timer_isr_test_hook(const void *arg)
59 : : {
60 : 0 : np_timer_isr(NULL);
61 : 0 : }
62 : :
63 : : /**
64 : : * @brief Stop announcing sys ticks into the kernel
65 : : *
66 : : * Disable the system ticks generation
67 : : */
68 : 0 : void sys_clock_disable(void)
69 : : {
70 : 0 : irq_disable(TIMER_TICK_IRQ);
71 : 0 : hwtimer_set_tick_one_shot(NSI_NEVER);
72 : 0 : }
73 : :
74 : : /**
75 : : * @brief Initialize system timer driver
76 : : *
77 : : * Enable the hw timer, and setup its interrupt
78 : : */
79 : 1 : static int sys_clock_driver_init(void)
80 : : {
81 : 1 : IRQ_CONNECT(TIMER_TICK_IRQ, 1, np_timer_isr, 0, 0);
82 : 1 : irq_enable(TIMER_TICK_IRQ);
83 : :
84 : 1 : timer_core_init();
85 : :
86 : 1 : return 0;
87 : : }
88 : :
89 : : SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
90 : : CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
|