Home | History | Annotate | Line # | Download | only in pci
pci_kn300.c revision 1.34.4.2
      1 /* $NetBSD: pci_kn300.c,v 1.34.4.2 2014/05/22 11:39:27 yamt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 by Matthew Jacob
      5  * NASA AMES Research Center.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice immediately at the beginning of the file, without modification,
     13  *    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. The name of the author 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 THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     24  * ANY 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 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 
     35 __KERNEL_RCSID(0, "$NetBSD: pci_kn300.c,v 1.34.4.2 2014/05/22 11:39:27 yamt Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/time.h>
     40 #include <sys/systm.h>
     41 #include <sys/errno.h>
     42 #include <sys/malloc.h>
     43 #include <sys/device.h>
     44 #include <sys/syslog.h>
     45 
     46 #include <machine/autoconf.h>
     47 
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcivar.h>
     50 
     51 #include <alpha/mcbus/mcbusvar.h>
     52 #include <alpha/mcbus/mcbusreg.h>
     53 #include <alpha/pci/mcpciareg.h>
     54 #include <alpha/pci/mcpciavar.h>
     55 #include <alpha/pci/pci_kn300.h>
     56 
     57 #include "sio.h"
     58 #if NSIO > 0 || NPCEB > 0
     59 #include <alpha/pci/siovar.h>
     60 #endif
     61 
     62 int	dec_kn300_intr_map(const struct pci_attach_args *,
     63 	    pci_intr_handle_t *);
     64 const char *dec_kn300_intr_string(void *, pci_intr_handle_t, char *, size_t);
     65 const struct evcnt *dec_kn300_intr_evcnt(void *, pci_intr_handle_t);
     66 void	*dec_kn300_intr_establish(void *, pci_intr_handle_t,
     67 	    int, int (*func)(void *), void *);
     68 void	dec_kn300_intr_disestablish(void *, void *);
     69 
     70 #define	KN300_PCEB_IRQ	16
     71 #define	NPIN		4
     72 
     73 #define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
     74 static int savirqs[NIRQ];
     75 
     76 static struct alpha_shared_intr *kn300_pci_intr;
     77 
     78 static struct mcpcia_config *mcpcia_eisaccp = NULL;
     79 
     80 void	kn300_iointr(void *, unsigned long);
     81 void	kn300_enable_intr(struct mcpcia_config *, int);
     82 void	kn300_disable_intr(struct mcpcia_config *, int);
     83 
     84 void
     85 pci_kn300_pickintr(struct mcpcia_config *ccp, int first)
     86 {
     87 	char *cp;
     88 	pci_chipset_tag_t pc = &ccp->cc_pc;
     89 
     90 	if (first) {
     91 		int g;
     92 
     93 #define PCI_KN300_IRQ_STR	16
     94 		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ,
     95 		    PCI_KN300_IRQ_STR);
     96 		for (g = 0; g < NIRQ; g++) {
     97 			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
     98 			cp = alpha_shared_intr_string(kn300_pci_intr, g);
     99 			snprintf(cp, PCI_KN300_IRQ_STR, "irq %d", g);
    100 			evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    101 			    kn300_pci_intr, g), EVCNT_TYPE_INTR, NULL,
    102 			    "kn300", cp);
    103 			savirqs[g] = (char) -1;
    104 		}
    105 	}
    106 
    107 	pc->pc_intr_v = ccp;
    108 	pc->pc_intr_map = dec_kn300_intr_map;
    109 	pc->pc_intr_string = dec_kn300_intr_string;
    110 	pc->pc_intr_evcnt = dec_kn300_intr_evcnt;
    111 	pc->pc_intr_establish = dec_kn300_intr_establish;
    112 	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
    113 
    114 	/* Not supported on KN300. */
    115 	pc->pc_pciide_compat_intr_establish = NULL;
    116 
    117 	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
    118 		mcpcia_eisaccp = ccp;
    119 #if NSIO > 0 || NPCEB > 0
    120 		sio_intr_setup(pc, &ccp->cc_iot);
    121 		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
    122 #endif
    123 	}
    124 }
    125 
    126 int
    127 dec_kn300_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    128 {
    129 	pcitag_t bustag = pa->pa_intrtag;
    130 	int buspin = pa->pa_intrpin;
    131 	pci_chipset_tag_t pc = pa->pa_pc;
    132 	struct mcpcia_config *ccp = (struct mcpcia_config *)pc->pc_intr_v;
    133 	int device;
    134 	int mcpcia_irq;
    135 
    136 	if (buspin == 0) {
    137 		/* No IRQ used. */
    138 		return 1;
    139 	}
    140 	if (buspin > 4 || buspin < 0) {
    141 		printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
    142 		return 1;
    143 	}
    144 
    145 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    146 
    147 	/*
    148 	 * On MID 5 device 1 is the internal NCR 53c810.
    149 	 */
    150 	if (ccp->cc_mid == 5 && device == 1) {
    151 		mcpcia_irq = 16;
    152 	} else if (device >= 2 && device <= 5) {
    153 		mcpcia_irq = (device - 2) * 4 + buspin - 1;
    154 	} else {
    155 		printf("dec_kn300_intr_map: weird device number %d\n", device);
    156 		return(1);
    157 	}
    158 
    159 	/*
    160 	 * handle layout:
    161 	 *
    162 	 *	Determine kn300 IRQ (encoded in SCB vector):
    163 	 *	bits 0..1	buspin-1
    164 	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
    165 	 *	bits 5..7	MID-4
    166 	 *	bits 8..10	7-GID
    167 	 *
    168 	 *	Software only:
    169 	 *	bits 11-15	MCPCIA IRQ
    170 	 */
    171 	*ihp = (pci_intr_handle_t)
    172 		(buspin - 1			    )	|
    173 		((device & 0x7)			<< 2)	|
    174 		((ccp->cc_mid - 4)		<< 5)	|
    175 		((7 - ccp->cc_gid)		<< 8)	|
    176 		(mcpcia_irq			<< 11);
    177 	return (0);
    178 }
    179 
    180 const char *
    181 dec_kn300_intr_string(void *ccv, pci_intr_handle_t ih, char *buf, size_t len)
    182 {
    183 	snprintf(buf, len, "kn300 irq %ld", ih & 0x3ff);
    184 	return buf;
    185 }
    186 
    187 const struct evcnt *
    188 dec_kn300_intr_evcnt(void *ccv, pci_intr_handle_t ih)
    189 {
    190 
    191 	return (alpha_shared_intr_evcnt(kn300_pci_intr, ih & 0x3ff));
    192 }
    193 
    194 void *
    195 dec_kn300_intr_establish(
    196 	void *ccv,
    197 	pci_intr_handle_t ih,
    198 	int level,
    199 	int (*func)(void *),
    200 	void *arg)
    201 {
    202 	struct mcpcia_config *ccp = ccv;
    203 	void *cookie;
    204 	int irq;
    205 
    206 	irq = ih & 0x3ff;
    207 	cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
    208 	    level, func, arg, "kn300 irq");
    209 
    210 	if (cookie != NULL &&
    211 	    alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
    212 		scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq),
    213 		    kn300_iointr, NULL, level);
    214 		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
    215 		savirqs[irq] = (ih >> 11) & 0x1f;
    216 		kn300_enable_intr(ccp, savirqs[irq]);
    217 		alpha_mb();
    218 	}
    219 	return (cookie);
    220 }
    221 
    222 void
    223 dec_kn300_intr_disestablish(void *ccv, void *cookie)
    224 {
    225 	panic("dec_kn300_intr_disestablish not implemented");
    226 }
    227 
    228 void
    229 kn300_iointr(void *arg, unsigned long vec)
    230 {
    231 	struct mcpcia_softc *mcp;
    232 	u_long irq;
    233 
    234 	irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);
    235 
    236 	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
    237 		/*
    238 		 * Any claim of an interrupt at this level is a hint to
    239 		 * reset the stray interrupt count- elsewise a slow leak
    240 		 * over time will cause this level to be shutdown.
    241 		 */
    242 		alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
    243 		return;
    244 	}
    245 
    246 	/*
    247 	 * If we haven't finished configuring yet, or there is no mcp
    248 	 * registered for this level yet, just return.
    249 	 */
    250 	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
    251 	if (mcp == NULL || mcp->mcpcia_cc == NULL)
    252 		return;
    253 
    254 	/*
    255 	 * We're getting an interrupt for a device we haven't enabled.
    256 	 * We had better not try and use -1 to find the right bit to disable.
    257 	 */
    258 	if (savirqs[irq] == -1) {
    259 		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
    260 		return;
    261 	}
    262 
    263 	/*
    264 	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
    265 	 * if we've reached the limit.
    266 	 */
    267 	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
    268 	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
    269 		return;
    270 	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
    271 }
    272 
    273 void
    274 kn300_enable_intr(struct mcpcia_config *ccp, int irq)
    275 {
    276 	alpha_mb();
    277 	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
    278 	alpha_mb();
    279 }
    280 
    281 void
    282 kn300_disable_intr(struct mcpcia_config *ccp, int irq)
    283 {
    284 	alpha_mb();
    285 	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
    286 	alpha_mb();
    287 }
    288