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 | volatile rtems_unsigned32 Ttimer_val; /* Updated from ISR!!! */ |
---|
33 | rtems_boolean Timer_driver_Find_average_overhead; |
---|
34 | |
---|
35 | extern void timerisr(); |
---|
36 | |
---|
37 | /* |
---|
38 | * Number of us per timer interrupt. Note: 1 us == 1 tick. |
---|
39 | */ |
---|
40 | |
---|
41 | #define US_PER_ISR 250 |
---|
42 | |
---|
43 | void TimerOn(const rtems_raw_irq_connect_data* used) |
---|
44 | { |
---|
45 | |
---|
46 | Ttimer_val = 0; /* clear timer ISR count */ |
---|
47 | |
---|
48 | /* Select timer, stay in mode 0 */ |
---|
49 | outport_byte (TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN); |
---|
50 | |
---|
51 | /* Set to 250 usec interval */ |
---|
52 | outport_byte (TIMER_CNTR0, US_PER_ISR & 0xFF ); |
---|
53 | outport_byte (TIMER_CNTR0, (US_PER_ISR >> 8) & 0xFF ); |
---|
54 | |
---|
55 | /* Enable all timers */ |
---|
56 | outport_byte (TIMER_CONFIG, 0x00); |
---|
57 | |
---|
58 | /* |
---|
59 | * enable interrrupt at i8259 level |
---|
60 | */ |
---|
61 | BSP_irq_enable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
62 | } |
---|
63 | |
---|
64 | static int TimerIsOn (const rtems_raw_irq_connect_data *used) |
---|
65 | { |
---|
66 | return BSP_irq_enabled_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
67 | } |
---|
68 | |
---|
69 | void TimerOff(const rtems_raw_irq_connect_data* used) |
---|
70 | { |
---|
71 | /* Disable all timers */ |
---|
72 | outport_byte (TIMER_CONFIG, 0x80); |
---|
73 | |
---|
74 | /* |
---|
75 | * disable interrrupt at i8259 level |
---|
76 | */ |
---|
77 | BSP_irq_disable_at_i8259s(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
78 | /* reset timer mode to standard (DOS) value */ |
---|
79 | } |
---|
80 | |
---|
81 | static rtems_raw_irq_connect_data timer_raw_irq_data = { |
---|
82 | BSP_PERIODIC_TIMER + BSP_IRQ_VECTOR_BASE, |
---|
83 | timerisr, |
---|
84 | TimerOn, |
---|
85 | TimerOff, |
---|
86 | TimerIsOn |
---|
87 | }; |
---|
88 | |
---|
89 | static rtems_raw_irq_connect_data old_raw_irq_data = { |
---|
90 | BSP_PERIODIC_TIMER + BSP_IRQ_VECTOR_BASE, |
---|
91 | }; |
---|
92 | |
---|
93 | void Timer_exit() |
---|
94 | { |
---|
95 | if (!i386_delete_idt_entry(&timer_raw_irq_data)) { |
---|
96 | printk("Timer_exit:Timer raw handler removal failed\n"); |
---|
97 | rtems_fatal_error_occurred(1); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void Timer_initialize(void) |
---|
102 | { |
---|
103 | |
---|
104 | static rtems_boolean First = TRUE; |
---|
105 | |
---|
106 | if (First) |
---|
107 | { |
---|
108 | First = FALSE; |
---|
109 | |
---|
110 | if (!i386_get_current_idt_entry (&old_raw_irq_data)) { |
---|
111 | printk("Timer_initialize: failed to get old raw irq entry.\n"); |
---|
112 | rtems_fatal_error_occurred(1); |
---|
113 | } |
---|
114 | |
---|
115 | if (!i386_delete_idt_entry (&old_raw_irq_data)) { |
---|
116 | printk("Timer_initialize: failed to delete old raw irq entry.\n"); |
---|
117 | rtems_fatal_error_occurred(1); |
---|
118 | } |
---|
119 | |
---|
120 | atexit(Timer_exit); /* Try not to hose the system at exit. */ |
---|
121 | if (!i386_set_idt_entry (&timer_raw_irq_data)) { |
---|
122 | printk("Timer_initialize: raw handler installation failed.\n"); |
---|
123 | rtems_fatal_error_occurred(1); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /* wait for ISR to be called at least once */ |
---|
128 | Ttimer_val = 0; |
---|
129 | while (Ttimer_val == 0) |
---|
130 | continue; |
---|
131 | Ttimer_val = 0; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | #define AVG_OVERHEAD 3 /* It typically takes 3.0 microseconds */ |
---|
136 | /* (3 ticks) to start/stop the timer. */ |
---|
137 | #define LEAST_VALID 4 /* Don't trust a value lower than this */ |
---|
138 | |
---|
139 | int Read_timer(void) |
---|
140 | { |
---|
141 | register rtems_unsigned32 clicks, total; |
---|
142 | register rtems_unsigned8 lsb, msb; |
---|
143 | |
---|
144 | /* latch the count */ |
---|
145 | outport_byte (TIMER_MODE, TIMER_SEL0|TIMER_LATCH ); |
---|
146 | |
---|
147 | /* read the count */ |
---|
148 | inport_byte (TIMER_CNTR0, lsb ); |
---|
149 | inport_byte (TIMER_CNTR0, msb ); |
---|
150 | |
---|
151 | /* |
---|
152 | * Timer ISR increments Ttimer_val every US_PER_ISR clock ticks, |
---|
153 | * where 1 tick == 1 us. Below, 'click' is in microseconds. |
---|
154 | * |
---|
155 | * This assumes the timer input clocks are sourced from the system's |
---|
156 | * prescaled clock (PSCLK), which is set to be at 1MHz. |
---|
157 | */ |
---|
158 | clicks = (msb << 8) | lsb; |
---|
159 | total = Ttimer_val * US_PER_ISR + (US_PER_ISR - clicks); |
---|
160 | |
---|
161 | if ( Timer_driver_Find_average_overhead == 1 ) |
---|
162 | return total; /* in one microsecond units */ |
---|
163 | else if ( total < LEAST_VALID ) |
---|
164 | return 0; /* below timer resolution */ |
---|
165 | else |
---|
166 | return (total - AVG_OVERHEAD); |
---|
167 | } |
---|
168 | |
---|
169 | rtems_status_code Empty_function( void ) |
---|
170 | { |
---|
171 | return RTEMS_SUCCESSFUL; |
---|
172 | } |
---|
173 | |
---|
174 | void Set_find_average_overhead( |
---|
175 | rtems_boolean find_flag |
---|
176 | ) |
---|
177 | { |
---|
178 | Timer_driver_Find_average_overhead = find_flag; |
---|
179 | } |
---|
180 | |
---|
181 | /* |
---|
182 | * Wait_X_ms: a delay loop used by the network driver... |
---|
183 | */ |
---|
184 | |
---|
185 | #define SLOW_DOWN_IO 0x80 /* I/O port which does nothing */ |
---|
186 | #define LOOP1MS 320 /* Just a guess.... */ |
---|
187 | |
---|
188 | void Wait_X_ms (unsigned timeToWait) |
---|
189 | { |
---|
190 | unsigned i, j; |
---|
191 | |
---|
192 | for (j=0; j<timeToWait; j++) |
---|
193 | for (i=0; i<LOOP1MS; i++) |
---|
194 | outport_byte (SLOW_DOWN_IO, 0); |
---|
195 | } |
---|