Home | History | Annotate | Line # | Download | only in gio
pci_gio.c revision 1.4
      1 /*	$NetBSD: pci_gio.c,v 1.4 2006/12/29 00:04:20 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.4 2006/12/29 00:04:20 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 is untested. It may use an older chipset -- 21140, I think.
     39  */
     40 
     41 #include "opt_pci.h"
     42 #include "pci.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/extent.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/machtype.h>
     52 
     53 #include <sgimips/gio/giovar.h>
     54 #include <sgimips/gio/gioreg.h>
     55 #include <sgimips/gio/giodevs.h>
     56 
     57 #include <sgimips/dev/imcvar.h>
     58 
     59 #include <mips/cache.h>
     60 
     61 #include <dev/pci/pcivar.h>
     62 #include <dev/pci/pcireg.h>
     63 #include <dev/pci/pcidevs.h>
     64 #include <dev/pci/pciconf.h>
     65 
     66 int giopci_debug = 0;
     67 #define DPRINTF(_x)	if (giopci_debug) printf _x
     68 
     69 struct giopci_softc {
     70 	struct device			sc_dev;
     71 	struct sgimips_pci_chipset	sc_pc;
     72 	int				sc_slot;
     73 	int				sc_gprid;
     74 	uint32_t			sc_pci_len;
     75 	bus_space_tag_t			sc_iot;
     76 	bus_space_handle_t		sc_ioh;
     77 };
     78 
     79 static int	giopci_match(struct device *, struct cfdata *, void *);
     80 static void	giopci_attach(struct device *, struct device *, void *);
     81 static int	giopci_bus_maxdevs(pci_chipset_tag_t, int);
     82 static pcireg_t	giopci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     83 static void	giopci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     84 static int	giopci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
     85 static int	giopci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
     86 static const char *
     87 		giopci_intr_string(pci_chipset_tag_t, pci_intr_handle_t);
     88 static void    *giopci_intr_establish(int, int, int (*)(void *), void *);
     89 static void	giopci_intr_disestablish(void *);
     90 
     91 #define PHOBOS_PCI_OFFSET	0x00100000
     92 #define PHOBOS_PCI_LENGTH	128		/* ~arbitrary */
     93 #define PHOBOS_TULIP_START	0x00101000
     94 #define PHOBOS_TULIP_END	0x001fffff
     95 
     96 #define SETENG_MAGIC_OFFSET	0x00020000
     97 #define SETENG_MAGIC_VALUE	0x00001000
     98 #define SETENG_PCI_OFFSET	0x00080000
     99 #define SETENG_PCI_LENGTH	128		/* ~arbitrary */
    100 #define SETENG_TLAN_START	0x00100000
    101 #define SETENG_TLAN_END		0x001fffff
    102 
    103 CFATTACH_DECL(giopci, sizeof(struct giopci_softc),
    104     giopci_match, giopci_attach, NULL, NULL);
    105 
    106 static int
    107 giopci_match(struct device *parent, struct cfdata *match, void *aux)
    108 {
    109 	struct gio_attach_args *ga = aux;
    110 	int gprid;
    111 
    112 	/*
    113 	 * I think that these cards are all GIO32-bis or GIO64. Thus
    114 	 * they work in either Indigo2/Challenge M or
    115 	 * Indy/Challenge S/Indigo R4k, according to form factor. However,
    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 		/*
    176 		 * NB: The SetEng board does not allow the ThunderLAN's DMA
    177 		 *     engine to properly transfer segments that span page
    178 		 *     boundaries. See sgimips/autoconf.c where we catch a
    179 		 *     tl(4) device attachment and create an appropriate
    180 		 *     proplib entry to enable the workaround.
    181 		 */
    182 		pci_off = SETENG_PCI_OFFSET;
    183 		pci_len = SETENG_PCI_LENGTH;
    184 		m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_START);
    185 		m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_END);
    186 		bus_space_write_4(ga->ga_iot, ga->ga_ioh,
    187 		    SETENG_MAGIC_OFFSET, SETENG_MAGIC_VALUE);
    188 		break;
    189 
    190 	default:
    191 		panic("giopci_attach: unsupported GIO product id 0x%02x",
    192 		    sc->sc_gprid);
    193 	}
    194 
    195 	if (bus_space_subregion(ga->ga_iot, ga->ga_ioh, pci_off, pci_len,
    196 	    &sc->sc_ioh)) {
    197 		printf("%s: unable to map PCI registers\n",sc->sc_dev.dv_xname);
    198 		return;
    199 	}
    200 	sc->sc_pci_len = pci_len;
    201 
    202 	pc->pc_bus_maxdevs	= giopci_bus_maxdevs;
    203 	pc->pc_conf_read	= giopci_conf_read;
    204 	pc->pc_conf_write	= giopci_conf_write;
    205 	pc->pc_conf_hook	= giopci_conf_hook;
    206 	pc->pc_intr_map		= giopci_intr_map;
    207 	pc->pc_intr_string	= giopci_intr_string;
    208 	pc->intr_establish	= giopci_intr_establish;
    209 	pc->intr_disestablish	= giopci_intr_disestablish;
    210 	pc->iot			= ga->ga_iot;
    211 	pc->ioh			= ga->ga_ioh;
    212 	pc->cookie		= sc;
    213 
    214 	printf(": %s\n", gio_product_string(sc->sc_gprid));
    215 
    216 #ifdef PCI_NETBSD_CONFIGURE
    217 	pc->pc_memext = extent_create("giopcimem", m_start, m_end,
    218 	    M_DEVBUF, NULL, 0, EX_NOWAIT);
    219 	pci_configure_bus(pc, NULL, pc->pc_memext, NULL, 0, mips_dcache_align);
    220 #endif
    221 
    222 	memset(&pba, 0, sizeof(pba));
    223 	pba.pba_memt	= SGIMIPS_BUS_SPACE_MEM;
    224 	pba.pba_dmat	= ga->ga_dmat;
    225 	pba.pba_pc	= pc;
    226 	pba.pba_flags	= PCI_FLAGS_MEM_ENABLED;
    227 	/* NB: do not set PCI_FLAGS_{MRL,MRM,MWI}_OKAY  -- true ?! */
    228 
    229 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    230 }
    231 
    232 static int
    233 giopci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    234 {
    235 
    236 	return (busno == 0);
    237 }
    238 
    239 static pcireg_t
    240 giopci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    241 {
    242 	struct giopci_softc *sc = pc->cookie;
    243 	int bus, dev, func;
    244 	pcireg_t data;
    245 
    246 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    247 	if (bus != 0 || dev != 0 || func != 0)
    248 		return (0);
    249 
    250 	/* XXX - should just use bus_space_peek */
    251 	if (reg >= sc->sc_pci_len) {
    252 		DPRINTF(("giopci_conf_read: reg 0x%x out of bounds\n", reg));
    253 		return (0);
    254 	}
    255 
    256 	DPRINTF(("giopci_conf_read: reg 0x%x = 0x", reg));
    257 	data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    258 	DPRINTF(("%08x\n", data));
    259 
    260 	return (data);
    261 }
    262 
    263 static void
    264 giopci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    265 {
    266 	struct giopci_softc *sc = pc->cookie;
    267 	int bus, dev, func;
    268 
    269 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    270 	if (bus != 0 || dev != 0 || func != 0)
    271 		return;
    272 
    273 	/* XXX - should just use bus_space_poke */
    274 	if (reg >= sc->sc_pci_len) {
    275 		DPRINTF(("giopci_conf_write: reg 0x%x out of bounds "
    276 		    "(val = 0x%08x)\n", reg, data));
    277 		return;
    278 	}
    279 
    280 	DPRINTF(("giopci_conf_write: reg 0x%x = 0x%08x\n", reg, data));
    281 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, data);
    282 }
    283 
    284 static int
    285 giopci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function,
    286     pcireg_t id)
    287 {
    288 
    289 	/* All devices use memory accesses only. */
    290 	return (PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_BM);
    291 }
    292 
    293 static int
    294 giopci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    295 {
    296 	struct giopci_softc *sc = pa->pa_pc->cookie;
    297 
    298 	*ihp = sc->sc_slot;
    299 
    300 	return (0);
    301 }
    302 
    303 static const char *
    304 giopci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    305 {
    306 	static char str[10];
    307 
    308 	snprintf(str, sizeof(str), "slot %s",
    309 	    (ih == GIO_SLOT_EXP0) ? "EXP0" :
    310 	    (ih == GIO_SLOT_EXP1) ? "EXP1" : "GFX");
    311 	return (str);
    312 }
    313 
    314 static void *
    315 giopci_intr_establish(int slot, int level, int (*func)(void *), void *arg)
    316 {
    317 
    318 	return (gio_intr_establish(slot, level, func, arg));
    319 }
    320 
    321 static void
    322 giopci_intr_disestablish(void *cookie)
    323 {
    324 
    325 	panic("giopci_intr_disestablish: impossible.");
    326 }
    327