xref: /linux/drivers/nubus/nubus.c (revision 71ae40e4cf3323b89ea910b82947d5e5e08c94bf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Macintosh Nubus Interface Code
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *      Originally by Alan Cox
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
71da177e4SLinus Torvalds  *      and others.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds #include <linux/nubus.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/delay.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 #include <asm/mac_via.h>
231da177e4SLinus Torvalds #include <asm/mac_oss.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds extern void via_nubus_init(void);
261da177e4SLinus Torvalds extern void oss_nubus_init(void);
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* Constants */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /* This is, of course, the size in bytelanes, rather than the size in
311da177e4SLinus Torvalds    actual bytes */
321da177e4SLinus Torvalds #define FORMAT_BLOCK_SIZE 20
331da177e4SLinus Torvalds #define ROM_DIR_OFFSET 0x24
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define NUBUS_TEST_PATTERN 0x5A932BC7
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* Define this if you like to live dangerously - it is known not to
381da177e4SLinus Torvalds    work on pretty much every machine except the Quadra 630 and the LC
391da177e4SLinus Torvalds    III. */
401da177e4SLinus Torvalds #undef I_WANT_TO_PROBE_SLOT_ZERO
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* This sometimes helps combat failure to boot */
431da177e4SLinus Torvalds #undef TRY_TO_DODGE_WSOD
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* Globals */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds struct nubus_dev*   nubus_devices;
481da177e4SLinus Torvalds struct nubus_board* nubus_boards;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* Meaning of "bytelanes":
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds    The card ROM may appear on any or all bytes of each long word in
531da177e4SLinus Torvalds    NuBus memory.  The low 4 bits of the "map" value found in the
541da177e4SLinus Torvalds    format block (at the top of the slot address space, as well as at
551da177e4SLinus Torvalds    the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
561da177e4SLinus Torvalds    offsets within each longword, are valid.  Thus:
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds    A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
591da177e4SLinus Torvalds    are valid.
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds    A map of 0xf0 means that no bytelanes are valid (We pray that we
621da177e4SLinus Torvalds    will never encounter this, but stranger things have happened)
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds    A map of 0xe1 means that only the MSB of each long word is actually
651da177e4SLinus Torvalds    part of the card ROM.  (We hope to never encounter NuBus on a
661da177e4SLinus Torvalds    little-endian machine.  Again, stranger things have happened)
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds    A map of 0x78 means that only the LSB of each long word is valid.
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds    Etcetera, etcetera.  Hopefully this clears up some confusion over
711da177e4SLinus Torvalds    what the following code actually does.  */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds static inline int not_useful(void *p, int map)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	unsigned long pv=(unsigned long)p;
761da177e4SLinus Torvalds 	pv &= 3;
771da177e4SLinus Torvalds 	if(map & (1<<pv))
781da177e4SLinus Torvalds 		return 0;
791da177e4SLinus Torvalds 	return 1;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	/* This will hold the result */
851da177e4SLinus Torvalds 	unsigned long v = 0;
861da177e4SLinus Torvalds 	unsigned char *p = *ptr;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	while(len)
891da177e4SLinus Torvalds 	{
901da177e4SLinus Torvalds 		v <<= 8;
911da177e4SLinus Torvalds 		while(not_useful(p,map))
921da177e4SLinus Torvalds 			p++;
931da177e4SLinus Torvalds 		v |= *p++;
941da177e4SLinus Torvalds 		len--;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 	*ptr = p;
971da177e4SLinus Torvalds 	return v;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void nubus_rewind(unsigned char **ptr, int len, int map)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	unsigned char *p=*ptr;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	/* Sanity check */
1051da177e4SLinus Torvalds 	if(len > 65536)
106*71ae40e4SDavid Huggins-Daines 		pr_err("rewind of 0x%08x!\n", len);
1071da177e4SLinus Torvalds 	while(len)
1081da177e4SLinus Torvalds 	{
1091da177e4SLinus Torvalds 		do
1101da177e4SLinus Torvalds 		{
1111da177e4SLinus Torvalds 			p--;
1121da177e4SLinus Torvalds 		}
1131da177e4SLinus Torvalds 		while(not_useful(p, map));
1141da177e4SLinus Torvalds 		len--;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 	*ptr=p;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static void nubus_advance(unsigned char **ptr, int len, int map)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	unsigned char *p = *ptr;
1221da177e4SLinus Torvalds 	if(len>65536)
123*71ae40e4SDavid Huggins-Daines 		pr_err("advance of 0x%08x!\n", len);
1241da177e4SLinus Torvalds 	while(len)
1251da177e4SLinus Torvalds 	{
1261da177e4SLinus Torvalds 		while(not_useful(p,map))
1271da177e4SLinus Torvalds 			p++;
1281da177e4SLinus Torvalds 		p++;
1291da177e4SLinus Torvalds 		len--;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 	*ptr = p;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static void nubus_move(unsigned char **ptr, int len, int map)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	if(len > 0)
1371da177e4SLinus Torvalds 		nubus_advance(ptr, len, map);
1381da177e4SLinus Torvalds 	else if(len < 0)
1391da177e4SLinus Torvalds 		nubus_rewind(ptr, -len, map);
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* Now, functions to read the sResource tree */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds /* Each sResource entry consists of a 1-byte ID and a 3-byte data
1451da177e4SLinus Torvalds    field.  If that data field contains an offset, then obviously we
1461da177e4SLinus Torvalds    have to expand it from a 24-bit signed number to a 32-bit signed
1471da177e4SLinus Torvalds    number. */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static inline long nubus_expand32(long foo)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	if(foo & 0x00800000)	/* 24bit negative */
1521da177e4SLinus Torvalds 		foo |= 0xFF000000;
1531da177e4SLinus Torvalds 	return foo;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static inline void *nubus_rom_addr(int slot)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	/*
1591da177e4SLinus Torvalds 	 *	Returns the first byte after the card. We then walk
1601da177e4SLinus Torvalds 	 *	backwards to get the lane register and the config
1611da177e4SLinus Torvalds 	 */
1621da177e4SLinus Torvalds 	return (void *)(0xF1000000+(slot<<24));
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	unsigned char *p = nd->base;
1681da177e4SLinus Torvalds 	/* Essentially, just step over the bytelanes using whatever
1691da177e4SLinus Torvalds 	   offset we might have found */
1701da177e4SLinus Torvalds 	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
1711da177e4SLinus Torvalds 	/* And return the value */
1721da177e4SLinus Torvalds 	return p;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* These two are for pulling resource data blocks (i.e. stuff that's
1761da177e4SLinus Torvalds    pointed to with offsets) out of the card ROM. */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent,
1791da177e4SLinus Torvalds 			int len)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unsigned char *t = (unsigned char *)dest;
1821da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
1831da177e4SLinus Torvalds 	while(len)
1841da177e4SLinus Torvalds 	{
1851da177e4SLinus Torvalds 		*t++ = nubus_get_rom(&p, 1, dirent->mask);
1861da177e4SLinus Torvalds 		len--;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds }
18999ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_mem);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
1921da177e4SLinus Torvalds 			int len)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	unsigned char *t=(unsigned char *)dest;
1951da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
1961da177e4SLinus Torvalds 	while(len)
1971da177e4SLinus Torvalds 	{
1981da177e4SLinus Torvalds 		*t = nubus_get_rom(&p, 1, dirent->mask);
1991da177e4SLinus Torvalds 		if(!*t++)
2001da177e4SLinus Torvalds 			break;
2011da177e4SLinus Torvalds 		len--;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds }
20499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_str);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds int nubus_get_root_dir(const struct nubus_board* board,
2071da177e4SLinus Torvalds 		       struct nubus_dir* dir)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2101da177e4SLinus Torvalds 	dir->done = 0;
2111da177e4SLinus Torvalds 	dir->mask = board->lanes;
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds }
21499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_root_dir);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /* This is a slyly renamed version of the above */
2171da177e4SLinus Torvalds int nubus_get_func_dir(const struct nubus_dev* dev,
2181da177e4SLinus Torvalds 		       struct nubus_dir* dir)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	dir->ptr = dir->base = dev->directory;
2211da177e4SLinus Torvalds 	dir->done = 0;
2221da177e4SLinus Torvalds 	dir->mask = dev->board->lanes;
2231da177e4SLinus Torvalds 	return 0;
2241da177e4SLinus Torvalds }
22599ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_func_dir);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds int nubus_get_board_dir(const struct nubus_board* board,
2281da177e4SLinus Torvalds 			struct nubus_dir* dir)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	struct nubus_dirent ent;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2331da177e4SLinus Torvalds 	dir->done = 0;
2341da177e4SLinus Torvalds 	dir->mask = board->lanes;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* Now dereference it (the first directory is always the board
2371da177e4SLinus Torvalds 	   directory) */
2381da177e4SLinus Torvalds 	if (nubus_readdir(dir, &ent) == -1)
2391da177e4SLinus Torvalds 		return -1;
2401da177e4SLinus Torvalds 	if (nubus_get_subdir(&ent, dir) == -1)
2411da177e4SLinus Torvalds 		return -1;
2421da177e4SLinus Torvalds 	return 0;
2431da177e4SLinus Torvalds }
24499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_board_dir);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds int nubus_get_subdir(const struct nubus_dirent *ent,
2471da177e4SLinus Torvalds 		     struct nubus_dir *dir)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	dir->ptr = dir->base = nubus_dirptr(ent);
2501da177e4SLinus Torvalds 	dir->done = 0;
2511da177e4SLinus Torvalds 	dir->mask = ent->mask;
2521da177e4SLinus Torvalds 	return 0;
2531da177e4SLinus Torvalds }
25499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_subdir);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	u32 resid;
2591da177e4SLinus Torvalds 	if (nd->done)
2601da177e4SLinus Torvalds 		return -1;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	/* Do this first, otherwise nubus_rewind & co are off by 4 */
2631da177e4SLinus Torvalds 	ent->base = nd->ptr;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* This moves nd->ptr forward */
2661da177e4SLinus Torvalds 	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* EOL marker, as per the Apple docs */
2691da177e4SLinus Torvalds 	if((resid&0xff000000) == 0xff000000)
2701da177e4SLinus Torvalds 	{
2711da177e4SLinus Torvalds 		/* Mark it as done */
2721da177e4SLinus Torvalds 		nd->done = 1;
2731da177e4SLinus Torvalds 		return -1;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* First byte is the resource ID */
2771da177e4SLinus Torvalds 	ent->type  = resid >> 24;
2781da177e4SLinus Torvalds 	/* Low 3 bytes might contain data (or might not) */
2791da177e4SLinus Torvalds 	ent->data = resid & 0xffffff;
2801da177e4SLinus Torvalds 	ent->mask  = nd->mask;
2811da177e4SLinus Torvalds 	return 0;
2821da177e4SLinus Torvalds }
28399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_readdir);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds int nubus_rewinddir(struct nubus_dir* dir)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds 	dir->ptr = dir->base;
288e36b9913SDavid Huggins-Daines 	dir->done = 0;
289e36b9913SDavid Huggins-Daines 
2901da177e4SLinus Torvalds 	return 0;
2911da177e4SLinus Torvalds }
29299ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_rewinddir);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /* Driver interface functions, more or less like in pci.c */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds struct nubus_dev*
2971da177e4SLinus Torvalds nubus_find_device(unsigned short category,
2981da177e4SLinus Torvalds 		  unsigned short type,
2991da177e4SLinus Torvalds 		  unsigned short dr_hw,
3001da177e4SLinus Torvalds 		  unsigned short dr_sw,
3011da177e4SLinus Torvalds 		  const struct nubus_dev* from)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct nubus_dev* itor =
3041da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while (itor) {
3071da177e4SLinus Torvalds 		if (itor->category == category
3081da177e4SLinus Torvalds 		    && itor->type == type
3091da177e4SLinus Torvalds 		    && itor->dr_hw == dr_hw
3101da177e4SLinus Torvalds 		    && itor->dr_sw == dr_sw)
3111da177e4SLinus Torvalds 			return itor;
3121da177e4SLinus Torvalds 		itor = itor->next;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	return NULL;
3151da177e4SLinus Torvalds }
31699ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_device);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds struct nubus_dev*
3191da177e4SLinus Torvalds nubus_find_type(unsigned short category,
3201da177e4SLinus Torvalds 		unsigned short type,
3211da177e4SLinus Torvalds 		const struct nubus_dev* from)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct nubus_dev* itor =
3241da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	while (itor) {
3271da177e4SLinus Torvalds 		if (itor->category == category
3281da177e4SLinus Torvalds 		    && itor->type == type)
3291da177e4SLinus Torvalds 			return itor;
3301da177e4SLinus Torvalds 		itor = itor->next;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 	return NULL;
3331da177e4SLinus Torvalds }
33499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_type);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds struct nubus_dev*
3371da177e4SLinus Torvalds nubus_find_slot(unsigned int slot,
3381da177e4SLinus Torvalds 		const struct nubus_dev* from)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct nubus_dev* itor =
3411da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	while (itor) {
3441da177e4SLinus Torvalds 		if (itor->board->slot == slot)
3451da177e4SLinus Torvalds 			return itor;
3461da177e4SLinus Torvalds 		itor = itor->next;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 	return NULL;
3491da177e4SLinus Torvalds }
35099ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_slot);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds int
3531da177e4SLinus Torvalds nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
3541da177e4SLinus Torvalds 		struct nubus_dirent* ent)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	while (nubus_readdir(dir, ent) != -1) {
3571da177e4SLinus Torvalds 		if (ent->type == rsrc_type)
3581da177e4SLinus Torvalds 			return 0;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	return -1;
3611da177e4SLinus Torvalds }
36299ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_rsrc);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /* Initialization functions - decide which slots contain stuff worth
3651da177e4SLinus Torvalds    looking at, and print out lots and lots of information from the
3661da177e4SLinus Torvalds    resource blocks. */
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /* FIXME: A lot of this stuff will eventually be useful after
369081985acSJoe Perches    initialization, for intelligently probing Ethernet and video chips,
3701da177e4SLinus Torvalds    among other things.  The rest of it should go in the /proc code.
3711da177e4SLinus Torvalds    For now, we just use it to give verbose boot logs. */
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds static int __init nubus_show_display_resource(struct nubus_dev* dev,
3741da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	switch (ent->type) {
3771da177e4SLinus Torvalds 	case NUBUS_RESID_GAMMADIR:
378*71ae40e4SDavid Huggins-Daines 		pr_info("    gamma directory offset: 0x%06x\n", ent->data);
3791da177e4SLinus Torvalds 		break;
3801da177e4SLinus Torvalds 	case 0x0080 ... 0x0085:
381*71ae40e4SDavid Huggins-Daines 		pr_info("    mode %02X info offset: 0x%06x\n",
3821da177e4SLinus Torvalds 		       ent->type, ent->data);
3831da177e4SLinus Torvalds 		break;
3841da177e4SLinus Torvalds 	default:
385*71ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
3861da177e4SLinus Torvalds 		       ent->type, ent->data);
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 	return 0;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds static int __init nubus_show_network_resource(struct nubus_dev* dev,
3921da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	switch (ent->type) {
3951da177e4SLinus Torvalds 	case NUBUS_RESID_MAC_ADDRESS:
3961da177e4SLinus Torvalds 	{
3971da177e4SLinus Torvalds 		char addr[6];
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		nubus_get_rsrc_mem(addr, ent, 6);
400*71ae40e4SDavid Huggins-Daines 		pr_info("    MAC address: %pM\n", addr);
4011da177e4SLinus Torvalds 		break;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	default:
404*71ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
4051da177e4SLinus Torvalds 		       ent->type, ent->data);
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 	return 0;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
4111da177e4SLinus Torvalds 					  const struct nubus_dirent* ent)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	switch (ent->type) {
4141da177e4SLinus Torvalds 	case NUBUS_RESID_MEMINFO:
4151da177e4SLinus Torvalds 	{
4161da177e4SLinus Torvalds 		unsigned long meminfo[2];
4171da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&meminfo, ent, 8);
418*71ae40e4SDavid Huggins-Daines 		pr_info("    memory: [ 0x%08lx 0x%08lx ]\n",
4191da177e4SLinus Torvalds 		       meminfo[0], meminfo[1]);
4201da177e4SLinus Torvalds 		break;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 	case NUBUS_RESID_ROMINFO:
4231da177e4SLinus Torvalds 	{
4241da177e4SLinus Torvalds 		unsigned long rominfo[2];
4251da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&rominfo, ent, 8);
426*71ae40e4SDavid Huggins-Daines 		pr_info("    ROM:    [ 0x%08lx 0x%08lx ]\n",
4271da177e4SLinus Torvalds 		       rominfo[0], rominfo[1]);
4281da177e4SLinus Torvalds 		break;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 	default:
431*71ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
4321da177e4SLinus Torvalds 		       ent->type, ent->data);
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static int __init nubus_show_private_resource(struct nubus_dev* dev,
4381da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	switch (dev->category) {
4411da177e4SLinus Torvalds 	case NUBUS_CAT_DISPLAY:
4421da177e4SLinus Torvalds 		nubus_show_display_resource(dev, ent);
4431da177e4SLinus Torvalds 		break;
4441da177e4SLinus Torvalds 	case NUBUS_CAT_NETWORK:
4451da177e4SLinus Torvalds 		nubus_show_network_resource(dev, ent);
4461da177e4SLinus Torvalds 		break;
4471da177e4SLinus Torvalds 	case NUBUS_CAT_CPU:
4481da177e4SLinus Torvalds 		nubus_show_cpu_resource(dev, ent);
4491da177e4SLinus Torvalds 		break;
4501da177e4SLinus Torvalds 	default:
451*71ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
4521da177e4SLinus Torvalds 		       ent->type, ent->data);
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds 	return 0;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds static struct nubus_dev* __init
4581da177e4SLinus Torvalds 	   nubus_get_functional_resource(struct nubus_board* board,
4591da177e4SLinus Torvalds 					 int slot,
4601da177e4SLinus Torvalds 					 const struct nubus_dirent* parent)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct nubus_dir    dir;
4631da177e4SLinus Torvalds 	struct nubus_dirent ent;
4641da177e4SLinus Torvalds 	struct nubus_dev*   dev;
4651da177e4SLinus Torvalds 
466*71ae40e4SDavid Huggins-Daines 	pr_info("  Function 0x%02x:\n", parent->type);
4671da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* Apple seems to have botched the ROM on the IIx */
4701da177e4SLinus Torvalds 	if (slot == 0 && (unsigned long)dir.base % 2)
4711da177e4SLinus Torvalds 		dir.base += 1;
4721da177e4SLinus Torvalds 
473*71ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
474*71ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
477dd00cc48SYoann Padioleau 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
4781da177e4SLinus Torvalds 		return NULL;
4791da177e4SLinus Torvalds 	dev->resid = parent->type;
4801da177e4SLinus Torvalds 	dev->directory = dir.base;
4811da177e4SLinus Torvalds 	dev->board = board;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1)
4841da177e4SLinus Torvalds 	{
4851da177e4SLinus Torvalds 		switch(ent.type)
4861da177e4SLinus Torvalds 		{
4871da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
4881da177e4SLinus Torvalds 		{
4891da177e4SLinus Torvalds 			unsigned short nbtdata[4];
4901da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
4911da177e4SLinus Torvalds 			dev->category = nbtdata[0];
4921da177e4SLinus Torvalds 			dev->type     = nbtdata[1];
4931da177e4SLinus Torvalds 			dev->dr_sw    = nbtdata[2];
4941da177e4SLinus Torvalds 			dev->dr_hw    = nbtdata[3];
495*71ae40e4SDavid Huggins-Daines 			pr_info("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
4961da177e4SLinus Torvalds 			        nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
4971da177e4SLinus Torvalds 			break;
4981da177e4SLinus Torvalds 		}
4991da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
5001da177e4SLinus Torvalds 		{
5011da177e4SLinus Torvalds 			nubus_get_rsrc_str(dev->name, &ent, 64);
502*71ae40e4SDavid Huggins-Daines 			pr_info("    name: %s\n", dev->name);
5031da177e4SLinus Torvalds 			break;
5041da177e4SLinus Torvalds 		}
5051da177e4SLinus Torvalds 		case NUBUS_RESID_DRVRDIR:
5061da177e4SLinus Torvalds 		{
5071da177e4SLinus Torvalds 			/* MacOS driver.  If we were NetBSD we might
5081da177e4SLinus Torvalds 			   use this :-) */
5091da177e4SLinus Torvalds 			struct nubus_dir drvr_dir;
5101da177e4SLinus Torvalds 			struct nubus_dirent drvr_ent;
5111da177e4SLinus Torvalds 			nubus_get_subdir(&ent, &drvr_dir);
5121da177e4SLinus Torvalds 			nubus_readdir(&drvr_dir, &drvr_ent);
5131da177e4SLinus Torvalds 			dev->driver = nubus_dirptr(&drvr_ent);
514*71ae40e4SDavid Huggins-Daines 			pr_info("    driver at: 0x%p\n", dev->driver);
5151da177e4SLinus Torvalds 			break;
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_BASEOS:
5181da177e4SLinus Torvalds 			/* We will need this in order to support
5191da177e4SLinus Torvalds 			   multiple framebuffers.  It might be handy
5201da177e4SLinus Torvalds 			   for Ethernet as well */
5211da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
522*71ae40e4SDavid Huggins-Daines 			pr_info("    memory offset: 0x%08lx\n", dev->iobase);
5231da177e4SLinus Torvalds 			break;
5241da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_LENGTH:
5251da177e4SLinus Torvalds 			/* Ditto */
5261da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
527*71ae40e4SDavid Huggins-Daines 			pr_info("    memory length: 0x%08lx\n", dev->iosize);
5281da177e4SLinus Torvalds 			break;
5291da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
5301da177e4SLinus Torvalds 			dev->flags = ent.data;
531*71ae40e4SDavid Huggins-Daines 			pr_info("    flags: 0x%06x\n", dev->flags);
5321da177e4SLinus Torvalds 			break;
5331da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
5341da177e4SLinus Torvalds 			dev->hwdevid = ent.data;
535*71ae40e4SDavid Huggins-Daines 			pr_info("    hwdevid: 0x%06x\n", dev->hwdevid);
5361da177e4SLinus Torvalds 			break;
5371da177e4SLinus Torvalds 		default:
5381da177e4SLinus Torvalds 			/* Local/Private resources have their own
5391da177e4SLinus Torvalds 			   function */
5401da177e4SLinus Torvalds 			nubus_show_private_resource(dev, &ent);
5411da177e4SLinus Torvalds 		}
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	return dev;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* This is cool. */
5481da177e4SLinus Torvalds static int __init nubus_get_vidnames(struct nubus_board* board,
5491da177e4SLinus Torvalds 				     const struct nubus_dirent* parent)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	struct nubus_dir    dir;
5521da177e4SLinus Torvalds 	struct nubus_dirent ent;
5531da177e4SLinus Torvalds 	/* FIXME: obviously we want to put this in a header file soon */
5541da177e4SLinus Torvalds 	struct vidmode {
5551da177e4SLinus Torvalds 		u32 size;
5561da177e4SLinus Torvalds 		/* Don't know what this is yet */
5571da177e4SLinus Torvalds 		u16 id;
5581da177e4SLinus Torvalds 		/* Longest one I've seen so far is 26 characters */
5591da177e4SLinus Torvalds 		char name[32];
5601da177e4SLinus Torvalds 	};
5611da177e4SLinus Torvalds 
562*71ae40e4SDavid Huggins-Daines 	pr_info("    video modes supported:\n");
5631da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
564*71ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
565*71ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
5681da177e4SLinus Torvalds 	{
5691da177e4SLinus Torvalds 		struct vidmode mode;
5701da177e4SLinus Torvalds 		u32 size;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 		/* First get the length */
5731da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&size, &ent, 4);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 		/* Now clobber the whole thing */
5761da177e4SLinus Torvalds 		if (size > sizeof(mode) - 1)
5771da177e4SLinus Torvalds 			size = sizeof(mode) - 1;
5781da177e4SLinus Torvalds 		memset(&mode, 0, sizeof(mode));
5791da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&mode, &ent, size);
580*71ae40e4SDavid Huggins-Daines 		pr_info("      %02X: (%02X) %s\n", ent.type,
5811da177e4SLinus Torvalds 			mode.id, mode.name);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 	return 0;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /* This is *really* cool. */
5871da177e4SLinus Torvalds static int __init nubus_get_icon(struct nubus_board* board,
5881da177e4SLinus Torvalds 				 const struct nubus_dirent* ent)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	/* Should be 32x32 if my memory serves me correctly */
5911da177e4SLinus Torvalds 	unsigned char icon[128];
5921da177e4SLinus Torvalds 	int x, y;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	nubus_get_rsrc_mem(&icon, ent, 128);
595*71ae40e4SDavid Huggins-Daines 	pr_info("    icon:\n");
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/* We should actually plot these somewhere in the framebuffer
5981da177e4SLinus Torvalds 	   init.  This is just to demonstrate that they do, in fact,
5991da177e4SLinus Torvalds 	   exist */
6001da177e4SLinus Torvalds 	for (y = 0; y < 32; y++) {
601*71ae40e4SDavid Huggins-Daines 		pr_info("      ");
6021da177e4SLinus Torvalds 		for (x = 0; x < 32; x++) {
6031da177e4SLinus Torvalds 			if (icon[y*4 + x/8]
6041da177e4SLinus Torvalds 			    & (0x80 >> (x%8)))
605*71ae40e4SDavid Huggins-Daines 				pr_cont("*");
6061da177e4SLinus Torvalds 			else
607*71ae40e4SDavid Huggins-Daines 				pr_cont(" ");
6081da177e4SLinus Torvalds 		}
609*71ae40e4SDavid Huggins-Daines 		pr_cont("\n");
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 	return 0;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds static int __init nubus_get_vendorinfo(struct nubus_board* board,
6151da177e4SLinus Torvalds 				       const struct nubus_dirent* parent)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	struct nubus_dir    dir;
6181da177e4SLinus Torvalds 	struct nubus_dirent ent;
6191da177e4SLinus Torvalds 	static char* vendor_fields[6] = {"ID", "serial", "revision",
6201da177e4SLinus Torvalds 					 "part", "date", "unknown field"};
6211da177e4SLinus Torvalds 
622*71ae40e4SDavid Huggins-Daines 	pr_info("    vendor info:\n");
6231da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
624*71ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
625*71ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
6281da177e4SLinus Torvalds 	{
6291da177e4SLinus Torvalds 		char name[64];
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 		/* These are all strings, we think */
6321da177e4SLinus Torvalds 		nubus_get_rsrc_str(name, &ent, 64);
6331da177e4SLinus Torvalds 		if (ent.type > 5)
6341da177e4SLinus Torvalds 			ent.type = 5;
635*71ae40e4SDavid Huggins-Daines 		pr_info("    %s: %s\n", vendor_fields[ent.type - 1], name);
6361da177e4SLinus Torvalds 	}
6371da177e4SLinus Torvalds 	return 0;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
6411da177e4SLinus Torvalds 					   const struct nubus_dirent* parent)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	struct nubus_dir    dir;
6441da177e4SLinus Torvalds 	struct nubus_dirent ent;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
647*71ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
648*71ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
6511da177e4SLinus Torvalds 	{
6521da177e4SLinus Torvalds 		switch (ent.type) {
6531da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
6541da177e4SLinus Torvalds 		{
6551da177e4SLinus Torvalds 			unsigned short nbtdata[4];
6561da177e4SLinus Torvalds 			/* This type is always the same, and is not
6571da177e4SLinus Torvalds 			   useful except insofar as it tells us that
6581da177e4SLinus Torvalds 			   we really are looking at a board resource. */
6591da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
660*71ae40e4SDavid Huggins-Daines 			pr_info("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
661*71ae40e4SDavid Huggins-Daines 			        nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
6621da177e4SLinus Torvalds 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
6631da177e4SLinus Torvalds 			    nbtdata[2] != 0 || nbtdata[3] != 0)
664*71ae40e4SDavid Huggins-Daines 				pr_err("this sResource is not a board resource!\n");
6651da177e4SLinus Torvalds 			break;
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
6681da177e4SLinus Torvalds 			nubus_get_rsrc_str(board->name, &ent, 64);
669*71ae40e4SDavid Huggins-Daines 			pr_info("    name: %s\n", board->name);
6701da177e4SLinus Torvalds 			break;
6711da177e4SLinus Torvalds 		case NUBUS_RESID_ICON:
6721da177e4SLinus Torvalds 			nubus_get_icon(board, &ent);
6731da177e4SLinus Torvalds 			break;
6741da177e4SLinus Torvalds 		case NUBUS_RESID_BOARDID:
675*71ae40e4SDavid Huggins-Daines 			pr_info("    board id: 0x%x\n", ent.data);
6761da177e4SLinus Torvalds 			break;
6771da177e4SLinus Torvalds 		case NUBUS_RESID_PRIMARYINIT:
678*71ae40e4SDavid Huggins-Daines 			pr_info("    primary init offset: 0x%06x\n", ent.data);
6791da177e4SLinus Torvalds 			break;
6801da177e4SLinus Torvalds 		case NUBUS_RESID_VENDORINFO:
6811da177e4SLinus Torvalds 			nubus_get_vendorinfo(board, &ent);
6821da177e4SLinus Torvalds 			break;
6831da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
684*71ae40e4SDavid Huggins-Daines 			pr_info("    flags: 0x%06x\n", ent.data);
6851da177e4SLinus Torvalds 			break;
6861da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
687*71ae40e4SDavid Huggins-Daines 			pr_info("    hwdevid: 0x%06x\n", ent.data);
6881da177e4SLinus Torvalds 			break;
6891da177e4SLinus Torvalds 		case NUBUS_RESID_SECONDINIT:
690*71ae40e4SDavid Huggins-Daines 			pr_info("    secondary init offset: 0x%06x\n", ent.data);
6911da177e4SLinus Torvalds 			break;
6921da177e4SLinus Torvalds 			/* WTF isn't this in the functional resources? */
6931da177e4SLinus Torvalds 		case NUBUS_RESID_VIDNAMES:
6941da177e4SLinus Torvalds 			nubus_get_vidnames(board, &ent);
6951da177e4SLinus Torvalds 			break;
6961da177e4SLinus Torvalds 			/* Same goes for this */
6971da177e4SLinus Torvalds 		case NUBUS_RESID_VIDMODES:
698*71ae40e4SDavid Huggins-Daines 			pr_info("    video mode parameter directory offset: 0x%06x\n",
6991da177e4SLinus Torvalds 			       ent.data);
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		default:
702*71ae40e4SDavid Huggins-Daines 			pr_info("    unknown resource %02X, data 0x%06x\n",
7031da177e4SLinus Torvalds 			       ent.type, ent.data);
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 	return 0;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /* Attempt to bypass the somewhat non-obvious arrangement of
7101da177e4SLinus Torvalds    sResources in the motherboard ROM */
7111da177e4SLinus Torvalds static void __init nubus_find_rom_dir(struct nubus_board* board)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	unsigned char* rp;
7141da177e4SLinus Torvalds 	unsigned char* romdir;
7151da177e4SLinus Torvalds 	struct nubus_dir dir;
7161da177e4SLinus Torvalds 	struct nubus_dirent ent;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* Check for the extra directory just under the format block */
7191da177e4SLinus Torvalds 	rp = board->fblock;
7201da177e4SLinus Torvalds 	nubus_rewind(&rp, 4, board->lanes);
7211da177e4SLinus Torvalds 	if (nubus_get_rom(&rp, 4, board->lanes) != NUBUS_TEST_PATTERN) {
7221da177e4SLinus Torvalds 		/* OK, the ROM was telling the truth */
7231da177e4SLinus Torvalds 		board->directory = board->fblock;
7241da177e4SLinus Torvalds 		nubus_move(&board->directory,
7251da177e4SLinus Torvalds 			   nubus_expand32(board->doffset),
7261da177e4SLinus Torvalds 			   board->lanes);
7271da177e4SLinus Torvalds 		return;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	/* On "slot zero", you have to walk down a few more
7311da177e4SLinus Torvalds 	   directories to get to the equivalent of a real card's root
7321da177e4SLinus Torvalds 	   directory.  We don't know what they were smoking when they
7331da177e4SLinus Torvalds 	   came up with this. */
7341da177e4SLinus Torvalds 	romdir = nubus_rom_addr(board->slot);
7351da177e4SLinus Torvalds 	nubus_rewind(&romdir, ROM_DIR_OFFSET, board->lanes);
7361da177e4SLinus Torvalds 	dir.base = dir.ptr = romdir;
7371da177e4SLinus Torvalds 	dir.done = 0;
7381da177e4SLinus Torvalds 	dir.mask = board->lanes;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* This one points to an "Unknown Macintosh" directory */
7411da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1)
7421da177e4SLinus Torvalds 		goto badrom;
7431da177e4SLinus Torvalds 
744a8fe19ebSBorislav Petkov 	if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
7451da177e4SLinus Torvalds 		printk(KERN_INFO "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
7461da177e4SLinus Torvalds 	/* This one takes us to where we want to go. */
7471da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1)
7481da177e4SLinus Torvalds 		goto badrom;
749a8fe19ebSBorislav Petkov 	if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
7501da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
7511da177e4SLinus Torvalds 	nubus_get_subdir(&ent, &dir);
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	/* Resource ID 01, also an "Unknown Macintosh" */
7541da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1)
7551da177e4SLinus Torvalds 		goto badrom;
756a8fe19ebSBorislav Petkov 	if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
7571da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/* FIXME: the first one is *not* always the right one.  We
7601da177e4SLinus Torvalds 	   suspect this has something to do with the ROM revision.
7611da177e4SLinus Torvalds 	   "The HORROR ROM" (LC-series) uses 0x7e, while "The HORROR
7621da177e4SLinus Torvalds 	   Continues" (Q630) uses 0x7b.  The DAFB Macs evidently use
7631da177e4SLinus Torvalds 	   something else.  Please run "Slots" on your Mac (see
7641da177e4SLinus Torvalds 	   include/linux/nubus.h for where to get this program) and
7651da177e4SLinus Torvalds 	   tell us where the 'SiDirPtr' for Slot 0 is.  If you feel
7661da177e4SLinus Torvalds 	   brave, you should also use MacsBug to walk down the ROM
7671da177e4SLinus Torvalds 	   directories like this function does and try to find the
7681da177e4SLinus Torvalds 	   path to that address... */
7691da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1)
7701da177e4SLinus Torvalds 		goto badrom;
771a8fe19ebSBorislav Petkov 	if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
7721da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	/* Bwahahahaha... */
7751da177e4SLinus Torvalds 	nubus_get_subdir(&ent, &dir);
7761da177e4SLinus Torvalds 	board->directory = dir.base;
7771da177e4SLinus Torvalds 	return;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	/* Even more evil laughter... */
7801da177e4SLinus Torvalds  badrom:
7811da177e4SLinus Torvalds 	board->directory = board->fblock;
7821da177e4SLinus Torvalds 	nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
7831da177e4SLinus Torvalds 	printk(KERN_ERR "nubus_get_rom_dir: ROM weirdness!  Notify the developers...\n");
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /* Add a board (might be many devices) to the list */
7871da177e4SLinus Torvalds static struct nubus_board* __init nubus_add_board(int slot, int bytelanes)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	struct nubus_board* board;
7901da177e4SLinus Torvalds 	struct nubus_board** boardp;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	unsigned char *rp;
7931da177e4SLinus Torvalds 	unsigned long dpat;
7941da177e4SLinus Torvalds 	struct nubus_dir dir;
7951da177e4SLinus Torvalds 	struct nubus_dirent ent;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	/* Move to the start of the format block */
7981da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
7991da177e4SLinus Torvalds 	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
802dd00cc48SYoann Padioleau 	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
8031da177e4SLinus Torvalds 		return NULL;
8041da177e4SLinus Torvalds 	board->fblock = rp;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* Dump the format block for debugging purposes */
807*71ae40e4SDavid Huggins-Daines 	pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
808*71ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
809*71ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
810*71ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
811*71ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
812*71ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
813*71ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
814*71ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
815*71ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
8161da177e4SLinus Torvalds 	rp = board->fblock;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	board->slot = slot;
8191da177e4SLinus Torvalds 	board->slot_addr = (unsigned long) nubus_slot_addr(slot);
8201da177e4SLinus Torvalds 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
8211da177e4SLinus Torvalds 	/* rom_length is *supposed* to be the total length of the
8221da177e4SLinus Torvalds 	 * ROM.  In practice it is the "amount of ROM used to compute
8231da177e4SLinus Torvalds 	 * the CRC."  So some jokers decide to set it to zero and
8241da177e4SLinus Torvalds 	 * set the crc to zero so they don't have to do any math.
8251da177e4SLinus Torvalds 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
8261da177e4SLinus Torvalds 	 */
8271da177e4SLinus Torvalds 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
8281da177e4SLinus Torvalds 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
8291da177e4SLinus Torvalds 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
8301da177e4SLinus Torvalds 	board->format = nubus_get_rom(&rp,1, bytelanes);
8311da177e4SLinus Torvalds 	board->lanes = bytelanes;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	/* Directory offset should be small and negative... */
8341da177e4SLinus Torvalds 	if(!(board->doffset & 0x00FF0000))
835*71ae40e4SDavid Huggins-Daines 		pr_warn("Dodgy doffset!\n");
8361da177e4SLinus Torvalds 	dpat = nubus_get_rom(&rp, 4, bytelanes);
8371da177e4SLinus Torvalds 	if(dpat != NUBUS_TEST_PATTERN)
838*71ae40e4SDavid Huggins-Daines 		pr_warn("Wrong test pattern %08lx!\n", dpat);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	/*
8411da177e4SLinus Torvalds 	 *	I wonder how the CRC is meant to work -
8421da177e4SLinus Torvalds 	 *		any takers ?
8431da177e4SLinus Torvalds 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
8441da177e4SLinus Torvalds 	 * since the initial Macintosh ROM releases skipped the check.
8451da177e4SLinus Torvalds 	 */
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/* Attempt to work around slot zero weirdness */
8481da177e4SLinus Torvalds 	nubus_find_rom_dir(board);
8491da177e4SLinus Torvalds 	nubus_get_root_dir(board, &dir);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	/* We're ready to rock */
852*71ae40e4SDavid Huggins-Daines 	pr_info("Slot %X:\n", slot);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	/* Each slot should have one board resource and any number of
8551da177e4SLinus Torvalds 	   functional resources.  So we'll fill in some fields in the
8561da177e4SLinus Torvalds 	   struct nubus_board from the board resource, then walk down
8571da177e4SLinus Torvalds 	   the list of functional resources, spinning out a nubus_dev
8581da177e4SLinus Torvalds 	   for each of them. */
8591da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1) {
8601da177e4SLinus Torvalds 		/* We can't have this! */
861*71ae40e4SDavid Huggins-Daines 		pr_err("Board resource not found!\n");
8621da177e4SLinus Torvalds 		return NULL;
8631da177e4SLinus Torvalds 	} else {
864*71ae40e4SDavid Huggins-Daines 		pr_info("  Board resource:\n");
8651da177e4SLinus Torvalds 		nubus_get_board_resource(board, slot, &ent);
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* Aaaarrrrgghh!  The LC III motherboard has *two* board
8691da177e4SLinus Torvalds 	   resources.  I have no idea WTF to do about this. */
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1) {
8721da177e4SLinus Torvalds 		struct nubus_dev*  dev;
8731da177e4SLinus Torvalds 		struct nubus_dev** devp;
8741da177e4SLinus Torvalds 		dev = nubus_get_functional_resource(board, slot, &ent);
8751da177e4SLinus Torvalds 		if (dev == NULL)
8761da177e4SLinus Torvalds 			continue;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		/* We zeroed this out above */
8791da177e4SLinus Torvalds 		if (board->first_dev == NULL)
8801da177e4SLinus Torvalds 			board->first_dev = dev;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		/* Put it on the global NuBus device chain. Keep entries in order. */
8831da177e4SLinus Torvalds 		for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
8841da177e4SLinus Torvalds 			/* spin */;
8851da177e4SLinus Torvalds 		*devp = dev;
8861da177e4SLinus Torvalds 		dev->next = NULL;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/* Put it on the global NuBus board chain. Keep entries in order. */
8901da177e4SLinus Torvalds 	for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
8911da177e4SLinus Torvalds 		/* spin */;
8921da177e4SLinus Torvalds 	*boardp = board;
8931da177e4SLinus Torvalds 	board->next = NULL;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	return board;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds void __init nubus_probe_slot(int slot)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	unsigned char dp;
9011da177e4SLinus Torvalds 	unsigned char* rp;
9021da177e4SLinus Torvalds 	int i;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
9051da177e4SLinus Torvalds 	for(i = 4; i; i--)
9061da177e4SLinus Torvalds 	{
9071da177e4SLinus Torvalds 		int card_present;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 		rp--;
9101da177e4SLinus Torvalds 		card_present = hwreg_present(rp);
9111da177e4SLinus Torvalds 		if (!card_present)
9121da177e4SLinus Torvalds 			continue;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		dp = *rp;
9151da177e4SLinus Torvalds 		if(dp == 0)
9161da177e4SLinus Torvalds 			continue;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 		/* The last byte of the format block consists of two
9191da177e4SLinus Torvalds 		   nybbles which are "mirror images" of each other.
9201da177e4SLinus Torvalds 		   These show us the valid bytelanes */
9211da177e4SLinus Torvalds 		if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
9221da177e4SLinus Torvalds 			continue;
9231da177e4SLinus Torvalds 		/* Check that this value is actually *on* one of the
9241da177e4SLinus Torvalds 		   bytelanes it claims are valid! */
9251da177e4SLinus Torvalds 		if ((dp & 0x0F) >= (1<<i))
9261da177e4SLinus Torvalds 			continue;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 		/* Looks promising.  Let's put it on the list. */
9291da177e4SLinus Torvalds 		nubus_add_board(slot, dp);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 		return;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds void __init nubus_scan_bus(void)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	int slot;
9381da177e4SLinus Torvalds 	/* This might not work on your machine */
9391da177e4SLinus Torvalds #ifdef I_WANT_TO_PROBE_SLOT_ZERO
9401da177e4SLinus Torvalds 	nubus_probe_slot(0);
9411da177e4SLinus Torvalds #endif
9421da177e4SLinus Torvalds 	for(slot = 9; slot < 15; slot++)
9431da177e4SLinus Torvalds 	{
9441da177e4SLinus Torvalds 		nubus_probe_slot(slot);
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static int __init nubus_init(void)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	if (!MACH_IS_MAC)
9511da177e4SLinus Torvalds 		return 0;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	/* Initialize the NuBus interrupts */
9541da177e4SLinus Torvalds 	if (oss_present) {
9551da177e4SLinus Torvalds 		oss_nubus_init();
9561da177e4SLinus Torvalds 	} else {
9571da177e4SLinus Torvalds 		via_nubus_init();
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds #ifdef TRY_TO_DODGE_WSOD
9611da177e4SLinus Torvalds 	/* Rogue Ethernet interrupts can kill the machine if we don't
9621da177e4SLinus Torvalds 	   do this.  Obviously this is bogus.  Hopefully the local VIA
9631da177e4SLinus Torvalds 	   gurus can fix the real cause of the problem. */
9641da177e4SLinus Torvalds 	mdelay(1000);
9651da177e4SLinus Torvalds #endif
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	/* And probe */
968*71ae40e4SDavid Huggins-Daines 	pr_info("NuBus: Scanning NuBus slots.\n");
9691da177e4SLinus Torvalds 	nubus_devices = NULL;
9701da177e4SLinus Torvalds 	nubus_boards  = NULL;
9711da177e4SLinus Torvalds 	nubus_scan_bus();
9721da177e4SLinus Torvalds 	nubus_proc_init();
9731da177e4SLinus Torvalds 	return 0;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds subsys_initcall(nubus_init);
977