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