1 /* (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
2 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <osmocom/core/linuxlist.h>
25 #include <comm/timer.h>
27 #include <calypso/timer.h>
28 #include <calypso/irq.h>
32 static LLIST_HEAD(timer_list);
34 unsigned long volatile jiffies;
38 #define time_after(a,b) \
39 (typecheck(unsigned long, a) && \
40 typecheck(unsigned long, b) && \
41 ((long)(b) - (long)(a) < 0))
42 #define time_before(a,b) time_after(b,a)
44 void add_timer(struct osmo_timer_list *timer)
46 struct osmo_timer_list *list_timer;
48 /* TODO: Optimize and remember the closest item... */
51 /* this might be called from within update_timers */
52 llist_for_each_entry(list_timer, &timer_list, entry)
53 if (timer == list_timer)
57 llist_add(&timer->entry, &timer_list);
60 void schedule_timer(struct osmo_timer_list *timer, int milliseconds)
62 timer->expires = jiffies + ((milliseconds * TIMER_HZ) / 1000);
66 void del_timer(struct osmo_timer_list *timer)
71 llist_del(&timer->entry);
75 int timer_pending(struct osmo_timer_list *timer)
82 * if we have a nearest time return the delta between the current
83 * time and the time of the nearest timer.
84 * If the nearest timer timed out return NULL and then we will
85 * dispatch everything after the select
87 struct timeval *nearest_timer()
89 struct timeval current_time;
91 if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0)
94 if (gettimeofday(¤t_time, NULL) == -1)
97 unsigned long long nearestTime = s_nearest_time.tv_sec * MICRO_SECONDS + s_nearest_time.tv_usec;
98 unsigned long long currentTime = current_time.tv_sec * MICRO_SECONDS + current_time.tv_usec;
100 if (nearestTime < currentTime) {
101 s_select_time.tv_sec = 0;
102 s_select_time.tv_usec = 0;
104 s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
105 s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
108 return &s_select_time;
112 * Find the nearest time and update s_nearest_time
114 void prepare_timers()
116 struct osmo_timer_list *timer, *nearest_timer = NULL;
117 llist_for_each_entry(timer, &timer_list, entry) {
118 if (!nearest_timer || time_before(timer->expires, nearest_timer->expires)) {
119 nearest_timer = timer;
124 s_nearest_time = nearest_timer->timeout;
126 memset(&s_nearest_time, 0, sizeof(struct timeval));
132 * fire all timers... and remove them
134 int update_timers(void)
136 struct osmo_timer_list *timer, *tmp;
140 * The callbacks might mess with our list and in this case
141 * even llist_for_each_entry_safe is not safe to use. To allow
142 * del_timer, add_timer, schedule_timer to be called from within
143 * the callback we jump through some loops.
145 * First we set the handled flag of each active timer to zero,
146 * then we iterate over the list and execute the callbacks. As the
147 * list might have been changed (specially the next) from within
148 * the callback we have to start over again. Once every callback
149 * is dispatched we will remove the non-active from the list.
151 * TODO: If this is a performance issue we can poison a global
152 * variable in add_timer and del_timer and only then restart.
154 llist_for_each_entry(timer, &timer_list, entry) {
159 llist_for_each_entry(timer, &timer_list, entry) {
160 if (!timer->handled && time_before(timer->expires, jiffies)) {
163 (*timer->cb)(timer->data);
169 llist_for_each_entry_safe(timer, tmp, &timer_list, entry) {
171 if (!timer->active) {
179 int timer_check(void)
181 struct osmo_timer_list *timer;
184 llist_for_each_entry(timer, &timer_list, entry) {
190 static void timer_irq(enum irq_nr irq)
192 /* we only increment jiffies here. FIXME: does this need to be atomic? */
198 void timer_init(void)
200 /* configure TIMER2 for our purpose */
201 hwtimer_enable(2, 1);
202 /* The timer runs at 13MHz / 32, i.e. 406.25kHz */
203 #if (TIMER_HZ == 100)
204 hwtimer_load(2, 4062);
205 hwtimer_config(2, 0, 1);
206 #elif (TIMER_HZ == 10)
207 /* prescaler 4, 1015 ticks until expiry */
208 hwtimer_load(2, 1015);
209 hwtimer_config(2, 4, 1);
211 hwtimer_enable(2, 1);
213 /* register interrupt handler with default priority, EDGE triggered */
214 irq_register_handler(IRQ_TIMER2, &timer_irq);
215 irq_config(IRQ_TIMER2, 0, 1, -1);
216 irq_enable(IRQ_TIMER2);