blob: 62f21106efec224d3447f408439a55bb7d4743e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/net/am79c961.c
3 *
4 * by Russell King <rmk@arm.linux.org.uk> 1995-2001.
5 *
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.
9 *
10 * Derived from various things including skeleton.c
11 *
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).
15 */
16#include <linux/kernel.h>
17#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/string.h>
22#include <linux/errno.h>
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/crc32.h>
28#include <linux/bitops.h>
Russell King37bb30e2005-10-31 17:14:57 +000029#include <linux/platform_device.h>
Russell King99730222009-03-25 10:21:35 +000030#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/hardware.h>
Russell King37bb30e2005-10-31 17:14:57 +000033#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define TX_BUFFERS 15
36#define RX_BUFFERS 25
37
38#include "am79c961a.h"
39
40static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +010041am79c961_interrupt (int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static unsigned int net_debug = NET_DEBUG;
44
45static const char version[] =
46 "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n";
47
48/* --------------------------------------------------------------------------- */
49
50#ifdef __arm__
51static void write_rreg(u_long base, u_int reg, u_int val)
52{
53 __asm__(
54 "str%?h %1, [%2] @ NET_RAP\n\t"
55 "str%?h %0, [%2, #-4] @ NET_RDP"
56 :
57 : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464));
58}
59
60static inline unsigned short read_rreg(u_long base_addr, u_int reg)
61{
62 unsigned short v;
63 __asm__(
64 "str%?h %1, [%2] @ NET_RAP\n\t"
65 "ldr%?h %0, [%2, #-4] @ NET_RDP"
66 : "=r" (v)
67 : "r" (reg), "r" (ISAIO_BASE + 0x0464));
68 return v;
69}
70
71static inline void write_ireg(u_long base, u_int reg, u_int val)
72{
73 __asm__(
74 "str%?h %1, [%2] @ NET_RAP\n\t"
75 "str%?h %0, [%2, #8] @ NET_IDP"
76 :
77 : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464));
78}
79
80static inline unsigned short read_ireg(u_long base_addr, u_int reg)
81{
82 u_short v;
83 __asm__(
84 "str%?h %1, [%2] @ NAT_RAP\n\t"
Russell King0e3a64e2005-10-02 18:02:25 +010085 "ldr%?h %0, [%2, #8] @ NET_IDP\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 : "=r" (v)
87 : "r" (reg), "r" (ISAIO_BASE + 0x0464));
88 return v;
89}
90
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))
93
94static inline void
95am_writebuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length)
96{
97 offset = ISAMEM_BASE + (offset << 1);
98 length = (length + 1) & ~1;
99 if ((int)buf & 2) {
100 __asm__ __volatile__("str%?h %2, [%0], #4"
101 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
102 buf += 2;
103 length -= 2;
104 }
105 while (length > 8) {
106 unsigned int tmp, tmp2;
107 __asm__ __volatile__(
108 "ldm%?ia %1!, {%2, %3}\n\t"
109 "str%?h %2, [%0], #4\n\t"
110 "mov%? %2, %2, lsr #16\n\t"
111 "str%?h %2, [%0], #4\n\t"
112 "str%?h %3, [%0], #4\n\t"
113 "mov%? %3, %3, lsr #16\n\t"
114 "str%?h %3, [%0], #4"
115 : "=&r" (offset), "=&r" (buf), "=r" (tmp), "=r" (tmp2)
116 : "0" (offset), "1" (buf));
117 length -= 8;
118 }
119 while (length > 0) {
120 __asm__ __volatile__("str%?h %2, [%0], #4"
121 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
122 buf += 2;
123 length -= 2;
124 }
125}
126
127static inline void
128am_readbuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length)
129{
130 offset = ISAMEM_BASE + (offset << 1);
131 length = (length + 1) & ~1;
132 if ((int)buf & 2) {
133 unsigned int tmp;
134 __asm__ __volatile__(
135 "ldr%?h %2, [%0], #4\n\t"
136 "str%?b %2, [%1], #1\n\t"
137 "mov%? %2, %2, lsr #8\n\t"
138 "str%?b %2, [%1], #1"
139 : "=&r" (offset), "=&r" (buf), "=r" (tmp): "0" (offset), "1" (buf));
140 length -= 2;
141 }
142 while (length > 8) {
143 unsigned int tmp, tmp2, tmp3;
144 __asm__ __volatile__(
145 "ldr%?h %2, [%0], #4\n\t"
146 "ldr%?h %3, [%0], #4\n\t"
147 "orr%? %2, %2, %3, lsl #16\n\t"
148 "ldr%?h %3, [%0], #4\n\t"
149 "ldr%?h %4, [%0], #4\n\t"
150 "orr%? %3, %3, %4, lsl #16\n\t"
151 "stm%?ia %1!, {%2, %3}"
152 : "=&r" (offset), "=&r" (buf), "=r" (tmp), "=r" (tmp2), "=r" (tmp3)
153 : "0" (offset), "1" (buf));
154 length -= 8;
155 }
156 while (length > 0) {
157 unsigned int tmp;
158 __asm__ __volatile__(
159 "ldr%?h %2, [%0], #4\n\t"
160 "str%?b %2, [%1], #1\n\t"
161 "mov%? %2, %2, lsr #8\n\t"
162 "str%?b %2, [%1], #1"
163 : "=&r" (offset), "=&r" (buf), "=r" (tmp) : "0" (offset), "1" (buf));
164 length -= 2;
165 }
166}
167#else
168#error Not compatible
169#endif
170
171static int
172am79c961_ramtest(struct net_device *dev, unsigned int val)
173{
174 unsigned char *buffer = kmalloc (65536, GFP_KERNEL);
175 int i, error = 0, errorcount = 0;
176
177 if (!buffer)
178 return 0;
179 memset (buffer, val, 65536);
180 am_writebuffer(dev, 0, buffer, 65536);
181 memset (buffer, val ^ 255, 65536);
182 am_readbuffer(dev, 0, buffer, 65536);
183 for (i = 0; i < 65536; i++) {
184 if (buffer[i] != val && !error) {
185 printk ("%s: buffer error (%02X %02X) %05X - ", dev->name, val, buffer[i], i);
186 error = 1;
187 errorcount ++;
188 } else if (error && buffer[i] == val) {
189 printk ("%05X\n", i);
190 error = 0;
191 }
192 }
193 if (error)
194 printk ("10000\n");
195 kfree (buffer);
196 return errorcount;
197}
198
199static void
200am79c961_init_for_open(struct net_device *dev)
201{
202 struct dev_priv *priv = netdev_priv(dev);
203 unsigned long flags;
204 unsigned char *p;
205 u_int hdr_addr, first_free_addr;
206 int i;
207
208 /*
209 * Stop the chip.
210 */
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100211 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 write_rreg (dev->base_addr, CSR0, CSR0_BABL|CSR0_CERR|CSR0_MISS|CSR0_MERR|CSR0_TINT|CSR0_RINT|CSR0_STOP);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100213 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 write_ireg (dev->base_addr, 5, 0x00a0); /* Receive address LED */
216 write_ireg (dev->base_addr, 6, 0x0081); /* Collision LED */
217 write_ireg (dev->base_addr, 7, 0x0090); /* XMIT LED */
218 write_ireg (dev->base_addr, 2, 0x0000); /* MODE register selects media */
219
220 for (i = LADRL; i <= LADRH; i++)
221 write_rreg (dev->base_addr, i, 0);
222
223 for (i = PADRL, p = dev->dev_addr; i <= PADRH; i++, p += 2)
224 write_rreg (dev->base_addr, i, p[0] | (p[1] << 8));
225
226 i = MODE_PORT_10BT;
227 if (dev->flags & IFF_PROMISC)
228 i |= MODE_PROMISC;
229
230 write_rreg (dev->base_addr, MODE, i);
231 write_rreg (dev->base_addr, POLLINT, 0);
232 write_rreg (dev->base_addr, SIZERXR, -RX_BUFFERS);
233 write_rreg (dev->base_addr, SIZETXR, -TX_BUFFERS);
234
235 first_free_addr = RX_BUFFERS * 8 + TX_BUFFERS * 8 + 16;
236 hdr_addr = 0;
237
238 priv->rxhead = 0;
239 priv->rxtail = 0;
240 priv->rxhdr = hdr_addr;
241
242 for (i = 0; i < RX_BUFFERS; i++) {
243 priv->rxbuffer[i] = first_free_addr;
244 am_writeword (dev, hdr_addr, first_free_addr);
245 am_writeword (dev, hdr_addr + 2, RMD_OWN);
246 am_writeword (dev, hdr_addr + 4, (-1600));
247 am_writeword (dev, hdr_addr + 6, 0);
248 first_free_addr += 1600;
249 hdr_addr += 8;
250 }
251 priv->txhead = 0;
252 priv->txtail = 0;
253 priv->txhdr = hdr_addr;
254 for (i = 0; i < TX_BUFFERS; i++) {
255 priv->txbuffer[i] = first_free_addr;
256 am_writeword (dev, hdr_addr, first_free_addr);
257 am_writeword (dev, hdr_addr + 2, TMD_STP|TMD_ENP);
258 am_writeword (dev, hdr_addr + 4, 0xf000);
259 am_writeword (dev, hdr_addr + 6, 0);
260 first_free_addr += 1600;
261 hdr_addr += 8;
262 }
263
264 write_rreg (dev->base_addr, BASERXL, priv->rxhdr);
265 write_rreg (dev->base_addr, BASERXH, 0);
266 write_rreg (dev->base_addr, BASETXL, priv->txhdr);
267 write_rreg (dev->base_addr, BASERXH, 0);
268 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
269 write_rreg (dev->base_addr, CSR3, CSR3_IDONM|CSR3_BABLM|CSR3_DXSUFLO);
270 write_rreg (dev->base_addr, CSR4, CSR4_APAD_XMIT|CSR4_MFCOM|CSR4_RCVCCOM|CSR4_TXSTRTM|CSR4_JABM);
271 write_rreg (dev->base_addr, CSR0, CSR0_IENA|CSR0_STRT);
272}
273
274static void am79c961_timer(unsigned long data)
275{
276 struct net_device *dev = (struct net_device *)data;
277 struct dev_priv *priv = netdev_priv(dev);
278 unsigned int lnkstat, carrier;
279
280 lnkstat = read_ireg(dev->base_addr, ISALED0) & ISALED0_LNKST;
281 carrier = netif_carrier_ok(dev);
282
Russell King37bb30e2005-10-31 17:14:57 +0000283 if (lnkstat && !carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 netif_carrier_on(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000285 printk("%s: link up\n", dev->name);
286 } else if (!lnkstat && carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 netif_carrier_off(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000288 printk("%s: link down\n", dev->name);
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Russell King487fd4e2005-10-02 18:12:03 +0100291 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Open/initialize the board.
296 */
297static int
298am79c961_open(struct net_device *dev)
299{
300 struct dev_priv *priv = netdev_priv(dev);
301 int ret;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ret = request_irq(dev->irq, am79c961_interrupt, 0, dev->name, dev);
304 if (ret)
305 return ret;
306
307 am79c961_init_for_open(dev);
308
309 netif_carrier_off(dev);
310
311 priv->timer.expires = jiffies;
312 add_timer(&priv->timer);
313
314 netif_start_queue(dev);
315
316 return 0;
317}
318
319/*
320 * The inverse routine to am79c961_open().
321 */
322static int
323am79c961_close(struct net_device *dev)
324{
325 struct dev_priv *priv = netdev_priv(dev);
326 unsigned long flags;
327
328 del_timer_sync(&priv->timer);
329
330 netif_stop_queue(dev);
331 netif_carrier_off(dev);
332
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100333 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
335 write_rreg (dev->base_addr, CSR3, CSR3_MASKALL);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100336 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 free_irq (dev->irq, dev);
339
340 return 0;
341}
342
343/*
344 * Get the current statistics.
345 */
346static struct net_device_stats *am79c961_getstats (struct net_device *dev)
347{
Tobias Klauser264fd772010-08-09 05:04:34 +0000348 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Jiri Pirko3b9a7722010-02-19 23:06:27 +0000351static void am79c961_mc_hash(char *addr, unsigned short *hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Jiri Pirko3b9a7722010-02-19 23:06:27 +0000353 if (addr[0] & 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int idx, bit;
355 u32 crc;
356
Jiri Pirko3b9a7722010-02-19 23:06:27 +0000357 crc = ether_crc_le(ETH_ALEN, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 idx = crc >> 30;
360 bit = (crc >> 26) & 15;
361
362 hash[idx] |= 1 << bit;
363 }
364}
365
366/*
367 * Set or clear promiscuous/multicast mode filter for this adapter.
368 */
369static void am79c961_setmulticastlist (struct net_device *dev)
370{
371 struct dev_priv *priv = netdev_priv(dev);
372 unsigned long flags;
373 unsigned short multi_hash[4], mode;
374 int i, stopped;
375
376 mode = MODE_PORT_10BT;
377
378 if (dev->flags & IFF_PROMISC) {
379 mode |= MODE_PROMISC;
380 } else if (dev->flags & IFF_ALLMULTI) {
381 memset(multi_hash, 0xff, sizeof(multi_hash));
382 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000383 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 memset(multi_hash, 0x00, sizeof(multi_hash));
386
Jiri Pirko22bedad32010-04-01 21:22:57 +0000387 netdev_for_each_mc_addr(ha, dev)
388 am79c961_mc_hash(ha->addr, multi_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100391 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 stopped = read_rreg(dev->base_addr, CSR0) & CSR0_STOP;
394
395 if (!stopped) {
396 /*
397 * Put the chip into suspend mode
398 */
399 write_rreg(dev->base_addr, CTRL1, CTRL1_SPND);
400
401 /*
402 * Spin waiting for chip to report suspend mode
403 */
404 while ((read_rreg(dev->base_addr, CTRL1) & CTRL1_SPND) == 0) {
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100405 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 nop();
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100407 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
410
411 /*
412 * Update the multicast hash table
413 */
Denis Chengff8ac602007-09-02 18:30:18 +0800414 for (i = 0; i < ARRAY_SIZE(multi_hash); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 write_rreg(dev->base_addr, i + LADRL, multi_hash[i]);
416
417 /*
418 * Write the mode register
419 */
420 write_rreg(dev->base_addr, MODE, mode);
421
422 if (!stopped) {
423 /*
424 * Put the chip back into running mode
425 */
426 write_rreg(dev->base_addr, CTRL1, 0);
427 }
428
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100429 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static void am79c961_timeout(struct net_device *dev)
433{
434 printk(KERN_WARNING "%s: transmit timed out, network cable problem?\n",
435 dev->name);
436
437 /*
438 * ought to do some setup of the tx side here
439 */
440
441 netif_wake_queue(dev);
442}
443
444/*
445 * Transmit a packet
446 */
447static int
448am79c961_sendpacket(struct sk_buff *skb, struct net_device *dev)
449{
450 struct dev_priv *priv = netdev_priv(dev);
451 unsigned int hdraddr, bufaddr;
452 unsigned int head;
453 unsigned long flags;
454
455 head = priv->txhead;
456 hdraddr = priv->txhdr + (head << 3);
457 bufaddr = priv->txbuffer[head];
458 head += 1;
459 if (head >= TX_BUFFERS)
460 head = 0;
461
462 am_writebuffer (dev, bufaddr, skb->data, skb->len);
463 am_writeword (dev, hdraddr + 4, -skb->len);
464 am_writeword (dev, hdraddr + 2, TMD_OWN|TMD_STP|TMD_ENP);
465 priv->txhead = head;
466
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100467 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 write_rreg (dev->base_addr, CSR0, CSR0_TDMD|CSR0_IENA);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100469 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /*
472 * If the next packet is owned by the ethernet device,
473 * then the tx ring is full and we can't add another
474 * packet.
475 */
476 if (am_readword(dev, priv->txhdr + (priv->txhead << 3) + 2) & TMD_OWN)
477 netif_stop_queue(dev);
478
479 dev_kfree_skb(skb);
480
Patrick McHardy6ed10652009-06-23 06:03:08 +0000481 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484/*
485 * If we have a good packet(s), get it/them out of the buffers.
486 */
487static void
488am79c961_rx(struct net_device *dev, struct dev_priv *priv)
489{
490 do {
491 struct sk_buff *skb;
492 u_int hdraddr;
493 u_int pktaddr;
494 u_int status;
495 int len;
496
497 hdraddr = priv->rxhdr + (priv->rxtail << 3);
498 pktaddr = priv->rxbuffer[priv->rxtail];
499
500 status = am_readword (dev, hdraddr + 2);
501 if (status & RMD_OWN) /* do we own it? */
502 break;
503
504 priv->rxtail ++;
505 if (priv->rxtail >= RX_BUFFERS)
506 priv->rxtail = 0;
507
508 if ((status & (RMD_ERR|RMD_STP|RMD_ENP)) != (RMD_STP|RMD_ENP)) {
509 am_writeword (dev, hdraddr + 2, RMD_OWN);
Tobias Klauser264fd772010-08-09 05:04:34 +0000510 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (status & RMD_ERR) {
512 if (status & RMD_FRAM)
Tobias Klauser264fd772010-08-09 05:04:34 +0000513 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (status & RMD_CRC)
Tobias Klauser264fd772010-08-09 05:04:34 +0000515 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else if (status & RMD_STP)
Tobias Klauser264fd772010-08-09 05:04:34 +0000517 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 continue;
519 }
520
521 len = am_readword(dev, hdraddr + 6);
522 skb = dev_alloc_skb(len + 2);
523
524 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 skb_reserve(skb, 2);
526
527 am_readbuffer(dev, pktaddr, skb_put(skb, len), len);
528 am_writeword(dev, hdraddr + 2, RMD_OWN);
529 skb->protocol = eth_type_trans(skb, dev);
530 netif_rx(skb);
Tobias Klauser264fd772010-08-09 05:04:34 +0000531 dev->stats.rx_bytes += len;
532 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
534 am_writeword (dev, hdraddr + 2, RMD_OWN);
535 printk (KERN_WARNING "%s: memory squeeze, dropping packet.\n", dev->name);
Tobias Klauser264fd772010-08-09 05:04:34 +0000536 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538 }
539 } while (1);
540}
541
542/*
543 * Update stats for the transmitted packet
544 */
545static void
546am79c961_tx(struct net_device *dev, struct dev_priv *priv)
547{
548 do {
549 short len;
550 u_int hdraddr;
551 u_int status;
552
553 hdraddr = priv->txhdr + (priv->txtail << 3);
554 status = am_readword (dev, hdraddr + 2);
555 if (status & TMD_OWN)
556 break;
557
558 priv->txtail ++;
559 if (priv->txtail >= TX_BUFFERS)
560 priv->txtail = 0;
561
562 if (status & TMD_ERR) {
563 u_int status2;
564
Tobias Klauser264fd772010-08-09 05:04:34 +0000565 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 status2 = am_readword (dev, hdraddr + 6);
568
569 /*
570 * Clear the error byte
571 */
572 am_writeword (dev, hdraddr + 6, 0);
573
574 if (status2 & TST_RTRY)
Tobias Klauser264fd772010-08-09 05:04:34 +0000575 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (status2 & TST_LCOL)
Tobias Klauser264fd772010-08-09 05:04:34 +0000577 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (status2 & TST_LCAR)
Tobias Klauser264fd772010-08-09 05:04:34 +0000579 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (status2 & TST_UFLO)
Tobias Klauser264fd772010-08-09 05:04:34 +0000581 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 continue;
583 }
Tobias Klauser264fd772010-08-09 05:04:34 +0000584 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 len = am_readword (dev, hdraddr + 4);
Tobias Klauser264fd772010-08-09 05:04:34 +0000586 dev->stats.tx_bytes += -len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } while (priv->txtail != priv->txhead);
588
589 netif_wake_queue(dev);
590}
591
592static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100593am79c961_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct net_device *dev = (struct net_device *)dev_id;
596 struct dev_priv *priv = netdev_priv(dev);
597 u_int status, n = 100;
598 int handled = 0;
599
600 do {
601 status = read_rreg(dev->base_addr, CSR0);
602 write_rreg(dev->base_addr, CSR0, status &
603 (CSR0_IENA|CSR0_TINT|CSR0_RINT|
604 CSR0_MERR|CSR0_MISS|CSR0_CERR|CSR0_BABL));
605
606 if (status & CSR0_RINT) {
607 handled = 1;
608 am79c961_rx(dev, priv);
609 }
610 if (status & CSR0_TINT) {
611 handled = 1;
612 am79c961_tx(dev, priv);
613 }
614 if (status & CSR0_MISS) {
615 handled = 1;
Tobias Klauser264fd772010-08-09 05:04:34 +0000616 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 if (status & CSR0_CERR) {
619 handled = 1;
620 mod_timer(&priv->timer, jiffies);
621 }
622 } while (--n && status & (CSR0_RINT | CSR0_TINT));
623
624 return IRQ_RETVAL(handled);
625}
626
627#ifdef CONFIG_NET_POLL_CONTROLLER
628static void am79c961_poll_controller(struct net_device *dev)
629{
630 unsigned long flags;
631 local_irq_save(flags);
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -0700632 am79c961_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 local_irq_restore(flags);
634}
635#endif
636
637/*
638 * Initialise the chip. Note that we always expect
639 * to be entered with interrupts enabled.
640 */
641static int
642am79c961_hw_init(struct net_device *dev)
643{
644 struct dev_priv *priv = netdev_priv(dev);
645
646 spin_lock_irq(&priv->chip_lock);
647 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
648 write_rreg (dev->base_addr, CSR3, CSR3_MASKALL);
649 spin_unlock_irq(&priv->chip_lock);
650
651 am79c961_ramtest(dev, 0x66);
652 am79c961_ramtest(dev, 0x99);
653
654 return 0;
655}
656
657static void __init am79c961_banner(void)
658{
659 static unsigned version_printed;
660
661 if (net_debug && version_printed++ == 0)
662 printk(KERN_INFO "%s", version);
663}
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000664static const struct net_device_ops am79c961_netdev_ops = {
665 .ndo_open = am79c961_open,
666 .ndo_stop = am79c961_close,
667 .ndo_start_xmit = am79c961_sendpacket,
668 .ndo_get_stats = am79c961_getstats,
669 .ndo_set_multicast_list = am79c961_setmulticastlist,
670 .ndo_tx_timeout = am79c961_timeout,
671 .ndo_validate_addr = eth_validate_addr,
672 .ndo_change_mtu = eth_change_mtu,
673 .ndo_set_mac_address = eth_mac_addr,
674#ifdef CONFIG_NET_POLL_CONTROLLER
675 .ndo_poll_controller = am79c961_poll_controller,
676#endif
677};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Uwe Kleine-König447d8c22010-02-12 12:10:47 -0800679static int __devinit am79c961_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Russell King37bb30e2005-10-31 17:14:57 +0000681 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct net_device *dev;
683 struct dev_priv *priv;
684 int i, ret;
685
Russell King37bb30e2005-10-31 17:14:57 +0000686 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
687 if (!res)
688 return -ENODEV;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dev = alloc_etherdev(sizeof(struct dev_priv));
691 ret = -ENOMEM;
692 if (!dev)
693 goto out;
694
Russell King37bb30e2005-10-31 17:14:57 +0000695 SET_NETDEV_DEV(dev, &pdev->dev);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 priv = netdev_priv(dev);
698
699 /*
700 * Fixed address and IRQ lines here.
701 * The PNP initialisation should have been
702 * done by the ether bootp loader.
703 */
Russell King37bb30e2005-10-31 17:14:57 +0000704 dev->base_addr = res->start;
Roel Kluin62783672008-04-23 23:44:03 +0200705 ret = platform_get_irq(pdev, 0);
706
707 if (ret < 0) {
708 ret = -ENODEV;
709 goto nodev;
710 }
711 dev->irq = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
David Vrabel48944732006-01-19 17:56:29 +0000713 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!request_region(dev->base_addr, 0x18, dev->name))
715 goto nodev;
716
717 /*
718 * Reset the device.
719 */
720 inb(dev->base_addr + NET_RESET);
721 udelay(5);
722
723 /*
724 * Check the manufacturer part of the
725 * ether address.
726 */
727 if (inb(dev->base_addr) != 0x08 ||
728 inb(dev->base_addr + 2) != 0x00 ||
729 inb(dev->base_addr + 4) != 0x2b)
730 goto release;
731
Russell King487fd4e2005-10-02 18:12:03 +0100732 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dev->dev_addr[i] = inb(dev->base_addr + i * 2) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Russell King37bb30e2005-10-31 17:14:57 +0000735 am79c961_banner();
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 spin_lock_init(&priv->chip_lock);
738 init_timer(&priv->timer);
739 priv->timer.data = (unsigned long)dev;
740 priv->timer.function = am79c961_timer;
741
742 if (am79c961_hw_init(dev))
743 goto release;
744
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000745 dev->netdev_ops = &am79c961_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 ret = register_netdev(dev);
Russell King487fd4e2005-10-02 18:12:03 +0100748 if (ret == 0) {
Johannes Berge1749612008-10-27 15:59:26 -0700749 printk(KERN_INFO "%s: ether address %pM\n",
750 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
Russell King487fd4e2005-10-02 18:12:03 +0100752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754release:
755 release_region(dev->base_addr, 0x18);
756nodev:
757 free_netdev(dev);
758out:
759 return ret;
760}
761
Russell King50465d02006-01-09 13:59:36 +0000762static struct platform_driver am79c961_driver = {
Russell King37bb30e2005-10-31 17:14:57 +0000763 .probe = am79c961_probe,
Russell King50465d02006-01-09 13:59:36 +0000764 .driver = {
765 .name = "am79c961",
766 },
Russell King37bb30e2005-10-31 17:14:57 +0000767};
768
769static int __init am79c961_init(void)
770{
Russell King50465d02006-01-09 13:59:36 +0000771 return platform_driver_register(&am79c961_driver);
Russell King37bb30e2005-10-31 17:14:57 +0000772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774__initcall(am79c961_init);