source: rtems/c/src/lib/libbsp/i386/go32/clock/rtc.c @ 637df35

4.104.114.84.95
Last change on this file since 637df35 was 637df35, checked in by Joel Sherrill <joel.sherrill@…>, on 07/12/95 at 19:47:25

Ada95, gnat, go32

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 *  $Id$
3 */
4
5#define IO_RTC          0x70    /* RTC */
6
7#define RTC_SEC         0x00    /* seconds */
8#define RTC_SECALRM     0x01    /* seconds alarm */
9#define RTC_MIN         0x02    /* minutes */
10#define RTC_MINALRM     0x03    /* minutes alarm */
11#define RTC_HRS         0x04    /* hours */
12#define RTC_HRSALRM     0x05    /* hours alarm */
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_INTR        0x0c    /* status register C (R) interrupt source */
23#define  RTCIR_UPDATE    0x10   /* update intr */
24#define  RTCIR_ALARM     0x20   /* alarm intr */
25#define  RTCIR_PERIOD    0x40   /* periodic intr */
26#define  RTCIR_INT       0x80   /* interrupt output signal */
27
28#define RTC_STATUSD     0x0d    /* status register D (R) Lost Power */
29#define  RTCSD_PWR       0x80   /* clock lost power */
30
31#define RTC_DIAG        0x0e    /* status register E - bios diagnostic */
32#define RTCDG_BITS      "\020\010clock_battery\007ROM_cksum\006config_unit\005memory_size\004fixed_disk\003invalid_time"
33
34#define RTC_CENTURY     0x32    /* current century - increment in Dec99 */
35
36
37
38#include <rtems.h>
39#include <cpu.h>
40#include <memory.h>
41
42void init_rtc( void )
43{
44    int s;
45
46        /* initialize brain-dead battery powered clock */
47        outport_byte( IO_RTC, RTC_STATUSA );
48        outport_byte( IO_RTC+1, 0x26 );
49        outport_byte( IO_RTC, RTC_STATUSB );
50        outport_byte( IO_RTC+1, 2 );
51
52        outport_byte( IO_RTC, RTC_DIAG );
53        inport_byte( IO_RTC+1, s );
54#if 0
55        if (s) printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS);
56#endif
57}
58
59
60/* convert 2 digit BCD number */
61static int bcd( unsigned int i )
62{
63        return ((i/16)*10 + (i%16));
64}
65
66/* convert years to seconds (from 1970) */
67static unsigned long ytos( int y )
68{
69        int i;
70        unsigned long ret;
71
72        ret = 0;
73        for(i = 1970; i < y; i++) {
74                if (i % 4) ret += 365*24*60*60;
75                else ret += 366*24*60*60;
76        }
77        return ret;
78}
79
80/* convert months to seconds */
81static unsigned long mtos( int m, int leap )
82{
83        int i;
84        unsigned long ret;
85
86        ret = 0;
87        for(i=1;i<m;i++) {
88                switch(i){
89                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
90                        ret += 31*24*60*60;
91                        break;
92                case 4: case 6: case 9: case 11:
93                        ret += 30*24*60*60;
94                        break;
95                case 2:
96                        if (leap)
97                            ret += 29*24*60*60;
98                        else
99                            ret += 28*24*60*60;
100                }
101        }
102        return ret;
103}
104
105
106static inline unsigned int rtcin( unsigned int what )
107{
108    unsigned int r;
109    outport_byte( IO_RTC, what );
110    inport_byte( IO_RTC+1, r );
111    return r;
112}
113
114
115/*
116 * Initialize the time of day register, based on the time base which is, e.g.
117 * from a filesystem.
118 */
119long rtc_read( rtems_time_of_day * tod )
120{
121        int sa;
122        unsigned long sec = 0;
123
124        memset( tod, 0, sizeof *tod );
125
126        /* do we have a realtime clock present? (otherwise we loop below) */
127        sa = rtcin(RTC_STATUSA);
128        if (sa == 0xff || sa == 0)
129            return -1;
130
131        /* ready for a read? */
132        while ((sa&RTCSA_TUP) == RTCSA_TUP)
133            sa = rtcin(RTC_STATUSA);
134
135        tod->year       = bcd(rtcin(RTC_YEAR)) + 1900;  /* year   */
136        if (tod->year < 1970)   tod->year += 100;       
137        tod->month      = bcd(rtcin(RTC_MONTH));        /* month   */
138        tod->day        = bcd(rtcin(RTC_DAY));          /* day     */
139        (void)            bcd(rtcin(RTC_WDAY));         /* weekday */
140        tod->hour       = bcd(rtcin(RTC_HRS));          /* hour    */
141        tod->minute     = bcd(rtcin(RTC_MIN));          /* minutes */
142        tod->second     = bcd(rtcin(RTC_SEC));          /* seconds */
143        tod->ticks      = 0;
144#if 0
145        sec = ytos( tod->year );
146        sec += mtos( tod->month, tod->year % 4 == 0 );
147        sec += tod->day * 24*60*60;
148        sec += tod->hour * 60*60;                       /* hour    */
149        sec += tod->minute * 60;                        /* minutes */
150        sec += tod->second;                             /* seconds */
151#else
152        sec = 0;
153#endif
154        return sec;
155}
156
157
158
159/* from djgpp: include before rtems.h to avoid conflicts */
160#undef delay
161#include <dos.h>
162
163void rtc_set_dos_date( void )
164{
165    int s;
166    struct date date;
167    struct time time;
168
169    /* initialize brain-dead battery powered clock */
170    outport_byte( IO_RTC, RTC_STATUSA );
171    outport_byte( IO_RTC+1, 0x26 );
172    outport_byte( IO_RTC, RTC_STATUSB );
173    outport_byte( IO_RTC+1, 2 );
174
175    outport_byte( IO_RTC, RTC_DIAG );
176    inport_byte( IO_RTC+1, s );
177    if (s)  {
178#if 0
179        printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS);
180#else
181        return;
182#endif
183    }
184
185    /* check for presence of clock */
186    s = rtcin(RTC_STATUSA);
187    if ( s == 0xff  ||  s == 0 )  {
188#if 0
189        printf( "Real-time clock not found\n" );
190#endif
191        return;
192    }
193
194    /* ready for a read? */
195    while ((s & RTCSA_TUP) == RTCSA_TUP)
196        s = rtcin(RTC_STATUSA);
197
198    date.da_year        = bcd(rtcin(RTC_YEAR)) + 1900;  /* year   */
199    if ( date.da_year < 1970)   date.da_year += 100;
200    date.da_year        -= 1980;
201    date.da_mon         = bcd(rtcin(RTC_MONTH));        /* month   */
202    date.da_day         = bcd(rtcin(RTC_DAY));          /* day     */
203
204    (void)bcd(rtcin(RTC_WDAY));         /* weekday */
205
206    time.ti_hour        = bcd(rtcin(RTC_HRS));          /* hour    */
207    time.ti_min         = bcd(rtcin(RTC_MIN));          /* minutes */
208    time.ti_sec         = bcd(rtcin(RTC_SEC));          /* seconds */
209    time.ti_hund        = 0;
210
211    setdate( & date );
212    settime( & time );
213}
Note: See TracBrowser for help on using the repository browser.