6 * Debugging Andreas Ehliar, Michael Schmitz
9 * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
11 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
14 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
16 * A driver for the Mac onboard Sonic ethernet chip.
18 * 98/12/21 MSch: judged from tests on Q800, it's basically working,
19 * but eating up both receive and transmit resources
20 * and duplicating packets. Needs more testing.
22 * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
24 * 00/10/31 sammy@oh.verio.com: Updated driver for 2.4 kernels, fixed problems
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/ctype.h>
31 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
33 #include <linux/init.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/string.h>
38 #include <linux/delay.h>
39 #include <linux/nubus.h>
40 #include <linux/errno.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/module.h>
46 #include <asm/bootinfo.h>
47 #include <asm/system.h>
48 #include <asm/bitops.h>
49 #include <asm/pgtable.h>
51 #include <asm/hwtest.h>
53 #include <asm/macintosh.h>
54 #include <asm/macints.h>
55 #include <asm/mac_via.h>
56 #include <asm/pgalloc.h>
58 #define SREGS_PAD(n) u16 n;
62 #define SONIC_READ(reg) \
63 nubus_readl(base_addr+(reg))
64 #define SONIC_WRITE(reg,val) \
65 nubus_writel((val), base_addr+(reg))
66 #define sonic_read(dev, reg) \
67 nubus_readl((dev)->base_addr+(reg))
68 #define sonic_write(dev, reg, val) \
69 nubus_writel((val), (dev)->base_addr+(reg))
72 static int sonic_debug
;
73 static int sonic_version_printed
;
75 static int reg_offset
;
77 extern int macsonic_probe(struct net_device
* dev
);
78 extern int mac_onboard_sonic_probe(struct net_device
* dev
);
79 extern int mac_nubus_sonic_probe(struct net_device
* dev
);
81 /* For onboard SONIC */
82 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
83 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
93 /* For the built-in SONIC in the Duo Dock */
94 #define DUODOCK_SONIC_REGISTERS 0xe10000
95 #define DUODOCK_SONIC_PROM_BASE 0xe12000
97 /* For Apple-style NuBus SONIC */
98 #define APPLE_SONIC_REGISTERS 0
99 #define APPLE_SONIC_PROM_BASE 0x40000
101 /* Daynalink LC SONIC */
102 #define DAYNALINK_PROM_BASE 0x400000
104 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
105 #define DAYNA_SONIC_REGISTERS 0x180000
106 /* This is what OpenBSD says. However, this is definitely in NuBus
107 ROM space so we should be able to get it by walking the NuBus
108 resource directories */
109 #define DAYNA_SONIC_MAC_ADDR 0xffe004
111 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
113 int __init
macsonic_probe(struct net_device
* dev
)
117 /* This will catch fatal stuff like -ENOMEM as well as success */
118 if ((rv
= mac_onboard_sonic_probe(dev
)) != -ENODEV
)
120 return mac_nubus_sonic_probe(dev
);
124 * For reversing the PROM address
127 static unsigned char nibbletab
[] = {0, 8, 4, 12, 2, 10, 6, 14,
128 1, 9, 5, 13, 3, 11, 7, 15};
130 static inline void bit_reverse_addr(unsigned char addr
[6])
134 for(i
= 0; i
< 6; i
++)
135 addr
[i
] = ((nibbletab
[addr
[i
] & 0xf] << 4) |
136 nibbletab
[(addr
[i
] >> 4) &0xf]);
139 int __init
macsonic_init(struct net_device
* dev
)
141 struct sonic_local
* lp
;
144 /* Allocate the entire chunk of memory for the descriptors.
145 Note that this cannot cross a 64K boundary. */
146 for (i
= 0; i
< 20; i
++) {
147 unsigned long desc_base
, desc_top
;
148 if((lp
= kmalloc(sizeof(struct sonic_local
), GFP_KERNEL
| GFP_DMA
)) == NULL
) {
149 printk(KERN_ERR
"%s: couldn't allocate descriptor buffers\n", dev
->name
);
153 desc_base
= (unsigned long) lp
;
154 desc_top
= desc_base
+ sizeof(struct sonic_local
);
155 if ((desc_top
& 0xffff) >= (desc_base
& 0xffff))
157 /* Hmm. try again (FIXME: does this actually work?) */
160 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
161 dev
->name
, desc_base
, desc_top
);
165 printk(KERN_ERR
"%s: tried 20 times to allocate descriptor buffers, giving up.\n",
173 /* this code is only here as a curiousity... mainly, where the
174 fuck did SONIC_BUS_SCALE come from, and what was it supposed
175 to do? the normal allocation works great for 32 bit stuffs.. */
177 /* Now set up the pointers to point to the appropriate places */
178 lp
->cda
= lp
->sonic_desc
;
179 lp
->tda
= lp
->cda
+ (SIZEOF_SONIC_CDA
* SONIC_BUS_SCALE(lp
->dma_bitmode
));
180 lp
->rda
= lp
->tda
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
181 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
182 lp
->rra
= lp
->rda
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
183 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
187 memset(lp
, 0, sizeof(struct sonic_local
));
189 lp
->cda_laddr
= (unsigned int)&(lp
->cda
);
190 lp
->tda_laddr
= (unsigned int)lp
->tda
;
191 lp
->rra_laddr
= (unsigned int)lp
->rra
;
192 lp
->rda_laddr
= (unsigned int)lp
->rda
;
194 /* FIXME, maybe we should use skbs */
195 if ((lp
->rba
= (char *)
196 kmalloc(SONIC_NUM_RRS
* SONIC_RBSIZE
, GFP_KERNEL
| GFP_DMA
)) == NULL
) {
197 printk(KERN_ERR
"%s: couldn't allocate receive buffers\n", dev
->name
);
202 lp
->rba_laddr
= (unsigned int)lp
->rba
;
207 /* almost always 12*4096, but let's not take chances */
208 rs
= ((SONIC_NUM_RRS
* SONIC_RBSIZE
+ 4095) / 4096) * 4096;
209 /* almost always under a page, but let's not take chances */
210 ds
= ((sizeof(struct sonic_local
) + 4095) / 4096) * 4096;
211 kernel_set_cachemode(lp
->rba
, rs
, IOMAP_NOCACHE_SER
);
212 kernel_set_cachemode(lp
, ds
, IOMAP_NOCACHE_SER
);
219 dev
->open
= sonic_open
;
220 dev
->stop
= sonic_close
;
221 dev
->hard_start_xmit
= sonic_send_packet
;
222 dev
->get_stats
= sonic_get_stats
;
223 dev
->set_multicast_list
= &sonic_multicast_list
;
226 * clear tally counter
228 sonic_write(dev
, SONIC_CRCT
, 0xffff);
229 sonic_write(dev
, SONIC_FAET
, 0xffff);
230 sonic_write(dev
, SONIC_MPT
, 0xffff);
232 /* Fill in the fields of the device structure with ethernet values. */
237 int __init
mac_onboard_sonic_ethernet_addr(struct net_device
* dev
)
239 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 for(i
= 0; i
< 6; i
++)
245 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
247 /* Most of the time, the address is bit-reversed. The NetBSD
248 source has a rather long and detailed historical account of
250 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
251 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
252 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
253 bit_reverse_addr(dev
->dev_addr
);
257 /* If we still have what seems to be a bogus address, we'll
258 look in the CAM. The top entry should be ours. */
259 /* Danger! This only works if MacOS has already initialized
261 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
262 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
263 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
267 printk(KERN_INFO
"macsonic: PROM seems to be wrong, trying CAM entry 15\n");
269 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
270 sonic_write(dev
, SONIC_CEP
, 15);
272 val
= sonic_read(dev
, SONIC_CAP2
);
273 dev
->dev_addr
[5] = val
>> 8;
274 dev
->dev_addr
[4] = val
& 0xff;
275 val
= sonic_read(dev
, SONIC_CAP1
);
276 dev
->dev_addr
[3] = val
>> 8;
277 dev
->dev_addr
[2] = val
& 0xff;
278 val
= sonic_read(dev
, SONIC_CAP0
);
279 dev
->dev_addr
[1] = val
>> 8;
280 dev
->dev_addr
[0] = val
& 0xff;
282 printk(KERN_INFO
"HW Address from CAM 15: ");
283 for (i
= 0; i
< 6; i
++) {
284 printk("%2.2x", dev
->dev_addr
[i
]);
291 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
292 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
293 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
296 * Still nonsense ... messed up someplace!
298 printk(KERN_ERR
"macsonic: ERROR (INVALID MAC)\n");
303 int __init
mac_onboard_sonic_probe(struct net_device
* dev
)
306 static int once_is_more_than_enough
;
310 if (once_is_more_than_enough
)
312 once_is_more_than_enough
= 1;
317 printk(KERN_INFO
"Checking for internal Macintosh ethernet (SONIC).. ");
319 if (macintosh_config
->ether_type
!= MAC_ETHER_SONIC
)
325 /* Bogus probing, on the models which may or may not have
326 Ethernet (BTW, the Ethernet *is* always at the same
327 address, and nothing else lives there, at least if Apple's
328 documentation is to be believed) */
329 if (macintosh_config
->ident
== MAC_MODEL_Q630
||
330 macintosh_config
->ident
== MAC_MODEL_P588
||
331 macintosh_config
->ident
== MAC_MODEL_C610
) {
335 local_irq_save(flags
);
336 card_present
= hwreg_present((void*)ONBOARD_SONIC_REGISTERS
);
337 local_irq_restore(flags
);
348 dev
= init_etherdev(dev
, sizeof(struct sonic_local
));
351 /* methinks this will always be true but better safe than sorry */
352 if (dev
->priv
== NULL
) {
353 dev
->priv
= kmalloc(sizeof(struct sonic_local
), GFP_KERNEL
);
358 dev
= init_etherdev(NULL
, sizeof(struct sonic_local
));
365 printk("%s: warning! sonic entering with priv already allocated!\n",
367 printk("%s: discarding, will attempt to reallocate\n", dev
->name
);
371 /* Danger! My arms are flailing wildly! You *must* set this
372 before using sonic_read() */
374 dev
->base_addr
= ONBOARD_SONIC_REGISTERS
;
376 dev
->irq
= IRQ_AUTO_3
;
378 dev
->irq
= IRQ_NUBUS_9
;
380 if (!sonic_version_printed
) {
381 printk(KERN_INFO
"%s", version
);
382 sonic_version_printed
= 1;
384 printk(KERN_INFO
"%s: onboard / comm-slot SONIC at 0x%08lx\n",
385 dev
->name
, dev
->base_addr
);
387 /* Now do a song and dance routine in an attempt to determine
390 /* The PowerBook's SONIC is 16 bit always. */
391 if (macintosh_config
->ident
== MAC_MODEL_PB520
) {
394 } else if (macintosh_config
->ident
== MAC_MODEL_C610
) {
398 /* Some of the comm-slot cards are 16 bit. But some
399 of them are not. The 32-bit cards use offset 2 and
400 pad with zeroes or sometimes ones (I think...)
401 Therefore, if we try offset 0 and get a silicon
402 revision of 0, we assume 16 bit. */
405 /* Technically this is not necessary since we zeroed
409 sr
= sonic_read(dev
, SONIC_SR
);
410 if (sr
== 0 || sr
== 0xffff) {
412 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
413 sr
= sonic_read(dev
, SONIC_SR
);
418 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
419 dev
->name
, sr
, dma_bitmode
?32:16, reg_offset
);
423 /* this carries my sincere apologies -- by the time I got to updating
424 the driver, support for "reg_offsets" appeares nowhere in the sonic
425 code, going back for over a year. Fortunately, my Mac does't seem
426 to use whatever this was.
428 If you know how this is supposed to be implemented, either fix it,
429 or contact me (sammy@oh.verio.com) to explain what it is. --Sam */
432 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev
->name
);
436 /* Software reset, then initialize control registers. */
437 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
438 sonic_write(dev
, SONIC_DCR
, SONIC_DCR_BMS
|
439 SONIC_DCR_RFT1
| SONIC_DCR_TFT0
| SONIC_DCR_EXBUS
|
440 (dma_bitmode
? SONIC_DCR_DW
: 0));
442 /* This *must* be written back to in order to restore the
443 extended programmable output bits */
444 sonic_write(dev
, SONIC_DCR2
, 0);
446 /* Clear *and* disable interrupts to be on the safe side */
447 sonic_write(dev
, SONIC_ISR
,0x7fff);
448 sonic_write(dev
, SONIC_IMR
,0);
450 /* Now look for the MAC address. */
451 if (mac_onboard_sonic_ethernet_addr(dev
) != 0)
454 printk(KERN_INFO
"MAC ");
455 for (i
= 0; i
< 6; i
++) {
456 printk("%2.2x", dev
->dev_addr
[i
]);
461 printk(" IRQ %d\n", dev
->irq
);
463 /* Shared init code */
464 return macsonic_init(dev
);
467 int __init
mac_nubus_sonic_ethernet_addr(struct net_device
* dev
,
468 unsigned long prom_addr
,
472 for(i
= 0; i
< 6; i
++)
473 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
474 /* For now we are going to assume that they're all bit-reversed */
475 bit_reverse_addr(dev
->dev_addr
);
480 int __init
macsonic_ident(struct nubus_dev
* ndev
)
482 if (ndev
->dr_hw
== NUBUS_DRHW_ASANTE_LC
&&
483 ndev
->dr_sw
== NUBUS_DRSW_SONIC_LC
)
484 return MACSONIC_DAYNALINK
;
485 if (ndev
->dr_hw
== NUBUS_DRHW_SONIC
&&
486 ndev
->dr_sw
== NUBUS_DRSW_APPLE
) {
487 /* There has to be a better way to do this... */
488 if (strstr(ndev
->board
->name
, "DuoDock"))
489 return MACSONIC_DUODOCK
;
491 return MACSONIC_APPLE
;
496 int __init
mac_nubus_sonic_probe(struct net_device
* dev
)
499 struct nubus_dev
* ndev
= NULL
;
500 struct sonic_local
* lp
;
501 unsigned long base_addr
, prom_addr
;
507 /* Find the first SONIC that hasn't been initialized already */
508 while ((ndev
= nubus_find_type(NUBUS_CAT_NETWORK
,
509 NUBUS_TYPE_ETHERNET
, ndev
)) != NULL
)
511 /* Have we seen it already? */
512 if (slots
& (1<<ndev
->board
->slot
))
514 slots
|= 1<<ndev
->board
->slot
;
516 /* Is it one of ours? */
517 if ((id
= macsonic_ident(ndev
)) != -1)
525 case MACSONIC_DUODOCK
:
526 base_addr
= ndev
->board
->slot_addr
+ DUODOCK_SONIC_REGISTERS
;
527 prom_addr
= ndev
->board
->slot_addr
+ DUODOCK_SONIC_PROM_BASE
;
528 sonic_dcr
= SONIC_DCR_EXBUS
| SONIC_DCR_RFT0
| SONIC_DCR_RFT1
534 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
535 prom_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_PROM_BASE
;
536 sonic_dcr
= SONIC_DCR_BMS
| SONIC_DCR_RFT1
| SONIC_DCR_TFT0
;
540 case MACSONIC_APPLE16
:
541 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
542 prom_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_PROM_BASE
;
543 sonic_dcr
= SONIC_DCR_EXBUS
544 | SONIC_DCR_RFT1
| SONIC_DCR_TFT0
545 | SONIC_DCR_PO1
| SONIC_DCR_BMS
;
549 case MACSONIC_DAYNALINK
:
550 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
551 prom_addr
= ndev
->board
->slot_addr
+ DAYNALINK_PROM_BASE
;
552 sonic_dcr
= SONIC_DCR_RFT1
| SONIC_DCR_TFT0
553 | SONIC_DCR_PO1
| SONIC_DCR_BMS
;
558 base_addr
= ndev
->board
->slot_addr
+ DAYNA_SONIC_REGISTERS
;
559 prom_addr
= ndev
->board
->slot_addr
+ DAYNA_SONIC_MAC_ADDR
;
560 sonic_dcr
= SONIC_DCR_BMS
561 | SONIC_DCR_RFT1
| SONIC_DCR_TFT0
| SONIC_DCR_PO1
;
566 printk(KERN_ERR
"macsonic: WTF, id is %d\n", id
);
571 dev
= init_etherdev(dev
, sizeof(struct sonic_local
));
574 /* methinks this will always be true but better safe than sorry */
575 if (dev
->priv
== NULL
) {
576 dev
->priv
= kmalloc(sizeof(struct sonic_local
), GFP_KERNEL
);
577 if (!dev
->priv
) /* FIXME: kfree dev if necessary */
581 dev
= init_etherdev(NULL
, sizeof(struct sonic_local
));
587 lp
= (struct sonic_local
*) dev
->priv
;
588 memset(lp
, 0, sizeof(struct sonic_local
));
589 /* Danger! My arms are flailing wildly! You *must* set this
590 before using sonic_read() */
591 dev
->base_addr
= base_addr
;
592 dev
->irq
= SLOT2IRQ(ndev
->board
->slot
);
594 if (!sonic_version_printed
) {
595 printk(KERN_INFO
"%s", version
);
596 sonic_version_printed
= 1;
598 printk(KERN_INFO
"%s: %s in slot %X\n",
599 dev
->name
, ndev
->board
->name
, ndev
->board
->slot
);
600 printk(KERN_INFO
"%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
601 dev
->name
, sonic_read(dev
, SONIC_SR
), dma_bitmode
?32:16, reg_offset
);
604 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev
->name
);
608 /* Software reset, then initialize control registers. */
609 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
610 sonic_write(dev
, SONIC_DCR
, sonic_dcr
611 | (dma_bitmode
? SONIC_DCR_DW
: 0));
613 /* Clear *and* disable interrupts to be on the safe side */
614 sonic_write(dev
, SONIC_ISR
,0x7fff);
615 sonic_write(dev
, SONIC_IMR
,0);
617 /* Now look for the MAC address. */
618 if (mac_nubus_sonic_ethernet_addr(dev
, prom_addr
, id
) != 0)
621 printk(KERN_INFO
"MAC ");
622 for (i
= 0; i
< 6; i
++) {
623 printk("%2.2x", dev
->dev_addr
[i
]);
627 printk(" IRQ %d\n", dev
->irq
);
629 /* Shared init code */
630 return macsonic_init(dev
);
634 static char namespace[16] = "";
635 static struct net_device dev_macsonic
;
637 MODULE_PARM(sonic_debug
, "i");
638 MODULE_PARM_DESC(sonic_debug
, "macsonic debug level (1-4)");
639 MODULE_LICENSE("GPL");
644 dev_macsonic
.name
= namespace;
645 dev_macsonic
.init
= macsonic_probe
;
647 if (register_netdev(&dev_macsonic
) != 0) {
648 printk(KERN_WARNING
"macsonic.c: No card found\n");
657 if (dev_macsonic
.priv
!= NULL
) {
658 unregister_netdev(&dev_macsonic
);
659 kfree(dev_macsonic
.priv
);
660 dev_macsonic
.priv
= NULL
;
666 #define vdma_alloc(foo, bar) ((u32)foo)
667 #define vdma_free(baz)
668 #define sonic_chiptomem(bat) (bat)
669 #define PHYSADDR(quux) (quux)
671 #define sonic_request_irq request_irq
672 #define sonic_free_irq free_irq
678 * 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"
680 * kept-new-versions: 5