Home | History | Annotate | Line # | Download | only in sun3x
clock.c revision 1.42
      1 /*	$NetBSD: clock.c,v 1.42 2024/01/14 17:51:16 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * the Systems Programming Group of the University of Utah Computer
      9  * Science Department.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	from: Utah Hdr: clock.c 1.18 91/01/21$
     36  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
     37  */
     38 
     39 /*
     40  * Copyright (c) 1994 Gordon W. Ross
     41  * Copyright (c) 1993 Adam Glass
     42  * Copyright (c) 1988 University of Utah.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * the Systems Programming Group of the University of Utah Computer
     46  * Science Department.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *	from: Utah Hdr: clock.c 1.18 91/01/21$
     77  *	from: @(#)clock.c	8.2 (Berkeley) 1/12/94
     78  */
     79 
     80 /*
     81  * Machine-dependent clock routines.  Sun3X machines may have
     82  * either the Mostek 48T02 or the Intersil 7170 clock.
     83  *
     84  * It is tricky to determine which you have, because there is
     85  * always something responding at the address where the Mostek
     86  * clock might be found: either a Mostek or plain-old EEPROM.
     87  * Therefore, we cheat.  If we find an Intersil clock, assume
     88  * that what responds at the end of the EEPROM space is just
     89  * plain-old EEPROM (not a Mostek clock).  Worse, there are
     90  * H/W problems with probing for an Intersil on the 3/80, so
     91  * on that machine we "know" there is a Mostek clock.
     92  *
     93  * Note that the probing algorithm described above requires
     94  * that we probe the intersil before we probe the mostek!
     95  */
     96 
     97 #include <sys/cdefs.h>
     98 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.42 2024/01/14 17:51:16 thorpej Exp $");
     99 
    100 #include <sys/param.h>
    101 #include <sys/systm.h>
    102 #include <sys/time.h>
    103 #include <sys/kernel.h>
    104 #include <sys/device.h>
    105 
    106 #include <uvm/uvm_extern.h>
    107 
    108 #include <m68k/asm_single.h>
    109 
    110 #include <machine/autoconf.h>
    111 #include <machine/bus.h>
    112 #include <machine/cpu.h>
    113 #include <machine/idprom.h>
    114 #include <machine/leds.h>
    115 #include <machine/vectors.h>
    116 
    117 #include <dev/clock_subr.h>
    118 #include <dev/ic/intersil7170reg.h>
    119 #include <dev/ic/intersil7170var.h>
    120 #include <dev/ic/mk48txxreg.h>
    121 #include <dev/ic/mk48txxvar.h>
    122 
    123 #include <sun3/sun3/machdep.h>
    124 #include <sun3/sun3/interreg.h>
    125 
    126 extern u_int intrcnt[];
    127 
    128 #define SUN3_470	Yes
    129 
    130 #define	CLOCK_PRI	5
    131 #define IREG_CLK_BITS	(IREG_CLOCK_ENAB_7 | IREG_CLOCK_ENAB_5)
    132 
    133 #define MKCLOCK_REG_OFFSET	(MK48T02_CLKOFF + MK48TXX_ICSR)
    134 
    135 /*
    136  * Only one of these two variables should be non-zero after
    137  * autoconfiguration determines which clock we have.
    138  */
    139 static volatile void *intersil_va;
    140 static volatile void *mostek_clk_va;
    141 
    142 void _isr_clock(void);	/* in locore.s */
    143 void clock_intr(struct clockframe);
    144 
    145 
    146 static int  clock_match(device_t, cfdata_t, void *);
    147 static void clock_attach(device_t, device_t, void *);
    148 
    149 CFATTACH_DECL_NEW(clock, sizeof(struct mk48txx_softc),
    150     clock_match, clock_attach, NULL, NULL);
    151 
    152 #ifdef	SUN3_470
    153 
    154 #define intersil_clock ((volatile struct intersil7170 *)intersil_va)
    155 
    156 #define intersil_clear() (void)intersil_clock->clk_intr_reg
    157 
    158 static int  oclock_match(device_t, cfdata_t, void *);
    159 static void oclock_attach(device_t, device_t, void *);
    160 
    161 CFATTACH_DECL_NEW(oclock, sizeof(struct intersil7170_softc),
    162     oclock_match, oclock_attach, NULL, NULL);
    163 
    164 
    165 /*
    166  * Is there an intersil clock?
    167  */
    168 static int
    169 oclock_match(device_t parent, cfdata_t cf, void *aux)
    170 {
    171 	struct confargs *ca = aux;
    172 
    173 	/* This driver only supports one unit. */
    174 	if (intersil_va)
    175 		return 0;
    176 
    177 	/*
    178 	 * The 3/80 can not probe the Intersil absent,
    179 	 * but it never has one, so "just say no."
    180 	 */
    181 	if (cpu_machine_id == ID_SUN3X_80)
    182 		return 0;
    183 
    184 	/* OK, really probe for the Intersil. */
    185 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, 1) == -1)
    186 		return 0;
    187 
    188 	/* Default interrupt priority. */
    189 	if (ca->ca_intpri == -1)
    190 		ca->ca_intpri = CLOCK_PRI;
    191 
    192 	return 1;
    193 }
    194 
    195 /*
    196  * Attach the intersil clock.
    197  */
    198 static void
    199 oclock_attach(device_t parent, device_t self, void *aux)
    200 {
    201 	struct intersil7170_softc *sc = device_private(self);
    202 	struct confargs *ca = aux;
    203 
    204 	sc->sc_dev = self;
    205 
    206 	/* Get a mapping for it. */
    207 	sc->sc_bst = ca->ca_bustag;
    208 	if (bus_space_map(sc->sc_bst, ca->ca_paddr, sizeof(struct intersil7170),
    209 	    0, &sc->sc_bsh) != 0) {
    210 		aprint_error(": can't map registers\n");
    211 		return;
    212 	}
    213 
    214 	intersil_va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
    215 
    216 #ifdef	DIAGNOSTIC
    217 	/* Verify correct probe order... */
    218 	if (mostek_clk_va) {
    219 		mostek_clk_va = NULL;
    220 		aprint_normal("\n");
    221 		aprint_error_dev(self, "warning - mostek found also!\n");
    222 	}
    223 #endif
    224 
    225 	/*
    226 	 * Set the clock to the correct interrupt rate, but
    227 	 * do not enable the interrupt until cpu_initclocks.
    228 	 * XXX: Actually, the interrupt_reg should be zero
    229 	 * at this point, so the clock interrupts should not
    230 	 * affect us, but we need to set the rate...
    231 	 */
    232 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, INTERSIL_ICMD,
    233 	    INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE));
    234 	(void)bus_space_read_1(sc->sc_bst, sc->sc_bsh, INTERSIL_IINTR);
    235 
    236 	/* Set the clock to 100 Hz, but do not enable it yet. */
    237 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
    238 	    INTERSIL_IINTR, INTERSIL_INTER_CSECONDS);
    239 
    240 	sc->sc_year0 = 1968;
    241 	intersil7170_attach(sc);
    242 
    243 	aprint_normal("\n");
    244 
    245 	/*
    246 	 * Can not hook up the ISR until cpu_initclocks()
    247 	 * because hardclock is not ready until then.
    248 	 * For now, the handler is _isr_autovec(), which
    249 	 * will complain if it gets clock interrupts.
    250 	 */
    251 }
    252 #endif	/* SUN3_470 */
    253 
    254 
    255 /*
    256  * Is there a Mostek clock?  Hard to tell...
    257  * (See comment at top of this file.)
    258  */
    259 static int
    260 clock_match(device_t parent, cfdata_t cf, void *args)
    261 {
    262 	struct confargs *ca = args;
    263 
    264 	/* This driver only supports one unit. */
    265 	if (mostek_clk_va)
    266 		return 0;
    267 
    268 	/* If intersil was found, use that. */
    269 	if (intersil_va)
    270 		return 0;
    271 	/* Else assume a Mostek is there... */
    272 
    273 	/* Default interrupt priority. */
    274 	if (ca->ca_intpri == -1)
    275 		ca->ca_intpri = CLOCK_PRI;
    276 
    277 	return 1;
    278 }
    279 
    280 /*
    281  * Attach the mostek clock.
    282  */
    283 static void
    284 clock_attach(device_t parent, device_t self, void *aux)
    285 {
    286 	struct mk48txx_softc *sc = device_private(self);
    287 	struct confargs *ca = aux;
    288 
    289 	sc->sc_dev = self;
    290 	sc->sc_bst = ca->ca_bustag;
    291 	if (bus_space_map(sc->sc_bst, ca->ca_paddr - MKCLOCK_REG_OFFSET,
    292 	    MK48T02_CLKSZ, 0, &sc->sc_bsh) != 0) {
    293 		aprint_error(": can't map device space\n");
    294 		return;
    295 	}
    296 
    297 	mostek_clk_va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
    298 
    299 	sc->sc_model = "mk48t02";
    300 	sc->sc_year0 = 1968;
    301 
    302 	mk48txx_attach(sc);
    303 
    304 	aprint_normal("\n");
    305 }
    306 
    307 /*
    308  * Set and/or clear the desired clock bits in the interrupt
    309  * register.  We have to be extremely careful that we do it
    310  * in such a manner that we don't get ourselves lost.
    311  * XXX:  Watch out!  It's really easy to break this!
    312  */
    313 void
    314 set_clk_mode(u_char on, u_char off, int enable_clk)
    315 {
    316 	u_char interreg;
    317 
    318 	/*
    319 	 * If we have not yet mapped the register,
    320 	 * then we do not want to do any of this...
    321 	 */
    322 	if (!interrupt_reg)
    323 		return;
    324 
    325 #ifdef	DIAGNOSTIC
    326 	/* Assertion: were are at splhigh! */
    327 	if ((getsr() & PSL_IPL) < PSL_IPL7)
    328 		panic("set_clk_mode: bad ipl");
    329 #endif
    330 
    331 	/*
    332 	 * make sure that we are only playing w/
    333 	 * clock interrupt register bits
    334 	 */
    335 	on  &= IREG_CLK_BITS;
    336 	off &= IREG_CLK_BITS;
    337 
    338 	/* First, turn off the "master" enable bit. */
    339 	single_inst_bclr_b(*interrupt_reg, IREG_ALL_ENAB);
    340 
    341 	/*
    342 	 * Save the current interrupt register clock bits,
    343 	 * and turn off/on the requested bits in the copy.
    344 	 */
    345 	interreg = *interrupt_reg & IREG_CLK_BITS;
    346 	interreg &= ~off;
    347 	interreg |= on;
    348 
    349 	/* Clear the CLK5 and CLK7 bits to clear the flip-flops. */
    350 	single_inst_bclr_b(*interrupt_reg, IREG_CLK_BITS);
    351 
    352 #ifdef	SUN3_470
    353 	if (intersil_va) {
    354 		/*
    355 		 * Then disable clock interrupts, and read the clock's
    356 		 * interrupt register to clear any pending signals there.
    357 		 */
    358 		intersil_clock->clk_cmd_reg =
    359 		    INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IDISABLE);
    360 		intersil_clear();
    361 	}
    362 #endif	/* SUN3_470 */
    363 
    364 	/* Set the requested bits in the interrupt register. */
    365 	single_inst_bset_b(*interrupt_reg, interreg);
    366 
    367 #ifdef	SUN3_470
    368 	/* Turn the clock back on (maybe) */
    369 	if (intersil_va && enable_clk)
    370 		intersil_clock->clk_cmd_reg =
    371 		    INTERSIL_COMMAND(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    372 #endif	/* SUN3_470 */
    373 
    374 	/* Finally, turn the "master" enable back on. */
    375 	single_inst_bset_b(*interrupt_reg, IREG_ALL_ENAB);
    376 }
    377 
    378 /*
    379  * Set up the real-time clock (enable clock interrupts).
    380  * Leave stathz 0 since there is no secondary clock available.
    381  * Note that clock interrupts MUST STAY DISABLED until here.
    382  */
    383 void
    384 cpu_initclocks(void)
    385 {
    386 	int s;
    387 
    388 	s = splhigh();
    389 
    390 	/* Install isr (in locore.s) that calls clock_intr(). */
    391 	vec_set_entry(VECI_INTRAV0 + CLOCK_PRI, (void *)_isr_clock);
    392 
    393 	/* Now enable the clock at level 5 in the interrupt reg. */
    394 	set_clk_mode(IREG_CLOCK_ENAB_5, 0, 1);
    395 
    396 	splx(s);
    397 }
    398 
    399 /*
    400  * This doesn't need to do anything, as we have only one timer and
    401  * profhz==stathz==hz.
    402  */
    403 void
    404 setstatclockrate(int newhz)
    405 {
    406 
    407 	/* nothing */
    408 }
    409 
    410 /*
    411  * Clock interrupt handler (for both Intersil and Mostek).
    412  * XXX - Is it worth the trouble to save a few cycles here
    413  * by making two separate interrupt handlers?
    414  *
    415  * This is called by the "custom" interrupt handler.
    416  * Note that we can get ZS interrupts while this runs,
    417  * and zshard may touch the interrupt_reg, so we must
    418  * be careful to use the single_inst_* macros to modify
    419  * the interrupt register atomically.
    420  */
    421 void
    422 clock_intr(struct clockframe cf)
    423 {
    424 	extern char _Idle[];	/* locore.s */
    425 
    426 	idepth++;
    427 
    428 #ifdef	SUN3_470
    429 	if (intersil_va) {
    430 		/* Read the clock interrupt register. */
    431 		intersil_clear();
    432 	}
    433 #endif	/* SUN3_470 */
    434 
    435 	/* Pulse the clock intr. enable low. */
    436 	single_inst_bclr_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
    437 	single_inst_bset_b(*interrupt_reg, IREG_CLOCK_ENAB_5);
    438 
    439 #ifdef	SUN3_470
    440 	if (intersil_va) {
    441 		/* Read the clock intr. reg. AGAIN! */
    442 		intersil_clear();
    443 	}
    444 #endif	/* SUN3_470 */
    445 
    446 	intrcnt[CLOCK_PRI]++;
    447 	curcpu()->ci_data.cpu_nintr++;
    448 
    449 	/* Entertainment! */
    450 	if (cf.cf_pc == (long)_Idle)
    451 		leds_intr();
    452 
    453 	/* Call common clock interrupt handler. */
    454 	hardclock(&cf);
    455 
    456 	idepth--;
    457 }
    458