|
|
1.1 root 1: /*-
2: * Copyright (c) 1990 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * This code is derived from software contributed to Berkeley by
6: * William Jolitz and Don Ahn.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: *
1.1.1.3 ! root 36: * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
! 37: * clock.c,v 1.11 1993/07/06 06:06:28 deraadt Exp
1.1 root 38: */
39:
40: /*
41: * Primitive clock interrupt routines.
42: */
43: #include "param.h"
1.1.1.2 root 44: #include "systm.h"
1.1 root 45: #include "time.h"
46: #include "kernel.h"
47: #include "machine/segments.h"
48: #include "i386/isa/icu.h"
49: #include "i386/isa/isa.h"
50: #include "i386/isa/rtc.h"
1.1.1.2 root 51: #include "i386/isa/timerreg.h"
1.1 root 52:
1.1.1.3 ! root 53: void spinwait __P((int));
! 54:
! 55: /* XXX all timezone stuff should be moved out of the kernel */
! 56: #if 1
1.1 root 57: #define DAYST 119
58: #define DAYEN 303
1.1.1.3 ! root 59: #endif
1.1 root 60:
1.1.1.3 ! root 61: void
1.1 root 62: startrtclock() {
63: int s;
64:
1.1.1.2 root 65: findcpuspeed(); /* use the clock (while it's free)
66: to find the cpu speed */
1.1 root 67: /* initialize 8253 clock */
1.1.1.2 root 68: outb(TIMER_MODE, TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT);
1.1.1.3 ! root 69:
! 70: /* Correct rounding will buy us a better precision in timekeeping */
! 71: outb (IO_TIMER1, TIMER_DIV(hz)%256);
! 72: outb (IO_TIMER1, TIMER_DIV(hz)/256);
1.1 root 73:
74: /* initialize brain-dead battery powered clock */
75: outb (IO_RTC, RTC_STATUSA);
76: outb (IO_RTC+1, 0x26);
77: outb (IO_RTC, RTC_STATUSB);
78: outb (IO_RTC+1, 2);
79:
80: outb (IO_RTC, RTC_DIAG);
81: if (s = inb (IO_RTC+1))
82: printf("RTC BIOS diagnostic error %b\n", s, RTCDG_BITS);
83: outb (IO_RTC, RTC_DIAG);
84: outb (IO_RTC+1, 0);
85: }
86:
1.1.1.2 root 87: unsigned int delaycount; /* calibrated loop variable (1 millisecond) */
88:
89: #define FIRST_GUESS 0x2000
90: findcpuspeed()
91: {
92: unsigned char low;
93: unsigned int remainder;
94:
95: /* Put counter in count down mode */
96: outb(IO_TIMER1+3, 0x34);
97: outb(IO_TIMER1, 0xff);
98: outb(IO_TIMER1, 0xff);
99: delaycount = FIRST_GUESS;
100: spinwait(1);
101: /* Read the value left in the counter */
102: low = inb(IO_TIMER1); /* least siginifcant */
103: remainder = inb(IO_TIMER1); /* most significant */
104: remainder = (remainder<<8) + low ;
105: /* Formula for delaycount is :
106: * (loopcount * timer clock speed)/ (counter ticks * 1000)
107: */
1.1.1.3 ! root 108: delaycount = (FIRST_GUESS * TIMER_DIV(1000)) / (0xffff-remainder);
1.1.1.2 root 109: }
110:
111:
1.1 root 112: /* convert 2 digit BCD number */
113: bcd(i)
114: int i;
115: {
116: return ((i/16)*10 + (i%16));
117: }
118:
119: /* convert years to seconds (from 1970) */
120: unsigned long
121: ytos(y)
122: int y;
123: {
124: int i;
125: unsigned long ret;
126:
1.1.1.2 root 127: ret = 0;
128: for(i = 1970; i < y; i++) {
1.1 root 129: if (i % 4) ret += 365*24*60*60;
130: else ret += 366*24*60*60;
131: }
132: return ret;
133: }
134:
135: /* convert months to seconds */
136: unsigned long
137: mtos(m,leap)
138: int m,leap;
139: {
140: int i;
141: unsigned long ret;
142:
143: ret = 0;
144: for(i=1;i<m;i++) {
145: switch(i){
146: case 1: case 3: case 5: case 7: case 8: case 10: case 12:
147: ret += 31*24*60*60; break;
148: case 4: case 6: case 9: case 11:
149: ret += 30*24*60*60; break;
150: case 2:
151: if (leap) ret += 29*24*60*60;
152: else ret += 28*24*60*60;
153: }
154: }
155: return ret;
156: }
157:
158:
159: /*
160: * Initialize the time of day register, based on the time base which is, e.g.
161: * from a filesystem.
162: */
163: inittodr(base)
164: time_t base;
165: {
166: unsigned long sec;
167: int leap,day_week,t,yd;
168: int sa,s;
169:
170: /* do we have a realtime clock present? (otherwise we loop below) */
171: sa = rtcin(RTC_STATUSA);
172: if (sa == 0xff || sa == 0) return;
173:
174: /* ready for a read? */
175: while ((sa&RTCSA_TUP) == RTCSA_TUP)
176: sa = rtcin(RTC_STATUSA);
177:
1.1.1.2 root 178: sec = bcd(rtcin(RTC_YEAR)) + 1900;
179: if (sec < 1970)
180: sec += 100;
181: leap = !(sec % 4); sec = ytos(sec); /* year */
1.1 root 182: yd = mtos(bcd(rtcin(RTC_MONTH)),leap); sec += yd; /* month */
183: t = (bcd(rtcin(RTC_DAY))-1) * 24*60*60; sec += t; yd += t; /* date */
184: day_week = rtcin(RTC_WDAY); /* day */
185: sec += bcd(rtcin(RTC_HRS)) * 60*60; /* hour */
186: sec += bcd(rtcin(RTC_MIN)) * 60; /* minutes */
187: sec += bcd(rtcin(RTC_SEC)); /* seconds */
188:
1.1.1.3 ! root 189: #ifdef DAYST
1.1 root 190: /* XXX off by one? Need to calculate DST on SUNDAY */
191: /* Perhaps we should have the RTC hold GMT time to save */
192: /* us the bother of converting. */
1.1.1.2 root 193: yd = yd / (24*60*60);
1.1 root 194: if ((yd >= DAYST) && ( yd <= DAYEN)) {
195: sec -= 60*60;
196: }
1.1.1.3 ! root 197: #endif
1.1 root 198: sec += tz.tz_minuteswest * 60;
199:
200: time.tv_sec = sec;
201: }
202:
203: #ifdef garbage
204: /*
205: * Initialze the time of day register, based on the time base which is, e.g.
206: * from a filesystem.
207: */
208: test_inittodr(base)
209: time_t base;
210: {
211:
212: outb(IO_RTC,9); /* year */
213: printf("%d ",bcd(inb(IO_RTC+1)));
214: outb(IO_RTC,8); /* month */
215: printf("%d ",bcd(inb(IO_RTC+1)));
216: outb(IO_RTC,7); /* day */
217: printf("%d ",bcd(inb(IO_RTC+1)));
218: outb(IO_RTC,4); /* hour */
219: printf("%d ",bcd(inb(IO_RTC+1)));
220: outb(IO_RTC,2); /* minutes */
221: printf("%d ",bcd(inb(IO_RTC+1)));
222: outb(IO_RTC,0); /* seconds */
223: printf("%d\n",bcd(inb(IO_RTC+1)));
224:
225: time.tv_sec = base;
226: }
227: #endif
228:
229: /*
230: * Restart the clock.
231: */
1.1.1.3 ! root 232: void
1.1 root 233: resettodr()
234: {
235: }
236:
237: /*
238: * Wire clock interrupt in.
239: */
1.1.1.3 ! root 240: #define VEC(s) __CONCAT(X, s)
! 241: extern VEC(clk)();
! 242: void
1.1 root 243: enablertclock() {
1.1.1.3 ! root 244: setidt(ICU_OFFSET+0, &VEC(clk), SDT_SYS386IGT, SEL_KPL);
1.1 root 245: INTREN(IRQ0);
246: }
1.1.1.2 root 247:
1.1.1.3 ! root 248: /*
! 249: * Delay for some number of milliseconds.
! 250: */
! 251: void
1.1.1.2 root 252: spinwait(millisecs)
1.1.1.3 ! root 253: int millisecs;
1.1.1.2 root 254: {
1.1.1.3 ! root 255: DELAY(1000 * millisecs);
1.1.1.2 root 256: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.