1 /*
2 * macsonic.c
3 *
4 * (C) 1998 Alan Cox
5 *
6 * Debugging Andreas Ehliar, Michael Schmitz
7 *
8 * Based on code
9 * (C) 1996 by Thomas Bogendoerfer (tsbogend@bigbug.franken.de)
10 *
11 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
13 *
14 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
15 *
16 * A driver for the Mac onboard Sonic ethernet chip.
17 *
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.
21 *
22 * 99/01/03 MSch: upgraded to version 0.92 of the core driver, fixed.
23 */
24
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/types.h>
28 #include <linux/ctype.h>
29 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
31 #include <linux/ptrace.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/in.h>
35 #include <linux/malloc.h>
36 #include <linux/string.h>
37 #include <linux/delay.h>
38 #include <linux/nubus.h>
39 #include <asm/bootinfo.h>
40 #include <asm/system.h>
41 #include <asm/bitops.h>
42 #include <asm/pgtable.h>
43 #include <asm/segment.h>
44 #include <asm/io.h>
45 #include <asm/hwtest.h>
46 #include <asm/dma.h>
47 #include <asm/macintosh.h>
48 #include <asm/macints.h>
49 #include <asm/mac_via.h>
50
51 #include <linux/errno.h>
52
53 #include <linux/netdevice.h>
54 #include <linux/etherdevice.h>
55 #include <linux/skbuff.h>
56 #include <linux/module.h>
57
58 #define SREGS_PAD(n) u16 n;
59
60 #include "sonic.h"
61
62 static int sonic_debug = 0;
63 static int sonic_version_printed = 0;
64
65 extern int macsonic_probe(struct net_device* dev);
66 extern int mac_onboard_sonic_probe(struct net_device* dev);
67 extern int mac_nubus_sonic_probe(struct net_device* dev);
68
69 /* For onboard SONIC */
70 #define ONBOARD_SONIC_REGISTERS 0x50F0A000
71 #define ONBOARD_SONIC_PROM_BASE 0x50f08000
72
73 enum macsonic_type {
74 MACSONIC_DUODOCK,
75 MACSONIC_APPLE,
76 MACSONIC_APPLE16,
77 MACSONIC_DAYNA,
78 MACSONIC_DAYNALINK
79 };
80
81 /* For the built-in SONIC in the Duo Dock */
82 #define DUODOCK_SONIC_REGISTERS 0xe10000
83 #define DUODOCK_SONIC_PROM_BASE 0xe12000
84
85 /* For Apple-style NuBus SONIC */
86 #define APPLE_SONIC_REGISTERS 0
87 #define APPLE_SONIC_PROM_BASE 0x40000
88
89 /* Daynalink LC SONIC */
90 #define DAYNALINK_PROM_BASE 0x400000
91
92 /* For Dayna-style NuBus SONIC (haven't seen one yet) */
93 #define DAYNA_SONIC_REGISTERS 0x180000
94 /* This is what OpenBSD says. However, this is definitely in NuBus
95 ROM space so we should be able to get it by walking the NuBus
96 resource directories */
97 #define DAYNA_SONIC_MAC_ADDR 0xffe004
98
99 #define SONIC_READ_PROM(addr) readb(prom_addr+addr)
100
101 int __init macsonic_probe(struct net_device* dev)
102 {
103 int rv;
104
105 /* This will catch fatal stuff like -ENOMEM as well as success */
106 if ((rv = mac_onboard_sonic_probe(dev)) != -ENODEV)
107 return rv;
108 return mac_nubus_sonic_probe(dev);
109 }
110
111 /*
112 * For reversing the PROM address
113 */
114
115 static unsigned char nibbletab[] = {0, 8, 4, 12, 2, 10, 6, 14,
116 1, 9, 5, 13, 3, 11, 7, 15};
117
118 static inline void bit_reverse_addr(unsigned char addr[6])
119 {
120 int i;
121
122 for(i = 0; i < 6; i++)
123 addr[i] = ((nibbletab[addr[i] & 0xf] << 4) |
124 nibbletab[(addr[i] >> 4) &0xf]);
125 }
126
127 int __init macsonic_init(struct net_device* dev)
128 {
129 struct sonic_local* lp = (struct sonic_local *)dev->priv;
130 int i;
131
132 /* Allocate the entire chunk of memory for the descriptors.
133 Note that this cannot cross a 64K boundary. */
134 for (i = 0; i < 20; i++) {
135 unsigned long desc_base, desc_top;
136 if ((lp->sonic_desc =
137 kmalloc(SIZEOF_SONIC_DESC
138 * SONIC_BUS_SCALE(lp->dma_bitmode), GFP_DMA)) == NULL) {
139 printk(KERN_ERR "%s: couldn't allocate descriptor buffers\n", dev->name);
140 }
141 desc_base = (unsigned long) lp->sonic_desc;
142 desc_top = desc_base + SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode);
143 if ((desc_top & 0xffff) >= (desc_base & 0xffff))
144 break;
145 /* Hmm. try again (FIXME: does this actually work?) */
146 kfree(lp->sonic_desc);
147 printk(KERN_DEBUG
148 "%s: didn't get continguous chunk [%08lx - %08lx], trying again\n",
149 dev->name, desc_base, desc_top);
150 }
151
152 if (lp->sonic_desc == NULL) {
153 printk(KERN_ERR "%s: tried 20 times to allocate descriptor buffers, giving up.\n",
154 dev->name);
155 return -ENOMEM;
156 }
157
158 /* Now set up the pointers to point to the appropriate places */
159 lp->cda = lp->sonic_desc;
160 lp->tda = lp->cda + (SIZEOF_SONIC_CDA * SONIC_BUS_SCALE(lp->dma_bitmode));
161 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
162 * SONIC_BUS_SCALE(lp->dma_bitmode));
163 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
164 * SONIC_BUS_SCALE(lp->dma_bitmode));
165
166 /* FIXME, maybe we should use skbs */
167 if ((lp->rba = (char *)
168 kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_DMA)) == NULL) {
169 printk(KERN_ERR "%s: couldn't allocate receive buffers\n", dev->name);
170 return -ENOMEM;
171 }
172
173 {
174 int rs, ds;
175
176 /* almost always 12*4096, but let's not take chances */
177 rs = ((SONIC_NUM_RRS * SONIC_RBSIZE + 4095) / 4096) * 4096;
178 /* almost always under a page, but let's not take chances */
179 ds = ((SIZEOF_SONIC_DESC + 4095) / 4096) * 4096;
180 kernel_set_cachemode(lp->rba, rs, IOMAP_NOCACHE_SER);
181 kernel_set_cachemode(lp->sonic_desc, ds, IOMAP_NOCACHE_SER);
182 }
183
184 #if 0
185 flush_cache_all();
186 #endif
187
188 dev->open = sonic_open;
189 dev->stop = sonic_close;
190 dev->hard_start_xmit = sonic_send_packet;
191 dev->get_stats = sonic_get_stats;
192 dev->set_multicast_list = &sonic_multicast_list;
193
194 /*
195 * clear tally counter
196 */
197 sonic_write(dev, SONIC_CRCT, 0xffff);
198 sonic_write(dev, SONIC_FAET, 0xffff);
199 sonic_write(dev, SONIC_MPT, 0xffff);
200
201 /* Fill in the fields of the device structure with ethernet values. */
202 ether_setup(dev);
203 return 0;
204 }
205
206 int __init mac_onboard_sonic_ethernet_addr(struct net_device* dev)
207 {
208 const int prom_addr = ONBOARD_SONIC_PROM_BASE;
209 int i;
210
211 /* On NuBus boards we can sometimes look in the ROM resources.
212 No such luck for comm-slot/onboard. */
213 for(i = 0; i < 6; i++)
214 dev->dev_addr[i] = SONIC_READ_PROM(i);
215
216 /* Most of the time, the address is bit-reversed. The NetBSD
217 source has a rather long and detailed historical account of
218 why this is so. */
219 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
220 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
221 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
222 bit_reverse_addr(dev->dev_addr);
223 else
224 return 0;
225
226 /* If we still have what seems to be a bogus address, we'll
227 look in the CAM. The top entry should be ours. */
228 /* Danger! This only works if MacOS has already initialized
229 the card... */
230 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
231 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
232 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
233 {
234 unsigned short val;
235
236 printk(KERN_INFO "macsonic: PROM seems to be wrong, trying CAM entry 15\n");
237
238 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
239 sonic_write(dev, SONIC_CEP, 15);
240
241 val = sonic_read(dev, SONIC_CAP2);
242 dev->dev_addr[5] = val >> 8;
243 dev->dev_addr[4] = val & 0xff;
244 val = sonic_read(dev, SONIC_CAP1);
245 dev->dev_addr[3] = val >> 8;
246 dev->dev_addr[2] = val & 0xff;
247 val = sonic_read(dev, SONIC_CAP0);
248 dev->dev_addr[1] = val >> 8;
249 dev->dev_addr[0] = val & 0xff;
250
251 printk(KERN_INFO "HW Address from CAM 15: ");
252 for (i = 0; i < 6; i++) {
253 printk("%2.2x", dev->dev_addr[i]);
254 if (i < 5)
255 printk(":");
256 }
257 printk("\n");
258 } else return 0;
259
260 if (memcmp(dev->dev_addr, "\x08\x00\x07", 3) &&
261 memcmp(dev->dev_addr, "\x00\xA0\x40", 3) &&
262 memcmp(dev->dev_addr, "\x00\x05\x02", 3))
263 {
264 /*
265 * Still nonsense ... messed up someplace!
266 */
267 printk(KERN_ERR "macsonic: ERROR (INVALID MAC)\n");
268 return -EIO;
269 } else return 0;
270 }
271
272 int __init mac_onboard_sonic_probe(struct net_device* dev)
273 {
274 /* Bwahahaha */
275 static int once_is_more_than_enough = 0;
276 struct sonic_local* lp;
277 int i;
278
279 if (once_is_more_than_enough)
280 return -ENODEV;
281 once_is_more_than_enough = 1;
282
283 if (!MACH_IS_MAC)
284 return -ENODEV;
285
286 printk(KERN_INFO "Checking for internal Macintosh ethernet (SONIC).. ");
287
288 if (macintosh_config->ether_type != MAC_ETHER_SONIC)
289 {
290 printk("none.\n");
291 return -ENODEV;
292 }
293
294 /* Bogus probing, on the models which may or may not have
295 Ethernet (BTW, the Ethernet *is* always at the same
296 address, and nothing else lives there, at least if Apple's
297 documentation is to be believed) */
298 if (macintosh_config->ident == MAC_MODEL_Q630 ||
299 macintosh_config->ident == MAC_MODEL_P588 ||
300 macintosh_config->ident == MAC_MODEL_C610) {
301 unsigned long flags;
302 int card_present;
303
304 save_flags(flags);
305 cli();
306 card_present = hwreg_present((void*)ONBOARD_SONIC_REGISTERS);
307 restore_flags(flags);
308
309 if (!card_present) {
310 printk("none.\n");
311 return -ENODEV;
312 }
313 }
314
315 printk("yes\n");
316
317 if (dev) {
318 dev = init_etherdev(dev, sizeof(struct sonic_local));
319 /* methinks this will always be true but better safe than sorry */
320 if (dev->priv == NULL)
321 dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
322 } else {
323 dev = init_etherdev(NULL, sizeof(struct sonic_local));
324 }
325
326 if (dev == NULL)
327 return -ENOMEM;
328
329 lp = (struct sonic_local*) dev->priv;
330 memset(lp, 0, sizeof(struct sonic_local));
331 /* Danger! My arms are flailing wildly! You *must* set this
332 before using sonic_read() */
333
334 dev->base_addr = ONBOARD_SONIC_REGISTERS;
335 if (via_alt_mapping)
336 dev->irq = IRQ_AUTO_3;
337 else
338 dev->irq = IRQ_NUBUS_9;
339
340 if (!sonic_version_printed) {
341 printk(KERN_INFO "%s", version);
342 sonic_version_printed = 1;
343 }
344 printk(KERN_INFO "%s: onboard / comm-slot SONIC at 0x%08lx\n",
345 dev->name, dev->base_addr);
346
347 /* Now do a song and dance routine in an attempt to determine
348 the bus width */
349
350 /* The PowerBook's SONIC is 16 bit always. */
351 if (macintosh_config->ident == MAC_MODEL_PB520) {
352 lp->reg_offset = 0;
353 lp->dma_bitmode = 0;
354 } else {
355 /* Some of the comm-slot cards are 16 bit. But some
356 of them are not. The 32-bit cards use offset 2 and
357 pad with zeroes or sometimes ones (I think...)
358 Therefore, if we try offset 0 and get a silicon
359 revision of 0, we assume 16 bit. */
360 int sr;
361
362 /* Technically this is not necessary since we zeroed
363 it above */
364 lp->reg_offset = 0;
365 lp->dma_bitmode = 0;
366 sr = sonic_read(dev, SONIC_SR);
367 if (sr == 0 || sr == 0xffff) {
368 lp->reg_offset = 2;
369 /* 83932 is 0x0004, 83934 is 0x0100 or 0x0101 */
370 sr = sonic_read(dev, SONIC_SR);
371 lp->dma_bitmode = 1;
372
373 }
374 printk(KERN_INFO
375 "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
376 dev->name, sr, lp->dma_bitmode?32:16, lp->reg_offset);
377 }
378
379
380 /* Software reset, then initialize control registers. */
381 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
382 sonic_write(dev, SONIC_DCR, SONIC_DCR_BMS |
383 SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_EXBUS |
384 (lp->dma_bitmode ? SONIC_DCR_DW : 0));
385 /* This *must* be written back to in order to restore the
386 extended programmable output bits */
387 sonic_write(dev, SONIC_DCR2, 0);
388
389 /* Clear *and* disable interrupts to be on the safe side */
390 sonic_write(dev, SONIC_ISR,0x7fff);
391 sonic_write(dev, SONIC_IMR,0);
392
393 /* Now look for the MAC address. */
394 if (mac_onboard_sonic_ethernet_addr(dev) != 0)
395 return -ENODEV;
396
397 printk(KERN_INFO "MAC ");
398 for (i = 0; i < 6; i++) {
399 printk("%2.2x", dev->dev_addr[i]);
400 if (i < 5)
401 printk(":");
402 }
403
404 printk(" IRQ %d\n", dev->irq);
405
406 /* Shared init code */
407 return macsonic_init(dev);
408 }
409
410 int __init mac_nubus_sonic_ethernet_addr(struct net_device* dev,
411 unsigned long prom_addr,
412 int id)
413 {
414 int i;
415 for(i = 0; i < 6; i++)
416 dev->dev_addr[i] = SONIC_READ_PROM(i);
417 /* For now we are going to assume that they're all bit-reversed */
418 bit_reverse_addr(dev->dev_addr);
419
420 return 0;
421 }
422
423 int __init macsonic_ident(struct nubus_dev* ndev)
424 {
425 if (ndev->dr_hw == NUBUS_DRHW_ASANTE_LC &&
426 ndev->dr_sw == NUBUS_DRSW_SONIC_LC)
427 return MACSONIC_DAYNALINK;
428 if (ndev->dr_hw == NUBUS_DRHW_SONIC &&
429 ndev->dr_sw == NUBUS_DRSW_APPLE) {
430 /* There has to be a better way to do this... */
431 if (strstr(ndev->board->name, "DuoDock"))
432 return MACSONIC_DUODOCK;
433 else
434 return MACSONIC_APPLE;
435 }
436 return -1;
437 }
438
439 int __init mac_nubus_sonic_probe(struct net_device* dev)
440 {
441 static int slots = 0;
442 struct nubus_dev* ndev = NULL;
443 struct sonic_local* lp;
444 unsigned long base_addr, prom_addr;
445 u16 sonic_dcr;
446 int id;
447 int i;
448 int reg_offset, dma_bitmode;
449
450 /* Find the first SONIC that hasn't been initialized already */
451 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK,
452 NUBUS_TYPE_ETHERNET, ndev)) != NULL)
453 {
454 /* Have we seen it already? */
455 if (slots & (1<<ndev->board->slot))
456 continue;
457 slots |= 1<<ndev->board->slot;
458
459 /* Is it one of ours? */
460 if ((id = macsonic_ident(ndev)) != -1)
461 break;
462 }
463
464 if (ndev == NULL)
465 return -ENODEV;
466
467 switch (id) {
468 case MACSONIC_DUODOCK:
469 base_addr = ndev->board->slot_addr + DUODOCK_SONIC_REGISTERS;
470 prom_addr = ndev->board->slot_addr + DUODOCK_SONIC_PROM_BASE;
471 sonic_dcr = SONIC_DCR_EXBUS | SONIC_DCR_RFT0 | SONIC_DCR_RFT1
472 | SONIC_DCR_TFT0;
473 reg_offset = 2;
474 dma_bitmode = 1;
475 break;
476 case MACSONIC_APPLE:
477 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
478 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
479 sonic_dcr = SONIC_DCR_BMS | SONIC_DCR_RFT1 | SONIC_DCR_TFT0;
480 reg_offset = 0;
481 dma_bitmode = 1;
482 break;
483 case MACSONIC_APPLE16:
484 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
485 prom_addr = ndev->board->slot_addr + APPLE_SONIC_PROM_BASE;
486 sonic_dcr = SONIC_DCR_EXBUS
487 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0
488 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
489 reg_offset = 0;
490 dma_bitmode = 0;
491 break;
492 case MACSONIC_DAYNALINK:
493 base_addr = ndev->board->slot_addr + APPLE_SONIC_REGISTERS;
494 prom_addr = ndev->board->slot_addr + DAYNALINK_PROM_BASE;
495 sonic_dcr = SONIC_DCR_RFT1 | SONIC_DCR_TFT0
496 | SONIC_DCR_PO1 | SONIC_DCR_BMS;
497 reg_offset = 0;
498 dma_bitmode = 0;
499 break;
500 case MACSONIC_DAYNA:
501 base_addr = ndev->board->slot_addr + DAYNA_SONIC_REGISTERS;
502 prom_addr = ndev->board->slot_addr + DAYNA_SONIC_MAC_ADDR;
503 sonic_dcr = SONIC_DCR_BMS
504 | SONIC_DCR_RFT1 | SONIC_DCR_TFT0 | SONIC_DCR_PO1;
505 reg_offset = 0;
506 dma_bitmode = 0;
507 break;
508 default:
509 printk(KERN_ERR "macsonic: WTF, id is %d\n", id);
510 return -ENODEV;
511 }
512
513 if (dev) {
514 dev = init_etherdev(dev, sizeof(struct sonic_local));
515 /* methinks this will always be true but better safe than sorry */
516 if (dev->priv == NULL)
517 dev->priv = kmalloc(sizeof(struct sonic_local), GFP_KERNEL);
518 } else {
519 dev = init_etherdev(NULL, sizeof(struct sonic_local));
520 }
521
522 if (dev == NULL)
523 return -ENOMEM;
524
525 lp = (struct sonic_local*) dev->priv;
526 memset(lp, 0, sizeof(struct sonic_local));
527 /* Danger! My arms are flailing wildly! You *must* set this
528 before using sonic_read() */
529 lp->reg_offset = reg_offset;
530 lp->dma_bitmode = dma_bitmode;
531 dev->base_addr = base_addr;
532 dev->irq = SLOT2IRQ(ndev->board->slot);
533
534 if (!sonic_version_printed) {
535 printk(KERN_INFO "%s", version);
536 sonic_version_printed = 1;
537 }
538 printk(KERN_INFO "%s: %s in slot %X\n",
539 dev->name, ndev->board->name, ndev->board->slot);
540 printk(KERN_INFO "%s: revision 0x%04x, using %d bit DMA and register offset %d\n",
541 dev->name, sonic_read(dev, SONIC_SR), dma_bitmode?32:16, reg_offset);
542
543 /* Software reset, then initialize control registers. */
544 sonic_write(dev, SONIC_CMD, SONIC_CR_RST);
545 sonic_write(dev, SONIC_DCR, sonic_dcr
546 | (dma_bitmode ? SONIC_DCR_DW : 0));
547
548 /* Clear *and* disable interrupts to be on the safe side */
549 sonic_write(dev, SONIC_ISR,0x7fff);
550 sonic_write(dev, SONIC_IMR,0);
551
552 /* Now look for the MAC address. */
553 if (mac_nubus_sonic_ethernet_addr(dev, prom_addr, id) != 0)
554 return -ENODEV;
555
556 printk(KERN_INFO "MAC ");
557 for (i = 0; i < 6; i++) {
558 printk("%2.2x", dev->dev_addr[i]);
559 if (i < 5)
560 printk(":");
561 }
562 printk(" IRQ %d\n", dev->irq);
563
564 /* Shared init code */
565 return macsonic_init(dev);
566 }
567
568 #ifdef MODULE
569 static char namespace[16] = "";
570 static struct net_device dev_macsonic = {
571 NULL,
572 0, 0, 0, 0,
573 0, 0,
574 0, 0, 0, NULL, NULL };
575
576 MODULE_PARM(sonic_debug, "i");
577
578 EXPORT_NO_SYMBOLS;
579
580 int
581 init_module(void)
582 {
583 dev_macsonic.name = namespace;
584 dev_macsonic.init = macsonic_probe;
585
586 if (register_netdev(&dev_macsonic) != 0) {
587 printk(KERN_WARNING "macsonic.c: No card found\n");
588 return -ENXIO;
589 }
590 return 0;
591 }
592
593 void
594 cleanup_module(void)
595 {
596 if (dev_macsonic.priv != NULL) {
597 unregister_netdev(&dev_macsonic);
598 kfree(dev_macsonic.priv);
599 dev_macsonic.priv = NULL;
600 }
601 }
602 #endif /* MODULE */
603
604
605 #define vdma_alloc(foo, bar) ((u32)foo)
606 #define vdma_free(baz)
607 #define sonic_chiptomem(bat) (bat)
608 #define PHYSADDR(quux) (quux)
609
610 #include "sonic.c"
611
612 /*
613 * Local variables:
614 * 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"
615 * version-control: t
616 * kept-new-versions: 5
617 * c-indent-level: 8
618 * tab-width: 8
619 * End:
620 *
621 */
622
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.