| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * arch/v850/kernel/highres_timer.c -- High resolution timing routines | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2001,02,03  NEC Electronics Corporation | 
|  | 5 | *  Copyright (C) 2001,02,03  Miles Bader <miles@gnu.org> | 
|  | 6 | * | 
|  | 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. | 
|  | 10 | * | 
|  | 11 | * Written by Miles Bader <miles@gnu.org> | 
|  | 12 | */ | 
|  | 13 |  | 
|  | 14 | #include <asm/system.h> | 
|  | 15 | #include <asm/v850e_timer_d.h> | 
|  | 16 | #include <asm/highres_timer.h> | 
|  | 17 |  | 
|  | 18 | #define HIGHRES_TIMER_USEC_SHIFT   12 | 
|  | 19 |  | 
|  | 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; | 
|  | 23 |  | 
|  | 24 | void highres_timer_slow_tick_irq (void) __attribute__ ((noreturn)); | 
|  | 25 | void highres_timer_slow_tick_irq (void) | 
|  | 26 | { | 
|  | 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" | 
|  | 37 | :: | 
|  | 38 | "i" (HIGHRES_TIMER_SLOW_TICKS_ADDR), | 
|  | 39 | "i" (ENTRY_SP_ADDR) | 
|  | 40 | : "memory"); | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | void highres_timer_reset (void) | 
|  | 44 | { | 
|  | 45 | V850E_TIMER_D_TMD (HIGHRES_TIMER_TIMER_D_UNIT) = 0; | 
|  | 46 | HIGHRES_TIMER_SLOW_TICKS = 0; | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | void highres_timer_start (void) | 
|  | 50 | { | 
|  | 51 | u32 fast_tick_rate; | 
|  | 52 |  | 
|  | 53 | /* Start hardware timer.  */ | 
|  | 54 | v850e_timer_d_configure (HIGHRES_TIMER_TIMER_D_UNIT, | 
|  | 55 | HIGHRES_TIMER_SLOW_TICK_RATE); | 
|  | 56 |  | 
|  | 57 | fast_tick_rate = | 
|  | 58 | (V850E_TIMER_D_BASE_FREQ | 
|  | 59 | >> V850E_TIMER_D_DIVLOG2 (HIGHRES_TIMER_TIMER_D_UNIT)); | 
|  | 60 |  | 
|  | 61 | /* The obvious way of calculating microseconds from fast ticks | 
|  | 62 | is to do: | 
|  | 63 |  | 
|  | 64 | usec = fast_ticks * 10^6 / fast_tick_rate | 
|  | 65 |  | 
|  | 66 | However, divisions are much slower than multiplications, and | 
|  | 67 | the above calculation can overflow, so we do this instead: | 
|  | 68 |  | 
|  | 69 | usec = fast_ticks * (10^6 * 2^12 / fast_tick_rate) / 2^12 | 
|  | 70 |  | 
|  | 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); | 
|  | 77 |  | 
|  | 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; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | void highres_timer_stop (void) | 
|  | 84 | { | 
|  | 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)); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | inline void highres_timer_read_ticks (u32 *slow_ticks, u32 *fast_ticks) | 
|  | 93 | { | 
|  | 94 | int flags; | 
|  | 95 | u32 fast_ticks_1, fast_ticks_2, _slow_ticks; | 
|  | 96 |  | 
|  | 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); | 
|  | 102 |  | 
|  | 103 | if (fast_ticks_2 < fast_ticks_1) | 
|  | 104 | _slow_ticks++; | 
|  | 105 |  | 
|  | 106 | *slow_ticks = _slow_ticks; | 
|  | 107 | *fast_ticks = fast_ticks_2; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | inline void highres_timer_ticks_to_timeval (u32 slow_ticks, u32 fast_ticks, | 
|  | 111 | struct timeval *tv) | 
|  | 112 | { | 
|  | 113 | unsigned long sec, sec_rem, usec; | 
|  | 114 |  | 
|  | 115 | usec = ((fast_ticks * highres_timer_usec_prescale) | 
|  | 116 | >> HIGHRES_TIMER_USEC_SHIFT); | 
|  | 117 |  | 
|  | 118 | sec = slow_ticks / HIGHRES_TIMER_SLOW_TICK_RATE; | 
|  | 119 | sec_rem = slow_ticks % HIGHRES_TIMER_SLOW_TICK_RATE; | 
|  | 120 |  | 
|  | 121 | usec += sec_rem * (1000000 / HIGHRES_TIMER_SLOW_TICK_RATE); | 
|  | 122 |  | 
|  | 123 | tv->tv_sec = sec; | 
|  | 124 | tv->tv_usec = usec; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void highres_timer_read (struct timeval *tv) | 
|  | 128 | { | 
|  | 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); | 
|  | 132 | } |