2 * linux/drivers/net/am79c961.c
4 * by Russell King <rmk@arm.linux.org.uk> 1995-2001.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Derived from various things including skeleton.c
12 * This is a special driver for the am79c961A Lance chip used in the
13 * Intel (formally Digital Equipment Corp) EBSA110 platform. Please
14 * note that this can not be built as a module (it doesn't make sense).
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/crc32.h>
28 #include <linux/bitops.h>
29 #include <linux/platform_device.h>
32 #include <mach/hardware.h>
33 #include <asm/system.h>
38 #include "am79c961a.h"
41 am79c961_interrupt (int irq
, void *dev_id
);
43 static unsigned int net_debug
= NET_DEBUG
;
45 static const char version
[] =
46 "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n";
48 /* --------------------------------------------------------------------------- */
51 static void write_rreg(u_long base
, u_int reg
, u_int val
)
54 "str%?h %1, [%2] @ NET_RAP\n\t"
55 "str%?h %0, [%2, #-4] @ NET_RDP"
57 : "r" (val
), "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
60 static inline unsigned short read_rreg(u_long base_addr
, u_int reg
)
64 "str%?h %1, [%2] @ NET_RAP\n\t"
65 "ldr%?h %0, [%2, #-4] @ NET_RDP"
67 : "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
71 static inline void write_ireg(u_long base
, u_int reg
, u_int val
)
74 "str%?h %1, [%2] @ NET_RAP\n\t"
75 "str%?h %0, [%2, #8] @ NET_IDP"
77 : "r" (val
), "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
80 static inline unsigned short read_ireg(u_long base_addr
, u_int reg
)
84 "str%?h %1, [%2] @ NAT_RAP\n\t"
85 "ldr%?h %0, [%2, #8] @ NET_IDP\n\t"
87 : "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
91 #define am_writeword(dev,off,val) __raw_writew(val, ISAMEM_BASE + ((off) << 1))
92 #define am_readword(dev,off) __raw_readw(ISAMEM_BASE + ((off) << 1))
95 am_writebuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
97 offset
= ISAMEM_BASE
+ (offset
<< 1);
98 length
= (length
+ 1) & ~1;
100 asm volatile("str%?h %2, [%0], #4"
101 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
106 register unsigned int tmp
asm("r2"), tmp2
asm("r3");
108 "ldm%?ia %0!, {%1, %2}"
109 : "+r" (buf
), "=&r" (tmp
), "=&r" (tmp2
));
112 "str%?h %1, [%0], #4\n\t"
113 "mov%? %1, %1, lsr #16\n\t"
114 "str%?h %1, [%0], #4\n\t"
115 "str%?h %2, [%0], #4\n\t"
116 "mov%? %2, %2, lsr #16\n\t"
117 "str%?h %2, [%0], #4"
118 : "+r" (offset
), "=&r" (tmp
), "=&r" (tmp2
));
121 asm volatile("str%?h %2, [%0], #4"
122 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
129 am_readbuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
131 offset
= ISAMEM_BASE
+ (offset
<< 1);
132 length
= (length
+ 1) & ~1;
136 "ldr%?h %2, [%0], #4\n\t"
137 "str%?b %2, [%1], #1\n\t"
138 "mov%? %2, %2, lsr #8\n\t"
139 "str%?b %2, [%1], #1"
140 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
): "0" (offset
), "1" (buf
));
144 register unsigned int tmp
asm("r2"), tmp2
asm("r3"), tmp3
;
146 "ldr%?h %2, [%0], #4\n\t"
147 "ldr%?h %4, [%0], #4\n\t"
148 "ldr%?h %3, [%0], #4\n\t"
149 "orr%? %2, %2, %4, lsl #16\n\t"
150 "ldr%?h %4, [%0], #4\n\t"
151 "orr%? %3, %3, %4, lsl #16\n\t"
152 "stm%?ia %1!, {%2, %3}"
153 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
), "=r" (tmp3
)
154 : "0" (offset
), "1" (buf
));
160 "ldr%?h %2, [%0], #4\n\t"
161 "str%?b %2, [%1], #1\n\t"
162 "mov%? %2, %2, lsr #8\n\t"
163 "str%?b %2, [%1], #1"
164 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
) : "0" (offset
), "1" (buf
));
169 #error Not compatible
173 am79c961_ramtest(struct net_device
*dev
, unsigned int val
)
175 unsigned char *buffer
= kmalloc (65536, GFP_KERNEL
);
176 int i
, error
= 0, errorcount
= 0;
180 memset (buffer
, val
, 65536);
181 am_writebuffer(dev
, 0, buffer
, 65536);
182 memset (buffer
, val
^ 255, 65536);
183 am_readbuffer(dev
, 0, buffer
, 65536);
184 for (i
= 0; i
< 65536; i
++) {
185 if (buffer
[i
] != val
&& !error
) {
186 printk ("%s: buffer error (%02X %02X) %05X - ", dev
->name
, val
, buffer
[i
], i
);
189 } else if (error
&& buffer
[i
] == val
) {
190 printk ("%05X\n", i
);
200 static void am79c961_mc_hash(char *addr
, u16
*hash
)
205 crc
= ether_crc_le(ETH_ALEN
, addr
);
208 bit
= (crc
>> 26) & 15;
210 hash
[idx
] |= 1 << bit
;
213 static unsigned int am79c961_get_rx_mode(struct net_device
*dev
, u16
*hash
)
215 unsigned int mode
= MODE_PORT_10BT
;
217 if (dev
->flags
& IFF_PROMISC
) {
218 mode
|= MODE_PROMISC
;
219 memset(hash
, 0xff, 4 * sizeof(*hash
));
220 } else if (dev
->flags
& IFF_ALLMULTI
) {
221 memset(hash
, 0xff, 4 * sizeof(*hash
));
223 struct netdev_hw_addr
*ha
;
225 memset(hash
, 0, 4 * sizeof(*hash
));
227 netdev_for_each_mc_addr(ha
, dev
)
228 am79c961_mc_hash(ha
->addr
, hash
);
235 am79c961_init_for_open(struct net_device
*dev
)
237 struct dev_priv
*priv
= netdev_priv(dev
);
240 u_int hdr_addr
, first_free_addr
;
241 u16 multi_hash
[4], mode
= am79c961_get_rx_mode(dev
, multi_hash
);
247 spin_lock_irqsave(&priv
->chip_lock
, flags
);
248 write_rreg (dev
->base_addr
, CSR0
, CSR0_BABL
|CSR0_CERR
|CSR0_MISS
|CSR0_MERR
|CSR0_TINT
|CSR0_RINT
|CSR0_STOP
);
249 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
251 write_ireg (dev
->base_addr
, 5, 0x00a0); /* Receive address LED */
252 write_ireg (dev
->base_addr
, 6, 0x0081); /* Collision LED */
253 write_ireg (dev
->base_addr
, 7, 0x0090); /* XMIT LED */
254 write_ireg (dev
->base_addr
, 2, 0x0000); /* MODE register selects media */
256 for (i
= LADRL
; i
<= LADRH
; i
++)
257 write_rreg (dev
->base_addr
, i
, multi_hash
[i
- LADRL
]);
259 for (i
= PADRL
, p
= dev
->dev_addr
; i
<= PADRH
; i
++, p
+= 2)
260 write_rreg (dev
->base_addr
, i
, p
[0] | (p
[1] << 8));
262 write_rreg (dev
->base_addr
, MODE
, mode
);
263 write_rreg (dev
->base_addr
, POLLINT
, 0);
264 write_rreg (dev
->base_addr
, SIZERXR
, -RX_BUFFERS
);
265 write_rreg (dev
->base_addr
, SIZETXR
, -TX_BUFFERS
);
267 first_free_addr
= RX_BUFFERS
* 8 + TX_BUFFERS
* 8 + 16;
272 priv
->rxhdr
= hdr_addr
;
274 for (i
= 0; i
< RX_BUFFERS
; i
++) {
275 priv
->rxbuffer
[i
] = first_free_addr
;
276 am_writeword (dev
, hdr_addr
, first_free_addr
);
277 am_writeword (dev
, hdr_addr
+ 2, RMD_OWN
);
278 am_writeword (dev
, hdr_addr
+ 4, (-1600));
279 am_writeword (dev
, hdr_addr
+ 6, 0);
280 first_free_addr
+= 1600;
285 priv
->txhdr
= hdr_addr
;
286 for (i
= 0; i
< TX_BUFFERS
; i
++) {
287 priv
->txbuffer
[i
] = first_free_addr
;
288 am_writeword (dev
, hdr_addr
, first_free_addr
);
289 am_writeword (dev
, hdr_addr
+ 2, TMD_STP
|TMD_ENP
);
290 am_writeword (dev
, hdr_addr
+ 4, 0xf000);
291 am_writeword (dev
, hdr_addr
+ 6, 0);
292 first_free_addr
+= 1600;
296 write_rreg (dev
->base_addr
, BASERXL
, priv
->rxhdr
);
297 write_rreg (dev
->base_addr
, BASERXH
, 0);
298 write_rreg (dev
->base_addr
, BASETXL
, priv
->txhdr
);
299 write_rreg (dev
->base_addr
, BASERXH
, 0);
300 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
301 write_rreg (dev
->base_addr
, CSR3
, CSR3_IDONM
|CSR3_BABLM
|CSR3_DXSUFLO
);
302 write_rreg (dev
->base_addr
, CSR4
, CSR4_APAD_XMIT
|CSR4_MFCOM
|CSR4_RCVCCOM
|CSR4_TXSTRTM
|CSR4_JABM
);
303 write_rreg (dev
->base_addr
, CSR0
, CSR0_IENA
|CSR0_STRT
);
306 static void am79c961_timer(unsigned long data
)
308 struct net_device
*dev
= (struct net_device
*)data
;
309 struct dev_priv
*priv
= netdev_priv(dev
);
310 unsigned int lnkstat
, carrier
;
313 spin_lock_irqsave(&priv
->chip_lock
, flags
);
314 lnkstat
= read_ireg(dev
->base_addr
, ISALED0
) & ISALED0_LNKST
;
315 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
316 carrier
= netif_carrier_ok(dev
);
318 if (lnkstat
&& !carrier
) {
319 netif_carrier_on(dev
);
320 printk("%s: link up\n", dev
->name
);
321 } else if (!lnkstat
&& carrier
) {
322 netif_carrier_off(dev
);
323 printk("%s: link down\n", dev
->name
);
326 mod_timer(&priv
->timer
, jiffies
+ msecs_to_jiffies(500));
330 * Open/initialize the board.
333 am79c961_open(struct net_device
*dev
)
335 struct dev_priv
*priv
= netdev_priv(dev
);
338 ret
= request_irq(dev
->irq
, am79c961_interrupt
, 0, dev
->name
, dev
);
342 am79c961_init_for_open(dev
);
344 netif_carrier_off(dev
);
346 priv
->timer
.expires
= jiffies
;
347 add_timer(&priv
->timer
);
349 netif_start_queue(dev
);
355 * The inverse routine to am79c961_open().
358 am79c961_close(struct net_device
*dev
)
360 struct dev_priv
*priv
= netdev_priv(dev
);
363 del_timer_sync(&priv
->timer
);
365 netif_stop_queue(dev
);
366 netif_carrier_off(dev
);
368 spin_lock_irqsave(&priv
->chip_lock
, flags
);
369 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
370 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
371 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
373 free_irq (dev
->irq
, dev
);
379 * Set or clear promiscuous/multicast mode filter for this adapter.
381 static void am79c961_setmulticastlist (struct net_device
*dev
)
383 struct dev_priv
*priv
= netdev_priv(dev
);
385 u16 multi_hash
[4], mode
= am79c961_get_rx_mode(dev
, multi_hash
);
388 spin_lock_irqsave(&priv
->chip_lock
, flags
);
390 stopped
= read_rreg(dev
->base_addr
, CSR0
) & CSR0_STOP
;
394 * Put the chip into suspend mode
396 write_rreg(dev
->base_addr
, CTRL1
, CTRL1_SPND
);
399 * Spin waiting for chip to report suspend mode
401 while ((read_rreg(dev
->base_addr
, CTRL1
) & CTRL1_SPND
) == 0) {
402 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
404 spin_lock_irqsave(&priv
->chip_lock
, flags
);
409 * Update the multicast hash table
411 for (i
= 0; i
< ARRAY_SIZE(multi_hash
); i
++)
412 write_rreg(dev
->base_addr
, i
+ LADRL
, multi_hash
[i
]);
415 * Write the mode register
417 write_rreg(dev
->base_addr
, MODE
, mode
);
421 * Put the chip back into running mode
423 write_rreg(dev
->base_addr
, CTRL1
, 0);
426 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
429 static void am79c961_timeout(struct net_device
*dev
)
431 printk(KERN_WARNING
"%s: transmit timed out, network cable problem?\n",
435 * ought to do some setup of the tx side here
438 netif_wake_queue(dev
);
445 am79c961_sendpacket(struct sk_buff
*skb
, struct net_device
*dev
)
447 struct dev_priv
*priv
= netdev_priv(dev
);
448 unsigned int hdraddr
, bufaddr
;
453 hdraddr
= priv
->txhdr
+ (head
<< 3);
454 bufaddr
= priv
->txbuffer
[head
];
456 if (head
>= TX_BUFFERS
)
459 am_writebuffer (dev
, bufaddr
, skb
->data
, skb
->len
);
460 am_writeword (dev
, hdraddr
+ 4, -skb
->len
);
461 am_writeword (dev
, hdraddr
+ 2, TMD_OWN
|TMD_STP
|TMD_ENP
);
464 spin_lock_irqsave(&priv
->chip_lock
, flags
);
465 write_rreg (dev
->base_addr
, CSR0
, CSR0_TDMD
|CSR0_IENA
);
466 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
469 * If the next packet is owned by the ethernet device,
470 * then the tx ring is full and we can't add another
473 if (am_readword(dev
, priv
->txhdr
+ (priv
->txhead
<< 3) + 2) & TMD_OWN
)
474 netif_stop_queue(dev
);
482 * If we have a good packet(s), get it/them out of the buffers.
485 am79c961_rx(struct net_device
*dev
, struct dev_priv
*priv
)
494 hdraddr
= priv
->rxhdr
+ (priv
->rxtail
<< 3);
495 pktaddr
= priv
->rxbuffer
[priv
->rxtail
];
497 status
= am_readword (dev
, hdraddr
+ 2);
498 if (status
& RMD_OWN
) /* do we own it? */
502 if (priv
->rxtail
>= RX_BUFFERS
)
505 if ((status
& (RMD_ERR
|RMD_STP
|RMD_ENP
)) != (RMD_STP
|RMD_ENP
)) {
506 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
507 dev
->stats
.rx_errors
++;
508 if (status
& RMD_ERR
) {
509 if (status
& RMD_FRAM
)
510 dev
->stats
.rx_frame_errors
++;
511 if (status
& RMD_CRC
)
512 dev
->stats
.rx_crc_errors
++;
513 } else if (status
& RMD_STP
)
514 dev
->stats
.rx_length_errors
++;
518 len
= am_readword(dev
, hdraddr
+ 6);
519 skb
= dev_alloc_skb(len
+ 2);
524 am_readbuffer(dev
, pktaddr
, skb_put(skb
, len
), len
);
525 am_writeword(dev
, hdraddr
+ 2, RMD_OWN
);
526 skb
->protocol
= eth_type_trans(skb
, dev
);
528 dev
->stats
.rx_bytes
+= len
;
529 dev
->stats
.rx_packets
++;
531 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
532 printk (KERN_WARNING
"%s: memory squeeze, dropping packet.\n", dev
->name
);
533 dev
->stats
.rx_dropped
++;
540 * Update stats for the transmitted packet
543 am79c961_tx(struct net_device
*dev
, struct dev_priv
*priv
)
550 hdraddr
= priv
->txhdr
+ (priv
->txtail
<< 3);
551 status
= am_readword (dev
, hdraddr
+ 2);
552 if (status
& TMD_OWN
)
556 if (priv
->txtail
>= TX_BUFFERS
)
559 if (status
& TMD_ERR
) {
562 dev
->stats
.tx_errors
++;
564 status2
= am_readword (dev
, hdraddr
+ 6);
567 * Clear the error byte
569 am_writeword (dev
, hdraddr
+ 6, 0);
571 if (status2
& TST_RTRY
)
572 dev
->stats
.collisions
+= 16;
573 if (status2
& TST_LCOL
)
574 dev
->stats
.tx_window_errors
++;
575 if (status2
& TST_LCAR
)
576 dev
->stats
.tx_carrier_errors
++;
577 if (status2
& TST_UFLO
)
578 dev
->stats
.tx_fifo_errors
++;
581 dev
->stats
.tx_packets
++;
582 len
= am_readword (dev
, hdraddr
+ 4);
583 dev
->stats
.tx_bytes
+= -len
;
584 } while (priv
->txtail
!= priv
->txhead
);
586 netif_wake_queue(dev
);
590 am79c961_interrupt(int irq
, void *dev_id
)
592 struct net_device
*dev
= (struct net_device
*)dev_id
;
593 struct dev_priv
*priv
= netdev_priv(dev
);
594 u_int status
, n
= 100;
598 status
= read_rreg(dev
->base_addr
, CSR0
);
599 write_rreg(dev
->base_addr
, CSR0
, status
&
600 (CSR0_IENA
|CSR0_TINT
|CSR0_RINT
|
601 CSR0_MERR
|CSR0_MISS
|CSR0_CERR
|CSR0_BABL
));
603 if (status
& CSR0_RINT
) {
605 am79c961_rx(dev
, priv
);
607 if (status
& CSR0_TINT
) {
609 am79c961_tx(dev
, priv
);
611 if (status
& CSR0_MISS
) {
613 dev
->stats
.rx_dropped
++;
615 if (status
& CSR0_CERR
) {
617 mod_timer(&priv
->timer
, jiffies
);
619 } while (--n
&& status
& (CSR0_RINT
| CSR0_TINT
));
621 return IRQ_RETVAL(handled
);
624 #ifdef CONFIG_NET_POLL_CONTROLLER
625 static void am79c961_poll_controller(struct net_device
*dev
)
628 local_irq_save(flags
);
629 am79c961_interrupt(dev
->irq
, dev
);
630 local_irq_restore(flags
);
635 * Initialise the chip. Note that we always expect
636 * to be entered with interrupts enabled.
639 am79c961_hw_init(struct net_device
*dev
)
641 struct dev_priv
*priv
= netdev_priv(dev
);
643 spin_lock_irq(&priv
->chip_lock
);
644 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
645 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
646 spin_unlock_irq(&priv
->chip_lock
);
648 am79c961_ramtest(dev
, 0x66);
649 am79c961_ramtest(dev
, 0x99);
654 static void __init
am79c961_banner(void)
656 static unsigned version_printed
;
658 if (net_debug
&& version_printed
++ == 0)
659 printk(KERN_INFO
"%s", version
);
661 static const struct net_device_ops am79c961_netdev_ops
= {
662 .ndo_open
= am79c961_open
,
663 .ndo_stop
= am79c961_close
,
664 .ndo_start_xmit
= am79c961_sendpacket
,
665 .ndo_set_rx_mode
= am79c961_setmulticastlist
,
666 .ndo_tx_timeout
= am79c961_timeout
,
667 .ndo_validate_addr
= eth_validate_addr
,
668 .ndo_change_mtu
= eth_change_mtu
,
669 .ndo_set_mac_address
= eth_mac_addr
,
670 #ifdef CONFIG_NET_POLL_CONTROLLER
671 .ndo_poll_controller
= am79c961_poll_controller
,
675 static int __devinit
am79c961_probe(struct platform_device
*pdev
)
677 struct resource
*res
;
678 struct net_device
*dev
;
679 struct dev_priv
*priv
;
682 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
686 dev
= alloc_etherdev(sizeof(struct dev_priv
));
691 SET_NETDEV_DEV(dev
, &pdev
->dev
);
693 priv
= netdev_priv(dev
);
696 * Fixed address and IRQ lines here.
697 * The PNP initialisation should have been
698 * done by the ether bootp loader.
700 dev
->base_addr
= res
->start
;
701 ret
= platform_get_irq(pdev
, 0);
710 if (!request_region(dev
->base_addr
, 0x18, dev
->name
))
716 inb(dev
->base_addr
+ NET_RESET
);
720 * Check the manufacturer part of the
723 if (inb(dev
->base_addr
) != 0x08 ||
724 inb(dev
->base_addr
+ 2) != 0x00 ||
725 inb(dev
->base_addr
+ 4) != 0x2b)
728 for (i
= 0; i
< 6; i
++)
729 dev
->dev_addr
[i
] = inb(dev
->base_addr
+ i
* 2) & 0xff;
733 spin_lock_init(&priv
->chip_lock
);
734 init_timer(&priv
->timer
);
735 priv
->timer
.data
= (unsigned long)dev
;
736 priv
->timer
.function
= am79c961_timer
;
738 if (am79c961_hw_init(dev
))
741 dev
->netdev_ops
= &am79c961_netdev_ops
;
743 ret
= register_netdev(dev
);
745 printk(KERN_INFO
"%s: ether address %pM\n",
746 dev
->name
, dev
->dev_addr
);
751 release_region(dev
->base_addr
, 0x18);
758 static struct platform_driver am79c961_driver
= {
759 .probe
= am79c961_probe
,
765 static int __init
am79c961_init(void)
767 return platform_driver_register(&am79c961_driver
);
770 __initcall(am79c961_init
);