Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / acorn / net / ether1.c
blob16baa4718194f65a8d667d6c458e3fc9f57f046b
1 /*
2 * linux/drivers/acorn/net/ether1.c
4 * Copyright (C) 1996-2000 Russell King
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.
10 * Acorn ether1 driver (82586 chip) for Acorn machines
12 * We basically keep two queues in the cards memory - one for transmit
13 * and one for receive. Each has a head and a tail. The head is where
14 * we/the chip adds packets to be transmitted/received, and the tail
15 * is where the transmitter has got to/where the receiver will stop.
16 * Both of these queues are circular, and since the chip is running
17 * all the time, we have to be careful when we modify the pointers etc
18 * so that the buffer memory contents is valid all the time.
20 * Change log:
21 * 1.00 RMK Released
22 * 1.01 RMK 19/03/1996 Transfers the last odd byte onto/off of the card now.
23 * 1.02 RMK 25/05/1997 Added code to restart RU if it goes not ready
24 * 1.03 RMK 14/09/1997 Cleaned up the handling of a reset during the TX interrupt.
25 * Should prevent lockup.
26 * 1.04 RMK 17/09/1997 Added more info when initialsation of chip goes wrong.
27 * TDR now only reports failure when chip reports non-zero
28 * TDR time-distance.
29 * 1.05 RMK 31/12/1997 Removed calls to dev_tint for 2.1
30 * 1.06 RMK 10/02/2000 Updated for 2.3.43
31 * 1.07 RMK 13/05/2000 Updated for 2.3.99-pre8
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
42 #include <linux/in.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/errno.h>
46 #include <linux/device.h>
47 #include <linux/init.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <asm/io.h>
55 #include <asm/dma.h>
56 #include <asm/ecard.h>
58 #define __ETHER1_C
59 #include "ether1.h"
61 static unsigned int net_debug = NET_DEBUG;
63 #define BUFFER_SIZE 0x10000
64 #define TX_AREA_START 0x00100
65 #define TX_AREA_END 0x05000
66 #define RX_AREA_START 0x05000
67 #define RX_AREA_END 0x0fc00
69 static int ether1_open(struct net_device *dev);
70 static int ether1_sendpacket(struct sk_buff *skb, struct net_device *dev);
71 static void ether1_interrupt(int irq, void *dev_id, struct pt_regs *regs);
72 static int ether1_close(struct net_device *dev);
73 static struct net_device_stats *ether1_getstats(struct net_device *dev);
74 static void ether1_setmulticastlist(struct net_device *dev);
75 static void ether1_timeout(struct net_device *dev);
77 /* ------------------------------------------------------------------------- */
79 static char version[] __initdata = "ether1 ethernet driver (c) 2000 Russell King v1.07\n";
81 #define BUS_16 16
82 #define BUS_8 8
84 /* ------------------------------------------------------------------------- */
86 #define DISABLEIRQS 1
87 #define NORMALIRQS 0
89 #define ether1_inw(dev, addr, type, offset, svflgs) ether1_inw_p (dev, addr + (int)(&((type *)0)->offset), svflgs)
90 #define ether1_outw(dev, val, addr, type, offset, svflgs) ether1_outw_p (dev, val, addr + (int)(&((type *)0)->offset), svflgs)
92 static inline unsigned short
93 ether1_inw_p (struct net_device *dev, int addr, int svflgs)
95 unsigned long flags;
96 unsigned short ret;
98 if (svflgs)
99 local_irq_save (flags);
101 outb (addr >> 12, REG_PAGE);
102 ret = inw (ETHER1_RAM + ((addr & 4095) >> 1));
103 if (svflgs)
104 local_irq_restore (flags);
105 return ret;
108 static inline void
109 ether1_outw_p (struct net_device *dev, unsigned short val, int addr, int svflgs)
111 unsigned long flags;
113 if (svflgs)
114 local_irq_save (flags);
116 outb (addr >> 12, REG_PAGE);
117 outw (val, ETHER1_RAM + ((addr & 4095) >> 1));
118 if (svflgs)
119 local_irq_restore (flags);
123 * Some inline assembler to allow fast transfers on to/off of the card.
124 * Since this driver depends on some features presented by the ARM
125 * specific architecture, and that you can't configure this driver
126 * without specifiing ARM mode, this is not a problem.
128 * This routine is essentially an optimised memcpy from the card's
129 * onboard RAM to kernel memory.
131 static void
132 ether1_writebuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
134 unsigned int page, thislen, offset, addr;
136 offset = start & 4095;
137 page = start >> 12;
138 addr = ioaddr(ETHER1_RAM + (offset >> 1));
140 if (offset + length > 4096)
141 thislen = 4096 - offset;
142 else
143 thislen = length;
145 do {
146 int used;
148 outb(page, REG_PAGE);
149 length -= thislen;
151 __asm__ __volatile__(
152 "subs %3, %3, #2
153 bmi 2f
154 1: ldr %0, [%1], #2
155 mov %0, %0, lsl #16
156 orr %0, %0, %0, lsr #16
157 str %0, [%2], #4
158 subs %3, %3, #2
159 bmi 2f
160 ldr %0, [%1], #2
161 mov %0, %0, lsl #16
162 orr %0, %0, %0, lsr #16
163 str %0, [%2], #4
164 subs %3, %3, #2
165 bmi 2f
166 ldr %0, [%1], #2
167 mov %0, %0, lsl #16
168 orr %0, %0, %0, lsr #16
169 str %0, [%2], #4
170 subs %3, %3, #2
171 bmi 2f
172 ldr %0, [%1], #2
173 mov %0, %0, lsl #16
174 orr %0, %0, %0, lsr #16
175 str %0, [%2], #4
176 subs %3, %3, #2
177 bpl 1b
178 2: adds %3, %3, #1
179 ldreqb %0, [%1]
180 streqb %0, [%2]"
181 : "=&r" (used), "=&r" (data)
182 : "r" (addr), "r" (thislen), "1" (data));
184 addr = ioaddr(ETHER1_RAM);
186 thislen = length;
187 if (thislen > 4096)
188 thislen = 4096;
189 page++;
190 } while (thislen);
193 static void
194 ether1_readbuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
196 unsigned int page, thislen, offset, addr;
198 offset = start & 4095;
199 page = start >> 12;
200 addr = ioaddr(ETHER1_RAM + (offset >> 1));
202 if (offset + length > 4096)
203 thislen = 4096 - offset;
204 else
205 thislen = length;
207 do {
208 int used;
210 outb(page, REG_PAGE);
211 length -= thislen;
213 __asm__ __volatile__(
214 "subs %3, %3, #2
215 bmi 2f
216 1: ldr %0, [%2], #4
217 strb %0, [%1], #1
218 mov %0, %0, lsr #8
219 strb %0, [%1], #1
220 subs %3, %3, #2
221 bmi 2f
222 ldr %0, [%2], #4
223 strb %0, [%1], #1
224 mov %0, %0, lsr #8
225 strb %0, [%1], #1
226 subs %3, %3, #2
227 bmi 2f
228 ldr %0, [%2], #4
229 strb %0, [%1], #1
230 mov %0, %0, lsr #8
231 strb %0, [%1], #1
232 subs %3, %3, #2
233 bmi 2f
234 ldr %0, [%2], #4
235 strb %0, [%1], #1
236 mov %0, %0, lsr #8
237 strb %0, [%1], #1
238 subs %3, %3, #2
239 bpl 1b
240 2: adds %3, %3, #1
241 ldreqb %0, [%2]
242 streqb %0, [%1]"
243 : "=&r" (used), "=&r" (data)
244 : "r" (addr), "r" (thislen), "1" (data));
246 addr = ioaddr(ETHER1_RAM);
248 thislen = length;
249 if (thislen > 4096)
250 thislen = 4096;
251 page++;
252 } while (thislen);
255 static int __init
256 ether1_ramtest(struct net_device *dev, unsigned char byte)
258 unsigned char *buffer = kmalloc (BUFFER_SIZE, GFP_KERNEL);
259 int i, ret = BUFFER_SIZE;
260 int max_errors = 15;
261 int bad = -1;
262 int bad_start = 0;
264 if (!buffer)
265 return 1;
267 memset (buffer, byte, BUFFER_SIZE);
268 ether1_writebuffer (dev, buffer, 0, BUFFER_SIZE);
269 memset (buffer, byte ^ 0xff, BUFFER_SIZE);
270 ether1_readbuffer (dev, buffer, 0, BUFFER_SIZE);
272 for (i = 0; i < BUFFER_SIZE; i++) {
273 if (buffer[i] != byte) {
274 if (max_errors >= 0 && bad != buffer[i]) {
275 if (bad != -1)
276 printk ("\n");
277 printk (KERN_CRIT "%s: RAM failed with (%02X instead of %02X) at 0x%04X",
278 dev->name, buffer[i], byte, i);
279 ret = -ENODEV;
280 max_errors --;
281 bad = buffer[i];
282 bad_start = i;
284 } else {
285 if (bad != -1) {
286 if (bad_start == i - 1)
287 printk ("\n");
288 else
289 printk (" - 0x%04X\n", i - 1);
290 bad = -1;
295 if (bad != -1)
296 printk (" - 0x%04X\n", BUFFER_SIZE);
297 kfree (buffer);
299 return ret;
302 static int
303 ether1_reset (struct net_device *dev)
305 outb (CTRL_RST|CTRL_ACK, REG_CONTROL);
306 return BUS_16;
309 static int __init
310 ether1_init_2(struct net_device *dev)
312 int i;
313 dev->mem_start = 0;
315 i = ether1_ramtest (dev, 0x5a);
317 if (i > 0)
318 i = ether1_ramtest (dev, 0x1e);
320 if (i <= 0)
321 return -ENODEV;
323 dev->mem_end = i;
324 return 0;
328 * These are the structures that are loaded into the ether RAM card to
329 * initialise the 82586
332 /* at 0x0100 */
333 #define NOP_ADDR (TX_AREA_START)
334 #define NOP_SIZE (0x06)
335 static nop_t init_nop = {
337 CMD_NOP,
338 NOP_ADDR
341 /* at 0x003a */
342 #define TDR_ADDR (0x003a)
343 #define TDR_SIZE (0x08)
344 static tdr_t init_tdr = {
346 CMD_TDR | CMD_INTR,
347 NOP_ADDR,
351 /* at 0x002e */
352 #define MC_ADDR (0x002e)
353 #define MC_SIZE (0x0c)
354 static mc_t init_mc = {
356 CMD_SETMULTICAST,
357 TDR_ADDR,
359 { { 0, } }
362 /* at 0x0022 */
363 #define SA_ADDR (0x0022)
364 #define SA_SIZE (0x0c)
365 static sa_t init_sa = {
367 CMD_SETADDRESS,
368 MC_ADDR,
369 { 0, }
372 /* at 0x0010 */
373 #define CFG_ADDR (0x0010)
374 #define CFG_SIZE (0x12)
375 static cfg_t init_cfg = {
377 CMD_CONFIG,
378 SA_ADDR,
381 CFG8_SRDY,
382 CFG9_PREAMB8 | CFG9_ADDRLENBUF | CFG9_ADDRLEN(6),
384 0x60,
386 CFG13_RETRY(15) | CFG13_SLOTH(2),
390 /* at 0x0000 */
391 #define SCB_ADDR (0x0000)
392 #define SCB_SIZE (0x10)
393 static scb_t init_scb = {
395 SCB_CMDACKRNR | SCB_CMDACKCNA | SCB_CMDACKFR | SCB_CMDACKCX,
396 CFG_ADDR,
397 RX_AREA_START,
404 /* at 0xffee */
405 #define ISCP_ADDR (0xffee)
406 #define ISCP_SIZE (0x08)
407 static iscp_t init_iscp = {
409 SCB_ADDR,
410 0x0000,
411 0x0000
414 /* at 0xfff6 */
415 #define SCP_ADDR (0xfff6)
416 #define SCP_SIZE (0x0a)
417 static scp_t init_scp = {
418 SCP_SY_16BBUS,
419 { 0, 0 },
420 ISCP_ADDR,
424 #define RFD_SIZE (0x16)
425 static rfd_t init_rfd = {
430 { 0, },
431 { 0, },
435 #define RBD_SIZE (0x0a)
436 static rbd_t init_rbd = {
441 ETH_FRAME_LEN + 8
444 #define TX_SIZE (0x08)
445 #define TBD_SIZE (0x08)
447 static int
448 ether1_init_for_open (struct net_device *dev)
450 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
451 int i, status, addr, next, next2;
452 int failures = 0;
454 outb (CTRL_RST|CTRL_ACK, REG_CONTROL);
456 for (i = 0; i < 6; i++)
457 init_sa.sa_addr[i] = dev->dev_addr[i];
459 /* load data structures into ether1 RAM */
460 ether1_writebuffer (dev, &init_scp, SCP_ADDR, SCP_SIZE);
461 ether1_writebuffer (dev, &init_iscp, ISCP_ADDR, ISCP_SIZE);
462 ether1_writebuffer (dev, &init_scb, SCB_ADDR, SCB_SIZE);
463 ether1_writebuffer (dev, &init_cfg, CFG_ADDR, CFG_SIZE);
464 ether1_writebuffer (dev, &init_sa, SA_ADDR, SA_SIZE);
465 ether1_writebuffer (dev, &init_mc, MC_ADDR, MC_SIZE);
466 ether1_writebuffer (dev, &init_tdr, TDR_ADDR, TDR_SIZE);
467 ether1_writebuffer (dev, &init_nop, NOP_ADDR, NOP_SIZE);
469 if (ether1_inw (dev, CFG_ADDR, cfg_t, cfg_command, NORMALIRQS) != CMD_CONFIG) {
470 printk (KERN_ERR "%s: detected either RAM fault or compiler bug\n",
471 dev->name);
472 return 1;
476 * setup circularly linked list of { rfd, rbd, buffer }, with
477 * all rfds circularly linked, rbds circularly linked.
478 * First rfd is linked to scp, first rbd is linked to first
479 * rfd. Last rbd has a suspend command.
481 addr = RX_AREA_START;
482 do {
483 next = addr + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
484 next2 = next + RFD_SIZE + RBD_SIZE + ETH_FRAME_LEN + 10;
486 if (next2 >= RX_AREA_END) {
487 next = RX_AREA_START;
488 init_rfd.rfd_command = RFD_CMDEL | RFD_CMDSUSPEND;
489 priv->rx_tail = addr;
490 } else
491 init_rfd.rfd_command = 0;
492 if (addr == RX_AREA_START)
493 init_rfd.rfd_rbdoffset = addr + RFD_SIZE;
494 else
495 init_rfd.rfd_rbdoffset = 0;
496 init_rfd.rfd_link = next;
497 init_rbd.rbd_link = next + RFD_SIZE;
498 init_rbd.rbd_bufl = addr + RFD_SIZE + RBD_SIZE;
500 ether1_writebuffer (dev, &init_rfd, addr, RFD_SIZE);
501 ether1_writebuffer (dev, &init_rbd, addr + RFD_SIZE, RBD_SIZE);
502 addr = next;
503 } while (next2 < RX_AREA_END);
505 priv->tx_link = NOP_ADDR;
506 priv->tx_head = NOP_ADDR + NOP_SIZE;
507 priv->tx_tail = TDR_ADDR;
508 priv->rx_head = RX_AREA_START;
510 /* release reset & give 586 a prod */
511 priv->resetting = 1;
512 priv->initialising = 1;
513 outb (CTRL_RST, REG_CONTROL);
514 outb (0, REG_CONTROL);
515 outb (CTRL_CA, REG_CONTROL);
517 /* 586 should now unset iscp.busy */
518 i = jiffies + HZ/2;
519 while (ether1_inw (dev, ISCP_ADDR, iscp_t, iscp_busy, DISABLEIRQS) == 1) {
520 if (time_after(jiffies, i)) {
521 printk (KERN_WARNING "%s: can't initialise 82586: iscp is busy\n", dev->name);
522 return 1;
526 /* check status of commands that we issued */
527 i += HZ/10;
528 while (((status = ether1_inw (dev, CFG_ADDR, cfg_t, cfg_status, DISABLEIRQS))
529 & STAT_COMPLETE) == 0) {
530 if (time_after(jiffies, i))
531 break;
534 if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
535 printk (KERN_WARNING "%s: can't initialise 82586: config status %04X\n", dev->name, status);
536 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
537 ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
538 ether1_inw (dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
539 ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
540 ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
541 failures += 1;
544 i += HZ/10;
545 while (((status = ether1_inw (dev, SA_ADDR, sa_t, sa_status, DISABLEIRQS))
546 & STAT_COMPLETE) == 0) {
547 if (time_after(jiffies, i))
548 break;
551 if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
552 printk (KERN_WARNING "%s: can't initialise 82586: set address status %04X\n", dev->name, status);
553 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
554 ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
555 ether1_inw (dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
556 ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
557 ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
558 failures += 1;
561 i += HZ/10;
562 while (((status = ether1_inw (dev, MC_ADDR, mc_t, mc_status, DISABLEIRQS))
563 & STAT_COMPLETE) == 0) {
564 if (time_after(jiffies, i))
565 break;
568 if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
569 printk (KERN_WARNING "%s: can't initialise 82586: set multicast status %04X\n", dev->name, status);
570 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
571 ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
572 ether1_inw (dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
573 ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
574 ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
575 failures += 1;
578 i += HZ;
579 while (((status = ether1_inw (dev, TDR_ADDR, tdr_t, tdr_status, DISABLEIRQS))
580 & STAT_COMPLETE) == 0) {
581 if (time_after(jiffies, i))
582 break;
585 if ((status & (STAT_COMPLETE | STAT_OK)) != (STAT_COMPLETE | STAT_OK)) {
586 printk (KERN_WARNING "%s: can't tdr (ignored)\n", dev->name);
587 printk (KERN_DEBUG "%s: SCB=[STS=%04X CMD=%04X CBL=%04X RFA=%04X]\n", dev->name,
588 ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
589 ether1_inw (dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
590 ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
591 ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
592 } else {
593 status = ether1_inw (dev, TDR_ADDR, tdr_t, tdr_result, DISABLEIRQS);
594 if (status & TDR_XCVRPROB)
595 printk (KERN_WARNING "%s: i/f failed tdr: transceiver problem\n", dev->name);
596 else if ((status & (TDR_SHORT|TDR_OPEN)) && (status & TDR_TIME)) {
597 #ifdef FANCY
598 printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d.%d us away\n", dev->name,
599 status & TDR_SHORT ? "short" : "open", (status & TDR_TIME) / 10,
600 (status & TDR_TIME) % 10);
601 #else
602 printk (KERN_WARNING "%s: i/f failed tdr: cable %s %d clks away\n", dev->name,
603 status & TDR_SHORT ? "short" : "open", (status & TDR_TIME));
604 #endif
608 if (failures)
609 ether1_reset (dev);
610 return failures ? 1 : 0;
613 /* ------------------------------------------------------------------------- */
615 static int
616 ether1_txalloc (struct net_device *dev, int size)
618 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
619 int start, tail;
621 size = (size + 1) & ~1;
622 tail = priv->tx_tail;
624 if (priv->tx_head + size > TX_AREA_END) {
625 if (tail > priv->tx_head)
626 return -1;
627 start = TX_AREA_START;
628 if (start + size > tail)
629 return -1;
630 priv->tx_head = start + size;
631 } else {
632 if (priv->tx_head < tail && (priv->tx_head + size) > tail)
633 return -1;
634 start = priv->tx_head;
635 priv->tx_head += size;
638 return start;
641 static int
642 ether1_open (struct net_device *dev)
644 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
646 if (!is_valid_ether_addr(dev->dev_addr)) {
647 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
648 dev->name);
649 return -EINVAL;
652 if (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
653 return -EAGAIN;
655 memset (&priv->stats, 0, sizeof (struct net_device_stats));
657 if (ether1_init_for_open (dev)) {
658 free_irq (dev->irq, dev);
659 return -EAGAIN;
662 netif_start_queue(dev);
664 return 0;
667 static void
668 ether1_timeout(struct net_device *dev)
670 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
672 printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
673 dev->name);
674 printk(KERN_WARNING "%s: resetting device\n", dev->name);
676 ether1_reset (dev);
678 if (ether1_init_for_open (dev))
679 printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
681 priv->stats.tx_errors++;
682 netif_wake_queue(dev);
685 static int
686 ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
688 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
689 int len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
690 int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
691 unsigned long flags;
692 tx_t tx;
693 tbd_t tbd;
694 nop_t nop;
696 if (priv->restart) {
697 printk(KERN_WARNING "%s: resetting device\n", dev->name);
699 ether1_reset(dev);
701 if (ether1_init_for_open(dev))
702 printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
703 else
704 priv->restart = 0;
708 * insert packet followed by a nop
710 txaddr = ether1_txalloc (dev, TX_SIZE);
711 tbdaddr = ether1_txalloc (dev, TBD_SIZE);
712 dataddr = ether1_txalloc (dev, len);
713 nopaddr = ether1_txalloc (dev, NOP_SIZE);
715 tx.tx_status = 0;
716 tx.tx_command = CMD_TX | CMD_INTR;
717 tx.tx_link = nopaddr;
718 tx.tx_tbdoffset = tbdaddr;
719 tbd.tbd_opts = TBD_EOL | len;
720 tbd.tbd_link = I82586_NULL;
721 tbd.tbd_bufl = dataddr;
722 tbd.tbd_bufh = 0;
723 nop.nop_status = 0;
724 nop.nop_command = CMD_NOP;
725 nop.nop_link = nopaddr;
727 local_irq_save(flags);
728 ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
729 ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
730 ether1_writebuffer (dev, skb->data, dataddr, len);
731 ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
732 tmp = priv->tx_link;
733 priv->tx_link = nopaddr;
735 /* now reset the previous nop pointer */
736 ether1_outw (dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
738 local_irq_restore(flags);
740 /* handle transmit */
741 dev->trans_start = jiffies;
743 /* check to see if we have room for a full sized ether frame */
744 tmp = priv->tx_head;
745 tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
746 priv->tx_head = tmp;
747 dev_kfree_skb (skb);
749 if (tst == -1)
750 netif_stop_queue(dev);
752 return 0;
755 static void
756 ether1_xmit_done (struct net_device *dev)
758 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
759 nop_t nop;
760 int caddr, tst;
762 caddr = priv->tx_tail;
764 again:
765 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
767 switch (nop.nop_command & CMD_MASK) {
768 case CMD_TDR:
769 /* special case */
770 if (ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
771 != (unsigned short)I82586_NULL) {
772 ether1_outw(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
773 scb_command, NORMALIRQS);
774 outb (CTRL_CA, REG_CONTROL);
776 priv->tx_tail = NOP_ADDR;
777 return;
779 case CMD_NOP:
780 if (nop.nop_link == caddr) {
781 if (priv->initialising == 0)
782 printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
783 else
784 priv->initialising = 0;
785 return;
787 if (caddr == nop.nop_link)
788 return;
789 caddr = nop.nop_link;
790 goto again;
792 case CMD_TX:
793 if (nop.nop_status & STAT_COMPLETE)
794 break;
795 printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
796 priv->restart = 1;
797 return;
799 default:
800 printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
801 nop.nop_command & CMD_MASK, caddr);
802 priv->restart = 1;
803 return;
806 while (nop.nop_status & STAT_COMPLETE) {
807 if (nop.nop_status & STAT_OK) {
808 priv->stats.tx_packets ++;
809 priv->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
810 } else {
811 priv->stats.tx_errors ++;
813 if (nop.nop_status & STAT_COLLAFTERTX)
814 priv->stats.collisions ++;
815 if (nop.nop_status & STAT_NOCARRIER)
816 priv->stats.tx_carrier_errors ++;
817 if (nop.nop_status & STAT_TXLOSTCTS)
818 printk (KERN_WARNING "%s: cts lost\n", dev->name);
819 if (nop.nop_status & STAT_TXSLOWDMA)
820 priv->stats.tx_fifo_errors ++;
821 if (nop.nop_status & STAT_COLLEXCESSIVE)
822 priv->stats.collisions += 16;
825 if (nop.nop_link == caddr) {
826 printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
827 break;
830 caddr = nop.nop_link;
831 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
832 if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
833 printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
834 break;
837 if (caddr == nop.nop_link)
838 break;
840 caddr = nop.nop_link;
841 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
842 if ((nop.nop_command & CMD_MASK) != CMD_TX) {
843 printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
844 break;
847 priv->tx_tail = caddr;
849 caddr = priv->tx_head;
850 tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
851 priv->tx_head = caddr;
852 if (tst != -1)
853 netif_wake_queue(dev);
856 static void
857 ether1_recv_done (struct net_device *dev)
859 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
860 int status;
861 int nexttail, rbdaddr;
862 rbd_t rbd;
864 do {
865 status = ether1_inw (dev, priv->rx_head, rfd_t, rfd_status, NORMALIRQS);
866 if ((status & RFD_COMPLETE) == 0)
867 break;
869 rbdaddr = ether1_inw (dev, priv->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
870 ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
872 if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
873 int length = rbd.rbd_status & RBD_ACNT;
874 struct sk_buff *skb;
876 length = (length + 1) & ~1;
877 skb = dev_alloc_skb (length + 2);
879 if (skb) {
880 skb->dev = dev;
881 skb_reserve (skb, 2);
883 ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
885 skb->protocol = eth_type_trans (skb, dev);
886 netif_rx (skb);
887 priv->stats.rx_packets ++;
888 } else
889 priv->stats.rx_dropped ++;
890 } else {
891 printk(KERN_WARNING "%s: %s\n", dev->name,
892 (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
893 priv->stats.rx_dropped ++;
896 nexttail = ether1_inw (dev, priv->rx_tail, rfd_t, rfd_link, NORMALIRQS);
897 /* nexttail should be rx_head */
898 if (nexttail != priv->rx_head)
899 printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
900 dev->name, nexttail, priv->rx_head);
901 ether1_outw (dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
902 ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_command, NORMALIRQS);
903 ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_status, NORMALIRQS);
904 ether1_outw (dev, 0, priv->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
906 priv->rx_tail = nexttail;
907 priv->rx_head = ether1_inw (dev, priv->rx_head, rfd_t, rfd_link, NORMALIRQS);
908 } while (1);
911 static void
912 ether1_interrupt (int irq, void *dev_id, struct pt_regs *regs)
914 struct net_device *dev = (struct net_device *)dev_id;
915 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
916 int status;
918 status = ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
920 if (status) {
921 ether1_outw(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
922 SCB_ADDR, scb_t, scb_command, NORMALIRQS);
923 outb (CTRL_CA | CTRL_ACK, REG_CONTROL);
924 if (status & SCB_STCX) {
925 ether1_xmit_done (dev);
927 if (status & SCB_STCNA) {
928 if (priv->resetting == 0)
929 printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
930 else
931 priv->resetting += 1;
932 if (ether1_inw (dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
933 != (unsigned short)I82586_NULL) {
934 ether1_outw (dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
935 outb (CTRL_CA, REG_CONTROL);
937 if (priv->resetting == 2)
938 priv->resetting = 0;
940 if (status & SCB_STFR) {
941 ether1_recv_done (dev);
943 if (status & SCB_STRNR) {
944 if (ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
945 printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
946 ether1_outw (dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
947 outb (CTRL_CA, REG_CONTROL);
948 priv->stats.rx_dropped ++; /* we suspended due to lack of buffer space */
949 } else
950 printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
951 ether1_inw (dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
952 printk (KERN_WARNING "RU ptr = %04X\n", ether1_inw (dev, SCB_ADDR, scb_t, scb_rfa_offset,
953 NORMALIRQS));
955 } else
956 outb (CTRL_ACK, REG_CONTROL);
959 static int
960 ether1_close (struct net_device *dev)
962 ether1_reset (dev);
964 free_irq(dev->irq, dev);
966 return 0;
969 static struct net_device_stats *
970 ether1_getstats (struct net_device *dev)
972 struct ether1_priv *priv = (struct ether1_priv *)dev->priv;
973 return &priv->stats;
976 static int
977 ether1_set_mac_address(struct net_device *dev, void *p)
979 struct sockaddr *addr = p;
981 if (netif_running(dev))
982 return -EBUSY;
984 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
987 * We'll set the MAC address on the chip when we open it.
990 return 0;
994 * Set or clear the multicast filter for this adaptor.
995 * num_addrs == -1 Promiscuous mode, receive all packets.
996 * num_addrs == 0 Normal mode, clear multicast list.
997 * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
998 * best-effort filtering.
1000 static void
1001 ether1_setmulticastlist (struct net_device *dev)
1005 /* ------------------------------------------------------------------------- */
1007 static void __init ether1_banner(void)
1009 static unsigned int version_printed = 0;
1011 if (net_debug && version_printed++ == 0)
1012 printk(KERN_INFO "%s", version);
1015 static int __devinit
1016 ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
1018 struct net_device *dev;
1019 struct ether1_priv *priv;
1020 int i, ret = 0;
1022 ether1_banner();
1024 dev = init_etherdev(NULL, sizeof(struct ether1_priv));
1025 if (!dev) {
1026 ret = -ENOMEM;
1027 goto out;
1030 SET_MODULE_OWNER(dev);
1032 dev->base_addr = ecard_address(ec, ECARD_IOC, ECARD_FAST);
1033 dev->irq = ec->irq;
1036 * these will not fail - the nature of the bus ensures this
1038 request_region(dev->base_addr, 16, dev->name);
1039 request_region(dev->base_addr + 0x800, 4096, dev->name);
1041 priv = (struct ether1_priv *)dev->priv;
1042 if ((priv->bus_type = ether1_reset(dev)) == 0) {
1043 ret = -ENODEV;
1044 goto release;
1047 printk(KERN_INFO "%s: ether1 in slot %d, ",
1048 dev->name, ec->slot_no);
1050 for (i = 0; i < 6; i++) {
1051 dev->dev_addr[i] = inb(IDPROM_ADDRESS + i);
1052 printk ("%2.2x%c", dev->dev_addr[i], i == 5 ? '\n' : ':');
1055 if (ether1_init_2(dev)) {
1056 ret = -ENODEV;
1057 goto release;
1060 dev->open = ether1_open;
1061 dev->stop = ether1_close;
1062 dev->hard_start_xmit = ether1_sendpacket;
1063 dev->get_stats = ether1_getstats;
1064 dev->set_multicast_list = ether1_setmulticastlist;
1065 dev->set_mac_address = ether1_set_mac_address;
1066 dev->tx_timeout = ether1_timeout;
1067 dev->watchdog_timeo = 5 * HZ / 100;
1069 ecard_set_drvdata(ec, dev);
1070 return 0;
1072 release:
1073 release_region(dev->base_addr, 16);
1074 release_region(dev->base_addr + 0x800, 4096);
1075 unregister_netdev(dev);
1076 kfree(dev);
1077 out:
1078 return ret;
1081 static void __devexit ether1_remove(struct expansion_card *ec)
1083 struct net_device *dev = ecard_get_drvdata(ec);
1085 ecard_set_drvdata(ec, NULL);
1087 unregister_netdev(dev);
1089 release_region(dev->base_addr, 16);
1090 release_region(dev->base_addr + 0x800, 4096);
1091 kfree(dev);
1094 static const struct ecard_id ether1_ids[] = {
1095 { MANU_ACORN, PROD_ACORN_ETHER1 },
1096 { 0xffff, 0xffff }
1099 static struct ecard_driver ether1_driver = {
1100 .probe = ether1_probe,
1101 .remove = __devexit_p(ether1_remove),
1102 .id_table = ether1_ids,
1103 .drv = {
1104 .name = "ether1",
1108 static int __init ether1_init(void)
1110 return ecard_register_driver(&ether1_driver);
1113 static void __exit ether1_exit(void)
1115 ecard_remove_driver(&ether1_driver);
1118 module_init(ether1_init);
1119 module_exit(ether1_exit);
1121 MODULE_LICENSE("GPL");