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