blob: a8f0512bad38a0035d92f934670f2765a47a5537 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Amiga Linux/68k A2065 Ethernet Driver
3 *
4 * (C) Copyright 1995-2003 by Geert Uytterhoeven <geert@linux-m68k.org>
5 *
6 * Fixes and tips by:
7 * - Janos Farkas (CHEXUM@sparta.banki.hu)
8 * - Jes Degn Soerensen (jds@kom.auc.dk)
9 * - Matt Domsch (Matt_Domsch@dell.com)
10 *
11 * ----------------------------------------------------------------------------
12 *
13 * This program is based on
14 *
15 * ariadne.?: Amiga Linux/68k Ariadne Ethernet Driver
16 * (C) Copyright 1995 by Geert Uytterhoeven,
17 * Peter De Schrijver
18 *
19 * lance.c: An AMD LANCE ethernet driver for linux.
20 * Written 1993-94 by Donald Becker.
21 *
22 * Am79C960: PCnet(tm)-ISA Single-Chip Ethernet Controller
23 * Advanced Micro Devices
24 * Publication #16907, Rev. B, Amendment/0, May 1994
25 *
26 * ----------------------------------------------------------------------------
27 *
28 * This file is subject to the terms and conditions of the GNU General Public
29 * License. See the file COPYING in the main directory of the Linux
30 * distribution for more details.
31 *
32 * ----------------------------------------------------------------------------
33 *
34 * The A2065 is a Zorro-II board made by Commodore/Ameristar. It contains:
35 *
36 * - an Am7990 Local Area Network Controller for Ethernet (LANCE) with
37 * both 10BASE-2 (thin coax) and AUI (DB-15) connectors
38 */
39
40#include <linux/errno.h>
41#include <linux/netdevice.h>
42#include <linux/etherdevice.h>
43#include <linux/module.h>
44#include <linux/stddef.h>
45#include <linux/kernel.h>
46#include <linux/interrupt.h>
47#include <linux/ioport.h>
48#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/init.h>
51#include <linux/crc32.h>
52#include <linux/zorro.h>
53#include <linux/bitops.h>
54
55#include <asm/irq.h>
56#include <asm/amigaints.h>
57#include <asm/amigahw.h>
58
59#include "a2065.h"
60
61
62 /*
63 * Transmit/Receive Ring Definitions
64 */
65
66#define LANCE_LOG_TX_BUFFERS (2)
67#define LANCE_LOG_RX_BUFFERS (4)
68
69#define TX_RING_SIZE (1<<LANCE_LOG_TX_BUFFERS)
70#define RX_RING_SIZE (1<<LANCE_LOG_RX_BUFFERS)
71
72#define TX_RING_MOD_MASK (TX_RING_SIZE-1)
73#define RX_RING_MOD_MASK (RX_RING_SIZE-1)
74
75#define PKT_BUF_SIZE (1544)
76#define RX_BUFF_SIZE PKT_BUF_SIZE
77#define TX_BUFF_SIZE PKT_BUF_SIZE
78
79
80 /*
81 * Layout of the Lance's RAM Buffer
82 */
83
84
85struct lance_init_block {
86 unsigned short mode; /* Pre-set mode (reg. 15) */
87 unsigned char phys_addr[6]; /* Physical ethernet address */
88 unsigned filter[2]; /* Multicast filter. */
89
90 /* Receive and transmit ring base, along with extra bits. */
91 unsigned short rx_ptr; /* receive descriptor addr */
92 unsigned short rx_len; /* receive len and high addr */
93 unsigned short tx_ptr; /* transmit descriptor addr */
94 unsigned short tx_len; /* transmit len and high addr */
Jeff Garzik6aa20a22006-09-13 13:24:59 -040095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
97 struct lance_rx_desc brx_ring[RX_RING_SIZE];
98 struct lance_tx_desc btx_ring[TX_RING_SIZE];
99
100 char rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
101 char tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
102};
103
104
105 /*
106 * Private Device Data
107 */
108
109struct lance_private {
110 char *name;
111 volatile struct lance_regs *ll;
112 volatile struct lance_init_block *init_block; /* Hosts view */
113 volatile struct lance_init_block *lance_init_block; /* Lance view */
114
115 int rx_new, tx_new;
116 int rx_old, tx_old;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int lance_log_rx_bufs, lance_log_tx_bufs;
119 int rx_ring_mod_mask, tx_ring_mod_mask;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int tpe; /* cable-selection is TPE */
122 int auto_select; /* cable-selection by carrier */
123 unsigned short busmaster_regval;
124
125#ifdef CONFIG_SUNLANCE
126 struct Linux_SBus_DMA *ledma; /* if set this points to ledma and arch=4m */
127 int burst_sizes; /* ledma SBus burst sizes */
128#endif
129 struct timer_list multicast_timer;
130};
131
132#define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
133 lp->tx_old+lp->tx_ring_mod_mask-lp->tx_new:\
134 lp->tx_old - lp->tx_new-1)
135
136
137#define LANCE_ADDR(x) ((int)(x) & ~0xff000000)
138
139/* Load the CSR registers */
140static void load_csrs (struct lance_private *lp)
141{
142 volatile struct lance_regs *ll = lp->ll;
143 volatile struct lance_init_block *aib = lp->lance_init_block;
144 int leptr;
145
146 leptr = LANCE_ADDR (aib);
147
148 ll->rap = LE_CSR1;
149 ll->rdp = (leptr & 0xFFFF);
150 ll->rap = LE_CSR2;
151 ll->rdp = leptr >> 16;
152 ll->rap = LE_CSR3;
153 ll->rdp = lp->busmaster_regval;
154
155 /* Point back to csr0 */
156 ll->rap = LE_CSR0;
157}
158
159#define ZERO 0
160
161/* Setup the Lance Rx and Tx rings */
162static void lance_init_ring (struct net_device *dev)
163{
164 struct lance_private *lp = netdev_priv(dev);
165 volatile struct lance_init_block *ib = lp->init_block;
166 volatile struct lance_init_block *aib; /* for LANCE_ADDR computations */
167 int leptr;
168 int i;
169
170 aib = lp->lance_init_block;
171
172 /* Lock out other processes while setting up hardware */
173 netif_stop_queue(dev);
174 lp->rx_new = lp->tx_new = 0;
175 lp->rx_old = lp->tx_old = 0;
176
177 ib->mode = 0;
178
179 /* Copy the ethernet address to the lance init block
180 * Note that on the sparc you need to swap the ethernet address.
181 */
182 ib->phys_addr [0] = dev->dev_addr [1];
183 ib->phys_addr [1] = dev->dev_addr [0];
184 ib->phys_addr [2] = dev->dev_addr [3];
185 ib->phys_addr [3] = dev->dev_addr [2];
186 ib->phys_addr [4] = dev->dev_addr [5];
187 ib->phys_addr [5] = dev->dev_addr [4];
188
189 if (ZERO)
190 printk(KERN_DEBUG "TX rings:\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* Setup the Tx ring entries */
193 for (i = 0; i <= (1<<lp->lance_log_tx_bufs); i++) {
194 leptr = LANCE_ADDR(&aib->tx_buf[i][0]);
195 ib->btx_ring [i].tmd0 = leptr;
196 ib->btx_ring [i].tmd1_hadr = leptr >> 16;
197 ib->btx_ring [i].tmd1_bits = 0;
198 ib->btx_ring [i].length = 0xf000; /* The ones required by tmd2 */
199 ib->btx_ring [i].misc = 0;
200 if (i < 3 && ZERO)
201 printk(KERN_DEBUG "%d: 0x%8.8x\n", i, leptr);
202 }
203
204 /* Setup the Rx ring entries */
205 if (ZERO)
206 printk(KERN_DEBUG "RX rings:\n");
207 for (i = 0; i < (1<<lp->lance_log_rx_bufs); i++) {
208 leptr = LANCE_ADDR(&aib->rx_buf[i][0]);
209
210 ib->brx_ring [i].rmd0 = leptr;
211 ib->brx_ring [i].rmd1_hadr = leptr >> 16;
212 ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
213 ib->brx_ring [i].length = -RX_BUFF_SIZE | 0xf000;
214 ib->brx_ring [i].mblength = 0;
215 if (i < 3 && ZERO)
216 printk(KERN_DEBUG "%d: 0x%8.8x\n", i, leptr);
217 }
218
219 /* Setup the initialization block */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Setup rx descriptor pointer */
222 leptr = LANCE_ADDR(&aib->brx_ring);
223 ib->rx_len = (lp->lance_log_rx_bufs << 13) | (leptr >> 16);
224 ib->rx_ptr = leptr;
225 if (ZERO)
226 printk(KERN_DEBUG "RX ptr: %8.8x\n", leptr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* Setup tx descriptor pointer */
229 leptr = LANCE_ADDR(&aib->btx_ring);
230 ib->tx_len = (lp->lance_log_tx_bufs << 13) | (leptr >> 16);
231 ib->tx_ptr = leptr;
232 if (ZERO)
233 printk(KERN_DEBUG "TX ptr: %8.8x\n", leptr);
234
235 /* Clear the multicast filter */
236 ib->filter [0] = 0;
237 ib->filter [1] = 0;
238}
239
240static int init_restart_lance (struct lance_private *lp)
241{
242 volatile struct lance_regs *ll = lp->ll;
243 int i;
244
245 ll->rap = LE_CSR0;
246 ll->rdp = LE_C0_INIT;
247
248 /* Wait for the lance to complete initialization */
249 for (i = 0; (i < 100) && !(ll->rdp & (LE_C0_ERR | LE_C0_IDON)); i++)
250 barrier();
251 if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
252 printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n",
253 i, ll->rdp);
254 return -EIO;
255 }
256
257 /* Clear IDON by writing a "1", enable interrupts and start lance */
258 ll->rdp = LE_C0_IDON;
259 ll->rdp = LE_C0_INEA | LE_C0_STRT;
260
261 return 0;
262}
263
264static int lance_rx (struct net_device *dev)
265{
266 struct lance_private *lp = netdev_priv(dev);
267 volatile struct lance_init_block *ib = lp->init_block;
268 volatile struct lance_regs *ll = lp->ll;
269 volatile struct lance_rx_desc *rd;
270 unsigned char bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272#ifdef TEST_HITS
273 int i;
274 printk(KERN_DEBUG "[");
275 for (i = 0; i < RX_RING_SIZE; i++) {
276 if (i == lp->rx_new)
277 printk ("%s",
278 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "_" : "X");
279 else
280 printk ("%s",
281 ib->brx_ring [i].rmd1_bits & LE_R1_OWN ? "." : "1");
282 }
283 printk ("]\n");
284#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ll->rdp = LE_C0_RINT|LE_C0_INEA;
287 for (rd = &ib->brx_ring [lp->rx_new];
288 !((bits = rd->rmd1_bits) & LE_R1_OWN);
289 rd = &ib->brx_ring [lp->rx_new]) {
290
291 /* We got an incomplete frame? */
292 if ((bits & LE_R1_POK) != LE_R1_POK) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700293 dev->stats.rx_over_errors++;
294 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 continue;
296 } else if (bits & LE_R1_ERR) {
297 /* Count only the end frame as a rx error,
298 * not the beginning
299 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700300 if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
301 if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
302 if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
303 if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
304 if (bits & LE_R1_EOP) dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 } else {
Al Viro79ea13c2008-01-24 02:06:46 -0800306 int len = (rd->mblength & 0xfff) - 4;
307 struct sk_buff *skb = dev_alloc_skb (len+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Al Viro79ea13c2008-01-24 02:06:46 -0800309 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 printk(KERN_WARNING "%s: Memory squeeze, "
311 "deferring packet.\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700312 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 rd->mblength = 0;
314 rd->rmd1_bits = LE_R1_OWN;
315 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
316 return 0;
317 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 skb_reserve (skb, 2); /* 16 byte align */
320 skb_put (skb, len); /* make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700321 skb_copy_to_linear_data(skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 (unsigned char *)&(ib->rx_buf [lp->rx_new][0]),
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700323 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 skb->protocol = eth_type_trans (skb, dev);
325 netif_rx (skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700326 dev->stats.rx_packets++;
327 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 /* Return the packet to the pool */
331 rd->mblength = 0;
332 rd->rmd1_bits = LE_R1_OWN;
333 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
334 }
335 return 0;
336}
337
338static int lance_tx (struct net_device *dev)
339{
340 struct lance_private *lp = netdev_priv(dev);
341 volatile struct lance_init_block *ib = lp->init_block;
342 volatile struct lance_regs *ll = lp->ll;
343 volatile struct lance_tx_desc *td;
344 int i, j;
345 int status;
346
347 /* csr0 is 2f3 */
348 ll->rdp = LE_C0_TINT | LE_C0_INEA;
349 /* csr0 is 73 */
350
351 j = lp->tx_old;
352 for (i = j; i != lp->tx_new; i = j) {
353 td = &ib->btx_ring [i];
354
355 /* If we hit a packet not owned by us, stop */
356 if (td->tmd1_bits & LE_T1_OWN)
357 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (td->tmd1_bits & LE_T1_ERR) {
360 status = td->misc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400361
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700362 dev->stats.tx_errors++;
363 if (status & LE_T3_RTY) dev->stats.tx_aborted_errors++;
364 if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (status & LE_T3_CLOS) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700367 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (lp->auto_select) {
369 lp->tpe = 1 - lp->tpe;
370 printk(KERN_ERR "%s: Carrier Lost, "
371 "trying %s\n", dev->name,
372 lp->tpe?"TPE":"AUI");
373 /* Stop the lance */
374 ll->rap = LE_CSR0;
375 ll->rdp = LE_C0_STOP;
376 lance_init_ring (dev);
377 load_csrs (lp);
378 init_restart_lance (lp);
379 return 0;
380 }
381 }
382
383 /* buffer errors and underflows turn off the transmitter */
384 /* Restart the adapter */
385 if (status & (LE_T3_BUF|LE_T3_UFL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700386 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, "
389 "restarting\n", dev->name);
390 /* Stop the lance */
391 ll->rap = LE_CSR0;
392 ll->rdp = LE_C0_STOP;
393 lance_init_ring (dev);
394 load_csrs (lp);
395 init_restart_lance (lp);
396 return 0;
397 }
398 } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
399 /*
400 * So we don't count the packet more than once.
401 */
402 td->tmd1_bits &= ~(LE_T1_POK);
403
404 /* One collision before packet was sent. */
405 if (td->tmd1_bits & LE_T1_EONE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700406 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* More than one collision, be optimistic. */
409 if (td->tmd1_bits & LE_T1_EMORE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700410 dev->stats.collisions += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700412 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 j = (j + 1) & lp->tx_ring_mod_mask;
416 }
417 lp->tx_old = j;
418 ll->rdp = LE_C0_TINT | LE_C0_INEA;
419 return 0;
420}
421
David Howells7d12e782006-10-05 14:55:46 +0100422static irqreturn_t lance_interrupt (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct net_device *dev;
425 struct lance_private *lp;
426 volatile struct lance_regs *ll;
427 int csr0;
428
429 dev = (struct net_device *) dev_id;
430
431 lp = netdev_priv(dev);
432 ll = lp->ll;
433
434 ll->rap = LE_CSR0; /* LANCE Controller Status */
435 csr0 = ll->rdp;
436
437 if (!(csr0 & LE_C0_INTR)) /* Check if any interrupt has */
438 return IRQ_NONE; /* been generated by the Lance. */
439
440 /* Acknowledge all the interrupt sources ASAP */
441 ll->rdp = csr0 & ~(LE_C0_INEA|LE_C0_TDMD|LE_C0_STOP|LE_C0_STRT|
442 LE_C0_INIT);
443
444 if ((csr0 & LE_C0_ERR)) {
445 /* Clear the error condition */
446 ll->rdp = LE_C0_BABL|LE_C0_ERR|LE_C0_MISS|LE_C0_INEA;
447 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (csr0 & LE_C0_RINT)
450 lance_rx (dev);
451
452 if (csr0 & LE_C0_TINT)
453 lance_tx (dev);
454
455 /* Log misc errors. */
456 if (csr0 & LE_C0_BABL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700457 dev->stats.tx_errors++; /* Tx babble. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (csr0 & LE_C0_MISS)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700459 dev->stats.rx_errors++; /* Missed a Rx frame. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (csr0 & LE_C0_MERR) {
461 printk(KERN_ERR "%s: Bus master arbitration failure, status "
462 "%4.4x.\n", dev->name, csr0);
463 /* Restart the chip. */
464 ll->rdp = LE_C0_STRT;
465 }
466
467 if (netif_queue_stopped(dev) && TX_BUFFS_AVAIL > 0)
468 netif_wake_queue(dev);
469
470 ll->rap = LE_CSR0;
471 ll->rdp = LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_MERR|
472 LE_C0_IDON|LE_C0_INEA;
473 return IRQ_HANDLED;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int lance_open (struct net_device *dev)
477{
478 struct lance_private *lp = netdev_priv(dev);
479 volatile struct lance_regs *ll = lp->ll;
480 int ret;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Stop the Lance */
483 ll->rap = LE_CSR0;
484 ll->rdp = LE_C0_STOP;
485
486 /* Install the Interrupt handler */
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700487 ret = request_irq(IRQ_AMIGA_PORTS, lance_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 dev->name, dev);
489 if (ret) return ret;
490
491 load_csrs (lp);
492 lance_init_ring (dev);
493
494 netif_start_queue(dev);
495
496 return init_restart_lance (lp);
497}
498
499static int lance_close (struct net_device *dev)
500{
501 struct lance_private *lp = netdev_priv(dev);
502 volatile struct lance_regs *ll = lp->ll;
503
504 netif_stop_queue(dev);
505 del_timer_sync(&lp->multicast_timer);
506
507 /* Stop the card */
508 ll->rap = LE_CSR0;
509 ll->rdp = LE_C0_STOP;
510
511 free_irq(IRQ_AMIGA_PORTS, dev);
512 return 0;
513}
514
515static inline int lance_reset (struct net_device *dev)
516{
517 struct lance_private *lp = netdev_priv(dev);
518 volatile struct lance_regs *ll = lp->ll;
519 int status;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Stop the lance */
522 ll->rap = LE_CSR0;
523 ll->rdp = LE_C0_STOP;
524
525 load_csrs (lp);
526
527 lance_init_ring (dev);
528 dev->trans_start = jiffies;
529 netif_start_queue(dev);
530
531 status = init_restart_lance (lp);
532#ifdef DEBUG_DRIVER
533 printk(KERN_DEBUG "Lance restart=%d\n", status);
534#endif
535 return status;
536}
537
538static void lance_tx_timeout(struct net_device *dev)
539{
540 struct lance_private *lp = netdev_priv(dev);
541 volatile struct lance_regs *ll = lp->ll;
542
543 printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
544 dev->name, ll->rdp);
545 lance_reset(dev);
546 netif_wake_queue(dev);
547}
548
Stephen Hemminger613573252009-08-31 19:50:58 +0000549static netdev_tx_t lance_start_xmit (struct sk_buff *skb,
550 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct lance_private *lp = netdev_priv(dev);
553 volatile struct lance_regs *ll = lp->ll;
554 volatile struct lance_init_block *ib = lp->init_block;
Dave Jonescedc1db2009-03-18 18:17:48 -0700555 int entry, skblen;
Patrick McHardyec634fe2009-07-05 19:23:38 -0700556 int status = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned long flags;
558
Dave Jonescedc1db2009-03-18 18:17:48 -0700559 if (skb_padto(skb, ETH_ZLEN))
Patrick McHardyec634fe2009-07-05 19:23:38 -0700560 return NETDEV_TX_OK;
Dave Jonescedc1db2009-03-18 18:17:48 -0700561 skblen = max_t(unsigned, skb->len, ETH_ZLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 local_irq_save(flags);
564
565 if (!TX_BUFFS_AVAIL){
566 local_irq_restore(flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000567 return NETDEV_TX_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570#ifdef DEBUG_DRIVER
571 /* dump the packet */
Joe Perchesad361c92009-07-06 13:05:40 -0700572 print_hex_dump(KERN_DEBUG, "skb->data: ", DUMP_PREFIX_NONE,
573 16, 1, skb->data, 64, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
575 entry = lp->tx_new & lp->tx_ring_mod_mask;
Dave Jonescedc1db2009-03-18 18:17:48 -0700576 ib->btx_ring [entry].length = (-skblen) | 0xf000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ib->btx_ring [entry].misc = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400578
Geert Uytterhoevencfa08bb2007-05-01 22:33:05 +0200579 skb_copy_from_linear_data(skb, (void *)&ib->tx_buf [entry][0], skblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Now, give the packet to the lance */
582 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
583 lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700584 dev->stats.tx_bytes += skblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (TX_BUFFS_AVAIL <= 0)
587 netif_stop_queue(dev);
588
589 /* Kick the lance: transmit now */
590 ll->rdp = LE_C0_INEA | LE_C0_TDMD;
591 dev->trans_start = jiffies;
592 dev_kfree_skb (skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 local_irq_restore(flags);
595
596 return status;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/* taken from the depca driver */
600static void lance_load_multicast (struct net_device *dev)
601{
602 struct lance_private *lp = netdev_priv(dev);
603 volatile struct lance_init_block *ib = lp->init_block;
604 volatile u16 *mcast_table = (u16 *)&ib->filter;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000605 struct dev_mc_list *dmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 char *addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 u32 crc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* set all multicast bits */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400610 if (dev->flags & IFF_ALLMULTI){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ib->filter [0] = 0xffffffff;
612 ib->filter [1] = 0xffffffff;
613 return;
614 }
615 /* clear the multicast filter */
616 ib->filter [0] = 0;
617 ib->filter [1] = 0;
618
619 /* Add addresses */
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000620 netdev_for_each_mc_addr(dmi, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 addrs = dmi->dmi_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* multicast address? */
624 if (!(*addrs & 1))
625 continue;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 crc = ether_crc_le(6, addrs);
628 crc = crc >> 26;
629 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
630 }
631 return;
632}
633
634static void lance_set_multicast (struct net_device *dev)
635{
636 struct lance_private *lp = netdev_priv(dev);
637 volatile struct lance_init_block *ib = lp->init_block;
638 volatile struct lance_regs *ll = lp->ll;
639
640 if (!netif_running(dev))
641 return;
642
643 if (lp->tx_old != lp->tx_new) {
644 mod_timer(&lp->multicast_timer, jiffies + 4);
645 netif_wake_queue(dev);
646 return;
647 }
648
649 netif_stop_queue(dev);
650
651 ll->rap = LE_CSR0;
652 ll->rdp = LE_C0_STOP;
653 lance_init_ring (dev);
654
655 if (dev->flags & IFF_PROMISC) {
656 ib->mode |= LE_MO_PROM;
657 } else {
658 ib->mode &= ~LE_MO_PROM;
659 lance_load_multicast (dev);
660 }
661 load_csrs (lp);
662 init_restart_lance (lp);
663 netif_wake_queue(dev);
664}
665
666static int __devinit a2065_init_one(struct zorro_dev *z,
667 const struct zorro_device_id *ent);
668static void __devexit a2065_remove_one(struct zorro_dev *z);
669
670
671static struct zorro_device_id a2065_zorro_tbl[] __devinitdata = {
672 { ZORRO_PROD_CBM_A2065_1 },
673 { ZORRO_PROD_CBM_A2065_2 },
674 { ZORRO_PROD_AMERISTAR_A2065 },
675 { 0 }
676};
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100677MODULE_DEVICE_TABLE(zorro, a2065_zorro_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679static struct zorro_driver a2065_driver = {
680 .name = "a2065",
681 .id_table = a2065_zorro_tbl,
682 .probe = a2065_init_one,
683 .remove = __devexit_p(a2065_remove_one),
684};
685
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000686static const struct net_device_ops lance_netdev_ops = {
687 .ndo_open = lance_open,
688 .ndo_stop = lance_close,
689 .ndo_start_xmit = lance_start_xmit,
690 .ndo_tx_timeout = lance_tx_timeout,
691 .ndo_set_multicast_list = lance_set_multicast,
692 .ndo_validate_addr = eth_validate_addr,
693 .ndo_change_mtu = eth_change_mtu,
694 .ndo_set_mac_address = eth_mac_addr,
695};
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static int __devinit a2065_init_one(struct zorro_dev *z,
698 const struct zorro_device_id *ent)
699{
700 struct net_device *dev;
701 struct lance_private *priv;
702 unsigned long board, base_addr, mem_start;
703 struct resource *r1, *r2;
704 int err;
705
706 board = z->resource.start;
707 base_addr = board+A2065_LANCE;
708 mem_start = board+A2065_RAM;
709
710 r1 = request_mem_region(base_addr, sizeof(struct lance_regs),
711 "Am7990");
712 if (!r1)
713 return -EBUSY;
714 r2 = request_mem_region(mem_start, A2065_RAM_SIZE, "RAM");
715 if (!r2) {
716 release_resource(r1);
717 return -EBUSY;
718 }
719
720 dev = alloc_etherdev(sizeof(struct lance_private));
721 if (dev == NULL) {
722 release_resource(r1);
723 release_resource(r2);
724 return -ENOMEM;
725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 priv = netdev_priv(dev);
728
729 r1->name = dev->name;
730 r2->name = dev->name;
731
732 dev->dev_addr[0] = 0x00;
733 if (z->id != ZORRO_PROD_AMERISTAR_A2065) { /* Commodore */
734 dev->dev_addr[1] = 0x80;
735 dev->dev_addr[2] = 0x10;
736 } else { /* Ameristar */
737 dev->dev_addr[1] = 0x00;
738 dev->dev_addr[2] = 0x9f;
739 }
740 dev->dev_addr[3] = (z->rom.er_SerialNumber>>16) & 0xff;
741 dev->dev_addr[4] = (z->rom.er_SerialNumber>>8) & 0xff;
742 dev->dev_addr[5] = z->rom.er_SerialNumber & 0xff;
743 dev->base_addr = ZTWO_VADDR(base_addr);
744 dev->mem_start = ZTWO_VADDR(mem_start);
745 dev->mem_end = dev->mem_start+A2065_RAM_SIZE;
746
747 priv->ll = (volatile struct lance_regs *)dev->base_addr;
748 priv->init_block = (struct lance_init_block *)dev->mem_start;
749 priv->lance_init_block = (struct lance_init_block *)A2065_RAM;
750 priv->auto_select = 0;
751 priv->busmaster_regval = LE_C3_BSWP;
752
753 priv->lance_log_rx_bufs = LANCE_LOG_RX_BUFFERS;
754 priv->lance_log_tx_bufs = LANCE_LOG_TX_BUFFERS;
755 priv->rx_ring_mod_mask = RX_RING_MOD_MASK;
756 priv->tx_ring_mod_mask = TX_RING_MOD_MASK;
757
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000758 dev->netdev_ops = &lance_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dev->watchdog_timeo = 5*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev->dma = 0;
761
762 init_timer(&priv->multicast_timer);
763 priv->multicast_timer.data = (unsigned long) dev;
764 priv->multicast_timer.function =
765 (void (*)(unsigned long)) &lance_set_multicast;
766
767 err = register_netdev(dev);
768 if (err) {
769 release_resource(r1);
770 release_resource(r2);
771 free_netdev(dev);
772 return err;
773 }
774 zorro_set_drvdata(z, dev);
775
776 printk(KERN_INFO "%s: A2065 at 0x%08lx, Ethernet Address "
Johannes Berge1749612008-10-27 15:59:26 -0700777 "%pM\n", dev->name, board, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 return 0;
780}
781
782
783static void __devexit a2065_remove_one(struct zorro_dev *z)
784{
785 struct net_device *dev = zorro_get_drvdata(z);
786
787 unregister_netdev(dev);
788 release_mem_region(ZTWO_PADDR(dev->base_addr),
789 sizeof(struct lance_regs));
790 release_mem_region(ZTWO_PADDR(dev->mem_start), A2065_RAM_SIZE);
791 free_netdev(dev);
792}
793
794static int __init a2065_init_module(void)
795{
Bjorn Helgaas33d86752006-03-25 03:07:20 -0800796 return zorro_register_driver(&a2065_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static void __exit a2065_cleanup_module(void)
800{
801 zorro_unregister_driver(&a2065_driver);
802}
803
804module_init(a2065_init_module);
805module_exit(a2065_cleanup_module);
806
807MODULE_LICENSE("GPL");