Home | History | Annotate | Line # | Download | only in malta
malta_intr.c revision 1.14
      1 /*	$NetBSD: malta_intr.c,v 1.14 2007/05/17 14:51:18 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Platform-specific interrupt support for the MIPS Malta.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: malta_intr.c,v 1.14 2007/05/17 14:51:18 yamt Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/device.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/systm.h>
     50 #include <sys/cpu.h>
     51 
     52 #include <mips/locore.h>
     53 
     54 #include <evbmips/malta/maltavar.h>
     55 #include <evbmips/malta/pci/pcibvar.h>
     56 
     57 #include <dev/ic/mc146818reg.h>		/* for malta_cal_timer() */
     58 
     59 #include <dev/isa/isavar.h>
     60 #include <dev/pci/pciidereg.h>
     61 
     62 /*
     63  * This is a mask of bits to clear in the SR when we go to a
     64  * given hardware interrupt priority level.
     65  */
     66 const uint32_t ipl_sr_bits[_IPL_N] = {
     67 	0,					/*  0: IPL_NONE */
     68 
     69 	MIPS_SOFT_INT_MASK_0,			/*  1: IPL_SOFT */
     70 
     71 	MIPS_SOFT_INT_MASK_0,			/*  2: IPL_SOFTCLOCK */
     72 
     73 	MIPS_SOFT_INT_MASK_0|
     74 		MIPS_SOFT_INT_MASK_1,		/*  3: IPL_SOFTNET */
     75 
     76 	MIPS_SOFT_INT_MASK_0|
     77 		MIPS_SOFT_INT_MASK_1,		/*  4: IPL_SOFTSERIAL */
     78 
     79 	MIPS_SOFT_INT_MASK_0|
     80 		MIPS_SOFT_INT_MASK_1|
     81 		MIPS_INT_MASK_0,		/*  5: IPL_BIO */
     82 
     83 	MIPS_SOFT_INT_MASK_0|
     84 		MIPS_SOFT_INT_MASK_1|
     85 		MIPS_INT_MASK_0,		/*  6: IPL_NET */
     86 
     87 	MIPS_SOFT_INT_MASK_0|
     88 		MIPS_SOFT_INT_MASK_1|
     89 		MIPS_INT_MASK_0,		/*  7: IPL_{TTY,SERIAL} */
     90 
     91 	MIPS_SOFT_INT_MASK_0|
     92 		MIPS_SOFT_INT_MASK_1|
     93 		MIPS_INT_MASK_0|
     94 		MIPS_INT_MASK_1|
     95 		MIPS_INT_MASK_2|
     96 		MIPS_INT_MASK_3|
     97 		MIPS_INT_MASK_4|
     98 		MIPS_INT_MASK_5,		/*  8: IPL_{CLOCK,HIGH} */
     99 };
    100 
    101 /*
    102  * This is a mask of bits to clear in the SR when we go to a
    103  * given software interrupt priority level.
    104  * Hardware ipls are port/board specific.
    105  */
    106 const uint32_t mips_ipl_si_to_sr[SI_NQUEUES] = {
    107 	[SI_SOFT] = MIPS_SOFT_INT_MASK_0,
    108 	[SI_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
    109 	[SI_SOFTNET] = MIPS_SOFT_INT_MASK_1,
    110 	[SI_SOFTSERIAL] = MIPS_SOFT_INT_MASK_1,
    111 };
    112 
    113 struct malta_cpuintr {
    114 	LIST_HEAD(, evbmips_intrhand) cintr_list;
    115 	struct evcnt cintr_count;
    116 };
    117 #define	NINTRS		5	/* MIPS INT0 - INT4 */
    118 
    119 struct malta_cpuintr malta_cpuintrs[NINTRS];
    120 const char *malta_cpuintrnames[NINTRS] = {
    121 	"int 0 (piix4)",
    122 	"int 1 (smi)",
    123 	"int 2 (uart)",
    124 	"int 3 (core hi/gt64120)",
    125 	"int 4 (core lo)",
    126 };
    127 
    128 static int	malta_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
    129 static const char
    130 		*malta_pci_intr_string(void *, pci_intr_handle_t);
    131 static const struct evcnt
    132 		*malta_pci_intr_evcnt(void *, pci_intr_handle_t);
    133 static void	*malta_pci_intr_establish(void *, pci_intr_handle_t, int,
    134 		    int (*)(void *), void *);
    135 static void	malta_pci_intr_disestablish(void *, void *);
    136 static void	malta_pci_conf_interrupt(void *, int, int, int, int, int *);
    137 static void	*malta_pciide_compat_intr_establish(void *, struct device *,
    138 		    struct pci_attach_args *, int, int (*)(void *), void *);
    139 
    140 void
    141 evbmips_intr_init(void)
    142 {
    143 	struct malta_config *mcp = &malta_configuration;
    144 	int i;
    145 
    146 	for (i = 0; i < NINTRS; i++) {
    147 		LIST_INIT(&malta_cpuintrs[i].cintr_list);
    148 		evcnt_attach_dynamic(&malta_cpuintrs[i].cintr_count,
    149 		    EVCNT_TYPE_INTR, NULL, "mips", malta_cpuintrnames[i]);
    150 	}
    151 
    152 	mcp->mc_pc.pc_intr_v = NULL;
    153 	mcp->mc_pc.pc_intr_map = malta_pci_intr_map;
    154 	mcp->mc_pc.pc_intr_string = malta_pci_intr_string;
    155 	mcp->mc_pc.pc_intr_evcnt = malta_pci_intr_evcnt;
    156 	mcp->mc_pc.pc_intr_establish = malta_pci_intr_establish;
    157 	mcp->mc_pc.pc_intr_disestablish = malta_pci_intr_disestablish;
    158 	mcp->mc_pc.pc_conf_interrupt = malta_pci_conf_interrupt;
    159 	mcp->mc_pc.pc_pciide_compat_intr_establish =
    160 	    malta_pciide_compat_intr_establish;
    161 }
    162 
    163 void
    164 malta_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
    165 {
    166 	uint32_t ctrdiff[4], startctr, endctr;
    167 	uint8_t regc;
    168 	int i;
    169 
    170 	/* Disable interrupts first. */
    171 	bus_space_write_1(st, sh, 0, MC_REGB);
    172 	bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
    173 	    MC_REGB_24HR);
    174 
    175 	/* Initialize for 16Hz. */
    176 	bus_space_write_1(st, sh, 0, MC_REGA);
    177 	bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
    178 
    179 	/* Run the loop an extra time to prime the cache. */
    180 	for (i = 0; i < 4; i++) {
    181 		// led_display('h', 'z', '0' + i, ' ');
    182 
    183 		/* Enable the interrupt. */
    184 		bus_space_write_1(st, sh, 0, MC_REGB);
    185 		bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
    186 		    MC_REGB_BINARY | MC_REGB_24HR);
    187 
    188 		/* Go to REGC. */
    189 		bus_space_write_1(st, sh, 0, MC_REGC);
    190 
    191 		/* Wait for it to happen. */
    192 		startctr = mips3_cp0_count_read();
    193 		do {
    194 			regc = bus_space_read_1(st, sh, 1);
    195 			endctr = mips3_cp0_count_read();
    196 		} while ((regc & MC_REGC_IRQF) == 0);
    197 
    198 		/* Already ACK'd. */
    199 
    200 		/* Disable. */
    201 		bus_space_write_1(st, sh, 0, MC_REGB);
    202 		bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
    203 		    MC_REGB_24HR);
    204 
    205 		ctrdiff[i] = endctr - startctr;
    206 	}
    207 
    208 	/* Compute the number of cycles per second. */
    209 	curcpu()->ci_cpu_freq = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16/*Hz*/;
    210 
    211 	/* Compute the number of ticks for hz. */
    212 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    213 
    214 	/* Compute the delay divisor and reciprical. */
    215 	curcpu()->ci_divisor_delay =
    216 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    217 	MIPS_SET_CI_RECIPRICAL(curcpu());
    218 
    219 	/*
    220 	 * Get correct cpu frequency if the CPU runs at twice the
    221 	 * external/cp0-count frequency.
    222 	 */
    223 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    224 		curcpu()->ci_cpu_freq *= 2;
    225 
    226 #ifdef DEBUG
    227 	printf("Timer calibration: %lu cycles/sec [(%u, %u) * 16]\n",
    228 	    curcpu()->ci_cpu_freq, ctrdiff[2], ctrdiff[3]);
    229 #endif
    230 }
    231 
    232 void *
    233 evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
    234 {
    235 	struct evbmips_intrhand *ih;
    236 	int s;
    237 
    238 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    239 	if (ih == NULL)
    240 		return (NULL);
    241 
    242 	ih->ih_func = func;
    243 	ih->ih_arg = arg;
    244 
    245 	s = splhigh();
    246 
    247 	/*
    248 	 * Link it into the tables.
    249 	 */
    250 	LIST_INSERT_HEAD(&malta_cpuintrs[0].cintr_list, ih, ih_q);
    251 
    252 	/* XXX - should check that MIPS_INT_MASK_0 is set... */
    253 
    254 	splx(s);
    255 
    256 	return (ih);
    257 }
    258 
    259 void
    260 evbmips_intr_disestablish(void *arg)
    261 {
    262 	struct evbmips_intrhand *ih = arg;
    263 	int s;
    264 
    265 	s = splhigh();
    266 
    267 	/*
    268 	 * First, remove it from the table.
    269 	 */
    270 	LIST_REMOVE(ih, ih_q);
    271 
    272 	/* XXX - disable MIPS_INT_MASK_0 if list is empty? */
    273 
    274 	splx(s);
    275 
    276 	free(ih, M_DEVBUF);
    277 }
    278 
    279 void
    280 evbmips_iointr(uint32_t status, uint32_t cause, uint32_t pc, uint32_t ipending)
    281 {
    282 	struct evbmips_intrhand *ih;
    283 
    284 	/* Check for error interrupts (SMI, GT64120) */
    285 	if (ipending & (MIPS_INT_MASK_1 | MIPS_INT_MASK_3)) {
    286 		if (ipending & MIPS_INT_MASK_1)
    287 			panic("piix4 SMI interrupt");
    288 		if (ipending & MIPS_INT_MASK_3)
    289 			panic("gt64120 error interrupt");
    290 	}
    291 
    292 	/*
    293 	 * Read the interrupt pending registers, mask them with the
    294 	 * ones we have enabled, and service them in order of decreasing
    295 	 * priority.
    296 	 */
    297 	if (ipending & MIPS_INT_MASK_0) {
    298 		/* All interrupts are gated through MIPS HW interrupt 0 */
    299 		malta_cpuintrs[0].cintr_count.ev_count++;
    300 		LIST_FOREACH(ih, &malta_cpuintrs[0].cintr_list, ih_q)
    301 			(*ih->ih_func)(ih->ih_arg);
    302 		cause &= ~MIPS_INT_MASK_0;
    303 	}
    304 
    305 	/* Re-enable anything that we have processed. */
    306 	_splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK));
    307 }
    308 
    309 /*
    310  * YAMON configures pa_intrline correctly (so far), so we trust it to DTRT
    311  * in the future...
    312  */
    313 #undef YAMON_IRQ_MAP_BAD
    314 
    315 /*
    316  * PCI interrupt support
    317  */
    318 static int
    319 malta_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    320 {
    321 #ifdef YAMON_IRQ_MAP_BAD
    322 	static const int pciirqmap[12/*device*/][4/*pin*/] = {
    323 		{ -1, -1, -1, 11 },	/* 10: USB */
    324 		{ 10, -1, -1, -1 },	/* 11: Ethernet */
    325 		{ 11, -1, -1, -1 },	/* 12: Audio */
    326 		{ -1, -1, -1, -1 },	/* 13: not used */
    327 		{ -1, -1, -1, -1 },	/* 14: not used */
    328 		{ -1, -1, -1, -1 },	/* 15: not used */
    329 		{ -1, -1, -1, -1 },	/* 16: not used */
    330 		{ -1, -1, -1, -1 },	/* 17: Core card(?) */
    331 		{ 10, 10, 11, 11 },	/* 18: PCI Slot 1 */
    332 		{ 10, 11, 11, 10 },	/* 19: PCI Slot 2 */
    333 		{ 11, 11, 10, 10 },	/* 20: PCI Slot 3 */
    334 		{ 11, 10, 10, 11 },	/* 21: PCI Slot 4 */
    335 	};
    336 	int buspin, device, irq;
    337 #else	/* !YAMON_IRQ_MAP_BAD */
    338 	int buspin;
    339 #endif	/* !YAMON_IRQ_MAP_BAD */
    340 
    341 	buspin = pa->pa_intrpin;
    342 
    343 	if (buspin == 0) {
    344 		/* No IRQ used. */
    345 		return (1);
    346 	}
    347 
    348 	if (buspin > 4) {
    349 		printf("malta_pci_intr_map: bad interrupt pin %d\n", buspin);
    350 		return (1);
    351 	}
    352 
    353 #ifdef YAMON_IRQ_MAP_BAD
    354 	pci_decompose_tag(pa->pa_pc, pa->pa_intrtag, NULL, &device, NULL);
    355 
    356 	if (device < 10 || device > 21) {
    357 		printf("malta_pci_intr_map: bad device %d\n", device);
    358 		return (1);
    359 	}
    360 
    361 	irq = pciirqmap[device - 10][buspin - 1];
    362 	if (irq == -1) {
    363 		printf("malta_pci_intr_map: no mapping for device %d pin %d\n",
    364 		    device, buspin);
    365 		return (1);
    366 	}
    367 
    368 	*ihp = irq;
    369 #else	/* !YAMON_IRQ_MAP_BAD */
    370 	*ihp = pa->pa_intrline;
    371 #endif	/* !YAMON_IRQ_MAP_BAD */
    372 	return (0);
    373 }
    374 
    375 static const char *
    376 malta_pci_intr_string(void *v, pci_intr_handle_t irq)
    377 {
    378 
    379 	return (isa_intr_string(pcib_ic, irq));
    380 }
    381 
    382 static const struct evcnt *
    383 malta_pci_intr_evcnt(void *v, pci_intr_handle_t irq)
    384 {
    385 
    386 	return (isa_intr_evcnt(pcib_ic, irq));
    387 }
    388 
    389 static void *
    390 malta_pci_intr_establish(void *v, pci_intr_handle_t irq, int level,
    391     int (*func)(void *), void *arg)
    392 {
    393 
    394 	return (isa_intr_establish(pcib_ic, irq, IST_LEVEL, level, func, arg));
    395 }
    396 
    397 static void
    398 malta_pci_intr_disestablish(void *v, void *arg)
    399 {
    400 
    401 	return (isa_intr_disestablish(pcib_ic, arg));
    402 }
    403 
    404 static void
    405 malta_pci_conf_interrupt(void *v, int bus, int dev, int func, int swiz,
    406     int *iline)
    407 {
    408 
    409 	/*
    410 	 * We actually don't need to do anything; everything is handled
    411 	 * in pci_intr_map().
    412 	 */
    413 	*iline = 0;
    414 }
    415 
    416 void *
    417 malta_pciide_compat_intr_establish(void *v, struct device *dev,
    418     struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
    419 {
    420 	pci_chipset_tag_t pc = pa->pa_pc;
    421 	void *cookie;
    422 	int bus, irq;
    423 
    424 	pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
    425 
    426 	/*
    427 	 * If this isn't PCI bus #0, all bets are off.
    428 	 */
    429 	if (bus != 0)
    430 		return (NULL);
    431 
    432 	irq = PCIIDE_COMPAT_IRQ(chan);
    433 	cookie = isa_intr_establish(pcib_ic, irq, IST_EDGE, IPL_BIO, func, arg);
    434 	if (cookie == NULL)
    435 		return (NULL);
    436 	printf("%s: %s channel interrupting at %s\n", dev->dv_xname,
    437 	    PCIIDE_CHANNEL_NAME(chan), malta_pci_intr_string(v, irq));
    438 	return (cookie);
    439 }
    440