File: /usr/src/linux/arch/cris/drivers/lpslave/e100lpslavenet.c

1     /* $Id: e100lpslavenet.c,v 1.4 2001/06/21 16:55:26 olof Exp $
2      *
3      * e100lpslavenet.c: A network driver for the ETRAX 100LX slave controller.
4      *
5      * Copyright (c) 1998-2001 Axis Communications AB.
6      *
7      * The outline of this driver comes from skeleton.c.
8      *
9      * $Log: e100lpslavenet.c,v $
10      * Revision 1.4  2001/06/21 16:55:26  olof
11      * Minimized par port setup time to gain bandwidth
12      *
13      * Revision 1.3  2001/06/21 15:49:02  olof
14      * Removed setting of default MAC address
15      *
16      * Revision 1.2  2001/06/11 15:39:52  olof
17      * Clean up and sync with ethernet.c rev 1.16. Increased reset time of slave.
18      *
19      * Revision 1.1  2001/06/06 08:56:26  olof
20      * Added support for slave Etrax defined by CONFIG_ETRAX_ETHERNET_LPSLAVE
21      *
22      */
23     
24     #include <linux/config.h>
25     
26     #include <linux/module.h>
27     
28     #include <linux/kernel.h>
29     #include <linux/sched.h>
30     #include <linux/delay.h>
31     #include <linux/types.h>
32     #include <linux/fcntl.h>
33     #include <linux/interrupt.h>
34     #include <linux/ptrace.h>
35     #include <linux/ioport.h>
36     #include <linux/in.h>
37     #include <linux/slab.h>
38     #include <linux/string.h>
39     #include <linux/spinlock.h>
40     #include <linux/errno.h>
41     #include <linux/init.h>
42     
43     #include <linux/netdevice.h>
44     #include <linux/etherdevice.h>
45     #include <linux/skbuff.h>
46     
47     #include <asm/svinto.h>     /* DMA and register descriptions */
48     #include <asm/io.h>         /* LED_* I/O functions */
49     #include <asm/irq.h>
50     #include <asm/dma.h>
51     #include <asm/system.h>
52     #include <asm/bitops.h>
53     
54     #include "e100lpslave.h"
55     
56     /* #define ETHDEBUG */
57     #define D(x)
58     
59     /*
60      * The name of the card. Is used for messages and in the requests for
61      * io regions, irqs and dma channels
62      */
63     
64     static const char* cardname = "Etrax 100LX ethernet slave controller";
65     
66     /* A default ethernet address. Highlevel SW will set the real one later */
67     
68     static struct sockaddr default_mac = {
69     	0,
70             { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
71     };
72     
73     /* Information that need to be kept for each board. */
74     struct net_local {
75     	struct net_device_stats stats;
76     
77     	/* Tx control lock.  This protects the transmit buffer ring
78     	 * state along with the "tx full" state of the driver.  This
79     	 * means all netif_queue flow control actions are protected
80     	 * by this lock as well.
81     	 */
82     	spinlock_t lock;
83     };
84     
85     /* Dma descriptors etc. */
86     
87     #define RX_BUF_SIZE 32768
88     #define ETHER_HEAD_LEN      14
89     
90     #define PAR0_ECP_IRQ_NBR    4
91     
92     #define RX_DESC_BUF_SIZE   256
93     #define NBR_OF_RX_DESC     (RX_BUF_SIZE / \
94     			    RX_DESC_BUF_SIZE)
95     
96     /* Size of slave etrax boot image */
97     #define ETRAX_PAR_BOOT_LENGTH 784
98     
99     static etrax_dma_descr *myNextRxDesc;  /* Points to the next descriptor to
100     					  to be processed */
101     static etrax_dma_descr *myLastRxDesc;  /* The last processed descriptor */
102     static etrax_dma_descr *myPrevRxDesc;  /* The descriptor right before myNextRxDesc */
103     
104     static unsigned char RxBuf[RX_BUF_SIZE];
105     
106     static etrax_dma_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(4)));
107     static etrax_dma_descr TxDescList[3] __attribute__ ((aligned(4)));
108                            /* host command, data, bogus ECP command */
109     
110     static struct sk_buff *tx_skb;
111     
112     /* Index to functions, as function prototypes. */
113     
114     static int etrax_ethernet_lpslave_init(struct net_device *dev);
115     
116     static int e100_open(struct net_device *dev);
117     static int e100_set_mac_address(struct net_device *dev, void *addr);
118     static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
119     static void e100rx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
120     static void e100tx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
121     static void ecp_interrupt(int irq, void *dev_id, struct pt_regs *regs);
122     static void e100_rx(struct net_device *dev);
123     static int e100_close(struct net_device *dev);
124     static struct net_device_stats *e100_get_stats(struct net_device *dev);
125     static void set_multicast_list(struct net_device *dev);
126     static void e100_hardware_send_packet(unsigned long hostcmd, char *buf, int length);
127     static void update_rx_stats(struct net_device_stats *);
128     static void update_tx_stats(struct net_device_stats *);
129     static void e100_reset_tranceiver(void);
130     
131     static void boot_slave(unsigned char *code);
132     
133     #ifdef ETHDEBUG
134     static void dump_parport_status(void);
135     #endif
136     
137     #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
138     
139     static unsigned long host_command;
140     extern unsigned char e100lpslaveprog;
141     
142     /*
143      * This driver uses PAR0 to recevice data from slave ETRAX and PAR1 to boot
144      * and send data to slave ETRAX.
145      * Used ETRAX100 DMAchannels with corresponding IRQ:
146      * PAR0 RX : DMA3 - IRQ 19
147      * PAR1 TX : DMA4 - IRQ 20
148      * IRQ 4 is used to detect ECP commands from slave ETRAX
149      *
150      * NOTE! PAR0 and PAR1 shares DMA and IRQ numbers with SER2 and SER3 
151      */
152     
153     
154     /*
155      * Check for a network adaptor of this type, and return '0' if one exists.
156      * If dev->base_addr == 0, probe all likely locations.
157      * If dev->base_addr == 1, always return failure.
158      * If dev->base_addr == 2, allocate space for the device and return success
159      * (detachable devices only).
160      */
161     static int __init
162     etrax_ethernet_lpslave_init(struct net_device *dev)
163     {
164     	int i;
165     	int anOffset = 0;
166     
167     	printk("Etrax/100 lpslave ethernet driver v0.3, (c) 1999 Axis Communications AB\n");
168     
169     	dev->base_addr = 2;
170     
171     	printk("%s initialized\n", dev->name);
172     
173     	/* make Linux aware of the new hardware  */
174     
175     	if (!dev) {
176     		printk(KERN_WARNING "%s: dev == NULL. Should this happen?\n",
177                            cardname);
178     		dev = init_etherdev(dev, sizeof(struct net_local));
179     		if (!dev)
180     			panic("init_etherdev failed\n");
181     	}
182     
183     	/* setup generic handlers and stuff in the dev struct */
184     
185     	ether_setup(dev);
186     
187     	/* make room for the local structure containing stats etc */
188     
189     	dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
190     	if (dev->priv == NULL)
191     		return -ENOMEM;
192     	memset(dev->priv, 0, sizeof(struct net_local));
193     
194     	/* now setup our etrax specific stuff */
195     
196     	dev->irq = DMA3_RX_IRQ_NBR; /* we really use DMATX as well... */
197             dev->dma = PAR0_RX_DMA_NBR;
198     
199     	/* fill in our handlers so the network layer can talk to us in the future */
200     
201     	dev->open               = e100_open;
202     	dev->hard_start_xmit    = e100_send_packet;
203     	dev->stop               = e100_close;
204     	dev->get_stats          = e100_get_stats;
205     	dev->set_multicast_list = set_multicast_list;
206     	dev->set_mac_address    = e100_set_mac_address;
207     
208     	/* Initialise the list of Etrax DMA-descriptors */
209     
210     	/* Initialise receive descriptors */
211     
212     	for(i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
213     		RxDescList[i].ctrl   = 0;
214     		RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
215     		RxDescList[i].next   = virt_to_phys(&RxDescList[i + 1]);
216     		RxDescList[i].buf    = virt_to_phys(RxBuf + anOffset);
217     		RxDescList[i].status = 0;
218     		RxDescList[i].hw_len = 0;
219     		anOffset += RX_DESC_BUF_SIZE;
220     	}
221     
222     	RxDescList[i].ctrl   = d_eol;
223     	RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
224     	RxDescList[i].next   = virt_to_phys(&RxDescList[0]);
225     	RxDescList[i].buf    = virt_to_phys(RxBuf + anOffset);
226     	RxDescList[i].status = 0;
227     	RxDescList[i].hw_len = 0;
228     
229     	/* Initialise initial pointers */
230     
231     	myNextRxDesc = &RxDescList[0];
232     	myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
233     	myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
234     
235             /* setup some TX descriptor data */
236     
237     	TxDescList[0].sw_len = 4;
238     	TxDescList[0].ctrl = 0;
239     	TxDescList[0].buf = virt_to_phys(&host_command);
240     	TxDescList[0].next = virt_to_phys(&TxDescList[1]);
241     
242     	return 0;
243     }
244     
245     /* set MAC address of the interface. called from the core after a
246      * SIOCSIFADDR ioctl, and from the bootup above.
247      */
248     
249     static int
250     e100_set_mac_address(struct net_device *dev, void *p)
251     {
252     	struct sockaddr *addr = p;
253     	int i;
254     
255     	/* remember it */
256     
257             memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
258     
259     	/* Write it to the hardware.
260     	 * Note the way the address is wrapped:
261     	 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
262     	 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
263     	 */
264     
265             tx_skb = 0;
266     	e100_hardware_send_packet(HOST_CMD_SETMAC, dev->dev_addr, 6);
267     
268     	/* show it in the log as well */
269     
270     	printk("%s: changed MAC to ", dev->name);
271     
272     	for (i = 0; i < 5; i++)
273     		printk("%02X:", dev->dev_addr[i]);
274     
275     	printk("%02X\n", dev->dev_addr[i]);
276     
277     	return 0;
278     }
279     
280     /*
281      * Open/initialize the board. This is called (in the current kernel)
282      * sometime after booting when the 'ifconfig' program is run.
283      *
284      * This routine should set everything up anew at each open, even
285      * registers that "should" only need to be set once at boot, so that
286      * there is non-reboot way to recover if something goes wrong.
287      */
288     
289     static int
290     e100_open(struct net_device *dev)
291     {
292     	unsigned long flags;
293     
294     	/* configure the PAR0 (RX) and PAR1 (TX) ports
295     	 *
296     	 * perror is nAckReverse, which must be 1 at the TX side,
297              * and 0 at the RX side
298              *
299     	 * select is XFlag, which must be 1 at both sides
300     	 */
301     #ifdef ETHDEBUG        
302             printk("Setting up PAR ports\n");
303     #endif
304             *R_PAR0_CONFIG =
305               /* We do not have an external buffer, don't care */
306     		IO_STATE(R_PAR0_CONFIG, ioe,     noninv)    |
307               /* Not connected, don't care */
308     		IO_STATE(R_PAR0_CONFIG, iseli,   noninv)    |
309               /* iautofd is not inverted, noninv */
310     		IO_STATE(R_PAR0_CONFIG, iautofd, noninv)    |
311               /* Not used in reverse direction, don't care */
312     		IO_STATE(R_PAR0_CONFIG, istrb,   noninv)    |
313               /* Not connected, don't care /
314     		IO_STATE(R_PAR0_CONFIG, iinit,   noninv)    |
315               /* perror is GND and reverse wants 0, noninv */
316     		IO_STATE(R_PAR0_CONFIG, iperr,   noninv)    |
317               /* ack is not inverted, noninv */
318     		IO_STATE(R_PAR0_CONFIG, iack,    noninv)    |
319               /* busy is not inverted, noninv */
320     		IO_STATE(R_PAR0_CONFIG, ibusy,   noninv)    |
321               /* fault is not inverted, noninv */
322     		IO_STATE(R_PAR0_CONFIG, ifault,  noninv)    |
323               /* select is Vcc and we want 1, noninv */
324     		IO_STATE(R_PAR0_CONFIG, isel,    noninv)    |
325               /* We will run dma, enable */
326     		IO_STATE(R_PAR0_CONFIG, dma, enable)        |
327               /* No run length encoding, disable */
328     		IO_STATE(R_PAR0_CONFIG, rle_in, disable)    |
329               /* No run length encoding, disable */
330     		IO_STATE(R_PAR0_CONFIG, rle_out, disable)   |
331               /* Enable parallel port */
332     		IO_STATE(R_PAR0_CONFIG, enable, on)         |
333               /* Force mode regardless of pin status */
334     		IO_STATE(R_PAR0_CONFIG, force, on)          |
335               /* We want ECP forward mode since PAR0 is RX */
336     		IO_STATE(R_PAR0_CONFIG, mode, ecp_rev);       
337     
338             *R_PAR1_CONFIG =
339               /* We do not have an external buffer, don't care */
340     		IO_STATE(R_PAR1_CONFIG, ioe,     noninv)    |
341               
342               /* Not connected, don't care */
343     		IO_STATE(R_PAR1_CONFIG, iseli,   noninv)    |
344               
345               /* HostAck must indicate data cycle, noninv */
346     		IO_STATE(R_PAR1_CONFIG, iautofd, noninv)    |
347               
348               /* HostClk has no external inverter, noninv */
349     		IO_STATE(R_PAR1_CONFIG, istrb,   noninv)    |
350               
351               /* Not connected, don't care */
352     		IO_STATE(R_PAR1_CONFIG, iinit,   noninv)    |
353               
354               /* nAckReverse must be 1 in forward mode but is grounded, inv */ 
355     		IO_STATE(R_PAR1_CONFIG, iperr,   inv)       |
356               
357               /* PeriphClk must be 1 in forward mode, noninv */
358     		IO_STATE(R_PAR1_CONFIG, iack,    noninv)    |
359               
360               /* PeriphAck has no external inverter, noninv */
361     		IO_STATE(R_PAR1_CONFIG, ibusy,   noninv)    |
362               
363               /* nPerihpRequest has no external inverter, noniv */
364     		IO_STATE(R_PAR1_CONFIG, ifault,  noninv)    |
365               
366               /* Select is VCC and we want 1, noninv */
367     		IO_STATE(R_PAR1_CONFIG, isel,    noninv)    |
368               
369               /* No EPP mode, disable */
370                     IO_STATE(R_PAR1_CONFIG, ext_mode, disable)  |
371               
372               /* We will run dma, enable */
373                     IO_STATE(R_PAR1_CONFIG, dma, enable)        |
374               
375               /* No run length encoding, disable */
376     		IO_STATE(R_PAR1_CONFIG, rle_in, disable)    |
377               
378               /* No run length encoding, disable */
379     		IO_STATE(R_PAR1_CONFIG, rle_out, disable)   |
380               
381               /* Enable parallel port */
382     		IO_STATE(R_PAR1_CONFIG, enable, on)         |
383               
384               /* Force mode regardless of pin status */
385     		IO_STATE(R_PAR1_CONFIG, force, on)          |
386               
387               /* We want ECP forward mode since PAR1 is TX */
388     	 	IO_STATE(R_PAR1_CONFIG, mode, ecp_fwd);        
389     
390             /* Setup time of value * 160 + 20 ns == 20 ns below */
391             *R_PAR1_DELAY = IO_FIELD(R_PAR1_DELAY, setup, 0);  
392     
393             *R_PAR1_CTRL = 0;
394     
395             while ((((*R_PAR1_STATUS)&0xE000) >> 13) != 5); /* Wait for ECP_FWD mode */
396     #ifdef ETHDEBUG
397             dump_parport_status();
398     #endif
399             
400             /* make sure ECP irq is acked when we enable it below */
401     
402     	(void)*R_PAR0_STATUS_DATA;
403     	(void)*R_PAR1_STATUS_DATA;
404     
405     	/* Reset and wait for the DMA channels */
406     
407             RESET_DMA(4); /* PAR1_TX_DMA_NBR */
408     	RESET_DMA(3); /* PAR0_RX_DMA_NBR */
409     	WAIT_DMA(4);  
410     	WAIT_DMA(3);
411             
412             /* boot the slave Etrax, by sending code on PAR1.
413     	 * do this before we start up the IRQ handlers and stuff,
414     	 * beacuse we simply poll for completion in boot_slave.
415     	 */
416             
417     	boot_slave(&e100lpslaveprog);
418     
419     	/* allocate the irq corresponding to the receiving DMA */
420     
421     	if (request_irq(DMA3_RX_IRQ_NBR, e100rx_interrupt, 0,
422     			cardname, (void *)dev)) {
423               printk("Failed to allocate DMA3_RX_IRQ_NBR\n");
424     		goto grace_exit;
425     	}
426     
427     	/* allocate the irq corresponding to the transmitting DMA */
428     
429     	if (request_irq(DMA4_TX_IRQ_NBR, e100tx_interrupt, 0,
430     			cardname, (void *)dev)) {
431               printk("Failed to allocate DMA4_TX_IRQ_NBR\n");
432               goto grace_exit;
433     	}
434             
435             /* allocate the irq used for detecting ECP commands on the RX port (PAR0) */
436     
437     	if (request_irq(PAR0_ECP_IRQ_NBR, ecp_interrupt, 0,
438     			cardname, (void *)dev)) {
439               printk("Failed to allocate PAR0_ECP_IRQ_NBR\n");
440               grace_exit:          
441                     free_irq(PAR0_ECP_IRQ_NBR, (void *)dev);
442                     free_irq(DMA4_TX_IRQ_NBR, (void *)dev);
443     		free_irq(DMA3_RX_IRQ_NBR, (void *)dev);
444                     
445     		return -EAGAIN;
446     	}
447     
448     #if 0
449             /* We are not allocating DMA since DMA4 is reserved for 'cascading'
450              * and will always fail with the current dma.c
451              */
452             
453     	/*
454     	 * Always allocate the DMA channels after the IRQ,
455     	 * and clean up on failure.
456     	 */
457     
458     	if(request_dma(PAR0_RX_DMA_NBR, cardname)) {
459               printk("Failed to allocate PAR0_RX_DMA_NBR\n");
460     		goto grace_exit;
461     	}
462     
463     	if(request_dma(PAR1_TX_DMA_NBR, cardname)) {
464               printk("Failed to allocate PAR1_TX_DMA_NBR\n");
465     	grace_exit:
466     		/* this will cause some 'trying to free free irq' but what the heck... */
467     
468     		free_dma(PAR1_TX_DMA_NBR);
469                     free_dma(PAR0_RX_DMA_NBR);
470                     free_irq(PAR0_ECP_IRQ_NBR, (void *)dev);
471                     free_irq(DMA4_TX_IRQ_NBR, (void *)dev);
472     		free_irq(DMA3_RX_IRQ_NBR, (void *)dev);
473     		
474     		return -EAGAIN;
475     	}
476     #endif
477             
478     #ifdef ETHDEBUG
479             printk("Par port IRQ and DMA allocated\n");
480     #endif
481     	save_flags(flags);
482     	cli();
483     
484     	/* enable the irq's for PAR0/1 DMA */
485     
486     	*R_IRQ_MASK2_SET =
487     		IO_STATE(R_IRQ_MASK2_SET, dma3_eop, set) |
488     		IO_STATE(R_IRQ_MASK2_SET, dma4_descr, set);
489     
490             *R_IRQ_MASK0_SET =
491     		IO_STATE(R_IRQ_MASK0_SET, par0_ecp_cmd, set);
492     
493     	tx_skb = 0;
494     
495     	/* make sure the irqs are cleared */
496     
497     	*R_DMA_CH3_CLR_INTR = IO_STATE(R_DMA_CH3_CLR_INTR, clr_eop, do);
498     	*R_DMA_CH4_CLR_INTR = IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr, do);
499     
500             /* Write the MAC address to the slave HW */
501     	udelay(5000);
502     	e100_hardware_send_packet(HOST_CMD_SETMAC, dev->dev_addr, 6);
503             
504     	/* make sure the rec and transmit error counters are cleared */
505     
506     	(void)*R_REC_COUNTERS;  /* dummy read */
507     	(void)*R_TR_COUNTERS;   /* dummy read */
508     
509     	/* start the receiving DMA channel so we can receive packets from now on */
510     
511     	*R_DMA_CH3_FIRST = virt_to_phys(myNextRxDesc);
512     	*R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, start);
513     
514     	restore_flags(flags);
515     	
516     	/* We are now ready to accept transmit requeusts from
517     	 * the queueing layer of the networking.
518     	 */
519     #ifdef ETHDEBUG
520             printk("Starting slave network transmit queue\n");
521     #endif
522     	netif_start_queue(dev);
523     
524     	return 0;
525     }
526     
527     static void 
528     e100_reset_tranceiver(void)
529     {
530       /* To do: Reboot and setup slave Etrax */
531     }
532     
533     /* Called by upper layers if they decide it took too long to complete
534      * sending a packet - we need to reset and stuff.
535      */
536     
537     static void
538     e100_tx_timeout(struct net_device *dev)
539     {
540     	struct net_local *np = (struct net_local *)dev->priv;
541     
542     	printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
543     	       tx_done(dev) ? "IRQ problem" : "network cable problem");
544     	
545     	/* remember we got an error */
546     	
547     	np->stats.tx_errors++; 
548     	
549     	/* reset the TX DMA in case it has hung on something */
550     	
551     	RESET_DMA(4);
552     	WAIT_DMA(4);
553     	
554     	/* Reset the tranceiver. */
555     	
556     	e100_reset_tranceiver();
557     	
558     	/* and get rid of the packet that never got an interrupt */
559     	
560     	dev_kfree_skb(tx_skb);
561     	tx_skb = 0;
562     	
563     	/* tell the upper layers we're ok again */
564     	
565     	netif_wake_queue(dev);
566     }
567     
568     
569     /* This will only be invoked if the driver is _not_ in XOFF state.
570      * What this means is that we need not check it, and that this
571      * invariant will hold if we make sure that the netif_*_queue()
572      * calls are done at the proper times.
573      */
574     
575     static int
576     e100_send_packet(struct sk_buff *skb, struct net_device *dev)
577     {
578     	struct net_local *np = (struct net_local *)dev->priv;
579     	int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
580     	unsigned char *buf = skb->data;
581     	
582     #ifdef ETHDEBUG
583             unsigned char *temp_data_ptr = buf;
584             int i;
585             
586     	printk("Sending a packet of length %d:\n", length);
587     	/* dump the first bytes in the packet */
588     	for(i = 0; i < 8; i++) {
589     		printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
590     		       temp_data_ptr[0],temp_data_ptr[1],temp_data_ptr[2],
591                            temp_data_ptr[3],temp_data_ptr[4],temp_data_ptr[5],
592                            temp_data_ptr[6],temp_data_ptr[7]);
593     		temp_data_ptr += 8;
594     	}
595     #endif
596     	spin_lock_irq(&np->lock);  /* protect from tx_interrupt */
597     
598     	tx_skb = skb; /* remember it so we can free it in the tx irq handler later */
599     	dev->trans_start = jiffies;
600     	
601     	e100_hardware_send_packet(HOST_CMD_SENDPACK, buf, length);
602     
603     	/* this simple TX driver has only one send-descriptor so we're full
604     	 * directly. If this had a send-ring instead, we would only do this if
605     	 * the ring got full.
606     	 */
607     
608     	netif_stop_queue(dev);
609             
610     	spin_unlock_irq(&np->lock);
611     
612     	return 0;
613     }
614     
615     /*
616      * The typical workload of the driver:
617      *   Handle the network interface interrupts.
618      */
619     
620     static void
621     e100rx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
622     {
623     	struct net_device *dev = (struct net_device *)dev_id;
624     	unsigned long irqbits = *R_IRQ_MASK2_RD;
625             
626     	if(irqbits & IO_STATE(R_IRQ_MASK2_RD, dma3_eop, active)) {
627     
628     		/* acknowledge the eop interrupt */
629     
630     		*R_DMA_CH3_CLR_INTR = IO_STATE(R_DMA_CH3_CLR_INTR, clr_eop, do);
631     
632     		/* check if one or more complete packets were indeed received */
633     
634     		while(*R_DMA_CH3_FIRST != virt_to_phys(myNextRxDesc)) {
635     			/* Take out the buffer and give it to the OS, then
636     			 * allocate a new buffer to put a packet in.
637     			 */
638     			e100_rx(dev);
639     			((struct net_local *)dev->priv)->stats.rx_packets++;
640     			/* restart/continue on the channel, for safety */
641     			*R_DMA_CH3_CMD = IO_STATE(R_DMA_CH3_CMD, cmd, restart);
642     			/* clear dma channel 3 eop/descr irq bits */
643     			*R_DMA_CH3_CLR_INTR =
644     				IO_STATE(R_DMA_CH3_CLR_INTR, clr_eop, do) |
645     				IO_STATE(R_DMA_CH3_CLR_INTR, clr_descr, do);
646     			
647     			/* now, we might have gotten another packet
648     			   so we have to loop back and check if so */
649     		}
650     	}
651     }
652     
653     /* the transmit dma channel interrupt
654      *
655      * this is supposed to free the skbuff which was pending during transmission,
656      * and inform the kernel that we can send one more buffer
657      */
658     
659     static void
660     e100tx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
661     {
662     	struct net_device *dev = (struct net_device *)dev_id;
663     	unsigned long irqbits = *R_IRQ_MASK2_RD;
664     	struct net_local *np = (struct net_local *)dev->priv;
665     
666     #ifdef ETHDEBUG
667             printk("We got tx interrupt\n");
668     #endif
669     	/* check for a dma4_eop interrupt */
670     	if(irqbits & IO_STATE(R_IRQ_MASK2_RD, dma4_descr, active)) {
671     		/* This protects us from concurrent execution of
672     		 * our dev->hard_start_xmit function above.
673     		 */
674     
675     		spin_lock(&np->lock);
676     		
677     		/* acknowledge the eop interrupt */
678                     
679     		*R_DMA_CH4_CLR_INTR = IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr, do);
680     
681                     /* skip *R_DMA_CH4_FIRST == 0 test since we use d_wait... */
682     		if(tx_skb) {
683     
684     			np->stats.tx_bytes += tx_skb->len;
685     			np->stats.tx_packets++;
686     			/* dma is ready with the transmission of the data in tx_skb, so now we can release the skb memory */
687     			dev_kfree_skb_irq(tx_skb);
688     			tx_skb = 0;
689     			netif_wake_queue(dev);
690     		} else {
691     			printk(KERN_WARNING "%s: tx weird interrupt\n",
692                                    cardname);
693     		}
694     
695     		spin_unlock(&np->lock);
696     	}
697     }
698     
699     static void
700     ecp_interrupt(int irq, void *dev_id, struct pt_regs * regs)
701     {
702     	struct net_device *dev = (struct net_device *)dev_id;
703     	struct net_local *lp = (struct net_local *)dev->priv;
704     	unsigned long temp, irqbits = *R_IRQ_MASK0_RD;
705     
706             /* check for ecp irq */
707     	if(irqbits & IO_MASK(R_IRQ_MASK0_RD, par0_ecp_cmd)) { 
708     		/* acknowledge by reading the bit */
709     		temp = *R_PAR0_STATUS_DATA;
710     		/* force an EOP on the incoming channel, so we'll get an rx interrupt */
711     		*R_SET_EOP = IO_STATE(R_SET_EOP, ch3_eop, set);
712     	}
713     }
714     
715     /* We have a good packet(s), get it/them out of the buffers. */
716     static void
717     e100_rx(struct net_device *dev)
718     {
719     	struct sk_buff *skb;
720     	int length=0;
721     	int i;
722     	struct net_local *np = (struct net_local *)dev->priv;
723     	struct etrax_dma_descr *mySaveRxDesc = myNextRxDesc;
724     	unsigned char *skb_data_ptr;
725     
726     	/* If the packet is broken down in many small packages then merge
727     	 * count how much space we will need to alloc with skb_alloc() for
728     	 * it to fit.
729     	 */
730     
731     	while (!(myNextRxDesc->status & d_eop)) {
732     		length += myNextRxDesc->sw_len; /* use sw_len for the first descs */
733     		myNextRxDesc->status = 0;
734     		myNextRxDesc = phys_to_virt(myNextRxDesc->next);
735     	}
736     
737     	length += myNextRxDesc->hw_len; /* use hw_len for the last descr */
738     
739     #ifdef ETHDEBUG
740     	printk("Got a packet of length %d:\n", length);
741     	/* dump the first bytes in the packet */
742     	skb_data_ptr = (unsigned char *)phys_to_virt(mySaveRxDesc->buf);
743     	for(i = 0; i < 8; i++) {
744     		printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
745     		       skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
746     		       skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
747     		skb_data_ptr += 8;
748     	}
749     #endif
750     
751     	skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
752     	if (!skb) {
753     		np->stats.rx_errors++;
754     		printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
755     		       dev->name);
756     		return;
757     	}
758     
759     	skb_put(skb, length - ETHER_HEAD_LEN);        /* allocate room for the packet body */
760     	skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
761     
762     #ifdef ETHDEBUG
763     	printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
764     	       skb->head, skb->data, skb->tail, skb->end);
765     	printk("copying packet to 0x%x.\n", skb_data_ptr);
766     #endif
767     
768     	/* this loop can be made using max two memcpy's if optimized */
769     
770     	while(mySaveRxDesc != myNextRxDesc) {
771     		memcpy(skb_data_ptr, phys_to_virt(mySaveRxDesc->buf),
772     		       mySaveRxDesc->sw_len);
773     		skb_data_ptr += mySaveRxDesc->sw_len;
774     		mySaveRxDesc = phys_to_virt(mySaveRxDesc->next);
775     	}
776     
777     	memcpy(skb_data_ptr, phys_to_virt(mySaveRxDesc->buf),
778     	       mySaveRxDesc->hw_len);
779     
780     	skb->dev = dev;
781     	skb->protocol = eth_type_trans(skb, dev);
782     
783     	/* Send the packet to the upper layers */
784     
785     	netif_rx(skb);
786     
787     	/* Prepare for next packet */
788     
789     	myNextRxDesc->status = 0;
790     	myPrevRxDesc = myNextRxDesc;
791     	myNextRxDesc = phys_to_virt(myNextRxDesc->next);
792     
793     	myPrevRxDesc->ctrl |= d_eol;
794     	myLastRxDesc->ctrl &= ~d_eol;
795     	myLastRxDesc = myPrevRxDesc;
796     
797     	return;
798     }
799     
800     /* The inverse routine to net_open(). */
801     static int
802     e100_close(struct net_device *dev)
803     {
804     	struct net_local *np = (struct net_local *)dev->priv;
805     
806     	printk("Closing %s.\n", dev->name);
807     
808     	netif_stop_queue(dev);
809     
810     	*R_IRQ_MASK0_CLR = IO_STATE(R_IRQ_MASK0_CLR, par0_ecp_cmd, clr);
811     
812     	*R_IRQ_MASK2_CLR =
813     		IO_STATE(R_IRQ_MASK2_CLR, dma3_eop, clr) |
814     		IO_STATE(R_IRQ_MASK2_CLR, dma4_descr, clr);	
815     
816     	/* Stop the receiver and the transmitter */
817     
818     	RESET_DMA(3);
819     	RESET_DMA(4);
820     
821     	/* Flush the Tx and disable Rx here. */
822     
823     	free_irq(DMA3_RX_IRQ_NBR, (void *)dev);
824     	free_irq(DMA4_TX_IRQ_NBR, (void *)dev);
825     	free_irq(PAR0_ECP_IRQ_NBR, (void *)dev);
826     
827     	free_dma(PAR1_TX_DMA_NBR);
828     	free_dma(PAR0_RX_DMA_NBR);
829     
830     	/* Update the statistics here. */
831     
832     	update_rx_stats(&np->stats);
833     	update_tx_stats(&np->stats);
834     
835     	return 0;
836     }
837     
838     static void
839     update_rx_stats(struct net_device_stats *es)
840     {
841     	unsigned long r = *R_REC_COUNTERS;
842     	/* update stats relevant to reception errors */
843     	es->rx_fifo_errors += r >> 24;            /* fifo overrun */
844     	es->rx_crc_errors += r & 0xff;            /* crc error */
845     	es->rx_frame_errors += (r >> 8) & 0xff;   /* alignment error */
846     	es->rx_length_errors += (r >> 16) & 0xff; /* oversized frames */
847     }
848     
849     static void
850     update_tx_stats(struct net_device_stats *es)
851     {
852     	unsigned long r = *R_TR_COUNTERS;
853     	/* update stats relevant to transmission errors */
854     	es->collisions += (r & 0xff) + ((r >> 8) & 0xff); /* single_col + multiple_col */
855     	es->tx_errors += (r >> 24) & 0xff; /* deferred transmit frames */
856     }
857     
858     /*
859      * Get the current statistics.
860      * This may be called with the card open or closed.
861      */
862     static struct net_device_stats *
863     e100_get_stats(struct net_device *dev)
864     {
865     	struct net_local *lp = (struct net_local *)dev->priv;
866     
867     	update_rx_stats(&lp->stats);
868     	update_tx_stats(&lp->stats);
869     
870     	return &lp->stats;
871     }
872     
873     /*
874      * Set or clear the multicast filter for this adaptor.
875      * num_addrs == -1	Promiscuous mode, receive all packets
876      * num_addrs == 0	Normal mode, clear multicast list
877      * num_addrs > 0	Multicast mode, receive normal and MC packets,
878      *			and do best-effort filtering.
879      */
880     static void
881     set_multicast_list(struct net_device *dev)
882     {
883       /* To do */
884     }
885     
886     void
887     e100_hardware_send_packet(unsigned long hostcmd, char *buf, int length)
888     {
889       static char bogus_ecp[] = { 42, 42 };
890       int i;
891       
892     
893     #ifdef ETHDEBUG
894     	printk("e100 send pack, buf 0x%x len %d\n", buf, length);
895     #endif
896             
897             host_command = hostcmd;
898     
899     	/* Configure the tx dma descriptor. Desc 0 is already configured.*/
900     
901             TxDescList[1].sw_len = length;
902     	/* bug workaround - etrax100 needs d_wait on the descriptor _before_
903     	 * a descriptor containing an ECP command
904     	 */
905     	TxDescList[1].ctrl = d_wait;
906     	TxDescList[1].buf = virt_to_phys(buf);
907     	TxDescList[1].next = virt_to_phys(&TxDescList[2]);
908     
909             /* append the ecp dummy descriptor - its only purpose is to 
910     	 * make the receiver generate an irq due to the ecp command
911     	 * so the receiver knows where packets end
912     	 */
913     
914     	TxDescList[2].sw_len = 1;
915     	TxDescList[2].ctrl = d_ecp | d_eol | d_int;
916     	TxDescList[2].buf = virt_to_phys(bogus_ecp);
917             
918     
919     	/* setup the dma channel and start it */
920     
921             *R_DMA_CH4_FIRST = virt_to_phys(TxDescList);
922     	*R_DMA_CH4_CMD = IO_STATE(R_DMA_CH4_CMD, cmd, start);
923             
924     #ifdef ETHDEBUG         
925              printk("done\n");
926     #endif
927     }
928     
929     /* send a chunk of code to the slave chip to boot it. */
930     
931     static void
932     boot_slave(unsigned char *code)
933     {
934       int i;
935     
936     #ifdef ETHDEBUG
937     	printk("  booting slave ETRAX...\n");
938     #endif
939             *R_PORT_PB_DATA = 0x7F; /* Reset slave */
940             udelay(15); /* Time enough to reset WAN tranciever */
941             *R_PORT_PB_DATA = 0xFF; /* Reset slave */
942     
943     	/* configure the tx dma data descriptor */
944     
945     	TxDescList[1].sw_len = ETRAX_PAR_BOOT_LENGTH;
946     	TxDescList[1].ctrl = d_eol | d_int; 
947                                                           
948     	TxDescList[1].buf = virt_to_phys(code);
949     	TxDescList[1].next = 0;
950             
951             /* setup the dma channel and start it */
952      	*R_DMA_CH4_FIRST = virt_to_phys(&TxDescList[1]);
953     	*R_DMA_CH4_CMD = IO_STATE(R_DMA_CH4_CMD, cmd, start);
954     
955     	/* wait for completion */
956     	while(!(*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma4_descr)));
957     
958     	/* ack the irq */
959     
960     	*R_DMA_CH4_CLR_INTR = IO_STATE(R_DMA_CH4_CLR_INTR, clr_descr, do);
961     
962     #if 0
963             /* manual transfer of boot code - requires dma turned off */
964             for (i=0; i<ETRAX_PAR_BOOT_LENGTH; i++)
965             {
966               printk("  sending byte: %u value: %x\n",i,code[i]);
967               while (((*R_PAR1_STATUS)&0x02) == 0); /* Wait while tr_rdy is busy*/
968               *R_PAR1_CTRL_DATA = code[i];
969             }
970     #endif
971     
972     #ifdef ETHDEBUG
973     	printk("  done\n");
974     #endif
975     }
976     
977     #ifdef ETHDEBUG
978     /* debug code to check the current status of PAR1 */
979     static void
980     dump_parport_status(void)
981     {
982       unsigned long temp;
983       
984       printk("Parport1 status:\n");
985       
986       temp = (*R_PAR1_STATUS)&0xE000;
987       temp = temp >> 13;
988       printk("Reg mode: %u (ecp_fwd(5), ecp_rev(6))\n", temp);
989       
990       temp = (*R_PAR1_STATUS)&0x1000;
991       temp = temp >> 12;
992       printk("Reg perr: %u (ecp_rev(0))\n", temp);
993       
994       temp = (*R_PAR1_STATUS)&0x0800;
995       temp = temp >> 11;
996       printk("Reg ack: %u (inactive (1), active (0))\n", temp);
997       
998       temp = (*R_PAR1_STATUS)&0x0400;
999       temp = temp >> 10;
1000       printk("Reg busy: %u (inactive (0), active (1))\n", temp);
1001       
1002       temp = (*R_PAR1_STATUS)&0x0200;
1003       temp = temp >> 9;
1004       printk("Reg fault: %u (inactive (1), active (0))\n", temp);
1005       
1006       temp = (*R_PAR1_STATUS)&0x0100;
1007       temp = temp >> 8;
1008       printk("Reg sel: %u (inactive (0), active (1), xflag(1))\n", temp);
1009     
1010       temp = (*R_PAR1_STATUS)&0x02;
1011       temp = temp >> 1;
1012       printk("Reg tr_rdy: %u (busy (0), ready (1))\n", temp);
1013     
1014     }
1015     #endif /* ETHDEBUG */
1016     
1017     static struct net_device dev_etrax_slave_ethernet;
1018     
1019     static int
1020     etrax_init_module(void)
1021     {
1022     	struct net_device *d = &dev_etrax_slave_ethernet;
1023     
1024     	d->init = etrax_ethernet_lpslave_init;
1025     
1026     	if(register_netdev(d) == 0)
1027     		return 0;
1028     	else
1029     		return -ENODEV;
1030     }
1031     
1032     module_init(etrax_init_module);
1033