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