• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/nubus/
1/*
2 *	Macintosh Nubus Interface Code
3 *
4 *      Originally by Alan Cox
5 *
6 *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
7 *      and others.
8 */
9
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/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>
26
27extern void via_nubus_init(void);
28extern void oss_nubus_init(void);
29
30/* Constants */
31
32/* This is, of course, the size in bytelanes, rather than the size in
33   actual bytes */
34#define FORMAT_BLOCK_SIZE 20
35#define ROM_DIR_OFFSET 0x24
36
37#define NUBUS_TEST_PATTERN 0x5A932BC7
38
39/* Define this if you like to live dangerously - it is known not to
40   work on pretty much every machine except the Quadra 630 and the LC
41   III. */
42#undef I_WANT_TO_PROBE_SLOT_ZERO
43
44/* This sometimes helps combat failure to boot */
45#undef TRY_TO_DODGE_WSOD
46
47/* Globals */
48
49struct nubus_dev*   nubus_devices;
50struct nubus_board* nubus_boards;
51
52/* Meaning of "bytelanes":
53
54   The card ROM may appear on any or all bytes of each long word in
55   NuBus memory.  The low 4 bits of the "map" value found in the
56   format block (at the top of the slot address space, as well as at
57   the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
58   offsets within each longword, are valid.  Thus:
59
60   A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
61   are valid.
62
63   A map of 0xf0 means that no bytelanes are valid (We pray that we
64   will never encounter this, but stranger things have happened)
65
66   A map of 0xe1 means that only the MSB of each long word is actually
67   part of the card ROM.  (We hope to never encounter NuBus on a
68   little-endian machine.  Again, stranger things have happened)
69
70   A map of 0x78 means that only the LSB of each long word is valid.
71
72   Etcetera, etcetera.  Hopefully this clears up some confusion over
73   what the following code actually does.  */
74
75static inline int not_useful(void *p, int map)
76{
77	unsigned long pv=(unsigned long)p;
78	pv &= 3;
79	if(map & (1<<pv))
80		return 0;
81	return 1;
82}
83
84static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
85{
86	/* This will hold the result */
87	unsigned long v = 0;
88	unsigned char *p = *ptr;
89
90	while(len)
91	{
92		v <<= 8;
93		while(not_useful(p,map))
94			p++;
95		v |= *p++;
96		len--;
97	}
98	*ptr = p;
99	return v;
100}
101
102static void nubus_rewind(unsigned char **ptr, int len, int map)
103{
104	unsigned char *p=*ptr;
105
106	/* Sanity check */
107	if(len > 65536)
108		printk(KERN_ERR "rewind of 0x%08x!\n", len);
109	while(len)
110	{
111		do
112		{
113			p--;
114		}
115		while(not_useful(p, map));
116		len--;
117	}
118	*ptr=p;
119}
120
121static void nubus_advance(unsigned char **ptr, int len, int map)
122{
123	unsigned char *p = *ptr;
124	if(len>65536)
125		printk(KERN_ERR "advance of 0x%08x!\n", len);
126	while(len)
127	{
128		while(not_useful(p,map))
129			p++;
130		p++;
131		len--;
132	}
133	*ptr = p;
134}
135
136static void nubus_move(unsigned char **ptr, int len, int map)
137{
138	if(len > 0)
139		nubus_advance(ptr, len, map);
140	else if(len < 0)
141		nubus_rewind(ptr, -len, map);
142}
143
144/* Now, functions to read the sResource tree */
145
146/* Each sResource entry consists of a 1-byte ID and a 3-byte data
147   field.  If that data field contains an offset, then obviously we
148   have to expand it from a 24-bit signed number to a 32-bit signed
149   number. */
150
151static inline long nubus_expand32(long foo)
152{
153	if(foo & 0x00800000)	/* 24bit negative */
154		foo |= 0xFF000000;
155	return foo;
156}
157
158static inline void *nubus_rom_addr(int slot)
159{
160	/*
161	 *	Returns the first byte after the card. We then walk
162	 *	backwards to get the lane register and the config
163	 */
164	return (void *)(0xF1000000+(slot<<24));
165}
166
167static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
168{
169	unsigned char *p = nd->base;
170	/* Essentially, just step over the bytelanes using whatever
171	   offset we might have found */
172	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
173	/* And return the value */
174	return p;
175}
176
177/* These two are for pulling resource data blocks (i.e. stuff that's
178   pointed to with offsets) out of the card ROM. */
179
180void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent,
181			int len)
182{
183	unsigned char *t = (unsigned char *)dest;
184	unsigned char *p = nubus_dirptr(dirent);
185	while(len)
186	{
187		*t++ = nubus_get_rom(&p, 1, dirent->mask);
188		len--;
189	}
190}
191EXPORT_SYMBOL(nubus_get_rsrc_mem);
192
193void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
194			int len)
195{
196	unsigned char *t=(unsigned char *)dest;
197	unsigned char *p = nubus_dirptr(dirent);
198	while(len)
199	{
200		*t = nubus_get_rom(&p, 1, dirent->mask);
201		if(!*t++)
202			break;
203		len--;
204	}
205}
206EXPORT_SYMBOL(nubus_get_rsrc_str);
207
208int nubus_get_root_dir(const struct nubus_board* board,
209		       struct nubus_dir* dir)
210{
211	dir->ptr = dir->base = board->directory;
212	dir->done = 0;
213	dir->mask = board->lanes;
214	return 0;
215}
216EXPORT_SYMBOL(nubus_get_root_dir);
217
218/* This is a slyly renamed version of the above */
219int nubus_get_func_dir(const struct nubus_dev* dev,
220		       struct nubus_dir* dir)
221{
222	dir->ptr = dir->base = dev->directory;
223	dir->done = 0;
224	dir->mask = dev->board->lanes;
225	return 0;
226}
227EXPORT_SYMBOL(nubus_get_func_dir);
228
229int nubus_get_board_dir(const struct nubus_board* board,
230			struct nubus_dir* dir)
231{
232	struct nubus_dirent ent;
233
234	dir->ptr = dir->base = board->directory;
235	dir->done = 0;
236	dir->mask = board->lanes;
237
238	/* Now dereference it (the first directory is always the board
239	   directory) */
240	if (nubus_readdir(dir, &ent) == -1)
241		return -1;
242	if (nubus_get_subdir(&ent, dir) == -1)
243		return -1;
244	return 0;
245}
246EXPORT_SYMBOL(nubus_get_board_dir);
247
248int nubus_get_subdir(const struct nubus_dirent *ent,
249		     struct nubus_dir *dir)
250{
251	dir->ptr = dir->base = nubus_dirptr(ent);
252	dir->done = 0;
253	dir->mask = ent->mask;
254	return 0;
255}
256EXPORT_SYMBOL(nubus_get_subdir);
257
258int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
259{
260	u32 resid;
261	if (nd->done)
262		return -1;
263
264	/* Do this first, otherwise nubus_rewind & co are off by 4 */
265	ent->base = nd->ptr;
266
267	/* This moves nd->ptr forward */
268	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
269
270	/* EOL marker, as per the Apple docs */
271	if((resid&0xff000000) == 0xff000000)
272	{
273		/* Mark it as done */
274		nd->done = 1;
275		return -1;
276	}
277
278	/* First byte is the resource ID */
279	ent->type  = resid >> 24;
280	/* Low 3 bytes might contain data (or might not) */
281	ent->data = resid & 0xffffff;
282	ent->mask  = nd->mask;
283	return 0;
284}
285EXPORT_SYMBOL(nubus_readdir);
286
287int nubus_rewinddir(struct nubus_dir* dir)
288{
289	dir->ptr = dir->base;
290	return 0;
291}
292EXPORT_SYMBOL(nubus_rewinddir);
293
294/* Driver interface functions, more or less like in pci.c */
295
296struct nubus_dev*
297nubus_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)
302{
303	struct nubus_dev* itor =
304		from ? from->next : nubus_devices;
305
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;
313	}
314	return NULL;
315}
316EXPORT_SYMBOL(nubus_find_device);
317
318struct nubus_dev*
319nubus_find_type(unsigned short category,
320		unsigned short type,
321		const struct nubus_dev* from)
322{
323	struct nubus_dev* itor =
324		from ? from->next : nubus_devices;
325
326	while (itor) {
327		if (itor->category == category
328		    && itor->type == type)
329			return itor;
330		itor = itor->next;
331	}
332	return NULL;
333}
334EXPORT_SYMBOL(nubus_find_type);
335
336struct nubus_dev*
337nubus_find_slot(unsigned int slot,
338		const struct nubus_dev* from)
339{
340	struct nubus_dev* itor =
341		from ? from->next : nubus_devices;
342
343	while (itor) {
344		if (itor->board->slot == slot)
345			return itor;
346		itor = itor->next;
347	}
348	return NULL;
349}
350EXPORT_SYMBOL(nubus_find_slot);
351
352int
353nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
354		struct nubus_dirent* ent)
355{
356	while (nubus_readdir(dir, ent) != -1) {
357		if (ent->type == rsrc_type)
358			return 0;
359	}
360	return -1;
361}
362EXPORT_SYMBOL(nubus_find_rsrc);
363
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. */
367
368
369static int __init nubus_show_display_resource(struct nubus_dev* dev,
370					      const struct nubus_dirent* ent)
371{
372	switch (ent->type) {
373	case NUBUS_RESID_GAMMADIR:
374		printk(KERN_INFO "    gamma directory offset: 0x%06x\n", ent->data);
375		break;
376	case 0x0080 ... 0x0085:
377		printk(KERN_INFO "    mode %02X info offset: 0x%06x\n",
378		       ent->type, ent->data);
379		break;
380	default:
381		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
382		       ent->type, ent->data);
383	}
384	return 0;
385}
386
387static int __init nubus_show_network_resource(struct nubus_dev* dev,
388					      const struct nubus_dirent* ent)
389{
390	switch (ent->type) {
391	case NUBUS_RESID_MAC_ADDRESS:
392	{
393		char addr[6];
394		int i;
395
396		nubus_get_rsrc_mem(addr, ent, 6);
397		printk(KERN_INFO "    MAC address: ");
398		for (i = 0; i < 6; i++)
399			printk("%02x%s", addr[i] & 0xff,
400			       i == 5 ? "" : ":");
401		printk("\n");
402		break;
403	}
404	default:
405		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
406		       ent->type, ent->data);
407	}
408	return 0;
409}
410
411static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
412					  const struct nubus_dirent* ent)
413{
414	switch (ent->type) {
415	case NUBUS_RESID_MEMINFO:
416	{
417		unsigned long meminfo[2];
418		nubus_get_rsrc_mem(&meminfo, ent, 8);
419		printk(KERN_INFO "    memory: [ 0x%08lx 0x%08lx ]\n",
420		       meminfo[0], meminfo[1]);
421		break;
422	}
423	case NUBUS_RESID_ROMINFO:
424	{
425		unsigned long rominfo[2];
426		nubus_get_rsrc_mem(&rominfo, ent, 8);
427		printk(KERN_INFO "    ROM:    [ 0x%08lx 0x%08lx ]\n",
428		       rominfo[0], rominfo[1]);
429		break;
430	}
431	default:
432		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
433		       ent->type, ent->data);
434	}
435	return 0;
436}
437
438static int __init nubus_show_private_resource(struct nubus_dev* dev,
439					      const struct nubus_dirent* ent)
440{
441	switch (dev->category) {
442	case NUBUS_CAT_DISPLAY:
443		nubus_show_display_resource(dev, ent);
444		break;
445	case NUBUS_CAT_NETWORK:
446		nubus_show_network_resource(dev, ent);
447		break;
448	case NUBUS_CAT_CPU:
449		nubus_show_cpu_resource(dev, ent);
450		break;
451	default:
452		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
453		       ent->type, ent->data);
454	}
455	return 0;
456}
457
458static struct nubus_dev* __init
459	   nubus_get_functional_resource(struct nubus_board* board,
460					 int slot,
461					 const struct nubus_dirent* parent)
462{
463	struct nubus_dir    dir;
464	struct nubus_dirent ent;
465	struct nubus_dev*   dev;
466
467	printk(KERN_INFO "  Function 0x%02x:\n", parent->type);
468	nubus_get_subdir(parent, &dir);
469
470	/* Apple seems to have botched the ROM on the IIx */
471	if (slot == 0 && (unsigned long)dir.base % 2)
472		dir.base += 1;
473
474	if (console_loglevel >= 10)
475		printk(KERN_DEBUG "nubus_get_functional_resource: parent is 0x%p, dir is 0x%p\n",
476		       parent->base, dir.base);
477
478	/* Actually we should probably panic if this fails */
479	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
480		return NULL;
481	dev->resid = parent->type;
482	dev->directory = dir.base;
483	dev->board = board;
484
485	while (nubus_readdir(&dir, &ent) != -1)
486	{
487		switch(ent.type)
488		{
489		case NUBUS_RESID_TYPE:
490		{
491			unsigned short nbtdata[4];
492			nubus_get_rsrc_mem(nbtdata, &ent, 8);
493			dev->category = nbtdata[0];
494			dev->type     = nbtdata[1];
495			dev->dr_sw    = nbtdata[2];
496			dev->dr_hw    = nbtdata[3];
497			printk(KERN_INFO "    type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
498			       nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
499			break;
500		}
501		case NUBUS_RESID_NAME:
502		{
503			nubus_get_rsrc_str(dev->name, &ent, 64);
504			printk(KERN_INFO "    name: %s\n", dev->name);
505			break;
506		}
507		case NUBUS_RESID_DRVRDIR:
508		{
509			/* MacOS driver.  If we were NetBSD we might
510			   use this :-) */
511			struct nubus_dir drvr_dir;
512			struct nubus_dirent drvr_ent;
513			nubus_get_subdir(&ent, &drvr_dir);
514			nubus_readdir(&drvr_dir, &drvr_ent);
515			dev->driver = nubus_dirptr(&drvr_ent);
516			printk(KERN_INFO "    driver at: 0x%p\n",
517			       dev->driver);
518			break;
519		}
520		case NUBUS_RESID_MINOR_BASEOS:
521			/* We will need this in order to support
522			   multiple framebuffers.  It might be handy
523			   for Ethernet as well */
524			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
525			printk(KERN_INFO "    memory offset: 0x%08lx\n",
526			       dev->iobase);
527			break;
528		case NUBUS_RESID_MINOR_LENGTH:
529			/* Ditto */
530			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
531			printk(KERN_INFO "    memory length: 0x%08lx\n",
532			       dev->iosize);
533			break;
534		case NUBUS_RESID_FLAGS:
535			dev->flags = ent.data;
536			printk(KERN_INFO "    flags: 0x%06x\n", dev->flags);
537			break;
538		case NUBUS_RESID_HWDEVID:
539			dev->hwdevid = ent.data;
540			printk(KERN_INFO "    hwdevid: 0x%06x\n", dev->hwdevid);
541			break;
542		default:
543			/* Local/Private resources have their own
544			   function */
545			nubus_show_private_resource(dev, &ent);
546		}
547	}
548
549	return dev;
550}
551
552/* This is cool. */
553static int __init nubus_get_vidnames(struct nubus_board* board,
554				     const struct nubus_dirent* parent)
555{
556	struct nubus_dir    dir;
557	struct nubus_dirent ent;
558	struct vidmode {
559		u32 size;
560		/* Don't know what this is yet */
561		u16 id;
562		/* Longest one I've seen so far is 26 characters */
563		char name[32];
564	};
565
566	printk(KERN_INFO "    video modes supported:\n");
567	nubus_get_subdir(parent, &dir);
568	if (console_loglevel >= 10)
569		printk(KERN_DEBUG "nubus_get_vidnames: parent is 0x%p, dir is 0x%p\n",
570		       parent->base, dir.base);
571
572	while(nubus_readdir(&dir, &ent) != -1)
573	{
574		struct vidmode mode;
575		u32 size;
576
577		/* First get the length */
578		nubus_get_rsrc_mem(&size, &ent, 4);
579
580		/* Now clobber the whole thing */
581		if (size > sizeof(mode) - 1)
582			size = sizeof(mode) - 1;
583		memset(&mode, 0, sizeof(mode));
584		nubus_get_rsrc_mem(&mode, &ent, size);
585		printk (KERN_INFO "      %02X: (%02X) %s\n", ent.type,
586			mode.id, mode.name);
587	}
588	return 0;
589}
590
591/* This is *really* cool. */
592static int __init nubus_get_icon(struct nubus_board* board,
593				 const struct nubus_dirent* ent)
594{
595	/* Should be 32x32 if my memory serves me correctly */
596	unsigned char icon[128];
597	int x, y;
598
599	nubus_get_rsrc_mem(&icon, ent, 128);
600	printk(KERN_INFO "    icon:\n");
601
602	/* We should actually plot these somewhere in the framebuffer
603	   init.  This is just to demonstrate that they do, in fact,
604	   exist */
605	for (y = 0; y < 32; y++) {
606		printk(KERN_INFO "      ");
607		for (x = 0; x < 32; x++) {
608			if (icon[y*4 + x/8]
609			    & (0x80 >> (x%8)))
610				printk("*");
611			else
612				printk(" ");
613		}
614		printk("\n");
615	}
616	return 0;
617}
618
619static int __init nubus_get_vendorinfo(struct nubus_board* board,
620				       const struct nubus_dirent* parent)
621{
622	struct nubus_dir    dir;
623	struct nubus_dirent ent;
624	static char* vendor_fields[6] = {"ID", "serial", "revision",
625					 "part", "date", "unknown field"};
626
627	printk(KERN_INFO "    vendor info:\n");
628	nubus_get_subdir(parent, &dir);
629	if (console_loglevel >= 10)
630		printk(KERN_DEBUG "nubus_get_vendorinfo: parent is 0x%p, dir is 0x%p\n",
631		       parent->base, dir.base);
632
633	while(nubus_readdir(&dir, &ent) != -1)
634	{
635		char name[64];
636
637		/* These are all strings, we think */
638		nubus_get_rsrc_str(name, &ent, 64);
639		if (ent.type > 5)
640			ent.type = 5;
641		printk(KERN_INFO "    %s: %s\n",
642		       vendor_fields[ent.type-1], name);
643	}
644	return 0;
645}
646
647static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
648					   const struct nubus_dirent* parent)
649{
650	struct nubus_dir    dir;
651	struct nubus_dirent ent;
652
653	nubus_get_subdir(parent, &dir);
654	if (console_loglevel >= 10)
655		printk(KERN_DEBUG "nubus_get_board_resource: parent is 0x%p, dir is 0x%p\n",
656		       parent->base, dir.base);
657
658	while(nubus_readdir(&dir, &ent) != -1)
659	{
660		switch (ent.type) {
661		case NUBUS_RESID_TYPE:
662		{
663			unsigned short nbtdata[4];
664			/* This type is always the same, and is not
665			   useful except insofar as it tells us that
666			   we really are looking at a board resource. */
667			nubus_get_rsrc_mem(nbtdata, &ent, 8);
668			printk(KERN_INFO "    type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
669			       nbtdata[0], nbtdata[1], nbtdata[2],
670			       nbtdata[3]);
671			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
672			    nbtdata[2] != 0 || nbtdata[3] != 0)
673				printk(KERN_ERR "this sResource is not a board resource!\n");
674			break;
675		}
676		case NUBUS_RESID_NAME:
677			nubus_get_rsrc_str(board->name, &ent, 64);
678			printk(KERN_INFO "    name: %s\n", board->name);
679			break;
680		case NUBUS_RESID_ICON:
681			nubus_get_icon(board, &ent);
682			break;
683		case NUBUS_RESID_BOARDID:
684			printk(KERN_INFO "    board id: 0x%x\n", ent.data);
685			break;
686		case NUBUS_RESID_PRIMARYINIT:
687			printk(KERN_INFO "    primary init offset: 0x%06x\n", ent.data);
688			break;
689		case NUBUS_RESID_VENDORINFO:
690			nubus_get_vendorinfo(board, &ent);
691			break;
692		case NUBUS_RESID_FLAGS:
693			printk(KERN_INFO "    flags: 0x%06x\n", ent.data);
694			break;
695		case NUBUS_RESID_HWDEVID:
696			printk(KERN_INFO "    hwdevid: 0x%06x\n", ent.data);
697			break;
698		case NUBUS_RESID_SECONDINIT:
699			printk(KERN_INFO "    secondary init offset: 0x%06x\n", ent.data);
700			break;
701			/* WTF isn't this in the functional resources? */
702		case NUBUS_RESID_VIDNAMES:
703			nubus_get_vidnames(board, &ent);
704			break;
705			/* Same goes for this */
706		case NUBUS_RESID_VIDMODES:
707			printk(KERN_INFO "    video mode parameter directory offset: 0x%06x\n",
708			       ent.data);
709			break;
710		default:
711			printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
712			       ent.type, ent.data);
713		}
714	}
715	return 0;
716}
717
718/* Attempt to bypass the somewhat non-obvious arrangement of
719   sResources in the motherboard ROM */
720static void __init nubus_find_rom_dir(struct nubus_board* board)
721{
722	unsigned char* rp;
723	unsigned char* romdir;
724	struct nubus_dir dir;
725	struct nubus_dirent ent;
726
727	/* Check for the extra directory just under the format block */
728	rp = board->fblock;
729	nubus_rewind(&rp, 4, board->lanes);
730	if (nubus_get_rom(&rp, 4, board->lanes) != NUBUS_TEST_PATTERN) {
731		/* OK, the ROM was telling the truth */
732		board->directory = board->fblock;
733		nubus_move(&board->directory,
734			   nubus_expand32(board->doffset),
735			   board->lanes);
736		return;
737	}
738
739	/* On "slot zero", you have to walk down a few more
740	   directories to get to the equivalent of a real card's root
741	   directory.  We don't know what they were smoking when they
742	   came up with this. */
743	romdir = nubus_rom_addr(board->slot);
744	nubus_rewind(&romdir, ROM_DIR_OFFSET, board->lanes);
745	dir.base = dir.ptr = romdir;
746	dir.done = 0;
747	dir.mask = board->lanes;
748
749	/* This one points to an "Unknown Macintosh" directory */
750	if (nubus_readdir(&dir, &ent) == -1)
751		goto badrom;
752
753	if (console_loglevel >= 10)
754		printk(KERN_INFO "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
755	/* This one takes us to where we want to go. */
756	if (nubus_readdir(&dir, &ent) == -1)
757		goto badrom;
758	if (console_loglevel >= 10)
759		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
760	nubus_get_subdir(&ent, &dir);
761
762	/* Resource ID 01, also an "Unknown Macintosh" */
763	if (nubus_readdir(&dir, &ent) == -1)
764		goto badrom;
765	if (console_loglevel >= 10)
766		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
767
768	if (nubus_readdir(&dir, &ent) == -1)
769		goto badrom;
770	if (console_loglevel >= 10)
771		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
772
773	/* Bwahahahaha... */
774	nubus_get_subdir(&ent, &dir);
775	board->directory = dir.base;
776	return;
777
778	/* Even more evil laughter... */
779 badrom:
780	board->directory = board->fblock;
781	nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
782	printk(KERN_ERR "nubus_get_rom_dir: ROM weirdness!  Notify the developers...\n");
783}
784
785/* Add a board (might be many devices) to the list */
786static struct nubus_board* __init nubus_add_board(int slot, int bytelanes)
787{
788	struct nubus_board* board;
789	struct nubus_board** boardp;
790
791	unsigned char *rp;
792	unsigned long dpat;
793	struct nubus_dir dir;
794	struct nubus_dirent ent;
795
796	/* Move to the start of the format block */
797	rp = nubus_rom_addr(slot);
798	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
799
800	/* Actually we should probably panic if this fails */
801	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
802		return NULL;
803	board->fblock = rp;
804
805	/* Dump the format block for debugging purposes */
806	if (console_loglevel >= 10) {
807		int i;
808		printk(KERN_DEBUG "Slot %X, format block at 0x%p\n",
809		       slot, rp);
810		printk(KERN_DEBUG "Format block: ");
811		for (i = 0; i < FORMAT_BLOCK_SIZE; i += 4) {
812			unsigned short foo, bar;
813			foo = nubus_get_rom(&rp, 2, bytelanes);
814			bar = nubus_get_rom(&rp, 2, bytelanes);
815			printk("%04x %04x  ", foo, bar);
816		}
817		printk("\n");
818		rp = board->fblock;
819	}
820
821	board->slot = slot;
822	board->slot_addr = (unsigned long) nubus_slot_addr(slot);
823	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
824	/* rom_length is *supposed* to be the total length of the
825	 * ROM.  In practice it is the "amount of ROM used to compute
826	 * the CRC."  So some jokers decide to set it to zero and
827	 * set the crc to zero so they don't have to do any math.
828	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
829	 */
830	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
831	board->crc = nubus_get_rom(&rp, 4, bytelanes);
832	board->rev = nubus_get_rom(&rp, 1, bytelanes);
833	board->format = nubus_get_rom(&rp,1, bytelanes);
834	board->lanes = bytelanes;
835
836	/* Directory offset should be small and negative... */
837	if(!(board->doffset & 0x00FF0000))
838		printk(KERN_WARNING "Dodgy doffset!\n");
839	dpat = nubus_get_rom(&rp, 4, bytelanes);
840	if(dpat != NUBUS_TEST_PATTERN)
841		printk(KERN_WARNING "Wrong test pattern %08lx!\n", dpat);
842
843	/*
844	 *	I wonder how the CRC is meant to work -
845	 *		any takers ?
846	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
847	 * since the initial Macintosh ROM releases skipped the check.
848	 */
849
850	nubus_find_rom_dir(board);
851	nubus_get_root_dir(board, &dir);
852
853	/* We're ready to rock */
854	printk(KERN_INFO "Slot %X:\n", slot);
855
856	/* Each slot should have one board resource and any number of
857	   functional resources.  So we'll fill in some fields in the
858	   struct nubus_board from the board resource, then walk down
859	   the list of functional resources, spinning out a nubus_dev
860	   for each of them. */
861	if (nubus_readdir(&dir, &ent) == -1) {
862		/* We can't have this! */
863		printk(KERN_ERR "Board resource not found!\n");
864		return NULL;
865	} else {
866		printk(KERN_INFO "  Board resource:\n");
867		nubus_get_board_resource(board, slot, &ent);
868	}
869
870	/* Aaaarrrrgghh!  The LC III motherboard has *two* board
871	   resources.  I have no idea WTF to do about this. */
872
873	while (nubus_readdir(&dir, &ent) != -1) {
874		struct nubus_dev*  dev;
875		struct nubus_dev** devp;
876		dev = nubus_get_functional_resource(board, slot, &ent);
877		if (dev == NULL)
878			continue;
879
880		/* We zeroed this out above */
881		if (board->first_dev == NULL)
882			board->first_dev = dev;
883
884		/* Put it on the global NuBus device chain. Keep entries in order. */
885		for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
886			/* spin */;
887		*devp = dev;
888		dev->next = NULL;
889	}
890
891	/* Put it on the global NuBus board chain. Keep entries in order. */
892	for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
893		/* spin */;
894	*boardp = board;
895	board->next = NULL;
896
897	return board;
898}
899
900void __init nubus_probe_slot(int slot)
901{
902	unsigned char dp;
903	unsigned char* rp;
904	int i;
905
906	rp = nubus_rom_addr(slot);
907	for(i = 4; i; i--)
908	{
909		unsigned long flags;
910		int card_present;
911
912		rp--;
913		local_irq_save(flags);
914		card_present = hwreg_present(rp);
915		local_irq_restore(flags);
916
917		if (!card_present)
918			continue;
919
920		printk(KERN_DEBUG "Now probing slot %X at %p\n", slot, rp);
921		dp = *rp;
922		if(dp == 0)
923			continue;
924
925		/* The last byte of the format block consists of two
926		   nybbles which are "mirror images" of each other.
927		   These show us the valid bytelanes */
928		if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
929			continue;
930		/* Check that this value is actually *on* one of the
931		   bytelanes it claims are valid! */
932		if ((dp & 0x0F) >= (1<<i))
933			continue;
934
935		/* Looks promising.  Let's put it on the list. */
936		nubus_add_board(slot, dp);
937
938		return;
939	}
940}
941
942#if defined(CONFIG_PROC_FS)
943
944/* /proc/nubus stuff */
945
946static int sprint_nubus_board(struct nubus_board* board, char* ptr, int len)
947{
948	if(len < 100)
949		return -1;
950
951	sprintf(ptr, "Slot %X: %s\n",
952		board->slot, board->name);
953
954	return strlen(ptr);
955}
956
957static int nubus_read_proc(char *page, char **start, off_t off,
958				int count, int *eof, void *data)
959{
960	int nprinted, len, begin = 0;
961	int size = PAGE_SIZE;
962	struct nubus_board* board;
963
964	len   = sprintf(page, "Nubus devices found:\n");
965	/* Walk the list of NuBus boards */
966	for (board = nubus_boards; board != NULL; board = board->next)
967	{
968		nprinted = sprint_nubus_board(board, page + len, size - len);
969		if (nprinted < 0)
970			break;
971		len += nprinted;
972		if (len+begin < off) {
973			begin += len;
974			len = 0;
975		}
976		if (len+begin >= off+count)
977			break;
978	}
979	if (len+begin < off)
980		*eof = 1;
981	off -= begin;
982	*start = page + off;
983	len -= off;
984	if (len>count)
985		len = count;
986	if (len<0)
987		len = 0;
988	return len;
989}
990#endif
991
992void __init nubus_scan_bus(void)
993{
994	int slot;
995	/* This might not work on your machine */
996#ifdef I_WANT_TO_PROBE_SLOT_ZERO
997	nubus_probe_slot(0);
998#endif
999	for(slot = 9; slot < 15; slot++)
1000	{
1001		nubus_probe_slot(slot);
1002	}
1003}
1004
1005static int __init nubus_init(void)
1006{
1007	if (!MACH_IS_MAC)
1008		return 0;
1009
1010	/* Initialize the NuBus interrupts */
1011	if (oss_present) {
1012		oss_nubus_init();
1013	} else {
1014		via_nubus_init();
1015	}
1016
1017#ifdef TRY_TO_DODGE_WSOD
1018	/* Rogue Ethernet interrupts can kill the machine if we don't
1019	   do this.  Obviously this is bogus.  Hopefully the local VIA
1020	   gurus can fix the real cause of the problem. */
1021	mdelay(1000);
1022#endif
1023
1024	/* And probe */
1025	printk("NuBus: Scanning NuBus slots.\n");
1026	nubus_devices = NULL;
1027	nubus_boards  = NULL;
1028	nubus_scan_bus();
1029
1030#ifdef CONFIG_PROC_FS
1031	create_proc_read_entry("nubus", 0, NULL, nubus_read_proc, NULL);
1032	nubus_proc_init();
1033#endif
1034	return 0;
1035}
1036
1037subsys_initcall(nubus_init);
1038