Home | History | Annotate | Line # | Download | only in sun3x
clock.c revision 1.4
      1  1.4  gwr /*	$NetBSD: clock.c,v 1.4 1997/01/25 21:46:19 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.4  gwr #include <sun3/sun3/sunmon.h>
     64  1.4  gwr 
     65  1.3  gwr #include "mostek48t02.h"
     66  1.1  gwr 
     67  1.1  gwr #define	CLOCK_PRI	5
     68  1.1  gwr 
     69  1.1  gwr void _isr_clock __P((void));	/* in locore.s */
     70  1.1  gwr void clock_intr __P((struct clockframe));
     71  1.1  gwr 
     72  1.1  gwr /* Note: this is used by locore.s:__isr_clock */
     73  1.3  gwr static volatile void *clock_va;
     74  1.1  gwr 
     75  1.1  gwr static int  clock_match __P((struct device *, struct cfdata *, void *args));
     76  1.1  gwr static void clock_attach __P((struct device *, struct device *, void *));
     77  1.1  gwr 
     78  1.1  gwr struct cfattach clock_ca = {
     79  1.1  gwr 	sizeof(struct device), clock_match, clock_attach
     80  1.1  gwr };
     81  1.1  gwr 
     82  1.1  gwr struct cfdriver clock_cd = {
     83  1.1  gwr 	NULL, "clock", DV_DULL
     84  1.1  gwr };
     85  1.1  gwr 
     86  1.1  gwr /*
     87  1.1  gwr  * XXX - Need to determine which type of clock we have!
     88  1.1  gwr  */
     89  1.1  gwr static int
     90  1.1  gwr clock_match(parent, cf, args)
     91  1.1  gwr     struct device *parent;
     92  1.1  gwr 	struct cfdata *cf;
     93  1.1  gwr     void *args;
     94  1.1  gwr {
     95  1.1  gwr 	struct confargs *ca = args;
     96  1.1  gwr 
     97  1.1  gwr 	/* This driver only supports one unit. */
     98  1.1  gwr 	if (cf->cf_unit != 0)
     99  1.1  gwr 		return (0);
    100  1.1  gwr 
    101  1.1  gwr 	/* Validate the given address. */
    102  1.1  gwr 	if (ca->ca_paddr != OBIO_CLOCK2)
    103  1.1  gwr 		return (0);
    104  1.1  gwr 
    105  1.1  gwr 	/* Default interrupt priority. */
    106  1.1  gwr 	if (ca->ca_intpri == -1)
    107  1.1  gwr 		ca->ca_intpri = CLOCK_PRI;
    108  1.1  gwr 
    109  1.1  gwr 	return (1);
    110  1.1  gwr }
    111  1.1  gwr 
    112  1.1  gwr static void
    113  1.1  gwr clock_attach(parent, self, args)
    114  1.1  gwr 	struct device *parent;
    115  1.1  gwr 	struct device *self;
    116  1.1  gwr 	void *args;
    117  1.1  gwr {
    118  1.1  gwr 
    119  1.1  gwr 	printf("\n");
    120  1.1  gwr 
    121  1.1  gwr 	/*
    122  1.1  gwr 	 * Can not hook up the ISR until cpu_initclocks()
    123  1.1  gwr 	 * because hardclock is not ready until then.
    124  1.1  gwr 	 * For now, the handler is _isr_autovec(), which
    125  1.1  gwr 	 * will complain if it gets clock interrupts.
    126  1.1  gwr 	 */
    127  1.1  gwr }
    128  1.1  gwr 
    129  1.1  gwr /*
    130  1.1  gwr  * Set and/or clear the desired clock bits in the interrupt
    131  1.1  gwr  * register.  We have to be extremely careful that we do it
    132  1.1  gwr  * in such a manner that we don't get ourselves lost.
    133  1.1  gwr  */
    134  1.1  gwr void
    135  1.1  gwr set_clk_mode(on, off, enable)
    136  1.1  gwr 	u_char on, off;
    137  1.1  gwr 	int enable;
    138  1.1  gwr {
    139  1.1  gwr 	register u_char interreg;
    140  1.1  gwr 	register int s;
    141  1.1  gwr 
    142  1.4  gwr 	/* If we don't have this, we must not have touched it! */
    143  1.4  gwr 	if (!clock_va)
    144  1.4  gwr 		return;
    145  1.4  gwr 
    146  1.1  gwr 	s = getsr();
    147  1.1  gwr 	if ((s & PSL_IPL) < PSL_IPL7)
    148  1.1  gwr 		panic("set_clk_mode: ipl");
    149  1.1  gwr 
    150  1.1  gwr 	/*
    151  1.1  gwr 	 * make sure that we are only playing w/
    152  1.1  gwr 	 * clock interrupt register bits
    153  1.1  gwr 	 */
    154  1.1  gwr 	on &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    155  1.1  gwr 	off &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    156  1.1  gwr 
    157  1.1  gwr 	/*
    158  1.1  gwr 	 * Get a copy of current interrupt register,
    159  1.1  gwr 	 * turning off any undesired bits (aka `off')
    160  1.1  gwr 	 */
    161  1.1  gwr 	interreg = *interrupt_reg & ~(off | IREG_ALL_ENAB);
    162  1.1  gwr 	*interrupt_reg &= ~IREG_ALL_ENAB;
    163  1.1  gwr 
    164  1.1  gwr 	/*
    165  1.1  gwr 	 * Next we turns off the CLK5 and CLK7 bits to clear
    166  1.1  gwr 	 * the flip-flops, then we disable clock interrupts.
    167  1.1  gwr 	 * Now we can read the clock's interrupt register
    168  1.1  gwr 	 * to clear any pending signals there.
    169  1.1  gwr 	 */
    170  1.1  gwr 	*interrupt_reg &= ~(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    171  1.3  gwr 
    172  1.3  gwr 	/* XXX - hit the clock? */
    173  1.1  gwr 
    174  1.1  gwr 	/*
    175  1.1  gwr 	 * Now we set all the desired bits
    176  1.1  gwr 	 * in the interrupt register, then
    177  1.1  gwr 	 * we turn the clock back on and
    178  1.1  gwr 	 * finally we can enable all interrupts.
    179  1.1  gwr 	 */
    180  1.1  gwr 	*interrupt_reg |= (interreg | on);		/* enable flip-flops */
    181  1.1  gwr 
    182  1.3  gwr 	/* XXX - hit the clock? */
    183  1.1  gwr 
    184  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    185  1.1  gwr }
    186  1.1  gwr 
    187  1.1  gwr /* Called very early by internal_configure. */
    188  1.1  gwr void clock_init()
    189  1.1  gwr {
    190  1.3  gwr 	/* XXX - Yes, use the EEPROM address.  Same H/W device. */
    191  1.3  gwr 	clock_va = obio_find_mapping(OBIO_EEPROM, sizeof(struct clockreg));
    192  1.1  gwr 
    193  1.4  gwr 	if (!clock_va || !interrupt_reg) {
    194  1.4  gwr 		mon_printf("clock_init\n");
    195  1.4  gwr 		sunmon_abort();
    196  1.4  gwr 	}
    197  1.1  gwr 
    198  1.1  gwr 	/* Turn off clock interrupts until cpu_initclocks() */
    199  1.1  gwr 	/* isr_init() already set the interrupt reg to zero. */
    200  1.1  gwr }
    201  1.1  gwr 
    202  1.1  gwr /*
    203  1.1  gwr  * Set up the real-time clock (enable clock interrupts).
    204  1.1  gwr  * Leave stathz 0 since there is no secondary clock available.
    205  1.1  gwr  * Note that clock interrupts MUST STAY DISABLED until here.
    206  1.1  gwr  */
    207  1.1  gwr void
    208  1.1  gwr cpu_initclocks(void)
    209  1.1  gwr {
    210  1.1  gwr 	int s;
    211  1.1  gwr 
    212  1.3  gwr 	if (!clock_va)
    213  1.1  gwr 		panic("cpu_initclocks");
    214  1.1  gwr 	s = splhigh();
    215  1.1  gwr 
    216  1.1  gwr 	/* Install isr (in locore.s) that calls clock_intr(). */
    217  1.1  gwr 	isr_add_custom(5, (void*)_isr_clock);
    218  1.1  gwr 
    219  1.1  gwr 	/* Set the clock to interrupt 100 time per second. */
    220  1.3  gwr 	/* XXX - Hard wired? */
    221  1.1  gwr 
    222  1.1  gwr 	*interrupt_reg |= IREG_CLOCK_ENAB_5;	/* enable clock */
    223  1.3  gwr 
    224  1.3  gwr 	/* XXX enable the clock? */
    225  1.3  gwr 
    226  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    227  1.1  gwr 	splx(s);
    228  1.1  gwr }
    229  1.1  gwr 
    230  1.1  gwr /*
    231  1.1  gwr  * This doesn't need to do anything, as we have only one timer and
    232  1.1  gwr  * profhz==stathz==hz.
    233  1.1  gwr  */
    234  1.1  gwr void
    235  1.1  gwr setstatclockrate(newhz)
    236  1.1  gwr 	int newhz;
    237  1.1  gwr {
    238  1.1  gwr 	/* nothing */
    239  1.1  gwr }
    240  1.1  gwr 
    241  1.1  gwr /*
    242  1.3  gwr  * This is is called by the "custom" interrupt handler.
    243  1.1  gwr  */
    244  1.1  gwr void
    245  1.1  gwr clock_intr(cf)
    246  1.1  gwr 	struct clockframe cf;
    247  1.1  gwr {
    248  1.3  gwr 	/* volatile struct clockreg *clk = clock_va; */
    249  1.1  gwr 
    250  1.3  gwr #if 1	/* XXX - Needed? */
    251  1.1  gwr 	/* Pulse the clock intr. enable low. */
    252  1.1  gwr 	*interrupt_reg &= ~IREG_CLOCK_ENAB_5;
    253  1.1  gwr 	*interrupt_reg |=  IREG_CLOCK_ENAB_5;
    254  1.3  gwr #endif
    255  1.1  gwr 
    256  1.3  gwr 	/* XXX - Need to do anything? */
    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 
    400  1.3  gwr static void
    401  1.1  gwr clk_get_dt(struct date_time *dt)
    402  1.3  gwr {
    403  1.1  gwr 	volatile struct clockreg *cl = clock_va;
    404  1.1  gwr 	int s;
    405  1.1  gwr 
    406  1.3  gwr 	s = splhigh();
    407  1.3  gwr 	/* enable read (stop time) */
    408  1.1  gwr 	cl->cl_csr |= CLK_READ;
    409  1.3  gwr 
    410  1.3  gwr 	/* Copy the info */
    411  1.3  gwr 	dt->dt_sec  = cl->cl_sec;
    412  1.3  gwr 	dt->dt_min  = cl->cl_min;
    413  1.3  gwr 	dt->dt_hour = cl->cl_hour;
    414  1.3  gwr 	dt->dt_wday = cl->cl_wday;
    415  1.3  gwr 	dt->dt_day  = cl->cl_mday;
    416  1.3  gwr 	dt->dt_mon  = cl->cl_month;
    417  1.1  gwr 	dt->dt_year = cl->cl_year;
    418  1.3  gwr 
    419  1.3  gwr 	/* Done reading (time wears on) */
    420  1.1  gwr 	cl->cl_csr &= ~CLK_READ;
    421  1.1  gwr 	splx(s);
    422  1.1  gwr }
    423  1.3  gwr 
    424  1.3  gwr static void
    425  1.1  gwr clk_set_dt(struct date_time *dt)
    426  1.3  gwr {
    427  1.1  gwr 	volatile struct clockreg *cl = clock_va;
    428  1.1  gwr 	int s;
    429  1.1  gwr 
    430  1.3  gwr 	s = splhigh();
    431  1.3  gwr 	/* enable write */
    432  1.1  gwr 	cl->cl_csr |= CLK_WRITE;
    433  1.3  gwr 
    434  1.3  gwr 	/* Copy the info */
    435  1.3  gwr 	cl->cl_sec = dt->dt_sec;
    436  1.3  gwr 	cl->cl_min = dt->dt_min;
    437  1.3  gwr 	cl->cl_hour = dt->dt_hour;
    438  1.3  gwr 	cl->cl_wday = dt->dt_wday;
    439  1.3  gwr 	cl->cl_mday = dt->dt_day;
    440  1.3  gwr 	cl->cl_month = dt->dt_mon;
    441  1.1  gwr 	cl->cl_year = dt->dt_year;
    442  1.3  gwr 
    443  1.3  gwr 	/* load them up */
    444  1.1  gwr 	cl->cl_csr &= ~CLK_WRITE;
    445  1.1  gwr 	splx(s);
    446  1.1  gwr }
    447  1.1  gwr 
    448  1.3  gwr 
    449  1.3  gwr /*
    450  1.3  gwr  * Now routines to get and set clock as POSIX time.
    451  1.3  gwr  * Our clock keeps "years since 1/1/1968", so we must
    452  1.3  gwr  * convert to/from "years since 1/1/1970" before the
    453  1.3  gwr  * common time conversion functions are used.
    454  1.3  gwr  */
    455  1.3  gwr #define	CLOCK_YEAR_ADJUST (POSIX_BASE_YEAR - 1968)
    456  1.3  gwr static long
    457  1.3  gwr clk_get_secs()
    458  1.3  gwr {
    459  1.3  gwr 	struct date_time dt;
    460  1.3  gwr 	long gmt;
    461  1.3  gwr 
    462  1.3  gwr 	clk_get_dt(&dt);
    463  1.3  gwr 	dt.dt_year -= CLOCK_YEAR_ADJUST;
    464  1.3  gwr 	gmt = dt_to_gmt(&dt);
    465  1.3  gwr 	return (gmt);
    466  1.3  gwr }
    467  1.3  gwr static void
    468  1.3  gwr clk_set_secs(secs)
    469  1.3  gwr 	long secs;
    470  1.3  gwr {
    471  1.3  gwr 	struct date_time dt;
    472  1.3  gwr 	long gmt;
    473  1.3  gwr 
    474  1.3  gwr 	gmt = secs;
    475  1.3  gwr 	gmt_to_dt(gmt, &dt);
    476  1.3  gwr 	dt.dt_year += CLOCK_YEAR_ADJUST;
    477  1.3  gwr 	clk_set_dt(&dt);
    478  1.3  gwr }
    479  1.3  gwr 
    480  1.1  gwr 
    481  1.3  gwr 
    482  1.3  gwr /*****************************************************************
    484  1.1  gwr  *
    485  1.1  gwr  * Generic routines to convert to or from a POSIX date
    486  1.1  gwr  * (seconds since 1/1/1970) and  yr/mo/day/hr/min/sec
    487  1.1  gwr  *
    488  1.1  gwr  * These are organized this way mostly to so the code
    489  1.3  gwr  * can easily be tested in an independent user program.
    490  1.3  gwr  * (These are derived from the hp300 code.)
    491  1.1  gwr  *
    492  1.3  gwr  * XXX - Should move these to libkern or somewhere...
    493  1.1  gwr  */
    494  1.1  gwr static inline int leapyear __P((int year));
    495  1.1  gwr #define FEBRUARY	2
    496  1.1  gwr #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
    497  1.3  gwr #define	days_in_month(a) 	(month_days[(a) - 1])
    498  1.3  gwr 
    499  1.3  gwr /*
    500  1.3  gwr  * Note:  This array may be modified by gmt_to_dt(),
    501  1.3  gwr  * but these functions DO NOT need to be reentrant.
    502  1.3  gwr  * If we ever DO need reentrance, we should just make
    503  1.4  gwr  * gmt_to_dt() copy this to a local before use. -gwr
    504  1.1  gwr  */
    505  1.1  gwr static int month_days[12] = {
    506  1.1  gwr 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    507  1.3  gwr };
    508  1.3  gwr 
    509  1.3  gwr /* Use an inline to make the logic more obvious. */
    510  1.3  gwr static inline int
    511  1.3  gwr leapyear(year)
    512  1.3  gwr 	int year;
    513  1.3  gwr {
    514  1.3  gwr 	int rv = 0;
    515  1.3  gwr 
    516  1.3  gwr 	if ((year % 4) == 0) {
    517  1.3  gwr 		rv = 1;
    518  1.3  gwr 		if ((year % 100) == 0) {
    519  1.3  gwr 			rv = 0;
    520  1.3  gwr 			if ((year % 400) == 0)
    521  1.3  gwr 				rv = 1;
    522  1.3  gwr 		}
    523  1.3  gwr 	}
    524  1.3  gwr 	return rv;
    525  1.3  gwr }
    526  1.1  gwr 
    527  1.3  gwr void gmt_to_dt(long gmt, struct date_time *dt)
    528  1.3  gwr {
    529  1.1  gwr 	long secs;
    530  1.3  gwr 	int i, days;
    531  1.3  gwr 
    532  1.1  gwr 	days = gmt / SECDAY;
    533  1.1  gwr 	secs = gmt % SECDAY;
    534  1.1  gwr 
    535  1.1  gwr 	/* Hours, minutes, seconds are easy */
    536  1.1  gwr 	dt->dt_hour = secs / 3600;
    537  1.1  gwr 	secs = secs % 3600;
    538  1.1  gwr 	dt->dt_min  = secs / 60;
    539  1.1  gwr 	secs = secs % 60;
    540  1.1  gwr 	dt->dt_sec  = secs;
    541  1.3  gwr 
    542  1.1  gwr 	/* Day of week (Note: 1/1/1970 was a Thursday) */
    543  1.3  gwr 	dt->dt_wday = (days + 4) % 7;
    544  1.1  gwr 
    545  1.1  gwr 	/* Subtract out whole years... */
    546  1.1  gwr 	i = POSIX_BASE_YEAR;
    547  1.1  gwr 	while (days >= days_in_year(i)) {
    548  1.1  gwr 		days -= days_in_year(i);
    549  1.3  gwr 		i++;
    550  1.1  gwr 	}
    551  1.3  gwr 	dt->dt_year = i - POSIX_BASE_YEAR;
    552  1.3  gwr 
    553  1.1  gwr 	/* Subtract out whole months... */
    554  1.1  gwr 	/* XXX - Note temporary change to month_days */
    555  1.1  gwr 	if (leapyear(i))
    556  1.1  gwr 		days_in_month(FEBRUARY) = 29;
    557  1.3  gwr 	for (i = 1; days >= days_in_month(i); i++)
    558  1.1  gwr 		days -= days_in_month(i);
    559  1.3  gwr 	/* XXX - Undo temporary change to month_days */
    560  1.1  gwr 	days_in_month(FEBRUARY) = 28;
    561  1.1  gwr 	dt->dt_mon = i;
    562  1.1  gwr 
    563  1.1  gwr 	/* Days are what is left over (+1) from all that. */
    564  1.1  gwr 	dt->dt_day = days + 1;
    565  1.3  gwr }
    566  1.1  gwr 
    567  1.3  gwr long dt_to_gmt(struct date_time *dt)
    568  1.3  gwr {
    569  1.1  gwr 	long gmt;
    570  1.1  gwr 	int i, year;
    571  1.1  gwr 
    572  1.1  gwr 	/*
    573  1.1  gwr 	 * Hours are different for some reason. Makes no sense really.
    574  1.3  gwr 	 */
    575  1.1  gwr 
    576  1.1  gwr 	gmt = 0;
    577  1.1  gwr 
    578  1.3  gwr 	if (dt->dt_hour >= 24) goto out;
    579  1.1  gwr 	if (dt->dt_day  >  31) goto out;
    580  1.3  gwr 	if (dt->dt_mon   > 12) goto out;
    581  1.1  gwr 
    582  1.1  gwr 	year = dt->dt_year + POSIX_BASE_YEAR;
    583  1.1  gwr 
    584  1.1  gwr 	/*
    585  1.1  gwr 	 * Compute days since start of time
    586  1.1  gwr 	 * First from years, then from months.
    587  1.3  gwr 	 */
    588  1.3  gwr 	for (i = POSIX_BASE_YEAR; i < year; i++)
    589  1.3  gwr 		gmt += days_in_year(i);
    590  1.1  gwr 	if (leapyear(year) && dt->dt_mon > FEBRUARY)
    591  1.1  gwr 		gmt++;
    592  1.3  gwr 
    593  1.3  gwr 	/* Months */
    594  1.3  gwr 	for (i = 1; i < dt->dt_mon; i++)
    595  1.1  gwr 	  	gmt += days_in_month(i);
    596  1.1  gwr 	gmt += (dt->dt_day - 1);
    597  1.3  gwr 
    598  1.1  gwr 	/* Now do hours */
    599  1.1  gwr 	gmt = gmt * 24 + dt->dt_hour;
    600  1.3  gwr 
    601  1.1  gwr 	/* Now do minutes */
    602  1.1  gwr 	gmt = gmt * 60 + dt->dt_min;
    603  1.3  gwr 
    604  1.1  gwr 	/* Now do seconds */
    605  1.1  gwr 	gmt = gmt * 60 + dt->dt_sec;
    606  1.3  gwr 
    607  1.1  gwr  out:
    608           	return gmt;
    609           }
    610