macsonic.c 17.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4
/*
 * macsonic.c
 *
5 6 7 8 9 10
 * (C) 2005 Finn Thain
 *
 * Converted to DMA API, converted to unified driver model, made it work as
 * a module again, and from the mac68k project, introduced more 32-bit cards
 * and dhd's support for 16-bit cards.
 *
L
Linus Torvalds 已提交
11 12 13 14 15 16
 * (C) 1998 Alan Cox
 *
 * Debugging Andreas Ehliar, Michael Schmitz
 *
 * Based on code
 * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
17
 *
L
Linus Torvalds 已提交
18 19
 * This driver is based on work from Andreas Busse, but most of
 * the code is rewritten.
20
 *
L
Linus Torvalds 已提交
21 22 23 24
 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
 *
 * A driver for the Mac onboard Sonic ethernet chip.
 *
25
 * 98/12/21 MSch: judged from tests on Q800, it's basically working,
L
Linus Torvalds 已提交
26 27 28 29
 *		  but eating up both receive and transmit resources
 *		  and duplicating packets. Needs more testing.
 *
 * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
30
 *
L
Linus Torvalds 已提交
31 32 33 34 35
 * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems
 *          on centris.
 */

#include <linux/kernel.h>
36
#include <linux/module.h>
L
Linus Torvalds 已提交
37 38
#include <linux/types.h>
#include <linux/fcntl.h>
39
#include <linux/gfp.h>
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/nubus.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
50
#include <linux/platform_device.h>
51
#include <linux/dma-mapping.h>
A
Al Viro 已提交
52
#include <linux/bitrev.h>
53
#include <linux/slab.h>
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62

#include <asm/pgtable.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>

63
static char mac_sonic_string[] = "macsonic";
L
Linus Torvalds 已提交
64 65 66

#include "sonic.h"

67 68 69 70 71 72 73 74 75 76 77
/* These should basically be bus-size and endian independent (since
   the SONIC is at least smart enough that it uses the same endianness
   as the host, unlike certain less enlightened Macintosh NICs) */
#define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \
	      + lp->reg_offset))
#define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
	      + lp->reg_offset))

/* use 0 for production, 1 for verification, >1 for debug */
#ifdef SONIC_DEBUG
static unsigned int sonic_debug = SONIC_DEBUG;
78
#else
79 80
static unsigned int sonic_debug = 1;
#endif
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

static int sonic_version_printed;

/* 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

#define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)

/*
 * For reversing the PROM address
 */

static inline void bit_reverse_addr(unsigned char addr[6])
{
	int i;

	for(i = 0; i < 6; i++)
A
Akinobu Mita 已提交
125
		addr[i] = bitrev8(addr[i]);
L
Linus Torvalds 已提交
126 127
}

F
Finn Thain 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
{
	irqreturn_t result;
	unsigned long flags;

	local_irq_save(flags);
	result = sonic_interrupt(irq, dev_id);
	local_irq_restore(flags);
	return result;
}

static int macsonic_open(struct net_device* dev)
{
141 142
	int retval;

143
	retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
144 145 146 147
	if (retval) {
		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
				dev->name, dev->irq);
		goto err;
F
Finn Thain 已提交
148 149 150 151 152
	}
	/* Under the A/UX interrupt scheme, the onboard SONIC interrupt comes
	 * in at priority level 3. However, we sometimes get the level 2 inter-
	 * rupt as well, which must prevent re-entrance of the sonic handler.
	 */
153
	if (dev->irq == IRQ_AUTO_3) {
154 155
		retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
				     "sonic", dev);
156 157 158 159
		if (retval) {
			printk(KERN_ERR "%s: unable to get IRQ %d.\n",
					dev->name, IRQ_NUBUS_9);
			goto err_irq;
F
Finn Thain 已提交
160
		}
161 162 163 164 165 166 167 168 169 170 171 172 173
	}
	retval = sonic_open(dev);
	if (retval)
		goto err_irq_nubus;
	return 0;

err_irq_nubus:
	if (dev->irq == IRQ_AUTO_3)
		free_irq(IRQ_NUBUS_9, dev);
err_irq:
	free_irq(dev->irq, dev);
err:
	return retval;
F
Finn Thain 已提交
174 175 176 177 178 179 180 181 182 183 184 185
}

static int macsonic_close(struct net_device* dev)
{
	int err;
	err = sonic_close(dev);
	free_irq(dev->irq, dev);
	if (dev->irq == IRQ_AUTO_3)
		free_irq(IRQ_NUBUS_9, dev);
	return err;
}

186 187 188 189
static const struct net_device_ops macsonic_netdev_ops = {
	.ndo_open		= macsonic_open,
	.ndo_stop		= macsonic_close,
	.ndo_start_xmit		= sonic_send_packet,
190
	.ndo_set_rx_mode	= sonic_multicast_list,
191 192 193 194 195 196
	.ndo_tx_timeout		= sonic_tx_timeout,
	.ndo_get_stats		= sonic_get_stats,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address	= eth_mac_addr,
};

B
Bill Pemberton 已提交
197
static int macsonic_init(struct net_device *dev)
L
Linus Torvalds 已提交
198
{
199
	struct sonic_local* lp = netdev_priv(dev);
L
Linus Torvalds 已提交
200 201 202

	/* Allocate the entire chunk of memory for the descriptors.
           Note that this cannot cross a 64K boundary. */
203 204 205 206 207 208
	lp->descriptors = dma_alloc_coherent(lp->device,
					     SIZEOF_SONIC_DESC *
					     SONIC_BUS_SCALE(lp->dma_bitmode),
					     &lp->descriptors_laddr,
					     GFP_KERNEL);
	if (lp->descriptors == NULL)
L
Linus Torvalds 已提交
209 210 211
		return -ENOMEM;

	/* Now set up the pointers to point to the appropriate places */
212 213 214
	lp->cda = lp->descriptors;
	lp->tda = lp->cda + (SIZEOF_SONIC_CDA
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
L
Linus Torvalds 已提交
215
	lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
216
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
L
Linus Torvalds 已提交
217
	lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
218
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
L
Linus Torvalds 已提交
219

220 221 222 223 224 225 226
	lp->cda_laddr = lp->descriptors_laddr;
	lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
	lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
	lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
L
Linus Torvalds 已提交
227

228
	dev->netdev_ops = &macsonic_netdev_ops;
229
	dev->watchdog_timeo = TX_TIMEOUT;
L
Linus Torvalds 已提交
230 231 232 233

	/*
	 * clear tally counter
	 */
234 235 236
	SONIC_WRITE(SONIC_CRCT, 0xffff);
	SONIC_WRITE(SONIC_FAET, 0xffff);
	SONIC_WRITE(SONIC_MPT, 0xffff);
L
Linus Torvalds 已提交
237 238 239 240

	return 0;
}

F
Finn Thain 已提交
241 242 243 244 245
#define INVALID_MAC(mac) (memcmp(mac, "\x08\x00\x07", 3) && \
                          memcmp(mac, "\x00\xA0\x40", 3) && \
                          memcmp(mac, "\x00\x80\x19", 3) && \
                          memcmp(mac, "\x00\x05\x02", 3))

B
Bill Pemberton 已提交
246
static void mac_onboard_sonic_ethernet_addr(struct net_device *dev)
L
Linus Torvalds 已提交
247
{
248
	struct sonic_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
249
	const int prom_addr = ONBOARD_SONIC_PROM_BASE;
F
Finn Thain 已提交
250
	unsigned short val;
L
Linus Torvalds 已提交
251

F
Finn Thain 已提交
252 253 254 255 256 257 258 259 260 261 262 263
	/*
	 * On NuBus boards we can sometimes look in the ROM resources.
	 * No such luck for comm-slot/onboard.
	 * On the PowerBook 520, the PROM base address is a mystery.
	 */
	if (hwreg_present((void *)prom_addr)) {
		int i;

		for (i = 0; i < 6; i++)
			dev->dev_addr[i] = SONIC_READ_PROM(i);
		if (!INVALID_MAC(dev->dev_addr))
			return;
L
Linus Torvalds 已提交
264

F
Finn Thain 已提交
265 266 267 268 269
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
270
		bit_reverse_addr(dev->dev_addr);
F
Finn Thain 已提交
271 272 273
		if (!INVALID_MAC(dev->dev_addr))
			return;

L
Linus Torvalds 已提交
274
		/*
F
Finn Thain 已提交
275 276
		 * If we still have what seems to be a bogus address, we'll
		 * look in the CAM. The top entry should be ours.
L
Linus Torvalds 已提交
277
		 */
F
Finn Thain 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		printk(KERN_WARNING "macsonic: MAC address in PROM seems "
		                    "to be invalid, trying CAM\n");
	} else {
		printk(KERN_WARNING "macsonic: cannot read MAC address from "
		                    "PROM, trying CAM\n");
	}

	/* This only works if MacOS has already initialized the card. */

	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
	SONIC_WRITE(SONIC_CEP, 15);

	val = SONIC_READ(SONIC_CAP2);
	dev->dev_addr[5] = val >> 8;
	dev->dev_addr[4] = val & 0xff;
	val = SONIC_READ(SONIC_CAP1);
	dev->dev_addr[3] = val >> 8;
	dev->dev_addr[2] = val & 0xff;
	val = SONIC_READ(SONIC_CAP0);
	dev->dev_addr[1] = val >> 8;
	dev->dev_addr[0] = val & 0xff;

	if (!INVALID_MAC(dev->dev_addr))
		return;

	/* Still nonsense ... messed up someplace! */

	printk(KERN_WARNING "macsonic: MAC address in CAM entry 15 "
	                    "seems invalid, will use a random MAC\n");
307
	eth_hw_addr_random(dev);
L
Linus Torvalds 已提交
308 309
}

B
Bill Pemberton 已提交
310
static int mac_onboard_sonic_probe(struct net_device *dev)
L
Linus Torvalds 已提交
311
{
312 313
	struct sonic_local* lp = netdev_priv(dev);
	int sr;
314
	bool commslot = macintosh_config->expansion_type == MAC_EXP_PDS_COMM;
315

L
Linus Torvalds 已提交
316 317 318
	if (!MACH_IS_MAC)
		return -ENODEV;

319
	printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
320

L
Linus Torvalds 已提交
321 322 323 324
	/* 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) */
325
	if (commslot || macintosh_config->ident == MAC_MODEL_C610) {
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334
		int card_present;

		card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
		if (!card_present) {
			printk("none.\n");
			return -ENODEV;
		}
	}

335
	printk("yes\n");
L
Linus Torvalds 已提交
336

337 338
	/* Danger!  My arms are flailing wildly!  You *must* set lp->reg_offset
	 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349
	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",
350
	       dev_name(lp->device), dev->base_addr);
L
Linus Torvalds 已提交
351 352 353

	/* The PowerBook's SONIC is 16 bit always. */
	if (macintosh_config->ident == MAC_MODEL_PB520) {
354 355 356 357
		lp->reg_offset = 0;
		lp->dma_bitmode = SONIC_BITMODE16;
		sr = SONIC_READ(SONIC_SR);
	} else if (commslot) {
L
Linus Torvalds 已提交
358
		/* Some of the comm-slot cards are 16 bit.  But some
359 360 361 362 363 364 365 366
		   of them are not.  The 32-bit cards use offset 2 and
		   have known revisions, we try reading the revision
		   register at offset 2, if we don't get a known revision
		   we assume 16 bit at offset 0.  */
		lp->reg_offset = 2;
		lp->dma_bitmode = SONIC_BITMODE16;

		sr = SONIC_READ(SONIC_SR);
367
		if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
368 369 370 371 372 373
			/* 83932 is 0x0004 or 0x0006, 83934 is 0x0100 or 0x0101 */
			lp->dma_bitmode = SONIC_BITMODE32;
		else {
			lp->dma_bitmode = SONIC_BITMODE16;
			lp->reg_offset = 0;
			sr = SONIC_READ(SONIC_SR);
L
Linus Torvalds 已提交
374
		}
375 376 377 378 379
	} else {
		/* All onboard cards are at offset 2 with 32 bit DMA. */
		lp->reg_offset = 2;
		lp->dma_bitmode = SONIC_BITMODE32;
		sr = SONIC_READ(SONIC_SR);
L
Linus Torvalds 已提交
380
	}
381 382
	printk(KERN_INFO
	       "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
383
	       dev_name(lp->device), sr, lp->dma_bitmode?32:16, lp->reg_offset);
L
Linus Torvalds 已提交
384

385
#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
386
	printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
387 388
	       SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
#endif
L
Linus Torvalds 已提交
389 390

	/* Software reset, then initialize control registers. */
391 392 393 394 395
	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);

	SONIC_WRITE(SONIC_DCR, SONIC_DCR_EXBUS | SONIC_DCR_BMS |
	                       SONIC_DCR_RFT1  | SONIC_DCR_TFT0 |
	                       (lp->dma_bitmode ? SONIC_DCR_DW : 0));
L
Linus Torvalds 已提交
396 397

	/* This *must* be written back to in order to restore the
398 399 400
	 * extended programmable output bits, as it may not have been
	 * initialised since the hardware reset. */
	SONIC_WRITE(SONIC_DCR2, 0);
L
Linus Torvalds 已提交
401 402

	/* Clear *and* disable interrupts to be on the safe side */
403 404
	SONIC_WRITE(SONIC_IMR, 0);
	SONIC_WRITE(SONIC_ISR, 0x7fff);
L
Linus Torvalds 已提交
405 406

	/* Now look for the MAC address. */
F
Finn Thain 已提交
407
	mac_onboard_sonic_ethernet_addr(dev);
L
Linus Torvalds 已提交
408 409 410 411 412

	/* Shared init code */
	return macsonic_init(dev);
}

B
Bill Pemberton 已提交
413
static int mac_nubus_sonic_ethernet_addr(struct net_device *dev,
414
					 unsigned long prom_addr, int id)
L
Linus Torvalds 已提交
415 416 417 418
{
	int i;
	for(i = 0; i < 6; i++)
		dev->dev_addr[i] = SONIC_READ_PROM(i);
419 420 421 422

	/* Some of the addresses are bit-reversed */
	if (id != MACSONIC_DAYNA)
		bit_reverse_addr(dev->dev_addr);
L
Linus Torvalds 已提交
423 424 425 426

	return 0;
}

F
Finn Thain 已提交
427
static int macsonic_ident(struct nubus_rsrc *fres)
L
Linus Torvalds 已提交
428
{
F
Finn Thain 已提交
429 430
	if (fres->dr_hw == NUBUS_DRHW_ASANTE_LC &&
	    fres->dr_sw == NUBUS_DRSW_SONIC_LC)
L
Linus Torvalds 已提交
431
		return MACSONIC_DAYNALINK;
F
Finn Thain 已提交
432 433
	if (fres->dr_hw == NUBUS_DRHW_SONIC &&
	    fres->dr_sw == NUBUS_DRSW_APPLE) {
L
Linus Torvalds 已提交
434
		/* There has to be a better way to do this... */
F
Finn Thain 已提交
435
		if (strstr(fres->board->name, "DuoDock"))
L
Linus Torvalds 已提交
436 437 438 439
			return MACSONIC_DUODOCK;
		else
			return MACSONIC_APPLE;
	}
440

F
Finn Thain 已提交
441 442
	if (fres->dr_hw == NUBUS_DRHW_SMC9194 &&
	    fres->dr_sw == NUBUS_DRSW_DAYNA)
443
		return MACSONIC_DAYNA;
444

F
Finn Thain 已提交
445 446
	if (fres->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
	    fres->dr_sw == 0) { /* huh? */
447 448
		return MACSONIC_APPLE16;
	}
L
Linus Torvalds 已提交
449 450 451
	return -1;
}

B
Bill Pemberton 已提交
452
static int mac_nubus_sonic_probe(struct net_device *dev)
L
Linus Torvalds 已提交
453 454
{
	static int slots;
F
Finn Thain 已提交
455
	struct nubus_rsrc *ndev = NULL;
456
	struct sonic_local* lp = netdev_priv(dev);
L
Linus Torvalds 已提交
457 458
	unsigned long base_addr, prom_addr;
	u16 sonic_dcr;
459 460
	int id = -1;
	int reg_offset, dma_bitmode;
461

L
Linus Torvalds 已提交
462
	/* Find the first SONIC that hasn't been initialized already */
463 464 465 466 467
	for_each_func_rsrc(ndev) {
		if (ndev->category != NUBUS_CAT_NETWORK ||
		    ndev->type != NUBUS_TYPE_ETHERNET)
			continue;

L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
		/* Have we seen it already? */
		if (slots & (1<<ndev->board->slot))
			continue;
		slots |= 1<<ndev->board->slot;

		/* Is it one of ours? */
		if ((id = macsonic_ident(ndev)) != -1)
			break;
	}

	if (ndev == NULL)
		return -ENODEV;

	switch (id) {
	case MACSONIC_DUODOCK:
		base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
		prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
485 486
		sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1 |
		            SONIC_DCR_TFT0;
L
Linus Torvalds 已提交
487
		reg_offset = 2;
488
		dma_bitmode = SONIC_BITMODE32;
L
Linus Torvalds 已提交
489 490 491 492 493 494
		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;
495
		dma_bitmode = SONIC_BITMODE32;
L
Linus Torvalds 已提交
496 497 498 499
		break;
	case MACSONIC_APPLE16:
		base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
		prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
500
		sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
501
		            SONIC_DCR_PO1 | SONIC_DCR_BMS;
L
Linus Torvalds 已提交
502
		reg_offset = 0;
503
		dma_bitmode = SONIC_BITMODE16;
L
Linus Torvalds 已提交
504 505 506 507
		break;
	case MACSONIC_DAYNALINK:
		base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
		prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
508
		sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
509
		            SONIC_DCR_PO1 | SONIC_DCR_BMS;
L
Linus Torvalds 已提交
510
		reg_offset = 0;
511
		dma_bitmode = SONIC_BITMODE16;
L
Linus Torvalds 已提交
512 513 514 515
		break;
	case MACSONIC_DAYNA:
		base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
		prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
516 517
		sonic_dcr = SONIC_DCR_BMS |
		            SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
L
Linus Torvalds 已提交
518
		reg_offset = 0;
519
		dma_bitmode = SONIC_BITMODE16;
L
Linus Torvalds 已提交
520 521 522 523 524 525
		break;
	default:
		printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
		return -ENODEV;
	}

526 527
	/* Danger!  My arms are flailing wildly!  You *must* set lp->reg_offset
	 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
L
Linus Torvalds 已提交
528
	dev->base_addr = base_addr;
529 530
	lp->reg_offset = reg_offset;
	lp->dma_bitmode = dma_bitmode;
L
Linus Torvalds 已提交
531 532 533 534 535 536 537
	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",
538
	       dev_name(lp->device), ndev->board->name, ndev->board->slot);
L
Linus Torvalds 已提交
539
	printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
540
	       dev_name(lp->device), SONIC_READ(SONIC_SR), dma_bitmode?32:16, reg_offset);
L
Linus Torvalds 已提交
541

542
#if 0 /* This is sometimes useful to find out how MacOS configured the card. */
543
	printk(KERN_INFO "%s: DCR: 0x%04x, DCR2: 0x%04x\n", dev_name(lp->device),
544 545
	       SONIC_READ(SONIC_DCR) & 0xffff, SONIC_READ(SONIC_DCR2) & 0xffff);
#endif
L
Linus Torvalds 已提交
546 547

	/* Software reset, then initialize control registers. */
548 549 550 551 552 553
	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
	SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
	/* This *must* be written back to in order to restore the
	 * extended programmable output bits, since it may not have been
	 * initialised since the hardware reset. */
	SONIC_WRITE(SONIC_DCR2, 0);
L
Linus Torvalds 已提交
554 555

	/* Clear *and* disable interrupts to be on the safe side */
556 557
	SONIC_WRITE(SONIC_IMR, 0);
	SONIC_WRITE(SONIC_ISR, 0x7fff);
L
Linus Torvalds 已提交
558 559 560 561 562

	/* Now look for the MAC address. */
	if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
		return -ENODEV;

563 564 565 566
	/* Shared init code */
	return macsonic_init(dev);
}

B
Bill Pemberton 已提交
567
static int mac_sonic_probe(struct platform_device *pdev)
568 569 570 571 572 573 574 575 576 577
{
	struct net_device *dev;
	struct sonic_local *lp;
	int err;

	dev = alloc_etherdev(sizeof(struct sonic_local));
	if (!dev)
		return -ENOMEM;

	lp = netdev_priv(dev);
F
Finn Thain 已提交
578 579
	lp->device = &pdev->dev;
	SET_NETDEV_DEV(dev, &pdev->dev);
580
	platform_set_drvdata(pdev, dev);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

	/* This will catch fatal stuff like -ENOMEM as well as success */
	err = mac_onboard_sonic_probe(dev);
	if (err == 0)
		goto found;
	if (err != -ENODEV)
		goto out;
	err = mac_nubus_sonic_probe(dev);
	if (err)
		goto out;
found:
	err = register_netdev(dev);
	if (err)
		goto out;

J
Johannes Berg 已提交
596
	printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
L
Linus Torvalds 已提交
597

598
	return 0;
L
Linus Torvalds 已提交
599

600 601
out:
	free_netdev(dev);
L
Linus Torvalds 已提交
602

603 604 605 606 607
	return err;
}

MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
module_param(sonic_debug, int, 0);
L
Linus Torvalds 已提交
608
MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
609
MODULE_ALIAS("platform:macsonic");
L
Linus Torvalds 已提交
610

611 612
#include "sonic.c"

B
Bill Pemberton 已提交
613
static int mac_sonic_device_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
614
{
F
Finn Thain 已提交
615
	struct net_device *dev = platform_get_drvdata(pdev);
616 617
	struct sonic_local* lp = netdev_priv(dev);

F
Finn Thain 已提交
618
	unregister_netdev(dev);
619 620
	dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
	                  lp->descriptors, lp->descriptors_laddr);
F
Finn Thain 已提交
621
	free_netdev(dev);
622

L
Linus Torvalds 已提交
623 624 625
	return 0;
}

626
static struct platform_driver mac_sonic_driver = {
627
	.probe  = mac_sonic_probe,
B
Bill Pemberton 已提交
628
	.remove = mac_sonic_device_remove,
629
	.driver	= {
630
		.name	= mac_sonic_string,
631
	},
632 633
};

634
module_platform_driver(mac_sonic_driver);