MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / net / ne.c
blobd5ae85d7e673a27546720a1807a1396783c6d71a
1 /* ne.c: A general non-shared-memory NS8390 ethernet driver for linux. */
2 /*
3 Written 1992-94 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
11 The author may be reached as becker@scyld.com, or C/O
12 Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
14 This driver should work with many programmed-I/O 8390-based ethernet
15 boards. Currently it supports the NE1000, NE2000, many clones,
16 and some Cabletron products.
18 Changelog:
20 Paul Gortmaker : use ENISR_RDC to monitor Tx PIO uploads, made
21 sanity checks and bad clone support optional.
22 Paul Gortmaker : new reset code, reset card after probe at boot.
23 Paul Gortmaker : multiple card support for module users.
24 Paul Gortmaker : Support for PCI ne2k clones, similar to lance.c
25 Paul Gortmaker : Allow users with bad cards to avoid full probe.
26 Paul Gortmaker : PCI probe changes, more PCI cards supported.
27 rjohnson@analogic.com : Changed init order so an interrupt will only
28 occur after memory is allocated for dev->priv. Deallocated memory
29 last in cleanup_modue()
30 Richard Guenther : Added support for ISAPnP cards
31 Paul Gortmaker : Discontinued PCI support - use ne2k-pci.c instead.
32 Hayato Fujiwara : Add m32r support.
33 Greg Ungerer : added some coldfire addressing code.
37 /* Routines for the NatSemi-based designs (NE[12]000). */
39 static const char version1[] =
40 "ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)\n";
41 static const char version2[] =
42 "Last modified Nov 1, 2000 by Paul Gortmaker\n";
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/isapnp.h>
49 #include <linux/init.h>
50 #include <linux/interrupt.h>
51 #include <linux/delay.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/jiffies.h>
56 #include <asm/system.h>
57 #include <asm/io.h>
59 #if defined(CONFIG_TOSHIBA_RBTX4927) || defined(CONFIG_TOSHIBA_RBTX4938)
60 #include <asm/tx4938/rbtx4938.h>
61 #endif
63 #include "8390.h"
65 #ifdef CONFIG_COLDFIRE
66 #define COLDFIRE_NE2000_FUNCS
67 #include <asm/coldfire.h>
68 #include <asm/mcfsim.h>
69 #include <asm/mcfne.h>
70 unsigned char ne_defethaddr[] = { 0x00, 0xd0, 0xcf, 0x00, 0x00, 0x01 };
71 #endif /* CONFIG_COLDFIRE */
72 #if defined(CONFIG_M5307) && defined(CONFIG_NETtel)
73 static unsigned int ne_portlist[] = { NE2000_ADDR0, NE2000_ADDR1, 0 };
74 static unsigned int ne_irqlist[] = { NE2000_IRQ_VECTOR0,NE2000_IRQ_VECTOR1,0 };
75 #endif
77 #define DRV_NAME "ne"
78 #if defined(CONFIG_CPU_H8300H)
79 #include <asm/h8300_ne.h>
80 #endif
82 /* Some defines that people can play with if so inclined. */
84 /* Do we support clones that don't adhere to 14,15 of the SAprom ? */
85 #define SUPPORT_NE_BAD_CLONES
87 /* Do we perform extra sanity checks on stuff ? */
88 /* #define NE_SANITY_CHECK */
90 /* Do we implement the read before write bugfix ? */
91 /* #define NE_RW_BUGFIX */
93 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
94 /* #define PACKETBUF_MEMSIZE 0x40 */
96 /* A zero-terminated list of I/O addresses to be probed at boot. */
97 #ifndef MODULE
98 static unsigned int netcard_portlist[] __initdata = {
99 0x300, 0x280, 0x320, 0x340, 0x360, 0x380, 0
101 #endif
103 static struct isapnp_device_id isapnp_clone_list[] __initdata = {
104 { ISAPNP_CARD_ID('A','X','E',0x2011),
105 ISAPNP_VENDOR('A','X','E'), ISAPNP_FUNCTION(0x2011),
106 (long) "NetGear EA201" },
107 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
108 ISAPNP_VENDOR('E','D','I'), ISAPNP_FUNCTION(0x0216),
109 (long) "NN NE2000" },
110 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
111 ISAPNP_VENDOR('P','N','P'), ISAPNP_FUNCTION(0x80d6),
112 (long) "Generic PNP" },
113 { } /* terminate list */
116 MODULE_DEVICE_TABLE(isapnp, isapnp_clone_list);
118 #ifdef SUPPORT_NE_BAD_CLONES
119 /* A list of bad clones that we none-the-less recognize. */
120 static struct { const char *name8, *name16; unsigned char SAprefix[4];}
121 bad_clone_list[] __initdata = {
122 {"DE100", "DE200", {0x00, 0xDE, 0x01,}},
123 {"DE120", "DE220", {0x00, 0x80, 0xc8,}},
124 {"DFI1000", "DFI2000", {'D', 'F', 'I',}}, /* Original, eh? */
125 {"EtherNext UTP8", "EtherNext UTP16", {0x00, 0x00, 0x79}},
126 {"NE1000","NE2000-invalid", {0x00, 0x00, 0xd8}}, /* Ancient real NE1000. */
127 {"NN1000", "NN2000", {0x08, 0x03, 0x08}}, /* Outlaw no-name clone. */
128 {"4-DIM8","4-DIM16", {0x00,0x00,0x4d,}}, /* Outlaw 4-Dimension cards. */
129 {"Con-Intl_8", "Con-Intl_16", {0x00, 0x00, 0x24}}, /* Connect Int'nl */
130 {"ET-100","ET-200", {0x00, 0x45, 0x54}}, /* YANG and YA clone */
131 {"COMPEX","COMPEX16",{0x00,0x80,0x48}}, /* Broken ISA Compex cards */
132 {"E-LAN100", "E-LAN200", {0x00, 0x00, 0x5d}}, /* Broken ne1000 clones */
133 {"PCM-4823", "PCM-4823", {0x00, 0xc0, 0x6c}}, /* Broken Advantech MoBo */
134 {"REALTEK", "RTL8019", {0x00, 0x00, 0xe8}}, /* no-name with Realtek chip */
135 #if defined(CONFIG_TOSHIBA_RBTX4927) || defined(CONFIG_TOSHIBA_RBTX4938)
136 {"RBHMA4X00-RTL8019", "RBHMA4X00/RTL8019", {0x00, 0x60, 0x0a}}, /* Toshiba built-in */
137 #endif
138 {"LCS-8834", "LCS-8836", {0x04, 0x04, 0x37}}, /* ShinyNet (SET) */
139 {NULL,}
141 #endif
143 /* ---- No user-serviceable parts below ---- */
145 #define NE_BASE (dev->base_addr)
146 #define NE_CMD 0x00
147 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
148 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
149 #define NE_IO_EXTENT 0x20
151 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
152 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
153 #define NESM_START_PG 0x40 /* First page of TX buffer */
154 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
156 #if defined(CONFIG_PLAT_MAPPI)
157 # define DCR_VAL 0x4b
158 #elif defined(CONFIG_PLAT_OAKS32R) || \
159 defined(CONFIG_TOSHIBA_RBTX4927) || defined(CONFIG_TOSHIBA_RBTX4938)
160 # define DCR_VAL 0x48 /* 8-bit mode */
161 #else
162 # define DCR_VAL 0x49
163 #endif
165 static int ne_probe1(struct net_device *dev, int ioaddr);
166 static int ne_probe_isapnp(struct net_device *dev);
168 static int ne_open(struct net_device *dev);
169 static int ne_close(struct net_device *dev);
171 static void ne_reset_8390(struct net_device *dev);
172 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
173 int ring_page);
174 static void ne_block_input(struct net_device *dev, int count,
175 struct sk_buff *skb, int ring_offset);
176 static void ne_block_output(struct net_device *dev, const int count,
177 const unsigned char *buf, const int start_page);
180 /* Probe for various non-shared-memory ethercards.
182 NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
183 buffer memory space. NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
184 the SAPROM, while other supposed NE2000 clones must be detected by their
185 SA prefix.
187 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
188 mode results in doubled values, which can be detected and compensated for.
190 The probe is also responsible for initializing the card and filling
191 in the 'dev' and 'ei_status' structures.
193 We use the minimum memory size for some ethercard product lines, iff we can't
194 distinguish models. You can increase the packet buffer size by setting
195 PACKETBUF_MEMSIZE. Reported Cabletron packet buffer locations are:
196 E1010 starts at 0x100 and ends at 0x2000.
197 E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
198 E2010 starts at 0x100 and ends at 0x4000.
199 E2010-x starts at 0x100 and ends at 0xffff. */
201 static int __init do_ne_probe(struct net_device *dev)
203 unsigned int base_addr = dev->base_addr;
204 #ifndef MODULE
205 int orig_irq = dev->irq;
206 #endif
208 #if defined (CONFIG_NETtel) && defined (CONFIG_M5307)
209 static int index = 0;
210 if (!ne_portlist[index])
211 return -ENXIO;
212 dev->base_addr = base_addr = ne_portlist[index];
213 dev->irq = ne_irqlist[index++];
214 #elif defined(CONFIG_COLDFIRE)
215 static int once = 0;
217 if (once)
218 return -ENXIO;
219 if (base_addr == 0) {
220 dev->base_addr = base_addr = NE2000_ADDR;
221 dev->irq = NE2000_IRQ_VECTOR;
222 once++;
224 #elif defined(CONFIG_CPU_H8300H)
225 static int once = 0;
226 if (once)
227 return -ENXIO;
228 dev->base_addr = base_addr = NE2000_ADDR;
229 dev->irq = NE2000_IRQ_VECTOR;
230 once++;
231 #endif
233 SET_MODULE_OWNER(dev);
235 /* First check any supplied i/o locations. User knows best. <cough> */
236 if (base_addr > 0x1ff) /* Check a single specified location. */
237 return ne_probe1(dev, base_addr);
238 else if (base_addr != 0) /* Don't probe at all. */
239 return -ENXIO;
241 /* Then look for any installed ISAPnP clones */
242 if (isapnp_present() && (ne_probe_isapnp(dev) == 0))
243 return 0;
245 #ifndef MODULE
246 /* Last resort. The semi-risky ISA auto-probe. */
247 for (base_addr = 0; netcard_portlist[base_addr] != 0; base_addr++) {
248 int ioaddr = netcard_portlist[base_addr];
249 dev->irq = orig_irq;
250 if (ne_probe1(dev, ioaddr) == 0)
251 return 0;
253 #endif
255 return -ENODEV;
258 #ifndef MODULE
259 struct net_device * __init ne_probe(int unit)
261 struct net_device *dev = alloc_ei_netdev();
262 int err;
264 if (!dev)
265 return ERR_PTR(-ENOMEM);
266 sprintf(dev->name, "eth%d", unit);
267 netdev_boot_setup_check(dev);
269 #ifdef CONFIG_TOSHIBA_RBTX4938
270 dev->base_addr = RBTX4938_RTL_8019_BASE;
271 dev->irq = RBTX4938_RTL_8019_IRQ;
272 #endif
273 err = do_ne_probe(dev);
274 if (err)
275 goto out;
276 return dev;
277 out:
278 free_netdev(dev);
279 return ERR_PTR(err);
281 #endif
283 static int __init ne_probe_isapnp(struct net_device *dev)
285 int i;
287 for (i = 0; isapnp_clone_list[i].vendor != 0; i++) {
288 struct pnp_dev *idev = NULL;
290 while ((idev = pnp_find_dev(NULL,
291 isapnp_clone_list[i].vendor,
292 isapnp_clone_list[i].function,
293 idev))) {
294 /* Avoid already found cards from previous calls */
295 if (pnp_device_attach(idev) < 0)
296 continue;
297 if (pnp_activate_dev(idev) < 0) {
298 pnp_device_detach(idev);
299 continue;
301 /* if no io and irq, search for next */
302 if (!pnp_port_valid(idev, 0) || !pnp_irq_valid(idev, 0)) {
303 pnp_device_detach(idev);
304 continue;
306 /* found it */
307 dev->base_addr = pnp_port_start(idev, 0);
308 dev->irq = pnp_irq(idev, 0);
309 printk(KERN_INFO "ne.c: ISAPnP reports %s at i/o %#lx, irq %d.\n",
310 (char *) isapnp_clone_list[i].driver_data,
311 dev->base_addr, dev->irq);
312 if (ne_probe1(dev, dev->base_addr) != 0) { /* Shouldn't happen. */
313 printk(KERN_ERR "ne.c: Probe of ISAPnP card at %#lx failed.\n", dev->base_addr);
314 pnp_device_detach(idev);
315 return -ENXIO;
317 ei_status.priv = (unsigned long)idev;
318 break;
320 if (!idev)
321 continue;
322 return 0;
325 return -ENODEV;
328 static int __init ne_probe1(struct net_device *dev, int ioaddr)
330 int i;
331 unsigned char SA_prom[32];
332 int wordlength = 2;
333 const char *name = NULL;
334 int start_page, stop_page;
335 int neX000, ctron, copam, bad_card;
336 int reg0, ret;
337 static unsigned version_printed;
339 #if !defined(CONFIG_COLDFIRE) && !defined(CONFIG_CPU_H8300H)
340 if (!request_region(ioaddr, NE_IO_EXTENT, dev->name))
341 return -EBUSY;
342 #endif
343 reg0 = inb_p(ioaddr);
344 if (reg0 == 0xFF) {
345 ret = -ENODEV;
346 goto err_out;
349 /* Do a preliminary verification that we have a 8390. */
351 int regd;
352 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
353 regd = inb_p(ioaddr + 0x0d);
354 outb_p(0xff, ioaddr + 0x0d);
355 outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
356 inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
357 if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
358 outb_p(reg0, ioaddr);
359 outb_p(regd, ioaddr + 0x0d); /* Restore the old values. */
360 ret = -ENODEV;
361 goto err_out;
365 if (ei_debug && version_printed++ == 0)
366 printk(KERN_INFO "%s" KERN_INFO "%s", version1, version2);
368 printk(KERN_INFO "NE*000 ethercard probe at %#3x:", ioaddr);
370 /* A user with a poor card that fails to ack the reset, or that
371 does not have a valid 0x57,0x57 signature can still use this
372 without having to recompile. Specifying an i/o address along
373 with an otherwise unused dev->mem_end value of "0xBAD" will
374 cause the driver to skip these parts of the probe. */
376 bad_card = ((dev->base_addr != 0) && (dev->mem_end == 0xbad));
378 /* Reset card. Who knows what dain-bramaged state it was left in. */
381 unsigned long reset_start_time = jiffies;
383 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
384 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
386 while ((inb_p(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
387 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
388 if (bad_card) {
389 printk(" (warning: no reset ack)");
390 break;
391 } else {
392 printk(" not found (no reset ack).\n");
393 ret = -ENODEV;
394 goto err_out;
398 outb_p(0xff, ioaddr + EN0_ISR); /* Ack all intr. */
401 /* Read the 16 bytes of station address PROM.
402 We must first initialize registers, similar to NS8390_init(eifdev, 0).
403 We can't reliably read the SAPROM address without this.
404 (I learned the hard way!). */
406 struct {unsigned char value, offset; } program_seq[] =
408 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
409 {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
410 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
411 {0x00, EN0_RCNTHI},
412 {0x00, EN0_IMR}, /* Mask completion irq. */
413 {0xFF, EN0_ISR},
414 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
415 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
416 {32, EN0_RCNTLO},
417 {0x00, EN0_RCNTHI},
418 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
419 {0x00, EN0_RSARHI},
420 {E8390_RREAD+E8390_START, E8390_CMD},
423 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
424 outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
427 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
428 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
429 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
430 if (SA_prom[i] != SA_prom[i+1])
431 wordlength = 1;
434 #if defined(CONFIG_M5307) || defined(CONFIG_M5407)
436 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
437 for(i = 0; i < 6; i++)
439 SA_prom[i] = inb(ioaddr + i + 1);
441 SA_prom[14] = SA_prom[15] = 0x57;
443 #endif /* CONFIG_M5307 || CONFIG_M5407 */
444 #if defined(CONFIG_NETtel) || defined(CONFIG_SECUREEDGEMP3)
446 unsigned char *ep;
447 static int nr = 0;
448 ep = (unsigned char *) (0xf0006000 + (nr++ * 6));
450 * MAC address should be in FLASH, check that it is valid.
451 * If good use it, otherwise use the default.
453 if (((ep[0] == 0xff) && (ep[1] == 0xff) && (ep[2] == 0xff) &&
454 (ep[3] == 0xff) && (ep[4] == 0xff) && (ep[5] == 0xff)) ||
455 ((ep[0] == 0) && (ep[1] == 0) && (ep[2] == 0) &&
456 (ep[3] == 0) && (ep[4] == 0) && (ep[5] == 0))) {
457 ep = (unsigned char *) &ne_defethaddr[0];
458 ne_defethaddr[5]++;
461 for(i = 0; i < 6; i++)
462 SA_prom[i] = ep[i];
463 SA_prom[14] = SA_prom[15] = 0x57;
465 #if defined(CONFIG_M5206e) && defined(CONFIG_NETtel)
466 wordlength = 1;
468 /* We must set the 8390 for 8bit mode. */
469 outb_p(0x48, ioaddr + EN0_DCFG);
470 #endif
471 start_page = NESM_START_PG;
472 stop_page = NESM_STOP_PG;
474 #elif defined(CONFIG_CFV240)
476 unsigned char *ep = (unsigned char *) 0xffc0406b;
478 * MAC address should be in FLASH, check that it is valid.
479 * If good use it, otherwise use the default.
481 if (((ep[0] == 0xff) && (ep[1] == 0xff) && (ep[2] == 0xff) &&
482 (ep[3] == 0xff) && (ep[4] == 0xff) && (ep[5] == 0xff)) ||
483 ((ep[0] == 0) && (ep[1] == 0) && (ep[2] == 0) &&
484 (ep[3] == 0) && (ep[4] == 0) && (ep[5] == 0))) {
485 ep = (unsigned char *) &ne_defethaddr[0];
486 ne_defethaddr[5]++;
488 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
489 for(i = 0; i < 6; i++)
490 SA_prom[i] = ep[i];
491 SA_prom[14] = SA_prom[15] = 0x57;
493 #elif defined(CONFIG_M5206e)
495 outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
496 for(i = 0; i < 6; i++)
498 SA_prom[i] = inb(ioaddr + i + 1);
500 SA_prom[14] = SA_prom[15] = 0x57;
501 if(!SA_prom[0] && !SA_prom[1] && !SA_prom[2]) {
502 /* It doesn't seem to be a correct start of a mac-address... */
503 printk("(change MAC=[%02X %02X %02X]->[%02X %02X %02X])",
504 SA_prom[0], SA_prom[1], SA_prom[2],
505 ne_defethaddr[0],ne_defethaddr[1],ne_defethaddr[2]);
506 SA_prom[0] = ne_defethaddr[0];
507 SA_prom[1] = ne_defethaddr[1];
508 SA_prom[2] = ne_defethaddr[2];
511 #endif /* CONFIG_M5206e */
513 #if !(defined(CONFIG_M5206e) && defined(CONFIG_NETtel))
514 if (wordlength == 2)
516 #ifndef CONFIG_COLDFIRE
517 for (i = 0; i < 16; i++)
518 SA_prom[i] = SA_prom[i+i];
519 #endif
520 /* We must set the 8390 for word mode. */
521 outb_p(DCR_VAL, ioaddr + EN0_DCFG);
522 start_page = NESM_START_PG;
525 * Realtek RTL8019AS datasheet says that the PSTOP register
526 * shouldn't exceed 0x60 in 8-bit mode.
527 * This chip can be identified by reading the signature from
528 * the remote byte count registers (otherwise write-only)...
530 if ((DCR_VAL & 0x01) == 0 && /* 8-bit mode */
531 inb(ioaddr + EN0_RCNTLO) == 0x50 &&
532 inb(ioaddr + EN0_RCNTHI) == 0x70)
533 stop_page = 0x60;
534 else
535 stop_page = NESM_STOP_PG;
536 } else {
537 start_page = NE1SM_START_PG;
538 stop_page = NE1SM_STOP_PG;
540 #endif
541 #if defined(CONFIG_CPU_H8300H)
542 start_page = NESM_START_PG;
543 stop_page = NESM_STOP_PG;
544 H8300_INIT_NE();
545 #endif
547 #if defined(CONFIG_PLAT_MAPPI) || defined(CONFIG_PLAT_OAKS32R)
548 neX000 = ((SA_prom[14] == 0x57 && SA_prom[15] == 0x57)
549 || (SA_prom[14] == 0x42 && SA_prom[15] == 0x42));
550 #else
551 neX000 = (SA_prom[14] == 0x57 && SA_prom[15] == 0x57);
552 #endif
553 ctron = (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
554 copam = (SA_prom[14] == 0x49 && SA_prom[15] == 0x00);
556 /* Set up the rest of the parameters. */
557 if (neX000 || bad_card || copam) {
558 name = (wordlength == 2) ? "NE2000" : "NE1000";
560 else if (ctron)
562 name = (wordlength == 2) ? "Ctron-8" : "Ctron-16";
563 start_page = 0x01;
564 stop_page = (wordlength == 2) ? 0x40 : 0x20;
566 else
568 #ifdef SUPPORT_NE_BAD_CLONES
569 /* Ack! Well, there might be a *bad* NE*000 clone there.
570 Check for total bogus addresses. */
571 for (i = 0; bad_clone_list[i].name8; i++)
573 if (SA_prom[0] == bad_clone_list[i].SAprefix[0] &&
574 SA_prom[1] == bad_clone_list[i].SAprefix[1] &&
575 SA_prom[2] == bad_clone_list[i].SAprefix[2])
577 if (wordlength == 2)
579 name = bad_clone_list[i].name16;
580 } else {
581 name = bad_clone_list[i].name8;
583 break;
586 if (bad_clone_list[i].name8 == NULL)
588 printk(" not found (invalid signature %2.2x %2.2x).\n",
589 SA_prom[14], SA_prom[15]);
590 ret = -ENXIO;
591 goto err_out;
593 #else
594 printk(" not found.\n");
595 ret = -ENXIO;
596 goto err_out;
597 #endif
600 if (dev->irq < 2)
602 unsigned long cookie = probe_irq_on();
603 outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
604 outb_p(0x00, ioaddr + EN0_RCNTLO);
605 outb_p(0x00, ioaddr + EN0_RCNTHI);
606 outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
607 mdelay(10); /* wait 10ms for interrupt to propagate */
608 outb_p(0x00, ioaddr + EN0_IMR); /* Mask it again. */
609 dev->irq = probe_irq_off(cookie);
610 if (ei_debug > 2)
611 printk(" autoirq is %d\n", dev->irq);
612 } else if (dev->irq == 2)
613 /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
614 or don't know which one to set. */
615 dev->irq = 9;
617 if (! dev->irq) {
618 printk(" failed to detect IRQ line.\n");
619 ret = -EAGAIN;
620 goto err_out;
623 /* Snarf the interrupt now. There's no point in waiting since we cannot
624 share and the board will usually be enabled. */
625 ret = request_irq(dev->irq, ei_interrupt, 0, name, dev);
626 #ifdef CONFIG_COLDFIRE
627 if (ret == 0)
628 ne2000_irqsetup(dev->irq);
629 #endif
630 if (ret) {
631 printk (" unable to get IRQ %d (errno=%d).\n", dev->irq, ret);
632 goto err_out;
635 dev->base_addr = ioaddr;
637 #ifdef CONFIG_PLAT_MAPPI
638 outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
639 ioaddr + E8390_CMD); /* 0x61 */
640 for (i = 0 ; i < ETHER_ADDR_LEN ; i++) {
641 dev->dev_addr[i] = SA_prom[i]
642 = inb_p(ioaddr + EN1_PHYS_SHIFT(i));
643 printk(" %2.2x", SA_prom[i]);
645 #else
646 for(i = 0; i < ETHER_ADDR_LEN; i++) {
647 printk(" %2.2x", SA_prom[i]);
648 dev->dev_addr[i] = SA_prom[i];
650 #endif
652 printk("\n%s: %s found at %#x, using IRQ %d.\n",
653 dev->name, name, ioaddr, dev->irq);
655 ei_status.name = name;
656 ei_status.tx_start_page = start_page;
657 ei_status.stop_page = stop_page;
659 /* Use 16-bit mode only if this wasn't overridden by DCR_VAL */
660 ei_status.word16 = (wordlength == 2 && (DCR_VAL & 0x01));
662 ei_status.rx_start_page = start_page + TX_PAGES;
663 #ifdef PACKETBUF_MEMSIZE
664 /* Allow the packet buffer size to be overridden by know-it-alls. */
665 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
666 #endif
668 ei_status.reset_8390 = &ne_reset_8390;
669 ei_status.block_input = &ne_block_input;
670 ei_status.block_output = &ne_block_output;
671 ei_status.get_8390_hdr = &ne_get_8390_hdr;
672 ei_status.priv = 0;
673 dev->open = &ne_open;
674 dev->stop = &ne_close;
675 #ifdef CONFIG_NET_POLL_CONTROLLER
676 dev->poll_controller = ei_poll;
677 #endif
678 NS8390_init(dev, 0);
680 ret = register_netdev(dev);
681 if (ret)
682 goto out_irq;
683 return 0;
685 out_irq:
686 free_irq(dev->irq, dev);
687 err_out:
688 #if !defined(CONFIG_COLDFIRE) && !defined(CONFIG_CPU_H8300H)
689 release_region(ioaddr, NE_IO_EXTENT);
690 #endif
691 return ret;
694 static int ne_open(struct net_device *dev)
696 ei_open(dev);
697 return 0;
700 static int ne_close(struct net_device *dev)
702 if (ei_debug > 1)
703 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
704 ei_close(dev);
705 return 0;
708 /* Hard reset the card. This used to pause for the same period that a
709 8390 reset command required, but that shouldn't be necessary. */
711 static void ne_reset_8390(struct net_device *dev)
713 unsigned long reset_start_time = jiffies;
715 if (ei_debug > 1)
716 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
718 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
719 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
721 ei_status.txing = 0;
722 ei_status.dmaing = 0;
724 /* This check _should_not_ be necessary, omit eventually. */
725 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
726 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
727 printk(KERN_WARNING "%s: ne_reset_8390() did not complete.\n", dev->name);
728 break;
730 outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
733 /* Grab the 8390 specific header. Similar to the block_input routine, but
734 we don't need to be concerned with ring wrap as the header will be at
735 the start of a page, so we optimize accordingly. */
737 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
739 int nic_base = dev->base_addr;
741 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
743 if (ei_status.dmaing)
745 printk(KERN_EMERG "%s: DMAing conflict in ne_get_8390_hdr "
746 "[DMAstat:%d][irqlock:%d].\n",
747 dev->name, ei_status.dmaing, ei_status.irqlock);
748 return;
751 ei_status.dmaing |= 0x01;
752 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
753 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
754 outb_p(0, nic_base + EN0_RCNTHI);
755 outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
756 outb_p(ring_page, nic_base + EN0_RSARHI);
757 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
759 if (ei_status.word16)
760 insw(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
761 else
762 insb(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
764 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
765 ei_status.dmaing &= ~0x01;
767 le16_to_cpus(&hdr->count);
770 /* Block input and output, similar to the Crynwr packet driver. If you
771 are porting to a new ethercard, look at the packet driver source for hints.
772 The NEx000 doesn't share the on-board packet memory -- you have to put
773 the packet out through the "remote DMA" dataport using outb. */
775 static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
777 #ifdef NE_SANITY_CHECK
778 int xfer_count = count;
779 #endif
780 int nic_base = dev->base_addr;
781 char *buf = skb->data;
783 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
784 if (ei_status.dmaing)
786 printk(KERN_EMERG "%s: DMAing conflict in ne_block_input "
787 "[DMAstat:%d][irqlock:%d].\n",
788 dev->name, ei_status.dmaing, ei_status.irqlock);
789 return;
791 ei_status.dmaing |= 0x01;
792 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
793 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
794 outb_p(count >> 8, nic_base + EN0_RCNTHI);
795 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
796 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
797 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
798 if (ei_status.word16)
800 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
801 if (count & 0x01)
803 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
804 #ifdef NE_SANITY_CHECK
805 xfer_count++;
806 #endif
808 } else {
809 insb(NE_BASE + NE_DATAPORT, buf, count);
812 #ifdef NE_SANITY_CHECK
813 /* This was for the ALPHA version only, but enough people have
814 been encountering problems so it is still here. If you see
815 this message you either 1) have a slightly incompatible clone
816 or 2) have noise/speed problems with your bus. */
818 if (ei_debug > 1)
820 /* DMA termination address check... */
821 int addr, tries = 20;
822 do {
823 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
824 -- it's broken for Rx on some cards! */
825 int high = inb_p(nic_base + EN0_RSARHI);
826 int low = inb_p(nic_base + EN0_RSARLO);
827 addr = (high << 8) + low;
828 if (((ring_offset + xfer_count) & 0xff) == low)
829 break;
830 } while (--tries > 0);
831 if (tries <= 0)
832 printk(KERN_WARNING "%s: RX transfer address mismatch,"
833 "%#4.4x (expected) vs. %#4.4x (actual).\n",
834 dev->name, ring_offset + xfer_count, addr);
836 #endif
837 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
838 ei_status.dmaing &= ~0x01;
841 static void ne_block_output(struct net_device *dev, int count,
842 const unsigned char *buf, const int start_page)
844 int nic_base = NE_BASE;
845 unsigned long dma_start;
846 #ifdef NE_SANITY_CHECK
847 int retries = 0;
848 #endif
850 /* Round the count up for word writes. Do we need to do this?
851 What effect will an odd byte count have on the 8390?
852 I should check someday. */
854 if (ei_status.word16 && (count & 0x01))
855 count++;
857 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
858 if (ei_status.dmaing)
860 printk(KERN_EMERG "%s: DMAing conflict in ne_block_output."
861 "[DMAstat:%d][irqlock:%d]\n",
862 dev->name, ei_status.dmaing, ei_status.irqlock);
863 return;
865 ei_status.dmaing |= 0x01;
866 /* We should already be in page 0, but to be safe... */
867 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
869 #ifdef NE_SANITY_CHECK
870 retry:
871 #endif
873 #ifdef NE8390_RW_BUGFIX
874 /* Handle the read-before-write bug the same way as the
875 Crynwr packet driver -- the NatSemi method doesn't work.
876 Actually this doesn't always work either, but if you have
877 problems with your NEx000 this is better than nothing! */
879 outb_p(0x42, nic_base + EN0_RCNTLO);
880 outb_p(0x00, nic_base + EN0_RCNTHI);
881 outb_p(0x42, nic_base + EN0_RSARLO);
882 outb_p(0x00, nic_base + EN0_RSARHI);
883 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
884 /* Make certain that the dummy read has occurred. */
885 udelay(6);
886 #endif
888 outb_p(ENISR_RDC, nic_base + EN0_ISR);
890 /* Now the normal output. */
891 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
892 outb_p(count >> 8, nic_base + EN0_RCNTHI);
893 outb_p(0x00, nic_base + EN0_RSARLO);
894 outb_p(start_page, nic_base + EN0_RSARHI);
896 outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
897 if (ei_status.word16) {
898 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
899 } else {
900 outsb(NE_BASE + NE_DATAPORT, buf, count);
903 dma_start = jiffies;
905 #ifdef NE_SANITY_CHECK
906 /* This was for the ALPHA version only, but enough people have
907 been encountering problems so it is still here. */
909 if (ei_debug > 1)
911 /* DMA termination address check... */
912 int addr, tries = 20;
913 do {
914 int high = inb_p(nic_base + EN0_RSARHI);
915 int low = inb_p(nic_base + EN0_RSARLO);
916 addr = (high << 8) + low;
917 if ((start_page << 8) + count == addr)
918 break;
919 } while (--tries > 0);
921 if (tries <= 0)
923 printk(KERN_WARNING "%s: Tx packet transfer address mismatch,"
924 "%#4.4x (expected) vs. %#4.4x (actual).\n",
925 dev->name, (start_page << 8) + count, addr);
926 if (retries++ == 0)
927 goto retry;
930 #endif
932 while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
933 if (time_after(jiffies, dma_start + 2*HZ/100)) { /* 20ms */
934 printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
935 ne_reset_8390(dev);
936 NS8390_init(dev,1);
937 break;
940 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
941 ei_status.dmaing &= ~0x01;
942 return;
946 #ifdef MODULE
947 #define MAX_NE_CARDS 4 /* Max number of NE cards per module */
948 static struct net_device *dev_ne[MAX_NE_CARDS];
949 static int io[MAX_NE_CARDS];
950 static int irq[MAX_NE_CARDS];
951 static int bad[MAX_NE_CARDS]; /* 0xbad = bad sig or no reset ack */
953 module_param_array(io, int, NULL, 0);
954 module_param_array(irq, int, NULL, 0);
955 module_param_array(bad, int, NULL, 0);
956 MODULE_PARM_DESC(io, "I/O base address(es),required");
957 MODULE_PARM_DESC(irq, "IRQ number(s)");
958 MODULE_PARM_DESC(bad, "Accept card(s) with bad signatures");
959 MODULE_DESCRIPTION("NE1000/NE2000 ISA/PnP Ethernet driver");
960 MODULE_LICENSE("GPL");
962 /* This is set up so that no ISA autoprobe takes place. We can't guarantee
963 that the ne2k probe is the last 8390 based probe to take place (as it
964 is at boot) and so the probe will get confused by any other 8390 cards.
965 ISA device autoprobes on a running machine are not recommended anyway. */
967 int __init init_module(void)
969 int this_dev, found = 0;
971 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
972 struct net_device *dev = alloc_ei_netdev();
973 if (!dev)
974 break;
975 dev->irq = irq[this_dev];
976 dev->mem_end = bad[this_dev];
977 dev->base_addr = io[this_dev];
978 if (do_ne_probe(dev) == 0) {
979 dev_ne[found++] = dev;
980 continue;
982 free_netdev(dev);
983 if (found)
984 break;
985 if (io[this_dev] != 0)
986 printk(KERN_WARNING "ne.c: No NE*000 card found at i/o = %#x\n", io[this_dev]);
987 else
988 printk(KERN_NOTICE "ne.c: You must supply \"io=0xNNN\" value(s) for ISA cards.\n");
989 return -ENXIO;
991 if (found)
992 return 0;
993 return -ENODEV;
996 static void cleanup_card(struct net_device *dev)
998 struct pnp_dev *idev = (struct pnp_dev *)ei_status.priv;
999 if (idev)
1000 pnp_device_detach(idev);
1001 free_irq(dev->irq, dev);
1002 #if !defined(CONFIG_COLDFIRE) && !defined(CONFIG_CPU_H8300H)
1003 release_region(dev->base_addr, NE_IO_EXTENT);
1004 #endif
1007 void cleanup_module(void)
1009 int this_dev;
1011 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
1012 struct net_device *dev = dev_ne[this_dev];
1013 if (dev) {
1014 unregister_netdev(dev);
1015 cleanup_card(dev);
1016 free_netdev(dev);
1020 #endif /* MODULE */