blob: 212fe72a190bcca65e9b5e3c57c97110b2bd7662 [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
Joe Perches747e2522011-06-22 20:38:54 +000040#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41
42/*#define DEBUG*/
43/*#define TEST_HITS*/
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/errno.h>
46#include <linux/netdevice.h>
47#include <linux/etherdevice.h>
48#include <linux/module.h>
49#include <linux/stddef.h>
50#include <linux/kernel.h>
51#include <linux/interrupt.h>
52#include <linux/ioport.h>
53#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/init.h>
56#include <linux/crc32.h>
57#include <linux/zorro.h>
58#include <linux/bitops.h>
59
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +020060#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/irq.h>
62#include <asm/amigaints.h>
63#include <asm/amigahw.h>
64
65#include "a2065.h"
66
Joe Perches747e2522011-06-22 20:38:54 +000067/* Transmit/Receive Ring Definitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define LANCE_LOG_TX_BUFFERS (2)
70#define LANCE_LOG_RX_BUFFERS (4)
71
Joe Perches747e2522011-06-22 20:38:54 +000072#define TX_RING_SIZE (1 << LANCE_LOG_TX_BUFFERS)
73#define RX_RING_SIZE (1 << LANCE_LOG_RX_BUFFERS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Joe Perches747e2522011-06-22 20:38:54 +000075#define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
76#define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define PKT_BUF_SIZE (1544)
79#define RX_BUFF_SIZE PKT_BUF_SIZE
80#define TX_BUFF_SIZE PKT_BUF_SIZE
81
Joe Perches747e2522011-06-22 20:38:54 +000082/* Layout of the Lance's RAM Buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84struct lance_init_block {
85 unsigned short mode; /* Pre-set mode (reg. 15) */
86 unsigned char phys_addr[6]; /* Physical ethernet address */
87 unsigned filter[2]; /* Multicast filter. */
88
89 /* Receive and transmit ring base, along with extra bits. */
90 unsigned short rx_ptr; /* receive descriptor addr */
91 unsigned short rx_len; /* receive len and high addr */
92 unsigned short tx_ptr; /* transmit descriptor addr */
93 unsigned short tx_len; /* transmit len and high addr */
Jeff Garzik6aa20a22006-09-13 13:24:59 -040094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
96 struct lance_rx_desc brx_ring[RX_RING_SIZE];
97 struct lance_tx_desc btx_ring[TX_RING_SIZE];
98
Joe Perches747e2522011-06-22 20:38:54 +000099 char rx_buf[RX_RING_SIZE][RX_BUFF_SIZE];
100 char tx_buf[TX_RING_SIZE][TX_BUFF_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Joe Perches747e2522011-06-22 20:38:54 +0000103/* Private Device Data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105struct lance_private {
106 char *name;
107 volatile struct lance_regs *ll;
108 volatile struct lance_init_block *init_block; /* Hosts view */
109 volatile struct lance_init_block *lance_init_block; /* Lance view */
110
111 int rx_new, tx_new;
112 int rx_old, tx_old;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int lance_log_rx_bufs, lance_log_tx_bufs;
115 int rx_ring_mod_mask, tx_ring_mod_mask;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int tpe; /* cable-selection is TPE */
118 int auto_select; /* cable-selection by carrier */
119 unsigned short busmaster_regval;
120
121#ifdef CONFIG_SUNLANCE
122 struct Linux_SBus_DMA *ledma; /* if set this points to ledma and arch=4m */
123 int burst_sizes; /* ledma SBus burst sizes */
124#endif
125 struct timer_list multicast_timer;
Kees Cookc6c52ba2017-10-26 22:54:38 -0700126 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define LANCE_ADDR(x) ((int)(x) & ~0xff000000)
130
131/* Load the CSR registers */
Joe Perches747e2522011-06-22 20:38:54 +0000132static void load_csrs(struct lance_private *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 volatile struct lance_regs *ll = lp->ll;
135 volatile struct lance_init_block *aib = lp->lance_init_block;
Joe Perches747e2522011-06-22 20:38:54 +0000136 int leptr = LANCE_ADDR(aib);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 ll->rap = LE_CSR1;
139 ll->rdp = (leptr & 0xFFFF);
140 ll->rap = LE_CSR2;
141 ll->rdp = leptr >> 16;
142 ll->rap = LE_CSR3;
143 ll->rdp = lp->busmaster_regval;
144
145 /* Point back to csr0 */
146 ll->rap = LE_CSR0;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* Setup the Lance Rx and Tx rings */
Joe Perches747e2522011-06-22 20:38:54 +0000150static void lance_init_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct lance_private *lp = netdev_priv(dev);
153 volatile struct lance_init_block *ib = lp->init_block;
Joe Perches747e2522011-06-22 20:38:54 +0000154 volatile struct lance_init_block *aib = lp->lance_init_block;
155 /* for LANCE_ADDR computations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int leptr;
157 int i;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Lock out other processes while setting up hardware */
160 netif_stop_queue(dev);
161 lp->rx_new = lp->tx_new = 0;
162 lp->rx_old = lp->tx_old = 0;
163
164 ib->mode = 0;
165
166 /* Copy the ethernet address to the lance init block
167 * Note that on the sparc you need to swap the ethernet address.
168 */
Joe Perches747e2522011-06-22 20:38:54 +0000169 ib->phys_addr[0] = dev->dev_addr[1];
170 ib->phys_addr[1] = dev->dev_addr[0];
171 ib->phys_addr[2] = dev->dev_addr[3];
172 ib->phys_addr[3] = dev->dev_addr[2];
173 ib->phys_addr[4] = dev->dev_addr[5];
174 ib->phys_addr[5] = dev->dev_addr[4];
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Setup the Tx ring entries */
Joe Perches747e2522011-06-22 20:38:54 +0000177 netdev_dbg(dev, "TX rings:\n");
178 for (i = 0; i <= 1 << lp->lance_log_tx_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 leptr = LANCE_ADDR(&aib->tx_buf[i][0]);
Joe Perches747e2522011-06-22 20:38:54 +0000180 ib->btx_ring[i].tmd0 = leptr;
181 ib->btx_ring[i].tmd1_hadr = leptr >> 16;
182 ib->btx_ring[i].tmd1_bits = 0;
183 ib->btx_ring[i].length = 0xf000; /* The ones required by tmd2 */
184 ib->btx_ring[i].misc = 0;
185 if (i < 3)
186 netdev_dbg(dev, "%d: 0x%08x\n", i, leptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 /* Setup the Rx ring entries */
Joe Perches747e2522011-06-22 20:38:54 +0000190 netdev_dbg(dev, "RX rings:\n");
191 for (i = 0; i < 1 << lp->lance_log_rx_bufs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 leptr = LANCE_ADDR(&aib->rx_buf[i][0]);
193
Joe Perches747e2522011-06-22 20:38:54 +0000194 ib->brx_ring[i].rmd0 = leptr;
195 ib->brx_ring[i].rmd1_hadr = leptr >> 16;
196 ib->brx_ring[i].rmd1_bits = LE_R1_OWN;
197 ib->brx_ring[i].length = -RX_BUFF_SIZE | 0xf000;
198 ib->brx_ring[i].mblength = 0;
199 if (i < 3)
200 netdev_dbg(dev, "%d: 0x%08x\n", i, leptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 /* Setup the initialization block */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Setup rx descriptor pointer */
206 leptr = LANCE_ADDR(&aib->brx_ring);
207 ib->rx_len = (lp->lance_log_rx_bufs << 13) | (leptr >> 16);
208 ib->rx_ptr = leptr;
Joe Perches747e2522011-06-22 20:38:54 +0000209 netdev_dbg(dev, "RX ptr: %08x\n", leptr);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Setup tx descriptor pointer */
212 leptr = LANCE_ADDR(&aib->btx_ring);
213 ib->tx_len = (lp->lance_log_tx_bufs << 13) | (leptr >> 16);
214 ib->tx_ptr = leptr;
Joe Perches747e2522011-06-22 20:38:54 +0000215 netdev_dbg(dev, "TX ptr: %08x\n", leptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Clear the multicast filter */
Joe Perches747e2522011-06-22 20:38:54 +0000218 ib->filter[0] = 0;
219 ib->filter[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Joe Perches747e2522011-06-22 20:38:54 +0000222static int init_restart_lance(struct lance_private *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 volatile struct lance_regs *ll = lp->ll;
225 int i;
226
227 ll->rap = LE_CSR0;
228 ll->rdp = LE_C0_INIT;
229
230 /* Wait for the lance to complete initialization */
231 for (i = 0; (i < 100) && !(ll->rdp & (LE_C0_ERR | LE_C0_IDON)); i++)
232 barrier();
233 if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
Joe Perches747e2522011-06-22 20:38:54 +0000234 pr_err("unopened after %d ticks, csr0=%04x\n", i, ll->rdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EIO;
236 }
237
238 /* Clear IDON by writing a "1", enable interrupts and start lance */
239 ll->rdp = LE_C0_IDON;
240 ll->rdp = LE_C0_INEA | LE_C0_STRT;
241
242 return 0;
243}
244
Joe Perches747e2522011-06-22 20:38:54 +0000245static int lance_rx(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct lance_private *lp = netdev_priv(dev);
248 volatile struct lance_init_block *ib = lp->init_block;
249 volatile struct lance_regs *ll = lp->ll;
250 volatile struct lance_rx_desc *rd;
251 unsigned char bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253#ifdef TEST_HITS
254 int i;
Joe Perches747e2522011-06-22 20:38:54 +0000255 char buf[RX_RING_SIZE + 1];
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 for (i = 0; i < RX_RING_SIZE; i++) {
Joe Perches747e2522011-06-22 20:38:54 +0000258 char r1_own = ib->brx_ring[i].rmd1_bits & LE_R1_OWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (i == lp->rx_new)
Joe Perches747e2522011-06-22 20:38:54 +0000260 buf[i] = r1_own ? '_' : 'X';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 else
Joe Perches747e2522011-06-22 20:38:54 +0000262 buf[i] = r1_own ? '.' : '1';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Joe Perches747e2522011-06-22 20:38:54 +0000264 buf[RX_RING_SIZE] = 0;
265
266 pr_debug("RxRing TestHits: [%s]\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#endif
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400268
Joe Perches747e2522011-06-22 20:38:54 +0000269 ll->rdp = LE_C0_RINT | LE_C0_INEA;
270 for (rd = &ib->brx_ring[lp->rx_new];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 !((bits = rd->rmd1_bits) & LE_R1_OWN);
Joe Perches747e2522011-06-22 20:38:54 +0000272 rd = &ib->brx_ring[lp->rx_new]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* We got an incomplete frame? */
275 if ((bits & LE_R1_POK) != LE_R1_POK) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700276 dev->stats.rx_over_errors++;
277 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 continue;
279 } else if (bits & LE_R1_ERR) {
280 /* Count only the end frame as a rx error,
281 * not the beginning
282 */
Joe Perches747e2522011-06-22 20:38:54 +0000283 if (bits & LE_R1_BUF)
284 dev->stats.rx_fifo_errors++;
285 if (bits & LE_R1_CRC)
286 dev->stats.rx_crc_errors++;
287 if (bits & LE_R1_OFL)
288 dev->stats.rx_over_errors++;
289 if (bits & LE_R1_FRA)
290 dev->stats.rx_frame_errors++;
291 if (bits & LE_R1_EOP)
292 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 } else {
Al Viro79ea13c2008-01-24 02:06:46 -0800294 int len = (rd->mblength & 0xfff) - 4;
Pradeep A Dalvi1d266432012-02-05 02:49:09 +0000295 struct sk_buff *skb = netdev_alloc_skb(dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Al Viro79ea13c2008-01-24 02:06:46 -0800297 if (!skb) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700298 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rd->mblength = 0;
300 rd->rmd1_bits = LE_R1_OWN;
301 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
302 return 0;
303 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400304
Joe Perches747e2522011-06-22 20:38:54 +0000305 skb_reserve(skb, 2); /* 16 byte align */
306 skb_put(skb, len); /* make room */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700307 skb_copy_to_linear_data(skb,
Joe Perches747e2522011-06-22 20:38:54 +0000308 (unsigned char *)&ib->rx_buf[lp->rx_new][0],
309 len);
310 skb->protocol = eth_type_trans(skb, dev);
311 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700312 dev->stats.rx_packets++;
313 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 /* Return the packet to the pool */
317 rd->mblength = 0;
318 rd->rmd1_bits = LE_R1_OWN;
319 lp->rx_new = (lp->rx_new + 1) & lp->rx_ring_mod_mask;
320 }
321 return 0;
322}
323
Joe Perches747e2522011-06-22 20:38:54 +0000324static int lance_tx(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct lance_private *lp = netdev_priv(dev);
327 volatile struct lance_init_block *ib = lp->init_block;
328 volatile struct lance_regs *ll = lp->ll;
329 volatile struct lance_tx_desc *td;
330 int i, j;
331 int status;
332
333 /* csr0 is 2f3 */
334 ll->rdp = LE_C0_TINT | LE_C0_INEA;
335 /* csr0 is 73 */
336
337 j = lp->tx_old;
338 for (i = j; i != lp->tx_new; i = j) {
Joe Perches747e2522011-06-22 20:38:54 +0000339 td = &ib->btx_ring[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* If we hit a packet not owned by us, stop */
342 if (td->tmd1_bits & LE_T1_OWN)
343 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (td->tmd1_bits & LE_T1_ERR) {
346 status = td->misc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400347
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700348 dev->stats.tx_errors++;
Joe Perches747e2522011-06-22 20:38:54 +0000349 if (status & LE_T3_RTY)
350 dev->stats.tx_aborted_errors++;
351 if (status & LE_T3_LCOL)
352 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (status & LE_T3_CLOS) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700355 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (lp->auto_select) {
357 lp->tpe = 1 - lp->tpe;
Joe Perches747e2522011-06-22 20:38:54 +0000358 netdev_err(dev, "Carrier Lost, trying %s\n",
359 lp->tpe ? "TPE" : "AUI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Stop the lance */
361 ll->rap = LE_CSR0;
362 ll->rdp = LE_C0_STOP;
Joe Perches747e2522011-06-22 20:38:54 +0000363 lance_init_ring(dev);
364 load_csrs(lp);
365 init_restart_lance(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367 }
368 }
369
Joe Perches747e2522011-06-22 20:38:54 +0000370 /* buffer errors and underflows turn off
371 * the transmitter, so restart the adapter
372 */
373 if (status & (LE_T3_BUF | LE_T3_UFL)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700374 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Joe Perches747e2522011-06-22 20:38:54 +0000376 netdev_err(dev, "Tx: ERR_BUF|ERR_UFL, restarting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Stop the lance */
378 ll->rap = LE_CSR0;
379 ll->rdp = LE_C0_STOP;
Joe Perches747e2522011-06-22 20:38:54 +0000380 lance_init_ring(dev);
381 load_csrs(lp);
382 init_restart_lance(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384 }
385 } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
Joe Perches747e2522011-06-22 20:38:54 +0000386 /* So we don't count the packet more than once. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 td->tmd1_bits &= ~(LE_T1_POK);
388
389 /* One collision before packet was sent. */
390 if (td->tmd1_bits & LE_T1_EONE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700391 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* More than one collision, be optimistic. */
394 if (td->tmd1_bits & LE_T1_EMORE)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700395 dev->stats.collisions += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700397 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 j = (j + 1) & lp->tx_ring_mod_mask;
401 }
402 lp->tx_old = j;
403 ll->rdp = LE_C0_TINT | LE_C0_INEA;
404 return 0;
405}
406
Joe Perches747e2522011-06-22 20:38:54 +0000407static int lance_tx_buffs_avail(struct lance_private *lp)
408{
409 if (lp->tx_old <= lp->tx_new)
410 return lp->tx_old + lp->tx_ring_mod_mask - lp->tx_new;
411 return lp->tx_old - lp->tx_new - 1;
412}
413
414static irqreturn_t lance_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Joe Perches43d620c2011-06-16 19:08:06 +0000416 struct net_device *dev = dev_id;
417 struct lance_private *lp = netdev_priv(dev);
418 volatile struct lance_regs *ll = lp->ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int csr0;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ll->rap = LE_CSR0; /* LANCE Controller Status */
422 csr0 = ll->rdp;
423
424 if (!(csr0 & LE_C0_INTR)) /* Check if any interrupt has */
425 return IRQ_NONE; /* been generated by the Lance. */
426
427 /* Acknowledge all the interrupt sources ASAP */
Joe Perches747e2522011-06-22 20:38:54 +0000428 ll->rdp = csr0 & ~(LE_C0_INEA | LE_C0_TDMD | LE_C0_STOP | LE_C0_STRT |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 LE_C0_INIT);
430
Joe Perches747e2522011-06-22 20:38:54 +0000431 if (csr0 & LE_C0_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Clear the error condition */
Joe Perches747e2522011-06-22 20:38:54 +0000433 ll->rdp = LE_C0_BABL | LE_C0_ERR | LE_C0_MISS | LE_C0_INEA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (csr0 & LE_C0_RINT)
Joe Perches747e2522011-06-22 20:38:54 +0000437 lance_rx(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (csr0 & LE_C0_TINT)
Joe Perches747e2522011-06-22 20:38:54 +0000440 lance_tx(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Log misc errors. */
443 if (csr0 & LE_C0_BABL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700444 dev->stats.tx_errors++; /* Tx babble. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (csr0 & LE_C0_MISS)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700446 dev->stats.rx_errors++; /* Missed a Rx frame. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (csr0 & LE_C0_MERR) {
Joe Perches747e2522011-06-22 20:38:54 +0000448 netdev_err(dev, "Bus master arbitration failure, status %04x\n",
449 csr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Restart the chip. */
451 ll->rdp = LE_C0_STRT;
452 }
453
Joe Perches747e2522011-06-22 20:38:54 +0000454 if (netif_queue_stopped(dev) && lance_tx_buffs_avail(lp) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 netif_wake_queue(dev);
456
457 ll->rap = LE_CSR0;
Joe Perches747e2522011-06-22 20:38:54 +0000458 ll->rdp = (LE_C0_BABL | LE_C0_CERR | LE_C0_MISS | LE_C0_MERR |
459 LE_C0_IDON | LE_C0_INEA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return IRQ_HANDLED;
461}
462
Joe Perches747e2522011-06-22 20:38:54 +0000463static int lance_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct lance_private *lp = netdev_priv(dev);
466 volatile struct lance_regs *ll = lp->ll;
467 int ret;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Stop the Lance */
470 ll->rap = LE_CSR0;
471 ll->rdp = LE_C0_STOP;
472
473 /* Install the Interrupt handler */
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700474 ret = request_irq(IRQ_AMIGA_PORTS, lance_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dev->name, dev);
Joe Perches747e2522011-06-22 20:38:54 +0000476 if (ret)
477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Joe Perches747e2522011-06-22 20:38:54 +0000479 load_csrs(lp);
480 lance_init_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 netif_start_queue(dev);
483
Joe Perches747e2522011-06-22 20:38:54 +0000484 return init_restart_lance(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Joe Perches747e2522011-06-22 20:38:54 +0000487static int lance_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct lance_private *lp = netdev_priv(dev);
490 volatile struct lance_regs *ll = lp->ll;
491
492 netif_stop_queue(dev);
493 del_timer_sync(&lp->multicast_timer);
494
495 /* Stop the card */
496 ll->rap = LE_CSR0;
497 ll->rdp = LE_C0_STOP;
498
499 free_irq(IRQ_AMIGA_PORTS, dev);
500 return 0;
501}
502
Joe Perches747e2522011-06-22 20:38:54 +0000503static inline int lance_reset(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct lance_private *lp = netdev_priv(dev);
506 volatile struct lance_regs *ll = lp->ll;
507 int status;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Stop the lance */
510 ll->rap = LE_CSR0;
511 ll->rdp = LE_C0_STOP;
512
Joe Perches747e2522011-06-22 20:38:54 +0000513 load_csrs(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Joe Perches747e2522011-06-22 20:38:54 +0000515 lance_init_ring(dev);
Florian Westphal860e9532016-05-03 16:33:13 +0200516 netif_trans_update(dev); /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 netif_start_queue(dev);
518
Joe Perches747e2522011-06-22 20:38:54 +0000519 status = init_restart_lance(lp);
520 netdev_dbg(dev, "Lance restart=%d\n", status);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return status;
523}
524
525static void lance_tx_timeout(struct net_device *dev)
526{
527 struct lance_private *lp = netdev_priv(dev);
528 volatile struct lance_regs *ll = lp->ll;
529
Joe Perches747e2522011-06-22 20:38:54 +0000530 netdev_err(dev, "transmit timed out, status %04x, reset\n", ll->rdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 lance_reset(dev);
532 netif_wake_queue(dev);
533}
534
Joe Perches747e2522011-06-22 20:38:54 +0000535static netdev_tx_t lance_start_xmit(struct sk_buff *skb,
536 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct lance_private *lp = netdev_priv(dev);
539 volatile struct lance_regs *ll = lp->ll;
540 volatile struct lance_init_block *ib = lp->init_block;
Dave Jonescedc1db2009-03-18 18:17:48 -0700541 int entry, skblen;
Patrick McHardyec634fe2009-07-05 19:23:38 -0700542 int status = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long flags;
544
Dave Jonescedc1db2009-03-18 18:17:48 -0700545 if (skb_padto(skb, ETH_ZLEN))
Patrick McHardyec634fe2009-07-05 19:23:38 -0700546 return NETDEV_TX_OK;
Dave Jonescedc1db2009-03-18 18:17:48 -0700547 skblen = max_t(unsigned, skb->len, ETH_ZLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 local_irq_save(flags);
550
Florian Westphal926f2732016-04-24 21:38:12 +0200551 if (!lance_tx_buffs_avail(lp))
552 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Joe Perches747e2522011-06-22 20:38:54 +0000554#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* dump the packet */
Joe Perchesad361c92009-07-06 13:05:40 -0700556 print_hex_dump(KERN_DEBUG, "skb->data: ", DUMP_PREFIX_NONE,
557 16, 1, skb->data, 64, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
559 entry = lp->tx_new & lp->tx_ring_mod_mask;
Joe Perches747e2522011-06-22 20:38:54 +0000560 ib->btx_ring[entry].length = (-skblen) | 0xf000;
561 ib->btx_ring[entry].misc = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400562
Joe Perches747e2522011-06-22 20:38:54 +0000563 skb_copy_from_linear_data(skb, (void *)&ib->tx_buf[entry][0], skblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Now, give the packet to the lance */
Joe Perches747e2522011-06-22 20:38:54 +0000566 ib->btx_ring[entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 lp->tx_new = (lp->tx_new+1) & lp->tx_ring_mod_mask;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700568 dev->stats.tx_bytes += skblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Joe Perches747e2522011-06-22 20:38:54 +0000570 if (lance_tx_buffs_avail(lp) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 netif_stop_queue(dev);
572
573 /* Kick the lance: transmit now */
574 ll->rdp = LE_C0_INEA | LE_C0_TDMD;
Florian Westphal926f2732016-04-24 21:38:12 +0200575 out_free:
Joe Perches747e2522011-06-22 20:38:54 +0000576 dev_kfree_skb(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 local_irq_restore(flags);
579
580 return status;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* taken from the depca driver */
Joe Perches747e2522011-06-22 20:38:54 +0000584static void lance_load_multicast(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct lance_private *lp = netdev_priv(dev);
587 volatile struct lance_init_block *ib = lp->init_block;
588 volatile u16 *mcast_table = (u16 *)&ib->filter;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000589 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 u32 crc;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* set all multicast bits */
Joe Perches747e2522011-06-22 20:38:54 +0000593 if (dev->flags & IFF_ALLMULTI) {
594 ib->filter[0] = 0xffffffff;
595 ib->filter[1] = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597 }
598 /* clear the multicast filter */
Joe Perches747e2522011-06-22 20:38:54 +0000599 ib->filter[0] = 0;
600 ib->filter[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Add addresses */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000603 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +0000604 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 crc = crc >> 26;
Joe Perches747e2522011-06-22 20:38:54 +0000606 mcast_table[crc >> 4] |= 1 << (crc & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Joe Perches747e2522011-06-22 20:38:54 +0000610static void lance_set_multicast(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct lance_private *lp = netdev_priv(dev);
613 volatile struct lance_init_block *ib = lp->init_block;
614 volatile struct lance_regs *ll = lp->ll;
615
616 if (!netif_running(dev))
617 return;
618
619 if (lp->tx_old != lp->tx_new) {
620 mod_timer(&lp->multicast_timer, jiffies + 4);
621 netif_wake_queue(dev);
622 return;
623 }
624
625 netif_stop_queue(dev);
626
627 ll->rap = LE_CSR0;
628 ll->rdp = LE_C0_STOP;
Joe Perches747e2522011-06-22 20:38:54 +0000629 lance_init_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (dev->flags & IFF_PROMISC) {
632 ib->mode |= LE_MO_PROM;
633 } else {
634 ib->mode &= ~LE_MO_PROM;
Joe Perches747e2522011-06-22 20:38:54 +0000635 lance_load_multicast(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Joe Perches747e2522011-06-22 20:38:54 +0000637 load_csrs(lp);
638 init_restart_lance(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 netif_wake_queue(dev);
640}
641
Kees Cookc6c52ba2017-10-26 22:54:38 -0700642static void lance_set_multicast_retry(struct timer_list *t)
643{
644 struct lance_private *lp = from_timer(lp, t, multicast_timer);
645
646 lance_set_multicast(lp->dev);
647}
648
Bill Pemberton0cb05682012-12-03 09:23:54 -0500649static int a2065_init_one(struct zorro_dev *z,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000650 const struct zorro_device_id *ent);
Bill Pemberton0cb05682012-12-03 09:23:54 -0500651static void a2065_remove_one(struct zorro_dev *z);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653
Arvind Yadav153890b2017-08-22 23:41:12 +0530654static const struct zorro_device_id a2065_zorro_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 { ZORRO_PROD_CBM_A2065_1 },
656 { ZORRO_PROD_CBM_A2065_2 },
657 { ZORRO_PROD_AMERISTAR_A2065 },
658 { 0 }
659};
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100660MODULE_DEVICE_TABLE(zorro, a2065_zorro_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662static struct zorro_driver a2065_driver = {
663 .name = "a2065",
664 .id_table = a2065_zorro_tbl,
665 .probe = a2065_init_one,
Bill Pemberton0cb05682012-12-03 09:23:54 -0500666 .remove = a2065_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667};
668
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000669static const struct net_device_ops lance_netdev_ops = {
670 .ndo_open = lance_open,
671 .ndo_stop = lance_close,
672 .ndo_start_xmit = lance_start_xmit,
673 .ndo_tx_timeout = lance_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000674 .ndo_set_rx_mode = lance_set_multicast,
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000675 .ndo_validate_addr = eth_validate_addr,
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000676 .ndo_set_mac_address = eth_mac_addr,
677};
678
Bill Pemberton0cb05682012-12-03 09:23:54 -0500679static int a2065_init_one(struct zorro_dev *z,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000680 const struct zorro_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct net_device *dev;
683 struct lance_private *priv;
Joe Perches747e2522011-06-22 20:38:54 +0000684 unsigned long board = z->resource.start;
685 unsigned long base_addr = board + A2065_LANCE;
686 unsigned long mem_start = board + A2065_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct resource *r1, *r2;
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +0200688 u32 serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int err;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 r1 = request_mem_region(base_addr, sizeof(struct lance_regs),
692 "Am7990");
693 if (!r1)
694 return -EBUSY;
695 r2 = request_mem_region(mem_start, A2065_RAM_SIZE, "RAM");
696 if (!r2) {
Julia Lawalle6937ee2011-03-22 07:15:23 +0000697 release_mem_region(base_addr, sizeof(struct lance_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -EBUSY;
699 }
700
701 dev = alloc_etherdev(sizeof(struct lance_private));
702 if (dev == NULL) {
Julia Lawalle6937ee2011-03-22 07:15:23 +0000703 release_mem_region(base_addr, sizeof(struct lance_regs));
704 release_mem_region(mem_start, A2065_RAM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 priv = netdev_priv(dev);
709
710 r1->name = dev->name;
711 r2->name = dev->name;
712
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +0200713 serial = be32_to_cpu(z->rom.er_SerialNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dev->dev_addr[0] = 0x00;
715 if (z->id != ZORRO_PROD_AMERISTAR_A2065) { /* Commodore */
716 dev->dev_addr[1] = 0x80;
717 dev->dev_addr[2] = 0x10;
718 } else { /* Ameristar */
719 dev->dev_addr[1] = 0x00;
720 dev->dev_addr[2] = 0x9f;
721 }
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +0200722 dev->dev_addr[3] = (serial >> 16) & 0xff;
723 dev->dev_addr[4] = (serial >> 8) & 0xff;
724 dev->dev_addr[5] = serial & 0xff;
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +0100725 dev->base_addr = (unsigned long)ZTWO_VADDR(base_addr);
726 dev->mem_start = (unsigned long)ZTWO_VADDR(mem_start);
Joe Perches747e2522011-06-22 20:38:54 +0000727 dev->mem_end = dev->mem_start + A2065_RAM_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 priv->ll = (volatile struct lance_regs *)dev->base_addr;
730 priv->init_block = (struct lance_init_block *)dev->mem_start;
731 priv->lance_init_block = (struct lance_init_block *)A2065_RAM;
732 priv->auto_select = 0;
733 priv->busmaster_regval = LE_C3_BSWP;
734
735 priv->lance_log_rx_bufs = LANCE_LOG_RX_BUFFERS;
736 priv->lance_log_tx_bufs = LANCE_LOG_TX_BUFFERS;
737 priv->rx_ring_mod_mask = RX_RING_MOD_MASK;
738 priv->tx_ring_mod_mask = TX_RING_MOD_MASK;
Kees Cookc6c52ba2017-10-26 22:54:38 -0700739 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Alexander Beregalov444f1a92009-04-14 18:30:21 +0000741 dev->netdev_ops = &lance_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev->watchdog_timeo = 5*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 dev->dma = 0;
744
Kees Cookc6c52ba2017-10-26 22:54:38 -0700745 timer_setup(&priv->multicast_timer, lance_set_multicast_retry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 err = register_netdev(dev);
748 if (err) {
Julia Lawalle6937ee2011-03-22 07:15:23 +0000749 release_mem_region(base_addr, sizeof(struct lance_regs));
750 release_mem_region(mem_start, A2065_RAM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 free_netdev(dev);
752 return err;
753 }
754 zorro_set_drvdata(z, dev);
755
Joe Perches747e2522011-06-22 20:38:54 +0000756 netdev_info(dev, "A2065 at 0x%08lx, Ethernet Address %pM\n",
757 board, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return 0;
760}
761
762
Bill Pemberton0cb05682012-12-03 09:23:54 -0500763static void a2065_remove_one(struct zorro_dev *z)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct net_device *dev = zorro_get_drvdata(z);
766
767 unregister_netdev(dev);
768 release_mem_region(ZTWO_PADDR(dev->base_addr),
769 sizeof(struct lance_regs));
770 release_mem_region(ZTWO_PADDR(dev->mem_start), A2065_RAM_SIZE);
771 free_netdev(dev);
772}
773
774static int __init a2065_init_module(void)
775{
Bjorn Helgaas33d86752006-03-25 03:07:20 -0800776 return zorro_register_driver(&a2065_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779static void __exit a2065_cleanup_module(void)
780{
781 zorro_unregister_driver(&a2065_driver);
782}
783
784module_init(a2065_init_module);
785module_exit(a2065_cleanup_module);
786
787MODULE_LICENSE("GPL");