1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/net/ethernet/amd/am79c961a.c
5 * by Russell King <rmk@arm.linux.org.uk> 1995-2001.
7 * Derived from various things including skeleton.c
9 * This is a special driver for the am79c961A Lance chip used in the
10 * Intel (formally Digital Equipment Corp) EBSA110 platform. Please
11 * note that this can not be built as a module (it doesn't make sense).
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/crc32.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
29 #include <mach/hardware.h>
34 #include "am79c961a.h"
37 am79c961_interrupt (int irq
, void *dev_id
);
39 static unsigned int net_debug
= NET_DEBUG
;
41 static const char version
[] =
42 "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n";
44 /* --------------------------------------------------------------------------- */
47 static void write_rreg(u_long base
, u_int reg
, u_int val
)
50 "strh %1, [%2] @ NET_RAP\n\t"
51 "strh %0, [%2, #-4] @ NET_RDP"
53 : "r" (val
), "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
56 static inline unsigned short read_rreg(u_long base_addr
, u_int reg
)
60 "strh %1, [%2] @ NET_RAP\n\t"
61 "ldrh %0, [%2, #-4] @ NET_RDP"
63 : "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
67 static inline void write_ireg(u_long base
, u_int reg
, u_int val
)
70 "strh %1, [%2] @ NET_RAP\n\t"
71 "strh %0, [%2, #8] @ NET_IDP"
73 : "r" (val
), "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
76 static inline unsigned short read_ireg(u_long base_addr
, u_int reg
)
80 "strh %1, [%2] @ NAT_RAP\n\t"
81 "ldrh %0, [%2, #8] @ NET_IDP\n\t"
83 : "r" (reg
), "r" (ISAIO_BASE
+ 0x0464));
87 #define am_writeword(dev,off,val) __raw_writew(val, ISAMEM_BASE + ((off) << 1))
88 #define am_readword(dev,off) __raw_readw(ISAMEM_BASE + ((off) << 1))
91 am_writebuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
93 offset
= ISAMEM_BASE
+ (offset
<< 1);
94 length
= (length
+ 1) & ~1;
96 asm volatile("strh %2, [%0], #4"
97 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
102 register unsigned int tmp
asm("r2"), tmp2
asm("r3");
104 "ldmia %0!, {%1, %2}"
105 : "+r" (buf
), "=&r" (tmp
), "=&r" (tmp2
));
108 "strh %1, [%0], #4\n\t"
109 "mov %1, %1, lsr #16\n\t"
110 "strh %1, [%0], #4\n\t"
111 "strh %2, [%0], #4\n\t"
112 "mov %2, %2, lsr #16\n\t"
114 : "+r" (offset
), "=&r" (tmp
), "=&r" (tmp2
));
117 asm volatile("strh %2, [%0], #4"
118 : "=&r" (offset
) : "0" (offset
), "r" (buf
[0] | (buf
[1] << 8)));
125 am_readbuffer(struct net_device
*dev
, u_int offset
, unsigned char *buf
, unsigned int length
)
127 offset
= ISAMEM_BASE
+ (offset
<< 1);
128 length
= (length
+ 1) & ~1;
132 "ldrh %2, [%0], #4\n\t"
133 "strb %2, [%1], #1\n\t"
134 "mov %2, %2, lsr #8\n\t"
136 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
): "0" (offset
), "1" (buf
));
140 register unsigned int tmp
asm("r2"), tmp2
asm("r3"), tmp3
;
142 "ldrh %2, [%0], #4\n\t"
143 "ldrh %4, [%0], #4\n\t"
144 "ldrh %3, [%0], #4\n\t"
145 "orr %2, %2, %4, lsl #16\n\t"
146 "ldrh %4, [%0], #4\n\t"
147 "orr %3, %3, %4, lsl #16\n\t"
148 "stmia %1!, {%2, %3}"
149 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
), "=r" (tmp2
), "=r" (tmp3
)
150 : "0" (offset
), "1" (buf
));
156 "ldrh %2, [%0], #4\n\t"
157 "strb %2, [%1], #1\n\t"
158 "mov %2, %2, lsr #8\n\t"
160 : "=&r" (offset
), "=&r" (buf
), "=r" (tmp
) : "0" (offset
), "1" (buf
));
165 #error Not compatible
169 am79c961_ramtest(struct net_device
*dev
, unsigned int val
)
171 unsigned char *buffer
= kmalloc (65536, GFP_KERNEL
);
172 int i
, error
= 0, errorcount
= 0;
176 memset (buffer
, val
, 65536);
177 am_writebuffer(dev
, 0, buffer
, 65536);
178 memset (buffer
, val
^ 255, 65536);
179 am_readbuffer(dev
, 0, buffer
, 65536);
180 for (i
= 0; i
< 65536; i
++) {
181 if (buffer
[i
] != val
&& !error
) {
182 printk ("%s: buffer error (%02X %02X) %05X - ", dev
->name
, val
, buffer
[i
], i
);
185 } else if (error
&& buffer
[i
] == val
) {
186 printk ("%05X\n", i
);
196 static void am79c961_mc_hash(char *addr
, u16
*hash
)
201 crc
= ether_crc_le(ETH_ALEN
, addr
);
204 bit
= (crc
>> 26) & 15;
206 hash
[idx
] |= 1 << bit
;
209 static unsigned int am79c961_get_rx_mode(struct net_device
*dev
, u16
*hash
)
211 unsigned int mode
= MODE_PORT_10BT
;
213 if (dev
->flags
& IFF_PROMISC
) {
214 mode
|= MODE_PROMISC
;
215 memset(hash
, 0xff, 4 * sizeof(*hash
));
216 } else if (dev
->flags
& IFF_ALLMULTI
) {
217 memset(hash
, 0xff, 4 * sizeof(*hash
));
219 struct netdev_hw_addr
*ha
;
221 memset(hash
, 0, 4 * sizeof(*hash
));
223 netdev_for_each_mc_addr(ha
, dev
)
224 am79c961_mc_hash(ha
->addr
, hash
);
231 am79c961_init_for_open(struct net_device
*dev
)
233 struct dev_priv
*priv
= netdev_priv(dev
);
236 u_int hdr_addr
, first_free_addr
;
237 u16 multi_hash
[4], mode
= am79c961_get_rx_mode(dev
, multi_hash
);
243 spin_lock_irqsave(&priv
->chip_lock
, flags
);
244 write_rreg (dev
->base_addr
, CSR0
, CSR0_BABL
|CSR0_CERR
|CSR0_MISS
|CSR0_MERR
|CSR0_TINT
|CSR0_RINT
|CSR0_STOP
);
245 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
247 write_ireg (dev
->base_addr
, 5, 0x00a0); /* Receive address LED */
248 write_ireg (dev
->base_addr
, 6, 0x0081); /* Collision LED */
249 write_ireg (dev
->base_addr
, 7, 0x0090); /* XMIT LED */
250 write_ireg (dev
->base_addr
, 2, 0x0000); /* MODE register selects media */
252 for (i
= LADRL
; i
<= LADRH
; i
++)
253 write_rreg (dev
->base_addr
, i
, multi_hash
[i
- LADRL
]);
255 for (i
= PADRL
, p
= dev
->dev_addr
; i
<= PADRH
; i
++, p
+= 2)
256 write_rreg (dev
->base_addr
, i
, p
[0] | (p
[1] << 8));
258 write_rreg (dev
->base_addr
, MODE
, mode
);
259 write_rreg (dev
->base_addr
, POLLINT
, 0);
260 write_rreg (dev
->base_addr
, SIZERXR
, -RX_BUFFERS
);
261 write_rreg (dev
->base_addr
, SIZETXR
, -TX_BUFFERS
);
263 first_free_addr
= RX_BUFFERS
* 8 + TX_BUFFERS
* 8 + 16;
268 priv
->rxhdr
= hdr_addr
;
270 for (i
= 0; i
< RX_BUFFERS
; i
++) {
271 priv
->rxbuffer
[i
] = first_free_addr
;
272 am_writeword (dev
, hdr_addr
, first_free_addr
);
273 am_writeword (dev
, hdr_addr
+ 2, RMD_OWN
);
274 am_writeword (dev
, hdr_addr
+ 4, (-1600));
275 am_writeword (dev
, hdr_addr
+ 6, 0);
276 first_free_addr
+= 1600;
281 priv
->txhdr
= hdr_addr
;
282 for (i
= 0; i
< TX_BUFFERS
; i
++) {
283 priv
->txbuffer
[i
] = first_free_addr
;
284 am_writeword (dev
, hdr_addr
, first_free_addr
);
285 am_writeword (dev
, hdr_addr
+ 2, TMD_STP
|TMD_ENP
);
286 am_writeword (dev
, hdr_addr
+ 4, 0xf000);
287 am_writeword (dev
, hdr_addr
+ 6, 0);
288 first_free_addr
+= 1600;
292 write_rreg (dev
->base_addr
, BASERXL
, priv
->rxhdr
);
293 write_rreg (dev
->base_addr
, BASERXH
, 0);
294 write_rreg (dev
->base_addr
, BASETXL
, priv
->txhdr
);
295 write_rreg (dev
->base_addr
, BASERXH
, 0);
296 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
297 write_rreg (dev
->base_addr
, CSR3
, CSR3_IDONM
|CSR3_BABLM
|CSR3_DXSUFLO
);
298 write_rreg (dev
->base_addr
, CSR4
, CSR4_APAD_XMIT
|CSR4_MFCOM
|CSR4_RCVCCOM
|CSR4_TXSTRTM
|CSR4_JABM
);
299 write_rreg (dev
->base_addr
, CSR0
, CSR0_IENA
|CSR0_STRT
);
302 static void am79c961_timer(struct timer_list
*t
)
304 struct dev_priv
*priv
= from_timer(priv
, t
, timer
);
305 struct net_device
*dev
= priv
->dev
;
306 unsigned int lnkstat
, carrier
;
309 spin_lock_irqsave(&priv
->chip_lock
, flags
);
310 lnkstat
= read_ireg(dev
->base_addr
, ISALED0
) & ISALED0_LNKST
;
311 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
312 carrier
= netif_carrier_ok(dev
);
314 if (lnkstat
&& !carrier
) {
315 netif_carrier_on(dev
);
316 printk("%s: link up\n", dev
->name
);
317 } else if (!lnkstat
&& carrier
) {
318 netif_carrier_off(dev
);
319 printk("%s: link down\n", dev
->name
);
322 mod_timer(&priv
->timer
, jiffies
+ msecs_to_jiffies(500));
326 * Open/initialize the board.
329 am79c961_open(struct net_device
*dev
)
331 struct dev_priv
*priv
= netdev_priv(dev
);
334 ret
= request_irq(dev
->irq
, am79c961_interrupt
, 0, dev
->name
, dev
);
338 am79c961_init_for_open(dev
);
340 netif_carrier_off(dev
);
342 priv
->timer
.expires
= jiffies
;
343 add_timer(&priv
->timer
);
345 netif_start_queue(dev
);
351 * The inverse routine to am79c961_open().
354 am79c961_close(struct net_device
*dev
)
356 struct dev_priv
*priv
= netdev_priv(dev
);
359 del_timer_sync(&priv
->timer
);
361 netif_stop_queue(dev
);
362 netif_carrier_off(dev
);
364 spin_lock_irqsave(&priv
->chip_lock
, flags
);
365 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
366 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
367 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
369 free_irq (dev
->irq
, dev
);
375 * Set or clear promiscuous/multicast mode filter for this adapter.
377 static void am79c961_setmulticastlist (struct net_device
*dev
)
379 struct dev_priv
*priv
= netdev_priv(dev
);
381 u16 multi_hash
[4], mode
= am79c961_get_rx_mode(dev
, multi_hash
);
384 spin_lock_irqsave(&priv
->chip_lock
, flags
);
386 stopped
= read_rreg(dev
->base_addr
, CSR0
) & CSR0_STOP
;
390 * Put the chip into suspend mode
392 write_rreg(dev
->base_addr
, CTRL1
, CTRL1_SPND
);
395 * Spin waiting for chip to report suspend mode
397 while ((read_rreg(dev
->base_addr
, CTRL1
) & CTRL1_SPND
) == 0) {
398 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
400 spin_lock_irqsave(&priv
->chip_lock
, flags
);
405 * Update the multicast hash table
407 for (i
= 0; i
< ARRAY_SIZE(multi_hash
); i
++)
408 write_rreg(dev
->base_addr
, i
+ LADRL
, multi_hash
[i
]);
411 * Write the mode register
413 write_rreg(dev
->base_addr
, MODE
, mode
);
417 * Put the chip back into running mode
419 write_rreg(dev
->base_addr
, CTRL1
, 0);
422 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
425 static void am79c961_timeout(struct net_device
*dev
, unsigned int txqueue
)
427 printk(KERN_WARNING
"%s: transmit timed out, network cable problem?\n",
431 * ought to do some setup of the tx side here
434 netif_wake_queue(dev
);
441 am79c961_sendpacket(struct sk_buff
*skb
, struct net_device
*dev
)
443 struct dev_priv
*priv
= netdev_priv(dev
);
444 unsigned int hdraddr
, bufaddr
;
449 hdraddr
= priv
->txhdr
+ (head
<< 3);
450 bufaddr
= priv
->txbuffer
[head
];
452 if (head
>= TX_BUFFERS
)
455 am_writebuffer (dev
, bufaddr
, skb
->data
, skb
->len
);
456 am_writeword (dev
, hdraddr
+ 4, -skb
->len
);
457 am_writeword (dev
, hdraddr
+ 2, TMD_OWN
|TMD_STP
|TMD_ENP
);
460 spin_lock_irqsave(&priv
->chip_lock
, flags
);
461 write_rreg (dev
->base_addr
, CSR0
, CSR0_TDMD
|CSR0_IENA
);
462 spin_unlock_irqrestore(&priv
->chip_lock
, flags
);
465 * If the next packet is owned by the ethernet device,
466 * then the tx ring is full and we can't add another
469 if (am_readword(dev
, priv
->txhdr
+ (priv
->txhead
<< 3) + 2) & TMD_OWN
)
470 netif_stop_queue(dev
);
472 dev_consume_skb_any(skb
);
478 * If we have a good packet(s), get it/them out of the buffers.
481 am79c961_rx(struct net_device
*dev
, struct dev_priv
*priv
)
490 hdraddr
= priv
->rxhdr
+ (priv
->rxtail
<< 3);
491 pktaddr
= priv
->rxbuffer
[priv
->rxtail
];
493 status
= am_readword (dev
, hdraddr
+ 2);
494 if (status
& RMD_OWN
) /* do we own it? */
498 if (priv
->rxtail
>= RX_BUFFERS
)
501 if ((status
& (RMD_ERR
|RMD_STP
|RMD_ENP
)) != (RMD_STP
|RMD_ENP
)) {
502 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
503 dev
->stats
.rx_errors
++;
504 if (status
& RMD_ERR
) {
505 if (status
& RMD_FRAM
)
506 dev
->stats
.rx_frame_errors
++;
507 if (status
& RMD_CRC
)
508 dev
->stats
.rx_crc_errors
++;
509 } else if (status
& RMD_STP
)
510 dev
->stats
.rx_length_errors
++;
514 len
= am_readword(dev
, hdraddr
+ 6);
515 skb
= netdev_alloc_skb(dev
, len
+ 2);
520 am_readbuffer(dev
, pktaddr
, skb_put(skb
, len
), len
);
521 am_writeword(dev
, hdraddr
+ 2, RMD_OWN
);
522 skb
->protocol
= eth_type_trans(skb
, dev
);
524 dev
->stats
.rx_bytes
+= len
;
525 dev
->stats
.rx_packets
++;
527 am_writeword (dev
, hdraddr
+ 2, RMD_OWN
);
528 dev
->stats
.rx_dropped
++;
535 * Update stats for the transmitted packet
538 am79c961_tx(struct net_device
*dev
, struct dev_priv
*priv
)
545 hdraddr
= priv
->txhdr
+ (priv
->txtail
<< 3);
546 status
= am_readword (dev
, hdraddr
+ 2);
547 if (status
& TMD_OWN
)
551 if (priv
->txtail
>= TX_BUFFERS
)
554 if (status
& TMD_ERR
) {
557 dev
->stats
.tx_errors
++;
559 status2
= am_readword (dev
, hdraddr
+ 6);
562 * Clear the error byte
564 am_writeword (dev
, hdraddr
+ 6, 0);
566 if (status2
& TST_RTRY
)
567 dev
->stats
.collisions
+= 16;
568 if (status2
& TST_LCOL
)
569 dev
->stats
.tx_window_errors
++;
570 if (status2
& TST_LCAR
)
571 dev
->stats
.tx_carrier_errors
++;
572 if (status2
& TST_UFLO
)
573 dev
->stats
.tx_fifo_errors
++;
576 dev
->stats
.tx_packets
++;
577 len
= am_readword (dev
, hdraddr
+ 4);
578 dev
->stats
.tx_bytes
+= -len
;
579 } while (priv
->txtail
!= priv
->txhead
);
581 netif_wake_queue(dev
);
585 am79c961_interrupt(int irq
, void *dev_id
)
587 struct net_device
*dev
= (struct net_device
*)dev_id
;
588 struct dev_priv
*priv
= netdev_priv(dev
);
589 u_int status
, n
= 100;
593 status
= read_rreg(dev
->base_addr
, CSR0
);
594 write_rreg(dev
->base_addr
, CSR0
, status
&
595 (CSR0_IENA
|CSR0_TINT
|CSR0_RINT
|
596 CSR0_MERR
|CSR0_MISS
|CSR0_CERR
|CSR0_BABL
));
598 if (status
& CSR0_RINT
) {
600 am79c961_rx(dev
, priv
);
602 if (status
& CSR0_TINT
) {
604 am79c961_tx(dev
, priv
);
606 if (status
& CSR0_MISS
) {
608 dev
->stats
.rx_dropped
++;
610 if (status
& CSR0_CERR
) {
612 mod_timer(&priv
->timer
, jiffies
);
614 } while (--n
&& status
& (CSR0_RINT
| CSR0_TINT
));
616 return IRQ_RETVAL(handled
);
619 #ifdef CONFIG_NET_POLL_CONTROLLER
620 static void am79c961_poll_controller(struct net_device
*dev
)
623 local_irq_save(flags
);
624 am79c961_interrupt(dev
->irq
, dev
);
625 local_irq_restore(flags
);
630 * Initialise the chip. Note that we always expect
631 * to be entered with interrupts enabled.
634 am79c961_hw_init(struct net_device
*dev
)
636 struct dev_priv
*priv
= netdev_priv(dev
);
638 spin_lock_irq(&priv
->chip_lock
);
639 write_rreg (dev
->base_addr
, CSR0
, CSR0_STOP
);
640 write_rreg (dev
->base_addr
, CSR3
, CSR3_MASKALL
);
641 spin_unlock_irq(&priv
->chip_lock
);
643 am79c961_ramtest(dev
, 0x66);
644 am79c961_ramtest(dev
, 0x99);
649 static void __init
am79c961_banner(void)
651 static unsigned version_printed
;
653 if (net_debug
&& version_printed
++ == 0)
654 printk(KERN_INFO
"%s", version
);
656 static const struct net_device_ops am79c961_netdev_ops
= {
657 .ndo_open
= am79c961_open
,
658 .ndo_stop
= am79c961_close
,
659 .ndo_start_xmit
= am79c961_sendpacket
,
660 .ndo_set_rx_mode
= am79c961_setmulticastlist
,
661 .ndo_tx_timeout
= am79c961_timeout
,
662 .ndo_validate_addr
= eth_validate_addr
,
663 .ndo_set_mac_address
= eth_mac_addr
,
664 #ifdef CONFIG_NET_POLL_CONTROLLER
665 .ndo_poll_controller
= am79c961_poll_controller
,
669 static int am79c961_probe(struct platform_device
*pdev
)
671 struct resource
*res
;
672 struct net_device
*dev
;
673 struct dev_priv
*priv
;
676 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
680 dev
= alloc_etherdev(sizeof(struct dev_priv
));
685 SET_NETDEV_DEV(dev
, &pdev
->dev
);
687 priv
= netdev_priv(dev
);
690 * Fixed address and IRQ lines here.
691 * The PNP initialisation should have been
692 * done by the ether bootp loader.
694 dev
->base_addr
= res
->start
;
695 ret
= platform_get_irq(pdev
, 0);
704 if (!request_region(dev
->base_addr
, 0x18, dev
->name
))
710 inb(dev
->base_addr
+ NET_RESET
);
714 * Check the manufacturer part of the
717 if (inb(dev
->base_addr
) != 0x08 ||
718 inb(dev
->base_addr
+ 2) != 0x00 ||
719 inb(dev
->base_addr
+ 4) != 0x2b)
722 for (i
= 0; i
< 6; i
++)
723 dev
->dev_addr
[i
] = inb(dev
->base_addr
+ i
* 2) & 0xff;
727 spin_lock_init(&priv
->chip_lock
);
729 timer_setup(&priv
->timer
, am79c961_timer
, 0);
731 if (am79c961_hw_init(dev
))
734 dev
->netdev_ops
= &am79c961_netdev_ops
;
736 ret
= register_netdev(dev
);
738 printk(KERN_INFO
"%s: ether address %pM\n",
739 dev
->name
, dev
->dev_addr
);
744 release_region(dev
->base_addr
, 0x18);
751 static struct platform_driver am79c961_driver
= {
752 .probe
= am79c961_probe
,
758 static int __init
am79c961_init(void)
760 return platform_driver_register(&am79c961_driver
);
763 __initcall(am79c961_init
);