Home | History | Annotate | Line # | Download | only in pci
pci_kn300.c revision 1.42
      1  1.42   thorpej /* $NetBSD: pci_kn300.c,v 1.42 2021/07/04 22:36:43 thorpej 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.42   thorpej __KERNEL_RCSID(0, "$NetBSD: pci_kn300.c,v 1.42 2021/07/04 22:36:43 thorpej 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.38   thorpej #include <sys/cpu.h>
     45   1.1    mjacob #include <sys/syslog.h>
     46   1.1    mjacob 
     47   1.1    mjacob #include <machine/autoconf.h>
     48  1.40   thorpej #include <machine/rpb.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 
     58   1.1    mjacob #include "sio.h"
     59  1.16   thorpej #if NSIO > 0 || NPCEB > 0
     60   1.1    mjacob #include <alpha/pci/siovar.h>
     61   1.1    mjacob #endif
     62   1.1    mjacob 
     63  1.37   thorpej static int	dec_kn300_intr_map(const struct pci_attach_args *,
     64  1.37   thorpej 		    pci_intr_handle_t *);
     65  1.37   thorpej static const char *dec_kn300_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
     66  1.37   thorpej 		    char *, size_t);
     67  1.37   thorpej static const struct evcnt *dec_kn300_intr_evcnt(pci_chipset_tag_t,
     68  1.37   thorpej 		    pci_intr_handle_t);
     69  1.37   thorpej static void	*dec_kn300_intr_establish(pci_chipset_tag_t,
     70  1.37   thorpej 		    pci_intr_handle_t, int, int (*func)(void *), void *);
     71  1.37   thorpej static void	dec_kn300_intr_disestablish(pci_chipset_tag_t, void *);
     72   1.1    mjacob 
     73   1.1    mjacob #define	KN300_PCEB_IRQ	16
     74  1.42   thorpej #define	KN300_STRAY_MAX	25
     75   1.6    mjacob #define	NPIN		4
     76   1.1    mjacob 
     77  1.12   thorpej #define	NIRQ	(MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
     78  1.12   thorpej static int savirqs[NIRQ];
     79   1.1    mjacob 
     80   1.6    mjacob static struct alpha_shared_intr *kn300_pci_intr;
     81   1.6    mjacob 
     82  1.13    mjacob static struct mcpcia_config *mcpcia_eisaccp = NULL;
     83  1.13    mjacob 
     84  1.37   thorpej static void	kn300_iointr(void *, unsigned long);
     85  1.37   thorpej static void	kn300_enable_intr(struct mcpcia_config *, int);
     86  1.37   thorpej static void	kn300_disable_intr(struct mcpcia_config *, int);
     87   1.1    mjacob 
     88  1.40   thorpej static void
     89  1.40   thorpej pci_kn300_pickintr(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
     90  1.40   thorpej     pci_chipset_tag_t pc)
     91   1.1    mjacob {
     92  1.40   thorpej 	struct mcpcia_config *ccp = core;
     93  1.42   thorpej 	struct evcnt *ev;
     94  1.42   thorpej 	const char *cp;
     95   1.1    mjacob 
     96  1.39   thorpej 	if (kn300_pci_intr == NULL) {
     97   1.1    mjacob 		int g;
     98   1.6    mjacob 
     99  1.42   thorpej 		kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
    100  1.12   thorpej 		for (g = 0; g < NIRQ; g++) {
    101  1.42   thorpej 			alpha_shared_intr_set_maxstrays(kn300_pci_intr, g,
    102  1.42   thorpej 			    KN300_STRAY_MAX);
    103  1.42   thorpej 
    104  1.42   thorpej 			ev = alpha_shared_intr_evcnt(kn300_pci_intr, g);
    105  1.20   thorpej 			cp = alpha_shared_intr_string(kn300_pci_intr, g);
    106  1.42   thorpej 
    107  1.42   thorpej 			evcnt_attach_dynamic(ev, EVCNT_TYPE_INTR, NULL,
    108  1.20   thorpej 			    "kn300", cp);
    109  1.42   thorpej 
    110   1.6    mjacob 			savirqs[g] = (char) -1;
    111   1.1    mjacob 		}
    112   1.1    mjacob 	}
    113   1.1    mjacob 
    114  1.40   thorpej 	pc->pc_intr_v = core;
    115   1.1    mjacob 	pc->pc_intr_map = dec_kn300_intr_map;
    116   1.1    mjacob 	pc->pc_intr_string = dec_kn300_intr_string;
    117  1.19       cgd 	pc->pc_intr_evcnt = dec_kn300_intr_evcnt;
    118   1.1    mjacob 	pc->pc_intr_establish = dec_kn300_intr_establish;
    119   1.1    mjacob 	pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
    120   1.3   thorpej 
    121   1.4    mjacob 	/* Not supported on KN300. */
    122   1.3   thorpej 	pc->pc_pciide_compat_intr_establish = NULL;
    123   1.1    mjacob 
    124  1.13    mjacob 	if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
    125  1.13    mjacob 		mcpcia_eisaccp = ccp;
    126  1.16   thorpej #if NSIO > 0 || NPCEB > 0
    127  1.11   thorpej 		sio_intr_setup(pc, &ccp->cc_iot);
    128   1.1    mjacob 		kn300_enable_intr(ccp, KN300_PCEB_IRQ);
    129  1.13    mjacob #endif
    130   1.1    mjacob 	}
    131   1.1    mjacob }
    132  1.40   thorpej ALPHA_PCI_INTR_INIT(ST_DEC_4100, pci_kn300_pickintr)
    133   1.1    mjacob 
    134  1.37   thorpej static int
    135  1.34    dyoung dec_kn300_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    136   1.1    mjacob {
    137  1.22  sommerfe 	pcitag_t bustag = pa->pa_intrtag;
    138  1.22  sommerfe 	int buspin = pa->pa_intrpin;
    139  1.22  sommerfe 	pci_chipset_tag_t pc = pa->pa_pc;
    140  1.22  sommerfe 	struct mcpcia_config *ccp = (struct mcpcia_config *)pc->pc_intr_v;
    141   1.1    mjacob 	int device;
    142  1.12   thorpej 	int mcpcia_irq;
    143   1.1    mjacob 
    144   1.5   thorpej 	if (buspin == 0) {
    145   1.5   thorpej 		/* No IRQ used. */
    146   1.5   thorpej 		return 1;
    147   1.5   thorpej 	}
    148  1.37   thorpej 	if (buspin < 0 || buspin > 4) {
    149   1.5   thorpej 		printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
    150   1.5   thorpej 		return 1;
    151   1.5   thorpej 	}
    152   1.1    mjacob 
    153  1.24   thorpej 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    154   1.1    mjacob 
    155  1.11   thorpej 	/*
    156  1.11   thorpej 	 * On MID 5 device 1 is the internal NCR 53c810.
    157  1.11   thorpej 	 */
    158  1.11   thorpej 	if (ccp->cc_mid == 5 && device == 1) {
    159  1.12   thorpej 		mcpcia_irq = 16;
    160   1.1    mjacob 	} else if (device >= 2 && device <= 5) {
    161  1.26    mhitch 		mcpcia_irq = (device - 2) * 4 + buspin - 1;
    162   1.1    mjacob 	} else {
    163  1.15   thorpej 		printf("dec_kn300_intr_map: weird device number %d\n", device);
    164  1.15   thorpej 		return(1);
    165   1.1    mjacob 	}
    166  1.11   thorpej 
    167   1.1    mjacob 	/*
    168   1.1    mjacob 	 * handle layout:
    169   1.1    mjacob 	 *
    170  1.15   thorpej 	 *	Determine kn300 IRQ (encoded in SCB vector):
    171   1.6    mjacob 	 *	bits 0..1	buspin-1
    172   1.6    mjacob 	 *	bits 2..4	PCI Slot (0..7- yes, some don't exist)
    173   1.6    mjacob 	 *	bits 5..7	MID-4
    174   1.6    mjacob 	 *	bits 8..10	7-GID
    175  1.12   thorpej 	 *
    176  1.12   thorpej 	 *	Software only:
    177  1.12   thorpej 	 *	bits 11-15	MCPCIA IRQ
    178   1.1    mjacob 	 */
    179  1.37   thorpej 	const u_int irq =
    180   1.6    mjacob 		(buspin - 1			    )	|
    181   1.6    mjacob 		((device & 0x7)			<< 2)	|
    182  1.11   thorpej 		((ccp->cc_mid - 4)		<< 5)	|
    183  1.11   thorpej 		((7 - ccp->cc_gid)		<< 8)	|
    184  1.15   thorpej 		(mcpcia_irq			<< 11);
    185  1.37   thorpej 	alpha_pci_intr_handle_init(ihp, irq, 0);
    186   1.1    mjacob 	return (0);
    187   1.1    mjacob }
    188   1.1    mjacob 
    189  1.37   thorpej static const char *
    190  1.37   thorpej dec_kn300_intr_string(pci_chipset_tag_t const pc __unused,
    191  1.37   thorpej     pci_intr_handle_t const ih, char * const buf, size_t const len)
    192   1.1    mjacob {
    193  1.37   thorpej 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih) & 0x3ff;
    194  1.37   thorpej 
    195  1.37   thorpej 	snprintf(buf, len, "kn300 irq %u", irq);
    196  1.36  christos 	return buf;
    197  1.19       cgd }
    198  1.19       cgd 
    199  1.37   thorpej static const struct evcnt *
    200  1.37   thorpej dec_kn300_intr_evcnt(pci_chipset_tag_t const pc __unused,
    201  1.37   thorpej     pci_intr_handle_t const ih)
    202  1.19       cgd {
    203  1.37   thorpej 	const u_int irq = alpha_pci_intr_handle_get_irq(&ih) & 0x3ff;
    204  1.19       cgd 
    205  1.37   thorpej 	return alpha_shared_intr_evcnt(kn300_pci_intr, irq);
    206   1.1    mjacob }
    207   1.1    mjacob 
    208  1.37   thorpej static void *
    209  1.35      matt dec_kn300_intr_establish(
    210  1.37   thorpej 	pci_chipset_tag_t const pc,
    211  1.37   thorpej 	pci_intr_handle_t const ih,
    212  1.37   thorpej 	int const level,
    213  1.35      matt 	int (*func)(void *),
    214  1.35      matt 	void *arg)
    215  1.35      matt {
    216  1.37   thorpej 	struct mcpcia_config * const ccp = pc->pc_intr_v;
    217   1.6    mjacob 	void *cookie;
    218  1.37   thorpej 	const u_int ihv = alpha_pci_intr_handle_get_irq(&ih);
    219  1.37   thorpej 	const u_int irq = ihv & 0x3ff;
    220  1.37   thorpej 	const u_int flags = alpha_pci_intr_handle_get_flags(&ih);
    221   1.1    mjacob 
    222  1.38   thorpej 	cookie = alpha_shared_intr_alloc_intrhand(kn300_pci_intr, irq,
    223  1.41   thorpej 	    IST_LEVEL, level, flags, func, arg, "kn300");
    224   1.6    mjacob 
    225  1.38   thorpej 	if (cookie == NULL)
    226  1.38   thorpej 		return NULL;
    227  1.38   thorpej 
    228  1.38   thorpej 	mutex_enter(&cpu_lock);
    229  1.38   thorpej 
    230  1.41   thorpej 	if (! alpha_shared_intr_link(kn300_pci_intr, cookie, "kn300")) {
    231  1.38   thorpej 		mutex_exit(&cpu_lock);
    232  1.38   thorpej 		alpha_shared_intr_free_intrhand(cookie);
    233  1.38   thorpej 		return NULL;
    234  1.38   thorpej 	}
    235  1.38   thorpej 
    236  1.38   thorpej 	if (alpha_shared_intr_firstactive(kn300_pci_intr, irq)) {
    237  1.37   thorpej 		scb_set(MCPCIA_VEC_PCI + SCB_IDXTOVEC(irq), kn300_iointr, NULL);
    238  1.15   thorpej 		alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
    239  1.37   thorpej 		savirqs[irq] = (ihv >> 11) & 0x1f;
    240  1.38   thorpej 		alpha_mb();
    241  1.15   thorpej 		kn300_enable_intr(ccp, savirqs[irq]);
    242   1.1    mjacob 	}
    243  1.38   thorpej 
    244  1.38   thorpej 	mutex_exit(&cpu_lock);
    245  1.38   thorpej 
    246  1.38   thorpej 	return cookie;
    247   1.1    mjacob }
    248   1.1    mjacob 
    249  1.37   thorpej static void
    250  1.37   thorpej dec_kn300_intr_disestablish(pci_chipset_tag_t const pc __unused,
    251  1.37   thorpej     void * const cookie __unused)
    252   1.1    mjacob {
    253   1.6    mjacob 	panic("dec_kn300_intr_disestablish not implemented");
    254   1.1    mjacob }
    255   1.1    mjacob 
    256  1.37   thorpej static void
    257  1.37   thorpej kn300_iointr(void * const arg __unused, unsigned long const vec)
    258   1.1    mjacob {
    259   1.6    mjacob 	struct mcpcia_softc *mcp;
    260  1.12   thorpej 	u_long irq;
    261   1.1    mjacob 
    262  1.23   thorpej 	irq = SCB_VECTOIDX(vec - MCPCIA_VEC_PCI);
    263   1.6    mjacob 
    264  1.17    mjacob 	if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
    265  1.17    mjacob 		/*
    266  1.17    mjacob 		 * Any claim of an interrupt at this level is a hint to
    267  1.17    mjacob 		 * reset the stray interrupt count- elsewise a slow leak
    268  1.17    mjacob 		 * over time will cause this level to be shutdown.
    269  1.17    mjacob 		 */
    270  1.27   thorpej 		alpha_shared_intr_reset_strays(kn300_pci_intr, irq);
    271  1.17    mjacob 		return;
    272  1.17    mjacob 	}
    273  1.17    mjacob 
    274  1.17    mjacob 	/*
    275  1.17    mjacob 	 * If we haven't finished configuring yet, or there is no mcp
    276  1.17    mjacob 	 * registered for this level yet, just return.
    277  1.17    mjacob 	 */
    278  1.17    mjacob 	mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
    279  1.17    mjacob 	if (mcp == NULL || mcp->mcpcia_cc == NULL)
    280  1.17    mjacob 		return;
    281  1.17    mjacob 
    282  1.17    mjacob 	/*
    283  1.17    mjacob 	 * We're getting an interrupt for a device we haven't enabled.
    284  1.17    mjacob 	 * We had better not try and use -1 to find the right bit to disable.
    285  1.17    mjacob 	 */
    286  1.17    mjacob 	if (savirqs[irq] == -1) {
    287  1.17    mjacob 		printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
    288   1.6    mjacob 		return;
    289  1.17    mjacob 	}
    290  1.11   thorpej 
    291   1.6    mjacob 	/*
    292  1.11   thorpej 	 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
    293  1.11   thorpej 	 * if we've reached the limit.
    294   1.6    mjacob 	 */
    295  1.41   thorpej 	alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300");
    296  1.12   thorpej 	if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
    297   1.6    mjacob 		return;
    298  1.12   thorpej 	kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
    299   1.1    mjacob }
    300   1.1    mjacob 
    301  1.37   thorpej static void
    302  1.37   thorpej kn300_enable_intr(struct mcpcia_config * const ccp, int const irq)
    303   1.1    mjacob {
    304   1.1    mjacob 	alpha_mb();
    305  1.11   thorpej 	REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
    306   1.1    mjacob 	alpha_mb();
    307   1.1    mjacob }
    308   1.1    mjacob 
    309  1.37   thorpej static void
    310  1.37   thorpej kn300_disable_intr(struct mcpcia_config * const ccp, int const irq)
    311   1.1    mjacob {
    312   1.1    mjacob 	alpha_mb();
    313  1.11   thorpej 	REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
    314   1.1    mjacob 	alpha_mb();
    315   1.1    mjacob }
    316