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@sammy.net: Updated driver for 2.4 kernels, fixed problems
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/ctype.h>
32 #include <linux/fcntl.h>
33 #include <linux/interrupt.h>
34 #include <linux/ptrace.h>
35 #include <linux/init.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/delay.h>
41 #include <linux/nubus.h>
42 #include <asm/bootinfo.h>
43 #include <asm/system.h>
44 #include <asm/bitops.h>
45 #include <asm/pgtable.h>
46 #include <asm/segment.h>
48 #include <asm/hwtest.h>
50 #include <asm/macintosh.h>
51 #include <asm/macints.h>
52 #include <asm/mac_via.h>
53 #include <asm/pgalloc.h>
55 #include <linux/errno.h>
57 #include <linux/netdevice.h>
58 #include <linux/etherdevice.h>
59 #include <linux/skbuff.h>
60 #include <linux/module.h>
62 #define SREGS_PAD(n) u16 n;
66 #define SONIC_READ(reg) \
67 nubus_readl(base_addr+(reg))
68 #define SONIC_WRITE(reg,val) \
69 nubus_writel((val), base_addr+(reg))
70 #define sonic_read(dev, reg) \
71 nubus_readl((dev)->base_addr+(reg))
72 #define sonic_write(dev, reg, val) \
73 nubus_writel((val), (dev)->base_addr+(reg))
76 static int sonic_debug;
77 static int sonic_version_printed;
79 static int reg_offset;
81 extern int macsonic_probe(struct net_device* dev);
82 extern int mac_onboard_sonic_probe(struct net_device* dev);
83 extern int mac_nubus_sonic_probe(struct net_device* dev);
85 /* For onboard SONIC */
86 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
87 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
97 /* For the built-in SONIC in the Duo Dock */
98 #define DUODOCK_SONIC_REGISTERS 0xe10000
99 #define DUODOCK_SONIC_PROM_BASE 0xe12000
101 /* For Apple-style NuBus SONIC */
102 #define APPLE_SONIC_REGISTERS 0
103 #define APPLE_SONIC_PROM_BASE 0x40000
105 /* Daynalink LC SONIC */
106 #define DAYNALINK_PROM_BASE 0x400000
108 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
109 #define DAYNA_SONIC_REGISTERS 0x180000
110 /* This is what OpenBSD says. However, this is definitely in NuBus
111 ROM space so we should be able to get it by walking the NuBus
112 resource directories */
113 #define DAYNA_SONIC_MAC_ADDR 0xffe004
115 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
117 int __init macsonic_probe(struct net_device* dev)
121 /* This will catch fatal stuff like -ENOMEM as well as success */
122 if ((rv = mac_onboard_sonic_probe(dev)) != -ENODEV)
124 return mac_nubus_sonic_probe(dev);
128 * For reversing the PROM address
131 static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14,
132 1, 9, 5, 13, 3, 11, 7, 15};
134 static inline void bit_reverse_addr(unsigned char addr[6])
138 for(i = 0; i < 6; i++)
139 addr[i] = ((nibbletab[addr[i] & 0xf] << 4) |
140 nibbletab[(addr[i] >> 4) &0xf]);
143 int __init macsonic_init(struct net_device* dev)
145 struct sonic_local* lp;
148 /* Allocate the entire chunk of memory for the descriptors.
149 Note that this cannot cross a 64K boundary. */
150 for (i = 0; i < 20; i++) {
151 unsigned long desc_base, desc_top;
152 if((lp = kmalloc(sizeof(struct sonic_local), GFP_KERNEL | GFP_DMA)) == NULL) {
153 printk(KERN_ERR "%s: couldn't allocate descriptor buffers\n", dev->name);
157 desc_base = (unsigned long) lp;
158 desc_top = desc_base + sizeof(struct sonic_local);
159 if ((desc_top & 0xffff) >= (desc_base & 0xffff))
161 /* Hmm. try again (FIXME: does this actually work?) */
164 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
165 dev->name, desc_base, desc_top);
169 printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n",
177 /* this code is only here as a curiousity... mainly, where the
178 fuck did SONIC_BUS_SCALE come from, and what was it supposed
179 to do? the normal allocation works great for 32 bit stuffs.. */
181 /* Now set up the pointers to point to the appropriate places */
182 lp->cda = lp->sonic_desc;
183 lp->tda = lp->cda + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode));
184 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
185 * SONIC_BUS_SCALE(lp->dma_bitmode));
186 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
187 * SONIC_BUS_SCALE(lp->dma_bitmode));
191 memset(lp, 0, sizeof(struct sonic_local));
193 lp->cda_laddr = (unsigned int)&(lp->cda);
194 lp->tda_laddr = (unsigned int)lp->tda;
195 lp->rra_laddr = (unsigned int)lp->rra;
196 lp->rda_laddr = (unsigned int)lp->rda;
198 /* FIXME, maybe we should use skbs */
199 if ((lp->rba = (char *)
200 kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL | GFP_DMA)) == NULL) {
201 printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name);
205 lp->rba_laddr = (unsigned int)lp->rba;
210 /* almost always 12*4096, but let's not take chances */
211 rs = ((SONIC_NUM_RRS * SONIC_RBSIZE + 4095) / 4096) * 4096;
212 /* almost always under a page, but let's not take chances */
213 ds = ((sizeof(struct sonic_local) + 4095) / 4096) * 4096;
214 kernel_set_cachemode(lp->rba, rs, IOMAP_NOCACHE_SER);
215 kernel_set_cachemode(lp, ds, IOMAP_NOCACHE_SER);
222 dev->open = sonic_open;
223 dev->stop = sonic_close;
224 dev->hard_start_xmit = sonic_send_packet;
225 dev->get_stats = sonic_get_stats;
226 dev->set_multicast_list = &sonic_multicast_list;
229 * clear tally counter
231 sonic_write(dev, SONIC_CRCT, 0xffff);
232 sonic_write(dev, SONIC_FAET, 0xffff);
233 sonic_write(dev, SONIC_MPT, 0xffff);
235 /* Fill in the fields of the device structure with ethernet values. */
240 int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev)
242 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
245 /* On NuBus boards we can sometimes look in the ROM resources.
246 No such luck for comm-slot/onboard. */
247 for(i = 0; i < 6; i++)
248 dev->dev_addr[i] = SONIC_READ_PROM(i);
250 /* Most of the time, the address is bit-reversed. The NetBSD
251 source has a rather long and detailed historical account of
253 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
254 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
255 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
256 bit_reverse_addr(dev->dev_addr);
260 /* If we still have what seems to be a bogus address, we'll
261 look in the CAM. The top entry should be ours. */
262 /* Danger! This only works if MacOS has already initialized
264 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
265 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
266 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
270 printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n");
272 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
273 sonic_write(dev, SONIC_CEP, 15);
275 val = sonic_read(dev, SONIC_CAP2);
276 dev->dev_addr[5] = val >> 8;
277 dev->dev_addr[4] = val & 0xff;
278 val = sonic_read(dev, SONIC_CAP1);
279 dev->dev_addr[3] = val >> 8;
280 dev->dev_addr[2] = val & 0xff;
281 val = sonic_read(dev, SONIC_CAP0);
282 dev->dev_addr[1] = val >> 8;
283 dev->dev_addr[0] = val & 0xff;
285 printk(KERN_INFO "HW Address from CAM 15: ");
286 for (i = 0; i < 6; i++) {
287 printk("%2.2x", dev->dev_addr[i]);
294 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
295 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
296 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
299 * Still nonsense ... messed up someplace!
301 printk(KERN_ERR "macsonic: ERROR (INVALID MAC)\n");
306 int __init mac_onboard_sonic_probe(struct net_device* dev)
309 static int once_is_more_than_enough;
313 if (once_is_more_than_enough)
315 once_is_more_than_enough = 1;
320 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
322 if (macintosh_config->ether_type != MAC_ETHER_SONIC)
328 /* Bogus probing, on the models which may or may not have
329 Ethernet (BTW, the Ethernet *is* always at the same
330 address, and nothing else lives there, at least if Apple's
331 documentation is to be believed) */
332 if (macintosh_config->ident == MAC_MODEL_Q630 ||
333 macintosh_config->ident == MAC_MODEL_P588 ||
334 macintosh_config->ident == MAC_MODEL_C610) {
340 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
341 restore_flags(flags);
352 dev = init_etherdev(dev, sizeof(struct sonic_local));
355 /* methinks this will always be true but better safe than sorry */
356 if (dev->priv == NULL) {
357 dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
362 dev = init_etherdev(NULL, sizeof(struct sonic_local));
369 printk("%s: warning! sonic entering with priv already allocated!\n",
371 printk("%s: discarding, will attempt to reallocate\n", dev->name);
375 /* Danger! My arms are flailing wildly! You *must* set this
376 before using sonic_read() */
378 dev->base_addr = ONBOARD_SONIC_REGISTERS;
380 dev->irq = IRQ_AUTO_3;
382 dev->irq = IRQ_NUBUS_9;
384 if (!sonic_version_printed) {
385 printk(KERN_INFO "%s", version);
386 sonic_version_printed = 1;
388 printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
389 dev->name, dev->base_addr);
391 /* Now do a song and dance routine in an attempt to determine
394 /* The PowerBook's SONIC is 16 bit always. */
395 if (macintosh_config->ident == MAC_MODEL_PB520) {
398 } else if (macintosh_config->ident == MAC_MODEL_C610) {
402 /* Some of the comm-slot cards are 16 bit. But some
403 of them are not. The 32-bit cards use offset 2 and
404 pad with zeroes or sometimes ones (I think...)
405 Therefore, if we try offset 0 and get a silicon
406 revision of 0, we assume 16 bit. */
409 /* Technically this is not necessary since we zeroed
413 sr = sonic_read(dev, SONIC_SR);
414 if (sr == 0 || sr == 0xffff) {
416 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
417 sr = sonic_read(dev, SONIC_SR);
422 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
423 dev->name, sr, dma_bitmode?32:16, reg_offset);
427 /* this carries my sincere apologies -- by the time I got to updating
428 the driver, support for "reg_offsets" appeares nowhere in the sonic
429 code, going back for over a year. Fortunately, my Mac does't seem
430 to use whatever this was.
432 If you know how this is supposed to be implemented, either fix it,
433 or contact me (sammy@sammy.net) to explain what it is. --Sam */
436 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
440 /* Software reset, then initialize control registers. */
441 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
442 sonic_write(dev, SONIC_DCR, SONIC_DCR_BMS |
443 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_EXBUS |
444 (dma_bitmode ? SONIC_DCR_DW : 0));
446 /* This *must* be written back to in order to restore the
447 extended programmable output bits */
448 sonic_write(dev, SONIC_DCR2, 0);
450 /* Clear *and* disable interrupts to be on the safe side */
451 sonic_write(dev, SONIC_ISR,0x7fff);
452 sonic_write(dev, SONIC_IMR,0);
454 /* Now look for the MAC address. */
455 if (mac_onboard_sonic_ethernet_addr(dev) != 0)
458 printk(KERN_INFO "MAC ");
459 for (i = 0; i < 6; i++) {
460 printk("%2.2x", dev->dev_addr[i]);
465 printk(" IRQ %d\n", dev->irq);
467 /* Shared init code */
468 return macsonic_init(dev);
471 int __init mac_nubus_sonic_ethernet_addr(struct net_device* dev,
472 unsigned long prom_addr,
476 for(i = 0; i < 6; i++)
477 dev->dev_addr[i] = SONIC_READ_PROM(i);
478 /* For now we are going to assume that they're all bit-reversed */
479 bit_reverse_addr(dev->dev_addr);
484 int __init macsonic_ident(struct nubus_dev* ndev)
486 if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
487 ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
488 return MACSONIC_DAYNALINK;
489 if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
490 ndev->dr_sw == NUBUS_DRSW_APPLE) {
491 /* There has to be a better way to do this... */
492 if (strstr(ndev->board->name, "DuoDock"))
493 return MACSONIC_DUODOCK;
495 return MACSONIC_APPLE;
500 int __init mac_nubus_sonic_probe(struct net_device* dev)
503 struct nubus_dev* ndev = NULL;
504 struct sonic_local* lp;
505 unsigned long base_addr, prom_addr;
511 /* Find the first SONIC that hasn't been initialized already */
512 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
513 NUBUS_TYPE_ETHERNET, ndev)) != NULL)
515 /* Have we seen it already? */
516 if (slots & (1<<ndev->board->slot))
518 slots |= 1<<ndev->board->slot;
520 /* Is it one of ours? */
521 if ((id = macsonic_ident(ndev)) != -1)
529 case MACSONIC_DUODOCK:
530 base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
531 prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
532 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1
538 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
539 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
540 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
544 case MACSONIC_APPLE16:
545 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
546 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
547 sonic_dcr = SONIC_DCR_EXBUS
548 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0
549 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
553 case MACSONIC_DAYNALINK:
554 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
555 prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
556 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0
557 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
562 base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
563 prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
564 sonic_dcr = SONIC_DCR_BMS
565 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
570 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
575 dev = init_etherdev(dev, sizeof(struct sonic_local));
578 /* methinks this will always be true but better safe than sorry */
579 if (dev->priv == NULL) {
580 dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
581 if (!dev->priv) /* FIXME: kfree dev if necessary */
585 dev = init_etherdev(NULL, sizeof(struct sonic_local));
591 lp = (struct sonic_local*) dev->priv;
592 memset(lp, 0, sizeof(struct sonic_local));
593 /* Danger! My arms are flailing wildly! You *must* set this
594 before using sonic_read() */
595 dev->base_addr = base_addr;
596 dev->irq = SLOT2IRQ(ndev->board->slot);
598 if (!sonic_version_printed) {
599 printk(KERN_INFO "%s", version);
600 sonic_version_printed = 1;
602 printk(KERN_INFO "%s: %s in slot %X\n",
603 dev->name, ndev->board->name, ndev->board->slot);
604 printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
605 dev->name, sonic_read(dev, SONIC_SR), dma_bitmode?32:16, reg_offset);
608 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
612 /* Software reset, then initialize control registers. */
613 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
614 sonic_write(dev, SONIC_DCR, sonic_dcr
615 | (dma_bitmode ? SONIC_DCR_DW : 0));
617 /* Clear *and* disable interrupts to be on the safe side */
618 sonic_write(dev, SONIC_ISR,0x7fff);
619 sonic_write(dev, SONIC_IMR,0);
621 /* Now look for the MAC address. */
622 if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
625 printk(KERN_INFO "MAC ");
626 for (i = 0; i < 6; i++) {
627 printk("%2.2x", dev->dev_addr[i]);
631 printk(" IRQ %d\n", dev->irq);
633 /* Shared init code */
634 return macsonic_init(dev);
638 static char namespace[16] = "";
639 static struct net_device dev_macsonic;
641 MODULE_PARM(sonic_debug, "i");
642 MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
643 MODULE_LICENSE("GPL");
650 dev_macsonic.name = namespace;
651 dev_macsonic.init = macsonic_probe;
653 if (register_netdev(&dev_macsonic) != 0) {
654 printk(KERN_WARNING "macsonic.c: No card found\n");
663 if (dev_macsonic.priv != NULL) {
664 unregister_netdev(&dev_macsonic);
665 kfree(dev_macsonic.priv);
666 dev_macsonic.priv = NULL;
672 #define vdma_alloc(foo, bar) ((u32)foo)
673 #define vdma_free(baz)
674 #define sonic_chiptomem(bat) (bat)
675 #define PHYSADDR(quux) (quux)
677 #define sonic_request_irq request_irq
678 #define sonic_free_irq free_irq
684 * 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"
686 * kept-new-versions: 5