Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
1da177e4 LT |
18 | #include <linux/interrupt.h> |
19 | #include <linux/ioport.h> | |
1da177e4 LT |
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> | |
1da177e4 LT |
25 | #include <linux/delay.h> |
26 | #include <linux/init.h> | |
27 | #include <linux/crc32.h> | |
28 | #include <linux/bitops.h> | |
37bb30e8 | 29 | #include <linux/platform_device.h> |
99730225 | 30 | #include <linux/io.h> |
1da177e4 | 31 | |
a09e64fb | 32 | #include <mach/hardware.h> |
37bb30e8 | 33 | #include <asm/system.h> |
1da177e4 LT |
34 | |
35 | #define TX_BUFFERS 15 | |
36 | #define RX_BUFFERS 25 | |
37 | ||
38 | #include "am79c961a.h" | |
39 | ||
40 | static irqreturn_t | |
7d12e780 | 41 | am79c961_interrupt (int irq, void *dev_id); |
1da177e4 LT |
42 | |
43 | static unsigned int net_debug = NET_DEBUG; | |
44 | ||
45 | static const char version[] = | |
46 | "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n"; | |
47 | ||
48 | /* --------------------------------------------------------------------------- */ | |
49 | ||
50 | #ifdef __arm__ | |
51 | static void write_rreg(u_long base, u_int reg, u_int val) | |
52 | { | |
d814dee0 | 53 | asm volatile( |
1da177e4 LT |
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 | ||
60 | static inline unsigned short read_rreg(u_long base_addr, u_int reg) | |
61 | { | |
62 | unsigned short v; | |
d814dee0 | 63 | asm volatile( |
1da177e4 LT |
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 | ||
71 | static inline void write_ireg(u_long base, u_int reg, u_int val) | |
72 | { | |
d814dee0 | 73 | asm volatile( |
1da177e4 LT |
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 | ||
80 | static inline unsigned short read_ireg(u_long base_addr, u_int reg) | |
81 | { | |
82 | u_short v; | |
d814dee0 | 83 | asm volatile( |
1da177e4 | 84 | "str%?h %1, [%2] @ NAT_RAP\n\t" |
0e3a64e2 | 85 | "ldr%?h %0, [%2, #8] @ NET_IDP\n\t" |
1da177e4 LT |
86 | : "=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 | ||
f7777378 | 94 | static void |
1da177e4 LT |
95 | am_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) { | |
f7777378 | 100 | asm volatile("str%?h %2, [%0], #4" |
1da177e4 LT |
101 | : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8))); |
102 | buf += 2; | |
103 | length -= 2; | |
104 | } | |
105 | while (length > 8) { | |
f7777378 RKAL |
106 | 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" | |
1da177e4 LT |
115 | "str%?h %2, [%0], #4\n\t" |
116 | "mov%? %2, %2, lsr #16\n\t" | |
f7777378 RKAL |
117 | "str%?h %2, [%0], #4" |
118 | : "+r" (offset), "=&r" (tmp), "=&r" (tmp2)); | |
1da177e4 LT |
119 | } |
120 | while (length > 0) { | |
f7777378 | 121 | asm volatile("str%?h %2, [%0], #4" |
1da177e4 LT |
122 | : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8))); |
123 | buf += 2; | |
124 | length -= 2; | |
125 | } | |
126 | } | |
127 | ||
f7777378 | 128 | static void |
1da177e4 LT |
129 | am_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; | |
d814dee0 | 135 | asm volatile( |
1da177e4 LT |
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)); | |
141 | length -= 2; | |
142 | } | |
143 | while (length > 8) { | |
f7777378 | 144 | register unsigned int tmp asm("r2"), tmp2 asm("r3"), tmp3; |
d814dee0 | 145 | asm volatile( |
1da177e4 | 146 | "ldr%?h %2, [%0], #4\n\t" |
f7777378 | 147 | "ldr%?h %4, [%0], #4\n\t" |
1da177e4 | 148 | "ldr%?h %3, [%0], #4\n\t" |
f7777378 | 149 | "orr%? %2, %2, %4, lsl #16\n\t" |
1da177e4 LT |
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)); | |
155 | length -= 8; | |
156 | } | |
157 | while (length > 0) { | |
158 | unsigned int tmp; | |
d814dee0 | 159 | asm volatile( |
1da177e4 LT |
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)); | |
165 | length -= 2; | |
166 | } | |
167 | } | |
168 | #else | |
169 | #error Not compatible | |
170 | #endif | |
171 | ||
172 | static int | |
173 | am79c961_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 | ||
bfc65013 RKAL |
200 | static void am79c961_mc_hash(char *addr, u16 *hash) |
201 | { | |
bda7ed47 TK |
202 | int idx, bit; |
203 | u32 crc; | |
bfc65013 | 204 | |
bda7ed47 | 205 | crc = ether_crc_le(ETH_ALEN, addr); |
bfc65013 | 206 | |
bda7ed47 TK |
207 | idx = crc >> 30; |
208 | bit = (crc >> 26) & 15; | |
bfc65013 | 209 | |
bda7ed47 | 210 | hash[idx] |= 1 << bit; |
bfc65013 RKAL |
211 | } |
212 | ||
213 | static unsigned int am79c961_get_rx_mode(struct net_device *dev, u16 *hash) | |
214 | { | |
215 | unsigned int mode = MODE_PORT_10BT; | |
216 | ||
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)); | |
222 | } else { | |
223 | struct netdev_hw_addr *ha; | |
224 | ||
225 | memset(hash, 0, 4 * sizeof(*hash)); | |
226 | ||
227 | netdev_for_each_mc_addr(ha, dev) | |
228 | am79c961_mc_hash(ha->addr, hash); | |
229 | } | |
230 | ||
231 | return mode; | |
232 | } | |
233 | ||
1da177e4 LT |
234 | static void |
235 | am79c961_init_for_open(struct net_device *dev) | |
236 | { | |
237 | struct dev_priv *priv = netdev_priv(dev); | |
238 | unsigned long flags; | |
239 | unsigned char *p; | |
240 | u_int hdr_addr, first_free_addr; | |
bfc65013 | 241 | u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash); |
1da177e4 LT |
242 | int i; |
243 | ||
244 | /* | |
245 | * Stop the chip. | |
246 | */ | |
fb22d727 | 247 | spin_lock_irqsave(&priv->chip_lock, flags); |
1da177e4 | 248 | write_rreg (dev->base_addr, CSR0, CSR0_BABL|CSR0_CERR|CSR0_MISS|CSR0_MERR|CSR0_TINT|CSR0_RINT|CSR0_STOP); |
fb22d727 | 249 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
1da177e4 LT |
250 | |
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 */ | |
255 | ||
256 | for (i = LADRL; i <= LADRH; i++) | |
bfc65013 | 257 | write_rreg (dev->base_addr, i, multi_hash[i - LADRL]); |
1da177e4 LT |
258 | |
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)); | |
261 | ||
bfc65013 | 262 | write_rreg (dev->base_addr, MODE, mode); |
1da177e4 LT |
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); | |
266 | ||
267 | first_free_addr = RX_BUFFERS * 8 + TX_BUFFERS * 8 + 16; | |
268 | hdr_addr = 0; | |
269 | ||
270 | priv->rxhead = 0; | |
271 | priv->rxtail = 0; | |
272 | priv->rxhdr = hdr_addr; | |
273 | ||
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; | |
281 | hdr_addr += 8; | |
282 | } | |
283 | priv->txhead = 0; | |
284 | priv->txtail = 0; | |
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; | |
293 | hdr_addr += 8; | |
294 | } | |
295 | ||
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); | |
304 | } | |
305 | ||
306 | static void am79c961_timer(unsigned long data) | |
307 | { | |
308 | struct net_device *dev = (struct net_device *)data; | |
309 | struct dev_priv *priv = netdev_priv(dev); | |
310 | unsigned int lnkstat, carrier; | |
311 | ||
312 | lnkstat = read_ireg(dev->base_addr, ISALED0) & ISALED0_LNKST; | |
313 | carrier = netif_carrier_ok(dev); | |
314 | ||
37bb30e8 | 315 | if (lnkstat && !carrier) { |
1da177e4 | 316 | netif_carrier_on(dev); |
37bb30e8 RK |
317 | printk("%s: link up\n", dev->name); |
318 | } else if (!lnkstat && carrier) { | |
1da177e4 | 319 | netif_carrier_off(dev); |
37bb30e8 RK |
320 | printk("%s: link down\n", dev->name); |
321 | } | |
1da177e4 | 322 | |
487fd4eb | 323 | mod_timer(&priv->timer, jiffies + msecs_to_jiffies(500)); |
1da177e4 LT |
324 | } |
325 | ||
326 | /* | |
327 | * Open/initialize the board. | |
328 | */ | |
329 | static int | |
330 | am79c961_open(struct net_device *dev) | |
331 | { | |
332 | struct dev_priv *priv = netdev_priv(dev); | |
333 | int ret; | |
334 | ||
1da177e4 LT |
335 | ret = request_irq(dev->irq, am79c961_interrupt, 0, dev->name, dev); |
336 | if (ret) | |
337 | return ret; | |
338 | ||
339 | am79c961_init_for_open(dev); | |
340 | ||
341 | netif_carrier_off(dev); | |
342 | ||
343 | priv->timer.expires = jiffies; | |
344 | add_timer(&priv->timer); | |
345 | ||
346 | netif_start_queue(dev); | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | /* | |
352 | * The inverse routine to am79c961_open(). | |
353 | */ | |
354 | static int | |
355 | am79c961_close(struct net_device *dev) | |
356 | { | |
357 | struct dev_priv *priv = netdev_priv(dev); | |
358 | unsigned long flags; | |
359 | ||
360 | del_timer_sync(&priv->timer); | |
361 | ||
362 | netif_stop_queue(dev); | |
363 | netif_carrier_off(dev); | |
364 | ||
fb22d727 | 365 | spin_lock_irqsave(&priv->chip_lock, flags); |
1da177e4 LT |
366 | write_rreg (dev->base_addr, CSR0, CSR0_STOP); |
367 | write_rreg (dev->base_addr, CSR3, CSR3_MASKALL); | |
fb22d727 | 368 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
1da177e4 LT |
369 | |
370 | free_irq (dev->irq, dev); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
1da177e4 LT |
375 | /* |
376 | * Set or clear promiscuous/multicast mode filter for this adapter. | |
377 | */ | |
378 | static void am79c961_setmulticastlist (struct net_device *dev) | |
379 | { | |
380 | struct dev_priv *priv = netdev_priv(dev); | |
381 | unsigned long flags; | |
bfc65013 | 382 | u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash); |
1da177e4 LT |
383 | int i, stopped; |
384 | ||
fb22d727 | 385 | spin_lock_irqsave(&priv->chip_lock, flags); |
1da177e4 LT |
386 | |
387 | stopped = read_rreg(dev->base_addr, CSR0) & CSR0_STOP; | |
388 | ||
389 | if (!stopped) { | |
390 | /* | |
391 | * Put the chip into suspend mode | |
392 | */ | |
393 | write_rreg(dev->base_addr, CTRL1, CTRL1_SPND); | |
394 | ||
395 | /* | |
396 | * Spin waiting for chip to report suspend mode | |
397 | */ | |
398 | while ((read_rreg(dev->base_addr, CTRL1) & CTRL1_SPND) == 0) { | |
fb22d727 | 399 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
1da177e4 | 400 | nop(); |
fb22d727 | 401 | spin_lock_irqsave(&priv->chip_lock, flags); |
1da177e4 LT |
402 | } |
403 | } | |
404 | ||
405 | /* | |
406 | * Update the multicast hash table | |
407 | */ | |
ff8ac609 | 408 | for (i = 0; i < ARRAY_SIZE(multi_hash); i++) |
1da177e4 LT |
409 | write_rreg(dev->base_addr, i + LADRL, multi_hash[i]); |
410 | ||
411 | /* | |
412 | * Write the mode register | |
413 | */ | |
414 | write_rreg(dev->base_addr, MODE, mode); | |
415 | ||
416 | if (!stopped) { | |
417 | /* | |
418 | * Put the chip back into running mode | |
419 | */ | |
420 | write_rreg(dev->base_addr, CTRL1, 0); | |
421 | } | |
422 | ||
fb22d727 | 423 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
1da177e4 LT |
424 | } |
425 | ||
426 | static void am79c961_timeout(struct net_device *dev) | |
427 | { | |
428 | printk(KERN_WARNING "%s: transmit timed out, network cable problem?\n", | |
429 | dev->name); | |
430 | ||
431 | /* | |
432 | * ought to do some setup of the tx side here | |
433 | */ | |
434 | ||
435 | netif_wake_queue(dev); | |
436 | } | |
437 | ||
438 | /* | |
439 | * Transmit a packet | |
440 | */ | |
441 | static int | |
442 | am79c961_sendpacket(struct sk_buff *skb, struct net_device *dev) | |
443 | { | |
444 | struct dev_priv *priv = netdev_priv(dev); | |
445 | unsigned int hdraddr, bufaddr; | |
446 | unsigned int head; | |
447 | unsigned long flags; | |
448 | ||
449 | head = priv->txhead; | |
450 | hdraddr = priv->txhdr + (head << 3); | |
451 | bufaddr = priv->txbuffer[head]; | |
452 | head += 1; | |
453 | if (head >= TX_BUFFERS) | |
454 | head = 0; | |
455 | ||
456 | am_writebuffer (dev, bufaddr, skb->data, skb->len); | |
457 | am_writeword (dev, hdraddr + 4, -skb->len); | |
458 | am_writeword (dev, hdraddr + 2, TMD_OWN|TMD_STP|TMD_ENP); | |
459 | priv->txhead = head; | |
460 | ||
fb22d727 | 461 | spin_lock_irqsave(&priv->chip_lock, flags); |
1da177e4 | 462 | write_rreg (dev->base_addr, CSR0, CSR0_TDMD|CSR0_IENA); |
fb22d727 | 463 | spin_unlock_irqrestore(&priv->chip_lock, flags); |
1da177e4 LT |
464 | |
465 | /* | |
466 | * If the next packet is owned by the ethernet device, | |
467 | * then the tx ring is full and we can't add another | |
468 | * packet. | |
469 | */ | |
470 | if (am_readword(dev, priv->txhdr + (priv->txhead << 3) + 2) & TMD_OWN) | |
471 | netif_stop_queue(dev); | |
472 | ||
473 | dev_kfree_skb(skb); | |
474 | ||
6ed10654 | 475 | return NETDEV_TX_OK; |
1da177e4 LT |
476 | } |
477 | ||
478 | /* | |
479 | * If we have a good packet(s), get it/them out of the buffers. | |
480 | */ | |
481 | static void | |
482 | am79c961_rx(struct net_device *dev, struct dev_priv *priv) | |
483 | { | |
484 | do { | |
485 | struct sk_buff *skb; | |
486 | u_int hdraddr; | |
487 | u_int pktaddr; | |
488 | u_int status; | |
489 | int len; | |
490 | ||
491 | hdraddr = priv->rxhdr + (priv->rxtail << 3); | |
492 | pktaddr = priv->rxbuffer[priv->rxtail]; | |
493 | ||
494 | status = am_readword (dev, hdraddr + 2); | |
495 | if (status & RMD_OWN) /* do we own it? */ | |
496 | break; | |
497 | ||
498 | priv->rxtail ++; | |
499 | if (priv->rxtail >= RX_BUFFERS) | |
500 | priv->rxtail = 0; | |
501 | ||
502 | if ((status & (RMD_ERR|RMD_STP|RMD_ENP)) != (RMD_STP|RMD_ENP)) { | |
503 | am_writeword (dev, hdraddr + 2, RMD_OWN); | |
264fd77a | 504 | dev->stats.rx_errors++; |
1da177e4 LT |
505 | if (status & RMD_ERR) { |
506 | if (status & RMD_FRAM) | |
264fd77a | 507 | dev->stats.rx_frame_errors++; |
1da177e4 | 508 | if (status & RMD_CRC) |
264fd77a | 509 | dev->stats.rx_crc_errors++; |
1da177e4 | 510 | } else if (status & RMD_STP) |
264fd77a | 511 | dev->stats.rx_length_errors++; |
1da177e4 LT |
512 | continue; |
513 | } | |
514 | ||
515 | len = am_readword(dev, hdraddr + 6); | |
516 | skb = dev_alloc_skb(len + 2); | |
517 | ||
518 | if (skb) { | |
1da177e4 LT |
519 | skb_reserve(skb, 2); |
520 | ||
521 | am_readbuffer(dev, pktaddr, skb_put(skb, len), len); | |
522 | am_writeword(dev, hdraddr + 2, RMD_OWN); | |
523 | skb->protocol = eth_type_trans(skb, dev); | |
524 | netif_rx(skb); | |
264fd77a TK |
525 | dev->stats.rx_bytes += len; |
526 | dev->stats.rx_packets++; | |
1da177e4 LT |
527 | } else { |
528 | am_writeword (dev, hdraddr + 2, RMD_OWN); | |
529 | printk (KERN_WARNING "%s: memory squeeze, dropping packet.\n", dev->name); | |
264fd77a | 530 | dev->stats.rx_dropped++; |
1da177e4 LT |
531 | break; |
532 | } | |
533 | } while (1); | |
534 | } | |
535 | ||
536 | /* | |
537 | * Update stats for the transmitted packet | |
538 | */ | |
539 | static void | |
540 | am79c961_tx(struct net_device *dev, struct dev_priv *priv) | |
541 | { | |
542 | do { | |
543 | short len; | |
544 | u_int hdraddr; | |
545 | u_int status; | |
546 | ||
547 | hdraddr = priv->txhdr + (priv->txtail << 3); | |
548 | status = am_readword (dev, hdraddr + 2); | |
549 | if (status & TMD_OWN) | |
550 | break; | |
551 | ||
552 | priv->txtail ++; | |
553 | if (priv->txtail >= TX_BUFFERS) | |
554 | priv->txtail = 0; | |
555 | ||
556 | if (status & TMD_ERR) { | |
557 | u_int status2; | |
558 | ||
264fd77a | 559 | dev->stats.tx_errors++; |
1da177e4 LT |
560 | |
561 | status2 = am_readword (dev, hdraddr + 6); | |
562 | ||
563 | /* | |
564 | * Clear the error byte | |
565 | */ | |
566 | am_writeword (dev, hdraddr + 6, 0); | |
567 | ||
568 | if (status2 & TST_RTRY) | |
264fd77a | 569 | dev->stats.collisions += 16; |
1da177e4 | 570 | if (status2 & TST_LCOL) |
264fd77a | 571 | dev->stats.tx_window_errors++; |
1da177e4 | 572 | if (status2 & TST_LCAR) |
264fd77a | 573 | dev->stats.tx_carrier_errors++; |
1da177e4 | 574 | if (status2 & TST_UFLO) |
264fd77a | 575 | dev->stats.tx_fifo_errors++; |
1da177e4 LT |
576 | continue; |
577 | } | |
264fd77a | 578 | dev->stats.tx_packets++; |
1da177e4 | 579 | len = am_readword (dev, hdraddr + 4); |
264fd77a | 580 | dev->stats.tx_bytes += -len; |
1da177e4 LT |
581 | } while (priv->txtail != priv->txhead); |
582 | ||
583 | netif_wake_queue(dev); | |
584 | } | |
585 | ||
586 | static irqreturn_t | |
7d12e780 | 587 | am79c961_interrupt(int irq, void *dev_id) |
1da177e4 LT |
588 | { |
589 | struct net_device *dev = (struct net_device *)dev_id; | |
590 | struct dev_priv *priv = netdev_priv(dev); | |
591 | u_int status, n = 100; | |
592 | int handled = 0; | |
593 | ||
594 | do { | |
595 | status = read_rreg(dev->base_addr, CSR0); | |
596 | write_rreg(dev->base_addr, CSR0, status & | |
597 | (CSR0_IENA|CSR0_TINT|CSR0_RINT| | |
598 | CSR0_MERR|CSR0_MISS|CSR0_CERR|CSR0_BABL)); | |
599 | ||
600 | if (status & CSR0_RINT) { | |
601 | handled = 1; | |
602 | am79c961_rx(dev, priv); | |
603 | } | |
604 | if (status & CSR0_TINT) { | |
605 | handled = 1; | |
606 | am79c961_tx(dev, priv); | |
607 | } | |
608 | if (status & CSR0_MISS) { | |
609 | handled = 1; | |
264fd77a | 610 | dev->stats.rx_dropped++; |
1da177e4 LT |
611 | } |
612 | if (status & CSR0_CERR) { | |
613 | handled = 1; | |
614 | mod_timer(&priv->timer, jiffies); | |
615 | } | |
616 | } while (--n && status & (CSR0_RINT | CSR0_TINT)); | |
617 | ||
618 | return IRQ_RETVAL(handled); | |
619 | } | |
620 | ||
621 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
622 | static void am79c961_poll_controller(struct net_device *dev) | |
623 | { | |
624 | unsigned long flags; | |
625 | local_irq_save(flags); | |
0da2f0f1 | 626 | am79c961_interrupt(dev->irq, dev); |
1da177e4 LT |
627 | local_irq_restore(flags); |
628 | } | |
629 | #endif | |
630 | ||
631 | /* | |
632 | * Initialise the chip. Note that we always expect | |
633 | * to be entered with interrupts enabled. | |
634 | */ | |
635 | static int | |
636 | am79c961_hw_init(struct net_device *dev) | |
637 | { | |
638 | struct dev_priv *priv = netdev_priv(dev); | |
639 | ||
640 | spin_lock_irq(&priv->chip_lock); | |
641 | write_rreg (dev->base_addr, CSR0, CSR0_STOP); | |
642 | write_rreg (dev->base_addr, CSR3, CSR3_MASKALL); | |
643 | spin_unlock_irq(&priv->chip_lock); | |
644 | ||
645 | am79c961_ramtest(dev, 0x66); | |
646 | am79c961_ramtest(dev, 0x99); | |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
651 | static void __init am79c961_banner(void) | |
652 | { | |
653 | static unsigned version_printed; | |
654 | ||
655 | if (net_debug && version_printed++ == 0) | |
656 | printk(KERN_INFO "%s", version); | |
657 | } | |
fefbfb1e AB |
658 | static const struct net_device_ops am79c961_netdev_ops = { |
659 | .ndo_open = am79c961_open, | |
660 | .ndo_stop = am79c961_close, | |
661 | .ndo_start_xmit = am79c961_sendpacket, | |
fefbfb1e AB |
662 | .ndo_set_multicast_list = am79c961_setmulticastlist, |
663 | .ndo_tx_timeout = am79c961_timeout, | |
664 | .ndo_validate_addr = eth_validate_addr, | |
665 | .ndo_change_mtu = eth_change_mtu, | |
666 | .ndo_set_mac_address = eth_mac_addr, | |
667 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
668 | .ndo_poll_controller = am79c961_poll_controller, | |
669 | #endif | |
670 | }; | |
1da177e4 | 671 | |
447d8c26 | 672 | static int __devinit am79c961_probe(struct platform_device *pdev) |
1da177e4 | 673 | { |
37bb30e8 | 674 | struct resource *res; |
1da177e4 LT |
675 | struct net_device *dev; |
676 | struct dev_priv *priv; | |
677 | int i, ret; | |
678 | ||
37bb30e8 RK |
679 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
680 | if (!res) | |
681 | return -ENODEV; | |
682 | ||
1da177e4 LT |
683 | dev = alloc_etherdev(sizeof(struct dev_priv)); |
684 | ret = -ENOMEM; | |
685 | if (!dev) | |
686 | goto out; | |
687 | ||
37bb30e8 RK |
688 | SET_NETDEV_DEV(dev, &pdev->dev); |
689 | ||
1da177e4 LT |
690 | priv = netdev_priv(dev); |
691 | ||
692 | /* | |
693 | * Fixed address and IRQ lines here. | |
694 | * The PNP initialisation should have been | |
695 | * done by the ether bootp loader. | |
696 | */ | |
37bb30e8 | 697 | dev->base_addr = res->start; |
62783679 | 698 | ret = platform_get_irq(pdev, 0); |
1da177e4 | 699 | |
62783679 RK |
700 | if (ret < 0) { |
701 | ret = -ENODEV; | |
48944738 | 702 | goto nodev; |
62783679 RK |
703 | } |
704 | dev->irq = ret; | |
705 | ||
706 | ret = -ENODEV; | |
1da177e4 LT |
707 | if (!request_region(dev->base_addr, 0x18, dev->name)) |
708 | goto nodev; | |
709 | ||
710 | /* | |
711 | * Reset the device. | |
712 | */ | |
713 | inb(dev->base_addr + NET_RESET); | |
714 | udelay(5); | |
715 | ||
716 | /* | |
717 | * Check the manufacturer part of the | |
718 | * ether address. | |
719 | */ | |
720 | if (inb(dev->base_addr) != 0x08 || | |
721 | inb(dev->base_addr + 2) != 0x00 || | |
722 | inb(dev->base_addr + 4) != 0x2b) | |
723 | goto release; | |
724 | ||
487fd4eb | 725 | for (i = 0; i < 6; i++) |
1da177e4 | 726 | dev->dev_addr[i] = inb(dev->base_addr + i * 2) & 0xff; |
1da177e4 | 727 | |
37bb30e8 RK |
728 | am79c961_banner(); |
729 | ||
1da177e4 LT |
730 | spin_lock_init(&priv->chip_lock); |
731 | init_timer(&priv->timer); | |
732 | priv->timer.data = (unsigned long)dev; | |
733 | priv->timer.function = am79c961_timer; | |
734 | ||
735 | if (am79c961_hw_init(dev)) | |
736 | goto release; | |
737 | ||
fefbfb1e | 738 | dev->netdev_ops = &am79c961_netdev_ops; |
1da177e4 LT |
739 | |
740 | ret = register_netdev(dev); | |
487fd4eb | 741 | if (ret == 0) { |
e174961c JB |
742 | printk(KERN_INFO "%s: ether address %pM\n", |
743 | dev->name, dev->dev_addr); | |
1da177e4 | 744 | return 0; |
487fd4eb | 745 | } |
1da177e4 LT |
746 | |
747 | release: | |
748 | release_region(dev->base_addr, 0x18); | |
749 | nodev: | |
750 | free_netdev(dev); | |
751 | out: | |
752 | return ret; | |
753 | } | |
754 | ||
50465d0d | 755 | static struct platform_driver am79c961_driver = { |
37bb30e8 | 756 | .probe = am79c961_probe, |
50465d0d RK |
757 | .driver = { |
758 | .name = "am79c961", | |
759 | }, | |
37bb30e8 RK |
760 | }; |
761 | ||
762 | static int __init am79c961_init(void) | |
763 | { | |
50465d0d | 764 | return platform_driver_register(&am79c961_driver); |
37bb30e8 RK |
765 | } |
766 | ||
1da177e4 | 767 | __initcall(am79c961_init); |