* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / 7990.c
blob118f92af341b4230de6bee318e821fa516557a4c
1 /*
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>
23 #include <linux/in.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>
30 #include <asm/io.h>
31 #include <asm/dma.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>
45 #include "7990.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 */
59 #ifdef UNDEF
60 #define PRINT_RINGS() \
61 do { \
62 int t; \
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);\
75 } while (0)
76 #else
77 #define PRINT_RINGS()
78 #endif
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;
84 int leptr;
85 DECLARE_LL;
87 leptr = LANCE_ADDR (aib);
89 WRITERAP(LE_CSR1); /* load address of init block */
90 WRITERDP(leptr & 0xFFFF);
91 WRITERAP(LE_CSR2);
92 WRITERDP(leptr >> 16);
93 WRITERAP(LE_CSR3);
94 WRITERDP(lp->busmaster_regval); /* set byteswap/ALEctrl/byte ctrl */
96 /* Point back to csr0 */
97 WRITERAP(LE_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 */
109 int leptr;
110 int i;
112 aib = lp->lance_init_block;
114 /* Lock out other processes while setting up hardware */
115 dev->tbusy = 1;
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
127 * everybody? Um.]
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.
131 #ifdef __BIG_ENDIAN
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];
138 #else
139 for (i=0; i<6; i++)
140 ib->phys_addr[i] = dev->dev_addr[i];
141 #endif
143 if (DEBUG_IRING)
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;
154 if (DEBUG_IRING)
155 printk ("%d: 0x%8.8x\n", i, leptr);
158 /* Setup the Rx ring entries */
159 if (DEBUG_IRING)
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;
170 if (DEBUG_IRING)
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);
179 ib->rx_ptr = leptr;
180 if (DEBUG_IRING)
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);
186 ib->tx_ptr = leptr;
187 if (DEBUG_IRING)
188 printk ("TX ptr: %8.8x\n", leptr);
190 /* Clear the multicast filter */
191 ib->filter [0] = 0;
192 ib->filter [1] = 0;
193 PRINT_RINGS();
196 /* LANCE must be STOPped before we do this, too... */
197 static int init_restart_lance (struct lance_private *lp)
199 int i;
200 DECLARE_LL;
202 WRITERAP(LE_CSR0);
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++)
209 barrier();
210 if ((i == 100) || (READRDP() & LE_C0_ERR)) {
211 printk ("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, READRDP());
212 return -1;
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);
219 return 0;
222 static int lance_reset (struct net_device *dev)
224 struct lance_private *lp = (struct lance_private *)dev->priv;
225 int status;
226 DECLARE_LL;
228 /* Stop the lance */
229 WRITERAP(LE_CSR0);
230 WRITERDP(LE_C0_STOP);
232 load_csrs (lp);
233 lance_init_ring (dev);
234 dev->trans_start = jiffies;
235 dev->interrupt = 0;
236 dev->start = 1;
237 dev->tbusy = 0;
238 status = init_restart_lance (lp);
239 #ifdef DEBUG_DRIVER
240 printk ("Lance restart=%d\n", status);
241 #endif
242 return 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;
250 unsigned char bits;
251 int len = 0; /* XXX shut up gcc warnings */
252 struct sk_buff *skb = 0; /* XXX shut up gcc warnings */
253 #ifdef TEST_HITS
254 int i;
255 #endif
256 DECLARE_LL;
258 #ifdef TEST_HITS
259 printk ("[");
260 for (i = 0; i < RX_RING_SIZE; i++) {
261 if (i == lp->rx_new)
262 printk ("%s",
263 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X");
264 else
265 printk ("%s",
266 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1");
268 printk ("]");
269 #endif
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++;
280 continue;
281 } else if (bits & LE_R1_ERR) {
282 /* Count only the end frame as a rx error,
283 * not the beginning
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++;
290 } else {
291 len = (rd->mblength & 0xfff) - 4;
292 skb = dev_alloc_skb (len+2);
294 if (skb == 0) {
295 printk ("%s: Memory squeeze, deferring packet.\n",
296 dev->name);
297 lp->stats.rx_dropped++;
298 rd->mblength = 0;
299 rd->rmd1_bits = LE_R1_OWN;
300 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
301 return 0;
304 skb->dev = dev;
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]),
309 len, 0);
310 skb->protocol = eth_type_trans (skb, dev);
311 netif_rx (skb);
312 lp->stats.rx_packets++;
315 /* Return the packet to the pool */
316 rd->mblength = 0;
317 rd->rmd1_bits = LE_R1_OWN;
318 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
320 return 0;
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;
328 int i, j;
329 int status;
330 DECLARE_LL;
332 /* csr0 is 2f3 */
333 WRITERDP(LE_C0_TINT | LE_C0_INEA);
334 /* csr0 is 73 */
336 j = lp->tx_old;
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)
342 break;
344 if (td->tmd1_bits & LE_T1_ERR) {
345 status = td->misc;
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");
357 /* Stop the lance */
358 WRITERAP(LE_CSR0);
359 WRITERDP(LE_C0_STOP);
360 lance_init_ring (dev);
361 load_csrs (lp);
362 init_restart_lance (lp);
363 return 0;
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",
373 dev->name);
374 /* Stop the lance */
375 WRITERAP(LE_CSR0);
376 WRITERDP(LE_C0_STOP);
377 lance_init_ring (dev);
378 load_csrs (lp);
379 init_restart_lance (lp);
380 return 0;
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;
401 lp->tx_old = j;
402 WRITERDP(LE_C0_TINT | LE_C0_INEA);
403 return 0;
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;
410 int csr0;
411 DECLARE_LL;
413 WRITERAP(LE_CSR0); /* LANCE Controller Status */
414 csr0 = READRDP();
416 PRINT_RINGS();
418 if (!(csr0 & LE_C0_INTR)) /* Check if any interrupt has */
419 return; /* been generated by the Lance. */
421 if (dev->interrupt)
422 printk ("%s: again", dev->name);
424 dev->interrupt = 1;
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)
435 lance_rx (dev);
437 if (csr0 & LE_C0_TINT)
438 lance_tx (dev);
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",
447 dev->name, csr0);
448 /* Restart the chip. */
449 WRITERDP(LE_C0_STRT);
452 if ((TX_BUFFS_AVAIL >= 0) && dev->tbusy) {
453 dev->tbusy = 0;
454 mark_bh (NET_BH);
457 WRITERAP(LE_CSR0);
458 WRITERDP(LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR|LE_C0_IDON|LE_C0_INEA);
460 dev->interrupt = 0;
463 int lance_open (struct net_device *dev)
465 struct lance_private *lp = (struct lance_private *)dev->priv;
466 DECLARE_LL;
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))
470 return -EAGAIN;
472 return lance_reset(dev);
475 int lance_close (struct net_device *dev)
477 struct lance_private *lp = (struct lance_private *) dev->priv;
478 DECLARE_LL;
480 dev->start = 0;
481 dev->tbusy = 1;
483 /* Stop the LANCE */
484 WRITERAP(LE_CSR0);
485 WRITERDP(LE_C0_STOP);
487 free_irq(lp->irq, dev);
489 return 0;
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;
497 int status = 0;
498 static int outs;
499 DECLARE_LL;
501 lance_reset(dev);
503 /* Transmitter timeout, serious problems */
504 if (dev->tbusy) {
505 int tickssofar = jiffies - dev->trans_start;
507 if (tickssofar < 100) {
508 status = -1;
509 } else {
510 printk ("%s: transmit timed out, status %04x, resetting\n",
511 dev->name, READRDP());
512 lance_reset (dev);
514 return status;
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");
520 return -1;
523 skblen = skb->len;
525 if (!TX_BUFFS_AVAIL)
526 return -1;
528 #ifdef DEBUG_DRIVER
529 /* dump the packet */
531 int i;
533 for (i = 0; i < 64; i++) {
534 if ((i % 16) == 0)
535 printk ("\n");
536 printk ("%2.2x ", skb->data [i]);
539 #endif
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;
551 outs++;
552 /* Kick the lance: transmit now */
553 WRITERDP(LE_C0_INEA | LE_C0_TDMD);
554 dev->trans_start = jiffies;
555 dev_kfree_skb (skb);
557 if (TX_BUFFS_AVAIL)
558 dev->tbusy = 0;
560 return status;
563 struct net_device_stats *lance_get_stats (struct net_device *dev)
565 struct lance_private *lp = (struct lance_private *) dev->priv;
567 return &lp->stats;
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;
577 char *addrs;
578 int i, j, bit, byte;
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;
585 return;
587 /* clear the multicast filter */
588 ib->filter [0] = 0;
589 ib->filter [1] = 0;
591 /* Add addresses */
592 for (i = 0; i < dev->mc_count; i++){
593 addrs = dmi->dmi_addr;
594 dmi = dmi->next;
596 /* multicast address? */
597 if (!(*addrs & 1))
598 continue;
600 crc = 0xffffffff;
601 for (byte = 0; byte < 6; byte++)
602 for (bit = *addrs++, j = 0; j < 8; j++, bit>>=1)
604 int test;
606 test = ((bit ^ crc) & 0x01);
607 crc >>= 1;
609 if (test)
611 crc = crc ^ poly;
615 crc = crc >> 26;
616 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
618 return;
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;
626 DECLARE_LL;
628 while (dev->tbusy)
629 schedule();
630 set_bit (0, (void *) &dev->tbusy);
632 while (lp->tx_old != lp->tx_new)
633 schedule();
635 WRITERAP(LE_CSR0);
636 WRITERDP(LE_C0_STOP);
637 lance_init_ring (dev);
639 if (dev->flags & IFF_PROMISC) {
640 ib->mode |= LE_MO_PROM;
641 } else {
642 ib->mode &= ~LE_MO_PROM;
643 lance_load_multicast (dev);
645 load_csrs (lp);
646 init_restart_lance (lp);
647 dev->tbusy = 0;