1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * macsonic.c
4  *
5  * (C) 2005 Finn Thain
6  *
7  * Converted to DMA API, converted to unified driver model, made it work as
8  * a module again, and from the mac68k project, introduced more 32-bit cards
9  * and dhd's support for 16-bit cards.
10  *
11  * (C) 1998 Alan Cox
12  *
13  * Debugging Andreas Ehliar, Michael Schmitz
14  *
15  * Based on code
16  * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
17  *
18  * This driver is based on work from Andreas Busse, but most of
19  * the code is rewritten.
20  *
21  * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
22  *
23  * A driver for the Mac onboard Sonic ethernet chip.
24  *
25  * 98/12/21 MSch: judged from tests on Q800, it's basically working,
26  *		  but eating up both receive and transmit resources
27  *		  and duplicating packets. Needs more testing.
28  *
29  * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
30  *
31  * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems
32  *          on centris.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/gfp.h>
40 #include <linux/interrupt.h>
41 #include <linux/ioport.h>
42 #include <linux/in.h>
43 #include <linux/string.h>
44 #include <linux/delay.h>
45 #include <linux/nubus.h>
46 #include <linux/errno.h>
47 #include <linux/netdevice.h>
48 #include <linux/etherdevice.h>
49 #include <linux/skbuff.h>
50 #include <linux/platform_device.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/bitrev.h>
53 #include <linux/slab.h>
54 
55 #include <asm/pgtable.h>
56 #include <asm/io.h>
57 #include <asm/hwtest.h>
58 #include <asm/dma.h>
59 #include <asm/macintosh.h>
60 #include <asm/macints.h>
61 #include <asm/mac_via.h>
62 
63 #include "sonic.h"
64 
65 /* These should basically be bus-size and endian independent (since
66    the SONIC is at least smart enough that it uses the same endianness
67    as the host, unlike certain less enlightened Macintosh NICs) */
68 #define SONIC_READ(reg) (nubus_readw(dev->base_addr + (reg * 4) \
69 	      + lp->reg_offset))
70 #define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
71 	      + lp->reg_offset))
72 
73 /* For onboard SONIC */
74 #define ONBOARD_SONIC_REGISTERS	0x50F0A000
75 #define ONBOARD_SONIC_PROM_BASE	0x50f08000
76 
77 enum macsonic_type {
78 	MACSONIC_DUODOCK,
79 	MACSONIC_APPLE,
80 	MACSONIC_APPLE16,
81 	MACSONIC_DAYNA,
82 	MACSONIC_DAYNALINK
83 };
84 
85 /* For the built-in SONIC in the Duo Dock */
86 #define DUODOCK_SONIC_REGISTERS 0xe10000
87 #define DUODOCK_SONIC_PROM_BASE 0xe12000
88 
89 /* For Apple-style NuBus SONIC */
90 #define APPLE_SONIC_REGISTERS	0
91 #define APPLE_SONIC_PROM_BASE	0x40000
92 
93 /* Daynalink LC SONIC */
94 #define DAYNALINK_PROM_BASE 0x400000
95 
96 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
97 #define DAYNA_SONIC_REGISTERS   0x180000
98 /* This is what OpenBSD says.  However, this is definitely in NuBus
99    ROM space so we should be able to get it by walking the NuBus
100    resource directories */
101 #define DAYNA_SONIC_MAC_ADDR	0xffe004
102 
103 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
104 
105 /*
106  * For reversing the PROM address
107  */
108 
109 static inline void bit_reverse_addr(unsigned char addr[6])
110 {
111 	int i;
112 
113 	for(i = 0; i < 6; i++)
114 		addr[i] = bitrev8(addr[i]);
115 }
116 
117 static irqreturn_t macsonic_interrupt(int irq, void *dev_id)
118 {
119 	irqreturn_t result;
120 	unsigned long flags;
121 
122 	local_irq_save(flags);
123 	result = sonic_interrupt(irq, dev_id);
124 	local_irq_restore(flags);
125 	return result;
126 }
127 
128 static int macsonic_open(struct net_device* dev)
129 {
130 	int retval;
131 
132 	retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
133 	if (retval) {
134 		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
135 				dev->name, dev->irq);
136 		goto err;
137 	}
138 	/* Under the A/UX interrupt scheme, the onboard SONIC interrupt comes
139 	 * in at priority level 3. However, we sometimes get the level 2 inter-
140 	 * rupt as well, which must prevent re-entrance of the sonic handler.
141 	 */
142 	if (dev->irq == IRQ_AUTO_3) {
143 		retval = request_irq(IRQ_NUBUS_9, macsonic_interrupt, 0,
144 				     "sonic", dev);
145 		if (retval) {
146 			printk(KERN_ERR "%s: unable to get IRQ %d.\n",
147 					dev->name, IRQ_NUBUS_9);
148 			goto err_irq;
149 		}
150 	}
151 	retval = sonic_open(dev);
152 	if (retval)
153 		goto err_irq_nubus;
154 	return 0;
155 
156 err_irq_nubus:
157 	if (dev->irq == IRQ_AUTO_3)
158 		free_irq(IRQ_NUBUS_9, dev);
159 err_irq:
160 	free_irq(dev->irq, dev);
161 err:
162 	return retval;
163 }
164 
165 static int macsonic_close(struct net_device* dev)
166 {
167 	int err;
168 	err = sonic_close(dev);
169 	free_irq(dev->irq, dev);
170 	if (dev->irq == IRQ_AUTO_3)
171 		free_irq(IRQ_NUBUS_9, dev);
172 	return err;
173 }
174 
175 static const struct net_device_ops macsonic_netdev_ops = {
176 	.ndo_open		= macsonic_open,
177 	.ndo_stop		= macsonic_close,
178 	.ndo_start_xmit		= sonic_send_packet,
179 	.ndo_set_rx_mode	= sonic_multicast_list,
180 	.ndo_tx_timeout		= sonic_tx_timeout,
181 	.ndo_get_stats		= sonic_get_stats,
182 	.ndo_validate_addr	= eth_validate_addr,
183 	.ndo_set_mac_address	= eth_mac_addr,
184 };
185 
186 static int macsonic_init(struct net_device *dev)
187 {
188 	struct sonic_local* lp = netdev_priv(dev);
189 
190 	/* Allocate the entire chunk of memory for the descriptors.
191            Note that this cannot cross a 64K boundary. */
192 	lp->descriptors = dma_alloc_coherent(lp->device,
193 					     SIZEOF_SONIC_DESC *
194 					     SONIC_BUS_SCALE(lp->dma_bitmode),
195 					     &lp->descriptors_laddr,
196 					     GFP_KERNEL);
197 	if (lp->descriptors == NULL)
198 		return -ENOMEM;
199 
200 	/* Now set up the pointers to point to the appropriate places */
201 	lp->cda = lp->descriptors;
202 	lp->tda = lp->cda + (SIZEOF_SONIC_CDA
203 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
204 	lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
205 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
206 	lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
207 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
208 
209 	lp->cda_laddr = lp->descriptors_laddr;
210 	lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
211 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
212 	lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
213 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
214 	lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
215 	                     * SONIC_BUS_SCALE(lp->dma_bitmode));
216 
217 	dev->netdev_ops = &macsonic_netdev_ops;
218 	dev->watchdog_timeo = TX_TIMEOUT;
219 
220 	/*
221 	 * clear tally counter
222 	 */
223 	SONIC_WRITE(SONIC_CRCT, 0xffff);
224 	SONIC_WRITE(SONIC_FAET, 0xffff);
225 	SONIC_WRITE(SONIC_MPT, 0xffff);
226 
227 	return 0;
228 }
229 
230 #define INVALID_MAC(mac) (memcmp(mac, "\x08\x00\x07", 3) && \
231                           memcmp(mac, "\x00\xA0\x40", 3) && \
232                           memcmp(mac, "\x00\x80\x19", 3) && \
233                           memcmp(mac, "\x00\x05\x02", 3))
234 
235 static void mac_onboard_sonic_ethernet_addr(struct net_device *dev)
236 {
237 	struct sonic_local *lp = netdev_priv(dev);
238 	const int prom_addr = ONBOARD_SONIC_PROM_BASE;
239 	unsigned short val;
240 
241 	/*
242 	 * On NuBus boards we can sometimes look in the ROM resources.
243 	 * No such luck for comm-slot/onboard.
244 	 * On the PowerBook 520, the PROM base address is a mystery.
245 	 */
246 	if (hwreg_present((void *)prom_addr)) {
247 		int i;
248 
249 		for (i = 0; i < 6; i++)
250 			dev->dev_addr[i] = SONIC_READ_PROM(i);
251 		if (!INVALID_MAC(dev->dev_addr))
252 			return;
253 
254 		/*
255 		 * Most of the time, the address is bit-reversed. The NetBSD
256 		 * source has a rather long and detailed historical account of
257 		 * why this is so.
258 		 */
259 		bit_reverse_addr(dev->dev_addr);
260 		if (!INVALID_MAC(dev->dev_addr))
261 			return;
262 
263 		/*
264 		 * If we still have what seems to be a bogus address, we'll
265 		 * look in the CAM. The top entry should be ours.
266 		 */
267 		printk(KERN_WARNING "macsonic: MAC address in PROM seems "
268 		                    "to be invalid, trying CAM\n");
269 	} else {
270 		printk(KERN_WARNING "macsonic: cannot read MAC address from "
271 		                    "PROM, trying CAM\n");
272 	}
273 
274 	/* This only works if MacOS has already initialized the card. */
275 
276 	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
277 	SONIC_WRITE(SONIC_CEP, 15);
278 
279 	val = SONIC_READ(SONIC_CAP2);
280 	dev->dev_addr[5] = val >> 8;
281 	dev->dev_addr[4] = val & 0xff;
282 	val = SONIC_READ(SONIC_CAP1);
283 	dev->dev_addr[3] = val >> 8;
284 	dev->dev_addr[2] = val & 0xff;
285 	val = SONIC_READ(SONIC_CAP0);
286 	dev->dev_addr[1] = val >> 8;
287 	dev->dev_addr[0] = val & 0xff;
288 
289 	if (!INVALID_MAC(dev->dev_addr))
290 		return;
291 
292 	/* Still nonsense ... messed up someplace! */
293 
294 	printk(KERN_WARNING "macsonic: MAC address in CAM entry 15 "
295 	                    "seems invalid, will use a random MAC\n");
296 	eth_hw_addr_random(dev);
297 }
298 
299 static int mac_onboard_sonic_probe(struct net_device *dev)
300 {
301 	struct sonic_local* lp = netdev_priv(dev);
302 	int sr;
303 	bool commslot = macintosh_config->expansion_type == MAC_EXP_PDS_COMM;
304 
305 	/* Bogus probing, on the models which may or may not have
306 	   Ethernet (BTW, the Ethernet *is* always at the same
307 	   address, and nothing else lives there, at least if Apple's
308 	   documentation is to be believed) */
309 	if (commslot || macintosh_config->ident == MAC_MODEL_C610) {
310 		int card_present;
311 
312 		card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
313 		if (!card_present) {
314 			pr_info("Onboard/comm-slot SONIC not found\n");
315 			return -ENODEV;
316 		}
317 	}
318 
319 	/* Danger!  My arms are flailing wildly!  You *must* set lp->reg_offset
320 	 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
321 	dev->base_addr = ONBOARD_SONIC_REGISTERS;
322 	if (via_alt_mapping)
323 		dev->irq = IRQ_AUTO_3;
324 	else
325 		dev->irq = IRQ_NUBUS_9;
326 
327 	/* The PowerBook's SONIC is 16 bit always. */
328 	if (macintosh_config->ident == MAC_MODEL_PB520) {
329 		lp->reg_offset = 0;
330 		lp->dma_bitmode = SONIC_BITMODE16;
331 	} else if (commslot) {
332 		/* Some of the comm-slot cards are 16 bit.  But some
333 		   of them are not.  The 32-bit cards use offset 2 and
334 		   have known revisions, we try reading the revision
335 		   register at offset 2, if we don't get a known revision
336 		   we assume 16 bit at offset 0.  */
337 		lp->reg_offset = 2;
338 		lp->dma_bitmode = SONIC_BITMODE16;
339 
340 		sr = SONIC_READ(SONIC_SR);
341 		if (sr == 0x0004 || sr == 0x0006 || sr == 0x0100 || sr == 0x0101)
342 			/* 83932 is 0x0004 or 0x0006, 83934 is 0x0100 or 0x0101 */
343 			lp->dma_bitmode = SONIC_BITMODE32;
344 		else {
345 			lp->dma_bitmode = SONIC_BITMODE16;
346 			lp->reg_offset = 0;
347 		}
348 	} else {
349 		/* All onboard cards are at offset 2 with 32 bit DMA. */
350 		lp->reg_offset = 2;
351 		lp->dma_bitmode = SONIC_BITMODE32;
352 	}
353 
354 	pr_info("Onboard/comm-slot SONIC, revision 0x%04x, %d bit DMA, register offset %d\n",
355 		SONIC_READ(SONIC_SR), lp->dma_bitmode ? 32 : 16,
356 		lp->reg_offset);
357 
358 	/* This is sometimes useful to find out how MacOS configured the card */
359 	pr_debug("%s: DCR=0x%04x, DCR2=0x%04x\n", __func__,
360 		 SONIC_READ(SONIC_DCR) & 0xffff,
361 		 SONIC_READ(SONIC_DCR2) & 0xffff);
362 
363 	/* Software reset, then initialize control registers. */
364 	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
365 
366 	SONIC_WRITE(SONIC_DCR, SONIC_DCR_EXBUS | SONIC_DCR_BMS |
367 	                       SONIC_DCR_RFT1  | SONIC_DCR_TFT0 |
368 	                       (lp->dma_bitmode ? SONIC_DCR_DW : 0));
369 
370 	/* This *must* be written back to in order to restore the
371 	 * extended programmable output bits, as it may not have been
372 	 * initialised since the hardware reset. */
373 	SONIC_WRITE(SONIC_DCR2, 0);
374 
375 	/* Clear *and* disable interrupts to be on the safe side */
376 	SONIC_WRITE(SONIC_IMR, 0);
377 	SONIC_WRITE(SONIC_ISR, 0x7fff);
378 
379 	/* Now look for the MAC address. */
380 	mac_onboard_sonic_ethernet_addr(dev);
381 
382 	pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
383 		dev->base_addr, dev->dev_addr, dev->irq);
384 
385 	/* Shared init code */
386 	return macsonic_init(dev);
387 }
388 
389 static int mac_sonic_nubus_ethernet_addr(struct net_device *dev,
390 					 unsigned long prom_addr, int id)
391 {
392 	int i;
393 	for(i = 0; i < 6; i++)
394 		dev->dev_addr[i] = SONIC_READ_PROM(i);
395 
396 	/* Some of the addresses are bit-reversed */
397 	if (id != MACSONIC_DAYNA)
398 		bit_reverse_addr(dev->dev_addr);
399 
400 	return 0;
401 }
402 
403 static int macsonic_ident(struct nubus_rsrc *fres)
404 {
405 	if (fres->dr_hw == NUBUS_DRHW_ASANTE_LC &&
406 	    fres->dr_sw == NUBUS_DRSW_SONIC_LC)
407 		return MACSONIC_DAYNALINK;
408 	if (fres->dr_hw == NUBUS_DRHW_SONIC &&
409 	    fres->dr_sw == NUBUS_DRSW_APPLE) {
410 		/* There has to be a better way to do this... */
411 		if (strstr(fres->board->name, "DuoDock"))
412 			return MACSONIC_DUODOCK;
413 		else
414 			return MACSONIC_APPLE;
415 	}
416 
417 	if (fres->dr_hw == NUBUS_DRHW_SMC9194 &&
418 	    fres->dr_sw == NUBUS_DRSW_DAYNA)
419 		return MACSONIC_DAYNA;
420 
421 	if (fres->dr_hw == NUBUS_DRHW_APPLE_SONIC_LC &&
422 	    fres->dr_sw == 0) { /* huh? */
423 		return MACSONIC_APPLE16;
424 	}
425 	return -1;
426 }
427 
428 static int mac_sonic_nubus_probe_board(struct nubus_board *board, int id,
429 				       struct net_device *dev)
430 {
431 	struct sonic_local* lp = netdev_priv(dev);
432 	unsigned long base_addr, prom_addr;
433 	u16 sonic_dcr;
434 	int reg_offset, dma_bitmode;
435 
436 	switch (id) {
437 	case MACSONIC_DUODOCK:
438 		base_addr = board->slot_addr + DUODOCK_SONIC_REGISTERS;
439 		prom_addr = board->slot_addr + DUODOCK_SONIC_PROM_BASE;
440 		sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1 |
441 		            SONIC_DCR_TFT0;
442 		reg_offset = 2;
443 		dma_bitmode = SONIC_BITMODE32;
444 		break;
445 	case MACSONIC_APPLE:
446 		base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
447 		prom_addr = board->slot_addr + APPLE_SONIC_PROM_BASE;
448 		sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
449 		reg_offset = 0;
450 		dma_bitmode = SONIC_BITMODE32;
451 		break;
452 	case MACSONIC_APPLE16:
453 		base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
454 		prom_addr = board->slot_addr + APPLE_SONIC_PROM_BASE;
455 		sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
456 		            SONIC_DCR_PO1 | SONIC_DCR_BMS;
457 		reg_offset = 0;
458 		dma_bitmode = SONIC_BITMODE16;
459 		break;
460 	case MACSONIC_DAYNALINK:
461 		base_addr = board->slot_addr + APPLE_SONIC_REGISTERS;
462 		prom_addr = board->slot_addr + DAYNALINK_PROM_BASE;
463 		sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0 |
464 		            SONIC_DCR_PO1 | SONIC_DCR_BMS;
465 		reg_offset = 0;
466 		dma_bitmode = SONIC_BITMODE16;
467 		break;
468 	case MACSONIC_DAYNA:
469 		base_addr = board->slot_addr + DAYNA_SONIC_REGISTERS;
470 		prom_addr = board->slot_addr + DAYNA_SONIC_MAC_ADDR;
471 		sonic_dcr = SONIC_DCR_BMS |
472 		            SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
473 		reg_offset = 0;
474 		dma_bitmode = SONIC_BITMODE16;
475 		break;
476 	default:
477 		printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
478 		return -ENODEV;
479 	}
480 
481 	/* Danger!  My arms are flailing wildly!  You *must* set lp->reg_offset
482 	 * and dev->base_addr before using SONIC_READ() or SONIC_WRITE() */
483 	dev->base_addr = base_addr;
484 	lp->reg_offset = reg_offset;
485 	lp->dma_bitmode = dma_bitmode;
486 	dev->irq = SLOT2IRQ(board->slot);
487 
488 	dev_info(&board->dev, "%s, revision 0x%04x, %d bit DMA, register offset %d\n",
489 		 board->name, SONIC_READ(SONIC_SR),
490 		 lp->dma_bitmode ? 32 : 16, lp->reg_offset);
491 
492 	/* This is sometimes useful to find out how MacOS configured the card */
493 	dev_dbg(&board->dev, "%s: DCR=0x%04x, DCR2=0x%04x\n", __func__,
494 		SONIC_READ(SONIC_DCR) & 0xffff,
495 		SONIC_READ(SONIC_DCR2) & 0xffff);
496 
497 	/* Software reset, then initialize control registers. */
498 	SONIC_WRITE(SONIC_CMD, SONIC_CR_RST);
499 	SONIC_WRITE(SONIC_DCR, sonic_dcr | (dma_bitmode ? SONIC_DCR_DW : 0));
500 	/* This *must* be written back to in order to restore the
501 	 * extended programmable output bits, since it may not have been
502 	 * initialised since the hardware reset. */
503 	SONIC_WRITE(SONIC_DCR2, 0);
504 
505 	/* Clear *and* disable interrupts to be on the safe side */
506 	SONIC_WRITE(SONIC_IMR, 0);
507 	SONIC_WRITE(SONIC_ISR, 0x7fff);
508 
509 	/* Now look for the MAC address. */
510 	if (mac_sonic_nubus_ethernet_addr(dev, prom_addr, id) != 0)
511 		return -ENODEV;
512 
513 	dev_info(&board->dev, "SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
514 		 dev->base_addr, dev->dev_addr, dev->irq);
515 
516 	/* Shared init code */
517 	return macsonic_init(dev);
518 }
519 
520 static int mac_sonic_platform_probe(struct platform_device *pdev)
521 {
522 	struct net_device *dev;
523 	struct sonic_local *lp;
524 	int err;
525 
526 	dev = alloc_etherdev(sizeof(struct sonic_local));
527 	if (!dev)
528 		return -ENOMEM;
529 
530 	lp = netdev_priv(dev);
531 	lp->device = &pdev->dev;
532 	SET_NETDEV_DEV(dev, &pdev->dev);
533 	platform_set_drvdata(pdev, dev);
534 
535 	err = mac_onboard_sonic_probe(dev);
536 	if (err)
537 		goto out;
538 
539 	sonic_msg_init(dev);
540 
541 	err = register_netdev(dev);
542 	if (err)
543 		goto out;
544 
545 	return 0;
546 
547 out:
548 	free_netdev(dev);
549 
550 	return err;
551 }
552 
553 MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
554 MODULE_ALIAS("platform:macsonic");
555 
556 #include "sonic.c"
557 
558 static int mac_sonic_platform_remove(struct platform_device *pdev)
559 {
560 	struct net_device *dev = platform_get_drvdata(pdev);
561 	struct sonic_local* lp = netdev_priv(dev);
562 
563 	unregister_netdev(dev);
564 	dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
565 	                  lp->descriptors, lp->descriptors_laddr);
566 	free_netdev(dev);
567 
568 	return 0;
569 }
570 
571 static struct platform_driver mac_sonic_platform_driver = {
572 	.probe  = mac_sonic_platform_probe,
573 	.remove = mac_sonic_platform_remove,
574 	.driver = {
575 		.name = "macsonic",
576 	},
577 };
578 
579 static int mac_sonic_nubus_probe(struct nubus_board *board)
580 {
581 	struct net_device *ndev;
582 	struct sonic_local *lp;
583 	struct nubus_rsrc *fres;
584 	int id = -1;
585 	int err;
586 
587 	/* The platform driver will handle a PDS or Comm Slot card (even if
588 	 * it has a pseudoslot declaration ROM).
589 	 */
590 	if (macintosh_config->expansion_type == MAC_EXP_PDS_COMM)
591 		return -ENODEV;
592 
593 	for_each_board_func_rsrc(board, fres) {
594 		if (fres->category != NUBUS_CAT_NETWORK ||
595 		    fres->type != NUBUS_TYPE_ETHERNET)
596 			continue;
597 
598 		id = macsonic_ident(fres);
599 		if (id != -1)
600 			break;
601 	}
602 	if (!fres)
603 		return -ENODEV;
604 
605 	ndev = alloc_etherdev(sizeof(struct sonic_local));
606 	if (!ndev)
607 		return -ENOMEM;
608 
609 	lp = netdev_priv(ndev);
610 	lp->device = &board->dev;
611 	SET_NETDEV_DEV(ndev, &board->dev);
612 
613 	err = mac_sonic_nubus_probe_board(board, id, ndev);
614 	if (err)
615 		goto out;
616 
617 	sonic_msg_init(ndev);
618 
619 	err = register_netdev(ndev);
620 	if (err)
621 		goto out;
622 
623 	nubus_set_drvdata(board, ndev);
624 
625 	return 0;
626 
627 out:
628 	free_netdev(ndev);
629 	return err;
630 }
631 
632 static int mac_sonic_nubus_remove(struct nubus_board *board)
633 {
634 	struct net_device *ndev = nubus_get_drvdata(board);
635 	struct sonic_local *lp = netdev_priv(ndev);
636 
637 	unregister_netdev(ndev);
638 	dma_free_coherent(lp->device,
639 			  SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
640 			  lp->descriptors, lp->descriptors_laddr);
641 	free_netdev(ndev);
642 
643 	return 0;
644 }
645 
646 static struct nubus_driver mac_sonic_nubus_driver = {
647 	.probe  = mac_sonic_nubus_probe,
648 	.remove = mac_sonic_nubus_remove,
649 	.driver = {
650 		.name = "macsonic-nubus",
651 		.owner = THIS_MODULE,
652 	},
653 };
654 
655 static int perr, nerr;
656 
657 static int __init mac_sonic_init(void)
658 {
659 	perr = platform_driver_register(&mac_sonic_platform_driver);
660 	nerr = nubus_driver_register(&mac_sonic_nubus_driver);
661 	return 0;
662 }
663 module_init(mac_sonic_init);
664 
665 static void __exit mac_sonic_exit(void)
666 {
667 	if (!perr)
668 		platform_driver_unregister(&mac_sonic_platform_driver);
669 	if (!nerr)
670 		nubus_driver_unregister(&mac_sonic_nubus_driver);
671 }
672 module_exit(mac_sonic_exit);
673