Home | History | Annotate | Line # | Download | only in pci
pci_kn20aa.c revision 1.49
      1 /* $NetBSD: pci_kn20aa.c,v 1.49 2009/03/14 15:35:59 dsl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     31 
     32 __KERNEL_RCSID(0, "$NetBSD: pci_kn20aa.c,v 1.49 2009/03/14 15:35:59 dsl Exp $");
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/time.h>
     37 #include <sys/systm.h>
     38 #include <sys/errno.h>
     39 #include <sys/malloc.h>
     40 #include <sys/device.h>
     41 #include <sys/syslog.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <machine/autoconf.h>
     46 
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcivar.h>
     49 
     50 #include <alpha/pci/ciareg.h>
     51 #include <alpha/pci/ciavar.h>
     52 
     53 #include <alpha/pci/pci_kn20aa.h>
     54 
     55 #include "sio.h"
     56 #if NSIO > 0 || NPCEB > 0
     57 #include <alpha/pci/siovar.h>
     58 #endif
     59 
     60 int	dec_kn20aa_intr_map(struct pci_attach_args *,
     61 	    pci_intr_handle_t *);
     62 const char *dec_kn20aa_intr_string(void *, pci_intr_handle_t);
     63 const struct evcnt *dec_kn20aa_intr_evcnt(void *, pci_intr_handle_t);
     64 void	*dec_kn20aa_intr_establish(void *, pci_intr_handle_t,
     65 	    int, int (*func)(void *), void *);
     66 void	dec_kn20aa_intr_disestablish(void *, void *);
     67 
     68 #define	KN20AA_PCEB_IRQ	31
     69 #define	KN20AA_MAX_IRQ	32
     70 #define	PCI_STRAY_MAX	5
     71 
     72 struct alpha_shared_intr *kn20aa_pci_intr;
     73 
     74 void	kn20aa_iointr(void *arg, unsigned long vec);
     75 void	kn20aa_enable_intr(int irq);
     76 void	kn20aa_disable_intr(int irq);
     77 
     78 void
     79 pci_kn20aa_pickintr(struct cia_config *ccp)
     80 {
     81 	int i;
     82 #if NSIO > 0 || NPCEB > 0
     83 	bus_space_tag_t iot = &ccp->cc_iot;
     84 #endif
     85 	pci_chipset_tag_t pc = &ccp->cc_pc;
     86 	char *cp;
     87 
     88         pc->pc_intr_v = ccp;
     89         pc->pc_intr_map = dec_kn20aa_intr_map;
     90         pc->pc_intr_string = dec_kn20aa_intr_string;
     91 	pc->pc_intr_evcnt = dec_kn20aa_intr_evcnt;
     92         pc->pc_intr_establish = dec_kn20aa_intr_establish;
     93         pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
     94 
     95 	/* Not supported on KN20AA. */
     96 	pc->pc_pciide_compat_intr_establish = NULL;
     97 
     98 	kn20aa_pci_intr = alpha_shared_intr_alloc(KN20AA_MAX_IRQ, 8);
     99 	for (i = 0; i < KN20AA_MAX_IRQ; i++) {
    100 		alpha_shared_intr_set_maxstrays(kn20aa_pci_intr, i,
    101 		    PCI_STRAY_MAX);
    102 
    103 		cp = alpha_shared_intr_string(kn20aa_pci_intr, i);
    104 		sprintf(cp, "irq %d", i);
    105 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    106 		    kn20aa_pci_intr, i), EVCNT_TYPE_INTR, NULL,
    107 		    "kn20aa", cp);
    108 	}
    109 
    110 #if NSIO > 0 || NPCEB > 0
    111 	sio_intr_setup(pc, iot);
    112 	kn20aa_enable_intr(KN20AA_PCEB_IRQ);
    113 #endif
    114 }
    115 
    116 int
    117 dec_kn20aa_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    118 {
    119 	pcitag_t bustag = pa->pa_intrtag;
    120 	int buspin = pa->pa_intrpin;
    121 	pci_chipset_tag_t pc = pa->pa_pc;
    122 	int device;
    123 	int kn20aa_irq;
    124 
    125 	if (buspin == 0) {
    126 		/* No IRQ used. */
    127 		return 1;
    128 	}
    129 	if (buspin > 4) {
    130 		printf("dec_kn20aa_intr_map: bad interrupt pin %d\n", buspin);
    131 		return 1;
    132 	}
    133 
    134 	/*
    135 	 * Slot->interrupt translation.  Appears to work, though it
    136 	 * may not hold up forever.
    137 	 *
    138 	 * The DEC engineers who did this hardware obviously engaged
    139 	 * in random drug testing.
    140 	 */
    141 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    142 	switch (device) {
    143 	case 11:
    144 	case 12:
    145 		kn20aa_irq = ((device - 11) + 0) * 4;
    146 		break;
    147 
    148 	case 7:
    149 		kn20aa_irq = 8;
    150 		break;
    151 
    152 	case 9:
    153 		kn20aa_irq = 12;
    154 		break;
    155 
    156 	case 6:					/* 21040 on AlphaStation 500 */
    157 		kn20aa_irq = 13;
    158 		break;
    159 
    160 	case 8:
    161 		kn20aa_irq = 16;
    162 		break;
    163 
    164 	default:
    165                 printf("dec_kn20aa_intr_map: weird device number %d\n",
    166 		    device);
    167                 return 1;
    168 	}
    169 
    170 	kn20aa_irq += buspin - 1;
    171 	if (kn20aa_irq > KN20AA_MAX_IRQ)
    172 		panic("dec_kn20aa_intr_map: kn20aa_irq too large (%d)",
    173 		    kn20aa_irq);
    174 
    175 	*ihp = kn20aa_irq;
    176 	return (0);
    177 }
    178 
    179 const char *
    180 dec_kn20aa_intr_string(void *ccv, pci_intr_handle_t ih)
    181 {
    182 #if 0
    183 	struct cia_config *ccp = ccv;
    184 #endif
    185         static char irqstr[15];          /* 11 + 2 + NULL + sanity */
    186 
    187         if (ih > KN20AA_MAX_IRQ)
    188                 panic("dec_kn20aa_intr_string: bogus kn20aa IRQ 0x%lx",
    189 		    ih);
    190 
    191         sprintf(irqstr, "kn20aa irq %ld", ih);
    192         return (irqstr);
    193 }
    194 
    195 const struct evcnt *
    196 dec_kn20aa_intr_evcnt(void *ccv, pci_intr_handle_t ih)
    197 {
    198 #if 0
    199 	struct cia_config *ccp = ccv;
    200 #endif
    201 
    202 	if (ih > KN20AA_MAX_IRQ)
    203 		panic("dec_kn20aa_intr_string: bogus kn20aa IRQ 0x%lx", ih);
    204 	return (alpha_shared_intr_evcnt(kn20aa_pci_intr, ih));
    205 }
    206 
    207 void *
    208 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
    209         void *ccv, *arg;
    210         pci_intr_handle_t ih;
    211         int level;
    212         int (*func)(void *);
    213 {
    214 #if 0
    215         struct cia_config *ccp = ccv;
    216 #endif
    217 	void *cookie;
    218 
    219         if (ih > KN20AA_MAX_IRQ)
    220                 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%lx",
    221 		    ih);
    222 
    223 	cookie = alpha_shared_intr_establish(kn20aa_pci_intr, ih, IST_LEVEL,
    224 	    level, func, arg, "kn20aa irq");
    225 
    226 	if (cookie != NULL &&
    227 	    alpha_shared_intr_firstactive(kn20aa_pci_intr, ih)) {
    228 		scb_set(0x900 + SCB_IDXTOVEC(ih), kn20aa_iointr, NULL,
    229 		    level);
    230 		kn20aa_enable_intr(ih);
    231 	}
    232 	return (cookie);
    233 }
    234 
    235 void
    236 dec_kn20aa_intr_disestablish(ccv, cookie)
    237         void *ccv, *cookie;
    238 {
    239 #if 0
    240 	struct cia_config *ccp = ccv;
    241 #endif
    242 	struct alpha_shared_intrhand *ih = cookie;
    243 	unsigned int irq = ih->ih_num;
    244 	int s;
    245 
    246 	s = splhigh();
    247 
    248 	alpha_shared_intr_disestablish(kn20aa_pci_intr, cookie,
    249 	    "kn20aa irq");
    250 	if (alpha_shared_intr_isactive(kn20aa_pci_intr, irq) == 0) {
    251 		kn20aa_disable_intr(irq);
    252 		alpha_shared_intr_set_dfltsharetype(kn20aa_pci_intr, irq,
    253 		    IST_NONE);
    254 		scb_free(0x900 + SCB_IDXTOVEC(irq));
    255 	}
    256 
    257 	splx(s);
    258 }
    259 
    260 void
    261 kn20aa_iointr(void *arg, unsigned long vec)
    262 {
    263 	int irq;
    264 
    265 	irq = SCB_VECTOIDX(vec - 0x900);
    266 
    267 	if (!alpha_shared_intr_dispatch(kn20aa_pci_intr, irq)) {
    268 		alpha_shared_intr_stray(kn20aa_pci_intr, irq,
    269 		    "kn20aa irq");
    270 		if (ALPHA_SHARED_INTR_DISABLE(kn20aa_pci_intr, irq))
    271 			kn20aa_disable_intr(irq);
    272 	} else
    273 		alpha_shared_intr_reset_strays(kn20aa_pci_intr, irq);
    274 }
    275 
    276 void
    277 kn20aa_enable_intr(int irq)
    278 {
    279 
    280 	/*
    281 	 * From disassembling small bits of the OSF/1 kernel:
    282 	 * the following appears to enable a given interrupt request.
    283 	 * "blech."  I'd give valuable body parts for better docs or
    284 	 * for a good decompiler.
    285 	 */
    286 	alpha_mb();
    287 	REGVAL(0x8780000000L + 0x40L) |= (1 << irq);	/* XXX */
    288 	alpha_mb();
    289 }
    290 
    291 void
    292 kn20aa_disable_intr(int irq)
    293 {
    294 
    295 	alpha_mb();
    296 	REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq);	/* XXX */
    297 	alpha_mb();
    298 }
    299