blob: 0842da492a6475627403e3c7fa949d215112535d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Paul Gortmaker3396c782012-01-27 13:36:01 +00003 * linux/drivers/net/ethernet/amd/am79c961a.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * by Russell King <rmk@arm.linux.org.uk> 1995-2001.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Derived from various things including skeleton.c
8 *
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).
12 */
13#include <linux/kernel.h>
14#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
16#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/crc32.h>
25#include <linux/bitops.h>
Russell King37bb30e2005-10-31 17:14:57 +000026#include <linux/platform_device.h>
Russell King99730222009-03-25 10:21:35 +000027#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define TX_BUFFERS 15
32#define RX_BUFFERS 25
33
34#include "am79c961a.h"
35
36static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +010037am79c961_interrupt (int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static unsigned int net_debug = NET_DEBUG;
40
41static const char version[] =
42 "am79c961 ethernet driver (C) 1995-2001 Russell King v0.04\n";
43
44/* --------------------------------------------------------------------------- */
45
46#ifdef __arm__
47static void write_rreg(u_long base, u_int reg, u_int val)
48{
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000049 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +010050 "strh %1, [%2] @ NET_RAP\n\t"
51 "strh %0, [%2, #-4] @ NET_RDP"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 :
53 : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464));
54}
55
56static inline unsigned short read_rreg(u_long base_addr, u_int reg)
57{
58 unsigned short v;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000059 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +010060 "strh %1, [%2] @ NET_RAP\n\t"
61 "ldrh %0, [%2, #-4] @ NET_RDP"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 : "=r" (v)
63 : "r" (reg), "r" (ISAIO_BASE + 0x0464));
64 return v;
65}
66
67static inline void write_ireg(u_long base, u_int reg, u_int val)
68{
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000069 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +010070 "strh %1, [%2] @ NET_RAP\n\t"
71 "strh %0, [%2, #8] @ NET_IDP"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 :
73 : "r" (val), "r" (reg), "r" (ISAIO_BASE + 0x0464));
74}
75
76static inline unsigned short read_ireg(u_long base_addr, u_int reg)
77{
78 u_short v;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +000079 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +010080 "strh %1, [%2] @ NAT_RAP\n\t"
81 "ldrh %0, [%2, #8] @ NET_IDP\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 : "=r" (v)
83 : "r" (reg), "r" (ISAIO_BASE + 0x0464));
84 return v;
85}
86
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))
89
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +000090static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070091am_writebuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length)
92{
93 offset = ISAMEM_BASE + (offset << 1);
94 length = (length + 1) & ~1;
95 if ((int)buf & 2) {
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +010096 asm volatile("strh %2, [%0], #4"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
98 buf += 2;
99 length -= 2;
100 }
101 while (length > 8) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000102 register unsigned int tmp asm("r2"), tmp2 asm("r3");
103 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100104 "ldmia %0!, {%1, %2}"
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000105 : "+r" (buf), "=&r" (tmp), "=&r" (tmp2));
106 length -= 8;
107 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100108 "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"
113 "strh %2, [%0], #4"
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000114 : "+r" (offset), "=&r" (tmp), "=&r" (tmp2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 while (length > 0) {
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100117 asm volatile("strh %2, [%0], #4"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 : "=&r" (offset) : "0" (offset), "r" (buf[0] | (buf[1] << 8)));
119 buf += 2;
120 length -= 2;
121 }
122}
123
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000124static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125am_readbuffer(struct net_device *dev, u_int offset, unsigned char *buf, unsigned int length)
126{
127 offset = ISAMEM_BASE + (offset << 1);
128 length = (length + 1) & ~1;
129 if ((int)buf & 2) {
130 unsigned int tmp;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000131 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100132 "ldrh %2, [%0], #4\n\t"
133 "strb %2, [%1], #1\n\t"
134 "mov %2, %2, lsr #8\n\t"
135 "strb %2, [%1], #1"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 : "=&r" (offset), "=&r" (buf), "=r" (tmp): "0" (offset), "1" (buf));
137 length -= 2;
138 }
139 while (length > 8) {
Russell King - ARM Linuxf7777372011-06-10 00:52:35 +0000140 register unsigned int tmp asm("r2"), tmp2 asm("r3"), tmp3;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000141 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100142 "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}"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 : "=&r" (offset), "=&r" (buf), "=r" (tmp), "=r" (tmp2), "=r" (tmp3)
150 : "0" (offset), "1" (buf));
151 length -= 8;
152 }
153 while (length > 0) {
154 unsigned int tmp;
Russell King - ARM Linuxd814dee2011-06-10 00:51:54 +0000155 asm volatile(
Arnd Bergmanna5a23ad2016-02-08 15:33:42 +0100156 "ldrh %2, [%0], #4\n\t"
157 "strb %2, [%1], #1\n\t"
158 "mov %2, %2, lsr #8\n\t"
159 "strb %2, [%1], #1"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 : "=&r" (offset), "=&r" (buf), "=r" (tmp) : "0" (offset), "1" (buf));
161 length -= 2;
162 }
163}
164#else
165#error Not compatible
166#endif
167
168static int
169am79c961_ramtest(struct net_device *dev, unsigned int val)
170{
171 unsigned char *buffer = kmalloc (65536, GFP_KERNEL);
172 int i, error = 0, errorcount = 0;
173
174 if (!buffer)
175 return 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);
183 error = 1;
184 errorcount ++;
185 } else if (error && buffer[i] == val) {
186 printk ("%05X\n", i);
187 error = 0;
188 }
189 }
190 if (error)
191 printk ("10000\n");
192 kfree (buffer);
193 return errorcount;
194}
195
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000196static void am79c961_mc_hash(char *addr, u16 *hash)
197{
Tobias Klauserbda7ed42011-06-29 02:14:46 +0000198 int idx, bit;
199 u32 crc;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000200
Tobias Klauserbda7ed42011-06-29 02:14:46 +0000201 crc = ether_crc_le(ETH_ALEN, addr);
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000202
Tobias Klauserbda7ed42011-06-29 02:14:46 +0000203 idx = crc >> 30;
204 bit = (crc >> 26) & 15;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000205
Tobias Klauserbda7ed42011-06-29 02:14:46 +0000206 hash[idx] |= 1 << bit;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000207}
208
209static unsigned int am79c961_get_rx_mode(struct net_device *dev, u16 *hash)
210{
211 unsigned int mode = MODE_PORT_10BT;
212
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));
218 } else {
219 struct netdev_hw_addr *ha;
220
221 memset(hash, 0, 4 * sizeof(*hash));
222
223 netdev_for_each_mc_addr(ha, dev)
224 am79c961_mc_hash(ha->addr, hash);
225 }
226
227 return mode;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void
231am79c961_init_for_open(struct net_device *dev)
232{
233 struct dev_priv *priv = netdev_priv(dev);
234 unsigned long flags;
235 unsigned char *p;
236 u_int hdr_addr, first_free_addr;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000237 u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int i;
239
240 /*
241 * Stop the chip.
242 */
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100243 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 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 +0100245 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
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 */
251
252 for (i = LADRL; i <= LADRH; i++)
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000253 write_rreg (dev->base_addr, i, multi_hash[i - LADRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
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));
257
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000258 write_rreg (dev->base_addr, MODE, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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);
262
263 first_free_addr = RX_BUFFERS * 8 + TX_BUFFERS * 8 + 16;
264 hdr_addr = 0;
265
266 priv->rxhead = 0;
267 priv->rxtail = 0;
268 priv->rxhdr = hdr_addr;
269
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;
277 hdr_addr += 8;
278 }
279 priv->txhead = 0;
280 priv->txtail = 0;
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;
289 hdr_addr += 8;
290 }
291
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);
300}
301
Kees Cookc6c52ba2017-10-26 22:54:38 -0700302static void am79c961_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Kees Cookc6c52ba2017-10-26 22:54:38 -0700304 struct dev_priv *priv = from_timer(priv, t, timer);
305 struct net_device *dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned int lnkstat, carrier;
David S. Miller8decf862011-09-22 03:23:13 -0400307 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David S. Miller8decf862011-09-22 03:23:13 -0400309 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 lnkstat = read_ireg(dev->base_addr, ISALED0) & ISALED0_LNKST;
David S. Miller8decf862011-09-22 03:23:13 -0400311 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 carrier = netif_carrier_ok(dev);
313
Russell King37bb30e2005-10-31 17:14:57 +0000314 if (lnkstat && !carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 netif_carrier_on(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000316 printk("%s: link up\n", dev->name);
317 } else if (!lnkstat && carrier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 netif_carrier_off(dev);
Russell King37bb30e2005-10-31 17:14:57 +0000319 printk("%s: link down\n", dev->name);
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Russell King487fd4e2005-10-02 18:12:03 +0100322 mod_timer(&priv->timer, jiffies + msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * Open/initialize the board.
327 */
328static int
329am79c961_open(struct net_device *dev)
330{
331 struct dev_priv *priv = netdev_priv(dev);
332 int ret;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ret = request_irq(dev->irq, am79c961_interrupt, 0, dev->name, dev);
335 if (ret)
336 return ret;
337
338 am79c961_init_for_open(dev);
339
340 netif_carrier_off(dev);
341
342 priv->timer.expires = jiffies;
343 add_timer(&priv->timer);
344
345 netif_start_queue(dev);
346
347 return 0;
348}
349
350/*
351 * The inverse routine to am79c961_open().
352 */
353static int
354am79c961_close(struct net_device *dev)
355{
356 struct dev_priv *priv = netdev_priv(dev);
357 unsigned long flags;
358
359 del_timer_sync(&priv->timer);
360
361 netif_stop_queue(dev);
362 netif_carrier_off(dev);
363
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100364 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 write_rreg (dev->base_addr, CSR0, CSR0_STOP);
366 write_rreg (dev->base_addr, CSR3, CSR3_MASKALL);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100367 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 free_irq (dev->irq, dev);
370
371 return 0;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * Set or clear promiscuous/multicast mode filter for this adapter.
376 */
377static void am79c961_setmulticastlist (struct net_device *dev)
378{
379 struct dev_priv *priv = netdev_priv(dev);
380 unsigned long flags;
Russell King - ARM Linuxbfc65012011-06-10 00:52:14 +0000381 u16 multi_hash[4], mode = am79c961_get_rx_mode(dev, multi_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int i, stopped;
383
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100384 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 stopped = read_rreg(dev->base_addr, CSR0) & CSR0_STOP;
387
388 if (!stopped) {
389 /*
390 * Put the chip into suspend mode
391 */
392 write_rreg(dev->base_addr, CTRL1, CTRL1_SPND);
393
394 /*
395 * Spin waiting for chip to report suspend mode
396 */
397 while ((read_rreg(dev->base_addr, CTRL1) & CTRL1_SPND) == 0) {
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100398 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 nop();
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100400 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
403
404 /*
405 * Update the multicast hash table
406 */
Denis Chengff8ac602007-09-02 18:30:18 +0800407 for (i = 0; i < ARRAY_SIZE(multi_hash); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 write_rreg(dev->base_addr, i + LADRL, multi_hash[i]);
409
410 /*
411 * Write the mode register
412 */
413 write_rreg(dev->base_addr, MODE, mode);
414
415 if (!stopped) {
416 /*
417 * Put the chip back into running mode
418 */
419 write_rreg(dev->base_addr, CTRL1, 0);
420 }
421
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100422 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static void am79c961_timeout(struct net_device *dev)
426{
427 printk(KERN_WARNING "%s: transmit timed out, network cable problem?\n",
428 dev->name);
429
430 /*
431 * ought to do some setup of the tx side here
432 */
433
434 netif_wake_queue(dev);
435}
436
437/*
438 * Transmit a packet
439 */
YueHaibingfe723522018-09-19 18:50:17 +0800440static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441am79c961_sendpacket(struct sk_buff *skb, struct net_device *dev)
442{
443 struct dev_priv *priv = netdev_priv(dev);
444 unsigned int hdraddr, bufaddr;
445 unsigned int head;
446 unsigned long flags;
447
448 head = priv->txhead;
449 hdraddr = priv->txhdr + (head << 3);
450 bufaddr = priv->txbuffer[head];
451 head += 1;
452 if (head >= TX_BUFFERS)
453 head = 0;
454
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);
458 priv->txhead = head;
459
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100460 spin_lock_irqsave(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 write_rreg (dev->base_addr, CSR0, CSR0_TDMD|CSR0_IENA);
Uwe Kleine-Königfb22d722009-01-22 23:29:42 +0100462 spin_unlock_irqrestore(&priv->chip_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * If the next packet is owned by the ethernet device,
466 * then the tx ring is full and we can't add another
467 * packet.
468 */
469 if (am_readword(dev, priv->txhdr + (priv->txhead << 3) + 2) & TMD_OWN)
470 netif_stop_queue(dev);
471
Eric W. Biedermane21106b2014-03-24 21:18:40 -0700472 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Patrick McHardy6ed10652009-06-23 06:03:08 +0000474 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * If we have a good packet(s), get it/them out of the buffers.
479 */
480static void
481am79c961_rx(struct net_device *dev, struct dev_priv *priv)
482{
483 do {
484 struct sk_buff *skb;
485 u_int hdraddr;
486 u_int pktaddr;
487 u_int status;
488 int len;
489
490 hdraddr = priv->rxhdr + (priv->rxtail << 3);
491 pktaddr = priv->rxbuffer[priv->rxtail];
492
493 status = am_readword (dev, hdraddr + 2);
494 if (status & RMD_OWN) /* do we own it? */
495 break;
496
497 priv->rxtail ++;
498 if (priv->rxtail >= RX_BUFFERS)
499 priv->rxtail = 0;
500
501 if ((status & (RMD_ERR|RMD_STP|RMD_ENP)) != (RMD_STP|RMD_ENP)) {
502 am_writeword (dev, hdraddr + 2, RMD_OWN);
Tobias Klauser264fd772010-08-09 05:04:34 +0000503 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (status & RMD_ERR) {
505 if (status & RMD_FRAM)
Tobias Klauser264fd772010-08-09 05:04:34 +0000506 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (status & RMD_CRC)
Tobias Klauser264fd772010-08-09 05:04:34 +0000508 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else if (status & RMD_STP)
Tobias Klauser264fd772010-08-09 05:04:34 +0000510 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 continue;
512 }
513
514 len = am_readword(dev, hdraddr + 6);
Pradeep A Dalvi1d266432012-02-05 02:49:09 +0000515 skb = netdev_alloc_skb(dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 skb_reserve(skb, 2);
519
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);
523 netif_rx(skb);
Tobias Klauser264fd772010-08-09 05:04:34 +0000524 dev->stats.rx_bytes += len;
525 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
527 am_writeword (dev, hdraddr + 2, RMD_OWN);
Tobias Klauser264fd772010-08-09 05:04:34 +0000528 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 }
531 } while (1);
532}
533
534/*
535 * Update stats for the transmitted packet
536 */
537static void
538am79c961_tx(struct net_device *dev, struct dev_priv *priv)
539{
540 do {
541 short len;
542 u_int hdraddr;
543 u_int status;
544
545 hdraddr = priv->txhdr + (priv->txtail << 3);
546 status = am_readword (dev, hdraddr + 2);
547 if (status & TMD_OWN)
548 break;
549
550 priv->txtail ++;
551 if (priv->txtail >= TX_BUFFERS)
552 priv->txtail = 0;
553
554 if (status & TMD_ERR) {
555 u_int status2;
556
Tobias Klauser264fd772010-08-09 05:04:34 +0000557 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 status2 = am_readword (dev, hdraddr + 6);
560
561 /*
562 * Clear the error byte
563 */
564 am_writeword (dev, hdraddr + 6, 0);
565
566 if (status2 & TST_RTRY)
Tobias Klauser264fd772010-08-09 05:04:34 +0000567 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (status2 & TST_LCOL)
Tobias Klauser264fd772010-08-09 05:04:34 +0000569 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (status2 & TST_LCAR)
Tobias Klauser264fd772010-08-09 05:04:34 +0000571 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (status2 & TST_UFLO)
Tobias Klauser264fd772010-08-09 05:04:34 +0000573 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 continue;
575 }
Tobias Klauser264fd772010-08-09 05:04:34 +0000576 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 len = am_readword (dev, hdraddr + 4);
Tobias Klauser264fd772010-08-09 05:04:34 +0000578 dev->stats.tx_bytes += -len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } while (priv->txtail != priv->txhead);
580
581 netif_wake_queue(dev);
582}
583
584static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100585am79c961_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct net_device *dev = (struct net_device *)dev_id;
588 struct dev_priv *priv = netdev_priv(dev);
589 u_int status, n = 100;
590 int handled = 0;
591
592 do {
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));
597
598 if (status & CSR0_RINT) {
599 handled = 1;
600 am79c961_rx(dev, priv);
601 }
602 if (status & CSR0_TINT) {
603 handled = 1;
604 am79c961_tx(dev, priv);
605 }
606 if (status & CSR0_MISS) {
607 handled = 1;
Tobias Klauser264fd772010-08-09 05:04:34 +0000608 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 if (status & CSR0_CERR) {
611 handled = 1;
612 mod_timer(&priv->timer, jiffies);
613 }
614 } while (--n && status & (CSR0_RINT | CSR0_TINT));
615
616 return IRQ_RETVAL(handled);
617}
618
619#ifdef CONFIG_NET_POLL_CONTROLLER
620static void am79c961_poll_controller(struct net_device *dev)
621{
622 unsigned long flags;
623 local_irq_save(flags);
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -0700624 am79c961_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 local_irq_restore(flags);
626}
627#endif
628
629/*
630 * Initialise the chip. Note that we always expect
631 * to be entered with interrupts enabled.
632 */
633static int
634am79c961_hw_init(struct net_device *dev)
635{
636 struct dev_priv *priv = netdev_priv(dev);
637
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);
642
643 am79c961_ramtest(dev, 0x66);
644 am79c961_ramtest(dev, 0x99);
645
646 return 0;
647}
648
649static void __init am79c961_banner(void)
650{
651 static unsigned version_printed;
652
653 if (net_debug && version_printed++ == 0)
654 printk(KERN_INFO "%s", version);
655}
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000656static const struct net_device_ops am79c961_netdev_ops = {
657 .ndo_open = am79c961_open,
658 .ndo_stop = am79c961_close,
659 .ndo_start_xmit = am79c961_sendpacket,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000660 .ndo_set_rx_mode = am79c961_setmulticastlist,
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000661 .ndo_tx_timeout = am79c961_timeout,
662 .ndo_validate_addr = eth_validate_addr,
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000663 .ndo_set_mac_address = eth_mac_addr,
664#ifdef CONFIG_NET_POLL_CONTROLLER
665 .ndo_poll_controller = am79c961_poll_controller,
666#endif
667};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Bill Pemberton92a66592012-12-03 09:22:45 -0500669static int am79c961_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Russell King37bb30e2005-10-31 17:14:57 +0000671 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct net_device *dev;
673 struct dev_priv *priv;
674 int i, ret;
675
Russell King37bb30e2005-10-31 17:14:57 +0000676 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
677 if (!res)
678 return -ENODEV;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 dev = alloc_etherdev(sizeof(struct dev_priv));
681 ret = -ENOMEM;
682 if (!dev)
683 goto out;
684
Russell King37bb30e2005-10-31 17:14:57 +0000685 SET_NETDEV_DEV(dev, &pdev->dev);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 priv = netdev_priv(dev);
688
689 /*
690 * Fixed address and IRQ lines here.
691 * The PNP initialisation should have been
692 * done by the ether bootp loader.
693 */
Russell King37bb30e2005-10-31 17:14:57 +0000694 dev->base_addr = res->start;
Roel Kluin627836792008-04-23 23:44:03 +0200695 ret = platform_get_irq(pdev, 0);
696
697 if (ret < 0) {
698 ret = -ENODEV;
699 goto nodev;
700 }
701 dev->irq = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David Vrabel48944732006-01-19 17:56:29 +0000703 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!request_region(dev->base_addr, 0x18, dev->name))
705 goto nodev;
706
707 /*
708 * Reset the device.
709 */
710 inb(dev->base_addr + NET_RESET);
711 udelay(5);
712
713 /*
714 * Check the manufacturer part of the
715 * ether address.
716 */
717 if (inb(dev->base_addr) != 0x08 ||
718 inb(dev->base_addr + 2) != 0x00 ||
719 inb(dev->base_addr + 4) != 0x2b)
720 goto release;
721
Russell King487fd4e2005-10-02 18:12:03 +0100722 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dev->dev_addr[i] = inb(dev->base_addr + i * 2) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Russell King37bb30e2005-10-31 17:14:57 +0000725 am79c961_banner();
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 spin_lock_init(&priv->chip_lock);
Kees Cookc6c52ba2017-10-26 22:54:38 -0700728 priv->dev = dev;
729 timer_setup(&priv->timer, am79c961_timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (am79c961_hw_init(dev))
732 goto release;
733
Alexander Beregalovfefbfb12009-04-09 17:25:25 +0000734 dev->netdev_ops = &am79c961_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 ret = register_netdev(dev);
Russell King487fd4e2005-10-02 18:12:03 +0100737 if (ret == 0) {
Johannes Berge1749612008-10-27 15:59:26 -0700738 printk(KERN_INFO "%s: ether address %pM\n",
739 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
Russell King487fd4e2005-10-02 18:12:03 +0100741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743release:
744 release_region(dev->base_addr, 0x18);
745nodev:
746 free_netdev(dev);
747out:
748 return ret;
749}
750
Russell King50465d02006-01-09 13:59:36 +0000751static struct platform_driver am79c961_driver = {
Russell King37bb30e2005-10-31 17:14:57 +0000752 .probe = am79c961_probe,
Russell King50465d02006-01-09 13:59:36 +0000753 .driver = {
754 .name = "am79c961",
755 },
Russell King37bb30e2005-10-31 17:14:57 +0000756};
757
758static int __init am79c961_init(void)
759{
Russell King50465d02006-01-09 13:59:36 +0000760 return platform_driver_register(&am79c961_driver);
Russell King37bb30e2005-10-31 17:14:57 +0000761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763__initcall(am79c961_init);