xref: /linux/drivers/nubus/nubus.c (revision 883b8cb31a8546b9921c98b255d5f7779d1bc9f6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Macintosh Nubus Interface Code
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *      Originally by Alan Cox
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
81da177e4SLinus Torvalds  *      and others.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/nubus.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/init.h>
1799ffab81SAdrian Bunk #include <linux/module.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <asm/setup.h>
201da177e4SLinus Torvalds #include <asm/page.h>
211da177e4SLinus Torvalds #include <asm/hwtest.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /* Constants */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* This is, of course, the size in bytelanes, rather than the size in
261da177e4SLinus Torvalds    actual bytes */
271da177e4SLinus Torvalds #define FORMAT_BLOCK_SIZE 20
281da177e4SLinus Torvalds #define ROM_DIR_OFFSET 0x24
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define NUBUS_TEST_PATTERN 0x5A932BC7
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /* Globals */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds struct nubus_dev *nubus_devices;
351da177e4SLinus Torvalds struct nubus_board *nubus_boards;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* Meaning of "bytelanes":
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds    The card ROM may appear on any or all bytes of each long word in
401da177e4SLinus Torvalds    NuBus memory.  The low 4 bits of the "map" value found in the
411da177e4SLinus Torvalds    format block (at the top of the slot address space, as well as at
421da177e4SLinus Torvalds    the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
431da177e4SLinus Torvalds    offsets within each longword, are valid.  Thus:
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds    A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
461da177e4SLinus Torvalds    are valid.
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds    A map of 0xf0 means that no bytelanes are valid (We pray that we
491da177e4SLinus Torvalds    will never encounter this, but stranger things have happened)
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds    A map of 0xe1 means that only the MSB of each long word is actually
521da177e4SLinus Torvalds    part of the card ROM.  (We hope to never encounter NuBus on a
531da177e4SLinus Torvalds    little-endian machine.  Again, stranger things have happened)
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds    A map of 0x78 means that only the LSB of each long word is valid.
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds    Etcetera, etcetera.  Hopefully this clears up some confusion over
581da177e4SLinus Torvalds    what the following code actually does.  */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static inline int not_useful(void *p, int map)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	unsigned long pv = (unsigned long)p;
63f42e5550SFinn Thain 
641da177e4SLinus Torvalds 	pv &= 3;
651da177e4SLinus Torvalds 	if (map & (1 << pv))
661da177e4SLinus Torvalds 		return 0;
671da177e4SLinus Torvalds 	return 1;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	/* This will hold the result */
731da177e4SLinus Torvalds 	unsigned long v = 0;
741da177e4SLinus Torvalds 	unsigned char *p = *ptr;
751da177e4SLinus Torvalds 
76f42e5550SFinn Thain 	while (len) {
771da177e4SLinus Torvalds 		v <<= 8;
781da177e4SLinus Torvalds 		while (not_useful(p, map))
791da177e4SLinus Torvalds 			p++;
801da177e4SLinus Torvalds 		v |= *p++;
811da177e4SLinus Torvalds 		len--;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	*ptr = p;
841da177e4SLinus Torvalds 	return v;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static void nubus_rewind(unsigned char **ptr, int len, int map)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	unsigned char *p = *ptr;
901da177e4SLinus Torvalds 
91f42e5550SFinn Thain 	while (len) {
92f42e5550SFinn Thain 		do {
931da177e4SLinus Torvalds 			p--;
94f42e5550SFinn Thain 		} while (not_useful(p, map));
951da177e4SLinus Torvalds 		len--;
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 	*ptr = p;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void nubus_advance(unsigned char **ptr, int len, int map)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	unsigned char *p = *ptr;
103f42e5550SFinn Thain 
104f42e5550SFinn Thain 	while (len) {
1051da177e4SLinus Torvalds 		while (not_useful(p, map))
1061da177e4SLinus Torvalds 			p++;
1071da177e4SLinus Torvalds 		p++;
1081da177e4SLinus Torvalds 		len--;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 	*ptr = p;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static void nubus_move(unsigned char **ptr, int len, int map)
1141da177e4SLinus Torvalds {
11585cc313aSFinn Thain 	unsigned long slot_space = (unsigned long)*ptr & 0xFF000000;
11685cc313aSFinn Thain 
1171da177e4SLinus Torvalds 	if (len > 0)
1181da177e4SLinus Torvalds 		nubus_advance(ptr, len, map);
1191da177e4SLinus Torvalds 	else if (len < 0)
1201da177e4SLinus Torvalds 		nubus_rewind(ptr, -len, map);
12185cc313aSFinn Thain 
12285cc313aSFinn Thain 	if (((unsigned long)*ptr & 0xFF000000) != slot_space)
12385cc313aSFinn Thain 		pr_err("%s: moved out of slot address space!\n", __func__);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* Now, functions to read the sResource tree */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* Each sResource entry consists of a 1-byte ID and a 3-byte data
1291da177e4SLinus Torvalds    field.  If that data field contains an offset, then obviously we
1301da177e4SLinus Torvalds    have to expand it from a 24-bit signed number to a 32-bit signed
1311da177e4SLinus Torvalds    number. */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static inline long nubus_expand32(long foo)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	if (foo & 0x00800000)	/* 24bit negative */
1361da177e4SLinus Torvalds 		foo |= 0xFF000000;
1371da177e4SLinus Torvalds 	return foo;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static inline void *nubus_rom_addr(int slot)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	/*
1431da177e4SLinus Torvalds 	 *	Returns the first byte after the card. We then walk
1441da177e4SLinus Torvalds 	 *	backwards to get the lane register and the config
1451da177e4SLinus Torvalds 	 */
1461da177e4SLinus Torvalds 	return (void *)(0xF1000000 + (slot << 24));
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	unsigned char *p = nd->base;
152f42e5550SFinn Thain 
1531da177e4SLinus Torvalds 	/* Essentially, just step over the bytelanes using whatever
1541da177e4SLinus Torvalds 	   offset we might have found */
1551da177e4SLinus Torvalds 	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
1561da177e4SLinus Torvalds 	/* And return the value */
1571da177e4SLinus Torvalds 	return p;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* These two are for pulling resource data blocks (i.e. stuff that's
1611da177e4SLinus Torvalds    pointed to with offsets) out of the card ROM. */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
1642f828fb2SFinn Thain 			unsigned int len)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	unsigned char *t = (unsigned char *)dest;
1671da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
168f42e5550SFinn Thain 
169f42e5550SFinn Thain 	while (len) {
1701da177e4SLinus Torvalds 		*t++ = nubus_get_rom(&p, 1, dirent->mask);
1711da177e4SLinus Torvalds 		len--;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
17499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_mem);
1751da177e4SLinus Torvalds 
1762f828fb2SFinn Thain void nubus_get_rsrc_str(char *dest, const struct nubus_dirent *dirent,
1772f828fb2SFinn Thain 			unsigned int len)
1781da177e4SLinus Torvalds {
1792f828fb2SFinn Thain 	char *t = dest;
1801da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
181f42e5550SFinn Thain 
1822f828fb2SFinn Thain 	while (len > 1) {
1832f828fb2SFinn Thain 		unsigned char c = nubus_get_rom(&p, 1, dirent->mask);
1842f828fb2SFinn Thain 
1852f828fb2SFinn Thain 		if (!c)
1861da177e4SLinus Torvalds 			break;
1872f828fb2SFinn Thain 		*t++ = c;
1881da177e4SLinus Torvalds 		len--;
1891da177e4SLinus Torvalds 	}
1902f828fb2SFinn Thain 	if (len > 0)
1912f828fb2SFinn Thain 		*t = '\0';
1921da177e4SLinus Torvalds }
19399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_str);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds int nubus_get_root_dir(const struct nubus_board *board,
1961da177e4SLinus Torvalds 		       struct nubus_dir *dir)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
1991da177e4SLinus Torvalds 	dir->done = 0;
2001da177e4SLinus Torvalds 	dir->mask = board->lanes;
2011da177e4SLinus Torvalds 	return 0;
2021da177e4SLinus Torvalds }
20399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_root_dir);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* This is a slyly renamed version of the above */
2061da177e4SLinus Torvalds int nubus_get_func_dir(const struct nubus_dev *dev,
2071da177e4SLinus Torvalds 		       struct nubus_dir *dir)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	dir->ptr = dir->base = dev->directory;
2101da177e4SLinus Torvalds 	dir->done = 0;
2111da177e4SLinus Torvalds 	dir->mask = dev->board->lanes;
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds }
21499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_func_dir);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds int nubus_get_board_dir(const struct nubus_board *board,
2171da177e4SLinus Torvalds 			struct nubus_dir *dir)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct nubus_dirent ent;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2221da177e4SLinus Torvalds 	dir->done = 0;
2231da177e4SLinus Torvalds 	dir->mask = board->lanes;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* Now dereference it (the first directory is always the board
2261da177e4SLinus Torvalds 	   directory) */
2271da177e4SLinus Torvalds 	if (nubus_readdir(dir, &ent) == -1)
2281da177e4SLinus Torvalds 		return -1;
2291da177e4SLinus Torvalds 	if (nubus_get_subdir(&ent, dir) == -1)
2301da177e4SLinus Torvalds 		return -1;
2311da177e4SLinus Torvalds 	return 0;
2321da177e4SLinus Torvalds }
23399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_board_dir);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds int nubus_get_subdir(const struct nubus_dirent *ent,
2361da177e4SLinus Torvalds 		     struct nubus_dir *dir)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	dir->ptr = dir->base = nubus_dirptr(ent);
2391da177e4SLinus Torvalds 	dir->done = 0;
2401da177e4SLinus Torvalds 	dir->mask = ent->mask;
2411da177e4SLinus Torvalds 	return 0;
2421da177e4SLinus Torvalds }
24399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_subdir);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	u32 resid;
248f42e5550SFinn Thain 
2491da177e4SLinus Torvalds 	if (nd->done)
2501da177e4SLinus Torvalds 		return -1;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* Do this first, otherwise nubus_rewind & co are off by 4 */
2531da177e4SLinus Torvalds 	ent->base = nd->ptr;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* This moves nd->ptr forward */
2561da177e4SLinus Torvalds 	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* EOL marker, as per the Apple docs */
259f42e5550SFinn Thain 	if ((resid & 0xff000000) == 0xff000000) {
2601da177e4SLinus Torvalds 		/* Mark it as done */
2611da177e4SLinus Torvalds 		nd->done = 1;
2621da177e4SLinus Torvalds 		return -1;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* First byte is the resource ID */
2661da177e4SLinus Torvalds 	ent->type = resid >> 24;
2671da177e4SLinus Torvalds 	/* Low 3 bytes might contain data (or might not) */
2681da177e4SLinus Torvalds 	ent->data = resid & 0xffffff;
2691da177e4SLinus Torvalds 	ent->mask = nd->mask;
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds }
27299ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_readdir);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds int nubus_rewinddir(struct nubus_dir *dir)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	dir->ptr = dir->base;
277e36b9913SDavid Huggins-Daines 	dir->done = 0;
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds }
28099ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_rewinddir);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /* Driver interface functions, more or less like in pci.c */
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds struct nubus_dev*
285f42e5550SFinn Thain nubus_find_type(unsigned short category, unsigned short type,
2861da177e4SLinus Torvalds 		const struct nubus_dev *from)
2871da177e4SLinus Torvalds {
288f42e5550SFinn Thain 	struct nubus_dev *itor = from ? from->next : nubus_devices;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	while (itor) {
291f42e5550SFinn Thain 		if (itor->category == category && itor->type == type)
2921da177e4SLinus Torvalds 			return itor;
2931da177e4SLinus Torvalds 		itor = itor->next;
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 	return NULL;
2961da177e4SLinus Torvalds }
29799ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_type);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds struct nubus_dev*
300f42e5550SFinn Thain nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
3011da177e4SLinus Torvalds {
302f42e5550SFinn Thain 	struct nubus_dev *itor = from ? from->next : nubus_devices;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	while (itor) {
3051da177e4SLinus Torvalds 		if (itor->board->slot == slot)
3061da177e4SLinus Torvalds 			return itor;
3071da177e4SLinus Torvalds 		itor = itor->next;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 	return NULL;
3101da177e4SLinus Torvalds }
31199ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_slot);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds int
3141da177e4SLinus Torvalds nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
3151da177e4SLinus Torvalds 		struct nubus_dirent *ent)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	while (nubus_readdir(dir, ent) != -1) {
3181da177e4SLinus Torvalds 		if (ent->type == rsrc_type)
3191da177e4SLinus Torvalds 			return 0;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	return -1;
3221da177e4SLinus Torvalds }
32399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_rsrc);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /* Initialization functions - decide which slots contain stuff worth
3261da177e4SLinus Torvalds    looking at, and print out lots and lots of information from the
3271da177e4SLinus Torvalds    resource blocks. */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /* FIXME: A lot of this stuff will eventually be useful after
330081985acSJoe Perches    initialization, for intelligently probing Ethernet and video chips,
3311da177e4SLinus Torvalds    among other things.  The rest of it should go in the /proc code.
3321da177e4SLinus Torvalds    For now, we just use it to give verbose boot logs. */
3331da177e4SLinus Torvalds 
334*883b8cb3SFinn Thain static int __init nubus_get_block_rsrc_dir(struct nubus_board *board,
335*883b8cb3SFinn Thain 					   const struct nubus_dirent *parent)
336*883b8cb3SFinn Thain {
337*883b8cb3SFinn Thain 	struct nubus_dir dir;
338*883b8cb3SFinn Thain 	struct nubus_dirent ent;
339*883b8cb3SFinn Thain 
340*883b8cb3SFinn Thain 	nubus_get_subdir(parent, &dir);
341*883b8cb3SFinn Thain 
342*883b8cb3SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
343*883b8cb3SFinn Thain 		u32 size;
344*883b8cb3SFinn Thain 
345*883b8cb3SFinn Thain 		nubus_get_rsrc_mem(&size, &ent, 4);
346*883b8cb3SFinn Thain 		pr_debug("        block (0x%x), size %d\n", ent.type, size);
347*883b8cb3SFinn Thain 	}
348*883b8cb3SFinn Thain 	return 0;
349*883b8cb3SFinn Thain }
350*883b8cb3SFinn Thain 
351*883b8cb3SFinn Thain static int __init nubus_get_display_vidmode(struct nubus_board *board,
352*883b8cb3SFinn Thain 					    const struct nubus_dirent *parent)
353*883b8cb3SFinn Thain {
354*883b8cb3SFinn Thain 	struct nubus_dir dir;
355*883b8cb3SFinn Thain 	struct nubus_dirent ent;
356*883b8cb3SFinn Thain 
357*883b8cb3SFinn Thain 	nubus_get_subdir(parent, &dir);
358*883b8cb3SFinn Thain 
359*883b8cb3SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
360*883b8cb3SFinn Thain 		switch (ent.type) {
361*883b8cb3SFinn Thain 		case 1: /* mVidParams */
362*883b8cb3SFinn Thain 		case 2: /* mTable */
363*883b8cb3SFinn Thain 		{
364*883b8cb3SFinn Thain 			u32 size;
365*883b8cb3SFinn Thain 
366*883b8cb3SFinn Thain 			nubus_get_rsrc_mem(&size, &ent, 4);
367*883b8cb3SFinn Thain 			pr_debug("        block (0x%x), size %d\n", ent.type,
368*883b8cb3SFinn Thain 				size);
369*883b8cb3SFinn Thain 			break;
370*883b8cb3SFinn Thain 		}
371*883b8cb3SFinn Thain 		default:
372*883b8cb3SFinn Thain 			pr_debug("        unknown resource 0x%02x, data 0x%06x\n",
373*883b8cb3SFinn Thain 				ent.type, ent.data);
374*883b8cb3SFinn Thain 		}
375*883b8cb3SFinn Thain 	}
376*883b8cb3SFinn Thain 	return 0;
377*883b8cb3SFinn Thain }
378*883b8cb3SFinn Thain 
379*883b8cb3SFinn Thain static int __init nubus_get_display_resource(struct nubus_dev *dev,
3801da177e4SLinus Torvalds 					     const struct nubus_dirent *ent)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds 	switch (ent->type) {
3831da177e4SLinus Torvalds 	case NUBUS_RESID_GAMMADIR:
384f53bad08SFinn Thain 		pr_debug("    gamma directory offset: 0x%06x\n", ent->data);
385*883b8cb3SFinn Thain 		nubus_get_block_rsrc_dir(dev->board, ent);
3861da177e4SLinus Torvalds 		break;
3871da177e4SLinus Torvalds 	case 0x0080 ... 0x0085:
388f53bad08SFinn Thain 		pr_debug("    mode 0x%02x info offset: 0x%06x\n",
3891da177e4SLinus Torvalds 			ent->type, ent->data);
390*883b8cb3SFinn Thain 		nubus_get_display_vidmode(dev->board, ent);
3911da177e4SLinus Torvalds 		break;
3921da177e4SLinus Torvalds 	default:
393f53bad08SFinn Thain 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
3941da177e4SLinus Torvalds 			ent->type, ent->data);
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399*883b8cb3SFinn Thain static int __init nubus_get_network_resource(struct nubus_dev *dev,
4001da177e4SLinus Torvalds 					     const struct nubus_dirent *ent)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	switch (ent->type) {
4031da177e4SLinus Torvalds 	case NUBUS_RESID_MAC_ADDRESS:
4041da177e4SLinus Torvalds 	{
4051da177e4SLinus Torvalds 		char addr[6];
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 		nubus_get_rsrc_mem(addr, ent, 6);
408f53bad08SFinn Thain 		pr_debug("    MAC address: %pM\n", addr);
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 	default:
412f53bad08SFinn Thain 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
4131da177e4SLinus Torvalds 			ent->type, ent->data);
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 	return 0;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418*883b8cb3SFinn Thain static int __init nubus_get_cpu_resource(struct nubus_dev *dev,
4191da177e4SLinus Torvalds 					 const struct nubus_dirent *ent)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	switch (ent->type) {
4221da177e4SLinus Torvalds 	case NUBUS_RESID_MEMINFO:
4231da177e4SLinus Torvalds 	{
4241da177e4SLinus Torvalds 		unsigned long meminfo[2];
425f42e5550SFinn Thain 
4261da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&meminfo, ent, 8);
427f53bad08SFinn Thain 		pr_debug("    memory: [ 0x%08lx 0x%08lx ]\n",
4281da177e4SLinus Torvalds 			meminfo[0], meminfo[1]);
4291da177e4SLinus Torvalds 		break;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	case NUBUS_RESID_ROMINFO:
4321da177e4SLinus Torvalds 	{
4331da177e4SLinus Torvalds 		unsigned long rominfo[2];
434f42e5550SFinn Thain 
4351da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&rominfo, ent, 8);
436f53bad08SFinn Thain 		pr_debug("    ROM:    [ 0x%08lx 0x%08lx ]\n",
4371da177e4SLinus Torvalds 			rominfo[0], rominfo[1]);
4381da177e4SLinus Torvalds 		break;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 	default:
441f53bad08SFinn Thain 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
4421da177e4SLinus Torvalds 			ent->type, ent->data);
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 	return 0;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
447*883b8cb3SFinn Thain static int __init nubus_get_private_resource(struct nubus_dev *dev,
4481da177e4SLinus Torvalds 					     const struct nubus_dirent *ent)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	switch (dev->category) {
4511da177e4SLinus Torvalds 	case NUBUS_CAT_DISPLAY:
452*883b8cb3SFinn Thain 		nubus_get_display_resource(dev, ent);
4531da177e4SLinus Torvalds 		break;
4541da177e4SLinus Torvalds 	case NUBUS_CAT_NETWORK:
455*883b8cb3SFinn Thain 		nubus_get_network_resource(dev, ent);
4561da177e4SLinus Torvalds 		break;
4571da177e4SLinus Torvalds 	case NUBUS_CAT_CPU:
458*883b8cb3SFinn Thain 		nubus_get_cpu_resource(dev, ent);
4591da177e4SLinus Torvalds 		break;
4601da177e4SLinus Torvalds 	default:
461f53bad08SFinn Thain 		pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
4621da177e4SLinus Torvalds 			ent->type, ent->data);
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 	return 0;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static struct nubus_dev * __init
468f42e5550SFinn Thain nubus_get_functional_resource(struct nubus_board *board, int slot,
4691da177e4SLinus Torvalds 			      const struct nubus_dirent *parent)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	struct nubus_dir dir;
4721da177e4SLinus Torvalds 	struct nubus_dirent ent;
4731da177e4SLinus Torvalds 	struct nubus_dev *dev;
4741da177e4SLinus Torvalds 
475f53bad08SFinn Thain 	pr_debug("  Functional resource 0x%02x:\n", parent->type);
4761da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
479dd00cc48SYoann Padioleau 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
4801da177e4SLinus Torvalds 		return NULL;
4811da177e4SLinus Torvalds 	dev->resid = parent->type;
4821da177e4SLinus Torvalds 	dev->directory = dir.base;
4831da177e4SLinus Torvalds 	dev->board = board;
4841da177e4SLinus Torvalds 
485f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
486f42e5550SFinn Thain 		switch (ent.type) {
4871da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
4881da177e4SLinus Torvalds 		{
4891da177e4SLinus Torvalds 			unsigned short nbtdata[4];
490f42e5550SFinn Thain 
4911da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
4921da177e4SLinus Torvalds 			dev->category = nbtdata[0];
4931da177e4SLinus Torvalds 			dev->type     = nbtdata[1];
4941da177e4SLinus Torvalds 			dev->dr_sw    = nbtdata[2];
4951da177e4SLinus Torvalds 			dev->dr_hw    = nbtdata[3];
496f53bad08SFinn Thain 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
4971da177e4SLinus Torvalds 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
4981da177e4SLinus Torvalds 			break;
4991da177e4SLinus Torvalds 		}
5001da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
5011da177e4SLinus Torvalds 		{
5029f97977dSFinn Thain 			char name[64];
5039f97977dSFinn Thain 
5049f97977dSFinn Thain 			nubus_get_rsrc_str(name, &ent, sizeof(name));
5059f97977dSFinn Thain 			pr_debug("    name: %s\n", name);
5061da177e4SLinus Torvalds 			break;
5071da177e4SLinus Torvalds 		}
5081da177e4SLinus Torvalds 		case NUBUS_RESID_DRVRDIR:
5091da177e4SLinus Torvalds 		{
5101da177e4SLinus Torvalds 			/* MacOS driver.  If we were NetBSD we might
5111da177e4SLinus Torvalds 			   use this :-) */
512*883b8cb3SFinn Thain 			pr_debug("    driver directory offset: 0x%06x\n",
513*883b8cb3SFinn Thain 				ent.data);
514*883b8cb3SFinn Thain 			nubus_get_block_rsrc_dir(board, &ent);
5151da177e4SLinus Torvalds 			break;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_BASEOS:
5189f97977dSFinn Thain 		{
5191da177e4SLinus Torvalds 			/* We will need this in order to support
5201da177e4SLinus Torvalds 			   multiple framebuffers.  It might be handy
5211da177e4SLinus Torvalds 			   for Ethernet as well */
5229f97977dSFinn Thain 			u32 base_offset;
5239f97977dSFinn Thain 
5249f97977dSFinn Thain 			nubus_get_rsrc_mem(&base_offset, &ent, 4);
5259f97977dSFinn Thain 			pr_debug("    memory offset: 0x%08x\n", base_offset);
5261da177e4SLinus Torvalds 			break;
5279f97977dSFinn Thain 		}
5281da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_LENGTH:
5299f97977dSFinn Thain 		{
5301da177e4SLinus Torvalds 			/* Ditto */
5319f97977dSFinn Thain 			u32 length;
5329f97977dSFinn Thain 
5339f97977dSFinn Thain 			nubus_get_rsrc_mem(&length, &ent, 4);
5349f97977dSFinn Thain 			pr_debug("    memory length: 0x%08x\n", length);
5351da177e4SLinus Torvalds 			break;
5369f97977dSFinn Thain 		}
5371da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
5389f97977dSFinn Thain 			pr_debug("    flags: 0x%06x\n", ent.data);
5391da177e4SLinus Torvalds 			break;
5401da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
5419f97977dSFinn Thain 			pr_debug("    hwdevid: 0x%06x\n", ent.data);
5421da177e4SLinus Torvalds 			break;
5431da177e4SLinus Torvalds 		default:
5441da177e4SLinus Torvalds 			/* Local/Private resources have their own
5451da177e4SLinus Torvalds 			   function */
546*883b8cb3SFinn Thain 			nubus_get_private_resource(dev, &ent);
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	return dev;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /* This is *really* cool. */
5541da177e4SLinus Torvalds static int __init nubus_get_icon(struct nubus_board *board,
5551da177e4SLinus Torvalds 				 const struct nubus_dirent *ent)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	/* Should be 32x32 if my memory serves me correctly */
558f53bad08SFinn Thain 	u32 icon[32];
559f53bad08SFinn Thain 	int i;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	nubus_get_rsrc_mem(&icon, ent, 128);
562f53bad08SFinn Thain 	pr_debug("    icon:\n");
563f53bad08SFinn Thain 	for (i = 0; i < 8; i++)
564f53bad08SFinn Thain 		pr_debug("        %08x %08x %08x %08x\n",
565f53bad08SFinn Thain 			icon[i * 4 + 0], icon[i * 4 + 1],
566f53bad08SFinn Thain 			icon[i * 4 + 2], icon[i * 4 + 3]);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	return 0;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds static int __init nubus_get_vendorinfo(struct nubus_board *board,
5721da177e4SLinus Torvalds 				       const struct nubus_dirent *parent)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct nubus_dir dir;
5751da177e4SLinus Torvalds 	struct nubus_dirent ent;
5761da177e4SLinus Torvalds 	static char *vendor_fields[6] = { "ID", "serial", "revision",
5771da177e4SLinus Torvalds 	                                  "part", "date", "unknown field" };
5781da177e4SLinus Torvalds 
579f53bad08SFinn Thain 	pr_debug("    vendor info:\n");
5801da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
5811da177e4SLinus Torvalds 
582f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
5831da177e4SLinus Torvalds 		char name[64];
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		/* These are all strings, we think */
5862f828fb2SFinn Thain 		nubus_get_rsrc_str(name, &ent, sizeof(name));
5872f828fb2SFinn Thain 		if (ent.type < 1 || ent.type > 5)
5881da177e4SLinus Torvalds 			ent.type = 5;
589f53bad08SFinn Thain 		pr_debug("    %s: %s\n", vendor_fields[ent.type - 1], name);
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 	return 0;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
5951da177e4SLinus Torvalds 					   const struct nubus_dirent *parent)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	struct nubus_dir dir;
5981da177e4SLinus Torvalds 	struct nubus_dirent ent;
5991da177e4SLinus Torvalds 
600f53bad08SFinn Thain 	pr_debug("  Board resource 0x%02x:\n", parent->type);
6011da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
6021da177e4SLinus Torvalds 
603f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
6041da177e4SLinus Torvalds 		switch (ent.type) {
6051da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
6061da177e4SLinus Torvalds 		{
6071da177e4SLinus Torvalds 			unsigned short nbtdata[4];
6081da177e4SLinus Torvalds 			/* This type is always the same, and is not
6091da177e4SLinus Torvalds 			   useful except insofar as it tells us that
6101da177e4SLinus Torvalds 			   we really are looking at a board resource. */
6111da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
612f53bad08SFinn Thain 			pr_debug("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
61371ae40e4SDavid Huggins-Daines 				nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
6141da177e4SLinus Torvalds 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
6151da177e4SLinus Torvalds 			    nbtdata[2] != 0 || nbtdata[3] != 0)
616d7811a36SFinn Thain 				pr_err("Slot %X: sResource is not a board resource!\n",
617d7811a36SFinn Thain 				       slot);
6181da177e4SLinus Torvalds 			break;
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
6212f828fb2SFinn Thain 			nubus_get_rsrc_str(board->name, &ent,
6222f828fb2SFinn Thain 					   sizeof(board->name));
623f53bad08SFinn Thain 			pr_debug("    name: %s\n", board->name);
6241da177e4SLinus Torvalds 			break;
6251da177e4SLinus Torvalds 		case NUBUS_RESID_ICON:
6261da177e4SLinus Torvalds 			nubus_get_icon(board, &ent);
6271da177e4SLinus Torvalds 			break;
6281da177e4SLinus Torvalds 		case NUBUS_RESID_BOARDID:
629f53bad08SFinn Thain 			pr_debug("    board id: 0x%x\n", ent.data);
6301da177e4SLinus Torvalds 			break;
6311da177e4SLinus Torvalds 		case NUBUS_RESID_PRIMARYINIT:
632f53bad08SFinn Thain 			pr_debug("    primary init offset: 0x%06x\n", ent.data);
6331da177e4SLinus Torvalds 			break;
6341da177e4SLinus Torvalds 		case NUBUS_RESID_VENDORINFO:
6351da177e4SLinus Torvalds 			nubus_get_vendorinfo(board, &ent);
6361da177e4SLinus Torvalds 			break;
6371da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
638f53bad08SFinn Thain 			pr_debug("    flags: 0x%06x\n", ent.data);
6391da177e4SLinus Torvalds 			break;
6401da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
641f53bad08SFinn Thain 			pr_debug("    hwdevid: 0x%06x\n", ent.data);
6421da177e4SLinus Torvalds 			break;
6431da177e4SLinus Torvalds 		case NUBUS_RESID_SECONDINIT:
644f53bad08SFinn Thain 			pr_debug("    secondary init offset: 0x%06x\n",
645f53bad08SFinn Thain 				 ent.data);
6461da177e4SLinus Torvalds 			break;
6471da177e4SLinus Torvalds 			/* WTF isn't this in the functional resources? */
6481da177e4SLinus Torvalds 		case NUBUS_RESID_VIDNAMES:
649*883b8cb3SFinn Thain 			pr_debug("    vidnames directory offset: 0x%06x\n",
650*883b8cb3SFinn Thain 				ent.data);
651*883b8cb3SFinn Thain 			nubus_get_block_rsrc_dir(board, &ent);
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 			/* Same goes for this */
6541da177e4SLinus Torvalds 		case NUBUS_RESID_VIDMODES:
655f53bad08SFinn Thain 			pr_debug("    video mode parameter directory offset: 0x%06x\n",
6561da177e4SLinus Torvalds 				ent.data);
6571da177e4SLinus Torvalds 			break;
6581da177e4SLinus Torvalds 		default:
659f53bad08SFinn Thain 			pr_debug("    unknown resource 0x%02x, data 0x%06x\n",
6601da177e4SLinus Torvalds 				ent.type, ent.data);
6611da177e4SLinus Torvalds 		}
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds /* Add a board (might be many devices) to the list */
6671da177e4SLinus Torvalds static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	struct nubus_board *board;
6701da177e4SLinus Torvalds 	struct nubus_board **boardp;
6711da177e4SLinus Torvalds 	unsigned char *rp;
6721da177e4SLinus Torvalds 	unsigned long dpat;
6731da177e4SLinus Torvalds 	struct nubus_dir dir;
6741da177e4SLinus Torvalds 	struct nubus_dirent ent;
675d7811a36SFinn Thain 	int prev_resid = -1;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/* Move to the start of the format block */
6781da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
6791da177e4SLinus Torvalds 	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
682dd00cc48SYoann Padioleau 	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
6831da177e4SLinus Torvalds 		return NULL;
6841da177e4SLinus Torvalds 	board->fblock = rp;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	/* Dump the format block for debugging purposes */
68771ae40e4SDavid Huggins-Daines 	pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
688f53bad08SFinn Thain 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
689f53bad08SFinn Thain 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
690f53bad08SFinn Thain 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
69171ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
69271ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
69371ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
69471ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
69571ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
6961da177e4SLinus Torvalds 	rp = board->fblock;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	board->slot = slot;
6991da177e4SLinus Torvalds 	board->slot_addr = (unsigned long)nubus_slot_addr(slot);
7001da177e4SLinus Torvalds 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
7011da177e4SLinus Torvalds 	/* rom_length is *supposed* to be the total length of the
7021da177e4SLinus Torvalds 	 * ROM.  In practice it is the "amount of ROM used to compute
7031da177e4SLinus Torvalds 	 * the CRC."  So some jokers decide to set it to zero and
7041da177e4SLinus Torvalds 	 * set the crc to zero so they don't have to do any math.
7051da177e4SLinus Torvalds 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
7061da177e4SLinus Torvalds 	 */
7071da177e4SLinus Torvalds 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
7081da177e4SLinus Torvalds 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
7091da177e4SLinus Torvalds 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
7101da177e4SLinus Torvalds 	board->format = nubus_get_rom(&rp, 1, bytelanes);
7111da177e4SLinus Torvalds 	board->lanes = bytelanes;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/* Directory offset should be small and negative... */
7141da177e4SLinus Torvalds 	if (!(board->doffset & 0x00FF0000))
715d7811a36SFinn Thain 		pr_warn("Slot %X: Dodgy doffset!\n", slot);
7161da177e4SLinus Torvalds 	dpat = nubus_get_rom(&rp, 4, bytelanes);
7171da177e4SLinus Torvalds 	if (dpat != NUBUS_TEST_PATTERN)
718d7811a36SFinn Thain 		pr_warn("Slot %X: Wrong test pattern %08lx!\n", slot, dpat);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/*
7211da177e4SLinus Torvalds 	 *	I wonder how the CRC is meant to work -
7221da177e4SLinus Torvalds 	 *		any takers ?
7231da177e4SLinus Torvalds 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
7241da177e4SLinus Torvalds 	 * since the initial Macintosh ROM releases skipped the check.
7251da177e4SLinus Torvalds 	 */
7261da177e4SLinus Torvalds 
727475e6e15SDavid Huggins-Daines 	/* Set up the directory pointer */
728475e6e15SDavid Huggins-Daines 	board->directory = board->fblock;
729475e6e15SDavid Huggins-Daines 	nubus_move(&board->directory, nubus_expand32(board->doffset),
730475e6e15SDavid Huggins-Daines 	           board->lanes);
731475e6e15SDavid Huggins-Daines 
7321da177e4SLinus Torvalds 	nubus_get_root_dir(board, &dir);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	/* We're ready to rock */
735f53bad08SFinn Thain 	pr_debug("Slot %X resources:\n", slot);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	/* Each slot should have one board resource and any number of
7381da177e4SLinus Torvalds 	   functional resources.  So we'll fill in some fields in the
7391da177e4SLinus Torvalds 	   struct nubus_board from the board resource, then walk down
7401da177e4SLinus Torvalds 	   the list of functional resources, spinning out a nubus_dev
7411da177e4SLinus Torvalds 	   for each of them. */
7421da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1) {
7431da177e4SLinus Torvalds 		/* We can't have this! */
744d7811a36SFinn Thain 		pr_err("Slot %X: Board resource not found!\n", slot);
7451da177e4SLinus Torvalds 		return NULL;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
748d7811a36SFinn Thain 	if (ent.type < 1 || ent.type > 127)
749d7811a36SFinn Thain 		pr_warn("Slot %X: Board resource ID is invalid!\n", slot);
750d7811a36SFinn Thain 
751d7811a36SFinn Thain 	nubus_get_board_resource(board, slot, &ent);
752d7811a36SFinn Thain 
7531da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1) {
7541da177e4SLinus Torvalds 		struct nubus_dev *dev;
7551da177e4SLinus Torvalds 		struct nubus_dev **devp;
756f42e5550SFinn Thain 
7571da177e4SLinus Torvalds 		dev = nubus_get_functional_resource(board, slot, &ent);
7581da177e4SLinus Torvalds 		if (dev == NULL)
7591da177e4SLinus Torvalds 			continue;
7601da177e4SLinus Torvalds 
761d7811a36SFinn Thain 		/* Resources should appear in ascending ID order. This sanity
762d7811a36SFinn Thain 		 * check prevents duplicate resource IDs.
763d7811a36SFinn Thain 		 */
764d7811a36SFinn Thain 		if (dev->resid <= prev_resid) {
765d7811a36SFinn Thain 			kfree(dev);
766d7811a36SFinn Thain 			continue;
767d7811a36SFinn Thain 		}
768d7811a36SFinn Thain 		prev_resid = dev->resid;
769d7811a36SFinn Thain 
7701da177e4SLinus Torvalds 		/* We zeroed this out above */
7711da177e4SLinus Torvalds 		if (board->first_dev == NULL)
7721da177e4SLinus Torvalds 			board->first_dev = dev;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		/* Put it on the global NuBus device chain. Keep entries in order. */
775f42e5550SFinn Thain 		for (devp = &nubus_devices; *devp != NULL;
776f42e5550SFinn Thain 		     devp = &((*devp)->next))
7771da177e4SLinus Torvalds 			/* spin */;
7781da177e4SLinus Torvalds 		*devp = dev;
7791da177e4SLinus Torvalds 		dev->next = NULL;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	/* Put it on the global NuBus board chain. Keep entries in order. */
783f42e5550SFinn Thain 	for (boardp = &nubus_boards; *boardp != NULL;
784f42e5550SFinn Thain 	     boardp = &((*boardp)->next))
7851da177e4SLinus Torvalds 		/* spin */;
7861da177e4SLinus Torvalds 	*boardp = board;
7871da177e4SLinus Torvalds 	board->next = NULL;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	return board;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
792460cf95eSFinn Thain static void __init nubus_probe_slot(int slot)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	unsigned char dp;
7951da177e4SLinus Torvalds 	unsigned char *rp;
7961da177e4SLinus Torvalds 	int i;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
799f42e5550SFinn Thain 	for (i = 4; i; i--) {
8001da177e4SLinus Torvalds 		rp--;
8019f97977dSFinn Thain 		if (!hwreg_present(rp))
8021da177e4SLinus Torvalds 			continue;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		dp = *rp;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		/* The last byte of the format block consists of two
8071da177e4SLinus Torvalds 		   nybbles which are "mirror images" of each other.
8081da177e4SLinus Torvalds 		   These show us the valid bytelanes */
8091da177e4SLinus Torvalds 		if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
8101da177e4SLinus Torvalds 			continue;
8111da177e4SLinus Torvalds 		/* Check that this value is actually *on* one of the
8121da177e4SLinus Torvalds 		   bytelanes it claims are valid! */
81385cc313aSFinn Thain 		if (not_useful(rp, dp))
8141da177e4SLinus Torvalds 			continue;
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		/* Looks promising.  Let's put it on the list. */
8171da177e4SLinus Torvalds 		nubus_add_board(slot, dp);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		return;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
823460cf95eSFinn Thain static void __init nubus_scan_bus(void)
8241da177e4SLinus Torvalds {
8251da177e4SLinus Torvalds 	int slot;
826f42e5550SFinn Thain 
827f53bad08SFinn Thain 	pr_info("NuBus: Scanning NuBus slots.\n");
828f42e5550SFinn Thain 	for (slot = 9; slot < 15; slot++) {
8291da177e4SLinus Torvalds 		nubus_probe_slot(slot);
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds static int __init nubus_init(void)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	if (!MACH_IS_MAC)
8361da177e4SLinus Torvalds 		return 0;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	nubus_scan_bus();
8391da177e4SLinus Torvalds 	nubus_proc_init();
8401da177e4SLinus Torvalds 	return 0;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds subsys_initcall(nubus_init);
844