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