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