blob: 2de9d5849ef023c286d67729b3c6c13a29b06338 [file] [log] [blame]
Jesper Nilssonec87ee22007-11-30 17:46:11 +01001/*
Mikael Starvik51533b62005-07-27 11:44:44 -07002 * linux/arch/cris/kernel/fasttimer.c
3 *
4 * Fast timers for ETRAX FS
Mikael Starvik51533b62005-07-27 11:44:44 -07005 *
Jesper Nilssonec87ee22007-11-30 17:46:11 +01006 * Copyright (C) 2000-2006 Axis Communications AB, Lund, Sweden
Mikael Starvik51533b62005-07-27 11:44:44 -07007 */
8
9#include <linux/errno.h>
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/param.h>
13#include <linux/string.h>
14#include <linux/vmalloc.h>
15#include <linux/interrupt.h>
16#include <linux/time.h>
17#include <linux/delay.h>
18
19#include <asm/irq.h>
20#include <asm/system.h>
21
Mikael Starvik51533b62005-07-27 11:44:44 -070022#include <linux/version.h>
23
Jesper Nilssonec87ee22007-11-30 17:46:11 +010024#include <hwregs/reg_map.h>
25#include <hwregs/reg_rdwr.h>
26#include <hwregs/timer_defs.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070027#include <asm/fasttimer.h>
28#include <linux/proc_fs.h>
29
30/*
31 * timer0 is running at 100MHz and generating jiffies timer ticks
32 * at 100 or 1000 HZ.
33 * fasttimer gives an API that gives timers that expire "between" the jiffies
34 * giving microsecond resolution (10 ns).
35 * fasttimer uses reg_timer_rw_trig register to get interrupt when
36 * r_time reaches a certain value.
37 */
38
39
40#define DEBUG_LOG_INCLUDED
41#define FAST_TIMER_LOG
Jesper Nilssonec87ee22007-11-30 17:46:11 +010042/* #define FAST_TIMER_TEST */
Mikael Starvik51533b62005-07-27 11:44:44 -070043
44#define FAST_TIMER_SANITY_CHECKS
45
46#ifdef FAST_TIMER_SANITY_CHECKS
Jesper Nilssonf64dd212008-01-24 14:34:37 +010047static int sanity_failed;
Mikael Starvik51533b62005-07-27 11:44:44 -070048#endif
49
50#define D1(x)
51#define D2(x)
52#define DP(x)
53
Jesper Nilssonec87ee22007-11-30 17:46:11 +010054static unsigned int fast_timer_running;
55static unsigned int fast_timers_added;
56static unsigned int fast_timers_started;
57static unsigned int fast_timers_expired;
58static unsigned int fast_timers_deleted;
59static unsigned int fast_timer_is_init;
60static unsigned int fast_timer_ints;
Mikael Starvik51533b62005-07-27 11:44:44 -070061
62struct fast_timer *fast_timer_list = NULL;
63
64#ifdef DEBUG_LOG_INCLUDED
65#define DEBUG_LOG_MAX 128
66static const char * debug_log_string[DEBUG_LOG_MAX];
67static unsigned long debug_log_value[DEBUG_LOG_MAX];
Jesper Nilssonec87ee22007-11-30 17:46:11 +010068static unsigned int debug_log_cnt;
69static unsigned int debug_log_cnt_wrapped;
Mikael Starvik51533b62005-07-27 11:44:44 -070070
71#define DEBUG_LOG(string, value) \
72{ \
73 unsigned long log_flags; \
74 local_irq_save(log_flags); \
75 debug_log_string[debug_log_cnt] = (string); \
76 debug_log_value[debug_log_cnt] = (unsigned long)(value); \
77 if (++debug_log_cnt >= DEBUG_LOG_MAX) \
78 { \
79 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
80 debug_log_cnt_wrapped = 1; \
81 } \
82 local_irq_restore(log_flags); \
83}
84#else
85#define DEBUG_LOG(string, value)
86#endif
87
88
89#define NUM_TIMER_STATS 16
90#ifdef FAST_TIMER_LOG
91struct fast_timer timer_added_log[NUM_TIMER_STATS];
92struct fast_timer timer_started_log[NUM_TIMER_STATS];
93struct fast_timer timer_expired_log[NUM_TIMER_STATS];
94#endif
95
96int timer_div_settings[NUM_TIMER_STATS];
97int timer_delay_settings[NUM_TIMER_STATS];
98
Jesper Nilssonec87ee22007-11-30 17:46:11 +010099struct work_struct fast_work;
Mikael Starvik51533b62005-07-27 11:44:44 -0700100
101static void
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100102timer_trig_handler(struct work_struct *work);
Mikael Starvik51533b62005-07-27 11:44:44 -0700103
104
105
106/* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100107inline void do_gettimeofday_fast(struct fasttime_t *tv)
Mikael Starvik51533b62005-07-27 11:44:44 -0700108{
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100109 tv->tv_jiff = jiffies;
110 tv->tv_usec = GET_JIFFIES_USEC();
Mikael Starvik51533b62005-07-27 11:44:44 -0700111}
112
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100113inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
Mikael Starvik51533b62005-07-27 11:44:44 -0700114{
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100115 /* Compare jiffies. Takes care of wrapping */
116 if (time_before(t0->tv_jiff, t1->tv_jiff))
117 return -1;
118 else if (time_after(t0->tv_jiff, t1->tv_jiff))
119 return 1;
120
121 /* Compare us */
122 if (t0->tv_usec < t1->tv_usec)
123 return -1;
124 else if (t0->tv_usec > t1->tv_usec)
125 return 1;
126 return 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700127}
128
129/* Called with ints off */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100130inline void start_timer_trig(unsigned long delay_us)
Mikael Starvik51533b62005-07-27 11:44:44 -0700131{
132 reg_timer_rw_ack_intr ack_intr = { 0 };
133 reg_timer_rw_intr_mask intr_mask;
134 reg_timer_rw_trig trig;
135 reg_timer_rw_trig_cfg trig_cfg = { 0 };
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100136 reg_timer_r_time r_time0;
137 reg_timer_r_time r_time1;
138 unsigned char trig_wrap;
139 unsigned char time_wrap;
Mikael Starvik51533b62005-07-27 11:44:44 -0700140
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100141 r_time0 = REG_RD(timer, regi_timer0, r_time);
Mikael Starvik51533b62005-07-27 11:44:44 -0700142
143 D1(printk("start_timer_trig : %d us freq: %i div: %i\n",
144 delay_us, freq_index, div));
145 /* Clear trig irq */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100146 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700147 intr_mask.trig = 0;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100148 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700149
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100150 /* Set timer values and check if trigger wraps. */
151 /* r_time is 100MHz (10 ns resolution) */
152 trig_wrap = (trig = r_time0 + delay_us*(1000/10)) < r_time0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700153
154 timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = trig;
155 timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
156
157 /* Ack interrupt */
158 ack_intr.trig = 1;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100159 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
Mikael Starvik51533b62005-07-27 11:44:44 -0700160
161 /* Start timer */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100162 REG_WR(timer, regi_timer0, rw_trig, trig);
Mikael Starvik51533b62005-07-27 11:44:44 -0700163 trig_cfg.tmr = regk_timer_time;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100164 REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
Mikael Starvik51533b62005-07-27 11:44:44 -0700165
166 /* Check if we have already passed the trig time */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100167 r_time1 = REG_RD(timer, regi_timer0, r_time);
168 time_wrap = r_time1 < r_time0;
169
170 if ((trig_wrap && !time_wrap) || (r_time1 < trig)) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700171 /* No, Enable trig irq */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100172 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700173 intr_mask.trig = 1;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100174 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700175 fast_timers_started++;
176 fast_timer_running = 1;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100177 } else {
Mikael Starvik51533b62005-07-27 11:44:44 -0700178 /* We have passed the time, disable trig point, ack intr */
179 trig_cfg.tmr = regk_timer_off;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100180 REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
181 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
182 /* call the int routine */
183 INIT_WORK(&fast_work, timer_trig_handler);
184 schedule_work(&fast_work);
Mikael Starvik51533b62005-07-27 11:44:44 -0700185 }
186
187}
188
189/* In version 1.4 this function takes 27 - 50 us */
190void start_one_shot_timer(struct fast_timer *t,
191 fast_timer_function_type *function,
192 unsigned long data,
193 unsigned long delay_us,
194 const char *name)
195{
196 unsigned long flags;
197 struct fast_timer *tmp;
198
199 D1(printk("sft %s %d us\n", name, delay_us));
200
201 local_irq_save(flags);
202
203 do_gettimeofday_fast(&t->tv_set);
204 tmp = fast_timer_list;
205
Jesper Nilssonf64dd212008-01-24 14:34:37 +0100206#ifdef FAST_TIMER_SANITY_CHECKS
207 /* Check so this is not in the list already... */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100208 while (tmp != NULL) {
209 if (tmp == t) {
210 printk(KERN_DEBUG
211 "timer name: %s data: 0x%08lX already "
212 "in list!\n", name, data);
213 sanity_failed++;
214 goto done;
215 } else
Jesper Nilssonf64dd212008-01-24 14:34:37 +0100216 tmp = tmp->next;
217 }
218 tmp = fast_timer_list;
219#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700220
221 t->delay_us = delay_us;
222 t->function = function;
223 t->data = data;
224 t->name = name;
225
226 t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100227 t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ;
228 if (t->tv_expires.tv_usec > 1000000) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700229 t->tv_expires.tv_usec -= 1000000;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100230 t->tv_expires.tv_jiff += HZ;
Mikael Starvik51533b62005-07-27 11:44:44 -0700231 }
232#ifdef FAST_TIMER_LOG
233 timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
234#endif
235 fast_timers_added++;
236
237 /* Check if this should timeout before anything else */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100238 if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700239 /* Put first in list and modify the timer value */
240 t->prev = NULL;
241 t->next = fast_timer_list;
242 if (fast_timer_list)
Mikael Starvik51533b62005-07-27 11:44:44 -0700243 fast_timer_list->prev = t;
Mikael Starvik51533b62005-07-27 11:44:44 -0700244 fast_timer_list = t;
245#ifdef FAST_TIMER_LOG
246 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
247#endif
248 start_timer_trig(delay_us);
249 } else {
250 /* Put in correct place in list */
251 while (tmp->next &&
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100252 fasttime_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0)
Mikael Starvik51533b62005-07-27 11:44:44 -0700253 tmp = tmp->next;
Mikael Starvik51533b62005-07-27 11:44:44 -0700254 /* Insert t after tmp */
255 t->prev = tmp;
256 t->next = tmp->next;
257 if (tmp->next)
258 {
259 tmp->next->prev = t;
260 }
261 tmp->next = t;
262 }
263
264 D2(printk("start_one_shot_timer: %d us done\n", delay_us));
265
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100266done:
Mikael Starvik51533b62005-07-27 11:44:44 -0700267 local_irq_restore(flags);
268} /* start_one_shot_timer */
269
270static inline int fast_timer_pending (const struct fast_timer * t)
271{
272 return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
273}
274
275static inline int detach_fast_timer (struct fast_timer *t)
276{
277 struct fast_timer *next, *prev;
278 if (!fast_timer_pending(t))
279 return 0;
280 next = t->next;
281 prev = t->prev;
282 if (next)
283 next->prev = prev;
284 if (prev)
285 prev->next = next;
286 else
287 fast_timer_list = next;
288 fast_timers_deleted++;
289 return 1;
290}
291
292int del_fast_timer(struct fast_timer * t)
293{
294 unsigned long flags;
295 int ret;
296
297 local_irq_save(flags);
298 ret = detach_fast_timer(t);
299 t->next = t->prev = NULL;
300 local_irq_restore(flags);
301 return ret;
302} /* del_fast_timer */
303
304
305/* Interrupt routines or functions called in interrupt context */
306
307/* Timer interrupt handler for trig interrupts */
308
309static irqreturn_t
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100310timer_trig_interrupt(int irq, void *dev_id)
Mikael Starvik51533b62005-07-27 11:44:44 -0700311{
312 reg_timer_r_masked_intr masked_intr;
Mikael Starvik51533b62005-07-27 11:44:44 -0700313 /* Check if the timer interrupt is for us (a trig int) */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100314 masked_intr = REG_RD(timer, regi_timer0, r_masked_intr);
Mikael Starvik51533b62005-07-27 11:44:44 -0700315 if (!masked_intr.trig)
316 return IRQ_NONE;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100317 timer_trig_handler(NULL);
Mikael Starvik51533b62005-07-27 11:44:44 -0700318 return IRQ_HANDLED;
319}
320
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100321static void timer_trig_handler(struct work_struct *work)
Mikael Starvik51533b62005-07-27 11:44:44 -0700322{
323 reg_timer_rw_ack_intr ack_intr = { 0 };
324 reg_timer_rw_intr_mask intr_mask;
325 reg_timer_rw_trig_cfg trig_cfg = { 0 };
326 struct fast_timer *t;
327 unsigned long flags;
328
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100329 /* We keep interrupts disabled not only when we modify the
330 * fast timer list, but any time we hold a reference to a
331 * timer in the list, since del_fast_timer may be called
332 * from (another) interrupt context. Thus, the only time
333 * when interrupts are enabled is when calling the timer
334 * callback function.
335 */
Mikael Starvik51533b62005-07-27 11:44:44 -0700336 local_irq_save(flags);
337
338 /* Clear timer trig interrupt */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100339 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700340 intr_mask.trig = 0;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100341 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700342
343 /* First stop timer, then ack interrupt */
344 /* Stop timer */
345 trig_cfg.tmr = regk_timer_off;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100346 REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
Mikael Starvik51533b62005-07-27 11:44:44 -0700347
348 /* Ack interrupt */
349 ack_intr.trig = 1;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100350 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
Mikael Starvik51533b62005-07-27 11:44:44 -0700351
352 fast_timer_running = 0;
353 fast_timer_ints++;
354
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100355 fast_timer_function_type *f;
356 unsigned long d;
Mikael Starvik51533b62005-07-27 11:44:44 -0700357
358 t = fast_timer_list;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100359 while (t) {
360 struct fasttime_t tv;
Mikael Starvik51533b62005-07-27 11:44:44 -0700361
362 /* Has it really expired? */
363 do_gettimeofday_fast(&tv);
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100364 D1(printk(KERN_DEBUG
365 "t: %is %06ius\n", tv.tv_jiff, tv.tv_usec));
Mikael Starvik51533b62005-07-27 11:44:44 -0700366
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100367 if (fasttime_cmp(&t->tv_expires, &tv) <= 0) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700368 /* Yes it has expired */
369#ifdef FAST_TIMER_LOG
370 timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;
371#endif
372 fast_timers_expired++;
373
374 /* Remove this timer before call, since it may reuse the timer */
Mikael Starvik51533b62005-07-27 11:44:44 -0700375 if (t->prev)
Mikael Starvik51533b62005-07-27 11:44:44 -0700376 t->prev->next = t->next;
Mikael Starvik51533b62005-07-27 11:44:44 -0700377 else
Mikael Starvik51533b62005-07-27 11:44:44 -0700378 fast_timer_list = t->next;
Mikael Starvik51533b62005-07-27 11:44:44 -0700379 if (t->next)
Mikael Starvik51533b62005-07-27 11:44:44 -0700380 t->next->prev = t->prev;
Mikael Starvik51533b62005-07-27 11:44:44 -0700381 t->prev = NULL;
382 t->next = NULL;
Mikael Starvik51533b62005-07-27 11:44:44 -0700383
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100384 /* Save function callback data before enabling
385 * interrupts, since the timer may be removed and we
386 * don't know how it was allocated (e.g. ->function
387 * and ->data may become overwritten after deletion
388 * if the timer was stack-allocated).
389 */
390 f = t->function;
391 d = t->data;
392
393 if (f != NULL) {
394 /* Run the callback function with interrupts
395 * enabled. */
396 local_irq_restore(flags);
397 f(d);
398 local_irq_save(flags);
399 } else
Mikael Starvik51533b62005-07-27 11:44:44 -0700400 DEBUG_LOG("!trimertrig %i function==NULL!\n", fast_timer_ints);
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100401 } else {
Mikael Starvik51533b62005-07-27 11:44:44 -0700402 /* Timer is to early, let's set it again using the normal routines */
403 D1(printk(".\n"));
404 }
405
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100406 t = fast_timer_list;
407 if (t != NULL) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700408 /* Start next timer.. */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100409 long us = 0;
410 struct fasttime_t tv;
Mikael Starvik51533b62005-07-27 11:44:44 -0700411
412 do_gettimeofday_fast(&tv);
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100413
414 /* time_after_eq takes care of wrapping */
415 if (time_after_eq(t->tv_expires.tv_jiff, tv.tv_jiff))
416 us = ((t->tv_expires.tv_jiff - tv.tv_jiff) *
417 1000000 / HZ + t->tv_expires.tv_usec -
418 tv.tv_usec);
419
420 if (us > 0) {
421 if (!fast_timer_running) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700422#ifdef FAST_TIMER_LOG
423 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
424#endif
425 start_timer_trig(us);
426 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700427 break;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100428 } else {
Mikael Starvik51533b62005-07-27 11:44:44 -0700429 /* Timer already expired, let's handle it better late than never.
430 * The normal loop handles it
431 */
432 D1(printk("e! %d\n", us));
433 }
434 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700435 }
436
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100437 local_irq_restore(flags);
438
439 if (!t)
Mikael Starvik51533b62005-07-27 11:44:44 -0700440 D1(printk("ttrig stop!\n"));
Mikael Starvik51533b62005-07-27 11:44:44 -0700441}
442
443static void wake_up_func(unsigned long data)
444{
Mikael Starvik51533b62005-07-27 11:44:44 -0700445 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
Mikael Starvik51533b62005-07-27 11:44:44 -0700446 wake_up(sleep_wait_p);
447}
448
449
450/* Useful API */
451
452void schedule_usleep(unsigned long us)
453{
454 struct fast_timer t;
Mikael Starvik51533b62005-07-27 11:44:44 -0700455 wait_queue_head_t sleep_wait;
456 init_waitqueue_head(&sleep_wait);
Mikael Starvik51533b62005-07-27 11:44:44 -0700457
458 D1(printk("schedule_usleep(%d)\n", us));
Mikael Starvik51533b62005-07-27 11:44:44 -0700459 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
460 "usleep");
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100461 /* Uninterruptible sleep on the fast timer. (The condition is
462 * somewhat redundant since the timer is what wakes us up.) */
463 wait_event(sleep_wait, !fast_timer_pending(&t));
464
Mikael Starvik51533b62005-07-27 11:44:44 -0700465 D1(printk("done schedule_usleep(%d)\n", us));
Mikael Starvik51533b62005-07-27 11:44:44 -0700466}
467
468#ifdef CONFIG_PROC_FS
469static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
Robert P. J. Day7a3e9652007-05-06 14:50:56 -0700470 ,int *eof, void *data_unused);
Mikael Starvik51533b62005-07-27 11:44:44 -0700471static struct proc_dir_entry *fasttimer_proc_entry;
Mikael Starvik51533b62005-07-27 11:44:44 -0700472#endif /* CONFIG_PROC_FS */
473
474#ifdef CONFIG_PROC_FS
475
476/* This value is very much based on testing */
477#define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
478
479static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
Robert P. J. Day7a3e9652007-05-06 14:50:56 -0700480 ,int *eof, void *data_unused)
Mikael Starvik51533b62005-07-27 11:44:44 -0700481{
482 unsigned long flags;
483 int i = 0;
484 int num_to_show;
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100485 struct fasttime_t tv;
Mikael Starvik51533b62005-07-27 11:44:44 -0700486 struct fast_timer *t, *nextt;
487 static char *bigbuf = NULL;
488 static unsigned long used;
489
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100490 if (!bigbuf) {
491 bigbuf = vmalloc(BIG_BUF_SIZE);
492 if (!bigbuf) {
493 used = 0;
494 if (buf)
495 buf[0] = '\0';
496 return 0;
497 }
498 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700499
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100500 if (!offset || !used) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700501 do_gettimeofday_fast(&tv);
502
503 used = 0;
504 used += sprintf(bigbuf + used, "Fast timers added: %i\n",
505 fast_timers_added);
506 used += sprintf(bigbuf + used, "Fast timers started: %i\n",
507 fast_timers_started);
508 used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
509 fast_timer_ints);
510 used += sprintf(bigbuf + used, "Fast timers expired: %i\n",
511 fast_timers_expired);
512 used += sprintf(bigbuf + used, "Fast timers deleted: %i\n",
513 fast_timers_deleted);
514 used += sprintf(bigbuf + used, "Fast timer running: %s\n",
515 fast_timer_running ? "yes" : "no");
516 used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n",
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100517 (unsigned long)tv.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700518 (unsigned long)tv.tv_usec);
519#ifdef FAST_TIMER_SANITY_CHECKS
520 used += sprintf(bigbuf + used, "Sanity failed: %i\n",
521 sanity_failed);
522#endif
523 used += sprintf(bigbuf + used, "\n");
524
525#ifdef DEBUG_LOG_INCLUDED
526 {
527 int end_i = debug_log_cnt;
528 i = 0;
529
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100530 if (debug_log_cnt_wrapped)
Mikael Starvik51533b62005-07-27 11:44:44 -0700531 i = debug_log_cnt;
Mikael Starvik51533b62005-07-27 11:44:44 -0700532
533 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
534 used+100 < BIG_BUF_SIZE)
535 {
536 used += sprintf(bigbuf + used, debug_log_string[i],
537 debug_log_value[i]);
538 i = (i+1) % DEBUG_LOG_MAX;
539 }
540 }
541 used += sprintf(bigbuf + used, "\n");
542#endif
543
544 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
545 NUM_TIMER_STATS);
546 used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
547 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
548 {
549 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
550
551#if 1 //ndef FAST_TIMER_LOG
552 used += sprintf(bigbuf + used, "div: %i delay: %i"
553 "\n",
554 timer_div_settings[cur],
555 timer_delay_settings[cur]
556 );
557#endif
558#ifdef FAST_TIMER_LOG
559 t = &timer_started_log[cur];
560 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
561 "d: %6li us data: 0x%08lX"
562 "\n",
563 t->name,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100564 (unsigned long)t->tv_set.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700565 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100566 (unsigned long)t->tv_expires.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700567 (unsigned long)t->tv_expires.tv_usec,
568 t->delay_us,
569 t->data
570 );
571#endif
572 }
573 used += sprintf(bigbuf + used, "\n");
574
575#ifdef FAST_TIMER_LOG
576 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
577 NUM_TIMER_STATS);
578 used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
579 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
580 {
581 t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
582 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
583 "d: %6li us data: 0x%08lX"
584 "\n",
585 t->name,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100586 (unsigned long)t->tv_set.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700587 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100588 (unsigned long)t->tv_expires.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700589 (unsigned long)t->tv_expires.tv_usec,
590 t->delay_us,
591 t->data
592 );
593 }
594 used += sprintf(bigbuf + used, "\n");
595
596 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
597 NUM_TIMER_STATS);
598 used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
599 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
600 {
601 t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
602 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
603 "d: %6li us data: 0x%08lX"
604 "\n",
605 t->name,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100606 (unsigned long)t->tv_set.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700607 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100608 (unsigned long)t->tv_expires.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700609 (unsigned long)t->tv_expires.tv_usec,
610 t->delay_us,
611 t->data
612 );
613 }
614 used += sprintf(bigbuf + used, "\n");
615#endif
616
617 used += sprintf(bigbuf + used, "Active timers:\n");
618 local_irq_save(flags);
Mikael Starvik51533b62005-07-27 11:44:44 -0700619 t = fast_timer_list;
620 while (t != NULL && (used+100 < BIG_BUF_SIZE))
621 {
622 nextt = t->next;
623 local_irq_restore(flags);
624 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100625 "d: %6li us data: 0x%08lX"
626/* " func: 0x%08lX" */
627 "\n",
628 t->name,
629 (unsigned long)t->tv_set.tv_jiff,
630 (unsigned long)t->tv_set.tv_usec,
631 (unsigned long)t->tv_expires.tv_jiff,
632 (unsigned long)t->tv_expires.tv_usec,
Mikael Starvik51533b62005-07-27 11:44:44 -0700633 t->delay_us,
634 t->data
635/* , t->function */
636 );
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100637 local_irq_save(flags);
Mikael Starvik51533b62005-07-27 11:44:44 -0700638 if (t->next != nextt)
639 {
640 printk("timer removed!\n");
641 }
642 t = nextt;
643 }
644 local_irq_restore(flags);
645 }
646
647 if (used - offset < len)
648 {
649 len = used - offset;
650 }
651
652 memcpy(buf, bigbuf + offset, len);
653 *start = buf;
Mikael Starvik51533b62005-07-27 11:44:44 -0700654 *eof = 1;
Mikael Starvik51533b62005-07-27 11:44:44 -0700655
656 return len;
657}
658#endif /* PROC_FS */
659
660#ifdef FAST_TIMER_TEST
661static volatile unsigned long i = 0;
662static volatile int num_test_timeout = 0;
663static struct fast_timer tr[10];
664static int exp_num[10];
665
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100666static struct fasttime_t tv_exp[100];
Mikael Starvik51533b62005-07-27 11:44:44 -0700667
668static void test_timeout(unsigned long data)
669{
670 do_gettimeofday_fast(&tv_exp[data]);
671 exp_num[data] = num_test_timeout;
672
673 num_test_timeout++;
674}
675
676static void test_timeout1(unsigned long data)
677{
678 do_gettimeofday_fast(&tv_exp[data]);
679 exp_num[data] = num_test_timeout;
680 if (data < 7)
681 {
682 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
683 i++;
684 }
685 num_test_timeout++;
686}
687
688DP(
689static char buf0[2000];
690static char buf1[2000];
691static char buf2[2000];
692static char buf3[2000];
693static char buf4[2000];
694);
695
696static char buf5[6000];
697static int j_u[1000];
698
699static void fast_timer_test(void)
700{
701 int prev_num;
702 int j;
703
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100704 struct fasttime_t tv, tv0, tv1, tv2;
Mikael Starvik51533b62005-07-27 11:44:44 -0700705
706 printk("fast_timer_test() start\n");
707 do_gettimeofday_fast(&tv);
708
709 for (j = 0; j < 1000; j++)
710 {
711 j_u[j] = GET_JIFFIES_USEC();
712 }
713 for (j = 0; j < 100; j++)
714 {
715 do_gettimeofday_fast(&tv_exp[j]);
716 }
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100717 printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
Mikael Starvik51533b62005-07-27 11:44:44 -0700718
719 for (j = 0; j < 1000; j++)
720 {
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100721 printk(KERN_DEBUG "%i %i %i %i %i\n",
722 j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]);
Mikael Starvik51533b62005-07-27 11:44:44 -0700723 j += 4;
724 }
725 for (j = 0; j < 100; j++)
726 {
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100727 printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
728 tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
729 tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
730 tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
731 tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
732 tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
Mikael Starvik51533b62005-07-27 11:44:44 -0700733 j += 4;
734 }
735 do_gettimeofday_fast(&tv0);
736 start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
737 DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
738 i++;
739 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
740 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
741 i++;
742 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
743 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
744 i++;
745 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
746 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
747 i++;
748 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
749 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
750 i++;
751 do_gettimeofday_fast(&tv1);
752
753 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
754
755 prev_num = num_test_timeout;
756 while (num_test_timeout < i)
757 {
758 if (num_test_timeout != prev_num)
Mikael Starvik51533b62005-07-27 11:44:44 -0700759 prev_num = num_test_timeout;
Mikael Starvik51533b62005-07-27 11:44:44 -0700760 }
761 do_gettimeofday_fast(&tv2);
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100762 printk(KERN_INFO "Timers started %is %06i\n",
763 tv0.tv_jiff, tv0.tv_usec);
764 printk(KERN_INFO "Timers started at %is %06i\n",
765 tv1.tv_jiff, tv1.tv_usec);
766 printk(KERN_INFO "Timers done %is %06i\n",
767 tv2.tv_jiff, tv2.tv_usec);
Mikael Starvik51533b62005-07-27 11:44:44 -0700768 DP(printk("buf0:\n");
769 printk(buf0);
770 printk("buf1:\n");
771 printk(buf1);
772 printk("buf2:\n");
773 printk(buf2);
774 printk("buf3:\n");
775 printk(buf3);
776 printk("buf4:\n");
777 printk(buf4);
778 );
779 printk("buf5:\n");
780 printk(buf5);
781
782 printk("timers set:\n");
783 for(j = 0; j<i; j++)
784 {
785 struct fast_timer *t = &tr[j];
786 printk("%-10s set: %6is %06ius exp: %6is %06ius "
787 "data: 0x%08X func: 0x%08X\n",
788 t->name,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100789 t->tv_set.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700790 t->tv_set.tv_usec,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100791 t->tv_expires.tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700792 t->tv_expires.tv_usec,
793 t->data,
794 t->function
795 );
796
797 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
798 t->delay_us,
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100799 tv_exp[j].tv_jiff,
Mikael Starvik51533b62005-07-27 11:44:44 -0700800 tv_exp[j].tv_usec,
801 exp_num[j],
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100802 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
803 1000000 + tv_exp[j].tv_usec -
804 t->tv_expires.tv_usec);
Mikael Starvik51533b62005-07-27 11:44:44 -0700805 }
806 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
807 printk("buf5 after all done:\n");
808 printk(buf5);
809 printk("fast_timer_test() done\n");
810}
811#endif
812
813
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100814int fast_timer_init(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700815{
816 /* For some reason, request_irq() hangs when called froom time_init() */
817 if (!fast_timer_is_init)
818 {
819 printk("fast_timer_init()\n");
820
821#ifdef CONFIG_PROC_FS
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100822 fasttimer_proc_entry = create_proc_entry("fasttimer", 0, 0);
823 if (fasttimer_proc_entry)
824 fasttimer_proc_entry->read_proc = proc_fasttimer_read;
Mikael Starvik51533b62005-07-27 11:44:44 -0700825#endif /* PROC_FS */
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100826 if (request_irq(TIMER0_INTR_VECT, timer_trig_interrupt,
827 IRQF_SHARED | IRQF_DISABLED,
828 "fast timer int", &fast_timer_list))
829 printk(KERN_ERR "err: fasttimer irq\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700830 fast_timer_is_init = 1;
831#ifdef FAST_TIMER_TEST
832 printk("do test\n");
833 fast_timer_test();
834#endif
835 }
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100836 return 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700837}
Jesper Nilssonec87ee22007-11-30 17:46:11 +0100838__initcall(fast_timer_init);