Home | History | Annotate | Line # | Download | only in dev
int.c revision 1.19.2.1
      1  1.19.2.1    skrll /*	$NetBSD: int.c,v 1.19.2.1 2009/03/03 18:29:13 skrll Exp $	*/
      2       1.1   sekiya 
      3       1.1   sekiya /*
      4  1.19.2.1    skrll  * Copyright (c) 2009 Stephen M. Rumble
      5       1.1   sekiya  * Copyright (c) 2004 Christopher SEKIYA
      6       1.1   sekiya  * All rights reserved.
      7       1.1   sekiya  *
      8       1.1   sekiya  * Redistribution and use in source and binary forms, with or without
      9       1.1   sekiya  * modification, are permitted provided that the following conditions
     10       1.1   sekiya  * are met:
     11       1.1   sekiya  * 1. Redistributions of source code must retain the above copyright
     12       1.1   sekiya  *    notice, this list of conditions and the following disclaimer.
     13       1.1   sekiya  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1   sekiya  *    notice, this list of conditions and the following disclaimer in the
     15       1.1   sekiya  *    documentation and/or other materials provided with the distribution.
     16       1.1   sekiya  * 3. The name of the author may not be used to endorse or promote products
     17       1.1   sekiya  *    derived from this software without specific prior written permission.
     18       1.1   sekiya  *
     19       1.1   sekiya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1   sekiya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1   sekiya  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1   sekiya  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1   sekiya  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.1   sekiya  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.1   sekiya  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.1   sekiya  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.1   sekiya  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.1   sekiya  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1   sekiya  */
     30       1.1   sekiya 
     31       1.1   sekiya /*
     32  1.19.2.1    skrll  * INT1/INT2/INT3 interrupt controllers (IP6, IP10, IP12, IP20, IP22, IP24...)
     33       1.1   sekiya  */
     34       1.1   sekiya 
     35       1.1   sekiya #include <sys/cdefs.h>
     36  1.19.2.1    skrll __KERNEL_RCSID(0, "$NetBSD: int.c,v 1.19.2.1 2009/03/03 18:29:13 skrll Exp $");
     37       1.1   sekiya 
     38       1.1   sekiya #include "opt_cputype.h"
     39       1.1   sekiya 
     40       1.1   sekiya #include <sys/param.h>
     41       1.1   sekiya #include <sys/proc.h>
     42       1.1   sekiya #include <sys/systm.h>
     43      1.14   rumble #include <sys/timetc.h>
     44       1.1   sekiya #include <sys/kernel.h>
     45       1.1   sekiya #include <sys/device.h>
     46       1.8   sekiya #include <sys/malloc.h>
     47       1.1   sekiya 
     48       1.1   sekiya #include <dev/ic/i8253reg.h>
     49       1.1   sekiya #include <machine/sysconf.h>
     50       1.1   sekiya #include <machine/machtype.h>
     51       1.1   sekiya #include <machine/bus.h>
     52       1.1   sekiya #include <mips/locore.h>
     53       1.1   sekiya 
     54       1.1   sekiya #include <mips/cache.h>
     55       1.1   sekiya 
     56  1.19.2.1    skrll #include <sgimips/dev/int1reg.h>
     57       1.1   sekiya #include <sgimips/dev/int2reg.h>
     58       1.3   sekiya #include <sgimips/dev/int2var.h>
     59       1.1   sekiya 
     60       1.1   sekiya static bus_space_handle_t ioh;
     61       1.1   sekiya static bus_space_tag_t iot;
     62       1.1   sekiya 
     63       1.1   sekiya struct int_softc {
     64       1.1   sekiya 	struct device sc_dev;
     65       1.1   sekiya };
     66       1.1   sekiya 
     67       1.1   sekiya 
     68       1.1   sekiya static int	int_match(struct device *, struct cfdata *, void *);
     69       1.1   sekiya static void	int_attach(struct device *, struct device *, void *);
     70  1.19.2.1    skrll static void	int1_local_intr(uint32_t, uint32_t, uint32_t, uint32_t);
     71  1.19.2.1    skrll static void    *int1_intr_establish(int, int, int (*)(void *), void *);
     72  1.19.2.1    skrll static void    *int2_intr_establish(int, int, int (*)(void *), void *);
     73  1.19.2.1    skrll static void 	int2_local0_intr(uint32_t, uint32_t, uint32_t, uint32_t);
     74  1.19.2.1    skrll static void	int2_local1_intr(uint32_t, uint32_t, uint32_t, uint32_t);
     75  1.19.2.1    skrll static int 	int2_mappable_intr(void *);
     76  1.19.2.1    skrll static void    *int2_intr_establish(int, int, int (*)(void *), void *);
     77      1.13   rumble static void	int_8254_cal(void);
     78      1.14   rumble static u_int	int_8254_get_timecount(struct timecounter *);
     79      1.19  tsutsui static void	int_8254_intr0(uint32_t, uint32_t, uint32_t, uint32_t);
     80      1.19  tsutsui static void	int_8254_intr1(uint32_t, uint32_t, uint32_t, uint32_t);
     81      1.13   rumble 
     82      1.13   rumble #ifdef MIPS3
     83  1.19.2.1    skrll static u_long	int2_cpu_freq(struct device *);
     84  1.19.2.1    skrll static u_long	int2_cal_timer(void);
     85      1.13   rumble #endif
     86       1.1   sekiya 
     87      1.14   rumble static struct timecounter int_8254_timecounter = {
     88      1.14   rumble 	int_8254_get_timecount,	/* get_timecount */
     89      1.14   rumble 	0,			/* no poll_pps */
     90      1.14   rumble 	~0u,			/* counter_mask */
     91  1.19.2.1    skrll 	0,			/* frequency; set in int_8254_cal */
     92      1.14   rumble 	"int i8254",		/* name */
     93      1.14   rumble 	100,			/* quality */
     94      1.14   rumble 	NULL,			/* prev */
     95      1.14   rumble 	NULL,			/* next */
     96      1.14   rumble };
     97      1.14   rumble 
     98      1.14   rumble static u_long int_8254_tc_count;
     99      1.14   rumble 
    100       1.1   sekiya CFATTACH_DECL(int, sizeof(struct int_softc),
    101      1.19  tsutsui     int_match, int_attach, NULL, NULL);
    102       1.1   sekiya 
    103       1.1   sekiya static int
    104       1.1   sekiya int_match(struct device *parent, struct cfdata *match, void *aux)
    105       1.1   sekiya {
    106       1.6    pooka 
    107  1.19.2.1    skrll 	switch (mach_type) {
    108  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    109  1.19.2.1    skrll 	case MACH_SGI_IP12:
    110  1.19.2.1    skrll 	case MACH_SGI_IP20:
    111  1.19.2.1    skrll 	case MACH_SGI_IP22:
    112       1.1   sekiya 		return 1;
    113  1.19.2.1    skrll 	}
    114       1.1   sekiya 
    115       1.1   sekiya 	return 0;
    116       1.1   sekiya }
    117       1.1   sekiya 
    118       1.1   sekiya static void
    119       1.1   sekiya int_attach(struct device *parent, struct device *self, void *aux)
    120       1.1   sekiya {
    121      1.19  tsutsui 	uint32_t address;
    122       1.1   sekiya 
    123  1.19.2.1    skrll 	switch (mach_type) {
    124  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    125  1.19.2.1    skrll 		address = INT1_IP6_IP10;
    126  1.19.2.1    skrll 		break;
    127  1.19.2.1    skrll 
    128  1.19.2.1    skrll 	case MACH_SGI_IP12:
    129  1.19.2.1    skrll 		address = INT2_IP12;
    130  1.19.2.1    skrll 		break;
    131  1.19.2.1    skrll 
    132  1.19.2.1    skrll 	case MACH_SGI_IP20:
    133  1.19.2.1    skrll 		address = INT2_IP20;
    134  1.19.2.1    skrll 		break;
    135  1.19.2.1    skrll 
    136  1.19.2.1    skrll 	case MACH_SGI_IP22:
    137       1.1   sekiya 		if (mach_subtype == MACH_SGI_IP22_FULLHOUSE)
    138  1.19.2.1    skrll 			address = INT2_IP22;
    139       1.1   sekiya 		else
    140  1.19.2.1    skrll 			address = INT2_IP24;
    141  1.19.2.1    skrll 		break;
    142  1.19.2.1    skrll 
    143  1.19.2.1    skrll 	default:
    144       1.1   sekiya 		panic("\nint0: passed match, but failed attach?");
    145  1.19.2.1    skrll 	}
    146       1.1   sekiya 
    147      1.16   rumble 	printf(" addr 0x%x\n", address);
    148      1.10  tsutsui 
    149       1.1   sekiya 	bus_space_map(iot, address, 0, 0, &ioh);
    150       1.1   sekiya 	iot = SGIMIPS_BUS_SPACE_NORMAL;
    151       1.1   sekiya 
    152       1.1   sekiya 	switch (mach_type) {
    153  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    154  1.19.2.1    skrll 		/* Clean out interrupt masks */
    155  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_LOCAL_MASK, 0);
    156  1.19.2.1    skrll 
    157  1.19.2.1    skrll 		/* Turn off timers and clear interrupts */
    158  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_TIMER_CONTROL,
    159  1.19.2.1    skrll 		    (TIMER_SEL0 | TIMER_16BIT | TIMER_SWSTROBE));
    160  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_TIMER_CONTROL,
    161  1.19.2.1    skrll 		    (TIMER_SEL1 | TIMER_16BIT | TIMER_SWSTROBE));
    162  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_TIMER_CONTROL,
    163  1.19.2.1    skrll 		    (TIMER_SEL2 | TIMER_16BIT | TIMER_SWSTROBE));
    164  1.19.2.1    skrll 		wbflush();
    165  1.19.2.1    skrll 		delay(4);
    166  1.19.2.1    skrll 		bus_space_read_1(iot, ioh, INT1_TIMER_0_ACK);
    167  1.19.2.1    skrll 		bus_space_read_1(iot, ioh, INT1_TIMER_1_ACK);
    168  1.19.2.1    skrll 
    169  1.19.2.1    skrll 		platform.intr_establish = int1_intr_establish;
    170  1.19.2.1    skrll 		platform.intr1 = int1_local_intr;
    171  1.19.2.1    skrll 		platform.intr2 = int_8254_intr0;
    172      1.19  tsutsui 		platform.intr4 = int_8254_intr1;
    173      1.19  tsutsui 		int_8254_cal();
    174      1.19  tsutsui 		break;
    175  1.19.2.1    skrll 
    176  1.19.2.1    skrll 	case MACH_SGI_IP12:
    177      1.19  tsutsui 	case MACH_SGI_IP20:
    178      1.19  tsutsui 	case MACH_SGI_IP22:
    179  1.19.2.1    skrll 		/* Clean out interrupt masks */
    180  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL0_MASK, 0);
    181  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL1_MASK, 0);
    182  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_MAP_MASK0, 0);
    183  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_MAP_MASK1, 0);
    184  1.19.2.1    skrll 
    185  1.19.2.1    skrll 		/* Reset timer interrupts */
    186  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    187  1.19.2.1    skrll 		    (TIMER_SEL0 | TIMER_16BIT | TIMER_SWSTROBE));
    188  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    189  1.19.2.1    skrll 		    (TIMER_SEL1 | TIMER_16BIT | TIMER_SWSTROBE));
    190  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    191  1.19.2.1    skrll 		    (TIMER_SEL2 | TIMER_16BIT | TIMER_SWSTROBE));
    192  1.19.2.1    skrll 		wbflush();
    193  1.19.2.1    skrll 		delay(4);
    194  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CLEAR, 0x03);
    195  1.19.2.1    skrll 
    196  1.19.2.1    skrll 		if (mach_type == MACH_SGI_IP12) {
    197  1.19.2.1    skrll 			platform.intr_establish = int2_intr_establish;
    198  1.19.2.1    skrll 			platform.intr1 = int2_local0_intr;
    199  1.19.2.1    skrll 			platform.intr2 = int2_local1_intr;
    200  1.19.2.1    skrll 			platform.intr3 = int_8254_intr0;
    201  1.19.2.1    skrll 			platform.intr4 = int_8254_intr1;
    202  1.19.2.1    skrll 			int_8254_cal();
    203  1.19.2.1    skrll 		} else {
    204  1.19.2.1    skrll 			platform.intr_establish = int2_intr_establish;
    205  1.19.2.1    skrll 			platform.intr0 = int2_local0_intr;
    206  1.19.2.1    skrll 			platform.intr1 = int2_local1_intr;
    207  1.19.2.1    skrll #ifdef MIPS3
    208  1.19.2.1    skrll 			curcpu()->ci_cpu_freq = int2_cpu_freq(self);
    209  1.19.2.1    skrll #endif
    210      1.19  tsutsui 		}
    211      1.19  tsutsui 		break;
    212  1.19.2.1    skrll 
    213      1.19  tsutsui 	default:
    214      1.19  tsutsui 		panic("int0: unsupported machine type %i\n", mach_type);
    215       1.1   sekiya 	}
    216       1.1   sekiya 
    217       1.1   sekiya 	curcpu()->ci_cycles_per_hz = curcpu()->ci_cpu_freq / (2 * hz);
    218       1.1   sekiya 	curcpu()->ci_divisor_delay = curcpu()->ci_cpu_freq / (2 * 1000000);
    219       1.1   sekiya 
    220       1.1   sekiya 	if (mach_type == MACH_SGI_IP22) {
    221       1.1   sekiya 		/* Wire interrupts 7, 11 to mappable interrupt 0,1 handlers */
    222  1.19.2.1    skrll 		intrtab[7].ih_fun = int2_mappable_intr;
    223       1.1   sekiya 		intrtab[7].ih_arg = (void*) 0;
    224       1.1   sekiya 
    225  1.19.2.1    skrll 		intrtab[11].ih_fun = int2_mappable_intr;
    226       1.1   sekiya 		intrtab[11].ih_arg = (void*) 1;
    227       1.1   sekiya 	}
    228       1.1   sekiya }
    229       1.1   sekiya 
    230       1.1   sekiya int
    231  1.19.2.1    skrll int2_mappable_intr(void *arg)
    232       1.1   sekiya {
    233       1.1   sekiya 	int i;
    234       1.1   sekiya 	int ret;
    235       1.1   sekiya 	int intnum;
    236      1.19  tsutsui 	uint32_t mstat;
    237      1.19  tsutsui 	uint32_t mmask;
    238       1.1   sekiya 	int which = (int)arg;
    239       1.8   sekiya 	struct sgimips_intrhand *ih;
    240       1.1   sekiya 
    241       1.1   sekiya 	ret = 0;
    242  1.19.2.1    skrll 	mstat = bus_space_read_1(iot, ioh, INT2_MAP_STATUS);
    243  1.19.2.1    skrll 	mmask = bus_space_read_1(iot, ioh, INT2_MAP_MASK0 + (which << 2));
    244       1.1   sekiya 
    245       1.1   sekiya 	mstat &= mmask;
    246       1.1   sekiya 
    247       1.1   sekiya 	for (i = 0; i < 8; i++) {
    248       1.1   sekiya 		intnum = i + 16 + (which << 3);
    249       1.1   sekiya 		if (mstat & (1 << i)) {
    250       1.8   sekiya 			for (ih = &intrtab[intnum]; ih != NULL;
    251      1.19  tsutsui 			    ih = ih->ih_next) {
    252       1.8   sekiya 				if (ih->ih_fun != NULL)
    253       1.8   sekiya 					ret |= (ih->ih_fun)(ih->ih_arg);
    254       1.8   sekiya 				else
    255       1.8   sekiya 					printf("int0: unexpected mapped "
    256       1.8   sekiya 					       "interrupt %d\n", intnum);
    257       1.8   sekiya 			}
    258       1.1   sekiya 		}
    259       1.1   sekiya 	}
    260       1.1   sekiya 
    261       1.1   sekiya 	return ret;
    262       1.1   sekiya }
    263       1.1   sekiya 
    264  1.19.2.1    skrll static void
    265  1.19.2.1    skrll int1_local_intr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipend)
    266  1.19.2.1    skrll {
    267  1.19.2.1    skrll 	int i;
    268  1.19.2.1    skrll 	uint16_t stat;
    269  1.19.2.1    skrll 	uint8_t  mask;
    270  1.19.2.1    skrll 	struct sgimips_intrhand *ih;
    271  1.19.2.1    skrll 
    272  1.19.2.1    skrll 	stat = bus_space_read_2(iot, ioh, INT1_LOCAL_STATUS);
    273  1.19.2.1    skrll 	mask = bus_space_read_1(iot, ioh, INT1_LOCAL_MASK);
    274  1.19.2.1    skrll 
    275  1.19.2.1    skrll 	/* for STATUS, a 0 bit means interrupt is pending */
    276  1.19.2.1    skrll 	stat = ~stat & mask;
    277  1.19.2.1    skrll 
    278  1.19.2.1    skrll 	for (i = 0; i < 16; i++) {
    279  1.19.2.1    skrll 		if (stat & (1 << i)) {
    280  1.19.2.1    skrll 			for (ih = &intrtab[i]; ih != NULL; ih = ih->ih_next) {
    281  1.19.2.1    skrll 				if (ih->ih_fun != NULL)
    282  1.19.2.1    skrll 					(ih->ih_fun)(ih->ih_arg);
    283  1.19.2.1    skrll 				else
    284  1.19.2.1    skrll 					printf("int0: unexpected local "
    285  1.19.2.1    skrll 					       "interrupt %d\n", i);
    286  1.19.2.1    skrll 			}
    287  1.19.2.1    skrll 		}
    288  1.19.2.1    skrll 	}
    289  1.19.2.1    skrll }
    290  1.19.2.1    skrll 
    291       1.1   sekiya void
    292  1.19.2.1    skrll int2_local0_intr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipend)
    293       1.1   sekiya {
    294       1.1   sekiya 	int i;
    295      1.19  tsutsui 	uint32_t l0stat;
    296      1.19  tsutsui 	uint32_t l0mask;
    297       1.8   sekiya 	struct sgimips_intrhand *ih;
    298       1.1   sekiya 
    299  1.19.2.1    skrll 	l0stat = bus_space_read_1(iot, ioh, INT2_LOCAL0_STATUS);
    300  1.19.2.1    skrll 	l0mask = bus_space_read_1(iot, ioh, INT2_LOCAL0_MASK);
    301       1.1   sekiya 
    302      1.12   rumble 	l0stat &= l0mask;
    303       1.1   sekiya 
    304       1.1   sekiya 	for (i = 0; i < 8; i++) {
    305      1.12   rumble 		if (l0stat & (1 << i)) {
    306       1.8   sekiya 			for (ih = &intrtab[i]; ih != NULL; ih = ih->ih_next) {
    307       1.8   sekiya 				if (ih->ih_fun != NULL)
    308       1.8   sekiya 					(ih->ih_fun)(ih->ih_arg);
    309       1.8   sekiya 				else
    310       1.8   sekiya 					printf("int0: unexpected local0 "
    311       1.8   sekiya 					       "interrupt %d\n", i);
    312       1.8   sekiya 			}
    313       1.1   sekiya 		}
    314       1.1   sekiya 	}
    315       1.1   sekiya }
    316       1.1   sekiya 
    317       1.1   sekiya void
    318  1.19.2.1    skrll int2_local1_intr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipend)
    319       1.1   sekiya {
    320       1.1   sekiya 	int i;
    321      1.19  tsutsui 	uint32_t l1stat;
    322      1.19  tsutsui 	uint32_t l1mask;
    323       1.8   sekiya 	struct sgimips_intrhand *ih;
    324       1.1   sekiya 
    325  1.19.2.1    skrll 	l1stat = bus_space_read_1(iot, ioh, INT2_LOCAL1_STATUS);
    326  1.19.2.1    skrll 	l1mask = bus_space_read_1(iot, ioh, INT2_LOCAL1_MASK);
    327       1.1   sekiya 
    328       1.1   sekiya 	l1stat &= l1mask;
    329       1.1   sekiya 
    330       1.1   sekiya 	for (i = 0; i < 8; i++) {
    331       1.1   sekiya 		if (l1stat & (1 << i)) {
    332       1.8   sekiya 			for (ih = &intrtab[8+i]; ih != NULL; ih = ih->ih_next) {
    333       1.8   sekiya 				if (ih->ih_fun != NULL)
    334       1.8   sekiya 					(ih->ih_fun)(ih->ih_arg);
    335       1.8   sekiya 				else
    336       1.8   sekiya 					printf("int0: unexpected local1 "
    337       1.8   sekiya 					       " interrupt %x\n", 8 + i);
    338       1.8   sekiya 			}
    339       1.1   sekiya 		}
    340       1.1   sekiya 	}
    341       1.1   sekiya }
    342       1.1   sekiya 
    343       1.1   sekiya void *
    344  1.19.2.1    skrll int1_intr_establish(int level, int ipl, int (*handler) (void *), void *arg)
    345  1.19.2.1    skrll {
    346  1.19.2.1    skrll 	uint8_t mask;
    347  1.19.2.1    skrll 
    348  1.19.2.1    skrll 	if (level < 0 || level >= NINTR)
    349  1.19.2.1    skrll 		panic("invalid interrupt level");
    350  1.19.2.1    skrll 
    351  1.19.2.1    skrll 	if (intrtab[level].ih_fun == NULL) {
    352  1.19.2.1    skrll 		intrtab[level].ih_fun = handler;
    353  1.19.2.1    skrll 		intrtab[level].ih_arg = arg;
    354  1.19.2.1    skrll 		intrtab[level].ih_next = NULL;
    355  1.19.2.1    skrll 	} else {
    356  1.19.2.1    skrll 		struct sgimips_intrhand *n, *ih;
    357  1.19.2.1    skrll 
    358  1.19.2.1    skrll 		ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
    359  1.19.2.1    skrll 		if (ih == NULL) {
    360  1.19.2.1    skrll 			printf("int0: can't allocate handler\n");
    361  1.19.2.1    skrll 			return (void *)NULL;
    362  1.19.2.1    skrll 		}
    363  1.19.2.1    skrll 
    364  1.19.2.1    skrll 		ih->ih_fun = handler;
    365  1.19.2.1    skrll 		ih->ih_arg = arg;
    366  1.19.2.1    skrll 		ih->ih_next = NULL;
    367  1.19.2.1    skrll 
    368  1.19.2.1    skrll 		for (n = &intrtab[level]; n->ih_next != NULL; n = n->ih_next)
    369  1.19.2.1    skrll 			;
    370  1.19.2.1    skrll 
    371  1.19.2.1    skrll 		n->ih_next = ih;
    372  1.19.2.1    skrll 
    373  1.19.2.1    skrll 		return NULL;	/* vector already set */
    374  1.19.2.1    skrll 	}
    375  1.19.2.1    skrll 
    376  1.19.2.1    skrll 	if (level < 8) {
    377  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT1_LOCAL_MASK);
    378  1.19.2.1    skrll 		mask |= (1 << level);
    379  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_LOCAL_MASK, mask);
    380  1.19.2.1    skrll 	} else {
    381  1.19.2.1    skrll 		printf("int0: level >= 16 (%d)\n", level);
    382  1.19.2.1    skrll 	}
    383  1.19.2.1    skrll 
    384  1.19.2.1    skrll 	return NULL;
    385  1.19.2.1    skrll }
    386  1.19.2.1    skrll 
    387  1.19.2.1    skrll void *
    388  1.19.2.1    skrll int2_intr_establish(int level, int ipl, int (*handler) (void *), void *arg)
    389       1.1   sekiya {
    390      1.19  tsutsui 	uint32_t mask;
    391       1.1   sekiya 
    392       1.1   sekiya 	if (level < 0 || level >= NINTR)
    393       1.1   sekiya 		panic("invalid interrupt level");
    394       1.1   sekiya 
    395       1.8   sekiya 	if (intrtab[level].ih_fun == NULL) {
    396       1.8   sekiya 		intrtab[level].ih_fun = handler;
    397       1.8   sekiya 		intrtab[level].ih_arg = arg;
    398       1.8   sekiya 		intrtab[level].ih_next = NULL;
    399       1.8   sekiya 	} else {
    400      1.19  tsutsui 		struct sgimips_intrhand *n, *ih;
    401       1.8   sekiya 
    402      1.19  tsutsui 		ih = malloc(sizeof *ih, M_DEVBUF, M_NOWAIT);
    403       1.8   sekiya 		if (ih == NULL) {
    404  1.19.2.1    skrll 			printf("int0: can't allocate handler\n");
    405      1.19  tsutsui 			return NULL;
    406       1.8   sekiya 		}
    407       1.8   sekiya 
    408       1.8   sekiya 		ih->ih_fun = handler;
    409       1.8   sekiya 		ih->ih_arg = arg;
    410       1.8   sekiya 		ih->ih_next = NULL;
    411       1.8   sekiya 
    412       1.8   sekiya 		for (n = &intrtab[level]; n->ih_next != NULL; n = n->ih_next)
    413      1.10  tsutsui 			;
    414      1.10  tsutsui 
    415       1.8   sekiya 		n->ih_next = ih;
    416       1.8   sekiya 
    417      1.19  tsutsui 		return NULL;	/* vector already set */
    418       1.1   sekiya 	}
    419       1.1   sekiya 
    420       1.1   sekiya 	if (level < 8) {
    421  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_LOCAL0_MASK);
    422       1.1   sekiya 		mask |= (1 << level);
    423  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL0_MASK, mask);
    424       1.1   sekiya 	} else if (level < 16) {
    425  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_LOCAL1_MASK);
    426       1.1   sekiya 		mask |= (1 << (level - 8));
    427  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL1_MASK, mask);
    428       1.1   sekiya 	} else if (level < 24) {
    429       1.1   sekiya 		/* Map0 interrupt maps to l0 bit 7, so turn that on too */
    430  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_LOCAL0_MASK);
    431       1.1   sekiya 		mask |= (1 << 7);
    432  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL0_MASK, mask);
    433       1.1   sekiya 
    434  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_MAP_MASK0);
    435       1.1   sekiya 		mask |= (1 << (level - 16));
    436  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_MAP_MASK0, mask);
    437       1.1   sekiya 	} else {
    438       1.1   sekiya 		/* Map1 interrupt maps to l1 bit 3, so turn that on too */
    439  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_LOCAL1_MASK);
    440       1.1   sekiya 		mask |= (1 << 3);
    441  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_LOCAL1_MASK, mask);
    442       1.1   sekiya 
    443  1.19.2.1    skrll 		mask = bus_space_read_1(iot, ioh, INT2_MAP_MASK1);
    444       1.1   sekiya 		mask |= (1 << (level - 24));
    445  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_MAP_MASK1, mask);
    446       1.1   sekiya 	}
    447       1.1   sekiya 
    448      1.19  tsutsui 	return NULL;
    449       1.1   sekiya }
    450       1.1   sekiya 
    451       1.4    pooka #ifdef MIPS3
    452      1.13   rumble static u_long
    453  1.19.2.1    skrll int2_cpu_freq(struct device *self)
    454  1.19.2.1    skrll {
    455  1.19.2.1    skrll 	int i;
    456  1.19.2.1    skrll 	unsigned long cps;
    457  1.19.2.1    skrll 	unsigned long ctrdiff[3];
    458  1.19.2.1    skrll 
    459  1.19.2.1    skrll 	/* calibrate timer */
    460  1.19.2.1    skrll 	int2_cal_timer();
    461  1.19.2.1    skrll 
    462  1.19.2.1    skrll 	cps = 0;
    463  1.19.2.1    skrll 	for (i = 0;
    464  1.19.2.1    skrll 	    i < sizeof(ctrdiff) / sizeof(ctrdiff[0]); i++) {
    465  1.19.2.1    skrll 		do {
    466  1.19.2.1    skrll 			ctrdiff[i] = int2_cal_timer();
    467  1.19.2.1    skrll 		} while (ctrdiff[i] == 0);
    468  1.19.2.1    skrll 
    469  1.19.2.1    skrll 		cps += ctrdiff[i];
    470  1.19.2.1    skrll 	}
    471  1.19.2.1    skrll 
    472  1.19.2.1    skrll 	cps = cps / (sizeof(ctrdiff) / sizeof(ctrdiff[0]));
    473  1.19.2.1    skrll 
    474  1.19.2.1    skrll 	printf("%s: bus %luMHz, CPU %luMHz\n",
    475  1.19.2.1    skrll 	    self->dv_xname, cps / 10000, cps / 5000);
    476  1.19.2.1    skrll 
    477  1.19.2.1    skrll 	/* R4k/R4400/R4600/R5k count at half CPU frequency */
    478  1.19.2.1    skrll 	return (2 * cps * hz);
    479  1.19.2.1    skrll }
    480  1.19.2.1    skrll 
    481  1.19.2.1    skrll static u_long
    482  1.19.2.1    skrll int2_cal_timer(void)
    483       1.1   sekiya {
    484       1.1   sekiya 	int s;
    485       1.1   sekiya 	int roundtime;
    486       1.1   sekiya 	int sampletime;
    487       1.1   sekiya 	int startmsb, lsb, msb;
    488       1.1   sekiya 	unsigned long startctr, endctr;
    489       1.1   sekiya 
    490       1.1   sekiya 	/*
    491       1.1   sekiya 	 * NOTE: HZ must be greater than 15 for this to work, as otherwise
    492  1.19.2.1    skrll 	 * we'll overflow the counter.  We round the answer to nearest 1
    493       1.1   sekiya 	 * MHz of the master (2x) clock.
    494       1.1   sekiya 	 */
    495       1.1   sekiya 	roundtime = (1000000 / hz) / 2;
    496       1.1   sekiya 	sampletime = (1000000 / hz) + 0xff;
    497       1.1   sekiya 	startmsb = (sampletime >> 8);
    498       1.1   sekiya 
    499       1.1   sekiya 	s = splhigh();
    500       1.1   sekiya 
    501  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    502      1.19  tsutsui 	    (TIMER_SEL2 | TIMER_16BIT | TIMER_RATEGEN));
    503  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, INT2_TIMER_2, (sampletime & 0xff));
    504  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, INT2_TIMER_2, (sampletime >> 8));
    505       1.1   sekiya 
    506       1.1   sekiya 	startctr = mips3_cp0_count_read();
    507       1.1   sekiya 
    508       1.1   sekiya 	/* Wait for the MSB to count down to zero */
    509       1.1   sekiya 	do {
    510  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL, TIMER_SEL2);
    511  1.19.2.1    skrll 		lsb = bus_space_read_1(iot, ioh, INT2_TIMER_2) & 0xff;
    512  1.19.2.1    skrll 		msb = bus_space_read_1(iot, ioh, INT2_TIMER_2) & 0xff;
    513       1.1   sekiya 
    514       1.1   sekiya 		endctr = mips3_cp0_count_read();
    515       1.1   sekiya 	} while (msb);
    516       1.1   sekiya 
    517       1.1   sekiya 	/* Turn off timer */
    518  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    519      1.19  tsutsui 	    (TIMER_SEL2 | TIMER_16BIT | TIMER_SWSTROBE));
    520       1.1   sekiya 
    521       1.1   sekiya 	splx(s);
    522       1.1   sekiya 
    523       1.1   sekiya 	return (endctr - startctr) / roundtime * roundtime;
    524       1.1   sekiya }
    525       1.4    pooka #endif /* MIPS3 */
    526       1.1   sekiya 
    527      1.14   rumble /*
    528  1.19.2.1    skrll  * A master clock is wired to TIMER_2, which in turn clocks the two other
    529  1.19.2.1    skrll  * timers. The master frequencies are as follows:
    530  1.19.2.1    skrll  *     IP6,  IP10:		3.6864MHz
    531  1.19.2.1    skrll  *     IP12, IP20, IP22:	1MHz
    532  1.19.2.1    skrll  *     IP17:			10MHz
    533      1.14   rumble  *
    534  1.19.2.1    skrll  * TIMER_0 and TIMER_1 interrupts are tied to MIPS interrupts as follows:
    535  1.19.2.1    skrll  *     IP6,  IP10:		TIMER_0: INT2, TIMER_1: INT4
    536  1.19.2.1    skrll  *     IP12:			TIMER_0: INT3, TIMER_1: INT4
    537  1.19.2.1    skrll  *     IP17, IP20, IP22:	TIMER_0: INT2, TIMER_1: INT3
    538  1.19.2.1    skrll  *
    539  1.19.2.1    skrll  * NB: Apparently int2 doesn't like counting down from one, but two works.
    540      1.14   rumble  */
    541       1.1   sekiya void
    542       1.1   sekiya int_8254_cal(void)
    543       1.1   sekiya {
    544  1.19.2.1    skrll 	bus_size_t timer_control, timer_0, timer_1, timer_2;
    545       1.1   sekiya 	int s;
    546       1.1   sekiya 
    547  1.19.2.1    skrll 	switch (mach_type) {
    548  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    549  1.19.2.1    skrll 		int_8254_timecounter.tc_frequency = 3686400 / 8;
    550  1.19.2.1    skrll 		timer_control	= INT1_TIMER_CONTROL;
    551  1.19.2.1    skrll 		timer_0		= INT1_TIMER_0;
    552  1.19.2.1    skrll 		timer_1		= INT1_TIMER_1;
    553  1.19.2.1    skrll 		timer_2		= INT1_TIMER_2;
    554  1.19.2.1    skrll 		break;
    555  1.19.2.1    skrll 
    556  1.19.2.1    skrll 	case MACH_SGI_IP12:
    557  1.19.2.1    skrll 		int_8254_timecounter.tc_frequency = 1000000 / 8;
    558  1.19.2.1    skrll 		timer_control	= INT2_TIMER_CONTROL;
    559  1.19.2.1    skrll 		timer_0		= INT2_TIMER_0;
    560  1.19.2.1    skrll 		timer_1		= INT2_TIMER_1;
    561  1.19.2.1    skrll 		timer_2		= INT2_TIMER_2;
    562  1.19.2.1    skrll 		break;
    563  1.19.2.1    skrll 
    564  1.19.2.1    skrll 	default:
    565  1.19.2.1    skrll 		panic("int_8254_cal");
    566  1.19.2.1    skrll 	}
    567  1.19.2.1    skrll 
    568       1.1   sekiya 	s = splhigh();
    569       1.1   sekiya 
    570  1.19.2.1    skrll 	/* Timer0 is our hz. */
    571  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_control,
    572  1.19.2.1    skrll 	    TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
    573  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_0,
    574  1.19.2.1    skrll 	    (int_8254_timecounter.tc_frequency / hz) % 256);
    575      1.14   rumble 	wbflush();
    576      1.14   rumble 	delay(4);
    577  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_0,
    578  1.19.2.1    skrll 	    (int_8254_timecounter.tc_frequency / hz) / 256);
    579      1.14   rumble 
    580  1.19.2.1    skrll 	/* Timer1 is for timecounting. */
    581  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_control,
    582  1.19.2.1    skrll 	    TIMER_SEL1 | TIMER_RATEGEN | TIMER_16BIT);
    583  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_1, 0xff);
    584       1.1   sekiya 	wbflush();
    585       1.1   sekiya 	delay(4);
    586  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_1, 0xff);
    587       1.1   sekiya 
    588  1.19.2.1    skrll 	/* Timer2 clocks timer0 and timer1. */
    589  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_control,
    590  1.19.2.1    skrll 	    TIMER_SEL2 | TIMER_RATEGEN | TIMER_16BIT);
    591  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_2, 8);
    592       1.1   sekiya 	wbflush();
    593       1.1   sekiya 	delay(4);
    594  1.19.2.1    skrll 	bus_space_write_1(iot, ioh, timer_2, 0);
    595      1.14   rumble 
    596      1.14   rumble 	splx(s);
    597      1.14   rumble 
    598  1.19.2.1    skrll 	tc_init(&int_8254_timecounter);
    599  1.19.2.1    skrll }
    600      1.14   rumble 
    601      1.14   rumble static u_int
    602      1.14   rumble int_8254_get_timecount(struct timecounter *tc)
    603      1.14   rumble {
    604      1.14   rumble 	int s;
    605      1.14   rumble 	u_int count;
    606  1.19.2.1    skrll 	u_char lo, hi;
    607      1.14   rumble 
    608      1.14   rumble 	s = splhigh();
    609      1.14   rumble 
    610  1.19.2.1    skrll 	switch (mach_type) {
    611  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    612  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT1_TIMER_CONTROL,
    613  1.19.2.1    skrll 		    TIMER_SEL1 | TIMER_LATCH);
    614  1.19.2.1    skrll 		lo = bus_space_read_1(iot, ioh, INT1_TIMER_1);
    615  1.19.2.1    skrll 		hi = bus_space_read_1(iot, ioh, INT1_TIMER_1);
    616  1.19.2.1    skrll 		break;
    617  1.19.2.1    skrll 
    618  1.19.2.1    skrll 	case MACH_SGI_IP12:
    619  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CONTROL,
    620  1.19.2.1    skrll 		    TIMER_SEL1 | TIMER_LATCH);
    621  1.19.2.1    skrll 		lo = bus_space_read_1(iot, ioh, INT2_TIMER_1);
    622  1.19.2.1    skrll 		hi = bus_space_read_1(iot, ioh, INT2_TIMER_1);
    623  1.19.2.1    skrll 		break;
    624      1.14   rumble 
    625  1.19.2.1    skrll 	default:
    626  1.19.2.1    skrll 		panic("int_8254_get_timecount");
    627  1.19.2.1    skrll 	}
    628  1.19.2.1    skrll 
    629  1.19.2.1    skrll 	count = 0xffff - ((hi << 8) | lo);
    630      1.14   rumble 	splx(s);
    631      1.14   rumble 
    632  1.19.2.1    skrll 	return (int_8254_tc_count + count);
    633      1.14   rumble }
    634      1.14   rumble 
    635      1.14   rumble static void
    636  1.19.2.1    skrll int_8254_intr0(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipend)
    637      1.15   rumble {
    638      1.15   rumble 	struct clockframe cf;
    639      1.15   rumble 
    640      1.15   rumble 	cf.pc = pc;
    641      1.15   rumble 	cf.sr = status;
    642      1.15   rumble 
    643      1.15   rumble 	hardclock(&cf);
    644      1.15   rumble 
    645  1.19.2.1    skrll 	switch (mach_type) {
    646  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    647  1.19.2.1    skrll 		bus_space_read_1(iot, ioh, INT1_TIMER_0_ACK);
    648  1.19.2.1    skrll 		break;
    649      1.15   rumble 
    650  1.19.2.1    skrll 	case MACH_SGI_IP12:
    651  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CLEAR, 0x01);
    652  1.19.2.1    skrll 		break;
    653  1.19.2.1    skrll 
    654  1.19.2.1    skrll 	default:
    655  1.19.2.1    skrll 		panic("int_8254_intr0");
    656  1.19.2.1    skrll 	}
    657  1.19.2.1    skrll }
    658      1.15   rumble 
    659      1.15   rumble static void
    660  1.19.2.1    skrll int_8254_intr1(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipend)
    661      1.14   rumble {
    662      1.14   rumble 	int s;
    663      1.14   rumble 
    664      1.14   rumble 	s = splhigh();
    665      1.14   rumble 
    666      1.14   rumble 	int_8254_tc_count += 0xffff;
    667  1.19.2.1    skrll 	switch (mach_type) {
    668  1.19.2.1    skrll 	case MACH_SGI_IP6 | MACH_SGI_IP10:
    669  1.19.2.1    skrll 		bus_space_read_1(iot, ioh, INT1_TIMER_1_ACK);
    670  1.19.2.1    skrll 		break;
    671  1.19.2.1    skrll 
    672  1.19.2.1    skrll 	case MACH_SGI_IP12:
    673  1.19.2.1    skrll 		bus_space_write_1(iot, ioh, INT2_TIMER_CLEAR, 0x02);
    674  1.19.2.1    skrll 		break;
    675  1.19.2.1    skrll 
    676  1.19.2.1    skrll 	default:
    677  1.19.2.1    skrll 		panic("int_8254_intr1");
    678  1.19.2.1    skrll 	}
    679      1.14   rumble 
    680       1.1   sekiya 	splx(s);
    681       1.1   sekiya }
    682       1.3   sekiya 
    683       1.3   sekiya void
    684      1.19  tsutsui int2_wait_fifo(uint32_t flag)
    685       1.3   sekiya {
    686      1.19  tsutsui 
    687       1.8   sekiya 	if (ioh == 0)
    688       1.8   sekiya 		delay(5000);
    689       1.8   sekiya 	else
    690  1.19.2.1    skrll 		while (bus_space_read_1(iot, ioh, INT2_LOCAL0_STATUS) & flag)
    691       1.8   sekiya 			;
    692       1.3   sekiya }
    693