1/*
2 * e100net.c: A network driver for the ETRAX 100LX network controller.
3 *
4 * Copyright (c) 1998-2002 Axis Communications AB.
5 *
6 * The outline of this driver comes from skeleton.c.
7 *
8 */
9
10
11#include <linux/module.h>
12
13#include <linux/kernel.h>
14#include <linux/delay.h>
15#include <linux/types.h>
16#include <linux/fcntl.h>
17#include <linux/interrupt.h>
18#include <linux/ptrace.h>
19#include <linux/ioport.h>
20#include <linux/in.h>
21#include <linux/string.h>
22#include <linux/spinlock.h>
23#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/bitops.h>
26
27#include <linux/if.h>
28#include <linux/mii.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/skbuff.h>
32#include <linux/ethtool.h>
33
34#include <arch/svinto.h>/* DMA and register descriptions */
35#include <asm/io.h>         /* CRIS_LED_* I/O functions */
36#include <asm/irq.h>
37#include <asm/dma.h>
38#include <asm/ethernet.h>
39#include <asm/cache.h>
40#include <arch/io_interface_mux.h>
41
42//#define ETHDEBUG
43#define D(x)
44
45/*
46 * The name of the card. Is used for messages and in the requests for
47 * io regions, irqs and dma channels
48 */
49
50static const char* cardname = "ETRAX 100LX built-in ethernet controller";
51
52/* A default ethernet address. Highlevel SW will set the real one later */
53
54static struct sockaddr default_mac = {
55	0,
56	{ 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
57};
58
59/* Information that need to be kept for each board. */
60struct net_local {
61	struct mii_if_info mii_if;
62
63	/* Tx control lock.  This protects the transmit buffer ring
64	 * state along with the "tx full" state of the driver.  This
65	 * means all netif_queue flow control actions are protected
66	 * by this lock as well.
67	 */
68	spinlock_t lock;
69
70	spinlock_t led_lock; /* Protect LED state */
71	spinlock_t transceiver_lock; /* Protect transceiver state. */
72};
73
74typedef struct etrax_eth_descr
75{
76	etrax_dma_descr descr;
77	struct sk_buff* skb;
78} etrax_eth_descr;
79
80/* Some transceivers requires special handling */
81struct transceiver_ops
82{
83	unsigned int oui;
84	void (*check_speed)(struct net_device* dev);
85	void (*check_duplex)(struct net_device* dev);
86};
87
88/* Duplex settings */
89enum duplex
90{
91	half,
92	full,
93	autoneg
94};
95
96/* Dma descriptors etc. */
97
98#define MAX_MEDIA_DATA_SIZE 1522
99
100#define MIN_PACKET_LEN      46
101#define ETHER_HEAD_LEN      14
102
103/*
104** MDIO constants.
105*/
106#define MDIO_START                          0x1
107#define MDIO_READ                           0x2
108#define MDIO_WRITE                          0x1
109#define MDIO_PREAMBLE              0xfffffffful
110
111/* Broadcom specific */
112#define MDIO_AUX_CTRL_STATUS_REG           0x18
113#define MDIO_BC_FULL_DUPLEX_IND             0x1
114#define MDIO_BC_SPEED                       0x2
115
116/* TDK specific */
117#define MDIO_TDK_DIAGNOSTIC_REG              18
118#define MDIO_TDK_DIAGNOSTIC_RATE          0x400
119#define MDIO_TDK_DIAGNOSTIC_DPLX          0x800
120
121/*Intel LXT972A specific*/
122#define MDIO_INT_STATUS_REG_2			0x0011
123#define MDIO_INT_FULL_DUPLEX_IND       (1 << 9)
124#define MDIO_INT_SPEED                (1 << 14)
125
126/* Network flash constants */
127#define NET_FLASH_TIME                  (HZ/50) /* 20 ms */
128#define NET_FLASH_PAUSE                (HZ/100) /* 10 ms */
129#define NET_LINK_UP_CHECK_INTERVAL       (2*HZ) /* 2 s   */
130#define NET_DUPLEX_CHECK_INTERVAL        (2*HZ) /* 2 s   */
131
132#define NO_NETWORK_ACTIVITY 0
133#define NETWORK_ACTIVITY    1
134
135#define NBR_OF_RX_DESC     32
136#define NBR_OF_TX_DESC     16
137
138/* Large packets are sent directly to upper layers while small packets are */
139/* copied (to reduce memory waste). The following constant decides the breakpoint */
140#define RX_COPYBREAK 256
141
142/* Due to a chip bug we need to flush the cache when descriptors are returned */
143/* to the DMA. To decrease performance impact we return descriptors in chunks. */
144/* The following constant determines the number of descriptors to return. */
145#define RX_QUEUE_THRESHOLD  NBR_OF_RX_DESC/2
146
147#define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)
148
149/* Define some macros to access ETRAX 100 registers */
150#define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
151					  IO_FIELD_(reg##_, field##_, val)
152#define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
153					  IO_STATE_(reg##_, field##_, _##val)
154
155static etrax_eth_descr *myNextRxDesc;  /* Points to the next descriptor to
156                                          to be processed */
157static etrax_eth_descr *myLastRxDesc;  /* The last processed descriptor */
158
159static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
160
161static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
162static etrax_eth_descr* myLastTxDesc;  /* End of send queue */
163static etrax_eth_descr* myNextTxDesc;  /* Next descriptor to use */
164static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
165
166static unsigned int network_rec_config_shadow = 0;
167
168static unsigned int network_tr_ctrl_shadow = 0;
169
170/* Network speed indication. */
171static DEFINE_TIMER(speed_timer, NULL, 0, 0);
172static DEFINE_TIMER(clear_led_timer, NULL, 0, 0);
173static int current_speed; /* Speed read from transceiver */
174static int current_speed_selection; /* Speed selected by user */
175static unsigned long led_next_time;
176static int led_active;
177static int rx_queue_len;
178
179/* Duplex */
180static DEFINE_TIMER(duplex_timer, NULL, 0, 0);
181static int full_duplex;
182static enum duplex current_duplex;
183
184/* Index to functions, as function prototypes. */
185
186static int etrax_ethernet_init(void);
187
188static int e100_open(struct net_device *dev);
189static int e100_set_mac_address(struct net_device *dev, void *addr);
190static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
191static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id);
192static irqreturn_t e100nw_interrupt(int irq, void *dev_id);
193static void e100_rx(struct net_device *dev);
194static int e100_close(struct net_device *dev);
195static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
196static int e100_set_config(struct net_device* dev, struct ifmap* map);
197static void e100_tx_timeout(struct net_device *dev);
198static struct net_device_stats *e100_get_stats(struct net_device *dev);
199static void set_multicast_list(struct net_device *dev);
200static void e100_hardware_send_packet(struct net_local* np, char *buf, int length);
201static void update_rx_stats(struct net_device_stats *);
202static void update_tx_stats(struct net_device_stats *);
203static int e100_probe_transceiver(struct net_device* dev);
204
205static void e100_check_speed(unsigned long priv);
206static void e100_set_speed(struct net_device* dev, unsigned long speed);
207static void e100_check_duplex(unsigned long priv);
208static void e100_set_duplex(struct net_device* dev, enum duplex);
209static void e100_negotiate(struct net_device* dev);
210
211static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
212static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);
213
214static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
215static void e100_send_mdio_bit(unsigned char bit);
216static unsigned char e100_receive_mdio_bit(void);
217static void e100_reset_transceiver(struct net_device* net);
218
219static void e100_clear_network_leds(unsigned long dummy);
220static void e100_set_network_leds(int active);
221
222static const struct ethtool_ops e100_ethtool_ops;
223#if defined(CONFIG_ETRAX_NO_PHY)
224static void dummy_check_speed(struct net_device* dev);
225static void dummy_check_duplex(struct net_device* dev);
226#else
227static void broadcom_check_speed(struct net_device* dev);
228static void broadcom_check_duplex(struct net_device* dev);
229static void tdk_check_speed(struct net_device* dev);
230static void tdk_check_duplex(struct net_device* dev);
231static void intel_check_speed(struct net_device* dev);
232static void intel_check_duplex(struct net_device* dev);
233static void generic_check_speed(struct net_device* dev);
234static void generic_check_duplex(struct net_device* dev);
235#endif
236#ifdef CONFIG_NET_POLL_CONTROLLER
237static void e100_netpoll(struct net_device* dev);
238#endif
239
240static int autoneg_normal = 1;
241
242struct transceiver_ops transceivers[] =
243{
244#if defined(CONFIG_ETRAX_NO_PHY)
245	{0x0000, dummy_check_speed, dummy_check_duplex}        /* Dummy */
246#else
247	{0x1018, broadcom_check_speed, broadcom_check_duplex},  /* Broadcom */
248	{0xC039, tdk_check_speed, tdk_check_duplex},            /* TDK 2120 */
249	{0x039C, tdk_check_speed, tdk_check_duplex},            /* TDK 2120C */
250        {0x04de, intel_check_speed, intel_check_duplex},     	/* Intel LXT972A*/
251	{0x0000, generic_check_speed, generic_check_duplex}     /* Generic, must be last */
252#endif
253};
254
255struct transceiver_ops* transceiver = &transceivers[0];
256
257static const struct net_device_ops e100_netdev_ops = {
258	.ndo_open		= e100_open,
259	.ndo_stop		= e100_close,
260	.ndo_start_xmit		= e100_send_packet,
261	.ndo_tx_timeout		= e100_tx_timeout,
262	.ndo_get_stats		= e100_get_stats,
263	.ndo_set_rx_mode	= set_multicast_list,
264	.ndo_do_ioctl		= e100_ioctl,
265	.ndo_set_mac_address	= e100_set_mac_address,
266	.ndo_validate_addr	= eth_validate_addr,
267	.ndo_change_mtu		= eth_change_mtu,
268	.ndo_set_config		= e100_set_config,
269#ifdef CONFIG_NET_POLL_CONTROLLER
270	.ndo_poll_controller	= e100_netpoll,
271#endif
272};
273
274#define tx_done(dev) (*R_DMA_CH0_CMD == 0)
275
276/*
277 * Check for a network adaptor of this type, and return '0' if one exists.
278 * If dev->base_addr == 0, probe all likely locations.
279 * If dev->base_addr == 1, always return failure.
280 * If dev->base_addr == 2, allocate space for the device and return success
281 * (detachable devices only).
282 */
283
284static int __init
285etrax_ethernet_init(void)
286{
287	struct net_device *dev;
288        struct net_local* np;
289	int i, err;
290
291	printk(KERN_INFO
292	       "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 1998-2007 Axis Communications AB\n");
293
294	if (cris_request_io_interface(if_eth, cardname)) {
295		printk(KERN_CRIT "etrax_ethernet_init failed to get IO interface\n");
296		return -EBUSY;
297	}
298
299	dev = alloc_etherdev(sizeof(struct net_local));
300	if (!dev)
301		return -ENOMEM;
302
303	np = netdev_priv(dev);
304
305	/* we do our own locking */
306	dev->features |= NETIF_F_LLTX;
307
308	dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
309
310	/* now setup our etrax specific stuff */
311
312	dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
313	dev->dma = NETWORK_RX_DMA_NBR;
314
315	/* fill in our handlers so the network layer can talk to us in the future */
316
317	dev->ethtool_ops	= &e100_ethtool_ops;
318	dev->netdev_ops		= &e100_netdev_ops;
319
320	spin_lock_init(&np->lock);
321	spin_lock_init(&np->led_lock);
322	spin_lock_init(&np->transceiver_lock);
323
324	/* Initialise the list of Etrax DMA-descriptors */
325
326	/* Initialise receive descriptors */
327
328	for (i = 0; i < NBR_OF_RX_DESC; i++) {
329		/* Allocate two extra cachelines to make sure that buffer used
330		 * by DMA does not share cacheline with any other data (to
331		 * avoid cache bug)
332		 */
333		RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
334		if (!RxDescList[i].skb)
335			return -ENOMEM;
336		RxDescList[i].descr.ctrl   = 0;
337		RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
338		RxDescList[i].descr.next   = virt_to_phys(&RxDescList[i + 1]);
339		RxDescList[i].descr.buf    = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
340		RxDescList[i].descr.status = 0;
341		RxDescList[i].descr.hw_len = 0;
342		prepare_rx_descriptor(&RxDescList[i].descr);
343	}
344
345	RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl   = d_eol;
346	RxDescList[NBR_OF_RX_DESC - 1].descr.next   = virt_to_phys(&RxDescList[0]);
347	rx_queue_len = 0;
348
349	/* Initialize transmit descriptors */
350	for (i = 0; i < NBR_OF_TX_DESC; i++) {
351		TxDescList[i].descr.ctrl   = 0;
352		TxDescList[i].descr.sw_len = 0;
353		TxDescList[i].descr.next   = virt_to_phys(&TxDescList[i + 1].descr);
354		TxDescList[i].descr.buf    = 0;
355		TxDescList[i].descr.status = 0;
356		TxDescList[i].descr.hw_len = 0;
357		TxDescList[i].skb = 0;
358	}
359
360	TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl   = d_eol;
361	TxDescList[NBR_OF_TX_DESC - 1].descr.next   = virt_to_phys(&TxDescList[0].descr);
362
363	/* Initialise initial pointers */
364
365	myNextRxDesc  = &RxDescList[0];
366	myLastRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
367	myFirstTxDesc = &TxDescList[0];
368	myNextTxDesc  = &TxDescList[0];
369	myLastTxDesc  = &TxDescList[NBR_OF_TX_DESC - 1];
370
371	/* Register device */
372	err = register_netdev(dev);
373	if (err) {
374		free_netdev(dev);
375		return err;
376	}
377
378	/* set the default MAC address */
379
380	e100_set_mac_address(dev, &default_mac);
381
382	/* Initialize speed indicator stuff. */
383
384	current_speed = 10;
385	current_speed_selection = 0; /* Auto */
386	speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
387	speed_timer.data = (unsigned long)dev;
388	speed_timer.function = e100_check_speed;
389
390	clear_led_timer.function = e100_clear_network_leds;
391	clear_led_timer.data = (unsigned long)dev;
392
393	full_duplex = 0;
394	current_duplex = autoneg;
395	duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
396        duplex_timer.data = (unsigned long)dev;
397	duplex_timer.function = e100_check_duplex;
398
399        /* Initialize mii interface */
400	np->mii_if.phy_id_mask = 0x1f;
401	np->mii_if.reg_num_mask = 0x1f;
402	np->mii_if.dev = dev;
403	np->mii_if.mdio_read = e100_get_mdio_reg;
404	np->mii_if.mdio_write = e100_set_mdio_reg;
405
406	/* Initialize group address registers to make sure that no */
407	/* unwanted addresses are matched */
408	*R_NETWORK_GA_0 = 0x00000000;
409	*R_NETWORK_GA_1 = 0x00000000;
410
411	/* Initialize next time the led can flash */
412	led_next_time = jiffies;
413	return 0;
414}
415
416/* set MAC address of the interface. called from the core after a
417 * SIOCSIFADDR ioctl, and from the bootup above.
418 */
419
420static int
421e100_set_mac_address(struct net_device *dev, void *p)
422{
423	struct net_local *np = netdev_priv(dev);
424	struct sockaddr *addr = p;
425
426	spin_lock(&np->lock); /* preemption protection */
427
428	/* remember it */
429
430	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
431
432	/* Write it to the hardware.
433	 * Note the way the address is wrapped:
434	 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
435	 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
436	 */
437
438	*R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
439		(dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
440	*R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
441	*R_NETWORK_SA_2 = 0;
442
443	/* show it in the log as well */
444
445	printk(KERN_INFO "%s: changed MAC to %pM\n", dev->name, dev->dev_addr);
446
447	spin_unlock(&np->lock);
448
449	return 0;
450}
451
452/*
453 * Open/initialize the board. This is called (in the current kernel)
454 * sometime after booting when the 'ifconfig' program is run.
455 *
456 * This routine should set everything up anew at each open, even
457 * registers that "should" only need to be set once at boot, so that
458 * there is non-reboot way to recover if something goes wrong.
459 */
460
461static int
462e100_open(struct net_device *dev)
463{
464	unsigned long flags;
465
466	/* enable the MDIO output pin */
467
468	*R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
469
470	*R_IRQ_MASK0_CLR =
471		IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
472		IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
473		IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
474
475	/* clear dma0 and 1 eop and descr irq masks */
476	*R_IRQ_MASK2_CLR =
477		IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
478		IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
479		IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
480		IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
481
482	/* Reset and wait for the DMA channels */
483
484	RESET_DMA(NETWORK_TX_DMA_NBR);
485	RESET_DMA(NETWORK_RX_DMA_NBR);
486	WAIT_DMA(NETWORK_TX_DMA_NBR);
487	WAIT_DMA(NETWORK_RX_DMA_NBR);
488
489	/* Initialise the etrax network controller */
490
491	/* allocate the irq corresponding to the receiving DMA */
492
493	if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt, 0, cardname,
494			(void *)dev)) {
495		goto grace_exit0;
496	}
497
498	/* allocate the irq corresponding to the transmitting DMA */
499
500	if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
501			cardname, (void *)dev)) {
502		goto grace_exit1;
503	}
504
505	/* allocate the irq corresponding to the network errors etc */
506
507	if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
508			cardname, (void *)dev)) {
509		goto grace_exit2;
510	}
511
512	/*
513	 * Always allocate the DMA channels after the IRQ,
514	 * and clean up on failure.
515	 */
516
517	if (cris_request_dma(NETWORK_TX_DMA_NBR,
518	                     cardname,
519	                     DMA_VERBOSE_ON_ERROR,
520	                     dma_eth)) {
521		goto grace_exit3;
522        }
523
524	if (cris_request_dma(NETWORK_RX_DMA_NBR,
525	                     cardname,
526	                     DMA_VERBOSE_ON_ERROR,
527	                     dma_eth)) {
528		goto grace_exit4;
529        }
530
531	/* give the HW an idea of what MAC address we want */
532
533	*R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
534		(dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
535	*R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
536	*R_NETWORK_SA_2 = 0;
537
538#if 0
539	/* use promiscuous mode for testing */
540	*R_NETWORK_GA_0 = 0xffffffff;
541	*R_NETWORK_GA_1 = 0xffffffff;
542
543	*R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
544#else
545	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, max_size, size1522);
546	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
547	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
548	SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
549	*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
550#endif
551
552	*R_NETWORK_GEN_CONFIG =
553		IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
554		IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
555
556	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
557	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
558	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
559	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
560	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
561	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
562	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
563	*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
564
565	local_irq_save(flags);
566
567	/* enable the irq's for ethernet DMA */
568
569	*R_IRQ_MASK2_SET =
570		IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
571		IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
572
573	*R_IRQ_MASK0_SET =
574		IO_STATE(R_IRQ_MASK0_SET, overrun,       set) |
575		IO_STATE(R_IRQ_MASK0_SET, underrun,      set) |
576		IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
577
578	/* make sure the irqs are cleared */
579
580	*R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
581	*R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
582
583	/* make sure the rec and transmit error counters are cleared */
584
585	(void)*R_REC_COUNTERS;  /* dummy read */
586	(void)*R_TR_COUNTERS;   /* dummy read */
587
588	/* start the receiving DMA channel so we can receive packets from now on */
589
590	*R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
591	*R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
592
593	/* Set up transmit DMA channel so it can be restarted later */
594
595	*R_DMA_CH0_FIRST = 0;
596	*R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
597	netif_start_queue(dev);
598
599	local_irq_restore(flags);
600
601	/* Probe for transceiver */
602	if (e100_probe_transceiver(dev))
603		goto grace_exit5;
604
605	/* Start duplex/speed timers */
606	add_timer(&speed_timer);
607	add_timer(&duplex_timer);
608
609	/* We are now ready to accept transmit requeusts from
610	 * the queueing layer of the networking.
611	 */
612	netif_carrier_on(dev);
613
614	return 0;
615
616grace_exit5:
617	cris_free_dma(NETWORK_RX_DMA_NBR, cardname);
618grace_exit4:
619	cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
620grace_exit3:
621	free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
622grace_exit2:
623	free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
624grace_exit1:
625	free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
626grace_exit0:
627	return -EAGAIN;
628}
629
630#if defined(CONFIG_ETRAX_NO_PHY)
631static void
632dummy_check_speed(struct net_device* dev)
633{
634	current_speed = 100;
635}
636#else
637static void
638generic_check_speed(struct net_device* dev)
639{
640	unsigned long data;
641	struct net_local *np = netdev_priv(dev);
642
643	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
644	if ((data & ADVERTISE_100FULL) ||
645	    (data & ADVERTISE_100HALF))
646		current_speed = 100;
647	else
648		current_speed = 10;
649}
650
651static void
652tdk_check_speed(struct net_device* dev)
653{
654	unsigned long data;
655	struct net_local *np = netdev_priv(dev);
656
657	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
658				 MDIO_TDK_DIAGNOSTIC_REG);
659	current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
660}
661
662static void
663broadcom_check_speed(struct net_device* dev)
664{
665	unsigned long data;
666	struct net_local *np = netdev_priv(dev);
667
668	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
669				 MDIO_AUX_CTRL_STATUS_REG);
670	current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
671}
672
673static void
674intel_check_speed(struct net_device* dev)
675{
676	unsigned long data;
677	struct net_local *np = netdev_priv(dev);
678
679	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
680				 MDIO_INT_STATUS_REG_2);
681	current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
682}
683#endif
684static void
685e100_check_speed(unsigned long priv)
686{
687	struct net_device* dev = (struct net_device*)priv;
688	struct net_local *np = netdev_priv(dev);
689	static int led_initiated = 0;
690	unsigned long data;
691	int old_speed = current_speed;
692
693	spin_lock(&np->transceiver_lock);
694
695	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMSR);
696	if (!(data & BMSR_LSTATUS)) {
697		current_speed = 0;
698	} else {
699		transceiver->check_speed(dev);
700	}
701
702	spin_lock(&np->led_lock);
703	if ((old_speed != current_speed) || !led_initiated) {
704		led_initiated = 1;
705		e100_set_network_leds(NO_NETWORK_ACTIVITY);
706		if (current_speed)
707			netif_carrier_on(dev);
708		else
709			netif_carrier_off(dev);
710	}
711	spin_unlock(&np->led_lock);
712
713	/* Reinitialize the timer. */
714	speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
715	add_timer(&speed_timer);
716
717	spin_unlock(&np->transceiver_lock);
718}
719
720static void
721e100_negotiate(struct net_device* dev)
722{
723	struct net_local *np = netdev_priv(dev);
724	unsigned short data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
725						MII_ADVERTISE);
726
727	/* Discard old speed and duplex settings */
728	data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL |
729	          ADVERTISE_10HALF | ADVERTISE_10FULL);
730
731	switch (current_speed_selection) {
732		case 10:
733			if (current_duplex == full)
734				data |= ADVERTISE_10FULL;
735			else if (current_duplex == half)
736				data |= ADVERTISE_10HALF;
737			else
738				data |= ADVERTISE_10HALF | ADVERTISE_10FULL;
739			break;
740
741		case 100:
742			 if (current_duplex == full)
743				data |= ADVERTISE_100FULL;
744			else if (current_duplex == half)
745				data |= ADVERTISE_100HALF;
746			else
747				data |= ADVERTISE_100HALF | ADVERTISE_100FULL;
748			break;
749
750		case 0: /* Auto */
751			 if (current_duplex == full)
752				data |= ADVERTISE_100FULL | ADVERTISE_10FULL;
753			else if (current_duplex == half)
754				data |= ADVERTISE_100HALF | ADVERTISE_10HALF;
755			else
756				data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
757				  ADVERTISE_100HALF | ADVERTISE_100FULL;
758			break;
759
760		default: /* assume autoneg speed and duplex */
761			data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
762				  ADVERTISE_100HALF | ADVERTISE_100FULL;
763			break;
764	}
765
766	e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE, data);
767
768	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
769	if (autoneg_normal) {
770		/* Renegotiate with link partner */
771		data |= BMCR_ANENABLE | BMCR_ANRESTART;
772	} else {
773		/* Don't negotiate speed or duplex */
774		data &= ~(BMCR_ANENABLE | BMCR_ANRESTART);
775
776		/* Set speed and duplex static */
777		if (current_speed_selection == 10)
778			data &= ~BMCR_SPEED100;
779		else
780			data |= BMCR_SPEED100;
781
782		if (current_duplex != full)
783			data &= ~BMCR_FULLDPLX;
784		else
785			data |= BMCR_FULLDPLX;
786	}
787	e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR, data);
788}
789
790static void
791e100_set_speed(struct net_device* dev, unsigned long speed)
792{
793	struct net_local *np = netdev_priv(dev);
794
795	spin_lock(&np->transceiver_lock);
796	if (speed != current_speed_selection) {
797		current_speed_selection = speed;
798		e100_negotiate(dev);
799	}
800	spin_unlock(&np->transceiver_lock);
801}
802
803static void
804e100_check_duplex(unsigned long priv)
805{
806	struct net_device *dev = (struct net_device *)priv;
807	struct net_local *np = netdev_priv(dev);
808	int old_duplex;
809
810	spin_lock(&np->transceiver_lock);
811	old_duplex = full_duplex;
812	transceiver->check_duplex(dev);
813	if (old_duplex != full_duplex) {
814		/* Duplex changed */
815		SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
816		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
817	}
818
819	/* Reinitialize the timer. */
820	duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
821	add_timer(&duplex_timer);
822	np->mii_if.full_duplex = full_duplex;
823	spin_unlock(&np->transceiver_lock);
824}
825#if defined(CONFIG_ETRAX_NO_PHY)
826static void
827dummy_check_duplex(struct net_device* dev)
828{
829	full_duplex = 1;
830}
831#else
832static void
833generic_check_duplex(struct net_device* dev)
834{
835	unsigned long data;
836	struct net_local *np = netdev_priv(dev);
837
838	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
839	if ((data & ADVERTISE_10FULL) ||
840	    (data & ADVERTISE_100FULL))
841		full_duplex = 1;
842	else
843		full_duplex = 0;
844}
845
846static void
847tdk_check_duplex(struct net_device* dev)
848{
849	unsigned long data;
850	struct net_local *np = netdev_priv(dev);
851
852	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
853				 MDIO_TDK_DIAGNOSTIC_REG);
854	full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
855}
856
857static void
858broadcom_check_duplex(struct net_device* dev)
859{
860	unsigned long data;
861	struct net_local *np = netdev_priv(dev);
862
863	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
864				 MDIO_AUX_CTRL_STATUS_REG);
865	full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
866}
867
868static void
869intel_check_duplex(struct net_device* dev)
870{
871	unsigned long data;
872	struct net_local *np = netdev_priv(dev);
873
874	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
875				 MDIO_INT_STATUS_REG_2);
876	full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
877}
878#endif
879static void
880e100_set_duplex(struct net_device* dev, enum duplex new_duplex)
881{
882	struct net_local *np = netdev_priv(dev);
883
884	spin_lock(&np->transceiver_lock);
885	if (new_duplex != current_duplex) {
886		current_duplex = new_duplex;
887		e100_negotiate(dev);
888	}
889	spin_unlock(&np->transceiver_lock);
890}
891
892static int
893e100_probe_transceiver(struct net_device* dev)
894{
895	int ret = 0;
896
897#if !defined(CONFIG_ETRAX_NO_PHY)
898	unsigned int phyid_high;
899	unsigned int phyid_low;
900	unsigned int oui;
901	struct transceiver_ops* ops = NULL;
902	struct net_local *np = netdev_priv(dev);
903
904	spin_lock(&np->transceiver_lock);
905
906	/* Probe MDIO physical address */
907	for (np->mii_if.phy_id = 0; np->mii_if.phy_id <= 31;
908	     np->mii_if.phy_id++) {
909		if (e100_get_mdio_reg(dev,
910				      np->mii_if.phy_id, MII_BMSR) != 0xffff)
911			break;
912	}
913	if (np->mii_if.phy_id == 32) {
914		ret = -ENODEV;
915		goto out;
916	}
917
918	/* Get manufacturer */
919	phyid_high = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID1);
920	phyid_low = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID2);
921	oui = (phyid_high << 6) | (phyid_low >> 10);
922
923	for (ops = &transceivers[0]; ops->oui; ops++) {
924		if (ops->oui == oui)
925			break;
926	}
927	transceiver = ops;
928out:
929	spin_unlock(&np->transceiver_lock);
930#endif
931	return ret;
932}
933
934static int
935e100_get_mdio_reg(struct net_device *dev, int phy_id, int location)
936{
937	unsigned short cmd;    /* Data to be sent on MDIO port */
938	int data;   /* Data read from MDIO */
939	int bitCounter;
940
941	/* Start of frame, OP Code, Physical Address, Register Address */
942	cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (phy_id << 7) |
943		(location << 2);
944
945	e100_send_mdio_cmd(cmd, 0);
946
947	data = 0;
948
949	/* Data... */
950	for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
951		data |= (e100_receive_mdio_bit() << bitCounter);
952	}
953
954	return data;
955}
956
957static void
958e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value)
959{
960	int bitCounter;
961	unsigned short cmd;
962
963	cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (phy_id << 7) |
964	      (location << 2);
965
966	e100_send_mdio_cmd(cmd, 1);
967
968	/* Data... */
969	for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
970		e100_send_mdio_bit(GET_BIT(bitCounter, value));
971	}
972
973}
974
975static void
976e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
977{
978	int bitCounter;
979	unsigned char data = 0x2;
980
981	/* Preamble */
982	for (bitCounter = 31; bitCounter>= 0; bitCounter--)
983		e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
984
985	for (bitCounter = 15; bitCounter >= 2; bitCounter--)
986		e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
987
988	/* Turnaround */
989	for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
990		if (write_cmd)
991			e100_send_mdio_bit(GET_BIT(bitCounter, data));
992		else
993			e100_receive_mdio_bit();
994}
995
996static void
997e100_send_mdio_bit(unsigned char bit)
998{
999	*R_NETWORK_MGM_CTRL =
1000		IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1001		IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1002	udelay(1);
1003	*R_NETWORK_MGM_CTRL =
1004		IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1005		IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
1006		IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1007	udelay(1);
1008}
1009
1010static unsigned char
1011e100_receive_mdio_bit()
1012{
1013	unsigned char bit;
1014	*R_NETWORK_MGM_CTRL = 0;
1015	bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
1016	udelay(1);
1017	*R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
1018	udelay(1);
1019	return bit;
1020}
1021
1022static void
1023e100_reset_transceiver(struct net_device* dev)
1024{
1025	struct net_local *np = netdev_priv(dev);
1026	unsigned short cmd;
1027	unsigned short data;
1028	int bitCounter;
1029
1030	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
1031
1032	cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (np->mii_if.phy_id << 7) | (MII_BMCR << 2);
1033
1034	e100_send_mdio_cmd(cmd, 1);
1035
1036	data |= 0x8000;
1037
1038	for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
1039		e100_send_mdio_bit(GET_BIT(bitCounter, data));
1040	}
1041}
1042
1043/* Called by upper layers if they decide it took too long to complete
1044 * sending a packet - we need to reset and stuff.
1045 */
1046
1047static void
1048e100_tx_timeout(struct net_device *dev)
1049{
1050	struct net_local *np = netdev_priv(dev);
1051	unsigned long flags;
1052
1053	spin_lock_irqsave(&np->lock, flags);
1054
1055	printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
1056	       tx_done(dev) ? "IRQ problem" : "network cable problem");
1057
1058	/* remember we got an error */
1059
1060	dev->stats.tx_errors++;
1061
1062	/* reset the TX DMA in case it has hung on something */
1063
1064	RESET_DMA(NETWORK_TX_DMA_NBR);
1065	WAIT_DMA(NETWORK_TX_DMA_NBR);
1066
1067	/* Reset the transceiver. */
1068
1069	e100_reset_transceiver(dev);
1070
1071	/* and get rid of the packets that never got an interrupt */
1072	while (myFirstTxDesc != myNextTxDesc) {
1073		dev_kfree_skb(myFirstTxDesc->skb);
1074		myFirstTxDesc->skb = 0;
1075		myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1076	}
1077
1078	/* Set up transmit DMA channel so it can be restarted later */
1079	*R_DMA_CH0_FIRST = 0;
1080	*R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
1081
1082	/* tell the upper layers we're ok again */
1083
1084	netif_wake_queue(dev);
1085	spin_unlock_irqrestore(&np->lock, flags);
1086}
1087
1088
1089/* This will only be invoked if the driver is _not_ in XOFF state.
1090 * What this means is that we need not check it, and that this
1091 * invariant will hold if we make sure that the netif_*_queue()
1092 * calls are done at the proper times.
1093 */
1094
1095static int
1096e100_send_packet(struct sk_buff *skb, struct net_device *dev)
1097{
1098	struct net_local *np = netdev_priv(dev);
1099	unsigned char *buf = skb->data;
1100	unsigned long flags;
1101
1102#ifdef ETHDEBUG
1103	printk("send packet len %d\n", length);
1104#endif
1105	spin_lock_irqsave(&np->lock, flags);  /* protect from tx_interrupt and ourself */
1106
1107	myNextTxDesc->skb = skb;
1108
1109	dev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
1110
1111	e100_hardware_send_packet(np, buf, skb->len);
1112
1113	myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
1114
1115	/* Stop queue if full */
1116	if (myNextTxDesc == myFirstTxDesc) {
1117		netif_stop_queue(dev);
1118	}
1119
1120	spin_unlock_irqrestore(&np->lock, flags);
1121
1122	return NETDEV_TX_OK;
1123}
1124
1125/*
1126 * The typical workload of the driver:
1127 *   Handle the network interface interrupts.
1128 */
1129
1130static irqreturn_t
1131e100rxtx_interrupt(int irq, void *dev_id)
1132{
1133	struct net_device *dev = (struct net_device *)dev_id;
1134	struct net_local *np = netdev_priv(dev);
1135	unsigned long irqbits;
1136
1137	/*
1138	 * Note that both rx and tx interrupts are blocked at this point,
1139	 * regardless of which got us here.
1140	 */
1141
1142	irqbits = *R_IRQ_MASK2_RD;
1143
1144	/* Handle received packets */
1145	if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
1146		/* acknowledge the eop interrupt */
1147
1148		*R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
1149
1150		/* check if one or more complete packets were indeed received */
1151
1152		while ((*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) &&
1153		       (myNextRxDesc != myLastRxDesc)) {
1154			/* Take out the buffer and give it to the OS, then
1155			 * allocate a new buffer to put a packet in.
1156			 */
1157			e100_rx(dev);
1158			dev->stats.rx_packets++;
1159			/* restart/continue on the channel, for safety */
1160			*R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
1161			/* clear dma channel 1 eop/descr irq bits */
1162			*R_DMA_CH1_CLR_INTR =
1163				IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
1164				IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
1165
1166			/* now, we might have gotten another packet
1167			   so we have to loop back and check if so */
1168		}
1169	}
1170
1171	/* Report any packets that have been sent */
1172	while (virt_to_phys(myFirstTxDesc) != *R_DMA_CH0_FIRST &&
1173	       (netif_queue_stopped(dev) || myFirstTxDesc != myNextTxDesc)) {
1174		dev->stats.tx_bytes += myFirstTxDesc->skb->len;
1175		dev->stats.tx_packets++;
1176
1177		/* dma is ready with the transmission of the data in tx_skb, so now
1178		   we can release the skb memory */
1179		dev_kfree_skb_irq(myFirstTxDesc->skb);
1180		myFirstTxDesc->skb = 0;
1181		myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1182                /* Wake up queue. */
1183		netif_wake_queue(dev);
1184	}
1185
1186	if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1187		/* acknowledge the eop interrupt. */
1188		*R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
1189	}
1190
1191	return IRQ_HANDLED;
1192}
1193
1194static irqreturn_t
1195e100nw_interrupt(int irq, void *dev_id)
1196{
1197	struct net_device *dev = (struct net_device *)dev_id;
1198	unsigned long irqbits = *R_IRQ_MASK0_RD;
1199
1200	/* check for underrun irq */
1201	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
1202		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1203		*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1204		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
1205		dev->stats.tx_errors++;
1206		D(printk("ethernet receiver underrun!\n"));
1207	}
1208
1209	/* check for overrun irq */
1210	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
1211		update_rx_stats(&dev->stats); /* this will ack the irq */
1212		D(printk("ethernet receiver overrun!\n"));
1213	}
1214	/* check for excessive collision irq */
1215	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
1216		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1217		*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1218		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
1219		dev->stats.tx_errors++;
1220		D(printk("ethernet excessive collisions!\n"));
1221	}
1222	return IRQ_HANDLED;
1223}
1224
1225/* We have a good packet(s), get it/them out of the buffers. */
1226static void
1227e100_rx(struct net_device *dev)
1228{
1229	struct sk_buff *skb;
1230	int length = 0;
1231	struct net_local *np = netdev_priv(dev);
1232	unsigned char *skb_data_ptr;
1233#ifdef ETHDEBUG
1234	int i;
1235#endif
1236	etrax_eth_descr *prevRxDesc;  /* The descriptor right before myNextRxDesc */
1237	spin_lock(&np->led_lock);
1238	if (!led_active && time_after(jiffies, led_next_time)) {
1239		/* light the network leds depending on the current speed. */
1240		e100_set_network_leds(NETWORK_ACTIVITY);
1241
1242		/* Set the earliest time we may clear the LED */
1243		led_next_time = jiffies + NET_FLASH_TIME;
1244		led_active = 1;
1245		mod_timer(&clear_led_timer, jiffies + HZ/10);
1246	}
1247	spin_unlock(&np->led_lock);
1248
1249	length = myNextRxDesc->descr.hw_len - 4;
1250	dev->stats.rx_bytes += length;
1251
1252#ifdef ETHDEBUG
1253	printk("Got a packet of length %d:\n", length);
1254	/* dump the first bytes in the packet */
1255	skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
1256	for (i = 0; i < 8; i++) {
1257		printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
1258		       skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
1259		       skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
1260		skb_data_ptr += 8;
1261	}
1262#endif
1263
1264	if (length < RX_COPYBREAK) {
1265		/* Small packet, copy data */
1266		skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
1267		if (!skb) {
1268			dev->stats.rx_errors++;
1269			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1270			goto update_nextrxdesc;
1271		}
1272
1273		skb_put(skb, length - ETHER_HEAD_LEN);        /* allocate room for the packet body */
1274		skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
1275
1276#ifdef ETHDEBUG
1277		printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
1278		       skb->head, skb->data, skb_tail_pointer(skb),
1279		       skb_end_pointer(skb));
1280		printk("copying packet to 0x%x.\n", skb_data_ptr);
1281#endif
1282
1283		memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
1284	}
1285	else {
1286		/* Large packet, send directly to upper layers and allocate new
1287		 * memory (aligned to cache line boundary to avoid bug).
1288		 * Before sending the skb to upper layers we must make sure
1289		 * that skb->data points to the aligned start of the packet.
1290		 */
1291		int align;
1292		struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
1293		if (!new_skb) {
1294			dev->stats.rx_errors++;
1295			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1296			goto update_nextrxdesc;
1297		}
1298		skb = myNextRxDesc->skb;
1299		align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
1300		skb_put(skb, length + align);
1301		skb_pull(skb, align); /* Remove alignment bytes */
1302		myNextRxDesc->skb = new_skb;
1303		myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
1304	}
1305
1306	skb->protocol = eth_type_trans(skb, dev);
1307
1308	/* Send the packet to the upper layers */
1309	netif_rx(skb);
1310
1311  update_nextrxdesc:
1312	/* Prepare for next packet */
1313	myNextRxDesc->descr.status = 0;
1314	prevRxDesc = myNextRxDesc;
1315	myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
1316
1317	rx_queue_len++;
1318
1319	/* Check if descriptors should be returned */
1320	if (rx_queue_len == RX_QUEUE_THRESHOLD) {
1321		flush_etrax_cache();
1322		prevRxDesc->descr.ctrl |= d_eol;
1323		myLastRxDesc->descr.ctrl &= ~d_eol;
1324		myLastRxDesc = prevRxDesc;
1325		rx_queue_len = 0;
1326	}
1327}
1328
1329/* The inverse routine to net_open(). */
1330static int
1331e100_close(struct net_device *dev)
1332{
1333	printk(KERN_INFO "Closing %s.\n", dev->name);
1334
1335	netif_stop_queue(dev);
1336
1337	*R_IRQ_MASK0_CLR =
1338		IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
1339		IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
1340		IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
1341
1342	*R_IRQ_MASK2_CLR =
1343		IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
1344		IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1345		IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
1346		IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1347
1348	/* Stop the receiver and the transmitter */
1349
1350	RESET_DMA(NETWORK_TX_DMA_NBR);
1351	RESET_DMA(NETWORK_RX_DMA_NBR);
1352
1353	/* Flush the Tx and disable Rx here. */
1354
1355	free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
1356	free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
1357	free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
1358
1359	cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
1360	cris_free_dma(NETWORK_RX_DMA_NBR, cardname);
1361
1362	/* Update the statistics here. */
1363
1364	update_rx_stats(&dev->stats);
1365	update_tx_stats(&dev->stats);
1366
1367	/* Stop speed/duplex timers */
1368	del_timer(&speed_timer);
1369	del_timer(&duplex_timer);
1370
1371	return 0;
1372}
1373
1374static int
1375e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1376{
1377	struct mii_ioctl_data *data = if_mii(ifr);
1378	struct net_local *np = netdev_priv(dev);
1379	int rc = 0;
1380        int old_autoneg;
1381
1382	spin_lock(&np->lock); /* Preempt protection */
1383	switch (cmd) {
1384		/* The ioctls below should be considered obsolete but are */
1385		/* still present for compatibility with old scripts/apps  */
1386		case SET_ETH_SPEED_10:                  /* 10 Mbps */
1387			e100_set_speed(dev, 10);
1388			break;
1389		case SET_ETH_SPEED_100:                /* 100 Mbps */
1390			e100_set_speed(dev, 100);
1391			break;
1392		case SET_ETH_SPEED_AUTO:        /* Auto-negotiate speed */
1393			e100_set_speed(dev, 0);
1394			break;
1395		case SET_ETH_DUPLEX_HALF:       /* Half duplex */
1396			e100_set_duplex(dev, half);
1397			break;
1398		case SET_ETH_DUPLEX_FULL:       /* Full duplex */
1399			e100_set_duplex(dev, full);
1400			break;
1401		case SET_ETH_DUPLEX_AUTO:       /* Auto-negotiate duplex */
1402			e100_set_duplex(dev, autoneg);
1403			break;
1404	        case SET_ETH_AUTONEG:
1405			old_autoneg = autoneg_normal;
1406		        autoneg_normal = *(int*)data;
1407			if (autoneg_normal != old_autoneg)
1408				e100_negotiate(dev);
1409			break;
1410		default:
1411			rc = generic_mii_ioctl(&np->mii_if, if_mii(ifr),
1412						cmd, NULL);
1413			break;
1414	}
1415	spin_unlock(&np->lock);
1416	return rc;
1417}
1418
1419static int e100_get_settings(struct net_device *dev,
1420			     struct ethtool_cmd *cmd)
1421{
1422	struct net_local *np = netdev_priv(dev);
1423	int err;
1424
1425	spin_lock_irq(&np->lock);
1426	err = mii_ethtool_gset(&np->mii_if, cmd);
1427	spin_unlock_irq(&np->lock);
1428
1429	/* The PHY may support 1000baseT, but the Etrax100 does not.  */
1430	cmd->supported &= ~(SUPPORTED_1000baseT_Half
1431			    | SUPPORTED_1000baseT_Full);
1432	return err;
1433}
1434
1435static int e100_set_settings(struct net_device *dev,
1436			     struct ethtool_cmd *ecmd)
1437{
1438	if (ecmd->autoneg == AUTONEG_ENABLE) {
1439		e100_set_duplex(dev, autoneg);
1440		e100_set_speed(dev, 0);
1441	} else {
1442		e100_set_duplex(dev, ecmd->duplex == DUPLEX_HALF ? half : full);
1443		e100_set_speed(dev, ecmd->speed == SPEED_10 ? 10: 100);
1444	}
1445
1446	return 0;
1447}
1448
1449static void e100_get_drvinfo(struct net_device *dev,
1450			     struct ethtool_drvinfo *info)
1451{
1452	strncpy(info->driver, "ETRAX 100LX", sizeof(info->driver) - 1);
1453	strncpy(info->version, "$Revision: 1.31 $", sizeof(info->version) - 1);
1454	strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
1455	strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
1456}
1457
1458static int e100_nway_reset(struct net_device *dev)
1459{
1460	if (current_duplex == autoneg && current_speed_selection == 0)
1461		e100_negotiate(dev);
1462	return 0;
1463}
1464
1465static const struct ethtool_ops e100_ethtool_ops = {
1466	.get_settings	= e100_get_settings,
1467	.set_settings	= e100_set_settings,
1468	.get_drvinfo	= e100_get_drvinfo,
1469	.nway_reset	= e100_nway_reset,
1470	.get_link	= ethtool_op_get_link,
1471};
1472
1473static int
1474e100_set_config(struct net_device *dev, struct ifmap *map)
1475{
1476	struct net_local *np = netdev_priv(dev);
1477
1478	spin_lock(&np->lock); /* Preempt protection */
1479
1480	switch(map->port) {
1481		case IF_PORT_UNKNOWN:
1482			/* Use autoneg */
1483			e100_set_speed(dev, 0);
1484			e100_set_duplex(dev, autoneg);
1485			break;
1486		case IF_PORT_10BASET:
1487			e100_set_speed(dev, 10);
1488			e100_set_duplex(dev, autoneg);
1489			break;
1490		case IF_PORT_100BASET:
1491		case IF_PORT_100BASETX:
1492			e100_set_speed(dev, 100);
1493			e100_set_duplex(dev, autoneg);
1494			break;
1495		case IF_PORT_100BASEFX:
1496		case IF_PORT_10BASE2:
1497		case IF_PORT_AUI:
1498			spin_unlock(&np->lock);
1499			return -EOPNOTSUPP;
1500			break;
1501		default:
1502			printk(KERN_ERR "%s: Invalid media selected", dev->name);
1503			spin_unlock(&np->lock);
1504			return -EINVAL;
1505	}
1506	spin_unlock(&np->lock);
1507	return 0;
1508}
1509
1510static void
1511update_rx_stats(struct net_device_stats *es)
1512{
1513	unsigned long r = *R_REC_COUNTERS;
1514	/* update stats relevant to reception errors */
1515	es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
1516	es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
1517	es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
1518	es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
1519}
1520
1521static void
1522update_tx_stats(struct net_device_stats *es)
1523{
1524	unsigned long r = *R_TR_COUNTERS;
1525	/* update stats relevant to transmission errors */
1526	es->collisions +=
1527		IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
1528		IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
1529}
1530
1531/*
1532 * Get the current statistics.
1533 * This may be called with the card open or closed.
1534 */
1535static struct net_device_stats *
1536e100_get_stats(struct net_device *dev)
1537{
1538	struct net_local *lp = netdev_priv(dev);
1539	unsigned long flags;
1540
1541	spin_lock_irqsave(&lp->lock, flags);
1542
1543	update_rx_stats(&dev->stats);
1544	update_tx_stats(&dev->stats);
1545
1546	spin_unlock_irqrestore(&lp->lock, flags);
1547	return &dev->stats;
1548}
1549
1550/*
1551 * Set or clear the multicast filter for this adaptor.
1552 * num_addrs == -1	Promiscuous mode, receive all packets
1553 * num_addrs == 0	Normal mode, clear multicast list
1554 * num_addrs > 0	Multicast mode, receive normal and MC packets,
1555 *			and do best-effort filtering.
1556 */
1557static void
1558set_multicast_list(struct net_device *dev)
1559{
1560	struct net_local *lp = netdev_priv(dev);
1561	int num_addr = netdev_mc_count(dev);
1562	unsigned long int lo_bits;
1563	unsigned long int hi_bits;
1564
1565	spin_lock(&lp->lock);
1566	if (dev->flags & IFF_PROMISC) {
1567		/* promiscuous mode */
1568		lo_bits = 0xfffffffful;
1569		hi_bits = 0xfffffffful;
1570
1571		/* Enable individual receive */
1572		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
1573		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1574	} else if (dev->flags & IFF_ALLMULTI) {
1575		/* enable all multicasts */
1576		lo_bits = 0xfffffffful;
1577		hi_bits = 0xfffffffful;
1578
1579		/* Disable individual receive */
1580		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1581		*R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
1582	} else if (num_addr == 0) {
1583		/* Normal, clear the mc list */
1584		lo_bits = 0x00000000ul;
1585		hi_bits = 0x00000000ul;
1586
1587		/* Disable individual receive */
1588		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1589		*R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
1590	} else {
1591		/* MC mode, receive normal and MC packets */
1592		char hash_ix;
1593		struct netdev_hw_addr *ha;
1594		char *baddr;
1595
1596		lo_bits = 0x00000000ul;
1597		hi_bits = 0x00000000ul;
1598		netdev_for_each_mc_addr(ha, dev) {
1599			/* Calculate the hash index for the GA registers */
1600
1601			hash_ix = 0;
1602			baddr = ha->addr;
1603			hash_ix ^= (*baddr) & 0x3f;
1604			hash_ix ^= ((*baddr) >> 6) & 0x03;
1605			++baddr;
1606			hash_ix ^= ((*baddr) << 2) & 0x03c;
1607			hash_ix ^= ((*baddr) >> 4) & 0xf;
1608			++baddr;
1609			hash_ix ^= ((*baddr) << 4) & 0x30;
1610			hash_ix ^= ((*baddr) >> 2) & 0x3f;
1611			++baddr;
1612			hash_ix ^= (*baddr) & 0x3f;
1613			hash_ix ^= ((*baddr) >> 6) & 0x03;
1614			++baddr;
1615			hash_ix ^= ((*baddr) << 2) & 0x03c;
1616			hash_ix ^= ((*baddr) >> 4) & 0xf;
1617			++baddr;
1618			hash_ix ^= ((*baddr) << 4) & 0x30;
1619			hash_ix ^= ((*baddr) >> 2) & 0x3f;
1620
1621			hash_ix &= 0x3f;
1622
1623			if (hash_ix >= 32) {
1624				hi_bits |= (1 << (hash_ix-32));
1625			} else {
1626				lo_bits |= (1 << hash_ix);
1627			}
1628		}
1629		/* Disable individual receive */
1630		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1631		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1632	}
1633	*R_NETWORK_GA_0 = lo_bits;
1634	*R_NETWORK_GA_1 = hi_bits;
1635	spin_unlock(&lp->lock);
1636}
1637
1638void
1639e100_hardware_send_packet(struct net_local *np, char *buf, int length)
1640{
1641	D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
1642
1643	spin_lock(&np->led_lock);
1644	if (!led_active && time_after(jiffies, led_next_time)) {
1645		/* light the network leds depending on the current speed. */
1646		e100_set_network_leds(NETWORK_ACTIVITY);
1647
1648		/* Set the earliest time we may clear the LED */
1649		led_next_time = jiffies + NET_FLASH_TIME;
1650		led_active = 1;
1651		mod_timer(&clear_led_timer, jiffies + HZ/10);
1652	}
1653	spin_unlock(&np->led_lock);
1654
1655	/* configure the tx dma descriptor */
1656	myNextTxDesc->descr.sw_len = length;
1657	myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
1658	myNextTxDesc->descr.buf = virt_to_phys(buf);
1659
1660        /* Move end of list */
1661        myLastTxDesc->descr.ctrl &= ~d_eol;
1662        myLastTxDesc = myNextTxDesc;
1663
1664	/* Restart DMA channel */
1665	*R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
1666}
1667
1668static void
1669e100_clear_network_leds(unsigned long dummy)
1670{
1671	struct net_device *dev = (struct net_device *)dummy;
1672	struct net_local *np = netdev_priv(dev);
1673
1674	spin_lock(&np->led_lock);
1675
1676	if (led_active && time_after(jiffies, led_next_time)) {
1677		e100_set_network_leds(NO_NETWORK_ACTIVITY);
1678
1679		/* Set the earliest time we may set the LED */
1680		led_next_time = jiffies + NET_FLASH_PAUSE;
1681		led_active = 0;
1682	}
1683
1684	spin_unlock(&np->led_lock);
1685}
1686
1687static void
1688e100_set_network_leds(int active)
1689{
1690#if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
1691	int light_leds = (active == NO_NETWORK_ACTIVITY);
1692#elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
1693	int light_leds = (active == NETWORK_ACTIVITY);
1694#else
1695#error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
1696#endif
1697
1698	if (!current_speed) {
1699		/* Make LED red, link is down */
1700		CRIS_LED_NETWORK_SET(CRIS_LED_OFF);
1701	} else if (light_leds) {
1702		if (current_speed == 10) {
1703			CRIS_LED_NETWORK_SET(CRIS_LED_ORANGE);
1704		} else {
1705			CRIS_LED_NETWORK_SET(CRIS_LED_GREEN);
1706		}
1707	} else {
1708		CRIS_LED_NETWORK_SET(CRIS_LED_OFF);
1709	}
1710}
1711
1712#ifdef CONFIG_NET_POLL_CONTROLLER
1713static void
1714e100_netpoll(struct net_device* netdev)
1715{
1716	e100rxtx_interrupt(NETWORK_DMA_TX_IRQ_NBR, netdev, NULL);
1717}
1718#endif
1719
1720static int
1721etrax_init_module(void)
1722{
1723	return etrax_ethernet_init();
1724}
1725
1726static int __init
1727e100_boot_setup(char* str)
1728{
1729	struct sockaddr sa = {0};
1730	int i;
1731
1732	/* Parse the colon separated Ethernet station address */
1733	for (i = 0; i <  ETH_ALEN; i++) {
1734		unsigned int tmp;
1735		if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
1736			printk(KERN_WARNING "Malformed station address");
1737			return 0;
1738		}
1739		sa.sa_data[i] = (char)tmp;
1740	}
1741
1742	default_mac = sa;
1743	return 1;
1744}
1745
1746__setup("etrax100_eth=", e100_boot_setup);
1747
1748module_init(etrax_init_module);
1749