Home | History | Annotate | Line # | Download | only in pci
mcpcia.c revision 1.17
      1 /* $NetBSD: mcpcia.c,v 1.17 2003/06/15 23:08:55 fvdl Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1998 by Matthew Jacob
     42  * NASA AMES Research Center.
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice immediately at the beginning of the file, without modification,
     50  *    this list of conditions, and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     61  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  */
     69 
     70 /*
     71  * MCPCIA mcbus to PCI bus adapter
     72  * found on AlphaServer 4100 systems.
     73  */
     74 
     75 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     76 
     77 __KERNEL_RCSID(0, "$NetBSD: mcpcia.c,v 1.17 2003/06/15 23:08:55 fvdl Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/device.h>
     82 #include <sys/malloc.h>
     83 
     84 #include <machine/autoconf.h>
     85 #include <machine/rpb.h>
     86 #include <machine/sysarch.h>
     87 
     88 #include <alpha/mcbus/mcbusreg.h>
     89 #include <alpha/mcbus/mcbusvar.h>
     90 #include <alpha/pci/mcpciareg.h>
     91 #include <alpha/pci/mcpciavar.h>
     92 #include <alpha/pci/pci_kn300.h>
     93 
     94 #define KV(_addr)	((caddr_t)ALPHA_PHYS_TO_K0SEG((_addr)))
     95 #define	MCPCIA_SYSBASE(mc)	\
     96 	((((unsigned long) (mc)->cc_gid) << MCBUS_GID_SHIFT) | \
     97 	 (((unsigned long) (mc)->cc_mid) << MCBUS_MID_SHIFT) | \
     98 	 (MCBUS_IOSPACE))
     99 
    100 #define	MCPCIA_PROBE(mid, gid)	\
    101 	badaddr((void *)KV(((((unsigned long) gid) << MCBUS_GID_SHIFT) | \
    102 	 (((unsigned long) mid) << MCBUS_MID_SHIFT) | \
    103 	 (MCBUS_IOSPACE) | MCPCIA_PCI_BRIDGE | _MCPCIA_PCI_REV)), \
    104 	sizeof(u_int32_t))
    105 
    106 static int	mcpciamatch __P((struct device *, struct cfdata *, void *));
    107 static void	mcpciaattach __P((struct device *, struct device *, void *));
    108 CFATTACH_DECL(mcpcia, sizeof(struct mcpcia_softc),
    109     mcpciamatch, mcpciaattach, NULL, NULL);
    110 
    111 static int	mcpciaprint __P((void *, const char *));
    112 
    113 void	mcpcia_init0 __P((struct mcpcia_config *, int));
    114 
    115 /*
    116  * We have one statically-allocated mcpcia_config structure; this is
    117  * the one used for the console (which, coincidentally, is the only
    118  * MCPCIA with an EISA adapter attached to it).
    119  */
    120 struct mcpcia_config mcpcia_console_configuration;
    121 
    122 int	mcpcia_bus_get_window __P((int, int,
    123 	    struct alpha_bus_space_translation *abst));
    124 
    125 static int
    126 mcpciaprint(aux, pnp)
    127 	void *aux;
    128 	const char *pnp;
    129 {
    130 	register struct pcibus_attach_args *pba = aux;
    131 	/* only PCIs can attach to MCPCIA for now */
    132 	if (pnp)
    133 		aprint_normal("%s at %s", pba->pba_busname, pnp);
    134 	aprint_normal(" bus %d", pba->pba_bus);
    135 	return (UNCONF);
    136 }
    137 
    138 static int
    139 mcpciamatch(parent, cf, aux)
    140 	struct device *parent;
    141 	struct cfdata *cf;
    142 	void *aux;
    143 {
    144 	struct mcbus_dev_attach_args *ma = aux;
    145 	if (ma->ma_type == MCBUS_TYPE_PCI)
    146 		return (1);
    147 	return (0);
    148 }
    149 
    150 static void
    151 mcpciaattach(parent, self, aux)
    152 	struct device *parent;
    153 	struct device *self;
    154 	void *aux;
    155 {
    156 	static int first = 1;
    157 	struct mcbus_dev_attach_args *ma = aux;
    158 	struct mcpcia_softc *mcp = (struct mcpcia_softc *)self;
    159 	struct mcpcia_config *ccp;
    160 	struct pcibus_attach_args pba;
    161 	u_int32_t ctl;
    162 
    163 	/*
    164 	 * Make sure this MCPCIA exists...
    165 	 */
    166 	if (MCPCIA_PROBE(ma->ma_mid, ma->ma_gid)) {
    167 		mcp->mcpcia_cc = NULL;
    168 		printf(" (not present)\n");
    169 		return;
    170 	}
    171 	printf("\n");
    172 
    173 	/*
    174 	 * Determine if we're the console's MCPCIA.
    175 	 */
    176 	if (ma->ma_mid == mcpcia_console_configuration.cc_mid &&
    177 	    ma->ma_gid == mcpcia_console_configuration.cc_gid)
    178 		ccp = &mcpcia_console_configuration;
    179 	else {
    180 		ccp = malloc(sizeof(struct mcpcia_config), M_DEVBUF, M_WAITOK);
    181 		memset(ccp, 0, sizeof(struct mcpcia_config));
    182 
    183 		ccp->cc_mid = ma->ma_mid;
    184 		ccp->cc_gid = ma->ma_gid;
    185 	}
    186 
    187 	mcp->mcpcia_cc = ccp;
    188 	ccp->cc_sc = mcp;
    189 
    190 	/* This initializes cc_sysbase so we can do register access. */
    191 	mcpcia_init0(ccp, 1);
    192 
    193 	ctl = REGVAL(MCPCIA_PCI_REV(ccp));
    194 	printf("%s: Horse Revision %d, %s Handed Saddle Revision %d,"
    195 	    " CAP Revision %d\n", mcp->mcpcia_dev.dv_xname, HORSE_REV(ctl),
    196 	    (SADDLE_TYPE(ctl) & 1)? "Right": "Left", SADDLE_REV(ctl),
    197 	    CAP_REV(ctl));
    198 
    199 	mcpcia_dma_init(ccp);
    200 
    201 	/*
    202 	 * Set up interrupts
    203 	 */
    204 	pci_kn300_pickintr(ccp, first);
    205 	first = 0;
    206 
    207 	/*
    208 	 * Attach PCI bus
    209 	 */
    210 	pba.pba_busname = "pci";
    211 	pba.pba_iot = &ccp->cc_iot;
    212 	pba.pba_memt = &ccp->cc_memt;
    213 	pba.pba_dmat =	/* start with direct, may change... */
    214 	    alphabus_dma_get_tag(&ccp->cc_dmat_direct, ALPHA_BUS_PCI);
    215 	pba.pba_dmat64 = NULL;
    216 	pba.pba_pc = &ccp->cc_pc;
    217 	pba.pba_bus = 0;
    218 	pba.pba_bridgetag = NULL;
    219 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
    220 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    221 	(void) config_found(self, &pba, mcpciaprint);
    222 
    223 	/*
    224 	 * Clear any errors that may have occurred during the probe
    225 	 * sequence.
    226 	 */
    227 	REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
    228 	alpha_mb();
    229 }
    230 
    231 void
    232 mcpcia_init()
    233 {
    234 	struct mcpcia_config *ccp = &mcpcia_console_configuration;
    235 	int i;
    236 
    237 	/*
    238 	 * Look for all of the MCPCIAs on the system.  One of them
    239 	 * will have an EISA attached to it.  This MCPCIA is the
    240 	 * only one that can be used for the console.  Once we find
    241 	 * that one, initialize it.
    242 	 */
    243 
    244 	for (i = 0; i < MCPCIA_PER_MCBUS; i++) {
    245 		ccp->cc_mid = mcbus_mcpcia_probe_order[i];
    246 		/*
    247 		 * XXX If we ever support more than one MCBUS, we'll
    248 		 * XXX have to probe for them, and map them to unit
    249 		 * XXX numbers.
    250 		 */
    251 		ccp->cc_gid = MCBUS_GID_FROM_INSTANCE(0);
    252 		ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
    253 
    254 		if (badaddr((void *)ALPHA_PHYS_TO_K0SEG(MCPCIA_PCI_REV(ccp)),
    255 		    sizeof(u_int32_t)))
    256 			continue;
    257 
    258 		if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
    259 			mcpcia_init0(ccp, 0);
    260 
    261 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 2;
    262 			alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 3;
    263 
    264 			alpha_bus_get_window = mcpcia_bus_get_window;
    265 			return;
    266 		}
    267 	}
    268 
    269 	panic("mcpcia_init: unable to find EISA bus");
    270 }
    271 
    272 void
    273 mcpcia_init0(ccp, mallocsafe)
    274 	struct mcpcia_config *ccp;
    275 	int mallocsafe;
    276 {
    277 	u_int32_t ctl;
    278 
    279 	if (ccp->cc_initted == 0) {
    280 		/* don't do these twice since they set up extents */
    281 		mcpcia_bus_io_init(&ccp->cc_iot, ccp);
    282 		mcpcia_bus_mem_init(&ccp->cc_memt, ccp);
    283 	}
    284 	ccp->cc_mallocsafe = mallocsafe;
    285 
    286 	mcpcia_pci_init(&ccp->cc_pc, ccp);
    287 
    288 	/*
    289 	 * Establish a precalculated base for convenience's sake.
    290 	 */
    291 	ccp->cc_sysbase = MCPCIA_SYSBASE(ccp);
    292 
    293 	/*
    294  	 * Disable interrupts and clear errors prior to probing
    295 	 */
    296 	REGVAL(MCPCIA_INT_MASK0(ccp)) = 0;
    297 	REGVAL(MCPCIA_INT_MASK1(ccp)) = 0;
    298 	REGVAL(MCPCIA_CAP_ERR(ccp)) = 0xFFFFFFFF;
    299 	alpha_mb();
    300 
    301 	if (ccp == &mcpcia_console_configuration) {
    302 		/*
    303 		 * Use this opportunity to also find out the MID and CPU
    304 		 * type of the currently running CPU (that's us, billybob....)
    305 		 */
    306 		ctl = REGVAL(MCPCIA_WHOAMI(ccp));
    307 		mcbus_primary.mcbus_cpu_mid = MCBUS_CPU_MID(ctl);
    308 		if ((MCBUS_CPU_INFO(ctl) & CPU_Fill_Err) == 0 &&
    309 		    mcbus_primary.mcbus_valid == 0) {
    310 			mcbus_primary.mcbus_bcache =
    311 			    MCBUS_CPU_INFO(ctl) & CPU_BCacheMask;
    312 			mcbus_primary.mcbus_valid = 1;
    313 		}
    314 		alpha_mb();
    315 	}
    316 
    317 	ccp->cc_initted = 1;
    318 }
    319 
    320 #ifdef TEST_PROBE_DEATH
    321 static void
    322 die_heathen_dog(arg)
    323 	void *arg;
    324 {
    325 	struct mcpcia_config *ccp = arg;
    326 
    327 	/* this causes a fatal machine check (0x670) */
    328 	REGVAL(MCPCIA_CAP_DIAG(ccp)) |= CAP_DIAG_MC_ADRPE;
    329 }
    330 #endif
    331 
    332 void
    333 mcpcia_config_cleanup()
    334 {
    335 	volatile u_int32_t ctl;
    336 	struct mcpcia_softc *mcp;
    337 	struct mcpcia_config *ccp;
    338 	int i;
    339 	extern struct cfdriver mcpcia_cd;
    340 
    341 	/*
    342 	 * Turn on Hard, Soft error interrupts. Maybe i2c too.
    343 	 */
    344 	for (i = 0; i < mcpcia_cd.cd_ndevs; i++) {
    345 		if ((mcp = mcpcia_cd.cd_devs[i]) == NULL)
    346 			continue;
    347 
    348 		ccp = mcp->mcpcia_cc;
    349 		if (ccp == NULL)
    350 			continue;
    351 
    352 		ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
    353 		ctl |= MCPCIA_GEN_IENABL;
    354 		REGVAL(MCPCIA_INT_MASK0(ccp)) = ctl;
    355 		alpha_mb();
    356 
    357 		/* force stall while write completes */
    358 		ctl = REGVAL(MCPCIA_INT_MASK0(ccp));
    359 	}
    360 #ifdef TEST_PROBE_DEATH
    361 	(void) timeout (die_heathen_dog, &mcpcia_console_configuration,
    362 	    30 * hz);
    363 #endif
    364 }
    365 
    366 int
    367 mcpcia_bus_get_window(type, window, abst)
    368 	int type, window;
    369 	struct alpha_bus_space_translation *abst;
    370 {
    371 	struct mcpcia_config *ccp = &mcpcia_console_configuration;
    372 	bus_space_tag_t st;
    373 
    374 	switch (type) {
    375 	case ALPHA_BUS_TYPE_PCI_IO:
    376 		st = &ccp->cc_iot;
    377 		break;
    378 
    379 	case ALPHA_BUS_TYPE_PCI_MEM:
    380 		st = &ccp->cc_memt;
    381 		break;
    382 
    383 	default:
    384 		panic("mcpcia_bus_get_window");
    385 	}
    386 
    387 	return (alpha_bus_space_get_window(st, window, abst));
    388 }
    389