Import 2.3.18pre1
[davej-history.git] / drivers / net / smc-ultra.c
blob938eaf3897d1ab15d62ae6c1f56824f7cb914508
1 /* smc-ultra.c: A SMC Ultra ethernet driver for linux. */
2 /*
3 This is a driver for the SMC Ultra and SMC EtherEZ ISA ethercards.
5 Written 1993-1998 by Donald Becker.
7 Copyright 1993 United States Government as represented by the
8 Director, National Security Agency.
10 This software may be used and distributed according to the terms
11 of the GNU Public License, incorporated herein by reference.
13 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
14 Center of Excellence in Space Data and Information Sciences
15 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
17 This driver uses the cards in the 8390-compatible mode.
18 Most of the run-time complexity is handled by the generic code in
19 8390.c. The code in this file is responsible for
21 ultra_probe() Detecting and initializing the card.
22 ultra_probe1()
24 ultra_open() The card-specific details of starting, stopping
25 ultra_reset_8390() and resetting the 8390 NIC core.
26 ultra_close()
28 ultra_block_input() Routines for reading and writing blocks of
29 ultra_block_output() packet buffer memory.
30 ultra_pio_input()
31 ultra_pio_output()
33 This driver enables the shared memory only when doing the actual data
34 transfers to avoid a bug in early version of the card that corrupted
35 data transferred by a AHA1542.
37 This driver now supports the programmed-I/O (PIO) data transfer mode of
38 the EtherEZ. It does not use the non-8390-compatible "Altego" mode.
39 That support (if available) is in smc-ez.c.
41 Changelog:
43 Paul Gortmaker : multiple card support for module users.
44 Donald Becker : 4/17/96 PIO support, minor potential problems avoided.
45 Donald Becker : 6/6/96 correctly set auto-wrap bit.
48 static const char *version =
49 "smc-ultra.c:v2.02 2/3/98 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
51 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/sched.h>
55 #include <linux/errno.h>
56 #include <linux/string.h>
57 #include <linux/init.h>
58 #include <asm/io.h>
59 #include <asm/system.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include "8390.h"
65 /* A zero-terminated list of I/O addresses to be probed. */
66 static unsigned int ultra_portlist[] __initdata =
67 {0x200, 0x220, 0x240, 0x280, 0x300, 0x340, 0x380, 0};
69 int ultra_probe(struct net_device *dev);
70 int ultra_probe1(struct net_device *dev, int ioaddr);
72 static int ultra_open(struct net_device *dev);
73 static void ultra_reset_8390(struct net_device *dev);
74 static void ultra_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
75 int ring_page);
76 static void ultra_block_input(struct net_device *dev, int count,
77 struct sk_buff *skb, int ring_offset);
78 static void ultra_block_output(struct net_device *dev, int count,
79 const unsigned char *buf, const int start_page);
80 static void ultra_pio_get_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
81 int ring_page);
82 static void ultra_pio_input(struct net_device *dev, int count,
83 struct sk_buff *skb, int ring_offset);
84 static void ultra_pio_output(struct net_device *dev, int count,
85 const unsigned char *buf, const int start_page);
86 static int ultra_close_card(struct net_device *dev);
89 #define START_PG 0x00 /* First page of TX buffer */
91 #define ULTRA_CMDREG 0 /* Offset to ASIC command register. */
92 #define ULTRA_RESET 0x80 /* Board reset, in ULTRA_CMDREG. */
93 #define ULTRA_MEMENB 0x40 /* Enable the shared memory. */
94 #define IOPD 0x02 /* I/O Pipe Data (16 bits), PIO operation. */
95 #define IOPA 0x07 /* I/O Pipe Address for PIO operation. */
96 #define ULTRA_NIC_OFFSET 16 /* NIC register offset from the base_addr. */
97 #define ULTRA_IO_EXTENT 32
98 #define EN0_ERWCNT 0x08 /* Early receive warning count. */
100 /* Probe for the Ultra. This looks like a 8013 with the station
101 address PROM at I/O ports <base>+8 to <base>+13, with a checksum
102 following.
104 #ifdef HAVE_DEVLIST
105 struct netdev_entry ultra_drv =
106 {"ultra", ultra_probe1, NETCARD_IO_EXTENT, netcard_portlist};
107 #else
109 int __init ultra_probe(struct net_device *dev)
111 int i;
112 int base_addr = dev ? dev->base_addr : 0;
114 if (base_addr > 0x1ff) /* Check a single specified location. */
115 return ultra_probe1(dev, base_addr);
116 else if (base_addr != 0) /* Don't probe at all. */
117 return ENXIO;
119 for (i = 0; ultra_portlist[i]; i++) {
120 int ioaddr = ultra_portlist[i];
121 if (check_region(ioaddr, ULTRA_IO_EXTENT))
122 continue;
123 if (ultra_probe1(dev, ioaddr) == 0)
124 return 0;
127 return ENODEV;
129 #endif
131 int __init ultra_probe1(struct net_device *dev, int ioaddr)
133 int i;
134 int checksum = 0;
135 const char *model_name;
136 unsigned char eeprom_irq = 0;
137 static unsigned version_printed = 0;
138 /* Values from various config regs. */
139 unsigned char num_pages, irqreg, addr, piomode;
140 unsigned char idreg = inb(ioaddr + 7);
141 unsigned char reg4 = inb(ioaddr + 4) & 0x7f;
143 /* Check the ID nibble. */
144 if ((idreg & 0xF0) != 0x20 /* SMC Ultra */
145 && (idreg & 0xF0) != 0x40) /* SMC EtherEZ */
146 return ENODEV;
148 /* Select the station address register set. */
149 outb(reg4, ioaddr + 4);
151 for (i = 0; i < 8; i++)
152 checksum += inb(ioaddr + 8 + i);
153 if ((checksum & 0xff) != 0xFF)
154 return ENODEV;
156 if (load_8390_module("smc-ultra.c"))
157 return -ENOSYS;
159 if (dev == NULL)
160 dev = init_etherdev(0, 0);
162 if (ei_debug && version_printed++ == 0)
163 printk(version);
165 model_name = (idreg & 0xF0) == 0x20 ? "SMC Ultra" : "SMC EtherEZ";
167 printk("%s: %s at %#3x,", dev->name, model_name, ioaddr);
169 for (i = 0; i < 6; i++)
170 printk(" %2.2X", dev->dev_addr[i] = inb(ioaddr + 8 + i));
172 /* Switch from the station address to the alternate register set and
173 read the useful registers there. */
174 outb(0x80 | reg4, ioaddr + 4);
176 /* Enabled FINE16 mode to avoid BIOS ROM width mismatches @ reboot. */
177 outb(0x80 | inb(ioaddr + 0x0c), ioaddr + 0x0c);
178 piomode = inb(ioaddr + 0x8);
179 addr = inb(ioaddr + 0xb);
180 irqreg = inb(ioaddr + 0xd);
182 /* Switch back to the station address register set so that the MS-DOS driver
183 can find the card after a warm boot. */
184 outb(reg4, ioaddr + 4);
186 if (dev->irq < 2) {
187 unsigned char irqmap[] = {0, 9, 3, 5, 7, 10, 11, 15};
188 int irq;
190 /* The IRQ bits are split. */
191 irq = irqmap[((irqreg & 0x40) >> 4) + ((irqreg & 0x0c) >> 2)];
193 if (irq == 0) {
194 printk(", failed to detect IRQ line.\n");
195 return -EAGAIN;
197 dev->irq = irq;
198 eeprom_irq = 1;
201 /* Allocate dev->priv and fill in 8390 specific dev fields. */
202 if (ethdev_init(dev)) {
203 printk (", no memory for dev->priv.\n");
204 return -ENOMEM;
207 /* OK, we are certain this is going to work. Setup the device. */
208 request_region(ioaddr, ULTRA_IO_EXTENT, model_name);
210 /* The 8390 isn't at the base address, so fake the offset */
211 dev->base_addr = ioaddr+ULTRA_NIC_OFFSET;
214 int addr_tbl[4] = {0x0C0000, 0x0E0000, 0xFC0000, 0xFE0000};
215 short num_pages_tbl[4] = {0x20, 0x40, 0x80, 0xff};
217 dev->mem_start = ((addr & 0x0f) << 13) + addr_tbl[(addr >> 6) & 3] ;
218 num_pages = num_pages_tbl[(addr >> 4) & 3];
221 ei_status.name = model_name;
222 ei_status.word16 = 1;
223 ei_status.tx_start_page = START_PG;
224 ei_status.rx_start_page = START_PG + TX_PAGES;
225 ei_status.stop_page = num_pages;
227 dev->rmem_start = dev->mem_start + TX_PAGES*256;
228 dev->mem_end = dev->rmem_end
229 = dev->mem_start + (ei_status.stop_page - START_PG)*256;
231 if (piomode) {
232 printk(",%s IRQ %d programmed-I/O mode.\n",
233 eeprom_irq ? "EEPROM" : "assigned ", dev->irq);
234 ei_status.block_input = &ultra_pio_input;
235 ei_status.block_output = &ultra_pio_output;
236 ei_status.get_8390_hdr = &ultra_pio_get_hdr;
237 } else {
238 printk(",%s IRQ %d memory %#lx-%#lx.\n", eeprom_irq ? "" : "assigned ",
239 dev->irq, dev->mem_start, dev->mem_end-1);
240 ei_status.block_input = &ultra_block_input;
241 ei_status.block_output = &ultra_block_output;
242 ei_status.get_8390_hdr = &ultra_get_8390_hdr;
244 ei_status.reset_8390 = &ultra_reset_8390;
245 dev->open = &ultra_open;
246 dev->stop = &ultra_close_card;
247 NS8390_init(dev, 0);
249 return 0;
252 static int
253 ultra_open(struct net_device *dev)
255 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
256 unsigned char irq2reg[] = {0, 0, 0x04, 0x08, 0, 0x0C, 0, 0x40,
257 0, 0x04, 0x44, 0x48, 0, 0, 0, 0x4C, };
259 if (request_irq(dev->irq, ei_interrupt, 0, ei_status.name, dev))
260 return -EAGAIN;
262 outb(0x00, ioaddr); /* Disable shared memory for safety. */
263 outb(0x80, ioaddr + 5);
264 /* Set the IRQ line. */
265 outb(inb(ioaddr + 4) | 0x80, ioaddr + 4);
266 outb((inb(ioaddr + 13) & ~0x4C) | irq2reg[dev->irq], ioaddr + 13);
267 outb(inb(ioaddr + 4) & 0x7f, ioaddr + 4);
269 if (ei_status.block_input == &ultra_pio_input) {
270 outb(0x11, ioaddr + 6); /* Enable interrupts and PIO. */
271 outb(0x01, ioaddr + 0x19); /* Enable ring read auto-wrap. */
272 } else
273 outb(0x01, ioaddr + 6); /* Enable interrupts and memory. */
274 /* Set the early receive warning level in window 0 high enough not
275 to receive ERW interrupts. */
276 outb_p(E8390_NODMA+E8390_PAGE0, dev->base_addr);
277 outb(0xff, dev->base_addr + EN0_ERWCNT);
278 ei_open(dev);
279 MOD_INC_USE_COUNT;
280 return 0;
283 static void
284 ultra_reset_8390(struct net_device *dev)
286 int cmd_port = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC base addr */
288 outb(ULTRA_RESET, cmd_port);
289 if (ei_debug > 1) printk("resetting Ultra, t=%ld...", jiffies);
290 ei_status.txing = 0;
292 outb(0x00, cmd_port); /* Disable shared memory for safety. */
293 outb(0x80, cmd_port + 5);
294 if (ei_status.block_input == &ultra_pio_input)
295 outb(0x11, cmd_port + 6); /* Enable interrupts and PIO. */
296 else
297 outb(0x01, cmd_port + 6); /* Enable interrupts and memory. */
299 if (ei_debug > 1) printk("reset done\n");
300 return;
303 /* Grab the 8390 specific header. Similar to the block_input routine, but
304 we don't need to be concerned with ring wrap as the header will be at
305 the start of a page, so we optimize accordingly. */
307 static void
308 ultra_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
310 unsigned long hdr_start = dev->mem_start + ((ring_page - START_PG)<<8);
312 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET); /* shmem on */
313 #ifdef notdef
314 /* Officially this is what we are doing, but the readl() is faster */
315 memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
316 #else
317 ((unsigned int*)hdr)[0] = readl(hdr_start);
318 #endif
319 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* shmem off */
322 /* Block input and output are easy on shared memory ethercards, the only
323 complication is when the ring buffer wraps. */
325 static void
326 ultra_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
328 unsigned long xfer_start = dev->mem_start + ring_offset - (START_PG<<8);
330 /* Enable shared memory. */
331 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET);
333 if (xfer_start + count > dev->rmem_end) {
334 /* We must wrap the input move. */
335 int semi_count = dev->rmem_end - xfer_start;
336 memcpy_fromio(skb->data, xfer_start, semi_count);
337 count -= semi_count;
338 memcpy_fromio(skb->data + semi_count, dev->rmem_start, count);
339 } else {
340 /* Packet is in one chunk -- we can copy + cksum. */
341 eth_io_copy_and_sum(skb, xfer_start, count, 0);
344 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* Disable memory. */
347 static void
348 ultra_block_output(struct net_device *dev, int count, const unsigned char *buf,
349 int start_page)
351 unsigned long shmem = dev->mem_start + ((start_page - START_PG)<<8);
353 /* Enable shared memory. */
354 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET);
356 memcpy_toio(shmem, buf, count);
358 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* Disable memory. */
361 /* The identical operations for programmed I/O cards.
362 The PIO model is trivial to use: the 16 bit start address is written
363 byte-sequentially to IOPA, with no intervening I/O operations, and the
364 data is read or written to the IOPD data port.
365 The only potential complication is that the address register is shared
366 and must be always be rewritten between each read/write direction change.
367 This is no problem for us, as the 8390 code ensures that we are single
368 threaded. */
369 static void ultra_pio_get_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
370 int ring_page)
372 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
373 outb(0x00, ioaddr + IOPA); /* Set the address, LSB first. */
374 outb(ring_page, ioaddr + IOPA);
375 insw(ioaddr + IOPD, hdr, sizeof(struct e8390_pkt_hdr)>>1);
378 static void ultra_pio_input(struct net_device *dev, int count,
379 struct sk_buff *skb, int ring_offset)
381 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
382 char *buf = skb->data;
384 /* For now set the address again, although it should already be correct. */
385 outb(ring_offset, ioaddr + IOPA); /* Set the address, LSB first. */
386 outb(ring_offset >> 8, ioaddr + IOPA);
387 /* We know skbuffs are padded to at least word alignment. */
388 insw(ioaddr + IOPD, buf, (count+1)>>1);
391 static void ultra_pio_output(struct net_device *dev, int count,
392 const unsigned char *buf, const int start_page)
394 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
395 outb(0x00, ioaddr + IOPA); /* Set the address, LSB first. */
396 outb(start_page, ioaddr + IOPA);
397 /* An extra odd byte is OK here as well. */
398 outsw(ioaddr + IOPD, buf, (count+1)>>1);
401 static int
402 ultra_close_card(struct net_device *dev)
404 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* CMDREG */
406 dev->start = 0;
407 dev->tbusy = 1;
409 if (ei_debug > 1)
410 printk("%s: Shutting down ethercard.\n", dev->name);
412 outb(0x00, ioaddr + 6); /* Disable interrupts. */
413 free_irq(dev->irq, dev);
415 NS8390_init(dev, 0);
417 /* We should someday disable shared memory and change to 8-bit mode
418 "just in case"... */
420 MOD_DEC_USE_COUNT;
422 return 0;
426 #ifdef MODULE
427 #define MAX_ULTRA_CARDS 4 /* Max number of Ultra cards per module */
428 #define NAMELEN 8 /* # of chars for storing dev->name */
429 static char namelist[NAMELEN * MAX_ULTRA_CARDS] = { 0, };
430 static struct net_device dev_ultra[MAX_ULTRA_CARDS] = {
432 NULL, /* assign a chunk of namelist[] below */
433 0, 0, 0, 0,
434 0, 0,
435 0, 0, 0, NULL, NULL
439 static int io[MAX_ULTRA_CARDS] = { 0, };
440 static int irq[MAX_ULTRA_CARDS] = { 0, };
442 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_ULTRA_CARDS) "i");
443 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_ULTRA_CARDS) "i");
445 EXPORT_NO_SYMBOLS;
447 /* This is set up so that only a single autoprobe takes place per call.
448 ISA device autoprobes on a running machine are not recommended. */
450 init_module(void)
452 int this_dev, found = 0;
454 for (this_dev = 0; this_dev < MAX_ULTRA_CARDS; this_dev++) {
455 struct net_device *dev = &dev_ultra[this_dev];
456 dev->name = namelist+(NAMELEN*this_dev);
457 dev->irq = irq[this_dev];
458 dev->base_addr = io[this_dev];
459 dev->init = ultra_probe;
460 if (io[this_dev] == 0) {
461 if (this_dev != 0) break; /* only autoprobe 1st one */
462 printk(KERN_NOTICE "smc-ultra.c: Presently autoprobing (not recommended) for a single card.\n");
464 if (register_netdev(dev) != 0) {
465 printk(KERN_WARNING "smc-ultra.c: No SMC Ultra card found (i/o = 0x%x).\n", io[this_dev]);
466 if (found != 0) return 0; /* Got at least one. */
467 return -ENXIO;
469 found++;
472 return 0;
475 void
476 cleanup_module(void)
478 int this_dev;
480 for (this_dev = 0; this_dev < MAX_ULTRA_CARDS; this_dev++) {
481 struct net_device *dev = &dev_ultra[this_dev];
482 if (dev->priv != NULL) {
483 /* NB: ultra_close_card() does free_irq + irq2dev */
484 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET;
485 kfree(dev->priv);
486 release_region(ioaddr, ULTRA_IO_EXTENT);
487 unregister_netdev(dev);
488 dev->priv = NULL;
492 #endif /* MODULE */
496 * Local variables:
497 * compile-command: "gcc -D__KERNEL__ -Wall -O6 -I/usr/src/linux/net/inet -c smc-ultra.c"
498 * version-control: t
499 * kept-new-versions: 5
500 * c-indent-level: 4
501 * c-basic-offset: 4
502 * tab-width: 4
503 * End: