1 From 1efe3ce91ab951090ac8db1872f0de32e0a88de8 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] 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 | 31 +
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 | 1027 ++++++++++++++
19 drivers/net/ethernet/ralink/gsw_mt7620a.h | 29 +
20 drivers/net/ethernet/ralink/mdio.c | 245 ++++
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/ralink_soc_eth.c | 746 ++++++++++
25 drivers/net/ethernet/ralink/ralink_soc_eth.h | 374 +++++
26 drivers/net/ethernet/ralink/soc_mt7620.c | 111 ++
27 drivers/net/ethernet/ralink/soc_rt2880.c | 51 +
28 drivers/net/ethernet/ralink/soc_rt305x.c | 113 ++
29 drivers/net/ethernet/ralink/soc_rt3883.c | 60 +
30 drivers/watchdog/rt2880_wdt.c | 9 +-
31 21 files changed, 4622 insertions(+), 4 deletions(-)
32 create mode 100644 arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
33 create mode 100644 drivers/net/ethernet/ralink/Kconfig
34 create mode 100644 drivers/net/ethernet/ralink/Makefile
35 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.c
36 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.h
37 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.c
38 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.h
39 create mode 100644 drivers/net/ethernet/ralink/mdio.c
40 create mode 100644 drivers/net/ethernet/ralink/mdio.h
41 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.c
42 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.h
43 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.c
44 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.h
45 create mode 100644 drivers/net/ethernet/ralink/soc_mt7620.c
46 create mode 100644 drivers/net/ethernet/ralink/soc_rt2880.c
47 create mode 100644 drivers/net/ethernet/ralink/soc_rt305x.c
48 create mode 100644 drivers/net/ethernet/ralink/soc_rt3883.c
51 +++ b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
54 + * Ralink RT305x SoC platform device registration
56 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
58 + * This program is free software; you can redistribute it and/or modify it
59 + * under the terms of the GNU General Public License version 2 as published
60 + * by the Free Software Foundation.
63 +#ifndef _RT305X_ESW_PLATFORM_H
64 +#define _RT305X_ESW_PLATFORM_H
67 + RT305X_ESW_VLAN_CONFIG_NONE = 0,
68 + RT305X_ESW_VLAN_CONFIG_LLLLW,
69 + RT305X_ESW_VLAN_CONFIG_WLLLL,
72 +struct rt305x_esw_platform_data
75 + u32 reg_initval_fct2;
76 + u32 reg_initval_fpa2;
79 +#endif /* _RT305X_ESW_PLATFORM_H */
80 --- a/arch/mips/ralink/rt305x.c
81 +++ b/arch/mips/ralink/rt305x.c
82 @@ -221,6 +221,7 @@ void __init ralink_clk_init(void)
85 ralink_clk_add("cpu", cpu_rate);
86 + ralink_clk_add("sys", sys_rate);
87 ralink_clk_add("10000b00.spi", sys_rate);
88 ralink_clk_add("10000100.timer", wdt_rate);
89 ralink_clk_add("10000120.watchdog", wdt_rate);
90 --- a/drivers/net/ethernet/Kconfig
91 +++ b/drivers/net/ethernet/Kconfig
92 @@ -135,6 +135,7 @@ config ETHOC
93 source "drivers/net/ethernet/packetengines/Kconfig"
94 source "drivers/net/ethernet/pasemi/Kconfig"
95 source "drivers/net/ethernet/qlogic/Kconfig"
96 +source "drivers/net/ethernet/ralink/Kconfig"
97 source "drivers/net/ethernet/realtek/Kconfig"
98 source "drivers/net/ethernet/renesas/Kconfig"
99 source "drivers/net/ethernet/rdc/Kconfig"
100 --- a/drivers/net/ethernet/Makefile
101 +++ b/drivers/net/ethernet/Makefile
102 @@ -53,6 +53,7 @@ obj-$(CONFIG_ETHOC) += ethoc.o
103 obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
104 obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
105 obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
106 +obj-$(CONFIG_NET_RALINK) += ralink/
107 obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
108 obj-$(CONFIG_SH_ETH) += renesas/
109 obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
111 +++ b/drivers/net/ethernet/ralink/Kconfig
114 + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
117 + This driver supports the ethernet mac inside the ralink wisocs
121 +config NET_RALINK_MDIO
122 + def_bool NET_RALINK
123 + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
126 +config NET_RALINK_MDIO_RT2880
127 + def_bool NET_RALINK
128 + depends on (SOC_RT288X || SOC_RT3883)
129 + select NET_RALINK_MDIO
131 +config NET_RALINK_ESW_RT3052
132 + def_bool NET_RALINK
133 + depends on SOC_RT305X
137 +config NET_RALINK_GSW_MT7620
138 + def_bool NET_RALINK
139 + depends on SOC_MT7620
140 + select NET_RALINK_MDIO
145 +++ b/drivers/net/ethernet/ralink/Makefile
148 +# Makefile for the Ralink SoCs built-in ethernet macs
151 +ralink-eth-y += ralink_soc_eth.o
153 +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
154 +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
156 +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
157 +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o
159 +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
160 +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
161 +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
162 +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
164 +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
166 +++ b/drivers/net/ethernet/ralink/esw_rt3052.c
169 + * This program is free software; you can redistribute it and/or modify
170 + * it under the terms of the GNU General Public License as published by
171 + * the Free Software Foundation; version 2 of the License
173 + * This program is distributed in the hope that it will be useful,
174 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
175 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
176 + * GNU General Public License for more details.
178 + * You should have received a copy of the GNU General Public License
179 + * along with this program; if not, write to the Free Software
180 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
182 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
185 +#include <linux/module.h>
186 +#include <linux/kernel.h>
187 +#include <linux/types.h>
188 +#include <linux/dma-mapping.h>
189 +#include <linux/init.h>
190 +#include <linux/skbuff.h>
191 +#include <linux/etherdevice.h>
192 +#include <linux/ethtool.h>
193 +#include <linux/platform_device.h>
194 +#include <linux/of_device.h>
195 +#include <linux/clk.h>
196 +#include <linux/of_net.h>
197 +#include <linux/of_mdio.h>
199 +#include <asm/mach-ralink/ralink_regs.h>
201 +#include "ralink_soc_eth.h"
203 +#include <linux/ioport.h>
204 +#include <linux/switch.h>
205 +#include <linux/mii.h>
207 +#include <ralink_regs.h>
208 +#include <asm/mach-ralink/rt305x.h>
209 +#include <asm/mach-ralink/rt305x_esw_platform.h>
212 + * HW limitations for this switch:
213 + * - No large frame support (PKT_MAX_LEN at most 1536)
214 + * - Can't have untagged vlan and tagged vlan on one port at the same time,
215 + * though this might be possible using the undocumented PPE.
218 +#define RT305X_ESW_REG_ISR 0x00
219 +#define RT305X_ESW_REG_IMR 0x04
220 +#define RT305X_ESW_REG_FCT0 0x08
221 +#define RT305X_ESW_REG_PFC1 0x14
222 +#define RT305X_ESW_REG_ATS 0x24
223 +#define RT305X_ESW_REG_ATS0 0x28
224 +#define RT305X_ESW_REG_ATS1 0x2c
225 +#define RT305X_ESW_REG_ATS2 0x30
226 +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
227 +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
228 +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
229 +#define RT305X_ESW_REG_POA 0x80
230 +#define RT305X_ESW_REG_FPA 0x84
231 +#define RT305X_ESW_REG_SOCPC 0x8c
232 +#define RT305X_ESW_REG_POC0 0x90
233 +#define RT305X_ESW_REG_POC1 0x94
234 +#define RT305X_ESW_REG_POC2 0x98
235 +#define RT305X_ESW_REG_SGC 0x9c
236 +#define RT305X_ESW_REG_STRT 0xa0
237 +#define RT305X_ESW_REG_PCR0 0xc0
238 +#define RT305X_ESW_REG_PCR1 0xc4
239 +#define RT305X_ESW_REG_FPA2 0xc8
240 +#define RT305X_ESW_REG_FCT2 0xcc
241 +#define RT305X_ESW_REG_SGC2 0xe4
242 +#define RT305X_ESW_REG_P0LED 0xa4
243 +#define RT305X_ESW_REG_P1LED 0xa8
244 +#define RT305X_ESW_REG_P2LED 0xac
245 +#define RT305X_ESW_REG_P3LED 0xb0
246 +#define RT305X_ESW_REG_P4LED 0xb4
247 +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
248 +#define RT305X_ESW_REG_P1PC 0xec
249 +#define RT305X_ESW_REG_P2PC 0xf0
250 +#define RT305X_ESW_REG_P3PC 0xf4
251 +#define RT305X_ESW_REG_P4PC 0xf8
252 +#define RT305X_ESW_REG_P5PC 0xfc
254 +#define RT305X_ESW_LED_LINK 0
255 +#define RT305X_ESW_LED_100M 1
256 +#define RT305X_ESW_LED_DUPLEX 2
257 +#define RT305X_ESW_LED_ACTIVITY 3
258 +#define RT305X_ESW_LED_COLLISION 4
259 +#define RT305X_ESW_LED_LINKACT 5
260 +#define RT305X_ESW_LED_DUPLCOLL 6
261 +#define RT305X_ESW_LED_10MACT 7
262 +#define RT305X_ESW_LED_100MACT 8
263 +/* Additional led states not in datasheet: */
264 +#define RT305X_ESW_LED_BLINK 10
265 +#define RT305X_ESW_LED_ON 12
267 +#define RT305X_ESW_LINK_S 25
268 +#define RT305X_ESW_DUPLEX_S 9
269 +#define RT305X_ESW_SPD_S 0
271 +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
272 +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
273 +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
275 +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
277 +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
278 +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
280 +#define RT305X_ESW_PVIDC_PVID_M 0xfff
281 +#define RT305X_ESW_PVIDC_PVID_S 12
283 +#define RT305X_ESW_VLANI_VID_M 0xfff
284 +#define RT305X_ESW_VLANI_VID_S 12
286 +#define RT305X_ESW_VMSC_MSC_M 0xff
287 +#define RT305X_ESW_VMSC_MSC_S 8
289 +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
290 +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
291 +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
292 +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
294 +#define RT305X_ESW_POC0_EN_BP_S 0
295 +#define RT305X_ESW_POC0_EN_FC_S 8
296 +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
297 +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
298 +#define RT305X_ESW_POC0_DIS_PORT_S 23
300 +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
301 +#define RT305X_ESW_POC2_UNTAG_EN_S 0
302 +#define RT305X_ESW_POC2_ENAGING_S 8
303 +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
305 +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
306 +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
307 +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
308 +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
310 +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
311 +#define RT305X_ESW_PFC1_EN_VLAN_S 16
312 +#define RT305X_ESW_PFC1_EN_TOS_S 24
314 +#define RT305X_ESW_VLAN_NONE 0xfff
316 +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
317 +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
319 +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
320 +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
322 +#define RT305X_ESW_POA_LINK_MASK 0x1f
323 +#define RT305X_ESW_POA_LINK_SHIFT 25
325 +#define RT305X_ESW_PORT_ST_CHG BIT(26)
326 +#define RT305X_ESW_PORT0 0
327 +#define RT305X_ESW_PORT1 1
328 +#define RT305X_ESW_PORT2 2
329 +#define RT305X_ESW_PORT3 3
330 +#define RT305X_ESW_PORT4 4
331 +#define RT305X_ESW_PORT5 5
332 +#define RT305X_ESW_PORT6 6
334 +#define RT305X_ESW_PORTS_NONE 0
336 +#define RT305X_ESW_PMAP_LLLLLL 0x3f
337 +#define RT305X_ESW_PMAP_LLLLWL 0x2f
338 +#define RT305X_ESW_PMAP_WLLLLL 0x3e
340 +#define RT305X_ESW_PORTS_INTERNAL \
341 + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
342 + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
343 + BIT(RT305X_ESW_PORT4))
345 +#define RT305X_ESW_PORTS_NOCPU \
346 + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
348 +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
350 +#define RT305X_ESW_PORTS_ALL \
351 + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
353 +#define RT305X_ESW_NUM_VLANS 16
354 +#define RT305X_ESW_NUM_VIDS 4096
355 +#define RT305X_ESW_NUM_PORTS 7
356 +#define RT305X_ESW_NUM_LANWAN 6
357 +#define RT305X_ESW_NUM_LEDS 5
359 +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
360 +#define RT5350_EWS_REG_LED_POLARITY 0x168
361 +#define RT5350_RESET_EPHY BIT(24)
362 +#define SYSC_REG_RESET_CTRL 0x34
365 + /* Global attributes. */
366 + RT305X_ESW_ATTR_ENABLE_VLAN,
367 + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
368 + RT305X_ESW_ATTR_BC_STATUS,
369 + RT305X_ESW_ATTR_LED_FREQ,
370 + /* Port attributes. */
371 + RT305X_ESW_ATTR_PORT_DISABLE,
372 + RT305X_ESW_ATTR_PORT_DOUBLETAG,
373 + RT305X_ESW_ATTR_PORT_UNTAG,
374 + RT305X_ESW_ATTR_PORT_LED,
375 + RT305X_ESW_ATTR_PORT_LAN,
376 + RT305X_ESW_ATTR_PORT_RECV_BAD,
377 + RT305X_ESW_ATTR_PORT_RECV_GOOD,
378 + RT5350_ESW_ATTR_PORT_TR_BAD,
379 + RT5350_ESW_ATTR_PORT_TR_GOOD,
396 + struct device *dev;
397 + void __iomem *base;
399 + const struct rt305x_esw_platform_data *pdata;
400 + /* Protects against concurrent register rmw operations. */
401 + spinlock_t reg_rw_lock;
403 + unsigned char port_map;
404 + unsigned int reg_initval_fct2;
405 + unsigned int reg_initval_fpa2;
406 + unsigned int reg_led_polarity;
409 + struct switch_dev swdev;
410 + bool global_vlan_enable;
411 + bool alt_vlan_disable;
412 + int bc_storm_protect;
414 + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
415 + struct esw_port ports[RT305X_ESW_NUM_PORTS];
419 +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
421 + __raw_writel(val, esw->base + reg);
424 +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
426 + return __raw_readl(esw->base + reg);
429 +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
434 + t = __raw_readl(esw->base + reg) & ~mask;
435 + __raw_writel(t | val, esw->base + reg);
438 +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
441 + unsigned long flags;
443 + spin_lock_irqsave(&esw->reg_rw_lock, flags);
444 + esw_rmw_raw(esw, reg, mask, val);
445 + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
448 +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
451 + unsigned long t_start = jiffies;
455 + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
456 + RT305X_ESW_PCR1_WT_DONE))
458 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
464 + write_data &= 0xffff;
466 + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
467 + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
468 + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
469 + RT305X_ESW_REG_PCR0);
473 + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
474 + RT305X_ESW_PCR1_WT_DONE)
477 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
484 + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
488 +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
493 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
494 + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
495 + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
500 +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
504 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
506 + RT305X_ESW_REG_VLANI(vlan / 2),
507 + RT305X_ESW_VLANI_VID_M << s,
508 + (vid & RT305X_ESW_VLANI_VID_M) << s);
511 +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
515 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
516 + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
517 + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
520 +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
524 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
526 + RT305X_ESW_REG_PVIDC(port / 2),
527 + RT305X_ESW_PVIDC_PVID_M << s,
528 + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
531 +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
535 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
536 + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
537 + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
542 +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
546 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
548 + RT305X_ESW_REG_VMSC(vlan / 4),
549 + RT305X_ESW_VMSC_MSC_M << s,
550 + (msc & RT305X_ESW_VMSC_MSC_M) << s);
553 +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
556 + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
557 + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
558 + RT305X_ESW_POC0_DIS_PORT_M;
561 +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
564 + unsigned enable_mask;
568 + old_mask = esw_get_port_disable(esw);
569 + changed = old_mask ^ disable_mask;
570 + enable_mask = old_mask & disable_mask;
572 + /* enable before writing to MII */
573 + esw_rmw(esw, RT305X_ESW_REG_POC0,
574 + (RT305X_ESW_POC0_DIS_PORT_M <<
575 + RT305X_ESW_POC0_DIS_PORT_S),
576 + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
578 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
579 + if (!(changed & (1 << i)))
581 + if (disable_mask & (1 << i)) {
583 + rt305x_mii_write(esw, i, MII_BMCR,
587 + rt305x_mii_write(esw, i, MII_BMCR,
595 + /* disable after writing to MII */
596 + esw_rmw(esw, RT305X_ESW_REG_POC0,
597 + (RT305X_ESW_POC0_DIS_PORT_M <<
598 + RT305X_ESW_POC0_DIS_PORT_S),
599 + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
602 +static void esw_set_gsc(struct rt305x_esw *esw)
604 + esw_rmw(esw, RT305X_ESW_REG_SGC,
605 + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
606 + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
607 + esw_rmw(esw, RT305X_ESW_REG_SGC,
608 + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
609 + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
612 +static int esw_apply_config(struct switch_dev *dev);
614 +static void esw_hw_init(struct rt305x_esw *esw)
617 + u8 port_disable = 0;
618 + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
620 + /* vodoo from original driver */
621 + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
622 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
623 + /* Port priority 1 for all ports, vlan enabled. */
624 + esw_w32(esw, 0x00005555 |
625 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
626 + RT305X_ESW_REG_PFC1);
628 + /* Enable Back Pressure, and Flow Control */
630 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
631 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
632 + RT305X_ESW_REG_POC0);
634 + /* Enable Aging, and VLAN TAG removal */
636 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
637 + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
638 + RT305X_ESW_REG_POC2);
640 + if (esw->reg_initval_fct2)
641 + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
643 + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
646 + * 300s aging timer, max packet len 1536, broadcast storm prevention
647 + * disabled, disable collision abort, mac xor48 hash, 10 packet back
648 + * pressure jam, GMII disable was_transmit, back pressure disabled,
649 + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
652 + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
654 + /* Setup SoC Port control register */
656 + (RT305X_ESW_SOCPC_CRC_PADDING |
657 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
658 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
659 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
660 + RT305X_ESW_REG_SOCPC);
662 + if (esw->reg_initval_fpa2)
663 + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
665 + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
666 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
668 + /* Force Link/Activity on ports */
669 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
670 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
671 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
672 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
673 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
675 + /* Copy disabled port configuration from bootloader setup */
676 + port_disable = esw_get_port_disable(esw);
677 + for (i = 0; i < 6; i++)
678 + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
680 + if (soc_is_rt3352()) {
682 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
683 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
684 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
686 + rt305x_mii_write(esw, 0, 31, 0x8000);
687 + for (i = 0; i < 5; i++) {
688 + if (esw->ports[i].disable) {
689 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
691 + rt305x_mii_write(esw, i, MII_BMCR,
696 + /* TX10 waveform coefficient LSB=0 disable PHY */
697 + rt305x_mii_write(esw, i, 26, 0x1601);
698 + /* TX100/TX10 AD/DA current bias */
699 + rt305x_mii_write(esw, i, 29, 0x7016);
700 + /* TX100 slew rate control */
701 + rt305x_mii_write(esw, i, 30, 0x0038);
704 + /* select global register */
705 + rt305x_mii_write(esw, 0, 31, 0x0);
706 + /* enlarge agcsel threshold 3 and threshold 2 */
707 + rt305x_mii_write(esw, 0, 1, 0x4a40);
708 + /* enlarge agcsel threshold 5 and threshold 4 */
709 + rt305x_mii_write(esw, 0, 2, 0x6254);
710 + /* enlarge agcsel threshold */
711 + rt305x_mii_write(esw, 0, 3, 0xa17f);
712 + rt305x_mii_write(esw, 0,12, 0x7eaa);
713 + /* longer TP_IDL tail length */
714 + rt305x_mii_write(esw, 0, 14, 0x65);
715 + /* increased squelch pulse count threshold. */
716 + rt305x_mii_write(esw, 0, 16, 0x0684);
717 + /* set TX10 signal amplitude threshold to minimum */
718 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
719 + /* set squelch amplitude to higher threshold */
720 + rt305x_mii_write(esw, 0, 18, 0x40ba);
721 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
722 + rt305x_mii_write(esw, 0, 22, 0x253f);
723 + /* set PLL/Receive bias current are calibrated */
724 + rt305x_mii_write(esw, 0, 27, 0x2fda);
725 + /* change PLL/Receive bias current to internal(RT3350) */
726 + rt305x_mii_write(esw, 0, 28, 0xc410);
727 + /* change PLL bias current to internal(RT3052_MP3) */
728 + rt305x_mii_write(esw, 0, 29, 0x598b);
729 + /* select local register */
730 + rt305x_mii_write(esw, 0, 31, 0x8000);
731 + } else if (soc_is_rt5350()) {
733 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
734 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
735 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
737 + /* set the led polarity */
738 + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
740 + /* local registers */
741 + rt305x_mii_write(esw, 0, 31, 0x8000);
742 + for (i = 0; i < 5; i++) {
743 + if (esw->ports[i].disable) {
744 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
746 + rt305x_mii_write(esw, i, MII_BMCR,
751 + /* TX10 waveform coefficient LSB=0 disable PHY */
752 + rt305x_mii_write(esw, i, 26, 0x1601);
753 + /* TX100/TX10 AD/DA current bias */
754 + rt305x_mii_write(esw, i, 29, 0x7015);
755 + /* TX100 slew rate control */
756 + rt305x_mii_write(esw, i, 30, 0x0038);
759 + /* global registers */
760 + rt305x_mii_write(esw, 0, 31, 0x0);
761 + /* enlarge agcsel threshold 3 and threshold 2 */
762 + rt305x_mii_write(esw, 0, 1, 0x4a40);
763 + /* enlarge agcsel threshold 5 and threshold 4 */
764 + rt305x_mii_write(esw, 0, 2, 0x6254);
765 + /* enlarge agcsel threshold 6 */
766 + rt305x_mii_write(esw, 0, 3, 0xa17f);
767 + rt305x_mii_write(esw, 0, 12, 0x7eaa);
768 + /* longer TP_IDL tail length */
769 + rt305x_mii_write(esw, 0, 14, 0x65);
770 + /* increased squelch pulse count threshold. */
771 + rt305x_mii_write(esw, 0, 16, 0x0684);
772 + /* set TX10 signal amplitude threshold to minimum */
773 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
774 + /* set squelch amplitude to higher threshold */
775 + rt305x_mii_write(esw, 0, 18, 0x40ba);
776 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
777 + rt305x_mii_write(esw, 0, 22, 0x253f);
778 + /* set PLL/Receive bias current are calibrated */
779 + rt305x_mii_write(esw, 0, 27, 0x2fda);
780 + /* change PLL/Receive bias current to internal(RT3350) */
781 + rt305x_mii_write(esw, 0, 28, 0xc410);
782 + /* change PLL bias current to internal(RT3052_MP3) */
783 + rt305x_mii_write(esw, 0, 29, 0x598b);
784 + /* select local register */
785 + rt305x_mii_write(esw, 0, 31, 0x8000);
787 + rt305x_mii_write(esw, 0, 31, 0x8000);
788 + for (i = 0; i < 5; i++) {
789 + if (esw->ports[i].disable) {
790 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
792 + rt305x_mii_write(esw, i, MII_BMCR,
797 + /* TX10 waveform coefficient */
798 + rt305x_mii_write(esw, i, 26, 0x1601);
799 + /* TX100/TX10 AD/DA current bias */
800 + rt305x_mii_write(esw, i, 29, 0x7058);
801 + /* TX100 slew rate control */
802 + rt305x_mii_write(esw, i, 30, 0x0018);
806 + /* select global register */
807 + rt305x_mii_write(esw, 0, 31, 0x0);
808 + /* tune TP_IDL tail and head waveform */
809 + rt305x_mii_write(esw, 0, 22, 0x052f);
810 + /* set TX10 signal amplitude threshold to minimum */
811 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
812 + /* set squelch amplitude to higher threshold */
813 + rt305x_mii_write(esw, 0, 18, 0x40ba);
814 + /* longer TP_IDL tail length */
815 + rt305x_mii_write(esw, 0, 14, 0x65);
816 + /* select local register */
817 + rt305x_mii_write(esw, 0, 31, 0x8000);
821 + port_map = esw->port_map;
823 + port_map = RT305X_ESW_PMAP_LLLLLL;
826 + * Unused HW feature, but still nice to be consistent here...
827 + * This is also exported to userspace ('lan' attribute) so it's
828 + * conveniently usable to decide which ports go into the wan vlan by
831 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
832 + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
833 + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
835 + /* make the switch leds blink */
836 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
837 + esw->ports[i].led = 0x05;
839 + /* Apply the empty config. */
840 + esw_apply_config(&esw->swdev);
842 + /* Only unmask the port change interrupt */
843 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
846 +static irqreturn_t esw_interrupt(int irq, void *_esw)
848 + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
851 + status = esw_r32(esw, RT305X_ESW_REG_ISR);
852 + if (status & RT305X_ESW_PORT_ST_CHG) {
853 + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
854 + link >>= RT305X_ESW_POA_LINK_SHIFT;
855 + link &= RT305X_ESW_POA_LINK_MASK;
856 + dev_info(esw->dev, "link changed 0x%02X\n", link);
858 + esw_w32(esw, status, RT305X_ESW_REG_ISR);
860 + return IRQ_HANDLED;
863 +static int esw_apply_config(struct switch_dev *dev)
865 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
872 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
874 + if (esw->global_vlan_enable) {
875 + vid = esw->vlans[i].vid;
876 + vmsc = esw->vlans[i].ports;
878 + vid = RT305X_ESW_VLAN_NONE;
879 + vmsc = RT305X_ESW_PORTS_NONE;
881 + esw_set_vlan_id(esw, i, vid);
882 + esw_set_vmsc(esw, i, vmsc);
885 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
887 + disable |= esw->ports[i].disable << i;
888 + if (esw->global_vlan_enable) {
889 + doubletag |= esw->ports[i].doubletag << i;
891 + untag |= esw->ports[i].untag << i;
892 + pvid = esw->ports[i].pvid;
894 + int x = esw->alt_vlan_disable ? 0 : 1;
895 + doubletag |= x << i;
900 + esw_set_pvid(esw, i, pvid);
901 + if (i < RT305X_ESW_NUM_LEDS)
902 + esw_w32(esw, esw->ports[i].led,
903 + RT305X_ESW_REG_P0LED + 4*i);
907 + esw_set_port_disable(esw, disable);
908 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
909 + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
910 + RT305X_ESW_SGC2_DOUBLE_TAG_S),
911 + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
912 + esw_rmw(esw, RT305X_ESW_REG_PFC1,
913 + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
914 + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
915 + esw_rmw(esw, RT305X_ESW_REG_POC2,
916 + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
917 + untag << RT305X_ESW_POC2_UNTAG_EN_S);
919 + if (!esw->global_vlan_enable) {
921 + * Still need to put all ports into vlan 0 or they'll be
923 + * NOTE: vlan 0 is special, no vlan tag is prepended
925 + esw_set_vlan_id(esw, 0, 0);
926 + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
932 +static int esw_reset_switch(struct switch_dev *dev)
934 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
936 + esw->global_vlan_enable = 0;
937 + memset(esw->ports, 0, sizeof(esw->ports));
938 + memset(esw->vlans, 0, sizeof(esw->vlans));
944 +static int esw_get_vlan_enable(struct switch_dev *dev,
945 + const struct switch_attr *attr,
946 + struct switch_val *val)
948 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
950 + val->value.i = esw->global_vlan_enable;
955 +static int esw_set_vlan_enable(struct switch_dev *dev,
956 + const struct switch_attr *attr,
957 + struct switch_val *val)
959 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
961 + esw->global_vlan_enable = val->value.i != 0;
966 +static int esw_get_alt_vlan_disable(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->alt_vlan_disable;
977 +static int esw_set_alt_vlan_disable(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->alt_vlan_disable = val->value.i != 0;
989 +rt305x_esw_set_bc_status(struct switch_dev *dev,
990 + const struct switch_attr *attr,
991 + struct switch_val *val)
993 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
995 + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
1001 +rt305x_esw_get_bc_status(struct switch_dev *dev,
1002 + const struct switch_attr *attr,
1003 + struct switch_val *val)
1005 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1007 + val->value.i = esw->bc_storm_protect;
1013 +rt305x_esw_set_led_freq(struct switch_dev *dev,
1014 + const struct switch_attr *attr,
1015 + struct switch_val *val)
1017 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1019 + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
1025 +rt305x_esw_get_led_freq(struct switch_dev *dev,
1026 + const struct switch_attr *attr,
1027 + struct switch_val *val)
1029 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1031 + val->value.i = esw->led_frequency;
1036 +static int esw_get_port_link(struct switch_dev *dev,
1038 + struct switch_port_link *link)
1040 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1043 + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
1046 + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
1048 + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
1049 + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
1050 + if (port < RT305X_ESW_NUM_LEDS) {
1051 + speed = (poa >> RT305X_ESW_SPD_S) & 1;
1053 + if (port == RT305X_ESW_NUM_PORTS - 1)
1055 + speed = (poa >> RT305X_ESW_SPD_S) & 3;
1059 + link->speed = SWITCH_PORT_SPEED_10;
1062 + link->speed = SWITCH_PORT_SPEED_100;
1065 + case 3: /* forced gige speed can be 2 or 3 */
1066 + link->speed = SWITCH_PORT_SPEED_1000;
1069 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
1076 +static int esw_get_port_bool(struct switch_dev *dev,
1077 + const struct switch_attr *attr,
1078 + struct switch_val *val)
1080 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1081 + int idx = val->port_vlan;
1082 + u32 x, reg, shift;
1084 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
1087 + switch (attr->id) {
1088 + case RT305X_ESW_ATTR_PORT_DISABLE:
1089 + reg = RT305X_ESW_REG_POC0;
1090 + shift = RT305X_ESW_POC0_DIS_PORT_S;
1092 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1093 + reg = RT305X_ESW_REG_SGC2;
1094 + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
1096 + case RT305X_ESW_ATTR_PORT_UNTAG:
1097 + reg = RT305X_ESW_REG_POC2;
1098 + shift = RT305X_ESW_POC2_UNTAG_EN_S;
1100 + case RT305X_ESW_ATTR_PORT_LAN:
1101 + reg = RT305X_ESW_REG_SGC2;
1102 + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
1103 + if (idx >= RT305X_ESW_NUM_LANWAN)
1110 + x = esw_r32(esw, reg);
1111 + val->value.i = (x >> (idx + shift)) & 1;
1116 +static int esw_set_port_bool(struct switch_dev *dev,
1117 + const struct switch_attr *attr,
1118 + struct switch_val *val)
1120 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1121 + int idx = val->port_vlan;
1123 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1124 + val->value.i < 0 || val->value.i > 1)
1127 + switch (attr->id) {
1128 + case RT305X_ESW_ATTR_PORT_DISABLE:
1129 + esw->ports[idx].disable = val->value.i;
1131 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1132 + esw->ports[idx].doubletag = val->value.i;
1134 + case RT305X_ESW_ATTR_PORT_UNTAG:
1135 + esw->ports[idx].untag = val->value.i;
1144 +static int esw_get_port_recv_badgood(struct switch_dev *dev,
1145 + const struct switch_attr *attr,
1146 + struct switch_val *val)
1148 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1149 + int idx = val->port_vlan;
1150 + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
1153 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1155 + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
1156 + val->value.i = (reg >> shift) & 0xffff;
1162 +esw_get_port_tr_badgood(struct switch_dev *dev,
1163 + const struct switch_attr *attr,
1164 + struct switch_val *val)
1166 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1168 + int idx = val->port_vlan;
1169 + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
1172 + if (!soc_is_rt5350())
1175 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1178 + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
1179 + val->value.i = (reg >> shift) & 0xffff;
1184 +static int esw_get_port_led(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);
1189 + int idx = val->port_vlan;
1191 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1192 + idx >= RT305X_ESW_NUM_LEDS)
1195 + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
1200 +static int esw_set_port_led(struct switch_dev *dev,
1201 + const struct switch_attr *attr,
1202 + struct switch_val *val)
1204 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1205 + int idx = val->port_vlan;
1207 + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
1210 + esw->ports[idx].led = val->value.i;
1215 +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1217 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1219 + if (port >= RT305X_ESW_NUM_PORTS)
1222 + *val = esw_get_pvid(esw, port);
1227 +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
1229 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1231 + if (port >= RT305X_ESW_NUM_PORTS)
1234 + esw->ports[port].pvid = val;
1239 +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1241 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1243 + int vlan_idx = -1;
1248 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
1252 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1253 + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
1254 + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
1260 + if (vlan_idx == -1)
1263 + vmsc = esw_get_vmsc(esw, vlan_idx);
1264 + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
1266 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
1267 + struct switch_port *p;
1268 + int port_mask = 1 << i;
1270 + if (!(vmsc & port_mask))
1273 + p = &val->value.ports[val->len++];
1275 + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
1278 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
1284 +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1286 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1288 + int vlan_idx = -1;
1291 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
1292 + val->len > RT305X_ESW_NUM_PORTS)
1295 + /* one of the already defined vlans? */
1296 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1297 + if (esw->vlans[i].vid == val->port_vlan &&
1298 + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
1304 + /* select a free slot */
1305 + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
1306 + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
1310 + /* bail if all slots are in use */
1311 + if (vlan_idx == -1)
1314 + ports = RT305X_ESW_PORTS_NONE;
1315 + for (i = 0; i < val->len; i++) {
1316 + struct switch_port *p = &val->value.ports[i];
1317 + int port_mask = 1 << p->id;
1318 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
1320 + if (p->id >= RT305X_ESW_NUM_PORTS)
1323 + ports |= port_mask;
1324 + esw->ports[p->id].untag = untagged;
1326 + esw->vlans[vlan_idx].ports = ports;
1327 + if (ports == RT305X_ESW_PORTS_NONE)
1328 + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
1330 + esw->vlans[vlan_idx].vid = val->port_vlan;
1335 +static const struct switch_attr esw_global[] = {
1337 + .type = SWITCH_TYPE_INT,
1338 + .name = "enable_vlan",
1339 + .description = "VLAN mode (1:enabled)",
1341 + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
1342 + .get = esw_get_vlan_enable,
1343 + .set = esw_set_vlan_enable,
1346 + .type = SWITCH_TYPE_INT,
1347 + .name = "alternate_vlan_disable",
1348 + .description = "Use en_vlan instead of doubletag to disable"
1351 + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
1352 + .get = esw_get_alt_vlan_disable,
1353 + .set = esw_set_alt_vlan_disable,
1356 + .type = SWITCH_TYPE_INT,
1357 + .name = "bc_storm_protect",
1358 + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1360 + .id = RT305X_ESW_ATTR_BC_STATUS,
1361 + .get = rt305x_esw_get_bc_status,
1362 + .set = rt305x_esw_set_bc_status,
1365 + .type = SWITCH_TYPE_INT,
1366 + .name = "led_frequency",
1367 + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1369 + .id = RT305X_ESW_ATTR_LED_FREQ,
1370 + .get = rt305x_esw_get_led_freq,
1371 + .set = rt305x_esw_set_led_freq,
1375 +static const struct switch_attr esw_port[] = {
1377 + .type = SWITCH_TYPE_INT,
1378 + .name = "disable",
1379 + .description = "Port state (1:disabled)",
1381 + .id = RT305X_ESW_ATTR_PORT_DISABLE,
1382 + .get = esw_get_port_bool,
1383 + .set = esw_set_port_bool,
1386 + .type = SWITCH_TYPE_INT,
1387 + .name = "doubletag",
1388 + .description = "Double tagging for incoming vlan packets "
1391 + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
1392 + .get = esw_get_port_bool,
1393 + .set = esw_set_port_bool,
1396 + .type = SWITCH_TYPE_INT,
1398 + .description = "Untag (1:strip outgoing vlan tag)",
1400 + .id = RT305X_ESW_ATTR_PORT_UNTAG,
1401 + .get = esw_get_port_bool,
1402 + .set = esw_set_port_bool,
1405 + .type = SWITCH_TYPE_INT,
1407 + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1408 + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1409 + " 8:100mact, 10:blink, 11:off, 12:on)",
1411 + .id = RT305X_ESW_ATTR_PORT_LED,
1412 + .get = esw_get_port_led,
1413 + .set = esw_set_port_led,
1416 + .type = SWITCH_TYPE_INT,
1418 + .description = "HW port group (0:wan, 1:lan)",
1420 + .id = RT305X_ESW_ATTR_PORT_LAN,
1421 + .get = esw_get_port_bool,
1424 + .type = SWITCH_TYPE_INT,
1425 + .name = "recv_bad",
1426 + .description = "Receive bad packet counter",
1427 + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
1428 + .get = esw_get_port_recv_badgood,
1431 + .type = SWITCH_TYPE_INT,
1432 + .name = "recv_good",
1433 + .description = "Receive good packet counter",
1434 + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
1435 + .get = esw_get_port_recv_badgood,
1438 + .type = SWITCH_TYPE_INT,
1441 + .description = "Transmit bad packet counter. rt5350 only",
1442 + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
1443 + .get = esw_get_port_tr_badgood,
1446 + .type = SWITCH_TYPE_INT,
1447 + .name = "tr_good",
1449 + .description = "Transmit good packet counter. rt5350 only",
1450 + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
1451 + .get = esw_get_port_tr_badgood,
1455 +static const struct switch_attr esw_vlan[] = {
1458 +static const struct switch_dev_ops esw_ops = {
1460 + .attr = esw_global,
1461 + .n_attr = ARRAY_SIZE(esw_global),
1465 + .n_attr = ARRAY_SIZE(esw_port),
1469 + .n_attr = ARRAY_SIZE(esw_vlan),
1471 + .get_vlan_ports = esw_get_vlan_ports,
1472 + .set_vlan_ports = esw_set_vlan_ports,
1473 + .get_port_pvid = esw_get_port_pvid,
1474 + .set_port_pvid = esw_set_port_pvid,
1475 + .get_port_link = esw_get_port_link,
1476 + .apply_config = esw_apply_config,
1477 + .reset_switch = esw_reset_switch,
1480 +static struct rt305x_esw_platform_data rt3050_esw_data = {
1481 + /* All ports are LAN ports. */
1482 + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
1483 + .reg_initval_fct2 = 0x00d6500c,
1485 + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1486 + * turbo mii off, rgmi 3.3v off
1488 + * port6: enabled, gige, full-duplex, rx/tx-flow-control
1490 + .reg_initval_fpa2 = 0x3f502b28,
1493 +static const struct of_device_id ralink_esw_match[] = {
1494 + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
1497 +MODULE_DEVICE_TABLE(of, ralink_esw_match);
1499 +static int esw_probe(struct platform_device *pdev)
1501 + struct device_node *np = pdev->dev.of_node;
1502 + const struct rt305x_esw_platform_data *pdata;
1503 + const __be32 *port_map, *reg_init;
1504 + struct rt305x_esw *esw;
1505 + struct switch_dev *swdev;
1506 + struct resource *res, *irq;
1509 + pdata = pdev->dev.platform_data;
1511 + const struct of_device_id *match;
1512 + match = of_match_device(ralink_esw_match, &pdev->dev);
1514 + pdata = (struct rt305x_esw_platform_data *) match->data;
1519 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1521 + dev_err(&pdev->dev, "no memory resource found\n");
1525 + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1527 + dev_err(&pdev->dev, "no irq resource found\n");
1531 + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
1533 + dev_err(&pdev->dev, "no memory for private data\n");
1537 + esw->dev = &pdev->dev;
1538 + esw->irq = irq->start;
1539 + esw->base = ioremap(res->start, resource_size(res));
1541 + dev_err(&pdev->dev, "ioremap failed\n");
1546 + port_map = of_get_property(np, "ralink,portmap", NULL);
1548 + esw->port_map = be32_to_cpu(*port_map);
1550 + reg_init = of_get_property(np, "ralink,fct2", NULL);
1552 + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
1554 + reg_init = of_get_property(np, "ralink,fpa2", NULL);
1556 + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
1558 + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
1560 + esw->reg_led_polarity = be32_to_cpu(*reg_init);
1562 + swdev = &esw->swdev;
1563 + swdev->of_node = pdev->dev.of_node;
1564 + swdev->name = "rt305x-esw";
1565 + swdev->alias = "rt305x";
1566 + swdev->cpu_port = RT305X_ESW_PORT6;
1567 + swdev->ports = RT305X_ESW_NUM_PORTS;
1568 + swdev->vlans = RT305X_ESW_NUM_VIDS;
1569 + swdev->ops = &esw_ops;
1571 + err = register_switch(swdev, NULL);
1573 + dev_err(&pdev->dev, "register_switch failed\n");
1577 + platform_set_drvdata(pdev, esw);
1579 + esw->pdata = pdata;
1580 + spin_lock_init(&esw->reg_rw_lock);
1584 + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
1585 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
1586 + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
1591 + iounmap(esw->base);
1597 +static int esw_remove(struct platform_device *pdev)
1599 + struct rt305x_esw *esw;
1601 + esw = platform_get_drvdata(pdev);
1603 + unregister_switch(&esw->swdev);
1604 + platform_set_drvdata(pdev, NULL);
1605 + iounmap(esw->base);
1612 +static struct platform_driver esw_driver = {
1613 + .probe = esw_probe,
1614 + .remove = esw_remove,
1616 + .name = "rt305x-esw",
1617 + .owner = THIS_MODULE,
1618 + .of_match_table = ralink_esw_match,
1622 +int __init rtesw_init(void)
1624 + return platform_driver_register(&esw_driver);
1627 +void rtesw_exit(void)
1629 + platform_driver_unregister(&esw_driver);
1632 +++ b/drivers/net/ethernet/ralink/esw_rt3052.h
1635 + * This program is free software; you can redistribute it and/or modify
1636 + * it under the terms of the GNU General Public License as published by
1637 + * the Free Software Foundation; version 2 of the License
1639 + * This program is distributed in the hope that it will be useful,
1640 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1641 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1642 + * GNU General Public License for more details.
1644 + * You should have received a copy of the GNU General Public License
1645 + * along with this program; if not, write to the Free Software
1646 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1648 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1651 +#ifndef _RALINK_ESW_RT3052_H__
1652 +#define _RALINK_ESW_RT3052_H__
1654 +#ifdef CONFIG_NET_RALINK_ESW_RT3052
1656 +int __init rtesw_init(void);
1657 +void rtesw_exit(void);
1661 +static inline int __init rtesw_init(void) { return 0; }
1662 +static inline void rtesw_exit(void) { }
1667 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1670 + * This program is free software; you can redistribute it and/or modify
1671 + * it under the terms of the GNU General Public License as published by
1672 + * the Free Software Foundation; version 2 of the License
1674 + * This program is distributed in the hope that it will be useful,
1675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1677 + * GNU General Public License for more details.
1679 + * You should have received a copy of the GNU General Public License
1680 + * along with this program; if not, write to the Free Software
1681 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1683 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1686 +#include <linux/module.h>
1687 +#include <linux/kernel.h>
1688 +#include <linux/types.h>
1689 +#include <linux/dma-mapping.h>
1690 +#include <linux/init.h>
1691 +#include <linux/skbuff.h>
1692 +#include <linux/etherdevice.h>
1693 +#include <linux/ethtool.h>
1694 +#include <linux/platform_device.h>
1695 +#include <linux/of_device.h>
1696 +#include <linux/clk.h>
1697 +#include <linux/of_net.h>
1698 +#include <linux/of_mdio.h>
1699 +#include <linux/of_irq.h>
1700 +#include <linux/of_address.h>
1701 +#include <linux/switch.h>
1703 +#include <asm/mach-ralink/ralink_regs.h>
1705 +#include "ralink_soc_eth.h"
1707 +#include <linux/ioport.h>
1708 +#include <linux/switch.h>
1709 +#include <linux/mii.h>
1711 +#include <ralink_regs.h>
1712 +#include <asm/mach-ralink/mt7620.h>
1714 +#include "ralink_soc_eth.h"
1715 +#include "gsw_mt7620a.h"
1718 +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
1720 +#define MT7620A_GSW_REG_PIAC 0x7004
1722 +#define GSW_NUM_VLANS 16
1723 +#define GSW_NUM_VIDS 4096
1724 +#define GSW_NUM_PORTS 7
1725 +#define GSW_PORT6 6
1727 +#define GSW_MDIO_ACCESS BIT(31)
1728 +#define GSW_MDIO_READ BIT(19)
1729 +#define GSW_MDIO_WRITE BIT(18)
1730 +#define GSW_MDIO_START BIT(16)
1731 +#define GSW_MDIO_ADDR_SHIFT 20
1732 +#define GSW_MDIO_REG_SHIFT 25
1734 +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
1735 +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
1736 +#define GSW_REG_SMACCR0 0x3fE4
1737 +#define GSW_REG_SMACCR1 0x3fE8
1738 +#define GSW_REG_CKGCR 0x3ff0
1740 +#define GSW_REG_IMR 0x7008
1741 +#define GSW_REG_ISR 0x700c
1743 +#define SYSC_REG_CFG1 0x14
1745 +#define PORT_IRQ_ST_CHG 0x7f
1747 +#define GSW_VLAN_VTCR 0x90
1748 +#define GSW_VLAN_VTCR_VID_M 0xfff
1749 +#define GSW_VLAN_ID(_x) (0x100 + (4 * (_x)))
1750 +#define GSW_VLAN_ID_VID_S 12
1751 +#define GSW_VLAN_ID_VID_M 0xfff
1753 +#define GSW_VAWD1 0x94
1754 +#define GSW_VAWD1_VTAG_EN BIT(28)
1755 +#define GSW_VAWD1_PORTM_S 16
1756 +#define GSW_VAWD1_PORTM_M 0xff
1758 +#define GSW_VAWD2 0x98
1759 +#define GSW_VAWD2_PORTT_S 16
1760 +#define GSW_VAWD2_PORTT_M 0xff
1762 +#define GSW_VTIM(_x) (0x100 + (4 * (_x)))
1763 +#define GSW_VTIM_M 0xfff
1764 +#define GSW_VTIM_S 12
1766 +#define GSW_REG_PCR(x) (0x2004 + (x * 0x100))
1767 +#define GSW_REG_PCR_EG_TAG_S 28
1768 +#define GSW_REG_PCR_EG_TAG_M 0x3
1770 +#define SYSCFG1 0x14
1772 +#define ESW_PHY_POLLING 0x7000
1774 +#define PMCR_IPG BIT(18)
1775 +#define PMCR_MAC_MODE BIT(16)
1776 +#define PMCR_FORCE BIT(15)
1777 +#define PMCR_TX_EN BIT(14)
1778 +#define PMCR_RX_EN BIT(13)
1779 +#define PMCR_BACKOFF BIT(9)
1780 +#define PMCR_BACKPRES BIT(8)
1781 +#define PMCR_RX_FC BIT(5)
1782 +#define PMCR_TX_FC BIT(4)
1783 +#define PMCR_SPEED(_x) (_x << 2)
1784 +#define PMCR_DUPLEX BIT(1)
1785 +#define PMCR_LINK BIT(0)
1787 +#define PHY_AN_EN BIT(31)
1788 +#define PHY_PRE_EN BIT(30)
1789 +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
1792 + /* Global attributes. */
1793 + GSW_ATTR_ENABLE_VLAN,
1794 + /* Port attributes. */
1795 + GSW_ATTR_PORT_UNTAG,
1814 +struct mt7620_gsw {
1815 + struct device *dev;
1816 + void __iomem *base;
1819 + struct switch_dev swdev;
1820 + bool global_vlan_enable;
1821 + struct gsw_vlan vlans[GSW_NUM_VLANS];
1822 + struct gsw_port ports[GSW_NUM_PORTS];
1823 + long unsigned int autopoll;
1827 +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
1829 + iowrite32(val, gsw->base + reg);
1832 +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
1834 + return ioread32(gsw->base + reg);
1837 +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
1839 + unsigned long t_start = jiffies;
1842 + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
1844 + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
1849 + printk(KERN_ERR "mdio: MDIO timeout\n");
1853 +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
1856 + if (mt7620_mii_busy_wait(gsw))
1859 + write_data &= 0xffff;
1861 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
1862 + (phy_register << GSW_MDIO_REG_SHIFT) |
1863 + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
1864 + MT7620A_GSW_REG_PIAC);
1866 + if (mt7620_mii_busy_wait(gsw))
1872 +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
1874 + struct fe_priv *priv = bus->priv;
1875 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1877 + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
1880 +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
1882 + struct fe_priv *priv = bus->priv;
1883 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1886 + if (mt7620_mii_busy_wait(gsw))
1889 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
1890 + (phy_reg << GSW_MDIO_REG_SHIFT) |
1891 + (phy_addr << GSW_MDIO_ADDR_SHIFT),
1892 + MT7620A_GSW_REG_PIAC);
1894 + if (mt7620_mii_busy_wait(gsw))
1897 + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
1902 +static unsigned char *fe_speed_str(int speed)
1919 +int mt7620a_has_carrier(struct fe_priv *priv)
1921 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1924 + for (i = 0; i < GSW_PORT6; i++)
1925 + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
1930 +static void mt7620a_handle_carrier(struct fe_priv *priv)
1935 + if (mt7620a_has_carrier(priv))
1936 + netif_carrier_on(priv->netdev);
1938 + netif_carrier_off(priv->netdev);
1941 +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
1943 + if (priv->link[port])
1944 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1945 + port, fe_speed_str(priv->phy->speed[port]),
1946 + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
1948 + netdev_info(priv->netdev, "port %d link down\n", port);
1949 + mt7620a_handle_carrier(priv);
1952 +static irqreturn_t gsw_interrupt(int irq, void *_priv)
1954 + struct fe_priv *priv = (struct fe_priv *) _priv;
1955 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1957 + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
1959 + status = gsw_r32(gsw, GSW_REG_ISR);
1960 + if (status & PORT_IRQ_ST_CHG)
1961 + for (i = 0; i <= max; i++) {
1962 + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
1963 + int link = status & 0x1;
1965 + if (link != priv->link[i]) {
1967 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1968 + i, fe_speed_str((status >> 2) & 3),
1969 + (status & 0x2) ? "Full" : "Half");
1971 + netdev_info(priv->netdev, "port %d link down\n", i);
1974 + priv->link[i] = link;
1976 + mt7620a_handle_carrier(priv);
1978 + gsw_w32(gsw, status, GSW_REG_ISR);
1980 + return IRQ_HANDLED;
1983 +static int mt7620_is_bga(void)
1985 + u32 bga = rt_sysc_r32(0x0c);
1987 + return (bga >> 16) & 1;
1990 +static void gsw_auto_poll(struct mt7620_gsw *gsw)
1993 + int lsb = -1, msb = 0;
1995 + for_each_set_bit(phy, &gsw->autopoll, 32) {
2001 + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
2004 +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
2006 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2007 + const __be32 *_id = of_get_property(np, "reg", NULL);
2008 + int phy_mode, size, id;
2010 + u32 val, mask = 0;
2011 + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
2013 + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
2015 + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
2017 + pr_err("%s: invalid port id\n", np->name);
2021 + id = be32_to_cpu(*_id);
2026 + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
2027 + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
2028 + pr_err("%s: invalid fixed link property\n", np->name);
2029 + priv->phy->phy_fixed[id] = NULL;
2033 + phy_mode = of_get_phy_mode(np);
2034 + switch (phy_mode) {
2035 + case PHY_INTERFACE_MODE_RGMII:
2038 + case PHY_INTERFACE_MODE_MII:
2041 + case PHY_INTERFACE_MODE_RMII:
2045 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[id]);
2049 + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
2050 + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
2053 + val = rt_sysc_r32(SYSCFG1);
2054 + val &= ~(3 << shift);
2055 + val |= mask << shift;
2056 + rt_sysc_w32(val, SYSCFG1);
2058 + if (priv->phy->phy_fixed[id]) {
2059 + const __be32 *link = priv->phy->phy_fixed[id];
2060 + int tx_fc = be32_to_cpup(link++);
2061 + int rx_fc = be32_to_cpup(link++);
2064 + priv->phy->speed[id] = be32_to_cpup(link++);
2065 + priv->phy->duplex[id] = be32_to_cpup(link++);
2066 + priv->link[id] = 1;
2068 + switch (priv->phy->speed[id]) {
2079 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
2080 + priv->phy->phy_fixed[id] = 0;
2083 + val = PMCR_SPEED(val);
2084 + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2085 + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
2087 + val |= PMCR_TX_FC;
2089 + val |= PMCR_RX_FC;
2090 + if (priv->phy->duplex[id])
2091 + val |= PMCR_DUPLEX;
2092 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2093 + dev_info(priv->device, "using fixed link parameters\n");
2097 + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
2098 + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2099 + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
2101 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2102 + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
2103 + gsw->autopoll |= BIT(id);
2104 + gsw_auto_poll(gsw);
2109 +static void gsw_hw_init(struct mt7620_gsw *gsw)
2111 + u32 is_BGA = mt7620_is_bga();
2113 + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
2114 + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
2116 + /*correct PHY setting L3.0 BGA*/
2117 + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
2119 + _mt7620_mii_write(gsw, 1, 17, 0x7444);
2121 + _mt7620_mii_write(gsw, 1, 19, 0x0114);
2123 + _mt7620_mii_write(gsw, 1, 19, 0x0117);
2125 + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
2126 + _mt7620_mii_write(gsw, 1, 25, 0x6212);
2127 + _mt7620_mii_write(gsw, 1, 26, 0x0777);
2128 + _mt7620_mii_write(gsw, 1, 29, 0x4000);
2129 + _mt7620_mii_write(gsw, 1, 28, 0xc077);
2130 + _mt7620_mii_write(gsw, 1, 24, 0x0000);
2132 + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
2133 + _mt7620_mii_write(gsw, 1, 17, 0x4838);
2135 + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
2137 + _mt7620_mii_write(gsw, 1, 21, 0x0515);
2138 + _mt7620_mii_write(gsw, 1, 22, 0x0053);
2139 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2140 + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
2141 + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
2142 + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
2144 + _mt7620_mii_write(gsw, 1, 21, 0x0517);
2145 + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
2146 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2147 + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
2148 + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
2149 + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
2151 + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
2152 + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
2154 + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
2155 + _mt7620_mii_write(gsw, 0, 30, 0xa000);
2156 + _mt7620_mii_write(gsw, 1, 30, 0xa000);
2157 + _mt7620_mii_write(gsw, 2, 30, 0xa000);
2158 + _mt7620_mii_write(gsw, 3, 30, 0xa000);
2160 + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
2161 + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
2162 + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
2163 + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
2164 + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
2165 + _mt7620_mii_write(gsw, 0, 16, 0x1111);
2166 + _mt7620_mii_write(gsw, 1, 16, 0x1010);
2167 + _mt7620_mii_write(gsw, 2, 16, 0x1515);
2168 + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
2170 + /* CPU Port6 Force Link 1G, FC ON */
2171 + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
2172 + /* Set Port6 CPU Port */
2173 + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
2177 + /* setup port 4 */
2178 + if (gsw->port4 == PORT4_EPHY) {
2179 + u32 val = rt_sysc_r32(SYSCFG1);
2181 + rt_sysc_w32(val, SYSCFG1);
2182 + _mt7620_mii_write(gsw, 4, 30, 0xa000);
2183 + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
2184 + _mt7620_mii_write(gsw, 4, 16, 0x1313);
2185 + pr_info("gsw: setting port4 to ephy mode\n");
2189 +static int gsw_reset_switch(struct switch_dev *dev)
2191 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2193 + gsw->global_vlan_enable = 0;
2194 + memset(gsw->ports, 0, sizeof(gsw->ports));
2195 + memset(gsw->vlans, 0, sizeof(gsw->vlans));
2201 +static int gsw_get_vlan_enable(struct switch_dev *dev,
2202 + const struct switch_attr *attr,
2203 + struct switch_val *val)
2205 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2207 + val->value.i = gsw->global_vlan_enable;
2212 +static int gsw_set_vlan_enable(struct switch_dev *dev,
2213 + const struct switch_attr *attr,
2214 + struct switch_val *val)
2216 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2218 + gsw->global_vlan_enable = val->value.i != 0;
2223 +static unsigned gsw_get_pvid(struct mt7620_gsw *gsw, unsigned port)
2227 + s = GSW_VTIM_S * (port % 2);
2228 + val = gsw_r32(gsw, GSW_VTIM(port / 2));
2230 + return (val >> s) & GSW_VTIM_M;
2233 +static void gsw_set_pvid(struct mt7620_gsw *gsw, unsigned port, unsigned pvid)
2237 + s = GSW_VTIM_S * (port % 2);
2238 + val = gsw_r32(gsw, GSW_VTIM(port / 2));
2239 + val &= ~(GSW_VTIM_M << s);
2240 + val |= (pvid && GSW_VTIM_M) << s;
2241 + gsw_w32(gsw, val, GSW_VTIM(port / 2));
2244 +static int gsw_get_port_bool(struct switch_dev *dev,
2245 + const struct switch_attr *attr,
2246 + struct switch_val *val)
2248 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2249 + int idx = val->port_vlan;
2251 + if (idx < 0 || idx >= GSW_NUM_PORTS)
2254 + switch (attr->id) {
2255 + case GSW_ATTR_PORT_UNTAG:
2256 + return gsw->ports[idx].untag;
2262 +static int gsw_get_port_pvid(struct switch_dev *dev, int port, int *val)
2264 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2266 + if (port >= GSW_NUM_PORTS)
2269 + *val = gsw_get_pvid(gsw, port);
2274 +static int gsw_set_port_pvid(struct switch_dev *dev, int port, int val)
2276 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2278 + if (port >= GSW_NUM_PORTS)
2281 + gsw->ports[port].pvid = val;
2286 +static void gsw_set_vtcr(struct switch_dev *dev, u32 vid)
2288 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2291 + gsw_w32(gsw, 0x80000000 | (BIT(vid) & GSW_VLAN_VTCR_VID_M), GSW_VLAN_VTCR);
2292 + while (retry-- && (gsw_r32(gsw, GSW_VLAN_VTCR) & 0x80000000))
2296 +static void gsw_apply_vtcr(struct switch_dev *dev, u32 vid)
2298 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2301 + gsw_w32(gsw, 0x80001000 | (BIT(vid) & GSW_VLAN_VTCR_VID_M), GSW_VLAN_VTCR);
2302 + while (retry-- && (gsw_r32(gsw, GSW_VLAN_VTCR) & 0x80000000))
2306 +static unsigned gsw_get_vlan_id(struct mt7620_gsw *gsw, unsigned vlan)
2311 + s = GSW_VLAN_ID_VID_S * (vlan % 2);
2312 + val = gsw_r32(gsw, GSW_VLAN_ID(vlan / 2));
2313 + val = (val >> s) & GSW_VLAN_ID_VID_M;
2318 +static void gsw_set_vlan_id(struct mt7620_gsw *gsw, unsigned vlan, unsigned vid)
2323 + s = GSW_VLAN_ID_VID_S * (vlan % 2);
2324 + val = gsw_r32(gsw, GSW_VLAN_ID(vlan / 2));
2325 + val &= ~(GSW_VLAN_ID_VID_M << s);
2326 + val |= (vid << s);
2327 + gsw_w32(gsw, val, GSW_VLAN_ID(vlan / 2));
2330 +static void gsw_vlan_tagging_enable(struct mt7620_gsw *gsw, unsigned vlan, unsigned enable)
2334 + val = gsw_r32(gsw, GSW_VAWD1);
2336 + val |= GSW_VAWD1_VTAG_EN;
2338 + val &= ~GSW_VAWD1_VTAG_EN;
2339 + gsw_w32(gsw, val, GSW_VAWD1);
2342 +static unsigned gsw_get_port_member(struct mt7620_gsw *gsw, unsigned vlan)
2346 + gsw_set_vtcr(&gsw->swdev, vlan);
2348 + val = gsw_r32(gsw, GSW_VAWD1);
2349 + val = (val >> GSW_VAWD1_PORTM_S) & GSW_VAWD1_PORTM_M;
2354 +static void gsw_set_port_member(struct mt7620_gsw *gsw, unsigned vlan, unsigned member)
2358 + val = gsw_r32(gsw, GSW_VAWD1);
2359 + val = ~(GSW_VAWD1_PORTM_M << GSW_VAWD1_PORTM_S);
2360 + val |= (member & GSW_VAWD1_PORTM_M) << GSW_VAWD1_PORTM_S;
2361 + gsw_w32(gsw, val, GSW_VAWD1);
2364 +static unsigned gsw_get_port_tag(struct mt7620_gsw *gsw, unsigned port)
2368 + val = gsw_r32(gsw, GSW_REG_PCR(port));
2369 + val >>= GSW_REG_PCR_EG_TAG_S;
2370 + val &= GSW_REG_PCR_EG_TAG_M;
2375 +static void gsw_set_port_untag(struct mt7620_gsw *gsw, unsigned port, unsigned untag)
2379 + val = gsw_r32(gsw, GSW_REG_PCR(port));
2384 + val &= ~(GSW_REG_PCR_EG_TAG_M << GSW_REG_PCR_EG_TAG_S);
2385 + val |= (untag & GSW_REG_PCR_EG_TAG_M) << GSW_REG_PCR_EG_TAG_S;
2386 + gsw_w32(gsw, val, GSW_REG_PCR(port));
2389 +static int gsw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
2391 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2392 + int vlan_idx = -1;
2398 + if (val->port_vlan < 0 || val->port_vlan >= GSW_NUM_VIDS)
2402 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2403 + if (gsw_get_vlan_id(gsw, i) != val->port_vlan)
2405 + member = gsw_get_port_member(gsw, i);
2410 + if (vlan_idx == -1)
2413 + for (i = 0; i < GSW_NUM_PORTS; i++) {
2414 + struct switch_port *p;
2415 + int port_mask = 1 << i;
2417 + if (!(member & port_mask))
2420 + p = &val->value.ports[val->len++];
2422 + if (gsw_get_port_tag(gsw, i))
2423 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
2431 +static int gsw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
2433 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2435 + int vlan_idx = -1;
2438 + if (val->port_vlan < 0 || val->port_vlan >= GSW_NUM_VIDS ||
2439 + val->len > GSW_NUM_PORTS)
2442 + /* one of the already defined vlans? */
2443 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2444 + if (gsw->vlans[i].vid == val->port_vlan &&
2445 + gsw->vlans[i].ports) {
2451 + /* select a free slot */
2452 + for (i = 0; vlan_idx == -1 && i < GSW_NUM_VLANS; i++) {
2453 + if (!gsw->vlans[i].ports)
2457 + /* bail if all slots are in use */
2458 + if (vlan_idx == -1)
2462 + for (i = 0; i < val->len; i++) {
2463 + struct switch_port *p = &val->value.ports[i];
2464 + int port_mask = 1 << p->id;
2465 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
2467 + if (p->id >= GSW_NUM_PORTS)
2470 + ports |= port_mask;
2471 + gsw->ports[p->id].untag = untagged;
2473 + gsw->vlans[vlan_idx].ports = ports;
2475 + gsw->vlans[vlan_idx].vid = 0xfff;
2477 + gsw->vlans[vlan_idx].vid = val->port_vlan;
2482 +static int gsw_apply_config(struct switch_dev *dev)
2484 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2487 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2488 + gsw_set_vtcr(&gsw->swdev, i);
2489 + if (gsw->global_vlan_enable) {
2490 + gsw_set_vlan_id(gsw, i, gsw->vlans[i].vid);
2491 + gsw_set_port_member(gsw, i, gsw->vlans[i].ports);
2492 + gsw_vlan_tagging_enable(gsw, i, 1);
2494 + gsw_set_vlan_id(gsw, i, 0xfff);
2495 + gsw_set_port_member(gsw, i, 0);
2496 + gsw_vlan_tagging_enable(gsw, i, 0);
2498 + gsw_apply_vtcr(&gsw->swdev, i);
2501 + for (i = 0; i < GSW_NUM_PORTS; i++) {
2502 + if (gsw->global_vlan_enable) {
2503 + gsw_set_port_untag(gsw, i, !gsw->ports[i].untag);
2504 + gsw_set_pvid(gsw, i, gsw->ports[i].pvid);
2506 + gsw_set_port_untag(gsw, i, 0);
2507 + gsw_set_pvid(gsw, i, 0);
2511 + if (!gsw->global_vlan_enable)
2512 + gsw_set_vlan_id(gsw, 0, 0);
2517 +static int gsw_get_port_link(struct switch_dev *dev,
2519 + struct switch_port_link *link)
2521 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2524 + if (port < 0 || port >= GSW_NUM_PORTS)
2527 + status = gsw_r32(gsw, GSW_REG_PORT_STATUS(port));
2528 + link->link = status & 0x1;
2529 + link->duplex = (status >> 1) & 1;
2531 + switch ((status >> 2) & 0x3) {
2533 + link->speed = SWITCH_PORT_SPEED_10;
2536 + link->speed = SWITCH_PORT_SPEED_100;
2539 + case 3: // forced gige speed can be 2 or 3
2540 + link->speed = SWITCH_PORT_SPEED_1000;
2547 +static int gsw_set_port_bool(struct switch_dev *dev,
2548 + const struct switch_attr *attr,
2549 + struct switch_val *val)
2551 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2552 + int idx = val->port_vlan;
2554 + if (idx < 0 || idx >= GSW_NUM_PORTS ||
2555 + val->value.i < 0 || val->value.i > 1)
2558 + switch (attr->id) {
2559 + case GSW_ATTR_PORT_UNTAG:
2560 + gsw->ports[idx].untag = val->value.i;
2569 +static const struct switch_attr gsw_global[] = {
2571 + .type = SWITCH_TYPE_INT,
2572 + .name = "enable_vlan",
2573 + .description = "VLAN mode (1:enabled)",
2575 + .id = GSW_ATTR_ENABLE_VLAN,
2576 + .get = gsw_get_vlan_enable,
2577 + .set = gsw_set_vlan_enable,
2581 +static const struct switch_attr gsw_port[] = {
2583 + .type = SWITCH_TYPE_INT,
2585 + .description = "Untag (1:strip outgoing vlan tag)",
2587 + .id = GSW_ATTR_PORT_UNTAG,
2588 + .get = gsw_get_port_bool,
2589 + .set = gsw_set_port_bool,
2593 +static const struct switch_attr gsw_vlan[] = {
2596 +static const struct switch_dev_ops gsw_ops = {
2598 + .attr = gsw_global,
2599 + .n_attr = ARRAY_SIZE(gsw_global),
2603 + .n_attr = ARRAY_SIZE(gsw_port),
2607 + .n_attr = ARRAY_SIZE(gsw_vlan),
2609 + .get_vlan_ports = gsw_get_vlan_ports,
2610 + .set_vlan_ports = gsw_set_vlan_ports,
2611 + .get_port_pvid = gsw_get_port_pvid,
2612 + .set_port_pvid = gsw_set_port_pvid,
2613 + .get_port_link = gsw_get_port_link,
2614 + .apply_config = gsw_apply_config,
2615 + .reset_switch = gsw_reset_switch,
2618 +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
2620 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2621 + unsigned long flags;
2623 + spin_lock_irqsave(&priv->page_lock, flags);
2624 + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
2625 + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
2627 + spin_unlock_irqrestore(&priv->page_lock, flags);
2630 +static struct of_device_id gsw_match[] = {
2631 + { .compatible = "ralink,mt7620a-gsw" },
2635 +int mt7620_gsw_probe(struct fe_priv *priv)
2637 + struct mt7620_gsw *gsw;
2638 + struct device_node *np;
2639 + struct switch_dev *swdev;
2640 + const char *port4 = NULL;
2642 + np = of_find_matching_node(NULL, gsw_match);
2644 + dev_err(priv->device, "no gsw node found\n");
2647 + np = of_node_get(np);
2649 + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
2651 + dev_err(priv->device, "no gsw memory for private data\n");
2655 + gsw->irq = irq_of_parse_and_map(np, 0);
2657 + dev_err(priv->device, "no gsw irq resource found\n");
2661 + gsw->base = of_iomap(np, 0);
2663 + dev_err(priv->device, "gsw ioremap failed\n");
2666 + gsw->dev = priv->device;
2667 + priv->soc->swpriv = gsw;
2669 + swdev = &gsw->swdev;
2670 + swdev->of_node = np;
2671 + swdev->name = "mt7620a-gsw";
2672 + swdev->alias = "mt7620x";
2673 + swdev->cpu_port = GSW_PORT6;
2674 + swdev->ports = GSW_NUM_PORTS;
2675 + swdev->vlans = GSW_NUM_VLANS;
2676 + swdev->ops = &gsw_ops;
2678 + if (register_switch(swdev, NULL))
2679 + dev_err(priv->device, "register_switch failed\n");
2681 + of_property_read_string(np, "ralink,port4", &port4);
2682 + if (port4 && !strcmp(port4, "ephy"))
2683 + gsw->port4 = PORT4_EPHY;
2684 + else if (port4 && !strcmp(port4, "gmac"))
2685 + gsw->port4 = PORT4_EXT;
2691 + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
2692 + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
2697 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2700 + * This program is free software; you can redistribute it and/or modify
2701 + * it under the terms of the GNU General Public License as published by
2702 + * the Free Software Foundation; version 2 of the License
2704 + * This program is distributed in the hope that it will be useful,
2705 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2706 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2707 + * GNU General Public License for more details.
2709 + * You should have received a copy of the GNU General Public License
2710 + * along with this program; if not, write to the Free Software
2711 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2713 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2716 +#ifndef _RALINK_GSW_MT7620_H__
2717 +#define _RALINK_GSW_MT7620_H__
2719 +extern int mt7620_gsw_probe(struct fe_priv *priv);
2720 +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
2721 +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2722 +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2723 +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
2724 +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
2725 +extern int mt7620a_has_carrier(struct fe_priv *priv);
2729 +++ b/drivers/net/ethernet/ralink/mdio.c
2732 + * This program is free software; you can redistribute it and/or modify
2733 + * it under the terms of the GNU General Public License as published by
2734 + * the Free Software Foundation; version 2 of the License
2736 + * This program is distributed in the hope that it will be useful,
2737 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2738 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2739 + * GNU General Public License for more details.
2741 + * You should have received a copy of the GNU General Public License
2742 + * along with this program; if not, write to the Free Software
2743 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2745 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2748 +#include <linux/module.h>
2749 +#include <linux/kernel.h>
2750 +#include <linux/types.h>
2751 +#include <linux/dma-mapping.h>
2752 +#include <linux/init.h>
2753 +#include <linux/skbuff.h>
2754 +#include <linux/etherdevice.h>
2755 +#include <linux/ethtool.h>
2756 +#include <linux/platform_device.h>
2757 +#include <linux/phy.h>
2758 +#include <linux/of_device.h>
2759 +#include <linux/clk.h>
2760 +#include <linux/of_net.h>
2761 +#include <linux/of_mdio.h>
2763 +#include "ralink_soc_eth.h"
2766 +static int fe_mdio_reset(struct mii_bus *bus)
2772 +static void fe_phy_link_adjust(struct net_device *dev)
2774 + struct fe_priv *priv = netdev_priv(dev);
2775 + unsigned long flags;
2778 + spin_lock_irqsave(&priv->phy->lock, flags);
2779 + for (i = 0; i < 8; i++) {
2780 + if (priv->phy->phy_node[i]) {
2781 + struct phy_device *phydev = priv->phy->phy[i];
2782 + int status_change = 0;
2785 + if (priv->phy->duplex[i] != phydev->duplex ||
2786 + priv->phy->speed[i] != phydev->speed)
2787 + status_change = 1;
2789 + if (phydev->link != priv->link[i])
2790 + status_change = 1;
2792 + switch (phydev->speed) {
2796 + priv->link[i] = phydev->link;
2797 + priv->phy->duplex[i] = phydev->duplex;
2798 + priv->phy->speed[i] = phydev->speed;
2800 + if (status_change && priv->soc->mdio_adjust_link)
2801 + priv->soc->mdio_adjust_link(priv, i);
2806 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2809 +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
2811 + const __be32 *_port = NULL;
2812 + struct phy_device *phydev;
2813 + int phy_mode, port;
2815 + _port = of_get_property(phy_node, "reg", NULL);
2817 + if (!_port || (be32_to_cpu(*_port) >= 8)) {
2818 + pr_err("%s: invalid port id\n", phy_node->name);
2821 + port = be32_to_cpu(*_port);
2822 + phy_mode = of_get_phy_mode(phy_node);
2823 + if (phy_mode < 0) {
2824 + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
2825 + priv->phy->phy_node[port] = NULL;
2829 + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
2831 + if (IS_ERR(phydev)) {
2832 + dev_err(priv->device, "could not connect to PHY\n");
2833 + priv->phy->phy_node[port] = NULL;
2834 + return PTR_ERR(phydev);
2837 + phydev->supported &= PHY_GBIT_FEATURES;
2838 + phydev->advertising = phydev->supported;
2839 + phydev->no_auto_carrier_off = 1;
2841 + dev_info(priv->device,
2842 + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
2843 + port, dev_name(&phydev->dev), phydev->phy_id,
2844 + phydev->drv->name);
2846 + priv->phy->phy[port] = phydev;
2847 + priv->link[port] = 0;
2852 +static int fe_phy_connect(struct fe_priv *priv)
2857 +static void fe_phy_disconnect(struct fe_priv *priv)
2859 + unsigned long flags;
2862 + for (i = 0; i < 8; i++)
2863 + if (priv->phy->phy_fixed[i]) {
2864 + spin_lock_irqsave(&priv->phy->lock, flags);
2865 + priv->link[i] = 0;
2866 + if (priv->soc->mdio_adjust_link)
2867 + priv->soc->mdio_adjust_link(priv, i);
2868 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2869 + } else if (priv->phy->phy[i]) {
2870 + phy_disconnect(priv->phy->phy[i]);
2874 +static void fe_phy_start(struct fe_priv *priv)
2876 + unsigned long flags;
2879 + for (i = 0; i < 8; i++) {
2880 + if (priv->phy->phy_fixed[i]) {
2881 + spin_lock_irqsave(&priv->phy->lock, flags);
2882 + priv->link[i] = 1;
2883 + if (priv->soc->mdio_adjust_link)
2884 + priv->soc->mdio_adjust_link(priv, i);
2885 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2886 + } else if (priv->phy->phy[i]) {
2887 + phy_start(priv->phy->phy[i]);
2892 +static void fe_phy_stop(struct fe_priv *priv)
2894 + unsigned long flags;
2897 + for (i = 0; i < 8; i++)
2898 + if (priv->phy->phy_fixed[i]) {
2899 + spin_lock_irqsave(&priv->phy->lock, flags);
2900 + priv->link[i] = 0;
2901 + if (priv->soc->mdio_adjust_link)
2902 + priv->soc->mdio_adjust_link(priv, i);
2903 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2904 + } else if (priv->phy->phy[i]) {
2905 + phy_stop(priv->phy->phy[i]);
2909 +static struct fe_phy phy_ralink = {
2910 + .connect = fe_phy_connect,
2911 + .disconnect = fe_phy_disconnect,
2912 + .start = fe_phy_start,
2913 + .stop = fe_phy_stop,
2916 +int fe_mdio_init(struct fe_priv *priv)
2918 + struct device_node *mii_np;
2921 + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
2924 + spin_lock_init(&phy_ralink.lock);
2925 + priv->phy = &phy_ralink;
2927 + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
2929 + dev_err(priv->device, "no %s child node found", "mdio-bus");
2933 + if (!of_device_is_available(mii_np)) {
2935 + goto err_put_node;
2938 + priv->mii_bus = mdiobus_alloc();
2939 + if (priv->mii_bus == NULL) {
2941 + goto err_put_node;
2944 + priv->mii_bus->name = "mdio";
2945 + priv->mii_bus->read = priv->soc->mdio_read;
2946 + priv->mii_bus->write = priv->soc->mdio_write;
2947 + priv->mii_bus->reset = fe_mdio_reset;