1 /* ne.c: A general non-shared-memory NS8390 ethernet driver for linux. */ 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 Public License, incorporated herein by reference. 11 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O 12 Center of Excellence in Space Data and Information Sciences 13 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 15 This driver should work with many programmed-I/O 8390-based ethernet 16 boards. Currently it supports the NE1000, NE2000, many clones, 17 and some Cabletron products. 21 Paul Gortmaker : use ENISR_RDC to monitor Tx PIO uploads, made 22 sanity checks and bad clone support optional. 23 Paul Gortmaker : new reset code, reset card after probe at boot. 27 /* Routines for the NatSemi-based designs (NE[12]000). */ 29 static const char*version
= 30 "ne.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n"; 34 #include <linux/module.h> 35 #include <linux/version.h> 38 #include <linux/kernel.h> 39 #include <linux/sched.h> 40 #include <linux/errno.h> 41 #include <asm/system.h> 44 #include <linux/netdevice.h> 47 /* Some defines that people can play with if so inclined. */ 49 /* Do we support clones that don't adhere to 14,15 of the SAprom ? */ 50 #define CONFIG_NE_BAD_CLONES 52 /* Do we perform extra sanity checks on stuff ? */ 53 /* #define CONFIG_NE_SANITY */ 54 #ifdef CONFIG_NE_SANITY 55 static int config_ne_sanity
=1; 57 static int config_ne_sanity
=0; 60 /* Do we implement the read before write bugfix ? */ 61 /* #define CONFIG_NE_RW_BUGFIX */ 62 #ifdef CONFIG_NE_RW_BUGFIX 63 static int ne8390_rw_bugfix
=1; 65 static int ne8390_rw_bugfix
=0; 68 /* ---- No user-serviceable parts below ---- */ 70 externstruct device
*init_etherdev(struct device
*dev
,int sizeof_private
, 71 unsigned long*mem_startp
); 74 /* A zero-terminated list of I/O addresses to be probed. */ 75 static unsigned int netcard_portlist
[] = 76 {0x300,0x280,0x320,0x340,0x360,0}; 78 #ifdef CONFIG_NE_BAD_CLONES 79 /* A list of bad clones that we none-the-less recognize. */ 80 static struct{const char*name8
, *name16
;unsigned char SAprefix
[4];} 82 {"DE100","DE200", {0x00,0xDE,0x01,}}, 83 {"DE120","DE220", {0x00,0x80,0xc8,}}, 84 {"DFI1000","DFI2000", {'D','F','I',}},/* Original, eh? */ 85 {"EtherNext UTP8","EtherNext UTP16", {0x00,0x00,0x79}}, 86 {"NE1000","NE2000-invalid", {0x00,0x00,0xd8}},/* Ancient real NE1000. */ 87 {"NN1000","NN2000", {0x08,0x03,0x08}},/* Outlaw no-name clone. */ 88 {"4-DIM8","4-DIM16", {0x00,0x00,0x4d,}},/* Outlaw 4-Dimension cards. */ 93 #define NE_BASE (dev->base_addr) 95 #define NE_DATAPORT 0x10/* NatSemi-defined port window offset. */ 96 #define NE_RESET 0x1f/* Issue a read to reset, a write to clear. */ 97 #define NE_IO_EXTENT 0x20 99 #define NE1SM_START_PG 0x20/* First page of TX buffer */ 100 #define NE1SM_STOP_PG 0x40/* Last page +1 of RX ring */ 101 #define NESM_START_PG 0x40/* First page of TX buffer */ 102 #define NESM_STOP_PG 0x80/* Last page +1 of RX ring */ 104 intne_probe(struct device
*dev
); 105 static intne_probe1(struct device
*dev
,int ioaddr
); 107 static voidne_reset_8390(struct device
*dev
); 108 static intne_block_input(struct device
*dev
,int count
, 109 char*buf
,int ring_offset
); 110 static voidne_block_output(struct device
*dev
,const int count
, 111 const unsigned char*buf
,const int start_page
); 114 /* Probe for various non-shared-memory ethercards. 116 NEx000-clone boards have a Station Address PROM (SAPROM) in the packet 117 buffer memory space. NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of 118 the SAPROM, while other supposed NE2000 clones must be detected by their 121 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide 122 mode results in doubled values, which can be detected and compensated for. 124 The probe is also responsible for initializing the card and filling 125 in the 'dev' and 'ei_status' structures. 127 We use the minimum memory size for some ethercard product lines, iff we can't 128 distinguish models. You can increase the packet buffer size by setting 129 PACKETBUF_MEMSIZE. Reported Cabletron packet buffer locations are: 130 E1010 starts at 0x100 and ends at 0x2000. 131 E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory") 132 E2010 starts at 0x100 and ends at 0x4000. 133 E2010-x starts at 0x100 and ends at 0xffff. */ 136 struct netdev_entry netcard_drv
= 137 {"ne", ne_probe1
, NE_IO_EXTENT
, netcard_portlist
}; 140 intne_probe(struct device
*dev
) 143 int base_addr
= dev
? dev
->base_addr
:0; 145 if(base_addr
>0x1ff)/* Check a single specified location. */ 146 returnne_probe1(dev
, base_addr
); 147 else if(base_addr
!=0)/* Don't probe at all. */ 150 for(i
=0; netcard_portlist
[i
]; i
++) { 151 int ioaddr
= netcard_portlist
[i
]; 152 if(check_region(ioaddr
, NE_IO_EXTENT
)) 154 if(ne_probe1(dev
, ioaddr
) ==0) 162 static intne_probe1(struct device
*dev
,int ioaddr
) 165 unsigned char SA_prom
[32]; 167 const char*name
= NULL
; 168 int start_page
, stop_page
; 170 int reg0
=inb_p(ioaddr
); 175 /* Do a preliminary verification that we have a 8390. */ 177 outb_p(E8390_NODMA
+E8390_PAGE1
+E8390_STOP
, ioaddr
+ E8390_CMD
); 178 regd
=inb_p(ioaddr
+0x0d); 179 outb_p(0xff, ioaddr
+0x0d); 180 outb_p(E8390_NODMA
+E8390_PAGE0
, ioaddr
+ E8390_CMD
); 181 inb_p(ioaddr
+ EN0_COUNTER0
);/* Clear the counter by reading. */ 182 if(inb_p(ioaddr
+ EN0_COUNTER0
) !=0) { 183 outb_p(reg0
, ioaddr
); 184 outb_p(regd
, ioaddr
+0x0d);/* Restore the old values. */ 189 printk("NE*000 ethercard probe at %#3x:", ioaddr
); 191 /* Reset card. Who knows what dain-bramaged state it was left in. */ 192 {unsigned long reset_start_time
= jiffies
; 194 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */ 195 outb(inb(ioaddr
+ NE_RESET
), ioaddr
+ NE_RESET
); 197 /* wait 20 ms for the dust to settle. */ 198 while(jiffies
- reset_start_time
<2*HZ
/100) 201 if((inb_p(ioaddr
+EN0_ISR
) & ENISR_RESET
) ==0) { 202 printk(" not found (no reset ack).\n"); 205 outb_p(0xff, ioaddr
+ EN0_ISR
);/* Ack all intr. */ 208 /* Read the 16 bytes of station address PROM. 209 We must first initialize registers, similar to NS8390_init(eifdev, 0). 210 We can't reliably read the SAPROM address without this. 211 (I learned the hard way!). */ 213 struct{unsigned char value
, offset
; } program_seq
[] = { 214 {E8390_NODMA
+E8390_PAGE0
+E8390_STOP
, E8390_CMD
},/* Select page 0*/ 215 {0x48, EN0_DCFG
},/* Set byte-wide (0x48) access. */ 216 {0x00, EN0_RCNTLO
},/* Clear the count regs. */ 218 {0x00, EN0_IMR
},/* Mask completion irq. */ 220 {E8390_RXOFF
, EN0_RXCR
},/* 0x20 Set to monitor */ 221 {E8390_TXOFF
, EN0_TXCR
},/* 0x02 and loopback mode. */ 224 {0x00, EN0_RSARLO
},/* DMA starting at 0x0000. */ 226 {E8390_RREAD
+E8390_START
, E8390_CMD
}, 228 for(i
=0; i
<sizeof(program_seq
)/sizeof(program_seq
[0]); i
++) 229 outb_p(program_seq
[i
].value
, ioaddr
+ program_seq
[i
].offset
); 232 for(i
=0; i
<32/*sizeof(SA_prom)*/; i
+=2) { 233 SA_prom
[i
] =inb(ioaddr
+ NE_DATAPORT
); 234 SA_prom
[i
+1] =inb(ioaddr
+ NE_DATAPORT
); 235 if(SA_prom
[i
] != SA_prom
[i
+1]) 240 /* We must set the 8390 for word mode. */ 241 outb_p(0x49, ioaddr
+ EN0_DCFG
); 242 /* We used to reset the ethercard here, but it doesn't seem 244 /* Un-double the SA_prom values. */ 245 for(i
=0; i
<16; i
++) 246 SA_prom
[i
] = SA_prom
[i
+i
]; 247 start_page
= NESM_START_PG
; 248 stop_page
= NESM_STOP_PG
; 250 start_page
= NE1SM_START_PG
; 251 stop_page
= NE1SM_STOP_PG
; 254 for(i
=0; i
< ETHER_ADDR_LEN
; i
++) { 255 dev
->dev_addr
[i
] = SA_prom
[i
]; 256 printk(" %2.2x", SA_prom
[i
]); 259 neX000
= (SA_prom
[14] ==0x57&& SA_prom
[15] ==0x57); 260 ctron
= (SA_prom
[0] ==0x00&& SA_prom
[1] ==0x00&& SA_prom
[2] ==0x1d); 262 /* Set up the rest of the parameters. */ 264 name
= (wordlength
==2) ?"NE2000":"NE1000"; 268 stop_page
= (wordlength
==2) ?0x40:0x20; 270 #ifdef CONFIG_NE_BAD_CLONES 271 /* Ack! Well, there might be a *bad* NE*000 clone there. 272 Check for total bogus addresses. */ 273 for(i
=0; bad_clone_list
[i
].name8
; i
++) { 274 if(SA_prom
[0] == bad_clone_list
[i
].SAprefix
[0] && 275 SA_prom
[1] == bad_clone_list
[i
].SAprefix
[1] && 276 SA_prom
[2] == bad_clone_list
[i
].SAprefix
[2]) { 278 name
= bad_clone_list
[i
].name16
; 280 name
= bad_clone_list
[i
].name8
; 285 if(bad_clone_list
[i
].name8
== NULL
) { 286 printk(" not found (invalid signature %2.2x %2.2x).\n", 287 SA_prom
[14], SA_prom
[15]); 291 printk(" not found.\n"); 299 dev
=init_etherdev(0,sizeof(struct ei_device
),0); 303 outb_p(0x50, ioaddr
+ EN0_IMR
);/* Enable one interrupt. */ 304 outb_p(0x00, ioaddr
+ EN0_RCNTLO
); 305 outb_p(0x00, ioaddr
+ EN0_RCNTHI
); 306 outb_p(E8390_RREAD
+E8390_START
, ioaddr
);/* Trigger it... */ 307 outb_p(0x00, ioaddr
+ EN0_IMR
);/* Mask it again. */ 308 dev
->irq
=autoirq_report(0); 310 printk(" autoirq is %d\n", dev
->irq
); 311 }else if(dev
->irq
==2) 312 /* Fixup for users that don't know that IRQ 2 is really IRQ 9, 313 or don't know which one to set. */ 316 /* Snarf the interrupt now. There's no point in waiting since we cannot 317 share and the board will usually be enabled. */ 319 int irqval
=request_irq(dev
->irq
, ei_interrupt
,0, wordlength
==2?"ne2000":"ne1000"); 321 printk(" unable to get IRQ %d (irqval=%d).\n", dev
->irq
, irqval
); 326 dev
->base_addr
= ioaddr
; 328 request_region(ioaddr
, NE_IO_EXTENT
, wordlength
==2?"ne2000":"ne1000"); 330 for(i
=0; i
< ETHER_ADDR_LEN
; i
++) 331 dev
->dev_addr
[i
] = SA_prom
[i
]; 334 printk("\n%s: %s found at %#x, using IRQ %d.\n", 335 dev
->name
, name
, ioaddr
, dev
->irq
); 340 ei_status
.name
= name
; 341 ei_status
.tx_start_page
= start_page
; 342 ei_status
.stop_page
= stop_page
; 343 ei_status
.word16
= (wordlength
==2); 345 ei_status
.rx_start_page
= start_page
+ TX_PAGES
; 346 #ifdef PACKETBUF_MEMSIZE 347 /* Allow the packet buffer size to be overridden by know-it-alls. */ 348 ei_status
.stop_page
= ei_status
.tx_start_page
+ PACKETBUF_MEMSIZE
; 351 ei_status
.reset_8390
= &ne_reset_8390
; 352 ei_status
.block_input
= &ne_block_input
; 353 ei_status
.block_output
= &ne_block_output
; 358 /* Hard reset the card. This used to pause for the same period that a 359 8390 reset command required, but that shouldn't be necessary. */ 361 ne_reset_8390(struct device
*dev
) 363 unsigned long reset_start_time
= jiffies
; 365 if(ei_debug
>1)printk("resetting the 8390 t=%ld...", jiffies
); 367 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */ 368 outb(inb(NE_BASE
+ NE_RESET
), NE_BASE
+ NE_RESET
); 373 /* This check _should_not_ be necessary, omit eventually. */ 374 while((inb_p(NE_BASE
+EN0_ISR
) & ENISR_RESET
) ==0) 375 if(jiffies
- reset_start_time
>2*HZ
/100) { 376 printk("%s: ne_reset_8390() did not complete.\n", dev
->name
); 379 outb_p(ENISR_RESET
, NE_BASE
+ EN0_ISR
);/* Ack intr. */ 382 /* Block input and output, similar to the Crynwr packet driver. If you 383 are porting to a new ethercard, look at the packet driver source for hints. 384 The NEx000 doesn't share it on-board packet memory -- you have to put 385 the packet out through the "remote DMA" dataport using outb. */ 388 ne_block_input(struct device
*dev
,int count
,char*buf
,int ring_offset
) 390 int nic_base
= dev
->base_addr
; 391 /* CONFIG_NE_SANITY */ 392 int xfer_count
= count
; 394 /* This *shouldn't* happen. If it does, it's the last thing you'll see */ 395 if(ei_status
.dmaing
) { 397 printk("%s: DMAing conflict in ne_block_input " 398 "[DMAstat:%d][irqlock:%d][intr:%d].\n", 399 dev
->name
, ei_status
.dmaing
, ei_status
.irqlock
, 403 ei_status
.dmaing
|=0x01; 404 outb_p(E8390_NODMA
+E8390_PAGE0
+E8390_START
, nic_base
+ NE_CMD
); 405 outb_p(count
&0xff, nic_base
+ EN0_RCNTLO
); 406 outb_p(count
>>8, nic_base
+ EN0_RCNTHI
); 407 outb_p(ring_offset
&0xff, nic_base
+ EN0_RSARLO
); 408 outb_p(ring_offset
>>8, nic_base
+ EN0_RSARHI
); 409 outb_p(E8390_RREAD
+E8390_START
, nic_base
+ NE_CMD
); 410 if(ei_status
.word16
) { 411 insw(NE_BASE
+ NE_DATAPORT
,buf
,count
>>1); 413 buf
[count
-1] =inb(NE_BASE
+ NE_DATAPORT
); 414 /* CONFIG_NE_SANITY */ 418 insb(NE_BASE
+ NE_DATAPORT
, buf
, count
); 421 /* This was for the ALPHA version only, but enough people have 422 been encountering problems so it is still here. If you see 423 this message you either 1) have a slightly incompatible clone 424 or 2) have noise/speed problems with your bus. */ 425 if(config_ne_sanity
&& 426 ei_debug
>1) {/* DMA termination address check... */ 429 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here 430 -- it's broken for Rx on some cards! */ 431 int high
=inb_p(nic_base
+ EN0_RSARHI
); 432 int low
=inb_p(nic_base
+ EN0_RSARLO
); 433 addr
= (high
<<8) + low
; 434 if(((ring_offset
+ xfer_count
) &0xff) == low
) 438 printk("%s: RX transfer address mismatch," 439 "%#4.4x (expected) vs. %#4.4x (actual).\n", 440 dev
->name
, ring_offset
+ xfer_count
, addr
); 442 outb_p(ENISR_RDC
, nic_base
+ EN0_ISR
);/* Ack intr. */ 443 ei_status
.dmaing
&= ~0x01; 444 return ring_offset
+ count
; 448 ne_block_output(struct device
*dev
,int count
, 449 const unsigned char*buf
,const int start_page
) 451 int nic_base
= NE_BASE
; 452 unsigned long dma_start
; 453 /* CONFIG_NE_SANITY */ 456 /* Round the count up for word writes. Do we need to do this? 457 What effect will an odd byte count have on the 8390? 458 I should check someday. */ 459 if(ei_status
.word16
&& (count
&0x01)) 462 /* This *shouldn't* happen. If it does, it's the last thing you'll see */ 463 if(ei_status
.dmaing
) { 465 printk("%s: DMAing conflict in ne_block_output." 466 "[DMAstat:%d][irqlock:%d][intr:%d]\n", 467 dev
->name
, ei_status
.dmaing
, ei_status
.irqlock
, 471 ei_status
.dmaing
|=0x01; 472 /* We should already be in page 0, but to be safe... */ 473 outb_p(E8390_PAGE0
+E8390_START
+E8390_NODMA
, nic_base
+ NE_CMD
); 477 if(ne8390_rw_bugfix
) { 478 /* Handle the read-before-write bug the same way as the 479 Crynwr packet driver -- the NatSemi method doesn't work. 480 Actually this doesn't always work either, but if you have 481 problems with your NEx000 this is better than nothing! */ 482 outb_p(0x42, nic_base
+ EN0_RCNTLO
); 483 outb_p(0x00, nic_base
+ EN0_RCNTHI
); 484 outb_p(0x42, nic_base
+ EN0_RSARLO
); 485 outb_p(0x00, nic_base
+ EN0_RSARHI
); 486 outb_p(E8390_RREAD
+E8390_START
, nic_base
+ NE_CMD
); 487 /* Make certain that the dummy read has occurred. */ 493 outb_p(ENISR_RDC
, nic_base
+ EN0_ISR
); 495 /* Now the normal output. */ 496 outb_p(count
&0xff, nic_base
+ EN0_RCNTLO
); 497 outb_p(count
>>8, nic_base
+ EN0_RCNTHI
); 498 outb_p(0x00, nic_base
+ EN0_RSARLO
); 499 outb_p(start_page
, nic_base
+ EN0_RSARHI
); 501 outb_p(E8390_RWRITE
+E8390_START
, nic_base
+ NE_CMD
); 502 if(ei_status
.word16
) { 503 outsw(NE_BASE
+ NE_DATAPORT
, buf
, count
>>1); 505 outsb(NE_BASE
+ NE_DATAPORT
, buf
, count
); 510 /* This was for the ALPHA version only, but enough people have 511 been encountering problems so it is still here. */ 512 if(config_ne_sanity
&& 513 ei_debug
>1) {/* DMA termination address check... */ 516 int high
=inb_p(nic_base
+ EN0_RSARHI
); 517 int low
=inb_p(nic_base
+ EN0_RSARLO
); 518 addr
= (high
<<8) + low
; 519 if((start_page
<<8) + count
== addr
) 523 printk("%s: Tx packet transfer address mismatch," 524 "%#4.4x (expected) vs. %#4.4x (actual).\n", 525 dev
->name
, (start_page
<<8) + count
, addr
); 531 while((inb_p(nic_base
+ EN0_ISR
) & ENISR_RDC
) ==0) 532 if(jiffies
- dma_start
>2*HZ
/100) {/* 20ms */ 533 printk("%s: timeout waiting for Tx RDC.\n", dev
->name
); 539 outb_p(ENISR_RDC
, nic_base
+ EN0_ISR
);/* Ack intr. */ 540 ei_status
.dmaing
&= ~0x01; 545 char kernel_version
[] = UTS_RELEASE
; 546 static struct device dev_ne2000
= { 547 " "/*"ne2000"*/,0,0,0,0,0,0,0,0,0, NULL
, ne_probe
}; 554 dev_ne2000
.base_addr
= io
; 555 dev_ne2000
.irq
= irq
; 556 if(register_netdev(&dev_ne2000
) !=0) 565 printk("ne2000: device busy, remove delayed\n"); 568 unregister_netdev(&dev_ne2000
); 575 * compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne.c" 577 * kept-new-versions: 5