1 | /* Clock_initialize |
---|
2 | * |
---|
3 | * This routine initializes the 8254 timer under GO32. |
---|
4 | * The tick frequency is 1 millisecond. |
---|
5 | * |
---|
6 | * Input parameters: NONE |
---|
7 | * |
---|
8 | * Output parameters: NONE |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <bsp.h> |
---|
14 | #include <clockdrv.h> |
---|
15 | #include <stdlib.h> |
---|
16 | |
---|
17 | volatile rtems_unsigned32 Clock_driver_ticks; |
---|
18 | rtems_unsigned32 Clock_isrs_per_tick; /* ISRs per tick */ |
---|
19 | rtems_unsigned32 Clock_isrs; /* ISRs until next tick */ |
---|
20 | rtems_isr_entry Old_ticker; |
---|
21 | |
---|
22 | rtems_device_driver Clock_initialize( |
---|
23 | rtems_device_major_number major, |
---|
24 | rtems_device_minor_number minor, |
---|
25 | void *pargp, |
---|
26 | rtems_id tid, |
---|
27 | rtems_unsigned32 *rval |
---|
28 | ) |
---|
29 | { |
---|
30 | Install_clock( Clock_isr ); |
---|
31 | } |
---|
32 | |
---|
33 | void ReInstall_clock( |
---|
34 | rtems_isr_entry clock_isr |
---|
35 | ) |
---|
36 | { |
---|
37 | rtems_unsigned32 isrlevel = 0; |
---|
38 | |
---|
39 | rtems_interrupt_disable( isrlevel ); |
---|
40 | (void) set_vector( clock_isr, 0x8, 1 ); |
---|
41 | rtems_interrupt_enable( isrlevel ); |
---|
42 | } |
---|
43 | |
---|
44 | void Install_clock( |
---|
45 | rtems_isr_entry clock_isr |
---|
46 | ) |
---|
47 | { |
---|
48 | unsigned int microseconds_per_isr; |
---|
49 | |
---|
50 | #if 0 |
---|
51 | /* Initialize clock from on-board real time clock. This breaks the */ |
---|
52 | /* test code which assumes which assumes the application will do it. */ |
---|
53 | { |
---|
54 | rtems_time_of_day Now; |
---|
55 | extern void init_rtc( void ); |
---|
56 | extern long rtc_read( rtems_time_of_day * tod ); |
---|
57 | init_rtc(); |
---|
58 | if ( rtc_read( &Now ) >= 0 ) |
---|
59 | clock_set( &Now ); |
---|
60 | } |
---|
61 | #endif |
---|
62 | |
---|
63 | /* Start by assuming hardware counter is large enough, then */ |
---|
64 | /* scale it until it actually fits. */ |
---|
65 | Clock_driver_ticks = 0; |
---|
66 | Clock_isrs_per_tick = 1; |
---|
67 | |
---|
68 | if ( BSP_Configuration.microseconds_per_tick == 0 ) |
---|
69 | microseconds_per_isr = 10000; /* default 10 ms */ |
---|
70 | else |
---|
71 | microseconds_per_isr = BSP_Configuration.microseconds_per_tick; |
---|
72 | while ( US_TO_TICK(microseconds_per_isr) > 65535 ) { |
---|
73 | Clock_isrs_per_tick *= 10; |
---|
74 | microseconds_per_isr /= 10; |
---|
75 | } |
---|
76 | |
---|
77 | /* Initialize count in ckisr.c */ |
---|
78 | Clock_isrs = Clock_isrs_per_tick; |
---|
79 | |
---|
80 | #if 0 |
---|
81 | /* This was dropped in the last revision. Its a nice thing to know. */ |
---|
82 | TICKS_PER_SECOND = 1000000 / (Clock_isrs_per_tick * microseconds_per_isr); |
---|
83 | #endif |
---|
84 | |
---|
85 | if ( BSP_Configuration.ticks_per_timeslice ) { |
---|
86 | /* 105/88 approximates TIMER_TICK*1e-6 */ |
---|
87 | unsigned int count = US_TO_TICK( microseconds_per_isr ); |
---|
88 | |
---|
89 | Old_ticker = (rtems_isr_entry) set_vector( clock_isr, 0x8, 1 ); |
---|
90 | outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN ); |
---|
91 | outport_byte( TIMER_CNTR0, count >> 0 & 0xff ); |
---|
92 | outport_byte( TIMER_CNTR0, count >> 8 & 0xff ); |
---|
93 | } |
---|
94 | atexit( Clock_exit ); |
---|
95 | } |
---|
96 | |
---|
97 | void Clock_exit( void ) |
---|
98 | { |
---|
99 | if ( BSP_Configuration.ticks_per_timeslice ) { |
---|
100 | extern void rtc_set_dos_date( void ); |
---|
101 | |
---|
102 | /* reset to DOS value: */ |
---|
103 | outport_byte( TIMER_MODE, TIMER_SEL0|TIMER_16BIT|TIMER_RATEGEN ); |
---|
104 | outport_byte( TIMER_CNTR0, 0 ); |
---|
105 | outport_byte( TIMER_CNTR0, 0 ); |
---|
106 | |
---|
107 | /* reset time-of-day */ |
---|
108 | rtc_set_dos_date(); |
---|
109 | |
---|
110 | /* re-enable old handler: assume it was one of ours */ |
---|
111 | set_vector( (rtems_isr_entry)Old_ticker, 0x8, 1 ); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | #if 0 && defined(pentium) |
---|
117 | /* This can be used to get extremely accurate timing on a pentium. */ |
---|
118 | /* It isn't supported. [bryce] */ |
---|
119 | #define HZ 90.0 |
---|
120 | volatile long long Last_RDTSC; |
---|
121 | #define RDTSC()\ |
---|
122 | ({ long long _now; __asm __volatile (".byte 0x0F,0x31":"=A"(_now)); _now; }) |
---|
123 | long long Kernel_Time_ns( void ) |
---|
124 | { |
---|
125 | extern rtems_unsigned32 _TOD_Ticks_per_second; |
---|
126 | unsigned isrs_per_second = Clock_isrs_per_tick * _TOD_Ticks_per_second; |
---|
127 | long long now; |
---|
128 | int flags; |
---|
129 | disable_intr( flags ); |
---|
130 | now = 1e9 * Clock_driver_ticks / isrs_per_second |
---|
131 | + (RDTSC() - Last_RDTSC) * (1000.0/HZ); |
---|
132 | enable_intr( flags ); |
---|
133 | return now; |
---|
134 | } |
---|
135 | #endif |
---|