fix resched clock bug, tod, reliably read rtc
[unix-history] / usr / src / sys / i386 / isa / clock.c
CommitLineData
f21663b5
WN
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.
7 *
cc60f649
DA
8 * Added stuff to read the cmos clock on startup - Don Ahn
9 *
f21663b5
WN
10 * %sccs.include.386.c%
11 *
5b97e489 12 * @(#)clock.c 5.3 (Berkeley) %G%
f21663b5
WN
13 */
14
15/*
16 * Primitive clock interrupt routines.
17 */
18#include "param.h"
19#include "time.h"
20#include "kernel.h"
21#include "icu.h"
5b97e489 22#include "isa.h"
f21663b5 23
cc60f649
DA
24#define DAYST 119
25#define DAYEN 303
26
f21663b5
WN
27startrtclock() {
28
29 /* initialize 8253 clock */
5b97e489
BJ
30 outb (IO_TIMER0+3, 0x36);
31 outb (IO_TIMER0, 1193182/hz);
32 outb (IO_TIMER0, (1193182/hz)/256);
f21663b5
WN
33}
34
cc60f649
DA
35/* convert 2 digit BCD number */
36bcd(i)
37int i;
38{
39 return ((i/16)*10 + (i%16));
40}
41
42/* convert years to seconds (from 1970) */
43unsigned long
44ytos(y)
45int y;
46{
47 int i;
48 unsigned long ret;
49
50 ret = 0; y = y - 70;
51 for(i=0;i<y;i++) {
52 if (i % 4) ret += 31536000;
53 else ret += 31622400;
54 }
55 return ret;
56}
57
58/* convert months to seconds */
59unsigned long
60mtos(m,leap)
61int m,leap;
62{
63 int i;
64 unsigned long ret;
65
66 ret = 0;
67 for(i=1;i<m;i++) {
68 switch(i){
69 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
70 ret += 2678400; break;
71 case 4: case 6: case 9: case 11:
72 ret += 2592000; break;
73 case 2:
74 if (leap) ret += 2505600;
75 else ret += 2419200;
76 }
77 }
78 return ret;
79}
80
81
f21663b5 82/*
5b97e489 83 * Initialize the time of day register, based on the time base which is, e.g.
f21663b5
WN
84 * from a filesystem.
85 */
86inittodr(base)
87 time_t base;
88{
cc60f649
DA
89 unsigned long sec;
90 int leap,day_week,t,yd;
91
5b97e489
BJ
92 sec = bcd(rtcin(9)); leap = !(sec % 4); sec += ytos(sec); /* year */
93 yd = mtos(bcd(rtcin(8)),leap); sec += yd; /* month */
94 t = (bcd(rtcin(7))-1) * 86400; sec += t; yd += t; /* date */
95 day_week = rtcin(6); /* day */
96 sec += bcd(rtcin(4)) * 3600; /* hour */
97 sec += bcd(rtcin(2)) * 60; /* minutes */
98 sec += bcd(rtcin(0)); /* seconds */
cc60f649
DA
99
100 /* XXX off by one? Need to calculate DST on SUNDAY */
101 /* Perhaps we should have the RTC hold GMT time to save */
102 /* us the bother of converting. */
103 yd = yd / 86400;
104 if ((yd >= DAYST) && ( yd <= DAYEN)) {
105 sec -= 3600;
106 }
107 sec += tz.tz_minuteswest * 60;
108
109 time.tv_sec = sec;
110}
111
5b97e489 112#ifdef garbage
cc60f649
DA
113/*
114 * Initialze the time of day register, based on the time base which is, e.g.
115 * from a filesystem.
116 */
117test_inittodr(base)
118 time_t base;
119{
120
5b97e489
BJ
121 outb(IO_RTC,9); /* year */
122 printf("%d ",bcd(inb(IO_RTC+1)));
123 outb(IO_RTC,8); /* month */
124 printf("%d ",bcd(inb(IO_RTC+1)));
125 outb(IO_RTC,7); /* day */
126 printf("%d ",bcd(inb(IO_RTC+1)));
127 outb(IO_RTC,4); /* hour */
128 printf("%d ",bcd(inb(IO_RTC+1)));
129 outb(IO_RTC,2); /* minutes */
130 printf("%d ",bcd(inb(IO_RTC+1)));
131 outb(IO_RTC,0); /* seconds */
132 printf("%d\n",bcd(inb(IO_RTC+1)));
f21663b5
WN
133
134 time.tv_sec = base;
135}
5b97e489 136#endif
f21663b5 137
5b97e489
BJ
138/*
139 * retreve a value from realtime clock
140 */
141u_char rtcin(n) {
142 u_char val;
143
144 outb(IO_RTC,n);
145 do val = inb(IO_RTC+1) ; while (val != inb(IO_RTC+1));
146 return (val);
147}
cc60f649 148
f21663b5
WN
149/*
150 * Restart the clock.
151 */
152resettodr()
153{
154}
155
156enablertclock() {
157 INTREN(IRQ0);
158 splnone();
159}