Home | History | Annotate | Line # | Download | only in pci
necpb.c revision 1.47
      1  1.47   thorpej /*	$NetBSD: necpb.c,v 1.47 2021/04/24 23:36:25 thorpej Exp $	*/
      2   1.1      soda 
      3   1.1      soda /*-
      4   1.1      soda  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1      soda  * All rights reserved.
      6   1.1      soda  *
      7   1.1      soda  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      soda  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1      soda  * NASA Ames Research Center.
     10   1.1      soda  *
     11   1.1      soda  * Redistribution and use in source and binary forms, with or without
     12   1.1      soda  * modification, are permitted provided that the following conditions
     13   1.1      soda  * are met:
     14   1.1      soda  * 1. Redistributions of source code must retain the above copyright
     15   1.1      soda  *    notice, this list of conditions and the following disclaimer.
     16   1.1      soda  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1      soda  *    notice, this list of conditions and the following disclaimer in the
     18   1.1      soda  *    documentation and/or other materials provided with the distribution.
     19   1.1      soda  *
     20   1.1      soda  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1      soda  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1      soda  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1      soda  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1      soda  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1      soda  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1      soda  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1      soda  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1      soda  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1      soda  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1      soda  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1      soda  */
     32   1.1      soda 
     33   1.1      soda /*
     34   1.1      soda  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     35   1.1      soda  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     36   1.1      soda  *
     37   1.1      soda  * Redistribution and use in source and binary forms, with or without
     38   1.1      soda  * modification, are permitted provided that the following conditions
     39   1.1      soda  * are met:
     40   1.1      soda  * 1. Redistributions of source code must retain the above copyright
     41   1.1      soda  *    notice, this list of conditions and the following disclaimer.
     42   1.1      soda  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1      soda  *    notice, this list of conditions and the following disclaimer in the
     44   1.1      soda  *    documentation and/or other materials provided with the distribution.
     45   1.1      soda  * 3. All advertising materials mentioning features or use of this software
     46   1.1      soda  *    must display the following acknowledgement:
     47   1.1      soda  *	This product includes software developed by Charles M. Hannum.
     48   1.1      soda  * 4. The name of the author may not be used to endorse or promote products
     49   1.1      soda  *    derived from this software without specific prior written permission.
     50   1.1      soda  *
     51   1.1      soda  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52   1.1      soda  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53   1.1      soda  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54   1.1      soda  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55   1.1      soda  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56   1.1      soda  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57   1.1      soda  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58   1.1      soda  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59   1.1      soda  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60   1.1      soda  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61   1.1      soda  */
     62  1.16     lukem 
     63  1.16     lukem #include <sys/cdefs.h>
     64  1.47   thorpej __KERNEL_RCSID(0, "$NetBSD: necpb.c,v 1.47 2021/04/24 23:36:25 thorpej Exp $");
     65  1.25   tsutsui 
     66  1.25   tsutsui #include "opt_pci.h"
     67   1.1      soda 
     68   1.1      soda #include <sys/types.h>
     69   1.1      soda #include <sys/param.h>
     70   1.1      soda #include <sys/time.h>
     71   1.1      soda #include <sys/systm.h>
     72   1.1      soda #include <sys/errno.h>
     73   1.1      soda #include <sys/device.h>
     74  1.46   thorpej #include <sys/kmem.h>
     75   1.1      soda #include <sys/extent.h>
     76   1.3       mrg 
     77   1.3       mrg #include <uvm/uvm_extern.h>
     78   1.1      soda 
     79   1.1      soda #define _ARC_BUS_DMA_PRIVATE
     80  1.37    dyoung #include <sys/bus.h>
     81   1.1      soda 
     82   1.1      soda #include <machine/pio.h>
     83   1.1      soda 
     84   1.1      soda #include <machine/autoconf.h>
     85   1.1      soda #include <machine/cpu.h>
     86   1.8      soda #include <machine/platform.h>
     87   1.1      soda 
     88  1.26   tsutsui #include <mips/cache.h>
     89  1.26   tsutsui 
     90   1.1      soda #include <dev/pci/pcivar.h>
     91   1.1      soda #include <dev/pci/pcireg.h>
     92  1.27   tsutsui #include <dev/pci/pcidevs.h>
     93  1.25   tsutsui #ifdef PCI_NETBSD_CONFIGURE
     94  1.25   tsutsui #include <dev/pci/pciconf.h>
     95  1.25   tsutsui #endif
     96   1.1      soda 
     97   1.6        ur #include <arc/jazz/rd94.h>
     98   1.1      soda #include <arc/pci/necpbvar.h>
     99   1.1      soda 
    100  1.22   tsutsui #include "ioconf.h"
    101  1.22   tsutsui 
    102  1.39       chs static int	necpbmatch(device_t, cfdata_t, void *);
    103  1.39       chs static void	necpbattach(device_t, device_t, void *);
    104   1.1      soda 
    105  1.39       chs static void	necpb_attach_hook(device_t, device_t,
    106  1.19   tsutsui 		    struct pcibus_attach_args *);
    107  1.32   tsutsui static int	necpb_bus_maxdevs(pci_chipset_tag_t, int);
    108  1.32   tsutsui static pcitag_t	necpb_make_tag(pci_chipset_tag_t, int, int, int);
    109  1.32   tsutsui static void	necpb_decompose_tag(pci_chipset_tag_t, pcitag_t, int *,
    110  1.19   tsutsui 		    int *, int *);
    111  1.32   tsutsui static pcireg_t	necpb_conf_read(pci_chipset_tag_t, pcitag_t, int);
    112  1.32   tsutsui static void	necpb_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
    113  1.35    dyoung static int	necpb_intr_map(const struct pci_attach_args *,
    114  1.35    dyoung 		    pci_intr_handle_t *);
    115  1.41  christos static const char *necpb_intr_string(pci_chipset_tag_t, pci_intr_handle_t,
    116  1.41  christos 		    char *, size_t);
    117  1.32   tsutsui static void	*necpb_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
    118  1.19   tsutsui 		    int, int (*func)(void *), void *);
    119  1.32   tsutsui static void	necpb_intr_disestablish(pci_chipset_tag_t, void *);
    120  1.25   tsutsui #ifdef PCI_NETBSD_CONFIGURE
    121  1.32   tsutsui static void	necpb_conf_interrupt(pci_chipset_tag_t, int, int, int, int,
    122  1.25   tsutsui 		    int *);
    123  1.32   tsutsui static int	necpb_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
    124  1.25   tsutsui #endif
    125   1.1      soda 
    126  1.32   tsutsui static uint32_t	necpb_intr(uint32_t, struct clockframe *);
    127   1.1      soda 
    128   1.1      soda 
    129  1.32   tsutsui CFATTACH_DECL_NEW(necpb, sizeof(struct necpb_softc),
    130  1.13   thorpej     necpbmatch, necpbattach, NULL, NULL);
    131   1.1      soda 
    132   1.1      soda static struct necpb_intrhand	*necpb_inttbl[4];
    133   1.1      soda 
    134   1.2      soda /* There can be only one. */
    135   1.2      soda int necpbfound;
    136   1.8      soda struct necpb_context necpb_main_context;
    137   1.2      soda static long necpb_mem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(10) / sizeof(long)];
    138   1.2      soda static long necpb_io_ex_storage[EXTENT_FIXED_STORAGE_SIZE(10) / sizeof(long)];
    139   1.2      soda 
    140  1.45   thorpej #define	PCI_IO_START	0x00100000
    141  1.45   thorpej #define	PCI_IO_END	0x01ffffff
    142  1.45   thorpej #define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
    143  1.45   thorpej 
    144  1.45   thorpej #define	PCI_MEM_START	0x08000000
    145  1.45   thorpej #define	PCI_MEM_END	0x3fffffff
    146  1.45   thorpej #define	PCI_MEM_SIZE	((PCI_MEM_END - PCI_MEM_START) + 1)
    147  1.45   thorpej 
    148  1.32   tsutsui static int
    149  1.32   tsutsui necpbmatch(device_t parent, cfdata_t cf, void *aux)
    150   1.1      soda {
    151   1.1      soda 	struct confargs *ca = aux;
    152   1.1      soda 
    153   1.1      soda 	if (strcmp(ca->ca_name, necpb_cd.cd_name) != 0)
    154  1.19   tsutsui 		return 0;
    155   1.1      soda 
    156   1.2      soda 	if (necpbfound)
    157  1.19   tsutsui 		return 0;
    158   1.2      soda 
    159  1.19   tsutsui 	return 1;
    160   1.1      soda }
    161   1.1      soda 
    162   1.2      soda /*
    163   1.2      soda  * Set up the chipset's function pointers.
    164   1.2      soda  */
    165   1.2      soda void
    166  1.19   tsutsui necpb_init(struct necpb_context *ncp)
    167   1.2      soda {
    168  1.23   tsutsui 	pci_chipset_tag_t pc;
    169  1.25   tsutsui #ifndef PCI_NETBSD_CONFIGURE
    170   1.2      soda 	pcitag_t tag;
    171  1.27   tsutsui 	pcireg_t id, class, csr;
    172  1.27   tsutsui 	u_int dev;
    173  1.25   tsutsui #endif
    174   1.2      soda 
    175   1.2      soda 	if (ncp->nc_initialized)
    176   1.2      soda 		return;
    177   1.2      soda 
    178   1.2      soda 	arc_large_bus_space_init(&ncp->nc_memt, "necpcimem",
    179   1.2      soda 	    RD94_P_PCI_MEM, 0, RD94_S_PCI_MEM);
    180  1.29  christos 	arc_bus_space_init_extent(&ncp->nc_memt, (void *)necpb_mem_ex_storage,
    181   1.2      soda 	    sizeof(necpb_mem_ex_storage));
    182   1.2      soda 
    183   1.2      soda 	arc_bus_space_init(&ncp->nc_iot, "necpciio",
    184   1.2      soda 	    RD94_P_PCI_IO, RD94_V_PCI_IO, 0, RD94_S_PCI_IO);
    185  1.29  christos 	arc_bus_space_init_extent(&ncp->nc_iot, (void *)necpb_io_ex_storage,
    186   1.2      soda 	    sizeof(necpb_io_ex_storage));
    187   1.2      soda 
    188   1.2      soda 	jazz_bus_dma_tag_init(&ncp->nc_dmat);
    189   1.2      soda 
    190  1.23   tsutsui 	pc = &ncp->nc_pc;
    191  1.23   tsutsui 	pc->pc_attach_hook = necpb_attach_hook;
    192  1.23   tsutsui 	pc->pc_bus_maxdevs = necpb_bus_maxdevs;
    193  1.23   tsutsui 	pc->pc_make_tag = necpb_make_tag;
    194  1.23   tsutsui 	pc->pc_decompose_tag = necpb_decompose_tag;
    195  1.23   tsutsui 	pc->pc_conf_read = necpb_conf_read;
    196  1.23   tsutsui 	pc->pc_conf_write = necpb_conf_write;
    197  1.23   tsutsui 	pc->pc_intr_map = necpb_intr_map;
    198  1.23   tsutsui 	pc->pc_intr_string = necpb_intr_string;
    199  1.23   tsutsui 	pc->pc_intr_establish = necpb_intr_establish;
    200  1.23   tsutsui 	pc->pc_intr_disestablish = necpb_intr_disestablish;
    201  1.25   tsutsui #ifdef PCI_NETBSD_CONFIGURE
    202  1.25   tsutsui 	pc->pc_conf_interrupt = necpb_conf_interrupt;
    203  1.25   tsutsui 	pc->pc_conf_hook = necpb_conf_hook;
    204  1.25   tsutsui #endif
    205   1.2      soda 
    206  1.25   tsutsui #ifndef PCI_NETBSD_CONFIGURE
    207   1.9        ur 	/*
    208   1.9        ur 	 * XXX:
    209   1.9        ur 	 *  NEC's firmware does not configure PCI devices completely.
    210   1.9        ur 	 *  We need to disable expansion ROM and enable mem/io/busmaster
    211   1.9        ur 	 *  bits here.
    212   1.9        ur 	 */
    213  1.27   tsutsui 	for (dev = 3; dev <= 5; dev++) {
    214  1.27   tsutsui 		tag = necpb_make_tag(pc, 0, dev, 0);
    215  1.27   tsutsui 		id = necpb_conf_read(pc, tag, PCI_ID_REG);
    216  1.27   tsutsui 
    217  1.27   tsutsui 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    218  1.27   tsutsui 			continue;
    219  1.27   tsutsui 
    220  1.27   tsutsui 		class = necpb_conf_read(pc, tag, PCI_CLASS_REG);
    221  1.27   tsutsui 		csr = necpb_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    222  1.27   tsutsui 		if (PCI_CLASS(class) != PCI_CLASS_BRIDGE ||
    223  1.27   tsutsui 		    PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_PCI) {
    224  1.27   tsutsui 			csr |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
    225  1.27   tsutsui 			necpb_conf_write(pc, tag, PCI_MAPREG_ROM, 0);
    226  1.27   tsutsui 		}
    227  1.27   tsutsui 		csr |= PCI_COMMAND_MASTER_ENABLE;
    228  1.27   tsutsui 		necpb_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    229  1.27   tsutsui 	}
    230  1.25   tsutsui #endif
    231   1.2      soda 
    232   1.2      soda 	ncp->nc_initialized = 1;
    233   1.2      soda }
    234   1.2      soda 
    235  1.32   tsutsui static void
    236  1.32   tsutsui necpbattach(device_t parent, device_t self, void *aux)
    237   1.1      soda {
    238  1.32   tsutsui 	struct necpb_softc *sc = device_private(self);
    239   1.1      soda 	struct pcibus_attach_args pba;
    240  1.23   tsutsui 	pci_chipset_tag_t pc;
    241   1.1      soda 	int i;
    242   1.1      soda 
    243  1.32   tsutsui 	sc->sc_dev = self;
    244  1.32   tsutsui 
    245   1.2      soda 	necpbfound = 1;
    246   1.2      soda 
    247  1.32   tsutsui 	aprint_normal("\n");
    248   1.1      soda 
    249   1.8      soda 	sc->sc_ncp = &necpb_main_context;
    250   1.2      soda 	necpb_init(sc->sc_ncp);
    251   1.1      soda 
    252  1.23   tsutsui 	pc = &sc->sc_ncp->nc_pc;
    253  1.25   tsutsui #ifdef PCI_NETBSD_CONFIGURE
    254  1.45   thorpej 	struct pciconf_resources *pcires = pciconf_resource_init();
    255  1.45   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    256  1.45   thorpej 	    PCI_IO_START, PCI_IO_SIZE);
    257  1.45   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    258  1.45   thorpej 	    PCI_MEM_START, PCI_MEM_SIZE);
    259  1.45   thorpej 	pci_configure_bus(pc, pcires, 0, mips_cache_info.mci_dcache_align);
    260  1.45   thorpej 	pciconf_resource_fini(pcires);
    261  1.25   tsutsui #endif
    262  1.23   tsutsui 
    263   1.1      soda 	out32(RD94_SYS_PCI_INTMASK, 0xf);
    264   1.1      soda 
    265   1.1      soda 	for (i = 0; i < 4; i++)
    266   1.1      soda 		necpb_inttbl[i] = NULL;
    267   1.1      soda 
    268  1.28   tsutsui 	(*platform->set_intr)(MIPS_INT_MASK_2, necpb_intr, ARC_INTPRI_PCIISA);
    269   1.1      soda 
    270   1.2      soda 	pba.pba_iot = &sc->sc_ncp->nc_iot;
    271   1.2      soda 	pba.pba_memt = &sc->sc_ncp->nc_memt;
    272   1.2      soda 	pba.pba_dmat = &sc->sc_ncp->nc_dmat;
    273  1.15      fvdl 	pba.pba_dmat64 = NULL;
    274  1.23   tsutsui 	pba.pba_pc = pc;
    275  1.36    dyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
    276   1.1      soda 	pba.pba_bus = 0;
    277  1.11   thorpej 	pba.pba_bridgetag = NULL;
    278   1.1      soda 
    279  1.47   thorpej 	config_found(self, &pba, pcibusprint, CFARG_EOL);
    280   1.1      soda }
    281   1.1      soda 
    282  1.32   tsutsui static void
    283  1.39       chs necpb_attach_hook(device_t parent, device_t self,
    284  1.19   tsutsui     struct pcibus_attach_args *pba)
    285   1.1      soda {
    286   1.1      soda }
    287   1.1      soda 
    288  1.32   tsutsui static int
    289  1.19   tsutsui necpb_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    290   1.1      soda {
    291  1.19   tsutsui 
    292  1.19   tsutsui 	return 32;
    293   1.1      soda }
    294   1.1      soda 
    295  1.32   tsutsui static pcitag_t
    296  1.19   tsutsui necpb_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    297   1.1      soda {
    298   1.1      soda 	pcitag_t tag;
    299   1.1      soda 
    300   1.1      soda 	if (bus >= 256 || device >= 32 || function >= 8)
    301  1.32   tsutsui 		panic("%s: bad request", __func__);
    302   1.1      soda 
    303   1.1      soda 	tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
    304  1.19   tsutsui 	return tag;
    305   1.1      soda }
    306   1.1      soda 
    307  1.32   tsutsui static void
    308  1.19   tsutsui necpb_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp,
    309  1.19   tsutsui    int *fp)
    310   1.1      soda {
    311  1.19   tsutsui 
    312   1.1      soda 	if (bp != NULL)
    313   1.1      soda 		*bp = (tag >> 16) & 0xff;
    314   1.1      soda 	if (dp != NULL)
    315   1.1      soda 		*dp = (tag >> 11) & 0x1f;
    316   1.1      soda 	if (fp != NULL)
    317   1.1      soda 		*fp = (tag >> 8) & 0x07;
    318   1.1      soda }
    319   1.1      soda 
    320  1.32   tsutsui static pcireg_t
    321  1.19   tsutsui necpb_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    322   1.1      soda {
    323   1.1      soda 	pcireg_t data;
    324   1.1      soda 	int s;
    325   1.1      soda 
    326  1.42   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    327  1.42   msaitoh 		return (pcireg_t) -1;
    328  1.42   msaitoh 
    329   1.1      soda 	s = splhigh();
    330   1.1      soda 	out32(RD94_SYS_PCI_CONFADDR, tag | reg);
    331   1.1      soda 	data = in32(RD94_SYS_PCI_CONFDATA);
    332   1.1      soda 	out32(RD94_SYS_PCI_CONFADDR, 0);
    333   1.1      soda 	splx(s);
    334   1.1      soda 
    335  1.19   tsutsui 	return data;
    336   1.1      soda }
    337   1.1      soda 
    338  1.32   tsutsui static void
    339  1.19   tsutsui necpb_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    340   1.1      soda {
    341   1.1      soda 	int s;
    342   1.1      soda 
    343  1.42   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    344  1.42   msaitoh 		return;
    345  1.42   msaitoh 
    346   1.1      soda 	s = splhigh();
    347   1.1      soda 	out32(RD94_SYS_PCI_CONFADDR, tag | reg);
    348   1.1      soda 	out32(RD94_SYS_PCI_CONFDATA, data);
    349   1.1      soda 	out32(RD94_SYS_PCI_CONFADDR, 0);
    350   1.1      soda 	splx(s);
    351   1.1      soda }
    352   1.1      soda 
    353  1.32   tsutsui static int
    354  1.35    dyoung necpb_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    355   1.1      soda {
    356   1.7        ur 	pci_chipset_tag_t pc = pa->pa_pc;
    357   1.7        ur 	pcitag_t intrtag = pa->pa_intrtag;
    358   1.7        ur 	int pin = pa->pa_intrpin;
    359   1.1      soda 	int bus, dev;
    360   1.1      soda 
    361   1.1      soda 	if (pin == 0) {
    362   1.1      soda 		/* No IRQ used. */
    363   1.1      soda 		*ihp = -1;
    364  1.19   tsutsui 		return 1;
    365   1.1      soda 	}
    366   1.1      soda 
    367   1.1      soda 	if (pin > 4) {
    368   1.1      soda 		printf("necpb_intr_map: bad interrupt pin %d\n", pin);
    369   1.1      soda 		*ihp = -1;
    370  1.19   tsutsui 		return 1;
    371   1.1      soda 	}
    372   1.1      soda 
    373   1.1      soda 	necpb_decompose_tag(pc, intrtag, &bus, &dev, NULL);
    374   1.1      soda 	if (bus != 0) {
    375  1.25   tsutsui 		printf("necpb_intr_map: unknown bus %d\n", bus);
    376   1.1      soda 		*ihp = -1;
    377  1.19   tsutsui 		return 1;
    378   1.1      soda 	}
    379   1.1      soda 
    380   1.1      soda 	switch (dev) {
    381   1.1      soda 	case 3:
    382  1.33   tsutsui 		*ihp = 3;
    383   1.1      soda 		break;
    384   1.1      soda 	case 4:
    385  1.33   tsutsui 		*ihp = 2;
    386   1.1      soda 		break;
    387   1.1      soda 	case 5:
    388  1.33   tsutsui 		*ihp = 1;
    389   1.1      soda 		break;
    390   1.1      soda 	default:
    391   1.1      soda 		*ihp = -1;
    392  1.19   tsutsui 		return 1;
    393   1.1      soda 	}
    394   1.1      soda 
    395  1.19   tsutsui 	return 0;
    396   1.1      soda }
    397   1.1      soda 
    398  1.32   tsutsui static const char *
    399  1.41  christos necpb_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
    400  1.41  christos     size_t len)
    401   1.1      soda {
    402   1.1      soda 	if (ih >= 4)
    403  1.32   tsutsui 		panic("%s: bogus handle %ld", __func__, ih);
    404  1.41  christos 	snprintf(buf, len, "int %c", 'A' + (int)ih);
    405  1.41  christos 	return buf;
    406   1.1      soda }
    407   1.1      soda 
    408  1.32   tsutsui static void *
    409  1.19   tsutsui necpb_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
    410  1.19   tsutsui     int (*func)(void *), void *arg)
    411   1.1      soda {
    412   1.1      soda 	struct necpb_intrhand *n, *p;
    413  1.23   tsutsui 	uint32_t mask;
    414  1.41  christos 	char buf[PCI_INTRSTR_LEN];
    415   1.1      soda 
    416   1.1      soda 	if (ih >= 4)
    417  1.32   tsutsui 		panic("%s: bogus handle", __func__);
    418   1.1      soda 
    419  1.46   thorpej 	n = kmem_alloc(sizeof(*n), KM_SLEEP);
    420   1.1      soda 	n->ih_func = func;
    421   1.1      soda 	n->ih_arg = arg;
    422   1.1      soda 	n->ih_next = NULL;
    423   1.1      soda 	n->ih_intn = ih;
    424  1.41  christos 	strlcpy(n->ih_evname, necpb_intr_string(pc, ih, buf, sizeof(buf)),
    425  1.41  christos 	    sizeof(n->ih_evname));
    426  1.17   tsutsui 	evcnt_attach_dynamic(&n->ih_evcnt, EVCNT_TYPE_INTR, NULL, "necpb",
    427  1.17   tsutsui 	    n->ih_evname);
    428   1.1      soda 
    429   1.1      soda 	if (necpb_inttbl[ih] == NULL) {
    430   1.1      soda 		necpb_inttbl[ih] = n;
    431   1.1      soda 		mask = in32(RD94_SYS_PCI_INTMASK);
    432   1.1      soda 		mask |= 1 << ih;
    433   1.1      soda 		out32(RD94_SYS_PCI_INTMASK, mask);
    434   1.1      soda 	} else {
    435   1.1      soda 		p = necpb_inttbl[ih];
    436   1.1      soda 		while (p->ih_next != NULL)
    437   1.1      soda 			p = p->ih_next;
    438   1.1      soda 		p->ih_next = n;
    439   1.1      soda 	}
    440   1.1      soda 
    441   1.1      soda 	return n;
    442   1.1      soda }
    443   1.1      soda 
    444  1.32   tsutsui static void
    445  1.19   tsutsui necpb_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
    446   1.1      soda {
    447   1.1      soda 	struct necpb_intrhand *n, *p, *q;
    448  1.23   tsutsui 	uint32_t mask;
    449   1.1      soda 
    450   1.1      soda 	n = cookie;
    451   1.1      soda 
    452   1.1      soda 	q = NULL;
    453   1.1      soda 	p = necpb_inttbl[n->ih_intn];
    454   1.1      soda 	while (p != n) {
    455   1.1      soda 		if (p == NULL)
    456  1.32   tsutsui 			panic("%s: broken intr table", __func__);
    457   1.1      soda 		q = p;
    458   1.1      soda 		p = p->ih_next;
    459   1.1      soda 	}
    460   1.1      soda 
    461   1.1      soda 	if (q == NULL) {
    462   1.1      soda 		necpb_inttbl[n->ih_intn] = n->ih_next;
    463   1.1      soda 		if (n->ih_next == NULL) {
    464   1.1      soda 			mask = in32(RD94_SYS_PCI_INTMASK);
    465   1.1      soda 			mask &= ~(1 << n->ih_intn);
    466   1.1      soda 			out32(RD94_SYS_PCI_INTMASK, mask);
    467   1.1      soda 		}
    468   1.1      soda 	} else
    469   1.1      soda 		q->ih_next = n->ih_next;
    470   1.1      soda 
    471  1.17   tsutsui 	evcnt_detach(&n->ih_evcnt);
    472  1.17   tsutsui 
    473  1.46   thorpej 	kmem_free(n, sizeof(*n));
    474   1.1      soda }
    475   1.1      soda 
    476   1.1      soda /*
    477   1.1      soda  *   Handle PCI/EISA interrupt.
    478   1.1      soda  */
    479  1.32   tsutsui static uint32_t
    480  1.20   tsutsui necpb_intr(uint32_t mask, struct clockframe *cf)
    481   1.1      soda {
    482  1.19   tsutsui 	uint32_t vector, stat;
    483   1.1      soda 	struct necpb_intrhand *p;
    484  1.24   tsutsui 	int i, handled;
    485   1.1      soda 
    486  1.24   tsutsui 	handled = 0;
    487   1.1      soda 	vector = in32(RD94_SYS_INTSTAT2) & 0xffff;
    488   1.1      soda 
    489   1.1      soda 	if (vector == 0x4000) {
    490   1.1      soda 		stat = in32(RD94_SYS_PCI_INTSTAT);
    491   1.1      soda 		stat &= in32(RD94_SYS_PCI_INTMASK);
    492  1.23   tsutsui 		for (i = 0; i < 4; i++) {
    493  1.23   tsutsui 			if (stat & (1 << i)) {
    494   1.1      soda #if 0
    495  1.23   tsutsui 				printf("pint %d\n", i);
    496   1.1      soda #endif
    497  1.23   tsutsui 				p = necpb_inttbl[i];
    498   1.1      soda 				while (p != NULL) {
    499  1.24   tsutsui 					if ((*p->ih_func)(p->ih_arg)) {
    500  1.24   tsutsui 						p->ih_evcnt.ev_count++;
    501  1.24   tsutsui 						handled |= 1;
    502  1.24   tsutsui 					}
    503   1.1      soda 					p = p->ih_next;
    504   1.1      soda 				}
    505   1.1      soda 			}
    506   1.1      soda 		}
    507   1.1      soda 	} else if (vector == 0x8000) {
    508   1.1      soda 		printf("eisa_nmi\n");
    509   1.1      soda 	} else {
    510   1.1      soda 		printf("eint %d\n", vector & 0xff);
    511   1.1      soda #if 0
    512  1.24   tsutsui 		if (eisa_intr(vector & 0xff)) {
    513  1.24   tsutsui 			handled |= 1;
    514  1.24   tsutsui 		}
    515   1.1      soda #endif
    516   1.1      soda 	}
    517   1.1      soda 
    518  1.30   tsutsui 	return handled ? MIPS_INT_MASK_2 : 0;
    519   1.1      soda }
    520  1.25   tsutsui 
    521  1.25   tsutsui #ifdef PCI_NETBSD_CONFIGURE
    522  1.32   tsutsui static void
    523  1.25   tsutsui necpb_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int func,
    524  1.25   tsutsui     int swiz, int *iline)
    525  1.25   tsutsui {
    526  1.25   tsutsui 
    527  1.25   tsutsui 	return;
    528  1.25   tsutsui }
    529  1.25   tsutsui 
    530  1.32   tsutsui static int
    531  1.25   tsutsui necpb_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func,
    532  1.25   tsutsui     pcireg_t id)
    533  1.25   tsutsui {
    534  1.25   tsutsui 
    535  1.25   tsutsui 	/* ignore bogus IDs */
    536  1.25   tsutsui 	if (id == 0)
    537  1.25   tsutsui 		return 0;
    538  1.25   tsutsui 
    539  1.25   tsutsui 	/* don't configure bridges */
    540  1.25   tsutsui 	if (bus == 0 && (dev == 1 || dev == 2))
    541  1.25   tsutsui 		return 0;
    542  1.25   tsutsui 
    543  1.25   tsutsui 	return PCI_CONF_DEFAULT;
    544  1.25   tsutsui }
    545  1.25   tsutsui #endif
    546