Annotation of Gnu-Mach/i386/i386at/rtc.c, revision 1.1.1.5

1.1.1.2   root        1: /*
1.1       root        2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
                      4:  * All Rights Reserved.
1.1.1.2   root        5:  *
1.1       root        6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
1.1.1.2   root       11:  *
1.1       root       12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1.1.1.2   root       15:  *
1.1       root       16:  * Carnegie Mellon requests users of this software to return to
1.1.1.2   root       17:  *
1.1       root       18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
1.1.1.2   root       22:  *
1.1       root       23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: 
                     27: /*
                     28:   Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
                     29: 
                     30:                All Rights Reserved
                     31: 
                     32: Permission to use, copy, modify, and distribute this software and
                     33: its documentation for any purpose and without fee is hereby
                     34: granted, provided that the above copyright notice appears in all
                     35: copies and that both the copyright notice and this permission notice
                     36: appear in supporting documentation, and that the name of Intel
                     37: not be used in advertising or publicity pertaining to distribution
                     38: of the software without specific, written prior permission.
                     39: 
                     40: INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
                     41: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
                     42: IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
                     43: CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
                     44: LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
                     45: NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
                     46: WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     47: */
                     48: 
                     49: #include <sys/types.h>
                     50: #include <sys/time.h>
1.1.1.3   root       51: #include <kern/mach_clock.h>
1.1       root       52: #include <i386/machspl.h>
1.1.1.3   root       53: #include <i386/pio.h>
1.1       root       54: #include <i386at/rtc.h>
                     55: 
1.1.1.4   root       56: static boolean_t first_rtcopen_ever = TRUE;
1.1       root       57: 
1.1.1.3   root       58: void
1.1.1.4   root       59: rtcinit(void)
1.1       root       60: {
                     61:        outb(RTC_ADDR, RTC_A);
                     62:        outb(RTC_DATA, RTC_DIV2 | RTC_RATE6);
                     63:        outb(RTC_ADDR, RTC_B);
                     64:        outb(RTC_DATA, RTC_HM);
                     65: }
                     66: 
                     67: 
                     68: int
1.1.1.5 ! root       69: rtcget(struct rtc_st *st)
1.1       root       70: {
1.1.1.5 ! root       71:        unsigned char *regs = (unsigned char *)st;
1.1       root       72:        if (first_rtcopen_ever) {
                     73:                rtcinit();
1.1.1.4   root       74:                first_rtcopen_ever = FALSE;
1.1       root       75:        }
1.1.1.2   root       76:        outb(RTC_ADDR, RTC_D);
1.1.1.3   root       77:        if ((inb(RTC_DATA) & RTC_VRT) == 0) return(-1);
1.1.1.2   root       78:        outb(RTC_ADDR, RTC_A);
1.1       root       79:        while (inb(RTC_DATA) & RTC_UIP)         /* busy wait */
1.1.1.2   root       80:                outb(RTC_ADDR, RTC_A);
1.1       root       81:        load_rtc(regs);
                     82:        return(0);
1.1.1.2   root       83: }
1.1       root       84: 
1.1.1.3   root       85: void
1.1.1.5 ! root       86: rtcput(struct rtc_st *st)
1.1       root       87: {
1.1.1.5 ! root       88:        unsigned char *regs = (unsigned char *)st;
1.1.1.4   root       89:        unsigned char   x;
1.1       root       90: 
                     91:        if (first_rtcopen_ever) {
                     92:                rtcinit();
1.1.1.4   root       93:                first_rtcopen_ever = FALSE;
1.1       root       94:        }
                     95:        outb(RTC_ADDR, RTC_B);
                     96:        x = inb(RTC_DATA);
                     97:        outb(RTC_ADDR, RTC_B);
1.1.1.2   root       98:        outb(RTC_DATA, x | RTC_SET);
1.1       root       99:        save_rtc(regs);
                    100:        outb(RTC_ADDR, RTC_B);
1.1.1.2   root      101:        outb(RTC_DATA, x & ~RTC_SET);
1.1       root      102: }
                    103: 
                    104: 
                    105: extern struct timeval time;
                    106: 
                    107: static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                    108: 
1.1.1.3   root      109: int
1.1.1.4   root      110: yeartoday(int year)
1.1       root      111: {
                    112:        return((year%4) ? 365 : 366);
                    113: }
                    114: 
1.1.1.3   root      115: int
1.1.1.4   root      116: hexdectodec(char n)
1.1       root      117: {
                    118:        return(((n>>4)&0x0F)*10 + (n&0x0F));
                    119: }
                    120: 
                    121: char
1.1.1.4   root      122: dectohexdec(int n)
1.1       root      123: {
                    124:        return((char)(((n/10)<<4)&0xF0) | ((n%10)&0x0F));
                    125: }
                    126: 
1.1.1.3   root      127: int
1.1.1.4   root      128: readtodc(u_int *tp)
1.1       root      129: {
                    130:        struct rtc_st rtclk;
                    131:        time_t n;
                    132:        int sec, min, hr, dom, mon, yr;
                    133:        int i, days = 0;
                    134:        spl_t   ospl;
                    135: 
                    136:        ospl = splclock();
                    137:        if (rtcget(&rtclk)) {
                    138:                splx(ospl);
                    139:                return(-1);
                    140:        }
                    141:        splx (ospl);
                    142: 
                    143:        sec = hexdectodec(rtclk.rtc_sec);
                    144:        min = hexdectodec(rtclk.rtc_min);
                    145:        hr = hexdectodec(rtclk.rtc_hr);
                    146:        dom = hexdectodec(rtclk.rtc_dom);
                    147:        mon = hexdectodec(rtclk.rtc_mon);
                    148:        yr = hexdectodec(rtclk.rtc_yr);
                    149:        yr = (yr < 70) ? yr+100 : yr;
                    150: 
                    151:        n = sec + 60 * min + 3600 * hr;
                    152:        n += (dom - 1) * 3600 * 24;
                    153: 
                    154:        if (yeartoday(yr) == 366)
                    155:                month[1] = 29;
                    156:        for (i = mon - 2; i >= 0; i--)
                    157:                days += month[i];
                    158:        month[1] = 28;
                    159:        for (i = 70; i < yr; i++)
                    160:                days += yeartoday(i);
                    161:        n += days * 3600 * 24;
                    162: 
                    163: 
                    164:        *tp = n;
                    165: 
                    166:        return(0);
                    167: }
                    168: 
1.1.1.3   root      169: int
1.1.1.4   root      170: writetodc(void)
1.1       root      171: {
                    172:        struct rtc_st rtclk;
                    173:        time_t n;
                    174:        int diff, i, j;
                    175:        spl_t   ospl;
                    176: 
                    177:        ospl = splclock();
                    178:        if (rtcget(&rtclk)) {
                    179:                splx(ospl);
                    180:                return(-1);
                    181:        }
                    182:        splx(ospl);
                    183: 
                    184:        diff = 0;
                    185:        n = (time.tv_sec - diff) % (3600 * 24);   /* hrs+mins+secs */
                    186:        rtclk.rtc_sec = dectohexdec(n%60);
                    187:        n /= 60;
                    188:        rtclk.rtc_min = dectohexdec(n%60);
                    189:        rtclk.rtc_hr = dectohexdec(n/60);
                    190: 
                    191:        n = (time.tv_sec - diff) / (3600 * 24); /* days */
                    192:        rtclk.rtc_dow = (n + 4) % 7;  /* 1/1/70 is Thursday */
                    193: 
                    194:        for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
                    195:                n -= i;
                    196: 
                    197:        rtclk.rtc_yr = dectohexdec(j - 1900);
                    198: 
                    199:        if (i == 366)
                    200:                month[1] = 29;
                    201:        for (i = 0; n >= month[i]; i++)
                    202:                n -= month[i];
                    203:        month[1] = 28;
                    204:        rtclk.rtc_mon = dectohexdec(++i);
                    205: 
                    206:        rtclk.rtc_dom = dectohexdec(++n);
                    207: 
                    208:        ospl = splclock();
                    209:        rtcput(&rtclk);
                    210:        splx(ospl);
                    211: 
                    212:        return(0);
                    213: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.