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

4.104.114.84.95
Last change on this file since b06e68ef was b06e68ef, checked in by Joel Sherrill <joel.sherrill@…>, on Aug 17, 1995 at 7:51:51 PM

Numerous miscellaneous features incorporated from Tony Bennett
(tbennett@…) including the following major additions:

+ variable length messages
+ named devices
+ debug monitor
+ association tables/variables

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