blob: 84e6c41becdea71e14d969a7d564958800cb854e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenheke5371492008-06-01 02:18:13 +020057static char mv643xx_eth_driver_name[] = "mv643xx_eth";
58static char mv643xx_eth_driver_version[] = "1.0";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020059
Lennert Buytenheke5371492008-06-01 02:18:13 +020060#define MV643XX_ETH_CHECKSUM_OFFLOAD_TX
61#define MV643XX_ETH_NAPI
62#define MV643XX_ETH_TX_FAST_REFILL
63#undef MV643XX_ETH_COAL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020064
Lennert Buytenheke5371492008-06-01 02:18:13 +020065#define MV643XX_ETH_TX_COAL 100
66#ifdef MV643XX_ETH_COAL
67#define MV643XX_ETH_RX_COAL 100
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020068#endif
69
Lennert Buytenheke5371492008-06-01 02:18:13 +020070#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020071#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020099#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
101#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
102#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
103#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
104#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
105#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200106#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200107#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
108#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
109#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200110#define INT_RX 0x00000804
111#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200113#define INT_EXT_LINK 0x00100000
114#define INT_EXT_PHY 0x00010000
115#define INT_EXT_TX_ERROR_0 0x00000100
116#define INT_EXT_TX_0 0x00000001
117#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200118#define INT_MASK(p) (0x0468 + ((p) << 10))
119#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
120#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
121#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
122#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
123#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
124#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
125#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
126#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
127#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200129
130/*
131 * SDMA configuration register.
132 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200134#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200136#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200137
138#if defined(__BIG_ENDIAN)
139#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
140 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#elif defined(__LITTLE_ENDIAN)
143#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
144 RX_BURST_SIZE_4_64BIT | \
145 BLM_RX_NO_SWAP | \
146 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147 TX_BURST_SIZE_4_64BIT
148#else
149#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
150#endif
151
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200152
153/*
154 * Port serial control register.
155 */
156#define SET_MII_SPEED_TO_100 (1 << 24)
157#define SET_GMII_SPEED_TO_1000 (1 << 23)
158#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200159#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200160#define MAX_RX_PACKET_9700BYTE (5 << 17)
161#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200162#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
163#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
164#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
165#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
166#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
167#define FORCE_LINK_PASS (1 << 1)
168#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200169
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200170#define DEFAULT_RX_QUEUE_SIZE 400
171#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200174#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
175#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
176#define SMI_OPCODE_WRITE 0 /* Completion of Read */
177#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200178
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200180/*
181 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200182 */
183#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200184struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200185 u16 byte_cnt; /* Descriptor buffer byte count */
186 u16 buf_size; /* Buffer size */
187 u32 cmd_sts; /* Descriptor command status */
188 u32 next_desc_ptr; /* Next descriptor pointer */
189 u32 buf_ptr; /* Descriptor buffer pointer */
190};
191
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200192struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200193 u16 byte_cnt; /* buffer byte count */
194 u16 l4i_chk; /* CPU provided TCP checksum */
195 u32 cmd_sts; /* Command/status field */
196 u32 next_desc_ptr; /* Pointer to next descriptor */
197 u32 buf_ptr; /* pointer to buffer for this descriptor*/
198};
199#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200200struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200201 u32 cmd_sts; /* Descriptor command status */
202 u16 buf_size; /* Buffer size */
203 u16 byte_cnt; /* Descriptor buffer byte count */
204 u32 buf_ptr; /* Descriptor buffer pointer */
205 u32 next_desc_ptr; /* Next descriptor pointer */
206};
207
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200208struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200209 u32 cmd_sts; /* Command/status field */
210 u16 l4i_chk; /* CPU provided TCP checksum */
211 u16 byte_cnt; /* buffer byte count */
212 u32 buf_ptr; /* pointer to buffer for this descriptor*/
213 u32 next_desc_ptr; /* Pointer to next descriptor */
214};
215#else
216#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
217#endif
218
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200219/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200220#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200221
222/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200223#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200224
225/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200226#define LAYER_4_CHECKSUM_OK 0x40000000
227#define RX_ENABLE_INTERRUPT 0x20000000
228#define RX_FIRST_DESC 0x08000000
229#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200230
231/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200232#define TX_ENABLE_INTERRUPT 0x00800000
233#define GEN_CRC 0x00400000
234#define TX_FIRST_DESC 0x00200000
235#define TX_LAST_DESC 0x00100000
236#define ZERO_PADDING 0x00080000
237#define GEN_IP_V4_CHECKSUM 0x00040000
238#define GEN_TCP_UDP_CHECKSUM 0x00020000
239#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200240
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200241#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200242
243
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200244/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200245struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200246 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200247
248 /* used to protect SMI_REG, which is shared across ports */
249 spinlock_t phy_lock;
250
251 u32 win_protect;
252
253 unsigned int t_clk;
254};
255
256
257/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200258struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200259 u64 good_octets_received;
260 u32 bad_octets_received;
261 u32 internal_mac_transmit_err;
262 u32 good_frames_received;
263 u32 bad_frames_received;
264 u32 broadcast_frames_received;
265 u32 multicast_frames_received;
266 u32 frames_64_octets;
267 u32 frames_65_to_127_octets;
268 u32 frames_128_to_255_octets;
269 u32 frames_256_to_511_octets;
270 u32 frames_512_to_1023_octets;
271 u32 frames_1024_to_max_octets;
272 u64 good_octets_sent;
273 u32 good_frames_sent;
274 u32 excessive_collision;
275 u32 multicast_frames_sent;
276 u32 broadcast_frames_sent;
277 u32 unrec_mac_control_received;
278 u32 fc_sent;
279 u32 good_fc_received;
280 u32 bad_fc_received;
281 u32 undersize_received;
282 u32 fragments_received;
283 u32 oversize_received;
284 u32 jabber_received;
285 u32 mac_receive_error;
286 u32 bad_crc_event;
287 u32 collision;
288 u32 late_collision;
289};
290
Lennert Buytenheke5371492008-06-01 02:18:13 +0200291struct mv643xx_eth_private {
292 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200293 int port_num; /* User Ethernet port number */
294
Lennert Buytenheke5371492008-06-01 02:18:13 +0200295 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200296
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200297 u32 rx_sram_addr; /* Base address of rx sram area */
298 u32 rx_sram_size; /* Size of rx sram area */
299 u32 tx_sram_addr; /* Base address of tx sram area */
300 u32 tx_sram_size; /* Size of tx sram area */
301
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200302 /* Tx/Rx rings managment indexes fields. For driver use */
303
304 /* Next available and first returning Rx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200305 int rx_curr_desc, rx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200306
307 /* Next available and first returning Tx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200308 int tx_curr_desc, tx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200309
Lennert Buytenheke5371492008-06-01 02:18:13 +0200310#ifdef MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200311 u32 tx_clean_threshold;
312#endif
313
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200314 struct rx_desc *rx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200315 dma_addr_t rx_desc_dma;
316 int rx_desc_area_size;
317 struct sk_buff **rx_skb;
318
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200319 struct tx_desc *tx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200320 dma_addr_t tx_desc_dma;
321 int tx_desc_area_size;
322 struct sk_buff **tx_skb;
323
324 struct work_struct tx_timeout_task;
325
326 struct net_device *dev;
327 struct napi_struct napi;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200328 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200329 spinlock_t lock;
330 /* Size of Tx Ring per queue */
331 int tx_ring_size;
332 /* Number of tx descriptors in use */
333 int tx_desc_count;
334 /* Size of Rx Ring per queue */
335 int rx_ring_size;
336 /* Number of rx descriptors in use */
337 int rx_desc_count;
338
339 /*
340 * Used in case RX Ring is empty, which can be caused when
341 * system does not have resources (skb's)
342 */
343 struct timer_list timeout;
344
345 u32 rx_int_coal;
346 u32 tx_int_coal;
347 struct mii_if_info mii;
348};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200350
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200351/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200352static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200354 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Lennert Buytenheke5371492008-06-01 02:18:13 +0200357static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200359 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200362
363/* rxq/txq helper functions *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200364static void mv643xx_eth_port_enable_rx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200365 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200367 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Lennert Buytenheke5371492008-06-01 02:18:13 +0200370static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200371{
372 unsigned int port_num = mp->port_num;
373 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700374
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200375 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200376 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200377 if (queues) {
378 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200379 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200380
381 /* Wait for all Rx activity to terminate. */
382 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200383 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200384 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200387 return queues;
388}
389
Lennert Buytenheke5371492008-06-01 02:18:13 +0200390static void mv643xx_eth_port_enable_tx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200391 unsigned int queues)
392{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200393 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200394}
395
Lennert Buytenheke5371492008-06-01 02:18:13 +0200396static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200397{
398 unsigned int port_num = mp->port_num;
399 u32 queues;
400
401 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200402 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200403 if (queues) {
404 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200405 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200406
407 /* Wait for all Tx activity to terminate. */
408 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200409 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200410 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200411
412 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200413 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200414 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200415 }
416
417 return queues;
418}
419
420
421/* rx ***********************************************************************/
422static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
423
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200424static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200425{
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200426 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200427 unsigned long flags;
428
429 spin_lock_irqsave(&mp->lock, flags);
430
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700431 while (mp->rx_desc_count < mp->rx_ring_size) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200432 struct sk_buff *skb;
433 int unaligned;
434 int rx;
435
Ralf Baechle908b6372007-02-26 19:52:06 +0000436 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200437 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200439
Ralf Baechle908b6372007-02-26 19:52:06 +0000440 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700441 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000442 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200443
444 mp->rx_desc_count++;
445 rx = mp->rx_used_desc;
446 mp->rx_used_desc = (rx + 1) % mp->rx_ring_size;
447
448 mp->rx_desc_area[rx].buf_ptr = dma_map_single(NULL,
449 skb->data,
450 ETH_RX_SKB_SIZE,
451 DMA_FROM_DEVICE);
452 mp->rx_desc_area[rx].buf_size = ETH_RX_SKB_SIZE;
453 mp->rx_skb[rx] = skb;
454 wmb();
455 mp->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
456 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
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700462 if (mp->rx_desc_count == 0) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200463 mp->timeout.expires = jiffies + (HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 add_timer(&mp->timeout);
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
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700470static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700472 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200477 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700478 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned int received_packets = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Lennert Buytenhek96587662008-06-01 10:31:56 +0200481 while (budget-- > 0) {
482 struct sk_buff *skb;
483 volatile struct rx_desc *rx_desc;
484 unsigned int cmd_sts;
485 unsigned long flags;
486
487 spin_lock_irqsave(&mp->lock, flags);
488
489 rx_desc = &mp->rx_desc_area[mp->rx_curr_desc];
490
491 cmd_sts = rx_desc->cmd_sts;
492 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
493 spin_unlock_irqrestore(&mp->lock, flags);
494 break;
495 }
496 rmb();
497
498 skb = mp->rx_skb[mp->rx_curr_desc];
499 mp->rx_skb[mp->rx_curr_desc] = NULL;
500
501 mp->rx_curr_desc = (mp->rx_curr_desc + 1) % mp->rx_ring_size;
502
503 spin_unlock_irqrestore(&mp->lock, flags);
504
505 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
506 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700507 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700509
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700510 /*
511 * Update statistics.
512 * Note byte count includes 4 byte CRC count
513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200515 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * In case received a packet without first / last bits on OR
519 * the error summary bit is on, the packets needs to be dropeed.
520 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200521 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200522 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200523 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200525 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200526 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (net_ratelimit())
528 printk(KERN_ERR
529 "%s: Received packet spread "
530 "on multiple descriptors\n",
531 dev->name);
532 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200533 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 stats->rx_errors++;
535
536 dev_kfree_skb_irq(skb);
537 } else {
538 /*
539 * The -4 is for the CRC in the trailer of the
540 * received packet
541 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200542 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Lennert Buytenhek96587662008-06-01 10:31:56 +0200544 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 skb->ip_summed = CHECKSUM_UNNECESSARY;
546 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200547 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 skb->protocol = eth_type_trans(skb, dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200550#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 netif_receive_skb(skb);
552#else
553 netif_rx(skb);
554#endif
555 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700556 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700558 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return received_packets;
561}
562
Lennert Buytenheke5371492008-06-01 02:18:13 +0200563#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200564static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200566 struct mv643xx_eth_private *mp = container_of(napi, struct mv643xx_eth_private, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700567 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700569 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Lennert Buytenheke5371492008-06-01 02:18:13 +0200571#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700573 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576#endif
577
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700578 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200579 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200580 != (u32) mp->rx_used_desc)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700581 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700583 if (work_done < budget) {
584 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200585 wrl(mp, INT_CAUSE(port_num), 0);
586 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200587 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700590 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592#endif
593
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200594
595/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700596static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
597{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700598 unsigned int frag;
599 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700600
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700601 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
602 fragp = &skb_shinfo(skb)->frags[frag];
603 if (fragp->size <= 8 && fragp->page_offset & 0x7)
604 return 1;
605 }
606 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700607}
608
Lennert Buytenheke5371492008-06-01 02:18:13 +0200609static int alloc_tx_desc_index(struct mv643xx_eth_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700610{
611 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700612
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700613 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700614
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200615 tx_desc_curr = mp->tx_curr_desc;
616 mp->tx_curr_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700617
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200618 BUG_ON(mp->tx_curr_desc == mp->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700619
620 return tx_desc_curr;
621}
622
Lennert Buytenheke5371492008-06-01 02:18:13 +0200623static void tx_fill_frag_descs(struct mv643xx_eth_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700624 struct sk_buff *skb)
625{
626 int frag;
627 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200628 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700629
630 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
631 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
632
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200633 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200634 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700635
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200636 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700637 /* Last Frag enables interrupt and frees the skb */
638 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200639 desc->cmd_sts |= ZERO_PADDING |
640 TX_LAST_DESC |
641 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700642 mp->tx_skb[tx_index] = skb;
643 } else
Al Viro05980772006-05-30 23:59:09 -0400644 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700645
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200646 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700647 desc->l4i_chk = 0;
648 desc->byte_cnt = this_frag->size;
649 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
650 this_frag->page_offset,
651 this_frag->size,
652 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700653 }
654}
655
Byron Bradley324ff2c2008-02-04 23:47:15 -0800656static inline __be16 sum16_as_be(__sum16 sum)
657{
658 return (__force __be16)sum;
659}
660
Lennert Buytenheke5371492008-06-01 02:18:13 +0200661static void tx_submit_descs_for_skb(struct mv643xx_eth_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700662 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700663{
664 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200665 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700666 u32 cmd_sts;
667 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700668 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700669
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200670 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700671
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200672 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200673 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700674
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700675 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200676 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700677
678 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400679 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700680 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200681 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700682 length = skb->len;
683 mp->tx_skb[tx_index] = skb;
684 }
685
686 desc->byte_cnt = length;
687 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700688
Patrick McHardy84fa7932006-08-29 16:44:56 -0700689 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800690 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700691
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200692 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
693 GEN_IP_V4_CHECKSUM |
694 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700695
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700696 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700697 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200698 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800699 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700700 break;
701 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800702 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700703 break;
704 default:
705 BUG();
706 }
707 } else {
708 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200709 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700710 desc->l4i_chk = 0;
711 }
712
713 /* ensure all other descriptors are written before first cmd_sts */
714 wmb();
715 desc->cmd_sts = cmd_sts;
716
717 /* ensure all descriptors are written before poking hardware */
718 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200719 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700721 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
725{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200726 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700727 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700730 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700731
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700732 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
733 stats->tx_dropped++;
734 printk(KERN_DEBUG "%s: failed to linearize tiny "
735 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700736 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_lock_irqsave(&mp->lock, flags);
740
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700741 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
742 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
743 netif_stop_queue(dev);
744 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700745 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700746 }
747
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200748 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700749 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 stats->tx_packets++;
751 dev->trans_start = jiffies;
752
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700753 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
754 netif_stop_queue(dev);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock_irqrestore(&mp->lock, flags);
757
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700758 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200761
762/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200763static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200764
Lennert Buytenheke5371492008-06-01 02:18:13 +0200765static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200766 unsigned int phy_reg, unsigned int *value)
767{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200768 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
769 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200770 unsigned long flags;
771 int i;
772
773 /* the SMI register is a shared resource */
774 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
775
776 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200777 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200778 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200779 printk("%s: PHY busy timeout\n", mp->dev->name);
780 goto out;
781 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200782 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200783 }
784
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200785 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200786
787 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200788 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200789 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200790 printk("%s: PHY read timeout\n", mp->dev->name);
791 goto out;
792 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200793 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200794 }
795
796 *value = readl(smi_reg) & 0xffff;
797out:
798 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
799}
800
Lennert Buytenheke5371492008-06-01 02:18:13 +0200801static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200802 unsigned int phy_reg, unsigned int value)
803{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200804 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
805 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200806 unsigned long flags;
807 int i;
808
809 /* the SMI register is a shared resource */
810 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
811
812 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200813 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200814 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200815 printk("%s: PHY busy timeout\n", mp->dev->name);
816 goto out;
817 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200818 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200819 }
820
821 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200822 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200823out:
824 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
825}
826
827
828/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200829static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200830{
831 unsigned int port_num = mp->port_num;
832 int i;
833
834 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200835 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200836 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200837}
838
Lennert Buytenheke5371492008-06-01 02:18:13 +0200839static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200840{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200841 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200842}
843
Lennert Buytenheke5371492008-06-01 02:18:13 +0200844static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200845{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200846 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200847
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200848 p->good_octets_received += read_mib(mp, 0x00);
849 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
850 p->bad_octets_received += read_mib(mp, 0x08);
851 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
852 p->good_frames_received += read_mib(mp, 0x10);
853 p->bad_frames_received += read_mib(mp, 0x14);
854 p->broadcast_frames_received += read_mib(mp, 0x18);
855 p->multicast_frames_received += read_mib(mp, 0x1c);
856 p->frames_64_octets += read_mib(mp, 0x20);
857 p->frames_65_to_127_octets += read_mib(mp, 0x24);
858 p->frames_128_to_255_octets += read_mib(mp, 0x28);
859 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
860 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
861 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
862 p->good_octets_sent += read_mib(mp, 0x38);
863 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
864 p->good_frames_sent += read_mib(mp, 0x40);
865 p->excessive_collision += read_mib(mp, 0x44);
866 p->multicast_frames_sent += read_mib(mp, 0x48);
867 p->broadcast_frames_sent += read_mib(mp, 0x4c);
868 p->unrec_mac_control_received += read_mib(mp, 0x50);
869 p->fc_sent += read_mib(mp, 0x54);
870 p->good_fc_received += read_mib(mp, 0x58);
871 p->bad_fc_received += read_mib(mp, 0x5c);
872 p->undersize_received += read_mib(mp, 0x60);
873 p->fragments_received += read_mib(mp, 0x64);
874 p->oversize_received += read_mib(mp, 0x68);
875 p->jabber_received += read_mib(mp, 0x6c);
876 p->mac_receive_error += read_mib(mp, 0x70);
877 p->bad_crc_event += read_mib(mp, 0x74);
878 p->collision += read_mib(mp, 0x78);
879 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200880}
881
882
883/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200884struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200885 char stat_string[ETH_GSTRING_LEN];
886 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +0200887 int netdev_off;
888 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200889};
890
Lennert Buytenhek16820052008-06-01 11:40:29 +0200891#define SSTAT(m) \
892 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
893 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200894
Lennert Buytenhek16820052008-06-01 11:40:29 +0200895#define MIBSTAT(m) \
896 { #m, FIELD_SIZEOF(struct mib_counters, m), \
897 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
898
899static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
900 SSTAT(rx_packets),
901 SSTAT(tx_packets),
902 SSTAT(rx_bytes),
903 SSTAT(tx_bytes),
904 SSTAT(rx_errors),
905 SSTAT(tx_errors),
906 SSTAT(rx_dropped),
907 SSTAT(tx_dropped),
908 MIBSTAT(good_octets_received),
909 MIBSTAT(bad_octets_received),
910 MIBSTAT(internal_mac_transmit_err),
911 MIBSTAT(good_frames_received),
912 MIBSTAT(bad_frames_received),
913 MIBSTAT(broadcast_frames_received),
914 MIBSTAT(multicast_frames_received),
915 MIBSTAT(frames_64_octets),
916 MIBSTAT(frames_65_to_127_octets),
917 MIBSTAT(frames_128_to_255_octets),
918 MIBSTAT(frames_256_to_511_octets),
919 MIBSTAT(frames_512_to_1023_octets),
920 MIBSTAT(frames_1024_to_max_octets),
921 MIBSTAT(good_octets_sent),
922 MIBSTAT(good_frames_sent),
923 MIBSTAT(excessive_collision),
924 MIBSTAT(multicast_frames_sent),
925 MIBSTAT(broadcast_frames_sent),
926 MIBSTAT(unrec_mac_control_received),
927 MIBSTAT(fc_sent),
928 MIBSTAT(good_fc_received),
929 MIBSTAT(bad_fc_received),
930 MIBSTAT(undersize_received),
931 MIBSTAT(fragments_received),
932 MIBSTAT(oversize_received),
933 MIBSTAT(jabber_received),
934 MIBSTAT(mac_receive_error),
935 MIBSTAT(bad_crc_event),
936 MIBSTAT(collision),
937 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200938};
939
Lennert Buytenheke5371492008-06-01 02:18:13 +0200940static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200941{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200942 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200943 int err;
944
945 spin_lock_irq(&mp->lock);
946 err = mii_ethtool_gset(&mp->mii, cmd);
947 spin_unlock_irq(&mp->lock);
948
949 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
950 cmd->supported &= ~SUPPORTED_1000baseT_Half;
951 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
952
953 return err;
954}
955
Lennert Buytenheke5371492008-06-01 02:18:13 +0200956static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200957{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200958 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200959 int err;
960
961 spin_lock_irq(&mp->lock);
962 err = mii_ethtool_sset(&mp->mii, cmd);
963 spin_unlock_irq(&mp->lock);
964
965 return err;
966}
967
Lennert Buytenheke5371492008-06-01 02:18:13 +0200968static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200969 struct ethtool_drvinfo *drvinfo)
970{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200971 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
972 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200973 strncpy(drvinfo->fw_version, "N/A", 32);
974 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +0200975 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200976}
977
978static int mv643xx_eth_nway_restart(struct net_device *dev)
979{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200980 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200981
982 return mii_nway_restart(&mp->mii);
983}
984
985static u32 mv643xx_eth_get_link(struct net_device *dev)
986{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200987 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200988
989 return mii_link_ok(&mp->mii);
990}
991
Lennert Buytenheke5371492008-06-01 02:18:13 +0200992static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200993 uint8_t *data)
994{
995 int i;
996
997 switch(stringset) {
998 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +0200999 for (i=0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001000 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001001 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001002 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001003 }
1004 break;
1005 }
1006}
1007
Lennert Buytenheke5371492008-06-01 02:18:13 +02001008static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001009 struct ethtool_stats *stats, uint64_t *data)
1010{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001011 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001012 int i;
1013
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001014 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001015
Lennert Buytenhek16820052008-06-01 11:40:29 +02001016 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1017 const struct mv643xx_eth_stats *stat;
1018 void *p;
1019
1020 stat = mv643xx_eth_stats + i;
1021
1022 if (stat->netdev_off >= 0)
1023 p = ((void *)mp->dev) + stat->netdev_off;
1024 else
1025 p = ((void *)mp) + stat->mp_off;
1026
1027 data[i] = (stat->sizeof_stat == 8) ?
1028 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001029 }
1030}
1031
Lennert Buytenheke5371492008-06-01 02:18:13 +02001032static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001033{
1034 switch (sset) {
1035 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001036 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001037 default:
1038 return -EOPNOTSUPP;
1039 }
1040}
1041
Lennert Buytenheke5371492008-06-01 02:18:13 +02001042static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1043 .get_settings = mv643xx_eth_get_settings,
1044 .set_settings = mv643xx_eth_set_settings,
1045 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001046 .get_link = mv643xx_eth_get_link,
1047 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001048 .get_sset_count = mv643xx_eth_get_sset_count,
1049 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1050 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001051 .nway_reset = mv643xx_eth_nway_restart,
1052};
1053
1054
1055/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001056static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001057{
1058 unsigned int port_num = mp->port_num;
1059 unsigned int mac_h;
1060 unsigned int mac_l;
1061
1062 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1063 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1064
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001065 addr[0] = (mac_h >> 24) & 0xff;
1066 addr[1] = (mac_h >> 16) & 0xff;
1067 addr[2] = (mac_h >> 8) & 0xff;
1068 addr[3] = mac_h & 0xff;
1069 addr[4] = (mac_l >> 8) & 0xff;
1070 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001071}
1072
Lennert Buytenheke5371492008-06-01 02:18:13 +02001073static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001074{
1075 unsigned int port_num = mp->port_num;
1076 int table_index;
1077
1078 /* Clear DA filter unicast table (Ex_dFUT) */
1079 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001080 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001081
1082 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1083 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001084 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001085 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001086 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001087 }
1088}
1089
Lennert Buytenheke5371492008-06-01 02:18:13 +02001090static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001091 int table, unsigned char entry)
1092{
1093 unsigned int table_reg;
1094 unsigned int tbl_offset;
1095 unsigned int reg_offset;
1096
1097 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1098 reg_offset = entry % 4; /* Entry offset within the register */
1099
1100 /* Set "accepts frame bit" at specified table entry */
1101 table_reg = rdl(mp, table + tbl_offset);
1102 table_reg |= 0x01 << (8 * reg_offset);
1103 wrl(mp, table + tbl_offset, table_reg);
1104}
1105
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001106static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001107{
1108 unsigned int port_num = mp->port_num;
1109 unsigned int mac_h;
1110 unsigned int mac_l;
1111 int table;
1112
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001113 mac_l = (addr[4] << 8) | (addr[5]);
1114 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1115 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001116
1117 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1118 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1119
1120 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001121 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001122 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001123}
1124
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001125static void mv643xx_eth_update_mac_address(struct net_device *dev)
1126{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001127 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001128
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001129 init_mac_tables(mp);
1130 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131}
1132
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001133static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1134{
1135 int i;
1136
1137 for (i = 0; i < 6; i++)
1138 /* +2 is for the offset of the HW addr type */
1139 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1140 mv643xx_eth_update_mac_address(dev);
1141 return 0;
1142}
1143
Lennert Buytenhek69876562008-06-01 11:43:32 +02001144static int addr_crc(unsigned char *addr)
1145{
1146 int crc = 0;
1147 int i;
1148
1149 for (i = 0; i < 6; i++) {
1150 int j;
1151
1152 crc = (crc ^ addr[i]) << 8;
1153 for (j = 7; j >= 0; j--) {
1154 if (crc & (0x100 << j))
1155 crc ^= 0x107 << j;
1156 }
1157 }
1158
1159 return crc;
1160}
1161
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001162static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001163{
1164 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001165 int table;
Lennert Buytenhek69876562008-06-01 11:43:32 +02001166 int crc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001167
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001168 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1169 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001170 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001171 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001172 return;
1173 }
1174
Lennert Buytenhek69876562008-06-01 11:43:32 +02001175 crc = addr_crc(addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001176
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001177 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhek69876562008-06-01 11:43:32 +02001178 set_filter_table_entry(mp, table, crc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001179}
1180
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001181static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001182{
1183
1184 struct dev_mc_list *mc_list;
1185 int i;
1186 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001187 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001188 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001189
1190 /* If the device is in promiscuous mode or in all multicast mode,
1191 * we will fully populate both multicast tables with accept.
1192 * This is guaranteed to yield a match on all multicast addresses...
1193 */
1194 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1195 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1196 /* Set all entries in DA filter special multicast
1197 * table (Ex_dFSMT)
1198 * Set for ETH_Q0 for now
1199 * Bits
1200 * 0 Accept=1, Drop=0
1201 * 3-1 Queue ETH_Q0=0
1202 * 7-4 Reserved = 0;
1203 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001204 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001205
1206 /* Set all entries in DA filter other multicast
1207 * table (Ex_dFOMT)
1208 * Set for ETH_Q0 for now
1209 * Bits
1210 * 0 Accept=1, Drop=0
1211 * 3-1 Queue ETH_Q0=0
1212 * 7-4 Reserved = 0;
1213 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001214 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001215 }
1216 return;
1217 }
1218
1219 /* We will clear out multicast tables every time we get the list.
1220 * Then add the entire new list...
1221 */
1222 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1223 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001224 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001225
1226 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001227 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001228 }
1229
1230 /* Get pointer to net_device multicast list and add each one... */
1231 for (i = 0, mc_list = dev->mc_list;
1232 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1233 i++, mc_list = mc_list->next)
1234 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001235 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001236}
1237
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001238static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1239{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001240 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001241 u32 config_reg;
1242
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001243 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001244 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001245 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001246 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001247 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001248 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001249
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001250 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001251}
1252
1253
1254/* rx/tx queue initialisation ***********************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001255static void ether_init_rx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001256{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001257 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258 int rx_desc_num = mp->rx_ring_size;
1259 int i;
1260
1261 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001262 p_rx_desc = (struct rx_desc *)mp->rx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001263 for (i = 0; i < rx_desc_num; i++) {
1264 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001265 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001266 }
1267
1268 /* Save Rx desc pointer to driver struct. */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001269 mp->rx_curr_desc = 0;
1270 mp->rx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001271
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001272 mp->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001273}
1274
1275static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1276{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001277 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001278 int curr;
1279
1280 /* Stop RX Queues */
1281 mv643xx_eth_port_disable_rx(mp);
1282
1283 /* Free preallocated skb's on RX rings */
1284 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1285 if (mp->rx_skb[curr]) {
1286 dev_kfree_skb(mp->rx_skb[curr]);
1287 mp->rx_desc_count--;
1288 }
1289 }
1290
1291 if (mp->rx_desc_count)
1292 printk(KERN_ERR
1293 "%s: Error in freeing Rx Ring. %d skb's still"
1294 " stuck in RX Ring - ignoring them\n", dev->name,
1295 mp->rx_desc_count);
1296 /* Free RX ring */
1297 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001298 iounmap(mp->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001299 else
1300 dma_free_coherent(NULL, mp->rx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001301 mp->rx_desc_area, mp->rx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001302}
1303
Lennert Buytenheke5371492008-06-01 02:18:13 +02001304static void ether_init_tx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001305{
1306 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001307 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001308 int i;
1309
1310 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001311 p_tx_desc = (struct tx_desc *)mp->tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001312 for (i = 0; i < tx_desc_num; i++) {
1313 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001314 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001315 }
1316
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001317 mp->tx_curr_desc = 0;
1318 mp->tx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001319
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001320 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001321}
1322
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001323static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1324{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001325 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001326 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001327 u32 cmd_sts;
1328 struct sk_buff *skb;
1329 unsigned long flags;
1330 int tx_index;
1331 dma_addr_t addr;
1332 int count;
1333 int released = 0;
1334
1335 while (mp->tx_desc_count > 0) {
1336 spin_lock_irqsave(&mp->lock, flags);
1337
1338 /* tx_desc_count might have changed before acquiring the lock */
1339 if (mp->tx_desc_count <= 0) {
1340 spin_unlock_irqrestore(&mp->lock, flags);
1341 return released;
1342 }
1343
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001344 tx_index = mp->tx_used_desc;
1345 desc = &mp->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001346 cmd_sts = desc->cmd_sts;
1347
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001348 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349 spin_unlock_irqrestore(&mp->lock, flags);
1350 return released;
1351 }
1352
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001353 mp->tx_used_desc = (tx_index + 1) % mp->tx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001354 mp->tx_desc_count--;
1355
1356 addr = desc->buf_ptr;
1357 count = desc->byte_cnt;
1358 skb = mp->tx_skb[tx_index];
1359 if (skb)
1360 mp->tx_skb[tx_index] = NULL;
1361
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001362 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001363 printk("%s: Error in TX\n", dev->name);
1364 dev->stats.tx_errors++;
1365 }
1366
1367 spin_unlock_irqrestore(&mp->lock, flags);
1368
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001369 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001370 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1371 else
1372 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1373
1374 if (skb)
1375 dev_kfree_skb_irq(skb);
1376
1377 released = 1;
1378 }
1379
1380 return released;
1381}
1382
1383static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1384{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001385 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001386
1387 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1388 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1389 netif_wake_queue(dev);
1390}
1391
1392static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1393{
1394 mv643xx_eth_free_tx_descs(dev, 1);
1395}
1396
1397static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1398{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001399 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001400
1401 /* Stop Tx Queues */
1402 mv643xx_eth_port_disable_tx(mp);
1403
1404 /* Free outstanding skb's on TX ring */
1405 mv643xx_eth_free_all_tx_descs(dev);
1406
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001407 BUG_ON(mp->tx_used_desc != mp->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001408
1409 /* Free TX ring */
1410 if (mp->tx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001411 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001412 else
1413 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001414 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001415}
1416
1417
1418/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001419static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001420
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001421static void mv643xx_eth_update_pscr(struct net_device *dev,
1422 struct ethtool_cmd *ecmd)
1423{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001424 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001425 int port_num = mp->port_num;
1426 u32 o_pscr, n_pscr;
1427 unsigned int queues;
1428
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001429 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001430 n_pscr = o_pscr;
1431
1432 /* clear speed, duplex and rx buffer size fields */
1433 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1434 SET_GMII_SPEED_TO_1000 |
1435 SET_FULL_DUPLEX_MODE |
1436 MAX_RX_PACKET_MASK);
1437
1438 if (ecmd->duplex == DUPLEX_FULL)
1439 n_pscr |= SET_FULL_DUPLEX_MODE;
1440
1441 if (ecmd->speed == SPEED_1000)
1442 n_pscr |= SET_GMII_SPEED_TO_1000 |
1443 MAX_RX_PACKET_9700BYTE;
1444 else {
1445 if (ecmd->speed == SPEED_100)
1446 n_pscr |= SET_MII_SPEED_TO_100;
1447 n_pscr |= MAX_RX_PACKET_1522BYTE;
1448 }
1449
1450 if (n_pscr != o_pscr) {
1451 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001452 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001453 else {
1454 queues = mv643xx_eth_port_disable_tx(mp);
1455
1456 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001457 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1458 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1459 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001460 if (queues)
1461 mv643xx_eth_port_enable_tx(mp, queues);
1462 }
1463 }
1464}
1465
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001466static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1467{
1468 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001469 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001470 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001471 unsigned int port_num = mp->port_num;
1472
1473 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001474 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1475 if (int_cause & INT_EXT) {
1476 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001477 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001478 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001479 }
1480
1481 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001482 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483 struct ethtool_cmd cmd;
1484
1485 if (mii_link_ok(&mp->mii)) {
1486 mii_ethtool_gset(&mp->mii, &cmd);
1487 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001488 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489 if (!netif_carrier_ok(dev)) {
1490 netif_carrier_on(dev);
1491 if (mp->tx_ring_size - mp->tx_desc_count >=
1492 MAX_DESCS_PER_SKB)
1493 netif_wake_queue(dev);
1494 }
1495 } else if (netif_carrier_ok(dev)) {
1496 netif_stop_queue(dev);
1497 netif_carrier_off(dev);
1498 }
1499 }
1500
Lennert Buytenheke5371492008-06-01 02:18:13 +02001501#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001502 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001503 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001504 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001505
1506 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001507 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001508
1509 netif_rx_schedule(dev, &mp->napi);
1510 }
1511#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001512 if (int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001513 mv643xx_eth_receive_queue(dev, INT_MAX);
1514#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001515 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001516 mv643xx_eth_free_completed_tx_descs(dev);
1517
1518 /*
1519 * If no real interrupt occured, exit.
1520 * This can happen when using gigE interrupt coalescing mechanism.
1521 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001522 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001523 return IRQ_NONE;
1524
1525 return IRQ_HANDLED;
1526}
1527
Lennert Buytenheke5371492008-06-01 02:18:13 +02001528static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001529{
1530 unsigned int phy_reg_data;
1531
1532 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001533 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001534 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001535 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001536
1537 /* wait for PHY to come out of reset */
1538 do {
1539 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001540 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001541 } while (phy_reg_data & 0x8000);
1542}
1543
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001544static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001545{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001546 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001547 unsigned int port_num = mp->port_num;
1548 int tx_curr_desc, rx_curr_desc;
1549 u32 pscr;
1550 struct ethtool_cmd ethtool_cmd;
1551
1552 /* Assignment of Tx CTRP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001553 tx_curr_desc = mp->tx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001554 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001555 (u32)((struct tx_desc *)mp->tx_desc_dma + tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001556
1557 /* Assignment of Rx CRDP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001558 rx_curr_desc = mp->rx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001559 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001560 (u32)((struct rx_desc *)mp->rx_desc_dma + rx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561
1562 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001563 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001564
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001565 /*
1566 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1567 * frames to RX queue #0.
1568 */
1569 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001570
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001571 /*
1572 * Treat BPDUs as normal multicasts, and disable partition mode.
1573 */
1574 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001575
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001576 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001577
1578 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001579 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001580
1581 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1582 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02001583 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001584 DO_NOT_FORCE_LINK_FAIL |
1585 SERIAL_PORT_CONTROL_RESERVED;
1586
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001587 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001588
1589 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001590 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001591
1592 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001593 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001594
1595 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001596 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597
1598 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001599 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001600
1601 /* save phy settings across reset */
Lennert Buytenheke5371492008-06-01 02:18:13 +02001602 mv643xx_eth_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001603 phy_reset(mp);
Lennert Buytenheke5371492008-06-01 02:18:13 +02001604 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001605}
1606
Lennert Buytenheke5371492008-06-01 02:18:13 +02001607#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001608static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001609 unsigned int delay)
1610{
1611 unsigned int port_num = mp->port_num;
1612 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1613
1614 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001615 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001616 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001617 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001618 & 0xffc000ff));
1619
1620 return coal;
1621}
1622#endif
1623
Lennert Buytenheke5371492008-06-01 02:18:13 +02001624static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001625 unsigned int delay)
1626{
1627 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1628
1629 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001630 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001631
1632 return coal;
1633}
1634
Lennert Buytenheke5371492008-06-01 02:18:13 +02001635static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001636{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001637 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001638
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001639 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001640}
1641
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001642static int mv643xx_eth_open(struct net_device *dev)
1643{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001644 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001645 unsigned int port_num = mp->port_num;
1646 unsigned int size;
1647 int err;
1648
1649 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001650 wrl(mp, INT_CAUSE(port_num), 0);
1651 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001652 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001653 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654
1655 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1656 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1657 if (err) {
1658 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1659 return -EAGAIN;
1660 }
1661
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001662 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001663
1664 memset(&mp->timeout, 0, sizeof(struct timer_list));
1665 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
1666 mp->timeout.data = (unsigned long)dev;
1667
1668 /* Allocate RX and TX skb rings */
1669 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1670 GFP_KERNEL);
1671 if (!mp->rx_skb) {
1672 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
1673 err = -ENOMEM;
1674 goto out_free_irq;
1675 }
1676 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1677 GFP_KERNEL);
1678 if (!mp->tx_skb) {
1679 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
1680 err = -ENOMEM;
1681 goto out_free_rx_skb;
1682 }
1683
1684 /* Allocate TX ring */
1685 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001686 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001687 mp->tx_desc_area_size = size;
1688
1689 if (mp->tx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001690 mp->tx_desc_area = ioremap(mp->tx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001691 mp->tx_sram_size);
1692 mp->tx_desc_dma = mp->tx_sram_addr;
1693 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001694 mp->tx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001695 &mp->tx_desc_dma,
1696 GFP_KERNEL);
1697
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001698 if (!mp->tx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001699 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1700 dev->name, size);
1701 err = -ENOMEM;
1702 goto out_free_tx_skb;
1703 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001704 BUG_ON((u32) mp->tx_desc_area & 0xf); /* check 16-byte alignment */
1705 memset((void *)mp->tx_desc_area, 0, mp->tx_desc_area_size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001706
1707 ether_init_tx_desc_ring(mp);
1708
1709 /* Allocate RX ring */
1710 mp->rx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001711 size = mp->rx_ring_size * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001712 mp->rx_desc_area_size = size;
1713
1714 if (mp->rx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001715 mp->rx_desc_area = ioremap(mp->rx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001716 mp->rx_sram_size);
1717 mp->rx_desc_dma = mp->rx_sram_addr;
1718 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001719 mp->rx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001720 &mp->rx_desc_dma,
1721 GFP_KERNEL);
1722
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001723 if (!mp->rx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001724 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1725 dev->name, size);
1726 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1727 dev->name);
1728 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001729 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001730 else
1731 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001732 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001733 err = -ENOMEM;
1734 goto out_free_tx_skb;
1735 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001736 memset((void *)mp->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001737
1738 ether_init_rx_desc_ring(mp);
1739
1740 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1741
Lennert Buytenheke5371492008-06-01 02:18:13 +02001742#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001743 napi_enable(&mp->napi);
1744#endif
1745
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001746 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001747
1748 /* Interrupt Coalescing */
1749
Lennert Buytenheke5371492008-06-01 02:18:13 +02001750#ifdef MV643XX_ETH_COAL
1751 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001752#endif
1753
Lennert Buytenheke5371492008-06-01 02:18:13 +02001754 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001755
1756 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001757 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758
1759 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001760 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761
1762 return 0;
1763
1764out_free_tx_skb:
1765 kfree(mp->tx_skb);
1766out_free_rx_skb:
1767 kfree(mp->rx_skb);
1768out_free_irq:
1769 free_irq(dev->irq, dev);
1770
1771 return err;
1772}
1773
Lennert Buytenheke5371492008-06-01 02:18:13 +02001774static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001775{
1776 unsigned int port_num = mp->port_num;
1777 unsigned int reg_data;
1778
1779 mv643xx_eth_port_disable_tx(mp);
1780 mv643xx_eth_port_disable_rx(mp);
1781
1782 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001783 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001784
1785 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001786 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001787 reg_data &= ~(SERIAL_PORT_ENABLE |
1788 DO_NOT_FORCE_LINK_FAIL |
1789 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001790 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001791}
1792
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001793static int mv643xx_eth_stop(struct net_device *dev)
1794{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001795 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001796 unsigned int port_num = mp->port_num;
1797
1798 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001799 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001801 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001802
Lennert Buytenheke5371492008-06-01 02:18:13 +02001803#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001804 napi_disable(&mp->napi);
1805#endif
1806 netif_carrier_off(dev);
1807 netif_stop_queue(dev);
1808
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001809 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001810
1811 mv643xx_eth_free_tx_rings(dev);
1812 mv643xx_eth_free_rx_rings(dev);
1813
1814 free_irq(dev->irq, dev);
1815
1816 return 0;
1817}
1818
1819static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1820{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001821 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001822
1823 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1824}
1825
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001826static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1827{
1828 if ((new_mtu > 9500) || (new_mtu < 64))
1829 return -EINVAL;
1830
1831 dev->mtu = new_mtu;
1832 if (!netif_running(dev))
1833 return 0;
1834
1835 /*
1836 * Stop and then re-open the interface. This will allocate RX
1837 * skbs of the new MTU.
1838 * There is a possible danger that the open will not succeed,
1839 * due to memory being full, which might fail the open function.
1840 */
1841 mv643xx_eth_stop(dev);
1842 if (mv643xx_eth_open(dev)) {
1843 printk(KERN_ERR "%s: Fatal error on opening device\n",
1844 dev->name);
1845 }
1846
1847 return 0;
1848}
1849
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001850static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1851{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001852 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001853 tx_timeout_task);
1854 struct net_device *dev = mp->dev;
1855
1856 if (!netif_running(dev))
1857 return;
1858
1859 netif_stop_queue(dev);
1860
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001861 port_reset(mp);
1862 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001863
1864 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1865 netif_wake_queue(dev);
1866}
1867
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001868static void mv643xx_eth_tx_timeout(struct net_device *dev)
1869{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001870 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001871
1872 printk(KERN_INFO "%s: TX timeout ", dev->name);
1873
1874 /* Do the reset outside of interrupt context */
1875 schedule_work(&mp->tx_timeout_task);
1876}
1877
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001878#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001879static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001880{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001881 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001882 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001883
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001884 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001885 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001886 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001887
Al Viro9da3b1a2006-10-08 15:00:44 +01001888 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001889
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001890 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001891}
1892#endif
1893
Lennert Buytenheke5371492008-06-01 02:18:13 +02001894static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001895{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001896 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001897 int val;
1898
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001899 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001900 return val;
1901}
1902
Lennert Buytenheke5371492008-06-01 02:18:13 +02001903static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
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 Buytenhekcc9754b2008-06-01 02:10:27 +02001906 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001907}
1908
1909
1910/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001911static void
1912mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1913 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001914{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001915 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001916 u32 win_enable;
1917 u32 win_protect;
1918 int i;
1919
1920 for (i = 0; i < 6; i++) {
1921 writel(0, base + WINDOW_BASE(i));
1922 writel(0, base + WINDOW_SIZE(i));
1923 if (i < 4)
1924 writel(0, base + WINDOW_REMAP_HIGH(i));
1925 }
1926
1927 win_enable = 0x3f;
1928 win_protect = 0;
1929
1930 for (i = 0; i < dram->num_cs; i++) {
1931 struct mbus_dram_window *cs = dram->cs + i;
1932
1933 writel((cs->base & 0xffff0000) |
1934 (cs->mbus_attr << 8) |
1935 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1936 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1937
1938 win_enable &= ~(1 << i);
1939 win_protect |= 3 << (2 * i);
1940 }
1941
1942 writel(win_enable, base + WINDOW_BAR_ENABLE);
1943 msp->win_protect = win_protect;
1944}
1945
1946static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1947{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001948 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001949 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001950 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001951 struct resource *res;
1952 int ret;
1953
Lennert Buytenheke5371492008-06-01 02:18:13 +02001954 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001955 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1956
1957 ret = -EINVAL;
1958 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1959 if (res == NULL)
1960 goto out;
1961
1962 ret = -ENOMEM;
1963 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
1964 if (msp == NULL)
1965 goto out;
1966 memset(msp, 0, sizeof(*msp));
1967
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001968 msp->base = ioremap(res->start, res->end - res->start + 1);
1969 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001970 goto out_free;
1971
1972 spin_lock_init(&msp->phy_lock);
1973 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
1974
1975 platform_set_drvdata(pdev, msp);
1976
1977 /*
1978 * (Re-)program MBUS remapping windows if we are asked to.
1979 */
1980 if (pd != NULL && pd->dram != NULL)
1981 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
1982
1983 return 0;
1984
1985out_free:
1986 kfree(msp);
1987out:
1988 return ret;
1989}
1990
1991static int mv643xx_eth_shared_remove(struct platform_device *pdev)
1992{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001993 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001994
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001995 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001996 kfree(msp);
1997
1998 return 0;
1999}
2000
2001static struct platform_driver mv643xx_eth_shared_driver = {
2002 .probe = mv643xx_eth_shared_probe,
2003 .remove = mv643xx_eth_shared_remove,
2004 .driver = {
2005 .name = MV643XX_ETH_SHARED_NAME,
2006 .owner = THIS_MODULE,
2007 },
2008};
2009
Lennert Buytenheke5371492008-06-01 02:18:13 +02002010static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002011{
2012 u32 reg_data;
2013 int addr_shift = 5 * mp->port_num;
2014
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002015 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002016 reg_data &= ~(0x1f << addr_shift);
2017 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002018 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002019}
2020
Lennert Buytenheke5371492008-06-01 02:18:13 +02002021static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002022{
2023 unsigned int reg_data;
2024
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002025 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002026
2027 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2028}
2029
Lennert Buytenheke5371492008-06-01 02:18:13 +02002030static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002031{
2032 unsigned int phy_reg_data0;
2033 int auto_neg;
2034
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002035 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002036 auto_neg = phy_reg_data0 & 0x1000;
2037 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002038 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002039
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002040 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002041 if ((phy_reg_data0 & 0x1000) == auto_neg)
2042 return -ENODEV; /* change didn't take */
2043
2044 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002045 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002046 return 0;
2047}
2048
James Chapmand0412d92006-01-27 01:15:30 -07002049static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2050 int speed, int duplex,
2051 struct ethtool_cmd *cmd)
2052{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002053 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002054
2055 memset(cmd, 0, sizeof(*cmd));
2056
2057 cmd->port = PORT_MII;
2058 cmd->transceiver = XCVR_INTERNAL;
2059 cmd->phy_address = phy_address;
2060
2061 if (speed == 0) {
2062 cmd->autoneg = AUTONEG_ENABLE;
2063 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2064 cmd->speed = SPEED_100;
2065 cmd->advertising = ADVERTISED_10baseT_Half |
2066 ADVERTISED_10baseT_Full |
2067 ADVERTISED_100baseT_Half |
2068 ADVERTISED_100baseT_Full;
2069 if (mp->mii.supports_gmii)
2070 cmd->advertising |= ADVERTISED_1000baseT_Full;
2071 } else {
2072 cmd->autoneg = AUTONEG_DISABLE;
2073 cmd->speed = speed;
2074 cmd->duplex = duplex;
2075 }
2076}
2077
Russell King3ae5eae2005-11-09 22:32:44 +00002078static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002081 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002082 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct net_device *dev;
2084 u8 *p;
2085 struct resource *res;
2086 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002087 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002088 int duplex = DUPLEX_HALF;
2089 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002090 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002092 pd = pdev->dev.platform_data;
2093 if (pd == NULL) {
2094 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2095 return -ENODEV;
2096 }
2097
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002098 if (pd->shared == NULL) {
2099 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2100 return -ENODEV;
2101 }
2102
Lennert Buytenheke5371492008-06-01 02:18:13 +02002103 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (!dev)
2105 return -ENOMEM;
2106
Russell King3ae5eae2005-11-09 22:32:44 +00002107 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002110 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002111#ifdef MV643XX_ETH_NAPI
2112 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2116 BUG_ON(!res);
2117 dev->irq = res->start;
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 dev->open = mv643xx_eth_open;
2120 dev->stop = mv643xx_eth_stop;
2121 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 dev->set_mac_address = mv643xx_eth_set_mac_address;
2123 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2124
2125 /* No need to Tx Timeout */
2126 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002128#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002129 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002130#endif
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 dev->base_addr = 0;
2134 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002135 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002136 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Lennert Buytenheke5371492008-06-01 02:18:13 +02002138#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#ifdef MAX_SKB_FRAGS
2140 /*
2141 * Zero copy can only work if we use Discovery II memory. Else, we will
2142 * have to map the buffers to ISA memory which is only 16 MB
2143 */
Wolfram Joost63890572006-01-16 16:57:41 -07002144 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145#endif
2146#endif
2147
2148 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002149 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 spin_lock_init(&mp->lock);
2152
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002153 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002154 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002155
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002156 if (mp->shared->win_protect)
2157 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2158
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002159 mp->shared_smi = mp->shared;
2160 if (pd->shared_smi != NULL)
2161 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002164 uc_addr_get(mp, dev->dev_addr);
2165 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2166 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002168 if (is_valid_ether_addr(pd->mac_addr))
2169 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002171 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002172 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002174 if (pd->rx_queue_size)
2175 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002177 if (pd->tx_queue_size)
2178 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002180 if (pd->tx_sram_size) {
2181 mp->tx_sram_size = pd->tx_sram_size;
2182 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002185 if (pd->rx_sram_size) {
2186 mp->rx_sram_size = pd->rx_sram_size;
2187 mp->rx_sram_addr = pd->rx_sram_addr;
2188 }
2189
2190 duplex = pd->duplex;
2191 speed = pd->speed;
2192
James Chapmanc28a4f82006-01-27 01:13:15 -07002193 /* Hook up MII support for ethtool */
2194 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002195 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2196 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002197 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002198 mp->mii.phy_id_mask = 0x3f;
2199 mp->mii.reg_num_mask = 0x1f;
2200
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002201 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002203 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002204 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002208 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002209 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002210 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2211 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002212 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002213
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002214 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 err = register_netdev(dev);
2216 if (err)
2217 goto out;
2218
2219 p = dev->dev_addr;
2220 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002221 "%s: port %d with MAC address %s\n",
2222 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (dev->features & NETIF_F_SG)
2225 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2226
2227 if (dev->features & NETIF_F_IP_CSUM)
2228 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2229 dev->name);
2230
Lennert Buytenheke5371492008-06-01 02:18:13 +02002231#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2233#endif
2234
Lennert Buytenheke5371492008-06-01 02:18:13 +02002235#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2237 dev->name);
2238#endif
2239
Lennert Buytenheke5371492008-06-01 02:18:13 +02002240#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2242#endif
2243
Nicolas DETb1529872005-10-28 17:46:30 -07002244 if (mp->tx_sram_size > 0)
2245 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return 0;
2248
2249out:
2250 free_netdev(dev);
2251
2252 return err;
2253}
2254
Russell King3ae5eae2005-11-09 22:32:44 +00002255static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Russell King3ae5eae2005-11-09 22:32:44 +00002257 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 unregister_netdev(dev);
2260 flush_scheduled_work();
2261
2262 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002263 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return 0;
2265}
2266
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002267static void mv643xx_eth_shutdown(struct platform_device *pdev)
2268{
2269 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002270 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002271 unsigned int port_num = mp->port_num;
2272
2273 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002274 wrl(mp, INT_MASK(port_num), 0);
2275 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002276
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002277 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002278}
2279
Russell King3ae5eae2005-11-09 22:32:44 +00002280static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 .probe = mv643xx_eth_probe,
2282 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002283 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002284 .driver = {
2285 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002286 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002287 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288};
2289
Lennert Buytenheke5371492008-06-01 02:18:13 +02002290static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int rc;
2293
Russell King3ae5eae2005-11-09 22:32:44 +00002294 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002296 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002298 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 return rc;
2301}
2302
Lennert Buytenheke5371492008-06-01 02:18:13 +02002303static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Russell King3ae5eae2005-11-09 22:32:44 +00002305 platform_driver_unregister(&mv643xx_eth_driver);
2306 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Lennert Buytenheke5371492008-06-01 02:18:13 +02002309module_init(mv643xx_eth_init_module);
2310module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312MODULE_LICENSE("GPL");
2313MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2314 " and Dale Farnsworth");
2315MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002316MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2317MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);