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