clock.c revision 1.7 1 1.7 gwr /* $NetBSD: clock.c,v 1.7 1997/02/12 16:00:31 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1994 Gordon W. Ross
5 1.1 gwr * Copyright (c) 1993 Adam Glass
6 1.1 gwr * Copyright (c) 1988 University of Utah.
7 1.1 gwr * Copyright (c) 1982, 1990, 1993
8 1.1 gwr * The Regents of the University of California. All rights reserved.
9 1.1 gwr *
10 1.1 gwr * This code is derived from software contributed to Berkeley by
11 1.1 gwr * the Systems Programming Group of the University of Utah Computer
12 1.1 gwr * Science Department.
13 1.1 gwr *
14 1.1 gwr * Redistribution and use in source and binary forms, with or without
15 1.1 gwr * modification, are permitted provided that the following conditions
16 1.1 gwr * are met:
17 1.1 gwr * 1. Redistributions of source code must retain the above copyright
18 1.1 gwr * notice, this list of conditions and the following disclaimer.
19 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 gwr * notice, this list of conditions and the following disclaimer in the
21 1.1 gwr * documentation and/or other materials provided with the distribution.
22 1.1 gwr * 3. All advertising materials mentioning features or use of this software
23 1.1 gwr * must display the following acknowledgement:
24 1.1 gwr * This product includes software developed by the University of
25 1.1 gwr * California, Berkeley and its contributors.
26 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
27 1.1 gwr * may be used to endorse or promote products derived from this software
28 1.1 gwr * without specific prior written permission.
29 1.1 gwr *
30 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 gwr * SUCH DAMAGE.
41 1.1 gwr *
42 1.1 gwr * from: Utah Hdr: clock.c 1.18 91/01/21$
43 1.1 gwr * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
44 1.1 gwr */
45 1.1 gwr
46 1.1 gwr /*
47 1.3 gwr * Machine-dependent clock routines for the Mostek48t02
48 1.1 gwr */
49 1.1 gwr
50 1.1 gwr #include <sys/param.h>
51 1.1 gwr #include <sys/systm.h>
52 1.1 gwr #include <sys/time.h>
53 1.1 gwr #include <sys/kernel.h>
54 1.1 gwr #include <sys/device.h>
55 1.1 gwr
56 1.1 gwr #include <machine/autoconf.h>
57 1.1 gwr #include <machine/cpu.h>
58 1.1 gwr #include <machine/mon.h>
59 1.1 gwr #include <machine/obio.h>
60 1.3 gwr #include <machine/machdep.h>
61 1.1 gwr
62 1.3 gwr #include <sun3/sun3/interreg.h>
63 1.3 gwr #include "mostek48t02.h"
64 1.1 gwr
65 1.1 gwr #define CLOCK_PRI 5
66 1.1 gwr
67 1.1 gwr void _isr_clock __P((void)); /* in locore.s */
68 1.1 gwr void clock_intr __P((struct clockframe));
69 1.1 gwr
70 1.1 gwr /* Note: this is used by locore.s:__isr_clock */
71 1.3 gwr static volatile void *clock_va;
72 1.1 gwr
73 1.1 gwr static int clock_match __P((struct device *, struct cfdata *, void *args));
74 1.1 gwr static void clock_attach __P((struct device *, struct device *, void *));
75 1.1 gwr
76 1.1 gwr struct cfattach clock_ca = {
77 1.1 gwr sizeof(struct device), clock_match, clock_attach
78 1.1 gwr };
79 1.1 gwr
80 1.1 gwr struct cfdriver clock_cd = {
81 1.1 gwr NULL, "clock", DV_DULL
82 1.1 gwr };
83 1.1 gwr
84 1.1 gwr /*
85 1.7 gwr * XXX Need to determine which type of clock we have!
86 1.7 gwr * XXX The Sun3/80 always has the MK4802, while the
87 1.7 gwr * XXX Sun3/470 can (reportedly) have that or the old
88 1.7 gwr * XXX intersil7170. Should have two clock drivers...
89 1.1 gwr */
90 1.1 gwr static int
91 1.1 gwr clock_match(parent, cf, args)
92 1.1 gwr struct device *parent;
93 1.1 gwr struct cfdata *cf;
94 1.1 gwr void *args;
95 1.1 gwr {
96 1.1 gwr struct confargs *ca = args;
97 1.1 gwr
98 1.1 gwr /* This driver only supports one unit. */
99 1.1 gwr if (cf->cf_unit != 0)
100 1.1 gwr return (0);
101 1.1 gwr
102 1.1 gwr /* Validate the given address. */
103 1.1 gwr if (ca->ca_paddr != OBIO_CLOCK2)
104 1.1 gwr return (0);
105 1.1 gwr
106 1.1 gwr /* Default interrupt priority. */
107 1.1 gwr if (ca->ca_intpri == -1)
108 1.1 gwr ca->ca_intpri = CLOCK_PRI;
109 1.1 gwr
110 1.1 gwr return (1);
111 1.1 gwr }
112 1.1 gwr
113 1.1 gwr static void
114 1.1 gwr clock_attach(parent, self, args)
115 1.1 gwr struct device *parent;
116 1.1 gwr struct device *self;
117 1.1 gwr void *args;
118 1.1 gwr {
119 1.1 gwr
120 1.1 gwr printf("\n");
121 1.1 gwr
122 1.1 gwr /*
123 1.1 gwr * Can not hook up the ISR until cpu_initclocks()
124 1.1 gwr * because hardclock is not ready until then.
125 1.1 gwr * For now, the handler is _isr_autovec(), which
126 1.1 gwr * will complain if it gets clock interrupts.
127 1.1 gwr */
128 1.1 gwr }
129 1.1 gwr
130 1.1 gwr /*
131 1.1 gwr * Set and/or clear the desired clock bits in the interrupt
132 1.1 gwr * register. We have to be extremely careful that we do it
133 1.1 gwr * in such a manner that we don't get ourselves lost.
134 1.1 gwr */
135 1.1 gwr void
136 1.1 gwr set_clk_mode(on, off, enable)
137 1.1 gwr u_char on, off;
138 1.1 gwr int enable;
139 1.1 gwr {
140 1.1 gwr register u_char interreg;
141 1.1 gwr register int s;
142 1.1 gwr
143 1.4 gwr /* If we don't have this, we must not have touched it! */
144 1.5 gwr if (!interrupt_reg)
145 1.4 gwr return;
146 1.4 gwr
147 1.1 gwr s = getsr();
148 1.1 gwr if ((s & PSL_IPL) < PSL_IPL7)
149 1.1 gwr panic("set_clk_mode: ipl");
150 1.1 gwr
151 1.1 gwr /*
152 1.1 gwr * make sure that we are only playing w/
153 1.1 gwr * clock interrupt register bits
154 1.1 gwr */
155 1.1 gwr on &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
156 1.1 gwr off &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
157 1.1 gwr
158 1.1 gwr /*
159 1.1 gwr * Get a copy of current interrupt register,
160 1.1 gwr * turning off any undesired bits (aka `off')
161 1.1 gwr */
162 1.1 gwr interreg = *interrupt_reg & ~(off | IREG_ALL_ENAB);
163 1.1 gwr *interrupt_reg &= ~IREG_ALL_ENAB;
164 1.1 gwr
165 1.1 gwr /*
166 1.1 gwr * Next we turns off the CLK5 and CLK7 bits to clear
167 1.1 gwr * the flip-flops, then we disable clock interrupts.
168 1.1 gwr * Now we can read the clock's interrupt register
169 1.1 gwr * to clear any pending signals there.
170 1.1 gwr */
171 1.1 gwr *interrupt_reg &= ~(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
172 1.3 gwr
173 1.3 gwr /* XXX - hit the clock? */
174 1.1 gwr
175 1.1 gwr /*
176 1.1 gwr * Now we set all the desired bits
177 1.1 gwr * in the interrupt register, then
178 1.1 gwr * we turn the clock back on and
179 1.1 gwr * finally we can enable all interrupts.
180 1.1 gwr */
181 1.1 gwr *interrupt_reg |= (interreg | on); /* enable flip-flops */
182 1.1 gwr
183 1.3 gwr /* XXX - hit the clock? */
184 1.1 gwr
185 1.1 gwr *interrupt_reg |= IREG_ALL_ENAB; /* enable interrupts */
186 1.1 gwr }
187 1.1 gwr
188 1.1 gwr /* Called very early by internal_configure. */
189 1.1 gwr void clock_init()
190 1.1 gwr {
191 1.3 gwr /* XXX - Yes, use the EEPROM address. Same H/W device. */
192 1.3 gwr clock_va = obio_find_mapping(OBIO_EEPROM, sizeof(struct clockreg));
193 1.1 gwr
194 1.4 gwr if (!clock_va || !interrupt_reg) {
195 1.4 gwr mon_printf("clock_init\n");
196 1.4 gwr sunmon_abort();
197 1.4 gwr }
198 1.1 gwr
199 1.1 gwr /* Turn off clock interrupts until cpu_initclocks() */
200 1.6 gwr /* intreg_init() already cleared the interrupt register. */
201 1.1 gwr }
202 1.1 gwr
203 1.1 gwr /*
204 1.1 gwr * Set up the real-time clock (enable clock interrupts).
205 1.1 gwr * Leave stathz 0 since there is no secondary clock available.
206 1.1 gwr * Note that clock interrupts MUST STAY DISABLED until here.
207 1.1 gwr */
208 1.1 gwr void
209 1.1 gwr cpu_initclocks(void)
210 1.1 gwr {
211 1.1 gwr int s;
212 1.1 gwr
213 1.3 gwr if (!clock_va)
214 1.1 gwr panic("cpu_initclocks");
215 1.1 gwr s = splhigh();
216 1.1 gwr
217 1.1 gwr /* Install isr (in locore.s) that calls clock_intr(). */
218 1.1 gwr isr_add_custom(5, (void*)_isr_clock);
219 1.1 gwr
220 1.1 gwr /* Set the clock to interrupt 100 time per second. */
221 1.3 gwr /* XXX - Hard wired? */
222 1.1 gwr
223 1.1 gwr *interrupt_reg |= IREG_CLOCK_ENAB_5; /* enable clock */
224 1.3 gwr
225 1.3 gwr /* XXX enable the clock? */
226 1.3 gwr
227 1.1 gwr *interrupt_reg |= IREG_ALL_ENAB; /* enable interrupts */
228 1.1 gwr splx(s);
229 1.1 gwr }
230 1.1 gwr
231 1.1 gwr /*
232 1.1 gwr * This doesn't need to do anything, as we have only one timer and
233 1.1 gwr * profhz==stathz==hz.
234 1.1 gwr */
235 1.1 gwr void
236 1.1 gwr setstatclockrate(newhz)
237 1.1 gwr int newhz;
238 1.1 gwr {
239 1.1 gwr /* nothing */
240 1.1 gwr }
241 1.1 gwr
242 1.1 gwr /*
243 1.3 gwr * This is is called by the "custom" interrupt handler.
244 1.1 gwr */
245 1.1 gwr void
246 1.1 gwr clock_intr(cf)
247 1.1 gwr struct clockframe cf;
248 1.1 gwr {
249 1.3 gwr /* volatile struct clockreg *clk = clock_va; */
250 1.1 gwr
251 1.3 gwr #if 1 /* XXX - Needed? */
252 1.1 gwr /* Pulse the clock intr. enable low. */
253 1.1 gwr *interrupt_reg &= ~IREG_CLOCK_ENAB_5;
254 1.1 gwr *interrupt_reg |= IREG_CLOCK_ENAB_5;
255 1.3 gwr #endif
256 1.1 gwr
257 1.1 gwr hardclock(&cf);
258 1.1 gwr }
259 1.1 gwr
260 1.1 gwr /*
261 1.1 gwr * Return the best possible estimate of the time in the timeval
262 1.1 gwr * to which tvp points. We do this by returning the current time
263 1.1 gwr * plus the amount of time since the last clock interrupt.
264 1.1 gwr *
265 1.1 gwr * Check that this time is no less than any previously-reported time,
266 1.1 gwr * which could happen around the time of a clock adjustment. Just for
267 1.1 gwr * fun, we guarantee that the time will be greater than the value
268 1.1 gwr * obtained by a previous call.
269 1.1 gwr */
270 1.1 gwr void
271 1.1 gwr microtime(tvp)
272 1.1 gwr register struct timeval *tvp;
273 1.1 gwr {
274 1.1 gwr int s = splhigh();
275 1.1 gwr static struct timeval lasttime;
276 1.1 gwr
277 1.1 gwr *tvp = time;
278 1.1 gwr tvp->tv_usec++; /* XXX */
279 1.1 gwr while (tvp->tv_usec > 1000000) {
280 1.1 gwr tvp->tv_sec++;
281 1.1 gwr tvp->tv_usec -= 1000000;
282 1.1 gwr }
283 1.1 gwr if (tvp->tv_sec == lasttime.tv_sec &&
284 1.1 gwr tvp->tv_usec <= lasttime.tv_usec &&
285 1.1 gwr (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000)
286 1.1 gwr {
287 1.1 gwr tvp->tv_sec++;
288 1.1 gwr tvp->tv_usec -= 1000000;
289 1.1 gwr }
290 1.1 gwr lasttime = *tvp;
291 1.1 gwr splx(s);
292 1.1 gwr }
293 1.1 gwr
294 1.1 gwr
295 1.1 gwr /*
296 1.1 gwr * Machine-dependent clock routines.
297 1.1 gwr *
298 1.1 gwr * Inittodr initializes the time of day hardware which provides
299 1.1 gwr * date functions.
300 1.1 gwr *
301 1.1 gwr * Resettodr restores the time of day hardware after a time change.
302 1.1 gwr */
303 1.1 gwr #define SECDAY 86400L
304 1.1 gwr #define SECYR (SECDAY * 365)
305 1.1 gwr
306 1.1 gwr static long clk_get_secs(void);
307 1.1 gwr static void clk_set_secs(long);
308 1.1 gwr
309 1.1 gwr /*
310 1.1 gwr * Initialize the time of day register, based on the time base
311 1.1 gwr * which is, e.g. from a filesystem.
312 1.1 gwr */
313 1.1 gwr void inittodr(fs_time)
314 1.1 gwr time_t fs_time;
315 1.1 gwr {
316 1.1 gwr long diff, clk_time;
317 1.1 gwr long long_ago = (5 * SECYR);
318 1.1 gwr int clk_bad = 0;
319 1.1 gwr
320 1.1 gwr /*
321 1.1 gwr * Sanity check time from file system.
322 1.1 gwr * If it is zero,assume filesystem time is just unknown
323 1.1 gwr * instead of preposterous. Don't bark.
324 1.1 gwr */
325 1.1 gwr if (fs_time < long_ago) {
326 1.1 gwr /*
327 1.1 gwr * If fs_time is zero, assume filesystem time is just
328 1.1 gwr * unknown instead of preposterous. Don't bark.
329 1.1 gwr */
330 1.1 gwr if (fs_time != 0)
331 1.1 gwr printf("WARNING: preposterous time in file system\n");
332 1.1 gwr /* 1991/07/01 12:00:00 */
333 1.1 gwr fs_time = 21*SECYR + 186*SECDAY + SECDAY/2;
334 1.1 gwr }
335 1.1 gwr
336 1.1 gwr clk_time = clk_get_secs();
337 1.1 gwr
338 1.1 gwr /* Sanity check time from clock. */
339 1.1 gwr if (clk_time < long_ago) {
340 1.1 gwr printf("WARNING: bad date in battery clock");
341 1.1 gwr clk_bad = 1;
342 1.1 gwr clk_time = fs_time;
343 1.1 gwr } else {
344 1.1 gwr /* Does the clock time jive with the file system? */
345 1.1 gwr diff = clk_time - fs_time;
346 1.1 gwr if (diff < 0)
347 1.1 gwr diff = -diff;
348 1.1 gwr if (diff >= (SECDAY*2)) {
349 1.1 gwr printf("WARNING: clock %s %d days",
350 1.1 gwr (clk_time < fs_time) ? "lost" : "gained",
351 1.1 gwr (int) (diff / SECDAY));
352 1.1 gwr clk_bad = 1;
353 1.1 gwr }
354 1.1 gwr }
355 1.1 gwr if (clk_bad)
356 1.1 gwr printf(" -- CHECK AND RESET THE DATE!\n");
357 1.1 gwr time.tv_sec = clk_time;
358 1.1 gwr }
359 1.1 gwr
360 1.1 gwr /*
361 1.1 gwr * Resettodr restores the time of day hardware after a time change.
362 1.1 gwr */
363 1.1 gwr void resettodr()
364 1.1 gwr {
365 1.1 gwr clk_set_secs(time.tv_sec);
366 1.1 gwr }
367 1.1 gwr
368 1.3 gwr
369 1.3 gwr /*
371 1.3 gwr * XXX - Todo: take one of the implementations of
372 1.3 gwr * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
373 1.3 gwr * and put that in libkern (or somewhere).
374 1.3 gwr * Also put this stuct in some header...
375 1.3 gwr */
376 1.3 gwr struct date_time {
377 1.3 gwr u_char dt_year; /* since POSIX_BASE_YEAR (1970) */
378 1.3 gwr u_char dt_mon;
379 1.3 gwr u_char dt_day;
380 1.3 gwr u_char dt_hour;
381 1.3 gwr u_char dt_min;
382 1.3 gwr u_char dt_sec;
383 1.3 gwr u_char dt_csec; /* hundredths of a second */
384 1.3 gwr u_char dt_wday; /* Day of week (needed?) */
385 1.3 gwr };
386 1.3 gwr void gmt_to_dt __P((long gmt, struct date_time *dt));
387 1.3 gwr long dt_to_gmt __P((struct date_time *dt));
388 1.3 gwr /* Traditional UNIX base year */
389 1.1 gwr #define POSIX_BASE_YEAR 1970
390 1.3 gwr /*
391 1.1 gwr * XXX - End of stuff that should move to a header.
392 1.1 gwr */
393 1.1 gwr
394 1.1 gwr
395 1.3 gwr /*
396 1.3 gwr * Routines to copy state into and out of the clock.
397 1.1 gwr * The clock CSR has to be set for read or write.
398 1.3 gwr */
399 1.3 gwr static void
400 1.1 gwr clk_get_dt(struct date_time *dt)
401 1.3 gwr {
402 1.1 gwr volatile struct clockreg *cl = clock_va;
403 1.1 gwr int s;
404 1.1 gwr
405 1.7 gwr s = splhigh();
406 1.7 gwr
407 1.7 gwr /* XXX - Wait for the end of this second? */
408 1.7 gwr dt->dt_csec = 0;
409 1.3 gwr
410 1.3 gwr /* enable read (stop time) */
411 1.1 gwr cl->cl_csr |= CLK_READ;
412 1.3 gwr
413 1.3 gwr /* Copy the info */
414 1.3 gwr dt->dt_sec = cl->cl_sec;
415 1.3 gwr dt->dt_min = cl->cl_min;
416 1.3 gwr dt->dt_hour = cl->cl_hour;
417 1.3 gwr dt->dt_wday = cl->cl_wday;
418 1.3 gwr dt->dt_day = cl->cl_mday;
419 1.3 gwr dt->dt_mon = cl->cl_month;
420 1.1 gwr dt->dt_year = cl->cl_year;
421 1.3 gwr
422 1.3 gwr /* Done reading (time wears on) */
423 1.1 gwr cl->cl_csr &= ~CLK_READ;
424 1.1 gwr splx(s);
425 1.1 gwr }
426 1.3 gwr
427 1.3 gwr static void
428 1.1 gwr clk_set_dt(struct date_time *dt)
429 1.3 gwr {
430 1.1 gwr volatile struct clockreg *cl = clock_va;
431 1.1 gwr int s;
432 1.1 gwr
433 1.3 gwr s = splhigh();
434 1.3 gwr /* enable write */
435 1.1 gwr cl->cl_csr |= CLK_WRITE;
436 1.3 gwr
437 1.3 gwr /* Copy the info */
438 1.3 gwr cl->cl_sec = dt->dt_sec;
439 1.3 gwr cl->cl_min = dt->dt_min;
440 1.3 gwr cl->cl_hour = dt->dt_hour;
441 1.3 gwr cl->cl_wday = dt->dt_wday;
442 1.3 gwr cl->cl_mday = dt->dt_day;
443 1.3 gwr cl->cl_month = dt->dt_mon;
444 1.1 gwr cl->cl_year = dt->dt_year;
445 1.3 gwr
446 1.3 gwr /* load them up */
447 1.1 gwr cl->cl_csr &= ~CLK_WRITE;
448 1.1 gwr splx(s);
449 1.1 gwr }
450 1.1 gwr
451 1.3 gwr
452 1.7 gwr /*
453 1.7 gwr * BCD to decimal and decimal to BCD.
454 1.7 gwr */
455 1.7 gwr #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
456 1.7 gwr #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
457 1.7 gwr
458 1.3 gwr /*
459 1.3 gwr * Now routines to get and set clock as POSIX time.
460 1.3 gwr * Our clock keeps "years since 1/1/1968", so we must
461 1.3 gwr * convert to/from "years since 1/1/1970" before the
462 1.3 gwr * common time conversion functions are used.
463 1.3 gwr */
464 1.3 gwr #define CLOCK_YEAR_ADJUST (POSIX_BASE_YEAR - 1968)
465 1.3 gwr static long
466 1.3 gwr clk_get_secs()
467 1.3 gwr {
468 1.3 gwr struct date_time dt;
469 1.3 gwr long gmt;
470 1.3 gwr
471 1.7 gwr clk_get_dt(&dt);
472 1.7 gwr
473 1.7 gwr /* Convert BCD values to binary. */
474 1.7 gwr dt.dt_sec = FROMBCD(dt.dt_sec);
475 1.7 gwr dt.dt_min = FROMBCD(dt.dt_min);
476 1.7 gwr dt.dt_hour = FROMBCD(dt.dt_hour);
477 1.7 gwr dt.dt_day = FROMBCD(dt.dt_day);
478 1.7 gwr dt.dt_mon = FROMBCD(dt.dt_mon);
479 1.7 gwr dt.dt_year = FROMBCD(dt.dt_year);
480 1.3 gwr
481 1.3 gwr dt.dt_year -= CLOCK_YEAR_ADJUST;
482 1.3 gwr gmt = dt_to_gmt(&dt);
483 1.3 gwr return (gmt);
484 1.7 gwr }
485 1.3 gwr
486 1.3 gwr static void
487 1.3 gwr clk_set_secs(secs)
488 1.3 gwr long secs;
489 1.3 gwr {
490 1.3 gwr struct date_time dt;
491 1.3 gwr long gmt;
492 1.3 gwr
493 1.3 gwr gmt = secs;
494 1.3 gwr gmt_to_dt(gmt, &dt);
495 1.7 gwr dt.dt_year += CLOCK_YEAR_ADJUST;
496 1.7 gwr
497 1.7 gwr /* Convert binary values to BCD. */
498 1.7 gwr dt.dt_sec = TOBCD(dt.dt_sec);
499 1.7 gwr dt.dt_min = TOBCD(dt.dt_min);
500 1.7 gwr dt.dt_hour = TOBCD(dt.dt_hour);
501 1.7 gwr dt.dt_day = TOBCD(dt.dt_day);
502 1.7 gwr dt.dt_mon = TOBCD(dt.dt_mon);
503 1.7 gwr dt.dt_year = TOBCD(dt.dt_year);
504 1.3 gwr
505 1.3 gwr clk_set_dt(&dt);
506 1.3 gwr }
507 1.3 gwr
508 1.1 gwr
509 1.3 gwr
510 1.3 gwr /*****************************************************************
512 1.1 gwr *
513 1.1 gwr * Generic routines to convert to or from a POSIX date
514 1.1 gwr * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
515 1.1 gwr *
516 1.1 gwr * These are organized this way mostly to so the code
517 1.3 gwr * can easily be tested in an independent user program.
518 1.3 gwr * (These are derived from the hp300 code.)
519 1.1 gwr *
520 1.3 gwr * XXX - Should move these to libkern or somewhere...
521 1.1 gwr */
522 1.1 gwr static inline int leapyear __P((int year));
523 1.1 gwr #define FEBRUARY 2
524 1.1 gwr #define days_in_year(a) (leapyear(a) ? 366 : 365)
525 1.3 gwr #define days_in_month(a) (month_days[(a) - 1])
526 1.3 gwr
527 1.3 gwr /*
528 1.3 gwr * Note: This array may be modified by gmt_to_dt(),
529 1.3 gwr * but these functions DO NOT need to be reentrant.
530 1.3 gwr * If we ever DO need reentrance, we should just make
531 1.4 gwr * gmt_to_dt() copy this to a local before use. -gwr
532 1.1 gwr */
533 1.1 gwr static int month_days[12] = {
534 1.1 gwr 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
535 1.3 gwr };
536 1.3 gwr
537 1.3 gwr /* Use an inline to make the logic more obvious. */
538 1.3 gwr static inline int
539 1.3 gwr leapyear(year)
540 1.3 gwr int year;
541 1.3 gwr {
542 1.3 gwr int rv = 0;
543 1.3 gwr
544 1.3 gwr if ((year % 4) == 0) {
545 1.3 gwr rv = 1;
546 1.3 gwr if ((year % 100) == 0) {
547 1.3 gwr rv = 0;
548 1.3 gwr if ((year % 400) == 0)
549 1.3 gwr rv = 1;
550 1.3 gwr }
551 1.3 gwr }
552 1.3 gwr return rv;
553 1.3 gwr }
554 1.1 gwr
555 1.3 gwr void gmt_to_dt(long gmt, struct date_time *dt)
556 1.3 gwr {
557 1.1 gwr long secs;
558 1.3 gwr int i, days;
559 1.3 gwr
560 1.1 gwr days = gmt / SECDAY;
561 1.1 gwr secs = gmt % SECDAY;
562 1.1 gwr
563 1.1 gwr /* Hours, minutes, seconds are easy */
564 1.1 gwr dt->dt_hour = secs / 3600;
565 1.1 gwr secs = secs % 3600;
566 1.1 gwr dt->dt_min = secs / 60;
567 1.1 gwr secs = secs % 60;
568 1.1 gwr dt->dt_sec = secs;
569 1.3 gwr
570 1.1 gwr /* Day of week (Note: 1/1/1970 was a Thursday) */
571 1.3 gwr dt->dt_wday = (days + 4) % 7;
572 1.1 gwr
573 1.1 gwr /* Subtract out whole years... */
574 1.1 gwr i = POSIX_BASE_YEAR;
575 1.1 gwr while (days >= days_in_year(i)) {
576 1.1 gwr days -= days_in_year(i);
577 1.3 gwr i++;
578 1.1 gwr }
579 1.3 gwr dt->dt_year = i - POSIX_BASE_YEAR;
580 1.3 gwr
581 1.1 gwr /* Subtract out whole months... */
582 1.1 gwr /* XXX - Note temporary change to month_days */
583 1.1 gwr if (leapyear(i))
584 1.1 gwr days_in_month(FEBRUARY) = 29;
585 1.3 gwr for (i = 1; days >= days_in_month(i); i++)
586 1.1 gwr days -= days_in_month(i);
587 1.3 gwr /* XXX - Undo temporary change to month_days */
588 1.1 gwr days_in_month(FEBRUARY) = 28;
589 1.1 gwr dt->dt_mon = i;
590 1.1 gwr
591 1.1 gwr /* Days are what is left over (+1) from all that. */
592 1.1 gwr dt->dt_day = days + 1;
593 1.3 gwr }
594 1.1 gwr
595 1.3 gwr long dt_to_gmt(struct date_time *dt)
596 1.3 gwr {
597 1.1 gwr long gmt;
598 1.1 gwr int i, year;
599 1.1 gwr
600 1.1 gwr /*
601 1.1 gwr * Hours are different for some reason. Makes no sense really.
602 1.3 gwr */
603 1.1 gwr
604 1.1 gwr gmt = 0;
605 1.1 gwr
606 1.3 gwr if (dt->dt_hour >= 24) goto out;
607 1.1 gwr if (dt->dt_day > 31) goto out;
608 1.3 gwr if (dt->dt_mon > 12) goto out;
609 1.1 gwr
610 1.1 gwr year = dt->dt_year + POSIX_BASE_YEAR;
611 1.1 gwr
612 1.1 gwr /*
613 1.1 gwr * Compute days since start of time
614 1.1 gwr * First from years, then from months.
615 1.3 gwr */
616 1.3 gwr for (i = POSIX_BASE_YEAR; i < year; i++)
617 1.3 gwr gmt += days_in_year(i);
618 1.1 gwr if (leapyear(year) && dt->dt_mon > FEBRUARY)
619 1.1 gwr gmt++;
620 1.3 gwr
621 1.3 gwr /* Months */
622 1.3 gwr for (i = 1; i < dt->dt_mon; i++)
623 1.1 gwr gmt += days_in_month(i);
624 1.1 gwr gmt += (dt->dt_day - 1);
625 1.3 gwr
626 1.1 gwr /* Now do hours */
627 1.1 gwr gmt = gmt * 24 + dt->dt_hour;
628 1.3 gwr
629 1.1 gwr /* Now do minutes */
630 1.1 gwr gmt = gmt * 60 + dt->dt_min;
631 1.3 gwr
632 1.1 gwr /* Now do seconds */
633 1.1 gwr gmt = gmt * 60 + dt->dt_sec;
634 1.3 gwr
635 1.1 gwr out:
636 return gmt;
637 }
638