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