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