source: rtems/c/src/lib/libbsp/i386/pc386/clock/ckinit.c @ 08311cc3

4.104.114.84.95
Last change on this file since 08311cc3 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: 9.3 KB
Line 
1/*-------------------------------------------------------------------------+
2| ckinit.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| This file contains the PC386 clock package.
5+--------------------------------------------------------------------------+
6| (C) Copyright 1997 -
7| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
8|
9| http://pandora.ist.utl.pt
10|
11| Instituto Superior Tecnico * Lisboa * PORTUGAL
12+--------------------------------------------------------------------------+
13| Disclaimer:
14|
15| This file is provided "AS IS" without warranty of any kind, either
16| expressed or implied.
17+--------------------------------------------------------------------------+
18| This code is based on:
19|   ckinit.c,v 1.4 1995/12/19 20:07:13 joel Exp - go32 BSP
20| With the following copyright notice:
21| **************************************************************************
22| *  COPYRIGHT (c) 1989-1999.
23| *  On-Line Applications Research Corporation (OAR).
24| *
25| *  The license and distribution terms for this file may be
26| *  found in found in the file LICENSE in this distribution or at
27| *  http://www.OARcorp.com/rtems/license.html.
28| **************************************************************************
29|
30|  $Id$
31+--------------------------------------------------------------------------*/
32
33
34#include <stdlib.h>
35
36#include <bsp.h>
37#include <irq.h>
38#include <rtems/libio.h>
39
40/*-------------------------------------------------------------------------+
41| Macros
42+--------------------------------------------------------------------------*/
43#if 0
44/* This was dropped in the last revision.  Its a nice thing to know. */
45#define TICKS_PER_SECOND() \
46          (1000000 / (Clock_isrs_per_tick * microseconds_per_isr))
47#endif /* 0 */
48
49/*-------------------------------------------------------------------------+
50| Global Variables
51+--------------------------------------------------------------------------*/
52
53volatile rtems_unsigned32 Clock_driver_ticks;   /* Tick (interrupt) counter. */
54         rtems_unsigned32 Clock_isrs_per_tick;  /* ISRs per tick.            */
55         rtems_unsigned32 Clock_isrs;           /* ISRs until next tick.     */
56
57/* The following variables are set by the clock driver during its init */
58
59rtems_device_major_number rtems_clock_major = ~0;
60rtems_device_minor_number rtems_clock_minor;
61
62/*-------------------------------------------------------------------------+
63|         Function: clockIsr
64|      Description: Interrupt Service Routine for clock (0h) interruption.
65| Global Variables: Clock_driver_ticks, Clock_isrs.
66|        Arguments: vector - standard RTEMS argument - see documentation.
67|          Returns: standard return value - see documentation.
68+--------------------------------------------------------------------------*/
69static void clockIsr()
70{
71  /*-------------------------------------------------------------------------+
72  | PLEASE NOTE: The following is directly transcribed from the go32 BSP for
73  |              those who wish to use it with PENTIUM based machine. It needs
74  |              to be correctly integrated with the rest of the code!!!
75  +--------------------------------------------------------------------------*/
76
77#if 0 && defined(pentium) /* more accurate clock for PENTIUMs (not supported) */
78  {
79    extern long long Last_RDTSC;
80    __asm __volatile(".byte 0x0F, 0x31" : "=A" (Last_RDTSC));
81  }
82#endif /* 0 && pentium */
83
84  Clock_driver_ticks++;
85
86  if ( Clock_isrs == 1 )
87  {
88    rtems_clock_tick();
89    Clock_isrs = Clock_isrs_per_tick;
90  }
91  else
92    Clock_isrs--;
93
94} /* clockIsr */
95
96/*-------------------------------------------------------------------------+
97|         Function: Clock_exit
98|      Description: Clock cleanup routine at RTEMS exit. NOTE: This routine is
99|                   not really necessary, since there will be a reset at exit.
100| Global Variables: None.
101|        Arguments: None.
102|          Returns: Nothing.
103+--------------------------------------------------------------------------*/
104void clockOff(const rtems_irq_connect_data* unused)
105{
106  if (BSP_Configuration.ticks_per_timeslice)
107  {
108    /* reset timer mode to standard (BIOS) value */
109    outport_byte(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN);
110    outport_byte(TIMER_CNTR0, 0);
111    outport_byte(TIMER_CNTR0, 0);
112  }
113} /* Clock_exit */
114
115
116/*-------------------------------------------------------------------------+
117|         Function: Install_clock
118|      Description: Initialize and install clock interrupt handler.
119| Global Variables: None.
120|        Arguments: None.
121|          Returns: Nothing.
122+--------------------------------------------------------------------------*/
123static void clockOn(const rtems_irq_connect_data* unused)
124{
125  rtems_unsigned32  microseconds_per_isr;
126
127#if 0
128  /* Initialize clock from on-board real time clock.  This breaks the  */
129  /* test code which assumes which assumes the application will do it. */
130  {
131    rtems_time_of_day now;
132
133    /* External Prototypes */
134    extern void init_rtc(void);                /* defined in 'rtc.c' */
135    extern long rtc_read(rtems_time_of_day *); /* defined in 'rtc.c' */
136
137    init_rtc();
138    if (rtc_read(&now) >= 0)
139      clock_set(&now);
140  }
141#endif /* 0 */
142
143  /* Start by assuming hardware counter is large enough, then  scale it until
144     it actually fits. */
145
146  Clock_driver_ticks  = 0;
147  Clock_isrs_per_tick = 1;
148
149  if (BSP_Configuration.microseconds_per_tick == 0)
150    microseconds_per_isr = 10000; /* default 10 ms */
151  else
152    microseconds_per_isr = BSP_Configuration.microseconds_per_tick;
153  while (US_TO_TICK(microseconds_per_isr) > 65535)
154  {
155    Clock_isrs_per_tick  *= 10;
156    microseconds_per_isr /= 10;
157  }
158
159  Clock_isrs = Clock_isrs_per_tick; /* Initialize Clock_isrs */
160
161  if (BSP_Configuration.ticks_per_timeslice)
162  {
163    /* 105/88 approximates TIMER_TICK * 1e-6 */
164    rtems_unsigned32 count = US_TO_TICK(microseconds_per_isr);
165
166    outport_byte(TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN);
167    outport_byte(TIMER_CNTR0, count >> 0 & 0xff);
168    outport_byte(TIMER_CNTR0, count >> 8 & 0xff);
169  }
170
171}
172
173int clockIsOn(const rtems_irq_connect_data* unused)
174{
175  return ((i8259s_cache & 0x1) == 0);
176}
177
178static rtems_irq_connect_data clockIrqData = {BSP_PERIODIC_TIMER,
179                                              clockIsr,
180                                              clockOn,
181                                              clockOff,
182                                              clockIsOn};
183                                             
184                                             
185
186/*-------------------------------------------------------------------------+
187| Clock device driver INITIALIZE entry point.
188+--------------------------------------------------------------------------+
189| Initilizes the clock driver.
190+--------------------------------------------------------------------------*/
191rtems_device_driver
192Clock_initialize(rtems_device_major_number major,
193                 rtems_device_minor_number minor,
194                 void                      *pargp)
195{
196
197  if (!BSP_install_rtems_irq_handler (&clockIrqData)) {
198    printk("Unable to initialize system clock\n");
199    rtems_fatal_error_occurred(1);
200  }
201  /* make major/minor avail to others such as shared memory driver */
202 
203  rtems_clock_major = major;
204  rtems_clock_minor = minor;
205 
206  return RTEMS_SUCCESSFUL;
207} /* Clock_initialize */
208
209                                             
210/*-------------------------------------------------------------------------+
211| Console device driver CONTROL entry point
212+--------------------------------------------------------------------------*/
213rtems_device_driver
214Clock_control(rtems_device_major_number major,
215              rtems_device_minor_number minor,
216              void                      *pargp)
217{
218  if (pargp != NULL)
219  {
220    rtems_libio_ioctl_args_t *args = pargp;
221       
222    /*-------------------------------------------------------------------------+
223    | This is hokey, but until we get a defined interface to do this, it will
224    | just be this simple...
225    +-------------------------------------------------------------------------*/
226 
227    if      (args->command == rtems_build_name('I', 'S', 'R', ' '))
228      clockIsr();
229    else if (args->command == rtems_build_name('N', 'E', 'W', ' '))
230    {
231      if (!BSP_install_rtems_irq_handler (&clockIrqData)) {
232        printk("Error installing clock interrupt handler!\n");
233        rtems_fatal_error_occurred(1);
234      }
235    }
236  }
237
238  return RTEMS_SUCCESSFUL;
239} /* Clock_control */
240
241void Clock_exit()
242{
243  BSP_remove_rtems_irq_handler (&clockIrqData);
244}
245
246/*-------------------------------------------------------------------------+
247| PLEASE NOTE: The following is directly transcribed from the go32 BSP for
248|              those who wish to use it with PENTIUM based machine. It needs
249|              to be correctly integrated with the rest of the code!!!
250+--------------------------------------------------------------------------*/
251
252
253#if 0 && defined(pentium)
254
255/* This can be used to get extremely accurate timing on a pentium. */
256/* It isn't supported. [bryce]                                     */
257
258#define HZ 90.0
259
260volatile long long Last_RDTSC;
261
262#define RDTSC()\
263  ({ long long _now; __asm __volatile (".byte 0x0F,0x31":"=A"(_now)); _now; })
264
265long long Kernel_Time_ns( void )
266{
267  extern rtems_unsigned32 _TOD_Ticks_per_second;
268
269  unsigned  isrs_per_second = Clock_isrs_per_tick * _TOD_Ticks_per_second;
270  long long now;
271  int       flags;
272
273  disable_intr(flags);
274  now = 1e9 * Clock_driver_ticks / isrs_per_second +
275        (RDTSC() - Last_RDTSC) * (1000.0/HZ);
276  enable_intr(flags);
277  return now;
278} /* Kernel_Time_ns */
279
280#endif /* 0 && pentium */
Note: See TracBrowser for help on using the repository browser.