ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / ac3200.c
1 /* ac3200.c: A driver for the Ansel Communications EISA ethernet adaptor. */
2 /*
3         Written 1993, 1994 by Donald Becker.
4         Copyright 1993 United States Government as represented by the Director,
5         National Security Agency.  This software may only be used and distributed
6         according to the terms of the GNU General Public License as modified by SRC,
7         incorporated herein by reference.
8
9         The author may be reached as becker@scyld.com, or C/O
10         Scyld Computing Corporation
11         410 Severn Ave., Suite 210
12         Annapolis MD 21403
13
14         This is driver for the Ansel Communications Model 3200 EISA Ethernet LAN
15         Adapter.  The programming information is from the users manual, as related
16         by glee@ardnassak.math.clemson.edu.
17
18         Changelog:
19
20         Paul Gortmaker 05/98    : add support for shared mem above 1MB.
21
22   */
23
24 static const char version[] =
25         "ac3200.c:v1.01 7/1/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
26
27 #include <linux/module.h>
28 #include <linux/eisa.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/init.h>
35
36 #include <asm/system.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39
40 #include "8390.h"
41
42 /* Offsets from the base address. */
43 #define AC_NIC_BASE     0x00
44 #define AC_SA_PROM      0x16                    /* The station address PROM. */
45 #define AC_ADDR0        0x00                    /* Prefix station address values. */
46 #define AC_ADDR1        0x40                    
47 #define AC_ADDR2        0x90
48 #define AC_ID_PORT      0xC80
49 #define AC_EISA_ID      0x0110d305
50 #define AC_RESET_PORT   0xC84
51 #define AC_RESET        0x00
52 #define AC_ENABLE       0x01
53 #define AC_CONFIG       0xC90   /* The configuration port. */
54
55 #define AC_IO_EXTENT 0x20
56                                 /* Actually accessed is:
57                                                                  * AC_NIC_BASE (0-15)
58                                                                  * AC_SA_PROM (0-5)
59                                                                  * AC_ID_PORT (0-3)
60                                                                  * AC_RESET_PORT
61                                                                  * AC_CONFIG
62                                                                  */
63
64 /* Decoding of the configuration register. */
65 static unsigned char config2irqmap[8] __initdata = {15, 12, 11, 10, 9, 7, 5, 3};
66 static int addrmap[8] =
67 {0xFF0000, 0xFE0000, 0xFD0000, 0xFFF0000, 0xFFE0000, 0xFFC0000,  0xD0000, 0 };
68 static const char *port_name[4] = { "10baseT", "invalid", "AUI", "10base2"};
69
70 #define config2irq(configval)   config2irqmap[((configval) >> 3) & 7]
71 #define config2mem(configval)   addrmap[(configval) & 7]
72 #define config2name(configval)  port_name[((configval) >> 6) & 3]
73
74 /* First and last 8390 pages. */
75 #define AC_START_PG             0x00    /* First page of 8390 TX buffer */
76 #define AC_STOP_PG              0x80    /* Last page +1 of the 8390 RX ring */
77
78 static int ac_probe1(int ioaddr, struct net_device *dev);
79
80 static int ac_open(struct net_device *dev);
81 static void ac_reset_8390(struct net_device *dev);
82 static void ac_block_input(struct net_device *dev, int count,
83                                         struct sk_buff *skb, int ring_offset);
84 static void ac_block_output(struct net_device *dev, const int count,
85                                                         const unsigned char *buf, const int start_page);
86 static void ac_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
87                                         int ring_page);
88
89 static int ac_close_card(struct net_device *dev);
90 \f
91
92 /*      Probe for the AC3200.
93
94         The AC3200 can be identified by either the EISA configuration registers,
95         or the unique value in the station address PROM.
96         */
97
98 static int __init do_ac3200_probe(struct net_device *dev)
99 {
100         unsigned short ioaddr = dev->base_addr;
101         int irq = dev->irq;
102         int mem_start = dev->mem_start;
103
104         SET_MODULE_OWNER(dev);
105
106         if (ioaddr > 0x1ff)             /* Check a single specified location. */
107                 return ac_probe1(ioaddr, dev);
108         else if (ioaddr > 0)            /* Don't probe at all. */
109                 return -ENXIO;
110
111         if ( ! EISA_bus)
112                 return -ENXIO;
113
114         for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
115                 if (ac_probe1(ioaddr, dev) == 0)
116                         return 0;
117                 dev->irq = irq;
118                 dev->mem_start = mem_start;
119         }
120
121         return -ENODEV;
122 }
123
124 static void cleanup_card(struct net_device *dev)
125 {
126         /* Someday free_irq may be in ac_close_card() */
127         free_irq(dev->irq, dev);
128         release_region(dev->base_addr, AC_IO_EXTENT);
129         if (ei_status.reg0)
130                 iounmap((void *)dev->mem_start);
131 }
132
133 struct net_device * __init ac3200_probe(int unit)
134 {
135         struct net_device *dev = alloc_ei_netdev();
136         int err;
137
138         if (!dev)
139                 return ERR_PTR(-ENOMEM);
140
141         sprintf(dev->name, "eth%d", unit);
142         netdev_boot_setup_check(dev);
143
144         err = do_ac3200_probe(dev);
145         if (err)
146                 goto out;
147         err = register_netdev(dev);
148         if (err)
149                 goto out1;
150         return dev;
151 out1:
152         cleanup_card(dev);
153 out:
154         free_netdev(dev);
155         return ERR_PTR(err);
156 }
157
158 static int __init ac_probe1(int ioaddr, struct net_device *dev)
159 {
160         int i, retval;
161
162         if (!request_region(ioaddr, AC_IO_EXTENT, dev->name))
163                 return -EBUSY;
164
165         if (inb_p(ioaddr + AC_ID_PORT) == 0xff) {
166                 retval = -ENODEV;
167                 goto out;
168         }
169
170         if (inl(ioaddr + AC_ID_PORT) != AC_EISA_ID) {
171                 retval = -ENODEV;
172                 goto out;
173         }
174
175 #ifndef final_version
176         printk(KERN_DEBUG "AC3200 ethercard configuration register is %#02x,"
177                    " EISA ID %02x %02x %02x %02x.\n", inb(ioaddr + AC_CONFIG),
178                    inb(ioaddr + AC_ID_PORT + 0), inb(ioaddr + AC_ID_PORT + 1),
179                    inb(ioaddr + AC_ID_PORT + 2), inb(ioaddr + AC_ID_PORT + 3));
180 #endif
181
182         printk("AC3200 in EISA slot %d, node", ioaddr/0x1000);
183         for(i = 0; i < 6; i++)
184                 printk(" %02x", dev->dev_addr[i] = inb(ioaddr + AC_SA_PROM + i));
185
186 #if 0
187         /* Check the vendor ID/prefix. Redundant after checking the EISA ID */
188         if (inb(ioaddr + AC_SA_PROM + 0) != AC_ADDR0
189                 || inb(ioaddr + AC_SA_PROM + 1) != AC_ADDR1
190                 || inb(ioaddr + AC_SA_PROM + 2) != AC_ADDR2 ) {
191                 printk(", not found (invalid prefix).\n");
192                 retval = -ENODEV;
193                 goto out;
194         }
195 #endif
196
197         /* Assign and allocate the interrupt now. */
198         if (dev->irq == 0) {
199                 dev->irq = config2irq(inb(ioaddr + AC_CONFIG));
200                 printk(", using");
201         } else {
202                 dev->irq = irq_canonicalize(dev->irq);
203                 printk(", assigning");
204         }
205
206         retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev);
207         if (retval) {
208                 printk (" nothing! Unable to get IRQ %d.\n", dev->irq);
209                 goto out1;
210         }
211
212         printk(" IRQ %d, %s port\n", dev->irq, port_name[dev->if_port]);
213
214         dev->base_addr = ioaddr;
215
216 #ifdef notyet
217         if (dev->mem_start)     {               /* Override the value from the board. */
218                 for (i = 0; i < 7; i++)
219                         if (addrmap[i] == dev->mem_start)
220                                 break;
221                 if (i >= 7)
222                         i = 0;
223                 outb((inb(ioaddr + AC_CONFIG) & ~7) | i, ioaddr + AC_CONFIG);
224         }
225 #endif
226
227         dev->if_port = inb(ioaddr + AC_CONFIG) >> 6;
228         dev->mem_start = config2mem(inb(ioaddr + AC_CONFIG));
229
230         printk("%s: AC3200 at %#3x with %dkB memory at physical address %#lx.\n", 
231                         dev->name, ioaddr, AC_STOP_PG/4, dev->mem_start);
232
233         /*
234          *  BEWARE!! Some dain-bramaged EISA SCUs will allow you to put
235          *  the card mem within the region covered by `normal' RAM  !!!
236          */
237         if (dev->mem_start > 1024*1024) {       /* phys addr > 1MB */
238                 if (dev->mem_start < virt_to_phys(high_memory)) {
239                         printk(KERN_CRIT "ac3200.c: Card RAM overlaps with normal memory!!!\n");
240                         printk(KERN_CRIT "ac3200.c: Use EISA SCU to set card memory below 1MB,\n");
241                         printk(KERN_CRIT "ac3200.c: or to an address above 0x%lx.\n", virt_to_phys(high_memory));
242                         printk(KERN_CRIT "ac3200.c: Driver NOT installed.\n");
243                         retval = -EINVAL;
244                         goto out1;
245                 }
246                 dev->mem_start = (unsigned long)ioremap(dev->mem_start, AC_STOP_PG*0x100);
247                 if (dev->mem_start == 0) {
248                         printk(KERN_ERR "ac3200.c: Unable to remap card memory above 1MB !!\n");
249                         printk(KERN_ERR "ac3200.c: Try using EISA SCU to set memory below 1MB.\n");
250                         printk(KERN_ERR "ac3200.c: Driver NOT installed.\n");
251                         retval = -EINVAL;
252                         goto out1;
253                 }
254                 ei_status.reg0 = 1;     /* Use as remap flag */
255                 printk("ac3200.c: remapped %dkB card memory to virtual address %#lx\n",
256                                 AC_STOP_PG/4, dev->mem_start);
257         }
258
259         ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
260         dev->mem_end = ei_status.rmem_end = dev->mem_start
261                 + (AC_STOP_PG - AC_START_PG)*256;
262
263         ei_status.name = "AC3200";
264         ei_status.tx_start_page = AC_START_PG;
265         ei_status.rx_start_page = AC_START_PG + TX_PAGES;
266         ei_status.stop_page = AC_STOP_PG;
267         ei_status.word16 = 1;
268
269         if (ei_debug > 0)
270                 printk(version);
271
272         ei_status.reset_8390 = &ac_reset_8390;
273         ei_status.block_input = &ac_block_input;
274         ei_status.block_output = &ac_block_output;
275         ei_status.get_8390_hdr = &ac_get_8390_hdr;
276
277         dev->open = &ac_open;
278         dev->stop = &ac_close_card;
279 #ifdef CONFIG_NET_POLL_CONTROLLER
280         dev->poll_controller = ei_poll;
281 #endif
282         NS8390_init(dev, 0);
283         return 0;
284 out1:
285         free_irq(dev->irq, dev);
286 out:
287         release_region(ioaddr, AC_IO_EXTENT);
288         return retval;
289 }
290
291 static int ac_open(struct net_device *dev)
292 {
293 #ifdef notyet
294         /* Someday we may enable the IRQ and shared memory here. */
295         int ioaddr = dev->base_addr;
296 #endif
297
298         ei_open(dev);
299         return 0;
300 }
301
302 static void ac_reset_8390(struct net_device *dev)
303 {
304         ushort ioaddr = dev->base_addr;
305
306         outb(AC_RESET, ioaddr + AC_RESET_PORT);
307         if (ei_debug > 1) printk("resetting AC3200, t=%ld...", jiffies);
308
309         ei_status.txing = 0;
310         outb(AC_ENABLE, ioaddr + AC_RESET_PORT);
311         if (ei_debug > 1) printk("reset done\n");
312
313         return;
314 }
315
316 /* Grab the 8390 specific header. Similar to the block_input routine, but
317    we don't need to be concerned with ring wrap as the header will be at
318    the start of a page, so we optimize accordingly. */
319
320 static void
321 ac_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
322 {
323         unsigned long hdr_start = dev->mem_start + ((ring_page - AC_START_PG)<<8);
324         isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
325 }
326
327 /*  Block input and output are easy on shared memory ethercards, the only
328         complication is when the ring buffer wraps. */
329
330 static void ac_block_input(struct net_device *dev, int count, struct sk_buff *skb,
331                                                   int ring_offset)
332 {
333         unsigned long xfer_start = dev->mem_start + ring_offset - (AC_START_PG<<8);
334
335         if (xfer_start + count > ei_status.rmem_end) {
336                 /* We must wrap the input move. */
337                 int semi_count = ei_status.rmem_end - xfer_start;
338                 isa_memcpy_fromio(skb->data, xfer_start, semi_count);
339                 count -= semi_count;
340                 isa_memcpy_fromio(skb->data + semi_count, ei_status.rmem_start, count);
341         } else {
342                 /* Packet is in one chunk -- we can copy + cksum. */
343                 isa_eth_io_copy_and_sum(skb, xfer_start, count, 0);
344         }
345 }
346
347 static void ac_block_output(struct net_device *dev, int count,
348                                                         const unsigned char *buf, int start_page)
349 {
350         unsigned long shmem = dev->mem_start + ((start_page - AC_START_PG)<<8);
351
352         isa_memcpy_toio(shmem, buf, count);
353 }
354
355 static int ac_close_card(struct net_device *dev)
356 {
357         if (ei_debug > 1)
358                 printk("%s: Shutting down ethercard.\n", dev->name);
359
360 #ifdef notyet
361         /* We should someday disable shared memory and interrupts. */
362         outb(0x00, ioaddr + 6); /* Disable interrupts. */
363         free_irq(dev->irq, dev);
364 #endif
365
366         ei_close(dev);
367         return 0;
368 }
369
370 #ifdef MODULE
371 #define MAX_AC32_CARDS  4       /* Max number of AC32 cards per module */
372 static struct net_device *dev_ac32[MAX_AC32_CARDS];
373 static int io[MAX_AC32_CARDS];
374 static int irq[MAX_AC32_CARDS];
375 static int mem[MAX_AC32_CARDS];
376 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_AC32_CARDS) "i");
377 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_AC32_CARDS) "i");
378 MODULE_PARM(mem, "1-" __MODULE_STRING(MAX_AC32_CARDS) "i");
379 MODULE_PARM_DESC(io, "I/O base address(es)");
380 MODULE_PARM_DESC(irq, "IRQ number(s)");
381 MODULE_PARM_DESC(mem, "Memory base address(es)");
382 MODULE_DESCRIPTION("Ansel AC3200 EISA ethernet driver");
383 MODULE_LICENSE("GPL");
384
385 int
386 init_module(void)
387 {
388         struct net_device *dev;
389         int this_dev, found = 0;
390
391         for (this_dev = 0; this_dev < MAX_AC32_CARDS; this_dev++) {
392                 if (io[this_dev] == 0 && this_dev != 0)
393                         break;
394                 dev = alloc_ei_netdev();
395                 if (!dev)
396                         break;
397                 dev->irq = irq[this_dev];
398                 dev->base_addr = io[this_dev];
399                 dev->mem_start = mem[this_dev];         /* Currently ignored by driver */
400                 if (do_ac3200_probe(dev) == 0) {
401                         if (register_netdev(dev) == 0) {
402                                 dev_ac32[found++] = dev;
403                                 continue;
404                         }
405                         cleanup_card(dev);
406                 }
407                 free_netdev(dev);
408                 printk(KERN_WARNING "ac3200.c: No ac3200 card found (i/o = 0x%x).\n", io[this_dev]);
409                 break;
410         }
411         if (found)
412                 return 0;
413         return -ENXIO;
414 }
415
416 void
417 cleanup_module(void)
418 {
419         int this_dev;
420
421         for (this_dev = 0; this_dev < MAX_AC32_CARDS; this_dev++) {
422                 struct net_device *dev = dev_ac32[this_dev];
423                 if (dev) {
424                         unregister_netdev(dev);
425                         cleanup_card(dev);
426                         free_netdev(dev);
427                 }
428         }
429 }
430 #endif /* MODULE */