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