MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / macsonic.c
blobe67bf67f122f0da1fc05021b1fd730df24a939c7
1 /*
2 * macsonic.c
4 * (C) 1998 Alan Cox
6 * Debugging Andreas Ehliar, Michael Schmitz
8 * Based on code
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
25 * on centris.
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>
35 #include <linux/in.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>
50 #include <asm/io.h>
51 #include <asm/hwtest.h>
52 #include <asm/dma.h>
53 #include <asm/macintosh.h>
54 #include <asm/macints.h>
55 #include <asm/mac_via.h>
57 #define SREGS_PAD(n) u16 n;
59 #include "sonic.h"
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
83 enum macsonic_type {
84 MACSONIC_DUODOCK,
85 MACSONIC_APPLE,
86 MACSONIC_APPLE16,
87 MACSONIC_DAYNA,
88 MACSONIC_DAYNALINK
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);
114 int err;
116 if (!dev)
117 return ERR_PTR(-ENOMEM);
119 if (unit >= 0)
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);
126 if (err == 0)
127 goto found;
128 if (err != -ENODEV)
129 goto out;
130 err = mac_nubus_sonic_probe(dev);
131 if (err)
132 goto out;
133 found:
134 err = register_netdev(dev);
135 if (err)
136 goto out1;
137 return dev;
138 out1:
139 kfree(dev->priv);
140 out:
141 free_netdev(dev);
142 return ERR_PTR(err);
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])
154 int i;
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;
164 int i;
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);
172 return -ENOMEM;
175 desc_base = (unsigned long) lp;
176 desc_top = desc_base + sizeof(struct sonic_local);
177 if ((desc_top & 0xffff) >= (desc_base & 0xffff))
178 break;
179 /* Hmm. try again (FIXME: does this actually work?) */
180 kfree(lp);
181 printk(KERN_DEBUG
182 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
183 dev->name, desc_base, desc_top);
186 if (lp == NULL) {
187 printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n",
188 dev->name);
189 return -ENOMEM;
192 dev->priv = lp;
194 #if 0
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));
207 #endif
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);
220 dev->priv = NULL;
221 kfree(lp);
222 return -ENOMEM;
225 lp->rba_laddr = (unsigned int)lp->rba;
228 int rs, ds;
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);
238 #if 0
239 flush_cache_all();
240 #endif
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);
255 return 0;
258 int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev)
260 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
261 int i;
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
270 why this is so. */
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);
275 else
276 return 0;
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
281 the card... */
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))
286 unsigned short val;
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]);
306 if (i < 5)
307 printk(":");
309 printk("\n");
310 } else return 0;
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");
320 return -EIO;
321 } else return 0;
324 int __init mac_onboard_sonic_probe(struct net_device* dev)
326 /* Bwahahaha */
327 static int once_is_more_than_enough;
328 int i;
329 int dma_bitmode;
331 if (once_is_more_than_enough)
332 return -ENODEV;
333 once_is_more_than_enough = 1;
335 if (!MACH_IS_MAC)
336 return -ENODEV;
338 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
340 if (macintosh_config->ether_type != MAC_ETHER_SONIC)
342 printk("none.\n");
343 return -ENODEV;
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) {
353 unsigned long flags;
354 int card_present;
356 local_irq_save(flags);
357 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
358 local_irq_restore(flags);
360 if (!card_present) {
361 printk("none.\n");
362 return -ENODEV;
366 printk("yes\n");
368 /* Danger! My arms are flailing wildly! You *must* set this
369 before using sonic_read() */
371 dev->base_addr = ONBOARD_SONIC_REGISTERS;
372 if (via_alt_mapping)
373 dev->irq = IRQ_AUTO_3;
374 else
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
385 the bus width */
387 /* The PowerBook's SONIC is 16 bit always. */
388 if (macintosh_config->ident == MAC_MODEL_PB520) {
389 reg_offset = 0;
390 dma_bitmode = 0;
391 } else if (macintosh_config->ident == MAC_MODEL_C610) {
392 reg_offset = 0;
393 dma_bitmode = 1;
394 } else {
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. */
400 int sr;
402 /* Technically this is not necessary since we zeroed
403 it above */
404 reg_offset = 0;
405 dma_bitmode = 0;
406 sr = sonic_read(dev, SONIC_SR);
407 if (sr == 0 || sr == 0xffff) {
408 reg_offset = 2;
409 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
410 sr = sonic_read(dev, SONIC_SR);
411 dma_bitmode = 1;
414 printk(KERN_INFO
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 */
428 if(reg_offset) {
429 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
430 return -ENODEV;
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)
449 return -ENODEV;
451 printk(KERN_INFO "MAC ");
452 for (i = 0; i < 6; i++) {
453 printk("%2.2x", dev->dev_addr[i]);
454 if (i < 5)
455 printk(":");
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,
466 int id)
468 int i;
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);
474 return 0;
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;
487 else
488 return MACSONIC_APPLE;
490 return -1;
493 int __init mac_nubus_sonic_probe(struct net_device* dev)
495 static int slots;
496 struct nubus_dev* ndev = NULL;
497 unsigned long base_addr, prom_addr;
498 u16 sonic_dcr;
499 int id;
500 int i;
501 int dma_bitmode;
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))
509 continue;
510 slots |= 1<<ndev->board->slot;
512 /* Is it one of ours? */
513 if ((id = macsonic_ident(ndev)) != -1)
514 break;
517 if (ndev == NULL)
518 return -ENODEV;
520 switch (id) {
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
525 | SONIC_DCR_TFT0;
526 reg_offset = 2;
527 dma_bitmode = 1;
528 break;
529 case MACSONIC_APPLE:
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;
533 reg_offset = 0;
534 dma_bitmode = 1;
535 break;
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;
542 reg_offset = 0;
543 dma_bitmode = 0;
544 break;
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;
550 reg_offset = 0;
551 dma_bitmode = 0;
552 break;
553 case MACSONIC_DAYNA:
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;
558 reg_offset = 0;
559 dma_bitmode = 0;
560 break;
561 default:
562 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
563 return -ENODEV;
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);
580 if(reg_offset) {
581 printk("%s: register offset unsupported. please fix this if you know what it is.\n", dev->name);
582 return -ENODEV;
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)
596 return -ENODEV;
598 printk(KERN_INFO "MAC ");
599 for (i = 0; i < 6; i++) {
600 printk("%2.2x", dev->dev_addr[i]);
601 if (i < 5)
602 printk(":");
604 printk(" IRQ %d\n", dev->irq);
606 /* Shared init code */
607 return macsonic_init(dev);
610 #ifdef MODULE
611 static struct net_device *dev_macsonic;
613 MODULE_PARM(sonic_debug, "i");
614 MODULE_PARM_DESC(sonic_debug, "macsonic debug level (1-4)");
617 init_module(void)
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);
624 return 0;
627 void
628 cleanup_module(void)
630 unregister_netdev(dev_macsonic);
631 kfree(dev_macsonic->priv);
632 free_netdev(dev_macsonic);
634 #endif /* MODULE */
637 #define vdma_alloc(foo, bar) ((u32)foo)
638 #define vdma_free(baz)
639 #define sonic_chiptomem(bat) (bat)
640 #define PHYSADDR(quux) (quux)
642 #define sonic_request_irq request_irq
643 #define sonic_free_irq free_irq
645 #include "sonic.c"
648 * Local variables:
649 * 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"
650 * version-control: t
651 * kept-new-versions: 5
652 * c-indent-level: 8
653 * tab-width: 8
654 * End: