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 */
18 #include <linux/version.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/fcntl.h>
23 #include <linux/interrupt.h>
24 #include <linux/ptrace.h>
25 #include <linux/ioport.h>
26 #include <linux/nubus.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/bitops.h>
37 #include <asm/system.h>
40 #include <asm/hwtest.h>
41 #include <asm/macints.h>
45 #define WD_START_PG 0x00 /* First page of TX buffer */
46 #define CABLETRON_RX_START_PG 0x00 /* First page of RX buffer */
47 #define CABLETRON_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
48 #define CABLETRON_TX_START_PG CABLETRON_RX_STOP_PG /* First page of TX buffer */
50 /* Unfortunately it seems we have to hardcode these for the moment */
51 /* 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? */
53 #define DAYNA_8390_BASE 0x80000
54 #define DAYNA_8390_MEM 0x00000
56 #define KINETICS_8390_BASE 0x80000
57 #define KINETICS_8390_MEM 0x00000
59 #define CABLETRON_8390_BASE 0x90000
60 #define CABLETRON_8390_MEM 0x00000
66 MAC8390_FARALLON
, /* Apple, Asante, and Farallon are all compatible */
77 static const char * cardname
[] = {
91 static int word16
[] = {
100 1, /* sonic systems */
105 /* on which cards do we use NuBus resources? */
106 static int useresources
[] = {
115 1, /* sonic systems */
120 static char version
[] __initdata
=
121 "mac8390.c: v0.4 2001-05-15 David Huggins-Daines <dhd@debian.org> and others\n";
123 extern enum mac8390_type
mac8390_ident(struct nubus_dev
* dev
);
124 extern int mac8390_memsize(unsigned long membase
);
125 extern int mac8390_memtest(struct net_device
* dev
);
126 static int mac8390_initdev(struct net_device
* dev
, struct nubus_dev
* ndev
,
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
);
133 /* Sane (32-bit chunk memory read/write) - Apple/Asante/Farallon do this*/
134 static void sane_get_8390_hdr(struct net_device
*dev
,
135 struct e8390_pkt_hdr
*hdr
, int ring_page
);
136 static void sane_block_input(struct net_device
* dev
, int count
,
137 struct sk_buff
* skb
, int ring_offset
);
138 static void sane_block_output(struct net_device
* dev
, int count
,
139 const unsigned char * buf
, const int start_page
);
141 /* dayna_memcpy to and from card */
142 static void dayna_memcpy_fromcard(struct net_device
*dev
, void *to
,
143 int from
, int count
);
144 static void dayna_memcpy_tocard(struct net_device
*dev
, int to
,
145 const void *from
, int count
);
147 /* Dayna - Dayna/Kinetics use this */
148 static void dayna_get_8390_hdr(struct net_device
*dev
,
149 struct e8390_pkt_hdr
*hdr
, int ring_page
);
150 static void dayna_block_input(struct net_device
*dev
, int count
,
151 struct sk_buff
*skb
, int ring_offset
);
152 static void dayna_block_output(struct net_device
*dev
, int count
,
153 const unsigned char *buf
, int start_page
);
155 #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
156 #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
158 /* Slow Sane (16-bit chunk memory read/write) Cabletron uses this */
159 static void slow_sane_get_8390_hdr(struct net_device
*dev
,
160 struct e8390_pkt_hdr
*hdr
, int ring_page
);
161 static void slow_sane_block_input(struct net_device
*dev
, int count
,
162 struct sk_buff
*skb
, int ring_offset
);
163 static void slow_sane_block_output(struct net_device
*dev
, int count
,
164 const unsigned char *buf
, int start_page
);
165 static void word_memcpy_tocard(void *tp
, const void *fp
, int count
);
166 static void word_memcpy_fromcard(void *tp
, const void *fp
, int count
);
168 enum mac8390_type __init
mac8390_ident(struct nubus_dev
* dev
)
170 if (dev
->dr_sw
== NUBUS_DRSW_ASANTE
)
171 return MAC8390_ASANTE
;
172 if (dev
->dr_sw
== NUBUS_DRSW_FARALLON
)
173 return MAC8390_FARALLON
;
174 if (dev
->dr_sw
== NUBUS_DRSW_KINETICS
)
175 return MAC8390_KINETICS
;
176 if (dev
->dr_sw
== NUBUS_DRSW_DAYNA
)
177 return MAC8390_DAYNA
;
178 if (dev
->dr_sw
== NUBUS_DRSW_DAYNA2
)
179 return MAC8390_DAYNA2
;
180 if (dev
->dr_sw
== NUBUS_DRSW_DAYNA_LC
)
181 return MAC8390_DAYNA3
;
182 if (dev
->dr_hw
== NUBUS_DRHW_CABLETRON
)
183 return MAC8390_CABLETRON
;
187 int __init
mac8390_memsize(unsigned long membase
)
192 local_irq_save(flags
);
193 /* Check up to 32K in 4K increments */
194 for (i
= 0; i
< 8; i
++) {
195 volatile unsigned short *m
= (unsigned short *) (membase
+ (i
* 0x1000));
197 /* Unwriteable - we have a fully decoded card and the
199 if (hwreg_present(m
) == 0)
202 /* write a distinctive byte */
204 /* check that we read back what we wrote */
205 if (*m
!= (0xA5A0 | i
))
208 /* check for partial decode and wrap */
209 for (j
= 0; j
< i
; j
++) {
210 volatile unsigned short *p
= (unsigned short *) (membase
+ (j
* 0x1000));
211 if (*p
!= (0xA5A0 | j
))
215 local_irq_restore(flags
);
216 /* in any case, we stopped once we tried one block too many,
217 or once we reached 32K */
221 struct net_device
* __init
mac8390_probe(int unit
)
223 struct net_device
*dev
;
224 volatile unsigned short *i
;
225 int version_disp
= 0;
226 struct nubus_dev
* ndev
= NULL
;
229 struct nubus_dir dir
;
230 struct nubus_dirent ent
;
232 static unsigned int slots
;
234 enum mac8390_type cardtype
;
236 /* probably should check for Nubus instead */
239 return ERR_PTR(-ENODEV
);
241 dev
= alloc_ei_netdev();
243 return ERR_PTR(-ENOMEM
);
246 sprintf(dev
->name
, "eth%d", unit
);
248 SET_MODULE_OWNER(dev
);
250 while ((ndev
= nubus_find_type(NUBUS_CAT_NETWORK
, NUBUS_TYPE_ETHERNET
, ndev
))) {
251 /* Have we seen it already? */
252 if (slots
& (1<<ndev
->board
->slot
))
254 slots
|= 1<<ndev
->board
->slot
;
256 if ((cardtype
= mac8390_ident(ndev
)) == MAC8390_NONE
)
259 if (version_disp
== 0) {
264 dev
->irq
= SLOT2IRQ(ndev
->board
->slot
);
265 /* This is getting to be a habit */
266 dev
->base_addr
= ndev
->board
->slot_addr
| ((ndev
->board
->slot
&0xf) << 20);
268 /* Get some Nubus info - we will trust the card's idea
269 of where its memory and registers are. */
271 if (nubus_get_func_dir(ndev
, &dir
) == -1) {
272 printk(KERN_ERR
"%s: Unable to get Nubus functional"
273 " directory for slot %X!\n",
274 dev
->name
, ndev
->board
->slot
);
278 /* Get the MAC address */
279 if ((nubus_find_rsrc(&dir
, NUBUS_RESID_MAC_ADDRESS
, &ent
)) == -1) {
280 printk(KERN_INFO
"%s: Couldn't get MAC address!\n",
284 nubus_get_rsrc_mem(dev
->dev_addr
, &ent
, 6);
285 /* Some Sonic Sys cards masquerade as Farallon */
286 if (cardtype
== MAC8390_FARALLON
&&
287 dev
->dev_addr
[0] == 0x0 &&
288 dev
->dev_addr
[1] == 0x40 &&
289 dev
->dev_addr
[2] == 0x10) {
290 /* This is really Sonic Sys card */
291 cardtype
= MAC8390_SONICSYS
;
295 if (useresources
[cardtype
] == 1) {
296 nubus_rewinddir(&dir
);
297 if (nubus_find_rsrc(&dir
, NUBUS_RESID_MINOR_BASEOS
, &ent
) == -1) {
298 printk(KERN_ERR
"%s: Memory offset resource"
299 " for slot %X not found!\n",
300 dev
->name
, ndev
->board
->slot
);
303 nubus_get_rsrc_mem(&offset
, &ent
, 4);
304 dev
->mem_start
= dev
->base_addr
+ offset
;
305 /* yes, this is how the Apple driver does it */
306 dev
->base_addr
= dev
->mem_start
+ 0x10000;
307 nubus_rewinddir(&dir
);
308 if (nubus_find_rsrc(&dir
, NUBUS_RESID_MINOR_LENGTH
, &ent
) == -1) {
309 printk(KERN_INFO
"%s: Memory length resource"
310 " for slot %X not found"
312 dev
->name
, ndev
->board
->slot
);
313 offset
= mac8390_memsize(dev
->mem_start
);
315 nubus_get_rsrc_mem(&offset
, &ent
, 4);
317 dev
->mem_end
= dev
->mem_start
+ offset
;
320 case MAC8390_KINETICS
:
321 case MAC8390_DAYNA
: /* it's the same */
323 (int)(ndev
->board
->slot_addr
+
326 (int)(ndev
->board
->slot_addr
+
330 mac8390_memsize(dev
->mem_start
);
332 case MAC8390_CABLETRON
:
334 (int)(ndev
->board
->slot_addr
+
335 CABLETRON_8390_BASE
);
337 (int)(ndev
->board
->slot_addr
+
339 /* The base address is unreadable if 0x00
340 * has been written to the command register
341 * Reset the chip by writing E8390_NODMA +
342 * E8390_PAGE0 + E8390_STOP just to be
345 i
= (void *)dev
->base_addr
;
349 mac8390_memsize(dev
->mem_start
);
353 printk(KERN_ERR
"Card type %s is"
354 " unsupported, sorry\n",
360 /* Do the nasty 8390 stuff */
361 if (!mac8390_initdev(dev
, ndev
, cardtype
))
367 err
= register_netdev(dev
);
378 MODULE_AUTHOR("David Huggins-Daines <dhd@debian.org> and others");
379 MODULE_DESCRIPTION("Macintosh NS8390-based Nubus Ethernet driver");
380 MODULE_LICENSE("GPL");
382 /* overkill, of course */
383 static struct net_device
*dev_mac8390
[15];
384 int init_module(void)
387 for (i
= 0; i
< 15; i
++) {
388 struct net_device
*dev
= mac8390_probe(-1);
394 printk(KERN_NOTICE
"mac8390.c: No useable cards found, driver NOT installed.\n");
400 void cleanup_module(void)
403 for (i
= 0; i
< 15; i
++) {
404 struct net_device
*dev
= dev_mac890
[i
];
406 unregister_netdev(dev
);
414 static int __init
mac8390_initdev(struct net_device
* dev
, struct nubus_dev
* ndev
,
415 enum mac8390_type type
)
417 static u32 fwrd4_offsets
[16]={
423 static u32 back4_offsets
[16]={
429 static u32 fwrd2_offsets
[16]={
438 /* Now fill in our stuff */
439 dev
->open
= &mac8390_open
;
440 dev
->stop
= &mac8390_close
;
441 #ifdef CONFIG_NET_POLL_CONTROLLER
442 dev
->poll_controller
= ei_poll
;
445 /* GAR, ei_status is actually a macro even though it looks global */
446 ei_status
.name
= cardname
[type
];
447 ei_status
.word16
= word16
[type
];
449 /* Cabletron's TX/RX buffers are backwards */
450 if (type
== MAC8390_CABLETRON
) {
451 ei_status
.tx_start_page
= CABLETRON_TX_START_PG
;
452 ei_status
.rx_start_page
= CABLETRON_RX_START_PG
;
453 ei_status
.stop_page
= CABLETRON_RX_STOP_PG
;
454 ei_status
.rmem_start
= dev
->mem_start
;
455 ei_status
.rmem_end
= dev
->mem_start
+ CABLETRON_RX_STOP_PG
*256;
457 ei_status
.tx_start_page
= WD_START_PG
;
458 ei_status
.rx_start_page
= WD_START_PG
+ TX_PAGES
;
459 ei_status
.stop_page
= (dev
->mem_end
- dev
->mem_start
)/256;
460 ei_status
.rmem_start
= dev
->mem_start
+ TX_PAGES
*256;
461 ei_status
.rmem_end
= dev
->mem_end
;
464 /* Fill in model-specific information and functions */
466 case MAC8390_SONICSYS
:
467 /* 16 bit card, register map is reversed */
468 ei_status
.reset_8390
= &mac8390_no_reset
;
469 ei_status
.block_input
= &slow_sane_block_input
;
470 ei_status
.block_output
= &slow_sane_block_output
;
471 ei_status
.get_8390_hdr
= &slow_sane_get_8390_hdr
;
472 ei_status
.reg_offset
= back4_offsets
;
475 case MAC8390_FARALLON
:
480 /* 32 bit card, register map is reversed */
482 ei_status
.reset_8390
= &mac8390_no_reset
;
483 ei_status
.block_input
= &sane_block_input
;
484 ei_status
.block_output
= &sane_block_output
;
485 ei_status
.get_8390_hdr
= &sane_get_8390_hdr
;
486 ei_status
.reg_offset
= back4_offsets
;
489 case MAC8390_CABLETRON
:
490 /* 16 bit card, register map is short forward */
491 ei_status
.reset_8390
= &mac8390_no_reset
;
492 ei_status
.block_input
= &slow_sane_block_input
;
493 ei_status
.block_output
= &slow_sane_block_output
;
494 ei_status
.get_8390_hdr
= &slow_sane_get_8390_hdr
;
495 ei_status
.reg_offset
= fwrd2_offsets
;
499 case MAC8390_KINETICS
:
501 /* dayna and similar */
502 ei_status
.reset_8390
= &mac8390_no_reset
;
503 ei_status
.block_input
= &dayna_block_input
;
504 ei_status
.block_output
= &dayna_block_output
;
505 ei_status
.get_8390_hdr
= &dayna_get_8390_hdr
;
506 ei_status
.reg_offset
= fwrd4_offsets
;
510 printk(KERN_ERR
"Card type %s is unsupported, sorry\n", cardname
[type
]);
516 /* Good, done, now spit out some messages */
517 printk(KERN_INFO
"%s: %s in slot %X (type %s)\n",
518 dev
->name
, ndev
->board
->name
, ndev
->board
->slot
, cardname
[type
]);
519 printk(KERN_INFO
"MAC ");
522 for (i
= 0; i
< 6; i
++) {
523 printk("%2.2x", dev
->dev_addr
[i
]);
528 printk(" IRQ %d, shared memory at %#lx-%#lx, %d-bit access.\n",
529 dev
->irq
, dev
->mem_start
, dev
->mem_end
-1,
530 access_bitmode
?32:16);
534 static int mac8390_open(struct net_device
*dev
)
537 if (request_irq(dev
->irq
, ei_interrupt
, 0, "8390 Ethernet", dev
)) {
538 printk ("%s: unable to get IRQ %d.\n", dev
->name
, dev
->irq
);
544 static int mac8390_close(struct net_device
*dev
)
546 free_irq(dev
->irq
, dev
);
551 static void mac8390_no_reset(struct net_device
*dev
)
555 printk("reset not supported\n");
559 /* dayna_memcpy_fromio/dayna_memcpy_toio */
560 /* directly from daynaport.c by Alan Cox */
561 static void dayna_memcpy_fromcard(struct net_device
*dev
, void *to
, int from
, int count
)
563 volatile unsigned short *ptr
;
564 unsigned short *target
=to
;
565 from
<<=1; /* word, skip overhead */
566 ptr
=(unsigned short *)(dev
->mem_start
+from
);
569 *((char *)target
)++ = *(((char *)ptr
++)-1);
574 *target
++=*ptr
++; /* Copy and */
575 ptr
++; /* skip cruft */
582 unsigned short v
=*ptr
;
583 *((char *)target
)=v
>>8;
587 static void dayna_memcpy_tocard(struct net_device
*dev
, int to
, const void *from
, int count
)
589 volatile unsigned short *ptr
;
590 const unsigned short *src
=from
;
591 to
<<=1; /* word, skip overhead */
592 ptr
=(unsigned short *)(dev
->mem_start
+to
);
594 if (to
&2) { /* avoid a byte write (stomps on other data) */
595 ptr
[-1] = (ptr
[-1]&0xFF00)|*((unsigned char *)src
)++;
601 *ptr
++=*src
++; /* Copy and */
602 ptr
++; /* skip cruft */
609 unsigned short v
=*src
;
610 /* card doesn't like byte writes */
611 *ptr
=(*ptr
&0x00FF)|(v
&0xFF00);
615 /* sane block input/output */
616 static void sane_get_8390_hdr(struct net_device
*dev
,
617 struct e8390_pkt_hdr
*hdr
, int ring_page
)
619 unsigned long hdr_start
= (ring_page
- WD_START_PG
)<<8;
620 memcpy_fromio((void *)hdr
, (char *)dev
->mem_start
+ hdr_start
, 4);
622 hdr
->count
= swab16(hdr
->count
);
625 static void sane_block_input(struct net_device
*dev
, int count
,
626 struct sk_buff
*skb
, int ring_offset
)
628 unsigned long xfer_base
= ring_offset
- (WD_START_PG
<<8);
629 unsigned long xfer_start
= xfer_base
+ dev
->mem_start
;
631 if (xfer_start
+ count
> ei_status
.rmem_end
) {
632 /* We must wrap the input move. */
633 int semi_count
= ei_status
.rmem_end
- xfer_start
;
634 memcpy_fromio(skb
->data
, (char *)dev
->mem_start
+ xfer_base
, semi_count
);
636 memcpy_toio(skb
->data
+ semi_count
, (char *)ei_status
.rmem_start
, count
);
638 memcpy_fromio(skb
->data
, (char *)dev
->mem_start
+ xfer_base
, count
);
642 static void sane_block_output(struct net_device
*dev
, int count
,
643 const unsigned char *buf
, int start_page
)
645 long shmem
= (start_page
- WD_START_PG
)<<8;
647 memcpy_toio((char *)dev
->mem_start
+ shmem
, buf
, count
);
650 /* dayna block input/output */
651 static void dayna_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
, int ring_page
)
653 unsigned long hdr_start
= (ring_page
- WD_START_PG
)<<8;
655 dayna_memcpy_fromcard(dev
, (void *)hdr
, hdr_start
, 4);
657 hdr
->count
=(hdr
->count
&0xFF)<<8|(hdr
->count
>>8);
660 static void dayna_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
, int ring_offset
)
662 unsigned long xfer_base
= ring_offset
- (WD_START_PG
<<8);
663 unsigned long xfer_start
= xfer_base
+dev
->mem_start
;
665 /* Note the offset math is done in card memory space which is word
666 per long onto our space. */
668 if (xfer_start
+ count
> ei_status
.rmem_end
)
670 /* We must wrap the input move. */
671 int semi_count
= ei_status
.rmem_end
- xfer_start
;
672 dayna_memcpy_fromcard(dev
, skb
->data
, xfer_base
, semi_count
);
674 dayna_memcpy_fromcard(dev
, skb
->data
+ semi_count
,
675 ei_status
.rmem_start
- dev
->mem_start
,
680 dayna_memcpy_fromcard(dev
, skb
->data
, xfer_base
, count
);
684 static void dayna_block_output(struct net_device
*dev
, int count
, const unsigned char *buf
,
687 long shmem
= (start_page
- WD_START_PG
)<<8;
689 dayna_memcpy_tocard(dev
, shmem
, buf
, count
);
692 /* Cabletron block I/O */
693 static void slow_sane_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
696 unsigned long hdr_start
= (ring_page
- WD_START_PG
)<<8;
697 word_memcpy_fromcard((void *)hdr
, (char *)dev
->mem_start
+hdr_start
, 4);
698 /* Register endianism - fix here rather than 8390.c */
699 hdr
->count
= (hdr
->count
&0xFF)<<8|(hdr
->count
>>8);
702 static void slow_sane_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
,
705 unsigned long xfer_base
= ring_offset
- (WD_START_PG
<<8);
706 unsigned long xfer_start
= xfer_base
+dev
->mem_start
;
708 if (xfer_start
+ count
> ei_status
.rmem_end
)
710 /* We must wrap the input move. */
711 int semi_count
= ei_status
.rmem_end
- xfer_start
;
712 word_memcpy_fromcard(skb
->data
, (char *)dev
->mem_start
+
713 xfer_base
, semi_count
);
715 word_memcpy_fromcard(skb
->data
+ semi_count
,
716 (char *)ei_status
.rmem_start
, count
);
720 word_memcpy_fromcard(skb
->data
, (char *)dev
->mem_start
+
725 static void slow_sane_block_output(struct net_device
*dev
, int count
, const unsigned char *buf
,
728 long shmem
= (start_page
- WD_START_PG
)<<8;
730 word_memcpy_tocard((char *)dev
->mem_start
+ shmem
, buf
, count
);
733 static void word_memcpy_tocard(void *tp
, const void *fp
, int count
)
735 volatile unsigned short *to
= tp
;
736 const unsigned short *from
= fp
;
745 static void word_memcpy_fromcard(void *tp
, const void *fp
, int count
)
747 unsigned short *to
= tp
;
748 const volatile unsigned short *from
= fp
;