Home | History | Annotate | Line # | Download | only in gio
gio.c revision 1.37
      1 /*	$NetBSD: gio.c,v 1.37 2021/04/24 23:36:48 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 Soren S. Jorvang
      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. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *          This product includes software developed for the
     18  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19  *          information about NetBSD.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: gio.c,v 1.37 2021/04/24 23:36:48 thorpej Exp $");
     37 
     38 #include "opt_ddb.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 
     44 #include <sys/bus.h>
     45 #include <machine/machtype.h>
     46 #include <machine/sysconf.h>
     47 
     48 #include <sgimips/gio/gioreg.h>
     49 #include <sgimips/gio/giovar.h>
     50 #include <sgimips/gio/giodevs_data.h>
     51 
     52 #include "locators.h"
     53 #include "newport.h"
     54 #include "grtwo.h"
     55 #include "light.h"
     56 #include "imc.h"
     57 #include "pic.h"
     58 
     59 #if (NNEWPORT > 0)
     60 #include <sgimips/gio/newportvar.h>
     61 #endif
     62 
     63 #if (NGRTWO > 0)
     64 #include <sgimips/gio/grtwovar.h>
     65 #endif
     66 
     67 #if (NLIGHT > 0)
     68 #include <sgimips/gio/lightvar.h>
     69 #endif
     70 
     71 #if (NIMC > 0)
     72 extern int imc_gio64_arb_config(int, uint32_t);
     73 #endif
     74 
     75 #if (NPIC > 0)
     76 extern int pic_gio32_arb_config(int, uint32_t);
     77 #endif
     78 
     79 
     80 static int	gio_match(device_t, cfdata_t, void *);
     81 static void	gio_attach(device_t, device_t, void *);
     82 static int	gio_print(void *, const char *);
     83 static int	gio_search(device_t, cfdata_t, const int *, void *);
     84 static int	gio_submatch(device_t, cfdata_t, const int *, void *);
     85 
     86 CFATTACH_DECL_NEW(gio, 0,
     87     gio_match, gio_attach, NULL, NULL);
     88 
     89 struct gio_probe {
     90 	uint32_t slot;
     91 	uint32_t base;
     92 	uint32_t mach_type;
     93 	uint32_t mach_subtype;
     94 };
     95 
     96 /*
     97  * Expansion Slot Base Addresses
     98  *
     99  * IP12, IP20 and IP24 have two GIO connectors: GIO_SLOT_EXP0 and
    100  * GIO_SLOT_EXP1.
    101  *
    102  * On IP24 these slots exist on the graphics board or the IOPLUS
    103  * "mezzanine" on Indy and Challenge S, respectively. The IOPLUS or
    104  * graphics board connects to the mainboard via a single GIO64 connector.
    105  *
    106  * IP22 has either three or four physical connectors, but only two
    107  * electrically distinct slots: GIO_SLOT_GFX and GIO_SLOT_EXP0.
    108  *
    109  * It should also be noted that DMA is (mostly) not supported in Challenge
    110  * S's GIO_SLOT_EXP1. See gio(4) for the story.
    111  */
    112 static const struct gio_probe slot_bases[] = {
    113 	{ GIO_SLOT_GFX,  0x1f000000, MACH_SGI_IP22, MACH_SGI_IP22_FULLHOUSE },
    114 
    115 	{ GIO_SLOT_EXP0, 0x1f400000, MACH_SGI_IP12, -1 },
    116 	{ GIO_SLOT_EXP0, 0x1f400000, MACH_SGI_IP20, -1 },
    117 	{ GIO_SLOT_EXP0, 0x1f400000, MACH_SGI_IP22, -1 },
    118 
    119 	{ GIO_SLOT_EXP1, 0x1f600000, MACH_SGI_IP12, -1 },
    120 	{ GIO_SLOT_EXP1, 0x1f600000, MACH_SGI_IP20, -1 },
    121 	{ GIO_SLOT_EXP1, 0x1f600000, MACH_SGI_IP22, MACH_SGI_IP22_GUINNESS },
    122 
    123 	{ 0, 0, 0, 0 }
    124 };
    125 
    126 /*
    127  * Graphic Board Base Addresses
    128  *
    129  * Graphics boards are not treated like expansion slot cards. Their base
    130  * addresses do not necessarily correspond to GIO slot addresses and they
    131  * do not contain product identification words.
    132  */
    133 static const struct gio_probe gfx_bases[] = {
    134 	/* grtwo, and newport on IP22 */
    135 	{ -1, 0x1f000000, MACH_SGI_IP12, -1 },
    136 	{ -1, 0x1f000000, MACH_SGI_IP20, -1 },
    137 	{ -1, 0x1f000000, MACH_SGI_IP22, -1 },
    138 
    139 	/* light */
    140 	{ -1, 0x1f3f0000, MACH_SGI_IP12, -1 },
    141 	{ -1, 0x1f3f0000, MACH_SGI_IP20, -1 },
    142 
    143 	/* light (dual headed) */
    144 	{ -1, 0x1f3f8000, MACH_SGI_IP12, -1 },
    145 	{ -1, 0x1f3f8000, MACH_SGI_IP20, -1 },
    146 
    147 	/* grtwo, and newport on IP22 */
    148 	{ -1, 0x1f400000, MACH_SGI_IP12, -1 },
    149 	{ -1, 0x1f400000, MACH_SGI_IP20, -1 },
    150 	{ -1, 0x1f400000, MACH_SGI_IP22, -1 },
    151 
    152 	/* grtwo */
    153 	{ -1, 0x1f600000, MACH_SGI_IP12, -1 },
    154 	{ -1, 0x1f600000, MACH_SGI_IP20, -1 },
    155 	{ -1, 0x1f600000, MACH_SGI_IP22, -1 },
    156 
    157 	/* newport */
    158 	{ -1, 0x1f800000, MACH_SGI_IP22, MACH_SGI_IP22_FULLHOUSE },
    159 
    160 	/* newport */
    161 	{ -1, 0x1fc00000, MACH_SGI_IP22, MACH_SGI_IP22_FULLHOUSE },
    162 
    163 	{ 0, 0, 0, 0 }
    164 };
    165 
    166 /* maximum number of graphics boards possible (arbitrarily large estimate) */
    167 #define MAXGFX 8
    168 
    169 static int
    170 gio_match(device_t parent, cfdata_t match, void *aux)
    171 {
    172 	if (mach_type == MACH_SGI_IP12 || mach_type == MACH_SGI_IP20 ||
    173 	    mach_type == MACH_SGI_IP22)
    174 		return 1;
    175 
    176 	return 0;
    177 }
    178 
    179 static void
    180 gio_attach(device_t parent, device_t self, void *aux)
    181 {
    182 	struct gio_attach_args ga;
    183 	uint32_t gfx[MAXGFX];
    184 	int i, j, ngfx;
    185 
    186 	printf("\n");
    187 
    188 	ngfx = 0;
    189 	memset(gfx, 0, sizeof(gfx));
    190 
    191 	/*
    192 	 * Attach graphics devices first. They do not contain a Product
    193 	 * Identification Word and have no slot number.
    194 	 *
    195 	 * Record addresses to which graphics devices attach so that
    196 	 * we do not confuse them with expansion slots, should the
    197 	 * addresses coincide.
    198 	 */
    199 	for (i = 0; gfx_bases[i].base != 0; i++) {
    200 		/* skip slots that don't apply to us */
    201 		if (gfx_bases[i].mach_type != mach_type)
    202 			continue;
    203 
    204 		if (gfx_bases[i].mach_subtype != -1 &&
    205 		    gfx_bases[i].mach_subtype != mach_subtype)
    206 			continue;
    207 
    208 		ga.ga_slot = -1;
    209 		ga.ga_addr = gfx_bases[i].base;
    210 		/* XXX */
    211 		if (platform.badaddr((void *)MIPS_PHYS_TO_KSEG1(ga.ga_addr),
    212 		    sizeof(uint32_t)))
    213 			continue;
    214 		ga.ga_iot = normal_memt;
    215 		if (bus_space_map(normal_memt, ga.ga_addr, 0,
    216 		    BUS_SPACE_MAP_LINEAR, &ga.ga_ioh) != 0)
    217 		    	continue;
    218 		ga.ga_dmat = &sgimips_default_bus_dma_tag;
    219 		ga.ga_product = -1;
    220 
    221 
    222 		if (config_found(self, &ga, gio_print,
    223 				 CFARG_SUBMATCH, gio_submatch,
    224 				 CFARG_EOL) != NULL) {
    225 			if (ngfx == MAXGFX)
    226 				panic("gio_attach: MAXGFX");
    227 			gfx[ngfx++] = gfx_bases[i].base;
    228 		}
    229 	}
    230 
    231 	/*
    232 	 * Now attach any GIO expansion cards.
    233 	 *
    234 	 * Be sure to skip any addresses to which a graphics device has
    235 	 * already been attached.
    236 	 */
    237 	for (i = 0; slot_bases[i].base != 0; i++) {
    238 		bool skip = false;
    239 
    240 		/* skip slots that don't apply to us */
    241 		if (slot_bases[i].mach_type != mach_type)
    242 			continue;
    243 
    244 		if (slot_bases[i].mach_subtype != -1 &&
    245 		    slot_bases[i].mach_subtype != mach_subtype)
    246 			continue;
    247 
    248 		for (j = 0; j < ngfx; j++) {
    249 			if (slot_bases[i].base == gfx[j]) {
    250 				skip = true;
    251 				break;
    252 			}
    253 		}
    254 		if (skip)
    255 			continue;
    256 
    257 		ga.ga_slot = slot_bases[i].slot;
    258 		ga.ga_addr = slot_bases[i].base;
    259 		/* XXX */
    260 		if (platform.badaddr((void *)MIPS_PHYS_TO_KSEG1(ga.ga_addr),
    261 		    sizeof(uint32_t)))
    262 			continue;
    263 		ga.ga_iot = normal_memt;
    264 		if (bus_space_map(normal_memt, ga.ga_addr, 0,
    265 		    BUS_SPACE_MAP_LINEAR, &ga.ga_ioh) != 0)
    266 		    	continue;
    267 		ga.ga_dmat = &sgimips_default_bus_dma_tag;
    268 
    269 		ga.ga_product = bus_space_read_4(ga.ga_iot, ga.ga_ioh, 0);
    270 
    271 		config_found(self, &ga, gio_print,
    272 		    CFARG_SUBMATCH, gio_submatch,
    273 		    CFARG_EOL);
    274 	}
    275 
    276 	config_search(self, &ga,
    277 	    CFARG_SEARCH, gio_search,
    278 	    CFARG_EOL);
    279 }
    280 
    281 static int
    282 gio_print(void *aux, const char *pnp)
    283 {
    284 	struct gio_attach_args *ga = aux;
    285 	int i = 0;
    286 
    287 	/* gfx probe */
    288 	if (ga->ga_product == -1)
    289 		return (QUIET);
    290 
    291 	if (pnp != NULL) {
    292 	  	int product, revision;
    293 
    294 		product = GIO_PRODUCT_PRODUCTID(ga->ga_product);
    295 
    296 		if (GIO_PRODUCT_32BIT_ID(ga->ga_product))
    297 			revision = GIO_PRODUCT_REVISION(ga->ga_product);
    298 		else
    299 			revision = 0;
    300 
    301 		while (gio_knowndevs[i].productid != 0) {
    302 			if (gio_knowndevs[i].productid == product) {
    303 				aprint_normal("%s", gio_knowndevs[i].product);
    304 				break;
    305 			}
    306 			i++;
    307 		}
    308 
    309 		if (gio_knowndevs[i].productid == 0)
    310 			aprint_normal("unknown GIO card");
    311 
    312 		aprint_normal(" (product 0x%02x revision 0x%02x) at %s",
    313 		    product, revision, pnp);
    314 	}
    315 
    316 	if (ga->ga_slot != GIOCF_SLOT_DEFAULT)
    317 		aprint_normal(" slot %d", ga->ga_slot);
    318 	if (ga->ga_addr != (uint32_t) GIOCF_ADDR_DEFAULT)
    319 		aprint_normal(" addr 0x%x", ga->ga_addr);
    320 
    321 	return UNCONF;
    322 }
    323 
    324 static int
    325 gio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    326 {
    327 	struct gio_attach_args *ga = aux;
    328 
    329 	do {
    330 		/* Handled by direct configuration, so skip here */
    331 		if (cf->cf_loc[GIOCF_ADDR] == GIOCF_ADDR_DEFAULT)
    332 			return 0;
    333 
    334 		ga->ga_slot = cf->cf_loc[GIOCF_SLOT];
    335 		ga->ga_addr = cf->cf_loc[GIOCF_ADDR];
    336 		ga->ga_iot = normal_memt;
    337 		ga->ga_ioh = MIPS_PHYS_TO_KSEG1(ga->ga_addr);
    338 
    339 		if (config_probe(parent, cf, ga))
    340 			config_attach(parent, cf, ga, gio_print, CFARG_EOL);
    341 	} while (cf->cf_fstate == FSTATE_STAR);
    342 
    343 	return 0;
    344 }
    345 
    346 static int
    347 gio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    348 {
    349 	struct gio_attach_args *ga = aux;
    350 
    351 	if (cf->cf_loc[GIOCF_SLOT] != GIOCF_SLOT_DEFAULT &&
    352 	    cf->cf_loc[GIOCF_SLOT] != ga->ga_slot)
    353 		return 0;
    354 
    355 	if (cf->cf_loc[GIOCF_ADDR] != GIOCF_ADDR_DEFAULT &&
    356 	    cf->cf_loc[GIOCF_ADDR] != ga->ga_addr)
    357 		return 0;
    358 
    359 	return config_match(parent, cf, aux);
    360 }
    361 
    362 int
    363 gio_cnattach(void)
    364 {
    365 	struct gio_attach_args ga;
    366 	int i;
    367 
    368 	for (i = 0; gfx_bases[i].base != 0; i++) {
    369 		/* skip bases that don't apply to us */
    370 		if (gfx_bases[i].mach_type != mach_type)
    371 			continue;
    372 
    373 		if (gfx_bases[i].mach_subtype != -1 &&
    374 		    gfx_bases[i].mach_subtype != mach_subtype)
    375 			continue;
    376 
    377 		ga.ga_slot = -1;
    378 		ga.ga_addr = gfx_bases[i].base;
    379 		/* XXX */
    380 		if (platform.badaddr((void *)MIPS_PHYS_TO_KSEG1(ga.ga_addr),
    381 		    sizeof(uint32_t)))
    382 			continue;
    383 		ga.ga_iot = normal_memt;
    384 		if (bus_space_map(normal_memt, ga.ga_addr, 0,
    385 		    BUS_SPACE_MAP_LINEAR, &ga.ga_ioh) != 0)
    386 		    	continue;
    387 		ga.ga_dmat = &sgimips_default_bus_dma_tag;
    388 		ga.ga_product = -1;
    389 
    390 #if (NGRTWO > 0)
    391 		if (grtwo_cnattach(&ga) == 0)
    392 			return 0;
    393 #endif
    394 
    395 #if (NLIGHT > 0)
    396 		if (light_cnattach(&ga) == 0)
    397 			return 0;
    398 #endif
    399 
    400 #if (NNEWPORT > 0)
    401 		if (newport_cnattach(&ga) == 0)
    402 			return 0;
    403 #endif
    404 
    405 	}
    406 
    407 	return ENXIO;
    408 }
    409 
    410 /*
    411  * Devices living in the expansion slots must enable or disable some
    412  * GIO arbiter settings. This is accomplished via imc(4) or pic(4)
    413  * registers, depending on the machine in question.
    414  */
    415 int
    416 gio_arb_config(int slot, uint32_t flags)
    417 {
    418 
    419 	if (flags == 0)
    420 		return (EINVAL);
    421 
    422 	if (flags & ~(GIO_ARB_RT | GIO_ARB_LB | GIO_ARB_MST | GIO_ARB_SLV |
    423 	    GIO_ARB_PIPE | GIO_ARB_NOPIPE | GIO_ARB_32BIT | GIO_ARB_64BIT |
    424 	    GIO_ARB_HPC2_32BIT | GIO_ARB_HPC2_64BIT))
    425 		return (EINVAL);
    426 
    427 	if (((flags & GIO_ARB_RT)   && (flags & GIO_ARB_LB))  ||
    428 	    ((flags & GIO_ARB_MST)  && (flags & GIO_ARB_SLV)) ||
    429 	    ((flags & GIO_ARB_PIPE) && (flags & GIO_ARB_NOPIPE)) ||
    430 	    ((flags & GIO_ARB_32BIT) && (flags & GIO_ARB_64BIT)) ||
    431 	    ((flags & GIO_ARB_HPC2_32BIT) && (flags & GIO_ARB_HPC2_64BIT)))
    432 		return (EINVAL);
    433 
    434 #if (NPIC > 0)
    435 	if (mach_type == MACH_SGI_IP12)
    436 		return (pic_gio32_arb_config(slot, flags));
    437 #endif
    438 
    439 #if (NIMC > 0)
    440 	if (mach_type == MACH_SGI_IP20 || mach_type == MACH_SGI_IP22)
    441 		return (imc_gio64_arb_config(slot, flags));
    442 #endif
    443 
    444 	return (EINVAL);
    445 }
    446 
    447 /*
    448  * Establish an interrupt handler for the specified slot.
    449  *
    450  * Indy and Challenge S have an interrupt per GIO slot. Indigo and Indigo2
    451  * share a single interrupt, however.
    452  */
    453 void *
    454 gio_intr_establish(int slot, int level, int (*func)(void *), void *arg)
    455 {
    456 	int intr;
    457 
    458 	switch (mach_type) {
    459 	case MACH_SGI_IP12:
    460 	case MACH_SGI_IP20:
    461 		if (slot == GIO_SLOT_GFX)
    462 			panic("gio_intr_establish: slot %d", slot);
    463 		intr = 6;
    464 		break;
    465 
    466 	case MACH_SGI_IP22:
    467 		if (mach_subtype == MACH_SGI_IP22_FULLHOUSE) {
    468 			if (slot == GIO_SLOT_EXP1)
    469 				panic("gio_intr_establish: slot %d", slot);
    470 			intr = 6;
    471 		} else {
    472 			if (slot == GIO_SLOT_GFX)
    473 				panic("gio_intr_establish: slot %d", slot);
    474 			intr = (slot == GIO_SLOT_EXP0) ? 22 : 23;
    475 		}
    476 		break;
    477 
    478 	default:
    479 		panic("gio_intr_establish: mach_type");
    480 	}
    481 
    482 	return (cpu_intr_establish(intr, level, func, arg));
    483 }
    484 
    485 const char *
    486 gio_product_string(int prid)
    487 {
    488 	int i;
    489 
    490 	for (i = 0; gio_knowndevs[i].product != NULL; i++)
    491 		if (gio_knowndevs[i].productid == prid)
    492 			return (gio_knowndevs[i].product);
    493 
    494 	return (NULL);
    495 }
    496