xref: /linux/drivers/nubus/nubus.c (revision dd00cc486ab1c17049a535413d1751ef3482141c)
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>
171da177e4SLinus Torvalds #include <asm/setup.h>
181da177e4SLinus Torvalds #include <asm/system.h>
191da177e4SLinus Torvalds #include <asm/page.h>
201da177e4SLinus Torvalds #include <asm/hwtest.h>
211da177e4SLinus Torvalds #include <linux/proc_fs.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)
1061da177e4SLinus Torvalds 		printk(KERN_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)
1231da177e4SLinus Torvalds 		printk(KERN_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 }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
1911da177e4SLinus Torvalds 			int len)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	unsigned char *t=(unsigned char *)dest;
1941da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
1951da177e4SLinus Torvalds 	while(len)
1961da177e4SLinus Torvalds 	{
1971da177e4SLinus Torvalds 		*t = nubus_get_rom(&p, 1, dirent->mask);
1981da177e4SLinus Torvalds 		if(!*t++)
1991da177e4SLinus Torvalds 			break;
2001da177e4SLinus Torvalds 		len--;
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds int nubus_get_root_dir(const struct nubus_board* board,
2051da177e4SLinus Torvalds 		       struct nubus_dir* dir)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2081da177e4SLinus Torvalds 	dir->done = 0;
2091da177e4SLinus Torvalds 	dir->mask = board->lanes;
2101da177e4SLinus Torvalds 	return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /* This is a slyly renamed version of the above */
2141da177e4SLinus Torvalds int nubus_get_func_dir(const struct nubus_dev* dev,
2151da177e4SLinus Torvalds 		       struct nubus_dir* dir)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	dir->ptr = dir->base = dev->directory;
2181da177e4SLinus Torvalds 	dir->done = 0;
2191da177e4SLinus Torvalds 	dir->mask = dev->board->lanes;
2201da177e4SLinus Torvalds 	return 0;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds int nubus_get_board_dir(const struct nubus_board* board,
2241da177e4SLinus Torvalds 			struct nubus_dir* dir)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	struct nubus_dirent ent;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2291da177e4SLinus Torvalds 	dir->done = 0;
2301da177e4SLinus Torvalds 	dir->mask = board->lanes;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/* Now dereference it (the first directory is always the board
2331da177e4SLinus Torvalds 	   directory) */
2341da177e4SLinus Torvalds 	if (nubus_readdir(dir, &ent) == -1)
2351da177e4SLinus Torvalds 		return -1;
2361da177e4SLinus Torvalds 	if (nubus_get_subdir(&ent, dir) == -1)
2371da177e4SLinus Torvalds 		return -1;
2381da177e4SLinus Torvalds 	return 0;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds int nubus_get_subdir(const struct nubus_dirent *ent,
2421da177e4SLinus Torvalds 		     struct nubus_dir *dir)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	dir->ptr = dir->base = nubus_dirptr(ent);
2451da177e4SLinus Torvalds 	dir->done = 0;
2461da177e4SLinus Torvalds 	dir->mask = ent->mask;
2471da177e4SLinus Torvalds 	return 0;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	u32 resid;
2531da177e4SLinus Torvalds 	if (nd->done)
2541da177e4SLinus Torvalds 		return -1;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* Do this first, otherwise nubus_rewind & co are off by 4 */
2571da177e4SLinus Torvalds 	ent->base = nd->ptr;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* This moves nd->ptr forward */
2601da177e4SLinus Torvalds 	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	/* EOL marker, as per the Apple docs */
2631da177e4SLinus Torvalds 	if((resid&0xff000000) == 0xff000000)
2641da177e4SLinus Torvalds 	{
2651da177e4SLinus Torvalds 		/* Mark it as done */
2661da177e4SLinus Torvalds 		nd->done = 1;
2671da177e4SLinus Torvalds 		return -1;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/* First byte is the resource ID */
2711da177e4SLinus Torvalds 	ent->type  = resid >> 24;
2721da177e4SLinus Torvalds 	/* Low 3 bytes might contain data (or might not) */
2731da177e4SLinus Torvalds 	ent->data = resid & 0xffffff;
2741da177e4SLinus Torvalds 	ent->mask  = nd->mask;
2751da177e4SLinus Torvalds 	return 0;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds int nubus_rewinddir(struct nubus_dir* dir)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	dir->ptr = dir->base;
2811da177e4SLinus Torvalds 	return 0;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /* Driver interface functions, more or less like in pci.c */
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds struct nubus_dev*
2871da177e4SLinus Torvalds nubus_find_device(unsigned short category,
2881da177e4SLinus Torvalds 		  unsigned short type,
2891da177e4SLinus Torvalds 		  unsigned short dr_hw,
2901da177e4SLinus Torvalds 		  unsigned short dr_sw,
2911da177e4SLinus Torvalds 		  const struct nubus_dev* from)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct nubus_dev* itor =
2941da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	while (itor) {
2971da177e4SLinus Torvalds 		if (itor->category == category
2981da177e4SLinus Torvalds 		    && itor->type == type
2991da177e4SLinus Torvalds 		    && itor->dr_hw == dr_hw
3001da177e4SLinus Torvalds 		    && itor->dr_sw == dr_sw)
3011da177e4SLinus Torvalds 			return itor;
3021da177e4SLinus Torvalds 		itor = itor->next;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 	return NULL;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds struct nubus_dev*
3081da177e4SLinus Torvalds nubus_find_type(unsigned short category,
3091da177e4SLinus Torvalds 		unsigned short type,
3101da177e4SLinus Torvalds 		const struct nubus_dev* from)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	struct nubus_dev* itor =
3131da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	while (itor) {
3161da177e4SLinus Torvalds 		if (itor->category == category
3171da177e4SLinus Torvalds 		    && itor->type == type)
3181da177e4SLinus Torvalds 			return itor;
3191da177e4SLinus Torvalds 		itor = itor->next;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	return NULL;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds struct nubus_dev*
3251da177e4SLinus Torvalds nubus_find_slot(unsigned int slot,
3261da177e4SLinus Torvalds 		const struct nubus_dev* from)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct nubus_dev* itor =
3291da177e4SLinus Torvalds 		from ? from->next : nubus_devices;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	while (itor) {
3321da177e4SLinus Torvalds 		if (itor->board->slot == slot)
3331da177e4SLinus Torvalds 			return itor;
3341da177e4SLinus Torvalds 		itor = itor->next;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	return NULL;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds int
3401da177e4SLinus Torvalds nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
3411da177e4SLinus Torvalds 		struct nubus_dirent* ent)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	while (nubus_readdir(dir, ent) != -1) {
3441da177e4SLinus Torvalds 		if (ent->type == rsrc_type)
3451da177e4SLinus Torvalds 			return 0;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	return -1;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /* Initialization functions - decide which slots contain stuff worth
3511da177e4SLinus Torvalds    looking at, and print out lots and lots of information from the
3521da177e4SLinus Torvalds    resource blocks. */
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /* FIXME: A lot of this stuff will eventually be useful after
3551da177e4SLinus Torvalds    initializaton, for intelligently probing Ethernet and video chips,
3561da177e4SLinus Torvalds    among other things.  The rest of it should go in the /proc code.
3571da177e4SLinus Torvalds    For now, we just use it to give verbose boot logs. */
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static int __init nubus_show_display_resource(struct nubus_dev* dev,
3601da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	switch (ent->type) {
3631da177e4SLinus Torvalds 	case NUBUS_RESID_GAMMADIR:
3641da177e4SLinus Torvalds 		printk(KERN_INFO "    gamma directory offset: 0x%06x\n", ent->data);
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	case 0x0080 ... 0x0085:
3671da177e4SLinus Torvalds 		printk(KERN_INFO "    mode %02X info offset: 0x%06x\n",
3681da177e4SLinus Torvalds 		       ent->type, ent->data);
3691da177e4SLinus Torvalds 		break;
3701da177e4SLinus Torvalds 	default:
3711da177e4SLinus Torvalds 		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
3721da177e4SLinus Torvalds 		       ent->type, ent->data);
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 	return 0;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds static int __init nubus_show_network_resource(struct nubus_dev* dev,
3781da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	switch (ent->type) {
3811da177e4SLinus Torvalds 	case NUBUS_RESID_MAC_ADDRESS:
3821da177e4SLinus Torvalds 	{
3831da177e4SLinus Torvalds 		char addr[6];
3841da177e4SLinus Torvalds 		int i;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		nubus_get_rsrc_mem(addr, ent, 6);
3871da177e4SLinus Torvalds 		printk(KERN_INFO "    MAC address: ");
3881da177e4SLinus Torvalds 		for (i = 0; i < 6; i++)
3891da177e4SLinus Torvalds 			printk("%02x%s", addr[i] & 0xff,
3901da177e4SLinus Torvalds 			       i == 5 ? "" : ":");
3911da177e4SLinus Torvalds 		printk("\n");
3921da177e4SLinus Torvalds 		break;
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	default:
3951da177e4SLinus Torvalds 		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
3961da177e4SLinus Torvalds 		       ent->type, ent->data);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 	return 0;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
4021da177e4SLinus Torvalds 					  const struct nubus_dirent* ent)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	switch (ent->type) {
4051da177e4SLinus Torvalds 	case NUBUS_RESID_MEMINFO:
4061da177e4SLinus Torvalds 	{
4071da177e4SLinus Torvalds 		unsigned long meminfo[2];
4081da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&meminfo, ent, 8);
4091da177e4SLinus Torvalds 		printk(KERN_INFO "    memory: [ 0x%08lx 0x%08lx ]\n",
4101da177e4SLinus Torvalds 		       meminfo[0], meminfo[1]);
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 	case NUBUS_RESID_ROMINFO:
4141da177e4SLinus Torvalds 	{
4151da177e4SLinus Torvalds 		unsigned long rominfo[2];
4161da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&rominfo, ent, 8);
4171da177e4SLinus Torvalds 		printk(KERN_INFO "    ROM:    [ 0x%08lx 0x%08lx ]\n",
4181da177e4SLinus Torvalds 		       rominfo[0], rominfo[1]);
4191da177e4SLinus Torvalds 		break;
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds 	default:
4221da177e4SLinus Torvalds 		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
4231da177e4SLinus Torvalds 		       ent->type, ent->data);
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	return 0;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds static int __init nubus_show_private_resource(struct nubus_dev* dev,
4291da177e4SLinus Torvalds 					      const struct nubus_dirent* ent)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	switch (dev->category) {
4321da177e4SLinus Torvalds 	case NUBUS_CAT_DISPLAY:
4331da177e4SLinus Torvalds 		nubus_show_display_resource(dev, ent);
4341da177e4SLinus Torvalds 		break;
4351da177e4SLinus Torvalds 	case NUBUS_CAT_NETWORK:
4361da177e4SLinus Torvalds 		nubus_show_network_resource(dev, ent);
4371da177e4SLinus Torvalds 		break;
4381da177e4SLinus Torvalds 	case NUBUS_CAT_CPU:
4391da177e4SLinus Torvalds 		nubus_show_cpu_resource(dev, ent);
4401da177e4SLinus Torvalds 		break;
4411da177e4SLinus Torvalds 	default:
4421da177e4SLinus Torvalds 		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
4431da177e4SLinus Torvalds 		       ent->type, ent->data);
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 	return 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds static struct nubus_dev* __init
4491da177e4SLinus Torvalds 	   nubus_get_functional_resource(struct nubus_board* board,
4501da177e4SLinus Torvalds 					 int slot,
4511da177e4SLinus Torvalds 					 const struct nubus_dirent* parent)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	struct nubus_dir    dir;
4541da177e4SLinus Torvalds 	struct nubus_dirent ent;
4551da177e4SLinus Torvalds 	struct nubus_dev*   dev;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	printk(KERN_INFO "  Function 0x%02x:\n", parent->type);
4581da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	/* Apple seems to have botched the ROM on the IIx */
4611da177e4SLinus Torvalds 	if (slot == 0 && (unsigned long)dir.base % 2)
4621da177e4SLinus Torvalds 		dir.base += 1;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (console_loglevel >= 10)
4651da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_functional_resource: parent is 0x%p, dir is 0x%p\n",
4661da177e4SLinus Torvalds 		       parent->base, dir.base);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
469*dd00cc48SYoann Padioleau 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
4701da177e4SLinus Torvalds 		return NULL;
4711da177e4SLinus Torvalds 	dev->resid = parent->type;
4721da177e4SLinus Torvalds 	dev->directory = dir.base;
4731da177e4SLinus Torvalds 	dev->board = board;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1)
4761da177e4SLinus Torvalds 	{
4771da177e4SLinus Torvalds 		switch(ent.type)
4781da177e4SLinus Torvalds 		{
4791da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
4801da177e4SLinus Torvalds 		{
4811da177e4SLinus Torvalds 			unsigned short nbtdata[4];
4821da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
4831da177e4SLinus Torvalds 			dev->category = nbtdata[0];
4841da177e4SLinus Torvalds 			dev->type     = nbtdata[1];
4851da177e4SLinus Torvalds 			dev->dr_sw    = nbtdata[2];
4861da177e4SLinus Torvalds 			dev->dr_hw    = nbtdata[3];
4871da177e4SLinus Torvalds 			printk(KERN_INFO "    type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
4881da177e4SLinus Torvalds 			       nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
4891da177e4SLinus Torvalds 			break;
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
4921da177e4SLinus Torvalds 		{
4931da177e4SLinus Torvalds 			nubus_get_rsrc_str(dev->name, &ent, 64);
4941da177e4SLinus Torvalds 			printk(KERN_INFO "    name: %s\n", dev->name);
4951da177e4SLinus Torvalds 			break;
4961da177e4SLinus Torvalds 		}
4971da177e4SLinus Torvalds 		case NUBUS_RESID_DRVRDIR:
4981da177e4SLinus Torvalds 		{
4991da177e4SLinus Torvalds 			/* MacOS driver.  If we were NetBSD we might
5001da177e4SLinus Torvalds 			   use this :-) */
5011da177e4SLinus Torvalds 			struct nubus_dir drvr_dir;
5021da177e4SLinus Torvalds 			struct nubus_dirent drvr_ent;
5031da177e4SLinus Torvalds 			nubus_get_subdir(&ent, &drvr_dir);
5041da177e4SLinus Torvalds 			nubus_readdir(&drvr_dir, &drvr_ent);
5051da177e4SLinus Torvalds 			dev->driver = nubus_dirptr(&drvr_ent);
5061da177e4SLinus Torvalds 			printk(KERN_INFO "    driver at: 0x%p\n",
5071da177e4SLinus Torvalds 			       dev->driver);
5081da177e4SLinus Torvalds 			break;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_BASEOS:
5111da177e4SLinus Torvalds 			/* We will need this in order to support
5121da177e4SLinus Torvalds 			   multiple framebuffers.  It might be handy
5131da177e4SLinus Torvalds 			   for Ethernet as well */
5141da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
5151da177e4SLinus Torvalds 			printk(KERN_INFO "    memory offset: 0x%08lx\n",
5161da177e4SLinus Torvalds 			       dev->iobase);
5171da177e4SLinus Torvalds 			break;
5181da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_LENGTH:
5191da177e4SLinus Torvalds 			/* Ditto */
5201da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
5211da177e4SLinus Torvalds 			printk(KERN_INFO "    memory length: 0x%08lx\n",
5221da177e4SLinus Torvalds 			       dev->iosize);
5231da177e4SLinus Torvalds 			break;
5241da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
5251da177e4SLinus Torvalds 			dev->flags = ent.data;
5261da177e4SLinus Torvalds 			printk(KERN_INFO "    flags: 0x%06x\n", dev->flags);
5271da177e4SLinus Torvalds 			break;
5281da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
5291da177e4SLinus Torvalds 			dev->hwdevid = ent.data;
5301da177e4SLinus Torvalds 			printk(KERN_INFO "    hwdevid: 0x%06x\n", dev->hwdevid);
5311da177e4SLinus Torvalds 			break;
5321da177e4SLinus Torvalds 		default:
5331da177e4SLinus Torvalds 			/* Local/Private resources have their own
5341da177e4SLinus Torvalds 			   function */
5351da177e4SLinus Torvalds 			nubus_show_private_resource(dev, &ent);
5361da177e4SLinus Torvalds 		}
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	return dev;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds /* This is cool. */
5431da177e4SLinus Torvalds static int __init nubus_get_vidnames(struct nubus_board* board,
5441da177e4SLinus Torvalds 				     const struct nubus_dirent* parent)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	struct nubus_dir    dir;
5471da177e4SLinus Torvalds 	struct nubus_dirent ent;
5481da177e4SLinus Torvalds 	/* FIXME: obviously we want to put this in a header file soon */
5491da177e4SLinus Torvalds 	struct vidmode {
5501da177e4SLinus Torvalds 		u32 size;
5511da177e4SLinus Torvalds 		/* Don't know what this is yet */
5521da177e4SLinus Torvalds 		u16 id;
5531da177e4SLinus Torvalds 		/* Longest one I've seen so far is 26 characters */
5541da177e4SLinus Torvalds 		char name[32];
5551da177e4SLinus Torvalds 	};
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	printk(KERN_INFO "    video modes supported:\n");
5581da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
5591da177e4SLinus Torvalds 	if (console_loglevel >= 10)
5601da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_vidnames: parent is 0x%p, dir is 0x%p\n",
5611da177e4SLinus Torvalds 		       parent->base, dir.base);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
5641da177e4SLinus Torvalds 	{
5651da177e4SLinus Torvalds 		struct vidmode mode;
5661da177e4SLinus Torvalds 		u32 size;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 		/* First get the length */
5691da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&size, &ent, 4);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 		/* Now clobber the whole thing */
5721da177e4SLinus Torvalds 		if (size > sizeof(mode) - 1)
5731da177e4SLinus Torvalds 			size = sizeof(mode) - 1;
5741da177e4SLinus Torvalds 		memset(&mode, 0, sizeof(mode));
5751da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&mode, &ent, size);
5761da177e4SLinus Torvalds 		printk (KERN_INFO "      %02X: (%02X) %s\n", ent.type,
5771da177e4SLinus Torvalds 			mode.id, mode.name);
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 	return 0;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds /* This is *really* cool. */
5831da177e4SLinus Torvalds static int __init nubus_get_icon(struct nubus_board* board,
5841da177e4SLinus Torvalds 				 const struct nubus_dirent* ent)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	/* Should be 32x32 if my memory serves me correctly */
5871da177e4SLinus Torvalds 	unsigned char icon[128];
5881da177e4SLinus Torvalds 	int x, y;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	nubus_get_rsrc_mem(&icon, ent, 128);
5911da177e4SLinus Torvalds 	printk(KERN_INFO "    icon:\n");
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	/* We should actually plot these somewhere in the framebuffer
5941da177e4SLinus Torvalds 	   init.  This is just to demonstrate that they do, in fact,
5951da177e4SLinus Torvalds 	   exist */
5961da177e4SLinus Torvalds 	for (y = 0; y < 32; y++) {
5971da177e4SLinus Torvalds 		printk(KERN_INFO "      ");
5981da177e4SLinus Torvalds 		for (x = 0; x < 32; x++) {
5991da177e4SLinus Torvalds 			if (icon[y*4 + x/8]
6001da177e4SLinus Torvalds 			    & (0x80 >> (x%8)))
6011da177e4SLinus Torvalds 				printk("*");
6021da177e4SLinus Torvalds 			else
6031da177e4SLinus Torvalds 				printk(" ");
6041da177e4SLinus Torvalds 		}
6051da177e4SLinus Torvalds 		printk("\n");
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 	return 0;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds static int __init nubus_get_vendorinfo(struct nubus_board* board,
6111da177e4SLinus Torvalds 				       const struct nubus_dirent* parent)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	struct nubus_dir    dir;
6141da177e4SLinus Torvalds 	struct nubus_dirent ent;
6151da177e4SLinus Torvalds 	static char* vendor_fields[6] = {"ID", "serial", "revision",
6161da177e4SLinus Torvalds 					 "part", "date", "unknown field"};
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	printk(KERN_INFO "    vendor info:\n");
6191da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
6201da177e4SLinus Torvalds 	if (console_loglevel >= 10)
6211da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_vendorinfo: parent is 0x%p, dir is 0x%p\n",
6221da177e4SLinus Torvalds 		       parent->base, dir.base);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
6251da177e4SLinus Torvalds 	{
6261da177e4SLinus Torvalds 		char name[64];
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 		/* These are all strings, we think */
6291da177e4SLinus Torvalds 		nubus_get_rsrc_str(name, &ent, 64);
6301da177e4SLinus Torvalds 		if (ent.type > 5)
6311da177e4SLinus Torvalds 			ent.type = 5;
6321da177e4SLinus Torvalds 		printk(KERN_INFO "    %s: %s\n",
6331da177e4SLinus Torvalds 		       vendor_fields[ent.type-1], name);
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 	return 0;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
6391da177e4SLinus Torvalds 					   const struct nubus_dirent* parent)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct nubus_dir    dir;
6421da177e4SLinus Torvalds 	struct nubus_dirent ent;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
6451da177e4SLinus Torvalds 	if (console_loglevel >= 10)
6461da177e4SLinus Torvalds 		printk(KERN_DEBUG "nubus_get_board_resource: parent is 0x%p, dir is 0x%p\n",
6471da177e4SLinus Torvalds 		       parent->base, dir.base);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	while(nubus_readdir(&dir, &ent) != -1)
6501da177e4SLinus Torvalds 	{
6511da177e4SLinus Torvalds 		switch (ent.type) {
6521da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
6531da177e4SLinus Torvalds 		{
6541da177e4SLinus Torvalds 			unsigned short nbtdata[4];
6551da177e4SLinus Torvalds 			/* This type is always the same, and is not
6561da177e4SLinus Torvalds 			   useful except insofar as it tells us that
6571da177e4SLinus Torvalds 			   we really are looking at a board resource. */
6581da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
6591da177e4SLinus Torvalds 			printk(KERN_INFO "    type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
6601da177e4SLinus Torvalds 			       nbtdata[0], nbtdata[1], nbtdata[2],
6611da177e4SLinus Torvalds 			       nbtdata[3]);
6621da177e4SLinus Torvalds 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
6631da177e4SLinus Torvalds 			    nbtdata[2] != 0 || nbtdata[3] != 0)
6641da177e4SLinus Torvalds 				printk(KERN_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);
6691da177e4SLinus Torvalds 			printk(KERN_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:
6751da177e4SLinus Torvalds 			printk(KERN_INFO "    board id: 0x%x\n", ent.data);
6761da177e4SLinus Torvalds 			break;
6771da177e4SLinus Torvalds 		case NUBUS_RESID_PRIMARYINIT:
6781da177e4SLinus Torvalds 			printk(KERN_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:
6841da177e4SLinus Torvalds 			printk(KERN_INFO "    flags: 0x%06x\n", ent.data);
6851da177e4SLinus Torvalds 			break;
6861da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
6871da177e4SLinus Torvalds 			printk(KERN_INFO "    hwdevid: 0x%06x\n", ent.data);
6881da177e4SLinus Torvalds 			break;
6891da177e4SLinus Torvalds 		case NUBUS_RESID_SECONDINIT:
6901da177e4SLinus Torvalds 			printk(KERN_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:
6981da177e4SLinus Torvalds 			printk(KERN_INFO "    video mode parameter directory offset: 0x%06x\n",
6991da177e4SLinus Torvalds 			       ent.data);
7001da177e4SLinus Torvalds 			break;
7011da177e4SLinus Torvalds 		default:
7021da177e4SLinus Torvalds 			printk(KERN_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 
7441da177e4SLinus Torvalds 	if (console_loglevel >= 10)
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;
7491da177e4SLinus Torvalds 	if (console_loglevel >= 10)
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;
7561da177e4SLinus Torvalds 	if (console_loglevel >= 10)
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;
7711da177e4SLinus Torvalds 	if (console_loglevel >= 10)
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 */
802*dd00cc48SYoann 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 */
8071da177e4SLinus Torvalds 	if (console_loglevel >= 10) {
8081da177e4SLinus Torvalds 		int i;
8091da177e4SLinus Torvalds 		printk(KERN_DEBUG "Slot %X, format block at 0x%p\n",
8101da177e4SLinus Torvalds 		       slot, rp);
8111da177e4SLinus Torvalds 		printk(KERN_DEBUG "Format block: ");
8121da177e4SLinus Torvalds 		for (i = 0; i < FORMAT_BLOCK_SIZE; i += 4) {
8131da177e4SLinus Torvalds 			unsigned short foo, bar;
8141da177e4SLinus Torvalds 			foo = nubus_get_rom(&rp, 2, bytelanes);
8151da177e4SLinus Torvalds 			bar = nubus_get_rom(&rp, 2, bytelanes);
8161da177e4SLinus Torvalds 			printk("%04x %04x  ", foo, bar);
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 		printk("\n");
8191da177e4SLinus Torvalds 		rp = board->fblock;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	board->slot = slot;
8231da177e4SLinus Torvalds 	board->slot_addr = (unsigned long) nubus_slot_addr(slot);
8241da177e4SLinus Torvalds 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
8251da177e4SLinus Torvalds 	/* rom_length is *supposed* to be the total length of the
8261da177e4SLinus Torvalds 	 * ROM.  In practice it is the "amount of ROM used to compute
8271da177e4SLinus Torvalds 	 * the CRC."  So some jokers decide to set it to zero and
8281da177e4SLinus Torvalds 	 * set the crc to zero so they don't have to do any math.
8291da177e4SLinus Torvalds 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
8301da177e4SLinus Torvalds 	 */
8311da177e4SLinus Torvalds 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
8321da177e4SLinus Torvalds 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
8331da177e4SLinus Torvalds 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
8341da177e4SLinus Torvalds 	board->format = nubus_get_rom(&rp,1, bytelanes);
8351da177e4SLinus Torvalds 	board->lanes = bytelanes;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	/* Directory offset should be small and negative... */
8381da177e4SLinus Torvalds 	if(!(board->doffset & 0x00FF0000))
8391da177e4SLinus Torvalds 		printk(KERN_WARNING "Dodgy doffset!\n");
8401da177e4SLinus Torvalds 	dpat = nubus_get_rom(&rp, 4, bytelanes);
8411da177e4SLinus Torvalds 	if(dpat != NUBUS_TEST_PATTERN)
8421da177e4SLinus Torvalds 		printk(KERN_WARNING "Wrong test pattern %08lx!\n", dpat);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/*
8451da177e4SLinus Torvalds 	 *	I wonder how the CRC is meant to work -
8461da177e4SLinus Torvalds 	 *		any takers ?
8471da177e4SLinus Torvalds 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
8481da177e4SLinus Torvalds 	 * since the initial Macintosh ROM releases skipped the check.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	/* Attempt to work around slot zero weirdness */
8521da177e4SLinus Torvalds 	nubus_find_rom_dir(board);
8531da177e4SLinus Torvalds 	nubus_get_root_dir(board, &dir);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/* We're ready to rock */
8561da177e4SLinus Torvalds 	printk(KERN_INFO "Slot %X:\n", slot);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/* Each slot should have one board resource and any number of
8591da177e4SLinus Torvalds 	   functional resources.  So we'll fill in some fields in the
8601da177e4SLinus Torvalds 	   struct nubus_board from the board resource, then walk down
8611da177e4SLinus Torvalds 	   the list of functional resources, spinning out a nubus_dev
8621da177e4SLinus Torvalds 	   for each of them. */
8631da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1) {
8641da177e4SLinus Torvalds 		/* We can't have this! */
8651da177e4SLinus Torvalds 		printk(KERN_ERR "Board resource not found!\n");
8661da177e4SLinus Torvalds 		return NULL;
8671da177e4SLinus Torvalds 	} else {
8681da177e4SLinus Torvalds 		printk(KERN_INFO "  Board resource:\n");
8691da177e4SLinus Torvalds 		nubus_get_board_resource(board, slot, &ent);
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	/* Aaaarrrrgghh!  The LC III motherboard has *two* board
8731da177e4SLinus Torvalds 	   resources.  I have no idea WTF to do about this. */
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1) {
8761da177e4SLinus Torvalds 		struct nubus_dev*  dev;
8771da177e4SLinus Torvalds 		struct nubus_dev** devp;
8781da177e4SLinus Torvalds 		dev = nubus_get_functional_resource(board, slot, &ent);
8791da177e4SLinus Torvalds 		if (dev == NULL)
8801da177e4SLinus Torvalds 			continue;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		/* We zeroed this out above */
8831da177e4SLinus Torvalds 		if (board->first_dev == NULL)
8841da177e4SLinus Torvalds 			board->first_dev = dev;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 		/* Put it on the global NuBus device chain. Keep entries in order. */
8871da177e4SLinus Torvalds 		for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
8881da177e4SLinus Torvalds 			/* spin */;
8891da177e4SLinus Torvalds 		*devp = dev;
8901da177e4SLinus Torvalds 		dev->next = NULL;
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	/* Put it on the global NuBus board chain. Keep entries in order. */
8941da177e4SLinus Torvalds 	for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
8951da177e4SLinus Torvalds 		/* spin */;
8961da177e4SLinus Torvalds 	*boardp = board;
8971da177e4SLinus Torvalds 	board->next = NULL;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	return board;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds void __init nubus_probe_slot(int slot)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	unsigned char dp;
9051da177e4SLinus Torvalds 	unsigned char* rp;
9061da177e4SLinus Torvalds 	int i;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
9091da177e4SLinus Torvalds 	for(i = 4; i; i--)
9101da177e4SLinus Torvalds 	{
9111da177e4SLinus Torvalds 		unsigned long flags;
9121da177e4SLinus Torvalds 		int card_present;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		rp--;
9151da177e4SLinus Torvalds 		local_irq_save(flags);
9161da177e4SLinus Torvalds 		card_present = hwreg_present(rp);
9171da177e4SLinus Torvalds 		local_irq_restore(flags);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 		if (!card_present)
9201da177e4SLinus Torvalds 			continue;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		printk(KERN_DEBUG "Now probing slot %X at %p\n", slot, rp);
9231da177e4SLinus Torvalds 		dp = *rp;
9241da177e4SLinus Torvalds 		if(dp == 0)
9251da177e4SLinus Torvalds 			continue;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 		/* The last byte of the format block consists of two
9281da177e4SLinus Torvalds 		   nybbles which are "mirror images" of each other.
9291da177e4SLinus Torvalds 		   These show us the valid bytelanes */
9301da177e4SLinus Torvalds 		if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
9311da177e4SLinus Torvalds 			continue;
9321da177e4SLinus Torvalds 		/* Check that this value is actually *on* one of the
9331da177e4SLinus Torvalds 		   bytelanes it claims are valid! */
9341da177e4SLinus Torvalds 		if ((dp & 0x0F) >= (1<<i))
9351da177e4SLinus Torvalds 			continue;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 		/* Looks promising.  Let's put it on the list. */
9381da177e4SLinus Torvalds 		nubus_add_board(slot, dp);
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 		return;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds #if defined(CONFIG_PROC_FS)
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds /* /proc/nubus stuff */
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static int sprint_nubus_board(struct nubus_board* board, char* ptr, int len)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	if(len < 100)
9511da177e4SLinus Torvalds 		return -1;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	sprintf(ptr, "Slot %X: %s\n",
9541da177e4SLinus Torvalds 		board->slot, board->name);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	return strlen(ptr);
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds static int nubus_read_proc(char *page, char **start, off_t off,
9601da177e4SLinus Torvalds 				int count, int *eof, void *data)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	int nprinted, len, begin = 0;
9631da177e4SLinus Torvalds 	int size = PAGE_SIZE;
9641da177e4SLinus Torvalds 	struct nubus_board* board;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	len   = sprintf(page, "Nubus devices found:\n");
9671da177e4SLinus Torvalds 	/* Walk the list of NuBus boards */
9681da177e4SLinus Torvalds 	for (board = nubus_boards; board != NULL; board = board->next)
9691da177e4SLinus Torvalds 	{
9701da177e4SLinus Torvalds 		nprinted = sprint_nubus_board(board, page + len, size - len);
9711da177e4SLinus Torvalds 		if (nprinted < 0)
9721da177e4SLinus Torvalds 			break;
9731da177e4SLinus Torvalds 		len += nprinted;
9741da177e4SLinus Torvalds 		if (len+begin < off) {
9751da177e4SLinus Torvalds 			begin += len;
9761da177e4SLinus Torvalds 			len = 0;
9771da177e4SLinus Torvalds 		}
9781da177e4SLinus Torvalds 		if (len+begin >= off+count)
9791da177e4SLinus Torvalds 			break;
9801da177e4SLinus Torvalds 	}
9811da177e4SLinus Torvalds 	if (len+begin < off)
9821da177e4SLinus Torvalds 		*eof = 1;
9831da177e4SLinus Torvalds 	off -= begin;
9841da177e4SLinus Torvalds 	*start = page + off;
9851da177e4SLinus Torvalds 	len -= off;
9861da177e4SLinus Torvalds 	if (len>count)
9871da177e4SLinus Torvalds 		len = count;
9881da177e4SLinus Torvalds 	if (len<0)
9891da177e4SLinus Torvalds 		len = 0;
9901da177e4SLinus Torvalds 	return len;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds #endif
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds void __init nubus_scan_bus(void)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	int slot;
9971da177e4SLinus Torvalds 	/* This might not work on your machine */
9981da177e4SLinus Torvalds #ifdef I_WANT_TO_PROBE_SLOT_ZERO
9991da177e4SLinus Torvalds 	nubus_probe_slot(0);
10001da177e4SLinus Torvalds #endif
10011da177e4SLinus Torvalds 	for(slot = 9; slot < 15; slot++)
10021da177e4SLinus Torvalds 	{
10031da177e4SLinus Torvalds 		nubus_probe_slot(slot);
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds static int __init nubus_init(void)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	if (!MACH_IS_MAC)
10101da177e4SLinus Torvalds 		return 0;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	/* Initialize the NuBus interrupts */
10131da177e4SLinus Torvalds 	if (oss_present) {
10141da177e4SLinus Torvalds 		oss_nubus_init();
10151da177e4SLinus Torvalds 	} else {
10161da177e4SLinus Torvalds 		via_nubus_init();
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds #ifdef TRY_TO_DODGE_WSOD
10201da177e4SLinus Torvalds 	/* Rogue Ethernet interrupts can kill the machine if we don't
10211da177e4SLinus Torvalds 	   do this.  Obviously this is bogus.  Hopefully the local VIA
10221da177e4SLinus Torvalds 	   gurus can fix the real cause of the problem. */
10231da177e4SLinus Torvalds 	mdelay(1000);
10241da177e4SLinus Torvalds #endif
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* And probe */
10271da177e4SLinus Torvalds 	printk("NuBus: Scanning NuBus slots.\n");
10281da177e4SLinus Torvalds 	nubus_devices = NULL;
10291da177e4SLinus Torvalds 	nubus_boards  = NULL;
10301da177e4SLinus Torvalds 	nubus_scan_bus();
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
10331da177e4SLinus Torvalds 	create_proc_read_entry("nubus", 0, NULL, nubus_read_proc, NULL);
10341da177e4SLinus Torvalds 	nubus_proc_init();
10351da177e4SLinus Torvalds #endif
10361da177e4SLinus Torvalds 	return 0;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds subsys_initcall(nubus_init);
1040