1 /* atarilance.c: Ethernet driver for VME Lance cards on the Atari */
2 /*
3 Written 1995/96 by Roman Hodek (Roman.Hodek@informatik.uni-erlangen.de)
4
5 This software may be used and distributed according to the terms
6 of the GNU Public License, incorporated herein by reference.
7
8 This drivers was written with the following sources of reference:
9 - The driver for the Riebl Lance card by the TU Vienna.
10 - The modified TUW driver for PAM's VME cards
11 - The PC-Linux driver for Lance cards (but this is for bus master
12 cards, not the shared memory ones)
13 - The Amiga Ariadne driver
14
15 v1.0: (in 1.2.13pl4/0.9.13)
16 Initial version
17 v1.1: (in 1.2.13pl5)
18 more comments
19 deleted some debugging stuff
20 optimized register access (keep AREG pointing to CSR0)
21 following AMD, CSR0_STRT should be set only after IDON is detected
22 use memcpy() for data transfers, that also employs long word moves
23 better probe procedure for 24-bit systems
24 non-VME-RieblCards need extra delays in memcpy
25 must also do write test, since 0xfxe00000 may hit ROM
26 use 8/32 tx/rx buffers, which should give better NFS performance;
27 this is made possible by shifting the last packet buffer after the
28 RieblCard reserved area
29 v1.2: (in 1.2.13pl8)
30 again fixed probing for the Falcon; 0xfe01000 hits phys. 0x00010000
31 and thus RAM, in case of no Lance found all memory contents have to
32 be restored!
33 Now possible to compile as module.
34 v1.3: 03/30/96 Jes Sorensen, Roman (in 1.3)
35 Several little 1.3 adaptions
36 When the lance is stopped it jumps back into little-endian
37 mode. It is therefore necessary to put it back where it
38 belongs, in big endian mode, in order to make things work.
39 This might be the reason why multicast-mode didn't work
40 before, but I'm not able to test it as I only got an Amiga
41 (we had similar problems with the A2065 driver).
42
43 */
44
45 static char *version = "atarilance.c: v1.3 04/04/96 "
46 "Roman.Hodek@informatik.uni-erlangen.de\n";
47
48 #include <linux/module.h>
49
50 #include <linux/stddef.h>
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/string.h>
54 #include <linux/ptrace.h>
55 #include <linux/errno.h>
56 #include <linux/malloc.h>
57 #include <linux/interrupt.h>
58 #include <linux/init.h>
59
60 #include <asm/setup.h>
61 #include <asm/irq.h>
62 #include <asm/atarihw.h>
63 #include <asm/atariints.h>
64 #include <asm/bitops.h>
65 #include <asm/io.h>
66
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/skbuff.h>
70
71 /* Debug level:
72 * 0 = silent, print only serious errors
73 * 1 = normal, print error messages
74 * 2 = debug, print debug infos
75 * 3 = debug, print even more debug infos (packet data)
76 */
77
78 #define LANCE_DEBUG 1
79
80 #ifdef LANCE_DEBUG
81 static int lance_debug = LANCE_DEBUG;
82 #else
83 static int lance_debug = 1;
84 #endif
85 MODULE_PARM(lance_debug, "i");
86
87 /* Print debug messages on probing? */
88 #undef LANCE_DEBUG_PROBE
89
90 #define DPRINTK(n,a) \
91 do { \
92 if (lance_debug >= n) \
93 printk a; \
94 } while( 0 )
95
96 #ifdef LANCE_DEBUG_PROBE
97 # define PROBE_PRINT(a) printk a
98 #else
99 # define PROBE_PRINT(a)
100 #endif
101
102 /* These define the number of Rx and Tx buffers as log2. (Only powers
103 * of two are valid)
104 * Much more rx buffers (32) are reserved than tx buffers (8), since receiving
105 * is more time critical then sending and packets may have to remain in the
106 * board's memory when main memory is low.
107 */
108
109 #define TX_LOG_RING_SIZE 3
110 #define RX_LOG_RING_SIZE 5
111
112 /* These are the derived values */
113
114 #define TX_RING_SIZE (1 << TX_LOG_RING_SIZE)
115 #define TX_RING_LEN_BITS (TX_LOG_RING_SIZE << 5)
116 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
117
118 #define RX_RING_SIZE (1 << RX_LOG_RING_SIZE)
119 #define RX_RING_LEN_BITS (RX_LOG_RING_SIZE << 5)
120 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
121
122 #define TX_TIMEOUT 20
123
124 /* The LANCE Rx and Tx ring descriptors. */
125 struct lance_rx_head {
126 unsigned short base; /* Low word of base addr */
127 volatile unsigned char flag;
128 unsigned char base_hi; /* High word of base addr (unused) */
129 short buf_length; /* This length is 2s complement! */
130 volatile short msg_length; /* This length is "normal". */
131 };
132
133 struct lance_tx_head {
134 unsigned short base; /* Low word of base addr */
135 volatile unsigned char flag;
136 unsigned char base_hi; /* High word of base addr (unused) */
137 short length; /* Length is 2s complement! */
138 volatile short misc;
139 };
140
141 struct ringdesc {
142 unsigned short adr_lo; /* Low 16 bits of address */
143 unsigned char len; /* Length bits */
144 unsigned char adr_hi; /* High 8 bits of address (unused) */
145 };
146
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;
155 };
156
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
165 * at runtime */
166 };
167
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.
174 */
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))
181
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.
185 */
186
187 static unsigned char OldRieblDefHwaddr[6] = {
188 0x00, 0x00, 0x36, 0x04, 0x00, 0x00
189 };
190
191
192 /* I/O registers of the Lance chip */
193
194 struct lance_ioreg {
195 /* base+0x0 */ volatile unsigned short data;
196 /* base+0x2 */ volatile unsigned short addr;
197 unsigned char _dummy1[3];
198 /* base+0x7 */ volatile unsigned char ivec;
199 unsigned char _dummy2[5];
200 /* base+0xd */ volatile unsigned char eeprom;
201 unsigned char _dummy3;
202 /* base+0xf */ volatile unsigned char mem;
203 };
204
205 /* Types of boards this driver supports */
206
207 enum lance_type {
208 OLD_RIEBL, /* old Riebl card without battery */
209 NEW_RIEBL, /* new Riebl card with battery */
210 PAM_CARD /* PAM card with EEPROM */
211 };
212
213 static char *lance_names[] = {
214 "Riebl-Card (without battery)",
215 "Riebl-Card (with battery)",
216 "PAM intern card"
217 };
218
219 /* The driver's private device structure */
220
221 struct lance_private {
222 enum lance_type cardtype;
223 struct lance_ioreg *iobase;
224 struct lance_memory *mem;
225 int cur_rx, cur_tx; /* The next free ring entry */
226 int dirty_tx; /* Ring entries to be freed. */
227 /* copy function */
228 void *(*memcpy_f)( void *, const void *, size_t );
229 struct net_device_stats stats;
230 /* This must be long for set_bit() */
231 long tx_full;
232 spinlock_t devlock;
233 };
234
235 /* I/O register access macros */
236
237 #define MEM lp->mem
238 #define DREG IO->data
239 #define AREG IO->addr
240 #define REGA(a) ( AREG = (a), DREG )
241
242 /* Definitions for packet buffer access: */
243 #define PKT_BUF_SZ 1544
244 /* Get the address of a packet buffer corresponding to a given buffer head */
245 #define PKTBUF_ADDR(head) (((unsigned char *)(MEM)) + (head)->base)
246
247 /* Possible memory/IO addresses for probing */
248
249 struct lance_addr {
250 unsigned long memaddr;
251 unsigned long ioaddr;
252 int slow_flag;
253 } lance_addr_list[] = {
254 { 0xfe010000, 0xfe00fff0, 0 }, /* RieblCard VME in TT */
255 { 0xffc10000, 0xffc0fff0, 0 }, /* RieblCard VME in MegaSTE
256 (highest byte stripped) */
257 { 0xffe00000, 0xffff7000, 1 }, /* RieblCard in ST
258 (highest byte stripped) */
259 { 0xffd00000, 0xffff7000, 1 }, /* RieblCard in ST with hw modif. to
260 avoid conflict with ROM
261 (highest byte stripped) */
262 { 0xffcf0000, 0xffcffff0, 0 }, /* PAMCard VME in TT and MSTE
263 (highest byte stripped) */
264 { 0xfecf0000, 0xfecffff0, 0 }, /* Rhotron's PAMCard VME in TT and MSTE
265 (highest byte stripped) */
266 };
267
268 #define N_LANCE_ADDR (sizeof(lance_addr_list)/sizeof(*lance_addr_list))
269
270
271 /* Definitions for the Lance */
272
273 /* tx_head flags */
274 #define TMD1_ENP 0x01 /* end of packet */
275 #define TMD1_STP 0x02 /* start of packet */
276 #define TMD1_DEF 0x04 /* deferred */
277 #define TMD1_ONE 0x08 /* one retry needed */
278 #define TMD1_MORE 0x10 /* more than one retry needed */
279 #define TMD1_ERR 0x40 /* error summary */
280 #define TMD1_OWN 0x80 /* ownership (set: chip owns) */
281
282 #define TMD1_OWN_CHIP TMD1_OWN
283 #define TMD1_OWN_HOST 0
284
285 /* tx_head misc field */
286 #define TMD3_TDR 0x03FF /* Time Domain Reflectometry counter */
287 #define TMD3_RTRY 0x0400 /* failed after 16 retries */
288 #define TMD3_LCAR 0x0800 /* carrier lost */
289 #define TMD3_LCOL 0x1000 /* late collision */
290 #define TMD3_UFLO 0x4000 /* underflow (late memory) */
291 #define TMD3_BUFF 0x8000 /* buffering error (no ENP) */
292
293 /* rx_head flags */
294 #define RMD1_ENP 0x01 /* end of packet */
295 #define RMD1_STP 0x02 /* start of packet */
296 #define RMD1_BUFF 0x04 /* buffer error */
297 #define RMD1_CRC 0x08 /* CRC error */
298 #define RMD1_OFLO 0x10 /* overflow */
299 #define RMD1_FRAM 0x20 /* framing error */
300 #define RMD1_ERR 0x40 /* error summary */
301 #define RMD1_OWN 0x80 /* ownership (set: ship owns) */
302
303 #define RMD1_OWN_CHIP RMD1_OWN
304 #define RMD1_OWN_HOST 0
305
306 /* register names */
307 #define CSR0 0 /* mode/status */
308 #define CSR1 1 /* init block addr (low) */
309 #define CSR2 2 /* init block addr (high) */
310 #define CSR3 3 /* misc */
311 #define CSR8 8 /* address filter */
312 #define CSR15 15 /* promiscuous mode */
313
314 /* CSR0 */
315 /* (R=readable, W=writeable, S=set on write, C=clear on write) */
316 #define CSR0_INIT 0x0001 /* initialize (RS) */
317 #define CSR0_STRT 0x0002 /* start (RS) */
318 #define CSR0_STOP 0x0004 /* stop (RS) */
319 #define CSR0_TDMD 0x0008 /* transmit demand (RS) */
320 #define CSR0_TXON 0x0010 /* transmitter on (R) */
321 #define CSR0_RXON 0x0020 /* receiver on (R) */
322 #define CSR0_INEA 0x0040 /* interrupt enable (RW) */
323 #define CSR0_INTR 0x0080 /* interrupt active (R) */
324 #define CSR0_IDON 0x0100 /* initialization done (RC) */
325 #define CSR0_TINT 0x0200 /* transmitter interrupt (RC) */
326 #define CSR0_RINT 0x0400 /* receiver interrupt (RC) */
327 #define CSR0_MERR 0x0800 /* memory error (RC) */
328 #define CSR0_MISS 0x1000 /* missed frame (RC) */
329 #define CSR0_CERR 0x2000 /* carrier error (no heartbeat :-) (RC) */
330 #define CSR0_BABL 0x4000 /* babble: tx-ed too many bits (RC) */
331 #define CSR0_ERR 0x8000 /* error (RC) */
332
333 /* CSR3 */
334 #define CSR3_BCON 0x0001 /* byte control */
335 #define CSR3_ACON 0x0002 /* ALE control */
336 #define CSR3_BSWP 0x0004 /* byte swap (1=big endian) */
337
338
339
340 /***************************** Prototypes *****************************/
341
342 static int addr_accessible( volatile void *regp, int wordflag, int
343 writeflag );
344 static unsigned long lance_probe1( struct net_device *dev, struct lance_addr
345 *init_rec );
346 static int lance_open( struct net_device *dev );
347 static void lance_init_ring( struct net_device *dev );
348 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
349 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
350 static int lance_rx( struct net_device *dev );
351 static int lance_close( struct net_device *dev );
352 static struct net_device_stats *lance_get_stats( struct net_device *dev );
353 static void set_multicast_list( struct net_device *dev );
354 static int lance_set_mac_address( struct net_device *dev, void *addr );
355 static void lance_tx_timeout (struct net_device *dev);
356
357 /************************* End of Prototypes **************************/
358
359
360
361
362
363 static void *slow_memcpy( void *dst, const void *src, size_t len )
364
365 { char *cto = dst;
366 const char *cfrom = src;
367
368 while( len-- ) {
369 *cto++ = *cfrom++;
370 MFPDELAY();
371 }
372 return( dst );
373 }
374
375
376 int __init atarilance_probe( struct net_device *dev )
377 {
378 int i;
379 static int found = 0;
380
381 SET_MODULE_OWNER(dev);
382
383 if (!MACH_IS_ATARI || found)
384 /* Assume there's only one board possible... That seems true, since
385 * the Riebl/PAM board's address cannot be changed. */
386 return( ENODEV );
387
388 for( i = 0; i < N_LANCE_ADDR; ++i ) {
389 if (lance_probe1( dev, &lance_addr_list[i] )) {
390 found = 1;
391 return( 0 );
392 }
393 }
394
395 return( ENODEV );
396 }
397
398
399 /* Derived from hwreg_present() in atari/config.c: */
400
401 static int __init addr_accessible( volatile void *regp, int wordflag, int writeflag )
402 {
403 int ret;
404 long flags;
405 long *vbr, save_berr;
406
407 save_flags(flags);
408 cli();
409
410 __asm__ __volatile__ ( "movec %/vbr,%0" : "=r" (vbr) : );
411 save_berr = vbr[2];
412
413 __asm__ __volatile__
414 ( "movel %/sp,%/d1\n\t"
415 "movel #Lberr,%2@\n\t"
416 "moveq #0,%0\n\t"
417 "tstl %3\n\t"
418 "bne 1f\n\t"
419 "moveb %1@,%/d0\n\t"
420 "nop \n\t"
421 "bra 2f\n"
422 "1: movew %1@,%/d0\n\t"
423 "nop \n"
424 "2: tstl %4\n\t"
425 "beq 2f\n\t"
426 "tstl %3\n\t"
427 "bne 1f\n\t"
428 "clrb %1@\n\t"
429 "nop \n\t"
430 "moveb %/d0,%1@\n\t"
431 "nop \n\t"
432 "bra 2f\n"
433 "1: clrw %1@\n\t"
434 "nop \n\t"
435 "movew %/d0,%1@\n\t"
436 "nop \n"
437 "2: moveq #1,%0\n"
438 "Lberr: movel %/d1,%/sp"
439 : "=&d" (ret)
440 : "a" (regp), "a" (&vbr[2]), "rm" (wordflag), "rm" (writeflag)
441 : "d0", "d1", "memory"
442 );
443
444 vbr[2] = save_berr;
445 restore_flags(flags);
446
447 return( ret );
448 }
449
450
451 static unsigned long __init lance_probe1( struct net_device *dev,
452 struct lance_addr *init_rec )
453 {
454 volatile unsigned short *memaddr =
455 (volatile unsigned short *)init_rec->memaddr;
456 volatile unsigned short *ioaddr =
457 (volatile unsigned short *)init_rec->ioaddr;
458 struct lance_private *lp;
459 struct lance_ioreg *IO;
460 int i;
461 static int did_version = 0;
462 unsigned short save1, save2;
463
464 PROBE_PRINT(( "Probing for Lance card at mem %#lx io %#lx\n",
465 (long)memaddr, (long)ioaddr ));
466
467 /* Test whether memory readable and writable */
468 PROBE_PRINT(( "lance_probe1: testing memory to be accessible\n" ));
469 if (!addr_accessible( memaddr, 1, 1 )) goto probe_fail;
470
471 /* Written values should come back... */
472 PROBE_PRINT(( "lance_probe1: testing memory to be writable (1)\n" ));
473 save1 = *memaddr;
474 *memaddr = 0x0001;
475 if (*memaddr != 0x0001) goto probe_fail;
476 PROBE_PRINT(( "lance_probe1: testing memory to be writable (2)\n" ));
477 *memaddr = 0x0000;
478 if (*memaddr != 0x0000) goto probe_fail;
479 *memaddr = save1;
480
481 /* First port should be readable and writable */
482 PROBE_PRINT(( "lance_probe1: testing ioport to be accessible\n" ));
483 if (!addr_accessible( ioaddr, 1, 1 )) goto probe_fail;
484
485 /* and written values should be readable */
486 PROBE_PRINT(( "lance_probe1: testing ioport to be writeable\n" ));
487 save2 = ioaddr[1];
488 ioaddr[1] = 0x0001;
489 if (ioaddr[1] != 0x0001) goto probe_fail;
490
491 /* The CSR0_INIT bit should not be readable */
492 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (1)\n" ));
493 save1 = ioaddr[0];
494 ioaddr[1] = CSR0;
495 ioaddr[0] = CSR0_INIT | CSR0_STOP;
496 if (ioaddr[0] != CSR0_STOP) {
497 ioaddr[0] = save1;
498 ioaddr[1] = save2;
499 goto probe_fail;
500 }
501 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (2)\n" ));
502 ioaddr[0] = CSR0_STOP;
503 if (ioaddr[0] != CSR0_STOP) {
504 ioaddr[0] = save1;
505 ioaddr[1] = save2;
506 goto probe_fail;
507 }
508
509 /* Now ok... */
510 PROBE_PRINT(( "lance_probe1: Lance card detected\n" ));
511 goto probe_ok;
512
513 probe_fail:
514 return( 0 );
515
516 probe_ok:
517 init_etherdev( dev, sizeof(struct lance_private) );
518 if (!dev->priv)
519 dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
520 lp = (struct lance_private *)dev->priv;
521 MEM = (struct lance_memory *)memaddr;
522 IO = lp->iobase = (struct lance_ioreg *)ioaddr;
523 dev->base_addr = (unsigned long)ioaddr; /* informational only */
524 lp->memcpy_f = init_rec->slow_flag ? slow_memcpy : memcpy;
525
526 REGA( CSR0 ) = CSR0_STOP;
527
528 /* Now test for type: If the eeprom I/O port is readable, it is a
529 * PAM card */
530 if (addr_accessible( &(IO->eeprom), 0, 0 )) {
531 /* Switch back to Ram */
532 i = IO->mem;
533 lp->cardtype = PAM_CARD;
534 }
535 else if (*RIEBL_MAGIC_ADDR == RIEBL_MAGIC) {
536 lp->cardtype = NEW_RIEBL;
537 }
538 else
539 lp->cardtype = OLD_RIEBL;
540
541 if (lp->cardtype == PAM_CARD ||
542 memaddr == (unsigned short *)0xffe00000) {
543 /* PAMs card and Riebl on ST use level 5 autovector */
544 request_irq(IRQ_AUTO_5, lance_interrupt, IRQ_TYPE_PRIO,
545 "PAM/Riebl-ST Ethernet", dev);
546 dev->irq = (unsigned short)IRQ_AUTO_5;
547 }
548 else {
549 /* For VME-RieblCards, request a free VME int;
550 * (This must be unsigned long, since dev->irq is short and the
551 * IRQ_MACHSPEC bit would be cut off...)
552 */
553 unsigned long irq = atari_register_vme_int();
554 if (!irq) {
555 printk( "Lance: request for VME interrupt failed\n" );
556 return( 0 );
557 }
558 request_irq(irq, lance_interrupt, IRQ_TYPE_PRIO,
559 "Riebl-VME Ethernet", dev);
560 dev->irq = irq;
561 }
562
563 printk("%s: %s at io %#lx, mem %#lx, irq %d%s, hwaddr ",
564 dev->name, lance_names[lp->cardtype],
565 (unsigned long)ioaddr,
566 (unsigned long)memaddr,
567 dev->irq,
568 init_rec->slow_flag ? " (slow memcpy)" : "" );
569
570 /* Get the ethernet address */
571 switch( lp->cardtype ) {
572 case OLD_RIEBL:
573 /* No ethernet address! (Set some default address) */
574 memcpy( dev->dev_addr, OldRieblDefHwaddr, 6 );
575 break;
576 case NEW_RIEBL:
577 lp->memcpy_f( dev->dev_addr, RIEBL_HWADDR_ADDR, 6 );
578 break;
579 case PAM_CARD:
580 i = IO->eeprom;
581 for( i = 0; i < 6; ++i )
582 dev->dev_addr[i] =
583 ((((unsigned short *)MEM)[i*2] & 0x0f) << 4) |
584 ((((unsigned short *)MEM)[i*2+1] & 0x0f));
585 i = IO->mem;
586 break;
587 }
588 for( i = 0; i < 6; ++i )
589 printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
590 if (lp->cardtype == OLD_RIEBL) {
591 printk( "%s: Warning: This is a default ethernet address!\n",
592 dev->name );
593 printk( " Use \"ifconfig hw ether ...\" to set the address.\n" );
594 }
595
596 lp->devlock = SPIN_LOCK_UNLOCKED;
597
598 MEM->init.mode = 0x0000; /* Disable Rx and Tx. */
599 for( i = 0; i < 6; i++ )
600 MEM->init.hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
601 MEM->init.filter[0] = 0x00000000;
602 MEM->init.filter[1] = 0x00000000;
603 MEM->init.rx_ring.adr_lo = offsetof( struct lance_memory, rx_head );
604 MEM->init.rx_ring.adr_hi = 0;
605 MEM->init.rx_ring.len = RX_RING_LEN_BITS;
606 MEM->init.tx_ring.adr_lo = offsetof( struct lance_memory, tx_head );
607 MEM->init.tx_ring.adr_hi = 0;
608 MEM->init.tx_ring.len = TX_RING_LEN_BITS;
609
610 if (lp->cardtype == PAM_CARD)
611 IO->ivec = IRQ_SOURCE_TO_VECTOR(dev->irq);
612 else
613 *RIEBL_IVEC_ADDR = IRQ_SOURCE_TO_VECTOR(dev->irq);
614
615 if (did_version++ == 0)
616 DPRINTK( 1, ( version ));
617
618 /* The LANCE-specific entries in the device structure. */
619 dev->open = &lance_open;
620 dev->hard_start_xmit = &lance_start_xmit;
621 dev->stop = &lance_close;
622 dev->get_stats = &lance_get_stats;
623 dev->set_multicast_list = &set_multicast_list;
624 dev->set_mac_address = &lance_set_mac_address;
625
626 /* XXX MSch */
627 dev->tx_timeout = lance_tx_timeout;
628 dev->watchdog_timeo = TX_TIMEOUT;
629
630
631 #if 0
632 dev->start = 0;
633 #endif
634
635 memset( &lp->stats, 0, sizeof(lp->stats) );
636
637 return( 1 );
638 }
639
640
641 static int lance_open( struct net_device *dev )
642
643 { struct lance_private *lp = (struct lance_private *)dev->priv;
644 struct lance_ioreg *IO = lp->iobase;
645 int i;
646
647 DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
648
649 lance_init_ring(dev);
650 /* Re-initialize the LANCE, and start it when done. */
651
652 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
653 REGA( CSR2 ) = 0;
654 REGA( CSR1 ) = 0;
655 REGA( CSR0 ) = CSR0_INIT;
656 /* From now on, AREG is kept to point to CSR0 */
657
658 i = 1000000;
659 while (--i > 0)
660 if (DREG & CSR0_IDON)
661 break;
662 if (i < 0 || (DREG & CSR0_ERR)) {
663 DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
664 dev->name, i, DREG ));
665 DREG = CSR0_STOP;
666 return( -EIO );
667 }
668 DREG = CSR0_IDON;
669 DREG = CSR0_STRT;
670 DREG = CSR0_INEA;
671
672 netif_start_queue (dev);
673
674 DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
675
676 return( 0 );
677 }
678
679
680 /* Initialize the LANCE Rx and Tx rings. */
681
682 static void lance_init_ring( struct net_device *dev )
683
684 { struct lance_private *lp = (struct lance_private *)dev->priv;
685 int i;
686 unsigned offset;
687
688 lp->tx_full = 0;
689 lp->cur_rx = lp->cur_tx = 0;
690 lp->dirty_tx = 0;
691
692 offset = offsetof( struct lance_memory, packet_area );
693
694 /* If the packet buffer at offset 'o' would conflict with the reserved area
695 * of RieblCards, advance it */
696 #define CHECK_OFFSET(o) \
697 do { \
698 if (lp->cardtype == OLD_RIEBL || lp->cardtype == NEW_RIEBL) { \
699 if (((o) < RIEBL_RSVD_START) ? (o)+PKT_BUF_SZ > RIEBL_RSVD_START \
700 : (o) < RIEBL_RSVD_END) \
701 (o) = RIEBL_RSVD_END; \
702 } \
703 } while(0)
704
705 for( i = 0; i < TX_RING_SIZE; i++ ) {
706 CHECK_OFFSET(offset);
707 MEM->tx_head[i].base = offset;
708 MEM->tx_head[i].flag = TMD1_OWN_HOST;
709 MEM->tx_head[i].base_hi = 0;
710 MEM->tx_head[i].length = 0;
711 MEM->tx_head[i].misc = 0;
712 offset += PKT_BUF_SZ;
713 }
714
715 for( i = 0; i < RX_RING_SIZE; i++ ) {
716 CHECK_OFFSET(offset);
717 MEM->rx_head[i].base = offset;
718 MEM->rx_head[i].flag = TMD1_OWN_CHIP;
719 MEM->rx_head[i].base_hi = 0;
720 MEM->rx_head[i].buf_length = -PKT_BUF_SZ;
721 MEM->rx_head[i].msg_length = 0;
722 offset += PKT_BUF_SZ;
723 }
724 }
725
726
727 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
728
729
730 static void lance_tx_timeout (struct net_device *dev)
731 {
732 struct lance_private *lp = (struct lance_private *) dev->priv;
733 struct lance_ioreg *IO = lp->iobase;
734
735 AREG = CSR0;
736 DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
737 dev->name, DREG ));
738 DREG = CSR0_STOP;
739 /*
740 * Always set BSWP after a STOP as STOP puts it back into
741 * little endian mode.
742 */
743 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
744 lp->stats.tx_errors++;
745 #ifndef final_version
746 { int i;
747 DPRINTK( 2, ( "Ring data: dirty_tx %d cur_tx %d%s cur_rx %d\n",
748 lp->dirty_tx, lp->cur_tx,
749 lp->tx_full ? " (full)" : "",
750 lp->cur_rx ));
751 for( i = 0 ; i < RX_RING_SIZE; i++ )
752 DPRINTK( 2, ( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
753 i, MEM->rx_head[i].base,
754 -MEM->rx_head[i].buf_length,
755 MEM->rx_head[i].msg_length ));
756 for( i = 0 ; i < TX_RING_SIZE; i++ )
757 DPRINTK( 2, ( "tx #%d: base=%04x len=%04x misc=%04x\n",
758 i, MEM->tx_head[i].base,
759 -MEM->tx_head[i].length,
760 MEM->tx_head[i].misc ));
761 }
762 #endif
763 /* XXX MSch: maybe purge/reinit ring here */
764 /* lance_restart, essentially */
765 lance_init_ring(dev);
766 REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
767 dev->trans_start = jiffies;
768 netif_start_queue (dev);
769 }
770
771 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
772
773 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
774
775 { struct lance_private *lp = (struct lance_private *)dev->priv;
776 struct lance_ioreg *IO = lp->iobase;
777 int entry, len;
778 struct lance_tx_head *head;
779 unsigned long flags;
780
781 DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
782 dev->name, DREG ));
783
784 netif_stop_queue (dev);
785
786 /* Fill in a Tx ring entry */
787 if (lance_debug >= 3) {
788 u_char *p;
789 int i;
790 printk( "%s: TX pkt type 0x%04x from ", dev->name,
791 ((u_short *)skb->data)[6]);
792 for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
793 printk("%02x%s", *p++, i != 5 ? ":" : "" );
794 printk(" to ");
795 for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
796 printk("%02x%s", *p++, i != 5 ? ":" : "" );
797 printk(" data at 0x%08x len %d\n", (int)skb->data,
798 (int)skb->len );
799 }
800
801 /* We're not prepared for the int until the last flags are set/reset. And
802 * the int may happen already after setting the OWN_CHIP... */
803 spin_lock_irqsave (&lp->devlock, flags);
804
805 /* Mask to ring buffer boundary. */
806 entry = lp->cur_tx & TX_RING_MOD_MASK;
807 head = &(MEM->tx_head[entry]);
808
809 /* Caution: the write order is important here, set the "ownership" bits
810 * last.
811 */
812
813 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
814 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
815 /* PAM-Card has a bug: Can only send packets with even number of bytes! */
816 if (lp->cardtype == PAM_CARD && (len & 1))
817 ++len;
818
819 head->length = -len;
820 head->misc = 0;
821 lp->memcpy_f( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
822 head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
823 dev_kfree_skb( skb );
824 lp->cur_tx++;
825 lp->stats.tx_bytes += skb->len;
826 while( lp->cur_tx >= TX_RING_SIZE && lp->dirty_tx >= TX_RING_SIZE ) {
827 lp->cur_tx -= TX_RING_SIZE;
828 lp->dirty_tx -= TX_RING_SIZE;
829 }
830
831 /* Trigger an immediate send poll. */
832 DREG = CSR0_INEA | CSR0_TDMD;
833 dev->trans_start = jiffies;
834
835 if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
836 TMD1_OWN_HOST)
837 netif_start_queue (dev);
838 else
839 lp->tx_full = 1;
840 spin_unlock_irqrestore (&lp->devlock, flags);
841
842 return 0;
843 }
844
845 /* The LANCE interrupt handler. */
846
847 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
848 {
849 struct net_device *dev = dev_id;
850 struct lance_private *lp;
851 struct lance_ioreg *IO;
852 int csr0, boguscnt = 10;
853
854 if (dev == NULL) {
855 DPRINTK( 1, ( "lance_interrupt(): interrupt for unknown device.\n" ));
856 return;
857 }
858
859 lp = (struct lance_private *)dev->priv;
860 IO = lp->iobase;
861 spin_lock (&lp->devlock);
862
863 AREG = CSR0;
864
865 while( ((csr0 = DREG) & (CSR0_ERR | CSR0_TINT | CSR0_RINT)) &&
866 --boguscnt >= 0) {
867 /* Acknowledge all of the current interrupt sources ASAP. */
868 DREG = csr0 & ~(CSR0_INIT | CSR0_STRT | CSR0_STOP |
869 CSR0_TDMD | CSR0_INEA);
870
871 DPRINTK( 2, ( "%s: interrupt csr0=%04x new csr=%04x.\n",
872 dev->name, csr0, DREG ));
873
874 if (csr0 & CSR0_RINT) /* Rx interrupt */
875 lance_rx( dev );
876
877 if (csr0 & CSR0_TINT) { /* Tx-done interrupt */
878 int dirty_tx = lp->dirty_tx;
879
880 while( dirty_tx < lp->cur_tx) {
881 int entry = dirty_tx & TX_RING_MOD_MASK;
882 int status = MEM->tx_head[entry].flag;
883
884 if (status & TMD1_OWN_CHIP)
885 break; /* It still hasn't been Txed */
886
887 MEM->tx_head[entry].flag = 0;
888
889 if (status & TMD1_ERR) {
890 /* There was an major error, log it. */
891 int err_status = MEM->tx_head[entry].misc;
892 lp->stats.tx_errors++;
893 if (err_status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
894 if (err_status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
895 if (err_status & TMD3_LCOL) lp->stats.tx_window_errors++;
896 if (err_status & TMD3_UFLO) {
897 /* Ackk! On FIFO errors the Tx unit is turned off! */
898 lp->stats.tx_fifo_errors++;
899 /* Remove this verbosity later! */
900 DPRINTK( 1, ( "%s: Tx FIFO error! Status %04x\n",
901 dev->name, csr0 ));
902 /* Restart the chip. */
903 DREG = CSR0_STRT;
904 }
905 } else {
906 if (status & (TMD1_MORE | TMD1_ONE | TMD1_DEF))
907 lp->stats.collisions++;
908 lp->stats.tx_packets++;
909 }
910
911 /* XXX MSch: free skb?? */
912 dirty_tx++;
913 }
914
915 #ifndef final_version
916 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
917 DPRINTK( 0, ( "out-of-sync dirty pointer,"
918 " %d vs. %d, full=%d.\n",
919 dirty_tx, lp->cur_tx, lp->tx_full ));
920 dirty_tx += TX_RING_SIZE;
921 }
922 #endif
923
924 if (lp->tx_full && (netif_queue_stopped(dev))
925 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
926 /* The ring is no longer full, clear tbusy. */
927 lp->tx_full = 0;
928 netif_wake_queue (dev);
929 }
930
931 lp->dirty_tx = dirty_tx;
932 }
933
934 /* Log misc errors. */
935 if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
936 if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
937 if (csr0 & CSR0_MERR) {
938 DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
939 "status %04x.\n", dev->name, csr0 ));
940 /* Restart the chip. */
941 DREG = CSR0_STRT;
942 }
943 }
944
945 /* Clear any other interrupt, and set interrupt enable. */
946 DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
947 CSR0_IDON | CSR0_INEA;
948
949 DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
950 dev->name, DREG ));
951
952 spin_unlock (&lp->devlock);
953 }
954
955
956 static int lance_rx( struct net_device *dev )
957
958 { struct lance_private *lp = (struct lance_private *)dev->priv;
959 int entry = lp->cur_rx & RX_RING_MOD_MASK;
960 int i;
961
962 DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
963 MEM->rx_head[entry].flag ));
964
965 /* If we own the next entry, it's a new packet. Send it up. */
966 while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
967 struct lance_rx_head *head = &(MEM->rx_head[entry]);
968 int status = head->flag;
969
970 if (status != (RMD1_ENP|RMD1_STP)) { /* There was an error. */
971 /* There is a tricky error noted by John Murphy,
972 <murf@perftech.com> to Russ Nelson: Even with full-sized
973 buffers it's possible for a jabber packet to use two
974 buffers, with only the last correctly noting the error. */
975 if (status & RMD1_ENP) /* Only count a general error at the */
976 lp->stats.rx_errors++; /* end of a packet.*/
977 if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
978 if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
979 if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
980 if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
981 head->flag &= (RMD1_ENP|RMD1_STP);
982 } else {
983 /* Malloc up new buffer, compatible with net-3. */
984 short pkt_len = head->msg_length & 0xfff;
985 struct sk_buff *skb;
986
987 if (pkt_len < 60) {
988 printk( "%s: Runt packet!\n", dev->name );
989 lp->stats.rx_errors++;
990 }
991 else {
992 skb = dev_alloc_skb( pkt_len+2 );
993 if (skb == NULL) {
994 DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
995 dev->name ));
996 for( i = 0; i < RX_RING_SIZE; i++ )
997 if (MEM->rx_head[(entry+i) & RX_RING_MOD_MASK].flag &
998 RMD1_OWN_CHIP)
999 break;
1000
1001 if (i > RX_RING_SIZE - 2) {
1002 lp->stats.rx_dropped++;
1003 head->flag |= RMD1_OWN_CHIP;
1004 lp->cur_rx++;
1005 }
1006 break;
1007 }
1008
1009 if (lance_debug >= 3) {
1010 u_char *data = PKTBUF_ADDR(head), *p;
1011 printk( "%s: RX pkt type 0x%04x from ", dev->name,
1012 ((u_short *)data)[6]);
1013 for( p = &data[6], i = 0; i < 6; i++ )
1014 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1015 printk(" to ");
1016 for( p = data, i = 0; i < 6; i++ )
1017 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1018 printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
1019 "len %d\n",
1020 data[15], data[16], data[17], data[18],
1021 data[19], data[20], data[21], data[22],
1022 pkt_len );
1023 }
1024
1025 skb->dev = dev;
1026 skb_reserve( skb, 2 ); /* 16 byte align */
1027 skb_put( skb, pkt_len ); /* Make room */
1028 lp->memcpy_f( skb->data, PKTBUF_ADDR(head), pkt_len );
1029 skb->protocol = eth_type_trans( skb, dev );
1030 netif_rx( skb );
1031 lp->stats.rx_packets++;
1032 lp->stats.rx_bytes += skb->len;
1033 }
1034 }
1035
1036 head->flag |= RMD1_OWN_CHIP;
1037 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1038 }
1039 lp->cur_rx &= RX_RING_MOD_MASK;
1040
1041 /* From lance.c (Donald Becker): */
1042 /* We should check that at least two ring entries are free. If not,
1043 we should free one and mark stats->rx_dropped++. */
1044
1045 return 0;
1046 }
1047
1048
1049 static int lance_close( struct net_device *dev )
1050
1051 { struct lance_private *lp = (struct lance_private *)dev->priv;
1052 struct lance_ioreg *IO = lp->iobase;
1053
1054 netif_stop_queue (dev);
1055
1056 AREG = CSR0;
1057
1058 DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
1059 dev->name, DREG ));
1060
1061 /* We stop the LANCE here -- it occasionally polls
1062 memory if we don't. */
1063 DREG = CSR0_STOP;
1064
1065 return 0;
1066 }
1067
1068
1069 static struct net_device_stats *lance_get_stats( struct net_device *dev )
1070
1071 { struct lance_private *lp = (struct lance_private *)dev->priv;
1072
1073 return &lp->stats;
1074 }
1075
1076
1077 /* Set or clear the multicast filter for this adaptor.
1078 num_addrs == -1 Promiscuous mode, receive all packets
1079 num_addrs == 0 Normal mode, clear multicast list
1080 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
1081 best-effort filtering.
1082 */
1083
1084 static void set_multicast_list( struct net_device *dev )
1085
1086 { struct lance_private *lp = (struct lance_private *)dev->priv;
1087 struct lance_ioreg *IO = lp->iobase;
1088
1089 if (netif_running(dev))
1090 /* Only possible if board is already started */
1091 return;
1092
1093 /* We take the simple way out and always enable promiscuous mode. */
1094 DREG = CSR0_STOP; /* Temporarily stop the lance. */
1095
1096 if (dev->flags & IFF_PROMISC) {
1097 /* Log any net taps. */
1098 DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
1099 REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
1100 } else {
1101 short multicast_table[4];
1102 int num_addrs = dev->mc_count;
1103 int i;
1104 /* We don't use the multicast table, but rely on upper-layer
1105 * filtering. */
1106 memset( multicast_table, (num_addrs == 0) ? 0 : -1,
1107 sizeof(multicast_table) );
1108 for( i = 0; i < 4; i++ )
1109 REGA( CSR8+i ) = multicast_table[i];
1110 REGA( CSR15 ) = 0; /* Unset promiscuous mode */
1111 }
1112
1113 /*
1114 * Always set BSWP after a STOP as STOP puts it back into
1115 * little endian mode.
1116 */
1117 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
1118
1119 /* Resume normal operation and reset AREG to CSR0 */
1120 REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
1121 }
1122
1123
1124 /* This is needed for old RieblCards and possible for new RieblCards */
1125
1126 static int lance_set_mac_address( struct net_device *dev, void *addr )
1127
1128 { struct lance_private *lp = (struct lance_private *)dev->priv;
1129 struct sockaddr *saddr = addr;
1130 int i;
1131
1132 if (lp->cardtype != OLD_RIEBL && lp->cardtype != NEW_RIEBL)
1133 return( -EOPNOTSUPP );
1134
1135 if (netif_running(dev)) {
1136 /* Only possible while card isn't started */
1137 DPRINTK( 1, ( "%s: hwaddr can be set only while card isn't open.\n",
1138 dev->name ));
1139 return( -EIO );
1140 }
1141
1142 memcpy( dev->dev_addr, saddr->sa_data, dev->addr_len );
1143 for( i = 0; i < 6; i++ )
1144 MEM->init.hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
1145 lp->memcpy_f( RIEBL_HWADDR_ADDR, dev->dev_addr, 6 );
1146 /* set also the magic for future sessions */
1147 *RIEBL_MAGIC_ADDR = RIEBL_MAGIC;
1148
1149 return( 0 );
1150 }
1151
1152
1153 #ifdef MODULE
1154 static struct net_device atarilance_dev;
1155
1156 int init_module(void)
1157
1158 { int err;
1159
1160 atarilance_dev.init = atarilance_probe;
1161 if ((err = register_netdev( &atarilance_dev ))) {
1162 if (err == -EIO) {
1163 printk( "No Atari Lance board found. Module not loaded.\n");
1164 }
1165 return( err );
1166 }
1167 return( 0 );
1168 }
1169
1170 void cleanup_module(void)
1171
1172 {
1173 unregister_netdev( &atarilance_dev );
1174 }
1175
1176 #endif /* MODULE */
1177
1178
1179 /*
1180 * Local variables:
1181 * c-indent-level: 4
1182 * tab-width: 4
1183 * End:
1184 */
1185
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.