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 static int sanity_failed;
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;
62 struct fast_timer *fast_timer_list = NULL;
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;
71 #define DEBUG_LOG(string, value) \
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) \
79 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
80 debug_log_cnt_wrapped = 1; \
82 local_irq_restore(log_flags); \
85 #define DEBUG_LOG(string, value)
89 #define NUM_TIMER_STATS 16
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];
96 int timer_div_settings[NUM_TIMER_STATS];
97 int timer_delay_settings[NUM_TIMER_STATS];
99 struct work_struct fast_work;
102 timer_trig_handler(struct work_struct *work);
106 /* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
107 inline void do_gettimeofday_fast(struct fasttime_t *tv)
109 tv->tv_jiff = jiffies;
110 tv->tv_usec = GET_JIFFIES_USEC();
113 inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
115 /* Compare jiffies. Takes care of wrapping */
116 if (time_before(t0->tv_jiff, t1->tv_jiff))
118 else if (time_after(t0->tv_jiff, t1->tv_jiff))
122 if (t0->tv_usec < t1->tv_usec)
124 else if (t0->tv_usec > t1->tv_usec)
129 /* Called with ints off */
130 inline void start_timer_trig(unsigned long delay_us)
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;
141 r_time0 = REG_RD(timer, regi_timer0, r_time);
143 D1(printk("start_timer_trig : %d us freq: %i div: %i\n",
144 delay_us, freq_index, div));
146 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
148 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
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;
154 timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = trig;
155 timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
159 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
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);
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;
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);
174 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
175 fast_timers_started++;
176 fast_timer_running = 1;
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);
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,
193 unsigned long delay_us,
197 struct fast_timer *tmp;
199 D1(printk("sft %s %d us\n", name, delay_us));
201 local_irq_save(flags);
203 do_gettimeofday_fast(&t->tv_set);
204 tmp = fast_timer_list;
206 #ifdef FAST_TIMER_SANITY_CHECKS
207 /* Check so this is not in the list already... */
208 while (tmp != NULL) {
211 "timer name: %s data: 0x%08lX already "
212 "in list!\n", name, data);
218 tmp = fast_timer_list;
221 t->delay_us = delay_us;
222 t->function = function;
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;
232 #ifdef FAST_TIMER_LOG
233 timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
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 */
241 t->next = fast_timer_list;
243 fast_timer_list->prev = t;
245 #ifdef FAST_TIMER_LOG
246 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
248 start_timer_trig(delay_us);
250 /* Put in correct place in list */
252 fasttime_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0)
254 /* Insert t after tmp */
264 D2(printk("start_one_shot_timer: %d us done\n", delay_us));
267 local_irq_restore(flags);
268 } /* start_one_shot_timer */
270 static inline int fast_timer_pending (const struct fast_timer * t)
272 return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
275 static inline int detach_fast_timer (struct fast_timer *t)
277 struct fast_timer *next, *prev;
278 if (!fast_timer_pending(t))
287 fast_timer_list = next;
288 fast_timers_deleted++;
292 int del_fast_timer(struct fast_timer * t)
297 local_irq_save(flags);
298 ret = detach_fast_timer(t);
299 t->next = t->prev = NULL;
300 local_irq_restore(flags);
302 } /* del_fast_timer */
305 /* Interrupt routines or functions called in interrupt context */
307 /* Timer interrupt handler for trig interrupts */
310 timer_trig_interrupt(int irq, void *dev_id)
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)
317 timer_trig_handler(NULL);
321 static void timer_trig_handler(struct work_struct *work)
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;
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
336 local_irq_save(flags);
338 /* Clear timer trig interrupt */
339 intr_mask = REG_RD(timer, regi_timer0, rw_intr_mask);
341 REG_WR(timer, regi_timer0, rw_intr_mask, intr_mask);
343 /* First stop timer, then ack interrupt */
345 trig_cfg.tmr = regk_timer_off;
346 REG_WR(timer, regi_timer0, rw_trig_cfg, trig_cfg);
350 REG_WR(timer, regi_timer0, rw_ack_intr, ack_intr);
352 fast_timer_running = 0;
355 fast_timer_function_type *f;
360 struct fasttime_t tv;
362 /* Has it really expired? */
363 do_gettimeofday_fast(&tv);
365 "t: %is %06ius\n", tv.tv_jiff, tv.tv_usec));
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;
372 fast_timers_expired++;
374 /* Remove this timer before call, since it may reuse the timer */
376 t->prev->next = t->next;
378 fast_timer_list = t->next;
380 t->next->prev = t->prev;
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).
394 /* Run the callback function with interrupts
396 local_irq_restore(flags);
398 local_irq_save(flags);
400 DEBUG_LOG("!trimertrig %i function==NULL!\n", fast_timer_ints);
402 /* Timer is to early, let's set it again using the normal routines */
408 /* Start next timer.. */
410 struct fasttime_t tv;
412 do_gettimeofday_fast(&tv);
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 -
421 if (!fast_timer_running) {
422 #ifdef FAST_TIMER_LOG
423 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
425 start_timer_trig(us);
429 /* Timer already expired, let's handle it better late than never.
430 * The normal loop handles it
432 D1(printk("e! %d\n", us));
437 local_irq_restore(flags);
440 D1(printk("ttrig stop!\n"));
443 static void wake_up_func(unsigned long data)
445 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
446 wake_up(sleep_wait_p);
452 void schedule_usleep(unsigned long us)
455 wait_queue_head_t sleep_wait;
456 init_waitqueue_head(&sleep_wait);
458 D1(printk("schedule_usleep(%d)\n", us));
459 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
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));
465 D1(printk("done schedule_usleep(%d)\n", us));
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 */
474 #ifdef CONFIG_PROC_FS
476 /* This value is very much based on testing */
477 #define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
479 static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
480 ,int *eof, void *data_unused)
485 struct fasttime_t tv;
486 struct fast_timer *t, *nextt;
487 static char *bigbuf = NULL;
488 static unsigned long used;
491 bigbuf = vmalloc(BIG_BUF_SIZE);
500 if (!offset || !used) {
501 do_gettimeofday_fast(&tv);
504 used += sprintf(bigbuf + used, "Fast timers added: %i\n",
506 used += sprintf(bigbuf + used, "Fast timers started: %i\n",
507 fast_timers_started);
508 used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
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",
523 used += sprintf(bigbuf + used, "\n");
525 #ifdef DEBUG_LOG_INCLUDED
527 int end_i = debug_log_cnt;
530 if (debug_log_cnt_wrapped)
533 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
534 used+100 < BIG_BUF_SIZE)
536 used += sprintf(bigbuf + used, debug_log_string[i],
538 i = (i+1) % DEBUG_LOG_MAX;
541 used += sprintf(bigbuf + used, "\n");
544 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
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++)
549 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
551 #if 1 //ndef FAST_TIMER_LOG
552 used += sprintf(bigbuf + used, "div: %i delay: %i"
554 timer_div_settings[cur],
555 timer_delay_settings[cur]
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"
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,
573 used += sprintf(bigbuf + used, "\n");
575 #ifdef FAST_TIMER_LOG
576 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
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++)
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"
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,
594 used += sprintf(bigbuf + used, "\n");
596 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
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++)
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"
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,
614 used += sprintf(bigbuf + used, "\n");
617 used += sprintf(bigbuf + used, "Active timers:\n");
618 local_irq_save(flags);
620 while (t != NULL && (used+100 < BIG_BUF_SIZE))
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" */
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,
637 local_irq_save(flags);
638 if (t->next != nextt)
640 printk("timer removed!\n");
644 local_irq_restore(flags);
647 if (used - offset < len)
652 memcpy(buf, bigbuf + offset, len);
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];
666 static struct fasttime_t tv_exp[100];
668 static void test_timeout(unsigned long data)
670 do_gettimeofday_fast(&tv_exp[data]);
671 exp_num[data] = num_test_timeout;
676 static void test_timeout1(unsigned long data)
678 do_gettimeofday_fast(&tv_exp[data]);
679 exp_num[data] = num_test_timeout;
682 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
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];
696 static char buf5[6000];
697 static int j_u[1000];
699 static void fast_timer_test(void)
704 struct fasttime_t tv, tv0, tv1, tv2;
706 printk("fast_timer_test() start\n");
707 do_gettimeofday_fast(&tv);
709 for (j = 0; j < 1000; j++)
711 j_u[j] = GET_JIFFIES_USEC();
713 for (j = 0; j < 100; j++)
715 do_gettimeofday_fast(&tv_exp[j]);
717 printk(KERN_DEBUG "fast_timer_test() %is %06i\n", tv.tv_jiff, tv.tv_usec);
719 for (j = 0; j < 1000; j++)
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]);
725 for (j = 0; j < 100; j++)
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);
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));
739 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
740 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
742 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
743 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
745 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
746 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
748 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
749 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
751 do_gettimeofday_fast(&tv1);
753 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
755 prev_num = num_test_timeout;
756 while (num_test_timeout < i)
758 if (num_test_timeout != prev_num)
759 prev_num = num_test_timeout;
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");
782 printk("timers set:\n");
785 struct fast_timer *t = &tr[j];
786 printk("%-10s set: %6is %06ius exp: %6is %06ius "
787 "data: 0x%08X func: 0x%08X\n",
791 t->tv_expires.tv_jiff,
792 t->tv_expires.tv_usec,
797 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
802 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
803 1000000 + tv_exp[j].tv_usec -
804 t->tv_expires.tv_usec);
806 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
807 printk("buf5 after all done:\n");
809 printk("fast_timer_test() done\n");
814 int fast_timer_init(void)
816 /* For some reason, request_irq() hangs when called froom time_init() */
817 if (!fast_timer_is_init)
819 printk("fast_timer_init()\n");
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;
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
838 __initcall(fast_timer_init);