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