9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
13 #include <linux/string.h>
17 #include <linux/time.h>
20 #include <asm/segment.h>
23 #include <asm/delay.h>
30 #define DEBUG_LOG_INCLUDED
31 #define FAST_TIMER_LOG
34 #define FAST_TIMER_SANITY_CHECKS
36 #ifdef FAST_TIMER_SANITY_CHECKS
37 static int sanity_failed;
44 static unsigned int fast_timer_running;
45 static unsigned int fast_timers_added;
46 static unsigned int fast_timers_started;
47 static unsigned int fast_timers_expired;
48 static unsigned int fast_timers_deleted;
49 static unsigned int fast_timer_is_init;
50 static unsigned int fast_timer_ints;
54 #ifdef DEBUG_LOG_INCLUDED
55 #define DEBUG_LOG_MAX 128
58 static unsigned int debug_log_cnt;
59 static unsigned int debug_log_cnt_wrapped;
61 #define DEBUG_LOG(string, value) \
63 unsigned long log_flags; \
64 local_irq_save(log_flags); \
65 debug_log_string[debug_log_cnt] = (string); \
66 debug_log_value[debug_log_cnt] = (unsigned long)(value); \
67 if (++debug_log_cnt >= DEBUG_LOG_MAX) \
69 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
70 debug_log_cnt_wrapped = 1; \
72 local_irq_restore(log_flags); \
75 #define DEBUG_LOG(string, value)
80 #define NUM_TIMER_FREQ 15
81 #define MAX_USABLE_TIMER_FREQ 7
82 #define MAX_DELAY_US 853333L
102 #define NUM_TIMER_STATS 16
103 #ifdef FAST_TIMER_LOG
125 else if (
time_after(t0->tv_jiff, t1->tv_jiff))
129 if (t0->tv_usec < t1->tv_usec)
131 else if (t0->tv_usec > t1->tv_usec)
178 delay_us, freq_index, div));
180 *R_IRQ_MASK0_CLR =
IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
185 ~
IO_MASK(R_TIMER_CTRL, timerdiv1) &
187 ~
IO_MASK(R_TIMER_CTRL, clksel1)) |
188 IO_FIELD(R_TIMER_CTRL, timerdiv1, div) |
189 IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
190 IO_FIELD(R_TIMER_CTRL, clksel1, freq_index );
202 *R_IRQ_MASK0_SET =
IO_STATE(R_IRQ_MASK0_SET, timer1,
set);
203 fast_timers_started++;
204 fast_timer_running = 1;
209 fast_timer_function_type *
function,
211 unsigned long delay_us,
215 struct fast_timer *
tmp;
217 D1(
printk(
"sft %s %d us\n", name, delay_us));
224 #ifdef FAST_TIMER_SANITY_CHECKS
226 while (tmp !=
NULL) {
229 "0x%08lX already in list!\n", name, data);
238 t->delay_us = delay_us;
239 t->function =
function;
243 t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
244 t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 /
HZ;
245 if (t->tv_expires.tv_usec > 1000000)
247 t->tv_expires.tv_usec -= 1000000;
248 t->tv_expires.tv_jiff +=
HZ;
250 #ifdef FAST_TIMER_LOG
266 #ifdef FAST_TIMER_LOG
273 &tmp->next->tv_expires) > 0)
287 D2(
printk(
"start_one_shot_timer: %d us done\n", delay_us));
293 static inline int fast_timer_pending (
const struct fast_timer *
t)
298 static inline int detach_fast_timer (
struct fast_timer *t)
301 if (!fast_timer_pending(t))
311 fast_timers_deleted++;
321 ret = detach_fast_timer(t);
322 t->next = t->prev =
NULL;
333 timer1_handler(
int irq,
void *
dev_id)
335 struct fast_timer *
t;
348 *R_IRQ_MASK0_CLR =
IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
354 IO_STATE(R_TIMER_CTRL, tm1, stop_ld);
359 fast_timer_running = 0;
365 struct fasttime_t tv;
366 fast_timer_function_type *
f;
372 tv.tv_jiff, tv.tv_usec));
377 #ifdef FAST_TIMER_LOG
380 fast_timers_expired++;
385 t->prev->next = t->next;
393 t->next->prev = t->prev;
413 DEBUG_LOG(
"!timer1 %i function==NULL!\n", fast_timer_ints);
425 struct fasttime_t tv;
431 us = ((t->tv_expires.tv_jiff - tv.tv_jiff) *
432 1000000 /
HZ + t->tv_expires.tv_usec -
437 if (!fast_timer_running)
439 #ifdef FAST_TIMER_LOG
466 static void wake_up_func(
unsigned long data)
481 D1(
printk(
"schedule_usleep(%d)\n", us));
486 wait_event(sleep_wait, !fast_timer_pending(&t));
488 D1(
printk(
"done schedule_usleep(%d)\n", us));
491 #ifdef CONFIG_PROC_FS
493 ,
int *eof,
void *data_unused);
497 #ifdef CONFIG_PROC_FS
500 #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
503 ,
int *eof,
void *data_unused)
508 struct fasttime_t tv;
509 struct fast_timer *
t, *nextt;
510 static char *bigbuf =
NULL;
511 static unsigned long used;
513 if (!bigbuf && !(bigbuf =
vmalloc(BIG_BUF_SIZE)))
521 if (!offset || !used)
526 used +=
sprintf(bigbuf + used,
"Fast timers added: %i\n",
528 used +=
sprintf(bigbuf + used,
"Fast timers started: %i\n",
529 fast_timers_started);
530 used +=
sprintf(bigbuf + used,
"Fast timer interrupts: %i\n",
532 used +=
sprintf(bigbuf + used,
"Fast timers expired: %i\n",
533 fast_timers_expired);
534 used +=
sprintf(bigbuf + used,
"Fast timers deleted: %i\n",
535 fast_timers_deleted);
536 used +=
sprintf(bigbuf + used,
"Fast timer running: %s\n",
537 fast_timer_running ?
"yes" :
"no");
538 used +=
sprintf(bigbuf + used,
"Current time: %lu.%06lu\n",
539 (
unsigned long)tv.tv_jiff,
540 (
unsigned long)tv.tv_usec);
541 #ifdef FAST_TIMER_SANITY_CHECKS
542 used +=
sprintf(bigbuf + used,
"Sanity failed: %i\n",
545 used +=
sprintf(bigbuf + used,
"\n");
547 #ifdef DEBUG_LOG_INCLUDED
549 int end_i = debug_log_cnt;
552 if (debug_log_cnt_wrapped)
557 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
558 used+100 < BIG_BUF_SIZE)
560 used +=
sprintf(bigbuf + used, debug_log_string[i],
565 used +=
sprintf(bigbuf + used,
"\n");
568 num_to_show = (fast_timers_started <
NUM_TIMER_STATS ? fast_timers_started:
570 used +=
sprintf(bigbuf + used,
"Timers started: %i\n", fast_timers_started);
571 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
575 #if 1 //ndef FAST_TIMER_LOG
576 used +=
sprintf(bigbuf + used,
"div: %i freq: %i delay: %i"
583 #ifdef FAST_TIMER_LOG
585 used +=
sprintf(bigbuf + used,
"%-14s s: %6lu.%06lu e: %6lu.%06lu "
586 "d: %6li us data: 0x%08lX"
589 (
unsigned long)t->tv_set.tv_jiff,
590 (
unsigned long)t->tv_set.tv_usec,
591 (
unsigned long)t->tv_expires.tv_jiff,
592 (
unsigned long)t->tv_expires.tv_usec,
598 used +=
sprintf(bigbuf + used,
"\n");
600 #ifdef FAST_TIMER_LOG
601 num_to_show = (fast_timers_added <
NUM_TIMER_STATS ? fast_timers_added:
603 used +=
sprintf(bigbuf + used,
"Timers added: %i\n", fast_timers_added);
604 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
607 used +=
sprintf(bigbuf + used,
"%-14s s: %6lu.%06lu e: %6lu.%06lu "
608 "d: %6li us data: 0x%08lX"
611 (
unsigned long)t->tv_set.tv_jiff,
612 (
unsigned long)t->tv_set.tv_usec,
613 (
unsigned long)t->tv_expires.tv_jiff,
614 (
unsigned long)t->tv_expires.tv_usec,
619 used +=
sprintf(bigbuf + used,
"\n");
621 num_to_show = (fast_timers_expired <
NUM_TIMER_STATS ? fast_timers_expired:
623 used +=
sprintf(bigbuf + used,
"Timers expired: %i\n", fast_timers_expired);
624 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
627 used +=
sprintf(bigbuf + used,
"%-14s s: %6lu.%06lu e: %6lu.%06lu "
628 "d: %6li us data: 0x%08lX"
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,
639 used +=
sprintf(bigbuf + used,
"\n");
642 used +=
sprintf(bigbuf + used,
"Active timers:\n");
645 while (t !=
NULL && (used+100 < BIG_BUF_SIZE))
649 used +=
sprintf(bigbuf + used,
"%-14s s: %6lu.%06lu e: %6lu.%06lu "
650 "d: %6li us data: 0x%08lX"
654 (
unsigned long)t->tv_set.tv_jiff,
655 (
unsigned long)t->tv_set.tv_usec,
656 (
unsigned long)t->tv_expires.tv_jiff,
657 (
unsigned long)t->tv_expires.tv_usec,
663 if (t->next != nextt)
672 if (used - offset < len)
677 memcpy(buf, bigbuf + offset, len);
685 #ifdef FAST_TIMER_TEST
686 static volatile unsigned long i = 0;
687 static volatile int num_test_timeout = 0;
688 static struct fast_timer
tr[10];
689 static int exp_num[10];
691 static struct fasttime_t tv_exp[100];
693 static void test_timeout(
unsigned long data)
696 exp_num[
data] = num_test_timeout;
701 static void test_timeout1(
unsigned long data)
704 exp_num[
data] = num_test_timeout;
714 static char buf0[2000];
715 static char buf1[2000];
716 static char buf2[2000];
717 static char buf3[2000];
718 static char buf4[2000];
721 static char buf5[6000];
722 static int j_u[1000];
724 static void fast_timer_test(
void)
729 struct fasttime_t tv, tv0, tv1, tv2;
731 printk(
"fast_timer_test() start\n");
734 for (j = 0; j < 1000; j++)
738 for (j = 0; j < 100; j++)
743 tv.tv_jiff, tv.tv_usec);
745 for (j = 0; j < 1000; j++)
747 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]);
750 for (j = 0; j < 100; j++)
753 tv_exp[j].tv_jiff, tv_exp[j].
tv_usec,
754 tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
755 tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
756 tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
757 tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
762 DP(proc_fasttimer_read(buf0,
NULL, 0, 0, 0));
765 DP(proc_fasttimer_read(buf1,
NULL, 0, 0, 0));
768 DP(proc_fasttimer_read(buf2,
NULL, 0, 0, 0));
771 DP(proc_fasttimer_read(buf3,
NULL, 0, 0, 0));
774 DP(proc_fasttimer_read(buf4,
NULL, 0, 0, 0));
778 proc_fasttimer_read(buf5,
NULL, 0, 0, 0);
780 prev_num = num_test_timeout;
781 while (num_test_timeout < i)
783 if (num_test_timeout != prev_num)
785 prev_num = num_test_timeout;
790 tv0.tv_jiff, tv0.tv_usec);
792 tv1.tv_jiff, tv1.tv_usec);
794 tv2.tv_jiff, tv2.tv_usec);
812 struct fast_timer *t = &
tr[
j];
813 printk(
"%-10s set: %6is %06ius exp: %6is %06ius "
814 "data: 0x%08X func: 0x%08X\n",
818 t->tv_expires.tv_jiff,
819 t->tv_expires.tv_usec,
824 printk(
" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
829 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
830 1000000 + tv_exp[j].tv_usec -
831 t->tv_expires.tv_usec);
833 proc_fasttimer_read(buf5,
NULL, 0, 0, 0);
834 printk(
"buf5 after all done:\n");
836 printk(
"fast_timer_test() done\n");
844 if (!fast_timer_is_init)
846 #if 0 && defined(FAST_TIMER_TEST)
852 #if 0 && defined(FAST_TIMER_TEST)
856 printk(
"%3i %6u\n", i, timer0_value_us[i]);
859 #ifdef CONFIG_PROC_FS
861 fasttimer_proc_entry->
read_proc = proc_fasttimer_read;
864 "fast timer int",
NULL))
866 printk(
"err: timer1 irq\n");
868 fast_timer_is_init = 1;
869 #ifdef FAST_TIMER_TEST