Import 2.3.18pre1
[davej-history.git] / drivers / net / cs89x0.c
blobac6ff986c8f8d1c72edfba75c1cb28210bf51c07
1 /* cs89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. */
2 /*
3 Written 1996 by Russell Nelson, with reference to skeleton.c
4 written 1993-1994 by Donald Becker.
6 This software may be used and distributed according to the terms
7 of the GNU Public License, incorporated herein by reference.
9 The author may be reached at nelson@crynwr.com, Crynwr
10 Software, 11 Grant St., Potsdam, NY 13676
12 Changelog:
14 Mike Cruse : mcruse@cti-ltd.com
15 : Changes for Linux 2.0 compatibility.
16 : Added dev_id parameter in net_interrupt(),
17 : request_irq() and free_irq(). Just NULL for now.
19 Mike Cruse : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
20 : in net_open() and net_close() so kerneld would know
21 : that the module is in use and wouldn't eject the
22 : driver prematurely.
24 Mike Cruse : Rewrote init_module() and cleanup_module using 8390.c
25 : as an example. Disabled autoprobing in init_module(),
26 : not a good thing to do to other devices while Linux
27 : is running from all accounts.
29 Alan Cox : Removed 1.2 support, added 2.1 extra counters.
32 static char *version =
33 "cs89x0.c:v1.03 11/26/96 Russell Nelson <nelson@crynwr.com>\n";
35 /* ======================= configure the driver here ======================= */
37 /* use 0 for production, 1 for verification, >2 for debug */
38 #ifndef NET_DEBUG
39 #define NET_DEBUG 2
40 #endif
42 /* ======================= end of configuration ======================= */
45 /* Always include 'config.h' first in case the user wants to turn on
46 or override something. */
47 #ifdef MODULE
48 #include <linux/module.h>
49 #include <linux/version.h>
50 #else
51 #define MOD_INC_USE_COUNT
52 #define MOD_DEC_USE_COUNT
53 #endif
55 #define PRINTK(x) printk x
58 Sources:
60 Crynwr packet driver epktisa.
62 Crystal Semiconductor data sheets.
66 #include <linux/kernel.h>
67 #include <linux/sched.h>
68 #include <linux/types.h>
69 #include <linux/fcntl.h>
70 #include <linux/interrupt.h>
71 #include <linux/ptrace.h>
72 #include <linux/ioport.h>
73 #include <linux/in.h>
74 #include <linux/malloc.h>
75 #include <linux/string.h>
76 #include <asm/system.h>
77 #include <asm/bitops.h>
78 #include <asm/io.h>
79 #include <linux/errno.h>
80 #include <linux/init.h>
82 #include <linux/netdevice.h>
83 #include <linux/etherdevice.h>
84 #include <linux/skbuff.h>
85 #include "cs89x0.h"
87 /* First, a few definitions that the brave might change. */
88 /* A zero-terminated list of I/O addresses to be probed. */
89 static unsigned int netcard_portlist[] __initdata =
90 { 0x300, 0x320, 0x340, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};
92 static unsigned int net_debug = NET_DEBUG;
94 /* The number of low I/O ports used by the ethercard. */
95 #define NETCARD_IO_EXTENT 16
97 /* Information that need to be kept for each board. */
98 struct net_local {
99 struct net_device_stats stats;
100 int chip_type; /* one of: CS8900, CS8920, CS8920M */
101 char chip_revision; /* revision letter of the chip ('A'...) */
102 int send_cmd; /* the propercommand used to send a packet. */
103 int auto_neg_cnf;
104 int adapter_cnf;
105 int isa_config;
106 int irq_map;
107 int rx_mode;
108 int curr_rx_cfg;
109 int linectl;
110 int send_underrun; /* keep track of how many underruns in a row we get */
111 struct sk_buff *skb;
114 /* Index to functions, as function prototypes. */
116 extern int cs89x0_probe(struct net_device *dev);
118 static int cs89x0_probe1(struct net_device *dev, int ioaddr);
119 static int net_open(struct net_device *dev);
120 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
121 static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
122 static void set_multicast_list(struct net_device *dev);
123 static void net_rx(struct net_device *dev);
124 static int net_close(struct net_device *dev);
125 static struct net_device_stats *net_get_stats(struct net_device *dev);
126 static void reset_chip(struct net_device *dev);
127 static int get_eeprom_data(struct net_device *dev, int off, int len, int *buffer);
128 static int get_eeprom_cksum(int off, int len, int *buffer);
129 static int set_mac_address(struct net_device *dev, void *addr);
132 /* Example routines you must write ;->. */
133 #define tx_done(dev) 1
136 /* Check for a network adaptor of this type, and return '0' iff one exists.
137 If dev->base_addr == 0, probe all likely locations.
138 If dev->base_addr == 1, always return failure.
139 If dev->base_addr == 2, allocate space for the device and return success
140 (detachable devices only).
142 #ifdef HAVE_DEVLIST
143 /* Support for a alternate probe manager, which will eliminate the
144 boilerplate below. */
145 struct netdev_entry netcard_drv =
146 {"netcard", cs89x0_probe1, NETCARD_IO_EXTENT, netcard_portlist};
147 #else
148 int __init
149 cs89x0_probe(struct net_device *dev)
151 int i;
152 int base_addr = dev ? dev->base_addr : 0;
154 if (base_addr > 0x1ff) /* Check a single specified location. */
155 return cs89x0_probe1(dev, base_addr);
156 else if (base_addr != 0) /* Don't probe at all. */
157 return ENXIO;
159 for (i = 0; netcard_portlist[i]; i++) {
160 int ioaddr = netcard_portlist[i];
161 if (check_region(ioaddr, NETCARD_IO_EXTENT))
162 continue;
163 if (cs89x0_probe1(dev, ioaddr) == 0)
164 return 0;
166 printk("cs89x0: no cs8900 or cs8920 detected. Be sure to disable PnP with SETUP\n");
167 return ENODEV;
169 #endif
171 static int inline
172 readreg(struct net_device *dev, int portno)
174 outw(portno, dev->base_addr + ADD_PORT);
175 return inw(dev->base_addr + DATA_PORT);
178 static void inline
179 writereg(struct net_device *dev, int portno, int value)
181 outw(portno, dev->base_addr + ADD_PORT);
182 outw(value, dev->base_addr + DATA_PORT);
186 static int inline
187 readword(struct net_device *dev, int portno)
189 return inw(dev->base_addr + portno);
192 static void inline
193 writeword(struct net_device *dev, int portno, int value)
195 outw(value, dev->base_addr + portno);
198 static int __init
199 wait_eeprom_ready(struct net_device *dev)
201 int timeout = jiffies;
202 /* check to see if the EEPROM is ready, a timeout is used -
203 just in case EEPROM is ready when SI_BUSY in the
204 PP_SelfST is clear */
205 while(readreg(dev, PP_SelfST) & SI_BUSY)
206 if (jiffies - timeout >= 40)
207 return -1;
208 return 0;
211 static int __init
212 get_eeprom_data(struct net_device *dev, int off, int len, int *buffer)
214 int i;
216 if (net_debug > 3) printk("EEPROM data from %x for %x:\n",off,len);
217 for (i = 0; i < len; i++) {
218 if (wait_eeprom_ready(dev) < 0) return -1;
219 /* Now send the EEPROM read command and EEPROM location to read */
220 writereg(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD);
221 if (wait_eeprom_ready(dev) < 0) return -1;
222 buffer[i] = readreg(dev, PP_EEData);
223 if (net_debug > 3) printk("%04x ", buffer[i]);
225 if (net_debug > 3) printk("\n");
226 return 0;
229 static int __init
230 get_eeprom_cksum(int off, int len, int *buffer)
232 int i, cksum;
234 cksum = 0;
235 for (i = 0; i < len; i++)
236 cksum += buffer[i];
237 cksum &= 0xffff;
238 if (cksum == 0)
239 return 0;
240 return -1;
243 /* This is the real probe routine. Linux has a history of friendly device
244 probes on the ISA bus. A good device probes avoids doing writes, and
245 verifies that the correct device exists and functions. */
247 static int __init cs89x0_probe1(struct net_device *dev, int ioaddr)
249 struct net_local *lp;
250 static unsigned version_printed = 0;
251 int i;
252 unsigned rev_type = 0;
253 int eeprom_buff[CHKSUM_LEN];
255 /* Initialize the device structure. */
256 if (dev->priv == NULL) {
257 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
258 memset(dev->priv, 0, sizeof(struct net_local));
260 lp = (struct net_local *)dev->priv;
262 /* if they give us an odd I/O address, then do ONE write to
263 the address port, to get it back to address zero, where we
264 expect to find the EISA signature word. */
265 if (ioaddr & 1) {
266 ioaddr &= ~1;
267 if ((inw(ioaddr + ADD_PORT) & ADD_MASK) != ADD_SIG)
268 return ENODEV;
269 outw(PP_ChipID, ioaddr + ADD_PORT);
272 if (inw(ioaddr + DATA_PORT) != CHIP_EISA_ID_SIG)
273 return ENODEV;
275 /* Fill in the 'dev' fields. */
276 dev->base_addr = ioaddr;
278 /* get the chip type */
279 rev_type = readreg(dev, PRODUCT_ID_ADD);
280 lp->chip_type = rev_type &~ REVISON_BITS;
281 lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
283 /* Check the chip type and revision in order to set the correct send command
284 CS8920 revision C and CS8900 revision F can use the faster send. */
285 lp->send_cmd = TX_AFTER_381;
286 if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
287 lp->send_cmd = TX_NOW;
288 if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
289 lp->send_cmd = TX_NOW;
291 if (net_debug && version_printed++ == 0)
292 printk(version);
294 printk("%s: cs89%c0%s rev %c found at %#3lx",
295 dev->name,
296 lp->chip_type==CS8900?'0':'2',
297 lp->chip_type==CS8920M?"M":"",
298 lp->chip_revision,
299 dev->base_addr);
301 reset_chip(dev);
303 /* First check to see if an EEPROM is attached*/
304 if ((readreg(dev, PP_SelfST) & EEPROM_PRESENT) == 0)
305 printk("\ncs89x0: No EEPROM, relying on command line....\n");
306 else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
307 printk("\ncs89x0: EEPROM read failed, relying on command line.\n");
308 } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) {
309 printk("\ncs89x0: EEPROM checksum bad, relying on command line\n");
310 } else {
311 /* get transmission control word but keep the autonegotiation bits */
312 if (!lp->auto_neg_cnf) lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
313 /* Store adapter configuration */
314 if (!lp->adapter_cnf) lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2];
315 /* Store ISA configuration */
316 lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];
317 /* store the initial memory base address */
318 dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8;
319 for (i = 0; i < ETH_ALEN/2; i++) {
320 dev->dev_addr[i*2] = eeprom_buff[i];
321 dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8;
326 printk(" media %s%s%s",
327 (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"",
328 (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"",
329 (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":"");
331 lp->irq_map = 0xffff;
333 /* If this is a CS8900 then no pnp soft */
334 if (lp->chip_type != CS8900 &&
335 /* Check if the ISA IRQ has been set */
336 (i = readreg(dev, PP_CS8920_ISAINT) & 0xff,
337 (i != 0 && i < CS8920_NO_INTS))) {
338 if (!dev->irq)
339 dev->irq = i;
340 } else {
341 i = lp->isa_config & INT_NO_MASK;
342 if (lp->chip_type == CS8900) {
343 /* the table that follows is dependent upon how you wired up your cs8900
344 * in your system. The table is the same as the cs8900 engineering demo
345 * board. irq_map also depends on the contents of the table. Also see
346 * write_irq, which is the reverse mapping of the table below. */
347 switch(i) {
348 case 0: i = 10; break;
349 case 1: i = 11; break;
350 case 2: i = 12; break;
351 case 3: i = 5; break;
352 default: printk("\ncs89x0: bug: isa_config is %d\n", i);
354 lp->irq_map = CS8900_IRQ_MAP; /* fixed IRQ map for CS8900 */
355 } else {
356 int irq_map_buff[IRQ_MAP_LEN/2];
358 if (get_eeprom_data(dev, IRQ_MAP_EEPROM_DATA,
359 IRQ_MAP_LEN/2,
360 irq_map_buff) >= 0) {
361 if ((irq_map_buff[0] & 0xff) == PNP_IRQ_FRMT)
362 lp->irq_map = (irq_map_buff[0]>>8) | (irq_map_buff[1] << 8);
365 if (!dev->irq)
366 dev->irq = i;
369 printk(" IRQ %d", dev->irq);
372 /* print the ethernet address. */
373 for (i = 0; i < ETH_ALEN; i++)
374 printk(" %2.2x", dev->dev_addr[i]);
376 /* Grab the region so we can find another board if autoIRQ fails. */
377 request_region(ioaddr, NETCARD_IO_EXTENT,"cs89x0");
379 dev->open = net_open;
380 dev->stop = net_close;
381 dev->hard_start_xmit = net_send_packet;
382 dev->get_stats = net_get_stats;
383 dev->set_multicast_list = &set_multicast_list;
384 dev->set_mac_address = &set_mac_address;
386 /* Fill in the fields of the device structure with ethernet values. */
387 ether_setup(dev);
389 printk("\n");
390 return 0;
393 void __init
394 reset_chip(struct net_device *dev)
396 struct net_local *lp = (struct net_local *)dev->priv;
397 int ioaddr = dev->base_addr;
398 int reset_start_time;
400 writereg(dev, PP_SelfCTL, readreg(dev, PP_SelfCTL) | POWER_ON_RESET);
402 /* wait 30 ms */
403 current->state = TASK_INTERRUPTIBLE;
404 schedule_timeout(30*HZ/1000);
406 if (lp->chip_type != CS8900) {
407 /* Hardware problem requires PNP registers to be reconfigured after a reset */
408 outw(PP_CS8920_ISAINT, ioaddr + ADD_PORT);
409 outb(dev->irq, ioaddr + DATA_PORT);
410 outb(0, ioaddr + DATA_PORT + 1);
412 outw(PP_CS8920_ISAMemB, ioaddr + ADD_PORT);
413 outb((dev->mem_start >> 8) & 0xff, ioaddr + DATA_PORT);
414 outb((dev->mem_start >> 24) & 0xff, ioaddr + DATA_PORT + 1);
416 /* Wait until the chip is reset */
417 reset_start_time = jiffies;
418 while( (readreg(dev, PP_SelfST) & INIT_DONE) == 0 && jiffies - reset_start_time < 2)
423 static void
424 control_dc_dc(struct net_device *dev, int on_not_off)
426 struct net_local *lp = (struct net_local *)dev->priv;
427 unsigned int selfcontrol;
428 int timenow = jiffies;
429 /* control the DC to DC convertor in the SelfControl register. */
431 selfcontrol = HCB1_ENBL; /* Enable the HCB1 bit as an output */
432 if (((lp->adapter_cnf & A_CNF_DC_DC_POLARITY) != 0) ^ on_not_off)
433 selfcontrol |= HCB1;
434 else
435 selfcontrol &= ~HCB1;
436 writereg(dev, PP_SelfCTL, selfcontrol);
438 /* Wait for the DC/DC converter to power up - 500ms */
439 while (jiffies - timenow < 100)
444 static int
445 detect_tp(struct net_device *dev)
447 struct net_local *lp = (struct net_local *)dev->priv;
448 int timenow = jiffies;
450 if (net_debug > 1) printk("%s: Attempting TP\n", dev->name);
452 /* If connected to another full duplex capable 10-Base-T card the link pulses
453 seem to be lost when the auto detect bit in the LineCTL is set.
454 To overcome this the auto detect bit will be cleared whilst testing the
455 10-Base-T interface. This would not be necessary for the sparrow chip but
456 is simpler to do it anyway. */
457 writereg(dev, PP_LineCTL, lp->linectl &~ AUI_ONLY);
458 control_dc_dc(dev, 0);
460 /* Delay for the hardware to work out if the TP cable is present - 150ms */
461 for (timenow = jiffies; jiffies - timenow < 15; )
463 if ((readreg(dev, PP_LineST) & LINK_OK) == 0)
464 return 0;
466 if (lp->chip_type != CS8900) {
468 writereg(dev, PP_AutoNegCTL, lp->auto_neg_cnf & AUTO_NEG_MASK);
470 if ((lp->auto_neg_cnf & AUTO_NEG_BITS) == AUTO_NEG_ENABLE) {
471 printk("%s: negotiating duplex...\n",dev->name);
472 while (readreg(dev, PP_AutoNegST) & AUTO_NEG_BUSY) {
473 if (jiffies - timenow > 4000) {
474 printk("**** Full / half duplex auto-negotiation timed out ****\n");
475 break;
479 if (readreg(dev, PP_AutoNegST) & FDX_ACTIVE)
480 printk("%s: using full duplex\n", dev->name);
481 else
482 printk("%s: using half duplex\n", dev->name);
485 return A_CNF_MEDIA_10B_T;
488 /* send a test packet - return true if carrier bits are ok */
489 static int
490 send_test_pkt(struct net_device *dev)
492 int ioaddr = dev->base_addr;
493 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
494 0, 46, /* A 46 in network order */
495 0, 0, /* DSAP=0 & SSAP=0 fields */
496 0xf3, 0 /* Control (Test Req + P bit set) */ };
497 long timenow = jiffies;
499 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_TX_ON);
501 memcpy(test_packet, dev->dev_addr, ETH_ALEN);
502 memcpy(test_packet+ETH_ALEN, dev->dev_addr, ETH_ALEN);
504 outw(TX_AFTER_ALL, ioaddr + TX_CMD_PORT);
505 outw(ETH_ZLEN, ioaddr + TX_LEN_PORT);
507 /* Test to see if the chip has allocated memory for the packet */
508 while (jiffies - timenow < 5)
509 if (readreg(dev, PP_BusST) & READY_FOR_TX_NOW)
510 break;
511 if (jiffies - timenow >= 5)
512 return 0; /* this shouldn't happen */
514 /* Write the contents of the packet */
515 if (dev->mem_start) {
516 memcpy((void *)dev->mem_start + PP_TxFrame, test_packet, ETH_ZLEN);
517 } else {
518 outsw(ioaddr + TX_FRAME_PORT,test_packet,(ETH_ZLEN+1) >>1);
521 if (net_debug > 1) printk("Sending test packet ");
522 /* wait a couple of jiffies for packet to be received */
523 for (timenow = jiffies; jiffies - timenow < 3; )
525 if ((readreg(dev, PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
526 if (net_debug > 1) printk("succeeded\n");
527 return 1;
529 if (net_debug > 1) printk("failed\n");
530 return 0;
534 static int
535 detect_aui(struct net_device *dev)
537 struct net_local *lp = (struct net_local *)dev->priv;
539 if (net_debug > 1) printk("%s: Attempting AUI\n", dev->name);
540 control_dc_dc(dev, 0);
542 writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
544 if (send_test_pkt(dev))
545 return A_CNF_MEDIA_AUI;
546 else
547 return 0;
550 static int
551 detect_bnc(struct net_device *dev)
553 struct net_local *lp = (struct net_local *)dev->priv;
555 if (net_debug > 1) printk("%s: Attempting BNC\n", dev->name);
556 control_dc_dc(dev, 1);
558 writereg(dev, PP_LineCTL, (lp->linectl &~ AUTO_AUI_10BASET) | AUI_ONLY);
560 if (send_test_pkt(dev))
561 return A_CNF_MEDIA_10B_2;
562 else
563 return 0;
567 static void
568 write_irq(struct net_device *dev, int chip_type, int irq)
570 int i;
572 if (chip_type == CS8900) {
573 switch(irq) {
574 case 10: i = 0; break;
575 case 11: i = 1; break;
576 case 12: i = 2; break;
577 case 5: i = 3; break;
578 default: i = 3; break;
580 writereg(dev, PP_CS8900_ISAINT, i);
581 } else {
582 writereg(dev, PP_CS8920_ISAINT, irq);
586 /* Open/initialize the board. This is called (in the current kernel)
587 sometime after booting when the 'ifconfig' program is run.
589 This routine should set everything up anew at each open, even
590 registers that "should" only need to be set once at boot, so that
591 there is non-reboot way to recover if something goes wrong.
593 static int
594 net_open(struct net_device *dev)
596 struct net_local *lp = (struct net_local *)dev->priv;
597 int result = 0;
598 int i;
600 if (dev->irq < 2) {
601 /* Allow interrupts to be generated by the chip */
602 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
603 for (i = 2; i < CS8920_NO_INTS; i++) if ((1 << dev->irq) & lp->irq_map) {
604 if (request_irq (i, NULL, 0, "cs8920", dev) != -EBUSY) {
605 write_irq(dev, lp->chip_type, i);
606 writereg(dev, PP_BufCFG, GENERATE_SW_INTERRUPT);
607 if (request_irq (dev->irq = i, &net_interrupt, 0, "cs89x0", dev) == 0)
608 break;
613 if (i >= CS8920_NO_INTS) {
614 writereg(dev, PP_BusCTL, 0); /* disable interrupts. */
615 return -EAGAIN;
617 } else {
618 if (((1 << dev->irq) & lp->irq_map) == 0) {
619 printk("%s: IRQ %d is not in our map of allowable IRQs, which is %x\n",
620 dev->name, dev->irq, lp->irq_map);
621 return -EAGAIN;
623 writereg(dev, PP_BusCTL, ENABLE_IRQ | MEMORY_ON);
624 write_irq(dev, lp->chip_type, dev->irq);
625 if (request_irq(dev->irq, &net_interrupt, 0, "cs89x0", dev)) {
626 return -EAGAIN;
630 /* set the Ethernet address */
631 for (i=0; i < ETH_ALEN/2; i++)
632 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
634 /* while we're testing the interface, leave interrupts disabled */
635 writereg(dev, PP_BusCTL, MEMORY_ON);
637 /* Set the LineCTL quintuplet based on adapter configuration read from EEPROM */
638 if ((lp->adapter_cnf & A_CNF_EXTND_10B_2) && (lp->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
639 lp->linectl = LOW_RX_SQUELCH;
640 else
641 lp->linectl = 0;
643 /* check to make sure that they have the "right" hardware available */
644 switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
645 case A_CNF_MEDIA_10B_T: result = lp->adapter_cnf & A_CNF_10B_T; break;
646 case A_CNF_MEDIA_AUI: result = lp->adapter_cnf & A_CNF_AUI; break;
647 case A_CNF_MEDIA_10B_2: result = lp->adapter_cnf & A_CNF_10B_2; break;
648 default: result = lp->adapter_cnf & (A_CNF_10B_T | A_CNF_AUI | A_CNF_10B_2);
650 if (!result) {
651 printk("%s: EEPROM is configured for unavailable media\n", dev->name);
652 release_irq:
653 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) & ~(SERIAL_TX_ON | SERIAL_RX_ON));
654 free_irq(dev->irq, dev);
655 return -EAGAIN;
658 /* set the hardware to the configured choice */
659 switch(lp->adapter_cnf & A_CNF_MEDIA_TYPE) {
660 case A_CNF_MEDIA_10B_T:
661 result = detect_tp(dev);
662 if (!result) printk("%s: 10Base-T (RJ-45) has no cable\n", dev->name);
663 if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
664 result = A_CNF_MEDIA_10B_T; /* Yes! I don't care if I see a link pulse */
665 break;
666 case A_CNF_MEDIA_AUI:
667 result = detect_aui(dev);
668 if (!result) printk("%s: 10Base-5 (AUI) has no cable\n", dev->name);
669 if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
670 result = A_CNF_MEDIA_AUI; /* Yes! I don't care if I see a carrrier */
671 break;
672 case A_CNF_MEDIA_10B_2:
673 result = detect_bnc(dev);
674 if (!result) printk("%s: 10Base-2 (BNC) has no cable\n", dev->name);
675 if (lp->auto_neg_cnf & IMM_BIT) /* check "ignore missing media" bit */
676 result = A_CNF_MEDIA_10B_2; /* Yes! I don't care if I can xmit a packet */
677 break;
678 case A_CNF_MEDIA_AUTO:
679 writereg(dev, PP_LineCTL, lp->linectl | AUTO_AUI_10BASET);
680 if (lp->adapter_cnf & A_CNF_10B_T)
681 if ((result = detect_tp(dev)) != 0)
682 break;
683 if (lp->adapter_cnf & A_CNF_AUI)
684 if ((result = detect_aui(dev)) != 0)
685 break;
686 if (lp->adapter_cnf & A_CNF_10B_2)
687 if ((result = detect_bnc(dev)) != 0)
688 break;
689 printk("%s: no media detected\n", dev->name);
690 goto release_irq;
692 switch(result) {
693 case 0: printk("%s: no network cable attached to configured media\n", dev->name);
694 goto release_irq;
695 case A_CNF_MEDIA_10B_T: printk("%s: using 10Base-T (RJ-45)\n", dev->name);break;
696 case A_CNF_MEDIA_AUI: printk("%s: using 10Base-5 (AUI)\n", dev->name);break;
697 case A_CNF_MEDIA_10B_2: printk("%s: using 10Base-2 (BNC)\n", dev->name);break;
698 default: printk("%s: unexpected result was %x\n", dev->name, result); goto release_irq;
701 /* Turn on both receive and transmit operations */
702 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
704 /* Receive only error free packets addressed to this card */
705 lp->rx_mode = 0;
706 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
708 lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
709 if (lp->isa_config & STREAM_TRANSFER)
710 lp->curr_rx_cfg |= RX_STREAM_ENBL;
712 writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
714 writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
715 TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
717 writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
718 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
720 /* now that we've got our act together, enable everything */
721 writereg(dev, PP_BusCTL, ENABLE_IRQ
723 dev->tbusy = 0;
724 dev->interrupt = 0;
725 dev->start = 1;
726 MOD_INC_USE_COUNT;
727 return 0;
730 static int
731 net_send_packet(struct sk_buff *skb, struct net_device *dev)
733 if (dev->tbusy) {
734 /* If we get here, some higher level has decided we are broken.
735 There should really be a "kick me" function call instead. */
736 int tickssofar = jiffies - dev->trans_start;
737 if (tickssofar < 5)
738 return 1;
739 if (net_debug > 0) printk("%s: transmit timed out, %s?\n", dev->name,
740 tx_done(dev) ? "IRQ conflict ?" : "network cable problem");
741 /* Try to restart the adaptor. */
742 dev->tbusy=0;
743 dev->trans_start = jiffies;
746 /* Block a timer-based transmit from overlapping. This could better be
747 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
748 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0)
749 printk("%s: Transmitter access conflict.\n", dev->name);
750 else {
751 struct net_local *lp = (struct net_local *)dev->priv;
752 short ioaddr = dev->base_addr;
753 unsigned long flags;
755 if (net_debug > 3)printk("%s: sent %d byte packet of type %x\n", dev->name, skb->len, (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
757 /* keep the upload from being interrupted, since we
758 ask the chip to start transmitting before the
759 whole packet has been completely uploaded. */
760 save_flags(flags);
761 cli();
763 /* initiate a transmit sequence */
764 outw(lp->send_cmd, ioaddr + TX_CMD_PORT);
765 outw(skb->len, ioaddr + TX_LEN_PORT);
767 /* Test to see if the chip has allocated memory for the packet */
768 if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
769 /* Gasp! It hasn't. But that shouldn't happen since
770 we're waiting for TxOk, so return 1 and requeue this packet. */
771 restore_flags(flags);
772 return 1;
775 /* Write the contents of the packet */
776 outsw(ioaddr + TX_FRAME_PORT,skb->data,(skb->len+1) >>1);
778 restore_flags(flags);
779 dev->trans_start = jiffies;
781 dev_kfree_skb (skb);
783 return 0;
786 /* The typical workload of the driver:
787 Handle the network interface interrupts. */
788 static void net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
790 struct net_device *dev = dev_id;
791 struct net_local *lp;
792 int ioaddr, status;
794 if (dev == NULL) {
795 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
796 return;
798 if (dev->interrupt)
799 printk("%s: Re-entering the interrupt handler.\n", dev->name);
800 dev->interrupt = 1;
802 ioaddr = dev->base_addr;
803 lp = (struct net_local *)dev->priv;
805 /* we MUST read all the events out of the ISQ, otherwise we'll never
806 get interrupted again. As a consequence, we can't have any limit
807 on the number of times we loop in the interrupt handler. The
808 hardware guarantees that eventually we'll run out of events. Of
809 course, if you're on a slow machine, and packets are arriving
810 faster than you can read them off, you're screwed. Hasta la
811 vista, baby! */
812 while ((status = readword(dev, ISQ_PORT))) {
813 if (net_debug > 4)printk("%s: event=%04x\n", dev->name, status);
814 switch(status & ISQ_EVENT_MASK) {
815 case ISQ_RECEIVER_EVENT:
816 /* Got a packet(s). */
817 net_rx(dev);
818 break;
819 case ISQ_TRANSMITTER_EVENT:
820 lp->stats.tx_packets++;
821 dev->tbusy = 0;
822 mark_bh(NET_BH); /* Inform upper layers. */
823 if ((status & TX_OK) == 0) lp->stats.tx_errors++;
824 if (status & TX_LOST_CRS) lp->stats.tx_carrier_errors++;
825 if (status & TX_SQE_ERROR) lp->stats.tx_heartbeat_errors++;
826 if (status & TX_LATE_COL) lp->stats.tx_window_errors++;
827 if (status & TX_16_COL) lp->stats.tx_aborted_errors++;
828 break;
829 case ISQ_BUFFER_EVENT:
830 if (status & READY_FOR_TX) {
831 /* we tried to transmit a packet earlier,
832 but inexplicably ran out of buffers.
833 That shouldn't happen since we only ever
834 load one packet. Shrug. Do the right
835 thing anyway. */
836 dev->tbusy = 0;
837 mark_bh(NET_BH); /* Inform upper layers. */
839 if (status & TX_UNDERRUN) {
840 if (net_debug > 0) printk("%s: transmit underrun\n", dev->name);
841 lp->send_underrun++;
842 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
843 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
844 /* transmit cycle is done, although
845 frame wasn't transmitted - this
846 avoids having to wait for the upper
847 layers to timeout on us, in the
848 event of a tx underrun */
849 dev->tbusy = 0;
850 mark_bh(NET_BH); /* Inform upper layers. */
852 break;
853 case ISQ_RX_MISS_EVENT:
854 lp->stats.rx_missed_errors += (status >>6);
855 break;
856 case ISQ_TX_COL_EVENT:
857 lp->stats.collisions += (status >>6);
858 break;
861 dev->interrupt = 0;
862 return;
865 /* We have a good packet(s), get it/them out of the buffers. */
866 static void
867 net_rx(struct net_device *dev)
869 struct net_local *lp = (struct net_local *)dev->priv;
870 int ioaddr = dev->base_addr;
871 struct sk_buff *skb;
872 int status, length;
874 status = inw(ioaddr + RX_FRAME_PORT);
875 length = inw(ioaddr + RX_FRAME_PORT);
876 if ((status & RX_OK) == 0) {
877 lp->stats.rx_errors++;
878 if (status & RX_RUNT) lp->stats.rx_length_errors++;
879 if (status & RX_EXTRA_DATA) lp->stats.rx_length_errors++;
880 if (status & RX_CRC_ERROR) if (!(status & (RX_EXTRA_DATA|RX_RUNT)))
881 /* per str 172 */
882 lp->stats.rx_crc_errors++;
883 if (status & RX_DRIBBLE) lp->stats.rx_frame_errors++;
884 return;
887 /* Malloc up new buffer. */
888 skb = alloc_skb(length, GFP_ATOMIC);
889 if (skb == NULL) {
890 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
891 lp->stats.rx_dropped++;
892 return;
894 skb->len = length;
895 skb->dev = dev;
897 insw(ioaddr + RX_FRAME_PORT, skb->data, length >> 1);
898 if (length & 1)
899 skb->data[length-1] = inw(ioaddr + RX_FRAME_PORT);
901 if (net_debug > 3)printk("%s: received %d byte packet of type %x\n",
902 dev->name, length,
903 (skb->data[ETH_ALEN+ETH_ALEN] << 8) | skb->data[ETH_ALEN+ETH_ALEN+1]);
905 skb->protocol=eth_type_trans(skb,dev);
906 netif_rx(skb);
907 lp->stats.rx_packets++;
908 lp->stats.rx_bytes+=skb->len;
909 return;
912 /* The inverse routine to net_open(). */
913 static int
914 net_close(struct net_device *dev)
917 writereg(dev, PP_RxCFG, 0);
918 writereg(dev, PP_TxCFG, 0);
919 writereg(dev, PP_BufCFG, 0);
920 writereg(dev, PP_BusCTL, 0);
922 dev->start = 0;
924 free_irq(dev->irq, dev);
926 /* Update the statistics here. */
928 MOD_DEC_USE_COUNT;
929 return 0;
933 /* Get the current statistics. This may be called with the card open or
934 closed. */
935 static struct net_device_stats *
936 net_get_stats(struct net_device *dev)
938 struct net_local *lp = (struct net_local *)dev->priv;
940 cli();
941 /* Update the statistics from the device registers. */
942 lp->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
943 lp->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
944 sti();
946 return &lp->stats;
949 static void set_multicast_list(struct net_device *dev)
951 struct net_local *lp = (struct net_local *)dev->priv;
953 if(dev->flags&IFF_PROMISC)
955 lp->rx_mode = RX_ALL_ACCEPT;
957 else if((dev->flags&IFF_ALLMULTI)||dev->mc_list)
959 /* The multicast-accept list is initialized to accept-all, and we
960 rely on higher-level filtering for now. */
961 lp->rx_mode = RX_MULTCAST_ACCEPT;
963 else
964 lp->rx_mode = 0;
966 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
968 /* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
969 writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
970 (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
974 static int set_mac_address(struct net_device *dev, void *addr)
976 int i;
977 if (dev->start)
978 return -EBUSY;
979 printk("%s: Setting MAC address to ", dev->name);
980 for (i = 0; i < 6; i++)
981 printk(" %2.2x", dev->dev_addr[i] = ((unsigned char *)addr)[i]);
982 printk(".\n");
983 /* set the Ethernet address */
984 for (i=0; i < ETH_ALEN/2; i++)
985 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
987 return 0;
990 #ifdef MODULE
992 static char namespace[16] = "";
993 static struct net_device dev_cs89x0 = {
994 NULL,
995 0, 0, 0, 0,
996 0, 0,
997 0, 0, 0, NULL, NULL };
999 static int io=0;
1000 static int irq=0;
1001 static int debug=0;
1002 static char media[8];
1003 static int duplex=-1;
1005 MODULE_PARM(io, "i");
1006 MODULE_PARM(irq, "i");
1007 MODULE_PARM(debug, "i");
1008 MODULE_PARM(media, "s");
1009 MODULE_PARM(duplex, "i");
1011 EXPORT_NO_SYMBOLS;
1014 * media=t - specify media type
1015 or media=2
1016 or media=aui
1017 or medai=auto
1018 * duplex=0 - specify forced half/full/autonegotiate duplex
1019 * debug=# - debug level
1022 * Default Chip Configuration:
1023 * DMA Burst = enabled
1024 * IOCHRDY Enabled = enabled
1025 * UseSA = enabled
1026 * CS8900 defaults to half-duplex if not specified on command-line
1027 * CS8920 defaults to autoneg if not specified on command-line
1028 * Use reset defaults for other config parameters
1030 * Assumptions:
1031 * media type specified is supported (circuitry is present)
1032 * if memory address is > 1MB, then required mem decode hw is present
1033 * if 10B-2, then agent other than driver will enable DC/DC converter
1034 (hw or software util)
1040 init_module(void)
1042 struct net_local *lp;
1044 net_debug = debug;
1045 dev_cs89x0.name = namespace;
1046 dev_cs89x0.irq = irq;
1047 dev_cs89x0.base_addr = io;
1048 dev_cs89x0.init = cs89x0_probe;
1049 dev_cs89x0.priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
1050 memset(dev_cs89x0.priv, 0, sizeof(struct net_local));
1051 lp = (struct net_local *)dev_cs89x0.priv;
1053 /* boy, they'd better get these right */
1054 if (!strcmp(media, "rj45"))
1055 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1056 else if (!strcmp(media, "aui"))
1057 lp->adapter_cnf = A_CNF_MEDIA_AUI | A_CNF_AUI;
1058 else if (!strcmp(media, "bnc"))
1059 lp->adapter_cnf = A_CNF_MEDIA_10B_2 | A_CNF_10B_2;
1060 else
1061 lp->adapter_cnf = A_CNF_MEDIA_10B_T | A_CNF_10B_T;
1063 if (duplex==-1)
1064 lp->auto_neg_cnf = AUTO_NEG_ENABLE;
1066 if (io == 0) {
1067 printk(KERN_NOTICE "cs89x0.c: Module autoprobing not allowed.\n");
1068 printk(KERN_NOTICE "cs89x0.c: Append io=0xNNN\n");
1069 return -EPERM;
1071 if (register_netdev(&dev_cs89x0) != 0) {
1072 printk(KERN_WARNING "cs89x0.c: No card found at 0x%x\n", io);
1073 return -ENXIO;
1075 return 0;
1078 void
1079 cleanup_module(void)
1082 #endif
1083 #ifdef MODULE
1084 outw(0, dev_cs89x0.base_addr + ADD_PORT);
1085 #endif
1086 #ifdef MODULE
1088 if (dev_cs89x0.priv != NULL) {
1089 /* Free up the private structure, or leak memory :-) */
1090 unregister_netdev(&dev_cs89x0);
1091 kfree(dev_cs89x0.priv);
1092 dev_cs89x0.priv = NULL; /* gets re-allocated by cs89x0_probe1 */
1093 /* If we don't do this, we can't re-insmod it later. */
1094 release_region(dev_cs89x0.base_addr, NETCARD_IO_EXTENT);
1097 #endif /* MODULE */
1100 * Local variables:
1101 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -DMODULE -DCONFIG_MODVERSIONS -c cs89x0.c"
1102 * version-control: t
1103 * kept-new-versions: 5
1104 * c-indent-level: 8
1105 * tab-width: 8
1106 * End: