2 * linux/arch/cris/kernel/fasttimer.c
4 * Fast timers for ETRAX FS
6 * Copyright (C) 2000-2006 Axis Communications AB, Lund, Sweden
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>
20 #include <asm/system.h>
22 #include <linux/version.h>
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>
31 * timer0 is running at 100MHz and generating jiffies timer ticks
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.
40 #define DEBUG_LOG_INCLUDED
41 #define FAST_TIMER_LOG
42 /* #define FAST_TIMER_TEST */
44 #define FAST_TIMER_SANITY_CHECKS
46 #ifdef FAST_TIMER_SANITY_CHECKS
47 #define SANITYCHECK(x) x
48 static int sanity_failed = 0;
50 #define SANITYCHECK(x)
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;
65 struct fast_timer *fast_timer_list = NULL;
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;
74 #define DEBUG_LOG(string, value) \
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) \
82 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
83 debug_log_cnt_wrapped = 1; \
85 local_irq_restore(log_flags); \
88 #define DEBUG_LOG(string, value)
92 #define NUM_TIMER_STATS 16
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];
99 int timer_div_settings[NUM_TIMER_STATS];
100 int timer_delay_settings[NUM_TIMER_STATS];
102 struct work_struct fast_work;
105 timer_trig_handler(struct work_struct *work);
109 /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
110 inline void do_gettimeofday_fast(struct fasttime_t *tv)
112 tv->tv_jiff = jiffies;
113 tv->tv_usec = GET_JIFFIES_USEC();
116 inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
118 /* Compare jiffies. Takes care of wrapping */
119 if (time_before(t0->tv_jiff, t1->tv_jiff))
121 else if (time_after(t0->tv_jiff, t1->tv_jiff))
125 if (t0->tv_usec < t1->tv_usec)
127 else if (t0->tv_usec > t1->tv_usec)
132 /* Called with ints off */
133 inline void start_timer_trig(unsigned long delay_us)
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;
144 r_time0 = REG_RD(timer, regi_timer0, r_time);
146 D1(printk("start_timer_trig : %d us freq: %i div: %i\n",
147 delay_us, freq_index, div));
149 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
151 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
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;
157 timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = trig;
158 timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
162 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
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);
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;
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);
177 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
178 fast_timers_started++;
179 fast_timer_running = 1;
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);
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,
196 unsigned long delay_us,
200 struct fast_timer *tmp;
202 D1(printk("sft %s %d us\n", name, delay_us));
204 local_irq_save(flags);
206 do_gettimeofday_fast(&t->tv_set);
207 tmp = fast_timer_list;
209 SANITYCHECK({ /* Check so this is not in the list already... */
210 while (tmp != NULL) {
213 "timer name: %s data: 0x%08lX already "
214 "in list!\n", name, data);
220 tmp = fast_timer_list;
223 t->delay_us = delay_us;
224 t->function = function;
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;
234 #ifdef FAST_TIMER_LOG
235 timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
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 */
243 t->next = fast_timer_list;
245 fast_timer_list->prev = t;
247 #ifdef FAST_TIMER_LOG
248 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
250 start_timer_trig(delay_us);
252 /* Put in correct place in list */
254 fasttime_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0)
256 /* Insert t after tmp */
266 D2(printk("start_one_shot_timer: %d us done\n", delay_us));
269 local_irq_restore(flags);
270 } /* start_one_shot_timer */
272 static inline int fast_timer_pending (const struct fast_timer * t)
274 return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
277 static inline int detach_fast_timer (struct fast_timer *t)
279 struct fast_timer *next, *prev;
280 if (!fast_timer_pending(t))
289 fast_timer_list = next;
290 fast_timers_deleted++;
294 int del_fast_timer(struct fast_timer * t)
299 local_irq_save(flags);
300 ret = detach_fast_timer(t);
301 t->next = t->prev = NULL;
302 local_irq_restore(flags);
304 } /* del_fast_timer */
307 /* Interrupt routines or functions called in interrupt context */
309 /* Timer interrupt handler for trig interrupts */
312 timer_trig_interrupt(int irq, void *dev_id)
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)
319 timer_trig_handler(NULL);
323 static void timer_trig_handler(struct work_struct *work)
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;
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
338 local_irq_save(flags);
340 /* Clear timer trig interrupt */
341 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
343 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
345 /* First stop timer, then ack interrupt */
347 trig_cfg.tmr = regk_timer_off;
348 REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
352 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
354 fast_timer_running = 0;
357 fast_timer_function_type *f;
362 struct fasttime_t tv;
364 /* Has it really expired? */
365 do_gettimeofday_fast(&tv);
367 "t: %is %06ius\n", tv.tv_jiff, tv.tv_usec));
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;
374 fast_timers_expired++;
376 /* Remove this timer before call, since it may reuse the timer */
378 t->prev->next = t->next;
380 fast_timer_list = t->next;
382 t->next->prev = t->prev;
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).
396 /* Run the callback function with interrupts
398 local_irq_restore(flags);
400 local_irq_save(flags);
402 DEBUG_LOG("!trimertrig %i function==NULL!\n", fast_timer_ints);
404 /* Timer is to early, let's set it again using the normal routines */
410 /* Start next timer.. */
412 struct fasttime_t tv;
414 do_gettimeofday_fast(&tv);
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 -
423 if (!fast_timer_running) {
424 #ifdef FAST_TIMER_LOG
425 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
427 start_timer_trig(us);
431 /* Timer already expired, let's handle it better late than never.
432 * The normal loop handles it
434 D1(printk("e! %d\n", us));
439 local_irq_restore(flags);
442 D1(printk("ttrig stop!\n"));
445 static void wake_up_func(unsigned long data)
447 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
448 wake_up(sleep_wait_p);
454 void schedule_usleep(unsigned long us)
457 wait_queue_head_t sleep_wait;
458 init_waitqueue_head(&sleep_wait);
460 D1(printk("schedule_usleep(%d)\n", us));
461 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
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));
467 D1(printk("done schedule_usleep(%d)\n", us));
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 */
476 #ifdef CONFIG_PROC_FS
478 /* This value is very much based on testing */
479 #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
481 static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
482 ,int *eof, void *data_unused)
487 struct fasttime_t tv;
488 struct fast_timer *t, *nextt;
489 static char *bigbuf = NULL;
490 static unsigned long used;
493 bigbuf = vmalloc(BIG_BUF_SIZE);
502 if (!offset || !used) {
503 do_gettimeofday_fast(&tv);
506 used += sprintf(bigbuf + used, "Fast timers added: %i\n",
508 used += sprintf(bigbuf + used, "Fast timers started: %i\n",
509 fast_timers_started);
510 used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
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",
525 used += sprintf(bigbuf + used, "\n");
527 #ifdef DEBUG_LOG_INCLUDED
529 int end_i = debug_log_cnt;
532 if (debug_log_cnt_wrapped)
535 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
536 used+100 < BIG_BUF_SIZE)
538 used += sprintf(bigbuf + used, debug_log_string[i],
540 i = (i+1) % DEBUG_LOG_MAX;
543 used += sprintf(bigbuf + used, "\n");
546 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
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++)
551 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
553 #if 1 //ndef FAST_TIMER_LOG
554 used += sprintf(bigbuf + used, "div: %i delay: %i"
556 timer_div_settings[cur],
557 timer_delay_settings[cur]
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"
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,
575 used += sprintf(bigbuf + used, "\n");
577 #ifdef FAST_TIMER_LOG
578 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
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++)
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"
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,
596 used += sprintf(bigbuf + used, "\n");
598 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
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++)
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"
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,
616 used += sprintf(bigbuf + used, "\n");
619 used += sprintf(bigbuf + used, "Active timers:\n");
620 local_irq_save(flags);
622 while (t != NULL && (used+100 < BIG_BUF_SIZE))
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" */
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 local_irq_save(flags);
640 if (t->next != nextt)
642 printk("timer removed!\n");
646 local_irq_restore(flags);
649 if (used - offset < len)
654 memcpy(buf, bigbuf + offset, len);
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];
668 static struct fasttime_t tv_exp[100];
670 static void test_timeout(unsigned long data)
672 do_gettimeofday_fast(&tv_exp[data]);
673 exp_num[data] = num_test_timeout;
678 static void test_timeout1(unsigned long data)
680 do_gettimeofday_fast(&tv_exp[data]);
681 exp_num[data] = num_test_timeout;
684 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
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];
698 static char buf5[6000];
699 static int j_u[1000];
701 static void fast_timer_test(void)
706 struct fasttime_t tv, tv0, tv1, tv2;
708 printk("fast_timer_test() start\n");
709 do_gettimeofday_fast(&tv);
711 for (j = 0; j < 1000; j++)
713 j_u[j] = GET_JIFFIES_USEC();
715 for (j = 0; j < 100; j++)
717 do_gettimeofday_fast(&tv_exp[j]);
719 printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
721 for (j = 0; j < 1000; j++)
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]);
727 for (j = 0; j < 100; j++)
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);
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));
741 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
742 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
744 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
745 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
747 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
748 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
750 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
751 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
753 do_gettimeofday_fast(&tv1);
755 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
757 prev_num = num_test_timeout;
758 while (num_test_timeout < i)
760 if (num_test_timeout != prev_num)
761 prev_num = num_test_timeout;
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");
784 printk("timers set:\n");
787 struct fast_timer *t = &tr[j];
788 printk("%-10s set: %6is %06ius exp: %6is %06ius "
789 "data: 0x%08X func: 0x%08X\n",
793 t->tv_expires.tv_jiff,
794 t->tv_expires.tv_usec,
799 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
804 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
805 1000000 + tv_exp[j].tv_usec -
806 t->tv_expires.tv_usec);
808 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
809 printk("buf5 after all done:\n");
811 printk("fast_timer_test() done\n");
816 int fast_timer_init(void)
818 /* For some reason, request_irq() hangs when called froom time_init() */
819 if (!fast_timer_is_init)
821 printk("fast_timer_init()\n");
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;
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
840 __initcall(fast_timer_init);