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