2 * linux/drivers/net/am79c961.c
4 * Derived from various things including skeleton.c
6 * Russell King 1995-2000.
8 * This is a special driver for the am79c961A Lance chip used in the
9 * Intel (formally Digital Equipment Corp) EBSA110 platform.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/interrupt.h>
17 #include <linux/ptrace.h>
18 #include <linux/ioport.h>
20 #include <linux/malloc.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
29 #include <asm/system.h>
30 #include <asm/bitops.h>
34 #include <asm/ecard.h>
39 #include "am79c961a.h"
41 static void am79c961_interrupt (int irq
, void *dev_id
, struct pt_regs
*regs
);
43 static unsigned int net_debug
= NET_DEBUG
;
45 static char *version
= "am79c961 ethernet driver (c) 1995 R.M.King v0.02\n";
47 /* --------------------------------------------------------------------------- */
51 write_rreg (unsigned long base
, unsigned int reg
, unsigned short val
)
53 __asm__("str%?h %1, [%2] @ NET_RAP
54 str%?h %0, [%2, #-4] @ NET_RDP
55 " : : "r" (val
), "r" (reg
), "r" (0xf0000464));
58 static inline unsigned short
59 read_rreg (unsigned int base_addr
, unsigned int reg
)
62 __asm__("str%?h %1, [%2] @ NET_RAP
63 ldr%?h %0, [%2, #-4] @ NET_RDP
64 " : "=r" (v
): "r" (reg
), "r" (0xf0000464));
69 write_ireg (unsigned long base
, unsigned int reg
, unsigned short val
)
71 __asm__("str%?h %1, [%2] @ NET_RAP
72 str%?h %0, [%2, #8] @ NET_IDP
73 " : : "r" (val
), "r" (reg
), "r" (0xf0000464));
76 #define am_writeword(dev,off,val)\
77 __asm__("str%?h %0, [%1]" : : \
78 "r" ((val) & 0xffff), "r" (0xe0000000 + ((off) << 1)));
81 am_writebuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
83 offset
= 0xe0000000 + (offset
<< 1);
84 length
= (length
+ 1) & ~1;
86 __asm__
__volatile__("str%?h %2, [%0], #4"
87 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
92 unsigned int tmp
, tmp2
;
93 __asm__
__volatile__("
101 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
)
102 : "0" (offset
), "1" (buf
));
106 __asm__
__volatile__("str%?h %2, [%0], #4"
107 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
114 * This reads a 16-bit quantity in little-endian
115 * mode from the am79c961 buffer.
117 static inline unsigned short am_readword(struct net_device
*dev
, u_int off
)
119 unsigned long address
= 0xe0000000 + (off
<< 1);
122 __asm__("ldr%?h %0, [%1]" : "=r" (val
): "r" (address
));
127 am_readbuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
129 offset
= 0xe0000000 + (offset
<< 1);
130 length
= (length
+ 1) & ~1;
133 __asm__
__volatile__("
138 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
): "0" (offset
), "1" (buf
));
142 unsigned int tmp
, tmp2
, tmp3
;
143 __asm__
__volatile__("
146 orr%? %2, %2, %3, lsl #16
149 orr%? %3, %3, %4, lsl #16
150 stm%?ia %1!, {%2, %3}
151 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
), "=r" (tmp3
)
152 : "0" (offset
), "1" (buf
));
157 __asm__
__volatile__("
162 " : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
) : "0" (offset
), "1" (buf
));
167 #error Not compatable
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
;
204 u_int hdr_addr
, first_free_addr
;
207 save_flags_cli (flags
);
208 write_rreg (dev
->base_addr
, CSR0
, CSR0_BABL
|CSR0_CERR
|CSR0_MISS
|CSR0_MERR
|CSR0_TINT
|CSR0_RINT
|CSR0_STOP
);
209 restore_flags (flags
);
211 write_ireg (dev
->base_addr
, 5, 0x00a0); /* Receive address LED */
212 write_ireg (dev
->base_addr
, 6, 0x0081); /* Collision LED */
213 write_ireg (dev
->base_addr
, 7, 0x0090); /* XMIT LED */
214 write_ireg (dev
->base_addr
, 2, 0x0000); /* MODE register selects media */
216 for (i
= LADRL
; i
<= LADRH
; i
++)
217 write_rreg (dev
->base_addr
, i
, 0);
219 for (i
= PADRL
, p
= dev
->dev_addr
; i
<= PADRH
; i
++, p
+= 2)
220 write_rreg (dev
->base_addr
, i
, p
[0] | (p
[1] << 8));
223 if (dev
->flags
& IFF_PROMISC
)
226 write_rreg (dev
->base_addr
, MODE
, i
);
227 write_rreg (dev
->base_addr
, POLLINT
, 0);
228 write_rreg (dev
->base_addr
, SIZERXR
, -RX_BUFFERS
);
229 write_rreg (dev
->base_addr
, SIZETXR
, -TX_BUFFERS
);
231 first_free_addr
= RX_BUFFERS
* 8 + TX_BUFFERS
* 8 + 16;
236 priv
->rxhdr
= hdr_addr
;
238 for (i
= 0; i
< RX_BUFFERS
; i
++) {
239 priv
->rxbuffer
[i
] = first_free_addr
;
240 am_writeword (dev
, hdr_addr
, first_free_addr
);
241 am_writeword (dev
, hdr_addr
+ 2, RMD_OWN
);
242 am_writeword (dev
, hdr_addr
+ 4, (-1600));
243 am_writeword (dev
, hdr_addr
+ 6, 0);
244 first_free_addr
+= 1600;
249 priv
->txhdr
= hdr_addr
;
250 for (i
= 0; i
< TX_BUFFERS
; i
++) {
251 priv
->txbuffer
[i
] = first_free_addr
;
252 am_writeword (dev
, hdr_addr
, first_free_addr
);
253 am_writeword (dev
, hdr_addr
+ 2, TMD_STP
|TMD_ENP
);
254 am_writeword (dev
, hdr_addr
+ 4, 0xf000);
255 am_writeword (dev
, hdr_addr
+ 6, 0);
256 first_free_addr
+= 1600;
260 write_rreg (dev
->base_addr
, BASERXL
, priv
->rxhdr
);
261 write_rreg (dev
->base_addr
, BASERXH
, 0);
262 write_rreg (dev
->base_addr
, BASETXL
, priv
->txhdr
);
263 write_rreg (dev
->base_addr
, BASERXH
, 0);
264 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
265 write_rreg (dev
->base_addr
, CSR3
, CSR3_IDONM
|CSR3_BABLM
|CSR3_DXSUFLO
);
266 write_rreg (dev
->base_addr
, CSR0
, CSR0_IENA
|CSR0_STRT
);
270 * Open/initialize the board. This is called (in the current kernel)
271 * sometime after booting when the 'ifconfig' program is run.
273 * This routine should set everything up anew at each open, even
274 * registers that "should" only need to be set once at boot, so that
275 * there is non-reboot way to recover if something goes wrong.
278 am79c961_open(struct net_device
*dev
)
280 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
284 memset (&priv
->stats
, 0, sizeof (priv
->stats
));
286 if (request_irq(dev
->irq
, am79c961_interrupt
, 0, "am79c961", dev
)) {
291 am79c961_init_for_open(dev
);
293 netif_start_queue(dev
);
299 * The inverse routine to am79c961_open().
302 am79c961_close(struct net_device
*dev
)
306 netif_stop_queue(dev
);
308 save_flags_cli (flags
);
310 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
311 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
313 restore_flags (flags
);
315 free_irq (dev
->irq
, dev
);
322 * Get the current statistics. This may be called with the card open or
325 static struct net_device_stats
*am79c961_getstats (struct net_device
*dev
)
327 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
331 static inline u32
update_crc(u32 crc
, u8 byte
)
335 for (i
= 8; i
!= 0; i
--) {
348 static void am79c961_mc_hash(struct dev_mc_list
*dmi
, unsigned short *hash
)
350 if (dmi
->dmi_addrlen
== ETH_ALEN
&& dmi
->dmi_addr
[0] & 0x01) {
356 for (i
= 0; i
< ETH_ALEN
; i
++)
357 crc
= update_crc(crc
, dmi
->dmi_addr
[i
]);
360 bit
= (crc
>> 26) & 15;
362 hash
[idx
] |= 1 << bit
;
367 * Set or clear promiscuous/multicast mode filter for this adaptor.
369 static void am79c961_setmulticastlist (struct net_device
*dev
)
372 unsigned short multi_hash
[4], mode
;
375 mode
= MODE_PORT_10BT
;
377 if (dev
->flags
& IFF_PROMISC
) {
378 mode
|= MODE_PROMISC
;
379 } else if (dev
->flags
& IFF_ALLMULTI
) {
380 memset(multi_hash
, 0xff, sizeof(multi_hash
));
382 struct dev_mc_list
*dmi
;
384 memset(multi_hash
, 0x00, sizeof(multi_hash
));
386 for (dmi
= dev
->mc_list
; dmi
; dmi
= dmi
->next
)
387 am79c961_mc_hash(dmi
, multi_hash
);
390 save_flags_cli(flags
);
392 stopped
= read_rreg(dev
->base_addr
, CSR0
) & CSR0_STOP
;
396 * Put the chip into suspend mode
398 write_rreg(dev
->base_addr
, CTRL1
, CTRL1_SPND
);
401 * Spin waiting for chip to report suspend mode
403 while ((read_rreg(dev
->base_addr
, CTRL1
) & CTRL1_SPND
) == 0) {
404 restore_flags(flags
);
406 save_flags_cli(flags
);
411 * Update the multicast hash table
413 for (i
= 0; i
< sizeof(multi_hash
) / sizeof(multi_hash
[0]); i
++)
414 write_rreg(dev
->base_addr
, i
+ LADRL
, multi_hash
[i
]);
417 * Write the mode register
419 write_rreg(dev
->base_addr
, MODE
, mode
);
423 * Put the chip back into running mode
425 write_rreg(dev
->base_addr
, CTRL1
, 0);
428 restore_flags(flags
);
431 static void am79c961_timeout(struct net_device
*dev
)
433 printk(KERN_WARNING
"%s: transmit timed out, network cable problem?\n",
437 * ought to do some setup of the tx side here
440 netif_wake_queue(dev
);
447 am79c961_sendpacket(struct sk_buff
*skb
, struct net_device
*dev
)
449 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
450 unsigned int length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
451 unsigned int hdraddr
, bufaddr
;
456 hdraddr
= priv
->txhdr
+ (head
<< 3);
457 bufaddr
= priv
->txbuffer
[head
];
459 if (head
>= TX_BUFFERS
)
462 am_writebuffer (dev
, bufaddr
, skb
->data
, length
);
463 am_writeword (dev
, hdraddr
+ 4, -length
);
464 am_writeword (dev
, hdraddr
+ 2, TMD_OWN
|TMD_STP
|TMD_ENP
);
467 save_flags_cli (flags
);
468 write_rreg (dev
->base_addr
, CSR0
, CSR0_TDMD
|CSR0_IENA
);
469 dev
->trans_start
= jiffies
;
470 restore_flags (flags
);
473 * If the next packet is owned by the ethernet device,
474 * then the tx ring is full and we can't add another
477 if (am_readword(dev
, priv
->txhdr
+ (priv
->txhead
<< 3) + 2) & TMD_OWN
) {
478 printk(KERN_DEBUG
"tx ring full, stopping queue\n");
479 netif_stop_queue(dev
);
488 * If we have a good packet(s), get it/them out of the buffers.
491 am79c961_rx(struct net_device
*dev
, struct dev_priv
*priv
)
500 hdraddr
= priv
->rxhdr
+ (priv
->rxtail
<< 3);
501 pktaddr
= priv
->rxbuffer
[priv
->rxtail
];
503 status
= am_readword (dev
, hdraddr
+ 2);
504 if (status
& RMD_OWN
) /* do we own it? */
508 if (priv
->rxtail
>= RX_BUFFERS
)
511 if ((status
& (RMD_ERR
|RMD_STP
|RMD_ENP
)) != (RMD_STP
|RMD_ENP
)) {
512 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
513 priv
->stats
.rx_errors
++;
514 if (status
& RMD_ERR
) {
515 if (status
& RMD_FRAM
)
516 priv
->stats
.rx_frame_errors
++;
517 if (status
& RMD_CRC
)
518 priv
->stats
.rx_crc_errors
++;
519 } else if (status
& RMD_STP
)
520 priv
->stats
.rx_length_errors
++;
524 len
= am_readword(dev
, hdraddr
+ 6);
525 skb
= dev_alloc_skb(len
+ 2);
531 am_readbuffer(dev
, pktaddr
, skb_put(skb
, len
), len
);
532 am_writeword(dev
, hdraddr
+ 2, RMD_OWN
);
533 skb
->protocol
= eth_type_trans(skb
, dev
);
535 dev
->last_rx
= jiffies
;
536 priv
->stats
.rx_bytes
+= len
;
537 priv
->stats
.rx_packets
++;
539 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
540 printk (KERN_WARNING
"%s: memory squeeze, dropping packet.\n", dev
->name
);
541 priv
->stats
.rx_dropped
++;
548 * Update stats for the transmitted packet
551 am79c961_tx(struct net_device
*dev
, struct dev_priv
*priv
)
558 bufnum
= priv
->txtail
;
559 hdraddr
= priv
->txhdr
+ (priv
->txtail
<< 3);
560 status
= am_readword (dev
, hdraddr
+ 2);
561 if (status
& TMD_OWN
)
565 if (priv
->txtail
>= TX_BUFFERS
)
568 if (status
& TMD_ERR
) {
571 priv
->stats
.tx_errors
++;
573 status2
= am_readword (dev
, hdraddr
+ 6);
576 * Clear the error byte
578 am_writeword (dev
, hdraddr
+ 6, 0);
580 if (status2
& TST_RTRY
)
581 priv
->stats
.collisions
+= 16;
582 if (status2
& TST_LCOL
)
583 priv
->stats
.tx_window_errors
++;
584 if (status2
& TST_LCAR
)
585 priv
->stats
.tx_carrier_errors
++;
586 if (status2
& TST_UFLO
)
587 priv
->stats
.tx_fifo_errors
++;
590 priv
->stats
.tx_packets
++;
591 } while (priv
->txtail
!= priv
->txhead
);
593 netif_wake_queue(dev
);
597 am79c961_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
599 struct net_device
*dev
= (struct net_device
*)dev_id
;
600 struct dev_priv
*priv
= (struct dev_priv
*)dev
->priv
;
603 status
= read_rreg(dev
->base_addr
, CSR0
);
604 write_rreg(dev
->base_addr
, CSR0
, status
& (CSR0_TINT
|CSR0_RINT
|CSR0_MISS
|CSR0_IENA
));
606 if (status
& CSR0_RINT
)
607 am79c961_rx(dev
, priv
);
608 if (status
& CSR0_TINT
)
609 am79c961_tx(dev
, priv
);
610 if (status
& CSR0_MISS
)
611 priv
->stats
.rx_dropped
++;
615 am79c961_hw_init(struct net_device
*dev
)
619 am79c961_ramtest(dev
, 0x66);
620 am79c961_ramtest(dev
, 0x99);
622 save_flags_cli (flags
);
624 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
625 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
627 restore_flags (flags
);
632 static void __init
am79c961_banner(void)
634 static unsigned version_printed
= 0;
636 if (net_debug
&& version_printed
++ == 0)
637 printk(KERN_INFO
"%s", version
);
640 static int __init
am79c961_init(void)
642 struct net_device
*dev
;
643 struct dev_priv
*priv
;
646 dev
= init_etherdev(NULL
, sizeof(struct dev_priv
));
651 priv
= (struct dev_priv
*) dev
->priv
;
654 * Fixed address and IRQ lines here.
655 * The PNP initialisation should have been
656 * done by the ether bootp loader.
658 dev
->base_addr
= 0x220;
659 dev
->irq
= IRQ_EBSA110_ETHERNET
;
664 inb((dev
->base_addr
+ NET_RESET
) >> 1);
668 * Check the manufacturer part of the
672 if (inb(dev
->base_addr
>> 1) != 0x08 ||
673 inb((dev
->base_addr
>> 1) + 1) != 00 ||
674 inb((dev
->base_addr
>> 1) + 2) != 0x2b)
677 if (!request_region(dev
->base_addr
, 0x18, dev
->name
))
681 printk(KERN_INFO
"%s: am79c961 found at %08lx, IRQ%d, ether address ",
682 dev
->name
, dev
->base_addr
, dev
->irq
);
684 /* Retrive and print the ethernet address. */
685 for (i
= 0; i
< 6; i
++) {
686 dev
->dev_addr
[i
] = inb((dev
->base_addr
>> 1) + i
) & 0xff;
687 printk (i
== 5 ? "%02x\n" : "%02x:", dev
->dev_addr
[i
]);
690 if (am79c961_hw_init(dev
))
693 dev
->open
= am79c961_open
;
694 dev
->stop
= am79c961_close
;
695 dev
->hard_start_xmit
= am79c961_sendpacket
;
696 dev
->get_stats
= am79c961_getstats
;
697 dev
->set_multicast_list
= am79c961_setmulticastlist
;
698 dev
->tx_timeout
= am79c961_timeout
;
703 release_region(dev
->base_addr
, 0x18);
705 unregister_netdev(dev
);
711 module_init(am79c961_init
);