ramips: set default vid of each vlan, so it won't need be set explicitly.
[openwrt/openwrt.git] / target / linux / ramips / patches-3.10 / 0118-NET-MIPS-add-ralink-SoC-ethernet-driver.patch
1 From b7d9374aee4b47a76dadaf1fe7f6838087c9c62d 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 118/133] NET: MIPS: add ralink SoC ethernet driver
5
6 Add support for Ralink FE and ESW.
7
8 Signed-off-by: John Crispin <blogic@openwrt.org>
9 ---
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 | 467 +++++++
25 drivers/net/ethernet/ralink/mt7530.h | 20 +
26 drivers/net/ethernet/ralink/ralink_soc_eth.c | 845 +++++++++++
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 | 51 +
30 drivers/net/ethernet/ralink/soc_rt305x.c | 113 ++
31 drivers/net/ethernet/ralink/soc_rt3883.c | 60 +
32 22 files changed, 4814 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
52
53 --- /dev/null
54 +++ b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
55 @@ -0,0 +1,27 @@
56 +/*
57 + * Ralink RT305x SoC platform device registration
58 + *
59 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
60 + *
61 + * This program is free software; you can redistribute it and/or modify it
62 + * under the terms of the GNU General Public License version 2 as published
63 + * by the Free Software Foundation.
64 + */
65 +
66 +#ifndef _RT305X_ESW_PLATFORM_H
67 +#define _RT305X_ESW_PLATFORM_H
68 +
69 +enum {
70 + RT305X_ESW_VLAN_CONFIG_NONE = 0,
71 + RT305X_ESW_VLAN_CONFIG_LLLLW,
72 + RT305X_ESW_VLAN_CONFIG_WLLLL,
73 +};
74 +
75 +struct rt305x_esw_platform_data
76 +{
77 + u8 vlan_config;
78 + u32 reg_initval_fct2;
79 + u32 reg_initval_fpa2;
80 +};
81 +
82 +#endif /* _RT305X_ESW_PLATFORM_H */
83 --- a/arch/mips/ralink/rt305x.c
84 +++ b/arch/mips/ralink/rt305x.c
85 @@ -194,6 +194,7 @@ void __init ralink_clk_init(void)
86 }
87
88 ralink_clk_add("cpu", cpu_rate);
89 + ralink_clk_add("sys", sys_rate);
90 ralink_clk_add("10000b00.spi", sys_rate);
91 ralink_clk_add("10000100.timer", wdt_rate);
92 ralink_clk_add("10000120.watchdog", wdt_rate);
93 --- a/drivers/net/ethernet/Kconfig
94 +++ b/drivers/net/ethernet/Kconfig
95 @@ -135,6 +135,7 @@ config ETHOC
96 source "drivers/net/ethernet/packetengines/Kconfig"
97 source "drivers/net/ethernet/pasemi/Kconfig"
98 source "drivers/net/ethernet/qlogic/Kconfig"
99 +source "drivers/net/ethernet/ralink/Kconfig"
100 source "drivers/net/ethernet/realtek/Kconfig"
101 source "drivers/net/ethernet/renesas/Kconfig"
102 source "drivers/net/ethernet/rdc/Kconfig"
103 --- a/drivers/net/ethernet/Makefile
104 +++ b/drivers/net/ethernet/Makefile
105 @@ -53,6 +53,7 @@ obj-$(CONFIG_ETHOC) += ethoc.o
106 obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
107 obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
108 obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
109 +obj-$(CONFIG_NET_RALINK) += ralink/
110 obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
111 obj-$(CONFIG_SH_ETH) += renesas/
112 obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
113 --- /dev/null
114 +++ b/drivers/net/ethernet/ralink/Kconfig
115 @@ -0,0 +1,32 @@
116 +config NET_RALINK
117 + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
118 + depends on RALINK
119 + help
120 + This driver supports the ethernet mac inside the ralink wisocs
121 +
122 +if NET_RALINK
123 +
124 +config NET_RALINK_MDIO
125 + def_bool NET_RALINK
126 + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
127 + select PHYLIB
128 +
129 +config NET_RALINK_MDIO_RT2880
130 + def_bool NET_RALINK
131 + depends on (SOC_RT288X || SOC_RT3883)
132 + select NET_RALINK_MDIO
133 +
134 +config NET_RALINK_ESW_RT3052
135 + def_bool NET_RALINK
136 + depends on SOC_RT305X
137 + select PHYLIB
138 + select SWCONFIG
139 +
140 +config NET_RALINK_GSW_MT7620
141 + def_bool NET_RALINK
142 + depends on SOC_MT7620
143 + select INET_LRO
144 + select NET_RALINK_MDIO
145 + select PHYLIB
146 + select SWCONFIG
147 +endif
148 --- /dev/null
149 +++ b/drivers/net/ethernet/ralink/Makefile
150 @@ -0,0 +1,18 @@
151 +#
152 +# Makefile for the Ralink SoCs built-in ethernet macs
153 +#
154 +
155 +ralink-eth-y += ralink_soc_eth.o
156 +
157 +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
158 +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
159 +
160 +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
161 +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o mt7530.o
162 +
163 +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
164 +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
165 +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
166 +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
167 +
168 +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
169 --- /dev/null
170 +++ b/drivers/net/ethernet/ralink/esw_rt3052.c
171 @@ -0,0 +1,1463 @@
172 +/*
173 + * This program is free software; you can redistribute it and/or modify
174 + * it under the terms of the GNU General Public License as published by
175 + * the Free Software Foundation; version 2 of the License
176 + *
177 + * This program is distributed in the hope that it will be useful,
178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
180 + * GNU General Public License for more details.
181 + *
182 + * You should have received a copy of the GNU General Public License
183 + * along with this program; if not, write to the Free Software
184 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
185 + *
186 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
187 + */
188 +
189 +#include <linux/module.h>
190 +#include <linux/kernel.h>
191 +#include <linux/types.h>
192 +#include <linux/dma-mapping.h>
193 +#include <linux/init.h>
194 +#include <linux/skbuff.h>
195 +#include <linux/etherdevice.h>
196 +#include <linux/ethtool.h>
197 +#include <linux/platform_device.h>
198 +#include <linux/of_device.h>
199 +#include <linux/clk.h>
200 +#include <linux/of_net.h>
201 +#include <linux/of_mdio.h>
202 +
203 +#include <asm/mach-ralink/ralink_regs.h>
204 +
205 +#include "ralink_soc_eth.h"
206 +
207 +#include <linux/ioport.h>
208 +#include <linux/switch.h>
209 +#include <linux/mii.h>
210 +
211 +#include <ralink_regs.h>
212 +#include <asm/mach-ralink/rt305x.h>
213 +#include <asm/mach-ralink/rt305x_esw_platform.h>
214 +
215 +/*
216 + * HW limitations for this switch:
217 + * - No large frame support (PKT_MAX_LEN at most 1536)
218 + * - Can't have untagged vlan and tagged vlan on one port at the same time,
219 + * though this might be possible using the undocumented PPE.
220 + */
221 +
222 +#define RT305X_ESW_REG_ISR 0x00
223 +#define RT305X_ESW_REG_IMR 0x04
224 +#define RT305X_ESW_REG_FCT0 0x08
225 +#define RT305X_ESW_REG_PFC1 0x14
226 +#define RT305X_ESW_REG_ATS 0x24
227 +#define RT305X_ESW_REG_ATS0 0x28
228 +#define RT305X_ESW_REG_ATS1 0x2c
229 +#define RT305X_ESW_REG_ATS2 0x30
230 +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
231 +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
232 +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
233 +#define RT305X_ESW_REG_POA 0x80
234 +#define RT305X_ESW_REG_FPA 0x84
235 +#define RT305X_ESW_REG_SOCPC 0x8c
236 +#define RT305X_ESW_REG_POC0 0x90
237 +#define RT305X_ESW_REG_POC1 0x94
238 +#define RT305X_ESW_REG_POC2 0x98
239 +#define RT305X_ESW_REG_SGC 0x9c
240 +#define RT305X_ESW_REG_STRT 0xa0
241 +#define RT305X_ESW_REG_PCR0 0xc0
242 +#define RT305X_ESW_REG_PCR1 0xc4
243 +#define RT305X_ESW_REG_FPA2 0xc8
244 +#define RT305X_ESW_REG_FCT2 0xcc
245 +#define RT305X_ESW_REG_SGC2 0xe4
246 +#define RT305X_ESW_REG_P0LED 0xa4
247 +#define RT305X_ESW_REG_P1LED 0xa8
248 +#define RT305X_ESW_REG_P2LED 0xac
249 +#define RT305X_ESW_REG_P3LED 0xb0
250 +#define RT305X_ESW_REG_P4LED 0xb4
251 +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
252 +#define RT305X_ESW_REG_P1PC 0xec
253 +#define RT305X_ESW_REG_P2PC 0xf0
254 +#define RT305X_ESW_REG_P3PC 0xf4
255 +#define RT305X_ESW_REG_P4PC 0xf8
256 +#define RT305X_ESW_REG_P5PC 0xfc
257 +
258 +#define RT305X_ESW_LED_LINK 0
259 +#define RT305X_ESW_LED_100M 1
260 +#define RT305X_ESW_LED_DUPLEX 2
261 +#define RT305X_ESW_LED_ACTIVITY 3
262 +#define RT305X_ESW_LED_COLLISION 4
263 +#define RT305X_ESW_LED_LINKACT 5
264 +#define RT305X_ESW_LED_DUPLCOLL 6
265 +#define RT305X_ESW_LED_10MACT 7
266 +#define RT305X_ESW_LED_100MACT 8
267 +/* Additional led states not in datasheet: */
268 +#define RT305X_ESW_LED_BLINK 10
269 +#define RT305X_ESW_LED_ON 12
270 +
271 +#define RT305X_ESW_LINK_S 25
272 +#define RT305X_ESW_DUPLEX_S 9
273 +#define RT305X_ESW_SPD_S 0
274 +
275 +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
276 +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
277 +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
278 +
279 +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
280 +
281 +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
282 +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
283 +
284 +#define RT305X_ESW_PVIDC_PVID_M 0xfff
285 +#define RT305X_ESW_PVIDC_PVID_S 12
286 +
287 +#define RT305X_ESW_VLANI_VID_M 0xfff
288 +#define RT305X_ESW_VLANI_VID_S 12
289 +
290 +#define RT305X_ESW_VMSC_MSC_M 0xff
291 +#define RT305X_ESW_VMSC_MSC_S 8
292 +
293 +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
294 +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
295 +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
296 +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
297 +
298 +#define RT305X_ESW_POC0_EN_BP_S 0
299 +#define RT305X_ESW_POC0_EN_FC_S 8
300 +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
301 +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
302 +#define RT305X_ESW_POC0_DIS_PORT_S 23
303 +
304 +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
305 +#define RT305X_ESW_POC2_UNTAG_EN_S 0
306 +#define RT305X_ESW_POC2_ENAGING_S 8
307 +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
308 +
309 +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
310 +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
311 +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
312 +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
313 +
314 +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
315 +#define RT305X_ESW_PFC1_EN_VLAN_S 16
316 +#define RT305X_ESW_PFC1_EN_TOS_S 24
317 +
318 +#define RT305X_ESW_VLAN_NONE 0xfff
319 +
320 +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
321 +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
322 +
323 +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
324 +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
325 +
326 +#define RT305X_ESW_POA_LINK_MASK 0x1f
327 +#define RT305X_ESW_POA_LINK_SHIFT 25
328 +
329 +#define RT305X_ESW_PORT_ST_CHG BIT(26)
330 +#define RT305X_ESW_PORT0 0
331 +#define RT305X_ESW_PORT1 1
332 +#define RT305X_ESW_PORT2 2
333 +#define RT305X_ESW_PORT3 3
334 +#define RT305X_ESW_PORT4 4
335 +#define RT305X_ESW_PORT5 5
336 +#define RT305X_ESW_PORT6 6
337 +
338 +#define RT305X_ESW_PORTS_NONE 0
339 +
340 +#define RT305X_ESW_PMAP_LLLLLL 0x3f
341 +#define RT305X_ESW_PMAP_LLLLWL 0x2f
342 +#define RT305X_ESW_PMAP_WLLLLL 0x3e
343 +
344 +#define RT305X_ESW_PORTS_INTERNAL \
345 + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
346 + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
347 + BIT(RT305X_ESW_PORT4))
348 +
349 +#define RT305X_ESW_PORTS_NOCPU \
350 + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
351 +
352 +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
353 +
354 +#define RT305X_ESW_PORTS_ALL \
355 + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
356 +
357 +#define RT305X_ESW_NUM_VLANS 16
358 +#define RT305X_ESW_NUM_VIDS 4096
359 +#define RT305X_ESW_NUM_PORTS 7
360 +#define RT305X_ESW_NUM_LANWAN 6
361 +#define RT305X_ESW_NUM_LEDS 5
362 +
363 +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
364 +#define RT5350_EWS_REG_LED_POLARITY 0x168
365 +#define RT5350_RESET_EPHY BIT(24)
366 +#define SYSC_REG_RESET_CTRL 0x34
367 +
368 +enum {
369 + /* Global attributes. */
370 + RT305X_ESW_ATTR_ENABLE_VLAN,
371 + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
372 + RT305X_ESW_ATTR_BC_STATUS,
373 + RT305X_ESW_ATTR_LED_FREQ,
374 + /* Port attributes. */
375 + RT305X_ESW_ATTR_PORT_DISABLE,
376 + RT305X_ESW_ATTR_PORT_DOUBLETAG,
377 + RT305X_ESW_ATTR_PORT_UNTAG,
378 + RT305X_ESW_ATTR_PORT_LED,
379 + RT305X_ESW_ATTR_PORT_LAN,
380 + RT305X_ESW_ATTR_PORT_RECV_BAD,
381 + RT305X_ESW_ATTR_PORT_RECV_GOOD,
382 + RT5350_ESW_ATTR_PORT_TR_BAD,
383 + RT5350_ESW_ATTR_PORT_TR_GOOD,
384 +};
385 +
386 +struct esw_port {
387 + bool disable;
388 + bool doubletag;
389 + bool untag;
390 + u8 led;
391 + u16 pvid;
392 +};
393 +
394 +struct esw_vlan {
395 + u8 ports;
396 + u16 vid;
397 +};
398 +
399 +struct rt305x_esw {
400 + struct device *dev;
401 + void __iomem *base;
402 + int irq;
403 + const struct rt305x_esw_platform_data *pdata;
404 + /* Protects against concurrent register rmw operations. */
405 + spinlock_t reg_rw_lock;
406 +
407 + unsigned char port_map;
408 + unsigned int reg_initval_fct2;
409 + unsigned int reg_initval_fpa2;
410 + unsigned int reg_led_polarity;
411 +
412 +
413 + struct switch_dev swdev;
414 + bool global_vlan_enable;
415 + bool alt_vlan_disable;
416 + int bc_storm_protect;
417 + int led_frequency;
418 + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
419 + struct esw_port ports[RT305X_ESW_NUM_PORTS];
420 +
421 +};
422 +
423 +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
424 +{
425 + __raw_writel(val, esw->base + reg);
426 +}
427 +
428 +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
429 +{
430 + return __raw_readl(esw->base + reg);
431 +}
432 +
433 +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
434 + unsigned long val)
435 +{
436 + unsigned long t;
437 +
438 + t = __raw_readl(esw->base + reg) & ~mask;
439 + __raw_writel(t | val, esw->base + reg);
440 +}
441 +
442 +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
443 + unsigned long val)
444 +{
445 + unsigned long flags;
446 +
447 + spin_lock_irqsave(&esw->reg_rw_lock, flags);
448 + esw_rmw_raw(esw, reg, mask, val);
449 + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
450 +}
451 +
452 +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
453 + u32 write_data)
454 +{
455 + unsigned long t_start = jiffies;
456 + int ret = 0;
457 +
458 + while (1) {
459 + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
460 + RT305X_ESW_PCR1_WT_DONE))
461 + break;
462 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
463 + ret = 1;
464 + goto out;
465 + }
466 + }
467 +
468 + write_data &= 0xffff;
469 + esw_w32(esw,
470 + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
471 + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
472 + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
473 + RT305X_ESW_REG_PCR0);
474 +
475 + t_start = jiffies;
476 + while (1) {
477 + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
478 + RT305X_ESW_PCR1_WT_DONE)
479 + break;
480 +
481 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
482 + ret = 1;
483 + break;
484 + }
485 + }
486 +out:
487 + if (ret)
488 + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
489 + return ret;
490 +}
491 +
492 +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
493 +{
494 + unsigned s;
495 + unsigned val;
496 +
497 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
498 + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
499 + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
500 +
501 + return val;
502 +}
503 +
504 +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
505 +{
506 + unsigned s;
507 +
508 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
509 + esw_rmw(esw,
510 + RT305X_ESW_REG_VLANI(vlan / 2),
511 + RT305X_ESW_VLANI_VID_M << s,
512 + (vid & RT305X_ESW_VLANI_VID_M) << s);
513 +}
514 +
515 +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
516 +{
517 + unsigned s, val;
518 +
519 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
520 + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
521 + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
522 +}
523 +
524 +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
525 +{
526 + unsigned s;
527 +
528 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
529 + esw_rmw(esw,
530 + RT305X_ESW_REG_PVIDC(port / 2),
531 + RT305X_ESW_PVIDC_PVID_M << s,
532 + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
533 +}
534 +
535 +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
536 +{
537 + unsigned s, val;
538 +
539 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
540 + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
541 + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
542 +
543 + return val;
544 +}
545 +
546 +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
547 +{
548 + unsigned s;
549 +
550 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
551 + esw_rmw(esw,
552 + RT305X_ESW_REG_VMSC(vlan / 4),
553 + RT305X_ESW_VMSC_MSC_M << s,
554 + (msc & RT305X_ESW_VMSC_MSC_M) << s);
555 +}
556 +
557 +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
558 +{
559 + unsigned reg;
560 + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
561 + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
562 + RT305X_ESW_POC0_DIS_PORT_M;
563 +}
564 +
565 +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
566 +{
567 + unsigned old_mask;
568 + unsigned enable_mask;
569 + unsigned changed;
570 + int i;
571 +
572 + old_mask = esw_get_port_disable(esw);
573 + changed = old_mask ^ disable_mask;
574 + enable_mask = old_mask & disable_mask;
575 +
576 + /* enable before writing to MII */
577 + esw_rmw(esw, RT305X_ESW_REG_POC0,
578 + (RT305X_ESW_POC0_DIS_PORT_M <<
579 + RT305X_ESW_POC0_DIS_PORT_S),
580 + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
581 +
582 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
583 + if (!(changed & (1 << i)))
584 + continue;
585 + if (disable_mask & (1 << i)) {
586 + /* disable */
587 + rt305x_mii_write(esw, i, MII_BMCR,
588 + BMCR_PDOWN);
589 + } else {
590 + /* enable */
591 + rt305x_mii_write(esw, i, MII_BMCR,
592 + BMCR_FULLDPLX |
593 + BMCR_ANENABLE |
594 + BMCR_ANRESTART |
595 + BMCR_SPEED100);
596 + }
597 + }
598 +
599 + /* disable after writing to MII */
600 + esw_rmw(esw, RT305X_ESW_REG_POC0,
601 + (RT305X_ESW_POC0_DIS_PORT_M <<
602 + RT305X_ESW_POC0_DIS_PORT_S),
603 + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
604 +}
605 +
606 +static void esw_set_gsc(struct rt305x_esw *esw)
607 +{
608 + esw_rmw(esw, RT305X_ESW_REG_SGC,
609 + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
610 + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
611 + esw_rmw(esw, RT305X_ESW_REG_SGC,
612 + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
613 + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
614 +}
615 +
616 +static int esw_apply_config(struct switch_dev *dev);
617 +
618 +static void esw_hw_init(struct rt305x_esw *esw)
619 +{
620 + int i;
621 + u8 port_disable = 0;
622 + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
623 +
624 + /* vodoo from original driver */
625 + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
626 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
627 + /* Port priority 1 for all ports, vlan enabled. */
628 + esw_w32(esw, 0x00005555 |
629 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
630 + RT305X_ESW_REG_PFC1);
631 +
632 + /* Enable Back Pressure, and Flow Control */
633 + esw_w32(esw,
634 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
635 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
636 + RT305X_ESW_REG_POC0);
637 +
638 + /* Enable Aging, and VLAN TAG removal */
639 + esw_w32(esw,
640 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
641 + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
642 + RT305X_ESW_REG_POC2);
643 +
644 + if (esw->reg_initval_fct2)
645 + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
646 + else
647 + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
648 +
649 + /*
650 + * 300s aging timer, max packet len 1536, broadcast storm prevention
651 + * disabled, disable collision abort, mac xor48 hash, 10 packet back
652 + * pressure jam, GMII disable was_transmit, back pressure disabled,
653 + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
654 + * ports.
655 + */
656 + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
657 +
658 + /* Setup SoC Port control register */
659 + esw_w32(esw,
660 + (RT305X_ESW_SOCPC_CRC_PADDING |
661 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
662 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
663 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
664 + RT305X_ESW_REG_SOCPC);
665 +
666 + if (esw->reg_initval_fpa2)
667 + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
668 + else
669 + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
670 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
671 +
672 + /* Force Link/Activity on ports */
673 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
674 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
675 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
676 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
677 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
678 +
679 + /* Copy disabled port configuration from bootloader setup */
680 + port_disable = esw_get_port_disable(esw);
681 + for (i = 0; i < 6; i++)
682 + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
683 +
684 + if (soc_is_rt3352()) {
685 + /* reset EPHY */
686 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
687 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
688 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
689 +
690 + rt305x_mii_write(esw, 0, 31, 0x8000);
691 + for (i = 0; i < 5; i++) {
692 + if (esw->ports[i].disable) {
693 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
694 + } else {
695 + rt305x_mii_write(esw, i, MII_BMCR,
696 + BMCR_FULLDPLX |
697 + BMCR_ANENABLE |
698 + BMCR_SPEED100);
699 + }
700 + /* TX10 waveform coefficient LSB=0 disable PHY */
701 + rt305x_mii_write(esw, i, 26, 0x1601);
702 + /* TX100/TX10 AD/DA current bias */
703 + rt305x_mii_write(esw, i, 29, 0x7016);
704 + /* TX100 slew rate control */
705 + rt305x_mii_write(esw, i, 30, 0x0038);
706 + }
707 +
708 + /* select global register */
709 + rt305x_mii_write(esw, 0, 31, 0x0);
710 + /* enlarge agcsel threshold 3 and threshold 2 */
711 + rt305x_mii_write(esw, 0, 1, 0x4a40);
712 + /* enlarge agcsel threshold 5 and threshold 4 */
713 + rt305x_mii_write(esw, 0, 2, 0x6254);
714 + /* enlarge agcsel threshold */
715 + rt305x_mii_write(esw, 0, 3, 0xa17f);
716 + rt305x_mii_write(esw, 0,12, 0x7eaa);
717 + /* longer TP_IDL tail length */
718 + rt305x_mii_write(esw, 0, 14, 0x65);
719 + /* increased squelch pulse count threshold. */
720 + rt305x_mii_write(esw, 0, 16, 0x0684);
721 + /* set TX10 signal amplitude threshold to minimum */
722 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
723 + /* set squelch amplitude to higher threshold */
724 + rt305x_mii_write(esw, 0, 18, 0x40ba);
725 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
726 + rt305x_mii_write(esw, 0, 22, 0x253f);
727 + /* set PLL/Receive bias current are calibrated */
728 + rt305x_mii_write(esw, 0, 27, 0x2fda);
729 + /* change PLL/Receive bias current to internal(RT3350) */
730 + rt305x_mii_write(esw, 0, 28, 0xc410);
731 + /* change PLL bias current to internal(RT3052_MP3) */
732 + rt305x_mii_write(esw, 0, 29, 0x598b);
733 + /* select local register */
734 + rt305x_mii_write(esw, 0, 31, 0x8000);
735 + } else if (soc_is_rt5350()) {
736 + /* reset EPHY */
737 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
738 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
739 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
740 +
741 + /* set the led polarity */
742 + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
743 +
744 + /* local registers */
745 + rt305x_mii_write(esw, 0, 31, 0x8000);
746 + for (i = 0; i < 5; i++) {
747 + if (esw->ports[i].disable) {
748 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
749 + } else {
750 + rt305x_mii_write(esw, i, MII_BMCR,
751 + BMCR_FULLDPLX |
752 + BMCR_ANENABLE |
753 + BMCR_SPEED100);
754 + }
755 + /* TX10 waveform coefficient LSB=0 disable PHY */
756 + rt305x_mii_write(esw, i, 26, 0x1601);
757 + /* TX100/TX10 AD/DA current bias */
758 + rt305x_mii_write(esw, i, 29, 0x7015);
759 + /* TX100 slew rate control */
760 + rt305x_mii_write(esw, i, 30, 0x0038);
761 + }
762 +
763 + /* global registers */
764 + rt305x_mii_write(esw, 0, 31, 0x0);
765 + /* enlarge agcsel threshold 3 and threshold 2 */
766 + rt305x_mii_write(esw, 0, 1, 0x4a40);
767 + /* enlarge agcsel threshold 5 and threshold 4 */
768 + rt305x_mii_write(esw, 0, 2, 0x6254);
769 + /* enlarge agcsel threshold 6 */
770 + rt305x_mii_write(esw, 0, 3, 0xa17f);
771 + rt305x_mii_write(esw, 0, 12, 0x7eaa);
772 + /* longer TP_IDL tail length */
773 + rt305x_mii_write(esw, 0, 14, 0x65);
774 + /* increased squelch pulse count threshold. */
775 + rt305x_mii_write(esw, 0, 16, 0x0684);
776 + /* set TX10 signal amplitude threshold to minimum */
777 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
778 + /* set squelch amplitude to higher threshold */
779 + rt305x_mii_write(esw, 0, 18, 0x40ba);
780 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
781 + rt305x_mii_write(esw, 0, 22, 0x253f);
782 + /* set PLL/Receive bias current are calibrated */
783 + rt305x_mii_write(esw, 0, 27, 0x2fda);
784 + /* change PLL/Receive bias current to internal(RT3350) */
785 + rt305x_mii_write(esw, 0, 28, 0xc410);
786 + /* change PLL bias current to internal(RT3052_MP3) */
787 + rt305x_mii_write(esw, 0, 29, 0x598b);
788 + /* select local register */
789 + rt305x_mii_write(esw, 0, 31, 0x8000);
790 + } else {
791 + rt305x_mii_write(esw, 0, 31, 0x8000);
792 + for (i = 0; i < 5; i++) {
793 + if (esw->ports[i].disable) {
794 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
795 + } else {
796 + rt305x_mii_write(esw, i, MII_BMCR,
797 + BMCR_FULLDPLX |
798 + BMCR_ANENABLE |
799 + BMCR_SPEED100);
800 + }
801 + /* TX10 waveform coefficient */
802 + rt305x_mii_write(esw, i, 26, 0x1601);
803 + /* TX100/TX10 AD/DA current bias */
804 + rt305x_mii_write(esw, i, 29, 0x7058);
805 + /* TX100 slew rate control */
806 + rt305x_mii_write(esw, i, 30, 0x0018);
807 + }
808 +
809 + /* PHY IOT */
810 + /* select global register */
811 + rt305x_mii_write(esw, 0, 31, 0x0);
812 + /* tune TP_IDL tail and head waveform */
813 + rt305x_mii_write(esw, 0, 22, 0x052f);
814 + /* set TX10 signal amplitude threshold to minimum */
815 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
816 + /* set squelch amplitude to higher threshold */
817 + rt305x_mii_write(esw, 0, 18, 0x40ba);
818 + /* longer TP_IDL tail length */
819 + rt305x_mii_write(esw, 0, 14, 0x65);
820 + /* select local register */
821 + rt305x_mii_write(esw, 0, 31, 0x8000);
822 + }
823 +
824 + if (esw->port_map)
825 + port_map = esw->port_map;
826 + else
827 + port_map = RT305X_ESW_PMAP_LLLLLL;
828 +
829 + /*
830 + * Unused HW feature, but still nice to be consistent here...
831 + * This is also exported to userspace ('lan' attribute) so it's
832 + * conveniently usable to decide which ports go into the wan vlan by
833 + * default.
834 + */
835 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
836 + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
837 + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
838 +
839 + /* make the switch leds blink */
840 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
841 + esw->ports[i].led = 0x05;
842 +
843 + /* Apply the empty config. */
844 + esw_apply_config(&esw->swdev);
845 +
846 + /* Only unmask the port change interrupt */
847 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
848 +}
849 +
850 +static irqreturn_t esw_interrupt(int irq, void *_esw)
851 +{
852 + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
853 + u32 status;
854 +
855 + status = esw_r32(esw, RT305X_ESW_REG_ISR);
856 + if (status & RT305X_ESW_PORT_ST_CHG) {
857 + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
858 + link >>= RT305X_ESW_POA_LINK_SHIFT;
859 + link &= RT305X_ESW_POA_LINK_MASK;
860 + dev_info(esw->dev, "link changed 0x%02X\n", link);
861 + }
862 + esw_w32(esw, status, RT305X_ESW_REG_ISR);
863 +
864 + return IRQ_HANDLED;
865 +}
866 +
867 +static int esw_apply_config(struct switch_dev *dev)
868 +{
869 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
870 + int i;
871 + u8 disable = 0;
872 + u8 doubletag = 0;
873 + u8 en_vlan = 0;
874 + u8 untag = 0;
875 +
876 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
877 + u32 vid, vmsc;
878 + if (esw->global_vlan_enable) {
879 + vid = esw->vlans[i].vid;
880 + vmsc = esw->vlans[i].ports;
881 + } else {
882 + vid = RT305X_ESW_VLAN_NONE;
883 + vmsc = RT305X_ESW_PORTS_NONE;
884 + }
885 + esw_set_vlan_id(esw, i, vid);
886 + esw_set_vmsc(esw, i, vmsc);
887 + }
888 +
889 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
890 + u32 pvid;
891 + disable |= esw->ports[i].disable << i;
892 + if (esw->global_vlan_enable) {
893 + doubletag |= esw->ports[i].doubletag << i;
894 + en_vlan |= 1 << i;
895 + untag |= esw->ports[i].untag << i;
896 + pvid = esw->ports[i].pvid;
897 + } else {
898 + int x = esw->alt_vlan_disable ? 0 : 1;
899 + doubletag |= x << i;
900 + en_vlan |= x << i;
901 + untag |= x << i;
902 + pvid = 0;
903 + }
904 + esw_set_pvid(esw, i, pvid);
905 + if (i < RT305X_ESW_NUM_LEDS)
906 + esw_w32(esw, esw->ports[i].led,
907 + RT305X_ESW_REG_P0LED + 4*i);
908 + }
909 +
910 + esw_set_gsc(esw);
911 + esw_set_port_disable(esw, disable);
912 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
913 + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
914 + RT305X_ESW_SGC2_DOUBLE_TAG_S),
915 + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
916 + esw_rmw(esw, RT305X_ESW_REG_PFC1,
917 + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
918 + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
919 + esw_rmw(esw, RT305X_ESW_REG_POC2,
920 + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
921 + untag << RT305X_ESW_POC2_UNTAG_EN_S);
922 +
923 + if (!esw->global_vlan_enable) {
924 + /*
925 + * Still need to put all ports into vlan 0 or they'll be
926 + * isolated.
927 + * NOTE: vlan 0 is special, no vlan tag is prepended
928 + */
929 + esw_set_vlan_id(esw, 0, 0);
930 + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
931 + }
932 +
933 + return 0;
934 +}
935 +
936 +static int esw_reset_switch(struct switch_dev *dev)
937 +{
938 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
939 +
940 + esw->global_vlan_enable = 0;
941 + memset(esw->ports, 0, sizeof(esw->ports));
942 + memset(esw->vlans, 0, sizeof(esw->vlans));
943 + esw_hw_init(esw);
944 +
945 + return 0;
946 +}
947 +
948 +static int esw_get_vlan_enable(struct switch_dev *dev,
949 + const struct switch_attr *attr,
950 + struct switch_val *val)
951 +{
952 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
953 +
954 + val->value.i = esw->global_vlan_enable;
955 +
956 + return 0;
957 +}
958 +
959 +static int esw_set_vlan_enable(struct switch_dev *dev,
960 + const struct switch_attr *attr,
961 + struct switch_val *val)
962 +{
963 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
964 +
965 + esw->global_vlan_enable = val->value.i != 0;
966 +
967 + return 0;
968 +}
969 +
970 +static int esw_get_alt_vlan_disable(struct switch_dev *dev,
971 + const struct switch_attr *attr,
972 + struct switch_val *val)
973 +{
974 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
975 +
976 + val->value.i = esw->alt_vlan_disable;
977 +
978 + return 0;
979 +}
980 +
981 +static int esw_set_alt_vlan_disable(struct switch_dev *dev,
982 + const struct switch_attr *attr,
983 + struct switch_val *val)
984 +{
985 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
986 +
987 + esw->alt_vlan_disable = val->value.i != 0;
988 +
989 + return 0;
990 +}
991 +
992 +static int
993 +rt305x_esw_set_bc_status(struct switch_dev *dev,
994 + const struct switch_attr *attr,
995 + struct switch_val *val)
996 +{
997 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
998 +
999 + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
1000 +
1001 + return 0;
1002 +}
1003 +
1004 +static int
1005 +rt305x_esw_get_bc_status(struct switch_dev *dev,
1006 + const struct switch_attr *attr,
1007 + struct switch_val *val)
1008 +{
1009 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1010 +
1011 + val->value.i = esw->bc_storm_protect;
1012 +
1013 + return 0;
1014 +}
1015 +
1016 +static int
1017 +rt305x_esw_set_led_freq(struct switch_dev *dev,
1018 + const struct switch_attr *attr,
1019 + struct switch_val *val)
1020 +{
1021 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1022 +
1023 + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
1024 +
1025 + return 0;
1026 +}
1027 +
1028 +static int
1029 +rt305x_esw_get_led_freq(struct switch_dev *dev,
1030 + const struct switch_attr *attr,
1031 + struct switch_val *val)
1032 +{
1033 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1034 +
1035 + val->value.i = esw->led_frequency;
1036 +
1037 + return 0;
1038 +}
1039 +
1040 +static int esw_get_port_link(struct switch_dev *dev,
1041 + int port,
1042 + struct switch_port_link *link)
1043 +{
1044 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1045 + u32 speed, poa;
1046 +
1047 + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
1048 + return -EINVAL;
1049 +
1050 + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
1051 +
1052 + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
1053 + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
1054 + if (port < RT305X_ESW_NUM_LEDS) {
1055 + speed = (poa >> RT305X_ESW_SPD_S) & 1;
1056 + } else {
1057 + if (port == RT305X_ESW_NUM_PORTS - 1)
1058 + poa >>= 1;
1059 + speed = (poa >> RT305X_ESW_SPD_S) & 3;
1060 + }
1061 + switch (speed) {
1062 + case 0:
1063 + link->speed = SWITCH_PORT_SPEED_10;
1064 + break;
1065 + case 1:
1066 + link->speed = SWITCH_PORT_SPEED_100;
1067 + break;
1068 + case 2:
1069 + case 3: /* forced gige speed can be 2 or 3 */
1070 + link->speed = SWITCH_PORT_SPEED_1000;
1071 + break;
1072 + default:
1073 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
1074 + break;
1075 + }
1076 +
1077 + return 0;
1078 +}
1079 +
1080 +static int esw_get_port_bool(struct switch_dev *dev,
1081 + const struct switch_attr *attr,
1082 + struct switch_val *val)
1083 +{
1084 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1085 + int idx = val->port_vlan;
1086 + u32 x, reg, shift;
1087 +
1088 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
1089 + return -EINVAL;
1090 +
1091 + switch (attr->id) {
1092 + case RT305X_ESW_ATTR_PORT_DISABLE:
1093 + reg = RT305X_ESW_REG_POC0;
1094 + shift = RT305X_ESW_POC0_DIS_PORT_S;
1095 + break;
1096 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1097 + reg = RT305X_ESW_REG_SGC2;
1098 + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
1099 + break;
1100 + case RT305X_ESW_ATTR_PORT_UNTAG:
1101 + reg = RT305X_ESW_REG_POC2;
1102 + shift = RT305X_ESW_POC2_UNTAG_EN_S;
1103 + break;
1104 + case RT305X_ESW_ATTR_PORT_LAN:
1105 + reg = RT305X_ESW_REG_SGC2;
1106 + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
1107 + if (idx >= RT305X_ESW_NUM_LANWAN)
1108 + return -EINVAL;
1109 + break;
1110 + default:
1111 + return -EINVAL;
1112 + }
1113 +
1114 + x = esw_r32(esw, reg);
1115 + val->value.i = (x >> (idx + shift)) & 1;
1116 +
1117 + return 0;
1118 +}
1119 +
1120 +static int esw_set_port_bool(struct switch_dev *dev,
1121 + const struct switch_attr *attr,
1122 + struct switch_val *val)
1123 +{
1124 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1125 + int idx = val->port_vlan;
1126 +
1127 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1128 + val->value.i < 0 || val->value.i > 1)
1129 + return -EINVAL;
1130 +
1131 + switch (attr->id) {
1132 + case RT305X_ESW_ATTR_PORT_DISABLE:
1133 + esw->ports[idx].disable = val->value.i;
1134 + break;
1135 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1136 + esw->ports[idx].doubletag = val->value.i;
1137 + break;
1138 + case RT305X_ESW_ATTR_PORT_UNTAG:
1139 + esw->ports[idx].untag = val->value.i;
1140 + break;
1141 + default:
1142 + return -EINVAL;
1143 + }
1144 +
1145 + return 0;
1146 +}
1147 +
1148 +static int esw_get_port_recv_badgood(struct switch_dev *dev,
1149 + const struct switch_attr *attr,
1150 + struct switch_val *val)
1151 +{
1152 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1153 + int idx = val->port_vlan;
1154 + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
1155 + u32 reg;
1156 +
1157 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1158 + return -EINVAL;
1159 + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
1160 + val->value.i = (reg >> shift) & 0xffff;
1161 +
1162 + return 0;
1163 +}
1164 +
1165 +static int
1166 +esw_get_port_tr_badgood(struct switch_dev *dev,
1167 + const struct switch_attr *attr,
1168 + struct switch_val *val)
1169 +{
1170 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1171 +
1172 + int idx = val->port_vlan;
1173 + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
1174 + u32 reg;
1175 +
1176 + if (!soc_is_rt5350())
1177 + return -EINVAL;
1178 +
1179 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1180 + return -EINVAL;
1181 +
1182 + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
1183 + val->value.i = (reg >> shift) & 0xffff;
1184 +
1185 + return 0;
1186 +}
1187 +
1188 +static int esw_get_port_led(struct switch_dev *dev,
1189 + const struct switch_attr *attr,
1190 + struct switch_val *val)
1191 +{
1192 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1193 + int idx = val->port_vlan;
1194 +
1195 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1196 + idx >= RT305X_ESW_NUM_LEDS)
1197 + return -EINVAL;
1198 +
1199 + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
1200 +
1201 + return 0;
1202 +}
1203 +
1204 +static int esw_set_port_led(struct switch_dev *dev,
1205 + const struct switch_attr *attr,
1206 + struct switch_val *val)
1207 +{
1208 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1209 + int idx = val->port_vlan;
1210 +
1211 + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
1212 + return -EINVAL;
1213 +
1214 + esw->ports[idx].led = val->value.i;
1215 +
1216 + return 0;
1217 +}
1218 +
1219 +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1220 +{
1221 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1222 +
1223 + if (port >= RT305X_ESW_NUM_PORTS)
1224 + return -EINVAL;
1225 +
1226 + *val = esw_get_pvid(esw, port);
1227 +
1228 + return 0;
1229 +}
1230 +
1231 +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
1232 +{
1233 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1234 +
1235 + if (port >= RT305X_ESW_NUM_PORTS)
1236 + return -EINVAL;
1237 +
1238 + esw->ports[port].pvid = val;
1239 +
1240 + return 0;
1241 +}
1242 +
1243 +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1244 +{
1245 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1246 + u32 vmsc, poc2;
1247 + int vlan_idx = -1;
1248 + int i;
1249 +
1250 + val->len = 0;
1251 +
1252 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
1253 + return -EINVAL;
1254 +
1255 + /* valid vlan? */
1256 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1257 + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
1258 + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
1259 + vlan_idx = i;
1260 + break;
1261 + }
1262 + }
1263 +
1264 + if (vlan_idx == -1)
1265 + return -EINVAL;
1266 +
1267 + vmsc = esw_get_vmsc(esw, vlan_idx);
1268 + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
1269 +
1270 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
1271 + struct switch_port *p;
1272 + int port_mask = 1 << i;
1273 +
1274 + if (!(vmsc & port_mask))
1275 + continue;
1276 +
1277 + p = &val->value.ports[val->len++];
1278 + p->id = i;
1279 + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
1280 + p->flags = 0;
1281 + else
1282 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
1283 + }
1284 +
1285 + return 0;
1286 +}
1287 +
1288 +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1289 +{
1290 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1291 + int ports;
1292 + int vlan_idx = -1;
1293 + int i;
1294 +
1295 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
1296 + val->len > RT305X_ESW_NUM_PORTS)
1297 + return -EINVAL;
1298 +
1299 + /* one of the already defined vlans? */
1300 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1301 + if (esw->vlans[i].vid == val->port_vlan &&
1302 + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
1303 + vlan_idx = i;
1304 + break;
1305 + }
1306 + }
1307 +
1308 + /* select a free slot */
1309 + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
1310 + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
1311 + vlan_idx = i;
1312 + }
1313 +
1314 + /* bail if all slots are in use */
1315 + if (vlan_idx == -1)
1316 + return -EINVAL;
1317 +
1318 + ports = RT305X_ESW_PORTS_NONE;
1319 + for (i = 0; i < val->len; i++) {
1320 + struct switch_port *p = &val->value.ports[i];
1321 + int port_mask = 1 << p->id;
1322 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
1323 +
1324 + if (p->id >= RT305X_ESW_NUM_PORTS)
1325 + return -EINVAL;
1326 +
1327 + ports |= port_mask;
1328 + esw->ports[p->id].untag = untagged;
1329 + }
1330 + esw->vlans[vlan_idx].ports = ports;
1331 + if (ports == RT305X_ESW_PORTS_NONE)
1332 + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
1333 + else
1334 + esw->vlans[vlan_idx].vid = val->port_vlan;
1335 +
1336 + return 0;
1337 +}
1338 +
1339 +static const struct switch_attr esw_global[] = {
1340 + {
1341 + .type = SWITCH_TYPE_INT,
1342 + .name = "enable_vlan",
1343 + .description = "VLAN mode (1:enabled)",
1344 + .max = 1,
1345 + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
1346 + .get = esw_get_vlan_enable,
1347 + .set = esw_set_vlan_enable,
1348 + },
1349 + {
1350 + .type = SWITCH_TYPE_INT,
1351 + .name = "alternate_vlan_disable",
1352 + .description = "Use en_vlan instead of doubletag to disable"
1353 + " VLAN mode",
1354 + .max = 1,
1355 + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
1356 + .get = esw_get_alt_vlan_disable,
1357 + .set = esw_set_alt_vlan_disable,
1358 + },
1359 + {
1360 + .type = SWITCH_TYPE_INT,
1361 + .name = "bc_storm_protect",
1362 + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1363 + .max = 3,
1364 + .id = RT305X_ESW_ATTR_BC_STATUS,
1365 + .get = rt305x_esw_get_bc_status,
1366 + .set = rt305x_esw_set_bc_status,
1367 + },
1368 + {
1369 + .type = SWITCH_TYPE_INT,
1370 + .name = "led_frequency",
1371 + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1372 + .max = 3,
1373 + .id = RT305X_ESW_ATTR_LED_FREQ,
1374 + .get = rt305x_esw_get_led_freq,
1375 + .set = rt305x_esw_set_led_freq,
1376 + }
1377 +};
1378 +
1379 +static const struct switch_attr esw_port[] = {
1380 + {
1381 + .type = SWITCH_TYPE_INT,
1382 + .name = "disable",
1383 + .description = "Port state (1:disabled)",
1384 + .max = 1,
1385 + .id = RT305X_ESW_ATTR_PORT_DISABLE,
1386 + .get = esw_get_port_bool,
1387 + .set = esw_set_port_bool,
1388 + },
1389 + {
1390 + .type = SWITCH_TYPE_INT,
1391 + .name = "doubletag",
1392 + .description = "Double tagging for incoming vlan packets "
1393 + "(1:enabled)",
1394 + .max = 1,
1395 + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
1396 + .get = esw_get_port_bool,
1397 + .set = esw_set_port_bool,
1398 + },
1399 + {
1400 + .type = SWITCH_TYPE_INT,
1401 + .name = "untag",
1402 + .description = "Untag (1:strip outgoing vlan tag)",
1403 + .max = 1,
1404 + .id = RT305X_ESW_ATTR_PORT_UNTAG,
1405 + .get = esw_get_port_bool,
1406 + .set = esw_set_port_bool,
1407 + },
1408 + {
1409 + .type = SWITCH_TYPE_INT,
1410 + .name = "led",
1411 + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1412 + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1413 + " 8:100mact, 10:blink, 11:off, 12:on)",
1414 + .max = 15,
1415 + .id = RT305X_ESW_ATTR_PORT_LED,
1416 + .get = esw_get_port_led,
1417 + .set = esw_set_port_led,
1418 + },
1419 + {
1420 + .type = SWITCH_TYPE_INT,
1421 + .name = "lan",
1422 + .description = "HW port group (0:wan, 1:lan)",
1423 + .max = 1,
1424 + .id = RT305X_ESW_ATTR_PORT_LAN,
1425 + .get = esw_get_port_bool,
1426 + },
1427 + {
1428 + .type = SWITCH_TYPE_INT,
1429 + .name = "recv_bad",
1430 + .description = "Receive bad packet counter",
1431 + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
1432 + .get = esw_get_port_recv_badgood,
1433 + },
1434 + {
1435 + .type = SWITCH_TYPE_INT,
1436 + .name = "recv_good",
1437 + .description = "Receive good packet counter",
1438 + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
1439 + .get = esw_get_port_recv_badgood,
1440 + },
1441 + {
1442 + .type = SWITCH_TYPE_INT,
1443 + .name = "tr_bad",
1444 +
1445 + .description = "Transmit bad packet counter. rt5350 only",
1446 + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
1447 + .get = esw_get_port_tr_badgood,
1448 + },
1449 + {
1450 + .type = SWITCH_TYPE_INT,
1451 + .name = "tr_good",
1452 +
1453 + .description = "Transmit good packet counter. rt5350 only",
1454 + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
1455 + .get = esw_get_port_tr_badgood,
1456 + },
1457 +};
1458 +
1459 +static const struct switch_attr esw_vlan[] = {
1460 +};
1461 +
1462 +static const struct switch_dev_ops esw_ops = {
1463 + .attr_global = {
1464 + .attr = esw_global,
1465 + .n_attr = ARRAY_SIZE(esw_global),
1466 + },
1467 + .attr_port = {
1468 + .attr = esw_port,
1469 + .n_attr = ARRAY_SIZE(esw_port),
1470 + },
1471 + .attr_vlan = {
1472 + .attr = esw_vlan,
1473 + .n_attr = ARRAY_SIZE(esw_vlan),
1474 + },
1475 + .get_vlan_ports = esw_get_vlan_ports,
1476 + .set_vlan_ports = esw_set_vlan_ports,
1477 + .get_port_pvid = esw_get_port_pvid,
1478 + .set_port_pvid = esw_set_port_pvid,
1479 + .get_port_link = esw_get_port_link,
1480 + .apply_config = esw_apply_config,
1481 + .reset_switch = esw_reset_switch,
1482 +};
1483 +
1484 +static struct rt305x_esw_platform_data rt3050_esw_data = {
1485 + /* All ports are LAN ports. */
1486 + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
1487 + .reg_initval_fct2 = 0x00d6500c,
1488 + /*
1489 + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1490 + * turbo mii off, rgmi 3.3v off
1491 + * port5: disabled
1492 + * port6: enabled, gige, full-duplex, rx/tx-flow-control
1493 + */
1494 + .reg_initval_fpa2 = 0x3f502b28,
1495 +};
1496 +
1497 +static const struct of_device_id ralink_esw_match[] = {
1498 + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
1499 + {},
1500 +};
1501 +MODULE_DEVICE_TABLE(of, ralink_esw_match);
1502 +
1503 +static int esw_probe(struct platform_device *pdev)
1504 +{
1505 + struct device_node *np = pdev->dev.of_node;
1506 + const struct rt305x_esw_platform_data *pdata;
1507 + const __be32 *port_map, *reg_init;
1508 + struct rt305x_esw *esw;
1509 + struct switch_dev *swdev;
1510 + struct resource *res, *irq;
1511 + int err;
1512 +
1513 + pdata = pdev->dev.platform_data;
1514 + if (!pdata) {
1515 + const struct of_device_id *match;
1516 + match = of_match_device(ralink_esw_match, &pdev->dev);
1517 + if (match)
1518 + pdata = (struct rt305x_esw_platform_data *) match->data;
1519 + }
1520 + if (!pdata)
1521 + return -EINVAL;
1522 +
1523 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1524 + if (!res) {
1525 + dev_err(&pdev->dev, "no memory resource found\n");
1526 + return -ENOMEM;
1527 + }
1528 +
1529 + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1530 + if (!irq) {
1531 + dev_err(&pdev->dev, "no irq resource found\n");
1532 + return -ENOMEM;
1533 + }
1534 +
1535 + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
1536 + if (!esw) {
1537 + dev_err(&pdev->dev, "no memory for private data\n");
1538 + return -ENOMEM;
1539 + }
1540 +
1541 + esw->dev = &pdev->dev;
1542 + esw->irq = irq->start;
1543 + esw->base = ioremap(res->start, resource_size(res));
1544 + if (!esw->base) {
1545 + dev_err(&pdev->dev, "ioremap failed\n");
1546 + err = -ENOMEM;
1547 + goto free_esw;
1548 + }
1549 +
1550 + port_map = of_get_property(np, "ralink,portmap", NULL);
1551 + if (port_map)
1552 + esw->port_map = be32_to_cpu(*port_map);
1553 +
1554 + reg_init = of_get_property(np, "ralink,fct2", NULL);
1555 + if (reg_init)
1556 + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
1557 +
1558 + reg_init = of_get_property(np, "ralink,fpa2", NULL);
1559 + if (reg_init)
1560 + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
1561 +
1562 + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
1563 + if (reg_init)
1564 + esw->reg_led_polarity = be32_to_cpu(*reg_init);
1565 +
1566 + swdev = &esw->swdev;
1567 + swdev->of_node = pdev->dev.of_node;
1568 + swdev->name = "rt305x-esw";
1569 + swdev->alias = "rt305x";
1570 + swdev->cpu_port = RT305X_ESW_PORT6;
1571 + swdev->ports = RT305X_ESW_NUM_PORTS;
1572 + swdev->vlans = RT305X_ESW_NUM_VIDS;
1573 + swdev->ops = &esw_ops;
1574 +
1575 + err = register_switch(swdev, NULL);
1576 + if (err < 0) {
1577 + dev_err(&pdev->dev, "register_switch failed\n");
1578 + goto unmap_base;
1579 + }
1580 +
1581 + platform_set_drvdata(pdev, esw);
1582 +
1583 + esw->pdata = pdata;
1584 + spin_lock_init(&esw->reg_rw_lock);
1585 +
1586 + esw_hw_init(esw);
1587 +
1588 + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
1589 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
1590 + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
1591 +
1592 + return 0;
1593 +
1594 +unmap_base:
1595 + iounmap(esw->base);
1596 +free_esw:
1597 + kfree(esw);
1598 + return err;
1599 +}
1600 +
1601 +static int esw_remove(struct platform_device *pdev)
1602 +{
1603 + struct rt305x_esw *esw;
1604 +
1605 + esw = platform_get_drvdata(pdev);
1606 + if (esw) {
1607 + unregister_switch(&esw->swdev);
1608 + platform_set_drvdata(pdev, NULL);
1609 + iounmap(esw->base);
1610 + kfree(esw);
1611 + }
1612 +
1613 + return 0;
1614 +}
1615 +
1616 +static struct platform_driver esw_driver = {
1617 + .probe = esw_probe,
1618 + .remove = esw_remove,
1619 + .driver = {
1620 + .name = "rt305x-esw",
1621 + .owner = THIS_MODULE,
1622 + .of_match_table = ralink_esw_match,
1623 + },
1624 +};
1625 +
1626 +int __init rtesw_init(void)
1627 +{
1628 + return platform_driver_register(&esw_driver);
1629 +}
1630 +
1631 +void rtesw_exit(void)
1632 +{
1633 + platform_driver_unregister(&esw_driver);
1634 +}
1635 --- /dev/null
1636 +++ b/drivers/net/ethernet/ralink/esw_rt3052.h
1637 @@ -0,0 +1,32 @@
1638 +/*
1639 + * This program is free software; you can redistribute it and/or modify
1640 + * it under the terms of the GNU General Public License as published by
1641 + * the Free Software Foundation; version 2 of the License
1642 + *
1643 + * This program is distributed in the hope that it will be useful,
1644 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1645 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1646 + * GNU General Public License for more details.
1647 + *
1648 + * You should have received a copy of the GNU General Public License
1649 + * along with this program; if not, write to the Free Software
1650 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1651 + *
1652 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1653 + */
1654 +
1655 +#ifndef _RALINK_ESW_RT3052_H__
1656 +#define _RALINK_ESW_RT3052_H__
1657 +
1658 +#ifdef CONFIG_NET_RALINK_ESW_RT3052
1659 +
1660 +int __init rtesw_init(void);
1661 +void rtesw_exit(void);
1662 +
1663 +#else
1664 +
1665 +static inline int __init rtesw_init(void) { return 0; }
1666 +static inline void rtesw_exit(void) { }
1667 +
1668 +#endif
1669 +#endif
1670 --- /dev/null
1671 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1672 @@ -0,0 +1,566 @@
1673 +/*
1674 + * This program is free software; you can redistribute it and/or modify
1675 + * it under the terms of the GNU General Public License as published by
1676 + * the Free Software Foundation; version 2 of the License
1677 + *
1678 + * This program is distributed in the hope that it will be useful,
1679 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1680 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1681 + * GNU General Public License for more details.
1682 + *
1683 + * You should have received a copy of the GNU General Public License
1684 + * along with this program; if not, write to the Free Software
1685 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1686 + *
1687 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1688 + */
1689 +
1690 +#include <linux/module.h>
1691 +#include <linux/kernel.h>
1692 +#include <linux/types.h>
1693 +#include <linux/dma-mapping.h>
1694 +#include <linux/init.h>
1695 +#include <linux/skbuff.h>
1696 +#include <linux/etherdevice.h>
1697 +#include <linux/ethtool.h>
1698 +#include <linux/platform_device.h>
1699 +#include <linux/of_device.h>
1700 +#include <linux/clk.h>
1701 +#include <linux/of_net.h>
1702 +#include <linux/of_mdio.h>
1703 +#include <linux/of_irq.h>
1704 +#include <linux/of_address.h>
1705 +#include <linux/switch.h>
1706 +
1707 +#include <asm/mach-ralink/ralink_regs.h>
1708 +
1709 +#include "ralink_soc_eth.h"
1710 +
1711 +#include <linux/ioport.h>
1712 +#include <linux/switch.h>
1713 +#include <linux/mii.h>
1714 +
1715 +#include <ralink_regs.h>
1716 +#include <asm/mach-ralink/mt7620.h>
1717 +
1718 +#include "ralink_soc_eth.h"
1719 +#include "gsw_mt7620a.h"
1720 +#include "mt7530.h"
1721 +#include "mdio.h"
1722 +
1723 +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
1724 +
1725 +#define MT7620A_GSW_REG_PIAC 0x7004
1726 +
1727 +#define GSW_NUM_VLANS 16
1728 +#define GSW_NUM_VIDS 4096
1729 +#define GSW_NUM_PORTS 7
1730 +#define GSW_PORT6 6
1731 +
1732 +#define GSW_MDIO_ACCESS BIT(31)
1733 +#define GSW_MDIO_READ BIT(19)
1734 +#define GSW_MDIO_WRITE BIT(18)
1735 +#define GSW_MDIO_START BIT(16)
1736 +#define GSW_MDIO_ADDR_SHIFT 20
1737 +#define GSW_MDIO_REG_SHIFT 25
1738 +
1739 +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
1740 +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
1741 +#define GSW_REG_SMACCR0 0x3fE4
1742 +#define GSW_REG_SMACCR1 0x3fE8
1743 +#define GSW_REG_CKGCR 0x3ff0
1744 +
1745 +#define GSW_REG_IMR 0x7008
1746 +#define GSW_REG_ISR 0x700c
1747 +
1748 +#define SYSC_REG_CFG1 0x14
1749 +
1750 +#define PORT_IRQ_ST_CHG 0x7f
1751 +
1752 +#define SYSCFG1 0x14
1753 +
1754 +#define ESW_PHY_POLLING 0x7000
1755 +
1756 +#define PMCR_IPG BIT(18)
1757 +#define PMCR_MAC_MODE BIT(16)
1758 +#define PMCR_FORCE BIT(15)
1759 +#define PMCR_TX_EN BIT(14)
1760 +#define PMCR_RX_EN BIT(13)
1761 +#define PMCR_BACKOFF BIT(9)
1762 +#define PMCR_BACKPRES BIT(8)
1763 +#define PMCR_RX_FC BIT(5)
1764 +#define PMCR_TX_FC BIT(4)
1765 +#define PMCR_SPEED(_x) (_x << 2)
1766 +#define PMCR_DUPLEX BIT(1)
1767 +#define PMCR_LINK BIT(0)
1768 +
1769 +#define PHY_AN_EN BIT(31)
1770 +#define PHY_PRE_EN BIT(30)
1771 +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
1772 +
1773 +enum {
1774 + /* Global attributes. */
1775 + GSW_ATTR_ENABLE_VLAN,
1776 + /* Port attributes. */
1777 + GSW_ATTR_PORT_UNTAG,
1778 +};
1779 +
1780 +enum {
1781 + PORT4_EPHY = 0,
1782 + PORT4_EXT,
1783 +};
1784 +
1785 +struct mt7620_gsw {
1786 + struct device *dev;
1787 + void __iomem *base;
1788 + int irq;
1789 + int port4;
1790 + long unsigned int autopoll;
1791 +};
1792 +
1793 +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
1794 +{
1795 + iowrite32(val, gsw->base + reg);
1796 +}
1797 +
1798 +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
1799 +{
1800 + return ioread32(gsw->base + reg);
1801 +}
1802 +
1803 +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
1804 +{
1805 + unsigned long t_start = jiffies;
1806 +
1807 + while (1) {
1808 + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
1809 + return 0;
1810 + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
1811 + break;
1812 + }
1813 + }
1814 +
1815 + printk(KERN_ERR "mdio: MDIO timeout\n");
1816 + return -1;
1817 +}
1818 +
1819 +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
1820 + u32 write_data)
1821 +{
1822 + if (mt7620_mii_busy_wait(gsw))
1823 + return -1;
1824 +
1825 + write_data &= 0xffff;
1826 +
1827 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
1828 + (phy_register << GSW_MDIO_REG_SHIFT) |
1829 + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
1830 + MT7620A_GSW_REG_PIAC);
1831 +
1832 + if (mt7620_mii_busy_wait(gsw))
1833 + return -1;
1834 +
1835 + return 0;
1836 +}
1837 +
1838 +static u32 _mt7620_mii_read(struct mt7620_gsw *gsw, int phy_addr, int phy_reg)
1839 +{
1840 + u32 d;
1841 +
1842 + if (mt7620_mii_busy_wait(gsw))
1843 + return 0xffff;
1844 +
1845 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
1846 + (phy_reg << GSW_MDIO_REG_SHIFT) |
1847 + (phy_addr << GSW_MDIO_ADDR_SHIFT),
1848 + MT7620A_GSW_REG_PIAC);
1849 +
1850 + if (mt7620_mii_busy_wait(gsw))
1851 + return 0xffff;
1852 +
1853 + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
1854 +
1855 + return d;
1856 +}
1857 +
1858 +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
1859 +{
1860 + struct fe_priv *priv = bus->priv;
1861 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1862 +
1863 + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
1864 +}
1865 +
1866 +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
1867 +{
1868 + struct fe_priv *priv = bus->priv;
1869 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1870 +
1871 + return _mt7620_mii_read(gsw, phy_addr, phy_reg);
1872 +}
1873 +
1874 +static unsigned char *fe_speed_str(int speed)
1875 +{
1876 + switch (speed) {
1877 + case 2:
1878 + case SPEED_1000:
1879 + return "1000";
1880 + case 1:
1881 + case SPEED_100:
1882 + return "100";
1883 + case 0:
1884 + case SPEED_10:
1885 + return "10";
1886 + }
1887 +
1888 + return "? ";
1889 +}
1890 +
1891 +int mt7620a_has_carrier(struct fe_priv *priv)
1892 +{
1893 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1894 + int i;
1895 +
1896 + for (i = 0; i < GSW_PORT6; i++)
1897 + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
1898 + return 1;
1899 + return 0;
1900 +}
1901 +
1902 +static void mt7620a_handle_carrier(struct fe_priv *priv)
1903 +{
1904 + if (!priv->phy)
1905 + return;
1906 +
1907 + if (mt7620a_has_carrier(priv))
1908 + netif_carrier_on(priv->netdev);
1909 + else
1910 + netif_carrier_off(priv->netdev);
1911 +}
1912 +
1913 +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
1914 +{
1915 + if (priv->link[port])
1916 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1917 + port, fe_speed_str(priv->phy->speed[port]),
1918 + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
1919 + else
1920 + netdev_info(priv->netdev, "port %d link down\n", port);
1921 + mt7620a_handle_carrier(priv);
1922 +}
1923 +
1924 +static irqreturn_t gsw_interrupt(int irq, void *_priv)
1925 +{
1926 + struct fe_priv *priv = (struct fe_priv *) _priv;
1927 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1928 + u32 status;
1929 + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
1930 +
1931 + status = gsw_r32(gsw, GSW_REG_ISR);
1932 + if (status & PORT_IRQ_ST_CHG)
1933 + for (i = 0; i <= max; i++) {
1934 + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
1935 + int link = status & 0x1;
1936 +
1937 + if (link != priv->link[i]) {
1938 + if (link)
1939 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1940 + i, fe_speed_str((status >> 2) & 3),
1941 + (status & 0x2) ? "Full" : "Half");
1942 + else
1943 + netdev_info(priv->netdev, "port %d link down\n", i);
1944 + }
1945 +
1946 + priv->link[i] = link;
1947 + }
1948 + mt7620a_handle_carrier(priv);
1949 +
1950 + gsw_w32(gsw, status, GSW_REG_ISR);
1951 +
1952 + return IRQ_HANDLED;
1953 +}
1954 +
1955 +static int mt7620_is_bga(void)
1956 +{
1957 + u32 bga = rt_sysc_r32(0x0c);
1958 +
1959 + return (bga >> 16) & 1;
1960 +}
1961 +
1962 +static void gsw_auto_poll(struct mt7620_gsw *gsw)
1963 +{
1964 + int phy;
1965 + int lsb = -1, msb = 0;
1966 +
1967 + for_each_set_bit(phy, &gsw->autopoll, 32) {
1968 + if (lsb < 0)
1969 + lsb = phy;
1970 + msb = phy;
1971 + }
1972 +
1973 + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
1974 +}
1975 +
1976 +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
1977 +{
1978 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1979 + const __be32 *_id = of_get_property(np, "reg", NULL);
1980 + int phy_mode, size, id;
1981 + int shift = 12;
1982 + u32 val, mask = 0;
1983 + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
1984 +
1985 + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
1986 + if (_id)
1987 + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
1988 + else
1989 + pr_err("%s: invalid port id\n", np->name);
1990 + return;
1991 + }
1992 +
1993 + id = be32_to_cpu(*_id);
1994 +
1995 + if (id == 4)
1996 + shift = 14;
1997 +
1998 + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
1999 + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
2000 + pr_err("%s: invalid fixed link property\n", np->name);
2001 + priv->phy->phy_fixed[id] = NULL;
2002 + return;
2003 + }
2004 +
2005 + phy_mode = of_get_phy_mode(np);
2006 + switch (phy_mode) {
2007 + case PHY_INTERFACE_MODE_RGMII:
2008 + mask = 0;
2009 + break;
2010 + case PHY_INTERFACE_MODE_MII:
2011 + mask = 1;
2012 + break;
2013 + case PHY_INTERFACE_MODE_RMII:
2014 + mask = 2;
2015 + break;
2016 + default:
2017 + dev_err(priv->device, "port %d - invalid phy mode\n", id);
2018 + return;
2019 + }
2020 +
2021 + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
2022 + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
2023 + return;
2024 +
2025 + val = rt_sysc_r32(SYSCFG1);
2026 + val &= ~(3 << shift);
2027 + val |= mask << shift;
2028 + rt_sysc_w32(val, SYSCFG1);
2029 +
2030 + if (priv->phy->phy_fixed[id]) {
2031 + const __be32 *link = priv->phy->phy_fixed[id];
2032 + int tx_fc, rx_fc;
2033 + u32 val = 0;
2034 +
2035 + priv->phy->speed[id] = be32_to_cpup(link++);
2036 + tx_fc = be32_to_cpup(link++);
2037 + rx_fc = be32_to_cpup(link++);
2038 + priv->phy->duplex[id] = be32_to_cpup(link++);
2039 + priv->link[id] = 1;
2040 +
2041 + switch (priv->phy->speed[id]) {
2042 + case SPEED_10:
2043 + val = 0;
2044 + break;
2045 + case SPEED_100:
2046 + val = 1;
2047 + break;
2048 + case SPEED_1000:
2049 + val = 2;
2050 + break;
2051 + default:
2052 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
2053 + priv->phy->phy_fixed[id] = 0;
2054 + return;
2055 + }
2056 + val = PMCR_SPEED(val);
2057 + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2058 + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
2059 + if (tx_fc)
2060 + val |= PMCR_TX_FC;
2061 + if (rx_fc)
2062 + val |= PMCR_RX_FC;
2063 + if (priv->phy->duplex[id])
2064 + val |= PMCR_DUPLEX;
2065 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2066 + dev_info(priv->device, "using fixed link parameters\n");
2067 + return;
2068 + }
2069 +
2070 + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
2071 + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2072 + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
2073 +
2074 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2075 + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
2076 + gsw->autopoll |= BIT(id);
2077 + gsw_auto_poll(gsw);
2078 + return;
2079 + }
2080 +}
2081 +
2082 +static void gsw_hw_init(struct mt7620_gsw *gsw)
2083 +{
2084 + u32 is_BGA = mt7620_is_bga();
2085 +
2086 + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
2087 + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
2088 +
2089 + /*correct PHY setting L3.0 BGA*/
2090 + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
2091 +
2092 + _mt7620_mii_write(gsw, 1, 17, 0x7444);
2093 + if (is_BGA)
2094 + _mt7620_mii_write(gsw, 1, 19, 0x0114);
2095 + else
2096 + _mt7620_mii_write(gsw, 1, 19, 0x0117);
2097 +
2098 + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
2099 + _mt7620_mii_write(gsw, 1, 25, 0x6212);
2100 + _mt7620_mii_write(gsw, 1, 26, 0x0777);
2101 + _mt7620_mii_write(gsw, 1, 29, 0x4000);
2102 + _mt7620_mii_write(gsw, 1, 28, 0xc077);
2103 + _mt7620_mii_write(gsw, 1, 24, 0x0000);
2104 +
2105 + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
2106 + _mt7620_mii_write(gsw, 1, 17, 0x4838);
2107 +
2108 + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
2109 + if (is_BGA) {
2110 + _mt7620_mii_write(gsw, 1, 21, 0x0515);
2111 + _mt7620_mii_write(gsw, 1, 22, 0x0053);
2112 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2113 + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
2114 + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
2115 + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
2116 + } else {
2117 + _mt7620_mii_write(gsw, 1, 21, 0x0517);
2118 + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
2119 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2120 + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
2121 + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
2122 + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
2123 + }
2124 + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
2125 + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
2126 +
2127 + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
2128 + _mt7620_mii_write(gsw, 0, 30, 0xa000);
2129 + _mt7620_mii_write(gsw, 1, 30, 0xa000);
2130 + _mt7620_mii_write(gsw, 2, 30, 0xa000);
2131 + _mt7620_mii_write(gsw, 3, 30, 0xa000);
2132 +
2133 + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
2134 + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
2135 + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
2136 + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
2137 + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
2138 + _mt7620_mii_write(gsw, 0, 16, 0x1111);
2139 + _mt7620_mii_write(gsw, 1, 16, 0x1010);
2140 + _mt7620_mii_write(gsw, 2, 16, 0x1515);
2141 + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
2142 +
2143 + /* CPU Port6 Force Link 1G, FC ON */
2144 + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
2145 + /* Set Port6 CPU Port */
2146 + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
2147 +
2148 + /* setup port 4 */
2149 + if (gsw->port4 == PORT4_EPHY) {
2150 + u32 val = rt_sysc_r32(SYSCFG1);
2151 + val |= 3 << 14;
2152 + rt_sysc_w32(val, SYSCFG1);
2153 + _mt7620_mii_write(gsw, 4, 30, 0xa000);
2154 + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
2155 + _mt7620_mii_write(gsw, 4, 16, 0x1313);
2156 + pr_info("gsw: setting port4 to ephy mode\n");
2157 + }
2158 +}
2159 +
2160 +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
2161 +{
2162 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2163 + unsigned long flags;
2164 +
2165 + spin_lock_irqsave(&priv->page_lock, flags);
2166 + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
2167 + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
2168 + GSW_REG_SMACCR0);
2169 + spin_unlock_irqrestore(&priv->page_lock, flags);
2170 +}
2171 +
2172 +static struct of_device_id gsw_match[] = {
2173 + { .compatible = "ralink,mt7620a-gsw" },
2174 + {}
2175 +};
2176 +
2177 +int mt7620_gsw_config(struct fe_priv *priv)
2178 +{
2179 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2180 +
2181 + /* is the mt7530 internal or external */
2182 + if ((_mt7620_mii_read(gsw, 0x1f, 2) == 1) && (_mt7620_mii_read(gsw, 0x1f, 3) == 0xbeef))
2183 + mt7530_probe(priv->device, NULL, priv->mii_bus);
2184 + else
2185 + mt7530_probe(priv->device, gsw->base, NULL);
2186 +
2187 + return 0;
2188 +}
2189 +
2190 +int mt7620_gsw_probe(struct fe_priv *priv)
2191 +{
2192 + struct mt7620_gsw *gsw;
2193 + struct device_node *np;
2194 + const char *port4 = NULL;
2195 +
2196 + np = of_find_matching_node(NULL, gsw_match);
2197 + if (!np) {
2198 + dev_err(priv->device, "no gsw node found\n");
2199 + return -EINVAL;
2200 + }
2201 + np = of_node_get(np);
2202 +
2203 + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
2204 + if (!gsw) {
2205 + dev_err(priv->device, "no gsw memory for private data\n");
2206 + return -ENOMEM;
2207 + }
2208 +
2209 + gsw->irq = irq_of_parse_and_map(np, 0);
2210 + if (!gsw->irq) {
2211 + dev_err(priv->device, "no gsw irq resource found\n");
2212 + return -ENOMEM;
2213 + }
2214 +
2215 + gsw->base = of_iomap(np, 0);
2216 + if (!gsw->base) {
2217 + dev_err(priv->device, "gsw ioremap failed\n");
2218 + return -ENOMEM;
2219 + }
2220 +
2221 + gsw->dev = priv->device;
2222 + priv->soc->swpriv = gsw;
2223 +
2224 + of_property_read_string(np, "ralink,port4", &port4);
2225 + if (port4 && !strcmp(port4, "ephy"))
2226 + gsw->port4 = PORT4_EPHY;
2227 + else if (port4 && !strcmp(port4, "gmac"))
2228 + gsw->port4 = PORT4_EXT;
2229 + else
2230 + WARN_ON(port4);
2231 +
2232 + gsw_hw_init(gsw);
2233 +
2234 + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
2235 + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
2236 +
2237 + return 0;
2238 +}
2239 --- /dev/null
2240 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2241 @@ -0,0 +1,30 @@
2242 +/*
2243 + * This program is free software; you can redistribute it and/or modify
2244 + * it under the terms of the GNU General Public License as published by
2245 + * the Free Software Foundation; version 2 of the License
2246 + *
2247 + * This program is distributed in the hope that it will be useful,
2248 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2249 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2250 + * GNU General Public License for more details.
2251 + *
2252 + * You should have received a copy of the GNU General Public License
2253 + * along with this program; if not, write to the Free Software
2254 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2255 + *
2256 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2257 + */
2258 +
2259 +#ifndef _RALINK_GSW_MT7620_H__
2260 +#define _RALINK_GSW_MT7620_H__
2261 +
2262 +extern int mt7620_gsw_config(struct fe_priv *priv);
2263 +extern int mt7620_gsw_probe(struct fe_priv *priv);
2264 +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
2265 +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2266 +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2267 +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
2268 +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
2269 +extern int mt7620a_has_carrier(struct fe_priv *priv);
2270 +
2271 +#endif
2272 --- /dev/null
2273 +++ b/drivers/net/ethernet/ralink/mdio.c
2274 @@ -0,0 +1,244 @@
2275 +/*
2276 + * This program is free software; you can redistribute it and/or modify
2277 + * it under the terms of the GNU General Public License as published by
2278 + * the Free Software Foundation; version 2 of the License
2279 + *
2280 + * This program is distributed in the hope that it will be useful,
2281 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2282 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2283 + * GNU General Public License for more details.
2284 + *
2285 + * You should have received a copy of the GNU General Public License
2286 + * along with this program; if not, write to the Free Software
2287 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2288 + *
2289 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2290 + */
2291 +
2292 +#include <linux/module.h>
2293 +#include <linux/kernel.h>
2294 +#include <linux/types.h>
2295 +#include <linux/dma-mapping.h>
2296 +#include <linux/init.h>
2297 +#include <linux/skbuff.h>
2298 +#include <linux/etherdevice.h>
2299 +#include <linux/ethtool.h>
2300 +#include <linux/platform_device.h>
2301 +#include <linux/phy.h>
2302 +#include <linux/of_device.h>
2303 +#include <linux/clk.h>
2304 +#include <linux/of_net.h>
2305 +#include <linux/of_mdio.h>
2306 +
2307 +#include "ralink_soc_eth.h"
2308 +#include "mdio.h"
2309 +
2310 +static int fe_mdio_reset(struct mii_bus *bus)
2311 +{
2312 + /* TODO */
2313 + return 0;
2314 +}
2315 +
2316 +static void fe_phy_link_adjust(struct net_device *dev)
2317 +{
2318 + struct fe_priv *priv = netdev_priv(dev);
2319 + unsigned long flags;
2320 + int i;
2321 +
2322 + spin_lock_irqsave(&priv->phy->lock, flags);
2323 + for (i = 0; i < 8; i++) {
2324 + if (priv->phy->phy_node[i]) {
2325 + struct phy_device *phydev = priv->phy->phy[i];
2326 + int status_change = 0;
2327 +
2328 + if (phydev->link)
2329 + if (priv->phy->duplex[i] != phydev->duplex ||
2330 + priv->phy->speed[i] != phydev->speed)
2331 + status_change = 1;
2332 +
2333 + if (phydev->link != priv->link[i])
2334 + status_change = 1;
2335 +
2336 + switch (phydev->speed) {
2337 + case SPEED_1000:
2338 + case SPEED_100:
2339 + case SPEED_10:
2340 + priv->link[i] = phydev->link;
2341 + priv->phy->duplex[i] = phydev->duplex;
2342 + priv->phy->speed[i] = phydev->speed;
2343 +
2344 + if (status_change && priv->soc->mdio_adjust_link)
2345 + priv->soc->mdio_adjust_link(priv, i);
2346 + break;
2347 + }
2348 + }
2349 + }
2350 +}
2351 +
2352 +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
2353 +{
2354 + const __be32 *_port = NULL;
2355 + struct phy_device *phydev;
2356 + int phy_mode, port;
2357 +
2358 + _port = of_get_property(phy_node, "reg", NULL);
2359 +
2360 + if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
2361 + pr_err("%s: invalid port id\n", phy_node->name);
2362 + return -EINVAL;
2363 + }
2364 + port = be32_to_cpu(*_port);
2365 + phy_mode = of_get_phy_mode(phy_node);
2366 + if (phy_mode < 0) {
2367 + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
2368 + priv->phy->phy_node[port] = NULL;
2369 + return -EINVAL;
2370 + }
2371 +
2372 + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
2373 + 0, phy_mode);
2374 + if (IS_ERR(phydev)) {
2375 + dev_err(priv->device, "could not connect to PHY\n");
2376 + priv->phy->phy_node[port] = NULL;
2377 + return PTR_ERR(phydev);
2378 + }
2379 +
2380 + phydev->supported &= PHY_GBIT_FEATURES;
2381 + phydev->advertising = phydev->supported;
2382 + phydev->no_auto_carrier_off = 1;
2383 +
2384 + dev_info(priv->device,
2385 + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
2386 + port, dev_name(&phydev->dev), phydev->phy_id,
2387 + phydev->drv->name);
2388 +
2389 + priv->phy->phy[port] = phydev;
2390 + priv->link[port] = 0;
2391 +
2392 + return 0;
2393 +}
2394 +
2395 +static int fe_phy_connect(struct fe_priv *priv)
2396 +{
2397 + return 0;
2398 +}
2399 +
2400 +static void fe_phy_disconnect(struct fe_priv *priv)
2401 +{
2402 + unsigned long flags;
2403 + int i;
2404 +
2405 + for (i = 0; i < 8; i++)
2406 + if (priv->phy->phy_fixed[i]) {
2407 + spin_lock_irqsave(&priv->phy->lock, flags);
2408 + priv->link[i] = 0;
2409 + if (priv->soc->mdio_adjust_link)
2410 + priv->soc->mdio_adjust_link(priv, i);
2411 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2412 + } else if (priv->phy->phy[i]) {
2413 + phy_disconnect(priv->phy->phy[i]);
2414 + }
2415 +}
2416 +
2417 +static void fe_phy_start(struct fe_priv *priv)
2418 +{
2419 + unsigned long flags;
2420 + int i;
2421 +
2422 + for (i = 0; i < 8; i++) {
2423 + if (priv->phy->phy_fixed[i]) {
2424 + spin_lock_irqsave(&priv->phy->lock, flags);
2425 + priv->link[i] = 1;
2426 + if (priv->soc->mdio_adjust_link)
2427 + priv->soc->mdio_adjust_link(priv, i);
2428 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2429 + } else if (priv->phy->phy[i]) {
2430 + phy_start(priv->phy->phy[i]);
2431 + }
2432 + }
2433 +}
2434 +
2435 +static void fe_phy_stop(struct fe_priv *priv)
2436 +{
2437 + unsigned long flags;
2438 + int i;
2439 +
2440 + for (i = 0; i < 8; i++)
2441 + if (priv->phy->phy_fixed[i]) {
2442 + spin_lock_irqsave(&priv->phy->lock, flags);
2443 + priv->link[i] = 0;
2444 + if (priv->soc->mdio_adjust_link)
2445 + priv->soc->mdio_adjust_link(priv, i);
2446 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2447 + } else if (priv->phy->phy[i]) {
2448 + phy_stop(priv->phy->phy[i]);
2449 + }
2450 +}
2451 +
2452 +static struct fe_phy phy_ralink = {
2453 + .connect = fe_phy_connect,
2454 + .disconnect = fe_phy_disconnect,
2455 + .start = fe_phy_start,
2456 + .stop = fe_phy_stop,
2457 +};
2458 +
2459 +int fe_mdio_init(struct fe_priv *priv)
2460 +{
2461 + struct device_node *mii_np;
2462 + int err;
2463 +
2464 + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
2465 + return 0;
2466 +
2467 + spin_lock_init(&phy_ralink.lock);
2468 + priv->phy = &phy_ralink;
2469 +
2470 + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
2471 + if (!mii_np) {
2472 + dev_err(priv->device, "no %s child node found", "mdio-bus");
2473 + return -ENODEV;
2474 + }
2475 +
2476 + if (!of_device_is_available(mii_np)) {
2477 + err = 0;
2478 + goto err_put_node;
2479 + }
2480 +
2481 + priv->mii_bus = mdiobus_alloc();
2482 + if (priv->mii_bus == NULL) {
2483 + err = -ENOMEM;
2484 + goto err_put_node;
2485 + }
2486 +
2487 + priv->mii_bus->name = "mdio";
2488 + priv->mii_bus->read = priv->soc->mdio_read;
2489 + priv->mii_bus->write = priv->soc->mdio_write;
2490 + priv->mii_bus->reset = fe_mdio_reset;
2491 + priv->mii_bus->irq = priv->mii_irq;
2492 + priv->mii_bus->priv = priv;
2493 + priv->mii_bus->parent = priv->device;
2494 +
2495 + snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
2496 + err = of_mdiobus_register(priv->mii_bus, mii_np);
2497 + if (err)
2498 + goto err_free_bus;
2499 +
2500 + return 0;
2501 +
2502 +err_free_bus:
2503 + kfree(priv->mii_bus);
2504 +err_put_node:
2505 + of_node_put(mii_np);
2506 + priv->mii_bus = NULL;
2507 + return err;
2508 +}
2509 +
2510 +void fe_mdio_cleanup(struct fe_priv *priv)
2511 +{
2512 + if (!priv->mii_bus)
2513 + return;
2514 +
2515 + mdiobus_unregister(priv->mii_bus);
2516 + of_node_put(priv->mii_bus->dev.of_node);
2517 + kfree(priv->mii_bus);
2518 +}
2519 --- /dev/null
2520 +++ b/drivers/net/ethernet/ralink/mdio.h
2521 @@ -0,0 +1,29 @@
2522 +/*
2523 + * This program is free software; you can redistribute it and/or modify
2524 + * it under the terms of the GNU General Public License as published by
2525 + * the Free Software Foundation; version 2 of the License
2526 + *
2527 + * This program is distributed in the hope that it will be useful,
2528 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2529 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2530 + * GNU General Public License for more details.
2531 + *
2532 + * You should have received a copy of the GNU General Public License
2533 + * along with this program; if not, write to the Free Software
2534 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2535 + *
2536 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2537 + */
2538 +
2539 +#ifndef _RALINK_MDIO_H__
2540 +#define _RALINK_MDIO_H__
2541 +
2542 +#ifdef CONFIG_NET_RALINK_MDIO
2543 +extern int fe_mdio_init(struct fe_priv *priv);
2544 +extern void fe_mdio_cleanup(struct fe_priv *priv);
2545 +extern int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node);
2546 +#else
2547 +static inline int fe_mdio_init(struct fe_priv *priv) { return 0; }
2548 +static inline void fe_mdio_cleanup(struct fe_priv *priv) {}
2549 +#endif
2550 +#endif
2551 --- /dev/null
2552 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.c
2553 @@ -0,0 +1,232 @@
2554 +/*
2555 + * This program is free software; you can redistribute it and/or modify
2556 + * it under the terms of the GNU General Public License as published by
2557 + * the Free Software Foundation; version 2 of the License
2558 + *
2559 + * This program is distributed in the hope that it will be useful,
2560 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2561 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2562 + * GNU General Public License for more details.
2563 + *
2564 + * You should have received a copy of the GNU General Public License
2565 + * along with this program; if not, write to the Free Software
2566 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2567 + *
2568 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2569 + */
2570 +
2571 +#include <linux/module.h>
2572 +#include <linux/kernel.h>
2573 +#include <linux/types.h>
2574 +#include <linux/dma-mapping.h>
2575 +#include <linux/init.h>
2576 +#include <linux/skbuff.h>
2577 +#include <linux/etherdevice.h>
2578 +#include <linux/ethtool.h>
2579 +#include <linux/platform_device.h>
2580 +#include <linux/phy.h>
2581 +#include <linux/of_device.h>
2582 +#include <linux/clk.h>
2583 +#include <linux/of_net.h>
2584 +#include <linux/of_mdio.h>
2585 +
2586 +#include "ralink_soc_eth.h"
2587 +#include "mdio_rt2880.h"
2588 +#include "mdio.h"
2589 +
2590 +#define FE_MDIO_RETRY 1000
2591 +
2592 +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
2593 +{
2594 + switch (priv->phy->speed[0]) {
2595 + case SPEED_1000:
2596 + return "1000";
2597 + case SPEED_100:
2598 + return "100";
2599 + case SPEED_10:
2600 + return "10";
2601 + }
2602 +
2603 + return "?";
2604 +}
2605 +
2606 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
2607 +{
2608 + u32 mdio_cfg;
2609 +
2610 + if (!priv->link[0]) {
2611 + netif_carrier_off(priv->netdev);
2612 + netdev_info(priv->netdev, "link down\n");
2613 + return;
2614 + }
2615 +
2616 + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
2617 + FE_MDIO_CFG_RX_CLK_SKEW_200 |
2618 + FE_MDIO_CFG_GP1_FRC_EN;
2619 +
2620 + if (priv->phy->duplex[0] == DUPLEX_FULL)
2621 + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
2622 +
2623 + if (priv->phy->tx_fc[0])
2624 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
2625 +
2626 + if (priv->phy->rx_fc[0])
2627 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
2628 +
2629 + switch (priv->phy->speed[0]) {
2630 + case SPEED_10:
2631 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
2632 + break;
2633 + case SPEED_100:
2634 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
2635 + break;
2636 + case SPEED_1000:
2637 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
2638 + break;
2639 + default:
2640 + BUG();
2641 + }
2642 +
2643 + fe_w32(mdio_cfg, FE_MDIO_CFG);
2644 +
2645 + netif_carrier_on(priv->netdev);
2646 + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
2647 + rt2880_speed_str(priv),
2648 + (DUPLEX_FULL == priv->phy->duplex[0]) ? "Full" : "Half");
2649 +}
2650 +
2651 +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
2652 +{
2653 + int retries;
2654 +
2655 + retries = FE_MDIO_RETRY;
2656 + while (1) {
2657 + u32 t;
2658 +
2659 + t = fe_r32(FE_MDIO_ACCESS);
2660 + if ((t & (0x1 << 31)) == 0)
2661 + return 0;
2662 +
2663 + if (retries-- == 0)
2664 + break;
2665 +
2666 + udelay(1);
2667 + }
2668 +
2669 + dev_err(priv->device, "MDIO operation timed out\n");
2670 + return -ETIMEDOUT;
2671 +}
2672 +
2673 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
2674 +{
2675 + struct fe_priv *priv = bus->priv;
2676 + int err;
2677 + u32 t;
2678 +
2679 + err = rt2880_mdio_wait_ready(priv);
2680 + if (err)
2681 + return 0xffff;
2682 +
2683 + t = (phy_addr << 24) | (phy_reg << 16);
2684 + fe_w32(t, FE_MDIO_ACCESS);
2685 + t |= (1 << 31);
2686 + fe_w32(t, FE_MDIO_ACCESS);
2687 +
2688 + err = rt2880_mdio_wait_ready(priv);
2689 + if (err)
2690 + return 0xffff;
2691 +
2692 + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2693 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2694 +
2695 + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
2696 +}
2697 +
2698 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
2699 +{
2700 + struct fe_priv *priv = bus->priv;
2701 + int err;
2702 + u32 t;
2703 +
2704 + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
2705 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
2706 +
2707 + err = rt2880_mdio_wait_ready(priv);
2708 + if (err)
2709 + return err;
2710 +
2711 + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
2712 + fe_w32(t, FE_MDIO_ACCESS);
2713 + t |= (1 << 31);
2714 + fe_w32(t, FE_MDIO_ACCESS);
2715 +
2716 + return rt2880_mdio_wait_ready(priv);
2717 +}
2718 +
2719 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
2720 +{
2721 + const __be32 *id = of_get_property(np, "reg", NULL);
2722 + const __be32 *link;
2723 + int size;
2724 + int phy_mode;
2725 +
2726 + if (!id || (be32_to_cpu(*id) != 0)) {
2727 + pr_err("%s: invalid port id\n", np->name);
2728 + return;
2729 + }
2730 +
2731 + priv->phy->phy_fixed[0] = of_get_property(np, "ralink,fixed-link", &size);
2732 + if (priv->phy->phy_fixed[0] && (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
2733 + pr_err("%s: invalid fixed link property\n", np->name);
2734 + priv->phy->phy_fixed[0] = NULL;
2735 + return;
2736 + }
2737 +
2738 + phy_mode = of_get_phy_mode(np);
2739 + switch (phy_mode) {
2740 + case PHY_INTERFACE_MODE_RGMII:
2741 + break;
2742 + case PHY_INTERFACE_MODE_MII:
2743 + break;
2744 + case PHY_INTERFACE_MODE_RMII:
2745 + break;
2746 + default:
2747 + if (!priv->phy->phy_fixed[0])
2748 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[0]);
2749 + break;
2750 + }
2751 +
2752 + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
2753 + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
2754 + return;
2755 +
2756 + if (priv->phy->phy_fixed[0]) {
2757 + link = priv->phy->phy_fixed[0];
2758 + priv->phy->speed[0] = be32_to_cpup(link++);
2759 + priv->phy->duplex[0] = be32_to_cpup(link++);
2760 + priv->phy->tx_fc[0] = be32_to_cpup(link++);
2761 + priv->phy->rx_fc[0] = be32_to_cpup(link++);
2762 +
2763 + priv->link[0] = 1;
2764 + switch (priv->phy->speed[0]) {
2765 + case SPEED_10:
2766 + break;
2767 + case SPEED_100:
2768 + break;
2769 + case SPEED_1000:
2770 + break;
2771 + default:
2772 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[0]);
2773 + priv->phy->phy_fixed[0] = 0;
2774 + return;
2775 + }
2776 + dev_info(priv->device, "using fixed link parameters\n");
2777 + rt2880_mdio_link_adjust(priv, 0);
2778 + return;
2779 + }
2780 + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0]) {
2781 + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
2782 + }
2783 +
2784 + return;
2785 +}
2786 --- /dev/null
2787 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.h
2788 @@ -0,0 +1,26 @@
2789 +/*
2790 + * This program is free software; you can redistribute it and/or modify
2791 + * it under the terms of the GNU General Public License as published by
2792 + * the Free Software Foundation; version 2 of the License
2793 + *
2794 + * This program is distributed in the hope that it will be useful,
2795 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2796 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2797 + * GNU General Public License for more details.
2798 + *
2799 + * You should have received a copy of the GNU General Public License
2800 + * along with this program; if not, write to the Free Software
2801 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2802 + *
2803 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2804 + */
2805 +
2806 +#ifndef _RALINK_MDIO_RT2880_H__
2807 +#define _RALINK_MDIO_RT2880_H__
2808 +
2809 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
2810 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2811 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2812 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
2813 +
2814 +#endif
2815 --- /dev/null
2816 +++ b/drivers/net/ethernet/ralink/mt7530.c
2817 @@ -0,0 +1,579 @@
2818 +/*
2819 + * This program is free software; you can redistribute it and/or
2820 + * modify it under the terms of the GNU General Public License
2821 + * as published by the Free Software Foundation; either version 2
2822 + * of the License, or (at your option) any later version.
2823 + *
2824 + * This program is distributed in the hope that it will be useful,
2825 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2826 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2827 + * GNU General Public License for more details.
2828 + *
2829 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
2830 + */
2831 +
2832 +#include <linux/if.h>
2833 +#include <linux/module.h>
2834 +#include <linux/init.h>
2835 +#include <linux/list.h>
2836 +#include <linux/if_ether.h>
2837 +#include <linux/skbuff.h>
2838 +#include <linux/netdevice.h>
2839 +#include <linux/netlink.h>
2840 +#include <linux/bitops.h>
2841 +#include <net/genetlink.h>
2842 +#include <linux/switch.h>
2843 +#include <linux/delay.h>
2844 +#include <linux/phy.h>
2845 +#include <linux/netdevice.h>
2846 +#include <linux/etherdevice.h>
2847 +#include <linux/lockdep.h>
2848 +#include <linux/workqueue.h>
2849 +#include <linux/of_device.h>
2850 +
2851 +#include "mt7530.h"
2852 +
2853 +#define MT7530_CPU_PORT 6
2854 +#define MT7530_NUM_PORTS 8
2855 +#define MT7530_NUM_VLANS 16
2856 +#define MT7530_MAX_VID 4095
2857 +#define MT7530_MIN_VID 0
2858 +
2859 +/* registers */
2860 +#define REG_ESW_VLAN_VTCR 0x90
2861 +#define REG_ESW_VLAN_VAWD1 0x94
2862 +#define REG_ESW_VLAN_VAWD2 0x98
2863 +#define REG_ESW_VLAN_VTIM(x) (0x100 + 4 * ((x) / 2))
2864 +
2865 +#define REG_ESW_VLAN_VAWD1_IVL_MAC BIT(30)
2866 +#define REG_ESW_VLAN_VAWD1_VTAG_EN BIT(28)
2867 +#define REG_ESW_VLAN_VAWD1_VALID BIT(0)
2868 +
2869 +/* vlan egress mode */
2870 +enum {
2871 + ETAG_CTRL_UNTAG = 0,
2872 + ETAG_CTRL_TAG = 2,
2873 + ETAG_CTRL_SWAP = 1,
2874 + ETAG_CTRL_STACK = 3,
2875 +};
2876 +
2877 +#define REG_ESW_PORT_PCR(x) (0x2004 | ((x) << 8))
2878 +#define REG_ESW_PORT_PVC(x) (0x2010 | ((x) << 8))
2879 +#define REG_ESW_PORT_PPBV1(x) (0x2014 | ((x) << 8))
2880 +enum {
2881 + /* Global attributes. */
2882 + MT7530_ATTR_ENABLE_VLAN,
2883 +};
2884 +
2885 +struct mt7530_port_entry {
2886 + u16 pvid;
2887 +};
2888 +
2889 +struct mt7530_vlan_entry {
2890 + u16 vid;
2891 + u8 member;
2892 + u8 etags;
2893 +};
2894 +
2895 +struct mt7530_priv {
2896 + void __iomem *base;
2897 + struct mii_bus *bus;
2898 + struct switch_dev swdev;
2899 +
2900 + bool global_vlan_enable;
2901 + struct mt7530_vlan_entry vlan_entries[MT7530_NUM_VLANS];
2902 + struct mt7530_port_entry port_entries[MT7530_NUM_PORTS];
2903 +};
2904 +
2905 +struct mt7530_mapping {
2906 + char *name;
2907 + u16 pvids[MT7530_NUM_PORTS];
2908 + u8 members[MT7530_NUM_VLANS];
2909 + u8 etags[MT7530_NUM_VLANS];
2910 + u16 vids[MT7530_NUM_VLANS];
2911 +} mt7530_defaults[] = {
2912 + {
2913 + .name = "llllw",
2914 + .pvids = { 1, 1, 1, 1, 2, 1, 1 },
2915 + .members = { 0, 0x6f, 0x50 },
2916 + .etags = { 0, 0x40, 0x40 },
2917 + .vids = { 0, 1, 2 },
2918 + }, {
2919 + .name = "wllll",
2920 + .pvids = { 2, 1, 1, 1, 1, 1, 1 },
2921 + .members = { 0, 0x7e, 0x41 },
2922 + .etags = { 0, 0x40, 0x40 },
2923 + .vids = { 0, 1, 2 },
2924 + },
2925 +};
2926 +
2927 +struct mt7530_mapping*
2928 +mt7530_find_mapping(struct device_node *np)
2929 +{
2930 + const char *map;
2931 + int i;
2932 +
2933 + if (of_property_read_string(np, "ralink,port-map", &map))
2934 + return NULL;
2935 +
2936 + for (i = 0; i < ARRAY_SIZE(mt7530_defaults); i++)
2937 + if (!strcmp(map, mt7530_defaults[i].name))
2938 + return &mt7530_defaults[i];
2939 +
2940 + return NULL;
2941 +}
2942 +
2943 +static void
2944 +mt7530_apply_mapping(struct mt7530_priv *mt7530, struct mt7530_mapping *map)
2945 +{
2946 + int i = 0;
2947 +
2948 + mt7530->global_vlan_enable = 1;
2949 +
2950 + for (i = 0; i < MT7530_NUM_PORTS; i++)
2951 + mt7530->port_entries[i].pvid = map->pvids[i];
2952 +
2953 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
2954 + mt7530->vlan_entries[i].member = map->members[i];
2955 + mt7530->vlan_entries[i].etags = map->etags[i];
2956 + mt7530->vlan_entries[i].vid = map->vids[i];
2957 + }
2958 +}
2959 +
2960 +static int
2961 +mt7530_reset_switch(struct switch_dev *dev)
2962 +{
2963 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
2964 + int i;
2965 +
2966 + memset(priv->port_entries, 0, sizeof(priv->port_entries));
2967 + memset(priv->vlan_entries, 0, sizeof(priv->vlan_entries));
2968 +
2969 + /* set default vid of each vlan to the same number of vlan, so the vid
2970 + * won't need be set explicitly.
2971 + */
2972 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
2973 + priv->vlan_entries[i].vid = i;
2974 + }
2975 +
2976 + return 0;
2977 +}
2978 +
2979 +static int
2980 +mt7530_get_vlan_enable(struct switch_dev *dev,
2981 + const struct switch_attr *attr,
2982 + struct switch_val *val)
2983 +{
2984 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
2985 +
2986 + val->value.i = priv->global_vlan_enable;
2987 +
2988 + return 0;
2989 +}
2990 +
2991 +static int
2992 +mt7530_set_vlan_enable(struct switch_dev *dev,
2993 + const struct switch_attr *attr,
2994 + struct switch_val *val)
2995 +{
2996 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
2997 +
2998 + priv->global_vlan_enable = val->value.i != 0;
2999 +
3000 + return 0;
3001 +}
3002 +
3003 +static u32
3004 +mt7530_r32(struct mt7530_priv *priv, u32 reg)
3005 +{
3006 + u32 val;
3007 + if (priv->bus) {
3008 + u16 high, low;
3009 +
3010 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3011 + low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf);
3012 + high = mdiobus_read(priv->bus, 0x1f, 0x10);
3013 +
3014 + return (high << 16) | (low & 0xffff);
3015 + }
3016 +
3017 + val = ioread32(priv->base + reg);
3018 + pr_debug("MT7530 MDIO Read [%04x]=%08x\n", reg, val);
3019 +
3020 + return val;
3021 +}
3022 +
3023 +static void
3024 +mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val)
3025 +{
3026 + if (priv->bus) {
3027 + mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff);
3028 + mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff);
3029 + mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16);
3030 + return;
3031 + }
3032 +
3033 + pr_debug("MT7530 MDIO Write[%04x]=%08x\n", reg, val);
3034 + iowrite32(val, priv->base + reg);
3035 +}
3036 +
3037 +static void
3038 +mt7530_vtcr(struct mt7530_priv *priv, u32 cmd, u32 val)
3039 +{
3040 + int i;
3041 +
3042 + mt7530_w32(priv, REG_ESW_VLAN_VTCR, BIT(31) | (cmd << 12) | val);
3043 +
3044 + for (i = 0; i < 20; i++) {
3045 + u32 val = mt7530_r32(priv, REG_ESW_VLAN_VTCR);
3046 +
3047 + if ((val & BIT(31)) == 0)
3048 + break;
3049 +
3050 + udelay(1000);
3051 + }
3052 + if (i == 20)
3053 + printk("mt7530: vtcr timeout\n");
3054 +}
3055 +
3056 +static int
3057 +mt7530_get_port_pvid(struct switch_dev *dev, int port, int *val)
3058 +{
3059 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3060 +
3061 + if (port >= MT7530_NUM_PORTS)
3062 + return -EINVAL;
3063 +
3064 + *val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(port));
3065 + *val &= 0xfff;
3066 +
3067 + return 0;
3068 +}
3069 +
3070 +static int
3071 +mt7530_set_port_pvid(struct switch_dev *dev, int port, int pvid)
3072 +{
3073 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3074 +
3075 + if (port >= MT7530_NUM_PORTS)
3076 + return -EINVAL;
3077 +
3078 + if (pvid < MT7530_MIN_VID || pvid > MT7530_MAX_VID)
3079 + return -EINVAL;
3080 +
3081 + priv->port_entries[port].pvid = pvid;
3082 +
3083 + return 0;
3084 +}
3085 +
3086 +static int
3087 +mt7530_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3088 +{
3089 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3090 + u32 member;
3091 + u32 etags;
3092 + int i;
3093 +
3094 + val->len = 0;
3095 +
3096 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS)
3097 + return -EINVAL;
3098 +
3099 + mt7530_vtcr(priv, 0, val->port_vlan);
3100 +
3101 + member = mt7530_r32(priv, REG_ESW_VLAN_VAWD1);
3102 + member >>= 16;
3103 + member &= 0xff;
3104 +
3105 + etags = mt7530_r32(priv, REG_ESW_VLAN_VAWD2);
3106 +
3107 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3108 + struct switch_port *p;
3109 + int etag;
3110 +
3111 + if (!(member & BIT(i)))
3112 + continue;
3113 +
3114 + p = &val->value.ports[val->len++];
3115 + p->id = i;
3116 +
3117 + etag = (etags >> (i * 2)) & 0x3;
3118 +
3119 + if (etag == ETAG_CTRL_TAG)
3120 + p->flags |= BIT(SWITCH_PORT_FLAG_TAGGED);
3121 + else if (etag != ETAG_CTRL_UNTAG)
3122 + printk("vlan egress tag control neither untag nor tag.\n");
3123 + }
3124 +
3125 + return 0;
3126 +}
3127 +
3128 +static int
3129 +mt7530_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
3130 +{
3131 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3132 + u8 member = 0;
3133 + u8 etags = 0;
3134 + int i;
3135 +
3136 + if (val->port_vlan < 0 || val->port_vlan >= MT7530_NUM_VLANS ||
3137 + val->len > MT7530_NUM_PORTS)
3138 + return -EINVAL;
3139 +
3140 + for (i = 0; i < val->len; i++) {
3141 + struct switch_port *p = &val->value.ports[i];
3142 +
3143 + if (p->id >= MT7530_NUM_PORTS)
3144 + return -EINVAL;
3145 +
3146 + member |= BIT(p->id);
3147 +
3148 + if (p->flags & BIT(SWITCH_PORT_FLAG_TAGGED))
3149 + etags |= BIT(p->id);
3150 + }
3151 + priv->vlan_entries[val->port_vlan].member = member;
3152 + priv->vlan_entries[val->port_vlan].etags = etags;
3153 +
3154 + return 0;
3155 +}
3156 +
3157 +static int
3158 +mt7530_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
3159 + struct switch_val *val)
3160 +{
3161 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3162 + int vlan;
3163 + u16 vid;
3164 +
3165 + vlan = val->port_vlan;
3166 + vid = (u16)val->value.i;
3167 +
3168 + if (vlan < 0 || vlan >= MT7530_NUM_VLANS)
3169 + return -EINVAL;
3170 +
3171 + if (vid < MT7530_MIN_VID || vid > MT7530_MAX_VID)
3172 + return -EINVAL;
3173 +
3174 + priv->vlan_entries[vlan].vid = vid;
3175 + return 0;
3176 +}
3177 +
3178 +static int
3179 +mt7530_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
3180 + struct switch_val *val)
3181 +{
3182 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3183 + u32 vid;
3184 + int vlan;
3185 +
3186 + vlan = val->port_vlan;
3187 +
3188 + vid = mt7530_r32(priv, REG_ESW_VLAN_VTIM(vlan));
3189 + if (vlan & 1)
3190 + vid = vid >> 12;
3191 + vid &= 0xfff;
3192 +
3193 + val->value.i = vid;
3194 + return 0;
3195 +}
3196 +
3197 +static int
3198 +mt7530_apply_config(struct switch_dev *dev)
3199 +{
3200 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3201 + int i, j;
3202 +
3203 + if (!priv->global_vlan_enable) {
3204 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3205 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0000);
3206 +
3207 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3208 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x8100000c);
3209 +
3210 + return 0;
3211 + }
3212 +
3213 + /* set all ports as security mode */
3214 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3215 + mt7530_w32(priv, REG_ESW_PORT_PCR(i), 0x00ff0003);
3216 +
3217 + /* set all ports as user port */
3218 + for (i = 0; i < MT7530_NUM_PORTS; i++)
3219 + mt7530_w32(priv, REG_ESW_PORT_PVC(i), 0x81000000);
3220 +
3221 + for (i = 0; i < MT7530_NUM_VLANS; i++) {
3222 + u16 vid = priv->vlan_entries[i].vid;
3223 + u8 member = priv->vlan_entries[i].member;
3224 + u8 etags = priv->vlan_entries[i].etags;
3225 + u32 val;
3226 +
3227 + /* vid of vlan */
3228 + val = mt7530_r32(priv, REG_ESW_VLAN_VTIM(i));
3229 + if (i % 2 == 0) {
3230 + val &= 0xfff000;
3231 + val |= vid;
3232 + } else {
3233 + val &= 0xfff;
3234 + val |= (vid << 12);
3235 + }
3236 + mt7530_w32(priv, REG_ESW_VLAN_VTIM(i), val);
3237 +
3238 + /* vlan port membership */
3239 + if (member)
3240 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, REG_ESW_VLAN_VAWD1_IVL_MAC |
3241 + REG_ESW_VLAN_VAWD1_VTAG_EN | (member << 16) |
3242 + REG_ESW_VLAN_VAWD1_VALID);
3243 + else
3244 + mt7530_w32(priv, REG_ESW_VLAN_VAWD1, 0);
3245 +
3246 + /* egress mode */
3247 + val = 0;
3248 + for (j = 0; j < MT7530_NUM_PORTS; j++) {
3249 + if (etags & BIT(j))
3250 + val |= ETAG_CTRL_TAG << (j * 2);
3251 + else
3252 + val |= ETAG_CTRL_UNTAG << (j * 2);
3253 + }
3254 + mt7530_w32(priv, REG_ESW_VLAN_VAWD2, val);
3255 +
3256 + /* write to vlan table */
3257 + mt7530_vtcr(priv, 1, i);
3258 + }
3259 +
3260 + /* Port Default PVID */
3261 + for (i = 0; i < MT7530_NUM_PORTS; i++) {
3262 + u32 val;
3263 + val = mt7530_r32(priv, REG_ESW_PORT_PPBV1(i));
3264 + val &= ~0xfff;
3265 + val |= priv->port_entries[i].pvid;
3266 + mt7530_w32(priv, REG_ESW_PORT_PPBV1(i), val);
3267 + }
3268 +
3269 + return 0;
3270 +}
3271 +
3272 +static int
3273 +mt7530_get_port_link(struct switch_dev *dev, int port,
3274 + struct switch_port_link *link)
3275 +{
3276 + struct mt7530_priv *priv = container_of(dev, struct mt7530_priv, swdev);
3277 + u32 speed, pmsr;
3278 +
3279 + if (port < 0 || port >= MT7530_NUM_PORTS)
3280 + return -EINVAL;
3281 +
3282 + pmsr = mt7530_r32(priv, 0x3008 + (0x100 * port));
3283 +
3284 + link->link = pmsr & 1;
3285 + link->duplex = (pmsr >> 1) & 1;
3286 + speed = (pmsr >> 2) & 3;
3287 +
3288 + switch (speed) {
3289 + case 0:
3290 + link->speed = SWITCH_PORT_SPEED_10;
3291 + break;
3292 + case 1:
3293 + link->speed = SWITCH_PORT_SPEED_100;
3294 + break;
3295 + case 2:
3296 + case 3: /* forced gige speed can be 2 or 3 */
3297 + link->speed = SWITCH_PORT_SPEED_1000;
3298 + break;
3299 + default:
3300 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
3301 + break;
3302 + }
3303 +
3304 + return 0;
3305 +}
3306 +
3307 +static const struct switch_attr mt7530_global[] = {
3308 + {
3309 + .type = SWITCH_TYPE_INT,
3310 + .name = "enable_vlan",
3311 + .description = "VLAN mode (1:enabled)",
3312 + .max = 1,
3313 + .id = MT7530_ATTR_ENABLE_VLAN,
3314 + .get = mt7530_get_vlan_enable,
3315 + .set = mt7530_set_vlan_enable,
3316 + },
3317 +};
3318 +
3319 +static const struct switch_attr mt7530_port[] = {
3320 +};
3321 +
3322 +static const struct switch_attr mt7530_vlan[] = {
3323 + {
3324 + .type = SWITCH_TYPE_INT,
3325 + .name = "vid",
3326 + .description = "VLAN ID (0-4094)",
3327 + .set = mt7530_set_vid,
3328 + .get = mt7530_get_vid,
3329 + .max = 4094,
3330 + },
3331 +};
3332 +
3333 +static const struct switch_dev_ops mt7530_ops = {
3334 + .attr_global = {
3335 + .attr = mt7530_global,
3336 + .n_attr = ARRAY_SIZE(mt7530_global),
3337 + },
3338 + .attr_port = {
3339 + .attr = mt7530_port,
3340 + .n_attr = ARRAY_SIZE(mt7530_port),
3341 + },
3342 + .attr_vlan = {
3343 + .attr = mt7530_vlan,
3344 + .n_attr = ARRAY_SIZE(mt7530_vlan),
3345 + },
3346 + .get_vlan_ports = mt7530_get_vlan_ports,