Debian Codegraph @

i386/i386at/rtc.c

raw
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
4 * All Rights Reserved.
5 *
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.
11 *
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.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or [email protected]
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
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 <kern/mach_clock.h>
51 #include <kern/printf.h>
52 #include <i386/spl.h>
53 #include <i386/pio.h>
54 #include <i386at/rtc.h>
55
56 /* time of day stored in RTC are currently between 1970 and 2070. Update that
57 * before 2070 please. */
58 #define CENTURY_START 1970
59
60 static boolean_t first_rtcopen_ever = TRUE;
61
62 static void
63 rtcinit(void)
64 {
65 outb(RTC_ADDR, RTC_A);
66 outb(RTC_DATA, RTC_DIV2 | RTC_RATE6);
67 outb(RTC_ADDR, RTC_B);
68 outb(RTC_DATA, RTC_HM);
69 }
70
71
72 static int
73 rtcget(struct rtc_st *st)
74 {
75 unsigned char *regs = (unsigned char *)st;
76 if (first_rtcopen_ever) {
77 rtcinit();
78 first_rtcopen_ever = FALSE;
79 }
80 outb(RTC_ADDR, RTC_D);
81 if ((inb(RTC_DATA) & RTC_VRT) == 0) return(-1);
82 outb(RTC_ADDR, RTC_A);
83 while (inb(RTC_DATA) & RTC_UIP) /* busy wait */
84 outb(RTC_ADDR, RTC_A);
85 load_rtc(regs);
86 return(0);
87 }
88
89 static void
90 rtcput(struct rtc_st *st)
91 {
92 unsigned char *regs = (unsigned char *)st;
93 unsigned char x;
94
95 if (first_rtcopen_ever) {
96 rtcinit();
97 first_rtcopen_ever = FALSE;
98 }
99 outb(RTC_ADDR, RTC_B);
100 x = inb(RTC_DATA);
101 outb(RTC_ADDR, RTC_B);
102 outb(RTC_DATA, x | RTC_SET);
103 save_rtc(regs);
104 outb(RTC_ADDR, RTC_B);
105 outb(RTC_DATA, x & ~RTC_SET);
106 }
107
108
109 static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
110
111 static int
112 yeartoday(int year)
113 {
114 if (year%4)
115 /* Not divisible by 4, not bissextile */
116 return 365;
117
118 /* Divisible by 4 */
119 if (year % 100)
120 /* Not divisible by 100, bissextile */
121 return 366;
122
123 /* Divisible by 100 */
124 if (year % 400)
125 /* Not divisible by 400, not bissextile */
126 return 365;
127
128 /* Divisible by 400 */
129 /* Rules for 2000 and further have not been officially decided yet.
130 * 2000 was made bissextile. */
131 return 366;
132 }
133
134 static int
135 hexdectodec(char n)
136 {
137 return(((n>>4)&0x0F)*10 + (n&0x0F));
138 }
139
140 static char
141 dectohexdec(int n)
142 {
143 return((char)(((n/10)<<4)&0xF0) | ((n%10)&0x0F));
144 }
145
146 int
147 readtodc(uint64_t *tp)
148 {
149 struct rtc_st rtclk;
150 time_t n;
151 int sec, min, hr, dom, mon, yr;
152 int i, days = 0;
153 spl_t ospl;
154
155 ospl = splclock();
156 if (rtcget(&rtclk)) {
157 splx(ospl);
158 return(-1);
159 }
160 splx (ospl);
161
162 sec = hexdectodec(rtclk.rtc_sec);
163 min = hexdectodec(rtclk.rtc_min);
164 hr = hexdectodec(rtclk.rtc_hr);
165 dom = hexdectodec(rtclk.rtc_dom);
166 mon = hexdectodec(rtclk.rtc_mon);
167 yr = hexdectodec(rtclk.rtc_yr);
168 yr = (yr < CENTURY_START%100) ?
169 yr+CENTURY_START-CENTURY_START%100+100 :
170 yr+CENTURY_START-CENTURY_START%100;
171
172 if (yr >= CENTURY_START+90) {
173 printf("FIXME: we are approaching %u, update CENTURY_START\n", CENTURY_START);
174 }
175
176 printf("RTC time is %04u-%02u-%02u %02u:%02u:%02u\n", yr, mon, dom, hr, min, sec);
177
178 n = sec + 60 * min + 3600 * hr;
179 n += (dom - 1) * 3600 * 24;
180
181 if (yeartoday(yr) == 366)
182 month[1] = 29;
183 for (i = mon - 2; i >= 0; i--)
184 days += month[i];
185 month[1] = 28;
186 /* Epoch shall be 1970 January 1st */
187 for (i = 1970; i < yr; i++)
188 days += yeartoday(i);
189 n += days * 3600 * 24;
190
191
192 *tp = n;
193
194 return(0);
195 }
196
197 int
198 writetodc(void)
199 {
200 struct rtc_st rtclk;
201 time_t n;
202 int diff, i, j;
203 spl_t ospl;
204
205 ospl = splclock();
206 if (rtcget(&rtclk)) {
207 splx(ospl);
208 return(-1);
209 }
210 splx(ospl);
211
212 diff = 0;
213 n = (time.seconds - diff) % (3600 * 24); /* hrs+mins+secs */
214 rtclk.rtc_sec = dectohexdec(n%60);
215 n /= 60;
216 rtclk.rtc_min = dectohexdec(n%60);
217 rtclk.rtc_hr = dectohexdec(n/60);
218
219 n = (time.seconds - diff) / (3600 * 24); /* days */
220 rtclk.rtc_dow = (n + 4) % 7; /* 1/1/70 is Thursday */
221
222 /* Epoch shall be 1970 January 1st */
223 for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
224 n -= i;
225
226 rtclk.rtc_yr = dectohexdec(j % 100);
227
228 if (i == 366)
229 month[1] = 29;
230 for (i = 0; n >= month[i]; i++)
231 n -= month[i];
232 month[1] = 28;
233 rtclk.rtc_mon = dectohexdec(++i);
234
235 rtclk.rtc_dom = dectohexdec(++n);
236
237 ospl = splclock();
238 rtcput(&rtclk);
239 splx(ospl);
240
241 return(0);
242 }