Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[pandora-kernel.git] / drivers / net / arm / ether1.c
1 /*
2  *  linux/drivers/acorn/net/ether1.c
3  *
4  *  Copyright (C) 1996-2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Acorn ether1 driver (82586 chip) for Acorn machines
11  *
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.
19  *
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
32  */
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/ioport.h>
40 #include <linux/in.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/errno.h>
44 #include <linux/device.h>
45 #include <linux/init.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/bitops.h>
50
51 #include <asm/system.h>
52 #include <asm/io.h>
53 #include <asm/dma.h>
54 #include <asm/ecard.h>
55
56 #define __ETHER1_C
57 #include "ether1.h"
58
59 static unsigned int net_debug = NET_DEBUG;
60
61 #define BUFFER_SIZE     0x10000
62 #define TX_AREA_START   0x00100
63 #define TX_AREA_END     0x05000
64 #define RX_AREA_START   0x05000
65 #define RX_AREA_END     0x0fc00
66
67 static int ether1_open(struct net_device *dev);
68 static int ether1_sendpacket(struct sk_buff *skb, struct net_device *dev);
69 static irqreturn_t ether1_interrupt(int irq, void *dev_id);
70 static int ether1_close(struct net_device *dev);
71 static struct net_device_stats *ether1_getstats(struct net_device *dev);
72 static void ether1_setmulticastlist(struct net_device *dev);
73 static void ether1_timeout(struct net_device *dev);
74
75 /* ------------------------------------------------------------------------- */
76
77 static char version[] __devinitdata = "ether1 ethernet driver (c) 2000 Russell King v1.07\n";
78
79 #define BUS_16 16
80 #define BUS_8  8
81
82 /* ------------------------------------------------------------------------- */
83
84 #define DISABLEIRQS 1
85 #define NORMALIRQS  0
86
87 #define ether1_readw(dev, addr, type, offset, svflgs) ether1_inw_p (dev, addr + (int)(&((type *)0)->offset), svflgs)
88 #define ether1_writew(dev, val, addr, type, offset, svflgs) ether1_outw_p (dev, val, addr + (int)(&((type *)0)->offset), svflgs)
89
90 static inline unsigned short
91 ether1_inw_p (struct net_device *dev, int addr, int svflgs)
92 {
93         unsigned long flags;
94         unsigned short ret;
95
96         if (svflgs)
97                 local_irq_save (flags);
98
99         writeb(addr >> 12, REG_PAGE);
100         ret = readw(ETHER1_RAM + ((addr & 4095) << 1));
101         if (svflgs)
102                 local_irq_restore (flags);
103         return ret;
104 }
105
106 static inline void
107 ether1_outw_p (struct net_device *dev, unsigned short val, int addr, int svflgs)
108 {
109         unsigned long flags;
110
111         if (svflgs)
112                 local_irq_save (flags);
113
114         writeb(addr >> 12, REG_PAGE);
115         writew(val, ETHER1_RAM + ((addr & 4095) << 1));
116         if (svflgs)
117                 local_irq_restore (flags);
118 }
119
120 /*
121  * Some inline assembler to allow fast transfers on to/off of the card.
122  * Since this driver depends on some features presented by the ARM
123  * specific architecture, and that you can't configure this driver
124  * without specifiing ARM mode, this is not a problem.
125  *
126  * This routine is essentially an optimised memcpy from the card's
127  * onboard RAM to kernel memory.
128  */
129 static void
130 ether1_writebuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
131 {
132         unsigned int page, thislen, offset;
133         void __iomem *addr;
134
135         offset = start & 4095;
136         page = start >> 12;
137         addr = ETHER1_RAM + (offset << 1);
138
139         if (offset + length > 4096)
140                 thislen = 4096 - offset;
141         else
142                 thislen = length;
143
144         do {
145                 int used;
146
147                 writeb(page, REG_PAGE);
148                 length -= thislen;
149
150                 __asm__ __volatile__(
151         "subs   %3, %3, #2\n\
152         bmi     2f\n\
153 1:      ldr     %0, [%1], #2\n\
154         mov     %0, %0, lsl #16\n\
155         orr     %0, %0, %0, lsr #16\n\
156         str     %0, [%2], #4\n\
157         subs    %3, %3, #2\n\
158         bmi     2f\n\
159         ldr     %0, [%1], #2\n\
160         mov     %0, %0, lsl #16\n\
161         orr     %0, %0, %0, lsr #16\n\
162         str     %0, [%2], #4\n\
163         subs    %3, %3, #2\n\
164         bmi     2f\n\
165         ldr     %0, [%1], #2\n\
166         mov     %0, %0, lsl #16\n\
167         orr     %0, %0, %0, lsr #16\n\
168         str     %0, [%2], #4\n\
169         subs    %3, %3, #2\n\
170         bmi     2f\n\
171         ldr     %0, [%1], #2\n\
172         mov     %0, %0, lsl #16\n\
173         orr     %0, %0, %0, lsr #16\n\
174         str     %0, [%2], #4\n\
175         subs    %3, %3, #2\n\
176         bpl     1b\n\
177 2:      adds    %3, %3, #1\n\
178         ldreqb  %0, [%1]\n\
179         streqb  %0, [%2]"
180                 : "=&r" (used), "=&r" (data)
181                 : "r"  (addr), "r" (thislen), "1" (data));
182
183                 addr = ETHER1_RAM;
184
185                 thislen = length;
186                 if (thislen > 4096)
187                         thislen = 4096;
188                 page++;
189         } while (thislen);
190 }
191
192 static void
193 ether1_readbuffer (struct net_device *dev, void *data, unsigned int start, unsigned int length)
194 {
195         unsigned int page, thislen, offset;
196         void __iomem *addr;
197
198         offset = start & 4095;
199         page = start >> 12;
200         addr = ETHER1_RAM + (offset << 1);
201
202         if (offset + length > 4096)
203                 thislen = 4096 - offset;
204         else
205                 thislen = length;
206
207         do {
208                 int used;
209
210                 writeb(page, REG_PAGE);
211                 length -= thislen;
212
213                 __asm__ __volatile__(
214         "subs   %3, %3, #2\n\
215         bmi     2f\n\
216 1:      ldr     %0, [%2], #4\n\
217         strb    %0, [%1], #1\n\
218         mov     %0, %0, lsr #8\n\
219         strb    %0, [%1], #1\n\
220         subs    %3, %3, #2\n\
221         bmi     2f\n\
222         ldr     %0, [%2], #4\n\
223         strb    %0, [%1], #1\n\
224         mov     %0, %0, lsr #8\n\
225         strb    %0, [%1], #1\n\
226         subs    %3, %3, #2\n\
227         bmi     2f\n\
228         ldr     %0, [%2], #4\n\
229         strb    %0, [%1], #1\n\
230         mov     %0, %0, lsr #8\n\
231         strb    %0, [%1], #1\n\
232         subs    %3, %3, #2\n\
233         bmi     2f\n\
234         ldr     %0, [%2], #4\n\
235         strb    %0, [%1], #1\n\
236         mov     %0, %0, lsr #8\n\
237         strb    %0, [%1], #1\n\
238         subs    %3, %3, #2\n\
239         bpl     1b\n\
240 2:      adds    %3, %3, #1\n\
241         ldreqb  %0, [%2]\n\
242         streqb  %0, [%1]"
243                 : "=&r" (used), "=&r" (data)
244                 : "r"  (addr), "r" (thislen), "1" (data));
245
246                 addr = ETHER1_RAM;
247
248                 thislen = length;
249                 if (thislen > 4096)
250                         thislen = 4096;
251                 page++;
252         } while (thislen);
253 }
254
255 static int __devinit
256 ether1_ramtest(struct net_device *dev, unsigned char byte)
257 {
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;
263
264         if (!buffer)
265                 return 1;
266
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);
271
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;
283                         }
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;
291                         }
292                 }
293         }
294
295         if (bad != -1)
296                 printk (" - 0x%04X\n", BUFFER_SIZE);
297         kfree (buffer);
298
299         return ret;
300 }
301
302 static int
303 ether1_reset (struct net_device *dev)
304 {
305         writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
306         return BUS_16;
307 }
308
309 static int __devinit
310 ether1_init_2(struct net_device *dev)
311 {
312         int i;
313         dev->mem_start = 0;
314
315         i = ether1_ramtest (dev, 0x5a);
316
317         if (i > 0)
318                 i = ether1_ramtest (dev, 0x1e);
319
320         if (i <= 0)
321                 return -ENODEV;
322
323         dev->mem_end = i;
324         return 0;
325 }
326
327 /*
328  * These are the structures that are loaded into the ether RAM card to
329  * initialise the 82586
330  */
331
332 /* at 0x0100 */
333 #define NOP_ADDR        (TX_AREA_START)
334 #define NOP_SIZE        (0x06)
335 static nop_t  init_nop  = {
336         0,
337         CMD_NOP,
338         NOP_ADDR
339 };
340
341 /* at 0x003a */
342 #define TDR_ADDR        (0x003a)
343 #define TDR_SIZE        (0x08)
344 static tdr_t  init_tdr  = {
345         0,
346         CMD_TDR | CMD_INTR,
347         NOP_ADDR,
348         0
349 };
350
351 /* at 0x002e */
352 #define MC_ADDR         (0x002e)
353 #define MC_SIZE         (0x0c)
354 static mc_t   init_mc   = {
355         0,
356         CMD_SETMULTICAST,
357         TDR_ADDR,
358         0,
359         { { 0, } }
360 };
361
362 /* at 0x0022 */
363 #define SA_ADDR         (0x0022)
364 #define SA_SIZE         (0x0c)
365 static sa_t   init_sa   = {
366         0,
367         CMD_SETADDRESS,
368         MC_ADDR,
369         { 0, }
370 };
371
372 /* at 0x0010 */
373 #define CFG_ADDR        (0x0010)
374 #define CFG_SIZE        (0x12)
375 static cfg_t  init_cfg  = {
376         0,
377         CMD_CONFIG,
378         SA_ADDR,
379         8,
380         8,
381         CFG8_SRDY,
382         CFG9_PREAMB8 | CFG9_ADDRLENBUF | CFG9_ADDRLEN(6),
383         0,
384         0x60,
385         0,
386         CFG13_RETRY(15) | CFG13_SLOTH(2),
387         0,
388 };
389
390 /* at 0x0000 */
391 #define SCB_ADDR        (0x0000)
392 #define SCB_SIZE        (0x10)
393 static scb_t  init_scb  = {
394         0,
395         SCB_CMDACKRNR | SCB_CMDACKCNA | SCB_CMDACKFR | SCB_CMDACKCX,
396         CFG_ADDR,
397         RX_AREA_START,
398         0,
399         0,
400         0,
401         0
402 };
403
404 /* at 0xffee */
405 #define ISCP_ADDR       (0xffee)
406 #define ISCP_SIZE       (0x08)
407 static iscp_t init_iscp = {
408         1,
409         SCB_ADDR,
410         0x0000,
411         0x0000
412 };
413
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,
421         0
422 };
423
424 #define RFD_SIZE        (0x16)
425 static rfd_t  init_rfd  = {
426         0,
427         0,
428         0,
429         0,
430         { 0, },
431         { 0, },
432         0
433 };
434
435 #define RBD_SIZE        (0x0a)
436 static rbd_t  init_rbd  = {
437         0,
438         0,
439         0,
440         0,
441         ETH_FRAME_LEN + 8
442 };
443
444 #define TX_SIZE         (0x08)
445 #define TBD_SIZE        (0x08)
446
447 static int
448 ether1_init_for_open (struct net_device *dev)
449 {
450         int i, status, addr, next, next2;
451         int failures = 0;
452         unsigned long timeout;
453
454         writeb(CTRL_RST|CTRL_ACK, REG_CONTROL);
455
456         for (i = 0; i < 6; i++)
457                 init_sa.sa_addr[i] = dev->dev_addr[i];
458
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);
468
469         if (ether1_readw(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;
473         }
474
475         /*
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.
480          */
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;
485
486                 if (next2 >= RX_AREA_END) {
487                         next = RX_AREA_START;
488                         init_rfd.rfd_command = RFD_CMDEL | RFD_CMDSUSPEND;
489                         priv(dev)->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;
499
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);
504
505         priv(dev)->tx_link = NOP_ADDR;
506         priv(dev)->tx_head = NOP_ADDR + NOP_SIZE;
507         priv(dev)->tx_tail = TDR_ADDR;
508         priv(dev)->rx_head = RX_AREA_START;
509
510         /* release reset & give 586 a prod */
511         priv(dev)->resetting = 1;
512         priv(dev)->initialising = 1;
513         writeb(CTRL_RST, REG_CONTROL);
514         writeb(0, REG_CONTROL);
515         writeb(CTRL_CA, REG_CONTROL);
516
517         /* 586 should now unset iscp.busy */
518         timeout = jiffies + HZ/2;
519         while (ether1_readw(dev, ISCP_ADDR, iscp_t, iscp_busy, DISABLEIRQS) == 1) {
520                 if (time_after(jiffies, timeout)) {
521                         printk (KERN_WARNING "%s: can't initialise 82586: iscp is busy\n", dev->name);
522                         return 1;
523                 }
524         }
525
526         /* check status of commands that we issued */
527         timeout += HZ/10;
528         while (((status = ether1_readw(dev, CFG_ADDR, cfg_t, cfg_status, DISABLEIRQS))
529                         & STAT_COMPLETE) == 0) {
530                 if (time_after(jiffies, timeout))
531                         break;
532         }
533
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_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
538                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
539                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
540                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
541                 failures += 1;
542         }
543
544         timeout += HZ/10;
545         while (((status = ether1_readw(dev, SA_ADDR, sa_t, sa_status, DISABLEIRQS))
546                         & STAT_COMPLETE) == 0) {
547                 if (time_after(jiffies, timeout))
548                         break;
549         }
550
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_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
555                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
556                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
557                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
558                 failures += 1;
559         }
560
561         timeout += HZ/10;
562         while (((status = ether1_readw(dev, MC_ADDR, mc_t, mc_status, DISABLEIRQS))
563                         & STAT_COMPLETE) == 0) {
564                 if (time_after(jiffies, timeout))
565                         break;
566         }
567
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_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
572                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
573                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
574                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
575                 failures += 1;
576         }
577
578         timeout += HZ;
579         while (((status = ether1_readw(dev, TDR_ADDR, tdr_t, tdr_status, DISABLEIRQS))
580                         & STAT_COMPLETE) == 0) {
581                 if (time_after(jiffies, timeout))
582                         break;
583         }
584
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_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS),
589                         ether1_readw(dev, SCB_ADDR, scb_t, scb_command, NORMALIRQS),
590                         ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS),
591                         ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset, NORMALIRQS));
592         } else {
593                 status = ether1_readw(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
605                 }
606         }
607
608         if (failures)
609                 ether1_reset (dev);
610         return failures ? 1 : 0;
611 }
612
613 /* ------------------------------------------------------------------------- */
614
615 static int
616 ether1_txalloc (struct net_device *dev, int size)
617 {
618         int start, tail;
619
620         size = (size + 1) & ~1;
621         tail = priv(dev)->tx_tail;
622
623         if (priv(dev)->tx_head + size > TX_AREA_END) {
624                 if (tail > priv(dev)->tx_head)
625                         return -1;
626                 start = TX_AREA_START;
627                 if (start + size > tail)
628                         return -1;
629                 priv(dev)->tx_head = start + size;
630         } else {
631                 if (priv(dev)->tx_head < tail && (priv(dev)->tx_head + size) > tail)
632                         return -1;
633                 start = priv(dev)->tx_head;
634                 priv(dev)->tx_head += size;
635         }
636
637         return start;
638 }
639
640 static int
641 ether1_open (struct net_device *dev)
642 {
643         if (!is_valid_ether_addr(dev->dev_addr)) {
644                 printk(KERN_WARNING "%s: invalid ethernet MAC address\n",
645                         dev->name);
646                 return -EINVAL;
647         }
648
649         if (request_irq(dev->irq, ether1_interrupt, 0, "ether1", dev))
650                 return -EAGAIN;
651
652         memset (&priv(dev)->stats, 0, sizeof (struct net_device_stats));
653
654         if (ether1_init_for_open (dev)) {
655                 free_irq (dev->irq, dev);
656                 return -EAGAIN;
657         }
658
659         netif_start_queue(dev);
660
661         return 0;
662 }
663
664 static void
665 ether1_timeout(struct net_device *dev)
666 {
667         printk(KERN_WARNING "%s: transmit timeout, network cable problem?\n",
668                 dev->name);
669         printk(KERN_WARNING "%s: resetting device\n", dev->name);
670
671         ether1_reset (dev);
672
673         if (ether1_init_for_open (dev))
674                 printk (KERN_ERR "%s: unable to restart interface\n", dev->name);
675
676         priv(dev)->stats.tx_errors++;
677         netif_wake_queue(dev);
678 }
679
680 static int
681 ether1_sendpacket (struct sk_buff *skb, struct net_device *dev)
682 {
683         int tmp, tst, nopaddr, txaddr, tbdaddr, dataddr;
684         unsigned long flags;
685         tx_t tx;
686         tbd_t tbd;
687         nop_t nop;
688
689         if (priv(dev)->restart) {
690                 printk(KERN_WARNING "%s: resetting device\n", dev->name);
691
692                 ether1_reset(dev);
693
694                 if (ether1_init_for_open(dev))
695                         printk(KERN_ERR "%s: unable to restart interface\n", dev->name);
696                 else
697                         priv(dev)->restart = 0;
698         }
699
700         if (skb->len < ETH_ZLEN) {
701                 if (skb_padto(skb, ETH_ZLEN))
702                         goto out;
703         }
704
705         /*
706          * insert packet followed by a nop
707          */
708         txaddr = ether1_txalloc (dev, TX_SIZE);
709         tbdaddr = ether1_txalloc (dev, TBD_SIZE);
710         dataddr = ether1_txalloc (dev, skb->len);
711         nopaddr = ether1_txalloc (dev, NOP_SIZE);
712
713         tx.tx_status = 0;
714         tx.tx_command = CMD_TX | CMD_INTR;
715         tx.tx_link = nopaddr;
716         tx.tx_tbdoffset = tbdaddr;
717         tbd.tbd_opts = TBD_EOL | skb->len;
718         tbd.tbd_link = I82586_NULL;
719         tbd.tbd_bufl = dataddr;
720         tbd.tbd_bufh = 0;
721         nop.nop_status = 0;
722         nop.nop_command = CMD_NOP;
723         nop.nop_link = nopaddr;
724
725         local_irq_save(flags);
726         ether1_writebuffer (dev, &tx, txaddr, TX_SIZE);
727         ether1_writebuffer (dev, &tbd, tbdaddr, TBD_SIZE);
728         ether1_writebuffer (dev, skb->data, dataddr, skb->len);
729         ether1_writebuffer (dev, &nop, nopaddr, NOP_SIZE);
730         tmp = priv(dev)->tx_link;
731         priv(dev)->tx_link = nopaddr;
732
733         /* now reset the previous nop pointer */
734         ether1_writew(dev, txaddr, tmp, nop_t, nop_link, NORMALIRQS);
735
736         local_irq_restore(flags);
737
738         /* handle transmit */
739         dev->trans_start = jiffies;
740
741         /* check to see if we have room for a full sized ether frame */
742         tmp = priv(dev)->tx_head;
743         tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
744         priv(dev)->tx_head = tmp;
745         dev_kfree_skb (skb);
746
747         if (tst == -1)
748                 netif_stop_queue(dev);
749
750  out:
751         return 0;
752 }
753
754 static void
755 ether1_xmit_done (struct net_device *dev)
756 {
757         nop_t nop;
758         int caddr, tst;
759
760         caddr = priv(dev)->tx_tail;
761
762 again:
763         ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
764
765         switch (nop.nop_command & CMD_MASK) {
766         case CMD_TDR:
767                 /* special case */
768                 if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
769                                 != (unsigned short)I82586_NULL) {
770                         ether1_writew(dev, SCB_CMDCUCSTART | SCB_CMDRXSTART, SCB_ADDR, scb_t,
771                                     scb_command, NORMALIRQS);
772                         writeb(CTRL_CA, REG_CONTROL);
773                 }
774                 priv(dev)->tx_tail = NOP_ADDR;
775                 return;
776
777         case CMD_NOP:
778                 if (nop.nop_link == caddr) {
779                         if (priv(dev)->initialising == 0)
780                                 printk (KERN_WARNING "%s: strange command complete with no tx command!\n", dev->name);
781                         else
782                                 priv(dev)->initialising = 0;
783                         return;
784                 }
785                 if (caddr == nop.nop_link)
786                         return;
787                 caddr = nop.nop_link;
788                 goto again;
789
790         case CMD_TX:
791                 if (nop.nop_status & STAT_COMPLETE)
792                         break;
793                 printk (KERN_ERR "%s: strange command complete without completed command\n", dev->name);
794                 priv(dev)->restart = 1;
795                 return;
796
797         default:
798                 printk (KERN_WARNING "%s: strange command %d complete! (offset %04X)", dev->name,
799                         nop.nop_command & CMD_MASK, caddr);
800                 priv(dev)->restart = 1;
801                 return;
802         }
803
804         while (nop.nop_status & STAT_COMPLETE) {
805                 if (nop.nop_status & STAT_OK) {
806                         priv(dev)->stats.tx_packets ++;
807                         priv(dev)->stats.collisions += (nop.nop_status & STAT_COLLISIONS);
808                 } else {
809                         priv(dev)->stats.tx_errors ++;
810
811                         if (nop.nop_status & STAT_COLLAFTERTX)
812                                 priv(dev)->stats.collisions ++;
813                         if (nop.nop_status & STAT_NOCARRIER)
814                                 priv(dev)->stats.tx_carrier_errors ++;
815                         if (nop.nop_status & STAT_TXLOSTCTS)
816                                 printk (KERN_WARNING "%s: cts lost\n", dev->name);
817                         if (nop.nop_status & STAT_TXSLOWDMA)
818                                 priv(dev)->stats.tx_fifo_errors ++;
819                         if (nop.nop_status & STAT_COLLEXCESSIVE)
820                                 priv(dev)->stats.collisions += 16;
821                 }
822
823                 if (nop.nop_link == caddr) {
824                         printk (KERN_ERR "%s: tx buffer chaining error: tx command points to itself\n", dev->name);
825                         break;
826                 }
827
828                 caddr = nop.nop_link;
829                 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
830                 if ((nop.nop_command & CMD_MASK) != CMD_NOP) {
831                         printk (KERN_ERR "%s: tx buffer chaining error: no nop after tx command\n", dev->name);
832                         break;
833                 }
834
835                 if (caddr == nop.nop_link)
836                         break;
837
838                 caddr = nop.nop_link;
839                 ether1_readbuffer (dev, &nop, caddr, NOP_SIZE);
840                 if ((nop.nop_command & CMD_MASK) != CMD_TX) {
841                         printk (KERN_ERR "%s: tx buffer chaining error: no tx command after nop\n", dev->name);
842                         break;
843                 }
844         }
845         priv(dev)->tx_tail = caddr;
846
847         caddr = priv(dev)->tx_head;
848         tst = ether1_txalloc (dev, TX_SIZE + TBD_SIZE + NOP_SIZE + ETH_FRAME_LEN);
849         priv(dev)->tx_head = caddr;
850         if (tst != -1)
851                 netif_wake_queue(dev);
852 }
853
854 static void
855 ether1_recv_done (struct net_device *dev)
856 {
857         int status;
858         int nexttail, rbdaddr;
859         rbd_t rbd;
860
861         do {
862                 status = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_status, NORMALIRQS);
863                 if ((status & RFD_COMPLETE) == 0)
864                         break;
865
866                 rbdaddr = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_rbdoffset, NORMALIRQS);
867                 ether1_readbuffer (dev, &rbd, rbdaddr, RBD_SIZE);
868
869                 if ((rbd.rbd_status & (RBD_EOF | RBD_ACNTVALID)) == (RBD_EOF | RBD_ACNTVALID)) {
870                         int length = rbd.rbd_status & RBD_ACNT;
871                         struct sk_buff *skb;
872
873                         length = (length + 1) & ~1;
874                         skb = dev_alloc_skb (length + 2);
875
876                         if (skb) {
877                                 skb_reserve (skb, 2);
878
879                                 ether1_readbuffer (dev, skb_put (skb, length), rbd.rbd_bufl, length);
880
881                                 skb->protocol = eth_type_trans (skb, dev);
882                                 netif_rx (skb);
883                                 priv(dev)->stats.rx_packets ++;
884                         } else
885                                 priv(dev)->stats.rx_dropped ++;
886                 } else {
887                         printk(KERN_WARNING "%s: %s\n", dev->name,
888                                 (rbd.rbd_status & RBD_EOF) ? "oversized packet" : "acnt not valid");
889                         priv(dev)->stats.rx_dropped ++;
890                 }
891
892                 nexttail = ether1_readw(dev, priv(dev)->rx_tail, rfd_t, rfd_link, NORMALIRQS);
893                 /* nexttail should be rx_head */
894                 if (nexttail != priv(dev)->rx_head)
895                         printk(KERN_ERR "%s: receiver buffer chaining error (%04X != %04X)\n",
896                                 dev->name, nexttail, priv(dev)->rx_head);
897                 ether1_writew(dev, RFD_CMDEL | RFD_CMDSUSPEND, nexttail, rfd_t, rfd_command, NORMALIRQS);
898                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_command, NORMALIRQS);
899                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_status, NORMALIRQS);
900                 ether1_writew(dev, 0, priv(dev)->rx_tail, rfd_t, rfd_rbdoffset, NORMALIRQS);
901         
902                 priv(dev)->rx_tail = nexttail;
903                 priv(dev)->rx_head = ether1_readw(dev, priv(dev)->rx_head, rfd_t, rfd_link, NORMALIRQS);
904         } while (1);
905 }
906
907 static irqreturn_t
908 ether1_interrupt (int irq, void *dev_id)
909 {
910         struct net_device *dev = (struct net_device *)dev_id;
911         int status;
912
913         status = ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS);
914
915         if (status) {
916                 ether1_writew(dev, status & (SCB_STRNR | SCB_STCNA | SCB_STFR | SCB_STCX),
917                             SCB_ADDR, scb_t, scb_command, NORMALIRQS);
918                 writeb(CTRL_CA | CTRL_ACK, REG_CONTROL);
919                 if (status & SCB_STCX) {
920                         ether1_xmit_done (dev);
921                 }
922                 if (status & SCB_STCNA) {
923                         if (priv(dev)->resetting == 0)
924                                 printk (KERN_WARNING "%s: CU went not ready ???\n", dev->name);
925                         else
926                                 priv(dev)->resetting += 1;
927                         if (ether1_readw(dev, SCB_ADDR, scb_t, scb_cbl_offset, NORMALIRQS)
928                                         != (unsigned short)I82586_NULL) {
929                                 ether1_writew(dev, SCB_CMDCUCSTART, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
930                                 writeb(CTRL_CA, REG_CONTROL);
931                         }
932                         if (priv(dev)->resetting == 2)
933                                 priv(dev)->resetting = 0;
934                 }
935                 if (status & SCB_STFR) {
936                         ether1_recv_done (dev);
937                 }
938                 if (status & SCB_STRNR) {
939                         if (ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS) & SCB_STRXSUSP) {
940                                 printk (KERN_WARNING "%s: RU went not ready: RU suspended\n", dev->name);
941                                 ether1_writew(dev, SCB_CMDRXRESUME, SCB_ADDR, scb_t, scb_command, NORMALIRQS);
942                                 writeb(CTRL_CA, REG_CONTROL);
943                                 priv(dev)->stats.rx_dropped ++; /* we suspended due to lack of buffer space */
944                         } else
945                                 printk(KERN_WARNING "%s: RU went not ready: %04X\n", dev->name,
946                                         ether1_readw(dev, SCB_ADDR, scb_t, scb_status, NORMALIRQS));
947                         printk (KERN_WARNING "RU ptr = %04X\n", ether1_readw(dev, SCB_ADDR, scb_t, scb_rfa_offset,
948                                                 NORMALIRQS));
949                 }
950         } else
951                 writeb(CTRL_ACK, REG_CONTROL);
952
953         return IRQ_HANDLED;
954 }
955
956 static int
957 ether1_close (struct net_device *dev)
958 {
959         ether1_reset (dev);
960
961         free_irq(dev->irq, dev);
962
963         return 0;
964 }
965
966 static struct net_device_stats *
967 ether1_getstats (struct net_device *dev)
968 {
969         return &priv(dev)->stats;
970 }
971
972 /*
973  * Set or clear the multicast filter for this adaptor.
974  * num_addrs == -1      Promiscuous mode, receive all packets.
975  * num_addrs == 0       Normal mode, clear multicast list.
976  * num_addrs > 0        Multicast mode, receive normal and MC packets, and do
977  *                      best-effort filtering.
978  */
979 static void
980 ether1_setmulticastlist (struct net_device *dev)
981 {
982 }
983
984 /* ------------------------------------------------------------------------- */
985
986 static void __devinit ether1_banner(void)
987 {
988         static unsigned int version_printed = 0;
989
990         if (net_debug && version_printed++ == 0)
991                 printk(KERN_INFO "%s", version);
992 }
993
994 static int __devinit
995 ether1_probe(struct expansion_card *ec, const struct ecard_id *id)
996 {
997         struct net_device *dev;
998         int i, ret = 0;
999         DECLARE_MAC_BUF(mac);
1000
1001         ether1_banner();
1002
1003         ret = ecard_request_resources(ec);
1004         if (ret)
1005                 goto out;
1006
1007         dev = alloc_etherdev(sizeof(struct ether1_priv));
1008         if (!dev) {
1009                 ret = -ENOMEM;
1010                 goto release;
1011         }
1012
1013         SET_NETDEV_DEV(dev, &ec->dev);
1014
1015         dev->irq = ec->irq;
1016         priv(dev)->base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
1017         if (!priv(dev)->base) {
1018                 ret = -ENOMEM;
1019                 goto free;
1020         }
1021
1022         if ((priv(dev)->bus_type = ether1_reset(dev)) == 0) {
1023                 ret = -ENODEV;
1024                 goto free;
1025         }
1026
1027         for (i = 0; i < 6; i++)
1028                 dev->dev_addr[i] = readb(IDPROM_ADDRESS + (i << 2));
1029
1030         if (ether1_init_2(dev)) {
1031                 ret = -ENODEV;
1032                 goto free;
1033         }
1034
1035         dev->open               = ether1_open;
1036         dev->stop               = ether1_close;
1037         dev->hard_start_xmit    = ether1_sendpacket;
1038         dev->get_stats          = ether1_getstats;
1039         dev->set_multicast_list = ether1_setmulticastlist;
1040         dev->tx_timeout         = ether1_timeout;
1041         dev->watchdog_timeo     = 5 * HZ / 100;
1042
1043         ret = register_netdev(dev);
1044         if (ret)
1045                 goto free;
1046
1047         printk(KERN_INFO "%s: ether1 in slot %d, %s\n",
1048                 dev->name, ec->slot_no, print_mac(mac, dev->dev_addr));
1049     
1050         ecard_set_drvdata(ec, dev);
1051         return 0;
1052
1053  free:
1054         free_netdev(dev);
1055  release:
1056         ecard_release_resources(ec);
1057  out:
1058         return ret;
1059 }
1060
1061 static void __devexit ether1_remove(struct expansion_card *ec)
1062 {
1063         struct net_device *dev = ecard_get_drvdata(ec);
1064
1065         ecard_set_drvdata(ec, NULL);    
1066
1067         unregister_netdev(dev);
1068         free_netdev(dev);
1069         ecard_release_resources(ec);
1070 }
1071
1072 static const struct ecard_id ether1_ids[] = {
1073         { MANU_ACORN, PROD_ACORN_ETHER1 },
1074         { 0xffff, 0xffff }
1075 };
1076
1077 static struct ecard_driver ether1_driver = {
1078         .probe          = ether1_probe,
1079         .remove         = __devexit_p(ether1_remove),
1080         .id_table       = ether1_ids,
1081         .drv = {
1082                 .name   = "ether1",
1083         },
1084 };
1085
1086 static int __init ether1_init(void)
1087 {
1088         return ecard_register_driver(&ether1_driver);
1089 }
1090
1091 static void __exit ether1_exit(void)
1092 {
1093         ecard_remove_driver(&ether1_driver);
1094 }
1095
1096 module_init(ether1_init);
1097 module_exit(ether1_exit);
1098
1099 MODULE_LICENSE("GPL");