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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
27 #include <linux/ptrace.h>
28 #include <linux/ioport.h>
29 #include <linux/nubus.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/bitops.h>
41 #include <asm/hwtest.h>
42 #include <asm/macints.h>
44 static char version[] =
45 "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
59 /* First page of TX buffer */
62 * Unfortunately it seems we have to hardcode these for the moment
63 * Shouldn't the card know about this?
64 * Does anyone know where to read it off the card?
65 * Do we trust the data provided by the card?
68 #define DAYNA_8390_BASE 0x80000
69 #define DAYNA_8390_MEM 0x00000
71 #define CABLETRON_8390_BASE 0x90000
72 #define CABLETRON_8390_MEM 0x00000
74 #define INTERLAN_8390_BASE 0xE0000
75 #define INTERLAN_8390_MEM 0xD0000
88 static const char *cardname[] = {
98 static const int word16[] = {
108 /* on which cards do we use NuBus resources? */
109 static const int useresources[] = {
119 enum mac8390_access {
125 extern int mac8390_memtest(struct net_device *dev);
126 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
127 enum mac8390_type type);
129 static int mac8390_open(struct net_device *dev);
130 static int mac8390_close(struct net_device *dev);
131 static void mac8390_no_reset(struct net_device *dev);
132 static void interlan_reset(struct net_device *dev);
134 /* Sane (32-bit chunk memory read/write) - Some Farallon and Apple do this*/
135 static void sane_get_8390_hdr(struct net_device *dev,
136 struct e8390_pkt_hdr *hdr, int ring_page);
137 static void sane_block_input(struct net_device *dev, int count,
138 struct sk_buff *skb, int ring_offset);
139 static void sane_block_output(struct net_device *dev, int count,
140 const unsigned char *buf, const int start_page);
142 /* dayna_memcpy to and from card */
143 static void dayna_memcpy_fromcard(struct net_device *dev, void *to,
144 int from, int count);
145 static void dayna_memcpy_tocard(struct net_device *dev, int to,
146 const void *from, int count);
148 /* Dayna - Dayna/Kinetics use this */
149 static void dayna_get_8390_hdr(struct net_device *dev,
150 struct e8390_pkt_hdr *hdr, int ring_page);
151 static void dayna_block_input(struct net_device *dev, int count,
152 struct sk_buff *skb, int ring_offset);
153 static void dayna_block_output(struct net_device *dev, int count,
154 const unsigned char *buf, int start_page);
156 #define memcpy_fromio(a, b, c) memcpy((a), (void *)(b), (c))
157 #define memcpy_toio(a, b, c) memcpy((void *)(a), (b), (c))
159 #define memcmp_withio(a, b, c) memcmp((a), (void *)(b), (c))
161 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
162 static void slow_sane_get_8390_hdr(struct net_device *dev,
163 struct e8390_pkt_hdr *hdr, int ring_page);
164 static void slow_sane_block_input(struct net_device *dev, int count,
165 struct sk_buff *skb, int ring_offset);
166 static void slow_sane_block_output(struct net_device *dev, int count,
167 const unsigned char *buf, int start_page);
168 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
169 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
171 static enum mac8390_type mac8390_ident(struct nubus_rsrc *fres)
173 switch (fres->dr_sw) {
174 case NUBUS_DRSW_3COM:
175 switch (fres->dr_hw) {
176 case NUBUS_DRHW_APPLE_SONIC_NB:
177 case NUBUS_DRHW_APPLE_SONIC_LC:
178 case NUBUS_DRHW_SONNET:
181 return MAC8390_APPLE;
185 case NUBUS_DRSW_APPLE:
186 switch (fres->dr_hw) {
187 case NUBUS_DRHW_ASANTE_LC:
189 case NUBUS_DRHW_CABLETRON:
190 return MAC8390_CABLETRON;
192 return MAC8390_APPLE;
196 case NUBUS_DRSW_ASANTE:
197 return MAC8390_ASANTE;
200 case NUBUS_DRSW_TECHWORKS:
201 case NUBUS_DRSW_DAYNA2:
202 case NUBUS_DRSW_DAYNA_LC:
203 if (fres->dr_hw == NUBUS_DRHW_CABLETRON)
204 return MAC8390_CABLETRON;
206 return MAC8390_APPLE;
209 case NUBUS_DRSW_FARALLON:
210 return MAC8390_FARALLON;
213 case NUBUS_DRSW_KINETICS:
214 switch (fres->dr_hw) {
215 case NUBUS_DRHW_INTERLAN:
216 return MAC8390_INTERLAN;
218 return MAC8390_KINETICS;
222 case NUBUS_DRSW_DAYNA:
224 * These correspond to Dayna Sonic cards
225 * which use the macsonic driver
227 if (fres->dr_hw == NUBUS_DRHW_SMC9194 ||
228 fres->dr_hw == NUBUS_DRHW_INTERLAN)
231 return MAC8390_DAYNA;
237 static enum mac8390_access mac8390_testio(unsigned long membase)
239 unsigned long outdata = 0xA5A0B5B0;
240 unsigned long indata = 0x00000000;
241 /* Try writing 32 bits */
242 memcpy_toio(membase, &outdata, 4);
243 /* Now compare them */
244 if (memcmp_withio(&outdata, membase, 4) == 0)
246 /* Write 16 bit output */
247 word_memcpy_tocard(membase, &outdata, 4);
248 /* Now read it back */
249 word_memcpy_fromcard(&indata, membase, 4);
250 if (outdata == indata)
252 return ACCESS_UNKNOWN;
255 static int mac8390_memsize(unsigned long membase)
260 local_irq_save(flags);
261 /* Check up to 32K in 4K increments */
262 for (i = 0; i < 8; i++) {
263 volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
265 /* Unwriteable - we have a fully decoded card and the
267 if (hwreg_present(m) == 0)
270 /* write a distinctive byte */
272 /* check that we read back what we wrote */
273 if (*m != (0xA5A0 | i))
276 /* check for partial decode and wrap */
277 for (j = 0; j < i; j++) {
278 volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
279 if (*p != (0xA5A0 | j))
283 local_irq_restore(flags);
285 * in any case, we stopped once we tried one block too many,
286 * or once we reached 32K
291 static bool mac8390_rsrc_init(struct net_device *dev,
292 struct nubus_rsrc *fres,
293 enum mac8390_type cardtype)
295 struct nubus_board *board = fres->board;
296 struct nubus_dir dir;
297 struct nubus_dirent ent;
299 volatile unsigned short *i;
301 dev->irq = SLOT2IRQ(board->slot);
302 /* This is getting to be a habit */
303 dev->base_addr = board->slot_addr | ((board->slot & 0xf) << 20);
306 * Get some Nubus info - we will trust the card's idea
307 * of where its memory and registers are.
310 if (nubus_get_func_dir(fres, &dir) == -1) {
312 "Unable to get Nubus functional directory\n");
316 /* Get the MAC address */
317 if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
318 dev_info(&board->dev, "MAC address resource not found\n");
322 nubus_get_rsrc_mem(dev->dev_addr, &ent, 6);
324 if (useresources[cardtype] == 1) {
325 nubus_rewinddir(&dir);
326 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
329 "Memory offset resource not found\n");
332 nubus_get_rsrc_mem(&offset, &ent, 4);
333 dev->mem_start = dev->base_addr + offset;
334 /* yes, this is how the Apple driver does it */
335 dev->base_addr = dev->mem_start + 0x10000;
336 nubus_rewinddir(&dir);
337 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
339 dev_info(&board->dev,
340 "Memory length resource not found, probing\n");
341 offset = mac8390_memsize(dev->mem_start);
343 nubus_get_rsrc_mem(&offset, &ent, 4);
345 dev->mem_end = dev->mem_start + offset;
348 case MAC8390_KINETICS:
349 case MAC8390_DAYNA: /* it's the same */
350 dev->base_addr = (int)(board->slot_addr +
352 dev->mem_start = (int)(board->slot_addr +
354 dev->mem_end = dev->mem_start +
355 mac8390_memsize(dev->mem_start);
357 case MAC8390_INTERLAN:
358 dev->base_addr = (int)(board->slot_addr +
360 dev->mem_start = (int)(board->slot_addr +
362 dev->mem_end = dev->mem_start +
363 mac8390_memsize(dev->mem_start);
365 case MAC8390_CABLETRON:
366 dev->base_addr = (int)(board->slot_addr +
367 CABLETRON_8390_BASE);
368 dev->mem_start = (int)(board->slot_addr +
370 /* The base address is unreadable if 0x00
371 * has been written to the command register
372 * Reset the chip by writing E8390_NODMA +
373 * E8390_PAGE0 + E8390_STOP just to be
376 i = (void *)dev->base_addr;
378 dev->mem_end = dev->mem_start +
379 mac8390_memsize(dev->mem_start);
384 "No known base address for card type\n");
392 static int mac8390_device_probe(struct nubus_board *board)
394 struct net_device *dev;
396 struct nubus_rsrc *fres;
397 enum mac8390_type cardtype = MAC8390_NONE;
399 dev = ____alloc_ei_netdev(0);
403 SET_NETDEV_DEV(dev, &board->dev);
405 for_each_board_func_rsrc(board, fres) {
406 if (fres->category != NUBUS_CAT_NETWORK ||
407 fres->type != NUBUS_TYPE_ETHERNET)
410 cardtype = mac8390_ident(fres);
411 if (cardtype == MAC8390_NONE)
414 if (mac8390_rsrc_init(dev, fres, cardtype))
420 err = mac8390_initdev(dev, board, cardtype);
424 err = register_netdev(dev);
428 nubus_set_drvdata(board, dev);
436 static int mac8390_device_remove(struct nubus_board *board)
438 struct net_device *dev = nubus_get_drvdata(board);
440 unregister_netdev(dev);
445 static struct nubus_driver mac8390_driver = {
446 .probe = mac8390_device_probe,
447 .remove = mac8390_device_remove,
449 .name = KBUILD_MODNAME,
450 .owner = THIS_MODULE,
454 MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
455 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
456 MODULE_LICENSE("GPL");
458 static int __init mac8390_init(void)
460 return nubus_driver_register(&mac8390_driver);
462 module_init(mac8390_init);
464 static void __exit mac8390_exit(void)
466 nubus_driver_unregister(&mac8390_driver);
468 module_exit(mac8390_exit);
470 static const struct net_device_ops mac8390_netdev_ops = {
471 .ndo_open = mac8390_open,
472 .ndo_stop = mac8390_close,
473 .ndo_start_xmit = __ei_start_xmit,
474 .ndo_tx_timeout = __ei_tx_timeout,
475 .ndo_get_stats = __ei_get_stats,
476 .ndo_set_rx_mode = __ei_set_multicast_list,
477 .ndo_validate_addr = eth_validate_addr,
478 .ndo_set_mac_address = eth_mac_addr,
479 #ifdef CONFIG_NET_POLL_CONTROLLER
480 .ndo_poll_controller = __ei_poll,
484 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
485 enum mac8390_type type)
487 static u32 fwrd4_offsets[16] = {
493 static u32 back4_offsets[16] = {
499 static u32 fwrd2_offsets[16] = {
506 int access_bitmode = 0;
508 /* Now fill in our stuff */
509 dev->netdev_ops = &mac8390_netdev_ops;
511 /* GAR, ei_status is actually a macro even though it looks global */
512 ei_status.name = cardname[type];
513 ei_status.word16 = word16[type];
515 /* Cabletron's TX/RX buffers are backwards */
516 if (type == MAC8390_CABLETRON) {
517 ei_status.tx_start_page = CABLETRON_TX_START_PG;
518 ei_status.rx_start_page = CABLETRON_RX_START_PG;
519 ei_status.stop_page = CABLETRON_RX_STOP_PG;
520 ei_status.rmem_start = dev->mem_start;
521 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
523 ei_status.tx_start_page = WD_START_PG;
524 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
525 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
526 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
527 ei_status.rmem_end = dev->mem_end;
530 /* Fill in model-specific information and functions */
532 case MAC8390_FARALLON:
534 switch (mac8390_testio(dev->mem_start)) {
537 "Don't know how to access card memory\n");
541 /* 16 bit card, register map is reversed */
542 ei_status.reset_8390 = mac8390_no_reset;
543 ei_status.block_input = slow_sane_block_input;
544 ei_status.block_output = slow_sane_block_output;
545 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
546 ei_status.reg_offset = back4_offsets;
550 /* 32 bit card, register map is reversed */
551 ei_status.reset_8390 = mac8390_no_reset;
552 ei_status.block_input = sane_block_input;
553 ei_status.block_output = sane_block_output;
554 ei_status.get_8390_hdr = sane_get_8390_hdr;
555 ei_status.reg_offset = back4_offsets;
562 /* Some Asante cards pass the 32 bit test
563 * but overwrite system memory when run at 32 bit.
564 * so we run them all at 16 bit.
566 ei_status.reset_8390 = mac8390_no_reset;
567 ei_status.block_input = slow_sane_block_input;
568 ei_status.block_output = slow_sane_block_output;
569 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
570 ei_status.reg_offset = back4_offsets;
573 case MAC8390_CABLETRON:
574 /* 16 bit card, register map is short forward */
575 ei_status.reset_8390 = mac8390_no_reset;
576 ei_status.block_input = slow_sane_block_input;
577 ei_status.block_output = slow_sane_block_output;
578 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
579 ei_status.reg_offset = fwrd2_offsets;
583 case MAC8390_KINETICS:
584 /* 16 bit memory, register map is forward */
585 /* dayna and similar */
586 ei_status.reset_8390 = mac8390_no_reset;
587 ei_status.block_input = dayna_block_input;
588 ei_status.block_output = dayna_block_output;
589 ei_status.get_8390_hdr = dayna_get_8390_hdr;
590 ei_status.reg_offset = fwrd4_offsets;
593 case MAC8390_INTERLAN:
594 /* 16 bit memory, register map is forward */
595 ei_status.reset_8390 = interlan_reset;
596 ei_status.block_input = slow_sane_block_input;
597 ei_status.block_output = slow_sane_block_output;
598 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
599 ei_status.reg_offset = fwrd4_offsets;
603 dev_err(&board->dev, "Unsupported card type\n");
607 __NS8390_init(dev, 0);
609 /* Good, done, now spit out some messages */
610 dev_info(&board->dev, "%s (type %s)\n", board->name, cardname[type]);
611 dev_info(&board->dev, "MAC %pM, IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
612 dev->dev_addr, dev->irq,
613 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
614 dev->mem_start, access_bitmode ? 32 : 16);
618 static int mac8390_open(struct net_device *dev)
623 err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
625 pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
629 static int mac8390_close(struct net_device *dev)
631 free_irq(dev->irq, dev);
636 static void mac8390_no_reset(struct net_device *dev)
638 struct ei_device *ei_local = netdev_priv(dev);
641 netif_info(ei_local, hw, dev, "reset not supported\n");
644 static void interlan_reset(struct net_device *dev)
646 unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
647 struct ei_device *ei_local = netdev_priv(dev);
649 netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
653 if (netif_msg_hw(ei_local))
654 pr_cont("reset complete\n");
657 /* dayna_memcpy_fromio/dayna_memcpy_toio */
658 /* directly from daynaport.c by Alan Cox */
659 static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
662 volatile unsigned char *ptr;
663 unsigned char *target = to;
664 from <<= 1; /* word, skip overhead */
665 ptr = (unsigned char *)(dev->mem_start+from);
673 *(unsigned short *)target = *(unsigned short volatile *)ptr;
674 ptr += 4; /* skip cruft */
683 static void dayna_memcpy_tocard(struct net_device *dev, int to,
684 const void *from, int count)
686 volatile unsigned short *ptr;
687 const unsigned char *src = from;
688 to <<= 1; /* word, skip overhead */
689 ptr = (unsigned short *)(dev->mem_start+to);
691 if (to & 2) { /* avoid a byte write (stomps on other data) */
692 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
697 *ptr++ = *(unsigned short *)src; /* Copy and */
698 ptr++; /* skip cruft */
704 /* card doesn't like byte writes */
705 *ptr = (*ptr & 0x00FF) | (*src << 8);
709 /* sane block input/output */
710 static void sane_get_8390_hdr(struct net_device *dev,
711 struct e8390_pkt_hdr *hdr, int ring_page)
713 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
714 memcpy_fromio(hdr, dev->mem_start + hdr_start, 4);
716 hdr->count = swab16(hdr->count);
719 static void sane_block_input(struct net_device *dev, int count,
720 struct sk_buff *skb, int ring_offset)
722 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
723 unsigned long xfer_start = xfer_base + dev->mem_start;
725 if (xfer_start + count > ei_status.rmem_end) {
726 /* We must wrap the input move. */
727 int semi_count = ei_status.rmem_end - xfer_start;
728 memcpy_fromio(skb->data, dev->mem_start + xfer_base,
731 memcpy_fromio(skb->data + semi_count, ei_status.rmem_start,
734 memcpy_fromio(skb->data, dev->mem_start + xfer_base, count);
738 static void sane_block_output(struct net_device *dev, int count,
739 const unsigned char *buf, int start_page)
741 long shmem = (start_page - WD_START_PG)<<8;
743 memcpy_toio(dev->mem_start + shmem, buf, count);
746 /* dayna block input/output */
747 static void dayna_get_8390_hdr(struct net_device *dev,
748 struct e8390_pkt_hdr *hdr, int ring_page)
750 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
752 dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
754 hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
757 static void dayna_block_input(struct net_device *dev, int count,
758 struct sk_buff *skb, int ring_offset)
760 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
761 unsigned long xfer_start = xfer_base+dev->mem_start;
763 /* Note the offset math is done in card memory space which is word
764 per long onto our space. */
766 if (xfer_start + count > ei_status.rmem_end) {
767 /* We must wrap the input move. */
768 int semi_count = ei_status.rmem_end - xfer_start;
769 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
771 dayna_memcpy_fromcard(dev, skb->data + semi_count,
772 ei_status.rmem_start - dev->mem_start,
775 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
779 static void dayna_block_output(struct net_device *dev, int count,
780 const unsigned char *buf,
783 long shmem = (start_page - WD_START_PG)<<8;
785 dayna_memcpy_tocard(dev, shmem, buf, count);
788 /* Cabletron block I/O */
789 static void slow_sane_get_8390_hdr(struct net_device *dev,
790 struct e8390_pkt_hdr *hdr,
793 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
794 word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
795 /* Register endianism - fix here rather than 8390.c */
796 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
799 static void slow_sane_block_input(struct net_device *dev, int count,
800 struct sk_buff *skb, int ring_offset)
802 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
803 unsigned long xfer_start = xfer_base+dev->mem_start;
805 if (xfer_start + count > ei_status.rmem_end) {
806 /* We must wrap the input move. */
807 int semi_count = ei_status.rmem_end - xfer_start;
808 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
811 word_memcpy_fromcard(skb->data + semi_count,
812 ei_status.rmem_start, count);
814 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
819 static void slow_sane_block_output(struct net_device *dev, int count,
820 const unsigned char *buf, int start_page)
822 long shmem = (start_page - WD_START_PG)<<8;
824 word_memcpy_tocard(dev->mem_start + shmem, buf, count);
827 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
829 volatile unsigned short *to = (void *)tp;
830 const unsigned short *from = fp;
839 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
841 unsigned short *to = tp;
842 const volatile unsigned short *from = (const void *)fp;