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