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