Full support for Ginger Console
[linux-ginger.git] / drivers / net / atp.c
blob9043294fe617b6ac130cf40469524eef934a9d44
1 /* atp.c: Attached (pocket) ethernet adapter driver for linux. */
2 /*
3 This is a driver for commonly OEM pocket (parallel port)
4 ethernet adapters based on the Realtek RTL8002 and RTL8012 chips.
6 Written 1993-2000 by Donald Becker.
8 This software may be used and distributed according to the terms of
9 the GNU General Public License (GPL), incorporated herein by reference.
10 Drivers based on or derived from this code fall under the GPL and must
11 retain the authorship, copyright and license notice. This file is not
12 a complete program and may only be used when the entire operating
13 system is licensed under the GPL.
15 Copyright 1993 United States Government as represented by the Director,
16 National Security Agency. Copyright 1994-2000 retained by the original
17 author, Donald Becker. The timer-based reset code was supplied in 1995
18 by Bill Carlson, wwc@super.org.
20 The author may be reached as becker@scyld.com, or C/O
21 Scyld Computing Corporation
22 410 Severn Ave., Suite 210
23 Annapolis MD 21403
25 Support information and updates available at
26 http://www.scyld.com/network/atp.html
29 Modular support/softnet added by Alan Cox.
30 _bit abuse fixed up by Alan Cox
34 static const char version[] =
35 "atp.c:v1.09=ac 2002/10/01 Donald Becker <becker@scyld.com>\n";
37 /* The user-configurable values.
38 These may be modified when a driver module is loaded.*/
40 static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */
41 #define net_debug debug
43 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
44 static int max_interrupt_work = 15;
46 #define NUM_UNITS 2
47 /* The standard set of ISA module parameters. */
48 static int io[NUM_UNITS];
49 static int irq[NUM_UNITS];
50 static int xcvr[NUM_UNITS]; /* The data transfer mode. */
52 /* Operational parameters that are set at compile time. */
54 /* Time in jiffies before concluding the transmitter is hung. */
55 #define TX_TIMEOUT (400*HZ/1000)
58 This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
59 ethernet adapter. This is a common low-cost OEM pocket ethernet
60 adapter, sold under many names.
62 Sources:
63 This driver was written from the packet driver assembly code provided by
64 Vincent Bono of AT-Lan-Tec. Ever try to figure out how a complicated
65 device works just from the assembly code? It ain't pretty. The following
66 description is written based on guesses and writing lots of special-purpose
67 code to test my theorized operation.
69 In 1997 Realtek made available the documentation for the second generation
70 RTL8012 chip, which has lead to several driver improvements.
71 http://www.realtek.com.tw/cn/cn.html
73 Theory of Operation
75 The RTL8002 adapter seems to be built around a custom spin of the SEEQ
76 controller core. It probably has a 16K or 64K internal packet buffer, of
77 which the first 4K is devoted to transmit and the rest to receive.
78 The controller maintains the queue of received packet and the packet buffer
79 access pointer internally, with only 'reset to beginning' and 'skip to next
80 packet' commands visible. The transmit packet queue holds two (or more?)
81 packets: both 'retransmit this packet' (due to collision) and 'transmit next
82 packet' commands must be started by hand.
84 The station address is stored in a standard bit-serial EEPROM which must be
85 read (ughh) by the device driver. (Provisions have been made for
86 substituting a 74S288 PROM, but I haven't gotten reports of any models
87 using it.) Unlike built-in devices, a pocket adapter can temporarily lose
88 power without indication to the device driver. The major effect is that
89 the station address, receive filter (promiscuous, etc.) and transceiver
90 must be reset.
92 The controller itself has 16 registers, some of which use only the lower
93 bits. The registers are read and written 4 bits at a time. The four bit
94 register address is presented on the data lines along with a few additional
95 timing and control bits. The data is then read from status port or written
96 to the data port.
98 Correction: the controller has two banks of 16 registers. The second
99 bank contains only the multicast filter table (now used) and the EEPROM
100 access registers.
102 Since the bulk data transfer of the actual packets through the slow
103 parallel port dominates the driver's running time, four distinct data
104 (non-register) transfer modes are provided by the adapter, two in each
105 direction. In the first mode timing for the nibble transfers is
106 provided through the data port. In the second mode the same timing is
107 provided through the control port. In either case the data is read from
108 the status port and written to the data port, just as it is accessing
109 registers.
111 In addition to the basic data transfer methods, several more are modes are
112 created by adding some delay by doing multiple reads of the data to allow
113 it to stabilize. This delay seems to be needed on most machines.
115 The data transfer mode is stored in the 'dev->if_port' field. Its default
116 value is '4'. It may be overridden at boot-time using the third parameter
117 to the "ether=..." initialization.
119 The header file <atp.h> provides inline functions that encapsulate the
120 register and data access methods. These functions are hand-tuned to
121 generate reasonable object code. This header file also documents my
122 interpretations of the device registers.
125 #include <linux/kernel.h>
126 #include <linux/module.h>
127 #include <linux/types.h>
128 #include <linux/fcntl.h>
129 #include <linux/interrupt.h>
130 #include <linux/ioport.h>
131 #include <linux/in.h>
132 #include <linux/slab.h>
133 #include <linux/string.h>
134 #include <linux/errno.h>
135 #include <linux/init.h>
136 #include <linux/crc32.h>
137 #include <linux/netdevice.h>
138 #include <linux/etherdevice.h>
139 #include <linux/skbuff.h>
140 #include <linux/spinlock.h>
141 #include <linux/delay.h>
142 #include <linux/bitops.h>
144 #include <asm/system.h>
145 #include <asm/io.h>
146 #include <asm/dma.h>
148 #include "atp.h"
150 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
151 MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
152 MODULE_LICENSE("GPL");
154 module_param(max_interrupt_work, int, 0);
155 module_param(debug, int, 0);
156 module_param_array(io, int, NULL, 0);
157 module_param_array(irq, int, NULL, 0);
158 module_param_array(xcvr, int, NULL, 0);
159 MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
160 MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
161 MODULE_PARM_DESC(io, "ATP I/O base address(es)");
162 MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
163 MODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)");
165 /* The number of low I/O ports used by the ethercard. */
166 #define ETHERCARD_TOTAL_SIZE 3
168 /* Sequence to switch an 8012 from printer mux to ethernet mode. */
169 static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
171 struct net_local {
172 spinlock_t lock;
173 struct net_device *next_module;
174 struct timer_list timer; /* Media selection timer. */
175 long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */
176 int saved_tx_size;
177 unsigned int tx_unit_busy:1;
178 unsigned char re_tx, /* Number of packet retransmissions. */
179 addr_mode, /* Current Rx filter e.g. promiscuous, etc. */
180 pac_cnt_in_tx_buf,
181 chip_type;
184 /* This code, written by wwc@super.org, resets the adapter every
185 TIMED_CHECKER ticks. This recovers from an unknown error which
186 hangs the device. */
187 #define TIMED_CHECKER (HZ/4)
188 #ifdef TIMED_CHECKER
189 #include <linux/timer.h>
190 static void atp_timed_checker(unsigned long ignored);
191 #endif
193 /* Index to functions, as function prototypes. */
195 static int atp_probe1(long ioaddr);
196 static void get_node_ID(struct net_device *dev);
197 static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
198 static int net_open(struct net_device *dev);
199 static void hardware_init(struct net_device *dev);
200 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
201 static void trigger_send(long ioaddr, int length);
202 static netdev_tx_t atp_send_packet(struct sk_buff *skb,
203 struct net_device *dev);
204 static irqreturn_t atp_interrupt(int irq, void *dev_id);
205 static void net_rx(struct net_device *dev);
206 static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
207 static int net_close(struct net_device *dev);
208 static void set_rx_mode(struct net_device *dev);
209 static void tx_timeout(struct net_device *dev);
212 /* A list of all installed ATP devices, for removing the driver module. */
213 static struct net_device *root_atp_dev;
215 /* Check for a network adapter of this type, and return '0' iff one exists.
216 If dev->base_addr == 0, probe all likely locations.
217 If dev->base_addr == 1, always return failure.
218 If dev->base_addr == 2, allocate space for the device and return success
219 (detachable devices only).
221 FIXME: we should use the parport layer for this
223 static int __init atp_init(void)
225 int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
226 int base_addr = io[0];
228 if (base_addr > 0x1ff) /* Check a single specified location. */
229 return atp_probe1(base_addr);
230 else if (base_addr == 1) /* Don't probe at all. */
231 return -ENXIO;
233 for (port = ports; *port; port++) {
234 long ioaddr = *port;
235 outb(0x57, ioaddr + PAR_DATA);
236 if (inb(ioaddr + PAR_DATA) != 0x57)
237 continue;
238 if (atp_probe1(ioaddr) == 0)
239 return 0;
242 return -ENODEV;
245 static const struct net_device_ops atp_netdev_ops = {
246 .ndo_open = net_open,
247 .ndo_stop = net_close,
248 .ndo_start_xmit = atp_send_packet,
249 .ndo_set_multicast_list = set_rx_mode,
250 .ndo_tx_timeout = tx_timeout,
251 .ndo_change_mtu = eth_change_mtu,
252 .ndo_set_mac_address = eth_mac_addr,
253 .ndo_validate_addr = eth_validate_addr,
256 static int __init atp_probe1(long ioaddr)
258 struct net_device *dev = NULL;
259 struct net_local *lp;
260 int saved_ctrl_reg, status, i;
261 int res;
263 outb(0xff, ioaddr + PAR_DATA);
264 /* Save the original value of the Control register, in case we guessed
265 wrong. */
266 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
267 if (net_debug > 3)
268 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
269 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
270 outb(0x04, ioaddr + PAR_CONTROL);
271 #ifndef final_version
272 if (net_debug > 3) {
273 /* Turn off the printer multiplexer on the 8012. */
274 for (i = 0; i < 8; i++)
275 outb(mux_8012[i], ioaddr + PAR_DATA);
276 write_reg(ioaddr, MODSEL, 0x00);
277 printk("atp: Registers are ");
278 for (i = 0; i < 32; i++)
279 printk(" %2.2x", read_nibble(ioaddr, i));
280 printk(".\n");
282 #endif
283 /* Turn off the printer multiplexer on the 8012. */
284 for (i = 0; i < 8; i++)
285 outb(mux_8012[i], ioaddr + PAR_DATA);
286 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
287 /* udelay() here? */
288 status = read_nibble(ioaddr, CMR1);
290 if (net_debug > 3) {
291 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
292 for (i = 0; i < 32; i++)
293 printk(" %2.2x", read_nibble(ioaddr, i));
294 printk("\n");
297 if ((status & 0x78) != 0x08) {
298 /* The pocket adapter probe failed, restore the control register. */
299 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
300 return -ENODEV;
302 status = read_nibble(ioaddr, CMR2_h);
303 if ((status & 0x78) != 0x10) {
304 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
305 return -ENODEV;
308 dev = alloc_etherdev(sizeof(struct net_local));
309 if (!dev)
310 return -ENOMEM;
312 /* Find the IRQ used by triggering an interrupt. */
313 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
314 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
316 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
317 if (irq[0])
318 dev->irq = irq[0];
319 else if (ioaddr == 0x378)
320 dev->irq = 7;
321 else
322 dev->irq = 5;
323 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
324 write_reg(ioaddr, CMR2, CMR2_NULL);
326 dev->base_addr = ioaddr;
328 /* Read the station address PROM. */
329 get_node_ID(dev);
331 #ifndef MODULE
332 if (net_debug)
333 printk(KERN_INFO "%s", version);
334 #endif
336 printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
337 "SAPROM %pM.\n",
338 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
340 /* Reset the ethernet hardware and activate the printer pass-through. */
341 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
343 lp = netdev_priv(dev);
344 lp->chip_type = RTL8002;
345 lp->addr_mode = CMR2h_Normal;
346 spin_lock_init(&lp->lock);
348 /* For the ATP adapter the "if_port" is really the data transfer mode. */
349 if (xcvr[0])
350 dev->if_port = xcvr[0];
351 else
352 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
353 if (dev->mem_end & 0xf)
354 net_debug = dev->mem_end & 7;
356 dev->netdev_ops = &atp_netdev_ops;
357 dev->watchdog_timeo = TX_TIMEOUT;
359 res = register_netdev(dev);
360 if (res) {
361 free_netdev(dev);
362 return res;
365 lp->next_module = root_atp_dev;
366 root_atp_dev = dev;
368 return 0;
371 /* Read the station address PROM, usually a word-wide EEPROM. */
372 static void __init get_node_ID(struct net_device *dev)
374 long ioaddr = dev->base_addr;
375 int sa_offset = 0;
376 int i;
378 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
380 /* Some adapters have the station address at offset 15 instead of offset
381 zero. Check for it, and fix it if needed. */
382 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
383 sa_offset = 15;
385 for (i = 0; i < 3; i++)
386 ((__be16 *)dev->dev_addr)[i] =
387 cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
389 write_reg(ioaddr, CMR2, CMR2_NULL);
393 An EEPROM read command starts by shifting out 0x60+address, and then
394 shifting in the serial data. See the NatSemi databook for details.
395 * ________________
396 * CS : __|
397 * ___ ___
398 * CLK: ______| |___| |
399 * __ _______ _______
400 * DI : __X_______X_______X
401 * DO : _________X_______X
404 static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
406 unsigned eedata_out = 0;
407 int num_bits = EE_CMD_SIZE;
409 while (--num_bits >= 0) {
410 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
411 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
412 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
413 eedata_out <<= 1;
414 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
415 eedata_out++;
417 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
418 return eedata_out;
422 /* Open/initialize the board. This is called (in the current kernel)
423 sometime after booting when the 'ifconfig' program is run.
425 This routine sets everything up anew at each open, even
426 registers that "should" only need to be set once at boot, so that
427 there is non-reboot way to recover if something goes wrong.
429 This is an attachable device: if there is no private entry then it wasn't
430 probed for at boot-time, and we need to probe for it again.
432 static int net_open(struct net_device *dev)
434 struct net_local *lp = netdev_priv(dev);
435 int ret;
437 /* The interrupt line is turned off (tri-stated) when the device isn't in
438 use. That's especially important for "attached" interfaces where the
439 port or interrupt may be shared. */
440 ret = request_irq(dev->irq, &atp_interrupt, 0, dev->name, dev);
441 if (ret)
442 return ret;
444 hardware_init(dev);
446 init_timer(&lp->timer);
447 lp->timer.expires = jiffies + TIMED_CHECKER;
448 lp->timer.data = (unsigned long)dev;
449 lp->timer.function = &atp_timed_checker; /* timer handler */
450 add_timer(&lp->timer);
452 netif_start_queue(dev);
453 return 0;
456 /* This routine resets the hardware. We initialize everything, assuming that
457 the hardware may have been temporarily detached. */
458 static void hardware_init(struct net_device *dev)
460 struct net_local *lp = netdev_priv(dev);
461 long ioaddr = dev->base_addr;
462 int i;
464 /* Turn off the printer multiplexer on the 8012. */
465 for (i = 0; i < 8; i++)
466 outb(mux_8012[i], ioaddr + PAR_DATA);
467 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
469 for (i = 0; i < 6; i++)
470 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
472 write_reg_high(ioaddr, CMR2, lp->addr_mode);
474 if (net_debug > 2) {
475 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
476 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
479 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
480 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
482 /* Enable the interrupt line from the serial port. */
483 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
485 /* Unmask the interesting interrupts. */
486 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
487 write_reg_high(ioaddr, IMR, ISRh_RxErr);
489 lp->tx_unit_busy = 0;
490 lp->pac_cnt_in_tx_buf = 0;
491 lp->saved_tx_size = 0;
494 static void trigger_send(long ioaddr, int length)
496 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
497 write_reg(ioaddr, TxCNT1, length >> 8);
498 write_reg(ioaddr, CMR1, CMR1_Xmit);
501 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
503 if (length & 1)
505 length++;
506 pad_len++;
509 outb(EOC+MAR, ioaddr + PAR_DATA);
510 if ((data_mode & 1) == 0) {
511 /* Write the packet out, starting with the write addr. */
512 outb(WrAddr+MAR, ioaddr + PAR_DATA);
513 do {
514 write_byte_mode0(ioaddr, *packet++);
515 } while (--length > pad_len) ;
516 do {
517 write_byte_mode0(ioaddr, 0);
518 } while (--length > 0) ;
519 } else {
520 /* Write the packet out in slow mode. */
521 unsigned char outbyte = *packet++;
523 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
524 outb(WrAddr+MAR, ioaddr + PAR_DATA);
526 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
527 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
528 outbyte >>= 4;
529 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
530 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
531 while (--length > pad_len)
532 write_byte_mode1(ioaddr, *packet++);
533 while (--length > 0)
534 write_byte_mode1(ioaddr, 0);
536 /* Terminate the Tx frame. End of write: ECB. */
537 outb(0xff, ioaddr + PAR_DATA);
538 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
541 static void tx_timeout(struct net_device *dev)
543 long ioaddr = dev->base_addr;
545 printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
546 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
547 : "IRQ conflict");
548 dev->stats.tx_errors++;
549 /* Try to restart the adapter. */
550 hardware_init(dev);
551 dev->trans_start = jiffies;
552 netif_wake_queue(dev);
553 dev->stats.tx_errors++;
556 static netdev_tx_t atp_send_packet(struct sk_buff *skb,
557 struct net_device *dev)
559 struct net_local *lp = netdev_priv(dev);
560 long ioaddr = dev->base_addr;
561 int length;
562 unsigned long flags;
564 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
566 netif_stop_queue(dev);
568 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
569 This sequence must not be interrupted by an incoming packet. */
571 spin_lock_irqsave(&lp->lock, flags);
572 write_reg(ioaddr, IMR, 0);
573 write_reg_high(ioaddr, IMR, 0);
574 spin_unlock_irqrestore(&lp->lock, flags);
576 write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
578 lp->pac_cnt_in_tx_buf++;
579 if (lp->tx_unit_busy == 0) {
580 trigger_send(ioaddr, length);
581 lp->saved_tx_size = 0; /* Redundant */
582 lp->re_tx = 0;
583 lp->tx_unit_busy = 1;
584 } else
585 lp->saved_tx_size = length;
586 /* Re-enable the LPT interrupts. */
587 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
588 write_reg_high(ioaddr, IMR, ISRh_RxErr);
590 dev->trans_start = jiffies;
591 dev_kfree_skb (skb);
592 return NETDEV_TX_OK;
596 /* The typical workload of the driver:
597 Handle the network interface interrupts. */
598 static irqreturn_t atp_interrupt(int irq, void *dev_instance)
600 struct net_device *dev = dev_instance;
601 struct net_local *lp;
602 long ioaddr;
603 static int num_tx_since_rx;
604 int boguscount = max_interrupt_work;
605 int handled = 0;
607 ioaddr = dev->base_addr;
608 lp = netdev_priv(dev);
610 spin_lock(&lp->lock);
612 /* Disable additional spurious interrupts. */
613 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
615 /* The adapter's output is currently the IRQ line, switch it to data. */
616 write_reg(ioaddr, CMR2, CMR2_NULL);
617 write_reg(ioaddr, IMR, 0);
619 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
620 while (--boguscount > 0) {
621 int status = read_nibble(ioaddr, ISR);
622 if (net_debug > 5) printk("loop status %02x..", status);
624 if (status & (ISR_RxOK<<3)) {
625 handled = 1;
626 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
627 do {
628 int read_status = read_nibble(ioaddr, CMR1);
629 if (net_debug > 6)
630 printk("handling Rx packet %02x..", read_status);
631 /* We acknowledged the normal Rx interrupt, so if the interrupt
632 is still outstanding we must have a Rx error. */
633 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
634 dev->stats.rx_over_errors++;
635 /* Set to no-accept mode long enough to remove a packet. */
636 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
637 net_rx(dev);
638 /* Clear the interrupt and return to normal Rx mode. */
639 write_reg_high(ioaddr, ISR, ISRh_RxErr);
640 write_reg_high(ioaddr, CMR2, lp->addr_mode);
641 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
642 net_rx(dev);
643 num_tx_since_rx = 0;
644 } else
645 break;
646 } while (--boguscount > 0);
647 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
648 handled = 1;
649 if (net_debug > 6) printk("handling Tx done..");
650 /* Clear the Tx interrupt. We should check for too many failures
651 and reinitialize the adapter. */
652 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
653 if (status & (ISR_TxErr<<3)) {
654 dev->stats.collisions++;
655 if (++lp->re_tx > 15) {
656 dev->stats.tx_aborted_errors++;
657 hardware_init(dev);
658 break;
660 /* Attempt to retransmit. */
661 if (net_debug > 6) printk("attempting to ReTx");
662 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
663 } else {
664 /* Finish up the transmit. */
665 dev->stats.tx_packets++;
666 lp->pac_cnt_in_tx_buf--;
667 if ( lp->saved_tx_size) {
668 trigger_send(ioaddr, lp->saved_tx_size);
669 lp->saved_tx_size = 0;
670 lp->re_tx = 0;
671 } else
672 lp->tx_unit_busy = 0;
673 netif_wake_queue(dev); /* Inform upper layers. */
675 num_tx_since_rx++;
676 } else if (num_tx_since_rx > 8
677 && time_after(jiffies, dev->last_rx + HZ)) {
678 if (net_debug > 2)
679 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
680 "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
681 num_tx_since_rx, jiffies - dev->last_rx, status,
682 (read_nibble(ioaddr, CMR1) >> 3) & 15);
683 dev->stats.rx_missed_errors++;
684 hardware_init(dev);
685 num_tx_since_rx = 0;
686 break;
687 } else
688 break;
691 /* This following code fixes a rare (and very difficult to track down)
692 problem where the adapter forgets its ethernet address. */
694 int i;
695 for (i = 0; i < 6; i++)
696 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
697 #if 0 && defined(TIMED_CHECKER)
698 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
699 #endif
702 /* Tell the adapter that it can go back to using the output line as IRQ. */
703 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
704 /* Enable the physical interrupt line, which is sure to be low until.. */
705 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
706 /* .. we enable the interrupt sources. */
707 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
708 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
710 spin_unlock(&lp->lock);
712 if (net_debug > 5) printk("exiting interrupt.\n");
713 return IRQ_RETVAL(handled);
716 #ifdef TIMED_CHECKER
717 /* This following code fixes a rare (and very difficult to track down)
718 problem where the adapter forgets its ethernet address. */
719 static void atp_timed_checker(unsigned long data)
721 struct net_device *dev = (struct net_device *)data;
722 long ioaddr = dev->base_addr;
723 struct net_local *lp = netdev_priv(dev);
724 int tickssofar = jiffies - lp->last_rx_time;
725 int i;
727 spin_lock(&lp->lock);
728 if (tickssofar > 2*HZ) {
729 #if 1
730 for (i = 0; i < 6; i++)
731 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
732 lp->last_rx_time = jiffies;
733 #else
734 for (i = 0; i < 6; i++)
735 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
737 struct net_local *lp = netdev_priv(atp_timed_dev);
738 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
739 if (i == 2)
740 dev->stats.tx_errors++;
741 else if (i == 3)
742 dev->stats.tx_dropped++;
743 else if (i == 4)
744 dev->stats.collisions++;
745 else
746 dev->stats.rx_errors++;
748 #endif
750 spin_unlock(&lp->lock);
751 lp->timer.expires = jiffies + TIMED_CHECKER;
752 add_timer(&lp->timer);
754 #endif
756 /* We have a good packet(s), get it/them out of the buffers. */
757 static void net_rx(struct net_device *dev)
759 struct net_local *lp = netdev_priv(dev);
760 long ioaddr = dev->base_addr;
761 struct rx_header rx_head;
763 /* Process the received packet. */
764 outb(EOC+MAR, ioaddr + PAR_DATA);
765 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
766 if (net_debug > 5)
767 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
768 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
769 if ((rx_head.rx_status & 0x77) != 0x01) {
770 dev->stats.rx_errors++;
771 if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
772 else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
773 if (net_debug > 3)
774 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
775 dev->name, rx_head.rx_status);
776 if (rx_head.rx_status & 0x0020) {
777 dev->stats.rx_fifo_errors++;
778 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
779 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
780 } else if (rx_head.rx_status & 0x0050)
781 hardware_init(dev);
782 return;
783 } else {
784 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
785 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
786 struct sk_buff *skb;
788 skb = dev_alloc_skb(pkt_len + 2);
789 if (skb == NULL) {
790 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
791 dev->name);
792 dev->stats.rx_dropped++;
793 goto done;
796 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
797 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
798 skb->protocol = eth_type_trans(skb, dev);
799 netif_rx(skb);
800 dev->last_rx = jiffies;
801 dev->stats.rx_packets++;
802 dev->stats.rx_bytes += pkt_len;
804 done:
805 write_reg(ioaddr, CMR1, CMR1_NextPkt);
806 lp->last_rx_time = jiffies;
807 return;
810 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
812 if (data_mode <= 3) { /* Mode 0 or 1 */
813 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
814 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
815 ioaddr + PAR_DATA);
816 if (data_mode <= 1) { /* Mode 0 or 1 */
817 do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0);
818 } else { /* Mode 2 or 3 */
819 do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0);
821 } else if (data_mode <= 5) {
822 do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0);
823 } else {
824 do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0);
827 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
828 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
831 /* The inverse routine to net_open(). */
832 static int
833 net_close(struct net_device *dev)
835 struct net_local *lp = netdev_priv(dev);
836 long ioaddr = dev->base_addr;
838 netif_stop_queue(dev);
840 del_timer_sync(&lp->timer);
842 /* Flush the Tx and disable Rx here. */
843 lp->addr_mode = CMR2h_OFF;
844 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
846 /* Free the IRQ line. */
847 outb(0x00, ioaddr + PAR_CONTROL);
848 free_irq(dev->irq, dev);
850 /* Reset the ethernet hardware and activate the printer pass-through. */
851 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
852 return 0;
856 * Set or clear the multicast filter for this adapter.
859 static void set_rx_mode_8002(struct net_device *dev)
861 struct net_local *lp = netdev_priv(dev);
862 long ioaddr = dev->base_addr;
864 if (dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
865 lp->addr_mode = CMR2h_PROMISC;
866 else
867 lp->addr_mode = CMR2h_Normal;
868 write_reg_high(ioaddr, CMR2, lp->addr_mode);
871 static void set_rx_mode_8012(struct net_device *dev)
873 struct net_local *lp = netdev_priv(dev);
874 long ioaddr = dev->base_addr;
875 unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
876 int i;
878 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
879 new_mode = CMR2h_PROMISC;
880 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
881 /* Too many to filter perfectly -- accept all multicasts. */
882 memset(mc_filter, 0xff, sizeof(mc_filter));
883 new_mode = CMR2h_Normal;
884 } else {
885 struct dev_mc_list *mclist;
887 memset(mc_filter, 0, sizeof(mc_filter));
888 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
889 i++, mclist = mclist->next)
891 int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
892 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
894 new_mode = CMR2h_Normal;
896 lp->addr_mode = new_mode;
897 write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
898 for (i = 0; i < 8; i++)
899 write_reg_byte(ioaddr, i, mc_filter[i]);
900 if (net_debug > 2 || 1) {
901 lp->addr_mode = 1;
902 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
903 dev->name, lp->addr_mode);
904 for (i = 0; i < 8; i++)
905 printk(" %2.2x", mc_filter[i]);
906 printk(".\n");
909 write_reg_high(ioaddr, CMR2, lp->addr_mode);
910 write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
913 static void set_rx_mode(struct net_device *dev)
915 struct net_local *lp = netdev_priv(dev);
917 if (lp->chip_type == RTL8002)
918 return set_rx_mode_8002(dev);
919 else
920 return set_rx_mode_8012(dev);
924 static int __init atp_init_module(void) {
925 if (debug) /* Emit version even if no cards detected. */
926 printk(KERN_INFO "%s", version);
927 return atp_init();
930 static void __exit atp_cleanup_module(void) {
931 struct net_device *next_dev;
933 while (root_atp_dev) {
934 struct net_local *atp_local = netdev_priv(root_atp_dev);
935 next_dev = atp_local->next_module;
936 unregister_netdev(root_atp_dev);
937 /* No need to release_region(), since we never snarf it. */
938 free_netdev(root_atp_dev);
939 root_atp_dev = next_dev;
943 module_init(atp_init_module);
944 module_exit(atp_cleanup_module);