18c2ecf20Sopenharmony_ci/* atp.c: Attached (pocket) ethernet adapter driver for linux. */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci	This is a driver for commonly OEM pocket (parallel port)
48c2ecf20Sopenharmony_ci	ethernet adapters based on the Realtek RTL8002 and RTL8012 chips.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci	Written 1993-2000 by Donald Becker.
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci	This software may be used and distributed according to the terms of
98c2ecf20Sopenharmony_ci	the GNU General Public License (GPL), incorporated herein by reference.
108c2ecf20Sopenharmony_ci	Drivers based on or derived from this code fall under the GPL and must
118c2ecf20Sopenharmony_ci	retain the authorship, copyright and license notice.  This file is not
128c2ecf20Sopenharmony_ci	a complete program and may only be used when the entire operating
138c2ecf20Sopenharmony_ci	system is licensed under the GPL.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	Copyright 1993 United States Government as represented by the Director,
168c2ecf20Sopenharmony_ci	National Security Agency.  Copyright 1994-2000 retained by the original
178c2ecf20Sopenharmony_ci	author, Donald Becker. The timer-based reset code was supplied in 1995
188c2ecf20Sopenharmony_ci	by Bill Carlson, wwc@super.org.
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	The author may be reached as becker@scyld.com, or C/O
218c2ecf20Sopenharmony_ci	Scyld Computing Corporation
228c2ecf20Sopenharmony_ci	410 Severn Ave., Suite 210
238c2ecf20Sopenharmony_ci	Annapolis MD 21403
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	Support information and updates available at
268c2ecf20Sopenharmony_ci	http://www.scyld.com/network/atp.html
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	Modular support/softnet added by Alan Cox.
308c2ecf20Sopenharmony_ci	_bit abuse fixed up by Alan Cox
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci*/
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const char version[] =
358c2ecf20Sopenharmony_ci"atp.c:v1.09=ac 2002/10/01 Donald Becker <becker@scyld.com>\n";
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* The user-configurable values.
388c2ecf20Sopenharmony_ci   These may be modified when a driver module is loaded.*/
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int debug = 1; 			/* 1 normal messages, 0 quiet .. 7 verbose. */
418c2ecf20Sopenharmony_ci#define net_debug debug
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
448c2ecf20Sopenharmony_cistatic int max_interrupt_work = 15;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define NUM_UNITS 2
478c2ecf20Sopenharmony_ci/* The standard set of ISA module parameters. */
488c2ecf20Sopenharmony_cistatic int io[NUM_UNITS];
498c2ecf20Sopenharmony_cistatic int irq[NUM_UNITS];
508c2ecf20Sopenharmony_cistatic int xcvr[NUM_UNITS]; 			/* The data transfer mode. */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Operational parameters that are set at compile time. */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* Time in jiffies before concluding the transmitter is hung. */
558c2ecf20Sopenharmony_ci#define TX_TIMEOUT  (400*HZ/1000)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci	This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
598c2ecf20Sopenharmony_ci	ethernet adapter.  This is a common low-cost OEM pocket ethernet
608c2ecf20Sopenharmony_ci	adapter, sold under many names.
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci  Sources:
638c2ecf20Sopenharmony_ci	This driver was written from the packet driver assembly code provided by
648c2ecf20Sopenharmony_ci	Vincent Bono of AT-Lan-Tec.	 Ever try to figure out how a complicated
658c2ecf20Sopenharmony_ci	device works just from the assembly code?  It ain't pretty.  The following
668c2ecf20Sopenharmony_ci	description is written based on guesses and writing lots of special-purpose
678c2ecf20Sopenharmony_ci	code to test my theorized operation.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	In 1997 Realtek made available the documentation for the second generation
708c2ecf20Sopenharmony_ci	RTL8012 chip, which has lead to several driver improvements.
718c2ecf20Sopenharmony_ci	  http://www.realtek.com.tw/
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci					Theory of Operation
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	The RTL8002 adapter seems to be built around a custom spin of the SEEQ
768c2ecf20Sopenharmony_ci	controller core.  It probably has a 16K or 64K internal packet buffer, of
778c2ecf20Sopenharmony_ci	which the first 4K is devoted to transmit and the rest to receive.
788c2ecf20Sopenharmony_ci	The controller maintains the queue of received packet and the packet buffer
798c2ecf20Sopenharmony_ci	access pointer internally, with only 'reset to beginning' and 'skip to next
808c2ecf20Sopenharmony_ci	packet' commands visible.  The transmit packet queue holds two (or more?)
818c2ecf20Sopenharmony_ci	packets: both 'retransmit this packet' (due to collision) and 'transmit next
828c2ecf20Sopenharmony_ci	packet' commands must be started by hand.
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	The station address is stored in a standard bit-serial EEPROM which must be
858c2ecf20Sopenharmony_ci	read (ughh) by the device driver.  (Provisions have been made for
868c2ecf20Sopenharmony_ci	substituting a 74S288 PROM, but I haven't gotten reports of any models
878c2ecf20Sopenharmony_ci	using it.)  Unlike built-in devices, a pocket adapter can temporarily lose
888c2ecf20Sopenharmony_ci	power without indication to the device driver.  The major effect is that
898c2ecf20Sopenharmony_ci	the station address, receive filter (promiscuous, etc.) and transceiver
908c2ecf20Sopenharmony_ci	must be reset.
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	The controller itself has 16 registers, some of which use only the lower
938c2ecf20Sopenharmony_ci	bits.  The registers are read and written 4 bits at a time.  The four bit
948c2ecf20Sopenharmony_ci	register address is presented on the data lines along with a few additional
958c2ecf20Sopenharmony_ci	timing and control bits.  The data is then read from status port or written
968c2ecf20Sopenharmony_ci	to the data port.
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	Correction: the controller has two banks of 16 registers.  The second
998c2ecf20Sopenharmony_ci	bank contains only the multicast filter table (now used) and the EEPROM
1008c2ecf20Sopenharmony_ci	access registers.
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	Since the bulk data transfer of the actual packets through the slow
1038c2ecf20Sopenharmony_ci	parallel port dominates the driver's running time, four distinct data
1048c2ecf20Sopenharmony_ci	(non-register) transfer modes are provided by the adapter, two in each
1058c2ecf20Sopenharmony_ci	direction.  In the first mode timing for the nibble transfers is
1068c2ecf20Sopenharmony_ci	provided through the data port.  In the second mode the same timing is
1078c2ecf20Sopenharmony_ci	provided through the control port.  In either case the data is read from
1088c2ecf20Sopenharmony_ci	the status port and written to the data port, just as it is accessing
1098c2ecf20Sopenharmony_ci	registers.
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	In addition to the basic data transfer methods, several more are modes are
1128c2ecf20Sopenharmony_ci	created by adding some delay by doing multiple reads of the data to allow
1138c2ecf20Sopenharmony_ci	it to stabilize.  This delay seems to be needed on most machines.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	The data transfer mode is stored in the 'dev->if_port' field.  Its default
1168c2ecf20Sopenharmony_ci	value is '4'.  It may be overridden at boot-time using the third parameter
1178c2ecf20Sopenharmony_ci	to the "ether=..." initialization.
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	The header file <atp.h> provides inline functions that encapsulate the
1208c2ecf20Sopenharmony_ci	register and data access methods.  These functions are hand-tuned to
1218c2ecf20Sopenharmony_ci	generate reasonable object code.  This header file also documents my
1228c2ecf20Sopenharmony_ci	interpretations of the device registers.
1238c2ecf20Sopenharmony_ci*/
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
1268c2ecf20Sopenharmony_ci#include <linux/module.h>
1278c2ecf20Sopenharmony_ci#include <linux/types.h>
1288c2ecf20Sopenharmony_ci#include <linux/fcntl.h>
1298c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
1308c2ecf20Sopenharmony_ci#include <linux/ioport.h>
1318c2ecf20Sopenharmony_ci#include <linux/in.h>
1328c2ecf20Sopenharmony_ci#include <linux/string.h>
1338c2ecf20Sopenharmony_ci#include <linux/errno.h>
1348c2ecf20Sopenharmony_ci#include <linux/init.h>
1358c2ecf20Sopenharmony_ci#include <linux/crc32.h>
1368c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
1378c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
1388c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
1398c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
1408c2ecf20Sopenharmony_ci#include <linux/delay.h>
1418c2ecf20Sopenharmony_ci#include <linux/bitops.h>
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#include <asm/io.h>
1448c2ecf20Sopenharmony_ci#include <asm/dma.h>
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#include "atp.h"
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Donald Becker <becker@scyld.com>");
1498c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
1508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cimodule_param(max_interrupt_work, int, 0);
1538c2ecf20Sopenharmony_cimodule_param(debug, int, 0);
1548c2ecf20Sopenharmony_cimodule_param_hw_array(io, int, ioport, NULL, 0);
1558c2ecf20Sopenharmony_cimodule_param_hw_array(irq, int, irq, NULL, 0);
1568c2ecf20Sopenharmony_cimodule_param_array(xcvr, int, NULL, 0);
1578c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
1588c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "ATP debug level (0-7)");
1598c2ecf20Sopenharmony_ciMODULE_PARM_DESC(io, "ATP I/O base address(es)");
1608c2ecf20Sopenharmony_ciMODULE_PARM_DESC(irq, "ATP IRQ number(s)");
1618c2ecf20Sopenharmony_ciMODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)");
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* The number of low I/O ports used by the ethercard. */
1648c2ecf20Sopenharmony_ci#define ETHERCARD_TOTAL_SIZE	3
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/* Sequence to switch an 8012 from printer mux to ethernet mode. */
1678c2ecf20Sopenharmony_cistatic char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct net_local {
1708c2ecf20Sopenharmony_ci    spinlock_t lock;
1718c2ecf20Sopenharmony_ci    struct net_device *next_module;
1728c2ecf20Sopenharmony_ci    struct timer_list timer;	/* Media selection timer. */
1738c2ecf20Sopenharmony_ci    struct net_device *dev;	/* Timer dev. */
1748c2ecf20Sopenharmony_ci    unsigned long last_rx_time;	/* Last Rx, in jiffies, to handle Rx hang. */
1758c2ecf20Sopenharmony_ci    int saved_tx_size;
1768c2ecf20Sopenharmony_ci    unsigned int tx_unit_busy:1;
1778c2ecf20Sopenharmony_ci    unsigned char re_tx,	/* Number of packet retransmissions. */
1788c2ecf20Sopenharmony_ci		addr_mode,		/* Current Rx filter e.g. promiscuous, etc. */
1798c2ecf20Sopenharmony_ci		pac_cnt_in_tx_buf;
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* This code, written by wwc@super.org, resets the adapter every
1838c2ecf20Sopenharmony_ci   TIMED_CHECKER ticks.  This recovers from an unknown error which
1848c2ecf20Sopenharmony_ci   hangs the device. */
1858c2ecf20Sopenharmony_ci#define TIMED_CHECKER (HZ/4)
1868c2ecf20Sopenharmony_ci#ifdef TIMED_CHECKER
1878c2ecf20Sopenharmony_ci#include <linux/timer.h>
1888c2ecf20Sopenharmony_cistatic void atp_timed_checker(struct timer_list *t);
1898c2ecf20Sopenharmony_ci#endif
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/* Index to functions, as function prototypes. */
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic int atp_probe1(long ioaddr);
1948c2ecf20Sopenharmony_cistatic void get_node_ID(struct net_device *dev);
1958c2ecf20Sopenharmony_cistatic unsigned short eeprom_op(long ioaddr, unsigned int cmd);
1968c2ecf20Sopenharmony_cistatic int net_open(struct net_device *dev);
1978c2ecf20Sopenharmony_cistatic void hardware_init(struct net_device *dev);
1988c2ecf20Sopenharmony_cistatic void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
1998c2ecf20Sopenharmony_cistatic void trigger_send(long ioaddr, int length);
2008c2ecf20Sopenharmony_cistatic netdev_tx_t atp_send_packet(struct sk_buff *skb,
2018c2ecf20Sopenharmony_ci				   struct net_device *dev);
2028c2ecf20Sopenharmony_cistatic irqreturn_t atp_interrupt(int irq, void *dev_id);
2038c2ecf20Sopenharmony_cistatic void net_rx(struct net_device *dev);
2048c2ecf20Sopenharmony_cistatic void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
2058c2ecf20Sopenharmony_cistatic int net_close(struct net_device *dev);
2068c2ecf20Sopenharmony_cistatic void set_rx_mode(struct net_device *dev);
2078c2ecf20Sopenharmony_cistatic void tx_timeout(struct net_device *dev, unsigned int txqueue);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci/* A list of all installed ATP devices, for removing the driver module. */
2118c2ecf20Sopenharmony_cistatic struct net_device *root_atp_dev;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* Check for a network adapter of this type, and return '0' iff one exists.
2148c2ecf20Sopenharmony_ci   If dev->base_addr == 0, probe all likely locations.
2158c2ecf20Sopenharmony_ci   If dev->base_addr == 1, always return failure.
2168c2ecf20Sopenharmony_ci   If dev->base_addr == 2, allocate space for the device and return success
2178c2ecf20Sopenharmony_ci   (detachable devices only).
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci   FIXME: we should use the parport layer for this
2208c2ecf20Sopenharmony_ci   */
2218c2ecf20Sopenharmony_cistatic int __init atp_init(void)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
2248c2ecf20Sopenharmony_ci	int base_addr = io[0];
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (base_addr > 0x1ff)		/* Check a single specified location. */
2278c2ecf20Sopenharmony_ci		return atp_probe1(base_addr);
2288c2ecf20Sopenharmony_ci	else if (base_addr == 1)	/* Don't probe at all. */
2298c2ecf20Sopenharmony_ci		return -ENXIO;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	for (port = ports; *port; port++) {
2328c2ecf20Sopenharmony_ci		long ioaddr = *port;
2338c2ecf20Sopenharmony_ci		outb(0x57, ioaddr + PAR_DATA);
2348c2ecf20Sopenharmony_ci		if (inb(ioaddr + PAR_DATA) != 0x57)
2358c2ecf20Sopenharmony_ci			continue;
2368c2ecf20Sopenharmony_ci		if (atp_probe1(ioaddr) == 0)
2378c2ecf20Sopenharmony_ci			return 0;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return -ENODEV;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic const struct net_device_ops atp_netdev_ops = {
2448c2ecf20Sopenharmony_ci	.ndo_open		= net_open,
2458c2ecf20Sopenharmony_ci	.ndo_stop		= net_close,
2468c2ecf20Sopenharmony_ci	.ndo_start_xmit		= atp_send_packet,
2478c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= set_rx_mode,
2488c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= tx_timeout,
2498c2ecf20Sopenharmony_ci	.ndo_set_mac_address 	= eth_mac_addr,
2508c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic int __init atp_probe1(long ioaddr)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	struct net_device *dev = NULL;
2568c2ecf20Sopenharmony_ci	struct net_local *lp;
2578c2ecf20Sopenharmony_ci	int saved_ctrl_reg, status, i;
2588c2ecf20Sopenharmony_ci	int res;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	outb(0xff, ioaddr + PAR_DATA);
2618c2ecf20Sopenharmony_ci	/* Save the original value of the Control register, in case we guessed
2628c2ecf20Sopenharmony_ci	   wrong. */
2638c2ecf20Sopenharmony_ci	saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
2648c2ecf20Sopenharmony_ci	if (net_debug > 3)
2658c2ecf20Sopenharmony_ci		printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
2668c2ecf20Sopenharmony_ci	/* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
2678c2ecf20Sopenharmony_ci	outb(0x04, ioaddr + PAR_CONTROL);
2688c2ecf20Sopenharmony_ci#ifndef final_version
2698c2ecf20Sopenharmony_ci	if (net_debug > 3) {
2708c2ecf20Sopenharmony_ci		/* Turn off the printer multiplexer on the 8012. */
2718c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++)
2728c2ecf20Sopenharmony_ci			outb(mux_8012[i], ioaddr + PAR_DATA);
2738c2ecf20Sopenharmony_ci		write_reg(ioaddr, MODSEL, 0x00);
2748c2ecf20Sopenharmony_ci		printk("atp: Registers are ");
2758c2ecf20Sopenharmony_ci		for (i = 0; i < 32; i++)
2768c2ecf20Sopenharmony_ci			printk(" %2.2x", read_nibble(ioaddr, i));
2778c2ecf20Sopenharmony_ci		printk(".\n");
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci#endif
2808c2ecf20Sopenharmony_ci	/* Turn off the printer multiplexer on the 8012. */
2818c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
2828c2ecf20Sopenharmony_ci		outb(mux_8012[i], ioaddr + PAR_DATA);
2838c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RESET);
2848c2ecf20Sopenharmony_ci	/* udelay() here? */
2858c2ecf20Sopenharmony_ci	status = read_nibble(ioaddr, CMR1);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (net_debug > 3) {
2888c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
2898c2ecf20Sopenharmony_ci		for (i = 0; i < 32; i++)
2908c2ecf20Sopenharmony_ci			printk(" %2.2x", read_nibble(ioaddr, i));
2918c2ecf20Sopenharmony_ci		printk("\n");
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if ((status & 0x78) != 0x08) {
2958c2ecf20Sopenharmony_ci		/* The pocket adapter probe failed, restore the control register. */
2968c2ecf20Sopenharmony_ci		outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
2978c2ecf20Sopenharmony_ci		return -ENODEV;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci	status = read_nibble(ioaddr, CMR2_h);
3008c2ecf20Sopenharmony_ci	if ((status & 0x78) != 0x10) {
3018c2ecf20Sopenharmony_ci		outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
3028c2ecf20Sopenharmony_ci		return -ENODEV;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(struct net_local));
3068c2ecf20Sopenharmony_ci	if (!dev)
3078c2ecf20Sopenharmony_ci		return -ENOMEM;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/* Find the IRQ used by triggering an interrupt. */
3108c2ecf20Sopenharmony_ci	write_reg_byte(ioaddr, CMR2, 0x01);			/* No accept mode, IRQ out. */
3118c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);	/* Enable Tx and Rx. */
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/* Omit autoIRQ routine for now. Use "table lookup" instead.  Uhgggh. */
3148c2ecf20Sopenharmony_ci	if (irq[0])
3158c2ecf20Sopenharmony_ci		dev->irq = irq[0];
3168c2ecf20Sopenharmony_ci	else if (ioaddr == 0x378)
3178c2ecf20Sopenharmony_ci		dev->irq = 7;
3188c2ecf20Sopenharmony_ci	else
3198c2ecf20Sopenharmony_ci		dev->irq = 5;
3208c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
3218c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_NULL);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	dev->base_addr = ioaddr;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/* Read the station address PROM.  */
3268c2ecf20Sopenharmony_ci	get_node_ID(dev);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci#ifndef MODULE
3298c2ecf20Sopenharmony_ci	if (net_debug)
3308c2ecf20Sopenharmony_ci		printk(KERN_INFO "%s", version);
3318c2ecf20Sopenharmony_ci#endif
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
3348c2ecf20Sopenharmony_ci	       "SAPROM %pM.\n",
3358c2ecf20Sopenharmony_ci	       dev->name, dev->base_addr, dev->irq, dev->dev_addr);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* Reset the ethernet hardware and activate the printer pass-through. */
3388c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	lp = netdev_priv(dev);
3418c2ecf20Sopenharmony_ci	lp->addr_mode = CMR2h_Normal;
3428c2ecf20Sopenharmony_ci	spin_lock_init(&lp->lock);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* For the ATP adapter the "if_port" is really the data transfer mode. */
3458c2ecf20Sopenharmony_ci	if (xcvr[0])
3468c2ecf20Sopenharmony_ci		dev->if_port = xcvr[0];
3478c2ecf20Sopenharmony_ci	else
3488c2ecf20Sopenharmony_ci		dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
3498c2ecf20Sopenharmony_ci	if (dev->mem_end & 0xf)
3508c2ecf20Sopenharmony_ci		net_debug = dev->mem_end & 7;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	dev->netdev_ops 	= &atp_netdev_ops;
3538c2ecf20Sopenharmony_ci	dev->watchdog_timeo	= TX_TIMEOUT;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	res = register_netdev(dev);
3568c2ecf20Sopenharmony_ci	if (res) {
3578c2ecf20Sopenharmony_ci		free_netdev(dev);
3588c2ecf20Sopenharmony_ci		return res;
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	lp->next_module = root_atp_dev;
3628c2ecf20Sopenharmony_ci	root_atp_dev = dev;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci/* Read the station address PROM, usually a word-wide EEPROM. */
3688c2ecf20Sopenharmony_cistatic void __init get_node_ID(struct net_device *dev)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
3718c2ecf20Sopenharmony_ci	int sa_offset = 0;
3728c2ecf20Sopenharmony_ci	int i;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_EEPROM);	  /* Point to the EEPROM control registers. */
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* Some adapters have the station address at offset 15 instead of offset
3778c2ecf20Sopenharmony_ci	   zero.  Check for it, and fix it if needed. */
3788c2ecf20Sopenharmony_ci	if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
3798c2ecf20Sopenharmony_ci		sa_offset = 15;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++)
3828c2ecf20Sopenharmony_ci		((__be16 *)dev->dev_addr)[i] =
3838c2ecf20Sopenharmony_ci			cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_NULL);
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/*
3898c2ecf20Sopenharmony_ci  An EEPROM read command starts by shifting out 0x60+address, and then
3908c2ecf20Sopenharmony_ci  shifting in the serial data. See the NatSemi databook for details.
3918c2ecf20Sopenharmony_ci *		   ________________
3928c2ecf20Sopenharmony_ci * CS : __|
3938c2ecf20Sopenharmony_ci *			   ___	   ___
3948c2ecf20Sopenharmony_ci * CLK: ______|	  |___|	  |
3958c2ecf20Sopenharmony_ci *		 __ _______ _______
3968c2ecf20Sopenharmony_ci * DI :	 __X_______X_______X
3978c2ecf20Sopenharmony_ci * DO :	 _________X_______X
3988c2ecf20Sopenharmony_ci */
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic unsigned short __init eeprom_op(long ioaddr, u32 cmd)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	unsigned eedata_out = 0;
4038c2ecf20Sopenharmony_ci	int num_bits = EE_CMD_SIZE;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	while (--num_bits >= 0) {
4068c2ecf20Sopenharmony_ci		char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
4078c2ecf20Sopenharmony_ci		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
4088c2ecf20Sopenharmony_ci		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
4098c2ecf20Sopenharmony_ci		eedata_out <<= 1;
4108c2ecf20Sopenharmony_ci		if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
4118c2ecf20Sopenharmony_ci			eedata_out++;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
4148c2ecf20Sopenharmony_ci	return eedata_out;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci/* Open/initialize the board.  This is called (in the current kernel)
4198c2ecf20Sopenharmony_ci   sometime after booting when the 'ifconfig' program is run.
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci   This routine sets everything up anew at each open, even
4228c2ecf20Sopenharmony_ci   registers that "should" only need to be set once at boot, so that
4238c2ecf20Sopenharmony_ci   there is non-reboot way to recover if something goes wrong.
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci   This is an attachable device: if there is no private entry then it wasn't
4268c2ecf20Sopenharmony_ci   probed for at boot-time, and we need to probe for it again.
4278c2ecf20Sopenharmony_ci   */
4288c2ecf20Sopenharmony_cistatic int net_open(struct net_device *dev)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
4318c2ecf20Sopenharmony_ci	int ret;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* The interrupt line is turned off (tri-stated) when the device isn't in
4348c2ecf20Sopenharmony_ci	   use.  That's especially important for "attached" interfaces where the
4358c2ecf20Sopenharmony_ci	   port or interrupt may be shared. */
4368c2ecf20Sopenharmony_ci	ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev);
4378c2ecf20Sopenharmony_ci	if (ret)
4388c2ecf20Sopenharmony_ci		return ret;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	hardware_init(dev);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	lp->dev = dev;
4438c2ecf20Sopenharmony_ci	timer_setup(&lp->timer, atp_timed_checker, 0);
4448c2ecf20Sopenharmony_ci	lp->timer.expires = jiffies + TIMED_CHECKER;
4458c2ecf20Sopenharmony_ci	add_timer(&lp->timer);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	netif_start_queue(dev);
4488c2ecf20Sopenharmony_ci	return 0;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/* This routine resets the hardware.  We initialize everything, assuming that
4528c2ecf20Sopenharmony_ci   the hardware may have been temporarily detached. */
4538c2ecf20Sopenharmony_cistatic void hardware_init(struct net_device *dev)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
4568c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
4578c2ecf20Sopenharmony_ci	int i;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/* Turn off the printer multiplexer on the 8012. */
4608c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
4618c2ecf20Sopenharmony_ci		outb(mux_8012[i], ioaddr + PAR_DATA);
4628c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RESET);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++)
4658c2ecf20Sopenharmony_ci		write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR2, lp->addr_mode);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (net_debug > 2) {
4708c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
4718c2ecf20Sopenharmony_ci			   (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_IRQOUT);
4758c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/* Enable the interrupt line from the serial port. */
4788c2ecf20Sopenharmony_ci	outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	/* Unmask the interesting interrupts. */
4818c2ecf20Sopenharmony_ci	write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
4828c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, IMR, ISRh_RxErr);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	lp->tx_unit_busy = 0;
4858c2ecf20Sopenharmony_ci	lp->pac_cnt_in_tx_buf = 0;
4868c2ecf20Sopenharmony_ci	lp->saved_tx_size = 0;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic void trigger_send(long ioaddr, int length)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	write_reg_byte(ioaddr, TxCNT0, length & 0xff);
4928c2ecf20Sopenharmony_ci	write_reg(ioaddr, TxCNT1, length >> 8);
4938c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR1, CMR1_Xmit);
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci    if (length & 1)
4998c2ecf20Sopenharmony_ci    {
5008c2ecf20Sopenharmony_ci    	length++;
5018c2ecf20Sopenharmony_ci    	pad_len++;
5028c2ecf20Sopenharmony_ci    }
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci    outb(EOC+MAR, ioaddr + PAR_DATA);
5058c2ecf20Sopenharmony_ci    if ((data_mode & 1) == 0) {
5068c2ecf20Sopenharmony_ci		/* Write the packet out, starting with the write addr. */
5078c2ecf20Sopenharmony_ci		outb(WrAddr+MAR, ioaddr + PAR_DATA);
5088c2ecf20Sopenharmony_ci		do {
5098c2ecf20Sopenharmony_ci			write_byte_mode0(ioaddr, *packet++);
5108c2ecf20Sopenharmony_ci		} while (--length > pad_len) ;
5118c2ecf20Sopenharmony_ci		do {
5128c2ecf20Sopenharmony_ci			write_byte_mode0(ioaddr, 0);
5138c2ecf20Sopenharmony_ci		} while (--length > 0) ;
5148c2ecf20Sopenharmony_ci    } else {
5158c2ecf20Sopenharmony_ci		/* Write the packet out in slow mode. */
5168c2ecf20Sopenharmony_ci		unsigned char outbyte = *packet++;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci		outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
5198c2ecf20Sopenharmony_ci		outb(WrAddr+MAR, ioaddr + PAR_DATA);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci		outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
5228c2ecf20Sopenharmony_ci		outb(outbyte & 0x0f, ioaddr + PAR_DATA);
5238c2ecf20Sopenharmony_ci		outbyte >>= 4;
5248c2ecf20Sopenharmony_ci		outb(outbyte & 0x0f, ioaddr + PAR_DATA);
5258c2ecf20Sopenharmony_ci		outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
5268c2ecf20Sopenharmony_ci		while (--length > pad_len)
5278c2ecf20Sopenharmony_ci			write_byte_mode1(ioaddr, *packet++);
5288c2ecf20Sopenharmony_ci		while (--length > 0)
5298c2ecf20Sopenharmony_ci			write_byte_mode1(ioaddr, 0);
5308c2ecf20Sopenharmony_ci    }
5318c2ecf20Sopenharmony_ci    /* Terminate the Tx frame.  End of write: ECB. */
5328c2ecf20Sopenharmony_ci    outb(0xff, ioaddr + PAR_DATA);
5338c2ecf20Sopenharmony_ci    outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic void tx_timeout(struct net_device *dev, unsigned int txqueue)
5378c2ecf20Sopenharmony_ci{
5388c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
5418c2ecf20Sopenharmony_ci		   inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
5428c2ecf20Sopenharmony_ci		   :  "IRQ conflict");
5438c2ecf20Sopenharmony_ci	dev->stats.tx_errors++;
5448c2ecf20Sopenharmony_ci	/* Try to restart the adapter. */
5458c2ecf20Sopenharmony_ci	hardware_init(dev);
5468c2ecf20Sopenharmony_ci	netif_trans_update(dev); /* prevent tx timeout */
5478c2ecf20Sopenharmony_ci	netif_wake_queue(dev);
5488c2ecf20Sopenharmony_ci	dev->stats.tx_errors++;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cistatic netdev_tx_t atp_send_packet(struct sk_buff *skb,
5528c2ecf20Sopenharmony_ci				   struct net_device *dev)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
5558c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
5568c2ecf20Sopenharmony_ci	int length;
5578c2ecf20Sopenharmony_ci	unsigned long flags;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	/* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
5648c2ecf20Sopenharmony_ci	   This sequence must not be interrupted by an incoming packet. */
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	spin_lock_irqsave(&lp->lock, flags);
5678c2ecf20Sopenharmony_ci	write_reg(ioaddr, IMR, 0);
5688c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, IMR, 0);
5698c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&lp->lock, flags);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	lp->pac_cnt_in_tx_buf++;
5748c2ecf20Sopenharmony_ci	if (lp->tx_unit_busy == 0) {
5758c2ecf20Sopenharmony_ci		trigger_send(ioaddr, length);
5768c2ecf20Sopenharmony_ci		lp->saved_tx_size = 0; 				/* Redundant */
5778c2ecf20Sopenharmony_ci		lp->re_tx = 0;
5788c2ecf20Sopenharmony_ci		lp->tx_unit_busy = 1;
5798c2ecf20Sopenharmony_ci	} else
5808c2ecf20Sopenharmony_ci		lp->saved_tx_size = length;
5818c2ecf20Sopenharmony_ci	/* Re-enable the LPT interrupts. */
5828c2ecf20Sopenharmony_ci	write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
5838c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, IMR, ISRh_RxErr);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	dev_kfree_skb (skb);
5868c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/* The typical workload of the driver:
5918c2ecf20Sopenharmony_ci   Handle the network interface interrupts. */
5928c2ecf20Sopenharmony_cistatic irqreturn_t atp_interrupt(int irq, void *dev_instance)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct net_device *dev = dev_instance;
5958c2ecf20Sopenharmony_ci	struct net_local *lp;
5968c2ecf20Sopenharmony_ci	long ioaddr;
5978c2ecf20Sopenharmony_ci	static int num_tx_since_rx;
5988c2ecf20Sopenharmony_ci	int boguscount = max_interrupt_work;
5998c2ecf20Sopenharmony_ci	int handled = 0;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	ioaddr = dev->base_addr;
6028c2ecf20Sopenharmony_ci	lp = netdev_priv(dev);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	spin_lock(&lp->lock);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	/* Disable additional spurious interrupts. */
6078c2ecf20Sopenharmony_ci	outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	/* The adapter's output is currently the IRQ line, switch it to data. */
6108c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_NULL);
6118c2ecf20Sopenharmony_ci	write_reg(ioaddr, IMR, 0);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	if (net_debug > 5)
6148c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "%s: In interrupt ", dev->name);
6158c2ecf20Sopenharmony_ci	while (--boguscount > 0) {
6168c2ecf20Sopenharmony_ci		int status = read_nibble(ioaddr, ISR);
6178c2ecf20Sopenharmony_ci		if (net_debug > 5)
6188c2ecf20Sopenharmony_ci			printk("loop status %02x..", status);
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci		if (status & (ISR_RxOK<<3)) {
6218c2ecf20Sopenharmony_ci			handled = 1;
6228c2ecf20Sopenharmony_ci			write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
6238c2ecf20Sopenharmony_ci			do {
6248c2ecf20Sopenharmony_ci				int read_status = read_nibble(ioaddr, CMR1);
6258c2ecf20Sopenharmony_ci				if (net_debug > 6)
6268c2ecf20Sopenharmony_ci					printk("handling Rx packet %02x..", read_status);
6278c2ecf20Sopenharmony_ci				/* We acknowledged the normal Rx interrupt, so if the interrupt
6288c2ecf20Sopenharmony_ci				   is still outstanding we must have a Rx error. */
6298c2ecf20Sopenharmony_ci				if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
6308c2ecf20Sopenharmony_ci					dev->stats.rx_over_errors++;
6318c2ecf20Sopenharmony_ci					/* Set to no-accept mode long enough to remove a packet. */
6328c2ecf20Sopenharmony_ci					write_reg_high(ioaddr, CMR2, CMR2h_OFF);
6338c2ecf20Sopenharmony_ci					net_rx(dev);
6348c2ecf20Sopenharmony_ci					/* Clear the interrupt and return to normal Rx mode. */
6358c2ecf20Sopenharmony_ci					write_reg_high(ioaddr, ISR, ISRh_RxErr);
6368c2ecf20Sopenharmony_ci					write_reg_high(ioaddr, CMR2, lp->addr_mode);
6378c2ecf20Sopenharmony_ci				} else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
6388c2ecf20Sopenharmony_ci					net_rx(dev);
6398c2ecf20Sopenharmony_ci					num_tx_since_rx = 0;
6408c2ecf20Sopenharmony_ci				} else
6418c2ecf20Sopenharmony_ci					break;
6428c2ecf20Sopenharmony_ci			} while (--boguscount > 0);
6438c2ecf20Sopenharmony_ci		} else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
6448c2ecf20Sopenharmony_ci			handled = 1;
6458c2ecf20Sopenharmony_ci			if (net_debug > 6)
6468c2ecf20Sopenharmony_ci				printk("handling Tx done..");
6478c2ecf20Sopenharmony_ci			/* Clear the Tx interrupt.  We should check for too many failures
6488c2ecf20Sopenharmony_ci			   and reinitialize the adapter. */
6498c2ecf20Sopenharmony_ci			write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
6508c2ecf20Sopenharmony_ci			if (status & (ISR_TxErr<<3)) {
6518c2ecf20Sopenharmony_ci				dev->stats.collisions++;
6528c2ecf20Sopenharmony_ci				if (++lp->re_tx > 15) {
6538c2ecf20Sopenharmony_ci					dev->stats.tx_aborted_errors++;
6548c2ecf20Sopenharmony_ci					hardware_init(dev);
6558c2ecf20Sopenharmony_ci					break;
6568c2ecf20Sopenharmony_ci				}
6578c2ecf20Sopenharmony_ci				/* Attempt to retransmit. */
6588c2ecf20Sopenharmony_ci				if (net_debug > 6)  printk("attempting to ReTx");
6598c2ecf20Sopenharmony_ci				write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
6608c2ecf20Sopenharmony_ci			} else {
6618c2ecf20Sopenharmony_ci				/* Finish up the transmit. */
6628c2ecf20Sopenharmony_ci				dev->stats.tx_packets++;
6638c2ecf20Sopenharmony_ci				lp->pac_cnt_in_tx_buf--;
6648c2ecf20Sopenharmony_ci				if ( lp->saved_tx_size) {
6658c2ecf20Sopenharmony_ci					trigger_send(ioaddr, lp->saved_tx_size);
6668c2ecf20Sopenharmony_ci					lp->saved_tx_size = 0;
6678c2ecf20Sopenharmony_ci					lp->re_tx = 0;
6688c2ecf20Sopenharmony_ci				} else
6698c2ecf20Sopenharmony_ci					lp->tx_unit_busy = 0;
6708c2ecf20Sopenharmony_ci				netif_wake_queue(dev);	/* Inform upper layers. */
6718c2ecf20Sopenharmony_ci			}
6728c2ecf20Sopenharmony_ci			num_tx_since_rx++;
6738c2ecf20Sopenharmony_ci		} else if (num_tx_since_rx > 8 &&
6748c2ecf20Sopenharmony_ci			   time_after(jiffies, lp->last_rx_time + HZ)) {
6758c2ecf20Sopenharmony_ci			if (net_debug > 2)
6768c2ecf20Sopenharmony_ci				printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
6778c2ecf20Sopenharmony_ci					   "%ld jiffies status %02x  CMR1 %02x.\n", dev->name,
6788c2ecf20Sopenharmony_ci					   num_tx_since_rx, jiffies - lp->last_rx_time, status,
6798c2ecf20Sopenharmony_ci					   (read_nibble(ioaddr, CMR1) >> 3) & 15);
6808c2ecf20Sopenharmony_ci			dev->stats.rx_missed_errors++;
6818c2ecf20Sopenharmony_ci			hardware_init(dev);
6828c2ecf20Sopenharmony_ci			num_tx_since_rx = 0;
6838c2ecf20Sopenharmony_ci			break;
6848c2ecf20Sopenharmony_ci		} else
6858c2ecf20Sopenharmony_ci			break;
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	/* This following code fixes a rare (and very difficult to track down)
6898c2ecf20Sopenharmony_ci	   problem where the adapter forgets its ethernet address. */
6908c2ecf20Sopenharmony_ci	{
6918c2ecf20Sopenharmony_ci		int i;
6928c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++)
6938c2ecf20Sopenharmony_ci			write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
6948c2ecf20Sopenharmony_ci#if 0 && defined(TIMED_CHECKER)
6958c2ecf20Sopenharmony_ci		mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
6968c2ecf20Sopenharmony_ci#endif
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* Tell the adapter that it can go back to using the output line as IRQ. */
7008c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR2, CMR2_IRQOUT);
7018c2ecf20Sopenharmony_ci	/* Enable the physical interrupt line, which is sure to be low until.. */
7028c2ecf20Sopenharmony_ci	outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
7038c2ecf20Sopenharmony_ci	/* .. we enable the interrupt sources. */
7048c2ecf20Sopenharmony_ci	write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
7058c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, IMR, ISRh_RxErr); 			/* Hmmm, really needed? */
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	spin_unlock(&lp->lock);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	if (net_debug > 5) printk("exiting interrupt.\n");
7108c2ecf20Sopenharmony_ci	return IRQ_RETVAL(handled);
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci#ifdef TIMED_CHECKER
7148c2ecf20Sopenharmony_ci/* This following code fixes a rare (and very difficult to track down)
7158c2ecf20Sopenharmony_ci   problem where the adapter forgets its ethernet address. */
7168c2ecf20Sopenharmony_cistatic void atp_timed_checker(struct timer_list *t)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	struct net_local *lp = from_timer(lp, t, timer);
7198c2ecf20Sopenharmony_ci	struct net_device *dev = lp->dev;
7208c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
7218c2ecf20Sopenharmony_ci	int tickssofar = jiffies - lp->last_rx_time;
7228c2ecf20Sopenharmony_ci	int i;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	spin_lock(&lp->lock);
7258c2ecf20Sopenharmony_ci	if (tickssofar > 2*HZ) {
7268c2ecf20Sopenharmony_ci#if 1
7278c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++)
7288c2ecf20Sopenharmony_ci			write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
7298c2ecf20Sopenharmony_ci		lp->last_rx_time = jiffies;
7308c2ecf20Sopenharmony_ci#else
7318c2ecf20Sopenharmony_ci		for (i = 0; i < 6; i++)
7328c2ecf20Sopenharmony_ci			if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
7338c2ecf20Sopenharmony_ci				{
7348c2ecf20Sopenharmony_ci			struct net_local *lp = netdev_priv(atp_timed_dev);
7358c2ecf20Sopenharmony_ci			write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
7368c2ecf20Sopenharmony_ci			if (i == 2)
7378c2ecf20Sopenharmony_ci			  dev->stats.tx_errors++;
7388c2ecf20Sopenharmony_ci			else if (i == 3)
7398c2ecf20Sopenharmony_ci			  dev->stats.tx_dropped++;
7408c2ecf20Sopenharmony_ci			else if (i == 4)
7418c2ecf20Sopenharmony_ci			  dev->stats.collisions++;
7428c2ecf20Sopenharmony_ci			else
7438c2ecf20Sopenharmony_ci			  dev->stats.rx_errors++;
7448c2ecf20Sopenharmony_ci		  }
7458c2ecf20Sopenharmony_ci#endif
7468c2ecf20Sopenharmony_ci	}
7478c2ecf20Sopenharmony_ci	spin_unlock(&lp->lock);
7488c2ecf20Sopenharmony_ci	lp->timer.expires = jiffies + TIMED_CHECKER;
7498c2ecf20Sopenharmony_ci	add_timer(&lp->timer);
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci#endif
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci/* We have a good packet(s), get it/them out of the buffers. */
7548c2ecf20Sopenharmony_cistatic void net_rx(struct net_device *dev)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
7578c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
7588c2ecf20Sopenharmony_ci	struct rx_header rx_head;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/* Process the received packet. */
7618c2ecf20Sopenharmony_ci	outb(EOC+MAR, ioaddr + PAR_DATA);
7628c2ecf20Sopenharmony_ci	read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
7638c2ecf20Sopenharmony_ci	if (net_debug > 5)
7648c2ecf20Sopenharmony_ci		printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
7658c2ecf20Sopenharmony_ci			   rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
7668c2ecf20Sopenharmony_ci	if ((rx_head.rx_status & 0x77) != 0x01) {
7678c2ecf20Sopenharmony_ci		dev->stats.rx_errors++;
7688c2ecf20Sopenharmony_ci		if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
7698c2ecf20Sopenharmony_ci		else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
7708c2ecf20Sopenharmony_ci		if (net_debug > 3)
7718c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
7728c2ecf20Sopenharmony_ci				   dev->name, rx_head.rx_status);
7738c2ecf20Sopenharmony_ci		if  (rx_head.rx_status & 0x0020) {
7748c2ecf20Sopenharmony_ci			dev->stats.rx_fifo_errors++;
7758c2ecf20Sopenharmony_ci			write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
7768c2ecf20Sopenharmony_ci			write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
7778c2ecf20Sopenharmony_ci		} else if (rx_head.rx_status & 0x0050)
7788c2ecf20Sopenharmony_ci			hardware_init(dev);
7798c2ecf20Sopenharmony_ci		return;
7808c2ecf20Sopenharmony_ci	} else {
7818c2ecf20Sopenharmony_ci		/* Malloc up new buffer. The "-4" omits the FCS (CRC). */
7828c2ecf20Sopenharmony_ci		int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
7838c2ecf20Sopenharmony_ci		struct sk_buff *skb;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci		skb = netdev_alloc_skb(dev, pkt_len + 2);
7868c2ecf20Sopenharmony_ci		if (skb == NULL) {
7878c2ecf20Sopenharmony_ci			dev->stats.rx_dropped++;
7888c2ecf20Sopenharmony_ci			goto done;
7898c2ecf20Sopenharmony_ci		}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci		skb_reserve(skb, 2);	/* Align IP on 16 byte boundaries */
7928c2ecf20Sopenharmony_ci		read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
7938c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, dev);
7948c2ecf20Sopenharmony_ci		netif_rx(skb);
7958c2ecf20Sopenharmony_ci		dev->stats.rx_packets++;
7968c2ecf20Sopenharmony_ci		dev->stats.rx_bytes += pkt_len;
7978c2ecf20Sopenharmony_ci	}
7988c2ecf20Sopenharmony_ci done:
7998c2ecf20Sopenharmony_ci	write_reg(ioaddr, CMR1, CMR1_NextPkt);
8008c2ecf20Sopenharmony_ci	lp->last_rx_time = jiffies;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	if (data_mode <= 3) { /* Mode 0 or 1 */
8068c2ecf20Sopenharmony_ci		outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
8078c2ecf20Sopenharmony_ci		outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
8088c2ecf20Sopenharmony_ci			 ioaddr + PAR_DATA);
8098c2ecf20Sopenharmony_ci		if (data_mode <= 1) { /* Mode 0 or 1 */
8108c2ecf20Sopenharmony_ci			do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0);
8118c2ecf20Sopenharmony_ci		} else { /* Mode 2 or 3 */
8128c2ecf20Sopenharmony_ci			do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0);
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci	} else if (data_mode <= 5) {
8158c2ecf20Sopenharmony_ci		do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0);
8168c2ecf20Sopenharmony_ci	} else {
8178c2ecf20Sopenharmony_ci		do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0);
8188c2ecf20Sopenharmony_ci	}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
8218c2ecf20Sopenharmony_ci	outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci/* The inverse routine to net_open(). */
8258c2ecf20Sopenharmony_cistatic int
8268c2ecf20Sopenharmony_cinet_close(struct net_device *dev)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
8298c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	del_timer_sync(&lp->timer);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	/* Flush the Tx and disable Rx here. */
8368c2ecf20Sopenharmony_ci	lp->addr_mode = CMR2h_OFF;
8378c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR2, CMR2h_OFF);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	/* Free the IRQ line. */
8408c2ecf20Sopenharmony_ci	outb(0x00, ioaddr + PAR_CONTROL);
8418c2ecf20Sopenharmony_ci	free_irq(dev->irq, dev);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	/* Reset the ethernet hardware and activate the printer pass-through. */
8448c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
8458c2ecf20Sopenharmony_ci	return 0;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci/*
8498c2ecf20Sopenharmony_ci *	Set or clear the multicast filter for this adapter.
8508c2ecf20Sopenharmony_ci */
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic void set_rx_mode(struct net_device *dev)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct net_local *lp = netdev_priv(dev);
8558c2ecf20Sopenharmony_ci	long ioaddr = dev->base_addr;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	if (!netdev_mc_empty(dev) || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
8588c2ecf20Sopenharmony_ci		lp->addr_mode = CMR2h_PROMISC;
8598c2ecf20Sopenharmony_ci	else
8608c2ecf20Sopenharmony_ci		lp->addr_mode = CMR2h_Normal;
8618c2ecf20Sopenharmony_ci	write_reg_high(ioaddr, CMR2, lp->addr_mode);
8628c2ecf20Sopenharmony_ci}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic int __init atp_init_module(void) {
8658c2ecf20Sopenharmony_ci	if (debug)					/* Emit version even if no cards detected. */
8668c2ecf20Sopenharmony_ci		printk(KERN_INFO "%s", version);
8678c2ecf20Sopenharmony_ci	return atp_init();
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_cistatic void __exit atp_cleanup_module(void) {
8718c2ecf20Sopenharmony_ci	struct net_device *next_dev;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	while (root_atp_dev) {
8748c2ecf20Sopenharmony_ci		struct net_local *atp_local = netdev_priv(root_atp_dev);
8758c2ecf20Sopenharmony_ci		next_dev = atp_local->next_module;
8768c2ecf20Sopenharmony_ci		unregister_netdev(root_atp_dev);
8778c2ecf20Sopenharmony_ci		/* No need to release_region(), since we never snarf it. */
8788c2ecf20Sopenharmony_ci		free_netdev(root_atp_dev);
8798c2ecf20Sopenharmony_ci		root_atp_dev = next_dev;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci}
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cimodule_init(atp_init_module);
8848c2ecf20Sopenharmony_cimodule_exit(atp_cleanup_module);
885