1 /* $Id: ethernet.c,v 1.22 2004/05/14 07:58:03 starvik Exp $
3 * e100net.c: A network driver for the ETRAX 100LX network controller.
5 * Copyright (c) 1998-2002 Axis Communications AB.
7 * The outline of this driver comes from skeleton.c.
10 * Revision 1.22 2004/05/14 07:58:03 starvik
11 * Merge of changes from 2.4
13 * Revision 1.20 2004/03/11 11:38:40 starvik
14 * Merge of Linux 2.6.4
16 * Revision 1.18 2003/12/03 13:45:46 starvik
17 * Use hardware pad for short packets to prevent information leakage.
19 * Revision 1.17 2003/07/04 08:27:37 starvik
20 * Merge of Linux 2.5.74
22 * Revision 1.16 2003/04/24 08:28:22 starvik
23 * New LED behaviour: LED off when no link
25 * Revision 1.15 2003/04/09 05:20:47 starvik
26 * Merge of Linux 2.5.67
28 * Revision 1.13 2003/03/06 16:11:01 henriken
29 * Off by one error in group address register setting.
31 * Revision 1.12 2003/02/27 17:24:19 starvik
32 * Corrected Rev to Revision
34 * Revision 1.11 2003/01/24 09:53:21 starvik
35 * Oops. Initialize GA to 0, not to 1
37 * Revision 1.10 2003/01/24 09:50:55 starvik
38 * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
40 * Revision 1.9 2002/12/13 07:40:58 starvik
41 * Added basic ethtool interface
42 * Handled out of memory when allocating new buffers
44 * Revision 1.8 2002/12/11 13:13:57 starvik
45 * Added arch/ to v10 specific includes
46 * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
48 * Revision 1.7 2002/11/26 09:41:42 starvik
49 * Added e100_set_config (standard interface to set media type)
50 * Added protection against preemptive scheduling
51 * Added standard MII ioctls
53 * Revision 1.6 2002/11/21 07:18:18 starvik
54 * Timers must be initialized in 2.5.48
56 * Revision 1.5 2002/11/20 11:56:11 starvik
57 * Merge of Linux 2.5.48
59 * Revision 1.4 2002/11/18 07:26:46 starvik
60 * Linux 2.5 port of latest Linux 2.4 ethernet driver
62 * Revision 1.33 2002/10/02 20:16:17 hp
63 * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
65 * Revision 1.32 2002/09/16 06:05:58 starvik
66 * Align memory returned by dev_alloc_skb
67 * Moved handling of sent packets to interrupt to avoid reference counting problem
69 * Revision 1.31 2002/09/10 13:28:23 larsv
70 * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
71 * for supported functionality by issuing special ioctls, i.e. wireless
74 * Revision 1.30 2002/05/07 18:50:08 johana
75 * Correct spelling in comments.
77 * Revision 1.29 2002/05/06 05:38:49 starvik
78 * Performance improvements:
79 * Large packets are not copied (breakpoint set to 256 bytes)
80 * The cache bug workaround is delayed until half of the receive list
83 * Transmit interrupts are only enabled when transmit queue is full
85 * Revision 1.28.2.1 2002/04/30 08:15:51 starvik
86 * Performance improvements:
87 * Large packets are not copied (breakpoint set to 256 bytes)
88 * The cache bug workaround is delayed until half of the receive list
91 * Transmit interrupts are only enabled when transmit queue is full
93 * Revision 1.28 2002/04/22 11:47:21 johana
94 * Fix according to 2.4.19-pre7. time_after/time_before and
95 * missing end of comment.
96 * The patch has a typo for ethernet.c in e100_clear_network_leds(),
99 * Revision 1.27 2002/04/12 11:55:11 bjornw
102 * Revision 1.26 2002/03/15 17:11:02 bjornw
103 * Use prepare_rx_descriptor after the CPU has touched the receiving descs
105 * Revision 1.25 2002/03/08 13:07:53 bjornw
106 * Unnecessary spinlock removed
108 * Revision 1.24 2002/02/20 12:57:43 fredriks
109 * Replaced MIN() with min().
111 * Revision 1.23 2002/02/20 10:58:14 fredriks
112 * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
114 * Revision 1.22 2002/01/30 07:48:22 matsfg
115 * Initiate R_NETWORK_TR_CTRL
117 * Revision 1.21 2001/11/23 11:54:49 starvik
118 * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
119 * Removed compiler warnings
121 * Revision 1.20 2001/11/12 19:26:00 pkj
122 * * Corrected e100_negotiate() to not assign half to current_duplex when
123 * it was supposed to compare them...
124 * * Cleaned up failure handling in e100_open().
125 * * Fixed compiler warnings.
127 * Revision 1.19 2001/11/09 07:43:09 starvik
128 * Added full duplex support
129 * Added ioctl to set speed and duplex
130 * Clear LED timer only runs when LED is lit
132 * Revision 1.18 2001/10/03 14:40:43 jonashg
133 * Update rx_bytes counter.
135 * Revision 1.17 2001/06/11 12:43:46 olof
136 * Modified defines for network LED behavior
138 * Revision 1.16 2001/05/30 06:12:46 markusl
139 * TxDesc.next should not be set to NULL
141 * Revision 1.15 2001/05/29 10:27:04 markusl
142 * Updated after review remarks:
146 * Revision 1.14 2001/05/29 09:20:14 jonashg
147 * Use driver name on printk output so one can tell which driver that complains.
149 * Revision 1.13 2001/05/09 12:35:59 johana
150 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
152 * Revision 1.12 2001/04/05 11:43:11 tobiasa
153 * Check dev before panic.
155 * Revision 1.11 2001/04/04 11:21:05 markusl
156 * Updated according to review remarks
158 * Revision 1.10 2001/03/26 16:03:06 bjornw
159 * Needs linux/config.h
161 * Revision 1.9 2001/03/19 14:47:48 pkj
162 * * Make sure there is always a pause after the network LEDs are
163 * changed so they will not look constantly lit during heavy traffic.
164 * * Always use HZ when setting times relative to jiffies.
165 * * Use LED_NETWORK_SET() when setting the network LEDs.
167 * Revision 1.8 2001/02/27 13:52:48 bjornw
170 * Revision 1.7 2001/02/23 13:46:38 bjornw
173 * Revision 1.6 2001/01/26 15:21:04 starvik
174 * Don't disable interrupts while reading MDIO registers (MDIO is slow)
175 * Corrected promiscuous mode
176 * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
178 * Revision 1.5 2000/11/29 17:22:22 bjornw
179 * Get rid of the udword types legacy stuff
181 * Revision 1.4 2000/11/22 16:36:09 bjornw
182 * Please marketing by using the correct case when spelling Etrax.
184 * Revision 1.3 2000/11/21 16:43:04 bjornw
185 * Minor short->int change
187 * Revision 1.2 2000/11/08 14:27:57 bjornw
190 * Revision 1.1 2000/11/06 13:56:00 bjornw
191 * Verbatim copy of the 1.24 version of e100net.c from elinux
193 * Revision 1.24 2000/10/04 15:55:23 bjornw
194 * * Use virt_to_phys etc. for DMA addresses
195 * * Removed bogus CHECKSUM_UNNECESSARY
200 #include <linux/config.h>
202 #include <linux/module.h>
204 #include <linux/kernel.h>
205 #include <linux/sched.h>
206 #include <linux/delay.h>
207 #include <linux/types.h>
208 #include <linux/fcntl.h>
209 #include <linux/interrupt.h>
210 #include <linux/ptrace.h>
211 #include <linux/ioport.h>
212 #include <linux/in.h>
213 #include <linux/slab.h>
214 #include <linux/string.h>
215 #include <linux/spinlock.h>
216 #include <linux/errno.h>
217 #include <linux/init.h>
219 #include <linux/if.h>
220 #include <linux/mii.h>
221 #include <linux/netdevice.h>
222 #include <linux/etherdevice.h>
223 #include <linux/skbuff.h>
224 #include <linux/ethtool.h>
226 #include <asm/arch/svinto.h>/* DMA and register descriptions */
227 #include <asm/io.h> /* LED_* I/O functions */
230 #include <asm/system.h>
231 #include <asm/bitops.h>
232 #include <asm/ethernet.h>
233 #include <asm/cache.h>
239 * The name of the card. Is used for messages and in the requests for
240 * io regions, irqs and dma channels
243 static const char* cardname = "ETRAX 100LX built-in ethernet controller";
245 /* A default ethernet address. Highlevel SW will set the real one later */
247 static struct sockaddr default_mac = {
249 { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
252 /* Information that need to be kept for each board. */
254 struct net_device_stats stats;
256 /* Tx control lock. This protects the transmit buffer ring
257 * state along with the "tx full" state of the driver. This
258 * means all netif_queue flow control actions are protected
259 * by this lock as well.
264 typedef struct etrax_eth_descr
266 etrax_dma_descr descr;
270 /* Some transceivers requires special handling */
271 struct transceiver_ops
274 void (*check_speed)(void);
275 void (*check_duplex)(void);
278 struct transceiver_ops* transceiver;
280 /* Duplex settings */
288 /* Dma descriptors etc. */
290 #define MAX_MEDIA_DATA_SIZE 1518
292 #define MIN_PACKET_LEN 46
293 #define ETHER_HEAD_LEN 14
298 #define MDIO_BASE_STATUS_REG 0x1
299 #define MDIO_BASE_CONTROL_REG 0x0
300 #define MDIO_PHY_ID_HIGH_REG 0x2
301 #define MDIO_PHY_ID_LOW_REG 0x3
302 #define MDIO_BC_NEGOTIATE 0x0200
303 #define MDIO_BC_FULL_DUPLEX_MASK 0x0100
304 #define MDIO_BC_AUTO_NEG_MASK 0x1000
305 #define MDIO_BC_SPEED_SELECT_MASK 0x2000
306 #define MDIO_STATUS_100_FD 0x4000
307 #define MDIO_STATUS_100_HD 0x2000
308 #define MDIO_STATUS_10_FD 0x1000
309 #define MDIO_STATUS_10_HD 0x0800
310 #define MDIO_STATUS_SPEED_DUPLEX_MASK 0x7800
311 #define MDIO_ADVERTISMENT_REG 0x4
312 #define MDIO_ADVERT_100_FD 0x100
313 #define MDIO_ADVERT_100_HD 0x080
314 #define MDIO_ADVERT_10_FD 0x040
315 #define MDIO_ADVERT_10_HD 0x020
316 #define MDIO_LINK_UP_MASK 0x4
317 #define MDIO_START 0x1
318 #define MDIO_READ 0x2
319 #define MDIO_WRITE 0x1
320 #define MDIO_PREAMBLE 0xfffffffful
322 /* Broadcom specific */
323 #define MDIO_AUX_CTRL_STATUS_REG 0x18
324 #define MDIO_BC_FULL_DUPLEX_IND 0x1
325 #define MDIO_BC_SPEED 0x2
328 #define MDIO_TDK_DIAGNOSTIC_REG 18
329 #define MDIO_TDK_DIAGNOSTIC_RATE 0x400
330 #define MDIO_TDK_DIAGNOSTIC_DPLX 0x800
332 /* Network flash constants */
333 #define NET_FLASH_TIME (HZ/50) /* 20 ms */
334 #define NET_FLASH_PAUSE (HZ/100) /* 10 ms */
335 #define NET_LINK_UP_CHECK_INTERVAL (2*HZ) /* 2 s */
336 #define NET_DUPLEX_CHECK_INTERVAL (2*HZ) /* 2 s */
338 #define NO_NETWORK_ACTIVITY 0
339 #define NETWORK_ACTIVITY 1
341 #define NBR_OF_RX_DESC 64
342 #define NBR_OF_TX_DESC 256
344 /* Large packets are sent directly to upper layers while small packets are */
345 /* copied (to reduce memory waste). The following constant decides the breakpoint */
346 #define RX_COPYBREAK 256
348 /* Due to a chip bug we need to flush the cache when descriptors are returned */
349 /* to the DMA. To decrease performance impact we return descriptors in chunks. */
350 /* The following constant determines the number of descriptors to return. */
351 #define RX_QUEUE_THRESHOLD NBR_OF_RX_DESC/2
353 #define GET_BIT(bit,val) (((val) >> (bit)) & 0x01)
355 /* Define some macros to access ETRAX 100 registers */
356 #define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
357 IO_FIELD_(reg##_, field##_, val)
358 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
359 IO_STATE_(reg##_, field##_, _##val)
361 static etrax_eth_descr *myNextRxDesc; /* Points to the next descriptor to
363 static etrax_eth_descr *myLastRxDesc; /* The last processed descriptor */
364 static etrax_eth_descr *myPrevRxDesc; /* The descriptor right before myNextRxDesc */
366 static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
368 static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
369 static etrax_eth_descr* myLastTxDesc; /* End of send queue */
370 static etrax_eth_descr* myNextTxDesc; /* Next descriptor to use */
371 static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
373 static unsigned int network_rec_config_shadow = 0;
374 static unsigned int mdio_phy_addr; /* Transciever address */
376 static unsigned int network_tr_ctrl_shadow = 0;
378 /* Network speed indication. */
379 static struct timer_list speed_timer = TIMER_INITIALIZER(NULL, 0, 0);
380 static struct timer_list clear_led_timer = TIMER_INITIALIZER(NULL, 0, 0);
381 static int current_speed; /* Speed read from transceiver */
382 static int current_speed_selection; /* Speed selected by user */
383 static unsigned long led_next_time;
384 static int led_active;
385 static int rx_queue_len;
388 static struct timer_list duplex_timer = TIMER_INITIALIZER(NULL, 0, 0);
389 static int full_duplex;
390 static enum duplex current_duplex;
392 /* Index to functions, as function prototypes. */
394 static int etrax_ethernet_init(void);
396 static int e100_open(struct net_device *dev);
397 static int e100_set_mac_address(struct net_device *dev, void *addr);
398 static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
399 static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
400 static irqreturn_t e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);
401 static void e100_rx(struct net_device *dev);
402 static int e100_close(struct net_device *dev);
403 static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
404 static int e100_ethtool_ioctl(struct net_device* dev, struct ifreq *ifr);
405 static int e100_set_config(struct net_device* dev, struct ifmap* map);
406 static void e100_tx_timeout(struct net_device *dev);
407 static struct net_device_stats *e100_get_stats(struct net_device *dev);
408 static void set_multicast_list(struct net_device *dev);
409 static void e100_hardware_send_packet(char *buf, int length);
410 static void update_rx_stats(struct net_device_stats *);
411 static void update_tx_stats(struct net_device_stats *);
412 static int e100_probe_transceiver(void);
414 static void e100_check_speed(unsigned long dummy);
415 static void e100_set_speed(unsigned long speed);
416 static void e100_check_duplex(unsigned long dummy);
417 static void e100_set_duplex(enum duplex);
418 static void e100_negotiate(void);
420 static unsigned short e100_get_mdio_reg(unsigned char reg_num);
421 static void e100_set_mdio_reg(unsigned char reg, unsigned short data);
422 static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
423 static void e100_send_mdio_bit(unsigned char bit);
424 static unsigned char e100_receive_mdio_bit(void);
425 static void e100_reset_transceiver(void);
427 static void e100_clear_network_leds(unsigned long dummy);
428 static void e100_set_network_leds(int active);
430 static void broadcom_check_speed(void);
431 static void broadcom_check_duplex(void);
432 static void tdk_check_speed(void);
433 static void tdk_check_duplex(void);
434 static void generic_check_speed(void);
435 static void generic_check_duplex(void);
437 struct transceiver_ops transceivers[] =
439 {0x1018, broadcom_check_speed, broadcom_check_duplex}, /* Broadcom */
440 {0xC039, tdk_check_speed, tdk_check_duplex}, /* TDK 2120 */
441 {0x039C, tdk_check_speed, tdk_check_duplex}, /* TDK 2120C */
442 {0x0000, generic_check_speed, generic_check_duplex} /* Generic, must be last */
445 #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
448 * Check for a network adaptor of this type, and return '0' if one exists.
449 * If dev->base_addr == 0, probe all likely locations.
450 * If dev->base_addr == 1, always return failure.
451 * If dev->base_addr == 2, allocate space for the device and return success
452 * (detachable devices only).
456 etrax_ethernet_init(void)
458 struct net_device *dev;
462 "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2003 Axis Communications AB\n");
464 dev = alloc_etherdev(sizeof(struct net_local));
468 dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
470 /* now setup our etrax specific stuff */
472 dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
473 dev->dma = NETWORK_RX_DMA_NBR;
475 /* fill in our handlers so the network layer can talk to us in the future */
477 dev->open = e100_open;
478 dev->hard_start_xmit = e100_send_packet;
479 dev->stop = e100_close;
480 dev->get_stats = e100_get_stats;
481 dev->set_multicast_list = set_multicast_list;
482 dev->set_mac_address = e100_set_mac_address;
483 dev->do_ioctl = e100_ioctl;
484 dev->set_config = e100_set_config;
485 dev->tx_timeout = e100_tx_timeout;
487 /* Initialise the list of Etrax DMA-descriptors */
489 /* Initialise receive descriptors */
491 for (i = 0; i < NBR_OF_RX_DESC; i++) {
492 /* Allocate two extra cachelines to make sure that buffer used by DMA
493 * does not share cacheline with any other data (to avoid cache bug)
495 RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
496 RxDescList[i].descr.ctrl = 0;
497 RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
498 RxDescList[i].descr.next = virt_to_phys(&RxDescList[i + 1]);
499 RxDescList[i].descr.buf = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
500 RxDescList[i].descr.status = 0;
501 RxDescList[i].descr.hw_len = 0;
502 prepare_rx_descriptor(&RxDescList[i].descr);
505 RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl = d_eol;
506 RxDescList[NBR_OF_RX_DESC - 1].descr.next = virt_to_phys(&RxDescList[0]);
509 /* Initialize transmit descriptors */
510 for (i = 0; i < NBR_OF_TX_DESC; i++) {
511 TxDescList[i].descr.ctrl = 0;
512 TxDescList[i].descr.sw_len = 0;
513 TxDescList[i].descr.next = virt_to_phys(&TxDescList[i + 1].descr);
514 TxDescList[i].descr.buf = 0;
515 TxDescList[i].descr.status = 0;
516 TxDescList[i].descr.hw_len = 0;
517 TxDescList[i].skb = 0;
520 TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl = d_eol;
521 TxDescList[NBR_OF_TX_DESC - 1].descr.next = virt_to_phys(&TxDescList[0].descr);
523 /* Initialise initial pointers */
525 myNextRxDesc = &RxDescList[0];
526 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
527 myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
528 myFirstTxDesc = &TxDescList[0];
529 myNextTxDesc = &TxDescList[0];
530 myLastTxDesc = &TxDescList[NBR_OF_TX_DESC - 1];
532 /* Register device */
533 err = register_netdev(dev);
539 /* set the default MAC address */
541 e100_set_mac_address(dev, &default_mac);
543 /* Initialize speed indicator stuff. */
546 current_speed_selection = 0; /* Auto */
547 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
548 speed_timer.function = e100_check_speed;
550 clear_led_timer.function = e100_clear_network_leds;
553 current_duplex = autoneg;
554 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
555 duplex_timer.function = e100_check_duplex;
557 /* Initialize group address registers to make sure that no */
558 /* unwanted addresses are matched */
559 *R_NETWORK_GA_0 = 0x00000000;
560 *R_NETWORK_GA_1 = 0x00000000;
564 /* set MAC address of the interface. called from the core after a
565 * SIOCSIFADDR ioctl, and from the bootup above.
569 e100_set_mac_address(struct net_device *dev, void *p)
571 struct net_local *np = (struct net_local *)dev->priv;
572 struct sockaddr *addr = p;
575 spin_lock(&np->lock); /* preemption protection */
579 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
581 /* Write it to the hardware.
582 * Note the way the address is wrapped:
583 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
584 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
587 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
588 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
589 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
592 /* show it in the log as well */
594 printk(KERN_INFO "%s: changed MAC to ", dev->name);
596 for (i = 0; i < 5; i++)
597 printk("%02X:", dev->dev_addr[i]);
599 printk("%02X\n", dev->dev_addr[i]);
601 spin_unlock(&np->lock);
607 * Open/initialize the board. This is called (in the current kernel)
608 * sometime after booting when the 'ifconfig' program is run.
610 * This routine should set everything up anew at each open, even
611 * registers that "should" only need to be set once at boot, so that
612 * there is non-reboot way to recover if something goes wrong.
616 e100_open(struct net_device *dev)
620 /* enable the MDIO output pin */
622 *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
625 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
626 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
627 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
629 /* clear dma0 and 1 eop and descr irq masks */
631 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
632 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
633 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
634 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
636 /* Reset and wait for the DMA channels */
638 RESET_DMA(NETWORK_TX_DMA_NBR);
639 RESET_DMA(NETWORK_RX_DMA_NBR);
640 WAIT_DMA(NETWORK_TX_DMA_NBR);
641 WAIT_DMA(NETWORK_RX_DMA_NBR);
643 /* Initialise the etrax network controller */
645 /* allocate the irq corresponding to the receiving DMA */
647 if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt, 0,
648 cardname, (void *)dev)) {
652 /* allocate the irq corresponding to the transmitting DMA */
654 if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
655 cardname, (void *)dev)) {
659 /* allocate the irq corresponding to the network errors etc */
661 if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
662 cardname, (void *)dev)) {
666 /* give the HW an idea of what MAC address we want */
668 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
669 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
670 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
674 /* use promiscuous mode for testing */
675 *R_NETWORK_GA_0 = 0xffffffff;
676 *R_NETWORK_GA_1 = 0xffffffff;
678 *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
680 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
681 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
682 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
683 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
686 *R_NETWORK_GEN_CONFIG =
687 IO_STATE(R_NETWORK_GEN_CONFIG, phy, mii_clk) |
688 IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
690 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
691 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
692 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
693 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
694 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
695 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
696 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
697 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
702 /* enable the irq's for ethernet DMA */
705 IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
706 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
709 IO_STATE(R_IRQ_MASK0_SET, overrun, set) |
710 IO_STATE(R_IRQ_MASK0_SET, underrun, set) |
711 IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
713 /* make sure the irqs are cleared */
715 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
716 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
718 /* make sure the rec and transmit error counters are cleared */
720 (void)*R_REC_COUNTERS; /* dummy read */
721 (void)*R_TR_COUNTERS; /* dummy read */
723 /* start the receiving DMA channel so we can receive packets from now on */
725 *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
726 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
728 /* Set up transmit DMA channel so it can be restarted later */
730 *R_DMA_CH0_FIRST = 0;
731 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
733 restore_flags(flags);
735 /* Probe for transceiver */
736 if (e100_probe_transceiver())
739 /* Start duplex/speed timers */
740 add_timer(&speed_timer);
741 add_timer(&duplex_timer);
743 /* We are now ready to accept transmit requeusts from
744 * the queueing layer of the networking.
746 netif_start_queue(dev);
751 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
753 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
755 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
762 generic_check_speed(void)
765 data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
766 if ((data & MDIO_ADVERT_100_FD) ||
767 (data & MDIO_ADVERT_100_HD))
774 tdk_check_speed(void)
777 data = e100_get_mdio_reg(MDIO_TDK_DIAGNOSTIC_REG);
778 current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
782 broadcom_check_speed(void)
785 data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
786 current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
790 e100_check_speed(unsigned long dummy)
792 static int led_initiated = 0;
794 int old_speed = current_speed;
796 data = e100_get_mdio_reg(MDIO_BASE_STATUS_REG);
797 if (!(data & MDIO_LINK_UP_MASK)) {
800 transceiver->check_speed();
803 if ((old_speed != current_speed) || !led_initiated) {
805 e100_set_network_leds(NO_NETWORK_ACTIVITY);
808 /* Reinitialize the timer. */
809 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
810 add_timer(&speed_timer);
816 unsigned short data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
818 /* Discard old speed and duplex settings */
819 data &= ~(MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD |
820 MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD);
822 switch (current_speed_selection) {
824 if (current_duplex == full)
825 data |= MDIO_ADVERT_10_FD;
826 else if (current_duplex == half)
827 data |= MDIO_ADVERT_10_HD;
829 data |= MDIO_ADVERT_10_HD | MDIO_ADVERT_10_FD;
833 if (current_duplex == full)
834 data |= MDIO_ADVERT_100_FD;
835 else if (current_duplex == half)
836 data |= MDIO_ADVERT_100_HD;
838 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD;
842 if (current_duplex == full)
843 data |= MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD;
844 else if (current_duplex == half)
845 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_10_HD;
847 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
850 default : /* assume autoneg speed and duplex */
851 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD |
852 MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
855 e100_set_mdio_reg(MDIO_ADVERTISMENT_REG, data);
857 /* Renegotiate with link partner */
858 data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
859 data |= MDIO_BC_NEGOTIATE;
861 e100_set_mdio_reg(MDIO_BASE_CONTROL_REG, data);
865 e100_set_speed(unsigned long speed)
867 if (speed != current_speed_selection) {
868 current_speed_selection = speed;
874 e100_check_duplex(unsigned long dummy)
876 int old_duplex = full_duplex;
877 transceiver->check_duplex();
878 if (old_duplex != full_duplex) {
880 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
881 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
884 /* Reinitialize the timer. */
885 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
886 add_timer(&duplex_timer);
890 generic_check_duplex(void)
893 data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
894 if ((data & MDIO_ADVERT_100_FD) ||
895 (data & MDIO_ADVERT_10_FD))
902 tdk_check_duplex(void)
905 data = e100_get_mdio_reg(MDIO_TDK_DIAGNOSTIC_REG);
906 full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
910 broadcom_check_duplex(void)
913 data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
914 full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
918 e100_set_duplex(enum duplex new_duplex)
920 if (new_duplex != current_duplex) {
921 current_duplex = new_duplex;
927 e100_probe_transceiver(void)
929 unsigned int phyid_high;
930 unsigned int phyid_low;
932 struct transceiver_ops* ops = NULL;
934 /* Probe MDIO physical address */
935 for (mdio_phy_addr = 0; mdio_phy_addr <= 31; mdio_phy_addr++) {
936 if (e100_get_mdio_reg(MDIO_BASE_STATUS_REG) != 0xffff)
939 if (mdio_phy_addr == 32)
942 /* Get manufacturer */
943 phyid_high = e100_get_mdio_reg(MDIO_PHY_ID_HIGH_REG);
944 phyid_low = e100_get_mdio_reg(MDIO_PHY_ID_LOW_REG);
945 oui = (phyid_high << 6) | (phyid_low >> 10);
947 for (ops = &transceivers[0]; ops->oui; ops++) {
956 static unsigned short
957 e100_get_mdio_reg(unsigned char reg_num)
959 unsigned short cmd; /* Data to be sent on MDIO port */
960 unsigned short data; /* Data read from MDIO */
963 /* Start of frame, OP Code, Physical Address, Register Address */
964 cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (mdio_phy_addr << 7) |
967 e100_send_mdio_cmd(cmd, 0);
972 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
973 data |= (e100_receive_mdio_bit() << bitCounter);
980 e100_set_mdio_reg(unsigned char reg, unsigned short data)
985 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) |
988 e100_send_mdio_cmd(cmd, 1);
991 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
992 e100_send_mdio_bit(GET_BIT(bitCounter, data));
998 e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
1001 unsigned char data = 0x2;
1004 for (bitCounter = 31; bitCounter>= 0; bitCounter--)
1005 e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
1007 for (bitCounter = 15; bitCounter >= 2; bitCounter--)
1008 e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
1011 for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
1013 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1015 e100_receive_mdio_bit();
1019 e100_send_mdio_bit(unsigned char bit)
1021 *R_NETWORK_MGM_CTRL =
1022 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1023 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1025 *R_NETWORK_MGM_CTRL =
1026 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1027 IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
1028 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1032 static unsigned char
1033 e100_receive_mdio_bit()
1036 *R_NETWORK_MGM_CTRL = 0;
1037 bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
1039 *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
1045 e100_reset_transceiver(void)
1048 unsigned short data;
1051 data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
1053 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) | (MDIO_BASE_CONTROL_REG << 2);
1055 e100_send_mdio_cmd(cmd, 1);
1059 for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
1060 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1064 /* Called by upper layers if they decide it took too long to complete
1065 * sending a packet - we need to reset and stuff.
1069 e100_tx_timeout(struct net_device *dev)
1071 struct net_local *np = (struct net_local *)dev->priv;
1072 unsigned long flags;
1074 spin_lock_irqsave(&np->lock, flags);
1076 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
1077 tx_done(dev) ? "IRQ problem" : "network cable problem");
1079 /* remember we got an error */
1081 np->stats.tx_errors++;
1083 /* reset the TX DMA in case it has hung on something */
1085 RESET_DMA(NETWORK_TX_DMA_NBR);
1086 WAIT_DMA(NETWORK_TX_DMA_NBR);
1088 /* Reset the transceiver. */
1090 e100_reset_transceiver();
1092 /* and get rid of the packets that never got an interrupt */
1093 while (myFirstTxDesc != myNextTxDesc)
1095 dev_kfree_skb(myFirstTxDesc->skb);
1096 myFirstTxDesc->skb = 0;
1097 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1100 /* Set up transmit DMA channel so it can be restarted later */
1101 *R_DMA_CH0_FIRST = 0;
1102 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
1104 /* tell the upper layers we're ok again */
1106 netif_wake_queue(dev);
1107 spin_unlock_irqrestore(&np->lock, flags);
1111 /* This will only be invoked if the driver is _not_ in XOFF state.
1112 * What this means is that we need not check it, and that this
1113 * invariant will hold if we make sure that the netif_*_queue()
1114 * calls are done at the proper times.
1118 e100_send_packet(struct sk_buff *skb, struct net_device *dev)
1120 struct net_local *np = (struct net_local *)dev->priv;
1121 unsigned char *buf = skb->data;
1122 unsigned long flags;
1125 printk("send packet len %d\n", length);
1127 spin_lock_irqsave(&np->lock, flags); /* protect from tx_interrupt and ourself */
1129 myNextTxDesc->skb = skb;
1131 dev->trans_start = jiffies;
1133 e100_hardware_send_packet(buf, skb->len);
1135 myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
1137 /* Stop queue if full */
1138 if (myNextTxDesc == myFirstTxDesc) {
1139 netif_stop_queue(dev);
1142 spin_unlock_irqrestore(&np->lock, flags);
1148 * The typical workload of the driver:
1149 * Handle the network interface interrupts.
1153 e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1155 struct net_device *dev = (struct net_device *)dev_id;
1156 struct net_local *np = (struct net_local *)dev->priv;
1157 unsigned long irqbits = *R_IRQ_MASK2_RD;
1159 /* Disable RX/TX IRQs to avoid reentrancy */
1161 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1162 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1164 /* Handle received packets */
1165 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
1166 /* acknowledge the eop interrupt */
1168 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
1170 /* check if one or more complete packets were indeed received */
1172 while (*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) {
1173 /* Take out the buffer and give it to the OS, then
1174 * allocate a new buffer to put a packet in.
1177 ((struct net_local *)dev->priv)->stats.rx_packets++;
1178 /* restart/continue on the channel, for safety */
1179 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
1180 /* clear dma channel 1 eop/descr irq bits */
1181 *R_DMA_CH1_CLR_INTR =
1182 IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
1183 IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
1185 /* now, we might have gotten another packet
1186 so we have to loop back and check if so */
1190 /* Report any packets that have been sent */
1191 while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
1192 myFirstTxDesc != myNextTxDesc)
1194 np->stats.tx_bytes += myFirstTxDesc->skb->len;
1195 np->stats.tx_packets++;
1197 /* dma is ready with the transmission of the data in tx_skb, so now
1198 we can release the skb memory */
1199 dev_kfree_skb_irq(myFirstTxDesc->skb);
1200 myFirstTxDesc->skb = 0;
1201 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1204 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1205 /* acknowledge the eop interrupt and wake up queue */
1206 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
1207 netif_wake_queue(dev);
1210 /* Enable RX/TX IRQs again */
1212 IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
1213 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
1219 e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1221 struct net_device *dev = (struct net_device *)dev_id;
1222 struct net_local *np = (struct net_local *)dev->priv;
1223 unsigned long irqbits = *R_IRQ_MASK0_RD;
1225 /* check for underrun irq */
1226 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
1227 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1228 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1229 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
1230 np->stats.tx_errors++;
1231 D(printk("ethernet receiver underrun!\n"));
1234 /* check for overrun irq */
1235 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
1236 update_rx_stats(&np->stats); /* this will ack the irq */
1237 D(printk("ethernet receiver overrun!\n"));
1239 /* check for excessive collision irq */
1240 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
1241 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1242 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1243 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
1244 *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
1245 np->stats.tx_errors++;
1246 D(printk("ethernet excessive collisions!\n"));
1251 /* We have a good packet(s), get it/them out of the buffers. */
1253 e100_rx(struct net_device *dev)
1255 struct sk_buff *skb;
1257 struct net_local *np = (struct net_local *)dev->priv;
1258 unsigned char *skb_data_ptr;
1263 if (!led_active && time_after(jiffies, led_next_time)) {
1264 /* light the network leds depending on the current speed. */
1265 e100_set_network_leds(NETWORK_ACTIVITY);
1267 /* Set the earliest time we may clear the LED */
1268 led_next_time = jiffies + NET_FLASH_TIME;
1270 mod_timer(&clear_led_timer, jiffies + HZ/10);
1273 length = myNextRxDesc->descr.hw_len - 4;
1274 ((struct net_local *)dev->priv)->stats.rx_bytes += length;
1277 printk("Got a packet of length %d:\n", length);
1278 /* dump the first bytes in the packet */
1279 skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
1280 for (i = 0; i < 8; i++) {
1281 printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
1282 skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
1283 skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
1288 if (length < RX_COPYBREAK) {
1289 /* Small packet, copy data */
1290 skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
1292 np->stats.rx_errors++;
1293 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1297 skb_put(skb, length - ETHER_HEAD_LEN); /* allocate room for the packet body */
1298 skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
1301 printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
1302 skb->head, skb->data, skb->tail, skb->end);
1303 printk("copying packet to 0x%x.\n", skb_data_ptr);
1306 memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
1309 /* Large packet, send directly to upper layers and allocate new
1310 * memory (aligned to cache line boundary to avoid bug).
1311 * Before sending the skb to upper layers we must make sure that
1312 * skb->data points to the aligned start of the packet.
1315 struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
1317 np->stats.rx_errors++;
1318 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1321 skb = myNextRxDesc->skb;
1322 align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
1323 skb_put(skb, length + align);
1324 skb_pull(skb, align); /* Remove alignment bytes */
1325 myNextRxDesc->skb = new_skb;
1326 myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
1330 skb->protocol = eth_type_trans(skb, dev);
1332 /* Send the packet to the upper layers */
1335 /* Prepare for next packet */
1336 myNextRxDesc->descr.status = 0;
1337 myPrevRxDesc = myNextRxDesc;
1338 myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
1342 /* Check if descriptors should be returned */
1343 if (rx_queue_len == RX_QUEUE_THRESHOLD) {
1344 flush_etrax_cache();
1345 myPrevRxDesc->descr.ctrl |= d_eol;
1346 myLastRxDesc->descr.ctrl &= ~d_eol;
1347 myLastRxDesc = myPrevRxDesc;
1352 /* The inverse routine to net_open(). */
1354 e100_close(struct net_device *dev)
1356 struct net_local *np = (struct net_local *)dev->priv;
1358 printk(KERN_INFO "Closing %s.\n", dev->name);
1360 netif_stop_queue(dev);
1363 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
1364 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
1365 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
1368 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
1369 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1370 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
1371 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1373 /* Stop the receiver and the transmitter */
1375 RESET_DMA(NETWORK_TX_DMA_NBR);
1376 RESET_DMA(NETWORK_RX_DMA_NBR);
1378 /* Flush the Tx and disable Rx here. */
1380 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
1381 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
1382 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
1384 /* Update the statistics here. */
1386 update_rx_stats(&np->stats);
1387 update_tx_stats(&np->stats);
1389 /* Stop speed/duplex timers */
1390 del_timer(&speed_timer);
1391 del_timer(&duplex_timer);
1397 e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1399 struct mii_ioctl_data *data = if_mii(ifr);
1400 struct net_local *np = netdev_priv(dev);
1402 spin_lock(&np->lock); /* Preempt protection */
1405 return e100_ethtool_ioctl(dev,ifr);
1406 case SIOCGMIIPHY: /* Get PHY address */
1407 data->phy_id = mdio_phy_addr;
1409 case SIOCGMIIREG: /* Read MII register */
1410 data->val_out = e100_get_mdio_reg(data->reg_num);
1412 case SIOCSMIIREG: /* Write MII register */
1413 e100_set_mdio_reg(data->reg_num, data->val_in);
1415 /* The ioctls below should be considered obsolete but are */
1416 /* still present for compatability with old scripts/apps */
1417 case SET_ETH_SPEED_10: /* 10 Mbps */
1420 case SET_ETH_SPEED_100: /* 100 Mbps */
1421 e100_set_speed(100);
1423 case SET_ETH_SPEED_AUTO: /* Auto negotiate speed */
1426 case SET_ETH_DUPLEX_HALF: /* Half duplex. */
1427 e100_set_duplex(half);
1429 case SET_ETH_DUPLEX_FULL: /* Full duplex. */
1430 e100_set_duplex(full);
1432 case SET_ETH_DUPLEX_AUTO: /* Autonegotiate duplex*/
1433 e100_set_duplex(autoneg);
1438 spin_unlock(&np->lock);
1443 e100_ethtool_ioctl(struct net_device *dev, struct ifreq *ifr)
1445 struct ethtool_cmd ecmd;
1447 if (copy_from_user(&ecmd, ifr->ifr_data, sizeof (ecmd)))
1453 memset((void *) &ecmd, 0, sizeof (ecmd));
1455 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
1456 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1457 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
1458 ecmd.port = PORT_TP;
1459 ecmd.transceiver = XCVR_EXTERNAL;
1460 ecmd.phy_address = mdio_phy_addr;
1461 ecmd.speed = current_speed;
1462 ecmd.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
1463 ecmd.advertising = ADVERTISED_TP;
1464 if (current_duplex == autoneg && current_speed_selection == 0)
1465 ecmd.advertising |= ADVERTISED_Autoneg;
1468 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
1469 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
1470 if (current_speed_selection == 10)
1471 ecmd.advertising &= ~(ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full);
1472 else if (current_speed_selection == 100)
1473 ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full);
1474 if (current_duplex == half)
1475 ecmd.advertising &= ~(ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full);
1476 else if (current_duplex == full)
1477 ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_100baseT_Half);
1479 ecmd.autoneg = AUTONEG_ENABLE;
1480 if (copy_to_user(ifr->ifr_data, &ecmd, sizeof (ecmd)))
1486 if (!capable(CAP_NET_ADMIN)) {
1489 if (ecmd.autoneg == AUTONEG_ENABLE) {
1490 e100_set_duplex(autoneg);
1493 e100_set_duplex(ecmd.duplex == DUPLEX_HALF ? half : full);
1494 e100_set_speed(ecmd.speed == SPEED_10 ? 10: 100);
1498 case ETHTOOL_GDRVINFO:
1500 struct ethtool_drvinfo info;
1501 memset((void *) &info, 0, sizeof (info));
1502 strncpy(info.driver, "ETRAX 100LX", sizeof(info.driver) - 1);
1503 strncpy(info.version, "$Revision: 1.22 $", sizeof(info.version) - 1);
1504 strncpy(info.fw_version, "N/A", sizeof(info.fw_version) - 1);
1505 strncpy(info.bus_info, "N/A", sizeof(info.bus_info) - 1);
1506 info.regdump_len = 0;
1507 info.eedump_len = 0;
1508 info.testinfo_len = 0;
1509 if (copy_to_user(ifr->ifr_data, &info, sizeof (info)))
1513 case ETHTOOL_NWAY_RST:
1514 if (current_duplex == autoneg && current_speed_selection == 0)
1525 e100_set_config(struct net_device *dev, struct ifmap *map)
1527 struct net_local *np = (struct net_local *)dev->priv;
1528 spin_lock(&np->lock); /* Preempt protection */
1531 case IF_PORT_UNKNOWN:
1534 e100_set_duplex(autoneg);
1536 case IF_PORT_10BASET:
1538 e100_set_duplex(autoneg);
1540 case IF_PORT_100BASET:
1541 case IF_PORT_100BASETX:
1542 e100_set_speed(100);
1543 e100_set_duplex(autoneg);
1545 case IF_PORT_100BASEFX:
1546 case IF_PORT_10BASE2:
1548 spin_unlock(&np->lock);
1552 printk(KERN_ERR "%s: Invalid media selected", dev->name);
1553 spin_unlock(&np->lock);
1556 spin_unlock(&np->lock);
1561 update_rx_stats(struct net_device_stats *es)
1563 unsigned long r = *R_REC_COUNTERS;
1564 /* update stats relevant to reception errors */
1565 es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
1566 es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
1567 es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
1568 es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
1572 update_tx_stats(struct net_device_stats *es)
1574 unsigned long r = *R_TR_COUNTERS;
1575 /* update stats relevant to transmission errors */
1577 IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
1578 IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
1579 es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
1583 * Get the current statistics.
1584 * This may be called with the card open or closed.
1586 static struct net_device_stats *
1587 e100_get_stats(struct net_device *dev)
1589 struct net_local *lp = (struct net_local *)dev->priv;
1590 unsigned long flags;
1591 spin_lock_irqsave(&lp->lock, flags);
1593 update_rx_stats(&lp->stats);
1594 update_tx_stats(&lp->stats);
1596 spin_unlock_irqrestore(&lp->lock, flags);
1601 * Set or clear the multicast filter for this adaptor.
1602 * num_addrs == -1 Promiscuous mode, receive all packets
1603 * num_addrs == 0 Normal mode, clear multicast list
1604 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1605 * and do best-effort filtering.
1608 set_multicast_list(struct net_device *dev)
1610 struct net_local *lp = (struct net_local *)dev->priv;
1611 int num_addr = dev->mc_count;
1612 unsigned long int lo_bits;
1613 unsigned long int hi_bits;
1614 spin_lock(&lp->lock);
1615 if (dev->flags & IFF_PROMISC)
1617 /* promiscuous mode */
1618 lo_bits = 0xfffffffful;
1619 hi_bits = 0xfffffffful;
1621 /* Enable individual receive */
1622 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
1623 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1624 } else if (dev->flags & IFF_ALLMULTI) {
1625 /* enable all multicasts */
1626 lo_bits = 0xfffffffful;
1627 hi_bits = 0xfffffffful;
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 } else if (num_addr == 0) {
1633 /* Normal, clear the mc list */
1634 lo_bits = 0x00000000ul;
1635 hi_bits = 0x00000000ul;
1637 /* Disable individual receive */
1638 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1639 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1641 /* MC mode, receive normal and MC packets */
1643 struct dev_mc_list *dmi = dev->mc_list;
1646 lo_bits = 0x00000000ul;
1647 hi_bits = 0x00000000ul;
1648 for (i=0; i<num_addr; i++) {
1649 /* Calculate the hash index for the GA registers */
1652 baddr = dmi->dmi_addr;
1653 hash_ix ^= (*baddr) & 0x3f;
1654 hash_ix ^= ((*baddr) >> 6) & 0x03;
1656 hash_ix ^= ((*baddr) << 2) & 0x03c;
1657 hash_ix ^= ((*baddr) >> 4) & 0xf;
1659 hash_ix ^= ((*baddr) << 4) & 0x30;
1660 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1662 hash_ix ^= (*baddr) & 0x3f;
1663 hash_ix ^= ((*baddr) >> 6) & 0x03;
1665 hash_ix ^= ((*baddr) << 2) & 0x03c;
1666 hash_ix ^= ((*baddr) >> 4) & 0xf;
1668 hash_ix ^= ((*baddr) << 4) & 0x30;
1669 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1673 if (hash_ix >= 32) {
1674 hi_bits |= (1 << (hash_ix-32));
1677 lo_bits |= (1 << hash_ix);
1681 /* Disable individual receive */
1682 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1683 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1685 *R_NETWORK_GA_0 = lo_bits;
1686 *R_NETWORK_GA_1 = hi_bits;
1687 spin_unlock(&lp->lock);
1691 e100_hardware_send_packet(char *buf, int length)
1693 D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
1695 if (!led_active && time_after(jiffies, led_next_time)) {
1696 /* light the network leds depending on the current speed. */
1697 e100_set_network_leds(NETWORK_ACTIVITY);
1699 /* Set the earliest time we may clear the LED */
1700 led_next_time = jiffies + NET_FLASH_TIME;
1702 mod_timer(&clear_led_timer, jiffies + HZ/10);
1705 /* configure the tx dma descriptor */
1706 myNextTxDesc->descr.sw_len = length;
1707 myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
1708 myNextTxDesc->descr.buf = virt_to_phys(buf);
1710 /* Move end of list */
1711 myLastTxDesc->descr.ctrl &= ~d_eol;
1712 myLastTxDesc = myNextTxDesc;
1714 /* Restart DMA channel */
1715 *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
1719 e100_clear_network_leds(unsigned long dummy)
1721 if (led_active && time_after(jiffies, led_next_time)) {
1722 e100_set_network_leds(NO_NETWORK_ACTIVITY);
1724 /* Set the earliest time we may set the LED */
1725 led_next_time = jiffies + NET_FLASH_PAUSE;
1731 e100_set_network_leds(int active)
1733 #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
1734 int light_leds = (active == NO_NETWORK_ACTIVITY);
1735 #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
1736 int light_leds = (active == NETWORK_ACTIVITY);
1738 #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
1741 if (!current_speed) {
1742 /* Make LED red, link is down */
1743 #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
1744 LED_NETWORK_SET(LED_RED);
1746 LED_NETWORK_SET(LED_OFF);
1749 else if (light_leds) {
1750 if (current_speed == 10) {
1751 LED_NETWORK_SET(LED_ORANGE);
1753 LED_NETWORK_SET(LED_GREEN);
1757 LED_NETWORK_SET(LED_OFF);
1762 etrax_init_module(void)
1764 return etrax_ethernet_init();
1768 e100_boot_setup(char* str)
1770 struct sockaddr sa = {0};
1773 /* Parse the colon separated Ethernet station address */
1774 for (i = 0; i < ETH_ALEN; i++) {
1776 if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
1777 printk(KERN_WARNING "Malformed station address");
1780 sa.sa_data[i] = (char)tmp;
1787 __setup("etrax100_eth=", e100_boot_setup);
1789 module_init(etrax_init_module);