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