Home | History | Annotate | Line # | Download | only in dev
int.c revision 1.9.14.3
      1  1.9.14.3    yamt /*	$NetBSD: int.c,v 1.9.14.3 2008/01/21 09:39:13 yamt Exp $	*/
      2       1.1  sekiya 
      3       1.1  sekiya /*
      4       1.1  sekiya  * Copyright (c) 2004 Christopher SEKIYA
      5       1.1  sekiya  * All rights reserved.
      6       1.1  sekiya  *
      7       1.1  sekiya  * Redistribution and use in source and binary forms, with or without
      8       1.1  sekiya  * modification, are permitted provided that the following conditions
      9       1.1  sekiya  * are met:
     10       1.1  sekiya  * 1. Redistributions of source code must retain the above copyright
     11       1.1  sekiya  *    notice, this list of conditions and the following disclaimer.
     12       1.1  sekiya  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  sekiya  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  sekiya  *    documentation and/or other materials provided with the distribution.
     15       1.1  sekiya  * 3. The name of the author may not be used to endorse or promote products
     16       1.1  sekiya  *    derived from this software without specific prior written permission.
     17       1.1  sekiya  *
     18       1.1  sekiya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1  sekiya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1  sekiya  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1  sekiya  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1  sekiya  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1  sekiya  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1  sekiya  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1  sekiya  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1  sekiya  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27       1.1  sekiya  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1  sekiya  */
     29       1.1  sekiya 
     30       1.1  sekiya /*
     31       1.5   pooka  * INT/INT2/INT3 interrupt controller (used in Indy's, Indigo's, etc..)
     32       1.1  sekiya  */
     33       1.1  sekiya 
     34       1.1  sekiya #include <sys/cdefs.h>
     35  1.9.14.3    yamt __KERNEL_RCSID(0, "$NetBSD: int.c,v 1.9.14.3 2008/01/21 09:39:13 yamt Exp $");
     36       1.1  sekiya 
     37       1.1  sekiya #include "opt_cputype.h"
     38       1.1  sekiya 
     39       1.1  sekiya #include <sys/param.h>
     40       1.1  sekiya #include <sys/proc.h>
     41       1.1  sekiya #include <sys/systm.h>
     42  1.9.14.2    yamt #include <sys/timetc.h>
     43       1.1  sekiya #include <sys/kernel.h>
     44       1.1  sekiya #include <sys/device.h>
     45       1.8  sekiya #include <sys/malloc.h>
     46       1.1  sekiya 
     47       1.1  sekiya #include <dev/ic/i8253reg.h>
     48       1.1  sekiya #include <machine/sysconf.h>
     49       1.1  sekiya #include <machine/machtype.h>
     50       1.1  sekiya #include <machine/bus.h>
     51       1.1  sekiya #include <mips/locore.h>
     52       1.1  sekiya 
     53       1.1  sekiya #include <mips/cache.h>
     54       1.1  sekiya 
     55       1.1  sekiya #include <sgimips/dev/int2reg.h>
     56       1.3  sekiya #include <sgimips/dev/int2var.h>
     57       1.1  sekiya 
     58       1.1  sekiya static bus_space_handle_t ioh;
     59       1.1  sekiya static bus_space_tag_t iot;
     60       1.1  sekiya 
     61       1.1  sekiya struct int_softc {
     62       1.1  sekiya 	struct device sc_dev;
     63       1.1  sekiya };
     64       1.1  sekiya 
     65       1.1  sekiya 
     66       1.1  sekiya static int	int_match(struct device *, struct cfdata *, void *);
     67       1.1  sekiya static void	int_attach(struct device *, struct device *, void *);
     68  1.9.14.2    yamt static void 	int_local0_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
     69  1.9.14.2    yamt static void	int_local1_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
     70  1.9.14.2    yamt static int 	int_mappable_intr(void *);
     71  1.9.14.2    yamt static void    *int_intr_establish(int, int, int (*)(void *), void *);
     72  1.9.14.2    yamt static void	int_8254_cal(void);
     73  1.9.14.2    yamt static u_int	int_8254_get_timecount(struct timecounter *);
     74  1.9.14.2    yamt static void	int_8254_intr0(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
     75  1.9.14.2    yamt static void	int_8254_intr1(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
     76  1.9.14.2    yamt 
     77  1.9.14.2    yamt #ifdef MIPS3
     78  1.9.14.2    yamt static u_long	int_cal_timer(void);
     79  1.9.14.2    yamt #endif
     80  1.9.14.2    yamt 
     81  1.9.14.2    yamt static struct timecounter int_8254_timecounter = {
     82  1.9.14.2    yamt 	int_8254_get_timecount,	/* get_timecount */
     83  1.9.14.2    yamt 	0,			/* no poll_pps */
     84  1.9.14.2    yamt 	~0u,			/* counter_mask */
     85  1.9.14.2    yamt 	500000,			/* frequency */
     86  1.9.14.2    yamt 	"int i8254",		/* name */
     87  1.9.14.2    yamt 	100,			/* quality */
     88  1.9.14.2    yamt 	NULL,			/* prev */
     89  1.9.14.2    yamt 	NULL,			/* next */
     90  1.9.14.2    yamt };
     91  1.9.14.2    yamt 
     92  1.9.14.2    yamt static u_long int_8254_tc_count;
     93       1.1  sekiya 
     94       1.1  sekiya CFATTACH_DECL(int, sizeof(struct int_softc),
     95       1.1  sekiya 	int_match, int_attach, NULL, NULL);
     96       1.1  sekiya 
     97       1.1  sekiya static int
     98       1.1  sekiya int_match(struct device *parent, struct cfdata *match, void *aux)
     99       1.1  sekiya {
    100       1.6   pooka 
    101       1.6   pooka 	if ((mach_type == MACH_SGI_IP12) || (mach_type == MACH_SGI_IP20) ||
    102       1.6   pooka 	    (mach_type == MACH_SGI_IP22) )
    103       1.1  sekiya 		return 1;
    104       1.1  sekiya 
    105       1.1  sekiya 	return 0;
    106       1.1  sekiya }
    107       1.1  sekiya 
    108       1.1  sekiya static void
    109       1.1  sekiya int_attach(struct device *parent, struct device *self, void *aux)
    110       1.1  sekiya {
    111       1.1  sekiya 	u_int32_t address;
    112       1.1  sekiya 
    113       1.1  sekiya 	if (mach_type == MACH_SGI_IP12)
    114       1.1  sekiya 		address = INT_IP12;
    115       1.1  sekiya 	else if (mach_type == MACH_SGI_IP20)
    116       1.1  sekiya 		address = INT_IP20;
    117       1.1  sekiya 	else if (mach_type == MACH_SGI_IP22) {
    118       1.1  sekiya 		if (mach_subtype == MACH_SGI_IP22_FULLHOUSE)
    119       1.1  sekiya 			address = INT_IP22;
    120       1.1  sekiya 		else
    121       1.1  sekiya 			address = INT_IP24;
    122       1.7   pooka 	} else
    123       1.1  sekiya 		panic("\nint0: passed match, but failed attach?");
    124       1.1  sekiya 
    125  1.9.14.2    yamt 	printf(" addr 0x%x\n", address);
    126  1.9.14.1    yamt 
    127       1.1  sekiya 	bus_space_map(iot, address, 0, 0, &ioh);
    128       1.1  sekiya 	iot = SGIMIPS_BUS_SPACE_NORMAL;
    129       1.1  sekiya 
    130       1.1  sekiya 	/* Clean out interrupt masks */
    131       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, 0);
    132       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, 0);
    133       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_MAP_MASK0, 0);
    134       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_MAP_MASK1, 0);
    135       1.1  sekiya 
    136       1.1  sekiya 	/* Reset timer interrupts */
    137       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_TIMER_CLEAR, 0x03);
    138       1.1  sekiya 
    139       1.1  sekiya 	switch (mach_type) {
    140       1.1  sekiya 		case MACH_SGI_IP12:
    141       1.1  sekiya 			platform.intr1 = int_local0_intr;
    142       1.1  sekiya 			platform.intr2 = int_local1_intr;
    143  1.9.14.2    yamt 			platform.intr3 = int_8254_intr0;
    144  1.9.14.2    yamt 			platform.intr4 = int_8254_intr1;
    145       1.1  sekiya 			int_8254_cal();
    146  1.9.14.2    yamt 			tc_init(&int_8254_timecounter);
    147       1.1  sekiya 			break;
    148       1.4   pooka #ifdef MIPS3
    149       1.1  sekiya 		case MACH_SGI_IP20:
    150       1.1  sekiya 		case MACH_SGI_IP22:
    151       1.4   pooka 		{
    152       1.4   pooka 			int i;
    153       1.4   pooka 			unsigned long cps;
    154       1.4   pooka 			unsigned long ctrdiff[3];
    155       1.4   pooka 
    156       1.1  sekiya 			platform.intr0 = int_local0_intr;
    157       1.1  sekiya 			platform.intr1 = int_local1_intr;
    158       1.1  sekiya 
    159       1.1  sekiya 			/* calibrate timer */
    160       1.1  sekiya 			int_cal_timer();
    161       1.1  sekiya 
    162       1.1  sekiya 			cps = 0;
    163       1.6   pooka 			for (i = 0;
    164       1.6   pooka 			    i < sizeof(ctrdiff) / sizeof(ctrdiff[0]); i++) {
    165       1.1  sekiya 				do {
    166       1.1  sekiya 					ctrdiff[i] = int_cal_timer();
    167       1.1  sekiya 				} while (ctrdiff[i] == 0);
    168       1.1  sekiya 
    169       1.1  sekiya 				cps += ctrdiff[i];
    170       1.1  sekiya 			}
    171       1.1  sekiya 
    172       1.1  sekiya 			cps = cps / (sizeof(ctrdiff) / sizeof(ctrdiff[0]));
    173       1.1  sekiya 
    174  1.9.14.2    yamt 			printf("%s: bus %luMHz, CPU %luMHz\n",
    175  1.9.14.2    yamt 			    self->dv_xname, cps / 10000, cps / 5000);
    176       1.1  sekiya 
    177       1.1  sekiya 			/* R4k/R4400/R4600/R5k count at half CPU frequency */
    178       1.1  sekiya 			curcpu()->ci_cpu_freq = 2 * cps * hz;
    179       1.4   pooka 		}
    180       1.4   pooka #endif /* MIPS3 */
    181       1.1  sekiya 
    182       1.1  sekiya 			break;
    183       1.1  sekiya 		default:
    184       1.1  sekiya 			panic("int0: unsupported machine type %i\n", mach_type);
    185       1.1  sekiya 			break;
    186       1.1  sekiya 	}
    187       1.1  sekiya 
    188       1.1  sekiya 	curcpu()->ci_cycles_per_hz = curcpu()->ci_cpu_freq / (2 * hz);
    189       1.1  sekiya 	curcpu()->ci_divisor_delay = curcpu()->ci_cpu_freq / (2 * 1000000);
    190  1.9.14.3    yamt 	MIPS_SET_CI_RECIPROCAL(curcpu());
    191       1.1  sekiya 
    192       1.1  sekiya 	if (mach_type == MACH_SGI_IP22) {
    193       1.1  sekiya 		/* Wire interrupts 7, 11 to mappable interrupt 0,1 handlers */
    194       1.1  sekiya 		intrtab[7].ih_fun = int_mappable_intr;
    195       1.1  sekiya 		intrtab[7].ih_arg = (void*) 0;
    196       1.1  sekiya 
    197       1.1  sekiya 		intrtab[11].ih_fun = int_mappable_intr;
    198       1.1  sekiya 		intrtab[11].ih_arg = (void*) 1;
    199       1.1  sekiya 	}
    200       1.1  sekiya 
    201       1.1  sekiya 	platform.intr_establish = int_intr_establish;
    202       1.1  sekiya }
    203       1.1  sekiya 
    204       1.1  sekiya int
    205       1.1  sekiya int_mappable_intr(void *arg)
    206       1.1  sekiya {
    207       1.1  sekiya 	int i;
    208       1.1  sekiya 	int ret;
    209       1.1  sekiya 	int intnum;
    210       1.1  sekiya 	u_int32_t mstat;
    211       1.1  sekiya 	u_int32_t mmask;
    212       1.1  sekiya 	int which = (int)arg;
    213       1.8  sekiya 	struct sgimips_intrhand *ih;
    214       1.1  sekiya 
    215       1.1  sekiya 	ret = 0;
    216       1.1  sekiya 	mstat = bus_space_read_4(iot, ioh, INT2_MAP_STATUS);
    217       1.1  sekiya 	mmask = bus_space_read_4(iot, ioh, INT2_MAP_MASK0 + (which << 2));
    218       1.1  sekiya 
    219       1.1  sekiya 	mstat &= mmask;
    220       1.1  sekiya 
    221       1.1  sekiya 	for (i = 0; i < 8; i++) {
    222       1.1  sekiya 		intnum = i + 16 + (which << 3);
    223       1.1  sekiya 		if (mstat & (1 << i)) {
    224       1.8  sekiya 			for (ih = &intrtab[intnum]; ih != NULL;
    225       1.8  sekiya 							ih = ih->ih_next) {
    226       1.8  sekiya 				if (ih->ih_fun != NULL)
    227       1.8  sekiya 					ret |= (ih->ih_fun)(ih->ih_arg);
    228       1.8  sekiya 				else
    229       1.8  sekiya 					printf("int0: unexpected mapped "
    230       1.8  sekiya 					       "interrupt %d\n", intnum);
    231       1.8  sekiya 			}
    232       1.1  sekiya 		}
    233       1.1  sekiya 	}
    234       1.1  sekiya 
    235       1.1  sekiya 	return ret;
    236       1.1  sekiya }
    237       1.1  sekiya 
    238       1.1  sekiya void
    239       1.6   pooka int_local0_intr(u_int32_t status, u_int32_t cause, u_int32_t pc,
    240       1.6   pooka 		u_int32_t ipending)
    241       1.1  sekiya {
    242       1.1  sekiya 	int i;
    243       1.1  sekiya 	u_int32_t l0stat;
    244       1.1  sekiya 	u_int32_t l0mask;
    245       1.8  sekiya 	struct sgimips_intrhand *ih;
    246       1.1  sekiya 
    247       1.1  sekiya 	l0stat = bus_space_read_4(iot, ioh, INT2_LOCAL0_STATUS);
    248       1.1  sekiya 	l0mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
    249       1.1  sekiya 
    250  1.9.14.2    yamt 	l0stat &= l0mask;
    251       1.1  sekiya 
    252       1.1  sekiya 	for (i = 0; i < 8; i++) {
    253  1.9.14.2    yamt 		if (l0stat & (1 << i)) {
    254       1.8  sekiya 			for (ih = &intrtab[i]; ih != NULL; ih = ih->ih_next) {
    255       1.8  sekiya 				if (ih->ih_fun != NULL)
    256       1.8  sekiya 					(ih->ih_fun)(ih->ih_arg);
    257       1.8  sekiya 				else
    258       1.8  sekiya 					printf("int0: unexpected local0 "
    259       1.8  sekiya 					       "interrupt %d\n", i);
    260       1.8  sekiya 			}
    261       1.1  sekiya 		}
    262       1.1  sekiya 	}
    263       1.1  sekiya }
    264       1.1  sekiya 
    265       1.1  sekiya void
    266       1.6   pooka int_local1_intr(u_int32_t status, u_int32_t cause, u_int32_t pc,
    267       1.6   pooka 		u_int32_t ipending)
    268       1.1  sekiya {
    269       1.1  sekiya 	int i;
    270       1.1  sekiya 	u_int32_t l1stat;
    271       1.1  sekiya 	u_int32_t l1mask;
    272       1.8  sekiya 	struct sgimips_intrhand *ih;
    273       1.1  sekiya 
    274       1.1  sekiya 	l1stat = bus_space_read_4(iot, ioh, INT2_LOCAL1_STATUS);
    275       1.1  sekiya 	l1mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
    276       1.1  sekiya 
    277       1.1  sekiya 	l1stat &= l1mask;
    278       1.1  sekiya 
    279       1.1  sekiya 	for (i = 0; i < 8; i++) {
    280       1.1  sekiya 		if (l1stat & (1 << i)) {
    281       1.8  sekiya 			for (ih = &intrtab[8+i]; ih != NULL; ih = ih->ih_next) {
    282       1.8  sekiya 				if (ih->ih_fun != NULL)
    283       1.8  sekiya 					(ih->ih_fun)(ih->ih_arg);
    284       1.8  sekiya 				else
    285       1.8  sekiya 					printf("int0: unexpected local1 "
    286       1.8  sekiya 					       " interrupt %x\n", 8 + i);
    287       1.8  sekiya 			}
    288       1.1  sekiya 		}
    289       1.1  sekiya 	}
    290       1.1  sekiya }
    291       1.1  sekiya 
    292       1.1  sekiya void *
    293       1.1  sekiya int_intr_establish(int level, int ipl, int (*handler) (void *), void *arg)
    294       1.1  sekiya {
    295       1.1  sekiya 	u_int32_t mask;
    296       1.1  sekiya 
    297       1.1  sekiya 	if (level < 0 || level >= NINTR)
    298       1.1  sekiya 		panic("invalid interrupt level");
    299       1.1  sekiya 
    300       1.8  sekiya 	if (intrtab[level].ih_fun == NULL) {
    301       1.8  sekiya 		intrtab[level].ih_fun = handler;
    302       1.8  sekiya 		intrtab[level].ih_arg = arg;
    303       1.8  sekiya 		intrtab[level].ih_next = NULL;
    304       1.8  sekiya 	} else {
    305       1.8  sekiya 		struct sgimips_intrhand *n, *ih = malloc(sizeof *ih,
    306       1.8  sekiya 							 M_DEVBUF, M_NOWAIT);
    307       1.8  sekiya 
    308       1.8  sekiya 		if (ih == NULL) {
    309  1.9.14.1    yamt 			printf("int_intr_establish: can't allocate handler\n");
    310       1.8  sekiya 			return (void *)NULL;
    311       1.8  sekiya 		}
    312       1.8  sekiya 
    313       1.8  sekiya 		ih->ih_fun = handler;
    314       1.8  sekiya 		ih->ih_arg = arg;
    315       1.8  sekiya 		ih->ih_next = NULL;
    316       1.8  sekiya 
    317       1.8  sekiya 		for (n = &intrtab[level]; n->ih_next != NULL; n = n->ih_next)
    318  1.9.14.1    yamt 			;
    319  1.9.14.1    yamt 
    320       1.8  sekiya 		n->ih_next = ih;
    321       1.8  sekiya 
    322       1.8  sekiya 		return (void *)NULL;	/* vector already set */
    323       1.1  sekiya 	}
    324       1.1  sekiya 
    325       1.1  sekiya 
    326       1.1  sekiya 	if (level < 8) {
    327       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
    328       1.1  sekiya 		mask |= (1 << level);
    329       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, mask);
    330       1.1  sekiya 	} else if (level < 16) {
    331       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
    332       1.1  sekiya 		mask |= (1 << (level - 8));
    333       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, mask);
    334       1.1  sekiya 	} else if (level < 24) {
    335       1.1  sekiya 		/* Map0 interrupt maps to l0 bit 7, so turn that on too */
    336       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_LOCAL0_MASK);
    337       1.1  sekiya 		mask |= (1 << 7);
    338       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_LOCAL0_MASK, mask);
    339       1.1  sekiya 
    340       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_MAP_MASK0);
    341       1.1  sekiya 		mask |= (1 << (level - 16));
    342       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_MAP_MASK0, mask);
    343       1.1  sekiya 	} else {
    344       1.1  sekiya 		/* Map1 interrupt maps to l1 bit 3, so turn that on too */
    345       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_LOCAL1_MASK);
    346       1.1  sekiya 		mask |= (1 << 3);
    347       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_LOCAL1_MASK, mask);
    348       1.1  sekiya 
    349       1.1  sekiya 		mask = bus_space_read_4(iot, ioh, INT2_MAP_MASK1);
    350       1.1  sekiya 		mask |= (1 << (level - 24));
    351       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_MAP_MASK1, mask);
    352       1.1  sekiya 	}
    353       1.1  sekiya 
    354       1.1  sekiya 	return (void *)NULL;
    355       1.1  sekiya }
    356       1.1  sekiya 
    357       1.4   pooka #ifdef MIPS3
    358  1.9.14.2    yamt static u_long
    359       1.1  sekiya int_cal_timer(void)
    360       1.1  sekiya {
    361       1.1  sekiya 	int s;
    362       1.1  sekiya 	int roundtime;
    363       1.1  sekiya 	int sampletime;
    364       1.1  sekiya 	int startmsb, lsb, msb;
    365       1.1  sekiya 	unsigned long startctr, endctr;
    366       1.1  sekiya 
    367       1.1  sekiya 	/*
    368       1.1  sekiya 	 * NOTE: HZ must be greater than 15 for this to work, as otherwise
    369       1.1  sekiya 	 * we'll overflow the counter.  We round the answer to hearest 1
    370       1.1  sekiya 	 * MHz of the master (2x) clock.
    371       1.1  sekiya 	 */
    372       1.1  sekiya 	roundtime = (1000000 / hz) / 2;
    373       1.1  sekiya 	sampletime = (1000000 / hz) + 0xff;
    374       1.1  sekiya 	startmsb = (sampletime >> 8);
    375       1.1  sekiya 
    376       1.1  sekiya 	s = splhigh();
    377       1.1  sekiya 
    378       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL,
    379       1.1  sekiya 		( TIMER_SEL2 | TIMER_16BIT | TIMER_RATEGEN) );
    380       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_TIMER_2, (sampletime & 0xff));
    381       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_TIMER_2, (sampletime >> 8));
    382       1.1  sekiya 
    383       1.1  sekiya 	startctr = mips3_cp0_count_read();
    384       1.1  sekiya 
    385       1.1  sekiya 	/* Wait for the MSB to count down to zero */
    386       1.1  sekiya 	do {
    387       1.1  sekiya 		bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL, TIMER_SEL2 );
    388       1.1  sekiya 		lsb = bus_space_read_4(iot, ioh, INT2_TIMER_2) & 0xff;
    389       1.1  sekiya 		msb = bus_space_read_4(iot, ioh, INT2_TIMER_2) & 0xff;
    390       1.1  sekiya 
    391       1.1  sekiya 		endctr = mips3_cp0_count_read();
    392       1.1  sekiya 	} while (msb);
    393       1.1  sekiya 
    394       1.1  sekiya 	/* Turn off timer */
    395       1.1  sekiya 	bus_space_write_4(iot, ioh, INT2_TIMER_CONTROL,
    396       1.1  sekiya 		( TIMER_SEL2 | TIMER_16BIT | TIMER_SWSTROBE) );
    397       1.1  sekiya 
    398       1.1  sekiya 	splx(s);
    399       1.1  sekiya 
    400       1.1  sekiya 	return (endctr - startctr) / roundtime * roundtime;
    401       1.1  sekiya }
    402       1.4   pooka #endif /* MIPS3 */
    403       1.1  sekiya 
    404  1.9.14.2    yamt /*
    405  1.9.14.2    yamt  * A 1.000MHz master clock is wired to TIMER2, which in turn clocks the two
    406  1.9.14.2    yamt  * other timers. On IP12 TIMER1 interrupts on MIPS interrupt 1 and TIMER2
    407  1.9.14.2    yamt  * on MIPS interrupt 2.
    408  1.9.14.2    yamt  *
    409  1.9.14.2    yamt  * Apparently int2 doesn't like counting down from one, but two works, so
    410  1.9.14.2    yamt  * we get a good 500000Hz.
    411  1.9.14.2    yamt  */
    412       1.1  sekiya void
    413       1.1  sekiya int_8254_cal(void)
    414       1.1  sekiya {
    415       1.1  sekiya 	int s;
    416       1.1  sekiya 
    417       1.1  sekiya 	s = splhigh();
    418       1.1  sekiya 
    419       1.7   pooka 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
    420  1.9.14.1    yamt 	    TIMER_SEL0|TIMER_RATEGEN|TIMER_16BIT);
    421  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 3, (500000 / hz) % 256);
    422       1.1  sekiya 	wbflush();
    423       1.1  sekiya 	delay(4);
    424  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 3, (500000 / hz) / 256);
    425  1.9.14.2    yamt 
    426  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
    427  1.9.14.2    yamt 	    TIMER_SEL1|TIMER_RATEGEN|TIMER_16BIT);
    428  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 7, 0xff);
    429  1.9.14.2    yamt 	wbflush();
    430  1.9.14.2    yamt 	delay(4);
    431  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 7, 0xff);
    432       1.1  sekiya 
    433       1.7   pooka 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
    434  1.9.14.1    yamt 	    TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
    435  1.9.14.2    yamt 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 11, 2);
    436       1.1  sekiya 	wbflush();
    437       1.1  sekiya 	delay(4);
    438       1.7   pooka 	bus_space_write_1(iot, ioh, INT2_TIMER_0 + 11, 0);
    439  1.9.14.2    yamt 
    440  1.9.14.2    yamt 	splx(s);
    441  1.9.14.2    yamt }
    442  1.9.14.2    yamt 
    443  1.9.14.2    yamt 
    444  1.9.14.2    yamt static u_int
    445  1.9.14.2    yamt int_8254_get_timecount(struct timecounter *tc)
    446  1.9.14.2    yamt {
    447  1.9.14.2    yamt 	int s;
    448  1.9.14.2    yamt 	u_int count;
    449  1.9.14.2    yamt 	u_char lo, hi;
    450  1.9.14.2    yamt 
    451  1.9.14.2    yamt 	s = splhigh();
    452  1.9.14.2    yamt 
    453  1.9.14.2    yamt         bus_space_write_1(iot, ioh, INT2_TIMER_0 + 15,
    454  1.9.14.2    yamt 	    TIMER_SEL1 | TIMER_LATCH);
    455  1.9.14.2    yamt 	lo = bus_space_read_1(iot, ioh, INT2_TIMER_0 + 7);
    456  1.9.14.2    yamt 	hi = bus_space_read_1(iot, ioh, INT2_TIMER_0 + 7);
    457  1.9.14.2    yamt 	count = 0xffff - ((hi << 8) | lo);
    458  1.9.14.2    yamt 
    459  1.9.14.2    yamt 	splx(s);
    460  1.9.14.2    yamt 
    461  1.9.14.2    yamt 	return (int_8254_tc_count + count);
    462  1.9.14.2    yamt }
    463  1.9.14.2    yamt 
    464  1.9.14.2    yamt static void
    465  1.9.14.2    yamt int_8254_intr0(u_int32_t status, u_int32_t cause, u_int32_t pc,
    466  1.9.14.2    yamt     u_int32_t ipending)
    467  1.9.14.2    yamt {
    468  1.9.14.2    yamt 	struct clockframe cf;
    469  1.9.14.2    yamt 
    470  1.9.14.2    yamt 	cf.pc = pc;
    471  1.9.14.2    yamt 	cf.sr = status;
    472  1.9.14.2    yamt 
    473  1.9.14.2    yamt 	hardclock(&cf);
    474  1.9.14.2    yamt 
    475  1.9.14.2    yamt 	bus_space_write_4(iot, ioh, INT2_TIMER_CLEAR, 1);
    476  1.9.14.2    yamt }
    477  1.9.14.2    yamt 
    478  1.9.14.2    yamt 
    479  1.9.14.2    yamt static void
    480  1.9.14.2    yamt int_8254_intr1(u_int32_t status, u_int32_t cause, u_int32_t pc,
    481  1.9.14.2    yamt     u_int32_t ipending)
    482  1.9.14.2    yamt {
    483  1.9.14.2    yamt 	int s;
    484  1.9.14.2    yamt 
    485  1.9.14.2    yamt 	s = splhigh();
    486  1.9.14.2    yamt 
    487  1.9.14.2    yamt 	int_8254_tc_count += 0xffff;
    488  1.9.14.2    yamt 	bus_space_write_4(iot, ioh, INT2_TIMER_CLEAR, 2);
    489  1.9.14.2    yamt 
    490       1.1  sekiya 	splx(s);
    491       1.1  sekiya }
    492       1.3  sekiya 
    493       1.3  sekiya void
    494       1.3  sekiya int2_wait_fifo(u_int32_t flag)
    495       1.3  sekiya {
    496       1.8  sekiya 	if (ioh == 0)
    497       1.8  sekiya 		delay(5000);
    498       1.8  sekiya 	else
    499       1.8  sekiya 		while (bus_space_read_4(iot, ioh, INT2_LOCAL0_STATUS) & flag)
    500       1.8  sekiya 			;
    501       1.3  sekiya }
    502