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