📄 fasttimer.c
字号:
/* * linux/arch/cris/kernel/fasttimer.c * * Fast timers for ETRAX100/ETRAX100LX * * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden */#include <linux/errno.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/param.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/vmalloc.h>#include <linux/interrupt.h>#include <linux/time.h>#include <linux/delay.h>#include <asm/segment.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/delay.h>#include <asm/rtc.h>#include <asm/arch/svinto.h>#include <asm/fasttimer.h>#include <linux/proc_fs.h>#define DEBUG_LOG_INCLUDED#define FAST_TIMER_LOG//#define FAST_TIMER_TEST#define FAST_TIMER_SANITY_CHECKS#ifdef FAST_TIMER_SANITY_CHECKS#define SANITYCHECK(x) xstatic int sanity_failed;#else#define SANITYCHECK(x)#endif#define D1(x)#define D2(x)#define DP(x)static unsigned int fast_timer_running;static unsigned int fast_timers_added;static unsigned int fast_timers_started;static unsigned int fast_timers_expired;static unsigned int fast_timers_deleted;static unsigned int fast_timer_is_init;static unsigned int fast_timer_ints;struct fast_timer *fast_timer_list = NULL;#ifdef DEBUG_LOG_INCLUDED#define DEBUG_LOG_MAX 128static const char * debug_log_string[DEBUG_LOG_MAX];static unsigned long debug_log_value[DEBUG_LOG_MAX];static unsigned int debug_log_cnt;static unsigned int debug_log_cnt_wrapped;#define DEBUG_LOG(string, value) \{ \ unsigned long log_flags; \ local_irq_save(log_flags); \ debug_log_string[debug_log_cnt] = (string); \ debug_log_value[debug_log_cnt] = (unsigned long)(value); \ if (++debug_log_cnt >= DEBUG_LOG_MAX) \ { \ debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \ debug_log_cnt_wrapped = 1; \ } \ local_irq_restore(log_flags); \}#else#define DEBUG_LOG(string, value)#endif/* The frequencies for index = clkselx number in R_TIMER_CTRL */#define NUM_TIMER_FREQ 15#define MAX_USABLE_TIMER_FREQ 7#define MAX_DELAY_US 853333Lconst unsigned long timer_freq_100[NUM_TIMER_FREQ] ={ 3, /* 0 3333 - 853333 us */ 6, /* 1 1666 - 426666 us */ 12, /* 2 833 - 213333 us */ 24, /* 3 416 - 106666 us */ 48, /* 4 208 - 53333 us */ 96, /* 5 104 - 26666 us */ 192, /* 6 52 - 13333 us */ 384, /* 7 26 - 6666 us */ 576, 1152, 2304, 4608, 9216, 18432, 62500, /* 15 = cascade */};#define NUM_TIMER_STATS 16#ifdef FAST_TIMER_LOGstruct fast_timer timer_added_log[NUM_TIMER_STATS];struct fast_timer timer_started_log[NUM_TIMER_STATS];struct fast_timer timer_expired_log[NUM_TIMER_STATS];#endifint timer_div_settings[NUM_TIMER_STATS];int timer_freq_settings[NUM_TIMER_STATS];int timer_delay_settings[NUM_TIMER_STATS];/* Not true gettimeofday, only checks the jiffies (uptime) + useconds */inline void do_gettimeofday_fast(struct fasttime_t *tv){ tv->tv_jiff = jiffies; tv->tv_usec = GET_JIFFIES_USEC();}inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1){ /* Compare jiffies. Takes care of wrapping */ if (time_before(t0->tv_jiff, t1->tv_jiff)) return -1; else if (time_after(t0->tv_jiff, t1->tv_jiff)) return 1; /* Compare us */ if (t0->tv_usec < t1->tv_usec) return -1; else if (t0->tv_usec > t1->tv_usec) return 1; return 0;}inline void start_timer1(unsigned long delay_us){ int freq_index = 0; /* This is the lowest resolution */ unsigned long upper_limit = MAX_DELAY_US; unsigned long div; /* Start/Restart the timer to the new shorter value */ /* t = 1/freq = 1/19200 = 53us * T=div*t, div = T/t = delay_us*freq/1000000 */#if 1 /* Adaptive timer settings */ while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ) { freq_index++; upper_limit >>= 1; /* Divide by 2 using shift */ } if (freq_index > 0) { freq_index--; }#else freq_index = 6;#endif div = delay_us * timer_freq_100[freq_index]/10000; if (div < 2) { /* Maybe increase timer freq? */ div = 2; } if (div > 255) { div = 0; /* This means 256, the max the timer takes */ /* If a longer timeout than the timer can handle is used, * then we must restart it when it goes off. */ } timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div; timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index; timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us; D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n", delay_us, freq_index, div)); /* Clear timer1 irq */ *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); /* Set timer values */ *R_TIMER_CTRL = r_timer_ctrl_shadow = (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, timerdiv1) & ~IO_MASK(R_TIMER_CTRL, tm1) & ~IO_MASK(R_TIMER_CTRL, clksel1)) | IO_FIELD(R_TIMER_CTRL, timerdiv1, div) | IO_STATE(R_TIMER_CTRL, tm1, stop_ld) | IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */ /* Ack interrupt */ *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr); /* Start timer */ *R_TIMER_CTRL = r_timer_ctrl_shadow = (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | IO_STATE(R_TIMER_CTRL, tm1, run); /* Enable timer1 irq */ *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set); fast_timers_started++; fast_timer_running = 1;}/* In version 1.4 this function takes 27 - 50 us */void start_one_shot_timer(struct fast_timer *t, fast_timer_function_type *function, unsigned long data, unsigned long delay_us, const char *name){ unsigned long flags; struct fast_timer *tmp; D1(printk("sft %s %d us\n", name, delay_us)); local_irq_save(flags); do_gettimeofday_fast(&t->tv_set); tmp = fast_timer_list; SANITYCHECK({ /* Check so this is not in the list already... */ while (tmp != NULL) { if (tmp == t) { printk(KERN_WARNING "timer name: %s data: 0x%08lX already in list!\n", name, data); sanity_failed++; goto done; } else { tmp = tmp->next; } } tmp = fast_timer_list; }); t->delay_us = delay_us; t->function = function; t->data = data; t->name = name; t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000; t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ; if (t->tv_expires.tv_usec > 1000000) { t->tv_expires.tv_usec -= 1000000; t->tv_expires.tv_jiff += HZ; }#ifdef FAST_TIMER_LOG timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;#endif fast_timers_added++; /* Check if this should timeout before anything else */ if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0) { /* Put first in list and modify the timer value */ t->prev = NULL; t->next = fast_timer_list; if (fast_timer_list) { fast_timer_list->prev = t; } fast_timer_list = t;#ifdef FAST_TIMER_LOG timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;#endif start_timer1(delay_us); } else { /* Put in correct place in list */ while (tmp->next && fasttime_cmp(&t->tv_expires, &tmp->next->tv_expires) > 0) { tmp = tmp->next; } /* Insert t after tmp */ t->prev = tmp; t->next = tmp->next; if (tmp->next) { tmp->next->prev = t; } tmp->next = t; } D2(printk("start_one_shot_timer: %d us done\n", delay_us));done: local_irq_restore(flags);} /* start_one_shot_timer */static inline int fast_timer_pending (const struct fast_timer * t){ return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);}static inline int detach_fast_timer (struct fast_timer *t){ struct fast_timer *next, *prev; if (!fast_timer_pending(t)) return 0; next = t->next; prev = t->prev; if (next) next->prev = prev; if (prev) prev->next = next; else fast_timer_list = next; fast_timers_deleted++; return 1;}int del_fast_timer(struct fast_timer * t){ unsigned long flags; int ret; local_irq_save(flags); ret = detach_fast_timer(t); t->next = t->prev = NULL; local_irq_restore(flags); return ret;} /* del_fast_timer *//* Interrupt routines or functions called in interrupt context *//* Timer 1 interrupt handler */static irqreturn_ttimer1_handler(int irq, void *dev_id){ struct fast_timer *t; unsigned long flags; /* We keep interrupts disabled not only when we modify the * fast timer list, but any time we hold a reference to a * timer in the list, since del_fast_timer may be called * from (another) interrupt context. Thus, the only time * when interrupts are enabled is when calling the timer * callback function. */ local_irq_save(flags); /* Clear timer1 irq */ *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr); /* First stop timer, then ack interrupt */ /* Stop timer */ *R_TIMER_CTRL = r_timer_ctrl_shadow = (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) | IO_STATE(R_TIMER_CTRL, tm1, stop_ld); /* Ack interrupt */ *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr); fast_timer_running = 0; fast_timer_ints++; t = fast_timer_list; while (t) { struct fasttime_t tv; fast_timer_function_type *f; unsigned long d; /* Has it really expired? */ do_gettimeofday_fast(&tv); D1(printk(KERN_DEBUG "t: %is %06ius\n", tv.tv_jiff, tv.tv_usec)); if (fasttime_cmp(&t->tv_expires, &tv) <= 0) { /* Yes it has expired */#ifdef FAST_TIMER_LOG timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;#endif fast_timers_expired++; /* Remove this timer before call, since it may reuse the timer */ if (t->prev) { t->prev->next = t->next; } else { fast_timer_list = t->next; } if (t->next) { t->next->prev = t->prev; } t->prev = NULL; t->next = NULL; /* Save function callback data before enabling * interrupts, since the timer may be removed and * we don't know how it was allocated * (e.g. ->function and ->data may become overwritten * after deletion if the timer was stack-allocated). */ f = t->function; d = t->data; if (f != NULL) { /* Run callback with interrupts enabled. */ local_irq_restore(flags); f(d); local_irq_save(flags); } else DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints); } else { /* Timer is to early, let's set it again using the normal routines */ D1(printk(".\n")); } if ((t = fast_timer_list) != NULL) { /* Start next timer.. */ long us = 0; struct fasttime_t tv; do_gettimeofday_fast(&tv); /* time_after_eq takes care of wrapping */ if (time_after_eq(t->tv_expires.tv_jiff, tv.tv_jiff)) us = ((t->tv_expires.tv_jiff - tv.tv_jiff) * 1000000 / HZ + t->tv_expires.tv_usec - tv.tv_usec); if (us > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -