Home | History | Annotate | Line # | Download | only in pci
pci_kn300.c revision 1.17
      1  1.17   mjacob /* $NetBSD: pci_kn300.c,v 1.17 2000/02/10 04:31:36 mjacob Exp $ */
      2   1.1   mjacob 
      3   1.1   mjacob /*
      4   1.1   mjacob  * Copyright (c) 1998 by Matthew Jacob
      5   1.1   mjacob  * NASA AMES Research Center.
      6   1.1   mjacob  * All rights reserved.
      7   1.1   mjacob  *
      8   1.1   mjacob  * Redistribution and use in source and binary forms, with or without
      9   1.1   mjacob  * modification, are permitted provided that the following conditions
     10   1.1   mjacob  * are met:
     11   1.1   mjacob  * 1. Redistributions of source code must retain the above copyright
     12   1.1   mjacob  *    notice immediately at the beginning of the file, without modification,
     13   1.1   mjacob  *    this list of conditions, and the following disclaimer.
     14   1.1   mjacob  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   mjacob  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   mjacob  *    documentation and/or other materials provided with the distribution.
     17   1.1   mjacob  * 3. The name of the author may not be used to endorse or promote products
     18   1.1   mjacob  *    derived from this software without specific prior written permission.
     19   1.1   mjacob  *
     20   1.1   mjacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21   1.1   mjacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1   mjacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1   mjacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     24   1.1   mjacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1   mjacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1   mjacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1   mjacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1   mjacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1   mjacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1   mjacob  * SUCH DAMAGE.
     31   1.1   mjacob  */
     32   1.1   mjacob 
     33   1.1   mjacob #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34   1.1   mjacob 
     35  1.17   mjacob __KERNEL_RCSID(0, "$NetBSD: pci_kn300.c,v 1.17 2000/02/10 04:31:36 mjacob Exp $");
     36   1.1   mjacob 
     37   1.1   mjacob #include <sys/types.h>
     38   1.1   mjacob #include <sys/param.h>
     39   1.1   mjacob #include <sys/time.h>
     40   1.1   mjacob #include <sys/systm.h>
     41   1.1   mjacob #include <sys/errno.h>
     42   1.1   mjacob #include <sys/malloc.h>
     43   1.1   mjacob #include <sys/device.h>
     44   1.1   mjacob #include <sys/syslog.h>
     45   1.1   mjacob 
     46   1.1   mjacob #include <vm/vm.h>
     47   1.1   mjacob 
     48   1.1   mjacob #include <machine/autoconf.h>
     49   1.1   mjacob 
     50   1.1   mjacob #include <dev/pci/pcireg.h>
     51   1.1   mjacob #include <dev/pci/pcivar.h>
     52   1.1   mjacob 
     53   1.1   mjacob #include <alpha/mcbus/mcbusvar.h>
     54   1.1   mjacob #include <alpha/mcbus/mcbusreg.h>
     55   1.1   mjacob #include <alpha/pci/mcpciareg.h>
     56   1.1   mjacob #include <alpha/pci/mcpciavar.h>
     57   1.1   mjacob #include <alpha/pci/pci_kn300.h>
     58   1.1   mjacob 
     59   1.1   mjacob #ifndef EVCNT_COUNTERS
     60   1.1   mjacob #include <machine/intrcnt.h>
     61   1.1   mjacob #endif
     62   1.1   mjacob 
     63   1.1   mjacob #include "sio.h"
     64  1.16  thorpej #if NSIO > 0 || NPCEB > 0
     65   1.1   mjacob #include <alpha/pci/siovar.h>
     66   1.1   mjacob #endif
     67   1.1   mjacob 
     68  1.12  thorpej int	dec_kn300_intr_map __P((void *, pcitag_t, int, int,
     69  1.12  thorpej 	    pci_intr_handle_t *));
     70   1.1   mjacob const char *dec_kn300_intr_string __P((void *, pci_intr_handle_t));
     71  1.12  thorpej void	*dec_kn300_intr_establish __P((void *, pci_intr_handle_t,
     72   1.1   mjacob 	    int, int (*func)(void *), void *));
     73   1.1   mjacob void	dec_kn300_intr_disestablish __P((void *, void *));
     74   1.1   mjacob 
     75   1.1   mjacob #define	KN300_PCEB_IRQ	16
     76   1.6   mjacob #define	NPIN		4
     77   1.1   mjacob 
     78  1.12  thorpej #define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
     79  1.12  thorpej static int savirqs[NIRQ];
     80   1.1   mjacob 
     81   1.6   mjacob static struct alpha_shared_intr *kn300_pci_intr;
     82   1.6   mjacob 
     83  1.13   mjacob static struct mcpcia_config *mcpcia_eisaccp = NULL;
     84  1.13   mjacob 
     85   1.1   mjacob #ifdef EVCNT_COUNTERS
     86   1.1   mjacob struct evcnt kn300_intr_evcnt;
     87   1.1   mjacob #endif
     88   1.1   mjacob 
     89   1.1   mjacob void	kn300_iointr __P((void *, unsigned long));
     90   1.1   mjacob void	kn300_enable_intr __P((struct mcpcia_config *, int));
     91   1.1   mjacob void	kn300_disable_intr __P((struct mcpcia_config *, int));
     92   1.1   mjacob 
     93   1.1   mjacob void
     94   1.1   mjacob pci_kn300_pickintr(ccp, first)
     95   1.1   mjacob 	struct mcpcia_config *ccp;
     96   1.1   mjacob 	int first;
     97   1.1   mjacob {
     98   1.1   mjacob 	pci_chipset_tag_t pc = &ccp->cc_pc;
     99   1.1   mjacob 
    100   1.1   mjacob 	if (first) {
    101   1.1   mjacob 		int g;
    102   1.6   mjacob 
    103  1.12  thorpej 		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
    104  1.12  thorpej 		for (g = 0; g < NIRQ; g++) {
    105  1.17   mjacob 			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 100);
    106   1.6   mjacob 			savirqs[g] = (char) -1;
    107   1.1   mjacob 		}
    108   1.1   mjacob 		set_iointr(kn300_iointr);
    109   1.1   mjacob 	}
    110   1.1   mjacob 
    111   1.1   mjacob 	pc->pc_intr_v = ccp;
    112   1.1   mjacob 	pc->pc_intr_map = dec_kn300_intr_map;
    113   1.1   mjacob 	pc->pc_intr_string = dec_kn300_intr_string;
    114   1.1   mjacob 	pc->pc_intr_establish = dec_kn300_intr_establish;
    115   1.1   mjacob 	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
    116   1.3  thorpej 
    117   1.4   mjacob 	/* Not supported on KN300. */
    118   1.3  thorpej 	pc->pc_pciide_compat_intr_establish = NULL;
    119   1.1   mjacob 
    120  1.13   mjacob 	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
    121  1.13   mjacob 		mcpcia_eisaccp = ccp;
    122  1.16  thorpej #if NSIO > 0 || NPCEB > 0
    123  1.11  thorpej 		sio_intr_setup(pc, &ccp->cc_iot);
    124   1.1   mjacob 		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
    125  1.13   mjacob #endif
    126   1.1   mjacob 	}
    127   1.1   mjacob }
    128   1.1   mjacob 
    129   1.1   mjacob int
    130   1.1   mjacob dec_kn300_intr_map(ccv, bustag, buspin, line, ihp)
    131  1.15  thorpej 	void *ccv;
    132  1.15  thorpej 	pcitag_t bustag;
    133  1.15  thorpej 	int buspin, line;
    134  1.15  thorpej 	pci_intr_handle_t *ihp;
    135   1.1   mjacob {
    136   1.1   mjacob 	struct mcpcia_config *ccp = ccv;
    137   1.1   mjacob 	pci_chipset_tag_t pc = &ccp->cc_pc;
    138   1.1   mjacob 	int device;
    139  1.12  thorpej 	int mcpcia_irq;
    140   1.1   mjacob 
    141   1.5  thorpej 	if (buspin == 0) {
    142   1.5  thorpej 		/* No IRQ used. */
    143   1.5  thorpej 		return 1;
    144   1.5  thorpej 	}
    145   1.5  thorpej 	if (buspin > 4 || buspin < 0) {
    146   1.5  thorpej 		printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
    147   1.5  thorpej 		return 1;
    148   1.5  thorpej 	}
    149   1.1   mjacob 
    150   1.1   mjacob 	alpha_pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    151   1.1   mjacob 
    152  1.11  thorpej 	/*
    153  1.11  thorpej 	 * On MID 5 device 1 is the internal NCR 53c810.
    154  1.11  thorpej 	 */
    155  1.11  thorpej 	if (ccp->cc_mid == 5 && device == 1) {
    156  1.12  thorpej 		mcpcia_irq = 16;
    157   1.1   mjacob 	} else if (device >= 2 && device <= 5) {
    158  1.12  thorpej 		mcpcia_irq = (device - 2) * 4;
    159   1.1   mjacob 	} else {
    160  1.15  thorpej 		printf("dec_kn300_intr_map: weird device number %d\n", device);
    161  1.15  thorpej 		return(1);
    162   1.1   mjacob 	}
    163  1.11  thorpej 
    164   1.1   mjacob 	/*
    165   1.1   mjacob 	 * handle layout:
    166   1.1   mjacob 	 *
    167  1.15  thorpej 	 *	Determine kn300 IRQ (encoded in SCB vector):
    168   1.6   mjacob 	 *	bits 0..1	buspin-1
    169   1.6   mjacob 	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
    170   1.6   mjacob 	 *	bits 5..7	MID-4
    171   1.6   mjacob 	 *	bits 8..10	7-GID
    172  1.12  thorpej 	 *
    173  1.12  thorpej 	 *	Software only:
    174  1.12  thorpej 	 *	bits 11-15	MCPCIA IRQ
    175   1.1   mjacob 	 */
    176   1.1   mjacob 	*ihp = (pci_intr_handle_t)
    177   1.6   mjacob 		(buspin - 1			    )	|
    178   1.6   mjacob 		((device & 0x7)			<< 2)	|
    179  1.11  thorpej 		((ccp->cc_mid - 4)		<< 5)	|
    180  1.11  thorpej 		((7 - ccp->cc_gid)		<< 8)	|
    181  1.15  thorpej 		(mcpcia_irq			<< 11);
    182   1.1   mjacob 	return (0);
    183   1.1   mjacob }
    184   1.1   mjacob 
    185   1.1   mjacob const char *
    186   1.1   mjacob dec_kn300_intr_string(ccv, ih)
    187   1.1   mjacob 	void *ccv;
    188   1.1   mjacob 	pci_intr_handle_t ih;
    189   1.1   mjacob {
    190  1.15  thorpej 	static char irqstr[64];
    191  1.12  thorpej 
    192  1.12  thorpej 	sprintf(irqstr, "kn300 irq %ld", ih & 0x3ff);
    193   1.1   mjacob 	return (irqstr);
    194   1.1   mjacob }
    195   1.1   mjacob 
    196   1.1   mjacob void *
    197   1.1   mjacob dec_kn300_intr_establish(ccv, ih, level, func, arg)
    198   1.1   mjacob         void *ccv;
    199   1.1   mjacob         pci_intr_handle_t ih;
    200   1.1   mjacob         int level;
    201   1.1   mjacob         int (*func) __P((void *));
    202   1.1   mjacob         void *arg;
    203   1.1   mjacob {
    204  1.15  thorpej 	struct mcpcia_config *ccp = ccv;
    205   1.6   mjacob 	void *cookie;
    206  1.12  thorpej 	int irq;
    207   1.1   mjacob 
    208  1.12  thorpej 	irq = ih & 0x3ff;
    209  1.12  thorpej 	cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
    210  1.12  thorpej 	    level, func, arg, "kn300 irq");
    211   1.6   mjacob 
    212  1.12  thorpej 	if (cookie != NULL && alpha_shared_intr_isactive(kn300_pci_intr, irq)) {
    213  1.15  thorpej 		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
    214  1.12  thorpej 		savirqs[irq] = (ih >> 11) & 0x1f;
    215  1.15  thorpej 		kn300_enable_intr(ccp, savirqs[irq]);
    216   1.6   mjacob 		alpha_mb();
    217   1.1   mjacob 	}
    218   1.1   mjacob 	return (cookie);
    219   1.1   mjacob }
    220   1.1   mjacob 
    221   1.1   mjacob void
    222   1.1   mjacob dec_kn300_intr_disestablish(ccv, cookie)
    223  1.15  thorpej 	void *ccv, *cookie;
    224   1.1   mjacob {
    225   1.6   mjacob 	panic("dec_kn300_intr_disestablish not implemented");
    226   1.1   mjacob }
    227   1.1   mjacob 
    228   1.1   mjacob void
    229   1.1   mjacob kn300_iointr(framep, vec)
    230   1.1   mjacob 	void *framep;
    231   1.1   mjacob 	unsigned long vec;
    232   1.1   mjacob {
    233   1.6   mjacob 	struct mcpcia_softc *mcp;
    234  1.12  thorpej 	u_long irq;
    235   1.1   mjacob 
    236   1.1   mjacob 	if (vec >= MCPCIA_VEC_EISA && vec < MCPCIA_VEC_PCI) {
    237  1.16  thorpej #if NSIO > 0 || NPCEB > 0
    238   1.1   mjacob 		sio_iointr(framep, vec);
    239   1.1   mjacob 		return;
    240   1.1   mjacob #else
    241  1.15  thorpej 		static const char *plaint = "kn300_iointr: (E)ISA interrupt "
    242  1.15  thorpej 		    "support not configured for vector 0x%x";
    243  1.13   mjacob 		if (mcpcia_eisaccp) {
    244  1.13   mjacob 			kn300_disable_intr(mcpcia_eisaccp, KN300_PCEB_IRQ);
    245  1.13   mjacob 			printf(plaint, vec);
    246  1.13   mjacob 		} else {
    247  1.13   mjacob 			panic(plaint, vec);
    248  1.13   mjacob 		}
    249   1.1   mjacob #endif
    250   1.1   mjacob 	}
    251   1.1   mjacob 
    252   1.1   mjacob #ifdef	EVCNT_COUNTERS
    253   1.1   mjacob 	kn300_intr_evcnt.ev_count++;
    254   1.1   mjacob #endif
    255  1.12  thorpej 
    256  1.12  thorpej 	irq = (vec - MCPCIA_VEC_PCI) >> 4;
    257  1.12  thorpej 
    258   1.1   mjacob 	/*
    259  1.12  thorpej 	 * Check for I2C interrupts.  These are technically within
    260  1.12  thorpej 	 * the PCI vector range, but no PCI device should ever map
    261  1.12  thorpej 	 * to them.
    262   1.1   mjacob 	 */
    263  1.12  thorpej 	if (vec == MCPCIA_I2C_CVEC) {
    264   1.1   mjacob #ifndef	EVCNT_COUNTERS
    265   1.1   mjacob 		intrcnt[INTRCNT_KN300_I2C_CTRL]++;
    266   1.1   mjacob #endif
    267   1.1   mjacob 		printf("i2c: controller interrupt\n");
    268   1.1   mjacob 		return;
    269   1.1   mjacob 	}
    270  1.12  thorpej 	if (vec == MCPCIA_I2C_BVEC) {
    271   1.1   mjacob #ifndef	EVCNT_COUNTERS
    272   1.1   mjacob 		intrcnt[INTRCNT_KN300_I2C_BUS]++;
    273   1.1   mjacob #endif
    274   1.1   mjacob 		printf("i2c: bus interrupt\n");
    275   1.1   mjacob 		return;
    276   1.1   mjacob 	}
    277   1.1   mjacob 
    278   1.1   mjacob #ifndef	EVCNT_COUNTERS
    279  1.12  thorpej 	if (savirqs[irq] >= 0 && savirqs[irq] <= INTRCNT_KN300_NCR810) {
    280  1.12  thorpej 		intrcnt[INTRCNT_KN300_IRQ + savirqs[irq]]++;
    281   1.1   mjacob 	}
    282   1.1   mjacob #endif
    283   1.6   mjacob 
    284  1.17   mjacob 	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
    285  1.17   mjacob 		/*
    286  1.17   mjacob 		 * Any claim of an interrupt at this level is a hint to
    287  1.17   mjacob 		 * reset the stray interrupt count- elsewise a slow leak
    288  1.17   mjacob 		 * over time will cause this level to be shutdown.
    289  1.17   mjacob 		 *
    290  1.17   mjacob 		 * Unfortunately, there's no clean mechanism to do this yet.
    291  1.17   mjacob 		 */
    292  1.17   mjacob 		return;
    293  1.17   mjacob 	}
    294  1.17   mjacob 
    295  1.17   mjacob 	/*
    296  1.17   mjacob 	 * If we haven't finished configuring yet, or there is no mcp
    297  1.17   mjacob 	 * registered for this level yet, just return.
    298  1.17   mjacob 	 */
    299  1.17   mjacob 	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
    300  1.17   mjacob 	if (mcp == NULL || mcp->mcpcia_cc == NULL)
    301  1.17   mjacob 		return;
    302  1.17   mjacob 
    303  1.17   mjacob 	/*
    304  1.17   mjacob 	 * We're getting an interrupt for a device we haven't enabled.
    305  1.17   mjacob 	 * We had better not try and use -1 to find the right bit to disable.
    306  1.17   mjacob 	 */
    307  1.17   mjacob 	if (savirqs[irq] == -1) {
    308  1.17   mjacob 		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
    309   1.6   mjacob 		return;
    310  1.17   mjacob 	}
    311  1.11  thorpej 
    312   1.6   mjacob 	/*
    313  1.11  thorpej 	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
    314  1.11  thorpej 	 * if we've reached the limit.
    315   1.6   mjacob 	 */
    316  1.14  thorpej 	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
    317  1.12  thorpej 	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
    318   1.6   mjacob 		return;
    319  1.12  thorpej 	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
    320   1.1   mjacob }
    321   1.1   mjacob 
    322   1.1   mjacob void
    323   1.1   mjacob kn300_enable_intr(ccp, irq)
    324   1.1   mjacob 	struct mcpcia_config *ccp;
    325   1.1   mjacob 	int irq;
    326   1.1   mjacob {
    327   1.1   mjacob 	alpha_mb();
    328  1.11  thorpej 	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
    329   1.1   mjacob 	alpha_mb();
    330   1.1   mjacob }
    331   1.1   mjacob 
    332   1.1   mjacob void
    333   1.1   mjacob kn300_disable_intr(ccp, irq)
    334   1.1   mjacob 	struct mcpcia_config *ccp;
    335   1.1   mjacob 	int irq;
    336   1.1   mjacob {
    337   1.1   mjacob 	alpha_mb();
    338  1.11  thorpej 	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
    339   1.1   mjacob 	alpha_mb();
    340   1.1   mjacob }
    341