Home | History | Annotate | Line # | Download | only in sun3x
clock.c revision 1.24
      1  1.24    lukem /*	$NetBSD: clock.c,v 1.24 2003/07/15 03:36:20 lukem 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.10      gwr  * Machine-dependent clock routines.  Sun3X machines may have
     48  1.10      gwr  * either the Mostek 48T02 or the Intersil 7170 clock.
     49  1.10      gwr  *
     50  1.10      gwr  * It is tricky to determine which you have, because there is
     51  1.10      gwr  * always something responding at the address where the Mostek
     52  1.10      gwr  * clock might be found: either a Mostek or plain-old EEPROM.
     53  1.10      gwr  * Therefore, we cheat.  If we find an Intersil clock, assume
     54  1.10      gwr  * that what responds at the end of the EEPROM space is just
     55  1.10      gwr  * plain-old EEPROM (not a Mostek clock).  Worse, there are
     56  1.10      gwr  * H/W problems with probing for an Intersil on the 3/80, so
     57  1.10      gwr  * on that machine we "know" there is a Mostek clock.
     58  1.10      gwr  *
     59  1.10      gwr  * Note that the probing algorithm described above requires
     60  1.10      gwr  * that we probe the intersil before we probe the mostek!
     61   1.1      gwr  */
     62  1.24    lukem 
     63  1.24    lukem #include <sys/cdefs.h>
     64  1.24    lukem __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.24 2003/07/15 03:36:20 lukem Exp $");
     65   1.1      gwr 
     66   1.1      gwr #include <sys/param.h>
     67   1.1      gwr #include <sys/systm.h>
     68   1.1      gwr #include <sys/time.h>
     69   1.1      gwr #include <sys/kernel.h>
     70   1.1      gwr #include <sys/device.h>
     71   1.1      gwr 
     72   1.9      gwr #include <m68k/asm_single.h>
     73   1.9      gwr 
     74   1.1      gwr #include <machine/autoconf.h>
     75   1.1      gwr #include <machine/cpu.h>
     76  1.10      gwr #include <machine/idprom.h>
     77  1.10      gwr #include <machine/leds.h>
     78  1.10      gwr 
     79  1.15      gwr #include <dev/clock_subr.h>
     80  1.15      gwr #include <dev/ic/intersil7170.h>
     81  1.15      gwr 
     82  1.15      gwr #include <sun3/sun3/machdep.h>
     83  1.10      gwr #include <sun3/sun3/interreg.h>
     84   1.1      gwr 
     85  1.15      gwr #include <sun3/sun3x/mk48t02.h>
     86   1.8      gwr 
     87  1.10      gwr #define SUN3_470	Yes
     88   1.1      gwr 
     89   1.1      gwr #define	CLOCK_PRI	5
     90   1.9      gwr #define IREG_CLK_BITS	(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5)
     91   1.1      gwr 
     92  1.10      gwr /*
     93  1.10      gwr  * Only one of these two variables should be non-zero after
     94  1.10      gwr  * autoconfiguration determines which clock we have.
     95  1.10      gwr  */
     96  1.10      gwr static volatile void *intersil_va;
     97  1.10      gwr static volatile void *mostek_clk_va;
     98  1.10      gwr 
     99   1.1      gwr void _isr_clock __P((void));	/* in locore.s */
    100   1.1      gwr void clock_intr __P((struct clockframe));
    101   1.1      gwr 
    102   1.1      gwr 
    103   1.1      gwr static int  clock_match __P((struct device *, struct cfdata *, void *args));
    104   1.1      gwr static void clock_attach __P((struct device *, struct device *, void *));
    105   1.1      gwr 
    106  1.22  thorpej CFATTACH_DECL(clock, sizeof(struct device),
    107  1.23  thorpej     clock_match, clock_attach, NULL, NULL);
    108   1.1      gwr 
    109  1.10      gwr #ifdef	SUN3_470
    110  1.10      gwr 
    111  1.10      gwr #define intersil_clock ((volatile struct intersil7170 *) intersil_va)
    112  1.10      gwr 
    113  1.10      gwr #define intersil_command(run, interrupt) \
    114  1.10      gwr 	(run | interrupt | INTERSIL_CMD_FREQ_32K | INTERSIL_CMD_24HR_MODE | \
    115  1.10      gwr 	 INTERSIL_CMD_NORMAL_MODE)
    116  1.10      gwr 
    117  1.10      gwr #define intersil_clear() (void)intersil_clock->clk_intr_reg
    118  1.10      gwr 
    119  1.10      gwr static int  oclock_match __P((struct device *, struct cfdata *, void *args));
    120  1.10      gwr static void oclock_attach __P((struct device *, struct device *, void *));
    121  1.10      gwr 
    122  1.22  thorpej CFATTACH_DECL(oclock, sizeof(struct device),
    123  1.23  thorpej     oclock_match, oclock_attach, NULL, NULL);
    124  1.10      gwr 
    125   1.9      gwr /*
    126  1.10      gwr  * Is there an intersil clock?
    127   1.9      gwr  */
    128  1.10      gwr static int
    129  1.10      gwr oclock_match(parent, cf, args)
    130  1.10      gwr     struct device *parent;
    131  1.10      gwr 	struct cfdata *cf;
    132  1.10      gwr     void *args;
    133  1.10      gwr {
    134  1.10      gwr 	struct confargs *ca = args;
    135  1.10      gwr 
    136  1.10      gwr 	/* This driver only supports one unit. */
    137  1.18  tsutsui 	if (intersil_va)
    138  1.10      gwr 		return (0);
    139  1.10      gwr 
    140  1.10      gwr 	/*
    141  1.10      gwr 	 * The 3/80 can not probe the Intersil absent,
    142  1.10      gwr 	 * but it never has one, so "just say no."
    143  1.10      gwr 	 */
    144  1.10      gwr 	if (cpu_machine_id == SUN3X_MACH_80)
    145  1.10      gwr 		return (0);
    146  1.10      gwr 
    147  1.10      gwr 	/* OK, really probe for the Intersil. */
    148  1.10      gwr 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
    149  1.10      gwr 		return (0);
    150  1.10      gwr 
    151  1.16      gwr 	/* Default interrupt priority. */
    152  1.16      gwr 	if (ca->ca_intpri == -1)
    153  1.16      gwr 		ca->ca_intpri = CLOCK_PRI;
    154  1.16      gwr 
    155  1.10      gwr 	return (1);
    156  1.10      gwr }
    157  1.10      gwr 
    158  1.10      gwr /*
    159  1.10      gwr  * Attach the intersil clock.
    160  1.10      gwr  */
    161  1.10      gwr static void
    162  1.10      gwr oclock_attach(parent, self, args)
    163  1.10      gwr 	struct device *parent;
    164  1.10      gwr 	struct device *self;
    165  1.10      gwr 	void *args;
    166   1.9      gwr {
    167  1.10      gwr 	struct confargs *ca = args;
    168  1.10      gwr 	caddr_t va;
    169  1.10      gwr 
    170  1.10      gwr 	printf("\n");
    171  1.10      gwr 
    172  1.10      gwr 	/* Get a mapping for it. */
    173  1.15      gwr 	va = bus_mapin(ca->ca_bustype,
    174  1.15      gwr 	    ca->ca_paddr, sizeof(struct intersil7170));
    175  1.10      gwr 	if (!va)
    176  1.10      gwr 		panic("oclock_attach");
    177  1.10      gwr 	intersil_va = va;
    178  1.10      gwr 
    179  1.10      gwr #ifdef	DIAGNOSTIC
    180  1.10      gwr 	/* Verify correct probe order... */
    181  1.10      gwr 	if (mostek_clk_va) {
    182  1.10      gwr 		mostek_clk_va = 0;
    183  1.10      gwr 		printf("%s: warning - mostek found also!\n",
    184  1.10      gwr 			   self->dv_xname);
    185   1.9      gwr 	}
    186  1.10      gwr #endif
    187  1.10      gwr 
    188  1.10      gwr 	/*
    189  1.10      gwr 	 * Set the clock to the correct interrupt rate, but
    190  1.10      gwr 	 * do not enable the interrupt until cpu_initclocks.
    191  1.10      gwr 	 * XXX: Actually, the interrupt_reg should be zero
    192  1.10      gwr 	 * at this point, so the clock interrupts should not
    193  1.10      gwr 	 * affect us, but we need to set the rate...
    194  1.10      gwr 	 */
    195  1.10      gwr 	intersil_clock->clk_cmd_reg =
    196  1.10      gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
    197  1.10      gwr 	intersil_clear();
    198  1.10      gwr 
    199  1.10      gwr 	/* Set the clock to 100 Hz, but do not enable it yet. */
    200  1.10      gwr 	intersil_clock->clk_intr_reg = INTERSIL_INTER_CSECONDS;
    201  1.10      gwr 
    202  1.10      gwr 	/*
    203  1.10      gwr 	 * Can not hook up the ISR until cpu_initclocks()
    204  1.10      gwr 	 * because hardclock is not ready until then.
    205  1.10      gwr 	 * For now, the handler is _isr_autovec(), which
    206  1.10      gwr 	 * will complain if it gets clock interrupts.
    207  1.10      gwr 	 */
    208   1.9      gwr }
    209  1.10      gwr #endif	/* SUN3_470 */
    210  1.10      gwr 
    211   1.9      gwr 
    212   1.1      gwr /*
    213  1.10      gwr  * Is there a Mostek clock?  Hard to tell...
    214  1.10      gwr  * (See comment at top of this file.)
    215   1.1      gwr  */
    216   1.1      gwr static int
    217   1.1      gwr clock_match(parent, cf, args)
    218   1.1      gwr     struct device *parent;
    219   1.1      gwr 	struct cfdata *cf;
    220   1.1      gwr     void *args;
    221   1.1      gwr {
    222  1.16      gwr 	struct confargs *ca = args;
    223   1.1      gwr 
    224   1.1      gwr 	/* This driver only supports one unit. */
    225  1.18  tsutsui 	if (mostek_clk_va)
    226  1.11      gwr 		return (0);
    227  1.11      gwr 
    228  1.10      gwr 	/* If intersil was found, use that. */
    229  1.10      gwr 	if (intersil_va)
    230   1.1      gwr 		return (0);
    231  1.16      gwr 	/* Else assume a Mostek is there... */
    232  1.16      gwr 
    233  1.16      gwr 	/* Default interrupt priority. */
    234  1.16      gwr 	if (ca->ca_intpri == -1)
    235  1.16      gwr 		ca->ca_intpri = CLOCK_PRI;
    236   1.1      gwr 
    237   1.1      gwr 	return (1);
    238   1.1      gwr }
    239   1.1      gwr 
    240  1.10      gwr /*
    241  1.10      gwr  * Attach the mostek clock.
    242  1.10      gwr  */
    243   1.1      gwr static void
    244   1.1      gwr clock_attach(parent, self, args)
    245   1.1      gwr 	struct device *parent;
    246   1.1      gwr 	struct device *self;
    247   1.1      gwr 	void *args;
    248   1.1      gwr {
    249  1.10      gwr 	struct confargs *ca = args;
    250  1.10      gwr 	caddr_t va;
    251   1.1      gwr 
    252   1.1      gwr 	printf("\n");
    253   1.1      gwr 
    254  1.10      gwr 	/* Get a mapping for it. */
    255  1.15      gwr 	va = bus_mapin(ca->ca_bustype,
    256  1.15      gwr 	    ca->ca_paddr, sizeof(struct mostek_clkreg));
    257  1.10      gwr 	if (!va)
    258  1.10      gwr 		panic("clock_attach");
    259  1.10      gwr 	mostek_clk_va = va;
    260  1.13      gwr 
    261   1.1      gwr 	/*
    262   1.1      gwr 	 * Can not hook up the ISR until cpu_initclocks()
    263   1.1      gwr 	 * because hardclock is not ready until then.
    264   1.1      gwr 	 * For now, the handler is _isr_autovec(), which
    265   1.1      gwr 	 * will complain if it gets clock interrupts.
    266   1.1      gwr 	 */
    267   1.1      gwr }
    268   1.1      gwr 
    269   1.1      gwr /*
    270   1.1      gwr  * Set and/or clear the desired clock bits in the interrupt
    271   1.1      gwr  * register.  We have to be extremely careful that we do it
    272   1.1      gwr  * in such a manner that we don't get ourselves lost.
    273   1.9      gwr  * XXX:  Watch out!  It's really easy to break this!
    274   1.1      gwr  */
    275   1.1      gwr void
    276   1.9      gwr set_clk_mode(on, off, enable_clk)
    277   1.1      gwr 	u_char on, off;
    278   1.9      gwr 	int enable_clk;
    279   1.1      gwr {
    280  1.19  tsutsui 	u_char interreg;
    281   1.1      gwr 
    282   1.9      gwr 	/*
    283   1.9      gwr 	 * If we have not yet mapped the register,
    284   1.9      gwr 	 * then we do not want to do any of this...
    285   1.9      gwr 	 */
    286   1.5      gwr 	if (!interrupt_reg)
    287   1.4      gwr 		return;
    288   1.4      gwr 
    289   1.9      gwr #ifdef	DIAGNOSTIC
    290   1.9      gwr 	/* Assertion: were are at splhigh! */
    291   1.9      gwr 	if ((getsr() & PSL_IPL) < PSL_IPL7)
    292   1.9      gwr 		panic("set_clk_mode: bad ipl");
    293   1.9      gwr #endif
    294   1.1      gwr 
    295   1.1      gwr 	/*
    296   1.1      gwr 	 * make sure that we are only playing w/
    297   1.1      gwr 	 * clock interrupt register bits
    298   1.1      gwr 	 */
    299   1.9      gwr 	on  &= IREG_CLK_BITS;
    300   1.9      gwr 	off &= IREG_CLK_BITS;
    301   1.1      gwr 
    302   1.9      gwr 	/* First, turn off the "master" enable bit. */
    303   1.9      gwr 	single_inst_bclr_b(*interrupt_reg, IREG_ALL_ENAB);
    304   1.1      gwr 
    305   1.1      gwr 	/*
    306   1.9      gwr 	 * Save the current interrupt register clock bits,
    307   1.9      gwr 	 * and turn off/on the requested bits in the copy.
    308   1.1      gwr 	 */
    309   1.9      gwr 	interreg = *interrupt_reg & IREG_CLK_BITS;
    310   1.9      gwr 	interreg &= ~off;
    311   1.9      gwr 	interreg |= on;
    312   1.9      gwr 
    313   1.9      gwr 	/* Clear the CLK5 and CLK7 bits to clear the flip-flops. */
    314   1.9      gwr 	single_inst_bclr_b(*interrupt_reg, IREG_CLK_BITS);
    315   1.9      gwr 
    316   1.9      gwr #ifdef	SUN3_470
    317   1.9      gwr 	if (intersil_va) {
    318   1.9      gwr 		/*
    319   1.9      gwr 		 * Then disable clock interrupts, and read the clock's
    320   1.9      gwr 		 * interrupt register to clear any pending signals there.
    321   1.9      gwr 		 */
    322   1.9      gwr 		intersil_clock->clk_cmd_reg =
    323   1.9      gwr 			intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
    324   1.9      gwr 		intersil_clear();
    325   1.9      gwr 	}
    326   1.9      gwr #endif	/* SUN3_470 */
    327   1.3      gwr 
    328   1.9      gwr 	/* Set the requested bits in the interrupt register. */
    329   1.9      gwr 	single_inst_bset_b(*interrupt_reg, interreg);
    330   1.1      gwr 
    331   1.9      gwr #ifdef	SUN3_470
    332   1.9      gwr 	/* Turn the clock back on (maybe) */
    333   1.9      gwr 	if (intersil_va && enable_clk)
    334   1.9      gwr 		intersil_clock->clk_cmd_reg =
    335   1.9      gwr 			intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    336   1.9      gwr #endif	/* SUN3_470 */
    337   1.1      gwr 
    338   1.9      gwr 	/* Finally, turn the "master" enable back on. */
    339   1.9      gwr 	single_inst_bset_b(*interrupt_reg, IREG_ALL_ENAB);
    340   1.1      gwr }
    341   1.1      gwr 
    342   1.1      gwr /*
    343   1.1      gwr  * Set up the real-time clock (enable clock interrupts).
    344   1.1      gwr  * Leave stathz 0 since there is no secondary clock available.
    345   1.1      gwr  * Note that clock interrupts MUST STAY DISABLED until here.
    346   1.1      gwr  */
    347   1.1      gwr void
    348   1.1      gwr cpu_initclocks(void)
    349   1.1      gwr {
    350   1.1      gwr 	int s;
    351   1.1      gwr 
    352   1.1      gwr 	s = splhigh();
    353   1.1      gwr 
    354   1.1      gwr 	/* Install isr (in locore.s) that calls clock_intr(). */
    355  1.16      gwr 	isr_add_custom(CLOCK_PRI, (void*)_isr_clock);
    356   1.1      gwr 
    357   1.9      gwr 	/* Now enable the clock at level 5 in the interrupt reg. */
    358   1.9      gwr 	set_clk_mode(IREG_CLOCK_ENAB_5, 0, 1);
    359   1.3      gwr 
    360   1.1      gwr 	splx(s);
    361   1.1      gwr }
    362   1.1      gwr 
    363   1.1      gwr /*
    364   1.1      gwr  * This doesn't need to do anything, as we have only one timer and
    365   1.1      gwr  * profhz==stathz==hz.
    366   1.1      gwr  */
    367   1.1      gwr void
    368   1.1      gwr setstatclockrate(newhz)
    369   1.1      gwr 	int newhz;
    370   1.1      gwr {
    371   1.1      gwr 	/* nothing */
    372   1.1      gwr }
    373   1.1      gwr 
    374   1.1      gwr /*
    375  1.10      gwr  * Clock interrupt handler (for both Intersil and Mostek).
    376  1.10      gwr  * XXX - Is it worth the trouble to save a few cycles here
    377  1.10      gwr  * by making two separate interrupt handlers?
    378  1.10      gwr  *
    379   1.3      gwr  * This is is called by the "custom" interrupt handler.
    380   1.9      gwr  * Note that we can get ZS interrupts while this runs,
    381   1.9      gwr  * and zshard may touch the interrupt_reg, so we must
    382   1.9      gwr  * be careful to use the single_inst_* macros to modify
    383   1.9      gwr  * the interrupt register atomically.
    384   1.1      gwr  */
    385   1.1      gwr void
    386   1.1      gwr clock_intr(cf)
    387   1.1      gwr 	struct clockframe cf;
    388   1.1      gwr {
    389  1.10      gwr 	extern char _Idle[];	/* locore.s */
    390  1.10      gwr 
    391  1.10      gwr #ifdef	SUN3_470
    392  1.10      gwr 	if (intersil_va) {
    393  1.10      gwr 		/* Read the clock interrupt register. */
    394  1.10      gwr 		intersil_clear();
    395  1.10      gwr 	}
    396  1.10      gwr #endif	/* SUN3_470 */
    397   1.1      gwr 
    398   1.1      gwr 	/* Pulse the clock intr. enable low. */
    399   1.9      gwr 	single_inst_bclr_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
    400   1.9      gwr 	single_inst_bset_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
    401   1.1      gwr 
    402  1.10      gwr #ifdef	SUN3_470
    403  1.10      gwr 	if (intersil_va) {
    404  1.10      gwr 		/* Read the clock intr. reg. AGAIN! */
    405  1.10      gwr 		intersil_clear();
    406  1.10      gwr 	}
    407  1.10      gwr #endif	/* SUN3_470 */
    408  1.13      gwr 
    409  1.13      gwr 	/* Entertainment! */
    410  1.13      gwr 	if (cf.cf_pc == (long)_Idle)
    411  1.13      gwr 		leds_intr();
    412  1.10      gwr 
    413   1.9      gwr 	/* Call common clock interrupt handler. */
    414   1.1      gwr 	hardclock(&cf);
    415   1.1      gwr }
    416   1.9      gwr 
    417   1.1      gwr 
    418   1.1      gwr /*
    419   1.1      gwr  * Return the best possible estimate of the time in the timeval
    420   1.1      gwr  * to which tvp points.  We do this by returning the current time
    421   1.1      gwr  * plus the amount of time since the last clock interrupt.
    422   1.1      gwr  *
    423   1.1      gwr  * Check that this time is no less than any previously-reported time,
    424   1.1      gwr  * which could happen around the time of a clock adjustment.  Just for
    425   1.1      gwr  * fun, we guarantee that the time will be greater than the value
    426   1.1      gwr  * obtained by a previous call.
    427   1.1      gwr  */
    428   1.1      gwr void
    429   1.1      gwr microtime(tvp)
    430  1.19  tsutsui 	struct timeval *tvp;
    431   1.1      gwr {
    432   1.1      gwr 	int s = splhigh();
    433   1.1      gwr 	static struct timeval lasttime;
    434   1.1      gwr 
    435   1.1      gwr 	*tvp = time;
    436   1.1      gwr 	tvp->tv_usec++; 	/* XXX */
    437  1.17  msaitoh 	while (tvp->tv_usec >= 1000000) {
    438   1.1      gwr 		tvp->tv_sec++;
    439   1.1      gwr 		tvp->tv_usec -= 1000000;
    440   1.1      gwr 	}
    441   1.1      gwr 	if (tvp->tv_sec == lasttime.tv_sec &&
    442   1.1      gwr 		tvp->tv_usec <= lasttime.tv_usec &&
    443  1.17  msaitoh 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    444   1.1      gwr 	{
    445   1.1      gwr 		tvp->tv_sec++;
    446   1.1      gwr 		tvp->tv_usec -= 1000000;
    447   1.1      gwr 	}
    448   1.1      gwr 	lasttime = *tvp;
    449   1.1      gwr 	splx(s);
    450   1.1      gwr }
    451   1.1      gwr 
    452   1.1      gwr 
    453   1.1      gwr /*
    454   1.1      gwr  * Machine-dependent clock routines.
    455   1.1      gwr  *
    456   1.1      gwr  * Inittodr initializes the time of day hardware which provides
    457   1.1      gwr  * date functions.
    458   1.1      gwr  *
    459   1.1      gwr  * Resettodr restores the time of day hardware after a time change.
    460   1.1      gwr  */
    461   1.1      gwr 
    462  1.10      gwr static long clk_get_secs __P((void));
    463  1.10      gwr static void clk_set_secs __P((long));
    464   1.1      gwr 
    465   1.1      gwr /*
    466   1.1      gwr  * Initialize the time of day register, based on the time base
    467   1.1      gwr  * which is, e.g. from a filesystem.
    468   1.1      gwr  */
    469   1.1      gwr void inittodr(fs_time)
    470   1.1      gwr 	time_t fs_time;
    471   1.1      gwr {
    472   1.1      gwr 	long diff, clk_time;
    473   1.1      gwr 	long long_ago = (5 * SECYR);
    474   1.1      gwr 	int clk_bad = 0;
    475   1.1      gwr 
    476   1.1      gwr 	/*
    477   1.1      gwr 	 * Sanity check time from file system.
    478   1.1      gwr 	 * If it is zero,assume filesystem time is just unknown
    479   1.1      gwr 	 * instead of preposterous.  Don't bark.
    480   1.1      gwr 	 */
    481   1.1      gwr 	if (fs_time < long_ago) {
    482   1.1      gwr 		/*
    483   1.1      gwr 		 * If fs_time is zero, assume filesystem time is just
    484   1.1      gwr 		 * unknown instead of preposterous.  Don't bark.
    485   1.1      gwr 		 */
    486   1.1      gwr 		if (fs_time != 0)
    487   1.1      gwr 			printf("WARNING: preposterous time in file system\n");
    488   1.1      gwr 		/* 1991/07/01  12:00:00 */
    489   1.1      gwr 		fs_time = 21*SECYR + 186*SECDAY + SECDAY/2;
    490   1.1      gwr 	}
    491   1.1      gwr 
    492   1.1      gwr 	clk_time = clk_get_secs();
    493   1.1      gwr 
    494   1.1      gwr 	/* Sanity check time from clock. */
    495   1.1      gwr 	if (clk_time < long_ago) {
    496   1.1      gwr 		printf("WARNING: bad date in battery clock");
    497   1.1      gwr 		clk_bad = 1;
    498   1.1      gwr 		clk_time = fs_time;
    499   1.1      gwr 	} else {
    500   1.1      gwr 		/* Does the clock time jive with the file system? */
    501   1.1      gwr 		diff = clk_time - fs_time;
    502   1.1      gwr 		if (diff < 0)
    503   1.1      gwr 			diff = -diff;
    504   1.1      gwr 		if (diff >= (SECDAY*2)) {
    505   1.1      gwr 			printf("WARNING: clock %s %d days",
    506   1.1      gwr 				   (clk_time < fs_time) ? "lost" : "gained",
    507   1.1      gwr 				   (int) (diff / SECDAY));
    508   1.1      gwr 			clk_bad = 1;
    509   1.1      gwr 		}
    510   1.1      gwr 	}
    511   1.1      gwr 	if (clk_bad)
    512   1.1      gwr 		printf(" -- CHECK AND RESET THE DATE!\n");
    513   1.1      gwr 	time.tv_sec = clk_time;
    514   1.1      gwr }
    515   1.1      gwr 
    516   1.1      gwr /*
    517   1.1      gwr  * Resettodr restores the time of day hardware after a time change.
    518   1.1      gwr  */
    519   1.1      gwr void resettodr()
    520   1.1      gwr {
    521   1.1      gwr 	clk_set_secs(time.tv_sec);
    522   1.1      gwr }
    523   1.1      gwr 
    524   1.1      gwr 
    525   1.1      gwr /*
    526  1.10      gwr  * Now routines to get and set clock as POSIX time.
    527  1.10      gwr  * Our clock keeps "years since 1/1/1968".
    528  1.10      gwr  */
    529  1.10      gwr #define	CLOCK_BASE_YEAR 1968
    530  1.10      gwr #ifdef	SUN3_470
    531  1.10      gwr static void intersil_get_dt __P((struct clock_ymdhms *));
    532  1.10      gwr static void intersil_set_dt __P((struct clock_ymdhms *));
    533  1.10      gwr #endif /* SUN3_470 */
    534  1.10      gwr static void mostek_get_dt __P((struct clock_ymdhms *));
    535  1.10      gwr static void mostek_set_dt __P((struct clock_ymdhms *));
    536  1.10      gwr 
    537  1.10      gwr static long
    538  1.10      gwr clk_get_secs()
    539  1.10      gwr {
    540  1.10      gwr 	struct clock_ymdhms dt;
    541  1.10      gwr 	long secs;
    542  1.10      gwr 
    543  1.20  tsutsui 	memset(&dt, 0, sizeof(dt));
    544  1.10      gwr 
    545  1.10      gwr #ifdef	SUN3_470
    546  1.10      gwr 	if (intersil_va)
    547  1.10      gwr 		intersil_get_dt(&dt);
    548  1.10      gwr #endif	/* SUN3_470 */
    549  1.10      gwr 	if (mostek_clk_va) {
    550  1.10      gwr 		/* Read the Mostek. */
    551  1.10      gwr 		mostek_get_dt(&dt);
    552  1.10      gwr 		/* Convert BCD values to binary. */
    553  1.10      gwr 		dt.dt_sec  = FROMBCD(dt.dt_sec);
    554  1.10      gwr 		dt.dt_min  = FROMBCD(dt.dt_min);
    555  1.10      gwr 		dt.dt_hour = FROMBCD(dt.dt_hour);
    556  1.10      gwr 		dt.dt_day  = FROMBCD(dt.dt_day);
    557  1.10      gwr 		dt.dt_mon  = FROMBCD(dt.dt_mon);
    558  1.10      gwr 		dt.dt_year = FROMBCD(dt.dt_year);
    559  1.10      gwr 	}
    560  1.10      gwr 
    561  1.10      gwr 	if ((dt.dt_hour > 24) ||
    562  1.10      gwr 		(dt.dt_day  > 31) ||
    563  1.10      gwr 		(dt.dt_mon  > 12))
    564  1.10      gwr 		return (0);
    565  1.10      gwr 
    566  1.10      gwr 	dt.dt_year += CLOCK_BASE_YEAR;
    567  1.10      gwr 	secs = clock_ymdhms_to_secs(&dt);
    568  1.10      gwr 	return (secs);
    569  1.10      gwr }
    570  1.10      gwr 
    571  1.10      gwr static void
    572  1.10      gwr clk_set_secs(secs)
    573  1.10      gwr 	long secs;
    574  1.10      gwr {
    575  1.10      gwr 	struct clock_ymdhms dt;
    576  1.10      gwr 
    577  1.10      gwr 	clock_secs_to_ymdhms(secs, &dt);
    578  1.10      gwr 	dt.dt_year -= CLOCK_BASE_YEAR;
    579  1.10      gwr 
    580  1.10      gwr #ifdef	SUN3_470
    581  1.10      gwr 	if (intersil_va)
    582  1.10      gwr 		intersil_set_dt(&dt);
    583  1.10      gwr #endif	/* SUN3_470 */
    584  1.10      gwr 
    585  1.10      gwr 	if (mostek_clk_va) {
    586  1.10      gwr 		/* Convert binary values to BCD. */
    587  1.10      gwr 		dt.dt_sec  = TOBCD(dt.dt_sec);
    588  1.10      gwr 		dt.dt_min  = TOBCD(dt.dt_min);
    589  1.10      gwr 		dt.dt_hour = TOBCD(dt.dt_hour);
    590  1.10      gwr 		dt.dt_day  = TOBCD(dt.dt_day);
    591  1.10      gwr 		dt.dt_mon  = TOBCD(dt.dt_mon);
    592  1.10      gwr 		dt.dt_year = TOBCD(dt.dt_year);
    593  1.10      gwr 		/* Write the Mostek. */
    594  1.10      gwr 		mostek_set_dt(&dt);
    595  1.10      gwr 	}
    596  1.10      gwr }
    597  1.10      gwr 
    598  1.10      gwr #ifdef	SUN3_470
    599  1.10      gwr 
    600  1.10      gwr /*
    601  1.10      gwr  * Routines to copy state into and out of the clock.
    602  1.10      gwr  * The intersil registers have to be read or written
    603  1.10      gwr  * in sequential order (or so it appears). -gwr
    604  1.10      gwr  */
    605  1.10      gwr static void
    606  1.10      gwr intersil_get_dt(struct clock_ymdhms *dt)
    607  1.10      gwr {
    608  1.10      gwr 	volatile struct intersil_dt *isdt;
    609  1.10      gwr 	int s;
    610  1.10      gwr 
    611  1.10      gwr 	isdt = &intersil_clock->counters;
    612  1.10      gwr 	s = splhigh();
    613  1.10      gwr 
    614  1.10      gwr 	/* Enable read (stop time) */
    615  1.10      gwr 	intersil_clock->clk_cmd_reg =
    616  1.10      gwr 		intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    617  1.10      gwr 
    618  1.10      gwr 	/* Copy the info.  Careful about the order! */
    619  1.10      gwr 	dt->dt_sec  = isdt->dt_csec;  /* throw-away */
    620  1.10      gwr 	dt->dt_hour = isdt->dt_hour;
    621  1.10      gwr 	dt->dt_min  = isdt->dt_min;
    622  1.10      gwr 	dt->dt_sec  = isdt->dt_sec;
    623  1.10      gwr 	dt->dt_mon  = isdt->dt_month;
    624  1.10      gwr 	dt->dt_day  = isdt->dt_day;
    625  1.10      gwr 	dt->dt_year = isdt->dt_year;
    626  1.10      gwr 	dt->dt_wday = isdt->dt_dow;
    627  1.10      gwr 
    628  1.10      gwr 	/* Done reading (time wears on) */
    629  1.10      gwr 	intersil_clock->clk_cmd_reg =
    630  1.10      gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    631  1.10      gwr 	splx(s);
    632  1.10      gwr }
    633  1.10      gwr 
    634  1.10      gwr static void
    635  1.10      gwr intersil_set_dt(struct clock_ymdhms *dt)
    636  1.10      gwr {
    637  1.10      gwr 	volatile struct intersil_dt *isdt;
    638  1.10      gwr 	int s;
    639  1.10      gwr 
    640  1.10      gwr 	isdt = &intersil_clock->counters;
    641  1.10      gwr 	s = splhigh();
    642  1.10      gwr 
    643  1.10      gwr 	/* Enable write (stop time) */
    644  1.10      gwr 	intersil_clock->clk_cmd_reg =
    645  1.10      gwr 		intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    646  1.10      gwr 
    647  1.10      gwr 	/* Copy the info.  Careful about the order! */
    648  1.10      gwr 	isdt->dt_csec = 0;
    649  1.10      gwr 	isdt->dt_hour = dt->dt_hour;
    650  1.10      gwr 	isdt->dt_min  = dt->dt_min;
    651  1.10      gwr 	isdt->dt_sec  = dt->dt_sec;
    652  1.10      gwr 	isdt->dt_month= dt->dt_mon;
    653  1.10      gwr 	isdt->dt_day  = dt->dt_day;
    654  1.10      gwr 	isdt->dt_year = dt->dt_year;
    655  1.10      gwr 	isdt->dt_dow  = dt->dt_wday;
    656  1.10      gwr 
    657  1.10      gwr 	/* Done writing (time wears on) */
    658  1.10      gwr 	intersil_clock->clk_cmd_reg =
    659  1.10      gwr 		intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    660  1.10      gwr 	splx(s);
    661  1.10      gwr }
    662  1.10      gwr 
    663  1.10      gwr #endif /* SUN3_470 */
    664  1.10      gwr 
    665  1.10      gwr 
    666  1.10      gwr /*
    667   1.3      gwr  * Routines to copy state into and out of the clock.
    668   1.3      gwr  * The clock CSR has to be set for read or write.
    669   1.1      gwr  */
    670   1.3      gwr static void
    671  1.10      gwr mostek_get_dt(struct clock_ymdhms *dt)
    672   1.1      gwr {
    673  1.10      gwr 	volatile struct mostek_clkreg *cl = mostek_clk_va;
    674   1.1      gwr 	int s;
    675   1.1      gwr 
    676   1.1      gwr 	s = splhigh();
    677   1.7      gwr 
    678   1.3      gwr 	/* enable read (stop time) */
    679   1.3      gwr 	cl->cl_csr |= CLK_READ;
    680   1.1      gwr 
    681   1.3      gwr 	/* Copy the info */
    682   1.3      gwr 	dt->dt_sec  = cl->cl_sec;
    683   1.3      gwr 	dt->dt_min  = cl->cl_min;
    684   1.3      gwr 	dt->dt_hour = cl->cl_hour;
    685   1.3      gwr 	dt->dt_wday = cl->cl_wday;
    686   1.3      gwr 	dt->dt_day  = cl->cl_mday;
    687   1.3      gwr 	dt->dt_mon  = cl->cl_month;
    688   1.3      gwr 	dt->dt_year = cl->cl_year;
    689   1.1      gwr 
    690   1.3      gwr 	/* Done reading (time wears on) */
    691   1.3      gwr 	cl->cl_csr &= ~CLK_READ;
    692   1.1      gwr 	splx(s);
    693   1.1      gwr }
    694   1.1      gwr 
    695   1.3      gwr static void
    696  1.10      gwr mostek_set_dt(struct clock_ymdhms *dt)
    697   1.1      gwr {
    698  1.10      gwr 	volatile struct mostek_clkreg *cl = mostek_clk_va;
    699   1.1      gwr 	int s;
    700   1.1      gwr 
    701   1.1      gwr 	s = splhigh();
    702   1.3      gwr 	/* enable write */
    703   1.3      gwr 	cl->cl_csr |= CLK_WRITE;
    704   1.1      gwr 
    705   1.3      gwr 	/* Copy the info */
    706   1.3      gwr 	cl->cl_sec = dt->dt_sec;
    707   1.3      gwr 	cl->cl_min = dt->dt_min;
    708   1.3      gwr 	cl->cl_hour = dt->dt_hour;
    709   1.3      gwr 	cl->cl_wday = dt->dt_wday;
    710   1.3      gwr 	cl->cl_mday = dt->dt_day;
    711   1.3      gwr 	cl->cl_month = dt->dt_mon;
    712   1.3      gwr 	cl->cl_year = dt->dt_year;
    713   1.1      gwr 
    714   1.3      gwr 	/* load them up */
    715   1.3      gwr 	cl->cl_csr &= ~CLK_WRITE;
    716   1.1      gwr 	splx(s);
    717   1.1      gwr }
    718   1.1      gwr 
    719