2 * linux/drivers/net/am79c961.c
4 * Derived from various things including skeleton.c
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/types.h>
12 #include <linux/fcntl.h>
13 #include <linux/interrupt.h>
14 #include <linux/ptrace.h>
15 #include <linux/ioport.h>
17 #include <linux/malloc.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/skbuff.h>
24 #include <asm/system.h>
25 #include <asm/bitops.h>
28 #include <asm/ecard.h>
29 #include <asm/delay.h>
34 #include "am79c961a.h"
36 static unsigned int net_debug
= NET_DEBUG
;
39 am79c961_setmulticastlist (struct net_device
*dev
);
41 static char *version
= "am79c961 ethernet driver (c) 1995 R.M.King v0.00\n";
43 #define FUNC_PROLOGUE \
44 struct dev_priv *priv = (struct dev_priv *)dev->priv
46 /* --------------------------------------------------------------------------- */
49 write_rreg (unsigned long base
, unsigned int reg
, unsigned short val
)
52 strh %1, [%2] @ NET_RAP
53 strh %0, [%2, #-4] @ NET_RDP
54 " : : "r" (val
), "r" (reg
), "r" (0xf0000464));
58 write_ireg (unsigned long base
, unsigned int reg
, unsigned short val
)
61 strh %1, [%2] @ NET_RAP
62 strh %0, [%2, #8] @ NET_RDP
63 " : : "r" (val
), "r" (reg
), "r" (0xf0000464));
66 #define am_writeword(dev,off,val)\
69 " : : "r" ((val) & 0xffff), "r" (0xe0000000 + ((off) << 1)));
72 am_writebuffer(struct net_device
*dev
, unsigned int offset
, unsigned char *buf
, unsigned int length
)
74 offset
= 0xe0000000 + (offset
<< 1);
75 length
= (length
+ 1) & ~1;
77 __asm__
__volatile__("
79 " : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
84 unsigned int tmp
, tmp2
;
85 __asm__
__volatile__("
93 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
)
94 : "0" (offset
), "1" (buf
));
98 __asm__
__volatile__("
100 " : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
106 static inline unsigned short
107 read_rreg (unsigned int base_addr
, unsigned int reg
)
111 strh %1, [%2] @ NET_RAP
112 ldrh %0, [%2, #-4] @ NET_IDP
113 " : "=r" (v
): "r" (reg
), "r" (0xf0000464));
117 static inline unsigned short
118 am_readword (struct net_device
*dev
, unsigned long off
)
120 unsigned long address
= 0xe0000000 + (off
<< 1);
125 " : "=r" (val
): "r" (address
));
130 am_readbuffer(struct net_device
*dev
, unsigned int offset
, unsigned char *buf
, unsigned int length
)
132 offset
= 0xe0000000 + (offset
<< 1);
133 length
= (length
+ 1) & ~1;
136 __asm__
__volatile__("
141 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
): "0" (offset
), "1" (buf
));
145 unsigned int tmp
, tmp2
, tmp3
;
146 __asm__
__volatile__("
149 orr %2, %2, %3, lsl #16
152 orr %3, %3, %4, lsl #16
154 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
), "=r" (tmp3
)
155 : "0" (offset
), "1" (buf
));
160 __asm__
__volatile__("
165 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
) : "0" (offset
), "1" (buf
));
171 am79c961_ramtest(struct net_device
*dev
, unsigned int val
)
173 unsigned char *buffer
= kmalloc (65536, GFP_KERNEL
);
174 int i
, error
= 0, errorcount
= 0;
178 memset (buffer
, val
, 65536);
179 am_writebuffer(dev
, 0, buffer
, 65536);
180 memset (buffer
, val
^ 255, 65536);
181 am_readbuffer(dev
, 0, buffer
, 65536);
182 for (i
= 0; i
< 65536; i
++) {
183 if (buffer
[i
] != val
&& !error
) {
184 printk ("%s: buffer error (%02X %02X) %05X - ", dev
->name
, val
, buffer
[i
], i
);
187 } else if (error
&& buffer
[i
] == val
) {
188 printk ("%05X\n", i
);
199 am79c961_init_for_open(struct net_device
*dev
)
201 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
202 unsigned long hdr_addr
, first_free_addr
;
207 save_flags_cli (flags
);
209 write_ireg (dev
->base_addr
, 2, 0x4000); /* autoselect media */
210 write_rreg (dev
->base_addr
, CSR0
, CSR0_BABL
|CSR0_CERR
|CSR0_MISS
|CSR0_MERR
|CSR0_TINT
|CSR0_RINT
|CSR0_STOP
);
212 restore_flags (flags
);
214 first_free_addr
= RX_BUFFERS
* 8 + TX_BUFFERS
* 8 + 16;
219 priv
->rxhdr
= hdr_addr
;
221 for (i
= 0; i
< RX_BUFFERS
; i
++) {
222 priv
->rxbuffer
[i
] = first_free_addr
;
223 am_writeword (dev
, hdr_addr
, first_free_addr
);
224 am_writeword (dev
, hdr_addr
+ 2, RMD_OWN
);
225 am_writeword (dev
, hdr_addr
+ 4, (-1600));
226 am_writeword (dev
, hdr_addr
+ 6, 0);
227 first_free_addr
+= 1600;
232 priv
->txhdr
= hdr_addr
;
233 for (i
= 0; i
< TX_BUFFERS
; i
++) {
234 priv
->txbuffer
[i
] = first_free_addr
;
235 am_writeword (dev
, hdr_addr
, first_free_addr
);
236 am_writeword (dev
, hdr_addr
+ 2, 0);
237 am_writeword (dev
, hdr_addr
+ 4, 0);
238 am_writeword (dev
, hdr_addr
+ 6, 0);
239 first_free_addr
+= 1600;
243 for (i
= LADRL
; i
<= LADRH
; i
++)
244 write_rreg (dev
->base_addr
, i
, 0);
246 for (i
= PADRL
, p
= dev
->dev_addr
; i
<= PADRH
; i
++, p
+= 2)
247 write_rreg (dev
->base_addr
, i
, p
[0] | (p
[1] << 8));
250 if (dev
->flags
& IFF_PROMISC
)
253 write_rreg (dev
->base_addr
, MODE
, i
);
254 write_rreg (dev
->base_addr
, BASERXL
, priv
->rxhdr
);
255 write_rreg (dev
->base_addr
, BASERXH
, 0);
256 write_rreg (dev
->base_addr
, BASETXL
, priv
->txhdr
);
257 write_rreg (dev
->base_addr
, BASERXH
, 0);
258 write_rreg (dev
->base_addr
, POLLINT
, 0);
259 write_rreg (dev
->base_addr
, SIZERXR
, -RX_BUFFERS
);
260 write_rreg (dev
->base_addr
, SIZETXR
, -TX_BUFFERS
);
261 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
262 write_rreg (dev
->base_addr
, CSR3
, CSR3_IDONM
|CSR3_BABLM
);
263 write_rreg (dev
->base_addr
, CSR0
, CSR0_IENA
|CSR0_STRT
);
267 am79c961_init(struct net_device
*dev
)
271 am79c961_ramtest(dev
, 0x66);
272 am79c961_ramtest(dev
, 0x99);
274 save_flags_cli (flags
);
276 write_ireg (dev
->base_addr
, 2, 0x4000); /* autoselect media */
277 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
278 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
280 restore_flags (flags
);
286 * This is the real probe routine.
289 am79c961_probe1(struct net_device
*dev
)
291 static unsigned version_printed
= 0;
292 struct dev_priv
*priv
;
296 dev
->priv
= kmalloc (sizeof (struct dev_priv
), GFP_KERNEL
);
301 priv
= (struct dev_priv
*) dev
->priv
;
302 memset (priv
, 0, sizeof(struct dev_priv
));
305 * The PNP initialisation should have been done by the ether bootp loader.
307 inb ((dev
->base_addr
+ NET_RESET
) >> 1); /* reset the device */
311 if (inb (dev
->base_addr
>> 1) != 0x08 ||
312 inb ((dev
->base_addr
>> 1) + 1) != 00 ||
313 inb ((dev
->base_addr
>> 1) + 2) != 0x2b) {
320 * Ok, we've found a valid hw ID
323 if (net_debug
&& version_printed
++ == 0)
324 printk (KERN_INFO
"%s", version
);
326 printk(KERN_INFO
"%s: am79c961 found [%04lx, %d] ", dev
->name
, dev
->base_addr
, dev
->irq
);
327 request_region (dev
->base_addr
, 0x18, "am79c961");
329 /* Retrive and print the ethernet address. */
330 for (i
= 0; i
< 6; i
++) {
331 dev
->dev_addr
[i
] = inb ((dev
->base_addr
>> 1) + i
) & 0xff;
332 printk (i
== 5 ? "%02x\n" : "%02x:", dev
->dev_addr
[i
]);
335 if (am79c961_init(dev
)) {
341 dev
->open
= am79c961_open
;
342 dev
->stop
= am79c961_close
;
343 dev
->hard_start_xmit
= am79c961_sendpacket
;
344 dev
->get_stats
= am79c961_getstats
;
345 dev
->set_multicast_list
= am79c961_setmulticastlist
;
347 /* Fill in the fields of the device structure with ethernet values. */
354 am79c961_probe(struct net_device
*dev
)
356 static int initialised
= 0;
362 dev
->base_addr
= 0x220;
365 return am79c961_probe1(dev
);
369 * Open/initialize the board. This is called (in the current kernel)
370 * sometime after booting when the 'ifconfig' program is run.
372 * This routine should set everything up anew at each open, even
373 * registers that "should" only need to be set once at boot, so that
374 * there is non-reboot way to recover if something goes wrong.
377 am79c961_open(struct net_device
*dev
)
379 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
383 memset (&priv
->stats
, 0, sizeof (priv
->stats
));
385 if (request_irq(dev
->irq
, am79c961_interrupt
, 0, "am79c961", dev
))
388 am79c961_init_for_open(dev
);
397 * The inverse routine to am79c961_open().
400 am79c961_close(struct net_device
*dev
)
407 free_irq (dev
->irq
, dev
);
414 * Get the current statistics. This may be called with the card open or
417 static struct enet_statistics
*am79c961_getstats (struct net_device
*dev
)
419 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
424 * Set or clear promiscuous/multicast mode filter for this adaptor.
426 * We don't attempt any packet filtering. The card may have a SEEQ 8004
427 * in which does not have the other ethernet address registers present...
429 static void am79c961_setmulticastlist (struct net_device
*dev
)
434 dev
->flags
&= ~IFF_ALLMULTI
;
437 if (dev
->flags
& IFF_PROMISC
)
440 save_flags_cli (flags
);
441 write_rreg (dev
->base_addr
, MODE
, i
);
442 restore_flags (flags
);
449 am79c961_sendpacket(struct sk_buff
*skb
, struct net_device
*dev
)
451 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
455 if (!test_and_set_bit(0, (void*)&dev
->tbusy
)) {
456 unsigned int length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
457 unsigned int hdraddr
, bufaddr
;
460 hdraddr
= priv
->txhdr
+ (priv
->txhead
<< 3);
461 bufaddr
= priv
->txbuffer
[priv
->txhead
];
463 if (priv
->txhead
>= TX_BUFFERS
)
466 am_writebuffer (dev
, bufaddr
, skb
->data
, length
);
467 am_writeword (dev
, hdraddr
+ 4, -length
);
468 am_writeword (dev
, hdraddr
+ 2, TMD_OWN
|TMD_STP
|TMD_ENP
);
470 save_flags_cli (flags
);
471 write_rreg (dev
->base_addr
, CSR0
, CSR0_TDMD
|CSR0_IENA
);
472 dev
->trans_start
= jiffies
;
473 restore_flags (flags
);
475 if (!(am_readword (dev
, priv
->txhdr
+ (priv
->txhead
<< 3) + 2) & TMD_OWN
))
480 printk(KERN_ERR
"%s: Transmitter access conflict.\n", dev
->name
);
483 int tickssofar
= jiffies
- dev
->trans_start
;
486 printk (KERN_WARNING
"%s: transmit timed out, network cable problem?\n", dev
->name
);
487 /* Try to restart the adaptor. */
489 dev
->trans_start
= jiffies
;
495 am79c961_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
497 struct net_device
*dev
= (struct net_device
*)dev_id
;
498 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
502 if(net_debug
& DEBUG_INT
)
503 printk(KERN_DEBUG
"am79c961irq: %d ", irq
);
508 status
= read_rreg (dev
->base_addr
, CSR0
);
509 write_rreg (dev
->base_addr
, CSR0
, status
& (CSR0_TINT
|CSR0_RINT
|CSR0_MISS
|CSR0_IENA
));
511 if (status
& CSR0_RINT
) /* Got a packet(s). */
512 am79c961_rx (dev
, priv
);
513 if (status
& CSR0_TINT
) /* Packets transmitted */
514 am79c961_tx (dev
, priv
);
515 if (status
& CSR0_MISS
)
516 priv
->stats
.rx_dropped
++;
521 if(net_debug
& DEBUG_INT
)
527 * If we have a good packet(s), get it/them out of the buffers.
530 am79c961_rx(struct net_device
*dev
, struct dev_priv
*priv
)
532 unsigned long hdraddr
;
533 unsigned long pktaddr
;
536 unsigned long status
;
540 hdraddr
= priv
->rxhdr
+ (priv
->rxtail
<< 3);
541 pktaddr
= priv
->rxbuffer
[priv
->rxtail
];
543 status
= am_readword (dev
, hdraddr
+ 2);
544 if (status
& RMD_OWN
) /* do we own it? */
548 if (priv
->rxtail
>= RX_BUFFERS
)
551 if ((status
& (RMD_ERR
|RMD_STP
|RMD_ENP
)) != (RMD_STP
|RMD_ENP
)) {
552 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
553 priv
->stats
.rx_errors
++;
554 if (status
& RMD_ERR
) {
555 if (status
& RMD_FRAM
)
556 priv
->stats
.rx_frame_errors
++;
557 if (status
& RMD_CRC
)
558 priv
->stats
.rx_crc_errors
++;
559 } else if (status
& RMD_STP
)
560 priv
->stats
.rx_length_errors
++;
564 len
= am_readword (dev
, hdraddr
+ 6);
565 skb
= dev_alloc_skb (len
+ 2);
571 skb_reserve (skb
, 2);
572 buf
= skb_put (skb
, len
);
574 am_readbuffer (dev
, pktaddr
, buf
, len
);
575 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
576 skb
->protocol
= eth_type_trans(skb
, dev
);
578 priv
->stats
.rx_packets
++;
580 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
581 printk (KERN_WARNING
"%s: memory squeeze, dropping packet.\n", dev
->name
);
582 priv
->stats
.rx_dropped
++;
589 * Update stats for the transmitted packet
592 am79c961_tx(struct net_device
*dev
, struct dev_priv
*priv
)
595 unsigned long hdraddr
;
596 unsigned long status
;
598 hdraddr
= priv
->txhdr
+ (priv
->txtail
<< 3);
599 status
= am_readword (dev
, hdraddr
+ 2);
600 if (status
& TMD_OWN
)
604 if (priv
->txtail
>= TX_BUFFERS
)
607 if (status
& TMD_ERR
) {
608 unsigned long status2
;
610 priv
->stats
.tx_errors
++;
612 status2
= am_readword (dev
, hdraddr
+ 6);
613 am_writeword (dev
, hdraddr
+ 6, 0);
615 if (status2
& TST_RTRY
)
616 priv
->stats
.collisions
+= 1;
617 if (status2
& TST_LCOL
)
618 priv
->stats
.tx_window_errors
++;
619 if (status2
& TST_LCAR
)
620 priv
->stats
.tx_carrier_errors
++;
621 if (status2
& TST_UFLO
)
622 priv
->stats
.tx_fifo_errors
++;
625 priv
->stats
.tx_packets
++;
626 } while (priv
->txtail
!= priv
->txhead
);