nubus.c revision 1.20 1 /* $NetBSD: nubus.c,v 1.20 1996/05/06 01:08:41 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.\n",
201 fmt->bytelanes, 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 hdr = (vm_offset_t)
213 bus_mapin(BUS_NUBUS,NUBUS_SLOT_TO_PADDR(fmt->slot),NBMEMSIZE);
214 if (hdr == NULL) {
215 printf("Failed to map %d bytes for NuBUS slot %d (0x%x) probe.\n",
216 NBMEMSIZE, fmt->slot,
217 (unsigned int) NUBUS_SLOT_TO_BASE(fmt->slot));
218 }
219 fmt->virtual_base = hdr;
220 hdr += NBMEMSIZE;
221
222 i = 0x10 | (fmt->bytelanes & 0x0f);
223 while ((i & 1) == 0) {
224 hdr++;
225 i >>= 1;
226 }
227 fmt->top = hdr;
228 hdr = IncPtr(fmt, hdr, -hdr_size);
229 #ifdef DEBUG
230 if (nubus_debug & NDB_PROBE)
231 printf("fmt->top is 0x%x, that minus 0x%x puts us at 0x%x.\n",
232 fmt->top, hdr_size, hdr);
233 #if 0
234 for (i=1 ; i < 8 ; i++) {
235 printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
236 hdr, i, IncPtr(fmt, hdr, -i),
237 i, IncPtr(fmt, hdr, i));
238 }
239 #endif
240 #endif
241
242 fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
243 hdr = IncPtr(fmt, hdr, 4);
244 fmt->length = GetLong(fmt, hdr);
245 hdr = IncPtr(fmt, hdr, 4);
246 fmt->crc = GetLong(fmt, hdr);
247 hdr = IncPtr(fmt, hdr, 4);
248 fmt->revision_level = GetByte(fmt, hdr);
249 hdr = IncPtr(fmt, hdr, 1);
250 fmt->format = GetByte(fmt, hdr);
251 hdr = IncPtr(fmt, hdr, 1);
252 fmt->test_pattern = GetLong(fmt, hdr);
253
254 #if DEBUG
255 if (nubus_debug & NDB_PROBE) {
256 printf("Directory offset 0x%x\t", fmt->directory_offset);
257 printf("Length 0x%x\t", fmt->length);
258 printf("CRC 0x%x\n", fmt->crc);
259 printf("Revision level 0x%x\t", fmt->revision_level);
260 printf("Format 0x%x\t", fmt->format);
261 printf("Test Pattern 0x%x\n", fmt->test_pattern);
262 }
263 #endif
264
265 if ((fmt->directory_offset & 0x00ff0000) == 0) {
266 printf("Invalid looking directory offset (0x%x)!\n",
267 fmt->directory_offset);
268 return 0;
269 }
270 if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
271 printf("Nubus--test pattern invalid:\n");
272 printf(" slot 0x%x, bytelanes 0x%x?\n",
273 fmt->slot, fmt->bytelanes);
274 printf(" read test 0x%x, compare with 0x%x.\n",
275 fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
276 return 0;
277 }
278
279 /* Perform CRC */
280 if (fmt->crc != nubus_calc_CRC(fmt)) {
281 printf("Nubus--crc check failed, slot 0x%x.\n",
282 fmt->slot);
283 return 0;
284 }
285
286 return 1;
287 }
288
289 /*
290 * Compute byte offset on card, taking into account bytelanes.
291 * Base must be on a valid bytelane for this function to work.
292 * Return the new address.
293 *
294 * XXX -- There has GOT to be a better way to do this.
295 */
296 static u_long
297 IncPtr(fmt, base, amt)
298 nubus_slot *fmt;
299 u_long base;
300 long amt;
301 {
302 u_char b, t;
303
304 if (!amt)
305 return base;
306
307 if (amt < 0) {
308 amt = -amt;
309 b = fmt->bytelanes;
310 t = (b << 4);
311 b <<= (3 - (base & 0x3));
312 while (amt) {
313 b <<= 1;
314 if (b == t)
315 b = fmt->bytelanes;
316 if (b & 0x08)
317 amt--;
318 base--;
319 }
320 return base;
321 }
322
323 t = (fmt->bytelanes & 0xf) | 0x10;
324 b = t >> (base & 0x3);
325 while (amt) {
326 b >>= 1;
327 if (b == 1)
328 b = t;
329 if (b & 1)
330 amt--;
331 base++;
332 }
333
334 return base;
335 }
336
337 static u_long
338 nubus_calc_CRC(fmt)
339 nubus_slot *fmt;
340 {
341 #if 0
342 u_long base, ptr, crc_loc, sum;
343 int i;
344
345 base = fmt->top;
346 crc_loc = NUBUS_ROM_offset(fmt, base, -12);
347 ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
348
349 sum = 0;
350 while (ptr < base)
351 roll #1, sum
352 if (ptr == crc_loc) {
353 roll #3, sum
354 ptr = IncPtr(fmt, ptr, 3);
355 } else {
356 sum += GetByte(fmt, ptr);
357 }
358 ptr = IncPtr(fmt, ptr, 1);
359 }
360
361 return sum;
362 #endif
363 return fmt->crc;
364 }
365
366 static u_char
367 GetByte(fmt, ptr)
368 nubus_slot *fmt;
369 u_long ptr;
370 {
371 return *(caddr_t)ptr;
372 }
373
374 #ifdef notyet
375 /* Nothing uses this, yet */
376 static u_short
377 GetWord(fmt, ptr)
378 nubus_slot *fmt;
379 u_long ptr;
380 {
381 u_short s;
382
383 s = (GetByte(fmt, ptr) << 8);
384 ptr = IncPtr(fmt, ptr, 1);
385 s |= GetByte(fmt, ptr);
386 return s;
387 }
388 #endif
389
390 static u_long
391 GetLong(fmt, ptr)
392 nubus_slot *fmt;
393 u_long ptr;
394 {
395 register u_long l;
396 register int i;
397
398 l = 0;
399 for ( i = 0; i < 4; i++) {
400 l = (l << 8) | GetByte(fmt, ptr);
401 ptr = IncPtr(fmt, ptr, 1);
402 }
403 return l;
404 }
405
406 void
407 nubus_get_main_dir(slot, dir_return)
408 nubus_slot *slot;
409 nubus_dir *dir_return;
410 {
411 #if DEBUG
412 if (nubus_debug & NDB_FOLLOW)
413 printf("nubus_get_main_dir(0x%x, 0x%x)\n",
414 (u_int) slot, (u_int) dir_return);
415 #endif
416 dir_return->dirbase = IncPtr(slot, slot->top,
417 slot->directory_offset - 20);
418 dir_return->curr_ent = dir_return->dirbase;
419 }
420
421 int
422 nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
423 nubus_slot *slot;
424 nubus_dir *dir;
425 u_int8_t rsrcid;
426 nubus_dirent *dirent_return;
427 {
428 u_long entry;
429 u_char byte;
430
431 #if DEBUG
432 if (nubus_debug & NDB_FOLLOW)
433 printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
434 (u_int) slot, (u_int) dir, (u_int) rsrcid,
435 (u_int) dirent_return);
436 #endif
437 if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
438 return -1;
439
440 entry = dir->curr_ent;
441 do {
442 byte = GetByte(slot, entry);
443 #if DEBUG
444 if (nubus_debug & NDB_FOLLOW)
445 printf("\tFound rsrc 0x%x.\n", byte);
446 #endif
447 if (byte == rsrcid) {
448 dirent_return->myloc = entry;
449 dirent_return->rsrc_id = rsrcid;
450 entry = GetLong(slot, entry);
451 dirent_return->offset = (entry & 0x00ffffff);
452 return 1;
453 }
454 if (byte == 0xff) {
455 entry = dir->dirbase;
456 } else {
457 entry = IncPtr(slot, entry, 4);
458 }
459 } while (entry != (u_long) dir->curr_ent);
460 return 0;
461 }
462
463 void
464 nubus_get_dir_from_rsrc(slot, dirent, dir_return)
465 nubus_slot *slot;
466 nubus_dirent *dirent;
467 nubus_dir *dir_return;
468 {
469 u_long loc;
470
471 #if DEBUG
472 if (nubus_debug & NDB_FOLLOW)
473 printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
474 (u_int) slot, (u_int) dirent, (u_int) dir_return);
475 #endif
476 if ((loc = dirent->offset) & 0x800000) {
477 loc |= 0xff000000;
478 }
479 dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
480 dir_return->curr_ent = dir_return->dirbase;
481 }
482
483 int
484 nubus_get_ind_data(slot, dirent, data_return, nbytes)
485 nubus_slot *slot;
486 nubus_dirent *dirent;
487 caddr_t data_return;
488 int nbytes;
489 {
490 u_long loc;
491
492 #if DEBUG
493 if (nubus_debug & NDB_FOLLOW)
494 printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
495 (u_int) slot, (u_int) dirent, (u_int) data_return,
496 nbytes);
497 #endif
498 if ((loc = dirent->offset) & 0x800000) {
499 loc |= 0xff000000;
500 }
501 loc = IncPtr(slot, dirent->myloc, loc);
502
503 while (nbytes--) {
504 *data_return++ = GetByte(slot, loc);
505 loc = IncPtr(slot, loc, 1);
506 }
507 return 1;
508 }
509
510 int
511 nubus_get_c_string(slot, dirent, data_return, max_bytes)
512 nubus_slot *slot;
513 nubus_dirent *dirent;
514 caddr_t data_return;
515 int max_bytes;
516 {
517 u_long loc;
518
519 #if DEBUG
520 if (nubus_debug & NDB_FOLLOW)
521 printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
522 (u_int) slot, (u_int) dirent, (u_int) data_return,
523 max_bytes);
524 #endif
525 if ((loc = dirent->offset) & 0x800000) {
526 loc |= 0xff000000;
527 }
528 loc = IncPtr(slot, dirent->myloc, loc);
529
530 *data_return = '\0';
531 while (max_bytes--) {
532 if ((*data_return++ = GetByte(slot, loc)) == 0)
533 return 1;
534 loc = IncPtr(slot, loc, 1);
535 }
536 return 0;
537 }
538
539 static char *huh = "???";
540
541 char *
542 nubus_get_vendor(slot, rsrc)
543 nubus_slot *slot;
544 int rsrc;
545 {
546 static char str_ret[64];
547 nubus_dir dir;
548 nubus_dirent ent;
549
550 #if DEBUG
551 if (nubus_debug & NDB_FOLLOW)
552 printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
553 #endif
554 nubus_get_main_dir(slot, &dir);
555 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
556 return huh;
557 nubus_get_dir_from_rsrc(slot, &ent, &dir);
558
559 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
560 return huh;
561 nubus_get_dir_from_rsrc(slot, &ent, &dir);
562
563 if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
564 return huh;
565
566 nubus_get_c_string(slot, &ent, str_ret, 64);
567
568 return str_ret;
569 }
570
571 char *
572 nubus_get_card_name(slot)
573 nubus_slot *slot;
574 {
575 static char name_ret[64];
576 nubus_dir dir;
577 nubus_dirent ent;
578
579 #if DEBUG
580 if (nubus_debug & NDB_FOLLOW)
581 printf("nubus_get_card_name(0x%x).\n", (u_long) slot);
582 #endif
583 nubus_get_main_dir(slot, &dir);
584
585 if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
586 return huh;
587
588 nubus_get_dir_from_rsrc(slot, &ent, &dir);
589
590 if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
591 return huh;
592
593 nubus_get_c_string(slot, &ent, name_ret, 64);
594
595 return name_ret;
596 }
597