source: rtems/c/src/lib/libcpu/hppa1.1/clock/clock.c @ bc2f04f

4.104.114.84.95
Last change on this file since bc2f04f was 08311cc3, checked in by Joel Sherrill <joel.sherrill@…>, on 11/17/99 at 17:51:34

Updated copyright notice.

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