nubus.c revision 1.19 1 /* $NetBSD: nubus.c,v 1.19 1996/05/05 06:17:03 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1995 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
36 #include <machine/autoconf.h>
37 #include <machine/cpu.h>
38
39 #include <vm/vm.h>
40
41 #include "nubus.h"
42
43 #if DEBUG
44 static int nubus_debug = 0x01;
45 #define NDB_PROBE 0x1
46 #define NDB_FOLLOW 0x2
47 #define NDB_ARITH 0x4
48 #endif
49
50 static int nubusprint __P((void *, char *));
51 static int nubusmatch __P((struct device *, void *, void *));
52 static void nubusattach __P((struct device *, struct device *, void *));
53
54 static int probe_slot __P((int slot, nubus_slot *fmt));
55 static u_long IncPtr __P((nubus_slot *fmt, u_long base, long amt));
56 static u_long nubus_calc_CRC __P((nubus_slot *fmt));
57 static u_char GetByte __P((nubus_slot *fmt, u_long ptr));
58 #ifdef notyet
59 /* unused */ static u_short GetWord __P((nubus_slot *fmt, u_long ptr));
60 #endif
61 static u_long GetLong __P((nubus_slot *fmt, u_long ptr));
62
63 struct cfattach nubus_ca = {
64 sizeof(struct nubus_softc), nubusmatch, nubusattach
65 };
66
67 struct cfdriver nubus_cd = {
68 NULL, "nubus", DV_DULL, 1
69 };
70
71 static int
72 nubusmatch(parent, vcf, aux)
73 struct device *parent;
74 void *vcf, *aux;
75 {
76 struct confargs *ca = aux;
77
78 if (ca->ca_bustype != BUS_NUBUS)
79 return (0);
80 printf("nubus match.\n");
81 return(1);
82 }
83
84 static void
85 nubusattach(parent, self, aux)
86 struct device *parent, *self;
87 void *aux;
88 {
89 nubus_slot fmtblock;
90 int i;
91
92 printf("\n");
93
94 for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
95 if (probe_slot(i, &fmtblock)) {
96 /*config_search(bus_scan, &fmtblock, nubusprint);*/
97 config_found(self, &fmtblock, nubusprint);
98 }
99 }
100 }
101
102 static int
103 nubusprint(aux, name)
104 void *aux;
105 char *name;
106 {
107 nubus_slot *fmt;
108
109 fmt = (nubus_slot *) aux;
110 if (name) {
111 printf("%s: slot %x: %s ", name, fmt->slot,
112 nubus_get_card_name(fmt));
113 printf("(Vendor: %s, ",
114 nubus_get_vendor(fmt, NUBUS_RSRC_VEND_ID));
115 printf("Part: %s) ",
116 nubus_get_vendor(fmt, NUBUS_RSRC_VEND_PART));
117 }
118 return (UNCONF);
119 }
120
121 /*
122 * Probe a given nubus slot. If a card is there and we can get the
123 * format block from it's clutching decl. ROMs, fill the format block
124 * and return non-zero. If we can't find a card there with a valid
125 * decl. ROM, return 0.
126 *
127 * First, we check to see if we can access the memory at the tail
128 * end of the slot. If so, then we check for a bytelanes byte. We
129 * could probably just return a failure status if we bus error on
130 * the first try, but there really is little reason not to go ahead
131 * and check the other three locations in case there's a wierd card
132 * out there.
133 *
134 * Checking for a card involves locating the "bytelanes" byte which
135 * tells us how to interpret the declaration ROM's data. The format
136 * block is at the top of the card's standard memory space and the
137 * bytelanes byte is at the end of that block.
138 *
139 * After some inspection of the bytelanes byte, it appears that it
140 * takes the form 0xXY where Y is a bitmask of the bytelanes in use
141 * and X is a bitmask of the lanes to ignore. Hence, (X ^ Y) == 0
142 * and (less obviously), Y will have the upper N bits clear if it is
143 * found N bytes from the last possible location. Both that and
144 * the exclusive-or check are made.
145 *
146 * If a valid
147 */
148 static u_char nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
149 static int
150 probe_slot(slot, fmt)
151 int slot;
152 nubus_slot *fmt;
153 {
154 caddr_t rom_probe;
155 vm_offset_t hdr;
156 u_int data;
157 int hdr_size, i;
158
159 fmt->bytelanes = 0;
160 fmt->slot = (u_long)slot;
161
162 rom_probe = (caddr_t) (NUBUS_SLOT_TO_PADDR(fmt->slot) + NBMEMSIZE);
163
164 #ifdef DEBUG
165 if (nubus_debug & NDB_PROBE) {
166 phys = pmap_extract(pmap_kernel(), (vm_offset_t)rom_probe-1);
167 printf("probing slot %d, first probe at 0x%x (phys 0x%x).\n",
168 slot, rom_probe-1, phys);
169 }
170 #endif
171
172 for (i = 4; i && (fmt->bytelanes == 0); i--) {
173
174 rom_probe--;
175
176 data = bus_peek(BUS_NUBUS, (vm_offset_t) rom_probe, 1);
177 if (data == -1)
178 continue;
179
180 if (data == 0)
181 continue;
182
183 if ( ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
184 && ((data & 0x0f) < (1 << i)) ) {
185 fmt->bytelanes = data;
186 fmt->step = nbits[(data & 0x0f)];
187 }
188 }
189 #ifdef DEBUG
190 if (nubus_debug & NDB_PROBE)
191 if (fmt->bytelanes == 0)
192 printf("bytelanes not found for slot 0x%x.\n", slot);
193 #endif
194
195 if (fmt->bytelanes == 0)
196 return 0;
197
198 #ifdef DEBUG
199 if (nubus_debug & NDB_PROBE)
200 printf("bytelanes of 0x%x found for slot 0x%x (base 0x%x).\n",
201 fmt->bytelanes, slot, NUBUS_SLOT_TO_BASE(slot));
202 #endif
203
204 hdr_size = 20;
205
206 /*
207 * Go ahead and attempt to load format header.
208 * First, we need to find the first byte beyond memory that
209 * would be valid. This is necessary for NUBUS_ROM_offset()
210 * to work.
211 */
212 printf("Mapping in space for slot %d.\n", fmt->slot);
213 hdr = (vm_offset_t)
214 bus_mapin(BUS_NUBUS,NUBUS_SLOT_TO_PADDR(fmt->slot),NBMEMSIZE);
215 if (hdr == NULL) {
216 printf("Failed to map %d bytes for NuBUS slot %d (0x%x) probe.\n",
217 NBMEMSIZE, fmt->slot,
218 (unsigned int) NUBUS_SLOT_TO_BASE(fmt->slot));
219 }
220 fmt->virtual_base = hdr;
221 hdr += NBMEMSIZE;
222
223 i = 0x10 | (fmt->bytelanes & 0x0f);
224 while ((i & 1) == 0) {
225 hdr++;
226 i >>= 1;
227 }
228 fmt->top = hdr;
229 hdr = IncPtr(fmt, hdr, -hdr_size);
230 #ifdef DEBUG
231 if (nubus_debug & NDB_PROBE)
232 printf("fmt->top is 0x%x, that minus 0x%x puts us at 0x%x.\n",
233 fmt->top, hdr_size, hdr);
234 #if 0
235 for (i=1 ; i < 8 ; i++) {
236 printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
237 hdr, i, IncPtr(fmt, hdr, -i),
238 i, IncPtr(fmt, hdr, i));
239 }
240 #endif
241 #endif
242
243 fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
244 hdr = IncPtr(fmt, hdr, 4);
245 fmt->length = GetLong(fmt, hdr);
246 hdr = IncPtr(fmt, hdr, 4);
247 fmt->crc = GetLong(fmt, hdr);
248 hdr = IncPtr(fmt, hdr, 4);
249 fmt->revision_level = GetByte(fmt, hdr);
250 hdr = IncPtr(fmt, hdr, 1);
251 fmt->format = GetByte(fmt, hdr);
252 hdr = IncPtr(fmt, hdr, 1);
253 fmt->test_pattern = GetLong(fmt, hdr);
254
255 #if DEBUG
256 if (nubus_debug & NDB_PROBE) {
257 printf("Directory offset 0x%x\t", fmt->directory_offset);
258 printf("Length 0x%x\t", fmt->length);
259 printf("CRC 0x%x\n", fmt->crc);
260 printf("Revision level 0x%x\t", fmt->revision_level);
261 printf("Format 0x%x\t", fmt->format);
262 printf("Test Pattern 0x%x\n", fmt->test_pattern);
263 }
264 #endif
265
266 if ((fmt->directory_offset & 0x00ff0000) == 0) {
267 printf("Invalid looking directory offset (0x%x)!\n",
268 fmt->directory_offset);
269 return 0;
270 }
271 if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
272 printf("Nubus--test pattern invalid:\n");
273 printf(" slot 0x%x, bytelanes 0x%x?\n",
274 fmt->slot, fmt->bytelanes);
275 printf(" read test 0x%x, compare with 0x%x.\n",
276 fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
277 return 0;
278 }
279
280 /* Perform CRC */
281 if (fmt->crc != nubus_calc_CRC(fmt)) {
282 printf("Nubus--crc check failed, slot 0x%x.\n",
283 fmt->slot);
284 return 0;
285 }
286
287 return 1;
288 }
289
290 /*
291 * Compute byte offset on card, taking into account bytelanes.
292 * Base must be on a valid bytelane for this function to work.
293 * Return the new address.
294 *
295 * XXX -- There has GOT to be a better way to do this.
296 */
297 static u_long
298 IncPtr(fmt, base, amt)
299 nubus_slot *fmt;
300 u_long base;
301 long amt;
302 {
303 u_char b, t;
304
305 if (!amt)
306 return base;
307
308 if (amt < 0) {
309 amt = -amt;
310 b = fmt->bytelanes;
311 t = (b << 4);
312 b <<= (3 - (base & 0x3));
313 while (amt) {
314 b <<= 1;
315 if (b == t)
316 b = fmt->bytelanes;
317 if (b & 0x08)
318 amt--;
319 base--;
320 }
321 return base;
322 }
323
324 t = (fmt->bytelanes & 0xf) | 0x10;
325 b = t >> (base & 0x3);
326 while (amt) {
327 b >>= 1;
328 if (b == 1)
329 b = t;
330 if (b & 1)
331 amt--;
332 base++;
333 }
334
335 return base;
336 }
337
338 static u_long
339 nubus_calc_CRC(fmt)
340 nubus_slot *fmt;
341 {
342 #if 0
343 u_long base, ptr, crc_loc, sum;
344 int i;
345
346 base = fmt->top;
347 crc_loc = NUBUS_ROM_offset(fmt, base, -12);
348 ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
349
350 sum = 0;
351 while (ptr < base)
352 roll #1, sum
353 if (ptr == crc_loc) {
354 roll #3, sum
355 ptr = IncPtr(fmt, ptr, 3);
356 } else {
357 sum += GetByte(fmt, ptr);
358 }
359 ptr = IncPtr(fmt, ptr, 1);
360 }
361
362 return sum;
363 #endif
364 return fmt->crc;
365 }
366
367 static u_char
368 GetByte(fmt, ptr)
369 nubus_slot *fmt;
370 u_long ptr;
371 {
372 return *(caddr_t)ptr;
373 }
374
375 #ifdef notyet
376 /* Nothing uses this, yet */
377 static u_short
378 GetWord(fmt, ptr)
379 nubus_slot *fmt;
380 u_long ptr;
381 {
382 u_short s;
383
384 s = (GetByte(fmt, ptr) << 8);
385 ptr = IncPtr(fmt, ptr, 1);
386 s |= GetByte(fmt, ptr);
387 return s;
388 }
389 #endif
390
391 static u_long
392 GetLong(fmt, ptr)
393 nubus_slot *fmt;
394 u_long ptr;
395 {
396 register u_long l;
397 register int i;
398
399 l = 0;
400 for ( i = 0; i < 4; i++) {
401 l = (l << 8) | GetByte(fmt, ptr);
402 ptr = IncPtr(fmt, ptr, 1);
403 }
404 return l;
405 }
406
407 void
408 nubus_get_main_dir(slot, dir_return)
409 nubus_slot *slot;
410 nubus_dir *dir_return;
411 {
412 #if DEBUG
413 if (nubus_debug & NDB_FOLLOW)
414 printf("nubus_get_main_dir(0x%x, 0x%x)\n",
415 (u_int) slot, (u_int) dir_return);
416 #endif
417 dir_return->dirbase = IncPtr(slot, slot->top,
418 slot->directory_offset - 20);
419 dir_return->curr_ent = dir_return->dirbase;
420 }
421
422 int
423 nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
424 nubus_slot *slot;
425 nubus_dir *dir;
426 u_int8_t rsrcid;
427 nubus_dirent *dirent_return;
428 {
429 u_long entry;
430 u_char byte;
431
432 #if DEBUG
433 if (nubus_debug & NDB_FOLLOW)
434 printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
435 (u_int) slot, (u_int) dir, (u_int) rsrcid,
436 (u_int) dirent_return);
437 #endif
438 if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
439 return -1;
440
441 entry = dir->curr_ent;
442 do {
443 byte = GetByte(slot, entry);
444 #if DEBUG
445 if (nubus_debug & NDB_FOLLOW)
446 printf("\tFound rsrc 0x%x.\n", byte);
447 #endif
448 if (byte == rsrcid) {
449 dirent_return->myloc = entry;
450 dirent_return->rsrc_id = rsrcid;
451 entry = GetLong(slot, entry);
452 dirent_return->offset = (entry & 0x00ffffff);
453 return 1;
454 }
455 if (byte == 0xff) {
456 entry = dir->dirbase;
457 } else {
458 entry = IncPtr(slot, entry, 4);
459 }
460 } while (entry != (u_long) dir->curr_ent);
461 return 0;
462 }
463
464 void
465 nubus_get_dir_from_rsrc(slot, dirent, dir_return)
466 nubus_slot *slot;
467 nubus_dirent *dirent;
468 nubus_dir *dir_return;
469 {
470 u_long loc;
471
472 #if DEBUG
473 if (nubus_debug & NDB_FOLLOW)
474 printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
475 (u_int) slot, (u_int) dirent, (u_int) dir_return);
476 #endif
477 if ((loc = dirent->offset) & 0x800000) {
478 loc |= 0xff000000;
479 }
480 dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
481 dir_return->curr_ent = dir_return->dirbase;
482 }
483
484 int
485 nubus_get_ind_data(slot, dirent, data_return, nbytes)
486 nubus_slot *slot;
487 nubus_dirent *dirent;
488 caddr_t data_return;
489 int nbytes;
490 {
491 u_long loc;
492
493 #if DEBUG
494 if (nubus_debug & NDB_FOLLOW)
495 printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
496 (u_int) slot, (u_int) dirent, (u_int) data_return,
497 nbytes);
498 #endif
499 if ((loc = dirent->offset) & 0x800000) {
500 loc |= 0xff000000;
501 }
502 loc = IncPtr(slot, dirent->myloc, loc);
503
504 while (nbytes--) {
505 *data_return++ = GetByte(slot, loc);
506 loc = IncPtr(slot, loc, 1);
507 }
508 return 1;
509 }
510
511 int
512 nubus_get_c_string(slot, dirent, data_return, max_bytes)
513 nubus_slot *slot;
514 nubus_dirent *dirent;
515 caddr_t data_return;
516 int max_bytes;
517 {
518 u_long loc;
519
520 #if DEBUG
521 if (nubus_debug & NDB_FOLLOW)
522 printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
523 (u_int) slot, (u_int) dirent, (u_int) data_return,
524 max_bytes);
525 #endif
526 if ((loc = dirent->offset) & 0x800000) {
527 loc |= 0xff000000;
528 }
529 loc = IncPtr(slot, dirent->myloc, loc);
530
531 *data_return = '\0';
532 while (max_bytes--) {
533 if ((*data_return++ = GetByte(slot, loc)) == 0)
534 return 1;
535 loc = IncPtr(slot, loc, 1);
536 }
537 return 0;
538 }
539
540 static char *huh = "???";
541
542 char *
543 nubus_get_vendor(slot, rsrc)
544 nubus_slot *slot;
545 int rsrc;
546 {
547 static char str_ret[64];
548 nubus_dir dir;
549 nubus_dirent ent;
550
551 #if DEBUG
552 if (nubus_debug & NDB_FOLLOW)
553 printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
554 #endif
555 nubus_get_main_dir(slot, &dir);
556 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
557 return huh;
558 nubus_get_dir_from_rsrc(slot, &ent, &dir);
559
560 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
561 return huh;
562 nubus_get_dir_from_rsrc(slot, &ent, &dir);
563
564 if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
565 return huh;
566
567 nubus_get_c_string(slot, &ent, str_ret, 64);
568
569 return str_ret;
570 }
571
572 char *
573 nubus_get_card_name(slot)
574 nubus_slot *slot;
575 {
576 static char name_ret[64];
577 nubus_dir dir;
578 nubus_dirent ent;
579
580 #if DEBUG
581 if (nubus_debug & NDB_FOLLOW)
582 printf("nubus_get_card_name(0x%x).\n", (u_long) slot);
583 #endif
584 nubus_get_main_dir(slot, &dir);
585
586 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
587 return huh;
588
589 nubus_get_dir_from_rsrc(slot, &ent, &dir);
590
591 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
592 return huh;
593
594 nubus_get_c_string(slot, &ent, name_ret, 64);
595
596 return name_ret;
597 }
598