blob: 7b3e23f38913657896b833a73d7c679b1723558a [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{
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000053 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 "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;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000063 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 "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{
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000073 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 "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;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000083 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 "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
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +000094static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070095am_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) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000100 asm volatile("str%?h %2, [%0], #4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
102 buf += 2;
103 length -= 2;
104 }
105 while (length > 8) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000106 register unsigned int tmp asm("r2"), tmp2 asm("r3");
107 asm volatile(
108 "ldm%?ia %0!, {%1, %2}"
109 : "+r" (buf), "=&r" (tmp), "=&r" (tmp2));
110 length -= 8;
111 asm volatile(
112 "str%?h %1, [%0], #4\n\t"
113 "mov%? %1, %1, lsr #16\n\t"
114 "str%?h %1, [%0], #4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 "str%?h %2, [%0], #4\n\t"
116 "mov%? %2, %2, lsr #16\n\t"
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000117 "str%?h %2, [%0], #4"
118 : "+r" (offset), "=&r" (tmp), "=&r" (tmp2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 while (length > 0) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000121 asm volatile("str%?h %2, [%0], #4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
123 buf += 2;
124 length -= 2;
125 }
126}
127
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000128static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129am_readbuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length)
130{
131 offset = ISAMEM_BASE + (offset << 1);
132 length = (length + 1) & ~1;
133 if ((int)buf & 2) {
134 unsigned int tmp;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000135 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 "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));
141 length -= 2;
142 }
143 while (length > 8) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000144 register unsigned int tmp asm("r2"), tmp2 asm("r3"), tmp3;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000145 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 "ldr%?h %2, [%0], #4\n\t"
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000147 "ldr%?h %4, [%0], #4\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 "ldr%?h %3, [%0], #4\n\t"
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000149 "orr%? %2, %2, %4, lsl #16\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 "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));
155 length -= 8;
156 }
157 while (length > 0) {
158 unsigned int tmp;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000159 asm volatile(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 "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));
165 length -= 2;
166 }
167}
168#else
169#error Not compatible
170#endif
171
172static int
173am79c961_ramtest(struct net_device *dev, unsigned int val)
174{
175 unsigned char *buffer = kmalloc (65536, GFP_KERNEL);
176 int i, error = 0, errorcount = 0;
177
178 if (!buffer)
179 return 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);
187 error = 1;
188 errorcount ++;
189 } else if (error && buffer[i] == val) {
190 printk ("%05X\n", i);
191 error = 0;
192 }
193 }
194 if (error)
195 printk ("10000\n");
196 kfree (buffer);
197 return errorcount;
198}
199
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000200static void am79c961_mc_hash(char *addr, u16 *hash)
201{
202 if (addr[0] & 0x01) {
203 int idx, bit;
204 u32 crc;
205
206 crc = ether_crc_le(ETH_ALEN, addr);
207
208 idx = crc >> 30;
209 bit = (crc >> 26) & 15;
210
211 hash[idx] |= 1 << bit;
212 }
213}
214
215static unsigned int am79c961_get_rx_mode(struct net_device *dev, u16 *hash)
216{
217 unsigned int mode = MODE_PORT_10BT;
218
219 if (dev->flags & IFF_PROMISC) {
220 mode |= MODE_PROMISC;
221 memset(hash, 0xff, 4 * sizeof(*hash));
222 } else if (dev->flags & IFF_ALLMULTI) {
223 memset(hash, 0xff, 4 * sizeof(*hash));
224 } else {
225 struct netdev_hw_addr *ha;
226
227 memset(hash, 0, 4 * sizeof(*hash));
228
229 netdev_for_each_mc_addr(ha, dev)
230 am79c961_mc_hash(ha->addr, hash);
231 }
232
233 return mode;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void
237am79c961_init_for_open(struct net_device *dev)
238{
239 struct dev_priv *priv = netdev_priv(dev);
240 unsigned long flags;
241 unsigned char *p;
242 u_int hdr_addr, first_free_addr;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000243 u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int i;
245
246 /*
247 * Stop the chip.
248 */
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100249 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 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 +0100251 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 write_ireg (dev->base_addr, 5, 0x00a0); /* Receive address LED */
254 write_ireg (dev->base_addr, 6, 0x0081); /* Collision LED */
255 write_ireg (dev->base_addr, 7, 0x0090); /* XMIT LED */
256 write_ireg (dev->base_addr, 2, 0x0000); /* MODE register selects media */
257
258 for (i = LADRL; i <= LADRH; i++)
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000259 write_rreg (dev->base_addr, i, multi_hash[i - LADRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 for (i = PADRL, p = dev->dev_addr; i <= PADRH; i++, p += 2)
262 write_rreg (dev->base_addr, i, p[0] | (p[1] << 8));
263
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000264 write_rreg (dev->base_addr, MODE, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 write_rreg (dev->base_addr, POLLINT, 0);
266 write_rreg (dev->base_addr, SIZERXR, -RX_BUFFERS);
267 write_rreg (dev->base_addr, SIZETXR, -TX_BUFFERS);
268
269 first_free_addr = RX_BUFFERS * 8 + TX_BUFFERS * 8 + 16;
270 hdr_addr = 0;
271
272 priv->rxhead = 0;
273 priv->rxtail = 0;
274 priv->rxhdr = hdr_addr;
275
276 for (i = 0; i < RX_BUFFERS; i++) {
277 priv->rxbuffer[i] = first_free_addr;
278 am_writeword (dev, hdr_addr, first_free_addr);
279 am_writeword (dev, hdr_addr + 2, RMD_OWN);
280 am_writeword (dev, hdr_addr + 4, (-1600));
281 am_writeword (dev, hdr_addr + 6, 0);
282 first_free_addr += 1600;
283 hdr_addr += 8;
284 }
285 priv->txhead = 0;
286 priv->txtail = 0;
287 priv->txhdr = hdr_addr;
288 for (i = 0; i < TX_BUFFERS; i++) {
289 priv->txbuffer[i] = first_free_addr;
290 am_writeword (dev, hdr_addr, first_free_addr);
291 am_writeword (dev, hdr_addr + 2, TMD_STP|TMD_ENP);
292 am_writeword (dev, hdr_addr + 4, 0xf000);
293 am_writeword (dev, hdr_addr + 6, 0);
294 first_free_addr += 1600;
295 hdr_addr += 8;
296 }
297
298 write_rreg (dev->base_addr, BASERXL, priv->rxhdr);
299 write_rreg (dev->base_addr, BASERXH, 0);
300 write_rreg (dev->base_addr, BASETXL, priv->txhdr);
301 write_rreg (dev->base_addr, BASERXH, 0);
302 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
303 write_rreg (dev->base_addr, CSR3, CSR3_IDONM|CSR3_BABLM|CSR3_DXSUFLO);
304 write_rreg (dev->base_addr, CSR4, CSR4_APAD_XMIT|CSR4_MFCOM|CSR4_RCVCCOM|CSR4_TXSTRTM|CSR4_JABM);
305 write_rreg (dev->base_addr, CSR0, CSR0_IENA|CSR0_STRT);
306}
307
308static void am79c961_timer(unsigned long data)
309{
310 struct net_device *dev = (struct net_device *)data;
311 struct dev_priv *priv = netdev_priv(dev);
312 unsigned int lnkstat, carrier;
313
314 lnkstat = read_ireg(dev->base_addr, ISALED0) & ISALED0_LNKST;
315 carrier = netif_carrier_ok(dev);
316
Russell King37bb30e2005-10-31 17:14:57 +0000317 if (lnkstat && !carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 netif_carrier_on(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000319 printk("%s: link up\n", dev->name);
320 } else if (!lnkstat && carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 netif_carrier_off(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000322 printk("%s: link down\n", dev->name);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Russell King487fd4e2005-10-02 18:12:03 +0100325 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
329 * Open/initialize the board.
330 */
331static int
332am79c961_open(struct net_device *dev)
333{
334 struct dev_priv *priv = netdev_priv(dev);
335 int ret;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ret = request_irq(dev->irq, am79c961_interrupt, 0, dev->name, dev);
338 if (ret)
339 return ret;
340
341 am79c961_init_for_open(dev);
342
343 netif_carrier_off(dev);
344
345 priv->timer.expires = jiffies;
346 add_timer(&priv->timer);
347
348 netif_start_queue(dev);
349
350 return 0;
351}
352
353/*
354 * The inverse routine to am79c961_open().
355 */
356static int
357am79c961_close(struct net_device *dev)
358{
359 struct dev_priv *priv = netdev_priv(dev);
360 unsigned long flags;
361
362 del_timer_sync(&priv->timer);
363
364 netif_stop_queue(dev);
365 netif_carrier_off(dev);
366
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100367 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
369 write_rreg (dev->base_addr, CSR3, CSR3_MASKALL);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100370 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 free_irq (dev->irq, dev);
373
374 return 0;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/*
378 * Set or clear promiscuous/multicast mode filter for this adapter.
379 */
380static void am79c961_setmulticastlist (struct net_device *dev)
381{
382 struct dev_priv *priv = netdev_priv(dev);
383 unsigned long flags;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000384 u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int i, stopped;
386
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100387 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 stopped = read_rreg(dev->base_addr, CSR0) & CSR0_STOP;
390
391 if (!stopped) {
392 /*
393 * Put the chip into suspend mode
394 */
395 write_rreg(dev->base_addr, CTRL1, CTRL1_SPND);
396
397 /*
398 * Spin waiting for chip to report suspend mode
399 */
400 while ((read_rreg(dev->base_addr, CTRL1) & CTRL1_SPND) == 0) {
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100401 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 nop();
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100403 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
406
407 /*
408 * Update the multicast hash table
409 */
Denis Chengff8ac602007-09-02 18:30:18 +0800410 for (i = 0; i < ARRAY_SIZE(multi_hash); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 write_rreg(dev->base_addr, i + LADRL, multi_hash[i]);
412
413 /*
414 * Write the mode register
415 */
416 write_rreg(dev->base_addr, MODE, mode);
417
418 if (!stopped) {
419 /*
420 * Put the chip back into running mode
421 */
422 write_rreg(dev->base_addr, CTRL1, 0);
423 }
424
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100425 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static void am79c961_timeout(struct net_device *dev)
429{
430 printk(KERN_WARNING "%s: transmit timed out, network cable problem?\n",
431 dev->name);
432
433 /*
434 * ought to do some setup of the tx side here
435 */
436
437 netif_wake_queue(dev);
438}
439
440/*
441 * Transmit a packet
442 */
443static int
444am79c961_sendpacket(struct sk_buff *skb, struct net_device *dev)
445{
446 struct dev_priv *priv = netdev_priv(dev);
447 unsigned int hdraddr, bufaddr;
448 unsigned int head;
449 unsigned long flags;
450
451 head = priv->txhead;
452 hdraddr = priv->txhdr + (head << 3);
453 bufaddr = priv->txbuffer[head];
454 head += 1;
455 if (head >= TX_BUFFERS)
456 head = 0;
457
458 am_writebuffer (dev, bufaddr, skb->data, skb->len);
459 am_writeword (dev, hdraddr + 4, -skb->len);
460 am_writeword (dev, hdraddr + 2, TMD_OWN|TMD_STP|TMD_ENP);
461 priv->txhead = head;
462
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100463 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 write_rreg (dev->base_addr, CSR0, CSR0_TDMD|CSR0_IENA);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100465 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * If the next packet is owned by the ethernet device,
469 * then the tx ring is full and we can't add another
470 * packet.
471 */
472 if (am_readword(dev, priv->txhdr + (priv->txhead << 3) + 2) & TMD_OWN)
473 netif_stop_queue(dev);
474
475 dev_kfree_skb(skb);
476
Patrick McHardy6ed10652009-06-23 06:03:08 +0000477 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/*
481 * If we have a good packet(s), get it/them out of the buffers.
482 */
483static void
484am79c961_rx(struct net_device *dev, struct dev_priv *priv)
485{
486 do {
487 struct sk_buff *skb;
488 u_int hdraddr;
489 u_int pktaddr;
490 u_int status;
491 int len;
492
493 hdraddr = priv->rxhdr + (priv->rxtail << 3);
494 pktaddr = priv->rxbuffer[priv->rxtail];
495
496 status = am_readword (dev, hdraddr + 2);
497 if (status & RMD_OWN) /* do we own it? */
498 break;
499
500 priv->rxtail ++;
501 if (priv->rxtail >= RX_BUFFERS)
502 priv->rxtail = 0;
503
504 if ((status & (RMD_ERR|RMD_STP|RMD_ENP)) != (RMD_STP|RMD_ENP)) {
505 am_writeword (dev, hdraddr + 2, RMD_OWN);
Tobias Klauser264fd772010-08-09 05:04:34 +0000506 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (status & RMD_ERR) {
508 if (status & RMD_FRAM)
Tobias Klauser264fd772010-08-09 05:04:34 +0000509 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (status & RMD_CRC)
Tobias Klauser264fd772010-08-09 05:04:34 +0000511 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else if (status & RMD_STP)
Tobias Klauser264fd772010-08-09 05:04:34 +0000513 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 continue;
515 }
516
517 len = am_readword(dev, hdraddr + 6);
518 skb = dev_alloc_skb(len + 2);
519
520 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 skb_reserve(skb, 2);
522
523 am_readbuffer(dev, pktaddr, skb_put(skb, len), len);
524 am_writeword(dev, hdraddr + 2, RMD_OWN);
525 skb->protocol = eth_type_trans(skb, dev);
526 netif_rx(skb);
Tobias Klauser264fd772010-08-09 05:04:34 +0000527 dev->stats.rx_bytes += len;
528 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else {
530 am_writeword (dev, hdraddr + 2, RMD_OWN);
531 printk (KERN_WARNING "%s: memory squeeze, dropping packet.\n", dev->name);
Tobias Klauser264fd772010-08-09 05:04:34 +0000532 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534 }
535 } while (1);
536}
537
538/*
539 * Update stats for the transmitted packet
540 */
541static void
542am79c961_tx(struct net_device *dev, struct dev_priv *priv)
543{
544 do {
545 short len;
546 u_int hdraddr;
547 u_int status;
548
549 hdraddr = priv->txhdr + (priv->txtail << 3);
550 status = am_readword (dev, hdraddr + 2);
551 if (status & TMD_OWN)
552 break;
553
554 priv->txtail ++;
555 if (priv->txtail >= TX_BUFFERS)
556 priv->txtail = 0;
557
558 if (status & TMD_ERR) {
559 u_int status2;
560
Tobias Klauser264fd772010-08-09 05:04:34 +0000561 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 status2 = am_readword (dev, hdraddr + 6);
564
565 /*
566 * Clear the error byte
567 */
568 am_writeword (dev, hdraddr + 6, 0);
569
570 if (status2 & TST_RTRY)
Tobias Klauser264fd772010-08-09 05:04:34 +0000571 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (status2 & TST_LCOL)
Tobias Klauser264fd772010-08-09 05:04:34 +0000573 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (status2 & TST_LCAR)
Tobias Klauser264fd772010-08-09 05:04:34 +0000575 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (status2 & TST_UFLO)
Tobias Klauser264fd772010-08-09 05:04:34 +0000577 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 continue;
579 }
Tobias Klauser264fd772010-08-09 05:04:34 +0000580 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 len = am_readword (dev, hdraddr + 4);
Tobias Klauser264fd772010-08-09 05:04:34 +0000582 dev->stats.tx_bytes += -len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } while (priv->txtail != priv->txhead);
584
585 netif_wake_queue(dev);
586}
587
588static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100589am79c961_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct net_device *dev = (struct net_device *)dev_id;
592 struct dev_priv *priv = netdev_priv(dev);
593 u_int status, n = 100;
594 int handled = 0;
595
596 do {
597 status = read_rreg(dev->base_addr, CSR0);
598 write_rreg(dev->base_addr, CSR0, status &
599 (CSR0_IENA|CSR0_TINT|CSR0_RINT|
600 CSR0_MERR|CSR0_MISS|CSR0_CERR|CSR0_BABL));
601
602 if (status & CSR0_RINT) {
603 handled = 1;
604 am79c961_rx(dev, priv);
605 }
606 if (status & CSR0_TINT) {
607 handled = 1;
608 am79c961_tx(dev, priv);
609 }
610 if (status & CSR0_MISS) {
611 handled = 1;
Tobias Klauser264fd772010-08-09 05:04:34 +0000612 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 if (status & CSR0_CERR) {
615 handled = 1;
616 mod_timer(&priv->timer, jiffies);
617 }
618 } while (--n && status & (CSR0_RINT | CSR0_TINT));
619
620 return IRQ_RETVAL(handled);
621}
622
623#ifdef CONFIG_NET_POLL_CONTROLLER
624static void am79c961_poll_controller(struct net_device *dev)
625{
626 unsigned long flags;
627 local_irq_save(flags);
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -0700628 am79c961_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 local_irq_restore(flags);
630}
631#endif
632
633/*
634 * Initialise the chip. Note that we always expect
635 * to be entered with interrupts enabled.
636 */
637static int
638am79c961_hw_init(struct net_device *dev)
639{
640 struct dev_priv *priv = netdev_priv(dev);
641
642 spin_lock_irq(&priv->chip_lock);
643 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
644 write_rreg (dev->base_addr, CSR3, CSR3_MASKALL);
645 spin_unlock_irq(&priv->chip_lock);
646
647 am79c961_ramtest(dev, 0x66);
648 am79c961_ramtest(dev, 0x99);
649
650 return 0;
651}
652
653static void __init am79c961_banner(void)
654{
655 static unsigned version_printed;
656
657 if (net_debug && version_printed++ == 0)
658 printk(KERN_INFO "%s", version);
659}
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000660static const struct net_device_ops am79c961_netdev_ops = {
661 .ndo_open = am79c961_open,
662 .ndo_stop = am79c961_close,
663 .ndo_start_xmit = am79c961_sendpacket,
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000664 .ndo_set_multicast_list = am79c961_setmulticastlist,
665 .ndo_tx_timeout = am79c961_timeout,
666 .ndo_validate_addr = eth_validate_addr,
667 .ndo_change_mtu = eth_change_mtu,
668 .ndo_set_mac_address = eth_mac_addr,
669#ifdef CONFIG_NET_POLL_CONTROLLER
670 .ndo_poll_controller = am79c961_poll_controller,
671#endif
672};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Uwe Kleine-König447d8c22010-02-12 12:10:47 -0800674static int __devinit am79c961_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Russell King37bb30e2005-10-31 17:14:57 +0000676 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct net_device *dev;
678 struct dev_priv *priv;
679 int i, ret;
680
Russell King37bb30e2005-10-31 17:14:57 +0000681 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
682 if (!res)
683 return -ENODEV;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dev = alloc_etherdev(sizeof(struct dev_priv));
686 ret = -ENOMEM;
687 if (!dev)
688 goto out;
689
Russell King37bb30e2005-10-31 17:14:57 +0000690 SET_NETDEV_DEV(dev, &pdev->dev);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 priv = netdev_priv(dev);
693
694 /*
695 * Fixed address and IRQ lines here.
696 * The PNP initialisation should have been
697 * done by the ether bootp loader.
698 */
Russell King37bb30e2005-10-31 17:14:57 +0000699 dev->base_addr = res->start;
Roel Kluin62783672008-04-23 23:44:03 +0200700 ret = platform_get_irq(pdev, 0);
701
702 if (ret < 0) {
703 ret = -ENODEV;
704 goto nodev;
705 }
706 dev->irq = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
David Vrabel48944732006-01-19 17:56:29 +0000708 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!request_region(dev->base_addr, 0x18, dev->name))
710 goto nodev;
711
712 /*
713 * Reset the device.
714 */
715 inb(dev->base_addr + NET_RESET);
716 udelay(5);
717
718 /*
719 * Check the manufacturer part of the
720 * ether address.
721 */
722 if (inb(dev->base_addr) != 0x08 ||
723 inb(dev->base_addr + 2) != 0x00 ||
724 inb(dev->base_addr + 4) != 0x2b)
725 goto release;
726
Russell King487fd4e2005-10-02 18:12:03 +0100727 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dev->dev_addr[i] = inb(dev->base_addr + i * 2) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Russell King37bb30e2005-10-31 17:14:57 +0000730 am79c961_banner();
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 spin_lock_init(&priv->chip_lock);
733 init_timer(&priv->timer);
734 priv->timer.data = (unsigned long)dev;
735 priv->timer.function = am79c961_timer;
736
737 if (am79c961_hw_init(dev))
738 goto release;
739
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000740 dev->netdev_ops = &am79c961_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 ret = register_netdev(dev);
Russell King487fd4e2005-10-02 18:12:03 +0100743 if (ret == 0) {
Johannes Berge1749612008-10-27 15:59:26 -0700744 printk(KERN_INFO "%s: ether address %pM\n",
745 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
Russell King487fd4e2005-10-02 18:12:03 +0100747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749release:
750 release_region(dev->base_addr, 0x18);
751nodev:
752 free_netdev(dev);
753out:
754 return ret;
755}
756
Russell King50465d02006-01-09 13:59:36 +0000757static struct platform_driver am79c961_driver = {
Russell King37bb30e2005-10-31 17:14:57 +0000758 .probe = am79c961_probe,
Russell King50465d02006-01-09 13:59:36 +0000759 .driver = {
760 .name = "am79c961",
761 },
Russell King37bb30e2005-10-31 17:14:57 +0000762};
763
764static int __init am79c961_init(void)
765{
Russell King50465d02006-01-09 13:59:36 +0000766 return platform_driver_register(&am79c961_driver);
Russell King37bb30e2005-10-31 17:14:57 +0000767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769__initcall(am79c961_init);