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/crc32.h>
16 #include <linux/delay.h>
17 #include <linux/dio.h>
18 #include <linux/errno.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioport.h>
29 #include <linux/route.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/skbuff.h>
33 /* Used for the temporal inet entries and routing */
34 #include <linux/socket.h>
36 #include <asm/system.h>
37 #include <asm/bitops.h>
40 #include <asm/pgtable.h>
44 /* Lossage Factor Nine, Mr Sulu. */
45 #define WRITERAP(x) (lp->writerap(lp,x))
46 #define WRITERDP(x) (lp->writerdp(lp,x))
47 #define READRDP() (lp->readrdp(lp))
48 /* These used to be ll->rap = x, ll->rdp = x, and (ll->rdp). Sigh.
49 * If you want to switch them back then
50 * #define DECLARE_LL volatile struct lance_regs *ll = lp->ll
52 #define DECLARE_LL /* nothing to declare */
54 /* debugging output macros, various flavours */
55 /* #define TEST_HITS */
57 #define PRINT_RINGS() \
60 for (t=0; t < RX_RING_SIZE; t++) { \
61 printk("R%d: @(%02X %04X) len %04X, mblen %04X, bits %02X\n",\
62 t, ib->brx_ring[t].rmd1_hadr, ib->brx_ring[t].rmd0,\
63 ib->brx_ring[t].length,\
64 ib->brx_ring[t].mblength, ib->brx_ring[t].rmd1_bits);\
66 for (t=0; t < TX_RING_SIZE; t++) { \
67 printk("T%d: @(%02X %04X) len %04X, misc %04X, bits %02X\n",\
68 t, ib->btx_ring[t].tmd1_hadr, ib->btx_ring[t].tmd0,\
69 ib->btx_ring[t].length,\
70 ib->btx_ring[t].misc, ib->btx_ring[t].tmd1_bits);\
77 /* Load the CSR registers. The LANCE has to be STOPped when we do this! */
78 static void load_csrs (struct lance_private
*lp
)
80 volatile struct lance_init_block
*aib
= lp
->lance_init_block
;
84 leptr
= LANCE_ADDR (aib
);
86 WRITERAP(LE_CSR1
); /* load address of init block */
87 WRITERDP(leptr
& 0xFFFF);
89 WRITERDP(leptr
>> 16);
91 WRITERDP(lp
->busmaster_regval
); /* set byteswap/ALEctrl/byte ctrl */
93 /* Point back to csr0 */
97 /* #define to 0 or 1 appropriately */
99 /* Set up the Lance Rx and Tx rings and the init block */
100 static void lance_init_ring (struct net_device
*dev
)
102 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
103 volatile struct lance_init_block
*ib
= lp
->init_block
;
104 volatile struct lance_init_block
*aib
; /* for LANCE_ADDR computations */
108 aib
= lp
->lance_init_block
;
110 lp
->rx_new
= lp
->tx_new
= 0;
111 lp
->rx_old
= lp
->tx_old
= 0;
113 ib
->mode
= LE_MO_PROM
; /* normal, enable Tx & Rx */
115 /* Copy the ethernet address to the lance init block
116 * Notice that we do a byteswap if we're big endian.
117 * [I think this is the right criterion; at least, sunlance,
118 * a2065 and atarilance do the byteswap and lance.c (PC) doesn't.
119 * However, the datasheet says that the BSWAP bit doesn't affect
120 * the init block, so surely it should be low byte first for
122 * We could define the ib->physaddr as three 16bit values and
123 * use (addr[1] << 8) | addr[0] & co, but this is more efficient.
126 ib
->phys_addr
[0] = dev
->dev_addr
[1];
127 ib
->phys_addr
[1] = dev
->dev_addr
[0];
128 ib
->phys_addr
[2] = dev
->dev_addr
[3];
129 ib
->phys_addr
[3] = dev
->dev_addr
[2];
130 ib
->phys_addr
[4] = dev
->dev_addr
[5];
131 ib
->phys_addr
[5] = dev
->dev_addr
[4];
134 ib
->phys_addr
[i
] = dev
->dev_addr
[i
];
138 printk ("TX rings:\n");
141 /* Setup the Tx ring entries */
142 for (i
= 0; i
< (1<<lp
->lance_log_tx_bufs
); i
++) {
143 leptr
= LANCE_ADDR(&aib
->tx_buf
[i
][0]);
144 ib
->btx_ring
[i
].tmd0
= leptr
;
145 ib
->btx_ring
[i
].tmd1_hadr
= leptr
>> 16;
146 ib
->btx_ring
[i
].tmd1_bits
= 0;
147 ib
->btx_ring
[i
].length
= 0xf000; /* The ones required by tmd2 */
148 ib
->btx_ring
[i
].misc
= 0;
150 printk ("%d: 0x%8.8x\n", i
, leptr
);
153 /* Setup the Rx ring entries */
155 printk ("RX rings:\n");
156 for (i
= 0; i
< (1<<lp
->lance_log_rx_bufs
); i
++) {
157 leptr
= LANCE_ADDR(&aib
->rx_buf
[i
][0]);
159 ib
->brx_ring
[i
].rmd0
= leptr
;
160 ib
->brx_ring
[i
].rmd1_hadr
= leptr
>> 16;
161 ib
->brx_ring
[i
].rmd1_bits
= LE_R1_OWN
;
162 /* 0xf000 == bits that must be one (reserved, presumably) */
163 ib
->brx_ring
[i
].length
= -RX_BUFF_SIZE
| 0xf000;
164 ib
->brx_ring
[i
].mblength
= 0;
166 printk ("%d: 0x%8.8x\n", i
, leptr
);
169 /* Setup the initialization block */
171 /* Setup rx descriptor pointer */
172 leptr
= LANCE_ADDR(&aib
->brx_ring
);
173 ib
->rx_len
= (lp
->lance_log_rx_bufs
<< 13) | (leptr
>> 16);
176 printk ("RX ptr: %8.8x\n", leptr
);
178 /* Setup tx descriptor pointer */
179 leptr
= LANCE_ADDR(&aib
->btx_ring
);
180 ib
->tx_len
= (lp
->lance_log_tx_bufs
<< 13) | (leptr
>> 16);
183 printk ("TX ptr: %8.8x\n", leptr
);
185 /* Clear the multicast filter */
191 /* LANCE must be STOPped before we do this, too... */
192 static int init_restart_lance (struct lance_private
*lp
)
198 WRITERDP(LE_C0_INIT
);
200 /* Need a hook here for sunlance ledma stuff */
202 /* Wait for the lance to complete initialization */
203 for (i
= 0; (i
< 100) && !(READRDP() & (LE_C0_ERR
| LE_C0_IDON
)); i
++)
205 if ((i
== 100) || (READRDP() & LE_C0_ERR
)) {
206 printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i
, READRDP());
210 /* Clear IDON by writing a "1", enable interrupts and start lance */
211 WRITERDP(LE_C0_IDON
);
212 WRITERDP(LE_C0_INEA
| LE_C0_STRT
);
217 static int lance_reset (struct net_device
*dev
)
219 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
225 WRITERDP(LE_C0_STOP
);
228 lance_init_ring (dev
);
229 dev
->trans_start
= jiffies
;
230 status
= init_restart_lance (lp
);
232 printk ("Lance restart=%d\n", status
);
237 static int lance_rx (struct net_device
*dev
)
239 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
240 volatile struct lance_init_block
*ib
= lp
->init_block
;
241 volatile struct lance_rx_desc
*rd
;
243 int len
= 0; /* XXX shut up gcc warnings */
244 struct sk_buff
*skb
= 0; /* XXX shut up gcc warnings */
252 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
255 ib
->brx_ring
[i
].rmd1_bits
& LE_R1_OWN
? "_" : "X");
258 ib
->brx_ring
[i
].rmd1_bits
& LE_R1_OWN
? "." : "1");
263 WRITERDP(LE_C0_RINT
| LE_C0_INEA
); /* ack Rx int, reenable ints */
264 for (rd
= &ib
->brx_ring
[lp
->rx_new
]; /* For each Rx ring we own... */
265 !((bits
= rd
->rmd1_bits
) & LE_R1_OWN
);
266 rd
= &ib
->brx_ring
[lp
->rx_new
]) {
268 /* We got an incomplete frame? */
269 if ((bits
& LE_R1_POK
) != LE_R1_POK
) {
270 lp
->stats
.rx_over_errors
++;
271 lp
->stats
.rx_errors
++;
273 } else if (bits
& LE_R1_ERR
) {
274 /* Count only the end frame as a rx error,
277 if (bits
& LE_R1_BUF
) lp
->stats
.rx_fifo_errors
++;
278 if (bits
& LE_R1_CRC
) lp
->stats
.rx_crc_errors
++;
279 if (bits
& LE_R1_OFL
) lp
->stats
.rx_over_errors
++;
280 if (bits
& LE_R1_FRA
) lp
->stats
.rx_frame_errors
++;
281 if (bits
& LE_R1_EOP
) lp
->stats
.rx_errors
++;
283 len
= (rd
->mblength
& 0xfff) - 4;
284 skb
= dev_alloc_skb (len
+2);
287 printk ("%s: Memory squeeze, deferring packet.\n",
289 lp
->stats
.rx_dropped
++;
291 rd
->rmd1_bits
= LE_R1_OWN
;
292 lp
->rx_new
= (lp
->rx_new
+ 1) & lp
->rx_ring_mod_mask
;
297 skb_reserve (skb
, 2); /* 16 byte align */
298 skb_put (skb
, len
); /* make room */
299 eth_copy_and_sum(skb
,
300 (unsigned char *)&(ib
->rx_buf
[lp
->rx_new
][0]),
302 skb
->protocol
= eth_type_trans (skb
, dev
);
304 dev
->last_rx
= jiffies
;
305 lp
->stats
.rx_packets
++;
306 lp
->stats
.rx_bytes
+= len
;
309 /* Return the packet to the pool */
311 rd
->rmd1_bits
= LE_R1_OWN
;
312 lp
->rx_new
= (lp
->rx_new
+ 1) & lp
->rx_ring_mod_mask
;
317 static int lance_tx (struct net_device
*dev
)
319 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
320 volatile struct lance_init_block
*ib
= lp
->init_block
;
321 volatile struct lance_tx_desc
*td
;
327 WRITERDP(LE_C0_TINT
| LE_C0_INEA
);
331 for (i
= j
; i
!= lp
->tx_new
; i
= j
) {
332 td
= &ib
->btx_ring
[i
];
334 /* If we hit a packet not owned by us, stop */
335 if (td
->tmd1_bits
& LE_T1_OWN
)
338 if (td
->tmd1_bits
& LE_T1_ERR
) {
341 lp
->stats
.tx_errors
++;
342 if (status
& LE_T3_RTY
) lp
->stats
.tx_aborted_errors
++;
343 if (status
& LE_T3_LCOL
) lp
->stats
.tx_window_errors
++;
345 if (status
& LE_T3_CLOS
) {
346 lp
->stats
.tx_carrier_errors
++;
347 if (lp
->auto_select
) {
348 lp
->tpe
= 1 - lp
->tpe
;
349 printk("%s: Carrier Lost, trying %s\n",
350 dev
->name
, lp
->tpe
?"TPE":"AUI");
353 WRITERDP(LE_C0_STOP
);
354 lance_init_ring (dev
);
356 init_restart_lance (lp
);
361 /* buffer errors and underflows turn off the transmitter */
362 /* Restart the adapter */
363 if (status
& (LE_T3_BUF
|LE_T3_UFL
)) {
364 lp
->stats
.tx_fifo_errors
++;
366 printk ("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
370 WRITERDP(LE_C0_STOP
);
371 lance_init_ring (dev
);
373 init_restart_lance (lp
);
376 } else if ((td
->tmd1_bits
& LE_T1_POK
) == LE_T1_POK
) {
378 * So we don't count the packet more than once.
380 td
->tmd1_bits
&= ~(LE_T1_POK
);
382 /* One collision before packet was sent. */
383 if (td
->tmd1_bits
& LE_T1_EONE
)
384 lp
->stats
.collisions
++;
386 /* More than one collision, be optimistic. */
387 if (td
->tmd1_bits
& LE_T1_EMORE
)
388 lp
->stats
.collisions
+= 2;
390 lp
->stats
.tx_packets
++;
393 j
= (j
+ 1) & lp
->tx_ring_mod_mask
;
396 WRITERDP(LE_C0_TINT
| LE_C0_INEA
);
401 lance_interrupt (int irq
, void *dev_id
, struct pt_regs
*regs
)
403 struct net_device
*dev
= (struct net_device
*)dev_id
;
404 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
408 spin_lock (&lp
->devlock
);
410 WRITERAP(LE_CSR0
); /* LANCE Controller Status */
415 if (!(csr0
& LE_C0_INTR
)) { /* Check if any interrupt has */
416 spin_lock (&lp
->devlock
);
417 return IRQ_NONE
; /* been generated by the Lance. */
420 /* Acknowledge all the interrupt sources ASAP */
421 WRITERDP(csr0
& ~(LE_C0_INEA
|LE_C0_TDMD
|LE_C0_STOP
|LE_C0_STRT
|LE_C0_INIT
));
423 if ((csr0
& LE_C0_ERR
)) {
424 /* Clear the error condition */
425 WRITERDP(LE_C0_BABL
|LE_C0_ERR
|LE_C0_MISS
|LE_C0_INEA
);
428 if (csr0
& LE_C0_RINT
)
431 if (csr0
& LE_C0_TINT
)
434 /* Log misc errors. */
435 if (csr0
& LE_C0_BABL
)
436 lp
->stats
.tx_errors
++; /* Tx babble. */
437 if (csr0
& LE_C0_MISS
)
438 lp
->stats
.rx_errors
++; /* Missed a Rx frame. */
439 if (csr0
& LE_C0_MERR
) {
440 printk("%s: Bus master arbitration failure, status %4.4x.\n",
442 /* Restart the chip. */
443 WRITERDP(LE_C0_STRT
);
446 if (lp
->tx_full
&& netif_queue_stopped(dev
) && (TX_BUFFS_AVAIL
>= 0)) {
448 netif_wake_queue (dev
);
452 WRITERDP(LE_C0_BABL
|LE_C0_CERR
|LE_C0_MISS
|LE_C0_MERR
|LE_C0_IDON
|LE_C0_INEA
);
454 spin_unlock (&lp
->devlock
);
458 int lance_open (struct net_device
*dev
)
460 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
464 /* Install the Interrupt handler. Or we could shunt this out to specific drivers? */
465 if (request_irq(lp
->irq
, lance_interrupt
, 0, lp
->name
, dev
))
468 res
= lance_reset(dev
);
469 lp
->devlock
= SPIN_LOCK_UNLOCKED
;
470 netif_start_queue (dev
);
475 int lance_close (struct net_device
*dev
)
477 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
480 netif_stop_queue (dev
);
484 WRITERDP(LE_C0_STOP
);
486 free_irq(lp
->irq
, dev
);
491 void lance_tx_timeout(struct net_device
*dev
)
493 printk("lance_tx_timeout\n");
495 dev
->trans_start
= jiffies
;
496 netif_wake_queue (dev
);
500 int lance_start_xmit (struct sk_buff
*skb
, struct net_device
*dev
)
502 struct lance_private
*lp
= (struct lance_private
*)dev
->priv
;
503 volatile struct lance_init_block
*ib
= lp
->init_block
;
504 int entry
, skblen
, len
;
512 netif_stop_queue (dev
);
517 /* dump the packet */
521 for (i
= 0; i
< 64; i
++) {
524 printk ("%2.2x ", skb
->data
[i
]);
528 len
= (skblen
<= ETH_ZLEN
) ? ETH_ZLEN
: skblen
;
529 entry
= lp
->tx_new
& lp
->tx_ring_mod_mask
;
530 ib
->btx_ring
[entry
].length
= (-len
) | 0xf000;
531 ib
->btx_ring
[entry
].misc
= 0;
533 if (skb
->len
< ETH_ZLEN
)
534 memset((char *)&ib
->tx_buf
[entry
][0], 0, ETH_ZLEN
);
535 memcpy ((char *)&ib
->tx_buf
[entry
][0], skb
->data
, skblen
);
537 /* Now, give the packet to the lance */
538 ib
->btx_ring
[entry
].tmd1_bits
= (LE_T1_POK
|LE_T1_OWN
);
539 lp
->tx_new
= (lp
->tx_new
+1) & lp
->tx_ring_mod_mask
;
542 /* Kick the lance: transmit now */
543 WRITERDP(LE_C0_INEA
| LE_C0_TDMD
);
544 dev
->trans_start
= jiffies
;
547 spin_lock_irqsave (&lp
->devlock
, flags
);
549 netif_start_queue (dev
);
552 spin_unlock_irqrestore (&lp
->devlock
, flags
);
557 struct net_device_stats
*lance_get_stats (struct net_device
*dev
)
559 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
564 /* taken from the depca driver via a2065.c */
565 static void lance_load_multicast (struct net_device
*dev
)
567 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
568 volatile struct lance_init_block
*ib
= lp
->init_block
;
569 volatile u16
*mcast_table
= (u16
*)&ib
->filter
;
570 struct dev_mc_list
*dmi
=dev
->mc_list
;
575 /* set all multicast bits */
576 if (dev
->flags
& IFF_ALLMULTI
){
577 ib
->filter
[0] = 0xffffffff;
578 ib
->filter
[1] = 0xffffffff;
581 /* clear the multicast filter */
586 for (i
= 0; i
< dev
->mc_count
; i
++){
587 addrs
= dmi
->dmi_addr
;
590 /* multicast address? */
594 crc
= ether_crc_le(6, addrs
);
596 mcast_table
[crc
>> 4] |= 1 << (crc
& 0xf);
602 void lance_set_multicast (struct net_device
*dev
)
604 struct lance_private
*lp
= (struct lance_private
*) dev
->priv
;
605 volatile struct lance_init_block
*ib
= lp
->init_block
;
609 stopped
= netif_queue_stopped(dev
);
611 netif_stop_queue (dev
);
613 while (lp
->tx_old
!= lp
->tx_new
)
617 WRITERDP(LE_C0_STOP
);
618 lance_init_ring (dev
);
620 if (dev
->flags
& IFF_PROMISC
) {
621 ib
->mode
|= LE_MO_PROM
;
623 ib
->mode
&= ~LE_MO_PROM
;
624 lance_load_multicast (dev
);
627 init_restart_lance (lp
);
630 netif_start_queue (dev
);
633 MODULE_LICENSE("GPL");