Import 2.1.36
[davej-history.git] / drivers / net / smc-ultra.c
blob30c6b49070eeefdbfcc78c9aaedaf379d0331586
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-1996 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, shared memory 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.
31 This driver enables the shared memory only when doing the actual data
32 transfers to avoid a bug in early version of the card that corrupted
33 data transferred by a AHA1542.
35 This driver now supports the programmed-I/O (PIO) data transfer mode of
36 the EtherEZ. It does not use the non-8390-compatible "Altego" mode.
37 That support (if available) is smc-ez.c.
39 Changelog:
41 Paul Gortmaker : multiple card support for module users.
42 Donald Becker : 4/17/96 PIO support, minor potential problems avoided.
43 Donald Becker : 6/6/96 correctly set auto-wrap bit.
46 static const char *version =
47 "smc-ultra.c:v2.00 6/6/96 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
50 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/errno.h>
55 #include <linux/string.h>
56 #include <linux/init.h>
57 #include <asm/io.h>
58 #include <asm/system.h>
60 #include <linux/netdevice.h>
61 #include <linux/etherdevice.h>
62 #include "8390.h"
64 /* A zero-terminated list of I/O addresses to be probed. */
65 static unsigned int ultra_portlist[] __initdata =
66 {0x200, 0x220, 0x240, 0x280, 0x300, 0x340, 0x380, 0};
68 int ultra_probe(struct device *dev);
69 int ultra_probe1(struct device *dev, int ioaddr);
71 static int ultra_open(struct device *dev);
72 static void ultra_reset_8390(struct device *dev);
73 static void ultra_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr,
74 int ring_page);
75 static void ultra_block_input(struct device *dev, int count,
76 struct sk_buff *skb, int ring_offset);
77 static void ultra_block_output(struct device *dev, int count,
78 const unsigned char *buf, const start_page);
79 static void ultra_pio_get_hdr(struct device *dev, struct e8390_pkt_hdr *hdr,
80 int ring_page);
81 static void ultra_pio_input(struct device *dev, int count,
82 struct sk_buff *skb, int ring_offset);
83 static void ultra_pio_output(struct device *dev, int count,
84 const unsigned char *buf, const start_page);
85 static int ultra_close_card(struct device *dev);
88 #define START_PG 0x00 /* First page of TX buffer */
90 #define ULTRA_CMDREG 0 /* Offset to ASIC command register. */
91 #define ULTRA_RESET 0x80 /* Board reset, in ULTRA_CMDREG. */
92 #define ULTRA_MEMENB 0x40 /* Enable the shared memory. */
93 #define IOPD 0x02 /* I/O Pipe Data (16 bits), PIO operation. */
94 #define IOPA 0x07 /* I/O Pipe Address for PIO operation. */
95 #define ULTRA_NIC_OFFSET 16 /* NIC register offset from the base_addr. */
96 #define ULTRA_IO_EXTENT 32
97 #define EN0_ERWCNT 0x08 /* Early receive warning count. */
99 /* Probe for the Ultra. This looks like a 8013 with the station
100 address PROM at I/O ports <base>+8 to <base>+13, with a checksum
101 following.
103 #ifdef HAVE_DEVLIST
104 struct netdev_entry ultra_drv =
105 {"ultra", ultra_probe1, NETCARD_IO_EXTENT, netcard_portlist};
106 #else
108 __initfunc(int ultra_probe(struct device *dev))
110 int i;
111 int base_addr = dev ? dev->base_addr : 0;
113 if (base_addr > 0x1ff) /* Check a single specified location. */
114 return ultra_probe1(dev, base_addr);
115 else if (base_addr != 0) /* Don't probe at all. */
116 return ENXIO;
118 for (i = 0; ultra_portlist[i]; i++) {
119 int ioaddr = ultra_portlist[i];
120 if (check_region(ioaddr, ULTRA_IO_EXTENT))
121 continue;
122 if (ultra_probe1(dev, ioaddr) == 0)
123 return 0;
126 return ENODEV;
128 #endif
130 __initfunc(int ultra_probe1(struct device *dev, int ioaddr))
132 int i;
133 int checksum = 0;
134 const char *model_name;
135 unsigned char eeprom_irq = 0;
136 static unsigned version_printed = 0;
137 /* Values from various config regs. */
138 unsigned char num_pages, irqreg, addr, piomode;
139 unsigned char idreg = inb(ioaddr + 7);
140 unsigned char reg4 = inb(ioaddr + 4) & 0x7f;
142 /* Check the ID nibble. */
143 if ((idreg & 0xF0) != 0x20 /* SMC Ultra */
144 && (idreg & 0xF0) != 0x40) /* SMC EtherEZ */
145 return ENODEV;
147 /* Select the station address register set. */
148 outb(reg4, ioaddr + 4);
150 for (i = 0; i < 8; i++)
151 checksum += inb(ioaddr + 8 + i);
152 if ((checksum & 0xff) != 0xFF)
153 return ENODEV;
155 /* We should have a "dev" from Space.c or the static module table. */
156 if (dev == NULL) {
157 printk("smc-ultra.c: Passed a NULL device.\n");
158 dev = init_etherdev(0, 0);
161 if (ei_debug && version_printed++ == 0)
162 printk(version);
164 model_name = (idreg & 0xF0) == 0x20 ? "SMC Ultra" : "SMC EtherEZ";
166 printk("%s: %s at %#3x,", dev->name, model_name, ioaddr);
168 for (i = 0; i < 6; i++)
169 printk(" %2.2X", dev->dev_addr[i] = inb(ioaddr + 8 + i));
171 /* Switch from the station address to the alternate register set and
172 read the useful registers there. */
173 outb(0x80 | reg4, ioaddr + 4);
175 /* Enabled FINE16 mode to avoid BIOS ROM width mismatches @ reboot. */
176 outb(0x80 | inb(ioaddr + 0x0c), ioaddr + 0x0c);
177 piomode = inb(ioaddr + 0x8);
178 addr = inb(ioaddr + 0xb);
179 irqreg = inb(ioaddr + 0xd);
181 /* Switch back to the station address register set so that the MS-DOS driver
182 can find the card after a warm boot. */
183 outb(reg4, ioaddr + 4);
185 if (dev->irq < 2) {
186 unsigned char irqmap[] = {0, 9, 3, 5, 7, 10, 11, 15};
187 int irq;
189 /* The IRQ bits are split. */
190 irq = irqmap[((irqreg & 0x40) >> 4) + ((irqreg & 0x0c) >> 2)];
192 if (irq == 0) {
193 printk(", failed to detect IRQ line.\n");
194 return -EAGAIN;
196 dev->irq = irq;
197 eeprom_irq = 1;
200 /* Allocate dev->priv and fill in 8390 specific dev fields. */
201 if (ethdev_init(dev)) {
202 printk (", no memory for dev->priv.\n");
203 return -ENOMEM;
206 /* OK, we are certain this is going to work. Setup the device. */
207 request_region(ioaddr, ULTRA_IO_EXTENT, model_name);
209 /* The 8390 isn't at the base address, so fake the offset */
210 dev->base_addr = ioaddr+ULTRA_NIC_OFFSET;
213 int addr_tbl[4] = {0x0C0000, 0x0E0000, 0xFC0000, 0xFE0000};
214 short num_pages_tbl[4] = {0x20, 0x40, 0x80, 0xff};
216 dev->mem_start = ((addr & 0x0f) << 13) + addr_tbl[(addr >> 6) & 3] ;
217 num_pages = num_pages_tbl[(addr >> 4) & 3];
220 ei_status.name = model_name;
221 ei_status.word16 = 1;
222 ei_status.tx_start_page = START_PG;
223 ei_status.rx_start_page = START_PG + TX_PAGES;
224 ei_status.stop_page = num_pages;
226 dev->rmem_start = dev->mem_start + TX_PAGES*256;
227 dev->mem_end = dev->rmem_end
228 = dev->mem_start + (ei_status.stop_page - START_PG)*256;
230 if (piomode) {
231 printk(",%s IRQ %d programmed-I/O mode.\n",
232 eeprom_irq ? "EEPROM" : "assigned ", dev->irq);
233 ei_status.block_input = &ultra_pio_input;
234 ei_status.block_output = &ultra_pio_output;
235 ei_status.get_8390_hdr = &ultra_pio_get_hdr;
236 } else {
237 printk(",%s IRQ %d memory %#lx-%#lx.\n", eeprom_irq ? "" : "assigned ",
238 dev->irq, dev->mem_start, dev->mem_end-1);
239 ei_status.block_input = &ultra_block_input;
240 ei_status.block_output = &ultra_block_output;
241 ei_status.get_8390_hdr = &ultra_get_8390_hdr;
243 ei_status.reset_8390 = &ultra_reset_8390;
244 dev->open = &ultra_open;
245 dev->stop = &ultra_close_card;
246 NS8390_init(dev, 0);
248 return 0;
251 static int
252 ultra_open(struct device *dev)
254 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
256 if (request_irq(dev->irq, ei_interrupt, 0, ei_status.name, NULL))
257 return -EAGAIN;
259 outb(0x00, ioaddr); /* Disable shared memory for safety. */
260 outb(0x80, ioaddr + 5);
261 if (ei_status.block_input == &ultra_pio_input) {
262 outb(0x11, ioaddr + 6); /* Enable interrupts and PIO. */
263 outb(0x01, ioaddr + 0x19); /* Enable ring read auto-wrap. */
264 } else
265 outb(0x01, ioaddr + 6); /* Enable interrupts and memory. */
266 /* Set the early receive warning level in window 0 high enough not
267 to receive ERW interrupts. */
268 outb_p(E8390_NODMA+E8390_PAGE0, dev->base_addr);
269 outb(0xff, dev->base_addr + EN0_ERWCNT);
270 ei_open(dev);
271 MOD_INC_USE_COUNT;
272 return 0;
275 static void
276 ultra_reset_8390(struct device *dev)
278 int cmd_port = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC base addr */
280 outb(ULTRA_RESET, cmd_port);
281 if (ei_debug > 1) printk("resetting Ultra, t=%ld...", jiffies);
282 ei_status.txing = 0;
284 outb(0x00, cmd_port); /* Disable shared memory for safety. */
285 outb(0x80, cmd_port + 5);
286 if (ei_status.block_input == &ultra_pio_input)
287 outb(0x11, cmd_port + 6); /* Enable interrupts and PIO. */
288 else
289 outb(0x01, cmd_port + 6); /* Enable interrupts and memory. */
291 if (ei_debug > 1) printk("reset done\n");
292 return;
295 /* Grab the 8390 specific header. Similar to the block_input routine, but
296 we don't need to be concerned with ring wrap as the header will be at
297 the start of a page, so we optimize accordingly. */
299 static void
300 ultra_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
302 unsigned long hdr_start = dev->mem_start + ((ring_page - START_PG)<<8);
304 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET); /* shmem on */
305 #ifdef notdef
306 /* Officially this is what we are doing, but the readl() is faster */
307 memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
308 #else
309 ((unsigned int*)hdr)[0] = readl(hdr_start);
310 #endif
311 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* shmem off */
314 /* Block input and output are easy on shared memory ethercards, the only
315 complication is when the ring buffer wraps. */
317 static void
318 ultra_block_input(struct device *dev, int count, struct sk_buff *skb, int ring_offset)
320 unsigned long xfer_start = dev->mem_start + ring_offset - (START_PG<<8);
322 /* Enable shared memory. */
323 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET);
325 if (xfer_start + count > dev->rmem_end) {
326 /* We must wrap the input move. */
327 int semi_count = dev->rmem_end - xfer_start;
328 memcpy_fromio(skb->data, xfer_start, semi_count);
329 count -= semi_count;
330 memcpy_fromio(skb->data + semi_count, dev->rmem_start, count);
331 } else {
332 /* Packet is in one chunk -- we can copy + cksum. */
333 eth_io_copy_and_sum(skb, xfer_start, count, 0);
336 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* Disable memory. */
339 static void
340 ultra_block_output(struct device *dev, int count, const unsigned char *buf,
341 int start_page)
343 unsigned long shmem = dev->mem_start + ((start_page - START_PG)<<8);
345 /* Enable shared memory. */
346 outb(ULTRA_MEMENB, dev->base_addr - ULTRA_NIC_OFFSET);
348 memcpy_toio(shmem, buf, count);
350 outb(0x00, dev->base_addr - ULTRA_NIC_OFFSET); /* Disable memory. */
353 /* The identical operations for programmed I/O cards.
354 The PIO model is trivial to use: the 16 bit start address is written
355 byte-sequentially to IOPA, with no intervening I/O operations, and the
356 data is read or written to the IOPD data port.
357 The only potential complication is that the address register is shared
358 must be always be rewritten between each read/write direction change.
359 This is no problem for us, as the 8390 code ensures that we are single
360 threaded. */
361 static void ultra_pio_get_hdr(struct device *dev, struct e8390_pkt_hdr *hdr,
362 int ring_page)
364 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
365 outb(0x00, ioaddr + IOPA); /* Set the address, LSB first. */
366 outb(ring_page, ioaddr + IOPA);
367 insw(ioaddr + IOPD, hdr, sizeof(struct e8390_pkt_hdr)>>1);
370 static void ultra_pio_input(struct device *dev, int count,
371 struct sk_buff *skb, int ring_offset)
373 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
374 char *buf = skb->data;
376 /* For now set the address again, although it should already be correct. */
377 outb(ring_offset, ioaddr + IOPA); /* Set the address, LSB first. */
378 outb(ring_offset >> 8, ioaddr + IOPA);
379 insw(ioaddr + IOPD, buf, (count+1)>>1);
380 #ifdef notdef
381 /* We don't need this -- skbuffs are padded to at least word alignment. */
382 if (count & 0x01) {
383 buf[count-1] = inb(ioaddr + IOPD);
384 #endif
387 static void ultra_pio_output(struct device *dev, int count,
388 const unsigned char *buf, const start_page)
390 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* ASIC addr */
391 outb(0x00, ioaddr + IOPA); /* Set the address, LSB first. */
392 outb(start_page, ioaddr + IOPA);
393 outsw(ioaddr + IOPD, buf, (count+1)>>1);
396 static int
397 ultra_close_card(struct device *dev)
399 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET; /* CMDREG */
401 dev->start = 0;
402 dev->tbusy = 1;
404 if (ei_debug > 1)
405 printk("%s: Shutting down ethercard.\n", dev->name);
407 outb(0x00, ioaddr + 6); /* Disable interrupts. */
408 free_irq(dev->irq, NULL);
409 irq2dev_map[dev->irq] = 0;
411 NS8390_init(dev, 0);
413 /* We should someday disable shared memory and change to 8-bit mode
414 "just in case"... */
416 MOD_DEC_USE_COUNT;
418 return 0;
422 #ifdef MODULE
423 #define MAX_ULTRA_CARDS 4 /* Max number of Ultra cards per module */
424 #define NAMELEN 8 /* # of chars for storing dev->name */
425 static char namelist[NAMELEN * MAX_ULTRA_CARDS] = { 0, };
426 static struct device dev_ultra[MAX_ULTRA_CARDS] = {
428 NULL, /* assign a chunk of namelist[] below */
429 0, 0, 0, 0,
430 0, 0,
431 0, 0, 0, NULL, NULL
435 static int io[MAX_ULTRA_CARDS] = { 0, };
436 static int irq[MAX_ULTRA_CARDS] = { 0, };
438 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_ULTRA_CARDS) "i");
439 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_ULTRA_CARDS) "i");
441 /* This is set up so that only a single autoprobe takes place per call.
442 ISA device autoprobes on a running machine are not recommended. */
444 init_module(void)
446 int this_dev, found = 0;
448 for (this_dev = 0; this_dev < MAX_ULTRA_CARDS; this_dev++) {
449 struct device *dev = &dev_ultra[this_dev];
450 dev->name = namelist+(NAMELEN*this_dev);
451 dev->irq = irq[this_dev];
452 dev->base_addr = io[this_dev];
453 dev->init = ultra_probe;
454 if (io[this_dev] == 0) {
455 if (this_dev != 0) break; /* only autoprobe 1st one */
456 printk(KERN_NOTICE "smc-ultra.c: Presently autoprobing (not recommended) for a single card.\n");
458 if (register_netdev(dev) != 0) {
459 printk(KERN_WARNING "smc-ultra.c: No SMC Ultra card found (i/o = 0x%x).\n", io[this_dev]);
460 if (found != 0) return 0; /* Got at least one. */
461 return -ENXIO;
463 found++;
466 return 0;
469 void
470 cleanup_module(void)
472 int this_dev;
474 for (this_dev = 0; this_dev < MAX_ULTRA_CARDS; this_dev++) {
475 struct device *dev = &dev_ultra[this_dev];
476 if (dev->priv != NULL) {
477 /* NB: ultra_close_card() does free_irq + irq2dev */
478 int ioaddr = dev->base_addr - ULTRA_NIC_OFFSET;
479 kfree(dev->priv);
480 dev->priv = NULL;
481 release_region(ioaddr, ULTRA_IO_EXTENT);
482 unregister_netdev(dev);
486 #endif /* MODULE */
490 * Local variables:
491 * compile-command: "gcc -D__KERNEL__ -Wall -O6 -I/usr/src/linux/net/inet -c smc-ultra.c"
492 * version-control: t
493 * kept-new-versions: 5
494 * c-indent-level: 4
495 * tab-width: 4
496 * End: