blob: 5ff08a8695e9705f597358d9f0cf51b0ba22962c [file] [log] [blame]
Jesper Nilssond8e52192007-11-14 17:01:23 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * linux/arch/cris/kernel/fasttimer.c
3 *
4 * Fast timers for ETRAX100/ETRAX100LX
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Jesper Nilssond8e52192007-11-14 17:01:23 -08006 * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
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/mm.h>
15#include <linux/vmalloc.h>
16#include <linux/interrupt.h>
17#include <linux/time.h>
18#include <linux/delay.h>
19
20#include <asm/segment.h>
21#include <asm/io.h>
22#include <asm/irq.h>
23#include <asm/delay.h>
24#include <asm/rtc.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Jesper Nilsson556dcee2008-10-21 17:45:58 +020027#include <arch/svinto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/fasttimer.h>
29#include <linux/proc_fs.h>
30
31
32#define DEBUG_LOG_INCLUDED
33#define FAST_TIMER_LOG
Jesper Nilsson4200c352008-01-18 14:31:19 +010034/* #define FAST_TIMER_TEST */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define FAST_TIMER_SANITY_CHECKS
37
38#ifdef FAST_TIMER_SANITY_CHECKS
Jesper Nilssond8e52192007-11-14 17:01:23 -080039static int sanity_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#endif
41
42#define D1(x)
43#define D2(x)
44#define DP(x)
45
Jesper Nilssond8e52192007-11-14 17:01:23 -080046static unsigned int fast_timer_running;
47static unsigned int fast_timers_added;
48static unsigned int fast_timers_started;
49static unsigned int fast_timers_expired;
50static unsigned int fast_timers_deleted;
51static unsigned int fast_timer_is_init;
52static unsigned int fast_timer_ints;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct fast_timer *fast_timer_list = NULL;
55
56#ifdef DEBUG_LOG_INCLUDED
57#define DEBUG_LOG_MAX 128
58static const char * debug_log_string[DEBUG_LOG_MAX];
59static unsigned long debug_log_value[DEBUG_LOG_MAX];
Jesper Nilssond8e52192007-11-14 17:01:23 -080060static unsigned int debug_log_cnt;
61static unsigned int debug_log_cnt_wrapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define DEBUG_LOG(string, value) \
64{ \
65 unsigned long log_flags; \
Mikael Starvik7e920422005-07-27 11:44:34 -070066 local_irq_save(log_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 debug_log_string[debug_log_cnt] = (string); \
68 debug_log_value[debug_log_cnt] = (unsigned long)(value); \
69 if (++debug_log_cnt >= DEBUG_LOG_MAX) \
70 { \
71 debug_log_cnt = debug_log_cnt % DEBUG_LOG_MAX; \
72 debug_log_cnt_wrapped = 1; \
73 } \
Mikael Starvik7e920422005-07-27 11:44:34 -070074 local_irq_restore(log_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76#else
77#define DEBUG_LOG(string, value)
78#endif
79
80
81/* The frequencies for index = clkselx number in R_TIMER_CTRL */
82#define NUM_TIMER_FREQ 15
83#define MAX_USABLE_TIMER_FREQ 7
84#define MAX_DELAY_US 853333L
85const unsigned long timer_freq_100[NUM_TIMER_FREQ] =
86{
87 3, /* 0 3333 - 853333 us */
88 6, /* 1 1666 - 426666 us */
89 12, /* 2 833 - 213333 us */
90 24, /* 3 416 - 106666 us */
91 48, /* 4 208 - 53333 us */
92 96, /* 5 104 - 26666 us */
93 192, /* 6 52 - 13333 us */
94 384, /* 7 26 - 6666 us */
95 576,
96 1152,
97 2304,
98 4608,
99 9216,
100 18432,
101 62500,
102 /* 15 = cascade */
103};
104#define NUM_TIMER_STATS 16
105#ifdef FAST_TIMER_LOG
106struct fast_timer timer_added_log[NUM_TIMER_STATS];
107struct fast_timer timer_started_log[NUM_TIMER_STATS];
108struct fast_timer timer_expired_log[NUM_TIMER_STATS];
109#endif
110
111int timer_div_settings[NUM_TIMER_STATS];
112int timer_freq_settings[NUM_TIMER_STATS];
113int timer_delay_settings[NUM_TIMER_STATS];
114
115/* Not true gettimeofday, only checks the jiffies (uptime) + useconds */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800116inline void do_gettimeofday_fast(struct fasttime_t *tv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Jesper Nilssond8e52192007-11-14 17:01:23 -0800118 tv->tv_jiff = jiffies;
119 tv->tv_usec = GET_JIFFIES_USEC();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800122inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jesper Nilssond8e52192007-11-14 17:01:23 -0800124 /* Compare jiffies. Takes care of wrapping */
125 if (time_before(t0->tv_jiff, t1->tv_jiff))
126 return -1;
127 else if (time_after(t0->tv_jiff, t1->tv_jiff))
128 return 1;
129
130 /* Compare us */
131 if (t0->tv_usec < t1->tv_usec)
132 return -1;
133 else if (t0->tv_usec > t1->tv_usec)
134 return 1;
135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800138inline void start_timer1(unsigned long delay_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int freq_index = 0; /* This is the lowest resolution */
141 unsigned long upper_limit = MAX_DELAY_US;
142
143 unsigned long div;
144 /* Start/Restart the timer to the new shorter value */
145 /* t = 1/freq = 1/19200 = 53us
146 * T=div*t, div = T/t = delay_us*freq/1000000
147 */
148#if 1 /* Adaptive timer settings */
149 while (delay_us < upper_limit && freq_index < MAX_USABLE_TIMER_FREQ)
150 {
151 freq_index++;
152 upper_limit >>= 1; /* Divide by 2 using shift */
153 }
154 if (freq_index > 0)
155 {
156 freq_index--;
157 }
158#else
159 freq_index = 6;
160#endif
161 div = delay_us * timer_freq_100[freq_index]/10000;
162 if (div < 2)
163 {
164 /* Maybe increase timer freq? */
165 div = 2;
166 }
167 if (div > 255)
168 {
169 div = 0; /* This means 256, the max the timer takes */
170 /* If a longer timeout than the timer can handle is used,
171 * then we must restart it when it goes off.
172 */
173 }
174
175 timer_div_settings[fast_timers_started % NUM_TIMER_STATS] = div;
176 timer_freq_settings[fast_timers_started % NUM_TIMER_STATS] = freq_index;
177 timer_delay_settings[fast_timers_started % NUM_TIMER_STATS] = delay_us;
178
Jesper Nilssond8e52192007-11-14 17:01:23 -0800179 D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 delay_us, freq_index, div));
181 /* Clear timer1 irq */
182 *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
183
184 /* Set timer values */
185 *R_TIMER_CTRL = r_timer_ctrl_shadow =
186 (r_timer_ctrl_shadow &
187 ~IO_MASK(R_TIMER_CTRL, timerdiv1) &
188 ~IO_MASK(R_TIMER_CTRL, tm1) &
189 ~IO_MASK(R_TIMER_CTRL, clksel1)) |
190 IO_FIELD(R_TIMER_CTRL, timerdiv1, div) |
191 IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
192 IO_FIELD(R_TIMER_CTRL, clksel1, freq_index ); /* 6=c19k2Hz */
193
194 /* Ack interrupt */
195 *R_TIMER_CTRL = r_timer_ctrl_shadow |
196 IO_STATE(R_TIMER_CTRL, i1, clr);
197
198 /* Start timer */
199 *R_TIMER_CTRL = r_timer_ctrl_shadow =
200 (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
201 IO_STATE(R_TIMER_CTRL, tm1, run);
202
203 /* Enable timer1 irq */
204 *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer1, set);
205 fast_timers_started++;
206 fast_timer_running = 1;
207}
208
209/* In version 1.4 this function takes 27 - 50 us */
210void start_one_shot_timer(struct fast_timer *t,
211 fast_timer_function_type *function,
212 unsigned long data,
213 unsigned long delay_us,
214 const char *name)
215{
216 unsigned long flags;
217 struct fast_timer *tmp;
218
219 D1(printk("sft %s %d us\n", name, delay_us));
220
Mikael Starvik7e920422005-07-27 11:44:34 -0700221 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 do_gettimeofday_fast(&t->tv_set);
224 tmp = fast_timer_list;
225
Jesper Nilsson4200c352008-01-18 14:31:19 +0100226#ifdef FAST_TIMER_SANITY_CHECKS
227 /* Check so this is not in the list already... */
228 while (tmp != NULL) {
229 if (tmp == t) {
230 printk(KERN_WARNING "timer name: %s data: "
231 "0x%08lX already in list!\n", name, data);
232 sanity_failed++;
233 goto done;
234 } else
235 tmp = tmp->next;
236 }
237 tmp = fast_timer_list;
238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 t->delay_us = delay_us;
241 t->function = function;
242 t->data = data;
243 t->name = name;
244
245 t->tv_expires.tv_usec = t->tv_set.tv_usec + delay_us % 1000000;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800246 t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (t->tv_expires.tv_usec > 1000000)
248 {
249 t->tv_expires.tv_usec -= 1000000;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800250 t->tv_expires.tv_jiff += HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252#ifdef FAST_TIMER_LOG
253 timer_added_log[fast_timers_added % NUM_TIMER_STATS] = *t;
254#endif
255 fast_timers_added++;
256
257 /* Check if this should timeout before anything else */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800258 if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 {
260 /* Put first in list and modify the timer value */
261 t->prev = NULL;
262 t->next = fast_timer_list;
263 if (fast_timer_list)
264 {
265 fast_timer_list->prev = t;
266 }
267 fast_timer_list = t;
268#ifdef FAST_TIMER_LOG
269 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
270#endif
271 start_timer1(delay_us);
272 } else {
273 /* Put in correct place in list */
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800274 while (tmp->next && fasttime_cmp(&t->tv_expires,
275 &tmp->next->tv_expires) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {
277 tmp = tmp->next;
278 }
279 /* Insert t after tmp */
280 t->prev = tmp;
281 t->next = tmp->next;
282 if (tmp->next)
283 {
284 tmp->next->prev = t;
285 }
286 tmp->next = t;
287 }
288
289 D2(printk("start_one_shot_timer: %d us done\n", delay_us));
290
Jesper Nilssond8e52192007-11-14 17:01:23 -0800291done:
Mikael Starvik7e920422005-07-27 11:44:34 -0700292 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293} /* start_one_shot_timer */
294
295static inline int fast_timer_pending (const struct fast_timer * t)
296{
297 return (t->next != NULL) || (t->prev != NULL) || (t == fast_timer_list);
298}
299
300static inline int detach_fast_timer (struct fast_timer *t)
301{
302 struct fast_timer *next, *prev;
303 if (!fast_timer_pending(t))
304 return 0;
305 next = t->next;
306 prev = t->prev;
307 if (next)
308 next->prev = prev;
309 if (prev)
310 prev->next = next;
311 else
312 fast_timer_list = next;
313 fast_timers_deleted++;
314 return 1;
315}
316
317int del_fast_timer(struct fast_timer * t)
318{
319 unsigned long flags;
320 int ret;
321
Mikael Starvik7e920422005-07-27 11:44:34 -0700322 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ret = detach_fast_timer(t);
324 t->next = t->prev = NULL;
Mikael Starvik7e920422005-07-27 11:44:34 -0700325 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return ret;
327} /* del_fast_timer */
328
329
330/* Interrupt routines or functions called in interrupt context */
331
332/* Timer 1 interrupt handler */
333
334static irqreturn_t
Jesper Nilssond8e52192007-11-14 17:01:23 -0800335timer1_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct fast_timer *t;
338 unsigned long flags;
339
Jesper Nilssond8e52192007-11-14 17:01:23 -0800340 /* We keep interrupts disabled not only when we modify the
341 * fast timer list, but any time we hold a reference to a
342 * timer in the list, since del_fast_timer may be called
343 * from (another) interrupt context. Thus, the only time
344 * when interrupts are enabled is when calling the timer
345 * callback function.
346 */
Mikael Starvik7e920422005-07-27 11:44:34 -0700347 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* Clear timer1 irq */
350 *R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, timer1, clr);
351
352 /* First stop timer, then ack interrupt */
353 /* Stop timer */
354 *R_TIMER_CTRL = r_timer_ctrl_shadow =
355 (r_timer_ctrl_shadow & ~IO_MASK(R_TIMER_CTRL, tm1)) |
356 IO_STATE(R_TIMER_CTRL, tm1, stop_ld);
357
358 /* Ack interrupt */
359 *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i1, clr);
360
361 fast_timer_running = 0;
362 fast_timer_ints++;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 t = fast_timer_list;
365 while (t)
366 {
Jesper Nilssond8e52192007-11-14 17:01:23 -0800367 struct fasttime_t tv;
368 fast_timer_function_type *f;
369 unsigned long d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* Has it really expired? */
372 do_gettimeofday_fast(&tv);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800373 D1(printk(KERN_DEBUG "t: %is %06ius\n",
374 tv.tv_jiff, tv.tv_usec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jesper Nilsson2f1f53b2007-11-14 17:01:33 -0800376 if (fasttime_cmp(&t->tv_expires, &tv) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 {
378 /* Yes it has expired */
379#ifdef FAST_TIMER_LOG
380 timer_expired_log[fast_timers_expired % NUM_TIMER_STATS] = *t;
381#endif
382 fast_timers_expired++;
383
384 /* Remove this timer before call, since it may reuse the timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (t->prev)
386 {
387 t->prev->next = t->next;
388 }
389 else
390 {
391 fast_timer_list = t->next;
392 }
393 if (t->next)
394 {
395 t->next->prev = t->prev;
396 }
397 t->prev = NULL;
398 t->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jesper Nilssond8e52192007-11-14 17:01:23 -0800400 /* Save function callback data before enabling
401 * interrupts, since the timer may be removed and
402 * we don't know how it was allocated
403 * (e.g. ->function and ->data may become overwritten
404 * after deletion if the timer was stack-allocated).
405 */
406 f = t->function;
407 d = t->data;
408
409 if (f != NULL) {
410 /* Run callback with interrupts enabled. */
411 local_irq_restore(flags);
412 f(d);
413 local_irq_save(flags);
414 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 else
418 {
419 /* Timer is to early, let's set it again using the normal routines */
420 D1(printk(".\n"));
421 }
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if ((t = fast_timer_list) != NULL)
424 {
425 /* Start next timer.. */
Jesper Nilssond8e52192007-11-14 17:01:23 -0800426 long us = 0;
427 struct fasttime_t tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 do_gettimeofday_fast(&tv);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800430
431 /* time_after_eq takes care of wrapping */
432 if (time_after_eq(t->tv_expires.tv_jiff, tv.tv_jiff))
433 us = ((t->tv_expires.tv_jiff - tv.tv_jiff) *
434 1000000 / HZ + t->tv_expires.tv_usec -
435 tv.tv_usec);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (us > 0)
438 {
439 if (!fast_timer_running)
440 {
441#ifdef FAST_TIMER_LOG
442 timer_started_log[fast_timers_started % NUM_TIMER_STATS] = *t;
443#endif
444 start_timer1(us);
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447 }
448 else
449 {
450 /* Timer already expired, let's handle it better late than never.
451 * The normal loop handles it
452 */
453 D1(printk("e! %d\n", us));
454 }
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Jesper Nilssond8e52192007-11-14 17:01:23 -0800458 local_irq_restore(flags);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!t)
461 {
462 D1(printk("t1 stop!\n"));
463 }
464
465 return IRQ_HANDLED;
466}
467
468static void wake_up_func(unsigned long data)
469{
470#ifdef DECLARE_WAITQUEUE
471 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t*)data;
472#else
473 struct wait_queue **sleep_wait_p = (struct wait_queue **)data;
474#endif
475 wake_up(sleep_wait_p);
476}
477
478
479/* Useful API */
480
481void schedule_usleep(unsigned long us)
482{
483 struct fast_timer t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 wait_queue_head_t sleep_wait;
485 init_waitqueue_head(&sleep_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 D1(printk("schedule_usleep(%d)\n", us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
489 "usleep");
Jesper Nilssond8e52192007-11-14 17:01:23 -0800490 /* Uninterruptible sleep on the fast timer. (The condition is somewhat
491 * redundant since the timer is what wakes us up.) */
492 wait_event(sleep_wait, !fast_timer_pending(&t));
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 D1(printk("done schedule_usleep(%d)\n", us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497#ifdef CONFIG_PROC_FS
498static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
499 ,int *eof, void *data_unused);
500static struct proc_dir_entry *fasttimer_proc_entry;
501#endif /* CONFIG_PROC_FS */
502
503#ifdef CONFIG_PROC_FS
504
505/* This value is very much based on testing */
506#define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
507
508static int proc_fasttimer_read(char *buf, char **start, off_t offset, int len
509 ,int *eof, void *data_unused)
510{
511 unsigned long flags;
512 int i = 0;
513 int num_to_show;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800514 struct fasttime_t tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct fast_timer *t, *nextt;
516 static char *bigbuf = NULL;
517 static unsigned long used;
518
519 if (!bigbuf && !(bigbuf = vmalloc(BIG_BUF_SIZE)))
520 {
521 used = 0;
Jesper Nilssond8e52192007-11-14 17:01:23 -0800522 if (buf)
523 buf[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
525 }
526
527 if (!offset || !used)
528 {
529 do_gettimeofday_fast(&tv);
530
531 used = 0;
532 used += sprintf(bigbuf + used, "Fast timers added: %i\n",
533 fast_timers_added);
534 used += sprintf(bigbuf + used, "Fast timers started: %i\n",
535 fast_timers_started);
536 used += sprintf(bigbuf + used, "Fast timer interrupts: %i\n",
537 fast_timer_ints);
538 used += sprintf(bigbuf + used, "Fast timers expired: %i\n",
539 fast_timers_expired);
540 used += sprintf(bigbuf + used, "Fast timers deleted: %i\n",
541 fast_timers_deleted);
542 used += sprintf(bigbuf + used, "Fast timer running: %s\n",
543 fast_timer_running ? "yes" : "no");
544 used += sprintf(bigbuf + used, "Current time: %lu.%06lu\n",
Jesper Nilssond8e52192007-11-14 17:01:23 -0800545 (unsigned long)tv.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (unsigned long)tv.tv_usec);
547#ifdef FAST_TIMER_SANITY_CHECKS
548 used += sprintf(bigbuf + used, "Sanity failed: %i\n",
549 sanity_failed);
550#endif
551 used += sprintf(bigbuf + used, "\n");
552
553#ifdef DEBUG_LOG_INCLUDED
554 {
555 int end_i = debug_log_cnt;
556 i = 0;
557
558 if (debug_log_cnt_wrapped)
559 {
560 i = debug_log_cnt;
561 }
562
563 while ((i != end_i || (debug_log_cnt_wrapped && !used)) &&
564 used+100 < BIG_BUF_SIZE)
565 {
566 used += sprintf(bigbuf + used, debug_log_string[i],
567 debug_log_value[i]);
568 i = (i+1) % DEBUG_LOG_MAX;
569 }
570 }
571 used += sprintf(bigbuf + used, "\n");
572#endif
573
574 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
575 NUM_TIMER_STATS);
576 used += sprintf(bigbuf + used, "Timers started: %i\n", fast_timers_started);
577 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE) ; i++)
578 {
579 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
580
581#if 1 //ndef FAST_TIMER_LOG
582 used += sprintf(bigbuf + used, "div: %i freq: %i delay: %i"
583 "\n",
584 timer_div_settings[cur],
585 timer_freq_settings[cur],
586 timer_delay_settings[cur]
587 );
588#endif
589#ifdef FAST_TIMER_LOG
590 t = &timer_started_log[cur];
591 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
592 "d: %6li us data: 0x%08lX"
593 "\n",
594 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800595 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800597 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 (unsigned long)t->tv_expires.tv_usec,
599 t->delay_us,
600 t->data
601 );
602#endif
603 }
604 used += sprintf(bigbuf + used, "\n");
605
606#ifdef FAST_TIMER_LOG
607 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
608 NUM_TIMER_STATS);
609 used += sprintf(bigbuf + used, "Timers added: %i\n", fast_timers_added);
610 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
611 {
612 t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
613 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
614 "d: %6li us data: 0x%08lX"
615 "\n",
616 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800617 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800619 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 (unsigned long)t->tv_expires.tv_usec,
621 t->delay_us,
622 t->data
623 );
624 }
625 used += sprintf(bigbuf + used, "\n");
626
627 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
628 NUM_TIMER_STATS);
629 used += sprintf(bigbuf + used, "Timers expired: %i\n", fast_timers_expired);
630 for (i = 0; i < num_to_show && (used+100 < BIG_BUF_SIZE); i++)
631 {
632 t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
633 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
634 "d: %6li us data: 0x%08lX"
635 "\n",
636 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800637 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800639 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 (unsigned long)t->tv_expires.tv_usec,
641 t->delay_us,
642 t->data
643 );
644 }
645 used += sprintf(bigbuf + used, "\n");
646#endif
647
648 used += sprintf(bigbuf + used, "Active timers:\n");
Mikael Starvik7e920422005-07-27 11:44:34 -0700649 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 t = fast_timer_list;
651 while (t != NULL && (used+100 < BIG_BUF_SIZE))
652 {
653 nextt = t->next;
Mikael Starvik7e920422005-07-27 11:44:34 -0700654 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 used += sprintf(bigbuf + used, "%-14s s: %6lu.%06lu e: %6lu.%06lu "
656 "d: %6li us data: 0x%08lX"
657/* " func: 0x%08lX" */
658 "\n",
659 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800660 (unsigned long)t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 (unsigned long)t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800662 (unsigned long)t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 (unsigned long)t->tv_expires.tv_usec,
664 t->delay_us,
665 t->data
666/* , t->function */
667 );
Jesper Nilssond8e52192007-11-14 17:01:23 -0800668 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (t->next != nextt)
670 {
671 printk(KERN_WARNING "timer removed!\n");
672 }
673 t = nextt;
674 }
Mikael Starvik7e920422005-07-27 11:44:34 -0700675 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 if (used - offset < len)
679 {
680 len = used - offset;
681 }
682
683 memcpy(buf, bigbuf + offset, len);
684 *start = buf;
685 *eof = 1;
686
687 return len;
688}
689#endif /* PROC_FS */
690
691#ifdef FAST_TIMER_TEST
692static volatile unsigned long i = 0;
693static volatile int num_test_timeout = 0;
694static struct fast_timer tr[10];
695static int exp_num[10];
696
Jesper Nilssond8e52192007-11-14 17:01:23 -0800697static struct fasttime_t tv_exp[100];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699static void test_timeout(unsigned long data)
700{
701 do_gettimeofday_fast(&tv_exp[data]);
702 exp_num[data] = num_test_timeout;
703
704 num_test_timeout++;
705}
706
707static void test_timeout1(unsigned long data)
708{
709 do_gettimeofday_fast(&tv_exp[data]);
710 exp_num[data] = num_test_timeout;
711 if (data < 7)
712 {
713 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
714 i++;
715 }
716 num_test_timeout++;
717}
718
719DP(
720static char buf0[2000];
721static char buf1[2000];
722static char buf2[2000];
723static char buf3[2000];
724static char buf4[2000];
725);
726
727static char buf5[6000];
728static int j_u[1000];
729
730static void fast_timer_test(void)
731{
732 int prev_num;
733 int j;
734
Jesper Nilssond8e52192007-11-14 17:01:23 -0800735 struct fasttime_t tv, tv0, tv1, tv2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 printk("fast_timer_test() start\n");
738 do_gettimeofday_fast(&tv);
739
740 for (j = 0; j < 1000; j++)
741 {
742 j_u[j] = GET_JIFFIES_USEC();
743 }
744 for (j = 0; j < 100; j++)
745 {
746 do_gettimeofday_fast(&tv_exp[j]);
747 }
Jesper Nilssond8e52192007-11-14 17:01:23 -0800748 printk(KERN_DEBUG "fast_timer_test() %is %06i\n",
749 tv.tv_jiff, tv.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 for (j = 0; j < 1000; j++)
752 {
753 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]);
754 j += 4;
755 }
756 for (j = 0; j < 100; j++)
757 {
Jesper Nilssond8e52192007-11-14 17:01:23 -0800758 printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
759 tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
760 tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
761 tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
762 tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
763 tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 j += 4;
765 }
766 do_gettimeofday_fast(&tv0);
767 start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
768 DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
769 i++;
770 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
771 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
772 i++;
773 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
774 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
775 i++;
776 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
777 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
778 i++;
779 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
780 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
781 i++;
782 do_gettimeofday_fast(&tv1);
783
784 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
785
786 prev_num = num_test_timeout;
787 while (num_test_timeout < i)
788 {
789 if (num_test_timeout != prev_num)
790 {
791 prev_num = num_test_timeout;
792 }
793 }
794 do_gettimeofday_fast(&tv2);
Jesper Nilssond8e52192007-11-14 17:01:23 -0800795 printk(KERN_DEBUG "Timers started %is %06i\n",
796 tv0.tv_jiff, tv0.tv_usec);
797 printk(KERN_DEBUG "Timers started at %is %06i\n",
798 tv1.tv_jiff, tv1.tv_usec);
799 printk(KERN_DEBUG "Timers done %is %06i\n",
800 tv2.tv_jiff, tv2.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 DP(printk("buf0:\n");
802 printk(buf0);
803 printk("buf1:\n");
804 printk(buf1);
805 printk("buf2:\n");
806 printk(buf2);
807 printk("buf3:\n");
808 printk(buf3);
809 printk("buf4:\n");
810 printk(buf4);
811 );
812 printk("buf5:\n");
813 printk(buf5);
814
815 printk("timers set:\n");
816 for(j = 0; j<i; j++)
817 {
818 struct fast_timer *t = &tr[j];
819 printk("%-10s set: %6is %06ius exp: %6is %06ius "
820 "data: 0x%08X func: 0x%08X\n",
821 t->name,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800822 t->tv_set.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 t->tv_set.tv_usec,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800824 t->tv_expires.tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 t->tv_expires.tv_usec,
826 t->data,
827 t->function
828 );
829
830 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
831 t->delay_us,
Jesper Nilssond8e52192007-11-14 17:01:23 -0800832 tv_exp[j].tv_jiff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 tv_exp[j].tv_usec,
834 exp_num[j],
Jesper Nilssond8e52192007-11-14 17:01:23 -0800835 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
836 1000000 + tv_exp[j].tv_usec -
837 t->tv_expires.tv_usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
840 printk("buf5 after all done:\n");
841 printk(buf5);
842 printk("fast_timer_test() done\n");
843}
844#endif
845
846
Jesper Nilssond8e52192007-11-14 17:01:23 -0800847int fast_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 /* For some reason, request_irq() hangs when called froom time_init() */
850 if (!fast_timer_is_init)
851 {
852#if 0 && defined(FAST_TIMER_TEST)
853 int i;
854#endif
855
856 printk(KERN_INFO "fast_timer_init()\n");
857
858#if 0 && defined(FAST_TIMER_TEST)
859 for (i = 0; i <= TIMER0_DIV; i++)
860 {
861 /* We must be careful not to get overflow... */
862 printk("%3i %6u\n", i, timer0_value_us[i]);
863 }
864#endif
865#ifdef CONFIG_PROC_FS
866 if ((fasttimer_proc_entry = create_proc_entry( "fasttimer", 0, 0 )))
867 fasttimer_proc_entry->read_proc = proc_fasttimer_read;
868#endif /* PROC_FS */
Mikael Starvik7e920422005-07-27 11:44:34 -0700869 if(request_irq(TIMER1_IRQ_NBR, timer1_handler, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 "fast timer int", NULL))
871 {
872 printk("err: timer1 irq\n");
873 }
874 fast_timer_is_init = 1;
875#ifdef FAST_TIMER_TEST
876 printk("do test\n");
877 fast_timer_test();
878#endif
879 }
Jesper Nilssond8e52192007-11-14 17:01:23 -0800880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Jesper Nilssond8e52192007-11-14 17:01:23 -0800882__initcall(fast_timer_init);