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