iwlwifi: use rmb/wmb to protect indirect mmio operation
[linux/fpc-iii.git] / drivers / net / atp.c
blob1d6b74c5d6c942a5479c36ffca6a6eaaa317ef19
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 int atp_send_packet(struct sk_buff *skb, struct net_device *dev);
203 static irqreturn_t atp_interrupt(int irq, void *dev_id);
204 static void net_rx(struct net_device *dev);
205 static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
206 static int net_close(struct net_device *dev);
207 static void set_rx_mode_8002(struct net_device *dev);
208 static void set_rx_mode_8012(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 int __init atp_probe1(long ioaddr)
247 struct net_device *dev = NULL;
248 struct net_local *lp;
249 int saved_ctrl_reg, status, i;
250 int res;
252 outb(0xff, ioaddr + PAR_DATA);
253 /* Save the original value of the Control register, in case we guessed
254 wrong. */
255 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
256 if (net_debug > 3)
257 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
258 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
259 outb(0x04, ioaddr + PAR_CONTROL);
260 #ifndef final_version
261 if (net_debug > 3) {
262 /* Turn off the printer multiplexer on the 8012. */
263 for (i = 0; i < 8; i++)
264 outb(mux_8012[i], ioaddr + PAR_DATA);
265 write_reg(ioaddr, MODSEL, 0x00);
266 printk("atp: Registers are ");
267 for (i = 0; i < 32; i++)
268 printk(" %2.2x", read_nibble(ioaddr, i));
269 printk(".\n");
271 #endif
272 /* Turn off the printer multiplexer on the 8012. */
273 for (i = 0; i < 8; i++)
274 outb(mux_8012[i], ioaddr + PAR_DATA);
275 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
276 /* udelay() here? */
277 status = read_nibble(ioaddr, CMR1);
279 if (net_debug > 3) {
280 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
281 for (i = 0; i < 32; i++)
282 printk(" %2.2x", read_nibble(ioaddr, i));
283 printk("\n");
286 if ((status & 0x78) != 0x08) {
287 /* The pocket adapter probe failed, restore the control register. */
288 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
289 return -ENODEV;
291 status = read_nibble(ioaddr, CMR2_h);
292 if ((status & 0x78) != 0x10) {
293 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
294 return -ENODEV;
297 dev = alloc_etherdev(sizeof(struct net_local));
298 if (!dev)
299 return -ENOMEM;
301 /* Find the IRQ used by triggering an interrupt. */
302 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
303 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
305 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
306 if (irq[0])
307 dev->irq = irq[0];
308 else if (ioaddr == 0x378)
309 dev->irq = 7;
310 else
311 dev->irq = 5;
312 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
313 write_reg(ioaddr, CMR2, CMR2_NULL);
315 dev->base_addr = ioaddr;
317 /* Read the station address PROM. */
318 get_node_ID(dev);
320 #ifndef MODULE
321 if (net_debug)
322 printk(KERN_INFO "%s", version);
323 #endif
325 printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
326 "SAPROM %pM.\n",
327 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
329 /* Reset the ethernet hardware and activate the printer pass-through. */
330 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
332 lp = netdev_priv(dev);
333 lp->chip_type = RTL8002;
334 lp->addr_mode = CMR2h_Normal;
335 spin_lock_init(&lp->lock);
337 /* For the ATP adapter the "if_port" is really the data transfer mode. */
338 if (xcvr[0])
339 dev->if_port = xcvr[0];
340 else
341 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
342 if (dev->mem_end & 0xf)
343 net_debug = dev->mem_end & 7;
345 dev->open = net_open;
346 dev->stop = net_close;
347 dev->hard_start_xmit = atp_send_packet;
348 dev->set_multicast_list =
349 lp->chip_type == RTL8002 ? &set_rx_mode_8002 : &set_rx_mode_8012;
350 dev->tx_timeout = tx_timeout;
351 dev->watchdog_timeo = TX_TIMEOUT;
353 res = register_netdev(dev);
354 if (res) {
355 free_netdev(dev);
356 return res;
359 lp->next_module = root_atp_dev;
360 root_atp_dev = dev;
362 return 0;
365 /* Read the station address PROM, usually a word-wide EEPROM. */
366 static void __init get_node_ID(struct net_device *dev)
368 long ioaddr = dev->base_addr;
369 int sa_offset = 0;
370 int i;
372 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
374 /* Some adapters have the station address at offset 15 instead of offset
375 zero. Check for it, and fix it if needed. */
376 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
377 sa_offset = 15;
379 for (i = 0; i < 3; i++)
380 ((__be16 *)dev->dev_addr)[i] =
381 cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
383 write_reg(ioaddr, CMR2, CMR2_NULL);
387 An EEPROM read command starts by shifting out 0x60+address, and then
388 shifting in the serial data. See the NatSemi databook for details.
389 * ________________
390 * CS : __|
391 * ___ ___
392 * CLK: ______| |___| |
393 * __ _______ _______
394 * DI : __X_______X_______X
395 * DO : _________X_______X
398 static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
400 unsigned eedata_out = 0;
401 int num_bits = EE_CMD_SIZE;
403 while (--num_bits >= 0) {
404 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
405 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
406 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
407 eedata_out <<= 1;
408 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
409 eedata_out++;
411 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
412 return eedata_out;
416 /* Open/initialize the board. This is called (in the current kernel)
417 sometime after booting when the 'ifconfig' program is run.
419 This routine sets everything up anew at each open, even
420 registers that "should" only need to be set once at boot, so that
421 there is non-reboot way to recover if something goes wrong.
423 This is an attachable device: if there is no private entry then it wasn't
424 probed for at boot-time, and we need to probe for it again.
426 static int net_open(struct net_device *dev)
428 struct net_local *lp = netdev_priv(dev);
429 int ret;
431 /* The interrupt line is turned off (tri-stated) when the device isn't in
432 use. That's especially important for "attached" interfaces where the
433 port or interrupt may be shared. */
434 ret = request_irq(dev->irq, &atp_interrupt, 0, dev->name, dev);
435 if (ret)
436 return ret;
438 hardware_init(dev);
440 init_timer(&lp->timer);
441 lp->timer.expires = jiffies + TIMED_CHECKER;
442 lp->timer.data = (unsigned long)dev;
443 lp->timer.function = &atp_timed_checker; /* timer handler */
444 add_timer(&lp->timer);
446 netif_start_queue(dev);
447 return 0;
450 /* This routine resets the hardware. We initialize everything, assuming that
451 the hardware may have been temporarily detached. */
452 static void hardware_init(struct net_device *dev)
454 struct net_local *lp = netdev_priv(dev);
455 long ioaddr = dev->base_addr;
456 int i;
458 /* Turn off the printer multiplexer on the 8012. */
459 for (i = 0; i < 8; i++)
460 outb(mux_8012[i], ioaddr + PAR_DATA);
461 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
463 for (i = 0; i < 6; i++)
464 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
466 write_reg_high(ioaddr, CMR2, lp->addr_mode);
468 if (net_debug > 2) {
469 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
470 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
473 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
474 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
476 /* Enable the interrupt line from the serial port. */
477 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
479 /* Unmask the interesting interrupts. */
480 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
481 write_reg_high(ioaddr, IMR, ISRh_RxErr);
483 lp->tx_unit_busy = 0;
484 lp->pac_cnt_in_tx_buf = 0;
485 lp->saved_tx_size = 0;
488 static void trigger_send(long ioaddr, int length)
490 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
491 write_reg(ioaddr, TxCNT1, length >> 8);
492 write_reg(ioaddr, CMR1, CMR1_Xmit);
495 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
497 if (length & 1)
499 length++;
500 pad_len++;
503 outb(EOC+MAR, ioaddr + PAR_DATA);
504 if ((data_mode & 1) == 0) {
505 /* Write the packet out, starting with the write addr. */
506 outb(WrAddr+MAR, ioaddr + PAR_DATA);
507 do {
508 write_byte_mode0(ioaddr, *packet++);
509 } while (--length > pad_len) ;
510 do {
511 write_byte_mode0(ioaddr, 0);
512 } while (--length > 0) ;
513 } else {
514 /* Write the packet out in slow mode. */
515 unsigned char outbyte = *packet++;
517 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
518 outb(WrAddr+MAR, ioaddr + PAR_DATA);
520 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
521 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
522 outbyte >>= 4;
523 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
524 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
525 while (--length > pad_len)
526 write_byte_mode1(ioaddr, *packet++);
527 while (--length > 0)
528 write_byte_mode1(ioaddr, 0);
530 /* Terminate the Tx frame. End of write: ECB. */
531 outb(0xff, ioaddr + PAR_DATA);
532 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
535 static void tx_timeout(struct net_device *dev)
537 long ioaddr = dev->base_addr;
539 printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
540 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
541 : "IRQ conflict");
542 dev->stats.tx_errors++;
543 /* Try to restart the adapter. */
544 hardware_init(dev);
545 dev->trans_start = jiffies;
546 netif_wake_queue(dev);
547 dev->stats.tx_errors++;
550 static int atp_send_packet(struct sk_buff *skb, struct net_device *dev)
552 struct net_local *lp = netdev_priv(dev);
553 long ioaddr = dev->base_addr;
554 int length;
555 unsigned long flags;
557 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
559 netif_stop_queue(dev);
561 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
562 This sequence must not be interrupted by an incoming packet. */
564 spin_lock_irqsave(&lp->lock, flags);
565 write_reg(ioaddr, IMR, 0);
566 write_reg_high(ioaddr, IMR, 0);
567 spin_unlock_irqrestore(&lp->lock, flags);
569 write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
571 lp->pac_cnt_in_tx_buf++;
572 if (lp->tx_unit_busy == 0) {
573 trigger_send(ioaddr, length);
574 lp->saved_tx_size = 0; /* Redundant */
575 lp->re_tx = 0;
576 lp->tx_unit_busy = 1;
577 } else
578 lp->saved_tx_size = length;
579 /* Re-enable the LPT interrupts. */
580 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
581 write_reg_high(ioaddr, IMR, ISRh_RxErr);
583 dev->trans_start = jiffies;
584 dev_kfree_skb (skb);
585 return 0;
589 /* The typical workload of the driver:
590 Handle the network interface interrupts. */
591 static irqreturn_t atp_interrupt(int irq, void *dev_instance)
593 struct net_device *dev = dev_instance;
594 struct net_local *lp;
595 long ioaddr;
596 static int num_tx_since_rx;
597 int boguscount = max_interrupt_work;
598 int handled = 0;
600 ioaddr = dev->base_addr;
601 lp = netdev_priv(dev);
603 spin_lock(&lp->lock);
605 /* Disable additional spurious interrupts. */
606 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
608 /* The adapter's output is currently the IRQ line, switch it to data. */
609 write_reg(ioaddr, CMR2, CMR2_NULL);
610 write_reg(ioaddr, IMR, 0);
612 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
613 while (--boguscount > 0) {
614 int status = read_nibble(ioaddr, ISR);
615 if (net_debug > 5) printk("loop status %02x..", status);
617 if (status & (ISR_RxOK<<3)) {
618 handled = 1;
619 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
620 do {
621 int read_status = read_nibble(ioaddr, CMR1);
622 if (net_debug > 6)
623 printk("handling Rx packet %02x..", read_status);
624 /* We acknowledged the normal Rx interrupt, so if the interrupt
625 is still outstanding we must have a Rx error. */
626 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
627 dev->stats.rx_over_errors++;
628 /* Set to no-accept mode long enough to remove a packet. */
629 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
630 net_rx(dev);
631 /* Clear the interrupt and return to normal Rx mode. */
632 write_reg_high(ioaddr, ISR, ISRh_RxErr);
633 write_reg_high(ioaddr, CMR2, lp->addr_mode);
634 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
635 net_rx(dev);
636 num_tx_since_rx = 0;
637 } else
638 break;
639 } while (--boguscount > 0);
640 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
641 handled = 1;
642 if (net_debug > 6) printk("handling Tx done..");
643 /* Clear the Tx interrupt. We should check for too many failures
644 and reinitialize the adapter. */
645 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
646 if (status & (ISR_TxErr<<3)) {
647 dev->stats.collisions++;
648 if (++lp->re_tx > 15) {
649 dev->stats.tx_aborted_errors++;
650 hardware_init(dev);
651 break;
653 /* Attempt to retransmit. */
654 if (net_debug > 6) printk("attempting to ReTx");
655 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
656 } else {
657 /* Finish up the transmit. */
658 dev->stats.tx_packets++;
659 lp->pac_cnt_in_tx_buf--;
660 if ( lp->saved_tx_size) {
661 trigger_send(ioaddr, lp->saved_tx_size);
662 lp->saved_tx_size = 0;
663 lp->re_tx = 0;
664 } else
665 lp->tx_unit_busy = 0;
666 netif_wake_queue(dev); /* Inform upper layers. */
668 num_tx_since_rx++;
669 } else if (num_tx_since_rx > 8
670 && time_after(jiffies, dev->last_rx + HZ)) {
671 if (net_debug > 2)
672 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
673 "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
674 num_tx_since_rx, jiffies - dev->last_rx, status,
675 (read_nibble(ioaddr, CMR1) >> 3) & 15);
676 dev->stats.rx_missed_errors++;
677 hardware_init(dev);
678 num_tx_since_rx = 0;
679 break;
680 } else
681 break;
684 /* This following code fixes a rare (and very difficult to track down)
685 problem where the adapter forgets its ethernet address. */
687 int i;
688 for (i = 0; i < 6; i++)
689 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
690 #if 0 && defined(TIMED_CHECKER)
691 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
692 #endif
695 /* Tell the adapter that it can go back to using the output line as IRQ. */
696 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
697 /* Enable the physical interrupt line, which is sure to be low until.. */
698 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
699 /* .. we enable the interrupt sources. */
700 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
701 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
703 spin_unlock(&lp->lock);
705 if (net_debug > 5) printk("exiting interrupt.\n");
706 return IRQ_RETVAL(handled);
709 #ifdef TIMED_CHECKER
710 /* This following code fixes a rare (and very difficult to track down)
711 problem where the adapter forgets its ethernet address. */
712 static void atp_timed_checker(unsigned long data)
714 struct net_device *dev = (struct net_device *)data;
715 long ioaddr = dev->base_addr;
716 struct net_local *lp = netdev_priv(dev);
717 int tickssofar = jiffies - lp->last_rx_time;
718 int i;
720 spin_lock(&lp->lock);
721 if (tickssofar > 2*HZ) {
722 #if 1
723 for (i = 0; i < 6; i++)
724 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
725 lp->last_rx_time = jiffies;
726 #else
727 for (i = 0; i < 6; i++)
728 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
730 struct net_local *lp = netdev_priv(atp_timed_dev);
731 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
732 if (i == 2)
733 dev->stats.tx_errors++;
734 else if (i == 3)
735 dev->stats.tx_dropped++;
736 else if (i == 4)
737 dev->stats.collisions++;
738 else
739 dev->stats.rx_errors++;
741 #endif
743 spin_unlock(&lp->lock);
744 lp->timer.expires = jiffies + TIMED_CHECKER;
745 add_timer(&lp->timer);
747 #endif
749 /* We have a good packet(s), get it/them out of the buffers. */
750 static void net_rx(struct net_device *dev)
752 struct net_local *lp = netdev_priv(dev);
753 long ioaddr = dev->base_addr;
754 struct rx_header rx_head;
756 /* Process the received packet. */
757 outb(EOC+MAR, ioaddr + PAR_DATA);
758 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
759 if (net_debug > 5)
760 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
761 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
762 if ((rx_head.rx_status & 0x77) != 0x01) {
763 dev->stats.rx_errors++;
764 if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
765 else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
766 if (net_debug > 3)
767 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
768 dev->name, rx_head.rx_status);
769 if (rx_head.rx_status & 0x0020) {
770 dev->stats.rx_fifo_errors++;
771 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
772 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
773 } else if (rx_head.rx_status & 0x0050)
774 hardware_init(dev);
775 return;
776 } else {
777 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
778 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
779 struct sk_buff *skb;
781 skb = dev_alloc_skb(pkt_len + 2);
782 if (skb == NULL) {
783 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
784 dev->name);
785 dev->stats.rx_dropped++;
786 goto done;
789 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
790 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
791 skb->protocol = eth_type_trans(skb, dev);
792 netif_rx(skb);
793 dev->last_rx = jiffies;
794 dev->stats.rx_packets++;
795 dev->stats.rx_bytes += pkt_len;
797 done:
798 write_reg(ioaddr, CMR1, CMR1_NextPkt);
799 lp->last_rx_time = jiffies;
800 return;
803 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
806 if (data_mode <= 3) { /* Mode 0 or 1 */
807 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
808 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
809 ioaddr + PAR_DATA);
810 if (data_mode <= 1) { /* Mode 0 or 1 */
811 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
812 } else /* Mode 2 or 3 */
813 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
814 } else if (data_mode <= 5)
815 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
816 else
817 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
819 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
820 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
823 /* The inverse routine to net_open(). */
824 static int
825 net_close(struct net_device *dev)
827 struct net_local *lp = netdev_priv(dev);
828 long ioaddr = dev->base_addr;
830 netif_stop_queue(dev);
832 del_timer_sync(&lp->timer);
834 /* Flush the Tx and disable Rx here. */
835 lp->addr_mode = CMR2h_OFF;
836 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
838 /* Free the IRQ line. */
839 outb(0x00, ioaddr + PAR_CONTROL);
840 free_irq(dev->irq, dev);
842 /* Reset the ethernet hardware and activate the printer pass-through. */
843 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
844 return 0;
848 * Set or clear the multicast filter for this adapter.
851 static void set_rx_mode_8002(struct net_device *dev)
853 struct net_local *lp = netdev_priv(dev);
854 long ioaddr = dev->base_addr;
856 if (dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
857 lp->addr_mode = CMR2h_PROMISC;
858 else
859 lp->addr_mode = CMR2h_Normal;
860 write_reg_high(ioaddr, CMR2, lp->addr_mode);
863 static void set_rx_mode_8012(struct net_device *dev)
865 struct net_local *lp = netdev_priv(dev);
866 long ioaddr = dev->base_addr;
867 unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
868 int i;
870 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
871 new_mode = CMR2h_PROMISC;
872 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
873 /* Too many to filter perfectly -- accept all multicasts. */
874 memset(mc_filter, 0xff, sizeof(mc_filter));
875 new_mode = CMR2h_Normal;
876 } else {
877 struct dev_mc_list *mclist;
879 memset(mc_filter, 0, sizeof(mc_filter));
880 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
881 i++, mclist = mclist->next)
883 int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
884 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
886 new_mode = CMR2h_Normal;
888 lp->addr_mode = new_mode;
889 write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
890 for (i = 0; i < 8; i++)
891 write_reg_byte(ioaddr, i, mc_filter[i]);
892 if (net_debug > 2 || 1) {
893 lp->addr_mode = 1;
894 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
895 dev->name, lp->addr_mode);
896 for (i = 0; i < 8; i++)
897 printk(" %2.2x", mc_filter[i]);
898 printk(".\n");
901 write_reg_high(ioaddr, CMR2, lp->addr_mode);
902 write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
905 static int __init atp_init_module(void) {
906 if (debug) /* Emit version even if no cards detected. */
907 printk(KERN_INFO "%s", version);
908 return atp_init();
911 static void __exit atp_cleanup_module(void) {
912 struct net_device *next_dev;
914 while (root_atp_dev) {
915 struct net_local *atp_local = netdev_priv(root_atp_dev);
916 next_dev = atp_local->next_module;
917 unregister_netdev(root_atp_dev);
918 /* No need to release_region(), since we never snarf it. */
919 free_netdev(root_atp_dev);
920 root_atp_dev = next_dev;
924 module_init(atp_init_module);
925 module_exit(atp_cleanup_module);