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