* add p cc
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / net / 3c503.c
blobbf86c6fa887319889ed685f95497576745694798
1 /* 3c503.c: A shared-memory NS8390 ethernet driver for linux. */
2 /*
3 Written 1992,1993 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency. This software may be used and
7 distributed according to the terms of the GNU Public License,
8 incorporated herein by reference.
10 This driver should work with the 3c503 and 3c503/16. It should be used
11 in shared memory mode for best performance, although it may also work
12 in programmed-I/O mode.
14 The Author may be reached as becker@super.org or
15 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
18 static char *version =
19 "3c503.c:v0.99.13 8/30/93 Donald Becker (becker@super.org)\n";
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/errno.h>
25 #include <asm/io.h>
26 #include <asm/system.h>
28 #include "dev.h"
30 #include "8390.h"
31 #include "3c503.h"
33 int el2_probe(struct device *dev);
34 int el2_pio_autoprobe(struct device *dev);
35 int el2probe1(int ioaddr, struct device *dev);
37 static int el2_open(struct device *dev);
38 static int el2_close(struct device *dev);
39 static void el2_reset_8390(struct device *dev);
40 static void el2_init_card(struct device *dev);
41 static void el2_block_output(struct device *dev, int count,
42 const unsigned char *buf, const start_page);
43 static int el2_block_input(struct device *dev, int count, char *buf,
44 int ring_offset);
47 /* This routine probes for a memory-mapped 3c503 board by looking for
48 the "location register" at the end of the jumpered boot PROM space.
49 This works even if a PROM isn't there.
51 If the ethercard isn't found there is an optional probe for
52 ethercard jumpered to programmed-I/O mode.
55 static int ports[] = {0x300,0x310,0x330,0x350,0x250,0x280,0x2a0,0x2e0,0};
57 int
58 el2_probe(struct device *dev)
60 int *addr, addrs[] = { 0xddffe, 0xd9ffe, 0xcdffe, 0xc9ffe, 0};
61 short ioaddr = dev->base_addr;
63 if (ioaddr < 0)
64 return ENXIO; /* Don't probe at all. */
65 if (ioaddr > 0)
66 return ! el2probe1(ioaddr, dev);
68 for (addr = addrs; *addr; addr++) {
69 int i;
70 unsigned int base_bits = *(unsigned char *)*addr;
71 /* Find first set bit. */
72 for(i = 7; i >= 0; i--, base_bits >>= 1)
73 if (base_bits & 0x1)
74 break;
75 if (base_bits != 1)
76 continue;
77 #ifdef HAVE_PORTRESERVE
78 if (check_region(ports[i], 16))
79 continue;
80 #endif
81 if (el2probe1(ports[i], dev))
82 return 0;
84 #ifndef no_probe_nonshared_memory
85 return el2_pio_autoprobe(dev);
86 #else
87 return ENODEV;
88 #endif
91 /* Try all of the locations that aren't obviously empty. This touches
92 a lot of locations, and is much riskier than the code above. */
93 int
94 el2_pio_autoprobe(struct device *dev)
96 int i;
97 for (i = 0; i < 8; i++) {
98 #ifdef HAVE_PORTRESERVE
99 if (check_region(ports[i], 16))
100 continue;
101 #endif
102 /* Reset and/or avoid any lurking NE2000 */
103 if (inb_p(ports[i] + 0x408) == 0xff)
104 continue;
105 if (inb(ports[i] + 0x403) == (0x80 >> i) /* Preliminary check */
106 && el2probe1(ports[i], dev))
107 return 0;
109 return ENODEV;
112 /* Probe for the Etherlink II card at I/O port base IOADDR,
113 returning non-zero on sucess. If found, set the station
114 address and memory parameters in DEVICE. */
116 el2probe1(int ioaddr, struct device *dev)
118 int i, iobase_reg, membase_reg, saved_406;
119 unsigned char *station_addr = dev->dev_addr;
121 /* We verify that it's a 3C503 board by checking the first three octets
122 of its ethernet address. */
123 printk("3c503 probe at %#3x:", ioaddr);
124 iobase_reg = inb(ioaddr+0x403);
125 membase_reg = inb(ioaddr+0x404);
126 /* Verify ASIC register that should be 0 or have a single bit set. */
127 if ( (iobase_reg & (iobase_reg - 1))
128 || (membase_reg & (membase_reg - 1))) {
129 printk(" not found.\n");
130 return 0;
132 saved_406 = inb_p(ioaddr + 0x406);
133 outb_p(ECNTRL_RESET|ECNTRL_THIN, ioaddr + 0x406); /* Reset it... */
134 outb_p(ECNTRL_THIN, ioaddr + 0x406);
135 /* Map the station addr PROM into the lower I/O ports. */
136 outb(ECNTRL_SAPROM|ECNTRL_THIN, ioaddr + 0x406);
137 for (i = 0; i < ETHER_ADDR_LEN; i++) {
138 printk(" %2.2X", (station_addr[i] = inb(ioaddr + i)));
140 if ( station_addr[0] != 0x02
141 || station_addr[1] != 0x60
142 || station_addr[2] != 0x8c) {
143 printk(" 3C503 not found.\n");
144 /* Restore the register we frobbed. */
145 outb(saved_406, ioaddr + 0x406);
146 return 0;
149 #ifdef HAVE_PORTRESERVE
150 snarf_region(ioaddr, 16);
151 #endif
152 ethdev_init(dev);
154 /* Map the 8390 back into the window. */
155 outb(ECNTRL_THIN, ioaddr + 0x406);
156 dev->base_addr = ioaddr;
157 /* Probe for, turn on and clear the board's shared memory. */
158 if (ei_debug > 2) printk(" memory jumpers %2.2x ", membase_reg);
159 outb(EGACFR_NORM, ioaddr + 0x405); /* Enable RAM */
161 /* This should be probed for (or set via an ioctl()) at run-time.
162 Right now we use a sleazy hack to pass in the interface number
163 at boot-time via the low bits of the mem_end field. That value is
164 unused, and the low bits would be discarded even if it was used. */
165 #if defined(EI8390_THICK) || defined(EL2_AUI)
166 ei_status.interface_num = 1;
167 #else
168 ei_status.interface_num = dev->mem_end & 0xf;
169 #endif
171 if ((membase_reg & 0xf0) == 0) {
172 dev->mem_start = 0;
173 } else {
174 dev->mem_start = ((membase_reg & 0xc0) ? 0xD8000 : 0xC8000) +
175 ((membase_reg & 0xA0) ? 0x4000 : 0);
177 #define EL2_MEMSIZE (EL2SM_STOP_PG - EL2SM_START_PG)*256
178 #ifdef EL2MEMTEST
179 /* This has never found an error, but someone might care. */
180 { /* Check the card's memory. */
181 int *mem_base = (int *)dev->mem_start;
182 int memtest_value = 0xbbadf00d;
183 mem_base[0] = 0xba5eba5e;
184 for (i = 1; i < EL2_MEMSIZE/sizeof(mem_base[0]); i++) {
185 mem_base[i] = memtest_value;
186 if (mem_base[0] != 0xba5eba5e
187 || mem_base[i] != memtest_value) {
188 printk(" memory failure or memory address conflict.\n");
189 dev->mem_start = 0;
190 break;
192 memtest_value += 0x55555555;
193 mem_base[i] = 0;
196 #endif /* EL2MEMTEST */
197 /* Divide the on-board memory into a single maximum-sized transmit
198 (double-sized for ping-pong transmit) buffer at the base, and
199 use the rest as a receive ring. */
200 dev->mem_end = dev->rmem_end = dev->mem_start + EL2_MEMSIZE;
201 dev->rmem_start = TX_PAGES*256 + dev->mem_start;
203 if (ei_debug > 2)
204 printk("\n3c503: memory params start=%#5x rstart=%#5x end=%#5x rend=%#5x.\n",
205 dev->mem_start, dev->rmem_start, dev->mem_end, dev->rmem_end);
207 /* Finish setting the board's parameters. */
208 ei_status.name = "3C503";
209 ei_status.tx_start_page = EL2SM_START_PG;
210 ei_status.rx_start_page = EL2SM_START_PG + TX_PAGES;
211 ei_status.stop_page = EL2SM_STOP_PG;
212 ei_status.reset_8390 = &el2_reset_8390;
213 ei_status.block_input = &el2_block_input;
214 ei_status.block_output = &el2_block_output;
216 if (dev->irq == 2)
217 dev->irq = 9;
218 else if (dev->irq > 5 && dev->irq != 9) {
219 printk("\n3c503: configured interrupt %d invalid, using autoIRQ.\n",
220 dev->irq);
221 dev->irq = 0;
224 ei_status.saved_irq = dev->irq;
226 dev->start = 0;
227 dev->open = &el2_open;
228 dev->stop = &el2_close;
230 if (dev->mem_start)
231 printk("\n%s: %s with shared memory at %#6x-%#6x,\n",
232 dev->name, ei_status.name, dev->mem_start, dev->mem_end-1);
233 else
234 printk("\n%s: %s using programmed I/O (REJUMPER for SHARED MEMORY).\n",
235 dev->name, ei_status.name);
236 if (ei_debug > 1)
237 printk(version);
239 return ioaddr;
242 static int
243 el2_open(struct device *dev)
246 if (dev->irq < 2) {
247 int irqlist[] = {5, 9, 3, 4, 0};
248 int *irqp = irqlist;
250 outb(EGACFR_NORM, E33G_GACFR); /* Enable RAM and interrupts. */
251 do {
252 if (request_irq (*irqp, NULL) != -EBUSY) {
253 /* Twinkle the interrupt, and check if it's seen. */
254 autoirq_setup(0);
255 outb_p(0x04 << ((*irqp == 9) ? 2 : *irqp), E33G_IDCFR);
256 outb_p(0x00, E33G_IDCFR);
257 if (*irqp == autoirq_report(0) /* It's a good IRQ line! */
258 && request_irq (dev->irq = *irqp, &ei_interrupt) == 0)
259 break;
261 } while (*++irqp);
262 if (*irqp == 0) {
263 outb(EGACFR_IRQOFF, E33G_GACFR); /* disable interrupts. */
264 return -EAGAIN;
266 } else {
267 if (request_irq(dev->irq, &ei_interrupt)) {
268 return -EAGAIN;
271 el2_init_card(dev);
272 return ei_open(dev);
275 static int
276 el2_close(struct device *dev)
278 free_irq(dev->irq);
279 dev->irq = ei_status.saved_irq;
280 irq2dev_map[dev->irq] = NULL;
281 outb(EGACFR_IRQOFF, E33G_GACFR); /* disable interrupts. */
283 NS8390_init(dev, 0);
285 return 0;
288 /* This is called whenever we have a unrecoverable failure:
289 transmit timeout
290 Bad ring buffer packet header
292 static void
293 el2_reset_8390(struct device *dev)
295 if (ei_debug > 1) {
296 printk("%s: Resetting the 3c503 board...", dev->name);
297 printk("%#x=%#02x %#x=%#02x %#x=%#02x...", E33G_IDCFR, inb(E33G_IDCFR),
298 E33G_CNTRL, inb(E33G_CNTRL), E33G_GACFR, inb(E33G_GACFR));
300 outb_p(ECNTRL_RESET|ECNTRL_THIN, E33G_CNTRL);
301 ei_status.txing = 0;
302 outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
303 el2_init_card(dev);
304 if (ei_debug > 1) printk("done\n");
307 /* Initialize the 3c503 GA registers after a reset. */
308 static void
309 el2_init_card(struct device *dev)
311 /* Unmap the station PROM and select the DIX or BNC connector. */
312 outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
314 /* Set ASIC copy of rx's first and last+1 buffer pages */
315 /* These must be the same as in the 8390. */
316 outb(ei_status.rx_start_page, E33G_STARTPG);
317 outb(ei_status.stop_page, E33G_STOPPG);
319 /* Point the vector pointer registers somewhere ?harmless?. */
320 outb(0xff, E33G_VP2); /* Point at the ROM restart location 0xffff0 */
321 outb(0xff, E33G_VP1);
322 outb(0x00, E33G_VP0);
323 /* Turn off all interrupts until we're opened. */
324 outb_p(0x00, dev->base_addr + EN0_IMR);
325 /* Enable IRQs iff started. */
326 outb(EGACFR_NORM, E33G_GACFR);
328 /* Set the interrupt line. */
329 outb_p((0x04 << (dev->irq == 9 ? 2 : dev->irq)), E33G_IDCFR);
330 outb_p(8, E33G_DRQCNT); /* Set burst size to 8 */
331 outb_p(0x20, E33G_DMAAH); /* Put a valid addr in the GA DMA */
332 outb_p(0x00, E33G_DMAAL);
333 return; /* We always succeed */
336 /* Either use the shared memory (if enabled on the board) or put the packet
337 out through the ASIC FIFO. The latter is probably much slower. */
338 static void
339 el2_block_output(struct device *dev, int count,
340 const unsigned char *buf, const start_page)
342 int i; /* Buffer index */
343 int boguscount = 0; /* timeout counter */
345 /* This should really be set with during an open(). */
346 outb(EGACFR_NORM, E33G_GACFR); /* Enable RAM and interrupts. */
348 if (dev->mem_start) { /* Shared memory transfer */
349 void *dest_addr = (void *)(dev->mem_start +
350 ((start_page - ei_status.tx_start_page) << 8));
351 memcpy(dest_addr, buf, count);
352 if (ei_debug > 2 && memcmp(dest_addr, buf, count))
353 printk("%s: 3c503 send_packet() bad memory copy @ %#5x.\n",
354 dev->name, (int) dest_addr);
355 else if (ei_debug > 4)
356 printk("%s: 3c503 send_packet() good memory copy @ %#5x.\n",
357 dev->name, (int) dest_addr);
358 return;
360 /* No shared memory, put the packet out the slow way. */
361 /* Set up then start the internal memory transfer to Tx Start Page */
362 outb(0x00, E33G_DMAAL);
363 outb_p(start_page, E33G_DMAAH);
364 outb_p((ei_status.interface_num ? ECNTRL_AUI : ECNTRL_THIN ) | ECNTRL_OUTPUT
365 | ECNTRL_START, E33G_CNTRL);
367 /* This is the byte copy loop: it should probably be tuned for
368 for speed once everything is working. I think it is possible
369 to output 8 bytes between each check of the status bit. */
370 for(i = 0; i < count; i++) {
371 if (i % 8 == 0)
372 while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
373 if (++boguscount > (i<<3) + 32) {
374 printk("%s: FIFO blocked in el2_block_output (at %d of %d, bc=%d).\n",
375 dev->name, i, count, boguscount);
376 return;
378 outb(buf[i], E33G_FIFOH);
380 outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
381 return;
384 /* Returns the new ring pointer. */
385 static int
386 el2_block_input(struct device *dev, int count, char *buf, int ring_offset)
388 int boguscount = 0;
389 int end_of_ring = dev->rmem_end;
390 unsigned int i;
392 /* Maybe enable shared memory just be to be safe... nahh.*/
393 if (dev->mem_start) { /* Use the shared memory. */
394 ring_offset -= (EL2SM_START_PG<<8);
395 if (dev->mem_start + ring_offset + count > end_of_ring) {
396 /* We must wrap the input move. */
397 int semi_count = end_of_ring - (dev->mem_start + ring_offset);
398 if (ei_debug > 4)
399 printk("%s: 3c503 block_input() @ %#5x+%x=%5x.\n",
400 dev->name, dev->mem_start, ring_offset,
401 dev->mem_start + ring_offset);
402 memcpy(buf, (char *)dev->mem_start + ring_offset, semi_count);
403 count -= semi_count;
404 memcpy(buf + semi_count, (char *)dev->rmem_start, count);
405 return dev->rmem_start + count;
407 if (ei_debug > 4)
408 printk("%s: 3c503 block_input() @ %#5x+%x=%5x.\n",
409 dev->name, dev->mem_start, ring_offset,
410 dev->mem_start + ring_offset);
411 memcpy(buf, (char *)dev->mem_start + ring_offset, count);
412 return ring_offset + count;
414 /* No shared memory, use programmed I/O. */
415 outb(ring_offset & 0xff, E33G_DMAAL);
416 outb_p((ring_offset >> 8) & 0xff, E33G_DMAAH);
417 outb_p((ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI) | ECNTRL_INPUT
418 | ECNTRL_START, E33G_CNTRL);
420 /* This is the byte copy loop: it should probably be tuned for
421 for speed once everything is working. */
422 for(i = 0; i < count; i++) {
423 if (i % 8 == 0)
424 while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
425 if (++boguscount > (i<<3) + 32) {
426 printk("%s: FIFO blocked in el2_block_input() (at %d of %d, bc=%d).\n",
427 dev->name, i, count, boguscount);
428 boguscount = 0;
429 break;
431 buf[i] = inb_p(E33G_FIFOH);
433 outb_p(ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
434 return 0;
438 * Local variables:
439 * version-control: t
440 * kept-new-versions: 5
441 * End: