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