xref: /openbmc/linux/drivers/nubus/nubus.c (revision d7811a36)
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_device(unsigned short category, unsigned short type,
286 		  unsigned short dr_hw, unsigned short dr_sw,
287 		  const struct nubus_dev *from)
288 {
289 	struct nubus_dev *itor = from ? from->next : nubus_devices;
290 
291 	while (itor) {
292 		if (itor->category == category && itor->type == type &&
293 		    itor->dr_hw == dr_hw && itor->dr_sw == dr_sw)
294 			return itor;
295 		itor = itor->next;
296 	}
297 	return NULL;
298 }
299 EXPORT_SYMBOL(nubus_find_device);
300 
301 struct nubus_dev*
302 nubus_find_type(unsigned short category, unsigned short type,
303 		const struct nubus_dev *from)
304 {
305 	struct nubus_dev *itor = from ? from->next : nubus_devices;
306 
307 	while (itor) {
308 		if (itor->category == category && itor->type == type)
309 			return itor;
310 		itor = itor->next;
311 	}
312 	return NULL;
313 }
314 EXPORT_SYMBOL(nubus_find_type);
315 
316 struct nubus_dev*
317 nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
318 {
319 	struct nubus_dev *itor = from ? from->next : nubus_devices;
320 
321 	while (itor) {
322 		if (itor->board->slot == slot)
323 			return itor;
324 		itor = itor->next;
325 	}
326 	return NULL;
327 }
328 EXPORT_SYMBOL(nubus_find_slot);
329 
330 int
331 nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
332 		struct nubus_dirent *ent)
333 {
334 	while (nubus_readdir(dir, ent) != -1) {
335 		if (ent->type == rsrc_type)
336 			return 0;
337 	}
338 	return -1;
339 }
340 EXPORT_SYMBOL(nubus_find_rsrc);
341 
342 /* Initialization functions - decide which slots contain stuff worth
343    looking at, and print out lots and lots of information from the
344    resource blocks. */
345 
346 /* FIXME: A lot of this stuff will eventually be useful after
347    initialization, for intelligently probing Ethernet and video chips,
348    among other things.  The rest of it should go in the /proc code.
349    For now, we just use it to give verbose boot logs. */
350 
351 static int __init nubus_show_display_resource(struct nubus_dev *dev,
352 					      const struct nubus_dirent *ent)
353 {
354 	switch (ent->type) {
355 	case NUBUS_RESID_GAMMADIR:
356 		pr_debug("    gamma directory offset: 0x%06x\n", ent->data);
357 		break;
358 	case 0x0080 ... 0x0085:
359 		pr_debug("    mode 0x%02x info offset: 0x%06x\n",
360 			ent->type, ent->data);
361 		break;
362 	default:
363 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
364 			ent->type, ent->data);
365 	}
366 	return 0;
367 }
368 
369 static int __init nubus_show_network_resource(struct nubus_dev *dev,
370 					      const struct nubus_dirent *ent)
371 {
372 	switch (ent->type) {
373 	case NUBUS_RESID_MAC_ADDRESS:
374 	{
375 		char addr[6];
376 
377 		nubus_get_rsrc_mem(addr, ent, 6);
378 		pr_debug("    MAC address: %pM\n", addr);
379 		break;
380 	}
381 	default:
382 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
383 			ent->type, ent->data);
384 	}
385 	return 0;
386 }
387 
388 static int __init nubus_show_cpu_resource(struct nubus_dev *dev,
389 					  const struct nubus_dirent *ent)
390 {
391 	switch (ent->type) {
392 	case NUBUS_RESID_MEMINFO:
393 	{
394 		unsigned long meminfo[2];
395 
396 		nubus_get_rsrc_mem(&meminfo, ent, 8);
397 		pr_debug("    memory: [ 0x%08lx 0x%08lx ]\n",
398 			meminfo[0], meminfo[1]);
399 		break;
400 	}
401 	case NUBUS_RESID_ROMINFO:
402 	{
403 		unsigned long rominfo[2];
404 
405 		nubus_get_rsrc_mem(&rominfo, ent, 8);
406 		pr_debug("    ROM:    [ 0x%08lx 0x%08lx ]\n",
407 			rominfo[0], rominfo[1]);
408 		break;
409 	}
410 	default:
411 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
412 			ent->type, ent->data);
413 	}
414 	return 0;
415 }
416 
417 static int __init nubus_show_private_resource(struct nubus_dev *dev,
418 					      const struct nubus_dirent *ent)
419 {
420 	switch (dev->category) {
421 	case NUBUS_CAT_DISPLAY:
422 		nubus_show_display_resource(dev, ent);
423 		break;
424 	case NUBUS_CAT_NETWORK:
425 		nubus_show_network_resource(dev, ent);
426 		break;
427 	case NUBUS_CAT_CPU:
428 		nubus_show_cpu_resource(dev, ent);
429 		break;
430 	default:
431 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
432 			ent->type, ent->data);
433 	}
434 	return 0;
435 }
436 
437 static struct nubus_dev * __init
438 nubus_get_functional_resource(struct nubus_board *board, int slot,
439 			      const struct nubus_dirent *parent)
440 {
441 	struct nubus_dir dir;
442 	struct nubus_dirent ent;
443 	struct nubus_dev *dev;
444 
445 	pr_debug("  Functional resource 0x%02x:\n", parent->type);
446 	nubus_get_subdir(parent, &dir);
447 
448 	/* Actually we should probably panic if this fails */
449 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
450 		return NULL;
451 	dev->resid = parent->type;
452 	dev->directory = dir.base;
453 	dev->board = board;
454 
455 	while (nubus_readdir(&dir, &ent) != -1) {
456 		switch (ent.type) {
457 		case NUBUS_RESID_TYPE:
458 		{
459 			unsigned short nbtdata[4];
460 
461 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
462 			dev->category = nbtdata[0];
463 			dev->type     = nbtdata[1];
464 			dev->dr_sw    = nbtdata[2];
465 			dev->dr_hw    = nbtdata[3];
466 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
467 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
468 			break;
469 		}
470 		case NUBUS_RESID_NAME:
471 		{
472 			nubus_get_rsrc_str(dev->name, &ent, sizeof(dev->name));
473 			pr_debug("    name: %s\n", dev->name);
474 			break;
475 		}
476 		case NUBUS_RESID_DRVRDIR:
477 		{
478 			/* MacOS driver.  If we were NetBSD we might
479 			   use this :-) */
480 			struct nubus_dir drvr_dir;
481 			struct nubus_dirent drvr_ent;
482 
483 			nubus_get_subdir(&ent, &drvr_dir);
484 			nubus_readdir(&drvr_dir, &drvr_ent);
485 			dev->driver = nubus_dirptr(&drvr_ent);
486 			pr_debug("    driver at: 0x%p\n", dev->driver);
487 			break;
488 		}
489 		case NUBUS_RESID_MINOR_BASEOS:
490 			/* We will need this in order to support
491 			   multiple framebuffers.  It might be handy
492 			   for Ethernet as well */
493 			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
494 			pr_debug("    memory offset: 0x%08lx\n", dev->iobase);
495 			break;
496 		case NUBUS_RESID_MINOR_LENGTH:
497 			/* Ditto */
498 			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
499 			pr_debug("    memory length: 0x%08lx\n", dev->iosize);
500 			break;
501 		case NUBUS_RESID_FLAGS:
502 			dev->flags = ent.data;
503 			pr_debug("    flags: 0x%06x\n", dev->flags);
504 			break;
505 		case NUBUS_RESID_HWDEVID:
506 			dev->hwdevid = ent.data;
507 			pr_debug("    hwdevid: 0x%06x\n", dev->hwdevid);
508 			break;
509 		default:
510 			/* Local/Private resources have their own
511 			   function */
512 			nubus_show_private_resource(dev, &ent);
513 		}
514 	}
515 
516 	return dev;
517 }
518 
519 /* This is cool. */
520 static int __init nubus_get_vidnames(struct nubus_board *board,
521 				     const struct nubus_dirent *parent)
522 {
523 	struct nubus_dir dir;
524 	struct nubus_dirent ent;
525 
526 	/* FIXME: obviously we want to put this in a header file soon */
527 	struct vidmode {
528 		u32 size;
529 		/* Don't know what this is yet */
530 		u16 id;
531 		/* Longest one I've seen so far is 26 characters */
532 		char name[36];
533 	};
534 
535 	pr_debug("    video modes supported:\n");
536 	nubus_get_subdir(parent, &dir);
537 
538 	while (nubus_readdir(&dir, &ent) != -1) {
539 		struct vidmode mode;
540 		u32 size;
541 
542 		/* First get the length */
543 		nubus_get_rsrc_mem(&size, &ent, 4);
544 
545 		/* Now clobber the whole thing */
546 		if (size > sizeof(mode) - 1)
547 			size = sizeof(mode) - 1;
548 		memset(&mode, 0, sizeof(mode));
549 		nubus_get_rsrc_mem(&mode, &ent, size);
550 		pr_debug("      0x%02x: 0x%04x %s\n", ent.type,
551 			mode.id, mode.name);
552 	}
553 	return 0;
554 }
555 
556 /* This is *really* cool. */
557 static int __init nubus_get_icon(struct nubus_board *board,
558 				 const struct nubus_dirent *ent)
559 {
560 	/* Should be 32x32 if my memory serves me correctly */
561 	u32 icon[32];
562 	int i;
563 
564 	nubus_get_rsrc_mem(&icon, ent, 128);
565 	pr_debug("    icon:\n");
566 	for (i = 0; i < 8; i++)
567 		pr_debug("        %08x %08x %08x %08x\n",
568 			icon[i * 4 + 0], icon[i * 4 + 1],
569 			icon[i * 4 + 2], icon[i * 4 + 3]);
570 
571 	return 0;
572 }
573 
574 static int __init nubus_get_vendorinfo(struct nubus_board *board,
575 				       const struct nubus_dirent *parent)
576 {
577 	struct nubus_dir dir;
578 	struct nubus_dirent ent;
579 	static char *vendor_fields[6] = { "ID", "serial", "revision",
580 	                                  "part", "date", "unknown field" };
581 
582 	pr_debug("    vendor info:\n");
583 	nubus_get_subdir(parent, &dir);
584 
585 	while (nubus_readdir(&dir, &ent) != -1) {
586 		char name[64];
587 
588 		/* These are all strings, we think */
589 		nubus_get_rsrc_str(name, &ent, sizeof(name));
590 		if (ent.type < 1 || ent.type > 5)
591 			ent.type = 5;
592 		pr_debug("    %s: %s\n", vendor_fields[ent.type - 1], name);
593 	}
594 	return 0;
595 }
596 
597 static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
598 					   const struct nubus_dirent *parent)
599 {
600 	struct nubus_dir dir;
601 	struct nubus_dirent ent;
602 
603 	pr_debug("  Board resource 0x%02x:\n", parent->type);
604 	nubus_get_subdir(parent, &dir);
605 
606 	while (nubus_readdir(&dir, &ent) != -1) {
607 		switch (ent.type) {
608 		case NUBUS_RESID_TYPE:
609 		{
610 			unsigned short nbtdata[4];
611 			/* This type is always the same, and is not
612 			   useful except insofar as it tells us that
613 			   we really are looking at a board resource. */
614 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
615 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
616 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
617 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
618 			    nbtdata[2] != 0 || nbtdata[3] != 0)
619 				pr_err("Slot %X: sResource is not a board resource!\n",
620 				       slot);
621 			break;
622 		}
623 		case NUBUS_RESID_NAME:
624 			nubus_get_rsrc_str(board->name, &ent,
625 					   sizeof(board->name));
626 			pr_debug("    name: %s\n", board->name);
627 			break;
628 		case NUBUS_RESID_ICON:
629 			nubus_get_icon(board, &ent);
630 			break;
631 		case NUBUS_RESID_BOARDID:
632 			pr_debug("    board id: 0x%x\n", ent.data);
633 			break;
634 		case NUBUS_RESID_PRIMARYINIT:
635 			pr_debug("    primary init offset: 0x%06x\n", ent.data);
636 			break;
637 		case NUBUS_RESID_VENDORINFO:
638 			nubus_get_vendorinfo(board, &ent);
639 			break;
640 		case NUBUS_RESID_FLAGS:
641 			pr_debug("    flags: 0x%06x\n", ent.data);
642 			break;
643 		case NUBUS_RESID_HWDEVID:
644 			pr_debug("    hwdevid: 0x%06x\n", ent.data);
645 			break;
646 		case NUBUS_RESID_SECONDINIT:
647 			pr_debug("    secondary init offset: 0x%06x\n",
648 				 ent.data);
649 			break;
650 			/* WTF isn't this in the functional resources? */
651 		case NUBUS_RESID_VIDNAMES:
652 			nubus_get_vidnames(board, &ent);
653 			break;
654 			/* Same goes for this */
655 		case NUBUS_RESID_VIDMODES:
656 			pr_debug("    video mode parameter directory offset: 0x%06x\n",
657 				ent.data);
658 			break;
659 		default:
660 			pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
661 				ent.type, ent.data);
662 		}
663 	}
664 	return 0;
665 }
666 
667 /* Add a board (might be many devices) to the list */
668 static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
669 {
670 	struct nubus_board *board;
671 	struct nubus_board **boardp;
672 	unsigned char *rp;
673 	unsigned long dpat;
674 	struct nubus_dir dir;
675 	struct nubus_dirent ent;
676 	int prev_resid = -1;
677 
678 	/* Move to the start of the format block */
679 	rp = nubus_rom_addr(slot);
680 	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
681 
682 	/* Actually we should probably panic if this fails */
683 	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
684 		return NULL;
685 	board->fblock = rp;
686 
687 	/* Dump the format block for debugging purposes */
688 	pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
689 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
690 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
691 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
692 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
693 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
694 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
695 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
696 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
697 	rp = board->fblock;
698 
699 	board->slot = slot;
700 	board->slot_addr = (unsigned long)nubus_slot_addr(slot);
701 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
702 	/* rom_length is *supposed* to be the total length of the
703 	 * ROM.  In practice it is the "amount of ROM used to compute
704 	 * the CRC."  So some jokers decide to set it to zero and
705 	 * set the crc to zero so they don't have to do any math.
706 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
707 	 */
708 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
709 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
710 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
711 	board->format = nubus_get_rom(&rp, 1, bytelanes);
712 	board->lanes = bytelanes;
713 
714 	/* Directory offset should be small and negative... */
715 	if (!(board->doffset & 0x00FF0000))
716 		pr_warn("Slot %X: Dodgy doffset!\n", slot);
717 	dpat = nubus_get_rom(&rp, 4, bytelanes);
718 	if (dpat != NUBUS_TEST_PATTERN)
719 		pr_warn("Slot %X: Wrong test pattern %08lx!\n", slot, dpat);
720 
721 	/*
722 	 *	I wonder how the CRC is meant to work -
723 	 *		any takers ?
724 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
725 	 * since the initial Macintosh ROM releases skipped the check.
726 	 */
727 
728 	/* Set up the directory pointer */
729 	board->directory = board->fblock;
730 	nubus_move(&board->directory, nubus_expand32(board->doffset),
731 	           board->lanes);
732 
733 	nubus_get_root_dir(board, &dir);
734 
735 	/* We're ready to rock */
736 	pr_debug("Slot %X resources:\n", slot);
737 
738 	/* Each slot should have one board resource and any number of
739 	   functional resources.  So we'll fill in some fields in the
740 	   struct nubus_board from the board resource, then walk down
741 	   the list of functional resources, spinning out a nubus_dev
742 	   for each of them. */
743 	if (nubus_readdir(&dir, &ent) == -1) {
744 		/* We can't have this! */
745 		pr_err("Slot %X: Board resource not found!\n", slot);
746 		return NULL;
747 	}
748 
749 	if (ent.type < 1 || ent.type > 127)
750 		pr_warn("Slot %X: Board resource ID is invalid!\n", slot);
751 
752 	nubus_get_board_resource(board, slot, &ent);
753 
754 	while (nubus_readdir(&dir, &ent) != -1) {
755 		struct nubus_dev *dev;
756 		struct nubus_dev **devp;
757 
758 		dev = nubus_get_functional_resource(board, slot, &ent);
759 		if (dev == NULL)
760 			continue;
761 
762 		/* Resources should appear in ascending ID order. This sanity
763 		 * check prevents duplicate resource IDs.
764 		 */
765 		if (dev->resid <= prev_resid) {
766 			kfree(dev);
767 			continue;
768 		}
769 		prev_resid = dev->resid;
770 
771 		/* We zeroed this out above */
772 		if (board->first_dev == NULL)
773 			board->first_dev = dev;
774 
775 		/* Put it on the global NuBus device chain. Keep entries in order. */
776 		for (devp = &nubus_devices; *devp != NULL;
777 		     devp = &((*devp)->next))
778 			/* spin */;
779 		*devp = dev;
780 		dev->next = NULL;
781 	}
782 
783 	/* Put it on the global NuBus board chain. Keep entries in order. */
784 	for (boardp = &nubus_boards; *boardp != NULL;
785 	     boardp = &((*boardp)->next))
786 		/* spin */;
787 	*boardp = board;
788 	board->next = NULL;
789 
790 	return board;
791 }
792 
793 static void __init nubus_probe_slot(int slot)
794 {
795 	unsigned char dp;
796 	unsigned char *rp;
797 	int i;
798 
799 	rp = nubus_rom_addr(slot);
800 	for (i = 4; i; i--) {
801 		int card_present;
802 
803 		rp--;
804 		card_present = hwreg_present(rp);
805 		if (!card_present)
806 			continue;
807 
808 		dp = *rp;
809 
810 		/* The last byte of the format block consists of two
811 		   nybbles which are "mirror images" of each other.
812 		   These show us the valid bytelanes */
813 		if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
814 			continue;
815 		/* Check that this value is actually *on* one of the
816 		   bytelanes it claims are valid! */
817 		if (not_useful(rp, dp))
818 			continue;
819 
820 		/* Looks promising.  Let's put it on the list. */
821 		nubus_add_board(slot, dp);
822 
823 		return;
824 	}
825 }
826 
827 static void __init nubus_scan_bus(void)
828 {
829 	int slot;
830 
831 	pr_info("NuBus: Scanning NuBus slots.\n");
832 	for (slot = 9; slot < 15; slot++) {
833 		nubus_probe_slot(slot);
834 	}
835 }
836 
837 static int __init nubus_init(void)
838 {
839 	if (!MACH_IS_MAC)
840 		return 0;
841 
842 	nubus_devices = NULL;
843 	nubus_boards = NULL;
844 	nubus_scan_bus();
845 	nubus_proc_init();
846 	return 0;
847 }
848 
849 subsys_initcall(nubus_init);
850