Linux 2.6.19.7
[linux-2.6/suspend2-2.6.19.git] / arch / v850 / kernel / highres_timer.c
blobb16ad1eaf9664f43aa4ebd04111f8ed6ea753ecc
1 /*
2 * arch/v850/kernel/highres_timer.c -- High resolution timing routines
4 * Copyright (C) 2001,02,03 NEC Electronics Corporation
5 * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
7 * This file is subject to the terms and conditions of the GNU General
8 * Public License. See the file COPYING in the main directory of this
9 * archive for more details.
11 * Written by Miles Bader <miles@gnu.org>
14 #include <asm/system.h>
15 #include <asm/v850e_timer_d.h>
16 #include <asm/highres_timer.h>
18 #define HIGHRES_TIMER_USEC_SHIFT 12
20 /* Pre-calculated constant used for converting ticks to real time
21 units. We initialize it to prevent it being put into BSS. */
22 static u32 highres_timer_usec_prescale = 1;
24 void highres_timer_slow_tick_irq (void) __attribute__ ((noreturn));
25 void highres_timer_slow_tick_irq (void)
27 /* This is an interrupt handler, so it must be very careful to
28 not to trash any registers. At this point, the stack-pointer
29 (r3) has been saved in the chip ram location ENTRY_SP by the
30 interrupt vector, so we can use it as a scratch register; we
31 must also restore it before returning. */
32 asm ("ld.w %0[r0], sp;"
33 "add 1, sp;"
34 "st.w sp, %0[r0];"
35 "ld.w %1[r0], sp;" /* restore pre-irq stack-pointer */
36 "reti"
38 "i" (HIGHRES_TIMER_SLOW_TICKS_ADDR),
39 "i" (ENTRY_SP_ADDR)
40 : "memory");
43 void highres_timer_reset (void)
45 V850E_TIMER_D_TMD (HIGHRES_TIMER_TIMER_D_UNIT) = 0;
46 HIGHRES_TIMER_SLOW_TICKS = 0;
49 void highres_timer_start (void)
51 u32 fast_tick_rate;
53 /* Start hardware timer. */
54 v850e_timer_d_configure (HIGHRES_TIMER_TIMER_D_UNIT,
55 HIGHRES_TIMER_SLOW_TICK_RATE);
57 fast_tick_rate =
58 (V850E_TIMER_D_BASE_FREQ
59 >> V850E_TIMER_D_DIVLOG2 (HIGHRES_TIMER_TIMER_D_UNIT));
61 /* The obvious way of calculating microseconds from fast ticks
62 is to do:
64 usec = fast_ticks * 10^6 / fast_tick_rate
66 However, divisions are much slower than multiplications, and
67 the above calculation can overflow, so we do this instead:
69 usec = fast_ticks * (10^6 * 2^12 / fast_tick_rate) / 2^12
71 since we can pre-calculate (10^6 * (2^12 / fast_tick_rate))
72 and use a shift for dividing by 2^12, this avoids division,
73 and is almost as accurate (it differs by about 2 microseconds
74 at the extreme value of the fast-tick counter's ranger). */
75 highres_timer_usec_prescale = ((1000000 << HIGHRES_TIMER_USEC_SHIFT)
76 / fast_tick_rate);
78 /* Enable the interrupt (which is hardwired to this use), and
79 give it the highest priority. */
80 V850E_INTC_IC (IRQ_INTCMD (HIGHRES_TIMER_TIMER_D_UNIT)) = 0;
83 void highres_timer_stop (void)
85 /* Stop the timer. */
86 V850E_TIMER_D_TMCD (HIGHRES_TIMER_TIMER_D_UNIT) =
87 V850E_TIMER_D_TMCD_CAE;
88 /* Disable its interrupt, just in case. */
89 v850e_intc_disable_irq (IRQ_INTCMD (HIGHRES_TIMER_TIMER_D_UNIT));
92 inline void highres_timer_read_ticks (u32 *slow_ticks, u32 *fast_ticks)
94 int flags;
95 u32 fast_ticks_1, fast_ticks_2, _slow_ticks;
97 local_irq_save (flags);
98 fast_ticks_1 = V850E_TIMER_D_TMD (HIGHRES_TIMER_TIMER_D_UNIT);
99 _slow_ticks = HIGHRES_TIMER_SLOW_TICKS;
100 fast_ticks_2 = V850E_TIMER_D_TMD (HIGHRES_TIMER_TIMER_D_UNIT);
101 local_irq_restore (flags);
103 if (fast_ticks_2 < fast_ticks_1)
104 _slow_ticks++;
106 *slow_ticks = _slow_ticks;
107 *fast_ticks = fast_ticks_2;
110 inline void highres_timer_ticks_to_timeval (u32 slow_ticks, u32 fast_ticks,
111 struct timeval *tv)
113 unsigned long sec, sec_rem, usec;
115 usec = ((fast_ticks * highres_timer_usec_prescale)
116 >> HIGHRES_TIMER_USEC_SHIFT);
118 sec = slow_ticks / HIGHRES_TIMER_SLOW_TICK_RATE;
119 sec_rem = slow_ticks % HIGHRES_TIMER_SLOW_TICK_RATE;
121 usec += sec_rem * (1000000 / HIGHRES_TIMER_SLOW_TICK_RATE);
123 tv->tv_sec = sec;
124 tv->tv_usec = usec;
127 void highres_timer_read (struct timeval *tv)
129 u32 fast_ticks, slow_ticks;
130 highres_timer_read_ticks (&slow_ticks, &fast_ticks);
131 highres_timer_ticks_to_timeval (slow_ticks, fast_ticks, tv);