]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/cris/arch-v10/kernel/fasttimer.c
Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[mirror_ubuntu-focal-kernel.git] / arch / cris / arch-v10 / kernel / fasttimer.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
d8e5219f 2/*
1da177e4
LT
3 * linux/arch/cris/kernel/fasttimer.c
4 *
5 * Fast timers for ETRAX100/ETRAX100LX
1da177e4 6 *
d8e5219f 7 * Copyright (C) 2000-2007 Axis Communications AB, Lund, Sweden
1da177e4
LT
8 */
9
10#include <linux/errno.h>
11#include <linux/sched.h>
12#include <linux/kernel.h>
13#include <linux/param.h>
14#include <linux/string.h>
15#include <linux/mm.h>
16#include <linux/vmalloc.h>
17#include <linux/interrupt.h>
18#include <linux/time.h>
19#include <linux/delay.h>
20
21#include <asm/segment.h>
22#include <asm/io.h>
23#include <asm/irq.h>
24#include <asm/delay.h>
1da177e4 25
556dcee7 26#include <arch/svinto.h>
1da177e4
LT
27#include <asm/fasttimer.h>
28#include <linux/proc_fs.h>
1eb5b23a 29#include <linux/seq_file.h>
1da177e4
LT
30
31
32#define DEBUG_LOG_INCLUDED
33#define FAST_TIMER_LOG
4200c35d 34/* #define FAST_TIMER_TEST */
1da177e4
LT
35
36#define FAST_TIMER_SANITY_CHECKS
37
38#ifdef FAST_TIMER_SANITY_CHECKS
d8e5219f 39static int sanity_failed;
1da177e4
LT
40#endif
41
42#define D1(x)
43#define D2(x)
44#define DP(x)
45
d8e5219f
JN
46static 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;
1da177e4
LT
53
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];
d8e5219f
JN
60static unsigned int debug_log_cnt;
61static unsigned int debug_log_cnt_wrapped;
1da177e4
LT
62
63#define DEBUG_LOG(string, value) \
64{ \
65 unsigned long log_flags; \
7e920426 66 local_irq_save(log_flags); \
1da177e4
LT
67 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 } \
7e920426 74 local_irq_restore(log_flags); \
1da177e4
LT
75}
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 */
2f1f53bd 116inline void do_gettimeofday_fast(struct fasttime_t *tv)
1da177e4 117{
d8e5219f
JN
118 tv->tv_jiff = jiffies;
119 tv->tv_usec = GET_JIFFIES_USEC();
1da177e4
LT
120}
121
2f1f53bd 122inline int fasttime_cmp(struct fasttime_t *t0, struct fasttime_t *t1)
1da177e4 123{
d8e5219f
JN
124 /* 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;
1da177e4
LT
136}
137
2f1f53bd 138inline void start_timer1(unsigned long delay_us)
1da177e4
LT
139{
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
d8e5219f 179 D1(printk(KERN_DEBUG "start_timer1 : %d us freq: %i div: %i\n",
1da177e4
LT
180 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
7e920426 221 local_irq_save(flags);
1da177e4
LT
222
223 do_gettimeofday_fast(&t->tv_set);
224 tmp = fast_timer_list;
225
4200c35d
JN
226#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
1da177e4
LT
239
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;
d8e5219f 246 t->tv_expires.tv_jiff = t->tv_set.tv_jiff + delay_us / 1000000 / HZ;
1da177e4
LT
247 if (t->tv_expires.tv_usec > 1000000)
248 {
249 t->tv_expires.tv_usec -= 1000000;
d8e5219f 250 t->tv_expires.tv_jiff += HZ;
1da177e4
LT
251 }
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 */
2f1f53bd 258 if (tmp == NULL || fasttime_cmp(&t->tv_expires, &tmp->tv_expires) < 0)
1da177e4
LT
259 {
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 */
2f1f53bd
JN
274 while (tmp->next && fasttime_cmp(&t->tv_expires,
275 &tmp->next->tv_expires) > 0)
1da177e4
LT
276 {
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
d8e5219f 291done:
7e920426 292 local_irq_restore(flags);
1da177e4
LT
293} /* 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
7e920426 322 local_irq_save(flags);
1da177e4
LT
323 ret = detach_fast_timer(t);
324 t->next = t->prev = NULL;
7e920426 325 local_irq_restore(flags);
1da177e4
LT
326 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
d8e5219f 335timer1_handler(int irq, void *dev_id)
1da177e4
LT
336{
337 struct fast_timer *t;
338 unsigned long flags;
339
d8e5219f
JN
340 /* 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 */
7e920426 347 local_irq_save(flags);
1da177e4
LT
348
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
1da177e4
LT
364 t = fast_timer_list;
365 while (t)
366 {
d8e5219f
JN
367 struct fasttime_t tv;
368 fast_timer_function_type *f;
369 unsigned long d;
1da177e4
LT
370
371 /* Has it really expired? */
372 do_gettimeofday_fast(&tv);
d8e5219f
JN
373 D1(printk(KERN_DEBUG "t: %is %06ius\n",
374 tv.tv_jiff, tv.tv_usec));
1da177e4 375
2f1f53bd 376 if (fasttime_cmp(&t->tv_expires, &tv) <= 0)
1da177e4
LT
377 {
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 */
1da177e4
LT
385 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;
1da177e4 399
d8e5219f
JN
400 /* 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
1da177e4 415 DEBUG_LOG("!timer1 %i function==NULL!\n", fast_timer_ints);
1da177e4
LT
416 }
417 else
418 {
419 /* Timer is to early, let's set it again using the normal routines */
420 D1(printk(".\n"));
421 }
422
1da177e4
LT
423 if ((t = fast_timer_list) != NULL)
424 {
425 /* Start next timer.. */
d8e5219f
JN
426 long us = 0;
427 struct fasttime_t tv;
1da177e4
LT
428
429 do_gettimeofday_fast(&tv);
d8e5219f
JN
430
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
1da177e4
LT
437 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 }
1da177e4
LT
446 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 }
1da177e4
LT
456 }
457
d8e5219f
JN
458 local_irq_restore(flags);
459
1da177e4
LT
460 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{
d77eab8c 470 wait_queue_head_t *sleep_wait_p = (wait_queue_head_t *)data;
1da177e4
LT
471 wake_up(sleep_wait_p);
472}
473
474
475/* Useful API */
476
477void schedule_usleep(unsigned long us)
478{
479 struct fast_timer t;
1da177e4
LT
480 wait_queue_head_t sleep_wait;
481 init_waitqueue_head(&sleep_wait);
1da177e4
LT
482
483 D1(printk("schedule_usleep(%d)\n", us));
1da177e4
LT
484 start_one_shot_timer(&t, wake_up_func, (unsigned long)&sleep_wait, us,
485 "usleep");
d8e5219f
JN
486 /* Uninterruptible sleep on the fast timer. (The condition is somewhat
487 * redundant since the timer is what wakes us up.) */
488 wait_event(sleep_wait, !fast_timer_pending(&t));
489
1da177e4 490 D1(printk("done schedule_usleep(%d)\n", us));
1da177e4
LT
491}
492
493#ifdef CONFIG_PROC_FS
1da177e4
LT
494/* This value is very much based on testing */
495#define BIG_BUF_SIZE (500 + NUM_TIMER_STATS * 300)
496
1eb5b23a 497static int proc_fasttimer_show(struct seq_file *m, void *v)
1da177e4 498{
1eb5b23a
DH
499 unsigned long flags;
500 int i = 0;
501 int num_to_show;
d8e5219f 502 struct fasttime_t tv;
1eb5b23a
DH
503 struct fast_timer *t, *nextt;
504
505 do_gettimeofday_fast(&tv);
506
507 seq_printf(m, "Fast timers added: %i\n", fast_timers_added);
508 seq_printf(m, "Fast timers started: %i\n", fast_timers_started);
509 seq_printf(m, "Fast timer interrupts: %i\n", fast_timer_ints);
510 seq_printf(m, "Fast timers expired: %i\n", fast_timers_expired);
511 seq_printf(m, "Fast timers deleted: %i\n", fast_timers_deleted);
512 seq_printf(m, "Fast timer running: %s\n",
513 fast_timer_running ? "yes" : "no");
514 seq_printf(m, "Current time: %lu.%06lu\n",
515 (unsigned long)tv.tv_jiff,
516 (unsigned long)tv.tv_usec);
1da177e4 517#ifdef FAST_TIMER_SANITY_CHECKS
1eb5b23a 518 seq_printf(m, "Sanity failed: %i\n", sanity_failed);
1da177e4 519#endif
1eb5b23a 520 seq_putc(m, '\n');
1da177e4
LT
521
522#ifdef DEBUG_LOG_INCLUDED
1eb5b23a
DH
523 {
524 int end_i = debug_log_cnt;
525 i = 0;
526
527 if (debug_log_cnt_wrapped)
528 i = debug_log_cnt;
529
530 while (i != end_i || debug_log_cnt_wrapped) {
dc640a88
JP
531 seq_printf(m, debug_log_string[i], debug_log_value[i]);
532 if (seq_has_overflowed(m))
1eb5b23a
DH
533 return 0;
534 i = (i+1) % DEBUG_LOG_MAX;
535 }
536 }
537 seq_putc(m, '\n');
1da177e4
LT
538#endif
539
1eb5b23a
DH
540 num_to_show = (fast_timers_started < NUM_TIMER_STATS ? fast_timers_started:
541 NUM_TIMER_STATS);
542 seq_printf(m, "Timers started: %i\n", fast_timers_started);
543 for (i = 0; i < num_to_show; i++) {
544 int cur = (fast_timers_started - i - 1) % NUM_TIMER_STATS;
1da177e4
LT
545
546#if 1 //ndef FAST_TIMER_LOG
dc640a88 547 seq_printf(m, "div: %i freq: %i delay: %i\n",
1eb5b23a
DH
548 timer_div_settings[cur],
549 timer_freq_settings[cur],
550 timer_delay_settings[cur]);
1da177e4
LT
551#endif
552#ifdef FAST_TIMER_LOG
1eb5b23a 553 t = &timer_started_log[cur];
dc640a88
JP
554 seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
555 t->name,
556 (unsigned long)t->tv_set.tv_jiff,
557 (unsigned long)t->tv_set.tv_usec,
558 (unsigned long)t->tv_expires.tv_jiff,
559 (unsigned long)t->tv_expires.tv_usec,
560 t->delay_us,
561 t->data);
562 if (seq_has_overflowed(m))
1eb5b23a 563 return 0;
1da177e4 564#endif
1eb5b23a
DH
565 }
566 seq_putc(m, '\n');
1da177e4
LT
567
568#ifdef FAST_TIMER_LOG
1eb5b23a
DH
569 num_to_show = (fast_timers_added < NUM_TIMER_STATS ? fast_timers_added:
570 NUM_TIMER_STATS);
571 seq_printf(m, "Timers added: %i\n", fast_timers_added);
572 for (i = 0; i < num_to_show; i++) {
573 t = &timer_added_log[(fast_timers_added - i - 1) % NUM_TIMER_STATS];
dc640a88
JP
574 seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
575 t->name,
576 (unsigned long)t->tv_set.tv_jiff,
577 (unsigned long)t->tv_set.tv_usec,
578 (unsigned long)t->tv_expires.tv_jiff,
579 (unsigned long)t->tv_expires.tv_usec,
580 t->delay_us,
581 t->data);
582 if (seq_has_overflowed(m))
1eb5b23a
DH
583 return 0;
584 }
585 seq_putc(m, '\n');
586
587 num_to_show = (fast_timers_expired < NUM_TIMER_STATS ? fast_timers_expired:
588 NUM_TIMER_STATS);
589 seq_printf(m, "Timers expired: %i\n", fast_timers_expired);
590 for (i = 0; i < num_to_show; i++) {
591 t = &timer_expired_log[(fast_timers_expired - i - 1) % NUM_TIMER_STATS];
dc640a88
JP
592 seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
593 t->name,
594 (unsigned long)t->tv_set.tv_jiff,
595 (unsigned long)t->tv_set.tv_usec,
596 (unsigned long)t->tv_expires.tv_jiff,
597 (unsigned long)t->tv_expires.tv_usec,
598 t->delay_us,
599 t->data);
600 if (seq_has_overflowed(m))
1eb5b23a
DH
601 return 0;
602 }
603 seq_putc(m, '\n');
1da177e4
LT
604#endif
605
1eb5b23a
DH
606 seq_puts(m, "Active timers:\n");
607 local_irq_save(flags);
608 t = fast_timer_list;
609 while (t) {
610 nextt = t->next;
611 local_irq_restore(flags);
dc640a88
JP
612 seq_printf(m, "%-14s s: %6lu.%06lu e: %6lu.%06lu d: %6li us data: 0x%08lX\n",
613 t->name,
614 (unsigned long)t->tv_set.tv_jiff,
615 (unsigned long)t->tv_set.tv_usec,
616 (unsigned long)t->tv_expires.tv_jiff,
617 (unsigned long)t->tv_expires.tv_usec,
618 t->delay_us,
619 t->data);
620 if (seq_has_overflowed(m))
1eb5b23a
DH
621 return 0;
622 local_irq_save(flags);
623 if (t->next != nextt)
624 printk(KERN_WARNING "timer removed!\n");
625 t = nextt;
626 }
627 local_irq_restore(flags);
1da177e4 628
1eb5b23a
DH
629 return 0;
630}
1da177e4 631
1eb5b23a
DH
632static int proc_fasttimer_open(struct inode *inode, struct file *file)
633{
634 return single_open_size(file, proc_fasttimer_show, PDE_DATA(inode), BIG_BUF_SIZE);
1da177e4 635}
1eb5b23a
DH
636
637static const struct file_operations proc_fasttimer_fops = {
638 .open = proc_fasttimer_open,
639 .read = seq_read,
640 .llseek = seq_lseek,
f42787df 641 .release = single_release,
1eb5b23a 642};
1da177e4
LT
643#endif /* PROC_FS */
644
645#ifdef FAST_TIMER_TEST
646static volatile unsigned long i = 0;
647static volatile int num_test_timeout = 0;
648static struct fast_timer tr[10];
649static int exp_num[10];
650
d8e5219f 651static struct fasttime_t tv_exp[100];
1da177e4
LT
652
653static void test_timeout(unsigned long data)
654{
655 do_gettimeofday_fast(&tv_exp[data]);
656 exp_num[data] = num_test_timeout;
657
658 num_test_timeout++;
659}
660
661static void test_timeout1(unsigned long data)
662{
663 do_gettimeofday_fast(&tv_exp[data]);
664 exp_num[data] = num_test_timeout;
665 if (data < 7)
666 {
667 start_one_shot_timer(&tr[i], test_timeout1, i, 1000, "timeout1");
668 i++;
669 }
670 num_test_timeout++;
671}
672
673DP(
674static char buf0[2000];
675static char buf1[2000];
676static char buf2[2000];
677static char buf3[2000];
678static char buf4[2000];
679);
680
681static char buf5[6000];
682static int j_u[1000];
683
684static void fast_timer_test(void)
685{
686 int prev_num;
687 int j;
688
d8e5219f 689 struct fasttime_t tv, tv0, tv1, tv2;
1da177e4
LT
690
691 printk("fast_timer_test() start\n");
692 do_gettimeofday_fast(&tv);
693
694 for (j = 0; j < 1000; j++)
695 {
696 j_u[j] = GET_JIFFIES_USEC();
697 }
698 for (j = 0; j < 100; j++)
699 {
700 do_gettimeofday_fast(&tv_exp[j]);
701 }
d8e5219f
JN
702 printk(KERN_DEBUG "fast_timer_test() %is %06i\n",
703 tv.tv_jiff, tv.tv_usec);
1da177e4
LT
704
705 for (j = 0; j < 1000; j++)
706 {
707 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]);
708 j += 4;
709 }
710 for (j = 0; j < 100; j++)
711 {
d8e5219f
JN
712 printk(KERN_DEBUG "%i.%i %i.%i %i.%i %i.%i %i.%i\n",
713 tv_exp[j].tv_jiff, tv_exp[j].tv_usec,
714 tv_exp[j+1].tv_jiff, tv_exp[j+1].tv_usec,
715 tv_exp[j+2].tv_jiff, tv_exp[j+2].tv_usec,
716 tv_exp[j+3].tv_jiff, tv_exp[j+3].tv_usec,
717 tv_exp[j+4].tv_jiff, tv_exp[j+4].tv_usec);
1da177e4
LT
718 j += 4;
719 }
720 do_gettimeofday_fast(&tv0);
721 start_one_shot_timer(&tr[i], test_timeout, i, 50000, "test0");
722 DP(proc_fasttimer_read(buf0, NULL, 0, 0, 0));
723 i++;
724 start_one_shot_timer(&tr[i], test_timeout, i, 70000, "test1");
725 DP(proc_fasttimer_read(buf1, NULL, 0, 0, 0));
726 i++;
727 start_one_shot_timer(&tr[i], test_timeout, i, 40000, "test2");
728 DP(proc_fasttimer_read(buf2, NULL, 0, 0, 0));
729 i++;
730 start_one_shot_timer(&tr[i], test_timeout, i, 60000, "test3");
731 DP(proc_fasttimer_read(buf3, NULL, 0, 0, 0));
732 i++;
733 start_one_shot_timer(&tr[i], test_timeout1, i, 55000, "test4xx");
734 DP(proc_fasttimer_read(buf4, NULL, 0, 0, 0));
735 i++;
736 do_gettimeofday_fast(&tv1);
737
738 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
739
740 prev_num = num_test_timeout;
741 while (num_test_timeout < i)
742 {
743 if (num_test_timeout != prev_num)
744 {
745 prev_num = num_test_timeout;
746 }
747 }
748 do_gettimeofday_fast(&tv2);
d8e5219f
JN
749 printk(KERN_DEBUG "Timers started %is %06i\n",
750 tv0.tv_jiff, tv0.tv_usec);
751 printk(KERN_DEBUG "Timers started at %is %06i\n",
752 tv1.tv_jiff, tv1.tv_usec);
753 printk(KERN_DEBUG "Timers done %is %06i\n",
754 tv2.tv_jiff, tv2.tv_usec);
1da177e4
LT
755 DP(printk("buf0:\n");
756 printk(buf0);
757 printk("buf1:\n");
758 printk(buf1);
759 printk("buf2:\n");
760 printk(buf2);
761 printk("buf3:\n");
762 printk(buf3);
763 printk("buf4:\n");
764 printk(buf4);
765 );
766 printk("buf5:\n");
767 printk(buf5);
768
769 printk("timers set:\n");
770 for(j = 0; j<i; j++)
771 {
772 struct fast_timer *t = &tr[j];
773 printk("%-10s set: %6is %06ius exp: %6is %06ius "
774 "data: 0x%08X func: 0x%08X\n",
775 t->name,
d8e5219f 776 t->tv_set.tv_jiff,
1da177e4 777 t->tv_set.tv_usec,
d8e5219f 778 t->tv_expires.tv_jiff,
1da177e4
LT
779 t->tv_expires.tv_usec,
780 t->data,
781 t->function
782 );
783
784 printk(" del: %6ius did exp: %6is %06ius as #%i error: %6li\n",
785 t->delay_us,
d8e5219f 786 tv_exp[j].tv_jiff,
1da177e4
LT
787 tv_exp[j].tv_usec,
788 exp_num[j],
d8e5219f
JN
789 (tv_exp[j].tv_jiff - t->tv_expires.tv_jiff) *
790 1000000 + tv_exp[j].tv_usec -
791 t->tv_expires.tv_usec);
1da177e4
LT
792 }
793 proc_fasttimer_read(buf5, NULL, 0, 0, 0);
794 printk("buf5 after all done:\n");
795 printk(buf5);
796 printk("fast_timer_test() done\n");
797}
798#endif
799
800
d8e5219f 801int fast_timer_init(void)
1da177e4
LT
802{
803 /* For some reason, request_irq() hangs when called froom time_init() */
804 if (!fast_timer_is_init)
805 {
806#if 0 && defined(FAST_TIMER_TEST)
807 int i;
808#endif
809
810 printk(KERN_INFO "fast_timer_init()\n");
811
812#if 0 && defined(FAST_TIMER_TEST)
813 for (i = 0; i <= TIMER0_DIV; i++)
814 {
815 /* We must be careful not to get overflow... */
816 printk("%3i %6u\n", i, timer0_value_us[i]);
817 }
818#endif
819#ifdef CONFIG_PROC_FS
1eb5b23a 820 proc_create("fasttimer", 0, NULL, &proc_fasttimer_fops);
1da177e4 821#endif /* PROC_FS */
7e920426 822 if(request_irq(TIMER1_IRQ_NBR, timer1_handler, 0,
1da177e4
LT
823 "fast timer int", NULL))
824 {
825 printk("err: timer1 irq\n");
826 }
827 fast_timer_is_init = 1;
828#ifdef FAST_TIMER_TEST
829 printk("do test\n");
830 fast_timer_test();
831#endif
832 }
d8e5219f 833 return 0;
1da177e4 834}
d8e5219f 835__initcall(fast_timer_init);