vserver 1.9.5.x5
[linux-2.6.git] / drivers / net / smc-mca.c
1 /* smc-mca.c: A SMC Ultra ethernet driver for linux. */
2 /*
3     Most of this driver, except for ultramca_probe is nearly
4     verbatim from smc-ultra.c by Donald Becker. The rest is
5     written and copyright 1996 by David Weis, weisd3458@uni.edu
6
7     This is a driver for the SMC Ultra and SMC EtherEZ ethercards.
8
9     This driver uses the cards in the 8390-compatible, shared memory mode.
10     Most of the run-time complexity is handled by the generic code in
11     8390.c.
12
13     This driver enables the shared memory only when doing the actual data
14     transfers to avoid a bug in early version of the card that corrupted
15     data transferred by a AHA1542.
16
17     This driver does not support the programmed-I/O data transfer mode of
18     the EtherEZ.  That support (if available) is smc-ez.c.  Nor does it
19     use the non-8390-compatible "Altego" mode. (No support currently planned.)
20
21     Changelog:
22
23     Paul Gortmaker       : multiple card support for module users.
24     David Weis           : Micro Channel-ized it.
25     Tom Sightler         : Added support for IBM PS/2 Ethernet Adapter/A
26     Christopher Turcksin : Changed MCA-probe so that multiple adapters are
27                            found correctly (Jul 16, 1997)
28     Chris Beauregard     : Tried to merge the two changes above (Dec 15, 1997)
29     Tom Sightler         : Fixed minor detection bug caused by above merge
30     Tom Sightler         : Added support for three more Western Digital
31                            MCA-adapters
32     Tom Sightler         : Added support for 2.2.x mca_find_unused_adapter
33     Hartmut Schmidt      : - Modified parameter detection to handle each
34                              card differently depending on a switch-list
35                            - 'card_ver' removed from the adapter list
36                            - Some minor bug fixes
37 */
38
39 #include <linux/mca.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/errno.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47
48 #include <asm/io.h>
49 #include <asm/system.h>
50
51 #include "8390.h"
52 #include "smc-mca.h"
53
54 #define DRV_NAME "smc-mca"
55
56 static int ultramca_open(struct net_device *dev);
57 static void ultramca_reset_8390(struct net_device *dev);
58 static void ultramca_get_8390_hdr(struct net_device *dev,
59                                   struct e8390_pkt_hdr *hdr,
60                                   int ring_page);
61 static void ultramca_block_input(struct net_device *dev, int count,
62                                  struct sk_buff *skb,
63                                  int ring_offset);
64 static void ultramca_block_output(struct net_device *dev, int count,
65                                   const unsigned char *buf,
66                                   const int start_page);
67 static int ultramca_close_card(struct net_device *dev);
68
69 #define START_PG        0x00    /* First page of TX buffer */
70
71 #define ULTRA_CMDREG 0      /* Offset to ASIC command register. */
72 #define ULTRA_RESET  0x80   /* Board reset, in ULTRA_CMDREG. */
73 #define ULTRA_MEMENB 0x40   /* Enable the shared memory. */
74 #define ULTRA_NIC_OFFSET 16 /* NIC register offset from the base_addr. */
75 #define ULTRA_IO_EXTENT 32
76 #define EN0_ERWCNT      0x08  /* Early receive warning count. */
77
78 #define _61c8_SMC_Ethercard_PLUS_Elite_A_BNC_AUI_WD8013EP_A            0
79 #define _61c9_SMC_Ethercard_PLUS_Elite_A_UTP_AUI_WD8013EP_A            1
80 #define _6fc0_WD_Ethercard_PLUS_A_WD8003E_A_OR_WD8003ET_A              2
81 #define _6fc1_WD_Starcard_PLUS_A_WD8003ST_A                            3
82 #define _6fc2_WD_Ethercard_PLUS_10T_A_WD8003W_A                        4
83 #define _efd4_IBM_PS2_Adapter_A_for_Ethernet_UTP_AUI_WD8013WP_A        5
84 #define _efd5_IBM_PS2_Adapter_A_for_Ethernet_BNC_AUI_WD8013WP_A        6
85 #define _efe5_IBM_PS2_Adapter_A_for_Ethernet                           7
86
87 struct smc_mca_adapters_t {
88         unsigned int id;
89         char *name;
90 };
91
92 #define MAX_ULTRAMCA_CARDS 4    /* Max number of Ultra cards per module */
93
94 static int ultra_io[MAX_ULTRAMCA_CARDS];
95 static int ultra_irq[MAX_ULTRAMCA_CARDS];
96 MODULE_LICENSE("GPL");
97
98 module_param_array(ultra_io, int, NULL, 0);
99 module_param_array(ultra_irq, int, NULL, 0);
100 MODULE_PARM_DESC(ultra_io, "SMC Ultra/EtherEZ MCA I/O base address(es)");
101 MODULE_PARM_DESC(ultra_irq, "SMC Ultra/EtherEZ MCA IRQ number(s)");
102
103 static short smc_mca_adapter_ids[] __initdata = {
104         0x61c8,
105         0x61c9,
106         0x6fc0,
107         0x6fc1,
108         0x6fc2,
109         0xefd4,
110         0xefd5,
111         0xefe5,
112         0x0000
113 };
114
115 static char *smc_mca_adapter_names[] __initdata = {
116         "SMC Ethercard PLUS Elite/A BNC/AUI (WD8013EP/A)",
117         "SMC Ethercard PLUS Elite/A UTP/AUI (WD8013WP/A)",
118         "WD Ethercard PLUS/A (WD8003E/A or WD8003ET/A)",
119         "WD Starcard PLUS/A (WD8003ST/A)",
120         "WD Ethercard PLUS 10T/A (WD8003W/A)",
121         "IBM PS/2 Adapter/A for Ethernet UTP/AUI (WD8013WP/A)",
122         "IBM PS/2 Adapter/A for Ethernet BNC/AUI (WD8013EP/A)",
123         "IBM PS/2 Adapter/A for Ethernet",
124         NULL
125 };
126
127 static int ultra_found = 0;
128
129 int __init ultramca_probe(struct device *gen_dev)
130 {
131         unsigned short ioaddr;
132         struct net_device *dev;
133         unsigned char reg4, num_pages;
134         struct mca_device *mca_dev = to_mca_device(gen_dev);
135         char slot = mca_dev->slot;
136         unsigned char pos2 = 0xff, pos3 = 0xff, pos4 = 0xff, pos5 = 0xff;
137         int i, rc;
138         int adapter = mca_dev->index;
139         int tbase = 0;
140         int tirq = 0;
141         int base_addr = ultra_io[ultra_found];
142         int irq = ultra_irq[ultra_found];
143
144         if (base_addr || irq) {
145                 printk(KERN_INFO "Probing for SMC MCA adapter");
146                 if (base_addr) {
147                         printk(KERN_INFO " at I/O address 0x%04x%c",
148                                base_addr, irq ? ' ' : '\n');
149                 }
150                 if (irq) {
151                         printk(KERN_INFO "using irq %d\n", irq);
152                 }
153         }
154
155         tirq = 0;
156         tbase = 0;
157
158         /* If we're trying to match a specificied irq or io address,
159          * we'll reject the adapter found unless it's the one we're
160          * looking for */
161
162         pos2 = mca_device_read_stored_pos(mca_dev, 2); /* io_addr */
163         pos3 = mca_device_read_stored_pos(mca_dev, 3); /* shared mem */
164         pos4 = mca_device_read_stored_pos(mca_dev, 4); /* ROM bios addr range */
165         pos5 = mca_device_read_stored_pos(mca_dev, 5); /* irq, media and RIPL */
166
167         /* Test the following conditions:
168          * - If an irq parameter is supplied, compare it
169          *   with the irq of the adapter we found
170          * - If a base_addr paramater is given, compare it
171          *   with the base_addr of the adapter we found
172          * - Check that the irq and the base_addr of the
173          *   adapter we found is not already in use by
174          *   this driver
175          */
176
177         switch (mca_dev->index) {
178         case _61c8_SMC_Ethercard_PLUS_Elite_A_BNC_AUI_WD8013EP_A:
179         case _61c9_SMC_Ethercard_PLUS_Elite_A_UTP_AUI_WD8013EP_A:
180         case _efd4_IBM_PS2_Adapter_A_for_Ethernet_UTP_AUI_WD8013WP_A:
181         case _efd5_IBM_PS2_Adapter_A_for_Ethernet_BNC_AUI_WD8013WP_A:
182                 {
183                         tbase = addr_table[(pos2 & 0xf0) >> 4].base_addr;
184                         tirq  = irq_table[(pos5 & 0xc) >> 2].new_irq;
185                         break;
186                 }
187         case _6fc0_WD_Ethercard_PLUS_A_WD8003E_A_OR_WD8003ET_A:
188         case _6fc1_WD_Starcard_PLUS_A_WD8003ST_A:
189         case _6fc2_WD_Ethercard_PLUS_10T_A_WD8003W_A:
190         case _efe5_IBM_PS2_Adapter_A_for_Ethernet:
191                 {
192                         tbase = ((pos2 & 0x0fe) * 0x10);
193                         tirq  = irq_table[(pos5 & 3)].old_irq;
194                         break;
195                 }
196         }
197         
198         if(!tirq || !tbase 
199            || (irq && irq != tirq) 
200            || (base_addr && tbase != base_addr))
201                 /* FIXME: we're trying to force the ordering of the
202                  * devices here, there should be a way of getting this
203                  * to happen */
204                 return -ENXIO;
205
206         /* Adapter found. */
207         dev  = alloc_ei_netdev();
208         if(!dev)
209                 return -ENODEV;
210
211         SET_MODULE_OWNER(dev);
212         SET_NETDEV_DEV(dev, gen_dev);
213         mca_device_set_name(mca_dev, smc_mca_adapter_names[adapter]);
214         mca_device_set_claim(mca_dev, 1);
215
216         printk(KERN_INFO "smc_mca: %s found in slot %d\n",
217                        smc_mca_adapter_names[adapter], slot + 1);
218
219         ultra_found++;
220
221         dev->base_addr = ioaddr = mca_device_transform_ioport(mca_dev, tbase);
222         dev->irq       = mca_device_transform_irq(mca_dev, tirq);
223         dev->mem_start = 0;
224         num_pages      = 40;
225
226         switch (adapter) {      /* card-# in const array above [hs] */
227                 case _61c8_SMC_Ethercard_PLUS_Elite_A_BNC_AUI_WD8013EP_A:
228                 case _61c9_SMC_Ethercard_PLUS_Elite_A_UTP_AUI_WD8013EP_A:
229                 {
230                         for (i = 0; i < 16; i++) { /* taking 16 counts
231                                                     * up to 15 [hs] */
232                                 if (mem_table[i].mem_index == (pos3 & ~MEM_MASK)) {
233                                         dev->mem_start = (unsigned long)
234                                           mca_device_transform_memory(mca_dev, (void *)mem_table[i].mem_start);
235                                         num_pages = mem_table[i].num_pages;
236                                 }
237                         }
238                         break;
239                 }
240                 case _6fc0_WD_Ethercard_PLUS_A_WD8003E_A_OR_WD8003ET_A:
241                 case _6fc1_WD_Starcard_PLUS_A_WD8003ST_A:
242                 case _6fc2_WD_Ethercard_PLUS_10T_A_WD8003W_A:
243                 case _efe5_IBM_PS2_Adapter_A_for_Ethernet:
244                 {
245                         dev->mem_start = (unsigned long)
246                           mca_device_transform_memory(mca_dev, (void *)((pos3 & 0xfc) * 0x1000));
247                         num_pages = 0x40;
248                         break;
249                 }
250                 case _efd4_IBM_PS2_Adapter_A_for_Ethernet_UTP_AUI_WD8013WP_A:
251                 case _efd5_IBM_PS2_Adapter_A_for_Ethernet_BNC_AUI_WD8013WP_A:
252                 {
253                         /* courtesy of gamera@quartz.ocn.ne.jp, pos3 indicates
254                          * the index of the 0x2000 step.
255                          * beware different number of pages [hs]
256                          */
257                         dev->mem_start = (unsigned long) 
258                           mca_device_transform_memory(mca_dev, (void *)(0xc0000 + (0x2000 * (pos3 & 0xf))));
259                         num_pages = 0x20 + (2 * (pos3 & 0x10));
260                         break;
261                 }
262         }
263
264         /* sanity check, shouldn't happen */
265         if (dev->mem_start == 0) {
266                 rc = -ENODEV;
267                 goto err_unclaim;
268         }
269
270         if (!request_region(ioaddr, ULTRA_IO_EXTENT, DRV_NAME)) {
271                 rc = -ENODEV;
272                 goto err_unclaim;
273         }
274
275         reg4 = inb(ioaddr + 4) & 0x7f;
276         outb(reg4, ioaddr + 4);
277
278         printk(KERN_INFO "smc_mca[%d]: Parameters: %#3x,", slot + 1, ioaddr);
279
280         for (i = 0; i < 6; i++)
281                 printk(" %2.2X", dev->dev_addr[i] = inb(ioaddr + 8 + i));
282
283         /* Switch from the station address to the alternate register set
284          * and read the useful registers there.
285          */
286
287         outb(0x80 | reg4, ioaddr + 4);
288
289         /* Enable FINE16 mode to avoid BIOS ROM width mismatches @ reboot.
290          */
291
292         outb(0x80 | inb(ioaddr + 0x0c), ioaddr + 0x0c);
293
294         /* Switch back to the station address register set so that
295          * the MS-DOS driver can find the card after a warm boot.
296          */
297
298         outb(reg4, ioaddr + 4);
299
300         gen_dev->driver_data = dev;
301
302         /* The 8390 isn't at the base address, so fake the offset
303          */
304
305         dev->base_addr = ioaddr + ULTRA_NIC_OFFSET;
306
307         ei_status.name = "SMC Ultra MCA";
308         ei_status.word16 = 1;
309         ei_status.tx_start_page = START_PG;
310         ei_status.rx_start_page = START_PG + TX_PAGES;
311         ei_status.stop_page = num_pages;
312
313         ei_status.rmem_start = dev->mem_start + TX_PAGES * 256;
314         dev->mem_end = ei_status.rmem_end =
315         dev->mem_start + (ei_status.stop_page - START_PG) * 256;
316
317         printk(", IRQ %d memory %#lx-%#lx.\n",
318         dev->irq, dev->mem_start, dev->mem_end - 1);
319
320         ei_status.reset_8390 = &ultramca_reset_8390;
321         ei_status.block_input = &ultramca_block_input;
322         ei_status.block_output = &ultramca_block_output;
323         ei_status.get_8390_hdr = &ultramca_get_8390_hdr;
324
325         ei_status.priv = slot;
326
327         dev->open = &ultramca_open;
328         dev->stop = &ultramca_close_card;
329 #ifdef CONFIG_NET_POLL_CONTROLLER
330         dev->poll_controller = ei_poll;
331 #endif
332
333         NS8390_init(dev, 0);
334
335         rc = register_netdev(dev);
336         if (rc)
337                 goto err_release_region;
338
339         return 0;
340
341 err_release_region:
342         release_region(ioaddr, ULTRA_IO_EXTENT);
343 err_unclaim:
344         mca_device_set_claim(mca_dev, 0);
345         free_netdev(dev);
346         return rc;
347 }
348
349 static int ultramca_open(struct net_device *dev)
350 {
351         int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
352         int retval;
353
354         if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev)))
355                 return retval;
356
357         outb(ULTRA_MEMENB, ioaddr); /* Enable memory */
358         outb(0x80, ioaddr + 5);     /* ??? */
359         outb(0x01, ioaddr + 6);     /* Enable interrupts and memory. */
360         outb(0x04, ioaddr + 5);     /* ??? */
361
362         /* Set the early receive warning level in window 0 high enough not
363          * to receive ERW interrupts.
364          */
365
366         /* outb_p(E8390_NODMA + E8390_PAGE0, dev->base_addr);
367          * outb(0xff, dev->base_addr + EN0_ERWCNT);
368          */
369
370         ei_open(dev);
371         return 0;
372 }
373
374 static void ultramca_reset_8390(struct net_device *dev)
375 {
376         int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
377
378         outb(ULTRA_RESET, ioaddr);
379         if (ei_debug > 1)
380                 printk("resetting Ultra, t=%ld...", jiffies);
381         ei_status.txing = 0;
382
383         outb(0x80, ioaddr + 5);     /* ??? */
384         outb(0x01, ioaddr + 6);     /* Enable interrupts and memory. */
385
386         if (ei_debug > 1)
387                 printk("reset done\n");
388         return;
389 }
390
391 /* Grab the 8390 specific header. Similar to the block_input routine, but
392  * we don't need to be concerned with ring wrap as the header will be at
393  * the start of a page, so we optimize accordingly.
394  */
395
396 static void ultramca_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
397 {
398         unsigned long hdr_start = dev->mem_start + ((ring_page - START_PG) << 8);
399
400 #ifdef notdef
401         /* Officially this is what we are doing, but the readl() is faster */
402         isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
403 #else
404         ((unsigned int*)hdr)[0] = isa_readl(hdr_start);
405 #endif
406 }
407
408 /* Block input and output are easy on shared memory ethercards, the only
409  * complication is when the ring buffer wraps.
410  */
411
412 static void ultramca_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
413 {
414         unsigned long xfer_start = dev->mem_start + ring_offset - (START_PG << 8);
415
416         if (xfer_start + count > ei_status.rmem_end) {
417                 /* We must wrap the input move. */
418                 int semi_count = ei_status.rmem_end - xfer_start;
419                 isa_memcpy_fromio(skb->data, xfer_start, semi_count);
420                 count -= semi_count;
421                 isa_memcpy_fromio(skb->data + semi_count, ei_status.rmem_start, count);
422         } else {
423                 /* Packet is in one chunk -- we can copy + cksum. */
424                 isa_eth_io_copy_and_sum(skb, xfer_start, count, 0);
425         }
426
427 }
428
429 static void ultramca_block_output(struct net_device *dev, int count, const unsigned char *buf,
430                 int start_page)
431 {
432         unsigned long shmem = dev->mem_start + ((start_page - START_PG) << 8);
433
434         isa_memcpy_toio(shmem, buf, count);
435 }
436
437 static int ultramca_close_card(struct net_device *dev)
438 {
439         int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
440
441         netif_stop_queue(dev);
442         
443         if (ei_debug > 1)
444                 printk("%s: Shutting down ethercard.\n", dev->name);
445
446         outb(0x00, ioaddr + 6);     /* Disable interrupts. */
447         free_irq(dev->irq, dev);
448
449         NS8390_init(dev, 0);
450         /* We should someday disable shared memory and change to 8-bit mode
451          * "just in case"...
452          */
453
454         return 0;
455 }
456
457 static int ultramca_remove(struct device *gen_dev)
458 {
459         struct mca_device *mca_dev = to_mca_device(gen_dev);
460         struct net_device *dev = (struct net_device *)gen_dev->driver_data;
461
462         if (dev) {
463                 /* NB: ultra_close_card() does free_irq */
464                 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET;
465
466                 unregister_netdev(dev);
467                 mca_device_set_claim(mca_dev, 0);
468                 release_region(ioaddr, ULTRA_IO_EXTENT);
469                 free_netdev(dev);
470         }
471         return 0;
472 }
473
474
475 static struct mca_driver ultra_driver = {
476         .id_table = smc_mca_adapter_ids,
477         .driver = {
478                 .name = "smc-mca",
479                 .bus = &mca_bus_type,
480                 .probe = ultramca_probe,
481                 .remove = ultramca_remove,
482         }
483 };
484
485 static int __init ultramca_init_module(void)
486 {
487         if(!MCA_bus)
488                 return -ENXIO;
489
490         mca_register_driver(&ultra_driver);
491
492         return ultra_found ? 0 : -ENXIO;
493 }
494
495 static void __exit ultramca_cleanup_module(void)
496 {
497         mca_unregister_driver(&ultra_driver);
498 }
499 module_init(ultramca_init_module);
500 module_exit(ultramca_cleanup_module);
501