1 | /* |
---|
2 | * COPYRIGHT (c) 1989-1999. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <rtems.h> |
---|
13 | #include <bsp.h> |
---|
14 | #include <stdlib.h> |
---|
15 | |
---|
16 | int Ttimer_val; |
---|
17 | bool benchmark_timer_find_average_overhead; |
---|
18 | |
---|
19 | extern void timerisr(void); |
---|
20 | extern int ClockIsOn(const rtems_raw_irq_connect_data*); |
---|
21 | |
---|
22 | #define TMR0 0xF040 |
---|
23 | #define TMR1 0xF041 |
---|
24 | #define TMR2 0xF042 |
---|
25 | #define TMRCON 0xF043 |
---|
26 | #define TMRCFG 0xF834 |
---|
27 | |
---|
28 | void TimerOn(const rtems_raw_irq_connect_data* used) |
---|
29 | { |
---|
30 | |
---|
31 | Ttimer_val = 0; /* clear timer ISR count */ |
---|
32 | |
---|
33 | outport_byte ( TMRCON , 0xb0 ); /* select tmr2, stay in mode 0 */ |
---|
34 | outport_byte ( TMR1 , 0xfa ); /* set to 250 usec interval */ |
---|
35 | outport_byte ( TMR1 , 0x00 ); |
---|
36 | outport_byte ( TMRCON , 0x64 ); /* change to mode 2 ( starts timer ) */ |
---|
37 | /* interrupts ARE enabled */ |
---|
38 | /* outport_byte( IERA, 0x41 ); enable interrupt */ |
---|
39 | /* |
---|
40 | * enable interrrupt at i8259 level |
---|
41 | */ |
---|
42 | BSP_irq_enable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
43 | } |
---|
44 | |
---|
45 | void TimerOff(const rtems_raw_irq_connect_data* used) |
---|
46 | { |
---|
47 | /* |
---|
48 | * disable interrrupt at i8259 level |
---|
49 | */ |
---|
50 | BSP_irq_disable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
51 | /* reset timer mode to standard (DOS) value */ |
---|
52 | } |
---|
53 | |
---|
54 | static rtems_raw_irq_connect_data timer_raw_irq_data = { |
---|
55 | BSP_RT_TIMER3 + BSP_IRQ_VECTOR_BASE, |
---|
56 | timerisr, |
---|
57 | TimerOn, |
---|
58 | TimerOff, |
---|
59 | ClockIsOn |
---|
60 | }; |
---|
61 | |
---|
62 | void Timer_exit(void) |
---|
63 | { |
---|
64 | if (!i386_delete_idt_entry(&timer_raw_irq_data)) { |
---|
65 | printk("Timer_exit:Timer raw handler removal failed\n"); |
---|
66 | rtems_fatal_error_occurred(1); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void benchmark_timer_initialize(void) |
---|
71 | { |
---|
72 | |
---|
73 | static bool First = true; |
---|
74 | |
---|
75 | if (First) |
---|
76 | { |
---|
77 | First = false; |
---|
78 | |
---|
79 | atexit(Timer_exit); /* Try not to hose the system at exit. */ |
---|
80 | if (!i386_set_idt_entry (&timer_raw_irq_data)) { |
---|
81 | printk("benchmark_timer_initialize: raw handler installation failed\n"); |
---|
82 | rtems_fatal_error_occurred(1); |
---|
83 | } |
---|
84 | } |
---|
85 | /* wait for ISR to be called at least once */ |
---|
86 | Ttimer_val = 0; |
---|
87 | while (Ttimer_val == 0) |
---|
88 | continue; |
---|
89 | Ttimer_val = 0; |
---|
90 | } |
---|
91 | |
---|
92 | #define AVG_OVERHEAD 3 /* It typically takes 3.0 microseconds */ |
---|
93 | /* (3 ticks) to start/stop the timer. */ |
---|
94 | #define LEAST_VALID 4 /* Don't trust a value lower than this */ |
---|
95 | |
---|
96 | int benchmark_timer_read(void) |
---|
97 | { |
---|
98 | register uint32_t clicks; |
---|
99 | register uint32_t total; |
---|
100 | |
---|
101 | /* outport_byte( TBCR, 0x00 ); stop the timer -- not needed on intel */ |
---|
102 | |
---|
103 | outport_byte ( TMRCON, 0x40 ); /* latch the count */ |
---|
104 | inport_byte ( TMR1, clicks ); /* read the count */ |
---|
105 | |
---|
106 | total = Ttimer_val + 250 - clicks; |
---|
107 | |
---|
108 | /* outport_byte( TBCR, 0x00 ); initial value */ |
---|
109 | /* outport_byte( IERA, 0x40 ); disable interrupt */ |
---|
110 | |
---|
111 | /* ??? Is "do not restore old vector" causing problems? */ |
---|
112 | |
---|
113 | if ( benchmark_timer_find_average_overhead == true ) |
---|
114 | return total; /* in one microsecond units */ |
---|
115 | |
---|
116 | else { |
---|
117 | if ( total < LEAST_VALID ) |
---|
118 | return 0; /* below timer resolution */ |
---|
119 | return (total - AVG_OVERHEAD); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | void benchmark_timer_disable_subtracting_average_overhead( |
---|
124 | bool find_flag |
---|
125 | ) |
---|
126 | { |
---|
127 | benchmark_timer_find_average_overhead = find_flag; |
---|
128 | } |
---|