source: rtems/c/src/lib/libbsp/i386/go32/clock/rtc.c @ 3652ad35

4.104.114.84.95
Last change on this file since 3652ad35 was 4ca27cf, checked in by Joel Sherrill <joel.sherrill@…>, on 07/18/95 at 21:19:53

committing for rtems-3.2.01 snapshot

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