xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/cirrus/mac89x0.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* mac89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun 	Written 1996 by Russell Nelson, with reference to skeleton.c
4*4882a593Smuzhiyun 	written 1993-1994 by Donald Becker.
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun 	This software may be used and distributed according to the terms
7*4882a593Smuzhiyun 	of the GNU General Public License, incorporated herein by reference.
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun 	The author may be reached at nelson@crynwr.com, Crynwr
10*4882a593Smuzhiyun 	Software, 11 Grant St., Potsdam, NY 13676
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun   Changelog:
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun   Mike Cruse        : mcruse@cti-ltd.com
15*4882a593Smuzhiyun                     : Changes for Linux 2.0 compatibility.
16*4882a593Smuzhiyun                     : Added dev_id parameter in net_interrupt(),
17*4882a593Smuzhiyun                     : request_irq() and free_irq(). Just NULL for now.
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun   Mike Cruse        : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros
20*4882a593Smuzhiyun                     : in net_open() and net_close() so kerneld would know
21*4882a593Smuzhiyun                     : that the module is in use and wouldn't eject the
22*4882a593Smuzhiyun                     : driver prematurely.
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun   Mike Cruse        : Rewrote init_module() and cleanup_module using 8390.c
25*4882a593Smuzhiyun                     : as an example. Disabled autoprobing in init_module(),
26*4882a593Smuzhiyun                     : not a good thing to do to other devices while Linux
27*4882a593Smuzhiyun                     : is running from all accounts.
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun   Alan Cox          : Removed 1.2 support, added 2.1 extra counters.
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun   David Huggins-Daines <dhd@debian.org>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun   Split this off into mac89x0.c, and gutted it of all parts which are
34*4882a593Smuzhiyun   not relevant to the existing CS8900 cards on the Macintosh
35*4882a593Smuzhiyun   (i.e. basically the Daynaport CS and LC cards).  To be precise:
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun     * Removed all the media-detection stuff, because these cards are
38*4882a593Smuzhiyun     TP-only.
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun     * Lobotomized the ISA interrupt bogosity, because these cards use
41*4882a593Smuzhiyun     a hardwired NuBus interrupt and a magic ISAIRQ value in the card.
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun     * Basically eliminated everything not relevant to getting the
44*4882a593Smuzhiyun     cards minimally functioning on the Macintosh.
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun   I might add that these cards are badly designed even from the Mac
47*4882a593Smuzhiyun   standpoint, in that Dayna, in their infinite wisdom, used NuBus slot
48*4882a593Smuzhiyun   I/O space and NuBus interrupts for these cards, but neglected to
49*4882a593Smuzhiyun   provide anything even remotely resembling a NuBus ROM.  Therefore we
50*4882a593Smuzhiyun   have to probe for them in a brain-damaged ISA-like fashion.
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun   Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 11/01/2001
53*4882a593Smuzhiyun   check kmalloc and release the allocated memory on failure in
54*4882a593Smuzhiyun   mac89x0_probe and in init_module
55*4882a593Smuzhiyun   use local_irq_{save,restore}(flags) in net_get_stat, not just
56*4882a593Smuzhiyun   local_irq_{dis,en}able()
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static const char version[] =
62*4882a593Smuzhiyun "cs89x0.c:v1.02 11/26/96 Russell Nelson <nelson@crynwr.com>\n";
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #include <linux/module.h>
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun   Sources:
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	Crynwr packet driver epktisa.
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	Crystal Semiconductor data sheets.
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #include <linux/kernel.h>
76*4882a593Smuzhiyun #include <linux/types.h>
77*4882a593Smuzhiyun #include <linux/fcntl.h>
78*4882a593Smuzhiyun #include <linux/interrupt.h>
79*4882a593Smuzhiyun #include <linux/ioport.h>
80*4882a593Smuzhiyun #include <linux/in.h>
81*4882a593Smuzhiyun #include <linux/string.h>
82*4882a593Smuzhiyun #include <linux/nubus.h>
83*4882a593Smuzhiyun #include <linux/errno.h>
84*4882a593Smuzhiyun #include <linux/init.h>
85*4882a593Smuzhiyun #include <linux/netdevice.h>
86*4882a593Smuzhiyun #include <linux/platform_device.h>
87*4882a593Smuzhiyun #include <linux/etherdevice.h>
88*4882a593Smuzhiyun #include <linux/skbuff.h>
89*4882a593Smuzhiyun #include <linux/delay.h>
90*4882a593Smuzhiyun #include <linux/bitops.h>
91*4882a593Smuzhiyun #include <linux/gfp.h>
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #include <asm/io.h>
94*4882a593Smuzhiyun #include <asm/hwtest.h>
95*4882a593Smuzhiyun #include <asm/macints.h>
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #include "cs89x0.h"
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static int debug = -1;
100*4882a593Smuzhiyun module_param(debug, int, 0);
101*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "debug message level");
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /* Information that need to be kept for each board. */
104*4882a593Smuzhiyun struct net_local {
105*4882a593Smuzhiyun 	int msg_enable;
106*4882a593Smuzhiyun 	int chip_type;		/* one of: CS8900, CS8920, CS8920M */
107*4882a593Smuzhiyun 	char chip_revision;	/* revision letter of the chip ('A'...) */
108*4882a593Smuzhiyun 	int send_cmd;		/* the propercommand used to send a packet. */
109*4882a593Smuzhiyun 	int rx_mode;
110*4882a593Smuzhiyun 	int curr_rx_cfg;
111*4882a593Smuzhiyun         int send_underrun;      /* keep track of how many underruns in a row we get */
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /* Index to functions, as function prototypes. */
115*4882a593Smuzhiyun static int net_open(struct net_device *dev);
116*4882a593Smuzhiyun static netdev_tx_t net_send_packet(struct sk_buff *skb, struct net_device *dev);
117*4882a593Smuzhiyun static irqreturn_t net_interrupt(int irq, void *dev_id);
118*4882a593Smuzhiyun static void set_multicast_list(struct net_device *dev);
119*4882a593Smuzhiyun static void net_rx(struct net_device *dev);
120*4882a593Smuzhiyun static int net_close(struct net_device *dev);
121*4882a593Smuzhiyun static struct net_device_stats *net_get_stats(struct net_device *dev);
122*4882a593Smuzhiyun static int set_mac_address(struct net_device *dev, void *addr);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /* For reading/writing registers ISA-style */
125*4882a593Smuzhiyun static inline int
readreg_io(struct net_device * dev,int portno)126*4882a593Smuzhiyun readreg_io(struct net_device *dev, int portno)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
129*4882a593Smuzhiyun 	return swab16(nubus_readw(dev->base_addr + DATA_PORT));
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static inline void
writereg_io(struct net_device * dev,int portno,int value)133*4882a593Smuzhiyun writereg_io(struct net_device *dev, int portno, int value)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
136*4882a593Smuzhiyun 	nubus_writew(swab16(value), dev->base_addr + DATA_PORT);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* These are for reading/writing registers in shared memory */
140*4882a593Smuzhiyun static inline int
readreg(struct net_device * dev,int portno)141*4882a593Smuzhiyun readreg(struct net_device *dev, int portno)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	return swab16(nubus_readw(dev->mem_start + portno));
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun static inline void
writereg(struct net_device * dev,int portno,int value)147*4882a593Smuzhiyun writereg(struct net_device *dev, int portno, int value)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	nubus_writew(swab16(value), dev->mem_start + portno);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static const struct net_device_ops mac89x0_netdev_ops = {
153*4882a593Smuzhiyun 	.ndo_open		= net_open,
154*4882a593Smuzhiyun 	.ndo_stop		= net_close,
155*4882a593Smuzhiyun 	.ndo_start_xmit		= net_send_packet,
156*4882a593Smuzhiyun 	.ndo_get_stats		= net_get_stats,
157*4882a593Smuzhiyun 	.ndo_set_rx_mode	= set_multicast_list,
158*4882a593Smuzhiyun 	.ndo_set_mac_address	= set_mac_address,
159*4882a593Smuzhiyun 	.ndo_validate_addr	= eth_validate_addr,
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /* Probe for the CS8900 card in slot E.  We won't bother looking
163*4882a593Smuzhiyun    anywhere else until we have a really good reason to do so. */
mac89x0_device_probe(struct platform_device * pdev)164*4882a593Smuzhiyun static int mac89x0_device_probe(struct platform_device *pdev)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	struct net_device *dev;
167*4882a593Smuzhiyun 	struct net_local *lp;
168*4882a593Smuzhiyun 	int i, slot;
169*4882a593Smuzhiyun 	unsigned rev_type = 0;
170*4882a593Smuzhiyun 	unsigned long ioaddr;
171*4882a593Smuzhiyun 	unsigned short sig;
172*4882a593Smuzhiyun 	int err = -ENODEV;
173*4882a593Smuzhiyun 	struct nubus_rsrc *fres;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	dev = alloc_etherdev(sizeof(struct net_local));
176*4882a593Smuzhiyun 	if (!dev)
177*4882a593Smuzhiyun 		return -ENOMEM;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* We might have to parameterize this later */
180*4882a593Smuzhiyun 	slot = 0xE;
181*4882a593Smuzhiyun 	/* Get out now if there's a real NuBus card in slot E */
182*4882a593Smuzhiyun 	for_each_func_rsrc(fres)
183*4882a593Smuzhiyun 		if (fres->board->slot == slot)
184*4882a593Smuzhiyun 			goto out;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	/* The pseudo-ISA bits always live at offset 0x300 (gee,
187*4882a593Smuzhiyun            wonder why...) */
188*4882a593Smuzhiyun 	ioaddr = (unsigned long)
189*4882a593Smuzhiyun 		nubus_slot_addr(slot) | (((slot&0xf) << 20) + DEFAULTIOBASE);
190*4882a593Smuzhiyun 	{
191*4882a593Smuzhiyun 		int card_present;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		card_present = (hwreg_present((void *)ioaddr + 4) &&
194*4882a593Smuzhiyun 				hwreg_present((void *)ioaddr + DATA_PORT));
195*4882a593Smuzhiyun 		if (!card_present)
196*4882a593Smuzhiyun 			goto out;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	nubus_writew(0, ioaddr + ADD_PORT);
200*4882a593Smuzhiyun 	sig = nubus_readw(ioaddr + DATA_PORT);
201*4882a593Smuzhiyun 	if (sig != swab16(CHIP_EISA_ID_SIG))
202*4882a593Smuzhiyun 		goto out;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	SET_NETDEV_DEV(dev, &pdev->dev);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Initialize the net_device structure. */
207*4882a593Smuzhiyun 	lp = netdev_priv(dev);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	lp->msg_enable = netif_msg_init(debug, 0);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/* Fill in the 'dev' fields. */
212*4882a593Smuzhiyun 	dev->base_addr = ioaddr;
213*4882a593Smuzhiyun 	dev->mem_start = (unsigned long)
214*4882a593Smuzhiyun 		nubus_slot_addr(slot) | (((slot&0xf) << 20) + MMIOBASE);
215*4882a593Smuzhiyun 	dev->mem_end = dev->mem_start + 0x1000;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* Turn on shared memory */
218*4882a593Smuzhiyun 	writereg_io(dev, PP_BusCTL, MEMORY_ON);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	/* get the chip type */
221*4882a593Smuzhiyun 	rev_type = readreg(dev, PRODUCT_ID_ADD);
222*4882a593Smuzhiyun 	lp->chip_type = rev_type &~ REVISON_BITS;
223*4882a593Smuzhiyun 	lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* Check the chip type and revision in order to set the correct send command
226*4882a593Smuzhiyun 	CS8920 revision C and CS8900 revision F can use the faster send. */
227*4882a593Smuzhiyun 	lp->send_cmd = TX_AFTER_381;
228*4882a593Smuzhiyun 	if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
229*4882a593Smuzhiyun 		lp->send_cmd = TX_NOW;
230*4882a593Smuzhiyun 	if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
231*4882a593Smuzhiyun 		lp->send_cmd = TX_NOW;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	netif_dbg(lp, drv, dev, "%s", version);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	pr_info("cs89%c0%s rev %c found at %#8lx\n",
236*4882a593Smuzhiyun 		lp->chip_type == CS8900 ? '0' : '2',
237*4882a593Smuzhiyun 		lp->chip_type == CS8920M ? "M" : "",
238*4882a593Smuzhiyun 		lp->chip_revision, dev->base_addr);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	/* Try to read the MAC address */
241*4882a593Smuzhiyun 	if ((readreg(dev, PP_SelfST) & (EEPROM_PRESENT | EEPROM_OK)) == 0) {
242*4882a593Smuzhiyun 		pr_info("No EEPROM, giving up now.\n");
243*4882a593Smuzhiyun 		goto out1;
244*4882a593Smuzhiyun         } else {
245*4882a593Smuzhiyun                 for (i = 0; i < ETH_ALEN; i += 2) {
246*4882a593Smuzhiyun 			/* Big-endian (why??!) */
247*4882a593Smuzhiyun 			unsigned short s = readreg(dev, PP_IA + i);
248*4882a593Smuzhiyun                         dev->dev_addr[i] = s >> 8;
249*4882a593Smuzhiyun                         dev->dev_addr[i+1] = s & 0xff;
250*4882a593Smuzhiyun                 }
251*4882a593Smuzhiyun         }
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	dev->irq = SLOT2IRQ(slot);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* print the IRQ and ethernet address. */
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	pr_info("MAC %pM, IRQ %d\n", dev->dev_addr, dev->irq);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	dev->netdev_ops		= &mac89x0_netdev_ops;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	err = register_netdev(dev);
262*4882a593Smuzhiyun 	if (err)
263*4882a593Smuzhiyun 		goto out1;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	platform_set_drvdata(pdev, dev);
266*4882a593Smuzhiyun 	return 0;
267*4882a593Smuzhiyun out1:
268*4882a593Smuzhiyun 	nubus_writew(0, dev->base_addr + ADD_PORT);
269*4882a593Smuzhiyun out:
270*4882a593Smuzhiyun 	free_netdev(dev);
271*4882a593Smuzhiyun 	return err;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /* Open/initialize the board.  This is called (in the current kernel)
275*4882a593Smuzhiyun    sometime after booting when the 'ifconfig' program is run.
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun    This routine should set everything up anew at each open, even
278*4882a593Smuzhiyun    registers that "should" only need to be set once at boot, so that
279*4882a593Smuzhiyun    there is non-reboot way to recover if something goes wrong.
280*4882a593Smuzhiyun    */
281*4882a593Smuzhiyun static int
net_open(struct net_device * dev)282*4882a593Smuzhiyun net_open(struct net_device *dev)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	struct net_local *lp = netdev_priv(dev);
285*4882a593Smuzhiyun 	int i;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/* Disable the interrupt for now */
288*4882a593Smuzhiyun 	writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) & ~ENABLE_IRQ);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* Grab the interrupt */
291*4882a593Smuzhiyun 	if (request_irq(dev->irq, net_interrupt, 0, "cs89x0", dev))
292*4882a593Smuzhiyun 		return -EAGAIN;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* Set up the IRQ - Apparently magic */
295*4882a593Smuzhiyun 	if (lp->chip_type == CS8900)
296*4882a593Smuzhiyun 		writereg(dev, PP_CS8900_ISAINT, 0);
297*4882a593Smuzhiyun 	else
298*4882a593Smuzhiyun 		writereg(dev, PP_CS8920_ISAINT, 0);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* set the Ethernet address */
301*4882a593Smuzhiyun 	for (i=0; i < ETH_ALEN/2; i++)
302*4882a593Smuzhiyun 		writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	/* Turn on both receive and transmit operations */
305*4882a593Smuzhiyun 	writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/* Receive only error free packets addressed to this card */
308*4882a593Smuzhiyun 	lp->rx_mode = 0;
309*4882a593Smuzhiyun 	writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
316*4882a593Smuzhiyun 	       TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
319*4882a593Smuzhiyun 		 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/* now that we've got our act together, enable everything */
322*4882a593Smuzhiyun 	writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
323*4882a593Smuzhiyun 	netif_start_queue(dev);
324*4882a593Smuzhiyun 	return 0;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun static netdev_tx_t
net_send_packet(struct sk_buff * skb,struct net_device * dev)328*4882a593Smuzhiyun net_send_packet(struct sk_buff *skb, struct net_device *dev)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun 	struct net_local *lp = netdev_priv(dev);
331*4882a593Smuzhiyun 	unsigned long flags;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	netif_dbg(lp, tx_queued, dev, "sent %d byte packet of type %x\n",
334*4882a593Smuzhiyun 		  skb->len, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
335*4882a593Smuzhiyun 		  skb->data[ETH_ALEN + ETH_ALEN + 1]);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	/* keep the upload from being interrupted, since we
338*4882a593Smuzhiyun 	   ask the chip to start transmitting before the
339*4882a593Smuzhiyun 	   whole packet has been completely uploaded. */
340*4882a593Smuzhiyun 	local_irq_save(flags);
341*4882a593Smuzhiyun 	netif_stop_queue(dev);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	/* initiate a transmit sequence */
344*4882a593Smuzhiyun 	writereg(dev, PP_TxCMD, lp->send_cmd);
345*4882a593Smuzhiyun 	writereg(dev, PP_TxLength, skb->len);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* Test to see if the chip has allocated memory for the packet */
348*4882a593Smuzhiyun 	if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
349*4882a593Smuzhiyun 		/* Gasp!  It hasn't.  But that shouldn't happen since
350*4882a593Smuzhiyun 		   we're waiting for TxOk, so return 1 and requeue this packet. */
351*4882a593Smuzhiyun 		local_irq_restore(flags);
352*4882a593Smuzhiyun 		return NETDEV_TX_BUSY;
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	/* Write the contents of the packet */
356*4882a593Smuzhiyun 	skb_copy_from_linear_data(skb, (void *)(dev->mem_start + PP_TxFrame),
357*4882a593Smuzhiyun 				  skb->len+1);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	local_irq_restore(flags);
360*4882a593Smuzhiyun 	dev_kfree_skb (skb);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	return NETDEV_TX_OK;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /* The typical workload of the driver:
366*4882a593Smuzhiyun    Handle the network interface interrupts. */
net_interrupt(int irq,void * dev_id)367*4882a593Smuzhiyun static irqreturn_t net_interrupt(int irq, void *dev_id)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	struct net_device *dev = dev_id;
370*4882a593Smuzhiyun 	struct net_local *lp;
371*4882a593Smuzhiyun 	int ioaddr, status;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	ioaddr = dev->base_addr;
374*4882a593Smuzhiyun 	lp = netdev_priv(dev);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	/* we MUST read all the events out of the ISQ, otherwise we'll never
377*4882a593Smuzhiyun            get interrupted again.  As a consequence, we can't have any limit
378*4882a593Smuzhiyun            on the number of times we loop in the interrupt handler.  The
379*4882a593Smuzhiyun            hardware guarantees that eventually we'll run out of events.  Of
380*4882a593Smuzhiyun            course, if you're on a slow machine, and packets are arriving
381*4882a593Smuzhiyun            faster than you can read them off, you're screwed.  Hasta la
382*4882a593Smuzhiyun            vista, baby!  */
383*4882a593Smuzhiyun 	while ((status = swab16(nubus_readw(dev->base_addr + ISQ_PORT)))) {
384*4882a593Smuzhiyun 		netif_dbg(lp, intr, dev, "status=%04x\n", status);
385*4882a593Smuzhiyun 		switch(status & ISQ_EVENT_MASK) {
386*4882a593Smuzhiyun 		case ISQ_RECEIVER_EVENT:
387*4882a593Smuzhiyun 			/* Got a packet(s). */
388*4882a593Smuzhiyun 			net_rx(dev);
389*4882a593Smuzhiyun 			break;
390*4882a593Smuzhiyun 		case ISQ_TRANSMITTER_EVENT:
391*4882a593Smuzhiyun 			dev->stats.tx_packets++;
392*4882a593Smuzhiyun 			netif_wake_queue(dev);
393*4882a593Smuzhiyun 			if ((status & TX_OK) == 0)
394*4882a593Smuzhiyun 				dev->stats.tx_errors++;
395*4882a593Smuzhiyun 			if (status & TX_LOST_CRS)
396*4882a593Smuzhiyun 				dev->stats.tx_carrier_errors++;
397*4882a593Smuzhiyun 			if (status & TX_SQE_ERROR)
398*4882a593Smuzhiyun 				dev->stats.tx_heartbeat_errors++;
399*4882a593Smuzhiyun 			if (status & TX_LATE_COL)
400*4882a593Smuzhiyun 				dev->stats.tx_window_errors++;
401*4882a593Smuzhiyun 			if (status & TX_16_COL)
402*4882a593Smuzhiyun 				dev->stats.tx_aborted_errors++;
403*4882a593Smuzhiyun 			break;
404*4882a593Smuzhiyun 		case ISQ_BUFFER_EVENT:
405*4882a593Smuzhiyun 			if (status & READY_FOR_TX) {
406*4882a593Smuzhiyun 				/* we tried to transmit a packet earlier,
407*4882a593Smuzhiyun                                    but inexplicably ran out of buffers.
408*4882a593Smuzhiyun                                    That shouldn't happen since we only ever
409*4882a593Smuzhiyun                                    load one packet.  Shrug.  Do the right
410*4882a593Smuzhiyun                                    thing anyway. */
411*4882a593Smuzhiyun 				netif_wake_queue(dev);
412*4882a593Smuzhiyun 			}
413*4882a593Smuzhiyun 			if (status & TX_UNDERRUN) {
414*4882a593Smuzhiyun 				netif_dbg(lp, tx_err, dev, "transmit underrun\n");
415*4882a593Smuzhiyun                                 lp->send_underrun++;
416*4882a593Smuzhiyun                                 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
417*4882a593Smuzhiyun                                 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
418*4882a593Smuzhiyun                         }
419*4882a593Smuzhiyun 			break;
420*4882a593Smuzhiyun 		case ISQ_RX_MISS_EVENT:
421*4882a593Smuzhiyun 			dev->stats.rx_missed_errors += (status >> 6);
422*4882a593Smuzhiyun 			break;
423*4882a593Smuzhiyun 		case ISQ_TX_COL_EVENT:
424*4882a593Smuzhiyun 			dev->stats.collisions += (status >> 6);
425*4882a593Smuzhiyun 			break;
426*4882a593Smuzhiyun 		}
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 	return IRQ_HANDLED;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun /* We have a good packet(s), get it/them out of the buffers. */
432*4882a593Smuzhiyun static void
net_rx(struct net_device * dev)433*4882a593Smuzhiyun net_rx(struct net_device *dev)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	struct net_local *lp = netdev_priv(dev);
436*4882a593Smuzhiyun 	struct sk_buff *skb;
437*4882a593Smuzhiyun 	int status, length;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	status = readreg(dev, PP_RxStatus);
440*4882a593Smuzhiyun 	if ((status & RX_OK) == 0) {
441*4882a593Smuzhiyun 		dev->stats.rx_errors++;
442*4882a593Smuzhiyun 		if (status & RX_RUNT)
443*4882a593Smuzhiyun 				dev->stats.rx_length_errors++;
444*4882a593Smuzhiyun 		if (status & RX_EXTRA_DATA)
445*4882a593Smuzhiyun 				dev->stats.rx_length_errors++;
446*4882a593Smuzhiyun 		if ((status & RX_CRC_ERROR) &&
447*4882a593Smuzhiyun 		    !(status & (RX_EXTRA_DATA|RX_RUNT)))
448*4882a593Smuzhiyun 			/* per str 172 */
449*4882a593Smuzhiyun 			dev->stats.rx_crc_errors++;
450*4882a593Smuzhiyun 		if (status & RX_DRIBBLE)
451*4882a593Smuzhiyun 				dev->stats.rx_frame_errors++;
452*4882a593Smuzhiyun 		return;
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	length = readreg(dev, PP_RxLength);
456*4882a593Smuzhiyun 	/* Malloc up new buffer. */
457*4882a593Smuzhiyun 	skb = alloc_skb(length, GFP_ATOMIC);
458*4882a593Smuzhiyun 	if (skb == NULL) {
459*4882a593Smuzhiyun 		dev->stats.rx_dropped++;
460*4882a593Smuzhiyun 		return;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun 	skb_put(skb, length);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	skb_copy_to_linear_data(skb, (void *)(dev->mem_start + PP_RxFrame),
465*4882a593Smuzhiyun 				length);
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	netif_dbg(lp, rx_status, dev, "received %d byte packet of type %x\n",
468*4882a593Smuzhiyun 		  length, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
469*4882a593Smuzhiyun 		  skb->data[ETH_ALEN + ETH_ALEN + 1]);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun         skb->protocol=eth_type_trans(skb,dev);
472*4882a593Smuzhiyun 	netif_rx(skb);
473*4882a593Smuzhiyun 	dev->stats.rx_packets++;
474*4882a593Smuzhiyun 	dev->stats.rx_bytes += length;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun /* The inverse routine to net_open(). */
478*4882a593Smuzhiyun static int
net_close(struct net_device * dev)479*4882a593Smuzhiyun net_close(struct net_device *dev)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	writereg(dev, PP_RxCFG, 0);
483*4882a593Smuzhiyun 	writereg(dev, PP_TxCFG, 0);
484*4882a593Smuzhiyun 	writereg(dev, PP_BufCFG, 0);
485*4882a593Smuzhiyun 	writereg(dev, PP_BusCTL, 0);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	netif_stop_queue(dev);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	free_irq(dev->irq, dev);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* Update the statistics here. */
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	return 0;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun /* Get the current statistics.	This may be called with the card open or
498*4882a593Smuzhiyun    closed. */
499*4882a593Smuzhiyun static struct net_device_stats *
net_get_stats(struct net_device * dev)500*4882a593Smuzhiyun net_get_stats(struct net_device *dev)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun 	unsigned long flags;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	local_irq_save(flags);
505*4882a593Smuzhiyun 	/* Update the statistics from the device registers. */
506*4882a593Smuzhiyun 	dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
507*4882a593Smuzhiyun 	dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
508*4882a593Smuzhiyun 	local_irq_restore(flags);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	return &dev->stats;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
set_multicast_list(struct net_device * dev)513*4882a593Smuzhiyun static void set_multicast_list(struct net_device *dev)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	struct net_local *lp = netdev_priv(dev);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if(dev->flags&IFF_PROMISC)
518*4882a593Smuzhiyun 	{
519*4882a593Smuzhiyun 		lp->rx_mode = RX_ALL_ACCEPT;
520*4882a593Smuzhiyun 	} else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
521*4882a593Smuzhiyun 		/* The multicast-accept list is initialized to accept-all, and we
522*4882a593Smuzhiyun 		   rely on higher-level filtering for now. */
523*4882a593Smuzhiyun 		lp->rx_mode = RX_MULTCAST_ACCEPT;
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun 	else
526*4882a593Smuzhiyun 		lp->rx_mode = 0;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* in promiscuous mode, we accept errored packets, so we have to enable interrupts on them also */
531*4882a593Smuzhiyun 	writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
532*4882a593Smuzhiyun 	     (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 
set_mac_address(struct net_device * dev,void * addr)536*4882a593Smuzhiyun static int set_mac_address(struct net_device *dev, void *addr)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun 	struct sockaddr *saddr = addr;
539*4882a593Smuzhiyun 	int i;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (!is_valid_ether_addr(saddr->sa_data))
542*4882a593Smuzhiyun 		return -EADDRNOTAVAIL;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
545*4882a593Smuzhiyun 	netdev_info(dev, "Setting MAC address to %pM\n", dev->dev_addr);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	/* set the Ethernet address */
548*4882a593Smuzhiyun 	for (i=0; i < ETH_ALEN/2; i++)
549*4882a593Smuzhiyun 		writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	return 0;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun MODULE_LICENSE("GPL");
555*4882a593Smuzhiyun 
mac89x0_device_remove(struct platform_device * pdev)556*4882a593Smuzhiyun static int mac89x0_device_remove(struct platform_device *pdev)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	struct net_device *dev = platform_get_drvdata(pdev);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	unregister_netdev(dev);
561*4882a593Smuzhiyun 	nubus_writew(0, dev->base_addr + ADD_PORT);
562*4882a593Smuzhiyun 	free_netdev(dev);
563*4882a593Smuzhiyun 	return 0;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun static struct platform_driver mac89x0_platform_driver = {
567*4882a593Smuzhiyun 	.probe = mac89x0_device_probe,
568*4882a593Smuzhiyun 	.remove = mac89x0_device_remove,
569*4882a593Smuzhiyun 	.driver = {
570*4882a593Smuzhiyun 		.name = "mac89x0",
571*4882a593Smuzhiyun 	},
572*4882a593Smuzhiyun };
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun module_platform_driver(mac89x0_platform_driver);
575