|
|
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:
56: static int first_rtcopen_ever = 1;
57:
1.1.1.3 ! root 58: void
1.1 root 59: rtcinit()
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
69: rtcget(regs)
70: unsigned char *regs;
71: {
72: if (first_rtcopen_ever) {
73: rtcinit();
74: first_rtcopen_ever = 0;
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 root 86: rtcput(regs)
87: unsigned char *regs;
88: {
89: register unsigned char x;
90:
91: if (first_rtcopen_ever) {
92: rtcinit();
93: first_rtcopen_ever = 0;
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: extern struct timezone tz;
107:
108: static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
109:
1.1.1.3 ! root 110: int
1.1 root 111: yeartoday(year)
112: int year;
113: {
114: return((year%4) ? 365 : 366);
115: }
116:
1.1.1.3 ! root 117: int
1.1 root 118: hexdectodec(n)
119: char n;
120: {
121: return(((n>>4)&0x0F)*10 + (n&0x0F));
122: }
123:
124: char
125: dectohexdec(n)
126: int n;
127: {
128: return((char)(((n/10)<<4)&0xF0) | ((n%10)&0x0F));
129: }
130:
1.1.1.3 ! root 131: int
1.1 root 132: readtodc(tp)
133: u_int *tp;
134: {
135: struct rtc_st rtclk;
136: time_t n;
137: int sec, min, hr, dom, mon, yr;
138: int i, days = 0;
139: spl_t ospl;
140:
141: ospl = splclock();
142: if (rtcget(&rtclk)) {
143: splx(ospl);
144: return(-1);
145: }
146: splx (ospl);
147:
148: sec = hexdectodec(rtclk.rtc_sec);
149: min = hexdectodec(rtclk.rtc_min);
150: hr = hexdectodec(rtclk.rtc_hr);
151: dom = hexdectodec(rtclk.rtc_dom);
152: mon = hexdectodec(rtclk.rtc_mon);
153: yr = hexdectodec(rtclk.rtc_yr);
154: yr = (yr < 70) ? yr+100 : yr;
155:
156: n = sec + 60 * min + 3600 * hr;
157: n += (dom - 1) * 3600 * 24;
158:
159: if (yeartoday(yr) == 366)
160: month[1] = 29;
161: for (i = mon - 2; i >= 0; i--)
162: days += month[i];
163: month[1] = 28;
164: for (i = 70; i < yr; i++)
165: days += yeartoday(i);
166: n += days * 3600 * 24;
167:
168:
169: *tp = n;
170:
171: return(0);
172: }
173:
1.1.1.3 ! root 174: int
1.1 root 175: writetodc()
176: {
177: struct rtc_st rtclk;
178: time_t n;
179: int diff, i, j;
180: spl_t ospl;
181:
182: ospl = splclock();
183: if (rtcget(&rtclk)) {
184: splx(ospl);
185: return(-1);
186: }
187: splx(ospl);
188:
189: diff = 0;
190: n = (time.tv_sec - diff) % (3600 * 24); /* hrs+mins+secs */
191: rtclk.rtc_sec = dectohexdec(n%60);
192: n /= 60;
193: rtclk.rtc_min = dectohexdec(n%60);
194: rtclk.rtc_hr = dectohexdec(n/60);
195:
196: n = (time.tv_sec - diff) / (3600 * 24); /* days */
197: rtclk.rtc_dow = (n + 4) % 7; /* 1/1/70 is Thursday */
198:
199: for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
200: n -= i;
201:
202: rtclk.rtc_yr = dectohexdec(j - 1900);
203:
204: if (i == 366)
205: month[1] = 29;
206: for (i = 0; n >= month[i]; i++)
207: n -= month[i];
208: month[1] = 28;
209: rtclk.rtc_mon = dectohexdec(++i);
210:
211: rtclk.rtc_dom = dectohexdec(++n);
212:
213: ospl = splclock();
214: rtcput(&rtclk);
215: splx(ospl);
216:
217: return(0);
218: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.