nubus.c revision 1.35 1 /* $NetBSD: nubus.c,v 1.35 1997/04/22 20:20:32 scottr Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Allen Briggs. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Allen Briggs.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/buf.h>
36 #include <sys/conf.h>
37 #include <sys/dmap.h>
38
39 #include <vm/vm.h>
40 #include <vm/vm_kern.h>
41 #include <vm/vm_map.h>
42
43 #include <machine/autoconf.h>
44 #include <machine/bus.h>
45 #include <machine/vmparam.h>
46 #include <machine/param.h>
47 #include <machine/cpu.h>
48 #include <machine/pte.h>
49 #include <machine/viareg.h>
50
51 #include <vm/vm.h>
52
53 #include "nubus.h"
54
55 #ifdef DEBUG
56 static int nubus_debug = 0x01;
57 #define NDB_PROBE 0x1
58 #define NDB_FOLLOW 0x2
59 #define NDB_ARITH 0x4
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 int nubus_video_resource __P((int));
66
67 static int probe_slot __P((int slot, nubus_slot *fmt));
68 static u_long IncPtr __P((nubus_slot *fmt, u_long base, long amt));
69 static u_long nubus_calc_CRC __P((nubus_slot *fmt));
70 static u_char GetByte __P((nubus_slot *fmt, u_long ptr));
71 #ifdef notyet
72 /* unused */ static u_short GetWord __P((nubus_slot *fmt, u_long ptr));
73 #endif
74 static u_long GetLong __P((nubus_slot *fmt, u_long ptr));
75
76 static int nubus_peek __P((vm_offset_t, int));
77 static char *nubus_mapin __P((int, int));
78
79 struct cfattach nubus_ca = {
80 sizeof(struct nubus_softc), nubus_match, nubus_attach
81 };
82
83 struct cfdriver nubus_cd = {
84 NULL, "nubus", DV_DULL,
85 };
86
87 static int
88 nubus_match(parent, cf, aux)
89 struct device *parent;
90 struct cfdata *cf;
91 void *aux;
92 {
93 static int nubus_matched = 0;
94
95 /* Allow only one instance. */
96 if (nubus_matched)
97 return (0);
98
99 nubus_matched = 1;
100 return (1);
101 }
102
103 static void
104 nubus_attach(parent, self, aux)
105 struct device *parent, *self;
106 void *aux;
107 {
108 struct nubus_attach_args na_args;
109 nubus_slot fmtblock;
110 nubus_dir dir;
111 nubus_dirent dirent;
112 nubus_type slottype;
113 u_long entry;
114 int i, rsrcid;
115
116 printf("\n");
117
118 for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
119 if (probe_slot(i, &fmtblock) <= 0)
120 continue;
121
122 rsrcid = 0x80;
123
124 nubus_get_main_dir(&fmtblock, &dir);
125
126 /*
127 * Get the resource for the first function on the card.
128 * This is assumed to be at resource ID 0x80. If we can
129 * not find this entry (as we can not on some video cards),
130 * check to see if we can get a different ID from the list
131 * of video resources given to us by the booter. If that
132 * doesn't work either, take the first resource following
133 * the board resource.
134 */
135 if (nubus_find_rsrc(&fmtblock, &dir, rsrcid, &dirent) <= 0) {
136 if ((rsrcid = nubus_video_resource(i)) == -1) {
137 /*
138 * Since nubus_find_rsrc failed, the directory
139 * is back at its base.
140 */
141 entry = dir.curr_ent;
142
143 /*
144 * All nubus cards should have a board
145 * resource, but be sure that's what it
146 * is before we skip it.
147 */
148 rsrcid = GetByte(&fmtblock, entry);
149 if (rsrcid == 0x1)
150 entry =
151 IncPtr(&fmtblock, dir.curr_ent, 4);
152
153 rsrcid = GetByte(&fmtblock, entry);
154 #ifdef DEBUG
155 if (nubus_debug & NDB_FOLLOW)
156 printf("\tUsing rsrc 0x%x.\n", rsrcid);
157 #endif
158 if (rsrcid == 0xff)
159 continue; /* end of chain */
160 }
161 /*
162 * Try to find the resource passed by the booter
163 * or the one we just tracked down.
164 */
165 if (nubus_find_rsrc(&fmtblock, &dir,
166 rsrcid, &dirent) <= 0) {
167 continue;
168 }
169 }
170
171 nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
172
173 if (nubus_find_rsrc(&fmtblock, &dir, NUBUS_RSRC_TYPE,
174 &dirent) <= 0)
175 continue;
176
177 if (nubus_get_ind_data(&fmtblock, &dirent,
178 (caddr_t) &slottype, sizeof(nubus_type)) <= 0)
179 continue;
180
181 /*
182 * If this is a display card, try to pull out the correct
183 * display mode as passed by the booter.
184 */
185 if (slottype.category == NUBUS_CATEGORY_DISPLAY) {
186 int r;
187
188 if ((r = nubus_video_resource(i)) != -1) {
189
190 nubus_get_main_dir(&fmtblock, &dir);
191
192 if (nubus_find_rsrc(&fmtblock, &dir,
193 r, &dirent) <= 0)
194 continue;
195
196 nubus_get_dir_from_rsrc(&fmtblock,
197 &dirent, &dir);
198
199 if (nubus_find_rsrc(&fmtblock, &dir,
200 NUBUS_RSRC_TYPE, &dirent) <= 0)
201 continue;
202
203 if (nubus_get_ind_data(&fmtblock, &dirent,
204 (caddr_t) &slottype,
205 sizeof(nubus_type)) <= 0)
206 continue;
207 }
208 }
209
210 na_args.slot = i;
211 na_args.rsrcid = rsrcid;
212 na_args.category = slottype.category;
213 na_args.type = slottype.type;
214 na_args.drsw = slottype.drsw;
215 na_args.drhw = slottype.drhw;
216 na_args.fmt = &fmtblock;
217
218 config_found(self, &na_args, nubus_print);
219 }
220
221 enable_nubus_intr();
222 }
223
224 static int
225 nubus_print(aux, name)
226 void *aux;
227 const char *name;
228 {
229 struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
230
231 if (name) {
232 printf("%s: slot %x: %s", name, na->fmt->slot,
233 nubus_get_card_name(na->fmt));
234 printf(" (Vendor: %s,",
235 nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_ID));
236 printf(" Part: %s",
237 nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_PART));
238 #ifdef DIAGNOSTIC
239 printf(" Type: %04x %04x %04x %04x",
240 na->category, na->type, na->drsw, na->drhw);
241 #endif
242 }
243 return (UNCONF);
244 }
245
246 int
247 nubus_video_resource(slot)
248 int slot;
249 {
250 extern u_int16_t mac68k_vrsrc_vec[];
251 int i;
252
253 for (i = 0 ; i < 6 ; i++)
254 if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
255 return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
256 return (-1);
257 }
258
259 /*
260 * Probe a given nubus slot. If a card is there and we can get the
261 * format block from it's clutching decl. ROMs, fill the format block
262 * and return non-zero. If we can't find a card there with a valid
263 * decl. ROM, return 0.
264 *
265 * First, we check to see if we can access the memory at the tail
266 * end of the slot. If so, then we check for a bytelanes byte. We
267 * could probably just return a failure status if we bus error on
268 * the first try, but there really is little reason not to go ahead
269 * and check the other three locations in case there's a wierd card
270 * out there.
271 *
272 * Checking for a card involves locating the "bytelanes" byte which
273 * tells us how to interpret the declaration ROM's data. The format
274 * block is at the top of the card's standard memory space and the
275 * bytelanes byte is at the end of that block.
276 *
277 * After some inspection of the bytelanes byte, it appears that it
278 * takes the form 0xXY where Y is a bitmask of the bytelanes in use
279 * and X is a bitmask of the lanes to ignore. Hence, (X ^ Y) == 0
280 * and (less obviously), Y will have the upper N bits clear if it is
281 * found N bytes from the last possible location. Both that and
282 * the exclusive-or check are made.
283 *
284 * If a valid
285 */
286 static u_char nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
287 static int
288 probe_slot(slot, fmt)
289 int slot;
290 nubus_slot *fmt;
291 {
292 caddr_t rom_probe;
293 vm_offset_t hdr;
294 #ifdef DEBUG
295 vm_offset_t pa;
296 #endif
297 u_int data;
298 int hdr_size, i;
299
300 fmt->bytelanes = 0;
301 fmt->slot = (u_long)slot;
302
303 rom_probe = (caddr_t) (NUBUS_SLOT2PA(fmt->slot) + NBMEMSIZE);
304
305 #ifdef DEBUG
306 if (nubus_debug & NDB_PROBE) {
307 pa = pmap_extract(pmap_kernel(), (vm_offset_t) rom_probe - 1);
308 printf("probing slot %d, first probe at 0x%p (PA 0x%lx).\n",
309 slot, rom_probe - 1, pa);
310 }
311 #endif
312
313 for (i = 4; i && (fmt->bytelanes == 0); i--) {
314
315 rom_probe--;
316
317 data = nubus_peek((vm_offset_t) rom_probe, 1);
318 if (data == -1)
319 continue;
320
321 if (data == 0)
322 continue;
323
324 if ( ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
325 && ((data & 0x0f) < (1 << i)) ) {
326 fmt->bytelanes = data;
327 fmt->step = nbits[(data & 0x0f)];
328 }
329 }
330 #ifdef DEBUG
331 if (nubus_debug & NDB_PROBE)
332 if (fmt->bytelanes == 0)
333 printf("bytelanes not found for slot 0x%x.\n", slot);
334 #endif
335
336 if (fmt->bytelanes == 0)
337 return 0;
338
339 #ifdef DEBUG
340 if (nubus_debug & NDB_PROBE)
341 printf("bytelanes of 0x%x found for slot 0x%x.\n",
342 fmt->bytelanes, slot);
343 #endif
344
345 hdr_size = 20;
346
347 /*
348 * Go ahead and attempt to load format header.
349 * First, we need to find the first byte beyond memory that
350 * would be valid. This is necessary for NUBUS_ROM_offset()
351 * to work.
352 */
353 hdr = (vm_offset_t)
354 nubus_mapin(NUBUS_SLOT2PA(fmt->slot), NBMEMSIZE);
355 if (hdr == NULL) {
356 printf("Failed to map %d bytes for NuBUS slot %d probe. ",
357 NBMEMSIZE, fmt->slot);
358 printf("Physical slot address %x\n",
359 (unsigned int) NUBUS_SLOT2PA(fmt->slot));
360 }
361 fmt->virtual_base = hdr;
362 hdr += NBMEMSIZE;
363
364 i = 0x10 | (fmt->bytelanes & 0x0f);
365 while ((i & 1) == 0) {
366 hdr++;
367 i >>= 1;
368 }
369 fmt->top = hdr;
370 hdr = IncPtr(fmt, hdr, -hdr_size);
371 #ifdef DEBUG
372 if (nubus_debug & NDB_PROBE)
373 printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
374 fmt->top, hdr_size, hdr);
375 #if 0
376 for (i=1 ; i < 8 ; i++) {
377 printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
378 hdr, i, IncPtr(fmt, hdr, -i),
379 i, IncPtr(fmt, hdr, i));
380 }
381 #endif
382 #endif
383
384 fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
385 hdr = IncPtr(fmt, hdr, 4);
386 fmt->length = GetLong(fmt, hdr);
387 hdr = IncPtr(fmt, hdr, 4);
388 fmt->crc = GetLong(fmt, hdr);
389 hdr = IncPtr(fmt, hdr, 4);
390 fmt->revision_level = GetByte(fmt, hdr);
391 hdr = IncPtr(fmt, hdr, 1);
392 fmt->format = GetByte(fmt, hdr);
393 hdr = IncPtr(fmt, hdr, 1);
394 fmt->test_pattern = GetLong(fmt, hdr);
395
396 #ifdef DEBUG
397 if (nubus_debug & NDB_PROBE) {
398 printf("Directory offset 0x%x\t", fmt->directory_offset);
399 printf("Length 0x%x\t", fmt->length);
400 printf("CRC 0x%x\n", fmt->crc);
401 printf("Revision level 0x%x\t", fmt->revision_level);
402 printf("Format 0x%x\t", fmt->format);
403 printf("Test Pattern 0x%x\n", fmt->test_pattern);
404 }
405 #endif
406
407 if ((fmt->directory_offset & 0x00ff0000) == 0) {
408 printf("Invalid looking directory offset (0x%x)!\n",
409 fmt->directory_offset);
410 return 0;
411 }
412 if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
413 printf("Nubus--test pattern invalid:\n");
414 printf(" slot 0x%x, bytelanes 0x%x?\n",
415 fmt->slot, fmt->bytelanes);
416 printf(" read test 0x%x, compare with 0x%x.\n",
417 fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
418 return 0;
419 }
420
421 /* Perform CRC */
422 if (fmt->crc != nubus_calc_CRC(fmt)) {
423 printf("Nubus--crc check failed, slot 0x%x.\n",
424 fmt->slot);
425 return 0;
426 }
427
428 return 1;
429 }
430
431 /*
432 * Compute byte offset on card, taking into account bytelanes.
433 * Base must be on a valid bytelane for this function to work.
434 * Return the new address.
435 *
436 * XXX -- There has GOT to be a better way to do this.
437 */
438 static u_long
439 IncPtr(fmt, base, amt)
440 nubus_slot *fmt;
441 u_long base;
442 long amt;
443 {
444 u_char b, t;
445
446 if (!amt)
447 return base;
448
449 if (amt < 0) {
450 amt = -amt;
451 b = fmt->bytelanes;
452 t = (b << 4);
453 b <<= (3 - (base & 0x3));
454 while (amt) {
455 b <<= 1;
456 if (b == t)
457 b = fmt->bytelanes;
458 if (b & 0x08)
459 amt--;
460 base--;
461 }
462 return base;
463 }
464
465 t = (fmt->bytelanes & 0xf) | 0x10;
466 b = t >> (base & 0x3);
467 while (amt) {
468 b >>= 1;
469 if (b == 1)
470 b = t;
471 if (b & 1)
472 amt--;
473 base++;
474 }
475
476 return base;
477 }
478
479 static u_long
480 nubus_calc_CRC(fmt)
481 nubus_slot *fmt;
482 {
483 #if 0
484 u_long base, ptr, crc_loc, sum;
485 int i;
486
487 base = fmt->top;
488 crc_loc = NUBUS_ROM_offset(fmt, base, -12);
489 ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
490
491 sum = 0;
492 while (ptr < base)
493 roll #1, sum
494 if (ptr == crc_loc) {
495 roll #3, sum
496 ptr = IncPtr(fmt, ptr, 3);
497 } else {
498 sum += GetByte(fmt, ptr);
499 }
500 ptr = IncPtr(fmt, ptr, 1);
501 }
502
503 return sum;
504 #endif
505 return fmt->crc;
506 }
507
508 static u_char
509 GetByte(fmt, ptr)
510 nubus_slot *fmt;
511 u_long ptr;
512 {
513 return *(caddr_t)ptr;
514 }
515
516 #ifdef notyet
517 /* Nothing uses this, yet */
518 static u_short
519 GetWord(fmt, ptr)
520 nubus_slot *fmt;
521 u_long ptr;
522 {
523 u_short s;
524
525 s = (GetByte(fmt, ptr) << 8);
526 ptr = IncPtr(fmt, ptr, 1);
527 s |= GetByte(fmt, ptr);
528 return s;
529 }
530 #endif
531
532 static u_long
533 GetLong(fmt, ptr)
534 nubus_slot *fmt;
535 u_long ptr;
536 {
537 register u_long l;
538 register int i;
539
540 l = 0;
541 for ( i = 0; i < 4; i++) {
542 l = (l << 8) | GetByte(fmt, ptr);
543 ptr = IncPtr(fmt, ptr, 1);
544 }
545 return l;
546 }
547
548 void
549 nubus_get_main_dir(slot, dir_return)
550 nubus_slot *slot;
551 nubus_dir *dir_return;
552 {
553 #ifdef DEBUG
554 if (nubus_debug & NDB_FOLLOW)
555 printf("nubus_get_main_dir(0x%x, 0x%x)\n",
556 (u_int) slot, (u_int) dir_return);
557 #endif
558 dir_return->dirbase = IncPtr(slot, slot->top,
559 slot->directory_offset - 20);
560 dir_return->curr_ent = dir_return->dirbase;
561 }
562
563 int
564 nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
565 nubus_slot *slot;
566 nubus_dir *dir;
567 u_int8_t rsrcid;
568 nubus_dirent *dirent_return;
569 {
570 u_long entry;
571 u_char byte;
572
573 #ifdef DEBUG
574 if (nubus_debug & NDB_FOLLOW)
575 printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
576 (u_int) slot, (u_int) dir, (u_int) rsrcid,
577 (u_int) dirent_return);
578 #endif
579 if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
580 return -1;
581
582 entry = dir->curr_ent;
583 do {
584 byte = GetByte(slot, entry);
585 #ifdef DEBUG
586 if (nubus_debug & NDB_FOLLOW)
587 printf("\tFound rsrc 0x%x.\n", byte);
588 #endif
589 if (byte == rsrcid) {
590 dirent_return->myloc = entry;
591 dirent_return->rsrc_id = rsrcid;
592 entry = GetLong(slot, entry);
593 dirent_return->offset = (entry & 0x00ffffff);
594 return 1;
595 }
596 if (byte == 0xff) {
597 entry = dir->dirbase;
598 } else {
599 entry = IncPtr(slot, entry, 4);
600 }
601 } while (entry != (u_long) dir->curr_ent);
602 return 0;
603 }
604
605 void
606 nubus_get_dir_from_rsrc(slot, dirent, dir_return)
607 nubus_slot *slot;
608 nubus_dirent *dirent;
609 nubus_dir *dir_return;
610 {
611 u_long loc;
612
613 #ifdef DEBUG
614 if (nubus_debug & NDB_FOLLOW)
615 printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
616 (u_int) slot, (u_int) dirent, (u_int) dir_return);
617 #endif
618 if ((loc = dirent->offset) & 0x800000) {
619 loc |= 0xff000000;
620 }
621 dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
622 dir_return->curr_ent = dir_return->dirbase;
623 }
624
625 int
626 nubus_get_ind_data(slot, dirent, data_return, nbytes)
627 nubus_slot *slot;
628 nubus_dirent *dirent;
629 caddr_t data_return;
630 int nbytes;
631 {
632 u_long loc;
633
634 #ifdef DEBUG
635 if (nubus_debug & NDB_FOLLOW)
636 printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
637 (u_int) slot, (u_int) dirent, (u_int) data_return,
638 nbytes);
639 #endif
640 if ((loc = dirent->offset) & 0x800000) {
641 loc |= 0xff000000;
642 }
643 loc = IncPtr(slot, dirent->myloc, loc);
644
645 while (nbytes--) {
646 *data_return++ = GetByte(slot, loc);
647 loc = IncPtr(slot, loc, 1);
648 }
649 return 1;
650 }
651
652 int
653 nubus_get_c_string(slot, dirent, data_return, max_bytes)
654 nubus_slot *slot;
655 nubus_dirent *dirent;
656 caddr_t data_return;
657 int max_bytes;
658 {
659 u_long loc;
660
661 #ifdef DEBUG
662 if (nubus_debug & NDB_FOLLOW)
663 printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
664 (u_int) slot, (u_int) dirent, (u_int) data_return,
665 max_bytes);
666 #endif
667 if ((loc = dirent->offset) & 0x800000) {
668 loc |= 0xff000000;
669 }
670 loc = IncPtr(slot, dirent->myloc, loc);
671
672 *data_return = '\0';
673 while (max_bytes--) {
674 if ((*data_return++ = GetByte(slot, loc)) == 0)
675 return 1;
676 loc = IncPtr(slot, loc, 1);
677 }
678 return 0;
679 }
680
681 static char *huh = "???";
682
683 char *
684 nubus_get_vendor(slot, rsrc)
685 nubus_slot *slot;
686 int rsrc;
687 {
688 static char str_ret[64];
689 nubus_dir dir;
690 nubus_dirent ent;
691
692 #ifdef DEBUG
693 if (nubus_debug & NDB_FOLLOW)
694 printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
695 #endif
696 nubus_get_main_dir(slot, &dir);
697 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
698 return huh;
699 nubus_get_dir_from_rsrc(slot, &ent, &dir);
700
701 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
702 return huh;
703 nubus_get_dir_from_rsrc(slot, &ent, &dir);
704
705 if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
706 return huh;
707
708 nubus_get_c_string(slot, &ent, str_ret, 64);
709
710 return str_ret;
711 }
712
713 char *
714 nubus_get_card_name(slot)
715 nubus_slot *slot;
716 {
717 static char name_ret[64];
718 nubus_dir dir;
719 nubus_dirent ent;
720
721 #ifdef DEBUG
722 if (nubus_debug & NDB_FOLLOW)
723 printf("nubus_get_card_name(0x%lx).\n", (u_long) slot);
724 #endif
725 nubus_get_main_dir(slot, &dir);
726
727 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
728 return huh;
729
730 nubus_get_dir_from_rsrc(slot, &ent, &dir);
731
732 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
733 return huh;
734
735 nubus_get_c_string(slot, &ent, name_ret, 64);
736
737 return name_ret;
738 }
739
740 /*
741 * bus_*() functions adapted from sun3 generic "bus" support
742 * by Allen Briggs.
743 */
744
745 vm_offset_t tmp_vpages[1];
746
747 /*
748 * Read addr with size len (1,2,4) into val.
749 * If this generates a bus error, return -1
750 *
751 * Create a temporary mapping,
752 * Try the access using peek_*
753 * Clean up temp. mapping
754 */
755 static int
756 nubus_peek(paddr, sz)
757 vm_offset_t paddr;
758 int sz;
759 {
760 int off, pte, rv;
761 vm_offset_t pgva;
762 caddr_t va;
763
764 off = paddr & PGOFSET;
765 paddr -= off;
766 pte = (paddr & PG_FRAME) | (PG_V | PG_W | PG_CI);
767
768 pgva = tmp_vpages[0];
769 va = (caddr_t)pgva + off;
770
771 mac68k_set_pte(pgva, pte);
772 TBIS(pgva);
773
774 /*
775 * OK, try the access using one of the assembly routines
776 * that will set pcb_onfault and catch any bus errors.
777 */
778 rv = -1;
779 switch (sz) {
780 case 1:
781 if (!badbaddr(va))
782 rv = *((u_char *) va);
783 break;
784 case 2:
785 if (!badwaddr(va))
786 rv = *((u_int16_t *) va);
787 break;
788 case 4:
789 if (!badladdr(va))
790 rv = *((u_int32_t *) va);
791 break;
792 default:
793 printf("bus_peek: invalid size=%d\n", sz);
794 rv = -1;
795 }
796
797 mac68k_set_pte(pgva, PG_NV);
798 TBIS(pgva);
799
800 return rv;
801 }
802
803 static char *
804 nubus_mapin(paddr, sz)
805 int paddr, sz;
806 {
807 int off, pa, pmt=0;
808 vm_offset_t va, retval;
809
810 off = paddr & PGOFSET;
811 pa = paddr - off;
812 sz += off;
813 sz = mac68k_round_page(sz);
814
815 /* Get some kernel virtual address space. */
816 va = kmem_alloc_wait(kernel_map, sz);
817 if (va == 0)
818 panic("bus_mapin");
819 retval = va + off;
820
821 /* Map it to the specified bus. */
822 #if 0 /* XXX */
823 /* This has a problem with wrap-around... */
824 pmap_map((int)va, pa | pmt, pa + sz, VM_PROT_ALL);
825 #else
826 do {
827 pmap_enter(pmap_kernel(), va, pa | pmt, VM_PROT_ALL, FALSE);
828 va += NBPG;
829 pa += NBPG;
830 } while ((sz -= NBPG) > 0);
831 #endif
832
833 return ((char*)retval);
834 }
835