Home | History | Annotate | Line # | Download | only in sun3x
clock.c revision 1.1
      1  1.1  gwr /*	$NetBSD: clock.c,v 1.1 1997/01/14 20:57:08 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.1  gwr  * Machine-dependent clock routines for the Intersil 7170:
     48  1.1  gwr  * Original by Adam Glass;  partially rewritten by Gordon Ross.
     49  1.1  gwr  */
     50  1.1  gwr 
     51  1.1  gwr #include <sys/param.h>
     52  1.1  gwr #include <sys/systm.h>
     53  1.1  gwr #include <sys/time.h>
     54  1.1  gwr #include <sys/kernel.h>
     55  1.1  gwr #include <sys/device.h>
     56  1.1  gwr 
     57  1.1  gwr #include <machine/autoconf.h>
     58  1.1  gwr #include <machine/cpu.h>
     59  1.1  gwr #include <machine/mon.h>
     60  1.1  gwr #include <machine/obio.h>
     61  1.1  gwr 
     62  1.1  gwr #include "intersil7170.h"
     63  1.1  gwr #include "interreg.h"
     64  1.1  gwr #include "machdep.h"
     65  1.1  gwr 
     66  1.1  gwr #define	CLOCK_PRI	5
     67  1.1  gwr 
     68  1.1  gwr void _isr_clock __P((void));	/* in locore.s */
     69  1.1  gwr void clock_intr __P((struct clockframe));
     70  1.1  gwr 
     71  1.1  gwr /* Note: this is used by locore.s:__isr_clock */
     72  1.1  gwr static volatile char *clock_va;
     73  1.1  gwr 
     74  1.1  gwr #define intersil_clock ((volatile struct intersil7170 *) clock_va)
     75  1.1  gwr 
     76  1.1  gwr #define intersil_command(run, interrupt) \
     77  1.1  gwr 	(run | interrupt | INTERSIL_CMD_FREQ_32K | INTERSIL_CMD_24HR_MODE | \
     78  1.1  gwr 	 INTERSIL_CMD_NORMAL_MODE)
     79  1.1  gwr 
     80  1.1  gwr #define intersil_clear() (void)intersil_clock->clk_intr_reg
     81  1.1  gwr 
     82  1.1  gwr static int  clock_match __P((struct device *, struct cfdata *, void *args));
     83  1.1  gwr static void clock_attach __P((struct device *, struct device *, void *));
     84  1.1  gwr 
     85  1.1  gwr struct cfattach clock_ca = {
     86  1.1  gwr 	sizeof(struct device), clock_match, clock_attach
     87  1.1  gwr };
     88  1.1  gwr 
     89  1.1  gwr struct cfdriver clock_cd = {
     90  1.1  gwr 	NULL, "clock", DV_DULL
     91  1.1  gwr };
     92  1.1  gwr 
     93  1.1  gwr /*
     94  1.1  gwr  * XXX - Need to determine which type of clock we have!
     95  1.1  gwr  */
     96  1.1  gwr static int
     97  1.1  gwr clock_match(parent, cf, args)
     98  1.1  gwr     struct device *parent;
     99  1.1  gwr 	struct cfdata *cf;
    100  1.1  gwr     void *args;
    101  1.1  gwr {
    102  1.1  gwr 	struct confargs *ca = args;
    103  1.1  gwr 
    104  1.1  gwr 	/* This driver only supports one unit. */
    105  1.1  gwr 	if (cf->cf_unit != 0)
    106  1.1  gwr 		return (0);
    107  1.1  gwr 
    108  1.1  gwr 	/* Validate the given address. */
    109  1.1  gwr 	if (ca->ca_paddr != OBIO_CLOCK2)
    110  1.1  gwr 		return (0);
    111  1.1  gwr 
    112  1.1  gwr 	/* Default interrupt priority. */
    113  1.1  gwr 	if (ca->ca_intpri == -1)
    114  1.1  gwr 		ca->ca_intpri = CLOCK_PRI;
    115  1.1  gwr 
    116  1.1  gwr 	return (1);
    117  1.1  gwr }
    118  1.1  gwr 
    119  1.1  gwr static void
    120  1.1  gwr clock_attach(parent, self, args)
    121  1.1  gwr 	struct device *parent;
    122  1.1  gwr 	struct device *self;
    123  1.1  gwr 	void *args;
    124  1.1  gwr {
    125  1.1  gwr 
    126  1.1  gwr 	printf("\n");
    127  1.1  gwr 
    128  1.1  gwr 	/*
    129  1.1  gwr 	 * Can not hook up the ISR until cpu_initclocks()
    130  1.1  gwr 	 * because hardclock is not ready until then.
    131  1.1  gwr 	 * For now, the handler is _isr_autovec(), which
    132  1.1  gwr 	 * will complain if it gets clock interrupts.
    133  1.1  gwr 	 */
    134  1.1  gwr }
    135  1.1  gwr 
    136  1.1  gwr /*
    137  1.1  gwr  * Set and/or clear the desired clock bits in the interrupt
    138  1.1  gwr  * register.  We have to be extremely careful that we do it
    139  1.1  gwr  * in such a manner that we don't get ourselves lost.
    140  1.1  gwr  */
    141  1.1  gwr void
    142  1.1  gwr set_clk_mode(on, off, enable)
    143  1.1  gwr 	u_char on, off;
    144  1.1  gwr 	int enable;
    145  1.1  gwr {
    146  1.1  gwr 	register u_char interreg;
    147  1.1  gwr 	register int s;
    148  1.1  gwr 
    149  1.1  gwr 	s = getsr();
    150  1.1  gwr 	if ((s & PSL_IPL) < PSL_IPL7)
    151  1.1  gwr 		panic("set_clk_mode: ipl");
    152  1.1  gwr 
    153  1.1  gwr 	if (!intersil_clock)
    154  1.1  gwr 		panic("set_clk_mode: map");
    155  1.1  gwr 
    156  1.1  gwr 	/*
    157  1.1  gwr 	 * make sure that we are only playing w/
    158  1.1  gwr 	 * clock interrupt register bits
    159  1.1  gwr 	 */
    160  1.1  gwr 	on &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    161  1.1  gwr 	off &= (IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    162  1.1  gwr 
    163  1.1  gwr 	/*
    164  1.1  gwr 	 * Get a copy of current interrupt register,
    165  1.1  gwr 	 * turning off any undesired bits (aka `off')
    166  1.1  gwr 	 */
    167  1.1  gwr 	interreg = *interrupt_reg & ~(off | IREG_ALL_ENAB);
    168  1.1  gwr 	*interrupt_reg &= ~IREG_ALL_ENAB;
    169  1.1  gwr 
    170  1.1  gwr 	/*
    171  1.1  gwr 	 * Next we turns off the CLK5 and CLK7 bits to clear
    172  1.1  gwr 	 * the flip-flops, then we disable clock interrupts.
    173  1.1  gwr 	 * Now we can read the clock's interrupt register
    174  1.1  gwr 	 * to clear any pending signals there.
    175  1.1  gwr 	 */
    176  1.1  gwr 	*interrupt_reg &= ~(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5);
    177  1.1  gwr 	intersil_clock->clk_cmd_reg =
    178  1.1  gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
    179  1.1  gwr 	intersil_clear();
    180  1.1  gwr 
    181  1.1  gwr 	/*
    182  1.1  gwr 	 * Now we set all the desired bits
    183  1.1  gwr 	 * in the interrupt register, then
    184  1.1  gwr 	 * we turn the clock back on and
    185  1.1  gwr 	 * finally we can enable all interrupts.
    186  1.1  gwr 	 */
    187  1.1  gwr 	*interrupt_reg |= (interreg | on);		/* enable flip-flops */
    188  1.1  gwr 
    189  1.1  gwr 	if (enable)
    190  1.1  gwr 		intersil_clock->clk_cmd_reg =
    191  1.1  gwr 			intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    192  1.1  gwr 
    193  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    194  1.1  gwr }
    195  1.1  gwr 
    196  1.1  gwr /* Called very early by internal_configure. */
    197  1.1  gwr void clock_init()
    198  1.1  gwr {
    199  1.1  gwr 	clock_va = obio_find_mapping(OBIO_CLOCK2, sizeof(struct intersil7170));
    200  1.1  gwr 
    201  1.1  gwr 	if (!clock_va)
    202  1.1  gwr 		mon_panic("clock_init: clock_va\n");
    203  1.1  gwr 	if (!interrupt_reg)
    204  1.1  gwr 		mon_panic("clock_init: interrupt_reg\n");
    205  1.1  gwr 
    206  1.1  gwr 	/* Turn off clock interrupts until cpu_initclocks() */
    207  1.1  gwr 	/* isr_init() already set the interrupt reg to zero. */
    208  1.1  gwr 	intersil_clock->clk_cmd_reg =
    209  1.1  gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
    210  1.1  gwr 	intersil_clear();
    211  1.1  gwr }
    212  1.1  gwr 
    213  1.1  gwr /*
    214  1.1  gwr  * Set up the real-time clock (enable clock interrupts).
    215  1.1  gwr  * Leave stathz 0 since there is no secondary clock available.
    216  1.1  gwr  * Note that clock interrupts MUST STAY DISABLED until here.
    217  1.1  gwr  */
    218  1.1  gwr void
    219  1.1  gwr cpu_initclocks(void)
    220  1.1  gwr {
    221  1.1  gwr 	int s;
    222  1.1  gwr 
    223  1.1  gwr 	if (!intersil_clock)
    224  1.1  gwr 		panic("cpu_initclocks");
    225  1.1  gwr 	s = splhigh();
    226  1.1  gwr 
    227  1.1  gwr 	/* Install isr (in locore.s) that calls clock_intr(). */
    228  1.1  gwr 	isr_add_custom(5, (void*)_isr_clock);
    229  1.1  gwr 
    230  1.1  gwr 	/* Set the clock to interrupt 100 time per second. */
    231  1.1  gwr 	intersil_clock->clk_intr_reg = INTERSIL_INTER_CSECONDS;
    232  1.1  gwr 
    233  1.1  gwr 	*interrupt_reg |= IREG_CLOCK_ENAB_5;	/* enable clock */
    234  1.1  gwr 	intersil_clock->clk_cmd_reg =
    235  1.1  gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    236  1.1  gwr 	*interrupt_reg |= IREG_ALL_ENAB;		/* enable interrupts */
    237  1.1  gwr 	splx(s);
    238  1.1  gwr }
    239  1.1  gwr 
    240  1.1  gwr /*
    241  1.1  gwr  * This doesn't need to do anything, as we have only one timer and
    242  1.1  gwr  * profhz==stathz==hz.
    243  1.1  gwr  */
    244  1.1  gwr void
    245  1.1  gwr setstatclockrate(newhz)
    246  1.1  gwr 	int newhz;
    247  1.1  gwr {
    248  1.1  gwr 	/* nothing */
    249  1.1  gwr }
    250  1.1  gwr 
    251  1.1  gwr /*
    252  1.1  gwr  * This is is called by the "custom" interrupt handler
    253  1.1  gwr  * after it has reset the pending bit in the clock.
    254  1.1  gwr  */
    255  1.1  gwr void
    256  1.1  gwr clock_intr(cf)
    257  1.1  gwr 	struct clockframe cf;
    258  1.1  gwr {
    259  1.1  gwr 	register volatile struct intersil7170 *clk = intersil_clock;
    260  1.1  gwr 
    261  1.1  gwr 	/* Read the clock interrupt register. */
    262  1.1  gwr 	(void) clk->clk_intr_reg;
    263  1.1  gwr 	/* Pulse the clock intr. enable low. */
    264  1.1  gwr 	*interrupt_reg &= ~IREG_CLOCK_ENAB_5;
    265  1.1  gwr 	*interrupt_reg |=  IREG_CLOCK_ENAB_5;
    266  1.1  gwr 	/* Read the clock intr. reg AGAIN! */
    267  1.1  gwr 	(void) clk->clk_intr_reg;
    268  1.1  gwr 
    269  1.1  gwr 	hardclock(&cf);
    270  1.1  gwr }
    271  1.1  gwr 
    272  1.1  gwr /*
    273  1.1  gwr  * Return the best possible estimate of the time in the timeval
    274  1.1  gwr  * to which tvp points.  We do this by returning the current time
    275  1.1  gwr  * plus the amount of time since the last clock interrupt.
    276  1.1  gwr  *
    277  1.1  gwr  * Check that this time is no less than any previously-reported time,
    278  1.1  gwr  * which could happen around the time of a clock adjustment.  Just for
    279  1.1  gwr  * fun, we guarantee that the time will be greater than the value
    280  1.1  gwr  * obtained by a previous call.
    281  1.1  gwr  */
    282  1.1  gwr void
    283  1.1  gwr microtime(tvp)
    284  1.1  gwr 	register struct timeval *tvp;
    285  1.1  gwr {
    286  1.1  gwr 	int s = splhigh();
    287  1.1  gwr 	static struct timeval lasttime;
    288  1.1  gwr 
    289  1.1  gwr 	*tvp = time;
    290  1.1  gwr 	tvp->tv_usec++; 	/* XXX */
    291  1.1  gwr 	while (tvp->tv_usec > 1000000) {
    292  1.1  gwr 		tvp->tv_sec++;
    293  1.1  gwr 		tvp->tv_usec -= 1000000;
    294  1.1  gwr 	}
    295  1.1  gwr 	if (tvp->tv_sec == lasttime.tv_sec &&
    296  1.1  gwr 		tvp->tv_usec <= lasttime.tv_usec &&
    297  1.1  gwr 		(tvp->tv_usec = lasttime.tv_usec + 1) > 1000000)
    298  1.1  gwr 	{
    299  1.1  gwr 		tvp->tv_sec++;
    300  1.1  gwr 		tvp->tv_usec -= 1000000;
    301  1.1  gwr 	}
    302  1.1  gwr 	lasttime = *tvp;
    303  1.1  gwr 	splx(s);
    304  1.1  gwr }
    305  1.1  gwr 
    306  1.1  gwr 
    307  1.1  gwr /*
    308  1.1  gwr  * Machine-dependent clock routines.
    309  1.1  gwr  *
    310  1.1  gwr  * Inittodr initializes the time of day hardware which provides
    311  1.1  gwr  * date functions.
    312  1.1  gwr  *
    313  1.1  gwr  * Resettodr restores the time of day hardware after a time change.
    314  1.1  gwr  */
    315  1.1  gwr #define SECDAY		86400L
    316  1.1  gwr #define SECYR		(SECDAY * 365)
    317  1.1  gwr 
    318  1.1  gwr static long clk_get_secs(void);
    319  1.1  gwr static void clk_set_secs(long);
    320  1.1  gwr 
    321  1.1  gwr /*
    322  1.1  gwr  * Initialize the time of day register, based on the time base
    323  1.1  gwr  * which is, e.g. from a filesystem.
    324  1.1  gwr  */
    325  1.1  gwr void inittodr(fs_time)
    326  1.1  gwr 	time_t fs_time;
    327  1.1  gwr {
    328  1.1  gwr 	long diff, clk_time;
    329  1.1  gwr 	long long_ago = (5 * SECYR);
    330  1.1  gwr 	int clk_bad = 0;
    331  1.1  gwr 
    332  1.1  gwr 	/*
    333  1.1  gwr 	 * Sanity check time from file system.
    334  1.1  gwr 	 * If it is zero,assume filesystem time is just unknown
    335  1.1  gwr 	 * instead of preposterous.  Don't bark.
    336  1.1  gwr 	 */
    337  1.1  gwr 	if (fs_time < long_ago) {
    338  1.1  gwr 		/*
    339  1.1  gwr 		 * If fs_time is zero, assume filesystem time is just
    340  1.1  gwr 		 * unknown instead of preposterous.  Don't bark.
    341  1.1  gwr 		 */
    342  1.1  gwr 		if (fs_time != 0)
    343  1.1  gwr 			printf("WARNING: preposterous time in file system\n");
    344  1.1  gwr 		/* 1991/07/01  12:00:00 */
    345  1.1  gwr 		fs_time = 21*SECYR + 186*SECDAY + SECDAY/2;
    346  1.1  gwr 	}
    347  1.1  gwr 
    348  1.1  gwr 	clk_time = clk_get_secs();
    349  1.1  gwr 
    350  1.1  gwr 	/* Sanity check time from clock. */
    351  1.1  gwr 	if (clk_time < long_ago) {
    352  1.1  gwr 		printf("WARNING: bad date in battery clock");
    353  1.1  gwr 		clk_bad = 1;
    354  1.1  gwr 		clk_time = fs_time;
    355  1.1  gwr 	} else {
    356  1.1  gwr 		/* Does the clock time jive with the file system? */
    357  1.1  gwr 		diff = clk_time - fs_time;
    358  1.1  gwr 		if (diff < 0)
    359  1.1  gwr 			diff = -diff;
    360  1.1  gwr 		if (diff >= (SECDAY*2)) {
    361  1.1  gwr 			printf("WARNING: clock %s %d days",
    362  1.1  gwr 				   (clk_time < fs_time) ? "lost" : "gained",
    363  1.1  gwr 				   (int) (diff / SECDAY));
    364  1.1  gwr 			clk_bad = 1;
    365  1.1  gwr 		}
    366  1.1  gwr 	}
    367  1.1  gwr 	if (clk_bad)
    368  1.1  gwr 		printf(" -- CHECK AND RESET THE DATE!\n");
    369  1.1  gwr 	time.tv_sec = clk_time;
    370  1.1  gwr }
    371  1.1  gwr 
    372  1.1  gwr /*
    373  1.1  gwr  * Resettodr restores the time of day hardware after a time change.
    374  1.1  gwr  */
    375  1.1  gwr void resettodr()
    376  1.1  gwr {
    377  1.1  gwr 	clk_set_secs(time.tv_sec);
    378  1.1  gwr }
    379  1.1  gwr 
    380  1.1  gwr /*
    381  1.1  gwr  * Machine dependent base year:
    382  1.1  gwr  * Note: must be < 1970
    383  1.1  gwr  */
    384  1.1  gwr #define	CLOCK_BASE_YEAR	1968
    385  1.1  gwr 
    386  1.1  gwr 
    387  1.1  gwr /*
    388  1.1  gwr  * Routine to copy state into and out of the clock.
    389  1.1  gwr  * The clock registers have to be read or written
    390  1.1  gwr  * in sequential order (or so it appears). -gwr
    391  1.1  gwr  */
    392  1.1  gwr static void clk_get_dt(struct date_time *dt)
    393  1.1  gwr {
    394  1.1  gwr 	int s;
    395  1.1  gwr 	register volatile char *src, *dst;
    396  1.1  gwr 
    397  1.1  gwr 	src = (char *) &intersil_clock->counters;
    398  1.1  gwr 
    399  1.1  gwr 	s = splhigh();
    400  1.1  gwr 	intersil_clock->clk_cmd_reg =
    401  1.1  gwr 		intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    402  1.1  gwr 
    403  1.1  gwr 	dst = (char *) dt;
    404  1.1  gwr 	dt++;	/* end marker */
    405  1.1  gwr 	do {
    406  1.1  gwr 		*dst++ = *src++;
    407  1.1  gwr 	} while (dst < (char*)dt);
    408  1.1  gwr 
    409  1.1  gwr 	intersil_clock->clk_cmd_reg =
    410  1.1  gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    411  1.1  gwr 	splx(s);
    412  1.1  gwr }
    413  1.1  gwr 
    414  1.1  gwr static void clk_set_dt(struct date_time *dt)
    415  1.1  gwr {
    416  1.1  gwr 	int s;
    417  1.1  gwr 	register volatile char *src, *dst;
    418  1.1  gwr 
    419  1.1  gwr 	dst = (char *) &intersil_clock->counters;
    420  1.1  gwr 
    421  1.1  gwr 	s = splhigh();
    422  1.1  gwr 	intersil_clock->clk_cmd_reg =
    423  1.1  gwr 		intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    424  1.1  gwr 
    425  1.1  gwr 	src = (char *) dt;
    426  1.1  gwr 	dt++;	/* end marker */
    427  1.1  gwr 	do {
    428  1.1  gwr 		*dst++ = *src++;
    429  1.1  gwr 	} while (src < (char *)dt);
    430  1.1  gwr 
    431  1.1  gwr 	intersil_clock->clk_cmd_reg =
    432  1.1  gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    433  1.1  gwr 	splx(s);
    434  1.1  gwr }
    435  1.1  gwr 
    436  1.1  gwr 
    437  1.1  gwr 
    438  1.1  gwr /*
    440  1.1  gwr  * Generic routines to convert to or from a POSIX date
    441  1.1  gwr  * (seconds since 1/1/1970) and  yr/mo/day/hr/min/sec
    442  1.1  gwr  *
    443  1.1  gwr  * These are organized this way mostly to so the code
    444  1.1  gwr  * can easily be tested in an independent user program.
    445  1.1  gwr  * (These are derived from the hp300 code.)
    446  1.1  gwr  */
    447  1.1  gwr 
    448  1.1  gwr /* Traditional UNIX base year */
    449  1.1  gwr #define	POSIX_BASE_YEAR	1970
    450  1.1  gwr #define FEBRUARY	2
    451  1.1  gwr 
    452  1.1  gwr #define	leapyear(year)		((year) % 4 == 0)
    453  1.1  gwr #define	days_in_year(a) 	(leapyear(a) ? 366 : 365)
    454  1.1  gwr #define	days_in_month(a) 	(month_days[(a) - 1])
    455  1.1  gwr 
    456  1.1  gwr static int month_days[12] = {
    457  1.1  gwr 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    458  1.1  gwr };
    459  1.1  gwr 
    460  1.1  gwr void gmt_to_dt(long *tp, struct date_time *dt)
    461  1.1  gwr {
    462  1.1  gwr 	register int i;
    463  1.1  gwr 	register long days, secs;
    464  1.1  gwr 
    465  1.1  gwr 	days = *tp / SECDAY;
    466  1.1  gwr 	secs = *tp % SECDAY;
    467  1.1  gwr 
    468  1.1  gwr 	/* Hours, minutes, seconds are easy */
    469  1.1  gwr 	dt->dt_hour = secs / 3600;
    470  1.1  gwr 	secs = secs % 3600;
    471  1.1  gwr 	dt->dt_min  = secs / 60;
    472  1.1  gwr 	secs = secs % 60;
    473  1.1  gwr 	dt->dt_sec  = secs;
    474  1.1  gwr 
    475  1.1  gwr 	/* Day of week (Note: 1/1/1970 was a Thursday) */
    476  1.1  gwr 	dt->dt_dow = (days + 4) % 7;
    477  1.1  gwr 
    478  1.1  gwr 	/* Number of years in days */
    479  1.1  gwr 	i = POSIX_BASE_YEAR;
    480  1.1  gwr 	while (days >= days_in_year(i)) {
    481  1.1  gwr 		days -= days_in_year(i);
    482  1.1  gwr 		i++;
    483  1.1  gwr 	}
    484  1.1  gwr 	dt->dt_year = i - CLOCK_BASE_YEAR;
    485  1.1  gwr 
    486  1.1  gwr 	/* Number of months in days left */
    487  1.1  gwr 	if (leapyear(i))
    488  1.1  gwr 		days_in_month(FEBRUARY) = 29;
    489  1.1  gwr 	for (i = 1; days >= days_in_month(i); i++)
    490  1.1  gwr 		days -= days_in_month(i);
    491  1.1  gwr 	days_in_month(FEBRUARY) = 28;
    492  1.1  gwr 	dt->dt_month = i;
    493  1.1  gwr 
    494  1.1  gwr 	/* Days are what is left over (+1) from all that. */
    495  1.1  gwr 	dt->dt_day = days + 1;
    496  1.1  gwr }
    497  1.1  gwr 
    498  1.1  gwr void dt_to_gmt(struct date_time *dt, long *tp)
    499  1.1  gwr {
    500  1.1  gwr 	register int i;
    501  1.1  gwr 	register long tmp;
    502  1.1  gwr 	int year;
    503  1.1  gwr 
    504  1.1  gwr 	/*
    505  1.1  gwr 	 * Hours are different for some reason. Makes no sense really.
    506  1.1  gwr 	 */
    507  1.1  gwr 
    508  1.1  gwr 	tmp = 0;
    509  1.1  gwr 
    510  1.1  gwr 	if (dt->dt_hour >= 24) goto out;
    511  1.1  gwr 	if (dt->dt_day  >  31) goto out;
    512  1.1  gwr 	if (dt->dt_month > 12) goto out;
    513  1.1  gwr 
    514  1.1  gwr 	year = dt->dt_year + CLOCK_BASE_YEAR;
    515  1.1  gwr 
    516  1.1  gwr 	/*
    517  1.1  gwr 	 * Compute days since start of time
    518  1.1  gwr 	 * First from years, then from months.
    519  1.1  gwr 	 */
    520  1.1  gwr 	for (i = POSIX_BASE_YEAR; i < year; i++)
    521  1.1  gwr 		tmp += days_in_year(i);
    522  1.1  gwr 	if (leapyear(year) && dt->dt_month > FEBRUARY)
    523  1.1  gwr 		tmp++;
    524  1.1  gwr 
    525  1.1  gwr 	/* Months */
    526  1.1  gwr 	for (i = 1; i < dt->dt_month; i++)
    527  1.1  gwr 	  	tmp += days_in_month(i);
    528  1.1  gwr 	tmp += (dt->dt_day - 1);
    529  1.1  gwr 
    530  1.1  gwr 	/* Now do hours */
    531  1.1  gwr 	tmp = tmp * 24 + dt->dt_hour;
    532  1.1  gwr 
    533  1.1  gwr 	/* Now do minutes */
    534  1.1  gwr 	tmp = tmp * 60 + dt->dt_min;
    535  1.1  gwr 
    536  1.1  gwr 	/* Now do seconds */
    537  1.1  gwr 	tmp = tmp * 60 + dt->dt_sec;
    538  1.1  gwr 
    539  1.1  gwr  out:
    540  1.1  gwr 	*tp = tmp;
    541  1.1  gwr }
    542  1.1  gwr 
    543  1.1  gwr /*
    544  1.1  gwr  * Now routines to get and set clock as POSIX time.
    545  1.1  gwr  */
    546  1.1  gwr 
    547  1.1  gwr static long clk_get_secs()
    548  1.1  gwr {
    549  1.1  gwr 	struct date_time dt;
    550  1.1  gwr 	long gmt;
    551  1.1  gwr 
    552  1.1  gwr 	clk_get_dt(&dt);
    553  1.1  gwr 	dt_to_gmt(&dt, &gmt);
    554  1.1  gwr 	return (gmt);
    555  1.1  gwr }
    556  1.1  gwr 
    557  1.1  gwr static void clk_set_secs(long secs)
    558  1.1  gwr {
    559  1.1  gwr 	struct date_time dt;
    560  1.1  gwr 	long gmt;
    561  1.1  gwr 
    562  1.1  gwr 	gmt = secs;
    563  1.1  gwr 	gmt_to_dt(&gmt, &dt);
    564  1.1  gwr 	clk_set_dt(&dt);
    565  1.1  gwr }
    566  1.1  gwr 
    567  1.1  gwr 
    568  1.1  gwr #ifdef	DEBUG
    569  1.1  gwr /* Call this from DDB or whatever... */
    570  1.1  gwr int clkdebug()
    571  1.1  gwr {
    572  1.1  gwr 	struct date_time dt;
    573  1.1  gwr 	long gmt;
    574  1.1  gwr 	long *lp;
    575  1.1  gwr 
    576  1.1  gwr 	bzero((char*)&dt, sizeof(dt));
    577  1.1  gwr 	clk_get_dt(&dt);
    578  1.1  gwr 	lp = (long*)&dt;
    579  1.1  gwr 	printf("clkdebug: dt=[%x,%x]\n", lp[0], lp[1]);
    580  1.1  gwr 
    581  1.1  gwr 	dt_to_gmt(&dt, &gmt);
    582  1.1  gwr 	printf("clkdebug: gmt=%x\n", gmt);
    583  1.1  gwr }
    584           #endif
    585