1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <dos.h> |
---|
7 | |
---|
8 | #define IO_RTC 0x70 /* RTC */ |
---|
9 | |
---|
10 | #define RTC_SEC 0x00 /* seconds */ |
---|
11 | #define RTC_MIN 0x02 /* minutes */ |
---|
12 | #define RTC_HRS 0x04 /* hours */ |
---|
13 | #define RTC_WDAY 0x06 /* week day */ |
---|
14 | #define RTC_DAY 0x07 /* day of month */ |
---|
15 | #define RTC_MONTH 0x08 /* month of year */ |
---|
16 | #define RTC_YEAR 0x09 /* month of year */ |
---|
17 | #define RTC_STATUSA 0x0a /* status register A */ |
---|
18 | #define RTCSA_TUP 0x80 /* time update, don't look now */ |
---|
19 | |
---|
20 | #define RTC_STATUSB 0x0b /* status register B */ |
---|
21 | |
---|
22 | #define RTC_DIAG 0x0e /* status register E - bios diagnostic */ |
---|
23 | #define RTCDG_BITS "\020\010clock_battery\007ROM_cksum\006config_unit\005memory_size\004fixed_disk\003invalid_time" |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | /* convert 2 digit BCD number */ |
---|
28 | static int bcd( unsigned int i ) |
---|
29 | { |
---|
30 | return ((i/16)*10 + (i%16)); |
---|
31 | } |
---|
32 | |
---|
33 | static unsigned int rtcin( unsigned int what ) |
---|
34 | { |
---|
35 | outportb( IO_RTC, what ); |
---|
36 | return inportb( IO_RTC+1 ); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | void fix_date( void ) |
---|
41 | { |
---|
42 | int s; |
---|
43 | struct date date; |
---|
44 | struct time time; |
---|
45 | |
---|
46 | /* initialize brain-dead battery powered clock */ |
---|
47 | outportb( IO_RTC, RTC_STATUSA ); |
---|
48 | outportb( IO_RTC+1, 0x26 ); |
---|
49 | outportb( IO_RTC, RTC_STATUSB ); |
---|
50 | outportb( IO_RTC+1, 2 ); |
---|
51 | |
---|
52 | outportb( IO_RTC, RTC_DIAG ); |
---|
53 | s = inportb( IO_RTC+1 ); |
---|
54 | if (s) printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS); |
---|
55 | |
---|
56 | /* check for presence of clock */ |
---|
57 | s = rtcin(RTC_STATUSA); |
---|
58 | if ( s == 0xff || s == 0 ) { |
---|
59 | printf( "Real-time clock not found\n" ); |
---|
60 | return; |
---|
61 | } |
---|
62 | |
---|
63 | /* ready for a read? */ |
---|
64 | while ((s & RTCSA_TUP) == RTCSA_TUP) |
---|
65 | s = rtcin(RTC_STATUSA); |
---|
66 | |
---|
67 | date.da_year = bcd(rtcin(RTC_YEAR)) + 1900; /* year */ |
---|
68 | if ( date.da_year < 1970) date.da_year += 100; |
---|
69 | date.da_year -= 1980; |
---|
70 | date.da_mon = bcd(rtcin(RTC_MONTH)); /* month */ |
---|
71 | date.da_day = bcd(rtcin(RTC_DAY)); /* day */ |
---|
72 | |
---|
73 | (void)bcd(rtcin(RTC_WDAY)); /* weekday */ |
---|
74 | |
---|
75 | time.ti_hour = bcd(rtcin(RTC_HRS)); /* hour */ |
---|
76 | time.ti_min = bcd(rtcin(RTC_MIN)); /* minutes */ |
---|
77 | time.ti_sec = bcd(rtcin(RTC_SEC)); /* seconds */ |
---|
78 | time.ti_hund = 0; |
---|
79 | |
---|
80 | setdate( & date ); |
---|
81 | settime( & time ); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | void fix_timer( void ) |
---|
88 | { |
---|
89 | #define PIT_PORT 0x40 |
---|
90 | #define TIMER_CNTR0 (PIT_PORT + 0) /* timer 0 counter port */ |
---|
91 | #define TIMER_MODE (PIT_PORT + 3) /* timer mode port */ |
---|
92 | #define TIMER_SEL0 0x00 /* select counter 0 */ |
---|
93 | #define TIMER_RATEGEN 0x04 /* mode 2, rate generator */ |
---|
94 | #define TIMER_16BIT 0x30 /* r/w counter 16 bits, LSB first */ |
---|
95 | |
---|
96 | /* set timer 0 to generate interrupts every period */ |
---|
97 | outportb( TIMER_MODE, TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT ); |
---|
98 | outportb( TIMER_CNTR0, 0 ); /* counter low */ |
---|
99 | outportb( TIMER_CNTR0, 0 ); /* counter high */ |
---|
100 | } |
---|
101 | |
---|
102 | int main(void) |
---|
103 | { |
---|
104 | /* Make timer count at correct pace */ |
---|
105 | fix_timer(); |
---|
106 | |
---|
107 | /* Fix the date */ |
---|
108 | fix_date(); |
---|
109 | |
---|
110 | return 0; |
---|
111 | } |
---|