Home | History | Annotate | Line # | Download | only in sun3x
clock.c revision 1.3
      1  1.3  gwr /*	$NetBSD: clock.c,v 1.3 1997/01/23 22:30:15 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.1  gwr  * XXX - Need to determine which type of clock we have!
     86  1.1  gwr  */
     87  1.1  gwr static int
     88  1.1  gwr clock_match(parent, cf, args)
     89  1.1  gwr     struct device *parent;
     90  1.1  gwr 	struct cfdata *cf;
     91  1.1  gwr     void *args;
     92  1.1  gwr {
     93  1.1  gwr 	struct confargs *ca = args;
     94  1.1  gwr 
     95  1.1  gwr 	/* This driver only supports one unit. */
     96  1.1  gwr 	if (cf->cf_unit != 0)
     97  1.1  gwr 		return (0);
     98  1.1  gwr 
     99  1.1  gwr 	/* Validate the given address. */
    100  1.1  gwr 	if (ca->ca_paddr != OBIO_CLOCK2)
    101  1.1  gwr 		return (0);
    102  1.1  gwr 
    103  1.1  gwr 	/* Default interrupt priority. */
    104  1.1  gwr 	if (ca->ca_intpri == -1)
    105  1.1  gwr 		ca->ca_intpri = CLOCK_PRI;
    106  1.1  gwr 
    107  1.1  gwr 	return (1);
    108  1.1  gwr }
    109  1.1  gwr 
    110  1.1  gwr static void
    111  1.1  gwr clock_attach(parent, self, args)
    112  1.1  gwr 	struct device *parent;
    113  1.1  gwr 	struct device *self;
    114  1.1  gwr 	void *args;
    115  1.1  gwr {
    116  1.1  gwr 
    117  1.1  gwr 	printf("\n");
    118  1.1  gwr 
    119  1.1  gwr 	/*
    120  1.1  gwr 	 * Can not hook up the ISR until cpu_initclocks()
    121  1.1  gwr 	 * because hardclock is not ready until then.
    122  1.1  gwr 	 * For now, the handler is _isr_autovec(), which
    123  1.1  gwr 	 * will complain if it gets clock interrupts.
    124  1.1  gwr 	 */
    125  1.1  gwr }
    126  1.1  gwr 
    127  1.1  gwr /*
    128  1.1  gwr  * Set and/or clear the desired clock bits in the interrupt
    129  1.1  gwr  * register.  We have to be extremely careful that we do it
    130  1.1  gwr  * in such a manner that we don't get ourselves lost.
    131  1.1  gwr  */
    132  1.1  gwr void
    133  1.1  gwr set_clk_mode(on, off, enable)
    134  1.1  gwr 	u_char on, off;
    135  1.1  gwr 	int enable;
    136  1.1  gwr {
    137  1.1  gwr 	register u_char interreg;
    138  1.1  gwr 	register int s;
    139  1.1  gwr 
    140  1.1  gwr 	s = getsr();
    141  1.1  gwr 	if ((s & PSL_IPL) < PSL_IPL7)
    142  1.1  gwr 		panic("set_clk_mode: ipl");
    143  1.1  gwr 
    144  1.3  gwr 	if (!clock_va)
    145  1.1  gwr 		panic("set_clk_mode: map");
    146  1.1  gwr 
    147  1.1  gwr 	/*
    148  1.1  gwr 	 * make sure that we are only playing w/
    149  1.1  gwr 	 * clock interrupt register bits
    150  1.1  gwr 	 */
    151  1.1  gwr 	on &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    152  1.1  gwr 	off &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    153  1.1  gwr 
    154  1.1  gwr 	/*
    155  1.1  gwr 	 * Get a copy of current interrupt register,
    156  1.1  gwr 	 * turning off any undesired bits (aka `off')
    157  1.1  gwr 	 */
    158  1.1  gwr 	interreg = *interrupt_reg & ~(off | IREG_ALL_ENAB);
    159  1.1  gwr 	*interrupt_reg &= ~IREG_ALL_ENAB;
    160  1.1  gwr 
    161  1.1  gwr 	/*
    162  1.1  gwr 	 * Next we turns off the CLK5 and CLK7 bits to clear
    163  1.1  gwr 	 * the flip-flops, then we disable clock interrupts.
    164  1.1  gwr 	 * Now we can read the clock's interrupt register
    165  1.1  gwr 	 * to clear any pending signals there.
    166  1.1  gwr 	 */
    167  1.1  gwr 	*interrupt_reg &= ~(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    168  1.3  gwr 
    169  1.3  gwr 	/* XXX - hit the clock? */
    170  1.1  gwr 
    171  1.1  gwr 	/*
    172  1.1  gwr 	 * Now we set all the desired bits
    173  1.1  gwr 	 * in the interrupt register, then
    174  1.1  gwr 	 * we turn the clock back on and
    175  1.1  gwr 	 * finally we can enable all interrupts.
    176  1.1  gwr 	 */
    177  1.1  gwr 	*interrupt_reg |= (interreg | on);		/* enable flip-flops */
    178  1.1  gwr 
    179  1.3  gwr 	/* XXX - hit the clock? */
    180  1.1  gwr 
    181  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    182  1.1  gwr }
    183  1.1  gwr 
    184  1.1  gwr /* Called very early by internal_configure. */
    185  1.1  gwr void clock_init()
    186  1.1  gwr {
    187  1.3  gwr 	/* XXX - Yes, use the EEPROM address.  Same H/W device. */
    188  1.3  gwr 	clock_va = obio_find_mapping(OBIO_EEPROM, sizeof(struct clockreg));
    189  1.1  gwr 
    190  1.1  gwr 	if (!clock_va)
    191  1.1  gwr 		mon_panic("clock_init: clock_va\n");
    192  1.1  gwr 	if (!interrupt_reg)
    193  1.1  gwr 		mon_panic("clock_init: interrupt_reg\n");
    194  1.1  gwr 
    195  1.1  gwr 	/* Turn off clock interrupts until cpu_initclocks() */
    196  1.1  gwr 	/* isr_init() already set the interrupt reg to zero. */
    197  1.1  gwr }
    198  1.1  gwr 
    199  1.1  gwr /*
    200  1.1  gwr  * Set up the real-time clock (enable clock interrupts).
    201  1.1  gwr  * Leave stathz 0 since there is no secondary clock available.
    202  1.1  gwr  * Note that clock interrupts MUST STAY DISABLED until here.
    203  1.1  gwr  */
    204  1.1  gwr void
    205  1.1  gwr cpu_initclocks(void)
    206  1.1  gwr {
    207  1.1  gwr 	int s;
    208  1.1  gwr 
    209  1.3  gwr 	if (!clock_va)
    210  1.1  gwr 		panic("cpu_initclocks");
    211  1.1  gwr 	s = splhigh();
    212  1.1  gwr 
    213  1.1  gwr 	/* Install isr (in locore.s) that calls clock_intr(). */
    214  1.1  gwr 	isr_add_custom(5, (void*)_isr_clock);
    215  1.1  gwr 
    216  1.1  gwr 	/* Set the clock to interrupt 100 time per second. */
    217  1.3  gwr 	/* XXX - Hard wired? */
    218  1.1  gwr 
    219  1.1  gwr 	*interrupt_reg |= IREG_CLOCK_ENAB_5;	/* enable clock */
    220  1.3  gwr 
    221  1.3  gwr 	/* XXX enable the clock? */
    222  1.3  gwr 
    223  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    224  1.1  gwr 	splx(s);
    225  1.1  gwr }
    226  1.1  gwr 
    227  1.1  gwr /*
    228  1.1  gwr  * This doesn't need to do anything, as we have only one timer and
    229  1.1  gwr  * profhz==stathz==hz.
    230  1.1  gwr  */
    231  1.1  gwr void
    232  1.1  gwr setstatclockrate(newhz)
    233  1.1  gwr 	int newhz;
    234  1.1  gwr {
    235  1.1  gwr 	/* nothing */
    236  1.1  gwr }
    237  1.1  gwr 
    238  1.1  gwr /*
    239  1.3  gwr  * This is is called by the "custom" interrupt handler.
    240  1.1  gwr  */
    241  1.1  gwr void
    242  1.1  gwr clock_intr(cf)
    243  1.1  gwr 	struct clockframe cf;
    244  1.1  gwr {
    245  1.3  gwr 	/* volatile struct clockreg *clk = clock_va; */
    246  1.1  gwr 
    247  1.3  gwr #if 1	/* XXX - Needed? */
    248  1.1  gwr 	/* Pulse the clock intr. enable low. */
    249  1.1  gwr 	*interrupt_reg &= ~IREG_CLOCK_ENAB_5;
    250  1.1  gwr 	*interrupt_reg |=  IREG_CLOCK_ENAB_5;
    251  1.3  gwr #endif
    252  1.1  gwr 
    253  1.3  gwr 	/* XXX - Need to do anything? */
    254  1.1  gwr 	hardclock(&cf);
    255  1.1  gwr }
    256  1.1  gwr 
    257  1.1  gwr /*
    258  1.1  gwr  * Return the best possible estimate of the time in the timeval
    259  1.1  gwr  * to which tvp points.  We do this by returning the current time
    260  1.1  gwr  * plus the amount of time since the last clock interrupt.
    261  1.1  gwr  *
    262  1.1  gwr  * Check that this time is no less than any previously-reported time,
    263  1.1  gwr  * which could happen around the time of a clock adjustment.  Just for
    264  1.1  gwr  * fun, we guarantee that the time will be greater than the value
    265  1.1  gwr  * obtained by a previous call.
    266  1.1  gwr  */
    267  1.1  gwr void
    268  1.1  gwr microtime(tvp)
    269  1.1  gwr 	register struct timeval *tvp;
    270  1.1  gwr {
    271  1.1  gwr 	int s = splhigh();
    272  1.1  gwr 	static struct timeval lasttime;
    273  1.1  gwr 
    274  1.1  gwr 	*tvp = time;
    275  1.1  gwr 	tvp->tv_usec++; 	/* XXX */
    276  1.1  gwr 	while (tvp->tv_usec > 1000000) {
    277  1.1  gwr 		tvp->tv_sec++;
    278  1.1  gwr 		tvp->tv_usec -= 1000000;
    279  1.1  gwr 	}
    280  1.1  gwr 	if (tvp->tv_sec == lasttime.tv_sec &&
    281  1.1  gwr 		tvp->tv_usec <= lasttime.tv_usec &&
    282  1.1  gwr 		(tvp->tv_usec = lasttime.tv_usec + 1) > 1000000)
    283  1.1  gwr 	{
    284  1.1  gwr 		tvp->tv_sec++;
    285  1.1  gwr 		tvp->tv_usec -= 1000000;
    286  1.1  gwr 	}
    287  1.1  gwr 	lasttime = *tvp;
    288  1.1  gwr 	splx(s);
    289  1.1  gwr }
    290  1.1  gwr 
    291  1.1  gwr 
    292  1.1  gwr /*
    293  1.1  gwr  * Machine-dependent clock routines.
    294  1.1  gwr  *
    295  1.1  gwr  * Inittodr initializes the time of day hardware which provides
    296  1.1  gwr  * date functions.
    297  1.1  gwr  *
    298  1.1  gwr  * Resettodr restores the time of day hardware after a time change.
    299  1.1  gwr  */
    300  1.1  gwr #define SECDAY		86400L
    301  1.1  gwr #define SECYR		(SECDAY * 365)
    302  1.1  gwr 
    303  1.1  gwr static long clk_get_secs(void);
    304  1.1  gwr static void clk_set_secs(long);
    305  1.1  gwr 
    306  1.1  gwr /*
    307  1.1  gwr  * Initialize the time of day register, based on the time base
    308  1.1  gwr  * which is, e.g. from a filesystem.
    309  1.1  gwr  */
    310  1.1  gwr void inittodr(fs_time)
    311  1.1  gwr 	time_t fs_time;
    312  1.1  gwr {
    313  1.1  gwr 	long diff, clk_time;
    314  1.1  gwr 	long long_ago = (5 * SECYR);
    315  1.1  gwr 	int clk_bad = 0;
    316  1.1  gwr 
    317  1.1  gwr 	/*
    318  1.1  gwr 	 * Sanity check time from file system.
    319  1.1  gwr 	 * If it is zero,assume filesystem time is just unknown
    320  1.1  gwr 	 * instead of preposterous.  Don't bark.
    321  1.1  gwr 	 */
    322  1.1  gwr 	if (fs_time < long_ago) {
    323  1.1  gwr 		/*
    324  1.1  gwr 		 * If fs_time is zero, assume filesystem time is just
    325  1.1  gwr 		 * unknown instead of preposterous.  Don't bark.
    326  1.1  gwr 		 */
    327  1.1  gwr 		if (fs_time != 0)
    328  1.1  gwr 			printf("WARNING: preposterous time in file system\n");
    329  1.1  gwr 		/* 1991/07/01  12:00:00 */
    330  1.1  gwr 		fs_time = 21*SECYR + 186*SECDAY + SECDAY/2;
    331  1.1  gwr 	}
    332  1.1  gwr 
    333  1.1  gwr 	clk_time = clk_get_secs();
    334  1.1  gwr 
    335  1.1  gwr 	/* Sanity check time from clock. */
    336  1.1  gwr 	if (clk_time < long_ago) {
    337  1.1  gwr 		printf("WARNING: bad date in battery clock");
    338  1.1  gwr 		clk_bad = 1;
    339  1.1  gwr 		clk_time = fs_time;
    340  1.1  gwr 	} else {
    341  1.1  gwr 		/* Does the clock time jive with the file system? */
    342  1.1  gwr 		diff = clk_time - fs_time;
    343  1.1  gwr 		if (diff < 0)
    344  1.1  gwr 			diff = -diff;
    345  1.1  gwr 		if (diff >= (SECDAY*2)) {
    346  1.1  gwr 			printf("WARNING: clock %s %d days",
    347  1.1  gwr 				   (clk_time < fs_time) ? "lost" : "gained",
    348  1.1  gwr 				   (int) (diff / SECDAY));
    349  1.1  gwr 			clk_bad = 1;
    350  1.1  gwr 		}
    351  1.1  gwr 	}
    352  1.1  gwr 	if (clk_bad)
    353  1.1  gwr 		printf(" -- CHECK AND RESET THE DATE!\n");
    354  1.1  gwr 	time.tv_sec = clk_time;
    355  1.1  gwr }
    356  1.1  gwr 
    357  1.1  gwr /*
    358  1.1  gwr  * Resettodr restores the time of day hardware after a time change.
    359  1.1  gwr  */
    360  1.1  gwr void resettodr()
    361  1.1  gwr {
    362  1.1  gwr 	clk_set_secs(time.tv_sec);
    363  1.1  gwr }
    364  1.1  gwr 
    365  1.3  gwr 
    366  1.3  gwr /*
    368  1.3  gwr  * XXX - Todo: take one of the implementations of
    369  1.3  gwr  * "POSIX time" to/from "YY/MM/DD/hh/mm/ss"
    370  1.3  gwr  * and put that in libkern (or somewhere).
    371  1.3  gwr  * Also put this stuct in some header...
    372  1.3  gwr  */
    373  1.3  gwr struct date_time {
    374  1.3  gwr     u_char dt_year;	/* since POSIX_BASE_YEAR (1970) */
    375  1.3  gwr     u_char dt_mon;
    376  1.3  gwr     u_char dt_day;
    377  1.3  gwr     u_char dt_hour;
    378  1.3  gwr     u_char dt_min;
    379  1.3  gwr     u_char dt_sec;
    380  1.3  gwr 	u_char dt_csec;	/* hundredths of a second */
    381  1.3  gwr 	u_char dt_wday;	/* Day of week (needed?) */
    382  1.3  gwr };
    383  1.3  gwr void gmt_to_dt __P((long gmt, struct date_time *dt));
    384  1.3  gwr long dt_to_gmt __P((struct date_time *dt));
    385  1.3  gwr /* Traditional UNIX base year */
    386  1.1  gwr #define	POSIX_BASE_YEAR	1970
    387  1.3  gwr /*
    388  1.1  gwr  * XXX - End of stuff that should move to a header.
    389  1.1  gwr  */
    390  1.1  gwr 
    391  1.1  gwr 
    392  1.3  gwr /*
    393  1.3  gwr  * Routines to copy state into and out of the clock.
    394  1.1  gwr  * The clock CSR has to be set for read or write.
    395  1.3  gwr  */
    396  1.3  gwr 
    397  1.3  gwr static void
    398  1.1  gwr clk_get_dt(struct date_time *dt)
    399  1.3  gwr {
    400  1.1  gwr 	volatile struct clockreg *cl = clock_va;
    401  1.1  gwr 	int s;
    402  1.1  gwr 
    403  1.3  gwr 	s = splhigh();
    404  1.3  gwr 	/* enable read (stop time) */
    405  1.1  gwr 	cl->cl_csr |= CLK_READ;
    406  1.3  gwr 
    407  1.3  gwr 	/* Copy the info */
    408  1.3  gwr 	dt->dt_sec  = cl->cl_sec;
    409  1.3  gwr 	dt->dt_min  = cl->cl_min;
    410  1.3  gwr 	dt->dt_hour = cl->cl_hour;
    411  1.3  gwr 	dt->dt_wday = cl->cl_wday;
    412  1.3  gwr 	dt->dt_day  = cl->cl_mday;
    413  1.3  gwr 	dt->dt_mon  = cl->cl_month;
    414  1.1  gwr 	dt->dt_year = cl->cl_year;
    415  1.3  gwr 
    416  1.3  gwr 	/* Done reading (time wears on) */
    417  1.1  gwr 	cl->cl_csr &= ~CLK_READ;
    418  1.1  gwr 	splx(s);
    419  1.1  gwr }
    420  1.3  gwr 
    421  1.3  gwr static void
    422  1.1  gwr clk_set_dt(struct date_time *dt)
    423  1.3  gwr {
    424  1.1  gwr 	volatile struct clockreg *cl = clock_va;
    425  1.1  gwr 	int s;
    426  1.1  gwr 
    427  1.3  gwr 	s = splhigh();
    428  1.3  gwr 	/* enable write */
    429  1.1  gwr 	cl->cl_csr |= CLK_WRITE;
    430  1.3  gwr 
    431  1.3  gwr 	/* Copy the info */
    432  1.3  gwr 	cl->cl_sec = dt->dt_sec;
    433  1.3  gwr 	cl->cl_min = dt->dt_min;
    434  1.3  gwr 	cl->cl_hour = dt->dt_hour;
    435  1.3  gwr 	cl->cl_wday = dt->dt_wday;
    436  1.3  gwr 	cl->cl_mday = dt->dt_day;
    437  1.3  gwr 	cl->cl_month = dt->dt_mon;
    438  1.1  gwr 	cl->cl_year = dt->dt_year;
    439  1.3  gwr 
    440  1.3  gwr 	/* load them up */
    441  1.1  gwr 	cl->cl_csr &= ~CLK_WRITE;
    442  1.1  gwr 	splx(s);
    443  1.1  gwr }
    444  1.1  gwr 
    445  1.3  gwr 
    446  1.3  gwr /*
    447  1.3  gwr  * Now routines to get and set clock as POSIX time.
    448  1.3  gwr  * Our clock keeps "years since 1/1/1968", so we must
    449  1.3  gwr  * convert to/from "years since 1/1/1970" before the
    450  1.3  gwr  * common time conversion functions are used.
    451  1.3  gwr  */
    452  1.3  gwr #define	CLOCK_YEAR_ADJUST (POSIX_BASE_YEAR - 1968)
    453  1.3  gwr static long
    454  1.3  gwr clk_get_secs()
    455  1.3  gwr {
    456  1.3  gwr 	struct date_time dt;
    457  1.3  gwr 	long gmt;
    458  1.3  gwr 
    459  1.3  gwr 	clk_get_dt(&dt);
    460  1.3  gwr 	dt.dt_year -= CLOCK_YEAR_ADJUST;
    461  1.3  gwr 	gmt = dt_to_gmt(&dt);
    462  1.3  gwr 	return (gmt);
    463  1.3  gwr }
    464  1.3  gwr static void
    465  1.3  gwr clk_set_secs(secs)
    466  1.3  gwr 	long 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.3  gwr 	gmt = secs;
    472  1.3  gwr 	gmt_to_dt(gmt, &dt);
    473  1.3  gwr 	dt.dt_year += CLOCK_YEAR_ADJUST;
    474  1.3  gwr 	clk_set_dt(&dt);
    475  1.3  gwr }
    476  1.3  gwr 
    477  1.1  gwr 
    478  1.3  gwr 
    479  1.3  gwr /*****************************************************************
    481  1.1  gwr  *
    482  1.1  gwr  * Generic routines to convert to or from a POSIX date
    483  1.1  gwr  * (seconds since 1/1/1970) and  yr/mo/day/hr/min/sec
    484  1.1  gwr  *
    485  1.1  gwr  * These are organized this way mostly to so the code
    486  1.3  gwr  * can easily be tested in an independent user program.
    487  1.3  gwr  * (These are derived from the hp300 code.)
    488  1.1  gwr  *
    489  1.3  gwr  * XXX - Should move these to libkern or somewhere...
    490  1.1  gwr  */
    491  1.1  gwr static inline int leapyear __P((int year));
    492  1.1  gwr #define FEBRUARY	2
    493  1.1  gwr #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
    494  1.3  gwr #define	days_in_month(a) 	(month_days[(a) - 1])
    495  1.3  gwr 
    496  1.3  gwr /*
    497  1.3  gwr  * Note:  This array may be modified by gmt_to_dt(),
    498  1.3  gwr  * but these functions DO NOT need to be reentrant.
    499  1.3  gwr  * If we ever DO need reentrance, we should just make
    500  1.3  gwr  * gmt_to_dt() copy this to a local before use. -gwr
    501  1.1  gwr  */
    502  1.1  gwr static char month_days[12] = {
    503  1.1  gwr 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    504  1.3  gwr };
    505  1.3  gwr 
    506  1.3  gwr /* Use an inline to make the logic more obvious. */
    507  1.3  gwr static inline int
    508  1.3  gwr leapyear(year)
    509  1.3  gwr 	int year;
    510  1.3  gwr {
    511  1.3  gwr 	int rv = 0;
    512  1.3  gwr 
    513  1.3  gwr 	if ((year % 4) == 0) {
    514  1.3  gwr 		rv = 1;
    515  1.3  gwr 		if ((year % 100) == 0) {
    516  1.3  gwr 			rv = 0;
    517  1.3  gwr 			if ((year % 400) == 0)
    518  1.3  gwr 				rv = 1;
    519  1.3  gwr 		}
    520  1.3  gwr 	}
    521  1.3  gwr 	return rv;
    522  1.3  gwr }
    523  1.1  gwr 
    524  1.3  gwr void gmt_to_dt(long gmt, struct date_time *dt)
    525  1.3  gwr {
    526  1.1  gwr 	long secs;
    527  1.3  gwr 	int i, days;
    528  1.3  gwr 
    529  1.1  gwr 	days = gmt / SECDAY;
    530  1.1  gwr 	secs = gmt % SECDAY;
    531  1.1  gwr 
    532  1.1  gwr 	/* Hours, minutes, seconds are easy */
    533  1.1  gwr 	dt->dt_hour = secs / 3600;
    534  1.1  gwr 	secs = secs % 3600;
    535  1.1  gwr 	dt->dt_min  = secs / 60;
    536  1.1  gwr 	secs = secs % 60;
    537  1.1  gwr 	dt->dt_sec  = secs;
    538  1.3  gwr 
    539  1.1  gwr 	/* Day of week (Note: 1/1/1970 was a Thursday) */
    540  1.3  gwr 	dt->dt_wday = (days + 4) % 7;
    541  1.1  gwr 
    542  1.1  gwr 	/* Subtract out whole years... */
    543  1.1  gwr 	i = POSIX_BASE_YEAR;
    544  1.1  gwr 	while (days >= days_in_year(i)) {
    545  1.1  gwr 		days -= days_in_year(i);
    546  1.3  gwr 		i++;
    547  1.1  gwr 	}
    548  1.3  gwr 	dt->dt_year = i - POSIX_BASE_YEAR;
    549  1.3  gwr 
    550  1.1  gwr 	/* Subtract out whole months... */
    551  1.1  gwr 	/* XXX - Note temporary change to month_days */
    552  1.1  gwr 	if (leapyear(i))
    553  1.1  gwr 		days_in_month(FEBRUARY) = 29;
    554  1.3  gwr 	for (i = 1; days >= days_in_month(i); i++)
    555  1.1  gwr 		days -= days_in_month(i);
    556  1.3  gwr 	/* XXX - Undo temporary change to month_days */
    557  1.1  gwr 	days_in_month(FEBRUARY) = 28;
    558  1.1  gwr 	dt->dt_mon = i;
    559  1.1  gwr 
    560  1.1  gwr 	/* Days are what is left over (+1) from all that. */
    561  1.1  gwr 	dt->dt_day = days + 1;
    562  1.3  gwr }
    563  1.1  gwr 
    564  1.3  gwr long dt_to_gmt(struct date_time *dt)
    565  1.3  gwr {
    566  1.1  gwr 	long gmt;
    567  1.1  gwr 	int i, year;
    568  1.1  gwr 
    569  1.1  gwr 	/*
    570  1.1  gwr 	 * Hours are different for some reason. Makes no sense really.
    571  1.3  gwr 	 */
    572  1.1  gwr 
    573  1.1  gwr 	gmt = 0;
    574  1.1  gwr 
    575  1.3  gwr 	if (dt->dt_hour >= 24) goto out;
    576  1.1  gwr 	if (dt->dt_day  >  31) goto out;
    577  1.3  gwr 	if (dt->dt_mon   > 12) goto out;
    578  1.1  gwr 
    579  1.1  gwr 	year = dt->dt_year + POSIX_BASE_YEAR;
    580  1.1  gwr 
    581  1.1  gwr 	/*
    582  1.1  gwr 	 * Compute days since start of time
    583  1.1  gwr 	 * First from years, then from months.
    584  1.3  gwr 	 */
    585  1.3  gwr 	for (i = POSIX_BASE_YEAR; i < year; i++)
    586  1.3  gwr 		gmt += days_in_year(i);
    587  1.1  gwr 	if (leapyear(year) && dt->dt_mon > FEBRUARY)
    588  1.1  gwr 		gmt++;
    589  1.3  gwr 
    590  1.3  gwr 	/* Months */
    591  1.3  gwr 	for (i = 1; i < dt->dt_mon; i++)
    592  1.1  gwr 	  	gmt += days_in_month(i);
    593  1.1  gwr 	gmt += (dt->dt_day - 1);
    594  1.3  gwr 
    595  1.1  gwr 	/* Now do hours */
    596  1.1  gwr 	gmt = gmt * 24 + dt->dt_hour;
    597  1.3  gwr 
    598  1.1  gwr 	/* Now do minutes */
    599  1.1  gwr 	gmt = gmt * 60 + dt->dt_min;
    600  1.3  gwr 
    601  1.1  gwr 	/* Now do seconds */
    602  1.1  gwr 	gmt = gmt * 60 + dt->dt_sec;
    603  1.3  gwr 
    604  1.1  gwr  out:
    605           	return gmt;
    606           }
    607