Home | History | Annotate | Line # | Download | only in pci
pci_6600.c revision 1.5
      1 /* $NetBSD: pci_6600.c,v 1.5 2000/06/06 00:50:15 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 by Ross Harvey.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Ross Harvey.
     17  * 4. The name of Ross Harvey may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
     21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
     24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(0, "$NetBSD: pci_6600.c,v 1.5 2000/06/06 00:50:15 thorpej Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <vm/vm.h>
     44 
     45 #include <machine/autoconf.h>
     46 #define _ALPHA_BUS_DMA_PRIVATE
     47 #include <machine/bus.h>
     48 #include <machine/rpb.h>
     49 #include <machine/alpha.h>
     50 
     51 #include <dev/pci/pcireg.h>
     52 #include <dev/pci/pcivar.h>
     53 #include <dev/pci/pciidereg.h>
     54 #include <dev/pci/pciidevar.h>
     55 
     56 #include <alpha/pci/tsreg.h>
     57 #include <alpha/pci/tsvar.h>
     58 #include <alpha/pci/pci_6600.h>
     59 
     60 #define pci_6600() { Generate ctags(1) key. }
     61 
     62 #include "sio.h"
     63 #if NSIO
     64 #include <alpha/pci/siovar.h>
     65 #endif
     66 
     67 #define	PCI_NIRQ		64
     68 #define	PCI_STRAY_MAX		5
     69 
     70 /*
     71  * Some Tsunami models have a PCI device (the USB controller) with interrupts
     72  * tied to ISA IRQ lines.  The IRQ is encoded as:
     73  *
     74  *	line = 0xe0 | isa_irq;
     75  */
     76 #define	DEC_6600_LINE_IS_ISA(line)	((line) >= 0xe0 && (line) <= 0xef)
     77 #define	DEC_6600_LINE_ISA_IRQ(line)	((line) & 0x0f)
     78 
     79 static char *irqtype = "6600 irq";
     80 static struct tsp_config *sioprimary;
     81 
     82 void dec_6600_intr_disestablish __P((void *, void *));
     83 void *dec_6600_intr_establish __P((
     84     void *, pci_intr_handle_t, int, int (*func)(void *), void *));
     85 const char *dec_6600_intr_string __P((void *, pci_intr_handle_t));
     86 const struct evcnt *dec_6600_intr_evcnt __P((void *, pci_intr_handle_t));
     87 int dec_6600_intr_map __P((void *, pcitag_t, int, int, pci_intr_handle_t *));
     88 void *dec_6600_pciide_compat_intr_establish __P((void *, struct device *,
     89     struct pci_attach_args *, int, int (*)(void *), void *));
     90 
     91 struct alpha_shared_intr *dec_6600_pci_intr;
     92 
     93 void dec_6600_iointr __P((void *framep, unsigned long vec));
     94 extern void dec_6600_intr_enable __P((int irq));
     95 extern void dec_6600_intr_disable __P((int irq));
     96 
     97 void
     98 pci_6600_pickintr(pcp)
     99 	struct tsp_config *pcp;
    100 {
    101 	bus_space_tag_t iot = &pcp->pc_iot;
    102 	pci_chipset_tag_t pc = &pcp->pc_pc;
    103 	char *cp;
    104 	int i;
    105 
    106         pc->pc_intr_v = pcp;
    107         pc->pc_intr_map = dec_6600_intr_map;
    108         pc->pc_intr_string = dec_6600_intr_string;
    109 	pc->pc_intr_evcnt = dec_6600_intr_evcnt;
    110         pc->pc_intr_establish = dec_6600_intr_establish;
    111         pc->pc_intr_disestablish = dec_6600_intr_disestablish;
    112 	pc->pc_pciide_compat_intr_establish = NULL;
    113 
    114 	/*
    115 	 * System-wide and Pchip-0-only logic...
    116 	 */
    117 	if (dec_6600_pci_intr == NULL) {
    118 		sioprimary = pcp;
    119 		pc->pc_pciide_compat_intr_establish =
    120 		    dec_6600_pciide_compat_intr_establish;
    121 		dec_6600_pci_intr = alpha_shared_intr_alloc(PCI_NIRQ, 8);
    122 		for (i = 0; i < PCI_NIRQ; i++) {
    123 			alpha_shared_intr_set_maxstrays(dec_6600_pci_intr, i,
    124 			    PCI_STRAY_MAX);
    125 			alpha_shared_intr_set_private(dec_6600_pci_intr, i,
    126 			    sioprimary);
    127 
    128 			cp = alpha_shared_intr_string(dec_6600_pci_intr, i);
    129 			sprintf(cp, "irq %d", i);
    130 			evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    131 			    dec_6600_pci_intr, 1), EVCNT_TYPE_INTR, NULL,
    132 			    "dec_6600", cp);
    133 		}
    134 #if NSIO
    135 		sio_intr_setup(pc, iot);
    136 		dec_6600_intr_enable(55);	/* irq line for sio */
    137 #endif
    138 		set_iointr(dec_6600_iointr);
    139 	}
    140 }
    141 
    142 int
    143 dec_6600_intr_map(acv, bustag, buspin, line, ihp)
    144         void *acv;
    145         pcitag_t bustag;
    146         int buspin, line;
    147         pci_intr_handle_t *ihp;
    148 {
    149 	struct tsp_config *pcp = acv;
    150 	pci_chipset_tag_t pc = &pcp->pc_pc;
    151 	int bus, device, function;
    152 
    153 	if (buspin == 0) {
    154 		/* No IRQ used. */
    155 		return 1;
    156 	}
    157 	if (buspin > 4) {
    158 		printf("intr_map: bad interrupt pin %d\n", buspin);
    159 		return 1;
    160 	}
    161 
    162 	alpha_pci_decompose_tag(pc, bustag, &bus, &device, &function);
    163 
    164 	/*
    165 	 * The console places the interrupt mapping in the "line" value.
    166 	 * A value of (char)-1 indicates there is no mapping.
    167 	 */
    168 	if (line == 0xff) {
    169 		printf("dec_6600_intr_map: no mapping for %d/%d/%d\n",
    170 		    bus, device, function);
    171 		return (1);
    172 	}
    173 
    174 #if NSIO == 0
    175 	if (DEC_6600_LINE_IS_ISA(line)) {
    176 		printf("dec_6600_intr_map: ISA IRQ %d for %d/%d/%d\n",
    177 		    DEC_6600_LINE_ISA_IRQ(line), bus, device, function);
    178 		return (1);
    179 	}
    180 #endif
    181 
    182 	if (DEC_6600_LINE_IS_ISA(line) == 0 && line >= PCI_NIRQ)
    183 		panic("dec_6600_intr_map: dec 6600 irq too large (%d)\n",
    184 		    line);
    185 
    186 	*ihp = line;
    187 	return (0);
    188 }
    189 
    190 const char *
    191 dec_6600_intr_string(acv, ih)
    192 	void *acv;
    193 	pci_intr_handle_t ih;
    194 {
    195 
    196 	static const char irqfmt[] = "dec 6600 irq %ld";
    197 	static char irqstr[sizeof irqfmt];
    198 
    199 #if NSIO
    200 	if (DEC_6600_LINE_IS_ISA(ih))
    201 		return (sio_intr_string(NULL /*XXX*/,
    202 		    DEC_6600_LINE_ISA_IRQ(ih)));
    203 #endif
    204 
    205 	snprintf(irqstr, sizeof irqstr, irqfmt, ih);
    206 	return (irqstr);
    207 }
    208 
    209 const struct evcnt *
    210 dec_6600_intr_evcnt(acv, ih)
    211 	void *acv;
    212 	pci_intr_handle_t ih;
    213 {
    214 
    215 #if NSIO
    216 	if (DEC_6600_LINE_IS_ISA(ih))
    217 		return (sio_intr_evcnt(NULL /*XXX*/,
    218 		    DEC_6600_LINE_ISA_IRQ(ih)));
    219 #endif
    220 
    221 	return (alpha_shared_intr_evcnt(dec_6600_pci_intr, ih));
    222 }
    223 
    224 void *
    225 dec_6600_intr_establish(acv, ih, level, func, arg)
    226         void *acv, *arg;
    227         pci_intr_handle_t ih;
    228         int level;
    229         int (*func) __P((void *));
    230 {
    231 	void *cookie;
    232 
    233 #if NSIO
    234 	if (DEC_6600_LINE_IS_ISA(ih))
    235 		return (sio_intr_establish(NULL /*XXX*/,
    236 		    DEC_6600_LINE_ISA_IRQ(ih), IST_LEVEL, level, func, arg));
    237 #endif
    238 
    239 	if (ih >= PCI_NIRQ)
    240 		panic("dec_6600_intr_establish: bogus dec 6600 IRQ 0x%lx\n",
    241 		    ih);
    242 
    243 	cookie = alpha_shared_intr_establish(dec_6600_pci_intr, ih, IST_LEVEL,
    244 	    level, func, arg, irqtype);
    245 
    246 	if (cookie != NULL && alpha_shared_intr_isactive(dec_6600_pci_intr, ih))
    247 		dec_6600_intr_enable(ih);
    248 	return (cookie);
    249 }
    250 
    251 void
    252 dec_6600_intr_disestablish(acv, cookie)
    253         void *acv, *cookie;
    254 {
    255 	struct alpha_shared_intrhand *ih = cookie;
    256 	unsigned int irq = ih->ih_num;
    257 	int s;
    258 
    259 #if NSIO
    260 	/*
    261 	 * We have to determine if this is an ISA IRQ or not!  We do this
    262 	 * by checking to see if the intrhand points back to an intrhead
    263 	 * that points to the sioprimary TSP.  If not, it's an ISA IRQ.
    264 	 * Pretty disgusting, eh?
    265 	 */
    266 	if (ih->ih_intrhead->intr_private != sioprimary) {
    267 		sio_intr_disestablish(NULL /*XXX*/, cookie);
    268 		return;
    269 	}
    270 #endif
    271 
    272 	s = splhigh();
    273 
    274 	alpha_shared_intr_disestablish(dec_6600_pci_intr, cookie, irqtype);
    275 	if (alpha_shared_intr_isactive(dec_6600_pci_intr, irq) == 0) {
    276 		dec_6600_intr_disable(irq);
    277 		alpha_shared_intr_set_dfltsharetype(dec_6600_pci_intr, irq,
    278 		    IST_NONE);
    279 	}
    280 
    281 	splx(s);
    282 }
    283 
    284 void
    285 dec_6600_iointr(framep, vec)
    286 	void *framep;
    287 	unsigned long vec;
    288 {
    289 	int irq;
    290 
    291 	if (vec >= 0x900) {
    292 		irq = (vec - 0x900) >> 4;
    293 
    294 		if (irq >= PCI_NIRQ)
    295 			panic("iointr: irq %d is too high", irq);
    296 
    297 		if (!alpha_shared_intr_dispatch(dec_6600_pci_intr, irq)) {
    298 			alpha_shared_intr_stray(dec_6600_pci_intr, irq,
    299 			    irqtype);
    300 			if (ALPHA_SHARED_INTR_DISABLE(dec_6600_pci_intr, irq))
    301 				dec_6600_intr_disable(irq);
    302 		}
    303 		return;
    304 	}
    305 #if NSIO
    306 	if (vec >= 0x800) {
    307 		sio_iointr(framep, vec);
    308 		return;
    309 	}
    310 #endif
    311 	panic("iointr: weird vec 0x%lx\n", vec);
    312 }
    313 
    314 void
    315 dec_6600_intr_enable(irq)
    316 	int irq;
    317 {
    318 	alpha_mb();
    319 	STQP(TS_C_DIM0) |= 1UL << irq;
    320 	alpha_mb();
    321 }
    322 
    323 void
    324 dec_6600_intr_disable(irq)
    325 	int irq;
    326 {
    327 	alpha_mb();
    328 	STQP(TS_C_DIM0) &= ~(1UL << irq);
    329 	alpha_mb();
    330 }
    331 
    332 void *
    333 dec_6600_pciide_compat_intr_establish(v, dev, pa, chan, func, arg)
    334 	void *v;
    335 	struct device *dev;
    336 	struct pci_attach_args *pa;
    337 	int chan;
    338 	int (*func) __P((void *));
    339 	void *arg;
    340 {
    341 	pci_chipset_tag_t pc = pa->pa_pc;
    342 	void *cookie = NULL;
    343 	int bus, irq;
    344 
    345 	alpha_pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
    346 
    347 	/*
    348 	 * If this isn't PCI bus #0 on the TSP that holds the PCI-ISA
    349 	 * bridge, all bets are off.
    350 	 */
    351 	if (bus != 0 || pc->pc_intr_v != sioprimary)
    352 		return (NULL);
    353 
    354 	irq = PCIIDE_COMPAT_IRQ(chan);
    355 #if NSIO
    356 	cookie = sio_intr_establish(NULL /*XXX*/, irq, IST_EDGE, IPL_BIO,
    357 	    func, arg);
    358 	if (cookie == NULL)
    359 		return (NULL);
    360 	printf("%s: %s channel interrupting at %s\n", dev->dv_xname,
    361 	    PCIIDE_CHANNEL_NAME(chan), sio_intr_string(NULL /*XXX*/, irq));
    362 #endif
    363 	return (cookie);
    364 }
    365