nubus: Clean up printk calls
[linux-2.6/btrfs-unstable.git] / drivers / nubus / nubus.c
blob56de633f19b98c505db41505d0e3df37d310467b
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/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/nubus.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <asm/setup.h>
20 #include <asm/page.h>
21 #include <asm/hwtest.h>
22 #include <asm/mac_via.h>
23 #include <asm/mac_oss.h>
25 extern void via_nubus_init(void);
26 extern void oss_nubus_init(void);
28 /* Constants */
30 /* This is, of course, the size in bytelanes, rather than the size in
31 actual bytes */
32 #define FORMAT_BLOCK_SIZE 20
33 #define ROM_DIR_OFFSET 0x24
35 #define NUBUS_TEST_PATTERN 0x5A932BC7
37 /* Define this if you like to live dangerously - it is known not to
38 work on pretty much every machine except the Quadra 630 and the LC
39 III. */
40 #undef I_WANT_TO_PROBE_SLOT_ZERO
42 /* This sometimes helps combat failure to boot */
43 #undef TRY_TO_DODGE_WSOD
45 /* Globals */
47 struct nubus_dev* nubus_devices;
48 struct nubus_board* nubus_boards;
50 /* Meaning of "bytelanes":
52 The card ROM may appear on any or all bytes of each long word in
53 NuBus memory. The low 4 bits of the "map" value found in the
54 format block (at the top of the slot address space, as well as at
55 the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
56 offsets within each longword, are valid. Thus:
58 A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
59 are valid.
61 A map of 0xf0 means that no bytelanes are valid (We pray that we
62 will never encounter this, but stranger things have happened)
64 A map of 0xe1 means that only the MSB of each long word is actually
65 part of the card ROM. (We hope to never encounter NuBus on a
66 little-endian machine. Again, stranger things have happened)
68 A map of 0x78 means that only the LSB of each long word is valid.
70 Etcetera, etcetera. Hopefully this clears up some confusion over
71 what the following code actually does. */
73 static inline int not_useful(void *p, int map)
75 unsigned long pv=(unsigned long)p;
76 pv &= 3;
77 if(map & (1<<pv))
78 return 0;
79 return 1;
82 static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
84 /* This will hold the result */
85 unsigned long v = 0;
86 unsigned char *p = *ptr;
88 while(len)
90 v <<= 8;
91 while(not_useful(p,map))
92 p++;
93 v |= *p++;
94 len--;
96 *ptr = p;
97 return v;
100 static void nubus_rewind(unsigned char **ptr, int len, int map)
102 unsigned char *p=*ptr;
104 /* Sanity check */
105 if(len > 65536)
106 pr_err("rewind of 0x%08x!\n", len);
107 while(len)
111 p--;
113 while(not_useful(p, map));
114 len--;
116 *ptr=p;
119 static void nubus_advance(unsigned char **ptr, int len, int map)
121 unsigned char *p = *ptr;
122 if(len>65536)
123 pr_err("advance of 0x%08x!\n", len);
124 while(len)
126 while(not_useful(p,map))
127 p++;
128 p++;
129 len--;
131 *ptr = p;
134 static void nubus_move(unsigned char **ptr, int len, int map)
136 if(len > 0)
137 nubus_advance(ptr, len, map);
138 else if(len < 0)
139 nubus_rewind(ptr, -len, map);
142 /* Now, functions to read the sResource tree */
144 /* Each sResource entry consists of a 1-byte ID and a 3-byte data
145 field. If that data field contains an offset, then obviously we
146 have to expand it from a 24-bit signed number to a 32-bit signed
147 number. */
149 static inline long nubus_expand32(long foo)
151 if(foo & 0x00800000) /* 24bit negative */
152 foo |= 0xFF000000;
153 return foo;
156 static inline void *nubus_rom_addr(int slot)
159 * Returns the first byte after the card. We then walk
160 * backwards to get the lane register and the config
162 return (void *)(0xF1000000+(slot<<24));
165 static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
167 unsigned char *p = nd->base;
168 /* Essentially, just step over the bytelanes using whatever
169 offset we might have found */
170 nubus_move(&p, nubus_expand32(nd->data), nd->mask);
171 /* And return the value */
172 return p;
175 /* These two are for pulling resource data blocks (i.e. stuff that's
176 pointed to with offsets) out of the card ROM. */
178 void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent,
179 int len)
181 unsigned char *t = (unsigned char *)dest;
182 unsigned char *p = nubus_dirptr(dirent);
183 while(len)
185 *t++ = nubus_get_rom(&p, 1, dirent->mask);
186 len--;
189 EXPORT_SYMBOL(nubus_get_rsrc_mem);
191 void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
192 int len)
194 unsigned char *t=(unsigned char *)dest;
195 unsigned char *p = nubus_dirptr(dirent);
196 while(len)
198 *t = nubus_get_rom(&p, 1, dirent->mask);
199 if(!*t++)
200 break;
201 len--;
204 EXPORT_SYMBOL(nubus_get_rsrc_str);
206 int nubus_get_root_dir(const struct nubus_board* board,
207 struct nubus_dir* dir)
209 dir->ptr = dir->base = board->directory;
210 dir->done = 0;
211 dir->mask = board->lanes;
212 return 0;
214 EXPORT_SYMBOL(nubus_get_root_dir);
216 /* This is a slyly renamed version of the above */
217 int nubus_get_func_dir(const struct nubus_dev* dev,
218 struct nubus_dir* dir)
220 dir->ptr = dir->base = dev->directory;
221 dir->done = 0;
222 dir->mask = dev->board->lanes;
223 return 0;
225 EXPORT_SYMBOL(nubus_get_func_dir);
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;
244 EXPORT_SYMBOL(nubus_get_board_dir);
246 int nubus_get_subdir(const struct nubus_dirent *ent,
247 struct nubus_dir *dir)
249 dir->ptr = dir->base = nubus_dirptr(ent);
250 dir->done = 0;
251 dir->mask = ent->mask;
252 return 0;
254 EXPORT_SYMBOL(nubus_get_subdir);
256 int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
258 u32 resid;
259 if (nd->done)
260 return -1;
262 /* Do this first, otherwise nubus_rewind & co are off by 4 */
263 ent->base = nd->ptr;
265 /* This moves nd->ptr forward */
266 resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
268 /* EOL marker, as per the Apple docs */
269 if((resid&0xff000000) == 0xff000000)
271 /* Mark it as done */
272 nd->done = 1;
273 return -1;
276 /* First byte is the resource ID */
277 ent->type = resid >> 24;
278 /* Low 3 bytes might contain data (or might not) */
279 ent->data = resid & 0xffffff;
280 ent->mask = nd->mask;
281 return 0;
283 EXPORT_SYMBOL(nubus_readdir);
285 int nubus_rewinddir(struct nubus_dir* dir)
287 dir->ptr = dir->base;
288 dir->done = 0;
290 return 0;
292 EXPORT_SYMBOL(nubus_rewinddir);
294 /* Driver interface functions, more or less like in pci.c */
296 struct nubus_dev*
297 nubus_find_device(unsigned short category,
298 unsigned short type,
299 unsigned short dr_hw,
300 unsigned short dr_sw,
301 const struct nubus_dev* from)
303 struct nubus_dev* itor =
304 from ? from->next : nubus_devices;
306 while (itor) {
307 if (itor->category == category
308 && itor->type == type
309 && itor->dr_hw == dr_hw
310 && itor->dr_sw == dr_sw)
311 return itor;
312 itor = itor->next;
314 return NULL;
316 EXPORT_SYMBOL(nubus_find_device);
318 struct nubus_dev*
319 nubus_find_type(unsigned short category,
320 unsigned short type,
321 const struct nubus_dev* from)
323 struct nubus_dev* itor =
324 from ? from->next : nubus_devices;
326 while (itor) {
327 if (itor->category == category
328 && itor->type == type)
329 return itor;
330 itor = itor->next;
332 return NULL;
334 EXPORT_SYMBOL(nubus_find_type);
336 struct nubus_dev*
337 nubus_find_slot(unsigned int slot,
338 const struct nubus_dev* from)
340 struct nubus_dev* itor =
341 from ? from->next : nubus_devices;
343 while (itor) {
344 if (itor->board->slot == slot)
345 return itor;
346 itor = itor->next;
348 return NULL;
350 EXPORT_SYMBOL(nubus_find_slot);
353 nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
354 struct nubus_dirent* ent)
356 while (nubus_readdir(dir, ent) != -1) {
357 if (ent->type == rsrc_type)
358 return 0;
360 return -1;
362 EXPORT_SYMBOL(nubus_find_rsrc);
364 /* Initialization functions - decide which slots contain stuff worth
365 looking at, and print out lots and lots of information from the
366 resource blocks. */
368 /* FIXME: A lot of this stuff will eventually be useful after
369 initialization, for intelligently probing Ethernet and video chips,
370 among other things. The rest of it should go in the /proc code.
371 For now, we just use it to give verbose boot logs. */
373 static int __init nubus_show_display_resource(struct nubus_dev* dev,
374 const struct nubus_dirent* ent)
376 switch (ent->type) {
377 case NUBUS_RESID_GAMMADIR:
378 pr_info(" gamma directory offset: 0x%06x\n", ent->data);
379 break;
380 case 0x0080 ... 0x0085:
381 pr_info(" mode %02X info offset: 0x%06x\n",
382 ent->type, ent->data);
383 break;
384 default:
385 pr_info(" unknown resource %02X, data 0x%06x\n",
386 ent->type, ent->data);
388 return 0;
391 static int __init nubus_show_network_resource(struct nubus_dev* dev,
392 const struct nubus_dirent* ent)
394 switch (ent->type) {
395 case NUBUS_RESID_MAC_ADDRESS:
397 char addr[6];
399 nubus_get_rsrc_mem(addr, ent, 6);
400 pr_info(" MAC address: %pM\n", addr);
401 break;
403 default:
404 pr_info(" unknown resource %02X, data 0x%06x\n",
405 ent->type, ent->data);
407 return 0;
410 static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
411 const struct nubus_dirent* ent)
413 switch (ent->type) {
414 case NUBUS_RESID_MEMINFO:
416 unsigned long meminfo[2];
417 nubus_get_rsrc_mem(&meminfo, ent, 8);
418 pr_info(" memory: [ 0x%08lx 0x%08lx ]\n",
419 meminfo[0], meminfo[1]);
420 break;
422 case NUBUS_RESID_ROMINFO:
424 unsigned long rominfo[2];
425 nubus_get_rsrc_mem(&rominfo, ent, 8);
426 pr_info(" ROM: [ 0x%08lx 0x%08lx ]\n",
427 rominfo[0], rominfo[1]);
428 break;
430 default:
431 pr_info(" unknown resource %02X, data 0x%06x\n",
432 ent->type, ent->data);
434 return 0;
437 static int __init nubus_show_private_resource(struct nubus_dev* dev,
438 const struct nubus_dirent* ent)
440 switch (dev->category) {
441 case NUBUS_CAT_DISPLAY:
442 nubus_show_display_resource(dev, ent);
443 break;
444 case NUBUS_CAT_NETWORK:
445 nubus_show_network_resource(dev, ent);
446 break;
447 case NUBUS_CAT_CPU:
448 nubus_show_cpu_resource(dev, ent);
449 break;
450 default:
451 pr_info(" unknown resource %02X, data 0x%06x\n",
452 ent->type, ent->data);
454 return 0;
457 static struct nubus_dev* __init
458 nubus_get_functional_resource(struct nubus_board* board,
459 int slot,
460 const struct nubus_dirent* parent)
462 struct nubus_dir dir;
463 struct nubus_dirent ent;
464 struct nubus_dev* dev;
466 pr_info(" Function 0x%02x:\n", parent->type);
467 nubus_get_subdir(parent, &dir);
469 /* Apple seems to have botched the ROM on the IIx */
470 if (slot == 0 && (unsigned long)dir.base % 2)
471 dir.base += 1;
473 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
474 __func__, parent->base, dir.base);
476 /* Actually we should probably panic if this fails */
477 if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
478 return NULL;
479 dev->resid = parent->type;
480 dev->directory = dir.base;
481 dev->board = board;
483 while (nubus_readdir(&dir, &ent) != -1)
485 switch(ent.type)
487 case NUBUS_RESID_TYPE:
489 unsigned short nbtdata[4];
490 nubus_get_rsrc_mem(nbtdata, &ent, 8);
491 dev->category = nbtdata[0];
492 dev->type = nbtdata[1];
493 dev->dr_sw = nbtdata[2];
494 dev->dr_hw = nbtdata[3];
495 pr_info(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
496 nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
497 break;
499 case NUBUS_RESID_NAME:
501 nubus_get_rsrc_str(dev->name, &ent, 64);
502 pr_info(" name: %s\n", dev->name);
503 break;
505 case NUBUS_RESID_DRVRDIR:
507 /* MacOS driver. If we were NetBSD we might
508 use this :-) */
509 struct nubus_dir drvr_dir;
510 struct nubus_dirent drvr_ent;
511 nubus_get_subdir(&ent, &drvr_dir);
512 nubus_readdir(&drvr_dir, &drvr_ent);
513 dev->driver = nubus_dirptr(&drvr_ent);
514 pr_info(" driver at: 0x%p\n", dev->driver);
515 break;
517 case NUBUS_RESID_MINOR_BASEOS:
518 /* We will need this in order to support
519 multiple framebuffers. It might be handy
520 for Ethernet as well */
521 nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
522 pr_info(" memory offset: 0x%08lx\n", dev->iobase);
523 break;
524 case NUBUS_RESID_MINOR_LENGTH:
525 /* Ditto */
526 nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
527 pr_info(" memory length: 0x%08lx\n", dev->iosize);
528 break;
529 case NUBUS_RESID_FLAGS:
530 dev->flags = ent.data;
531 pr_info(" flags: 0x%06x\n", dev->flags);
532 break;
533 case NUBUS_RESID_HWDEVID:
534 dev->hwdevid = ent.data;
535 pr_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 pr_info(" video modes supported:\n");
563 nubus_get_subdir(parent, &dir);
564 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
565 __func__, parent->base, dir.base);
567 while(nubus_readdir(&dir, &ent) != -1)
569 struct vidmode mode;
570 u32 size;
572 /* First get the length */
573 nubus_get_rsrc_mem(&size, &ent, 4);
575 /* Now clobber the whole thing */
576 if (size > sizeof(mode) - 1)
577 size = sizeof(mode) - 1;
578 memset(&mode, 0, sizeof(mode));
579 nubus_get_rsrc_mem(&mode, &ent, size);
580 pr_info(" %02X: (%02X) %s\n", ent.type,
581 mode.id, mode.name);
583 return 0;
586 /* This is *really* cool. */
587 static int __init nubus_get_icon(struct nubus_board* board,
588 const struct nubus_dirent* ent)
590 /* Should be 32x32 if my memory serves me correctly */
591 unsigned char icon[128];
592 int x, y;
594 nubus_get_rsrc_mem(&icon, ent, 128);
595 pr_info(" icon:\n");
597 /* We should actually plot these somewhere in the framebuffer
598 init. This is just to demonstrate that they do, in fact,
599 exist */
600 for (y = 0; y < 32; y++) {
601 pr_info(" ");
602 for (x = 0; x < 32; x++) {
603 if (icon[y*4 + x/8]
604 & (0x80 >> (x%8)))
605 pr_cont("*");
606 else
607 pr_cont(" ");
609 pr_cont("\n");
611 return 0;
614 static int __init nubus_get_vendorinfo(struct nubus_board* board,
615 const struct nubus_dirent* parent)
617 struct nubus_dir dir;
618 struct nubus_dirent ent;
619 static char* vendor_fields[6] = {"ID", "serial", "revision",
620 "part", "date", "unknown field"};
622 pr_info(" vendor info:\n");
623 nubus_get_subdir(parent, &dir);
624 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
625 __func__, parent->base, dir.base);
627 while(nubus_readdir(&dir, &ent) != -1)
629 char name[64];
631 /* These are all strings, we think */
632 nubus_get_rsrc_str(name, &ent, 64);
633 if (ent.type > 5)
634 ent.type = 5;
635 pr_info(" %s: %s\n", vendor_fields[ent.type - 1], name);
637 return 0;
640 static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
641 const struct nubus_dirent* parent)
643 struct nubus_dir dir;
644 struct nubus_dirent ent;
646 nubus_get_subdir(parent, &dir);
647 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
648 __func__, parent->base, dir.base);
650 while(nubus_readdir(&dir, &ent) != -1)
652 switch (ent.type) {
653 case NUBUS_RESID_TYPE:
655 unsigned short nbtdata[4];
656 /* This type is always the same, and is not
657 useful except insofar as it tells us that
658 we really are looking at a board resource. */
659 nubus_get_rsrc_mem(nbtdata, &ent, 8);
660 pr_info(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
661 nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
662 if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
663 nbtdata[2] != 0 || nbtdata[3] != 0)
664 pr_err("this sResource is not a board resource!\n");
665 break;
667 case NUBUS_RESID_NAME:
668 nubus_get_rsrc_str(board->name, &ent, 64);
669 pr_info(" name: %s\n", board->name);
670 break;
671 case NUBUS_RESID_ICON:
672 nubus_get_icon(board, &ent);
673 break;
674 case NUBUS_RESID_BOARDID:
675 pr_info(" board id: 0x%x\n", ent.data);
676 break;
677 case NUBUS_RESID_PRIMARYINIT:
678 pr_info(" primary init offset: 0x%06x\n", ent.data);
679 break;
680 case NUBUS_RESID_VENDORINFO:
681 nubus_get_vendorinfo(board, &ent);
682 break;
683 case NUBUS_RESID_FLAGS:
684 pr_info(" flags: 0x%06x\n", ent.data);
685 break;
686 case NUBUS_RESID_HWDEVID:
687 pr_info(" hwdevid: 0x%06x\n", ent.data);
688 break;
689 case NUBUS_RESID_SECONDINIT:
690 pr_info(" secondary init offset: 0x%06x\n", ent.data);
691 break;
692 /* WTF isn't this in the functional resources? */
693 case NUBUS_RESID_VIDNAMES:
694 nubus_get_vidnames(board, &ent);
695 break;
696 /* Same goes for this */
697 case NUBUS_RESID_VIDMODES:
698 pr_info(" video mode parameter directory offset: 0x%06x\n",
699 ent.data);
700 break;
701 default:
702 pr_info(" unknown resource %02X, data 0x%06x\n",
703 ent.type, ent.data);
706 return 0;
709 /* Attempt to bypass the somewhat non-obvious arrangement of
710 sResources in the motherboard ROM */
711 static void __init nubus_find_rom_dir(struct nubus_board* board)
713 unsigned char* rp;
714 unsigned char* romdir;
715 struct nubus_dir dir;
716 struct nubus_dirent ent;
718 /* Check for the extra directory just under the format block */
719 rp = board->fblock;
720 nubus_rewind(&rp, 4, board->lanes);
721 if (nubus_get_rom(&rp, 4, board->lanes) != NUBUS_TEST_PATTERN) {
722 /* OK, the ROM was telling the truth */
723 board->directory = board->fblock;
724 nubus_move(&board->directory,
725 nubus_expand32(board->doffset),
726 board->lanes);
727 return;
730 /* On "slot zero", you have to walk down a few more
731 directories to get to the equivalent of a real card's root
732 directory. We don't know what they were smoking when they
733 came up with this. */
734 romdir = nubus_rom_addr(board->slot);
735 nubus_rewind(&romdir, ROM_DIR_OFFSET, board->lanes);
736 dir.base = dir.ptr = romdir;
737 dir.done = 0;
738 dir.mask = board->lanes;
740 /* This one points to an "Unknown Macintosh" directory */
741 if (nubus_readdir(&dir, &ent) == -1)
742 goto badrom;
744 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
745 printk(KERN_INFO "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
746 /* This one takes us to where we want to go. */
747 if (nubus_readdir(&dir, &ent) == -1)
748 goto badrom;
749 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
750 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
751 nubus_get_subdir(&ent, &dir);
753 /* Resource ID 01, also an "Unknown Macintosh" */
754 if (nubus_readdir(&dir, &ent) == -1)
755 goto badrom;
756 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
757 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
759 /* FIXME: the first one is *not* always the right one. We
760 suspect this has something to do with the ROM revision.
761 "The HORROR ROM" (LC-series) uses 0x7e, while "The HORROR
762 Continues" (Q630) uses 0x7b. The DAFB Macs evidently use
763 something else. Please run "Slots" on your Mac (see
764 include/linux/nubus.h for where to get this program) and
765 tell us where the 'SiDirPtr' for Slot 0 is. If you feel
766 brave, you should also use MacsBug to walk down the ROM
767 directories like this function does and try to find the
768 path to that address... */
769 if (nubus_readdir(&dir, &ent) == -1)
770 goto badrom;
771 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
772 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
774 /* Bwahahahaha... */
775 nubus_get_subdir(&ent, &dir);
776 board->directory = dir.base;
777 return;
779 /* Even more evil laughter... */
780 badrom:
781 board->directory = board->fblock;
782 nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
783 printk(KERN_ERR "nubus_get_rom_dir: ROM weirdness! Notify the developers...\n");
786 /* Add a board (might be many devices) to the list */
787 static struct nubus_board* __init nubus_add_board(int slot, int bytelanes)
789 struct nubus_board* board;
790 struct nubus_board** boardp;
792 unsigned char *rp;
793 unsigned long dpat;
794 struct nubus_dir dir;
795 struct nubus_dirent ent;
797 /* Move to the start of the format block */
798 rp = nubus_rom_addr(slot);
799 nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
801 /* Actually we should probably panic if this fails */
802 if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
803 return NULL;
804 board->fblock = rp;
806 /* Dump the format block for debugging purposes */
807 pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
808 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
809 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
810 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
811 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
812 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
813 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
814 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
815 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
816 rp = board->fblock;
818 board->slot = slot;
819 board->slot_addr = (unsigned long) nubus_slot_addr(slot);
820 board->doffset = nubus_get_rom(&rp, 4, bytelanes);
821 /* rom_length is *supposed* to be the total length of the
822 * ROM. In practice it is the "amount of ROM used to compute
823 * the CRC." So some jokers decide to set it to zero and
824 * set the crc to zero so they don't have to do any math.
825 * See the Performa 460 ROM, for example. Those Apple "engineers".
827 board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
828 board->crc = nubus_get_rom(&rp, 4, bytelanes);
829 board->rev = nubus_get_rom(&rp, 1, bytelanes);
830 board->format = nubus_get_rom(&rp,1, bytelanes);
831 board->lanes = bytelanes;
833 /* Directory offset should be small and negative... */
834 if(!(board->doffset & 0x00FF0000))
835 pr_warn("Dodgy doffset!\n");
836 dpat = nubus_get_rom(&rp, 4, bytelanes);
837 if(dpat != NUBUS_TEST_PATTERN)
838 pr_warn("Wrong test pattern %08lx!\n", dpat);
841 * I wonder how the CRC is meant to work -
842 * any takers ?
843 * CSA: According to MAC docs, not all cards pass the CRC anyway,
844 * since the initial Macintosh ROM releases skipped the check.
847 /* Attempt to work around slot zero weirdness */
848 nubus_find_rom_dir(board);
849 nubus_get_root_dir(board, &dir);
851 /* We're ready to rock */
852 pr_info("Slot %X:\n", slot);
854 /* Each slot should have one board resource and any number of
855 functional resources. So we'll fill in some fields in the
856 struct nubus_board from the board resource, then walk down
857 the list of functional resources, spinning out a nubus_dev
858 for each of them. */
859 if (nubus_readdir(&dir, &ent) == -1) {
860 /* We can't have this! */
861 pr_err("Board resource not found!\n");
862 return NULL;
863 } else {
864 pr_info(" Board resource:\n");
865 nubus_get_board_resource(board, slot, &ent);
868 /* Aaaarrrrgghh! The LC III motherboard has *two* board
869 resources. I have no idea WTF to do about this. */
871 while (nubus_readdir(&dir, &ent) != -1) {
872 struct nubus_dev* dev;
873 struct nubus_dev** devp;
874 dev = nubus_get_functional_resource(board, slot, &ent);
875 if (dev == NULL)
876 continue;
878 /* We zeroed this out above */
879 if (board->first_dev == NULL)
880 board->first_dev = dev;
882 /* Put it on the global NuBus device chain. Keep entries in order. */
883 for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
884 /* spin */;
885 *devp = dev;
886 dev->next = NULL;
889 /* Put it on the global NuBus board chain. Keep entries in order. */
890 for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
891 /* spin */;
892 *boardp = board;
893 board->next = NULL;
895 return board;
898 void __init nubus_probe_slot(int slot)
900 unsigned char dp;
901 unsigned char* rp;
902 int i;
904 rp = nubus_rom_addr(slot);
905 for(i = 4; i; i--)
907 int card_present;
909 rp--;
910 card_present = hwreg_present(rp);
911 if (!card_present)
912 continue;
914 dp = *rp;
915 if(dp == 0)
916 continue;
918 /* The last byte of the format block consists of two
919 nybbles which are "mirror images" of each other.
920 These show us the valid bytelanes */
921 if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
922 continue;
923 /* Check that this value is actually *on* one of the
924 bytelanes it claims are valid! */
925 if ((dp & 0x0F) >= (1<<i))
926 continue;
928 /* Looks promising. Let's put it on the list. */
929 nubus_add_board(slot, dp);
931 return;
935 void __init nubus_scan_bus(void)
937 int slot;
938 /* This might not work on your machine */
939 #ifdef I_WANT_TO_PROBE_SLOT_ZERO
940 nubus_probe_slot(0);
941 #endif
942 for(slot = 9; slot < 15; slot++)
944 nubus_probe_slot(slot);
948 static int __init nubus_init(void)
950 if (!MACH_IS_MAC)
951 return 0;
953 /* Initialize the NuBus interrupts */
954 if (oss_present) {
955 oss_nubus_init();
956 } else {
957 via_nubus_init();
960 #ifdef TRY_TO_DODGE_WSOD
961 /* Rogue Ethernet interrupts can kill the machine if we don't
962 do this. Obviously this is bogus. Hopefully the local VIA
963 gurus can fix the real cause of the problem. */
964 mdelay(1000);
965 #endif
967 /* And probe */
968 pr_info("NuBus: Scanning NuBus slots.\n");
969 nubus_devices = NULL;
970 nubus_boards = NULL;
971 nubus_scan_bus();
972 nubus_proc_init();
973 return 0;
976 subsys_initcall(nubus_init);