xref: /openbmc/linux/drivers/nubus/nubus.c (revision 9f97977d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	Macintosh Nubus Interface Code
4  *
5  *      Originally by Alan Cox
6  *
7  *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
8  *      and others.
9  */
10 
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/nubus.h>
15 #include <linux/errno.h>
16 #include <linux/init.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 
23 /* Constants */
24 
25 /* This is, of course, the size in bytelanes, rather than the size in
26    actual bytes */
27 #define FORMAT_BLOCK_SIZE 20
28 #define ROM_DIR_OFFSET 0x24
29 
30 #define NUBUS_TEST_PATTERN 0x5A932BC7
31 
32 /* Globals */
33 
34 struct nubus_dev *nubus_devices;
35 struct nubus_board *nubus_boards;
36 
37 /* Meaning of "bytelanes":
38 
39    The card ROM may appear on any or all bytes of each long word in
40    NuBus memory.  The low 4 bits of the "map" value found in the
41    format block (at the top of the slot address space, as well as at
42    the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
43    offsets within each longword, are valid.  Thus:
44 
45    A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
46    are valid.
47 
48    A map of 0xf0 means that no bytelanes are valid (We pray that we
49    will never encounter this, but stranger things have happened)
50 
51    A map of 0xe1 means that only the MSB of each long word is actually
52    part of the card ROM.  (We hope to never encounter NuBus on a
53    little-endian machine.  Again, stranger things have happened)
54 
55    A map of 0x78 means that only the LSB of each long word is valid.
56 
57    Etcetera, etcetera.  Hopefully this clears up some confusion over
58    what the following code actually does.  */
59 
60 static inline int not_useful(void *p, int map)
61 {
62 	unsigned long pv = (unsigned long)p;
63 
64 	pv &= 3;
65 	if (map & (1 << pv))
66 		return 0;
67 	return 1;
68 }
69 
70 static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
71 {
72 	/* This will hold the result */
73 	unsigned long v = 0;
74 	unsigned char *p = *ptr;
75 
76 	while (len) {
77 		v <<= 8;
78 		while (not_useful(p, map))
79 			p++;
80 		v |= *p++;
81 		len--;
82 	}
83 	*ptr = p;
84 	return v;
85 }
86 
87 static void nubus_rewind(unsigned char **ptr, int len, int map)
88 {
89 	unsigned char *p = *ptr;
90 
91 	while (len) {
92 		do {
93 			p--;
94 		} while (not_useful(p, map));
95 		len--;
96 	}
97 	*ptr = p;
98 }
99 
100 static void nubus_advance(unsigned char **ptr, int len, int map)
101 {
102 	unsigned char *p = *ptr;
103 
104 	while (len) {
105 		while (not_useful(p, map))
106 			p++;
107 		p++;
108 		len--;
109 	}
110 	*ptr = p;
111 }
112 
113 static void nubus_move(unsigned char **ptr, int len, int map)
114 {
115 	unsigned long slot_space = (unsigned long)*ptr & 0xFF000000;
116 
117 	if (len > 0)
118 		nubus_advance(ptr, len, map);
119 	else if (len < 0)
120 		nubus_rewind(ptr, -len, map);
121 
122 	if (((unsigned long)*ptr & 0xFF000000) != slot_space)
123 		pr_err("%s: moved out of slot address space!\n", __func__);
124 }
125 
126 /* Now, functions to read the sResource tree */
127 
128 /* Each sResource entry consists of a 1-byte ID and a 3-byte data
129    field.  If that data field contains an offset, then obviously we
130    have to expand it from a 24-bit signed number to a 32-bit signed
131    number. */
132 
133 static inline long nubus_expand32(long foo)
134 {
135 	if (foo & 0x00800000)	/* 24bit negative */
136 		foo |= 0xFF000000;
137 	return foo;
138 }
139 
140 static inline void *nubus_rom_addr(int slot)
141 {
142 	/*
143 	 *	Returns the first byte after the card. We then walk
144 	 *	backwards to get the lane register and the config
145 	 */
146 	return (void *)(0xF1000000 + (slot << 24));
147 }
148 
149 static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
150 {
151 	unsigned char *p = nd->base;
152 
153 	/* Essentially, just step over the bytelanes using whatever
154 	   offset we might have found */
155 	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
156 	/* And return the value */
157 	return p;
158 }
159 
160 /* These two are for pulling resource data blocks (i.e. stuff that's
161    pointed to with offsets) out of the card ROM. */
162 
163 void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
164 			unsigned int len)
165 {
166 	unsigned char *t = (unsigned char *)dest;
167 	unsigned char *p = nubus_dirptr(dirent);
168 
169 	while (len) {
170 		*t++ = nubus_get_rom(&p, 1, dirent->mask);
171 		len--;
172 	}
173 }
174 EXPORT_SYMBOL(nubus_get_rsrc_mem);
175 
176 void nubus_get_rsrc_str(char *dest, const struct nubus_dirent *dirent,
177 			unsigned int len)
178 {
179 	char *t = dest;
180 	unsigned char *p = nubus_dirptr(dirent);
181 
182 	while (len > 1) {
183 		unsigned char c = nubus_get_rom(&p, 1, dirent->mask);
184 
185 		if (!c)
186 			break;
187 		*t++ = c;
188 		len--;
189 	}
190 	if (len > 0)
191 		*t = '\0';
192 }
193 EXPORT_SYMBOL(nubus_get_rsrc_str);
194 
195 int nubus_get_root_dir(const struct nubus_board *board,
196 		       struct nubus_dir *dir)
197 {
198 	dir->ptr = dir->base = board->directory;
199 	dir->done = 0;
200 	dir->mask = board->lanes;
201 	return 0;
202 }
203 EXPORT_SYMBOL(nubus_get_root_dir);
204 
205 /* This is a slyly renamed version of the above */
206 int nubus_get_func_dir(const struct nubus_dev *dev,
207 		       struct nubus_dir *dir)
208 {
209 	dir->ptr = dir->base = dev->directory;
210 	dir->done = 0;
211 	dir->mask = dev->board->lanes;
212 	return 0;
213 }
214 EXPORT_SYMBOL(nubus_get_func_dir);
215 
216 int nubus_get_board_dir(const struct nubus_board *board,
217 			struct nubus_dir *dir)
218 {
219 	struct nubus_dirent ent;
220 
221 	dir->ptr = dir->base = board->directory;
222 	dir->done = 0;
223 	dir->mask = board->lanes;
224 
225 	/* Now dereference it (the first directory is always the board
226 	   directory) */
227 	if (nubus_readdir(dir, &ent) == -1)
228 		return -1;
229 	if (nubus_get_subdir(&ent, dir) == -1)
230 		return -1;
231 	return 0;
232 }
233 EXPORT_SYMBOL(nubus_get_board_dir);
234 
235 int nubus_get_subdir(const struct nubus_dirent *ent,
236 		     struct nubus_dir *dir)
237 {
238 	dir->ptr = dir->base = nubus_dirptr(ent);
239 	dir->done = 0;
240 	dir->mask = ent->mask;
241 	return 0;
242 }
243 EXPORT_SYMBOL(nubus_get_subdir);
244 
245 int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
246 {
247 	u32 resid;
248 
249 	if (nd->done)
250 		return -1;
251 
252 	/* Do this first, otherwise nubus_rewind & co are off by 4 */
253 	ent->base = nd->ptr;
254 
255 	/* This moves nd->ptr forward */
256 	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
257 
258 	/* EOL marker, as per the Apple docs */
259 	if ((resid & 0xff000000) == 0xff000000) {
260 		/* Mark it as done */
261 		nd->done = 1;
262 		return -1;
263 	}
264 
265 	/* First byte is the resource ID */
266 	ent->type = resid >> 24;
267 	/* Low 3 bytes might contain data (or might not) */
268 	ent->data = resid & 0xffffff;
269 	ent->mask = nd->mask;
270 	return 0;
271 }
272 EXPORT_SYMBOL(nubus_readdir);
273 
274 int nubus_rewinddir(struct nubus_dir *dir)
275 {
276 	dir->ptr = dir->base;
277 	dir->done = 0;
278 	return 0;
279 }
280 EXPORT_SYMBOL(nubus_rewinddir);
281 
282 /* Driver interface functions, more or less like in pci.c */
283 
284 struct nubus_dev*
285 nubus_find_type(unsigned short category, unsigned short type,
286 		const struct nubus_dev *from)
287 {
288 	struct nubus_dev *itor = from ? from->next : nubus_devices;
289 
290 	while (itor) {
291 		if (itor->category == category && itor->type == type)
292 			return itor;
293 		itor = itor->next;
294 	}
295 	return NULL;
296 }
297 EXPORT_SYMBOL(nubus_find_type);
298 
299 struct nubus_dev*
300 nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
301 {
302 	struct nubus_dev *itor = from ? from->next : nubus_devices;
303 
304 	while (itor) {
305 		if (itor->board->slot == slot)
306 			return itor;
307 		itor = itor->next;
308 	}
309 	return NULL;
310 }
311 EXPORT_SYMBOL(nubus_find_slot);
312 
313 int
314 nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
315 		struct nubus_dirent *ent)
316 {
317 	while (nubus_readdir(dir, ent) != -1) {
318 		if (ent->type == rsrc_type)
319 			return 0;
320 	}
321 	return -1;
322 }
323 EXPORT_SYMBOL(nubus_find_rsrc);
324 
325 /* Initialization functions - decide which slots contain stuff worth
326    looking at, and print out lots and lots of information from the
327    resource blocks. */
328 
329 /* FIXME: A lot of this stuff will eventually be useful after
330    initialization, for intelligently probing Ethernet and video chips,
331    among other things.  The rest of it should go in the /proc code.
332    For now, we just use it to give verbose boot logs. */
333 
334 static int __init nubus_show_display_resource(struct nubus_dev *dev,
335 					      const struct nubus_dirent *ent)
336 {
337 	switch (ent->type) {
338 	case NUBUS_RESID_GAMMADIR:
339 		pr_debug("    gamma directory offset: 0x%06x\n", ent->data);
340 		break;
341 	case 0x0080 ... 0x0085:
342 		pr_debug("    mode 0x%02x info offset: 0x%06x\n",
343 			ent->type, ent->data);
344 		break;
345 	default:
346 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
347 			ent->type, ent->data);
348 	}
349 	return 0;
350 }
351 
352 static int __init nubus_show_network_resource(struct nubus_dev *dev,
353 					      const struct nubus_dirent *ent)
354 {
355 	switch (ent->type) {
356 	case NUBUS_RESID_MAC_ADDRESS:
357 	{
358 		char addr[6];
359 
360 		nubus_get_rsrc_mem(addr, ent, 6);
361 		pr_debug("    MAC address: %pM\n", addr);
362 		break;
363 	}
364 	default:
365 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
366 			ent->type, ent->data);
367 	}
368 	return 0;
369 }
370 
371 static int __init nubus_show_cpu_resource(struct nubus_dev *dev,
372 					  const struct nubus_dirent *ent)
373 {
374 	switch (ent->type) {
375 	case NUBUS_RESID_MEMINFO:
376 	{
377 		unsigned long meminfo[2];
378 
379 		nubus_get_rsrc_mem(&meminfo, ent, 8);
380 		pr_debug("    memory: [ 0x%08lx 0x%08lx ]\n",
381 			meminfo[0], meminfo[1]);
382 		break;
383 	}
384 	case NUBUS_RESID_ROMINFO:
385 	{
386 		unsigned long rominfo[2];
387 
388 		nubus_get_rsrc_mem(&rominfo, ent, 8);
389 		pr_debug("    ROM:    [ 0x%08lx 0x%08lx ]\n",
390 			rominfo[0], rominfo[1]);
391 		break;
392 	}
393 	default:
394 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
395 			ent->type, ent->data);
396 	}
397 	return 0;
398 }
399 
400 static int __init nubus_show_private_resource(struct nubus_dev *dev,
401 					      const struct nubus_dirent *ent)
402 {
403 	switch (dev->category) {
404 	case NUBUS_CAT_DISPLAY:
405 		nubus_show_display_resource(dev, ent);
406 		break;
407 	case NUBUS_CAT_NETWORK:
408 		nubus_show_network_resource(dev, ent);
409 		break;
410 	case NUBUS_CAT_CPU:
411 		nubus_show_cpu_resource(dev, ent);
412 		break;
413 	default:
414 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
415 			ent->type, ent->data);
416 	}
417 	return 0;
418 }
419 
420 static struct nubus_dev * __init
421 nubus_get_functional_resource(struct nubus_board *board, int slot,
422 			      const struct nubus_dirent *parent)
423 {
424 	struct nubus_dir dir;
425 	struct nubus_dirent ent;
426 	struct nubus_dev *dev;
427 
428 	pr_debug("  Functional resource 0x%02x:\n", parent->type);
429 	nubus_get_subdir(parent, &dir);
430 
431 	/* Actually we should probably panic if this fails */
432 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
433 		return NULL;
434 	dev->resid = parent->type;
435 	dev->directory = dir.base;
436 	dev->board = board;
437 
438 	while (nubus_readdir(&dir, &ent) != -1) {
439 		switch (ent.type) {
440 		case NUBUS_RESID_TYPE:
441 		{
442 			unsigned short nbtdata[4];
443 
444 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
445 			dev->category = nbtdata[0];
446 			dev->type     = nbtdata[1];
447 			dev->dr_sw    = nbtdata[2];
448 			dev->dr_hw    = nbtdata[3];
449 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
450 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
451 			break;
452 		}
453 		case NUBUS_RESID_NAME:
454 		{
455 			char name[64];
456 
457 			nubus_get_rsrc_str(name, &ent, sizeof(name));
458 			pr_debug("    name: %s\n", name);
459 			break;
460 		}
461 		case NUBUS_RESID_DRVRDIR:
462 		{
463 			/* MacOS driver.  If we were NetBSD we might
464 			   use this :-) */
465 			struct nubus_dir drvr_dir;
466 			struct nubus_dirent drvr_ent;
467 			unsigned char *driver;
468 
469 			nubus_get_subdir(&ent, &drvr_dir);
470 			nubus_readdir(&drvr_dir, &drvr_ent);
471 			driver = nubus_dirptr(&drvr_ent);
472 			pr_debug("    driver at: 0x%p\n", driver);
473 			break;
474 		}
475 		case NUBUS_RESID_MINOR_BASEOS:
476 		{
477 			/* We will need this in order to support
478 			   multiple framebuffers.  It might be handy
479 			   for Ethernet as well */
480 			u32 base_offset;
481 
482 			nubus_get_rsrc_mem(&base_offset, &ent, 4);
483 			pr_debug("    memory offset: 0x%08x\n", base_offset);
484 			break;
485 		}
486 		case NUBUS_RESID_MINOR_LENGTH:
487 		{
488 			/* Ditto */
489 			u32 length;
490 
491 			nubus_get_rsrc_mem(&length, &ent, 4);
492 			pr_debug("    memory length: 0x%08x\n", length);
493 			break;
494 		}
495 		case NUBUS_RESID_FLAGS:
496 			pr_debug("    flags: 0x%06x\n", ent.data);
497 			break;
498 		case NUBUS_RESID_HWDEVID:
499 			pr_debug("    hwdevid: 0x%06x\n", ent.data);
500 			break;
501 		default:
502 			/* Local/Private resources have their own
503 			   function */
504 			nubus_show_private_resource(dev, &ent);
505 		}
506 	}
507 
508 	return dev;
509 }
510 
511 /* This is cool. */
512 static int __init nubus_get_vidnames(struct nubus_board *board,
513 				     const struct nubus_dirent *parent)
514 {
515 	struct nubus_dir dir;
516 	struct nubus_dirent ent;
517 
518 	/* FIXME: obviously we want to put this in a header file soon */
519 	struct vidmode {
520 		u32 size;
521 		/* Don't know what this is yet */
522 		u16 id;
523 		/* Longest one I've seen so far is 26 characters */
524 		char name[36];
525 	};
526 
527 	pr_debug("    video modes supported:\n");
528 	nubus_get_subdir(parent, &dir);
529 
530 	while (nubus_readdir(&dir, &ent) != -1) {
531 		struct vidmode mode;
532 		u32 size;
533 
534 		/* First get the length */
535 		nubus_get_rsrc_mem(&size, &ent, 4);
536 
537 		/* Now clobber the whole thing */
538 		if (size > sizeof(mode) - 1)
539 			size = sizeof(mode) - 1;
540 		memset(&mode, 0, sizeof(mode));
541 		nubus_get_rsrc_mem(&mode, &ent, size);
542 		pr_debug("      0x%02x: 0x%04x %s\n", ent.type,
543 			mode.id, mode.name);
544 	}
545 	return 0;
546 }
547 
548 /* This is *really* cool. */
549 static int __init nubus_get_icon(struct nubus_board *board,
550 				 const struct nubus_dirent *ent)
551 {
552 	/* Should be 32x32 if my memory serves me correctly */
553 	u32 icon[32];
554 	int i;
555 
556 	nubus_get_rsrc_mem(&icon, ent, 128);
557 	pr_debug("    icon:\n");
558 	for (i = 0; i < 8; i++)
559 		pr_debug("        %08x %08x %08x %08x\n",
560 			icon[i * 4 + 0], icon[i * 4 + 1],
561 			icon[i * 4 + 2], icon[i * 4 + 3]);
562 
563 	return 0;
564 }
565 
566 static int __init nubus_get_vendorinfo(struct nubus_board *board,
567 				       const struct nubus_dirent *parent)
568 {
569 	struct nubus_dir dir;
570 	struct nubus_dirent ent;
571 	static char *vendor_fields[6] = { "ID", "serial", "revision",
572 	                                  "part", "date", "unknown field" };
573 
574 	pr_debug("    vendor info:\n");
575 	nubus_get_subdir(parent, &dir);
576 
577 	while (nubus_readdir(&dir, &ent) != -1) {
578 		char name[64];
579 
580 		/* These are all strings, we think */
581 		nubus_get_rsrc_str(name, &ent, sizeof(name));
582 		if (ent.type < 1 || ent.type > 5)
583 			ent.type = 5;
584 		pr_debug("    %s: %s\n", vendor_fields[ent.type - 1], name);
585 	}
586 	return 0;
587 }
588 
589 static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
590 					   const struct nubus_dirent *parent)
591 {
592 	struct nubus_dir dir;
593 	struct nubus_dirent ent;
594 
595 	pr_debug("  Board resource 0x%02x:\n", parent->type);
596 	nubus_get_subdir(parent, &dir);
597 
598 	while (nubus_readdir(&dir, &ent) != -1) {
599 		switch (ent.type) {
600 		case NUBUS_RESID_TYPE:
601 		{
602 			unsigned short nbtdata[4];
603 			/* This type is always the same, and is not
604 			   useful except insofar as it tells us that
605 			   we really are looking at a board resource. */
606 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
607 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
608 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
609 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
610 			    nbtdata[2] != 0 || nbtdata[3] != 0)
611 				pr_err("Slot %X: sResource is not a board resource!\n",
612 				       slot);
613 			break;
614 		}
615 		case NUBUS_RESID_NAME:
616 			nubus_get_rsrc_str(board->name, &ent,
617 					   sizeof(board->name));
618 			pr_debug("    name: %s\n", board->name);
619 			break;
620 		case NUBUS_RESID_ICON:
621 			nubus_get_icon(board, &ent);
622 			break;
623 		case NUBUS_RESID_BOARDID:
624 			pr_debug("    board id: 0x%x\n", ent.data);
625 			break;
626 		case NUBUS_RESID_PRIMARYINIT:
627 			pr_debug("    primary init offset: 0x%06x\n", ent.data);
628 			break;
629 		case NUBUS_RESID_VENDORINFO:
630 			nubus_get_vendorinfo(board, &ent);
631 			break;
632 		case NUBUS_RESID_FLAGS:
633 			pr_debug("    flags: 0x%06x\n", ent.data);
634 			break;
635 		case NUBUS_RESID_HWDEVID:
636 			pr_debug("    hwdevid: 0x%06x\n", ent.data);
637 			break;
638 		case NUBUS_RESID_SECONDINIT:
639 			pr_debug("    secondary init offset: 0x%06x\n",
640 				 ent.data);
641 			break;
642 			/* WTF isn't this in the functional resources? */
643 		case NUBUS_RESID_VIDNAMES:
644 			nubus_get_vidnames(board, &ent);
645 			break;
646 			/* Same goes for this */
647 		case NUBUS_RESID_VIDMODES:
648 			pr_debug("    video mode parameter directory offset: 0x%06x\n",
649 				ent.data);
650 			break;
651 		default:
652 			pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
653 				ent.type, ent.data);
654 		}
655 	}
656 	return 0;
657 }
658 
659 /* Add a board (might be many devices) to the list */
660 static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
661 {
662 	struct nubus_board *board;
663 	struct nubus_board **boardp;
664 	unsigned char *rp;
665 	unsigned long dpat;
666 	struct nubus_dir dir;
667 	struct nubus_dirent ent;
668 	int prev_resid = -1;
669 
670 	/* Move to the start of the format block */
671 	rp = nubus_rom_addr(slot);
672 	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
673 
674 	/* Actually we should probably panic if this fails */
675 	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
676 		return NULL;
677 	board->fblock = rp;
678 
679 	/* Dump the format block for debugging purposes */
680 	pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
681 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
682 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
683 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
684 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
685 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
686 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
687 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
688 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
689 	rp = board->fblock;
690 
691 	board->slot = slot;
692 	board->slot_addr = (unsigned long)nubus_slot_addr(slot);
693 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
694 	/* rom_length is *supposed* to be the total length of the
695 	 * ROM.  In practice it is the "amount of ROM used to compute
696 	 * the CRC."  So some jokers decide to set it to zero and
697 	 * set the crc to zero so they don't have to do any math.
698 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
699 	 */
700 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
701 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
702 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
703 	board->format = nubus_get_rom(&rp, 1, bytelanes);
704 	board->lanes = bytelanes;
705 
706 	/* Directory offset should be small and negative... */
707 	if (!(board->doffset & 0x00FF0000))
708 		pr_warn("Slot %X: Dodgy doffset!\n", slot);
709 	dpat = nubus_get_rom(&rp, 4, bytelanes);
710 	if (dpat != NUBUS_TEST_PATTERN)
711 		pr_warn("Slot %X: Wrong test pattern %08lx!\n", slot, dpat);
712 
713 	/*
714 	 *	I wonder how the CRC is meant to work -
715 	 *		any takers ?
716 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
717 	 * since the initial Macintosh ROM releases skipped the check.
718 	 */
719 
720 	/* Set up the directory pointer */
721 	board->directory = board->fblock;
722 	nubus_move(&board->directory, nubus_expand32(board->doffset),
723 	           board->lanes);
724 
725 	nubus_get_root_dir(board, &dir);
726 
727 	/* We're ready to rock */
728 	pr_debug("Slot %X resources:\n", slot);
729 
730 	/* Each slot should have one board resource and any number of
731 	   functional resources.  So we'll fill in some fields in the
732 	   struct nubus_board from the board resource, then walk down
733 	   the list of functional resources, spinning out a nubus_dev
734 	   for each of them. */
735 	if (nubus_readdir(&dir, &ent) == -1) {
736 		/* We can't have this! */
737 		pr_err("Slot %X: Board resource not found!\n", slot);
738 		return NULL;
739 	}
740 
741 	if (ent.type < 1 || ent.type > 127)
742 		pr_warn("Slot %X: Board resource ID is invalid!\n", slot);
743 
744 	nubus_get_board_resource(board, slot, &ent);
745 
746 	while (nubus_readdir(&dir, &ent) != -1) {
747 		struct nubus_dev *dev;
748 		struct nubus_dev **devp;
749 
750 		dev = nubus_get_functional_resource(board, slot, &ent);
751 		if (dev == NULL)
752 			continue;
753 
754 		/* Resources should appear in ascending ID order. This sanity
755 		 * check prevents duplicate resource IDs.
756 		 */
757 		if (dev->resid <= prev_resid) {
758 			kfree(dev);
759 			continue;
760 		}
761 		prev_resid = dev->resid;
762 
763 		/* We zeroed this out above */
764 		if (board->first_dev == NULL)
765 			board->first_dev = dev;
766 
767 		/* Put it on the global NuBus device chain. Keep entries in order. */
768 		for (devp = &nubus_devices; *devp != NULL;
769 		     devp = &((*devp)->next))
770 			/* spin */;
771 		*devp = dev;
772 		dev->next = NULL;
773 	}
774 
775 	/* Put it on the global NuBus board chain. Keep entries in order. */
776 	for (boardp = &nubus_boards; *boardp != NULL;
777 	     boardp = &((*boardp)->next))
778 		/* spin */;
779 	*boardp = board;
780 	board->next = NULL;
781 
782 	return board;
783 }
784 
785 static void __init nubus_probe_slot(int slot)
786 {
787 	unsigned char dp;
788 	unsigned char *rp;
789 	int i;
790 
791 	rp = nubus_rom_addr(slot);
792 	for (i = 4; i; i--) {
793 		rp--;
794 		if (!hwreg_present(rp))
795 			continue;
796 
797 		dp = *rp;
798 
799 		/* The last byte of the format block consists of two
800 		   nybbles which are "mirror images" of each other.
801 		   These show us the valid bytelanes */
802 		if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
803 			continue;
804 		/* Check that this value is actually *on* one of the
805 		   bytelanes it claims are valid! */
806 		if (not_useful(rp, dp))
807 			continue;
808 
809 		/* Looks promising.  Let's put it on the list. */
810 		nubus_add_board(slot, dp);
811 
812 		return;
813 	}
814 }
815 
816 static void __init nubus_scan_bus(void)
817 {
818 	int slot;
819 
820 	pr_info("NuBus: Scanning NuBus slots.\n");
821 	for (slot = 9; slot < 15; slot++) {
822 		nubus_probe_slot(slot);
823 	}
824 }
825 
826 static int __init nubus_init(void)
827 {
828 	if (!MACH_IS_MAC)
829 		return 0;
830 
831 	nubus_scan_bus();
832 	nubus_proc_init();
833 	return 0;
834 }
835 
836 subsys_initcall(nubus_init);
837