1 /* mac8390.c: New driver for 8390-based Nubus (or Nubus-alike)
2 Ethernet cards on Linux */
3 /* Based on the former daynaport.c driver, by Alan Cox. Some code
4 taken from or inspired by skeleton.c by Donald Becker, acenic.c by
5 Jes Sorensen, and ne2k-pci.c by Donald Becker and Paul Gortmaker.
7 This software may be used and distributed according to the terms of
8 the GNU Public License, incorporated herein by reference. */
10 /* 2000-02-28: support added for Dayna and Kinetics cards by
11 A.G.deWijn@phys.uu.nl */
12 /* 2000-04-04: support added for Dayna2 by bart@etpmod.phys.tue.nl */
13 /* 2001-04-18: support for DaynaPort E/LC-M by rayk@knightsmanor.org */
14 /* 2001-05-15: support for Cabletron ported from old daynaport driver
15 * and fixed access to Sonic Sys card which masquerades as a Farallon
16 * by rayk@knightsmanor.org */
17 /* 2002-12-30: Try to support more cards, some clues from NetBSD driver */
18 /* 2003-12-26: Make sure Asante cards always work. */
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/interrupt.h>
25 #include <linux/ptrace.h>
26 #include <linux/ioport.h>
27 #include <linux/nubus.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/bitops.h>
38 #include <asm/system.h>
41 #include <asm/hwtest.h>
42 #include <asm/macints.h>
44 static char version[] =
45 "mac8390.c: v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
47 #define EI_SHIFT(x) (ei_local->reg_offset[x])
48 #define ei_inb(port) in_8(port)
49 #define ei_outb(val,port) out_8(port,val)
50 #define ei_inb_p(port) in_8(port)
51 #define ei_outb_p(val,port) out_8(port,val)
55 #define WD_START_PG 0x00 /* First page of TX buffer */
56 #define CABLETRON_RX_START_PG 0x00 /* First page of RX buffer */
57 #define CABLETRON_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
58 #define CABLETRON_TX_START_PG CABLETRON_RX_STOP_PG /* First page of TX buffer */
60 /* Unfortunately it seems we have to hardcode these for the moment */
61 /* Shouldn't the card know about this? Does anyone know where to read it off the card? Do we trust the data provided by the card? */
63 #define DAYNA_8390_BASE 0x80000
64 #define DAYNA_8390_MEM 0x00000
66 #define CABLETRON_8390_BASE 0x90000
67 #define CABLETRON_8390_MEM 0x00000
69 #define INTERLAN_8390_BASE 0xE0000
70 #define INTERLAN_8390_MEM 0xD0000
83 static const char * cardname[] = {
93 static int word16[] = {
103 /* on which cards do we use NuBus resources? */
104 static int useresources[] = {
114 enum mac8390_access {
120 extern int mac8390_memtest(struct net_device * dev);
121 static int mac8390_initdev(struct net_device * dev, struct nubus_dev * ndev,
122 enum mac8390_type type);
124 static int mac8390_open(struct net_device * dev);
125 static int mac8390_close(struct net_device * dev);
126 static void mac8390_no_reset(struct net_device *dev);
127 static void interlan_reset(struct net_device *dev);
129 /* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
130 static void sane_get_8390_hdr(struct net_device *dev,
131 struct e8390_pkt_hdr *hdr, int ring_page);
132 static void sane_block_input(struct net_device * dev, int count,
133 struct sk_buff * skb, int ring_offset);
134 static void sane_block_output(struct net_device * dev, int count,
135 const unsigned char * buf, const int start_page);
137 /* dayna_memcpy to and from card */
138 static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
139 int from, int count);
140 static void dayna_memcpy_tocard(struct net_device *dev, int to,
141 const void *from, int count);
143 /* Dayna - Dayna/Kinetics use this */
144 static void dayna_get_8390_hdr(struct net_device *dev,
145 struct e8390_pkt_hdr *hdr, int ring_page);
146 static void dayna_block_input(struct net_device *dev, int count,
147 struct sk_buff *skb, int ring_offset);
148 static void dayna_block_output(struct net_device *dev, int count,
149 const unsigned char *buf, int start_page);
151 #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
152 #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
154 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
155 static void slow_sane_get_8390_hdr(struct net_device *dev,
156 struct e8390_pkt_hdr *hdr, int ring_page);
157 static void slow_sane_block_input(struct net_device *dev, int count,
158 struct sk_buff *skb, int ring_offset);
159 static void slow_sane_block_output(struct net_device *dev, int count,
160 const unsigned char *buf, int start_page);
161 static void word_memcpy_tocard(void *tp, const void *fp, int count);
162 static void word_memcpy_fromcard(void *tp, const void *fp, int count);
164 static enum mac8390_type __init mac8390_ident(struct nubus_dev *dev)
166 switch (dev->dr_sw) {
167 case NUBUS_DRSW_3COM:
168 switch (dev->dr_hw) {
169 case NUBUS_DRHW_APPLE_SONIC_NB:
170 case NUBUS_DRHW_APPLE_SONIC_LC:
171 case NUBUS_DRHW_SONNET:
175 return MAC8390_APPLE;
180 case NUBUS_DRSW_APPLE:
181 switch (dev->dr_hw) {
182 case NUBUS_DRHW_ASANTE_LC:
185 case NUBUS_DRHW_CABLETRON:
186 return MAC8390_CABLETRON;
189 return MAC8390_APPLE;
194 case NUBUS_DRSW_ASANTE:
195 return MAC8390_ASANTE;
198 case NUBUS_DRSW_TECHWORKS:
199 case NUBUS_DRSW_DAYNA2:
200 case NUBUS_DRSW_DAYNA_LC:
201 if (dev->dr_hw == NUBUS_DRHW_CABLETRON)
202 return MAC8390_CABLETRON;
204 return MAC8390_APPLE;
207 case NUBUS_DRSW_FARALLON:
208 return MAC8390_FARALLON;
211 case NUBUS_DRSW_KINETICS:
212 switch (dev->dr_hw) {
213 case NUBUS_DRHW_INTERLAN:
214 return MAC8390_INTERLAN;
217 return MAC8390_KINETICS;
222 case NUBUS_DRSW_DAYNA:
223 // These correspond to Dayna Sonic cards
224 // which use the macsonic driver
225 if (dev->dr_hw == NUBUS_DRHW_SMC9194 ||
226 dev->dr_hw == NUBUS_DRHW_INTERLAN )
229 return MAC8390_DAYNA;
235 static enum mac8390_access __init mac8390_testio(volatile unsigned long membase)
237 unsigned long outdata = 0xA5A0B5B0;
238 unsigned long indata = 0x00000000;
239 /* Try writing 32 bits */
240 memcpy((char *)membase, (char *)&outdata, 4);
241 /* Now compare them */
242 if (memcmp((char *)&outdata, (char *)membase, 4) == 0)
244 /* Write 16 bit output */
245 word_memcpy_tocard((char *)membase, (char *)&outdata, 4);
246 /* Now read it back */
247 word_memcpy_fromcard((char *)&indata, (char *)membase, 4);
248 if (outdata == indata)
250 return ACCESS_UNKNOWN;
253 static int __init mac8390_memsize(unsigned long membase)
258 local_irq_save(flags);
259 /* Check up to 32K in 4K increments */
260 for (i = 0; i < 8; i++) {
261 volatile unsigned short *m = (unsigned short *) (membase + (i * 0x1000));
263 /* Unwriteable - we have a fully decoded card and the
265 if (hwreg_present(m) == 0)
268 /* write a distinctive byte */
270 /* check that we read back what we wrote */
271 if (*m != (0xA5A0 | i))
274 /* check for partial decode and wrap */
275 for (j = 0; j < i; j++) {
276 volatile unsigned short *p = (unsigned short *) (membase + (j * 0x1000));
277 if (*p != (0xA5A0 | j))
281 local_irq_restore(flags);
282 /* in any case, we stopped once we tried one block too many,
283 or once we reached 32K */
287 struct net_device * __init mac8390_probe(int unit)
289 struct net_device *dev;
290 volatile unsigned short *i;
291 int version_disp = 0;
292 struct nubus_dev * ndev = NULL;
295 struct nubus_dir dir;
296 struct nubus_dirent ent;
298 static unsigned int slots;
300 enum mac8390_type cardtype;
302 /* probably should check for Nubus instead */
305 return ERR_PTR(-ENODEV);
307 dev = alloc_ei_netdev();
309 return ERR_PTR(-ENOMEM);
312 sprintf(dev->name, "eth%d", unit);
314 while ((ndev = nubus_find_type(NUBUS_CAT_NETWORK, NUBUS_TYPE_ETHERNET, ndev))) {
315 /* Have we seen it already? */
316 if (slots & (1<<ndev->board->slot))
318 slots |= 1<<ndev->board->slot;
320 if ((cardtype = mac8390_ident(ndev)) == MAC8390_NONE)
323 if (version_disp == 0) {
328 dev->irq = SLOT2IRQ(ndev->board->slot);
329 /* This is getting to be a habit */
330 dev->base_addr = ndev->board->slot_addr | ((ndev->board->slot&0xf) << 20);
332 /* Get some Nubus info - we will trust the card's idea
333 of where its memory and registers are. */
335 if (nubus_get_func_dir(ndev, &dir) == -1) {
336 printk(KERN_ERR "%s: Unable to get Nubus functional"
337 " directory for slot %X!\n",
338 dev->name, ndev->board->slot);
342 /* Get the MAC address */
343 if ((nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent)) == -1) {
344 printk(KERN_INFO "%s: Couldn't get MAC address!\n",
348 nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
351 if (useresources[cardtype] == 1) {
352 nubus_rewinddir(&dir);
353 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS, &ent) == -1) {
354 printk(KERN_ERR "%s: Memory offset resource"
355 " for slot %X not found!\n",
356 dev->name, ndev->board->slot);
359 nubus_get_rsrc_mem(&offset, &ent, 4);
360 dev->mem_start = dev->base_addr + offset;
361 /* yes, this is how the Apple driver does it */
362 dev->base_addr = dev->mem_start + 0x10000;
363 nubus_rewinddir(&dir);
364 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH, &ent) == -1) {
365 printk(KERN_INFO "%s: Memory length resource"
366 " for slot %X not found"
368 dev->name, ndev->board->slot);
369 offset = mac8390_memsize(dev->mem_start);
371 nubus_get_rsrc_mem(&offset, &ent, 4);
373 dev->mem_end = dev->mem_start + offset;
376 case MAC8390_KINETICS:
377 case MAC8390_DAYNA: /* it's the same */
379 (int)(ndev->board->slot_addr +
382 (int)(ndev->board->slot_addr +
386 mac8390_memsize(dev->mem_start);
388 case MAC8390_INTERLAN:
390 (int)(ndev->board->slot_addr +
393 (int)(ndev->board->slot_addr +
397 mac8390_memsize(dev->mem_start);
399 case MAC8390_CABLETRON:
401 (int)(ndev->board->slot_addr +
402 CABLETRON_8390_BASE);
404 (int)(ndev->board->slot_addr +
406 /* The base address is unreadable if 0x00
407 * has been written to the command register
408 * Reset the chip by writing E8390_NODMA +
409 * E8390_PAGE0 + E8390_STOP just to be
412 i = (void *)dev->base_addr;
416 mac8390_memsize(dev->mem_start);
420 printk(KERN_ERR "Card type %s is"
421 " unsupported, sorry\n",
427 /* Do the nasty 8390 stuff */
428 if (!mac8390_initdev(dev, ndev, cardtype))
434 err = register_netdev(dev);
445 MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
446 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
447 MODULE_LICENSE("GPL");
449 /* overkill, of course */
450 static struct net_device *dev_mac8390[15];
451 int init_module(void)
454 for (i = 0; i < 15; i++) {
455 struct net_device *dev = mac8390_probe(-1);
461 printk(KERN_NOTICE "mac8390.c: No useable cards found, driver NOT installed.\n");
467 void cleanup_module(void)
470 for (i = 0; i < 15; i++) {
471 struct net_device *dev = dev_mac890[i];
473 unregister_netdev(dev);
481 static const struct net_device_ops mac8390_netdev_ops = {
482 .ndo_open = mac8390_open,
483 .ndo_stop = mac8390_close,
484 .ndo_start_xmit = ei_start_xmit,
485 .ndo_tx_timeout = ei_tx_timeout,
486 .ndo_get_stats = ei_get_stats,
487 .ndo_set_multicast_list = ei_set_multicast_list,
488 .ndo_validate_addr = eth_validate_addr,
489 .ndo_change_mtu = eth_change_mtu,
490 #ifdef CONFIG_NET_POLL_CONTROLLER
491 .ndo_poll_controller = ei_poll,
495 static int __init mac8390_initdev(struct net_device * dev, struct nubus_dev * ndev,
496 enum mac8390_type type)
498 static u32 fwrd4_offsets[16]={
504 static u32 back4_offsets[16]={
510 static u32 fwrd2_offsets[16]={
517 int access_bitmode = 0;
519 /* Now fill in our stuff */
520 dev->netdev_ops = &mac8390_netdev_ops;
522 /* GAR, ei_status is actually a macro even though it looks global */
523 ei_status.name = cardname[type];
524 ei_status.word16 = word16[type];
526 /* Cabletron's TX/RX buffers are backwards */
527 if (type == MAC8390_CABLETRON) {
528 ei_status.tx_start_page = CABLETRON_TX_START_PG;
529 ei_status.rx_start_page = CABLETRON_RX_START_PG;
530 ei_status.stop_page = CABLETRON_RX_STOP_PG;
531 ei_status.rmem_start = dev->mem_start;
532 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
534 ei_status.tx_start_page = WD_START_PG;
535 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
536 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
537 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
538 ei_status.rmem_end = dev->mem_end;
541 /* Fill in model-specific information and functions */
543 case MAC8390_FARALLON:
545 switch(mac8390_testio(dev->mem_start)) {
547 printk("Don't know how to access card memory!\n");
552 /* 16 bit card, register map is reversed */
553 ei_status.reset_8390 = &mac8390_no_reset;
554 ei_status.block_input = &slow_sane_block_input;
555 ei_status.block_output = &slow_sane_block_output;
556 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
557 ei_status.reg_offset = back4_offsets;
561 /* 32 bit card, register map is reversed */
562 ei_status.reset_8390 = &mac8390_no_reset;
563 ei_status.block_input = &sane_block_input;
564 ei_status.block_output = &sane_block_output;
565 ei_status.get_8390_hdr = &sane_get_8390_hdr;
566 ei_status.reg_offset = back4_offsets;
573 /* Some Asante cards pass the 32 bit test
574 * but overwrite system memory when run at 32 bit.
575 * so we run them all at 16 bit.
577 ei_status.reset_8390 = &mac8390_no_reset;
578 ei_status.block_input = &slow_sane_block_input;
579 ei_status.block_output = &slow_sane_block_output;
580 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
581 ei_status.reg_offset = back4_offsets;
584 case MAC8390_CABLETRON:
585 /* 16 bit card, register map is short forward */
586 ei_status.reset_8390 = &mac8390_no_reset;
587 ei_status.block_input = &slow_sane_block_input;
588 ei_status.block_output = &slow_sane_block_output;
589 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
590 ei_status.reg_offset = fwrd2_offsets;
594 case MAC8390_KINETICS:
595 /* 16 bit memory, register map is forward */
596 /* dayna and similar */
597 ei_status.reset_8390 = &mac8390_no_reset;
598 ei_status.block_input = &dayna_block_input;
599 ei_status.block_output = &dayna_block_output;
600 ei_status.get_8390_hdr = &dayna_get_8390_hdr;
601 ei_status.reg_offset = fwrd4_offsets;
604 case MAC8390_INTERLAN:
605 /* 16 bit memory, register map is forward */
606 ei_status.reset_8390 = &interlan_reset;
607 ei_status.block_input = &slow_sane_block_input;
608 ei_status.block_output = &slow_sane_block_output;
609 ei_status.get_8390_hdr = &slow_sane_get_8390_hdr;
610 ei_status.reg_offset = fwrd4_offsets;
614 printk(KERN_ERR "Card type %s is unsupported, sorry\n", ndev->board->name);
618 __NS8390_init(dev, 0);
620 /* Good, done, now spit out some messages */
621 printk(KERN_INFO "%s: %s in slot %X (type %s)\n",
622 dev->name, ndev->board->name, ndev->board->slot, cardname[type]);
623 printk(KERN_INFO "MAC ");
626 for (i = 0; i < 6; i++) {
627 printk("%2.2x", dev->dev_addr[i]);
632 printk(" IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
633 dev->irq, (int)((dev->mem_end - dev->mem_start)/0x1000) * 4,
634 dev->mem_start, access_bitmode?32:16);
638 static int mac8390_open(struct net_device *dev)
641 if (request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev)) {
642 printk ("%s: unable to get IRQ %d.\n", dev->name, dev->irq);
648 static int mac8390_close(struct net_device *dev)
650 free_irq(dev->irq, dev);
655 static void mac8390_no_reset(struct net_device *dev)
659 printk("reset not supported\n");
663 static void interlan_reset(struct net_device *dev)
665 unsigned char *target=nubus_slot_addr(IRQ2SLOT(dev->irq));
667 printk("Need to reset the NS8390 t=%lu...", jiffies);
671 printk("reset complete\n");
675 /* dayna_memcpy_fromio/dayna_memcpy_toio */
676 /* directly from daynaport.c by Alan Cox */
677 static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from, int count)
679 volatile unsigned char *ptr;
680 unsigned char *target=to;
681 from<<=1; /* word, skip overhead */
682 ptr=(unsigned char *)(dev->mem_start+from);
691 *(unsigned short *)target = *(unsigned short volatile *)ptr;
692 ptr += 4; /* skip cruft */
701 static void dayna_memcpy_tocard(struct net_device *dev, int to, const void *from, int count)
703 volatile unsigned short *ptr;
704 const unsigned char *src=from;
705 to<<=1; /* word, skip overhead */
706 ptr=(unsigned short *)(dev->mem_start+to);
708 if (to&2) { /* avoid a byte write (stomps on other data) */
709 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
715 *ptr++=*(unsigned short *)src; /* Copy and */
716 ptr++; /* skip cruft */
723 /* card doesn't like byte writes */
724 *ptr=(*ptr&0x00FF)|(*src << 8);
728 /* sane block input/output */
729 static void sane_get_8390_hdr(struct net_device *dev,
730 struct e8390_pkt_hdr *hdr, int ring_page)
732 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
733 memcpy_fromio((void *)hdr, (char *)dev->mem_start + hdr_start, 4);
735 hdr->count = swab16(hdr->count);
738 static void sane_block_input(struct net_device *dev, int count,
739 struct sk_buff *skb, int ring_offset)
741 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
742 unsigned long xfer_start = xfer_base + dev->mem_start;
744 if (xfer_start + count > ei_status.rmem_end) {
745 /* We must wrap the input move. */
746 int semi_count = ei_status.rmem_end - xfer_start;
747 memcpy_fromio(skb->data, (char *)dev->mem_start + xfer_base, semi_count);
749 memcpy_toio(skb->data + semi_count, (char *)ei_status.rmem_start, count);
751 memcpy_fromio(skb->data, (char *)dev->mem_start + xfer_base, count);
755 static void sane_block_output(struct net_device *dev, int count,
756 const unsigned char *buf, int start_page)
758 long shmem = (start_page - WD_START_PG)<<8;
760 memcpy_toio((char *)dev->mem_start + shmem, buf, count);
763 /* dayna block input/output */
764 static void dayna_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
766 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
768 dayna_memcpy_fromcard(dev, (void *)hdr, hdr_start, 4);
770 hdr->count=(hdr->count&0xFF)<<8|(hdr->count>>8);
773 static void dayna_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
775 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
776 unsigned long xfer_start = xfer_base+dev->mem_start;
778 /* Note the offset math is done in card memory space which is word
779 per long onto our space. */
781 if (xfer_start + count > ei_status.rmem_end)
783 /* We must wrap the input move. */
784 int semi_count = ei_status.rmem_end - xfer_start;
785 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
787 dayna_memcpy_fromcard(dev, skb->data + semi_count,
788 ei_status.rmem_start - dev->mem_start,
793 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
797 static void dayna_block_output(struct net_device *dev, int count, const unsigned char *buf,
800 long shmem = (start_page - WD_START_PG)<<8;
802 dayna_memcpy_tocard(dev, shmem, buf, count);
805 /* Cabletron block I/O */
806 static void slow_sane_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
809 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
810 word_memcpy_fromcard((void *)hdr, (char *)dev->mem_start+hdr_start, 4);
811 /* Register endianism - fix here rather than 8390.c */
812 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
815 static void slow_sane_block_input(struct net_device *dev, int count, struct sk_buff *skb,
818 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
819 unsigned long xfer_start = xfer_base+dev->mem_start;
821 if (xfer_start + count > ei_status.rmem_end)
823 /* We must wrap the input move. */
824 int semi_count = ei_status.rmem_end - xfer_start;
825 word_memcpy_fromcard(skb->data, (char *)dev->mem_start +
826 xfer_base, semi_count);
828 word_memcpy_fromcard(skb->data + semi_count,
829 (char *)ei_status.rmem_start, count);
833 word_memcpy_fromcard(skb->data, (char *)dev->mem_start +
838 static void slow_sane_block_output(struct net_device *dev, int count, const unsigned char *buf,
841 long shmem = (start_page - WD_START_PG)<<8;
843 word_memcpy_tocard((char *)dev->mem_start + shmem, buf, count);
846 static void word_memcpy_tocard(void *tp, const void *fp, int count)
848 volatile unsigned short *to = tp;
849 const unsigned short *from = fp;
858 static void word_memcpy_fromcard(void *tp, const void *fp, int count)
860 unsigned short *to = tp;
861 const volatile unsigned short *from = fp;