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