summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/go32/timer/timer.c
blob: a7f911baee49a5849b2bfb04a0d392f45da183cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*  Timer_init()
 *
 *  This routine initializes the timer on the IBM 386.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters:  NONE
 *
 *  NOTE: It is important that the timer start/stop overhead be 
 *        determined when porting or modifying this code.
 *
 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
 *  On-Line Applications Research Corporation (OAR).
 *  All rights assigned to U.S. Government, 1994.
 *
 *  This material may be reproduced by or for the U.S. Government pursuant
 *  to the copyright license under the clause at DFARS 252.227-7013.  This
 *  notice must appear in all copies of this file and its derivatives.
 *
 *  $Id$
 */

#include <rtems.h>
#include <bsp.h>

volatile rtems_unsigned32 Ttimer_val;
rtems_boolean Timer_driver_Find_average_overhead;

#if defined(pentium)
static inline unsigned long long rdtsc( void )
{
    /* Return the value of the on-chip cycle counter. */
    unsigned long long result;
    __asm __volatile( ".byte 0x0F, 0x31" : "=A" (result) );
    return result;
}
static void restore_timer( void )
{
    CLOCK_ENABLE();
}
#else /* pentium */
rtems_isr timerisr();
#endif /* pentium */

void Timer_initialize()
{
    static int First = 1;
#if defined(pentium)
    if ( First )  {
        extern int atexit( void (*)(void) );
        First = 0;
        /* Disable the programmable timer. */
        CLOCK_DISABLE();
        /* Try not to hose the system on return to DOS. */
        atexit( restore_timer );
    }
    Ttimer_val = rdtsc();
#else /* pentium */

#define WAIT() \
  { \
    Ttimer_val = 0; \
    while ( Ttimer_val == 0 ) \
      continue; \
    Ttimer_val = 0; \
  }

    if ( First )  {
        First = 0;

        /* install ISR */
        set_vector( timerisr, 0x8, 0 );

       /* Wait for ISR to be called at least once */
       WAIT();

       /* load timer for 250 microsecond period */
       outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN );
       outport_byte( TIMER_CNTR0, US_TO_TICK(250) >> 0 & 0xff);
       outport_byte( TIMER_CNTR0, US_TO_TICK(250) >> 8 & 0xff);
    }

    /* Wait for ISR to be called at least once */
    WAIT();
#endif /* PENTIUM */
}

#define AVG_OVERHEAD       0  /* 0.1 microseconds to start/stop timer. */
#define LEAST_VALID        1  /* Don't trust a value lower than this */


int Read_timer()
{
    register rtems_unsigned32 total;
#if defined(pentium)
    total = rdtsc() - Ttimer_val;
#else /* pentium */
    register rtems_unsigned8 lsb, msb;
    register rtems_unsigned32 clicks;
    outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_LATCH );
    inport_byte( TIMER_CNTR0, lsb );
    inport_byte( TIMER_CNTR0, msb );
    clicks = msb << 8 | lsb;
    total = Ttimer_val + (250 - TICK_TO_US( clicks ));
#endif /* pentium */

    if ( Timer_driver_Find_average_overhead == 1 )
        return total;
    else if ( total < LEAST_VALID )
        return 0;               /* below timer resolution */
    else
        return total - AVG_OVERHEAD;
}

rtems_status_code Empty_function( void )
{
    return RTEMS_SUCCESSFUL;
}


void Set_find_average_overhead(
  rtems_boolean find_flag
)
{
  Timer_driver_Find_average_overhead = find_flag;
}