blob: 76cf89c3ee27f721391f0c40db92d46e73a3a4e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020057static char mv643xx_driver_name[] = "mv643xx_eth";
58static char mv643xx_driver_version[] = "1.0";
59
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020060#define MV643XX_CHECKSUM_OFFLOAD_TX
61#define MV643XX_NAPI
62#define MV643XX_TX_FAST_REFILL
63#undef MV643XX_COAL
64
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020065#define MV643XX_TX_COAL 100
66#ifdef MV643XX_COAL
67#define MV643XX_RX_COAL 100
68#endif
69
70#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
71#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
99#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
100#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
101#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
102#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
103#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
104#define PORT_STATUS(p) (0x0444 + ((p) << 10))
105#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
106#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
107#define INT_CAUSE(p) (0x0460 + ((p) << 10))
108#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
109#define INT_MASK(p) (0x0468 + ((p) << 10))
110#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
111#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
112#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
113#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
114#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
115#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
116#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
117#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
118#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200119
120/* These macros describe Ethernet Port configuration reg (Px_cR) bits */
121#define UNICAST_NORMAL_MODE (0 << 0)
122#define UNICAST_PROMISCUOUS_MODE (1 << 0)
123#define DEFAULT_RX_QUEUE(queue) ((queue) << 1)
124#define DEFAULT_RX_ARP_QUEUE(queue) ((queue) << 4)
125#define RECEIVE_BC_IF_NOT_IP_OR_ARP (0 << 7)
126#define REJECT_BC_IF_NOT_IP_OR_ARP (1 << 7)
127#define RECEIVE_BC_IF_IP (0 << 8)
128#define REJECT_BC_IF_IP (1 << 8)
129#define RECEIVE_BC_IF_ARP (0 << 9)
130#define REJECT_BC_IF_ARP (1 << 9)
131#define TX_AM_NO_UPDATE_ERROR_SUMMARY (1 << 12)
132#define CAPTURE_TCP_FRAMES_DIS (0 << 14)
133#define CAPTURE_TCP_FRAMES_EN (1 << 14)
134#define CAPTURE_UDP_FRAMES_DIS (0 << 15)
135#define CAPTURE_UDP_FRAMES_EN (1 << 15)
136#define DEFAULT_RX_TCP_QUEUE(queue) ((queue) << 16)
137#define DEFAULT_RX_UDP_QUEUE(queue) ((queue) << 19)
138#define DEFAULT_RX_BPDU_QUEUE(queue) ((queue) << 22)
139
140#define PORT_CONFIG_DEFAULT_VALUE \
141 UNICAST_NORMAL_MODE | \
142 DEFAULT_RX_QUEUE(0) | \
143 DEFAULT_RX_ARP_QUEUE(0) | \
144 RECEIVE_BC_IF_NOT_IP_OR_ARP | \
145 RECEIVE_BC_IF_IP | \
146 RECEIVE_BC_IF_ARP | \
147 CAPTURE_TCP_FRAMES_DIS | \
148 CAPTURE_UDP_FRAMES_DIS | \
149 DEFAULT_RX_TCP_QUEUE(0) | \
150 DEFAULT_RX_UDP_QUEUE(0) | \
151 DEFAULT_RX_BPDU_QUEUE(0)
152
153/* These macros describe Ethernet Port configuration extend reg (Px_cXR) bits*/
154#define CLASSIFY_EN (1 << 0)
155#define SPAN_BPDU_PACKETS_AS_NORMAL (0 << 1)
156#define SPAN_BPDU_PACKETS_TO_RX_QUEUE_7 (1 << 1)
157#define PARTITION_DISABLE (0 << 2)
158#define PARTITION_ENABLE (1 << 2)
159
160#define PORT_CONFIG_EXTEND_DEFAULT_VALUE \
161 SPAN_BPDU_PACKETS_AS_NORMAL | \
162 PARTITION_DISABLE
163
164/* These macros describe Ethernet Port Sdma configuration reg (SDCR) bits */
165#define RIFB (1 << 0)
166#define RX_BURST_SIZE_1_64BIT (0 << 1)
167#define RX_BURST_SIZE_2_64BIT (1 << 1)
168#define RX_BURST_SIZE_4_64BIT (2 << 1)
169#define RX_BURST_SIZE_8_64BIT (3 << 1)
170#define RX_BURST_SIZE_16_64BIT (4 << 1)
171#define BLM_RX_NO_SWAP (1 << 4)
172#define BLM_RX_BYTE_SWAP (0 << 4)
173#define BLM_TX_NO_SWAP (1 << 5)
174#define BLM_TX_BYTE_SWAP (0 << 5)
175#define DESCRIPTORS_BYTE_SWAP (1 << 6)
176#define DESCRIPTORS_NO_SWAP (0 << 6)
177#define IPG_INT_RX(value) (((value) & 0x3fff) << 8)
178#define TX_BURST_SIZE_1_64BIT (0 << 22)
179#define TX_BURST_SIZE_2_64BIT (1 << 22)
180#define TX_BURST_SIZE_4_64BIT (2 << 22)
181#define TX_BURST_SIZE_8_64BIT (3 << 22)
182#define TX_BURST_SIZE_16_64BIT (4 << 22)
183
184#if defined(__BIG_ENDIAN)
185#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
186 RX_BURST_SIZE_4_64BIT | \
187 IPG_INT_RX(0) | \
188 TX_BURST_SIZE_4_64BIT
189#elif defined(__LITTLE_ENDIAN)
190#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
191 RX_BURST_SIZE_4_64BIT | \
192 BLM_RX_NO_SWAP | \
193 BLM_TX_NO_SWAP | \
194 IPG_INT_RX(0) | \
195 TX_BURST_SIZE_4_64BIT
196#else
197#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
198#endif
199
200/* These macros describe Ethernet Port serial control reg (PSCR) bits */
201#define SERIAL_PORT_DISABLE (0 << 0)
202#define SERIAL_PORT_ENABLE (1 << 0)
203#define DO_NOT_FORCE_LINK_PASS (0 << 1)
204#define FORCE_LINK_PASS (1 << 1)
205#define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
206#define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
207#define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
208#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
209#define ADV_NO_FLOW_CTRL (0 << 4)
210#define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
211#define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
212#define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
213#define FORCE_BP_MODE_NO_JAM (0 << 7)
214#define FORCE_BP_MODE_JAM_TX (1 << 7)
215#define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
216#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
217#define FORCE_LINK_FAIL (0 << 10)
218#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
219#define RETRANSMIT_16_ATTEMPTS (0 << 11)
220#define RETRANSMIT_FOREVER (1 << 11)
221#define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
222#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
223#define DTE_ADV_0 (0 << 14)
224#define DTE_ADV_1 (1 << 14)
225#define DISABLE_AUTO_NEG_BYPASS (0 << 15)
226#define ENABLE_AUTO_NEG_BYPASS (1 << 15)
227#define AUTO_NEG_NO_CHANGE (0 << 16)
228#define RESTART_AUTO_NEG (1 << 16)
229#define MAX_RX_PACKET_1518BYTE (0 << 17)
230#define MAX_RX_PACKET_1522BYTE (1 << 17)
231#define MAX_RX_PACKET_1552BYTE (2 << 17)
232#define MAX_RX_PACKET_9022BYTE (3 << 17)
233#define MAX_RX_PACKET_9192BYTE (4 << 17)
234#define MAX_RX_PACKET_9700BYTE (5 << 17)
235#define MAX_RX_PACKET_MASK (7 << 17)
236#define CLR_EXT_LOOPBACK (0 << 20)
237#define SET_EXT_LOOPBACK (1 << 20)
238#define SET_HALF_DUPLEX_MODE (0 << 21)
239#define SET_FULL_DUPLEX_MODE (1 << 21)
240#define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
241#define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
242#define SET_GMII_SPEED_TO_10_100 (0 << 23)
243#define SET_GMII_SPEED_TO_1000 (1 << 23)
244#define SET_MII_SPEED_TO_10 (0 << 24)
245#define SET_MII_SPEED_TO_100 (1 << 24)
246
247#define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
248 DO_NOT_FORCE_LINK_PASS | \
249 ENABLE_AUTO_NEG_FOR_DUPLX | \
250 DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
251 ADV_SYMMETRIC_FLOW_CTRL | \
252 FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
253 FORCE_BP_MODE_NO_JAM | \
254 (1 << 9) /* reserved */ | \
255 DO_NOT_FORCE_LINK_FAIL | \
256 RETRANSMIT_16_ATTEMPTS | \
257 ENABLE_AUTO_NEG_SPEED_GMII | \
258 DTE_ADV_0 | \
259 DISABLE_AUTO_NEG_BYPASS | \
260 AUTO_NEG_NO_CHANGE | \
261 MAX_RX_PACKET_9700BYTE | \
262 CLR_EXT_LOOPBACK | \
263 SET_FULL_DUPLEX_MODE | \
264 ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
265
266/* These macros describe Ethernet Serial Status reg (PSR) bits */
267#define PORT_STATUS_MODE_10_BIT (1 << 0)
268#define PORT_STATUS_LINK_UP (1 << 1)
269#define PORT_STATUS_FULL_DUPLEX (1 << 2)
270#define PORT_STATUS_FLOW_CONTROL (1 << 3)
271#define PORT_STATUS_GMII_1000 (1 << 4)
272#define PORT_STATUS_MII_100 (1 << 5)
273/* PSR bit 6 is undocumented */
274#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
275#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
276#define PORT_STATUS_PARTITION (1 << 9)
277#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
278/* PSR bits 11-31 are reserved */
279
280#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
281#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
282
283#define DESC_SIZE 64
284
285#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
286#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
287
288#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
289#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
290#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
291#define ETH_INT_CAUSE_EXT 0x00000002
292#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
293
294#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
295#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
296#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
297#define ETH_INT_CAUSE_PHY 0x00010000
298#define ETH_INT_CAUSE_STATE 0x00100000
299#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
300 ETH_INT_CAUSE_STATE)
301
302#define ETH_INT_MASK_ALL 0x00000000
303#define ETH_INT_MASK_ALL_EXT 0x00000000
304
305#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
306#define PHY_WAIT_MICRO_SECONDS 10
307
308/* Buffer offset from buffer pointer */
309#define RX_BUF_OFFSET 0x2
310
311/* Gigabit Ethernet Unit Global Registers */
312
313/* MIB Counters register definitions */
314#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
315#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
316#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
317#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
318#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
319#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
320#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
321#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
322#define ETH_MIB_FRAMES_64_OCTETS 0x20
323#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
324#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
325#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
326#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
327#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
328#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
329#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
330#define ETH_MIB_GOOD_FRAMES_SENT 0x40
331#define ETH_MIB_EXCESSIVE_COLLISION 0x44
332#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
333#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
334#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
335#define ETH_MIB_FC_SENT 0x54
336#define ETH_MIB_GOOD_FC_RECEIVED 0x58
337#define ETH_MIB_BAD_FC_RECEIVED 0x5c
338#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
339#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
340#define ETH_MIB_OVERSIZE_RECEIVED 0x68
341#define ETH_MIB_JABBER_RECEIVED 0x6c
342#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
343#define ETH_MIB_BAD_CRC_EVENT 0x74
344#define ETH_MIB_COLLISION 0x78
345#define ETH_MIB_LATE_COLLISION 0x7c
346
347/* Port serial status reg (PSR) */
348#define ETH_INTERFACE_PCM 0x00000001
349#define ETH_LINK_IS_UP 0x00000002
350#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
351#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
352#define ETH_GMII_SPEED_1000 0x00000010
353#define ETH_MII_SPEED_100 0x00000020
354#define ETH_TX_IN_PROGRESS 0x00000080
355#define ETH_BYPASS_ACTIVE 0x00000100
356#define ETH_PORT_AT_PARTITION_STATE 0x00000200
357#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
358
359/* SMI reg */
360#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
361#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
362#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
363#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
364
365/* Interrupt Cause Register Bit Definitions */
366
367/* SDMA command status fields macros */
368
369/* Tx & Rx descriptors status */
370#define ETH_ERROR_SUMMARY 0x00000001
371
372/* Tx & Rx descriptors command */
373#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
374
375/* Tx descriptors status */
376#define ETH_LC_ERROR 0
377#define ETH_UR_ERROR 0x00000002
378#define ETH_RL_ERROR 0x00000004
379#define ETH_LLC_SNAP_FORMAT 0x00000200
380
381/* Rx descriptors status */
382#define ETH_OVERRUN_ERROR 0x00000002
383#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
384#define ETH_RESOURCE_ERROR 0x00000006
385#define ETH_VLAN_TAGGED 0x00080000
386#define ETH_BPDU_FRAME 0x00100000
387#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
388#define ETH_OTHER_FRAME_TYPE 0x00400000
389#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
390#define ETH_FRAME_TYPE_IP_V_4 0x01000000
391#define ETH_FRAME_HEADER_OK 0x02000000
392#define ETH_RX_LAST_DESC 0x04000000
393#define ETH_RX_FIRST_DESC 0x08000000
394#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
395#define ETH_RX_ENABLE_INTERRUPT 0x20000000
396#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
397
398/* Rx descriptors byte count */
399#define ETH_FRAME_FRAGMENTED 0x00000004
400
401/* Tx descriptors command */
402#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
403#define ETH_FRAME_SET_TO_VLAN 0x00008000
404#define ETH_UDP_FRAME 0x00010000
405#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
406#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
407#define ETH_ZERO_PADDING 0x00080000
408#define ETH_TX_LAST_DESC 0x00100000
409#define ETH_TX_FIRST_DESC 0x00200000
410#define ETH_GEN_CRC 0x00400000
411#define ETH_TX_ENABLE_INTERRUPT 0x00800000
412#define ETH_AUTO_MODE 0x40000000
413
414#define ETH_TX_IHL_SHIFT 11
415
416/* typedefs */
417
418typedef enum _eth_func_ret_status {
419 ETH_OK, /* Returned as expected. */
420 ETH_ERROR, /* Fundamental error. */
421 ETH_RETRY, /* Could not process request. Try later.*/
422 ETH_END_OF_JOB, /* Ring has nothing to process. */
423 ETH_QUEUE_FULL, /* Ring resource error. */
424 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
425} ETH_FUNC_RET_STATUS;
426
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200427/* These are for big-endian machines. Little endian needs different
428 * definitions.
429 */
430#if defined(__BIG_ENDIAN)
431struct eth_rx_desc {
432 u16 byte_cnt; /* Descriptor buffer byte count */
433 u16 buf_size; /* Buffer size */
434 u32 cmd_sts; /* Descriptor command status */
435 u32 next_desc_ptr; /* Next descriptor pointer */
436 u32 buf_ptr; /* Descriptor buffer pointer */
437};
438
439struct eth_tx_desc {
440 u16 byte_cnt; /* buffer byte count */
441 u16 l4i_chk; /* CPU provided TCP checksum */
442 u32 cmd_sts; /* Command/status field */
443 u32 next_desc_ptr; /* Pointer to next descriptor */
444 u32 buf_ptr; /* pointer to buffer for this descriptor*/
445};
446#elif defined(__LITTLE_ENDIAN)
447struct eth_rx_desc {
448 u32 cmd_sts; /* Descriptor command status */
449 u16 buf_size; /* Buffer size */
450 u16 byte_cnt; /* Descriptor buffer byte count */
451 u32 buf_ptr; /* Descriptor buffer pointer */
452 u32 next_desc_ptr; /* Next descriptor pointer */
453};
454
455struct eth_tx_desc {
456 u32 cmd_sts; /* Command/status field */
457 u16 l4i_chk; /* CPU provided TCP checksum */
458 u16 byte_cnt; /* buffer byte count */
459 u32 buf_ptr; /* pointer to buffer for this descriptor*/
460 u32 next_desc_ptr; /* Pointer to next descriptor */
461};
462#else
463#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
464#endif
465
466/* Unified struct for Rx and Tx operations. The user is not required to */
467/* be familier with neither Tx nor Rx descriptors. */
468struct pkt_info {
469 unsigned short byte_cnt; /* Descriptor buffer byte count */
470 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
471 unsigned int cmd_sts; /* Descriptor command status */
472 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
473 struct sk_buff *return_info; /* User resource return information */
474};
475
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200476
477/* global *******************************************************************/
478struct mv643xx_shared_private {
479 void __iomem *eth_base;
480
481 /* used to protect SMI_REG, which is shared across ports */
482 spinlock_t phy_lock;
483
484 u32 win_protect;
485
486 unsigned int t_clk;
487};
488
489
490/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200491struct mv643xx_mib_counters {
492 u64 good_octets_received;
493 u32 bad_octets_received;
494 u32 internal_mac_transmit_err;
495 u32 good_frames_received;
496 u32 bad_frames_received;
497 u32 broadcast_frames_received;
498 u32 multicast_frames_received;
499 u32 frames_64_octets;
500 u32 frames_65_to_127_octets;
501 u32 frames_128_to_255_octets;
502 u32 frames_256_to_511_octets;
503 u32 frames_512_to_1023_octets;
504 u32 frames_1024_to_max_octets;
505 u64 good_octets_sent;
506 u32 good_frames_sent;
507 u32 excessive_collision;
508 u32 multicast_frames_sent;
509 u32 broadcast_frames_sent;
510 u32 unrec_mac_control_received;
511 u32 fc_sent;
512 u32 good_fc_received;
513 u32 bad_fc_received;
514 u32 undersize_received;
515 u32 fragments_received;
516 u32 oversize_received;
517 u32 jabber_received;
518 u32 mac_receive_error;
519 u32 bad_crc_event;
520 u32 collision;
521 u32 late_collision;
522};
523
524struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200525 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200526 int port_num; /* User Ethernet port number */
527
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200528 struct mv643xx_shared_private *shared_smi;
529
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200530 u32 rx_sram_addr; /* Base address of rx sram area */
531 u32 rx_sram_size; /* Size of rx sram area */
532 u32 tx_sram_addr; /* Base address of tx sram area */
533 u32 tx_sram_size; /* Size of tx sram area */
534
535 int rx_resource_err; /* Rx ring resource error flag */
536
537 /* Tx/Rx rings managment indexes fields. For driver use */
538
539 /* Next available and first returning Rx resource */
540 int rx_curr_desc_q, rx_used_desc_q;
541
542 /* Next available and first returning Tx resource */
543 int tx_curr_desc_q, tx_used_desc_q;
544
545#ifdef MV643XX_TX_FAST_REFILL
546 u32 tx_clean_threshold;
547#endif
548
549 struct eth_rx_desc *p_rx_desc_area;
550 dma_addr_t rx_desc_dma;
551 int rx_desc_area_size;
552 struct sk_buff **rx_skb;
553
554 struct eth_tx_desc *p_tx_desc_area;
555 dma_addr_t tx_desc_dma;
556 int tx_desc_area_size;
557 struct sk_buff **tx_skb;
558
559 struct work_struct tx_timeout_task;
560
561 struct net_device *dev;
562 struct napi_struct napi;
563 struct net_device_stats stats;
564 struct mv643xx_mib_counters mib_counters;
565 spinlock_t lock;
566 /* Size of Tx Ring per queue */
567 int tx_ring_size;
568 /* Number of tx descriptors in use */
569 int tx_desc_count;
570 /* Size of Rx Ring per queue */
571 int rx_ring_size;
572 /* Number of rx descriptors in use */
573 int rx_desc_count;
574
575 /*
576 * Used in case RX Ring is empty, which can be caused when
577 * system does not have resources (skb's)
578 */
579 struct timer_list timeout;
580
581 u32 rx_int_coal;
582 u32 tx_int_coal;
583 struct mii_if_info mii;
584};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200586
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200587/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700588static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200590 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700593static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200595 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200598
599/* rxq/txq helper functions *************************************************/
600static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
601 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200603 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200606static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
607{
608 unsigned int port_num = mp->port_num;
609 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700610
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200611 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200612 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200613 if (queues) {
614 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200615 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200616
617 /* Wait for all Rx activity to terminate. */
618 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200619 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200620 udelay(PHY_WAIT_MICRO_SECONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200623 return queues;
624}
625
626static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
627 unsigned int queues)
628{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200629 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200630}
631
632static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
633{
634 unsigned int port_num = mp->port_num;
635 u32 queues;
636
637 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200638 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200639 if (queues) {
640 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200641 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200642
643 /* Wait for all Tx activity to terminate. */
644 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200645 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200646 udelay(PHY_WAIT_MICRO_SECONDS);
647
648 /* Wait for Tx FIFO to empty */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200649 while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200650 udelay(PHY_WAIT_MICRO_SECONDS);
651 }
652
653 return queues;
654}
655
656
657/* rx ***********************************************************************/
658static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
659
660/*
661 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
662 *
663 * DESCRIPTION:
664 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
665 * next 'used' descriptor and attached the returned buffer to it.
666 * In case the Rx ring was in "resource error" condition, where there are
667 * no available Rx resources, the function resets the resource error flag.
668 *
669 * INPUT:
670 * struct mv643xx_private *mp Ethernet Port Control srtuct.
671 * struct pkt_info *p_pkt_info Information on returned buffer.
672 *
673 * OUTPUT:
674 * New available Rx resource in Rx descriptor ring.
675 *
676 * RETURN:
677 * ETH_ERROR in case the routine can not access Rx desc ring.
678 * ETH_OK otherwise.
679 */
680static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
681 struct pkt_info *p_pkt_info)
682{
683 int used_rx_desc; /* Where to return Rx resource */
684 volatile struct eth_rx_desc *p_used_rx_desc;
685 unsigned long flags;
686
687 spin_lock_irqsave(&mp->lock, flags);
688
689 /* Get 'used' Rx descriptor */
690 used_rx_desc = mp->rx_used_desc_q;
691 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
692
693 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
694 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
695 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
696
697 /* Flush the write pipe */
698
699 /* Return the descriptor to DMA ownership */
700 wmb();
701 p_used_rx_desc->cmd_sts =
702 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
703 wmb();
704
705 /* Move the used descriptor pointer to the next descriptor */
706 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
707
708 /* Any Rx return cancels the Rx resource error status */
709 mp->rx_resource_err = 0;
710
711 spin_unlock_irqrestore(&mp->lock, flags);
712
713 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700717 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
719 * Fills / refills RX queue on a certain gigabit ethernet port
720 *
721 * Input : pointer to ethernet interface network device structure
722 * Output : N/A
723 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700724static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct mv643xx_private *mp = netdev_priv(dev);
727 struct pkt_info pkt_info;
728 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700729 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700731 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000732 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!skb)
734 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700735 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000736 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700737 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000738 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700740 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
741 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
742 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 pkt_info.return_info = skb;
744 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
745 printk(KERN_ERR
746 "%s: Error allocating RX Ring\n", dev->name);
747 break;
748 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700749 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
752 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700753 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700755 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700757 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700763 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
765 * Timer routine to wake up RX queue filling task. This function is
766 * used only in case the RX queue is empty, and all alloc_skb has
767 * failed (due to out of memory event).
768 *
769 * Input : pointer to ethernet interface network device structure
770 * Output : N/A
771 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700772static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700774 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200778 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200780 * DESCRIPTION:
781 * This routine returns the received data to the caller. There is no
782 * data copying during routine operation. All information is returned
783 * using pointer to packet information struct passed from the caller.
784 * If the routine exhausts Rx ring resources then the resource error flag
785 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200787 * INPUT:
788 * struct mv643xx_private *mp Ethernet Port Control srtuct.
789 * struct pkt_info *p_pkt_info User packet buffer.
790 *
791 * OUTPUT:
792 * Rx ring current and used indexes are updated.
793 *
794 * RETURN:
795 * ETH_ERROR in case the routine can not access Rx desc ring.
796 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
797 * ETH_END_OF_JOB if there is no received data.
798 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200800static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
801 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200803 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
804 volatile struct eth_rx_desc *p_rx_desc;
805 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700806 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200808 /* Do not process Rx ring in case of Rx ring resource error */
809 if (mp->rx_resource_err)
810 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700811
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200812 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700813
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200814 /* Get the Rx Desc ring 'curr and 'used' indexes */
815 rx_curr_desc = mp->rx_curr_desc_q;
816 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200818 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700819
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200820 /* The following parameters are used to save readings from memory */
821 command_status = p_rx_desc->cmd_sts;
822 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700823
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200824 /* Nothing to receive... */
825 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700826 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200827 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200830 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
831 p_pkt_info->cmd_sts = command_status;
832 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
833 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
834 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200836 /*
837 * Clean the return info field to indicate that the
838 * packet has been moved to the upper layers
839 */
840 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700841
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200842 /* Update current index in data structure */
843 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
844 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700845
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200846 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
847 if (rx_next_curr_desc == rx_used_desc)
848 mp->rx_resource_err = 1;
849
850 spin_unlock_irqrestore(&mp->lock, flags);
851
852 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/*
856 * mv643xx_eth_receive
857 *
858 * This function is forward packets that are received from the port's
859 * queues toward kernel core or FastRoute them to another interface.
860 *
861 * Input : dev - a pointer to the required interface
862 * max - maximum number to receive (0 means unlimted)
863 *
864 * Output : number of served packets
865 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700869 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 unsigned int received_packets = 0;
871 struct sk_buff *skb;
872 struct pkt_info pkt_info;
873
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700874 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400875 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700876 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700877 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700879
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700880 /*
881 * Update statistics.
882 * Note byte count includes 4 byte CRC count
883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 stats->rx_packets++;
885 stats->rx_bytes += pkt_info.byte_cnt;
886 skb = pkt_info.return_info;
887 /*
888 * In case received a packet without first / last bits on OR
889 * the error summary bit is on, the packets needs to be dropeed.
890 */
891 if (((pkt_info.cmd_sts
892 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
893 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
894 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
895 stats->rx_dropped++;
896 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
897 ETH_RX_LAST_DESC)) !=
898 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
899 if (net_ratelimit())
900 printk(KERN_ERR
901 "%s: Received packet spread "
902 "on multiple descriptors\n",
903 dev->name);
904 }
905 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
906 stats->rx_errors++;
907
908 dev_kfree_skb_irq(skb);
909 } else {
910 /*
911 * The -4 is for the CRC in the trailer of the
912 * received packet
913 */
914 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
917 skb->ip_summed = CHECKSUM_UNNECESSARY;
918 skb->csum = htons(
919 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
920 }
921 skb->protocol = eth_type_trans(skb, dev);
922#ifdef MV643XX_NAPI
923 netif_receive_skb(skb);
924#else
925 netif_rx(skb);
926#endif
927 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700928 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700930 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 return received_packets;
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * mv643xx_poll
938 *
939 * This function is used in case of NAPI
940 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700941static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700943 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
944 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700946 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948#ifdef MV643XX_TX_FAST_REFILL
949 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700950 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953#endif
954
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700955 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200956 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700957 != (u32) mp->rx_used_desc_q)
958 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700960 if (work_done < budget) {
961 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200962 wrl(mp, INT_CAUSE(port_num), 0);
963 wrl(mp, INT_CAUSE_EXT(port_num), 0);
964 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700967 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969#endif
970
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200971
972/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700973/**
974 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
975 *
976 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700977 * This helper function detects that case.
978 */
979
980static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
981{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700982 unsigned int frag;
983 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700984
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700985 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
986 fragp = &skb_shinfo(skb)->frags[frag];
987 if (fragp->size <= 8 && fragp->page_offset & 0x7)
988 return 1;
989 }
990 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700991}
992
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700993/**
994 * eth_alloc_tx_desc_index - return the index of the next available tx desc
995 */
996static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
997{
998 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700999
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001000 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001001
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001002 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001003 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
1004
1005 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
1006
1007 return tx_desc_curr;
1008}
1009
1010/**
1011 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001013 * Ensure the data for each fragment to be transmitted is mapped properly,
1014 * then fill in descriptors in the tx hw queue.
1015 */
1016static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
1017 struct sk_buff *skb)
1018{
1019 int frag;
1020 int tx_index;
1021 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001022
1023 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1024 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1025
1026 tx_index = eth_alloc_tx_desc_index(mp);
1027 desc = &mp->p_tx_desc_area[tx_index];
1028
1029 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
1030 /* Last Frag enables interrupt and frees the skb */
1031 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
1032 desc->cmd_sts |= ETH_ZERO_PADDING |
1033 ETH_TX_LAST_DESC |
1034 ETH_TX_ENABLE_INTERRUPT;
1035 mp->tx_skb[tx_index] = skb;
1036 } else
Al Viro05980772006-05-30 23:59:09 -04001037 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001038
1039 desc = &mp->p_tx_desc_area[tx_index];
1040 desc->l4i_chk = 0;
1041 desc->byte_cnt = this_frag->size;
1042 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
1043 this_frag->page_offset,
1044 this_frag->size,
1045 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001046 }
1047}
1048
Byron Bradley324ff2c2008-02-04 23:47:15 -08001049static inline __be16 sum16_as_be(__sum16 sum)
1050{
1051 return (__force __be16)sum;
1052}
1053
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001054/**
1055 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001057 * Ensure the data for an skb to be transmitted is mapped properly,
1058 * then fill in descriptors in the tx hw queue and start the hardware.
1059 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001060static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1061 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001062{
1063 int tx_index;
1064 struct eth_tx_desc *desc;
1065 u32 cmd_sts;
1066 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001067 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001068
1069 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1070
1071 tx_index = eth_alloc_tx_desc_index(mp);
1072 desc = &mp->p_tx_desc_area[tx_index];
1073
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001074 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001075 eth_tx_fill_frag_descs(mp, skb);
1076
1077 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -04001078 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001079 } else {
1080 cmd_sts |= ETH_ZERO_PADDING |
1081 ETH_TX_LAST_DESC |
1082 ETH_TX_ENABLE_INTERRUPT;
1083 length = skb->len;
1084 mp->tx_skb[tx_index] = skb;
1085 }
1086
1087 desc->byte_cnt = length;
1088 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001089
Patrick McHardy84fa7932006-08-29 16:44:56 -07001090 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -08001091 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001092
1093 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1094 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001095 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001096
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001097 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001098 case IPPROTO_UDP:
1099 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -08001100 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001101 break;
1102 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -08001103 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001104 break;
1105 default:
1106 BUG();
1107 }
1108 } else {
1109 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1110 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1111 desc->l4i_chk = 0;
1112 }
1113
1114 /* ensure all other descriptors are written before first cmd_sts */
1115 wmb();
1116 desc->cmd_sts = cmd_sts;
1117
1118 /* ensure all descriptors are written before poking hardware */
1119 wmb();
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001120 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001121
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001122 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001123}
1124
1125/**
1126 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
1129static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1130{
1131 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001132 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001135 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001136
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001137 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1138 stats->tx_dropped++;
1139 printk(KERN_DEBUG "%s: failed to linearize tiny "
1140 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001141 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 spin_lock_irqsave(&mp->lock, flags);
1145
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001146 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1147 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1148 netif_stop_queue(dev);
1149 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001150 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001151 }
1152
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001153 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001154 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 stats->tx_packets++;
1156 dev->trans_start = jiffies;
1157
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001158 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1159 netif_stop_queue(dev);
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_unlock_irqrestore(&mp->lock, flags);
1162
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001163 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001166
1167/* mii management interface *************************************************/
1168static int ethernet_phy_get(struct mv643xx_private *mp);
1169
1170/*
1171 * eth_port_read_smi_reg - Read PHY registers
1172 *
1173 * DESCRIPTION:
1174 * This routine utilize the SMI interface to interact with the PHY in
1175 * order to perform PHY register read.
1176 *
1177 * INPUT:
1178 * struct mv643xx_private *mp Ethernet Port.
1179 * unsigned int phy_reg PHY register address offset.
1180 * unsigned int *value Register value buffer.
1181 *
1182 * OUTPUT:
1183 * Write the value of a specified PHY register into given buffer.
1184 *
1185 * RETURN:
1186 * false if the PHY is busy or read data is not in valid state.
1187 * true otherwise.
1188 *
1189 */
1190static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1191 unsigned int phy_reg, unsigned int *value)
1192{
1193 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1194 int phy_addr = ethernet_phy_get(mp);
1195 unsigned long flags;
1196 int i;
1197
1198 /* the SMI register is a shared resource */
1199 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1200
1201 /* wait for the SMI register to become available */
1202 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1203 if (i == PHY_WAIT_ITERATIONS) {
1204 printk("%s: PHY busy timeout\n", mp->dev->name);
1205 goto out;
1206 }
1207 udelay(PHY_WAIT_MICRO_SECONDS);
1208 }
1209
1210 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1211 smi_reg);
1212
1213 /* now wait for the data to be valid */
1214 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
1215 if (i == PHY_WAIT_ITERATIONS) {
1216 printk("%s: PHY read timeout\n", mp->dev->name);
1217 goto out;
1218 }
1219 udelay(PHY_WAIT_MICRO_SECONDS);
1220 }
1221
1222 *value = readl(smi_reg) & 0xffff;
1223out:
1224 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1225}
1226
1227/*
1228 * eth_port_write_smi_reg - Write to PHY registers
1229 *
1230 * DESCRIPTION:
1231 * This routine utilize the SMI interface to interact with the PHY in
1232 * order to perform writes to PHY registers.
1233 *
1234 * INPUT:
1235 * struct mv643xx_private *mp Ethernet Port.
1236 * unsigned int phy_reg PHY register address offset.
1237 * unsigned int value Register value.
1238 *
1239 * OUTPUT:
1240 * Write the given value to the specified PHY register.
1241 *
1242 * RETURN:
1243 * false if the PHY is busy.
1244 * true otherwise.
1245 *
1246 */
1247static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1248 unsigned int phy_reg, unsigned int value)
1249{
1250 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1251 int phy_addr = ethernet_phy_get(mp);
1252 unsigned long flags;
1253 int i;
1254
1255 /* the SMI register is a shared resource */
1256 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1257
1258 /* wait for the SMI register to become available */
1259 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1260 if (i == PHY_WAIT_ITERATIONS) {
1261 printk("%s: PHY busy timeout\n", mp->dev->name);
1262 goto out;
1263 }
1264 udelay(PHY_WAIT_MICRO_SECONDS);
1265 }
1266
1267 writel((phy_addr << 16) | (phy_reg << 21) |
1268 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1269out:
1270 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1271}
1272
1273
1274/* mib counters *************************************************************/
1275/*
1276 * eth_clear_mib_counters - Clear all MIB counters
1277 *
1278 * DESCRIPTION:
1279 * This function clears all MIB counters of a specific ethernet port.
1280 * A read from the MIB counter will reset the counter.
1281 *
1282 * INPUT:
1283 * struct mv643xx_private *mp Ethernet Port.
1284 *
1285 * OUTPUT:
1286 * After reading all MIB counters, the counters resets.
1287 *
1288 * RETURN:
1289 * MIB counter value.
1290 *
1291 */
1292static void eth_clear_mib_counters(struct mv643xx_private *mp)
1293{
1294 unsigned int port_num = mp->port_num;
1295 int i;
1296
1297 /* Perform dummy reads from MIB counters */
1298 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
1299 i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001300 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001301}
1302
1303static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1304{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001305 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001306}
1307
1308static void eth_update_mib_counters(struct mv643xx_private *mp)
1309{
1310 struct mv643xx_mib_counters *p = &mp->mib_counters;
1311 int offset;
1312
1313 p->good_octets_received +=
1314 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
1315 p->good_octets_received +=
1316 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
1317
1318 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
1319 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
1320 offset += 4)
1321 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1322
1323 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
1324 p->good_octets_sent +=
1325 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
1326
1327 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
1328 offset <= ETH_MIB_LATE_COLLISION;
1329 offset += 4)
1330 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1331}
1332
1333
1334/* ethtool ******************************************************************/
1335struct mv643xx_stats {
1336 char stat_string[ETH_GSTRING_LEN];
1337 int sizeof_stat;
1338 int stat_offset;
1339};
1340
1341#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1342 offsetof(struct mv643xx_private, m)
1343
1344static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1345 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1346 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1347 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1348 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1349 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1350 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1351 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1352 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1353 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1354 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1355 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1356 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1357 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1358 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1359 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1360 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1361 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1362 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1363 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1364 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1365 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1366 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1367 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1368 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1369 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1370 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1371 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1372 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1373 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1374 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1375 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1376 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1377 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1378 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1379 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1380 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1381 { "collision", MV643XX_STAT(mib_counters.collision) },
1382 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1383};
1384
1385#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1386
1387static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1388{
1389 struct mv643xx_private *mp = netdev_priv(dev);
1390 int err;
1391
1392 spin_lock_irq(&mp->lock);
1393 err = mii_ethtool_gset(&mp->mii, cmd);
1394 spin_unlock_irq(&mp->lock);
1395
1396 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1397 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1398 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1399
1400 return err;
1401}
1402
1403static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1404{
1405 struct mv643xx_private *mp = netdev_priv(dev);
1406 int err;
1407
1408 spin_lock_irq(&mp->lock);
1409 err = mii_ethtool_sset(&mp->mii, cmd);
1410 spin_unlock_irq(&mp->lock);
1411
1412 return err;
1413}
1414
1415static void mv643xx_get_drvinfo(struct net_device *netdev,
1416 struct ethtool_drvinfo *drvinfo)
1417{
1418 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1419 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1420 strncpy(drvinfo->fw_version, "N/A", 32);
1421 strncpy(drvinfo->bus_info, "mv643xx", 32);
1422 drvinfo->n_stats = MV643XX_STATS_LEN;
1423}
1424
1425static int mv643xx_eth_nway_restart(struct net_device *dev)
1426{
1427 struct mv643xx_private *mp = netdev_priv(dev);
1428
1429 return mii_nway_restart(&mp->mii);
1430}
1431
1432static u32 mv643xx_eth_get_link(struct net_device *dev)
1433{
1434 struct mv643xx_private *mp = netdev_priv(dev);
1435
1436 return mii_link_ok(&mp->mii);
1437}
1438
1439static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1440 uint8_t *data)
1441{
1442 int i;
1443
1444 switch(stringset) {
1445 case ETH_SS_STATS:
1446 for (i=0; i < MV643XX_STATS_LEN; i++) {
1447 memcpy(data + i * ETH_GSTRING_LEN,
1448 mv643xx_gstrings_stats[i].stat_string,
1449 ETH_GSTRING_LEN);
1450 }
1451 break;
1452 }
1453}
1454
1455static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1456 struct ethtool_stats *stats, uint64_t *data)
1457{
1458 struct mv643xx_private *mp = netdev->priv;
1459 int i;
1460
1461 eth_update_mib_counters(mp);
1462
1463 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1464 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1465 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1466 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1467 }
1468}
1469
1470static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1471{
1472 switch (sset) {
1473 case ETH_SS_STATS:
1474 return MV643XX_STATS_LEN;
1475 default:
1476 return -EOPNOTSUPP;
1477 }
1478}
1479
1480static const struct ethtool_ops mv643xx_ethtool_ops = {
1481 .get_settings = mv643xx_get_settings,
1482 .set_settings = mv643xx_set_settings,
1483 .get_drvinfo = mv643xx_get_drvinfo,
1484 .get_link = mv643xx_eth_get_link,
1485 .set_sg = ethtool_op_set_sg,
1486 .get_sset_count = mv643xx_get_sset_count,
1487 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1488 .get_strings = mv643xx_get_strings,
1489 .nway_reset = mv643xx_eth_nway_restart,
1490};
1491
1492
1493/* address handling *********************************************************/
1494/*
1495 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1496 */
1497static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1498 unsigned char *p_addr)
1499{
1500 unsigned int port_num = mp->port_num;
1501 unsigned int mac_h;
1502 unsigned int mac_l;
1503
1504 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1505 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1506
1507 p_addr[0] = (mac_h >> 24) & 0xff;
1508 p_addr[1] = (mac_h >> 16) & 0xff;
1509 p_addr[2] = (mac_h >> 8) & 0xff;
1510 p_addr[3] = mac_h & 0xff;
1511 p_addr[4] = (mac_l >> 8) & 0xff;
1512 p_addr[5] = mac_l & 0xff;
1513}
1514
1515/*
1516 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1517 *
1518 * DESCRIPTION:
1519 * Go through all the DA filter tables (Unicast, Special Multicast &
1520 * Other Multicast) and set each entry to 0.
1521 *
1522 * INPUT:
1523 * struct mv643xx_private *mp Ethernet Port.
1524 *
1525 * OUTPUT:
1526 * Multicast and Unicast packets are rejected.
1527 *
1528 * RETURN:
1529 * None.
1530 */
1531static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1532{
1533 unsigned int port_num = mp->port_num;
1534 int table_index;
1535
1536 /* Clear DA filter unicast table (Ex_dFUT) */
1537 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001538 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001539
1540 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1541 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001542 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001543 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001544 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001545 }
1546}
1547
1548/*
1549 * The entries in each table are indexed by a hash of a packet's MAC
1550 * address. One bit in each entry determines whether the packet is
1551 * accepted. There are 4 entries (each 8 bits wide) in each register
1552 * of the table. The bits in each entry are defined as follows:
1553 * 0 Accept=1, Drop=0
1554 * 3-1 Queue (ETH_Q0=0)
1555 * 7-4 Reserved = 0;
1556 */
1557static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1558 int table, unsigned char entry)
1559{
1560 unsigned int table_reg;
1561 unsigned int tbl_offset;
1562 unsigned int reg_offset;
1563
1564 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1565 reg_offset = entry % 4; /* Entry offset within the register */
1566
1567 /* Set "accepts frame bit" at specified table entry */
1568 table_reg = rdl(mp, table + tbl_offset);
1569 table_reg |= 0x01 << (8 * reg_offset);
1570 wrl(mp, table + tbl_offset, table_reg);
1571}
1572
1573/*
1574 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1575 */
1576static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1577 unsigned char *p_addr)
1578{
1579 unsigned int port_num = mp->port_num;
1580 unsigned int mac_h;
1581 unsigned int mac_l;
1582 int table;
1583
1584 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1585 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1586 (p_addr[3] << 0);
1587
1588 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1589 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1590
1591 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001592 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001593 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1594}
1595
1596/*
1597 * mv643xx_eth_update_mac_address
1598 *
1599 * Update the MAC address of the port in the address table
1600 *
1601 * Input : pointer to ethernet interface network device structure
1602 * Output : N/A
1603 */
1604static void mv643xx_eth_update_mac_address(struct net_device *dev)
1605{
1606 struct mv643xx_private *mp = netdev_priv(dev);
1607
1608 eth_port_init_mac_tables(mp);
1609 eth_port_uc_addr_set(mp, dev->dev_addr);
1610}
1611
1612/*
1613 * mv643xx_eth_set_mac_address
1614 *
1615 * Change the interface's mac address.
1616 * No special hardware thing should be done because interface is always
1617 * put in promiscuous mode.
1618 *
1619 * Input : pointer to ethernet interface network device structure and
1620 * a pointer to the designated entry to be added to the cache.
1621 * Output : zero upon success, negative upon failure
1622 */
1623static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1624{
1625 int i;
1626
1627 for (i = 0; i < 6; i++)
1628 /* +2 is for the offset of the HW addr type */
1629 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1630 mv643xx_eth_update_mac_address(dev);
1631 return 0;
1632}
1633
1634/*
1635 * eth_port_mc_addr - Multicast address settings.
1636 *
1637 * The MV device supports multicast using two tables:
1638 * 1) Special Multicast Table for MAC addresses of the form
1639 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1640 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1641 * Table entries in the DA-Filter table.
1642 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1643 * is used as an index to the Other Multicast Table entries in the
1644 * DA-Filter table. This function calculates the CRC-8bit value.
1645 * In either case, eth_port_set_filter_table_entry() is then called
1646 * to set to set the actual table entry.
1647 */
1648static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1649{
1650 unsigned int port_num = mp->port_num;
1651 unsigned int mac_h;
1652 unsigned int mac_l;
1653 unsigned char crc_result = 0;
1654 int table;
1655 int mac_array[48];
1656 int crc[8];
1657 int i;
1658
1659 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1660 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001661 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001662 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1663 return;
1664 }
1665
1666 /* Calculate CRC-8 out of the given address */
1667 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1668 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1669 (p_addr[4] << 8) | (p_addr[5] << 0);
1670
1671 for (i = 0; i < 32; i++)
1672 mac_array[i] = (mac_l >> i) & 0x1;
1673 for (i = 32; i < 48; i++)
1674 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1675
1676 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1677 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1678 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1679 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1680 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1681
1682 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1683 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1684 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1685 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1686 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1687 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1688 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1689
1690 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1691 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1692 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1693 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1694 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1695 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1696
1697 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1698 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1699 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1700 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1701 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1702 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1703
1704 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1705 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1706 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1707 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1708 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1709 mac_array[3] ^ mac_array[2];
1710
1711 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1712 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1713 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1714 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1715 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1716 mac_array[4] ^ mac_array[3];
1717
1718 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1719 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1720 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1721 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1722 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1723 mac_array[4];
1724
1725 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1726 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1727 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1728 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1729 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1730
1731 for (i = 0; i < 8; i++)
1732 crc_result = crc_result | (crc[i] << i);
1733
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001734 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001735 eth_port_set_filter_table_entry(mp, table, crc_result);
1736}
1737
1738/*
1739 * Set the entire multicast list based on dev->mc_list.
1740 */
1741static void eth_port_set_multicast_list(struct net_device *dev)
1742{
1743
1744 struct dev_mc_list *mc_list;
1745 int i;
1746 int table_index;
1747 struct mv643xx_private *mp = netdev_priv(dev);
1748 unsigned int eth_port_num = mp->port_num;
1749
1750 /* If the device is in promiscuous mode or in all multicast mode,
1751 * we will fully populate both multicast tables with accept.
1752 * This is guaranteed to yield a match on all multicast addresses...
1753 */
1754 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1755 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1756 /* Set all entries in DA filter special multicast
1757 * table (Ex_dFSMT)
1758 * Set for ETH_Q0 for now
1759 * Bits
1760 * 0 Accept=1, Drop=0
1761 * 3-1 Queue ETH_Q0=0
1762 * 7-4 Reserved = 0;
1763 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001764 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765
1766 /* Set all entries in DA filter other multicast
1767 * table (Ex_dFOMT)
1768 * Set for ETH_Q0 for now
1769 * Bits
1770 * 0 Accept=1, Drop=0
1771 * 3-1 Queue ETH_Q0=0
1772 * 7-4 Reserved = 0;
1773 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001774 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001775 }
1776 return;
1777 }
1778
1779 /* We will clear out multicast tables every time we get the list.
1780 * Then add the entire new list...
1781 */
1782 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1783 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001784 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001785
1786 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001787 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001788 }
1789
1790 /* Get pointer to net_device multicast list and add each one... */
1791 for (i = 0, mc_list = dev->mc_list;
1792 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1793 i++, mc_list = mc_list->next)
1794 if (mc_list->dmi_addrlen == 6)
1795 eth_port_mc_addr(mp, mc_list->dmi_addr);
1796}
1797
1798/*
1799 * mv643xx_eth_set_rx_mode
1800 *
1801 * Change from promiscuos to regular rx mode
1802 *
1803 * Input : pointer to ethernet interface network device structure
1804 * Output : N/A
1805 */
1806static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1807{
1808 struct mv643xx_private *mp = netdev_priv(dev);
1809 u32 config_reg;
1810
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001811 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001812 if (dev->flags & IFF_PROMISC)
1813 config_reg |= (u32) UNICAST_PROMISCUOUS_MODE;
1814 else
1815 config_reg &= ~(u32) UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001816 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001817
1818 eth_port_set_multicast_list(dev);
1819}
1820
1821
1822/* rx/tx queue initialisation ***********************************************/
1823/*
1824 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1825 *
1826 * DESCRIPTION:
1827 * This function prepares a Rx chained list of descriptors and packet
1828 * buffers in a form of a ring. The routine must be called after port
1829 * initialization routine and before port start routine.
1830 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1831 * devices in the system (i.e. DRAM). This function uses the ethernet
1832 * struct 'virtual to physical' routine (set by the user) to set the ring
1833 * with physical addresses.
1834 *
1835 * INPUT:
1836 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1837 *
1838 * OUTPUT:
1839 * The routine updates the Ethernet port control struct with information
1840 * regarding the Rx descriptors and buffers.
1841 *
1842 * RETURN:
1843 * None.
1844 */
1845static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1846{
1847 volatile struct eth_rx_desc *p_rx_desc;
1848 int rx_desc_num = mp->rx_ring_size;
1849 int i;
1850
1851 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1852 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1853 for (i = 0; i < rx_desc_num; i++) {
1854 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1855 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1856 }
1857
1858 /* Save Rx desc pointer to driver struct. */
1859 mp->rx_curr_desc_q = 0;
1860 mp->rx_used_desc_q = 0;
1861
1862 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1863}
1864
1865static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1866{
1867 struct mv643xx_private *mp = netdev_priv(dev);
1868 int curr;
1869
1870 /* Stop RX Queues */
1871 mv643xx_eth_port_disable_rx(mp);
1872
1873 /* Free preallocated skb's on RX rings */
1874 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1875 if (mp->rx_skb[curr]) {
1876 dev_kfree_skb(mp->rx_skb[curr]);
1877 mp->rx_desc_count--;
1878 }
1879 }
1880
1881 if (mp->rx_desc_count)
1882 printk(KERN_ERR
1883 "%s: Error in freeing Rx Ring. %d skb's still"
1884 " stuck in RX Ring - ignoring them\n", dev->name,
1885 mp->rx_desc_count);
1886 /* Free RX ring */
1887 if (mp->rx_sram_size)
1888 iounmap(mp->p_rx_desc_area);
1889 else
1890 dma_free_coherent(NULL, mp->rx_desc_area_size,
1891 mp->p_rx_desc_area, mp->rx_desc_dma);
1892}
1893
1894/*
1895 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1896 *
1897 * DESCRIPTION:
1898 * This function prepares a Tx chained list of descriptors and packet
1899 * buffers in a form of a ring. The routine must be called after port
1900 * initialization routine and before port start routine.
1901 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1902 * devices in the system (i.e. DRAM). This function uses the ethernet
1903 * struct 'virtual to physical' routine (set by the user) to set the ring
1904 * with physical addresses.
1905 *
1906 * INPUT:
1907 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1908 *
1909 * OUTPUT:
1910 * The routine updates the Ethernet port control struct with information
1911 * regarding the Tx descriptors and buffers.
1912 *
1913 * RETURN:
1914 * None.
1915 */
1916static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1917{
1918 int tx_desc_num = mp->tx_ring_size;
1919 struct eth_tx_desc *p_tx_desc;
1920 int i;
1921
1922 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1923 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1924 for (i = 0; i < tx_desc_num; i++) {
1925 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1926 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1927 }
1928
1929 mp->tx_curr_desc_q = 0;
1930 mp->tx_used_desc_q = 0;
1931
1932 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1933}
1934
1935/**
1936 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1937 *
1938 * If force is non-zero, frees uncompleted descriptors as well
1939 */
1940static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1941{
1942 struct mv643xx_private *mp = netdev_priv(dev);
1943 struct eth_tx_desc *desc;
1944 u32 cmd_sts;
1945 struct sk_buff *skb;
1946 unsigned long flags;
1947 int tx_index;
1948 dma_addr_t addr;
1949 int count;
1950 int released = 0;
1951
1952 while (mp->tx_desc_count > 0) {
1953 spin_lock_irqsave(&mp->lock, flags);
1954
1955 /* tx_desc_count might have changed before acquiring the lock */
1956 if (mp->tx_desc_count <= 0) {
1957 spin_unlock_irqrestore(&mp->lock, flags);
1958 return released;
1959 }
1960
1961 tx_index = mp->tx_used_desc_q;
1962 desc = &mp->p_tx_desc_area[tx_index];
1963 cmd_sts = desc->cmd_sts;
1964
1965 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1966 spin_unlock_irqrestore(&mp->lock, flags);
1967 return released;
1968 }
1969
1970 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1971 mp->tx_desc_count--;
1972
1973 addr = desc->buf_ptr;
1974 count = desc->byte_cnt;
1975 skb = mp->tx_skb[tx_index];
1976 if (skb)
1977 mp->tx_skb[tx_index] = NULL;
1978
1979 if (cmd_sts & ETH_ERROR_SUMMARY) {
1980 printk("%s: Error in TX\n", dev->name);
1981 dev->stats.tx_errors++;
1982 }
1983
1984 spin_unlock_irqrestore(&mp->lock, flags);
1985
1986 if (cmd_sts & ETH_TX_FIRST_DESC)
1987 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1988 else
1989 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1990
1991 if (skb)
1992 dev_kfree_skb_irq(skb);
1993
1994 released = 1;
1995 }
1996
1997 return released;
1998}
1999
2000static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
2001{
2002 struct mv643xx_private *mp = netdev_priv(dev);
2003
2004 if (mv643xx_eth_free_tx_descs(dev, 0) &&
2005 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2006 netif_wake_queue(dev);
2007}
2008
2009static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
2010{
2011 mv643xx_eth_free_tx_descs(dev, 1);
2012}
2013
2014static void mv643xx_eth_free_tx_rings(struct net_device *dev)
2015{
2016 struct mv643xx_private *mp = netdev_priv(dev);
2017
2018 /* Stop Tx Queues */
2019 mv643xx_eth_port_disable_tx(mp);
2020
2021 /* Free outstanding skb's on TX ring */
2022 mv643xx_eth_free_all_tx_descs(dev);
2023
2024 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
2025
2026 /* Free TX ring */
2027 if (mp->tx_sram_size)
2028 iounmap(mp->p_tx_desc_area);
2029 else
2030 dma_free_coherent(NULL, mp->tx_desc_area_size,
2031 mp->p_tx_desc_area, mp->tx_desc_dma);
2032}
2033
2034
2035/* netdev ops and related ***************************************************/
2036static void eth_port_reset(struct mv643xx_private *mp);
2037
2038/* Set the mv643xx port configuration register for the speed/duplex mode. */
2039static void mv643xx_eth_update_pscr(struct net_device *dev,
2040 struct ethtool_cmd *ecmd)
2041{
2042 struct mv643xx_private *mp = netdev_priv(dev);
2043 int port_num = mp->port_num;
2044 u32 o_pscr, n_pscr;
2045 unsigned int queues;
2046
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002047 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002048 n_pscr = o_pscr;
2049
2050 /* clear speed, duplex and rx buffer size fields */
2051 n_pscr &= ~(SET_MII_SPEED_TO_100 |
2052 SET_GMII_SPEED_TO_1000 |
2053 SET_FULL_DUPLEX_MODE |
2054 MAX_RX_PACKET_MASK);
2055
2056 if (ecmd->duplex == DUPLEX_FULL)
2057 n_pscr |= SET_FULL_DUPLEX_MODE;
2058
2059 if (ecmd->speed == SPEED_1000)
2060 n_pscr |= SET_GMII_SPEED_TO_1000 |
2061 MAX_RX_PACKET_9700BYTE;
2062 else {
2063 if (ecmd->speed == SPEED_100)
2064 n_pscr |= SET_MII_SPEED_TO_100;
2065 n_pscr |= MAX_RX_PACKET_1522BYTE;
2066 }
2067
2068 if (n_pscr != o_pscr) {
2069 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002070 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002071 else {
2072 queues = mv643xx_eth_port_disable_tx(mp);
2073
2074 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002075 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
2076 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
2077 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002078 if (queues)
2079 mv643xx_eth_port_enable_tx(mp, queues);
2080 }
2081 }
2082}
2083
2084/*
2085 * mv643xx_eth_int_handler
2086 *
2087 * Main interrupt handler for the gigbit ethernet ports
2088 *
2089 * Input : irq - irq number (not used)
2090 * dev_id - a pointer to the required interface's data structure
2091 * regs - not used
2092 * Output : N/A
2093 */
2094
2095static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
2096{
2097 struct net_device *dev = (struct net_device *)dev_id;
2098 struct mv643xx_private *mp = netdev_priv(dev);
2099 u32 eth_int_cause, eth_int_cause_ext = 0;
2100 unsigned int port_num = mp->port_num;
2101
2102 /* Read interrupt cause registers */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002103 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & ETH_INT_UNMASK_ALL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002104 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002105 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
2106 & ETH_INT_UNMASK_ALL_EXT;
2107 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002108 }
2109
2110 /* PHY status changed */
2111 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
2112 struct ethtool_cmd cmd;
2113
2114 if (mii_link_ok(&mp->mii)) {
2115 mii_ethtool_gset(&mp->mii, &cmd);
2116 mv643xx_eth_update_pscr(dev, &cmd);
2117 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
2118 if (!netif_carrier_ok(dev)) {
2119 netif_carrier_on(dev);
2120 if (mp->tx_ring_size - mp->tx_desc_count >=
2121 MAX_DESCS_PER_SKB)
2122 netif_wake_queue(dev);
2123 }
2124 } else if (netif_carrier_ok(dev)) {
2125 netif_stop_queue(dev);
2126 netif_carrier_off(dev);
2127 }
2128 }
2129
2130#ifdef MV643XX_NAPI
2131 if (eth_int_cause & ETH_INT_CAUSE_RX) {
2132 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002133 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002134
2135 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002136 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002137
2138 netif_rx_schedule(dev, &mp->napi);
2139 }
2140#else
2141 if (eth_int_cause & ETH_INT_CAUSE_RX)
2142 mv643xx_eth_receive_queue(dev, INT_MAX);
2143#endif
2144 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
2145 mv643xx_eth_free_completed_tx_descs(dev);
2146
2147 /*
2148 * If no real interrupt occured, exit.
2149 * This can happen when using gigE interrupt coalescing mechanism.
2150 */
2151 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
2152 return IRQ_NONE;
2153
2154 return IRQ_HANDLED;
2155}
2156
2157/*
2158 * ethernet_phy_reset - Reset Ethernet port PHY.
2159 *
2160 * DESCRIPTION:
2161 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2162 *
2163 * INPUT:
2164 * struct mv643xx_private *mp Ethernet Port.
2165 *
2166 * OUTPUT:
2167 * The PHY is reset.
2168 *
2169 * RETURN:
2170 * None.
2171 *
2172 */
2173static void ethernet_phy_reset(struct mv643xx_private *mp)
2174{
2175 unsigned int phy_reg_data;
2176
2177 /* Reset the PHY */
2178 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2179 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2180 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2181
2182 /* wait for PHY to come out of reset */
2183 do {
2184 udelay(1);
2185 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2186 } while (phy_reg_data & 0x8000);
2187}
2188
2189/*
2190 * eth_port_start - Start the Ethernet port activity.
2191 *
2192 * DESCRIPTION:
2193 * This routine prepares the Ethernet port for Rx and Tx activity:
2194 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2195 * has been initialized a descriptor's ring (using
2196 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2197 * 2. Initialize and enable the Ethernet configuration port by writing to
2198 * the port's configuration and command registers.
2199 * 3. Initialize and enable the SDMA by writing to the SDMA's
2200 * configuration and command registers. After completing these steps,
2201 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2202 *
2203 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2204 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2205 * and ether_init_rx_desc_ring for Rx queues).
2206 *
2207 * INPUT:
2208 * dev - a pointer to the required interface
2209 *
2210 * OUTPUT:
2211 * Ethernet port is ready to receive and transmit.
2212 *
2213 * RETURN:
2214 * None.
2215 */
2216static void eth_port_start(struct net_device *dev)
2217{
2218 struct mv643xx_private *mp = netdev_priv(dev);
2219 unsigned int port_num = mp->port_num;
2220 int tx_curr_desc, rx_curr_desc;
2221 u32 pscr;
2222 struct ethtool_cmd ethtool_cmd;
2223
2224 /* Assignment of Tx CTRP of given queue */
2225 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002226 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002227 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2228
2229 /* Assignment of Rx CRDP of given queue */
2230 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002231 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002232 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2233
2234 /* Add the assigned Ethernet address to the port's address table */
2235 eth_port_uc_addr_set(mp, dev->dev_addr);
2236
2237 /* Assign port configuration and command. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002238 wrl(mp, PORT_CONFIG(port_num), PORT_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002239
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002240 wrl(mp, PORT_CONFIG_EXT(port_num), PORT_CONFIG_EXTEND_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002241
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002242 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002243
2244 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002245 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002246
2247 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2248 DISABLE_AUTO_NEG_SPEED_GMII |
2249 DISABLE_AUTO_NEG_FOR_DUPLX |
2250 DO_NOT_FORCE_LINK_FAIL |
2251 SERIAL_PORT_CONTROL_RESERVED;
2252
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002253 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002254
2255 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002256 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002257
2258 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002259 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002260
2261 /* Enable port Rx. */
2262 mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
2263
2264 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002265 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002266
2267 /* save phy settings across reset */
2268 mv643xx_get_settings(dev, &ethtool_cmd);
2269 ethernet_phy_reset(mp);
2270 mv643xx_set_settings(dev, &ethtool_cmd);
2271}
2272
2273#ifdef MV643XX_COAL
2274
2275/*
2276 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2277 *
2278 * DESCRIPTION:
2279 * This routine sets the RX coalescing interrupt mechanism parameter.
2280 * This parameter is a timeout counter, that counts in 64 t_clk
2281 * chunks ; that when timeout event occurs a maskable interrupt
2282 * occurs.
2283 * The parameter is calculated using the tClk of the MV-643xx chip
2284 * , and the required delay of the interrupt in usec.
2285 *
2286 * INPUT:
2287 * struct mv643xx_private *mp Ethernet port
2288 * unsigned int delay Delay in usec
2289 *
2290 * OUTPUT:
2291 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2292 *
2293 * RETURN:
2294 * The interrupt coalescing value set in the gigE port.
2295 *
2296 */
2297static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2298 unsigned int delay)
2299{
2300 unsigned int port_num = mp->port_num;
2301 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2302
2303 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002304 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002305 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002306 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002307 & 0xffc000ff));
2308
2309 return coal;
2310}
2311#endif
2312
2313/*
2314 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2315 *
2316 * DESCRIPTION:
2317 * This routine sets the TX coalescing interrupt mechanism parameter.
2318 * This parameter is a timeout counter, that counts in 64 t_clk
2319 * chunks ; that when timeout event occurs a maskable interrupt
2320 * occurs.
2321 * The parameter is calculated using the t_cLK frequency of the
2322 * MV-643xx chip and the required delay in the interrupt in uSec
2323 *
2324 * INPUT:
2325 * struct mv643xx_private *mp Ethernet port
2326 * unsigned int delay Delay in uSeconds
2327 *
2328 * OUTPUT:
2329 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2330 *
2331 * RETURN:
2332 * The interrupt coalescing value set in the gigE port.
2333 *
2334 */
2335static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2336 unsigned int delay)
2337{
2338 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2339
2340 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002341 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002342
2343 return coal;
2344}
2345
2346/*
2347 * eth_port_init - Initialize the Ethernet port driver
2348 *
2349 * DESCRIPTION:
2350 * This function prepares the ethernet port to start its activity:
2351 * 1) Completes the ethernet port driver struct initialization toward port
2352 * start routine.
2353 * 2) Resets the device to a quiescent state in case of warm reboot.
2354 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2355 * 4) Clean MAC tables. The reset status of those tables is unknown.
2356 * 5) Set PHY address.
2357 * Note: Call this routine prior to eth_port_start routine and after
2358 * setting user values in the user fields of Ethernet port control
2359 * struct.
2360 *
2361 * INPUT:
2362 * struct mv643xx_private *mp Ethernet port control struct
2363 *
2364 * OUTPUT:
2365 * See description.
2366 *
2367 * RETURN:
2368 * None.
2369 */
2370static void eth_port_init(struct mv643xx_private *mp)
2371{
2372 mp->rx_resource_err = 0;
2373
2374 eth_port_reset(mp);
2375
2376 eth_port_init_mac_tables(mp);
2377}
2378
2379/*
2380 * mv643xx_eth_open
2381 *
2382 * This function is called when openning the network device. The function
2383 * should initialize all the hardware, initialize cyclic Rx/Tx
2384 * descriptors chain and buffers and allocate an IRQ to the network
2385 * device.
2386 *
2387 * Input : a pointer to the network device structure
2388 *
2389 * Output : zero of success , nonzero if fails.
2390 */
2391
2392static int mv643xx_eth_open(struct net_device *dev)
2393{
2394 struct mv643xx_private *mp = netdev_priv(dev);
2395 unsigned int port_num = mp->port_num;
2396 unsigned int size;
2397 int err;
2398
2399 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002400 wrl(mp, INT_CAUSE(port_num), 0);
2401 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002402 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002403 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002404
2405 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2406 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2407 if (err) {
2408 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2409 return -EAGAIN;
2410 }
2411
2412 eth_port_init(mp);
2413
2414 memset(&mp->timeout, 0, sizeof(struct timer_list));
2415 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2416 mp->timeout.data = (unsigned long)dev;
2417
2418 /* Allocate RX and TX skb rings */
2419 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2420 GFP_KERNEL);
2421 if (!mp->rx_skb) {
2422 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2423 err = -ENOMEM;
2424 goto out_free_irq;
2425 }
2426 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2427 GFP_KERNEL);
2428 if (!mp->tx_skb) {
2429 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2430 err = -ENOMEM;
2431 goto out_free_rx_skb;
2432 }
2433
2434 /* Allocate TX ring */
2435 mp->tx_desc_count = 0;
2436 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2437 mp->tx_desc_area_size = size;
2438
2439 if (mp->tx_sram_size) {
2440 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2441 mp->tx_sram_size);
2442 mp->tx_desc_dma = mp->tx_sram_addr;
2443 } else
2444 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2445 &mp->tx_desc_dma,
2446 GFP_KERNEL);
2447
2448 if (!mp->p_tx_desc_area) {
2449 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2450 dev->name, size);
2451 err = -ENOMEM;
2452 goto out_free_tx_skb;
2453 }
2454 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2455 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2456
2457 ether_init_tx_desc_ring(mp);
2458
2459 /* Allocate RX ring */
2460 mp->rx_desc_count = 0;
2461 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2462 mp->rx_desc_area_size = size;
2463
2464 if (mp->rx_sram_size) {
2465 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2466 mp->rx_sram_size);
2467 mp->rx_desc_dma = mp->rx_sram_addr;
2468 } else
2469 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2470 &mp->rx_desc_dma,
2471 GFP_KERNEL);
2472
2473 if (!mp->p_rx_desc_area) {
2474 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2475 dev->name, size);
2476 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2477 dev->name);
2478 if (mp->rx_sram_size)
2479 iounmap(mp->p_tx_desc_area);
2480 else
2481 dma_free_coherent(NULL, mp->tx_desc_area_size,
2482 mp->p_tx_desc_area, mp->tx_desc_dma);
2483 err = -ENOMEM;
2484 goto out_free_tx_skb;
2485 }
2486 memset((void *)mp->p_rx_desc_area, 0, size);
2487
2488 ether_init_rx_desc_ring(mp);
2489
2490 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2491
2492#ifdef MV643XX_NAPI
2493 napi_enable(&mp->napi);
2494#endif
2495
2496 eth_port_start(dev);
2497
2498 /* Interrupt Coalescing */
2499
2500#ifdef MV643XX_COAL
2501 mp->rx_int_coal =
2502 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2503#endif
2504
2505 mp->tx_int_coal =
2506 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2507
2508 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002509 wrl(mp, INT_MASK_EXT(port_num), ETH_INT_UNMASK_ALL_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002510
2511 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002512 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002513
2514 return 0;
2515
2516out_free_tx_skb:
2517 kfree(mp->tx_skb);
2518out_free_rx_skb:
2519 kfree(mp->rx_skb);
2520out_free_irq:
2521 free_irq(dev->irq, dev);
2522
2523 return err;
2524}
2525
2526/*
2527 * eth_port_reset - Reset Ethernet port
2528 *
2529 * DESCRIPTION:
2530 * This routine resets the chip by aborting any SDMA engine activity and
2531 * clearing the MIB counters. The Receiver and the Transmit unit are in
2532 * idle state after this command is performed and the port is disabled.
2533 *
2534 * INPUT:
2535 * struct mv643xx_private *mp Ethernet Port.
2536 *
2537 * OUTPUT:
2538 * Channel activity is halted.
2539 *
2540 * RETURN:
2541 * None.
2542 *
2543 */
2544static void eth_port_reset(struct mv643xx_private *mp)
2545{
2546 unsigned int port_num = mp->port_num;
2547 unsigned int reg_data;
2548
2549 mv643xx_eth_port_disable_tx(mp);
2550 mv643xx_eth_port_disable_rx(mp);
2551
2552 /* Clear all MIB counters */
2553 eth_clear_mib_counters(mp);
2554
2555 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002556 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002557 reg_data &= ~(SERIAL_PORT_ENABLE |
2558 DO_NOT_FORCE_LINK_FAIL |
2559 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002560 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002561}
2562
2563/*
2564 * mv643xx_eth_stop
2565 *
2566 * This function is used when closing the network device.
2567 * It updates the hardware,
2568 * release all memory that holds buffers and descriptors and release the IRQ.
2569 * Input : a pointer to the device structure
2570 * Output : zero if success , nonzero if fails
2571 */
2572
2573static int mv643xx_eth_stop(struct net_device *dev)
2574{
2575 struct mv643xx_private *mp = netdev_priv(dev);
2576 unsigned int port_num = mp->port_num;
2577
2578 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002579 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002580 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002581 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002582
2583#ifdef MV643XX_NAPI
2584 napi_disable(&mp->napi);
2585#endif
2586 netif_carrier_off(dev);
2587 netif_stop_queue(dev);
2588
2589 eth_port_reset(mp);
2590
2591 mv643xx_eth_free_tx_rings(dev);
2592 mv643xx_eth_free_rx_rings(dev);
2593
2594 free_irq(dev->irq, dev);
2595
2596 return 0;
2597}
2598
2599static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2600{
2601 struct mv643xx_private *mp = netdev_priv(dev);
2602
2603 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2604}
2605
2606/*
2607 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2608 *
2609 * Input : pointer to ethernet interface network device structure
2610 * new mtu size
2611 * Output : 0 upon success, -EINVAL upon failure
2612 */
2613static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2614{
2615 if ((new_mtu > 9500) || (new_mtu < 64))
2616 return -EINVAL;
2617
2618 dev->mtu = new_mtu;
2619 if (!netif_running(dev))
2620 return 0;
2621
2622 /*
2623 * Stop and then re-open the interface. This will allocate RX
2624 * skbs of the new MTU.
2625 * There is a possible danger that the open will not succeed,
2626 * due to memory being full, which might fail the open function.
2627 */
2628 mv643xx_eth_stop(dev);
2629 if (mv643xx_eth_open(dev)) {
2630 printk(KERN_ERR "%s: Fatal error on opening device\n",
2631 dev->name);
2632 }
2633
2634 return 0;
2635}
2636
2637/*
2638 * mv643xx_eth_tx_timeout_task
2639 *
2640 * Actual routine to reset the adapter when a timeout on Tx has occurred
2641 */
2642static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2643{
2644 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2645 tx_timeout_task);
2646 struct net_device *dev = mp->dev;
2647
2648 if (!netif_running(dev))
2649 return;
2650
2651 netif_stop_queue(dev);
2652
2653 eth_port_reset(mp);
2654 eth_port_start(dev);
2655
2656 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2657 netif_wake_queue(dev);
2658}
2659
2660/*
2661 * mv643xx_eth_tx_timeout
2662 *
2663 * Called upon a timeout on transmitting a packet
2664 *
2665 * Input : pointer to ethernet interface network device structure.
2666 * Output : N/A
2667 */
2668static void mv643xx_eth_tx_timeout(struct net_device *dev)
2669{
2670 struct mv643xx_private *mp = netdev_priv(dev);
2671
2672 printk(KERN_INFO "%s: TX timeout ", dev->name);
2673
2674 /* Do the reset outside of interrupt context */
2675 schedule_work(&mp->tx_timeout_task);
2676}
2677
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002678#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002679static void mv643xx_netpoll(struct net_device *netdev)
2680{
2681 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002682 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002683
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002684 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002685 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002686 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002687
Al Viro9da3b1a2006-10-08 15:00:44 +01002688 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002689
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002690 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002691}
2692#endif
2693
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002694/*
2695 * Wrappers for MII support library.
2696 */
2697static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2698{
2699 struct mv643xx_private *mp = netdev_priv(dev);
2700 int val;
2701
2702 eth_port_read_smi_reg(mp, location, &val);
2703 return val;
2704}
2705
2706static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2707{
2708 struct mv643xx_private *mp = netdev_priv(dev);
2709 eth_port_write_smi_reg(mp, location, val);
2710}
2711
2712
2713/* platform glue ************************************************************/
2714static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2715 struct mbus_dram_target_info *dram)
2716{
2717 void __iomem *base = msp->eth_base;
2718 u32 win_enable;
2719 u32 win_protect;
2720 int i;
2721
2722 for (i = 0; i < 6; i++) {
2723 writel(0, base + WINDOW_BASE(i));
2724 writel(0, base + WINDOW_SIZE(i));
2725 if (i < 4)
2726 writel(0, base + WINDOW_REMAP_HIGH(i));
2727 }
2728
2729 win_enable = 0x3f;
2730 win_protect = 0;
2731
2732 for (i = 0; i < dram->num_cs; i++) {
2733 struct mbus_dram_window *cs = dram->cs + i;
2734
2735 writel((cs->base & 0xffff0000) |
2736 (cs->mbus_attr << 8) |
2737 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2738 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2739
2740 win_enable &= ~(1 << i);
2741 win_protect |= 3 << (2 * i);
2742 }
2743
2744 writel(win_enable, base + WINDOW_BAR_ENABLE);
2745 msp->win_protect = win_protect;
2746}
2747
2748static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2749{
2750 static int mv643xx_version_printed = 0;
2751 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2752 struct mv643xx_shared_private *msp;
2753 struct resource *res;
2754 int ret;
2755
2756 if (!mv643xx_version_printed++)
2757 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2758
2759 ret = -EINVAL;
2760 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2761 if (res == NULL)
2762 goto out;
2763
2764 ret = -ENOMEM;
2765 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2766 if (msp == NULL)
2767 goto out;
2768 memset(msp, 0, sizeof(*msp));
2769
2770 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2771 if (msp->eth_base == NULL)
2772 goto out_free;
2773
2774 spin_lock_init(&msp->phy_lock);
2775 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2776
2777 platform_set_drvdata(pdev, msp);
2778
2779 /*
2780 * (Re-)program MBUS remapping windows if we are asked to.
2781 */
2782 if (pd != NULL && pd->dram != NULL)
2783 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2784
2785 return 0;
2786
2787out_free:
2788 kfree(msp);
2789out:
2790 return ret;
2791}
2792
2793static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2794{
2795 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2796
2797 iounmap(msp->eth_base);
2798 kfree(msp);
2799
2800 return 0;
2801}
2802
2803static struct platform_driver mv643xx_eth_shared_driver = {
2804 .probe = mv643xx_eth_shared_probe,
2805 .remove = mv643xx_eth_shared_remove,
2806 .driver = {
2807 .name = MV643XX_ETH_SHARED_NAME,
2808 .owner = THIS_MODULE,
2809 },
2810};
2811
2812/*
2813 * ethernet_phy_set - Set the ethernet port PHY address.
2814 *
2815 * DESCRIPTION:
2816 * This routine sets the given ethernet port PHY address.
2817 *
2818 * INPUT:
2819 * struct mv643xx_private *mp Ethernet Port.
2820 * int phy_addr PHY address.
2821 *
2822 * OUTPUT:
2823 * None.
2824 *
2825 * RETURN:
2826 * None.
2827 *
2828 */
2829static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2830{
2831 u32 reg_data;
2832 int addr_shift = 5 * mp->port_num;
2833
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002834 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002835 reg_data &= ~(0x1f << addr_shift);
2836 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002837 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002838}
2839
2840/*
2841 * ethernet_phy_get - Get the ethernet port PHY address.
2842 *
2843 * DESCRIPTION:
2844 * This routine returns the given ethernet port PHY address.
2845 *
2846 * INPUT:
2847 * struct mv643xx_private *mp Ethernet Port.
2848 *
2849 * OUTPUT:
2850 * None.
2851 *
2852 * RETURN:
2853 * PHY address.
2854 *
2855 */
2856static int ethernet_phy_get(struct mv643xx_private *mp)
2857{
2858 unsigned int reg_data;
2859
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002860 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002861
2862 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2863}
2864
2865/*
2866 * ethernet_phy_detect - Detect whether a phy is present
2867 *
2868 * DESCRIPTION:
2869 * This function tests whether there is a PHY present on
2870 * the specified port.
2871 *
2872 * INPUT:
2873 * struct mv643xx_private *mp Ethernet Port.
2874 *
2875 * OUTPUT:
2876 * None
2877 *
2878 * RETURN:
2879 * 0 on success
2880 * -ENODEV on failure
2881 *
2882 */
2883static int ethernet_phy_detect(struct mv643xx_private *mp)
2884{
2885 unsigned int phy_reg_data0;
2886 int auto_neg;
2887
2888 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2889 auto_neg = phy_reg_data0 & 0x1000;
2890 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2891 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2892
2893 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2894 if ((phy_reg_data0 & 0x1000) == auto_neg)
2895 return -ENODEV; /* change didn't take */
2896
2897 phy_reg_data0 ^= 0x1000;
2898 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2899 return 0;
2900}
2901
James Chapmand0412d92006-01-27 01:15:30 -07002902static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2903 int speed, int duplex,
2904 struct ethtool_cmd *cmd)
2905{
2906 struct mv643xx_private *mp = netdev_priv(dev);
2907
2908 memset(cmd, 0, sizeof(*cmd));
2909
2910 cmd->port = PORT_MII;
2911 cmd->transceiver = XCVR_INTERNAL;
2912 cmd->phy_address = phy_address;
2913
2914 if (speed == 0) {
2915 cmd->autoneg = AUTONEG_ENABLE;
2916 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2917 cmd->speed = SPEED_100;
2918 cmd->advertising = ADVERTISED_10baseT_Half |
2919 ADVERTISED_10baseT_Full |
2920 ADVERTISED_100baseT_Half |
2921 ADVERTISED_100baseT_Full;
2922 if (mp->mii.supports_gmii)
2923 cmd->advertising |= ADVERTISED_1000baseT_Full;
2924 } else {
2925 cmd->autoneg = AUTONEG_DISABLE;
2926 cmd->speed = speed;
2927 cmd->duplex = duplex;
2928 }
2929}
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/*/
2932 * mv643xx_eth_probe
2933 *
2934 * First function called after registering the network device.
2935 * It's purpose is to initialize the device as an ethernet device,
2936 * fill the ethernet device structure with pointers * to functions,
2937 * and set the MAC address of the interface
2938 *
2939 * Input : struct device *
2940 * Output : -ENOMEM if failed , 0 if success
2941 */
Russell King3ae5eae2005-11-09 22:32:44 +00002942static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002945 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 struct mv643xx_private *mp;
2947 struct net_device *dev;
2948 u8 *p;
2949 struct resource *res;
2950 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002951 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002952 int duplex = DUPLEX_HALF;
2953 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002954 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002956 pd = pdev->dev.platform_data;
2957 if (pd == NULL) {
2958 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2959 return -ENODEV;
2960 }
2961
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002962 if (pd->shared == NULL) {
2963 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2964 return -ENODEV;
2965 }
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2968 if (!dev)
2969 return -ENOMEM;
2970
Russell King3ae5eae2005-11-09 22:32:44 +00002971 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002974 mp->dev = dev;
2975#ifdef MV643XX_NAPI
2976 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2980 BUG_ON(!res);
2981 dev->irq = res->start;
2982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 dev->open = mv643xx_eth_open;
2984 dev->stop = mv643xx_eth_stop;
2985 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 dev->set_mac_address = mv643xx_eth_set_mac_address;
2987 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2988
2989 /* No need to Tx Timeout */
2990 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002992#ifdef CONFIG_NET_POLL_CONTROLLER
2993 dev->poll_controller = mv643xx_netpoll;
2994#endif
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 dev->base_addr = 0;
2998 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002999 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
3001
3002#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
3003#ifdef MAX_SKB_FRAGS
3004 /*
3005 * Zero copy can only work if we use Discovery II memory. Else, we will
3006 * have to map the buffers to ISA memory which is only 16 MB
3007 */
Wolfram Joost63890572006-01-16 16:57:41 -07003008 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009#endif
3010#endif
3011
3012 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00003013 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 spin_lock_init(&mp->lock);
3016
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02003017 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07003018 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003019
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02003020 if (mp->shared->win_protect)
3021 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
3022
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02003023 mp->shared_smi = mp->shared;
3024 if (pd->shared_smi != NULL)
3025 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003028 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02003029 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
3030 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003032 if (is_valid_ether_addr(pd->mac_addr))
3033 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003035 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003036 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003038 if (pd->rx_queue_size)
3039 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003041 if (pd->tx_queue_size)
3042 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003044 if (pd->tx_sram_size) {
3045 mp->tx_sram_size = pd->tx_sram_size;
3046 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003049 if (pd->rx_sram_size) {
3050 mp->rx_sram_size = pd->rx_sram_size;
3051 mp->rx_sram_addr = pd->rx_sram_addr;
3052 }
3053
3054 duplex = pd->duplex;
3055 speed = pd->speed;
3056
James Chapmanc28a4f82006-01-27 01:13:15 -07003057 /* Hook up MII support for ethtool */
3058 mp->mii.dev = dev;
3059 mp->mii.mdio_read = mv643xx_mdio_read;
3060 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003061 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003062 mp->mii.phy_id_mask = 0x3f;
3063 mp->mii.reg_num_mask = 0x1f;
3064
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003065 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07003067 pr_debug("%s: No PHY detected at addr %d\n",
3068 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07003069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003072 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003073 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07003074 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
3075 mv643xx_eth_update_pscr(dev, &cmd);
3076 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07003077
Olaf Heringb0b8dab2006-04-27 18:23:49 -07003078 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 err = register_netdev(dev);
3080 if (err)
3081 goto out;
3082
3083 p = dev->dev_addr;
3084 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07003085 "%s: port %d with MAC address %s\n",
3086 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 if (dev->features & NETIF_F_SG)
3089 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
3090
3091 if (dev->features & NETIF_F_IP_CSUM)
3092 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
3093 dev->name);
3094
3095#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
3096 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
3097#endif
3098
3099#ifdef MV643XX_COAL
3100 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
3101 dev->name);
3102#endif
3103
3104#ifdef MV643XX_NAPI
3105 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
3106#endif
3107
Nicolas DETb1529872005-10-28 17:46:30 -07003108 if (mp->tx_sram_size > 0)
3109 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return 0;
3112
3113out:
3114 free_netdev(dev);
3115
3116 return err;
3117}
3118
Russell King3ae5eae2005-11-09 22:32:44 +00003119static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Russell King3ae5eae2005-11-09 22:32:44 +00003121 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 unregister_netdev(dev);
3124 flush_scheduled_work();
3125
3126 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00003127 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return 0;
3129}
3130
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003131static void mv643xx_eth_shutdown(struct platform_device *pdev)
3132{
3133 struct net_device *dev = platform_get_drvdata(pdev);
3134 struct mv643xx_private *mp = netdev_priv(dev);
3135 unsigned int port_num = mp->port_num;
3136
3137 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02003138 wrl(mp, INT_MASK(port_num), 0);
3139 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003140
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003141 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003142}
3143
Russell King3ae5eae2005-11-09 22:32:44 +00003144static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 .probe = mv643xx_eth_probe,
3146 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003147 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003148 .driver = {
3149 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003150 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003151 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152};
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154/*
3155 * mv643xx_init_module
3156 *
3157 * Registers the network drivers into the Linux kernel
3158 *
3159 * Input : N/A
3160 *
3161 * Output : N/A
3162 */
3163static int __init mv643xx_init_module(void)
3164{
3165 int rc;
3166
Russell King3ae5eae2005-11-09 22:32:44 +00003167 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003169 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003171 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173 return rc;
3174}
3175
3176/*
3177 * mv643xx_cleanup_module
3178 *
3179 * Registers the network drivers into the Linux kernel
3180 *
3181 * Input : N/A
3182 *
3183 * Output : N/A
3184 */
3185static void __exit mv643xx_cleanup_module(void)
3186{
Russell King3ae5eae2005-11-09 22:32:44 +00003187 platform_driver_unregister(&mv643xx_eth_driver);
3188 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
3191module_init(mv643xx_init_module);
3192module_exit(mv643xx_cleanup_module);
3193
3194MODULE_LICENSE("GPL");
3195MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3196 " and Dale Farnsworth");
3197MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003198MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3199MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);