1 // SPDX-License-Identifier: GPL-2.0
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.
13 * Debugging Andreas Ehliar, Michael Schmitz
16 * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
18 * This driver is based on work from Andreas Busse, but most of
19 * the code is rewritten.
21 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
23 * A driver for the Mac onboard Sonic ethernet chip.
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.
29 * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
31 * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems
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>
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>
55 #include <asm/pgtable.h>
57 #include <asm/hwtest.h>
59 #include <asm/macintosh.h>
60 #include <asm/macints.h>
61 #include <asm/mac_via.h>
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) \
70 #define SONIC_WRITE(reg,val) (nubus_writew(val, dev->base_addr + (reg * 4) \
73 /* For onboard SONIC */
74 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
75 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
85 /* For the built-in SONIC in the Duo Dock */
86 #define DUODOCK_SONIC_REGISTERS 0xe10000
87 #define DUODOCK_SONIC_PROM_BASE 0xe12000
89 /* For Apple-style NuBus SONIC */
90 #define APPLE_SONIC_REGISTERS 0
91 #define APPLE_SONIC_PROM_BASE 0x40000
93 /* Daynalink LC SONIC */
94 #define DAYNALINK_PROM_BASE 0x400000
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
103 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
106 * For reversing the PROM address
109 static inline void bit_reverse_addr(unsigned char addr
[6])
113 for(i
= 0; i
< 6; i
++)
114 addr
[i
] = bitrev8(addr
[i
]);
117 static irqreturn_t
macsonic_interrupt(int irq
, void *dev_id
)
122 local_irq_save(flags
);
123 result
= sonic_interrupt(irq
, dev_id
);
124 local_irq_restore(flags
);
128 static int macsonic_open(struct net_device
* dev
)
132 retval
= request_irq(dev
->irq
, sonic_interrupt
, 0, "sonic", dev
);
134 printk(KERN_ERR
"%s: unable to get IRQ %d.\n",
135 dev
->name
, dev
->irq
);
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.
142 if (dev
->irq
== IRQ_AUTO_3
) {
143 retval
= request_irq(IRQ_NUBUS_9
, macsonic_interrupt
, 0,
146 printk(KERN_ERR
"%s: unable to get IRQ %d.\n",
147 dev
->name
, IRQ_NUBUS_9
);
151 retval
= sonic_open(dev
);
157 if (dev
->irq
== IRQ_AUTO_3
)
158 free_irq(IRQ_NUBUS_9
, dev
);
160 free_irq(dev
->irq
, dev
);
165 static int macsonic_close(struct net_device
* dev
)
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
);
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
,
186 static int macsonic_init(struct net_device
*dev
)
188 struct sonic_local
* lp
= netdev_priv(dev
);
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
,
194 SONIC_BUS_SCALE(lp
->dma_bitmode
),
195 &lp
->descriptors_laddr
,
197 if (lp
->descriptors
== NULL
)
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
));
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
));
217 dev
->netdev_ops
= &macsonic_netdev_ops
;
218 dev
->watchdog_timeo
= TX_TIMEOUT
;
221 * clear tally counter
223 SONIC_WRITE(SONIC_CRCT
, 0xffff);
224 SONIC_WRITE(SONIC_FAET
, 0xffff);
225 SONIC_WRITE(SONIC_MPT
, 0xffff);
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))
235 static void mac_onboard_sonic_ethernet_addr(struct net_device
*dev
)
237 struct sonic_local
*lp
= netdev_priv(dev
);
238 const int prom_addr
= ONBOARD_SONIC_PROM_BASE
;
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.
246 if (hwreg_present((void *)prom_addr
)) {
249 for (i
= 0; i
< 6; i
++)
250 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
251 if (!INVALID_MAC(dev
->dev_addr
))
255 * Most of the time, the address is bit-reversed. The NetBSD
256 * source has a rather long and detailed historical account of
259 bit_reverse_addr(dev
->dev_addr
);
260 if (!INVALID_MAC(dev
->dev_addr
))
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.
267 printk(KERN_WARNING
"macsonic: MAC address in PROM seems "
268 "to be invalid, trying CAM\n");
270 printk(KERN_WARNING
"macsonic: cannot read MAC address from "
271 "PROM, trying CAM\n");
274 /* This only works if MacOS has already initialized the card. */
276 SONIC_WRITE(SONIC_CMD
, SONIC_CR_RST
);
277 SONIC_WRITE(SONIC_CEP
, 15);
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;
289 if (!INVALID_MAC(dev
->dev_addr
))
292 /* Still nonsense ... messed up someplace! */
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
);
299 static int mac_onboard_sonic_probe(struct net_device
*dev
)
301 struct sonic_local
* lp
= netdev_priv(dev
);
303 bool commslot
= macintosh_config
->expansion_type
== MAC_EXP_PDS_COMM
;
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
) {
312 card_present
= hwreg_present((void*)ONBOARD_SONIC_REGISTERS
);
314 pr_info("Onboard/comm-slot SONIC not found\n");
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
;
323 dev
->irq
= IRQ_AUTO_3
;
325 dev
->irq
= IRQ_NUBUS_9
;
327 /* The PowerBook's SONIC is 16 bit always. */
328 if (macintosh_config
->ident
== MAC_MODEL_PB520
) {
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. */
338 lp
->dma_bitmode
= SONIC_BITMODE16
;
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
;
345 lp
->dma_bitmode
= SONIC_BITMODE16
;
349 /* All onboard cards are at offset 2 with 32 bit DMA. */
351 lp
->dma_bitmode
= SONIC_BITMODE32
;
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,
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);
363 /* Software reset, then initialize control registers. */
364 SONIC_WRITE(SONIC_CMD
, SONIC_CR_RST
);
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));
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);
375 /* Clear *and* disable interrupts to be on the safe side */
376 SONIC_WRITE(SONIC_IMR
, 0);
377 SONIC_WRITE(SONIC_ISR
, 0x7fff);
379 /* Now look for the MAC address. */
380 mac_onboard_sonic_ethernet_addr(dev
);
382 pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
383 dev
->base_addr
, dev
->dev_addr
, dev
->irq
);
385 /* Shared init code */
386 return macsonic_init(dev
);
389 static int mac_sonic_nubus_ethernet_addr(struct net_device
*dev
,
390 unsigned long prom_addr
, int id
)
393 for(i
= 0; i
< 6; i
++)
394 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
396 /* Some of the addresses are bit-reversed */
397 if (id
!= MACSONIC_DAYNA
)
398 bit_reverse_addr(dev
->dev_addr
);
403 static int macsonic_ident(struct nubus_rsrc
*fres
)
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
;
414 return MACSONIC_APPLE
;
417 if (fres
->dr_hw
== NUBUS_DRHW_SMC9194
&&
418 fres
->dr_sw
== NUBUS_DRSW_DAYNA
)
419 return MACSONIC_DAYNA
;
421 if (fres
->dr_hw
== NUBUS_DRHW_APPLE_SONIC_LC
&&
422 fres
->dr_sw
== 0) { /* huh? */
423 return MACSONIC_APPLE16
;
428 static int mac_sonic_nubus_probe_board(struct nubus_board
*board
, int id
,
429 struct net_device
*dev
)
431 struct sonic_local
* lp
= netdev_priv(dev
);
432 unsigned long base_addr
, prom_addr
;
434 int reg_offset
, dma_bitmode
;
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
|
443 dma_bitmode
= SONIC_BITMODE32
;
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
;
450 dma_bitmode
= SONIC_BITMODE32
;
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
;
458 dma_bitmode
= SONIC_BITMODE16
;
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
;
466 dma_bitmode
= SONIC_BITMODE16
;
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
;
474 dma_bitmode
= SONIC_BITMODE16
;
477 printk(KERN_ERR
"macsonic: WTF, id is %d\n", id
);
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
);
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
);
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);
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);
505 /* Clear *and* disable interrupts to be on the safe side */
506 SONIC_WRITE(SONIC_IMR
, 0);
507 SONIC_WRITE(SONIC_ISR
, 0x7fff);
509 /* Now look for the MAC address. */
510 if (mac_sonic_nubus_ethernet_addr(dev
, prom_addr
, id
) != 0)
513 dev_info(&board
->dev
, "SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
514 dev
->base_addr
, dev
->dev_addr
, dev
->irq
);
516 /* Shared init code */
517 return macsonic_init(dev
);
520 static int mac_sonic_platform_probe(struct platform_device
*pdev
)
522 struct net_device
*dev
;
523 struct sonic_local
*lp
;
526 dev
= alloc_etherdev(sizeof(struct sonic_local
));
530 lp
= netdev_priv(dev
);
531 lp
->device
= &pdev
->dev
;
532 SET_NETDEV_DEV(dev
, &pdev
->dev
);
533 platform_set_drvdata(pdev
, dev
);
535 err
= mac_onboard_sonic_probe(dev
);
541 err
= register_netdev(dev
);
553 MODULE_DESCRIPTION("Macintosh SONIC ethernet driver");
554 MODULE_ALIAS("platform:macsonic");
558 static int mac_sonic_platform_remove(struct platform_device
*pdev
)
560 struct net_device
*dev
= platform_get_drvdata(pdev
);
561 struct sonic_local
* lp
= netdev_priv(dev
);
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
);
571 static struct platform_driver mac_sonic_platform_driver
= {
572 .probe
= mac_sonic_platform_probe
,
573 .remove
= mac_sonic_platform_remove
,
579 static int mac_sonic_nubus_probe(struct nubus_board
*board
)
581 struct net_device
*ndev
;
582 struct sonic_local
*lp
;
583 struct nubus_rsrc
*fres
;
587 /* The platform driver will handle a PDS or Comm Slot card (even if
588 * it has a pseudoslot declaration ROM).
590 if (macintosh_config
->expansion_type
== MAC_EXP_PDS_COMM
)
593 for_each_board_func_rsrc(board
, fres
) {
594 if (fres
->category
!= NUBUS_CAT_NETWORK
||
595 fres
->type
!= NUBUS_TYPE_ETHERNET
)
598 id
= macsonic_ident(fres
);
605 ndev
= alloc_etherdev(sizeof(struct sonic_local
));
609 lp
= netdev_priv(ndev
);
610 lp
->device
= &board
->dev
;
611 SET_NETDEV_DEV(ndev
, &board
->dev
);
613 err
= mac_sonic_nubus_probe_board(board
, id
, ndev
);
617 sonic_msg_init(ndev
);
619 err
= register_netdev(ndev
);
623 nubus_set_drvdata(board
, ndev
);
632 static int mac_sonic_nubus_remove(struct nubus_board
*board
)
634 struct net_device
*ndev
= nubus_get_drvdata(board
);
635 struct sonic_local
*lp
= netdev_priv(ndev
);
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
);
646 static struct nubus_driver mac_sonic_nubus_driver
= {
647 .probe
= mac_sonic_nubus_probe
,
648 .remove
= mac_sonic_nubus_remove
,
650 .name
= "macsonic-nubus",
651 .owner
= THIS_MODULE
,
655 static int perr
, nerr
;
657 static int __init
mac_sonic_init(void)
659 perr
= platform_driver_register(&mac_sonic_platform_driver
);
660 nerr
= nubus_driver_register(&mac_sonic_nubus_driver
);
663 module_init(mac_sonic_init
);
665 static void __exit
mac_sonic_exit(void)
668 platform_driver_unregister(&mac_sonic_platform_driver
);
670 nubus_driver_unregister(&mac_sonic_nubus_driver
);
672 module_exit(mac_sonic_exit
);