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