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 diff --git a/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
55 index 0000000..2098c5c
57 +++ b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
60 + * Ralink RT305x SoC platform device registration
62 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
64 + * This program is free software; you can redistribute it and/or modify it
65 + * under the terms of the GNU General Public License version 2 as published
66 + * by the Free Software Foundation.
69 +#ifndef _RT305X_ESW_PLATFORM_H
70 +#define _RT305X_ESW_PLATFORM_H
73 + RT305X_ESW_VLAN_CONFIG_NONE = 0,
74 + RT305X_ESW_VLAN_CONFIG_LLLLW,
75 + RT305X_ESW_VLAN_CONFIG_WLLLL,
78 +struct rt305x_esw_platform_data
81 + u32 reg_initval_fct2;
82 + u32 reg_initval_fpa2;
85 +#endif /* _RT305X_ESW_PLATFORM_H */
86 diff --git a/arch/mips/ralink/rt305x.c b/arch/mips/ralink/rt305x.c
87 index 356d6a0..ef99d78 100644
88 --- a/arch/mips/ralink/rt305x.c
89 +++ b/arch/mips/ralink/rt305x.c
90 @@ -199,6 +199,7 @@ void __init ralink_clk_init(void)
93 ralink_clk_add("cpu", cpu_rate);
94 + ralink_clk_add("sys", sys_rate);
95 ralink_clk_add("10000b00.spi", sys_rate);
96 ralink_clk_add("10000100.timer", wdt_rate);
97 ralink_clk_add("10000120.watchdog", wdt_rate);
98 diff --git a/drivers/net/ethernet/Kconfig b/drivers/net/ethernet/Kconfig
99 index 506b024..ef6a274 100644
100 --- a/drivers/net/ethernet/Kconfig
101 +++ b/drivers/net/ethernet/Kconfig
102 @@ -134,6 +134,7 @@ config ETHOC
103 source "drivers/net/ethernet/packetengines/Kconfig"
104 source "drivers/net/ethernet/pasemi/Kconfig"
105 source "drivers/net/ethernet/qlogic/Kconfig"
106 +source "drivers/net/ethernet/ralink/Kconfig"
107 source "drivers/net/ethernet/realtek/Kconfig"
108 source "drivers/net/ethernet/renesas/Kconfig"
109 source "drivers/net/ethernet/rdc/Kconfig"
110 diff --git a/drivers/net/ethernet/Makefile b/drivers/net/ethernet/Makefile
111 index c0b8789..7c3eb7b 100644
112 --- a/drivers/net/ethernet/Makefile
113 +++ b/drivers/net/ethernet/Makefile
114 @@ -56,6 +56,7 @@ obj-$(CONFIG_ETHOC) += ethoc.o
115 obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
116 obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
117 obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
118 +obj-$(CONFIG_NET_RALINK) += ralink/
119 obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
120 obj-$(CONFIG_SH_ETH) += renesas/
121 obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
122 diff --git a/drivers/net/ethernet/ralink/Kconfig b/drivers/net/ethernet/ralink/Kconfig
124 index 0000000..727ed78
126 +++ b/drivers/net/ethernet/ralink/Kconfig
129 + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
132 + This driver supports the ethernet mac inside the ralink wisocs
136 +config NET_RALINK_MDIO
137 + def_bool NET_RALINK
138 + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
141 +config NET_RALINK_MDIO_RT2880
142 + def_bool NET_RALINK
143 + depends on (SOC_RT288X || SOC_RT3883)
144 + select NET_RALINK_MDIO
146 +config NET_RALINK_ESW_RT3052
147 + def_bool NET_RALINK
148 + depends on SOC_RT305X
152 +config NET_RALINK_GSW_MT7620
153 + def_bool NET_RALINK
154 + depends on SOC_MT7620
156 + select NET_RALINK_MDIO
160 diff --git a/drivers/net/ethernet/ralink/Makefile b/drivers/net/ethernet/ralink/Makefile
162 index 0000000..de64edf
164 +++ b/drivers/net/ethernet/ralink/Makefile
167 +# Makefile for the Ralink SoCs built-in ethernet macs
170 +ralink-eth-y += ralink_soc_eth.o
172 +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
173 +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
175 +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
176 +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o mt7530.o
178 +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
179 +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
180 +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
181 +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
183 +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
184 diff --git a/drivers/net/ethernet/ralink/esw_rt3052.c b/drivers/net/ethernet/ralink/esw_rt3052.c
186 index 0000000..b937062
188 +++ b/drivers/net/ethernet/ralink/esw_rt3052.c
191 + * This program is free software; you can redistribute it and/or modify
192 + * it under the terms of the GNU General Public License as published by
193 + * the Free Software Foundation; version 2 of the License
195 + * This program is distributed in the hope that it will be useful,
196 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
197 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
198 + * GNU General Public License for more details.
200 + * You should have received a copy of the GNU General Public License
201 + * along with this program; if not, write to the Free Software
202 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
204 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
207 +#include <linux/module.h>
208 +#include <linux/kernel.h>
209 +#include <linux/types.h>
210 +#include <linux/dma-mapping.h>
211 +#include <linux/init.h>
212 +#include <linux/skbuff.h>
213 +#include <linux/etherdevice.h>
214 +#include <linux/ethtool.h>
215 +#include <linux/platform_device.h>
216 +#include <linux/of_device.h>
217 +#include <linux/clk.h>
218 +#include <linux/of_net.h>
219 +#include <linux/of_mdio.h>
221 +#include <asm/mach-ralink/ralink_regs.h>
223 +#include "ralink_soc_eth.h"
225 +#include <linux/ioport.h>
226 +#include <linux/switch.h>
227 +#include <linux/mii.h>
229 +#include <ralink_regs.h>
230 +#include <asm/mach-ralink/rt305x.h>
231 +#include <asm/mach-ralink/rt305x_esw_platform.h>
234 + * HW limitations for this switch:
235 + * - No large frame support (PKT_MAX_LEN at most 1536)
236 + * - Can't have untagged vlan and tagged vlan on one port at the same time,
237 + * though this might be possible using the undocumented PPE.
240 +#define RT305X_ESW_REG_ISR 0x00
241 +#define RT305X_ESW_REG_IMR 0x04
242 +#define RT305X_ESW_REG_FCT0 0x08
243 +#define RT305X_ESW_REG_PFC1 0x14
244 +#define RT305X_ESW_REG_ATS 0x24
245 +#define RT305X_ESW_REG_ATS0 0x28
246 +#define RT305X_ESW_REG_ATS1 0x2c
247 +#define RT305X_ESW_REG_ATS2 0x30
248 +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
249 +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
250 +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
251 +#define RT305X_ESW_REG_POA 0x80
252 +#define RT305X_ESW_REG_FPA 0x84
253 +#define RT305X_ESW_REG_SOCPC 0x8c
254 +#define RT305X_ESW_REG_POC0 0x90
255 +#define RT305X_ESW_REG_POC1 0x94
256 +#define RT305X_ESW_REG_POC2 0x98
257 +#define RT305X_ESW_REG_SGC 0x9c
258 +#define RT305X_ESW_REG_STRT 0xa0
259 +#define RT305X_ESW_REG_PCR0 0xc0
260 +#define RT305X_ESW_REG_PCR1 0xc4
261 +#define RT305X_ESW_REG_FPA2 0xc8
262 +#define RT305X_ESW_REG_FCT2 0xcc
263 +#define RT305X_ESW_REG_SGC2 0xe4
264 +#define RT305X_ESW_REG_P0LED 0xa4
265 +#define RT305X_ESW_REG_P1LED 0xa8
266 +#define RT305X_ESW_REG_P2LED 0xac
267 +#define RT305X_ESW_REG_P3LED 0xb0
268 +#define RT305X_ESW_REG_P4LED 0xb4
269 +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
270 +#define RT305X_ESW_REG_P1PC 0xec
271 +#define RT305X_ESW_REG_P2PC 0xf0
272 +#define RT305X_ESW_REG_P3PC 0xf4
273 +#define RT305X_ESW_REG_P4PC 0xf8
274 +#define RT305X_ESW_REG_P5PC 0xfc
276 +#define RT305X_ESW_LED_LINK 0
277 +#define RT305X_ESW_LED_100M 1
278 +#define RT305X_ESW_LED_DUPLEX 2
279 +#define RT305X_ESW_LED_ACTIVITY 3
280 +#define RT305X_ESW_LED_COLLISION 4
281 +#define RT305X_ESW_LED_LINKACT 5
282 +#define RT305X_ESW_LED_DUPLCOLL 6
283 +#define RT305X_ESW_LED_10MACT 7
284 +#define RT305X_ESW_LED_100MACT 8
285 +/* Additional led states not in datasheet: */
286 +#define RT305X_ESW_LED_BLINK 10
287 +#define RT305X_ESW_LED_ON 12
289 +#define RT305X_ESW_LINK_S 25
290 +#define RT305X_ESW_DUPLEX_S 9
291 +#define RT305X_ESW_SPD_S 0
293 +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
294 +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
295 +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
297 +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
299 +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
300 +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
302 +#define RT305X_ESW_PVIDC_PVID_M 0xfff
303 +#define RT305X_ESW_PVIDC_PVID_S 12
305 +#define RT305X_ESW_VLANI_VID_M 0xfff
306 +#define RT305X_ESW_VLANI_VID_S 12
308 +#define RT305X_ESW_VMSC_MSC_M 0xff
309 +#define RT305X_ESW_VMSC_MSC_S 8
311 +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
312 +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
313 +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
314 +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
316 +#define RT305X_ESW_POC0_EN_BP_S 0
317 +#define RT305X_ESW_POC0_EN_FC_S 8
318 +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
319 +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
320 +#define RT305X_ESW_POC0_DIS_PORT_S 23
322 +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
323 +#define RT305X_ESW_POC2_UNTAG_EN_S 0
324 +#define RT305X_ESW_POC2_ENAGING_S 8
325 +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
327 +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
328 +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
329 +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
330 +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
332 +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
333 +#define RT305X_ESW_PFC1_EN_VLAN_S 16
334 +#define RT305X_ESW_PFC1_EN_TOS_S 24
336 +#define RT305X_ESW_VLAN_NONE 0xfff
338 +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
339 +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
341 +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
342 +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
344 +#define RT305X_ESW_POA_LINK_MASK 0x1f
345 +#define RT305X_ESW_POA_LINK_SHIFT 25
347 +#define RT305X_ESW_PORT_ST_CHG BIT(26)
348 +#define RT305X_ESW_PORT0 0
349 +#define RT305X_ESW_PORT1 1
350 +#define RT305X_ESW_PORT2 2
351 +#define RT305X_ESW_PORT3 3
352 +#define RT305X_ESW_PORT4 4
353 +#define RT305X_ESW_PORT5 5
354 +#define RT305X_ESW_PORT6 6
356 +#define RT305X_ESW_PORTS_NONE 0
358 +#define RT305X_ESW_PMAP_LLLLLL 0x3f
359 +#define RT305X_ESW_PMAP_LLLLWL 0x2f
360 +#define RT305X_ESW_PMAP_WLLLLL 0x3e
362 +#define RT305X_ESW_PORTS_INTERNAL \
363 + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
364 + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
365 + BIT(RT305X_ESW_PORT4))
367 +#define RT305X_ESW_PORTS_NOCPU \
368 + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
370 +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
372 +#define RT305X_ESW_PORTS_ALL \
373 + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
375 +#define RT305X_ESW_NUM_VLANS 16
376 +#define RT305X_ESW_NUM_VIDS 4096
377 +#define RT305X_ESW_NUM_PORTS 7
378 +#define RT305X_ESW_NUM_LANWAN 6
379 +#define RT305X_ESW_NUM_LEDS 5
381 +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
382 +#define RT5350_EWS_REG_LED_POLARITY 0x168
383 +#define RT5350_RESET_EPHY BIT(24)
384 +#define SYSC_REG_RESET_CTRL 0x34
387 + /* Global attributes. */
388 + RT305X_ESW_ATTR_ENABLE_VLAN,
389 + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
390 + RT305X_ESW_ATTR_BC_STATUS,
391 + RT305X_ESW_ATTR_LED_FREQ,
392 + /* Port attributes. */
393 + RT305X_ESW_ATTR_PORT_DISABLE,
394 + RT305X_ESW_ATTR_PORT_DOUBLETAG,
395 + RT305X_ESW_ATTR_PORT_UNTAG,
396 + RT305X_ESW_ATTR_PORT_LED,
397 + RT305X_ESW_ATTR_PORT_LAN,
398 + RT305X_ESW_ATTR_PORT_RECV_BAD,
399 + RT305X_ESW_ATTR_PORT_RECV_GOOD,
400 + RT5350_ESW_ATTR_PORT_TR_BAD,
401 + RT5350_ESW_ATTR_PORT_TR_GOOD,
418 + struct device *dev;
419 + void __iomem *base;
421 + const struct rt305x_esw_platform_data *pdata;
422 + /* Protects against concurrent register rmw operations. */
423 + spinlock_t reg_rw_lock;
425 + unsigned char port_map;
426 + unsigned int reg_initval_fct2;
427 + unsigned int reg_initval_fpa2;
428 + unsigned int reg_led_polarity;
431 + struct switch_dev swdev;
432 + bool global_vlan_enable;
433 + bool alt_vlan_disable;
434 + int bc_storm_protect;
436 + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
437 + struct esw_port ports[RT305X_ESW_NUM_PORTS];
441 +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
443 + __raw_writel(val, esw->base + reg);
446 +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
448 + return __raw_readl(esw->base + reg);
451 +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
456 + t = __raw_readl(esw->base + reg) & ~mask;
457 + __raw_writel(t | val, esw->base + reg);
460 +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
463 + unsigned long flags;
465 + spin_lock_irqsave(&esw->reg_rw_lock, flags);
466 + esw_rmw_raw(esw, reg, mask, val);
467 + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
470 +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
473 + unsigned long t_start = jiffies;
477 + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
478 + RT305X_ESW_PCR1_WT_DONE))
480 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
486 + write_data &= 0xffff;
488 + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
489 + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
490 + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
491 + RT305X_ESW_REG_PCR0);
495 + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
496 + RT305X_ESW_PCR1_WT_DONE)
499 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
506 + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
510 +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
515 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
516 + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
517 + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
522 +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
526 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
528 + RT305X_ESW_REG_VLANI(vlan / 2),
529 + RT305X_ESW_VLANI_VID_M << s,
530 + (vid & RT305X_ESW_VLANI_VID_M) << s);
533 +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
537 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
538 + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
539 + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
542 +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
546 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
548 + RT305X_ESW_REG_PVIDC(port / 2),
549 + RT305X_ESW_PVIDC_PVID_M << s,
550 + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
553 +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
557 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
558 + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
559 + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
564 +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
568 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
570 + RT305X_ESW_REG_VMSC(vlan / 4),
571 + RT305X_ESW_VMSC_MSC_M << s,
572 + (msc & RT305X_ESW_VMSC_MSC_M) << s);
575 +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
578 + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
579 + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
580 + RT305X_ESW_POC0_DIS_PORT_M;
583 +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
586 + unsigned enable_mask;
590 + old_mask = esw_get_port_disable(esw);
591 + changed = old_mask ^ disable_mask;
592 + enable_mask = old_mask & disable_mask;
594 + /* enable before writing to MII */
595 + esw_rmw(esw, RT305X_ESW_REG_POC0,
596 + (RT305X_ESW_POC0_DIS_PORT_M <<
597 + RT305X_ESW_POC0_DIS_PORT_S),
598 + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
600 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
601 + if (!(changed & (1 << i)))
603 + if (disable_mask & (1 << i)) {
605 + rt305x_mii_write(esw, i, MII_BMCR,
609 + rt305x_mii_write(esw, i, MII_BMCR,
617 + /* disable after writing to MII */
618 + esw_rmw(esw, RT305X_ESW_REG_POC0,
619 + (RT305X_ESW_POC0_DIS_PORT_M <<
620 + RT305X_ESW_POC0_DIS_PORT_S),
621 + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
624 +static void esw_set_gsc(struct rt305x_esw *esw)
626 + esw_rmw(esw, RT305X_ESW_REG_SGC,
627 + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
628 + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
629 + esw_rmw(esw, RT305X_ESW_REG_SGC,
630 + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
631 + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
634 +static int esw_apply_config(struct switch_dev *dev);
636 +static void esw_hw_init(struct rt305x_esw *esw)
639 + u8 port_disable = 0;
640 + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
642 + /* vodoo from original driver */
643 + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
644 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
645 + /* Port priority 1 for all ports, vlan enabled. */
646 + esw_w32(esw, 0x00005555 |
647 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
648 + RT305X_ESW_REG_PFC1);
650 + /* Enable Back Pressure, and Flow Control */
652 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
653 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
654 + RT305X_ESW_REG_POC0);
656 + /* Enable Aging, and VLAN TAG removal */
658 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
659 + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
660 + RT305X_ESW_REG_POC2);
662 + if (esw->reg_initval_fct2)
663 + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
665 + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
668 + * 300s aging timer, max packet len 1536, broadcast storm prevention
669 + * disabled, disable collision abort, mac xor48 hash, 10 packet back
670 + * pressure jam, GMII disable was_transmit, back pressure disabled,
671 + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
674 + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
676 + /* Setup SoC Port control register */
678 + (RT305X_ESW_SOCPC_CRC_PADDING |
679 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
680 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
681 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
682 + RT305X_ESW_REG_SOCPC);
684 + if (esw->reg_initval_fpa2)
685 + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
687 + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
688 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
690 + /* Force Link/Activity on ports */
691 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
692 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
693 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
694 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
695 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
697 + /* Copy disabled port configuration from bootloader setup */
698 + port_disable = esw_get_port_disable(esw);
699 + for (i = 0; i < 6; i++)
700 + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
702 + if (soc_is_rt3352()) {
704 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
705 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
706 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
708 + rt305x_mii_write(esw, 0, 31, 0x8000);
709 + for (i = 0; i < 5; i++) {
710 + if (esw->ports[i].disable) {
711 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
713 + rt305x_mii_write(esw, i, MII_BMCR,
718 + /* TX10 waveform coefficient LSB=0 disable PHY */
719 + rt305x_mii_write(esw, i, 26, 0x1601);
720 + /* TX100/TX10 AD/DA current bias */
721 + rt305x_mii_write(esw, i, 29, 0x7016);
722 + /* TX100 slew rate control */
723 + rt305x_mii_write(esw, i, 30, 0x0038);
726 + /* select global register */
727 + rt305x_mii_write(esw, 0, 31, 0x0);
728 + /* enlarge agcsel threshold 3 and threshold 2 */
729 + rt305x_mii_write(esw, 0, 1, 0x4a40);
730 + /* enlarge agcsel threshold 5 and threshold 4 */
731 + rt305x_mii_write(esw, 0, 2, 0x6254);
732 + /* enlarge agcsel threshold */
733 + rt305x_mii_write(esw, 0, 3, 0xa17f);
734 + rt305x_mii_write(esw, 0,12, 0x7eaa);
735 + /* longer TP_IDL tail length */
736 + rt305x_mii_write(esw, 0, 14, 0x65);
737 + /* increased squelch pulse count threshold. */
738 + rt305x_mii_write(esw, 0, 16, 0x0684);
739 + /* set TX10 signal amplitude threshold to minimum */
740 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
741 + /* set squelch amplitude to higher threshold */
742 + rt305x_mii_write(esw, 0, 18, 0x40ba);
743 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
744 + rt305x_mii_write(esw, 0, 22, 0x253f);
745 + /* set PLL/Receive bias current are calibrated */
746 + rt305x_mii_write(esw, 0, 27, 0x2fda);
747 + /* change PLL/Receive bias current to internal(RT3350) */
748 + rt305x_mii_write(esw, 0, 28, 0xc410);
749 + /* change PLL bias current to internal(RT3052_MP3) */
750 + rt305x_mii_write(esw, 0, 29, 0x598b);
751 + /* select local register */
752 + rt305x_mii_write(esw, 0, 31, 0x8000);
753 + } else if (soc_is_rt5350()) {
755 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
756 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
757 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
759 + /* set the led polarity */
760 + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
762 + /* local registers */
763 + rt305x_mii_write(esw, 0, 31, 0x8000);
764 + for (i = 0; i < 5; i++) {
765 + if (esw->ports[i].disable) {
766 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
768 + rt305x_mii_write(esw, i, MII_BMCR,
773 + /* TX10 waveform coefficient LSB=0 disable PHY */
774 + rt305x_mii_write(esw, i, 26, 0x1601);
775 + /* TX100/TX10 AD/DA current bias */
776 + rt305x_mii_write(esw, i, 29, 0x7015);
777 + /* TX100 slew rate control */
778 + rt305x_mii_write(esw, i, 30, 0x0038);
781 + /* global registers */
782 + rt305x_mii_write(esw, 0, 31, 0x0);
783 + /* enlarge agcsel threshold 3 and threshold 2 */
784 + rt305x_mii_write(esw, 0, 1, 0x4a40);
785 + /* enlarge agcsel threshold 5 and threshold 4 */
786 + rt305x_mii_write(esw, 0, 2, 0x6254);
787 + /* enlarge agcsel threshold 6 */
788 + rt305x_mii_write(esw, 0, 3, 0xa17f);
789 + rt305x_mii_write(esw, 0, 12, 0x7eaa);
790 + /* longer TP_IDL tail length */
791 + rt305x_mii_write(esw, 0, 14, 0x65);
792 + /* increased squelch pulse count threshold. */
793 + rt305x_mii_write(esw, 0, 16, 0x0684);
794 + /* set TX10 signal amplitude threshold to minimum */
795 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
796 + /* set squelch amplitude to higher threshold */
797 + rt305x_mii_write(esw, 0, 18, 0x40ba);
798 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
799 + rt305x_mii_write(esw, 0, 22, 0x253f);
800 + /* set PLL/Receive bias current are calibrated */
801 + rt305x_mii_write(esw, 0, 27, 0x2fda);
802 + /* change PLL/Receive bias current to internal(RT3350) */
803 + rt305x_mii_write(esw, 0, 28, 0xc410);
804 + /* change PLL bias current to internal(RT3052_MP3) */
805 + rt305x_mii_write(esw, 0, 29, 0x598b);
806 + /* select local register */
807 + rt305x_mii_write(esw, 0, 31, 0x8000);
809 + rt305x_mii_write(esw, 0, 31, 0x8000);
810 + for (i = 0; i < 5; i++) {
811 + if (esw->ports[i].disable) {
812 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
814 + rt305x_mii_write(esw, i, MII_BMCR,
819 + /* TX10 waveform coefficient */
820 + rt305x_mii_write(esw, i, 26, 0x1601);
821 + /* TX100/TX10 AD/DA current bias */
822 + rt305x_mii_write(esw, i, 29, 0x7058);
823 + /* TX100 slew rate control */
824 + rt305x_mii_write(esw, i, 30, 0x0018);
828 + /* select global register */
829 + rt305x_mii_write(esw, 0, 31, 0x0);
830 + /* tune TP_IDL tail and head waveform */
831 + rt305x_mii_write(esw, 0, 22, 0x052f);
832 + /* set TX10 signal amplitude threshold to minimum */
833 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
834 + /* set squelch amplitude to higher threshold */
835 + rt305x_mii_write(esw, 0, 18, 0x40ba);
836 + /* longer TP_IDL tail length */
837 + rt305x_mii_write(esw, 0, 14, 0x65);
838 + /* select local register */
839 + rt305x_mii_write(esw, 0, 31, 0x8000);
843 + port_map = esw->port_map;
845 + port_map = RT305X_ESW_PMAP_LLLLLL;
848 + * Unused HW feature, but still nice to be consistent here...
849 + * This is also exported to userspace ('lan' attribute) so it's
850 + * conveniently usable to decide which ports go into the wan vlan by
853 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
854 + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
855 + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
857 + /* make the switch leds blink */
858 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
859 + esw->ports[i].led = 0x05;
861 + /* Apply the empty config. */
862 + esw_apply_config(&esw->swdev);
864 + /* Only unmask the port change interrupt */
865 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
868 +static irqreturn_t esw_interrupt(int irq, void *_esw)
870 + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
873 + status = esw_r32(esw, RT305X_ESW_REG_ISR);
874 + if (status & RT305X_ESW_PORT_ST_CHG) {
875 + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
876 + link >>= RT305X_ESW_POA_LINK_SHIFT;
877 + link &= RT305X_ESW_POA_LINK_MASK;
878 + dev_info(esw->dev, "link changed 0x%02X\n", link);
880 + esw_w32(esw, status, RT305X_ESW_REG_ISR);
882 + return IRQ_HANDLED;
885 +static int esw_apply_config(struct switch_dev *dev)
887 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
894 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
896 + if (esw->global_vlan_enable) {
897 + vid = esw->vlans[i].vid;
898 + vmsc = esw->vlans[i].ports;
900 + vid = RT305X_ESW_VLAN_NONE;
901 + vmsc = RT305X_ESW_PORTS_NONE;
903 + esw_set_vlan_id(esw, i, vid);
904 + esw_set_vmsc(esw, i, vmsc);
907 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
909 + disable |= esw->ports[i].disable << i;
910 + if (esw->global_vlan_enable) {
911 + doubletag |= esw->ports[i].doubletag << i;
913 + untag |= esw->ports[i].untag << i;
914 + pvid = esw->ports[i].pvid;
916 + int x = esw->alt_vlan_disable ? 0 : 1;
917 + doubletag |= x << i;
922 + esw_set_pvid(esw, i, pvid);
923 + if (i < RT305X_ESW_NUM_LEDS)
924 + esw_w32(esw, esw->ports[i].led,
925 + RT305X_ESW_REG_P0LED + 4*i);
929 + esw_set_port_disable(esw, disable);
930 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
931 + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
932 + RT305X_ESW_SGC2_DOUBLE_TAG_S),
933 + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
934 + esw_rmw(esw, RT305X_ESW_REG_PFC1,
935 + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
936 + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
937 + esw_rmw(esw, RT305X_ESW_REG_POC2,
938 + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
939 + untag << RT305X_ESW_POC2_UNTAG_EN_S);
941 + if (!esw->global_vlan_enable) {
943 + * Still need to put all ports into vlan 0 or they'll be
945 + * NOTE: vlan 0 is special, no vlan tag is prepended
947 + esw_set_vlan_id(esw, 0, 0);
948 + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
954 +static int esw_reset_switch(struct switch_dev *dev)
956 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
958 + esw->global_vlan_enable = 0;
959 + memset(esw->ports, 0, sizeof(esw->ports));
960 + memset(esw->vlans, 0, sizeof(esw->vlans));
966 +static int esw_get_vlan_enable(struct switch_dev *dev,
967 + const struct switch_attr *attr,
968 + struct switch_val *val)
970 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
972 + val->value.i = esw->global_vlan_enable;
977 +static int esw_set_vlan_enable(struct switch_dev *dev,
978 + const struct switch_attr *attr,
979 + struct switch_val *val)
981 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
983 + esw->global_vlan_enable = val->value.i != 0;
988 +static int esw_get_alt_vlan_disable(struct switch_dev *dev,
989 + const struct switch_attr *attr,
990 + struct switch_val *val)
992 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
994 + val->value.i = esw->alt_vlan_disable;
999 +static int esw_set_alt_vlan_disable(struct switch_dev *dev,
1000 + const struct switch_attr *attr,
1001 + struct switch_val *val)
1003 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1005 + esw->alt_vlan_disable = val->value.i != 0;
1011 +rt305x_esw_set_bc_status(struct switch_dev *dev,
1012 + const struct switch_attr *attr,
1013 + struct switch_val *val)
1015 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1017 + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
1023 +rt305x_esw_get_bc_status(struct switch_dev *dev,
1024 + const struct switch_attr *attr,
1025 + struct switch_val *val)
1027 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1029 + val->value.i = esw->bc_storm_protect;
1035 +rt305x_esw_set_led_freq(struct switch_dev *dev,
1036 + const struct switch_attr *attr,
1037 + struct switch_val *val)
1039 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1041 + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
1047 +rt305x_esw_get_led_freq(struct switch_dev *dev,
1048 + const struct switch_attr *attr,
1049 + struct switch_val *val)
1051 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1053 + val->value.i = esw->led_frequency;
1058 +static int esw_get_port_link(struct switch_dev *dev,
1060 + struct switch_port_link *link)
1062 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1065 + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
1068 + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
1070 + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
1071 + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
1072 + if (port < RT305X_ESW_NUM_LEDS) {
1073 + speed = (poa >> RT305X_ESW_SPD_S) & 1;
1075 + if (port == RT305X_ESW_NUM_PORTS - 1)
1077 + speed = (poa >> RT305X_ESW_SPD_S) & 3;
1081 + link->speed = SWITCH_PORT_SPEED_10;
1084 + link->speed = SWITCH_PORT_SPEED_100;
1087 + case 3: /* forced gige speed can be 2 or 3 */
1088 + link->speed = SWITCH_PORT_SPEED_1000;
1091 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
1098 +static int esw_get_port_bool(struct switch_dev *dev,
1099 + const struct switch_attr *attr,
1100 + struct switch_val *val)
1102 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1103 + int idx = val->port_vlan;
1104 + u32 x, reg, shift;
1106 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
1109 + switch (attr->id) {
1110 + case RT305X_ESW_ATTR_PORT_DISABLE:
1111 + reg = RT305X_ESW_REG_POC0;
1112 + shift = RT305X_ESW_POC0_DIS_PORT_S;
1114 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1115 + reg = RT305X_ESW_REG_SGC2;
1116 + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
1118 + case RT305X_ESW_ATTR_PORT_UNTAG:
1119 + reg = RT305X_ESW_REG_POC2;
1120 + shift = RT305X_ESW_POC2_UNTAG_EN_S;
1122 + case RT305X_ESW_ATTR_PORT_LAN:
1123 + reg = RT305X_ESW_REG_SGC2;
1124 + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
1125 + if (idx >= RT305X_ESW_NUM_LANWAN)
1132 + x = esw_r32(esw, reg);
1133 + val->value.i = (x >> (idx + shift)) & 1;
1138 +static int esw_set_port_bool(struct switch_dev *dev,
1139 + const struct switch_attr *attr,
1140 + struct switch_val *val)
1142 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1143 + int idx = val->port_vlan;
1145 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1146 + val->value.i < 0 || val->value.i > 1)
1149 + switch (attr->id) {
1150 + case RT305X_ESW_ATTR_PORT_DISABLE:
1151 + esw->ports[idx].disable = val->value.i;
1153 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1154 + esw->ports[idx].doubletag = val->value.i;
1156 + case RT305X_ESW_ATTR_PORT_UNTAG:
1157 + esw->ports[idx].untag = val->value.i;
1166 +static int esw_get_port_recv_badgood(struct switch_dev *dev,
1167 + const struct switch_attr *attr,
1168 + struct switch_val *val)
1170 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1171 + int idx = val->port_vlan;
1172 + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
1175 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1177 + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
1178 + val->value.i = (reg >> shift) & 0xffff;
1184 +esw_get_port_tr_badgood(struct switch_dev *dev,
1185 + const struct switch_attr *attr,
1186 + struct switch_val *val)
1188 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1190 + int idx = val->port_vlan;
1191 + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
1194 + if (!soc_is_rt5350())
1197 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1200 + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
1201 + val->value.i = (reg >> shift) & 0xffff;
1206 +static int esw_get_port_led(struct switch_dev *dev,
1207 + const struct switch_attr *attr,
1208 + struct switch_val *val)
1210 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1211 + int idx = val->port_vlan;
1213 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1214 + idx >= RT305X_ESW_NUM_LEDS)
1217 + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
1222 +static int esw_set_port_led(struct switch_dev *dev,
1223 + const struct switch_attr *attr,
1224 + struct switch_val *val)
1226 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1227 + int idx = val->port_vlan;
1229 + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
1232 + esw->ports[idx].led = val->value.i;
1237 +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1239 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1241 + if (port >= RT305X_ESW_NUM_PORTS)
1244 + *val = esw_get_pvid(esw, port);
1249 +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
1251 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1253 + if (port >= RT305X_ESW_NUM_PORTS)
1256 + esw->ports[port].pvid = val;
1261 +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1263 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1265 + int vlan_idx = -1;
1270 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
1274 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1275 + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
1276 + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
1282 + if (vlan_idx == -1)
1285 + vmsc = esw_get_vmsc(esw, vlan_idx);
1286 + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
1288 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
1289 + struct switch_port *p;
1290 + int port_mask = 1 << i;
1292 + if (!(vmsc & port_mask))
1295 + p = &val->value.ports[val->len++];
1297 + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
1300 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
1306 +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1308 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1310 + int vlan_idx = -1;
1313 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
1314 + val->len > RT305X_ESW_NUM_PORTS)
1317 + /* one of the already defined vlans? */
1318 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1319 + if (esw->vlans[i].vid == val->port_vlan &&
1320 + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
1326 + /* select a free slot */
1327 + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
1328 + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
1332 + /* bail if all slots are in use */
1333 + if (vlan_idx == -1)
1336 + ports = RT305X_ESW_PORTS_NONE;
1337 + for (i = 0; i < val->len; i++) {
1338 + struct switch_port *p = &val->value.ports[i];
1339 + int port_mask = 1 << p->id;
1340 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
1342 + if (p->id >= RT305X_ESW_NUM_PORTS)
1345 + ports |= port_mask;
1346 + esw->ports[p->id].untag = untagged;
1348 + esw->vlans[vlan_idx].ports = ports;
1349 + if (ports == RT305X_ESW_PORTS_NONE)
1350 + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
1352 + esw->vlans[vlan_idx].vid = val->port_vlan;
1357 +static const struct switch_attr esw_global[] = {
1359 + .type = SWITCH_TYPE_INT,
1360 + .name = "enable_vlan",
1361 + .description = "VLAN mode (1:enabled)",
1363 + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
1364 + .get = esw_get_vlan_enable,
1365 + .set = esw_set_vlan_enable,
1368 + .type = SWITCH_TYPE_INT,
1369 + .name = "alternate_vlan_disable",
1370 + .description = "Use en_vlan instead of doubletag to disable"
1373 + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
1374 + .get = esw_get_alt_vlan_disable,
1375 + .set = esw_set_alt_vlan_disable,
1378 + .type = SWITCH_TYPE_INT,
1379 + .name = "bc_storm_protect",
1380 + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1382 + .id = RT305X_ESW_ATTR_BC_STATUS,
1383 + .get = rt305x_esw_get_bc_status,
1384 + .set = rt305x_esw_set_bc_status,
1387 + .type = SWITCH_TYPE_INT,
1388 + .name = "led_frequency",
1389 + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1391 + .id = RT305X_ESW_ATTR_LED_FREQ,
1392 + .get = rt305x_esw_get_led_freq,
1393 + .set = rt305x_esw_set_led_freq,
1397 +static const struct switch_attr esw_port[] = {
1399 + .type = SWITCH_TYPE_INT,
1400 + .name = "disable",
1401 + .description = "Port state (1:disabled)",
1403 + .id = RT305X_ESW_ATTR_PORT_DISABLE,
1404 + .get = esw_get_port_bool,
1405 + .set = esw_set_port_bool,
1408 + .type = SWITCH_TYPE_INT,
1409 + .name = "doubletag",
1410 + .description = "Double tagging for incoming vlan packets "
1413 + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
1414 + .get = esw_get_port_bool,
1415 + .set = esw_set_port_bool,
1418 + .type = SWITCH_TYPE_INT,
1420 + .description = "Untag (1:strip outgoing vlan tag)",
1422 + .id = RT305X_ESW_ATTR_PORT_UNTAG,
1423 + .get = esw_get_port_bool,
1424 + .set = esw_set_port_bool,
1427 + .type = SWITCH_TYPE_INT,
1429 + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1430 + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1431 + " 8:100mact, 10:blink, 11:off, 12:on)",
1433 + .id = RT305X_ESW_ATTR_PORT_LED,
1434 + .get = esw_get_port_led,
1435 + .set = esw_set_port_led,
1438 + .type = SWITCH_TYPE_INT,
1440 + .description = "HW port group (0:wan, 1:lan)",
1442 + .id = RT305X_ESW_ATTR_PORT_LAN,
1443 + .get = esw_get_port_bool,
1446 + .type = SWITCH_TYPE_INT,
1447 + .name = "recv_bad",
1448 + .description = "Receive bad packet counter",
1449 + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
1450 + .get = esw_get_port_recv_badgood,
1453 + .type = SWITCH_TYPE_INT,
1454 + .name = "recv_good",
1455 + .description = "Receive good packet counter",
1456 + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
1457 + .get = esw_get_port_recv_badgood,
1460 + .type = SWITCH_TYPE_INT,
1463 + .description = "Transmit bad packet counter. rt5350 only",
1464 + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
1465 + .get = esw_get_port_tr_badgood,
1468 + .type = SWITCH_TYPE_INT,
1469 + .name = "tr_good",
1471 + .description = "Transmit good packet counter. rt5350 only",
1472 + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
1473 + .get = esw_get_port_tr_badgood,
1477 +static const struct switch_attr esw_vlan[] = {
1480 +static const struct switch_dev_ops esw_ops = {
1482 + .attr = esw_global,
1483 + .n_attr = ARRAY_SIZE(esw_global),
1487 + .n_attr = ARRAY_SIZE(esw_port),
1491 + .n_attr = ARRAY_SIZE(esw_vlan),
1493 + .get_vlan_ports = esw_get_vlan_ports,
1494 + .set_vlan_ports = esw_set_vlan_ports,
1495 + .get_port_pvid = esw_get_port_pvid,
1496 + .set_port_pvid = esw_set_port_pvid,
1497 + .get_port_link = esw_get_port_link,
1498 + .apply_config = esw_apply_config,
1499 + .reset_switch = esw_reset_switch,
1502 +static struct rt305x_esw_platform_data rt3050_esw_data = {
1503 + /* All ports are LAN ports. */
1504 + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
1505 + .reg_initval_fct2 = 0x00d6500c,
1507 + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1508 + * turbo mii off, rgmi 3.3v off
1510 + * port6: enabled, gige, full-duplex, rx/tx-flow-control
1512 + .reg_initval_fpa2 = 0x3f502b28,
1515 +static const struct of_device_id ralink_esw_match[] = {
1516 + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
1519 +MODULE_DEVICE_TABLE(of, ralink_esw_match);
1521 +static int esw_probe(struct platform_device *pdev)
1523 + struct device_node *np = pdev->dev.of_node;
1524 + const struct rt305x_esw_platform_data *pdata;
1525 + const __be32 *port_map, *reg_init;
1526 + struct rt305x_esw *esw;
1527 + struct switch_dev *swdev;
1528 + struct resource *res, *irq;
1531 + pdata = pdev->dev.platform_data;
1533 + const struct of_device_id *match;
1534 + match = of_match_device(ralink_esw_match, &pdev->dev);
1536 + pdata = (struct rt305x_esw_platform_data *) match->data;
1541 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1543 + dev_err(&pdev->dev, "no memory resource found\n");
1547 + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1549 + dev_err(&pdev->dev, "no irq resource found\n");
1553 + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
1555 + dev_err(&pdev->dev, "no memory for private data\n");
1559 + esw->dev = &pdev->dev;
1560 + esw->irq = irq->start;
1561 + esw->base = ioremap(res->start, resource_size(res));
1563 + dev_err(&pdev->dev, "ioremap failed\n");
1568 + port_map = of_get_property(np, "ralink,portmap", NULL);
1570 + esw->port_map = be32_to_cpu(*port_map);
1572 + reg_init = of_get_property(np, "ralink,fct2", NULL);
1574 + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
1576 + reg_init = of_get_property(np, "ralink,fpa2", NULL);
1578 + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
1580 + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
1582 + esw->reg_led_polarity = be32_to_cpu(*reg_init);
1584 + swdev = &esw->swdev;
1585 + swdev->of_node = pdev->dev.of_node;
1586 + swdev->name = "rt305x-esw";
1587 + swdev->alias = "rt305x";
1588 + swdev->cpu_port = RT305X_ESW_PORT6;
1589 + swdev->ports = RT305X_ESW_NUM_PORTS;
1590 + swdev->vlans = RT305X_ESW_NUM_VIDS;
1591 + swdev->ops = &esw_ops;
1593 + err = register_switch(swdev, NULL);
1595 + dev_err(&pdev->dev, "register_switch failed\n");
1599 + platform_set_drvdata(pdev, esw);
1601 + esw->pdata = pdata;
1602 + spin_lock_init(&esw->reg_rw_lock);
1606 + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
1607 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
1608 + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
1613 + iounmap(esw->base);
1619 +static int esw_remove(struct platform_device *pdev)
1621 + struct rt305x_esw *esw;
1623 + esw = platform_get_drvdata(pdev);
1625 + unregister_switch(&esw->swdev);
1626 + platform_set_drvdata(pdev, NULL);
1627 + iounmap(esw->base);
1634 +static struct platform_driver esw_driver = {
1635 + .probe = esw_probe,
1636 + .remove = esw_remove,
1638 + .name = "rt305x-esw",
1639 + .owner = THIS_MODULE,
1640 + .of_match_table = ralink_esw_match,
1644 +int __init rtesw_init(void)
1646 + return platform_driver_register(&esw_driver);
1649 +void rtesw_exit(void)
1651 + platform_driver_unregister(&esw_driver);
1653 diff --git a/drivers/net/ethernet/ralink/esw_rt3052.h b/drivers/net/ethernet/ralink/esw_rt3052.h
1654 new file mode 100644
1655 index 0000000..2ced3dff
1657 +++ b/drivers/net/ethernet/ralink/esw_rt3052.h
1660 + * This program is free software; you can redistribute it and/or modify
1661 + * it under the terms of the GNU General Public License as published by
1662 + * the Free Software Foundation; version 2 of the License
1664 + * This program is distributed in the hope that it will be useful,
1665 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1666 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1667 + * GNU General Public License for more details.
1669 + * You should have received a copy of the GNU General Public License
1670 + * along with this program; if not, write to the Free Software
1671 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1673 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1676 +#ifndef _RALINK_ESW_RT3052_H__
1677 +#define _RALINK_ESW_RT3052_H__
1679 +#ifdef CONFIG_NET_RALINK_ESW_RT3052
1681 +int __init rtesw_init(void);
1682 +void rtesw_exit(void);
1686 +static inline int __init rtesw_init(void) { return 0; }
1687 +static inline void rtesw_exit(void) { }
1691 diff --git a/drivers/net/ethernet/ralink/gsw_mt7620a.c b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1692 new file mode 100644
1693 index 0000000..70f81e3
1695 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1698 + * This program is free software; you can redistribute it and/or modify
1699 + * it under the terms of the GNU General Public License as published by
1700 + * the Free Software Foundation; version 2 of the License
1702 + * This program is distributed in the hope that it will be useful,
1703 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1704 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1705 + * GNU General Public License for more details.
1707 + * You should have received a copy of the GNU General Public License
1708 + * along with this program; if not, write to the Free Software
1709 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1711 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1714 +#include <linux/module.h>
1715 +#include <linux/kernel.h>
1716 +#include <linux/types.h>
1717 +#include <linux/dma-mapping.h>
1718 +#include <linux/init.h>
1719 +#include <linux/skbuff.h>
1720 +#include <linux/etherdevice.h>
1721 +#include <linux/ethtool.h>
1722 +#include <linux/platform_device.h>
1723 +#include <linux/of_device.h>
1724 +#include <linux/clk.h>
1725 +#include <linux/of_net.h>
1726 +#include <linux/of_mdio.h>
1727 +#include <linux/of_irq.h>
1728 +#include <linux/of_address.h>
1729 +#include <linux/switch.h>
1731 +#include <asm/mach-ralink/ralink_regs.h>
1733 +#include "ralink_soc_eth.h"
1735 +#include <linux/ioport.h>
1736 +#include <linux/switch.h>
1737 +#include <linux/mii.h>
1739 +#include <ralink_regs.h>
1740 +#include <asm/mach-ralink/mt7620.h>
1742 +#include "ralink_soc_eth.h"
1743 +#include "gsw_mt7620a.h"
1744 +#include "mt7530.h"
1747 +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
1749 +#define MT7620A_GSW_REG_PIAC 0x7004
1751 +#define GSW_NUM_VLANS 16
1752 +#define GSW_NUM_VIDS 4096
1753 +#define GSW_NUM_PORTS 7
1754 +#define GSW_PORT6 6
1756 +#define GSW_MDIO_ACCESS BIT(31)
1757 +#define GSW_MDIO_READ BIT(19)
1758 +#define GSW_MDIO_WRITE BIT(18)
1759 +#define GSW_MDIO_START BIT(16)
1760 +#define GSW_MDIO_ADDR_SHIFT 20
1761 +#define GSW_MDIO_REG_SHIFT 25
1763 +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
1764 +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
1765 +#define GSW_REG_SMACCR0 0x3fE4
1766 +#define GSW_REG_SMACCR1 0x3fE8
1767 +#define GSW_REG_CKGCR 0x3ff0
1769 +#define GSW_REG_IMR 0x7008
1770 +#define GSW_REG_ISR 0x700c
1772 +#define SYSC_REG_CFG1 0x14
1774 +#define PORT_IRQ_ST_CHG 0x7f
1776 +#define SYSCFG1 0x14
1778 +#define ESW_PHY_POLLING 0x7000
1780 +#define PMCR_IPG BIT(18)
1781 +#define PMCR_MAC_MODE BIT(16)
1782 +#define PMCR_FORCE BIT(15)
1783 +#define PMCR_TX_EN BIT(14)
1784 +#define PMCR_RX_EN BIT(13)
1785 +#define PMCR_BACKOFF BIT(9)
1786 +#define PMCR_BACKPRES BIT(8)
1787 +#define PMCR_RX_FC BIT(5)
1788 +#define PMCR_TX_FC BIT(4)
1789 +#define PMCR_SPEED(_x) (_x << 2)
1790 +#define PMCR_DUPLEX BIT(1)
1791 +#define PMCR_LINK BIT(0)
1793 +#define PHY_AN_EN BIT(31)
1794 +#define PHY_PRE_EN BIT(30)
1795 +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
1798 + /* Global attributes. */
1799 + GSW_ATTR_ENABLE_VLAN,
1800 + /* Port attributes. */
1801 + GSW_ATTR_PORT_UNTAG,
1809 +struct mt7620_gsw {
1810 + struct device *dev;
1811 + void __iomem *base;
1814 + long unsigned int autopoll;
1817 +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
1819 + iowrite32(val, gsw->base + reg);
1822 +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
1824 + return ioread32(gsw->base + reg);
1827 +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
1829 + unsigned long t_start = jiffies;
1832 + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
1834 + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
1839 + printk(KERN_ERR "mdio: MDIO timeout\n");
1843 +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
1846 + if (mt7620_mii_busy_wait(gsw))
1849 + write_data &= 0xffff;
1851 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
1852 + (phy_register << GSW_MDIO_REG_SHIFT) |
1853 + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
1854 + MT7620A_GSW_REG_PIAC);
1856 + if (mt7620_mii_busy_wait(gsw))
1862 +static u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg)
1866 + if (mt7620_mii_busy_wait(gsw))
1869 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
1870 + (phy_reg << GSW_MDIO_REG_SHIFT) |
1871 + (phy_addr << GSW_MDIO_ADDR_SHIFT),
1872 + MT7620A_GSW_REG_PIAC);
1874 + if (mt7620_mii_busy_wait(gsw))
1877 + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
1882 +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
1884 + struct fe_priv *priv = bus->priv;
1885 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1887 + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
1890 +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
1892 + struct fe_priv *priv = bus->priv;
1893 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1895 + return _mt7620_mii_read(gsw, phy_addr, phy_reg);
1898 +static unsigned char *fe_speed_str(int speed)
1915 +int mt7620a_has_carrier(struct fe_priv *priv)
1917 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1920 + for (i = 0; i < GSW_PORT6; i++)
1921 + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
1926 +static void mt7620a_handle_carrier(struct fe_priv *priv)
1931 + if (mt7620a_has_carrier(priv))
1932 + netif_carrier_on(priv->netdev);
1934 + netif_carrier_off(priv->netdev);
1937 +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
1939 + if (priv->link[port])
1940 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1941 + port, fe_speed_str(priv->phy->speed[port]),
1942 + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
1944 + netdev_info(priv->netdev, "port %d link down\n", port);
1945 + mt7620a_handle_carrier(priv);
1948 +static irqreturn_t gsw_interrupt(int irq, void *_priv)
1950 + struct fe_priv *priv = (struct fe_priv *) _priv;
1951 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1953 + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
1955 + status = gsw_r32(gsw, GSW_REG_ISR);
1956 + if (status & PORT_IRQ_ST_CHG)
1957 + for (i = 0; i <= max; i++) {
1958 + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
1959 + int link = status & 0x1;
1961 + if (link != priv->link[i]) {
1963 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1964 + i, fe_speed_str((status >> 2) & 3),
1965 + (status & 0x2) ? "Full" : "Half");
1967 + netdev_info(priv->netdev, "port %d link down\n", i);
1970 + priv->link[i] = link;
1972 + mt7620a_handle_carrier(priv);
1974 + gsw_w32(gsw, status, GSW_REG_ISR);
1976 + return IRQ_HANDLED;
1979 +static int mt7620_is_bga(void)
1981 + u32 bga = rt_sysc_r32(0x0c);
1983 + return (bga >> 16) & 1;
1986 +static void gsw_auto_poll(struct mt7620_gsw *gsw)
1989 + int lsb = -1, msb = 0;
1991 + for_each_set_bit(phy, &gsw->autopoll, 32) {
1997 + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
2000 +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
2002 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2003 + const __be32 *_id = of_get_property(np, "reg", NULL);
2004 + int phy_mode, size, id;
2006 + u32 val, mask = 0;
2007 + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
2009 + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
2011 + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
2013 + pr_err("%s: invalid port id\n", np->name);
2017 + id = be32_to_cpu(*_id);
2022 + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
2023 + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
2024 + pr_err("%s: invalid fixed link property\n", np->name);
2025 + priv->phy->phy_fixed[id] = NULL;
2029 + phy_mode = of_get_phy_mode(np);
2030 + switch (phy_mode) {
2031 + case PHY_INTERFACE_MODE_RGMII:
2034 + case PHY_INTERFACE_MODE_MII:
2037 + case PHY_INTERFACE_MODE_RMII:
2041 + dev_err(priv->device, "port %d - invalid phy mode\n", id);
2045 + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
2046 + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
2049 + val = rt_sysc_r32(SYSCFG1);
2050 + val &= ~(3 << shift);
2051 + val |= mask << shift;
2052 + rt_sysc_w32(val, SYSCFG1);
2054 + if (priv->phy->phy_fixed[id]) {
2055 + const __be32 *link = priv->phy->phy_fixed[id];
2059 + priv->phy->speed[id] = be32_to_cpup(link++);
2060 + tx_fc = be32_to_cpup(link++);
2061 + rx_fc = be32_to_cpup(link++);
2062 + priv->phy->duplex[id] = be32_to_cpup(link++);
2063 + priv->link[id] = 1;
2065 + switch (priv->phy->speed[id]) {
2076 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
2077 + priv->phy->phy_fixed[id] = 0;
2080 + val = PMCR_SPEED(val);
2081 + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2082 + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
2084 + val |= PMCR_TX_FC;
2086 + val |= PMCR_RX_FC;
2087 + if (priv->phy->duplex[id])
2088 + val |= PMCR_DUPLEX;
2089 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2090 + dev_info(priv->device, "using fixed link parameters\n");
2094 + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
2095 + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2096 + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
2098 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2099 + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
2100 + gsw->autopoll |= BIT(id);
2101 + gsw_auto_poll(gsw);
2106 +static void gsw_hw_init(struct mt7620_gsw *gsw)
2108 + u32 is_BGA = mt7620_is_bga();
2110 + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
2111 + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
2113 + /*correct PHY setting L3.0 BGA*/
2114 + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
2116 + _mt7620_mii_write(gsw, 1, 17, 0x7444);
2118 + _mt7620_mii_write(gsw, 1, 19, 0x0114);
2120 + _mt7620_mii_write(gsw, 1, 19, 0x0117);
2122 + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
2123 + _mt7620_mii_write(gsw, 1, 25, 0x6212);
2124 + _mt7620_mii_write(gsw, 1, 26, 0x0777);
2125 + _mt7620_mii_write(gsw, 1, 29, 0x4000);
2126 + _mt7620_mii_write(gsw, 1, 28, 0xc077);
2127 + _mt7620_mii_write(gsw, 1, 24, 0x0000);
2129 + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
2130 + _mt7620_mii_write(gsw, 1, 17, 0x4838);
2132 + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
2134 + _mt7620_mii_write(gsw, 1, 21, 0x0515);
2135 + _mt7620_mii_write(gsw, 1, 22, 0x0053);
2136 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2137 + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
2138 + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
2139 + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
2141 + _mt7620_mii_write(gsw, 1, 21, 0x0517);
2142 + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
2143 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2144 + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
2145 + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
2146 + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
2148 + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
2149 + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
2151 + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
2152 + _mt7620_mii_write(gsw, 0, 30, 0xa000);
2153 + _mt7620_mii_write(gsw, 1, 30, 0xa000);
2154 + _mt7620_mii_write(gsw, 2, 30, 0xa000);
2155 + _mt7620_mii_write(gsw, 3, 30, 0xa000);
2157 + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
2158 + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
2159 + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
2160 + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
2161 + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
2162 + _mt7620_mii_write(gsw, 0, 16, 0x1111);
2163 + _mt7620_mii_write(gsw, 1, 16, 0x1010);
2164 + _mt7620_mii_write(gsw, 2, 16, 0x1515);
2165 + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
2167 + /* CPU Port6 Force Link 1G, FC ON */
2168 + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
2169 + /* Set Port6 CPU Port */
2170 + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
2172 + /* setup port 4 */
2173 + if (gsw->port4 == PORT4_EPHY) {
2174 + u32 val = rt_sysc_r32(SYSCFG1);
2176 + rt_sysc_w32(val, SYSCFG1);
2177 + _mt7620_mii_write(gsw, 4, 30, 0xa000);
2178 + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
2179 + _mt7620_mii_write(gsw, 4, 16, 0x1313);
2180 + pr_info("gsw: setting port4 to ephy mode\n");
2184 +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
2186 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2187 + unsigned long flags;
2189 + spin_lock_irqsave(&priv->page_lock, flags);
2190 + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
2191 + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
2193 + spin_unlock_irqrestore(&priv->page_lock, flags);
2196 +static struct of_device_id gsw_match[] = {
2197 + { .compatible = "ralink,mt7620a-gsw" },
2201 +int mt7620_gsw_config(struct fe_priv *priv)
2203 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2205 + /* is the mt7530 internal or external */
2206 + if ((_mt7620_mii_read(gsw, 0x1f, 2) == 1) && (_mt7620_mii_read(gsw, 0x1f, 3) == 0xbeef))
2207 + mt7530_probe(priv->device, NULL, priv->mii_bus);
2209 + mt7530_probe(priv->device, gsw->base, NULL);
2214 +int mt7620_gsw_probe(struct fe_priv *priv)
2216 + struct mt7620_gsw *gsw;
2217 + struct device_node *np;
2218 + const char *port4 = NULL;
2220 + np = of_find_matching_node(NULL, gsw_match);
2222 + dev_err(priv->device, "no gsw node found\n");
2225 + np = of_node_get(np);
2227 + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
2229 + dev_err(priv->device, "no gsw memory for private data\n");
2233 + gsw->irq = irq_of_parse_and_map(np, 0);
2235 + dev_err(priv->device, "no gsw irq resource found\n");
2239 + gsw->base = of_iomap(np, 0);
2241 + dev_err(priv->device, "gsw ioremap failed\n");
2245 + gsw->dev = priv->device;
2246 + priv->soc->swpriv = gsw;
2248 + of_property_read_string(np, "ralink,port4", &port4);
2249 + if (port4 && !strcmp(port4, "ephy"))
2250 + gsw->port4 = PORT4_EPHY;
2251 + else if (port4 && !strcmp(port4, "gmac"))
2252 + gsw->port4 = PORT4_EXT;
2258 + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
2259 + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
2263 diff --git a/drivers/net/ethernet/ralink/gsw_mt7620a.h b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2264 new file mode 100644
2265 index 0000000..c87761a
2267 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2270 + * This program is free software; you can redistribute it and/or modify
2271 + * it under the terms of the GNU General Public License as published by
2272 + * the Free Software Foundation; version 2 of the License
2274 + * This program is distributed in the hope that it will be useful,
2275 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2276 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2277 + * GNU General Public License for more details.
2279 + * You should have received a copy of the GNU General Public License
2280 + * along with this program; if not, write to the Free Software
2281 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2283 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2286 +#ifndef _RALINK_GSW_MT7620_H__
2287 +#define _RALINK_GSW_MT7620_H__
2289 +extern int mt7620_gsw_config(struct fe_priv *priv);
2290 +extern int mt7620_gsw_probe(struct fe_priv *priv);
2291 +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
2292 +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2293 +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2294 +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
2295 +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
2296 +extern int mt7620a_has_carrier(struct fe_priv *priv);
2299 diff --git a/drivers/net/ethernet/ralink/mdio.c b/drivers/net/ethernet/ralink/mdio.c
2300 new file mode 100644
2301 index 0000000..32bf98b
2303 +++ b/drivers/net/ethernet/ralink/mdio.c
2306 + * This program is free software; you can redistribute it and/or modify
2307 + * it under the terms of the GNU General Public License as published by
2308 + * the Free Software Foundation; version 2 of the License
2310 + * This program is distributed in the hope that it will be useful,
2311 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2312 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2313 + * GNU General Public License for more details.
2315 + * You should have received a copy of the GNU General Public License
2316 + * along with this program; if not, write to the Free Software
2317 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2319 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2322 +#include <linux/module.h>
2323 +#include <linux/kernel.h>
2324 +#include <linux/types.h>
2325 +#include <linux/dma-mapping.h>
2326 +#include <linux/init.h>
2327 +#include <linux/skbuff.h>
2328 +#include <linux/etherdevice.h>
2329 +#include <linux/ethtool.h>
2330 +#include <linux/platform_device.h>
2331 +#include <linux/phy.h>
2332 +#include <linux/of_device.h>
2333 +#include <linux/clk.h>
2334 +#include <linux/of_net.h>
2335 +#include <linux/of_mdio.h>
2337 +#include "ralink_soc_eth.h"
2340 +static int fe_mdio_reset(struct mii_bus *bus)
2346 +static void fe_phy_link_adjust(struct net_device *dev)
2348 + struct fe_priv *priv = netdev_priv(dev);
2349 + unsigned long flags;
2352 + spin_lock_irqsave(&priv->phy->lock, flags);
2353 + for (i = 0; i < 8; i++) {
2354 + if (priv->phy->phy_node[i]) {
2355 + struct phy_device *phydev = priv->phy->phy[i];
2356 + int status_change = 0;
2359 + if (priv->phy->duplex[i] != phydev->duplex ||
2360 + priv->phy->speed[i] != phydev->speed)
2361 + status_change = 1;
2363 + if (phydev->link != priv->link[i])
2364 + status_change = 1;
2366 + switch (phydev->speed) {
2370 + priv->link[i] = phydev->link;
2371 + priv->phy->duplex[i] = phydev->duplex;
2372 + priv->phy->speed[i] = phydev->speed;
2374 + if (status_change && priv->soc->mdio_adjust_link)
2375 + priv->soc->mdio_adjust_link(priv, i);
2382 +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
2384 + const __be32 *_port = NULL;
2385 + struct phy_device *phydev;
2386 + int phy_mode, port;
2388 + _port = of_get_property(phy_node, "reg", NULL);
2390 + if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
2391 + pr_err("%s: invalid port id\n", phy_node->name);
2394 + port = be32_to_cpu(*_port);
2395 + phy_mode = of_get_phy_mode(phy_node);
2396 + if (phy_mode < 0) {
2397 + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
2398 + priv->phy->phy_node[port] = NULL;
2402 + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
2404 + if (IS_ERR(phydev)) {
2405 + dev_err(priv->device, "could not connect to PHY\n");
2406 + priv->phy->phy_node[port] = NULL;
2407 + return PTR_ERR(phydev);
2410 + phydev->supported &= PHY_GBIT_FEATURES;
2411 + phydev->advertising = phydev->supported;
2412 + phydev->no_auto_carrier_off = 1;
2414 + dev_info(priv->device,
2415 + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
2416 + port, dev_name(&phydev->dev), phydev->phy_id,
2417 + phydev->drv->name);
2419 + priv->phy->phy[port] = phydev;
2420 + priv->link[port] = 0;
2425 +static int fe_phy_connect(struct fe_priv *priv)
2430 +static void fe_phy_disconnect(struct fe_priv *priv)
2432 + unsigned long flags;
2435 + for (i = 0; i < 8; i++)
2436 + if (priv->phy->phy_fixed[i]) {
2437 + spin_lock_irqsave(&priv->phy->lock, flags);
2438 + priv->link[i] = 0;
2439 + if (priv->soc->mdio_adjust_link)
2440 + priv->soc->mdio_adjust_link(priv, i);
2441 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2442 + } else if (priv->phy->phy[i]) {
2443 + phy_disconnect(priv->phy->phy[i]);
2447 +static void fe_phy_start(struct fe_priv *priv)
2449 + unsigned long flags;
2452 + for (i = 0; i < 8; i++) {
2453 + if (priv->phy->phy_fixed[i]) {
2454 + spin_lock_irqsave(&priv->phy->lock, flags);
2455 + priv->link[i] = 1;
2456 + if (priv->soc->mdio_adjust_link)
2457 + priv->soc->mdio_adjust_link(priv, i);
2458 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2459 + } else if (priv->phy->phy[i]) {
2460 + phy_start(priv->phy->phy[i]);
2465 +static void fe_phy_stop(struct fe_priv *priv)
2467 + unsigned long flags;
2470 + for (i = 0; i < 8; i++)
2471 + if (priv->phy->phy_fixed[i]) {
2472 + spin_lock_irqsave(&priv->phy->lock, flags);
2473 + priv->link[i] = 0;
2474 + if (priv->soc->mdio_adjust_link)
2475 + priv->soc->mdio_adjust_link(priv, i);
2476 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2477 + } else if (priv->phy->phy[i]) {
2478 + phy_stop(priv->phy->phy[i]);
2482 +static struct fe_phy phy_ralink = {
2483 + .connect = fe_phy_connect,
2484 + .disconnect = fe_phy_disconnect,
2485 + .start = fe_phy_start,
2486 + .stop = fe_phy_stop,
2489 +int fe_mdio_init(struct fe_priv *priv)
2491 + struct device_node *mii_np;
2494 + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
2497 + spin_lock_init(&phy_ralink.lock);
2498 + priv->phy = &phy_ralink;
2500 + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
2502 + dev_err(priv->device, "no %s child node found", "mdio-bus");
2506 + if (!of_device_is_available(mii_np)) {
2508 + goto err_put_node;
2511 + priv->mii_bus = mdiobus_alloc();
2512 + if (priv->mii_bus == NULL) {
2514 + goto err_put_node;
2517 + priv->mii_bus->name = "mdio";
2518 + priv->mii_bus->read = priv->soc->mdio_read;
2519 + priv->mii_bus->write = priv->soc->mdio_write;
2520 + priv->mii_bus->reset = fe_mdio_reset;
2521 + priv->mii_bus->irq = priv->mii_irq;
2522 + priv->mii_bus->priv = priv;
2523 + priv->mii_bus->parent = priv->device;
2525 + snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
2526 + err = of_mdiobus_register(priv->mii_bus, mii_np);
2528 + goto err_free_bus;
2533 + kfree(priv->mii_bus);
2535 + of_node_put(mii_np);
2536 + priv->mii_bus = NULL;
2540 +void fe_mdio_cleanup(struct fe_priv *priv)
2542 + if (!priv->mii_bus)
2545 + mdiobus_unregister(priv->mii_bus);
2546 + of_node_put(priv->mii_bus->dev.of_node);
2547 + kfree(priv->mii_bus);
2549 diff --git a/drivers/net/ethernet/ralink/mdio.h b/drivers/net/ethernet/ralink/mdio.h
2550 new file mode 100644
2551 index 0000000..c3910a0
2553 +++ b/drivers/net/ethernet/ralink/mdio.h
2556 + * This program is free software; you can redistribute it and/or modify
2557 + * it under the terms of the GNU General Public License as published by
2558 + * the Free Software Foundation; version 2 of the License
2560 + * This program is distributed in the hope that it will be useful,
2561 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2562 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2563 + * GNU General Public License for more details.
2565 + * You should have received a copy of the GNU General Public License
2566 + * along with this program; if not, write to the Free Software
2567 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2569 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2572 +#ifndef _RALINK_MDIO_H__
2573 +#define _RALINK_MDIO_H__
2575 +#ifdef CONFIG_NET_RALINK_MDIO
2576 +extern int fe_mdio_init(struct fe_priv *priv);
2577 +extern void fe_mdio_cleanup(struct fe_priv *priv);
2578 +extern int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node);
2580 +static inline int fe_mdio_init(struct fe_priv *priv) { return 0; }
2581 +static inline void fe_mdio_cleanup(struct fe_priv *priv) {}
2584 diff --git a/drivers/net/ethernet/ralink/mdio_rt2880.c b/drivers/net/ethernet/ralink/mdio_rt2880.c
2585 new file mode 100644
2586 index 0000000..b99eb46
2588 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.c
2591 + * This program is free software; you can redistribute it and/or modify
2592 + * it under the terms of the GNU General Public License as published by
2593 + * the Free Software Foundation; version 2 of the License
2595 + * This program is distributed in the hope that it will be useful,
2596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2598 + * GNU General Public License for more details.
2600 + * You should have received a copy of the GNU General Public License
2601 + * along with this program; if not, write to the Free Software
2602 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2604 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2607 +#include <linux/module.h>
2608 +#include <linux/kernel.h>
2609 +#include <linux/types.h>
2610 +#include <linux/dma-mapping.h>
2611 +#include <linux/init.h>
2612 +#include <linux/skbuff.h>
2613 +#include <linux/etherdevice.h>
2614 +#include <linux/ethtool.h>
2615 +#include <linux/platform_device.h>
2616 +#include <linux/phy.h>
2617 +#include <linux/of_device.h>
2618 +#include <linux/clk.h>
2619 +#include <linux/of_net.h>
2620 +#include <linux/of_mdio.h>
2622 +#include "ralink_soc_eth.h"
2623 +#include "mdio_rt2880.h"
2626 +#define FE_MDIO_RETRY 1000
2628 +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
2630 + switch (priv->phy->speed[0]) {
2642 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
2646 + if (!priv->link[0]) {
2647 + netif_carrier_off(priv->netdev);
2648 + netdev_info(priv->netdev, "link down\n");
2652 + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
2653 + FE_MDIO_CFG_RX_CLK_SKEW_200 |
2654 + FE_MDIO_CFG_GP1_FRC_EN;
2656 + if (priv->phy->duplex[0] == DUPLEX_FULL)
2657 + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
2659 + if (priv->phy->tx_fc[0])
2660 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
2662 + if (priv->phy->rx_fc[0])
2663 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
2665 + switch (priv->phy->speed[0]) {
2667 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
2670 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
2673 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
2679 + fe_w32(mdio_cfg, FE_MDIO_CFG);
2681 + netif_carrier_on(priv->netdev);
2682 + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
2683 + rt2880_speed_str(priv),
2684 + (DUPLEX_FULL == priv->phy->duplex[0]) ? "Full" : "Half");
2687 +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
2691 + retries = FE_MDIO_RETRY;
2695 + t = fe_r32(FE_MDIO_ACCESS);
2696 + if ((t & (0x1 << 31)) == 0)
2699 + if (retries-- == 0)
2705 + dev_err(priv->device, "MDIO operation timed out\n");
2706 + return -ETIMEDOUT;
2709 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
2711 + struct fe_priv *priv = bus->priv;
2715 + err = rt2880_mdio_wait_ready(priv);
2719 + t = (phy_addr << 24) | (phy_reg << 16);
2720 + fe_w32(t, FE_MDIO_ACCESS);
2722 + fe_w32(t, FE_MDIO_ACCESS);
2724 + err = rt2880_mdio_wait_ready(priv);
2728 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2729 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2731 + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
2734 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
2736 + struct fe_priv *priv = bus->priv;
2740 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2741 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2743 + err = rt2880_mdio_wait_ready(priv);
2747 + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
2748 + fe_w32(t, FE_MDIO_ACCESS);
2750 + fe_w32(t, FE_MDIO_ACCESS);
2752 + return rt2880_mdio_wait_ready(priv);
2755 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
2757 + const __be32 *id = of_get_property(np, "reg", NULL);
2758 + const __be32 *link;
2762 + if (!id || (be32_to_cpu(*id) != 0)) {
2763 + pr_err("%s: invalid port id\n", np->name);
2767 + priv->phy->phy_fixed[0] = of_get_property(np, "ralink,fixed-link", &size);
2768 + if (priv->phy->phy_fixed[0] && (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
2769 + pr_err("%s: invalid fixed link property\n", np->name);
2770 + priv->phy->phy_fixed[0] = NULL;
2774 + phy_mode = of_get_phy_mode(np);
2775 + switch (phy_mode) {
2776 + case PHY_INTERFACE_MODE_RGMII:
2778 + case PHY_INTERFACE_MODE_MII:
2780 + case PHY_INTERFACE_MODE_RMII:
2783 + if (!priv->phy->phy_fixed[0])
2784 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[0]);
2788 + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
2789 + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
2792 + if (priv->phy->phy_fixed[0]) {
2793 + link = priv->phy->phy_fixed[0];
2794 + priv->phy->speed[0] = be32_to_cpup(link++);
2795 + priv->phy->duplex[0] = be32_to_cpup(link++);
2796 + priv->phy->tx_fc[0] = be32_to_cpup(link++);
2797 + priv->phy->rx_fc[0] = be32_to_cpup(link++);
2799 + priv->link[0] = 1;
2800 + switch (priv->phy->speed[0]) {
2808 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[0]);
2809 + priv->phy->phy_fixed[0] = 0;
2812 + dev_info(priv->device, "using fixed link parameters\n");
2813 + rt2880_mdio_link_adjust(priv, 0);
2816 + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0]) {
2817 + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
2822 diff --git a/drivers/net/ethernet/ralink/mdio_rt2880.h b/drivers/net/ethernet/ralink/mdio_rt2880.h
2823 new file mode 100644
2824 index 0000000..51e3633
2826 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.h
2829 + * This program is free software; you can redistribute it and/or modify
2830 + * it under the terms of the GNU General Public License as published by
2831 + * the Free Software Foundation; version 2 of the License
2833 + * This program is distributed in the hope that it will be useful,
2834 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2835 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2836 + * GNU General Public License for more details.
2838 + * You should have received a copy of the GNU General Public License
2839 + * along with this program; if not, write to the Free Software
2840 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2842 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2845 +#ifndef _RALINK_MDIO_RT2880_H__
2846 +#define _RALINK_MDIO_RT2880_H__
2848 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
2849 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2850 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2851 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
2854 diff --git a/drivers/net/ethernet/ralink/mt7530.c b/drivers/net/ethernet/ralink/mt7530.c
2855 new file mode 100644
2856 index 0000000..06b67ee
2858 +++ b/drivers/net/ethernet/ralink/mt7530.c
2861 + * This program is free software; you can redistribute it and/or
2862 + * modify it under the terms of the GNU General Public License
2863 + * as published by the Free Software Foundation; either version 2
2864 + * of the License, or (at your option) any later version.
2866 + * This program is distributed in the hope that it will be useful,
2867 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2868 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2869 + * GNU General Public License for more details.
2871 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
2874 +#include <linux/if.h>
2875 +#include <linux/module.h>
2876 +#include <linux/init.h>
2877 +#include <linux/list.h>
2878 +#include <linux/if_ether.h>
2879 +#include <linux/skbuff.h>
2880 +#include <linux/netdevice.h>
2881 +#include <linux/netlink.h>
2882 +#include <linux/bitops.h>
2883 +#include <net/genetlink.h>
2884 +#include <linux/switch.h>
2885 +#include <linux/delay.h>
2886 +#include <linux/phy.h>
2887 +#include <linux/netdevice.h>
2888 +#include <linux/etherdevice.h>
2889 +#include <linux/lockdep.h>
2890 +#include <linux/workqueue.h>
2891 +#include <linux/of_device.h>
2893 +#include "mt7530.h"
2895 +#define MT7530_CPU_PORT 6
2896 +#define MT7530_NUM_PORTS 8
2897 +#define MT7530_NUM_VLANS 16
2898 +#define MT7530_MAX_VID 4095
2899 +#define MT7530_MIN_VID 0
2902 +#define REG_ESW_VLAN_VTCR 0x90
2903 +#define REG_ESW_VLAN_VAWD1 0x94
2904 +#define REG_ESW_VLAN_VAWD2 0x98
2905 +#define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
2907 +#define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
2908 +#define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
2909 +#define REG_ESW_VLAN_VAWD1_VALID BIT(0)
2911 +/* vlan egress mode */
2913 + ETAG_CTRL_UNTAG = 0,
2914 + ETAG_CTRL_TAG = 2,
2915 + ETAG_CTRL_SWAP = 1,
2916 + ETAG_CTRL_STACK = 3,
2919 +#define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
2920 +#define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
2921 +#define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
2923 + /* Global attributes. */
2924 + MT7530_ATTR_ENABLE_VLAN,
2927 +struct mt7530_port_entry {
2931 +struct mt7530_vlan_entry {
2937 +struct mt7530_priv {
2938 + void __iomem *base;
2939 + struct mii_bus *bus;
2940 + struct switch_dev swdev;
2942 + bool global_vlan_enable;
2943 + struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
2944 + struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
2947 +struct mt7530_mapping {
2949 + u16 pvids[MT7530_NUM_PORTS];
2950 + u8 members[MT7530_NUM_VLANS];
2951 + u8 etags[MT7530_NUM_VLANS];
2952 + u16 vids[MT7530_NUM_VLANS];
2953 +} mt7530_defaults[] = {
2956 + .pvids = { 1, 1, 1, 1, 2, 1, 1 },
2957 + .members = { 0, 0x6f, 0x50 },
2958 + .etags = { 0, 0x40, 0x40 },
2959 + .vids = { 0, 1, 2 },
2962 + .pvids = { 2, 1, 1, 1, 1, 1, 1 },
2963 + .members = { 0, 0x7e, 0x41 },
2964 + .etags = { 0, 0x40, 0x40 },
2965 + .vids = { 0, 1, 2 },
2969 +struct mt7530_mapping*
2970 +mt7530_find_mapping(struct device_node *np)
2975 + if (of_property_read_string(np, "ralink,port-map", &map))
2978 + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
2979 + if (!strcmp(map, mt7530_defaults[i].name))
2980 + return &mt7530_defaults[i];
2986 +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
2990 + mt7530->global_vlan_enable = 1;
2992 + for (i = 0; i < MT7530_NUM_PORTS; i++)
2993 + mt7530->port_entries[i].pvid = map->pvids[i];
2995 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
2996 + mt7530->vlan_entries[i].member = map->members[i];
2997 + mt7530->vlan_entries[i].etags = map->etags[i];
2998 + mt7530->vlan_entries[i].vid = map->vids[i];
3003 +mt7530_reset_switch(struct switch_dev *dev)
3005 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3008 + memset(priv->port_entries, 0, sizeof(priv->port_entries));
3009 + memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
3011 + /* set default vid of each vlan to the same number of vlan, so the vid
3012 + * won't need be set explicitly.
3014 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3015 + priv->vlan_entries[i].vid = i;
3022 +mt7530_get_vlan_enable(struct switch_dev *dev,
3023 + const struct switch_attr *attr,
3024 + struct switch_val *val)
3026 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3028 + val->value.i = priv->global_vlan_enable;
3034 +mt7530_set_vlan_enable(struct switch_dev *dev,
3035 + const struct switch_attr *attr,
3036 + struct switch_val *val)
3038 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3040 + priv->global_vlan_enable = val->value.i != 0;
3046 +mt7530_r32(struct mt7530_priv *priv, u32 reg)
3052 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3053 + low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
3054 + high = mdiobus_read(priv->bus, 0x1f, 0x10);
3056 + return (high << 16) | (low & 0xffff);
3059 + val = ioread32(priv->base + reg);
3060 + pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
3066 +mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
3069 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3070 + mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
3071 + mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
3075 + pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
3076 + iowrite32(val, priv->base + reg);
3080 +mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
3084 + mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
3086 + for (i = 0; i < 20; i++) {
3087 + u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
3089 + if ((val & BIT(31)) == 0)
3095 + printk("mt7530: vtcr timeout\n");
3099 +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
3101 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3103 + if (port >= MT7530_NUM_PORTS)
3106 + *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
3113 +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
3115 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3117 + if (port >= MT7530_NUM_PORTS)
3120 + if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
3123 + priv->port_entries[port].pvid = pvid;
3129 +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3131 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3138 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
3141 + mt7530_vtcr(priv, 0, val->port_vlan);
3143 + member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
3147 + etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
3149 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3150 + struct switch_port *p;
3153 + if (!(member & BIT(i)))
3156 + p = &val->value.ports[val->len++];
3159 + etag = (etags >> (i * 2)) & 0x3;
3161 + if (etag == ETAG_CTRL_TAG)
3162 + p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
3163 + else if (etag != ETAG_CTRL_UNTAG)
3164 + printk("vlan egress tag control neither untag nor tag.\n");
3171 +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3173 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3178 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
3179 + val->len > MT7530_NUM_PORTS)
3182 + for (i = 0; i < val->len; i++) {
3183 + struct switch_port *p = &val->value.ports[i];
3185 + if (p->id >= MT7530_NUM_PORTS)
3188 + member |= BIT(p->id);
3190 + if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
3191 + etags |= BIT(p->id);
3193 + priv->vlan_entries[val->port_vlan].member = member;
3194 + priv->vlan_entries[val->port_vlan].etags = etags;
3200 +mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
3201 + struct switch_val *val)
3203 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3207 + vlan = val->port_vlan;
3208 + vid = (u16)val->value.i;
3210 + if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
3213 + if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
3216 + priv->vlan_entries[vlan].vid = vid;
3221 +mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
3222 + struct switch_val *val)
3224 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3228 + vlan = val->port_vlan;
3230 + vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
3235 + val->value.i = vid;
3240 +mt7530_apply_config(struct switch_dev *dev)
3242 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3245 + if (!priv->global_vlan_enable) {
3246 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3247 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
3249 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3250 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x8100000c);
3255 + /* set all ports as security mode */
3256 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3257 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
3259 + /* set all ports as user port */
3260 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3261 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x81000000);
3263 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3264 + u16 vid = priv->vlan_entries[i].vid;
3265 + u8 member = priv->vlan_entries[i].member;
3266 + u8 etags = priv->vlan_entries[i].etags;
3270 + val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
3276 + val |= (vid << 12);
3278 + mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
3280 + /* vlan port membership */
3282 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
3283 + REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
3284 + REG_ESW_VLAN_VAWD1_VALID);
3286 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
3290 + for (j = 0; j < MT7530_NUM_PORTS; j++) {
3291 + if (etags & BIT(j))
3292 + val |= ETAG_CTRL_TAG << (j * 2);
3294 + val |= ETAG_CTRL_UNTAG << (j * 2);
3296 + mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
3298 + /* write to vlan table */
3299 + mt7530_vtcr(priv, 1, i);
3302 + /* Port Default PVID */
3303 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3305 + val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
3307 + val |= priv->port_entries[i].pvid;
3308 + mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
3315 +mt7530_get_port_link(struct switch_dev *dev, int port,
3316 + struct switch_port_link *link)
3318 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3321 + if (port < 0 || port >= MT7530_NUM_PORTS)
3324 + pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
3326 + link->link = pmsr & 1;
3327 + link->duplex = (pmsr >> 1) & 1;
3328 + speed = (pmsr >> 2) & 3;
3332 + link->speed = SWITCH_PORT_SPEED_10;
3335 + link->speed = SWITCH_PORT_SPEED_100;
3338 + case 3: /* forced gige speed can be 2 or 3 */
3339 + link->speed = SWITCH_PORT_SPEED_1000;
3342 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
3349 +static const struct switch_attr mt7530_global[] = {
3351 + .type = SWITCH_TYPE_INT,
3352 + .name = "enable_vlan",
3353 + .description = "VLAN mode (1:enabled)",
3355 + .id = MT7530_ATTR_ENABLE_VLAN,
3356 + .get = mt7530_get_vlan_enable,
3357 + .set = mt7530_set_vlan_enable,
3361 +static const struct switch_attr mt7530_port[] = {
3364 +static const struct switch_attr mt7530_vlan[] = {
3366 + .type = SWITCH_TYPE_INT,
3368 + .description = "VLAN ID (0-4094)",
3369 + .set = mt7530_set_vid,
3370 + .get = mt7530_get_vid,
3375 +static const struct switch_dev_ops mt7530_ops = {
3377 + .attr = mt7530_global,
3378 + .n_attr = ARRAY_SIZE(mt7530_global),
3381 + .attr = mt7530_port,
3382 + .n_attr = ARRAY_SIZE(mt7530_port),
3385 + .attr = mt7530_vlan,
3386 + .n_attr = ARRAY_SIZE(mt7530_vlan),
3388 + .get_vlan_ports = mt7530_get_vlan_ports,
3389 + .set_vlan_ports = mt7530_set_vlan_ports,
3390 + .get_port_pvid = mt7530_get_port_pvid,
3391 + .set_port_pvid = mt7530_set_port_pvid,
3392 + .get_port_link = mt7530_get_port_link,
3393 + .apply_config = mt7530_apply_config,
3394 + .reset_switch = mt7530_reset_switch,
3398 +mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus)
3400 + struct switch_dev *swdev;
3401 + struct mt7530_priv *mt7530;
3402 + struct mt7530_mapping *map;
3405 + if (bus && bus->phy_map[0x1f]->phy_id != 0x1beef)
3408 + mt7530 = devm_kzalloc(dev, sizeof(struct mt7530_priv), GFP_KERNEL);
3412 + mt7530->base = base;
3413 + mt7530->bus = bus;
3414 + mt7530->global_vlan_enable = 1;
3416 + swdev = &mt7530->swdev;
3417 + swdev->name = "mt7530";
3418 + swdev->alias = "mt7530";
3419 + swdev->cpu_port = MT7530_CPU_PORT;
3420 + swdev->ports = MT7530_NUM_PORTS;
3421 + swdev->vlans = MT7530_NUM_VLANS;
3422 + swdev->ops = &mt7530_ops;
3424 + ret = register_switch(swdev, NULL);
3426 + dev_err(dev, "failed to register mt7530\n");
3430 + dev_info(dev, "loaded mt7530 driver\n");
3432 + map = mt7530_find_mapping(dev->of_node);
3434 + mt7530_apply_mapping(mt7530, map);
3435 + mt7530_apply_config(swdev);
3439 diff --git a/drivers/net/ethernet/ralink/mt7530.h b/drivers/net/ethernet/ralink/mt7530.h
3440 new file mode 100644
3441 index 0000000..169b888
3443 +++ b/drivers/net/ethernet/ralink/mt7530.h
3446 + * This program is free software; you can redistribute it and/or
3447 + * modify it under the terms of the GNU General Public License
3448 + * as published by the Free Software Foundation; either version 2
3449 + * of the License, or (at your option) any later version.
3451 + * This program is distributed in the hope that it will be useful,
3452 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3453 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3454 + * GNU General Public License for more details.
3456 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
3459 +#ifndef _MT7530_H__
3460 +#define _MT7530_H__
3462 +int mt7530_probe(struct device *dev, void __iomem *base, struct mii_bus *bus);
3465 diff --git a/drivers/net/ethernet/ralink/ralink_soc_eth.c b/drivers/net/ethernet/ralink/ralink_soc_eth.c
3466 new file mode 100644
3467 index 0000000..f3abab7
3469 +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.c
3472 + * This program is free software; you can redistribute it and/or modify
3473 + * it under the terms of the GNU General Public License as published by
3474 + * the Free Software Foundation; version 2 of the License
3476 + * This program is distributed in the hope that it will be useful,
3477 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3478 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3479 + * GNU General Public License for more details.
3481 + * You should have received a copy of the GNU General Public License
3482 + * along with this program; if not, write to the Free Software
3483 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3485 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3488 +#include <linux/module.h>
3489 +#include <linux/kernel.h>
3490 +#include <linux/types.h>
3491 +#include <linux/dma-mapping.h>
3492 +#include <linux/init.h>
3493 +#include <linux/skbuff.h>
3494 +#include <linux/etherdevice.h>
3495 +#include <linux/ethtool.h>
3496 +#include <linux/platform_device.h>
3497 +#include <linux/of_device.h>
3498 +#include <linux/clk.h>
3499 +#include <linux/of_net.h>
3500 +#include <linux/of_mdio.h>
3501 +#include <linux/if_vlan.h>
3502 +#include <linux/reset.h>
3504 +#include <asm/mach-ralink/ralink_regs.h>
3506 +#include "ralink_soc_eth.h"
3507 +#include "esw_rt3052.h"
3510 +#define TX_TIMEOUT (2 * HZ)
3511 +#define MAX_RX_LENGTH 1536
3512 +#define DMA_DUMMY_DESC 0xffffffff
3514 +static const u32 fe_reg_table_default[FE_REG_COUNT] = {
3515 + [FE_REG_PDMA_GLO_CFG] = FE_PDMA_GLO_CFG,
3516 + [FE_REG_PDMA_RST_CFG] = FE_PDMA_RST_CFG,
3517 + [FE_REG_DLY_INT_CFG] = FE_DLY_INT_CFG,
3518 + [FE_REG_TX_BASE_PTR0] = FE_TX_BASE_PTR0,
3519 + [FE_REG_TX_MAX_CNT0] = FE_TX_MAX_CNT0,
3520 + [FE_REG_TX_CTX_IDX0] = FE_TX_CTX_IDX0,
3521 + [FE_REG_RX_BASE_PTR0] = FE_RX_BASE_PTR0,
3522 + [FE_REG_RX_MAX_CNT0] = FE_RX_MAX_CNT0,
3523 + [FE_REG_RX_CALC_IDX0] = FE_RX_CALC_IDX0,
3524 + [FE_REG_FE_INT_ENABLE] = FE_FE_INT_ENABLE,
3525 + [FE_REG_FE_INT_STATUS] = FE_FE_INT_STATUS,
3528 +static const u32 *fe_reg_table = fe_reg_table_default;
3530 +static void __iomem *fe_base = 0;
3532 +void fe_w32(u32 val, unsigned reg)
3534 + __raw_writel(val, fe_base + reg);
3537 +u32 fe_r32(unsigned reg)
3539 + return __raw_readl(fe_base + reg);
3542 +static inline void fe_reg_w32(u32 val, enum fe_reg reg)
3544 + fe_w32(val, fe_reg_table[reg]);
3547 +static inline u32 fe_reg_r32(enum fe_reg reg)
3549 + return fe_r32(fe_reg_table[reg]);
3552 +static inline void fe_int_disable(u32 mask)
3554 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) & ~mask,
3555 + FE_REG_FE_INT_ENABLE);
3557 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3560 +static inline void fe_int_enable(u32 mask)
3562 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) | mask,
3563 + FE_REG_FE_INT_ENABLE);
3565 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3568 +static inline void fe_hw_set_macaddr(struct fe_priv *priv, unsigned char *mac)
3570 + unsigned long flags;
3572 + spin_lock_irqsave(&priv->page_lock, flags);
3573 + fe_w32((mac[0] << 8) | mac[1], FE_GDMA1_MAC_ADRH);
3574 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
3575 + FE_GDMA1_MAC_ADRL);
3576 + spin_unlock_irqrestore(&priv->page_lock, flags);
3579 +static int fe_set_mac_address(struct net_device *dev, void *p)
3581 + int ret = eth_mac_addr(dev, p);
3584 + struct fe_priv *priv = netdev_priv(dev);
3586 + if (priv->soc->set_mac)
3587 + priv->soc->set_mac(priv, dev->dev_addr);
3589 + fe_hw_set_macaddr(priv, p);
3595 +static struct sk_buff* fe_alloc_skb(struct fe_priv *priv)
3597 + struct sk_buff *skb;
3599 + skb = netdev_alloc_skb(priv->netdev, MAX_RX_LENGTH + NET_IP_ALIGN);
3603 + skb_reserve(skb, NET_IP_ALIGN);
3608 +static int fe_alloc_rx(struct fe_priv *priv)
3610 + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
3613 + priv->rx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
3614 + &priv->rx_phys, GFP_ATOMIC);
3615 + if (!priv->rx_dma)
3618 + memset(priv->rx_dma, 0, size);
3620 + for (i = 0; i < NUM_DMA_DESC; i++) {
3621 + priv->rx_skb[i] = fe_alloc_skb(priv);
3622 + if (!priv->rx_skb[i])
3626 + for (i = 0; i < NUM_DMA_DESC; i++) {
3627 + dma_addr_t dma_addr = dma_map_single(&priv->netdev->dev,
3628 + priv->rx_skb[i]->data,
3631 + priv->rx_dma[i].rxd1 = (unsigned int) dma_addr;
3633 + if (priv->soc->rx_dma)
3634 + priv->soc->rx_dma(priv, i, MAX_RX_LENGTH);
3636 + priv->rx_dma[i].rxd2 = RX_DMA_LSO;
3640 + fe_reg_w32(priv->rx_phys, FE_REG_RX_BASE_PTR0);
3641 + fe_reg_w32(NUM_DMA_DESC, FE_REG_RX_MAX_CNT0);
3642 + fe_reg_w32((NUM_DMA_DESC - 1), FE_REG_RX_CALC_IDX0);
3643 + fe_reg_w32(FE_PST_DRX_IDX0, FE_REG_PDMA_RST_CFG);
3648 +static int fe_alloc_tx(struct fe_priv *priv)
3650 + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
3653 + priv->tx_free_idx = 0;
3655 + priv->tx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
3656 + &priv->tx_phys, GFP_ATOMIC);
3657 + if (!priv->tx_dma)
3660 + memset(priv->tx_dma, 0, size);
3662 + for (i = 0; i < NUM_DMA_DESC; i++) {
3663 + if (priv->soc->tx_dma) {
3664 + priv->soc->tx_dma(priv, i, NULL);
3668 + priv->tx_dma[i].txd2 = TX_DMA_LSO | TX_DMA_DONE;
3669 + priv->tx_dma[i].txd4 = TX_DMA_QN(3) | TX_DMA_PN(1);
3672 + fe_reg_w32(priv->tx_phys, FE_REG_TX_BASE_PTR0);
3673 + fe_reg_w32(NUM_DMA_DESC, FE_REG_TX_MAX_CNT0);
3674 + fe_reg_w32(0, FE_REG_TX_CTX_IDX0);
3675 + fe_reg_w32(FE_PST_DTX_IDX0, FE_REG_PDMA_RST_CFG);
3680 +static void fe_free_dma(struct fe_priv *priv)
3684 + for (i = 0; i < NUM_DMA_DESC; i++) {
3685 + if (priv->rx_skb[i]) {
3686 + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[i].rxd1,
3687 + MAX_RX_LENGTH, DMA_FROM_DEVICE);
3688 + dev_kfree_skb_any(priv->rx_skb[i]);
3689 + priv->rx_skb[i] = NULL;
3692 + if (priv->tx_skb[i]) {
3693 + dev_kfree_skb_any(priv->tx_skb[i]);
3694 + priv->tx_skb[i] = NULL;
3698 + if (priv->rx_dma) {
3699 + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
3700 + dma_free_coherent(&priv->netdev->dev, size, priv->rx_dma,
3704 + if (priv->tx_dma) {
3705 + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
3706 + dma_free_coherent(&priv->netdev->dev, size, priv->tx_dma,
3710 + netdev_reset_queue(priv->netdev);
3713 +static void fe_start_tso(struct sk_buff *skb, struct net_device *dev, unsigned int nr_frags, int idx)
3715 + struct fe_priv *priv = netdev_priv(dev);
3716 + struct skb_frag_struct *frag;
3719 + for (i = 0; i < nr_frags; i++) {
3720 + dma_addr_t mapped_addr;
3722 + frag = &skb_shinfo(skb)->frags[i];
3723 + mapped_addr = skb_frag_dma_map(&dev->dev, frag, 0, skb_frag_size(frag), DMA_TO_DEVICE);
3725 + idx = (idx + 1) % NUM_DMA_DESC;
3726 + priv->tx_dma[idx].txd1 = mapped_addr;
3727 + if (i == nr_frags - 1)
3728 + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(frag->size);
3730 + priv->tx_dma[idx].txd2 = TX_DMA_PLEN0(frag->size);
3732 + priv->tx_dma[idx].txd3 = mapped_addr;
3733 + if (i == nr_frags - 1)
3734 + priv->tx_dma[idx].txd2 |= TX_DMA_LS1 | TX_DMA_PLEN1(frag->size);
3736 + priv->tx_dma[idx].txd2 |= TX_DMA_PLEN1(frag->size);
3741 +static int fe_start_xmit(struct sk_buff *skb, struct net_device *dev)
3743 + unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
3744 + struct fe_priv *priv = netdev_priv(dev);
3745 + dma_addr_t mapped_addr;
3746 + u32 tx_next, tx, tx_num = 1;
3749 + if (priv->soc->min_pkt_len) {
3750 + if (skb->len < priv->soc->min_pkt_len) {
3751 + if (skb_padto(skb, priv->soc->min_pkt_len)) {
3753 + "fe_eth: skb_padto failed\n");
3757 + skb_put(skb, priv->soc->min_pkt_len - skb->len);
3761 + dev->trans_start = jiffies;
3762 + mapped_addr = dma_map_single(&priv->netdev->dev, skb->data,
3763 + skb->len, DMA_TO_DEVICE);
3765 + spin_lock(&priv->page_lock);
3767 + tx = fe_reg_r32(FE_REG_TX_CTX_IDX0);
3768 + if (priv->soc->tso && nr_frags)
3769 + tx_num += nr_frags >> 1;
3770 + tx_next = (tx + tx_num) % NUM_DMA_DESC;
3771 + if ((priv->tx_skb[tx]) || (priv->tx_skb[tx_next]) ||
3772 + !(priv->tx_dma[tx].txd2 & TX_DMA_DONE) ||
3773 + !(priv->tx_dma[tx_next].txd2 & TX_DMA_DONE))
3775 + spin_unlock(&priv->page_lock);
3776 + dev->stats.tx_dropped++;
3779 + return NETDEV_TX_OK;
3782 + if (priv->soc->tso) {
3785 + priv->tx_skb[(tx + t - 1) % NUM_DMA_DESC] = skb;
3787 + priv->tx_skb[(tx + t - 1) % NUM_DMA_DESC] = (struct sk_buff *) DMA_DUMMY_DESC;
3789 + priv->tx_skb[tx] = skb;
3791 + priv->tx_dma[tx].txd1 = (unsigned int) mapped_addr;
3794 + priv->tx_dma[tx].txd4 &= ~0x80;
3795 + if (priv->soc->tx_dma)
3796 + priv->soc->tx_dma(priv, tx, skb);
3798 + priv->tx_dma[tx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(skb->len);
3800 + if (skb->ip_summed == CHECKSUM_PARTIAL)
3801 + priv->tx_dma[tx].txd4 |= TX_DMA_CHKSUM;
3803 + priv->tx_dma[tx].txd4 &= ~TX_DMA_CHKSUM;
3805 + if (priv->soc->tso)
3806 + fe_start_tso(skb, dev, nr_frags, tx);
3808 + if (priv->soc->tso && (skb_shinfo(skb)->gso_segs > 1)) {
3809 + struct iphdr *iph = NULL;
3810 + struct tcphdr *th = NULL;
3811 + struct ipv6hdr *ip6h = NULL;
3813 + ip6h = (struct ipv6hdr *) skb_network_header(skb);
3814 + iph = (struct iphdr *) skb_network_header(skb);
3815 + if ((iph->version == 4) && (iph->protocol == IPPROTO_TCP)) {
3816 + th = (struct tcphdr *)skb_transport_header(skb);
3817 + priv->tx_dma[tx].txd4 |= BIT(28);
3818 + th->check = htons(skb_shinfo(skb)->gso_size);
3819 + dma_cache_sync(NULL, th, sizeof(struct tcphdr), DMA_TO_DEVICE);
3820 + } else if ((ip6h->version == 6) && (ip6h->nexthdr == NEXTHDR_TCP)) {
3821 + th = (struct tcphdr *)skb_transport_header(skb);
3822 + priv->tx_dma[tx].txd4 |= BIT(28);
3823 + th->check = htons(skb_shinfo(skb)->gso_size);
3824 + dma_cache_sync(NULL, th, sizeof(struct tcphdr), DMA_TO_DEVICE);
3828 + for (i = 0; i < tx_num; i++)
3829 + dma_cache_sync(NULL, &priv->tx_dma[tx + i], sizeof(struct fe_tx_dma), DMA_TO_DEVICE);
3831 + dev->stats.tx_packets++;
3832 + dev->stats.tx_bytes += skb->len;
3835 + fe_reg_w32(tx_next, FE_REG_TX_CTX_IDX0);
3836 + netdev_sent_queue(dev, skb->len);
3838 + spin_unlock(&priv->page_lock);
3840 + return NETDEV_TX_OK;
3843 +static int fe_poll_rx(struct napi_struct *napi, int budget)
3845 + struct fe_priv *priv = container_of(napi, struct fe_priv, rx_napi);
3846 + int idx = fe_reg_r32(FE_REG_RX_CALC_IDX0);
3850 + while ((rx < budget) && !complete) {
3851 + idx = (idx + 1) % NUM_DMA_DESC;
3853 + if (priv->rx_dma[idx].rxd2 & RX_DMA_DONE) {
3854 + struct sk_buff *new_skb = fe_alloc_skb(priv);
3857 + int pktlen = RX_DMA_PLEN0(priv->rx_dma[idx].rxd2);
3858 + dma_addr_t dma_addr;
3860 + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[idx].rxd1,
3861 + MAX_RX_LENGTH, DMA_FROM_DEVICE);
3863 + skb_put(priv->rx_skb[idx], pktlen);
3864 + priv->rx_skb[idx]->dev = priv->netdev;
3865 + priv->rx_skb[idx]->protocol = eth_type_trans(priv->rx_skb[idx], priv->netdev);
3866 + if (priv->rx_dma[idx].rxd4 & priv->soc->checksum_bit)
3867 + priv->rx_skb[idx]->ip_summed = CHECKSUM_UNNECESSARY;
3869 + priv->rx_skb[idx]->ip_summed = CHECKSUM_NONE;
3870 + priv->netdev->stats.rx_packets++;
3871 + priv->netdev->stats.rx_bytes += pktlen;
3873 +#ifdef CONFIG_INET_LRO
3874 + if (priv->soc->get_skb_header && priv->rx_skb[idx]->ip_summed == CHECKSUM_UNNECESSARY)
3875 + lro_receive_skb(&priv->lro_mgr, priv->rx_skb[idx], NULL);
3878 + netif_receive_skb(priv->rx_skb[idx]);
3880 + priv->rx_skb[idx] = new_skb;
3882 + dma_addr = dma_map_single(&priv->netdev->dev,
3886 + priv->rx_dma[idx].rxd1 = (unsigned int) dma_addr;
3889 + priv->netdev->stats.rx_dropped++;
3892 + if (priv->soc->rx_dma)
3893 + priv->soc->rx_dma(priv, idx, MAX_RX_LENGTH);
3895 + priv->rx_dma[idx].rxd2 = RX_DMA_LSO;
3896 + fe_reg_w32(idx, FE_REG_RX_CALC_IDX0);
3904 +#ifdef CONFIG_INET_LRO
3905 + if (priv->soc->get_skb_header)
3906 + lro_flush_all(&priv->lro_mgr);
3909 + napi_complete(&priv->rx_napi);
3910 + fe_int_enable(priv->soc->rx_dly_int);
3916 +static void fe_tx_housekeeping(unsigned long ptr)
3918 + struct net_device *dev = (struct net_device*)ptr;
3919 + struct fe_priv *priv = netdev_priv(dev);
3920 + unsigned int bytes_compl = 0;
3921 + unsigned int pkts_compl = 0;
3923 + spin_lock(&priv->page_lock);
3925 + struct fe_tx_dma *txd;
3927 + txd = &priv->tx_dma[priv->tx_free_idx];
3929 + if (!(txd->txd2 & TX_DMA_DONE) || !(priv->tx_skb[priv->tx_free_idx]))
3932 + if (priv->tx_skb[priv->tx_free_idx] != (struct sk_buff *) DMA_DUMMY_DESC) {
3933 + bytes_compl += priv->tx_skb[priv->tx_free_idx]->len;
3934 + dev_kfree_skb_irq(priv->tx_skb[priv->tx_free_idx]);
3937 + priv->tx_skb[priv->tx_free_idx] = NULL;
3938 + priv->tx_free_idx++;
3939 + if (priv->tx_free_idx >= NUM_DMA_DESC)
3940 + priv->tx_free_idx = 0;
3943 + netdev_completed_queue(priv->netdev, pkts_compl, bytes_compl);
3944 + spin_unlock(&priv->page_lock);
3946 + fe_int_enable(priv->soc->tx_dly_int);
3949 +static void fe_tx_timeout(struct net_device *dev)
3951 + struct fe_priv *priv = netdev_priv(dev);
3953 + tasklet_schedule(&priv->tx_tasklet);
3954 + priv->netdev->stats.tx_errors++;
3955 + netdev_err(dev, "transmit timed out, waking up the queue\n");
3956 + netif_wake_queue(dev);
3959 +static irqreturn_t fe_handle_irq(int irq, void *dev)
3961 + struct fe_priv *priv = netdev_priv(dev);
3962 + unsigned int status;
3963 + unsigned int mask;
3965 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
3966 + mask = fe_reg_r32(FE_REG_FE_INT_ENABLE);
3968 + if (!(status & mask))
3971 + if (status & priv->soc->rx_dly_int) {
3972 + fe_int_disable(priv->soc->rx_dly_int);
3973 + napi_schedule(&priv->rx_napi);
3976 + if (status & priv->soc->tx_dly_int) {
3977 + fe_int_disable(priv->soc->tx_dly_int);
3978 + tasklet_schedule(&priv->tx_tasklet);
3981 + fe_reg_w32(status, FE_REG_FE_INT_STATUS);
3983 + return IRQ_HANDLED;
3986 +static int fe_hw_init(struct net_device *dev)
3988 + struct fe_priv *priv = netdev_priv(dev);
3991 + err = devm_request_irq(priv->device, dev->irq, fe_handle_irq, 0,
3992 + dev_name(priv->device), dev);
3996 + err = fe_alloc_rx(priv);
3998 + err = fe_alloc_tx(priv);
4002 + if (priv->soc->set_mac)
4003 + priv->soc->set_mac(priv, dev->dev_addr);
4005 + fe_hw_set_macaddr(priv, dev->dev_addr);
4007 + fe_reg_w32(FE_DELAY_INIT, FE_REG_DLY_INT_CFG);
4009 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4011 + tasklet_init(&priv->tx_tasklet, fe_tx_housekeeping, (unsigned long)dev);
4013 + if (priv->soc->fwd_config) {
4014 + priv->soc->fwd_config(priv);
4016 + unsigned long sysclk = priv->sysclk;
4019 + netdev_err(dev, "unable to get clock\n");
4023 + sysclk /= FE_US_CYC_CNT_DIVISOR;
4024 + sysclk <<= FE_US_CYC_CNT_SHIFT;
4026 + fe_w32((fe_r32(FE_FE_GLO_CFG) &
4027 + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
4030 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) & ~0xffff, FE_GDMA1_FWD_CFG);
4031 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) | (FE_GDM1_ICS_EN | FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
4032 + FE_GDMA1_FWD_CFG);
4033 + fe_w32(fe_r32(FE_CDMA_CSG_CFG) | (FE_ICS_GEN_EN | FE_TCS_GEN_EN | FE_UCS_GEN_EN),
4035 + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
4038 + fe_w32(1, FE_FE_RST_GL);
4039 + fe_w32(0, FE_FE_RST_GL);
4044 +static int fe_open(struct net_device *dev)
4046 + struct fe_priv *priv = netdev_priv(dev);
4047 + unsigned long flags;
4050 + spin_lock_irqsave(&priv->page_lock, flags);
4051 + napi_enable(&priv->rx_napi);
4053 + val = FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
4054 + val |= priv->soc->pdma_glo_cfg;
4055 + fe_reg_w32(val, FE_REG_PDMA_GLO_CFG);
4057 + spin_unlock_irqrestore(&priv->page_lock, flags);
4060 + priv->phy->start(priv);
4062 + if (priv->soc->has_carrier && priv->soc->has_carrier(priv))
4063 + netif_carrier_on(dev);
4065 + netif_start_queue(dev);
4066 + fe_int_enable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4071 +static int fe_stop(struct net_device *dev)
4073 + struct fe_priv *priv = netdev_priv(dev);
4074 + unsigned long flags;
4076 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
4078 + netif_stop_queue(dev);
4081 + priv->phy->stop(priv);
4083 + spin_lock_irqsave(&priv->page_lock, flags);
4084 + napi_disable(&priv->rx_napi);
4086 + fe_reg_w32(fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
4087 + ~(FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN),
4088 + FE_REG_PDMA_GLO_CFG);
4089 + spin_unlock_irqrestore(&priv->page_lock, flags);
4094 +static int __init fe_init(struct net_device *dev)
4096 + struct fe_priv *priv = netdev_priv(dev);
4097 + struct device_node *port;
4100 + BUG_ON(!priv->soc->reset_fe);
4101 + priv->soc->reset_fe();
4103 + if (priv->soc->switch_init)
4104 + priv->soc->switch_init(priv);
4106 + memcpy(dev->dev_addr, priv->soc->mac, ETH_ALEN);
4107 + of_get_mac_address_mtd(priv->device->of_node, dev->dev_addr);
4109 + err = fe_mdio_init(priv);
4114 + err = priv->phy->connect(priv);
4116 + goto err_mdio_cleanup;
4119 + if (priv->soc->port_init)
4120 + for_each_child_of_node(priv->device->of_node, port)
4121 + if (of_device_is_compatible(port, "ralink,eth-port") && of_device_is_available(port))
4122 + priv->soc->port_init(priv, port);
4124 + err = fe_hw_init(dev);
4126 + goto err_phy_disconnect;
4128 + if (priv->soc->switch_config)
4129 + priv->soc->switch_config(priv);
4133 +err_phy_disconnect:
4135 + priv->phy->disconnect(priv);
4137 + fe_mdio_cleanup(priv);
4142 +static void fe_uninit(struct net_device *dev)
4144 + struct fe_priv *priv = netdev_priv(dev);
4146 + tasklet_kill(&priv->tx_tasklet);
4149 + priv->phy->disconnect(priv);
4150 + fe_mdio_cleanup(priv);
4152 + fe_reg_w32(0, FE_REG_FE_INT_ENABLE);
4153 + free_irq(dev->irq, dev);
4155 + fe_free_dma(priv);
4158 +static const struct net_device_ops fe_netdev_ops = {
4159 + .ndo_init = fe_init,
4160 + .ndo_uninit = fe_uninit,
4161 + .ndo_open = fe_open,
4162 + .ndo_stop = fe_stop,
4163 + .ndo_start_xmit = fe_start_xmit,
4164 + .ndo_tx_timeout = fe_tx_timeout,
4165 + .ndo_set_mac_address = fe_set_mac_address,
4166 + .ndo_change_mtu = eth_change_mtu,
4167 + .ndo_validate_addr = eth_validate_addr,
4170 +static int fe_probe(struct platform_device *pdev)
4172 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4173 + const struct of_device_id *match;
4174 + struct fe_soc_data *soc = NULL;
4175 + struct net_device *netdev;
4176 + struct fe_priv *priv;
4177 + struct clk *sysclk;
4180 + device_reset(&pdev->dev);
4182 + match = of_match_device(of_fe_match, &pdev->dev);
4183 + soc = (struct fe_soc_data *) match->data;
4185 + if (soc->init_data)
4186 + soc->init_data(soc);
4187 + if (soc->reg_table)
4188 + fe_reg_table = soc->reg_table;
4190 + fe_base = devm_request_and_ioremap(&pdev->dev, res);
4194 + netdev = alloc_etherdev(sizeof(struct fe_priv));
4196 + dev_err(&pdev->dev, "alloc_etherdev failed\n");
4200 + strcpy(netdev->name, "eth%d");
4201 + netdev->netdev_ops = &fe_netdev_ops;
4202 + netdev->base_addr = (unsigned long) fe_base;
4203 + netdev->watchdog_timeo = TX_TIMEOUT;
4204 + netdev->features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
4206 + if (fe_reg_table[FE_REG_FE_DMA_VID_BASE])
4207 + netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
4210 + dev_info(&pdev->dev, "Enabling TSO\n");
4211 + netdev->features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_IPV6_CSUM;
4213 + netdev->hw_features = netdev->features;
4215 + netdev->irq = platform_get_irq(pdev, 0);
4216 + if (netdev->irq < 0) {
4217 + dev_err(&pdev->dev, "no IRQ resource found\n");
4222 + priv = netdev_priv(netdev);
4223 + memset(priv, 0, sizeof(struct fe_priv));
4224 + spin_lock_init(&priv->page_lock);
4226 + sysclk = devm_clk_get(&pdev->dev, NULL);
4227 + if (!IS_ERR(sysclk))
4228 + priv->sysclk = clk_get_rate(sysclk);
4230 + priv->netdev = netdev;
4231 + priv->device = &pdev->dev;
4234 + err = register_netdev(netdev);
4236 + dev_err(&pdev->dev, "error bringing up device\n");
4240 + netif_napi_add(netdev, &priv->rx_napi, fe_poll_rx, 32);
4242 +#ifdef CONFIG_INET_LRO
4243 + if (priv->soc->get_skb_header) {
4244 + priv->lro_mgr.dev = netdev;
4245 + memset(&priv->lro_mgr.stats, 0, sizeof(priv->lro_mgr.stats));
4246 + priv->lro_mgr.features = LRO_F_NAPI;
4247 + priv->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
4248 + priv->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
4249 + priv->lro_mgr.max_desc = ARRAY_SIZE(priv->lro_arr);
4250 + priv->lro_mgr.max_aggr = 64;
4251 + priv->lro_mgr.frag_align_pad = 0;
4252 + priv->lro_mgr.lro_arr = priv->lro_arr;
4253 + priv->lro_mgr.get_skb_header = priv->soc->get_skb_header;
4257 + platform_set_drvdata(pdev, netdev);
4259 + netdev_info(netdev, "done loading\n");
4264 +static int fe_remove(struct platform_device *pdev)
4266 + struct net_device *dev = platform_get_drvdata(pdev);
4267 + struct fe_priv *priv = netdev_priv(dev);
4269 + netif_stop_queue(dev);
4270 + netif_napi_del(&priv->rx_napi);
4272 + unregister_netdev(dev);
4278 +static struct platform_driver fe_driver = {
4279 + .probe = fe_probe,
4280 + .remove = fe_remove,
4282 + .name = "ralink_soc_eth",
4283 + .owner = THIS_MODULE,
4284 + .of_match_table = of_fe_match,
4288 +static int __init init_rtfe(void)
4292 + ret = rtesw_init();
4296 + ret = platform_driver_register(&fe_driver);
4303 +static void __exit exit_rtfe(void)
4305 + platform_driver_unregister(&fe_driver);
4309 +module_init(init_rtfe);
4310 +module_exit(exit_rtfe);
4312 +MODULE_LICENSE("GPL");
4313 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
4314 +MODULE_DESCRIPTION("Ethernet driver for Ralink SoC");
4315 diff --git a/drivers/net/ethernet/ralink/ralink_soc_eth.h b/drivers/net/ethernet/ralink/ralink_soc_eth.h
4316 new file mode 100644
4317 index 0000000..ca16788
4319 +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.h
4322 + * This program is free software; you can redistribute it and/or modify
4323 + * it under the terms of the GNU General Public License as published by
4324 + * the Free Software Foundation; version 2 of the License
4326 + * This program is distributed in the hope that it will be useful,
4327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4329 + * GNU General Public License for more details.
4331 + * You should have received a copy of the GNU General Public License
4332 + * along with this program; if not, write to the Free Software
4333 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4335 + * based on Ralink SDK3.3
4336 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4342 +#include <linux/mii.h>
4343 +#include <linux/interrupt.h>
4344 +#include <linux/netdevice.h>
4345 +#include <linux/dma-mapping.h>
4346 +#include <linux/phy.h>
4347 +#include <linux/inet_lro.h>
4351 + FE_REG_PDMA_GLO_CFG = 0,
4352 + FE_REG_PDMA_RST_CFG,
4353 + FE_REG_DLY_INT_CFG,
4354 + FE_REG_TX_BASE_PTR0,
4355 + FE_REG_TX_MAX_CNT0,
4356 + FE_REG_TX_CTX_IDX0,
4357 + FE_REG_RX_BASE_PTR0,
4358 + FE_REG_RX_MAX_CNT0,
4359 + FE_REG_RX_CALC_IDX0,
4360 + FE_REG_FE_INT_ENABLE,
4361 + FE_REG_FE_INT_STATUS,
4362 + FE_REG_FE_DMA_VID_BASE,
4366 +#define NUM_DMA_DESC 0x100
4368 +#define FE_DELAY_EN_INT 0x80
4369 +#define FE_DELAY_MAX_INT 0x04
4370 +#define FE_DELAY_MAX_TOUT 0x04
4371 +#define FE_DELAY_CHAN (((FE_DELAY_EN_INT | FE_DELAY_MAX_INT) << 8) | FE_DELAY_MAX_TOUT)
4372 +#define FE_DELAY_INIT ((FE_DELAY_CHAN << 16) | FE_DELAY_CHAN)
4373 +#define FE_PSE_FQFC_CFG_INIT 0x80504000
4375 +/* interrupt bits */
4376 +#define FE_CNT_PPE_AF BIT(31)
4377 +#define FE_CNT_GDM_AF BIT(29)
4378 +#define FE_PSE_P2_FC BIT(26)
4379 +#define FE_PSE_BUF_DROP BIT(24)
4380 +#define FE_GDM_OTHER_DROP BIT(23)
4381 +#define FE_PSE_P1_FC BIT(22)
4382 +#define FE_PSE_P0_FC BIT(21)
4383 +#define FE_PSE_FQ_EMPTY BIT(20)
4384 +#define FE_GE1_STA_CHG BIT(18)
4385 +#define FE_TX_COHERENT BIT(17)
4386 +#define FE_RX_COHERENT BIT(16)
4387 +#define FE_TX_DONE_INT3 BIT(11)
4388 +#define FE_TX_DONE_INT2 BIT(10)
4389 +#define FE_TX_DONE_INT1 BIT(9)
4390 +#define FE_TX_DONE_INT0 BIT(8)
4391 +#define FE_RX_DONE_INT0 BIT(2)
4392 +#define FE_TX_DLY_INT BIT(1)
4393 +#define FE_RX_DLY_INT BIT(0)
4395 +#define RT5350_RX_DLY_INT BIT(30)
4396 +#define RT5350_TX_DLY_INT BIT(28)
4399 +#define FE_FE_OFFSET 0x0000
4400 +#define FE_GDMA_OFFSET 0x0020
4401 +#define FE_PSE_OFFSET 0x0040
4402 +#define FE_GDMA2_OFFSET 0x0060
4403 +#define FE_CDMA_OFFSET 0x0080
4404 +#define FE_DMA_VID0 0x00a8
4405 +#define FE_PDMA_OFFSET 0x0100
4406 +#define FE_PPE_OFFSET 0x0200
4407 +#define FE_CMTABLE_OFFSET 0x0400
4408 +#define FE_POLICYTABLE_OFFSET 0x1000
4410 +#define RT5350_PDMA_OFFSET 0x0800
4411 +#define RT5350_SDM_OFFSET 0x0c00
4413 +#define FE_MDIO_ACCESS (FE_FE_OFFSET + 0x00)
4414 +#define FE_MDIO_CFG (FE_FE_OFFSET + 0x04)
4415 +#define FE_FE_GLO_CFG (FE_FE_OFFSET + 0x08)
4416 +#define FE_FE_RST_GL (FE_FE_OFFSET + 0x0C)
4417 +#define FE_FE_INT_STATUS (FE_FE_OFFSET + 0x10)
4418 +#define FE_FE_INT_ENABLE (FE_FE_OFFSET + 0x14)
4419 +#define FE_MDIO_CFG2 (FE_FE_OFFSET + 0x18)
4420 +#define FE_FOC_TS_T (FE_FE_OFFSET + 0x1C)
4422 +#define FE_GDMA1_FWD_CFG (FE_GDMA_OFFSET + 0x00)
4423 +#define FE_GDMA1_SCH_CFG (FE_GDMA_OFFSET + 0x04)
4424 +#define FE_GDMA1_SHPR_CFG (FE_GDMA_OFFSET + 0x08)
4425 +#define FE_GDMA1_MAC_ADRL (FE_GDMA_OFFSET + 0x0C)
4426 +#define FE_GDMA1_MAC_ADRH (FE_GDMA_OFFSET + 0x10)
4428 +#define FE_GDMA2_FWD_CFG (FE_GDMA2_OFFSET + 0x00)
4429 +#define FE_GDMA2_SCH_CFG (FE_GDMA2_OFFSET + 0x04)
4430 +#define FE_GDMA2_SHPR_CFG (FE_GDMA2_OFFSET + 0x08)
4431 +#define FE_GDMA2_MAC_ADRL (FE_GDMA2_OFFSET + 0x0C)
4432 +#define FE_GDMA2_MAC_ADRH (FE_GDMA2_OFFSET + 0x10)
4434 +#define FE_PSE_FQ_CFG (FE_PSE_OFFSET + 0x00)
4435 +#define FE_CDMA_FC_CFG (FE_PSE_OFFSET + 0x04)
4436 +#define FE_GDMA1_FC_CFG (FE_PSE_OFFSET + 0x08)
4437 +#define FE_GDMA2_FC_CFG (FE_PSE_OFFSET + 0x0C)
4439 +#define FE_CDMA_CSG_CFG (FE_CDMA_OFFSET + 0x00)
4440 +#define FE_CDMA_SCH_CFG (FE_CDMA_OFFSET + 0x04)
4442 +#define MT7620A_GDMA_OFFSET 0x0600
4443 +#define MT7620A_GDMA1_FWD_CFG (MT7620A_GDMA_OFFSET + 0x00)
4444 +#define MT7620A_FE_GDMA1_SCH_CFG (MT7620A_GDMA_OFFSET + 0x04)
4445 +#define MT7620A_FE_GDMA1_SHPR_CFG (MT7620A_GDMA_OFFSET + 0x08)
4446 +#define MT7620A_FE_GDMA1_MAC_ADRL (MT7620A_GDMA_OFFSET + 0x0C)
4447 +#define MT7620A_FE_GDMA1_MAC_ADRH (MT7620A_GDMA_OFFSET + 0x10)
4449 +#define RT5350_TX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x00)
4450 +#define RT5350_TX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x04)
4451 +#define RT5350_TX_CTX_IDX0 (RT5350_PDMA_OFFSET + 0x08)
4452 +#define RT5350_TX_DTX_IDX0 (RT5350_PDMA_OFFSET + 0x0C)
4453 +#define RT5350_TX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x10)
4454 +#define RT5350_TX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x14)
4455 +#define RT5350_TX_CTX_IDX1 (RT5350_PDMA_OFFSET + 0x18)
4456 +#define RT5350_TX_DTX_IDX1 (RT5350_PDMA_OFFSET + 0x1C)
4457 +#define RT5350_TX_BASE_PTR2 (RT5350_PDMA_OFFSET + 0x20)
4458 +#define RT5350_TX_MAX_CNT2 (RT5350_PDMA_OFFSET + 0x24)
4459 +#define RT5350_TX_CTX_IDX2 (RT5350_PDMA_OFFSET + 0x28)
4460 +#define RT5350_TX_DTX_IDX2 (RT5350_PDMA_OFFSET + 0x2C)
4461 +#define RT5350_TX_BASE_PTR3 (RT5350_PDMA_OFFSET + 0x30)
4462 +#define RT5350_TX_MAX_CNT3 (RT5350_PDMA_OFFSET + 0x34)
4463 +#define RT5350_TX_CTX_IDX3 (RT5350_PDMA_OFFSET + 0x38)
4464 +#define RT5350_TX_DTX_IDX3 (RT5350_PDMA_OFFSET + 0x3C)
4465 +#define RT5350_RX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x100)
4466 +#define RT5350_RX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x104)
4467 +#define RT5350_RX_CALC_IDX0 (RT5350_PDMA_OFFSET + 0x108)
4468 +#define RT5350_RX_DRX_IDX0 (RT5350_PDMA_OFFSET + 0x10C)
4469 +#define RT5350_RX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x110)
4470 +#define RT5350_RX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x114)
4471 +#define RT5350_RX_CALC_IDX1 (RT5350_PDMA_OFFSET + 0x118)
4472 +#define RT5350_RX_DRX_IDX1 (RT5350_PDMA_OFFSET + 0x11C)
4473 +#define RT5350_PDMA_GLO_CFG (RT5350_PDMA_OFFSET + 0x204)
4474 +#define RT5350_PDMA_RST_CFG (RT5350_PDMA_OFFSET + 0x208)
4475 +#define RT5350_DLY_INT_CFG (RT5350_PDMA_OFFSET + 0x20c)
4476 +#define RT5350_FE_INT_STATUS (RT5350_PDMA_OFFSET + 0x220)
4477 +#define RT5350_FE_INT_ENABLE (RT5350_PDMA_OFFSET + 0x228)
4478 +#define RT5350_PDMA_SCH_CFG (RT5350_PDMA_OFFSET + 0x280)
4480 +#define FE_PDMA_GLO_CFG (FE_PDMA_OFFSET + 0x00)
4481 +#define FE_PDMA_RST_CFG (FE_PDMA_OFFSET + 0x04)
4482 +#define FE_PDMA_SCH_CFG (FE_PDMA_OFFSET + 0x08)
4483 +#define FE_DLY_INT_CFG (FE_PDMA_OFFSET + 0x0C)
4484 +#define FE_TX_BASE_PTR0 (FE_PDMA_OFFSET + 0x10)
4485 +#define FE_TX_MAX_CNT0 (FE_PDMA_OFFSET + 0x14)
4486 +#define FE_TX_CTX_IDX0 (FE_PDMA_OFFSET + 0x18)
4487 +#define FE_TX_DTX_IDX0 (FE_PDMA_OFFSET + 0x1C)
4488 +#define FE_TX_BASE_PTR1 (FE_PDMA_OFFSET + 0x20)
4489 +#define FE_TX_MAX_CNT1 (FE_PDMA_OFFSET + 0x24)
4490 +#define FE_TX_CTX_IDX1 (FE_PDMA_OFFSET + 0x28)
4491 +#define FE_TX_DTX_IDX1 (FE_PDMA_OFFSET + 0x2C)
4492 +#define FE_RX_BASE_PTR0 (FE_PDMA_OFFSET + 0x30)
4493 +#define FE_RX_MAX_CNT0 (FE_PDMA_OFFSET + 0x34)
4494 +#define FE_RX_CALC_IDX0 (FE_PDMA_OFFSET + 0x38)
4495 +#define FE_RX_DRX_IDX0 (FE_PDMA_OFFSET + 0x3C)
4496 +#define FE_TX_BASE_PTR2 (FE_PDMA_OFFSET + 0x40)
4497 +#define FE_TX_MAX_CNT2 (FE_PDMA_OFFSET + 0x44)
4498 +#define FE_TX_CTX_IDX2 (FE_PDMA_OFFSET + 0x48)
4499 +#define FE_TX_DTX_IDX2 (FE_PDMA_OFFSET + 0x4C)
4500 +#define FE_TX_BASE_PTR3 (FE_PDMA_OFFSET + 0x50)
4501 +#define FE_TX_MAX_CNT3 (FE_PDMA_OFFSET + 0x54)
4502 +#define FE_TX_CTX_IDX3 (FE_PDMA_OFFSET + 0x58)
4503 +#define FE_TX_DTX_IDX3 (FE_PDMA_OFFSET + 0x5C)
4504 +#define FE_RX_BASE_PTR1 (FE_PDMA_OFFSET + 0x60)
4505 +#define FE_RX_MAX_CNT1 (FE_PDMA_OFFSET + 0x64)
4506 +#define FE_RX_CALC_IDX1 (FE_PDMA_OFFSET + 0x68)
4507 +#define FE_RX_DRX_IDX1 (FE_PDMA_OFFSET + 0x6C)
4509 +#define RT5350_SDM_CFG (RT5350_SDM_OFFSET + 0x00) //Switch DMA configuration
4510 +#define RT5350_SDM_RRING (RT5350_SDM_OFFSET + 0x04) //Switch DMA Rx Ring
4511 +#define RT5350_SDM_TRING (RT5350_SDM_OFFSET + 0x08) //Switch DMA Tx Ring
4512 +#define RT5350_SDM_MAC_ADRL (RT5350_SDM_OFFSET + 0x0C) //Switch MAC address LSB
4513 +#define RT5350_SDM_MAC_ADRH (RT5350_SDM_OFFSET + 0x10) //Switch MAC Address MSB
4514 +#define RT5350_SDM_TPCNT (RT5350_SDM_OFFSET + 0x100) //Switch DMA Tx packet count
4515 +#define RT5350_SDM_TBCNT (RT5350_SDM_OFFSET + 0x104) //Switch DMA Tx byte count
4516 +#define RT5350_SDM_RPCNT (RT5350_SDM_OFFSET + 0x108) //Switch DMA rx packet count
4517 +#define RT5350_SDM_RBCNT (RT5350_SDM_OFFSET + 0x10C) //Switch DMA rx byte count
4518 +#define RT5350_SDM_CS_ERR (RT5350_SDM_OFFSET + 0x110) //Switch DMA rx checksum error count
4520 +#define RT5350_SDM_ICS_EN BIT(16)
4521 +#define RT5350_SDM_TCS_EN BIT(17)
4522 +#define RT5350_SDM_UCS_EN BIT(18)
4525 +/* MDIO_CFG register bits */
4526 +#define FE_MDIO_CFG_AUTO_POLL_EN BIT(29)
4527 +#define FE_MDIO_CFG_GP1_BP_EN BIT(16)
4528 +#define FE_MDIO_CFG_GP1_FRC_EN BIT(15)
4529 +#define FE_MDIO_CFG_GP1_SPEED_10 (0 << 13)
4530 +#define FE_MDIO_CFG_GP1_SPEED_100 (1 << 13)
4531 +#define FE_MDIO_CFG_GP1_SPEED_1000 (2 << 13)
4532 +#define FE_MDIO_CFG_GP1_DUPLEX BIT(12)
4533 +#define FE_MDIO_CFG_GP1_FC_TX BIT(11)
4534 +#define FE_MDIO_CFG_GP1_FC_RX BIT(10)
4535 +#define FE_MDIO_CFG_GP1_LNK_DWN BIT(9)
4536 +#define FE_MDIO_CFG_GP1_AN_FAIL BIT(8)
4537 +#define FE_MDIO_CFG_MDC_CLK_DIV_1 (0 << 6)
4538 +#define FE_MDIO_CFG_MDC_CLK_DIV_2 (1 << 6)
4539 +#define FE_MDIO_CFG_MDC_CLK_DIV_4 (2 << 6)
4540 +#define FE_MDIO_CFG_MDC_CLK_DIV_8 (3 << 6)
4541 +#define FE_MDIO_CFG_TURBO_MII_FREQ BIT(5)
4542 +#define FE_MDIO_CFG_TURBO_MII_MODE BIT(4)
4543 +#define FE_MDIO_CFG_RX_CLK_SKEW_0 (0 << 2)
4544 +#define FE_MDIO_CFG_RX_CLK_SKEW_200 (1 << 2)
4545 +#define FE_MDIO_CFG_RX_CLK_SKEW_400 (2 << 2)
4546 +#define FE_MDIO_CFG_RX_CLK_SKEW_INV (3 << 2)
4547 +#define FE_MDIO_CFG_TX_CLK_SKEW_0 0
4548 +#define FE_MDIO_CFG_TX_CLK_SKEW_200 1
4549 +#define FE_MDIO_CFG_TX_CLK_SKEW_400 2
4550 +#define FE_MDIO_CFG_TX_CLK_SKEW_INV 3
4552 +/* uni-cast port */
4553 +#define FE_GDM1_ICS_EN BIT(22)
4554 +#define FE_GDM1_TCS_EN BIT(21)
4555 +#define FE_GDM1_UCS_EN BIT(20)
4556 +#define FE_GDM1_JMB_EN BIT(19)
4557 +#define FE_GDM1_STRPCRC BIT(16)
4558 +#define FE_GDM1_UFRC_P_CPU (0 << 12)
4559 +#define FE_GDM1_UFRC_P_GDMA1 (1 << 12)
4560 +#define FE_GDM1_UFRC_P_PPE (6 << 12)
4563 +#define FE_ICS_GEN_EN BIT(2)
4564 +#define FE_UCS_GEN_EN BIT(1)
4565 +#define FE_TCS_GEN_EN BIT(0)
4568 +#define FE_PST_DRX_IDX0 BIT(16)
4569 +#define FE_PST_DTX_IDX3 BIT(3)
4570 +#define FE_PST_DTX_IDX2 BIT(2)
4571 +#define FE_PST_DTX_IDX1 BIT(1)
4572 +#define FE_PST_DTX_IDX0 BIT(0)
4574 +#define FE_TX_WB_DDONE BIT(6)
4575 +#define FE_RX_DMA_BUSY BIT(3)
4576 +#define FE_TX_DMA_BUSY BIT(1)
4577 +#define FE_RX_DMA_EN BIT(2)
4578 +#define FE_TX_DMA_EN BIT(0)
4580 +#define FE_PDMA_SIZE_4DWORDS (0 << 4)
4581 +#define FE_PDMA_SIZE_8DWORDS (1 << 4)
4582 +#define FE_PDMA_SIZE_16DWORDS (2 << 4)
4584 +#define FE_US_CYC_CNT_MASK 0xff
4585 +#define FE_US_CYC_CNT_SHIFT 0x8
4586 +#define FE_US_CYC_CNT_DIVISOR 1000000
4588 +#define RX_DMA_PLEN0(_x) (((_x) >> 16) & 0x3fff)
4589 +#define RX_DMA_LSO BIT(30)
4590 +#define RX_DMA_DONE BIT(31)
4591 +#define RX_DMA_L4VALID BIT(30)
4594 + unsigned int rxd1;
4595 + unsigned int rxd2;
4596 + unsigned int rxd3;
4597 + unsigned int rxd4;
4598 +} __packed __aligned(4);
4600 +#define TX_DMA_PLEN0_MASK ((0x3fff) << 16)
4601 +#define TX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
4602 +#define TX_DMA_PLEN1(_x) ((_x) & 0x3fff)
4603 +#define TX_DMA_LS1 BIT(14)
4604 +#define TX_DMA_LSO BIT(30)
4605 +#define TX_DMA_DONE BIT(31)
4606 +#define TX_DMA_QN(_x) ((_x) << 16)
4607 +#define TX_DMA_PN(_x) ((_x) << 24)
4608 +#define TX_DMA_QN_MASK TX_DMA_QN(0x7)
4609 +#define TX_DMA_PN_MASK TX_DMA_PN(0x7)
4610 +#define TX_DMA_CHKSUM (0x7 << 29)
4613 + unsigned int txd1;
4614 + unsigned int txd2;
4615 + unsigned int txd3;
4616 + unsigned int txd4;
4617 +} __packed __aligned(4);
4622 + struct phy_device *phy[8];
4623 + struct device_node *phy_node[8];
4624 + const __be32 *phy_fixed[8];
4631 + int (*connect)(struct fe_priv *priv);
4632 + void (*disconnect)(struct fe_priv *priv);
4633 + void (*start)(struct fe_priv *priv);
4634 + void (*stop)(struct fe_priv *priv);
4639 + unsigned char mac[6];
4640 + const u32 *reg_table;
4642 + void (*init_data)(struct fe_soc_data *data);
4643 + void (*reset_fe)(void);
4644 + void (*set_mac)(struct fe_priv *priv, unsigned char *mac);
4645 + void (*fwd_config)(struct fe_priv *priv);
4646 + void (*tx_dma)(struct fe_priv *priv, int idx, struct sk_buff *skb);
4647 + void (*rx_dma)(struct fe_priv *priv, int idx, int len);
4648 + int (*switch_init)(struct fe_priv *priv);
4649 + int (*switch_config)(struct fe_priv *priv);
4650 + void (*port_init)(struct fe_priv *priv, struct device_node *port);
4651 + int (*has_carrier)(struct fe_priv *priv);
4652 + int (*mdio_init)(struct fe_priv *priv);
4653 + void (*mdio_cleanup)(struct fe_priv *priv);
4654 + int (*mdio_write)(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
4655 + int (*mdio_read)(struct mii_bus *bus, int phy_addr, int phy_reg);
4656 + void (*mdio_adjust_link)(struct fe_priv *priv, int port);
4657 + int (*get_skb_header)(struct sk_buff *skb, void **iphdr, void **tcph, u64 *hdr_flags, void *priv);
4671 + spinlock_t page_lock;
4673 + struct fe_soc_data *soc;
4674 + struct net_device *netdev;
4675 + struct device *device;
4676 + unsigned long sysclk;
4678 + struct fe_rx_dma *rx_dma;
4679 + struct napi_struct rx_napi;
4680 + struct sk_buff *rx_skb[NUM_DMA_DESC];
4681 + dma_addr_t rx_phys;
4683 + struct fe_tx_dma *tx_dma;
4684 + struct tasklet_struct tx_tasklet;
4685 + struct sk_buff *tx_skb[NUM_DMA_DESC];
4686 + dma_addr_t tx_phys;
4687 + unsigned int tx_free_idx;
4689 + struct fe_phy *phy;
4690 + struct mii_bus *mii_bus;
4691 + int mii_irq[PHY_MAX_ADDR];
4695 + struct net_lro_mgr lro_mgr;
4696 + struct net_lro_desc lro_arr[8];
4699 +extern const struct of_device_id of_fe_match[];
4701 +void fe_w32(u32 val, unsigned reg);
4702 +u32 fe_r32(unsigned reg);
4704 +#endif /* FE_ETH_H */
4705 diff --git a/drivers/net/ethernet/ralink/soc_mt7620.c b/drivers/net/ethernet/ralink/soc_mt7620.c
4706 new file mode 100644
4707 index 0000000..79b7b85
4709 +++ b/drivers/net/ethernet/ralink/soc_mt7620.c
4712 + * This program is free software; you can redistribute it and/or modify
4713 + * it under the terms of the GNU General Public License as published by
4714 + * the Free Software Foundation; version 2 of the License
4716 + * This program is distributed in the hope that it will be useful,
4717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4719 + * GNU General Public License for more details.
4721 + * You should have received a copy of the GNU General Public License
4722 + * along with this program; if not, write to the Free Software
4723 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4725 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4728 +#include <linux/module.h>
4729 +#include <linux/platform_device.h>
4730 +#include <linux/if_vlan.h>
4732 +#include <asm/mach-ralink/ralink_regs.h>
4734 +#include <mt7620.h>
4735 +#include "ralink_soc_eth.h"
4736 +#include "gsw_mt7620a.h"
4738 +#define MT7620A_CDMA_CSG_CFG 0x400
4739 +#define MT7620_DMA_VID (MT7620A_CDMA_CSG_CFG | 0x30)
4740 +#define MT7620A_DMA_2B_OFFSET BIT(31)
4741 +#define MT7620A_RESET_FE BIT(21)
4742 +#define MT7620A_RESET_ESW BIT(23)
4743 +#define MT7620_L4_VALID BIT(23)
4745 +#define SYSC_REG_RESET_CTRL 0x34
4746 +#define MAX_RX_LENGTH 1536
4748 +#define CDMA_ICS_EN BIT(2)
4749 +#define CDMA_UCS_EN BIT(1)
4750 +#define CDMA_TCS_EN BIT(0)
4752 +#define GDMA_ICS_EN BIT(22)
4753 +#define GDMA_TCS_EN BIT(21)
4754 +#define GDMA_UCS_EN BIT(20)
4756 +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
4757 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
4758 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
4759 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
4760 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
4761 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
4762 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
4763 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
4764 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
4765 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
4766 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
4767 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
4768 + [FE_REG_FE_DMA_VID_BASE] = MT7620_DMA_VID,
4771 +static void mt7620_fe_reset(void)
4773 + rt_sysc_w32(MT7620A_RESET_FE | MT7620A_RESET_ESW, SYSC_REG_RESET_CTRL);
4774 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
4777 +static void mt7620_fwd_config(struct fe_priv *priv)
4781 + /* frame engine will push VLAN tag regarding to VIDX feild in Tx desc. */
4782 + for (i = 0; i < 16; i += 2)
4783 + fe_w32(((i + 1) << 16) + i, MT7620_DMA_VID + (i * 2));
4785 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~7, MT7620A_GDMA1_FWD_CFG);
4786 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN | GDMA_TCS_EN | GDMA_UCS_EN), MT7620A_GDMA1_FWD_CFG);
4787 + fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) | (CDMA_ICS_EN | CDMA_UCS_EN | CDMA_TCS_EN), MT7620A_CDMA_CSG_CFG);
4790 +static void mt7620_tx_dma(struct fe_priv *priv, int idx, struct sk_buff *skb)
4792 + unsigned int nr_frags = 0;
4793 + unsigned int len = 0;
4796 + nr_frags = skb_shinfo(skb)->nr_frags;
4797 + len = skb->len - skb->data_len;
4801 + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_DONE;
4802 + else if (!nr_frags)
4803 + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(len);
4805 + priv->tx_dma[idx].txd2 = TX_DMA_PLEN0(len);
4807 + if(skb && vlan_tx_tag_present(skb))
4808 + priv->tx_dma[idx].txd4 = 0x80 | (vlan_tx_tag_get(skb) >> 13) << 4 | (vlan_tx_tag_get(skb) & 0xF);
4810 + priv->tx_dma[idx].txd4 = 0;
4813 +static void mt7620_rx_dma(struct fe_priv *priv, int idx, int len)
4815 + priv->rx_dma[idx].rxd2 = RX_DMA_PLEN0(len);
4818 +#ifdef CONFIG_INET_LRO
4820 +mt7620_get_skb_header(struct sk_buff *skb, void **iphdr, void **tcph,
4821 + u64 *hdr_flags, void *_priv)
4823 + struct iphdr *iph = NULL;
4827 + * Make sure that this packet is Ethernet II, is not VLAN
4828 + * tagged, is IPv4, has a valid IP header, and is TCP.
4830 + if (skb->protocol == 0x0081)
4831 + vhdr_len = VLAN_HLEN;
4833 + iph = (struct iphdr *)(skb->data + vhdr_len);
4834 + if(iph->protocol != IPPROTO_TCP)
4838 + *tcph = skb->data + (iph->ihl << 2) + vhdr_len;
4839 + *hdr_flags = LRO_IPV4 | LRO_TCP;
4845 +static void mt7620_init_data(struct fe_soc_data *data)
4847 + if (mt7620_get_eco() >= 5)
4851 +static struct fe_soc_data mt7620_data = {
4852 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4853 + .init_data = mt7620_init_data,
4854 + .reset_fe = mt7620_fe_reset,
4855 + .set_mac = mt7620_set_mac,
4856 + .fwd_config = mt7620_fwd_config,
4857 + .tx_dma = mt7620_tx_dma,
4858 + .rx_dma = mt7620_rx_dma,
4859 + .switch_init = mt7620_gsw_probe,
4860 + .switch_config = mt7620_gsw_config,
4861 + .port_init = mt7620_port_init,
4863 + .reg_table = rt5350_reg_table,
4864 + .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS | MT7620A_DMA_2B_OFFSET,
4865 + .rx_dly_int = RT5350_RX_DLY_INT,
4866 + .tx_dly_int = RT5350_TX_DLY_INT,
4867 + .checksum_bit = MT7620_L4_VALID,
4868 + .has_carrier = mt7620a_has_carrier,
4869 + .mdio_read = mt7620_mdio_read,
4870 + .mdio_write = mt7620_mdio_write,
4871 + .mdio_adjust_link = mt7620_mdio_link_adjust,
4872 +#ifdef CONFIG_INET_LRO
4873 + .get_skb_header = mt7620_get_skb_header,
4877 +const struct of_device_id of_fe_match[] = {
4878 + { .compatible = "ralink,mt7620a-eth", .data = &mt7620_data },
4882 +MODULE_DEVICE_TABLE(of, of_fe_match);
4883 diff --git a/drivers/net/ethernet/ralink/soc_rt2880.c b/drivers/net/ethernet/ralink/soc_rt2880.c
4884 new file mode 100644
4885 index 0000000..e0c418b
4887 +++ b/drivers/net/ethernet/ralink/soc_rt2880.c
4890 + * This program is free software; you can redistribute it and/or modify
4891 + * it under the terms of the GNU General Public License as published by
4892 + * the Free Software Foundation; version 2 of the License
4894 + * This program is distributed in the hope that it will be useful,
4895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4897 + * GNU General Public License for more details.
4899 + * You should have received a copy of the GNU General Public License
4900 + * along with this program; if not, write to the Free Software
4901 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4903 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4906 +#include <linux/module.h>
4908 +#include <asm/mach-ralink/ralink_regs.h>
4910 +#include "ralink_soc_eth.h"
4911 +#include "mdio_rt2880.h"
4913 +#define SYSC_REG_RESET_CTRL 0x034
4914 +#define RT2880_RESET_FE BIT(18)
4916 +void rt2880_fe_reset(void)
4918 + rt_sysc_w32(RT2880_RESET_FE, SYSC_REG_RESET_CTRL);
4921 +struct fe_soc_data rt2880_data = {
4922 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4923 + .reset_fe = rt2880_fe_reset,
4924 + .min_pkt_len = 64,
4925 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
4926 + .checksum_bit = RX_DMA_L4VALID,
4927 + .rx_dly_int = FE_RX_DLY_INT,
4928 + .tx_dly_int = FE_TX_DLY_INT,
4929 + .mdio_read = rt2880_mdio_read,
4930 + .mdio_write = rt2880_mdio_write,
4931 + .mdio_adjust_link = rt2880_mdio_link_adjust,
4932 + .port_init = rt2880_port_init,
4935 +const struct of_device_id of_fe_match[] = {
4936 + { .compatible = "ralink,rt2880-eth", .data = &rt2880_data },
4940 +MODULE_DEVICE_TABLE(of, of_fe_match);
4941 diff --git a/drivers/net/ethernet/ralink/soc_rt305x.c b/drivers/net/ethernet/ralink/soc_rt305x.c
4942 new file mode 100644
4943 index 0000000..482ca1f
4945 +++ b/drivers/net/ethernet/ralink/soc_rt305x.c
4948 + * This program is free software; you can redistribute it and/or modify
4949 + * it under the terms of the GNU General Public License as published by
4950 + * the Free Software Foundation; version 2 of the License
4952 + * This program is distributed in the hope that it will be useful,
4953 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4954 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4955 + * GNU General Public License for more details.
4957 + * You should have received a copy of the GNU General Public License
4958 + * along with this program; if not, write to the Free Software
4959 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4961 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4964 +#include <linux/module.h>
4966 +#include <asm/mach-ralink/ralink_regs.h>
4968 +#include "ralink_soc_eth.h"
4970 +#define RT305X_RESET_FE BIT(21)
4971 +#define RT305X_RESET_ESW BIT(23)
4972 +#define SYSC_REG_RESET_CTRL 0x034
4974 +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
4975 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
4976 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
4977 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
4978 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
4979 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
4980 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
4981 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
4982 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
4983 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
4984 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
4985 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
4986 + [FE_REG_FE_DMA_VID_BASE] = 0,
4989 +static void rt305x_fe_reset(void)
4991 + rt_sysc_w32(RT305X_RESET_FE, SYSC_REG_RESET_CTRL);
4992 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
4995 +static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
4997 + unsigned long flags;
4999 + spin_lock_irqsave(&priv->page_lock, flags);
5000 + fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
5001 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
5002 + RT5350_SDM_MAC_ADRL);
5003 + spin_unlock_irqrestore(&priv->page_lock, flags);
5006 +static void rt5350_fwd_config(struct fe_priv *priv)
5008 + unsigned long sysclk = priv->sysclk;
5011 + sysclk /= FE_US_CYC_CNT_DIVISOR;
5012 + sysclk <<= FE_US_CYC_CNT_SHIFT;
5014 + fe_w32((fe_r32(FE_FE_GLO_CFG) &
5015 + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
5019 + fe_w32(fe_r32(RT5350_SDM_CFG) & ~0xffff, RT5350_SDM_CFG);
5020 + fe_w32(fe_r32(RT5350_SDM_CFG) | RT5350_SDM_ICS_EN | RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN,
5024 +static void rt5350_fe_reset(void)
5026 + rt_sysc_w32(RT305X_RESET_FE | RT305X_RESET_ESW, SYSC_REG_RESET_CTRL);
5027 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
5030 +static struct fe_soc_data rt3050_data = {
5031 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5032 + .reset_fe = rt305x_fe_reset,
5033 + .min_pkt_len = 64,
5034 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
5035 + .checksum_bit = RX_DMA_L4VALID,
5036 + .rx_dly_int = FE_RX_DLY_INT,
5037 + .tx_dly_int = FE_TX_DLY_INT,
5040 +static struct fe_soc_data rt5350_data = {
5041 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5042 + .reg_table = rt5350_reg_table,
5043 + .reset_fe = rt5350_fe_reset,
5044 + .set_mac = rt5350_set_mac,
5045 + .fwd_config = rt5350_fwd_config,
5046 + .min_pkt_len = 64,
5047 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
5048 + .checksum_bit = RX_DMA_L4VALID,
5049 + .rx_dly_int = RT5350_RX_DLY_INT,
5050 + .tx_dly_int = RT5350_TX_DLY_INT,
5053 +const struct of_device_id of_fe_match[] = {
5054 + { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
5055 + { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
5059 +MODULE_DEVICE_TABLE(of, of_fe_match);
5060 diff --git a/drivers/net/ethernet/ralink/soc_rt3883.c b/drivers/net/ethernet/ralink/soc_rt3883.c
5061 new file mode 100644
5062 index 0000000..c660529c
5064 +++ b/drivers/net/ethernet/ralink/soc_rt3883.c
5067 + * This program is free software; you can redistribute it and/or modify
5068 + * it under the terms of the GNU General Public License as published by
5069 + * the Free Software Foundation; version 2 of the License
5071 + * This program is distributed in the hope that it will be useful,
5072 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5073 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5074 + * GNU General Public License for more details.
5076 + * You should have received a copy of the GNU General Public License
5077 + * along with this program; if not, write to the Free Software
5078 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
5080 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
5083 +#include <linux/module.h>
5085 +#include <asm/mach-ralink/ralink_regs.h>
5087 +#include "ralink_soc_eth.h"
5088 +#include "mdio_rt2880.h"
5090 +#define RT3883_SYSC_REG_RSTCTRL 0x34
5091 +#define RT3883_RSTCTRL_FE BIT(21)
5093 +static void rt3883_fe_reset(void)
5097 + t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
5098 + t |= RT3883_RSTCTRL_FE;
5099 + rt_sysc_w32(t , RT3883_SYSC_REG_RSTCTRL);
5101 + t &= ~RT3883_RSTCTRL_FE;
5102 + rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
5105 +static struct fe_soc_data rt3883_data = {
5106 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
5107 + .reset_fe = rt3883_fe_reset,
5108 + .min_pkt_len = 64,
5109 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
5110 + .rx_dly_int = FE_RX_DLY_INT,
5111 + .tx_dly_int = FE_TX_DLY_INT,
5112 + .checksum_bit = RX_DMA_L4VALID,
5113 + .mdio_read = rt2880_mdio_read,
5114 + .mdio_write = rt2880_mdio_write,
5115 + .mdio_adjust_link = rt2880_mdio_link_adjust,
5116 + .port_init = rt2880_port_init,
5119 +const struct of_device_id of_fe_match[] = {
5120 + { .compatible = "ralink,rt3883-eth", .data = &rt3883_data },
5124 +MODULE_DEVICE_TABLE(of, of_fe_match);