make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / net / ariadne2.c
1 /*
2  *  Amiga Linux/m68k and Linux/PPC Ariadne II and X-Surf Ethernet Driver
3  *
4  *  (C) Copyright 1998-2000 by some Elitist 680x0 Users(TM)
5  *
6  *  ---------------------------------------------------------------------------
7  *
8  *  This program is based on all the other NE2000 drivers for Linux
9  *
10  *  ---------------------------------------------------------------------------
11  *
12  *  This file is subject to the terms and conditions of the GNU General Public
13  *  License.  See the file COPYING in the main directory of the Linux
14  *  distribution for more details.
15  *
16  *  ---------------------------------------------------------------------------
17  *
18  *  The Ariadne II and X-Surf are Zorro-II boards containing Realtek RTL8019AS
19  *  Ethernet Controllers.
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/zorro.h>
31
32 #include <asm/system.h>
33
34 #include <asm/irq.h>
35 #include <asm/amigaints.h>
36 #include <asm/amigahw.h>
37
38 #include "8390.h"
39
40
41 #define NE_BASE         (dev->base_addr)
42 #define NE_CMD          (0x00*2)
43 #define NE_DATAPORT     (0x10*2)        /* NatSemi-defined port window offset. */
44 #define NE_RESET        (0x1f*2)        /* Issue a read to reset, a write to clear. */
45 #define NE_IO_EXTENT    (0x20*2)
46
47 #define NE_EN0_ISR      (0x07*2)
48 #define NE_EN0_DCFG     (0x0e*2)
49
50 #define NE_EN0_RSARLO   (0x08*2)
51 #define NE_EN0_RSARHI   (0x09*2)
52 #define NE_EN0_RCNTLO   (0x0a*2)
53 #define NE_EN0_RXCR     (0x0c*2)
54 #define NE_EN0_TXCR     (0x0d*2)
55 #define NE_EN0_RCNTHI   (0x0b*2)
56 #define NE_EN0_IMR      (0x0f*2)
57
58 #define NESM_START_PG   0x40    /* First page of TX buffer */
59 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
60
61
62 #define WORDSWAP(a)     ((((a)>>8)&0xff) | ((a)<<8))
63
64 #ifdef MODULE
65 static struct net_device *root_ariadne2_dev;
66 #endif
67
68 static const struct card_info {
69     zorro_id id;
70     const char *name;
71     unsigned int offset;
72 } cards[] __initdata = {
73     { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE2, "Ariadne II", 0x0600 },
74     { ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, "X-Surf", 0x8600 },
75 };
76
77 static int __init ariadne2_probe(void);
78 static int __init ariadne2_init(struct net_device *dev, unsigned long board,
79                                 const char *name, unsigned long ioaddr);
80 static int ariadne2_open(struct net_device *dev);
81 static int ariadne2_close(struct net_device *dev);
82 static void ariadne2_reset_8390(struct net_device *dev);
83 static void ariadne2_get_8390_hdr(struct net_device *dev,
84                                   struct e8390_pkt_hdr *hdr, int ring_page);
85 static void ariadne2_block_input(struct net_device *dev, int count,
86                                  struct sk_buff *skb, int ring_offset);
87 static void ariadne2_block_output(struct net_device *dev, const int count,
88                                   const unsigned char *buf,
89                                   const int start_page);
90 static void __exit ariadne2_cleanup(void);
91
92 static int __init ariadne2_probe(void)
93 {
94     struct net_device *dev;
95     struct zorro_dev *z = NULL;
96     unsigned long board, ioaddr;
97     int err = -ENODEV;
98     int i;
99
100     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
101         for (i = ARRAY_SIZE(cards)-1; i >= 0; i--)
102             if (z->id == cards[i].id)
103                 break;
104         if (i < 0)
105             continue;
106         board = z->resource.start;
107         ioaddr = board+cards[i].offset;
108         dev = init_etherdev(0, 0);
109         SET_MODULE_OWNER(dev);
110         if (!dev)
111             return -ENOMEM;
112         if (!request_mem_region(ioaddr, NE_IO_EXTENT*2, dev->name)) {
113             kfree(dev);
114             continue;
115         }
116         if ((err = ariadne2_init(dev, board, cards[i].name,
117                                  ZTWO_VADDR(ioaddr)))) {
118             release_mem_region(ioaddr, NE_IO_EXTENT*2);
119             kfree(dev);
120             return err;
121         }
122         err = 0;
123     }
124
125     if (err == -ENODEV)
126         printk("No Ariadne II or X-Surf ethernet card found.\n");
127     return err;
128 }
129
130 static int __init ariadne2_init(struct net_device *dev, unsigned long board,
131                                 const char *name, unsigned long ioaddr)
132 {
133     int i;
134     unsigned char SA_prom[32];
135     int start_page, stop_page;
136     static u32 ariadne2_offsets[16] = {
137         0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
138         0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e,
139     };
140
141     /* Reset card. Who knows what dain-bramaged state it was left in. */
142     {
143         unsigned long reset_start_time = jiffies;
144
145         z_writeb(z_readb(ioaddr + NE_RESET), ioaddr + NE_RESET);
146
147         while ((z_readb(ioaddr + NE_EN0_ISR) & ENISR_RESET) == 0)
148             if (jiffies - reset_start_time > 2*HZ/100) {
149                 printk(" not found (no reset ack).\n");
150                 return -ENODEV;
151             }
152
153         z_writeb(0xff, ioaddr + NE_EN0_ISR);            /* Ack all intr. */
154     }
155
156     /* Read the 16 bytes of station address PROM.
157        We must first initialize registers, similar to NS8390_init(eifdev, 0).
158        We can't reliably read the SAPROM address without this.
159        (I learned the hard way!). */
160     {
161         struct {
162             u32 value;
163             u32 offset;
164         } program_seq[] = {
165             {E8390_NODMA+E8390_PAGE0+E8390_STOP, NE_CMD}, /* Select page 0*/
166             {0x48,      NE_EN0_DCFG},   /* Set byte-wide (0x48) access. */
167             {0x00,      NE_EN0_RCNTLO}, /* Clear the count regs. */
168             {0x00,      NE_EN0_RCNTHI},
169             {0x00,      NE_EN0_IMR},    /* Mask completion irq. */
170             {0xFF,      NE_EN0_ISR},
171             {E8390_RXOFF, NE_EN0_RXCR}, /* 0x20  Set to monitor */
172             {E8390_TXOFF, NE_EN0_TXCR}, /* 0x02  and loopback mode. */
173             {32,        NE_EN0_RCNTLO},
174             {0x00,      NE_EN0_RCNTHI},
175             {0x00,      NE_EN0_RSARLO}, /* DMA starting at 0x0000. */
176             {0x00,      NE_EN0_RSARHI},
177             {E8390_RREAD+E8390_START, NE_CMD},
178         };
179         for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) {
180             z_writeb(program_seq[i].value, ioaddr + program_seq[i].offset);
181         }
182     }
183     for (i = 0; i < 16; i++) {
184         SA_prom[i] = z_readb(ioaddr + NE_DATAPORT);
185         (void)z_readb(ioaddr + NE_DATAPORT);
186     }
187
188     /* We must set the 8390 for word mode. */
189     z_writeb(0x49, ioaddr + NE_EN0_DCFG);
190     start_page = NESM_START_PG;
191     stop_page = NESM_STOP_PG;
192
193     dev->base_addr = ioaddr;
194     dev->irq = IRQ_AMIGA_PORTS;
195
196     /* Install the Interrupt handler */
197     i = request_irq(IRQ_AMIGA_PORTS, ei_interrupt, SA_SHIRQ, dev->name, dev);
198     if (i) return i;
199
200     /* Allocate dev->priv and fill in 8390 specific dev fields. */
201     if (ethdev_init(dev)) {
202         printk("Unable to get memory for dev->priv.\n");
203         return -ENOMEM;
204     }
205
206     for(i = 0; i < ETHER_ADDR_LEN; i++) {
207 #ifdef DEBUG
208         printk(" %2.2x", SA_prom[i]);
209 #endif
210         dev->dev_addr[i] = SA_prom[i];
211     }
212
213     printk("%s: %s at 0x%08lx, Ethernet Address "
214            "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, name, board,
215            dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
216            dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
217
218     ei_status.name = name;
219     ei_status.tx_start_page = start_page;
220     ei_status.stop_page = stop_page;
221     ei_status.word16 = 1;
222
223     ei_status.rx_start_page = start_page + TX_PAGES;
224
225     ei_status.reset_8390 = &ariadne2_reset_8390;
226     ei_status.block_input = &ariadne2_block_input;
227     ei_status.block_output = &ariadne2_block_output;
228     ei_status.get_8390_hdr = &ariadne2_get_8390_hdr;
229     ei_status.reg_offset = ariadne2_offsets;
230     dev->open = &ariadne2_open;
231     dev->stop = &ariadne2_close;
232 #ifdef MODULE
233     ei_status.priv = (unsigned long)root_ariadne2_dev;
234     root_ariadne2_dev = dev;
235 #endif
236     NS8390_init(dev, 0);
237     return 0;
238 }
239
240 static int ariadne2_open(struct net_device *dev)
241 {
242     ei_open(dev);
243     return 0;
244 }
245
246 static int ariadne2_close(struct net_device *dev)
247 {
248     if (ei_debug > 1)
249         printk("%s: Shutting down ethercard.\n", dev->name);
250     ei_close(dev);
251     return 0;
252 }
253
254 /* Hard reset the card.  This used to pause for the same period that a
255    8390 reset command required, but that shouldn't be necessary. */
256 static void ariadne2_reset_8390(struct net_device *dev)
257 {
258     unsigned long reset_start_time = jiffies;
259
260     if (ei_debug > 1)
261         printk("resetting the 8390 t=%ld...", jiffies);
262
263     z_writeb(z_readb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
264
265     ei_status.txing = 0;
266     ei_status.dmaing = 0;
267
268     /* This check _should_not_ be necessary, omit eventually. */
269     while ((z_readb(NE_BASE+NE_EN0_ISR) & ENISR_RESET) == 0)
270         if (jiffies - reset_start_time > 2*HZ/100) {
271             printk("%s: ne_reset_8390() did not complete.\n", dev->name);
272             break;
273         }
274     z_writeb(ENISR_RESET, NE_BASE + NE_EN0_ISR);        /* Ack intr. */
275 }
276
277 /* Grab the 8390 specific header. Similar to the block_input routine, but
278    we don't need to be concerned with ring wrap as the header will be at
279    the start of a page, so we optimize accordingly. */
280
281 static void ariadne2_get_8390_hdr(struct net_device *dev,
282                                   struct e8390_pkt_hdr *hdr, int ring_page)
283 {
284     int nic_base = dev->base_addr;
285     int cnt;
286     short *ptrs;
287
288     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
289     if (ei_status.dmaing) {
290         printk("%s: DMAing conflict in ne_get_8390_hdr "
291            "[DMAstat:%d][irqlock:%d].\n", dev->name, ei_status.dmaing,
292            ei_status.irqlock);
293         return;
294     }
295
296     ei_status.dmaing |= 0x01;
297     z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
298     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
299     z_writeb(sizeof(struct e8390_pkt_hdr), nic_base + NE_EN0_RCNTLO);
300     z_writeb(0, nic_base + NE_EN0_RCNTHI);
301     z_writeb(0, nic_base + NE_EN0_RSARLO);              /* On page boundary */
302     z_writeb(ring_page, nic_base + NE_EN0_RSARHI);
303     z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
304
305     ptrs = (short*)hdr;
306     for (cnt = 0; cnt < (sizeof(struct e8390_pkt_hdr)>>1); cnt++)
307         *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
308
309     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
310
311     hdr->count = WORDSWAP(hdr->count);
312
313     ei_status.dmaing &= ~0x01;
314 }
315
316 /* Block input and output, similar to the Crynwr packet driver.  If you
317    are porting to a new ethercard, look at the packet driver source for hints.
318    The NEx000 doesn't share the on-board packet memory -- you have to put
319    the packet out through the "remote DMA" dataport using z_writeb. */
320
321 static void ariadne2_block_input(struct net_device *dev, int count,
322                                  struct sk_buff *skb, int ring_offset)
323 {
324     int nic_base = dev->base_addr;
325     char *buf = skb->data;
326     short *ptrs;
327     int cnt;
328
329     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
330     if (ei_status.dmaing) {
331         printk("%s: DMAing conflict in ne_block_input "
332            "[DMAstat:%d][irqlock:%d].\n",
333            dev->name, ei_status.dmaing, ei_status.irqlock);
334         return;
335     }
336     ei_status.dmaing |= 0x01;
337     z_writeb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
338     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
339     z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
340     z_writeb(count >> 8, nic_base + NE_EN0_RCNTHI);
341     z_writeb(ring_offset & 0xff, nic_base + NE_EN0_RSARLO);
342     z_writeb(ring_offset >> 8, nic_base + NE_EN0_RSARHI);
343     z_writeb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
344     ptrs = (short*)buf;
345     for (cnt = 0; cnt < (count>>1); cnt++)
346         *ptrs++ = z_readw(NE_BASE + NE_DATAPORT);
347     if (count & 0x01)
348         buf[count-1] = z_readb(NE_BASE + NE_DATAPORT);
349
350     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
351     ei_status.dmaing &= ~0x01;
352 }
353
354 static void ariadne2_block_output(struct net_device *dev, int count,
355                                   const unsigned char *buf,
356                                   const int start_page)
357 {
358     int nic_base = NE_BASE;
359     unsigned long dma_start;
360     short *ptrs;
361     int cnt;
362
363     /* Round the count up for word writes.  Do we need to do this?
364        What effect will an odd byte count have on the 8390?
365        I should check someday. */
366     if (count & 0x01)
367         count++;
368
369     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
370     if (ei_status.dmaing) {
371         printk("%s: DMAing conflict in ne_block_output."
372            "[DMAstat:%d][irqlock:%d]\n", dev->name, ei_status.dmaing,
373            ei_status.irqlock);
374         return;
375     }
376     ei_status.dmaing |= 0x01;
377     /* We should already be in page 0, but to be safe... */
378     z_writeb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
379
380     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR);
381
382    /* Now the normal output. */
383     z_writeb(count & 0xff, nic_base + NE_EN0_RCNTLO);
384     z_writeb(count >> 8,   nic_base + NE_EN0_RCNTHI);
385     z_writeb(0x00, nic_base + NE_EN0_RSARLO);
386     z_writeb(start_page, nic_base + NE_EN0_RSARHI);
387
388     z_writeb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
389     ptrs = (short*)buf;
390     for (cnt = 0; cnt < count>>1; cnt++)
391         z_writew(*ptrs++, NE_BASE+NE_DATAPORT);
392
393     dma_start = jiffies;
394
395     while ((z_readb(NE_BASE + NE_EN0_ISR) & ENISR_RDC) == 0)
396         if (jiffies - dma_start > 2*HZ/100) {           /* 20ms */
397                 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
398                 ariadne2_reset_8390(dev);
399                 NS8390_init(dev,1);
400                 break;
401         }
402
403     z_writeb(ENISR_RDC, nic_base + NE_EN0_ISR); /* Ack intr. */
404     ei_status.dmaing &= ~0x01;
405     return;
406 }
407
408 static void __exit ariadne2_cleanup(void)
409 {
410 #ifdef MODULE
411     struct net_device *dev, *next;
412
413     while ((dev = root_ariadne2_dev)) {
414         next = (struct net_device *)(ei_status.priv);
415         unregister_netdev(dev);
416         free_irq(IRQ_AMIGA_PORTS, dev);
417         release_mem_region(ZTWO_PADDR(dev->base_addr), NE_IO_EXTENT*2);
418         kfree(dev);
419         root_ariadne2_dev = next;
420     }
421 #endif
422 }
423
424 module_init(ariadne2_probe);
425 module_exit(ariadne2_cleanup);
426
427 MODULE_LICENSE("GPL");