src: use namespace prefix osmo_timer*
[osmocom-bb.git] / src / target / firmware / comm / timer.c
1 /* (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
2  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
3  * All Rights Reserved
4  *
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.
9  *
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.
14  *
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.
18  *
19  */
20
21 #include <stdint.h>
22 #include <debug.h>
23 #include <osmocom/core/linuxlist.h>
24
25 #include <comm/timer.h>
26
27 #include <calypso/timer.h>
28 #include <calypso/irq.h>
29
30 #include <keypad.h>
31
32 static LLIST_HEAD(timer_list);
33
34 unsigned long volatile jiffies;
35
36 #define TIMER_HZ        100
37
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)
43
44 void add_timer(struct osmo_timer_list *timer)
45 {
46         struct osmo_timer_list *list_timer;
47
48         /* TODO: Optimize and remember the closest item... */
49         timer->active = 1;
50
51         /* this might be called from within update_timers */
52         llist_for_each_entry(list_timer, &timer_list, entry)
53                 if (timer == list_timer)
54                         return;
55
56         timer->in_list = 1;
57         llist_add(&timer->entry, &timer_list);
58 }
59
60 void schedule_timer(struct osmo_timer_list *timer, int milliseconds)
61 {
62         timer->expires = jiffies + ((milliseconds * TIMER_HZ) / 1000);
63         add_timer(timer);
64 }
65
66 void del_timer(struct osmo_timer_list *timer)
67 {
68         if (timer->in_list) {
69                 timer->active = 0;
70                 timer->in_list = 0;
71                 llist_del(&timer->entry);
72         }
73 }
74
75 int timer_pending(struct osmo_timer_list *timer)
76 {
77         return timer->active;
78 }
79
80 #if 0
81 /*
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
86  */
87 struct timeval *nearest_timer()
88 {
89         struct timeval current_time;
90
91         if (s_nearest_time.tv_sec == 0 && s_nearest_time.tv_usec == 0)
92                 return NULL;
93
94         if (gettimeofday(&current_time, NULL) == -1)
95                 return NULL;
96
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;
99
100         if (nearestTime < currentTime) {
101                 s_select_time.tv_sec = 0;
102                 s_select_time.tv_usec = 0;
103         } else {
104                 s_select_time.tv_sec = (nearestTime - currentTime) / MICRO_SECONDS;
105                 s_select_time.tv_usec = (nearestTime - currentTime) % MICRO_SECONDS;
106         }
107
108         return &s_select_time;
109 }
110
111 /*
112  * Find the nearest time and update s_nearest_time
113  */
114 void prepare_timers()
115 {
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;
120                 }
121         }
122
123         if (nearest_timer) {
124                 s_nearest_time = nearest_timer->timeout;
125         } else {
126                 memset(&s_nearest_time, 0, sizeof(struct timeval));
127         }
128 }
129 #endif
130
131 /*
132  * fire all timers... and remove them
133  */
134 int update_timers(void)
135 {
136         struct osmo_timer_list *timer, *tmp;
137         int work = 0;
138
139         /*
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.
144          *
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.
150          *
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.
153          */
154         llist_for_each_entry(timer, &timer_list, entry) {
155                 timer->handled = 0;
156         }
157
158 restart:
159         llist_for_each_entry(timer, &timer_list, entry) {
160                 if (!timer->handled && time_before(timer->expires, jiffies)) {
161                         timer->handled = 1;
162                         timer->active = 0;
163                         (*timer->cb)(timer->data);
164                         work = 1;
165                         goto restart;
166                 }
167         }
168
169         llist_for_each_entry_safe(timer, tmp, &timer_list, entry) {
170                 timer->handled = 0;
171                 if (!timer->active) {
172                         del_timer(timer);
173                 }
174         }
175
176         return work;
177 }
178
179 int timer_check(void)
180 {
181         struct osmo_timer_list *timer;
182         int i = 0;
183
184         llist_for_each_entry(timer, &timer_list, entry) {
185                 i++;
186         }
187         return i;
188 }
189
190 static void timer_irq(enum irq_nr irq)
191 {
192         /* we only increment jiffies here.  FIXME: does this need to be atomic? */
193         jiffies++;
194
195         keypad_poll();
196 }
197
198 void timer_init(void)
199 {
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);
210 #endif
211         hwtimer_enable(2, 1);
212
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);
217 }