ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / es3210.c
1 /*
2         es3210.c
3
4         Linux driver for Racal-Interlan ES3210 EISA Network Adapter
5
6         Copyright (C) 1996, Paul Gortmaker.
7
8         This software may be used and distributed according to the terms
9         of the GNU General Public License, incorporated herein by reference.
10
11         Information and Code Sources:
12
13         1) The existing myriad of Linux 8390 drivers written by Donald Becker.
14
15         2) Once again Russ Nelson's asm packet driver provided additional info.
16
17         3) Info for getting IRQ and sh-mem gleaned from the EISA cfg files.
18            Too bad it doesn't work -- see below.
19
20         The ES3210 is an EISA shared memory NS8390 implementation. Note
21         that all memory copies to/from the board must be 32bit transfers.
22         Which rules out using eth_io_copy_and_sum() in this driver.
23
24         Apparently there are two slightly different revisions of the
25         card, since there are two distinct EISA cfg files (!rii0101.cfg
26         and !rii0102.cfg) One has media select in the cfg file and the
27         other doesn't. Hopefully this will work with either.
28
29         That is about all I can tell you about it, having never actually
30         even seen one of these cards. :)  Try http://www.interlan.com
31         if you want more info.
32
33         Thanks go to Mark Salazar for testing v0.02 of this driver.
34
35         Bugs, to-fix, etc:
36
37         1) The EISA cfg ports that are *supposed* to have the IRQ and shared
38            mem values just read 0xff all the time. Hrrmpf. Apparently the
39            same happens with the packet driver as the code for reading
40            these registers is disabled there. In the meantime, boot with:
41            ether=<IRQ>,0,0x<shared_mem_addr>,eth0 to override the IRQ and
42            shared memory detection. (The i/o port detection is okay.)
43
44         2) Module support currently untested. Probably works though.
45
46 */
47
48 static const char version[] =
49         "es3210.c: Driver revision v0.03, 14/09/96\n";
50
51 #include <linux/module.h>
52 #include <linux/eisa.h>
53 #include <linux/kernel.h>
54 #include <linux/errno.h>
55 #include <linux/string.h>
56 #include <linux/init.h>
57 #include <linux/netdevice.h>
58 #include <linux/etherdevice.h>
59
60 #include <asm/io.h>
61 #include <asm/system.h>
62
63 #include "8390.h"
64
65 static int es_probe1(struct net_device *dev, int ioaddr);
66
67 static int es_open(struct net_device *dev);
68 static int es_close(struct net_device *dev);
69
70 static void es_reset_8390(struct net_device *dev);
71
72 static void es_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page);
73 static void es_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset);
74 static void es_block_output(struct net_device *dev, int count, const unsigned char *buf, int start_page);
75
76 #define ES_START_PG     0x00    /* First page of TX buffer              */
77 #define ES_STOP_PG      0x40    /* Last page +1 of RX ring              */
78
79 #define ES_IO_EXTENT    0x37    /* The cfg file says 0xc90 -> 0xcc7     */
80 #define ES_ID_PORT      0xc80   /* Same for all EISA cards              */
81 #define ES_SA_PROM      0xc90   /* Start of e'net addr.                 */
82 #define ES_RESET_PORT   0xc84   /* From the packet driver source        */
83 #define ES_NIC_OFFSET   0xca0   /* Hello, the 8390 is *here*            */
84
85 #define ES_ADDR0        0x02    /* 3 byte vendor prefix                 */
86 #define ES_ADDR1        0x07
87 #define ES_ADDR2        0x01
88
89 /*
90  * Two card revisions. EISA ID's are always rev. minor, rev. major,, and
91  * then the three vendor letters stored in 5 bits each, with an "a" = 1.
92  * For eg: "rii" = 10010 01001 01001 = 0x4929, which is how the EISA
93  * config utility determines automagically what config file(s) to use.
94  */
95 #define ES_EISA_ID1     0x01012949      /* !rii0101.cfg                 */
96 #define ES_EISA_ID2     0x02012949      /* !rii0102.cfg                 */
97
98 #define ES_CFG1         0xcc0   /* IOPORT(1) --> IOPORT(6) in cfg file  */
99 #define ES_CFG2         0xcc1
100 #define ES_CFG3         0xcc2
101 #define ES_CFG4         0xcc3
102 #define ES_CFG5         0xcc4
103 #define ES_CFG6         0xc84   /* NB: 0xc84 is also "reset" port.      */
104
105 /*
106  *      You can OR any of the following bits together and assign it
107  *      to ES_DEBUG to get verbose driver info during operation.
108  *      Some of these don't do anything yet.
109  */
110
111 #define ES_D_PROBE      0x01
112 #define ES_D_RX_PKT     0x02
113 #define ES_D_TX_PKT     0x04
114 #define ED_D_IRQ        0x08
115
116 #define ES_DEBUG        0
117
118 static unsigned char lo_irq_map[] __initdata = {3, 4, 5, 6, 7, 9, 10};
119 static unsigned char hi_irq_map[] __initdata = {11, 12, 0, 14, 0, 0, 0, 15};
120
121 /*
122  *      Probe for the card. The best way is to read the EISA ID if it
123  *      is known. Then we check the prefix of the station address
124  *      PROM for a match against the Racal-Interlan assigned value.
125  */
126
127 static int __init do_es_probe(struct net_device *dev)
128 {
129         unsigned short ioaddr = dev->base_addr;
130         int irq = dev->irq;
131         int mem_start = dev->mem_start;
132
133         SET_MODULE_OWNER(dev);
134
135         if (ioaddr > 0x1ff)             /* Check a single specified location. */
136                 return es_probe1(dev, ioaddr);
137         else if (ioaddr > 0)            /* Don't probe at all. */
138                 return -ENXIO;
139
140         if (!EISA_bus) {
141 #if ES_DEBUG & ES_D_PROBE
142                 printk("es3210.c: Not EISA bus. Not probing high ports.\n");
143 #endif
144                 return -ENXIO;
145         }
146
147         /* EISA spec allows for up to 16 slots, but 8 is typical. */
148         for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
149                 if (es_probe1(dev, ioaddr) == 0)
150                         return 0;
151                 dev->irq = irq;
152                 dev->mem_start = mem_start;
153         }
154
155         return -ENODEV;
156 }
157
158 static void cleanup_card(struct net_device *dev)
159 {
160         free_irq(dev->irq, dev);
161         release_region(dev->base_addr, ES_IO_EXTENT);
162 }
163
164 struct net_device * __init es_probe(int unit)
165 {
166         struct net_device *dev = alloc_ei_netdev();
167         int err;
168
169         if (!dev)
170                 return ERR_PTR(-ENOMEM);
171
172         sprintf(dev->name, "eth%d", unit);
173         netdev_boot_setup_check(dev);
174
175         err = do_es_probe(dev);
176         if (err)
177                 goto out;
178         err = register_netdev(dev);
179         if (err)
180                 goto out1;
181         return dev;
182 out1:
183         cleanup_card(dev);
184 out:
185         free_netdev(dev);
186         return ERR_PTR(err);
187 }
188
189 static int __init es_probe1(struct net_device *dev, int ioaddr)
190 {
191         int i, retval;
192         unsigned long eisa_id;
193
194         if (!request_region(ioaddr + ES_SA_PROM, ES_IO_EXTENT, "es3210"))
195                 return -ENODEV;
196
197 #if ES_DEBUG & ES_D_PROBE
198         printk("es3210.c: probe at %#x, ID %#8x\n", ioaddr, inl(ioaddr + ES_ID_PORT));
199         printk("es3210.c: config regs: %#x %#x %#x %#x %#x %#x\n",
200                 inb(ioaddr + ES_CFG1), inb(ioaddr + ES_CFG2), inb(ioaddr + ES_CFG3),
201                 inb(ioaddr + ES_CFG4), inb(ioaddr + ES_CFG5), inb(ioaddr + ES_CFG6));
202 #endif
203
204
205 /*      Check the EISA ID of the card. */
206         eisa_id = inl(ioaddr + ES_ID_PORT);
207         if ((eisa_id != ES_EISA_ID1) && (eisa_id != ES_EISA_ID2)) {
208                 retval = -ENODEV;
209                 goto out;
210         }
211
212 /*      Check the Racal vendor ID as well. */
213         if (inb(ioaddr + ES_SA_PROM + 0) != ES_ADDR0
214                 || inb(ioaddr + ES_SA_PROM + 1) != ES_ADDR1
215                 || inb(ioaddr + ES_SA_PROM + 2) != ES_ADDR2 ) {
216                 printk("es3210.c: card not found");
217                 for(i = 0; i < ETHER_ADDR_LEN; i++)
218                         printk(" %02x", inb(ioaddr + ES_SA_PROM + i));
219                 printk(" (invalid prefix).\n");
220                 retval = -ENODEV;
221                 goto out;
222         }
223
224         printk("es3210.c: ES3210 rev. %ld at %#x, node", eisa_id>>24, ioaddr);
225         for(i = 0; i < ETHER_ADDR_LEN; i++)
226                 printk(" %02x", (dev->dev_addr[i] = inb(ioaddr + ES_SA_PROM + i)));
227
228         /* Snarf the interrupt now. */
229         if (dev->irq == 0) {
230                 unsigned char hi_irq = inb(ioaddr + ES_CFG2) & 0x07;
231                 unsigned char lo_irq = inb(ioaddr + ES_CFG1) & 0xfe;
232
233                 if (hi_irq != 0) {
234                         dev->irq = hi_irq_map[hi_irq - 1];
235                 } else {
236                         int i = 0;
237                         while (lo_irq > (1<<i)) i++;
238                         dev->irq = lo_irq_map[i];
239                 }
240                 printk(" using IRQ %d", dev->irq);
241 #if ES_DEBUG & ES_D_PROBE
242                 printk("es3210.c: hi_irq %#x, lo_irq %#x, dev->irq = %d\n",
243                                         hi_irq, lo_irq, dev->irq);
244 #endif
245         } else {
246                 if (dev->irq == 2)
247                         dev->irq = 9;                   /* Doh! */
248                 printk(" assigning IRQ %d", dev->irq);
249         }
250
251         if (request_irq(dev->irq, ei_interrupt, 0, "es3210", dev)) {
252                 printk (" unable to get IRQ %d.\n", dev->irq);
253                 retval = -EAGAIN;
254                 goto out;
255         }
256
257         if (dev->mem_start == 0) {
258                 unsigned char mem_enabled = inb(ioaddr + ES_CFG2) & 0xc0;
259                 unsigned char mem_bits = inb(ioaddr + ES_CFG3) & 0x07;
260
261                 if (mem_enabled != 0x80) {
262                         printk(" shared mem disabled - giving up\n");
263                         retval = -ENXIO;
264                         goto out1;
265                 }
266                 dev->mem_start = 0xC0000 + mem_bits*0x4000;
267                 printk(" using ");
268         } else {
269                 printk(" assigning ");
270         }
271
272         dev->mem_end = ei_status.rmem_end = dev->mem_start
273                 + (ES_STOP_PG - ES_START_PG)*256;
274         ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
275
276         printk("mem %#lx-%#lx\n", dev->mem_start, dev->mem_end-1);
277
278 #if ES_DEBUG & ES_D_PROBE
279         if (inb(ioaddr + ES_CFG5))
280                 printk("es3210: Warning - DMA channel enabled, but not used here.\n");
281 #endif
282         /* Note, point at the 8390, and not the card... */
283         dev->base_addr = ioaddr + ES_NIC_OFFSET;
284
285         ei_status.name = "ES3210";
286         ei_status.tx_start_page = ES_START_PG;
287         ei_status.rx_start_page = ES_START_PG + TX_PAGES;
288         ei_status.stop_page = ES_STOP_PG;
289         ei_status.word16 = 1;
290
291         if (ei_debug > 0)
292                 printk(version);
293
294         ei_status.reset_8390 = &es_reset_8390;
295         ei_status.block_input = &es_block_input;
296         ei_status.block_output = &es_block_output;
297         ei_status.get_8390_hdr = &es_get_8390_hdr;
298
299         dev->open = &es_open;
300         dev->stop = &es_close;
301 #ifdef CONFIG_NET_POLL_CONTROLLER
302         dev->poll_controller = ei_poll;
303 #endif
304         NS8390_init(dev, 0);
305         return 0;
306 out1:
307         free_irq(dev->irq, dev);
308 out:
309         release_region(ioaddr + ES_SA_PROM, ES_IO_EXTENT);
310         return retval;
311 }
312
313 /*
314  *      Reset as per the packet driver method. Judging by the EISA cfg
315  *      file, this just toggles the "Board Enable" bits (bit 2 and 0).
316  */
317
318 static void es_reset_8390(struct net_device *dev)
319 {
320         unsigned short ioaddr = dev->base_addr;
321         unsigned long end;
322
323         outb(0x04, ioaddr + ES_RESET_PORT);
324         if (ei_debug > 1) printk("%s: resetting the ES3210...", dev->name);
325
326         end = jiffies + 2*HZ/100;
327         while ((signed)(end - jiffies) > 0) continue;
328
329         ei_status.txing = 0;
330         outb(0x01, ioaddr + ES_RESET_PORT);
331         if (ei_debug > 1) printk("reset done\n");
332
333         return;
334 }
335
336 /*
337  *      Note: In the following three functions is the implicit assumption
338  *      that the associated memcpy will only use "rep; movsl" as long as
339  *      we keep the counts as some multiple of doublewords. This is a
340  *      requirement of the hardware, and also prevents us from using
341  *      eth_io_copy_and_sum() since we can't guarantee it will limit
342  *      itself to doubleword access.
343  */
344
345 /*
346  *      Grab the 8390 specific header. Similar to the block_input routine, but
347  *      we don't need to be concerned with ring wrap as the header will be at
348  *      the start of a page, so we optimize accordingly. (A single doubleword.)
349  */
350
351 static void
352 es_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
353 {
354         unsigned long hdr_start = dev->mem_start + ((ring_page - ES_START_PG)<<8);
355         isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
356         hdr->count = (hdr->count + 3) & ~3;     /* Round up allocation. */
357 }
358
359 /*
360  *      Block input and output are easy on shared memory ethercards, the only
361  *      complication is when the ring buffer wraps. The count will already
362  *      be rounded up to a doubleword value via es_get_8390_hdr() above.
363  */
364
365 static void es_block_input(struct net_device *dev, int count, struct sk_buff *skb,
366                                                   int ring_offset)
367 {
368         unsigned long xfer_start = dev->mem_start + ring_offset - (ES_START_PG<<8);
369
370         if (xfer_start + count > ei_status.rmem_end) {
371                 /* Packet wraps over end of ring buffer. */
372                 int semi_count = ei_status.rmem_end - xfer_start;
373                 isa_memcpy_fromio(skb->data, xfer_start, semi_count);
374                 count -= semi_count;
375                 isa_memcpy_fromio(skb->data + semi_count, ei_status.rmem_start, count);
376         } else {
377                 /* Packet is in one chunk. */
378                 isa_eth_io_copy_and_sum(skb, xfer_start, count, 0);
379         }
380 }
381
382 static void es_block_output(struct net_device *dev, int count,
383                                 const unsigned char *buf, int start_page)
384 {
385         unsigned long shmem = dev->mem_start + ((start_page - ES_START_PG)<<8);
386
387         count = (count + 3) & ~3;     /* Round up to doubleword */
388         isa_memcpy_toio(shmem, buf, count);
389 }
390
391 static int es_open(struct net_device *dev)
392 {
393         ei_open(dev);
394         return 0;
395 }
396
397 static int es_close(struct net_device *dev)
398 {
399
400         if (ei_debug > 1)
401                 printk("%s: Shutting down ethercard.\n", dev->name);
402
403         ei_close(dev);
404         return 0;
405 }
406
407 #ifdef MODULE
408 #define MAX_ES_CARDS    4       /* Max number of ES3210 cards per module */
409 #define NAMELEN         8       /* # of chars for storing dev->name */
410 static struct net_device *dev_es3210[MAX_ES_CARDS];
411 static int io[MAX_ES_CARDS];
412 static int irq[MAX_ES_CARDS];
413 static int mem[MAX_ES_CARDS];
414
415 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_ES_CARDS) "i");
416 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_ES_CARDS) "i");
417 MODULE_PARM(mem, "1-" __MODULE_STRING(MAX_ES_CARDS) "i");
418 MODULE_PARM_DESC(io, "I/O base address(es)");
419 MODULE_PARM_DESC(irq, "IRQ number(s)");
420 MODULE_PARM_DESC(mem, "memory base address(es)");
421 MODULE_DESCRIPTION("Racal-Interlan ES3210 EISA ethernet driver");
422 MODULE_LICENSE("GPL");
423
424 int
425 init_module(void)
426 {
427         struct net_device *dev;
428         int this_dev, found = 0;
429
430         for (this_dev = 0; this_dev < MAX_ES_CARDS; this_dev++) {
431                 if (io[this_dev] == 0 && this_dev != 0)
432                         break;
433                 dev = alloc_ei_netdev();
434                 if (!dev)
435                         break;
436                 dev->irq = irq[this_dev];
437                 dev->base_addr = io[this_dev];
438                 dev->mem_start = mem[this_dev];
439                 if (do_es_probe(dev) == 0) {
440                         if (register_netdev(dev) == 0) {
441                                 dev_es3210[found++] = dev;
442                                 continue;
443                         }
444                         cleanup_card(dev);
445                 }
446                 free_netdev(dev);
447                 printk(KERN_WARNING "es3210.c: No es3210 card found (i/o = 0x%x).\n", io[this_dev]);
448                 break;
449         }
450         if (found)
451                 return 0;
452         return -ENXIO;
453 }
454
455 void
456 cleanup_module(void)
457 {
458         int this_dev;
459
460         for (this_dev = 0; this_dev < MAX_ES_CARDS; this_dev++) {
461                 struct net_device *dev = dev_es3210[this_dev];
462                 if (dev) {
463                         unregister_netdev(dev);
464                         cleanup_card(dev);
465                         free_netdev(dev);
466                 }
467         }
468 }
469 #endif /* MODULE */
470