summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/go32/timer/timer.c
blob: 252c1a771f5aeb8bc5ad24b372acc8c0c0cef238 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*  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-1997.
 *  On-Line Applications Research Corporation (OAR).
 *  Copyright assigned to U.S. Government, 1994.
 *
 *  The license and distribution terms for this file may in
 *  the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $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;
}
#else
#define US_PER_ISR  250
extern rtems_isr timerisr();
static rtems_isr_entry Old_Ticker;
#endif

static void Timer_exit( void )
{
#if defined(pentium)
    CLOCK_ENABLE();
#else /* pentium */
    extern void rtc_set_dos_date( void );

    /* reset to DOS value: */
    outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN );
    outport_byte( TIMER_CNTR0, 0 );
    outport_byte( TIMER_CNTR0, 0 );

    /* reset time-of-day */
    rtc_set_dos_date();
        
    /* re-enable old handler: assume it was one of ours */
    set_vector( (rtems_isr_entry)Old_Ticker, 0x8, 0 );
#endif /* pentium */
}

void Timer_initialize()
{
    extern int atexit( void (*)(void) );

    static int First = 1;

    if ( First )  {
        First = 0;

        /* Try not to hose the system on return to DOS. */
        atexit( Timer_exit );

#if defined(pentium)
        /* Disable the programmable timer so ticks don't interfere. */
        CLOCK_DISABLE();
#else /* pentium */
        /* install a timer ISR */
        Old_Ticker = (rtems_isr_entry) set_vector( timerisr, 0x8, 0 );

        /* Wait for ISR to be called at least once */
        Ttimer_val = 0;
        while ( Ttimer_val == 0 )
            continue;

        /* load timer for US_PER_ISR microsecond period */
        outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN );
        outport_byte( TIMER_CNTR0, US_TO_TICK(US_PER_ISR) >> 0 & 0xff );
        outport_byte( TIMER_CNTR0, US_TO_TICK(US_PER_ISR) >> 8 & 0xff );
#endif /* PENTIUM */
    }
#if defined(pentium)
    Ttimer_val = rdtsc();       /* read starting time */
#else
    /* Wait for ISR to be called at least once */
    asm( "sti" );
    Ttimer_val = 0;
    while ( Ttimer_val == 0 )
        continue;
    Ttimer_val = 0;
#endif
}

#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 * US_PER_ISR) + (US_PER_ISR - 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;
}