1 From c55d6cf3e2c593bf7d228c6532ec9bd8da82e09d Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 22 Apr 2013 23:20:03 +0200
4 Subject: [PATCH 35/57] NET: MIPS: add ralink SoC ethernet driver
6 Add support for Ralink FE and ESW.
8 Signed-off-by: John Crispin <blogic@openwrt.org>
10 .../include/asm/mach-ralink/rt305x_esw_platform.h | 27 +
11 arch/mips/ralink/rt305x.c | 1 +
12 drivers/net/ethernet/Kconfig | 1 +
13 drivers/net/ethernet/Makefile | 1 +
14 drivers/net/ethernet/ralink/Kconfig | 32 +
15 drivers/net/ethernet/ralink/Makefile | 18 +
16 drivers/net/ethernet/ralink/esw_rt3052.c | 1463 ++++++++++++++++++++
17 drivers/net/ethernet/ralink/esw_rt3052.h | 32 +
18 drivers/net/ethernet/ralink/gsw_mt7620a.c | 566 ++++++++
19 drivers/net/ethernet/ralink/gsw_mt7620a.h | 30 +
20 drivers/net/ethernet/ralink/mdio.c | 244 ++++
21 drivers/net/ethernet/ralink/mdio.h | 29 +
22 drivers/net/ethernet/ralink/mdio_rt2880.c | 232 ++++
23 drivers/net/ethernet/ralink/mdio_rt2880.h | 26 +
24 drivers/net/ethernet/ralink/mt7530.c | 579 ++++++++
25 drivers/net/ethernet/ralink/mt7530.h | 20 +
26 drivers/net/ethernet/ralink/ralink_soc_eth.c | 844 +++++++++++
27 drivers/net/ethernet/ralink/ralink_soc_eth.h | 384 +++++
28 drivers/net/ethernet/ralink/soc_mt7620.c | 172 +++
29 drivers/net/ethernet/ralink/soc_rt2880.c | 52 +
30 drivers/net/ethernet/ralink/soc_rt305x.c | 113 ++
31 drivers/net/ethernet/ralink/soc_rt3883.c | 60 +
32 22 files changed, 4926 insertions(+)
33 create mode 100644 arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
34 create mode 100644 drivers/net/ethernet/ralink/Kconfig
35 create mode 100644 drivers/net/ethernet/ralink/Makefile
36 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.c
37 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.h
38 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.c
39 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.h
40 create mode 100644 drivers/net/ethernet/ralink/mdio.c
41 create mode 100644 drivers/net/ethernet/ralink/mdio.h
42 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.c
43 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.h
44 create mode 100644 drivers/net/ethernet/ralink/mt7530.c
45 create mode 100644 drivers/net/ethernet/ralink/mt7530.h
46 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.c
47 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.h
48 create mode 100644 drivers/net/ethernet/ralink/soc_mt7620.c
49 create mode 100644 drivers/net/ethernet/ralink/soc_rt2880.c
50 create mode 100644 drivers/net/ethernet/ralink/soc_rt305x.c
51 create mode 100644 drivers/net/ethernet/ralink/soc_rt3883.c
53 Index: linux-3.14.18/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
54 ===================================================================
55 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
56 +++ linux-3.14.18/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h 2014-10-29 20:25:55.429357372 +0100
59 + * Ralink RT305x SoC platform device registration
61 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
63 + * This program is free software; you can redistribute it and/or modify it
64 + * under the terms of the GNU General Public License version 2 as published
65 + * by the Free Software Foundation.
68 +#ifndef _RT305X_ESW_PLATFORM_H
69 +#define _RT305X_ESW_PLATFORM_H
72 + RT305X_ESW_VLAN_CONFIG_NONE = 0,
73 + RT305X_ESW_VLAN_CONFIG_LLLLW,
74 + RT305X_ESW_VLAN_CONFIG_WLLLL,
77 +struct rt305x_esw_platform_data
80 + u32 reg_initval_fct2;
81 + u32 reg_initval_fpa2;
84 +#endif /* _RT305X_ESW_PLATFORM_H */
85 Index: linux-3.14.18/arch/mips/ralink/rt305x.c
86 ===================================================================
87 --- linux-3.14.18.orig/arch/mips/ralink/rt305x.c 2014-10-29 20:25:55.377355456 +0100
88 +++ linux-3.14.18/arch/mips/ralink/rt305x.c 2014-10-29 20:25:55.433357519 +0100
92 ralink_clk_add("cpu", cpu_rate);
93 + ralink_clk_add("sys", sys_rate);
94 ralink_clk_add("10000b00.spi", sys_rate);
95 ralink_clk_add("10000100.timer", wdt_rate);
96 ralink_clk_add("10000120.watchdog", wdt_rate);
97 Index: linux-3.14.18/drivers/net/ethernet/Kconfig
98 ===================================================================
99 --- linux-3.14.18.orig/drivers/net/ethernet/Kconfig 2014-09-06 01:34:59.000000000 +0200
100 +++ linux-3.14.18/drivers/net/ethernet/Kconfig 2014-10-30 11:09:42.442557047 +0100
102 source "drivers/net/ethernet/packetengines/Kconfig"
103 source "drivers/net/ethernet/pasemi/Kconfig"
104 source "drivers/net/ethernet/qlogic/Kconfig"
105 +source "drivers/net/ethernet/ralink/Kconfig"
106 source "drivers/net/ethernet/realtek/Kconfig"
107 source "drivers/net/ethernet/renesas/Kconfig"
108 source "drivers/net/ethernet/rdc/Kconfig"
109 Index: linux-3.14.18/drivers/net/ethernet/Makefile
110 ===================================================================
111 --- linux-3.14.18.orig/drivers/net/ethernet/Makefile 2014-09-06 01:34:59.000000000 +0200
112 +++ linux-3.14.18/drivers/net/ethernet/Makefile 2014-10-30 11:09:42.442557047 +0100
114 obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
115 obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
116 obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
117 +obj-$(CONFIG_NET_RALINK) += ralink/
118 obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
119 obj-$(CONFIG_SH_ETH) += renesas/
120 obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
121 Index: linux-3.14.18/drivers/net/ethernet/ralink/Kconfig
122 ===================================================================
123 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
124 +++ linux-3.14.18/drivers/net/ethernet/ralink/Kconfig 2014-10-29 20:25:55.433357519 +0100
127 + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
130 + This driver supports the ethernet mac inside the ralink wisocs
134 +config NET_RALINK_MDIO
135 + def_bool NET_RALINK
136 + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
139 +config NET_RALINK_MDIO_RT2880
140 + def_bool NET_RALINK
141 + depends on (SOC_RT288X || SOC_RT3883)
142 + select NET_RALINK_MDIO
144 +config NET_RALINK_ESW_RT3052
145 + def_bool NET_RALINK
146 + depends on SOC_RT305X
150 +config NET_RALINK_GSW_MT7620
151 + def_bool NET_RALINK
152 + depends on SOC_MT7620
153 + select NET_RALINK_MDIO
157 Index: linux-3.14.18/drivers/net/ethernet/ralink/Makefile
158 ===================================================================
159 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
160 +++ linux-3.14.18/drivers/net/ethernet/ralink/Makefile 2014-10-29 20:25:55.433357519 +0100
163 +# Makefile for the Ralink SoCs built-in ethernet macs
166 +ralink-eth-y += ralink_soc_eth.o ralink_ethtool.o
168 +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
169 +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
171 +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
172 +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o mt7530.o
174 +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
175 +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
176 +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
177 +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
179 +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
180 Index: linux-3.14.18/drivers/net/ethernet/ralink/esw_rt3052.c
181 ===================================================================
182 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
183 +++ linux-3.14.18/drivers/net/ethernet/ralink/esw_rt3052.c 2014-10-29 20:25:55.433357519 +0100
186 + * This program is free software; you can redistribute it and/or modify
187 + * it under the terms of the GNU General Public License as published by
188 + * the Free Software Foundation; version 2 of the License
190 + * This program is distributed in the hope that it will be useful,
191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193 + * GNU General Public License for more details.
195 + * You should have received a copy of the GNU General Public License
196 + * along with this program; if not, write to the Free Software
197 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
199 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
202 +#include <linux/module.h>
203 +#include <linux/kernel.h>
204 +#include <linux/types.h>
205 +#include <linux/dma-mapping.h>
206 +#include <linux/init.h>
207 +#include <linux/skbuff.h>
208 +#include <linux/etherdevice.h>
209 +#include <linux/ethtool.h>
210 +#include <linux/platform_device.h>
211 +#include <linux/of_device.h>
212 +#include <linux/clk.h>
213 +#include <linux/of_net.h>
214 +#include <linux/of_mdio.h>
216 +#include <asm/mach-ralink/ralink_regs.h>
218 +#include "ralink_soc_eth.h"
220 +#include <linux/ioport.h>
221 +#include <linux/switch.h>
222 +#include <linux/mii.h>
224 +#include <ralink_regs.h>
225 +#include <asm/mach-ralink/rt305x.h>
226 +#include <asm/mach-ralink/rt305x_esw_platform.h>
229 + * HW limitations for this switch:
230 + * - No large frame support (PKT_MAX_LEN at most 1536)
231 + * - Can't have untagged vlan and tagged vlan on one port at the same time,
232 + * though this might be possible using the undocumented PPE.
235 +#define RT305X_ESW_REG_ISR 0x00
236 +#define RT305X_ESW_REG_IMR 0x04
237 +#define RT305X_ESW_REG_FCT0 0x08
238 +#define RT305X_ESW_REG_PFC1 0x14
239 +#define RT305X_ESW_REG_ATS 0x24
240 +#define RT305X_ESW_REG_ATS0 0x28
241 +#define RT305X_ESW_REG_ATS1 0x2c
242 +#define RT305X_ESW_REG_ATS2 0x30
243 +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
244 +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
245 +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
246 +#define RT305X_ESW_REG_POA 0x80
247 +#define RT305X_ESW_REG_FPA 0x84
248 +#define RT305X_ESW_REG_SOCPC 0x8c
249 +#define RT305X_ESW_REG_POC0 0x90
250 +#define RT305X_ESW_REG_POC1 0x94
251 +#define RT305X_ESW_REG_POC2 0x98
252 +#define RT305X_ESW_REG_SGC 0x9c
253 +#define RT305X_ESW_REG_STRT 0xa0
254 +#define RT305X_ESW_REG_PCR0 0xc0
255 +#define RT305X_ESW_REG_PCR1 0xc4
256 +#define RT305X_ESW_REG_FPA2 0xc8
257 +#define RT305X_ESW_REG_FCT2 0xcc
258 +#define RT305X_ESW_REG_SGC2 0xe4
259 +#define RT305X_ESW_REG_P0LED 0xa4
260 +#define RT305X_ESW_REG_P1LED 0xa8
261 +#define RT305X_ESW_REG_P2LED 0xac
262 +#define RT305X_ESW_REG_P3LED 0xb0
263 +#define RT305X_ESW_REG_P4LED 0xb4
264 +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
265 +#define RT305X_ESW_REG_P1PC 0xec
266 +#define RT305X_ESW_REG_P2PC 0xf0
267 +#define RT305X_ESW_REG_P3PC 0xf4
268 +#define RT305X_ESW_REG_P4PC 0xf8
269 +#define RT305X_ESW_REG_P5PC 0xfc
271 +#define RT305X_ESW_LED_LINK 0
272 +#define RT305X_ESW_LED_100M 1
273 +#define RT305X_ESW_LED_DUPLEX 2
274 +#define RT305X_ESW_LED_ACTIVITY 3
275 +#define RT305X_ESW_LED_COLLISION 4
276 +#define RT305X_ESW_LED_LINKACT 5
277 +#define RT305X_ESW_LED_DUPLCOLL 6
278 +#define RT305X_ESW_LED_10MACT 7
279 +#define RT305X_ESW_LED_100MACT 8
280 +/* Additional led states not in datasheet: */
281 +#define RT305X_ESW_LED_BLINK 10
282 +#define RT305X_ESW_LED_ON 12
284 +#define RT305X_ESW_LINK_S 25
285 +#define RT305X_ESW_DUPLEX_S 9
286 +#define RT305X_ESW_SPD_S 0
288 +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
289 +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
290 +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
292 +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
294 +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
295 +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
297 +#define RT305X_ESW_PVIDC_PVID_M 0xfff
298 +#define RT305X_ESW_PVIDC_PVID_S 12
300 +#define RT305X_ESW_VLANI_VID_M 0xfff
301 +#define RT305X_ESW_VLANI_VID_S 12
303 +#define RT305X_ESW_VMSC_MSC_M 0xff
304 +#define RT305X_ESW_VMSC_MSC_S 8
306 +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
307 +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
308 +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
309 +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
311 +#define RT305X_ESW_POC0_EN_BP_S 0
312 +#define RT305X_ESW_POC0_EN_FC_S 8
313 +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
314 +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
315 +#define RT305X_ESW_POC0_DIS_PORT_S 23
317 +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
318 +#define RT305X_ESW_POC2_UNTAG_EN_S 0
319 +#define RT305X_ESW_POC2_ENAGING_S 8
320 +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
322 +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
323 +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
324 +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
325 +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
327 +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
328 +#define RT305X_ESW_PFC1_EN_VLAN_S 16
329 +#define RT305X_ESW_PFC1_EN_TOS_S 24
331 +#define RT305X_ESW_VLAN_NONE 0xfff
333 +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
334 +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
336 +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
337 +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
339 +#define RT305X_ESW_POA_LINK_MASK 0x1f
340 +#define RT305X_ESW_POA_LINK_SHIFT 25
342 +#define RT305X_ESW_PORT_ST_CHG BIT(26)
343 +#define RT305X_ESW_PORT0 0
344 +#define RT305X_ESW_PORT1 1
345 +#define RT305X_ESW_PORT2 2
346 +#define RT305X_ESW_PORT3 3
347 +#define RT305X_ESW_PORT4 4
348 +#define RT305X_ESW_PORT5 5
349 +#define RT305X_ESW_PORT6 6
351 +#define RT305X_ESW_PORTS_NONE 0
353 +#define RT305X_ESW_PMAP_LLLLLL 0x3f
354 +#define RT305X_ESW_PMAP_LLLLWL 0x2f
355 +#define RT305X_ESW_PMAP_WLLLLL 0x3e
357 +#define RT305X_ESW_PORTS_INTERNAL \
358 + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
359 + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
360 + BIT(RT305X_ESW_PORT4))
362 +#define RT305X_ESW_PORTS_NOCPU \
363 + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
365 +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
367 +#define RT305X_ESW_PORTS_ALL \
368 + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
370 +#define RT305X_ESW_NUM_VLANS 16
371 +#define RT305X_ESW_NUM_VIDS 4096
372 +#define RT305X_ESW_NUM_PORTS 7
373 +#define RT305X_ESW_NUM_LANWAN 6
374 +#define RT305X_ESW_NUM_LEDS 5
376 +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
377 +#define RT5350_EWS_REG_LED_POLARITY 0x168
378 +#define RT5350_RESET_EPHY BIT(24)
379 +#define SYSC_REG_RESET_CTRL 0x34
382 + /* Global attributes. */
383 + RT305X_ESW_ATTR_ENABLE_VLAN,
384 + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
385 + RT305X_ESW_ATTR_BC_STATUS,
386 + RT305X_ESW_ATTR_LED_FREQ,
387 + /* Port attributes. */
388 + RT305X_ESW_ATTR_PORT_DISABLE,
389 + RT305X_ESW_ATTR_PORT_DOUBLETAG,
390 + RT305X_ESW_ATTR_PORT_UNTAG,
391 + RT305X_ESW_ATTR_PORT_LED,
392 + RT305X_ESW_ATTR_PORT_LAN,
393 + RT305X_ESW_ATTR_PORT_RECV_BAD,
394 + RT305X_ESW_ATTR_PORT_RECV_GOOD,
395 + RT5350_ESW_ATTR_PORT_TR_BAD,
396 + RT5350_ESW_ATTR_PORT_TR_GOOD,
413 + struct device *dev;
414 + void __iomem *base;
416 + const struct rt305x_esw_platform_data *pdata;
417 + /* Protects against concurrent register rmw operations. */
418 + spinlock_t reg_rw_lock;
420 + unsigned char port_map;
421 + unsigned int reg_initval_fct2;
422 + unsigned int reg_initval_fpa2;
423 + unsigned int reg_led_polarity;
426 + struct switch_dev swdev;
427 + bool global_vlan_enable;
428 + bool alt_vlan_disable;
429 + int bc_storm_protect;
431 + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
432 + struct esw_port ports[RT305X_ESW_NUM_PORTS];
436 +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
438 + __raw_writel(val, esw->base + reg);
441 +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
443 + return __raw_readl(esw->base + reg);
446 +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
451 + t = __raw_readl(esw->base + reg) & ~mask;
452 + __raw_writel(t | val, esw->base + reg);
455 +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
458 + unsigned long flags;
460 + spin_lock_irqsave(&esw->reg_rw_lock, flags);
461 + esw_rmw_raw(esw, reg, mask, val);
462 + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
465 +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
468 + unsigned long t_start = jiffies;
472 + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
473 + RT305X_ESW_PCR1_WT_DONE))
475 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
481 + write_data &= 0xffff;
483 + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
484 + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
485 + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
486 + RT305X_ESW_REG_PCR0);
490 + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
491 + RT305X_ESW_PCR1_WT_DONE)
494 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
501 + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
505 +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
510 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
511 + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
512 + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
517 +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
521 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
523 + RT305X_ESW_REG_VLANI(vlan / 2),
524 + RT305X_ESW_VLANI_VID_M << s,
525 + (vid & RT305X_ESW_VLANI_VID_M) << s);
528 +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
532 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
533 + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
534 + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
537 +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
541 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
543 + RT305X_ESW_REG_PVIDC(port / 2),
544 + RT305X_ESW_PVIDC_PVID_M << s,
545 + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
548 +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
552 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
553 + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
554 + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
559 +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
563 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
565 + RT305X_ESW_REG_VMSC(vlan / 4),
566 + RT305X_ESW_VMSC_MSC_M << s,
567 + (msc & RT305X_ESW_VMSC_MSC_M) << s);
570 +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
573 + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
574 + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
575 + RT305X_ESW_POC0_DIS_PORT_M;
578 +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
581 + unsigned enable_mask;
585 + old_mask = esw_get_port_disable(esw);
586 + changed = old_mask ^ disable_mask;
587 + enable_mask = old_mask & disable_mask;
589 + /* enable before writing to MII */
590 + esw_rmw(esw, RT305X_ESW_REG_POC0,
591 + (RT305X_ESW_POC0_DIS_PORT_M <<
592 + RT305X_ESW_POC0_DIS_PORT_S),
593 + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
595 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
596 + if (!(changed & (1 << i)))
598 + if (disable_mask & (1 << i)) {
600 + rt305x_mii_write(esw, i, MII_BMCR,
604 + rt305x_mii_write(esw, i, MII_BMCR,
612 + /* disable after writing to MII */
613 + esw_rmw(esw, RT305X_ESW_REG_POC0,
614 + (RT305X_ESW_POC0_DIS_PORT_M <<
615 + RT305X_ESW_POC0_DIS_PORT_S),
616 + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
619 +static void esw_set_gsc(struct rt305x_esw *esw)
621 + esw_rmw(esw, RT305X_ESW_REG_SGC,
622 + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
623 + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
624 + esw_rmw(esw, RT305X_ESW_REG_SGC,
625 + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
626 + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
629 +static int esw_apply_config(struct switch_dev *dev);
631 +static void esw_hw_init(struct rt305x_esw *esw)
634 + u8 port_disable = 0;
635 + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
637 + /* vodoo from original driver */
638 + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
639 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
640 + /* Port priority 1 for all ports, vlan enabled. */
641 + esw_w32(esw, 0x00005555 |
642 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
643 + RT305X_ESW_REG_PFC1);
645 + /* Enable Back Pressure, and Flow Control */
647 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
648 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
649 + RT305X_ESW_REG_POC0);
651 + /* Enable Aging, and VLAN TAG removal */
653 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
654 + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
655 + RT305X_ESW_REG_POC2);
657 + if (esw->reg_initval_fct2)
658 + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
660 + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
663 + * 300s aging timer, max packet len 1536, broadcast storm prevention
664 + * disabled, disable collision abort, mac xor48 hash, 10 packet back
665 + * pressure jam, GMII disable was_transmit, back pressure disabled,
666 + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
669 + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
671 + /* Setup SoC Port control register */
673 + (RT305X_ESW_SOCPC_CRC_PADDING |
674 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
675 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
676 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
677 + RT305X_ESW_REG_SOCPC);
679 + if (esw->reg_initval_fpa2)
680 + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
682 + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
683 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
685 + /* Force Link/Activity on ports */
686 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
687 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
688 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
689 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
690 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
692 + /* Copy disabled port configuration from bootloader setup */
693 + port_disable = esw_get_port_disable(esw);
694 + for (i = 0; i < 6; i++)
695 + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
697 + if (soc_is_rt3352()) {
699 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
700 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
701 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
703 + rt305x_mii_write(esw, 0, 31, 0x8000);
704 + for (i = 0; i < 5; i++) {
705 + if (esw->ports[i].disable) {
706 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
708 + rt305x_mii_write(esw, i, MII_BMCR,
713 + /* TX10 waveform coefficient LSB=0 disable PHY */
714 + rt305x_mii_write(esw, i, 26, 0x1601);
715 + /* TX100/TX10 AD/DA current bias */
716 + rt305x_mii_write(esw, i, 29, 0x7016);
717 + /* TX100 slew rate control */
718 + rt305x_mii_write(esw, i, 30, 0x0038);
721 + /* select global register */
722 + rt305x_mii_write(esw, 0, 31, 0x0);
723 + /* enlarge agcsel threshold 3 and threshold 2 */
724 + rt305x_mii_write(esw, 0, 1, 0x4a40);
725 + /* enlarge agcsel threshold 5 and threshold 4 */
726 + rt305x_mii_write(esw, 0, 2, 0x6254);
727 + /* enlarge agcsel threshold */
728 + rt305x_mii_write(esw, 0, 3, 0xa17f);
729 + rt305x_mii_write(esw, 0,12, 0x7eaa);
730 + /* longer TP_IDL tail length */
731 + rt305x_mii_write(esw, 0, 14, 0x65);
732 + /* increased squelch pulse count threshold. */
733 + rt305x_mii_write(esw, 0, 16, 0x0684);
734 + /* set TX10 signal amplitude threshold to minimum */
735 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
736 + /* set squelch amplitude to higher threshold */
737 + rt305x_mii_write(esw, 0, 18, 0x40ba);
738 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
739 + rt305x_mii_write(esw, 0, 22, 0x253f);
740 + /* set PLL/Receive bias current are calibrated */
741 + rt305x_mii_write(esw, 0, 27, 0x2fda);
742 + /* change PLL/Receive bias current to internal(RT3350) */
743 + rt305x_mii_write(esw, 0, 28, 0xc410);
744 + /* change PLL bias current to internal(RT3052_MP3) */
745 + rt305x_mii_write(esw, 0, 29, 0x598b);
746 + /* select local register */
747 + rt305x_mii_write(esw, 0, 31, 0x8000);
748 + } else if (soc_is_rt5350()) {
750 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
751 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
752 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
754 + /* set the led polarity */
755 + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
757 + /* local registers */
758 + rt305x_mii_write(esw, 0, 31, 0x8000);
759 + for (i = 0; i < 5; i++) {
760 + if (esw->ports[i].disable) {
761 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
763 + rt305x_mii_write(esw, i, MII_BMCR,
768 + /* TX10 waveform coefficient LSB=0 disable PHY */
769 + rt305x_mii_write(esw, i, 26, 0x1601);
770 + /* TX100/TX10 AD/DA current bias */
771 + rt305x_mii_write(esw, i, 29, 0x7015);
772 + /* TX100 slew rate control */
773 + rt305x_mii_write(esw, i, 30, 0x0038);
776 + /* global registers */
777 + rt305x_mii_write(esw, 0, 31, 0x0);
778 + /* enlarge agcsel threshold 3 and threshold 2 */
779 + rt305x_mii_write(esw, 0, 1, 0x4a40);
780 + /* enlarge agcsel threshold 5 and threshold 4 */
781 + rt305x_mii_write(esw, 0, 2, 0x6254);
782 + /* enlarge agcsel threshold 6 */
783 + rt305x_mii_write(esw, 0, 3, 0xa17f);
784 + rt305x_mii_write(esw, 0, 12, 0x7eaa);
785 + /* longer TP_IDL tail length */
786 + rt305x_mii_write(esw, 0, 14, 0x65);
787 + /* increased squelch pulse count threshold. */
788 + rt305x_mii_write(esw, 0, 16, 0x0684);
789 + /* set TX10 signal amplitude threshold to minimum */
790 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
791 + /* set squelch amplitude to higher threshold */
792 + rt305x_mii_write(esw, 0, 18, 0x40ba);
793 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
794 + rt305x_mii_write(esw, 0, 22, 0x253f);
795 + /* set PLL/Receive bias current are calibrated */
796 + rt305x_mii_write(esw, 0, 27, 0x2fda);
797 + /* change PLL/Receive bias current to internal(RT3350) */
798 + rt305x_mii_write(esw, 0, 28, 0xc410);
799 + /* change PLL bias current to internal(RT3052_MP3) */
800 + rt305x_mii_write(esw, 0, 29, 0x598b);
801 + /* select local register */
802 + rt305x_mii_write(esw, 0, 31, 0x8000);
804 + rt305x_mii_write(esw, 0, 31, 0x8000);
805 + for (i = 0; i < 5; i++) {
806 + if (esw->ports[i].disable) {
807 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
809 + rt305x_mii_write(esw, i, MII_BMCR,
814 + /* TX10 waveform coefficient */
815 + rt305x_mii_write(esw, i, 26, 0x1601);
816 + /* TX100/TX10 AD/DA current bias */
817 + rt305x_mii_write(esw, i, 29, 0x7058);
818 + /* TX100 slew rate control */
819 + rt305x_mii_write(esw, i, 30, 0x0018);
823 + /* select global register */
824 + rt305x_mii_write(esw, 0, 31, 0x0);
825 + /* tune TP_IDL tail and head waveform */
826 + rt305x_mii_write(esw, 0, 22, 0x052f);
827 + /* set TX10 signal amplitude threshold to minimum */
828 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
829 + /* set squelch amplitude to higher threshold */
830 + rt305x_mii_write(esw, 0, 18, 0x40ba);
831 + /* longer TP_IDL tail length */
832 + rt305x_mii_write(esw, 0, 14, 0x65);
833 + /* select local register */
834 + rt305x_mii_write(esw, 0, 31, 0x8000);
838 + port_map = esw->port_map;
840 + port_map = RT305X_ESW_PMAP_LLLLLL;
843 + * Unused HW feature, but still nice to be consistent here...
844 + * This is also exported to userspace ('lan' attribute) so it's
845 + * conveniently usable to decide which ports go into the wan vlan by
848 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
849 + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
850 + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
852 + /* make the switch leds blink */
853 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
854 + esw->ports[i].led = 0x05;
856 + /* Apply the empty config. */
857 + esw_apply_config(&esw->swdev);
859 + /* Only unmask the port change interrupt */
860 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
863 +static irqreturn_t esw_interrupt(int irq, void *_esw)
865 + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
868 + status = esw_r32(esw, RT305X_ESW_REG_ISR);
869 + if (status & RT305X_ESW_PORT_ST_CHG) {
870 + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
871 + link >>= RT305X_ESW_POA_LINK_SHIFT;
872 + link &= RT305X_ESW_POA_LINK_MASK;
873 + dev_info(esw->dev, "link changed 0x%02X\n", link);
875 + esw_w32(esw, status, RT305X_ESW_REG_ISR);
877 + return IRQ_HANDLED;
880 +static int esw_apply_config(struct switch_dev *dev)
882 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
889 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
891 + if (esw->global_vlan_enable) {
892 + vid = esw->vlans[i].vid;
893 + vmsc = esw->vlans[i].ports;
895 + vid = RT305X_ESW_VLAN_NONE;
896 + vmsc = RT305X_ESW_PORTS_NONE;
898 + esw_set_vlan_id(esw, i, vid);
899 + esw_set_vmsc(esw, i, vmsc);
902 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
904 + disable |= esw->ports[i].disable << i;
905 + if (esw->global_vlan_enable) {
906 + doubletag |= esw->ports[i].doubletag << i;
908 + untag |= esw->ports[i].untag << i;
909 + pvid = esw->ports[i].pvid;
911 + int x = esw->alt_vlan_disable ? 0 : 1;
912 + doubletag |= x << i;
917 + esw_set_pvid(esw, i, pvid);
918 + if (i < RT305X_ESW_NUM_LEDS)
919 + esw_w32(esw, esw->ports[i].led,
920 + RT305X_ESW_REG_P0LED + 4*i);
924 + esw_set_port_disable(esw, disable);
925 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
926 + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
927 + RT305X_ESW_SGC2_DOUBLE_TAG_S),
928 + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
929 + esw_rmw(esw, RT305X_ESW_REG_PFC1,
930 + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
931 + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
932 + esw_rmw(esw, RT305X_ESW_REG_POC2,
933 + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
934 + untag << RT305X_ESW_POC2_UNTAG_EN_S);
936 + if (!esw->global_vlan_enable) {
938 + * Still need to put all ports into vlan 0 or they'll be
940 + * NOTE: vlan 0 is special, no vlan tag is prepended
942 + esw_set_vlan_id(esw, 0, 0);
943 + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
949 +static int esw_reset_switch(struct switch_dev *dev)
951 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
953 + esw->global_vlan_enable = 0;
954 + memset(esw->ports, 0, sizeof(esw->ports));
955 + memset(esw->vlans, 0, sizeof(esw->vlans));
961 +static int esw_get_vlan_enable(struct switch_dev *dev,
962 + const struct switch_attr *attr,
963 + struct switch_val *val)
965 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
967 + val->value.i = esw->global_vlan_enable;
972 +static int esw_set_vlan_enable(struct switch_dev *dev,
973 + const struct switch_attr *attr,
974 + struct switch_val *val)
976 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
978 + esw->global_vlan_enable = val->value.i != 0;
983 +static int esw_get_alt_vlan_disable(struct switch_dev *dev,
984 + const struct switch_attr *attr,
985 + struct switch_val *val)
987 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
989 + val->value.i = esw->alt_vlan_disable;
994 +static int esw_set_alt_vlan_disable(struct switch_dev *dev,
995 + const struct switch_attr *attr,
996 + struct switch_val *val)
998 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1000 + esw->alt_vlan_disable = val->value.i != 0;
1006 +rt305x_esw_set_bc_status(struct switch_dev *dev,
1007 + const struct switch_attr *attr,
1008 + struct switch_val *val)
1010 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1012 + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
1018 +rt305x_esw_get_bc_status(struct switch_dev *dev,
1019 + const struct switch_attr *attr,
1020 + struct switch_val *val)
1022 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1024 + val->value.i = esw->bc_storm_protect;
1030 +rt305x_esw_set_led_freq(struct switch_dev *dev,
1031 + const struct switch_attr *attr,
1032 + struct switch_val *val)
1034 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1036 + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
1042 +rt305x_esw_get_led_freq(struct switch_dev *dev,
1043 + const struct switch_attr *attr,
1044 + struct switch_val *val)
1046 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1048 + val->value.i = esw->led_frequency;
1053 +static int esw_get_port_link(struct switch_dev *dev,
1055 + struct switch_port_link *link)
1057 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1060 + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
1063 + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
1065 + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
1066 + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
1067 + if (port < RT305X_ESW_NUM_LEDS) {
1068 + speed = (poa >> RT305X_ESW_SPD_S) & 1;
1070 + if (port == RT305X_ESW_NUM_PORTS - 1)
1072 + speed = (poa >> RT305X_ESW_SPD_S) & 3;
1076 + link->speed = SWITCH_PORT_SPEED_10;
1079 + link->speed = SWITCH_PORT_SPEED_100;
1082 + case 3: /* forced gige speed can be 2 or 3 */
1083 + link->speed = SWITCH_PORT_SPEED_1000;
1086 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
1093 +static int esw_get_port_bool(struct switch_dev *dev,
1094 + const struct switch_attr *attr,
1095 + struct switch_val *val)
1097 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1098 + int idx = val->port_vlan;
1099 + u32 x, reg, shift;
1101 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
1104 + switch (attr->id) {
1105 + case RT305X_ESW_ATTR_PORT_DISABLE:
1106 + reg = RT305X_ESW_REG_POC0;
1107 + shift = RT305X_ESW_POC0_DIS_PORT_S;
1109 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1110 + reg = RT305X_ESW_REG_SGC2;
1111 + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
1113 + case RT305X_ESW_ATTR_PORT_UNTAG:
1114 + reg = RT305X_ESW_REG_POC2;
1115 + shift = RT305X_ESW_POC2_UNTAG_EN_S;
1117 + case RT305X_ESW_ATTR_PORT_LAN:
1118 + reg = RT305X_ESW_REG_SGC2;
1119 + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
1120 + if (idx >= RT305X_ESW_NUM_LANWAN)
1127 + x = esw_r32(esw, reg);
1128 + val->value.i = (x >> (idx + shift)) & 1;
1133 +static int esw_set_port_bool(struct switch_dev *dev,
1134 + const struct switch_attr *attr,
1135 + struct switch_val *val)
1137 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1138 + int idx = val->port_vlan;
1140 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1141 + val->value.i < 0 || val->value.i > 1)
1144 + switch (attr->id) {
1145 + case RT305X_ESW_ATTR_PORT_DISABLE:
1146 + esw->ports[idx].disable = val->value.i;
1148 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1149 + esw->ports[idx].doubletag = val->value.i;
1151 + case RT305X_ESW_ATTR_PORT_UNTAG:
1152 + esw->ports[idx].untag = val->value.i;
1161 +static int esw_get_port_recv_badgood(struct switch_dev *dev,
1162 + const struct switch_attr *attr,
1163 + struct switch_val *val)
1165 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1166 + int idx = val->port_vlan;
1167 + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
1170 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1172 + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
1173 + val->value.i = (reg >> shift) & 0xffff;
1179 +esw_get_port_tr_badgood(struct switch_dev *dev,
1180 + const struct switch_attr *attr,
1181 + struct switch_val *val)
1183 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1185 + int idx = val->port_vlan;
1186 + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
1189 + if (!soc_is_rt5350())
1192 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1195 + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
1196 + val->value.i = (reg >> shift) & 0xffff;
1201 +static int esw_get_port_led(struct switch_dev *dev,
1202 + const struct switch_attr *attr,
1203 + struct switch_val *val)
1205 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1206 + int idx = val->port_vlan;
1208 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1209 + idx >= RT305X_ESW_NUM_LEDS)
1212 + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
1217 +static int esw_set_port_led(struct switch_dev *dev,
1218 + const struct switch_attr *attr,
1219 + struct switch_val *val)
1221 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1222 + int idx = val->port_vlan;
1224 + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
1227 + esw->ports[idx].led = val->value.i;
1232 +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1234 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1236 + if (port >= RT305X_ESW_NUM_PORTS)
1239 + *val = esw_get_pvid(esw, port);
1244 +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
1246 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1248 + if (port >= RT305X_ESW_NUM_PORTS)
1251 + esw->ports[port].pvid = val;
1256 +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1258 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1260 + int vlan_idx = -1;
1265 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
1269 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1270 + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
1271 + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
1277 + if (vlan_idx == -1)
1280 + vmsc = esw_get_vmsc(esw, vlan_idx);
1281 + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
1283 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
1284 + struct switch_port *p;
1285 + int port_mask = 1 << i;
1287 + if (!(vmsc & port_mask))
1290 + p = &val->value.ports[val->len++];
1292 + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
1295 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
1301 +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1303 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1305 + int vlan_idx = -1;
1308 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
1309 + val->len > RT305X_ESW_NUM_PORTS)
1312 + /* one of the already defined vlans? */
1313 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1314 + if (esw->vlans[i].vid == val->port_vlan &&
1315 + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
1321 + /* select a free slot */
1322 + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
1323 + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
1327 + /* bail if all slots are in use */
1328 + if (vlan_idx == -1)
1331 + ports = RT305X_ESW_PORTS_NONE;
1332 + for (i = 0; i < val->len; i++) {
1333 + struct switch_port *p = &val->value.ports[i];
1334 + int port_mask = 1 << p->id;
1335 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
1337 + if (p->id >= RT305X_ESW_NUM_PORTS)
1340 + ports |= port_mask;
1341 + esw->ports[p->id].untag = untagged;
1343 + esw->vlans[vlan_idx].ports = ports;
1344 + if (ports == RT305X_ESW_PORTS_NONE)
1345 + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
1347 + esw->vlans[vlan_idx].vid = val->port_vlan;
1352 +static const struct switch_attr esw_global[] = {
1354 + .type = SWITCH_TYPE_INT,
1355 + .name = "enable_vlan",
1356 + .description = "VLAN mode (1:enabled)",
1358 + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
1359 + .get = esw_get_vlan_enable,
1360 + .set = esw_set_vlan_enable,
1363 + .type = SWITCH_TYPE_INT,
1364 + .name = "alternate_vlan_disable",
1365 + .description = "Use en_vlan instead of doubletag to disable"
1368 + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
1369 + .get = esw_get_alt_vlan_disable,
1370 + .set = esw_set_alt_vlan_disable,
1373 + .type = SWITCH_TYPE_INT,
1374 + .name = "bc_storm_protect",
1375 + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1377 + .id = RT305X_ESW_ATTR_BC_STATUS,
1378 + .get = rt305x_esw_get_bc_status,
1379 + .set = rt305x_esw_set_bc_status,
1382 + .type = SWITCH_TYPE_INT,
1383 + .name = "led_frequency",
1384 + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1386 + .id = RT305X_ESW_ATTR_LED_FREQ,
1387 + .get = rt305x_esw_get_led_freq,
1388 + .set = rt305x_esw_set_led_freq,
1392 +static const struct switch_attr esw_port[] = {
1394 + .type = SWITCH_TYPE_INT,
1395 + .name = "disable",
1396 + .description = "Port state (1:disabled)",
1398 + .id = RT305X_ESW_ATTR_PORT_DISABLE,
1399 + .get = esw_get_port_bool,
1400 + .set = esw_set_port_bool,
1403 + .type = SWITCH_TYPE_INT,
1404 + .name = "doubletag",
1405 + .description = "Double tagging for incoming vlan packets "
1408 + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
1409 + .get = esw_get_port_bool,
1410 + .set = esw_set_port_bool,
1413 + .type = SWITCH_TYPE_INT,
1415 + .description = "Untag (1:strip outgoing vlan tag)",
1417 + .id = RT305X_ESW_ATTR_PORT_UNTAG,
1418 + .get = esw_get_port_bool,
1419 + .set = esw_set_port_bool,
1422 + .type = SWITCH_TYPE_INT,
1424 + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1425 + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1426 + " 8:100mact, 10:blink, 11:off, 12:on)",
1428 + .id = RT305X_ESW_ATTR_PORT_LED,
1429 + .get = esw_get_port_led,
1430 + .set = esw_set_port_led,
1433 + .type = SWITCH_TYPE_INT,
1435 + .description = "HW port group (0:wan, 1:lan)",
1437 + .id = RT305X_ESW_ATTR_PORT_LAN,
1438 + .get = esw_get_port_bool,
1441 + .type = SWITCH_TYPE_INT,
1442 + .name = "recv_bad",
1443 + .description = "Receive bad packet counter",
1444 + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
1445 + .get = esw_get_port_recv_badgood,
1448 + .type = SWITCH_TYPE_INT,
1449 + .name = "recv_good",
1450 + .description = "Receive good packet counter",
1451 + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
1452 + .get = esw_get_port_recv_badgood,
1455 + .type = SWITCH_TYPE_INT,
1458 + .description = "Transmit bad packet counter. rt5350 only",
1459 + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
1460 + .get = esw_get_port_tr_badgood,
1463 + .type = SWITCH_TYPE_INT,
1464 + .name = "tr_good",
1466 + .description = "Transmit good packet counter. rt5350 only",
1467 + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
1468 + .get = esw_get_port_tr_badgood,
1472 +static const struct switch_attr esw_vlan[] = {
1475 +static const struct switch_dev_ops esw_ops = {
1477 + .attr = esw_global,
1478 + .n_attr = ARRAY_SIZE(esw_global),
1482 + .n_attr = ARRAY_SIZE(esw_port),
1486 + .n_attr = ARRAY_SIZE(esw_vlan),
1488 + .get_vlan_ports = esw_get_vlan_ports,
1489 + .set_vlan_ports = esw_set_vlan_ports,
1490 + .get_port_pvid = esw_get_port_pvid,
1491 + .set_port_pvid = esw_set_port_pvid,
1492 + .get_port_link = esw_get_port_link,
1493 + .apply_config = esw_apply_config,
1494 + .reset_switch = esw_reset_switch,
1497 +static struct rt305x_esw_platform_data rt3050_esw_data = {
1498 + /* All ports are LAN ports. */
1499 + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
1500 + .reg_initval_fct2 = 0x00d6500c,
1502 + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1503 + * turbo mii off, rgmi 3.3v off
1505 + * port6: enabled, gige, full-duplex, rx/tx-flow-control
1507 + .reg_initval_fpa2 = 0x3f502b28,
1510 +static const struct of_device_id ralink_esw_match[] = {
1511 + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
1514 +MODULE_DEVICE_TABLE(of, ralink_esw_match);
1516 +static int esw_probe(struct platform_device *pdev)
1518 + struct device_node *np = pdev->dev.of_node;
1519 + const struct rt305x_esw_platform_data *pdata;
1520 + const __be32 *port_map, *reg_init;
1521 + struct rt305x_esw *esw;
1522 + struct switch_dev *swdev;
1523 + struct resource *res, *irq;
1526 + pdata = pdev->dev.platform_data;
1528 + const struct of_device_id *match;
1529 + match = of_match_device(ralink_esw_match, &pdev->dev);
1531 + pdata = (struct rt305x_esw_platform_data *) match->data;
1536 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1538 + dev_err(&pdev->dev, "no memory resource found\n");
1542 + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1544 + dev_err(&pdev->dev, "no irq resource found\n");
1548 + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
1550 + dev_err(&pdev->dev, "no memory for private data\n");
1554 + esw->dev = &pdev->dev;
1555 + esw->irq = irq->start;
1556 + esw->base = ioremap(res->start, resource_size(res));
1558 + dev_err(&pdev->dev, "ioremap failed\n");
1563 + port_map = of_get_property(np, "ralink,portmap", NULL);
1565 + esw->port_map = be32_to_cpu(*port_map);
1567 + reg_init = of_get_property(np, "ralink,fct2", NULL);
1569 + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
1571 + reg_init = of_get_property(np, "ralink,fpa2", NULL);
1573 + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
1575 + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
1577 + esw->reg_led_polarity = be32_to_cpu(*reg_init);
1579 + swdev = &esw->swdev;
1580 + swdev->of_node = pdev->dev.of_node;
1581 + swdev->name = "rt305x-esw";
1582 + swdev->alias = "rt305x";
1583 + swdev->cpu_port = RT305X_ESW_PORT6;
1584 + swdev->ports = RT305X_ESW_NUM_PORTS;
1585 + swdev->vlans = RT305X_ESW_NUM_VIDS;
1586 + swdev->ops = &esw_ops;
1588 + err = register_switch(swdev, NULL);
1590 + dev_err(&pdev->dev, "register_switch failed\n");
1594 + platform_set_drvdata(pdev, esw);
1596 + esw->pdata = pdata;
1597 + spin_lock_init(&esw->reg_rw_lock);
1601 + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
1602 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
1603 + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
1608 + iounmap(esw->base);
1614 +static int esw_remove(struct platform_device *pdev)
1616 + struct rt305x_esw *esw;
1618 + esw = platform_get_drvdata(pdev);
1620 + unregister_switch(&esw->swdev);
1621 + platform_set_drvdata(pdev, NULL);
1622 + iounmap(esw->base);
1629 +static struct platform_driver esw_driver = {
1630 + .probe = esw_probe,
1631 + .remove = esw_remove,
1633 + .name = "rt305x-esw",
1634 + .owner = THIS_MODULE,
1635 + .of_match_table = ralink_esw_match,
1639 +int __init rtesw_init(void)
1641 + return platform_driver_register(&esw_driver);
1644 +void rtesw_exit(void)
1646 + platform_driver_unregister(&esw_driver);
1648 Index: linux-3.14.18/drivers/net/ethernet/ralink/esw_rt3052.h
1649 ===================================================================
1650 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1651 +++ linux-3.14.18/drivers/net/ethernet/ralink/esw_rt3052.h 2014-10-29 20:25:55.433357519 +0100
1654 + * This program is free software; you can redistribute it and/or modify
1655 + * it under the terms of the GNU General Public License as published by
1656 + * the Free Software Foundation; version 2 of the License
1658 + * This program is distributed in the hope that it will be useful,
1659 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1660 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1661 + * GNU General Public License for more details.
1663 + * You should have received a copy of the GNU General Public License
1664 + * along with this program; if not, write to the Free Software
1665 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1667 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1670 +#ifndef _RALINK_ESW_RT3052_H__
1671 +#define _RALINK_ESW_RT3052_H__
1673 +#ifdef CONFIG_NET_RALINK_ESW_RT3052
1675 +int __init rtesw_init(void);
1676 +void rtesw_exit(void);
1680 +static inline int __init rtesw_init(void) { return 0; }
1681 +static inline void rtesw_exit(void) { }
1685 Index: linux-3.14.18/drivers/net/ethernet/ralink/gsw_mt7620a.c
1686 ===================================================================
1687 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1688 +++ linux-3.14.18/drivers/net/ethernet/ralink/gsw_mt7620a.c 2014-10-30 11:05:31.228845263 +0100
1691 + * This program is free software; you can redistribute it and/or modify
1692 + * it under the terms of the GNU General Public License as published by
1693 + * the Free Software Foundation; version 2 of the License
1695 + * This program is distributed in the hope that it will be useful,
1696 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1697 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1698 + * GNU General Public License for more details.
1700 + * You should have received a copy of the GNU General Public License
1701 + * along with this program; if not, write to the Free Software
1702 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1704 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1707 +#include <linux/module.h>
1708 +#include <linux/kernel.h>
1709 +#include <linux/types.h>
1710 +#include <linux/dma-mapping.h>
1711 +#include <linux/init.h>
1712 +#include <linux/skbuff.h>
1713 +#include <linux/etherdevice.h>
1714 +#include <linux/ethtool.h>
1715 +#include <linux/platform_device.h>
1716 +#include <linux/of_device.h>
1717 +#include <linux/clk.h>
1718 +#include <linux/of_net.h>
1719 +#include <linux/of_mdio.h>
1720 +#include <linux/of_irq.h>
1721 +#include <linux/of_address.h>
1722 +#include <linux/switch.h>
1724 +#include <asm/mach-ralink/ralink_regs.h>
1726 +#include "ralink_soc_eth.h"
1728 +#include <linux/ioport.h>
1729 +#include <linux/switch.h>
1730 +#include <linux/mii.h>
1732 +#include <ralink_regs.h>
1733 +#include <asm/mach-ralink/mt7620.h>
1735 +#include "ralink_soc_eth.h"
1736 +#include "gsw_mt7620a.h"
1737 +#include "mt7530.h"
1740 +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
1742 +#define MT7620A_GSW_REG_PIAC 0x7004
1744 +#define GSW_NUM_VLANS 16
1745 +#define GSW_NUM_VIDS 4096
1746 +#define GSW_NUM_PORTS 7
1747 +#define GSW_PORT6 6
1749 +#define GSW_MDIO_ACCESS BIT(31)
1750 +#define GSW_MDIO_READ BIT(19)
1751 +#define GSW_MDIO_WRITE BIT(18)
1752 +#define GSW_MDIO_START BIT(16)
1753 +#define GSW_MDIO_ADDR_SHIFT 20
1754 +#define GSW_MDIO_REG_SHIFT 25
1756 +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
1757 +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
1758 +#define GSW_REG_SMACCR0 0x3fE4
1759 +#define GSW_REG_SMACCR1 0x3fE8
1760 +#define GSW_REG_CKGCR 0x3ff0
1762 +#define GSW_REG_IMR 0x7008
1763 +#define GSW_REG_ISR 0x700c
1765 +#define SYSC_REG_CFG1 0x14
1767 +#define PORT_IRQ_ST_CHG 0x7f
1769 +#define SYSCFG1 0x14
1771 +#define ESW_PHY_POLLING 0x7000
1773 +#define PMCR_IPG BIT(18)
1774 +#define PMCR_MAC_MODE BIT(16)
1775 +#define PMCR_FORCE BIT(15)
1776 +#define PMCR_TX_EN BIT(14)
1777 +#define PMCR_RX_EN BIT(13)
1778 +#define PMCR_BACKOFF BIT(9)
1779 +#define PMCR_BACKPRES BIT(8)
1780 +#define PMCR_RX_FC BIT(5)
1781 +#define PMCR_TX_FC BIT(4)
1782 +#define PMCR_SPEED(_x) (_x << 2)
1783 +#define PMCR_DUPLEX BIT(1)
1784 +#define PMCR_LINK BIT(0)
1786 +#define PHY_AN_EN BIT(31)
1787 +#define PHY_PRE_EN BIT(30)
1788 +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
1791 + /* Global attributes. */
1792 + GSW_ATTR_ENABLE_VLAN,
1793 + /* Port attributes. */
1794 + GSW_ATTR_PORT_UNTAG,
1802 +struct mt7620_gsw {
1803 + struct device *dev;
1804 + void __iomem *base;
1807 + long unsigned int autopoll;
1810 +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
1812 + iowrite32(val, gsw->base + reg);
1815 +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
1817 + return ioread32(gsw->base + reg);
1820 +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
1822 + unsigned long t_start = jiffies;
1825 + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
1827 + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
1832 + printk(KERN_ERR "mdio: MDIO timeout\n");
1836 +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
1839 + if (mt7620_mii_busy_wait(gsw))
1842 + write_data &= 0xffff;
1844 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
1845 + (phy_register << GSW_MDIO_REG_SHIFT) |
1846 + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
1847 + MT7620A_GSW_REG_PIAC);
1849 + if (mt7620_mii_busy_wait(gsw))
1855 +static u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg)
1859 + if (mt7620_mii_busy_wait(gsw))
1862 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
1863 + (phy_reg << GSW_MDIO_REG_SHIFT) |
1864 + (phy_addr << GSW_MDIO_ADDR_SHIFT),
1865 + MT7620A_GSW_REG_PIAC);
1867 + if (mt7620_mii_busy_wait(gsw))
1870 + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
1875 +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
1877 + struct fe_priv *priv = bus->priv;
1878 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1880 + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
1883 +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
1885 + struct fe_priv *priv = bus->priv;
1886 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1888 + return _mt7620_mii_read(gsw, phy_addr, phy_reg);
1891 +static unsigned char *fe_speed_str(int speed)
1908 +int mt7620a_has_carrier(struct fe_priv *priv)
1910 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1913 + for (i = 0; i < GSW_PORT6; i++)
1914 + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
1919 +static void mt7620a_handle_carrier(struct fe_priv *priv)
1924 + if (mt7620a_has_carrier(priv))
1925 + netif_carrier_on(priv->netdev);
1927 + netif_carrier_off(priv->netdev);
1930 +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
1932 + if (priv->link[port])
1933 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1934 + port, fe_speed_str(priv->phy->speed[port]),
1935 + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
1937 + netdev_info(priv->netdev, "port %d link down\n", port);
1938 + mt7620a_handle_carrier(priv);
1941 +static irqreturn_t gsw_interrupt(int irq, void *_priv)
1943 + struct fe_priv *priv = (struct fe_priv *) _priv;
1944 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1946 + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
1948 + status = gsw_r32(gsw, GSW_REG_ISR);
1949 + if (status & PORT_IRQ_ST_CHG)
1950 + for (i = 0; i <= max; i++) {
1951 + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
1952 + int link = status & 0x1;
1954 + if (link != priv->link[i]) {
1956 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1957 + i, fe_speed_str((status >> 2) & 3),
1958 + (status & 0x2) ? "Full" : "Half");
1960 + netdev_info(priv->netdev, "port %d link down\n", i);
1963 + priv->link[i] = link;
1965 + mt7620a_handle_carrier(priv);
1967 + gsw_w32(gsw, status, GSW_REG_ISR);
1969 + return IRQ_HANDLED;
1972 +static int mt7620_is_bga(void)
1974 + u32 bga = rt_sysc_r32(0x0c);
1976 + return (bga >> 16) & 1;
1979 +static void gsw_auto_poll(struct mt7620_gsw *gsw)
1982 + int lsb = -1, msb = 0;
1984 + for_each_set_bit(phy, &gsw->autopoll, 32) {
1990 + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
1993 +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
1995 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1996 + const __be32 *_id = of_get_property(np, "reg", NULL);
1997 + int phy_mode, size, id;
1999 + u32 val, mask = 0;
2000 + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
2002 + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
2004 + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
2006 + pr_err("%s: invalid port id\n", np->name);
2010 + id = be32_to_cpu(*_id);
2015 + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
2016 + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
2017 + pr_err("%s: invalid fixed link property\n", np->name);
2018 + priv->phy->phy_fixed[id] = NULL;
2022 + phy_mode = of_get_phy_mode(np);
2023 + switch (phy_mode) {
2024 + case PHY_INTERFACE_MODE_RGMII:
2027 + case PHY_INTERFACE_MODE_MII:
2030 + case PHY_INTERFACE_MODE_RMII:
2034 + dev_err(priv->device, "port %d - invalid phy mode\n", id);
2038 + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
2039 + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
2042 + val = rt_sysc_r32(SYSCFG1);
2043 + val &= ~(3 << shift);
2044 + val |= mask << shift;
2045 + rt_sysc_w32(val, SYSCFG1);
2047 + if (priv->phy->phy_fixed[id]) {
2048 + const __be32 *link = priv->phy->phy_fixed[id];
2052 + priv->phy->speed[id] = be32_to_cpup(link++);
2053 + tx_fc = be32_to_cpup(link++);
2054 + rx_fc = be32_to_cpup(link++);
2055 + priv->phy->duplex[id] = be32_to_cpup(link++);
2056 + priv->link[id] = 1;
2058 + switch (priv->phy->speed[id]) {
2069 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
2070 + priv->phy->phy_fixed[id] = 0;
2073 + val = PMCR_SPEED(val);
2074 + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2075 + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
2077 + val |= PMCR_TX_FC;
2079 + val |= PMCR_RX_FC;
2080 + if (priv->phy->duplex[id])
2081 + val |= PMCR_DUPLEX;
2082 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2083 + dev_info(priv->device, "using fixed link parameters\n");
2087 + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
2088 + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2089 + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
2091 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2092 + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
2093 + gsw->autopoll |= BIT(id);
2094 + gsw_auto_poll(gsw);
2099 +static void gsw_hw_init(struct mt7620_gsw *gsw)
2101 + u32 is_BGA = mt7620_is_bga();
2103 + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
2104 + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
2106 + /*correct PHY setting L3.0 BGA*/
2107 + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
2109 + _mt7620_mii_write(gsw, 1, 17, 0x7444);
2111 + _mt7620_mii_write(gsw, 1, 19, 0x0114);
2113 + _mt7620_mii_write(gsw, 1, 19, 0x0117);
2115 + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
2116 + _mt7620_mii_write(gsw, 1, 25, 0x6212);
2117 + _mt7620_mii_write(gsw, 1, 26, 0x0777);
2118 + _mt7620_mii_write(gsw, 1, 29, 0x4000);
2119 + _mt7620_mii_write(gsw, 1, 28, 0xc077);
2120 + _mt7620_mii_write(gsw, 1, 24, 0x0000);
2122 + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
2123 + _mt7620_mii_write(gsw, 1, 17, 0x4838);
2125 + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
2127 + _mt7620_mii_write(gsw, 1, 21, 0x0515);
2128 + _mt7620_mii_write(gsw, 1, 22, 0x0053);
2129 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2130 + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
2131 + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
2132 + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
2134 + _mt7620_mii_write(gsw, 1, 21, 0x0517);
2135 + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
2136 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2137 + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
2138 + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
2139 + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
2141 + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
2142 + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
2144 + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
2145 + _mt7620_mii_write(gsw, 0, 30, 0xa000);
2146 + _mt7620_mii_write(gsw, 1, 30, 0xa000);
2147 + _mt7620_mii_write(gsw, 2, 30, 0xa000);
2148 + _mt7620_mii_write(gsw, 3, 30, 0xa000);
2150 + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
2151 + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
2152 + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
2153 + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
2155 + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
2156 + _mt7620_mii_write(gsw, 0, 16, 0x1111);
2157 + _mt7620_mii_write(gsw, 1, 16, 0x1010);
2158 + _mt7620_mii_write(gsw, 2, 16, 0x1515);
2159 + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
2161 + /* CPU Port6 Force Link 1G, FC ON */
2162 + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
2163 + /* Set Port6 CPU Port */
2164 + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
2166 + /* setup port 4 */
2167 + if (gsw->port4 == PORT4_EPHY) {
2168 + u32 val = rt_sysc_r32(SYSCFG1);
2170 + rt_sysc_w32(val, SYSCFG1);
2171 + _mt7620_mii_write(gsw, 4, 30, 0xa000);
2172 + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
2173 + _mt7620_mii_write(gsw, 4, 16, 0x1313);
2174 + pr_info("gsw: setting port4 to ephy mode\n");
2178 +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
2180 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2181 + unsigned long flags;
2183 + spin_lock_irqsave(&priv->page_lock, flags);
2184 + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
2185 + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
2187 + spin_unlock_irqrestore(&priv->page_lock, flags);
2190 +static struct of_device_id gsw_match[] = {
2191 + { .compatible = "ralink,mt7620a-gsw" },
2195 +int mt7620_gsw_config(struct fe_priv *priv)
2197 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2199 + /* is the mt7530 internal or external */
2200 + if (priv->mii_bus && priv->mii_bus->phy_map[0x1f]) {
2201 + mt7530_probe(priv->device, gsw->base, NULL, 0);
2202 + mt7530_probe(priv->device, NULL, priv->mii_bus, 1);
2204 + mt7530_probe(priv->device, gsw->base, NULL, 1);
2210 +int mt7620_gsw_probe(struct fe_priv *priv)
2212 + struct mt7620_gsw *gsw;
2213 + struct device_node *np;
2214 + const char *port4 = NULL;
2216 + np = of_find_matching_node(NULL, gsw_match);
2218 + dev_err(priv->device, "no gsw node found\n");
2221 + np = of_node_get(np);
2223 + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
2225 + dev_err(priv->device, "no gsw memory for private data\n");
2229 + gsw->irq = irq_of_parse_and_map(np, 0);
2231 + dev_err(priv->device, "no gsw irq resource found\n");
2235 + gsw->base = of_iomap(np, 0);
2237 + dev_err(priv->device, "gsw ioremap failed\n");
2241 + gsw->dev = priv->device;
2242 + priv->soc->swpriv = gsw;
2244 + of_property_read_string(np, "ralink,port4", &port4);
2245 + if (port4 && !strcmp(port4, "ephy"))
2246 + gsw->port4 = PORT4_EPHY;
2247 + else if (port4 && !strcmp(port4, "gmac"))
2248 + gsw->port4 = PORT4_EXT;
2254 + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
2255 + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
2259 Index: linux-3.14.18/drivers/net/ethernet/ralink/gsw_mt7620a.h
2260 ===================================================================
2261 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2262 +++ linux-3.14.18/drivers/net/ethernet/ralink/gsw_mt7620a.h 2014-10-29 20:25:55.433357519 +0100
2265 + * This program is free software; you can redistribute it and/or modify
2266 + * it under the terms of the GNU General Public License as published by
2267 + * the Free Software Foundation; version 2 of the License
2269 + * This program is distributed in the hope that it will be useful,
2270 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2271 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2272 + * GNU General Public License for more details.
2274 + * You should have received a copy of the GNU General Public License
2275 + * along with this program; if not, write to the Free Software
2276 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2278 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2281 +#ifndef _RALINK_GSW_MT7620_H__
2282 +#define _RALINK_GSW_MT7620_H__
2284 +extern int mt7620_gsw_config(struct fe_priv *priv);
2285 +extern int mt7620_gsw_probe(struct fe_priv *priv);
2286 +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
2287 +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2288 +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2289 +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
2290 +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
2291 +extern int mt7620a_has_carrier(struct fe_priv *priv);
2294 Index: linux-3.14.18/drivers/net/ethernet/ralink/mdio.c
2295 ===================================================================
2296 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2297 +++ linux-3.14.18/drivers/net/ethernet/ralink/mdio.c 2014-10-29 20:25:55.433357519 +0100
2300 + * This program is free software; you can redistribute it and/or modify
2301 + * it under the terms of the GNU General Public License as published by
2302 + * the Free Software Foundation; version 2 of the License
2304 + * This program is distributed in the hope that it will be useful,
2305 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2306 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2307 + * GNU General Public License for more details.
2309 + * You should have received a copy of the GNU General Public License
2310 + * along with this program; if not, write to the Free Software
2311 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2313 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2316 +#include <linux/module.h>
2317 +#include <linux/kernel.h>
2318 +#include <linux/types.h>
2319 +#include <linux/dma-mapping.h>
2320 +#include <linux/init.h>
2321 +#include <linux/skbuff.h>
2322 +#include <linux/etherdevice.h>
2323 +#include <linux/ethtool.h>
2324 +#include <linux/platform_device.h>
2325 +#include <linux/phy.h>
2326 +#include <linux/of_device.h>
2327 +#include <linux/clk.h>
2328 +#include <linux/of_net.h>
2329 +#include <linux/of_mdio.h>
2331 +#include "ralink_soc_eth.h"
2334 +static int fe_mdio_reset(struct mii_bus *bus)
2340 +static void fe_phy_link_adjust(struct net_device *dev)
2342 + struct fe_priv *priv = netdev_priv(dev);
2343 + unsigned long flags;
2346 + spin_lock_irqsave(&priv->phy->lock, flags);
2347 + for (i = 0; i < 8; i++) {
2348 + if (priv->phy->phy_node[i]) {
2349 + struct phy_device *phydev = priv->phy->phy[i];
2350 + int status_change = 0;
2353 + if (priv->phy->duplex[i] != phydev->duplex ||
2354 + priv->phy->speed[i] != phydev->speed)
2355 + status_change = 1;
2357 + if (phydev->link != priv->link[i])
2358 + status_change = 1;
2360 + switch (phydev->speed) {
2364 + priv->link[i] = phydev->link;
2365 + priv->phy->duplex[i] = phydev->duplex;
2366 + priv->phy->speed[i] = phydev->speed;
2368 + if (status_change && priv->soc->mdio_adjust_link)
2369 + priv->soc->mdio_adjust_link(priv, i);
2376 +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
2378 + const __be32 *_port = NULL;
2379 + struct phy_device *phydev;
2380 + int phy_mode, port;
2382 + _port = of_get_property(phy_node, "reg", NULL);
2384 + if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
2385 + pr_err("%s: invalid port id\n", phy_node->name);
2388 + port = be32_to_cpu(*_port);
2389 + phy_mode = of_get_phy_mode(phy_node);
2390 + if (phy_mode < 0) {
2391 + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
2392 + priv->phy->phy_node[port] = NULL;
2396 + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
2398 + if (IS_ERR(phydev)) {
2399 + dev_err(priv->device, "could not connect to PHY\n");
2400 + priv->phy->phy_node[port] = NULL;
2401 + return PTR_ERR(phydev);
2404 + phydev->supported &= PHY_GBIT_FEATURES;
2405 + phydev->advertising = phydev->supported;
2406 + phydev->no_auto_carrier_off = 1;
2408 + dev_info(priv->device,
2409 + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
2410 + port, dev_name(&phydev->dev), phydev->phy_id,
2411 + phydev->drv->name);
2413 + priv->phy->phy[port] = phydev;
2414 + priv->link[port] = 0;
2419 +static void phy_init(struct fe_priv *priv, struct phy_device *phy)
2421 + phy_attach(priv->netdev, dev_name(&phy->dev), PHY_INTERFACE_MODE_MII);
2423 + phy->autoneg = AUTONEG_ENABLE;
2426 + phy->supported &= PHY_BASIC_FEATURES;
2427 + phy->advertising = phy->supported | ADVERTISED_Autoneg;
2429 + phy_start_aneg(phy);
2432 +static int fe_phy_connect(struct fe_priv *priv)
2436 + for (i = 0; i < 8; i++) {
2437 + if (priv->phy->phy_node[i]) {
2438 + if (!priv->phy_dev) {
2439 + priv->phy_dev = priv->phy->phy[i];
2440 + priv->phy_flags = FE_PHY_FLAG_PORT;
2442 + } else if (priv->mii_bus && priv->mii_bus->phy_map[i]) {
2443 + phy_init(priv, priv->mii_bus->phy_map[i]);
2444 + if (!priv->phy_dev) {
2445 + priv->phy_dev = priv->mii_bus->phy_map[i];
2446 + priv->phy_flags = FE_PHY_FLAG_ATTACH;
2454 +static void fe_phy_disconnect(struct fe_priv *priv)
2456 + unsigned long flags;
2459 + for (i = 0; i < 8; i++)
2460 + if (priv->phy->phy_fixed[i]) {
2461 + spin_lock_irqsave(&priv->phy->lock, flags);
2462 + priv->link[i] = 0;
2463 + if (priv->soc->mdio_adjust_link)
2464 + priv->soc->mdio_adjust_link(priv, i);
2465 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2466 + } else if (priv->phy->phy[i]) {
2467 + phy_disconnect(priv->phy->phy[i]);
2468 + } else if (priv->mii_bus && priv->mii_bus->phy_map[i]) {
2469 + phy_detach(priv->mii_bus->phy_map[i]);
2473 +static void fe_phy_start(struct fe_priv *priv)
2475 + unsigned long flags;
2478 + for (i = 0; i < 8; i++) {
2479 + if (priv->phy->phy_fixed[i]) {
2480 + spin_lock_irqsave(&priv->phy->lock, flags);
2481 + priv->link[i] = 1;
2482 + if (priv->soc->mdio_adjust_link)
2483 + priv->soc->mdio_adjust_link(priv, i);
2484 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2485 + } else if (priv->phy->phy[i]) {
2486 + phy_start(priv->phy->phy[i]);
2491 +static void fe_phy_stop(struct fe_priv *priv)
2493 + unsigned long flags;
2496 + for (i = 0; i < 8; i++)
2497 + if (priv->phy->phy_fixed[i]) {
2498 + spin_lock_irqsave(&priv->phy->lock, flags);
2499 + priv->link[i] = 0;
2500 + if (priv->soc->mdio_adjust_link)
2501 + priv->soc->mdio_adjust_link(priv, i);
2502 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2503 + } else if (priv->phy->phy[i]) {
2504 + phy_stop(priv->phy->phy[i]);
2508 +static struct fe_phy phy_ralink = {
2509 + .connect = fe_phy_connect,
2510 + .disconnect = fe_phy_disconnect,
2511 + .start = fe_phy_start,
2512 + .stop = fe_phy_stop,
2515 +int fe_mdio_init(struct fe_priv *priv)
2517 + struct device_node *mii_np;
2520 + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
2523 + spin_lock_init(&phy_ralink.lock);
2524 + priv->phy = &phy_ralink;
2526 + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
2528 + dev_err(priv->device, "no %s child node found", "mdio-bus");
2532 + if (!of_device_is_available(mii_np)) {
2534 + goto err_put_node;
2537 + priv->mii_bus = mdiobus_alloc();
2538 + if (priv->mii_bus == NULL) {
2540 + goto err_put_node;
2543 + priv->mii_bus->name = "mdio";
2544 + priv->mii_bus->read = priv->soc->mdio_read;
2545 + priv->mii_bus->write = priv->soc->mdio_write;
2546 + priv->mii_bus->reset = fe_mdio_reset;
2547 + priv->mii_bus->priv = priv;
2548 + priv->mii_bus->parent = priv->device;
2550 + snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
2551 + err = of_mdiobus_register(priv->mii_bus, mii_np);
2553 + goto err_free_bus;
2558 + kfree(priv->mii_bus);
2560 + of_node_put(mii_np);
2561 + priv->mii_bus = NULL;
2565 +void fe_mdio_cleanup(struct fe_priv *priv)
2567 + if (!priv->mii_bus)
2570 + mdiobus_unregister(priv->mii_bus);
2571 + of_node_put(priv->mii_bus->dev.of_node);
2572 + kfree(priv->mii_bus);
2574 Index: linux-3.14.18/drivers/net/ethernet/ralink/mdio.h
2575 ===================================================================
2576 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2577 +++ linux-3.14.18/drivers/net/ethernet/ralink/mdio.h 2014-10-29 20:25:55.433357519 +0100
2580 + * This program is free software; you can redistribute it and/or modify
2581 + * it under the terms of the GNU General Public License as published by
2582 + * the Free Software Foundation; version 2 of the License
2584 + * This program is distributed in the hope that it will be useful,
2585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2587 + * GNU General Public License for more details.
2589 + * You should have received a copy of the GNU General Public License
2590 + * along with this program; if not, write to the Free Software
2591 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2593 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2596 +#ifndef _RALINK_MDIO_H__
2597 +#define _RALINK_MDIO_H__
2599 +#ifdef CONFIG_NET_RALINK_MDIO
2600 +extern int fe_mdio_init(struct fe_priv *priv);
2601 +extern void fe_mdio_cleanup(struct fe_priv *priv);
2602 +extern int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node);
2604 +static inline int fe_mdio_init(struct fe_priv *priv) { return 0; }
2605 +static inline void fe_mdio_cleanup(struct fe_priv *priv) {}
2608 Index: linux-3.14.18/drivers/net/ethernet/ralink/mdio_rt2880.c
2609 ===================================================================
2610 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2611 +++ linux-3.14.18/drivers/net/ethernet/ralink/mdio_rt2880.c 2014-10-29 20:25:55.433357519 +0100
2614 + * This program is free software; you can redistribute it and/or modify
2615 + * it under the terms of the GNU General Public License as published by
2616 + * the Free Software Foundation; version 2 of the License
2618 + * This program is distributed in the hope that it will be useful,
2619 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2620 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2621 + * GNU General Public License for more details.
2623 + * You should have received a copy of the GNU General Public License
2624 + * along with this program; if not, write to the Free Software
2625 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2627 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2630 +#include <linux/module.h>
2631 +#include <linux/kernel.h>
2632 +#include <linux/types.h>
2633 +#include <linux/dma-mapping.h>
2634 +#include <linux/init.h>
2635 +#include <linux/skbuff.h>
2636 +#include <linux/etherdevice.h>
2637 +#include <linux/ethtool.h>
2638 +#include <linux/platform_device.h>
2639 +#include <linux/phy.h>
2640 +#include <linux/of_device.h>
2641 +#include <linux/clk.h>
2642 +#include <linux/of_net.h>
2643 +#include <linux/of_mdio.h>
2645 +#include "ralink_soc_eth.h"
2646 +#include "mdio_rt2880.h"
2649 +#define FE_MDIO_RETRY 1000
2651 +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
2653 + switch (priv->phy->speed[0]) {
2665 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
2669 + if (!priv->link[0]) {
2670 + netif_carrier_off(priv->netdev);
2671 + netdev_info(priv->netdev, "link down\n");
2675 + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
2676 + FE_MDIO_CFG_RX_CLK_SKEW_200 |
2677 + FE_MDIO_CFG_GP1_FRC_EN;
2679 + if (priv->phy->duplex[0] == DUPLEX_FULL)
2680 + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
2682 + if (priv->phy->tx_fc[0])
2683 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
2685 + if (priv->phy->rx_fc[0])
2686 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
2688 + switch (priv->phy->speed[0]) {
2690 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
2693 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
2696 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
2702 + fe_w32(mdio_cfg, FE_MDIO_CFG);
2704 + netif_carrier_on(priv->netdev);
2705 + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
2706 + rt2880_speed_str(priv),
2707 + (DUPLEX_FULL == priv->phy->duplex[0]) ? "Full" : "Half");
2710 +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
2714 + retries = FE_MDIO_RETRY;
2718 + t = fe_r32(FE_MDIO_ACCESS);
2719 + if ((t & (0x1 << 31)) == 0)
2722 + if (retries-- == 0)
2728 + dev_err(priv->device, "MDIO operation timed out\n");
2729 + return -ETIMEDOUT;
2732 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
2734 + struct fe_priv *priv = bus->priv;
2738 + err = rt2880_mdio_wait_ready(priv);
2742 + t = (phy_addr << 24) | (phy_reg << 16);
2743 + fe_w32(t, FE_MDIO_ACCESS);
2745 + fe_w32(t, FE_MDIO_ACCESS);
2747 + err = rt2880_mdio_wait_ready(priv);
2751 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2752 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2754 + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
2757 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
2759 + struct fe_priv *priv = bus->priv;
2763 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2764 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2766 + err = rt2880_mdio_wait_ready(priv);
2770 + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
2771 + fe_w32(t, FE_MDIO_ACCESS);
2773 + fe_w32(t, FE_MDIO_ACCESS);
2775 + return rt2880_mdio_wait_ready(priv);
2778 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
2780 + const __be32 *id = of_get_property(np, "reg", NULL);
2781 + const __be32 *link;
2785 + if (!id || (be32_to_cpu(*id) != 0)) {
2786 + pr_err("%s: invalid port id\n", np->name);
2790 + priv->phy->phy_fixed[0] = of_get_property(np, "ralink,fixed-link", &size);
2791 + if (priv->phy->phy_fixed[0] && (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
2792 + pr_err("%s: invalid fixed link property\n", np->name);
2793 + priv->phy->phy_fixed[0] = NULL;
2797 + phy_mode = of_get_phy_mode(np);
2798 + switch (phy_mode) {
2799 + case PHY_INTERFACE_MODE_RGMII:
2801 + case PHY_INTERFACE_MODE_MII:
2803 + case PHY_INTERFACE_MODE_RMII:
2806 + if (!priv->phy->phy_fixed[0])
2807 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[0]);
2811 + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
2812 + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
2815 + if (priv->phy->phy_fixed[0]) {
2816 + link = priv->phy->phy_fixed[0];
2817 + priv->phy->speed[0] = be32_to_cpup(link++);
2818 + priv->phy->duplex[0] = be32_to_cpup(link++);
2819 + priv->phy->tx_fc[0] = be32_to_cpup(link++);
2820 + priv->phy->rx_fc[0] = be32_to_cpup(link++);
2822 + priv->link[0] = 1;
2823 + switch (priv->phy->speed[0]) {
2831 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[0]);
2832 + priv->phy->phy_fixed[0] = 0;
2835 + dev_info(priv->device, "using fixed link parameters\n");
2836 + rt2880_mdio_link_adjust(priv, 0);
2839 + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0]) {
2840 + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
2845 Index: linux-3.14.18/drivers/net/ethernet/ralink/mdio_rt2880.h
2846 ===================================================================
2847 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2848 +++ linux-3.14.18/drivers/net/ethernet/ralink/mdio_rt2880.h 2014-10-29 20:25:55.433357519 +0100
2851 + * This program is free software; you can redistribute it and/or modify
2852 + * it under the terms of the GNU General Public License as published by
2853 + * the Free Software Foundation; version 2 of the License
2855 + * This program is distributed in the hope that it will be useful,
2856 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2857 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2858 + * GNU General Public License for more details.
2860 + * You should have received a copy of the GNU General Public License
2861 + * along with this program; if not, write to the Free Software
2862 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2864 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2867 +#ifndef _RALINK_MDIO_RT2880_H__
2868 +#define _RALINK_MDIO_RT2880_H__
2870 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
2871 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2872 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2873 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
2876 Index: linux-3.14.18/drivers/net/ethernet/ralink/mt7530.c
2877 ===================================================================
2878 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2879 +++ linux-3.14.18/drivers/net/ethernet/ralink/mt7530.c 2014-10-30 09:37:26.847089658 +0100
2882 + * This program is free software; you can redistribute it and/or
2883 + * modify it under the terms of the GNU General Public License
2884 + * as published by the Free Software Foundation; either version 2
2885 + * of the License, or (at your option) any later version.
2887 + * This program is distributed in the hope that it will be useful,
2888 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2889 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2890 + * GNU General Public License for more details.
2892 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
2895 +#include <linux/if.h>
2896 +#include <linux/module.h>
2897 +#include <linux/init.h>
2898 +#include <linux/list.h>
2899 +#include <linux/if_ether.h>
2900 +#include <linux/skbuff.h>
2901 +#include <linux/netdevice.h>
2902 +#include <linux/netlink.h>
2903 +#include <linux/bitops.h>
2904 +#include <net/genetlink.h>
2905 +#include <linux/switch.h>
2906 +#include <linux/delay.h>
2907 +#include <linux/phy.h>
2908 +#include <linux/netdevice.h>
2909 +#include <linux/etherdevice.h>
2910 +#include <linux/lockdep.h>
2911 +#include <linux/workqueue.h>
2912 +#include <linux/of_device.h>
2914 +#include "mt7530.h"
2916 +#define MT7530_CPU_PORT 6
2917 +#define MT7530_NUM_PORTS 8
2918 +#define MT7530_NUM_VLANS 16
2919 +#define MT7530_MAX_VID 4095
2920 +#define MT7530_MIN_VID 0
2923 +#define REG_ESW_VLAN_VTCR 0x90
2924 +#define REG_ESW_VLAN_VAWD1 0x94
2925 +#define REG_ESW_VLAN_VAWD2 0x98
2926 +#define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
2928 +#define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
2929 +#define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
2930 +#define REG_ESW_VLAN_VAWD1_VALID BIT(0)
2932 +/* vlan egress mode */
2934 + ETAG_CTRL_UNTAG = 0,
2935 + ETAG_CTRL_TAG = 2,
2936 + ETAG_CTRL_SWAP = 1,
2937 + ETAG_CTRL_STACK = 3,
2940 +#define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
2941 +#define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
2942 +#define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
2944 +#define REG_HWTRAP 0x7804
2947 + /* Global attributes. */
2948 + MT7530_ATTR_ENABLE_VLAN,
2951 +struct mt7530_port_entry {
2955 +struct mt7530_vlan_entry {
2961 +struct mt7530_priv {
2962 + void __iomem *base;
2963 + struct mii_bus *bus;
2964 + struct switch_dev swdev;
2966 + bool global_vlan_enable;
2967 + struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
2968 + struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
2971 +struct mt7530_mapping {
2973 + u16 pvids[MT7530_NUM_PORTS];
2974 + u8 members[MT7530_NUM_VLANS];
2975 + u8 etags[MT7530_NUM_VLANS];
2976 + u16 vids[MT7530_NUM_VLANS];
2977 +} mt7530_defaults[] = {
2980 + .pvids = { 1, 1, 1, 1, 2, 1, 1 },
2981 + .members = { 0, 0x6f, 0x50 },
2982 + .etags = { 0, 0x40, 0x40 },
2983 + .vids = { 0, 1, 2 },
2986 + .pvids = { 2, 1, 1, 1, 1, 1, 1 },
2987 + .members = { 0, 0x7e, 0x41 },
2988 + .etags = { 0, 0x40, 0x40 },
2989 + .vids = { 0, 1, 2 },
2993 +struct mt7530_mapping*
2994 +mt7530_find_mapping(struct device_node *np)
2999 + if (of_property_read_string(np, "ralink,port-map", &map))
3002 + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
3003 + if (!strcmp(map, mt7530_defaults[i].name))
3004 + return &mt7530_defaults[i];
3010 +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
3014 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3015 + mt7530->port_entries[i].pvid = map->pvids[i];
3017 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3018 + mt7530->vlan_entries[i].member = map->members[i];
3019 + mt7530->vlan_entries[i].etags = map->etags[i];
3020 + mt7530->vlan_entries[i].vid = map->vids[i];
3025 +mt7530_reset_switch(struct switch_dev *dev)
3027 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3030 + memset(priv->port_entries, 0, sizeof(priv->port_entries));
3031 + memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
3033 + /* set default vid of each vlan to the same number of vlan, so the vid
3034 + * won't need be set explicitly.
3036 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3037 + priv->vlan_entries[i].vid = i;
3044 +mt7530_get_vlan_enable(struct switch_dev *dev,
3045 + const struct switch_attr *attr,
3046 + struct switch_val *val)
3048 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3050 + val->value.i = priv->global_vlan_enable;
3056 +mt7530_set_vlan_enable(struct switch_dev *dev,
3057 + const struct switch_attr *attr,
3058 + struct switch_val *val)
3060 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3062 + priv->global_vlan_enable = val->value.i != 0;
3068 +mt7530_r32(struct mt7530_priv *priv, u32 reg)
3074 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3075 + low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
3076 + high = mdiobus_read(priv->bus, 0x1f, 0x10);
3078 + return (high << 16) | (low & 0xffff);
3081 + val = ioread32(priv->base + reg);
3082 + pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
3088 +mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
3091 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3092 + mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
3093 + mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
3097 + pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
3098 + iowrite32(val, priv->base + reg);
3102 +mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
3106 + mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
3108 + for (i = 0; i < 20; i++) {
3109 + u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
3111 + if ((val & BIT(31)) == 0)
3117 + printk("mt7530: vtcr timeout\n");
3121 +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
3123 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3125 + if (port >= MT7530_NUM_PORTS)
3128 + *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
3135 +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
3137 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3139 + if (port >= MT7530_NUM_PORTS)
3142 + if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
3145 + priv->port_entries[port].pvid = pvid;
3151 +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3153 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3160 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
3163 + mt7530_vtcr(priv, 0, val->port_vlan);
3165 + member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
3169 + etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
3171 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3172 + struct switch_port *p;
3175 + if (!(member & BIT(i)))
3178 + p = &val->value.ports[val->len++];
3181 + etag = (etags >> (i * 2)) & 0x3;
3183 + if (etag == ETAG_CTRL_TAG)
3184 + p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
3185 + else if (etag != ETAG_CTRL_UNTAG)
3186 + printk("vlan egress tag control neither untag nor tag.\n");
3193 +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3195 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3200 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
3201 + val->len > MT7530_NUM_PORTS)
3204 + for (i = 0; i < val->len; i++) {
3205 + struct switch_port *p = &val->value.ports[i];
3207 + if (p->id >= MT7530_NUM_PORTS)
3210 + member |= BIT(p->id);
3212 + if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
3213 + etags |= BIT(p->id);
3215 + priv->vlan_entries[val->port_vlan].member = member;
3216 + priv->vlan_entries[val->port_vlan].etags = etags;
3222 +mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
3223 + struct switch_val *val)
3225 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3229 + vlan = val->port_vlan;
3230 + vid = (u16)val->value.i;
3232 + if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
3235 + if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
3238 + priv->vlan_entries[vlan].vid = vid;
3243 +mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
3244 + struct switch_val *val)
3246 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3250 + vlan = val->port_vlan;
3252 + vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
3257 + val->value.i = vid;
3262 +mt7530_apply_config(struct switch_dev *dev)
3264 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3267 + if (!priv->global_vlan_enable) {
3268 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3269 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
3271 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3272 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x810000c0);
3277 + /* set all ports as security mode */
3278 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3279 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
3281 + /* set all ports as user port */
3282 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3283 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x81000000);
3285 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3286 + u16 vid = priv->vlan_entries[i].vid;
3287 + u8 member = priv->vlan_entries[i].member;
3288 + u8 etags = priv->vlan_entries[i].etags;
3292 + val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
3298 + val |= (vid << 12);
3300 + mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
3302 + /* vlan port membership */
3304 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
3305 + REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
3306 + REG_ESW_VLAN_VAWD1_VALID);
3308 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
3312 + for (j = 0; j < MT7530_NUM_PORTS; j++) {
3313 + if (etags & BIT(j))
3314 + val |= ETAG_CTRL_TAG << (j * 2);
3316 + val |= ETAG_CTRL_UNTAG << (j * 2);
3318 + mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
3320 + /* write to vlan table */
3321 + mt7530_vtcr(priv, 1, i);
3324 + /* Port Default PVID */
3325 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3327 + val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
3329 + val |= priv->port_entries[i].pvid;
3330 + mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
3337 +mt7530_get_port_link(struct switch_dev *dev, int port,
3338 + struct switch_port_link *link)
3340 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3343 + if (port < 0 || port >= MT7530_NUM_PORTS)
3346 + pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
3348 + link->link = pmsr & 1;
3349 + link->duplex = (pmsr >> 1) & 1;
3350 + speed = (pmsr >> 2) & 3;
3354 + link->speed = SWITCH_PORT_SPEED_10;
3357 + link->speed = SWITCH_PORT_SPEED_100;
3360 + case 3: /* forced gige speed can be 2 or 3 */
3361 + link->speed = SWITCH_PORT_SPEED_1000;
3364 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
3371 +static const struct switch_attr mt7530_global[] = {
3373 + .type = SWITCH_TYPE_INT,
3374 + .name = "enable_vlan",
3375 + .description = "VLAN mode (1:enabled)",
3377 + .id = MT7530_ATTR_ENABLE_VLAN,
3378 + .get = mt7530_get_vlan_enable,
3379 + .set = mt7530_set_vlan_enable,
3383 +static const struct switch_attr mt7530_port[] = {
3386 +static const struct switch_attr mt7530_vlan[] = {
3388 + .type = SWITCH_TYPE_INT,
3390 + .description = "VLAN ID (0-4094)",
3391 + .set = mt7530_set_vid,
3392 + .get = mt7530_get_vid,
3397 +static const struct switch_dev_ops mt7530_ops = {
3399 + .attr = mt7530_global,
3400 + .n_attr = ARRAY_SIZE(mt7530_global),
3403 + .attr = mt7530_port,
3404 + .n_attr = ARRAY_SIZE(mt7530_port),
3407 + .attr = mt7530_vlan,
3408 + .n_attr = ARRAY_SIZE(mt7530_vlan),
3410 + .get_vlan_ports = mt7530_get_vlan_ports,
3411 + .set_vlan_ports = mt7530_set_vlan_ports,
3412 + .get_port_pvid = mt7530_get_port_pvid,
3413 + .set_port_pvid = mt7530_set_port_pvid,
3414 + .get_port_link = mt7530_get_port_link,
3415 + .apply_config = mt7530_apply_config,
3416 + .reset_switch = mt7530_reset_switch,
3420 +mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan)
3422 + struct switch_dev *swdev;
3423 + struct mt7530_priv *mt7530;
3424 + struct mt7530_mapping *map;
3427 + mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
3431 + mt7530->base = base;
3432 + mt7530->bus = bus;
3433 + mt7530->global_vlan_enable = vlan;
3435 + swdev = &mt7530->swdev;
3437 + swdev->alias = "mt7530";
3438 + swdev->name = "mt7530";
3440 + swdev->alias = "mt7620";
3441 + swdev->name = "mt7620";
3443 + swdev->cpu_port = MT7530_CPU_PORT;
3444 + swdev->ports = MT7530_NUM_PORTS;
3445 + swdev->vlans = MT7530_NUM_VLANS;
3446 + swdev->ops = &mt7530_ops;
3448 + ret = register_switch(swdev, NULL);
3450 + dev_err(dev, "failed to register mt7530\n");
3455 + map = mt7530_find_mapping(dev->of_node);
3457 + mt7530_apply_mapping(mt7530, map);
3458 + mt7530_apply_config(swdev);
3461 + if (bus && mt7530_r32(mt7530, REG_HWTRAP) != 0x1117edf) {
3462 + dev_info(dev, "fixing up MHWTRAP register - bootloader probably played with it\n");
3463 + mt7530_w32(mt7530, REG_HWTRAP, 0x1117edf);
3465 + dev_info(dev, "loaded %s driver\n", swdev->name);
3469 Index: linux-3.14.18/drivers/net/ethernet/ralink/mt7530.h
3470 ===================================================================
3471 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3472 +++ linux-3.14.18/drivers/net/ethernet/ralink/mt7530.h 2014-10-29 20:30:49.296185172 +0100
3475 + * This program is free software; you can redistribute it and/or
3476 + * modify it under the terms of the GNU General Public License
3477 + * as published by the Free Software Foundation; either version 2
3478 + * of the License, or (at your option) any later version.
3480 + * This program is distributed in the hope that it will be useful,
3481 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3482 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3483 + * GNU General Public License for more details.
3485 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
3488 +#ifndef _MT7530_H__
3489 +#define _MT7530_H__
3491 +int mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus, int vlan);
3494 Index: linux-3.14.18/drivers/net/ethernet/ralink/ralink_soc_eth.c
3495 ===================================================================
3496 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3497 +++ linux-3.14.18/drivers/net/ethernet/ralink/ralink_soc_eth.c 2014-10-30 07:59:41.679492638 +0100
3500 + * This program is free software; you can redistribute it and/or modify
3501 + * it under the terms of the GNU General Public License as published by
3502 + * the Free Software Foundation; version 2 of the License
3504 + * This program is distributed in the hope that it will be useful,
3505 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3506 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3507 + * GNU General Public License for more details.
3509 + * You should have received a copy of the GNU General Public License
3510 + * along with this program; if not, write to the Free Software
3511 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3513 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3516 +#include <linux/module.h>
3517 +#include <linux/kernel.h>
3518 +#include <linux/types.h>
3519 +#include <linux/dma-mapping.h>
3520 +#include <linux/init.h>
3521 +#include <linux/skbuff.h>
3522 +#include <linux/etherdevice.h>
3523 +#include <linux/ethtool.h>
3524 +#include <linux/platform_device.h>
3525 +#include <linux/of_device.h>
3526 +#include <linux/clk.h>
3527 +#include <linux/of_net.h>
3528 +#include <linux/of_mdio.h>
3529 +#include <linux/if_vlan.h>
3530 +#include <linux/reset.h>
3531 +#include <linux/tcp.h>
3532 +#include <linux/io.h>
3534 +#include <asm/mach-ralink/ralink_regs.h>
3536 +#include "ralink_soc_eth.h"
3537 +#include "esw_rt3052.h"
3539 +#include "ralink_ethtool.h"
3541 +#define TX_TIMEOUT (2 * HZ)
3542 +#define MAX_RX_LENGTH 1536
3543 +#define FE_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
3544 +#define FE_RX_HLEN (FE_RX_OFFSET + VLAN_ETH_HLEN + VLAN_HLEN + \
3546 +#define DMA_DUMMY_DESC 0xffffffff
3547 +#define FE_DEFAULT_MSG_ENABLE \
3548 + (NETIF_MSG_DRV | \
3549 + NETIF_MSG_PROBE | \
3550 + NETIF_MSG_LINK | \
3551 + NETIF_MSG_TIMER | \
3552 + NETIF_MSG_IFDOWN | \
3553 + NETIF_MSG_IFUP | \
3554 + NETIF_MSG_RX_ERR | \
3557 +#define TX_DMA_DESP2_DEF (TX_DMA_LS0 | TX_DMA_DONE)
3558 +#define TX_DMA_DESP4_DEF (TX_DMA_QN(3) | TX_DMA_PN(1))
3559 +#define NEXT_TX_DESP_IDX(X) (((X) + 1) & (NUM_DMA_DESC - 1))
3560 +#define NEXT_RX_DESP_IDX(X) (((X) + 1) & (NUM_DMA_DESC - 1))
3562 +static int fe_msg_level = -1;
3563 +module_param_named(msg_level, fe_msg_level, int, 0);
3564 +MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
3566 +static const u32 fe_reg_table_default[FE_REG_COUNT] = {
3567 + [FE_REG_PDMA_GLO_CFG] = FE_PDMA_GLO_CFG,
3568 + [FE_REG_PDMA_RST_CFG] = FE_PDMA_RST_CFG,
3569 + [FE_REG_DLY_INT_CFG] = FE_DLY_INT_CFG,
3570 + [FE_REG_TX_BASE_PTR0] = FE_TX_BASE_PTR0,
3571 + [FE_REG_TX_MAX_CNT0] = FE_TX_MAX_CNT0,
3572 + [FE_REG_TX_CTX_IDX0] = FE_TX_CTX_IDX0,
3573 + [FE_REG_RX_BASE_PTR0] = FE_RX_BASE_PTR0,
3574 + [FE_REG_RX_MAX_CNT0] = FE_RX_MAX_CNT0,
3575 + [FE_REG_RX_CALC_IDX0] = FE_RX_CALC_IDX0,
3576 + [FE_REG_FE_INT_ENABLE] = FE_FE_INT_ENABLE,
3577 + [FE_REG_FE_INT_STATUS] = FE_FE_INT_STATUS,
3578 + [FE_REG_FE_DMA_VID_BASE] = FE_DMA_VID0,
3579 + [FE_REG_FE_COUNTER_BASE] = FE_GDMA1_TX_GBCNT,
3582 +static const u32 *fe_reg_table = fe_reg_table_default;
3584 +static void __iomem *fe_base = 0;
3586 +void fe_w32(u32 val, unsigned reg)
3588 + __raw_writel(val, fe_base + reg);
3591 +u32 fe_r32(unsigned reg)
3593 + return __raw_readl(fe_base + reg);
3596 +void fe_reg_w32(u32 val, enum fe_reg reg)
3598 + fe_w32(val, fe_reg_table[reg]);
3601 +u32 fe_reg_r32(enum fe_reg reg)
3603 + return fe_r32(fe_reg_table[reg]);
3606 +static inline void fe_int_disable(u32 mask)
3608 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) & ~mask,
3609 + FE_REG_FE_INT_ENABLE);
3611 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3614 +static inline void fe_int_enable(u32 mask)
3616 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) | mask,
3617 + FE_REG_FE_INT_ENABLE);
3619 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3622 +static inline void fe_hw_set_macaddr(struct fe_priv *priv, unsigned char *mac)
3624 + unsigned long flags;
3626 + spin_lock_irqsave(&priv->page_lock, flags);
3627 + fe_w32((mac[0] << 8) | mac[1], FE_GDMA1_MAC_ADRH);
3628 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
3629 + FE_GDMA1_MAC_ADRL);
3630 + spin_unlock_irqrestore(&priv->page_lock, flags);
3633 +static int fe_set_mac_address(struct net_device *dev, void *p)
3635 + int ret = eth_mac_addr(dev, p);
3638 + struct fe_priv *priv = netdev_priv(dev);
3640 + if (priv->soc->set_mac)
3641 + priv->soc->set_mac(priv, dev->dev_addr);
3643 + fe_hw_set_macaddr(priv, p);
3649 +static inline int fe_max_frag_size(int mtu)
3651 + return SKB_DATA_ALIGN(FE_RX_HLEN + mtu) +
3652 + SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3655 +static inline int fe_max_buf_size(int frag_size)
3657 + return frag_size - FE_RX_HLEN -
3658 + SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3661 +static void fe_clean_rx(struct fe_priv *priv)
3665 + if (priv->rx_data) {
3666 + for (i = 0; i < NUM_DMA_DESC; i++)
3667 + if (priv->rx_data[i]) {
3668 + if (priv->rx_dma && priv->rx_dma[i].rxd1)
3669 + dma_unmap_single(&priv->netdev->dev,
3670 + priv->rx_dma[i].rxd1,
3671 + priv->rx_buf_size,
3673 + put_page(virt_to_head_page(priv->rx_data[i]));
3676 + kfree(priv->rx_data);
3677 + priv->rx_data = NULL;
3680 + if (priv->rx_dma) {
3681 + dma_free_coherent(&priv->netdev->dev,
3682 + NUM_DMA_DESC * sizeof(*priv->rx_dma),
3685 + priv->rx_dma = NULL;
3689 +static int fe_alloc_rx(struct fe_priv *priv)
3691 + struct net_device *netdev = priv->netdev;
3694 + priv->rx_data = kcalloc(NUM_DMA_DESC, sizeof(*priv->rx_data),
3696 + if (!priv->rx_data)
3699 + for (i = 0; i < NUM_DMA_DESC; i++) {
3700 + priv->rx_data[i] = netdev_alloc_frag(priv->frag_size);
3701 + if (!priv->rx_data[i])
3705 + priv->rx_dma = dma_alloc_coherent(&netdev->dev,
3706 + NUM_DMA_DESC * sizeof(*priv->rx_dma),
3708 + GFP_ATOMIC | __GFP_ZERO);
3709 + if (!priv->rx_dma)
3712 + for (i = 0; i < NUM_DMA_DESC; i++) {
3713 + dma_addr_t dma_addr = dma_map_single(&netdev->dev,
3714 + priv->rx_data[i] + FE_RX_OFFSET,
3715 + priv->rx_buf_size,
3717 + if (unlikely(dma_mapping_error(&netdev->dev, dma_addr)))
3719 + priv->rx_dma[i].rxd1 = (unsigned int) dma_addr;
3721 + if (priv->soc->rx_dma)
3722 + priv->soc->rx_dma(priv, i, priv->rx_buf_size);
3724 + priv->rx_dma[i].rxd2 = RX_DMA_LSO;
3728 + fe_reg_w32(priv->rx_phys, FE_REG_RX_BASE_PTR0);
3729 + fe_reg_w32(NUM_DMA_DESC, FE_REG_RX_MAX_CNT0);
3730 + fe_reg_w32((NUM_DMA_DESC - 1), FE_REG_RX_CALC_IDX0);
3731 + fe_reg_w32(FE_PST_DRX_IDX0, FE_REG_PDMA_RST_CFG);
3739 +static void fe_clean_tx(struct fe_priv *priv)
3743 + if (priv->tx_skb) {
3744 + for (i = 0; i < NUM_DMA_DESC; i++) {
3745 + if (priv->tx_skb[i])
3746 + dev_kfree_skb_any(priv->tx_skb[i]);
3748 + kfree(priv->tx_skb);
3749 + priv->tx_skb = NULL;
3752 + if (priv->tx_dma) {
3753 + dma_free_coherent(&priv->netdev->dev,
3754 + NUM_DMA_DESC * sizeof(*priv->tx_dma),
3757 + priv->tx_dma = NULL;
3761 +static int fe_alloc_tx(struct fe_priv *priv)
3765 + priv->tx_free_idx = 0;
3767 + priv->tx_skb = kcalloc(NUM_DMA_DESC, sizeof(*priv->tx_skb),
3769 + if (!priv->tx_skb)
3772 + priv->tx_dma = dma_alloc_coherent(&priv->netdev->dev,
3773 + NUM_DMA_DESC * sizeof(*priv->tx_dma),
3775 + GFP_ATOMIC | __GFP_ZERO);
3776 + if (!priv->tx_dma)
3779 + for (i = 0; i < NUM_DMA_DESC; i++) {
3780 + if (priv->soc->tx_dma) {
3781 + priv->soc->tx_dma(priv, i, NULL);
3784 + priv->tx_dma[i].txd2 = TX_DMA_DESP2_DEF;
3788 + fe_reg_w32(priv->tx_phys, FE_REG_TX_BASE_PTR0);
3789 + fe_reg_w32(NUM_DMA_DESC, FE_REG_TX_MAX_CNT0);
3790 + fe_reg_w32(0, FE_REG_TX_CTX_IDX0);
3791 + fe_reg_w32(FE_PST_DTX_IDX0, FE_REG_PDMA_RST_CFG);
3799 +static int fe_init_dma(struct fe_priv *priv)
3803 + err = fe_alloc_tx(priv);
3807 + err = fe_alloc_rx(priv);
3814 +static void fe_free_dma(struct fe_priv *priv)
3816 + fe_clean_tx(priv);
3817 + fe_clean_rx(priv);
3819 + netdev_reset_queue(priv->netdev);
3822 +static inline void txd_unmap_single(struct device *dev, struct fe_tx_dma *txd)
3824 + if (txd->txd1 && TX_DMA_GET_PLEN0(txd->txd2))
3825 + dma_unmap_single(dev, txd->txd1,
3826 + TX_DMA_GET_PLEN0(txd->txd2),
3830 +static inline void txd_unmap_page0(struct device *dev, struct fe_tx_dma *txd)
3832 + if (txd->txd1 && TX_DMA_GET_PLEN0(txd->txd2))
3833 + dma_unmap_page(dev, txd->txd1,
3834 + TX_DMA_GET_PLEN0(txd->txd2),
3838 +static inline void txd_unmap_page1(struct device *dev, struct fe_tx_dma *txd)
3840 + if (txd->txd3 && TX_DMA_GET_PLEN1(txd->txd2))
3841 + dma_unmap_page(dev, txd->txd3,
3842 + TX_DMA_GET_PLEN1(txd->txd2),
3846 +void fe_stats_update(struct fe_priv *priv)
3848 + struct fe_hw_stats *hwstats = priv->hw_stats;
3849 + unsigned int base = fe_reg_table[FE_REG_FE_COUNTER_BASE];
3851 + u64_stats_update_begin(&hwstats->syncp);
3853 + hwstats->tx_bytes += fe_r32(base);
3854 + hwstats->tx_packets += fe_r32(base + 0x04);
3855 + hwstats->tx_skip += fe_r32(base + 0x08);
3856 + hwstats->tx_collisions += fe_r32(base + 0x0c);
3857 + hwstats->rx_bytes += fe_r32(base + 0x20);
3858 + hwstats->rx_packets += fe_r32(base + 0x24);
3859 + hwstats->rx_overflow += fe_r32(base + 0x28);
3860 + hwstats->rx_fcs_errors += fe_r32(base + 0x2c);
3861 + hwstats->rx_short_errors += fe_r32(base + 0x30);
3862 + hwstats->rx_long_errors += fe_r32(base + 0x34);
3863 + hwstats->rx_checksum_errors += fe_r32(base + 0x38);
3864 + hwstats->rx_flow_control_packets += fe_r32(base + 0x3c);
3866 + u64_stats_update_end(&hwstats->syncp);
3869 +static struct rtnl_link_stats64 *fe_get_stats64(struct net_device *dev,
3870 + struct rtnl_link_stats64 *storage)
3872 + struct fe_priv *priv = netdev_priv(dev);
3873 + struct fe_hw_stats *hwstats = priv->hw_stats;
3874 + unsigned int base = fe_reg_table[FE_REG_FE_COUNTER_BASE];
3875 + unsigned int start;
3878 + netdev_stats_to_stats64(storage, &dev->stats);
3882 + if (netif_running(dev) && netif_device_present(dev)) {
3883 + if (spin_trylock(&hwstats->stats_lock)) {
3884 + fe_stats_update(priv);
3885 + spin_unlock(&hwstats->stats_lock);
3890 + start = u64_stats_fetch_begin_bh(&hwstats->syncp);
3891 + storage->rx_packets = hwstats->rx_packets;
3892 + storage->tx_packets = hwstats->tx_packets;
3893 + storage->rx_bytes = hwstats->rx_bytes;
3894 + storage->tx_bytes = hwstats->tx_bytes;
3895 + storage->collisions = hwstats->tx_collisions;
3896 + storage->rx_length_errors = hwstats->rx_short_errors +
3897 + hwstats->rx_long_errors;
3898 + storage->rx_over_errors = hwstats->rx_overflow;
3899 + storage->rx_crc_errors = hwstats->rx_fcs_errors;
3900 + storage->rx_errors = hwstats->rx_checksum_errors;
3901 + storage->tx_aborted_errors = hwstats->tx_skip;
3902 + } while (u64_stats_fetch_retry_bh(&hwstats->syncp, start));
3904 + storage->tx_errors = priv->netdev->stats.tx_errors;
3905 + storage->rx_dropped = priv->netdev->stats.rx_dropped;
3906 + storage->tx_dropped = priv->netdev->stats.tx_dropped;
3911 +static int fe_tx_map_dma(struct sk_buff *skb, struct net_device *dev,
3914 + struct fe_priv *priv = netdev_priv(dev);
3915 + struct skb_frag_struct *frag;
3916 + struct fe_tx_dma *txd;
3917 + dma_addr_t mapped_addr;
3918 + unsigned int nr_frags;
3919 + u32 def_txd4, txd2;
3920 + int i, j, unmap_idx, tx_num;
3922 + txd = &priv->tx_dma[idx];
3923 + nr_frags = skb_shinfo(skb)->nr_frags;
3924 + tx_num = 1 + (nr_frags >> 1);
3926 + /* init tx descriptor */
3927 + if (priv->soc->tx_dma)
3928 + priv->soc->tx_dma(priv, idx, skb);
3930 + txd->txd4 = TX_DMA_DESP4_DEF;
3931 + def_txd4 = txd->txd4;
3933 + /* use dma_unmap_single to free it */
3934 + txd->txd4 |= priv->soc->tx_udf_bit;
3936 + /* TX Checksum offload */
3937 + if (skb->ip_summed == CHECKSUM_PARTIAL)
3938 + txd->txd4 |= TX_DMA_CHKSUM;
3940 + /* VLAN header offload */
3941 + if (vlan_tx_tag_present(skb)) {
3942 + txd->txd4 |= TX_DMA_INS_VLAN |
3943 + ((vlan_tx_tag_get(skb) >> VLAN_PRIO_SHIFT) << 4) |
3944 + (vlan_tx_tag_get(skb) & 0xF);
3947 + /* TSO: fill MSS info in tcp checksum field */
3948 + if (skb_is_gso(skb)) {
3949 + if (skb_cow_head(skb, 0)) {
3950 + netif_warn(priv, tx_err, dev,
3951 + "GSO expand head fail.\n");
3954 + if (skb_shinfo(skb)->gso_type &
3955 + (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
3956 + txd->txd4 |= TX_DMA_TSO;
3957 + tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
3961 + mapped_addr = dma_map_single(&dev->dev, skb->data,
3962 + skb_headlen(skb), DMA_TO_DEVICE);
3963 + if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
3965 + txd->txd1 = mapped_addr;
3966 + txd2 = TX_DMA_PLEN0(skb_headlen(skb));
3968 + /* TX SG offload */
3970 + for (i = 0; i < nr_frags; i++) {
3972 + frag = &skb_shinfo(skb)->frags[i];
3973 + mapped_addr = skb_frag_dma_map(&dev->dev, frag, 0,
3974 + skb_frag_size(frag), DMA_TO_DEVICE);
3975 + if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
3979 + j = NEXT_TX_DESP_IDX(j);
3980 + txd = &priv->tx_dma[j];
3981 + txd->txd1 = mapped_addr;
3982 + txd2 = TX_DMA_PLEN0(frag->size);
3983 + txd->txd4 = def_txd4;
3985 + txd->txd3 = mapped_addr;
3986 + txd2 |= TX_DMA_PLEN1(frag->size);
3987 + if (i != (nr_frags -1))
3989 + priv->tx_skb[j] = (struct sk_buff *) DMA_DUMMY_DESC;
3993 + /* set last segment */
3994 + if (nr_frags & 0x1)
3995 + txd->txd2 = (txd2 | TX_DMA_LS1);
3997 + txd->txd2 = (txd2 | TX_DMA_LS0);
3999 + /* store skb to cleanup */
4000 + priv->tx_skb[j] = skb;
4003 + j = NEXT_TX_DESP_IDX(j);
4004 + fe_reg_w32(j, FE_REG_TX_CTX_IDX0);
4010 + txd = &priv->tx_dma[idx];
4011 + txd_unmap_single(&dev->dev, txd);
4015 + for (i = 0; i < unmap_idx; i++) {
4017 + j = NEXT_TX_DESP_IDX(j);
4018 + txd = &priv->tx_dma[j];
4019 + txd_unmap_page0(&dev->dev, txd);
4021 + txd_unmap_page1(&dev->dev, txd);
4026 + /* reinit descriptors and skb */
4028 + for (i = 0; i < tx_num; i++) {
4029 + priv->tx_dma[j].txd2 = TX_DMA_DESP2_DEF;
4030 + priv->tx_skb[j] = NULL;
4031 + j = NEXT_TX_DESP_IDX(j);
4038 +static inline int fe_skb_padto(struct sk_buff *skb, struct fe_priv *priv) {
4043 + if (unlikely(skb->len < VLAN_ETH_ZLEN)) {
4044 + if ((priv->flags & FE_FLAG_PADDING_64B) &&
4045 + !(priv->flags & FE_FLAG_PADDING_BUG))
4048 + if (vlan_tx_tag_present(skb))
4050 + else if (skb->protocol == cpu_to_be16(ETH_P_8021Q))
4051 + len = VLAN_ETH_ZLEN;
4052 + else if(!(priv->flags & FE_FLAG_PADDING_64B))
4057 + if (skb->len < len) {
4058 + if ((ret = skb_pad(skb, len - skb->len)) < 0)
4061 + skb_set_tail_pointer(skb, len);
4068 +static inline u32 fe_empty_txd(struct fe_priv *priv, u32 tx_fill_idx)
4070 + return (u32)(NUM_DMA_DESC - ((tx_fill_idx - priv->tx_free_idx) &
4071 + (NUM_DMA_DESC - 1)));
4074 +static int fe_start_xmit(struct sk_buff *skb, struct net_device *dev)
4076 + struct fe_priv *priv = netdev_priv(dev);
4077 + struct net_device_stats *stats = &dev->stats;
4081 + if (fe_skb_padto(skb, priv)) {
4082 + netif_warn(priv, tx_err, dev, "tx padding failed!\n");
4083 + return NETDEV_TX_OK;
4086 + spin_lock(&priv->page_lock);
4087 + tx_num = 1 + (skb_shinfo(skb)->nr_frags >> 1);
4088 + tx = fe_reg_r32(FE_REG_TX_CTX_IDX0);
4089 + if (unlikely(fe_empty_txd(priv, tx) <= tx_num))
4091 + netif_stop_queue(dev);
4092 + spin_unlock(&priv->page_lock);
4093 + netif_err(priv, tx_queued,dev,
4094 + "Tx Ring full when queue awake!\n");
4095 + return NETDEV_TX_BUSY;
4098 + if (fe_tx_map_dma(skb, dev, tx) < 0) {
4101 + stats->tx_dropped++;
4103 + netdev_sent_queue(dev, skb->len);
4104 + skb_tx_timestamp(skb);
4106 + stats->tx_packets++;
4107 + stats->tx_bytes += skb->len;
4110 + spin_unlock(&priv->page_lock);
4112 + return NETDEV_TX_OK;
4115 +static inline void fe_rx_vlan(struct sk_buff *skb)
4117 + struct ethhdr *ehdr;
4120 + if (!__vlan_get_tag(skb, &vlanid)) {
4121 + /* pop the vlan tag */
4122 + ehdr = (struct ethhdr *)skb->data;
4123 + memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
4124 + skb_pull(skb, VLAN_HLEN);
4125 + __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
4129 +static int fe_poll_rx(struct napi_struct *napi, int budget,
4130 + struct fe_priv *priv)
4132 + struct net_device *netdev = priv->netdev;
4133 + struct net_device_stats *stats = &netdev->stats;
4134 + struct fe_soc_data *soc = priv->soc;
4136 + int idx = fe_reg_r32(FE_REG_RX_CALC_IDX0);
4137 + struct sk_buff *skb;
4138 + u8 *data, *new_data;
4139 + struct fe_rx_dma *rxd;
4141 + bool rx_vlan = netdev->features & NETIF_F_HW_VLAN_CTAG_RX;
4143 + if (netdev->features & NETIF_F_RXCSUM)
4144 + checksum_bit = soc->checksum_bit;
4148 + while (done < budget) {
4149 + unsigned int pktlen;
4150 + dma_addr_t dma_addr;
4151 + idx = NEXT_RX_DESP_IDX(idx);
4152 + rxd = &priv->rx_dma[idx];
4153 + data = priv->rx_data[idx];
4155 + if (!(rxd->rxd2 & RX_DMA_DONE))
4158 + /* alloc new buffer */
4159 + new_data = netdev_alloc_frag(priv->frag_size);
4160 + if (unlikely(!new_data)) {
4161 + stats->rx_dropped++;
4162 + goto release_desc;
4164 + dma_addr = dma_map_single(&netdev->dev,
4165 + new_data + FE_RX_OFFSET,
4166 + priv->rx_buf_size,
4168 + if (unlikely(dma_mapping_error(&netdev->dev, dma_addr))) {
4169 + put_page(virt_to_head_page(new_data));
4170 + goto release_desc;
4173 + /* receive data */
4174 + skb = build_skb(data, priv->frag_size);
4175 + if (unlikely(!skb)) {
4176 + put_page(virt_to_head_page(new_data));
4177 + goto release_desc;
4179 + skb_reserve(skb, FE_RX_OFFSET);
4181 + dma_unmap_single(&netdev->dev, rxd->rxd1,
4182 + priv->rx_buf_size, DMA_FROM_DEVICE);
4183 + pktlen = RX_DMA_PLEN0(rxd->rxd2);
4184 + skb_put(skb, pktlen);
4185 + skb->dev = netdev;
4186 + if (rxd->rxd4 & checksum_bit) {
4187 + skb->ip_summed = CHECKSUM_UNNECESSARY;
4189 + skb_checksum_none_assert(skb);
4193 + skb->protocol = eth_type_trans(skb, netdev);
4195 + stats->rx_packets++;
4196 + stats->rx_bytes += pktlen;
4198 + napi_gro_receive(napi, skb);
4200 + priv->rx_data[idx] = new_data;
4201 + rxd->rxd1 = (unsigned int) dma_addr;
4205 + soc->rx_dma(priv, idx, priv->rx_buf_size);
4207 + rxd->rxd2 = RX_DMA_LSO;
4210 + fe_reg_w32(idx, FE_REG_RX_CALC_IDX0);
4217 +static int fe_poll_tx(struct fe_priv *priv, int budget)
4219 + struct net_device *netdev = priv->netdev;
4220 + struct device *dev = &netdev->dev;
4221 + unsigned int bytes_compl = 0;
4222 + struct sk_buff *skb;
4223 + struct fe_tx_dma *txd;
4224 + int done = 0, idx;
4225 + u32 udf_bit = priv->soc->tx_udf_bit;
4227 + idx = priv->tx_free_idx;
4228 + while (done < budget) {
4229 + txd = &priv->tx_dma[idx];
4230 + skb = priv->tx_skb[idx];
4232 + if (!(txd->txd2 & TX_DMA_DONE) || !skb)
4235 + txd_unmap_page1(dev, txd);
4237 + if (txd->txd4 & udf_bit)
4238 + txd_unmap_single(dev, txd);
4240 + txd_unmap_page0(dev, txd);
4242 + if (skb != (struct sk_buff *) DMA_DUMMY_DESC) {
4243 + bytes_compl += skb->len;
4244 + dev_kfree_skb_any(skb);
4247 + priv->tx_skb[idx] = NULL;
4248 + idx = NEXT_TX_DESP_IDX(idx);
4250 + priv->tx_free_idx = idx;
4255 + netdev_completed_queue(netdev, done, bytes_compl);
4256 + if (unlikely(netif_queue_stopped(netdev) &&
4257 + netif_carrier_ok(netdev))) {
4258 + netif_wake_queue(netdev);
4264 +static int fe_poll(struct napi_struct *napi, int budget)
4266 + struct fe_priv *priv = container_of(napi, struct fe_priv, rx_napi);
4267 + struct fe_hw_stats *hwstat = priv->hw_stats;
4268 + int tx_done, rx_done;
4270 + u32 tx_intr, rx_intr;
4272 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
4273 + tx_intr = priv->soc->tx_dly_int;
4274 + rx_intr = priv->soc->rx_dly_int;
4275 + tx_done = rx_done = 0;
4278 + if (status & tx_intr) {
4279 + tx_done += fe_poll_tx(priv, budget - tx_done);
4280 + if (tx_done < budget) {
4281 + fe_reg_w32(tx_intr, FE_REG_FE_INT_STATUS);
4283 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
4286 + if (status & rx_intr) {
4287 + rx_done += fe_poll_rx(napi, budget - rx_done, priv);
4288 + if (rx_done < budget) {
4289 + fe_reg_w32(rx_intr, FE_REG_FE_INT_STATUS);
4293 + if (unlikely(hwstat && (status & FE_CNT_GDM_AF))) {
4294 + if (spin_trylock(&hwstat->stats_lock)) {
4295 + fe_stats_update(priv);
4296 + spin_unlock(&hwstat->stats_lock);
4298 + fe_reg_w32(FE_CNT_GDM_AF, FE_REG_FE_INT_STATUS);
4301 + if (unlikely(netif_msg_intr(priv))) {
4302 + mask = fe_reg_r32(FE_REG_FE_INT_ENABLE);
4303 + netdev_info(priv->netdev,
4304 + "done tx %d, rx %d, intr 0x%x/0x%x\n",
4305 + tx_done, rx_done, status, mask);
4308 + if ((tx_done < budget) && (rx_done < budget)) {
4309 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
4310 + if (status & (tx_intr | rx_intr )) {
4313 + napi_complete(napi);
4314 + fe_int_enable(tx_intr | rx_intr);
4320 +static void fe_tx_timeout(struct net_device *dev)
4322 + struct fe_priv *priv = netdev_priv(dev);
4324 + priv->netdev->stats.tx_errors++;
4325 + netif_err(priv, tx_err, dev,
4326 + "transmit timed out, waking up the queue\n");
4327 + netif_info(priv, drv, dev, ": dma_cfg:%08x, free_idx:%d, " \
4328 + "dma_ctx_idx=%u, dma_crx_idx=%u\n",
4329 + fe_reg_r32(FE_REG_PDMA_GLO_CFG), priv->tx_free_idx,
4330 + fe_reg_r32(FE_REG_TX_CTX_IDX0),
4331 + fe_reg_r32(FE_REG_RX_CALC_IDX0));
4332 + netif_wake_queue(dev);
4335 +static irqreturn_t fe_handle_irq(int irq, void *dev)
4337 + struct fe_priv *priv = netdev_priv(dev);
4338 + u32 status, dly_int;
4340 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
4342 + if (unlikely(!status))
4345 + dly_int = (priv->soc->rx_dly_int | priv->soc->tx_dly_int);
4346 + if (likely(status & dly_int)) {
4347 + fe_int_disable(dly_int);
4348 + napi_schedule(&priv->rx_napi);
4350 + fe_reg_w32(status, FE_REG_FE_INT_STATUS);
4353 + return IRQ_HANDLED;
4356 +int fe_set_clock_cycle(struct fe_priv *priv)
4358 + unsigned long sysclk = priv->sysclk;
4364 + sysclk /= FE_US_CYC_CNT_DIVISOR;
4365 + sysclk <<= FE_US_CYC_CNT_SHIFT;
4367 + fe_w32((fe_r32(FE_FE_GLO_CFG) &
4368 + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) |
4374 +void fe_fwd_config(struct fe_priv *priv)
4378 + fwd_cfg = fe_r32(FE_GDMA1_FWD_CFG);
4380 + /* disable jumbo frame */
4381 + if (priv->flags & FE_FLAG_JUMBO_FRAME)
4382 + fwd_cfg &= ~FE_GDM1_JMB_EN;
4384 + /* set unicast/multicast/broadcast frame to cpu */
4385 + fwd_cfg &= ~0xffff;
4387 + fe_w32(fwd_cfg, FE_GDMA1_FWD_CFG);
4390 +static void fe_rxcsum_config(bool enable)
4393 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) | (FE_GDM1_ICS_EN |
4394 + FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
4395 + FE_GDMA1_FWD_CFG);
4397 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) & ~(FE_GDM1_ICS_EN |
4398 + FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
4399 + FE_GDMA1_FWD_CFG);
4402 +static void fe_txcsum_config(bool enable)
4405 + fe_w32(fe_r32(FE_CDMA_CSG_CFG) | (FE_ICS_GEN_EN |
4406 + FE_TCS_GEN_EN | FE_UCS_GEN_EN),
4409 + fe_w32(fe_r32(FE_CDMA_CSG_CFG) & ~(FE_ICS_GEN_EN |
4410 + FE_TCS_GEN_EN | FE_UCS_GEN_EN),
4414 +void fe_csum_config(struct fe_priv *priv)
4416 + struct net_device *dev = priv_netdev(priv);
4418 + fe_txcsum_config((dev->features & NETIF_F_IP_CSUM));
4419 + fe_rxcsum_config((dev->features & NETIF_F_RXCSUM));
4422 +static int fe_hw_init(struct net_device *dev)
4424 + struct fe_priv *priv = netdev_priv(dev);
4427 + err = devm_request_irq(priv->device, dev->irq, fe_handle_irq, 0,
4428 + dev_name(priv->device), dev);
4432 + if (priv->soc->set_mac)
4433 + priv->soc->set_mac(priv, dev->dev_addr);
4435 + fe_hw_set_macaddr(priv, dev->dev_addr);
4437 + fe_reg_w32(FE_DELAY_INIT, FE_REG_DLY_INT_CFG);
4439 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4441 + /* frame engine will push VLAN tag regarding to VIDX feild in Tx desc. */
4442 + if (fe_reg_table[FE_REG_FE_DMA_VID_BASE])
4443 + for (i = 0; i < 16; i += 2)
4444 + fe_w32(((i + 1) << 16) + i,
4445 + fe_reg_table[FE_REG_FE_DMA_VID_BASE] +
4448 + BUG_ON(!priv->soc->fwd_config);
4449 + if (priv->soc->fwd_config(priv))
4450 + netdev_err(dev, "unable to get clock\n");
4452 + fe_w32(1, FE_FE_RST_GL);
4453 + fe_w32(0, FE_FE_RST_GL);
4458 +static int fe_open(struct net_device *dev)
4460 + struct fe_priv *priv = netdev_priv(dev);
4461 + unsigned long flags;
4465 + err = fe_init_dma(priv);
4469 + spin_lock_irqsave(&priv->page_lock, flags);
4470 + napi_enable(&priv->rx_napi);
4472 + val = FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
4473 + val |= priv->soc->pdma_glo_cfg;
4474 + fe_reg_w32(val, FE_REG_PDMA_GLO_CFG);
4476 + spin_unlock_irqrestore(&priv->page_lock, flags);
4479 + priv->phy->start(priv);
4481 + if (priv->soc->has_carrier && priv->soc->has_carrier(priv))
4482 + netif_carrier_on(dev);
4484 + netif_start_queue(dev);
4485 + fe_int_enable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4490 + fe_free_dma(priv);
4494 +static int fe_stop(struct net_device *dev)
4496 + struct fe_priv *priv = netdev_priv(dev);
4497 + unsigned long flags;
4500 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4502 + netif_tx_disable(dev);
4505 + priv->phy->stop(priv);
4507 + spin_lock_irqsave(&priv->page_lock, flags);
4508 + napi_disable(&priv->rx_napi);
4510 + fe_reg_w32(fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
4511 + ~(FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN),
4512 + FE_REG_PDMA_GLO_CFG);
4513 + spin_unlock_irqrestore(&priv->page_lock, flags);
4515 + /* wait dma stop */
4516 + for (i = 0; i < 10; i++) {
4517 + if (fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
4518 + (FE_TX_DMA_BUSY | FE_RX_DMA_BUSY)) {
4525 + fe_free_dma(priv);
4530 +static int __init fe_init(struct net_device *dev)
4532 + struct fe_priv *priv = netdev_priv(dev);
4533 + struct device_node *port;
4536 + BUG_ON(!priv->soc->reset_fe);
4537 + priv->soc->reset_fe();
4539 + if (priv->soc->switch_init)
4540 + priv->soc->switch_init(priv);
4542 + memcpy(dev->dev_addr, priv->soc->mac, ETH_ALEN);
4543 + of_get_mac_address_mtd(priv->device->of_node, dev->dev_addr);
4545 + err = fe_mdio_init(priv);
4549 + if (priv->soc->port_init)
4550 + for_each_child_of_node(priv->device->of_node, port)
4551 + if (of_device_is_compatible(port, "ralink,eth-port") && of_device_is_available(port))
4552 + priv->soc->port_init(priv, port);
4555 + err = priv->phy->connect(priv);
4557 + goto err_phy_disconnect;
4560 + err = fe_hw_init(dev);
4562 + goto err_phy_disconnect;
4564 + if (priv->soc->switch_config)
4565 + priv->soc->switch_config(priv);
4569 +err_phy_disconnect:
4571 + priv->phy->disconnect(priv);
4572 + fe_mdio_cleanup(priv);
4577 +static void fe_uninit(struct net_device *dev)
4579 + struct fe_priv *priv = netdev_priv(dev);
4582 + priv->phy->disconnect(priv);
4583 + fe_mdio_cleanup(priv);
4585 + fe_reg_w32(0, FE_REG_FE_INT_ENABLE);
4586 + free_irq(dev->irq, dev);
4589 +static int fe_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4591 + struct fe_priv *priv = netdev_priv(dev);
4593 + if (!priv->phy_dev)
4598 + return phy_ethtool_ioctl(priv->phy_dev,
4599 + (void *) ifr->ifr_data);
4603 + return phy_mii_ioctl(priv->phy_dev, ifr, cmd);
4608 + return -EOPNOTSUPP;
4611 +static int fe_change_mtu(struct net_device *dev, int new_mtu)
4613 + struct fe_priv *priv = netdev_priv(dev);
4614 + int frag_size, old_mtu;
4617 + if (!(priv->flags & FE_FLAG_JUMBO_FRAME))
4618 + return eth_change_mtu(dev, new_mtu);
4620 + frag_size = fe_max_frag_size(new_mtu);
4621 + if (new_mtu < 68 || frag_size > PAGE_SIZE)
4624 + old_mtu = dev->mtu;
4625 + dev->mtu = new_mtu;
4627 + /* return early if the buffer sizes will not change */
4628 + if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
4630 + if (old_mtu > ETH_DATA_LEN && new_mtu > ETH_DATA_LEN)
4633 + if (new_mtu <= ETH_DATA_LEN) {
4634 + priv->frag_size = fe_max_frag_size(ETH_DATA_LEN);
4635 + priv->rx_buf_size = fe_max_buf_size(ETH_DATA_LEN);
4637 + priv->frag_size = PAGE_SIZE;
4638 + priv->rx_buf_size = fe_max_buf_size(PAGE_SIZE);
4641 + if (!netif_running(dev))
4645 + fwd_cfg = fe_r32(FE_GDMA1_FWD_CFG);
4646 + if (new_mtu <= ETH_DATA_LEN)
4647 + fwd_cfg &= ~FE_GDM1_JMB_EN;
4649 + fwd_cfg &= ~(FE_GDM1_JMB_LEN_MASK << FE_GDM1_JMB_LEN_SHIFT);
4650 + fwd_cfg |= (DIV_ROUND_UP(frag_size, 1024) <<
4651 + FE_GDM1_JMB_LEN_SHIFT) | FE_GDM1_JMB_EN;
4653 + fe_w32(fwd_cfg, FE_GDMA1_FWD_CFG);
4655 + return fe_open(dev);
4658 +static const struct net_device_ops fe_netdev_ops = {
4659 + .ndo_init = fe_init,
4660 + .ndo_uninit = fe_uninit,
4661 + .ndo_open = fe_open,
4662 + .ndo_stop = fe_stop,
4663 + .ndo_start_xmit = fe_start_xmit,
4664 + .ndo_set_mac_address = fe_set_mac_address,
4665 + .ndo_validate_addr = eth_validate_addr,
4666 + .ndo_do_ioctl = fe_do_ioctl,
4667 + .ndo_change_mtu = fe_change_mtu,
4668 + .ndo_tx_timeout = fe_tx_timeout,
4669 + .ndo_get_stats64 = fe_get_stats64,
4672 +static int fe_probe(struct platform_device *pdev)
4674 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4675 + const struct of_device_id *match;
4676 + struct fe_soc_data *soc;
4677 + struct net_device *netdev;
4678 + struct fe_priv *priv;
4679 + struct clk *sysclk;
4682 + device_reset(&pdev->dev);
4684 + match = of_match_device(of_fe_match, &pdev->dev);
4685 + soc = (struct fe_soc_data *) match->data;
4687 + if (soc->reg_table)
4688 + fe_reg_table = soc->reg_table;
4690 + soc->reg_table = fe_reg_table;
4692 + fe_base = devm_request_and_ioremap(&pdev->dev, res);
4694 + err = -EADDRNOTAVAIL;
4698 + netdev = alloc_etherdev(sizeof(*priv));
4700 + dev_err(&pdev->dev, "alloc_etherdev failed\n");
4705 + SET_NETDEV_DEV(netdev, &pdev->dev);
4706 + netdev->netdev_ops = &fe_netdev_ops;
4707 + netdev->base_addr = (unsigned long) fe_base;
4708 + netdev->watchdog_timeo = TX_TIMEOUT;
4710 + netdev->irq = platform_get_irq(pdev, 0);
4711 + if (netdev->irq < 0) {
4712 + dev_err(&pdev->dev, "no IRQ resource found\n");
4714 + goto err_free_dev;
4717 + if (soc->init_data)
4718 + soc->init_data(soc, netdev);
4719 + /* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
4720 + netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4721 + netdev->vlan_features = netdev->hw_features &
4722 + ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
4723 + netdev->features |= netdev->hw_features;
4725 + priv = netdev_priv(netdev);
4726 + spin_lock_init(&priv->page_lock);
4727 + if (fe_reg_table[FE_REG_FE_COUNTER_BASE]) {
4728 + priv->hw_stats = kzalloc(sizeof(*priv->hw_stats), GFP_KERNEL);
4729 + if (!priv->hw_stats) {
4731 + goto err_free_dev;
4733 + spin_lock_init(&priv->hw_stats->stats_lock);
4736 + sysclk = devm_clk_get(&pdev->dev, NULL);
4737 + if (!IS_ERR(sysclk))
4738 + priv->sysclk = clk_get_rate(sysclk);
4740 + priv->netdev = netdev;
4741 + priv->device = &pdev->dev;
4743 + priv->msg_enable = netif_msg_init(fe_msg_level, FE_DEFAULT_MSG_ENABLE);
4744 + priv->frag_size = fe_max_frag_size(ETH_DATA_LEN);
4745 + priv->rx_buf_size = fe_max_buf_size(ETH_DATA_LEN);
4746 + if (priv->frag_size > PAGE_SIZE) {
4747 + dev_err(&pdev->dev, "error frag size.\n");
4749 + goto err_free_dev;
4752 + netif_napi_add(netdev, &priv->rx_napi, fe_poll, 32);
4753 + fe_set_ethtool_ops(netdev);
4755 + err = register_netdev(netdev);
4757 + dev_err(&pdev->dev, "error bringing up device\n");
4758 + goto err_free_dev;
4761 + platform_set_drvdata(pdev, netdev);
4763 + netif_info(priv, probe, netdev, "ralink at 0x%08lx, irq %d\n",
4764 + netdev->base_addr, netdev->irq);
4769 + free_netdev(netdev);
4771 + devm_iounmap(&pdev->dev, fe_base);
4776 +static int fe_remove(struct platform_device *pdev)
4778 + struct net_device *dev = platform_get_drvdata(pdev);
4779 + struct fe_priv *priv = netdev_priv(dev);
4781 + netif_napi_del(&priv->rx_napi);
4782 + if (priv->hw_stats)
4783 + kfree(priv->hw_stats);
4785 + unregister_netdev(dev);
4787 + platform_set_drvdata(pdev, NULL);
4792 +static struct platform_driver fe_driver = {
4793 + .probe = fe_probe,
4794 + .remove = fe_remove,
4796 + .name = "ralink_soc_eth",
4797 + .owner = THIS_MODULE,
4798 + .of_match_table = of_fe_match,
4802 +static int __init init_rtfe(void)
4806 + ret = rtesw_init();
4810 + ret = platform_driver_register(&fe_driver);
4817 +static void __exit exit_rtfe(void)
4819 + platform_driver_unregister(&fe_driver);
4823 +module_init(init_rtfe);
4824 +module_exit(exit_rtfe);
4826 +MODULE_LICENSE("GPL");
4827 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
4828 +MODULE_DESCRIPTION("Ethernet driver for Ralink SoC");
4829 +MODULE_VERSION(FE_DRV_VERSION);
4830 Index: linux-3.14.18/drivers/net/ethernet/ralink/ralink_soc_eth.h
4831 ===================================================================
4832 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4833 +++ linux-3.14.18/drivers/net/ethernet/ralink/ralink_soc_eth.h 2014-10-29 20:25:55.433357519 +0100
4836 + * This program is free software; you can redistribute it and/or modify
4837 + * it under the terms of the GNU General Public License as published by
4838 + * the Free Software Foundation; version 2 of the License
4840 + * This program is distributed in the hope that it will be useful,
4841 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4842 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4843 + * GNU General Public License for more details.
4845 + * You should have received a copy of the GNU General Public License
4846 + * along with this program; if not, write to the Free Software
4847 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4849 + * based on Ralink SDK3.3
4850 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4856 +#include <linux/mii.h>
4857 +#include <linux/interrupt.h>
4858 +#include <linux/netdevice.h>
4859 +#include <linux/dma-mapping.h>
4860 +#include <linux/phy.h>
4861 +#include <linux/ethtool.h>
4864 + FE_REG_PDMA_GLO_CFG = 0,
4865 + FE_REG_PDMA_RST_CFG,
4866 + FE_REG_DLY_INT_CFG,
4867 + FE_REG_TX_BASE_PTR0,
4868 + FE_REG_TX_MAX_CNT0,
4869 + FE_REG_TX_CTX_IDX0,
4870 + FE_REG_RX_BASE_PTR0,
4871 + FE_REG_RX_MAX_CNT0,
4872 + FE_REG_RX_CALC_IDX0,
4873 + FE_REG_FE_INT_ENABLE,
4874 + FE_REG_FE_INT_STATUS,
4875 + FE_REG_FE_DMA_VID_BASE,
4876 + FE_REG_FE_COUNTER_BASE,
4880 +#define FE_DRV_VERSION "0.1.0"
4882 +/* power of 2 to let NEXT_TX_DESP_IDX work */
4883 +#define NUM_DMA_DESC (1 << 7)
4884 +#define MAX_DMA_DESC 0xfff
4886 +#define FE_DELAY_EN_INT 0x80
4887 +#define FE_DELAY_MAX_INT 0x04
4888 +#define FE_DELAY_MAX_TOUT 0x04
4889 +#define FE_DELAY_TIME 20
4890 +#define FE_DELAY_CHAN (((FE_DELAY_EN_INT | FE_DELAY_MAX_INT) << 8) | FE_DELAY_MAX_TOUT)
4891 +#define FE_DELAY_INIT ((FE_DELAY_CHAN << 16) | FE_DELAY_CHAN)
4892 +#define FE_PSE_FQFC_CFG_INIT 0x80504000
4893 +#define FE_PSE_FQFC_CFG_256Q 0xff908000
4895 +/* interrupt bits */
4896 +#define FE_CNT_PPE_AF BIT(31)
4897 +#define FE_CNT_GDM_AF BIT(29)
4898 +#define FE_PSE_P2_FC BIT(26)
4899 +#define FE_PSE_BUF_DROP BIT(24)
4900 +#define FE_GDM_OTHER_DROP BIT(23)
4901 +#define FE_PSE_P1_FC BIT(22)
4902 +#define FE_PSE_P0_FC BIT(21)
4903 +#define FE_PSE_FQ_EMPTY BIT(20)
4904 +#define FE_GE1_STA_CHG BIT(18)
4905 +#define FE_TX_COHERENT BIT(17)
4906 +#define FE_RX_COHERENT BIT(16)
4907 +#define FE_TX_DONE_INT3 BIT(11)
4908 +#define FE_TX_DONE_INT2 BIT(10)
4909 +#define FE_TX_DONE_INT1 BIT(9)
4910 +#define FE_TX_DONE_INT0 BIT(8)
4911 +#define FE_RX_DONE_INT0 BIT(2)
4912 +#define FE_TX_DLY_INT BIT(1)
4913 +#define FE_RX_DLY_INT BIT(0)
4915 +#define RT5350_RX_DLY_INT BIT(30)
4916 +#define RT5350_TX_DLY_INT BIT(28)
4919 +#define FE_FE_OFFSET 0x0000
4920 +#define FE_GDMA_OFFSET 0x0020
4921 +#define FE_PSE_OFFSET 0x0040
4922 +#define FE_GDMA2_OFFSET 0x0060
4923 +#define FE_CDMA_OFFSET 0x0080
4924 +#define FE_DMA_VID0 0x00a8
4925 +#define FE_PDMA_OFFSET 0x0100
4926 +#define FE_PPE_OFFSET 0x0200
4927 +#define FE_CMTABLE_OFFSET 0x0400
4928 +#define FE_POLICYTABLE_OFFSET 0x1000
4930 +#define RT5350_PDMA_OFFSET 0x0800
4931 +#define RT5350_SDM_OFFSET 0x0c00
4933 +#define FE_MDIO_ACCESS (FE_FE_OFFSET + 0x00)
4934 +#define FE_MDIO_CFG (FE_FE_OFFSET + 0x04)
4935 +#define FE_FE_GLO_CFG (FE_FE_OFFSET + 0x08)
4936 +#define FE_FE_RST_GL (FE_FE_OFFSET + 0x0C)
4937 +#define FE_FE_INT_STATUS (FE_FE_OFFSET + 0x10)
4938 +#define FE_FE_INT_ENABLE (FE_FE_OFFSET + 0x14)
4939 +#define FE_MDIO_CFG2 (FE_FE_OFFSET + 0x18)
4940 +#define FE_FOC_TS_T (FE_FE_OFFSET + 0x1C)
4942 +#define FE_GDMA1_FWD_CFG (FE_GDMA_OFFSET + 0x00)
4943 +#define FE_GDMA1_SCH_CFG (FE_GDMA_OFFSET + 0x04)
4944 +#define FE_GDMA1_SHPR_CFG (FE_GDMA_OFFSET + 0x08)
4945 +#define FE_GDMA1_MAC_ADRL (FE_GDMA_OFFSET + 0x0C)
4946 +#define FE_GDMA1_MAC_ADRH (FE_GDMA_OFFSET + 0x10)
4948 +#define FE_GDMA2_FWD_CFG (FE_GDMA2_OFFSET + 0x00)
4949 +#define FE_GDMA2_SCH_CFG (FE_GDMA2_OFFSET + 0x04)
4950 +#define FE_GDMA2_SHPR_CFG (FE_GDMA2_OFFSET + 0x08)
4951 +#define FE_GDMA2_MAC_ADRL (FE_GDMA2_OFFSET + 0x0C)
4952 +#define FE_GDMA2_MAC_ADRH (FE_GDMA2_OFFSET + 0x10)
4954 +#define FE_PSE_FQ_CFG (FE_PSE_OFFSET + 0x00)
4955 +#define FE_CDMA_FC_CFG (FE_PSE_OFFSET + 0x04)
4956 +#define FE_GDMA1_FC_CFG (FE_PSE_OFFSET + 0x08)
4957 +#define FE_GDMA2_FC_CFG (FE_PSE_OFFSET + 0x0C)
4959 +#define FE_CDMA_CSG_CFG (FE_CDMA_OFFSET + 0x00)
4960 +#define FE_CDMA_SCH_CFG (FE_CDMA_OFFSET + 0x04)
4962 +#define MT7620A_GDMA_OFFSET 0x0600
4963 +#define MT7620A_GDMA1_FWD_CFG (MT7620A_GDMA_OFFSET + 0x00)
4964 +#define MT7620A_FE_GDMA1_SCH_CFG (MT7620A_GDMA_OFFSET + 0x04)
4965 +#define MT7620A_FE_GDMA1_SHPR_CFG (MT7620A_GDMA_OFFSET + 0x08)
4966 +#define MT7620A_FE_GDMA1_MAC_ADRL (MT7620A_GDMA_OFFSET + 0x0C)
4967 +#define MT7620A_FE_GDMA1_MAC_ADRH (MT7620A_GDMA_OFFSET + 0x10)
4969 +#define RT5350_TX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x00)
4970 +#define RT5350_TX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x04)
4971 +#define RT5350_TX_CTX_IDX0 (RT5350_PDMA_OFFSET + 0x08)
4972 +#define RT5350_TX_DTX_IDX0 (RT5350_PDMA_OFFSET + 0x0C)
4973 +#define RT5350_TX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x10)
4974 +#define RT5350_TX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x14)
4975 +#define RT5350_TX_CTX_IDX1 (RT5350_PDMA_OFFSET + 0x18)
4976 +#define RT5350_TX_DTX_IDX1 (RT5350_PDMA_OFFSET + 0x1C)
4977 +#define RT5350_TX_BASE_PTR2 (RT5350_PDMA_OFFSET + 0x20)
4978 +#define RT5350_TX_MAX_CNT2 (RT5350_PDMA_OFFSET + 0x24)
4979 +#define RT5350_TX_CTX_IDX2 (RT5350_PDMA_OFFSET + 0x28)
4980 +#define RT5350_TX_DTX_IDX2 (RT5350_PDMA_OFFSET + 0x2C)
4981 +#define RT5350_TX_BASE_PTR3 (RT5350_PDMA_OFFSET + 0x30)
4982 +#define RT5350_TX_MAX_CNT3 (RT5350_PDMA_OFFSET + 0x34)
4983 +#define RT5350_TX_CTX_IDX3 (RT5350_PDMA_OFFSET + 0x38)
4984 +#define RT5350_TX_DTX_IDX3 (RT5350_PDMA_OFFSET + 0x3C)
4985 +#define RT5350_RX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x100)
4986 +#define RT5350_RX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x104)
4987 +#define RT5350_RX_CALC_IDX0 (RT5350_PDMA_OFFSET + 0x108)
4988 +#define RT5350_RX_DRX_IDX0 (RT5350_PDMA_OFFSET + 0x10C)
4989 +#define RT5350_RX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x110)
4990 +#define RT5350_RX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x114)
4991 +#define RT5350_RX_CALC_IDX1 (RT5350_PDMA_OFFSET + 0x118)
4992 +#define RT5350_RX_DRX_IDX1 (RT5350_PDMA_OFFSET + 0x11C)
4993 +#define RT5350_PDMA_GLO_CFG (RT5350_PDMA_OFFSET + 0x204)
4994 +#define RT5350_PDMA_RST_CFG (RT5350_PDMA_OFFSET + 0x208)
4995 +#define RT5350_DLY_INT_CFG (RT5350_PDMA_OFFSET + 0x20c)
4996 +#define RT5350_FE_INT_STATUS (RT5350_PDMA_OFFSET + 0x220)
4997 +#define RT5350_FE_INT_ENABLE (RT5350_PDMA_OFFSET + 0x228)
4998 +#define RT5350_PDMA_SCH_CFG (RT5350_PDMA_OFFSET + 0x280)
5000 +#define FE_PDMA_GLO_CFG (FE_PDMA_OFFSET + 0x00)
5001 +#define FE_PDMA_RST_CFG (FE_PDMA_OFFSET + 0x04)
5002 +#define FE_PDMA_SCH_CFG (FE_PDMA_OFFSET + 0x08)
5003 +#define FE_DLY_INT_CFG (FE_PDMA_OFFSET + 0x0C)
5004 +#define FE_TX_BASE_PTR0 (FE_PDMA_OFFSET + 0x10)
5005 +#define FE_TX_MAX_CNT0 (FE_PDMA_OFFSET + 0x14)
5006 +#define FE_TX_CTX_IDX0 (FE_PDMA_OFFSET + 0x18)
5007 +#define FE_TX_DTX_IDX0 (FE_PDMA_OFFSET + 0x1C)
5008 +#define FE_TX_BASE_PTR1 (FE_PDMA_OFFSET + 0x20)
5009 +#define FE_TX_MAX_CNT1 (FE_PDMA_OFFSET + 0x24)
5010 +#define FE_TX_CTX_IDX1 (FE_PDMA_OFFSET + 0x28)
5011 +#define FE_TX_DTX_IDX1 (FE_PDMA_OFFSET + 0x2C)
5012 +#define FE_RX_BASE_PTR0 (FE_PDMA_OFFSET + 0x30)
5013 +#define FE_RX_MAX_CNT0 (FE_PDMA_OFFSET + 0x34)
5014 +#define FE_RX_CALC_IDX0 (FE_PDMA_OFFSET + 0x38)
5015 +#define FE_RX_DRX_IDX0 (FE_PDMA_OFFSET + 0x3C)
5016 +#define FE_TX_BASE_PTR2 (FE_PDMA_OFFSET + 0x40)
5017 +#define FE_TX_MAX_CNT2 (FE_PDMA_OFFSET + 0x44)
5018 +#define FE_TX_CTX_IDX2 (FE_PDMA_OFFSET + 0x48)
5019 +#define FE_TX_DTX_IDX2 (FE_PDMA_OFFSET + 0x4C)
5020 +#define FE_TX_BASE_PTR3 (FE_PDMA_OFFSET + 0x50)
5021 +#define FE_TX_MAX_CNT3 (FE_PDMA_OFFSET + 0x54)
5022 +#define FE_TX_CTX_IDX3 (FE_PDMA_OFFSET + 0x58)
5023 +#define FE_TX_DTX_IDX3 (FE_PDMA_OFFSET + 0x5C)
5024 +#define FE_RX_BASE_PTR1 (FE_PDMA_OFFSET + 0x60)
5025 +#define FE_RX_MAX_CNT1 (FE_PDMA_OFFSET + 0x64)
5026 +#define FE_RX_CALC_IDX1 (FE_PDMA_OFFSET + 0x68)
5027 +#define FE_RX_DRX_IDX1 (FE_PDMA_OFFSET + 0x6C)
5029 +#define RT5350_SDM_CFG (RT5350_SDM_OFFSET + 0x00) //Switch DMA configuration
5030 +#define RT5350_SDM_RRING (RT5350_SDM_OFFSET + 0x04) //Switch DMA Rx Ring
5031 +#define RT5350_SDM_TRING (RT5350_SDM_OFFSET + 0x08) //Switch DMA Tx Ring
5032 +#define RT5350_SDM_MAC_ADRL (RT5350_SDM_OFFSET + 0x0C) //Switch MAC address LSB
5033 +#define RT5350_SDM_MAC_ADRH (RT5350_SDM_OFFSET + 0x10) //Switch MAC Address MSB
5034 +#define RT5350_SDM_TPCNT (RT5350_SDM_OFFSET + 0x100) //Switch DMA Tx packet count
5035 +#define RT5350_SDM_TBCNT (RT5350_SDM_OFFSET + 0x104) //Switch DMA Tx byte count
5036 +#define RT5350_SDM_RPCNT (RT5350_SDM_OFFSET + 0x108) //Switch DMA rx packet count
5037 +#define RT5350_SDM_RBCNT (RT5350_SDM_OFFSET + 0x10C) //Switch DMA rx byte count
5038 +#define RT5350_SDM_CS_ERR (RT5350_SDM_OFFSET + 0x110) //Switch DMA rx checksum error count
5040 +#define RT5350_SDM_ICS_EN BIT(16)
5041 +#define RT5350_SDM_TCS_EN BIT(17)
5042 +#define RT5350_SDM_UCS_EN BIT(18)
5045 +/* MDIO_CFG register bits */
5046 +#define FE_MDIO_CFG_AUTO_POLL_EN BIT(29)
5047 +#define FE_MDIO_CFG_GP1_BP_EN BIT(16)
5048 +#define FE_MDIO_CFG_GP1_FRC_EN BIT(15)
5049 +#define FE_MDIO_CFG_GP1_SPEED_10 (0 << 13)
5050 +#define FE_MDIO_CFG_GP1_SPEED_100 (1 << 13)
5051 +#define FE_MDIO_CFG_GP1_SPEED_1000 (2 << 13)
5052 +#define FE_MDIO_CFG_GP1_DUPLEX BIT(12)
5053 +#define FE_MDIO_CFG_GP1_FC_TX BIT(11)
5054 +#define FE_MDIO_CFG_GP1_FC_RX BIT(10)
5055 +#define FE_MDIO_CFG_GP1_LNK_DWN BIT(9)
5056 +#define FE_MDIO_CFG_GP1_AN_FAIL BIT(8)
5057 +#define FE_MDIO_CFG_MDC_CLK_DIV_1 (0 << 6)
5058 +#define FE_MDIO_CFG_MDC_CLK_DIV_2 (1 << 6)
5059 +#define FE_MDIO_CFG_MDC_CLK_DIV_4 (2 << 6)
5060 +#define FE_MDIO_CFG_MDC_CLK_DIV_8 (3 << 6)
5061 +#define FE_MDIO_CFG_TURBO_MII_FREQ BIT(5)
5062 +#define FE_MDIO_CFG_TURBO_MII_MODE BIT(4)
5063 +#define FE_MDIO_CFG_RX_CLK_SKEW_0 (0 << 2)
5064 +#define FE_MDIO_CFG_RX_CLK_SKEW_200 (1 << 2)
5065 +#define FE_MDIO_CFG_RX_CLK_SKEW_400 (2 << 2)
5066 +#define FE_MDIO_CFG_RX_CLK_SKEW_INV (3 << 2)
5067 +#define FE_MDIO_CFG_TX_CLK_SKEW_0 0
5068 +#define FE_MDIO_CFG_TX_CLK_SKEW_200 1
5069 +#define FE_MDIO_CFG_TX_CLK_SKEW_400 2
5070 +#define FE_MDIO_CFG_TX_CLK_SKEW_INV 3
5072 +/* uni-cast port */
5073 +#define FE_GDM1_JMB_LEN_MASK 0xf
5074 +#define FE_GDM1_JMB_LEN_SHIFT 28
5075 +#define FE_GDM1_ICS_EN BIT(22)
5076 +#define FE_GDM1_TCS_EN BIT(21)
5077 +#define FE_GDM1_UCS_EN BIT(20)
5078 +#define FE_GDM1_JMB_EN BIT(19)
5079 +#define FE_GDM1_STRPCRC BIT(16)
5080 +#define FE_GDM1_UFRC_P_CPU (0 << 12)
5081 +#define FE_GDM1_UFRC_P_GDMA1 (1 << 12)
5082 +#define FE_GDM1_UFRC_P_PPE (6 << 12)
5085 +#define FE_ICS_GEN_EN BIT(2)
5086 +#define FE_UCS_GEN_EN BIT(1)
5087 +#define FE_TCS_GEN_EN BIT(0)
5090 +#define FE_PST_DRX_IDX0 BIT(16)
5091 +#define FE_PST_DTX_IDX3 BIT(3)
5092 +#define FE_PST_DTX_IDX2 BIT(2)
5093 +#define FE_PST_DTX_IDX1 BIT(1)
5094 +#define FE_PST_DTX_IDX0 BIT(0)
5096 +#define FE_TX_WB_DDONE BIT(6)
5097 +#define FE_RX_DMA_BUSY BIT(3)
5098 +#define FE_TX_DMA_BUSY BIT(1)
5099 +#define FE_RX_DMA_EN BIT(2)
5100 +#define FE_TX_DMA_EN BIT(0)
5102 +#define FE_PDMA_SIZE_4DWORDS (0 << 4)
5103 +#define FE_PDMA_SIZE_8DWORDS (1 << 4)
5104 +#define FE_PDMA_SIZE_16DWORDS (2 << 4)
5106 +#define FE_US_CYC_CNT_MASK 0xff
5107 +#define FE_US_CYC_CNT_SHIFT 0x8
5108 +#define FE_US_CYC_CNT_DIVISOR 1000000
5110 +#define RX_DMA_PLEN0(_x) (((_x) >> 16) & 0x3fff)
5111 +#define RX_DMA_LSO BIT(30)
5112 +#define RX_DMA_DONE BIT(31)
5113 +#define RX_DMA_L4VALID BIT(30)
5116 + unsigned int rxd1;
5117 + unsigned int rxd2;
5118 + unsigned int rxd3;
5119 + unsigned int rxd4;
5120 +} __packed __aligned(4);
5122 +#define TX_DMA_PLEN0_MASK ((0x3fff) << 16)
5123 +#define TX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
5124 +#define TX_DMA_PLEN1(_x) ((_x) & 0x3fff)
5125 +#define TX_DMA_GET_PLEN0(_x) (((_x) >> 16 ) & 0x3fff)
5126 +#define TX_DMA_GET_PLEN1(_x) ((_x) & 0x3fff)
5127 +#define TX_DMA_LS1 BIT(14)
5128 +#define TX_DMA_LS0 BIT(30)
5129 +#define TX_DMA_DONE BIT(31)
5131 +#define TX_DMA_INS_VLAN BIT(7)
5132 +#define TX_DMA_INS_PPPOE BIT(12)
5133 +#define TX_DMA_QN(_x) ((_x) << 16)
5134 +#define TX_DMA_PN(_x) ((_x) << 24)
5135 +#define TX_DMA_QN_MASK TX_DMA_QN(0x7)
5136 +#define TX_DMA_PN_MASK TX_DMA_PN(0x7)
5137 +#define TX_DMA_UDF BIT(20)
5138 +#define TX_DMA_CHKSUM (0x7 << 29)
5139 +#define TX_DMA_TSO BIT(28)
5141 +/* frame engine counters */
5142 +#define FE_PPE_AC_BCNT0 (FE_CMTABLE_OFFSET + 0x00)
5143 +#define FE_GDMA1_TX_GBCNT (FE_CMTABLE_OFFSET + 0x300)
5144 +#define FE_GDMA2_TX_GBCNT (FE_GDMA1_TX_GBCNT + 0x40)
5146 +/* phy device flags */
5147 +#define FE_PHY_FLAG_PORT BIT(0)
5148 +#define FE_PHY_FLAG_ATTACH BIT(1)
5151 + unsigned int txd1;
5152 + unsigned int txd2;
5153 + unsigned int txd3;
5154 + unsigned int txd4;
5155 +} __packed __aligned(4);
5160 + struct phy_device *phy[8];
5161 + struct device_node *phy_node[8];
5162 + const __be32 *phy_fixed[8];
5169 + int (*connect)(struct fe_priv *priv);
5170 + void (*disconnect)(struct fe_priv *priv);
5171 + void (*start)(struct fe_priv *priv);
5172 + void (*stop)(struct fe_priv *priv);
5177 + unsigned char mac[6];
5178 + const u32 *reg_table;
5180 + void (*init_data)(struct fe_soc_data *data, struct net_device *netdev);
5181 + void (*reset_fe)(void);
5182 + void (*set_mac)(struct fe_priv *priv, unsigned char *mac);
5183 + int (*fwd_config)(struct fe_priv *priv);
5184 + void (*tx_dma)(struct fe_priv *priv, int idx, struct sk_buff *skb);
5185 + void (*rx_dma)(struct fe_priv *priv, int idx, int len);
5186 + int (*switch_init)(struct fe_priv *priv);
5187 + int (*switch_config)(struct fe_priv *priv);
5188 + void (*port_init)(struct fe_priv *priv, struct device_node *port);
5189 + int (*has_carrier)(struct fe_priv *priv);
5190 + int (*mdio_init)(struct fe_priv *priv);
5191 + void (*mdio_cleanup)(struct fe_priv *priv);
5192 + int (*mdio_write)(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
5193 + int (*mdio_read)(struct mii_bus *bus, int phy_addr, int phy_reg);
5194 + void (*mdio_adjust_link)(struct fe_priv *priv, int port);
5204 +#define FE_FLAG_PADDING_64B BIT(0)
5205 +#define FE_FLAG_PADDING_BUG BIT(1)
5206 +#define FE_FLAG_JUMBO_FRAME BIT(2)
5208 +#define FE_STAT_REG_DECLARE \
5212 + _FE(tx_collisions) \
5215 + _FE(rx_overflow) \
5216 + _FE(rx_fcs_errors) \
5217 + _FE(rx_short_errors) \
5218 + _FE(rx_long_errors) \
5219 + _FE(rx_checksum_errors) \
5220 + _FE(rx_flow_control_packets)
5224 + spinlock_t stats_lock;
5225 + struct u64_stats_sync syncp;
5226 +#define _FE(x) u64 x;
5227 +FE_STAT_REG_DECLARE
5233 + spinlock_t page_lock;
5235 + struct fe_soc_data *soc;
5236 + struct net_device *netdev;
5240 + struct device *device;
5241 + unsigned long sysclk;
5245 + struct fe_rx_dma *rx_dma;
5247 + dma_addr_t rx_phys;
5248 + struct napi_struct rx_napi;
5250 + struct fe_tx_dma *tx_dma;
5251 + struct sk_buff **tx_skb;
5252 + dma_addr_t tx_phys;
5253 + unsigned int tx_free_idx;
5255 + struct fe_phy *phy;
5256 + struct mii_bus *mii_bus;
5257 + struct phy_device *phy_dev;
5262 + struct fe_hw_stats *hw_stats;
5265 +extern const struct of_device_id of_fe_match[];
5267 +void fe_w32(u32 val, unsigned reg);
5268 +u32 fe_r32(unsigned reg);
5270 +int fe_set_clock_cycle(struct fe_priv *priv);
5271 +void fe_csum_config(struct fe_priv *priv);
5272 +void fe_stats_update(struct fe_priv *priv);
5273 +void fe_fwd_config(struct fe_priv *priv);
5274 +void fe_reg_w32(u32 val, enum fe_reg reg);
5275 +u32 fe_reg_r32(enum fe_reg reg);
5277 +static inline void *priv_netdev(struct fe_priv *priv)
5279 + return (char *)priv - ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
5282 +#endif /* FE_ETH_H */
5283 Index: linux-3.14.18/drivers/net/ethernet/ralink/soc_mt7620.c
5284 ===================================================================
5285 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5286 +++ linux-3.14.18/drivers/net/ethernet/ralink/soc_mt7620.c 2014-10-29 20:25:55.433357519 +0100
5289 + * This program is free software; you can redistribute it and/or modify
5290 + * it under the terms of the GNU General Public License as published by
5291 + * the Free Software Foundation; version 2 of the License
5293 + * This program is distributed in the hope that it will be useful,
5294 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5295 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5296 + * GNU General Public License for more details.
5298 + * You should have received a copy of the GNU General Public License
5299 + * along with this program; if not, write to the Free Software
5300 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5302 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
5305 +#include <linux/module.h>
5306 +#include <linux/platform_device.h>
5307 +#include <linux/if_vlan.h>
5309 +#include <asm/mach-ralink/ralink_regs.h>
5311 +#include <mt7620.h>
5312 +#include "ralink_soc_eth.h"
5313 +#include "gsw_mt7620a.h"
5315 +#define MT7620A_CDMA_CSG_CFG 0x400
5316 +#define MT7620_DMA_VID (MT7620A_CDMA_CSG_CFG | 0x30)
5317 +#define MT7620A_DMA_2B_OFFSET BIT(31)
5318 +#define MT7620A_RESET_FE BIT(21)
5319 +#define MT7620A_RESET_ESW BIT(23)
5320 +#define MT7620_L4_VALID BIT(23)
5322 +#define MT7620_TX_DMA_UDF BIT(15)
5323 +#define TX_DMA_FP_BMAP ((0xff) << 19)
5325 +#define SYSC_REG_RESET_CTRL 0x34
5327 +#define CDMA_ICS_EN BIT(2)
5328 +#define CDMA_UCS_EN BIT(1)
5329 +#define CDMA_TCS_EN BIT(0)
5331 +#define GDMA_ICS_EN BIT(22)
5332 +#define GDMA_TCS_EN BIT(21)
5333 +#define GDMA_UCS_EN BIT(20)
5335 +/* frame engine counters */
5336 +#define MT7620_REG_MIB_OFFSET 0x1000
5337 +#define MT7620_PPE_AC_BCNT0 (MT7620_REG_MIB_OFFSET + 0x00)
5338 +#define MT7620_GDM1_TX_GBCNT (MT7620_REG_MIB_OFFSET + 0x300)
5339 +#define MT7620_GDM2_TX_GBCNT (MT7620_GDM1_TX_GBCNT + 0x40)
5341 +static const u32 mt7620_reg_table[FE_REG_COUNT] = {
5342 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
5343 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
5344 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
5345 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
5346 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
5347 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
5348 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
5349 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
5350 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
5351 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
5352 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
5353 + [FE_REG_FE_DMA_VID_BASE] = MT7620_DMA_VID,
5354 + [FE_REG_FE_COUNTER_BASE] = MT7620_GDM1_TX_GBCNT,
5357 +static void mt7620_fe_reset(void)
5359 + rt_sysc_w32(MT7620A_RESET_FE | MT7620A_RESET_ESW, SYSC_REG_RESET_CTRL);
5360 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
5363 +static void mt7620_rxcsum_config(bool enable)
5366 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN |
5367 + GDMA_TCS_EN | GDMA_UCS_EN),
5368 + MT7620A_GDMA1_FWD_CFG);
5370 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~(GDMA_ICS_EN |
5371 + GDMA_TCS_EN | GDMA_UCS_EN),
5372 + MT7620A_GDMA1_FWD_CFG);
5375 +static void mt7620_txcsum_config(bool enable)
5378 + fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) | (CDMA_ICS_EN |
5379 + CDMA_UCS_EN | CDMA_TCS_EN),
5380 + MT7620A_CDMA_CSG_CFG);
5382 + fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) & ~(CDMA_ICS_EN |
5383 + CDMA_UCS_EN | CDMA_TCS_EN),
5384 + MT7620A_CDMA_CSG_CFG);
5387 +static int mt7620_fwd_config(struct fe_priv *priv)
5389 + struct net_device *dev = priv_netdev(priv);
5391 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~7, MT7620A_GDMA1_FWD_CFG);
5393 + mt7620_txcsum_config((dev->features & NETIF_F_IP_CSUM));
5394 + mt7620_rxcsum_config((dev->features & NETIF_F_RXCSUM));
5399 +static void mt7620_tx_dma(struct fe_priv *priv, int idx, struct sk_buff *skb)
5401 + priv->tx_dma[idx].txd4 = 0;
5404 +static void mt7620_rx_dma(struct fe_priv *priv, int idx, int len)
5406 + priv->rx_dma[idx].rxd2 = RX_DMA_PLEN0(len);
5409 +static void mt7620_init_data(struct fe_soc_data *data,
5410 + struct net_device *netdev)
5412 + struct fe_priv *priv = netdev_priv(netdev);
5414 + priv->flags = FE_FLAG_PADDING_64B;
5415 + netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
5416 + NETIF_F_HW_VLAN_CTAG_TX;
5418 + if (mt7620_get_eco() >= 5)
5419 + netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
5420 + NETIF_F_IPV6_CSUM;
5423 +static struct fe_soc_data mt7620_data = {
5424 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5425 + .init_data = mt7620_init_data,
5426 + .reset_fe = mt7620_fe_reset,
5427 + .set_mac = mt7620_set_mac,
5428 + .fwd_config = mt7620_fwd_config,
5429 + .tx_dma = mt7620_tx_dma,
5430 + .rx_dma = mt7620_rx_dma,
5431 + .switch_init = mt7620_gsw_probe,
5432 + .switch_config = mt7620_gsw_config,
5433 + .port_init = mt7620_port_init,
5434 + .reg_table = mt7620_reg_table,
5435 + .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS | MT7620A_DMA_2B_OFFSET,
5436 + .rx_dly_int = RT5350_RX_DLY_INT,
5437 + .tx_dly_int = RT5350_TX_DLY_INT,
5438 + .checksum_bit = MT7620_L4_VALID,
5439 + .tx_udf_bit = MT7620_TX_DMA_UDF,
5440 + .has_carrier = mt7620a_has_carrier,
5441 + .mdio_read = mt7620_mdio_read,
5442 + .mdio_write = mt7620_mdio_write,
5443 + .mdio_adjust_link = mt7620_mdio_link_adjust,
5446 +const struct of_device_id of_fe_match[] = {
5447 + { .compatible = "ralink,mt7620a-eth", .data = &mt7620_data },
5451 +MODULE_DEVICE_TABLE(of, of_fe_match);
5452 Index: linux-3.14.18/drivers/net/ethernet/ralink/soc_rt2880.c
5453 ===================================================================
5454 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5455 +++ linux-3.14.18/drivers/net/ethernet/ralink/soc_rt2880.c 2014-10-29 20:25:55.433357519 +0100
5458 + * This program is free software; you can redistribute it and/or modify
5459 + * it under the terms of the GNU General Public License as published by
5460 + * the Free Software Foundation; version 2 of the License
5462 + * This program is distributed in the hope that it will be useful,
5463 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5464 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5465 + * GNU General Public License for more details.
5467 + * You should have received a copy of the GNU General Public License
5468 + * along with this program; if not, write to the Free Software
5469 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5471 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
5474 +#include <linux/module.h>
5476 +#include <asm/mach-ralink/ralink_regs.h>
5478 +#include "ralink_soc_eth.h"
5479 +#include "mdio_rt2880.h"
5481 +#define SYSC_REG_RESET_CTRL 0x034
5482 +#define RT2880_RESET_FE BIT(18)
5484 +static void rt2880_init_data(struct fe_soc_data *data,
5485 + struct net_device *netdev)
5487 + struct fe_priv *priv = netdev_priv(netdev);
5489 + priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG |
5490 + FE_FLAG_JUMBO_FRAME;
5491 + netdev->hw_features = NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_TX;
5492 + /* maybe have hardware bug. */
5493 + //netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
5496 +void rt2880_fe_reset(void)
5498 + rt_sysc_w32(RT2880_RESET_FE, SYSC_REG_RESET_CTRL);
5501 +static int rt2880_fwd_config(struct fe_priv *priv)
5505 + ret = fe_set_clock_cycle(priv);
5509 + fe_fwd_config(priv);
5510 + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
5511 + fe_csum_config(priv);
5516 +struct fe_soc_data rt2880_data = {
5517 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5518 + .init_data = rt2880_init_data,
5519 + .reset_fe = rt2880_fe_reset,
5520 + .fwd_config = rt2880_fwd_config,
5521 + .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
5522 + .checksum_bit = RX_DMA_L4VALID,
5523 + .tx_udf_bit = TX_DMA_UDF,
5524 + .rx_dly_int = FE_RX_DLY_INT,
5525 + .tx_dly_int = FE_TX_DLY_INT,
5526 + .mdio_read = rt2880_mdio_read,
5527 + .mdio_write = rt2880_mdio_write,
5528 + .mdio_adjust_link = rt2880_mdio_link_adjust,
5529 + .port_init = rt2880_port_init,
5532 +const struct of_device_id of_fe_match[] = {
5533 + { .compatible = "ralink,rt2880-eth", .data = &rt2880_data },
5537 +MODULE_DEVICE_TABLE(of, of_fe_match);
5538 Index: linux-3.14.18/drivers/net/ethernet/ralink/soc_rt305x.c
5539 ===================================================================
5540 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5541 +++ linux-3.14.18/drivers/net/ethernet/ralink/soc_rt305x.c 2014-10-29 20:25:55.433357519 +0100
5544 + * This program is free software; you can redistribute it and/or modify
5545 + * it under the terms of the GNU General Public License as published by
5546 + * the Free Software Foundation; version 2 of the License
5548 + * This program is distributed in the hope that it will be useful,
5549 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5550 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5551 + * GNU General Public License for more details.
5553 + * You should have received a copy of the GNU General Public License
5554 + * along with this program; if not, write to the Free Software
5555 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5557 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
5560 +#include <linux/module.h>
5562 +#include <asm/mach-ralink/ralink_regs.h>
5563 +#include <asm/mach-ralink/rt305x.h>
5565 +#include "ralink_soc_eth.h"
5566 +#include "mdio_rt2880.h"
5568 +#define RT305X_RESET_FE BIT(21)
5569 +#define RT305X_RESET_ESW BIT(23)
5570 +#define SYSC_REG_RESET_CTRL 0x034
5572 +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
5573 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
5574 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
5575 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
5576 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
5577 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
5578 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
5579 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
5580 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
5581 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
5582 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
5583 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
5584 + [FE_REG_FE_DMA_VID_BASE] = 0,
5587 +static void rt305x_init_data(struct fe_soc_data *data,
5588 + struct net_device *netdev)
5590 + struct fe_priv *priv = netdev_priv(netdev);
5592 + priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG;
5593 + netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
5594 + NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX;
5597 +static int rt3050_fwd_config(struct fe_priv *priv)
5601 + if (soc_is_rt3052()) {
5602 + ret = fe_set_clock_cycle(priv);
5607 + fe_fwd_config(priv);
5608 + if (!soc_is_rt3352())
5609 + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
5610 + fe_csum_config(priv);
5615 +static void rt305x_fe_reset(void)
5617 + rt_sysc_w32(RT305X_RESET_FE, SYSC_REG_RESET_CTRL);
5618 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
5621 +static void rt5350_init_data(struct fe_soc_data *data,
5622 + struct net_device *netdev)
5624 + netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM;
5627 +static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
5629 + unsigned long flags;
5631 + spin_lock_irqsave(&priv->page_lock, flags);
5632 + fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
5633 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
5634 + RT5350_SDM_MAC_ADRL);
5635 + spin_unlock_irqrestore(&priv->page_lock, flags);
5638 +static void rt5350_rxcsum_config(bool enable)
5641 + fe_w32(fe_r32(RT5350_SDM_CFG) | (RT5350_SDM_ICS_EN |
5642 + RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
5645 + fe_w32(fe_r32(RT5350_SDM_CFG) & ~(RT5350_SDM_ICS_EN |
5646 + RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
5650 +static int rt5350_fwd_config(struct fe_priv *priv)
5652 + struct net_device *dev = priv_netdev(priv);
5654 + rt5350_rxcsum_config((dev->features & NETIF_F_RXCSUM));
5659 +static void rt5350_tx_dma(struct fe_priv *priv, int idx, struct sk_buff *skb)
5661 + priv->tx_dma[idx].txd4 = 0;
5664 +static void rt5350_fe_reset(void)
5666 + rt_sysc_w32(RT305X_RESET_FE | RT305X_RESET_ESW, SYSC_REG_RESET_CTRL);
5667 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
5670 +static struct fe_soc_data rt3050_data = {
5671 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5672 + .init_data = rt305x_init_data,
5673 + .reset_fe = rt305x_fe_reset,
5674 + .fwd_config = rt3050_fwd_config,
5675 + .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
5676 + .checksum_bit = RX_DMA_L4VALID,
5677 + .tx_udf_bit = TX_DMA_UDF,
5678 + .rx_dly_int = FE_RX_DLY_INT,
5679 + .tx_dly_int = FE_TX_DLY_INT,
5682 +static struct fe_soc_data rt5350_data = {
5683 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5684 + .init_data = rt5350_init_data,
5685 + .reg_table = rt5350_reg_table,
5686 + .reset_fe = rt5350_fe_reset,
5687 + .set_mac = rt5350_set_mac,
5688 + .fwd_config = rt5350_fwd_config,
5689 + .tx_dma = rt5350_tx_dma,
5690 + .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
5691 + .checksum_bit = RX_DMA_L4VALID,
5692 + .tx_udf_bit = TX_DMA_UDF,
5693 + .rx_dly_int = RT5350_RX_DLY_INT,
5694 + .tx_dly_int = RT5350_TX_DLY_INT,
5697 +const struct of_device_id of_fe_match[] = {
5698 + { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
5699 + { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
5703 +MODULE_DEVICE_TABLE(of, of_fe_match);
5704 Index: linux-3.14.18/drivers/net/ethernet/ralink/soc_rt3883.c
5705 ===================================================================
5706 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5707 +++ linux-3.14.18/drivers/net/ethernet/ralink/soc_rt3883.c 2014-10-29 20:25:55.433357519 +0100
5710 + * This program is free software; you can redistribute it and/or modify
5711 + * it under the terms of the GNU General Public License as published by
5712 + * the Free Software Foundation; version 2 of the License
5714 + * This program is distributed in the hope that it will be useful,
5715 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5716 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5717 + * GNU General Public License for more details.
5719 + * You should have received a copy of the GNU General Public License
5720 + * along with this program; if not, write to the Free Software
5721 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5723 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
5726 +#include <linux/module.h>
5728 +#include <asm/mach-ralink/ralink_regs.h>
5730 +#include "ralink_soc_eth.h"
5731 +#include "mdio_rt2880.h"
5733 +#define RT3883_SYSC_REG_RSTCTRL 0x34
5734 +#define RT3883_RSTCTRL_FE BIT(21)
5736 +static void rt3883_fe_reset(void)
5740 + t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
5741 + t |= RT3883_RSTCTRL_FE;
5742 + rt_sysc_w32(t , RT3883_SYSC_REG_RSTCTRL);
5744 + t &= ~RT3883_RSTCTRL_FE;
5745 + rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
5748 +static int rt3883_fwd_config(struct fe_priv *priv)
5752 + ret = fe_set_clock_cycle(priv);
5756 + fe_fwd_config(priv);
5757 + fe_w32(FE_PSE_FQFC_CFG_256Q, FE_PSE_FQ_CFG);
5758 + fe_csum_config(priv);
5763 +static void rt3883_init_data(struct fe_soc_data *data,
5764 + struct net_device *netdev)
5766 + struct fe_priv *priv = netdev_priv(netdev);
5768 + priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG |
5769 + FE_FLAG_JUMBO_FRAME;
5770 + netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
5771 + NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX;
5774 +static struct fe_soc_data rt3883_data = {
5775 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5776 + .init_data = rt3883_init_data,
5777 + .reset_fe = rt3883_fe_reset,
5778 + .fwd_config = rt3883_fwd_config,
5779 + .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
5780 + .rx_dly_int = FE_RX_DLY_INT,
5781 + .tx_dly_int = FE_TX_DLY_INT,
5782 + .checksum_bit = RX_DMA_L4VALID,
5783 + .tx_udf_bit = TX_DMA_UDF,
5784 + .mdio_read = rt2880_mdio_read,
5785 + .mdio_write = rt2880_mdio_write,
5786 + .mdio_adjust_link = rt2880_mdio_link_adjust,
5787 + .port_init = rt2880_port_init,
5790 +const struct of_device_id of_fe_match[] = {
5791 + { .compatible = "ralink,rt3883-eth", .data = &rt3883_data },
5795 +MODULE_DEVICE_TABLE(of, of_fe_match);
5797 Index: linux-3.14.18/drivers/net/ethernet/ralink/ralink_ethtool.c
5798 ===================================================================
5799 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5800 +++ linux-3.14.18/drivers/net/ethernet/ralink/ralink_ethtool.c 2014-10-29 20:25:55.433357519 +0100
5803 + * This program is free software; you can redistribute it and/or modify
5804 + * it under the terms of the GNU General Public License as published by
5805 + * the Free Software Foundation; version 2 of the License
5807 + * This program is distributed in the hope that it will be useful,
5808 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5809 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5810 + * GNU General Public License for more details.
5812 + * You should have received a copy of the GNU General Public License
5813 + * along with this program; if not, write to the Free Software
5814 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5816 + * Copyright (C) 2009-2013 Michael Lee <igvtee@gmail.com>
5819 +#include "ralink_soc_eth.h"
5821 +static const char fe_gdma_str[][ETH_GSTRING_LEN] = {
5822 +#define _FE(x...) # x,
5823 +FE_STAT_REG_DECLARE
5827 +static int fe_get_settings(struct net_device *dev,
5828 + struct ethtool_cmd *cmd)
5830 + struct fe_priv *priv = netdev_priv(dev);
5833 + if (!priv->phy_dev)
5836 + if (priv->phy_flags == FE_PHY_FLAG_ATTACH) {
5837 + err = phy_read_status(priv->phy_dev);
5842 + return phy_ethtool_gset(priv->phy_dev, cmd);
5848 +static int fe_set_settings(struct net_device *dev,
5849 + struct ethtool_cmd *cmd)
5851 + struct fe_priv *priv = netdev_priv(dev);
5853 + if (!priv->phy_dev)
5856 + if (cmd->phy_address != priv->phy_dev->addr) {
5857 + if (priv->phy->phy_node[cmd->phy_address]) {
5858 + priv->phy_dev = priv->phy->phy[cmd->phy_address];
5859 + priv->phy_flags = FE_PHY_FLAG_PORT;
5860 + } else if (priv->mii_bus &&
5861 + priv->mii_bus->phy_map[cmd->phy_address]) {
5862 + priv->phy_dev = priv->mii_bus->phy_map[cmd->phy_address];
5863 + priv->phy_flags = FE_PHY_FLAG_ATTACH;
5868 + return phy_ethtool_sset(priv->phy_dev, cmd);
5874 +static void fe_get_drvinfo (struct net_device *dev,
5875 + struct ethtool_drvinfo *info)
5877 + struct fe_priv *priv = netdev_priv(dev);
5878 + struct fe_soc_data *soc = priv->soc;
5880 + strlcpy(info->driver, priv->device->driver->name, sizeof(info->driver));
5881 + strlcpy(info->version, FE_DRV_VERSION, sizeof(info->version));
5882 + strlcpy(info->bus_info, dev_name(priv->device), sizeof(info->bus_info));
5884 + if (soc->reg_table[FE_REG_FE_COUNTER_BASE])
5885 + info->n_stats = ARRAY_SIZE(fe_gdma_str);
5888 +static u32 fe_get_msglevel(struct net_device *dev)
5890 + struct fe_priv *priv = netdev_priv(dev);
5892 + return priv->msg_enable;
5895 +static void fe_set_msglevel(struct net_device *dev, u32 value)
5897 + struct fe_priv *priv = netdev_priv(dev);
5899 + priv->msg_enable = value;
5902 +static int fe_nway_reset(struct net_device *dev)
5904 + struct fe_priv *priv = netdev_priv(dev);
5906 + if (!priv->phy_dev)
5907 + goto out_nway_reset;
5909 + return genphy_restart_aneg(priv->phy_dev);
5912 + return -EOPNOTSUPP;
5915 +static u32 fe_get_link(struct net_device *dev)
5917 + struct fe_priv *priv = netdev_priv(dev);
5920 + if (!priv->phy_dev)
5921 + goto out_get_link;
5923 + if (priv->phy_flags == FE_PHY_FLAG_ATTACH) {
5924 + err = genphy_update_link(priv->phy_dev);
5926 + goto out_get_link;
5929 + return priv->phy_dev->link;
5932 + return ethtool_op_get_link(dev);
5935 +static void fe_get_ringparam(struct net_device *dev,
5936 + struct ethtool_ringparam *ring)
5938 + ring->rx_max_pending = MAX_DMA_DESC;
5939 + ring->tx_max_pending = MAX_DMA_DESC;
5940 + ring->rx_pending = NUM_DMA_DESC;
5941 + ring->tx_pending = NUM_DMA_DESC;
5944 +static int fe_get_coalesce(struct net_device *dev,
5945 + struct ethtool_coalesce *coal)
5947 + u32 delay_cfg = fe_reg_r32(FE_REG_DLY_INT_CFG);
5949 + coal->rx_coalesce_usecs = (delay_cfg & 0xff) * FE_DELAY_TIME;
5950 + coal->rx_max_coalesced_frames = ((delay_cfg >> 8) & 0x7f);
5951 + coal->use_adaptive_rx_coalesce = (delay_cfg >> 15) & 0x1;
5953 + coal->tx_coalesce_usecs = ((delay_cfg >> 16 )& 0xff) * FE_DELAY_TIME;
5954 + coal->tx_max_coalesced_frames = ((delay_cfg >> 24) & 0x7f);
5955 + coal->use_adaptive_tx_coalesce = (delay_cfg >> 31) & 0x1;
5960 +static int fe_set_coalesce(struct net_device *dev,
5961 + struct ethtool_coalesce *coal)
5964 + u32 rx_usecs, tx_usecs;
5965 + u32 rx_frames, tx_frames;
5967 + if (!coal->use_adaptive_rx_coalesce || !coal->use_adaptive_tx_coalesce)
5970 + rx_usecs = DIV_ROUND_UP(coal->rx_coalesce_usecs, FE_DELAY_TIME);
5971 + rx_frames = coal->rx_max_coalesced_frames;
5972 + tx_usecs = DIV_ROUND_UP(coal->tx_coalesce_usecs, FE_DELAY_TIME);
5973 + tx_frames = coal->tx_max_coalesced_frames;
5975 + if (((tx_usecs == 0) && (tx_frames ==0)) ||
5976 + ((rx_usecs == 0) && (rx_frames ==0)))
5979 + if (rx_usecs > 0xff) rx_usecs = 0xff;
5980 + if (rx_frames > 0x7f) rx_frames = 0x7f;
5981 + if (tx_usecs > 0xff) tx_usecs = 0xff;
5982 + if (tx_frames > 0x7f) tx_frames = 0x7f;
5984 + delay_cfg = ((((FE_DELAY_EN_INT | tx_frames) << 8) | tx_usecs) << 16) |
5985 + (((FE_DELAY_EN_INT | rx_frames) << 8) | rx_usecs);
5987 + fe_reg_w32(delay_cfg, FE_REG_DLY_INT_CFG);
5992 +static void fe_get_strings(struct net_device *dev, u32 stringset, u8 *data)
5994 + switch (stringset) {
5995 + case ETH_SS_STATS:
5996 + memcpy(data, *fe_gdma_str, sizeof(fe_gdma_str));
6001 +static int fe_get_sset_count(struct net_device *dev, int sset)
6004 + case ETH_SS_STATS:
6005 + return ARRAY_SIZE(fe_gdma_str);
6007 + return -EOPNOTSUPP;
6011 +static void fe_get_ethtool_stats(struct net_device *dev,
6012 + struct ethtool_stats *stats, u64 *data)
6014 + struct fe_priv *priv = netdev_priv(dev);
6015 + struct fe_hw_stats *hwstats = priv->hw_stats;
6016 + u64 *data_src, *data_dst;
6017 + unsigned int start;
6020 + if (netif_running(dev) && netif_device_present(dev)) {
6021 + if (spin_trylock(&hwstats->stats_lock)) {
6022 + fe_stats_update(priv);
6023 + spin_unlock(&hwstats->stats_lock);
6028 + data_src = &hwstats->tx_bytes;
6030 + start = u64_stats_fetch_begin_bh(&hwstats->syncp);
6032 + for (i = 0; i < ARRAY_SIZE(fe_gdma_str); i++)
6033 + *data_dst++ = *data_src++;
6035 + } while (u64_stats_fetch_retry_bh(&hwstats->syncp, start));
6038 +static struct ethtool_ops fe_ethtool_ops = {
6039 + .get_settings = fe_get_settings,
6040 + .set_settings = fe_set_settings,
6041 + .get_drvinfo = fe_get_drvinfo,
6042 + .get_msglevel = fe_get_msglevel,
6043 + .set_msglevel = fe_set_msglevel,
6044 + .nway_reset = fe_nway_reset,
6045 + .get_link = fe_get_link,
6046 + .get_ringparam = fe_get_ringparam,
6047 + .get_coalesce = fe_get_coalesce,
6048 + .set_coalesce = fe_set_coalesce,
6051 +void fe_set_ethtool_ops(struct net_device *netdev)
6053 + struct fe_priv *priv = netdev_priv(netdev);
6054 + struct fe_soc_data *soc = priv->soc;
6056 + if (soc->reg_table[FE_REG_FE_COUNTER_BASE]) {
6057 + fe_ethtool_ops.get_strings = fe_get_strings;
6058 + fe_ethtool_ops.get_sset_count = fe_get_sset_count;
6059 + fe_ethtool_ops.get_ethtool_stats = fe_get_ethtool_stats;
6062 + SET_ETHTOOL_OPS(netdev, &fe_ethtool_ops);
6064 Index: linux-3.14.18/drivers/net/ethernet/ralink/ralink_ethtool.h
6065 ===================================================================
6066 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
6067 +++ linux-3.14.18/drivers/net/ethernet/ralink/ralink_ethtool.h 2014-10-29 20:25:55.433357519 +0100
6070 + * This program is free software; you can redistribute it and/or modify
6071 + * it under the terms of the GNU General Public License as published by
6072 + * the Free Software Foundation; version 2 of the License
6074 + * This program is distributed in the hope that it will be useful,
6075 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6076 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6077 + * GNU General Public License for more details.
6079 + * You should have received a copy of the GNU General Public License
6080 + * along with this program; if not, write to the Free Software
6081 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
6083 + * Copyright (C) 2009-2013 Michael Lee <igvtee@gmail.com>
6086 +#ifndef FE_ETHTOOL_H
6087 +#define FE_ETHTOOL_H
6089 +#include <linux/ethtool.h>
6091 +void fe_set_ethtool_ops(struct net_device *netdev);
6093 +#endif /* FE_ETHTOOL_H */