Home | History | Annotate | Line # | Download | only in nubus
nubus.c revision 1.51.6.2
      1 /*	$NetBSD: nubus.c,v 1.51.6.2 2002/04/17 00:03:39 nathanw 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/malloc.h>
     35 #include <sys/device.h>
     36 #include <sys/buf.h>
     37 #include <sys/conf.h>
     38 
     39 #include <uvm/uvm_extern.h>
     40 
     41 #include <machine/autoconf.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 | NDB_FOLLOW | NDB_ARITH */ ;
     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 	struct mainbus_attach_args *mba;
    104 	bus_space_tag_t bst;
    105 	bus_space_handle_t bsh;
    106 	nubus_slot fmtblock;
    107 	nubus_dir dir;
    108 	nubus_dirent dirent;
    109 	nubus_type slottype;
    110 	u_long entry;
    111 	int i, rsrcid;
    112 	u_int8_t lanes;
    113 
    114 	mba = aux;
    115 	KASSERT(NULL != mba->mba_dmat);
    116 
    117 	printf("\n");
    118 
    119 	for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
    120 		na_args.slot = i;
    121 		na_args.na_tag = bst = mba->mba_bst;
    122 		na_args.na_dmat = mba->mba_dmat;
    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 		 * If we only find a board resource, report that.
    156 		 * There are cards that do not have anything else; their
    157 		 * driver then has to match on the board resource and
    158 		 * the card name.
    159 		 */
    160 		if (nubus_find_rsrc(bst, bsh,
    161 		    &fmtblock, &dir, rsrcid, &dirent) <= 0) {
    162 			if ((rsrcid = nubus_video_resource(i)) == -1) {
    163 				int has_board_rsrc = 0;
    164 
    165 				/*
    166 				 * Since nubus_find_rsrc failed, the directory
    167 				 * is back at its base.
    168 				 */
    169 				entry = dir.curr_ent;
    170 
    171 				/*
    172 				 * All nubus cards should have a board
    173 				 * resource, but be sure that's what it
    174 				 * is before we skip it, and note the fact.
    175 				 */
    176 				rsrcid = nubus_read_1(bst, bsh,
    177 				    lanes, entry);
    178 				if (rsrcid == 0x1) {
    179 					has_board_rsrc = 1;
    180 					entry = nubus_adjust_ptr(lanes,
    181 					    dir.curr_ent, 4);
    182 				}
    183 				rsrcid = nubus_read_1(bst, bsh, lanes, entry);
    184 				/* end of chain? */
    185 				if (rsrcid == 0xff) {
    186 					if (!has_board_rsrc)
    187 						goto notfound;
    188 					else
    189 						rsrcid = 0x01;
    190 				}
    191 #ifdef DEBUG
    192 				if (nubus_debug & NDB_FOLLOW)
    193 					printf("\tUsing rsrc 0x%x.\n", rsrcid);
    194 #endif
    195 			}
    196 			/*
    197 			 * Try to find the resource passed by the booter
    198 			 * or the one we just tracked down.
    199 			 */
    200 			if (nubus_find_rsrc(bst, bsh,
    201 			    &fmtblock, &dir, rsrcid, &dirent) <= 0)
    202 				goto notfound;
    203 		}
    204 
    205 		nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
    206 
    207 		if (nubus_find_rsrc(bst, bsh,
    208 		    &fmtblock, &dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
    209 			goto notfound;
    210 
    211 		if (nubus_get_ind_data(bst, bsh, &fmtblock, &dirent,
    212 		    (caddr_t)&slottype, sizeof(nubus_type)) <= 0)
    213 			goto notfound;
    214 
    215 		/*
    216 		 * If this is a display card, try to pull out the correct
    217 		 * display mode as passed by the booter.
    218 		 */
    219 		if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
    220 			int r;
    221 
    222 			if ((r = nubus_video_resource(i)) != -1) {
    223 
    224 				nubus_get_main_dir(&fmtblock, &dir);
    225 
    226 				if (nubus_find_rsrc(bst, bsh,
    227 				    &fmtblock, &dir, r, &dirent) <= 0)
    228 					goto notfound;
    229 
    230 				nubus_get_dir_from_rsrc(&fmtblock,
    231 				    &dirent, &dir);
    232 
    233 				if (nubus_find_rsrc(bst, bsh, &fmtblock, &dir,
    234 				    NUBUS_RSRC_TYPE, &dirent) <= 0)
    235 					goto notfound;
    236 
    237 				if (nubus_get_ind_data(bst, bsh,
    238 				    &fmtblock, &dirent, (caddr_t)&slottype,
    239 				    sizeof(nubus_type)) <= 0)
    240 					goto notfound;
    241 
    242 				rsrcid = r;
    243 			}
    244 		}
    245 
    246 		na_args.slot = i;
    247 		na_args.rsrcid = rsrcid;
    248 		na_args.category = slottype.category;
    249 		na_args.type = slottype.type;
    250 		na_args.drsw = slottype.drsw;
    251 		na_args.drhw = slottype.drhw;
    252 		na_args.fmt = &fmtblock;
    253 
    254 		bus_space_unmap(bst, bsh, NBMEMSIZE);
    255 
    256 		config_found(self, &na_args, nubus_print);
    257 	}
    258 
    259 	enable_nubus_intr();
    260 }
    261 
    262 static int
    263 nubus_print(aux, pnp)
    264 	void *aux;
    265 	const char *pnp;
    266 {
    267 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    268 	bus_space_tag_t bst = na->na_tag;
    269 	bus_space_handle_t bsh;
    270 
    271 	if (pnp) {
    272 		printf("%s slot %x", pnp, na->slot);
    273 		if (bus_space_map(bst,
    274 		    NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &bsh) == 0) {
    275 			printf(": %s", nubus_get_card_name(bst, bsh, na->fmt));
    276 			printf(" (Vendor: %s,", nubus_get_vendor(bst, bsh,
    277 			    na->fmt, NUBUS_RSRC_VEND_ID));
    278 			printf(" Part: %s)", nubus_get_vendor(bst, bsh,
    279 			    na->fmt, NUBUS_RSRC_VEND_PART));
    280 			bus_space_unmap(bst, bsh, NBMEMSIZE);
    281 		}
    282 #ifdef DIAGNOSTIC
    283 		else
    284 			printf(":");
    285 		printf(" Type: %04x %04x %04x %04x",
    286 		    na->category, na->type, na->drsw, na->drhw);
    287 #endif
    288 	} else {
    289 		printf(" slot %x", na->slot);
    290 	}
    291 	return (UNCONF);
    292 }
    293 
    294 static int
    295 nubus_video_resource(slot)
    296 	int slot;
    297 {
    298 	extern u_int16_t mac68k_vrsrc_vec[];
    299 	int i;
    300 
    301 	for (i = 0 ; i < 6 ; i++)
    302 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
    303 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    304 	return (-1);
    305 }
    306 
    307 /*
    308  * Probe a given nubus slot.  If a card is there and we can get the
    309  * format block from it's clutching decl. ROMs, fill the format block
    310  * and return non-zero.  If we can't find a card there with a valid
    311  * decl. ROM, return 0.
    312  *
    313  * First, we check to see if we can access the memory at the tail
    314  * end of the slot.  If so, then we check for a bytelanes byte.  We
    315  * could probably just return a failure status if we bus error on
    316  * the first try, but there really is little reason not to go ahead
    317  * and check the other three locations in case there's a weird card
    318  * out there.
    319  *
    320  * Checking for a card involves locating the "bytelanes" byte which
    321  * tells us how to interpret the declaration ROM's data.  The format
    322  * block is at the top of the card's standard memory space and the
    323  * bytelanes byte is at the end of that block.
    324  *
    325  * After some inspection of the bytelanes byte, it appears that it
    326  * takes the form 0xXY where Y is a bitmask of the bytelanes in use
    327  * and X is a bitmask of the lanes to ignore.  Hence, (X ^ Y) == 0
    328  * and (less obviously), Y will have the upper N bits clear if it is
    329  * found N bytes from the last possible location.  Both that and
    330  * the exclusive-or check are made.
    331  *
    332  * If a valid
    333  */
    334 static u_int8_t	nbits[] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    335 static int
    336 nubus_probe_slot(bst, bsh, slot, fmt)
    337 	bus_space_tag_t bst;
    338 	bus_space_handle_t bsh;
    339 	int slot;
    340 	nubus_slot *fmt;
    341 {
    342 	u_long ofs, hdr;
    343 	int i, j, found, hdr_size;
    344 	u_int8_t lanes;
    345 
    346 #ifdef DEBUG
    347 	if (nubus_debug & NDB_PROBE)
    348 		printf("probing slot %x\n", slot);
    349 #endif
    350 
    351 	/*
    352 	 * The idea behind this glorious work of art is to probe for only
    353 	 * valid bytelanes values at appropriate locations (see DC&D p. 159
    354 	 * for a list).  Note the pattern:  the first 8 values are at offset
    355 	 * 0xffffff in the slot's space; the next 4 values at 0xfffffe; the
    356 	 * next 2 values at 0xfffffd; and the last one at 0xfffffc.
    357 	 *
    358 	 * The nested loops implement an efficient search of this space,
    359 	 * probing first for a valid address, then checking for each of the
    360 	 * valid bytelanes values at that address.
    361 	 */
    362 	ofs = NBMEMSIZE;
    363 	lanes = 0xf;
    364 
    365 	for (j = 8, found = 0; j > 0 && !found; j >>= 1) {
    366 		ofs--;
    367 		for (i = j; i > 0; i--, lanes--) {
    368 			if (!mac68k_bus_space_probe(bst, bsh, ofs, 1)) {
    369 				lanes -= i;
    370 				break;
    371 			}
    372 			if (bus_space_read_1(bst, bsh, ofs) ==
    373 			    (((~lanes & 0xf) << 4) | lanes)) {
    374 				found = 1;
    375 				break;
    376 			}
    377 		}
    378 	}
    379 
    380 	if (!found) {
    381 #ifdef DEBUG
    382 		if (nubus_debug & NDB_PROBE)
    383 			printf("bytelanes not found for slot %x\n", slot);
    384 #endif
    385 		return 0;
    386 	}
    387 
    388 	fmt->bytelanes = lanes;
    389 	fmt->step = nbits[(lanes & 0x0f)];
    390 	fmt->slot = slot;	/* XXX redundant; get rid of this someday */
    391 
    392 #ifdef DEBUG
    393 	if (nubus_debug & NDB_PROBE)
    394 		printf("bytelanes of 0x%x found for slot 0x%x.\n",
    395 		    fmt->bytelanes, slot);
    396 #endif
    397 
    398 	/*
    399 	 * Go ahead and attempt to load format header.
    400 	 * First, we need to find the first byte beyond memory that
    401 	 * would be valid.  This is necessary for NUBUS_ROM_offset()
    402 	 * to work.
    403 	 */
    404 	hdr = NBMEMSIZE;
    405 	hdr_size = 20;
    406 
    407 	i = 0x10 | (lanes & 0x0f);
    408 	while ((i & 1) == 0) {
    409 		hdr++;
    410 		i >>= 1;
    411 	}
    412 	fmt->top = hdr;
    413 	hdr = nubus_adjust_ptr(lanes, hdr, -hdr_size);
    414 #ifdef DEBUG
    415 	if (nubus_debug & NDB_PROBE)
    416 		printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
    417 		    fmt->top, hdr_size, hdr);
    418 	if (nubus_debug & NDB_ARITH)
    419 		for (i = 1 ; i < 8 ; i++)
    420 			printf("0x%lx - 0x%x = 0x%lx, + 0x%x = 0x%lx.\n",
    421 			    hdr, i, nubus_adjust_ptr(lanes, hdr, -i),
    422 			    i, nubus_adjust_ptr(lanes, hdr,  i));
    423 #endif
    424 
    425 	fmt->directory_offset =
    426 	    0xff000000 | nubus_read_4(bst, bsh, lanes, hdr);
    427 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    428 	fmt->length = nubus_read_4(bst, bsh, lanes, hdr);
    429 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    430 	fmt->crc = nubus_read_4(bst, bsh, lanes, hdr);
    431 	hdr = nubus_adjust_ptr(lanes, hdr, 4);
    432 	fmt->revision_level = nubus_read_1(bst, bsh, lanes, hdr);
    433 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    434 	fmt->format = nubus_read_1(bst, bsh, lanes, hdr);
    435 	hdr = nubus_adjust_ptr(lanes, hdr, 1);
    436 	fmt->test_pattern = nubus_read_4(bst, bsh, lanes, hdr);
    437 
    438 #ifdef DEBUG
    439 	if (nubus_debug & NDB_PROBE) {
    440 		printf("Directory offset 0x%x\t", fmt->directory_offset);
    441 		printf("Length 0x%x\t", fmt->length);
    442 		printf("CRC 0x%x\n", fmt->crc);
    443 		printf("Revision level 0x%x\t", fmt->revision_level);
    444 		printf("Format 0x%x\t", fmt->format);
    445 		printf("Test Pattern 0x%x\n", fmt->test_pattern);
    446 	}
    447 #endif
    448 
    449 	if ((fmt->directory_offset & 0x00ff0000) == 0) {
    450 		printf("Invalid looking directory offset (0x%x)!\n",
    451 		    fmt->directory_offset);
    452 		return 0;
    453 	}
    454 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
    455 		printf("Nubus--test pattern invalid:\n");
    456 		printf("       slot 0x%x, bytelanes 0x%x?\n", fmt->slot, lanes);
    457 		printf("       read test 0x%x, compare with 0x%x.\n",
    458 		    fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
    459 		return 0;
    460 	}
    461 
    462 	/* Perform CRC */
    463 	if (fmt->crc != nubus_calc_CRC(bst, bsh, fmt)) {
    464 		printf("Nubus--crc check failed, slot 0x%x.\n", fmt->slot);
    465 		return 0;
    466 	}
    467 
    468 	return 1;
    469 }
    470 
    471 static u_int32_t
    472 nubus_calc_CRC(bst, bsh, fmt)
    473 	bus_space_tag_t bst;
    474 	bus_space_handle_t bsh;
    475 	nubus_slot	*fmt;
    476 {
    477 #if 0
    478 	u_long base, ptr, crc_loc;
    479 	u_int32_t sum;
    480 	u_int8_t lanes = fmt->bytelanes;
    481 
    482 	base = fmt->top;
    483 	crc_loc = NUBUS_ROM_offset(fmt, base, -12);
    484 	ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
    485 
    486 	sum = 0;
    487 	while (ptr < base)
    488 		roll #1, sum
    489 		if (ptr == crc_loc) {
    490 			roll #3, sum
    491 			ptr = nubus_adjust_ptr(lanes, ptr, 3);
    492 		} else {
    493 			sum += nubus_read_1(bst, bsh, lanes, ptr);
    494 		}
    495 		ptr = nubus_adjust_ptr(lanes, ptr, 1);
    496 	}
    497 
    498 	return sum;
    499 #endif
    500 	return fmt->crc;
    501 }
    502 
    503 /*
    504  * Compute byte offset on card, taking into account bytelanes.
    505  * Base must be on a valid bytelane for this function to work.
    506  * Return the new address.
    507  *
    508  * XXX -- There has GOT to be a better way to do this.
    509  */
    510 static u_long
    511 nubus_adjust_ptr(lanes, base, amt)
    512 	u_int8_t lanes;
    513 	u_long base;
    514 	long amt;
    515 {
    516 	u_int8_t b, t;
    517 
    518 	if (!amt)
    519 		return base;
    520 
    521 	if (amt < 0) {
    522 		amt = -amt;
    523 		b = lanes;
    524 		t = (b << 4);
    525 		b <<= (3 - (base & 0x3));
    526 		while (amt) {
    527 			b <<= 1;
    528 			if (b == t)
    529 				b = lanes;
    530 			if (b & 0x08)
    531 				amt--;
    532 			base--;
    533 		}
    534 		return base;
    535 	}
    536 
    537 	t = (lanes & 0xf) | 0x10;
    538 	b = t >> (base & 0x3);
    539 	while (amt) {
    540 		b >>= 1;
    541 		if (b == 1)
    542 			b = t;
    543 		if (b & 1)
    544 			amt--;
    545 		base++;
    546 	}
    547 
    548 	return base;
    549 }
    550 
    551 static u_int8_t
    552 nubus_read_1(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 	return bus_space_read_1(bst, bsh, ofs);
    559 }
    560 
    561 #ifdef notyet
    562 /* Nothing uses this, yet */
    563 static u_int16_t
    564 nubus_read_2(bst, bsh, lanes, ofs)
    565 	bus_space_tag_t bst;
    566 	bus_space_handle_t bsh;
    567 	u_int8_t lanes;
    568 	u_long ofs;
    569 {
    570 	u_int16_t s;
    571 
    572 	s = (nubus_read_1(bst, bsh, lanes, ofs) << 8);
    573 	ofs = nubus_adjust_ptr(lanes, ofs, 1);
    574 	s |= nubus_read_1(bst, bsh, lanes, ofs);
    575 	return s;
    576 }
    577 #endif
    578 
    579 static u_int32_t
    580 nubus_read_4(bst, bsh, lanes, ofs)
    581 	bus_space_tag_t bst;
    582 	bus_space_handle_t bsh;
    583 	u_int8_t lanes;
    584 	u_long ofs;
    585 {
    586 	u_int32_t l;
    587 	int i;
    588 
    589 	l = 0;
    590 	for (i = 0; i < 4; i++) {
    591 		l = (l << 8) | nubus_read_1(bst, bsh, lanes, ofs);
    592 		ofs = nubus_adjust_ptr(lanes, ofs, 1);
    593 	}
    594 	return l;
    595 }
    596 
    597 void
    598 nubus_get_main_dir(fmt, dir_return)
    599 	nubus_slot *fmt;
    600 	nubus_dir *dir_return;
    601 {
    602 #ifdef DEBUG
    603 	if (nubus_debug & NDB_FOLLOW)
    604 		printf("nubus_get_main_dir(%p, %p)\n",
    605 		    fmt, dir_return);
    606 #endif
    607 	dir_return->dirbase = nubus_adjust_ptr(fmt->bytelanes, fmt->top,
    608 	    fmt->directory_offset - 20);
    609 	dir_return->curr_ent = dir_return->dirbase;
    610 }
    611 
    612 void
    613 nubus_get_dir_from_rsrc(fmt, dirent, dir_return)
    614 	nubus_slot *fmt;
    615 	nubus_dirent *dirent;
    616 	nubus_dir *dir_return;
    617 {
    618 	u_long loc;
    619 
    620 #ifdef DEBUG
    621 	if (nubus_debug & NDB_FOLLOW)
    622 		printf("nubus_get_dir_from_rsrc(%p, %p, %p).\n",
    623 		    fmt, dirent, dir_return);
    624 #endif
    625 	if ((loc = dirent->offset) & 0x800000) {
    626 		loc |= 0xff000000;
    627 	}
    628 	dir_return->dirbase =
    629 	    nubus_adjust_ptr(fmt->bytelanes, dirent->myloc, loc);
    630 	dir_return->curr_ent = dir_return->dirbase;
    631 }
    632 
    633 int
    634 nubus_find_rsrc(bst, bsh, fmt, dir, rsrcid, dirent_return)
    635 	bus_space_tag_t bst;
    636 	bus_space_handle_t bsh;
    637 	nubus_slot *fmt;
    638 	nubus_dir *dir;
    639 	u_int8_t rsrcid;
    640 	nubus_dirent *dirent_return;
    641 {
    642 	u_long entry;
    643 	u_int8_t byte, lanes = fmt->bytelanes;
    644 
    645 #ifdef DEBUG
    646 	if (nubus_debug & NDB_FOLLOW)
    647 		printf("nubus_find_rsrc(%p, %p, 0x%x, %p)\n",
    648 		    fmt, dir, rsrcid, dirent_return);
    649 #endif
    650 	if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN)
    651 		return -1;
    652 
    653 	entry = dir->curr_ent;
    654 	do {
    655 		byte = nubus_read_1(bst, bsh, lanes, entry);
    656 #ifdef DEBUG
    657 		if (nubus_debug & NDB_FOLLOW)
    658 			printf("\tFound rsrc 0x%x.\n", byte);
    659 #endif
    660 		if (byte == rsrcid) {
    661 			dirent_return->myloc = entry;
    662 			dirent_return->rsrc_id = rsrcid;
    663 			entry = nubus_read_4(bst, bsh, lanes, entry);
    664 			dirent_return->offset = (entry & 0x00ffffff);
    665 			return 1;
    666 		}
    667 		if (byte == 0xff) {
    668 			entry = dir->dirbase;
    669 		} else {
    670 			entry = nubus_adjust_ptr(lanes, entry, 4);
    671 		}
    672 	} while (entry != (u_long)dir->curr_ent);
    673 	return 0;
    674 }
    675 
    676 int
    677 nubus_get_ind_data(bst, bsh, fmt, dirent, data_return, nbytes)
    678 	bus_space_tag_t bst;
    679 	bus_space_handle_t bsh;
    680 	nubus_slot *fmt;
    681 	nubus_dirent *dirent;
    682 	caddr_t data_return;
    683 	int nbytes;
    684 {
    685 	u_long loc;
    686 	u_int8_t lanes = fmt->bytelanes;
    687 
    688 #ifdef DEBUG
    689 	if (nubus_debug & NDB_FOLLOW)
    690 		printf("nubus_get_ind_data(%p, %p, %p, %d).\n",
    691 		    fmt, dirent, data_return, nbytes);
    692 #endif
    693 	if ((loc = dirent->offset) & 0x800000) {
    694 		loc |= 0xff000000;
    695 	}
    696 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    697 
    698 	while (nbytes--) {
    699 		*data_return++ = nubus_read_1(bst, bsh, lanes, loc);
    700 		loc = nubus_adjust_ptr(lanes, loc, 1);
    701 	}
    702 	return 1;
    703 }
    704 
    705 int
    706 nubus_get_c_string(bst, bsh, fmt, dirent, data_return, max_bytes)
    707 	bus_space_tag_t bst;
    708 	bus_space_handle_t bsh;
    709 	nubus_slot *fmt;
    710 	nubus_dirent *dirent;
    711 	caddr_t data_return;
    712 	int max_bytes;
    713 {
    714 	u_long loc;
    715 	u_int8_t lanes = fmt->bytelanes;
    716 
    717 #ifdef DEBUG
    718 	if (nubus_debug & NDB_FOLLOW)
    719 		printf("nubus_get_c_string(%p, %p, %p, %d).\n",
    720 		    fmt, dirent, data_return, max_bytes);
    721 #endif
    722 	if ((loc = dirent->offset) & 0x800000)
    723 		loc |= 0xff000000;
    724 
    725 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    726 
    727 	*data_return = '\0';
    728 	while (max_bytes--) {
    729 		if ((*data_return++ =
    730 		    nubus_read_1(bst, bsh, lanes, loc)) == 0)
    731 			return 1;
    732 		loc = nubus_adjust_ptr(lanes, loc, 1);
    733 	}
    734 	*(data_return-1) = '\0';
    735 	return 0;
    736 }
    737 
    738 /*
    739  * Get list of address ranges for an sMemory resource
    740  * ->  DC&D, p.171
    741  */
    742 int
    743 nubus_get_smem_addr_rangelist(bst, bsh, fmt, dirent, data_return)
    744 	bus_space_tag_t bst;
    745 	bus_space_handle_t bsh;
    746 	nubus_slot *fmt;
    747 	nubus_dirent *dirent;
    748 	caddr_t data_return;
    749 {
    750 	u_long loc;
    751 	u_int8_t lanes = fmt->bytelanes;
    752 	long blocklen;
    753 	caddr_t blocklist;
    754 
    755 #ifdef DEBUG
    756 	if (nubus_debug & NDB_FOLLOW)
    757 		printf("nubus_get_smem_addr_rangelist(%p, %p, %p).\n",
    758 		    fmt, dirent, data_return);
    759 #endif
    760 	if ((loc = dirent->offset) & 0x800000) {
    761 		loc |= 0xff000000;
    762 	}
    763 	loc = nubus_adjust_ptr(lanes, dirent->myloc, loc);
    764 
    765 	/* Obtain the block length from the head of the list */
    766 	blocklen = nubus_read_4(bst, bsh, lanes, loc);
    767 
    768 	/*
    769 	 * malloc a block of (blocklen) bytes
    770 	 * caller must recycle block after use
    771 	 */
    772 	MALLOC(blocklist,caddr_t,blocklen,M_TEMP,M_WAITOK);
    773 
    774 	/* read ((blocklen - 4) / 8) (length,offset) pairs into block */
    775 	nubus_get_ind_data(bst, bsh, fmt, dirent, blocklist, blocklen);
    776 #ifdef DEBUG
    777 	if (nubus_debug & NDB_FOLLOW) {
    778 		int ii;
    779 		nubus_smem_rangelist *rlist;
    780 
    781 		rlist = (nubus_smem_rangelist *)blocklist;
    782 		printf("\tblock@%p, len 0x0%X\n", rlist, rlist->length);
    783 
    784 		for (ii=0; ii < ((blocklen - 4) / 8); ii++) {
    785 			printf("\tRange %d: base addr 0x%X [0x%X]\n", ii,
    786 			    rlist->range[ii].offset, rlist->range[ii].length);
    787 		}
    788 	}
    789 #endif
    790 	*(caddr_t *)data_return = blocklist;
    791 
    792 	return 1;
    793 }
    794 
    795 static char	*huh = "???";
    796 
    797 char *
    798 nubus_get_vendor(bst, bsh, fmt, rsrc)
    799 	bus_space_tag_t bst;
    800 	bus_space_handle_t bsh;
    801 	nubus_slot *fmt;
    802 	int rsrc;
    803 {
    804 	static char str_ret[64];
    805 	nubus_dir dir;
    806 	nubus_dirent ent;
    807 
    808 #ifdef DEBUG
    809 	if (nubus_debug & NDB_FOLLOW)
    810 		printf("nubus_get_vendor(%p, 0x%x).\n", fmt, rsrc);
    811 #endif
    812 	nubus_get_main_dir(fmt, &dir);
    813 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    814 		return huh;
    815 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    816 
    817 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_VENDORINFO, &ent)
    818 	    <= 0)
    819 		return huh;
    820 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    821 
    822 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, rsrc, &ent) <= 0)
    823 		return huh;
    824 
    825 	nubus_get_c_string(bst, bsh, fmt, &ent, str_ret, 64);
    826 
    827 	return str_ret;
    828 }
    829 
    830 char *
    831 nubus_get_card_name(bst, bsh, fmt)
    832 	bus_space_tag_t bst;
    833 	bus_space_handle_t bsh;
    834 	nubus_slot *fmt;
    835 {
    836 	static char name_ret[64];
    837 	nubus_dir dir;
    838 	nubus_dirent ent;
    839 
    840 #ifdef DEBUG
    841 	if (nubus_debug & NDB_FOLLOW)
    842 		printf("nubus_get_card_name(%p).\n", fmt);
    843 #endif
    844 	nubus_get_main_dir(fmt, &dir);
    845 
    846 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, 1, &ent) <= 0)
    847 		return huh;
    848 
    849 	nubus_get_dir_from_rsrc(fmt, &ent, &dir);
    850 
    851 	if (nubus_find_rsrc(bst, bsh, fmt, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
    852 		return huh;
    853 
    854 	nubus_get_c_string(bst, bsh, fmt, &ent, name_ret, 64);
    855 
    856 	return name_ret;
    857 }
    858 
    859 #ifdef DEBUG
    860 void
    861 nubus_scan_slot(bst, slotno)
    862 	bus_space_tag_t bst;
    863 	int slotno;
    864 {
    865 	int i=0, state=0;
    866 	char twirl[] = "-\\|/";
    867 	bus_space_handle_t sc_bsh;
    868 
    869 	if (bus_space_map(bst, NUBUS_SLOT2PA(slotno), NBMEMSIZE, 0, &sc_bsh)) {
    870 		printf("nubus_scan_slot: failed to map slot %x\n", slotno);
    871 		return;
    872 	}
    873 
    874 	printf("Scanning slot %c for accessible regions:\n",
    875 		slotno == 9 ? '9' : slotno - 10 + 'A');
    876 	for (i=0 ; i<NBMEMSIZE; i++) {
    877 		if (mac68k_bus_space_probe(bst, sc_bsh, i, 1)) {
    878 			if (state == 0) {
    879 				printf("\t0x%x-", i);
    880 				state = 1;
    881 			}
    882 		} else {
    883 			if (state) {
    884 				printf("0x%x\n", i);
    885 				state = 0;
    886 			}
    887 		}
    888 		if (i%100 == 0) {
    889 			printf("%c\b", twirl[(i/100)%4]);
    890 		}
    891 	}
    892 	if (state) {
    893 		printf("0x%x\n", i);
    894 	}
    895 	return;
    896 }
    897 #endif
    898