1 | /* Clock |
---|
2 | * |
---|
3 | * This routine initializes the interval timer on the |
---|
4 | * PA-RISC CPU. The tick frequency is specified by the bsp. |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * All rights assigned to U.S. Government, 1994. |
---|
9 | * |
---|
10 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
11 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
12 | * notice must appear in all copies of this file and its derivatives. |
---|
13 | * |
---|
14 | * $Id$ |
---|
15 | */ |
---|
16 | |
---|
17 | #include <rtems.h> |
---|
18 | #include <rtems/libio.h> |
---|
19 | |
---|
20 | /* should get this from bsp.h, but it is not installed yet */ |
---|
21 | rtems_isr_entry set_vector(rtems_isr_entry, rtems_vector_number, int); |
---|
22 | extern rtems_configuration_table BSP_Configuration; |
---|
23 | |
---|
24 | #include <stdlib.h> /* for atexit() */ |
---|
25 | |
---|
26 | extern rtems_cpu_table Cpu_table; /* owned by BSP */ |
---|
27 | |
---|
28 | typedef unsigned long long hppa_click_count_t; |
---|
29 | |
---|
30 | /* |
---|
31 | * These are set by clock driver during its init |
---|
32 | */ |
---|
33 | |
---|
34 | rtems_device_major_number rtems_clock_major = ~0; |
---|
35 | rtems_device_minor_number rtems_clock_minor; |
---|
36 | |
---|
37 | /* |
---|
38 | * CPU_HPPA_CLICKS_PER_TICK is either a #define or an rtems_unsigned32 |
---|
39 | * allocated and set by bsp_start() |
---|
40 | */ |
---|
41 | |
---|
42 | #ifndef CPU_HPPA_CLICKS_PER_TICK |
---|
43 | extern rtems_unsigned32 CPU_HPPA_CLICKS_PER_TICK; |
---|
44 | #endif |
---|
45 | |
---|
46 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
47 | rtems_unsigned32 Clock_isrs; /* ISRs until next tick */ |
---|
48 | |
---|
49 | rtems_unsigned32 most_recent_itimer_value; |
---|
50 | |
---|
51 | rtems_unsigned64 Clock_clicks; /* running total of cycles */ |
---|
52 | |
---|
53 | rtems_unsigned32 Clock_clicks_interrupt; |
---|
54 | |
---|
55 | void Clock_exit(void); |
---|
56 | |
---|
57 | void |
---|
58 | ReInstall_clock(rtems_isr_entry new_clock_isr) |
---|
59 | { |
---|
60 | rtems_unsigned32 isrlevel = 0; |
---|
61 | |
---|
62 | rtems_interrupt_disable(isrlevel); |
---|
63 | (void) set_vector( |
---|
64 | new_clock_isr, |
---|
65 | HPPA_INTERRUPT_EXTERNAL_INTERVAL_TIMER, |
---|
66 | 1 |
---|
67 | ); |
---|
68 | rtems_interrupt_enable(isrlevel); |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * read itimer and update Clock_clicks as appropriate |
---|
73 | */ |
---|
74 | |
---|
75 | rtems_unsigned32 |
---|
76 | Clock_read_itimer() |
---|
77 | { |
---|
78 | rtems_unsigned32 isrlevel; |
---|
79 | rtems_unsigned32 itimer_value; |
---|
80 | rtems_unsigned32 wrap_count; |
---|
81 | rtems_unsigned32 recent_count; |
---|
82 | |
---|
83 | rtems_interrupt_disable(isrlevel); |
---|
84 | |
---|
85 | wrap_count = (Clock_clicks & 0xFFFFFFFF00000000ULL) >> 32; |
---|
86 | recent_count = (rtems_unsigned32) Clock_clicks; |
---|
87 | |
---|
88 | itimer_value = get_itimer(); |
---|
89 | |
---|
90 | if (itimer_value < recent_count) |
---|
91 | wrap_count++; |
---|
92 | Clock_clicks = (((rtems_unsigned64) wrap_count) << 32) + itimer_value; |
---|
93 | |
---|
94 | rtems_interrupt_enable(isrlevel); |
---|
95 | |
---|
96 | return itimer_value; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void Install_clock(rtems_isr_entry clock_isr) |
---|
101 | { |
---|
102 | Clock_driver_ticks = 0; |
---|
103 | Clock_clicks_interrupt = 0; |
---|
104 | Clock_clicks = 0; |
---|
105 | |
---|
106 | Clock_isrs = BSP_Configuration.microseconds_per_tick / 1000; |
---|
107 | |
---|
108 | if (BSP_Configuration.ticks_per_timeslice) |
---|
109 | { |
---|
110 | /* |
---|
111 | * initialize the interval here |
---|
112 | * First tick is set to right amount of time in the future |
---|
113 | * Future ticks will be incremented over last value set |
---|
114 | * in order to provide consistent clicks in the face of |
---|
115 | * interrupt overhead |
---|
116 | */ |
---|
117 | |
---|
118 | Clock_clicks_interrupt = Clock_read_itimer() + CPU_HPPA_CLICKS_PER_TICK; |
---|
119 | set_itimer((rtems_unsigned32) Clock_clicks_interrupt); |
---|
120 | |
---|
121 | (void) set_vector(clock_isr, HPPA_INTERRUPT_EXTERNAL_INTERVAL_TIMER, 1); |
---|
122 | } |
---|
123 | atexit(Clock_exit); |
---|
124 | } |
---|
125 | |
---|
126 | rtems_isr |
---|
127 | Clock_isr(rtems_vector_number vector) |
---|
128 | { |
---|
129 | rtems_unsigned32 clicks_til_next_interrupt; |
---|
130 | rtems_unsigned32 itimer_value; |
---|
131 | |
---|
132 | /* |
---|
133 | * setup for next interrupt; making sure the new value is reasonably |
---|
134 | * in the future.... in case we lost out on an interrupt somehow |
---|
135 | */ |
---|
136 | |
---|
137 | itimer_value = Clock_read_itimer(); |
---|
138 | Clock_clicks_interrupt += CPU_HPPA_CLICKS_PER_TICK; |
---|
139 | |
---|
140 | /* |
---|
141 | * how far away is next interrupt *really* |
---|
142 | * It may be a long time; this subtraction works even if |
---|
143 | * Clock_clicks_interrupt < Clock_clicks_low_order via |
---|
144 | * the miracle of unsigned math. |
---|
145 | */ |
---|
146 | clicks_til_next_interrupt = Clock_clicks_interrupt - itimer_value; |
---|
147 | |
---|
148 | /* |
---|
149 | * If it is too soon then bump it up. |
---|
150 | * This should only happen if CPU_HPPA_CLICKS_PER_TICK is too small. |
---|
151 | * But setting it low is useful for debug, so... |
---|
152 | */ |
---|
153 | |
---|
154 | if (clicks_til_next_interrupt < 400) |
---|
155 | { |
---|
156 | Clock_clicks_interrupt = itimer_value + 1000; |
---|
157 | /* XXX: count these! this should be rare */ |
---|
158 | } |
---|
159 | |
---|
160 | /* |
---|
161 | * If it is too late, that means we missed the interrupt somehow. |
---|
162 | * Rather than wait 35-50s for a wrap, we just fudge it here. |
---|
163 | */ |
---|
164 | |
---|
165 | if (clicks_til_next_interrupt > CPU_HPPA_CLICKS_PER_TICK) |
---|
166 | { |
---|
167 | Clock_clicks_interrupt = itimer_value + 1000; |
---|
168 | /* XXX: count these! this should never happen :-) */ |
---|
169 | } |
---|
170 | |
---|
171 | set_itimer((rtems_unsigned32) Clock_clicks_interrupt); |
---|
172 | |
---|
173 | Clock_driver_ticks++; |
---|
174 | |
---|
175 | if (Clock_isrs == 1) |
---|
176 | { |
---|
177 | rtems_clock_tick(); |
---|
178 | Clock_isrs = BSP_Configuration.microseconds_per_tick / 10000; |
---|
179 | if (Clock_isrs == 0) |
---|
180 | Clock_isrs = 1; |
---|
181 | } |
---|
182 | else |
---|
183 | Clock_isrs--; |
---|
184 | } |
---|
185 | |
---|
186 | /* |
---|
187 | * Called via atexit() |
---|
188 | * Remove the clock interrupt handler by setting handler to NULL |
---|
189 | */ |
---|
190 | |
---|
191 | void |
---|
192 | Clock_exit(void) |
---|
193 | { |
---|
194 | if ( BSP_Configuration.ticks_per_timeslice ) |
---|
195 | { |
---|
196 | (void) set_vector(0, HPPA_INTERRUPT_EXTERNAL_INTERVAL_TIMER, 1); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /* |
---|
201 | * spin delay for specified number of microseconds |
---|
202 | * used by RTEMS delay macro |
---|
203 | */ |
---|
204 | |
---|
205 | void |
---|
206 | Clock_delay(rtems_unsigned32 microseconds) |
---|
207 | { |
---|
208 | rtems_unsigned64 future_time; |
---|
209 | |
---|
210 | (void) Clock_read_itimer(); |
---|
211 | future_time = Clock_clicks + |
---|
212 | ((rtems_unsigned64) microseconds) * |
---|
213 | Cpu_table.itimer_clicks_per_microsecond; |
---|
214 | |
---|
215 | for (;;) |
---|
216 | { |
---|
217 | (void) Clock_read_itimer(); |
---|
218 | if (future_time <= Clock_clicks) |
---|
219 | break; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | rtems_device_driver Clock_initialize( |
---|
224 | rtems_device_major_number major, |
---|
225 | rtems_device_minor_number minor, |
---|
226 | void *pargp |
---|
227 | ) |
---|
228 | { |
---|
229 | Install_clock(Clock_isr); |
---|
230 | |
---|
231 | /* |
---|
232 | * make major/minor avail to others such as shared memory driver |
---|
233 | */ |
---|
234 | rtems_clock_major = major; |
---|
235 | rtems_clock_minor = minor; |
---|
236 | |
---|
237 | return RTEMS_SUCCESSFUL; |
---|
238 | } |
---|
239 | |
---|
240 | rtems_device_driver Clock_control( |
---|
241 | rtems_device_major_number major, |
---|
242 | rtems_device_minor_number minor, |
---|
243 | void *pargp |
---|
244 | ) |
---|
245 | { |
---|
246 | rtems_libio_ioctl_args_t *args = pargp; |
---|
247 | |
---|
248 | if (args == 0) |
---|
249 | goto done; |
---|
250 | |
---|
251 | /* |
---|
252 | * This is hokey, but until we get a defined interface |
---|
253 | * to do this, it will just be this simple... |
---|
254 | */ |
---|
255 | |
---|
256 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) |
---|
257 | { |
---|
258 | Clock_isr(HPPA_INTERRUPT_EXTERNAL_INTERVAL_TIMER); |
---|
259 | } |
---|
260 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) |
---|
261 | { |
---|
262 | ReInstall_clock(args->buffer); |
---|
263 | } |
---|
264 | |
---|
265 | done: |
---|
266 | return RTEMS_SUCCESSFUL; |
---|
267 | } |
---|