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