1 | /* clock.c |
---|
2 | * |
---|
3 | * This routine initializes the PIT on the MPC8xx. |
---|
4 | * The tick frequency is specified by the bsp. |
---|
5 | * |
---|
6 | * Author: Jay Monkman (jmonkman@frasca.com) |
---|
7 | * Copyright (C) 1998 by Frasca International, Inc. |
---|
8 | * |
---|
9 | * Derived from c/src/lib/libcpu/ppc/ppc403/clock/clock.c: |
---|
10 | * |
---|
11 | * Author: Andrew Bray <andy@i-cubed.co.uk> |
---|
12 | * |
---|
13 | * COPYRIGHT (c) 1995 by i-cubed ltd. |
---|
14 | * |
---|
15 | * To anyone who acknowledges that this file is provided "AS IS" |
---|
16 | * without any express or implied warranty: |
---|
17 | * permission to use, copy, modify, and distribute this file |
---|
18 | * for any purpose is hereby granted without fee, provided that |
---|
19 | * the above copyright notice and this notice appears in all |
---|
20 | * copies, and that the name of i-cubed limited not be used in |
---|
21 | * advertising or publicity pertaining to distribution of the |
---|
22 | * software without specific, written prior permission. |
---|
23 | * i-cubed limited makes no representations about the suitability |
---|
24 | * of this software for any purpose. |
---|
25 | * |
---|
26 | * Derived from c/src/lib/libcpu/hppa1_1/clock/clock.c: |
---|
27 | * |
---|
28 | * COPYRIGHT (c) 1989-1998. |
---|
29 | * On-Line Applications Research Corporation (OAR). |
---|
30 | * Copyright assigned to U.S. Government, 1994. |
---|
31 | * |
---|
32 | * The license and distribution terms for this file may be |
---|
33 | * found in the file LICENSE in this distribution or at |
---|
34 | * http://www.OARcorp.com/rtems/license.html. |
---|
35 | * |
---|
36 | * $Id$ |
---|
37 | */ |
---|
38 | |
---|
39 | #include <rtems.h> |
---|
40 | #include <clockdrv.h> |
---|
41 | #include <rtems/libio.h> |
---|
42 | |
---|
43 | #include <stdlib.h> /* for atexit() */ |
---|
44 | #include <mpc8260.h> |
---|
45 | #include <bsp/irq.h> |
---|
46 | |
---|
47 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
48 | extern int BSP_get_clock_irq_level(); |
---|
49 | extern int BSP_connect_clock_handler(rtems_isr_entry); |
---|
50 | extern int BSP_disconnect_clock_handler(); |
---|
51 | |
---|
52 | void Clock_exit( void ); |
---|
53 | |
---|
54 | rtems_unsigned32 decrementer_value; |
---|
55 | |
---|
56 | volatile int ClockInitialised = 0; |
---|
57 | |
---|
58 | |
---|
59 | /* |
---|
60 | * These are set by clock driver during its init |
---|
61 | */ |
---|
62 | |
---|
63 | rtems_device_major_number rtems_clock_major = ~0; |
---|
64 | rtems_device_minor_number rtems_clock_minor; |
---|
65 | |
---|
66 | /* |
---|
67 | * ISR Handler |
---|
68 | */ |
---|
69 | rtems_isr Clock_isr(rtems_vector_number vector) |
---|
70 | { |
---|
71 | int clicks; |
---|
72 | |
---|
73 | if( ClockInitialised ) { |
---|
74 | PPC_Get_decrementer( clicks ); |
---|
75 | do { |
---|
76 | clicks += decrementer_value; |
---|
77 | PPC_Set_decrementer( clicks ); |
---|
78 | |
---|
79 | Clock_driver_ticks++; |
---|
80 | rtems_clock_tick(); |
---|
81 | } while( clicks < 100 ); |
---|
82 | } |
---|
83 | #if 0 |
---|
84 | m8260.piscr |= M8260_PISCR_PS; |
---|
85 | Clock_driver_ticks++; |
---|
86 | rtems_clock_tick(); |
---|
87 | #endif |
---|
88 | |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | void clockOn(void* unused) |
---|
93 | { |
---|
94 | |
---|
95 | decrementer_value = rtems_configuration_get_microseconds_per_tick() * |
---|
96 | rtems_cpu_configuration_get_clicks_per_usec() - 1; |
---|
97 | |
---|
98 | PPC_Set_decrementer( decrementer_value ); |
---|
99 | Clock_driver_ticks = 0; |
---|
100 | |
---|
101 | ClockInitialised = 1; |
---|
102 | |
---|
103 | |
---|
104 | #if 0 |
---|
105 | unsigned desiredLevel; |
---|
106 | rtems_unsigned32 pit_value; |
---|
107 | |
---|
108 | pit_value = (rtems_configuration_get_microseconds_per_tick() * |
---|
109 | rtems_cpu_configuration_get_clicks_per_usec()) - 1 ; |
---|
110 | |
---|
111 | if (pit_value > 0xffff) { /* pit is only 16 bits long */ |
---|
112 | rtems_fatal_error_occurred(-1); |
---|
113 | } |
---|
114 | m8260.sccr &= ~(1<<24); |
---|
115 | m8260.pitc = pit_value; |
---|
116 | |
---|
117 | desiredLevel = BSP_get_clock_irq_level(); |
---|
118 | /* set PIT irq level, enable PIT, PIT interrupts */ |
---|
119 | /* and clear int. status */ |
---|
120 | m8260.piscr = /*M8260_PISCR_PIRQ(desiredLevel) |*/ |
---|
121 | M8260_PISCR_PTE | M8260_PISCR_PS | M8260_PISCR_PIE; |
---|
122 | #endif |
---|
123 | } |
---|
124 | /* |
---|
125 | * Called via atexit() |
---|
126 | * Remove the clock interrupt handler by setting handler to NULL |
---|
127 | */ |
---|
128 | void |
---|
129 | clockOff(void* unused) |
---|
130 | { |
---|
131 | #if 0 |
---|
132 | /* disable PIT and PIT interrupts */ |
---|
133 | m8260.piscr &= ~(M8260_PISCR_PTE | M8260_PISCR_PIE); |
---|
134 | #endif |
---|
135 | ClockInitialised = 0; |
---|
136 | } |
---|
137 | |
---|
138 | int clockIsOn(void* unused) |
---|
139 | { |
---|
140 | return ClockInitialised; |
---|
141 | #if 0 |
---|
142 | if (m8260.piscr & M8260_PISCR_PIE) return 1; |
---|
143 | return 0; |
---|
144 | #endif |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | /* |
---|
149 | * Called via atexit() |
---|
150 | * Remove the clock interrupt handler by setting handler to NULL |
---|
151 | */ |
---|
152 | void |
---|
153 | Clock_exit(void) |
---|
154 | { |
---|
155 | (void) BSP_disconnect_clock_handler (); |
---|
156 | } |
---|
157 | |
---|
158 | void Install_clock(rtems_isr_entry clock_isr) |
---|
159 | { |
---|
160 | Clock_driver_ticks = 0; |
---|
161 | |
---|
162 | |
---|
163 | decrementer_value = rtems_configuration_get_microseconds_per_tick() * |
---|
164 | rtems_cpu_configuration_get_clicks_per_usec() - 1; |
---|
165 | |
---|
166 | PPC_Set_decrementer( decrementer_value ); |
---|
167 | |
---|
168 | BSP_connect_clock_handler (clock_isr); |
---|
169 | |
---|
170 | |
---|
171 | ClockInitialised = 1; |
---|
172 | |
---|
173 | atexit(Clock_exit); |
---|
174 | |
---|
175 | } |
---|
176 | |
---|
177 | void |
---|
178 | ReInstall_clock(rtems_isr_entry new_clock_isr) |
---|
179 | { |
---|
180 | BSP_connect_clock_handler (new_clock_isr); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | rtems_device_driver Clock_initialize( |
---|
185 | rtems_device_major_number major, |
---|
186 | rtems_device_minor_number minor, |
---|
187 | void *pargp |
---|
188 | ) |
---|
189 | { |
---|
190 | Install_clock( Clock_isr ); |
---|
191 | |
---|
192 | /* |
---|
193 | * make major/minor avail to others such as shared memory driver |
---|
194 | */ |
---|
195 | |
---|
196 | rtems_clock_major = major; |
---|
197 | rtems_clock_minor = minor; |
---|
198 | |
---|
199 | return RTEMS_SUCCESSFUL; |
---|
200 | } |
---|
201 | |
---|
202 | rtems_device_driver Clock_control( |
---|
203 | rtems_device_major_number major, |
---|
204 | rtems_device_minor_number minor, |
---|
205 | void *pargp |
---|
206 | ) |
---|
207 | { |
---|
208 | rtems_libio_ioctl_args_t *args = pargp; |
---|
209 | |
---|
210 | if (args == 0) |
---|
211 | goto done; |
---|
212 | |
---|
213 | /* |
---|
214 | * This is hokey, but until we get a defined interface |
---|
215 | * to do this, it will just be this simple... |
---|
216 | */ |
---|
217 | |
---|
218 | if (args->command == rtems_build_name('I', 'S', 'R', ' ')) { |
---|
219 | Clock_isr(BSP_PERIODIC_TIMER); |
---|
220 | } |
---|
221 | else if (args->command == rtems_build_name('N', 'E', 'W', ' ')) { |
---|
222 | ReInstall_clock(args->buffer); |
---|
223 | } |
---|
224 | |
---|
225 | done: |
---|
226 | return RTEMS_SUCCESSFUL; |
---|
227 | } |
---|
228 | |
---|