blob: 5bd4b38e953d9a0b87656fcac695d408ad673d1e [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
Lennert Buytenhek8a578112008-06-01 18:09:35 +020076#define ETH_HW_IP_ALIGN 2
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020077
78/*
79 * Registers shared between all ports.
80 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020081#define PHY_ADDR 0x0000
82#define SMI_REG 0x0004
83#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
84#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
85#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
86#define WINDOW_BAR_ENABLE 0x0290
87#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020088
89/*
90 * Per-port registers.
91 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020092#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020093#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020094#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
95#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
96#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
97#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
98#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
99#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200100#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200101#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
102#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
103#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200104#define INT_RX 0x00000804
105#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200106#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200107#define INT_EXT_LINK 0x00100000
108#define INT_EXT_PHY 0x00010000
109#define INT_EXT_TX_ERROR_0 0x00000100
110#define INT_EXT_TX_0 0x00000001
111#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_MASK(p) (0x0468 + ((p) << 10))
113#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
114#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
115#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
116#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
117#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
118#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
119#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
120#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
121#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200122
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200123
124/*
125 * SDMA configuration register.
126 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200127#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200129#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200130#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200131
132#if defined(__BIG_ENDIAN)
133#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
134 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135 TX_BURST_SIZE_4_64BIT
136#elif defined(__LITTLE_ENDIAN)
137#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
138 RX_BURST_SIZE_4_64BIT | \
139 BLM_RX_NO_SWAP | \
140 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#else
143#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
144#endif
145
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200146
147/*
148 * Port serial control register.
149 */
150#define SET_MII_SPEED_TO_100 (1 << 24)
151#define SET_GMII_SPEED_TO_1000 (1 << 23)
152#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200153#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200154#define MAX_RX_PACKET_9700BYTE (5 << 17)
155#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200156#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
157#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
158#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
159#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
160#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
161#define FORCE_LINK_PASS (1 << 1)
162#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200163
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200164#define DEFAULT_RX_QUEUE_SIZE 400
165#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200166
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200167/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200168#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
169#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
170#define SMI_OPCODE_WRITE 0 /* Completion of Read */
171#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200174/*
175 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200176 */
177#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200178struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179 u16 byte_cnt; /* Descriptor buffer byte count */
180 u16 buf_size; /* Buffer size */
181 u32 cmd_sts; /* Descriptor command status */
182 u32 next_desc_ptr; /* Next descriptor pointer */
183 u32 buf_ptr; /* Descriptor buffer pointer */
184};
185
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200186struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200187 u16 byte_cnt; /* buffer byte count */
188 u16 l4i_chk; /* CPU provided TCP checksum */
189 u32 cmd_sts; /* Command/status field */
190 u32 next_desc_ptr; /* Pointer to next descriptor */
191 u32 buf_ptr; /* pointer to buffer for this descriptor*/
192};
193#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200194struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200195 u32 cmd_sts; /* Descriptor command status */
196 u16 buf_size; /* Buffer size */
197 u16 byte_cnt; /* Descriptor buffer byte count */
198 u32 buf_ptr; /* Descriptor buffer pointer */
199 u32 next_desc_ptr; /* Next descriptor pointer */
200};
201
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200202struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200203 u32 cmd_sts; /* Command/status field */
204 u16 l4i_chk; /* CPU provided TCP checksum */
205 u16 byte_cnt; /* buffer byte count */
206 u32 buf_ptr; /* pointer to buffer for this descriptor*/
207 u32 next_desc_ptr; /* Pointer to next descriptor */
208};
209#else
210#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
211#endif
212
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200213/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200214#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200215
216/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200217#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200218
219/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200220#define LAYER_4_CHECKSUM_OK 0x40000000
221#define RX_ENABLE_INTERRUPT 0x20000000
222#define RX_FIRST_DESC 0x08000000
223#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200224
225/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200226#define TX_ENABLE_INTERRUPT 0x00800000
227#define GEN_CRC 0x00400000
228#define TX_FIRST_DESC 0x00200000
229#define TX_LAST_DESC 0x00100000
230#define ZERO_PADDING 0x00080000
231#define GEN_IP_V4_CHECKSUM 0x00040000
232#define GEN_TCP_UDP_CHECKSUM 0x00020000
233#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200234
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200235#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200236
237
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200238/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200239struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200240 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200241
242 /* used to protect SMI_REG, which is shared across ports */
243 spinlock_t phy_lock;
244
245 u32 win_protect;
246
247 unsigned int t_clk;
248};
249
250
251/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200252struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200253 u64 good_octets_received;
254 u32 bad_octets_received;
255 u32 internal_mac_transmit_err;
256 u32 good_frames_received;
257 u32 bad_frames_received;
258 u32 broadcast_frames_received;
259 u32 multicast_frames_received;
260 u32 frames_64_octets;
261 u32 frames_65_to_127_octets;
262 u32 frames_128_to_255_octets;
263 u32 frames_256_to_511_octets;
264 u32 frames_512_to_1023_octets;
265 u32 frames_1024_to_max_octets;
266 u64 good_octets_sent;
267 u32 good_frames_sent;
268 u32 excessive_collision;
269 u32 multicast_frames_sent;
270 u32 broadcast_frames_sent;
271 u32 unrec_mac_control_received;
272 u32 fc_sent;
273 u32 good_fc_received;
274 u32 bad_fc_received;
275 u32 undersize_received;
276 u32 fragments_received;
277 u32 oversize_received;
278 u32 jabber_received;
279 u32 mac_receive_error;
280 u32 bad_crc_event;
281 u32 collision;
282 u32 late_collision;
283};
284
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200285struct rx_queue {
286 int rx_ring_size;
287
288 int rx_desc_count;
289 int rx_curr_desc;
290 int rx_used_desc;
291
292 struct rx_desc *rx_desc_area;
293 dma_addr_t rx_desc_dma;
294 int rx_desc_area_size;
295 struct sk_buff **rx_skb;
296
297 struct timer_list rx_oom;
298};
299
Lennert Buytenheke5371492008-06-01 02:18:13 +0200300struct mv643xx_eth_private {
301 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200302 int port_num; /* User Ethernet port number */
303
Lennert Buytenheke5371492008-06-01 02:18:13 +0200304 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200305
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200306 u32 tx_sram_addr; /* Base address of tx sram area */
307 u32 tx_sram_size; /* Size of tx sram area */
308
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200309 /* Tx/Rx rings managment indexes fields. For driver use */
310
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200311 /* Next available and first returning Tx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200312 int tx_curr_desc, tx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200313
Lennert Buytenheke5371492008-06-01 02:18:13 +0200314#ifdef MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200315 u32 tx_clean_threshold;
316#endif
317
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200318 struct tx_desc *tx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200319 dma_addr_t tx_desc_dma;
320 int tx_desc_area_size;
321 struct sk_buff **tx_skb;
322
323 struct work_struct tx_timeout_task;
324
325 struct net_device *dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200326 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200327 spinlock_t lock;
328 /* Size of Tx Ring per queue */
329 int tx_ring_size;
330 /* Number of tx descriptors in use */
331 int tx_desc_count;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200332
333 u32 rx_int_coal;
334 u32 tx_int_coal;
335 struct mii_if_info mii;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200336
337 /*
338 * RX state.
339 */
340 int default_rx_ring_size;
341 unsigned long rx_desc_sram_addr;
342 int rx_desc_sram_size;
343 struct napi_struct napi;
344 struct rx_queue rxq[1];
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200345};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200347
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200348/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200349static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200351 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Lennert Buytenheke5371492008-06-01 02:18:13 +0200354static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200356 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200359
360/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200361static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200363 return container_of(rxq, struct mv643xx_eth_private, rxq[0]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200366static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200367{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200368 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
369 wrl(mp, RXQ_COMMAND(mp->port_num), 1);
370}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700371
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200372static void rxq_disable(struct rx_queue *rxq)
373{
374 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
375 u8 mask = 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200376
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200377 wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
378 while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
379 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200380}
381
Lennert Buytenheke5371492008-06-01 02:18:13 +0200382static void mv643xx_eth_port_enable_tx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200383 unsigned int queues)
384{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200385 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200386}
387
Lennert Buytenheke5371492008-06-01 02:18:13 +0200388static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200389{
390 unsigned int port_num = mp->port_num;
391 u32 queues;
392
393 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200394 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200395 if (queues) {
396 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200397 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200398
399 /* Wait for all Tx activity to terminate. */
400 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200401 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200402 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200403
404 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200405 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200406 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200407 }
408
409 return queues;
410}
411
412
413/* rx ***********************************************************************/
414static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
415
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200416static void rxq_refill(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200417{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200418 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200419 unsigned long flags;
420
421 spin_lock_irqsave(&mp->lock, flags);
422
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200423 while (rxq->rx_desc_count < rxq->rx_ring_size) {
424 int skb_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200425 struct sk_buff *skb;
426 int unaligned;
427 int rx;
428
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200429 /*
430 * Reserve 2+14 bytes for an ethernet header (the
431 * hardware automatically prepends 2 bytes of dummy
432 * data to each received packet), 4 bytes for a VLAN
433 * header, and 4 bytes for the trailing FCS -- 24
434 * bytes total.
435 */
436 skb_size = mp->dev->mtu + 24;
437
438 skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200439 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200441
Ralf Baechle908b6372007-02-26 19:52:06 +0000442 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700443 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000444 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200445
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200446 rxq->rx_desc_count++;
447 rx = rxq->rx_used_desc;
448 rxq->rx_used_desc = (rx + 1) % rxq->rx_ring_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200449
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200450 rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
451 skb_size, DMA_FROM_DEVICE);
452 rxq->rx_desc_area[rx].buf_size = skb_size;
453 rxq->rx_skb[rx] = skb;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200454 wmb();
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200455 rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200456 RX_ENABLE_INTERRUPT;
457 wmb();
458
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700459 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200461
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200462 if (rxq->rx_desc_count == 0) {
463 rxq->rx_oom.expires = jiffies + (HZ / 10);
464 add_timer(&rxq->rx_oom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200466
467 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200470static inline void rxq_refill_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200472 rxq_refill((struct rx_queue *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200475static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200477 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
478 struct net_device_stats *stats = &mp->dev->stats;
479 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200481 rx = 0;
482 while (rx < budget) {
Lennert Buytenhek96587662008-06-01 10:31:56 +0200483 struct sk_buff *skb;
484 volatile struct rx_desc *rx_desc;
485 unsigned int cmd_sts;
486 unsigned long flags;
487
488 spin_lock_irqsave(&mp->lock, flags);
489
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200490 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200491
492 cmd_sts = rx_desc->cmd_sts;
493 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
494 spin_unlock_irqrestore(&mp->lock, flags);
495 break;
496 }
497 rmb();
498
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200499 skb = rxq->rx_skb[rxq->rx_curr_desc];
500 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200501
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200502 rxq->rx_curr_desc = (rxq->rx_curr_desc + 1) % rxq->rx_ring_size;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200503
504 spin_unlock_irqrestore(&mp->lock, flags);
505
506 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200507 mp->dev->mtu + 24, DMA_FROM_DEVICE);
508 rxq->rx_desc_count--;
509 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700510
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700511 /*
512 * Update statistics.
513 * Note byte count includes 4 byte CRC count
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200516 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * In case received a packet without first / last bits on OR
520 * the error summary bit is on, the packets needs to be dropeed.
521 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200522 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200523 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200524 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200526 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200527 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (net_ratelimit())
529 printk(KERN_ERR
530 "%s: Received packet spread "
531 "on multiple descriptors\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200532 mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200534 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 stats->rx_errors++;
536
537 dev_kfree_skb_irq(skb);
538 } else {
539 /*
540 * The -4 is for the CRC in the trailer of the
541 * received packet
542 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200543 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Lennert Buytenhek96587662008-06-01 10:31:56 +0200545 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 skb->ip_summed = CHECKSUM_UNNECESSARY;
547 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200548 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200550 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200551#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 netif_receive_skb(skb);
553#else
554 netif_rx(skb);
555#endif
556 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200557 mp->dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200559 rxq_refill(rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200561 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Lennert Buytenheke5371492008-06-01 02:18:13 +0200564#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200565static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200567 struct mv643xx_eth_private *mp;
568 int rx;
569
570 mp = container_of(napi, struct mv643xx_eth_private, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Lennert Buytenheke5371492008-06-01 02:18:13 +0200572#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (++mp->tx_clean_threshold > 5) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200574 mv643xx_eth_free_completed_tx_descs(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577#endif
578
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200579 rx = rxq_process(mp->rxq, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200581 if (rx < budget) {
582 netif_rx_complete(mp->dev, napi);
583 wrl(mp, INT_CAUSE(mp->port_num), 0);
584 wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
585 wrl(mp, INT_MASK(mp->port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200588 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590#endif
591
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200592
593/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700594static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
595{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700596 unsigned int frag;
597 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700598
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700599 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
600 fragp = &skb_shinfo(skb)->frags[frag];
601 if (fragp->size <= 8 && fragp->page_offset & 0x7)
602 return 1;
603 }
604 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700605}
606
Lennert Buytenheke5371492008-06-01 02:18:13 +0200607static int alloc_tx_desc_index(struct mv643xx_eth_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700608{
609 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700610
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700611 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700612
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200613 tx_desc_curr = mp->tx_curr_desc;
614 mp->tx_curr_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700615
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200616 BUG_ON(mp->tx_curr_desc == mp->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700617
618 return tx_desc_curr;
619}
620
Lennert Buytenheke5371492008-06-01 02:18:13 +0200621static void tx_fill_frag_descs(struct mv643xx_eth_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700622 struct sk_buff *skb)
623{
624 int frag;
625 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200626 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700627
628 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
629 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
630
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200631 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200632 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700633
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200634 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700635 /* Last Frag enables interrupt and frees the skb */
636 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200637 desc->cmd_sts |= ZERO_PADDING |
638 TX_LAST_DESC |
639 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700640 mp->tx_skb[tx_index] = skb;
641 } else
Al Viro05980772006-05-30 23:59:09 -0400642 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700643
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200644 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700645 desc->l4i_chk = 0;
646 desc->byte_cnt = this_frag->size;
647 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
648 this_frag->page_offset,
649 this_frag->size,
650 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700651 }
652}
653
Byron Bradley324ff2c2008-02-04 23:47:15 -0800654static inline __be16 sum16_as_be(__sum16 sum)
655{
656 return (__force __be16)sum;
657}
658
Lennert Buytenheke5371492008-06-01 02:18:13 +0200659static void tx_submit_descs_for_skb(struct mv643xx_eth_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700660 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700661{
662 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200663 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700664 u32 cmd_sts;
665 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700666 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700667
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200668 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700669
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200670 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200671 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700672
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700673 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200674 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700675
676 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400677 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700678 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200679 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700680 length = skb->len;
681 mp->tx_skb[tx_index] = skb;
682 }
683
684 desc->byte_cnt = length;
685 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700686
Patrick McHardy84fa7932006-08-29 16:44:56 -0700687 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800688 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700689
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200690 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
691 GEN_IP_V4_CHECKSUM |
692 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700693
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700694 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700695 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200696 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800697 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700698 break;
699 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800700 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700701 break;
702 default:
703 BUG();
704 }
705 } else {
706 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200707 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700708 desc->l4i_chk = 0;
709 }
710
711 /* ensure all other descriptors are written before first cmd_sts */
712 wmb();
713 desc->cmd_sts = cmd_sts;
714
715 /* ensure all descriptors are written before poking hardware */
716 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200717 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700718
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700719 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
723{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200724 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700725 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700728 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700729
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700730 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
731 stats->tx_dropped++;
732 printk(KERN_DEBUG "%s: failed to linearize tiny "
733 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700734 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 spin_lock_irqsave(&mp->lock, flags);
738
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700739 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
740 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
741 netif_stop_queue(dev);
742 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700743 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700744 }
745
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200746 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700747 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 stats->tx_packets++;
749 dev->trans_start = jiffies;
750
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700751 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
752 netif_stop_queue(dev);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_unlock_irqrestore(&mp->lock, flags);
755
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700756 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200759
760/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200761static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200762
Lennert Buytenheke5371492008-06-01 02:18:13 +0200763static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200764 unsigned int phy_reg, unsigned int *value)
765{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200766 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
767 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200768 unsigned long flags;
769 int i;
770
771 /* the SMI register is a shared resource */
772 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
773
774 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200775 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200776 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200777 printk("%s: PHY busy timeout\n", mp->dev->name);
778 goto out;
779 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200780 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200781 }
782
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200783 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200784
785 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200786 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200787 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200788 printk("%s: PHY read timeout\n", mp->dev->name);
789 goto out;
790 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200791 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200792 }
793
794 *value = readl(smi_reg) & 0xffff;
795out:
796 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
797}
798
Lennert Buytenheke5371492008-06-01 02:18:13 +0200799static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200800 unsigned int phy_reg, unsigned int value)
801{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200802 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
803 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200804 unsigned long flags;
805 int i;
806
807 /* the SMI register is a shared resource */
808 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
809
810 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200811 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200812 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200813 printk("%s: PHY busy timeout\n", mp->dev->name);
814 goto out;
815 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200816 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200817 }
818
819 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200820 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200821out:
822 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
823}
824
825
826/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200827static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200828{
829 unsigned int port_num = mp->port_num;
830 int i;
831
832 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200833 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200834 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200835}
836
Lennert Buytenheke5371492008-06-01 02:18:13 +0200837static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200838{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200839 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200840}
841
Lennert Buytenheke5371492008-06-01 02:18:13 +0200842static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200843{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200844 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200845
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200846 p->good_octets_received += read_mib(mp, 0x00);
847 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
848 p->bad_octets_received += read_mib(mp, 0x08);
849 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
850 p->good_frames_received += read_mib(mp, 0x10);
851 p->bad_frames_received += read_mib(mp, 0x14);
852 p->broadcast_frames_received += read_mib(mp, 0x18);
853 p->multicast_frames_received += read_mib(mp, 0x1c);
854 p->frames_64_octets += read_mib(mp, 0x20);
855 p->frames_65_to_127_octets += read_mib(mp, 0x24);
856 p->frames_128_to_255_octets += read_mib(mp, 0x28);
857 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
858 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
859 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
860 p->good_octets_sent += read_mib(mp, 0x38);
861 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
862 p->good_frames_sent += read_mib(mp, 0x40);
863 p->excessive_collision += read_mib(mp, 0x44);
864 p->multicast_frames_sent += read_mib(mp, 0x48);
865 p->broadcast_frames_sent += read_mib(mp, 0x4c);
866 p->unrec_mac_control_received += read_mib(mp, 0x50);
867 p->fc_sent += read_mib(mp, 0x54);
868 p->good_fc_received += read_mib(mp, 0x58);
869 p->bad_fc_received += read_mib(mp, 0x5c);
870 p->undersize_received += read_mib(mp, 0x60);
871 p->fragments_received += read_mib(mp, 0x64);
872 p->oversize_received += read_mib(mp, 0x68);
873 p->jabber_received += read_mib(mp, 0x6c);
874 p->mac_receive_error += read_mib(mp, 0x70);
875 p->bad_crc_event += read_mib(mp, 0x74);
876 p->collision += read_mib(mp, 0x78);
877 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200878}
879
880
881/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200882struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200883 char stat_string[ETH_GSTRING_LEN];
884 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +0200885 int netdev_off;
886 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200887};
888
Lennert Buytenhek16820052008-06-01 11:40:29 +0200889#define SSTAT(m) \
890 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
891 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200892
Lennert Buytenhek16820052008-06-01 11:40:29 +0200893#define MIBSTAT(m) \
894 { #m, FIELD_SIZEOF(struct mib_counters, m), \
895 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
896
897static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
898 SSTAT(rx_packets),
899 SSTAT(tx_packets),
900 SSTAT(rx_bytes),
901 SSTAT(tx_bytes),
902 SSTAT(rx_errors),
903 SSTAT(tx_errors),
904 SSTAT(rx_dropped),
905 SSTAT(tx_dropped),
906 MIBSTAT(good_octets_received),
907 MIBSTAT(bad_octets_received),
908 MIBSTAT(internal_mac_transmit_err),
909 MIBSTAT(good_frames_received),
910 MIBSTAT(bad_frames_received),
911 MIBSTAT(broadcast_frames_received),
912 MIBSTAT(multicast_frames_received),
913 MIBSTAT(frames_64_octets),
914 MIBSTAT(frames_65_to_127_octets),
915 MIBSTAT(frames_128_to_255_octets),
916 MIBSTAT(frames_256_to_511_octets),
917 MIBSTAT(frames_512_to_1023_octets),
918 MIBSTAT(frames_1024_to_max_octets),
919 MIBSTAT(good_octets_sent),
920 MIBSTAT(good_frames_sent),
921 MIBSTAT(excessive_collision),
922 MIBSTAT(multicast_frames_sent),
923 MIBSTAT(broadcast_frames_sent),
924 MIBSTAT(unrec_mac_control_received),
925 MIBSTAT(fc_sent),
926 MIBSTAT(good_fc_received),
927 MIBSTAT(bad_fc_received),
928 MIBSTAT(undersize_received),
929 MIBSTAT(fragments_received),
930 MIBSTAT(oversize_received),
931 MIBSTAT(jabber_received),
932 MIBSTAT(mac_receive_error),
933 MIBSTAT(bad_crc_event),
934 MIBSTAT(collision),
935 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200936};
937
Lennert Buytenheke5371492008-06-01 02:18:13 +0200938static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200939{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200940 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200941 int err;
942
943 spin_lock_irq(&mp->lock);
944 err = mii_ethtool_gset(&mp->mii, cmd);
945 spin_unlock_irq(&mp->lock);
946
947 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
948 cmd->supported &= ~SUPPORTED_1000baseT_Half;
949 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
950
951 return err;
952}
953
Lennert Buytenheke5371492008-06-01 02:18:13 +0200954static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200955{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200956 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200957 int err;
958
959 spin_lock_irq(&mp->lock);
960 err = mii_ethtool_sset(&mp->mii, cmd);
961 spin_unlock_irq(&mp->lock);
962
963 return err;
964}
965
Lennert Buytenheke5371492008-06-01 02:18:13 +0200966static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200967 struct ethtool_drvinfo *drvinfo)
968{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200969 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
970 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200971 strncpy(drvinfo->fw_version, "N/A", 32);
972 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +0200973 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200974}
975
976static int mv643xx_eth_nway_restart(struct net_device *dev)
977{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200978 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200979
980 return mii_nway_restart(&mp->mii);
981}
982
983static u32 mv643xx_eth_get_link(struct net_device *dev)
984{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200985 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200986
987 return mii_link_ok(&mp->mii);
988}
989
Lennert Buytenheke5371492008-06-01 02:18:13 +0200990static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200991 uint8_t *data)
992{
993 int i;
994
995 switch(stringset) {
996 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +0200997 for (i=0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200998 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +0200999 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001000 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001001 }
1002 break;
1003 }
1004}
1005
Lennert Buytenheke5371492008-06-01 02:18:13 +02001006static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001007 struct ethtool_stats *stats, uint64_t *data)
1008{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001009 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001010 int i;
1011
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001012 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001013
Lennert Buytenhek16820052008-06-01 11:40:29 +02001014 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1015 const struct mv643xx_eth_stats *stat;
1016 void *p;
1017
1018 stat = mv643xx_eth_stats + i;
1019
1020 if (stat->netdev_off >= 0)
1021 p = ((void *)mp->dev) + stat->netdev_off;
1022 else
1023 p = ((void *)mp) + stat->mp_off;
1024
1025 data[i] = (stat->sizeof_stat == 8) ?
1026 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001027 }
1028}
1029
Lennert Buytenheke5371492008-06-01 02:18:13 +02001030static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001031{
1032 switch (sset) {
1033 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001034 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001035 default:
1036 return -EOPNOTSUPP;
1037 }
1038}
1039
Lennert Buytenheke5371492008-06-01 02:18:13 +02001040static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1041 .get_settings = mv643xx_eth_get_settings,
1042 .set_settings = mv643xx_eth_set_settings,
1043 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001044 .get_link = mv643xx_eth_get_link,
1045 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001046 .get_sset_count = mv643xx_eth_get_sset_count,
1047 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1048 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001049 .nway_reset = mv643xx_eth_nway_restart,
1050};
1051
1052
1053/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001054static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001055{
1056 unsigned int port_num = mp->port_num;
1057 unsigned int mac_h;
1058 unsigned int mac_l;
1059
1060 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1061 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1062
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001063 addr[0] = (mac_h >> 24) & 0xff;
1064 addr[1] = (mac_h >> 16) & 0xff;
1065 addr[2] = (mac_h >> 8) & 0xff;
1066 addr[3] = mac_h & 0xff;
1067 addr[4] = (mac_l >> 8) & 0xff;
1068 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001069}
1070
Lennert Buytenheke5371492008-06-01 02:18:13 +02001071static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001072{
1073 unsigned int port_num = mp->port_num;
1074 int table_index;
1075
1076 /* Clear DA filter unicast table (Ex_dFUT) */
1077 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001078 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001079
1080 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1081 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001082 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001083 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001084 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001085 }
1086}
1087
Lennert Buytenheke5371492008-06-01 02:18:13 +02001088static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001089 int table, unsigned char entry)
1090{
1091 unsigned int table_reg;
1092 unsigned int tbl_offset;
1093 unsigned int reg_offset;
1094
1095 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1096 reg_offset = entry % 4; /* Entry offset within the register */
1097
1098 /* Set "accepts frame bit" at specified table entry */
1099 table_reg = rdl(mp, table + tbl_offset);
1100 table_reg |= 0x01 << (8 * reg_offset);
1101 wrl(mp, table + tbl_offset, table_reg);
1102}
1103
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001104static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001105{
1106 unsigned int port_num = mp->port_num;
1107 unsigned int mac_h;
1108 unsigned int mac_l;
1109 int table;
1110
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001111 mac_l = (addr[4] << 8) | (addr[5]);
1112 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1113 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001114
1115 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1116 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1117
1118 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001119 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001120 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001121}
1122
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001123static void mv643xx_eth_update_mac_address(struct net_device *dev)
1124{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001125 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001126
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001127 init_mac_tables(mp);
1128 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001129}
1130
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1132{
1133 int i;
1134
1135 for (i = 0; i < 6; i++)
1136 /* +2 is for the offset of the HW addr type */
1137 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1138 mv643xx_eth_update_mac_address(dev);
1139 return 0;
1140}
1141
Lennert Buytenhek69876562008-06-01 11:43:32 +02001142static int addr_crc(unsigned char *addr)
1143{
1144 int crc = 0;
1145 int i;
1146
1147 for (i = 0; i < 6; i++) {
1148 int j;
1149
1150 crc = (crc ^ addr[i]) << 8;
1151 for (j = 7; j >= 0; j--) {
1152 if (crc & (0x100 << j))
1153 crc ^= 0x107 << j;
1154 }
1155 }
1156
1157 return crc;
1158}
1159
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001160static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001161{
1162 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001163 int table;
Lennert Buytenhek69876562008-06-01 11:43:32 +02001164 int crc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001165
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001166 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1167 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001168 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001169 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001170 return;
1171 }
1172
Lennert Buytenhek69876562008-06-01 11:43:32 +02001173 crc = addr_crc(addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001174
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001175 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhek69876562008-06-01 11:43:32 +02001176 set_filter_table_entry(mp, table, crc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001177}
1178
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001179static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001180{
1181
1182 struct dev_mc_list *mc_list;
1183 int i;
1184 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001185 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001186 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001187
1188 /* If the device is in promiscuous mode or in all multicast mode,
1189 * we will fully populate both multicast tables with accept.
1190 * This is guaranteed to yield a match on all multicast addresses...
1191 */
1192 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1193 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1194 /* Set all entries in DA filter special multicast
1195 * table (Ex_dFSMT)
1196 * Set for ETH_Q0 for now
1197 * Bits
1198 * 0 Accept=1, Drop=0
1199 * 3-1 Queue ETH_Q0=0
1200 * 7-4 Reserved = 0;
1201 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001202 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001203
1204 /* Set all entries in DA filter other multicast
1205 * table (Ex_dFOMT)
1206 * Set for ETH_Q0 for now
1207 * Bits
1208 * 0 Accept=1, Drop=0
1209 * 3-1 Queue ETH_Q0=0
1210 * 7-4 Reserved = 0;
1211 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001212 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001213 }
1214 return;
1215 }
1216
1217 /* We will clear out multicast tables every time we get the list.
1218 * Then add the entire new list...
1219 */
1220 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1221 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001222 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001223
1224 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001225 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001226 }
1227
1228 /* Get pointer to net_device multicast list and add each one... */
1229 for (i = 0, mc_list = dev->mc_list;
1230 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1231 i++, mc_list = mc_list->next)
1232 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001233 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001234}
1235
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001236static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1237{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001238 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001239 u32 config_reg;
1240
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001241 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001242 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001243 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001244 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001245 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001246 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001247
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001248 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001249}
1250
1251
1252/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001253static int rxq_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001254{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001255 struct rx_queue *rxq = mp->rxq;
1256 struct rx_desc *rx_desc;
1257 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258 int i;
1259
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001260 rxq->rx_ring_size = mp->default_rx_ring_size;
1261
1262 rxq->rx_desc_count = 0;
1263 rxq->rx_curr_desc = 0;
1264 rxq->rx_used_desc = 0;
1265
1266 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1267
1268 if (size <= mp->rx_desc_sram_size) {
1269 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1270 mp->rx_desc_sram_size);
1271 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1272 } else {
1273 rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
1274 &rxq->rx_desc_dma,
1275 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001276 }
1277
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001278 if (rxq->rx_desc_area == NULL) {
1279 dev_printk(KERN_ERR, &mp->dev->dev,
1280 "can't allocate rx ring (%d bytes)\n", size);
1281 goto out;
1282 }
1283 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001284
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001285 rxq->rx_desc_area_size = size;
1286 rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1287 GFP_KERNEL);
1288 if (rxq->rx_skb == NULL) {
1289 dev_printk(KERN_ERR, &mp->dev->dev,
1290 "can't allocate rx skb ring\n");
1291 goto out_free;
1292 }
1293
1294 rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1295 for (i = 0; i < rxq->rx_ring_size; i++) {
1296 int nexti = (i + 1) % rxq->rx_ring_size;
1297 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1298 nexti * sizeof(struct rx_desc);
1299 }
1300
1301 init_timer(&rxq->rx_oom);
1302 rxq->rx_oom.data = (unsigned long)rxq;
1303 rxq->rx_oom.function = rxq_refill_timer_wrapper;
1304
1305 return 0;
1306
1307
1308out_free:
1309 if (size <= mp->rx_desc_sram_size)
1310 iounmap(rxq->rx_desc_area);
1311 else
1312 dma_free_coherent(NULL, size,
1313 rxq->rx_desc_area,
1314 rxq->rx_desc_dma);
1315
1316out:
1317 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001318}
1319
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001320static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001321{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001322 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1323 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001324
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001325 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001327 del_timer_sync(&rxq->rx_oom);
1328
1329 for (i = 0; i < rxq->rx_ring_size; i++) {
1330 if (rxq->rx_skb[i]) {
1331 dev_kfree_skb(rxq->rx_skb[i]);
1332 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001333 }
1334 }
1335
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001336 if (rxq->rx_desc_count) {
1337 dev_printk(KERN_ERR, &mp->dev->dev,
1338 "error freeing rx ring -- %d skbs stuck\n",
1339 rxq->rx_desc_count);
1340 }
1341
1342 if (rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1343 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001344 else
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001345 dma_free_coherent(NULL, rxq->rx_desc_area_size,
1346 rxq->rx_desc_area, rxq->rx_desc_dma);
1347
1348 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349}
1350
Lennert Buytenheke5371492008-06-01 02:18:13 +02001351static void ether_init_tx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001352{
1353 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001354 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001355 int i;
1356
1357 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001358 p_tx_desc = (struct tx_desc *)mp->tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001359 for (i = 0; i < tx_desc_num; i++) {
1360 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001361 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001362 }
1363
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001364 mp->tx_curr_desc = 0;
1365 mp->tx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001366
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001367 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001368}
1369
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001370static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1371{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001372 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001373 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001374 u32 cmd_sts;
1375 struct sk_buff *skb;
1376 unsigned long flags;
1377 int tx_index;
1378 dma_addr_t addr;
1379 int count;
1380 int released = 0;
1381
1382 while (mp->tx_desc_count > 0) {
1383 spin_lock_irqsave(&mp->lock, flags);
1384
1385 /* tx_desc_count might have changed before acquiring the lock */
1386 if (mp->tx_desc_count <= 0) {
1387 spin_unlock_irqrestore(&mp->lock, flags);
1388 return released;
1389 }
1390
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001391 tx_index = mp->tx_used_desc;
1392 desc = &mp->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001393 cmd_sts = desc->cmd_sts;
1394
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001395 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001396 spin_unlock_irqrestore(&mp->lock, flags);
1397 return released;
1398 }
1399
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001400 mp->tx_used_desc = (tx_index + 1) % mp->tx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001401 mp->tx_desc_count--;
1402
1403 addr = desc->buf_ptr;
1404 count = desc->byte_cnt;
1405 skb = mp->tx_skb[tx_index];
1406 if (skb)
1407 mp->tx_skb[tx_index] = NULL;
1408
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001409 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001410 printk("%s: Error in TX\n", dev->name);
1411 dev->stats.tx_errors++;
1412 }
1413
1414 spin_unlock_irqrestore(&mp->lock, flags);
1415
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001416 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001417 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1418 else
1419 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1420
1421 if (skb)
1422 dev_kfree_skb_irq(skb);
1423
1424 released = 1;
1425 }
1426
1427 return released;
1428}
1429
1430static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1431{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001432 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001433
1434 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1435 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1436 netif_wake_queue(dev);
1437}
1438
1439static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1440{
1441 mv643xx_eth_free_tx_descs(dev, 1);
1442}
1443
1444static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1445{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001446 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001447
1448 /* Stop Tx Queues */
1449 mv643xx_eth_port_disable_tx(mp);
1450
1451 /* Free outstanding skb's on TX ring */
1452 mv643xx_eth_free_all_tx_descs(dev);
1453
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001454 BUG_ON(mp->tx_used_desc != mp->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001455
1456 /* Free TX ring */
1457 if (mp->tx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001458 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001459 else
1460 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001461 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001462}
1463
1464
1465/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001466static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001467
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001468static void mv643xx_eth_update_pscr(struct net_device *dev,
1469 struct ethtool_cmd *ecmd)
1470{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001471 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001472 int port_num = mp->port_num;
1473 u32 o_pscr, n_pscr;
1474 unsigned int queues;
1475
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001476 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001477 n_pscr = o_pscr;
1478
1479 /* clear speed, duplex and rx buffer size fields */
1480 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1481 SET_GMII_SPEED_TO_1000 |
1482 SET_FULL_DUPLEX_MODE |
1483 MAX_RX_PACKET_MASK);
1484
1485 if (ecmd->duplex == DUPLEX_FULL)
1486 n_pscr |= SET_FULL_DUPLEX_MODE;
1487
1488 if (ecmd->speed == SPEED_1000)
1489 n_pscr |= SET_GMII_SPEED_TO_1000 |
1490 MAX_RX_PACKET_9700BYTE;
1491 else {
1492 if (ecmd->speed == SPEED_100)
1493 n_pscr |= SET_MII_SPEED_TO_100;
1494 n_pscr |= MAX_RX_PACKET_1522BYTE;
1495 }
1496
1497 if (n_pscr != o_pscr) {
1498 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001499 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001500 else {
1501 queues = mv643xx_eth_port_disable_tx(mp);
1502
1503 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001504 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1505 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1506 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001507 if (queues)
1508 mv643xx_eth_port_enable_tx(mp, queues);
1509 }
1510 }
1511}
1512
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001513static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1514{
1515 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001516 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001517 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001518 unsigned int port_num = mp->port_num;
1519
1520 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001521 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1522 if (int_cause & INT_EXT) {
1523 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001524 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001525 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001526 }
1527
1528 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001529 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001530 struct ethtool_cmd cmd;
1531
1532 if (mii_link_ok(&mp->mii)) {
1533 mii_ethtool_gset(&mp->mii, &cmd);
1534 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001535 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001536 if (!netif_carrier_ok(dev)) {
1537 netif_carrier_on(dev);
1538 if (mp->tx_ring_size - mp->tx_desc_count >=
1539 MAX_DESCS_PER_SKB)
1540 netif_wake_queue(dev);
1541 }
1542 } else if (netif_carrier_ok(dev)) {
1543 netif_stop_queue(dev);
1544 netif_carrier_off(dev);
1545 }
1546 }
1547
Lennert Buytenheke5371492008-06-01 02:18:13 +02001548#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001549 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001550 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001551 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001552
1553 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001554 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001555
1556 netif_rx_schedule(dev, &mp->napi);
1557 }
1558#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001559 if (int_cause & INT_RX)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001560 rxq_process(mp->rxq, INT_MAX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001562 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001563 mv643xx_eth_free_completed_tx_descs(dev);
1564
1565 /*
1566 * If no real interrupt occured, exit.
1567 * This can happen when using gigE interrupt coalescing mechanism.
1568 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001569 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001570 return IRQ_NONE;
1571
1572 return IRQ_HANDLED;
1573}
1574
Lennert Buytenheke5371492008-06-01 02:18:13 +02001575static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001576{
1577 unsigned int phy_reg_data;
1578
1579 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001580 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001581 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001582 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001583
1584 /* wait for PHY to come out of reset */
1585 do {
1586 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001587 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001588 } while (phy_reg_data & 0x8000);
1589}
1590
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001591static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001592{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001593 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001594 u32 pscr;
1595 struct ethtool_cmd ethtool_cmd;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001596 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001598 /*
1599 * Configure basic link parameters.
1600 */
1601 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
1602 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
1603 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1604 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1605 DISABLE_AUTO_NEG_SPEED_GMII |
1606 DISABLE_AUTO_NEG_FOR_DUPLEX |
1607 DO_NOT_FORCE_LINK_FAIL |
1608 SERIAL_PORT_CONTROL_RESERVED;
1609 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1610 pscr |= SERIAL_PORT_ENABLE;
1611 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001612
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001613 wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
1614
1615 mv643xx_eth_get_settings(dev, &ethtool_cmd);
1616 phy_reset(mp);
1617 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001618
1619 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001620 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001621
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001622 /*
1623 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1624 * frames to RX queue #0.
1625 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001626 wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001627
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001628 /*
1629 * Treat BPDUs as normal multicasts, and disable partition mode.
1630 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001631 wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001632
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001633 /*
1634 * Enable the receive queue.
1635 */
1636 for (i = 0; i < 1; i++) {
1637 struct rx_queue *rxq = mp->rxq;
1638 int off = RXQ_CURRENT_DESC_PTR(mp->port_num);
1639 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001640
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001641 addr = (u32)rxq->rx_desc_dma;
1642 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
1643 wrl(mp, off, addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001644
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001645 rxq_enable(rxq);
1646 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001647
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001648
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001649 wrl(mp, TXQ_CURRENT_DESC_PTR(mp->port_num),
1650 (u32)((struct tx_desc *)mp->tx_desc_dma + mp->tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001651
1652 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001653 wrl(mp, TX_BW_MTU(mp->port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654}
1655
Lennert Buytenheke5371492008-06-01 02:18:13 +02001656#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001657static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001658 unsigned int delay)
1659{
1660 unsigned int port_num = mp->port_num;
1661 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1662
1663 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001664 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001665 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001666 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001667 & 0xffc000ff));
1668
1669 return coal;
1670}
1671#endif
1672
Lennert Buytenheke5371492008-06-01 02:18:13 +02001673static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001674 unsigned int delay)
1675{
1676 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1677
1678 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001679 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001680
1681 return coal;
1682}
1683
Lennert Buytenheke5371492008-06-01 02:18:13 +02001684static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001685{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001686 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001687
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001688 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001689}
1690
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001691static int mv643xx_eth_open(struct net_device *dev)
1692{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001693 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001694 unsigned int port_num = mp->port_num;
1695 unsigned int size;
1696 int err;
1697
1698 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001699 wrl(mp, INT_CAUSE(port_num), 0);
1700 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001701 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001702 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001703
1704 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1705 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1706 if (err) {
1707 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1708 return -EAGAIN;
1709 }
1710
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001711 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001712
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001713 err = rxq_init(mp);
1714 if (err)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001715 goto out_free_irq;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001716 rxq_refill(mp->rxq);
1717
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001718 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1719 GFP_KERNEL);
1720 if (!mp->tx_skb) {
1721 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
1722 err = -ENOMEM;
1723 goto out_free_rx_skb;
1724 }
1725
1726 /* Allocate TX ring */
1727 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001728 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001729 mp->tx_desc_area_size = size;
1730
1731 if (mp->tx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001732 mp->tx_desc_area = ioremap(mp->tx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001733 mp->tx_sram_size);
1734 mp->tx_desc_dma = mp->tx_sram_addr;
1735 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001736 mp->tx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001737 &mp->tx_desc_dma,
1738 GFP_KERNEL);
1739
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001740 if (!mp->tx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001741 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1742 dev->name, size);
1743 err = -ENOMEM;
1744 goto out_free_tx_skb;
1745 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001746 BUG_ON((u32) mp->tx_desc_area & 0xf); /* check 16-byte alignment */
1747 memset((void *)mp->tx_desc_area, 0, mp->tx_desc_area_size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001748
1749 ether_init_tx_desc_ring(mp);
1750
Lennert Buytenheke5371492008-06-01 02:18:13 +02001751#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001752 napi_enable(&mp->napi);
1753#endif
1754
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001755 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001756
1757 /* Interrupt Coalescing */
1758
Lennert Buytenheke5371492008-06-01 02:18:13 +02001759#ifdef MV643XX_ETH_COAL
1760 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761#endif
1762
Lennert Buytenheke5371492008-06-01 02:18:13 +02001763 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001764
1765 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001766 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001767
1768 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001769 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001770
1771 return 0;
1772
1773out_free_tx_skb:
1774 kfree(mp->tx_skb);
1775out_free_rx_skb:
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001776 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001777out_free_irq:
1778 free_irq(dev->irq, dev);
1779
1780 return err;
1781}
1782
Lennert Buytenheke5371492008-06-01 02:18:13 +02001783static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001784{
1785 unsigned int port_num = mp->port_num;
1786 unsigned int reg_data;
1787
1788 mv643xx_eth_port_disable_tx(mp);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001789 rxq_disable(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001790
1791 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001792 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001793
1794 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001795 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001796 reg_data &= ~(SERIAL_PORT_ENABLE |
1797 DO_NOT_FORCE_LINK_FAIL |
1798 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001799 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800}
1801
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001802static int mv643xx_eth_stop(struct net_device *dev)
1803{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001804 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001805 unsigned int port_num = mp->port_num;
1806
1807 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001808 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001809 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001810 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001811
Lennert Buytenheke5371492008-06-01 02:18:13 +02001812#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001813 napi_disable(&mp->napi);
1814#endif
1815 netif_carrier_off(dev);
1816 netif_stop_queue(dev);
1817
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001818 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001819
1820 mv643xx_eth_free_tx_rings(dev);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001821 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001822
1823 free_irq(dev->irq, dev);
1824
1825 return 0;
1826}
1827
1828static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1829{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001830 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001831
1832 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1833}
1834
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001835static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1836{
1837 if ((new_mtu > 9500) || (new_mtu < 64))
1838 return -EINVAL;
1839
1840 dev->mtu = new_mtu;
1841 if (!netif_running(dev))
1842 return 0;
1843
1844 /*
1845 * Stop and then re-open the interface. This will allocate RX
1846 * skbs of the new MTU.
1847 * There is a possible danger that the open will not succeed,
1848 * due to memory being full, which might fail the open function.
1849 */
1850 mv643xx_eth_stop(dev);
1851 if (mv643xx_eth_open(dev)) {
1852 printk(KERN_ERR "%s: Fatal error on opening device\n",
1853 dev->name);
1854 }
1855
1856 return 0;
1857}
1858
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001859static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1860{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001861 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001862 tx_timeout_task);
1863 struct net_device *dev = mp->dev;
1864
1865 if (!netif_running(dev))
1866 return;
1867
1868 netif_stop_queue(dev);
1869
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001870 port_reset(mp);
1871 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001872
1873 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1874 netif_wake_queue(dev);
1875}
1876
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001877static void mv643xx_eth_tx_timeout(struct net_device *dev)
1878{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001879 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001880
1881 printk(KERN_INFO "%s: TX timeout ", dev->name);
1882
1883 /* Do the reset outside of interrupt context */
1884 schedule_work(&mp->tx_timeout_task);
1885}
1886
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001887#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001888static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001889{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001890 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001891 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001892
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001893 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001894 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001895 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001896
Al Viro9da3b1a2006-10-08 15:00:44 +01001897 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001898
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001899 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001900}
1901#endif
1902
Lennert Buytenheke5371492008-06-01 02:18:13 +02001903static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001904{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001905 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001906 int val;
1907
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001908 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001909 return val;
1910}
1911
Lennert Buytenheke5371492008-06-01 02:18:13 +02001912static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001913{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001914 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001915 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001916}
1917
1918
1919/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001920static void
1921mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1922 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001923{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001924 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001925 u32 win_enable;
1926 u32 win_protect;
1927 int i;
1928
1929 for (i = 0; i < 6; i++) {
1930 writel(0, base + WINDOW_BASE(i));
1931 writel(0, base + WINDOW_SIZE(i));
1932 if (i < 4)
1933 writel(0, base + WINDOW_REMAP_HIGH(i));
1934 }
1935
1936 win_enable = 0x3f;
1937 win_protect = 0;
1938
1939 for (i = 0; i < dram->num_cs; i++) {
1940 struct mbus_dram_window *cs = dram->cs + i;
1941
1942 writel((cs->base & 0xffff0000) |
1943 (cs->mbus_attr << 8) |
1944 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1945 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1946
1947 win_enable &= ~(1 << i);
1948 win_protect |= 3 << (2 * i);
1949 }
1950
1951 writel(win_enable, base + WINDOW_BAR_ENABLE);
1952 msp->win_protect = win_protect;
1953}
1954
1955static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1956{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001957 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001958 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001959 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001960 struct resource *res;
1961 int ret;
1962
Lennert Buytenheke5371492008-06-01 02:18:13 +02001963 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001964 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1965
1966 ret = -EINVAL;
1967 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1968 if (res == NULL)
1969 goto out;
1970
1971 ret = -ENOMEM;
1972 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
1973 if (msp == NULL)
1974 goto out;
1975 memset(msp, 0, sizeof(*msp));
1976
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001977 msp->base = ioremap(res->start, res->end - res->start + 1);
1978 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001979 goto out_free;
1980
1981 spin_lock_init(&msp->phy_lock);
1982 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
1983
1984 platform_set_drvdata(pdev, msp);
1985
1986 /*
1987 * (Re-)program MBUS remapping windows if we are asked to.
1988 */
1989 if (pd != NULL && pd->dram != NULL)
1990 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
1991
1992 return 0;
1993
1994out_free:
1995 kfree(msp);
1996out:
1997 return ret;
1998}
1999
2000static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2001{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002002 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002003
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002004 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002005 kfree(msp);
2006
2007 return 0;
2008}
2009
2010static struct platform_driver mv643xx_eth_shared_driver = {
2011 .probe = mv643xx_eth_shared_probe,
2012 .remove = mv643xx_eth_shared_remove,
2013 .driver = {
2014 .name = MV643XX_ETH_SHARED_NAME,
2015 .owner = THIS_MODULE,
2016 },
2017};
2018
Lennert Buytenheke5371492008-06-01 02:18:13 +02002019static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002020{
2021 u32 reg_data;
2022 int addr_shift = 5 * mp->port_num;
2023
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002024 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002025 reg_data &= ~(0x1f << addr_shift);
2026 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002027 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002028}
2029
Lennert Buytenheke5371492008-06-01 02:18:13 +02002030static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002031{
2032 unsigned int reg_data;
2033
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002034 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002035
2036 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2037}
2038
Lennert Buytenheke5371492008-06-01 02:18:13 +02002039static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002040{
2041 unsigned int phy_reg_data0;
2042 int auto_neg;
2043
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002044 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002045 auto_neg = phy_reg_data0 & 0x1000;
2046 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002047 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002048
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002049 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002050 if ((phy_reg_data0 & 0x1000) == auto_neg)
2051 return -ENODEV; /* change didn't take */
2052
2053 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002054 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002055 return 0;
2056}
2057
James Chapmand0412d92006-01-27 01:15:30 -07002058static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2059 int speed, int duplex,
2060 struct ethtool_cmd *cmd)
2061{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002062 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002063
2064 memset(cmd, 0, sizeof(*cmd));
2065
2066 cmd->port = PORT_MII;
2067 cmd->transceiver = XCVR_INTERNAL;
2068 cmd->phy_address = phy_address;
2069
2070 if (speed == 0) {
2071 cmd->autoneg = AUTONEG_ENABLE;
2072 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2073 cmd->speed = SPEED_100;
2074 cmd->advertising = ADVERTISED_10baseT_Half |
2075 ADVERTISED_10baseT_Full |
2076 ADVERTISED_100baseT_Half |
2077 ADVERTISED_100baseT_Full;
2078 if (mp->mii.supports_gmii)
2079 cmd->advertising |= ADVERTISED_1000baseT_Full;
2080 } else {
2081 cmd->autoneg = AUTONEG_DISABLE;
2082 cmd->speed = speed;
2083 cmd->duplex = duplex;
2084 }
2085}
2086
Russell King3ae5eae2005-11-09 22:32:44 +00002087static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002090 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002091 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct net_device *dev;
2093 u8 *p;
2094 struct resource *res;
2095 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002096 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002097 int duplex = DUPLEX_HALF;
2098 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002099 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002101 pd = pdev->dev.platform_data;
2102 if (pd == NULL) {
2103 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2104 return -ENODEV;
2105 }
2106
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002107 if (pd->shared == NULL) {
2108 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2109 return -ENODEV;
2110 }
2111
Lennert Buytenheke5371492008-06-01 02:18:13 +02002112 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (!dev)
2114 return -ENOMEM;
2115
Russell King3ae5eae2005-11-09 22:32:44 +00002116 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002119 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002120#ifdef MV643XX_ETH_NAPI
2121 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2125 BUG_ON(!res);
2126 dev->irq = res->start;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 dev->open = mv643xx_eth_open;
2129 dev->stop = mv643xx_eth_stop;
2130 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 dev->set_mac_address = mv643xx_eth_set_mac_address;
2132 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2133
2134 /* No need to Tx Timeout */
2135 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002137#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002138 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002139#endif
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 dev->base_addr = 0;
2143 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002144 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002145 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Lennert Buytenheke5371492008-06-01 02:18:13 +02002147#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148#ifdef MAX_SKB_FRAGS
2149 /*
2150 * Zero copy can only work if we use Discovery II memory. Else, we will
2151 * have to map the buffers to ISA memory which is only 16 MB
2152 */
Wolfram Joost63890572006-01-16 16:57:41 -07002153 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154#endif
2155#endif
2156
2157 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002158 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 spin_lock_init(&mp->lock);
2161
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002162 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002163 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002164
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002165 if (mp->shared->win_protect)
2166 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2167
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002168 mp->shared_smi = mp->shared;
2169 if (pd->shared_smi != NULL)
2170 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002173 uc_addr_get(mp, dev->dev_addr);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002174 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002176 if (is_valid_ether_addr(pd->mac_addr))
2177 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002179 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002180 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002182 mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002183 if (pd->rx_queue_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002184 mp->default_rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002186 if (pd->tx_queue_size)
2187 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002189 if (pd->tx_sram_size) {
2190 mp->tx_sram_size = pd->tx_sram_size;
2191 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002194 if (pd->rx_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002195 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2196 mp->rx_desc_sram_size = pd->rx_sram_size;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002197 }
2198
2199 duplex = pd->duplex;
2200 speed = pd->speed;
2201
James Chapmanc28a4f82006-01-27 01:13:15 -07002202 /* Hook up MII support for ethtool */
2203 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002204 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2205 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002206 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002207 mp->mii.phy_id_mask = 0x3f;
2208 mp->mii.reg_num_mask = 0x1f;
2209
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002210 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002212 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002213 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002217 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002218 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002219 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2220 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002221 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002222
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002223 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 err = register_netdev(dev);
2225 if (err)
2226 goto out;
2227
2228 p = dev->dev_addr;
2229 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002230 "%s: port %d with MAC address %s\n",
2231 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if (dev->features & NETIF_F_SG)
2234 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2235
2236 if (dev->features & NETIF_F_IP_CSUM)
2237 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2238 dev->name);
2239
Lennert Buytenheke5371492008-06-01 02:18:13 +02002240#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2242#endif
2243
Lennert Buytenheke5371492008-06-01 02:18:13 +02002244#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2246 dev->name);
2247#endif
2248
Lennert Buytenheke5371492008-06-01 02:18:13 +02002249#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2251#endif
2252
Nicolas DETb1529872005-10-28 17:46:30 -07002253 if (mp->tx_sram_size > 0)
2254 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
2257
2258out:
2259 free_netdev(dev);
2260
2261 return err;
2262}
2263
Russell King3ae5eae2005-11-09 22:32:44 +00002264static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Russell King3ae5eae2005-11-09 22:32:44 +00002266 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 unregister_netdev(dev);
2269 flush_scheduled_work();
2270
2271 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002272 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return 0;
2274}
2275
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002276static void mv643xx_eth_shutdown(struct platform_device *pdev)
2277{
2278 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002279 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002280 unsigned int port_num = mp->port_num;
2281
2282 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002283 wrl(mp, INT_MASK(port_num), 0);
2284 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002285
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002286 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002287}
2288
Russell King3ae5eae2005-11-09 22:32:44 +00002289static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .probe = mv643xx_eth_probe,
2291 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002292 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002293 .driver = {
2294 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002295 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002296 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297};
2298
Lennert Buytenheke5371492008-06-01 02:18:13 +02002299static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 int rc;
2302
Russell King3ae5eae2005-11-09 22:32:44 +00002303 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002305 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002307 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 return rc;
2310}
2311
Lennert Buytenheke5371492008-06-01 02:18:13 +02002312static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Russell King3ae5eae2005-11-09 22:32:44 +00002314 platform_driver_unregister(&mv643xx_eth_driver);
2315 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Lennert Buytenheke5371492008-06-01 02:18:13 +02002318module_init(mv643xx_eth_init_module);
2319module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321MODULE_LICENSE("GPL");
2322MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2323 " and Dale Farnsworth");
2324MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002325MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2326MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);