Home | History | Annotate | Line # | Download | only in gio
pci_gio.c revision 1.1
      1 /*	$NetBSD: pci_gio.c,v 1.1 2006/08/30 23:58:13 rumble Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Stephen M. Rumble
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. The name of the author may not be used to endorse or promote products
     13  *    derived from this software without specific prior written permission.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: pci_gio.c,v 1.1 2006/08/30 23:58:13 rumble Exp $");
     29 
     30 /*
     31  * Glue for PCI devices that are connected to the GIO bus by various little
     32  * GIO<->PCI ASICs.
     33  *
     34  * We presently support the following boards:
     35  *	o Phobos G100/G130/G160	(if_tlp, lxtphy)
     36  *	o Set Engineering GFE	(if_tl, nsphy)
     37  *
     38  * XXX - G100 and G160 are untested. The former may use an older chipset,
     39  * (21140, I think) though the latter should be essentially identical to
     40  * the G130.
     41  */
     42 
     43 #include "opt_pci.h"
     44 #include "pci.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <sys/extent.h>
     51 
     52 #include <machine/bus.h>
     53 #include <machine/machtype.h>
     54 
     55 #include <sgimips/gio/giovar.h>
     56 #include <sgimips/gio/gioreg.h>
     57 #include <sgimips/gio/giodevs.h>
     58 
     59 #include <sgimips/dev/imcvar.h>
     60 
     61 #include <mips/cache.h>
     62 
     63 #include <dev/pci/pcivar.h>
     64 #include <dev/pci/pcireg.h>
     65 #include <dev/pci/pcidevs.h>
     66 #include <dev/pci/pciconf.h>
     67 
     68 int giopci_debug = 0;
     69 #define DPRINTF(_x)	if (giopci_debug) printf _x
     70 
     71 struct giopci_softc {
     72 	struct device			sc_dev;
     73 	struct sgimips_pci_chipset	sc_pc;
     74 	int				sc_slot;
     75 	int				sc_gprid;
     76 	bus_space_tag_t			sc_iot;
     77 	bus_space_handle_t		sc_ioh;
     78 };
     79 
     80 static int	giopci_match(struct device *, struct cfdata *, void *);
     81 static void	giopci_attach(struct device *, struct device *, void *);
     82 static int	giopci_bus_maxdevs(pci_chipset_tag_t, int);
     83 static pcireg_t	giopci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     84 static void	giopci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     85 static int	giopci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
     86 static int	giopci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
     87 static const char *
     88 		giopci_intr_string(pci_chipset_tag_t, pci_intr_handle_t);
     89 static void    *giopci_intr_establish(int, int, int (*)(void *), void *);
     90 static void	giopci_intr_disestablish(void *);
     91 
     92 #define PHOBOS_PCI_OFFSET	0x00100000
     93 #define PHOBOS_PCI_LENGTH	128		/* ~arbitrary */
     94 #define PHOBOS_TULIP_START	0x00101000
     95 #define PHOBOS_TULIP_END	0x001fffff
     96 
     97 #define SETENG_MAGIC_OFFSET	0x00020000
     98 #define SETENG_MAGIC_VALUE	0x00001000
     99 #define SETENG_PCI_OFFSET	0x00080000
    100 #define SETENG_PCI_LENGTH	128		/* ~arbitrary */
    101 #define SETENG_TLAN_START	0x00100000
    102 #define SETENG_TLAN_END		0x001fffff
    103 
    104 CFATTACH_DECL(giopci, sizeof(struct giopci_softc),
    105     giopci_match, giopci_attach, NULL, NULL);
    106 
    107 static int
    108 giopci_match(struct device *parent, struct cfdata *match, void *aux)
    109 {
    110 	struct gio_attach_args *ga = aux;
    111 	int gprid;
    112 
    113 	/*
    114 	 * I think that these cards are all GIO32-bis. Thus they work
    115 	 * in Indy/Challenge-S and perhaps Indigo R4k as well, though
    116 	 * there are some exceptions (e.g. my Indigo R4k won't power
    117 	 * on with the Set Engineering card installed).
    118 	 */
    119 	if (mach_type != MACH_SGI_IP20 && mach_type != MACH_SGI_IP22)
    120 		return (0);
    121 
    122 	gprid = GIO_PRODUCT_PRODUCTID(ga->ga_product);
    123 	if (gprid == PHOBOS_G100 || gprid == PHOBOS_G130 ||
    124 	    gprid == PHOBOS_G160 || gprid == SETENG_GFE)
    125 		return (1);
    126 
    127 	return (0);
    128 }
    129 
    130 static void
    131 giopci_attach(struct device *parent, struct device *self, void *aux)
    132 {
    133 	struct giopci_softc *sc = (void *)self;
    134 	pci_chipset_tag_t pc = &sc->sc_pc;
    135 	struct gio_attach_args *ga = aux;
    136 	uint32_t pci_off, pci_len, arb;
    137 	struct pcibus_attach_args pba;
    138 	u_long m_start, m_end;
    139 #ifdef PCI_NETBSD_CONFIGURE
    140 	extern int pci_conf_debug;
    141 
    142 	pci_conf_debug = giopci_debug;
    143 #endif
    144 
    145 	sc->sc_iot	= ga->ga_iot;
    146 	sc->sc_slot	= ga->ga_slot;
    147 	sc->sc_gprid	= GIO_PRODUCT_PRODUCTID(ga->ga_product);
    148 
    149 	if (mach_type == MACH_SGI_IP22 &&
    150 	    mach_subtype == MACH_SGI_IP22_FULLHOUSE)
    151 		arb = GIO_ARB_RT | GIO_ARB_MST | GIO_ARB_PIPE;
    152 	else
    153 		arb = GIO_ARB_RT | GIO_ARB_MST;
    154 
    155 	if (gio_arb_config(ga->ga_slot, arb)) {
    156 		printf(": failed to configure GIO bus arbiter\n");
    157 		return;
    158 	}
    159 
    160 #if (NIMC > 0)
    161 	imc_disable_sysad_parity();
    162 #endif
    163 
    164 	switch (sc->sc_gprid) {
    165 	case PHOBOS_G100:
    166 	case PHOBOS_G130:
    167 	case PHOBOS_G160:
    168 		pci_off = PHOBOS_PCI_OFFSET;
    169 		pci_len = PHOBOS_PCI_LENGTH;
    170 		m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_START);
    171 		m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_END);
    172 		break;
    173 
    174 	case SETENG_GFE:
    175 		pci_off = SETENG_PCI_OFFSET;
    176 		pci_len = SETENG_PCI_LENGTH;
    177 		m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_START);
    178 		m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_END);
    179 		bus_space_write_4(ga->ga_iot, ga->ga_ioh,
    180 		    SETENG_MAGIC_OFFSET, SETENG_MAGIC_VALUE);
    181 		break;
    182 
    183 	default:
    184 		panic("giopci_attach: unsupported GIO product id 0x%02x",
    185 		    sc->sc_gprid);
    186 	}
    187 
    188 	if (bus_space_subregion(ga->ga_iot, ga->ga_ioh, pci_off, pci_len,
    189 	    &sc->sc_ioh)) {
    190 		printf("%s: unable to map PCI registers\n",sc->sc_dev.dv_xname);
    191 		return;
    192 	}
    193 
    194 	pc->pc_bus_maxdevs	= giopci_bus_maxdevs;
    195 	pc->pc_conf_read	= giopci_conf_read;
    196 	pc->pc_conf_write	= giopci_conf_write;
    197 	pc->pc_conf_hook	= giopci_conf_hook;
    198 	pc->pc_intr_map		= giopci_intr_map;
    199 	pc->pc_intr_string	= giopci_intr_string;
    200 	pc->intr_establish	= giopci_intr_establish;
    201 	pc->intr_disestablish	= giopci_intr_disestablish;
    202 	pc->iot			= ga->ga_iot;
    203 	pc->ioh			= ga->ga_ioh;
    204 	pc->cookie		= sc;
    205 
    206 	printf(": %s\n", gio_product_string(sc->sc_gprid));
    207 
    208 #ifdef PCI_NETBSD_CONFIGURE
    209 	pc->pc_memext = extent_create("giopcimem", m_start, m_end,
    210 	    M_DEVBUF, NULL, 0, EX_NOWAIT);
    211 	pci_configure_bus(pc, NULL, pc->pc_memext, NULL, 0, mips_dcache_align);
    212 #endif
    213 
    214 	memset(&pba, 0, sizeof(pba));
    215 	pba.pba_memt	= SGIMIPS_BUS_SPACE_MEM;
    216 	pba.pba_dmat	= ga->ga_dmat;
    217 	pba.pba_pc	= pc;
    218 	pba.pba_flags	= PCI_FLAGS_MEM_ENABLED;
    219 	/* NB: do not set PCI_FLAGS_{MRL,MRM,MWI}_OKAY  -- true ?! */
    220 
    221 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    222 }
    223 
    224 static int
    225 giopci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    226 {
    227 
    228 	return (busno == 0);
    229 }
    230 
    231 static pcireg_t
    232 giopci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    233 {
    234 	struct giopci_softc *sc = pc->cookie;
    235 	int bus, dev, func;
    236 	pcireg_t data;
    237 
    238 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    239 	if (bus != 0 || dev != 0 || func != 0)
    240 		return (0);
    241 
    242 	DPRINTF(("giopci_conf_read: reg 0x%x = 0x", reg));
    243 	data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    244 	DPRINTF(("%08x\n", data));
    245 
    246 	return (data);
    247 }
    248 
    249 static void
    250 giopci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    251 {
    252 	struct giopci_softc *sc = pc->cookie;
    253 	int bus, dev, func;
    254 
    255 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    256 	if (bus != 0 || dev != 0 || func != 0)
    257 		return;
    258 
    259 	DPRINTF(("giopci_conf_write: reg 0x%x = 0x%08x\n", reg, data));
    260 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, data);
    261 }
    262 
    263 static int
    264 giopci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function,
    265     pcireg_t id)
    266 {
    267 
    268 	/* All devices use memory accesses only. */
    269 	return (PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_BM);
    270 }
    271 
    272 static int
    273 giopci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    274 {
    275 	struct giopci_softc *sc = pa->pa_pc->cookie;
    276 
    277 	*ihp = sc->sc_slot;
    278 
    279 	return (0);
    280 }
    281 
    282 static const char *
    283 giopci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    284 {
    285 	static char str[10];
    286 
    287 	snprintf(str, sizeof(str), "slot %s",
    288 	    (ih == GIO_SLOT_EXP0) ? "EXP0" :
    289 	    (ih == GIO_SLOT_EXP1) ? "EXP1" : "GFX");
    290 	return (str);
    291 }
    292 
    293 static void *
    294 giopci_intr_establish(int slot, int level, int (*func)(void *), void *arg)
    295 {
    296 
    297 	return (gio_intr_establish(slot, level, func, arg));
    298 }
    299 
    300 static void
    301 giopci_intr_disestablish(void *cookie)
    302 {
    303 
    304 	panic("giopci_intr_disestablish: impossible.");
    305 }
    306