blob: 917b7b46f1a72d409d7babfb3fa0ca369bb83da2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: ethernet.c,v 1.31 2004/10/18 14:49:03 starvik Exp $
2 *
3 * e100net.c: A network driver for the ETRAX 100LX network controller.
4 *
5 * Copyright (c) 1998-2002 Axis Communications AB.
6 *
7 * The outline of this driver comes from skeleton.c.
8 *
9 * $Log: ethernet.c,v $
10 * Revision 1.31 2004/10/18 14:49:03 starvik
11 * Use RX interrupt as random source
12 *
13 * Revision 1.30 2004/09/29 10:44:04 starvik
14 * Enabed MAC-address output again
15 *
16 * Revision 1.29 2004/08/24 07:14:05 starvik
17 * Make use of generic MDIO interface and constants.
18 *
19 * Revision 1.28 2004/08/20 09:37:11 starvik
20 * Added support for Intel LXT972A. Creds to Randy Scarborough.
21 *
22 * Revision 1.27 2004/08/16 12:37:22 starvik
23 * Merge of Linux 2.6.8
24 *
25 * Revision 1.25 2004/06/21 10:29:57 starvik
26 * Merge of Linux 2.6.7
27 *
28 * Revision 1.23 2004/06/09 05:29:22 starvik
29 * Avoid any race where R_DMA_CH1_FIRST is NULL (may trigger cache bug).
30 *
31 * Revision 1.22 2004/05/14 07:58:03 starvik
32 * Merge of changes from 2.4
33 *
34 * Revision 1.20 2004/03/11 11:38:40 starvik
35 * Merge of Linux 2.6.4
36 *
37 * Revision 1.18 2003/12/03 13:45:46 starvik
38 * Use hardware pad for short packets to prevent information leakage.
39 *
40 * Revision 1.17 2003/07/04 08:27:37 starvik
41 * Merge of Linux 2.5.74
42 *
43 * Revision 1.16 2003/04/24 08:28:22 starvik
44 * New LED behaviour: LED off when no link
45 *
46 * Revision 1.15 2003/04/09 05:20:47 starvik
47 * Merge of Linux 2.5.67
48 *
49 * Revision 1.13 2003/03/06 16:11:01 henriken
50 * Off by one error in group address register setting.
51 *
52 * Revision 1.12 2003/02/27 17:24:19 starvik
53 * Corrected Rev to Revision
54 *
55 * Revision 1.11 2003/01/24 09:53:21 starvik
56 * Oops. Initialize GA to 0, not to 1
57 *
58 * Revision 1.10 2003/01/24 09:50:55 starvik
59 * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
60 *
61 * Revision 1.9 2002/12/13 07:40:58 starvik
62 * Added basic ethtool interface
63 * Handled out of memory when allocating new buffers
64 *
65 * Revision 1.8 2002/12/11 13:13:57 starvik
66 * Added arch/ to v10 specific includes
67 * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
68 *
69 * Revision 1.7 2002/11/26 09:41:42 starvik
70 * Added e100_set_config (standard interface to set media type)
71 * Added protection against preemptive scheduling
72 * Added standard MII ioctls
73 *
74 * Revision 1.6 2002/11/21 07:18:18 starvik
75 * Timers must be initialized in 2.5.48
76 *
77 * Revision 1.5 2002/11/20 11:56:11 starvik
78 * Merge of Linux 2.5.48
79 *
80 * Revision 1.4 2002/11/18 07:26:46 starvik
81 * Linux 2.5 port of latest Linux 2.4 ethernet driver
82 *
83 * Revision 1.33 2002/10/02 20:16:17 hp
84 * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
85 *
86 * Revision 1.32 2002/09/16 06:05:58 starvik
87 * Align memory returned by dev_alloc_skb
88 * Moved handling of sent packets to interrupt to avoid reference counting problem
89 *
90 * Revision 1.31 2002/09/10 13:28:23 larsv
91 * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
92 * for supported functionality by issuing special ioctls, i.e. wireless
93 * extensions.
94 *
95 * Revision 1.30 2002/05/07 18:50:08 johana
96 * Correct spelling in comments.
97 *
98 * Revision 1.29 2002/05/06 05:38:49 starvik
99 * Performance improvements:
100 * Large packets are not copied (breakpoint set to 256 bytes)
101 * The cache bug workaround is delayed until half of the receive list
102 * has been used
103 * Added transmit list
104 * Transmit interrupts are only enabled when transmit queue is full
105 *
106 * Revision 1.28.2.1 2002/04/30 08:15:51 starvik
107 * Performance improvements:
108 * Large packets are not copied (breakpoint set to 256 bytes)
109 * The cache bug workaround is delayed until half of the receive list
110 * has been used.
111 * Added transmit list
112 * Transmit interrupts are only enabled when transmit queue is full
113 *
114 * Revision 1.28 2002/04/22 11:47:21 johana
115 * Fix according to 2.4.19-pre7. time_after/time_before and
116 * missing end of comment.
117 * The patch has a typo for ethernet.c in e100_clear_network_leds(),
118 * that is fixed here.
119 *
120 * Revision 1.27 2002/04/12 11:55:11 bjornw
121 * Added TODO
122 *
123 * Revision 1.26 2002/03/15 17:11:02 bjornw
124 * Use prepare_rx_descriptor after the CPU has touched the receiving descs
125 *
126 * Revision 1.25 2002/03/08 13:07:53 bjornw
127 * Unnecessary spinlock removed
128 *
129 * Revision 1.24 2002/02/20 12:57:43 fredriks
130 * Replaced MIN() with min().
131 *
132 * Revision 1.23 2002/02/20 10:58:14 fredriks
133 * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
134 *
135 * Revision 1.22 2002/01/30 07:48:22 matsfg
136 * Initiate R_NETWORK_TR_CTRL
137 *
138 * Revision 1.21 2001/11/23 11:54:49 starvik
139 * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
140 * Removed compiler warnings
141 *
142 * Revision 1.20 2001/11/12 19:26:00 pkj
143 * * Corrected e100_negotiate() to not assign half to current_duplex when
144 * it was supposed to compare them...
145 * * Cleaned up failure handling in e100_open().
146 * * Fixed compiler warnings.
147 *
148 * Revision 1.19 2001/11/09 07:43:09 starvik
149 * Added full duplex support
150 * Added ioctl to set speed and duplex
151 * Clear LED timer only runs when LED is lit
152 *
153 * Revision 1.18 2001/10/03 14:40:43 jonashg
154 * Update rx_bytes counter.
155 *
156 * Revision 1.17 2001/06/11 12:43:46 olof
157 * Modified defines for network LED behavior
158 *
159 * Revision 1.16 2001/05/30 06:12:46 markusl
160 * TxDesc.next should not be set to NULL
161 *
162 * Revision 1.15 2001/05/29 10:27:04 markusl
163 * Updated after review remarks:
164 * +Use IO_EXTRACT
165 * +Handle underrun
166 *
167 * Revision 1.14 2001/05/29 09:20:14 jonashg
168 * Use driver name on printk output so one can tell which driver that complains.
169 *
170 * Revision 1.13 2001/05/09 12:35:59 johana
171 * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
172 *
173 * Revision 1.12 2001/04/05 11:43:11 tobiasa
174 * Check dev before panic.
175 *
176 * Revision 1.11 2001/04/04 11:21:05 markusl
177 * Updated according to review remarks
178 *
179 * Revision 1.10 2001/03/26 16:03:06 bjornw
180 * Needs linux/config.h
181 *
182 * Revision 1.9 2001/03/19 14:47:48 pkj
183 * * Make sure there is always a pause after the network LEDs are
184 * changed so they will not look constantly lit during heavy traffic.
185 * * Always use HZ when setting times relative to jiffies.
186 * * Use LED_NETWORK_SET() when setting the network LEDs.
187 *
188 * Revision 1.8 2001/02/27 13:52:48 bjornw
189 * malloc.h -> slab.h
190 *
191 * Revision 1.7 2001/02/23 13:46:38 bjornw
192 * Spellling check
193 *
194 * Revision 1.6 2001/01/26 15:21:04 starvik
195 * Don't disable interrupts while reading MDIO registers (MDIO is slow)
196 * Corrected promiscuous mode
197 * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
198 *
199 * Revision 1.5 2000/11/29 17:22:22 bjornw
200 * Get rid of the udword types legacy stuff
201 *
202 * Revision 1.4 2000/11/22 16:36:09 bjornw
203 * Please marketing by using the correct case when spelling Etrax.
204 *
205 * Revision 1.3 2000/11/21 16:43:04 bjornw
206 * Minor short->int change
207 *
208 * Revision 1.2 2000/11/08 14:27:57 bjornw
209 * 2.4 port
210 *
211 * Revision 1.1 2000/11/06 13:56:00 bjornw
212 * Verbatim copy of the 1.24 version of e100net.c from elinux
213 *
214 * Revision 1.24 2000/10/04 15:55:23 bjornw
215 * * Use virt_to_phys etc. for DMA addresses
216 * * Removed bogus CHECKSUM_UNNECESSARY
217 *
218 *
219 */
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222#include <linux/module.h>
223
224#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#include <linux/delay.h>
226#include <linux/types.h>
227#include <linux/fcntl.h>
228#include <linux/interrupt.h>
229#include <linux/ptrace.h>
230#include <linux/ioport.h>
231#include <linux/in.h>
232#include <linux/slab.h>
233#include <linux/string.h>
234#include <linux/spinlock.h>
235#include <linux/errno.h>
236#include <linux/init.h>
Jiri Slaby1977f032007-10-18 23:40:25 -0700237#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239#include <linux/if.h>
240#include <linux/mii.h>
241#include <linux/netdevice.h>
242#include <linux/etherdevice.h>
243#include <linux/skbuff.h>
244#include <linux/ethtool.h>
245
246#include <asm/arch/svinto.h>/* DMA and register descriptions */
247#include <asm/io.h> /* LED_* I/O functions */
248#include <asm/irq.h>
249#include <asm/dma.h>
250#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#include <asm/ethernet.h>
252#include <asm/cache.h>
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800253#include <asm/arch/io_interface_mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255//#define ETHDEBUG
256#define D(x)
257
258/*
259 * The name of the card. Is used for messages and in the requests for
260 * io regions, irqs and dma channels
261 */
262
263static const char* cardname = "ETRAX 100LX built-in ethernet controller";
264
265/* A default ethernet address. Highlevel SW will set the real one later */
266
267static struct sockaddr default_mac = {
268 0,
269 { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
270};
271
272/* Information that need to be kept for each board. */
273struct net_local {
274 struct net_device_stats stats;
275 struct mii_if_info mii_if;
276
277 /* Tx control lock. This protects the transmit buffer ring
278 * state along with the "tx full" state of the driver. This
279 * means all netif_queue flow control actions are protected
280 * by this lock as well.
281 */
282 spinlock_t lock;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800283
284 spinlock_t led_lock; /* Protect LED state */
285 spinlock_t transceiver_lock; /* Protect transceiver state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
288typedef struct etrax_eth_descr
289{
290 etrax_dma_descr descr;
291 struct sk_buff* skb;
292} etrax_eth_descr;
293
294/* Some transceivers requires special handling */
295struct transceiver_ops
296{
297 unsigned int oui;
298 void (*check_speed)(struct net_device* dev);
299 void (*check_duplex)(struct net_device* dev);
300};
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* Duplex settings */
303enum duplex
304{
305 half,
306 full,
307 autoneg
308};
309
310/* Dma descriptors etc. */
311
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800312#define MAX_MEDIA_DATA_SIZE 1522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314#define MIN_PACKET_LEN 46
315#define ETHER_HEAD_LEN 14
316
317/*
318** MDIO constants.
319*/
320#define MDIO_START 0x1
321#define MDIO_READ 0x2
322#define MDIO_WRITE 0x1
323#define MDIO_PREAMBLE 0xfffffffful
324
325/* Broadcom specific */
326#define MDIO_AUX_CTRL_STATUS_REG 0x18
327#define MDIO_BC_FULL_DUPLEX_IND 0x1
328#define MDIO_BC_SPEED 0x2
329
330/* TDK specific */
331#define MDIO_TDK_DIAGNOSTIC_REG 18
332#define MDIO_TDK_DIAGNOSTIC_RATE 0x400
333#define MDIO_TDK_DIAGNOSTIC_DPLX 0x800
334
335/*Intel LXT972A specific*/
336#define MDIO_INT_STATUS_REG_2 0x0011
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800337#define MDIO_INT_FULL_DUPLEX_IND (1 << 9)
338#define MDIO_INT_SPEED (1 << 14)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/* Network flash constants */
341#define NET_FLASH_TIME (HZ/50) /* 20 ms */
342#define NET_FLASH_PAUSE (HZ/100) /* 10 ms */
343#define NET_LINK_UP_CHECK_INTERVAL (2*HZ) /* 2 s */
344#define NET_DUPLEX_CHECK_INTERVAL (2*HZ) /* 2 s */
345
346#define NO_NETWORK_ACTIVITY 0
347#define NETWORK_ACTIVITY 1
348
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800349#define NBR_OF_RX_DESC 32
350#define NBR_OF_TX_DESC 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352/* Large packets are sent directly to upper layers while small packets are */
353/* copied (to reduce memory waste). The following constant decides the breakpoint */
354#define RX_COPYBREAK 256
355
356/* Due to a chip bug we need to flush the cache when descriptors are returned */
357/* to the DMA. To decrease performance impact we return descriptors in chunks. */
358/* The following constant determines the number of descriptors to return. */
359#define RX_QUEUE_THRESHOLD NBR_OF_RX_DESC/2
360
361#define GET_BIT(bit,val) (((val) >> (bit)) & 0x01)
362
363/* Define some macros to access ETRAX 100 registers */
364#define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
365 IO_FIELD_(reg##_, field##_, val)
366#define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
367 IO_STATE_(reg##_, field##_, _##val)
368
369static etrax_eth_descr *myNextRxDesc; /* Points to the next descriptor to
370 to be processed */
371static etrax_eth_descr *myLastRxDesc; /* The last processed descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
374
375static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
376static etrax_eth_descr* myLastTxDesc; /* End of send queue */
377static etrax_eth_descr* myNextTxDesc; /* Next descriptor to use */
378static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
379
380static unsigned int network_rec_config_shadow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382static unsigned int network_tr_ctrl_shadow = 0;
383
384/* Network speed indication. */
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700385static DEFINE_TIMER(speed_timer, NULL, 0, 0);
386static DEFINE_TIMER(clear_led_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static int current_speed; /* Speed read from transceiver */
388static int current_speed_selection; /* Speed selected by user */
389static unsigned long led_next_time;
390static int led_active;
391static int rx_queue_len;
392
393/* Duplex */
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700394static DEFINE_TIMER(duplex_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static int full_duplex;
396static enum duplex current_duplex;
397
398/* Index to functions, as function prototypes. */
399
400static int etrax_ethernet_init(void);
401
402static int e100_open(struct net_device *dev);
403static int e100_set_mac_address(struct net_device *dev, void *addr);
404static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100405static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id);
406static irqreturn_t e100nw_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static void e100_rx(struct net_device *dev);
408static int e100_close(struct net_device *dev);
409static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static int e100_set_config(struct net_device* dev, struct ifmap* map);
411static void e100_tx_timeout(struct net_device *dev);
412static struct net_device_stats *e100_get_stats(struct net_device *dev);
413static void set_multicast_list(struct net_device *dev);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800414static void e100_hardware_send_packet(struct net_local* np, char *buf, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void update_rx_stats(struct net_device_stats *);
416static void update_tx_stats(struct net_device_stats *);
417static int e100_probe_transceiver(struct net_device* dev);
418
419static void e100_check_speed(unsigned long priv);
420static void e100_set_speed(struct net_device* dev, unsigned long speed);
421static void e100_check_duplex(unsigned long priv);
422static void e100_set_duplex(struct net_device* dev, enum duplex);
423static void e100_negotiate(struct net_device* dev);
424
425static int e100_get_mdio_reg(struct net_device *dev, int phy_id, int location);
426static void e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value);
427
428static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
429static void e100_send_mdio_bit(unsigned char bit);
430static unsigned char e100_receive_mdio_bit(void);
431static void e100_reset_transceiver(struct net_device* net);
432
433static void e100_clear_network_leds(unsigned long dummy);
434static void e100_set_network_leds(int active);
435
Jeff Garzik7282d492006-09-13 14:30:00 -0400436static const struct ethtool_ops e100_ethtool_ops;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800437#if defined(CONFIG_ETRAX_NO_PHY)
438static void dummy_check_speed(struct net_device* dev);
439static void dummy_check_duplex(struct net_device* dev);
440#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void broadcom_check_speed(struct net_device* dev);
442static void broadcom_check_duplex(struct net_device* dev);
443static void tdk_check_speed(struct net_device* dev);
444static void tdk_check_duplex(struct net_device* dev);
445static void intel_check_speed(struct net_device* dev);
446static void intel_check_duplex(struct net_device* dev);
447static void generic_check_speed(struct net_device* dev);
448static void generic_check_duplex(struct net_device* dev);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800449#endif
450#ifdef CONFIG_NET_POLL_CONTROLLER
451static void e100_netpoll(struct net_device* dev);
452#endif
453
454static int autoneg_normal = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456struct transceiver_ops transceivers[] =
457{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800458#if defined(CONFIG_ETRAX_NO_PHY)
459 {0x0000, dummy_check_speed, dummy_check_duplex} /* Dummy */
460#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 {0x1018, broadcom_check_speed, broadcom_check_duplex}, /* Broadcom */
462 {0xC039, tdk_check_speed, tdk_check_duplex}, /* TDK 2120 */
463 {0x039C, tdk_check_speed, tdk_check_duplex}, /* TDK 2120C */
464 {0x04de, intel_check_speed, intel_check_duplex}, /* Intel LXT972A*/
465 {0x0000, generic_check_speed, generic_check_duplex} /* Generic, must be last */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467};
468
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800469struct transceiver_ops* transceiver = &transceivers[0];
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#define tx_done(dev) (*R_DMA_CH0_CMD == 0)
472
473/*
474 * Check for a network adaptor of this type, and return '0' if one exists.
475 * If dev->base_addr == 0, probe all likely locations.
476 * If dev->base_addr == 1, always return failure.
477 * If dev->base_addr == 2, allocate space for the device and return success
478 * (detachable devices only).
479 */
480
481static int __init
482etrax_ethernet_init(void)
483{
484 struct net_device *dev;
485 struct net_local* np;
486 int i, err;
487
488 printk(KERN_INFO
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800489 "ETRAX 100LX 10/100MBit ethernet v2.0 (c) 1998-2007 Axis Communications AB\n");
490
491 if (cris_request_io_interface(if_eth, cardname)) {
492 printk(KERN_CRIT "etrax_ethernet_init failed to get IO interface\n");
493 return -EBUSY;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 dev = alloc_etherdev(sizeof(struct net_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!dev)
498 return -ENOMEM;
499
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800500 np = netdev_priv(dev);
501
502 /* we do our own locking */
503 dev->features |= NETIF_F_LLTX;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */
506
507 /* now setup our etrax specific stuff */
508
509 dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
510 dev->dma = NETWORK_RX_DMA_NBR;
511
512 /* fill in our handlers so the network layer can talk to us in the future */
513
514 dev->open = e100_open;
515 dev->hard_start_xmit = e100_send_packet;
516 dev->stop = e100_close;
517 dev->get_stats = e100_get_stats;
518 dev->set_multicast_list = set_multicast_list;
519 dev->set_mac_address = e100_set_mac_address;
Christoph Hellwig76f2b4d2005-11-07 06:18:57 +0100520 dev->ethtool_ops = &e100_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 dev->do_ioctl = e100_ioctl;
522 dev->set_config = e100_set_config;
523 dev->tx_timeout = e100_tx_timeout;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800524#ifdef CONFIG_NET_POLL_CONTROLLER
525 dev->poll_controller = e100_netpoll;
526#endif
527
528 spin_lock_init(&np->lock);
529 spin_lock_init(&np->led_lock);
530 spin_lock_init(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* Initialise the list of Etrax DMA-descriptors */
533
534 /* Initialise receive descriptors */
535
536 for (i = 0; i < NBR_OF_RX_DESC; i++) {
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800537 /* Allocate two extra cachelines to make sure that buffer used
538 * by DMA does not share cacheline with any other data (to
539 * avoid cache bug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
541 RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
David Rientjes92b1f902006-11-08 19:49:15 -0800542 if (!RxDescList[i].skb)
543 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 RxDescList[i].descr.ctrl = 0;
545 RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
546 RxDescList[i].descr.next = virt_to_phys(&RxDescList[i + 1]);
547 RxDescList[i].descr.buf = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
548 RxDescList[i].descr.status = 0;
549 RxDescList[i].descr.hw_len = 0;
550 prepare_rx_descriptor(&RxDescList[i].descr);
551 }
552
553 RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl = d_eol;
554 RxDescList[NBR_OF_RX_DESC - 1].descr.next = virt_to_phys(&RxDescList[0]);
555 rx_queue_len = 0;
556
557 /* Initialize transmit descriptors */
558 for (i = 0; i < NBR_OF_TX_DESC; i++) {
559 TxDescList[i].descr.ctrl = 0;
560 TxDescList[i].descr.sw_len = 0;
561 TxDescList[i].descr.next = virt_to_phys(&TxDescList[i + 1].descr);
562 TxDescList[i].descr.buf = 0;
563 TxDescList[i].descr.status = 0;
564 TxDescList[i].descr.hw_len = 0;
565 TxDescList[i].skb = 0;
566 }
567
568 TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl = d_eol;
569 TxDescList[NBR_OF_TX_DESC - 1].descr.next = virt_to_phys(&TxDescList[0].descr);
570
571 /* Initialise initial pointers */
572
573 myNextRxDesc = &RxDescList[0];
574 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 myFirstTxDesc = &TxDescList[0];
576 myNextTxDesc = &TxDescList[0];
577 myLastTxDesc = &TxDescList[NBR_OF_TX_DESC - 1];
578
579 /* Register device */
580 err = register_netdev(dev);
581 if (err) {
582 free_netdev(dev);
583 return err;
584 }
585
586 /* set the default MAC address */
587
588 e100_set_mac_address(dev, &default_mac);
589
590 /* Initialize speed indicator stuff. */
591
592 current_speed = 10;
593 current_speed_selection = 0; /* Auto */
594 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800595 speed_timer.data = (unsigned long)dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 speed_timer.function = e100_check_speed;
597
598 clear_led_timer.function = e100_clear_network_leds;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800599 clear_led_timer.data = (unsigned long)dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 full_duplex = 0;
602 current_duplex = autoneg;
603 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
604 duplex_timer.data = (unsigned long)dev;
605 duplex_timer.function = e100_check_duplex;
606
607 /* Initialize mii interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 np->mii_if.phy_id_mask = 0x1f;
609 np->mii_if.reg_num_mask = 0x1f;
610 np->mii_if.dev = dev;
611 np->mii_if.mdio_read = e100_get_mdio_reg;
612 np->mii_if.mdio_write = e100_set_mdio_reg;
613
614 /* Initialize group address registers to make sure that no */
615 /* unwanted addresses are matched */
616 *R_NETWORK_GA_0 = 0x00000000;
617 *R_NETWORK_GA_1 = 0x00000000;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800618
619 /* Initialize next time the led can flash */
620 led_next_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622}
623
624/* set MAC address of the interface. called from the core after a
625 * SIOCSIFADDR ioctl, and from the bootup above.
626 */
627
628static int
629e100_set_mac_address(struct net_device *dev, void *p)
630{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800631 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct sockaddr *addr = p;
Jesper Nilsson3eed6392007-11-14 17:01:29 -0800633 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 spin_lock(&np->lock); /* preemption protection */
636
637 /* remember it */
638
639 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
640
641 /* Write it to the hardware.
642 * Note the way the address is wrapped:
643 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
644 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
645 */
646
647 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
648 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
649 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
650 *R_NETWORK_SA_2 = 0;
651
652 /* show it in the log as well */
653
Joe Perches0795af52007-10-03 17:59:30 -0700654 printk(KERN_INFO "%s: changed MAC to %s\n",
655 dev->name, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 spin_unlock(&np->lock);
658
659 return 0;
660}
661
662/*
663 * Open/initialize the board. This is called (in the current kernel)
664 * sometime after booting when the 'ifconfig' program is run.
665 *
666 * This routine should set everything up anew at each open, even
667 * registers that "should" only need to be set once at boot, so that
668 * there is non-reboot way to recover if something goes wrong.
669 */
670
671static int
672e100_open(struct net_device *dev)
673{
674 unsigned long flags;
675
676 /* enable the MDIO output pin */
677
678 *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
679
680 *R_IRQ_MASK0_CLR =
681 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
682 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
683 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
684
685 /* clear dma0 and 1 eop and descr irq masks */
686 *R_IRQ_MASK2_CLR =
687 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
688 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
689 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
690 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
691
692 /* Reset and wait for the DMA channels */
693
694 RESET_DMA(NETWORK_TX_DMA_NBR);
695 RESET_DMA(NETWORK_RX_DMA_NBR);
696 WAIT_DMA(NETWORK_TX_DMA_NBR);
697 WAIT_DMA(NETWORK_RX_DMA_NBR);
698
699 /* Initialise the etrax network controller */
700
701 /* allocate the irq corresponding to the receiving DMA */
702
703 if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700704 IRQF_SAMPLE_RANDOM, cardname, (void *)dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto grace_exit0;
706 }
707
708 /* allocate the irq corresponding to the transmitting DMA */
709
710 if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
711 cardname, (void *)dev)) {
712 goto grace_exit1;
713 }
714
715 /* allocate the irq corresponding to the network errors etc */
716
717 if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
718 cardname, (void *)dev)) {
719 goto grace_exit2;
720 }
721
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800722 /*
723 * Always allocate the DMA channels after the IRQ,
724 * and clean up on failure.
725 */
726
727 if (cris_request_dma(NETWORK_TX_DMA_NBR,
728 cardname,
729 DMA_VERBOSE_ON_ERROR,
730 dma_eth)) {
731 goto grace_exit3;
732 }
733
734 if (cris_request_dma(NETWORK_RX_DMA_NBR,
735 cardname,
736 DMA_VERBOSE_ON_ERROR,
737 dma_eth)) {
738 goto grace_exit4;
739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* give the HW an idea of what MAC address we want */
742
743 *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
744 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
745 *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
746 *R_NETWORK_SA_2 = 0;
747
748#if 0
749 /* use promiscuous mode for testing */
750 *R_NETWORK_GA_0 = 0xffffffff;
751 *R_NETWORK_GA_1 = 0xffffffff;
752
753 *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
754#else
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800755 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, max_size, size1522);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
757 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
758 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
759 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
760#endif
761
762 *R_NETWORK_GEN_CONFIG =
763 IO_STATE(R_NETWORK_GEN_CONFIG, phy, mii_clk) |
764 IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
765
766 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
767 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, delay, none);
768 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cancel, dont);
769 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, cd, enable);
770 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, retry, enable);
771 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, pad, enable);
772 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, crc, enable);
773 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
774
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800775 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* enable the irq's for ethernet DMA */
778
779 *R_IRQ_MASK2_SET =
780 IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set) |
781 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set);
782
783 *R_IRQ_MASK0_SET =
784 IO_STATE(R_IRQ_MASK0_SET, overrun, set) |
785 IO_STATE(R_IRQ_MASK0_SET, underrun, set) |
786 IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
787
788 /* make sure the irqs are cleared */
789
790 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
791 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
792
793 /* make sure the rec and transmit error counters are cleared */
794
795 (void)*R_REC_COUNTERS; /* dummy read */
796 (void)*R_TR_COUNTERS; /* dummy read */
797
798 /* start the receiving DMA channel so we can receive packets from now on */
799
800 *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
801 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
802
803 /* Set up transmit DMA channel so it can be restarted later */
804
805 *R_DMA_CH0_FIRST = 0;
806 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800807 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800809 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Probe for transceiver */
812 if (e100_probe_transceiver(dev))
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800813 goto grace_exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Start duplex/speed timers */
816 add_timer(&speed_timer);
817 add_timer(&duplex_timer);
818
819 /* We are now ready to accept transmit requeusts from
820 * the queueing layer of the networking.
821 */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800822 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 return 0;
825
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800826grace_exit5:
827 cris_free_dma(NETWORK_RX_DMA_NBR, cardname);
828grace_exit4:
829 cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830grace_exit3:
831 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
832grace_exit2:
833 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
834grace_exit1:
835 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
836grace_exit0:
837 return -EAGAIN;
838}
839
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800840#if defined(CONFIG_ETRAX_NO_PHY)
841static void
842dummy_check_speed(struct net_device* dev)
843{
844 current_speed = 100;
845}
846#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static void
848generic_check_speed(struct net_device* dev)
849{
850 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800851 struct net_local *np = netdev_priv(dev);
852
853 data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if ((data & ADVERTISE_100FULL) ||
855 (data & ADVERTISE_100HALF))
856 current_speed = 100;
857 else
858 current_speed = 10;
859}
860
861static void
862tdk_check_speed(struct net_device* dev)
863{
864 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800865 struct net_local *np = netdev_priv(dev);
866
867 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
868 MDIO_TDK_DIAGNOSTIC_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
870}
871
872static void
873broadcom_check_speed(struct net_device* dev)
874{
875 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800876 struct net_local *np = netdev_priv(dev);
877
878 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
879 MDIO_AUX_CTRL_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
881}
882
883static void
884intel_check_speed(struct net_device* dev)
885{
886 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800887 struct net_local *np = netdev_priv(dev);
888
889 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
890 MDIO_INT_STATUS_REG_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
892}
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static void
895e100_check_speed(unsigned long priv)
896{
897 struct net_device* dev = (struct net_device*)priv;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800898 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 static int led_initiated = 0;
900 unsigned long data;
901 int old_speed = current_speed;
902
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800903 spin_lock(&np->transceiver_lock);
904
905 data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!(data & BMSR_LSTATUS)) {
907 current_speed = 0;
908 } else {
909 transceiver->check_speed(dev);
910 }
911
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800912 spin_lock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if ((old_speed != current_speed) || !led_initiated) {
914 led_initiated = 1;
915 e100_set_network_leds(NO_NETWORK_ACTIVITY);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800916 if (current_speed)
917 netif_carrier_on(dev);
918 else
919 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800921 spin_unlock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Reinitialize the timer. */
924 speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
925 add_timer(&speed_timer);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800926
927 spin_unlock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static void
931e100_negotiate(struct net_device* dev)
932{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800933 struct net_local *np = netdev_priv(dev);
934 unsigned short data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
935 MII_ADVERTISE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /* Discard old speed and duplex settings */
938 data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL |
939 ADVERTISE_10HALF | ADVERTISE_10FULL);
940
941 switch (current_speed_selection) {
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800942 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (current_duplex == full)
944 data |= ADVERTISE_10FULL;
945 else if (current_duplex == half)
946 data |= ADVERTISE_10HALF;
947 else
948 data |= ADVERTISE_10HALF | ADVERTISE_10FULL;
949 break;
950
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800951 case 100:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (current_duplex == full)
953 data |= ADVERTISE_100FULL;
954 else if (current_duplex == half)
955 data |= ADVERTISE_100HALF;
956 else
957 data |= ADVERTISE_100HALF | ADVERTISE_100FULL;
958 break;
959
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800960 case 0: /* Auto */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (current_duplex == full)
962 data |= ADVERTISE_100FULL | ADVERTISE_10FULL;
963 else if (current_duplex == half)
964 data |= ADVERTISE_100HALF | ADVERTISE_10HALF;
965 else
966 data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
967 ADVERTISE_100HALF | ADVERTISE_100FULL;
968 break;
969
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800970 default: /* assume autoneg speed and duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
972 ADVERTISE_100HALF | ADVERTISE_100FULL;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800976 e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /* Renegotiate with link partner */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800979 if (autoneg_normal) {
980 data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 data |= BMCR_ANENABLE | BMCR_ANRESTART;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800982 }
983 e100_set_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986static void
987e100_set_speed(struct net_device* dev, unsigned long speed)
988{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800989 struct net_local *np = netdev_priv(dev);
990
991 spin_lock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (speed != current_speed_selection) {
993 current_speed_selection = speed;
994 e100_negotiate(dev);
995 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -0800996 spin_unlock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void
1000e100_check_duplex(unsigned long priv)
1001{
1002 struct net_device *dev = (struct net_device *)priv;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001003 struct net_local *np = netdev_priv(dev);
1004 int old_duplex;
1005
1006 spin_lock(&np->transceiver_lock);
1007 old_duplex = full_duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 transceiver->check_duplex(dev);
1009 if (old_duplex != full_duplex) {
1010 /* Duplex changed */
1011 SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
1012 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1013 }
1014
1015 /* Reinitialize the timer. */
1016 duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
1017 add_timer(&duplex_timer);
1018 np->mii_if.full_duplex = full_duplex;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001019 spin_unlock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001021#if defined(CONFIG_ETRAX_NO_PHY)
1022static void
1023dummy_check_duplex(struct net_device* dev)
1024{
1025 full_duplex = 1;
1026}
1027#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static void
1029generic_check_duplex(struct net_device* dev)
1030{
1031 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001032 struct net_local *np = netdev_priv(dev);
1033
1034 data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_ADVERTISE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if ((data & ADVERTISE_10FULL) ||
1036 (data & ADVERTISE_100FULL))
1037 full_duplex = 1;
1038 else
1039 full_duplex = 0;
1040}
1041
1042static void
1043tdk_check_duplex(struct net_device* dev)
1044{
1045 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001046 struct net_local *np = netdev_priv(dev);
1047
1048 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
1049 MDIO_TDK_DIAGNOSTIC_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
1051}
1052
1053static void
1054broadcom_check_duplex(struct net_device* dev)
1055{
1056 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001057 struct net_local *np = netdev_priv(dev);
1058
1059 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
1060 MDIO_AUX_CTRL_STATUS_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
1062}
1063
1064static void
1065intel_check_duplex(struct net_device* dev)
1066{
1067 unsigned long data;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001068 struct net_local *np = netdev_priv(dev);
1069
1070 data = e100_get_mdio_reg(dev, np->mii_if.phy_id,
1071 MDIO_INT_STATUS_REG_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
1073}
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075static void
1076e100_set_duplex(struct net_device* dev, enum duplex new_duplex)
1077{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001078 struct net_local *np = netdev_priv(dev);
1079
1080 spin_lock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (new_duplex != current_duplex) {
1082 current_duplex = new_duplex;
1083 e100_negotiate(dev);
1084 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001085 spin_unlock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088static int
1089e100_probe_transceiver(struct net_device* dev)
1090{
Andrew Morton633edf52007-11-14 17:00:58 -08001091 int ret = 0;
1092
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001093#if !defined(CONFIG_ETRAX_NO_PHY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 unsigned int phyid_high;
1095 unsigned int phyid_low;
1096 unsigned int oui;
1097 struct transceiver_ops* ops = NULL;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001098 struct net_local *np = netdev_priv(dev);
1099
1100 spin_lock(&np->transceiver_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* Probe MDIO physical address */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001103 for (np->mii_if.phy_id = 0; np->mii_if.phy_id <= 31;
1104 np->mii_if.phy_id++) {
1105 if (e100_get_mdio_reg(dev,
1106 np->mii_if.phy_id, MII_BMSR) != 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
1108 }
Andrew Morton633edf52007-11-14 17:00:58 -08001109 if (np->mii_if.phy_id == 32) {
1110 ret = -ENODEV;
1111 goto out;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /* Get manufacturer */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001115 phyid_high = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID1);
1116 phyid_low = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_PHYSID2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 oui = (phyid_high << 6) | (phyid_low >> 10);
1118
1119 for (ops = &transceivers[0]; ops->oui; ops++) {
1120 if (ops->oui == oui)
1121 break;
1122 }
1123 transceiver = ops;
Andrew Morton633edf52007-11-14 17:00:58 -08001124out:
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001125 spin_unlock(&np->transceiver_lock);
1126#endif
Andrew Morton633edf52007-11-14 17:00:58 -08001127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static int
1131e100_get_mdio_reg(struct net_device *dev, int phy_id, int location)
1132{
1133 unsigned short cmd; /* Data to be sent on MDIO port */
1134 int data; /* Data read from MDIO */
1135 int bitCounter;
1136
1137 /* Start of frame, OP Code, Physical Address, Register Address */
1138 cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (phy_id << 7) |
1139 (location << 2);
1140
1141 e100_send_mdio_cmd(cmd, 0);
1142
1143 data = 0;
1144
1145 /* Data... */
1146 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
1147 data |= (e100_receive_mdio_bit() << bitCounter);
1148 }
1149
1150 return data;
1151}
1152
1153static void
1154e100_set_mdio_reg(struct net_device *dev, int phy_id, int location, int value)
1155{
1156 int bitCounter;
1157 unsigned short cmd;
1158
1159 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (phy_id << 7) |
1160 (location << 2);
1161
1162 e100_send_mdio_cmd(cmd, 1);
1163
1164 /* Data... */
1165 for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
1166 e100_send_mdio_bit(GET_BIT(bitCounter, value));
1167 }
1168
1169}
1170
1171static void
1172e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
1173{
1174 int bitCounter;
1175 unsigned char data = 0x2;
1176
1177 /* Preamble */
1178 for (bitCounter = 31; bitCounter>= 0; bitCounter--)
1179 e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
1180
1181 for (bitCounter = 15; bitCounter >= 2; bitCounter--)
1182 e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
1183
1184 /* Turnaround */
1185 for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
1186 if (write_cmd)
1187 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1188 else
1189 e100_receive_mdio_bit();
1190}
1191
1192static void
1193e100_send_mdio_bit(unsigned char bit)
1194{
1195 *R_NETWORK_MGM_CTRL =
1196 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1197 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1198 udelay(1);
1199 *R_NETWORK_MGM_CTRL =
1200 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
1201 IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
1202 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
1203 udelay(1);
1204}
1205
1206static unsigned char
1207e100_receive_mdio_bit()
1208{
1209 unsigned char bit;
1210 *R_NETWORK_MGM_CTRL = 0;
1211 bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
1212 udelay(1);
1213 *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
1214 udelay(1);
1215 return bit;
1216}
1217
1218static void
1219e100_reset_transceiver(struct net_device* dev)
1220{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001221 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 unsigned short cmd;
1223 unsigned short data;
1224 int bitCounter;
1225
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001226 data = e100_get_mdio_reg(dev, np->mii_if.phy_id, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001228 cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (np->mii_if.phy_id << 7) | (MII_BMCR << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 e100_send_mdio_cmd(cmd, 1);
1231
1232 data |= 0x8000;
1233
1234 for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
1235 e100_send_mdio_bit(GET_BIT(bitCounter, data));
1236 }
1237}
1238
1239/* Called by upper layers if they decide it took too long to complete
1240 * sending a packet - we need to reset and stuff.
1241 */
1242
1243static void
1244e100_tx_timeout(struct net_device *dev)
1245{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001246 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned long flags;
1248
1249 spin_lock_irqsave(&np->lock, flags);
1250
1251 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
1252 tx_done(dev) ? "IRQ problem" : "network cable problem");
1253
1254 /* remember we got an error */
1255
1256 np->stats.tx_errors++;
1257
1258 /* reset the TX DMA in case it has hung on something */
1259
1260 RESET_DMA(NETWORK_TX_DMA_NBR);
1261 WAIT_DMA(NETWORK_TX_DMA_NBR);
1262
1263 /* Reset the transceiver. */
1264
1265 e100_reset_transceiver(dev);
1266
1267 /* and get rid of the packets that never got an interrupt */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001268 while (myFirstTxDesc != myNextTxDesc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 dev_kfree_skb(myFirstTxDesc->skb);
1270 myFirstTxDesc->skb = 0;
1271 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1272 }
1273
1274 /* Set up transmit DMA channel so it can be restarted later */
1275 *R_DMA_CH0_FIRST = 0;
1276 *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
1277
1278 /* tell the upper layers we're ok again */
1279
1280 netif_wake_queue(dev);
1281 spin_unlock_irqrestore(&np->lock, flags);
1282}
1283
1284
1285/* This will only be invoked if the driver is _not_ in XOFF state.
1286 * What this means is that we need not check it, and that this
1287 * invariant will hold if we make sure that the netif_*_queue()
1288 * calls are done at the proper times.
1289 */
1290
1291static int
1292e100_send_packet(struct sk_buff *skb, struct net_device *dev)
1293{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001294 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned char *buf = skb->data;
1296 unsigned long flags;
1297
1298#ifdef ETHDEBUG
1299 printk("send packet len %d\n", length);
1300#endif
1301 spin_lock_irqsave(&np->lock, flags); /* protect from tx_interrupt and ourself */
1302
1303 myNextTxDesc->skb = skb;
1304
1305 dev->trans_start = jiffies;
1306
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001307 e100_hardware_send_packet(np, buf, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
1310
1311 /* Stop queue if full */
1312 if (myNextTxDesc == myFirstTxDesc) {
1313 netif_stop_queue(dev);
1314 }
1315
1316 spin_unlock_irqrestore(&np->lock, flags);
1317
1318 return 0;
1319}
1320
1321/*
1322 * The typical workload of the driver:
1323 * Handle the network interface interrupts.
1324 */
1325
1326static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001327e100rxtx_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct net_device *dev = (struct net_device *)dev_id;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001330 struct net_local *np = netdev_priv(dev);
1331 unsigned long irqbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001333 /*
1334 * Note that both rx and tx interrupts are blocked at this point,
1335 * regardless of which got us here.
1336 */
1337
1338 irqbits = *R_IRQ_MASK2_RD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* Handle received packets */
1341 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
1342 /* acknowledge the eop interrupt */
1343
1344 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
1345
1346 /* check if one or more complete packets were indeed received */
1347
1348 while ((*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) &&
1349 (myNextRxDesc != myLastRxDesc)) {
1350 /* Take out the buffer and give it to the OS, then
1351 * allocate a new buffer to put a packet in.
1352 */
1353 e100_rx(dev);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001354 np->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* restart/continue on the channel, for safety */
1356 *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
1357 /* clear dma channel 1 eop/descr irq bits */
1358 *R_DMA_CH1_CLR_INTR =
1359 IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
1360 IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
1361
1362 /* now, we might have gotten another packet
1363 so we have to loop back and check if so */
1364 }
1365 }
1366
1367 /* Report any packets that have been sent */
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001368 while (virt_to_phys(myFirstTxDesc) != *R_DMA_CH0_FIRST &&
1369 (netif_queue_stopped(dev) || myFirstTxDesc != myNextTxDesc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 np->stats.tx_bytes += myFirstTxDesc->skb->len;
1371 np->stats.tx_packets++;
1372
1373 /* dma is ready with the transmission of the data in tx_skb, so now
1374 we can release the skb memory */
1375 dev_kfree_skb_irq(myFirstTxDesc->skb);
1376 myFirstTxDesc->skb = 0;
1377 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001378 /* Wake up queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 netif_wake_queue(dev);
1380 }
1381
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001382 if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1383 /* acknowledge the eop interrupt. */
1384 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 return IRQ_HANDLED;
1388}
1389
1390static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001391e100nw_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct net_device *dev = (struct net_device *)dev_id;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001394 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned long irqbits = *R_IRQ_MASK0_RD;
1396
1397 /* check for underrun irq */
1398 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) {
1399 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1400 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1401 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
1402 np->stats.tx_errors++;
1403 D(printk("ethernet receiver underrun!\n"));
1404 }
1405
1406 /* check for overrun irq */
1407 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) {
1408 update_rx_stats(&np->stats); /* this will ack the irq */
1409 D(printk("ethernet receiver overrun!\n"));
1410 }
1411 /* check for excessive collision irq */
1412 if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) {
1413 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, clr);
1414 *R_NETWORK_TR_CTRL = network_tr_ctrl_shadow;
1415 SETS(network_tr_ctrl_shadow, R_NETWORK_TR_CTRL, clr_error, nop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 np->stats.tx_errors++;
1417 D(printk("ethernet excessive collisions!\n"));
1418 }
1419 return IRQ_HANDLED;
1420}
1421
1422/* We have a good packet(s), get it/them out of the buffers. */
1423static void
1424e100_rx(struct net_device *dev)
1425{
1426 struct sk_buff *skb;
1427 int length = 0;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001428 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned char *skb_data_ptr;
1430#ifdef ETHDEBUG
1431 int i;
1432#endif
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001433 etrax_eth_descr *prevRxDesc; /* The descriptor right before myNextRxDesc */
1434 spin_lock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!led_active && time_after(jiffies, led_next_time)) {
1436 /* light the network leds depending on the current speed. */
1437 e100_set_network_leds(NETWORK_ACTIVITY);
1438
1439 /* Set the earliest time we may clear the LED */
1440 led_next_time = jiffies + NET_FLASH_TIME;
1441 led_active = 1;
1442 mod_timer(&clear_led_timer, jiffies + HZ/10);
1443 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001444 spin_unlock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 length = myNextRxDesc->descr.hw_len - 4;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001447 np->stats.rx_bytes += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449#ifdef ETHDEBUG
1450 printk("Got a packet of length %d:\n", length);
1451 /* dump the first bytes in the packet */
1452 skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
1453 for (i = 0; i < 8; i++) {
1454 printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
1455 skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
1456 skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
1457 skb_data_ptr += 8;
1458 }
1459#endif
1460
1461 if (length < RX_COPYBREAK) {
1462 /* Small packet, copy data */
1463 skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
1464 if (!skb) {
1465 np->stats.rx_errors++;
1466 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001467 goto update_nextrxdesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 skb_put(skb, length - ETHER_HEAD_LEN); /* allocate room for the packet body */
1471 skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
1472
1473#ifdef ETHDEBUG
1474 printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001475 skb->head, skb->data, skb_tail_pointer(skb),
1476 skb_end_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 printk("copying packet to 0x%x.\n", skb_data_ptr);
1478#endif
1479
1480 memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
1481 }
1482 else {
1483 /* Large packet, send directly to upper layers and allocate new
1484 * memory (aligned to cache line boundary to avoid bug).
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001485 * Before sending the skb to upper layers we must make sure
1486 * that skb->data points to the aligned start of the packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
1488 int align;
1489 struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
1490 if (!new_skb) {
1491 np->stats.rx_errors++;
1492 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001493 goto update_nextrxdesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 skb = myNextRxDesc->skb;
1496 align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;
1497 skb_put(skb, length + align);
1498 skb_pull(skb, align); /* Remove alignment bytes */
1499 myNextRxDesc->skb = new_skb;
1500 myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
1501 }
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 skb->protocol = eth_type_trans(skb, dev);
1504
1505 /* Send the packet to the upper layers */
1506 netif_rx(skb);
1507
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001508 update_nextrxdesc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* Prepare for next packet */
1510 myNextRxDesc->descr.status = 0;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001511 prevRxDesc = myNextRxDesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
1513
1514 rx_queue_len++;
1515
1516 /* Check if descriptors should be returned */
1517 if (rx_queue_len == RX_QUEUE_THRESHOLD) {
1518 flush_etrax_cache();
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001519 prevRxDesc->descr.ctrl |= d_eol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 myLastRxDesc->descr.ctrl &= ~d_eol;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001521 myLastRxDesc = prevRxDesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 rx_queue_len = 0;
1523 }
1524}
1525
1526/* The inverse routine to net_open(). */
1527static int
1528e100_close(struct net_device *dev)
1529{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001530 struct net_local *np = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 printk(KERN_INFO "Closing %s.\n", dev->name);
1533
1534 netif_stop_queue(dev);
1535
1536 *R_IRQ_MASK0_CLR =
1537 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
1538 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
1539 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
1540
1541 *R_IRQ_MASK2_CLR =
1542 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
1543 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1544 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
1545 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1546
1547 /* Stop the receiver and the transmitter */
1548
1549 RESET_DMA(NETWORK_TX_DMA_NBR);
1550 RESET_DMA(NETWORK_RX_DMA_NBR);
1551
1552 /* Flush the Tx and disable Rx here. */
1553
1554 free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
1555 free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
1556 free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
1557
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001558 cris_free_dma(NETWORK_TX_DMA_NBR, cardname);
1559 cris_free_dma(NETWORK_RX_DMA_NBR, cardname);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Update the statistics here. */
1562
1563 update_rx_stats(&np->stats);
1564 update_tx_stats(&np->stats);
1565
1566 /* Stop speed/duplex timers */
1567 del_timer(&speed_timer);
1568 del_timer(&duplex_timer);
1569
1570 return 0;
1571}
1572
1573static int
1574e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1575{
1576 struct mii_ioctl_data *data = if_mii(ifr);
1577 struct net_local *np = netdev_priv(dev);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001578 int rc = 0;
1579 int old_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 spin_lock(&np->lock); /* Preempt protection */
1582 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* The ioctls below should be considered obsolete but are */
1584 /* still present for compatability with old scripts/apps */
1585 case SET_ETH_SPEED_10: /* 10 Mbps */
1586 e100_set_speed(dev, 10);
1587 break;
1588 case SET_ETH_SPEED_100: /* 100 Mbps */
1589 e100_set_speed(dev, 100);
1590 break;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001591 case SET_ETH_SPEED_AUTO: /* Auto-negotiate speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 e100_set_speed(dev, 0);
1593 break;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001594 case SET_ETH_DUPLEX_HALF: /* Half duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 e100_set_duplex(dev, half);
1596 break;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001597 case SET_ETH_DUPLEX_FULL: /* Full duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 e100_set_duplex(dev, full);
1599 break;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001600 case SET_ETH_DUPLEX_AUTO: /* Auto-negotiate duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 e100_set_duplex(dev, autoneg);
1602 break;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001603 case SET_ETH_AUTONEG:
1604 old_autoneg = autoneg_normal;
1605 autoneg_normal = *(int*)data;
1606 if (autoneg_normal != old_autoneg)
1607 e100_negotiate(dev);
1608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 default:
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001610 rc = generic_mii_ioctl(&np->mii_if, if_mii(ifr),
1611 cmd, NULL);
1612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 spin_unlock(&np->lock);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001615 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001618static int e100_get_settings(struct net_device *dev,
1619 struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001621 struct net_local *np = netdev_priv(dev);
1622 int err;
Christoph Hellwig76f2b4d2005-11-07 06:18:57 +01001623
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001624 spin_lock_irq(&np->lock);
1625 err = mii_ethtool_gset(&np->mii_if, cmd);
1626 spin_unlock_irq(&np->lock);
Christoph Hellwig76f2b4d2005-11-07 06:18:57 +01001627
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001628 /* The PHY may support 1000baseT, but the Etrax100 does not. */
1629 cmd->supported &= ~(SUPPORTED_1000baseT_Half
1630 | SUPPORTED_1000baseT_Full);
1631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Christoph Hellwig76f2b4d2005-11-07 06:18:57 +01001634static int e100_set_settings(struct net_device *dev,
1635 struct ethtool_cmd *ecmd)
1636{
1637 if (ecmd->autoneg == AUTONEG_ENABLE) {
1638 e100_set_duplex(dev, autoneg);
1639 e100_set_speed(dev, 0);
1640 } else {
1641 e100_set_duplex(dev, ecmd->duplex == DUPLEX_HALF ? half : full);
1642 e100_set_speed(dev, ecmd->speed == SPEED_10 ? 10: 100);
1643 }
1644
1645 return 0;
1646}
1647
1648static void e100_get_drvinfo(struct net_device *dev,
1649 struct ethtool_drvinfo *info)
1650{
1651 strncpy(info->driver, "ETRAX 100LX", sizeof(info->driver) - 1);
1652 strncpy(info->version, "$Revision: 1.31 $", sizeof(info->version) - 1);
1653 strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
1654 strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
1655}
1656
1657static int e100_nway_reset(struct net_device *dev)
1658{
1659 if (current_duplex == autoneg && current_speed_selection == 0)
1660 e100_negotiate(dev);
1661 return 0;
1662}
1663
Jeff Garzik7282d492006-09-13 14:30:00 -04001664static const struct ethtool_ops e100_ethtool_ops = {
Christoph Hellwig76f2b4d2005-11-07 06:18:57 +01001665 .get_settings = e100_get_settings,
1666 .set_settings = e100_set_settings,
1667 .get_drvinfo = e100_get_drvinfo,
1668 .nway_reset = e100_nway_reset,
1669 .get_link = ethtool_op_get_link,
1670};
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672static int
1673e100_set_config(struct net_device *dev, struct ifmap *map)
1674{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001675 struct net_local *np = netdev_priv(dev);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 spin_lock(&np->lock); /* Preempt protection */
1678
1679 switch(map->port) {
1680 case IF_PORT_UNKNOWN:
1681 /* Use autoneg */
1682 e100_set_speed(dev, 0);
1683 e100_set_duplex(dev, autoneg);
1684 break;
1685 case IF_PORT_10BASET:
1686 e100_set_speed(dev, 10);
1687 e100_set_duplex(dev, autoneg);
1688 break;
1689 case IF_PORT_100BASET:
1690 case IF_PORT_100BASETX:
1691 e100_set_speed(dev, 100);
1692 e100_set_duplex(dev, autoneg);
1693 break;
1694 case IF_PORT_100BASEFX:
1695 case IF_PORT_10BASE2:
1696 case IF_PORT_AUI:
1697 spin_unlock(&np->lock);
1698 return -EOPNOTSUPP;
1699 break;
1700 default:
1701 printk(KERN_ERR "%s: Invalid media selected", dev->name);
1702 spin_unlock(&np->lock);
1703 return -EINVAL;
1704 }
1705 spin_unlock(&np->lock);
1706 return 0;
1707}
1708
1709static void
1710update_rx_stats(struct net_device_stats *es)
1711{
1712 unsigned long r = *R_REC_COUNTERS;
1713 /* update stats relevant to reception errors */
1714 es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
1715 es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
1716 es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
1717 es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
1718}
1719
1720static void
1721update_tx_stats(struct net_device_stats *es)
1722{
1723 unsigned long r = *R_TR_COUNTERS;
1724 /* update stats relevant to transmission errors */
1725 es->collisions +=
1726 IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
1727 IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/*
1731 * Get the current statistics.
1732 * This may be called with the card open or closed.
1733 */
1734static struct net_device_stats *
1735e100_get_stats(struct net_device *dev)
1736{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001737 struct net_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned long flags;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 spin_lock_irqsave(&lp->lock, flags);
1741
1742 update_rx_stats(&lp->stats);
1743 update_tx_stats(&lp->stats);
1744
1745 spin_unlock_irqrestore(&lp->lock, flags);
1746 return &lp->stats;
1747}
1748
1749/*
1750 * Set or clear the multicast filter for this adaptor.
1751 * num_addrs == -1 Promiscuous mode, receive all packets
1752 * num_addrs == 0 Normal mode, clear multicast list
1753 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1754 * and do best-effort filtering.
1755 */
1756static void
1757set_multicast_list(struct net_device *dev)
1758{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001759 struct net_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int num_addr = dev->mc_count;
1761 unsigned long int lo_bits;
1762 unsigned long int hi_bits;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_lock(&lp->lock);
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001765 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* promiscuous mode */
1767 lo_bits = 0xfffffffful;
1768 hi_bits = 0xfffffffful;
1769
1770 /* Enable individual receive */
1771 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
1772 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1773 } else if (dev->flags & IFF_ALLMULTI) {
1774 /* enable all multicasts */
1775 lo_bits = 0xfffffffful;
1776 hi_bits = 0xfffffffful;
1777
1778 /* Disable individual receive */
1779 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1780 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1781 } else if (num_addr == 0) {
1782 /* Normal, clear the mc list */
1783 lo_bits = 0x00000000ul;
1784 hi_bits = 0x00000000ul;
1785
1786 /* Disable individual receive */
1787 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1788 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1789 } else {
1790 /* MC mode, receive normal and MC packets */
1791 char hash_ix;
1792 struct dev_mc_list *dmi = dev->mc_list;
1793 int i;
1794 char *baddr;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 lo_bits = 0x00000000ul;
1797 hi_bits = 0x00000000ul;
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001798 for (i = 0; i < num_addr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* Calculate the hash index for the GA registers */
1800
1801 hash_ix = 0;
1802 baddr = dmi->dmi_addr;
1803 hash_ix ^= (*baddr) & 0x3f;
1804 hash_ix ^= ((*baddr) >> 6) & 0x03;
1805 ++baddr;
1806 hash_ix ^= ((*baddr) << 2) & 0x03c;
1807 hash_ix ^= ((*baddr) >> 4) & 0xf;
1808 ++baddr;
1809 hash_ix ^= ((*baddr) << 4) & 0x30;
1810 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1811 ++baddr;
1812 hash_ix ^= (*baddr) & 0x3f;
1813 hash_ix ^= ((*baddr) >> 6) & 0x03;
1814 ++baddr;
1815 hash_ix ^= ((*baddr) << 2) & 0x03c;
1816 hash_ix ^= ((*baddr) >> 4) & 0xf;
1817 ++baddr;
1818 hash_ix ^= ((*baddr) << 4) & 0x30;
1819 hash_ix ^= ((*baddr) >> 2) & 0x3f;
1820
1821 hash_ix &= 0x3f;
1822
1823 if (hash_ix >= 32) {
1824 hi_bits |= (1 << (hash_ix-32));
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 lo_bits |= (1 << hash_ix);
1827 }
1828 dmi = dmi->next;
1829 }
1830 /* Disable individual receive */
1831 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1832 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1833 }
1834 *R_NETWORK_GA_0 = lo_bits;
1835 *R_NETWORK_GA_1 = hi_bits;
1836 spin_unlock(&lp->lock);
1837}
1838
1839void
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001840e100_hardware_send_packet(struct net_local *np, char *buf, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
1843
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001844 spin_lock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (!led_active && time_after(jiffies, led_next_time)) {
1846 /* light the network leds depending on the current speed. */
1847 e100_set_network_leds(NETWORK_ACTIVITY);
1848
1849 /* Set the earliest time we may clear the LED */
1850 led_next_time = jiffies + NET_FLASH_TIME;
1851 led_active = 1;
1852 mod_timer(&clear_led_timer, jiffies + HZ/10);
1853 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001854 spin_unlock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* configure the tx dma descriptor */
1857 myNextTxDesc->descr.sw_len = length;
1858 myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
1859 myNextTxDesc->descr.buf = virt_to_phys(buf);
1860
1861 /* Move end of list */
1862 myLastTxDesc->descr.ctrl &= ~d_eol;
1863 myLastTxDesc = myNextTxDesc;
1864
1865 /* Restart DMA channel */
1866 *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
1867}
1868
1869static void
1870e100_clear_network_leds(unsigned long dummy)
1871{
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001872 struct net_device *dev = (struct net_device *)dummy;
1873 struct net_local *np = netdev_priv(dev);
1874
1875 spin_lock(&np->led_lock);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (led_active && time_after(jiffies, led_next_time)) {
1878 e100_set_network_leds(NO_NETWORK_ACTIVITY);
1879
1880 /* Set the earliest time we may set the LED */
1881 led_next_time = jiffies + NET_FLASH_PAUSE;
1882 led_active = 0;
1883 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001884
1885 spin_unlock(&np->led_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888static void
1889e100_set_network_leds(int active)
1890{
1891#if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
1892 int light_leds = (active == NO_NETWORK_ACTIVITY);
1893#elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
1894 int light_leds = (active == NETWORK_ACTIVITY);
1895#else
1896#error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
1897#endif
1898
1899 if (!current_speed) {
1900 /* Make LED red, link is down */
1901#if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
1902 LED_NETWORK_SET(LED_RED);
1903#else
1904 LED_NETWORK_SET(LED_OFF);
1905#endif
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001906 } else if (light_leds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (current_speed == 10) {
1908 LED_NETWORK_SET(LED_ORANGE);
1909 } else {
1910 LED_NETWORK_SET(LED_GREEN);
1911 }
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001912 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 LED_NETWORK_SET(LED_OFF);
1914 }
1915}
1916
Jesper Nilssonbafef0a2007-11-14 17:00:55 -08001917#ifdef CONFIG_NET_POLL_CONTROLLER
1918static void
1919e100_netpoll(struct net_device* netdev)
1920{
1921 e100rxtx_interrupt(NETWORK_DMA_TX_IRQ_NBR, netdev, NULL);
1922}
1923#endif
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925static int
1926etrax_init_module(void)
1927{
1928 return etrax_ethernet_init();
1929}
1930
1931static int __init
1932e100_boot_setup(char* str)
1933{
1934 struct sockaddr sa = {0};
1935 int i;
1936
1937 /* Parse the colon separated Ethernet station address */
1938 for (i = 0; i < ETH_ALEN; i++) {
1939 unsigned int tmp;
1940 if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
1941 printk(KERN_WARNING "Malformed station address");
1942 return 0;
1943 }
1944 sa.sa_data[i] = (char)tmp;
1945 }
1946
1947 default_mac = sa;
1948 return 1;
1949}
1950
1951__setup("etrax100_eth=", e100_boot_setup);
1952
1953module_init(etrax_init_module);