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