patch-2.2.18 linux/drivers/nubus/nubus.c

Next file: linux/drivers/nubus/nubus_syms.c
Previous file: linux/drivers/nubus/Makefile
Back to the patch index
Back to the overall index

diff -u --new-file --recursive --exclude-from /usr/src/exclude v2.2.17/drivers/nubus/nubus.c linux/drivers/nubus/nubus.c
@@ -1,78 +1,112 @@
 /*
  *	Macintosh Nubus Interface Code
+ *
+ *      Originally by Alan Cox
+ *
+ *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
+ *      and others.
  */
 
 #include <linux/config.h>
 #include <linux/ptrace.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
-#include <linux/bios32.h>
-#include <linux/pci.h>
 #include <linux/string.h>
 #include <linux/nubus.h>
 #include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/delay.h>
 #include <asm/setup.h>
 #include <asm/system.h>
 #include <asm/page.h>
 #include <asm/hwtest.h>
-/* for LCIII stuff; better find a general way like MACH_HAS_NUBUS */
-#include <asm/macintosh.h>
 #include <linux/proc_fs.h>
+#include <asm/mac_via.h>
+#include <asm/mac_oss.h>
 
+extern void via_nubus_init(void);
+extern void oss_nubus_init(void);
 
-#undef LCIII_WEIRDNESS
- 
-static struct nubus_slot nubus_slots[16];
- 
-/*
- *	Please skip to the bottom of this file if you ate lunch recently
- *				-- Alan
- */
- 
+/* Constants */
 
-/*
- *	Yes this sucks. The ROM can appear on arbitary bytes of the long
- *	word. We are not amused.
- */
+/* This is, of course, the size in bytelanes, rather than the size in
+   actual bytes */
+#define FORMAT_BLOCK_SIZE 20
+#define ROM_DIR_OFFSET 0x24
+
+#define NUBUS_TEST_PATTERN 0x5A932BC7
+
+/* Globals */
+
+struct nubus_dev*   nubus_devices;
+struct nubus_board* nubus_boards;
+
+extern int console_loglevel;
+
+/* Meaning of "bytelanes":
+
+   The card ROM may appear on any or all bytes of each long word in
+   NuBus memory.  The low 4 bits of the "map" value found in the
+   format block (at the top of the slot address space, as well as at
+   the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
+   offsets within each longword, are valid.  Thus:
+
+   A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
+   are valid.
+
+   A map of 0xf0 means that no bytelanes are valid (We pray that we
+   will never encounter this, but stranger things have happened)
+
+   A map of 0xe1 means that only the MSB of each long word is actually
+   part of the card ROM.  (We hope to never encounter NuBus on a
+   little-endian machine.  Again, stranger things have happened)
+
+   A map of 0x78 means that only the LSB of each long word is valid.
+
+   Etcetera, etcetera.  Hopefully this clears up some confusion over
+   what the following code actually does.  */
  
-extern __inline__ int not_useful(void *p, int map)
+extern inline int not_useful(void *p, int map)
 {
 	unsigned long pv=(unsigned long)p;
-	pv&=3;
-	if(map&(1<<pv))
+	pv &= 3;
+	if(map & (1<<pv))
 		return 0;
 	return 1;
 }
  
 static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
 {
-	unsigned long v=0;
-	unsigned char *p=*ptr;	/* as v|=*((*ptr)++) upset someone */
+	/* This will hold the result */
+	unsigned long v = 0;
+	unsigned char *p = *ptr;
+
 	while(len)
 	{
-		v<<=8;
+		v <<= 8;
 		while(not_useful(p,map))
 			p++;
-		v|=*p++;
+		v |= *p++;
 		len--;
 	}
-	*ptr=p;
+	*ptr = p;
 	return v;
 }
 
 static void nubus_rewind(unsigned char **ptr, int len, int map)
 {
 	unsigned char *p=*ptr;
-	
-	if(len>65536)
-		printk("rewind of %d!\n", len);
+
+	/* Sanity check */
+	if(len > 65536)
+		printk(KERN_ERR "rewind of 0x%08x!\n", len);
 	while(len)
 	{
 		do
 		{
 			p--;
 		}
-		while(not_useful(p,map));
+		while(not_useful(p, map));
 		len--;
 	}
 	*ptr=p;
@@ -80,9 +114,9 @@
 
 static void nubus_advance(unsigned char **ptr, int len, int map)
 {
-	unsigned char *p=*ptr;
+	unsigned char *p = *ptr;
 	if(len>65536)
-		printk("advance of %d!\n", len);
+		printk(KERN_ERR "advance of 0x%08x!\n", len);
 	while(len)
 	{
 		while(not_useful(p,map))
@@ -90,29 +124,32 @@
 			p++;
 		len--;
 	}
-	*ptr=p;
+	*ptr = p;
 }
 
-/*
- *	24bit signed offset to 32bit
- */
- 
-static unsigned long nubus_expand32(unsigned long foo)
+static void nubus_move(unsigned char **ptr, int len, int map)
 {
-	if(foo&0x00800000)	/* 24bit negative */
-		foo|=0xFF000000;
-	return foo;
+	if(len > 0)
+		nubus_advance(ptr, len, map);
+	else if(len < 0)
+		nubus_rewind(ptr, -len, map);
 }
 
-static void nubus_move(unsigned char **ptr, int len, int map)
+/* Now, functions to read the sResource tree */
+
+/* Each sResource entry consists of a 1-byte ID and a 3-byte data
+   field.  If that data field contains an offset, then obviously we
+   have to expand it from a 24-bit signed number to a 32-bit signed
+   number. */
+
+extern inline long nubus_expand32(long foo)
 {
-	if(len>0)
-		nubus_advance(ptr,len,map);
-	else if(len<0)
-		nubus_rewind(ptr,-len,map);
+	if(foo & 0x00800000)	/* 24bit negative */
+		foo |= 0xFF000000;
+	return foo;
 }
 
-static void *nubus_rom_addr(int slot)
+extern inline void *nubus_rom_addr(int slot)
 {	
 	/*
 	 *	Returns the first byte after the card. We then walk
@@ -121,472 +158,734 @@
 	return (void *)(0xF1000000+(slot<<24));
 }
 
-void nubus_memcpy(int slot, void *to, unsigned char *p, int len)
+static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
 {
-	unsigned char *t=(unsigned char *)to;
+	unsigned char *p = nd->base;
+	/* Essentially, just step over the bytelanes using whatever
+	   offset we might have found */
+	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
+	/* And return the value */
+	return p;
+}
+
+/* These two are for pulling resource data blocks (i.e. stuff that's
+   pointed to with offsets) out of the card ROM. */
+
+void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent,
+			int len)
+{
+	unsigned char *t = (unsigned char *)dest;
+	unsigned char *p = nubus_dirptr(dirent);
 	while(len)
 	{
-		*t++=nubus_get_rom(&p,1, nubus_slots[slot].slot_lanes);
+		*t++ = nubus_get_rom(&p, 1, dirent->mask);
 		len--;
 	}
 }
 
-void nubus_strncpy(int slot, void *to, unsigned char *p, int len)
+void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent,
+			int len)
 {
-	unsigned char *t=(unsigned char *)to;
+	unsigned char *t=(unsigned char *)dest;
+	unsigned char *p = nubus_dirptr(dirent);
 	while(len)
 	{
-		*t=nubus_get_rom(&p,1, nubus_slots[slot].slot_lanes);
+		*t = nubus_get_rom(&p, 1, dirent->mask);
 		if(!*t++)
 			break;
 		len--;
 	}
 }
 
-
-
-	
-unsigned char *nubus_dirptr(struct nubus_dirent *nd)
+int nubus_get_root_dir(const struct nubus_board* board,
+		       struct nubus_dir* dir)
 {
-	unsigned char *p=(unsigned char *)(nd->base);
-	
-	nubus_move(&p, nubus_expand32(nd->value), nd->mask);
-	return p;
+	dir->ptr = dir->base = board->directory;
+	dir->done = 0;
+	dir->mask = board->lanes;
+	return 0;
 }
 
-	
-struct nubus_dir *nubus_openrootdir(int slot)
-{
-	static struct nubus_dir nbdir;
-	unsigned char *rp=nubus_rom_addr(slot);
-	
-	nubus_rewind(&rp,20, nubus_slots[slot].slot_lanes);
-
-	nubus_move(&rp, nubus_expand32(nubus_slots[slot].slot_directory),
-		nubus_slots[slot].slot_lanes);
-		
-	nbdir.base=rp;
-	nbdir.length=nubus_slots[slot].slot_dlength;
-	nbdir.count=0;
-	nbdir.mask=nubus_slots[slot].slot_lanes;
-	return &nbdir;
+/* This is a slyly renamed version of the above */
+int nubus_get_func_dir(const struct nubus_dev* dev,
+		       struct nubus_dir* dir)
+{
+	dir->ptr = dir->base = dev->directory;
+	dir->done = 0;
+	dir->mask = dev->board->lanes;
+	return 0;
 }
 
-struct nubus_dir *nubus_opensubdir(struct nubus_dirent *d)
+int nubus_get_board_dir(const struct nubus_board* board,
+			struct nubus_dir* dir)
 {
-	static struct nubus_dir nbdir;
-	unsigned char *rp=nubus_dirptr(d);
-	nbdir.base=rp;
-	nbdir.length=99999;/*slots[i].slot_dlength;*/
-	nbdir.count=0;
-	nbdir.mask=d->mask;
-	return &nbdir;
+	struct nubus_dirent ent;
+	
+	dir->ptr = dir->base = board->directory;
+	dir->done = 0;
+	dir->mask = board->lanes;
+
+	/* Now dereference it (the first directory is always the board
+	   directory) */
+	if (nubus_readdir(dir, &ent) == -1)
+		return -1;
+	if (nubus_get_subdir(&ent, dir) == -1)
+		return -1;
+	return 0;
 }
 
-void nubus_closedir(struct nubus_dir *nd)
+int nubus_get_subdir(const struct nubus_dirent *ent,
+		     struct nubus_dir *dir)
 {
-	;
+	dir->ptr = dir->base = nubus_dirptr(ent);
+	dir->done = 0;
+	dir->mask = ent->mask;
+	return 0;
 }
 
-struct nubus_dirent *nubus_readdir(struct nubus_dir *nd)
+int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
 {
 	u32 resid;
-	u8 rescode;
-	static struct nubus_dirent d;
-	
-	if(nd->count==nd->length)
-		return NULL;
+	if (nd->done)
+		return -1;
 
-	d.base=(unsigned long)nd->base;
-		
-	resid=nubus_get_rom(&nd->base, 4, nd->mask);
-	nd->count++;
-	rescode=resid>>24;
-	if(rescode==0xFF)
-	{
-		nd->count=nd->length;
-		return NULL;
-	}
-	d.type=rescode;
-	d.value=resid&0xFFFFFF;
-	d.mask=nd->mask;
-	return &d;
-}
+	/* Do this first, otherwise nubus_rewind & co are off by 4 */
+	ent->base = nd->ptr;
 
-/*
- *	MAC video handling irritations
- */
+	/* This moves nd->ptr forward */
+	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
 
-static unsigned char nubus_vid_byte[16];
-static unsigned long nubus_vid_offset[16];
+	/* EOL marker, as per the Apple docs */
+	if((resid&0xff000000) == 0xff000000)
+	{
+		/* Mark it as done */
+		nd->done = 1;
+		return -1;
+	}
 
-static void nubus_irqsplat(int slot, void *dev_id, struct pt_regs *regs)
-{
-	unsigned char *p=((unsigned char *)nubus_slot_addr(slot))+
-			nubus_vid_offset[slot];
-	*p=nubus_vid_byte[slot];
+	/* First byte is the resource ID */
+	ent->type  = resid >> 24;
+	/* Low 3 bytes might contain data (or might not) */
+	ent->data = resid & 0xffffff;
+	ent->mask  = nd->mask;
+	return 0;
 }
 
-static int nubus_add_irqsplatter(int slot, unsigned long ptr, unsigned char v)
+int nubus_rewinddir(struct nubus_dir* dir)
 {
-	nubus_vid_byte[slot]=v;
-	nubus_vid_offset[slot]=ptr;
-	nubus_request_irq(slot, NULL, nubus_irqsplat);
+	dir->ptr = dir->base;
+	dir->done = 0;
 	return 0;
 }
- 
-void nubus_video_shutup(int slot, struct nubus_type *nt)
-{
-	if(nt->category!=3 /* Display */ || nt->type!=1 /* Video */
-		|| nt->DrSW!=1 /* Quickdraw device */)
-		return;
-	switch(nt->DrHW)
-	{
-		/*
-		 *	Toby and MacII Hires cards. These behave in a MacII
-		 *	anyway but not on an RBV box
-		 */
-		case 0x0001:
-		case 0x0013:
-			nubus_add_irqsplatter(slot, 0xA0000, 0);
-			break;
-		/*
-		 *	Apple workstation video card.
-		 */
-		case 0x0006:
-			nubus_add_irqsplatter(slot, 0xA00000, 0);
-			break;
-		/*
-		 *	Futura cards
-		 */
-		case 0x0417:
-		case 0x042F:
-			nubus_add_irqsplatter(slot, 0xF05000, 0x80);
-			break;
-			
-		/*
-		 *	Fingers crossed 8)
-		 *	
-		 *	If you have another card and an RBV based mac you'll
-		 *	almost certainly have to add it here to make it work.
-		 */
-		
-		default:
-			break;
+
+/* Driver interface functions, more or less like in pci.c */
+
+struct nubus_dev*
+nubus_find_device(unsigned short category,
+		  unsigned short type,
+		  unsigned short dr_hw,
+		  unsigned short dr_sw,
+		  const struct nubus_dev* from)
+{
+	struct nubus_dev* itor =
+		from ? from->next : nubus_devices;
+
+	while (itor) {
+		if (itor->category == category
+		    && itor->type == type
+		    && itor->dr_hw == dr_hw
+		    && itor->dr_sw == dr_sw)
+			return itor;
+		itor = itor->next;
 	}
+	return NULL;
 }
 
-/*
- *	Device list
- */
+struct nubus_dev*
+nubus_find_type(unsigned short category,
+		unsigned short type,
+		const struct nubus_dev* from)
+{
+	struct nubus_dev* itor =
+		from ? from->next : nubus_devices;
+
+	while (itor) {
+		if (itor->category == category
+		    && itor->type == type)
+			return itor;
+		itor = itor->next;
+	}
+	return NULL;
+}
 
-static struct nubus_device_specifier *nubus_device_list=NULL;
- 
-void register_nubus_device(struct nubus_device_specifier *d)
-{
-	d->next=nubus_device_list;
-	nubus_device_list=d;
+struct nubus_dev*
+nubus_find_slot(unsigned int slot,
+		const struct nubus_dev* from)
+{
+	struct nubus_dev* itor =
+		from ? from->next : nubus_devices;
+	
+	while (itor) {
+		if (itor->board->slot == slot)
+			return itor;
+		itor = itor->next;
+	}
+	return NULL;
 }
 
-void unregister_nubus_device(struct nubus_device_specifier *nb)
-{
-	struct nubus_device_specifier **t=&nubus_device_list;
-	while(*t!=nb && *t)
-		t=&((*t)->next);
-	*t=nb->next;
+int
+nubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type,
+		struct nubus_dirent* ent)
+{
+	while (nubus_readdir(dir, ent) != -1) {
+		if (ent->type == rsrc_type)
+			return 0;
+	}	
+	return -1;
+}
+
+/* Initialization functions - decide which slots contain stuff worth
+   looking at, and print out lots and lots of information from the
+   resource blocks. */
+
+/* FIXME: A lot of this stuff will eventually be useful after
+   initializaton, for intelligently probing Ethernet and video chips,
+   among other things.  The rest of it should go in the /proc code.
+   For now, we just use it to give verbose boot logs. */
+
+static int __init nubus_show_display_resource(struct nubus_dev* dev,
+					      const struct nubus_dirent* ent)
+{
+	switch (ent->type) {
+	case NUBUS_RESID_GAMMADIR:
+		printk(KERN_INFO "    gamma directory offset: 0x%06x\n", ent->data);
+		break;
+	case 0x0080 ... 0x0085:
+		printk(KERN_INFO "    mode %02X info offset: 0x%06x\n",
+		       ent->type, ent->data);
+		break;
+	default:
+		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
+		       ent->type, ent->data);
+	}
+	return 0;
 }
 
-static struct nubus_device_specifier *find_nubus_device(int slot, struct nubus_type *nt)
+static int __init nubus_show_network_resource(struct nubus_dev* dev,
+					      const struct nubus_dirent* ent)
 {
-	struct nubus_device_specifier *t=nubus_device_list; 
-	while(t!=NULL)
+	switch (ent->type) {
+	case NUBUS_RESID_MAC_ADDRESS:
 	{
-		if(t->setup(t,slot, nt)==0)
-			return t;
-		t=t->next;
+		char addr[6];
+		int i;
+		
+		nubus_get_rsrc_mem(addr, ent, 6);
+		printk(KERN_INFO "    MAC address: ");
+		for (i = 0; i < 6; i++)
+			printk("%02x%s", addr[i] & 0xff,
+			       i == 5 ? "" : ":");
+		printk("\n");
+		break;
+	}
+	default:
+		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
+		       ent->type, ent->data);
 	}
-	printk("No driver for device [%d %d %d %d]\n",
-		nt->category, nt->type, nt->DrHW, nt->DrSW);
-	return NULL;
+	return 0;
 }
 
-/*
- *	Probe a nubus slot
- */
+static int __init nubus_show_cpu_resource(struct nubus_dev* dev,
+					  const struct nubus_dirent* ent)
+{
+	switch (ent->type) {
+	case NUBUS_RESID_MEMINFO:
+	{
+		unsigned long meminfo[2];
+		nubus_get_rsrc_mem(&meminfo, ent, 8);
+		printk(KERN_INFO "    memory: [ 0x%08lx 0x%08lx ]\n",
+		       meminfo[0], meminfo[1]);
+		break;
+	}
+	case NUBUS_RESID_ROMINFO:
+	{
+		unsigned long rominfo[2];
+		nubus_get_rsrc_mem(&rominfo, ent, 8);
+		printk(KERN_INFO "    ROM:    [ 0x%08lx 0x%08lx ]\n",
+		       rominfo[0], rominfo[1]);
+		break;
+	}
+	default:
+		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
+		       ent->type, ent->data);
+	}
+	return 0;
+}
 
-void nubus_probe_slot(int slot, int mode)
+static int __init nubus_show_private_resource(struct nubus_dev* dev,
+					      const struct nubus_dirent* ent)
 {
-	unsigned char *rp;
-	unsigned char dp;
-	int lanes;
-	int i;
-	unsigned long dpat;
-	struct nubus_dir *dir;
-	struct nubus_dirent *nd;
-	struct nubus_type type_info;
+	switch (dev->category) {
+	case NUBUS_CAT_DISPLAY:
+		nubus_show_display_resource(dev, ent);
+		break;
+	case NUBUS_CAT_NETWORK:
+		nubus_show_network_resource(dev, ent);
+		break;
+	case NUBUS_CAT_CPU:
+		nubus_show_cpu_resource(dev, ent);
+		break;
+	default:
+		printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
+		       ent->type, ent->data);
+	}
+	return 0;
+}
 
-	/*
-	 *	Ok see whats cooking in the bytelanes
-	 */
+static struct nubus_dev* __init
+nubus_get_functional_resource(struct nubus_board* board,
+			      int slot,
+			      const struct nubus_dirent* parent)
+{
+	struct nubus_dir    dir;
+	struct nubus_dirent ent;
+	struct nubus_dev*   dev;
+	
+	printk(KERN_INFO "  Function 0x%02x:\n", parent->type);
+	nubus_get_subdir(parent, &dir);
+
+	printk(KERN_DEBUG "nubus_get_functional_resource: parent is 0x%p, dir is 0x%p\n",
+	       parent->base, dir.base);
+
+	/* Actually we should probably panic if this fails */
+	if ((dev = kmalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
+		return NULL;	
+	memset(dev, 0, sizeof(*dev));
+	dev->resid = parent->type;
+	dev->directory = dir.base;
+	dev->board = board;
 	
-	rp=nubus_rom_addr(slot);
-	
-	for(i=4;i;i--)
+	while (nubus_readdir(&dir, &ent) != -1)
 	{
-		rp--;
-		
-		if(!hwreg_present(rp))
-			continue;
-			
-		dp=*rp;
-	
-		if(dp==0)
-			continue;
-		
-		/*
-		 *	Valid ?
-		 */
-		 
-		if((((dp>>4)^dp)&0x0F)!=0x0F)
-			continue;
-			
-		if((dp&0x0F) >= 1<<i)
-			continue;
-			
-		/*
-		 *	Looks promising
-		 */
-		
-		nubus_slots[slot].slot_flags|=NUBUS_DEVICE_PRESENT;
-		lanes=dp;
-
-		if (mode==0)
-			printk("nubus%c: ",
-				"0123456789abcdef"[slot]);
-		
-		
-		/*
-		 *	Time to dig deeper. Find the ROM base
-		 *	and read it
-		 */
-		 
-		rp=nubus_rom_addr(slot); 
-		
-		/*
-		 *	Now to make this more fun the ROM is only visible
-		 *	on its bytelanes - that is smeared across the address
-		 *	space.
-		 */
-		
-		nubus_rewind(&rp,20,lanes);
-		
-		nubus_slots[slot].slot_directory=	nubus_get_rom(&rp,4,lanes);
-		nubus_slots[slot].slot_dlength	=	nubus_get_rom(&rp,4,lanes);
-		nubus_slots[slot].slot_crc	=	nubus_get_rom(&rp,4,lanes);
-		nubus_slots[slot].slot_rev	=	nubus_get_rom(&rp,1,lanes);
-		nubus_slots[slot].slot_format	=	nubus_get_rom(&rp,1,lanes);
-		nubus_slots[slot].slot_lanes	=	lanes;
-		
-		dpat=nubus_get_rom(&rp,4,lanes);
-		
-		/*
-		 *	Ok now check what we got
-		 */
-		
-		if(!(nubus_slots[slot].slot_directory&0x00FF0000))
-			printk("Dodgy doffset ??\n");
-		if(dpat!=0x5A932BC7)
-			printk("Wrong test pattern %lx\n",dpat);
-		
-		/*
-		 *	I wonder how the CRC is meant to work -
-		 *		any takers ?
-		 */
-		
-		
-		/*
-		 *	Now parse the directories on the card
-		 */
-		 
-		
-		dir=nubus_openrootdir(slot);
-		
-		/*
-		 *	Find the board resource
-		 */
-		 
-		while((nd=nubus_readdir(dir))!=NULL)
+		switch(ent.type)
+		{
+		case NUBUS_RESID_TYPE:
 		{
-			/*
-			 *	This ought to be 1. 1 doesn't work, 0x80
-			 *	does. Seems the Apple docs are wrong.
-			 */
-			if(nd->type==0x80/*RES_ID_BOARD_DIR*/)
-				break;
+			unsigned short nbtdata[4];
+			nubus_get_rsrc_mem(nbtdata, &ent, 8);
+			dev->category = nbtdata[0];
+			dev->type     = nbtdata[1];
+			dev->dr_sw    = nbtdata[2];
+			dev->dr_hw    = nbtdata[3];
+			printk(KERN_INFO "    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
+			       nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
+			break;
 		}
-		
-		nubus_closedir(dir);
-		
-		if(nd==NULL)
+		case NUBUS_RESID_NAME:
 		{
-			printk("board resource not found!\n");
-			return;
+			nubus_get_rsrc_str(dev->name, &ent, 64);
+			printk(KERN_INFO "    name: %s\n", dev->name);
+			break;
 		}
-		
-		dir=nubus_opensubdir(nd);
-				
-		/*
-		 *	Walk the board resource
-		 */
-		 
-		while((nd=nubus_readdir(dir))!=NULL)
+		case NUBUS_RESID_DRVRDIR:
 		{
-			switch(nd->type)
-			{
-				case RES_ID_TYPE:
-				{
-					unsigned short nbtdata[4];
-					nubus_memcpy(slot, nbtdata, nubus_dirptr(nd), 8);
-					type_info.category=nbtdata[0];
-					type_info.type=nbtdata[1];
-					type_info.DrHW=nbtdata[2];
-					type_info.DrSW=nbtdata[3];
-					break;
-				}
-				case RES_ID_NAME:
-					nubus_strncpy(slot, nubus_slots[slot].slot_cardname,nubus_dirptr(nd),64);
-					break;
-				default:
-					;
-			}
+			/* MacOS driver.  If we were NetBSD we might
+			   use this :-) */
+			struct nubus_dir drvr_dir;
+			struct nubus_dirent drvr_ent;
+			nubus_get_subdir(&ent, &drvr_dir);
+			nubus_readdir(&drvr_dir, &drvr_ent);
+			dev->driver = nubus_dirptr(&drvr_ent);
+			printk(KERN_INFO "    driver at: 0x%p\n",
+			       dev->driver);
+			break;
 		}
-		
-		nubus_closedir(dir);
-
-		/*
-		 *	Attempt to bind a driver to this slot
-		 */
-		
-		if (mode==0) {
-			printk("%s\n",
-				nubus_slots[slot].slot_cardname);
-			find_nubus_device(slot,&type_info);
+		case NUBUS_RESID_MINOR_BASEOS:
+			/* We will need this in order to support
+			   multiple framebuffers.  It might be handy
+			   for Ethernet as well */
+			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
+			printk(KERN_INFO "    memory offset: 0x%08lx\n",
+			       dev->iobase);
+			break;
+		case NUBUS_RESID_MINOR_LENGTH:
+			/* Ditto */
+			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
+			printk(KERN_INFO "    memory length: 0x%08lx\n",
+			       dev->iosize);
+			break;			
+		case NUBUS_RESID_FLAGS:
+			dev->flags = ent.data;
+			printk(KERN_INFO "    flags: 0x%06x\n", dev->flags);
+			break;
+		case NUBUS_RESID_HWDEVID:
+			dev->hwdevid = ent.data;
+			printk(KERN_INFO "    hwdevid: 0x%06x\n", dev->hwdevid);
+			break;
+		default:
+			/* Local/Private resources have their own
+			   function */
+			nubus_show_private_resource(dev, &ent);
 		}
-		if (mode==1)
-			nubus_video_shutup(slot, &type_info);
+	}
+		
+	return dev;
+}
 
-		return;
+/* This is cool. */
+static int __init nubus_get_vidnames(struct nubus_board* board,
+				     const struct nubus_dirent* parent)
+{
+	struct nubus_dir    dir;
+	struct nubus_dirent ent;
+	/* FIXME: obviously we want to put this in a header file soon */
+	struct vidmode {
+		u32 size;
+		/* Don't know what this is yet */
+		u16 id;
+		/* Longest one I've seen so far is 26 characters */
+		char name[32];
+	};
+
+	printk(KERN_INFO "    video modes supported:\n");
+	nubus_get_subdir(parent, &dir);
+	printk(KERN_DEBUG "nubus_get_vidnames: parent is 0x%p, dir is 0x%p\n",
+	       parent->base, dir.base);
+
+	while(nubus_readdir(&dir, &ent) != -1)
+	{
+		struct vidmode mode;
+		u32 size;
+
+		/* First get the length */
+		nubus_get_rsrc_mem(&size, &ent, 4);
+		
+		/* Now clobber the whole thing */
+		if (size > sizeof(mode) - 1)
+			size = sizeof(mode) - 1;
+		memset(&mode, sizeof(mode), 0);
+		nubus_get_rsrc_mem(&mode, &ent, size);
+		printk (KERN_INFO "      %02X: (%02X) %s\n", ent.type,
+			mode.id, mode.name);
 	}
+	return 0;
 }
 
+/* This is *really* cool. */
+static int __init nubus_get_icon(struct nubus_board* board,
+				 const struct nubus_dirent* ent)
+{
+	/* Should be 32x32 if my memory serves me correctly */
+	unsigned char icon[128];
+	int x, y;
+	
+	nubus_get_rsrc_mem(&icon, ent, 128);
+	printk(KERN_INFO "    icon:\n");
+
+	/* We should actually plot these somewhere in the framebuffer
+	   init.  This is just to demonstrate that they do, in fact,
+	   exist */
+	for (y = 0; y < 32; y++) {
+		printk(KERN_INFO "      ");
+		for (x = 0; x < 32; x++) {
+			if (icon[y*4 + x/8]
+			    & (0x80 >> (x%8)))
+				printk("*");
+			else
+				printk(" ");
+		}
+		printk("\n");
+	}
+	return 0;
+}
 
-void nubus_probe_bus(void)
+static int __init nubus_get_vendorinfo(struct nubus_board* board,
+				       const struct nubus_dirent* parent)
 {
-	int i;
-	for(i=9;i<15;i++)
-	{
-		/* printk("nubus: probing slot %d !\n", i); */
-		nubus_probe_slot(i, 0);
+	struct nubus_dir    dir;
+	struct nubus_dirent ent;
+	static char* vendor_fields[6] = {"ID", "serial", "revision",
+					 "part", "date", "unknown field"};
+
+	printk(KERN_INFO "    vendor info:\n");
+	nubus_get_subdir(parent, &dir);
+	printk(KERN_DEBUG "nubus_get_vendorinfo: parent is 0x%p, dir is 0x%p\n",
+	       parent->base, dir.base);
+
+	while(nubus_readdir(&dir, &ent) != -1)
+	{
+		char name[64];
+		
+		/* These are all strings, we think */
+		nubus_get_rsrc_str(name, &ent, 64);
+		if (ent.type > 5)
+			ent.type = 5;
+		printk(KERN_INFO "    %s: %s\n",
+		       vendor_fields[ent.type-1], name);
 	}
+	return 0;
 }
 
-/*
- *	RBV machines have level triggered video interrupts, and a VIA
- *	emulation that doesn't always seem to include being able to disable 
- *	an interrupt. Totally lusing hardware. Before we can init irq's we
- *	have to install a handler to shut the bloody things up.
- */
-
-void nubus_sweep_video(void)
+static int __init nubus_get_board_resource(struct nubus_board* board, int slot,
+					   const struct nubus_dirent* parent)
 {
-	int i;
-	return; /* XXX why ?? */
-	for(i=9;i<15;i++)
+	struct nubus_dir    dir;
+	struct nubus_dirent ent;
+	
+	nubus_get_subdir(parent, &dir);
+	printk(KERN_DEBUG "nubus_get_board_resource: parent is 0x%p, dir is 0x%p\n",
+	       parent->base, dir.base);
+
+	while(nubus_readdir(&dir, &ent) != -1)
 	{
-		nubus_probe_slot(i,1);
+		switch (ent.type) {
+		case NUBUS_RESID_TYPE:
+		{
+			unsigned short nbtdata[4];
+			/* This type is always the same, and is not
+			   useful except insofar as it tells us that
+			   we really are looking at a board resource. */
+			nubus_get_rsrc_mem(nbtdata, &ent, 8);
+			printk(KERN_INFO "    type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n",
+			       nbtdata[0], nbtdata[1], nbtdata[2],
+			       nbtdata[3]);
+			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
+			    nbtdata[2] != 0 || nbtdata[3] != 0)
+				printk(KERN_ERR "this sResource is not a board resource!\n");
+			break;
+		}
+		case NUBUS_RESID_NAME:
+			nubus_get_rsrc_str(board->name, &ent, 64);
+			printk(KERN_INFO "    name: %s\n", board->name);
+			break;
+		case NUBUS_RESID_ICON:
+			nubus_get_icon(board, &ent);
+			break;
+		case NUBUS_RESID_BOARDID:
+			printk(KERN_INFO "    board id: 0x%x\n", ent.data);
+			break;
+		case NUBUS_RESID_PRIMARYINIT:
+			printk(KERN_INFO "    primary init offset: 0x%06x\n", ent.data);
+			break;
+		case NUBUS_RESID_VENDORINFO:
+			nubus_get_vendorinfo(board, &ent);
+			break;
+		case NUBUS_RESID_FLAGS:
+			printk(KERN_INFO "    flags: 0x%06x\n", ent.data);
+			break;
+		case NUBUS_RESID_HWDEVID:
+			printk(KERN_INFO "    hwdevid: 0x%06x\n", ent.data);
+			break;
+		case NUBUS_RESID_SECONDINIT:
+			printk(KERN_INFO "    secondary init offset: 0x%06x\n", ent.data);
+			break;
+			/* WTF isn't this in the functional resources? */ 
+		case NUBUS_RESID_VIDNAMES:
+			nubus_get_vidnames(board, &ent);
+			break;
+			/* Same goes for this */
+		case NUBUS_RESID_VIDMODES:
+			printk(KERN_INFO "    video mode parameter directory offset: 0x%06x\n",
+			       ent.data);
+			break;			
+		default:
+			printk(KERN_INFO "    unknown resource %02X, data 0x%06x\n",
+			       ent.type, ent.data);
+		}
 	}
+	return 0;
 }
 
-/*
- *	Support functions
- */
- 
-int nubus_ethernet_addr(int slot, unsigned char *addr)
+/* Add a board (might be many devices) to the list */
+static struct nubus_board* __init nubus_add_board(int slot, int bytelanes)
 {
-	struct nubus_dir *nb;
-	struct nubus_dirent *d;
-	int ng=-ENOENT;
-		
-	nb=nubus_openrootdir(slot);
+	struct nubus_board* board;
+	struct nubus_board** boardp;
+
+	unsigned char *rp;
+	unsigned long dpat;
+	struct nubus_dir dir;
+	struct nubus_dirent ent;
+
+	/* Move to the start of the format block */
+	rp = nubus_rom_addr(slot);		
+	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
+
+	/* Actually we should probably panic if this fails */
+	if ((board = kmalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
+		return NULL;	
+	memset(board, 0, sizeof(*board));
+	board->fblock = rp;
+
+	/* Dump the format block for debugging purposes */
+	if (console_loglevel >= 10) {
+		int i;
+		printk(KERN_DEBUG "Slot %X, format block at 0x%p\n",
+		       slot, rp);
+		printk(KERN_DEBUG "Format block: ");
+		for (i = 0; i < FORMAT_BLOCK_SIZE; i += 4) {
+			unsigned short foo, bar;
+			foo = nubus_get_rom(&rp, 2, bytelanes);
+			bar = nubus_get_rom(&rp, 2, bytelanes);
+			printk("%04x %04x  ", foo, bar);
+		}
+		printk("\n");
+		rp = board->fblock;
+	}
 	
-	if(nb==NULL)
-		return -ENOENT;
+	board->slot = slot;
+	board->slot_addr = (unsigned long) nubus_slot_addr(slot);
+	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
+	/* rom_length is *supposed* to be the total length of the
+	 * ROM.  In practice it is the "amount of ROM used to compute
+	 * the CRC."  So some jokers decide to set it to zero and
+	 * set the crc to zero so they don't have to do any math.
+	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
+	 */
+	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
+	board->crc = nubus_get_rom(&rp, 4, bytelanes);
+	board->rev = nubus_get_rom(&rp, 1, bytelanes);
+	board->format = nubus_get_rom(&rp,1, bytelanes);
+	board->lanes = bytelanes;
+
+	/* Directory offset should be small and negative... */
+	if(!(board->doffset & 0x00FF0000))
+		printk(KERN_WARNING "Dodgy doffset!\n");
+	dpat = nubus_get_rom(&rp, 4, bytelanes);
+	if(dpat != NUBUS_TEST_PATTERN)
+		printk(KERN_WARNING "Wrong test pattern %08lx!\n", dpat);
 		
-	while((d=nubus_readdir(nb))!=NULL)
-	{
-		if(d->type==0x80)	/* First private resource */
-			break;
+	/*
+	 *	I wonder how the CRC is meant to work -
+	 *		any takers ?
+	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
+	 * since the initial Macintosh ROM releases skipped the check.
+	 */
+
+	/* Set up the directory pointer */
+	board->directory = board->fblock;
+	nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
+
+	nubus_get_root_dir(board, &dir);	
+
+	/* We're ready to rock */
+	printk(KERN_INFO "Slot %X:\n", slot);
+
+	/* Each slot should have one board resource and any number of
+	   functional resources.  So we'll fill in some fields in the
+	   struct nubus_board from the board resource, then walk down
+	   the list of functional resources, spinning out a nubus_dev
+	   for each of them. */
+	if (nubus_readdir(&dir, &ent) == -1) {
+		/* We can't have this! */
+		printk(KERN_ERR "Board resource not found!\n");
+		return NULL;
+	} else {
+		printk(KERN_INFO "  Board resource:\n");
+		nubus_get_board_resource(board, slot, &ent);
 	}
-	if(d==NULL)
-		return -ENOENT;
-	
-	nb=nubus_opensubdir(d);
+
+	while (nubus_readdir(&dir, &ent) != -1) {
+		struct nubus_dev*  dev;
+		struct nubus_dev** devp;
+		dev = nubus_get_functional_resource(board, slot, &ent);
+		if (dev == NULL)
+			continue;
+
+		/* We zeroed this out above */
+		if (board->first_dev == NULL)
+			board->first_dev = dev;
+		
+		/* Put it on the global NuBus device chain. Keep entries in order. */
+		for (devp=&nubus_devices; *devp!=NULL; devp=&((*devp)->next))
+			/* spin */;
+		*devp = dev;
+		dev->next = NULL;		
+	}
+
+	/* Put it on the global NuBus board chain. Keep entries in order. */
+	for (boardp=&nubus_boards; *boardp!=NULL; boardp=&((*boardp)->next))
+		/* spin */;
+	*boardp = board;
+	board->next = NULL;
 	
-	while((d=nubus_readdir(nb))!=NULL)
+	return board;
+}
+
+void __init nubus_probe_slot(int slot)
+{
+	unsigned char dp;
+	unsigned char* rp;
+	int i;
+
+	rp = nubus_rom_addr(slot);	
+	for(i = 4; i; i--)
 	{
-		if(d->type==0x80)	/* First private field is the mac */
-		{
-			int i;
-			nubus_memcpy(slot, addr, nubus_dirptr(d), 6);
-/*			printk("d.base=%lX, d.value=%lX\n",
-				d->base,d->value);
-			memcpy(addr,"\xC0\xC1\xC2\xC3\xC4\xC5",6);*/
-			printk("MAC address: ");
-			for(i=0;i<6;i++)
-			{
-				printk("%s%02X", i?":":"", addr[i]);
-			}
-			ng=0;
-			break;
-		}
-		else
-			printk("ID=%d val=%x\n",
-				d->type, d->value);
+		unsigned long flags;
+		int card_present;
+
+		rp--;
+		save_flags(flags);
+		cli();
+		card_present = hwreg_present(rp);
+		restore_flags(flags);
+	       
+		if (!card_present)
+			continue;
+
+		dp = *rp;
+		if(dp == 0)
+			continue;
+
+		/* The last byte of the format block consists of two
+		   nybbles which are "mirror images" of each other.
+		   These show us the valid bytelanes */
+		if ((((dp>>4) ^ dp) & 0x0F) != 0x0F)
+			continue;
+		/* Check that this value is actually *on* one of the
+		   bytelanes it claims are valid! */
+		if ((dp & 0x0F) >= (1<<i))
+			continue;
+
+		/* Looks promising.  Let's put it on the list. */
+		nubus_add_board(slot, dp);
+
+		return;
 	}
-	return ng;
 }
 
-#ifdef CONFIG_PROC_FS
+#if defined(CONFIG_PROC_FS)
 
-/*
- *	/proc for Nubus devices
- */
- 
-static int sprint_nubus_config(int slot, char *ptr, int len)
+/* /proc/nubus stuff */
+
+static int sprint_nubus_board(struct nubus_board* board, char* ptr, int len)
 {
-	if(len<150)
+	if(len < 100)
 		return -1;
-	sprintf(ptr, "Device: %s %s\n", nubus_slots[slot].slot_cardname,
-		(nubus_slots[slot].slot_flags&NUBUS_DEVICE_ACTIVE)?
-			"[active]":"[unused]");
+	
+	sprintf(ptr, "Slot %X: %s\n",
+		board->slot, board->name);
+	
 	return strlen(ptr);
 }
 
+/* We're going to have to be a bit more sophisticated about this, I
+   think, because it doesn't really seem to work right when you do a
+   full listing of boards and devices */
 int get_nubus_list(char *buf)
 {
 	int nprinted, len, size;
-	int slot;
+	struct nubus_board* board;
 #define MSG "\nwarning: page-size limit reached!\n"
 
 	/* reserve same for truncation warning message: */
 	size  = PAGE_SIZE - (strlen(MSG) + 1);
-	len   = sprintf(buf, "Nubus devices found:\n");
+	len   = sprintf(buf, "Nubus boards found:\n");
 
-	for (slot=0; slot< 16; slot++) 
+	/* Walk the list of NuBus boards */
+	for (board = nubus_boards; board != NULL; board = board->next)
 	{
-		if(!(nubus_slots[slot].slot_flags&NUBUS_DEVICE_PRESENT))
-			continue;
-		nprinted = sprint_nubus_config(slot, buf + len, size - len);
+		nprinted = sprint_nubus_board(board, buf + len, size - len);
 		if (nprinted < 0) {
 			return len + sprintf(buf + len, MSG);
 		}
@@ -595,46 +894,42 @@
 	return len;
 }
 
-static struct proc_dir_entry proc_nubus = {
+static struct proc_dir_entry proc_old_nubus = {
 	PROC_NUBUS, 5, "nubus",
 	S_IFREG | S_IRUGO, 1, 0, 0,
 	0, &proc_array_inode_operations
 };
-#endif
+#endif /* CONFIG_PROC_FS */
 
-void nubus_init(void)
+void __init nubus_scan_bus(void)
 {
-	/* 
-	 *	Register cards
-	 */
-#ifdef CONFIG_DAYNAPORT
-	extern struct nubus_device_specifier nubus_8390;	
-#endif
+	int slot;
+	for(slot = 9; slot < 15; slot++)
+	{
+		nubus_probe_slot(slot);
+	}
+}
 
+void __init nubus_init(void)
+{
 	if (!MACH_IS_MAC) 
 		return;
 
-#ifdef LCIII_WEIRDNESS
-	if (macintosh_config->ident == MAC_MODEL_LCIII) {
-	        printk("nubus init: LCIII has no nubus!\n");
-		return;
+	/* Initialize the NuBus interrupts */
+	if (oss_present) {
+		oss_nubus_init();
+	} else {
+		via_nubus_init();
 	}
-#endif
 
-#ifdef CONFIG_DAYNAPORT
-	register_nubus_device(&nubus_8390);
-#endif
+	/* And probe */
+	printk("NuBus: Scanning NuBus slots.\n");
+	nubus_devices = NULL;
+	nubus_boards  = NULL;
+	nubus_scan_bus();
 
-	/*
-	 *	And probe
-	 */
-	 
-	nubus_init_via();
-	printk("Scanning nubus slots.\n");
-	nubus_probe_bus();
 #ifdef CONFIG_PROC_FS
-	proc_register(&proc_root, &proc_nubus);
+	proc_register(&proc_root, &proc_old_nubus);
+	nubus_proc_init();
 #endif
 }
-
-	
\ No newline at end of file

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)