1 | /* timer.c |
---|
2 | * |
---|
3 | * This file implements a benchmark timer using timer 2. |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1998. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * Ported to LEON implementation of the SPARC by On-Line Applications |
---|
13 | * Research Corporation (OAR) under contract to the European Space |
---|
14 | * Agency (ESA). |
---|
15 | * |
---|
16 | * LEON modifications of respective RTEMS file: COPYRIGHT (c) 1995. |
---|
17 | * European Space Agency. |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | #include <bsp.h> |
---|
22 | #include <rtems/btimer.h> |
---|
23 | |
---|
24 | #if defined(RTEMS_MULTIPROCESSING) |
---|
25 | #define LEON3_TIMER_INDEX \ |
---|
26 | ((rtems_configuration_get_user_multiprocessing_table()) ? \ |
---|
27 | (rtems_configuration_get_user_multiprocessing_table()->node) - 1 : 1) |
---|
28 | #else |
---|
29 | #define LEON3_TIMER_INDEX 0 |
---|
30 | #endif |
---|
31 | |
---|
32 | bool benchmark_timer_find_average_overhead; |
---|
33 | |
---|
34 | bool benchmark_timer_is_initialized = false; |
---|
35 | |
---|
36 | extern volatile struct gptimer_regs *LEON3_Timer_Regs; |
---|
37 | |
---|
38 | void benchmark_timer_initialize(void) |
---|
39 | { |
---|
40 | /* |
---|
41 | * Timer runs long and accurate enough not to require an interrupt. |
---|
42 | */ |
---|
43 | if (LEON3_Timer_Regs) { |
---|
44 | if ( benchmark_timer_is_initialized == false ) { |
---|
45 | /* approximately 1 us per countdown */ |
---|
46 | LEON3_Timer_Regs->timer[LEON3_TIMER_INDEX].reload = 0xffffff; |
---|
47 | LEON3_Timer_Regs->timer[LEON3_TIMER_INDEX].value = 0xffffff; |
---|
48 | } else { |
---|
49 | benchmark_timer_is_initialized = true; |
---|
50 | } |
---|
51 | LEON3_Timer_Regs->timer[LEON3_TIMER_INDEX].ctrl = LEON3_GPTIMER_EN | LEON3_GPTIMER_LD; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | #define AVG_OVERHEAD 3 /* It typically takes 3.0 microseconds */ |
---|
56 | /* to start/stop the timer. */ |
---|
57 | #define LEAST_VALID 2 /* Don't trust a value lower than this */ |
---|
58 | |
---|
59 | uint32_t benchmark_timer_read(void) |
---|
60 | { |
---|
61 | uint32_t total; |
---|
62 | |
---|
63 | if (LEON3_Timer_Regs) { |
---|
64 | total = LEON3_Timer_Regs->timer[LEON3_TIMER_INDEX].value; |
---|
65 | |
---|
66 | total = 0xffffff - total; |
---|
67 | |
---|
68 | if ( benchmark_timer_find_average_overhead == true ) |
---|
69 | return total; /* in one microsecond units */ |
---|
70 | |
---|
71 | if ( total < LEAST_VALID ) |
---|
72 | return 0; /* below timer resolution */ |
---|
73 | |
---|
74 | return total - AVG_OVERHEAD; |
---|
75 | } |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | void benchmark_timer_disable_subtracting_average_overhead( |
---|
80 | bool find_flag |
---|
81 | ) |
---|
82 | { |
---|
83 | benchmark_timer_find_average_overhead = find_flag; |
---|
84 | } |
---|