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