blob: ac7fc7a53678d32a3e04d3aa74fbb9e654ece9ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenheke5371492008-06-01 02:18:13 +020057static char mv643xx_eth_driver_name[] = "mv643xx_eth";
58static char mv643xx_eth_driver_version[] = "1.0";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020059
Lennert Buytenheke5371492008-06-01 02:18:13 +020060#define MV643XX_ETH_CHECKSUM_OFFLOAD_TX
61#define MV643XX_ETH_NAPI
62#define MV643XX_ETH_TX_FAST_REFILL
63#undef MV643XX_ETH_COAL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020064
Lennert Buytenheke5371492008-06-01 02:18:13 +020065#define MV643XX_ETH_TX_COAL 100
66#ifdef MV643XX_ETH_COAL
67#define MV643XX_ETH_RX_COAL 100
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020068#endif
69
Lennert Buytenheke5371492008-06-01 02:18:13 +020070#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020071#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020099#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
101#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
102#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
103#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
104#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
105#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200106#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200107#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
108#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
109#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200110#define INT_RX 0x00000804
111#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200113#define INT_EXT_LINK 0x00100000
114#define INT_EXT_PHY 0x00010000
115#define INT_EXT_TX_ERROR_0 0x00000100
116#define INT_EXT_TX_0 0x00000001
117#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200118#define INT_MASK(p) (0x0468 + ((p) << 10))
119#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
120#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
121#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
122#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
123#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
124#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
125#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
126#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
127#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200129
130/*
131 * SDMA configuration register.
132 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200134#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200136#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200137
138#if defined(__BIG_ENDIAN)
139#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
140 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#elif defined(__LITTLE_ENDIAN)
143#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
144 RX_BURST_SIZE_4_64BIT | \
145 BLM_RX_NO_SWAP | \
146 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147 TX_BURST_SIZE_4_64BIT
148#else
149#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
150#endif
151
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200152
153/*
154 * Port serial control register.
155 */
156#define SET_MII_SPEED_TO_100 (1 << 24)
157#define SET_GMII_SPEED_TO_1000 (1 << 23)
158#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200159#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200160#define MAX_RX_PACKET_9700BYTE (5 << 17)
161#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200162#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
163#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
164#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
165#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
166#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
167#define FORCE_LINK_PASS (1 << 1)
168#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200169
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200170#define DEFAULT_RX_QUEUE_SIZE 400
171#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200174#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
175#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
176#define SMI_OPCODE_WRITE 0 /* Completion of Read */
177#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200178
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179/* typedefs */
180
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200181typedef enum _func_ret_status {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200182 ETH_OK, /* Returned as expected. */
183 ETH_ERROR, /* Fundamental error. */
184 ETH_RETRY, /* Could not process request. Try later.*/
185 ETH_END_OF_JOB, /* Ring has nothing to process. */
186 ETH_QUEUE_FULL, /* Ring resource error. */
187 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200188} FUNC_RET_STATUS;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200189
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200190/*
191 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200192 */
193#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200194struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200195 u16 byte_cnt; /* Descriptor buffer byte count */
196 u16 buf_size; /* Buffer size */
197 u32 cmd_sts; /* Descriptor command status */
198 u32 next_desc_ptr; /* Next descriptor pointer */
199 u32 buf_ptr; /* Descriptor buffer pointer */
200};
201
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200202struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200203 u16 byte_cnt; /* buffer byte count */
204 u16 l4i_chk; /* CPU provided TCP checksum */
205 u32 cmd_sts; /* Command/status field */
206 u32 next_desc_ptr; /* Pointer to next descriptor */
207 u32 buf_ptr; /* pointer to buffer for this descriptor*/
208};
209#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200210struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200211 u32 cmd_sts; /* Descriptor command status */
212 u16 buf_size; /* Buffer size */
213 u16 byte_cnt; /* Descriptor buffer byte count */
214 u32 buf_ptr; /* Descriptor buffer pointer */
215 u32 next_desc_ptr; /* Next descriptor pointer */
216};
217
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200218struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200219 u32 cmd_sts; /* Command/status field */
220 u16 l4i_chk; /* CPU provided TCP checksum */
221 u16 byte_cnt; /* buffer byte count */
222 u32 buf_ptr; /* pointer to buffer for this descriptor*/
223 u32 next_desc_ptr; /* Pointer to next descriptor */
224};
225#else
226#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
227#endif
228
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200229/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200230#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200231
232/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200233#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200234
235/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200236#define LAYER_4_CHECKSUM_OK 0x40000000
237#define RX_ENABLE_INTERRUPT 0x20000000
238#define RX_FIRST_DESC 0x08000000
239#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200240
241/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200242#define TX_ENABLE_INTERRUPT 0x00800000
243#define GEN_CRC 0x00400000
244#define TX_FIRST_DESC 0x00200000
245#define TX_LAST_DESC 0x00100000
246#define ZERO_PADDING 0x00080000
247#define GEN_IP_V4_CHECKSUM 0x00040000
248#define GEN_TCP_UDP_CHECKSUM 0x00020000
249#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200250
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200251#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200252
253
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200254/* Unified struct for Rx and Tx operations. The user is not required to */
255/* be familier with neither Tx nor Rx descriptors. */
256struct pkt_info {
257 unsigned short byte_cnt; /* Descriptor buffer byte count */
258 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
259 unsigned int cmd_sts; /* Descriptor command status */
260 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
261 struct sk_buff *return_info; /* User resource return information */
262};
263
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200264
265/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200266struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200267 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200268
269 /* used to protect SMI_REG, which is shared across ports */
270 spinlock_t phy_lock;
271
272 u32 win_protect;
273
274 unsigned int t_clk;
275};
276
277
278/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200279struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200280 u64 good_octets_received;
281 u32 bad_octets_received;
282 u32 internal_mac_transmit_err;
283 u32 good_frames_received;
284 u32 bad_frames_received;
285 u32 broadcast_frames_received;
286 u32 multicast_frames_received;
287 u32 frames_64_octets;
288 u32 frames_65_to_127_octets;
289 u32 frames_128_to_255_octets;
290 u32 frames_256_to_511_octets;
291 u32 frames_512_to_1023_octets;
292 u32 frames_1024_to_max_octets;
293 u64 good_octets_sent;
294 u32 good_frames_sent;
295 u32 excessive_collision;
296 u32 multicast_frames_sent;
297 u32 broadcast_frames_sent;
298 u32 unrec_mac_control_received;
299 u32 fc_sent;
300 u32 good_fc_received;
301 u32 bad_fc_received;
302 u32 undersize_received;
303 u32 fragments_received;
304 u32 oversize_received;
305 u32 jabber_received;
306 u32 mac_receive_error;
307 u32 bad_crc_event;
308 u32 collision;
309 u32 late_collision;
310};
311
Lennert Buytenheke5371492008-06-01 02:18:13 +0200312struct mv643xx_eth_private {
313 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200314 int port_num; /* User Ethernet port number */
315
Lennert Buytenheke5371492008-06-01 02:18:13 +0200316 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200317
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200318 u32 rx_sram_addr; /* Base address of rx sram area */
319 u32 rx_sram_size; /* Size of rx sram area */
320 u32 tx_sram_addr; /* Base address of tx sram area */
321 u32 tx_sram_size; /* Size of tx sram area */
322
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200323 /* Tx/Rx rings managment indexes fields. For driver use */
324
325 /* Next available and first returning Rx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200326 int rx_curr_desc, rx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200327
328 /* Next available and first returning Tx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200329 int tx_curr_desc, tx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200330
Lennert Buytenheke5371492008-06-01 02:18:13 +0200331#ifdef MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200332 u32 tx_clean_threshold;
333#endif
334
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200335 struct rx_desc *rx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200336 dma_addr_t rx_desc_dma;
337 int rx_desc_area_size;
338 struct sk_buff **rx_skb;
339
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200340 struct tx_desc *tx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200341 dma_addr_t tx_desc_dma;
342 int tx_desc_area_size;
343 struct sk_buff **tx_skb;
344
345 struct work_struct tx_timeout_task;
346
347 struct net_device *dev;
348 struct napi_struct napi;
349 struct net_device_stats stats;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200350 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200351 spinlock_t lock;
352 /* Size of Tx Ring per queue */
353 int tx_ring_size;
354 /* Number of tx descriptors in use */
355 int tx_desc_count;
356 /* Size of Rx Ring per queue */
357 int rx_ring_size;
358 /* Number of rx descriptors in use */
359 int rx_desc_count;
360
361 /*
362 * Used in case RX Ring is empty, which can be caused when
363 * system does not have resources (skb's)
364 */
365 struct timer_list timeout;
366
367 u32 rx_int_coal;
368 u32 tx_int_coal;
369 struct mii_if_info mii;
370};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200372
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200373/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200374static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200376 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Lennert Buytenheke5371492008-06-01 02:18:13 +0200379static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200381 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200384
385/* rxq/txq helper functions *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200386static void mv643xx_eth_port_enable_rx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200387 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200389 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Lennert Buytenheke5371492008-06-01 02:18:13 +0200392static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200393{
394 unsigned int port_num = mp->port_num;
395 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700396
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200397 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200398 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200399 if (queues) {
400 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200401 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200402
403 /* Wait for all Rx activity to terminate. */
404 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200405 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200406 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200409 return queues;
410}
411
Lennert Buytenheke5371492008-06-01 02:18:13 +0200412static void mv643xx_eth_port_enable_tx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200413 unsigned int queues)
414{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200415 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200416}
417
Lennert Buytenheke5371492008-06-01 02:18:13 +0200418static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200419{
420 unsigned int port_num = mp->port_num;
421 u32 queues;
422
423 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200424 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200425 if (queues) {
426 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200427 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200428
429 /* Wait for all Tx activity to terminate. */
430 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200431 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200432 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200433
434 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200435 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200436 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200437 }
438
439 return queues;
440}
441
442
443/* rx ***********************************************************************/
444static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
445
Lennert Buytenheke5371492008-06-01 02:18:13 +0200446static FUNC_RET_STATUS rx_return_buff(struct mv643xx_eth_private *mp,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200447 struct pkt_info *pkt_info)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200448{
449 int used_rx_desc; /* Where to return Rx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200450 volatile struct rx_desc *rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200451 unsigned long flags;
452
453 spin_lock_irqsave(&mp->lock, flags);
454
455 /* Get 'used' Rx descriptor */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200456 used_rx_desc = mp->rx_used_desc;
457 rx_desc = &mp->rx_desc_area[used_rx_desc];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200458
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200459 rx_desc->buf_ptr = pkt_info->buf_ptr;
460 rx_desc->buf_size = pkt_info->byte_cnt;
461 mp->rx_skb[used_rx_desc] = pkt_info->return_info;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200462
463 /* Flush the write pipe */
464
465 /* Return the descriptor to DMA ownership */
466 wmb();
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200467 rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200468 wmb();
469
470 /* Move the used descriptor pointer to the next descriptor */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200471 mp->rx_used_desc = (used_rx_desc + 1) % mp->rx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200472
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200473 spin_unlock_irqrestore(&mp->lock, flags);
474
475 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700478static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200480 struct mv643xx_eth_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct pkt_info pkt_info;
482 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700483 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700485 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000486 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!skb)
488 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700489 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000490 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700491 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000492 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200493 pkt_info.cmd_sts = RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700494 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
495 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
496 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 pkt_info.return_info = skb;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200498 if (rx_return_buff(mp, &pkt_info) != ETH_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 printk(KERN_ERR
500 "%s: Error allocating RX Ring\n", dev->name);
501 break;
502 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700503 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /*
506 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700507 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700509 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700511 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700516static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700518 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Lennert Buytenheke5371492008-06-01 02:18:13 +0200521static FUNC_RET_STATUS port_receive(struct mv643xx_eth_private *mp,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200522 struct pkt_info *pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200524 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200525 volatile struct rx_desc *rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200526 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700527 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200529 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700530
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200531 /* Get the Rx Desc ring 'curr and 'used' indexes */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200532 rx_curr_desc = mp->rx_curr_desc;
533 rx_used_desc = mp->rx_used_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200535 rx_desc = &mp->rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700536
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200537 /* The following parameters are used to save readings from memory */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200538 command_status = rx_desc->cmd_sts;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200539 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700540
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200541 /* Nothing to receive... */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200542 if (command_status & BUFFER_OWNED_BY_DMA) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700543 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200544 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200547 pkt_info->byte_cnt = rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
548 pkt_info->cmd_sts = command_status;
549 pkt_info->buf_ptr = rx_desc->buf_ptr + ETH_HW_IP_ALIGN;
550 pkt_info->return_info = mp->rx_skb[rx_curr_desc];
551 pkt_info->l4i_chk = rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200553 /*
554 * Clean the return info field to indicate that the
555 * packet has been moved to the upper layers
556 */
557 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700558
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200559 /* Update current index in data structure */
560 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200561 mp->rx_curr_desc = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700562
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200563 spin_unlock_irqrestore(&mp->lock, flags);
564
565 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200570 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700571 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned int received_packets = 0;
573 struct sk_buff *skb;
574 struct pkt_info pkt_info;
575
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200576 while (budget-- > 0 && port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400577 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700578 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700579 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700581
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700582 /*
583 * Update statistics.
584 * Note byte count includes 4 byte CRC count
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 stats->rx_packets++;
587 stats->rx_bytes += pkt_info.byte_cnt;
588 skb = pkt_info.return_info;
589 /*
590 * In case received a packet without first / last bits on OR
591 * the error summary bit is on, the packets needs to be dropeed.
592 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200593 if (((pkt_info.cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
594 (RX_FIRST_DESC | RX_LAST_DESC))
595 || (pkt_info.cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 stats->rx_dropped++;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200597 if ((pkt_info.cmd_sts & (RX_FIRST_DESC |
598 RX_LAST_DESC)) !=
599 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (net_ratelimit())
601 printk(KERN_ERR
602 "%s: Received packet spread "
603 "on multiple descriptors\n",
604 dev->name);
605 }
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200606 if (pkt_info.cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 stats->rx_errors++;
608
609 dev_kfree_skb_irq(skb);
610 } else {
611 /*
612 * The -4 is for the CRC in the trailer of the
613 * received packet
614 */
615 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200617 if (pkt_info.cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 skb->ip_summed = CHECKSUM_UNNECESSARY;
619 skb->csum = htons(
620 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
621 }
622 skb->protocol = eth_type_trans(skb, dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200623#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 netif_receive_skb(skb);
625#else
626 netif_rx(skb);
627#endif
628 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700629 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700631 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 return received_packets;
634}
635
Lennert Buytenheke5371492008-06-01 02:18:13 +0200636#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200637static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200639 struct mv643xx_eth_private *mp = container_of(napi, struct mv643xx_eth_private, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700640 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700642 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Lennert Buytenheke5371492008-06-01 02:18:13 +0200644#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700646 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649#endif
650
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700651 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200652 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200653 != (u32) mp->rx_used_desc)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700654 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700656 if (work_done < budget) {
657 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200658 wrl(mp, INT_CAUSE(port_num), 0);
659 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200660 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700663 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665#endif
666
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200667
668/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700669static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
670{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700671 unsigned int frag;
672 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700673
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700674 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
675 fragp = &skb_shinfo(skb)->frags[frag];
676 if (fragp->size <= 8 && fragp->page_offset & 0x7)
677 return 1;
678 }
679 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700680}
681
Lennert Buytenheke5371492008-06-01 02:18:13 +0200682static int alloc_tx_desc_index(struct mv643xx_eth_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700683{
684 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700685
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700686 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700687
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200688 tx_desc_curr = mp->tx_curr_desc;
689 mp->tx_curr_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700690
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200691 BUG_ON(mp->tx_curr_desc == mp->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700692
693 return tx_desc_curr;
694}
695
Lennert Buytenheke5371492008-06-01 02:18:13 +0200696static void tx_fill_frag_descs(struct mv643xx_eth_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700697 struct sk_buff *skb)
698{
699 int frag;
700 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200701 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700702
703 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
704 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
705
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200706 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200707 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700708
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200709 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700710 /* Last Frag enables interrupt and frees the skb */
711 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200712 desc->cmd_sts |= ZERO_PADDING |
713 TX_LAST_DESC |
714 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700715 mp->tx_skb[tx_index] = skb;
716 } else
Al Viro05980772006-05-30 23:59:09 -0400717 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700718
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200719 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720 desc->l4i_chk = 0;
721 desc->byte_cnt = this_frag->size;
722 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
723 this_frag->page_offset,
724 this_frag->size,
725 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700726 }
727}
728
Byron Bradley324ff2c2008-02-04 23:47:15 -0800729static inline __be16 sum16_as_be(__sum16 sum)
730{
731 return (__force __be16)sum;
732}
733
Lennert Buytenheke5371492008-06-01 02:18:13 +0200734static void tx_submit_descs_for_skb(struct mv643xx_eth_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700735 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700736{
737 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200738 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700739 u32 cmd_sts;
740 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700741 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700742
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200743 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700744
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200745 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200746 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700747
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700748 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200749 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700750
751 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400752 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700753 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200754 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700755 length = skb->len;
756 mp->tx_skb[tx_index] = skb;
757 }
758
759 desc->byte_cnt = length;
760 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700761
Patrick McHardy84fa7932006-08-29 16:44:56 -0700762 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800763 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700764
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200765 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
766 GEN_IP_V4_CHECKSUM |
767 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700768
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700769 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700770 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200771 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800772 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700773 break;
774 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800775 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700776 break;
777 default:
778 BUG();
779 }
780 } else {
781 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200782 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700783 desc->l4i_chk = 0;
784 }
785
786 /* ensure all other descriptors are written before first cmd_sts */
787 wmb();
788 desc->cmd_sts = cmd_sts;
789
790 /* ensure all descriptors are written before poking hardware */
791 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200792 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700793
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700794 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
798{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200799 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700800 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700803 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700804
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700805 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
806 stats->tx_dropped++;
807 printk(KERN_DEBUG "%s: failed to linearize tiny "
808 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700809 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 spin_lock_irqsave(&mp->lock, flags);
813
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700814 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
815 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
816 netif_stop_queue(dev);
817 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700818 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700819 }
820
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200821 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700822 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 stats->tx_packets++;
824 dev->trans_start = jiffies;
825
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700826 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
827 netif_stop_queue(dev);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 spin_unlock_irqrestore(&mp->lock, flags);
830
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700831 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200834
835/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200836static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200837
Lennert Buytenheke5371492008-06-01 02:18:13 +0200838static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200839 unsigned int phy_reg, unsigned int *value)
840{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200841 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
842 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200843 unsigned long flags;
844 int i;
845
846 /* the SMI register is a shared resource */
847 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
848
849 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200850 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200851 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200852 printk("%s: PHY busy timeout\n", mp->dev->name);
853 goto out;
854 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200855 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200856 }
857
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200858 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200859
860 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200861 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200862 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200863 printk("%s: PHY read timeout\n", mp->dev->name);
864 goto out;
865 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200866 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200867 }
868
869 *value = readl(smi_reg) & 0xffff;
870out:
871 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
872}
873
Lennert Buytenheke5371492008-06-01 02:18:13 +0200874static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200875 unsigned int phy_reg, unsigned int value)
876{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200877 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
878 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200879 unsigned long flags;
880 int i;
881
882 /* the SMI register is a shared resource */
883 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
884
885 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200886 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200887 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200888 printk("%s: PHY busy timeout\n", mp->dev->name);
889 goto out;
890 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200891 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200892 }
893
894 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200895 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200896out:
897 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
898}
899
900
901/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200902static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200903{
904 unsigned int port_num = mp->port_num;
905 int i;
906
907 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200908 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200909 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200910}
911
Lennert Buytenheke5371492008-06-01 02:18:13 +0200912static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200913{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200914 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200915}
916
Lennert Buytenheke5371492008-06-01 02:18:13 +0200917static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200918{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200919 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200920
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200921 p->good_octets_received += read_mib(mp, 0x00);
922 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
923 p->bad_octets_received += read_mib(mp, 0x08);
924 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
925 p->good_frames_received += read_mib(mp, 0x10);
926 p->bad_frames_received += read_mib(mp, 0x14);
927 p->broadcast_frames_received += read_mib(mp, 0x18);
928 p->multicast_frames_received += read_mib(mp, 0x1c);
929 p->frames_64_octets += read_mib(mp, 0x20);
930 p->frames_65_to_127_octets += read_mib(mp, 0x24);
931 p->frames_128_to_255_octets += read_mib(mp, 0x28);
932 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
933 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
934 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
935 p->good_octets_sent += read_mib(mp, 0x38);
936 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
937 p->good_frames_sent += read_mib(mp, 0x40);
938 p->excessive_collision += read_mib(mp, 0x44);
939 p->multicast_frames_sent += read_mib(mp, 0x48);
940 p->broadcast_frames_sent += read_mib(mp, 0x4c);
941 p->unrec_mac_control_received += read_mib(mp, 0x50);
942 p->fc_sent += read_mib(mp, 0x54);
943 p->good_fc_received += read_mib(mp, 0x58);
944 p->bad_fc_received += read_mib(mp, 0x5c);
945 p->undersize_received += read_mib(mp, 0x60);
946 p->fragments_received += read_mib(mp, 0x64);
947 p->oversize_received += read_mib(mp, 0x68);
948 p->jabber_received += read_mib(mp, 0x6c);
949 p->mac_receive_error += read_mib(mp, 0x70);
950 p->bad_crc_event += read_mib(mp, 0x74);
951 p->collision += read_mib(mp, 0x78);
952 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200953}
954
955
956/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200957struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200958 char stat_string[ETH_GSTRING_LEN];
959 int sizeof_stat;
960 int stat_offset;
961};
962
Lennert Buytenheke5371492008-06-01 02:18:13 +0200963#define MV643XX_ETH_STAT(m) FIELD_SIZEOF(struct mv643xx_eth_private, m), \
964 offsetof(struct mv643xx_eth_private, m)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200965
Lennert Buytenheke5371492008-06-01 02:18:13 +0200966static const struct mv643xx_eth_stats mv643xx_eth_gstrings_stats[] = {
967 { "rx_packets", MV643XX_ETH_STAT(stats.rx_packets) },
968 { "tx_packets", MV643XX_ETH_STAT(stats.tx_packets) },
969 { "rx_bytes", MV643XX_ETH_STAT(stats.rx_bytes) },
970 { "tx_bytes", MV643XX_ETH_STAT(stats.tx_bytes) },
971 { "rx_errors", MV643XX_ETH_STAT(stats.rx_errors) },
972 { "tx_errors", MV643XX_ETH_STAT(stats.tx_errors) },
973 { "rx_dropped", MV643XX_ETH_STAT(stats.rx_dropped) },
974 { "tx_dropped", MV643XX_ETH_STAT(stats.tx_dropped) },
975 { "good_octets_received", MV643XX_ETH_STAT(mib_counters.good_octets_received) },
976 { "bad_octets_received", MV643XX_ETH_STAT(mib_counters.bad_octets_received) },
977 { "internal_mac_transmit_err", MV643XX_ETH_STAT(mib_counters.internal_mac_transmit_err) },
978 { "good_frames_received", MV643XX_ETH_STAT(mib_counters.good_frames_received) },
979 { "bad_frames_received", MV643XX_ETH_STAT(mib_counters.bad_frames_received) },
980 { "broadcast_frames_received", MV643XX_ETH_STAT(mib_counters.broadcast_frames_received) },
981 { "multicast_frames_received", MV643XX_ETH_STAT(mib_counters.multicast_frames_received) },
982 { "frames_64_octets", MV643XX_ETH_STAT(mib_counters.frames_64_octets) },
983 { "frames_65_to_127_octets", MV643XX_ETH_STAT(mib_counters.frames_65_to_127_octets) },
984 { "frames_128_to_255_octets", MV643XX_ETH_STAT(mib_counters.frames_128_to_255_octets) },
985 { "frames_256_to_511_octets", MV643XX_ETH_STAT(mib_counters.frames_256_to_511_octets) },
986 { "frames_512_to_1023_octets", MV643XX_ETH_STAT(mib_counters.frames_512_to_1023_octets) },
987 { "frames_1024_to_max_octets", MV643XX_ETH_STAT(mib_counters.frames_1024_to_max_octets) },
988 { "good_octets_sent", MV643XX_ETH_STAT(mib_counters.good_octets_sent) },
989 { "good_frames_sent", MV643XX_ETH_STAT(mib_counters.good_frames_sent) },
990 { "excessive_collision", MV643XX_ETH_STAT(mib_counters.excessive_collision) },
991 { "multicast_frames_sent", MV643XX_ETH_STAT(mib_counters.multicast_frames_sent) },
992 { "broadcast_frames_sent", MV643XX_ETH_STAT(mib_counters.broadcast_frames_sent) },
993 { "unrec_mac_control_received", MV643XX_ETH_STAT(mib_counters.unrec_mac_control_received) },
994 { "fc_sent", MV643XX_ETH_STAT(mib_counters.fc_sent) },
995 { "good_fc_received", MV643XX_ETH_STAT(mib_counters.good_fc_received) },
996 { "bad_fc_received", MV643XX_ETH_STAT(mib_counters.bad_fc_received) },
997 { "undersize_received", MV643XX_ETH_STAT(mib_counters.undersize_received) },
998 { "fragments_received", MV643XX_ETH_STAT(mib_counters.fragments_received) },
999 { "oversize_received", MV643XX_ETH_STAT(mib_counters.oversize_received) },
1000 { "jabber_received", MV643XX_ETH_STAT(mib_counters.jabber_received) },
1001 { "mac_receive_error", MV643XX_ETH_STAT(mib_counters.mac_receive_error) },
1002 { "bad_crc_event", MV643XX_ETH_STAT(mib_counters.bad_crc_event) },
1003 { "collision", MV643XX_ETH_STAT(mib_counters.collision) },
1004 { "late_collision", MV643XX_ETH_STAT(mib_counters.late_collision) },
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001005};
1006
Lennert Buytenheke5371492008-06-01 02:18:13 +02001007#define MV643XX_ETH_STATS_LEN ARRAY_SIZE(mv643xx_eth_gstrings_stats)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001008
Lennert Buytenheke5371492008-06-01 02:18:13 +02001009static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001010{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001011 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001012 int err;
1013
1014 spin_lock_irq(&mp->lock);
1015 err = mii_ethtool_gset(&mp->mii, cmd);
1016 spin_unlock_irq(&mp->lock);
1017
1018 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1019 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1020 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1021
1022 return err;
1023}
1024
Lennert Buytenheke5371492008-06-01 02:18:13 +02001025static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001026{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001027 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001028 int err;
1029
1030 spin_lock_irq(&mp->lock);
1031 err = mii_ethtool_sset(&mp->mii, cmd);
1032 spin_unlock_irq(&mp->lock);
1033
1034 return err;
1035}
1036
Lennert Buytenheke5371492008-06-01 02:18:13 +02001037static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001038 struct ethtool_drvinfo *drvinfo)
1039{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001040 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
1041 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001042 strncpy(drvinfo->fw_version, "N/A", 32);
1043 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenheke5371492008-06-01 02:18:13 +02001044 drvinfo->n_stats = MV643XX_ETH_STATS_LEN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001045}
1046
1047static int mv643xx_eth_nway_restart(struct net_device *dev)
1048{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001049 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001050
1051 return mii_nway_restart(&mp->mii);
1052}
1053
1054static u32 mv643xx_eth_get_link(struct net_device *dev)
1055{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001056 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001057
1058 return mii_link_ok(&mp->mii);
1059}
1060
Lennert Buytenheke5371492008-06-01 02:18:13 +02001061static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001062 uint8_t *data)
1063{
1064 int i;
1065
1066 switch(stringset) {
1067 case ETH_SS_STATS:
Lennert Buytenheke5371492008-06-01 02:18:13 +02001068 for (i=0; i < MV643XX_ETH_STATS_LEN; i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001069 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001070 mv643xx_eth_gstrings_stats[i].stat_string,
1071 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001072 }
1073 break;
1074 }
1075}
1076
Lennert Buytenheke5371492008-06-01 02:18:13 +02001077static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001078 struct ethtool_stats *stats, uint64_t *data)
1079{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001080 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001081 int i;
1082
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001083 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001084
Lennert Buytenheke5371492008-06-01 02:18:13 +02001085 for (i = 0; i < MV643XX_ETH_STATS_LEN; i++) {
1086 char *p = (char *)mp+mv643xx_eth_gstrings_stats[i].stat_offset;
1087 data[i] = (mv643xx_eth_gstrings_stats[i].sizeof_stat ==
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001088 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1089 }
1090}
1091
Lennert Buytenheke5371492008-06-01 02:18:13 +02001092static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001093{
1094 switch (sset) {
1095 case ETH_SS_STATS:
Lennert Buytenheke5371492008-06-01 02:18:13 +02001096 return MV643XX_ETH_STATS_LEN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001097 default:
1098 return -EOPNOTSUPP;
1099 }
1100}
1101
Lennert Buytenheke5371492008-06-01 02:18:13 +02001102static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1103 .get_settings = mv643xx_eth_get_settings,
1104 .set_settings = mv643xx_eth_set_settings,
1105 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001106 .get_link = mv643xx_eth_get_link,
1107 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001108 .get_sset_count = mv643xx_eth_get_sset_count,
1109 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1110 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001111 .nway_reset = mv643xx_eth_nway_restart,
1112};
1113
1114
1115/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001116static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001117{
1118 unsigned int port_num = mp->port_num;
1119 unsigned int mac_h;
1120 unsigned int mac_l;
1121
1122 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1123 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1124
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001125 addr[0] = (mac_h >> 24) & 0xff;
1126 addr[1] = (mac_h >> 16) & 0xff;
1127 addr[2] = (mac_h >> 8) & 0xff;
1128 addr[3] = mac_h & 0xff;
1129 addr[4] = (mac_l >> 8) & 0xff;
1130 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131}
1132
Lennert Buytenheke5371492008-06-01 02:18:13 +02001133static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001134{
1135 unsigned int port_num = mp->port_num;
1136 int table_index;
1137
1138 /* Clear DA filter unicast table (Ex_dFUT) */
1139 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001140 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001141
1142 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1143 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001144 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001145 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001146 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001147 }
1148}
1149
Lennert Buytenheke5371492008-06-01 02:18:13 +02001150static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001151 int table, unsigned char entry)
1152{
1153 unsigned int table_reg;
1154 unsigned int tbl_offset;
1155 unsigned int reg_offset;
1156
1157 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1158 reg_offset = entry % 4; /* Entry offset within the register */
1159
1160 /* Set "accepts frame bit" at specified table entry */
1161 table_reg = rdl(mp, table + tbl_offset);
1162 table_reg |= 0x01 << (8 * reg_offset);
1163 wrl(mp, table + tbl_offset, table_reg);
1164}
1165
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001166static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001167{
1168 unsigned int port_num = mp->port_num;
1169 unsigned int mac_h;
1170 unsigned int mac_l;
1171 int table;
1172
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001173 mac_l = (addr[4] << 8) | (addr[5]);
1174 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1175 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001176
1177 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1178 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1179
1180 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001181 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001182 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001183}
1184
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001185static void mv643xx_eth_update_mac_address(struct net_device *dev)
1186{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001187 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001188
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001189 init_mac_tables(mp);
1190 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001191}
1192
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001193static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1194{
1195 int i;
1196
1197 for (i = 0; i < 6; i++)
1198 /* +2 is for the offset of the HW addr type */
1199 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1200 mv643xx_eth_update_mac_address(dev);
1201 return 0;
1202}
1203
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001204static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001205{
1206 unsigned int port_num = mp->port_num;
1207 unsigned int mac_h;
1208 unsigned int mac_l;
1209 unsigned char crc_result = 0;
1210 int table;
1211 int mac_array[48];
1212 int crc[8];
1213 int i;
1214
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001215 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1216 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001217 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001218 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001219 return;
1220 }
1221
1222 /* Calculate CRC-8 out of the given address */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001223 mac_h = (addr[0] << 8) | (addr[1]);
1224 mac_l = (addr[2] << 24) | (addr[3] << 16) |
1225 (addr[4] << 8) | (addr[5] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001226
1227 for (i = 0; i < 32; i++)
1228 mac_array[i] = (mac_l >> i) & 0x1;
1229 for (i = 32; i < 48; i++)
1230 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1231
1232 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1233 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1234 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1235 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1236 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1237
1238 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1239 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1240 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1241 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1242 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1243 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1244 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1245
1246 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1247 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1248 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1249 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1250 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1251 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1252
1253 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1254 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1255 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1256 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1257 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1258 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1259
1260 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1261 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1262 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1263 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1264 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1265 mac_array[3] ^ mac_array[2];
1266
1267 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1268 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1269 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1270 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1271 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1272 mac_array[4] ^ mac_array[3];
1273
1274 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1275 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1276 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1277 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1278 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1279 mac_array[4];
1280
1281 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1282 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1283 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1284 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1285 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1286
1287 for (i = 0; i < 8; i++)
1288 crc_result = crc_result | (crc[i] << i);
1289
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001290 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001291 set_filter_table_entry(mp, table, crc_result);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001292}
1293
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001294static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001295{
1296
1297 struct dev_mc_list *mc_list;
1298 int i;
1299 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001300 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001301 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001302
1303 /* If the device is in promiscuous mode or in all multicast mode,
1304 * we will fully populate both multicast tables with accept.
1305 * This is guaranteed to yield a match on all multicast addresses...
1306 */
1307 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1308 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1309 /* Set all entries in DA filter special multicast
1310 * table (Ex_dFSMT)
1311 * Set for ETH_Q0 for now
1312 * Bits
1313 * 0 Accept=1, Drop=0
1314 * 3-1 Queue ETH_Q0=0
1315 * 7-4 Reserved = 0;
1316 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001317 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001318
1319 /* Set all entries in DA filter other multicast
1320 * table (Ex_dFOMT)
1321 * Set for ETH_Q0 for now
1322 * Bits
1323 * 0 Accept=1, Drop=0
1324 * 3-1 Queue ETH_Q0=0
1325 * 7-4 Reserved = 0;
1326 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001327 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001328 }
1329 return;
1330 }
1331
1332 /* We will clear out multicast tables every time we get the list.
1333 * Then add the entire new list...
1334 */
1335 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1336 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001337 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001338
1339 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001340 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001341 }
1342
1343 /* Get pointer to net_device multicast list and add each one... */
1344 for (i = 0, mc_list = dev->mc_list;
1345 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1346 i++, mc_list = mc_list->next)
1347 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001348 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349}
1350
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001351static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1352{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001353 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001354 u32 config_reg;
1355
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001356 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001357 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001358 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001359 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001360 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001361 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001362
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001363 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001364}
1365
1366
1367/* rx/tx queue initialisation ***********************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001368static void ether_init_rx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001369{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001370 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001371 int rx_desc_num = mp->rx_ring_size;
1372 int i;
1373
1374 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001375 p_rx_desc = (struct rx_desc *)mp->rx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001376 for (i = 0; i < rx_desc_num; i++) {
1377 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001378 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001379 }
1380
1381 /* Save Rx desc pointer to driver struct. */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001382 mp->rx_curr_desc = 0;
1383 mp->rx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001384
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001385 mp->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001386}
1387
1388static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1389{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001390 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001391 int curr;
1392
1393 /* Stop RX Queues */
1394 mv643xx_eth_port_disable_rx(mp);
1395
1396 /* Free preallocated skb's on RX rings */
1397 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1398 if (mp->rx_skb[curr]) {
1399 dev_kfree_skb(mp->rx_skb[curr]);
1400 mp->rx_desc_count--;
1401 }
1402 }
1403
1404 if (mp->rx_desc_count)
1405 printk(KERN_ERR
1406 "%s: Error in freeing Rx Ring. %d skb's still"
1407 " stuck in RX Ring - ignoring them\n", dev->name,
1408 mp->rx_desc_count);
1409 /* Free RX ring */
1410 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001411 iounmap(mp->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001412 else
1413 dma_free_coherent(NULL, mp->rx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001414 mp->rx_desc_area, mp->rx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001415}
1416
Lennert Buytenheke5371492008-06-01 02:18:13 +02001417static void ether_init_tx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001418{
1419 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001420 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001421 int i;
1422
1423 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001424 p_tx_desc = (struct tx_desc *)mp->tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001425 for (i = 0; i < tx_desc_num; i++) {
1426 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001427 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001428 }
1429
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001430 mp->tx_curr_desc = 0;
1431 mp->tx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001432
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001433 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001434}
1435
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001436static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1437{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001438 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001439 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001440 u32 cmd_sts;
1441 struct sk_buff *skb;
1442 unsigned long flags;
1443 int tx_index;
1444 dma_addr_t addr;
1445 int count;
1446 int released = 0;
1447
1448 while (mp->tx_desc_count > 0) {
1449 spin_lock_irqsave(&mp->lock, flags);
1450
1451 /* tx_desc_count might have changed before acquiring the lock */
1452 if (mp->tx_desc_count <= 0) {
1453 spin_unlock_irqrestore(&mp->lock, flags);
1454 return released;
1455 }
1456
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001457 tx_index = mp->tx_used_desc;
1458 desc = &mp->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001459 cmd_sts = desc->cmd_sts;
1460
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001461 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001462 spin_unlock_irqrestore(&mp->lock, flags);
1463 return released;
1464 }
1465
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001466 mp->tx_used_desc = (tx_index + 1) % mp->tx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001467 mp->tx_desc_count--;
1468
1469 addr = desc->buf_ptr;
1470 count = desc->byte_cnt;
1471 skb = mp->tx_skb[tx_index];
1472 if (skb)
1473 mp->tx_skb[tx_index] = NULL;
1474
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001475 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001476 printk("%s: Error in TX\n", dev->name);
1477 dev->stats.tx_errors++;
1478 }
1479
1480 spin_unlock_irqrestore(&mp->lock, flags);
1481
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001482 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1484 else
1485 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1486
1487 if (skb)
1488 dev_kfree_skb_irq(skb);
1489
1490 released = 1;
1491 }
1492
1493 return released;
1494}
1495
1496static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1497{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001498 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001499
1500 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1501 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1502 netif_wake_queue(dev);
1503}
1504
1505static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1506{
1507 mv643xx_eth_free_tx_descs(dev, 1);
1508}
1509
1510static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1511{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001512 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001513
1514 /* Stop Tx Queues */
1515 mv643xx_eth_port_disable_tx(mp);
1516
1517 /* Free outstanding skb's on TX ring */
1518 mv643xx_eth_free_all_tx_descs(dev);
1519
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001520 BUG_ON(mp->tx_used_desc != mp->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001521
1522 /* Free TX ring */
1523 if (mp->tx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001524 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001525 else
1526 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001527 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001528}
1529
1530
1531/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001532static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001533
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001534static void mv643xx_eth_update_pscr(struct net_device *dev,
1535 struct ethtool_cmd *ecmd)
1536{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001537 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001538 int port_num = mp->port_num;
1539 u32 o_pscr, n_pscr;
1540 unsigned int queues;
1541
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001542 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001543 n_pscr = o_pscr;
1544
1545 /* clear speed, duplex and rx buffer size fields */
1546 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1547 SET_GMII_SPEED_TO_1000 |
1548 SET_FULL_DUPLEX_MODE |
1549 MAX_RX_PACKET_MASK);
1550
1551 if (ecmd->duplex == DUPLEX_FULL)
1552 n_pscr |= SET_FULL_DUPLEX_MODE;
1553
1554 if (ecmd->speed == SPEED_1000)
1555 n_pscr |= SET_GMII_SPEED_TO_1000 |
1556 MAX_RX_PACKET_9700BYTE;
1557 else {
1558 if (ecmd->speed == SPEED_100)
1559 n_pscr |= SET_MII_SPEED_TO_100;
1560 n_pscr |= MAX_RX_PACKET_1522BYTE;
1561 }
1562
1563 if (n_pscr != o_pscr) {
1564 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001565 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001566 else {
1567 queues = mv643xx_eth_port_disable_tx(mp);
1568
1569 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001570 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1571 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1572 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001573 if (queues)
1574 mv643xx_eth_port_enable_tx(mp, queues);
1575 }
1576 }
1577}
1578
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001579static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1580{
1581 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001582 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001583 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001584 unsigned int port_num = mp->port_num;
1585
1586 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001587 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1588 if (int_cause & INT_EXT) {
1589 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001590 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001591 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001592 }
1593
1594 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001595 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001596 struct ethtool_cmd cmd;
1597
1598 if (mii_link_ok(&mp->mii)) {
1599 mii_ethtool_gset(&mp->mii, &cmd);
1600 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001601 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001602 if (!netif_carrier_ok(dev)) {
1603 netif_carrier_on(dev);
1604 if (mp->tx_ring_size - mp->tx_desc_count >=
1605 MAX_DESCS_PER_SKB)
1606 netif_wake_queue(dev);
1607 }
1608 } else if (netif_carrier_ok(dev)) {
1609 netif_stop_queue(dev);
1610 netif_carrier_off(dev);
1611 }
1612 }
1613
Lennert Buytenheke5371492008-06-01 02:18:13 +02001614#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001615 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001616 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001617 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001618
1619 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001620 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001621
1622 netif_rx_schedule(dev, &mp->napi);
1623 }
1624#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001625 if (int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001626 mv643xx_eth_receive_queue(dev, INT_MAX);
1627#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001628 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001629 mv643xx_eth_free_completed_tx_descs(dev);
1630
1631 /*
1632 * If no real interrupt occured, exit.
1633 * This can happen when using gigE interrupt coalescing mechanism.
1634 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001635 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001636 return IRQ_NONE;
1637
1638 return IRQ_HANDLED;
1639}
1640
Lennert Buytenheke5371492008-06-01 02:18:13 +02001641static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001642{
1643 unsigned int phy_reg_data;
1644
1645 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001646 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001647 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001648 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001649
1650 /* wait for PHY to come out of reset */
1651 do {
1652 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001653 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654 } while (phy_reg_data & 0x8000);
1655}
1656
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001657static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001658{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001659 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001660 unsigned int port_num = mp->port_num;
1661 int tx_curr_desc, rx_curr_desc;
1662 u32 pscr;
1663 struct ethtool_cmd ethtool_cmd;
1664
1665 /* Assignment of Tx CTRP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001666 tx_curr_desc = mp->tx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001667 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001668 (u32)((struct tx_desc *)mp->tx_desc_dma + tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001669
1670 /* Assignment of Rx CRDP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001671 rx_curr_desc = mp->rx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001672 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001673 (u32)((struct rx_desc *)mp->rx_desc_dma + rx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001674
1675 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001676 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001677
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001678 /*
1679 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1680 * frames to RX queue #0.
1681 */
1682 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001683
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001684 /*
1685 * Treat BPDUs as normal multicasts, and disable partition mode.
1686 */
1687 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001688
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001689 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001690
1691 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001692 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001693
1694 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1695 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02001696 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001697 DO_NOT_FORCE_LINK_FAIL |
1698 SERIAL_PORT_CONTROL_RESERVED;
1699
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001700 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001701
1702 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001703 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001704
1705 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001706 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001707
1708 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001709 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001710
1711 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001712 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001713
1714 /* save phy settings across reset */
Lennert Buytenheke5371492008-06-01 02:18:13 +02001715 mv643xx_eth_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001716 phy_reset(mp);
Lennert Buytenheke5371492008-06-01 02:18:13 +02001717 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001718}
1719
Lennert Buytenheke5371492008-06-01 02:18:13 +02001720#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001721static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001722 unsigned int delay)
1723{
1724 unsigned int port_num = mp->port_num;
1725 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1726
1727 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001728 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001729 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001730 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001731 & 0xffc000ff));
1732
1733 return coal;
1734}
1735#endif
1736
Lennert Buytenheke5371492008-06-01 02:18:13 +02001737static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001738 unsigned int delay)
1739{
1740 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1741
1742 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001743 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001744
1745 return coal;
1746}
1747
Lennert Buytenheke5371492008-06-01 02:18:13 +02001748static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001749{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001750 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001751
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001752 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001753}
1754
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001755static int mv643xx_eth_open(struct net_device *dev)
1756{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001757 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758 unsigned int port_num = mp->port_num;
1759 unsigned int size;
1760 int err;
1761
1762 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001763 wrl(mp, INT_CAUSE(port_num), 0);
1764 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001766 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001767
1768 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1769 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1770 if (err) {
1771 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1772 return -EAGAIN;
1773 }
1774
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001775 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001776
1777 memset(&mp->timeout, 0, sizeof(struct timer_list));
1778 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
1779 mp->timeout.data = (unsigned long)dev;
1780
1781 /* Allocate RX and TX skb rings */
1782 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1783 GFP_KERNEL);
1784 if (!mp->rx_skb) {
1785 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
1786 err = -ENOMEM;
1787 goto out_free_irq;
1788 }
1789 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1790 GFP_KERNEL);
1791 if (!mp->tx_skb) {
1792 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
1793 err = -ENOMEM;
1794 goto out_free_rx_skb;
1795 }
1796
1797 /* Allocate TX ring */
1798 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001799 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800 mp->tx_desc_area_size = size;
1801
1802 if (mp->tx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001803 mp->tx_desc_area = ioremap(mp->tx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001804 mp->tx_sram_size);
1805 mp->tx_desc_dma = mp->tx_sram_addr;
1806 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001807 mp->tx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001808 &mp->tx_desc_dma,
1809 GFP_KERNEL);
1810
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001811 if (!mp->tx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001812 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1813 dev->name, size);
1814 err = -ENOMEM;
1815 goto out_free_tx_skb;
1816 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001817 BUG_ON((u32) mp->tx_desc_area & 0xf); /* check 16-byte alignment */
1818 memset((void *)mp->tx_desc_area, 0, mp->tx_desc_area_size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001819
1820 ether_init_tx_desc_ring(mp);
1821
1822 /* Allocate RX ring */
1823 mp->rx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001824 size = mp->rx_ring_size * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001825 mp->rx_desc_area_size = size;
1826
1827 if (mp->rx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001828 mp->rx_desc_area = ioremap(mp->rx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001829 mp->rx_sram_size);
1830 mp->rx_desc_dma = mp->rx_sram_addr;
1831 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001832 mp->rx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001833 &mp->rx_desc_dma,
1834 GFP_KERNEL);
1835
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001836 if (!mp->rx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001837 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1838 dev->name, size);
1839 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1840 dev->name);
1841 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001842 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001843 else
1844 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001845 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001846 err = -ENOMEM;
1847 goto out_free_tx_skb;
1848 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001849 memset((void *)mp->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001850
1851 ether_init_rx_desc_ring(mp);
1852
1853 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1854
Lennert Buytenheke5371492008-06-01 02:18:13 +02001855#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001856 napi_enable(&mp->napi);
1857#endif
1858
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001859 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001860
1861 /* Interrupt Coalescing */
1862
Lennert Buytenheke5371492008-06-01 02:18:13 +02001863#ifdef MV643XX_ETH_COAL
1864 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001865#endif
1866
Lennert Buytenheke5371492008-06-01 02:18:13 +02001867 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001868
1869 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001870 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001871
1872 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001873 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001874
1875 return 0;
1876
1877out_free_tx_skb:
1878 kfree(mp->tx_skb);
1879out_free_rx_skb:
1880 kfree(mp->rx_skb);
1881out_free_irq:
1882 free_irq(dev->irq, dev);
1883
1884 return err;
1885}
1886
Lennert Buytenheke5371492008-06-01 02:18:13 +02001887static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001888{
1889 unsigned int port_num = mp->port_num;
1890 unsigned int reg_data;
1891
1892 mv643xx_eth_port_disable_tx(mp);
1893 mv643xx_eth_port_disable_rx(mp);
1894
1895 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001896 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001897
1898 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001899 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001900 reg_data &= ~(SERIAL_PORT_ENABLE |
1901 DO_NOT_FORCE_LINK_FAIL |
1902 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001903 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001904}
1905
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001906static int mv643xx_eth_stop(struct net_device *dev)
1907{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001908 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001909 unsigned int port_num = mp->port_num;
1910
1911 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001912 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001913 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001914 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001915
Lennert Buytenheke5371492008-06-01 02:18:13 +02001916#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001917 napi_disable(&mp->napi);
1918#endif
1919 netif_carrier_off(dev);
1920 netif_stop_queue(dev);
1921
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001922 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001923
1924 mv643xx_eth_free_tx_rings(dev);
1925 mv643xx_eth_free_rx_rings(dev);
1926
1927 free_irq(dev->irq, dev);
1928
1929 return 0;
1930}
1931
1932static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1933{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001934 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001935
1936 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1937}
1938
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001939static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1940{
1941 if ((new_mtu > 9500) || (new_mtu < 64))
1942 return -EINVAL;
1943
1944 dev->mtu = new_mtu;
1945 if (!netif_running(dev))
1946 return 0;
1947
1948 /*
1949 * Stop and then re-open the interface. This will allocate RX
1950 * skbs of the new MTU.
1951 * There is a possible danger that the open will not succeed,
1952 * due to memory being full, which might fail the open function.
1953 */
1954 mv643xx_eth_stop(dev);
1955 if (mv643xx_eth_open(dev)) {
1956 printk(KERN_ERR "%s: Fatal error on opening device\n",
1957 dev->name);
1958 }
1959
1960 return 0;
1961}
1962
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001963static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1964{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001965 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001966 tx_timeout_task);
1967 struct net_device *dev = mp->dev;
1968
1969 if (!netif_running(dev))
1970 return;
1971
1972 netif_stop_queue(dev);
1973
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001974 port_reset(mp);
1975 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001976
1977 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1978 netif_wake_queue(dev);
1979}
1980
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001981static void mv643xx_eth_tx_timeout(struct net_device *dev)
1982{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001983 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001984
1985 printk(KERN_INFO "%s: TX timeout ", dev->name);
1986
1987 /* Do the reset outside of interrupt context */
1988 schedule_work(&mp->tx_timeout_task);
1989}
1990
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001991#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001992static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001993{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001994 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001995 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001996
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001997 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001998 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001999 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002000
Al Viro9da3b1a2006-10-08 15:00:44 +01002001 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002002
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002003 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002004}
2005#endif
2006
Lennert Buytenheke5371492008-06-01 02:18:13 +02002007static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002008{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002009 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002010 int val;
2011
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002012 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002013 return val;
2014}
2015
Lennert Buytenheke5371492008-06-01 02:18:13 +02002016static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002017{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002018 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002019 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002020}
2021
2022
2023/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002024static void
2025mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
2026 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002027{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002028 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029 u32 win_enable;
2030 u32 win_protect;
2031 int i;
2032
2033 for (i = 0; i < 6; i++) {
2034 writel(0, base + WINDOW_BASE(i));
2035 writel(0, base + WINDOW_SIZE(i));
2036 if (i < 4)
2037 writel(0, base + WINDOW_REMAP_HIGH(i));
2038 }
2039
2040 win_enable = 0x3f;
2041 win_protect = 0;
2042
2043 for (i = 0; i < dram->num_cs; i++) {
2044 struct mbus_dram_window *cs = dram->cs + i;
2045
2046 writel((cs->base & 0xffff0000) |
2047 (cs->mbus_attr << 8) |
2048 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2049 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2050
2051 win_enable &= ~(1 << i);
2052 win_protect |= 3 << (2 * i);
2053 }
2054
2055 writel(win_enable, base + WINDOW_BAR_ENABLE);
2056 msp->win_protect = win_protect;
2057}
2058
2059static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2060{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002061 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002062 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002063 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002064 struct resource *res;
2065 int ret;
2066
Lennert Buytenheke5371492008-06-01 02:18:13 +02002067 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002068 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2069
2070 ret = -EINVAL;
2071 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2072 if (res == NULL)
2073 goto out;
2074
2075 ret = -ENOMEM;
2076 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2077 if (msp == NULL)
2078 goto out;
2079 memset(msp, 0, sizeof(*msp));
2080
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002081 msp->base = ioremap(res->start, res->end - res->start + 1);
2082 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002083 goto out_free;
2084
2085 spin_lock_init(&msp->phy_lock);
2086 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2087
2088 platform_set_drvdata(pdev, msp);
2089
2090 /*
2091 * (Re-)program MBUS remapping windows if we are asked to.
2092 */
2093 if (pd != NULL && pd->dram != NULL)
2094 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2095
2096 return 0;
2097
2098out_free:
2099 kfree(msp);
2100out:
2101 return ret;
2102}
2103
2104static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2105{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002106 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002107
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002108 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002109 kfree(msp);
2110
2111 return 0;
2112}
2113
2114static struct platform_driver mv643xx_eth_shared_driver = {
2115 .probe = mv643xx_eth_shared_probe,
2116 .remove = mv643xx_eth_shared_remove,
2117 .driver = {
2118 .name = MV643XX_ETH_SHARED_NAME,
2119 .owner = THIS_MODULE,
2120 },
2121};
2122
Lennert Buytenheke5371492008-06-01 02:18:13 +02002123static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002124{
2125 u32 reg_data;
2126 int addr_shift = 5 * mp->port_num;
2127
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002128 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002129 reg_data &= ~(0x1f << addr_shift);
2130 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002131 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002132}
2133
Lennert Buytenheke5371492008-06-01 02:18:13 +02002134static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002135{
2136 unsigned int reg_data;
2137
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002138 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002139
2140 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2141}
2142
Lennert Buytenheke5371492008-06-01 02:18:13 +02002143static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002144{
2145 unsigned int phy_reg_data0;
2146 int auto_neg;
2147
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002148 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002149 auto_neg = phy_reg_data0 & 0x1000;
2150 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002151 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002152
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002153 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002154 if ((phy_reg_data0 & 0x1000) == auto_neg)
2155 return -ENODEV; /* change didn't take */
2156
2157 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002158 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002159 return 0;
2160}
2161
James Chapmand0412d92006-01-27 01:15:30 -07002162static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2163 int speed, int duplex,
2164 struct ethtool_cmd *cmd)
2165{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002166 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002167
2168 memset(cmd, 0, sizeof(*cmd));
2169
2170 cmd->port = PORT_MII;
2171 cmd->transceiver = XCVR_INTERNAL;
2172 cmd->phy_address = phy_address;
2173
2174 if (speed == 0) {
2175 cmd->autoneg = AUTONEG_ENABLE;
2176 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2177 cmd->speed = SPEED_100;
2178 cmd->advertising = ADVERTISED_10baseT_Half |
2179 ADVERTISED_10baseT_Full |
2180 ADVERTISED_100baseT_Half |
2181 ADVERTISED_100baseT_Full;
2182 if (mp->mii.supports_gmii)
2183 cmd->advertising |= ADVERTISED_1000baseT_Full;
2184 } else {
2185 cmd->autoneg = AUTONEG_DISABLE;
2186 cmd->speed = speed;
2187 cmd->duplex = duplex;
2188 }
2189}
2190
Russell King3ae5eae2005-11-09 22:32:44 +00002191static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002194 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002195 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 struct net_device *dev;
2197 u8 *p;
2198 struct resource *res;
2199 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002200 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002201 int duplex = DUPLEX_HALF;
2202 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002203 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002205 pd = pdev->dev.platform_data;
2206 if (pd == NULL) {
2207 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2208 return -ENODEV;
2209 }
2210
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002211 if (pd->shared == NULL) {
2212 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2213 return -ENODEV;
2214 }
2215
Lennert Buytenheke5371492008-06-01 02:18:13 +02002216 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (!dev)
2218 return -ENOMEM;
2219
Russell King3ae5eae2005-11-09 22:32:44 +00002220 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002223 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002224#ifdef MV643XX_ETH_NAPI
2225 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2229 BUG_ON(!res);
2230 dev->irq = res->start;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 dev->open = mv643xx_eth_open;
2233 dev->stop = mv643xx_eth_stop;
2234 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 dev->set_mac_address = mv643xx_eth_set_mac_address;
2236 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2237
2238 /* No need to Tx Timeout */
2239 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002241#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002242 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002243#endif
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 dev->base_addr = 0;
2247 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002248 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002249 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Lennert Buytenheke5371492008-06-01 02:18:13 +02002251#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252#ifdef MAX_SKB_FRAGS
2253 /*
2254 * Zero copy can only work if we use Discovery II memory. Else, we will
2255 * have to map the buffers to ISA memory which is only 16 MB
2256 */
Wolfram Joost63890572006-01-16 16:57:41 -07002257 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258#endif
2259#endif
2260
2261 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002262 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 spin_lock_init(&mp->lock);
2265
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002266 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002267 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002268
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002269 if (mp->shared->win_protect)
2270 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2271
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002272 mp->shared_smi = mp->shared;
2273 if (pd->shared_smi != NULL)
2274 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002277 uc_addr_get(mp, dev->dev_addr);
2278 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2279 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002281 if (is_valid_ether_addr(pd->mac_addr))
2282 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002284 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002285 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002287 if (pd->rx_queue_size)
2288 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002290 if (pd->tx_queue_size)
2291 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002293 if (pd->tx_sram_size) {
2294 mp->tx_sram_size = pd->tx_sram_size;
2295 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002298 if (pd->rx_sram_size) {
2299 mp->rx_sram_size = pd->rx_sram_size;
2300 mp->rx_sram_addr = pd->rx_sram_addr;
2301 }
2302
2303 duplex = pd->duplex;
2304 speed = pd->speed;
2305
James Chapmanc28a4f82006-01-27 01:13:15 -07002306 /* Hook up MII support for ethtool */
2307 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002308 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2309 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002310 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002311 mp->mii.phy_id_mask = 0x3f;
2312 mp->mii.reg_num_mask = 0x1f;
2313
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002314 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002316 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002317 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002321 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002322 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002323 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2324 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002325 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002326
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002327 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 err = register_netdev(dev);
2329 if (err)
2330 goto out;
2331
2332 p = dev->dev_addr;
2333 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002334 "%s: port %d with MAC address %s\n",
2335 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (dev->features & NETIF_F_SG)
2338 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2339
2340 if (dev->features & NETIF_F_IP_CSUM)
2341 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2342 dev->name);
2343
Lennert Buytenheke5371492008-06-01 02:18:13 +02002344#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2346#endif
2347
Lennert Buytenheke5371492008-06-01 02:18:13 +02002348#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2350 dev->name);
2351#endif
2352
Lennert Buytenheke5371492008-06-01 02:18:13 +02002353#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2355#endif
2356
Nicolas DETb1529872005-10-28 17:46:30 -07002357 if (mp->tx_sram_size > 0)
2358 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return 0;
2361
2362out:
2363 free_netdev(dev);
2364
2365 return err;
2366}
2367
Russell King3ae5eae2005-11-09 22:32:44 +00002368static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Russell King3ae5eae2005-11-09 22:32:44 +00002370 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 unregister_netdev(dev);
2373 flush_scheduled_work();
2374
2375 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002376 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return 0;
2378}
2379
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002380static void mv643xx_eth_shutdown(struct platform_device *pdev)
2381{
2382 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002383 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002384 unsigned int port_num = mp->port_num;
2385
2386 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002387 wrl(mp, INT_MASK(port_num), 0);
2388 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002389
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002390 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002391}
2392
Russell King3ae5eae2005-11-09 22:32:44 +00002393static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 .probe = mv643xx_eth_probe,
2395 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002396 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002397 .driver = {
2398 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002399 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002400 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401};
2402
Lennert Buytenheke5371492008-06-01 02:18:13 +02002403static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 int rc;
2406
Russell King3ae5eae2005-11-09 22:32:44 +00002407 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002409 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002411 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 return rc;
2414}
2415
Lennert Buytenheke5371492008-06-01 02:18:13 +02002416static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Russell King3ae5eae2005-11-09 22:32:44 +00002418 platform_driver_unregister(&mv643xx_eth_driver);
2419 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Lennert Buytenheke5371492008-06-01 02:18:13 +02002422module_init(mv643xx_eth_init_module);
2423module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425MODULE_LICENSE("GPL");
2426MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2427 " and Dale Farnsworth");
2428MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002429MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2430MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);