xref: /openbmc/linux/drivers/net/ethernet/marvell/mv643xx_eth.c (revision 785bf6f7904352242d187ff6087d523a4ce1b3ac)
1527a6266SJeff Kirsher /*
2527a6266SJeff Kirsher  * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
3527a6266SJeff Kirsher  * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4527a6266SJeff Kirsher  *
5527a6266SJeff Kirsher  * Based on the 64360 driver from:
6527a6266SJeff Kirsher  * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7527a6266SJeff Kirsher  *		      Rabeeh Khoury <rabeeh@marvell.com>
8527a6266SJeff Kirsher  *
9527a6266SJeff Kirsher  * Copyright (C) 2003 PMC-Sierra, Inc.,
10527a6266SJeff Kirsher  *	written by Manish Lachwani
11527a6266SJeff Kirsher  *
12527a6266SJeff Kirsher  * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13527a6266SJeff Kirsher  *
14527a6266SJeff Kirsher  * Copyright (C) 2004-2006 MontaVista Software, Inc.
15527a6266SJeff Kirsher  *			   Dale Farnsworth <dale@farnsworth.org>
16527a6266SJeff Kirsher  *
17527a6266SJeff Kirsher  * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18527a6266SJeff Kirsher  *				     <sjhill@realitydiluted.com>
19527a6266SJeff Kirsher  *
20527a6266SJeff Kirsher  * Copyright (C) 2007-2008 Marvell Semiconductor
21527a6266SJeff Kirsher  *			   Lennert Buytenhek <buytenh@marvell.com>
22527a6266SJeff Kirsher  *
233871c387SMichael Stapelberg  * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
243871c387SMichael Stapelberg  *
25527a6266SJeff Kirsher  * This program is free software; you can redistribute it and/or
26527a6266SJeff Kirsher  * modify it under the terms of the GNU General Public License
27527a6266SJeff Kirsher  * as published by the Free Software Foundation; either version 2
28527a6266SJeff Kirsher  * of the License, or (at your option) any later version.
29527a6266SJeff Kirsher  *
30527a6266SJeff Kirsher  * This program is distributed in the hope that it will be useful,
31527a6266SJeff Kirsher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32527a6266SJeff Kirsher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33527a6266SJeff Kirsher  * GNU General Public License for more details.
34527a6266SJeff Kirsher  *
35527a6266SJeff Kirsher  * You should have received a copy of the GNU General Public License
36527a6266SJeff Kirsher  * along with this program; if not, write to the Free Software
37527a6266SJeff Kirsher  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38527a6266SJeff Kirsher  */
39527a6266SJeff Kirsher 
40527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41527a6266SJeff Kirsher 
42527a6266SJeff Kirsher #include <linux/init.h>
43527a6266SJeff Kirsher #include <linux/dma-mapping.h>
44527a6266SJeff Kirsher #include <linux/in.h>
45527a6266SJeff Kirsher #include <linux/ip.h>
46527a6266SJeff Kirsher #include <linux/tcp.h>
47527a6266SJeff Kirsher #include <linux/udp.h>
48527a6266SJeff Kirsher #include <linux/etherdevice.h>
49527a6266SJeff Kirsher #include <linux/delay.h>
50527a6266SJeff Kirsher #include <linux/ethtool.h>
51527a6266SJeff Kirsher #include <linux/platform_device.h>
52527a6266SJeff Kirsher #include <linux/module.h>
53527a6266SJeff Kirsher #include <linux/kernel.h>
54527a6266SJeff Kirsher #include <linux/spinlock.h>
55527a6266SJeff Kirsher #include <linux/workqueue.h>
56527a6266SJeff Kirsher #include <linux/phy.h>
57527a6266SJeff Kirsher #include <linux/mv643xx_eth.h>
58527a6266SJeff Kirsher #include <linux/io.h>
593619eb85SSebastian Hesselbarth #include <linux/interrupt.h>
60527a6266SJeff Kirsher #include <linux/types.h>
61527a6266SJeff Kirsher #include <linux/slab.h>
62452503ebSAndrew Lunn #include <linux/clk.h>
6376723bcaSSebastian Hesselbarth #include <linux/of.h>
6476723bcaSSebastian Hesselbarth #include <linux/of_irq.h>
6576723bcaSSebastian Hesselbarth #include <linux/of_net.h>
66cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h>
67527a6266SJeff Kirsher 
68527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth";
69527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4";
70527a6266SJeff Kirsher 
71527a6266SJeff Kirsher 
72527a6266SJeff Kirsher /*
73527a6266SJeff Kirsher  * Registers shared between all ports.
74527a6266SJeff Kirsher  */
75527a6266SJeff Kirsher #define PHY_ADDR			0x0000
76527a6266SJeff Kirsher #define WINDOW_BASE(w)			(0x0200 + ((w) << 3))
77527a6266SJeff Kirsher #define WINDOW_SIZE(w)			(0x0204 + ((w) << 3))
78527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w)		(0x0280 + ((w) << 2))
79527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE		0x0290
80527a6266SJeff Kirsher #define WINDOW_PROTECT(w)		(0x0294 + ((w) << 4))
81527a6266SJeff Kirsher 
82527a6266SJeff Kirsher /*
83527a6266SJeff Kirsher  * Main per-port registers.  These live at offset 0x0400 for
84527a6266SJeff Kirsher  * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
85527a6266SJeff Kirsher  */
86527a6266SJeff Kirsher #define PORT_CONFIG			0x0000
87527a6266SJeff Kirsher #define  UNICAST_PROMISCUOUS_MODE	0x00000001
88527a6266SJeff Kirsher #define PORT_CONFIG_EXT			0x0004
89527a6266SJeff Kirsher #define MAC_ADDR_LOW			0x0014
90527a6266SJeff Kirsher #define MAC_ADDR_HIGH			0x0018
91527a6266SJeff Kirsher #define SDMA_CONFIG			0x001c
92527a6266SJeff Kirsher #define  TX_BURST_SIZE_16_64BIT		0x01000000
93527a6266SJeff Kirsher #define  TX_BURST_SIZE_4_64BIT		0x00800000
94527a6266SJeff Kirsher #define  BLM_TX_NO_SWAP			0x00000020
95527a6266SJeff Kirsher #define  BLM_RX_NO_SWAP			0x00000010
96527a6266SJeff Kirsher #define  RX_BURST_SIZE_16_64BIT		0x00000008
97527a6266SJeff Kirsher #define  RX_BURST_SIZE_4_64BIT		0x00000004
98527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL		0x003c
99527a6266SJeff Kirsher #define  SET_MII_SPEED_TO_100		0x01000000
100527a6266SJeff Kirsher #define  SET_GMII_SPEED_TO_1000		0x00800000
101527a6266SJeff Kirsher #define  SET_FULL_DUPLEX_MODE		0x00200000
102527a6266SJeff Kirsher #define  MAX_RX_PACKET_9700BYTE		0x000a0000
103527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_SPEED_GMII	0x00002000
104527a6266SJeff Kirsher #define  DO_NOT_FORCE_LINK_FAIL		0x00000400
105527a6266SJeff Kirsher #define  SERIAL_PORT_CONTROL_RESERVED	0x00000200
106527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_FLOW_CTRL	0x00000008
107527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_DUPLEX	0x00000004
108527a6266SJeff Kirsher #define  FORCE_LINK_PASS		0x00000002
109527a6266SJeff Kirsher #define  SERIAL_PORT_ENABLE		0x00000001
110527a6266SJeff Kirsher #define PORT_STATUS			0x0044
111527a6266SJeff Kirsher #define  TX_FIFO_EMPTY			0x00000400
112527a6266SJeff Kirsher #define  TX_IN_PROGRESS			0x00000080
113527a6266SJeff Kirsher #define  PORT_SPEED_MASK		0x00000030
114527a6266SJeff Kirsher #define  PORT_SPEED_1000		0x00000010
115527a6266SJeff Kirsher #define  PORT_SPEED_100			0x00000020
116527a6266SJeff Kirsher #define  PORT_SPEED_10			0x00000000
117527a6266SJeff Kirsher #define  FLOW_CONTROL_ENABLED		0x00000008
118527a6266SJeff Kirsher #define  FULL_DUPLEX			0x00000004
119527a6266SJeff Kirsher #define  LINK_UP			0x00000002
120527a6266SJeff Kirsher #define TXQ_COMMAND			0x0048
121527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF		0x004c
122cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1		0x004c
123cb85215fSSebastian Hesselbarth #define  CLK125_BYPASS_EN		0x00000010
124527a6266SJeff Kirsher #define TX_BW_RATE			0x0050
125527a6266SJeff Kirsher #define TX_BW_MTU			0x0058
126527a6266SJeff Kirsher #define TX_BW_BURST			0x005c
127527a6266SJeff Kirsher #define INT_CAUSE			0x0060
128527a6266SJeff Kirsher #define  INT_TX_END			0x07f80000
129527a6266SJeff Kirsher #define  INT_TX_END_0			0x00080000
130527a6266SJeff Kirsher #define  INT_RX				0x000003fc
131527a6266SJeff Kirsher #define  INT_RX_0			0x00000004
132527a6266SJeff Kirsher #define  INT_EXT			0x00000002
133527a6266SJeff Kirsher #define INT_CAUSE_EXT			0x0064
134527a6266SJeff Kirsher #define  INT_EXT_LINK_PHY		0x00110000
135527a6266SJeff Kirsher #define  INT_EXT_TX			0x000000ff
136527a6266SJeff Kirsher #define INT_MASK			0x0068
137527a6266SJeff Kirsher #define INT_MASK_EXT			0x006c
138527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD	0x0074
139302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT		0x0084
140302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT		0x0088
141527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED		0x00dc
142527a6266SJeff Kirsher #define TX_BW_RATE_MOVED		0x00e0
143527a6266SJeff Kirsher #define TX_BW_MTU_MOVED			0x00e8
144527a6266SJeff Kirsher #define TX_BW_BURST_MOVED		0x00ec
145527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q)		(0x020c + ((q) << 4))
146527a6266SJeff Kirsher #define RXQ_COMMAND			0x0280
147527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q)		(0x02c0 + ((q) << 2))
148527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q)		(0x0300 + ((q) << 4))
149527a6266SJeff Kirsher #define TXQ_BW_CONF(q)			(0x0304 + ((q) << 4))
150527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q)		(0x0308 + ((q) << 4))
151527a6266SJeff Kirsher 
152527a6266SJeff Kirsher /*
153527a6266SJeff Kirsher  * Misc per-port registers.
154527a6266SJeff Kirsher  */
155527a6266SJeff Kirsher #define MIB_COUNTERS(p)			(0x1000 + ((p) << 7))
156527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p)		(0x1400 + ((p) << 10))
157527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p)		(0x1500 + ((p) << 10))
158527a6266SJeff Kirsher #define UNICAST_TABLE(p)		(0x1600 + ((p) << 10))
159527a6266SJeff Kirsher 
160527a6266SJeff Kirsher 
161527a6266SJeff Kirsher /*
162527a6266SJeff Kirsher  * SDMA configuration register default value.
163527a6266SJeff Kirsher  */
164527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
165527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
166527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
167527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
168527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
169527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
170527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
171527a6266SJeff Kirsher 		 BLM_RX_NO_SWAP		|	\
172527a6266SJeff Kirsher 		 BLM_TX_NO_SWAP		|	\
173527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
174527a6266SJeff Kirsher #else
175527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
176527a6266SJeff Kirsher #endif
177527a6266SJeff Kirsher 
178527a6266SJeff Kirsher 
179527a6266SJeff Kirsher /*
180527a6266SJeff Kirsher  * Misc definitions.
181527a6266SJeff Kirsher  */
182527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE	128
183527a6266SJeff Kirsher #define DEFAULT_TX_QUEUE_SIZE	256
184527a6266SJeff Kirsher #define SKB_DMA_REALIGN		((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
185527a6266SJeff Kirsher 
186527a6266SJeff Kirsher 
187527a6266SJeff Kirsher /*
188527a6266SJeff Kirsher  * RX/TX descriptors.
189527a6266SJeff Kirsher  */
190527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
191527a6266SJeff Kirsher struct rx_desc {
192527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
193527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
194527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
195527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
196527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
197527a6266SJeff Kirsher };
198527a6266SJeff Kirsher 
199527a6266SJeff Kirsher struct tx_desc {
200527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
201527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
202527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
203527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
204527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
205527a6266SJeff Kirsher };
206527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
207527a6266SJeff Kirsher struct rx_desc {
208527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
209527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
210527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
211527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
212527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
213527a6266SJeff Kirsher };
214527a6266SJeff Kirsher 
215527a6266SJeff Kirsher struct tx_desc {
216527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
217527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
218527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
219527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
220527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
221527a6266SJeff Kirsher };
222527a6266SJeff Kirsher #else
223527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
224527a6266SJeff Kirsher #endif
225527a6266SJeff Kirsher 
226527a6266SJeff Kirsher /* RX & TX descriptor command */
227527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA		0x80000000
228527a6266SJeff Kirsher 
229527a6266SJeff Kirsher /* RX & TX descriptor status */
230527a6266SJeff Kirsher #define ERROR_SUMMARY			0x00000001
231527a6266SJeff Kirsher 
232527a6266SJeff Kirsher /* RX descriptor status */
233527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK		0x40000000
234527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT		0x20000000
235527a6266SJeff Kirsher #define RX_FIRST_DESC			0x08000000
236527a6266SJeff Kirsher #define RX_LAST_DESC			0x04000000
237527a6266SJeff Kirsher #define RX_IP_HDR_OK			0x02000000
238527a6266SJeff Kirsher #define RX_PKT_IS_IPV4			0x01000000
239527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2		0x00800000
240527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK		0x00600000
241527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4	0x00000000
242527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED		0x00080000
243527a6266SJeff Kirsher 
244527a6266SJeff Kirsher /* TX descriptor command */
245527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT		0x00800000
246527a6266SJeff Kirsher #define GEN_CRC				0x00400000
247527a6266SJeff Kirsher #define TX_FIRST_DESC			0x00200000
248527a6266SJeff Kirsher #define TX_LAST_DESC			0x00100000
249527a6266SJeff Kirsher #define ZERO_PADDING			0x00080000
250527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM		0x00040000
251527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM		0x00020000
252527a6266SJeff Kirsher #define UDP_FRAME			0x00010000
253527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES		0x00008000
254527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES		0x00000200
255527a6266SJeff Kirsher 
256527a6266SJeff Kirsher #define TX_IHL_SHIFT			11
257527a6266SJeff Kirsher 
258527a6266SJeff Kirsher 
259527a6266SJeff Kirsher /* global *******************************************************************/
260527a6266SJeff Kirsher struct mv643xx_eth_shared_private {
261527a6266SJeff Kirsher 	/*
262527a6266SJeff Kirsher 	 * Ethernet controller base address.
263527a6266SJeff Kirsher 	 */
264527a6266SJeff Kirsher 	void __iomem *base;
265527a6266SJeff Kirsher 
266527a6266SJeff Kirsher 	/*
267527a6266SJeff Kirsher 	 * Per-port MBUS window access register value.
268527a6266SJeff Kirsher 	 */
269527a6266SJeff Kirsher 	u32 win_protect;
270527a6266SJeff Kirsher 
271527a6266SJeff Kirsher 	/*
272527a6266SJeff Kirsher 	 * Hardware-specific parameters.
273527a6266SJeff Kirsher 	 */
274527a6266SJeff Kirsher 	int extended_rx_coal_limit;
275527a6266SJeff Kirsher 	int tx_bw_control;
276527a6266SJeff Kirsher 	int tx_csum_limit;
27720922486SSebastian Hesselbarth 	struct clk *clk;
278527a6266SJeff Kirsher };
279527a6266SJeff Kirsher 
280527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT		0
281527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT	1
282527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT	2
283527a6266SJeff Kirsher 
284527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev);
285527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev);
286527a6266SJeff Kirsher 
287527a6266SJeff Kirsher 
288527a6266SJeff Kirsher /* per-port *****************************************************************/
289527a6266SJeff Kirsher struct mib_counters {
290527a6266SJeff Kirsher 	u64 good_octets_received;
291527a6266SJeff Kirsher 	u32 bad_octets_received;
292527a6266SJeff Kirsher 	u32 internal_mac_transmit_err;
293527a6266SJeff Kirsher 	u32 good_frames_received;
294527a6266SJeff Kirsher 	u32 bad_frames_received;
295527a6266SJeff Kirsher 	u32 broadcast_frames_received;
296527a6266SJeff Kirsher 	u32 multicast_frames_received;
297527a6266SJeff Kirsher 	u32 frames_64_octets;
298527a6266SJeff Kirsher 	u32 frames_65_to_127_octets;
299527a6266SJeff Kirsher 	u32 frames_128_to_255_octets;
300527a6266SJeff Kirsher 	u32 frames_256_to_511_octets;
301527a6266SJeff Kirsher 	u32 frames_512_to_1023_octets;
302527a6266SJeff Kirsher 	u32 frames_1024_to_max_octets;
303527a6266SJeff Kirsher 	u64 good_octets_sent;
304527a6266SJeff Kirsher 	u32 good_frames_sent;
305527a6266SJeff Kirsher 	u32 excessive_collision;
306527a6266SJeff Kirsher 	u32 multicast_frames_sent;
307527a6266SJeff Kirsher 	u32 broadcast_frames_sent;
308527a6266SJeff Kirsher 	u32 unrec_mac_control_received;
309527a6266SJeff Kirsher 	u32 fc_sent;
310527a6266SJeff Kirsher 	u32 good_fc_received;
311527a6266SJeff Kirsher 	u32 bad_fc_received;
312527a6266SJeff Kirsher 	u32 undersize_received;
313527a6266SJeff Kirsher 	u32 fragments_received;
314527a6266SJeff Kirsher 	u32 oversize_received;
315527a6266SJeff Kirsher 	u32 jabber_received;
316527a6266SJeff Kirsher 	u32 mac_receive_error;
317527a6266SJeff Kirsher 	u32 bad_crc_event;
318527a6266SJeff Kirsher 	u32 collision;
319527a6266SJeff Kirsher 	u32 late_collision;
320302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
321302476c9SPaulius Zaleckas 	u32 rx_discard;
322302476c9SPaulius Zaleckas 	u32 rx_overrun;
323527a6266SJeff Kirsher };
324527a6266SJeff Kirsher 
325527a6266SJeff Kirsher struct rx_queue {
326527a6266SJeff Kirsher 	int index;
327527a6266SJeff Kirsher 
328527a6266SJeff Kirsher 	int rx_ring_size;
329527a6266SJeff Kirsher 
330527a6266SJeff Kirsher 	int rx_desc_count;
331527a6266SJeff Kirsher 	int rx_curr_desc;
332527a6266SJeff Kirsher 	int rx_used_desc;
333527a6266SJeff Kirsher 
334527a6266SJeff Kirsher 	struct rx_desc *rx_desc_area;
335527a6266SJeff Kirsher 	dma_addr_t rx_desc_dma;
336527a6266SJeff Kirsher 	int rx_desc_area_size;
337527a6266SJeff Kirsher 	struct sk_buff **rx_skb;
338527a6266SJeff Kirsher };
339527a6266SJeff Kirsher 
340527a6266SJeff Kirsher struct tx_queue {
341527a6266SJeff Kirsher 	int index;
342527a6266SJeff Kirsher 
343527a6266SJeff Kirsher 	int tx_ring_size;
344527a6266SJeff Kirsher 
345527a6266SJeff Kirsher 	int tx_desc_count;
346527a6266SJeff Kirsher 	int tx_curr_desc;
347527a6266SJeff Kirsher 	int tx_used_desc;
348527a6266SJeff Kirsher 
349527a6266SJeff Kirsher 	struct tx_desc *tx_desc_area;
350527a6266SJeff Kirsher 	dma_addr_t tx_desc_dma;
351527a6266SJeff Kirsher 	int tx_desc_area_size;
352527a6266SJeff Kirsher 
353527a6266SJeff Kirsher 	struct sk_buff_head tx_skb;
354527a6266SJeff Kirsher 
355527a6266SJeff Kirsher 	unsigned long tx_packets;
356527a6266SJeff Kirsher 	unsigned long tx_bytes;
357527a6266SJeff Kirsher 	unsigned long tx_dropped;
358527a6266SJeff Kirsher };
359527a6266SJeff Kirsher 
360527a6266SJeff Kirsher struct mv643xx_eth_private {
361527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *shared;
362527a6266SJeff Kirsher 	void __iomem *base;
363527a6266SJeff Kirsher 	int port_num;
364527a6266SJeff Kirsher 
365527a6266SJeff Kirsher 	struct net_device *dev;
366527a6266SJeff Kirsher 
367527a6266SJeff Kirsher 	struct phy_device *phy;
368527a6266SJeff Kirsher 
369527a6266SJeff Kirsher 	struct timer_list mib_counters_timer;
370527a6266SJeff Kirsher 	spinlock_t mib_counters_lock;
371527a6266SJeff Kirsher 	struct mib_counters mib_counters;
372527a6266SJeff Kirsher 
373527a6266SJeff Kirsher 	struct work_struct tx_timeout_task;
374527a6266SJeff Kirsher 
375527a6266SJeff Kirsher 	struct napi_struct napi;
376527a6266SJeff Kirsher 	u32 int_mask;
377527a6266SJeff Kirsher 	u8 oom;
378527a6266SJeff Kirsher 	u8 work_link;
379527a6266SJeff Kirsher 	u8 work_tx;
380527a6266SJeff Kirsher 	u8 work_tx_end;
381527a6266SJeff Kirsher 	u8 work_rx;
382527a6266SJeff Kirsher 	u8 work_rx_refill;
383527a6266SJeff Kirsher 
384527a6266SJeff Kirsher 	int skb_size;
385527a6266SJeff Kirsher 
386527a6266SJeff Kirsher 	/*
387527a6266SJeff Kirsher 	 * RX state.
388527a6266SJeff Kirsher 	 */
389527a6266SJeff Kirsher 	int rx_ring_size;
390527a6266SJeff Kirsher 	unsigned long rx_desc_sram_addr;
391527a6266SJeff Kirsher 	int rx_desc_sram_size;
392527a6266SJeff Kirsher 	int rxq_count;
393527a6266SJeff Kirsher 	struct timer_list rx_oom;
394527a6266SJeff Kirsher 	struct rx_queue rxq[8];
395527a6266SJeff Kirsher 
396527a6266SJeff Kirsher 	/*
397527a6266SJeff Kirsher 	 * TX state.
398527a6266SJeff Kirsher 	 */
399527a6266SJeff Kirsher 	int tx_ring_size;
400527a6266SJeff Kirsher 	unsigned long tx_desc_sram_addr;
401527a6266SJeff Kirsher 	int tx_desc_sram_size;
402527a6266SJeff Kirsher 	int txq_count;
403527a6266SJeff Kirsher 	struct tx_queue txq[8];
404452503ebSAndrew Lunn 
405452503ebSAndrew Lunn 	/*
406452503ebSAndrew Lunn 	 * Hardware-specific parameters.
407452503ebSAndrew Lunn 	 */
408452503ebSAndrew Lunn 	struct clk *clk;
409452503ebSAndrew Lunn 	unsigned int t_clk;
410527a6266SJeff Kirsher };
411527a6266SJeff Kirsher 
412527a6266SJeff Kirsher 
413527a6266SJeff Kirsher /* port register accessors **************************************************/
414527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
415527a6266SJeff Kirsher {
416527a6266SJeff Kirsher 	return readl(mp->shared->base + offset);
417527a6266SJeff Kirsher }
418527a6266SJeff Kirsher 
419527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
420527a6266SJeff Kirsher {
421527a6266SJeff Kirsher 	return readl(mp->base + offset);
422527a6266SJeff Kirsher }
423527a6266SJeff Kirsher 
424527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
425527a6266SJeff Kirsher {
426527a6266SJeff Kirsher 	writel(data, mp->shared->base + offset);
427527a6266SJeff Kirsher }
428527a6266SJeff Kirsher 
429527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
430527a6266SJeff Kirsher {
431527a6266SJeff Kirsher 	writel(data, mp->base + offset);
432527a6266SJeff Kirsher }
433527a6266SJeff Kirsher 
434527a6266SJeff Kirsher 
435527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/
436527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
437527a6266SJeff Kirsher {
438527a6266SJeff Kirsher 	return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
439527a6266SJeff Kirsher }
440527a6266SJeff Kirsher 
441527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
442527a6266SJeff Kirsher {
443527a6266SJeff Kirsher 	return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
444527a6266SJeff Kirsher }
445527a6266SJeff Kirsher 
446527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq)
447527a6266SJeff Kirsher {
448527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
449527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
450527a6266SJeff Kirsher }
451527a6266SJeff Kirsher 
452527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq)
453527a6266SJeff Kirsher {
454527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
455527a6266SJeff Kirsher 	u8 mask = 1 << rxq->index;
456527a6266SJeff Kirsher 
457527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, mask << 8);
458527a6266SJeff Kirsher 	while (rdlp(mp, RXQ_COMMAND) & mask)
459527a6266SJeff Kirsher 		udelay(10);
460527a6266SJeff Kirsher }
461527a6266SJeff Kirsher 
462527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq)
463527a6266SJeff Kirsher {
464527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
465527a6266SJeff Kirsher 	u32 addr;
466527a6266SJeff Kirsher 
467527a6266SJeff Kirsher 	addr = (u32)txq->tx_desc_dma;
468527a6266SJeff Kirsher 	addr += txq->tx_curr_desc * sizeof(struct tx_desc);
469527a6266SJeff Kirsher 	wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
470527a6266SJeff Kirsher }
471527a6266SJeff Kirsher 
472527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq)
473527a6266SJeff Kirsher {
474527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
475527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, 1 << txq->index);
476527a6266SJeff Kirsher }
477527a6266SJeff Kirsher 
478527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq)
479527a6266SJeff Kirsher {
480527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
481527a6266SJeff Kirsher 	u8 mask = 1 << txq->index;
482527a6266SJeff Kirsher 
483527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, mask << 8);
484527a6266SJeff Kirsher 	while (rdlp(mp, TXQ_COMMAND) & mask)
485527a6266SJeff Kirsher 		udelay(10);
486527a6266SJeff Kirsher }
487527a6266SJeff Kirsher 
488527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq)
489527a6266SJeff Kirsher {
490527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
491527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
492527a6266SJeff Kirsher 
493527a6266SJeff Kirsher 	if (netif_tx_queue_stopped(nq)) {
494527a6266SJeff Kirsher 		__netif_tx_lock(nq, smp_processor_id());
495527a6266SJeff Kirsher 		if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
496527a6266SJeff Kirsher 			netif_tx_wake_queue(nq);
497527a6266SJeff Kirsher 		__netif_tx_unlock(nq);
498527a6266SJeff Kirsher 	}
499527a6266SJeff Kirsher }
500527a6266SJeff Kirsher 
501527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget)
502527a6266SJeff Kirsher {
503527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
504527a6266SJeff Kirsher 	struct net_device_stats *stats = &mp->dev->stats;
505527a6266SJeff Kirsher 	int rx;
506527a6266SJeff Kirsher 
507527a6266SJeff Kirsher 	rx = 0;
508527a6266SJeff Kirsher 	while (rx < budget && rxq->rx_desc_count) {
509527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
510527a6266SJeff Kirsher 		unsigned int cmd_sts;
511527a6266SJeff Kirsher 		struct sk_buff *skb;
512527a6266SJeff Kirsher 		u16 byte_cnt;
513527a6266SJeff Kirsher 
514527a6266SJeff Kirsher 		rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
515527a6266SJeff Kirsher 
516527a6266SJeff Kirsher 		cmd_sts = rx_desc->cmd_sts;
517527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA)
518527a6266SJeff Kirsher 			break;
519527a6266SJeff Kirsher 		rmb();
520527a6266SJeff Kirsher 
521527a6266SJeff Kirsher 		skb = rxq->rx_skb[rxq->rx_curr_desc];
522527a6266SJeff Kirsher 		rxq->rx_skb[rxq->rx_curr_desc] = NULL;
523527a6266SJeff Kirsher 
524527a6266SJeff Kirsher 		rxq->rx_curr_desc++;
525527a6266SJeff Kirsher 		if (rxq->rx_curr_desc == rxq->rx_ring_size)
526527a6266SJeff Kirsher 			rxq->rx_curr_desc = 0;
527527a6266SJeff Kirsher 
528527a6266SJeff Kirsher 		dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
529527a6266SJeff Kirsher 				 rx_desc->buf_size, DMA_FROM_DEVICE);
530527a6266SJeff Kirsher 		rxq->rx_desc_count--;
531527a6266SJeff Kirsher 		rx++;
532527a6266SJeff Kirsher 
533527a6266SJeff Kirsher 		mp->work_rx_refill |= 1 << rxq->index;
534527a6266SJeff Kirsher 
535527a6266SJeff Kirsher 		byte_cnt = rx_desc->byte_cnt;
536527a6266SJeff Kirsher 
537527a6266SJeff Kirsher 		/*
538527a6266SJeff Kirsher 		 * Update statistics.
539527a6266SJeff Kirsher 		 *
540527a6266SJeff Kirsher 		 * Note that the descriptor byte count includes 2 dummy
541527a6266SJeff Kirsher 		 * bytes automatically inserted by the hardware at the
542527a6266SJeff Kirsher 		 * start of the packet (which we don't count), and a 4
543527a6266SJeff Kirsher 		 * byte CRC at the end of the packet (which we do count).
544527a6266SJeff Kirsher 		 */
545527a6266SJeff Kirsher 		stats->rx_packets++;
546527a6266SJeff Kirsher 		stats->rx_bytes += byte_cnt - 2;
547527a6266SJeff Kirsher 
548527a6266SJeff Kirsher 		/*
549527a6266SJeff Kirsher 		 * In case we received a packet without first / last bits
550527a6266SJeff Kirsher 		 * on, or the error summary bit is set, the packet needs
551527a6266SJeff Kirsher 		 * to be dropped.
552527a6266SJeff Kirsher 		 */
553527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
554527a6266SJeff Kirsher 			!= (RX_FIRST_DESC | RX_LAST_DESC))
555527a6266SJeff Kirsher 			goto err;
556527a6266SJeff Kirsher 
557527a6266SJeff Kirsher 		/*
558527a6266SJeff Kirsher 		 * The -4 is for the CRC in the trailer of the
559527a6266SJeff Kirsher 		 * received packet
560527a6266SJeff Kirsher 		 */
561527a6266SJeff Kirsher 		skb_put(skb, byte_cnt - 2 - 4);
562527a6266SJeff Kirsher 
563527a6266SJeff Kirsher 		if (cmd_sts & LAYER_4_CHECKSUM_OK)
564527a6266SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
565527a6266SJeff Kirsher 		skb->protocol = eth_type_trans(skb, mp->dev);
566527a6266SJeff Kirsher 
56709bf1c10SSebastian Hesselbarth 		napi_gro_receive(&mp->napi, skb);
568527a6266SJeff Kirsher 
569527a6266SJeff Kirsher 		continue;
570527a6266SJeff Kirsher 
571527a6266SJeff Kirsher err:
572527a6266SJeff Kirsher 		stats->rx_dropped++;
573527a6266SJeff Kirsher 
574527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
575527a6266SJeff Kirsher 			(RX_FIRST_DESC | RX_LAST_DESC)) {
576527a6266SJeff Kirsher 			if (net_ratelimit())
577527a6266SJeff Kirsher 				netdev_err(mp->dev,
578527a6266SJeff Kirsher 					   "received packet spanning multiple descriptors\n");
579527a6266SJeff Kirsher 		}
580527a6266SJeff Kirsher 
581527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY)
582527a6266SJeff Kirsher 			stats->rx_errors++;
583527a6266SJeff Kirsher 
584527a6266SJeff Kirsher 		dev_kfree_skb(skb);
585527a6266SJeff Kirsher 	}
586527a6266SJeff Kirsher 
587527a6266SJeff Kirsher 	if (rx < budget)
588527a6266SJeff Kirsher 		mp->work_rx &= ~(1 << rxq->index);
589527a6266SJeff Kirsher 
590527a6266SJeff Kirsher 	return rx;
591527a6266SJeff Kirsher }
592527a6266SJeff Kirsher 
593527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget)
594527a6266SJeff Kirsher {
595527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
596527a6266SJeff Kirsher 	int refilled;
597527a6266SJeff Kirsher 
598527a6266SJeff Kirsher 	refilled = 0;
599527a6266SJeff Kirsher 	while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
600527a6266SJeff Kirsher 		struct sk_buff *skb;
601527a6266SJeff Kirsher 		int rx;
602527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
603527a6266SJeff Kirsher 		int size;
604527a6266SJeff Kirsher 
605c056b734SPradeep A Dalvi 		skb = netdev_alloc_skb(mp->dev, mp->skb_size);
606527a6266SJeff Kirsher 
607527a6266SJeff Kirsher 		if (skb == NULL) {
608527a6266SJeff Kirsher 			mp->oom = 1;
609527a6266SJeff Kirsher 			goto oom;
610527a6266SJeff Kirsher 		}
611527a6266SJeff Kirsher 
612527a6266SJeff Kirsher 		if (SKB_DMA_REALIGN)
613527a6266SJeff Kirsher 			skb_reserve(skb, SKB_DMA_REALIGN);
614527a6266SJeff Kirsher 
615527a6266SJeff Kirsher 		refilled++;
616527a6266SJeff Kirsher 		rxq->rx_desc_count++;
617527a6266SJeff Kirsher 
618527a6266SJeff Kirsher 		rx = rxq->rx_used_desc++;
619527a6266SJeff Kirsher 		if (rxq->rx_used_desc == rxq->rx_ring_size)
620527a6266SJeff Kirsher 			rxq->rx_used_desc = 0;
621527a6266SJeff Kirsher 
622527a6266SJeff Kirsher 		rx_desc = rxq->rx_desc_area + rx;
623527a6266SJeff Kirsher 
62418f1d054SIsaku Yamahata 		size = skb_end_pointer(skb) - skb->data;
625527a6266SJeff Kirsher 		rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
626527a6266SJeff Kirsher 						  skb->data, size,
627527a6266SJeff Kirsher 						  DMA_FROM_DEVICE);
628527a6266SJeff Kirsher 		rx_desc->buf_size = size;
629527a6266SJeff Kirsher 		rxq->rx_skb[rx] = skb;
630527a6266SJeff Kirsher 		wmb();
631527a6266SJeff Kirsher 		rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
632527a6266SJeff Kirsher 		wmb();
633527a6266SJeff Kirsher 
634527a6266SJeff Kirsher 		/*
635527a6266SJeff Kirsher 		 * The hardware automatically prepends 2 bytes of
636527a6266SJeff Kirsher 		 * dummy data to each received packet, so that the
637527a6266SJeff Kirsher 		 * IP header ends up 16-byte aligned.
638527a6266SJeff Kirsher 		 */
639527a6266SJeff Kirsher 		skb_reserve(skb, 2);
640527a6266SJeff Kirsher 	}
641527a6266SJeff Kirsher 
642527a6266SJeff Kirsher 	if (refilled < budget)
643527a6266SJeff Kirsher 		mp->work_rx_refill &= ~(1 << rxq->index);
644527a6266SJeff Kirsher 
645527a6266SJeff Kirsher oom:
646527a6266SJeff Kirsher 	return refilled;
647527a6266SJeff Kirsher }
648527a6266SJeff Kirsher 
649527a6266SJeff Kirsher 
650527a6266SJeff Kirsher /* tx ***********************************************************************/
651527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
652527a6266SJeff Kirsher {
653527a6266SJeff Kirsher 	int frag;
654527a6266SJeff Kirsher 
655527a6266SJeff Kirsher 	for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
6569e903e08SEric Dumazet 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
6579e903e08SEric Dumazet 
6589e903e08SEric Dumazet 		if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
659527a6266SJeff Kirsher 			return 1;
660527a6266SJeff Kirsher 	}
661527a6266SJeff Kirsher 
662527a6266SJeff Kirsher 	return 0;
663527a6266SJeff Kirsher }
664527a6266SJeff Kirsher 
665527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
666527a6266SJeff Kirsher {
667527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
668527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
669527a6266SJeff Kirsher 	int frag;
670527a6266SJeff Kirsher 
671527a6266SJeff Kirsher 	for (frag = 0; frag < nr_frags; frag++) {
672527a6266SJeff Kirsher 		skb_frag_t *this_frag;
673527a6266SJeff Kirsher 		int tx_index;
674527a6266SJeff Kirsher 		struct tx_desc *desc;
675527a6266SJeff Kirsher 
676527a6266SJeff Kirsher 		this_frag = &skb_shinfo(skb)->frags[frag];
677527a6266SJeff Kirsher 		tx_index = txq->tx_curr_desc++;
678527a6266SJeff Kirsher 		if (txq->tx_curr_desc == txq->tx_ring_size)
679527a6266SJeff Kirsher 			txq->tx_curr_desc = 0;
680527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
681527a6266SJeff Kirsher 
682527a6266SJeff Kirsher 		/*
683527a6266SJeff Kirsher 		 * The last fragment will generate an interrupt
684527a6266SJeff Kirsher 		 * which will free the skb on TX completion.
685527a6266SJeff Kirsher 		 */
686527a6266SJeff Kirsher 		if (frag == nr_frags - 1) {
687527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA |
688527a6266SJeff Kirsher 					ZERO_PADDING | TX_LAST_DESC |
689527a6266SJeff Kirsher 					TX_ENABLE_INTERRUPT;
690527a6266SJeff Kirsher 		} else {
691527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA;
692527a6266SJeff Kirsher 		}
693527a6266SJeff Kirsher 
694527a6266SJeff Kirsher 		desc->l4i_chk = 0;
6959e903e08SEric Dumazet 		desc->byte_cnt = skb_frag_size(this_frag);
696f106358bSIan Campbell 		desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
697f106358bSIan Campbell 						 this_frag, 0,
6989e903e08SEric Dumazet 						 skb_frag_size(this_frag),
699f106358bSIan Campbell 						 DMA_TO_DEVICE);
700527a6266SJeff Kirsher 	}
701527a6266SJeff Kirsher }
702527a6266SJeff Kirsher 
703527a6266SJeff Kirsher static inline __be16 sum16_as_be(__sum16 sum)
704527a6266SJeff Kirsher {
705527a6266SJeff Kirsher 	return (__force __be16)sum;
706527a6266SJeff Kirsher }
707527a6266SJeff Kirsher 
708527a6266SJeff Kirsher static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
709527a6266SJeff Kirsher {
710527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
711527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
712527a6266SJeff Kirsher 	int tx_index;
713527a6266SJeff Kirsher 	struct tx_desc *desc;
714527a6266SJeff Kirsher 	u32 cmd_sts;
715527a6266SJeff Kirsher 	u16 l4i_chk;
716527a6266SJeff Kirsher 	int length;
717527a6266SJeff Kirsher 
718527a6266SJeff Kirsher 	cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
719527a6266SJeff Kirsher 	l4i_chk = 0;
720527a6266SJeff Kirsher 
721527a6266SJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
722527a6266SJeff Kirsher 		int hdr_len;
723527a6266SJeff Kirsher 		int tag_bytes;
724527a6266SJeff Kirsher 
725527a6266SJeff Kirsher 		BUG_ON(skb->protocol != htons(ETH_P_IP) &&
726527a6266SJeff Kirsher 		       skb->protocol != htons(ETH_P_8021Q));
727527a6266SJeff Kirsher 
728527a6266SJeff Kirsher 		hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
729527a6266SJeff Kirsher 		tag_bytes = hdr_len - ETH_HLEN;
730527a6266SJeff Kirsher 		if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
731527a6266SJeff Kirsher 		    unlikely(tag_bytes & ~12)) {
732527a6266SJeff Kirsher 			if (skb_checksum_help(skb) == 0)
733527a6266SJeff Kirsher 				goto no_csum;
734527a6266SJeff Kirsher 			kfree_skb(skb);
735527a6266SJeff Kirsher 			return 1;
736527a6266SJeff Kirsher 		}
737527a6266SJeff Kirsher 
738527a6266SJeff Kirsher 		if (tag_bytes & 4)
739527a6266SJeff Kirsher 			cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
740527a6266SJeff Kirsher 		if (tag_bytes & 8)
741527a6266SJeff Kirsher 			cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
742527a6266SJeff Kirsher 
743527a6266SJeff Kirsher 		cmd_sts |= GEN_TCP_UDP_CHECKSUM |
744527a6266SJeff Kirsher 			   GEN_IP_V4_CHECKSUM   |
745527a6266SJeff Kirsher 			   ip_hdr(skb)->ihl << TX_IHL_SHIFT;
746527a6266SJeff Kirsher 
747527a6266SJeff Kirsher 		switch (ip_hdr(skb)->protocol) {
748527a6266SJeff Kirsher 		case IPPROTO_UDP:
749527a6266SJeff Kirsher 			cmd_sts |= UDP_FRAME;
750527a6266SJeff Kirsher 			l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
751527a6266SJeff Kirsher 			break;
752527a6266SJeff Kirsher 		case IPPROTO_TCP:
753527a6266SJeff Kirsher 			l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
754527a6266SJeff Kirsher 			break;
755527a6266SJeff Kirsher 		default:
756527a6266SJeff Kirsher 			BUG();
757527a6266SJeff Kirsher 		}
758527a6266SJeff Kirsher 	} else {
759527a6266SJeff Kirsher no_csum:
760527a6266SJeff Kirsher 		/* Errata BTS #50, IHL must be 5 if no HW checksum */
761527a6266SJeff Kirsher 		cmd_sts |= 5 << TX_IHL_SHIFT;
762527a6266SJeff Kirsher 	}
763527a6266SJeff Kirsher 
764527a6266SJeff Kirsher 	tx_index = txq->tx_curr_desc++;
765527a6266SJeff Kirsher 	if (txq->tx_curr_desc == txq->tx_ring_size)
766527a6266SJeff Kirsher 		txq->tx_curr_desc = 0;
767527a6266SJeff Kirsher 	desc = &txq->tx_desc_area[tx_index];
768527a6266SJeff Kirsher 
769527a6266SJeff Kirsher 	if (nr_frags) {
770527a6266SJeff Kirsher 		txq_submit_frag_skb(txq, skb);
771527a6266SJeff Kirsher 		length = skb_headlen(skb);
772527a6266SJeff Kirsher 	} else {
773527a6266SJeff Kirsher 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
774527a6266SJeff Kirsher 		length = skb->len;
775527a6266SJeff Kirsher 	}
776527a6266SJeff Kirsher 
777527a6266SJeff Kirsher 	desc->l4i_chk = l4i_chk;
778527a6266SJeff Kirsher 	desc->byte_cnt = length;
779527a6266SJeff Kirsher 	desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
780527a6266SJeff Kirsher 				       length, DMA_TO_DEVICE);
781527a6266SJeff Kirsher 
782527a6266SJeff Kirsher 	__skb_queue_tail(&txq->tx_skb, skb);
783527a6266SJeff Kirsher 
784527a6266SJeff Kirsher 	skb_tx_timestamp(skb);
785527a6266SJeff Kirsher 
786527a6266SJeff Kirsher 	/* ensure all other descriptors are written before first cmd_sts */
787527a6266SJeff Kirsher 	wmb();
788527a6266SJeff Kirsher 	desc->cmd_sts = cmd_sts;
789527a6266SJeff Kirsher 
790527a6266SJeff Kirsher 	/* clear TX_END status */
791527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
792527a6266SJeff Kirsher 
793527a6266SJeff Kirsher 	/* ensure all descriptors are written before poking hardware */
794527a6266SJeff Kirsher 	wmb();
795527a6266SJeff Kirsher 	txq_enable(txq);
796527a6266SJeff Kirsher 
797527a6266SJeff Kirsher 	txq->tx_desc_count += nr_frags + 1;
798527a6266SJeff Kirsher 
799527a6266SJeff Kirsher 	return 0;
800527a6266SJeff Kirsher }
801527a6266SJeff Kirsher 
802527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
803527a6266SJeff Kirsher {
804527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
805527a6266SJeff Kirsher 	int length, queue;
806527a6266SJeff Kirsher 	struct tx_queue *txq;
807527a6266SJeff Kirsher 	struct netdev_queue *nq;
808527a6266SJeff Kirsher 
809527a6266SJeff Kirsher 	queue = skb_get_queue_mapping(skb);
810527a6266SJeff Kirsher 	txq = mp->txq + queue;
811527a6266SJeff Kirsher 	nq = netdev_get_tx_queue(dev, queue);
812527a6266SJeff Kirsher 
813527a6266SJeff Kirsher 	if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
814527a6266SJeff Kirsher 		txq->tx_dropped++;
815527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
816527a6266SJeff Kirsher 			      "failed to linearize skb with tiny unaligned fragment\n");
817527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
818527a6266SJeff Kirsher 	}
819527a6266SJeff Kirsher 
820527a6266SJeff Kirsher 	if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
821527a6266SJeff Kirsher 		if (net_ratelimit())
822527a6266SJeff Kirsher 			netdev_err(dev, "tx queue full?!\n");
823527a6266SJeff Kirsher 		kfree_skb(skb);
824527a6266SJeff Kirsher 		return NETDEV_TX_OK;
825527a6266SJeff Kirsher 	}
826527a6266SJeff Kirsher 
827527a6266SJeff Kirsher 	length = skb->len;
828527a6266SJeff Kirsher 
829527a6266SJeff Kirsher 	if (!txq_submit_skb(txq, skb)) {
830527a6266SJeff Kirsher 		int entries_left;
831527a6266SJeff Kirsher 
832527a6266SJeff Kirsher 		txq->tx_bytes += length;
833527a6266SJeff Kirsher 		txq->tx_packets++;
834527a6266SJeff Kirsher 
835527a6266SJeff Kirsher 		entries_left = txq->tx_ring_size - txq->tx_desc_count;
836527a6266SJeff Kirsher 		if (entries_left < MAX_SKB_FRAGS + 1)
837527a6266SJeff Kirsher 			netif_tx_stop_queue(nq);
838527a6266SJeff Kirsher 	}
839527a6266SJeff Kirsher 
840527a6266SJeff Kirsher 	return NETDEV_TX_OK;
841527a6266SJeff Kirsher }
842527a6266SJeff Kirsher 
843527a6266SJeff Kirsher 
844527a6266SJeff Kirsher /* tx napi ******************************************************************/
845527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq)
846527a6266SJeff Kirsher {
847527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
848527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
849527a6266SJeff Kirsher 	u32 hw_desc_ptr;
850527a6266SJeff Kirsher 	u32 expected_ptr;
851527a6266SJeff Kirsher 
852527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
853527a6266SJeff Kirsher 
854527a6266SJeff Kirsher 	if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
855527a6266SJeff Kirsher 		goto out;
856527a6266SJeff Kirsher 
857527a6266SJeff Kirsher 	hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
858527a6266SJeff Kirsher 	expected_ptr = (u32)txq->tx_desc_dma +
859527a6266SJeff Kirsher 				txq->tx_curr_desc * sizeof(struct tx_desc);
860527a6266SJeff Kirsher 
861527a6266SJeff Kirsher 	if (hw_desc_ptr != expected_ptr)
862527a6266SJeff Kirsher 		txq_enable(txq);
863527a6266SJeff Kirsher 
864527a6266SJeff Kirsher out:
865527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
866527a6266SJeff Kirsher 
867527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
868527a6266SJeff Kirsher }
869527a6266SJeff Kirsher 
870527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force)
871527a6266SJeff Kirsher {
872527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
873527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
874527a6266SJeff Kirsher 	int reclaimed;
875527a6266SJeff Kirsher 
8763aefe2b4SRussell King 	__netif_tx_lock_bh(nq);
877527a6266SJeff Kirsher 
878527a6266SJeff Kirsher 	reclaimed = 0;
879527a6266SJeff Kirsher 	while (reclaimed < budget && txq->tx_desc_count > 0) {
880527a6266SJeff Kirsher 		int tx_index;
881527a6266SJeff Kirsher 		struct tx_desc *desc;
882527a6266SJeff Kirsher 		u32 cmd_sts;
883527a6266SJeff Kirsher 		struct sk_buff *skb;
884527a6266SJeff Kirsher 
885527a6266SJeff Kirsher 		tx_index = txq->tx_used_desc;
886527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
887527a6266SJeff Kirsher 		cmd_sts = desc->cmd_sts;
888527a6266SJeff Kirsher 
889527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA) {
890527a6266SJeff Kirsher 			if (!force)
891527a6266SJeff Kirsher 				break;
892527a6266SJeff Kirsher 			desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
893527a6266SJeff Kirsher 		}
894527a6266SJeff Kirsher 
895527a6266SJeff Kirsher 		txq->tx_used_desc = tx_index + 1;
896527a6266SJeff Kirsher 		if (txq->tx_used_desc == txq->tx_ring_size)
897527a6266SJeff Kirsher 			txq->tx_used_desc = 0;
898527a6266SJeff Kirsher 
899527a6266SJeff Kirsher 		reclaimed++;
900527a6266SJeff Kirsher 		txq->tx_desc_count--;
901527a6266SJeff Kirsher 
902527a6266SJeff Kirsher 		skb = NULL;
903527a6266SJeff Kirsher 		if (cmd_sts & TX_LAST_DESC)
904527a6266SJeff Kirsher 			skb = __skb_dequeue(&txq->tx_skb);
905527a6266SJeff Kirsher 
906527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY) {
907527a6266SJeff Kirsher 			netdev_info(mp->dev, "tx error\n");
908527a6266SJeff Kirsher 			mp->dev->stats.tx_errors++;
909527a6266SJeff Kirsher 		}
910527a6266SJeff Kirsher 
911527a6266SJeff Kirsher 		if (cmd_sts & TX_FIRST_DESC) {
912527a6266SJeff Kirsher 			dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
913527a6266SJeff Kirsher 					 desc->byte_cnt, DMA_TO_DEVICE);
914527a6266SJeff Kirsher 		} else {
915527a6266SJeff Kirsher 			dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
916527a6266SJeff Kirsher 				       desc->byte_cnt, DMA_TO_DEVICE);
917527a6266SJeff Kirsher 		}
918527a6266SJeff Kirsher 
919527a6266SJeff Kirsher 		dev_kfree_skb(skb);
920527a6266SJeff Kirsher 	}
921527a6266SJeff Kirsher 
9223aefe2b4SRussell King 	__netif_tx_unlock_bh(nq);
923527a6266SJeff Kirsher 
924527a6266SJeff Kirsher 	if (reclaimed < budget)
925527a6266SJeff Kirsher 		mp->work_tx &= ~(1 << txq->index);
926527a6266SJeff Kirsher 
927527a6266SJeff Kirsher 	return reclaimed;
928527a6266SJeff Kirsher }
929527a6266SJeff Kirsher 
930527a6266SJeff Kirsher 
931527a6266SJeff Kirsher /* tx rate control **********************************************************/
932527a6266SJeff Kirsher /*
933527a6266SJeff Kirsher  * Set total maximum TX rate (shared by all TX queues for this port)
934527a6266SJeff Kirsher  * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
935527a6266SJeff Kirsher  */
936527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
937527a6266SJeff Kirsher {
938527a6266SJeff Kirsher 	int token_rate;
939527a6266SJeff Kirsher 	int mtu;
940527a6266SJeff Kirsher 	int bucket_size;
941527a6266SJeff Kirsher 
942452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
943527a6266SJeff Kirsher 	if (token_rate > 1023)
944527a6266SJeff Kirsher 		token_rate = 1023;
945527a6266SJeff Kirsher 
946527a6266SJeff Kirsher 	mtu = (mp->dev->mtu + 255) >> 8;
947527a6266SJeff Kirsher 	if (mtu > 63)
948527a6266SJeff Kirsher 		mtu = 63;
949527a6266SJeff Kirsher 
950527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
951527a6266SJeff Kirsher 	if (bucket_size > 65535)
952527a6266SJeff Kirsher 		bucket_size = 65535;
953527a6266SJeff Kirsher 
954527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
955527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
956527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE, token_rate);
957527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU, mtu);
958527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST, bucket_size);
959527a6266SJeff Kirsher 		break;
960527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
961527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE_MOVED, token_rate);
962527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU_MOVED, mtu);
963527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
964527a6266SJeff Kirsher 		break;
965527a6266SJeff Kirsher 	}
966527a6266SJeff Kirsher }
967527a6266SJeff Kirsher 
968527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
969527a6266SJeff Kirsher {
970527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
971527a6266SJeff Kirsher 	int token_rate;
972527a6266SJeff Kirsher 	int bucket_size;
973527a6266SJeff Kirsher 
974452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
975527a6266SJeff Kirsher 	if (token_rate > 1023)
976527a6266SJeff Kirsher 		token_rate = 1023;
977527a6266SJeff Kirsher 
978527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
979527a6266SJeff Kirsher 	if (bucket_size > 65535)
980527a6266SJeff Kirsher 		bucket_size = 65535;
981527a6266SJeff Kirsher 
982527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
983527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
984527a6266SJeff Kirsher }
985527a6266SJeff Kirsher 
986527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq)
987527a6266SJeff Kirsher {
988527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
989527a6266SJeff Kirsher 	int off;
990527a6266SJeff Kirsher 	u32 val;
991527a6266SJeff Kirsher 
992527a6266SJeff Kirsher 	/*
993527a6266SJeff Kirsher 	 * Turn on fixed priority mode.
994527a6266SJeff Kirsher 	 */
995527a6266SJeff Kirsher 	off = 0;
996527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
997527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
998527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF;
999527a6266SJeff Kirsher 		break;
1000527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1001527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF_MOVED;
1002527a6266SJeff Kirsher 		break;
1003527a6266SJeff Kirsher 	}
1004527a6266SJeff Kirsher 
1005527a6266SJeff Kirsher 	if (off) {
1006527a6266SJeff Kirsher 		val = rdlp(mp, off);
1007527a6266SJeff Kirsher 		val |= 1 << txq->index;
1008527a6266SJeff Kirsher 		wrlp(mp, off, val);
1009527a6266SJeff Kirsher 	}
1010527a6266SJeff Kirsher }
1011527a6266SJeff Kirsher 
1012527a6266SJeff Kirsher 
1013527a6266SJeff Kirsher /* mii management interface *************************************************/
1014260055bbSPhil Sutter static void mv643xx_adjust_pscr(struct mv643xx_eth_private *mp)
1015260055bbSPhil Sutter {
1016260055bbSPhil Sutter 	u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1017260055bbSPhil Sutter 	u32 autoneg_disable = FORCE_LINK_PASS |
1018260055bbSPhil Sutter 	             DISABLE_AUTO_NEG_SPEED_GMII |
1019260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1020260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_DUPLEX;
1021260055bbSPhil Sutter 
1022260055bbSPhil Sutter 	if (mp->phy->autoneg == AUTONEG_ENABLE) {
1023260055bbSPhil Sutter 		/* enable auto negotiation */
1024260055bbSPhil Sutter 		pscr &= ~autoneg_disable;
1025260055bbSPhil Sutter 		goto out_write;
1026260055bbSPhil Sutter 	}
1027260055bbSPhil Sutter 
1028260055bbSPhil Sutter 	pscr |= autoneg_disable;
1029260055bbSPhil Sutter 
1030260055bbSPhil Sutter 	if (mp->phy->speed == SPEED_1000) {
1031260055bbSPhil Sutter 		/* force gigabit, half duplex not supported */
1032260055bbSPhil Sutter 		pscr |= SET_GMII_SPEED_TO_1000;
1033260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1034260055bbSPhil Sutter 		goto out_write;
1035260055bbSPhil Sutter 	}
1036260055bbSPhil Sutter 
1037260055bbSPhil Sutter 	pscr &= ~SET_GMII_SPEED_TO_1000;
1038260055bbSPhil Sutter 
1039260055bbSPhil Sutter 	if (mp->phy->speed == SPEED_100)
1040260055bbSPhil Sutter 		pscr |= SET_MII_SPEED_TO_100;
1041260055bbSPhil Sutter 	else
1042260055bbSPhil Sutter 		pscr &= ~SET_MII_SPEED_TO_100;
1043260055bbSPhil Sutter 
1044260055bbSPhil Sutter 	if (mp->phy->duplex == DUPLEX_FULL)
1045260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1046260055bbSPhil Sutter 	else
1047260055bbSPhil Sutter 		pscr &= ~SET_FULL_DUPLEX_MODE;
1048260055bbSPhil Sutter 
1049260055bbSPhil Sutter out_write:
1050260055bbSPhil Sutter 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1051260055bbSPhil Sutter }
1052260055bbSPhil Sutter 
1053527a6266SJeff Kirsher /* statistics ***************************************************************/
1054527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1055527a6266SJeff Kirsher {
1056527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1057527a6266SJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
1058527a6266SJeff Kirsher 	unsigned long tx_packets = 0;
1059527a6266SJeff Kirsher 	unsigned long tx_bytes = 0;
1060527a6266SJeff Kirsher 	unsigned long tx_dropped = 0;
1061527a6266SJeff Kirsher 	int i;
1062527a6266SJeff Kirsher 
1063527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
1064527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
1065527a6266SJeff Kirsher 
1066527a6266SJeff Kirsher 		tx_packets += txq->tx_packets;
1067527a6266SJeff Kirsher 		tx_bytes += txq->tx_bytes;
1068527a6266SJeff Kirsher 		tx_dropped += txq->tx_dropped;
1069527a6266SJeff Kirsher 	}
1070527a6266SJeff Kirsher 
1071527a6266SJeff Kirsher 	stats->tx_packets = tx_packets;
1072527a6266SJeff Kirsher 	stats->tx_bytes = tx_bytes;
1073527a6266SJeff Kirsher 	stats->tx_dropped = tx_dropped;
1074527a6266SJeff Kirsher 
1075527a6266SJeff Kirsher 	return stats;
1076527a6266SJeff Kirsher }
1077527a6266SJeff Kirsher 
1078527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
1079527a6266SJeff Kirsher {
1080527a6266SJeff Kirsher 	return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
1081527a6266SJeff Kirsher }
1082527a6266SJeff Kirsher 
1083527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp)
1084527a6266SJeff Kirsher {
1085527a6266SJeff Kirsher 	int i;
1086527a6266SJeff Kirsher 
1087527a6266SJeff Kirsher 	for (i = 0; i < 0x80; i += 4)
1088527a6266SJeff Kirsher 		mib_read(mp, i);
1089302476c9SPaulius Zaleckas 
1090302476c9SPaulius Zaleckas 	/* Clear non MIB hw counters also */
1091302476c9SPaulius Zaleckas 	rdlp(mp, RX_DISCARD_FRAME_CNT);
1092302476c9SPaulius Zaleckas 	rdlp(mp, RX_OVERRUN_FRAME_CNT);
1093527a6266SJeff Kirsher }
1094527a6266SJeff Kirsher 
1095527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp)
1096527a6266SJeff Kirsher {
1097527a6266SJeff Kirsher 	struct mib_counters *p = &mp->mib_counters;
1098527a6266SJeff Kirsher 
1099527a6266SJeff Kirsher 	spin_lock_bh(&mp->mib_counters_lock);
1100527a6266SJeff Kirsher 	p->good_octets_received += mib_read(mp, 0x00);
1101527a6266SJeff Kirsher 	p->bad_octets_received += mib_read(mp, 0x08);
1102527a6266SJeff Kirsher 	p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1103527a6266SJeff Kirsher 	p->good_frames_received += mib_read(mp, 0x10);
1104527a6266SJeff Kirsher 	p->bad_frames_received += mib_read(mp, 0x14);
1105527a6266SJeff Kirsher 	p->broadcast_frames_received += mib_read(mp, 0x18);
1106527a6266SJeff Kirsher 	p->multicast_frames_received += mib_read(mp, 0x1c);
1107527a6266SJeff Kirsher 	p->frames_64_octets += mib_read(mp, 0x20);
1108527a6266SJeff Kirsher 	p->frames_65_to_127_octets += mib_read(mp, 0x24);
1109527a6266SJeff Kirsher 	p->frames_128_to_255_octets += mib_read(mp, 0x28);
1110527a6266SJeff Kirsher 	p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1111527a6266SJeff Kirsher 	p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1112527a6266SJeff Kirsher 	p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1113527a6266SJeff Kirsher 	p->good_octets_sent += mib_read(mp, 0x38);
1114527a6266SJeff Kirsher 	p->good_frames_sent += mib_read(mp, 0x40);
1115527a6266SJeff Kirsher 	p->excessive_collision += mib_read(mp, 0x44);
1116527a6266SJeff Kirsher 	p->multicast_frames_sent += mib_read(mp, 0x48);
1117527a6266SJeff Kirsher 	p->broadcast_frames_sent += mib_read(mp, 0x4c);
1118527a6266SJeff Kirsher 	p->unrec_mac_control_received += mib_read(mp, 0x50);
1119527a6266SJeff Kirsher 	p->fc_sent += mib_read(mp, 0x54);
1120527a6266SJeff Kirsher 	p->good_fc_received += mib_read(mp, 0x58);
1121527a6266SJeff Kirsher 	p->bad_fc_received += mib_read(mp, 0x5c);
1122527a6266SJeff Kirsher 	p->undersize_received += mib_read(mp, 0x60);
1123527a6266SJeff Kirsher 	p->fragments_received += mib_read(mp, 0x64);
1124527a6266SJeff Kirsher 	p->oversize_received += mib_read(mp, 0x68);
1125527a6266SJeff Kirsher 	p->jabber_received += mib_read(mp, 0x6c);
1126527a6266SJeff Kirsher 	p->mac_receive_error += mib_read(mp, 0x70);
1127527a6266SJeff Kirsher 	p->bad_crc_event += mib_read(mp, 0x74);
1128527a6266SJeff Kirsher 	p->collision += mib_read(mp, 0x78);
1129527a6266SJeff Kirsher 	p->late_collision += mib_read(mp, 0x7c);
1130302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
1131302476c9SPaulius Zaleckas 	p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1132302476c9SPaulius Zaleckas 	p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
1133527a6266SJeff Kirsher 	spin_unlock_bh(&mp->mib_counters_lock);
1134527a6266SJeff Kirsher 
1135527a6266SJeff Kirsher 	mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1136527a6266SJeff Kirsher }
1137527a6266SJeff Kirsher 
1138527a6266SJeff Kirsher static void mib_counters_timer_wrapper(unsigned long _mp)
1139527a6266SJeff Kirsher {
1140527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)_mp;
1141527a6266SJeff Kirsher 
1142527a6266SJeff Kirsher 	mib_counters_update(mp);
1143527a6266SJeff Kirsher }
1144527a6266SJeff Kirsher 
1145527a6266SJeff Kirsher 
1146527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/
1147527a6266SJeff Kirsher /*
1148527a6266SJeff Kirsher  * Hardware coalescing parameters are set in units of 64 t_clk
1149527a6266SJeff Kirsher  * cycles.  I.e.:
1150527a6266SJeff Kirsher  *
1151527a6266SJeff Kirsher  *	coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1152527a6266SJeff Kirsher  *
1153527a6266SJeff Kirsher  *	register_value = coal_delay_in_usec * t_clk_rate / 64000000
1154527a6266SJeff Kirsher  *
1155527a6266SJeff Kirsher  * In the ->set*() methods, we round the computed register value
1156527a6266SJeff Kirsher  * to the nearest integer.
1157527a6266SJeff Kirsher  */
1158527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1159527a6266SJeff Kirsher {
1160527a6266SJeff Kirsher 	u32 val = rdlp(mp, SDMA_CONFIG);
1161527a6266SJeff Kirsher 	u64 temp;
1162527a6266SJeff Kirsher 
1163527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit)
1164527a6266SJeff Kirsher 		temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1165527a6266SJeff Kirsher 	else
1166527a6266SJeff Kirsher 		temp = (val & 0x003fff00) >> 8;
1167527a6266SJeff Kirsher 
1168527a6266SJeff Kirsher 	temp *= 64000000;
1169452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1170527a6266SJeff Kirsher 
1171527a6266SJeff Kirsher 	return (unsigned int)temp;
1172527a6266SJeff Kirsher }
1173527a6266SJeff Kirsher 
1174527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1175527a6266SJeff Kirsher {
1176527a6266SJeff Kirsher 	u64 temp;
1177527a6266SJeff Kirsher 	u32 val;
1178527a6266SJeff Kirsher 
1179452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1180527a6266SJeff Kirsher 	temp += 31999999;
1181527a6266SJeff Kirsher 	do_div(temp, 64000000);
1182527a6266SJeff Kirsher 
1183527a6266SJeff Kirsher 	val = rdlp(mp, SDMA_CONFIG);
1184527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit) {
1185527a6266SJeff Kirsher 		if (temp > 0xffff)
1186527a6266SJeff Kirsher 			temp = 0xffff;
1187527a6266SJeff Kirsher 		val &= ~0x023fff80;
1188527a6266SJeff Kirsher 		val |= (temp & 0x8000) << 10;
1189527a6266SJeff Kirsher 		val |= (temp & 0x7fff) << 7;
1190527a6266SJeff Kirsher 	} else {
1191527a6266SJeff Kirsher 		if (temp > 0x3fff)
1192527a6266SJeff Kirsher 			temp = 0x3fff;
1193527a6266SJeff Kirsher 		val &= ~0x003fff00;
1194527a6266SJeff Kirsher 		val |= (temp & 0x3fff) << 8;
1195527a6266SJeff Kirsher 	}
1196527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, val);
1197527a6266SJeff Kirsher }
1198527a6266SJeff Kirsher 
1199527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1200527a6266SJeff Kirsher {
1201527a6266SJeff Kirsher 	u64 temp;
1202527a6266SJeff Kirsher 
1203527a6266SJeff Kirsher 	temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1204527a6266SJeff Kirsher 	temp *= 64000000;
1205452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1206527a6266SJeff Kirsher 
1207527a6266SJeff Kirsher 	return (unsigned int)temp;
1208527a6266SJeff Kirsher }
1209527a6266SJeff Kirsher 
1210527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1211527a6266SJeff Kirsher {
1212527a6266SJeff Kirsher 	u64 temp;
1213527a6266SJeff Kirsher 
1214452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1215527a6266SJeff Kirsher 	temp += 31999999;
1216527a6266SJeff Kirsher 	do_div(temp, 64000000);
1217527a6266SJeff Kirsher 
1218527a6266SJeff Kirsher 	if (temp > 0x3fff)
1219527a6266SJeff Kirsher 		temp = 0x3fff;
1220527a6266SJeff Kirsher 
1221527a6266SJeff Kirsher 	wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1222527a6266SJeff Kirsher }
1223527a6266SJeff Kirsher 
1224527a6266SJeff Kirsher 
1225527a6266SJeff Kirsher /* ethtool ******************************************************************/
1226527a6266SJeff Kirsher struct mv643xx_eth_stats {
1227527a6266SJeff Kirsher 	char stat_string[ETH_GSTRING_LEN];
1228527a6266SJeff Kirsher 	int sizeof_stat;
1229527a6266SJeff Kirsher 	int netdev_off;
1230527a6266SJeff Kirsher 	int mp_off;
1231527a6266SJeff Kirsher };
1232527a6266SJeff Kirsher 
1233527a6266SJeff Kirsher #define SSTAT(m)						\
1234527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct net_device_stats, m),		\
1235527a6266SJeff Kirsher 	  offsetof(struct net_device, stats.m), -1 }
1236527a6266SJeff Kirsher 
1237527a6266SJeff Kirsher #define MIBSTAT(m)						\
1238527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct mib_counters, m),		\
1239527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1240527a6266SJeff Kirsher 
1241527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1242527a6266SJeff Kirsher 	SSTAT(rx_packets),
1243527a6266SJeff Kirsher 	SSTAT(tx_packets),
1244527a6266SJeff Kirsher 	SSTAT(rx_bytes),
1245527a6266SJeff Kirsher 	SSTAT(tx_bytes),
1246527a6266SJeff Kirsher 	SSTAT(rx_errors),
1247527a6266SJeff Kirsher 	SSTAT(tx_errors),
1248527a6266SJeff Kirsher 	SSTAT(rx_dropped),
1249527a6266SJeff Kirsher 	SSTAT(tx_dropped),
1250527a6266SJeff Kirsher 	MIBSTAT(good_octets_received),
1251527a6266SJeff Kirsher 	MIBSTAT(bad_octets_received),
1252527a6266SJeff Kirsher 	MIBSTAT(internal_mac_transmit_err),
1253527a6266SJeff Kirsher 	MIBSTAT(good_frames_received),
1254527a6266SJeff Kirsher 	MIBSTAT(bad_frames_received),
1255527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_received),
1256527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_received),
1257527a6266SJeff Kirsher 	MIBSTAT(frames_64_octets),
1258527a6266SJeff Kirsher 	MIBSTAT(frames_65_to_127_octets),
1259527a6266SJeff Kirsher 	MIBSTAT(frames_128_to_255_octets),
1260527a6266SJeff Kirsher 	MIBSTAT(frames_256_to_511_octets),
1261527a6266SJeff Kirsher 	MIBSTAT(frames_512_to_1023_octets),
1262527a6266SJeff Kirsher 	MIBSTAT(frames_1024_to_max_octets),
1263527a6266SJeff Kirsher 	MIBSTAT(good_octets_sent),
1264527a6266SJeff Kirsher 	MIBSTAT(good_frames_sent),
1265527a6266SJeff Kirsher 	MIBSTAT(excessive_collision),
1266527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_sent),
1267527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_sent),
1268527a6266SJeff Kirsher 	MIBSTAT(unrec_mac_control_received),
1269527a6266SJeff Kirsher 	MIBSTAT(fc_sent),
1270527a6266SJeff Kirsher 	MIBSTAT(good_fc_received),
1271527a6266SJeff Kirsher 	MIBSTAT(bad_fc_received),
1272527a6266SJeff Kirsher 	MIBSTAT(undersize_received),
1273527a6266SJeff Kirsher 	MIBSTAT(fragments_received),
1274527a6266SJeff Kirsher 	MIBSTAT(oversize_received),
1275527a6266SJeff Kirsher 	MIBSTAT(jabber_received),
1276527a6266SJeff Kirsher 	MIBSTAT(mac_receive_error),
1277527a6266SJeff Kirsher 	MIBSTAT(bad_crc_event),
1278527a6266SJeff Kirsher 	MIBSTAT(collision),
1279527a6266SJeff Kirsher 	MIBSTAT(late_collision),
1280302476c9SPaulius Zaleckas 	MIBSTAT(rx_discard),
1281302476c9SPaulius Zaleckas 	MIBSTAT(rx_overrun),
1282527a6266SJeff Kirsher };
1283527a6266SJeff Kirsher 
1284527a6266SJeff Kirsher static int
1285527a6266SJeff Kirsher mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1286527a6266SJeff Kirsher 			     struct ethtool_cmd *cmd)
1287527a6266SJeff Kirsher {
1288527a6266SJeff Kirsher 	int err;
1289527a6266SJeff Kirsher 
1290527a6266SJeff Kirsher 	err = phy_read_status(mp->phy);
1291527a6266SJeff Kirsher 	if (err == 0)
1292527a6266SJeff Kirsher 		err = phy_ethtool_gset(mp->phy, cmd);
1293527a6266SJeff Kirsher 
1294527a6266SJeff Kirsher 	/*
1295527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1296527a6266SJeff Kirsher 	 */
1297527a6266SJeff Kirsher 	cmd->supported &= ~SUPPORTED_1000baseT_Half;
1298527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1299527a6266SJeff Kirsher 
1300527a6266SJeff Kirsher 	return err;
1301527a6266SJeff Kirsher }
1302527a6266SJeff Kirsher 
1303527a6266SJeff Kirsher static int
1304527a6266SJeff Kirsher mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
1305527a6266SJeff Kirsher 				 struct ethtool_cmd *cmd)
1306527a6266SJeff Kirsher {
1307527a6266SJeff Kirsher 	u32 port_status;
1308527a6266SJeff Kirsher 
1309527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1310527a6266SJeff Kirsher 
1311527a6266SJeff Kirsher 	cmd->supported = SUPPORTED_MII;
1312527a6266SJeff Kirsher 	cmd->advertising = ADVERTISED_MII;
1313527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1314527a6266SJeff Kirsher 	case PORT_SPEED_10:
1315527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_10);
1316527a6266SJeff Kirsher 		break;
1317527a6266SJeff Kirsher 	case PORT_SPEED_100:
1318527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_100);
1319527a6266SJeff Kirsher 		break;
1320527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1321527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_1000);
1322527a6266SJeff Kirsher 		break;
1323527a6266SJeff Kirsher 	default:
1324527a6266SJeff Kirsher 		cmd->speed = -1;
1325527a6266SJeff Kirsher 		break;
1326527a6266SJeff Kirsher 	}
1327527a6266SJeff Kirsher 	cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
1328527a6266SJeff Kirsher 	cmd->port = PORT_MII;
1329527a6266SJeff Kirsher 	cmd->phy_address = 0;
1330527a6266SJeff Kirsher 	cmd->transceiver = XCVR_INTERNAL;
1331527a6266SJeff Kirsher 	cmd->autoneg = AUTONEG_DISABLE;
1332527a6266SJeff Kirsher 	cmd->maxtxpkt = 1;
1333527a6266SJeff Kirsher 	cmd->maxrxpkt = 1;
1334527a6266SJeff Kirsher 
1335527a6266SJeff Kirsher 	return 0;
1336527a6266SJeff Kirsher }
1337527a6266SJeff Kirsher 
13383871c387SMichael Stapelberg static void
13393871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13403871c387SMichael Stapelberg {
13413871c387SMichael Stapelberg 	struct mv643xx_eth_private *mp = netdev_priv(dev);
13423871c387SMichael Stapelberg 	wol->supported = 0;
13433871c387SMichael Stapelberg 	wol->wolopts = 0;
13443871c387SMichael Stapelberg 	if (mp->phy)
13453871c387SMichael Stapelberg 		phy_ethtool_get_wol(mp->phy, wol);
13463871c387SMichael Stapelberg }
13473871c387SMichael Stapelberg 
13483871c387SMichael Stapelberg static int
13493871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
13503871c387SMichael Stapelberg {
13513871c387SMichael Stapelberg 	struct mv643xx_eth_private *mp = netdev_priv(dev);
13523871c387SMichael Stapelberg 	int err;
13533871c387SMichael Stapelberg 
13543871c387SMichael Stapelberg 	if (mp->phy == NULL)
13553871c387SMichael Stapelberg 		return -EOPNOTSUPP;
13563871c387SMichael Stapelberg 
13573871c387SMichael Stapelberg 	err = phy_ethtool_set_wol(mp->phy, wol);
13583871c387SMichael Stapelberg 	/* Given that mv643xx_eth works without the marvell-specific PHY driver,
13593871c387SMichael Stapelberg 	 * this debugging hint is useful to have.
13603871c387SMichael Stapelberg 	 */
13613871c387SMichael Stapelberg 	if (err == -EOPNOTSUPP)
13623871c387SMichael Stapelberg 		netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
13633871c387SMichael Stapelberg 	return err;
13643871c387SMichael Stapelberg }
13653871c387SMichael Stapelberg 
1366527a6266SJeff Kirsher static int
1367527a6266SJeff Kirsher mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1368527a6266SJeff Kirsher {
1369527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1370527a6266SJeff Kirsher 
1371527a6266SJeff Kirsher 	if (mp->phy != NULL)
1372527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phy(mp, cmd);
1373527a6266SJeff Kirsher 	else
1374527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phyless(mp, cmd);
1375527a6266SJeff Kirsher }
1376527a6266SJeff Kirsher 
1377527a6266SJeff Kirsher static int
1378527a6266SJeff Kirsher mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1379527a6266SJeff Kirsher {
1380527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1381260055bbSPhil Sutter 	int ret;
1382527a6266SJeff Kirsher 
1383527a6266SJeff Kirsher 	if (mp->phy == NULL)
1384527a6266SJeff Kirsher 		return -EINVAL;
1385527a6266SJeff Kirsher 
1386527a6266SJeff Kirsher 	/*
1387527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1388527a6266SJeff Kirsher 	 */
1389527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1390527a6266SJeff Kirsher 
1391260055bbSPhil Sutter 	ret = phy_ethtool_sset(mp->phy, cmd);
1392260055bbSPhil Sutter 	if (!ret)
1393260055bbSPhil Sutter 		mv643xx_adjust_pscr(mp);
1394260055bbSPhil Sutter 	return ret;
1395527a6266SJeff Kirsher }
1396527a6266SJeff Kirsher 
1397527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1398527a6266SJeff Kirsher 				    struct ethtool_drvinfo *drvinfo)
1399527a6266SJeff Kirsher {
14006f39da2cSAxel Lin 	strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
14016f39da2cSAxel Lin 		sizeof(drvinfo->driver));
140268aad78cSRick Jones 	strlcpy(drvinfo->version, mv643xx_eth_driver_version,
14036f39da2cSAxel Lin 		sizeof(drvinfo->version));
14046f39da2cSAxel Lin 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
14056f39da2cSAxel Lin 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
1406527a6266SJeff Kirsher 	drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
1407527a6266SJeff Kirsher }
1408527a6266SJeff Kirsher 
1409527a6266SJeff Kirsher static int mv643xx_eth_nway_reset(struct net_device *dev)
1410527a6266SJeff Kirsher {
1411527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1412527a6266SJeff Kirsher 
1413527a6266SJeff Kirsher 	if (mp->phy == NULL)
1414527a6266SJeff Kirsher 		return -EINVAL;
1415527a6266SJeff Kirsher 
1416527a6266SJeff Kirsher 	return genphy_restart_aneg(mp->phy);
1417527a6266SJeff Kirsher }
1418527a6266SJeff Kirsher 
1419527a6266SJeff Kirsher static int
1420527a6266SJeff Kirsher mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1421527a6266SJeff Kirsher {
1422527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1423527a6266SJeff Kirsher 
1424527a6266SJeff Kirsher 	ec->rx_coalesce_usecs = get_rx_coal(mp);
1425527a6266SJeff Kirsher 	ec->tx_coalesce_usecs = get_tx_coal(mp);
1426527a6266SJeff Kirsher 
1427527a6266SJeff Kirsher 	return 0;
1428527a6266SJeff Kirsher }
1429527a6266SJeff Kirsher 
1430527a6266SJeff Kirsher static int
1431527a6266SJeff Kirsher mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1432527a6266SJeff Kirsher {
1433527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1434527a6266SJeff Kirsher 
1435527a6266SJeff Kirsher 	set_rx_coal(mp, ec->rx_coalesce_usecs);
1436527a6266SJeff Kirsher 	set_tx_coal(mp, ec->tx_coalesce_usecs);
1437527a6266SJeff Kirsher 
1438527a6266SJeff Kirsher 	return 0;
1439527a6266SJeff Kirsher }
1440527a6266SJeff Kirsher 
1441527a6266SJeff Kirsher static void
1442527a6266SJeff Kirsher mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1443527a6266SJeff Kirsher {
1444527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1445527a6266SJeff Kirsher 
1446527a6266SJeff Kirsher 	er->rx_max_pending = 4096;
1447527a6266SJeff Kirsher 	er->tx_max_pending = 4096;
1448527a6266SJeff Kirsher 
1449527a6266SJeff Kirsher 	er->rx_pending = mp->rx_ring_size;
1450527a6266SJeff Kirsher 	er->tx_pending = mp->tx_ring_size;
1451527a6266SJeff Kirsher }
1452527a6266SJeff Kirsher 
1453527a6266SJeff Kirsher static int
1454527a6266SJeff Kirsher mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1455527a6266SJeff Kirsher {
1456527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1457527a6266SJeff Kirsher 
1458527a6266SJeff Kirsher 	if (er->rx_mini_pending || er->rx_jumbo_pending)
1459527a6266SJeff Kirsher 		return -EINVAL;
1460527a6266SJeff Kirsher 
1461527a6266SJeff Kirsher 	mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1462527a6266SJeff Kirsher 	mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
1463527a6266SJeff Kirsher 
1464527a6266SJeff Kirsher 	if (netif_running(dev)) {
1465527a6266SJeff Kirsher 		mv643xx_eth_stop(dev);
1466527a6266SJeff Kirsher 		if (mv643xx_eth_open(dev)) {
1467527a6266SJeff Kirsher 			netdev_err(dev,
1468527a6266SJeff Kirsher 				   "fatal error on re-opening device after ring param change\n");
1469527a6266SJeff Kirsher 			return -ENOMEM;
1470527a6266SJeff Kirsher 		}
1471527a6266SJeff Kirsher 	}
1472527a6266SJeff Kirsher 
1473527a6266SJeff Kirsher 	return 0;
1474527a6266SJeff Kirsher }
1475527a6266SJeff Kirsher 
1476527a6266SJeff Kirsher 
1477527a6266SJeff Kirsher static int
1478c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
1479527a6266SJeff Kirsher {
1480527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
14813ad9b358SMichał Mirosław 	bool rx_csum = features & NETIF_F_RXCSUM;
1482527a6266SJeff Kirsher 
1483527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1484527a6266SJeff Kirsher 
1485527a6266SJeff Kirsher 	return 0;
1486527a6266SJeff Kirsher }
1487527a6266SJeff Kirsher 
1488527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev,
1489527a6266SJeff Kirsher 				    uint32_t stringset, uint8_t *data)
1490527a6266SJeff Kirsher {
1491527a6266SJeff Kirsher 	int i;
1492527a6266SJeff Kirsher 
1493527a6266SJeff Kirsher 	if (stringset == ETH_SS_STATS) {
1494527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1495527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
1496527a6266SJeff Kirsher 				mv643xx_eth_stats[i].stat_string,
1497527a6266SJeff Kirsher 				ETH_GSTRING_LEN);
1498527a6266SJeff Kirsher 		}
1499527a6266SJeff Kirsher 	}
1500527a6266SJeff Kirsher }
1501527a6266SJeff Kirsher 
1502527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1503527a6266SJeff Kirsher 					  struct ethtool_stats *stats,
1504527a6266SJeff Kirsher 					  uint64_t *data)
1505527a6266SJeff Kirsher {
1506527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1507527a6266SJeff Kirsher 	int i;
1508527a6266SJeff Kirsher 
1509527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
1510527a6266SJeff Kirsher 	mib_counters_update(mp);
1511527a6266SJeff Kirsher 
1512527a6266SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1513527a6266SJeff Kirsher 		const struct mv643xx_eth_stats *stat;
1514527a6266SJeff Kirsher 		void *p;
1515527a6266SJeff Kirsher 
1516527a6266SJeff Kirsher 		stat = mv643xx_eth_stats + i;
1517527a6266SJeff Kirsher 
1518527a6266SJeff Kirsher 		if (stat->netdev_off >= 0)
1519527a6266SJeff Kirsher 			p = ((void *)mp->dev) + stat->netdev_off;
1520527a6266SJeff Kirsher 		else
1521527a6266SJeff Kirsher 			p = ((void *)mp) + stat->mp_off;
1522527a6266SJeff Kirsher 
1523527a6266SJeff Kirsher 		data[i] = (stat->sizeof_stat == 8) ?
1524527a6266SJeff Kirsher 				*(uint64_t *)p : *(uint32_t *)p;
1525527a6266SJeff Kirsher 	}
1526527a6266SJeff Kirsher }
1527527a6266SJeff Kirsher 
1528527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
1529527a6266SJeff Kirsher {
1530527a6266SJeff Kirsher 	if (sset == ETH_SS_STATS)
1531527a6266SJeff Kirsher 		return ARRAY_SIZE(mv643xx_eth_stats);
1532527a6266SJeff Kirsher 
1533527a6266SJeff Kirsher 	return -EOPNOTSUPP;
1534527a6266SJeff Kirsher }
1535527a6266SJeff Kirsher 
1536527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1537527a6266SJeff Kirsher 	.get_settings		= mv643xx_eth_get_settings,
1538527a6266SJeff Kirsher 	.set_settings		= mv643xx_eth_set_settings,
1539527a6266SJeff Kirsher 	.get_drvinfo		= mv643xx_eth_get_drvinfo,
1540527a6266SJeff Kirsher 	.nway_reset		= mv643xx_eth_nway_reset,
1541527a6266SJeff Kirsher 	.get_link		= ethtool_op_get_link,
1542527a6266SJeff Kirsher 	.get_coalesce		= mv643xx_eth_get_coalesce,
1543527a6266SJeff Kirsher 	.set_coalesce		= mv643xx_eth_set_coalesce,
1544527a6266SJeff Kirsher 	.get_ringparam		= mv643xx_eth_get_ringparam,
1545527a6266SJeff Kirsher 	.set_ringparam		= mv643xx_eth_set_ringparam,
1546527a6266SJeff Kirsher 	.get_strings		= mv643xx_eth_get_strings,
1547527a6266SJeff Kirsher 	.get_ethtool_stats	= mv643xx_eth_get_ethtool_stats,
1548527a6266SJeff Kirsher 	.get_sset_count		= mv643xx_eth_get_sset_count,
1549ebad0a8dSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
15503871c387SMichael Stapelberg 	.get_wol                = mv643xx_eth_get_wol,
15513871c387SMichael Stapelberg 	.set_wol                = mv643xx_eth_set_wol,
1552527a6266SJeff Kirsher };
1553527a6266SJeff Kirsher 
1554527a6266SJeff Kirsher 
1555527a6266SJeff Kirsher /* address handling *********************************************************/
1556527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
1557527a6266SJeff Kirsher {
1558527a6266SJeff Kirsher 	unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1559527a6266SJeff Kirsher 	unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
1560527a6266SJeff Kirsher 
1561527a6266SJeff Kirsher 	addr[0] = (mac_h >> 24) & 0xff;
1562527a6266SJeff Kirsher 	addr[1] = (mac_h >> 16) & 0xff;
1563527a6266SJeff Kirsher 	addr[2] = (mac_h >> 8) & 0xff;
1564527a6266SJeff Kirsher 	addr[3] = mac_h & 0xff;
1565527a6266SJeff Kirsher 	addr[4] = (mac_l >> 8) & 0xff;
1566527a6266SJeff Kirsher 	addr[5] = mac_l & 0xff;
1567527a6266SJeff Kirsher }
1568527a6266SJeff Kirsher 
1569527a6266SJeff Kirsher static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
1570527a6266SJeff Kirsher {
1571527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_HIGH,
1572527a6266SJeff Kirsher 		(addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1573527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
1574527a6266SJeff Kirsher }
1575527a6266SJeff Kirsher 
1576527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev)
1577527a6266SJeff Kirsher {
1578527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1579527a6266SJeff Kirsher 	u32 nibbles;
1580527a6266SJeff Kirsher 
1581527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)
1582527a6266SJeff Kirsher 		return 0;
1583527a6266SJeff Kirsher 
1584527a6266SJeff Kirsher 	nibbles = 1 << (dev->dev_addr[5] & 0x0f);
1585527a6266SJeff Kirsher 	netdev_for_each_uc_addr(ha, dev) {
1586527a6266SJeff Kirsher 		if (memcmp(dev->dev_addr, ha->addr, 5))
1587527a6266SJeff Kirsher 			return 0;
1588527a6266SJeff Kirsher 		if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
1589527a6266SJeff Kirsher 			return 0;
1590527a6266SJeff Kirsher 
1591527a6266SJeff Kirsher 		nibbles |= 1 << (ha->addr[5] & 0x0f);
1592527a6266SJeff Kirsher 	}
1593527a6266SJeff Kirsher 
1594527a6266SJeff Kirsher 	return nibbles;
1595527a6266SJeff Kirsher }
1596527a6266SJeff Kirsher 
1597527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
1598527a6266SJeff Kirsher {
1599527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1600527a6266SJeff Kirsher 	u32 port_config;
1601527a6266SJeff Kirsher 	u32 nibbles;
1602527a6266SJeff Kirsher 	int i;
1603527a6266SJeff Kirsher 
1604527a6266SJeff Kirsher 	uc_addr_set(mp, dev->dev_addr);
1605527a6266SJeff Kirsher 
1606527a6266SJeff Kirsher 	port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1607527a6266SJeff Kirsher 
1608527a6266SJeff Kirsher 	nibbles = uc_addr_filter_mask(dev);
1609527a6266SJeff Kirsher 	if (!nibbles) {
1610527a6266SJeff Kirsher 		port_config |= UNICAST_PROMISCUOUS_MODE;
1611527a6266SJeff Kirsher 		nibbles = 0xffff;
1612527a6266SJeff Kirsher 	}
1613527a6266SJeff Kirsher 
1614527a6266SJeff Kirsher 	for (i = 0; i < 16; i += 4) {
1615527a6266SJeff Kirsher 		int off = UNICAST_TABLE(mp->port_num) + i;
1616527a6266SJeff Kirsher 		u32 v;
1617527a6266SJeff Kirsher 
1618527a6266SJeff Kirsher 		v = 0;
1619527a6266SJeff Kirsher 		if (nibbles & 1)
1620527a6266SJeff Kirsher 			v |= 0x00000001;
1621527a6266SJeff Kirsher 		if (nibbles & 2)
1622527a6266SJeff Kirsher 			v |= 0x00000100;
1623527a6266SJeff Kirsher 		if (nibbles & 4)
1624527a6266SJeff Kirsher 			v |= 0x00010000;
1625527a6266SJeff Kirsher 		if (nibbles & 8)
1626527a6266SJeff Kirsher 			v |= 0x01000000;
1627527a6266SJeff Kirsher 		nibbles >>= 4;
1628527a6266SJeff Kirsher 
1629527a6266SJeff Kirsher 		wrl(mp, off, v);
1630527a6266SJeff Kirsher 	}
1631527a6266SJeff Kirsher 
1632527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, port_config);
1633527a6266SJeff Kirsher }
1634527a6266SJeff Kirsher 
1635527a6266SJeff Kirsher static int addr_crc(unsigned char *addr)
1636527a6266SJeff Kirsher {
1637527a6266SJeff Kirsher 	int crc = 0;
1638527a6266SJeff Kirsher 	int i;
1639527a6266SJeff Kirsher 
1640527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
1641527a6266SJeff Kirsher 		int j;
1642527a6266SJeff Kirsher 
1643527a6266SJeff Kirsher 		crc = (crc ^ addr[i]) << 8;
1644527a6266SJeff Kirsher 		for (j = 7; j >= 0; j--) {
1645527a6266SJeff Kirsher 			if (crc & (0x100 << j))
1646527a6266SJeff Kirsher 				crc ^= 0x107 << j;
1647527a6266SJeff Kirsher 		}
1648527a6266SJeff Kirsher 	}
1649527a6266SJeff Kirsher 
1650527a6266SJeff Kirsher 	return crc;
1651527a6266SJeff Kirsher }
1652527a6266SJeff Kirsher 
1653527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
1654527a6266SJeff Kirsher {
1655527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1656527a6266SJeff Kirsher 	u32 *mc_spec;
1657527a6266SJeff Kirsher 	u32 *mc_other;
1658527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1659527a6266SJeff Kirsher 	int i;
1660527a6266SJeff Kirsher 
1661527a6266SJeff Kirsher 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1662527a6266SJeff Kirsher 		int port_num;
1663527a6266SJeff Kirsher 		u32 accept;
1664527a6266SJeff Kirsher 
1665527a6266SJeff Kirsher oom:
1666527a6266SJeff Kirsher 		port_num = mp->port_num;
1667527a6266SJeff Kirsher 		accept = 0x01010101;
1668527a6266SJeff Kirsher 		for (i = 0; i < 0x100; i += 4) {
1669527a6266SJeff Kirsher 			wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1670527a6266SJeff Kirsher 			wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
1671527a6266SJeff Kirsher 		}
1672527a6266SJeff Kirsher 		return;
1673527a6266SJeff Kirsher 	}
1674527a6266SJeff Kirsher 
1675527a6266SJeff Kirsher 	mc_spec = kmalloc(0x200, GFP_ATOMIC);
1676527a6266SJeff Kirsher 	if (mc_spec == NULL)
1677527a6266SJeff Kirsher 		goto oom;
1678527a6266SJeff Kirsher 	mc_other = mc_spec + (0x100 >> 2);
1679527a6266SJeff Kirsher 
1680527a6266SJeff Kirsher 	memset(mc_spec, 0, 0x100);
1681527a6266SJeff Kirsher 	memset(mc_other, 0, 0x100);
1682527a6266SJeff Kirsher 
1683527a6266SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
1684527a6266SJeff Kirsher 		u8 *a = ha->addr;
1685527a6266SJeff Kirsher 		u32 *table;
1686527a6266SJeff Kirsher 		int entry;
1687527a6266SJeff Kirsher 
1688527a6266SJeff Kirsher 		if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1689527a6266SJeff Kirsher 			table = mc_spec;
1690527a6266SJeff Kirsher 			entry = a[5];
1691527a6266SJeff Kirsher 		} else {
1692527a6266SJeff Kirsher 			table = mc_other;
1693527a6266SJeff Kirsher 			entry = addr_crc(a);
1694527a6266SJeff Kirsher 		}
1695527a6266SJeff Kirsher 
1696527a6266SJeff Kirsher 		table[entry >> 2] |= 1 << (8 * (entry & 3));
1697527a6266SJeff Kirsher 	}
1698527a6266SJeff Kirsher 
1699527a6266SJeff Kirsher 	for (i = 0; i < 0x100; i += 4) {
1700527a6266SJeff Kirsher 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1701527a6266SJeff Kirsher 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1702527a6266SJeff Kirsher 	}
1703527a6266SJeff Kirsher 
1704527a6266SJeff Kirsher 	kfree(mc_spec);
1705527a6266SJeff Kirsher }
1706527a6266SJeff Kirsher 
1707527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1708527a6266SJeff Kirsher {
1709527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1710527a6266SJeff Kirsher 	mv643xx_eth_program_multicast_filter(dev);
1711527a6266SJeff Kirsher }
1712527a6266SJeff Kirsher 
1713527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1714527a6266SJeff Kirsher {
1715527a6266SJeff Kirsher 	struct sockaddr *sa = addr;
1716527a6266SJeff Kirsher 
1717527a6266SJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
1718504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
1719527a6266SJeff Kirsher 
1720527a6266SJeff Kirsher 	memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1721527a6266SJeff Kirsher 
1722527a6266SJeff Kirsher 	netif_addr_lock_bh(dev);
1723527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1724527a6266SJeff Kirsher 	netif_addr_unlock_bh(dev);
1725527a6266SJeff Kirsher 
1726527a6266SJeff Kirsher 	return 0;
1727527a6266SJeff Kirsher }
1728527a6266SJeff Kirsher 
1729527a6266SJeff Kirsher 
1730527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/
1731527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index)
1732527a6266SJeff Kirsher {
1733527a6266SJeff Kirsher 	struct rx_queue *rxq = mp->rxq + index;
1734527a6266SJeff Kirsher 	struct rx_desc *rx_desc;
1735527a6266SJeff Kirsher 	int size;
1736527a6266SJeff Kirsher 	int i;
1737527a6266SJeff Kirsher 
1738527a6266SJeff Kirsher 	rxq->index = index;
1739527a6266SJeff Kirsher 
1740527a6266SJeff Kirsher 	rxq->rx_ring_size = mp->rx_ring_size;
1741527a6266SJeff Kirsher 
1742527a6266SJeff Kirsher 	rxq->rx_desc_count = 0;
1743527a6266SJeff Kirsher 	rxq->rx_curr_desc = 0;
1744527a6266SJeff Kirsher 	rxq->rx_used_desc = 0;
1745527a6266SJeff Kirsher 
1746527a6266SJeff Kirsher 	size = rxq->rx_ring_size * sizeof(struct rx_desc);
1747527a6266SJeff Kirsher 
1748527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size) {
1749527a6266SJeff Kirsher 		rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1750527a6266SJeff Kirsher 						mp->rx_desc_sram_size);
1751527a6266SJeff Kirsher 		rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1752527a6266SJeff Kirsher 	} else {
1753527a6266SJeff Kirsher 		rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1754527a6266SJeff Kirsher 						       size, &rxq->rx_desc_dma,
1755527a6266SJeff Kirsher 						       GFP_KERNEL);
1756527a6266SJeff Kirsher 	}
1757527a6266SJeff Kirsher 
1758527a6266SJeff Kirsher 	if (rxq->rx_desc_area == NULL) {
1759527a6266SJeff Kirsher 		netdev_err(mp->dev,
1760527a6266SJeff Kirsher 			   "can't allocate rx ring (%d bytes)\n", size);
1761527a6266SJeff Kirsher 		goto out;
1762527a6266SJeff Kirsher 	}
1763527a6266SJeff Kirsher 	memset(rxq->rx_desc_area, 0, size);
1764527a6266SJeff Kirsher 
1765527a6266SJeff Kirsher 	rxq->rx_desc_area_size = size;
17669fa8e980SLubomir Rintel 	rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1767527a6266SJeff Kirsher 				    GFP_KERNEL);
1768b2adaca9SJoe Perches 	if (rxq->rx_skb == NULL)
1769527a6266SJeff Kirsher 		goto out_free;
1770527a6266SJeff Kirsher 
177164699336SJoe Perches 	rx_desc = rxq->rx_desc_area;
1772527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1773527a6266SJeff Kirsher 		int nexti;
1774527a6266SJeff Kirsher 
1775527a6266SJeff Kirsher 		nexti = i + 1;
1776527a6266SJeff Kirsher 		if (nexti == rxq->rx_ring_size)
1777527a6266SJeff Kirsher 			nexti = 0;
1778527a6266SJeff Kirsher 
1779527a6266SJeff Kirsher 		rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1780527a6266SJeff Kirsher 					nexti * sizeof(struct rx_desc);
1781527a6266SJeff Kirsher 	}
1782527a6266SJeff Kirsher 
1783527a6266SJeff Kirsher 	return 0;
1784527a6266SJeff Kirsher 
1785527a6266SJeff Kirsher 
1786527a6266SJeff Kirsher out_free:
1787527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size)
1788527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1789527a6266SJeff Kirsher 	else
1790527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, size,
1791527a6266SJeff Kirsher 				  rxq->rx_desc_area,
1792527a6266SJeff Kirsher 				  rxq->rx_desc_dma);
1793527a6266SJeff Kirsher 
1794527a6266SJeff Kirsher out:
1795527a6266SJeff Kirsher 	return -ENOMEM;
1796527a6266SJeff Kirsher }
1797527a6266SJeff Kirsher 
1798527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq)
1799527a6266SJeff Kirsher {
1800527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1801527a6266SJeff Kirsher 	int i;
1802527a6266SJeff Kirsher 
1803527a6266SJeff Kirsher 	rxq_disable(rxq);
1804527a6266SJeff Kirsher 
1805527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1806527a6266SJeff Kirsher 		if (rxq->rx_skb[i]) {
1807527a6266SJeff Kirsher 			dev_kfree_skb(rxq->rx_skb[i]);
1808527a6266SJeff Kirsher 			rxq->rx_desc_count--;
1809527a6266SJeff Kirsher 		}
1810527a6266SJeff Kirsher 	}
1811527a6266SJeff Kirsher 
1812527a6266SJeff Kirsher 	if (rxq->rx_desc_count) {
1813527a6266SJeff Kirsher 		netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
1814527a6266SJeff Kirsher 			   rxq->rx_desc_count);
1815527a6266SJeff Kirsher 	}
1816527a6266SJeff Kirsher 
1817527a6266SJeff Kirsher 	if (rxq->index == 0 &&
1818527a6266SJeff Kirsher 	    rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1819527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1820527a6266SJeff Kirsher 	else
1821527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
1822527a6266SJeff Kirsher 				  rxq->rx_desc_area, rxq->rx_desc_dma);
1823527a6266SJeff Kirsher 
1824527a6266SJeff Kirsher 	kfree(rxq->rx_skb);
1825527a6266SJeff Kirsher }
1826527a6266SJeff Kirsher 
1827527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index)
1828527a6266SJeff Kirsher {
1829527a6266SJeff Kirsher 	struct tx_queue *txq = mp->txq + index;
1830527a6266SJeff Kirsher 	struct tx_desc *tx_desc;
1831527a6266SJeff Kirsher 	int size;
1832527a6266SJeff Kirsher 	int i;
1833527a6266SJeff Kirsher 
1834527a6266SJeff Kirsher 	txq->index = index;
1835527a6266SJeff Kirsher 
1836527a6266SJeff Kirsher 	txq->tx_ring_size = mp->tx_ring_size;
1837527a6266SJeff Kirsher 
1838527a6266SJeff Kirsher 	txq->tx_desc_count = 0;
1839527a6266SJeff Kirsher 	txq->tx_curr_desc = 0;
1840527a6266SJeff Kirsher 	txq->tx_used_desc = 0;
1841527a6266SJeff Kirsher 
1842527a6266SJeff Kirsher 	size = txq->tx_ring_size * sizeof(struct tx_desc);
1843527a6266SJeff Kirsher 
1844527a6266SJeff Kirsher 	if (index == 0 && size <= mp->tx_desc_sram_size) {
1845527a6266SJeff Kirsher 		txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1846527a6266SJeff Kirsher 						mp->tx_desc_sram_size);
1847527a6266SJeff Kirsher 		txq->tx_desc_dma = mp->tx_desc_sram_addr;
1848527a6266SJeff Kirsher 	} else {
1849527a6266SJeff Kirsher 		txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1850527a6266SJeff Kirsher 						       size, &txq->tx_desc_dma,
1851527a6266SJeff Kirsher 						       GFP_KERNEL);
1852527a6266SJeff Kirsher 	}
1853527a6266SJeff Kirsher 
1854527a6266SJeff Kirsher 	if (txq->tx_desc_area == NULL) {
1855527a6266SJeff Kirsher 		netdev_err(mp->dev,
1856527a6266SJeff Kirsher 			   "can't allocate tx ring (%d bytes)\n", size);
1857527a6266SJeff Kirsher 		return -ENOMEM;
1858527a6266SJeff Kirsher 	}
1859527a6266SJeff Kirsher 	memset(txq->tx_desc_area, 0, size);
1860527a6266SJeff Kirsher 
1861527a6266SJeff Kirsher 	txq->tx_desc_area_size = size;
1862527a6266SJeff Kirsher 
186364699336SJoe Perches 	tx_desc = txq->tx_desc_area;
1864527a6266SJeff Kirsher 	for (i = 0; i < txq->tx_ring_size; i++) {
1865527a6266SJeff Kirsher 		struct tx_desc *txd = tx_desc + i;
1866527a6266SJeff Kirsher 		int nexti;
1867527a6266SJeff Kirsher 
1868527a6266SJeff Kirsher 		nexti = i + 1;
1869527a6266SJeff Kirsher 		if (nexti == txq->tx_ring_size)
1870527a6266SJeff Kirsher 			nexti = 0;
1871527a6266SJeff Kirsher 
1872527a6266SJeff Kirsher 		txd->cmd_sts = 0;
1873527a6266SJeff Kirsher 		txd->next_desc_ptr = txq->tx_desc_dma +
1874527a6266SJeff Kirsher 					nexti * sizeof(struct tx_desc);
1875527a6266SJeff Kirsher 	}
1876527a6266SJeff Kirsher 
1877527a6266SJeff Kirsher 	skb_queue_head_init(&txq->tx_skb);
1878527a6266SJeff Kirsher 
1879527a6266SJeff Kirsher 	return 0;
1880527a6266SJeff Kirsher }
1881527a6266SJeff Kirsher 
1882527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq)
1883527a6266SJeff Kirsher {
1884527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1885527a6266SJeff Kirsher 
1886527a6266SJeff Kirsher 	txq_disable(txq);
1887527a6266SJeff Kirsher 	txq_reclaim(txq, txq->tx_ring_size, 1);
1888527a6266SJeff Kirsher 
1889527a6266SJeff Kirsher 	BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
1890527a6266SJeff Kirsher 
1891527a6266SJeff Kirsher 	if (txq->index == 0 &&
1892527a6266SJeff Kirsher 	    txq->tx_desc_area_size <= mp->tx_desc_sram_size)
1893527a6266SJeff Kirsher 		iounmap(txq->tx_desc_area);
1894527a6266SJeff Kirsher 	else
1895527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
1896527a6266SJeff Kirsher 				  txq->tx_desc_area, txq->tx_desc_dma);
1897527a6266SJeff Kirsher }
1898527a6266SJeff Kirsher 
1899527a6266SJeff Kirsher 
1900527a6266SJeff Kirsher /* netdev ops and related ***************************************************/
1901527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
1902527a6266SJeff Kirsher {
1903527a6266SJeff Kirsher 	u32 int_cause;
1904527a6266SJeff Kirsher 	u32 int_cause_ext;
1905527a6266SJeff Kirsher 
1906527a6266SJeff Kirsher 	int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
1907527a6266SJeff Kirsher 	if (int_cause == 0)
1908527a6266SJeff Kirsher 		return 0;
1909527a6266SJeff Kirsher 
1910527a6266SJeff Kirsher 	int_cause_ext = 0;
1911527a6266SJeff Kirsher 	if (int_cause & INT_EXT) {
1912527a6266SJeff Kirsher 		int_cause &= ~INT_EXT;
1913527a6266SJeff Kirsher 		int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
1914527a6266SJeff Kirsher 	}
1915527a6266SJeff Kirsher 
1916527a6266SJeff Kirsher 	if (int_cause) {
1917527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE, ~int_cause);
1918527a6266SJeff Kirsher 		mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
1919527a6266SJeff Kirsher 				~(rdlp(mp, TXQ_COMMAND) & 0xff);
1920527a6266SJeff Kirsher 		mp->work_rx |= (int_cause & INT_RX) >> 2;
1921527a6266SJeff Kirsher 	}
1922527a6266SJeff Kirsher 
1923527a6266SJeff Kirsher 	int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
1924527a6266SJeff Kirsher 	if (int_cause_ext) {
1925527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
1926527a6266SJeff Kirsher 		if (int_cause_ext & INT_EXT_LINK_PHY)
1927527a6266SJeff Kirsher 			mp->work_link = 1;
1928527a6266SJeff Kirsher 		mp->work_tx |= int_cause_ext & INT_EXT_TX;
1929527a6266SJeff Kirsher 	}
1930527a6266SJeff Kirsher 
1931527a6266SJeff Kirsher 	return 1;
1932527a6266SJeff Kirsher }
1933527a6266SJeff Kirsher 
1934527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
1935527a6266SJeff Kirsher {
1936527a6266SJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_id;
1937527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1938527a6266SJeff Kirsher 
1939527a6266SJeff Kirsher 	if (unlikely(!mv643xx_eth_collect_events(mp)))
1940527a6266SJeff Kirsher 		return IRQ_NONE;
1941527a6266SJeff Kirsher 
1942527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
1943527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
1944527a6266SJeff Kirsher 
1945527a6266SJeff Kirsher 	return IRQ_HANDLED;
1946527a6266SJeff Kirsher }
1947527a6266SJeff Kirsher 
1948527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp)
1949527a6266SJeff Kirsher {
1950527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
1951527a6266SJeff Kirsher 	u32 port_status;
1952527a6266SJeff Kirsher 	int speed;
1953527a6266SJeff Kirsher 	int duplex;
1954527a6266SJeff Kirsher 	int fc;
1955527a6266SJeff Kirsher 
1956527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1957527a6266SJeff Kirsher 	if (!(port_status & LINK_UP)) {
1958527a6266SJeff Kirsher 		if (netif_carrier_ok(dev)) {
1959527a6266SJeff Kirsher 			int i;
1960527a6266SJeff Kirsher 
1961527a6266SJeff Kirsher 			netdev_info(dev, "link down\n");
1962527a6266SJeff Kirsher 
1963527a6266SJeff Kirsher 			netif_carrier_off(dev);
1964527a6266SJeff Kirsher 
1965527a6266SJeff Kirsher 			for (i = 0; i < mp->txq_count; i++) {
1966527a6266SJeff Kirsher 				struct tx_queue *txq = mp->txq + i;
1967527a6266SJeff Kirsher 
1968527a6266SJeff Kirsher 				txq_reclaim(txq, txq->tx_ring_size, 1);
1969527a6266SJeff Kirsher 				txq_reset_hw_ptr(txq);
1970527a6266SJeff Kirsher 			}
1971527a6266SJeff Kirsher 		}
1972527a6266SJeff Kirsher 		return;
1973527a6266SJeff Kirsher 	}
1974527a6266SJeff Kirsher 
1975527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1976527a6266SJeff Kirsher 	case PORT_SPEED_10:
1977527a6266SJeff Kirsher 		speed = 10;
1978527a6266SJeff Kirsher 		break;
1979527a6266SJeff Kirsher 	case PORT_SPEED_100:
1980527a6266SJeff Kirsher 		speed = 100;
1981527a6266SJeff Kirsher 		break;
1982527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1983527a6266SJeff Kirsher 		speed = 1000;
1984527a6266SJeff Kirsher 		break;
1985527a6266SJeff Kirsher 	default:
1986527a6266SJeff Kirsher 		speed = -1;
1987527a6266SJeff Kirsher 		break;
1988527a6266SJeff Kirsher 	}
1989527a6266SJeff Kirsher 	duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
1990527a6266SJeff Kirsher 	fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
1991527a6266SJeff Kirsher 
1992527a6266SJeff Kirsher 	netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
1993527a6266SJeff Kirsher 		    speed, duplex ? "full" : "half", fc ? "en" : "dis");
1994527a6266SJeff Kirsher 
1995527a6266SJeff Kirsher 	if (!netif_carrier_ok(dev))
1996527a6266SJeff Kirsher 		netif_carrier_on(dev);
1997527a6266SJeff Kirsher }
1998527a6266SJeff Kirsher 
1999527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
2000527a6266SJeff Kirsher {
2001527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2002527a6266SJeff Kirsher 	int work_done;
2003527a6266SJeff Kirsher 
2004527a6266SJeff Kirsher 	mp = container_of(napi, struct mv643xx_eth_private, napi);
2005527a6266SJeff Kirsher 
2006527a6266SJeff Kirsher 	if (unlikely(mp->oom)) {
2007527a6266SJeff Kirsher 		mp->oom = 0;
2008527a6266SJeff Kirsher 		del_timer(&mp->rx_oom);
2009527a6266SJeff Kirsher 	}
2010527a6266SJeff Kirsher 
2011527a6266SJeff Kirsher 	work_done = 0;
2012527a6266SJeff Kirsher 	while (work_done < budget) {
2013527a6266SJeff Kirsher 		u8 queue_mask;
2014527a6266SJeff Kirsher 		int queue;
2015527a6266SJeff Kirsher 		int work_tbd;
2016527a6266SJeff Kirsher 
2017527a6266SJeff Kirsher 		if (mp->work_link) {
2018527a6266SJeff Kirsher 			mp->work_link = 0;
2019527a6266SJeff Kirsher 			handle_link_event(mp);
2020527a6266SJeff Kirsher 			work_done++;
2021527a6266SJeff Kirsher 			continue;
2022527a6266SJeff Kirsher 		}
2023527a6266SJeff Kirsher 
2024527a6266SJeff Kirsher 		queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2025527a6266SJeff Kirsher 		if (likely(!mp->oom))
2026527a6266SJeff Kirsher 			queue_mask |= mp->work_rx_refill;
2027527a6266SJeff Kirsher 
2028527a6266SJeff Kirsher 		if (!queue_mask) {
2029527a6266SJeff Kirsher 			if (mv643xx_eth_collect_events(mp))
2030527a6266SJeff Kirsher 				continue;
2031527a6266SJeff Kirsher 			break;
2032527a6266SJeff Kirsher 		}
2033527a6266SJeff Kirsher 
2034527a6266SJeff Kirsher 		queue = fls(queue_mask) - 1;
2035527a6266SJeff Kirsher 		queue_mask = 1 << queue;
2036527a6266SJeff Kirsher 
2037527a6266SJeff Kirsher 		work_tbd = budget - work_done;
2038527a6266SJeff Kirsher 		if (work_tbd > 16)
2039527a6266SJeff Kirsher 			work_tbd = 16;
2040527a6266SJeff Kirsher 
2041527a6266SJeff Kirsher 		if (mp->work_tx_end & queue_mask) {
2042527a6266SJeff Kirsher 			txq_kick(mp->txq + queue);
2043527a6266SJeff Kirsher 		} else if (mp->work_tx & queue_mask) {
2044527a6266SJeff Kirsher 			work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2045527a6266SJeff Kirsher 			txq_maybe_wake(mp->txq + queue);
2046527a6266SJeff Kirsher 		} else if (mp->work_rx & queue_mask) {
2047527a6266SJeff Kirsher 			work_done += rxq_process(mp->rxq + queue, work_tbd);
2048527a6266SJeff Kirsher 		} else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
2049527a6266SJeff Kirsher 			work_done += rxq_refill(mp->rxq + queue, work_tbd);
2050527a6266SJeff Kirsher 		} else {
2051527a6266SJeff Kirsher 			BUG();
2052527a6266SJeff Kirsher 		}
2053527a6266SJeff Kirsher 	}
2054527a6266SJeff Kirsher 
2055527a6266SJeff Kirsher 	if (work_done < budget) {
2056527a6266SJeff Kirsher 		if (mp->oom)
2057527a6266SJeff Kirsher 			mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2058527a6266SJeff Kirsher 		napi_complete(napi);
2059527a6266SJeff Kirsher 		wrlp(mp, INT_MASK, mp->int_mask);
2060527a6266SJeff Kirsher 	}
2061527a6266SJeff Kirsher 
2062527a6266SJeff Kirsher 	return work_done;
2063527a6266SJeff Kirsher }
2064527a6266SJeff Kirsher 
2065527a6266SJeff Kirsher static inline void oom_timer_wrapper(unsigned long data)
2066527a6266SJeff Kirsher {
2067527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)data;
2068527a6266SJeff Kirsher 
2069527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2070527a6266SJeff Kirsher }
2071527a6266SJeff Kirsher 
2072527a6266SJeff Kirsher static void phy_reset(struct mv643xx_eth_private *mp)
2073527a6266SJeff Kirsher {
2074527a6266SJeff Kirsher 	int data;
2075527a6266SJeff Kirsher 
2076527a6266SJeff Kirsher 	data = phy_read(mp->phy, MII_BMCR);
2077527a6266SJeff Kirsher 	if (data < 0)
2078527a6266SJeff Kirsher 		return;
2079527a6266SJeff Kirsher 
2080527a6266SJeff Kirsher 	data |= BMCR_RESET;
2081527a6266SJeff Kirsher 	if (phy_write(mp->phy, MII_BMCR, data) < 0)
2082527a6266SJeff Kirsher 		return;
2083527a6266SJeff Kirsher 
2084527a6266SJeff Kirsher 	do {
2085527a6266SJeff Kirsher 		data = phy_read(mp->phy, MII_BMCR);
2086527a6266SJeff Kirsher 	} while (data >= 0 && data & BMCR_RESET);
2087527a6266SJeff Kirsher }
2088527a6266SJeff Kirsher 
2089527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp)
2090527a6266SJeff Kirsher {
2091527a6266SJeff Kirsher 	u32 pscr;
2092527a6266SJeff Kirsher 	int i;
2093527a6266SJeff Kirsher 
2094527a6266SJeff Kirsher 	/*
2095527a6266SJeff Kirsher 	 * Perform PHY reset, if there is a PHY.
2096527a6266SJeff Kirsher 	 */
2097527a6266SJeff Kirsher 	if (mp->phy != NULL) {
2098527a6266SJeff Kirsher 		struct ethtool_cmd cmd;
2099527a6266SJeff Kirsher 
2100527a6266SJeff Kirsher 		mv643xx_eth_get_settings(mp->dev, &cmd);
2101527a6266SJeff Kirsher 		phy_reset(mp);
2102527a6266SJeff Kirsher 		mv643xx_eth_set_settings(mp->dev, &cmd);
2103527a6266SJeff Kirsher 	}
2104527a6266SJeff Kirsher 
2105527a6266SJeff Kirsher 	/*
2106527a6266SJeff Kirsher 	 * Configure basic link parameters.
2107527a6266SJeff Kirsher 	 */
2108527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2109527a6266SJeff Kirsher 
2110527a6266SJeff Kirsher 	pscr |= SERIAL_PORT_ENABLE;
2111527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2112527a6266SJeff Kirsher 
2113527a6266SJeff Kirsher 	pscr |= DO_NOT_FORCE_LINK_FAIL;
2114527a6266SJeff Kirsher 	if (mp->phy == NULL)
2115527a6266SJeff Kirsher 		pscr |= FORCE_LINK_PASS;
2116527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2117527a6266SJeff Kirsher 
2118527a6266SJeff Kirsher 	/*
2119527a6266SJeff Kirsher 	 * Configure TX path and queues.
2120527a6266SJeff Kirsher 	 */
2121527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2122527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2123527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
2124527a6266SJeff Kirsher 
2125527a6266SJeff Kirsher 		txq_reset_hw_ptr(txq);
2126527a6266SJeff Kirsher 		txq_set_rate(txq, 1000000000, 16777216);
2127527a6266SJeff Kirsher 		txq_set_fixed_prio_mode(txq);
2128527a6266SJeff Kirsher 	}
2129527a6266SJeff Kirsher 
2130527a6266SJeff Kirsher 	/*
2131527a6266SJeff Kirsher 	 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2132527a6266SJeff Kirsher 	 * frames to RX queue #0, and include the pseudo-header when
2133527a6266SJeff Kirsher 	 * calculating receive checksums.
2134527a6266SJeff Kirsher 	 */
2135527a6266SJeff Kirsher 	mv643xx_eth_set_features(mp->dev, mp->dev->features);
2136527a6266SJeff Kirsher 
2137527a6266SJeff Kirsher 	/*
2138527a6266SJeff Kirsher 	 * Treat BPDUs as normal multicasts, and disable partition mode.
2139527a6266SJeff Kirsher 	 */
2140527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
2141527a6266SJeff Kirsher 
2142527a6266SJeff Kirsher 	/*
2143527a6266SJeff Kirsher 	 * Add configured unicast addresses to address filter table.
2144527a6266SJeff Kirsher 	 */
2145527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(mp->dev);
2146527a6266SJeff Kirsher 
2147527a6266SJeff Kirsher 	/*
2148527a6266SJeff Kirsher 	 * Enable the receive queues.
2149527a6266SJeff Kirsher 	 */
2150527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2151527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
2152527a6266SJeff Kirsher 		u32 addr;
2153527a6266SJeff Kirsher 
2154527a6266SJeff Kirsher 		addr = (u32)rxq->rx_desc_dma;
2155527a6266SJeff Kirsher 		addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2156527a6266SJeff Kirsher 		wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
2157527a6266SJeff Kirsher 
2158527a6266SJeff Kirsher 		rxq_enable(rxq);
2159527a6266SJeff Kirsher 	}
2160527a6266SJeff Kirsher }
2161527a6266SJeff Kirsher 
2162527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2163527a6266SJeff Kirsher {
2164527a6266SJeff Kirsher 	int skb_size;
2165527a6266SJeff Kirsher 
2166527a6266SJeff Kirsher 	/*
2167527a6266SJeff Kirsher 	 * Reserve 2+14 bytes for an ethernet header (the hardware
2168527a6266SJeff Kirsher 	 * automatically prepends 2 bytes of dummy data to each
2169527a6266SJeff Kirsher 	 * received packet), 16 bytes for up to four VLAN tags, and
2170527a6266SJeff Kirsher 	 * 4 bytes for the trailing FCS -- 36 bytes total.
2171527a6266SJeff Kirsher 	 */
2172527a6266SJeff Kirsher 	skb_size = mp->dev->mtu + 36;
2173527a6266SJeff Kirsher 
2174527a6266SJeff Kirsher 	/*
2175527a6266SJeff Kirsher 	 * Make sure that the skb size is a multiple of 8 bytes, as
2176527a6266SJeff Kirsher 	 * the lower three bits of the receive descriptor's buffer
2177527a6266SJeff Kirsher 	 * size field are ignored by the hardware.
2178527a6266SJeff Kirsher 	 */
2179527a6266SJeff Kirsher 	mp->skb_size = (skb_size + 7) & ~7;
2180527a6266SJeff Kirsher 
2181527a6266SJeff Kirsher 	/*
2182527a6266SJeff Kirsher 	 * If NET_SKB_PAD is smaller than a cache line,
2183527a6266SJeff Kirsher 	 * netdev_alloc_skb() will cause skb->data to be misaligned
2184527a6266SJeff Kirsher 	 * to a cache line boundary.  If this is the case, include
2185527a6266SJeff Kirsher 	 * some extra space to allow re-aligning the data area.
2186527a6266SJeff Kirsher 	 */
2187527a6266SJeff Kirsher 	mp->skb_size += SKB_DMA_REALIGN;
2188527a6266SJeff Kirsher }
2189527a6266SJeff Kirsher 
2190527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev)
2191527a6266SJeff Kirsher {
2192527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2193527a6266SJeff Kirsher 	int err;
2194527a6266SJeff Kirsher 	int i;
2195527a6266SJeff Kirsher 
2196527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE, 0);
2197527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE_EXT, 0);
2198527a6266SJeff Kirsher 	rdlp(mp, INT_CAUSE_EXT);
2199527a6266SJeff Kirsher 
2200527a6266SJeff Kirsher 	err = request_irq(dev->irq, mv643xx_eth_irq,
2201527a6266SJeff Kirsher 			  IRQF_SHARED, dev->name, dev);
2202527a6266SJeff Kirsher 	if (err) {
2203527a6266SJeff Kirsher 		netdev_err(dev, "can't assign irq\n");
2204527a6266SJeff Kirsher 		return -EAGAIN;
2205527a6266SJeff Kirsher 	}
2206527a6266SJeff Kirsher 
2207527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2208527a6266SJeff Kirsher 
2209527a6266SJeff Kirsher 	napi_enable(&mp->napi);
2210527a6266SJeff Kirsher 
2211527a6266SJeff Kirsher 	mp->int_mask = INT_EXT;
2212527a6266SJeff Kirsher 
2213527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2214527a6266SJeff Kirsher 		err = rxq_init(mp, i);
2215527a6266SJeff Kirsher 		if (err) {
2216527a6266SJeff Kirsher 			while (--i >= 0)
2217527a6266SJeff Kirsher 				rxq_deinit(mp->rxq + i);
2218527a6266SJeff Kirsher 			goto out;
2219527a6266SJeff Kirsher 		}
2220527a6266SJeff Kirsher 
2221527a6266SJeff Kirsher 		rxq_refill(mp->rxq + i, INT_MAX);
2222527a6266SJeff Kirsher 		mp->int_mask |= INT_RX_0 << i;
2223527a6266SJeff Kirsher 	}
2224527a6266SJeff Kirsher 
2225527a6266SJeff Kirsher 	if (mp->oom) {
2226527a6266SJeff Kirsher 		mp->rx_oom.expires = jiffies + (HZ / 10);
2227527a6266SJeff Kirsher 		add_timer(&mp->rx_oom);
2228527a6266SJeff Kirsher 	}
2229527a6266SJeff Kirsher 
2230527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2231527a6266SJeff Kirsher 		err = txq_init(mp, i);
2232527a6266SJeff Kirsher 		if (err) {
2233527a6266SJeff Kirsher 			while (--i >= 0)
2234527a6266SJeff Kirsher 				txq_deinit(mp->txq + i);
2235527a6266SJeff Kirsher 			goto out_free;
2236527a6266SJeff Kirsher 		}
2237527a6266SJeff Kirsher 		mp->int_mask |= INT_TX_END_0 << i;
2238527a6266SJeff Kirsher 	}
2239527a6266SJeff Kirsher 
2240527a6266SJeff Kirsher 	port_start(mp);
2241527a6266SJeff Kirsher 
2242527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
2243527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2244527a6266SJeff Kirsher 
2245527a6266SJeff Kirsher 	return 0;
2246527a6266SJeff Kirsher 
2247527a6266SJeff Kirsher 
2248527a6266SJeff Kirsher out_free:
2249527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2250527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2251527a6266SJeff Kirsher out:
2252527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2253527a6266SJeff Kirsher 
2254527a6266SJeff Kirsher 	return err;
2255527a6266SJeff Kirsher }
2256527a6266SJeff Kirsher 
2257527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp)
2258527a6266SJeff Kirsher {
2259527a6266SJeff Kirsher 	unsigned int data;
2260527a6266SJeff Kirsher 	int i;
2261527a6266SJeff Kirsher 
2262527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2263527a6266SJeff Kirsher 		rxq_disable(mp->rxq + i);
2264527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2265527a6266SJeff Kirsher 		txq_disable(mp->txq + i);
2266527a6266SJeff Kirsher 
2267527a6266SJeff Kirsher 	while (1) {
2268527a6266SJeff Kirsher 		u32 ps = rdlp(mp, PORT_STATUS);
2269527a6266SJeff Kirsher 
2270527a6266SJeff Kirsher 		if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2271527a6266SJeff Kirsher 			break;
2272527a6266SJeff Kirsher 		udelay(10);
2273527a6266SJeff Kirsher 	}
2274527a6266SJeff Kirsher 
2275527a6266SJeff Kirsher 	/* Reset the Enable bit in the Configuration Register */
2276527a6266SJeff Kirsher 	data = rdlp(mp, PORT_SERIAL_CONTROL);
2277527a6266SJeff Kirsher 	data &= ~(SERIAL_PORT_ENABLE		|
2278527a6266SJeff Kirsher 		  DO_NOT_FORCE_LINK_FAIL	|
2279527a6266SJeff Kirsher 		  FORCE_LINK_PASS);
2280527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, data);
2281527a6266SJeff Kirsher }
2282527a6266SJeff Kirsher 
2283527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev)
2284527a6266SJeff Kirsher {
2285527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2286527a6266SJeff Kirsher 	int i;
2287527a6266SJeff Kirsher 
2288527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, 0x00000000);
2289527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2290527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2291527a6266SJeff Kirsher 
2292527a6266SJeff Kirsher 	napi_disable(&mp->napi);
2293527a6266SJeff Kirsher 
2294527a6266SJeff Kirsher 	del_timer_sync(&mp->rx_oom);
2295527a6266SJeff Kirsher 
2296527a6266SJeff Kirsher 	netif_carrier_off(dev);
2297527a6266SJeff Kirsher 
2298527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2299527a6266SJeff Kirsher 
2300527a6266SJeff Kirsher 	port_reset(mp);
2301527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
2302527a6266SJeff Kirsher 	mib_counters_update(mp);
2303527a6266SJeff Kirsher 	del_timer_sync(&mp->mib_counters_timer);
2304527a6266SJeff Kirsher 
2305527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2306527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2307527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2308527a6266SJeff Kirsher 		txq_deinit(mp->txq + i);
2309527a6266SJeff Kirsher 
2310527a6266SJeff Kirsher 	return 0;
2311527a6266SJeff Kirsher }
2312527a6266SJeff Kirsher 
2313527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2314527a6266SJeff Kirsher {
2315527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2316260055bbSPhil Sutter 	int ret;
2317527a6266SJeff Kirsher 
2318260055bbSPhil Sutter 	if (mp->phy == NULL)
2319260055bbSPhil Sutter 		return -ENOTSUPP;
2320527a6266SJeff Kirsher 
2321260055bbSPhil Sutter 	ret = phy_mii_ioctl(mp->phy, ifr, cmd);
2322260055bbSPhil Sutter 	if (!ret)
2323260055bbSPhil Sutter 		mv643xx_adjust_pscr(mp);
2324260055bbSPhil Sutter 	return ret;
2325527a6266SJeff Kirsher }
2326527a6266SJeff Kirsher 
2327527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2328527a6266SJeff Kirsher {
2329527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2330527a6266SJeff Kirsher 
2331527a6266SJeff Kirsher 	if (new_mtu < 64 || new_mtu > 9500)
2332527a6266SJeff Kirsher 		return -EINVAL;
2333527a6266SJeff Kirsher 
2334527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2335527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2336527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2337527a6266SJeff Kirsher 
2338527a6266SJeff Kirsher 	if (!netif_running(dev))
2339527a6266SJeff Kirsher 		return 0;
2340527a6266SJeff Kirsher 
2341527a6266SJeff Kirsher 	/*
2342527a6266SJeff Kirsher 	 * Stop and then re-open the interface. This will allocate RX
2343527a6266SJeff Kirsher 	 * skbs of the new MTU.
2344527a6266SJeff Kirsher 	 * There is a possible danger that the open will not succeed,
2345527a6266SJeff Kirsher 	 * due to memory being full.
2346527a6266SJeff Kirsher 	 */
2347527a6266SJeff Kirsher 	mv643xx_eth_stop(dev);
2348527a6266SJeff Kirsher 	if (mv643xx_eth_open(dev)) {
2349527a6266SJeff Kirsher 		netdev_err(dev,
2350527a6266SJeff Kirsher 			   "fatal error on re-opening device after MTU change\n");
2351527a6266SJeff Kirsher 	}
2352527a6266SJeff Kirsher 
2353527a6266SJeff Kirsher 	return 0;
2354527a6266SJeff Kirsher }
2355527a6266SJeff Kirsher 
2356527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly)
2357527a6266SJeff Kirsher {
2358527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2359527a6266SJeff Kirsher 
2360527a6266SJeff Kirsher 	mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2361527a6266SJeff Kirsher 	if (netif_running(mp->dev)) {
2362527a6266SJeff Kirsher 		netif_tx_stop_all_queues(mp->dev);
2363527a6266SJeff Kirsher 		port_reset(mp);
2364527a6266SJeff Kirsher 		port_start(mp);
2365527a6266SJeff Kirsher 		netif_tx_wake_all_queues(mp->dev);
2366527a6266SJeff Kirsher 	}
2367527a6266SJeff Kirsher }
2368527a6266SJeff Kirsher 
2369527a6266SJeff Kirsher static void mv643xx_eth_tx_timeout(struct net_device *dev)
2370527a6266SJeff Kirsher {
2371527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2372527a6266SJeff Kirsher 
2373527a6266SJeff Kirsher 	netdev_info(dev, "tx timeout\n");
2374527a6266SJeff Kirsher 
2375527a6266SJeff Kirsher 	schedule_work(&mp->tx_timeout_task);
2376527a6266SJeff Kirsher }
2377527a6266SJeff Kirsher 
2378527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2379527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev)
2380527a6266SJeff Kirsher {
2381527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2382527a6266SJeff Kirsher 
2383527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2384527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2385527a6266SJeff Kirsher 
2386527a6266SJeff Kirsher 	mv643xx_eth_irq(dev->irq, dev);
2387527a6266SJeff Kirsher 
2388527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2389527a6266SJeff Kirsher }
2390527a6266SJeff Kirsher #endif
2391527a6266SJeff Kirsher 
2392527a6266SJeff Kirsher 
2393527a6266SJeff Kirsher /* platform glue ************************************************************/
2394527a6266SJeff Kirsher static void
2395527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
239663a9332bSAndrew Lunn 			      const struct mbus_dram_target_info *dram)
2397527a6266SJeff Kirsher {
2398527a6266SJeff Kirsher 	void __iomem *base = msp->base;
2399527a6266SJeff Kirsher 	u32 win_enable;
2400527a6266SJeff Kirsher 	u32 win_protect;
2401527a6266SJeff Kirsher 	int i;
2402527a6266SJeff Kirsher 
2403527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
2404527a6266SJeff Kirsher 		writel(0, base + WINDOW_BASE(i));
2405527a6266SJeff Kirsher 		writel(0, base + WINDOW_SIZE(i));
2406527a6266SJeff Kirsher 		if (i < 4)
2407527a6266SJeff Kirsher 			writel(0, base + WINDOW_REMAP_HIGH(i));
2408527a6266SJeff Kirsher 	}
2409527a6266SJeff Kirsher 
2410527a6266SJeff Kirsher 	win_enable = 0x3f;
2411527a6266SJeff Kirsher 	win_protect = 0;
2412527a6266SJeff Kirsher 
2413527a6266SJeff Kirsher 	for (i = 0; i < dram->num_cs; i++) {
241463a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
2415527a6266SJeff Kirsher 
2416527a6266SJeff Kirsher 		writel((cs->base & 0xffff0000) |
2417527a6266SJeff Kirsher 			(cs->mbus_attr << 8) |
2418527a6266SJeff Kirsher 			dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2419527a6266SJeff Kirsher 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2420527a6266SJeff Kirsher 
2421527a6266SJeff Kirsher 		win_enable &= ~(1 << i);
2422527a6266SJeff Kirsher 		win_protect |= 3 << (2 * i);
2423527a6266SJeff Kirsher 	}
2424527a6266SJeff Kirsher 
2425527a6266SJeff Kirsher 	writel(win_enable, base + WINDOW_BAR_ENABLE);
2426527a6266SJeff Kirsher 	msp->win_protect = win_protect;
2427527a6266SJeff Kirsher }
2428527a6266SJeff Kirsher 
2429527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2430527a6266SJeff Kirsher {
2431527a6266SJeff Kirsher 	/*
2432527a6266SJeff Kirsher 	 * Check whether we have a 14-bit coal limit field in bits
2433527a6266SJeff Kirsher 	 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2434527a6266SJeff Kirsher 	 * SDMA config register.
2435527a6266SJeff Kirsher 	 */
2436527a6266SJeff Kirsher 	writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2437527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
2438527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 1;
2439527a6266SJeff Kirsher 	else
2440527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 0;
2441527a6266SJeff Kirsher 
2442527a6266SJeff Kirsher 	/*
2443527a6266SJeff Kirsher 	 * Check whether the MAC supports TX rate control, and if
2444527a6266SJeff Kirsher 	 * yes, whether its associated registers are in the old or
2445527a6266SJeff Kirsher 	 * the new place.
2446527a6266SJeff Kirsher 	 */
2447527a6266SJeff Kirsher 	writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2448527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
2449527a6266SJeff Kirsher 		msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2450527a6266SJeff Kirsher 	} else {
2451527a6266SJeff Kirsher 		writel(7, msp->base + 0x0400 + TX_BW_RATE);
2452527a6266SJeff Kirsher 		if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
2453527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2454527a6266SJeff Kirsher 		else
2455527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2456527a6266SJeff Kirsher 	}
2457527a6266SJeff Kirsher }
2458527a6266SJeff Kirsher 
245976723bcaSSebastian Hesselbarth #if defined(CONFIG_OF)
246076723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = {
246176723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,orion-eth", },
246276723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,kirkwood-eth", },
246376723bcaSSebastian Hesselbarth 	{ }
246476723bcaSSebastian Hesselbarth };
246576723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
246676723bcaSSebastian Hesselbarth #endif
246776723bcaSSebastian Hesselbarth 
246876723bcaSSebastian Hesselbarth #if defined(CONFIG_OF) && !defined(CONFIG_MV64X60)
246976723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v)				\
247076723bcaSSebastian Hesselbarth 	do {								\
247176723bcaSSebastian Hesselbarth 		u32 tmp;						\
247276723bcaSSebastian Hesselbarth 		if (!of_property_read_u32(_np, "marvell," _name, &tmp))	\
247376723bcaSSebastian Hesselbarth 			_v = tmp;					\
247476723bcaSSebastian Hesselbarth 	} while (0)
247576723bcaSSebastian Hesselbarth 
247676723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3];
247776723bcaSSebastian Hesselbarth 
247876723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
247976723bcaSSebastian Hesselbarth 					  struct device_node *pnp)
248076723bcaSSebastian Hesselbarth {
248176723bcaSSebastian Hesselbarth 	struct platform_device *ppdev;
248276723bcaSSebastian Hesselbarth 	struct mv643xx_eth_platform_data ppd;
248376723bcaSSebastian Hesselbarth 	struct resource res;
248476723bcaSSebastian Hesselbarth 	const char *mac_addr;
248576723bcaSSebastian Hesselbarth 	int ret;
2486*785bf6f7SJonas Gorski 	int dev_num = 0;
248776723bcaSSebastian Hesselbarth 
248876723bcaSSebastian Hesselbarth 	memset(&ppd, 0, sizeof(ppd));
248976723bcaSSebastian Hesselbarth 	ppd.shared = pdev;
249076723bcaSSebastian Hesselbarth 
249176723bcaSSebastian Hesselbarth 	memset(&res, 0, sizeof(res));
249276723bcaSSebastian Hesselbarth 	if (!of_irq_to_resource(pnp, 0, &res)) {
249376723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "missing interrupt on %s\n", pnp->name);
249476723bcaSSebastian Hesselbarth 		return -EINVAL;
249576723bcaSSebastian Hesselbarth 	}
249676723bcaSSebastian Hesselbarth 
249776723bcaSSebastian Hesselbarth 	if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
249876723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "missing reg property on %s\n", pnp->name);
249976723bcaSSebastian Hesselbarth 		return -EINVAL;
250076723bcaSSebastian Hesselbarth 	}
250176723bcaSSebastian Hesselbarth 
250276723bcaSSebastian Hesselbarth 	if (ppd.port_number >= 3) {
250376723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "invalid reg property on %s\n", pnp->name);
250476723bcaSSebastian Hesselbarth 		return -EINVAL;
250576723bcaSSebastian Hesselbarth 	}
250676723bcaSSebastian Hesselbarth 
2507*785bf6f7SJonas Gorski 	while (dev_num < 3 && port_platdev[dev_num])
2508*785bf6f7SJonas Gorski 		dev_num++;
2509*785bf6f7SJonas Gorski 
2510*785bf6f7SJonas Gorski 	if (dev_num == 3) {
2511*785bf6f7SJonas Gorski 		dev_err(&pdev->dev, "too many ports registered\n");
2512*785bf6f7SJonas Gorski 		return -EINVAL;
2513*785bf6f7SJonas Gorski 	}
2514*785bf6f7SJonas Gorski 
251576723bcaSSebastian Hesselbarth 	mac_addr = of_get_mac_address(pnp);
251676723bcaSSebastian Hesselbarth 	if (mac_addr)
251776723bcaSSebastian Hesselbarth 		memcpy(ppd.mac_addr, mac_addr, 6);
251876723bcaSSebastian Hesselbarth 
251976723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
252076723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
252176723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
252276723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
252376723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
252476723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
252576723bcaSSebastian Hesselbarth 
252676723bcaSSebastian Hesselbarth 	ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
252776723bcaSSebastian Hesselbarth 	if (!ppd.phy_node) {
252876723bcaSSebastian Hesselbarth 		ppd.phy_addr = MV643XX_ETH_PHY_NONE;
252976723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "speed", &ppd.speed);
253076723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "duplex", &ppd.duplex);
253176723bcaSSebastian Hesselbarth 	}
253276723bcaSSebastian Hesselbarth 
2533*785bf6f7SJonas Gorski 	ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
253476723bcaSSebastian Hesselbarth 	if (!ppdev)
253576723bcaSSebastian Hesselbarth 		return -ENOMEM;
253676723bcaSSebastian Hesselbarth 	ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
253776723bcaSSebastian Hesselbarth 
253876723bcaSSebastian Hesselbarth 	ret = platform_device_add_resources(ppdev, &res, 1);
253976723bcaSSebastian Hesselbarth 	if (ret)
254076723bcaSSebastian Hesselbarth 		goto port_err;
254176723bcaSSebastian Hesselbarth 
254276723bcaSSebastian Hesselbarth 	ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
254376723bcaSSebastian Hesselbarth 	if (ret)
254476723bcaSSebastian Hesselbarth 		goto port_err;
254576723bcaSSebastian Hesselbarth 
254676723bcaSSebastian Hesselbarth 	ret = platform_device_add(ppdev);
254776723bcaSSebastian Hesselbarth 	if (ret)
254876723bcaSSebastian Hesselbarth 		goto port_err;
254976723bcaSSebastian Hesselbarth 
2550*785bf6f7SJonas Gorski 	port_platdev[dev_num] = ppdev;
255176723bcaSSebastian Hesselbarth 
255276723bcaSSebastian Hesselbarth 	return 0;
255376723bcaSSebastian Hesselbarth 
255476723bcaSSebastian Hesselbarth port_err:
255576723bcaSSebastian Hesselbarth 	platform_device_put(ppdev);
255676723bcaSSebastian Hesselbarth 	return ret;
255776723bcaSSebastian Hesselbarth }
255876723bcaSSebastian Hesselbarth 
255976723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
256076723bcaSSebastian Hesselbarth {
256176723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
256276723bcaSSebastian Hesselbarth 	struct device_node *pnp, *np = pdev->dev.of_node;
256376723bcaSSebastian Hesselbarth 	int ret;
256476723bcaSSebastian Hesselbarth 
256576723bcaSSebastian Hesselbarth 	/* bail out if not registered from DT */
256676723bcaSSebastian Hesselbarth 	if (!np)
256776723bcaSSebastian Hesselbarth 		return 0;
256876723bcaSSebastian Hesselbarth 
256976723bcaSSebastian Hesselbarth 	pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
257076723bcaSSebastian Hesselbarth 	if (!pd)
257176723bcaSSebastian Hesselbarth 		return -ENOMEM;
257276723bcaSSebastian Hesselbarth 	pdev->dev.platform_data = pd;
257376723bcaSSebastian Hesselbarth 
257476723bcaSSebastian Hesselbarth 	mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
257576723bcaSSebastian Hesselbarth 
257676723bcaSSebastian Hesselbarth 	for_each_available_child_of_node(np, pnp) {
257776723bcaSSebastian Hesselbarth 		ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
257876723bcaSSebastian Hesselbarth 		if (ret)
257976723bcaSSebastian Hesselbarth 			return ret;
258076723bcaSSebastian Hesselbarth 	}
258176723bcaSSebastian Hesselbarth 	return 0;
258276723bcaSSebastian Hesselbarth }
258376723bcaSSebastian Hesselbarth 
258476723bcaSSebastian Hesselbarth static void mv643xx_eth_shared_of_remove(void)
258576723bcaSSebastian Hesselbarth {
258676723bcaSSebastian Hesselbarth 	int n;
258776723bcaSSebastian Hesselbarth 
258876723bcaSSebastian Hesselbarth 	for (n = 0; n < 3; n++) {
258976723bcaSSebastian Hesselbarth 		platform_device_del(port_platdev[n]);
259076723bcaSSebastian Hesselbarth 		port_platdev[n] = NULL;
259176723bcaSSebastian Hesselbarth 	}
259276723bcaSSebastian Hesselbarth }
259376723bcaSSebastian Hesselbarth #else
2594ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
259576723bcaSSebastian Hesselbarth {
2596ff20877aSArnd Bergmann 	return 0;
259776723bcaSSebastian Hesselbarth }
259876723bcaSSebastian Hesselbarth 
2599ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void)
2600ff20877aSArnd Bergmann {
2601ff20877aSArnd Bergmann }
260276723bcaSSebastian Hesselbarth #endif
260376723bcaSSebastian Hesselbarth 
2604527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2605527a6266SJeff Kirsher {
2606527a6266SJeff Kirsher 	static int mv643xx_eth_version_printed;
260776723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
2608527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp;
260963a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
2610527a6266SJeff Kirsher 	struct resource *res;
261176723bcaSSebastian Hesselbarth 	int ret;
2612527a6266SJeff Kirsher 
2613527a6266SJeff Kirsher 	if (!mv643xx_eth_version_printed++)
2614527a6266SJeff Kirsher 		pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2615527a6266SJeff Kirsher 			  mv643xx_eth_driver_version);
2616527a6266SJeff Kirsher 
2617527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2618527a6266SJeff Kirsher 	if (res == NULL)
2619727f957aSSebastian Hesselbarth 		return -EINVAL;
2620527a6266SJeff Kirsher 
2621727f957aSSebastian Hesselbarth 	msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
2622527a6266SJeff Kirsher 	if (msp == NULL)
2623727f957aSSebastian Hesselbarth 		return -ENOMEM;
262476723bcaSSebastian Hesselbarth 	platform_set_drvdata(pdev, msp);
2625527a6266SJeff Kirsher 
262665a6f969SSebastian Hesselbarth 	msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
2627527a6266SJeff Kirsher 	if (msp->base == NULL)
2628727f957aSSebastian Hesselbarth 		return -ENOMEM;
2629527a6266SJeff Kirsher 
263020922486SSebastian Hesselbarth 	msp->clk = devm_clk_get(&pdev->dev, NULL);
263120922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
263220922486SSebastian Hesselbarth 		clk_prepare_enable(msp->clk);
263320922486SSebastian Hesselbarth 
2634527a6266SJeff Kirsher 	/*
2635527a6266SJeff Kirsher 	 * (Re-)program MBUS remapping windows if we are asked to.
2636527a6266SJeff Kirsher 	 */
263763a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
263863a9332bSAndrew Lunn 	if (dram)
263963a9332bSAndrew Lunn 		mv643xx_eth_conf_mbus_windows(msp, dram);
2640527a6266SJeff Kirsher 
264176723bcaSSebastian Hesselbarth 	ret = mv643xx_eth_shared_of_probe(pdev);
264276723bcaSSebastian Hesselbarth 	if (ret)
264376723bcaSSebastian Hesselbarth 		return ret;
264476723bcaSSebastian Hesselbarth 	pd = pdev->dev.platform_data;
264576723bcaSSebastian Hesselbarth 
2646527a6266SJeff Kirsher 	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2647527a6266SJeff Kirsher 					pd->tx_csum_limit : 9 * 1024;
2648527a6266SJeff Kirsher 	infer_hw_params(msp);
2649527a6266SJeff Kirsher 
2650527a6266SJeff Kirsher 	return 0;
2651527a6266SJeff Kirsher }
2652527a6266SJeff Kirsher 
2653527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2654527a6266SJeff Kirsher {
2655527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
2656527a6266SJeff Kirsher 
265776723bcaSSebastian Hesselbarth 	mv643xx_eth_shared_of_remove();
265820922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
265920922486SSebastian Hesselbarth 		clk_disable_unprepare(msp->clk);
2660527a6266SJeff Kirsher 	return 0;
2661527a6266SJeff Kirsher }
2662527a6266SJeff Kirsher 
2663527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = {
2664527a6266SJeff Kirsher 	.probe		= mv643xx_eth_shared_probe,
2665527a6266SJeff Kirsher 	.remove		= mv643xx_eth_shared_remove,
2666527a6266SJeff Kirsher 	.driver = {
2667527a6266SJeff Kirsher 		.name	= MV643XX_ETH_SHARED_NAME,
2668527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
266976723bcaSSebastian Hesselbarth 		.of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
2670527a6266SJeff Kirsher 	},
2671527a6266SJeff Kirsher };
2672527a6266SJeff Kirsher 
2673527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
2674527a6266SJeff Kirsher {
2675527a6266SJeff Kirsher 	int addr_shift = 5 * mp->port_num;
2676527a6266SJeff Kirsher 	u32 data;
2677527a6266SJeff Kirsher 
2678527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2679527a6266SJeff Kirsher 	data &= ~(0x1f << addr_shift);
2680527a6266SJeff Kirsher 	data |= (phy_addr & 0x1f) << addr_shift;
2681527a6266SJeff Kirsher 	wrl(mp, PHY_ADDR, data);
2682527a6266SJeff Kirsher }
2683527a6266SJeff Kirsher 
2684527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp)
2685527a6266SJeff Kirsher {
2686527a6266SJeff Kirsher 	unsigned int data;
2687527a6266SJeff Kirsher 
2688527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2689527a6266SJeff Kirsher 
2690527a6266SJeff Kirsher 	return (data >> (5 * mp->port_num)) & 0x1f;
2691527a6266SJeff Kirsher }
2692527a6266SJeff Kirsher 
2693527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp,
2694527a6266SJeff Kirsher 		       struct mv643xx_eth_platform_data *pd)
2695527a6266SJeff Kirsher {
2696527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2697527a6266SJeff Kirsher 
2698527a6266SJeff Kirsher 	if (is_valid_ether_addr(pd->mac_addr))
2699527a6266SJeff Kirsher 		memcpy(dev->dev_addr, pd->mac_addr, 6);
2700527a6266SJeff Kirsher 	else
2701527a6266SJeff Kirsher 		uc_addr_get(mp, dev->dev_addr);
2702527a6266SJeff Kirsher 
2703527a6266SJeff Kirsher 	mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2704527a6266SJeff Kirsher 	if (pd->rx_queue_size)
2705527a6266SJeff Kirsher 		mp->rx_ring_size = pd->rx_queue_size;
2706527a6266SJeff Kirsher 	mp->rx_desc_sram_addr = pd->rx_sram_addr;
2707527a6266SJeff Kirsher 	mp->rx_desc_sram_size = pd->rx_sram_size;
2708527a6266SJeff Kirsher 
2709527a6266SJeff Kirsher 	mp->rxq_count = pd->rx_queue_count ? : 1;
2710527a6266SJeff Kirsher 
2711527a6266SJeff Kirsher 	mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2712527a6266SJeff Kirsher 	if (pd->tx_queue_size)
2713527a6266SJeff Kirsher 		mp->tx_ring_size = pd->tx_queue_size;
2714527a6266SJeff Kirsher 	mp->tx_desc_sram_addr = pd->tx_sram_addr;
2715527a6266SJeff Kirsher 	mp->tx_desc_sram_size = pd->tx_sram_size;
2716527a6266SJeff Kirsher 
2717527a6266SJeff Kirsher 	mp->txq_count = pd->tx_queue_count ? : 1;
2718527a6266SJeff Kirsher }
2719527a6266SJeff Kirsher 
2720c3a07134SFlorian Fainelli static void mv643xx_eth_adjust_link(struct net_device *dev)
2721c3a07134SFlorian Fainelli {
2722c3a07134SFlorian Fainelli 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2723c3a07134SFlorian Fainelli 
2724c3a07134SFlorian Fainelli 	mv643xx_adjust_pscr(mp);
2725c3a07134SFlorian Fainelli }
2726c3a07134SFlorian Fainelli 
2727527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2728527a6266SJeff Kirsher 				   int phy_addr)
2729527a6266SJeff Kirsher {
2730527a6266SJeff Kirsher 	struct phy_device *phydev;
2731527a6266SJeff Kirsher 	int start;
2732527a6266SJeff Kirsher 	int num;
2733527a6266SJeff Kirsher 	int i;
2734c3a07134SFlorian Fainelli 	char phy_id[MII_BUS_ID_SIZE + 3];
2735527a6266SJeff Kirsher 
2736527a6266SJeff Kirsher 	if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2737527a6266SJeff Kirsher 		start = phy_addr_get(mp) & 0x1f;
2738527a6266SJeff Kirsher 		num = 32;
2739527a6266SJeff Kirsher 	} else {
2740527a6266SJeff Kirsher 		start = phy_addr & 0x1f;
2741527a6266SJeff Kirsher 		num = 1;
2742527a6266SJeff Kirsher 	}
2743527a6266SJeff Kirsher 
2744c3a07134SFlorian Fainelli 	/* Attempt to connect to the PHY using orion-mdio */
2745976c90b9SSimon Baatz 	phydev = ERR_PTR(-ENODEV);
2746527a6266SJeff Kirsher 	for (i = 0; i < num; i++) {
2747527a6266SJeff Kirsher 		int addr = (start + i) & 0x1f;
2748527a6266SJeff Kirsher 
2749c3a07134SFlorian Fainelli 		snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
2750c3a07134SFlorian Fainelli 				"orion-mdio-mii", addr);
2751527a6266SJeff Kirsher 
2752c3a07134SFlorian Fainelli 		phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
2753c3a07134SFlorian Fainelli 				PHY_INTERFACE_MODE_GMII);
2754c3a07134SFlorian Fainelli 		if (!IS_ERR(phydev)) {
2755527a6266SJeff Kirsher 			phy_addr_set(mp, addr);
2756c3a07134SFlorian Fainelli 			break;
2757527a6266SJeff Kirsher 		}
2758527a6266SJeff Kirsher 	}
2759527a6266SJeff Kirsher 
2760527a6266SJeff Kirsher 	return phydev;
2761527a6266SJeff Kirsher }
2762527a6266SJeff Kirsher 
2763527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
2764527a6266SJeff Kirsher {
2765527a6266SJeff Kirsher 	struct phy_device *phy = mp->phy;
2766527a6266SJeff Kirsher 
2767527a6266SJeff Kirsher 	phy_reset(mp);
2768527a6266SJeff Kirsher 
2769527a6266SJeff Kirsher 	if (speed == 0) {
2770527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_ENABLE;
2771527a6266SJeff Kirsher 		phy->speed = 0;
2772527a6266SJeff Kirsher 		phy->duplex = 0;
2773527a6266SJeff Kirsher 		phy->advertising = phy->supported | ADVERTISED_Autoneg;
2774527a6266SJeff Kirsher 	} else {
2775527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_DISABLE;
2776527a6266SJeff Kirsher 		phy->advertising = 0;
2777527a6266SJeff Kirsher 		phy->speed = speed;
2778527a6266SJeff Kirsher 		phy->duplex = duplex;
2779527a6266SJeff Kirsher 	}
2780527a6266SJeff Kirsher 	phy_start_aneg(phy);
2781527a6266SJeff Kirsher }
2782527a6266SJeff Kirsher 
2783527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2784527a6266SJeff Kirsher {
2785527a6266SJeff Kirsher 	u32 pscr;
2786527a6266SJeff Kirsher 
2787527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2788527a6266SJeff Kirsher 	if (pscr & SERIAL_PORT_ENABLE) {
2789527a6266SJeff Kirsher 		pscr &= ~SERIAL_PORT_ENABLE;
2790527a6266SJeff Kirsher 		wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2791527a6266SJeff Kirsher 	}
2792527a6266SJeff Kirsher 
2793527a6266SJeff Kirsher 	pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
2794527a6266SJeff Kirsher 	if (mp->phy == NULL) {
2795527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2796527a6266SJeff Kirsher 		if (speed == SPEED_1000)
2797527a6266SJeff Kirsher 			pscr |= SET_GMII_SPEED_TO_1000;
2798527a6266SJeff Kirsher 		else if (speed == SPEED_100)
2799527a6266SJeff Kirsher 			pscr |= SET_MII_SPEED_TO_100;
2800527a6266SJeff Kirsher 
2801527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2802527a6266SJeff Kirsher 
2803527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2804527a6266SJeff Kirsher 		if (duplex == DUPLEX_FULL)
2805527a6266SJeff Kirsher 			pscr |= SET_FULL_DUPLEX_MODE;
2806527a6266SJeff Kirsher 	}
2807527a6266SJeff Kirsher 
2808527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2809527a6266SJeff Kirsher }
2810527a6266SJeff Kirsher 
2811527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = {
2812527a6266SJeff Kirsher 	.ndo_open		= mv643xx_eth_open,
2813527a6266SJeff Kirsher 	.ndo_stop		= mv643xx_eth_stop,
2814527a6266SJeff Kirsher 	.ndo_start_xmit		= mv643xx_eth_xmit,
2815527a6266SJeff Kirsher 	.ndo_set_rx_mode	= mv643xx_eth_set_rx_mode,
2816527a6266SJeff Kirsher 	.ndo_set_mac_address	= mv643xx_eth_set_mac_address,
2817527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2818527a6266SJeff Kirsher 	.ndo_do_ioctl		= mv643xx_eth_ioctl,
2819527a6266SJeff Kirsher 	.ndo_change_mtu		= mv643xx_eth_change_mtu,
2820527a6266SJeff Kirsher 	.ndo_set_features	= mv643xx_eth_set_features,
2821527a6266SJeff Kirsher 	.ndo_tx_timeout		= mv643xx_eth_tx_timeout,
2822527a6266SJeff Kirsher 	.ndo_get_stats		= mv643xx_eth_get_stats,
2823527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2824527a6266SJeff Kirsher 	.ndo_poll_controller	= mv643xx_eth_netpoll,
2825527a6266SJeff Kirsher #endif
2826527a6266SJeff Kirsher };
2827527a6266SJeff Kirsher 
2828527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev)
2829527a6266SJeff Kirsher {
2830527a6266SJeff Kirsher 	struct mv643xx_eth_platform_data *pd;
2831527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2832527a6266SJeff Kirsher 	struct net_device *dev;
2833527a6266SJeff Kirsher 	struct resource *res;
2834527a6266SJeff Kirsher 	int err;
2835527a6266SJeff Kirsher 
2836527a6266SJeff Kirsher 	pd = pdev->dev.platform_data;
2837527a6266SJeff Kirsher 	if (pd == NULL) {
2838527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
2839527a6266SJeff Kirsher 		return -ENODEV;
2840527a6266SJeff Kirsher 	}
2841527a6266SJeff Kirsher 
2842527a6266SJeff Kirsher 	if (pd->shared == NULL) {
2843527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
2844527a6266SJeff Kirsher 		return -ENODEV;
2845527a6266SJeff Kirsher 	}
2846527a6266SJeff Kirsher 
2847527a6266SJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
2848527a6266SJeff Kirsher 	if (!dev)
2849527a6266SJeff Kirsher 		return -ENOMEM;
2850527a6266SJeff Kirsher 
2851527a6266SJeff Kirsher 	mp = netdev_priv(dev);
2852527a6266SJeff Kirsher 	platform_set_drvdata(pdev, mp);
2853527a6266SJeff Kirsher 
2854527a6266SJeff Kirsher 	mp->shared = platform_get_drvdata(pd->shared);
2855527a6266SJeff Kirsher 	mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
2856527a6266SJeff Kirsher 	mp->port_num = pd->port_number;
2857527a6266SJeff Kirsher 
2858527a6266SJeff Kirsher 	mp->dev = dev;
2859527a6266SJeff Kirsher 
2860cb85215fSSebastian Hesselbarth 	/* Kirkwood resets some registers on gated clocks. Especially
2861cb85215fSSebastian Hesselbarth 	 * CLK125_BYPASS_EN must be cleared but is not available on
2862cb85215fSSebastian Hesselbarth 	 * all other SoCs/System Controllers using this driver.
2863cb85215fSSebastian Hesselbarth 	 */
2864cb85215fSSebastian Hesselbarth 	if (of_device_is_compatible(pdev->dev.of_node,
2865cb85215fSSebastian Hesselbarth 				    "marvell,kirkwood-eth-port"))
2866cb85215fSSebastian Hesselbarth 		wrlp(mp, PORT_SERIAL_CONTROL1,
2867cb85215fSSebastian Hesselbarth 		     rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN);
2868cb85215fSSebastian Hesselbarth 
2869452503ebSAndrew Lunn 	/*
28709a43a026SAndrew Lunn 	 * Start with a default rate, and if there is a clock, allow
28719a43a026SAndrew Lunn 	 * it to override the default.
2872452503ebSAndrew Lunn 	 */
28739a43a026SAndrew Lunn 	mp->t_clk = 133000000;
287420922486SSebastian Hesselbarth 	mp->clk = devm_clk_get(&pdev->dev, NULL);
2875452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
2876452503ebSAndrew Lunn 		clk_prepare_enable(mp->clk);
2877452503ebSAndrew Lunn 		mp->t_clk = clk_get_rate(mp->clk);
287876723bcaSSebastian Hesselbarth 	} else if (!IS_ERR(mp->shared->clk)) {
287976723bcaSSebastian Hesselbarth 		mp->t_clk = clk_get_rate(mp->shared->clk);
2880452503ebSAndrew Lunn 	}
288120922486SSebastian Hesselbarth 
2882527a6266SJeff Kirsher 	set_params(mp, pd);
2883527a6266SJeff Kirsher 	netif_set_real_num_tx_queues(dev, mp->txq_count);
2884527a6266SJeff Kirsher 	netif_set_real_num_rx_queues(dev, mp->rxq_count);
2885527a6266SJeff Kirsher 
2886cc9d4598SSebastian Hesselbarth 	err = 0;
2887cc9d4598SSebastian Hesselbarth 	if (pd->phy_node) {
2888cc9d4598SSebastian Hesselbarth 		mp->phy = of_phy_connect(mp->dev, pd->phy_node,
2889cc9d4598SSebastian Hesselbarth 					 mv643xx_eth_adjust_link, 0,
2890cc9d4598SSebastian Hesselbarth 					 PHY_INTERFACE_MODE_GMII);
2891cc9d4598SSebastian Hesselbarth 		if (!mp->phy)
2892cc9d4598SSebastian Hesselbarth 			err = -ENODEV;
2893cc9d4598SSebastian Hesselbarth 	} else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
2894527a6266SJeff Kirsher 		mp->phy = phy_scan(mp, pd->phy_addr);
2895527a6266SJeff Kirsher 
2896cc9d4598SSebastian Hesselbarth 		if (IS_ERR(mp->phy))
2897976c90b9SSimon Baatz 			err = PTR_ERR(mp->phy);
2898cc9d4598SSebastian Hesselbarth 		else
2899cc9d4598SSebastian Hesselbarth 			phy_init(mp, pd->speed, pd->duplex);
2900cc9d4598SSebastian Hesselbarth 	}
2901cc9d4598SSebastian Hesselbarth 	if (err == -ENODEV) {
2902976c90b9SSimon Baatz 		err = -EPROBE_DEFER;
2903976c90b9SSimon Baatz 		goto out;
2904976c90b9SSimon Baatz 	}
2905cc9d4598SSebastian Hesselbarth 	if (err)
2906cc9d4598SSebastian Hesselbarth 		goto out;
2907527a6266SJeff Kirsher 
2908527a6266SJeff Kirsher 	SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
2909527a6266SJeff Kirsher 
2910527a6266SJeff Kirsher 	init_pscr(mp, pd->speed, pd->duplex);
2911527a6266SJeff Kirsher 
2912527a6266SJeff Kirsher 
2913527a6266SJeff Kirsher 	mib_counters_clear(mp);
2914527a6266SJeff Kirsher 
2915527a6266SJeff Kirsher 	init_timer(&mp->mib_counters_timer);
2916527a6266SJeff Kirsher 	mp->mib_counters_timer.data = (unsigned long)mp;
2917527a6266SJeff Kirsher 	mp->mib_counters_timer.function = mib_counters_timer_wrapper;
2918527a6266SJeff Kirsher 	mp->mib_counters_timer.expires = jiffies + 30 * HZ;
2919527a6266SJeff Kirsher 	add_timer(&mp->mib_counters_timer);
2920527a6266SJeff Kirsher 
2921527a6266SJeff Kirsher 	spin_lock_init(&mp->mib_counters_lock);
2922527a6266SJeff Kirsher 
2923527a6266SJeff Kirsher 	INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2924527a6266SJeff Kirsher 
2925a3659aa0SAndrew Lunn 	netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT);
2926527a6266SJeff Kirsher 
2927527a6266SJeff Kirsher 	init_timer(&mp->rx_oom);
2928527a6266SJeff Kirsher 	mp->rx_oom.data = (unsigned long)mp;
2929527a6266SJeff Kirsher 	mp->rx_oom.function = oom_timer_wrapper;
2930527a6266SJeff Kirsher 
2931527a6266SJeff Kirsher 
2932527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2933527a6266SJeff Kirsher 	BUG_ON(!res);
2934527a6266SJeff Kirsher 	dev->irq = res->start;
2935527a6266SJeff Kirsher 
2936527a6266SJeff Kirsher 	dev->netdev_ops = &mv643xx_eth_netdev_ops;
2937527a6266SJeff Kirsher 
2938527a6266SJeff Kirsher 	dev->watchdog_timeo = 2 * HZ;
2939527a6266SJeff Kirsher 	dev->base_addr = 0;
2940527a6266SJeff Kirsher 
29413619eb85SSebastian Hesselbarth 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2942527a6266SJeff Kirsher 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2943527a6266SJeff Kirsher 	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
2944527a6266SJeff Kirsher 
294501789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
294601789349SJiri Pirko 
2947527a6266SJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
2948527a6266SJeff Kirsher 
2949527a6266SJeff Kirsher 	if (mp->shared->win_protect)
2950527a6266SJeff Kirsher 		wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2951527a6266SJeff Kirsher 
2952527a6266SJeff Kirsher 	netif_carrier_off(dev);
2953527a6266SJeff Kirsher 
2954527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
2955527a6266SJeff Kirsher 
2956527a6266SJeff Kirsher 	set_rx_coal(mp, 250);
2957527a6266SJeff Kirsher 	set_tx_coal(mp, 0);
2958527a6266SJeff Kirsher 
2959527a6266SJeff Kirsher 	err = register_netdev(dev);
2960527a6266SJeff Kirsher 	if (err)
2961527a6266SJeff Kirsher 		goto out;
2962527a6266SJeff Kirsher 
2963527a6266SJeff Kirsher 	netdev_notice(dev, "port %d with MAC address %pM\n",
2964527a6266SJeff Kirsher 		      mp->port_num, dev->dev_addr);
2965527a6266SJeff Kirsher 
2966527a6266SJeff Kirsher 	if (mp->tx_desc_sram_size > 0)
2967527a6266SJeff Kirsher 		netdev_notice(dev, "configured with sram\n");
2968527a6266SJeff Kirsher 
2969527a6266SJeff Kirsher 	return 0;
2970527a6266SJeff Kirsher 
2971527a6266SJeff Kirsher out:
297220922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
2973baffab28SSimon Baatz 		clk_disable_unprepare(mp->clk);
2974527a6266SJeff Kirsher 	free_netdev(dev);
2975527a6266SJeff Kirsher 
2976527a6266SJeff Kirsher 	return err;
2977527a6266SJeff Kirsher }
2978527a6266SJeff Kirsher 
2979527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev)
2980527a6266SJeff Kirsher {
2981527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
2982527a6266SJeff Kirsher 
2983527a6266SJeff Kirsher 	unregister_netdev(mp->dev);
2984527a6266SJeff Kirsher 	if (mp->phy != NULL)
2985cec753f5SSebastian Hesselbarth 		phy_disconnect(mp->phy);
2986527a6266SJeff Kirsher 	cancel_work_sync(&mp->tx_timeout_task);
2987452503ebSAndrew Lunn 
298820922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
2989452503ebSAndrew Lunn 		clk_disable_unprepare(mp->clk);
29909a43a026SAndrew Lunn 
2991527a6266SJeff Kirsher 	free_netdev(mp->dev);
2992527a6266SJeff Kirsher 
2993527a6266SJeff Kirsher 	return 0;
2994527a6266SJeff Kirsher }
2995527a6266SJeff Kirsher 
2996527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev)
2997527a6266SJeff Kirsher {
2998527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
2999527a6266SJeff Kirsher 
3000527a6266SJeff Kirsher 	/* Mask all interrupts on ethernet port */
3001527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
3002527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
3003527a6266SJeff Kirsher 
3004527a6266SJeff Kirsher 	if (netif_running(mp->dev))
3005527a6266SJeff Kirsher 		port_reset(mp);
3006527a6266SJeff Kirsher }
3007527a6266SJeff Kirsher 
3008527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = {
3009527a6266SJeff Kirsher 	.probe		= mv643xx_eth_probe,
3010527a6266SJeff Kirsher 	.remove		= mv643xx_eth_remove,
3011527a6266SJeff Kirsher 	.shutdown	= mv643xx_eth_shutdown,
3012527a6266SJeff Kirsher 	.driver = {
3013527a6266SJeff Kirsher 		.name	= MV643XX_ETH_NAME,
3014527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
3015527a6266SJeff Kirsher 	},
3016527a6266SJeff Kirsher };
3017527a6266SJeff Kirsher 
3018527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void)
3019527a6266SJeff Kirsher {
3020527a6266SJeff Kirsher 	int rc;
3021527a6266SJeff Kirsher 
3022527a6266SJeff Kirsher 	rc = platform_driver_register(&mv643xx_eth_shared_driver);
3023527a6266SJeff Kirsher 	if (!rc) {
3024527a6266SJeff Kirsher 		rc = platform_driver_register(&mv643xx_eth_driver);
3025527a6266SJeff Kirsher 		if (rc)
3026527a6266SJeff Kirsher 			platform_driver_unregister(&mv643xx_eth_shared_driver);
3027527a6266SJeff Kirsher 	}
3028527a6266SJeff Kirsher 
3029527a6266SJeff Kirsher 	return rc;
3030527a6266SJeff Kirsher }
3031527a6266SJeff Kirsher module_init(mv643xx_eth_init_module);
3032527a6266SJeff Kirsher 
3033527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void)
3034527a6266SJeff Kirsher {
3035527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_driver);
3036527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_shared_driver);
3037527a6266SJeff Kirsher }
3038527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module);
3039527a6266SJeff Kirsher 
3040527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3041527a6266SJeff Kirsher 	      "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
3042527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
3043527a6266SJeff Kirsher MODULE_LICENSE("GPL");
3044527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
3045527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3046