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
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
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[] =
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 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
157 static void slow_sane_get_8390_hdr(struct net_device *dev,
158 struct e8390_pkt_hdr *hdr, int ring_page);
159 static void slow_sane_block_input(struct net_device *dev, int count,
160 struct sk_buff *skb, int ring_offset);
161 static void slow_sane_block_output(struct net_device *dev, int count,
162 const unsigned char *buf, int start_page);
163 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count);
164 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count);
166 static enum mac8390_type mac8390_ident(struct nubus_rsrc *fres)
168 switch (fres->dr_sw) {
169 case NUBUS_DRSW_3COM:
170 switch (fres->dr_hw) {
171 case NUBUS_DRHW_APPLE_SONIC_NB:
172 case NUBUS_DRHW_APPLE_SONIC_LC:
173 case NUBUS_DRHW_SONNET:
176 return MAC8390_APPLE;
179 case NUBUS_DRSW_APPLE:
180 switch (fres->dr_hw) {
181 case NUBUS_DRHW_ASANTE_LC:
183 case NUBUS_DRHW_CABLETRON:
184 return MAC8390_CABLETRON;
186 return MAC8390_APPLE;
189 case NUBUS_DRSW_ASANTE:
190 return MAC8390_ASANTE;
192 case NUBUS_DRSW_TECHWORKS:
193 case NUBUS_DRSW_DAYNA2:
194 case NUBUS_DRSW_DAYNA_LC:
195 if (fres->dr_hw == NUBUS_DRHW_CABLETRON)
196 return MAC8390_CABLETRON;
198 return MAC8390_APPLE;
200 case NUBUS_DRSW_FARALLON:
201 return MAC8390_FARALLON;
203 case NUBUS_DRSW_KINETICS:
204 switch (fres->dr_hw) {
205 case NUBUS_DRHW_INTERLAN:
206 return MAC8390_INTERLAN;
208 return MAC8390_KINETICS;
211 case NUBUS_DRSW_DAYNA:
213 * These correspond to Dayna Sonic cards
214 * which use the macsonic driver
216 if (fres->dr_hw == NUBUS_DRHW_SMC9194 ||
217 fres->dr_hw == NUBUS_DRHW_INTERLAN)
220 return MAC8390_DAYNA;
225 static enum mac8390_access mac8390_testio(unsigned long membase)
227 u32 outdata = 0xA5A0B5B0;
230 /* Try writing 32 bits */
231 nubus_writel(outdata, membase);
232 /* Now read it back */
233 indata = nubus_readl(membase);
234 if (outdata == indata)
237 outdata = 0xC5C0D5D0;
240 /* Write 16 bit output */
241 word_memcpy_tocard(membase, &outdata, 4);
242 /* Now read it back */
243 word_memcpy_fromcard(&indata, membase, 4);
244 if (outdata == indata)
247 return ACCESS_UNKNOWN;
250 static int mac8390_memsize(unsigned long membase)
255 local_irq_save(flags);
256 /* Check up to 32K in 4K increments */
257 for (i = 0; i < 8; i++) {
258 volatile unsigned short *m = (unsigned short *)(membase + (i * 0x1000));
260 /* Unwriteable - we have a fully decoded card and the
262 if (hwreg_present(m) == 0)
265 /* write a distinctive byte */
267 /* check that we read back what we wrote */
268 if (*m != (0xA5A0 | i))
271 /* check for partial decode and wrap */
272 for (j = 0; j < i; j++) {
273 volatile unsigned short *p = (unsigned short *)(membase + (j * 0x1000));
274 if (*p != (0xA5A0 | j))
278 local_irq_restore(flags);
280 * in any case, we stopped once we tried one block too many,
281 * or once we reached 32K
286 static bool mac8390_rsrc_init(struct net_device *dev,
287 struct nubus_rsrc *fres,
288 enum mac8390_type cardtype)
290 struct nubus_board *board = fres->board;
291 struct nubus_dir dir;
292 struct nubus_dirent ent;
294 volatile unsigned short *i;
297 dev->irq = SLOT2IRQ(board->slot);
298 /* This is getting to be a habit */
299 dev->base_addr = board->slot_addr | ((board->slot & 0xf) << 20);
302 * Get some Nubus info - we will trust the card's idea
303 * of where its memory and registers are.
306 if (nubus_get_func_dir(fres, &dir) == -1) {
308 "Unable to get Nubus functional directory\n");
312 /* Get the MAC address */
313 if (nubus_find_rsrc(&dir, NUBUS_RESID_MAC_ADDRESS, &ent) == -1) {
314 dev_info(&board->dev, "MAC address resource not found\n");
318 nubus_get_rsrc_mem(addr, &ent, 6);
319 eth_hw_addr_set(dev, addr);
321 if (useresources[cardtype] == 1) {
322 nubus_rewinddir(&dir);
323 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_BASEOS,
326 "Memory offset resource not found\n");
329 nubus_get_rsrc_mem(&offset, &ent, 4);
330 dev->mem_start = dev->base_addr + offset;
331 /* yes, this is how the Apple driver does it */
332 dev->base_addr = dev->mem_start + 0x10000;
333 nubus_rewinddir(&dir);
334 if (nubus_find_rsrc(&dir, NUBUS_RESID_MINOR_LENGTH,
336 dev_info(&board->dev,
337 "Memory length resource not found, probing\n");
338 offset = mac8390_memsize(dev->mem_start);
340 nubus_get_rsrc_mem(&offset, &ent, 4);
342 dev->mem_end = dev->mem_start + offset;
345 case MAC8390_KINETICS:
346 case MAC8390_DAYNA: /* it's the same */
347 dev->base_addr = (int)(board->slot_addr +
349 dev->mem_start = (int)(board->slot_addr +
351 dev->mem_end = dev->mem_start +
352 mac8390_memsize(dev->mem_start);
354 case MAC8390_INTERLAN:
355 dev->base_addr = (int)(board->slot_addr +
357 dev->mem_start = (int)(board->slot_addr +
359 dev->mem_end = dev->mem_start +
360 mac8390_memsize(dev->mem_start);
362 case MAC8390_CABLETRON:
363 dev->base_addr = (int)(board->slot_addr +
364 CABLETRON_8390_BASE);
365 dev->mem_start = (int)(board->slot_addr +
367 /* The base address is unreadable if 0x00
368 * has been written to the command register
369 * Reset the chip by writing E8390_NODMA +
370 * E8390_PAGE0 + E8390_STOP just to be
373 i = (void *)dev->base_addr;
375 dev->mem_end = dev->mem_start +
376 mac8390_memsize(dev->mem_start);
381 "No known base address for card type\n");
389 static int mac8390_device_probe(struct nubus_board *board)
391 struct net_device *dev;
393 struct nubus_rsrc *fres;
394 enum mac8390_type cardtype = MAC8390_NONE;
396 dev = ____alloc_ei_netdev(0);
400 SET_NETDEV_DEV(dev, &board->dev);
402 for_each_board_func_rsrc(board, fres) {
403 if (fres->category != NUBUS_CAT_NETWORK ||
404 fres->type != NUBUS_TYPE_ETHERNET)
407 cardtype = mac8390_ident(fres);
408 if (cardtype == MAC8390_NONE)
411 if (mac8390_rsrc_init(dev, fres, cardtype))
417 err = mac8390_initdev(dev, board, cardtype);
421 err = register_netdev(dev);
425 nubus_set_drvdata(board, dev);
433 static void mac8390_device_remove(struct nubus_board *board)
435 struct net_device *dev = nubus_get_drvdata(board);
437 unregister_netdev(dev);
441 static struct nubus_driver mac8390_driver = {
442 .probe = mac8390_device_probe,
443 .remove = mac8390_device_remove,
445 .name = KBUILD_MODNAME,
446 .owner = THIS_MODULE,
451 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
452 MODULE_LICENSE("GPL");
454 static int __init mac8390_init(void)
456 return nubus_driver_register(&mac8390_driver);
458 module_init(mac8390_init);
460 static void __exit mac8390_exit(void)
462 nubus_driver_unregister(&mac8390_driver);
464 module_exit(mac8390_exit);
466 static const struct net_device_ops mac8390_netdev_ops = {
467 .ndo_open = mac8390_open,
468 .ndo_stop = mac8390_close,
469 .ndo_start_xmit = __ei_start_xmit,
470 .ndo_tx_timeout = __ei_tx_timeout,
471 .ndo_get_stats = __ei_get_stats,
472 .ndo_set_rx_mode = __ei_set_multicast_list,
473 .ndo_validate_addr = eth_validate_addr,
474 .ndo_set_mac_address = eth_mac_addr,
475 #ifdef CONFIG_NET_POLL_CONTROLLER
476 .ndo_poll_controller = __ei_poll,
480 static int mac8390_initdev(struct net_device *dev, struct nubus_board *board,
481 enum mac8390_type type)
483 static u32 fwrd4_offsets[16] = {
489 static u32 back4_offsets[16] = {
495 static u32 fwrd2_offsets[16] = {
502 int access_bitmode = 0;
504 /* Now fill in our stuff */
505 dev->netdev_ops = &mac8390_netdev_ops;
507 /* GAR, ei_status is actually a macro even though it looks global */
508 ei_status.name = cardname[type];
509 ei_status.word16 = word16[type];
511 /* Cabletron's TX/RX buffers are backwards */
512 if (type == MAC8390_CABLETRON) {
513 ei_status.tx_start_page = CABLETRON_TX_START_PG;
514 ei_status.rx_start_page = CABLETRON_RX_START_PG;
515 ei_status.stop_page = CABLETRON_RX_STOP_PG;
516 ei_status.rmem_start = dev->mem_start;
517 ei_status.rmem_end = dev->mem_start + CABLETRON_RX_STOP_PG*256;
519 ei_status.tx_start_page = WD_START_PG;
520 ei_status.rx_start_page = WD_START_PG + TX_PAGES;
521 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
522 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
523 ei_status.rmem_end = dev->mem_end;
526 /* Fill in model-specific information and functions */
528 case MAC8390_FARALLON:
530 switch (mac8390_testio(dev->mem_start)) {
533 "Don't know how to access card memory\n");
537 /* 16 bit card, register map is reversed */
538 ei_status.reset_8390 = mac8390_no_reset;
539 ei_status.block_input = slow_sane_block_input;
540 ei_status.block_output = slow_sane_block_output;
541 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
542 ei_status.reg_offset = back4_offsets;
546 /* 32 bit card, register map is reversed */
547 ei_status.reset_8390 = mac8390_no_reset;
548 ei_status.block_input = sane_block_input;
549 ei_status.block_output = sane_block_output;
550 ei_status.get_8390_hdr = sane_get_8390_hdr;
551 ei_status.reg_offset = back4_offsets;
558 /* Some Asante cards pass the 32 bit test
559 * but overwrite system memory when run at 32 bit.
560 * so we run them all at 16 bit.
562 ei_status.reset_8390 = mac8390_no_reset;
563 ei_status.block_input = slow_sane_block_input;
564 ei_status.block_output = slow_sane_block_output;
565 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
566 ei_status.reg_offset = back4_offsets;
569 case MAC8390_CABLETRON:
570 /* 16 bit card, register map is short forward */
571 ei_status.reset_8390 = mac8390_no_reset;
572 ei_status.block_input = slow_sane_block_input;
573 ei_status.block_output = slow_sane_block_output;
574 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
575 ei_status.reg_offset = fwrd2_offsets;
579 case MAC8390_KINETICS:
580 /* 16 bit memory, register map is forward */
581 /* dayna and similar */
582 ei_status.reset_8390 = mac8390_no_reset;
583 ei_status.block_input = dayna_block_input;
584 ei_status.block_output = dayna_block_output;
585 ei_status.get_8390_hdr = dayna_get_8390_hdr;
586 ei_status.reg_offset = fwrd4_offsets;
589 case MAC8390_INTERLAN:
590 /* 16 bit memory, register map is forward */
591 ei_status.reset_8390 = interlan_reset;
592 ei_status.block_input = slow_sane_block_input;
593 ei_status.block_output = slow_sane_block_output;
594 ei_status.get_8390_hdr = slow_sane_get_8390_hdr;
595 ei_status.reg_offset = fwrd4_offsets;
599 dev_err(&board->dev, "Unsupported card type\n");
603 __NS8390_init(dev, 0);
605 /* Good, done, now spit out some messages */
606 dev_info(&board->dev, "%s (type %s)\n", board->name, cardname[type]);
607 dev_info(&board->dev, "MAC %pM, IRQ %d, %d KB shared memory at %#lx, %d-bit access.\n",
608 dev->dev_addr, dev->irq,
609 (unsigned int)(dev->mem_end - dev->mem_start) >> 10,
610 dev->mem_start, access_bitmode ? 32 : 16);
614 static int mac8390_open(struct net_device *dev)
619 err = request_irq(dev->irq, __ei_interrupt, 0, "8390 Ethernet", dev);
621 pr_err("%s: unable to get IRQ %d\n", dev->name, dev->irq);
625 static int mac8390_close(struct net_device *dev)
627 free_irq(dev->irq, dev);
632 static void mac8390_no_reset(struct net_device *dev)
634 struct ei_device *ei_local = netdev_priv(dev);
637 netif_info(ei_local, hw, dev, "reset not supported\n");
640 static void interlan_reset(struct net_device *dev)
642 unsigned char *target = nubus_slot_addr(IRQ2SLOT(dev->irq));
643 struct ei_device *ei_local = netdev_priv(dev);
645 netif_info(ei_local, hw, dev, "Need to reset the NS8390 t=%lu...",
649 if (netif_msg_hw(ei_local))
650 pr_cont("reset complete\n");
653 /* dayna_memcpy_fromio/dayna_memcpy_toio */
654 /* directly from daynaport.c by Alan Cox */
655 static void dayna_memcpy_fromcard(struct net_device *dev, void *to, int from,
658 volatile unsigned char *ptr;
659 unsigned char *target = to;
660 from <<= 1; /* word, skip overhead */
661 ptr = (unsigned char *)(dev->mem_start+from);
669 *(unsigned short *)target = *(unsigned short volatile *)ptr;
670 ptr += 4; /* skip cruft */
679 static void dayna_memcpy_tocard(struct net_device *dev, int to,
680 const void *from, int count)
682 volatile unsigned short *ptr;
683 const unsigned char *src = from;
684 to <<= 1; /* word, skip overhead */
685 ptr = (unsigned short *)(dev->mem_start+to);
687 if (to & 2) { /* avoid a byte write (stomps on other data) */
688 ptr[-1] = (ptr[-1]&0xFF00)|*src++;
693 *ptr++ = *(unsigned short *)src; /* Copy and */
694 ptr++; /* skip cruft */
700 /* card doesn't like byte writes */
701 *ptr = (*ptr & 0x00FF) | (*src << 8);
705 /* sane block input/output */
706 static void sane_get_8390_hdr(struct net_device *dev,
707 struct e8390_pkt_hdr *hdr, int ring_page)
709 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
710 memcpy_fromio(hdr, (void __iomem *)dev->mem_start + hdr_start, 4);
712 hdr->count = swab16(hdr->count);
715 static void sane_block_input(struct net_device *dev, int count,
716 struct sk_buff *skb, int ring_offset)
718 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
719 unsigned long xfer_start = xfer_base + dev->mem_start;
721 if (xfer_start + count > ei_status.rmem_end) {
722 /* We must wrap the input move. */
723 int semi_count = ei_status.rmem_end - xfer_start;
724 memcpy_fromio(skb->data,
725 (void __iomem *)dev->mem_start + xfer_base,
728 memcpy_fromio(skb->data + semi_count,
729 (void __iomem *)ei_status.rmem_start, count);
731 memcpy_fromio(skb->data,
732 (void __iomem *)dev->mem_start + xfer_base,
737 static void sane_block_output(struct net_device *dev, int count,
738 const unsigned char *buf, int start_page)
740 long shmem = (start_page - WD_START_PG)<<8;
742 memcpy_toio((void __iomem *)dev->mem_start + shmem, buf, count);
745 /* dayna block input/output */
746 static void dayna_get_8390_hdr(struct net_device *dev,
747 struct e8390_pkt_hdr *hdr, int ring_page)
749 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
751 dayna_memcpy_fromcard(dev, hdr, hdr_start, 4);
753 hdr->count = (hdr->count & 0xFF) << 8 | (hdr->count >> 8);
756 static void dayna_block_input(struct net_device *dev, int count,
757 struct sk_buff *skb, int ring_offset)
759 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
760 unsigned long xfer_start = xfer_base+dev->mem_start;
762 /* Note the offset math is done in card memory space which is word
763 per long onto our space. */
765 if (xfer_start + count > ei_status.rmem_end) {
766 /* We must wrap the input move. */
767 int semi_count = ei_status.rmem_end - xfer_start;
768 dayna_memcpy_fromcard(dev, skb->data, xfer_base, semi_count);
770 dayna_memcpy_fromcard(dev, skb->data + semi_count,
771 ei_status.rmem_start - dev->mem_start,
774 dayna_memcpy_fromcard(dev, skb->data, xfer_base, count);
778 static void dayna_block_output(struct net_device *dev, int count,
779 const unsigned char *buf,
782 long shmem = (start_page - WD_START_PG)<<8;
784 dayna_memcpy_tocard(dev, shmem, buf, count);
787 /* Cabletron block I/O */
788 static void slow_sane_get_8390_hdr(struct net_device *dev,
789 struct e8390_pkt_hdr *hdr,
792 unsigned long hdr_start = (ring_page - WD_START_PG)<<8;
793 word_memcpy_fromcard(hdr, dev->mem_start + hdr_start, 4);
794 /* Register endianism - fix here rather than 8390.c */
795 hdr->count = (hdr->count&0xFF)<<8|(hdr->count>>8);
798 static void slow_sane_block_input(struct net_device *dev, int count,
799 struct sk_buff *skb, int ring_offset)
801 unsigned long xfer_base = ring_offset - (WD_START_PG<<8);
802 unsigned long xfer_start = xfer_base+dev->mem_start;
804 if (xfer_start + count > ei_status.rmem_end) {
805 /* We must wrap the input move. */
806 int semi_count = ei_status.rmem_end - xfer_start;
807 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
810 word_memcpy_fromcard(skb->data + semi_count,
811 ei_status.rmem_start, count);
813 word_memcpy_fromcard(skb->data, dev->mem_start + xfer_base,
818 static void slow_sane_block_output(struct net_device *dev, int count,
819 const unsigned char *buf, int start_page)
821 long shmem = (start_page - WD_START_PG)<<8;
823 word_memcpy_tocard(dev->mem_start + shmem, buf, count);
826 static void word_memcpy_tocard(unsigned long tp, const void *fp, int count)
828 volatile unsigned short *to = (void *)tp;
829 const unsigned short *from = fp;
838 static void word_memcpy_fromcard(void *tp, unsigned long fp, int count)
840 unsigned short *to = tp;
841 const volatile unsigned short *from = (const void *)fp;