Import 2.4.0-test6pre3
[davej-history.git] / drivers / nubus / nubus.c
blob47b3d88fefcf5a6998265c85e39bce05dc1f878e
1 /*
2 * Macintosh Nubus Interface Code
4 * Originally by Alan Cox
6 * Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
7 * and others.
8 */
10 #include <linux/config.h>
11 #include <linux/ptrace.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/nubus.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <asm/setup.h>
20 #include <asm/system.h>
21 #include <asm/page.h>
22 #include <asm/hwtest.h>
23 #include <linux/proc_fs.h>
24 #include <asm/mac_via.h>
25 #include <asm/mac_oss.h>
27 extern void via_nubus_init(void);
28 extern void oss_nubus_init(void);
30 extern int console_loglevel;
32 /* Constants */
34 /* This is, of course, the size in bytelanes, rather than the size in
35 actual bytes */
36 #define FORMAT_BLOCK_SIZE 20
37 #define ROM_DIR_OFFSET 0x24
39 #define NUBUS_TEST_PATTERN 0x5A932BC7
41 /* Define this if you like to live dangerously - it is known not to
42 work on pretty much every machine except the Quadra 630 and the LC
43 III. */
44 #undef I_WANT_TO_PROBE_SLOT_ZERO
46 /* This sometimes helps combat failure to boot */
47 #undef TRY_TO_DODGE_WSOD
49 /* Globals */
51 struct nubus_dev* nubus_devices;
52 struct nubus_board* nubus_boards;
54 /* Meaning of "bytelanes":
56 The card ROM may appear on any or all bytes of each long word in
57 NuBus memory. The low 4 bits of the "map" value found in the
58 format block (at the top of the slot address space, as well as at
59 the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
60 offsets within each longword, are valid. Thus:
62 A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
63 are valid.
65 A map of 0xf0 means that no bytelanes are valid (We pray that we
66 will never encounter this, but stranger things have happened)
68 A map of 0xe1 means that only the MSB of each long word is actually
69 part of the card ROM. (We hope to never encounter NuBus on a
70 little-endian machine. Again, stranger things have happened)
72 A map of 0x78 means that only the LSB of each long word is valid.
74 Etcetera, etcetera. Hopefully this clears up some confusion over
75 what the following code actually does. */
77 extern inline int not_useful(void *p, int map)
79 unsigned long pv=(unsigned long)p;
80 pv &= 3;
81 if(map & (1<<pv))
82 return 0;
83 return 1;
86 static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
88 /* This will hold the result */
89 unsigned long v = 0;
90 unsigned char *p = *ptr;
92 while(len)
94 v <<= 8;
95 while(not_useful(p,map))
96 p++;
97 v |= *p++;
98 len--;
100 *ptr = p;
101 return v;
104 static void nubus_rewind(unsigned char **ptr, int len, int map)
106 unsigned char *p=*ptr;
108 /* Sanity check */
109 if(len > 65536)
110 printk(KERN_ERR "rewind of 0x%08x!\n", len);
111 while(len)
115 p--;
117 while(not_useful(p, map));
118 len--;
120 *ptr=p;
123 static void nubus_advance(unsigned char **ptr, int len, int map)
125 unsigned char *p = *ptr;
126 if(len>65536)
127 printk(KERN_ERR "advance of 0x%08x!\n", len);
128 while(len)
130 while(not_useful(p,map))
131 p++;
132 p++;
133 len--;
135 *ptr = p;
138 static void nubus_move(unsigned char **ptr, int len, int map)
140 if(len > 0)
141 nubus_advance(ptr, len, map);
142 else if(len < 0)
143 nubus_rewind(ptr, -len, map);
146 /* Now, functions to read the sResource tree */
148 /* Each sResource entry consists of a 1-byte ID and a 3-byte data
149 field. If that data field contains an offset, then obviously we
150 have to expand it from a 24-bit signed number to a 32-bit signed
151 number. */
153 extern inline long nubus_expand32(long foo)
155 if(foo & 0x00800000) /* 24bit negative */
156 foo |= 0xFF000000;
157 return foo;
160 extern inline void *nubus_rom_addr(int slot)
163 * Returns the first byte after the card. We then walk
164 * backwards to get the lane register and the config
166 return (void *)(0xF1000000+(slot<<24));
169 static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
171 unsigned char *p = nd->base;
172 /* Essentially, just step over the bytelanes using whatever
173 offset we might have found */
174 nubus_move(&p, nubus_expand32(nd->data), nd->mask);
175 /* And return the value */
176 return p;
179 /* These two are for pulling resource data blocks (i.e. stuff that's
180 pointed to with offsets) out of the card ROM. */
182 void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent,
183 int len)
185 unsigned char *t = (unsigned char *)dest;
186 unsigned char *p = nubus_dirptr(dirent);
187 while(len)
189 *t++ = nubus_get_rom(&p, 1, dirent->mask);
190 len--;
194 void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
195 int len)
197 unsigned char *t=(unsigned char *)dest;
198 unsigned char *p = nubus_dirptr(dirent);
199 while(len)
201 *t = nubus_get_rom(&p, 1, dirent->mask);
202 if(!*t++)
203 break;
204 len--;
208 int nubus_get_root_dir(const struct nubus_board* board,
209 struct nubus_dir* dir)
211 dir->ptr = dir->base = board->directory;
212 dir->done = 0;
213 dir->mask = board->lanes;
214 return 0;
217 /* This is a slyly renamed version of the above */
218 int nubus_get_func_dir(const struct nubus_dev* dev,
219 struct nubus_dir* dir)
221 dir->ptr = dir->base = dev->directory;
222 dir->done = 0;
223 dir->mask = dev->board->lanes;
224 return 0;
227 int nubus_get_board_dir(const struct nubus_board* board,
228 struct nubus_dir* dir)
230 struct nubus_dirent ent;
232 dir->ptr = dir->base = board->directory;
233 dir->done = 0;
234 dir->mask = board->lanes;
236 /* Now dereference it (the first directory is always the board
237 directory) */
238 if (nubus_readdir(dir, &ent) == -1)
239 return -1;
240 if (nubus_get_subdir(&ent, dir) == -1)
241 return -1;
242 return 0;
245 int nubus_get_subdir(const struct nubus_dirent *ent,
246 struct nubus_dir *dir)
248 dir->ptr = dir->base = nubus_dirptr(ent);
249 dir->done = 0;
250 dir->mask = ent->mask;
251 return 0;
254 int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
256 u32 resid;
257 if (nd->done)
258 return -1;
260 /* Do this first, otherwise nubus_rewind & co are off by 4 */
261 ent->base = nd->ptr;
263 /* This moves nd->ptr forward */
264 resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
266 /* EOL marker, as per the Apple docs */
267 if((resid&0xff000000) == 0xff000000)
269 /* Mark it as done */
270 nd->done = 1;
271 return -1;
274 /* First byte is the resource ID */
275 ent->type = resid >> 24;
276 /* Low 3 bytes might contain data (or might not) */
277 ent->data = resid & 0xffffff;
278 ent->mask = nd->mask;
279 return 0;
282 int nubus_rewinddir(struct nubus_dir* dir)
284 dir->ptr = dir->base;
285 return 0;
288 /* Driver interface functions, more or less like in pci.c */
290 struct nubus_dev*
291 nubus_find_device(unsigned short category,
292 unsigned short type,
293 unsigned short dr_hw,
294 unsigned short dr_sw,
295 const struct nubus_dev* from)
297 struct nubus_dev* itor =
298 from ? from->next : nubus_devices;
300 while (itor) {
301 if (itor->category == category
302 && itor->type == type
303 && itor->dr_hw == dr_hw
304 && itor->dr_sw == dr_sw)
305 return itor;
306 itor = itor->next;
308 return NULL;
311 struct nubus_dev*
312 nubus_find_type(unsigned short category,
313 unsigned short type,
314 const struct nubus_dev* from)
316 struct nubus_dev* itor =
317 from ? from->next : nubus_devices;
319 while (itor) {
320 if (itor->category == category
321 && itor->type == type)
322 return itor;
323 itor = itor->next;
325 return NULL;
328 struct nubus_dev*
329 nubus_find_slot(unsigned int slot,
330 const struct nubus_dev* from)
332 struct nubus_dev* itor =
333 from ? from->next : nubus_devices;
335 while (itor) {
336 if (itor->board->slot == slot)
337 return itor;
338 itor = itor->next;
340 return NULL;
344 nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
345 struct nubus_dirent* ent)
347 while (nubus_readdir(dir, ent) != -1) {
348 if (ent->type == rsrc_type)
349 return 0;
351 return -1;
354 /* Initialization functions - decide which slots contain stuff worth
355 looking at, and print out lots and lots of information from the
356 resource blocks. */
358 /* FIXME: A lot of this stuff will eventually be useful after
359 initializaton, for intelligently probing Ethernet and video chips,
360 among other things. The rest of it should go in the /proc code.
361 For now, we just use it to give verbose boot logs. */
363 static int __init nubus_show_display_resource(struct nubus_dev* dev,
364 const struct nubus_dirent* ent)
366 switch (ent->type) {
367 case NUBUS_RESID_GAMMADIR:
368 printk(KERN_INFO " gamma directory offset: 0x%06x\n", ent->data);
369 break;
370 case 0x0080 ... 0x0085:
371 printk(KERN_INFO " mode %02X info offset: 0x%06x\n",
372 ent->type, ent->data);
373 break;
374 default:
375 printk(KERN_INFO " unknown resource %02X, data 0x%06x\n",
376 ent->type, ent->data);
378 return 0;
381 static int __init nubus_show_network_resource(struct nubus_dev* dev,
382 const struct nubus_dirent* ent)
384 switch (ent->type) {
385 case NUBUS_RESID_MAC_ADDRESS:
387 char addr[6];
388 int i;
390 nubus_get_rsrc_mem(addr, ent, 6);
391 printk(KERN_INFO " MAC address: ");
392 for (i = 0; i < 6; i++)
393 printk("%02x%s", addr[i] & 0xff,
394 i == 5 ? "" : ":");
395 printk("\n");
396 break;
398 default:
399 printk(KERN_INFO " unknown resource %02X, data 0x%06x\n",
400 ent->type, ent->data);
402 return 0;
405 static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
406 const struct nubus_dirent* ent)
408 switch (ent->type) {
409 case NUBUS_RESID_MEMINFO:
411 unsigned long meminfo[2];
412 nubus_get_rsrc_mem(&meminfo, ent, 8);
413 printk(KERN_INFO " memory: [ 0x%08lx 0x%08lx ]\n",
414 meminfo[0], meminfo[1]);
415 break;
417 case NUBUS_RESID_ROMINFO:
419 unsigned long rominfo[2];
420 nubus_get_rsrc_mem(&rominfo, ent, 8);
421 printk(KERN_INFO " ROM: [ 0x%08lx 0x%08lx ]\n",
422 rominfo[0], rominfo[1]);
423 break;
425 default:
426 printk(KERN_INFO " unknown resource %02X, data 0x%06x\n",
427 ent->type, ent->data);
429 return 0;
432 static int __init nubus_show_private_resource(struct nubus_dev* dev,
433 const struct nubus_dirent* ent)
435 switch (dev->category) {
436 case NUBUS_CAT_DISPLAY:
437 nubus_show_display_resource(dev, ent);
438 break;
439 case NUBUS_CAT_NETWORK:
440 nubus_show_network_resource(dev, ent);
441 break;
442 case NUBUS_CAT_CPU:
443 nubus_show_cpu_resource(dev, ent);
444 break;
445 default:
446 printk(KERN_INFO " unknown resource %02X, data 0x%06x\n",
447 ent->type, ent->data);
449 return 0;
452 static struct nubus_dev* __init
453 nubus_get_functional_resource(struct nubus_board* board,
454 int slot,
455 const struct nubus_dirent* parent)
457 struct nubus_dir dir;
458 struct nubus_dirent ent;
459 struct nubus_dev* dev;
461 printk(KERN_INFO " Function 0x%02x:\n", parent->type);
462 nubus_get_subdir(parent, &dir);
464 /* Apple seems to have botched the ROM on the IIx */
465 if (slot == 0 && (unsigned long)dir.base % 2)
466 dir.base += 1;
468 if (console_loglevel >= 10)
469 printk(KERN_DEBUG "nubus_get_functional_resource: parent is 0x%p, dir is 0x%p\n",
470 parent->base, dir.base);
472 /* Actually we should probably panic if this fails */
473 if ((dev = kmalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
474 return NULL;
475 memset(dev, 0, sizeof(*dev));
476 dev->resid = parent->type;
477 dev->directory = dir.base;
478 dev->board = board;
480 while (nubus_readdir(&dir, &ent) != -1)
482 switch(ent.type)
484 case NUBUS_RESID_TYPE:
486 unsigned short nbtdata[4];
487 nubus_get_rsrc_mem(nbtdata, &ent, 8);
488 dev->category = nbtdata[0];
489 dev->type = nbtdata[1];
490 dev->dr_sw = nbtdata[2];
491 dev->dr_hw = nbtdata[3];
492 printk(KERN_INFO " type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
493 nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
494 break;
496 case NUBUS_RESID_NAME:
498 nubus_get_rsrc_str(dev->name, &ent, 64);
499 printk(KERN_INFO " name: %s\n", dev->name);
500 break;
502 case NUBUS_RESID_DRVRDIR:
504 /* MacOS driver. If we were NetBSD we might
505 use this :-) */
506 struct nubus_dir drvr_dir;
507 struct nubus_dirent drvr_ent;
508 nubus_get_subdir(&ent, &drvr_dir);
509 nubus_readdir(&drvr_dir, &drvr_ent);
510 dev->driver = nubus_dirptr(&drvr_ent);
511 printk(KERN_INFO " driver at: 0x%p\n",
512 dev->driver);
513 break;
515 case NUBUS_RESID_MINOR_BASEOS:
516 /* We will need this in order to support
517 multiple framebuffers. It might be handy
518 for Ethernet as well */
519 nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
520 printk(KERN_INFO " memory offset: 0x%08lx\n",
521 dev->iobase);
522 break;
523 case NUBUS_RESID_MINOR_LENGTH:
524 /* Ditto */
525 nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
526 printk(KERN_INFO " memory length: 0x%08lx\n",
527 dev->iosize);
528 break;
529 case NUBUS_RESID_FLAGS:
530 dev->flags = ent.data;
531 printk(KERN_INFO " flags: 0x%06x\n", dev->flags);
532 break;
533 case NUBUS_RESID_HWDEVID:
534 dev->hwdevid = ent.data;
535 printk(KERN_INFO " hwdevid: 0x%06x\n", dev->hwdevid);
536 break;
537 default:
538 /* Local/Private resources have their own
539 function */
540 nubus_show_private_resource(dev, &ent);
544 return dev;
547 /* This is cool. */
548 static int __init nubus_get_vidnames(struct nubus_board* board,
549 const struct nubus_dirent* parent)
551 struct nubus_dir dir;
552 struct nubus_dirent ent;
553 /* FIXME: obviously we want to put this in a header file soon */
554 struct vidmode {
555 u32 size;
556 /* Don't know what this is yet */
557 u16 id;
558 /* Longest one I've seen so far is 26 characters */
559 char name[32];
562 printk(KERN_INFO " video modes supported:\n");
563 nubus_get_subdir(parent, &dir);
564 if (console_loglevel >= 10)
565 printk(KERN_DEBUG "nubus_get_vidnames: parent is 0x%p, dir is 0x%p\n",
566 parent->base, dir.base);
568 while(nubus_readdir(&dir, &ent) != -1)
570 struct vidmode mode;
571 u32 size;
573 /* First get the length */
574 nubus_get_rsrc_mem(&size, &ent, 4);
576 /* Now clobber the whole thing */
577 if (size > sizeof(mode) - 1)
578 size = sizeof(mode) - 1;
579 memset(&mode, sizeof(mode), 0);
580 nubus_get_rsrc_mem(&mode, &ent, size);
581 printk (KERN_INFO " %02X: (%02X) %s\n", ent.type,
582 mode.id, mode.name);
584 return 0;
587 /* This is *really* cool. */
588 static int __init nubus_get_icon(struct nubus_board* board,
589 const struct nubus_dirent* ent)
591 /* Should be 32x32 if my memory serves me correctly */
592 unsigned char icon[128];
593 int x, y;
595 nubus_get_rsrc_mem(&icon, ent, 128);
596 printk(KERN_INFO " icon:\n");
598 /* We should actually plot these somewhere in the framebuffer
599 init. This is just to demonstrate that they do, in fact,
600 exist */
601 for (y = 0; y < 32; y++) {
602 printk(KERN_INFO " ");
603 for (x = 0; x < 32; x++) {
604 if (icon[y*4 + x/8]
605 & (0x80 >> (x%8)))
606 printk("*");
607 else
608 printk(" ");
610 printk("\n");
612 return 0;
615 static int __init nubus_get_vendorinfo(struct nubus_board* board,
616 const struct nubus_dirent* parent)
618 struct nubus_dir dir;
619 struct nubus_dirent ent;
620 static char* vendor_fields[6] = {"ID", "serial", "revision",
621 "part", "date", "unknown field"};
623 printk(KERN_INFO " vendor info:\n");
624 nubus_get_subdir(parent, &dir);
625 if (console_loglevel >= 10)
626 printk(KERN_DEBUG "nubus_get_vendorinfo: parent is 0x%p, dir is 0x%p\n",
627 parent->base, dir.base);
629 while(nubus_readdir(&dir, &ent) != -1)
631 char name[64];
633 /* These are all strings, we think */
634 nubus_get_rsrc_str(name, &ent, 64);
635 if (ent.type > 5)
636 ent.type = 5;
637 printk(KERN_INFO " %s: %s\n",
638 vendor_fields[ent.type-1], name);
640 return 0;
643 static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
644 const struct nubus_dirent* parent)
646 struct nubus_dir dir;
647 struct nubus_dirent ent;
649 nubus_get_subdir(parent, &dir);
650 if (console_loglevel >= 10)
651 printk(KERN_DEBUG "nubus_get_board_resource: parent is 0x%p, dir is 0x%p\n",
652 parent->base, dir.base);
654 while(nubus_readdir(&dir, &ent) != -1)
656 switch (ent.type) {
657 case NUBUS_RESID_TYPE:
659 unsigned short nbtdata[4];
660 /* This type is always the same, and is not
661 useful except insofar as it tells us that
662 we really are looking at a board resource. */
663 nubus_get_rsrc_mem(nbtdata, &ent, 8);
664 printk(KERN_INFO " type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
665 nbtdata[0], nbtdata[1], nbtdata[2],
666 nbtdata[3]);
667 if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
668 nbtdata[2] != 0 || nbtdata[3] != 0)
669 printk(KERN_ERR "this sResource is not a board resource!\n");
670 break;
672 case NUBUS_RESID_NAME:
673 nubus_get_rsrc_str(board->name, &ent, 64);
674 printk(KERN_INFO " name: %s\n", board->name);
675 break;
676 case NUBUS_RESID_ICON:
677 nubus_get_icon(board, &ent);
678 break;
679 case NUBUS_RESID_BOARDID:
680 printk(KERN_INFO " board id: 0x%x\n", ent.data);
681 break;
682 case NUBUS_RESID_PRIMARYINIT:
683 printk(KERN_INFO " primary init offset: 0x%06x\n", ent.data);
684 break;
685 case NUBUS_RESID_VENDORINFO:
686 nubus_get_vendorinfo(board, &ent);
687 break;
688 case NUBUS_RESID_FLAGS:
689 printk(KERN_INFO " flags: 0x%06x\n", ent.data);
690 break;
691 case NUBUS_RESID_HWDEVID:
692 printk(KERN_INFO " hwdevid: 0x%06x\n", ent.data);
693 break;
694 case NUBUS_RESID_SECONDINIT:
695 printk(KERN_INFO " secondary init offset: 0x%06x\n", ent.data);
696 break;
697 /* WTF isn't this in the functional resources? */
698 case NUBUS_RESID_VIDNAMES:
699 nubus_get_vidnames(board, &ent);
700 break;
701 /* Same goes for this */
702 case NUBUS_RESID_VIDMODES:
703 printk(KERN_INFO " video mode parameter directory offset: 0x%06x\n",
704 ent.data);
705 break;
706 default:
707 printk(KERN_INFO " unknown resource %02X, data 0x%06x\n",
708 ent.type, ent.data);
711 return 0;
714 /* Attempt to bypass the somewhat non-obvious arrangement of
715 sResources in the motherboard ROM */
716 static void __init nubus_find_rom_dir(struct nubus_board* board)
718 unsigned char* rp;
719 unsigned char* romdir;
720 struct nubus_dir dir;
721 struct nubus_dirent ent;
723 /* Check for the extra directory just under the format block */
724 rp = board->fblock;
725 nubus_rewind(&rp, 4, board->lanes);
726 if (nubus_get_rom(&rp, 4, board->lanes) != NUBUS_TEST_PATTERN) {
727 /* OK, the ROM was telling the truth */
728 board->directory = board->fblock;
729 nubus_move(&board->directory,
730 nubus_expand32(board->doffset),
731 board->lanes);
732 return;
735 /* On "slot zero", you have to walk down a few more
736 directories to get to the equivalent of a real card's root
737 directory. We don't know what they were smoking when they
738 came up with this. */
739 romdir = nubus_rom_addr(board->slot);
740 nubus_rewind(&romdir, ROM_DIR_OFFSET, board->lanes);
741 dir.base = dir.ptr = romdir;
742 dir.done = 0;
743 dir.mask = board->lanes;
745 /* This one points to an "Unknown Macintosh" directory */
746 if (nubus_readdir(&dir, &ent) == -1)
747 goto badrom;
749 if (console_loglevel >= 10)
750 printk(KERN_INFO "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
751 /* This one takes us to where we want to go. */
752 if (nubus_readdir(&dir, &ent) == -1)
753 goto badrom;
754 if (console_loglevel >= 10)
755 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
756 nubus_get_subdir(&ent, &dir);
758 /* Resource ID 01, also an "Unknown Macintosh" */
759 if (nubus_readdir(&dir, &ent) == -1)
760 goto badrom;
761 if (console_loglevel >= 10)
762 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
764 /* FIXME: the first one is *not* always the right one. We
765 suspect this has something to do with the ROM revision.
766 "The HORROR ROM" (LC-series) uses 0x7e, while "The HORROR
767 Continues" (Q630) uses 0x7b. The DAFB Macs evidently use
768 something else. Please run "Slots" on your Mac (see
769 include/linux/nubus.h for where to get this program) and
770 tell us where the 'SiDirPtr' for Slot 0 is. If you feel
771 brave, you should also use MacsBug to walk down the ROM
772 directories like this function does and try to find the
773 path to that address... */
774 if (nubus_readdir(&dir, &ent) == -1)
775 goto badrom;
776 if (console_loglevel >= 10)
777 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
779 /* Bwahahahaha... */
780 nubus_get_subdir(&ent, &dir);
781 board->directory = dir.base;
782 return;
784 /* Even more evil laughter... */
785 badrom:
786 board->directory = board->fblock;
787 nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
788 printk(KERN_ERR "nubus_get_rom_dir: ROM weirdness! Notify the developers...\n");
791 /* Add a board (might be many devices) to the list */
792 static struct nubus_board* __init nubus_add_board(int slot, int bytelanes)
794 struct nubus_board* board;
795 struct nubus_board** boardp;
797 unsigned char *rp;
798 unsigned long dpat;
799 struct nubus_dir dir;
800 struct nubus_dirent ent;
802 /* Move to the start of the format block */
803 rp = nubus_rom_addr(slot);
804 nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
806 /* Actually we should probably panic if this fails */
807 if ((board = kmalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
808 return NULL;
809 memset(board, 0, sizeof(*board));
810 board->fblock = rp;
812 /* Dump the format block for debugging purposes */
813 if (console_loglevel >= 10) {
814 int i;
815 printk(KERN_DEBUG "Slot %X, format block at 0x%p\n",
816 slot, rp);
817 printk(KERN_DEBUG "Format block: ");
818 for (i = 0; i < FORMAT_BLOCK_SIZE; i += 4) {
819 unsigned short foo, bar;
820 foo = nubus_get_rom(&rp, 2, bytelanes);
821 bar = nubus_get_rom(&rp, 2, bytelanes);
822 printk("%04x %04x ", foo, bar);
824 printk("\n");
825 rp = board->fblock;
828 board->slot = slot;
829 board->slot_addr = (unsigned long) nubus_slot_addr(slot);
830 board->doffset = nubus_get_rom(&rp, 4, bytelanes);
831 /* rom_length is *supposed* to be the total length of the
832 * ROM. In practice it is the "amount of ROM used to compute
833 * the CRC." So some jokers decide to set it to zero and
834 * set the crc to zero so they don't have to do any math.
835 * See the Performa 460 ROM, for example. Those Apple "engineers".
837 board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
838 board->crc = nubus_get_rom(&rp, 4, bytelanes);
839 board->rev = nubus_get_rom(&rp, 1, bytelanes);
840 board->format = nubus_get_rom(&rp,1, bytelanes);
841 board->lanes = bytelanes;
843 /* Directory offset should be small and negative... */
844 if(!(board->doffset & 0x00FF0000))
845 printk(KERN_WARNING "Dodgy doffset!\n");
846 dpat = nubus_get_rom(&rp, 4, bytelanes);
847 if(dpat != NUBUS_TEST_PATTERN)
848 printk(KERN_WARNING "Wrong test pattern %08lx!\n", dpat);
851 * I wonder how the CRC is meant to work -
852 * any takers ?
853 * CSA: According to MAC docs, not all cards pass the CRC anyway,
854 * since the initial Macintosh ROM releases skipped the check.
857 /* Attempt to work around slot zero weirdness */
858 nubus_find_rom_dir(board);
859 nubus_get_root_dir(board, &dir);
861 /* We're ready to rock */
862 printk(KERN_INFO "Slot %X:\n", slot);
864 /* Each slot should have one board resource and any number of
865 functional resources. So we'll fill in some fields in the
866 struct nubus_board from the board resource, then walk down
867 the list of functional resources, spinning out a nubus_dev
868 for each of them. */
869 if (nubus_readdir(&dir, &ent) == -1) {
870 /* We can't have this! */
871 printk(KERN_ERR "Board resource not found!\n");
872 return NULL;
873 } else {
874 printk(KERN_INFO " Board resource:\n");
875 nubus_get_board_resource(board, slot, &ent);
878 /* Aaaarrrrgghh! The LC III motherboard has *two* board
879 resources. I have no idea WTF to do about this. */
881 while (nubus_readdir(&dir, &ent) != -1) {
882 struct nubus_dev* dev;
883 struct nubus_dev** devp;
884 dev = nubus_get_functional_resource(board, slot, &ent);
885 if (dev == NULL)
886 continue;
888 /* We zeroed this out above */
889 if (board->first_dev == NULL)
890 board->first_dev = dev;
892 /* Put it on the global NuBus device chain. Keep entries in order. */
893 for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
894 /* spin */;
895 *devp = dev;
896 dev->next = NULL;
899 /* Put it on the global NuBus board chain. Keep entries in order. */
900 for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
901 /* spin */;
902 *boardp = board;
903 board->next = NULL;
905 return board;
908 void __init nubus_probe_slot(int slot)
910 unsigned char dp;
911 unsigned char* rp;
912 int i;
914 rp = nubus_rom_addr(slot);
915 for(i = 4; i; i--)
917 unsigned long flags;
918 int card_present;
920 rp--;
921 save_flags(flags);
922 cli();
923 card_present = hwreg_present(rp);
924 restore_flags(flags);
926 if (!card_present)
927 continue;
929 printk(KERN_DEBUG "Now probing slot %X at %p\n", slot, rp);
930 dp = *rp;
931 if(dp == 0)
932 continue;
934 /* The last byte of the format block consists of two
935 nybbles which are "mirror images" of each other.
936 These show us the valid bytelanes */
937 if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
938 continue;
939 /* Check that this value is actually *on* one of the
940 bytelanes it claims are valid! */
941 if ((dp & 0x0F) >= (1<<i))
942 continue;
944 /* Looks promising. Let's put it on the list. */
945 nubus_add_board(slot, dp);
947 return;
951 #if defined(CONFIG_PROC_FS)
953 /* /proc/nubus stuff */
955 static int sprint_nubus_board(struct nubus_board* board, char* ptr, int len)
957 if(len < 100)
958 return -1;
960 sprintf(ptr, "Slot %X: %s\n",
961 board->slot, board->name);
963 return strlen(ptr);
966 static int nubus_read_proc(char *buf, char **start, off_t off,
967 int count, int *eof, void *data)
969 int nprinted, len, begin = 0;
970 int slot;
972 len = sprintf(buf, "Nubus devices found:\n");
973 /* Walk the list of NuBus boards */
974 for (board = nubus_boards; board != NULL; board = board->next)
976 nprinted = sprint_nubus_board(board, buf + len, size - len);
977 if (nprinted < 0)
978 break;
979 len += nprinted;
980 if (len+begin < off) {
981 begin += len;
982 len = 0;
984 if (len+begin >= off+count)
985 break;
987 if (slot==16 || len+begin < off)
988 *eof = 1;
989 off -= begin;
990 *strat = buf + off;
991 len -= off;
992 if (len>count)
993 len = count;
994 if (len<0)
995 len = 0;
996 return len;
998 #endif
1000 void __init nubus_scan_bus(void)
1002 int slot;
1003 /* This might not work on your machine */
1004 #ifdef I_WANT_TO_PROBE_SLOT_ZERO
1005 nubus_probe_slot(0);
1006 #endif
1007 for(slot = 9; slot < 15; slot++)
1009 nubus_probe_slot(slot);
1013 void __init nubus_init(void)
1015 if (!MACH_IS_MAC)
1016 return;
1018 /* Initialize the NuBus interrupts */
1019 if (oss_present) {
1020 oss_nubus_init();
1021 } else {
1022 via_nubus_init();
1025 #ifdef TRY_TO_DODGE_WSOD
1026 /* Rogue Ethernet interrupts can kill the machine if we don't
1027 do this. Obviously this is bogus. Hopefully the local VIA
1028 gurus can fix the real cause of the problem. */
1029 mdelay(1000);
1030 #endif
1032 /* And probe */
1033 printk("NuBus: Scanning NuBus slots.\n");
1034 nubus_devices = NULL;
1035 nubus_boards = NULL;
1036 nubus_scan_bus();
1038 #ifdef CONFIG_PROC_FS
1039 create_proc_read_entry("nubus", 0, NULL, nubus_read_proc, NULL);
1040 nubus_proc_init();
1041 #endif