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 mac_onboard_sonic_probe(struct net_device* dev);
78 extern int mac_nubus_sonic_probe(struct net_device* dev);
80 /* For onboard SONIC */
81 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
82 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
92 /* For the built-in SONIC in the Duo Dock */
93 #define DUODOCK_SONIC_REGISTERS 0xe10000
94 #define DUODOCK_SONIC_PROM_BASE 0xe12000
96 /* For Apple-style NuBus SONIC */
97 #define APPLE_SONIC_REGISTERS 0
98 #define APPLE_SONIC_PROM_BASE 0x40000
100 /* Daynalink LC SONIC */
101 #define DAYNALINK_PROM_BASE 0x400000
103 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
104 #define DAYNA_SONIC_REGISTERS 0x180000
105 /* This is what OpenBSD says. However, this is definitely in NuBus
106 ROM space so we should be able to get it by walking the NuBus
107 resource directories */
108 #define DAYNA_SONIC_MAC_ADDR 0xffe004
110 #define SONIC_READ_PROM(addr) nubus_readb(prom_addr+addr)
112 struct net_device * __init macsonic_probe(int unit)
114 struct net_device *dev = alloc_etherdev(0);
118 return ERR_PTR(-ENOMEM);
121 sprintf(dev->name, "eth%d", unit);
123 SET_MODULE_OWNER(dev);
125 /* This will catch fatal stuff like -ENOMEM as well as success */
126 err = mac_onboard_sonic_probe(dev);
131 err = mac_nubus_sonic_probe(dev);
135 err = register_netdev(dev);
147 * For reversing the PROM address
150 static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14,
151 1, 9, 5, 13, 3, 11, 7, 15};
153 static inline void bit_reverse_addr(unsigned char addr[6])
157 for(i = 0; i < 6; i++)
158 addr[i] = ((nibbletab[addr[i] & 0xf] << 4) |
159 nibbletab[(addr[i] >> 4) &0xf]);
162 int __init macsonic_init(struct net_device* dev)
164 struct sonic_local* lp = NULL;
167 /* Allocate the entire chunk of memory for the descriptors.
168 Note that this cannot cross a 64K boundary. */
169 for (i = 0; i < 20; i++) {
170 unsigned long desc_base, desc_top;
171 if((lp = kmalloc(sizeof(struct sonic_local), GFP_KERNEL | GFP_DMA)) == NULL) {
172 printk(KERN_ERR "%s: couldn't allocate descriptor buffers\n", dev->name);
176 desc_base = (unsigned long) lp;
177 desc_top = desc_base + sizeof(struct sonic_local);
178 if ((desc_top & 0xffff) >= (desc_base & 0xffff))
180 /* Hmm. try again (FIXME: does this actually work?) */
183 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
184 dev->name, desc_base, desc_top);
188 printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n",
196 /* this code is only here as a curiousity... mainly, where the
197 fuck did SONIC_BUS_SCALE come from, and what was it supposed
198 to do? the normal allocation works great for 32 bit stuffs.. */
200 /* Now set up the pointers to point to the appropriate places */
201 lp->cda = lp->sonic_desc;
202 lp->tda = lp->cda + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode));
203 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
204 * SONIC_BUS_SCALE(lp->dma_bitmode));
205 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
206 * SONIC_BUS_SCALE(lp->dma_bitmode));
210 memset(lp, 0, sizeof(struct sonic_local));
212 lp->cda_laddr = (unsigned int)&(lp->cda);
213 lp->tda_laddr = (unsigned int)lp->tda;
214 lp->rra_laddr = (unsigned int)lp->rra;
215 lp->rda_laddr = (unsigned int)lp->rda;
217 /* FIXME, maybe we should use skbs */
218 if ((lp->rba = (char *)
219 kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL | GFP_DMA)) == NULL) {
220 printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name);
226 lp->rba_laddr = (unsigned int)lp->rba;
231 /* almost always 12*4096, but let's not take chances */
232 rs = ((SONIC_NUM_RRS * SONIC_RBSIZE + 4095) / 4096) * 4096;
233 /* almost always under a page, but let's not take chances */
234 ds = ((sizeof(struct sonic_local) + 4095) / 4096) * 4096;
235 kernel_set_cachemode(lp->rba, rs, IOMAP_NOCACHE_SER);
236 kernel_set_cachemode(lp, ds, IOMAP_NOCACHE_SER);
243 dev->open = sonic_open;
244 dev->stop = sonic_close;
245 dev->hard_start_xmit = sonic_send_packet;
246 dev->get_stats = sonic_get_stats;
247 dev->set_multicast_list = &sonic_multicast_list;
250 * clear tally counter
252 sonic_write(dev, SONIC_CRCT, 0xffff);
253 sonic_write(dev, SONIC_FAET, 0xffff);
254 sonic_write(dev, SONIC_MPT, 0xffff);
259 int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev)
261 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
264 /* On NuBus boards we can sometimes look in the ROM resources.
265 No such luck for comm-slot/onboard. */
266 for(i = 0; i < 6; i++)
267 dev->dev_addr[i] = SONIC_READ_PROM(i);
269 /* Most of the time, the address is bit-reversed. The NetBSD
270 source has a rather long and detailed historical account of
272 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
273 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
274 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
275 bit_reverse_addr(dev->dev_addr);
279 /* If we still have what seems to be a bogus address, we'll
280 look in the CAM. The top entry should be ours. */
281 /* Danger! This only works if MacOS has already initialized
283 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
284 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
285 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
289 printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n");
291 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
292 sonic_write(dev, SONIC_CEP, 15);
294 val = sonic_read(dev, SONIC_CAP2);
295 dev->dev_addr[5] = val >> 8;
296 dev->dev_addr[4] = val & 0xff;
297 val = sonic_read(dev, SONIC_CAP1);
298 dev->dev_addr[3] = val >> 8;
299 dev->dev_addr[2] = val & 0xff;
300 val = sonic_read(dev, SONIC_CAP0);
301 dev->dev_addr[1] = val >> 8;
302 dev->dev_addr[0] = val & 0xff;
304 printk(KERN_INFO "HW Address from CAM 15: ");
305 for (i = 0; i < 6; i++) {
306 printk("%2.2x", dev->dev_addr[i]);
313 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
314 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
315 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
318 * Still nonsense ... messed up someplace!
320 printk(KERN_ERR "macsonic: ERROR (INVALID MAC)\n");
325 int __init mac_onboard_sonic_probe(struct net_device* dev)
328 static int once_is_more_than_enough;
332 if (once_is_more_than_enough)
334 once_is_more_than_enough = 1;
339 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
341 if (macintosh_config->ether_type != MAC_ETHER_SONIC)
347 /* Bogus probing, on the models which may or may not have
348 Ethernet (BTW, the Ethernet *is* always at the same
349 address, and nothing else lives there, at least if Apple's
350 documentation is to be believed) */
351 if (macintosh_config->ident == MAC_MODEL_Q630 ||
352 macintosh_config->ident == MAC_MODEL_P588 ||
353 macintosh_config->ident == MAC_MODEL_C610) {
357 local_irq_save(flags);
358 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
359 local_irq_restore(flags);
369 /* Danger! My arms are flailing wildly! You *must* set this
370 before using sonic_read() */
372 dev->base_addr = ONBOARD_SONIC_REGISTERS;
374 dev->irq = IRQ_AUTO_3;
376 dev->irq = IRQ_NUBUS_9;
378 if (!sonic_version_printed) {
379 printk(KERN_INFO "%s", version);
380 sonic_version_printed = 1;
382 printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
383 dev->name, dev->base_addr);
385 /* Now do a song and dance routine in an attempt to determine
388 /* The PowerBook's SONIC is 16 bit always. */
389 if (macintosh_config->ident == MAC_MODEL_PB520) {
392 } else if (macintosh_config->ident == MAC_MODEL_C610) {
396 /* Some of the comm-slot cards are 16 bit. But some
397 of them are not. The 32-bit cards use offset 2 and
398 pad with zeroes or sometimes ones (I think...)
399 Therefore, if we try offset 0 and get a silicon
400 revision of 0, we assume 16 bit. */
403 /* Technically this is not necessary since we zeroed
407 sr = sonic_read(dev, SONIC_SR);
408 if (sr == 0 || sr == 0xffff) {
410 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
411 sr = sonic_read(dev, SONIC_SR);
416 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
417 dev->name, sr, dma_bitmode?32:16, reg_offset);
421 /* this carries my sincere apologies -- by the time I got to updating
422 the driver, support for "reg_offsets" appeares nowhere in the sonic
423 code, going back for over a year. Fortunately, my Mac does't seem
424 to use whatever this was.
426 If you know how this is supposed to be implemented, either fix it,
427 or contact me (sammy@oh.verio.com) to explain what it is. --Sam */
430 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
434 /* Software reset, then initialize control registers. */
435 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
436 sonic_write(dev, SONIC_DCR, SONIC_DCR_BMS |
437 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_EXBUS |
438 (dma_bitmode ? SONIC_DCR_DW : 0));
440 /* This *must* be written back to in order to restore the
441 extended programmable output bits */
442 sonic_write(dev, SONIC_DCR2, 0);
444 /* Clear *and* disable interrupts to be on the safe side */
445 sonic_write(dev, SONIC_ISR,0x7fff);
446 sonic_write(dev, SONIC_IMR,0);
448 /* Now look for the MAC address. */
449 if (mac_onboard_sonic_ethernet_addr(dev) != 0)
452 printk(KERN_INFO "MAC ");
453 for (i = 0; i < 6; i++) {
454 printk("%2.2x", dev->dev_addr[i]);
459 printk(" IRQ %d\n", dev->irq);
461 /* Shared init code */
462 return macsonic_init(dev);
465 int __init mac_nubus_sonic_ethernet_addr(struct net_device* dev,
466 unsigned long prom_addr,
470 for(i = 0; i < 6; i++)
471 dev->dev_addr[i] = SONIC_READ_PROM(i);
472 /* For now we are going to assume that they're all bit-reversed */
473 bit_reverse_addr(dev->dev_addr);
478 int __init macsonic_ident(struct nubus_dev* ndev)
480 if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
481 ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
482 return MACSONIC_DAYNALINK;
483 if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
484 ndev->dr_sw == NUBUS_DRSW_APPLE) {
485 /* There has to be a better way to do this... */
486 if (strstr(ndev->board->name, "DuoDock"))
487 return MACSONIC_DUODOCK;
489 return MACSONIC_APPLE;
494 int __init mac_nubus_sonic_probe(struct net_device* dev)
497 struct nubus_dev* ndev = NULL;
498 unsigned long base_addr, prom_addr;
504 /* Find the first SONIC that hasn't been initialized already */
505 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
506 NUBUS_TYPE_ETHERNET, ndev)) != NULL)
508 /* Have we seen it already? */
509 if (slots & (1<<ndev->board->slot))
511 slots |= 1<<ndev->board->slot;
513 /* Is it one of ours? */
514 if ((id = macsonic_ident(ndev)) != -1)
522 case MACSONIC_DUODOCK:
523 base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
524 prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
525 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1
531 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
532 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
533 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
537 case MACSONIC_APPLE16:
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_EXBUS
541 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0
542 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
546 case MACSONIC_DAYNALINK:
547 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
548 prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
549 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0
550 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
555 base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
556 prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
557 sonic_dcr = SONIC_DCR_BMS
558 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
563 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
567 /* Danger! My arms are flailing wildly! You *must* set this
568 before using sonic_read() */
569 dev->base_addr = base_addr;
570 dev->irq = SLOT2IRQ(ndev->board->slot);
572 if (!sonic_version_printed) {
573 printk(KERN_INFO "%s", version);
574 sonic_version_printed = 1;
576 printk(KERN_INFO "%s: %s in slot %X\n",
577 dev->name, ndev->board->name, ndev->board->slot);
578 printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
579 dev->name, sonic_read(dev, SONIC_SR), dma_bitmode?32:16, reg_offset);
582 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
586 /* Software reset, then initialize control registers. */
587 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
588 sonic_write(dev, SONIC_DCR, sonic_dcr
589 | (dma_bitmode ? SONIC_DCR_DW : 0));
591 /* Clear *and* disable interrupts to be on the safe side */
592 sonic_write(dev, SONIC_ISR,0x7fff);
593 sonic_write(dev, SONIC_IMR,0);
595 /* Now look for the MAC address. */
596 if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
599 printk(KERN_INFO "MAC ");
600 for (i = 0; i < 6; i++) {
601 printk("%2.2x", dev->dev_addr[i]);
605 printk(" IRQ %d\n", dev->irq);
607 /* Shared init code */
608 return macsonic_init(dev);
612 static struct net_device *dev_macsonic;
614 MODULE_PARM(sonic_debug, "i");
615 MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
616 MODULE_LICENSE("GPL");
621 dev_macsonic = macsonic_probe(-1);
622 if (IS_ERR(dev_macsonic)) {
623 printk(KERN_WARNING "macsonic.c: No card found\n");
624 return PTR_ERR(dev_macsonic);
632 unregister_netdev(dev_macsonic);
633 kfree(dev_macsonic->priv);
634 free_netdev(dev_macsonic);
639 #define vdma_alloc(foo, bar) ((u32)foo)
640 #define vdma_free(baz)
641 #define sonic_chiptomem(bat) (bat)
642 #define PHYSADDR(quux) (quux)
644 #define sonic_request_irq request_irq
645 #define sonic_free_irq free_irq
651 * 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"
653 * kept-new-versions: 5