ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / smc-ultra32.c
1 /*      smc-ultra32.c: An SMC Ultra32 EISA ethernet driver for linux.
2
3 Sources:
4
5         This driver is based on (cloned from) the ISA SMC Ultra driver
6         written by Donald Becker. Modifications to support the EISA
7         version of the card by Paul Gortmaker and Leonard N. Zubkoff.
8
9         This software may be used and distributed according to the terms
10         of the GNU General Public License, incorporated herein by reference.
11
12 Theory of Operation:
13
14         The SMC Ultra32C card uses the SMC 83c790 chip which is also
15         found on the ISA SMC Ultra cards. It has a shared memory mode of
16         operation that makes it similar to the ISA version of the card.
17         The main difference is that the EISA card has 32KB of RAM, but
18         only an 8KB window into that memory. The EISA card also can be
19         set for a bus-mastering mode of operation via the ECU, but that
20         is not (and probably will never be) supported by this driver.
21         The ECU should be run to enable shared memory and to disable the
22         bus-mastering feature for use with linux.
23
24         By programming the 8390 to use only 8KB RAM, the modifications
25         to the ISA driver can be limited to the probe and initialization
26         code. This allows easy integration of EISA support into the ISA
27         driver. However, the driver development kit from SMC provided the
28         register information for sliding the 8KB window, and hence the 8390
29         is programmed to use the full 32KB RAM.
30
31         Unfortunately this required code changes outside the probe/init
32         routines, and thus we decided to separate the EISA driver from
33         the ISA one. In this way, ISA users don't end up with a larger
34         driver due to the EISA code, and EISA users don't end up with a
35         larger driver due to the ISA EtherEZ PIO code. The driver is
36         similar to the 3c503/16 driver, in that the window must be set
37         back to the 1st 8KB of space for access to the two 8390 Tx slots.
38
39         In testing, using only 8KB RAM (3 Tx / 5 Rx) didn't appear to
40         be a limiting factor, since the EISA bus could get packets off
41         the card fast enough, but having the use of lots of RAM as Rx
42         space is extra insurance if interrupt latencies become excessive.
43
44 */
45
46 static const char *version = "smc-ultra32.c: 06/97 v1.00\n";
47
48
49 #include <linux/module.h>
50 #include <linux/eisa.h>
51 #include <linux/kernel.h>
52 #include <linux/errno.h>
53 #include <linux/string.h>
54 #include <linux/init.h>
55 #include <linux/interrupt.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58
59 #include <asm/io.h>
60 #include <asm/system.h>
61
62 #include "8390.h"
63
64 static int ultra32_probe1(struct net_device *dev, int ioaddr);
65 static int ultra32_open(struct net_device *dev);
66 static void ultra32_reset_8390(struct net_device *dev);
67 static void ultra32_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
68                                  int ring_page);
69 static void ultra32_block_input(struct net_device *dev, int count,
70                                 struct sk_buff *skb, int ring_offset);
71 static void ultra32_block_output(struct net_device *dev, int count,
72                                  const unsigned char *buf,
73                                  const int start_page);
74 static int ultra32_close(struct net_device *dev);
75 \f
76 #define ULTRA32_CMDREG  0       /* Offset to ASIC command register. */
77 #define  ULTRA32_RESET  0x80    /* Board reset, in ULTRA32_CMDREG. */
78 #define  ULTRA32_MEMENB 0x40    /* Enable the shared memory. */
79 #define ULTRA32_NIC_OFFSET 16   /* NIC register offset from the base_addr. */
80 #define ULTRA32_IO_EXTENT 32
81 #define EN0_ERWCNT              0x08    /* Early receive warning count. */
82
83 /*
84  * Defines that apply only to the Ultra32 EISA card. Note that
85  * "smc" = 10011 01101 00011 = 0x4da3, and hence !smc8010.cfg translates
86  * into an EISA ID of 0x1080A34D
87  */
88 #define ULTRA32_BASE    0xca0
89 #define ULTRA32_ID      0x1080a34d
90 #define ULTRA32_IDPORT  (-0x20) /* 0xc80 */
91 /* Config regs 1->7 from the EISA !SMC8010.CFG file. */
92 #define ULTRA32_CFG1    0x04    /* 0xca4 */
93 #define ULTRA32_CFG2    0x05    /* 0xca5 */
94 #define ULTRA32_CFG3    (-0x18) /* 0xc88 */
95 #define ULTRA32_CFG4    (-0x17) /* 0xc89 */
96 #define ULTRA32_CFG5    (-0x16) /* 0xc8a */
97 #define ULTRA32_CFG6    (-0x15) /* 0xc8b */
98 #define ULTRA32_CFG7    0x0d    /* 0xcad */
99
100 static void cleanup_card(struct net_device *dev)
101 {
102         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET;
103         /* NB: ultra32_close_card() does free_irq */
104         release_region(ioaddr, ULTRA32_IO_EXTENT);
105 }
106
107 /*      Probe for the Ultra32.  This looks like a 8013 with the station
108         address PROM at I/O ports <base>+8 to <base>+13, with a checksum
109         following.
110 */
111
112 struct net_device * __init ultra32_probe(int unit)
113 {
114         struct net_device *dev;
115         int base;
116         int irq;
117         int err = -ENODEV;
118
119         if (!EISA_bus)
120                 return ERR_PTR(-ENODEV);
121
122         dev = alloc_ei_netdev();
123
124         if (!dev)
125                 return ERR_PTR(-ENOMEM);
126
127         if (unit >= 0) {
128                 sprintf(dev->name, "eth%d", unit);
129                 netdev_boot_setup_check(dev);
130         }
131
132         SET_MODULE_OWNER(dev);
133
134         irq = dev->irq;
135
136         /* EISA spec allows for up to 16 slots, but 8 is typical. */
137         for (base = 0x1000 + ULTRA32_BASE; base < 0x9000; base += 0x1000) {
138                 if (ultra32_probe1(dev, base) == 0)
139                         break;
140                 dev->irq = irq;
141         }
142         if (base >= 0x9000)
143                 goto out;
144         err = register_netdev(dev);
145         if (err)
146                 goto out1;
147         return dev;
148 out1:
149         cleanup_card(dev);
150 out:
151         free_netdev(dev);
152         return ERR_PTR(err);
153 }
154
155 static int __init ultra32_probe1(struct net_device *dev, int ioaddr)
156 {
157         int i, edge, media, retval;
158         int checksum = 0;
159         const char *model_name;
160         static unsigned version_printed;
161         /* Values from various config regs. */
162         unsigned char idreg;
163         unsigned char reg4;
164         const char *ifmap[] = {"UTP No Link", "", "UTP/AUI", "UTP/BNC"};
165
166         if (!request_region(ioaddr, ULTRA32_IO_EXTENT, dev->name))
167                 return -EBUSY;
168
169         if (inb(ioaddr + ULTRA32_IDPORT) == 0xff ||
170             inl(ioaddr + ULTRA32_IDPORT) != ULTRA32_ID) {
171                 retval = -ENODEV;
172                 goto out;
173         }
174
175         media = inb(ioaddr + ULTRA32_CFG7) & 0x03;
176         edge = inb(ioaddr + ULTRA32_CFG5) & 0x08;
177         printk("SMC Ultra32 in EISA Slot %d, Media: %s, %s IRQs.\n",
178                 ioaddr >> 12, ifmap[media],
179                 (edge ? "Edge Triggered" : "Level Sensitive"));
180
181         idreg = inb(ioaddr + 7);
182         reg4 = inb(ioaddr + 4) & 0x7f;
183
184         /* Check the ID nibble. */
185         if ((idreg & 0xf0) != 0x20) {                   /* SMC Ultra */
186                 retval = -ENODEV;
187                 goto out;
188         }
189
190         /* Select the station address register set. */
191         outb(reg4, ioaddr + 4);
192
193         for (i = 0; i < 8; i++)
194                 checksum += inb(ioaddr + 8 + i);
195         if ((checksum & 0xff) != 0xff) {
196                 retval = -ENODEV;
197                 goto out;
198         }
199
200         if (ei_debug  &&  version_printed++ == 0)
201                 printk(version);
202
203         model_name = "SMC Ultra32";
204
205         printk("%s: %s at 0x%X,", dev->name, model_name, ioaddr);
206
207         for (i = 0; i < 6; i++)
208                 printk(" %2.2X", dev->dev_addr[i] = inb(ioaddr + 8 + i));
209
210         /* Switch from the station address to the alternate register set and
211            read the useful registers there. */
212         outb(0x80 | reg4, ioaddr + 4);
213
214         /* Enable FINE16 mode to avoid BIOS ROM width mismatches @ reboot. */
215         outb(0x80 | inb(ioaddr + 0x0c), ioaddr + 0x0c);
216
217         /* Reset RAM addr. */
218         outb(0x00, ioaddr + 0x0b);
219
220         /* Switch back to the station address register set so that the
221            MS-DOS driver can find the card after a warm boot. */
222         outb(reg4, ioaddr + 4);
223
224         if ((inb(ioaddr + ULTRA32_CFG5) & 0x40) == 0) {
225                 printk("\nsmc-ultra32: Card RAM is disabled!  "
226                        "Run EISA config utility.\n");
227                 retval = -ENODEV;
228                 goto out;
229         }
230         if ((inb(ioaddr + ULTRA32_CFG2) & 0x04) == 0)
231                 printk("\nsmc-ultra32: Ignoring Bus-Master enable bit.  "
232                        "Run EISA config utility.\n");
233
234         if (dev->irq < 2) {
235                 unsigned char irqmap[] = {0, 9, 3, 5, 7, 10, 11, 15};
236                 int irq = irqmap[inb(ioaddr + ULTRA32_CFG5) & 0x07];
237                 if (irq == 0) {
238                         printk(", failed to detect IRQ line.\n");
239                         retval = -EAGAIN;
240                         goto out;
241                 }
242                 dev->irq = irq;
243         }
244
245         /* The 8390 isn't at the base address, so fake the offset */
246         dev->base_addr = ioaddr + ULTRA32_NIC_OFFSET;
247
248         /* Save RAM address in the unused reg0 to avoid excess inb's. */
249         ei_status.reg0 = inb(ioaddr + ULTRA32_CFG3) & 0xfc;
250
251         dev->mem_start =  0xc0000 + ((ei_status.reg0 & 0x7c) << 11);
252
253         ei_status.name = model_name;
254         ei_status.word16 = 1;
255         ei_status.tx_start_page = 0;
256         ei_status.rx_start_page = TX_PAGES;
257         /* All Ultra32 cards have 32KB memory with an 8KB window. */
258         ei_status.stop_page = 128;
259
260         ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
261         dev->mem_end = ei_status.rmem_end = dev->mem_start + 0x1fff;
262
263         printk(", IRQ %d, 32KB memory, 8KB window at 0x%lx-0x%lx.\n",
264                dev->irq, dev->mem_start, dev->mem_end);
265         ei_status.block_input = &ultra32_block_input;
266         ei_status.block_output = &ultra32_block_output;
267         ei_status.get_8390_hdr = &ultra32_get_8390_hdr;
268         ei_status.reset_8390 = &ultra32_reset_8390;
269         dev->open = &ultra32_open;
270         dev->stop = &ultra32_close;
271 #ifdef CONFIG_NET_POLL_CONTROLLER
272         dev->poll_controller = ei_poll;
273 #endif
274         NS8390_init(dev, 0);
275
276         return 0;
277 out:
278         release_region(ioaddr, ULTRA32_IO_EXTENT);
279         return retval;
280 }
281
282 static int ultra32_open(struct net_device *dev)
283 {
284         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* ASIC addr */
285         int irq_flags = (inb(ioaddr + ULTRA32_CFG5) & 0x08) ? 0 : SA_SHIRQ;
286         int retval;
287
288         retval = request_irq(dev->irq, ei_interrupt, irq_flags, dev->name, dev);
289         if (retval)
290                 return retval;
291
292         outb(ULTRA32_MEMENB, ioaddr); /* Enable Shared Memory. */
293         outb(0x80, ioaddr + ULTRA32_CFG6); /* Enable Interrupts. */
294         outb(0x84, ioaddr + 5); /* Enable MEM16 & Disable Bus Master. */
295         outb(0x01, ioaddr + 6); /* Enable Interrupts. */
296         /* Set the early receive warning level in window 0 high enough not
297            to receive ERW interrupts. */
298         outb_p(E8390_NODMA+E8390_PAGE0, dev->base_addr);
299         outb(0xff, dev->base_addr + EN0_ERWCNT);
300         ei_open(dev);
301         return 0;
302 }
303
304 static int ultra32_close(struct net_device *dev)
305 {
306         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* CMDREG */
307
308         netif_stop_queue(dev);
309         
310         if (ei_debug > 1)
311                 printk("%s: Shutting down ethercard.\n", dev->name);
312
313         outb(0x00, ioaddr + ULTRA32_CFG6); /* Disable Interrupts. */
314         outb(0x00, ioaddr + 6);         /* Disable interrupts. */
315         free_irq(dev->irq, dev);
316
317         NS8390_init(dev, 0);
318
319         return 0;
320 }
321
322 static void ultra32_reset_8390(struct net_device *dev)
323 {
324         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* ASIC base addr */
325
326         outb(ULTRA32_RESET, ioaddr);
327         if (ei_debug > 1) printk("resetting Ultra32, t=%ld...", jiffies);
328         ei_status.txing = 0;
329
330         outb(ULTRA32_MEMENB, ioaddr); /* Enable Shared Memory. */
331         outb(0x80, ioaddr + ULTRA32_CFG6); /* Enable Interrupts. */
332         outb(0x84, ioaddr + 5); /* Enable MEM16 & Disable Bus Master. */
333         outb(0x01, ioaddr + 6); /* Enable Interrupts. */
334         if (ei_debug > 1) printk("reset done\n");
335         return;
336 }
337
338 /* Grab the 8390 specific header. Similar to the block_input routine, but
339    we don't need to be concerned with ring wrap as the header will be at
340    the start of a page, so we optimize accordingly. */
341
342 static void ultra32_get_8390_hdr(struct net_device *dev,
343                                  struct e8390_pkt_hdr *hdr,
344                                  int ring_page)
345 {
346         unsigned long hdr_start = dev->mem_start + ((ring_page & 0x1f) << 8);
347         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
348
349         /* Select correct 8KB Window. */
350         outb(ei_status.reg0 | ((ring_page & 0x60) >> 5), RamReg);
351
352 #ifdef __BIG_ENDIAN
353         /* Officially this is what we are doing, but the readl() is faster */
354         /* unfortunately it isn't endian aware of the struct               */
355         isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
356         hdr->count = le16_to_cpu(hdr->count);
357 #else
358         ((unsigned int*)hdr)[0] = isa_readl(hdr_start);
359 #endif
360 }
361
362 /* Block input and output are easy on shared memory ethercards, the only
363    complication is when the ring buffer wraps, or in this case, when a
364    packet spans an 8KB boundary. Note that the current 8KB segment is
365    already set by the get_8390_hdr routine. */
366
367 static void ultra32_block_input(struct net_device *dev,
368                                 int count,
369                                 struct sk_buff *skb,
370                                 int ring_offset)
371 {
372         unsigned long xfer_start = dev->mem_start + (ring_offset & 0x1fff);
373         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
374
375         if ((ring_offset & ~0x1fff) != ((ring_offset + count - 1) & ~0x1fff)) {
376                 int semi_count = 8192 - (ring_offset & 0x1FFF);
377                 isa_memcpy_fromio(skb->data, xfer_start, semi_count);
378                 count -= semi_count;
379                 if (ring_offset < 96*256) {
380                         /* Select next 8KB Window. */
381                         ring_offset += semi_count;
382                         outb(ei_status.reg0 | ((ring_offset & 0x6000) >> 13), RamReg);
383                         isa_memcpy_fromio(skb->data + semi_count, dev->mem_start, count);
384                 } else {
385                         /* Select first 8KB Window. */
386                         outb(ei_status.reg0, RamReg);
387                         isa_memcpy_fromio(skb->data + semi_count, ei_status.rmem_start, count);
388                 }
389         } else {
390                 /* Packet is in one chunk -- we can copy + cksum. */
391                 isa_eth_io_copy_and_sum(skb, xfer_start, count, 0);
392         }
393 }
394
395 static void ultra32_block_output(struct net_device *dev,
396                                  int count,
397                                  const unsigned char *buf,
398                                  int start_page)
399 {
400         unsigned long xfer_start = dev->mem_start + (start_page<<8);
401         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
402
403         /* Select first 8KB Window. */
404         outb(ei_status.reg0, RamReg);
405
406         isa_memcpy_toio(xfer_start, buf, count);
407 }
408 \f
409 #ifdef MODULE
410 #define MAX_ULTRA32_CARDS   4   /* Max number of Ultra cards per module */
411 static struct net_device *dev_ultra[MAX_ULTRA32_CARDS];
412
413 MODULE_DESCRIPTION("SMC Ultra32 EISA ethernet driver");
414 MODULE_LICENSE("GPL");
415
416 int init_module(void)
417 {
418         int this_dev, found = 0;
419
420         for (this_dev = 0; this_dev < MAX_ULTRA32_CARDS; this_dev++) {
421                 struct net_device *dev = ultra32_probe(-1);
422                 if (IS_ERR(dev))
423                         break;
424                 dev_ultra[found++] = dev;
425         }
426         if (found)
427                 return 0;
428         printk(KERN_WARNING "smc-ultra32.c: No SMC Ultra32 found.\n");
429         return -ENXIO;
430 }
431
432 void cleanup_module(void)
433 {
434         int this_dev;
435
436         for (this_dev = 0; this_dev < MAX_ULTRA32_CARDS; this_dev++) {
437                 struct net_device *dev = dev_ultra[this_dev];
438                 if (dev) {
439                         unregister_netdev(dev);
440                         cleanup_card(dev);
441                         free_netdev(dev);
442                 }
443         }
444 }
445 #endif /* MODULE */
446