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>
45 #include <linux/bitops.h>
47 #include <asm/bootinfo.h>
48 #include <asm/system.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>
57 #define SREGS_PAD(n) u16 n;
61 #define SONIC_READ(reg) \
62 nubus_readl(base_addr+(reg))
63 #define SONIC_WRITE(reg,val) \
64 nubus_writel((val), base_addr+(reg))
65 #define sonic_read(dev, reg) \
66 nubus_readl((dev)->base_addr+(reg))
67 #define sonic_write(dev, reg, val) \
68 nubus_writel((val), (dev)->base_addr+(reg))
71 static int sonic_debug
;
72 static int sonic_version_printed
;
74 static int reg_offset
;
76 extern int mac_onboard_sonic_probe(struct net_device
* dev
);
77 extern int mac_nubus_sonic_probe(struct net_device
* dev
);
79 /* For onboard SONIC */
80 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
81 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
91 /* For the built-in SONIC in the Duo Dock */
92 #define DUODOCK_SONIC_REGISTERS 0xe10000
93 #define DUODOCK_SONIC_PROM_BASE 0xe12000
95 /* For Apple-style NuBus SONIC */
96 #define APPLE_SONIC_REGISTERS 0
97 #define APPLE_SONIC_PROM_BASE 0x40000
99 /* Daynalink LC SONIC */
100 #define DAYNALINK_PROM_BASE 0x400000
102 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
103 #define DAYNA_SONIC_REGISTERS 0x180000
104 /* This is what OpenBSD says. However, this is definitely in NuBus
105 ROM space so we should be able to get it by walking the NuBus
106 resource directories */
107 #define DAYNA_SONIC_MAC_ADDR 0xffe004
109 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
111 struct net_device
* __init
macsonic_probe(int unit
)
113 struct net_device
*dev
= alloc_etherdev(0);
117 return ERR_PTR(-ENOMEM
);
120 sprintf(dev
->name
, "eth%d", unit
);
122 SET_MODULE_OWNER(dev
);
124 /* This will catch fatal stuff like -ENOMEM as well as success */
125 err
= mac_onboard_sonic_probe(dev
);
130 err
= mac_nubus_sonic_probe(dev
);
134 err
= register_netdev(dev
);
146 * For reversing the PROM address
149 static unsigned char nibbletab
[] = {0, 8, 4, 12, 2, 10, 6, 14,
150 1, 9, 5, 13, 3, 11, 7, 15};
152 static inline void bit_reverse_addr(unsigned char addr
[6])
156 for(i
= 0; i
< 6; i
++)
157 addr
[i
] = ((nibbletab
[addr
[i
] & 0xf] << 4) |
158 nibbletab
[(addr
[i
] >> 4) &0xf]);
161 int __init
macsonic_init(struct net_device
* dev
)
163 struct sonic_local
* lp
= NULL
;
166 /* Allocate the entire chunk of memory for the descriptors.
167 Note that this cannot cross a 64K boundary. */
168 for (i
= 0; i
< 20; i
++) {
169 unsigned long desc_base
, desc_top
;
170 if((lp
= kmalloc(sizeof(struct sonic_local
), GFP_KERNEL
| GFP_DMA
)) == NULL
) {
171 printk(KERN_ERR
"%s: couldn't allocate descriptor buffers\n", dev
->name
);
175 desc_base
= (unsigned long) lp
;
176 desc_top
= desc_base
+ sizeof(struct sonic_local
);
177 if ((desc_top
& 0xffff) >= (desc_base
& 0xffff))
179 /* Hmm. try again (FIXME: does this actually work?) */
182 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
183 dev
->name
, desc_base
, desc_top
);
187 printk(KERN_ERR
"%s: tried 20 times to allocate descriptor buffers, giving up.\n",
195 /* this code is only here as a curiousity... mainly, where the
196 fuck did SONIC_BUS_SCALE come from, and what was it supposed
197 to do? the normal allocation works great for 32 bit stuffs.. */
199 /* Now set up the pointers to point to the appropriate places */
200 lp
->cda
= lp
->sonic_desc
;
201 lp
->tda
= lp
->cda
+ (SIZEOF_SONIC_CDA
* SONIC_BUS_SCALE(lp
->dma_bitmode
));
202 lp
->rda
= lp
->tda
+ (SIZEOF_SONIC_TD
* SONIC_NUM_TDS
203 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
204 lp
->rra
= lp
->rda
+ (SIZEOF_SONIC_RD
* SONIC_NUM_RDS
205 * SONIC_BUS_SCALE(lp
->dma_bitmode
));
209 memset(lp
, 0, sizeof(struct sonic_local
));
211 lp
->cda_laddr
= (unsigned int)&(lp
->cda
);
212 lp
->tda_laddr
= (unsigned int)lp
->tda
;
213 lp
->rra_laddr
= (unsigned int)lp
->rra
;
214 lp
->rda_laddr
= (unsigned int)lp
->rda
;
216 /* FIXME, maybe we should use skbs */
217 if ((lp
->rba
= (char *)
218 kmalloc(SONIC_NUM_RRS
* SONIC_RBSIZE
, GFP_KERNEL
| GFP_DMA
)) == NULL
) {
219 printk(KERN_ERR
"%s: couldn't allocate receive buffers\n", dev
->name
);
225 lp
->rba_laddr
= (unsigned int)lp
->rba
;
230 /* almost always 12*4096, but let's not take chances */
231 rs
= ((SONIC_NUM_RRS
* SONIC_RBSIZE
+ 4095) / 4096) * 4096;
232 /* almost always under a page, but let's not take chances */
233 ds
= ((sizeof(struct sonic_local
) + 4095) / 4096) * 4096;
234 kernel_set_cachemode(lp
->rba
, rs
, IOMAP_NOCACHE_SER
);
235 kernel_set_cachemode(lp
, ds
, IOMAP_NOCACHE_SER
);
242 dev
->open
= sonic_open
;
243 dev
->stop
= sonic_close
;
244 dev
->hard_start_xmit
= sonic_send_packet
;
245 dev
->get_stats
= sonic_get_stats
;
246 dev
->set_multicast_list
= &sonic_multicast_list
;
249 * clear tally counter
251 sonic_write(dev
, SONIC_CRCT
, 0xffff);
252 sonic_write(dev
, SONIC_FAET
, 0xffff);
253 sonic_write(dev
, SONIC_MPT
, 0xffff);
258 int __init
mac_onboard_sonic_ethernet_addr(struct net_device
* dev
)
260 const int prom_addr
= ONBOARD_SONIC_PROM_BASE
;
263 /* On NuBus boards we can sometimes look in the ROM resources.
264 No such luck for comm-slot/onboard. */
265 for(i
= 0; i
< 6; i
++)
266 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
268 /* Most of the time, the address is bit-reversed. The NetBSD
269 source has a rather long and detailed historical account of
271 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
272 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
273 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
274 bit_reverse_addr(dev
->dev_addr
);
278 /* If we still have what seems to be a bogus address, we'll
279 look in the CAM. The top entry should be ours. */
280 /* Danger! This only works if MacOS has already initialized
282 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
283 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
284 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
288 printk(KERN_INFO
"macsonic: PROM seems to be wrong, trying CAM entry 15\n");
290 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
291 sonic_write(dev
, SONIC_CEP
, 15);
293 val
= sonic_read(dev
, SONIC_CAP2
);
294 dev
->dev_addr
[5] = val
>> 8;
295 dev
->dev_addr
[4] = val
& 0xff;
296 val
= sonic_read(dev
, SONIC_CAP1
);
297 dev
->dev_addr
[3] = val
>> 8;
298 dev
->dev_addr
[2] = val
& 0xff;
299 val
= sonic_read(dev
, SONIC_CAP0
);
300 dev
->dev_addr
[1] = val
>> 8;
301 dev
->dev_addr
[0] = val
& 0xff;
303 printk(KERN_INFO
"HW Address from CAM 15: ");
304 for (i
= 0; i
< 6; i
++) {
305 printk("%2.2x", dev
->dev_addr
[i
]);
312 if (memcmp(dev
->dev_addr
, "\x08\x00\x07", 3) &&
313 memcmp(dev
->dev_addr
, "\x00\xA0\x40", 3) &&
314 memcmp(dev
->dev_addr
, "\x00\x05\x02", 3))
317 * Still nonsense ... messed up someplace!
319 printk(KERN_ERR
"macsonic: ERROR (INVALID MAC)\n");
324 int __init
mac_onboard_sonic_probe(struct net_device
* dev
)
327 static int once_is_more_than_enough
;
331 if (once_is_more_than_enough
)
333 once_is_more_than_enough
= 1;
338 printk(KERN_INFO
"Checking for internal Macintosh ethernet (SONIC).. ");
340 if (macintosh_config
->ether_type
!= MAC_ETHER_SONIC
)
346 /* Bogus probing, on the models which may or may not have
347 Ethernet (BTW, the Ethernet *is* always at the same
348 address, and nothing else lives there, at least if Apple's
349 documentation is to be believed) */
350 if (macintosh_config
->ident
== MAC_MODEL_Q630
||
351 macintosh_config
->ident
== MAC_MODEL_P588
||
352 macintosh_config
->ident
== MAC_MODEL_C610
) {
356 local_irq_save(flags
);
357 card_present
= hwreg_present((void*)ONBOARD_SONIC_REGISTERS
);
358 local_irq_restore(flags
);
368 /* Danger! My arms are flailing wildly! You *must* set this
369 before using sonic_read() */
371 dev
->base_addr
= ONBOARD_SONIC_REGISTERS
;
373 dev
->irq
= IRQ_AUTO_3
;
375 dev
->irq
= IRQ_NUBUS_9
;
377 if (!sonic_version_printed
) {
378 printk(KERN_INFO
"%s", version
);
379 sonic_version_printed
= 1;
381 printk(KERN_INFO
"%s: onboard / comm-slot SONIC at 0x%08lx\n",
382 dev
->name
, dev
->base_addr
);
384 /* Now do a song and dance routine in an attempt to determine
387 /* The PowerBook's SONIC is 16 bit always. */
388 if (macintosh_config
->ident
== MAC_MODEL_PB520
) {
391 } else if (macintosh_config
->ident
== MAC_MODEL_C610
) {
395 /* Some of the comm-slot cards are 16 bit. But some
396 of them are not. The 32-bit cards use offset 2 and
397 pad with zeroes or sometimes ones (I think...)
398 Therefore, if we try offset 0 and get a silicon
399 revision of 0, we assume 16 bit. */
402 /* Technically this is not necessary since we zeroed
406 sr
= sonic_read(dev
, SONIC_SR
);
407 if (sr
== 0 || sr
== 0xffff) {
409 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
410 sr
= sonic_read(dev
, SONIC_SR
);
415 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
416 dev
->name
, sr
, dma_bitmode
?32:16, reg_offset
);
420 /* this carries my sincere apologies -- by the time I got to updating
421 the driver, support for "reg_offsets" appeares nowhere in the sonic
422 code, going back for over a year. Fortunately, my Mac does't seem
423 to use whatever this was.
425 If you know how this is supposed to be implemented, either fix it,
426 or contact me (sammy@oh.verio.com) to explain what it is. --Sam */
429 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev
->name
);
433 /* Software reset, then initialize control registers. */
434 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
435 sonic_write(dev
, SONIC_DCR
, SONIC_DCR_BMS
|
436 SONIC_DCR_RFT1
| SONIC_DCR_TFT0
| SONIC_DCR_EXBUS
|
437 (dma_bitmode
? SONIC_DCR_DW
: 0));
439 /* This *must* be written back to in order to restore the
440 extended programmable output bits */
441 sonic_write(dev
, SONIC_DCR2
, 0);
443 /* Clear *and* disable interrupts to be on the safe side */
444 sonic_write(dev
, SONIC_ISR
,0x7fff);
445 sonic_write(dev
, SONIC_IMR
,0);
447 /* Now look for the MAC address. */
448 if (mac_onboard_sonic_ethernet_addr(dev
) != 0)
451 printk(KERN_INFO
"MAC ");
452 for (i
= 0; i
< 6; i
++) {
453 printk("%2.2x", dev
->dev_addr
[i
]);
458 printk(" IRQ %d\n", dev
->irq
);
460 /* Shared init code */
461 return macsonic_init(dev
);
464 int __init
mac_nubus_sonic_ethernet_addr(struct net_device
* dev
,
465 unsigned long prom_addr
,
469 for(i
= 0; i
< 6; i
++)
470 dev
->dev_addr
[i
] = SONIC_READ_PROM(i
);
471 /* For now we are going to assume that they're all bit-reversed */
472 bit_reverse_addr(dev
->dev_addr
);
477 int __init
macsonic_ident(struct nubus_dev
* ndev
)
479 if (ndev
->dr_hw
== NUBUS_DRHW_ASANTE_LC
&&
480 ndev
->dr_sw
== NUBUS_DRSW_SONIC_LC
)
481 return MACSONIC_DAYNALINK
;
482 if (ndev
->dr_hw
== NUBUS_DRHW_SONIC
&&
483 ndev
->dr_sw
== NUBUS_DRSW_APPLE
) {
484 /* There has to be a better way to do this... */
485 if (strstr(ndev
->board
->name
, "DuoDock"))
486 return MACSONIC_DUODOCK
;
488 return MACSONIC_APPLE
;
493 int __init
mac_nubus_sonic_probe(struct net_device
* dev
)
496 struct nubus_dev
* ndev
= NULL
;
497 unsigned long base_addr
, prom_addr
;
503 /* Find the first SONIC that hasn't been initialized already */
504 while ((ndev
= nubus_find_type(NUBUS_CAT_NETWORK
,
505 NUBUS_TYPE_ETHERNET
, ndev
)) != NULL
)
507 /* Have we seen it already? */
508 if (slots
& (1<<ndev
->board
->slot
))
510 slots
|= 1<<ndev
->board
->slot
;
512 /* Is it one of ours? */
513 if ((id
= macsonic_ident(ndev
)) != -1)
521 case MACSONIC_DUODOCK
:
522 base_addr
= ndev
->board
->slot_addr
+ DUODOCK_SONIC_REGISTERS
;
523 prom_addr
= ndev
->board
->slot_addr
+ DUODOCK_SONIC_PROM_BASE
;
524 sonic_dcr
= SONIC_DCR_EXBUS
| SONIC_DCR_RFT0
| SONIC_DCR_RFT1
530 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
531 prom_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_PROM_BASE
;
532 sonic_dcr
= SONIC_DCR_BMS
| SONIC_DCR_RFT1
| SONIC_DCR_TFT0
;
536 case MACSONIC_APPLE16
:
537 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
538 prom_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_PROM_BASE
;
539 sonic_dcr
= SONIC_DCR_EXBUS
540 | SONIC_DCR_RFT1
| SONIC_DCR_TFT0
541 | SONIC_DCR_PO1
| SONIC_DCR_BMS
;
545 case MACSONIC_DAYNALINK
:
546 base_addr
= ndev
->board
->slot_addr
+ APPLE_SONIC_REGISTERS
;
547 prom_addr
= ndev
->board
->slot_addr
+ DAYNALINK_PROM_BASE
;
548 sonic_dcr
= SONIC_DCR_RFT1
| SONIC_DCR_TFT0
549 | SONIC_DCR_PO1
| SONIC_DCR_BMS
;
554 base_addr
= ndev
->board
->slot_addr
+ DAYNA_SONIC_REGISTERS
;
555 prom_addr
= ndev
->board
->slot_addr
+ DAYNA_SONIC_MAC_ADDR
;
556 sonic_dcr
= SONIC_DCR_BMS
557 | SONIC_DCR_RFT1
| SONIC_DCR_TFT0
| SONIC_DCR_PO1
;
562 printk(KERN_ERR
"macsonic: WTF, id is %d\n", id
);
566 /* Danger! My arms are flailing wildly! You *must* set this
567 before using sonic_read() */
568 dev
->base_addr
= base_addr
;
569 dev
->irq
= SLOT2IRQ(ndev
->board
->slot
);
571 if (!sonic_version_printed
) {
572 printk(KERN_INFO
"%s", version
);
573 sonic_version_printed
= 1;
575 printk(KERN_INFO
"%s: %s in slot %X\n",
576 dev
->name
, ndev
->board
->name
, ndev
->board
->slot
);
577 printk(KERN_INFO
"%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
578 dev
->name
, sonic_read(dev
, SONIC_SR
), dma_bitmode
?32:16, reg_offset
);
581 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev
->name
);
585 /* Software reset, then initialize control registers. */
586 sonic_write(dev
, SONIC_CMD
, SONIC_CR_RST
);
587 sonic_write(dev
, SONIC_DCR
, sonic_dcr
588 | (dma_bitmode
? SONIC_DCR_DW
: 0));
590 /* Clear *and* disable interrupts to be on the safe side */
591 sonic_write(dev
, SONIC_ISR
,0x7fff);
592 sonic_write(dev
, SONIC_IMR
,0);
594 /* Now look for the MAC address. */
595 if (mac_nubus_sonic_ethernet_addr(dev
, prom_addr
, id
) != 0)
598 printk(KERN_INFO
"MAC ");
599 for (i
= 0; i
< 6; i
++) {
600 printk("%2.2x", dev
->dev_addr
[i
]);
604 printk(" IRQ %d\n", dev
->irq
);
606 /* Shared init code */
607 return macsonic_init(dev
);
611 static struct net_device
*dev_macsonic
;
613 MODULE_PARM(sonic_debug
, "i");
614 MODULE_PARM_DESC(sonic_debug
, "macsonic debug level (1-4)");
619 dev_macsonic
= macsonic_probe(-1);
620 if (IS_ERR(dev_macsonic
)) {
621 printk(KERN_WARNING
"macsonic.c: No card found\n");
622 return PTR_ERR(dev_macsonic
);
630 unregister_netdev(dev_macsonic
);
631 kfree(dev_macsonic
->priv
);
632 free_netdev(dev_macsonic
);
637 #define vdma_alloc(foo, bar) ((u32)foo)
638 #define vdma_free(baz)
639 #define sonic_chiptomem(bat) (bat)
640 #define PHYSADDR(quux) (quux)
641 #define CPHYSADDR(quux) (quux)
643 #define sonic_request_irq request_irq
644 #define sonic_free_irq free_irq
650 * 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"
652 * kept-new-versions: 5