Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.50
      1 /*	$NetBSD: nubus.c,v 1.50 2000/07/31 22:39:17 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Allen Briggs.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Allen Briggs.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/buf.h>
     36 #include <sys/conf.h>
     37 
     38 #include <uvm/uvm_extern.h>
     39 
     40 #include <machine/autoconf.h>
     41 #include <machine/bus.h>
     42 #include <machine/vmparam.h>
     43 #include <machine/param.h>
     44 #include <machine/cpu.h>
     45 #include <machine/pte.h>
     46 #include <machine/viareg.h>
     47 
     48 #include <mac68k/nubus/nubus.h>
     49 
     50 #ifdef DEBUG
     51 #define NDB_PROBE	0x1
     52 #define NDB_FOLLOW	0x2
     53 #define NDB_ARITH	0x4
     54 static int	nubus_debug = 0 /* | NDB_PROBE */;
     55 #endif
     56 
     57 static int	nubus_print __P((void *, const char *));
     58 static int	nubus_match __P((struct device *, struct cfdata *, void *));
     59 static void	nubus_attach __P((struct device *, struct device *, void *));
     60 static int	nubus_video_resource __P((int));
     61 
     62 static int	nubus_probe_slot __P((bus_space_tag_t, bus_space_handle_t,
     63 		    int, nubus_slot *));
     64 static u_int32_t nubus_calc_CRC __P((bus_space_tag_t, bus_space_handle_t,
     65 		    nubus_slot *));
     66 
     67 static u_long	nubus_adjust_ptr __P((u_int8_t, u_long, long));
     68 static u_int8_t	nubus_read_1 __P((bus_space_tag_t, bus_space_handle_t,
     69 		    u_int8_t, u_long));
     70 #ifdef notyet
     71 static u_int16_t nubus_read_2 __P((bus_space_tag_t, bus_space_handle_t,
     72 		    u_int8_t, u_long));
     73 #endif
     74 static u_int32_t nubus_read_4 __P((bus_space_tag_t, bus_space_handle_t,
     75 		    u_int8_t, u_long));
     76 
     77 struct cfattach nubus_ca = {
     78 	sizeof(struct nubus_softc), nubus_match, nubus_attach
     79 };
     80 
     81 static int
     82 nubus_match(parent, cf, aux)
     83 	struct device *parent;
     84 	struct cfdata *cf;
     85 	void *aux;
     86 {
     87 	static int nubus_matched = 0;
     88 
     89 	/* Allow only one instance. */
     90 	if (nubus_matched)
     91 		return (0);
     92 
     93 	nubus_matched = 1;
     94 	return (1);
     95 }
     96 
     97 static void
     98 nubus_attach(parent, self, aux)
     99 	struct device *parent, *self;
    100 	void *aux;
    101 {
    102 	struct nubus_attach_args na_args;
    103 	bus_space_tag_t bst;
    104 	bus_space_handle_t bsh;
    105 	nubus_slot fmtblock;
    106 	nubus_dir dir;
    107 	nubus_dirent dirent;
    108 	nubus_type slottype;
    109 	u_long entry;
    110 	int i, rsrcid;
    111 	u_int8_t lanes;
    112 
    113 	printf("\n");
    114 
    115 	for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    116 		na_args.slot = i;
    117 		na_args.na_tag = bst = MAC68K_BUS_SPACE_MEM;
    118 
    119 		if (bus_space_map(bst,
    120 		    NUBUS_SLOT2PA(na_args.slot), NBMEMSIZE, 0, &bsh)) {
    121 #ifdef DEBUG
    122 			if (nubus_debug & NDB_PROBE)
    123 				printf("%s: failed to map slot %x, "
    124 				    "address %p (in use?)\n",
    125 				    self->dv_xname, i,
    126 				    (void *)NUBUS_SLOT2PA(i));
    127 #endif
    128 			continue;
    129 		}
    130 
    131 		if (nubus_probe_slot(bst, bsh, i, &fmtblock) <= 0) {
    132 notfound:
    133 			bus_space_unmap(bst, bsh, NBMEMSIZE);
    134 			continue;
    135 		}
    136 
    137 		rsrcid = 0x80;
    138 		lanes = fmtblock.bytelanes;
    139 
    140 		nubus_get_main_dir(&fmtblock, &dir);
    141 
    142 		/*
    143 		 * Get the resource for the first function on the card.
    144 		 * This is assumed to be at resource ID 0x80.  If we can
    145 		 * not find this entry (as we can not on some video cards),
    146 		 * check to see if we can get a different ID from the list
    147 		 * of video resources given to us by the booter.  If that
    148 		 * doesn't work either, take the first resource following
    149 		 * the board resource.
    150 		 */
    151 		if (nubus_find_rsrc(bst, bsh,
    152 		    &fmtblock, &dir, rsrcid, &dirent) <= 0) {
    153 			if ((rsrcid = nubus_video_resource(i)) == -1) {
    154 				/*
    155 				 * Since nubus_find_rsrc failed, the directory
    156 				 * is back at its base.
    157 				 */
    158 				entry = dir.curr_ent;
    159 
    160 				/*
    161 				 * All nubus cards should have a board
    162 				 * resource, but be sure that's what it
    163 				 * is before we skip it.
    164 				 */
    165 				rsrcid = nubus_read_1(bst, bsh,
    166 				    lanes, entry);
    167 				if (rsrcid == 0x1)
    168 					entry = nubus_adjust_ptr(lanes,
    169 					    dir.curr_ent, 4);
    170 
    171 				rsrcid = nubus_read_1(bst, bsh, lanes, entry);
    172 #ifdef DEBUG
    173 				if (nubus_debug & NDB_FOLLOW)
    174 					printf("\tUsing rsrc 0x%x.\n", rsrcid);
    175 #endif
    176 				if (rsrcid == 0xff)	/* end of chain */
    177 					goto notfound;
    178 			}
    179 			/*
    180 			 * Try to find the resource passed by the booter
    181 			 * or the one we just tracked down.
    182 			 */
    183 			if (nubus_find_rsrc(bst, bsh,
    184 			    &fmtblock, &dir, rsrcid, &dirent) <= 0)
    185 				goto notfound;
    186 		}
    187 
    188 		nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
    189 
    190 		if (nubus_find_rsrc(bst, bsh,
    191 		    &fmtblock, &dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
    192 			goto notfound;
    193 
    194 		if (nubus_get_ind_data(bst, bsh, &fmtblock, &dirent,
    195 		    (caddr_t)&slottype, sizeof(nubus_type)) <= 0)
    196 			goto notfound;
    197 
    198 		/*
    199 		 * If this is a display card, try to pull out the correct
    200 		 * display mode as passed by the booter.
    201 		 */
    202 		if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
    203 			int r;
    204 
    205 			if ((r = nubus_video_resource(i)) != -1) {
    206 
    207 				nubus_get_main_dir(&fmtblock, &dir);
    208 
    209 				if (nubus_find_rsrc(bst, bsh,
    210 				    &fmtblock, &dir, r, &dirent) <= 0)
    211 					goto notfound;
    212 
    213 				nubus_get_dir_from_rsrc(&fmtblock,
    214 				    &dirent, &dir);
    215 
    216 				if (nubus_find_rsrc(bst, bsh, &fmtblock, &dir,
    217 				    NUBUS_RSRC_TYPE, &dirent) <= 0)
    218 					goto notfound;
    219 
    220 				if (nubus_get_ind_data(bst, bsh,
    221 				    &fmtblock, &dirent, (caddr_t)&slottype,
    222 				    sizeof(nubus_type)) <= 0)
    223 					goto notfound;
    224 
    225 				rsrcid = r;
    226 			}
    227 		}
    228 
    229 		na_args.slot = i;
    230 		na_args.rsrcid = rsrcid;
    231 		na_args.category = slottype.category;
    232 		na_args.type = slottype.type;
    233 		na_args.drsw = slottype.drsw;
    234 		na_args.drhw = slottype.drhw;
    235 		na_args.fmt = &fmtblock;
    236 
    237 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    238 
    239 		config_found(self, &na_args, nubus_print);
    240 	}
    241 
    242 	enable_nubus_intr();
    243 }
    244 
    245 static int
    246 nubus_print(aux, pnp)
    247 	void *aux;
    248 	const char *pnp;
    249 {
    250 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    251 	bus_space_tag_t bst = na->na_tag;
    252 	bus_space_handle_t bsh;
    253 
    254 	if (pnp) {
    255 		printf("%s slot %x", pnp, na->slot);
    256 		if (bus_space_map(bst,
    257 		    NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh) == 0) {
    258 			printf(": %s", nubus_get_card_name(bst, bsh, na->fmt));
    259 			printf(" (Vendor: %s,", nubus_get_vendor(bst, bsh,
    260 			    na->fmt, NUBUS_RSRC_VEND_ID));
    261 			printf(" Part: %s)", nubus_get_vendor(bst, bsh,
    262 			    na->fmt, NUBUS_RSRC_VEND_PART));
    263 			bus_space_unmap(bst, bsh, NBMEMSIZE);
    264 		}
    265 #ifdef DIAGNOSTIC
    266 		else
    267 			printf(":");
    268 		printf(" Type: %04x %04x %04x %04x",
    269 		    na->category, na->type, na->drsw, na->drhw);
    270 #endif
    271 	} else {
    272 		printf(" slot %x", na->slot);
    273 	}
    274 	return (UNCONF);
    275 }
    276 
    277 static int
    278 nubus_video_resource(slot)
    279 	int slot;
    280 {
    281 	extern u_int16_t mac68k_vrsrc_vec[];
    282 	int i;
    283 
    284 	for (i = 0 ; i < 6 ; i++)
    285 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
    286 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    287 	return (-1);
    288 }
    289 
    290 /*
    291  * Probe a given nubus slot.  If a card is there and we can get the
    292  * format block from it's clutching decl. ROMs, fill the format block
    293  * and return non-zero.  If we can't find a card there with a valid
    294  * decl. ROM, return 0.
    295  *
    296  * First, we check to see if we can access the memory at the tail
    297  * end of the slot.  If so, then we check for a bytelanes byte.  We
    298  * could probably just return a failure status if we bus error on
    299  * the first try, but there really is little reason not to go ahead
    300  * and check the other three locations in case there's a wierd card
    301  * out there.
    302  *
    303  * Checking for a card involves locating the "bytelanes" byte which
    304  * tells us how to interpret the declaration ROM's data.  The format
    305  * block is at the top of the card's standard memory space and the
    306  * bytelanes byte is at the end of that block.
    307  *
    308  * After some inspection of the bytelanes byte, it appears that it
    309  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    310  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    311  * and (less obviously), Y will have the upper N bits clear if it is
    312  * found N bytes from the last possible location.  Both that and
    313  * the exclusive-or check are made.
    314  *
    315  * If a valid
    316  */
    317 static u_int8_t	nbits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    318 static int
    319 nubus_probe_slot(bst, bsh, slot, fmt)
    320 	bus_space_tag_t bst;
    321 	bus_space_handle_t bsh;
    322 	int slot;
    323 	nubus_slot *fmt;
    324 {
    325 	u_long ofs, hdr;
    326 	int i, j, found, hdr_size;
    327 	u_int8_t lanes;
    328 
    329 #ifdef DEBUG
    330 	if (nubus_debug & NDB_PROBE)
    331 		printf("probing slot %x\n", slot);
    332 #endif
    333 
    334 	/*
    335 	 * The idea behind this glorious work of art is to probe for only
    336 	 * valid bytelanes values at appropriate locations (see DC&D p. 159
    337 	 * for a list).  Note the pattern:  the first 8 values are at offset
    338 	 * 0xffffff in the slot's space; the next 4 values at 0xfffffe; the
    339 	 * next 2 values at 0xfffffd; and the last one at 0xfffffc.
    340 	 *
    341 	 * The nested loops implement an efficient search of this space,
    342 	 * probing first for a valid address, then checking for each of the
    343 	 * valid bytelanes values at that address.
    344 	 */
    345 	ofs = NBMEMSIZE;
    346 	lanes = 0xf;
    347 
    348 	for (j = 8, found = 0; j > 0 && !found; j >>= 1) {
    349 		ofs--;
    350 		for (i = j; i > 0; i--, lanes--) {
    351 			if (!mac68k_bus_space_probe(bst, bsh, ofs, 1)) {
    352 				lanes -= i;
    353 				break;
    354 			}
    355 			if (bus_space_read_1(bst, bsh, ofs) ==
    356 			    (((~lanes & 0xf) << 4) | lanes)) {
    357 				found = 1;
    358 				break;
    359 			}
    360 		}
    361 	}
    362 
    363 	if (!found) {
    364 #ifdef DEBUG
    365 		if (nubus_debug & NDB_PROBE)
    366 			printf("bytelanes not found for slot %x\n", slot);
    367 #endif
    368 		return 0;
    369 	}
    370 
    371 	fmt->bytelanes = lanes;
    372 	fmt->step = nbits[(lanes & 0x0f)];
    373 	fmt->slot = slot;	/* XXX redundant; get rid of this someday */
    374 
    375 #ifdef DEBUG
    376 	if (nubus_debug & NDB_PROBE)
    377 		printf("bytelanes of 0x%x found for slot 0x%x.\n",
    378 		    fmt->bytelanes, slot);
    379 #endif
    380 
    381 	/*
    382 	 * Go ahead and attempt to load format header.
    383 	 * First, we need to find the first byte beyond memory that
    384 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    385 	 * to work.
    386 	 */
    387 	hdr = NBMEMSIZE;
    388 	hdr_size = 20;
    389 
    390 	i = 0x10 | (lanes & 0x0f);
    391 	while ((i & 1) == 0) {
    392 		hdr++;
    393 		i >>= 1;
    394 	}
    395 	fmt->top = hdr;
    396 	hdr = nubus_adjust_ptr(lanes, hdr, -hdr_size);
    397 #ifdef DEBUG
    398 	if (nubus_debug & NDB_PROBE)
    399 		printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
    400 		    fmt->top, hdr_size, hdr);
    401 	if (nubus_debug & NDB_ARITH)
    402 		for (i = 1 ; i < 8 ; i++)
    403 			printf("0x%lx - 0x%x = 0x%lx, + 0x%x = 0x%lx.\n",
    404 			    hdr, i, nubus_adjust_ptr(lanes, hdr, -i),
    405 			    i, nubus_adjust_ptr(lanes, hdr,  i));
    406 #endif
    407 
    408 	fmt->directory_offset =
    409 	    0xff000000 | nubus_read_4(bst, bsh, lanes, hdr);
    410 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    411 	fmt->length = nubus_read_4(bst, bsh, lanes, hdr);
    412 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    413 	fmt->crc = nubus_read_4(bst, bsh, lanes, hdr);
    414 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    415 	fmt->revision_level = nubus_read_1(bst, bsh, lanes, hdr);
    416 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    417 	fmt->format = nubus_read_1(bst, bsh, lanes, hdr);
    418 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    419 	fmt->test_pattern = nubus_read_4(bst, bsh, lanes, hdr);
    420 
    421 #ifdef DEBUG
    422 	if (nubus_debug & NDB_PROBE) {
    423 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    424 		printf("Length 0x%x\t", fmt->length);
    425 		printf("CRC 0x%x\n", fmt->crc);
    426 		printf("Revision level 0x%x\t", fmt->revision_level);
    427 		printf("Format 0x%x\t", fmt->format);
    428 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    429 	}
    430 #endif
    431 
    432 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    433 		printf("Invalid looking directory offset (0x%x)!\n",
    434 		    fmt->directory_offset);
    435 		return 0;
    436 	}
    437 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    438 		printf("Nubus--test pattern invalid:\n");
    439 		printf("       slot 0x%x, bytelanes 0x%x?\n", fmt->slot, lanes);
    440 		printf("       read test 0x%x, compare with 0x%x.\n",
    441 		    fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    442 		return 0;
    443 	}
    444 
    445 	/* Perform CRC */
    446 	if (fmt->crc != nubus_calc_CRC(bst, bsh, fmt)) {
    447 		printf("Nubus--crc check failed, slot 0x%x.\n", fmt->slot);
    448 		return 0;
    449 	}
    450 
    451 	return 1;
    452 }
    453 
    454 static u_int32_t
    455 nubus_calc_CRC(bst, bsh, fmt)
    456 	bus_space_tag_t bst;
    457 	bus_space_handle_t bsh;
    458 	nubus_slot	*fmt;
    459 {
    460 #if 0
    461 	u_long base, ptr, crc_loc;
    462 	u_int32_t sum;
    463 	u_int8_t lanes = fmt->bytelanes;
    464 
    465 	base = fmt->top;
    466 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    467 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    468 
    469 	sum = 0;
    470 	while (ptr < base)
    471 		roll #1, sum
    472 		if (ptr == crc_loc) {
    473 			roll #3, sum
    474 			ptr = nubus_adjust_ptr(lanes, ptr, 3);
    475 		} else {
    476 			sum += nubus_read_1(bst, bsh, lanes, ptr);
    477 		}
    478 		ptr = nubus_adjust_ptr(lanes, ptr, 1);
    479 	}
    480 
    481 	return sum;
    482 #endif
    483 	return fmt->crc;
    484 }
    485 
    486 /*
    487  * Compute byte offset on card, taking into account bytelanes.
    488  * Base must be on a valid bytelane for this function to work.
    489  * Return the new address.
    490  *
    491  * XXX -- There has GOT to be a better way to do this.
    492  */
    493 static u_long
    494 nubus_adjust_ptr(lanes, base, amt)
    495 	u_int8_t lanes;
    496 	u_long base;
    497 	long amt;
    498 {
    499 	u_int8_t b, t;
    500 
    501 	if (!amt)
    502 		return base;
    503 
    504 	if (amt < 0) {
    505 		amt = -amt;
    506 		b = lanes;
    507 		t = (b << 4);
    508 		b <<= (3 - (base & 0x3));
    509 		while (amt) {
    510 			b <<= 1;
    511 			if (b == t)
    512 				b = lanes;
    513 			if (b & 0x08)
    514 				amt--;
    515 			base--;
    516 		}
    517 		return base;
    518 	}
    519 
    520 	t = (lanes & 0xf) | 0x10;
    521 	b = t >> (base & 0x3);
    522 	while (amt) {
    523 		b >>= 1;
    524 		if (b == 1)
    525 			b = t;
    526 		if (b & 1)
    527 			amt--;
    528 		base++;
    529 	}
    530 
    531 	return base;
    532 }
    533 
    534 static u_int8_t
    535 nubus_read_1(bst, bsh, lanes, ofs)
    536 	bus_space_tag_t bst;
    537 	bus_space_handle_t bsh;
    538 	u_int8_t lanes;
    539 	u_long ofs;
    540 {
    541 	return bus_space_read_1(bst, bsh, ofs);
    542 }
    543 
    544 #ifdef notyet
    545 /* Nothing uses this, yet */
    546 static u_int16_t
    547 nubus_read_2(bst, bsh, lanes, ofs)
    548 	bus_space_tag_t bst;
    549 	bus_space_handle_t bsh;
    550 	u_int8_t lanes;
    551 	u_long ofs;
    552 {
    553 	u_int16_t s;
    554 
    555 	s = (nubus_read_1(bst, bsh, lanes, ofs) << 8);
    556 	ofs = nubus_adjust_ptr(lanes, ofs, 1);
    557 	s |= nubus_read_1(bst, bsh, lanes, ofs);
    558 	return s;
    559 }
    560 #endif
    561 
    562 static u_int32_t
    563 nubus_read_4(bst, bsh, lanes, ofs)
    564 	bus_space_tag_t bst;
    565 	bus_space_handle_t bsh;
    566 	u_int8_t lanes;
    567 	u_long ofs;
    568 {
    569 	u_int32_t l;
    570 	int i;
    571 
    572 	l = 0;
    573 	for (i = 0; i < 4; i++) {
    574 		l = (l << 8) | nubus_read_1(bst, bsh, lanes, ofs);
    575 		ofs = nubus_adjust_ptr(lanes, ofs, 1);
    576 	}
    577 	return l;
    578 }
    579 
    580 void
    581 nubus_get_main_dir(fmt, dir_return)
    582 	nubus_slot *fmt;
    583 	nubus_dir *dir_return;
    584 {
    585 #ifdef DEBUG
    586 	if (nubus_debug & NDB_FOLLOW)
    587 		printf("nubus_get_main_dir(%p, %p)\n",
    588 		    fmt, dir_return);
    589 #endif
    590 	dir_return->dirbase = nubus_adjust_ptr(fmt->bytelanes, fmt->top,
    591 	    fmt->directory_offset - 20);
    592 	dir_return->curr_ent = dir_return->dirbase;
    593 }
    594 
    595 void
    596 nubus_get_dir_from_rsrc(fmt, dirent, dir_return)
    597 	nubus_slot *fmt;
    598 	nubus_dirent *dirent;
    599 	nubus_dir *dir_return;
    600 {
    601 	u_long loc;
    602 
    603 #ifdef DEBUG
    604 	if (nubus_debug & NDB_FOLLOW)
    605 		printf("nubus_get_dir_from_rsrc(%p, %p, %p).\n",
    606 		    fmt, dirent, dir_return);
    607 #endif
    608 	if ((loc = dirent->offset) & 0x800000) {
    609 		loc |= 0xff000000;
    610 	}
    611 	dir_return->dirbase =
    612 	    nubus_adjust_ptr(fmt->bytelanes, dirent->myloc, loc);
    613 	dir_return->curr_ent = dir_return->dirbase;
    614 }
    615 
    616 int
    617 nubus_find_rsrc(bst, bsh, fmt, dir, rsrcid, dirent_return)
    618 	bus_space_tag_t bst;
    619 	bus_space_handle_t bsh;
    620 	nubus_slot *fmt;
    621 	nubus_dir *dir;
    622 	u_int8_t rsrcid;
    623 	nubus_dirent *dirent_return;
    624 {
    625 	u_long entry;
    626 	u_int8_t byte, lanes = fmt->bytelanes;
    627 
    628 #ifdef DEBUG
    629 	if (nubus_debug & NDB_FOLLOW)
    630 		printf("nubus_find_rsrc(%p, %p, 0x%x, %p)\n",
    631 		    fmt, dir, rsrcid, dirent_return);
    632 #endif
    633 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN)
    634 		return -1;
    635 
    636 	entry = dir->curr_ent;
    637 	do {
    638 		byte = nubus_read_1(bst, bsh, lanes, entry);
    639 #ifdef DEBUG
    640 		if (nubus_debug & NDB_FOLLOW)
    641 			printf("\tFound rsrc 0x%x.\n", byte);
    642 #endif
    643 		if (byte == rsrcid) {
    644 			dirent_return->myloc = entry;
    645 			dirent_return->rsrc_id = rsrcid;
    646 			entry = nubus_read_4(bst, bsh, lanes, entry);
    647 			dirent_return->offset = (entry & 0x00ffffff);
    648 			return 1;
    649 		}
    650 		if (byte == 0xff) {
    651 			entry = dir->dirbase;
    652 		} else {
    653 			entry = nubus_adjust_ptr(lanes, entry, 4);
    654 		}
    655 	} while (entry != (u_long)dir->curr_ent);
    656 	return 0;
    657 }
    658 
    659 int
    660 nubus_get_ind_data(bst, bsh, fmt, dirent, data_return, nbytes)
    661 	bus_space_tag_t bst;
    662 	bus_space_handle_t bsh;
    663 	nubus_slot *fmt;
    664 	nubus_dirent *dirent;
    665 	caddr_t data_return;
    666 	int nbytes;
    667 {
    668 	u_long loc;
    669 	u_int8_t lanes = fmt->bytelanes;
    670 
    671 #ifdef DEBUG
    672 	if (nubus_debug & NDB_FOLLOW)
    673 		printf("nubus_get_ind_data(%p, %p, %p, %d).\n",
    674 		    fmt, dirent, data_return, nbytes);
    675 #endif
    676 	if ((loc = dirent->offset) & 0x800000) {
    677 		loc |= 0xff000000;
    678 	}
    679 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    680 
    681 	while (nbytes--) {
    682 		*data_return++ = nubus_read_1(bst, bsh, lanes, loc);
    683 		loc = nubus_adjust_ptr(lanes, loc, 1);
    684 	}
    685 	return 1;
    686 }
    687 
    688 int
    689 nubus_get_c_string(bst, bsh, fmt, dirent, data_return, max_bytes)
    690 	bus_space_tag_t bst;
    691 	bus_space_handle_t bsh;
    692 	nubus_slot *fmt;
    693 	nubus_dirent *dirent;
    694 	caddr_t data_return;
    695 	int max_bytes;
    696 {
    697 	u_long loc;
    698 	u_int8_t lanes = fmt->bytelanes;
    699 
    700 #ifdef DEBUG
    701 	if (nubus_debug & NDB_FOLLOW)
    702 		printf("nubus_get_c_string(%p, %p, %p, %d).\n",
    703 		    fmt, dirent, data_return, max_bytes);
    704 #endif
    705 	if ((loc = dirent->offset) & 0x800000)
    706 		loc |= 0xff000000;
    707 
    708 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    709 
    710 	*data_return = '\0';
    711 	while (max_bytes--) {
    712 		if ((*data_return++ =
    713 		    nubus_read_1(bst, bsh, lanes, loc)) == 0)
    714 			return 1;
    715 		loc = nubus_adjust_ptr(lanes, loc, 1);
    716 	}
    717 	*(data_return-1) = '\0';
    718 	return 0;
    719 }
    720 
    721 static char	*huh = "???";
    722 
    723 char *
    724 nubus_get_vendor(bst, bsh, fmt, rsrc)
    725 	bus_space_tag_t bst;
    726 	bus_space_handle_t bsh;
    727 	nubus_slot *fmt;
    728 	int rsrc;
    729 {
    730 	static char str_ret[64];
    731 	nubus_dir dir;
    732 	nubus_dirent ent;
    733 
    734 #ifdef DEBUG
    735 	if (nubus_debug & NDB_FOLLOW)
    736 		printf("nubus_get_vendor(%p, 0x%x).\n", fmt, rsrc);
    737 #endif
    738 	nubus_get_main_dir(fmt, &dir);
    739 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    740 		return huh;
    741 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    742 
    743 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_VENDORINFO, &ent)
    744 	    <= 0)
    745 		return huh;
    746 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    747 
    748 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, rsrc, &ent) <= 0)
    749 		return huh;
    750 
    751 	nubus_get_c_string(bst, bsh, fmt, &ent, str_ret, 64);
    752 
    753 	return str_ret;
    754 }
    755 
    756 char *
    757 nubus_get_card_name(bst, bsh, fmt)
    758 	bus_space_tag_t bst;
    759 	bus_space_handle_t bsh;
    760 	nubus_slot *fmt;
    761 {
    762 	static char name_ret[64];
    763 	nubus_dir dir;
    764 	nubus_dirent ent;
    765 
    766 #ifdef DEBUG
    767 	if (nubus_debug & NDB_FOLLOW)
    768 		printf("nubus_get_card_name(%p).\n", fmt);
    769 #endif
    770 	nubus_get_main_dir(fmt, &dir);
    771 
    772 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    773 		return huh;
    774 
    775 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    776 
    777 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    778 		return huh;
    779 
    780 	nubus_get_c_string(bst, bsh, fmt, &ent, name_ret, 64);
    781 
    782 	return name_ret;
    783 }
    784 
    785 #ifdef DEBUG
    786 void
    787 nubus_scan_slot(bst, slotno)
    788 	bus_space_tag_t bst;
    789 	int slotno;
    790 {
    791 	int i=0, state=0;
    792 	char twirl[] = "-\\|/";
    793 	bus_space_handle_t sc_bsh;
    794 
    795 	if (bus_space_map(bst, NUBUS_SLOT2PA(slotno), NBMEMSIZE, 0, &sc_bsh)) {
    796 		printf("nubus_scan_slot: failed to map slot %d\n", slotno);
    797 		return;
    798 	}
    799 
    800 	printf("Scanning slot %c for accessible regions:\n",
    801 		slotno == 9 ? '9' : slotno - 10 + 'A');
    802 	for (i=0 ; i<NBMEMSIZE; i++) {
    803 		if (mac68k_bus_space_probe(bst, sc_bsh, i, 1)) {
    804 			if (state == 0) {
    805 				printf("\t0x%x-", i);
    806 				state = 1;
    807 			}
    808 		} else {
    809 			if (state) {
    810 				printf("0x%x\n", i);
    811 				state = 0;
    812 			}
    813 		}
    814 		if (i%100 == 0) {
    815 			printf("%c\b", twirl[(i/100)%4]);
    816 		}
    817 	}
    818 	if (state) {
    819 		printf("0x%x\n", i);
    820 	}
    821 	return;
    822 }
    823 #endif
    824