ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / kernel / fasttimer.c
1 /* $Id: fasttimer.c,v 1.4 2003/07/04 08:27:41 starvik Exp $
2  * linux/arch/cris/kernel/fasttimer.c
3  *
4  * Fast timers for ETRAX100/ETRAX100LX
5  * This may be useful in other OS than Linux so use 2 space indentation...
6  *
7  * $Log: fasttimer.c,v $
8  * Revision 1.4  2003/07/04 08:27:41  starvik
9  * Merge of Linux 2.5.74
10  *
11  * Revision 1.3  2002/12/12 08:26:32  starvik
12  * Don't use C-comments inside CVS comments
13  *
14  * Revision 1.2  2002/12/11 15:42:02  starvik
15  * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/
16  *
17  * Revision 1.1  2002/11/18 07:58:06  starvik
18  * Fast timers (from Linux 2.4)
19  *
20  * Revision 1.5  2002/10/15 06:21:39  starvik
21  * Added call to init_waitqueue_head
22  *
23  * Revision 1.4  2002/05/28 17:47:59  johana
24  * Added del_fast_timer()
25  *
26  * Revision 1.3  2002/05/28 16:16:07  johana
27  * Handle empty fast_timer_list
28  *
29  * Revision 1.2  2002/05/27 15:38:42  johana
30  * Made it compile without warnings on Linux 2.4.
31  * (includes, wait_queue, PROC_FS and snprintf)
32  *
33  * Revision 1.1  2002/05/27 15:32:25  johana
34  * arch/etrax100/kernel/fasttimer.c v1.8 from the elinux tree.
35  *
36  * Revision 1.8  2001/11/27 13:50:40  pkj
37  * Disable interrupts while stopping the timer and while modifying the
38  * list of active timers in timer1_handler() as it may be interrupted
39  * by other interrupts (e.g., the serial interrupt) which may add fast
40  * timers.
41  *
42  * Revision 1.7  2001/11/22 11:50:32  pkj
43  * * Only store information about the last 16 timers.
44  * * proc_fasttimer_read() now uses an allocated buffer, since it
45  *   requires more space than just a page even for only writing the
46  *   last 16 timers. The buffer is only allocated on request, so
47  *   unless /proc/fasttimer is read, it is never allocated.
48  * * Renamed fast_timer_started to fast_timers_started to match
49  *   fast_timers_added and fast_timers_expired.
50  * * Some clean-up.
51  *
52  * Revision 1.6  2000/12/13 14:02:08  johana
53  * Removed volatile for fast_timer_list
54  *
55  * Revision 1.5  2000/12/13 13:55:35  johana
56  * Added DEBUG_LOG, added som cli() and cleanup
57  *
58  * Revision 1.4  2000/12/05 13:48:50  johana
59  * Added range check when writing proc file, modified timer int handling
60  *
61  * Revision 1.3  2000/11/23 10:10:20  johana
62  * More debug/logging possibilities.
63  * Moved GET_JIFFIES_USEC() to timex.h and time.c
64  *
65  * Revision 1.2  2000/11/01 13:41:04  johana
66  * Clean up and bugfixes.
67  * Created new do_gettimeofday_fast() that gets a timeval struct
68  * with time based on jiffies and *R_TIMER0_DATA, uses a table
69  * for fast conversion of timer value to microseconds.
70  * (Much faster the standard do_gettimeofday() and we don't really
71  * wan't to use the true time - we wan't the "uptime" so timers don't screw up
72  * when we change the time.
73  * TODO: Add efficient support for continuous timers as well.
74  *
75  * Revision 1.1  2000/10/26 15:49:16  johana
76  * Added fasttimer, highresolution timers.
77  *
78  * Copyright (C) 2000,2001 2002 Axis Communications AB, Lund, Sweden
79  */
80
81 #include <linux/errno.h>
82 #include <linux/sched.h>
83 #include <linux/kernel.h>
84 #include <linux/param.h>
85 #include <linux/string.h>
86 #include <linux/mm.h>
87 #include <linux/vmalloc.h>
88 #include <linux/interrupt.h>
89 #include <linux/time.h>
90 #include <linux/delay.h>
91
92 #include <asm/segment.h>
93 #include <asm/io.h>
94 #include <asm/irq.h>
95 #include <asm/delay.h>
96 #include <asm/rtc.h>
97
98 #include <linux/config.h>
99 #include <linux/version.h>
100
101 #include <asm/arch/svinto.h>
102 #include <asm/fasttimer.h>
103 #include <linux/proc_fs.h>
104
105
106 #define DEBUG_LOG_INCLUDED
107 #define FAST_TIMER_LOG
108 //#define FAST_TIMER_TEST
109
110 #define FAST_TIMER_SANITY_CHECKS
111
112 #ifdef FAST_TIMER_SANITY_CHECKS
113 #define SANITYCHECK(x) x
114 static int sanity_failed = 0;
115 #else
116 #define SANITYCHECK(x)
117 #endif
118
119 #define D1(x)
120 #define D2(x)
121 #define DP(x)
122
123 #define __INLINE__ inline
124
125 static int fast_timer_running = 0;
126 static int fast_timers_added = 0;
127 static int fast_timers_started = 0;
128 static int fast_timers_expired = 0;
129 static int fast_timers_deleted = 0;
130 static int fast_timer_is_init = 0;
131 static int fast_timer_ints = 0;
132
133 static struct fast_timer *fast_timer_list = NULL;
134
135 #ifdef DEBUG_LOG_INCLUDED
136 #define DEBUG_LOG_MAX 128
137 static const char * debug_log_string[DEBUG_LOG_MAX];
138 static unsigned long debug_log_value[DEBUG_LOG_MAX];
139 static int debug_log_cnt = 0;
140 static int debug_log_cnt_wrapped = 0;
141
142 #define DEBUG_LOG(string, value) \
143 { \
144   unsigned long log_flags; \
145   save_flags(log_flags); \
146   cli(); \
147   debug_log_string[debug_log_cnt] = (string); \
148   debug_log_value[debug_log_cnt] = (unsigned long)(value); \
149   if (++debug_log_cnt >= DEBUG_LOG_MAX) \
150   { \
151     debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
152     debug_log_cnt_wrapped = 1; \
153   } \
154   restore_flags(log_flags); \
155 }
156 #else
157 #define DEBUG_LOG(string, value)
158 #endif
159
160
161 /* The frequencies for index = clkselx number in R_TIMER_CTRL */
162 #define NUM_TIMER_FREQ 15
163 #define MAX_USABLE_TIMER_FREQ 7
164 #define MAX_DELAY_US  853333L
165 const unsigned long timer_freq_100[NUM_TIMER_FREQ] =
166 {
167   3,   /* 0 3333 - 853333 us */
168   6,   /* 1 1666 - 426666 us */
169   12,  /* 2  833 - 213333 us */
170   24,  /* 3  416 - 106666 us */
171   48,  /* 4  208 -  53333 us */
172   96,  /* 5  104 -  26666 us */
173   192, /* 6   52 -  13333 us */
174   384, /* 7   26 -   6666 us */
175   576,
176   1152,
177   2304,
178   4608,
179   9216,
180   18432,
181   62500,
182   /* 15 = cascade */
183 };
184 #define NUM_TIMER_STATS 16
185 #ifdef FAST_TIMER_LOG
186 struct fast_timer timer_added_log[NUM_TIMER_STATS];
187 struct fast_timer timer_started_log[NUM_TIMER_STATS];
188 struct fast_timer timer_expired_log[NUM_TIMER_STATS];
189 #endif
190
191 int timer_div_settings[NUM_TIMER_STATS];
192 int timer_freq_settings[NUM_TIMER_STATS];
193 int timer_delay_settings[NUM_TIMER_STATS];
194
195 /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
196 void __INLINE__ do_gettimeofday_fast(struct timeval *tv)
197 {
198   unsigned long sec = jiffies;
199   unsigned long usec = GET_JIFFIES_USEC();
200
201   usec += (sec % HZ) * (1000000 / HZ);
202   sec = sec / HZ;
203
204   if (usec > 1000000)
205   {
206     usec -= 1000000;
207     sec++;
208   }
209   tv->tv_sec = sec;
210   tv->tv_usec = usec;
211 }
212
213 int __INLINE__ timeval_cmp(struct timeval *t0, struct timeval *t1)
214 {
215   if (t0->tv_sec < t1->tv_sec)
216   {
217     return -1;
218   }
219   else if (t0->tv_sec > t1->tv_sec)
220   {
221     return 1;
222   }
223   if (t0->tv_usec < t1->tv_usec)
224   {
225     return -1;
226   }
227   else if (t0->tv_usec > t1->tv_usec)
228   {
229     return 1;
230   }
231   return 0;
232 }
233
234 void __INLINE__ start_timer1(unsigned long delay_us)
235 {
236   int freq_index = 0; /* This is the lowest resolution */
237   unsigned long upper_limit = MAX_DELAY_US;
238
239   unsigned long div;
240   /* Start/Restart the timer to the new shorter value */
241   /* t = 1/freq = 1/19200 = 53us
242    * T=div*t,  div = T/t = delay_us*freq/1000000
243    */
244 #if 1 /* Adaptive timer settings */
245   while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ)
246   {
247     freq_index++;
248     upper_limit >>= 1; /* Divide by 2 using shift */
249   }
250   if (freq_index > 0)
251   {
252     freq_index--;
253   }
254 #else
255   freq_index = 6;
256 #endif
257   div = delay_us * timer_freq_100[freq_index]/10000;
258   if (div < 2)
259   {
260     /* Maybe increase timer freq? */
261     div = 2;
262   }
263   if (div > 255)
264   {
265     div = 0; /* This means 256, the max the timer takes */
266     /* If a longer timeout than the timer can handle is used,
267      * then we must restart it when it goes off.
268      */
269   }
270
271   timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div;
272   timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index;
273   timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
274
275   D1(printk("start_timer1 : %d us freq: %i div: %i\n",
276             delay_us, freq_index, div));
277   /* Clear timer1 irq */
278   *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
279
280   /* Set timer values */
281   *R_TIMER_CTRL = r_timer_ctrl_shadow =
282     (r_timer_ctrl_shadow &
283      ~IO_MASK(R_TIMER_CTRL, timerdiv1) &
284      ~IO_MASK(R_TIMER_CTRL, tm1) &
285      ~IO_MASK(R_TIMER_CTRL, clksel1)) |
286     IO_FIELD(R_TIMER_CTRL, timerdiv1, div) |
287     IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
288     IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */
289
290   /* Ack interrupt */
291   *R_TIMER_CTRL =  r_timer_ctrl_shadow |
292     IO_STATE(R_TIMER_CTRL, i1, clr);
293
294   /* Start timer */
295   *R_TIMER_CTRL = r_timer_ctrl_shadow =
296     (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
297     IO_STATE(R_TIMER_CTRL, tm1, run);
298
299   /* Enable timer1 irq */
300   *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set);
301   fast_timers_started++;
302   fast_timer_running = 1;
303 }
304
305 /* In version 1.4 this function takes 27 - 50 us */
306 void start_one_shot_timer(struct fast_timer *t,
307                           fast_timer_function_type *function,
308                           unsigned long data,
309                           unsigned long delay_us,
310                           const char *name)
311 {
312   unsigned long flags;
313   struct fast_timer *tmp;
314
315   D1(printk("sft %s %d us\n", name, delay_us));
316
317   save_flags(flags);
318   cli();
319
320   do_gettimeofday_fast(&t->tv_set);
321   tmp = fast_timer_list;
322
323   SANITYCHECK({ /* Check so this is not in the list already... */
324     while (tmp != NULL)
325     {
326       if (tmp == t)
327       {
328         printk("timer name: %s data: 0x%08lX already in list!\n", name, data);
329         sanity_failed++;
330         return;
331       }
332       else
333       {
334         tmp = tmp->next;
335       }
336     }
337     tmp = fast_timer_list;
338   });
339
340   t->delay_us = delay_us;
341   t->function = function;
342   t->data = data;
343   t->name = name;
344
345   t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
346   t->tv_expires.tv_sec  = t->tv_set.tv_sec  + delay_us / 1000000;
347   if (t->tv_expires.tv_usec > 1000000)
348   {
349     t->tv_expires.tv_usec -= 1000000;
350     t->tv_expires.tv_sec++;
351   }
352 #ifdef FAST_TIMER_LOG
353   timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
354 #endif
355   fast_timers_added++;
356
357   /* Check if this should timeout before anything else */
358   if (tmp == NULL || timeval_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
359   {
360     /* Put first in list and modify the timer value */
361     t->prev = NULL;
362     t->next = fast_timer_list;
363     if (fast_timer_list)
364     {
365       fast_timer_list->prev = t;
366     }
367     fast_timer_list = t;
368 #ifdef FAST_TIMER_LOG
369     timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
370 #endif
371     start_timer1(delay_us);
372   } else {
373     /* Put in correct place in list */
374     while (tmp->next &&
375            timeval_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0)
376     {
377       tmp = tmp->next;
378     }
379     /* Insert t after tmp */
380     t->prev = tmp;
381     t->next = tmp->next;
382     if (tmp->next)
383     {
384       tmp->next->prev = t;
385     }
386     tmp->next = t;
387   }
388
389   D2(printk("start_one_shot_timer: %d us done\n", delay_us));
390
391   restore_flags(flags);
392 } /* start_one_shot_timer */
393
394 static inline int fast_timer_pending (const struct fast_timer * t)
395 {
396   return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
397 }
398
399 static inline int detach_fast_timer (struct fast_timer *t)
400 {
401   struct fast_timer *next, *prev;
402   if (!fast_timer_pending(t))
403     return 0;
404   next = t->next;
405   prev = t->prev;
406   if (next)
407     next->prev = prev;
408   if (prev)
409     prev->next = next;
410   else
411     fast_timer_list = next;
412   fast_timers_deleted++;
413   return 1;
414 }
415
416 int del_fast_timer(struct fast_timer * t)
417 {
418   unsigned long flags;
419   int ret;
420   
421   save_flags(flags);
422   cli();
423   ret = detach_fast_timer(t);
424   t->next = t->prev = NULL;
425   restore_flags(flags);
426   return ret;
427 } /* del_fast_timer */
428
429
430 /* Interrupt routines or functions called in interrupt context */
431
432 /* Timer 1 interrupt handler */
433
434 static irqreturn_t
435 timer1_handler(int irq, void *dev_id, struct pt_regs *regs)
436 {
437   struct fast_timer *t;
438   unsigned long flags;
439
440   save_flags(flags);
441   cli();
442
443   /* Clear timer1 irq */
444   *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
445
446   /* First stop timer, then ack interrupt */
447   /* Stop timer */
448   *R_TIMER_CTRL = r_timer_ctrl_shadow =
449     (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
450     IO_STATE(R_TIMER_CTRL, tm1, stop_ld);
451
452   /* Ack interrupt */
453   *R_TIMER_CTRL =  r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr);
454
455   fast_timer_running = 0;
456   fast_timer_ints++;
457
458   restore_flags(flags);
459
460   t = fast_timer_list;
461   while (t)
462   {
463     struct timeval tv;
464
465     /* Has it really expired? */
466     do_gettimeofday_fast(&tv);
467     D1(printk("t: %is %06ius\n", tv.tv_sec, tv.tv_usec));
468
469     if (timeval_cmp(&t->tv_expires, &tv) <= 0)
470     {
471       /* Yes it has expired */
472 #ifdef FAST_TIMER_LOG
473       timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;
474 #endif
475       fast_timers_expired++;
476
477       /* Remove this timer before call, since it may reuse the timer */
478       save_flags(flags);
479       cli();
480       if (t->prev)
481       {
482         t->prev->next = t->next;
483       }
484       else
485       {
486         fast_timer_list = t->next;
487       }
488       if (t->next)
489       {
490         t->next->prev = t->prev;
491       }
492       t->prev = NULL;
493       t->next = NULL;
494       restore_flags(flags);
495
496       if (t->function != NULL)
497       {
498         t->function(t->data);
499       }
500       else
501       {
502         DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints);
503       }
504     }
505     else
506     {
507       /* Timer is to early, let's set it again using the normal routines */
508       D1(printk(".\n"));
509     }
510
511     save_flags(flags);
512     cli();
513     if ((t = fast_timer_list) != NULL)
514     {
515       /* Start next timer.. */
516       long us;
517       struct timeval tv;
518
519       do_gettimeofday_fast(&tv);
520       us = ((t->tv_expires.tv_sec - tv.tv_sec) * 1000000 +
521             t->tv_expires.tv_usec - tv.tv_usec);
522       if (us > 0)
523       {
524         if (!fast_timer_running)
525         {
526 #ifdef FAST_TIMER_LOG
527           timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
528 #endif
529           start_timer1(us);
530         }
531         restore_flags(flags);
532         break;
533       }
534       else
535       {
536         /* Timer already expired, let's handle it better late than never.
537          * The normal loop handles it
538          */
539         D1(printk("e! %d\n", us));
540       }
541     }
542     restore_flags(flags);
543   }
544
545   if (!t)
546   {
547     D1(printk("t1 stop!\n"));
548   }
549
550   return IRQ_HANDLED;
551 }
552
553 static void wake_up_func(unsigned long data)
554 {
555 #ifdef DECLARE_WAITQUEUE
556   wait_queue_head_t  *sleep_wait_p = (wait_queue_head_t*)data;
557 #else
558   struct wait_queue **sleep_wait_p = (struct wait_queue **)data;
559 #endif
560   wake_up(sleep_wait_p);
561 }
562
563
564 /* Useful API */
565
566 void schedule_usleep(unsigned long us)
567 {
568   struct fast_timer t;
569 #ifdef DECLARE_WAITQUEUE
570   wait_queue_head_t sleep_wait;
571   init_waitqueue_head(&sleep_wait);
572   {
573   DECLARE_WAITQUEUE(wait, current);
574 #else
575   struct wait_queue *sleep_wait = NULL;
576   struct wait_queue wait = { current, NULL };
577 #endif
578
579   D1(printk("schedule_usleep(%d)\n", us));
580   add_wait_queue(&sleep_wait, &wait);
581   set_current_state(TASK_INTERRUPTIBLE);
582   start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
583                        "usleep");
584   schedule();
585   set_current_state(TASK_RUNNING);
586   remove_wait_queue(&sleep_wait, &wait);
587   D1(printk("done schedule_usleep(%d)\n", us));
588 #ifdef DECLARE_WAITQUEUE
589   }
590 #endif  
591 }
592
593 #ifdef CONFIG_PROC_FS
594 static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
595 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
596                        ,int *eof, void *data_unused
597 #else
598                         ,int unused
599 #endif
600                                );
601 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
602 static struct proc_dir_entry *fasttimer_proc_entry;
603 #else
604 static struct proc_dir_entry fasttimer_proc_entry =
605 {
606   0, 9, "fasttimer",
607   S_IFREG | S_IRUGO, 1, 0, 0,
608   0, NULL /* ops -- default to array */,
609   &proc_fasttimer_read /* get_info */,
610 };
611 #endif
612 #endif /* CONFIG_PROC_FS */
613
614 #ifdef CONFIG_PROC_FS
615
616 /* This value is very much based on testing */
617 #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
618
619 static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
620 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
621                        ,int *eof, void *data_unused
622 #else
623                         ,int unused
624 #endif
625                                )
626 {
627   unsigned long flags;
628   int i = 0;
629   int num_to_show;
630   struct timeval tv;
631   struct fast_timer *t, *nextt;
632   static char *bigbuf = NULL;
633   static unsigned long used;
634
635   if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE)))
636   {
637     used = 0;
638     bigbuf[0] = '\0';
639     return 0;
640   }
641
642   if (!offset || !used)
643   {
644     do_gettimeofday_fast(&tv);
645
646     used = 0;
647     used += sprintf(bigbuf + used, "Fast timers added:     %i\n",
648                     fast_timers_added);
649     used += sprintf(bigbuf + used, "Fast timers started:   %i\n",
650                     fast_timers_started);
651     used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
652                     fast_timer_ints);
653     used += sprintf(bigbuf + used, "Fast timers expired:   %i\n",
654                     fast_timers_expired);
655     used += sprintf(bigbuf + used, "Fast timers deleted:   %i\n",
656                     fast_timers_deleted);
657     used += sprintf(bigbuf + used, "Fast timer running:    %s\n",
658                     fast_timer_running ? "yes" : "no");
659     used += sprintf(bigbuf + used, "Current time:          %lu.%06lu\n",
660                     (unsigned long)tv.tv_sec,
661                     (unsigned long)tv.tv_usec);
662 #ifdef FAST_TIMER_SANITY_CHECKS
663     used += sprintf(bigbuf + used, "Sanity failed:         %i\n",
664                     sanity_failed);
665 #endif
666     used += sprintf(bigbuf + used, "\n");
667
668 #ifdef DEBUG_LOG_INCLUDED
669     {
670       int end_i = debug_log_cnt;
671       i = 0;
672
673       if (debug_log_cnt_wrapped)
674       {
675         i = debug_log_cnt;
676       }
677
678       while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
679              used+100 < BIG_BUF_SIZE)
680       {
681         used += sprintf(bigbuf + used, debug_log_string[i],
682                         debug_log_value[i]);
683         i = (i+1) % DEBUG_LOG_MAX;
684       }
685     }
686     used += sprintf(bigbuf + used, "\n");
687 #endif
688
689     num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
690                    NUM_TIMER_STATS);
691     used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
692     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
693     {
694       int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
695
696 #if 1 //ndef FAST_TIMER_LOG
697       used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i"
698                       "\n",
699                       timer_div_settings[cur],
700                       timer_freq_settings[cur],
701                       timer_delay_settings[cur]
702                       );
703 #endif
704 #ifdef FAST_TIMER_LOG
705       t = &timer_started_log[cur];
706       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
707                       "d: %6li us data: 0x%08lX"
708                       "\n",
709                       t->name,
710                       (unsigned long)t->tv_set.tv_sec,
711                       (unsigned long)t->tv_set.tv_usec,
712                       (unsigned long)t->tv_expires.tv_sec,
713                       (unsigned long)t->tv_expires.tv_usec,
714                       t->delay_us,
715                       t->data
716                       );
717 #endif
718     }
719     used += sprintf(bigbuf + used, "\n");
720
721 #ifdef FAST_TIMER_LOG
722     num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
723                    NUM_TIMER_STATS);
724     used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
725     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
726     {
727       t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
728       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
729                       "d: %6li us data: 0x%08lX"
730                       "\n",
731                       t->name,
732                       (unsigned long)t->tv_set.tv_sec,
733                       (unsigned long)t->tv_set.tv_usec,
734                       (unsigned long)t->tv_expires.tv_sec,
735                       (unsigned long)t->tv_expires.tv_usec,
736                       t->delay_us,
737                       t->data
738                       );
739     }
740     used += sprintf(bigbuf + used, "\n");
741
742     num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
743                    NUM_TIMER_STATS);
744     used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
745     for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
746     {
747       t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
748       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
749                       "d: %6li us data: 0x%08lX"
750                       "\n",
751                       t->name,
752                       (unsigned long)t->tv_set.tv_sec,
753                       (unsigned long)t->tv_set.tv_usec,
754                       (unsigned long)t->tv_expires.tv_sec,
755                       (unsigned long)t->tv_expires.tv_usec,
756                       t->delay_us,
757                       t->data
758                       );
759     }
760     used += sprintf(bigbuf + used, "\n");
761 #endif
762
763     used += sprintf(bigbuf + used, "Active timers:\n");
764     save_flags(flags);
765     cli();
766     t = fast_timer_list;
767     while (t != NULL && (used+100 < BIG_BUF_SIZE))
768     {
769       nextt = t->next;
770       restore_flags(flags);
771       used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
772                       "d: %6li us data: 0x%08lX"
773 /*                      " func: 0x%08lX" */
774                       "\n",
775                       t->name,
776                       (unsigned long)t->tv_set.tv_sec,
777                       (unsigned long)t->tv_set.tv_usec,
778                       (unsigned long)t->tv_expires.tv_sec,
779                       (unsigned long)t->tv_expires.tv_usec,
780                       t->delay_us,
781                       t->data
782 /*                      , t->function */
783                       );
784       cli();
785       if (t->next != nextt)
786       {
787         printk("timer removed!\n");
788       }
789       t = nextt;
790     }
791     restore_flags(flags);
792   }
793
794   if (used - offset < len)
795   {
796     len = used - offset;
797   }
798
799   memcpy(buf, bigbuf + offset, len);
800   *start = buf;
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
802   *eof = 1;
803 #endif
804
805   return len;
806 }
807 #endif /* PROC_FS */
808
809 #ifdef FAST_TIMER_TEST
810 static volatile unsigned long i = 0;
811 static volatile int num_test_timeout = 0;
812 static struct fast_timer tr[10];
813 static int exp_num[10];
814
815 static struct timeval tv_exp[100];
816
817 static void test_timeout(unsigned long data)
818 {
819   do_gettimeofday_fast(&tv_exp[data]);
820   exp_num[data] = num_test_timeout;
821
822   num_test_timeout++;
823 }
824
825 static void test_timeout1(unsigned long data)
826 {
827   do_gettimeofday_fast(&tv_exp[data]);
828   exp_num[data] = num_test_timeout;
829   if (data < 7)
830   {
831     start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
832     i++;
833   }
834   num_test_timeout++;
835 }
836
837 DP(
838 static char buf0[2000];
839 static char buf1[2000];
840 static char buf2[2000];
841 static char buf3[2000];
842 static char buf4[2000];
843 );
844
845 static char buf5[6000];
846 static int j_u[1000];
847
848 static void fast_timer_test(void)
849 {
850   int prev_num;
851   int j;
852
853   struct timeval tv, tv0, tv1, tv2;
854
855   printk("fast_timer_test() start\n");
856   do_gettimeofday_fast(&tv);
857
858   for (j = 0; j < 1000; j++)
859   {
860     j_u[j] = GET_JIFFIES_USEC();
861   }
862   for (j = 0; j < 100; j++)
863   {
864     do_gettimeofday_fast(&tv_exp[j]);
865   }
866   printk("fast_timer_test() %is %06i\n", tv.tv_sec, tv.tv_usec);
867
868   for (j = 0; j < 1000; j++)
869   {
870     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]);
871     j += 4;
872   }
873   for (j = 0; j < 100; j++)
874   {
875     printk("%i.%i %i.%i %i.%i %i.%i %i.%i\n",
876            tv_exp[j].tv_sec,tv_exp[j].tv_usec,
877            tv_exp[j+1].tv_sec,tv_exp[j+1].tv_usec,
878            tv_exp[j+2].tv_sec,tv_exp[j+2].tv_usec,
879            tv_exp[j+3].tv_sec,tv_exp[j+3].tv_usec,
880            tv_exp[j+4].tv_sec,tv_exp[j+4].tv_usec);
881     j += 4;
882   }
883   do_gettimeofday_fast(&tv0);
884   start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
885   DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
886   i++;
887   start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
888   DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
889   i++;
890   start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
891   DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
892   i++;
893   start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
894   DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
895   i++;
896   start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
897   DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
898   i++;
899   do_gettimeofday_fast(&tv1);
900
901   proc_fasttimer_read(buf5, NULL, 0, 0, 0);
902
903   prev_num = num_test_timeout;
904   while (num_test_timeout < i)
905   {
906     if (num_test_timeout != prev_num)
907     {
908       prev_num = num_test_timeout;
909     }
910   }
911   do_gettimeofday_fast(&tv2);
912   printk("Timers started    %is %06i\n", tv0.tv_sec, tv0.tv_usec);
913   printk("Timers started at %is %06i\n", tv1.tv_sec, tv1.tv_usec);
914   printk("Timers done       %is %06i\n", tv2.tv_sec, tv2.tv_usec);
915   DP(printk("buf0:\n");
916      printk(buf0);
917      printk("buf1:\n");
918      printk(buf1);
919      printk("buf2:\n");
920      printk(buf2);
921      printk("buf3:\n");
922      printk(buf3);
923      printk("buf4:\n");
924      printk(buf4);
925   );
926   printk("buf5:\n");
927   printk(buf5);
928
929   printk("timers set:\n");
930   for(j = 0; j<i; j++)
931   {
932     struct fast_timer *t = &tr[j];
933     printk("%-10s set: %6is %06ius exp: %6is %06ius "
934            "data: 0x%08X func: 0x%08X\n",
935            t->name,
936            t->tv_set.tv_sec,
937            t->tv_set.tv_usec,
938            t->tv_expires.tv_sec,
939            t->tv_expires.tv_usec,
940            t->data,
941            t->function
942            );
943
944     printk("           del: %6ius     did exp: %6is %06ius as #%i error: %6li\n",
945            t->delay_us,
946            tv_exp[j].tv_sec,
947            tv_exp[j].tv_usec,
948            exp_num[j],
949            (tv_exp[j].tv_sec - t->tv_expires.tv_sec)*1000000 + tv_exp[j].tv_usec - t->tv_expires.tv_usec);
950   }
951   proc_fasttimer_read(buf5, NULL, 0, 0, 0);
952   printk("buf5 after all done:\n");
953   printk(buf5);
954   printk("fast_timer_test() done\n");
955 }
956 #endif
957
958
959 void fast_timer_init(void)
960 {
961   /* For some reason, request_irq() hangs when called froom time_init() */
962   if (!fast_timer_is_init)
963   {
964 #if 0 && defined(FAST_TIMER_TEST)
965     int i;
966 #endif
967
968     printk("fast_timer_init()\n");
969
970 #if 0 && defined(FAST_TIMER_TEST)
971     for (i = 0; i <= TIMER0_DIV; i++)
972     {
973       /* We must be careful not to get overflow... */
974       printk("%3i %6u\n", i, timer0_value_us[i]);
975     }
976 #endif
977 #ifdef CONFIG_PROC_FS
978 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
979    if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 )))
980      fasttimer_proc_entry->read_proc = proc_fasttimer_read;
981 #else
982     proc_register_dynamic(&proc_root, &fasttimer_proc_entry);
983 #endif
984 #endif /* PROC_FS */
985     if(request_irq(TIMER1_IRQ_NBR, timer1_handler, SA_SHIRQ,
986                    "fast timer int", NULL))
987     {
988       printk("err: timer1 irq\n");
989     }
990     fast_timer_is_init = 1;
991 #ifdef FAST_TIMER_TEST
992     printk("do test\n");
993     fast_timer_test();
994 #endif
995   }
996 }