blob: c1a3a2100ee71e1094a8df045bec8f5948ef5023 [file] [log] [blame]
Jesper Nilssond8e52192007-11-14 17:01:23 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * linux/arch/cris/kernel/fasttimer.c
3 *
4 * Fast timers for ETRAX100/ETRAX100LX
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Jesper Nilssond8e52192007-11-14 17:01:23 -08006 * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden
Linus Torvalds1da177e2005-04-16 15:20:36 -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/mm.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/time.h>
18#include <linux/delay.h>
19
20#include <asm/segment.h>
21#include <asm/io.h>
22#include <asm/irq.h>
23#include <asm/delay.h>
24#include <asm/rtc.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/arch/svinto.h>
28#include <asm/fasttimer.h>
29#include <linux/proc_fs.h>
30
31
32#define DEBUG_LOG_INCLUDED
33#define FAST_TIMER_LOG
34//#define FAST_TIMER_TEST
35
36#define FAST_TIMER_SANITY_CHECKS
37
38#ifdef FAST_TIMER_SANITY_CHECKS
39#define SANITYCHECK(x) x
Jesper Nilssond8e52192007-11-14 17:01:23 -080040static int sanity_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#else
42#define SANITYCHECK(x)
43#endif
44
45#define D1(x)
46#define D2(x)
47#define DP(x)
48
Jesper Nilssond8e52192007-11-14 17:01:23 -080049static unsigned int fast_timer_running;
50static unsigned int fast_timers_added;
51static unsigned int fast_timers_started;
52static unsigned int fast_timers_expired;
53static unsigned int fast_timers_deleted;
54static unsigned int fast_timer_is_init;
55static unsigned int fast_timer_ints;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57struct fast_timer *fast_timer_list = NULL;
58
59#ifdef DEBUG_LOG_INCLUDED
60#define DEBUG_LOG_MAX 128
61static const char * debug_log_string[DEBUG_LOG_MAX];
62static unsigned long debug_log_value[DEBUG_LOG_MAX];
Jesper Nilssond8e52192007-11-14 17:01:23 -080063static unsigned int debug_log_cnt;
64static unsigned int debug_log_cnt_wrapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define DEBUG_LOG(string, value) \
67{ \
68 unsigned long log_flags; \
Mikael Starvik7e920422005-07-27 11:44:34 -070069 local_irq_save(log_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 debug_log_string[debug_log_cnt] = (string); \
71 debug_log_value[debug_log_cnt] = (unsigned long)(value); \
72 if (++debug_log_cnt >= DEBUG_LOG_MAX) \
73 { \
74 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
75 debug_log_cnt_wrapped = 1; \
76 } \
Mikael Starvik7e920422005-07-27 11:44:34 -070077 local_irq_restore(log_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79#else
80#define DEBUG_LOG(string, value)
81#endif
82
83
84/* The frequencies for index = clkselx number in R_TIMER_CTRL */
85#define NUM_TIMER_FREQ 15
86#define MAX_USABLE_TIMER_FREQ 7
87#define MAX_DELAY_US 853333L
88const unsigned long timer_freq_100[NUM_TIMER_FREQ] =
89{
90 3, /* 0 3333 - 853333 us */
91 6, /* 1 1666 - 426666 us */
92 12, /* 2 833 - 213333 us */
93 24, /* 3 416 - 106666 us */
94 48, /* 4 208 - 53333 us */
95 96, /* 5 104 - 26666 us */
96 192, /* 6 52 - 13333 us */
97 384, /* 7 26 - 6666 us */
98 576,
99 1152,
100 2304,
101 4608,
102 9216,
103 18432,
104 62500,
105 /* 15 = cascade */
106};
107#define NUM_TIMER_STATS 16
108#ifdef FAST_TIMER_LOG
109struct fast_timer timer_added_log[NUM_TIMER_STATS];
110struct fast_timer timer_started_log[NUM_TIMER_STATS];
111struct fast_timer timer_expired_log[NUM_TIMER_STATS];
112#endif
113
114int timer_div_settings[NUM_TIMER_STATS];
115int timer_freq_settings[NUM_TIMER_STATS];
116int timer_delay_settings[NUM_TIMER_STATS];
117
118/* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800119inline void do_gettimeofday_fast(struct fasttime_t *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Jesper Nilssond8e52192007-11-14 17:01:23 -0800121 tv->tv_jiff = jiffies;
122 tv->tv_usec = GET_JIFFIES_USEC();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800125inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jesper Nilssond8e52192007-11-14 17:01:23 -0800127 /* Compare jiffies. Takes care of wrapping */
128 if (time_before(t0->tv_jiff, t1->tv_jiff))
129 return -1;
130 else if (time_after(t0->tv_jiff, t1->tv_jiff))
131 return 1;
132
133 /* Compare us */
134 if (t0->tv_usec < t1->tv_usec)
135 return -1;
136 else if (t0->tv_usec > t1->tv_usec)
137 return 1;
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800141inline void start_timer1(unsigned long delay_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 int freq_index = 0; /* This is the lowest resolution */
144 unsigned long upper_limit = MAX_DELAY_US;
145
146 unsigned long div;
147 /* Start/Restart the timer to the new shorter value */
148 /* t = 1/freq = 1/19200 = 53us
149 * T=div*t, div = T/t = delay_us*freq/1000000
150 */
151#if 1 /* Adaptive timer settings */
152 while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ)
153 {
154 freq_index++;
155 upper_limit >>= 1; /* Divide by 2 using shift */
156 }
157 if (freq_index > 0)
158 {
159 freq_index--;
160 }
161#else
162 freq_index = 6;
163#endif
164 div = delay_us * timer_freq_100[freq_index]/10000;
165 if (div < 2)
166 {
167 /* Maybe increase timer freq? */
168 div = 2;
169 }
170 if (div > 255)
171 {
172 div = 0; /* This means 256, the max the timer takes */
173 /* If a longer timeout than the timer can handle is used,
174 * then we must restart it when it goes off.
175 */
176 }
177
178 timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div;
179 timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index;
180 timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
181
Jesper Nilssond8e52192007-11-14 17:01:23 -0800182 D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 delay_us, freq_index, div));
184 /* Clear timer1 irq */
185 *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
186
187 /* Set timer values */
188 *R_TIMER_CTRL = r_timer_ctrl_shadow =
189 (r_timer_ctrl_shadow &
190 ~IO_MASK(R_TIMER_CTRL, timerdiv1) &
191 ~IO_MASK(R_TIMER_CTRL, tm1) &
192 ~IO_MASK(R_TIMER_CTRL, clksel1)) |
193 IO_FIELD(R_TIMER_CTRL, timerdiv1, div) |
194 IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
195 IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */
196
197 /* Ack interrupt */
198 *R_TIMER_CTRL = r_timer_ctrl_shadow |
199 IO_STATE(R_TIMER_CTRL, i1, clr);
200
201 /* Start timer */
202 *R_TIMER_CTRL = r_timer_ctrl_shadow =
203 (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
204 IO_STATE(R_TIMER_CTRL, tm1, run);
205
206 /* Enable timer1 irq */
207 *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set);
208 fast_timers_started++;
209 fast_timer_running = 1;
210}
211
212/* In version 1.4 this function takes 27 - 50 us */
213void start_one_shot_timer(struct fast_timer *t,
214 fast_timer_function_type *function,
215 unsigned long data,
216 unsigned long delay_us,
217 const char *name)
218{
219 unsigned long flags;
220 struct fast_timer *tmp;
221
222 D1(printk("sft %s %d us\n", name, delay_us));
223
Mikael Starvik7e920422005-07-27 11:44:34 -0700224 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 do_gettimeofday_fast(&t->tv_set);
227 tmp = fast_timer_list;
228
229 SANITYCHECK({ /* Check so this is not in the list already... */
230 while (tmp != NULL)
231 {
232 if (tmp == t)
233 {
234 printk(KERN_WARNING
235 "timer name: %s data: 0x%08lX already in list!\n", name, data);
236 sanity_failed++;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800237 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 else
240 {
241 tmp = tmp->next;
242 }
243 }
244 tmp = fast_timer_list;
245 });
246
247 t->delay_us = delay_us;
248 t->function = function;
249 t->data = data;
250 t->name = name;
251
252 t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800253 t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (t->tv_expires.tv_usec > 1000000)
255 {
256 t->tv_expires.tv_usec -= 1000000;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800257 t->tv_expires.tv_jiff += HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259#ifdef FAST_TIMER_LOG
260 timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
261#endif
262 fast_timers_added++;
263
264 /* Check if this should timeout before anything else */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800265 if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 {
267 /* Put first in list and modify the timer value */
268 t->prev = NULL;
269 t->next = fast_timer_list;
270 if (fast_timer_list)
271 {
272 fast_timer_list->prev = t;
273 }
274 fast_timer_list = t;
275#ifdef FAST_TIMER_LOG
276 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
277#endif
278 start_timer1(delay_us);
279 } else {
280 /* Put in correct place in list */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800281 while (tmp->next && fasttime_cmp(&t->tv_expires,
282 &tmp->next->tv_expires) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 {
284 tmp = tmp->next;
285 }
286 /* Insert t after tmp */
287 t->prev = tmp;
288 t->next = tmp->next;
289 if (tmp->next)
290 {
291 tmp->next->prev = t;
292 }
293 tmp->next = t;
294 }
295
296 D2(printk("start_one_shot_timer: %d us done\n", delay_us));
297
Jesper Nilssond8e52192007-11-14 17:01:23 -0800298done:
Mikael Starvik7e920422005-07-27 11:44:34 -0700299 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300} /* start_one_shot_timer */
301
302static inline int fast_timer_pending (const struct fast_timer * t)
303{
304 return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
305}
306
307static inline int detach_fast_timer (struct fast_timer *t)
308{
309 struct fast_timer *next, *prev;
310 if (!fast_timer_pending(t))
311 return 0;
312 next = t->next;
313 prev = t->prev;
314 if (next)
315 next->prev = prev;
316 if (prev)
317 prev->next = next;
318 else
319 fast_timer_list = next;
320 fast_timers_deleted++;
321 return 1;
322}
323
324int del_fast_timer(struct fast_timer * t)
325{
326 unsigned long flags;
327 int ret;
328
Mikael Starvik7e920422005-07-27 11:44:34 -0700329 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ret = detach_fast_timer(t);
331 t->next = t->prev = NULL;
Mikael Starvik7e920422005-07-27 11:44:34 -0700332 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return ret;
334} /* del_fast_timer */
335
336
337/* Interrupt routines or functions called in interrupt context */
338
339/* Timer 1 interrupt handler */
340
341static irqreturn_t
Jesper Nilssond8e52192007-11-14 17:01:23 -0800342timer1_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct fast_timer *t;
345 unsigned long flags;
346
Jesper Nilssond8e52192007-11-14 17:01:23 -0800347 /* We keep interrupts disabled not only when we modify the
348 * fast timer list, but any time we hold a reference to a
349 * timer in the list, since del_fast_timer may be called
350 * from (another) interrupt context. Thus, the only time
351 * when interrupts are enabled is when calling the timer
352 * callback function.
353 */
Mikael Starvik7e920422005-07-27 11:44:34 -0700354 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Clear timer1 irq */
357 *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
358
359 /* First stop timer, then ack interrupt */
360 /* Stop timer */
361 *R_TIMER_CTRL = r_timer_ctrl_shadow =
362 (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
363 IO_STATE(R_TIMER_CTRL, tm1, stop_ld);
364
365 /* Ack interrupt */
366 *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr);
367
368 fast_timer_running = 0;
369 fast_timer_ints++;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 t = fast_timer_list;
372 while (t)
373 {
Jesper Nilssond8e52192007-11-14 17:01:23 -0800374 struct fasttime_t tv;
375 fast_timer_function_type *f;
376 unsigned long d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* Has it really expired? */
379 do_gettimeofday_fast(&tv);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800380 D1(printk(KERN_DEBUG "t: %is %06ius\n",
381 tv.tv_jiff, tv.tv_usec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800383 if (fasttime_cmp(&t->tv_expires, &tv) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 {
385 /* Yes it has expired */
386#ifdef FAST_TIMER_LOG
387 timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;
388#endif
389 fast_timers_expired++;
390
391 /* Remove this timer before call, since it may reuse the timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (t->prev)
393 {
394 t->prev->next = t->next;
395 }
396 else
397 {
398 fast_timer_list = t->next;
399 }
400 if (t->next)
401 {
402 t->next->prev = t->prev;
403 }
404 t->prev = NULL;
405 t->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jesper Nilssond8e52192007-11-14 17:01:23 -0800407 /* Save function callback data before enabling
408 * interrupts, since the timer may be removed and
409 * we don't know how it was allocated
410 * (e.g. ->function and ->data may become overwritten
411 * after deletion if the timer was stack-allocated).
412 */
413 f = t->function;
414 d = t->data;
415
416 if (f != NULL) {
417 /* Run callback with interrupts enabled. */
418 local_irq_restore(flags);
419 f(d);
420 local_irq_save(flags);
421 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 else
425 {
426 /* Timer is to early, let's set it again using the normal routines */
427 D1(printk(".\n"));
428 }
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if ((t = fast_timer_list) != NULL)
431 {
432 /* Start next timer.. */
Jesper Nilssond8e52192007-11-14 17:01:23 -0800433 long us = 0;
434 struct fasttime_t tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 do_gettimeofday_fast(&tv);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800437
438 /* time_after_eq takes care of wrapping */
439 if (time_after_eq(t->tv_expires.tv_jiff, tv.tv_jiff))
440 us = ((t->tv_expires.tv_jiff - tv.tv_jiff) *
441 1000000 / HZ + t->tv_expires.tv_usec -
442 tv.tv_usec);
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (us > 0)
445 {
446 if (!fast_timer_running)
447 {
448#ifdef FAST_TIMER_LOG
449 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
450#endif
451 start_timer1(us);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 }
455 else
456 {
457 /* Timer already expired, let's handle it better late than never.
458 * The normal loop handles it
459 */
460 D1(printk("e! %d\n", us));
461 }
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
Jesper Nilssond8e52192007-11-14 17:01:23 -0800465 local_irq_restore(flags);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!t)
468 {
469 D1(printk("t1 stop!\n"));
470 }
471
472 return IRQ_HANDLED;
473}
474
475static void wake_up_func(unsigned long data)
476{
477#ifdef DECLARE_WAITQUEUE
478 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
479#else
480 struct wait_queue **sleep_wait_p = (struct wait_queue **)data;
481#endif
482 wake_up(sleep_wait_p);
483}
484
485
486/* Useful API */
487
488void schedule_usleep(unsigned long us)
489{
490 struct fast_timer t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 wait_queue_head_t sleep_wait;
492 init_waitqueue_head(&sleep_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 D1(printk("schedule_usleep(%d)\n", us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
496 "usleep");
Jesper Nilssond8e52192007-11-14 17:01:23 -0800497 /* Uninterruptible sleep on the fast timer. (The condition is somewhat
498 * redundant since the timer is what wakes us up.) */
499 wait_event(sleep_wait, !fast_timer_pending(&t));
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 D1(printk("done schedule_usleep(%d)\n", us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504#ifdef CONFIG_PROC_FS
505static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
506 ,int *eof, void *data_unused);
507static struct proc_dir_entry *fasttimer_proc_entry;
508#endif /* CONFIG_PROC_FS */
509
510#ifdef CONFIG_PROC_FS
511
512/* This value is very much based on testing */
513#define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
514
515static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
516 ,int *eof, void *data_unused)
517{
518 unsigned long flags;
519 int i = 0;
520 int num_to_show;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800521 struct fasttime_t tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct fast_timer *t, *nextt;
523 static char *bigbuf = NULL;
524 static unsigned long used;
525
526 if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE)))
527 {
528 used = 0;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800529 if (buf)
530 buf[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532 }
533
534 if (!offset || !used)
535 {
536 do_gettimeofday_fast(&tv);
537
538 used = 0;
539 used += sprintf(bigbuf + used, "Fast timers added: %i\n",
540 fast_timers_added);
541 used += sprintf(bigbuf + used, "Fast timers started: %i\n",
542 fast_timers_started);
543 used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
544 fast_timer_ints);
545 used += sprintf(bigbuf + used, "Fast timers expired: %i\n",
546 fast_timers_expired);
547 used += sprintf(bigbuf + used, "Fast timers deleted: %i\n",
548 fast_timers_deleted);
549 used += sprintf(bigbuf + used, "Fast timer running: %s\n",
550 fast_timer_running ? "yes" : "no");
551 used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n",
Jesper Nilssond8e52192007-11-14 17:01:23 -0800552 (unsigned long)tv.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 (unsigned long)tv.tv_usec);
554#ifdef FAST_TIMER_SANITY_CHECKS
555 used += sprintf(bigbuf + used, "Sanity failed: %i\n",
556 sanity_failed);
557#endif
558 used += sprintf(bigbuf + used, "\n");
559
560#ifdef DEBUG_LOG_INCLUDED
561 {
562 int end_i = debug_log_cnt;
563 i = 0;
564
565 if (debug_log_cnt_wrapped)
566 {
567 i = debug_log_cnt;
568 }
569
570 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
571 used+100 < BIG_BUF_SIZE)
572 {
573 used += sprintf(bigbuf + used, debug_log_string[i],
574 debug_log_value[i]);
575 i = (i+1) % DEBUG_LOG_MAX;
576 }
577 }
578 used += sprintf(bigbuf + used, "\n");
579#endif
580
581 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
582 NUM_TIMER_STATS);
583 used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
584 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
585 {
586 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
587
588#if 1 //ndef FAST_TIMER_LOG
589 used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i"
590 "\n",
591 timer_div_settings[cur],
592 timer_freq_settings[cur],
593 timer_delay_settings[cur]
594 );
595#endif
596#ifdef FAST_TIMER_LOG
597 t = &timer_started_log[cur];
598 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
599 "d: %6li us data: 0x%08lX"
600 "\n",
601 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800602 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800604 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 (unsigned long)t->tv_expires.tv_usec,
606 t->delay_us,
607 t->data
608 );
609#endif
610 }
611 used += sprintf(bigbuf + used, "\n");
612
613#ifdef FAST_TIMER_LOG
614 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
615 NUM_TIMER_STATS);
616 used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
617 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
618 {
619 t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
620 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
621 "d: %6li us data: 0x%08lX"
622 "\n",
623 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800624 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800626 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 (unsigned long)t->tv_expires.tv_usec,
628 t->delay_us,
629 t->data
630 );
631 }
632 used += sprintf(bigbuf + used, "\n");
633
634 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
635 NUM_TIMER_STATS);
636 used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
637 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
638 {
639 t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
640 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
641 "d: %6li us data: 0x%08lX"
642 "\n",
643 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800644 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800646 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 (unsigned long)t->tv_expires.tv_usec,
648 t->delay_us,
649 t->data
650 );
651 }
652 used += sprintf(bigbuf + used, "\n");
653#endif
654
655 used += sprintf(bigbuf + used, "Active timers:\n");
Mikael Starvik7e920422005-07-27 11:44:34 -0700656 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 t = fast_timer_list;
658 while (t != NULL && (used+100 < BIG_BUF_SIZE))
659 {
660 nextt = t->next;
Mikael Starvik7e920422005-07-27 11:44:34 -0700661 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
663 "d: %6li us data: 0x%08lX"
664/* " func: 0x%08lX" */
665 "\n",
666 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800667 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800669 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 (unsigned long)t->tv_expires.tv_usec,
671 t->delay_us,
672 t->data
673/* , t->function */
674 );
Jesper Nilssond8e52192007-11-14 17:01:23 -0800675 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (t->next != nextt)
677 {
678 printk(KERN_WARNING "timer removed!\n");
679 }
680 t = nextt;
681 }
Mikael Starvik7e920422005-07-27 11:44:34 -0700682 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (used - offset < len)
686 {
687 len = used - offset;
688 }
689
690 memcpy(buf, bigbuf + offset, len);
691 *start = buf;
692 *eof = 1;
693
694 return len;
695}
696#endif /* PROC_FS */
697
698#ifdef FAST_TIMER_TEST
699static volatile unsigned long i = 0;
700static volatile int num_test_timeout = 0;
701static struct fast_timer tr[10];
702static int exp_num[10];
703
Jesper Nilssond8e52192007-11-14 17:01:23 -0800704static struct fasttime_t tv_exp[100];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706static void test_timeout(unsigned long data)
707{
708 do_gettimeofday_fast(&tv_exp[data]);
709 exp_num[data] = num_test_timeout;
710
711 num_test_timeout++;
712}
713
714static void test_timeout1(unsigned long data)
715{
716 do_gettimeofday_fast(&tv_exp[data]);
717 exp_num[data] = num_test_timeout;
718 if (data < 7)
719 {
720 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
721 i++;
722 }
723 num_test_timeout++;
724}
725
726DP(
727static char buf0[2000];
728static char buf1[2000];
729static char buf2[2000];
730static char buf3[2000];
731static char buf4[2000];
732);
733
734static char buf5[6000];
735static int j_u[1000];
736
737static void fast_timer_test(void)
738{
739 int prev_num;
740 int j;
741
Jesper Nilssond8e52192007-11-14 17:01:23 -0800742 struct fasttime_t tv, tv0, tv1, tv2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 printk("fast_timer_test() start\n");
745 do_gettimeofday_fast(&tv);
746
747 for (j = 0; j < 1000; j++)
748 {
749 j_u[j] = GET_JIFFIES_USEC();
750 }
751 for (j = 0; j < 100; j++)
752 {
753 do_gettimeofday_fast(&tv_exp[j]);
754 }
Jesper Nilssond8e52192007-11-14 17:01:23 -0800755 printk(KERN_DEBUG "fast_timer_test() %is %06i\n",
756 tv.tv_jiff, tv.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 for (j = 0; j < 1000; j++)
759 {
760 printk("%i %i %i %i %i\n",j_u[j], j_u[j+1], j_u[j+2], j_u[j+3], j_u[j+4]);
761 j += 4;
762 }
763 for (j = 0; j < 100; j++)
764 {
Jesper Nilssond8e52192007-11-14 17:01:23 -0800765 printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
766 tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
767 tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
768 tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
769 tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
770 tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 j += 4;
772 }
773 do_gettimeofday_fast(&tv0);
774 start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
775 DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
776 i++;
777 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
778 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
779 i++;
780 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
781 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
782 i++;
783 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
784 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
785 i++;
786 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
787 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
788 i++;
789 do_gettimeofday_fast(&tv1);
790
791 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
792
793 prev_num = num_test_timeout;
794 while (num_test_timeout < i)
795 {
796 if (num_test_timeout != prev_num)
797 {
798 prev_num = num_test_timeout;
799 }
800 }
801 do_gettimeofday_fast(&tv2);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800802 printk(KERN_DEBUG "Timers started %is %06i\n",
803 tv0.tv_jiff, tv0.tv_usec);
804 printk(KERN_DEBUG "Timers started at %is %06i\n",
805 tv1.tv_jiff, tv1.tv_usec);
806 printk(KERN_DEBUG "Timers done %is %06i\n",
807 tv2.tv_jiff, tv2.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 DP(printk("buf0:\n");
809 printk(buf0);
810 printk("buf1:\n");
811 printk(buf1);
812 printk("buf2:\n");
813 printk(buf2);
814 printk("buf3:\n");
815 printk(buf3);
816 printk("buf4:\n");
817 printk(buf4);
818 );
819 printk("buf5:\n");
820 printk(buf5);
821
822 printk("timers set:\n");
823 for(j = 0; j<i; j++)
824 {
825 struct fast_timer *t = &tr[j];
826 printk("%-10s set: %6is %06ius exp: %6is %06ius "
827 "data: 0x%08X func: 0x%08X\n",
828 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800829 t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800831 t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 t->tv_expires.tv_usec,
833 t->data,
834 t->function
835 );
836
837 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
838 t->delay_us,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800839 tv_exp[j].tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 tv_exp[j].tv_usec,
841 exp_num[j],
Jesper Nilssond8e52192007-11-14 17:01:23 -0800842 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
843 1000000 + tv_exp[j].tv_usec -
844 t->tv_expires.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
847 printk("buf5 after all done:\n");
848 printk(buf5);
849 printk("fast_timer_test() done\n");
850}
851#endif
852
853
Jesper Nilssond8e52192007-11-14 17:01:23 -0800854int fast_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 /* For some reason, request_irq() hangs when called froom time_init() */
857 if (!fast_timer_is_init)
858 {
859#if 0 && defined(FAST_TIMER_TEST)
860 int i;
861#endif
862
863 printk(KERN_INFO "fast_timer_init()\n");
864
865#if 0 && defined(FAST_TIMER_TEST)
866 for (i = 0; i <= TIMER0_DIV; i++)
867 {
868 /* We must be careful not to get overflow... */
869 printk("%3i %6u\n", i, timer0_value_us[i]);
870 }
871#endif
872#ifdef CONFIG_PROC_FS
873 if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 )))
874 fasttimer_proc_entry->read_proc = proc_fasttimer_read;
875#endif /* PROC_FS */
Mikael Starvik7e920422005-07-27 11:44:34 -0700876 if(request_irq(TIMER1_IRQ_NBR, timer1_handler, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 "fast timer int", NULL))
878 {
879 printk("err: timer1 irq\n");
880 }
881 fast_timer_is_init = 1;
882#ifdef FAST_TIMER_TEST
883 printk("do test\n");
884 fast_timer_test();
885#endif
886 }
Jesper Nilssond8e52192007-11-14 17:01:23 -0800887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
Jesper Nilssond8e52192007-11-14 17:01:23 -0800889__initcall(fast_timer_init);