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