patch-2.3.42 linux/drivers/net/macsonic.c

Next file: linux/drivers/net/sun3lance.c
Previous file: linux/drivers/net/macmace.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.3.41/linux/drivers/net/macsonic.c linux/drivers/net/macsonic.c
@@ -42,153 +42,245 @@
 #include <asm/pgtable.h>
 #include <asm/segment.h>
 #include <asm/io.h>
+#include <asm/hwtest.h>
 #include <asm/dma.h>
 #include <asm/macintosh.h>
+#include <asm/macints.h>
+#include <asm/mac_via.h>
 
 #include <linux/errno.h>
 
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <linux/skbuff.h>
-
-#include <config/macsonic.h>
+#include <linux/module.h>
 
 #define SREGS_PAD(n)    u16 n;
 
 #include "sonic.h"
 
-extern int mac_onboard_sonic_probe(void);
+static int sonic_debug = 0;
+static int sonic_version_printed = 0;
 
-static int setup_debug = -1;
-static int setup_offset = -1;
-static int setup_shift = -1;
+extern int macsonic_probe(struct net_device* dev);
+extern int mac_onboard_sonic_probe(struct net_device* dev);
+extern int mac_nubus_sonic_probe(struct net_device* dev);
+
+/* For onboard SONIC */
+#define ONBOARD_SONIC_REGISTERS	0x50F0A000
+#define ONBOARD_SONIC_PROM_BASE	0x50f08000
+
+enum macsonic_type {
+	MACSONIC_DUODOCK,
+	MACSONIC_APPLE,
+	MACSONIC_APPLE16,
+	MACSONIC_DAYNA,
+	MACSONIC_DAYNALINK
+};
+
+/* For the built-in SONIC in the Duo Dock */
+#define DUODOCK_SONIC_REGISTERS 0xe10000
+#define DUODOCK_SONIC_PROM_BASE 0xe12000
+
+/* For Apple-style NuBus SONIC */
+#define APPLE_SONIC_REGISTERS	0
+#define APPLE_SONIC_PROM_BASE	0x40000
+
+/* Daynalink LC SONIC */
+#define DAYNALINK_PROM_BASE 0x400000
+
+/* For Dayna-style NuBus SONIC (haven't seen one yet) */
+#define DAYNA_SONIC_REGISTERS   0x180000
+/* This is what OpenBSD says.  However, this is definitely in NuBus
+   ROM space so we should be able to get it by walking the NuBus
+   resource directories */
+#define DAYNA_SONIC_MAC_ADDR	0xffe004
 
-/*
- * This seems to be the right default for the Q800
- */
+#define SONIC_READ_PROM(addr) readb(prom_addr+addr)
 
-static int reg_offset = 0;
-static int reg_shift = 0;
+int __init macsonic_probe(struct net_device* dev)
+{
+	int rv;
 
-/*
- * Macros to access SONIC registers
- */
- 
-#define MAC_SONIC_REGISTERS	0x50F0A000
-#define MAC_SONIC_PROM_BASE	0x50f08000
-#define MAC_SONIC_IRQ		9	/* Nubus 9 */
+	/* This will catch fatal stuff like -ENOMEM as well as success */
+	if ((rv = mac_onboard_sonic_probe(dev)) != -ENODEV)
+		return rv;
+	return mac_nubus_sonic_probe(dev);
+}
 
 /*
- *    FIXME: We may need to invert the byte ordering. These should
- *      be ok for other aspects as they are uncached spaces.
- *      The original macros from jazzsonic.c works for me
- *      on my LC 630, YMMV /Andreas Ehliar
+ * For reversing the PROM address
  */
 
-#if 0
-#define SONIC_READ(reg) \
-	*((volatile unsigned int *)base_addr+((reg)<<2)+2)
+static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14,
+				    1, 9, 5, 13, 3, 11, 7, 15};
 
-#define SONIC_WRITE(reg,val) \
-	*((volatile unsigned int *)base_addr+((reg)<<2)+2) = val
-#else
-#define SONIC_READ(reg) \
-	*((volatile unsigned int *)base_addr+reg)
+static inline void bit_reverse_addr(unsigned char addr[6])
+{
+	int i;
 
-#define SONIC_WRITE(reg,val) \
-	*((volatile unsigned int *)base_addr+reg) = val
-#endif
+	for(i = 0; i < 6; i++)
+		addr[i] = ((nibbletab[addr[i] & 0xf] << 4) | 
+			   nibbletab[(addr[i] >> 4) &0xf]);
+}
 
-#define SONIC_READ_PROM(addr) \
-	*((volatile unsigned char *)prom_addr+addr)
-/*
- * Function : mac_sonic_setup(char *str, int *ints)
- *
- * Purpose : booter command line initialization of the overrides array,
- *
- * Inputs : str - unused, ints - array of integer parameters with ints[0]
- *	equal to the number of ints.
- *
- * Currently unused in the new driver; need to add settable parameters to the 
- * detect function.
- *
- */
+int __init macsonic_init(struct net_device* dev)
+{
+	struct sonic_local* lp = (struct sonic_local *)dev->priv;
+	int i;
 
-void mac_sonic_setup(char *str, int *ints) {
-	/* Format of macsonic parameter is:
-	 *   macsonic=<debug>,<offset>,<shift>
-	 * Negative values mean don't change.
-	 */
-	
-	/* Grmbl... the standard parameter parsing can't handle negative numbers
-	 * :-( So let's do it ourselves!
-	 */
+	/* Allocate the entire chunk of memory for the descriptors.
+           Note that this cannot cross a 64K boundary. */
+	for (i = 0; i < 20; i++) {
+		unsigned long desc_base, desc_top;
+		if ((lp->sonic_desc = 
+		     kmalloc(SIZEOF_SONIC_DESC
+			     * SONIC_BUS_SCALE(lp->dma_bitmode), GFP_DMA)) == NULL) {
+			printk(KERN_ERR "%s: couldn't allocate descriptor buffers\n", dev->name);
+		}
+		desc_base = (unsigned long) lp->sonic_desc;
+		desc_top = desc_base + SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode);
+		if ((desc_top & 0xffff) >= (desc_base & 0xffff))
+			break;
+		/* Hmm. try again (FIXME: does this actually work?) */
+		kfree(lp->sonic_desc);
+		printk(KERN_DEBUG
+		       "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
+		       dev->name, desc_base, desc_top);
+	}
+
+	if (lp->sonic_desc == NULL) {
+		printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n",
+		       dev->name);
+		return -ENOMEM;
+	}		       
 
-	int i = ints[0]+1, fact;
+	/* Now set up the pointers to point to the appropriate places */
+	lp->cda = lp->sonic_desc;
+	lp->tda = lp->cda + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode));
+	lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
+			     * SONIC_BUS_SCALE(lp->dma_bitmode));
+	lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
+			     * SONIC_BUS_SCALE(lp->dma_bitmode));
+
+	/* FIXME, maybe we should use skbs */
+	if ((lp->rba = (char *)
+	     kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_DMA)) == NULL) {
+		printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name);
+		return -ENOMEM;
+	}
 
-	while( str && (isdigit(*str) || *str == '-') && i <= 10) {
-		if (*str == '-')
-			fact = -1, ++str;
-		else
-			fact = 1;
-		ints[i++] = simple_strtoul( str, NULL, 0 ) * fact;
-		if ((str = strchr( str, ',' )) != NULL)
-			++str;
+	{
+		int rs, ds;
+
+		/* almost always 12*4096, but let's not take chances */
+		rs = ((SONIC_NUM_RRS * SONIC_RBSIZE + 4095) / 4096) * 4096;
+		/* almost always under a page, but let's not take chances */
+		ds = ((SIZEOF_SONIC_DESC + 4095) / 4096) * 4096;
+		kernel_set_cachemode(lp->rba, rs, IOMAP_NOCACHE_SER);
+		kernel_set_cachemode(lp->sonic_desc, ds, IOMAP_NOCACHE_SER);
 	}
-	ints[0] = i-1;
 	
-	if (ints[0] < 1) {
-		printk( "mac_sonic_setup: no arguments!\n" );
-		return;
-	}
-
-	if (ints[0] >= 1) {
-	       	/* 0 <= n <= 2 */
-		if (ints[1] >= 0 && ints[1] <= 8)
-			setup_debug = ints[1];
-		else if (ints[1] > 16)
-			printk( "mac_sonic_setup: invalid debug level %d !\n", ints[1] );
-	}
-	if (ints[0] >= 2) {
-	       	/* 0 <= n <= 2 */
-		if (ints[2] >= 0 && ints[2] <= 16)
-			setup_offset = ints[2];
-		else if (ints[2] > 16)
-			printk( "mac_sonic_setup: invalid offset %d !\n", ints[2] );
-	}
-	if (ints[0] >= 3) {
-	       	/* 0 <= n <= 2 */
-		if (ints[3] >= 0 && ints[3] <= 16)
-			setup_shift = ints[3];
-		else if (ints[3] > 16)
-			printk( "mac_sonic_setup: invalid shift %d !\n", ints[3] );
-	}
+#if 0
+	flush_cache_all();
+#endif
+
+	dev->open = sonic_open;
+	dev->stop = sonic_close;
+	dev->hard_start_xmit = sonic_send_packet;
+	dev->get_stats = sonic_get_stats;
+	dev->set_multicast_list = &sonic_multicast_list;
+
+	/*
+	 * clear tally counter
+	 */
+	sonic_write(dev, SONIC_CRCT, 0xffff);
+	sonic_write(dev, SONIC_FAET, 0xffff);
+	sonic_write(dev, SONIC_MPT, 0xffff);
+
+	/* Fill in the fields of the device structure with ethernet values. */
+	ether_setup(dev);
+	return 0;
 }
 
-static int sonic_debug = 0;
+int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev)
+{
+	const int prom_addr = ONBOARD_SONIC_PROM_BASE;
+	int i;
 
-/*
- * For reversing the PROM address
- */
+	/* On NuBus boards we can sometimes look in the ROM resources.
+	   No such luck for comm-slot/onboard. */
+	for(i = 0; i < 6; i++)
+		dev->dev_addr[i] = SONIC_READ_PROM(i);
+
+	/* Most of the time, the address is bit-reversed.  The NetBSD
+	   source has a rather long and detailed historical account of
+	   why this is so. */
+	if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
+	    memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
+	    memcmp(dev->dev_addr, "\x00\x05\x02", 3))
+		bit_reverse_addr(dev->dev_addr);
+	else
+		return 0;
+
+	/* If we still have what seems to be a bogus address, we'll
+           look in the CAM.  The top entry should be ours. */
+	/* Danger! This only works if MacOS has already initialized
+           the card... */
+	if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
+	    memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
+	    memcmp(dev->dev_addr, "\x00\x05\x02", 3))
+	{
+		unsigned short val;
 
-static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14,
-				    1, 9, 5, 13, 3, 11, 7, 15};
+		printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n");
+		
+		sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
+		sonic_write(dev, SONIC_CEP, 15);
+
+		val = sonic_read(dev, SONIC_CAP2);
+		dev->dev_addr[5] = val >> 8;
+		dev->dev_addr[4] = val & 0xff;
+		val = sonic_read(dev, SONIC_CAP1);
+		dev->dev_addr[3] = val >> 8;
+		dev->dev_addr[2] = val & 0xff;
+		val = sonic_read(dev, SONIC_CAP0);
+		dev->dev_addr[1] = val >> 8;
+		dev->dev_addr[0] = val & 0xff;
+		
+		printk(KERN_INFO "HW Address from CAM 15: ");
+		for (i = 0; i < 6; i++) {
+			printk("%2.2x", dev->dev_addr[i]);
+			if (i < 5)
+				printk(":");
+		}
+		printk("\n");
+	} else return 0;
 
-int __init mac_onboard_sonic_probe(void)
+	if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
+	    memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
+	    memcmp(dev->dev_addr, "\x00\x05\x02", 3))
+	{
+		/*
+		 * Still nonsense ... messed up someplace!
+		 */
+		printk(KERN_ERR "macsonic: ERROR (INVALID MAC)\n");
+		return -EIO;
+	} else return 0;
+}
+
+int __init mac_onboard_sonic_probe(struct net_device* dev)
 {
-	struct net_device *dev;
-	unsigned int silicon_revision;
-	unsigned int val;
-	struct sonic_local *lp;
+	/* Bwahahaha */
+	static int once_is_more_than_enough = 0;
+	struct sonic_local* lp;
 	int i;
-	int base_addr = MAC_SONIC_REGISTERS;
-	int prom_addr = MAC_SONIC_PROM_BASE;
-	static int one=0;
 	
-	if (!MACH_IS_MAC)
+	if (once_is_more_than_enough)
 		return -ENODEV;
+	once_is_more_than_enough = 1;
 
-	if(++one!=1)	/* Only one is allowed */
+	if (!MACH_IS_MAC)
 		return -ENODEV;
 
 	printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
@@ -198,288 +290,332 @@
 		printk("none.\n");
 		return -ENODEV;
 	}
-	
-	printk("yes\n");
-	
-	if (setup_debug >= 0)
-	  sonic_debug = setup_debug;
-
-	/*
-	 * This may depend on the actual Mac model ... works for me.
-	 */
-	reg_offset = 
-	  (setup_offset >= 0) ? setup_offset : 0;
-	reg_shift = 
-	  (setup_shift >= 0) ? setup_shift : 0;
 
-	/*
-	 * get the Silicon Revision ID. If this is one of the known
-	 * one assume that we found a SONIC ethernet controller at
-	 * the expected location.
-	 * (This is not implemented in the Macintosh driver yet; need
-	 * to collect values from various sources. Mine is 0x4 ...)
-	 */
+	/* Bogus probing, on the models which may or may not have
+	   Ethernet (BTW, the Ethernet *is* always at the same
+	   address, and nothing else lives there, at least if Apple's
+	   documentation is to be believed) */
+	if (macintosh_config->ident == MAC_MODEL_Q630 ||
+	    macintosh_config->ident == MAC_MODEL_P588 ||
+	    macintosh_config->ident == MAC_MODEL_C610) {
+		unsigned long flags;
+		int card_present;
+
+		save_flags(flags);
+		cli();
+		card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
+		restore_flags(flags);
+
+		if (!card_present) {
+			printk("none.\n");
+			return -ENODEV;
+		}
+	}
 
-	silicon_revision = SONIC_READ(SONIC_SR);
-	if (sonic_debug > 1)
-		printk("SONIC Silicon Revision = 0x%04x\n", silicon_revision);
+	printk("yes\n");	
+
+	if (dev) {
+		dev = init_etherdev(dev, sizeof(struct sonic_local));
+		/* methinks this will always be true but better safe than sorry */
+		if (dev->priv == NULL)
+			dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
+	} else {
+		dev = init_etherdev(NULL, sizeof(struct sonic_local));
+	}
 
-	/*
-	 * We need to allocate sonic_local later on, making sure it's
-	 * aligned on a 64k boundary. So, no space for dev->priv allocated
-	 * here ...
-	 */
-	dev = init_etherdev(0,0);
-	
-	if(dev==NULL)
+	if (dev == NULL)
 		return -ENOMEM;
 
-	printk("%s: %s found at 0x%08x, ",
-	       dev->name, "SONIC ethernet", base_addr);
+	lp = (struct sonic_local*) dev->priv;
+	memset(lp, 0, sizeof(struct sonic_local));
+	/* Danger!  My arms are flailing wildly!  You *must* set this
+           before using sonic_read() */
+
+	dev->base_addr = ONBOARD_SONIC_REGISTERS;
+	if (via_alt_mapping)
+		dev->irq = IRQ_AUTO_3;
+	else
+		dev->irq = IRQ_NUBUS_9;
+
+	if (!sonic_version_printed) {
+		printk(KERN_INFO "%s", version);
+		sonic_version_printed = 1;
+	}
+	printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
+	       dev->name, dev->base_addr);
+
+	/* Now do a song and dance routine in an attempt to determine
+           the bus width */
+
+	/* The PowerBook's SONIC is 16 bit always. */
+	if (macintosh_config->ident == MAC_MODEL_PB520) {
+		lp->reg_offset = 0;
+		lp->dma_bitmode = 0;
+	} else {
+		/* Some of the comm-slot cards are 16 bit.  But some
+                   of them are not.  The 32-bit cards use offset 2 and
+                   pad with zeroes or sometimes ones (I think...)
+                   Therefore, if we try offset 0 and get a silicon
+                   revision of 0, we assume 16 bit. */
+		int sr;
+
+		/* Technically this is not necessary since we zeroed
+                   it above */
+		lp->reg_offset = 0;
+		lp->dma_bitmode = 0;
+		sr = sonic_read(dev, SONIC_SR);
+		if (sr == 0 || sr == 0xffff) {
+			lp->reg_offset = 2;
+			/* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
+			sr = sonic_read(dev, SONIC_SR);
+			lp->dma_bitmode = 1;
+			
+		}
+		printk(KERN_INFO
+		       "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
+		       dev->name, sr, lp->dma_bitmode?32:16, lp->reg_offset);
+	}
+
+
+	/* Software reset, then initialize control registers. */
+	sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
+	sonic_write(dev, SONIC_DCR, SONIC_DCR_BMS |
+		    SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_EXBUS |
+		    (lp->dma_bitmode ? SONIC_DCR_DW : 0));
+	/* This *must* be written back to in order to restore the
+           extended programmable output bits */
+	sonic_write(dev, SONIC_DCR2, 0);
+
+	/* Clear *and* disable interrupts to be on the safe side */
+	sonic_write(dev, SONIC_ISR,0x7fff);
+	sonic_write(dev, SONIC_IMR,0);
 
-	if (sonic_debug > 1)
-		printk("using offset %d shift %d,", reg_offset, reg_shift);
-
-	/* Fill in the 'dev' fields. */
-	dev->base_addr = base_addr;
-	dev->irq = MAC_SONIC_IRQ;
+	/* Now look for the MAC address. */
+	if (mac_onboard_sonic_ethernet_addr(dev) != 0)
+		return -ENODEV;
 
-	/*
-	 * Put the sonic into software reset, then
-	 * retrieve and print the ethernet address.
-	 */
+	printk(KERN_INFO "MAC ");
+	for (i = 0; i < 6; i++) {
+		printk("%2.2x", dev->dev_addr[i]);
+		if (i < 5)
+			printk(":");
+	}
 
-	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
+	printk(" IRQ %d\n", dev->irq);
 
-	/*
-	 *        We can't trust MacOS to initialise things it seems.
-	 */
+	/* Shared init code */
+	return macsonic_init(dev);
+}
 
-	if (sonic_debug > 1)
-		printk("SONIC_DCR was %X\n",SONIC_READ(SONIC_DCR));
-	
-	SONIC_WRITE(SONIC_DCR,
-		    SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_EXBUS | SONIC_DCR_DW);
+int __init mac_nubus_sonic_ethernet_addr(struct net_device* dev,
+					 unsigned long prom_addr,
+					 int id)
+{
+	int i;
+	for(i = 0; i < 6; i++)
+		dev->dev_addr[i] = SONIC_READ_PROM(i);
+	/* For now we are going to assume that they're all bit-reversed */
+	bit_reverse_addr(dev->dev_addr);
 
-	/*
-	 *  We don't want floating spare IRQ's around, not on
-	 *  level triggered systems!
-	 *  Strange though - writing to the ISR only clears currently
-	 *  pending IRQs, but doesn't disable them... Does this make 
-	 *  a difference?? Seems it does ...
-	 */
-#if 1
-	SONIC_WRITE(SONIC_ISR,0x7fff);
-	SONIC_WRITE(SONIC_IMR,0);
-#else
-	SONIC_WRITE(SONIC_ISR, SONIC_IMR_DEFAULT);
-#endif
-	
-	/* This is how it is done in jazzsonic.c
-	 * It doesn't seem to work here though.
-	 */
-	if (sonic_debug > 2) {
-		printk("Retreiving CAM entry 0. This should be the HW address.\n");
-		
-		SONIC_WRITE(SONIC_CEP, 0);
-		for (i = 0; i < 3; i++)
-		{
-			val = SONIC_READ(SONIC_CAP0 - i);
-			dev->dev_addr[i * 2] = val;
-			dev->dev_addr[i * 2 + 1] = val >> 8;
-		}
+	return 0;
+}
 
-		printk("HW Address from CAM 0: ");
-		for (i = 0; i < 6; i++)
-		{
-			printk("%2.2x", dev->dev_addr[i]);
-			if (i < 5)
-				printk(":");
-		}
-		printk("\n");
+int __init macsonic_ident(struct nubus_dev* ndev)
+{
+	if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC && 
+	    ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
+		return MACSONIC_DAYNALINK;
+	if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
+	    ndev->dr_sw == NUBUS_DRSW_APPLE) {
+		/* There has to be a better way to do this... */
+		if (strstr(ndev->board->name, "DuoDock"))
+			return MACSONIC_DUODOCK;
+		else
+			return MACSONIC_APPLE;
+	}
+	return -1;
+}
 
-		printk("Retreiving CAM entry 15. Another candidate...\n");
+int __init mac_nubus_sonic_probe(struct net_device* dev)
+{
+	static int slots = 0;
+	struct nubus_dev* ndev = NULL;
+	struct sonic_local* lp;
+	unsigned long base_addr, prom_addr;
+	u16 sonic_dcr;
+	int id;
+	int i;
+	int reg_offset, dma_bitmode;
 
-		/*
-		 * MacOS seems to use CAM entry 15 ...
-		 */
-	       	SONIC_WRITE(SONIC_CEP, 15);
-		for (i = 0; i < 3; i++)
-		{
-			val = SONIC_READ(SONIC_CAP0 - i);
-			dev->dev_addr[i * 2] = val;
-			dev->dev_addr[i * 2 + 1] = val >> 8;
-		}
+	/* Find the first SONIC that hasn't been initialized already */
+	while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
+				       NUBUS_TYPE_ETHERNET, ndev)) != NULL)
+	{
+		/* Have we seen it already? */
+		if (slots & (1<<ndev->board->slot))
+			continue;
+		slots |= 1<<ndev->board->slot;
 
-		printk("HW Address from CAM 15: ");
-		for (i = 0; i < 6; i++)
-		{
-			printk("%2.2x", dev->dev_addr[i]);
-			if (i < 5)
-				printk(":");
-		}
-		printk("\n");
+		/* Is it one of ours? */
+		if ((id = macsonic_ident(ndev)) != -1)
+			break;
 	}
 
-	/*
-	 * if we can read the PROM, we're safe :-)
-	 */
-	if (sonic_debug > 1)
-		printk("Retreiving HW address from the PROM: ");
+	if (ndev == NULL)
+		return -ENODEV;
 
-	for(i=0;i<6;i++){
-                dev->dev_addr[i]=SONIC_READ_PROM(i);
-        }                             
-	if (sonic_debug > 1) {
-	        for (i = 0; i < 6; i++)
-		{
-			printk("%2.2x", dev->dev_addr[i]);
-			if (i <	5)
-				printk(":");
-		}
-		printk("\n");
+	switch (id) {
+	case MACSONIC_DUODOCK:
+		base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
+		prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
+		sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1
+			| SONIC_DCR_TFT0;
+		reg_offset = 2;
+		dma_bitmode = 1;
+		break;
+	case MACSONIC_APPLE:
+		base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
+		prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
+		sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
+		reg_offset = 0;
+		dma_bitmode = 1;
+		break;
+	case MACSONIC_APPLE16:
+		base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
+		prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
+		sonic_dcr = SONIC_DCR_EXBUS
+ 			| SONIC_DCR_RFT1 | SONIC_DCR_TFT0
+			| SONIC_DCR_PO1 | SONIC_DCR_BMS; 
+		reg_offset = 0;
+		dma_bitmode = 0;
+		break;
+	case MACSONIC_DAYNALINK:
+		base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
+		prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
+		sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0
+			| SONIC_DCR_PO1 | SONIC_DCR_BMS; 
+		reg_offset = 0;
+		dma_bitmode = 0;
+		break;
+	case MACSONIC_DAYNA:
+		base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
+		prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
+		sonic_dcr = SONIC_DCR_BMS
+			| SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
+		reg_offset = 0;
+		dma_bitmode = 0;
+		break;
+	default:
+		printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
+		return -ENODEV;
 	}
-	/*
-	 *                If its not one of these we have
-	 *          screwed up on this Mac model
-	 */
 
-	if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
-	    memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
-	    memcmp(dev->dev_addr, "\x00\x05\x02", 3))
-	{
-		/*
-		 * Try bit reversed
-		 */
-		for(i=0;i<6;i++){
-			val = SONIC_READ_PROM(i);
-			dev->dev_addr[i]=(nibbletab[val & 0xf] << 4) | 
-					  nibbletab[(val >> 4) &0xf];
-		}
-		if (sonic_debug > 1) {
-			printk("Trying bit reversed:  ");
-			for (i = 0; i < 6; i++)
-			{
-				printk("%2.2x", dev->dev_addr[i]);
-				if (i < 5)
-					printk(":");
-			}
-			printk("\n");
-		}
-		if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
-		    memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
-		    memcmp(dev->dev_addr, "\x00\x05\x02", 3))
-		{
-		        /*
-			 * Still nonsense ... messed up someplace!
-			 */
-			printk("ERROR (INVALID MAC)\n");
-			return -EIO;
-		}
+	if (dev) {
+		dev = init_etherdev(dev, sizeof(struct sonic_local));
+		/* methinks this will always be true but better safe than sorry */
+		if (dev->priv == NULL)
+			dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
+	} else {
+		dev = init_etherdev(NULL, sizeof(struct sonic_local));
 	}
 
-	printk(" MAC ");
-	for (i = 0; i < 6; i++)
-	{
+	if (dev == NULL)
+		return -ENOMEM;
+
+	lp = (struct sonic_local*) dev->priv;
+	memset(lp, 0, sizeof(struct sonic_local));
+	/* Danger!  My arms are flailing wildly!  You *must* set this
+           before using sonic_read() */
+	lp->reg_offset = reg_offset;
+	lp->dma_bitmode = dma_bitmode;
+	dev->base_addr = base_addr;
+	dev->irq = SLOT2IRQ(ndev->board->slot);
+
+	if (!sonic_version_printed) {
+		printk(KERN_INFO "%s", version);
+		sonic_version_printed = 1;
+	}
+	printk(KERN_INFO "%s: %s in slot %X\n",
+	       dev->name, ndev->board->name, ndev->board->slot);
+	printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
+	       dev->name, sonic_read(dev, SONIC_SR), dma_bitmode?32:16, reg_offset);
+
+	/* Software reset, then initialize control registers. */
+	sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
+	sonic_write(dev, SONIC_DCR, sonic_dcr
+		    | (dma_bitmode ? SONIC_DCR_DW : 0));
+
+	/* Clear *and* disable interrupts to be on the safe side */
+	sonic_write(dev, SONIC_ISR,0x7fff);
+	sonic_write(dev, SONIC_IMR,0);
+
+	/* Now look for the MAC address. */
+	if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
+		return -ENODEV;
+
+	printk(KERN_INFO "MAC ");
+	for (i = 0; i < 6; i++) {
 		printk("%2.2x", dev->dev_addr[i]);
 		if (i < 5)
 			printk(":");
 	}
+	printk(" IRQ %d\n", dev->irq);
 
-	printk(" IRQ %d\n", MAC_SONIC_IRQ);
+	/* Shared init code */
+	return macsonic_init(dev);
+}
 
-	/* Initialize the device structure. */
-	if (dev->priv == NULL)
-	{
-		if (sonic_debug > 2) {
-			printk("Allocating memory for dev->priv aka lp\n");
-			printk("Memory to allocate: %d\n",sizeof(*lp));
-		}
-		/*
-		 * the memory be located in the same 64kb segment
-		 */
-		lp = NULL;
-		i = 0;
-		do
-		{
-			lp = (struct sonic_local *) kmalloc(sizeof(*lp), GFP_KERNEL);
-			if ((unsigned long) lp >> 16 != ((unsigned long) lp + sizeof(*lp)) >> 16)
-			{
-				/* FIXME, free the memory later */
-				kfree(lp);
-				lp = NULL;
-			}
-		}
-		while (lp == NULL && i++ < 20);
+#ifdef MODULE
+static char namespace[16] = "";
+static struct net_device dev_macsonic = {
+        NULL,
+        0, 0, 0, 0,
+        0, 0,
+        0, 0, 0, NULL, NULL };
 
-		if (lp == NULL)
-		{
-			printk("%s: couldn't allocate memory for descriptors\n",
-			       dev->name);
-			return -ENOMEM;
-		}
+MODULE_PARM(sonic_debug, "i");
 
-		if (sonic_debug > 2) {
-			printk("Memory allocated after %d tries\n",i);
-		}
+EXPORT_NO_SYMBOLS;
 
-		/* XXX sonic_local has the TDA, RRA, RDA, don't cache */
-		kernel_set_cachemode((u32)lp, 8192, IOMAP_NOCACHE_SER);
-		memset(lp, 0, sizeof(struct sonic_local));
-
-		lp->cda_laddr = (u32)lp;
-		if (sonic_debug > 2) {
-			printk("memory allocated for sonic at 0x%x\n",lp);
-		}
-		lp->tda_laddr = lp->cda_laddr + sizeof(lp->cda);
-		lp->rra_laddr = lp->tda_laddr + sizeof(lp->tda);
-		lp->rda_laddr = lp->rra_laddr + sizeof(lp->rra);
-
-		/* allocate receive buffer area */
-		/* FIXME, maybe we should use skbs */
-		if ((lp->rba = (char *) kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL)) == NULL)
-		{
-			printk("%s: couldn't allocate receive buffers\n", dev->name);
-			return -ENOMEM;
-		}
-		/* XXX RBA written by Sonic, not cached either */
-		kernel_set_cachemode((u32)lp->rba, 6*8192, IOMAP_NOCACHE_SER);
-		lp->rba_laddr = (u32)lp->rba;
-		flush_cache_all();
-		dev->priv = (struct sonic_local *) lp;
-	}
-	lp = (struct sonic_local *) dev->priv;
-	dev->open = sonic_open;
-	dev->stop = sonic_close;
-	dev->hard_start_xmit = sonic_send_packet;
-	dev->get_stats = sonic_get_stats;
-	dev->set_multicast_list = &sonic_multicast_list;
+int
+init_module(void)
+{
+        dev_macsonic.name = namespace;
+        dev_macsonic.init = macsonic_probe;
 
-	/* Fill in the fields of the device structure with ethernet values. */
-	ether_setup(dev);
+        if (register_netdev(&dev_macsonic) != 0) {
+                printk(KERN_WARNING "macsonic.c: No card found\n");
+                return -ENXIO;
+        }
 	return 0;
 }
 
-/*
- *    SONIC uses a nubus IRQ
- */
+void
+cleanup_module(void)
+{
+        if (dev_macsonic.priv != NULL) {
+                unregister_netdev(&dev_macsonic);
+                kfree(dev_macsonic.priv);
+                dev_macsonic.priv = NULL;
+        }
+}
+#endif /* MODULE */
 
-#define sonic_request_irq(irq, vec, flags, name, dev) \
-		nubus_request_irq(irq, dev, vec)
-#define sonic_free_irq(irq,id)	nubus_free_irq(irq)
 
-/*
- *    No funnies on memory mapping.
- */
+#define vdma_alloc(foo, bar) ((u32)foo)
+#define vdma_free(baz)
+#define sonic_chiptomem(bat) (bat)
+#define PHYSADDR(quux) (quux)
 
-#define sonic_chiptomem(x)	(x)
+#include "sonic.c"
 
 /*
- *    No VDMA on a Macintosh. So we need request no such facility.
+ * Local variables:
+ *  compile-command: "m68k-linux-gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -fno-strength-reduce -ffixed-a2 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h   -c -o macsonic.o macsonic.c"
+ *  version-control: t
+ *  kept-new-versions: 5
+ *  c-indent-level: 8
+ *  tab-width: 8
+ * End:
+ *
  */
-
-#define vdma_alloc(x,y)		((u32)(x))
-#define vdma_free(x)
-#define PHYSADDR(x)		(x)
-
-#include "sonic.c"

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