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;"
35 "ld.w %1[r0], sp;" /* restore pre-irq stack-pointer */
38 "i" (HIGHRES_TIMER_SLOW_TICKS_ADDR),
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)
53 /* Start hardware timer. */
54 v850e_timer_d_configure (HIGHRES_TIMER_TIMER_D_UNIT,
55 HIGHRES_TIMER_SLOW_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
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)
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)
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)
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)
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,
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);
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);