1 | /* ckinit.c |
---|
2 | * |
---|
3 | * This file provides a template for the clock device driver initialization. |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-1998. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * Copyright assigned to U.S. Government, 1994. |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.OARcorp.com/rtems/license.html. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #include <stdlib.h> |
---|
17 | |
---|
18 | #include <rtems.h> |
---|
19 | #include <rtems/libio.h> |
---|
20 | #include <bsp.h> |
---|
21 | |
---|
22 | void Clock_exit( void ); |
---|
23 | rtems_isr Clock_isr( rtems_vector_number vector ); |
---|
24 | |
---|
25 | |
---|
26 | /* |
---|
27 | * The interrupt vector number associated with the clock tick device |
---|
28 | * driver. |
---|
29 | */ |
---|
30 | |
---|
31 | #define CLOCK_VECTOR PPC_IRQ_DECREMENTER |
---|
32 | |
---|
33 | /* |
---|
34 | * Clock_driver_ticks is a monotonically increasing counter of the |
---|
35 | * number of clock ticks since the driver was initialized. |
---|
36 | */ |
---|
37 | |
---|
38 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
39 | |
---|
40 | /* |
---|
41 | * Clocks_per_tick is the number of clocks of the decrementer needed |
---|
42 | * to fill BSP_Configuration.microseconds_per_tick. |
---|
43 | * |
---|
44 | * The clock rate is 1/4 the bus clock, or 16.666667 MHz, so: |
---|
45 | * |
---|
46 | * Clocks_per_tick = ((50/3)*1E6 clocks/sec) * (1 sec/1E6 us) * (X us/tick) |
---|
47 | * = X * 50 / 3 clocks/tick |
---|
48 | * where X = BSP_Configuration.microseconds_per_tick |
---|
49 | */ |
---|
50 | |
---|
51 | rtems_signed32 Clocks_per_tick; |
---|
52 | |
---|
53 | /* |
---|
54 | * These are set by clock driver during its init |
---|
55 | */ |
---|
56 | |
---|
57 | rtems_device_major_number rtems_clock_major = ~0; |
---|
58 | rtems_device_minor_number rtems_clock_minor; |
---|
59 | |
---|
60 | /* |
---|
61 | * The previous ISR on this clock tick interrupt vector. |
---|
62 | */ |
---|
63 | |
---|
64 | rtems_isr_entry Old_ticker; |
---|
65 | |
---|
66 | void Clock_exit( void ); |
---|
67 | |
---|
68 | /* |
---|
69 | * Inline assembly routines to access the decrementer register |
---|
70 | */ |
---|
71 | static inline rtems_signed32 read_decrementer(void) { |
---|
72 | rtems_signed32 result; |
---|
73 | asm volatile ("mfdec %0" : "=r" (result) :); |
---|
74 | return result; |
---|
75 | } |
---|
76 | |
---|
77 | static inline void write_decrementer(rtems_signed32 value) { |
---|
78 | asm volatile ("mtdec %0" : : "r" (value)); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /* |
---|
83 | * Isr Handler |
---|
84 | */ |
---|
85 | |
---|
86 | rtems_isr Clock_isr( |
---|
87 | rtems_vector_number vector |
---|
88 | ) |
---|
89 | { |
---|
90 | rtems_signed32 count, repeat = 0; |
---|
91 | const rtems_signed32 period = Clocks_per_tick; |
---|
92 | |
---|
93 | /* |
---|
94 | * bump the number of clock driver ticks since initialization |
---|
95 | */ |
---|
96 | Clock_driver_ticks++; |
---|
97 | |
---|
98 | /* |
---|
99 | * be very paranoid and count number of "ticks" in case we missed some |
---|
100 | * |
---|
101 | * update the decrementer and signal rtems the appropriate number of times |
---|
102 | */ |
---|
103 | count = read_decrementer(); |
---|
104 | while (count < 0) { |
---|
105 | count += period; |
---|
106 | repeat++; |
---|
107 | } |
---|
108 | write_decrementer(count); |
---|
109 | |
---|
110 | while (repeat-- > 0) { |
---|
111 | rtems_clock_tick(); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /* |
---|
116 | * Install_clock |
---|
117 | * |
---|
118 | * Install a clock tick handler and reprograms the chip. This |
---|
119 | * is used to initially establish the clock tick. |
---|
120 | */ |
---|
121 | |
---|
122 | void Install_clock( |
---|
123 | rtems_isr_entry clock_isr |
---|
124 | ) |
---|
125 | { |
---|
126 | /* |
---|
127 | * Initialize the clock tick device driver variables |
---|
128 | */ |
---|
129 | |
---|
130 | Clock_driver_ticks = 0; |
---|
131 | switch (Falcon_SYSCR.SystemClock) { |
---|
132 | case SYSCLK_50_MHZ: |
---|
133 | Clocks_per_tick = |
---|
134 | (BSP_Configuration.microseconds_per_tick * 25 + 1) / 2; |
---|
135 | break; |
---|
136 | case SYSCLK_60_MHZ: |
---|
137 | Clocks_per_tick = BSP_Configuration.microseconds_per_tick * 15; |
---|
138 | break; |
---|
139 | case SYSCLK_67_MHZ: |
---|
140 | Clocks_per_tick = |
---|
141 | (BSP_Configuration.microseconds_per_tick * 50 + 1) / 3; |
---|
142 | break; |
---|
143 | } |
---|
144 | |
---|
145 | /* |
---|
146 | * If ticks_per_timeslice is configured as non-zero, then the user |
---|
147 | * wants a clock tick. |
---|
148 | */ |
---|
149 | |
---|
150 | if ( BSP_Configuration.ticks_per_timeslice ) { |
---|
151 | Old_ticker = (rtems_isr_entry) set_vector( clock_isr, CLOCK_VECTOR, 1 ); |
---|
152 | |
---|
153 | /* |
---|
154 | * Hardware specific initialize goes here |
---|
155 | */ |
---|
156 | write_decrementer(Clocks_per_tick); |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | * Schedule the clock cleanup routine to execute if the application exits. |
---|
161 | */ |
---|
162 | |
---|
163 | atexit( Clock_exit ); |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * Clean up before the application exits |
---|
168 | */ |
---|
169 | |
---|
170 | void Clock_exit( void ) |
---|
171 | { |
---|
172 | if ( BSP_Configuration.ticks_per_timeslice ) { |
---|
173 | |
---|
174 | /* XXX: turn off the timer interrupts */ |
---|
175 | |
---|
176 | /* we can't really disable the timer without disabling all external |
---|
177 | interupts. we'll slow down the decrementer to it's minimum speed. */ |
---|
178 | write_decrementer(~0); |
---|
179 | |
---|
180 | /* XXX: If necessary, restore the old vector */ |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /* |
---|
185 | * Clock_initialize |
---|
186 | * |
---|
187 | * Device driver entry point for clock tick driver initialization. |
---|
188 | */ |
---|
189 | |
---|
190 | rtems_device_driver Clock_initialize( |
---|
191 | rtems_device_major_number major, |
---|
192 | rtems_device_minor_number minor, |
---|
193 | void *pargp |
---|
194 | ) |
---|
195 | { |
---|
196 | Install_clock( Clock_isr ); |
---|
197 | |
---|
198 | /* |
---|
199 | * make major/minor avail to others such as shared memory driver |
---|
200 | */ |
---|
201 | |
---|
202 | rtems_clock_major = major; |
---|
203 | rtems_clock_minor = minor; |
---|
204 | |
---|
205 | return RTEMS_SUCCESSFUL; |
---|
206 | } |
---|
207 | |
---|
208 | rtems_device_driver Clock_control( |
---|
209 | rtems_device_major_number major, |
---|
210 | rtems_device_minor_number minor, |
---|
211 | void *pargp |
---|
212 | ) |
---|
213 | { |
---|
214 | rtems_unsigned32 isrlevel; |
---|
215 | rtems_libio_ioctl_args_t *args = pargp; |
---|
216 | |
---|
217 | if (args == 0) |
---|
218 | goto done; |
---|
219 | |
---|
220 | /* |
---|
221 | * This is hokey, but until we get a defined interface |
---|
222 | * to do this, it will just be this simple... |
---|
223 | */ |
---|
224 | |
---|
225 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) |
---|
226 | { |
---|
227 | Clock_isr(CLOCK_VECTOR); |
---|
228 | } |
---|
229 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) |
---|
230 | { |
---|
231 | rtems_interrupt_disable( isrlevel ); |
---|
232 | (void) set_vector( args->buffer, CLOCK_VECTOR, 1 ); |
---|
233 | rtems_interrupt_enable( isrlevel ); |
---|
234 | } |
---|
235 | |
---|
236 | done: |
---|
237 | return RTEMS_SUCCESSFUL; |
---|
238 | } |
---|