1 /* $Id: ethernet.c,v 1.48 2003/12/03 13:44:39 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.48 2003/12/03 13:44:39 starvik
11 * Use hardware pad for short packets. This prevents information leakage
14 * Revision 1.47 2003/11/25 15:12:38 anderstj
15 * Make sure the LED always is inititated.
17 * Revision 1.46 2003/08/28 14:35:29 jonasw
18 * Added support for TDK 2120C and fixed led when not connected
20 * Revision 1.45 2003/08/21 07:22:25 matsfg
21 * Optional behaviour on networkled when no connection.
23 * Revision 1.44 2003/07/01 10:55:07 starvik
24 * Never bring down link to make stupid POE equipment happy
26 * Revision 1.43 2003/04/24 08:26:50 starvik
27 * New LED behaviour: LED off when no link
29 * Revision 1.42 2003/04/10 07:14:58 starvik
30 * Merge of changes from todo list
32 * Revision 1.41 2003/04/09 08:31:14 pkj
33 * Typo correction (taken from Linux 2.5).
35 * Revision 1.40 2003/04/01 14:12:06 starvik
36 * Added loglevel for lots of printks
38 * Revision 1.39.2.3 2003/04/01 07:51:14 starvik
39 * Default Ethernet Stations Address can be specified with command line option.
41 * Revision 1.39.2.2 2003/03/31 15:41:07 starvik
42 * Only communicate with transciever when ETRAX is properly configured.
44 * Revision 1.39.2.1 2003/03/31 14:12:46 starvik
45 * Transmit interrupt always enabled. This has two side effects:
46 * 1. UDP (and possibly other protocols) works on quiet networks
47 * 2. Slightly lower transmit performance
49 * Revision 1.39 2003/03/06 15:45:39 henriken
50 * Off by one error in group address register setting.
52 * Revision 1.38 2003/02/27 17:23:07 starvik
53 * Corrected Rev to Revision
55 * Revision 1.37 2003/02/27 10:52:59 magnusmn
56 * More generic transceiver support.
58 * Revision 1.33.2.4 2003/02/24 16:51:26 magnusmn
59 * TDK specific registers to check speed/duplex
61 * Revision 1.33.2.3 2003/02/24 09:16:31 magnusmn
64 * Revision 1.33.2.2 2003/02/21 11:57:27 magnusmn
65 * Merging differences between 1.36 and 1.33 into ethernet.c (needed ethtool ioctl)
67 * Revision 1.33.2.1 2002/12/04 07:54:49 starvik
68 * First attempt to get more generic transceiver support
70 * Revision 1.33 2002/10/02 20:16:17 hp
71 * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
73 * Revision 1.32 2002/09/16 06:05:58 starvik
74 * Align memory returned by dev_alloc_skb
75 * Moved handling of sent packets to interrupt to avoid reference counting problem
77 * Revision 1.31 2002/09/10 13:28:23 larsv
78 * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
79 * for supported functionality by issuing special ioctls, i.e. wireless
82 * Revision 1.30 2002/05/07 18:50:08 johana
83 * Correct spelling in comments.
85 * Revision 1.29 2002/05/06 05:38:49 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.2.1 2002/04/30 08:15:51 starvik
94 * Performance improvements:
95 * Large packets are not copied (breakpoint set to 256 bytes)
96 * The cache bug workaround is delayed until half of the receive list
99 * Transmit interrupts are only enabled when transmit queue is full
101 * Revision 1.28 2002/04/22 11:47:21 johana
102 * Fix according to 2.4.19-pre7. time_after/time_before and
103 * missing end of comment.
104 * The patch has a typo for ethernet.c in e100_clear_network_leds(),
105 * that is fixed here.
107 * Revision 1.27 2002/04/12 11:55:11 bjornw
110 * Revision 1.26 2002/03/15 17:11:02 bjornw
111 * Use prepare_rx_descriptor after the CPU has touched the receiving descs
113 * Revision 1.25 2002/03/08 13:07:53 bjornw
114 * Unnecessary spinlock removed
116 * Revision 1.24 2002/02/20 12:57:43 fredriks
117 * Replaced MIN() with min().
119 * Revision 1.23 2002/02/20 10:58:14 fredriks
120 * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
122 * Revision 1.22 2002/01/30 07:48:22 matsfg
123 * Initiate R_NETWORK_TR_CTRL
125 * Revision 1.21 2001/11/23 11:54:49 starvik
126 * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
127 * Removed compiler warnings
129 * Revision 1.20 2001/11/12 19:26:00 pkj
130 * * Corrected e100_negotiate() to not assign half to current_duplex when
131 * it was supposed to compare them...
132 * * Cleaned up failure handling in e100_open().
133 * * Fixed compiler warnings.
135 * Revision 1.19 2001/11/09 07:43:09 starvik
136 * Added full duplex support
137 * Added ioctl to set speed and duplex
138 * Clear LED timer only runs when LED is lit
140 * Revision 1.18 2001/10/03 14:40:43 jonashg
141 * Update rx_bytes counter.
143 * Revision 1.17 2001/06/11 12:43:46 olof
144 * Modified defines for network LED behavior
146 * Revision 1.16 2001/05/30 06:12:46 markusl
147 * TxDesc.next should not be set to NULL
149 * Revision 1.15 2001/05/29 10:27:04 markusl
150 * Updated after review remarks:
154 * Revision 1.14 2001/05/29 09:20:14 jonashg
155 * Use driver name on printk output so one can tell which driver that complains.
157 * Revision 1.13 2001/05/09 12:35:59 johana
158 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
160 * Revision 1.12 2001/04/05 11:43:11 tobiasa
161 * Check dev before panic.
163 * Revision 1.11 2001/04/04 11:21:05 markusl
164 * Updated according to review remarks
166 * Revision 1.10 2001/03/26 16:03:06 bjornw
167 * Needs linux/config.h
169 * Revision 1.9 2001/03/19 14:47:48 pkj
170 * * Make sure there is always a pause after the network LEDs are
171 * changed so they will not look constantly lit during heavy traffic.
172 * * Always use HZ when setting times relative to jiffies.
173 * * Use LED_NETWORK_SET() when setting the network LEDs.
175 * Revision 1.8 2001/02/27 13:52:48 bjornw
178 * Revision 1.7 2001/02/23 13:46:38 bjornw
181 * Revision 1.6 2001/01/26 15:21:04 starvik
182 * Don't disable interrupts while reading MDIO registers (MDIO is slow)
183 * Corrected promiscuous mode
184 * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
186 * Revision 1.5 2000/11/29 17:22:22 bjornw
187 * Get rid of the udword types legacy stuff
189 * Revision 1.4 2000/11/22 16:36:09 bjornw
190 * Please marketing by using the correct case when spelling Etrax.
192 * Revision 1.3 2000/11/21 16:43:04 bjornw
193 * Minor short->int change
195 * Revision 1.2 2000/11/08 14:27:57 bjornw
198 * Revision 1.1 2000/11/06 13:56:00 bjornw
199 * Verbatim copy of the 1.24 version of e100net.c from elinux
201 * Revision 1.24 2000/10/04 15:55:23 bjornw
202 * * Use virt_to_phys etc. for DMA addresses
203 * * Removed bogus CHECKSUM_UNNECESSARY
208 #include <linux/config.h>
210 #include <linux/module.h>
212 #include <linux/kernel.h>
213 #include <linux/sched.h>
214 #include <linux/delay.h>
215 #include <linux/types.h>
216 #include <linux/fcntl.h>
217 #include <linux/interrupt.h>
218 #include <linux/ptrace.h>
219 #include <linux/ioport.h>
220 #include <linux/in.h>
221 #include <linux/slab.h>
222 #include <linux/string.h>
223 #include <linux/spinlock.h>
224 #include <linux/errno.h>
225 #include <linux/init.h>
227 #include <linux/if.h>
228 #include <linux/mii.h>
229 #include <linux/netdevice.h>
230 #include <linux/etherdevice.h>
231 #include <linux/skbuff.h>
232 #include <linux/ethtool.h>
234 #include <asm/svinto.h> /* DMA and register descriptions */
235 #include <asm/io.h> /* LED_* I/O functions */
238 #include <asm/system.h>
239 #include <asm/bitops.h>
240 #include <asm/ethernet.h>
241 #include <asm/cache.h>
242 #include <asm/uaccess.h>
248 * The name of the card. Is used for messages and in the requests for
249 * io regions, irqs and dma channels
252 static const char* cardname = "ETRAX 100LX built-in ethernet controller";
254 /* A default ethernet address. Highlevel SW will set the real one later */
256 static struct sockaddr default_mac = {
258 { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
261 /* Information that need to be kept for each board. */
263 struct net_device_stats stats;
265 /* Tx control lock. This protects the transmit buffer ring
266 * state along with the "tx full" state of the driver. This
267 * means all netif_queue flow control actions are protected
268 * by this lock as well.
273 typedef struct etrax_eth_descr
275 etrax_dma_descr descr;
279 /* Some transceivers requires special handling */
280 struct transceiver_ops
283 void (*check_speed)(void);
284 void (*check_duplex)(void);
287 struct transceiver_ops* transceiver;
289 /* Duplex settings */
297 /* Dma descriptors etc. */
299 #define MAX_MEDIA_DATA_SIZE 1518
301 #define MIN_PACKET_LEN 46
302 #define ETHER_HEAD_LEN 14
307 #define MDIO_BASE_STATUS_REG 0x1
308 #define MDIO_BASE_CONTROL_REG 0x0
309 #define MDIO_PHY_ID_HIGH_REG 0x2
310 #define MDIO_PHY_ID_LOW_REG 0x3
311 #define MDIO_BC_NEGOTIATE 0x0200
312 #define MDIO_BC_FULL_DUPLEX_MASK 0x0100
313 #define MDIO_BC_AUTO_NEG_MASK 0x1000
314 #define MDIO_BC_SPEED_SELECT_MASK 0x2000
315 #define MDIO_STATUS_100_FD 0x4000
316 #define MDIO_STATUS_100_HD 0x2000
317 #define MDIO_STATUS_10_FD 0x1000
318 #define MDIO_STATUS_10_HD 0x0800
319 #define MDIO_STATUS_SPEED_DUPLEX_MASK 0x7800
320 #define MDIO_ADVERTISMENT_REG 0x4
321 #define MDIO_ADVERT_100_FD 0x100
322 #define MDIO_ADVERT_100_HD 0x080
323 #define MDIO_ADVERT_10_FD 0x040
324 #define MDIO_ADVERT_10_HD 0x020
325 #define MDIO_LINK_UP_MASK 0x4
326 #define MDIO_START 0x1
327 #define MDIO_READ 0x2
328 #define MDIO_WRITE 0x1
329 #define MDIO_PREAMBLE 0xfffffffful
331 /* Broadcom specific */
332 #define MDIO_AUX_CTRL_STATUS_REG 0x18
333 #define MDIO_BC_FULL_DUPLEX_IND 0x1
334 #define MDIO_BC_SPEED 0x2
337 #define MDIO_TDK_DIAGNOSTIC_REG 18
338 #define MDIO_TDK_DIAGNOSTIC_RATE 0x400
339 #define MDIO_TDK_DIAGNOSTIC_DPLX 0x800
341 /* Network flash constants */
342 #define NET_FLASH_TIME (HZ/50) /* 20 ms */
343 #define NET_FLASH_PAUSE (HZ/100) /* 10 ms */
344 #define NET_LINK_UP_CHECK_INTERVAL (2*HZ) /* 2 s */
345 #define NET_DUPLEX_CHECK_INTERVAL (2*HZ) /* 2 s */
347 #define NO_NETWORK_ACTIVITY 0
348 #define NETWORK_ACTIVITY 1
350 #define NBR_OF_RX_DESC 64
351 #define NBR_OF_TX_DESC 256
353 /* Large packets are sent directly to upper layers while small packets are */
354 /* copied (to reduce memory waste). The following constant decides the breakpoint */
355 #define RX_COPYBREAK 256
357 /* Due to a chip bug we need to flush the cache when descriptors are returned */
358 /* to the DMA. To decrease performance impact we return descriptors in chunks. */
359 /* The following constant determines the number of descriptors to return. */
360 #define RX_QUEUE_THRESHOLD NBR_OF_RX_DESC/2
362 #define GET_BIT(bit,val) (((val) >> (bit)) & 0x01)
364 /* Define some macros to access ETRAX 100 registers */
365 #define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
366 IO_FIELD_(reg##_, field##_, val)
367 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
368 IO_STATE_(reg##_, field##_, _##val)
370 static etrax_eth_descr *myNextRxDesc; /* Points to the next descriptor to
372 static etrax_eth_descr *myLastRxDesc; /* The last processed descriptor */
373 static etrax_eth_descr *myPrevRxDesc; /* The descriptor right before myNextRxDesc */
375 static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
377 static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
378 static etrax_eth_descr* myLastTxDesc; /* End of send queue */
379 static etrax_eth_descr* myNextTxDesc; /* Next descriptor to use */
380 static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
382 static unsigned int network_rec_config_shadow = 0;
383 static unsigned int mdio_phy_addr; /* Transciever address */
385 /* Network speed indication. */
386 static struct timer_list speed_timer;
387 static struct timer_list clear_led_timer;
388 static int current_speed; /* Speed read from transceiver */
389 static int current_speed_selection; /* Speed selected by user */
390 static int led_next_time;
391 static int led_active;
392 static int rx_queue_len;
395 static struct timer_list duplex_timer;
396 static int full_duplex;
397 static enum duplex current_duplex;
399 /* Index to functions, as function prototypes. */
401 static int etrax_ethernet_init(struct net_device *dev);
403 static int e100_open(struct net_device *dev);
404 static int e100_set_mac_address(struct net_device *dev, void *addr);
405 static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
406 static void e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
407 static void e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);
408 static void e100_rx(struct net_device *dev);
409 static int e100_close(struct net_device *dev);
410 static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
411 static int e100_ethtool_ioctl(struct net_device* dev, struct ifreq *ifr);
412 static void e100_tx_timeout(struct net_device *dev);
413 static struct net_device_stats *e100_get_stats(struct net_device *dev);
414 static void set_multicast_list(struct net_device *dev);
415 static void e100_hardware_send_packet(char *buf, int length);
416 static void update_rx_stats(struct net_device_stats *);
417 static void update_tx_stats(struct net_device_stats *);
418 static int e100_probe_transceiver(void);
420 static void e100_check_speed(unsigned long dummy);
421 static void e100_set_speed(unsigned long speed);
422 static void e100_check_duplex(unsigned long dummy);
423 static void e100_set_duplex(enum duplex);
424 static void e100_negotiate(void);
426 static unsigned short e100_get_mdio_reg(unsigned char reg_num);
427 static void e100_set_mdio_reg(unsigned char reg, unsigned short data);
428 static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
429 static void e100_send_mdio_bit(unsigned char bit);
430 static unsigned char e100_receive_mdio_bit(void);
431 static void e100_reset_transceiver(void);
433 static void e100_clear_network_leds(unsigned long dummy);
434 static void e100_set_network_leds(int active);
436 static void broadcom_check_speed(void);
437 static void broadcom_check_duplex(void);
438 static void tdk_check_speed(void);
439 static void tdk_check_duplex(void);
440 static void generic_check_speed(void);
441 static void generic_check_duplex(void);
443 struct transceiver_ops transceivers[] =
445 {0x1018, broadcom_check_speed, broadcom_check_duplex}, /* Broadcom */
446 {0xC039, tdk_check_speed, tdk_check_duplex}, /* TDK 2120 */
447 {0x039C, tdk_check_speed, tdk_check_duplex}, /* TDK 2120C */
448 {0x0000, generic_check_speed, generic_check_duplex} /* Generic, must be last */
451 #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
454 * Check for a network adaptor of this type, and return '0' if one exists.
455 * If dev->base_addr == 0, probe all likely locations.
456 * If dev->base_addr == 1, always return failure.
457 * If dev->base_addr == 2, allocate space for the device and return success
458 * (detachable devices only).
462 etrax_ethernet_init(struct net_device *dev)
467 "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2001 Axis Communications AB\n");
469 dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
471 printk(KERN_INFO "%s initialized\n", dev->name);
473 /* make Linux aware of the new hardware */
476 printk(KERN_WARNING "%s: dev == NULL. Should this happen?\n",
478 dev = init_etherdev(dev, sizeof(struct net_local));
480 panic("init_etherdev failed\n");
483 /* setup generic handlers and stuff in the dev struct */
487 /* make room for the local structure containing stats etc */
489 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
490 if (dev->priv == NULL)
492 memset(dev->priv, 0, sizeof(struct net_local));
494 /* now setup our etrax specific stuff */
496 dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
497 dev->dma = NETWORK_RX_DMA_NBR;
499 /* fill in our handlers so the network layer can talk to us in the future */
501 dev->open = e100_open;
502 dev->hard_start_xmit = e100_send_packet;
503 dev->stop = e100_close;
504 dev->get_stats = e100_get_stats;
505 dev->set_multicast_list = set_multicast_list;
506 dev->set_mac_address = e100_set_mac_address;
507 dev->do_ioctl = e100_ioctl;
508 dev->tx_timeout = e100_tx_timeout;
510 /* set the default MAC address */
512 e100_set_mac_address(dev, &default_mac);
514 /* Initialise the list of Etrax DMA-descriptors */
516 /* Initialise receive descriptors */
518 for (i = 0; i < NBR_OF_RX_DESC; i++) {
519 /* Allocate two extra cachelines to make sure that buffer used by DMA
520 * does not share cacheline with any other data (to avoid cache bug)
522 RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
523 RxDescList[i].descr.ctrl = 0;
524 RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
525 RxDescList[i].descr.next = virt_to_phys(&RxDescList[i + 1]);
526 RxDescList[i].descr.buf = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
527 RxDescList[i].descr.status = 0;
528 RxDescList[i].descr.hw_len = 0;
530 prepare_rx_descriptor(&RxDescList[i].descr);
533 RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl = d_eol;
534 RxDescList[NBR_OF_RX_DESC - 1].descr.next = virt_to_phys(&RxDescList[0]);
537 /* Initialize transmit descriptors */
538 for (i = 0; i < NBR_OF_TX_DESC; i++) {
539 TxDescList[i].descr.ctrl = 0;
540 TxDescList[i].descr.sw_len = 0;
541 TxDescList[i].descr.next = virt_to_phys(&TxDescList[i + 1].descr);
542 TxDescList[i].descr.buf = 0;
543 TxDescList[i].descr.status = 0;
544 TxDescList[i].descr.hw_len = 0;
545 TxDescList[i].skb = 0;
548 TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl = d_eol;
549 TxDescList[NBR_OF_TX_DESC - 1].descr.next = virt_to_phys(&TxDescList[0].descr);
551 /* Initialise initial pointers */
553 myNextRxDesc = &RxDescList[0];
554 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
555 myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
556 myFirstTxDesc = &TxDescList[0];
557 myNextTxDesc = &TxDescList[0];
558 myLastTxDesc = &TxDescList[NBR_OF_TX_DESC - 1];
560 /* Initialize speed indicator stuff. */
563 current_speed_selection = 0; /* Auto */
564 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
565 speed_timer.function = e100_check_speed;
567 clear_led_timer.function = e100_clear_network_leds;
570 current_duplex = autoneg;
571 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
572 duplex_timer.function = e100_check_duplex;
574 /* Initialize group address registers to make sure that no */
575 /* unwanted addresses are matched */
576 *R_NETWORK_GA_0 = 0x00000000;
577 *R_NETWORK_GA_1 = 0x00000000;
581 /* set MAC address of the interface. called from the core after a
582 * SIOCSIFADDR ioctl, and from the bootup above.
586 e100_set_mac_address(struct net_device *dev, void *p)
588 struct sockaddr *addr = p;
593 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
595 /* Write it to the hardware.
596 * Note the way the address is wrapped:
597 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
598 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
601 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
602 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
603 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
606 /* show it in the log as well */
608 printk(KERN_INFO "%s: changed MAC to ", dev->name);
609 for (i = 0; i < 5; i++)
610 printk("%02X:", dev->dev_addr[i]);
612 printk("%02X\n", dev->dev_addr[i]);
618 * Open/initialize the board. This is called (in the current kernel)
619 * sometime after booting when the 'ifconfig' program is run.
621 * This routine should set everything up anew at each open, even
622 * registers that "should" only need to be set once at boot, so that
623 * there is non-reboot way to recover if something goes wrong.
627 e100_open(struct net_device *dev)
631 /* enable the MDIO output pin */
633 *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
636 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
637 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
638 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
640 /* clear dma0 and 1 eop and descr irq masks */
642 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
643 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
644 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
645 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
647 /* Reset and wait for the DMA channels */
649 RESET_DMA(NETWORK_TX_DMA_NBR);
650 RESET_DMA(NETWORK_RX_DMA_NBR);
651 WAIT_DMA(NETWORK_TX_DMA_NBR);
652 WAIT_DMA(NETWORK_RX_DMA_NBR);
654 /* Initialise the etrax network controller */
656 /* allocate the irq corresponding to the receiving DMA */
658 if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt, 0,
659 cardname, (void *)dev)) {
663 /* allocate the irq corresponding to the transmitting DMA */
665 if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
666 cardname, (void *)dev)) {
670 /* allocate the irq corresponding to the network errors etc */
672 if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
673 cardname, (void *)dev)) {
678 * Always allocate the DMA channels after the IRQ,
679 * and clean up on failure.
682 if (request_dma(NETWORK_TX_DMA_NBR, cardname)) {
686 if (request_dma(NETWORK_RX_DMA_NBR, cardname)) {
690 /* give the HW an idea of what MAC address we want */
692 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
693 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
694 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
698 /* use promiscuous mode for testing */
699 *R_NETWORK_GA_0 = 0xffffffff;
700 *R_NETWORK_GA_1 = 0xffffffff;
702 *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
704 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
705 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
706 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
707 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
710 *R_NETWORK_GEN_CONFIG =
711 IO_STATE(R_NETWORK_GEN_CONFIG, phy, mii_clk) |
712 IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
715 IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr) |
716 IO_STATE(R_NETWORK_TR_CTRL, delay, none) |
717 IO_STATE(R_NETWORK_TR_CTRL, cancel, dont) |
718 IO_STATE(R_NETWORK_TR_CTRL, cd, enable) |
719 IO_STATE(R_NETWORK_TR_CTRL, retry, enable) |
720 IO_STATE(R_NETWORK_TR_CTRL, pad, enable) |
721 IO_STATE(R_NETWORK_TR_CTRL, crc, enable);
726 /* enable the irq's for ethernet DMA */
729 IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
730 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
733 IO_STATE(R_IRQ_MASK0_SET, overrun, set) |
734 IO_STATE(R_IRQ_MASK0_SET, underrun, set) |
735 IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
737 /* make sure the irqs are cleared */
739 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
740 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
742 /* make sure the rec and transmit error counters are cleared */
744 (void)*R_REC_COUNTERS; /* dummy read */
745 (void)*R_TR_COUNTERS; /* dummy read */
747 /* start the receiving DMA channel so we can receive packets from now on */
749 *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
750 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
752 /* Set up transmit DMA channel so it can be restarted later */
754 *R_DMA_CH0_FIRST = 0;
755 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
757 restore_flags(flags);
759 /* Probe for transceiver */
760 if (e100_probe_transceiver())
763 /* Start duplex/speed timers */
764 add_timer(&speed_timer);
765 add_timer(&duplex_timer);
767 /* We are now ready to accept transmit requeusts from
768 * the queueing layer of the networking.
770 netif_start_queue(dev);
775 free_dma(NETWORK_TX_DMA_NBR);
777 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
779 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
781 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
787 generic_check_speed(void)
790 data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
791 if ((data & MDIO_ADVERT_100_FD) ||
792 (data & MDIO_ADVERT_100_HD))
799 tdk_check_speed(void)
802 data = e100_get_mdio_reg(MDIO_TDK_DIAGNOSTIC_REG);
803 current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
807 broadcom_check_speed(void)
810 data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
811 current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
815 e100_check_speed(unsigned long dummy)
817 static int led_initiated = 0;
819 int old_speed = current_speed;
821 data = e100_get_mdio_reg(MDIO_BASE_STATUS_REG);
822 if (!(data & MDIO_LINK_UP_MASK)) {
825 transceiver->check_speed();
828 if ((old_speed != current_speed) || !led_initiated) {
830 e100_set_network_leds(NO_NETWORK_ACTIVITY);
833 /* Reinitialize the timer. */
834 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
835 add_timer(&speed_timer);
841 unsigned short data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
843 /* Discard old speed and duplex settings */
844 data &= ~(MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD |
845 MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD);
847 switch (current_speed_selection) {
849 if (current_duplex == full)
850 data |= MDIO_ADVERT_10_FD;
851 else if (current_duplex == half)
852 data |= MDIO_ADVERT_10_HD;
854 data |= MDIO_ADVERT_10_HD | MDIO_ADVERT_10_FD;
858 if (current_duplex == full)
859 data |= MDIO_ADVERT_100_FD;
860 else if (current_duplex == half)
861 data |= MDIO_ADVERT_100_HD;
863 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD;
867 if (current_duplex == full)
868 data |= MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD;
869 else if (current_duplex == half)
870 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_10_HD;
872 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
875 default : /* assume autoneg speed and duplex */
876 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD |
877 MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
880 e100_set_mdio_reg(MDIO_ADVERTISMENT_REG, data);
882 /* Renegotiate with link partner */
883 data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
884 data |= MDIO_BC_NEGOTIATE;
886 e100_set_mdio_reg(MDIO_BASE_CONTROL_REG, data);
890 e100_set_speed(unsigned long speed)
892 if (speed != current_speed_selection) {
893 current_speed_selection = speed;
899 e100_check_duplex(unsigned long dummy)
901 int old_duplex = full_duplex;
902 transceiver->check_duplex();
903 if (old_duplex != full_duplex) {
905 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
906 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
909 /* Reinitialize the timer. */
910 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
911 add_timer(&duplex_timer);
915 generic_check_duplex(void)
918 data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
919 if ((data & MDIO_ADVERT_100_FD) ||
920 (data & MDIO_ADVERT_10_FD))
927 tdk_check_duplex(void)
930 data = e100_get_mdio_reg(MDIO_TDK_DIAGNOSTIC_REG);
931 full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
935 broadcom_check_duplex(void)
938 data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
939 full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
943 e100_set_duplex(enum duplex new_duplex)
945 if (new_duplex != current_duplex) {
946 current_duplex = new_duplex;
952 e100_probe_transceiver(void)
954 unsigned int phyid_high;
955 unsigned int phyid_low;
957 struct transceiver_ops* ops = NULL;
959 /* Probe MDIO physical address */
960 for (mdio_phy_addr = 0; mdio_phy_addr <= 31; mdio_phy_addr++) {
961 if (e100_get_mdio_reg(MDIO_BASE_STATUS_REG) != 0xffff)
964 if (mdio_phy_addr == 32)
967 /* Get manufacturer */
968 phyid_high = e100_get_mdio_reg(MDIO_PHY_ID_HIGH_REG);
969 phyid_low = e100_get_mdio_reg(MDIO_PHY_ID_LOW_REG);
970 oui = (phyid_high << 6) | (phyid_low >> 10);
972 for (ops = &transceivers[0]; ops->oui; ops++) {
981 static unsigned short
982 e100_get_mdio_reg(unsigned char reg_num)
984 unsigned short cmd; /* Data to be sent on MDIO port */
985 unsigned short data; /* Data read from MDIO */
988 /* Start of frame, OP Code, Physical Address, Register Address */
989 cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (mdio_phy_addr << 7) |
992 e100_send_mdio_cmd(cmd, 0);
997 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
998 data |= (e100_receive_mdio_bit() << bitCounter);
1005 e100_set_mdio_reg(unsigned char reg, unsigned short data)
1010 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) |
1013 e100_send_mdio_cmd(cmd, 1);
1016 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
1017 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1023 e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
1026 unsigned char data = 0x2;
1029 for (bitCounter = 31; bitCounter>= 0; bitCounter--)
1030 e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
1032 for (bitCounter = 15; bitCounter >= 2; bitCounter--)
1033 e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
1036 for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
1038 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1040 e100_receive_mdio_bit();
1044 e100_send_mdio_bit(unsigned char bit)
1046 *R_NETWORK_MGM_CTRL =
1047 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1048 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1050 *R_NETWORK_MGM_CTRL =
1051 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1052 IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
1053 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1057 static unsigned char
1058 e100_receive_mdio_bit()
1061 *R_NETWORK_MGM_CTRL = 0;
1062 bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
1064 *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
1070 e100_reset_transceiver(void)
1073 unsigned short data;
1076 data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
1078 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (mdio_phy_addr << 7) | (MDIO_BASE_CONTROL_REG << 2);
1080 e100_send_mdio_cmd(cmd, 1);
1084 for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
1085 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1089 /* Called by upper layers if they decide it took too long to complete
1090 * sending a packet - we need to reset and stuff.
1094 e100_tx_timeout(struct net_device *dev)
1096 struct net_local *np = (struct net_local *)dev->priv;
1098 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
1099 tx_done(dev) ? "IRQ problem" : "network cable problem");
1101 /* remember we got an error */
1103 np->stats.tx_errors++;
1105 /* reset the TX DMA in case it has hung on something */
1107 RESET_DMA(NETWORK_TX_DMA_NBR);
1108 WAIT_DMA(NETWORK_TX_DMA_NBR);
1110 /* Reset the transceiver. */
1112 e100_reset_transceiver();
1114 /* and get rid of the packets that never got an interrupt */
1115 while (myFirstTxDesc != myNextTxDesc)
1117 dev_kfree_skb(myFirstTxDesc->skb);
1118 myFirstTxDesc->skb = 0;
1119 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1122 /* Set up transmit DMA channel so it can be restarted later */
1123 *R_DMA_CH0_FIRST = 0;
1124 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
1126 /* tell the upper layers we're ok again */
1128 netif_wake_queue(dev);
1132 /* This will only be invoked if the driver is _not_ in XOFF state.
1133 * What this means is that we need not check it, and that this
1134 * invariant will hold if we make sure that the netif_*_queue()
1135 * calls are done at the proper times.
1139 e100_send_packet(struct sk_buff *skb, struct net_device *dev)
1141 struct net_local *np = (struct net_local *)dev->priv;
1142 unsigned char *buf = skb->data;
1145 printk("send packet len %d\n", length);
1147 spin_lock_irq(&np->lock); /* protect from tx_interrupt and ourself */
1149 myNextTxDesc->skb = skb;
1151 dev->trans_start = jiffies;
1153 e100_hardware_send_packet(buf, skb->len);
1155 myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
1157 /* Stop queue if full */
1158 if (myNextTxDesc == myFirstTxDesc) {
1159 netif_stop_queue(dev);
1162 spin_unlock_irq(&np->lock);
1168 * The typical workload of the driver:
1169 * Handle the network interface interrupts.
1173 e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1175 struct net_device *dev = (struct net_device *)dev_id;
1176 struct net_local *np = (struct net_local *)dev->priv;
1177 unsigned long irqbits = *R_IRQ_MASK2_RD;
1179 /* Disable RX/TX IRQs to avoid reentrancy */
1181 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1182 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1184 /* Handle received packets */
1185 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
1186 /* acknowledge the eop interrupt */
1188 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
1190 /* check if one or more complete packets were indeed received */
1192 while (*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) {
1193 /* Take out the buffer and give it to the OS, then
1194 * allocate a new buffer to put a packet in.
1197 ((struct net_local *)dev->priv)->stats.rx_packets++;
1198 /* restart/continue on the channel, for safety */
1199 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
1200 /* clear dma channel 1 eop/descr irq bits */
1201 *R_DMA_CH1_CLR_INTR =
1202 IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
1203 IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
1205 /* now, we might have gotten another packet
1206 so we have to loop back and check if so */
1210 /* Report any packets that have been sent */
1211 while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
1212 myFirstTxDesc != myNextTxDesc)
1214 np->stats.tx_bytes += myFirstTxDesc->skb->len;
1215 np->stats.tx_packets++;
1217 /* dma is ready with the transmission of the data in tx_skb, so now
1218 we can release the skb memory */
1219 dev_kfree_skb_irq(myFirstTxDesc->skb);
1220 myFirstTxDesc->skb = 0;
1221 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1224 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1225 /* acknowledge the eop interrupt and wake up queue */
1226 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
1227 netif_wake_queue(dev);
1230 /* Enable RX/TX IRQs again */
1232 IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
1233 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
1237 e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1239 struct net_device *dev = (struct net_device *)dev_id;
1240 struct net_local *np = (struct net_local *)dev->priv;
1241 unsigned long irqbits = *R_IRQ_MASK0_RD;
1243 /* check for underrun irq */
1244 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
1245 *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
1246 np->stats.tx_errors++;
1247 D(printk("ethernet receiver underrun!\n"));
1250 /* check for overrun irq */
1251 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
1252 update_rx_stats(&np->stats); /* this will ack the irq */
1253 D(printk("ethernet receiver overrun!\n"));
1255 /* check for excessive collision irq */
1256 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
1257 *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
1258 np->stats.tx_errors++;
1259 D(printk("ethernet excessive collisions!\n"));
1263 /* We have a good packet(s), get it/them out of the buffers. */
1265 e100_rx(struct net_device *dev)
1267 struct sk_buff *skb;
1269 struct net_local *np = (struct net_local *)dev->priv;
1270 unsigned char *skb_data_ptr;
1275 if (!led_active && time_after(jiffies, led_next_time)) {
1276 /* light the network leds depending on the current speed. */
1277 e100_set_network_leds(NETWORK_ACTIVITY);
1279 /* Set the earliest time we may clear the LED */
1280 led_next_time = jiffies + NET_FLASH_TIME;
1282 mod_timer(&clear_led_timer, jiffies + HZ/10);
1285 length = myNextRxDesc->descr.hw_len - 4;
1286 ((struct net_local *)dev->priv)->stats.rx_bytes += length;
1289 printk("Got a packet of length %d:\n", length);
1290 /* dump the first bytes in the packet */
1291 skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
1292 for (i = 0; i < 8; i++) {
1293 printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
1294 skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
1295 skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
1300 if (length < RX_COPYBREAK) {
1301 /* Small packet, copy data */
1302 skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
1304 np->stats.rx_errors++;
1305 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1309 skb_put(skb, length - ETHER_HEAD_LEN); /* allocate room for the packet body */
1310 skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
1313 printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
1314 skb->head, skb->data, skb->tail, skb->end);
1315 printk("copying packet to 0x%x.\n", skb_data_ptr);
1318 memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
1321 /* Large packet, send directly to upper layers and allocate new
1322 * memory (aligned to cache line boundary to avoid bug).
1323 * Before sending the skb to upper layers we must make sure that
1324 * skb->data points to the aligned start of the packet.
1327 struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
1329 np->stats.rx_errors++;
1330 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1333 skb = myNextRxDesc->skb;
1334 align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
1335 skb_put(skb, length + align);
1336 skb_pull(skb, align); /* Remove alignment bytes */
1337 myNextRxDesc->skb = new_skb;
1338 myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
1342 skb->protocol = eth_type_trans(skb, dev);
1344 /* Send the packet to the upper layers */
1347 /* Prepare for next packet */
1348 myNextRxDesc->descr.status = 0;
1349 myPrevRxDesc = myNextRxDesc;
1350 myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
1354 /* Check if descriptors should be returned */
1355 if (rx_queue_len == RX_QUEUE_THRESHOLD) {
1356 flush_etrax_cache();
1357 myPrevRxDesc->descr.ctrl |= d_eol;
1358 myLastRxDesc->descr.ctrl &= ~d_eol;
1359 myLastRxDesc = myPrevRxDesc;
1364 /* The inverse routine to net_open(). */
1366 e100_close(struct net_device *dev)
1368 struct net_local *np = (struct net_local *)dev->priv;
1370 printk(KERN_INFO "Closing %s.\n", dev->name);
1372 netif_stop_queue(dev);
1375 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
1376 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
1377 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
1380 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
1381 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1382 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
1383 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1385 /* Stop the receiver and the transmitter */
1387 RESET_DMA(NETWORK_TX_DMA_NBR);
1388 RESET_DMA(NETWORK_RX_DMA_NBR);
1390 /* Flush the Tx and disable Rx here. */
1392 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
1393 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
1394 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
1396 free_dma(NETWORK_TX_DMA_NBR);
1397 free_dma(NETWORK_RX_DMA_NBR);
1399 /* Update the statistics here. */
1401 update_rx_stats(&np->stats);
1402 update_tx_stats(&np->stats);
1404 /* Stop speed/duplex timers */
1405 del_timer(&speed_timer);
1406 del_timer(&duplex_timer);
1411 e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1413 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&ifr->ifr_data;
1417 return e100_ethtool_ioctl(dev,ifr);
1418 case SIOCGMIIPHY: /* Get PHY address */
1419 data->phy_id = mdio_phy_addr;
1421 case SIOCGMIIREG: /* Read MII register */
1422 data->val_out = e100_get_mdio_reg(data->reg_num);
1424 case SIOCSMIIREG: /* Write MII register */
1425 e100_set_mdio_reg(data->reg_num, data->val_in);
1427 /* The ioctls below should be considered obsolete but are */
1428 /* still present for compatability with old scripts/apps */
1429 case SET_ETH_SPEED_10: /* 10 Mbps */
1432 case SET_ETH_SPEED_100: /* 100 Mbps */
1433 e100_set_speed(100);
1435 case SET_ETH_SPEED_AUTO: /* Auto negotiate speed */
1438 case SET_ETH_DUPLEX_HALF: /* Half duplex. */
1439 e100_set_duplex(half);
1441 case SET_ETH_DUPLEX_FULL: /* Full duplex. */
1442 e100_set_duplex(full);
1444 case SET_ETH_DUPLEX_AUTO: /* Autonegotiate duplex*/
1445 e100_set_duplex(autoneg);
1454 e100_ethtool_ioctl(struct net_device *dev, struct ifreq *ifr)
1456 struct ethtool_cmd ecmd;
1458 if (copy_from_user(&ecmd, ifr->ifr_data, sizeof (ecmd)))
1464 memset((void *) &ecmd, 0, sizeof (ecmd));
1466 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
1467 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1468 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
1469 ecmd.port = PORT_TP;
1470 ecmd.transceiver = XCVR_EXTERNAL;
1471 ecmd.phy_address = mdio_phy_addr;
1472 ecmd.speed = current_speed;
1473 ecmd.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
1474 ecmd.advertising = ADVERTISED_TP;
1475 if (current_duplex == autoneg && current_speed_selection == 0)
1476 ecmd.advertising |= ADVERTISED_Autoneg;
1479 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
1480 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
1481 if (current_speed_selection == 10)
1482 ecmd.advertising &= ~(ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full);
1483 else if (current_speed_selection == 100)
1484 ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full);
1485 if (current_duplex == half)
1486 ecmd.advertising &= ~(ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full);
1487 else if (current_duplex == full)
1488 ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_100baseT_Half);
1490 ecmd.autoneg = AUTONEG_ENABLE;
1491 if (copy_to_user(ifr->ifr_data, &ecmd, sizeof (ecmd)))
1497 if (!capable(CAP_NET_ADMIN)) {
1500 if (ecmd.autoneg == AUTONEG_ENABLE) {
1501 e100_set_duplex(autoneg);
1504 e100_set_duplex(ecmd.duplex == DUPLEX_HALF ? half : full);
1505 e100_set_speed(ecmd.speed == SPEED_10 ? 10: 100);
1509 case ETHTOOL_GDRVINFO:
1511 struct ethtool_drvinfo info;
1512 memset((void *) &info, 0, sizeof (info));
1513 strncpy(info.driver, "ETRAX 100LX", sizeof(info.driver) - 1);
1514 strncpy(info.version, "$Revision: 1.48 $", sizeof(info.version) - 1);
1515 strncpy(info.fw_version, "N/A", sizeof(info.fw_version) - 1);
1516 strncpy(info.bus_info, "N/A", sizeof(info.bus_info) - 1);
1517 info.regdump_len = 0;
1518 info.eedump_len = 0;
1519 info.testinfo_len = 0;
1520 if (copy_to_user(ifr->ifr_data, &info, sizeof (info)))
1524 case ETHTOOL_NWAY_RST:
1525 if (current_duplex == autoneg && current_speed_selection == 0)
1536 update_rx_stats(struct net_device_stats *es)
1538 unsigned long r = *R_REC_COUNTERS;
1539 /* update stats relevant to reception errors */
1540 es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
1541 es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
1542 es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
1543 es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
1547 update_tx_stats(struct net_device_stats *es)
1549 unsigned long r = *R_TR_COUNTERS;
1550 /* update stats relevant to transmission errors */
1552 IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
1553 IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
1554 es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
1558 * Get the current statistics.
1559 * This may be called with the card open or closed.
1561 static struct net_device_stats *
1562 e100_get_stats(struct net_device *dev)
1564 struct net_local *lp = (struct net_local *)dev->priv;
1566 update_rx_stats(&lp->stats);
1567 update_tx_stats(&lp->stats);
1573 * Set or clear the multicast filter for this adaptor.
1574 * num_addrs == -1 Promiscuous mode, receive all packets
1575 * num_addrs == 0 Normal mode, clear multicast list
1576 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1577 * and do best-effort filtering.
1580 set_multicast_list(struct net_device *dev)
1582 int num_addr = dev->mc_count;
1583 unsigned long int lo_bits;
1584 unsigned long int hi_bits;
1585 if (dev->flags & IFF_PROMISC)
1587 /* promiscuous mode */
1588 lo_bits = 0xfffffffful;
1589 hi_bits = 0xfffffffful;
1591 /* Enable individual receive */
1592 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
1593 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1594 } else if (dev->flags & IFF_ALLMULTI) {
1595 /* enable all multicasts */
1596 lo_bits = 0xfffffffful;
1597 hi_bits = 0xfffffffful;
1599 /* Disable individual receive */
1600 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1601 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1602 } else if (num_addr == 0) {
1603 /* Normal, clear the mc list */
1604 lo_bits = 0x00000000ul;
1605 hi_bits = 0x00000000ul;
1607 /* Disable individual receive */
1608 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1609 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1611 /* MC mode, receive normal and MC packets */
1613 struct dev_mc_list *dmi = dev->mc_list;
1616 lo_bits = 0x00000000ul;
1617 hi_bits = 0x00000000ul;
1618 for (i=0; i<num_addr; i++) {
1619 /* Calculate the hash index for the GA registers */
1622 baddr = dmi->dmi_addr;
1623 hash_ix ^= (*baddr) & 0x3f;
1624 hash_ix ^= ((*baddr) >> 6) & 0x03;
1626 hash_ix ^= ((*baddr) << 2) & 0x03c;
1627 hash_ix ^= ((*baddr) >> 4) & 0xf;
1629 hash_ix ^= ((*baddr) << 4) & 0x30;
1630 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1632 hash_ix ^= (*baddr) & 0x3f;
1633 hash_ix ^= ((*baddr) >> 6) & 0x03;
1635 hash_ix ^= ((*baddr) << 2) & 0x03c;
1636 hash_ix ^= ((*baddr) >> 4) & 0xf;
1638 hash_ix ^= ((*baddr) << 4) & 0x30;
1639 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1643 if (hash_ix >= 32) {
1644 hi_bits |= (1 << (hash_ix-32));
1647 lo_bits |= (1 << hash_ix);
1651 /* Disable individual receive */
1652 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1653 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1655 *R_NETWORK_GA_0 = lo_bits;
1656 *R_NETWORK_GA_1 = hi_bits;
1660 e100_hardware_send_packet(char *buf, int length)
1662 D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
1664 if (!led_active && time_after(jiffies, led_next_time)) {
1665 /* light the network leds depending on the current speed. */
1666 e100_set_network_leds(NETWORK_ACTIVITY);
1668 /* Set the earliest time we may clear the LED */
1669 led_next_time = jiffies + NET_FLASH_TIME;
1671 mod_timer(&clear_led_timer, jiffies + HZ/10);
1674 /* configure the tx dma descriptor */
1675 myNextTxDesc->descr.sw_len = length;
1676 myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
1677 myNextTxDesc->descr.buf = virt_to_phys(buf);
1679 /* Move end of list */
1680 myLastTxDesc->descr.ctrl &= ~d_eol;
1681 myLastTxDesc = myNextTxDesc;
1683 /* Restart DMA channel */
1684 *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
1688 e100_clear_network_leds(unsigned long dummy)
1690 if (led_active && time_after(jiffies, led_next_time)) {
1691 e100_set_network_leds(NO_NETWORK_ACTIVITY);
1693 /* Set the earliest time we may set the LED */
1694 led_next_time = jiffies + NET_FLASH_PAUSE;
1700 e100_set_network_leds(int active)
1702 #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
1703 int light_leds = (active == NO_NETWORK_ACTIVITY);
1704 #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
1705 int light_leds = (active == NETWORK_ACTIVITY);
1707 #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
1710 if (!current_speed) {
1711 /* Make LED red, link is down */
1712 #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
1713 LED_NETWORK_SET(LED_RED);
1715 LED_NETWORK_SET(LED_OFF);
1718 else if (light_leds) {
1719 if (current_speed == 10) {
1720 LED_NETWORK_SET(LED_ORANGE);
1722 LED_NETWORK_SET(LED_GREEN);
1726 LED_NETWORK_SET(LED_OFF);
1730 static struct net_device dev_etrax_ethernet; /* only got one */
1733 etrax_init_module(void)
1735 struct net_device *d = &dev_etrax_ethernet;
1737 d->init = etrax_ethernet_init;
1739 if (register_netdev(d) == 0)
1746 e100_boot_setup(char* str)
1748 struct sockaddr sa = {0};
1751 /* Parse the colon separated Ethernet station address */
1752 for (i = 0; i < ETH_ALEN; i++) {
1754 if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
1755 printk(KERN_WARNING "Malformed station address");
1758 sa.sa_data[i] = (char)tmp;
1765 __setup("etrax100_eth=", e100_boot_setup);
1767 module_init(etrax_init_module);