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

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.4 ! root       69: rtcget(unsigned char *regs)
1.1       root       70: {
                     71:        if (first_rtcopen_ever) {
                     72:                rtcinit();
1.1.1.4 ! root       73:                first_rtcopen_ever = FALSE;
1.1       root       74:        }
1.1.1.2   root       75:        outb(RTC_ADDR, RTC_D);
1.1.1.3   root       76:        if ((inb(RTC_DATA) & RTC_VRT) == 0) return(-1);
1.1.1.2   root       77:        outb(RTC_ADDR, RTC_A);
1.1       root       78:        while (inb(RTC_DATA) & RTC_UIP)         /* busy wait */
1.1.1.2   root       79:                outb(RTC_ADDR, RTC_A);
1.1       root       80:        load_rtc(regs);
                     81:        return(0);
1.1.1.2   root       82: }
1.1       root       83: 
1.1.1.3   root       84: void
1.1.1.4 ! root       85: rtcput(unsigned char *regs)
1.1       root       86: {
1.1.1.4 ! root       87:        unsigned char   x;
1.1       root       88: 
                     89:        if (first_rtcopen_ever) {
                     90:                rtcinit();
1.1.1.4 ! root       91:                first_rtcopen_ever = FALSE;
1.1       root       92:        }
                     93:        outb(RTC_ADDR, RTC_B);
                     94:        x = inb(RTC_DATA);
                     95:        outb(RTC_ADDR, RTC_B);
1.1.1.2   root       96:        outb(RTC_DATA, x | RTC_SET);
1.1       root       97:        save_rtc(regs);
                     98:        outb(RTC_ADDR, RTC_B);
1.1.1.2   root       99:        outb(RTC_DATA, x & ~RTC_SET);
1.1       root      100: }
                    101: 
                    102: 
                    103: extern struct timeval time;
                    104: 
                    105: static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                    106: 
1.1.1.3   root      107: int
1.1.1.4 ! root      108: yeartoday(int year)
1.1       root      109: {
                    110:        return((year%4) ? 365 : 366);
                    111: }
                    112: 
1.1.1.3   root      113: int
1.1.1.4 ! root      114: hexdectodec(char n)
1.1       root      115: {
                    116:        return(((n>>4)&0x0F)*10 + (n&0x0F));
                    117: }
                    118: 
                    119: char
1.1.1.4 ! root      120: dectohexdec(int n)
1.1       root      121: {
                    122:        return((char)(((n/10)<<4)&0xF0) | ((n%10)&0x0F));
                    123: }
                    124: 
1.1.1.3   root      125: int
1.1.1.4 ! root      126: readtodc(u_int *tp)
1.1       root      127: {
                    128:        struct rtc_st rtclk;
                    129:        time_t n;
                    130:        int sec, min, hr, dom, mon, yr;
                    131:        int i, days = 0;
                    132:        spl_t   ospl;
                    133: 
                    134:        ospl = splclock();
                    135:        if (rtcget(&rtclk)) {
                    136:                splx(ospl);
                    137:                return(-1);
                    138:        }
                    139:        splx (ospl);
                    140: 
                    141:        sec = hexdectodec(rtclk.rtc_sec);
                    142:        min = hexdectodec(rtclk.rtc_min);
                    143:        hr = hexdectodec(rtclk.rtc_hr);
                    144:        dom = hexdectodec(rtclk.rtc_dom);
                    145:        mon = hexdectodec(rtclk.rtc_mon);
                    146:        yr = hexdectodec(rtclk.rtc_yr);
                    147:        yr = (yr < 70) ? yr+100 : yr;
                    148: 
                    149:        n = sec + 60 * min + 3600 * hr;
                    150:        n += (dom - 1) * 3600 * 24;
                    151: 
                    152:        if (yeartoday(yr) == 366)
                    153:                month[1] = 29;
                    154:        for (i = mon - 2; i >= 0; i--)
                    155:                days += month[i];
                    156:        month[1] = 28;
                    157:        for (i = 70; i < yr; i++)
                    158:                days += yeartoday(i);
                    159:        n += days * 3600 * 24;
                    160: 
                    161: 
                    162:        *tp = n;
                    163: 
                    164:        return(0);
                    165: }
                    166: 
1.1.1.3   root      167: int
1.1.1.4 ! root      168: writetodc(void)
1.1       root      169: {
                    170:        struct rtc_st rtclk;
                    171:        time_t n;
                    172:        int diff, i, j;
                    173:        spl_t   ospl;
                    174: 
                    175:        ospl = splclock();
                    176:        if (rtcget(&rtclk)) {
                    177:                splx(ospl);
                    178:                return(-1);
                    179:        }
                    180:        splx(ospl);
                    181: 
                    182:        diff = 0;
                    183:        n = (time.tv_sec - diff) % (3600 * 24);   /* hrs+mins+secs */
                    184:        rtclk.rtc_sec = dectohexdec(n%60);
                    185:        n /= 60;
                    186:        rtclk.rtc_min = dectohexdec(n%60);
                    187:        rtclk.rtc_hr = dectohexdec(n/60);
                    188: 
                    189:        n = (time.tv_sec - diff) / (3600 * 24); /* days */
                    190:        rtclk.rtc_dow = (n + 4) % 7;  /* 1/1/70 is Thursday */
                    191: 
                    192:        for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
                    193:                n -= i;
                    194: 
                    195:        rtclk.rtc_yr = dectohexdec(j - 1900);
                    196: 
                    197:        if (i == 366)
                    198:                month[1] = 29;
                    199:        for (i = 0; n >= month[i]; i++)
                    200:                n -= month[i];
                    201:        month[1] = 28;
                    202:        rtclk.rtc_mon = dectohexdec(++i);
                    203: 
                    204:        rtclk.rtc_dom = dectohexdec(++n);
                    205: 
                    206:        ospl = splclock();
                    207:        rtcput(&rtclk);
                    208:        splx(ospl);
                    209: 
                    210:        return(0);
                    211: }

unix.superglobalmegacorp.com

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