* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / net / ne.c
blobe198d55755e79e79f6f18f8d7600606863148e4f
1 /* ne.c: A general non-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 many 8390-based ethernet boards. Currently
11 it support the NE1000, NE2000, clones, and some Cabletron products.
13 The Author may be reached as becker@super.org or
14 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
17 /* Routines for the NatSemi-based designs (NE[12]000). */
19 static char *version =
20 "ne.c:v0.99-14a 12/3/93 Donald Becker (becker@super.org)\n";
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <asm/system.h>
27 #include <asm/io.h>
29 #include "dev.h"
30 #include "8390.h"
32 #define NE_BASE (dev->base_addr)
33 #define NE_CMD 0x00
34 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
35 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
37 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
38 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
39 #define NESM_START_PG 0x40 /* First page of TX buffer */
40 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
42 int ne_probe(struct device *dev);
43 static int neprobe1(int ioaddr, struct device *dev, int verbose);
45 static void ne_reset_8390(struct device *dev);
46 static int ne_block_input(struct device *dev, int count,
47 char *buf, int ring_offset);
48 static void ne_block_output(struct device *dev, const int count,
49 const unsigned char *buf, const int start_page);
52 /* Probe for various non-shared-memory ethercards.
54 NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
55 buffer memory space. NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
56 the SAPROM, while other supposed NE2000 clones must be detected by their
57 SA prefix.
59 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
60 mode results in doubled values, which can be detected and compansated for.
62 The probe is also responsible for initializing the card and filling
63 in the 'dev' and 'ei_status' structures.
65 We use the minimum memory size for some ethercard product lines, iff we can't
66 distinguish models. You can increase the packet buffer size by setting
67 PACKETBUF_MEMSIZE. Reported Cabletron packet buffer locations are:
68 E1010 starts at 0x100 and ends at 0x2000.
69 E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
70 E2010 starts at 0x100 and ends at 0x4000.
71 E2010-x starts at 0x100 and ends at 0xffff. */
73 int ne_probe(struct device *dev)
75 int *port, ports[] = {0x300, 0x280, 0x320, 0x340, 0x360, 0};
76 short ioaddr = dev->base_addr;
78 if (ioaddr < 0)
79 return ENXIO; /* Don't probe at all. */
80 if (ioaddr > 0x100)
81 return ! neprobe1(ioaddr, dev, 1);
83 for (port = &ports[0]; *port; port++) {
84 #ifdef HAVE_PORTRESERVE
85 if (check_region(*port, 32))
86 continue;
87 #endif
88 if (inb_p(*port) != 0xff && neprobe1(*port, dev, 0)) {
89 dev->base_addr = *port;
90 return 0;
93 dev->base_addr = ioaddr;
94 return ENODEV;
97 static int neprobe1(int ioaddr, struct device *dev, int verbose)
99 int i;
100 unsigned char SA_prom[32];
101 int wordlength = 2;
102 char *name;
103 int start_page, stop_page;
104 int neX000, ctron, dlink, dfi;
105 int reg0 = inb(ioaddr);
107 if ( reg0 == 0xFF)
108 return 0;
110 /* Do a quick preliminary check that we have a 8390. */
111 { int regd;
112 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
113 regd = inb_p(ioaddr + 0x0d);
114 outb_p(0xff, ioaddr + 0x0d);
115 outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
116 inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
117 if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
118 outb_p(reg0, ioaddr);
119 outb(regd, ioaddr + 0x0d); /* Restore the old values. */
120 return 0;
124 printk("NE*000 ethercard probe at %#3x:", ioaddr);
126 /* Read the 16 bytes of station address prom, returning 1 for
127 an eight-bit interface and 2 for a 16-bit interface.
128 We must first initialize registers, similar to NS8390_init(eifdev, 0).
129 We can't reliably read the SAPROM address without this.
130 (I learned the hard way!). */
132 struct {unsigned char value, offset; } program_seq[] = {
133 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
134 {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
135 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
136 {0x00, EN0_RCNTHI},
137 {0x00, EN0_IMR}, /* Mask completion irq. */
138 {0xFF, EN0_ISR},
139 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
140 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
141 {32, EN0_RCNTLO},
142 {0x00, EN0_RCNTHI},
143 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
144 {0x00, EN0_RSARHI},
145 {E8390_RREAD+E8390_START, E8390_CMD},
147 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
148 outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
150 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
151 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
152 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
153 if (SA_prom[i] != SA_prom[i+1])
154 wordlength = 1;
157 if (wordlength == 2) {
158 /* We must set the 8390 for word mode, AND RESET IT. */
159 int tmp;
160 outb_p(0x49, ioaddr + EN0_DCFG);
161 tmp = inb_p(NE_BASE + NE_RESET);
162 outb(tmp, NE_BASE + NE_RESET);
163 /* Un-double the SA_prom values. */
164 for (i = 0; i < 16; i++)
165 SA_prom[i] = SA_prom[i+i];
168 #if defined(show_all_SAPROM)
169 /* If your ethercard isn't detected define this to see the SA_PROM. */
170 for(i = 0; i < sizeof(SA_prom); i++)
171 printk(" %2.2x", SA_prom[i]);
172 #else
173 for(i = 0; i < ETHER_ADDR_LEN; i++) {
174 dev->dev_addr[i] = SA_prom[i];
175 printk(" %2.2x", SA_prom[i]);
177 #endif
179 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
180 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
181 dlink = (SA_prom[0] == 0x00 && SA_prom[1] == 0xDE && SA_prom[2] == 0x01);
182 dfi = (SA_prom[0] == 'D' && SA_prom[1] == 'F' && SA_prom[2] == 'I');
184 /* Set up the rest of the parameters. */
185 if (neX000 || dlink || dfi) {
186 if (wordlength == 2) {
187 name = dlink ? "DE200" : "NE2000";
188 start_page = NESM_START_PG;
189 stop_page = NESM_STOP_PG;
190 } else {
191 name = dlink ? "DE100" : "NE1000";
192 start_page = NE1SM_START_PG;
193 stop_page = NE1SM_STOP_PG;
195 } else if (ctron) {
196 name = "Cabletron";
197 start_page = 0x01;
198 stop_page = (wordlength == 2) ? 0x40 : 0x20;
199 } else {
200 printk(" not found.\n");
201 return 0;
204 if (dev->irq < 2) {
205 autoirq_setup(0);
206 outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
207 outb_p(0x00, ioaddr + EN0_RCNTLO);
208 outb_p(0x00, ioaddr + EN0_RCNTHI);
209 outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
210 outb_p(0x00, ioaddr + EN0_IMR); /* Mask it again. */
211 dev->irq = autoirq_report(0);
212 if (ei_debug > 2)
213 printk(" autoirq is %d", dev->irq);
214 } else if (dev->irq == 2)
215 /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
216 or don't know which one to set. */
217 dev->irq = 9;
219 /* Snarf the interrupt now. There's no point in waiting since we cannot
220 share and the board will usually be enabled. */
222 int irqval = irqaction (dev->irq, &ei_sigaction);
223 if (irqval) {
224 printk (" unable to get IRQ %d (irqval=%d).\n", dev->irq, irqval);
225 return 0;
229 dev->base_addr = ioaddr;
231 #ifdef HAVE_PORTRESERVE
232 snarf_region(ioaddr, 32);
233 #endif
235 ethdev_init(dev);
236 printk("\n%s: %s found at %#x, using IRQ %d.\n",
237 dev->name, name, ioaddr, dev->irq);
239 if (ei_debug > 0)
240 printk(version);
242 ei_status.name = name;
243 ei_status.tx_start_page = start_page;
244 ei_status.stop_page = stop_page;
245 ei_status.word16 = (wordlength == 2);
247 ei_status.rx_start_page = start_page + TX_PAGES;
248 #ifdef PACKETBUF_MEMSIZE
249 /* Allow the packet buffer size to be overridden by know-it-alls. */
250 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
251 #endif
253 ei_status.reset_8390 = &ne_reset_8390;
254 ei_status.block_input = &ne_block_input;
255 ei_status.block_output = &ne_block_output;
256 NS8390_init(dev, 0);
257 return dev->base_addr;
260 /* Hard reset the card. This used to pause for the same period that a
261 8390 reset command required, but that shouldn't be necessary. */
262 static void
263 ne_reset_8390(struct device *dev)
265 int tmp = inb_p(NE_BASE + NE_RESET);
266 int reset_start_time = jiffies;
268 if (ei_debug > 1) printk("resetting the 8390 t=%d...", jiffies);
269 ei_status.txing = 0;
271 outb_p(tmp, NE_BASE + NE_RESET);
272 /* This check _should_not_ be necessary, omit eventually. */
273 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
274 if (jiffies - reset_start_time > 2) {
275 printk("%s: ne_reset_8390() did not complete.\n", dev->name);
276 break;
280 /* Block input and output, similar to the Crynwr packet driver. If you
281 porting to a new ethercard look at the packet driver source for hints.
282 The NEx000 doesn't share it on-board packet memory -- you have to put
283 the packet out through the "remote DMA" dataport using outb. */
285 static int
286 ne_block_input(struct device *dev, int count, char *buf, int ring_offset)
288 int xfer_count = count;
289 int nic_base = dev->base_addr;
291 if (ei_status.dmaing) {
292 if (ei_debug > 0)
293 printk("%s: DMAing conflict in ne_block_input."
294 "[DMAstat:%1x][irqlock:%1x]\n",
295 dev->name, ei_status.dmaing, ei_status.irqlock);
296 return 0;
298 ei_status.dmaing |= 0x01;
299 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
300 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
301 outb_p(count >> 8, nic_base + EN0_RCNTHI);
302 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
303 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
304 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
305 if (ei_status.word16) {
306 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
307 if (count & 0x01)
308 buf[count-1] = inb(NE_BASE + NE_DATAPORT), xfer_count++;
309 } else {
310 insb(NE_BASE + NE_DATAPORT, buf, count);
313 /* This was for the ALPHA version only, but enough people have
314 encountering problems that it is still here. If you see
315 this message you either 1) have an slightly imcompatible clone
316 or 2) have noise/speed problems with your bus. */
317 if (ei_debug > 1) { /* DMA termination address check... */
318 int addr, tries = 20;
319 do {
320 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
321 -- it's broken! Check the "DMA" address instead. */
322 int high = inb_p(nic_base + EN0_RSARHI);
323 int low = inb_p(nic_base + EN0_RSARLO);
324 addr = (high << 8) + low;
325 if (((ring_offset + xfer_count) & 0xff) == low)
326 break;
327 } while (--tries > 0);
328 if (tries <= 0)
329 printk("%s: RX transfer address mismatch,"
330 "%#4.4x (expected) vs. %#4.4x (actual).\n",
331 dev->name, ring_offset + xfer_count, addr);
333 ei_status.dmaing &= ~0x01;
334 return ring_offset + count;
337 static void
338 ne_block_output(struct device *dev, int count,
339 const unsigned char *buf, const int start_page)
341 int retries = 0;
342 int nic_base = NE_BASE;
344 /* Round the count up for word writes. Do we need to do this?
345 What effect will an odd byte count have on the 8390?
346 I should check someday. */
347 if (ei_status.word16 && (count & 0x01))
348 count++;
349 if (ei_status.dmaing) {
350 if (ei_debug > 0)
351 printk("%s: DMAing conflict in ne_block_output."
352 "[DMAstat:%1x][irqlock:%1x]\n",
353 dev->name, ei_status.dmaing, ei_status.irqlock);
354 return;
356 ei_status.dmaing |= 0x02;
357 /* We should already be in page 0, but to be safe... */
358 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
360 retry:
361 #if defined(rw_bugfix)
362 /* Handle the read-before-write bug the same way as the
363 Crynwr packet driver -- the NatSemi method doesn't work.
364 Actually this doesn't aways work either, but if you have
365 problems with your NEx000 this is better than nothing! */
366 outb_p(0x42, nic_base + EN0_RCNTLO);
367 outb_p(0x00, nic_base + EN0_RCNTHI);
368 outb_p(0x42, nic_base + EN0_RSARLO);
369 outb_p(0x00, nic_base + EN0_RSARHI);
370 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
371 /* Make certain that the dummy read has occured. */
372 SLOW_DOWN_IO;
373 SLOW_DOWN_IO;
374 SLOW_DOWN_IO;
375 #endif /* rw_bugfix */
377 /* Now the normal output. */
378 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
379 outb_p(count >> 8, nic_base + EN0_RCNTHI);
380 outb_p(0x00, nic_base + EN0_RSARLO);
381 outb_p(start_page, nic_base + EN0_RSARHI);
383 outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
384 if (ei_status.word16) {
385 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
386 } else {
387 outsb(NE_BASE + NE_DATAPORT, buf, count);
390 /* This was for the ALPHA version only, but enough people have
391 encountering problems that it is still here. */
392 if (ei_debug > 1) { /* DMA termination address check... */
393 int addr, tries = 20;
394 do {
395 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
396 -- it's broken! Check the "DMA" address instead. */
397 int high = inb_p(nic_base + EN0_RSARHI);
398 int low = inb_p(nic_base + EN0_RSARLO);
399 addr = (high << 8) + low;
400 if ((start_page << 8) + count == addr)
401 break;
402 } while (--tries > 0);
403 if (tries <= 0) {
404 printk("%s: Tx packet transfer address mismatch,"
405 "%#4.4x (expected) vs. %#4.4x (actual).\n",
406 dev->name, (start_page << 8) + count, addr);
407 if (retries++ == 0)
408 goto retry;
411 ei_status.dmaing &= ~0x02;
412 return;
417 * Local variables:
418 * compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne.c"
419 * version-control: t
420 * kept-new-versions: 5
421 * End: