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