eth_v10.c 54.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
/* $Id: ethernet.c,v 1.31 2004/10/18 14:49:03 starvik Exp $
 *
 * e100net.c: A network driver for the ETRAX 100LX network controller.
 *
 * Copyright (c) 1998-2002 Axis Communications AB.
 *
 * The outline of this driver comes from skeleton.c.
 *
 * $Log: ethernet.c,v $
 * Revision 1.31  2004/10/18 14:49:03  starvik
 * Use RX interrupt as random source
 *
 * Revision 1.30  2004/09/29 10:44:04  starvik
 * Enabed MAC-address output again
 *
 * Revision 1.29  2004/08/24 07:14:05  starvik
 * Make use of generic MDIO interface and constants.
 *
 * Revision 1.28  2004/08/20 09:37:11  starvik
 * Added support for Intel LXT972A. Creds to Randy Scarborough.
 *
 * Revision 1.27  2004/08/16 12:37:22  starvik
 * Merge of Linux 2.6.8
 *
 * Revision 1.25  2004/06/21 10:29:57  starvik
 * Merge of Linux 2.6.7
 *
 * Revision 1.23  2004/06/09 05:29:22  starvik
 * Avoid any race where R_DMA_CH1_FIRST is NULL (may trigger cache bug).
 *
 * Revision 1.22  2004/05/14 07:58:03  starvik
 * Merge of changes from 2.4
 *
 * Revision 1.20  2004/03/11 11:38:40  starvik
 * Merge of Linux 2.6.4
 *
 * Revision 1.18  2003/12/03 13:45:46  starvik
 * Use hardware pad for short packets to prevent information leakage.
 *
 * Revision 1.17  2003/07/04 08:27:37  starvik
 * Merge of Linux 2.5.74
 *
 * Revision 1.16  2003/04/24 08:28:22  starvik
 * New LED behaviour: LED off when no link
 *
 * Revision 1.15  2003/04/09 05:20:47  starvik
 * Merge of Linux 2.5.67
 *
 * Revision 1.13  2003/03/06 16:11:01  henriken
 * Off by one error in group address register setting.
 *
 * Revision 1.12  2003/02/27 17:24:19  starvik
 * Corrected Rev to Revision
 *
 * Revision 1.11  2003/01/24 09:53:21  starvik
 * Oops. Initialize GA to 0, not to 1
 *
 * Revision 1.10  2003/01/24 09:50:55  starvik
 * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
 *
 * Revision 1.9  2002/12/13 07:40:58  starvik
 * Added basic ethtool interface
 * Handled out of memory when allocating new buffers
 *
 * Revision 1.8  2002/12/11 13:13:57  starvik
 * Added arch/ to v10 specific includes
 * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
 *
 * Revision 1.7  2002/11/26 09:41:42  starvik
 * Added e100_set_config (standard interface to set media type)
 * Added protection against preemptive scheduling
 * Added standard MII ioctls
 *
 * Revision 1.6  2002/11/21 07:18:18  starvik
 * Timers must be initialized in 2.5.48
 *
 * Revision 1.5  2002/11/20 11:56:11  starvik
 * Merge of Linux 2.5.48
 *
 * Revision 1.4  2002/11/18 07:26:46  starvik
 * Linux 2.5 port of latest Linux 2.4 ethernet driver
 *
 * Revision 1.33  2002/10/02 20:16:17  hp
 * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
 *
 * Revision 1.32  2002/09/16 06:05:58  starvik
 * Align memory returned by dev_alloc_skb
 * Moved handling of sent packets to interrupt to avoid reference counting problem
 *
 * Revision 1.31  2002/09/10 13:28:23  larsv
 * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
 * for supported functionality by issuing special ioctls, i.e. wireless
 * extensions.
 *
 * Revision 1.30  2002/05/07 18:50:08  johana
 * Correct spelling in comments.
 *
 * Revision 1.29  2002/05/06 05:38:49  starvik
 * Performance improvements:
 *    Large packets are not copied (breakpoint set to 256 bytes)
 *    The cache bug workaround is delayed until half of the receive list
 *      has been used
 *    Added transmit list
 *    Transmit interrupts are only enabled when transmit queue is full
 *
 * Revision 1.28.2.1  2002/04/30 08:15:51  starvik
 * Performance improvements:
 *   Large packets are not copied (breakpoint set to 256 bytes)
 *   The cache bug workaround is delayed until half of the receive list
 *     has been used.
 *   Added transmit list
 *   Transmit interrupts are only enabled when transmit queue is full
 *
 * Revision 1.28  2002/04/22 11:47:21  johana
 * Fix according to 2.4.19-pre7. time_after/time_before and
 * missing end of comment.
 * The patch has a typo for ethernet.c in e100_clear_network_leds(),
 *  that is fixed here.
 *
 * Revision 1.27  2002/04/12 11:55:11  bjornw
 * Added TODO
 *
 * Revision 1.26  2002/03/15 17:11:02  bjornw
 * Use prepare_rx_descriptor after the CPU has touched the receiving descs
 *
 * Revision 1.25  2002/03/08 13:07:53  bjornw
 * Unnecessary spinlock removed
 *
 * Revision 1.24  2002/02/20 12:57:43  fredriks
 * Replaced MIN() with min().
 *
 * Revision 1.23  2002/02/20 10:58:14  fredriks
 * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
 *
 * Revision 1.22  2002/01/30 07:48:22  matsfg
 * Initiate R_NETWORK_TR_CTRL
 *
 * Revision 1.21  2001/11/23 11:54:49  starvik
 * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
 * Removed compiler warnings
 *
 * Revision 1.20  2001/11/12 19:26:00  pkj
 * * Corrected e100_negotiate() to not assign half to current_duplex when
 *   it was supposed to compare them...
 * * Cleaned up failure handling in e100_open().
 * * Fixed compiler warnings.
 *
 * Revision 1.19  2001/11/09 07:43:09  starvik
 * Added full duplex support
 * Added ioctl to set speed and duplex
 * Clear LED timer only runs when LED is lit
 *
 * Revision 1.18  2001/10/03 14:40:43  jonashg
 * Update rx_bytes counter.
 *
 * Revision 1.17  2001/06/11 12:43:46  olof
 * Modified defines for network LED behavior
 *
 * Revision 1.16  2001/05/30 06:12:46  markusl
 * TxDesc.next should not be set to NULL
 *
 * Revision 1.15  2001/05/29 10:27:04  markusl
 * Updated after review remarks:
 * +Use IO_EXTRACT
 * +Handle underrun
 *
 * Revision 1.14  2001/05/29 09:20:14  jonashg
 * Use driver name on printk output so one can tell which driver that complains.
 *
 * Revision 1.13  2001/05/09 12:35:59  johana
 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
 *
 * Revision 1.12  2001/04/05 11:43:11  tobiasa
 * Check dev before panic.
 *
 * Revision 1.11  2001/04/04 11:21:05  markusl
 * Updated according to review remarks
 *
 * Revision 1.10  2001/03/26 16:03:06  bjornw
 * Needs linux/config.h
 *
 * Revision 1.9  2001/03/19 14:47:48  pkj
 * * Make sure there is always a pause after the network LEDs are
 *   changed so they will not look constantly lit during heavy traffic.
 * * Always use HZ when setting times relative to jiffies.
 * * Use LED_NETWORK_SET() when setting the network LEDs.
 *
 * Revision 1.8  2001/02/27 13:52:48  bjornw
 * malloc.h -> slab.h
 *
 * Revision 1.7  2001/02/23 13:46:38  bjornw
 * Spellling check
 *
 * Revision 1.6  2001/01/26 15:21:04  starvik
 * Don't disable interrupts while reading MDIO registers (MDIO is slow)
 * Corrected promiscuous mode
 * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
 *
 * Revision 1.5  2000/11/29 17:22:22  bjornw
 * Get rid of the udword types legacy stuff
 *
 * Revision 1.4  2000/11/22 16:36:09  bjornw
 * Please marketing by using the correct case when spelling Etrax.
 *
 * Revision 1.3  2000/11/21 16:43:04  bjornw
 * Minor short->int change
 *
 * Revision 1.2  2000/11/08 14:27:57  bjornw
 * 2.4 port
 *
 * Revision 1.1  2000/11/06 13:56:00  bjornw
 * Verbatim copy of the 1.24 version of e100net.c from elinux
 *
 * Revision 1.24  2000/10/04 15:55:23  bjornw
 * * Use virt_to_phys etc. for DMA addresses
 * * Removed bogus CHECKSUM_UNNECESSARY
 *
 *
 */


#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/init.h>
J
Jiri Slaby 已提交
237
#include <linux/bitops.h>
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

#include <linux/if.h>
#include <linux/mii.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>

#include <asm/arch/svinto.h>/* DMA and register descriptions */
#include <asm/io.h>         /* LED_* I/O functions */
#include <asm/irq.h>
#include <asm/dma.h>
#include <asm/system.h>
#include <asm/ethernet.h>
#include <asm/cache.h>
253
#include <asm/arch/io_interface_mux.h>
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

//#define ETHDEBUG
#define D(x)

/*
 * The name of the card. Is used for messages and in the requests for
 * io regions, irqs and dma channels
 */

static const char* cardname = "ETRAX 100LX built-in ethernet controller";

/* A default ethernet address. Highlevel SW will set the real one later */

static struct sockaddr default_mac = {
	0,
	{ 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
};

/* Information that need to be kept for each board. */
struct net_local {
	struct net_device_stats stats;
	struct mii_if_info mii_if;

	/* Tx control lock.  This protects the transmit buffer ring
	 * state along with the "tx full" state of the driver.  This
	 * means all netif_queue flow control actions are protected
	 * by this lock as well.
	 */
	spinlock_t lock;
283 284 285

	spinlock_t led_lock; /* Protect LED state */
	spinlock_t transceiver_lock; /* Protect transceiver state. */
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
};

typedef struct etrax_eth_descr
{
	etrax_dma_descr descr;
	struct sk_buff* skb;
} etrax_eth_descr;

/* Some transceivers requires special handling */
struct transceiver_ops
{
	unsigned int oui;
	void (*check_speed)(struct net_device* dev);
	void (*check_duplex)(struct net_device* dev);
};

/* Duplex settings */
enum duplex
{
	half,
	full,
	autoneg
};

/* Dma descriptors etc. */

312
#define MAX_MEDIA_DATA_SIZE 1522
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

#define MIN_PACKET_LEN      46
#define ETHER_HEAD_LEN      14

/*
** MDIO constants.
*/
#define MDIO_START                          0x1
#define MDIO_READ                           0x2
#define MDIO_WRITE                          0x1
#define MDIO_PREAMBLE              0xfffffffful

/* Broadcom specific */
#define MDIO_AUX_CTRL_STATUS_REG           0x18
#define MDIO_BC_FULL_DUPLEX_IND             0x1
#define MDIO_BC_SPEED                       0x2

/* TDK specific */
#define MDIO_TDK_DIAGNOSTIC_REG              18
#define MDIO_TDK_DIAGNOSTIC_RATE          0x400
#define MDIO_TDK_DIAGNOSTIC_DPLX          0x800

/*Intel LXT972A specific*/
#define MDIO_INT_STATUS_REG_2			0x0011
337 338
#define MDIO_INT_FULL_DUPLEX_IND       (1 << 9)
#define MDIO_INT_SPEED                (1 << 14)
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348

/* Network flash constants */
#define NET_FLASH_TIME                  (HZ/50) /* 20 ms */
#define NET_FLASH_PAUSE                (HZ/100) /* 10 ms */
#define NET_LINK_UP_CHECK_INTERVAL       (2*HZ) /* 2 s   */
#define NET_DUPLEX_CHECK_INTERVAL        (2*HZ) /* 2 s   */

#define NO_NETWORK_ACTIVITY 0
#define NETWORK_ACTIVITY    1

349 350
#define NBR_OF_RX_DESC     32
#define NBR_OF_TX_DESC     16
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

/* Large packets are sent directly to upper layers while small packets are */
/* copied (to reduce memory waste). The following constant decides the breakpoint */
#define RX_COPYBREAK 256

/* Due to a chip bug we need to flush the cache when descriptors are returned */
/* to the DMA. To decrease performance impact we return descriptors in chunks. */
/* The following constant determines the number of descriptors to return. */
#define RX_QUEUE_THRESHOLD  NBR_OF_RX_DESC/2

#define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)

/* Define some macros to access ETRAX 100 registers */
#define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
					  IO_FIELD_(reg##_, field##_, val)
#define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
					  IO_STATE_(reg##_, field##_, _##val)

static etrax_eth_descr *myNextRxDesc;  /* Points to the next descriptor to
                                          to be processed */
static etrax_eth_descr *myLastRxDesc;  /* The last processed descriptor */

static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));

static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
static etrax_eth_descr* myLastTxDesc;  /* End of send queue */
static etrax_eth_descr* myNextTxDesc;  /* Next descriptor to use */
static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));

static unsigned int network_rec_config_shadow = 0;

static unsigned int network_tr_ctrl_shadow = 0;

/* Network speed indication. */
385 386
static DEFINE_TIMER(speed_timer, NULL, 0, 0);
static DEFINE_TIMER(clear_led_timer, NULL, 0, 0);
L
Linus Torvalds 已提交
387 388 389 390 391 392 393
static int current_speed; /* Speed read from transceiver */
static int current_speed_selection; /* Speed selected by user */
static unsigned long led_next_time;
static int led_active;
static int rx_queue_len;

/* Duplex */
394
static DEFINE_TIMER(duplex_timer, NULL, 0, 0);
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404
static int full_duplex;
static enum duplex current_duplex;

/* Index to functions, as function prototypes. */

static int etrax_ethernet_init(void);

static int e100_open(struct net_device *dev);
static int e100_set_mac_address(struct net_device *dev, void *addr);
static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
405 406
static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id);
static irqreturn_t e100nw_interrupt(int irq, void *dev_id);
L
Linus Torvalds 已提交
407 408 409 410 411 412 413
static void e100_rx(struct net_device *dev);
static int e100_close(struct net_device *dev);
static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
static int e100_set_config(struct net_device* dev, struct ifmap* map);
static void e100_tx_timeout(struct net_device *dev);
static struct net_device_stats *e100_get_stats(struct net_device *dev);
static void set_multicast_list(struct net_device *dev);
414
static void e100_hardware_send_packet(struct net_local* np, char *buf, int length);
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
static void update_rx_stats(struct net_device_stats *);
static void update_tx_stats(struct net_device_stats *);
static int e100_probe_transceiver(struct net_device* dev);

static void e100_check_speed(unsigned long priv);
static void e100_set_speed(struct net_device* dev, unsigned long speed);
static void e100_check_duplex(unsigned long priv);
static void e100_set_duplex(struct net_device* dev, enum duplex);
static void e100_negotiate(struct net_device* dev);

static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);

static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
static void e100_send_mdio_bit(unsigned char bit);
static unsigned char e100_receive_mdio_bit(void);
static void e100_reset_transceiver(struct net_device* net);

static void e100_clear_network_leds(unsigned long dummy);
static void e100_set_network_leds(int active);

436
static const struct ethtool_ops e100_ethtool_ops;
437 438 439 440
#if defined(CONFIG_ETRAX_NO_PHY)
static void dummy_check_speed(struct net_device* dev);
static void dummy_check_duplex(struct net_device* dev);
#else
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448
static void broadcom_check_speed(struct net_device* dev);
static void broadcom_check_duplex(struct net_device* dev);
static void tdk_check_speed(struct net_device* dev);
static void tdk_check_duplex(struct net_device* dev);
static void intel_check_speed(struct net_device* dev);
static void intel_check_duplex(struct net_device* dev);
static void generic_check_speed(struct net_device* dev);
static void generic_check_duplex(struct net_device* dev);
449 450 451 452 453 454
#endif
#ifdef CONFIG_NET_POLL_CONTROLLER
static void e100_netpoll(struct net_device* dev);
#endif

static int autoneg_normal = 1;
L
Linus Torvalds 已提交
455 456 457

struct transceiver_ops transceivers[] =
{
458 459 460
#if defined(CONFIG_ETRAX_NO_PHY)
	{0x0000, dummy_check_speed, dummy_check_duplex}        /* Dummy */
#else
L
Linus Torvalds 已提交
461 462 463 464 465
	{0x1018, broadcom_check_speed, broadcom_check_duplex},  /* Broadcom */
	{0xC039, tdk_check_speed, tdk_check_duplex},            /* TDK 2120 */
	{0x039C, tdk_check_speed, tdk_check_duplex},            /* TDK 2120C */
        {0x04de, intel_check_speed, intel_check_duplex},     	/* Intel LXT972A*/
	{0x0000, generic_check_speed, generic_check_duplex}     /* Generic, must be last */
466
#endif
L
Linus Torvalds 已提交
467 468
};

469 470
struct transceiver_ops* transceiver = &transceivers[0];

L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
#define tx_done(dev) (*R_DMA_CH0_CMD == 0)

/*
 * Check for a network adaptor of this type, and return '0' if one exists.
 * If dev->base_addr == 0, probe all likely locations.
 * If dev->base_addr == 1, always return failure.
 * If dev->base_addr == 2, allocate space for the device and return success
 * (detachable devices only).
 */

static int __init
etrax_ethernet_init(void)
{
	struct net_device *dev;
        struct net_local* np;
	int i, err;

	printk(KERN_INFO
489
	       "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 1998-2007 Axis Communications AB\n");
L
Linus Torvalds 已提交
490

491 492 493 494
	if (cris_request_io_interface(if_eth, cardname)) {
		printk(KERN_CRIT "etrax_ethernet_init failed to get IO interface\n");
		return -EBUSY;
	}
L
Linus Torvalds 已提交
495

496
	dev = alloc_etherdev(sizeof(struct net_local));
L
Linus Torvalds 已提交
497 498 499
	if (!dev)
		return -ENOMEM;

500 501 502 503 504
	np = netdev_priv(dev);

	/* we do our own locking */
	dev->features |= NETIF_F_LLTX;

L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */

	/* now setup our etrax specific stuff */

	dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
	dev->dma = NETWORK_RX_DMA_NBR;

	/* fill in our handlers so the network layer can talk to us in the future */

	dev->open               = e100_open;
	dev->hard_start_xmit    = e100_send_packet;
	dev->stop               = e100_close;
	dev->get_stats          = e100_get_stats;
	dev->set_multicast_list = set_multicast_list;
	dev->set_mac_address    = e100_set_mac_address;
520
	dev->ethtool_ops	= &e100_ethtool_ops;
L
Linus Torvalds 已提交
521 522 523
	dev->do_ioctl           = e100_ioctl;
	dev->set_config		= e100_set_config;
	dev->tx_timeout         = e100_tx_timeout;
524 525 526 527 528 529 530
#ifdef CONFIG_NET_POLL_CONTROLLER
	dev->poll_controller = e100_netpoll;
#endif

	spin_lock_init(&np->lock);
	spin_lock_init(&np->led_lock);
	spin_lock_init(&np->transceiver_lock);
L
Linus Torvalds 已提交
531 532 533 534 535 536

	/* Initialise the list of Etrax DMA-descriptors */

	/* Initialise receive descriptors */

	for (i = 0; i < NBR_OF_RX_DESC; i++) {
537 538 539
		/* Allocate two extra cachelines to make sure that buffer used
		 * by DMA does not share cacheline with any other data (to
		 * avoid cache bug)
L
Linus Torvalds 已提交
540 541
		 */
		RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
542 543
		if (!RxDescList[i].skb)
			return -ENOMEM;
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
		RxDescList[i].descr.ctrl   = 0;
		RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
		RxDescList[i].descr.next   = virt_to_phys(&RxDescList[i + 1]);
		RxDescList[i].descr.buf    = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
		RxDescList[i].descr.status = 0;
		RxDescList[i].descr.hw_len = 0;
		prepare_rx_descriptor(&RxDescList[i].descr);
	}

	RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl   = d_eol;
	RxDescList[NBR_OF_RX_DESC - 1].descr.next   = virt_to_phys(&RxDescList[0]);
	rx_queue_len = 0;

	/* Initialize transmit descriptors */
	for (i = 0; i < NBR_OF_TX_DESC; i++) {
		TxDescList[i].descr.ctrl   = 0;
		TxDescList[i].descr.sw_len = 0;
		TxDescList[i].descr.next   = virt_to_phys(&TxDescList[i + 1].descr);
		TxDescList[i].descr.buf    = 0;
		TxDescList[i].descr.status = 0;
		TxDescList[i].descr.hw_len = 0;
		TxDescList[i].skb = 0;
	}

	TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl   = d_eol;
	TxDescList[NBR_OF_TX_DESC - 1].descr.next   = virt_to_phys(&TxDescList[0].descr);

	/* Initialise initial pointers */

	myNextRxDesc  = &RxDescList[0];
	myLastRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
	myFirstTxDesc = &TxDescList[0];
	myNextTxDesc  = &TxDescList[0];
	myLastTxDesc  = &TxDescList[NBR_OF_TX_DESC - 1];

	/* Register device */
	err = register_netdev(dev);
	if (err) {
		free_netdev(dev);
		return err;
	}

	/* set the default MAC address */

	e100_set_mac_address(dev, &default_mac);

	/* Initialize speed indicator stuff. */

	current_speed = 10;
	current_speed_selection = 0; /* Auto */
	speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
595
	speed_timer.data = (unsigned long)dev;
L
Linus Torvalds 已提交
596 597 598
	speed_timer.function = e100_check_speed;

	clear_led_timer.function = e100_clear_network_leds;
599
	clear_led_timer.data = (unsigned long)dev;
L
Linus Torvalds 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

	full_duplex = 0;
	current_duplex = autoneg;
	duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
        duplex_timer.data = (unsigned long)dev;
	duplex_timer.function = e100_check_duplex;

        /* Initialize mii interface */
	np->mii_if.phy_id_mask = 0x1f;
	np->mii_if.reg_num_mask = 0x1f;
	np->mii_if.dev = dev;
	np->mii_if.mdio_read = e100_get_mdio_reg;
	np->mii_if.mdio_write = e100_set_mdio_reg;

	/* Initialize group address registers to make sure that no */
	/* unwanted addresses are matched */
	*R_NETWORK_GA_0 = 0x00000000;
	*R_NETWORK_GA_1 = 0x00000000;
618 619 620

	/* Initialize next time the led can flash */
	led_next_time = jiffies;
L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628 629 630
	return 0;
}

/* set MAC address of the interface. called from the core after a
 * SIOCSIFADDR ioctl, and from the bootup above.
 */

static int
e100_set_mac_address(struct net_device *dev, void *p)
{
631
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	struct sockaddr *addr = p;
	int i;

	spin_lock(&np->lock); /* preemption protection */

	/* remember it */

	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

	/* Write it to the hardware.
	 * Note the way the address is wrapped:
	 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
	 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
	 */

	*R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
		(dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
	*R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
	*R_NETWORK_SA_2 = 0;

	/* show it in the log as well */

654 655
	printk(KERN_INFO "%s: changed MAC to %s\n",
	       dev->name, print_mac(mac, dev->dev_addr));
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

	spin_unlock(&np->lock);

	return 0;
}

/*
 * Open/initialize the board. This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */

static int
e100_open(struct net_device *dev)
{
	unsigned long flags;

	/* enable the MDIO output pin */

	*R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);

	*R_IRQ_MASK0_CLR =
		IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
		IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
		IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);

	/* clear dma0 and 1 eop and descr irq masks */
	*R_IRQ_MASK2_CLR =
		IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);

	/* Reset and wait for the DMA channels */

	RESET_DMA(NETWORK_TX_DMA_NBR);
	RESET_DMA(NETWORK_RX_DMA_NBR);
	WAIT_DMA(NETWORK_TX_DMA_NBR);
	WAIT_DMA(NETWORK_RX_DMA_NBR);

	/* Initialise the etrax network controller */

	/* allocate the irq corresponding to the receiving DMA */

	if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt,
704
			IRQF_SAMPLE_RANDOM, cardname, (void *)dev)) {
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
		goto grace_exit0;
	}

	/* allocate the irq corresponding to the transmitting DMA */

	if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
			cardname, (void *)dev)) {
		goto grace_exit1;
	}

	/* allocate the irq corresponding to the network errors etc */

	if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
			cardname, (void *)dev)) {
		goto grace_exit2;
	}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	/*
	 * Always allocate the DMA channels after the IRQ,
	 * and clean up on failure.
	 */

	if (cris_request_dma(NETWORK_TX_DMA_NBR,
	                     cardname,
	                     DMA_VERBOSE_ON_ERROR,
	                     dma_eth)) {
		goto grace_exit3;
        }

	if (cris_request_dma(NETWORK_RX_DMA_NBR,
	                     cardname,
	                     DMA_VERBOSE_ON_ERROR,
	                     dma_eth)) {
		goto grace_exit4;
        }

L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754
	/* give the HW an idea of what MAC address we want */

	*R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
		(dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
	*R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
	*R_NETWORK_SA_2 = 0;

#if 0
	/* use promiscuous mode for testing */
	*R_NETWORK_GA_0 = 0xffffffff;
	*R_NETWORK_GA_1 = 0xffffffff;

	*R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
#else
755
	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, max_size, size1522);
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
	SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
	SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
	*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
#endif

	*R_NETWORK_GEN_CONFIG =
		IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
		IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);

	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
	SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
	*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;

775
	local_irq_save(flags);
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

	/* enable the irq's for ethernet DMA */

	*R_IRQ_MASK2_SET =
		IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
		IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);

	*R_IRQ_MASK0_SET =
		IO_STATE(R_IRQ_MASK0_SET, overrun,       set) |
		IO_STATE(R_IRQ_MASK0_SET, underrun,      set) |
		IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);

	/* make sure the irqs are cleared */

	*R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
	*R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);

	/* make sure the rec and transmit error counters are cleared */

	(void)*R_REC_COUNTERS;  /* dummy read */
	(void)*R_TR_COUNTERS;   /* dummy read */

	/* start the receiving DMA channel so we can receive packets from now on */

	*R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
	*R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);

	/* Set up transmit DMA channel so it can be restarted later */

	*R_DMA_CH0_FIRST = 0;
	*R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
807
	netif_start_queue(dev);
L
Linus Torvalds 已提交
808

809
	local_irq_restore(flags);
L
Linus Torvalds 已提交
810 811 812

	/* Probe for transceiver */
	if (e100_probe_transceiver(dev))
813
		goto grace_exit5;
L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821

	/* Start duplex/speed timers */
	add_timer(&speed_timer);
	add_timer(&duplex_timer);

	/* We are now ready to accept transmit requeusts from
	 * the queueing layer of the networking.
	 */
822
	netif_carrier_on(dev);
L
Linus Torvalds 已提交
823 824 825

	return 0;

826 827 828 829
grace_exit5:
	cris_free_dma(NETWORK_RX_DMA_NBR, cardname);
grace_exit4:
	cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
L
Linus Torvalds 已提交
830 831 832 833 834 835 836 837 838 839
grace_exit3:
	free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
grace_exit2:
	free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
grace_exit1:
	free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
grace_exit0:
	return -EAGAIN;
}

840 841 842 843 844 845 846
#if defined(CONFIG_ETRAX_NO_PHY)
static void
dummy_check_speed(struct net_device* dev)
{
	current_speed = 100;
}
#else
L
Linus Torvalds 已提交
847 848 849 850
static void
generic_check_speed(struct net_device* dev)
{
	unsigned long data;
851 852 853
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863 864
	if ((data & ADVERTISE_100FULL) ||
	    (data & ADVERTISE_100HALF))
		current_speed = 100;
	else
		current_speed = 10;
}

static void
tdk_check_speed(struct net_device* dev)
{
	unsigned long data;
865 866 867 868
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_TDK_DIAGNOSTIC_REG);
L
Linus Torvalds 已提交
869 870 871 872 873 874 875
	current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
}

static void
broadcom_check_speed(struct net_device* dev)
{
	unsigned long data;
876 877 878 879
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_AUX_CTRL_STATUS_REG);
L
Linus Torvalds 已提交
880 881 882 883 884 885 886
	current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
}

static void
intel_check_speed(struct net_device* dev)
{
	unsigned long data;
887 888 889 890
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_INT_STATUS_REG_2);
L
Linus Torvalds 已提交
891 892
	current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
}
893
#endif
L
Linus Torvalds 已提交
894 895 896 897
static void
e100_check_speed(unsigned long priv)
{
	struct net_device* dev = (struct net_device*)priv;
898
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
899 900 901 902
	static int led_initiated = 0;
	unsigned long data;
	int old_speed = current_speed;

903 904 905
	spin_lock(&np->transceiver_lock);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMSR);
L
Linus Torvalds 已提交
906 907 908 909 910 911
	if (!(data & BMSR_LSTATUS)) {
		current_speed = 0;
	} else {
		transceiver->check_speed(dev);
	}

912
	spin_lock(&np->led_lock);
L
Linus Torvalds 已提交
913 914 915
	if ((old_speed != current_speed) || !led_initiated) {
		led_initiated = 1;
		e100_set_network_leds(NO_NETWORK_ACTIVITY);
916 917 918 919
		if (current_speed)
			netif_carrier_on(dev);
		else
			netif_carrier_off(dev);
L
Linus Torvalds 已提交
920
	}
921
	spin_unlock(&np->led_lock);
L
Linus Torvalds 已提交
922 923 924 925

	/* Reinitialize the timer. */
	speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
	add_timer(&speed_timer);
926 927

	spin_unlock(&np->transceiver_lock);
L
Linus Torvalds 已提交
928 929 930 931 932
}

static void
e100_negotiate(struct net_device* dev)
{
933 934 935
	struct net_local *np = netdev_priv(dev);
	unsigned short data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
						MII_ADVERTISE);
L
Linus Torvalds 已提交
936 937 938 939 940 941

	/* Discard old speed and duplex settings */
	data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL |
	          ADVERTISE_10HALF | ADVERTISE_10FULL);

	switch (current_speed_selection) {
942
		case 10:
L
Linus Torvalds 已提交
943 944 945 946 947 948 949 950
			if (current_duplex == full)
				data |= ADVERTISE_10FULL;
			else if (current_duplex == half)
				data |= ADVERTISE_10HALF;
			else
				data |= ADVERTISE_10HALF | ADVERTISE_10FULL;
			break;

951
		case 100:
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959
			 if (current_duplex == full)
				data |= ADVERTISE_100FULL;
			else if (current_duplex == half)
				data |= ADVERTISE_100HALF;
			else
				data |= ADVERTISE_100HALF | ADVERTISE_100FULL;
			break;

960
		case 0: /* Auto */
L
Linus Torvalds 已提交
961 962 963 964 965 966 967 968 969
			 if (current_duplex == full)
				data |= ADVERTISE_100FULL | ADVERTISE_10FULL;
			else if (current_duplex == half)
				data |= ADVERTISE_100HALF | ADVERTISE_10HALF;
			else
				data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
				  ADVERTISE_100HALF | ADVERTISE_100FULL;
			break;

970
		default: /* assume autoneg speed and duplex */
L
Linus Torvalds 已提交
971 972
			data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
				  ADVERTISE_100HALF | ADVERTISE_100FULL;
973
			break;
L
Linus Torvalds 已提交
974 975
	}

976
	e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE, data);
L
Linus Torvalds 已提交
977 978

	/* Renegotiate with link partner */
979 980
	if (autoneg_normal) {
	  data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
L
Linus Torvalds 已提交
981
	data |= BMCR_ANENABLE | BMCR_ANRESTART;
982 983
	}
	e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR, data);
L
Linus Torvalds 已提交
984 985 986 987 988
}

static void
e100_set_speed(struct net_device* dev, unsigned long speed)
{
989 990 991
	struct net_local *np = netdev_priv(dev);

	spin_lock(&np->transceiver_lock);
L
Linus Torvalds 已提交
992 993 994 995
	if (speed != current_speed_selection) {
		current_speed_selection = speed;
		e100_negotiate(dev);
	}
996
	spin_unlock(&np->transceiver_lock);
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002
}

static void
e100_check_duplex(unsigned long priv)
{
	struct net_device *dev = (struct net_device *)priv;
1003 1004 1005 1006 1007
	struct net_local *np = netdev_priv(dev);
	int old_duplex;

	spin_lock(&np->transceiver_lock);
	old_duplex = full_duplex;
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	transceiver->check_duplex(dev);
	if (old_duplex != full_duplex) {
		/* Duplex changed */
		SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
	}

	/* Reinitialize the timer. */
	duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
	add_timer(&duplex_timer);
	np->mii_if.full_duplex = full_duplex;
1019
	spin_unlock(&np->transceiver_lock);
L
Linus Torvalds 已提交
1020
}
1021 1022 1023 1024 1025 1026 1027
#if defined(CONFIG_ETRAX_NO_PHY)
static void
dummy_check_duplex(struct net_device* dev)
{
	full_duplex = 1;
}
#else
L
Linus Torvalds 已提交
1028 1029 1030 1031
static void
generic_check_duplex(struct net_device* dev)
{
	unsigned long data;
1032 1033 1034
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	if ((data & ADVERTISE_10FULL) ||
	    (data & ADVERTISE_100FULL))
		full_duplex = 1;
	else
		full_duplex = 0;
}

static void
tdk_check_duplex(struct net_device* dev)
{
	unsigned long data;
1046 1047 1048 1049
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_TDK_DIAGNOSTIC_REG);
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055 1056
	full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
}

static void
broadcom_check_duplex(struct net_device* dev)
{
	unsigned long data;
1057 1058 1059 1060
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_AUX_CTRL_STATUS_REG);
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067
	full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
}

static void
intel_check_duplex(struct net_device* dev)
{
	unsigned long data;
1068 1069 1070 1071
	struct net_local *np = netdev_priv(dev);

	data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
				 MDIO_INT_STATUS_REG_2);
L
Linus Torvalds 已提交
1072 1073
	full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
}
1074
#endif
L
Linus Torvalds 已提交
1075 1076 1077
static void
e100_set_duplex(struct net_device* dev, enum duplex new_duplex)
{
1078 1079 1080
	struct net_local *np = netdev_priv(dev);

	spin_lock(&np->transceiver_lock);
L
Linus Torvalds 已提交
1081 1082 1083 1084
	if (new_duplex != current_duplex) {
		current_duplex = new_duplex;
		e100_negotiate(dev);
	}
1085
	spin_unlock(&np->transceiver_lock);
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090
}

static int
e100_probe_transceiver(struct net_device* dev)
{
1091 1092
	int ret = 0;

1093
#if !defined(CONFIG_ETRAX_NO_PHY)
L
Linus Torvalds 已提交
1094 1095 1096 1097
	unsigned int phyid_high;
	unsigned int phyid_low;
	unsigned int oui;
	struct transceiver_ops* ops = NULL;
1098 1099 1100
	struct net_local *np = netdev_priv(dev);

	spin_lock(&np->transceiver_lock);
L
Linus Torvalds 已提交
1101 1102

	/* Probe MDIO physical address */
1103 1104 1105 1106
	for (np->mii_if.phy_id = 0; np->mii_if.phy_id <= 31;
	     np->mii_if.phy_id++) {
		if (e100_get_mdio_reg(dev,
				      np->mii_if.phy_id, MII_BMSR) != 0xffff)
L
Linus Torvalds 已提交
1107 1108
			break;
	}
1109 1110 1111 1112
	if (np->mii_if.phy_id == 32) {
		ret = -ENODEV;
		goto out;
	}
L
Linus Torvalds 已提交
1113 1114

	/* Get manufacturer */
1115 1116
	phyid_high = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID1);
	phyid_low = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID2);
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123
	oui = (phyid_high << 6) | (phyid_low >> 10);

	for (ops = &transceivers[0]; ops->oui; ops++) {
		if (ops->oui == oui)
			break;
	}
	transceiver = ops;
1124
out:
1125 1126
	spin_unlock(&np->transceiver_lock);
#endif
1127
	return ret;
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
}

static int
e100_get_mdio_reg(struct net_device *dev, int phy_id, int location)
{
	unsigned short cmd;    /* Data to be sent on MDIO port */
	int data;   /* Data read from MDIO */
	int bitCounter;

	/* Start of frame, OP Code, Physical Address, Register Address */
	cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (phy_id << 7) |
		(location << 2);

	e100_send_mdio_cmd(cmd, 0);

	data = 0;

	/* Data... */
	for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
		data |= (e100_receive_mdio_bit() << bitCounter);
	}

	return data;
}

static void
e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value)
{
	int bitCounter;
	unsigned short cmd;

	cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (phy_id << 7) |
	      (location << 2);

	e100_send_mdio_cmd(cmd, 1);

	/* Data... */
	for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
		e100_send_mdio_bit(GET_BIT(bitCounter, value));
	}

}

static void
e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
{
	int bitCounter;
	unsigned char data = 0x2;

	/* Preamble */
	for (bitCounter = 31; bitCounter>= 0; bitCounter--)
		e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));

	for (bitCounter = 15; bitCounter >= 2; bitCounter--)
		e100_send_mdio_bit(GET_BIT(bitCounter, cmd));

	/* Turnaround */
	for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
		if (write_cmd)
			e100_send_mdio_bit(GET_BIT(bitCounter, data));
		else
			e100_receive_mdio_bit();
}

static void
e100_send_mdio_bit(unsigned char bit)
{
	*R_NETWORK_MGM_CTRL =
		IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
		IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
	udelay(1);
	*R_NETWORK_MGM_CTRL =
		IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
		IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
		IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
	udelay(1);
}

static unsigned char
e100_receive_mdio_bit()
{
	unsigned char bit;
	*R_NETWORK_MGM_CTRL = 0;
	bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
	udelay(1);
	*R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
	udelay(1);
	return bit;
}

static void
e100_reset_transceiver(struct net_device* dev)
{
1221
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1222 1223 1224 1225
	unsigned short cmd;
	unsigned short data;
	int bitCounter;

1226
	data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
L
Linus Torvalds 已提交
1227

1228
	cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (np->mii_if.phy_id << 7) | (MII_BMCR << 2);
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

	e100_send_mdio_cmd(cmd, 1);

	data |= 0x8000;

	for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
		e100_send_mdio_bit(GET_BIT(bitCounter, data));
	}
}

/* Called by upper layers if they decide it took too long to complete
 * sending a packet - we need to reset and stuff.
 */

static void
e100_tx_timeout(struct net_device *dev)
{
1246
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	unsigned long flags;

	spin_lock_irqsave(&np->lock, flags);

	printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
	       tx_done(dev) ? "IRQ problem" : "network cable problem");

	/* remember we got an error */

	np->stats.tx_errors++;

	/* reset the TX DMA in case it has hung on something */

	RESET_DMA(NETWORK_TX_DMA_NBR);
	WAIT_DMA(NETWORK_TX_DMA_NBR);

	/* Reset the transceiver. */

	e100_reset_transceiver(dev);

	/* and get rid of the packets that never got an interrupt */
1268
	while (myFirstTxDesc != myNextTxDesc) {
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		dev_kfree_skb(myFirstTxDesc->skb);
		myFirstTxDesc->skb = 0;
		myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
	}

	/* Set up transmit DMA channel so it can be restarted later */
	*R_DMA_CH0_FIRST = 0;
	*R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);

	/* tell the upper layers we're ok again */

	netif_wake_queue(dev);
	spin_unlock_irqrestore(&np->lock, flags);
}


/* This will only be invoked if the driver is _not_ in XOFF state.
 * What this means is that we need not check it, and that this
 * invariant will hold if we make sure that the netif_*_queue()
 * calls are done at the proper times.
 */

static int
e100_send_packet(struct sk_buff *skb, struct net_device *dev)
{
1294
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	unsigned char *buf = skb->data;
	unsigned long flags;

#ifdef ETHDEBUG
	printk("send packet len %d\n", length);
#endif
	spin_lock_irqsave(&np->lock, flags);  /* protect from tx_interrupt and ourself */

	myNextTxDesc->skb = skb;

	dev->trans_start = jiffies;

1307
	e100_hardware_send_packet(np, buf, skb->len);
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

	myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);

	/* Stop queue if full */
	if (myNextTxDesc == myFirstTxDesc) {
		netif_stop_queue(dev);
	}

	spin_unlock_irqrestore(&np->lock, flags);

	return 0;
}

/*
 * The typical workload of the driver:
 *   Handle the network interface interrupts.
 */

static irqreturn_t
1327
e100rxtx_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1328 1329
{
	struct net_device *dev = (struct net_device *)dev_id;
1330 1331
	struct net_local *np = netdev_priv(dev);
	unsigned long irqbits;
L
Linus Torvalds 已提交
1332

1333 1334 1335 1336 1337 1338
	/*
	 * Note that both rx and tx interrupts are blocked at this point,
	 * regardless of which got us here.
	 */

	irqbits = *R_IRQ_MASK2_RD;
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

	/* Handle received packets */
	if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
		/* acknowledge the eop interrupt */

		*R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);

		/* check if one or more complete packets were indeed received */

		while ((*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) &&
		       (myNextRxDesc != myLastRxDesc)) {
			/* Take out the buffer and give it to the OS, then
			 * allocate a new buffer to put a packet in.
			 */
			e100_rx(dev);
1354
			np->stats.rx_packets++;
L
Linus Torvalds 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
			/* restart/continue on the channel, for safety */
			*R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
			/* clear dma channel 1 eop/descr irq bits */
			*R_DMA_CH1_CLR_INTR =
				IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
				IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);

			/* now, we might have gotten another packet
			   so we have to loop back and check if so */
		}
	}

	/* Report any packets that have been sent */
1368 1369
	while (virt_to_phys(myFirstTxDesc) != *R_DMA_CH0_FIRST &&
	       (netif_queue_stopped(dev) || myFirstTxDesc != myNextTxDesc)) {
L
Linus Torvalds 已提交
1370 1371 1372 1373 1374 1375 1376 1377
		np->stats.tx_bytes += myFirstTxDesc->skb->len;
		np->stats.tx_packets++;

		/* dma is ready with the transmission of the data in tx_skb, so now
		   we can release the skb memory */
		dev_kfree_skb_irq(myFirstTxDesc->skb);
		myFirstTxDesc->skb = 0;
		myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1378 1379
                /* Wake up queue. */
		netif_wake_queue(dev);
L
Linus Torvalds 已提交
1380 1381 1382
	}

	if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1383
		/* acknowledge the eop interrupt. */
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388 1389 1390
		*R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
	}

	return IRQ_HANDLED;
}

static irqreturn_t
1391
e100nw_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1392 1393
{
	struct net_device *dev = (struct net_device *)dev_id;
1394
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
	unsigned long irqbits = *R_IRQ_MASK0_RD;

	/* check for underrun irq */
	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
		*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
		np->stats.tx_errors++;
		D(printk("ethernet receiver underrun!\n"));
	}

	/* check for overrun irq */
	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
		update_rx_stats(&np->stats); /* this will ack the irq */
		D(printk("ethernet receiver overrun!\n"));
	}
	/* check for excessive collision irq */
	if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
		*R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
		SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
		np->stats.tx_errors++;
		D(printk("ethernet excessive collisions!\n"));
	}
	return IRQ_HANDLED;
}

/* We have a good packet(s), get it/them out of the buffers. */
static void
e100_rx(struct net_device *dev)
{
	struct sk_buff *skb;
	int length = 0;
1428
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1429 1430 1431 1432
	unsigned char *skb_data_ptr;
#ifdef ETHDEBUG
	int i;
#endif
1433 1434
	etrax_eth_descr *prevRxDesc;  /* The descriptor right before myNextRxDesc */
	spin_lock(&np->led_lock);
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443
	if (!led_active && time_after(jiffies, led_next_time)) {
		/* light the network leds depending on the current speed. */
		e100_set_network_leds(NETWORK_ACTIVITY);

		/* Set the earliest time we may clear the LED */
		led_next_time = jiffies + NET_FLASH_TIME;
		led_active = 1;
		mod_timer(&clear_led_timer, jiffies + HZ/10);
	}
1444
	spin_unlock(&np->led_lock);
L
Linus Torvalds 已提交
1445 1446

	length = myNextRxDesc->descr.hw_len - 4;
1447
	np->stats.rx_bytes += length;
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

#ifdef ETHDEBUG
	printk("Got a packet of length %d:\n", length);
	/* dump the first bytes in the packet */
	skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
	for (i = 0; i < 8; i++) {
		printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
		       skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
		       skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
		skb_data_ptr += 8;
	}
#endif

	if (length < RX_COPYBREAK) {
		/* Small packet, copy data */
		skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
		if (!skb) {
			np->stats.rx_errors++;
			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1467
			goto update_nextrxdesc;
L
Linus Torvalds 已提交
1468 1469 1470 1471 1472 1473 1474
		}

		skb_put(skb, length - ETHER_HEAD_LEN);        /* allocate room for the packet body */
		skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */

#ifdef ETHDEBUG
		printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
1475 1476
		       skb->head, skb->data, skb_tail_pointer(skb),
		       skb_end_pointer(skb));
L
Linus Torvalds 已提交
1477 1478 1479 1480 1481 1482 1483 1484
		printk("copying packet to 0x%x.\n", skb_data_ptr);
#endif

		memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
	}
	else {
		/* Large packet, send directly to upper layers and allocate new
		 * memory (aligned to cache line boundary to avoid bug).
1485 1486
		 * Before sending the skb to upper layers we must make sure
		 * that skb->data points to the aligned start of the packet.
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492
		 */
		int align;
		struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
		if (!new_skb) {
			np->stats.rx_errors++;
			printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1493
			goto update_nextrxdesc;
L
Linus Torvalds 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
		}
		skb = myNextRxDesc->skb;
		align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
		skb_put(skb, length + align);
		skb_pull(skb, align); /* Remove alignment bytes */
		myNextRxDesc->skb = new_skb;
		myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
	}

	skb->protocol = eth_type_trans(skb, dev);

	/* Send the packet to the upper layers */
	netif_rx(skb);

1508
  update_nextrxdesc:
L
Linus Torvalds 已提交
1509 1510
	/* Prepare for next packet */
	myNextRxDesc->descr.status = 0;
1511
	prevRxDesc = myNextRxDesc;
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518
	myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);

	rx_queue_len++;

	/* Check if descriptors should be returned */
	if (rx_queue_len == RX_QUEUE_THRESHOLD) {
		flush_etrax_cache();
1519
		prevRxDesc->descr.ctrl |= d_eol;
L
Linus Torvalds 已提交
1520
		myLastRxDesc->descr.ctrl &= ~d_eol;
1521
		myLastRxDesc = prevRxDesc;
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528 1529
		rx_queue_len = 0;
	}
}

/* The inverse routine to net_open(). */
static int
e100_close(struct net_device *dev)
{
1530
	struct net_local *np = netdev_priv(dev);
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

	printk(KERN_INFO "Closing %s.\n", dev->name);

	netif_stop_queue(dev);

	*R_IRQ_MASK0_CLR =
		IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
		IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
		IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);

	*R_IRQ_MASK2_CLR =
		IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
		IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);

	/* Stop the receiver and the transmitter */

	RESET_DMA(NETWORK_TX_DMA_NBR);
	RESET_DMA(NETWORK_RX_DMA_NBR);

	/* Flush the Tx and disable Rx here. */

	free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
	free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
	free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);

1558 1559 1560
	cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
	cris_free_dma(NETWORK_RX_DMA_NBR, cardname);

L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	/* Update the statistics here. */

	update_rx_stats(&np->stats);
	update_tx_stats(&np->stats);

	/* Stop speed/duplex timers */
	del_timer(&speed_timer);
	del_timer(&duplex_timer);

	return 0;
}

static int
e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	struct mii_ioctl_data *data = if_mii(ifr);
	struct net_local *np = netdev_priv(dev);
1578 1579
	int rc = 0;
        int old_autoneg;
L
Linus Torvalds 已提交
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590

	spin_lock(&np->lock); /* Preempt protection */
	switch (cmd) {
		/* The ioctls below should be considered obsolete but are */
		/* still present for compatability with old scripts/apps  */
		case SET_ETH_SPEED_10:                  /* 10 Mbps */
			e100_set_speed(dev, 10);
			break;
		case SET_ETH_SPEED_100:                /* 100 Mbps */
			e100_set_speed(dev, 100);
			break;
1591
		case SET_ETH_SPEED_AUTO:        /* Auto-negotiate speed */
L
Linus Torvalds 已提交
1592 1593
			e100_set_speed(dev, 0);
			break;
1594
		case SET_ETH_DUPLEX_HALF:       /* Half duplex */
L
Linus Torvalds 已提交
1595 1596
			e100_set_duplex(dev, half);
			break;
1597
		case SET_ETH_DUPLEX_FULL:       /* Full duplex */
L
Linus Torvalds 已提交
1598 1599
			e100_set_duplex(dev, full);
			break;
1600
		case SET_ETH_DUPLEX_AUTO:       /* Auto-negotiate duplex */
L
Linus Torvalds 已提交
1601 1602
			e100_set_duplex(dev, autoneg);
			break;
1603 1604 1605 1606 1607 1608
	        case SET_ETH_AUTONEG:
			old_autoneg = autoneg_normal;
		        autoneg_normal = *(int*)data;
			if (autoneg_normal != old_autoneg)
				e100_negotiate(dev);
			break;
L
Linus Torvalds 已提交
1609
		default:
1610 1611 1612
			rc = generic_mii_ioctl(&np->mii_if, if_mii(ifr),
						cmd, NULL);
			break;
L
Linus Torvalds 已提交
1613 1614
	}
	spin_unlock(&np->lock);
1615
	return rc;
L
Linus Torvalds 已提交
1616 1617
}

1618 1619
static int e100_get_settings(struct net_device *dev,
			     struct ethtool_cmd *cmd)
L
Linus Torvalds 已提交
1620
{
1621 1622
	struct net_local *np = netdev_priv(dev);
	int err;
1623

1624 1625 1626
	spin_lock_irq(&np->lock);
	err = mii_ethtool_gset(&np->mii_if, cmd);
	spin_unlock_irq(&np->lock);
1627

1628 1629 1630 1631
	/* The PHY may support 1000baseT, but the Etrax100 does not.  */
	cmd->supported &= ~(SUPPORTED_1000baseT_Half
			    | SUPPORTED_1000baseT_Full);
	return err;
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
}

static int e100_set_settings(struct net_device *dev,
			     struct ethtool_cmd *ecmd)
{
	if (ecmd->autoneg == AUTONEG_ENABLE) {
		e100_set_duplex(dev, autoneg);
		e100_set_speed(dev, 0);
	} else {
		e100_set_duplex(dev, ecmd->duplex == DUPLEX_HALF ? half : full);
		e100_set_speed(dev, ecmd->speed == SPEED_10 ? 10: 100);
L
Linus Torvalds 已提交
1643
	}
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

	return 0;
}

static void e100_get_drvinfo(struct net_device *dev,
			     struct ethtool_drvinfo *info)
{
	strncpy(info->driver, "ETRAX 100LX", sizeof(info->driver) - 1);
	strncpy(info->version, "$Revision: 1.31 $", sizeof(info->version) - 1);
	strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
	strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
}

static int e100_nway_reset(struct net_device *dev)
{
	if (current_duplex == autoneg && current_speed_selection == 0)
		e100_negotiate(dev);
L
Linus Torvalds 已提交
1661 1662 1663
	return 0;
}

1664
static const struct ethtool_ops e100_ethtool_ops = {
1665 1666 1667 1668 1669 1670 1671
	.get_settings	= e100_get_settings,
	.set_settings	= e100_set_settings,
	.get_drvinfo	= e100_get_drvinfo,
	.nway_reset	= e100_nway_reset,
	.get_link	= ethtool_op_get_link,
};

L
Linus Torvalds 已提交
1672 1673 1674
static int
e100_set_config(struct net_device *dev, struct ifmap *map)
{
1675 1676
	struct net_local *np = netdev_priv(dev);

L
Linus Torvalds 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	spin_lock(&np->lock); /* Preempt protection */

	switch(map->port) {
		case IF_PORT_UNKNOWN:
			/* Use autoneg */
			e100_set_speed(dev, 0);
			e100_set_duplex(dev, autoneg);
			break;
		case IF_PORT_10BASET:
			e100_set_speed(dev, 10);
			e100_set_duplex(dev, autoneg);
			break;
		case IF_PORT_100BASET:
		case IF_PORT_100BASETX:
			e100_set_speed(dev, 100);
			e100_set_duplex(dev, autoneg);
			break;
		case IF_PORT_100BASEFX:
		case IF_PORT_10BASE2:
		case IF_PORT_AUI:
			spin_unlock(&np->lock);
			return -EOPNOTSUPP;
			break;
		default:
			printk(KERN_ERR "%s: Invalid media selected", dev->name);
			spin_unlock(&np->lock);
			return -EINVAL;
	}
	spin_unlock(&np->lock);
	return 0;
}

static void
update_rx_stats(struct net_device_stats *es)
{
	unsigned long r = *R_REC_COUNTERS;
	/* update stats relevant to reception errors */
	es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
	es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
	es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
	es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
}

static void
update_tx_stats(struct net_device_stats *es)
{
	unsigned long r = *R_TR_COUNTERS;
	/* update stats relevant to transmission errors */
	es->collisions +=
		IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
		IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
}

/*
 * Get the current statistics.
 * This may be called with the card open or closed.
 */
static struct net_device_stats *
e100_get_stats(struct net_device *dev)
{
1737
	struct net_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
1738
	unsigned long flags;
1739

L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	spin_lock_irqsave(&lp->lock, flags);

	update_rx_stats(&lp->stats);
	update_tx_stats(&lp->stats);

	spin_unlock_irqrestore(&lp->lock, flags);
	return &lp->stats;
}

/*
 * Set or clear the multicast filter for this adaptor.
 * num_addrs == -1	Promiscuous mode, receive all packets
 * num_addrs == 0	Normal mode, clear multicast list
 * num_addrs > 0	Multicast mode, receive normal and MC packets,
 *			and do best-effort filtering.
 */
static void
set_multicast_list(struct net_device *dev)
{
1759
	struct net_local *lp = netdev_priv(dev);
L
Linus Torvalds 已提交
1760 1761 1762
	int num_addr = dev->mc_count;
	unsigned long int lo_bits;
	unsigned long int hi_bits;
1763

L
Linus Torvalds 已提交
1764
	spin_lock(&lp->lock);
1765
	if (dev->flags & IFF_PROMISC) {
L
Linus Torvalds 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
		/* promiscuous mode */
		lo_bits = 0xfffffffful;
		hi_bits = 0xfffffffful;

		/* Enable individual receive */
		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
	} else if (dev->flags & IFF_ALLMULTI) {
		/* enable all multicasts */
		lo_bits = 0xfffffffful;
		hi_bits = 0xfffffffful;

		/* Disable individual receive */
		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
		*R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
	} else if (num_addr == 0) {
		/* Normal, clear the mc list */
		lo_bits = 0x00000000ul;
		hi_bits = 0x00000000ul;

		/* Disable individual receive */
		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
		*R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
	} else {
		/* MC mode, receive normal and MC packets */
		char hash_ix;
		struct dev_mc_list *dmi = dev->mc_list;
		int i;
		char *baddr;
1795

L
Linus Torvalds 已提交
1796 1797
		lo_bits = 0x00000000ul;
		hi_bits = 0x00000000ul;
1798
		for (i = 0; i < num_addr; i++) {
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
			/* Calculate the hash index for the GA registers */

			hash_ix = 0;
			baddr = dmi->dmi_addr;
			hash_ix ^= (*baddr) & 0x3f;
			hash_ix ^= ((*baddr) >> 6) & 0x03;
			++baddr;
			hash_ix ^= ((*baddr) << 2) & 0x03c;
			hash_ix ^= ((*baddr) >> 4) & 0xf;
			++baddr;
			hash_ix ^= ((*baddr) << 4) & 0x30;
			hash_ix ^= ((*baddr) >> 2) & 0x3f;
			++baddr;
			hash_ix ^= (*baddr) & 0x3f;
			hash_ix ^= ((*baddr) >> 6) & 0x03;
			++baddr;
			hash_ix ^= ((*baddr) << 2) & 0x03c;
			hash_ix ^= ((*baddr) >> 4) & 0xf;
			++baddr;
			hash_ix ^= ((*baddr) << 4) & 0x30;
			hash_ix ^= ((*baddr) >> 2) & 0x3f;

			hash_ix &= 0x3f;

			if (hash_ix >= 32) {
				hi_bits |= (1 << (hash_ix-32));
1825
			} else {
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
				lo_bits |= (1 << hash_ix);
			}
			dmi = dmi->next;
		}
		/* Disable individual receive */
		SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
		*R_NETWORK_REC_CONFIG = network_rec_config_shadow;
	}
	*R_NETWORK_GA_0 = lo_bits;
	*R_NETWORK_GA_1 = hi_bits;
	spin_unlock(&lp->lock);
}

void
1840
e100_hardware_send_packet(struct net_local *np, char *buf, int length)
L
Linus Torvalds 已提交
1841 1842 1843
{
	D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));

1844
	spin_lock(&np->led_lock);
L
Linus Torvalds 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853
	if (!led_active && time_after(jiffies, led_next_time)) {
		/* light the network leds depending on the current speed. */
		e100_set_network_leds(NETWORK_ACTIVITY);

		/* Set the earliest time we may clear the LED */
		led_next_time = jiffies + NET_FLASH_TIME;
		led_active = 1;
		mod_timer(&clear_led_timer, jiffies + HZ/10);
	}
1854
	spin_unlock(&np->led_lock);
L
Linus Torvalds 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871

	/* configure the tx dma descriptor */
	myNextTxDesc->descr.sw_len = length;
	myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
	myNextTxDesc->descr.buf = virt_to_phys(buf);

        /* Move end of list */
        myLastTxDesc->descr.ctrl &= ~d_eol;
        myLastTxDesc = myNextTxDesc;

	/* Restart DMA channel */
	*R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
}

static void
e100_clear_network_leds(unsigned long dummy)
{
1872 1873 1874 1875 1876
	struct net_device *dev = (struct net_device *)dummy;
	struct net_local *np = netdev_priv(dev);

	spin_lock(&np->led_lock);

L
Linus Torvalds 已提交
1877 1878 1879 1880 1881 1882 1883
	if (led_active && time_after(jiffies, led_next_time)) {
		e100_set_network_leds(NO_NETWORK_ACTIVITY);

		/* Set the earliest time we may set the LED */
		led_next_time = jiffies + NET_FLASH_PAUSE;
		led_active = 0;
	}
1884 1885

	spin_unlock(&np->led_lock);
L
Linus Torvalds 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
}

static void
e100_set_network_leds(int active)
{
#if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
	int light_leds = (active == NO_NETWORK_ACTIVITY);
#elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
	int light_leds = (active == NETWORK_ACTIVITY);
#else
#error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
#endif

	if (!current_speed) {
		/* Make LED red, link is down */
#if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
		LED_NETWORK_SET(LED_RED);
#else
		LED_NETWORK_SET(LED_OFF);
#endif
1906
	} else if (light_leds) {
L
Linus Torvalds 已提交
1907 1908 1909 1910 1911
		if (current_speed == 10) {
			LED_NETWORK_SET(LED_ORANGE);
		} else {
			LED_NETWORK_SET(LED_GREEN);
		}
1912
	} else {
L
Linus Torvalds 已提交
1913 1914 1915 1916
		LED_NETWORK_SET(LED_OFF);
	}
}

1917 1918 1919 1920 1921 1922 1923 1924
#ifdef CONFIG_NET_POLL_CONTROLLER
static void
e100_netpoll(struct net_device* netdev)
{
	e100rxtx_interrupt(NETWORK_DMA_TX_IRQ_NBR, netdev, NULL);
}
#endif

L
Linus Torvalds 已提交
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
static int
etrax_init_module(void)
{
	return etrax_ethernet_init();
}

static int __init
e100_boot_setup(char* str)
{
	struct sockaddr sa = {0};
	int i;

	/* Parse the colon separated Ethernet station address */
	for (i = 0; i <  ETH_ALEN; i++) {
		unsigned int tmp;
		if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
			printk(KERN_WARNING "Malformed station address");
			return 0;
		}
		sa.sa_data[i] = (char)tmp;
	}

	default_mac = sa;
	return 1;
}

__setup("etrax100_eth=", e100_boot_setup);

module_init(etrax_init_module);