1 /* $Id: bagetlance.c,v 1.1.1.1 2005/04/11 02:50:26 jack Exp $
2 * bagetlance.c: Ethernet driver for VME Lance cards on Baget/MIPS
3 * This code stealed and adopted from linux/drivers/net/atarilance.c
4 * See that for author info
6 * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
10 * Driver code for Baget/Lance taken from atarilance.c, which also
11 * works well in case of Besta. Most significant changes made here
12 * related with 16BIT-only access to A24 space.
15 static char *version = "bagetlance.c: v1.1 11/10/98\n";
17 #include <linux/module.h>
19 #include <linux/stddef.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
30 #include <asm/bitops.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
37 #include <asm/baget/baget.h>
39 #define BAGET_LANCE_IRQ BAGET_IRQ_MASK(0xdf)
42 * Define following if you don't need 16BIT-only access to Lance memory
43 * (Normally BAGET needs it)
45 #undef NORMAL_MEM_ACCESS
48 * 0 = silent, print only serious errors
49 * 1 = normal, print error messages
50 * 2 = debug, print debug infos
51 * 3 = debug, print even more debug infos (packet data)
57 static int lance_debug = LANCE_DEBUG;
59 static int lance_debug = 1;
61 MODULE_PARM(lance_debug, "i");
62 MODULE_PARM_DESC(lance_debug, "Lance debug level (0-3)");
63 MODULE_LICENSE("GPL");
65 /* Print debug messages on probing? */
66 #undef LANCE_DEBUG_PROBE
68 #define DPRINTK(n,a) \
70 if (lance_debug >= n) \
74 #ifdef LANCE_DEBUG_PROBE
75 # define PROBE_PRINT(a) printk a
77 # define PROBE_PRINT(a)
80 /* These define the number of Rx and Tx buffers as log2. (Only powers
82 * Much more rx buffers (32) are reserved than tx buffers (8), since receiving
83 * is more time critical then sending and packets may have to remain in the
84 * board's memory when main memory is low.
87 /* Baget Lance has 64K on-board memory, so it looks we can't increase
88 buffer quantity (40*1.5K is about 64K) */
90 #define TX_LOG_RING_SIZE 3
91 #define RX_LOG_RING_SIZE 5
93 /* These are the derived values */
95 #define TX_RING_SIZE (1 << TX_LOG_RING_SIZE)
96 #define TX_RING_LEN_BITS (TX_LOG_RING_SIZE << 5)
97 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
99 #define RX_RING_SIZE (1 << RX_LOG_RING_SIZE)
100 #define RX_RING_LEN_BITS (RX_LOG_RING_SIZE << 5)
101 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
103 /* The LANCE Rx and Tx ring descriptors. */
104 struct lance_rx_head {
105 volatile unsigned short base; /* Low word of base addr */
106 #ifdef NORMAL_MEM_ACCESS
107 /* Following two fields are joined into one short to guarantee
108 16BIT access to Baget lance registers */
109 volatile unsigned char flag;
110 unsigned char base_hi; /* High word of base addr (unused) */
112 /* Following macros are used as replecements to 8BIT fields */
113 #define GET_FLAG(h) (((h)->flag_base_hi >> 8) & 0xff)
114 #define SET_FLAG(h,f) (h)->flag_base_hi = ((h)->flag_base_hi & 0xff) | \
115 (((unsigned)(f)) << 8)
116 volatile unsigned short flag_base_hi;
118 volatile short buf_length; /* This length is 2s complement! */
119 volatile short msg_length; /* This length is "normal". */
123 struct lance_tx_head {
124 volatile unsigned short base; /* Low word of base addr */
125 #ifdef NORMAL_MEM_ACCESS
126 /* See comments above about 8BIT-access Baget A24-space problems */
127 volatile unsigned char flag;
128 unsigned char base_hi; /* High word of base addr (unused) */
130 volatile unsigned short flag_base_hi;
132 volatile short length; /* Length is 2s complement! */
137 volatile unsigned short adr_lo; /* Low 16 bits of address */
138 #ifdef NORMAL_MEM_ACCESS
139 /* See comments above about 8BIT-access Bage A24-space problems */
140 unsigned char len; /* Length bits */
141 unsigned char adr_hi; /* High 8 bits of address (unused) */
143 volatile unsigned short len_adr_hi;
147 /* The LANCE initialization block, described in databook. */
148 struct lance_init_block {
149 unsigned short mode; /* Pre-set mode */
150 unsigned char hwaddr[6]; /* Physical ethernet address */
151 unsigned filter[2]; /* Multicast filter (unused). */
152 /* Receive and transmit ring base, along with length bits. */
153 struct ringdesc rx_ring;
154 struct ringdesc tx_ring;
157 /* The whole layout of the Lance shared memory */
158 struct lance_memory {
159 struct lance_init_block init;
160 struct lance_tx_head tx_head[TX_RING_SIZE];
161 struct lance_rx_head rx_head[RX_RING_SIZE];
162 char packet_area[0]; /* packet data follow after the
163 * init block and the ring
164 * descriptors and are located
168 /* RieblCard specifics:
169 * The original TOS driver for these cards reserves the area from offset
170 * 0xee70 to 0xeebb for storing configuration data. Of interest to us is the
171 * Ethernet address there, and the magic for verifying the data's validity.
172 * The reserved area isn't touch by packet buffers. Furthermore, offset 0xfffe
173 * is reserved for the interrupt vector number.
175 #define RIEBL_RSVD_START 0xee70
176 #define RIEBL_RSVD_END 0xeec0
177 #define RIEBL_MAGIC 0x09051990
178 #define RIEBL_MAGIC_ADDR ((unsigned long *)(((char *)MEM) + 0xee8a))
179 #define RIEBL_HWADDR_ADDR ((unsigned char *)(((char *)MEM) + 0xee8e))
180 #define RIEBL_IVEC_ADDR ((unsigned short *)(((char *)MEM) + 0xfffe))
182 /* This is a default address for the old RieblCards without a battery
183 * that have no ethernet address at boot time. 00:00:36:04 is the
184 * prefix for Riebl cards, the 00:00 at the end is arbitrary.
187 static unsigned char OldRieblDefHwaddr[6] = {
188 0x00, 0x00, 0x36, 0x04, 0x00, 0x00
191 /* I/O registers of the Lance chip */
194 /* base+0x0 */ volatile unsigned short data;
195 /* base+0x2 */ volatile unsigned short addr;
196 unsigned char _dummy1[3];
197 /* base+0x7 */ volatile unsigned char ivec;
198 unsigned char _dummy2[5];
199 /* base+0xd */ volatile unsigned char eeprom;
200 unsigned char _dummy3;
201 /* base+0xf */ volatile unsigned char mem;
204 /* Types of boards this driver supports */
207 OLD_RIEBL, /* old Riebl card without battery */
208 NEW_RIEBL, /* new Riebl card with battery */
209 PAM_CARD /* PAM card with EEPROM */
212 static char *lance_names[] = {
213 "Riebl-Card (without battery)",
214 "Riebl-Card (with battery)",
218 /* The driver's private device structure */
220 struct lance_private {
221 enum lance_type cardtype;
222 struct lance_ioreg *iobase;
223 struct lance_memory *mem;
224 int cur_rx, cur_tx; /* The next free ring entry */
225 int dirty_tx; /* Ring entries to be freed. */
227 void *(*memcpy_f)( void *, const void *, size_t );
228 struct net_device_stats stats;
229 /* These two must be longs for set_bit() */
234 /* I/O register access macros */
237 #define DREG IO->data
238 #define AREG IO->addr
239 #define REGA(a) ( AREG = (a), DREG )
241 /* Definitions for packet buffer access: */
242 #define PKT_BUF_SZ 1544
243 /* Get the address of a packet buffer corresponding to a given buffer head */
244 #define PKTBUF_ADDR(head) (((unsigned char *)(MEM)) + (head)->base)
246 /* Possible memory/IO addresses for probing */
249 unsigned long memaddr;
250 unsigned long ioaddr;
252 } lance_addr_list[] = {
253 { BAGET_LANCE_MEM_BASE, BAGET_LANCE_IO_BASE, 1 } /* Baget Lance */
256 #define N_LANCE_ADDR (sizeof(lance_addr_list)/sizeof(*lance_addr_list))
259 #define LANCE_HI_BASE (0xff & (BAGET_LANCE_MEM_BASE >> 16))
261 /* Definitions for the Lance */
264 #define TMD1_ENP 0x01 /* end of packet */
265 #define TMD1_STP 0x02 /* start of packet */
266 #define TMD1_DEF 0x04 /* deferred */
267 #define TMD1_ONE 0x08 /* one retry needed */
268 #define TMD1_MORE 0x10 /* more than one retry needed */
269 #define TMD1_ERR 0x40 /* error summary */
270 #define TMD1_OWN 0x80 /* ownership (set: chip owns) */
272 #define TMD1_OWN_CHIP TMD1_OWN
273 #define TMD1_OWN_HOST 0
275 /* tx_head misc field */
276 #define TMD3_TDR 0x03FF /* Time Domain Reflectometry counter */
277 #define TMD3_RTRY 0x0400 /* failed after 16 retries */
278 #define TMD3_LCAR 0x0800 /* carrier lost */
279 #define TMD3_LCOL 0x1000 /* late collision */
280 #define TMD3_UFLO 0x4000 /* underflow (late memory) */
281 #define TMD3_BUFF 0x8000 /* buffering error (no ENP) */
284 #define RMD1_ENP 0x01 /* end of packet */
285 #define RMD1_STP 0x02 /* start of packet */
286 #define RMD1_BUFF 0x04 /* buffer error */
287 #define RMD1_CRC 0x08 /* CRC error */
288 #define RMD1_OFLO 0x10 /* overflow */
289 #define RMD1_FRAM 0x20 /* framing error */
290 #define RMD1_ERR 0x40 /* error summary */
291 #define RMD1_OWN 0x80 /* ownership (set: ship owns) */
293 #define RMD1_OWN_CHIP RMD1_OWN
294 #define RMD1_OWN_HOST 0
297 #define CSR0 0 /* mode/status */
298 #define CSR1 1 /* init block addr (low) */
299 #define CSR2 2 /* init block addr (high) */
300 #define CSR3 3 /* misc */
301 #define CSR8 8 /* address filter */
302 #define CSR15 15 /* promiscuous mode */
305 /* (R=readable, W=writeable, S=set on write, C=clear on write) */
306 #define CSR0_INIT 0x0001 /* initialize (RS) */
307 #define CSR0_STRT 0x0002 /* start (RS) */
308 #define CSR0_STOP 0x0004 /* stop (RS) */
309 #define CSR0_TDMD 0x0008 /* transmit demand (RS) */
310 #define CSR0_TXON 0x0010 /* transmitter on (R) */
311 #define CSR0_RXON 0x0020 /* receiver on (R) */
312 #define CSR0_INEA 0x0040 /* interrupt enable (RW) */
313 #define CSR0_INTR 0x0080 /* interrupt active (R) */
314 #define CSR0_IDON 0x0100 /* initialization done (RC) */
315 #define CSR0_TINT 0x0200 /* transmitter interrupt (RC) */
316 #define CSR0_RINT 0x0400 /* receiver interrupt (RC) */
317 #define CSR0_MERR 0x0800 /* memory error (RC) */
318 #define CSR0_MISS 0x1000 /* missed frame (RC) */
319 #define CSR0_CERR 0x2000 /* carrier error (no heartbeat :-) (RC) */
320 #define CSR0_BABL 0x4000 /* babble: tx-ed too many bits (RC) */
321 #define CSR0_ERR 0x8000 /* error (RC) */
324 #define CSR3_BCON 0x0001 /* byte control */
325 #define CSR3_ACON 0 // fixme: 0x0002 /* ALE control */
326 #define CSR3_BSWP 0x0004 /* byte swap (1=big endian) */
330 /***************************** Prototypes *****************************/
332 static int addr_accessible( volatile void *regp, int wordflag, int
334 static int lance_probe1( struct net_device *dev, struct lance_addr *init_rec );
335 static int lance_open( struct net_device *dev );
336 static void lance_init_ring( struct net_device *dev );
337 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
338 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
339 static int lance_rx( struct net_device *dev );
340 static int lance_close( struct net_device *dev );
341 static struct net_device_stats *lance_get_stats( struct net_device *dev );
342 static void set_multicast_list( struct net_device *dev );
343 static int lance_set_mac_address( struct net_device *dev, void *addr );
345 /************************* End of Prototypes **************************/
347 /* Network traffic statistic (bytes) */
351 static void update_lance_stat (int len) {
356 This function is used to access Baget/Lance memory to avoid
357 8/32BIT access to VAC A24 space
358 ALL memcpy calls was chenged to this function to avoid dbe problems
359 Don't confuse with function name -- it stays from original code
362 void *slow_memcpy( void *dst, const void *src, size_t len )
365 unsigned long to = (unsigned long)dst;
366 unsigned long from = (unsigned long)src;
367 unsigned long to_end = to +len;
369 /* Unaligned flags */
371 int odd_from = from & 1;
373 int odd_to_end = to_end & 1;
375 /* Align for 16BIT-access first */
377 register unsigned short *from_a = (unsigned short*) (from & ~1);
378 register unsigned short *to_a = (unsigned short*) (to & ~1);
379 register unsigned short *to_end_a = (unsigned short*) (to_end & ~1);
381 /* Caching values -- not in loop invariant */
383 register unsigned short from_v;
384 register unsigned short to_v;
386 /* Invariant is: from_a and to_a are pointers before or exactly to
387 currently copying byte */
390 /* First byte unaligned case */
395 to_v |= 0xff & (from_v >> (odd_from ? 0 : 8));
398 if (odd_from) from_a++;
400 if (odd_from == odd_to) {
402 while (to_a + 7 < to_end_a) {
403 unsigned long dummy1, dummy2;
404 unsigned long reg1, reg2, reg3, reg4;
406 __asm__ __volatile__(
407 ".set\tnoreorder\n\t"
429 :"=r" (dummy1), "=r" (dummy2),
430 "=&r" (reg1), "=&r" (reg2), "=&r" (reg3), "=&r" (reg4)
431 :"0" (to_a), "1" (from_a)
438 while (to_a < to_end_a) {
442 /* Different parity */
444 while (to_a < to_end_a) {
445 unsigned short from_v_next;
446 from_v_next = *++from_a;
447 *to_a++ = ((from_v & 0xff)<<8) | ((from_v_next>>8) & 0xff);
448 from_v = from_v_next;
453 /* Last byte unaligned case */
458 if (odd_from == odd_to) {
459 to_v |= from_v & 0xff00;
461 to_v |= (from_v<<8) & 0xff00;
467 update_lance_stat( len );
473 int __init bagetlance_probe( struct net_device *dev )
478 SET_MODULE_OWNER(dev);
481 /* Assume there's only one board possible... That seems true, since
482 * the Riebl/PAM board's address cannot be changed. */
485 for( i = 0; i < N_LANCE_ADDR; ++i ) {
486 if (lance_probe1( dev, &lance_addr_list[i] )) {
497 /* Derived from hwreg_present() in vme/config.c: */
499 static int __init addr_accessible( volatile void *regp,
503 /* We have a fine function to do it */
504 extern int try_read(unsigned long, int);
505 return try_read((unsigned long)regp, sizeof(short)) != -1;
510 /* Original atari driver uses it */
511 #define IRQ_TYPE_PRIO SA_INTERRUPT
512 #define IRQ_SOURCE_TO_VECTOR(x) (x)
514 static int __init lance_probe1( struct net_device *dev,
515 struct lance_addr *init_rec )
517 { volatile unsigned short *memaddr =
518 (volatile unsigned short *)init_rec->memaddr;
519 volatile unsigned short *ioaddr =
520 (volatile unsigned short *)init_rec->ioaddr;
521 struct lance_private *lp;
522 struct lance_ioreg *IO;
524 static int did_version;
525 unsigned short save1, save2;
527 PROBE_PRINT(( "Probing for Lance card at mem %#lx io %#lx\n",
528 (long)memaddr, (long)ioaddr ));
530 /* Test whether memory readable and writable */
531 PROBE_PRINT(( "lance_probe1: testing memory to be accessible\n" ));
532 if (!addr_accessible( memaddr, 1, 1 )) goto probe_fail;
534 if ((unsigned long)memaddr >= KSEG2) {
535 extern int kseg2_alloc_io (unsigned long addr, unsigned long size);
536 if (kseg2_alloc_io((unsigned long)memaddr, BAGET_LANCE_MEM_SIZE)) {
537 printk("bagetlance: unable map lance memory\n");
542 /* Written values should come back... */
543 PROBE_PRINT(( "lance_probe1: testing memory to be writable (1)\n" ));
546 if (*memaddr != 0x0001) goto probe_fail;
547 PROBE_PRINT(( "lance_probe1: testing memory to be writable (2)\n" ));
549 if (*memaddr != 0x0000) goto probe_fail;
552 /* First port should be readable and writable */
553 PROBE_PRINT(( "lance_probe1: testing ioport to be accessible\n" ));
554 if (!addr_accessible( ioaddr, 1, 1 )) goto probe_fail;
556 /* and written values should be readable */
557 PROBE_PRINT(( "lance_probe1: testing ioport to be writeable\n" ));
560 if (ioaddr[1] != 0x0001) goto probe_fail;
562 /* The CSR0_INIT bit should not be readable */
563 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (1)\n" ));
566 ioaddr[0] = CSR0_INIT | CSR0_STOP;
567 if (ioaddr[0] != CSR0_STOP) {
572 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (2)\n" ));
573 ioaddr[0] = CSR0_STOP;
574 if (ioaddr[0] != CSR0_STOP) {
581 PROBE_PRINT(( "lance_probe1: Lance card detected\n" ));
588 init_etherdev( dev, sizeof(struct lance_private) );
590 dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
594 lp = (struct lance_private *)dev->priv;
595 MEM = (struct lance_memory *)memaddr;
596 IO = lp->iobase = (struct lance_ioreg *)ioaddr;
597 dev->base_addr = (unsigned long)ioaddr; /* informational only */
598 lp->memcpy_f = init_rec->slow_flag ? slow_memcpy : memcpy;
600 REGA( CSR0 ) = CSR0_STOP;
602 /* Now test for type: If the eeprom I/O port is readable, it is a
604 if (addr_accessible( &(IO->eeprom), 0, 0 )) {
605 /* Switch back to Ram */
607 lp->cardtype = PAM_CARD;
609 #ifdef NORMAL_MEM_ACCESS
610 else if (*RIEBL_MAGIC_ADDR == RIEBL_MAGIC) {
613 unsigned short *a = (unsigned short*)RIEBL_MAGIC_ADDR;
614 (((int)a[0]) << 16) + ((int)a[1]) == RIEBL_MAGIC;
617 lp->cardtype = NEW_RIEBL;
620 lp->cardtype = OLD_RIEBL;
622 if (lp->cardtype == PAM_CARD ||
623 memaddr == (unsigned short *)0xffe00000) {
624 /* PAMs card and Riebl on ST use level 5 autovector */
625 request_irq(BAGET_LANCE_IRQ, lance_interrupt, IRQ_TYPE_PRIO,
626 "PAM/Riebl-ST Ethernet", dev);
627 dev->irq = (unsigned short)BAGET_LANCE_IRQ;
630 /* For VME-RieblCards, request a free VME int;
631 * (This must be unsigned long, since dev->irq is short and the
632 * IRQ_MACHSPEC bit would be cut off...)
634 unsigned long irq = BAGET_LANCE_IRQ;
636 printk( "Lance: request for VME interrupt failed\n" );
639 request_irq(irq, lance_interrupt, IRQ_TYPE_PRIO,
640 "Riebl-VME Ethernet", dev);
644 printk("%s: %s at io %#lx, mem %#lx, irq %d%s, hwaddr ",
645 dev->name, lance_names[lp->cardtype],
646 (unsigned long)ioaddr,
647 (unsigned long)memaddr,
649 init_rec->slow_flag ? " (slow memcpy)" : "" );
651 /* Get the ethernet address */
652 switch( lp->cardtype ) {
654 /* No ethernet address! (Set some default address) */
655 slow_memcpy( dev->dev_addr, OldRieblDefHwaddr, 6 );
658 lp->memcpy_f( dev->dev_addr, RIEBL_HWADDR_ADDR, 6 );
662 for( i = 0; i < 6; ++i )
664 ((((unsigned short *)MEM)[i*2] & 0x0f) << 4) |
665 ((((unsigned short *)MEM)[i*2+1] & 0x0f));
669 for( i = 0; i < 6; ++i )
670 printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
671 if (lp->cardtype == OLD_RIEBL) {
672 printk( "%s: Warning: This is a default ethernet address!\n",
674 printk( " Use \"ifconfig hw ether ...\" to set the address.\n" );
677 MEM->init.mode = 0x0000; /* Disable Rx and Tx. */
680 unsigned char hwaddr[6];
681 for( i = 0; i < 6; i++ )
682 hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
683 slow_memcpy(MEM->init.hwaddr, hwaddr, sizeof(hwaddr));
686 MEM->init.filter[0] = 0x00000000;
687 MEM->init.filter[1] = 0x00000000;
688 MEM->init.rx_ring.adr_lo = offsetof( struct lance_memory, rx_head );
690 #ifdef NORMAL_MEM_ACCESS
691 MEM->init.rx_ring.adr_hi = LANCE_HI_BASE;
692 MEM->init.rx_ring.len = RX_RING_LEN_BITS;
694 MEM->init.rx_ring.len_adr_hi =
695 ((unsigned)RX_RING_LEN_BITS << 8) | LANCE_HI_BASE;
699 MEM->init.tx_ring.adr_lo = offsetof( struct lance_memory, tx_head );
701 #ifdef NORMAL_MEM_ACCESS
702 MEM->init.tx_ring.adr_hi = LANCE_HI_BASE;
703 MEM->init.tx_ring.len = TX_RING_LEN_BITS;
705 MEM->init.tx_ring.len_adr_hi =
706 ((unsigned)TX_RING_LEN_BITS<<8) | LANCE_HI_BASE;
709 if (lp->cardtype == PAM_CARD)
710 IO->ivec = IRQ_SOURCE_TO_VECTOR(dev->irq);
712 *RIEBL_IVEC_ADDR = IRQ_SOURCE_TO_VECTOR(dev->irq);
714 if (did_version++ == 0)
715 DPRINTK( 1, ( version ));
717 /* The LANCE-specific entries in the device structure. */
718 dev->open = &lance_open;
719 dev->hard_start_xmit = &lance_start_xmit;
720 dev->stop = &lance_close;
721 dev->get_stats = &lance_get_stats;
722 dev->set_multicast_list = &set_multicast_list;
723 dev->set_mac_address = &lance_set_mac_address;
726 memset( &lp->stats, 0, sizeof(lp->stats) );
732 static int lance_open( struct net_device *dev )
734 { struct lance_private *lp = (struct lance_private *)dev->priv;
735 struct lance_ioreg *IO = lp->iobase;
738 DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
740 lance_init_ring(dev);
741 /* Re-initialize the LANCE, and start it when done. */
743 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
746 REGA( CSR0 ) = CSR0_INIT;
747 /* From now on, AREG is kept to point to CSR0 */
751 if (DREG & CSR0_IDON)
753 if (i < 0 || (DREG & CSR0_ERR)) {
754 DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
755 dev->name, i, DREG ));
767 DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
772 /* Initialize the LANCE Rx and Tx rings. */
774 static void lance_init_ring( struct net_device *dev )
776 { struct lance_private *lp = (struct lance_private *)dev->priv;
782 lp->cur_rx = lp->cur_tx = 0;
785 offset = offsetof( struct lance_memory, packet_area );
787 /* If the packet buffer at offset 'o' would conflict with the reserved area
788 * of RieblCards, advance it */
789 #define CHECK_OFFSET(o) \
791 if (lp->cardtype == OLD_RIEBL || lp->cardtype == NEW_RIEBL) { \
792 if (((o) < RIEBL_RSVD_START) ? (o)+PKT_BUF_SZ > RIEBL_RSVD_START \
793 : (o) < RIEBL_RSVD_END) \
794 (o) = RIEBL_RSVD_END; \
798 for( i = 0; i < TX_RING_SIZE; i++ ) {
799 CHECK_OFFSET(offset);
800 MEM->tx_head[i].base = offset;
801 #ifdef NORMAL_MEM_ACCESS
802 MEM->tx_head[i].flag = TMD1_OWN_HOST;
803 MEM->tx_head[i].base_hi = LANCE_HI_BASE;
805 MEM->tx_head[i].flag_base_hi =
806 (TMD1_OWN_HOST<<8) | LANCE_HI_BASE;
808 MEM->tx_head[i].length = 0;
809 MEM->tx_head[i].misc = 0;
810 offset += PKT_BUF_SZ;
813 for( i = 0; i < RX_RING_SIZE; i++ ) {
814 CHECK_OFFSET(offset);
815 MEM->rx_head[i].base = offset;
816 #ifdef NORMAL_MEM_ACCESS
817 MEM->rx_head[i].flag = TMD1_OWN_CHIP;
818 MEM->rx_head[i].base_hi = LANCE_HI_BASE;
820 MEM->rx_head[i].flag_base_hi =
821 (TMD1_OWN_CHIP<<8) | LANCE_HI_BASE;
823 MEM->rx_head[i].buf_length = -PKT_BUF_SZ;
824 MEM->rx_head[i].msg_length = 0;
825 offset += PKT_BUF_SZ;
830 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
832 { struct lance_private *lp = (struct lance_private *)dev->priv;
833 struct lance_ioreg *IO = lp->iobase;
835 struct lance_tx_head *head;
838 /* Transmitter timeout, serious problems. */
840 int tickssofar = jiffies - dev->trans_start;
844 DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
848 * Always set BSWP after a STOP as STOP puts it back into
849 * little endian mode.
851 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
852 lp->stats.tx_errors++;
853 #ifndef final_version
855 DPRINTK( 2, ( "Ring data: dirty_tx %d cur_tx %d%s cur_rx %d\n",
856 lp->dirty_tx, lp->cur_tx,
857 lp->tx_full ? " (full)" : "",
859 for( i = 0 ; i < RX_RING_SIZE; i++ )
860 DPRINTK( 2, ( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
861 i, MEM->rx_head[i].base,
862 -MEM->rx_head[i].buf_length,
863 MEM->rx_head[i].msg_length ));
864 for( i = 0 ; i < TX_RING_SIZE; i++ )
865 DPRINTK( 2, ( "tx #%d: base=%04x len=%04x misc=%04x\n",
866 i, MEM->tx_head[i].base,
867 -MEM->tx_head[i].length,
868 MEM->tx_head[i].misc ));
871 lance_init_ring(dev);
872 REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
875 dev->trans_start = jiffies;
880 DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
883 /* Block a timer-based transmit from overlapping. This could better be
884 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
885 if (test_and_set_bit( 0, (void*)&dev->tbusy ) != 0) {
886 DPRINTK( 0, ( "%s: Transmitter access conflict.\n", dev->name ));
890 if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
891 DPRINTK( 0, ( "%s: tx queue lock!.\n", dev->name ));
892 /* don't clear dev->tbusy flag. */
896 /* Fill in a Tx ring entry */
897 if (lance_debug >= 3) {
900 printk( "%s: TX pkt type 0x%04x from ", dev->name,
901 ((u_short *)skb->data)[6]);
902 for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
903 printk("%02x%s", *p++, i != 5 ? ":" : "" );
905 for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
906 printk("%02x%s", *p++, i != 5 ? ":" : "" );
907 printk(" data at 0x%08x len %d\n", (int)skb->data,
911 /* We're not prepared for the int until the last flags are set/reset. And
912 * the int may happen already after setting the OWN_CHIP... */
916 /* Mask to ring buffer boundary. */
917 entry = lp->cur_tx & TX_RING_MOD_MASK;
918 head = &(MEM->tx_head[entry]);
920 /* Caution: the write order is important here, set the "ownership" bits
924 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
925 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
926 /* PAM-Card has a bug: Can only send packets with even number of bytes! */
927 if (lp->cardtype == PAM_CARD && (len & 1))
932 lp->memcpy_f( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
933 #ifdef NORMAL_MEM_ACCESS
934 head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
936 SET_FLAG(head,(TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP));
938 lp->stats.tx_bytes += skb->len;
939 dev_kfree_skb( skb );
941 while( lp->cur_tx >= TX_RING_SIZE && lp->dirty_tx >= TX_RING_SIZE ) {
942 lp->cur_tx -= TX_RING_SIZE;
943 lp->dirty_tx -= TX_RING_SIZE;
946 /* Trigger an immediate send poll. */
947 DREG = CSR0_INEA | CSR0_TDMD;
948 dev->trans_start = jiffies;
951 #ifdef NORMAL_MEM_ACCESS
952 if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
954 if ((GET_FLAG(&MEM->tx_head[(entry+1) & TX_RING_MOD_MASK]) & TMD1_OWN) ==
960 restore_flags(flags);
965 /* The LANCE interrupt handler. */
967 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
969 struct net_device *dev = dev_id;
970 struct lance_private *lp;
971 struct lance_ioreg *IO;
972 int csr0, boguscnt = 10;
975 DPRINTK( 1, ( "lance_interrupt(): interrupt for unknown device.\n" ));
979 lp = (struct lance_private *)dev->priv;
983 if (dev->interrupt) {
984 DPRINTK( 1, ( "Re-entering CAUSE=%08x STATUS=%08x\n",
985 read_32bit_cp0_register(CP0_CAUSE),
986 read_32bit_cp0_register(CP0_STATUS) ));
987 panic("lance: interrupt handler reentered !");
992 while( ((csr0 = DREG) & (CSR0_ERR | CSR0_TINT | CSR0_RINT)) &&
994 /* Acknowledge all of the current interrupt sources ASAP. */
995 DREG = csr0 & ~(CSR0_INIT | CSR0_STRT | CSR0_STOP |
996 CSR0_TDMD | CSR0_INEA);
998 DPRINTK( 2, ( "%s: interrupt csr0=%04x new csr=%04x.\n",
999 dev->name, csr0, DREG ));
1001 if (csr0 & CSR0_RINT) /* Rx interrupt */
1004 if (csr0 & CSR0_TINT) { /* Tx-done interrupt */
1005 int dirty_tx = lp->dirty_tx;
1007 while( dirty_tx < lp->cur_tx) {
1008 int entry = dirty_tx & TX_RING_MOD_MASK;
1009 #ifdef NORMAL_MEM_ACCESS
1010 int status = MEM->tx_head[entry].flag;
1012 int status = GET_FLAG(&MEM->tx_head[entry]);
1014 if (status & TMD1_OWN_CHIP)
1015 break; /* It still hasn't been Txed */
1017 #ifdef NORMAL_MEM_ACCESS
1018 MEM->tx_head[entry].flag = 0;
1020 SET_FLAG(&MEM->tx_head[entry],0);
1023 if (status & TMD1_ERR) {
1024 /* There was an major error, log it. */
1025 int err_status = MEM->tx_head[entry].misc;
1026 lp->stats.tx_errors++;
1027 if (err_status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
1028 if (err_status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
1029 if (err_status & TMD3_LCOL) lp->stats.tx_window_errors++;
1030 if (err_status & TMD3_UFLO) {
1031 /* Ackk! On FIFO errors the Tx unit is turned off! */
1032 lp->stats.tx_fifo_errors++;
1033 /* Remove this verbosity later! */
1034 DPRINTK( 1, ( "%s: Tx FIFO error! Status %04x\n",
1036 /* Restart the chip. */
1040 if (status & (TMD1_MORE | TMD1_ONE | TMD1_DEF))
1041 lp->stats.collisions++;
1042 lp->stats.tx_packets++;
1047 #ifndef final_version
1048 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1049 DPRINTK( 0, ( "out-of-sync dirty pointer,"
1050 " %d vs. %d, full=%d.\n",
1051 dirty_tx, lp->cur_tx, lp->tx_full ));
1052 dirty_tx += TX_RING_SIZE;
1056 if (lp->tx_full && dev->tbusy
1057 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1058 /* The ring is no longer full, clear tbusy. */
1064 lp->dirty_tx = dirty_tx;
1067 /* Log misc errors. */
1068 if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
1069 if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
1070 if (csr0 & CSR0_MERR) {
1071 DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
1072 "status %04x.\n", dev->name, csr0 ));
1073 /* Restart the chip. */
1078 /* Clear any other interrupt, and set interrupt enable. */
1079 DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
1080 CSR0_IDON | CSR0_INEA;
1082 DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
1089 static int lance_rx( struct net_device *dev )
1091 { struct lance_private *lp = (struct lance_private *)dev->priv;
1092 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1095 #ifdef NORMAL_MEM_ACCESS
1096 DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
1097 MEM->rx_head[entry].flag ));
1099 DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
1100 GET_FLAG(&MEM->rx_head[entry]) ));
1103 /* If we own the next entry, it's a new packet. Send it up. */
1104 #ifdef NORMAL_MEM_ACCESS
1105 while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
1107 while( (GET_FLAG(&MEM->rx_head[entry]) & RMD1_OWN) == RMD1_OWN_HOST ) {
1109 struct lance_rx_head *head = &(MEM->rx_head[entry]);
1110 #ifdef NORMAL_MEM_ACCESS
1111 int status = head->flag;
1113 int status = GET_FLAG(head);
1116 if (status != (RMD1_ENP|RMD1_STP)) { /* There was an error. */
1117 /* There is a tricky error noted by John Murphy,
1118 <murf@perftech.com> to Russ Nelson: Even with full-sized
1119 buffers it's possible for a jabber packet to use two
1120 buffers, with only the last correctly noting the error. */
1121 if (status & RMD1_ENP) /* Only count a general error at the */
1122 lp->stats.rx_errors++; /* end of a packet.*/
1123 if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
1124 if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
1125 if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
1126 if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
1127 #ifdef NORMAL_MEM_ACCESS
1128 head->flag &= (RMD1_ENP|RMD1_STP);
1130 SET_FLAG(head,GET_FLAG(head) & (RMD1_ENP|RMD1_STP));
1133 /* Malloc up new buffer, compatible with net-3. */
1134 short pkt_len = head->msg_length & 0xfff;
1135 struct sk_buff *skb;
1138 printk( "%s: Runt packet!\n", dev->name );
1139 lp->stats.rx_errors++;
1142 skb = dev_alloc_skb( pkt_len+2 );
1144 DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
1146 for( i = 0; i < RX_RING_SIZE; i++ )
1147 #ifdef NORMAL_MEM_ACCESS
1148 if (MEM->rx_head[(entry+i) & RX_RING_MOD_MASK].flag &
1150 if (GET_FLAG(&MEM->rx_head[(entry+i) & \
1151 RX_RING_MOD_MASK]) &
1156 if (i > RX_RING_SIZE - 2) {
1157 lp->stats.rx_dropped++;
1158 #ifdef NORMAL_MEM_ACCESS
1159 head->flag |= RMD1_OWN_CHIP;
1161 SET_FLAG(head,GET_FLAG(head) | RMD1_OWN_CHIP);
1168 if (lance_debug >= 3) {
1169 u_char *data = PKTBUF_ADDR(head), *p;
1170 printk( "%s: RX pkt type 0x%04x from ", dev->name,
1171 ((u_short *)data)[6]);
1172 for( p = &data[6], i = 0; i < 6; i++ )
1173 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1175 for( p = data, i = 0; i < 6; i++ )
1176 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1177 printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
1179 data[15], data[16], data[17], data[18],
1180 data[19], data[20], data[21], data[22],
1185 skb_reserve( skb, 2 ); /* 16 byte align */
1186 skb_put( skb, pkt_len ); /* Make room */
1187 lp->memcpy_f( skb->data, PKTBUF_ADDR(head), pkt_len );
1188 skb->protocol = eth_type_trans( skb, dev );
1190 dev->last_rx = jiffies;
1191 lp->stats.rx_packets++;
1192 lp->stats.rx_bytes += pkt_len;
1196 #ifdef NORMAL_MEM_ACCESS
1197 head->flag |= RMD1_OWN_CHIP;
1199 SET_FLAG(head,GET_FLAG(head) | RMD1_OWN_CHIP);
1201 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1203 lp->cur_rx &= RX_RING_MOD_MASK;
1205 /* From lance.c (Donald Becker): */
1206 /* We should check that at least two ring entries are free. If not,
1207 we should free one and mark stats->rx_dropped++. */
1213 static int lance_close( struct net_device *dev )
1215 { struct lance_private *lp = (struct lance_private *)dev->priv;
1216 struct lance_ioreg *IO = lp->iobase;
1223 DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
1226 /* We stop the LANCE here -- it occasionally polls
1227 memory if we don't. */
1234 static struct net_device_stats *lance_get_stats( struct net_device *dev )
1237 struct lance_private *lp = (struct lance_private *)dev->priv;
1242 /* Set or clear the multicast filter for this adaptor.
1243 num_addrs == -1 Promiscuous mode, receive all packets
1244 num_addrs == 0 Normal mode, clear multicast list
1245 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
1246 best-effort filtering.
1249 static void set_multicast_list( struct net_device *dev )
1251 { struct lance_private *lp = (struct lance_private *)dev->priv;
1252 struct lance_ioreg *IO = lp->iobase;
1255 /* Only possible if board is already started */
1258 /* We take the simple way out and always enable promiscuous mode. */
1259 DREG = CSR0_STOP; /* Temporarily stop the lance. */
1261 if (dev->flags & IFF_PROMISC) {
1262 /* Log any net taps. */
1263 DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
1264 REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
1266 short multicast_table[4];
1267 int num_addrs = dev->mc_count;
1269 /* We don't use the multicast table, but rely on upper-layer
1271 memset( multicast_table, (num_addrs == 0) ? 0 : -1,
1272 sizeof(multicast_table) );
1273 for( i = 0; i < 4; i++ )
1274 REGA( CSR8+i ) = multicast_table[i];
1275 REGA( CSR15 ) = 0; /* Unset promiscuous mode */
1279 * Always set BSWP after a STOP as STOP puts it back into
1280 * little endian mode.
1282 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
1284 /* Resume normal operation and reset AREG to CSR0 */
1285 REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
1289 /* This is needed for old RieblCards and possible for new RieblCards */
1291 static int lance_set_mac_address( struct net_device *dev, void *addr )
1293 { struct lance_private *lp = (struct lance_private *)dev->priv;
1294 struct sockaddr *saddr = addr;
1297 if (lp->cardtype != OLD_RIEBL && lp->cardtype != NEW_RIEBL)
1298 return( -EOPNOTSUPP );
1301 /* Only possible while card isn't started */
1302 DPRINTK( 1, ( "%s: hwaddr can be set only while card isn't open.\n",
1307 slow_memcpy( dev->dev_addr, saddr->sa_data, dev->addr_len );
1310 unsigned char hwaddr[6];
1311 for( i = 0; i < 6; i++ )
1312 hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
1313 slow_memcpy(MEM->init.hwaddr, hwaddr, sizeof(hwaddr));
1316 lp->memcpy_f( RIEBL_HWADDR_ADDR, dev->dev_addr, 6 );
1317 /* set also the magic for future sessions */
1318 #ifdef NORMAL_MEM_ACCESS
1319 *RIEBL_MAGIC_ADDR = RIEBL_MAGIC;
1322 unsigned long magic = RIEBL_MAGIC;
1323 slow_memcpy(RIEBL_MAGIC_ADDR, &magic, sizeof(*RIEBL_MAGIC_ADDR));
1331 static struct net_device bagetlance_dev;
1333 int init_module(void)
1337 bagetlance_dev.init = bagetlance_probe;
1338 if ((err = register_netdev( &bagetlance_dev ))) {
1340 printk( "No Vme Lance board found. Module not loaded.\n");
1347 void cleanup_module(void)
1350 unregister_netdev( &bagetlance_dev );