Home | History | Annotate | Line # | Download | only in gio
pci_gio.c revision 1.15
      1  1.15   msaitoh /*	$NetBSD: pci_gio.c,v 1.15 2015/10/02 05:22:52 msaitoh 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.15   msaitoh __KERNEL_RCSID(0, "$NetBSD: pci_gio.c,v 1.15 2015/10/02 05:22:52 msaitoh 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.14  macallan static void pcimem_bus_mem_init(bus_space_tag_t, void *);
    106  1.14  macallan static struct mips_bus_space	pcimem_mbst;
    107  1.14  macallan bus_space_tag_t	gio_pci_memt = NULL;
    108  1.14  macallan 
    109   1.1    rumble static int
    110  1.11       chs giopci_match(device_t parent, cfdata_t match, void *aux)
    111   1.1    rumble {
    112   1.1    rumble 	struct gio_attach_args *ga = aux;
    113   1.1    rumble 	int gprid;
    114   1.1    rumble 
    115   1.1    rumble 	/*
    116   1.3    rumble 	 * I think that these cards are all GIO32-bis or GIO64. Thus
    117   1.3    rumble 	 * they work in either Indigo2/Challenge M or
    118   1.3    rumble 	 * Indy/Challenge S/Indigo R4k, according to form factor. However,
    119   1.1    rumble 	 * there are some exceptions (e.g. my Indigo R4k won't power
    120   1.1    rumble 	 * on with the Set Engineering card installed).
    121   1.1    rumble 	 */
    122   1.1    rumble 	if (mach_type != MACH_SGI_IP20 && mach_type != MACH_SGI_IP22)
    123   1.1    rumble 		return (0);
    124   1.1    rumble 
    125   1.1    rumble 	gprid = GIO_PRODUCT_PRODUCTID(ga->ga_product);
    126   1.1    rumble 	if (gprid == PHOBOS_G100 || gprid == PHOBOS_G130 ||
    127   1.1    rumble 	    gprid == PHOBOS_G160 || gprid == SETENG_GFE)
    128   1.1    rumble 		return (1);
    129   1.1    rumble 
    130   1.1    rumble 	return (0);
    131   1.1    rumble }
    132   1.1    rumble 
    133   1.1    rumble static void
    134  1.11       chs giopci_attach(device_t parent, device_t self, void *aux)
    135   1.1    rumble {
    136  1.11       chs 	struct giopci_softc *sc = device_private(self);
    137   1.1    rumble 	pci_chipset_tag_t pc = &sc->sc_pc;
    138   1.1    rumble 	struct gio_attach_args *ga = aux;
    139   1.1    rumble 	uint32_t pci_off, pci_len, arb;
    140   1.1    rumble 	struct pcibus_attach_args pba;
    141   1.1    rumble 	u_long m_start, m_end;
    142   1.1    rumble #ifdef PCI_NETBSD_CONFIGURE
    143   1.1    rumble 	extern int pci_conf_debug;
    144   1.1    rumble 
    145   1.1    rumble 	pci_conf_debug = giopci_debug;
    146   1.1    rumble #endif
    147   1.1    rumble 
    148   1.1    rumble 	sc->sc_iot	= ga->ga_iot;
    149   1.1    rumble 	sc->sc_slot	= ga->ga_slot;
    150   1.1    rumble 	sc->sc_gprid	= GIO_PRODUCT_PRODUCTID(ga->ga_product);
    151   1.1    rumble 
    152  1.14  macallan 	pcimem_bus_mem_init(&pcimem_mbst, NULL);
    153  1.14  macallan 	gio_pci_memt = &pcimem_mbst;
    154  1.14  macallan 
    155   1.1    rumble 	if (mach_type == MACH_SGI_IP22 &&
    156   1.1    rumble 	    mach_subtype == MACH_SGI_IP22_FULLHOUSE)
    157   1.1    rumble 		arb = GIO_ARB_RT | GIO_ARB_MST | GIO_ARB_PIPE;
    158   1.1    rumble 	else
    159   1.1    rumble 		arb = GIO_ARB_RT | GIO_ARB_MST;
    160   1.1    rumble 
    161   1.1    rumble 	if (gio_arb_config(ga->ga_slot, arb)) {
    162   1.1    rumble 		printf(": failed to configure GIO bus arbiter\n");
    163   1.1    rumble 		return;
    164   1.1    rumble 	}
    165   1.1    rumble 
    166   1.1    rumble #if (NIMC > 0)
    167   1.1    rumble 	imc_disable_sysad_parity();
    168   1.1    rumble #endif
    169   1.1    rumble 
    170   1.1    rumble 	switch (sc->sc_gprid) {
    171   1.1    rumble 	case PHOBOS_G100:
    172   1.1    rumble 	case PHOBOS_G130:
    173   1.1    rumble 	case PHOBOS_G160:
    174   1.1    rumble 		pci_off = PHOBOS_PCI_OFFSET;
    175   1.1    rumble 		pci_len = PHOBOS_PCI_LENGTH;
    176   1.1    rumble 		m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_START);
    177   1.1    rumble 		m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + PHOBOS_TULIP_END);
    178   1.1    rumble 		break;
    179   1.1    rumble 
    180   1.1    rumble 	case SETENG_GFE:
    181   1.4    rumble 		/*
    182   1.4    rumble 		 * NB: The SetEng board does not allow the ThunderLAN's DMA
    183   1.4    rumble 		 *     engine to properly transfer segments that span page
    184   1.4    rumble 		 *     boundaries. See sgimips/autoconf.c where we catch a
    185   1.4    rumble 		 *     tl(4) device attachment and create an appropriate
    186   1.4    rumble 		 *     proplib entry to enable the workaround.
    187   1.4    rumble 		 */
    188   1.1    rumble 		pci_off = SETENG_PCI_OFFSET;
    189   1.1    rumble 		pci_len = SETENG_PCI_LENGTH;
    190   1.1    rumble 		m_start = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_START);
    191   1.1    rumble 		m_end = MIPS_KSEG1_TO_PHYS(ga->ga_addr + SETENG_TLAN_END);
    192   1.1    rumble 		bus_space_write_4(ga->ga_iot, ga->ga_ioh,
    193   1.1    rumble 		    SETENG_MAGIC_OFFSET, SETENG_MAGIC_VALUE);
    194   1.1    rumble 		break;
    195   1.1    rumble 
    196   1.1    rumble 	default:
    197   1.1    rumble 		panic("giopci_attach: unsupported GIO product id 0x%02x",
    198   1.1    rumble 		    sc->sc_gprid);
    199   1.1    rumble 	}
    200   1.1    rumble 
    201   1.1    rumble 	if (bus_space_subregion(ga->ga_iot, ga->ga_ioh, pci_off, pci_len,
    202   1.1    rumble 	    &sc->sc_ioh)) {
    203  1.11       chs 		printf("%s: unable to map PCI registers\n", device_xname(self));
    204   1.1    rumble 		return;
    205   1.1    rumble 	}
    206   1.2    rumble 	sc->sc_pci_len = pci_len;
    207   1.1    rumble 
    208   1.1    rumble 	pc->pc_bus_maxdevs	= giopci_bus_maxdevs;
    209   1.1    rumble 	pc->pc_conf_read	= giopci_conf_read;
    210   1.1    rumble 	pc->pc_conf_write	= giopci_conf_write;
    211   1.1    rumble 	pc->pc_conf_hook	= giopci_conf_hook;
    212   1.1    rumble 	pc->pc_intr_map		= giopci_intr_map;
    213   1.1    rumble 	pc->pc_intr_string	= giopci_intr_string;
    214   1.1    rumble 	pc->intr_establish	= giopci_intr_establish;
    215   1.1    rumble 	pc->intr_disestablish	= giopci_intr_disestablish;
    216   1.1    rumble 	pc->iot			= ga->ga_iot;
    217   1.1    rumble 	pc->ioh			= ga->ga_ioh;
    218   1.1    rumble 	pc->cookie		= sc;
    219   1.1    rumble 
    220   1.1    rumble 	printf(": %s\n", gio_product_string(sc->sc_gprid));
    221   1.1    rumble 
    222   1.1    rumble #ifdef PCI_NETBSD_CONFIGURE
    223   1.1    rumble 	pc->pc_memext = extent_create("giopcimem", m_start, m_end,
    224  1.10      para 	    NULL, 0, EX_NOWAIT);
    225   1.6      matt 	pci_configure_bus(pc, NULL, pc->pc_memext, NULL, 0,
    226   1.6      matt 	    mips_cache_info.mci_dcache_align);
    227   1.1    rumble #endif
    228   1.1    rumble 
    229   1.1    rumble 	memset(&pba, 0, sizeof(pba));
    230  1.14  macallan 	pba.pba_memt	= gio_pci_memt;
    231   1.1    rumble 	pba.pba_dmat	= ga->ga_dmat;
    232   1.1    rumble 	pba.pba_pc	= pc;
    233   1.8    dyoung 	pba.pba_flags	= PCI_FLAGS_MEM_OKAY;
    234   1.1    rumble 	/* NB: do not set PCI_FLAGS_{MRL,MRM,MWI}_OKAY  -- true ?! */
    235   1.1    rumble 
    236   1.1    rumble 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    237   1.1    rumble }
    238   1.1    rumble 
    239   1.1    rumble static int
    240   1.1    rumble giopci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    241   1.1    rumble {
    242   1.1    rumble 
    243   1.1    rumble 	return (busno == 0);
    244   1.1    rumble }
    245   1.1    rumble 
    246   1.1    rumble static pcireg_t
    247   1.1    rumble giopci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    248   1.1    rumble {
    249   1.1    rumble 	struct giopci_softc *sc = pc->cookie;
    250   1.1    rumble 	int bus, dev, func;
    251   1.1    rumble 	pcireg_t data;
    252   1.1    rumble 
    253  1.15   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    254  1.15   msaitoh 		return (pcireg_t) -1;
    255  1.15   msaitoh 
    256   1.1    rumble 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    257   1.1    rumble 	if (bus != 0 || dev != 0 || func != 0)
    258   1.1    rumble 		return (0);
    259   1.1    rumble 
    260   1.2    rumble 	/* XXX - should just use bus_space_peek */
    261   1.2    rumble 	if (reg >= sc->sc_pci_len) {
    262   1.2    rumble 		DPRINTF(("giopci_conf_read: reg 0x%x out of bounds\n", reg));
    263   1.2    rumble 		return (0);
    264   1.2    rumble 	}
    265   1.2    rumble 
    266   1.1    rumble 	DPRINTF(("giopci_conf_read: reg 0x%x = 0x", reg));
    267   1.1    rumble 	data = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
    268   1.1    rumble 	DPRINTF(("%08x\n", data));
    269   1.1    rumble 
    270   1.1    rumble 	return (data);
    271   1.1    rumble }
    272   1.1    rumble 
    273   1.1    rumble static void
    274   1.1    rumble giopci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    275   1.1    rumble {
    276   1.1    rumble 	struct giopci_softc *sc = pc->cookie;
    277   1.1    rumble 	int bus, dev, func;
    278   1.1    rumble 
    279  1.15   msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    280  1.15   msaitoh 		return;
    281  1.15   msaitoh 
    282   1.1    rumble 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    283   1.1    rumble 	if (bus != 0 || dev != 0 || func != 0)
    284   1.1    rumble 		return;
    285   1.1    rumble 
    286   1.2    rumble 	/* XXX - should just use bus_space_poke */
    287   1.2    rumble 	if (reg >= sc->sc_pci_len) {
    288   1.2    rumble 		DPRINTF(("giopci_conf_write: reg 0x%x out of bounds "
    289   1.2    rumble 		    "(val = 0x%08x)\n", reg, data));
    290   1.2    rumble 		return;
    291   1.2    rumble 	}
    292   1.2    rumble 
    293   1.1    rumble 	DPRINTF(("giopci_conf_write: reg 0x%x = 0x%08x\n", reg, data));
    294   1.1    rumble 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, data);
    295   1.1    rumble }
    296   1.1    rumble 
    297   1.1    rumble static int
    298   1.1    rumble giopci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function,
    299   1.1    rumble     pcireg_t id)
    300   1.1    rumble {
    301   1.1    rumble 
    302   1.1    rumble 	/* All devices use memory accesses only. */
    303   1.1    rumble 	return (PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_BM);
    304   1.1    rumble }
    305   1.1    rumble 
    306   1.1    rumble static int
    307   1.7    dyoung giopci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    308   1.1    rumble {
    309   1.1    rumble 	struct giopci_softc *sc = pa->pa_pc->cookie;
    310   1.1    rumble 
    311   1.1    rumble 	*ihp = sc->sc_slot;
    312   1.1    rumble 
    313   1.1    rumble 	return (0);
    314   1.1    rumble }
    315   1.1    rumble 
    316   1.1    rumble static const char *
    317  1.12  christos giopci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char * buf,
    318  1.13     ozaki     size_t len)
    319   1.1    rumble {
    320  1.12  christos 	snprintf(buf, len, "slot %s", (ih == GIO_SLOT_EXP0) ? "EXP0" :
    321   1.1    rumble 	    (ih == GIO_SLOT_EXP1) ? "EXP1" : "GFX");
    322  1.12  christos 	return buf;
    323   1.1    rumble }
    324   1.1    rumble 
    325   1.1    rumble static void *
    326   1.1    rumble giopci_intr_establish(int slot, int level, int (*func)(void *), void *arg)
    327   1.1    rumble {
    328   1.1    rumble 
    329   1.1    rumble 	return (gio_intr_establish(slot, level, func, arg));
    330   1.1    rumble }
    331   1.1    rumble 
    332   1.1    rumble static void
    333   1.1    rumble giopci_intr_disestablish(void *cookie)
    334   1.1    rumble {
    335   1.1    rumble 
    336   1.1    rumble 	panic("giopci_intr_disestablish: impossible.");
    337   1.1    rumble }
    338  1.14  macallan 
    339  1.14  macallan #define CHIP	   		pcimem
    340  1.14  macallan #define	CHIP_MEM		/* defined */
    341  1.14  macallan #define CHIP_WRONG_ENDIAN
    342  1.14  macallan 
    343  1.14  macallan #define	CHIP_W1_BUS_START(v)	0x00000000UL
    344  1.14  macallan #define CHIP_W1_BUS_END(v)	0xffffffffUL
    345  1.14  macallan #define	CHIP_W1_SYS_START(v)	0x00000000UL
    346  1.14  macallan #define	CHIP_W1_SYS_END(v)	0xffffffffUL
    347  1.14  macallan 
    348  1.14  macallan #include <mips/mips/bus_space_alignstride_chipdep.c>
    349