2 * 7990.c -- LANCE ethernet IC generic routines.
3 * This is an attempt to separate out the bits of various ethernet
4 * drivers that are common because they all use the AMD 7990 LANCE
5 * (Local Area Network Controller for Ethernet) chip.
7 * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
9 * Most of this stuff was obtained by looking at other LANCE drivers,
10 * in particular a2065.[ch]. The AMD C-LANCE datasheet was also helpful.
11 * NB: this was made easy by the fact that Jes Sorensen had cleaned up
12 * most of a2025 and sunlance with the aim of merging them, so the
13 * common code was pretty obvious.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/fcntl.h>
20 #include <linux/interrupt.h>
21 #include <linux/ptrace.h>
22 #include <linux/ioport.h>
24 #include <linux/malloc.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <asm/system.h>
29 #include <asm/bitops.h>
32 #include <asm/pgtable.h>
33 #include <linux/errno.h>
35 /* Used for the temporal inet entries and routing */
36 #include <linux/socket.h>
37 #include <linux/route.h>
39 #include <linux/dio.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
47 /* Lossage Factor Nine, Mr Sulu. */
48 #define WRITERAP(x) (lp->writerap(lp,x))
49 #define WRITERDP(x) (lp->writerdp(lp,x))
50 #define READRDP() (lp->readrdp(lp))
51 /* These used to be ll->rap = x, ll->rdp = x, and (ll->rdp). Sigh.
52 * If you want to switch them back then
53 * #define DECLARE_LL volatile struct lance_regs *ll = lp->ll
55 #define DECLARE_LL /* nothing to declare */
57 /* debugging output macros, various flavours */
58 /* #define TEST_HITS */
60 #define PRINT_RINGS() \
63 for (t=0; t < RX_RING_SIZE; t++) { \
64 printk("R%d: @(%02X %04X) len %04X, mblen %04X, bits %02X\n",\
65 t, ib->brx_ring[t].rmd1_hadr, ib->brx_ring[t].rmd0,\
66 ib->brx_ring[t].length,\
67 ib->brx_ring[t].mblength, ib->brx_ring[t].rmd1_bits);\
69 for (t=0; t < TX_RING_SIZE; t++) { \
70 printk("T%d: @(%02X %04X) len %04X, misc %04X, bits %02X\n",\
71 t, ib->btx_ring[t].tmd1_hadr, ib->btx_ring[t].tmd0,\
72 ib->btx_ring[t].length,\
73 ib->btx_ring[t].misc, ib->btx_ring[t].tmd1_bits);\
80 /* Load the CSR registers. The LANCE has to be STOPped when we do this! */
81 static void load_csrs (struct lance_private
*lp
)
83 volatile struct lance_init_block
*aib
= lp
->lance_init_block
;
87 leptr
= LANCE_ADDR (aib
);
89 WRITERAP(LE_CSR1
); /* load address of init block */
90 WRITERDP(leptr
& 0xFFFF);
92 WRITERDP(leptr
>> 16);
94 WRITERDP(lp
->busmaster_regval
); /* set byteswap/ALEctrl/byte ctrl */
96 /* Point back to csr0 */
100 /* #define to 0 or 1 appropriately */
101 #define DEBUG_IRING 0
102 /* Set up the Lance Rx and Tx rings and the init block */
103 /* Sets dev->tbusy */
104 static void lance_init_ring (struct net_device
*dev
)
106 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
107 volatile struct lance_init_block
*ib
= lp
->init_block
;
108 volatile struct lance_init_block
*aib
; /* for LANCE_ADDR computations */
112 aib
= lp
->lance_init_block
;
114 /* Lock out other processes while setting up hardware */
116 lp
->rx_new
= lp
->tx_new
= 0;
117 lp
->rx_old
= lp
->tx_old
= 0;
119 ib
->mode
= LE_MO_PROM
; /* normal, enable Tx & Rx */
121 /* Copy the ethernet address to the lance init block
122 * Notice that we do a byteswap if we're big endian.
123 * [I think this is the right criterion; at least, sunlance,
124 * a2065 and atarilance do the byteswap and lance.c (PC) doesn't.
125 * However, the datasheet says that the BSWAP bit doesn't affect
126 * the init block, so surely it should be low byte first for
128 * We could define the ib->physaddr as three 16bit values and
129 * use (addr[1] << 8) | addr[0] & co, but this is more efficient.
132 ib
->phys_addr
[0] = dev
->dev_addr
[1];
133 ib
->phys_addr
[1] = dev
->dev_addr
[0];
134 ib
->phys_addr
[2] = dev
->dev_addr
[3];
135 ib
->phys_addr
[3] = dev
->dev_addr
[2];
136 ib
->phys_addr
[4] = dev
->dev_addr
[5];
137 ib
->phys_addr
[5] = dev
->dev_addr
[4];
140 ib
->phys_addr
[i
] = dev
->dev_addr
[i
];
144 printk ("TX rings:\n");
146 /* Setup the Tx ring entries */
147 for (i
= 0; i
< (1<<lp
->lance_log_tx_bufs
); i
++) {
148 leptr
= LANCE_ADDR(&aib
->tx_buf
[i
][0]);
149 ib
->btx_ring
[i
].tmd0
= leptr
;
150 ib
->btx_ring
[i
].tmd1_hadr
= leptr
>> 16;
151 ib
->btx_ring
[i
].tmd1_bits
= 0;
152 ib
->btx_ring
[i
].length
= 0xf000; /* The ones required by tmd2 */
153 ib
->btx_ring
[i
].misc
= 0;
155 printk ("%d: 0x%8.8x\n", i
, leptr
);
158 /* Setup the Rx ring entries */
160 printk ("RX rings:\n");
161 for (i
= 0; i
< (1<<lp
->lance_log_rx_bufs
); i
++) {
162 leptr
= LANCE_ADDR(&aib
->rx_buf
[i
][0]);
164 ib
->brx_ring
[i
].rmd0
= leptr
;
165 ib
->brx_ring
[i
].rmd1_hadr
= leptr
>> 16;
166 ib
->brx_ring
[i
].rmd1_bits
= LE_R1_OWN
;
167 /* 0xf000 == bits that must be one (reserved, presumably) */
168 ib
->brx_ring
[i
].length
= -RX_BUFF_SIZE
| 0xf000;
169 ib
->brx_ring
[i
].mblength
= 0;
171 printk ("%d: 0x%8.8x\n", i
, leptr
);
174 /* Setup the initialization block */
176 /* Setup rx descriptor pointer */
177 leptr
= LANCE_ADDR(&aib
->brx_ring
);
178 ib
->rx_len
= (lp
->lance_log_rx_bufs
<< 13) | (leptr
>> 16);
181 printk ("RX ptr: %8.8x\n", leptr
);
183 /* Setup tx descriptor pointer */
184 leptr
= LANCE_ADDR(&aib
->btx_ring
);
185 ib
->tx_len
= (lp
->lance_log_tx_bufs
<< 13) | (leptr
>> 16);
188 printk ("TX ptr: %8.8x\n", leptr
);
190 /* Clear the multicast filter */
196 /* LANCE must be STOPped before we do this, too... */
197 static int init_restart_lance (struct lance_private
*lp
)
203 WRITERDP(LE_C0_INIT
);
205 /* Need a hook here for sunlance ledma stuff */
207 /* Wait for the lance to complete initialization */
208 for (i
= 0; (i
< 100) && !(READRDP() & (LE_C0_ERR
| LE_C0_IDON
)); i
++)
210 if ((i
== 100) || (READRDP() & LE_C0_ERR
)) {
211 printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i
, READRDP());
215 /* Clear IDON by writing a "1", enable interrupts and start lance */
216 WRITERDP(LE_C0_IDON
);
217 WRITERDP(LE_C0_INEA
| LE_C0_STRT
);
222 static int lance_reset (struct net_device
*dev
)
224 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
230 WRITERDP(LE_C0_STOP
);
233 lance_init_ring (dev
);
234 dev
->trans_start
= jiffies
;
238 status
= init_restart_lance (lp
);
240 printk ("Lance restart=%d\n", status
);
245 static int lance_rx (struct net_device
*dev
)
247 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
248 volatile struct lance_init_block
*ib
= lp
->init_block
;
249 volatile struct lance_rx_desc
*rd
;
251 int len
= 0; /* XXX shut up gcc warnings */
252 struct sk_buff
*skb
= 0; /* XXX shut up gcc warnings */
260 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
263 ib
->brx_ring
[i
].rmd1_bits
& LE_R1_OWN
? "_" : "X");
266 ib
->brx_ring
[i
].rmd1_bits
& LE_R1_OWN
? "." : "1");
271 WRITERDP(LE_C0_RINT
| LE_C0_INEA
); /* ack Rx int, reenable ints */
272 for (rd
= &ib
->brx_ring
[lp
->rx_new
]; /* For each Rx ring we own... */
273 !((bits
= rd
->rmd1_bits
) & LE_R1_OWN
);
274 rd
= &ib
->brx_ring
[lp
->rx_new
]) {
276 /* We got an incomplete frame? */
277 if ((bits
& LE_R1_POK
) != LE_R1_POK
) {
278 lp
->stats
.rx_over_errors
++;
279 lp
->stats
.rx_errors
++;
281 } else if (bits
& LE_R1_ERR
) {
282 /* Count only the end frame as a rx error,
285 if (bits
& LE_R1_BUF
) lp
->stats
.rx_fifo_errors
++;
286 if (bits
& LE_R1_CRC
) lp
->stats
.rx_crc_errors
++;
287 if (bits
& LE_R1_OFL
) lp
->stats
.rx_over_errors
++;
288 if (bits
& LE_R1_FRA
) lp
->stats
.rx_frame_errors
++;
289 if (bits
& LE_R1_EOP
) lp
->stats
.rx_errors
++;
291 len
= (rd
->mblength
& 0xfff) - 4;
292 skb
= dev_alloc_skb (len
+2);
295 printk ("%s: Memory squeeze, deferring packet.\n",
297 lp
->stats
.rx_dropped
++;
299 rd
->rmd1_bits
= LE_R1_OWN
;
300 lp
->rx_new
= (lp
->rx_new
+ 1) & lp
->rx_ring_mod_mask
;
305 skb_reserve (skb
, 2); /* 16 byte align */
306 skb_put (skb
, len
); /* make room */
307 eth_copy_and_sum(skb
,
308 (unsigned char *)&(ib
->rx_buf
[lp
->rx_new
][0]),
310 skb
->protocol
= eth_type_trans (skb
, dev
);
312 lp
->stats
.rx_packets
++;
315 /* Return the packet to the pool */
317 rd
->rmd1_bits
= LE_R1_OWN
;
318 lp
->rx_new
= (lp
->rx_new
+ 1) & lp
->rx_ring_mod_mask
;
323 static int lance_tx (struct net_device
*dev
)
325 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
326 volatile struct lance_init_block
*ib
= lp
->init_block
;
327 volatile struct lance_tx_desc
*td
;
333 WRITERDP(LE_C0_TINT
| LE_C0_INEA
);
337 for (i
= j
; i
!= lp
->tx_new
; i
= j
) {
338 td
= &ib
->btx_ring
[i
];
340 /* If we hit a packet not owned by us, stop */
341 if (td
->tmd1_bits
& LE_T1_OWN
)
344 if (td
->tmd1_bits
& LE_T1_ERR
) {
347 lp
->stats
.tx_errors
++;
348 if (status
& LE_T3_RTY
) lp
->stats
.tx_aborted_errors
++;
349 if (status
& LE_T3_LCOL
) lp
->stats
.tx_window_errors
++;
351 if (status
& LE_T3_CLOS
) {
352 lp
->stats
.tx_carrier_errors
++;
353 if (lp
->auto_select
) {
354 lp
->tpe
= 1 - lp
->tpe
;
355 printk("%s: Carrier Lost, trying %s\n",
356 dev
->name
, lp
->tpe
?"TPE":"AUI");
359 WRITERDP(LE_C0_STOP
);
360 lance_init_ring (dev
);
362 init_restart_lance (lp
);
367 /* buffer errors and underflows turn off the transmitter */
368 /* Restart the adapter */
369 if (status
& (LE_T3_BUF
|LE_T3_UFL
)) {
370 lp
->stats
.tx_fifo_errors
++;
372 printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
376 WRITERDP(LE_C0_STOP
);
377 lance_init_ring (dev
);
379 init_restart_lance (lp
);
382 } else if ((td
->tmd1_bits
& LE_T1_POK
) == LE_T1_POK
) {
384 * So we don't count the packet more than once.
386 td
->tmd1_bits
&= ~(LE_T1_POK
);
388 /* One collision before packet was sent. */
389 if (td
->tmd1_bits
& LE_T1_EONE
)
390 lp
->stats
.collisions
++;
392 /* More than one collision, be optimistic. */
393 if (td
->tmd1_bits
& LE_T1_EMORE
)
394 lp
->stats
.collisions
+= 2;
396 lp
->stats
.tx_packets
++;
399 j
= (j
+ 1) & lp
->tx_ring_mod_mask
;
402 WRITERDP(LE_C0_TINT
| LE_C0_INEA
);
406 static void lance_interrupt (int irq
, void *dev_id
, struct pt_regs
*regs
)
408 struct net_device
*dev
= (struct net_device
*)dev_id
;
409 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
413 WRITERAP(LE_CSR0
); /* LANCE Controller Status */
418 if (!(csr0
& LE_C0_INTR
)) /* Check if any interrupt has */
419 return; /* been generated by the Lance. */
422 printk ("%s: again", dev
->name
);
426 /* Acknowledge all the interrupt sources ASAP */
427 WRITERDP(csr0
& ~(LE_C0_INEA
|LE_C0_TDMD
|LE_C0_STOP
|LE_C0_STRT
|LE_C0_INIT
));
429 if ((csr0
& LE_C0_ERR
)) {
430 /* Clear the error condition */
431 WRITERDP(LE_C0_BABL
|LE_C0_ERR
|LE_C0_MISS
|LE_C0_INEA
);
434 if (csr0
& LE_C0_RINT
)
437 if (csr0
& LE_C0_TINT
)
440 /* Log misc errors. */
441 if (csr0
& LE_C0_BABL
)
442 lp
->stats
.tx_errors
++; /* Tx babble. */
443 if (csr0
& LE_C0_MISS
)
444 lp
->stats
.rx_errors
++; /* Missed a Rx frame. */
445 if (csr0
& LE_C0_MERR
) {
446 printk("%s: Bus master arbitration failure, status %4.4x.\n",
448 /* Restart the chip. */
449 WRITERDP(LE_C0_STRT
);
452 if ((TX_BUFFS_AVAIL
>= 0) && dev
->tbusy
) {
458 WRITERDP(LE_C0_BABL
|LE_C0_CERR
|LE_C0_MISS
|LE_C0_MERR
|LE_C0_IDON
|LE_C0_INEA
);
463 int lance_open (struct net_device
*dev
)
465 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
468 /* Install the Interrupt handler. Or we could shunt this out to specific drivers? */
469 if (request_irq(lp
->irq
, lance_interrupt
, 0, lp
->name
, dev
))
472 return lance_reset(dev
);
475 int lance_close (struct net_device
*dev
)
477 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
485 WRITERDP(LE_C0_STOP
);
487 free_irq(lp
->irq
, dev
);
492 int lance_start_xmit (struct sk_buff
*skb
, struct net_device
*dev
)
494 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
495 volatile struct lance_init_block
*ib
= lp
->init_block
;
496 int entry
, skblen
, len
;
503 /* Transmitter timeout, serious problems */
505 int tickssofar
= jiffies
- dev
->trans_start
;
507 if (tickssofar
< 100) {
510 printk ("%s: transmit timed out, status %04x, resetting\n",
511 dev
->name
, READRDP());
517 /* Block a timer-based transmit from overlapping. */
518 if (test_and_set_bit (0, (void *) &dev
->tbusy
) != 0) {
519 printk ("Transmitter access conflict.\n");
529 /* dump the packet */
533 for (i
= 0; i
< 64; i
++) {
536 printk ("%2.2x ", skb
->data
[i
]);
540 len
= (skblen
<= ETH_ZLEN
) ? ETH_ZLEN
: skblen
;
541 entry
= lp
->tx_new
& lp
->tx_ring_mod_mask
;
542 ib
->btx_ring
[entry
].length
= (-len
) | 0xf000;
543 ib
->btx_ring
[entry
].misc
= 0;
545 memcpy ((char *)&ib
->tx_buf
[entry
][0], skb
->data
, skblen
);
547 /* Now, give the packet to the lance */
548 ib
->btx_ring
[entry
].tmd1_bits
= (LE_T1_POK
|LE_T1_OWN
);
549 lp
->tx_new
= (lp
->tx_new
+1) & lp
->tx_ring_mod_mask
;
552 /* Kick the lance: transmit now */
553 WRITERDP(LE_C0_INEA
| LE_C0_TDMD
);
554 dev
->trans_start
= jiffies
;
563 struct net_device_stats
*lance_get_stats (struct net_device
*dev
)
565 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
570 /* taken from the depca driver via a2065.c */
571 static void lance_load_multicast (struct net_device
*dev
)
573 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
574 volatile struct lance_init_block
*ib
= lp
->init_block
;
575 volatile u16
*mcast_table
= (u16
*)&ib
->filter
;
576 struct dev_mc_list
*dmi
=dev
->mc_list
;
579 u32 crc
, poly
= CRC_POLYNOMIAL_LE
;
581 /* set all multicast bits */
582 if (dev
->flags
& IFF_ALLMULTI
){
583 ib
->filter
[0] = 0xffffffff;
584 ib
->filter
[1] = 0xffffffff;
587 /* clear the multicast filter */
592 for (i
= 0; i
< dev
->mc_count
; i
++){
593 addrs
= dmi
->dmi_addr
;
596 /* multicast address? */
601 for (byte
= 0; byte
< 6; byte
++)
602 for (bit
= *addrs
++, j
= 0; j
< 8; j
++, bit
>>=1)
606 test
= ((bit
^ crc
) & 0x01);
616 mcast_table
[crc
>> 4] |= 1 << (crc
& 0xf);
622 void lance_set_multicast (struct net_device
*dev
)
624 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
625 volatile struct lance_init_block
*ib
= lp
->init_block
;
630 set_bit (0, (void *) &dev
->tbusy
);
632 while (lp
->tx_old
!= lp
->tx_new
)
636 WRITERDP(LE_C0_STOP
);
637 lance_init_ring (dev
);
639 if (dev
->flags
& IFF_PROMISC
) {
640 ib
->mode
|= LE_MO_PROM
;
642 ib
->mode
&= ~LE_MO_PROM
;
643 lance_load_multicast (dev
);
646 init_restart_lance (lp
);