fbd45b829ab3d5fb873d75f17ca9502cac6919c2
[openwrt/svn-archive/archive.git] / target / linux / ramips / patches-3.10 / 0016-NET-MIPS-add-ralink-SoC-ethernet-driver.patch
1 From ad11aedcc16574c0b3d3f5e40c67227d1846b94e 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 16/33] 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 | 31 +
15 drivers/net/ethernet/ralink/Makefile | 18 +
16 drivers/net/ethernet/ralink/esw_rt3052.c | 1463 ++++++++++++++++++++
17 drivers/net/ethernet/ralink/esw_rt3052.h | 32 +
18 drivers/net/ethernet/ralink/gsw_mt7620a.c | 1027 ++++++++++++++
19 drivers/net/ethernet/ralink/gsw_mt7620a.h | 29 +
20 drivers/net/ethernet/ralink/mdio.c | 245 ++++
21 drivers/net/ethernet/ralink/mdio.h | 29 +
22 drivers/net/ethernet/ralink/mdio_rt2880.c | 232 ++++
23 drivers/net/ethernet/ralink/mdio_rt2880.h | 26 +
24 drivers/net/ethernet/ralink/ralink_soc_eth.c | 735 ++++++++++
25 drivers/net/ethernet/ralink/ralink_soc_eth.h | 374 +++++
26 drivers/net/ethernet/ralink/soc_mt7620.c | 111 ++
27 drivers/net/ethernet/ralink/soc_rt2880.c | 51 +
28 drivers/net/ethernet/ralink/soc_rt305x.c | 113 ++
29 drivers/net/ethernet/ralink/soc_rt3883.c | 60 +
30 20 files changed, 4606 insertions(+)
31 create mode 100644 arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
32 create mode 100644 drivers/net/ethernet/ralink/Kconfig
33 create mode 100644 drivers/net/ethernet/ralink/Makefile
34 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.c
35 create mode 100644 drivers/net/ethernet/ralink/esw_rt3052.h
36 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.c
37 create mode 100644 drivers/net/ethernet/ralink/gsw_mt7620a.h
38 create mode 100644 drivers/net/ethernet/ralink/mdio.c
39 create mode 100644 drivers/net/ethernet/ralink/mdio.h
40 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.c
41 create mode 100644 drivers/net/ethernet/ralink/mdio_rt2880.h
42 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.c
43 create mode 100644 drivers/net/ethernet/ralink/ralink_soc_eth.h
44 create mode 100644 drivers/net/ethernet/ralink/soc_mt7620.c
45 create mode 100644 drivers/net/ethernet/ralink/soc_rt2880.c
46 create mode 100644 drivers/net/ethernet/ralink/soc_rt305x.c
47 create mode 100644 drivers/net/ethernet/ralink/soc_rt3883.c
48
49 diff --git a/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
50 new file mode 100644
51 index 0000000..2098c5c
52 --- /dev/null
53 +++ b/arch/mips/include/asm/mach-ralink/rt305x_esw_platform.h
54 @@ -0,0 +1,27 @@
55 +/*
56 + * Ralink RT305x SoC platform device registration
57 + *
58 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
59 + *
60 + * This program is free software; you can redistribute it and/or modify it
61 + * under the terms of the GNU General Public License version 2 as published
62 + * by the Free Software Foundation.
63 + */
64 +
65 +#ifndef _RT305X_ESW_PLATFORM_H
66 +#define _RT305X_ESW_PLATFORM_H
67 +
68 +enum {
69 + RT305X_ESW_VLAN_CONFIG_NONE = 0,
70 + RT305X_ESW_VLAN_CONFIG_LLLLW,
71 + RT305X_ESW_VLAN_CONFIG_WLLLL,
72 +};
73 +
74 +struct rt305x_esw_platform_data
75 +{
76 + u8 vlan_config;
77 + u32 reg_initval_fct2;
78 + u32 reg_initval_fpa2;
79 +};
80 +
81 +#endif /* _RT305X_ESW_PLATFORM_H */
82 diff --git a/arch/mips/ralink/rt305x.c b/arch/mips/ralink/rt305x.c
83 index ca7ee3a..1a6b458 100644
84 --- a/arch/mips/ralink/rt305x.c
85 +++ b/arch/mips/ralink/rt305x.c
86 @@ -221,6 +221,7 @@ void __init ralink_clk_init(void)
87 }
88
89 ralink_clk_add("cpu", cpu_rate);
90 + ralink_clk_add("sys", sys_rate);
91 ralink_clk_add("10000b00.spi", sys_rate);
92 ralink_clk_add("10000100.timer", wdt_rate);
93 ralink_clk_add("10000120.watchdog", wdt_rate);
94 diff --git a/drivers/net/ethernet/Kconfig b/drivers/net/ethernet/Kconfig
95 index ed956e0..0b3caa1 100644
96 --- a/drivers/net/ethernet/Kconfig
97 +++ b/drivers/net/ethernet/Kconfig
98 @@ -135,6 +135,7 @@ config ETHOC
99 source "drivers/net/ethernet/packetengines/Kconfig"
100 source "drivers/net/ethernet/pasemi/Kconfig"
101 source "drivers/net/ethernet/qlogic/Kconfig"
102 +source "drivers/net/ethernet/ralink/Kconfig"
103 source "drivers/net/ethernet/realtek/Kconfig"
104 source "drivers/net/ethernet/renesas/Kconfig"
105 source "drivers/net/ethernet/rdc/Kconfig"
106 diff --git a/drivers/net/ethernet/Makefile b/drivers/net/ethernet/Makefile
107 index 8268d85..508c494 100644
108 --- a/drivers/net/ethernet/Makefile
109 +++ b/drivers/net/ethernet/Makefile
110 @@ -53,6 +53,7 @@ obj-$(CONFIG_ETHOC) += ethoc.o
111 obj-$(CONFIG_NET_PACKET_ENGINE) += packetengines/
112 obj-$(CONFIG_NET_VENDOR_PASEMI) += pasemi/
113 obj-$(CONFIG_NET_VENDOR_QLOGIC) += qlogic/
114 +obj-$(CONFIG_NET_RALINK) += ralink/
115 obj-$(CONFIG_NET_VENDOR_REALTEK) += realtek/
116 obj-$(CONFIG_SH_ETH) += renesas/
117 obj-$(CONFIG_NET_VENDOR_RDC) += rdc/
118 diff --git a/drivers/net/ethernet/ralink/Kconfig b/drivers/net/ethernet/ralink/Kconfig
119 new file mode 100644
120 index 0000000..ca2c9ad
121 --- /dev/null
122 +++ b/drivers/net/ethernet/ralink/Kconfig
123 @@ -0,0 +1,31 @@
124 +config NET_RALINK
125 + tristate "Ralink RT288X/RT3X5X/RT3662/RT3883/MT7620 ethernet driver"
126 + depends on RALINK
127 + help
128 + This driver supports the ethernet mac inside the ralink wisocs
129 +
130 +if NET_RALINK
131 +
132 +config NET_RALINK_MDIO
133 + def_bool NET_RALINK
134 + depends on (SOC_RT288X || SOC_RT3883 || SOC_MT7620)
135 + select PHYLIB
136 +
137 +config NET_RALINK_MDIO_RT2880
138 + def_bool NET_RALINK
139 + depends on (SOC_RT288X || SOC_RT3883)
140 + select NET_RALINK_MDIO
141 +
142 +config NET_RALINK_ESW_RT3052
143 + def_bool NET_RALINK
144 + depends on SOC_RT305X
145 + select PHYLIB
146 + select SWCONFIG
147 +
148 +config NET_RALINK_GSW_MT7620
149 + def_bool NET_RALINK
150 + depends on SOC_MT7620
151 + select NET_RALINK_MDIO
152 + select PHYLIB
153 + select SWCONFIG
154 +endif
155 diff --git a/drivers/net/ethernet/ralink/Makefile b/drivers/net/ethernet/ralink/Makefile
156 new file mode 100644
157 index 0000000..a38fa21
158 --- /dev/null
159 +++ b/drivers/net/ethernet/ralink/Makefile
160 @@ -0,0 +1,18 @@
161 +#
162 +# Makefile for the Ralink SoCs built-in ethernet macs
163 +#
164 +
165 +ralink-eth-y += ralink_soc_eth.o
166 +
167 +ralink-eth-$(CONFIG_NET_RALINK_MDIO) += mdio.o
168 +ralink-eth-$(CONFIG_NET_RALINK_MDIO_RT2880) += mdio_rt2880.o
169 +
170 +ralink-eth-$(CONFIG_NET_RALINK_ESW_RT3052) += esw_rt3052.o
171 +ralink-eth-$(CONFIG_NET_RALINK_GSW_MT7620) += gsw_mt7620a.o
172 +
173 +ralink-eth-$(CONFIG_SOC_RT288X) += soc_rt2880.o
174 +ralink-eth-$(CONFIG_SOC_RT305X) += soc_rt305x.o
175 +ralink-eth-$(CONFIG_SOC_RT3883) += soc_rt3883.o
176 +ralink-eth-$(CONFIG_SOC_MT7620) += soc_mt7620.o
177 +
178 +obj-$(CONFIG_NET_RALINK) += ralink-eth.o
179 diff --git a/drivers/net/ethernet/ralink/esw_rt3052.c b/drivers/net/ethernet/ralink/esw_rt3052.c
180 new file mode 100644
181 index 0000000..b937062
182 --- /dev/null
183 +++ b/drivers/net/ethernet/ralink/esw_rt3052.c
184 @@ -0,0 +1,1463 @@
185 +/*
186 + * This program is free software; you can redistribute it and/or modify
187 + * it under the terms of the GNU General Public License as published by
188 + * the Free Software Foundation; version 2 of the License
189 + *
190 + * This program is distributed in the hope that it will be useful,
191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193 + * GNU General Public License for more details.
194 + *
195 + * You should have received a copy of the GNU General Public License
196 + * along with this program; if not, write to the Free Software
197 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
198 + *
199 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
200 + */
201 +
202 +#include <linux/module.h>
203 +#include <linux/kernel.h>
204 +#include <linux/types.h>
205 +#include <linux/dma-mapping.h>
206 +#include <linux/init.h>
207 +#include <linux/skbuff.h>
208 +#include <linux/etherdevice.h>
209 +#include <linux/ethtool.h>
210 +#include <linux/platform_device.h>
211 +#include <linux/of_device.h>
212 +#include <linux/clk.h>
213 +#include <linux/of_net.h>
214 +#include <linux/of_mdio.h>
215 +
216 +#include <asm/mach-ralink/ralink_regs.h>
217 +
218 +#include "ralink_soc_eth.h"
219 +
220 +#include <linux/ioport.h>
221 +#include <linux/switch.h>
222 +#include <linux/mii.h>
223 +
224 +#include <ralink_regs.h>
225 +#include <asm/mach-ralink/rt305x.h>
226 +#include <asm/mach-ralink/rt305x_esw_platform.h>
227 +
228 +/*
229 + * HW limitations for this switch:
230 + * - No large frame support (PKT_MAX_LEN at most 1536)
231 + * - Can't have untagged vlan and tagged vlan on one port at the same time,
232 + * though this might be possible using the undocumented PPE.
233 + */
234 +
235 +#define RT305X_ESW_REG_ISR 0x00
236 +#define RT305X_ESW_REG_IMR 0x04
237 +#define RT305X_ESW_REG_FCT0 0x08
238 +#define RT305X_ESW_REG_PFC1 0x14
239 +#define RT305X_ESW_REG_ATS 0x24
240 +#define RT305X_ESW_REG_ATS0 0x28
241 +#define RT305X_ESW_REG_ATS1 0x2c
242 +#define RT305X_ESW_REG_ATS2 0x30
243 +#define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
244 +#define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
245 +#define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
246 +#define RT305X_ESW_REG_POA 0x80
247 +#define RT305X_ESW_REG_FPA 0x84
248 +#define RT305X_ESW_REG_SOCPC 0x8c
249 +#define RT305X_ESW_REG_POC0 0x90
250 +#define RT305X_ESW_REG_POC1 0x94
251 +#define RT305X_ESW_REG_POC2 0x98
252 +#define RT305X_ESW_REG_SGC 0x9c
253 +#define RT305X_ESW_REG_STRT 0xa0
254 +#define RT305X_ESW_REG_PCR0 0xc0
255 +#define RT305X_ESW_REG_PCR1 0xc4
256 +#define RT305X_ESW_REG_FPA2 0xc8
257 +#define RT305X_ESW_REG_FCT2 0xcc
258 +#define RT305X_ESW_REG_SGC2 0xe4
259 +#define RT305X_ESW_REG_P0LED 0xa4
260 +#define RT305X_ESW_REG_P1LED 0xa8
261 +#define RT305X_ESW_REG_P2LED 0xac
262 +#define RT305X_ESW_REG_P3LED 0xb0
263 +#define RT305X_ESW_REG_P4LED 0xb4
264 +#define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
265 +#define RT305X_ESW_REG_P1PC 0xec
266 +#define RT305X_ESW_REG_P2PC 0xf0
267 +#define RT305X_ESW_REG_P3PC 0xf4
268 +#define RT305X_ESW_REG_P4PC 0xf8
269 +#define RT305X_ESW_REG_P5PC 0xfc
270 +
271 +#define RT305X_ESW_LED_LINK 0
272 +#define RT305X_ESW_LED_100M 1
273 +#define RT305X_ESW_LED_DUPLEX 2
274 +#define RT305X_ESW_LED_ACTIVITY 3
275 +#define RT305X_ESW_LED_COLLISION 4
276 +#define RT305X_ESW_LED_LINKACT 5
277 +#define RT305X_ESW_LED_DUPLCOLL 6
278 +#define RT305X_ESW_LED_10MACT 7
279 +#define RT305X_ESW_LED_100MACT 8
280 +/* Additional led states not in datasheet: */
281 +#define RT305X_ESW_LED_BLINK 10
282 +#define RT305X_ESW_LED_ON 12
283 +
284 +#define RT305X_ESW_LINK_S 25
285 +#define RT305X_ESW_DUPLEX_S 9
286 +#define RT305X_ESW_SPD_S 0
287 +
288 +#define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
289 +#define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
290 +#define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
291 +
292 +#define RT305X_ESW_PCR1_WT_DONE BIT(0)
293 +
294 +#define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
295 +#define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
296 +
297 +#define RT305X_ESW_PVIDC_PVID_M 0xfff
298 +#define RT305X_ESW_PVIDC_PVID_S 12
299 +
300 +#define RT305X_ESW_VLANI_VID_M 0xfff
301 +#define RT305X_ESW_VLANI_VID_S 12
302 +
303 +#define RT305X_ESW_VMSC_MSC_M 0xff
304 +#define RT305X_ESW_VMSC_MSC_S 8
305 +
306 +#define RT305X_ESW_SOCPC_DISUN2CPU_S 0
307 +#define RT305X_ESW_SOCPC_DISMC2CPU_S 8
308 +#define RT305X_ESW_SOCPC_DISBC2CPU_S 16
309 +#define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
310 +
311 +#define RT305X_ESW_POC0_EN_BP_S 0
312 +#define RT305X_ESW_POC0_EN_FC_S 8
313 +#define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
314 +#define RT305X_ESW_POC0_DIS_PORT_M 0x7f
315 +#define RT305X_ESW_POC0_DIS_PORT_S 23
316 +
317 +#define RT305X_ESW_POC2_UNTAG_EN_M 0xff
318 +#define RT305X_ESW_POC2_UNTAG_EN_S 0
319 +#define RT305X_ESW_POC2_ENAGING_S 8
320 +#define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
321 +
322 +#define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
323 +#define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
324 +#define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
325 +#define RT305X_ESW_SGC2_LAN_PMAP_S 24
326 +
327 +#define RT305X_ESW_PFC1_EN_VLAN_M 0xff
328 +#define RT305X_ESW_PFC1_EN_VLAN_S 16
329 +#define RT305X_ESW_PFC1_EN_TOS_S 24
330 +
331 +#define RT305X_ESW_VLAN_NONE 0xfff
332 +
333 +#define RT305X_ESW_GSC_BC_STROM_MASK 0x3
334 +#define RT305X_ESW_GSC_BC_STROM_SHIFT 4
335 +
336 +#define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
337 +#define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
338 +
339 +#define RT305X_ESW_POA_LINK_MASK 0x1f
340 +#define RT305X_ESW_POA_LINK_SHIFT 25
341 +
342 +#define RT305X_ESW_PORT_ST_CHG BIT(26)
343 +#define RT305X_ESW_PORT0 0
344 +#define RT305X_ESW_PORT1 1
345 +#define RT305X_ESW_PORT2 2
346 +#define RT305X_ESW_PORT3 3
347 +#define RT305X_ESW_PORT4 4
348 +#define RT305X_ESW_PORT5 5
349 +#define RT305X_ESW_PORT6 6
350 +
351 +#define RT305X_ESW_PORTS_NONE 0
352 +
353 +#define RT305X_ESW_PMAP_LLLLLL 0x3f
354 +#define RT305X_ESW_PMAP_LLLLWL 0x2f
355 +#define RT305X_ESW_PMAP_WLLLLL 0x3e
356 +
357 +#define RT305X_ESW_PORTS_INTERNAL \
358 + (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
359 + BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
360 + BIT(RT305X_ESW_PORT4))
361 +
362 +#define RT305X_ESW_PORTS_NOCPU \
363 + (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
364 +
365 +#define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
366 +
367 +#define RT305X_ESW_PORTS_ALL \
368 + (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
369 +
370 +#define RT305X_ESW_NUM_VLANS 16
371 +#define RT305X_ESW_NUM_VIDS 4096
372 +#define RT305X_ESW_NUM_PORTS 7
373 +#define RT305X_ESW_NUM_LANWAN 6
374 +#define RT305X_ESW_NUM_LEDS 5
375 +
376 +#define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
377 +#define RT5350_EWS_REG_LED_POLARITY 0x168
378 +#define RT5350_RESET_EPHY BIT(24)
379 +#define SYSC_REG_RESET_CTRL 0x34
380 +
381 +enum {
382 + /* Global attributes. */
383 + RT305X_ESW_ATTR_ENABLE_VLAN,
384 + RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
385 + RT305X_ESW_ATTR_BC_STATUS,
386 + RT305X_ESW_ATTR_LED_FREQ,
387 + /* Port attributes. */
388 + RT305X_ESW_ATTR_PORT_DISABLE,
389 + RT305X_ESW_ATTR_PORT_DOUBLETAG,
390 + RT305X_ESW_ATTR_PORT_UNTAG,
391 + RT305X_ESW_ATTR_PORT_LED,
392 + RT305X_ESW_ATTR_PORT_LAN,
393 + RT305X_ESW_ATTR_PORT_RECV_BAD,
394 + RT305X_ESW_ATTR_PORT_RECV_GOOD,
395 + RT5350_ESW_ATTR_PORT_TR_BAD,
396 + RT5350_ESW_ATTR_PORT_TR_GOOD,
397 +};
398 +
399 +struct esw_port {
400 + bool disable;
401 + bool doubletag;
402 + bool untag;
403 + u8 led;
404 + u16 pvid;
405 +};
406 +
407 +struct esw_vlan {
408 + u8 ports;
409 + u16 vid;
410 +};
411 +
412 +struct rt305x_esw {
413 + struct device *dev;
414 + void __iomem *base;
415 + int irq;
416 + const struct rt305x_esw_platform_data *pdata;
417 + /* Protects against concurrent register rmw operations. */
418 + spinlock_t reg_rw_lock;
419 +
420 + unsigned char port_map;
421 + unsigned int reg_initval_fct2;
422 + unsigned int reg_initval_fpa2;
423 + unsigned int reg_led_polarity;
424 +
425 +
426 + struct switch_dev swdev;
427 + bool global_vlan_enable;
428 + bool alt_vlan_disable;
429 + int bc_storm_protect;
430 + int led_frequency;
431 + struct esw_vlan vlans[RT305X_ESW_NUM_VLANS];
432 + struct esw_port ports[RT305X_ESW_NUM_PORTS];
433 +
434 +};
435 +
436 +static inline void esw_w32(struct rt305x_esw *esw, u32 val, unsigned reg)
437 +{
438 + __raw_writel(val, esw->base + reg);
439 +}
440 +
441 +static inline u32 esw_r32(struct rt305x_esw *esw, unsigned reg)
442 +{
443 + return __raw_readl(esw->base + reg);
444 +}
445 +
446 +static inline void esw_rmw_raw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
447 + unsigned long val)
448 +{
449 + unsigned long t;
450 +
451 + t = __raw_readl(esw->base + reg) & ~mask;
452 + __raw_writel(t | val, esw->base + reg);
453 +}
454 +
455 +static void esw_rmw(struct rt305x_esw *esw, unsigned reg, unsigned long mask,
456 + unsigned long val)
457 +{
458 + unsigned long flags;
459 +
460 + spin_lock_irqsave(&esw->reg_rw_lock, flags);
461 + esw_rmw_raw(esw, reg, mask, val);
462 + spin_unlock_irqrestore(&esw->reg_rw_lock, flags);
463 +}
464 +
465 +static u32 rt305x_mii_write(struct rt305x_esw *esw, u32 phy_addr, u32 phy_register,
466 + u32 write_data)
467 +{
468 + unsigned long t_start = jiffies;
469 + int ret = 0;
470 +
471 + while (1) {
472 + if (!(esw_r32(esw, RT305X_ESW_REG_PCR1) &
473 + RT305X_ESW_PCR1_WT_DONE))
474 + break;
475 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
476 + ret = 1;
477 + goto out;
478 + }
479 + }
480 +
481 + write_data &= 0xffff;
482 + esw_w32(esw,
483 + (write_data << RT305X_ESW_PCR0_WT_NWAY_DATA_S) |
484 + (phy_register << RT305X_ESW_PCR0_CPU_PHY_REG_S) |
485 + (phy_addr) | RT305X_ESW_PCR0_WT_PHY_CMD,
486 + RT305X_ESW_REG_PCR0);
487 +
488 + t_start = jiffies;
489 + while (1) {
490 + if (esw_r32(esw, RT305X_ESW_REG_PCR1) &
491 + RT305X_ESW_PCR1_WT_DONE)
492 + break;
493 +
494 + if (time_after(jiffies, t_start + RT305X_ESW_PHY_TIMEOUT)) {
495 + ret = 1;
496 + break;
497 + }
498 + }
499 +out:
500 + if (ret)
501 + printk(KERN_ERR "ramips_eth: MDIO timeout\n");
502 + return ret;
503 +}
504 +
505 +static unsigned esw_get_vlan_id(struct rt305x_esw *esw, unsigned vlan)
506 +{
507 + unsigned s;
508 + unsigned val;
509 +
510 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
511 + val = esw_r32(esw, RT305X_ESW_REG_VLANI(vlan / 2));
512 + val = (val >> s) & RT305X_ESW_VLANI_VID_M;
513 +
514 + return val;
515 +}
516 +
517 +static void esw_set_vlan_id(struct rt305x_esw *esw, unsigned vlan, unsigned vid)
518 +{
519 + unsigned s;
520 +
521 + s = RT305X_ESW_VLANI_VID_S * (vlan % 2);
522 + esw_rmw(esw,
523 + RT305X_ESW_REG_VLANI(vlan / 2),
524 + RT305X_ESW_VLANI_VID_M << s,
525 + (vid & RT305X_ESW_VLANI_VID_M) << s);
526 +}
527 +
528 +static unsigned esw_get_pvid(struct rt305x_esw *esw, unsigned port)
529 +{
530 + unsigned s, val;
531 +
532 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
533 + val = esw_r32(esw, RT305X_ESW_REG_PVIDC(port / 2));
534 + return (val >> s) & RT305X_ESW_PVIDC_PVID_M;
535 +}
536 +
537 +static void esw_set_pvid(struct rt305x_esw *esw, unsigned port, unsigned pvid)
538 +{
539 + unsigned s;
540 +
541 + s = RT305X_ESW_PVIDC_PVID_S * (port % 2);
542 + esw_rmw(esw,
543 + RT305X_ESW_REG_PVIDC(port / 2),
544 + RT305X_ESW_PVIDC_PVID_M << s,
545 + (pvid & RT305X_ESW_PVIDC_PVID_M) << s);
546 +}
547 +
548 +static unsigned esw_get_vmsc(struct rt305x_esw *esw, unsigned vlan)
549 +{
550 + unsigned s, val;
551 +
552 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
553 + val = esw_r32(esw, RT305X_ESW_REG_VMSC(vlan / 4));
554 + val = (val >> s) & RT305X_ESW_VMSC_MSC_M;
555 +
556 + return val;
557 +}
558 +
559 +static void esw_set_vmsc(struct rt305x_esw *esw, unsigned vlan, unsigned msc)
560 +{
561 + unsigned s;
562 +
563 + s = RT305X_ESW_VMSC_MSC_S * (vlan % 4);
564 + esw_rmw(esw,
565 + RT305X_ESW_REG_VMSC(vlan / 4),
566 + RT305X_ESW_VMSC_MSC_M << s,
567 + (msc & RT305X_ESW_VMSC_MSC_M) << s);
568 +}
569 +
570 +static unsigned esw_get_port_disable(struct rt305x_esw *esw)
571 +{
572 + unsigned reg;
573 + reg = esw_r32(esw, RT305X_ESW_REG_POC0);
574 + return (reg >> RT305X_ESW_POC0_DIS_PORT_S) &
575 + RT305X_ESW_POC0_DIS_PORT_M;
576 +}
577 +
578 +static void esw_set_port_disable(struct rt305x_esw *esw, unsigned disable_mask)
579 +{
580 + unsigned old_mask;
581 + unsigned enable_mask;
582 + unsigned changed;
583 + int i;
584 +
585 + old_mask = esw_get_port_disable(esw);
586 + changed = old_mask ^ disable_mask;
587 + enable_mask = old_mask & disable_mask;
588 +
589 + /* enable before writing to MII */
590 + esw_rmw(esw, RT305X_ESW_REG_POC0,
591 + (RT305X_ESW_POC0_DIS_PORT_M <<
592 + RT305X_ESW_POC0_DIS_PORT_S),
593 + enable_mask << RT305X_ESW_POC0_DIS_PORT_S);
594 +
595 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++) {
596 + if (!(changed & (1 << i)))
597 + continue;
598 + if (disable_mask & (1 << i)) {
599 + /* disable */
600 + rt305x_mii_write(esw, i, MII_BMCR,
601 + BMCR_PDOWN);
602 + } else {
603 + /* enable */
604 + rt305x_mii_write(esw, i, MII_BMCR,
605 + BMCR_FULLDPLX |
606 + BMCR_ANENABLE |
607 + BMCR_ANRESTART |
608 + BMCR_SPEED100);
609 + }
610 + }
611 +
612 + /* disable after writing to MII */
613 + esw_rmw(esw, RT305X_ESW_REG_POC0,
614 + (RT305X_ESW_POC0_DIS_PORT_M <<
615 + RT305X_ESW_POC0_DIS_PORT_S),
616 + disable_mask << RT305X_ESW_POC0_DIS_PORT_S);
617 +}
618 +
619 +static void esw_set_gsc(struct rt305x_esw *esw)
620 +{
621 + esw_rmw(esw, RT305X_ESW_REG_SGC,
622 + RT305X_ESW_GSC_BC_STROM_MASK << RT305X_ESW_GSC_BC_STROM_SHIFT,
623 + esw->bc_storm_protect << RT305X_ESW_GSC_BC_STROM_SHIFT);
624 + esw_rmw(esw, RT305X_ESW_REG_SGC,
625 + RT305X_ESW_GSC_LED_FREQ_MASK << RT305X_ESW_GSC_LED_FREQ_SHIFT,
626 + esw->led_frequency << RT305X_ESW_GSC_LED_FREQ_SHIFT);
627 +}
628 +
629 +static int esw_apply_config(struct switch_dev *dev);
630 +
631 +static void esw_hw_init(struct rt305x_esw *esw)
632 +{
633 + int i;
634 + u8 port_disable = 0;
635 + u8 port_map = RT305X_ESW_PMAP_LLLLLL;
636 +
637 + /* vodoo from original driver */
638 + esw_w32(esw, 0xC8A07850, RT305X_ESW_REG_FCT0);
639 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_SGC2);
640 + /* Port priority 1 for all ports, vlan enabled. */
641 + esw_w32(esw, 0x00005555 |
642 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_PFC1_EN_VLAN_S),
643 + RT305X_ESW_REG_PFC1);
644 +
645 + /* Enable Back Pressure, and Flow Control */
646 + esw_w32(esw,
647 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_BP_S) |
648 + (RT305X_ESW_PORTS_ALL << RT305X_ESW_POC0_EN_FC_S)),
649 + RT305X_ESW_REG_POC0);
650 +
651 + /* Enable Aging, and VLAN TAG removal */
652 + esw_w32(esw,
653 + ((RT305X_ESW_PORTS_ALL << RT305X_ESW_POC2_ENAGING_S) |
654 + (RT305X_ESW_PORTS_NOCPU << RT305X_ESW_POC2_UNTAG_EN_S)),
655 + RT305X_ESW_REG_POC2);
656 +
657 + if (esw->reg_initval_fct2)
658 + esw_w32(esw, esw->reg_initval_fct2, RT305X_ESW_REG_FCT2);
659 + else
660 + esw_w32(esw, esw->pdata->reg_initval_fct2, RT305X_ESW_REG_FCT2);
661 +
662 + /*
663 + * 300s aging timer, max packet len 1536, broadcast storm prevention
664 + * disabled, disable collision abort, mac xor48 hash, 10 packet back
665 + * pressure jam, GMII disable was_transmit, back pressure disabled,
666 + * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
667 + * ports.
668 + */
669 + esw_w32(esw, 0x0008a301, RT305X_ESW_REG_SGC);
670 +
671 + /* Setup SoC Port control register */
672 + esw_w32(esw,
673 + (RT305X_ESW_SOCPC_CRC_PADDING |
674 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISUN2CPU_S) |
675 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISMC2CPU_S) |
676 + (RT305X_ESW_PORTS_CPU << RT305X_ESW_SOCPC_DISBC2CPU_S)),
677 + RT305X_ESW_REG_SOCPC);
678 +
679 + if (esw->reg_initval_fpa2)
680 + esw_w32(esw, esw->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
681 + else
682 + esw_w32(esw, esw->pdata->reg_initval_fpa2, RT305X_ESW_REG_FPA2);
683 + esw_w32(esw, 0x00000000, RT305X_ESW_REG_FPA);
684 +
685 + /* Force Link/Activity on ports */
686 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P0LED);
687 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P1LED);
688 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P2LED);
689 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P3LED);
690 + esw_w32(esw, 0x00000005, RT305X_ESW_REG_P4LED);
691 +
692 + /* Copy disabled port configuration from bootloader setup */
693 + port_disable = esw_get_port_disable(esw);
694 + for (i = 0; i < 6; i++)
695 + esw->ports[i].disable = (port_disable & (1 << i)) != 0;
696 +
697 + if (soc_is_rt3352()) {
698 + /* reset EPHY */
699 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
700 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
701 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
702 +
703 + rt305x_mii_write(esw, 0, 31, 0x8000);
704 + for (i = 0; i < 5; i++) {
705 + if (esw->ports[i].disable) {
706 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
707 + } else {
708 + rt305x_mii_write(esw, i, MII_BMCR,
709 + BMCR_FULLDPLX |
710 + BMCR_ANENABLE |
711 + BMCR_SPEED100);
712 + }
713 + /* TX10 waveform coefficient LSB=0 disable PHY */
714 + rt305x_mii_write(esw, i, 26, 0x1601);
715 + /* TX100/TX10 AD/DA current bias */
716 + rt305x_mii_write(esw, i, 29, 0x7016);
717 + /* TX100 slew rate control */
718 + rt305x_mii_write(esw, i, 30, 0x0038);
719 + }
720 +
721 + /* select global register */
722 + rt305x_mii_write(esw, 0, 31, 0x0);
723 + /* enlarge agcsel threshold 3 and threshold 2 */
724 + rt305x_mii_write(esw, 0, 1, 0x4a40);
725 + /* enlarge agcsel threshold 5 and threshold 4 */
726 + rt305x_mii_write(esw, 0, 2, 0x6254);
727 + /* enlarge agcsel threshold */
728 + rt305x_mii_write(esw, 0, 3, 0xa17f);
729 + rt305x_mii_write(esw, 0,12, 0x7eaa);
730 + /* longer TP_IDL tail length */
731 + rt305x_mii_write(esw, 0, 14, 0x65);
732 + /* increased squelch pulse count threshold. */
733 + rt305x_mii_write(esw, 0, 16, 0x0684);
734 + /* set TX10 signal amplitude threshold to minimum */
735 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
736 + /* set squelch amplitude to higher threshold */
737 + rt305x_mii_write(esw, 0, 18, 0x40ba);
738 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
739 + rt305x_mii_write(esw, 0, 22, 0x253f);
740 + /* set PLL/Receive bias current are calibrated */
741 + rt305x_mii_write(esw, 0, 27, 0x2fda);
742 + /* change PLL/Receive bias current to internal(RT3350) */
743 + rt305x_mii_write(esw, 0, 28, 0xc410);
744 + /* change PLL bias current to internal(RT3052_MP3) */
745 + rt305x_mii_write(esw, 0, 29, 0x598b);
746 + /* select local register */
747 + rt305x_mii_write(esw, 0, 31, 0x8000);
748 + } else if (soc_is_rt5350()) {
749 + /* reset EPHY */
750 + u32 val = rt_sysc_r32(SYSC_REG_RESET_CTRL);
751 + rt_sysc_w32(val | RT5350_RESET_EPHY, SYSC_REG_RESET_CTRL);
752 + rt_sysc_w32(val, SYSC_REG_RESET_CTRL);
753 +
754 + /* set the led polarity */
755 + esw_w32(esw, esw->reg_led_polarity & 0x1F, RT5350_EWS_REG_LED_POLARITY);
756 +
757 + /* local registers */
758 + rt305x_mii_write(esw, 0, 31, 0x8000);
759 + for (i = 0; i < 5; i++) {
760 + if (esw->ports[i].disable) {
761 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
762 + } else {
763 + rt305x_mii_write(esw, i, MII_BMCR,
764 + BMCR_FULLDPLX |
765 + BMCR_ANENABLE |
766 + BMCR_SPEED100);
767 + }
768 + /* TX10 waveform coefficient LSB=0 disable PHY */
769 + rt305x_mii_write(esw, i, 26, 0x1601);
770 + /* TX100/TX10 AD/DA current bias */
771 + rt305x_mii_write(esw, i, 29, 0x7015);
772 + /* TX100 slew rate control */
773 + rt305x_mii_write(esw, i, 30, 0x0038);
774 + }
775 +
776 + /* global registers */
777 + rt305x_mii_write(esw, 0, 31, 0x0);
778 + /* enlarge agcsel threshold 3 and threshold 2 */
779 + rt305x_mii_write(esw, 0, 1, 0x4a40);
780 + /* enlarge agcsel threshold 5 and threshold 4 */
781 + rt305x_mii_write(esw, 0, 2, 0x6254);
782 + /* enlarge agcsel threshold 6 */
783 + rt305x_mii_write(esw, 0, 3, 0xa17f);
784 + rt305x_mii_write(esw, 0, 12, 0x7eaa);
785 + /* longer TP_IDL tail length */
786 + rt305x_mii_write(esw, 0, 14, 0x65);
787 + /* increased squelch pulse count threshold. */
788 + rt305x_mii_write(esw, 0, 16, 0x0684);
789 + /* set TX10 signal amplitude threshold to minimum */
790 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
791 + /* set squelch amplitude to higher threshold */
792 + rt305x_mii_write(esw, 0, 18, 0x40ba);
793 + /* tune TP_IDL tail and head waveform, enable power down slew rate control */
794 + rt305x_mii_write(esw, 0, 22, 0x253f);
795 + /* set PLL/Receive bias current are calibrated */
796 + rt305x_mii_write(esw, 0, 27, 0x2fda);
797 + /* change PLL/Receive bias current to internal(RT3350) */
798 + rt305x_mii_write(esw, 0, 28, 0xc410);
799 + /* change PLL bias current to internal(RT3052_MP3) */
800 + rt305x_mii_write(esw, 0, 29, 0x598b);
801 + /* select local register */
802 + rt305x_mii_write(esw, 0, 31, 0x8000);
803 + } else {
804 + rt305x_mii_write(esw, 0, 31, 0x8000);
805 + for (i = 0; i < 5; i++) {
806 + if (esw->ports[i].disable) {
807 + rt305x_mii_write(esw, i, MII_BMCR, BMCR_PDOWN);
808 + } else {
809 + rt305x_mii_write(esw, i, MII_BMCR,
810 + BMCR_FULLDPLX |
811 + BMCR_ANENABLE |
812 + BMCR_SPEED100);
813 + }
814 + /* TX10 waveform coefficient */
815 + rt305x_mii_write(esw, i, 26, 0x1601);
816 + /* TX100/TX10 AD/DA current bias */
817 + rt305x_mii_write(esw, i, 29, 0x7058);
818 + /* TX100 slew rate control */
819 + rt305x_mii_write(esw, i, 30, 0x0018);
820 + }
821 +
822 + /* PHY IOT */
823 + /* select global register */
824 + rt305x_mii_write(esw, 0, 31, 0x0);
825 + /* tune TP_IDL tail and head waveform */
826 + rt305x_mii_write(esw, 0, 22, 0x052f);
827 + /* set TX10 signal amplitude threshold to minimum */
828 + rt305x_mii_write(esw, 0, 17, 0x0fe0);
829 + /* set squelch amplitude to higher threshold */
830 + rt305x_mii_write(esw, 0, 18, 0x40ba);
831 + /* longer TP_IDL tail length */
832 + rt305x_mii_write(esw, 0, 14, 0x65);
833 + /* select local register */
834 + rt305x_mii_write(esw, 0, 31, 0x8000);
835 + }
836 +
837 + if (esw->port_map)
838 + port_map = esw->port_map;
839 + else
840 + port_map = RT305X_ESW_PMAP_LLLLLL;
841 +
842 + /*
843 + * Unused HW feature, but still nice to be consistent here...
844 + * This is also exported to userspace ('lan' attribute) so it's
845 + * conveniently usable to decide which ports go into the wan vlan by
846 + * default.
847 + */
848 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
849 + RT305X_ESW_SGC2_LAN_PMAP_M << RT305X_ESW_SGC2_LAN_PMAP_S,
850 + port_map << RT305X_ESW_SGC2_LAN_PMAP_S);
851 +
852 + /* make the switch leds blink */
853 + for (i = 0; i < RT305X_ESW_NUM_LEDS; i++)
854 + esw->ports[i].led = 0x05;
855 +
856 + /* Apply the empty config. */
857 + esw_apply_config(&esw->swdev);
858 +
859 + /* Only unmask the port change interrupt */
860 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
861 +}
862 +
863 +static irqreturn_t esw_interrupt(int irq, void *_esw)
864 +{
865 + struct rt305x_esw *esw = (struct rt305x_esw *) _esw;
866 + u32 status;
867 +
868 + status = esw_r32(esw, RT305X_ESW_REG_ISR);
869 + if (status & RT305X_ESW_PORT_ST_CHG) {
870 + u32 link = esw_r32(esw, RT305X_ESW_REG_POA);
871 + link >>= RT305X_ESW_POA_LINK_SHIFT;
872 + link &= RT305X_ESW_POA_LINK_MASK;
873 + dev_info(esw->dev, "link changed 0x%02X\n", link);
874 + }
875 + esw_w32(esw, status, RT305X_ESW_REG_ISR);
876 +
877 + return IRQ_HANDLED;
878 +}
879 +
880 +static int esw_apply_config(struct switch_dev *dev)
881 +{
882 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
883 + int i;
884 + u8 disable = 0;
885 + u8 doubletag = 0;
886 + u8 en_vlan = 0;
887 + u8 untag = 0;
888 +
889 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
890 + u32 vid, vmsc;
891 + if (esw->global_vlan_enable) {
892 + vid = esw->vlans[i].vid;
893 + vmsc = esw->vlans[i].ports;
894 + } else {
895 + vid = RT305X_ESW_VLAN_NONE;
896 + vmsc = RT305X_ESW_PORTS_NONE;
897 + }
898 + esw_set_vlan_id(esw, i, vid);
899 + esw_set_vmsc(esw, i, vmsc);
900 + }
901 +
902 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
903 + u32 pvid;
904 + disable |= esw->ports[i].disable << i;
905 + if (esw->global_vlan_enable) {
906 + doubletag |= esw->ports[i].doubletag << i;
907 + en_vlan |= 1 << i;
908 + untag |= esw->ports[i].untag << i;
909 + pvid = esw->ports[i].pvid;
910 + } else {
911 + int x = esw->alt_vlan_disable ? 0 : 1;
912 + doubletag |= x << i;
913 + en_vlan |= x << i;
914 + untag |= x << i;
915 + pvid = 0;
916 + }
917 + esw_set_pvid(esw, i, pvid);
918 + if (i < RT305X_ESW_NUM_LEDS)
919 + esw_w32(esw, esw->ports[i].led,
920 + RT305X_ESW_REG_P0LED + 4*i);
921 + }
922 +
923 + esw_set_gsc(esw);
924 + esw_set_port_disable(esw, disable);
925 + esw_rmw(esw, RT305X_ESW_REG_SGC2,
926 + (RT305X_ESW_SGC2_DOUBLE_TAG_M <<
927 + RT305X_ESW_SGC2_DOUBLE_TAG_S),
928 + doubletag << RT305X_ESW_SGC2_DOUBLE_TAG_S);
929 + esw_rmw(esw, RT305X_ESW_REG_PFC1,
930 + RT305X_ESW_PFC1_EN_VLAN_M << RT305X_ESW_PFC1_EN_VLAN_S,
931 + en_vlan << RT305X_ESW_PFC1_EN_VLAN_S);
932 + esw_rmw(esw, RT305X_ESW_REG_POC2,
933 + RT305X_ESW_POC2_UNTAG_EN_M << RT305X_ESW_POC2_UNTAG_EN_S,
934 + untag << RT305X_ESW_POC2_UNTAG_EN_S);
935 +
936 + if (!esw->global_vlan_enable) {
937 + /*
938 + * Still need to put all ports into vlan 0 or they'll be
939 + * isolated.
940 + * NOTE: vlan 0 is special, no vlan tag is prepended
941 + */
942 + esw_set_vlan_id(esw, 0, 0);
943 + esw_set_vmsc(esw, 0, RT305X_ESW_PORTS_ALL);
944 + }
945 +
946 + return 0;
947 +}
948 +
949 +static int esw_reset_switch(struct switch_dev *dev)
950 +{
951 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
952 +
953 + esw->global_vlan_enable = 0;
954 + memset(esw->ports, 0, sizeof(esw->ports));
955 + memset(esw->vlans, 0, sizeof(esw->vlans));
956 + esw_hw_init(esw);
957 +
958 + return 0;
959 +}
960 +
961 +static int esw_get_vlan_enable(struct switch_dev *dev,
962 + const struct switch_attr *attr,
963 + struct switch_val *val)
964 +{
965 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
966 +
967 + val->value.i = esw->global_vlan_enable;
968 +
969 + return 0;
970 +}
971 +
972 +static int esw_set_vlan_enable(struct switch_dev *dev,
973 + const struct switch_attr *attr,
974 + struct switch_val *val)
975 +{
976 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
977 +
978 + esw->global_vlan_enable = val->value.i != 0;
979 +
980 + return 0;
981 +}
982 +
983 +static int esw_get_alt_vlan_disable(struct switch_dev *dev,
984 + const struct switch_attr *attr,
985 + struct switch_val *val)
986 +{
987 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
988 +
989 + val->value.i = esw->alt_vlan_disable;
990 +
991 + return 0;
992 +}
993 +
994 +static int esw_set_alt_vlan_disable(struct switch_dev *dev,
995 + const struct switch_attr *attr,
996 + struct switch_val *val)
997 +{
998 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
999 +
1000 + esw->alt_vlan_disable = val->value.i != 0;
1001 +
1002 + return 0;
1003 +}
1004 +
1005 +static int
1006 +rt305x_esw_set_bc_status(struct switch_dev *dev,
1007 + const struct switch_attr *attr,
1008 + struct switch_val *val)
1009 +{
1010 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1011 +
1012 + esw->bc_storm_protect = val->value.i & RT305X_ESW_GSC_BC_STROM_MASK;
1013 +
1014 + return 0;
1015 +}
1016 +
1017 +static int
1018 +rt305x_esw_get_bc_status(struct switch_dev *dev,
1019 + const struct switch_attr *attr,
1020 + struct switch_val *val)
1021 +{
1022 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1023 +
1024 + val->value.i = esw->bc_storm_protect;
1025 +
1026 + return 0;
1027 +}
1028 +
1029 +static int
1030 +rt305x_esw_set_led_freq(struct switch_dev *dev,
1031 + const struct switch_attr *attr,
1032 + struct switch_val *val)
1033 +{
1034 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1035 +
1036 + esw->led_frequency = val->value.i & RT305X_ESW_GSC_LED_FREQ_MASK;
1037 +
1038 + return 0;
1039 +}
1040 +
1041 +static int
1042 +rt305x_esw_get_led_freq(struct switch_dev *dev,
1043 + const struct switch_attr *attr,
1044 + struct switch_val *val)
1045 +{
1046 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1047 +
1048 + val->value.i = esw->led_frequency;
1049 +
1050 + return 0;
1051 +}
1052 +
1053 +static int esw_get_port_link(struct switch_dev *dev,
1054 + int port,
1055 + struct switch_port_link *link)
1056 +{
1057 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1058 + u32 speed, poa;
1059 +
1060 + if (port < 0 || port >= RT305X_ESW_NUM_PORTS)
1061 + return -EINVAL;
1062 +
1063 + poa = esw_r32(esw, RT305X_ESW_REG_POA) >> port;
1064 +
1065 + link->link = (poa >> RT305X_ESW_LINK_S) & 1;
1066 + link->duplex = (poa >> RT305X_ESW_DUPLEX_S) & 1;
1067 + if (port < RT305X_ESW_NUM_LEDS) {
1068 + speed = (poa >> RT305X_ESW_SPD_S) & 1;
1069 + } else {
1070 + if (port == RT305X_ESW_NUM_PORTS - 1)
1071 + poa >>= 1;
1072 + speed = (poa >> RT305X_ESW_SPD_S) & 3;
1073 + }
1074 + switch (speed) {
1075 + case 0:
1076 + link->speed = SWITCH_PORT_SPEED_10;
1077 + break;
1078 + case 1:
1079 + link->speed = SWITCH_PORT_SPEED_100;
1080 + break;
1081 + case 2:
1082 + case 3: /* forced gige speed can be 2 or 3 */
1083 + link->speed = SWITCH_PORT_SPEED_1000;
1084 + break;
1085 + default:
1086 + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
1087 + break;
1088 + }
1089 +
1090 + return 0;
1091 +}
1092 +
1093 +static int esw_get_port_bool(struct switch_dev *dev,
1094 + const struct switch_attr *attr,
1095 + struct switch_val *val)
1096 +{
1097 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1098 + int idx = val->port_vlan;
1099 + u32 x, reg, shift;
1100 +
1101 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS)
1102 + return -EINVAL;
1103 +
1104 + switch (attr->id) {
1105 + case RT305X_ESW_ATTR_PORT_DISABLE:
1106 + reg = RT305X_ESW_REG_POC0;
1107 + shift = RT305X_ESW_POC0_DIS_PORT_S;
1108 + break;
1109 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1110 + reg = RT305X_ESW_REG_SGC2;
1111 + shift = RT305X_ESW_SGC2_DOUBLE_TAG_S;
1112 + break;
1113 + case RT305X_ESW_ATTR_PORT_UNTAG:
1114 + reg = RT305X_ESW_REG_POC2;
1115 + shift = RT305X_ESW_POC2_UNTAG_EN_S;
1116 + break;
1117 + case RT305X_ESW_ATTR_PORT_LAN:
1118 + reg = RT305X_ESW_REG_SGC2;
1119 + shift = RT305X_ESW_SGC2_LAN_PMAP_S;
1120 + if (idx >= RT305X_ESW_NUM_LANWAN)
1121 + return -EINVAL;
1122 + break;
1123 + default:
1124 + return -EINVAL;
1125 + }
1126 +
1127 + x = esw_r32(esw, reg);
1128 + val->value.i = (x >> (idx + shift)) & 1;
1129 +
1130 + return 0;
1131 +}
1132 +
1133 +static int esw_set_port_bool(struct switch_dev *dev,
1134 + const struct switch_attr *attr,
1135 + struct switch_val *val)
1136 +{
1137 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1138 + int idx = val->port_vlan;
1139 +
1140 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1141 + val->value.i < 0 || val->value.i > 1)
1142 + return -EINVAL;
1143 +
1144 + switch (attr->id) {
1145 + case RT305X_ESW_ATTR_PORT_DISABLE:
1146 + esw->ports[idx].disable = val->value.i;
1147 + break;
1148 + case RT305X_ESW_ATTR_PORT_DOUBLETAG:
1149 + esw->ports[idx].doubletag = val->value.i;
1150 + break;
1151 + case RT305X_ESW_ATTR_PORT_UNTAG:
1152 + esw->ports[idx].untag = val->value.i;
1153 + break;
1154 + default:
1155 + return -EINVAL;
1156 + }
1157 +
1158 + return 0;
1159 +}
1160 +
1161 +static int esw_get_port_recv_badgood(struct switch_dev *dev,
1162 + const struct switch_attr *attr,
1163 + struct switch_val *val)
1164 +{
1165 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1166 + int idx = val->port_vlan;
1167 + int shift = attr->id == RT305X_ESW_ATTR_PORT_RECV_GOOD ? 0 : 16;
1168 + u32 reg;
1169 +
1170 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1171 + return -EINVAL;
1172 + reg = esw_r32(esw, RT305X_ESW_REG_PXPC(idx));
1173 + val->value.i = (reg >> shift) & 0xffff;
1174 +
1175 + return 0;
1176 +}
1177 +
1178 +static int
1179 +esw_get_port_tr_badgood(struct switch_dev *dev,
1180 + const struct switch_attr *attr,
1181 + struct switch_val *val)
1182 +{
1183 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1184 +
1185 + int idx = val->port_vlan;
1186 + int shift = attr->id == RT5350_ESW_ATTR_PORT_TR_GOOD ? 0 : 16;
1187 + u32 reg;
1188 +
1189 + if (!soc_is_rt5350())
1190 + return -EINVAL;
1191 +
1192 + if (idx < 0 || idx >= RT305X_ESW_NUM_LANWAN)
1193 + return -EINVAL;
1194 +
1195 + reg = esw_r32(esw, RT5350_ESW_REG_PXTPC(idx));
1196 + val->value.i = (reg >> shift) & 0xffff;
1197 +
1198 + return 0;
1199 +}
1200 +
1201 +static int esw_get_port_led(struct switch_dev *dev,
1202 + const struct switch_attr *attr,
1203 + struct switch_val *val)
1204 +{
1205 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1206 + int idx = val->port_vlan;
1207 +
1208 + if (idx < 0 || idx >= RT305X_ESW_NUM_PORTS ||
1209 + idx >= RT305X_ESW_NUM_LEDS)
1210 + return -EINVAL;
1211 +
1212 + val->value.i = esw_r32(esw, RT305X_ESW_REG_P0LED + 4*idx);
1213 +
1214 + return 0;
1215 +}
1216 +
1217 +static int esw_set_port_led(struct switch_dev *dev,
1218 + const struct switch_attr *attr,
1219 + struct switch_val *val)
1220 +{
1221 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1222 + int idx = val->port_vlan;
1223 +
1224 + if (idx < 0 || idx >= RT305X_ESW_NUM_LEDS)
1225 + return -EINVAL;
1226 +
1227 + esw->ports[idx].led = val->value.i;
1228 +
1229 + return 0;
1230 +}
1231 +
1232 +static int esw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1233 +{
1234 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1235 +
1236 + if (port >= RT305X_ESW_NUM_PORTS)
1237 + return -EINVAL;
1238 +
1239 + *val = esw_get_pvid(esw, port);
1240 +
1241 + return 0;
1242 +}
1243 +
1244 +static int esw_set_port_pvid(struct switch_dev *dev, int port, int val)
1245 +{
1246 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1247 +
1248 + if (port >= RT305X_ESW_NUM_PORTS)
1249 + return -EINVAL;
1250 +
1251 + esw->ports[port].pvid = val;
1252 +
1253 + return 0;
1254 +}
1255 +
1256 +static int esw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1257 +{
1258 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1259 + u32 vmsc, poc2;
1260 + int vlan_idx = -1;
1261 + int i;
1262 +
1263 + val->len = 0;
1264 +
1265 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS)
1266 + return -EINVAL;
1267 +
1268 + /* valid vlan? */
1269 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1270 + if (esw_get_vlan_id(esw, i) == val->port_vlan &&
1271 + esw_get_vmsc(esw, i) != RT305X_ESW_PORTS_NONE) {
1272 + vlan_idx = i;
1273 + break;
1274 + }
1275 + }
1276 +
1277 + if (vlan_idx == -1)
1278 + return -EINVAL;
1279 +
1280 + vmsc = esw_get_vmsc(esw, vlan_idx);
1281 + poc2 = esw_r32(esw, RT305X_ESW_REG_POC2);
1282 +
1283 + for (i = 0; i < RT305X_ESW_NUM_PORTS; i++) {
1284 + struct switch_port *p;
1285 + int port_mask = 1 << i;
1286 +
1287 + if (!(vmsc & port_mask))
1288 + continue;
1289 +
1290 + p = &val->value.ports[val->len++];
1291 + p->id = i;
1292 + if (poc2 & (port_mask << RT305X_ESW_POC2_UNTAG_EN_S))
1293 + p->flags = 0;
1294 + else
1295 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
1296 + }
1297 +
1298 + return 0;
1299 +}
1300 +
1301 +static int esw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1302 +{
1303 + struct rt305x_esw *esw = container_of(dev, struct rt305x_esw, swdev);
1304 + int ports;
1305 + int vlan_idx = -1;
1306 + int i;
1307 +
1308 + if (val->port_vlan < 0 || val->port_vlan >= RT305X_ESW_NUM_VIDS ||
1309 + val->len > RT305X_ESW_NUM_PORTS)
1310 + return -EINVAL;
1311 +
1312 + /* one of the already defined vlans? */
1313 + for (i = 0; i < RT305X_ESW_NUM_VLANS; i++) {
1314 + if (esw->vlans[i].vid == val->port_vlan &&
1315 + esw->vlans[i].ports != RT305X_ESW_PORTS_NONE) {
1316 + vlan_idx = i;
1317 + break;
1318 + }
1319 + }
1320 +
1321 + /* select a free slot */
1322 + for (i = 0; vlan_idx == -1 && i < RT305X_ESW_NUM_VLANS; i++) {
1323 + if (esw->vlans[i].ports == RT305X_ESW_PORTS_NONE)
1324 + vlan_idx = i;
1325 + }
1326 +
1327 + /* bail if all slots are in use */
1328 + if (vlan_idx == -1)
1329 + return -EINVAL;
1330 +
1331 + ports = RT305X_ESW_PORTS_NONE;
1332 + for (i = 0; i < val->len; i++) {
1333 + struct switch_port *p = &val->value.ports[i];
1334 + int port_mask = 1 << p->id;
1335 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
1336 +
1337 + if (p->id >= RT305X_ESW_NUM_PORTS)
1338 + return -EINVAL;
1339 +
1340 + ports |= port_mask;
1341 + esw->ports[p->id].untag = untagged;
1342 + }
1343 + esw->vlans[vlan_idx].ports = ports;
1344 + if (ports == RT305X_ESW_PORTS_NONE)
1345 + esw->vlans[vlan_idx].vid = RT305X_ESW_VLAN_NONE;
1346 + else
1347 + esw->vlans[vlan_idx].vid = val->port_vlan;
1348 +
1349 + return 0;
1350 +}
1351 +
1352 +static const struct switch_attr esw_global[] = {
1353 + {
1354 + .type = SWITCH_TYPE_INT,
1355 + .name = "enable_vlan",
1356 + .description = "VLAN mode (1:enabled)",
1357 + .max = 1,
1358 + .id = RT305X_ESW_ATTR_ENABLE_VLAN,
1359 + .get = esw_get_vlan_enable,
1360 + .set = esw_set_vlan_enable,
1361 + },
1362 + {
1363 + .type = SWITCH_TYPE_INT,
1364 + .name = "alternate_vlan_disable",
1365 + .description = "Use en_vlan instead of doubletag to disable"
1366 + " VLAN mode",
1367 + .max = 1,
1368 + .id = RT305X_ESW_ATTR_ALT_VLAN_DISABLE,
1369 + .get = esw_get_alt_vlan_disable,
1370 + .set = esw_set_alt_vlan_disable,
1371 + },
1372 + {
1373 + .type = SWITCH_TYPE_INT,
1374 + .name = "bc_storm_protect",
1375 + .description = "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1376 + .max = 3,
1377 + .id = RT305X_ESW_ATTR_BC_STATUS,
1378 + .get = rt305x_esw_get_bc_status,
1379 + .set = rt305x_esw_set_bc_status,
1380 + },
1381 + {
1382 + .type = SWITCH_TYPE_INT,
1383 + .name = "led_frequency",
1384 + .description = "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1385 + .max = 3,
1386 + .id = RT305X_ESW_ATTR_LED_FREQ,
1387 + .get = rt305x_esw_get_led_freq,
1388 + .set = rt305x_esw_set_led_freq,
1389 + }
1390 +};
1391 +
1392 +static const struct switch_attr esw_port[] = {
1393 + {
1394 + .type = SWITCH_TYPE_INT,
1395 + .name = "disable",
1396 + .description = "Port state (1:disabled)",
1397 + .max = 1,
1398 + .id = RT305X_ESW_ATTR_PORT_DISABLE,
1399 + .get = esw_get_port_bool,
1400 + .set = esw_set_port_bool,
1401 + },
1402 + {
1403 + .type = SWITCH_TYPE_INT,
1404 + .name = "doubletag",
1405 + .description = "Double tagging for incoming vlan packets "
1406 + "(1:enabled)",
1407 + .max = 1,
1408 + .id = RT305X_ESW_ATTR_PORT_DOUBLETAG,
1409 + .get = esw_get_port_bool,
1410 + .set = esw_set_port_bool,
1411 + },
1412 + {
1413 + .type = SWITCH_TYPE_INT,
1414 + .name = "untag",
1415 + .description = "Untag (1:strip outgoing vlan tag)",
1416 + .max = 1,
1417 + .id = RT305X_ESW_ATTR_PORT_UNTAG,
1418 + .get = esw_get_port_bool,
1419 + .set = esw_set_port_bool,
1420 + },
1421 + {
1422 + .type = SWITCH_TYPE_INT,
1423 + .name = "led",
1424 + .description = "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1425 + " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1426 + " 8:100mact, 10:blink, 11:off, 12:on)",
1427 + .max = 15,
1428 + .id = RT305X_ESW_ATTR_PORT_LED,
1429 + .get = esw_get_port_led,
1430 + .set = esw_set_port_led,
1431 + },
1432 + {
1433 + .type = SWITCH_TYPE_INT,
1434 + .name = "lan",
1435 + .description = "HW port group (0:wan, 1:lan)",
1436 + .max = 1,
1437 + .id = RT305X_ESW_ATTR_PORT_LAN,
1438 + .get = esw_get_port_bool,
1439 + },
1440 + {
1441 + .type = SWITCH_TYPE_INT,
1442 + .name = "recv_bad",
1443 + .description = "Receive bad packet counter",
1444 + .id = RT305X_ESW_ATTR_PORT_RECV_BAD,
1445 + .get = esw_get_port_recv_badgood,
1446 + },
1447 + {
1448 + .type = SWITCH_TYPE_INT,
1449 + .name = "recv_good",
1450 + .description = "Receive good packet counter",
1451 + .id = RT305X_ESW_ATTR_PORT_RECV_GOOD,
1452 + .get = esw_get_port_recv_badgood,
1453 + },
1454 + {
1455 + .type = SWITCH_TYPE_INT,
1456 + .name = "tr_bad",
1457 +
1458 + .description = "Transmit bad packet counter. rt5350 only",
1459 + .id = RT5350_ESW_ATTR_PORT_TR_BAD,
1460 + .get = esw_get_port_tr_badgood,
1461 + },
1462 + {
1463 + .type = SWITCH_TYPE_INT,
1464 + .name = "tr_good",
1465 +
1466 + .description = "Transmit good packet counter. rt5350 only",
1467 + .id = RT5350_ESW_ATTR_PORT_TR_GOOD,
1468 + .get = esw_get_port_tr_badgood,
1469 + },
1470 +};
1471 +
1472 +static const struct switch_attr esw_vlan[] = {
1473 +};
1474 +
1475 +static const struct switch_dev_ops esw_ops = {
1476 + .attr_global = {
1477 + .attr = esw_global,
1478 + .n_attr = ARRAY_SIZE(esw_global),
1479 + },
1480 + .attr_port = {
1481 + .attr = esw_port,
1482 + .n_attr = ARRAY_SIZE(esw_port),
1483 + },
1484 + .attr_vlan = {
1485 + .attr = esw_vlan,
1486 + .n_attr = ARRAY_SIZE(esw_vlan),
1487 + },
1488 + .get_vlan_ports = esw_get_vlan_ports,
1489 + .set_vlan_ports = esw_set_vlan_ports,
1490 + .get_port_pvid = esw_get_port_pvid,
1491 + .set_port_pvid = esw_set_port_pvid,
1492 + .get_port_link = esw_get_port_link,
1493 + .apply_config = esw_apply_config,
1494 + .reset_switch = esw_reset_switch,
1495 +};
1496 +
1497 +static struct rt305x_esw_platform_data rt3050_esw_data = {
1498 + /* All ports are LAN ports. */
1499 + .vlan_config = RT305X_ESW_VLAN_CONFIG_NONE,
1500 + .reg_initval_fct2 = 0x00d6500c,
1501 + /*
1502 + * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1503 + * turbo mii off, rgmi 3.3v off
1504 + * port5: disabled
1505 + * port6: enabled, gige, full-duplex, rx/tx-flow-control
1506 + */
1507 + .reg_initval_fpa2 = 0x3f502b28,
1508 +};
1509 +
1510 +static const struct of_device_id ralink_esw_match[] = {
1511 + { .compatible = "ralink,rt3050-esw", .data = &rt3050_esw_data },
1512 + {},
1513 +};
1514 +MODULE_DEVICE_TABLE(of, ralink_esw_match);
1515 +
1516 +static int esw_probe(struct platform_device *pdev)
1517 +{
1518 + struct device_node *np = pdev->dev.of_node;
1519 + const struct rt305x_esw_platform_data *pdata;
1520 + const __be32 *port_map, *reg_init;
1521 + struct rt305x_esw *esw;
1522 + struct switch_dev *swdev;
1523 + struct resource *res, *irq;
1524 + int err;
1525 +
1526 + pdata = pdev->dev.platform_data;
1527 + if (!pdata) {
1528 + const struct of_device_id *match;
1529 + match = of_match_device(ralink_esw_match, &pdev->dev);
1530 + if (match)
1531 + pdata = (struct rt305x_esw_platform_data *) match->data;
1532 + }
1533 + if (!pdata)
1534 + return -EINVAL;
1535 +
1536 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1537 + if (!res) {
1538 + dev_err(&pdev->dev, "no memory resource found\n");
1539 + return -ENOMEM;
1540 + }
1541 +
1542 + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1543 + if (!irq) {
1544 + dev_err(&pdev->dev, "no irq resource found\n");
1545 + return -ENOMEM;
1546 + }
1547 +
1548 + esw = kzalloc(sizeof(struct rt305x_esw), GFP_KERNEL);
1549 + if (!esw) {
1550 + dev_err(&pdev->dev, "no memory for private data\n");
1551 + return -ENOMEM;
1552 + }
1553 +
1554 + esw->dev = &pdev->dev;
1555 + esw->irq = irq->start;
1556 + esw->base = ioremap(res->start, resource_size(res));
1557 + if (!esw->base) {
1558 + dev_err(&pdev->dev, "ioremap failed\n");
1559 + err = -ENOMEM;
1560 + goto free_esw;
1561 + }
1562 +
1563 + port_map = of_get_property(np, "ralink,portmap", NULL);
1564 + if (port_map)
1565 + esw->port_map = be32_to_cpu(*port_map);
1566 +
1567 + reg_init = of_get_property(np, "ralink,fct2", NULL);
1568 + if (reg_init)
1569 + esw->reg_initval_fct2 = be32_to_cpu(*reg_init);
1570 +
1571 + reg_init = of_get_property(np, "ralink,fpa2", NULL);
1572 + if (reg_init)
1573 + esw->reg_initval_fpa2 = be32_to_cpu(*reg_init);
1574 +
1575 + reg_init = of_get_property(np, "ralink,led_polarity", NULL);
1576 + if (reg_init)
1577 + esw->reg_led_polarity = be32_to_cpu(*reg_init);
1578 +
1579 + swdev = &esw->swdev;
1580 + swdev->of_node = pdev->dev.of_node;
1581 + swdev->name = "rt305x-esw";
1582 + swdev->alias = "rt305x";
1583 + swdev->cpu_port = RT305X_ESW_PORT6;
1584 + swdev->ports = RT305X_ESW_NUM_PORTS;
1585 + swdev->vlans = RT305X_ESW_NUM_VIDS;
1586 + swdev->ops = &esw_ops;
1587 +
1588 + err = register_switch(swdev, NULL);
1589 + if (err < 0) {
1590 + dev_err(&pdev->dev, "register_switch failed\n");
1591 + goto unmap_base;
1592 + }
1593 +
1594 + platform_set_drvdata(pdev, esw);
1595 +
1596 + esw->pdata = pdata;
1597 + spin_lock_init(&esw->reg_rw_lock);
1598 +
1599 + esw_hw_init(esw);
1600 +
1601 + esw_w32(esw, RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_ISR);
1602 + esw_w32(esw, ~RT305X_ESW_PORT_ST_CHG, RT305X_ESW_REG_IMR);
1603 + request_irq(esw->irq, esw_interrupt, 0, "esw", esw);
1604 +
1605 + return 0;
1606 +
1607 +unmap_base:
1608 + iounmap(esw->base);
1609 +free_esw:
1610 + kfree(esw);
1611 + return err;
1612 +}
1613 +
1614 +static int esw_remove(struct platform_device *pdev)
1615 +{
1616 + struct rt305x_esw *esw;
1617 +
1618 + esw = platform_get_drvdata(pdev);
1619 + if (esw) {
1620 + unregister_switch(&esw->swdev);
1621 + platform_set_drvdata(pdev, NULL);
1622 + iounmap(esw->base);
1623 + kfree(esw);
1624 + }
1625 +
1626 + return 0;
1627 +}
1628 +
1629 +static struct platform_driver esw_driver = {
1630 + .probe = esw_probe,
1631 + .remove = esw_remove,
1632 + .driver = {
1633 + .name = "rt305x-esw",
1634 + .owner = THIS_MODULE,
1635 + .of_match_table = ralink_esw_match,
1636 + },
1637 +};
1638 +
1639 +int __init rtesw_init(void)
1640 +{
1641 + return platform_driver_register(&esw_driver);
1642 +}
1643 +
1644 +void rtesw_exit(void)
1645 +{
1646 + platform_driver_unregister(&esw_driver);
1647 +}
1648 diff --git a/drivers/net/ethernet/ralink/esw_rt3052.h b/drivers/net/ethernet/ralink/esw_rt3052.h
1649 new file mode 100644
1650 index 0000000..2ced3dff
1651 --- /dev/null
1652 +++ b/drivers/net/ethernet/ralink/esw_rt3052.h
1653 @@ -0,0 +1,32 @@
1654 +/*
1655 + * This program is free software; you can redistribute it and/or modify
1656 + * it under the terms of the GNU General Public License as published by
1657 + * the Free Software Foundation; version 2 of the License
1658 + *
1659 + * This program is distributed in the hope that it will be useful,
1660 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1661 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1662 + * GNU General Public License for more details.
1663 + *
1664 + * You should have received a copy of the GNU General Public License
1665 + * along with this program; if not, write to the Free Software
1666 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1667 + *
1668 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1669 + */
1670 +
1671 +#ifndef _RALINK_ESW_RT3052_H__
1672 +#define _RALINK_ESW_RT3052_H__
1673 +
1674 +#ifdef CONFIG_NET_RALINK_ESW_RT3052
1675 +
1676 +int __init rtesw_init(void);
1677 +void rtesw_exit(void);
1678 +
1679 +#else
1680 +
1681 +static inline int __init rtesw_init(void) { return 0; }
1682 +static inline void rtesw_exit(void) { }
1683 +
1684 +#endif
1685 +#endif
1686 diff --git a/drivers/net/ethernet/ralink/gsw_mt7620a.c b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1687 new file mode 100644
1688 index 0000000..9fa6a54
1689 --- /dev/null
1690 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.c
1691 @@ -0,0 +1,1027 @@
1692 +/*
1693 + * This program is free software; you can redistribute it and/or modify
1694 + * it under the terms of the GNU General Public License as published by
1695 + * the Free Software Foundation; version 2 of the License
1696 + *
1697 + * This program is distributed in the hope that it will be useful,
1698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1700 + * GNU General Public License for more details.
1701 + *
1702 + * You should have received a copy of the GNU General Public License
1703 + * along with this program; if not, write to the Free Software
1704 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
1705 + *
1706 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
1707 + */
1708 +
1709 +#include <linux/module.h>
1710 +#include <linux/kernel.h>
1711 +#include <linux/types.h>
1712 +#include <linux/dma-mapping.h>
1713 +#include <linux/init.h>
1714 +#include <linux/skbuff.h>
1715 +#include <linux/etherdevice.h>
1716 +#include <linux/ethtool.h>
1717 +#include <linux/platform_device.h>
1718 +#include <linux/of_device.h>
1719 +#include <linux/clk.h>
1720 +#include <linux/of_net.h>
1721 +#include <linux/of_mdio.h>
1722 +#include <linux/of_irq.h>
1723 +#include <linux/of_address.h>
1724 +#include <linux/switch.h>
1725 +
1726 +#include <asm/mach-ralink/ralink_regs.h>
1727 +
1728 +#include "ralink_soc_eth.h"
1729 +
1730 +#include <linux/ioport.h>
1731 +#include <linux/switch.h>
1732 +#include <linux/mii.h>
1733 +
1734 +#include <ralink_regs.h>
1735 +#include <asm/mach-ralink/mt7620.h>
1736 +
1737 +#include "ralink_soc_eth.h"
1738 +#include "gsw_mt7620a.h"
1739 +#include "mdio.h"
1740 +
1741 +#define GSW_REG_PHY_TIMEOUT (5 * HZ)
1742 +
1743 +#define MT7620A_GSW_REG_PIAC 0x7004
1744 +
1745 +#define GSW_NUM_VLANS 16
1746 +#define GSW_NUM_VIDS 4096
1747 +#define GSW_NUM_PORTS 7
1748 +#define GSW_PORT6 6
1749 +
1750 +#define GSW_MDIO_ACCESS BIT(31)
1751 +#define GSW_MDIO_READ BIT(19)
1752 +#define GSW_MDIO_WRITE BIT(18)
1753 +#define GSW_MDIO_START BIT(16)
1754 +#define GSW_MDIO_ADDR_SHIFT 20
1755 +#define GSW_MDIO_REG_SHIFT 25
1756 +
1757 +#define GSW_REG_PORT_PMCR(x) (0x3000 + (x * 0x100))
1758 +#define GSW_REG_PORT_STATUS(x) (0x3008 + (x * 0x100))
1759 +#define GSW_REG_SMACCR0 0x3fE4
1760 +#define GSW_REG_SMACCR1 0x3fE8
1761 +#define GSW_REG_CKGCR 0x3ff0
1762 +
1763 +#define GSW_REG_IMR 0x7008
1764 +#define GSW_REG_ISR 0x700c
1765 +
1766 +#define SYSC_REG_CFG1 0x14
1767 +
1768 +#define PORT_IRQ_ST_CHG 0x7f
1769 +
1770 +#define GSW_VLAN_VTCR 0x90
1771 +#define GSW_VLAN_VTCR_VID_M 0xfff
1772 +#define GSW_VLAN_ID(_x) (0x100 + (4 * (_x)))
1773 +#define GSW_VLAN_ID_VID_S 12
1774 +#define GSW_VLAN_ID_VID_M 0xfff
1775 +
1776 +#define GSW_VAWD1 0x94
1777 +#define GSW_VAWD1_VTAG_EN BIT(28)
1778 +#define GSW_VAWD1_PORTM_S 16
1779 +#define GSW_VAWD1_PORTM_M 0xff
1780 +
1781 +#define GSW_VAWD2 0x98
1782 +#define GSW_VAWD2_PORTT_S 16
1783 +#define GSW_VAWD2_PORTT_M 0xff
1784 +
1785 +#define GSW_VTIM(_x) (0x100 + (4 * (_x)))
1786 +#define GSW_VTIM_M 0xfff
1787 +#define GSW_VTIM_S 12
1788 +
1789 +#define GSW_REG_PCR(x) (0x2004 + (x * 0x100))
1790 +#define GSW_REG_PCR_EG_TAG_S 28
1791 +#define GSW_REG_PCR_EG_TAG_M 0x3
1792 +
1793 +#define SYSCFG1 0x14
1794 +
1795 +#define ESW_PHY_POLLING 0x7000
1796 +
1797 +#define PMCR_IPG BIT(18)
1798 +#define PMCR_MAC_MODE BIT(16)
1799 +#define PMCR_FORCE BIT(15)
1800 +#define PMCR_TX_EN BIT(14)
1801 +#define PMCR_RX_EN BIT(13)
1802 +#define PMCR_BACKOFF BIT(9)
1803 +#define PMCR_BACKPRES BIT(8)
1804 +#define PMCR_RX_FC BIT(5)
1805 +#define PMCR_TX_FC BIT(4)
1806 +#define PMCR_SPEED(_x) (_x << 2)
1807 +#define PMCR_DUPLEX BIT(1)
1808 +#define PMCR_LINK BIT(0)
1809 +
1810 +#define PHY_AN_EN BIT(31)
1811 +#define PHY_PRE_EN BIT(30)
1812 +#define PMY_MDC_CONF(_x) ((_x & 0x3f) << 24)
1813 +
1814 +enum {
1815 + /* Global attributes. */
1816 + GSW_ATTR_ENABLE_VLAN,
1817 + /* Port attributes. */
1818 + GSW_ATTR_PORT_UNTAG,
1819 +};
1820 +
1821 +enum {
1822 + PORT4_EPHY = 0,
1823 + PORT4_EXT,
1824 +};
1825 +
1826 +struct gsw_port {
1827 + bool disable;
1828 + bool untag;
1829 + u16 pvid;
1830 +};
1831 +
1832 +struct gsw_vlan {
1833 + u8 ports;
1834 + u16 vid;
1835 +};
1836 +
1837 +struct mt7620_gsw {
1838 + struct device *dev;
1839 + void __iomem *base;
1840 + int irq;
1841 +
1842 + struct switch_dev swdev;
1843 + bool global_vlan_enable;
1844 + struct gsw_vlan vlans[GSW_NUM_VLANS];
1845 + struct gsw_port ports[GSW_NUM_PORTS];
1846 + long unsigned int autopoll;
1847 + int port4;
1848 +};
1849 +
1850 +static inline void gsw_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
1851 +{
1852 + iowrite32(val, gsw->base + reg);
1853 +}
1854 +
1855 +static inline u32 gsw_r32(struct mt7620_gsw *gsw, unsigned reg)
1856 +{
1857 + return ioread32(gsw->base + reg);
1858 +}
1859 +
1860 +static int mt7620_mii_busy_wait(struct mt7620_gsw *gsw)
1861 +{
1862 + unsigned long t_start = jiffies;
1863 +
1864 + while (1) {
1865 + if (!(gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & GSW_MDIO_ACCESS))
1866 + return 0;
1867 + if (time_after(jiffies, t_start + GSW_REG_PHY_TIMEOUT)) {
1868 + break;
1869 + }
1870 + }
1871 +
1872 + printk(KERN_ERR "mdio: MDIO timeout\n");
1873 + return -1;
1874 +}
1875 +
1876 +static u32 _mt7620_mii_write(struct mt7620_gsw *gsw, u32 phy_addr, u32 phy_register,
1877 + u32 write_data)
1878 +{
1879 + if (mt7620_mii_busy_wait(gsw))
1880 + return -1;
1881 +
1882 + write_data &= 0xffff;
1883 +
1884 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_WRITE |
1885 + (phy_register << GSW_MDIO_REG_SHIFT) |
1886 + (phy_addr << GSW_MDIO_ADDR_SHIFT) | write_data,
1887 + MT7620A_GSW_REG_PIAC);
1888 +
1889 + if (mt7620_mii_busy_wait(gsw))
1890 + return -1;
1891 +
1892 + return 0;
1893 +}
1894 +
1895 +int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
1896 +{
1897 + struct fe_priv *priv = bus->priv;
1898 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1899 +
1900 + return _mt7620_mii_write(gsw, phy_addr, phy_reg, val);
1901 +}
1902 +
1903 +int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
1904 +{
1905 + struct fe_priv *priv = bus->priv;
1906 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1907 + u32 d;
1908 +
1909 + if (mt7620_mii_busy_wait(gsw))
1910 + return 0xffff;
1911 +
1912 + gsw_w32(gsw, GSW_MDIO_ACCESS | GSW_MDIO_START | GSW_MDIO_READ |
1913 + (phy_reg << GSW_MDIO_REG_SHIFT) |
1914 + (phy_addr << GSW_MDIO_ADDR_SHIFT),
1915 + MT7620A_GSW_REG_PIAC);
1916 +
1917 + if (mt7620_mii_busy_wait(gsw))
1918 + return 0xffff;
1919 +
1920 + d = gsw_r32(gsw, MT7620A_GSW_REG_PIAC) & 0xffff;
1921 +
1922 + return d;
1923 +}
1924 +
1925 +static unsigned char *fe_speed_str(int speed)
1926 +{
1927 + switch (speed) {
1928 + case 2:
1929 + case SPEED_1000:
1930 + return "1000";
1931 + case 1:
1932 + case SPEED_100:
1933 + return "100";
1934 + case 0:
1935 + case SPEED_10:
1936 + return "10";
1937 + }
1938 +
1939 + return "? ";
1940 +}
1941 +
1942 +int mt7620a_has_carrier(struct fe_priv *priv)
1943 +{
1944 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1945 + int i;
1946 +
1947 + for (i = 0; i < GSW_PORT6; i++)
1948 + if (gsw_r32(gsw, GSW_REG_PORT_STATUS(i)) & 0x1)
1949 + return 1;
1950 + return 0;
1951 +}
1952 +
1953 +static void mt7620a_handle_carrier(struct fe_priv *priv)
1954 +{
1955 + if (!priv->phy)
1956 + return;
1957 +
1958 + if (mt7620a_has_carrier(priv))
1959 + netif_carrier_on(priv->netdev);
1960 + else
1961 + netif_carrier_off(priv->netdev);
1962 +}
1963 +
1964 +void mt7620_mdio_link_adjust(struct fe_priv *priv, int port)
1965 +{
1966 + if (priv->link[port])
1967 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1968 + port, fe_speed_str(priv->phy->speed[port]),
1969 + (DUPLEX_FULL == priv->phy->duplex[port]) ? "Full" : "Half");
1970 + else
1971 + netdev_info(priv->netdev, "port %d link down\n", port);
1972 + mt7620a_handle_carrier(priv);
1973 +}
1974 +
1975 +static irqreturn_t gsw_interrupt(int irq, void *_priv)
1976 +{
1977 + struct fe_priv *priv = (struct fe_priv *) _priv;
1978 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
1979 + u32 status;
1980 + int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
1981 +
1982 + status = gsw_r32(gsw, GSW_REG_ISR);
1983 + if (status & PORT_IRQ_ST_CHG)
1984 + for (i = 0; i <= max; i++) {
1985 + u32 status = gsw_r32(gsw, GSW_REG_PORT_STATUS(i));
1986 + int link = status & 0x1;
1987 +
1988 + if (link != priv->link[i]) {
1989 + if (link)
1990 + netdev_info(priv->netdev, "port %d link up (%sMbps/%s duplex)\n",
1991 + i, fe_speed_str((status >> 2) & 3),
1992 + (status & 0x2) ? "Full" : "Half");
1993 + else
1994 + netdev_info(priv->netdev, "port %d link down\n", i);
1995 + }
1996 +
1997 + priv->link[i] = link;
1998 + }
1999 + mt7620a_handle_carrier(priv);
2000 +
2001 + gsw_w32(gsw, status, GSW_REG_ISR);
2002 +
2003 + return IRQ_HANDLED;
2004 +}
2005 +
2006 +static int mt7620_is_bga(void)
2007 +{
2008 + u32 bga = rt_sysc_r32(0x0c);
2009 +
2010 + return (bga >> 16) & 1;
2011 +}
2012 +
2013 +static void gsw_auto_poll(struct mt7620_gsw *gsw)
2014 +{
2015 + int phy;
2016 + int lsb = -1, msb = 0;
2017 +
2018 + for_each_set_bit(phy, &gsw->autopoll, 32) {
2019 + if (lsb < 0)
2020 + lsb = phy;
2021 + msb = phy;
2022 + }
2023 +
2024 + gsw_w32(gsw, PHY_AN_EN | PHY_PRE_EN | PMY_MDC_CONF(5) | (msb << 8) | lsb, ESW_PHY_POLLING);
2025 +}
2026 +
2027 +void mt7620_port_init(struct fe_priv *priv, struct device_node *np)
2028 +{
2029 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2030 + const __be32 *_id = of_get_property(np, "reg", NULL);
2031 + int phy_mode, size, id;
2032 + int shift = 12;
2033 + u32 val, mask = 0;
2034 + int min = (gsw->port4 == PORT4_EPHY) ? (5) : (4);
2035 +
2036 + if (!_id || (be32_to_cpu(*_id) < min) || (be32_to_cpu(*_id) > 5)) {
2037 + if (_id)
2038 + pr_err("%s: invalid port id %d\n", np->name, be32_to_cpu(*_id));
2039 + else
2040 + pr_err("%s: invalid port id\n", np->name);
2041 + return;
2042 + }
2043 +
2044 + id = be32_to_cpu(*_id);
2045 +
2046 + if (id == 4)
2047 + shift = 14;
2048 +
2049 + priv->phy->phy_fixed[id] = of_get_property(np, "ralink,fixed-link", &size);
2050 + if (priv->phy->phy_fixed[id] && (size != (4 * sizeof(*priv->phy->phy_fixed[id])))) {
2051 + pr_err("%s: invalid fixed link property\n", np->name);
2052 + priv->phy->phy_fixed[id] = NULL;
2053 + return;
2054 + }
2055 +
2056 + phy_mode = of_get_phy_mode(np);
2057 + switch (phy_mode) {
2058 + case PHY_INTERFACE_MODE_RGMII:
2059 + mask = 0;
2060 + break;
2061 + case PHY_INTERFACE_MODE_MII:
2062 + mask = 1;
2063 + break;
2064 + case PHY_INTERFACE_MODE_RMII:
2065 + mask = 2;
2066 + break;
2067 + default:
2068 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[id]);
2069 + return;
2070 + }
2071 +
2072 + priv->phy->phy_node[id] = of_parse_phandle(np, "phy-handle", 0);
2073 + if (!priv->phy->phy_node[id] && !priv->phy->phy_fixed[id])
2074 + return;
2075 +
2076 + val = rt_sysc_r32(SYSCFG1);
2077 + val &= ~(3 << shift);
2078 + val |= mask << shift;
2079 + rt_sysc_w32(val, SYSCFG1);
2080 +
2081 + if (priv->phy->phy_fixed[id]) {
2082 + const __be32 *link = priv->phy->phy_fixed[id];
2083 + int tx_fc = be32_to_cpup(link++);
2084 + int rx_fc = be32_to_cpup(link++);
2085 + u32 val = 0;
2086 +
2087 + priv->phy->speed[id] = be32_to_cpup(link++);
2088 + priv->phy->duplex[id] = be32_to_cpup(link++);
2089 + priv->link[id] = 1;
2090 +
2091 + switch (priv->phy->speed[id]) {
2092 + case SPEED_10:
2093 + val = 0;
2094 + break;
2095 + case SPEED_100:
2096 + val = 1;
2097 + break;
2098 + case SPEED_1000:
2099 + val = 2;
2100 + break;
2101 + default:
2102 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[id]);
2103 + priv->phy->phy_fixed[id] = 0;
2104 + return;
2105 + }
2106 + val = PMCR_SPEED(val);
2107 + val |= PMCR_LINK | PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2108 + PMCR_TX_EN | PMCR_FORCE | PMCR_MAC_MODE | PMCR_IPG;
2109 + if (tx_fc)
2110 + val |= PMCR_TX_FC;
2111 + if (rx_fc)
2112 + val |= PMCR_RX_FC;
2113 + if (priv->phy->duplex[id])
2114 + val |= PMCR_DUPLEX;
2115 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2116 + dev_info(priv->device, "using fixed link parameters\n");
2117 + return;
2118 + }
2119 +
2120 + if (priv->phy->phy_node[id] && priv->mii_bus->phy_map[id]) {
2121 + u32 val = PMCR_BACKPRES | PMCR_BACKOFF | PMCR_RX_EN |
2122 + PMCR_TX_EN | PMCR_MAC_MODE | PMCR_IPG;
2123 +
2124 + gsw_w32(gsw, val, GSW_REG_PORT_PMCR(id));
2125 + fe_connect_phy_node(priv, priv->phy->phy_node[id]);
2126 + gsw->autopoll |= BIT(id);
2127 + gsw_auto_poll(gsw);
2128 + return;
2129 + }
2130 +}
2131 +
2132 +static void gsw_hw_init(struct mt7620_gsw *gsw)
2133 +{
2134 + u32 is_BGA = mt7620_is_bga();
2135 +
2136 + rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
2137 + gsw_w32(gsw, gsw_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
2138 +
2139 + /*correct PHY setting L3.0 BGA*/
2140 + _mt7620_mii_write(gsw, 1, 31, 0x4000); //global, page 4
2141 +
2142 + _mt7620_mii_write(gsw, 1, 17, 0x7444);
2143 + if (is_BGA)
2144 + _mt7620_mii_write(gsw, 1, 19, 0x0114);
2145 + else
2146 + _mt7620_mii_write(gsw, 1, 19, 0x0117);
2147 +
2148 + _mt7620_mii_write(gsw, 1, 22, 0x10cf);
2149 + _mt7620_mii_write(gsw, 1, 25, 0x6212);
2150 + _mt7620_mii_write(gsw, 1, 26, 0x0777);
2151 + _mt7620_mii_write(gsw, 1, 29, 0x4000);
2152 + _mt7620_mii_write(gsw, 1, 28, 0xc077);
2153 + _mt7620_mii_write(gsw, 1, 24, 0x0000);
2154 +
2155 + _mt7620_mii_write(gsw, 1, 31, 0x3000); //global, page 3
2156 + _mt7620_mii_write(gsw, 1, 17, 0x4838);
2157 +
2158 + _mt7620_mii_write(gsw, 1, 31, 0x2000); //global, page 2
2159 + if (is_BGA) {
2160 + _mt7620_mii_write(gsw, 1, 21, 0x0515);
2161 + _mt7620_mii_write(gsw, 1, 22, 0x0053);
2162 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2163 + _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
2164 + _mt7620_mii_write(gsw, 1, 25, 0x0fad);
2165 + _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
2166 + } else {
2167 + _mt7620_mii_write(gsw, 1, 21, 0x0517);
2168 + _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
2169 + _mt7620_mii_write(gsw, 1, 23, 0x00bf);
2170 + _mt7620_mii_write(gsw, 1, 24, 0x0aab);
2171 + _mt7620_mii_write(gsw, 1, 25, 0x00ae);
2172 + _mt7620_mii_write(gsw, 1, 26, 0x0fff);
2173 + }
2174 + _mt7620_mii_write(gsw, 1, 31, 0x1000); //global, page 1
2175 + _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
2176 +
2177 + _mt7620_mii_write(gsw, 1, 31, 0x8000); //local, page 0
2178 + _mt7620_mii_write(gsw, 0, 30, 0xa000);
2179 + _mt7620_mii_write(gsw, 1, 30, 0xa000);
2180 + _mt7620_mii_write(gsw, 2, 30, 0xa000);
2181 + _mt7620_mii_write(gsw, 3, 30, 0xa000);
2182 +
2183 + _mt7620_mii_write(gsw, 0, 4, 0x05e1);
2184 + _mt7620_mii_write(gsw, 1, 4, 0x05e1);
2185 + _mt7620_mii_write(gsw, 2, 4, 0x05e1);
2186 + _mt7620_mii_write(gsw, 3, 4, 0x05e1);
2187 + _mt7620_mii_write(gsw, 1, 31, 0xa000); //local, page 2
2188 + _mt7620_mii_write(gsw, 0, 16, 0x1111);
2189 + _mt7620_mii_write(gsw, 1, 16, 0x1010);
2190 + _mt7620_mii_write(gsw, 2, 16, 0x1515);
2191 + _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
2192 +
2193 + /* CPU Port6 Force Link 1G, FC ON */
2194 + gsw_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
2195 + /* Set Port6 CPU Port */
2196 + gsw_w32(gsw, 0x7f7f7fe0, 0x0010);
2197 +
2198 +// GSW_VAWD2
2199 +
2200 + /* setup port 4 */
2201 + if (gsw->port4 == PORT4_EPHY) {
2202 + u32 val = rt_sysc_r32(SYSCFG1);
2203 + val |= 3 << 14;
2204 + rt_sysc_w32(val, SYSCFG1);
2205 + _mt7620_mii_write(gsw, 4, 30, 0xa000);
2206 + _mt7620_mii_write(gsw, 4, 4, 0x05e1);
2207 + _mt7620_mii_write(gsw, 4, 16, 0x1313);
2208 + pr_info("gsw: setting port4 to ephy mode\n");
2209 + }
2210 +}
2211 +
2212 +static int gsw_reset_switch(struct switch_dev *dev)
2213 +{
2214 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2215 +
2216 + gsw->global_vlan_enable = 0;
2217 + memset(gsw->ports, 0, sizeof(gsw->ports));
2218 + memset(gsw->vlans, 0, sizeof(gsw->vlans));
2219 + gsw_hw_init(gsw);
2220 +
2221 + return 0;
2222 +}
2223 +
2224 +static int gsw_get_vlan_enable(struct switch_dev *dev,
2225 + const struct switch_attr *attr,
2226 + struct switch_val *val)
2227 +{
2228 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2229 +
2230 + val->value.i = gsw->global_vlan_enable;
2231 +
2232 + return 0;
2233 +}
2234 +
2235 +static int gsw_set_vlan_enable(struct switch_dev *dev,
2236 + const struct switch_attr *attr,
2237 + struct switch_val *val)
2238 +{
2239 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2240 +
2241 + gsw->global_vlan_enable = val->value.i != 0;
2242 +
2243 + return 0;
2244 +}
2245 +
2246 +static unsigned gsw_get_pvid(struct mt7620_gsw *gsw, unsigned port)
2247 +{
2248 + unsigned s, val;
2249 +
2250 + s = GSW_VTIM_S * (port % 2);
2251 + val = gsw_r32(gsw, GSW_VTIM(port / 2));
2252 +
2253 + return (val >> s) & GSW_VTIM_M;
2254 +}
2255 +
2256 +static void gsw_set_pvid(struct mt7620_gsw *gsw, unsigned port, unsigned pvid)
2257 +{
2258 + unsigned s, val;
2259 +
2260 + s = GSW_VTIM_S * (port % 2);
2261 + val = gsw_r32(gsw, GSW_VTIM(port / 2));
2262 + val &= ~(GSW_VTIM_M << s);
2263 + val |= (pvid && GSW_VTIM_M) << s;
2264 + gsw_w32(gsw, val, GSW_VTIM(port / 2));
2265 +}
2266 +
2267 +static int gsw_get_port_bool(struct switch_dev *dev,
2268 + const struct switch_attr *attr,
2269 + struct switch_val *val)
2270 +{
2271 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2272 + int idx = val->port_vlan;
2273 +
2274 + if (idx < 0 || idx >= GSW_NUM_PORTS)
2275 + return -EINVAL;
2276 +
2277 + switch (attr->id) {
2278 + case GSW_ATTR_PORT_UNTAG:
2279 + return gsw->ports[idx].untag;
2280 + }
2281 +
2282 + return -EINVAL;
2283 +}
2284 +
2285 +static int gsw_get_port_pvid(struct switch_dev *dev, int port, int *val)
2286 +{
2287 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2288 +
2289 + if (port >= GSW_NUM_PORTS)
2290 + return -EINVAL;
2291 +
2292 + *val = gsw_get_pvid(gsw, port);
2293 +
2294 + return 0;
2295 +}
2296 +
2297 +static int gsw_set_port_pvid(struct switch_dev *dev, int port, int val)
2298 +{
2299 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2300 +
2301 + if (port >= GSW_NUM_PORTS)
2302 + return -EINVAL;
2303 +
2304 + gsw->ports[port].pvid = val;
2305 +
2306 + return 0;
2307 +}
2308 +
2309 +static void gsw_set_vtcr(struct switch_dev *dev, u32 vid)
2310 +{
2311 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2312 + int retry = 1000;
2313 +
2314 + gsw_w32(gsw, 0x80000000 | (BIT(vid) & GSW_VLAN_VTCR_VID_M), GSW_VLAN_VTCR);
2315 + while (retry-- && (gsw_r32(gsw, GSW_VLAN_VTCR) & 0x80000000))
2316 + ;
2317 +}
2318 +
2319 +static void gsw_apply_vtcr(struct switch_dev *dev, u32 vid)
2320 +{
2321 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2322 + int retry = 1000;
2323 +
2324 + gsw_w32(gsw, 0x80001000 | (BIT(vid) & GSW_VLAN_VTCR_VID_M), GSW_VLAN_VTCR);
2325 + while (retry-- && (gsw_r32(gsw, GSW_VLAN_VTCR) & 0x80000000))
2326 + ;
2327 +}
2328 +
2329 +static unsigned gsw_get_vlan_id(struct mt7620_gsw *gsw, unsigned vlan)
2330 +{
2331 + unsigned s;
2332 + unsigned val;
2333 +
2334 + s = GSW_VLAN_ID_VID_S * (vlan % 2);
2335 + val = gsw_r32(gsw, GSW_VLAN_ID(vlan / 2));
2336 + val = (val >> s) & GSW_VLAN_ID_VID_M;
2337 +
2338 + return val;
2339 +}
2340 +
2341 +static void gsw_set_vlan_id(struct mt7620_gsw *gsw, unsigned vlan, unsigned vid)
2342 +{
2343 + unsigned s;
2344 + unsigned val;
2345 +
2346 + s = GSW_VLAN_ID_VID_S * (vlan % 2);
2347 + val = gsw_r32(gsw, GSW_VLAN_ID(vlan / 2));
2348 + val &= ~(GSW_VLAN_ID_VID_M << s);
2349 + val |= (vid << s);
2350 + gsw_w32(gsw, val, GSW_VLAN_ID(vlan / 2));
2351 +}
2352 +
2353 +static void gsw_vlan_tagging_enable(struct mt7620_gsw *gsw, unsigned vlan, unsigned enable)
2354 +{
2355 + unsigned val;
2356 +
2357 + val = gsw_r32(gsw, GSW_VAWD1);
2358 + if (enable)
2359 + val |= GSW_VAWD1_VTAG_EN;
2360 + else
2361 + val &= ~GSW_VAWD1_VTAG_EN;
2362 + gsw_w32(gsw, val, GSW_VAWD1);
2363 +}
2364 +
2365 +static unsigned gsw_get_port_member(struct mt7620_gsw *gsw, unsigned vlan)
2366 +{
2367 + unsigned val;
2368 +
2369 + gsw_set_vtcr(&gsw->swdev, vlan);
2370 +
2371 + val = gsw_r32(gsw, GSW_VAWD1);
2372 + val = (val >> GSW_VAWD1_PORTM_S) & GSW_VAWD1_PORTM_M;
2373 +
2374 + return val;
2375 +}
2376 +
2377 +static void gsw_set_port_member(struct mt7620_gsw *gsw, unsigned vlan, unsigned member)
2378 +{
2379 + unsigned val;
2380 +
2381 + val = gsw_r32(gsw, GSW_VAWD1);
2382 + val = ~(GSW_VAWD1_PORTM_M << GSW_VAWD1_PORTM_S);
2383 + val |= (member & GSW_VAWD1_PORTM_M) << GSW_VAWD1_PORTM_S;
2384 + gsw_w32(gsw, val, GSW_VAWD1);
2385 +}
2386 +
2387 +static unsigned gsw_get_port_tag(struct mt7620_gsw *gsw, unsigned port)
2388 +{
2389 + unsigned val;
2390 +
2391 + val = gsw_r32(gsw, GSW_REG_PCR(port));
2392 + val >>= GSW_REG_PCR_EG_TAG_S;
2393 + val &= GSW_REG_PCR_EG_TAG_M;
2394 +
2395 + return !!val;
2396 +}
2397 +
2398 +static void gsw_set_port_untag(struct mt7620_gsw *gsw, unsigned port, unsigned untag)
2399 +{
2400 + unsigned val;
2401 +
2402 + val = gsw_r32(gsw, GSW_REG_PCR(port));
2403 + if (!untag)
2404 + untag = 0x2;
2405 + else
2406 + untag = 0;
2407 + val &= ~(GSW_REG_PCR_EG_TAG_M << GSW_REG_PCR_EG_TAG_S);
2408 + val |= (untag & GSW_REG_PCR_EG_TAG_M) << GSW_REG_PCR_EG_TAG_S;
2409 + gsw_w32(gsw, val, GSW_REG_PCR(port));
2410 +}
2411 +
2412 +static int gsw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
2413 +{
2414 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2415 + int vlan_idx = -1;
2416 + u32 member;
2417 + int i;
2418 +
2419 + val->len = 0;
2420 +
2421 + if (val->port_vlan < 0 || val->port_vlan >= GSW_NUM_VIDS)
2422 + return -EINVAL;
2423 +
2424 + /* valid vlan? */
2425 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2426 + if (gsw_get_vlan_id(gsw, i) != val->port_vlan)
2427 + continue;
2428 + member = gsw_get_port_member(gsw, i);
2429 + vlan_idx = i;
2430 + break;
2431 + }
2432 +
2433 + if (vlan_idx == -1)
2434 + return -EINVAL;
2435 +
2436 + for (i = 0; i < GSW_NUM_PORTS; i++) {
2437 + struct switch_port *p;
2438 + int port_mask = 1 << i;
2439 +
2440 + if (!(member & port_mask))
2441 + continue;
2442 +
2443 + p = &val->value.ports[val->len++];
2444 + p->id = i;
2445 + if (gsw_get_port_tag(gsw, i))
2446 + p->flags = 1 << SWITCH_PORT_FLAG_TAGGED;
2447 + else
2448 + p->flags = 0;
2449 + }
2450 +
2451 + return 0;
2452 +}
2453 +
2454 +static int gsw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
2455 +{
2456 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2457 + int ports;
2458 + int vlan_idx = -1;
2459 + int i;
2460 +
2461 + if (val->port_vlan < 0 || val->port_vlan >= GSW_NUM_VIDS ||
2462 + val->len > GSW_NUM_PORTS)
2463 + return -EINVAL;
2464 +
2465 + /* one of the already defined vlans? */
2466 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2467 + if (gsw->vlans[i].vid == val->port_vlan &&
2468 + gsw->vlans[i].ports) {
2469 + vlan_idx = i;
2470 + break;
2471 + }
2472 + }
2473 +
2474 + /* select a free slot */
2475 + for (i = 0; vlan_idx == -1 && i < GSW_NUM_VLANS; i++) {
2476 + if (!gsw->vlans[i].ports)
2477 + vlan_idx = i;
2478 + }
2479 +
2480 + /* bail if all slots are in use */
2481 + if (vlan_idx == -1)
2482 + return -EINVAL;
2483 +
2484 + ports = 0;
2485 + for (i = 0; i < val->len; i++) {
2486 + struct switch_port *p = &val->value.ports[i];
2487 + int port_mask = 1 << p->id;
2488 + bool untagged = !(p->flags & (1 << SWITCH_PORT_FLAG_TAGGED));
2489 +
2490 + if (p->id >= GSW_NUM_PORTS)
2491 + return -EINVAL;
2492 +
2493 + ports |= port_mask;
2494 + gsw->ports[p->id].untag = untagged;
2495 + }
2496 + gsw->vlans[vlan_idx].ports = ports;
2497 + if (!ports)
2498 + gsw->vlans[vlan_idx].vid = 0xfff;
2499 + else
2500 + gsw->vlans[vlan_idx].vid = val->port_vlan;
2501 +
2502 + return 0;
2503 +}
2504 +
2505 +static int gsw_apply_config(struct switch_dev *dev)
2506 +{
2507 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2508 + int i;
2509 +
2510 + for (i = 0; i < GSW_NUM_VLANS; i++) {
2511 + gsw_set_vtcr(&gsw->swdev, i);
2512 + if (gsw->global_vlan_enable) {
2513 + gsw_set_vlan_id(gsw, i, gsw->vlans[i].vid);
2514 + gsw_set_port_member(gsw, i, gsw->vlans[i].ports);
2515 + gsw_vlan_tagging_enable(gsw, i, 1);
2516 + } else {
2517 + gsw_set_vlan_id(gsw, i, 0xfff);
2518 + gsw_set_port_member(gsw, i, 0);
2519 + gsw_vlan_tagging_enable(gsw, i, 0);
2520 + }
2521 + gsw_apply_vtcr(&gsw->swdev, i);
2522 + }
2523 +
2524 + for (i = 0; i < GSW_NUM_PORTS; i++) {
2525 + if (gsw->global_vlan_enable) {
2526 + gsw_set_port_untag(gsw, i, !gsw->ports[i].untag);
2527 + gsw_set_pvid(gsw, i, gsw->ports[i].pvid);
2528 + } else {
2529 + gsw_set_port_untag(gsw, i, 0);
2530 + gsw_set_pvid(gsw, i, 0);
2531 + }
2532 + }
2533 +
2534 + if (!gsw->global_vlan_enable)
2535 + gsw_set_vlan_id(gsw, 0, 0);
2536 +
2537 + return 0;
2538 +}
2539 +
2540 +static int gsw_get_port_link(struct switch_dev *dev,
2541 + int port,
2542 + struct switch_port_link *link)
2543 +{
2544 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2545 + u32 status;
2546 +
2547 + if (port < 0 || port >= GSW_NUM_PORTS)
2548 + return -EINVAL;
2549 +
2550 + status = gsw_r32(gsw, GSW_REG_PORT_STATUS(port));
2551 + link->link = status & 0x1;
2552 + link->duplex = (status >> 1) & 1;
2553 +
2554 + switch ((status >> 2) & 0x3) {
2555 + case 0:
2556 + link->speed = SWITCH_PORT_SPEED_10;
2557 + break;
2558 + case 1:
2559 + link->speed = SWITCH_PORT_SPEED_100;
2560 + break;
2561 + case 2:
2562 + case 3: // forced gige speed can be 2 or 3
2563 + link->speed = SWITCH_PORT_SPEED_1000;
2564 + break;
2565 + }
2566 +
2567 + return 0;
2568 +}
2569 +
2570 +static int gsw_set_port_bool(struct switch_dev *dev,
2571 + const struct switch_attr *attr,
2572 + struct switch_val *val)
2573 +{
2574 + struct mt7620_gsw *gsw = container_of(dev, struct mt7620_gsw, swdev);
2575 + int idx = val->port_vlan;
2576 +
2577 + if (idx < 0 || idx >= GSW_NUM_PORTS ||
2578 + val->value.i < 0 || val->value.i > 1)
2579 + return -EINVAL;
2580 +
2581 + switch (attr->id) {
2582 + case GSW_ATTR_PORT_UNTAG:
2583 + gsw->ports[idx].untag = val->value.i;
2584 + break;
2585 + default:
2586 + return -EINVAL;
2587 + }
2588 +
2589 + return 0;
2590 +}
2591 +
2592 +static const struct switch_attr gsw_global[] = {
2593 + {
2594 + .type = SWITCH_TYPE_INT,
2595 + .name = "enable_vlan",
2596 + .description = "VLAN mode (1:enabled)",
2597 + .max = 1,
2598 + .id = GSW_ATTR_ENABLE_VLAN,
2599 + .get = gsw_get_vlan_enable,
2600 + .set = gsw_set_vlan_enable,
2601 + },
2602 +};
2603 +
2604 +static const struct switch_attr gsw_port[] = {
2605 + {
2606 + .type = SWITCH_TYPE_INT,
2607 + .name = "untag",
2608 + .description = "Untag (1:strip outgoing vlan tag)",
2609 + .max = 1,
2610 + .id = GSW_ATTR_PORT_UNTAG,
2611 + .get = gsw_get_port_bool,
2612 + .set = gsw_set_port_bool,
2613 + },
2614 +};
2615 +
2616 +static const struct switch_attr gsw_vlan[] = {
2617 +};
2618 +
2619 +static const struct switch_dev_ops gsw_ops = {
2620 + .attr_global = {
2621 + .attr = gsw_global,
2622 + .n_attr = ARRAY_SIZE(gsw_global),
2623 + },
2624 + .attr_port = {
2625 + .attr = gsw_port,
2626 + .n_attr = ARRAY_SIZE(gsw_port),
2627 + },
2628 + .attr_vlan = {
2629 + .attr = gsw_vlan,
2630 + .n_attr = ARRAY_SIZE(gsw_vlan),
2631 + },
2632 + .get_vlan_ports = gsw_get_vlan_ports,
2633 + .set_vlan_ports = gsw_set_vlan_ports,
2634 + .get_port_pvid = gsw_get_port_pvid,
2635 + .set_port_pvid = gsw_set_port_pvid,
2636 + .get_port_link = gsw_get_port_link,
2637 + .apply_config = gsw_apply_config,
2638 + .reset_switch = gsw_reset_switch,
2639 +};
2640 +
2641 +void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac)
2642 +{
2643 + struct mt7620_gsw *gsw = (struct mt7620_gsw *) priv->soc->swpriv;
2644 + unsigned long flags;
2645 +
2646 + spin_lock_irqsave(&priv->page_lock, flags);
2647 + gsw_w32(gsw, (mac[0] << 8) | mac[1], GSW_REG_SMACCR1);
2648 + gsw_w32(gsw, (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
2649 + GSW_REG_SMACCR0);
2650 + spin_unlock_irqrestore(&priv->page_lock, flags);
2651 +}
2652 +
2653 +static struct of_device_id gsw_match[] = {
2654 + { .compatible = "ralink,mt7620a-gsw" },
2655 + {}
2656 +};
2657 +
2658 +int mt7620_gsw_probe(struct fe_priv *priv)
2659 +{
2660 + struct mt7620_gsw *gsw;
2661 + struct device_node *np;
2662 + struct switch_dev *swdev;
2663 + const char *port4 = NULL;
2664 +
2665 + np = of_find_matching_node(NULL, gsw_match);
2666 + if (!np) {
2667 + dev_err(priv->device, "no gsw node found\n");
2668 + return -EINVAL;
2669 + }
2670 + np = of_node_get(np);
2671 +
2672 + gsw = devm_kzalloc(priv->device, sizeof(struct mt7620_gsw), GFP_KERNEL);
2673 + if (!gsw) {
2674 + dev_err(priv->device, "no gsw memory for private data\n");
2675 + return -ENOMEM;
2676 + }
2677 +
2678 + gsw->irq = irq_of_parse_and_map(np, 0);
2679 + if (!gsw->irq) {
2680 + dev_err(priv->device, "no gsw irq resource found\n");
2681 + return -ENOMEM;
2682 + }
2683 +
2684 + gsw->base = of_iomap(np, 0);
2685 + if (!gsw->base) {
2686 + dev_err(priv->device, "gsw ioremap failed\n");
2687 + }
2688 +
2689 + gsw->dev = priv->device;
2690 + priv->soc->swpriv = gsw;
2691 +
2692 + swdev = &gsw->swdev;
2693 + swdev->of_node = np;
2694 + swdev->name = "mt7620a-gsw";
2695 + swdev->alias = "mt7620x";
2696 + swdev->cpu_port = GSW_PORT6;
2697 + swdev->ports = GSW_NUM_PORTS;
2698 + swdev->vlans = GSW_NUM_VLANS;
2699 + swdev->ops = &gsw_ops;
2700 +
2701 + if (register_switch(swdev, NULL))
2702 + dev_err(priv->device, "register_switch failed\n");
2703 +
2704 + of_property_read_string(np, "ralink,port4", &port4);
2705 + if (port4 && !strcmp(port4, "ephy"))
2706 + gsw->port4 = PORT4_EPHY;
2707 + else if (port4 && !strcmp(port4, "gmac"))
2708 + gsw->port4 = PORT4_EXT;
2709 + else
2710 + WARN_ON(port4);
2711 +
2712 + gsw_hw_init(gsw);
2713 +
2714 + gsw_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
2715 + request_irq(gsw->irq, gsw_interrupt, 0, "gsw", priv);
2716 +
2717 + return 0;
2718 +}
2719 diff --git a/drivers/net/ethernet/ralink/gsw_mt7620a.h b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2720 new file mode 100644
2721 index 0000000..fd4add5
2722 --- /dev/null
2723 +++ b/drivers/net/ethernet/ralink/gsw_mt7620a.h
2724 @@ -0,0 +1,29 @@
2725 +/*
2726 + * This program is free software; you can redistribute it and/or modify
2727 + * it under the terms of the GNU General Public License as published by
2728 + * the Free Software Foundation; version 2 of the License
2729 + *
2730 + * This program is distributed in the hope that it will be useful,
2731 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2732 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2733 + * GNU General Public License for more details.
2734 + *
2735 + * You should have received a copy of the GNU General Public License
2736 + * along with this program; if not, write to the Free Software
2737 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2738 + *
2739 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2740 + */
2741 +
2742 +#ifndef _RALINK_GSW_MT7620_H__
2743 +#define _RALINK_GSW_MT7620_H__
2744 +
2745 +extern int mt7620_gsw_probe(struct fe_priv *priv);
2746 +extern void mt7620_set_mac(struct fe_priv *priv, unsigned char *mac);
2747 +extern int mt7620_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
2748 +extern int mt7620_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
2749 +extern void mt7620_mdio_link_adjust(struct fe_priv *priv, int port);
2750 +extern void mt7620_port_init(struct fe_priv *priv, struct device_node *np);
2751 +extern int mt7620a_has_carrier(struct fe_priv *priv);
2752 +
2753 +#endif
2754 diff --git a/drivers/net/ethernet/ralink/mdio.c b/drivers/net/ethernet/ralink/mdio.c
2755 new file mode 100644
2756 index 0000000..b265c75
2757 --- /dev/null
2758 +++ b/drivers/net/ethernet/ralink/mdio.c
2759 @@ -0,0 +1,245 @@
2760 +/*
2761 + * This program is free software; you can redistribute it and/or modify
2762 + * it under the terms of the GNU General Public License as published by
2763 + * the Free Software Foundation; version 2 of the License
2764 + *
2765 + * This program is distributed in the hope that it will be useful,
2766 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2767 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2768 + * GNU General Public License for more details.
2769 + *
2770 + * You should have received a copy of the GNU General Public License
2771 + * along with this program; if not, write to the Free Software
2772 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
2773 + *
2774 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
2775 + */
2776 +
2777 +#include <linux/module.h>
2778 +#include <linux/kernel.h>
2779 +#include <linux/types.h>
2780 +#include <linux/dma-mapping.h>
2781 +#include <linux/init.h>
2782 +#include <linux/skbuff.h>
2783 +#include <linux/etherdevice.h>
2784 +#include <linux/ethtool.h>
2785 +#include <linux/platform_device.h>
2786 +#include <linux/phy.h>
2787 +#include <linux/of_device.h>
2788 +#include <linux/clk.h>
2789 +#include <linux/of_net.h>
2790 +#include <linux/of_mdio.h>
2791 +
2792 +#include "ralink_soc_eth.h"
2793 +#include "mdio.h"
2794 +
2795 +static int fe_mdio_reset(struct mii_bus *bus)
2796 +{
2797 + /* TODO */
2798 + return 0;
2799 +}
2800 +
2801 +static void fe_phy_link_adjust(struct net_device *dev)
2802 +{
2803 + struct fe_priv *priv = netdev_priv(dev);
2804 + unsigned long flags;
2805 + int i;
2806 +
2807 + spin_lock_irqsave(&priv->phy->lock, flags);
2808 + for (i = 0; i < 8; i++) {
2809 + if (priv->phy->phy_node[i]) {
2810 + struct phy_device *phydev = priv->phy->phy[i];
2811 + int status_change = 0;
2812 +
2813 + if (phydev->link)
2814 + if (priv->phy->duplex[i] != phydev->duplex ||
2815 + priv->phy->speed[i] != phydev->speed)
2816 + status_change = 1;
2817 +
2818 + if (phydev->link != priv->link[i])
2819 + status_change = 1;
2820 +
2821 + switch (phydev->speed) {
2822 + case SPEED_1000:
2823 + case SPEED_100:
2824 + case SPEED_10:
2825 + priv->link[i] = phydev->link;
2826 + priv->phy->duplex[i] = phydev->duplex;
2827 + priv->phy->speed[i] = phydev->speed;
2828 +
2829 + if (status_change && priv->soc->mdio_adjust_link)
2830 + priv->soc->mdio_adjust_link(priv, i);
2831 + break;
2832 + }
2833 + }
2834 + }
2835 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2836 +}
2837 +
2838 +int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
2839 +{
2840 + const __be32 *_port = NULL;
2841 + struct phy_device *phydev;
2842 + int phy_mode, port;
2843 +
2844 + _port = of_get_property(phy_node, "reg", NULL);
2845 +
2846 + if (!_port || (be32_to_cpu(*_port) >= 8)) {
2847 + pr_err("%s: invalid port id\n", phy_node->name);
2848 + return -EINVAL;
2849 + }
2850 + port = be32_to_cpu(*_port);
2851 + phy_mode = of_get_phy_mode(phy_node);
2852 + if (phy_mode < 0) {
2853 + dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
2854 + priv->phy->phy_node[port] = NULL;
2855 + return -EINVAL;
2856 + }
2857 +
2858 + phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
2859 + 0, phy_mode);
2860 + if (IS_ERR(phydev)) {
2861 + dev_err(priv->device, "could not connect to PHY\n");
2862 + priv->phy->phy_node[port] = NULL;
2863 + return PTR_ERR(phydev);
2864 + }
2865 +
2866 + phydev->supported &= PHY_GBIT_FEATURES;
2867 + phydev->advertising = phydev->supported;
2868 + phydev->no_auto_carrier_off = 1;
2869 +
2870 + dev_info(priv->device,
2871 + "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
2872 + port, dev_name(&phydev->dev), phydev->phy_id,
2873 + phydev->drv->name);
2874 +
2875 + priv->phy->phy[port] = phydev;
2876 + priv->link[port] = 0;
2877 +
2878 + return 0;
2879 +}
2880 +
2881 +static int fe_phy_connect(struct fe_priv *priv)
2882 +{
2883 + return 0;
2884 +}
2885 +
2886 +static void fe_phy_disconnect(struct fe_priv *priv)
2887 +{
2888 + unsigned long flags;
2889 + int i;
2890 +
2891 + for (i = 0; i < 8; i++)
2892 + if (priv->phy->phy_fixed[i]) {
2893 + spin_lock_irqsave(&priv->phy->lock, flags);
2894 + priv->link[i] = 0;
2895 + if (priv->soc->mdio_adjust_link)
2896 + priv->soc->mdio_adjust_link(priv, i);
2897 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2898 + } else if (priv->phy->phy[i]) {
2899 + phy_disconnect(priv->phy->phy[i]);
2900 + }
2901 +}
2902 +
2903 +static void fe_phy_start(struct fe_priv *priv)
2904 +{
2905 + unsigned long flags;
2906 + int i;
2907 +
2908 + for (i = 0; i < 8; i++) {
2909 + if (priv->phy->phy_fixed[i]) {
2910 + spin_lock_irqsave(&priv->phy->lock, flags);
2911 + priv->link[i] = 1;
2912 + if (priv->soc->mdio_adjust_link)
2913 + priv->soc->mdio_adjust_link(priv, i);
2914 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2915 + } else if (priv->phy->phy[i]) {
2916 + phy_start(priv->phy->phy[i]);
2917 + }
2918 + }
2919 +}
2920 +
2921 +static void fe_phy_stop(struct fe_priv *priv)
2922 +{
2923 + unsigned long flags;
2924 + int i;
2925 +
2926 + for (i = 0; i < 8; i++)
2927 + if (priv->phy->phy_fixed[i]) {
2928 + spin_lock_irqsave(&priv->phy->lock, flags);
2929 + priv->link[i] = 0;
2930 + if (priv->soc->mdio_adjust_link)
2931 + priv->soc->mdio_adjust_link(priv, i);
2932 + spin_unlock_irqrestore(&priv->phy->lock, flags);
2933 + } else if (priv->phy->phy[i]) {
2934 + phy_stop(priv->phy->phy[i]);
2935 + }
2936 +}
2937 +
2938 +static struct fe_phy phy_ralink = {
2939 + .connect = fe_phy_connect,
2940 + .disconnect = fe_phy_disconnect,
2941 + .start = fe_phy_start,
2942 + .stop = fe_phy_stop,
2943 +};
2944 +
2945 +int fe_mdio_init(struct fe_priv *priv)
2946 +{
2947 + struct device_node *mii_np;
2948 + int err;
2949 +
2950 + if (!priv->soc->mdio_read || !priv->soc->mdio_write)
2951 + return 0;
2952 +
2953 + spin_lock_init(&phy_ralink.lock);
2954 + priv->phy = &phy_ralink;
2955 +
2956 + mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
2957 + if (!mii_np) {
2958 + dev_err(priv->device, "no %s child node found", "mdio-bus");
2959 + return -ENODEV;
2960 + }
2961 +
2962 + if (!of_device_is_available(mii_np)) {
2963 + err = 0;
2964 + goto err_put_node;
2965 + }
2966 +
2967 + priv->mii_bus = mdiobus_alloc();
2968 + if (priv->mii_bus == NULL) {
2969 + err = -ENOMEM;
2970 + goto err_put_node;
2971 + }
2972 +
2973 + priv->mii_bus->name = "mdio";
2974 + priv->mii_bus->read = priv->soc->mdio_read;
2975 + priv->mii_bus->write = priv->soc->mdio_write;
2976 + priv->mii_bus->reset = fe_mdio_reset;
2977 + priv->mii_bus->irq = priv->mii_irq;
2978 + priv->mii_bus->priv = priv;
2979 + priv->mii_bus->parent = priv->device;
2980 +
2981 + snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
2982 + err = of_mdiobus_register(priv->mii_bus, mii_np);
2983 + if (err)
2984 + goto err_free_bus;
2985 +
2986 + return 0;
2987 +
2988 +err_free_bus:
2989 + kfree(priv->mii_bus);
2990 +err_put_node:
2991 + of_node_put(mii_np);
2992 + priv->mii_bus = NULL;
2993 + return err;
2994 +}
2995 +
2996 +void fe_mdio_cleanup(struct fe_priv *priv)
2997 +{
2998 + if (!priv->mii_bus)
2999 + return;
3000 +
3001 + mdiobus_unregister(priv->mii_bus);
3002 + of_node_put(priv->mii_bus->dev.of_node);
3003 + kfree(priv->mii_bus);
3004 +}
3005 diff --git a/drivers/net/ethernet/ralink/mdio.h b/drivers/net/ethernet/ralink/mdio.h
3006 new file mode 100644
3007 index 0000000..c3910a0
3008 --- /dev/null
3009 +++ b/drivers/net/ethernet/ralink/mdio.h
3010 @@ -0,0 +1,29 @@
3011 +/*
3012 + * This program is free software; you can redistribute it and/or modify
3013 + * it under the terms of the GNU General Public License as published by
3014 + * the Free Software Foundation; version 2 of the License
3015 + *
3016 + * This program is distributed in the hope that it will be useful,
3017 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3018 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3019 + * GNU General Public License for more details.
3020 + *
3021 + * You should have received a copy of the GNU General Public License
3022 + * along with this program; if not, write to the Free Software
3023 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3024 + *
3025 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3026 + */
3027 +
3028 +#ifndef _RALINK_MDIO_H__
3029 +#define _RALINK_MDIO_H__
3030 +
3031 +#ifdef CONFIG_NET_RALINK_MDIO
3032 +extern int fe_mdio_init(struct fe_priv *priv);
3033 +extern void fe_mdio_cleanup(struct fe_priv *priv);
3034 +extern int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node);
3035 +#else
3036 +static inline int fe_mdio_init(struct fe_priv *priv) { return 0; }
3037 +static inline void fe_mdio_cleanup(struct fe_priv *priv) {}
3038 +#endif
3039 +#endif
3040 diff --git a/drivers/net/ethernet/ralink/mdio_rt2880.c b/drivers/net/ethernet/ralink/mdio_rt2880.c
3041 new file mode 100644
3042 index 0000000..701c7b6
3043 --- /dev/null
3044 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.c
3045 @@ -0,0 +1,232 @@
3046 +/*
3047 + * This program is free software; you can redistribute it and/or modify
3048 + * it under the terms of the GNU General Public License as published by
3049 + * the Free Software Foundation; version 2 of the License
3050 + *
3051 + * This program is distributed in the hope that it will be useful,
3052 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3053 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3054 + * GNU General Public License for more details.
3055 + *
3056 + * You should have received a copy of the GNU General Public License
3057 + * along with this program; if not, write to the Free Software
3058 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3059 + *
3060 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3061 + */
3062 +
3063 +#include <linux/module.h>
3064 +#include <linux/kernel.h>
3065 +#include <linux/types.h>
3066 +#include <linux/dma-mapping.h>
3067 +#include <linux/init.h>
3068 +#include <linux/skbuff.h>
3069 +#include <linux/etherdevice.h>
3070 +#include <linux/ethtool.h>
3071 +#include <linux/platform_device.h>
3072 +#include <linux/phy.h>
3073 +#include <linux/of_device.h>
3074 +#include <linux/clk.h>
3075 +#include <linux/of_net.h>
3076 +#include <linux/of_mdio.h>
3077 +
3078 +#include "ralink_soc_eth.h"
3079 +#include "mdio_rt2880.h"
3080 +#include "mdio.h"
3081 +
3082 +#define FE_MDIO_RETRY 1000
3083 +
3084 +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
3085 +{
3086 + switch (priv->phy->speed[0]) {
3087 + case SPEED_1000:
3088 + return "1000";
3089 + case SPEED_100:
3090 + return "100";
3091 + case SPEED_10:
3092 + return "10";
3093 + }
3094 +
3095 + return "?";
3096 +}
3097 +
3098 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
3099 +{
3100 + u32 mdio_cfg;
3101 +
3102 + if (!priv->link[0]) {
3103 + netif_carrier_off(priv->netdev);
3104 + netdev_info(priv->netdev, "link down\n");
3105 + return;
3106 + }
3107 +
3108 + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
3109 + FE_MDIO_CFG_RX_CLK_SKEW_200 |
3110 + FE_MDIO_CFG_GP1_FRC_EN;
3111 +
3112 + if (priv->phy->duplex[0] == DUPLEX_FULL)
3113 + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
3114 +
3115 + if (priv->phy->tx_fc[0])
3116 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
3117 +
3118 + if (priv->phy->rx_fc[0])
3119 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
3120 +
3121 + switch (priv->phy->speed[0]) {
3122 + case SPEED_10:
3123 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
3124 + break;
3125 + case SPEED_100:
3126 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
3127 + break;
3128 + case SPEED_1000:
3129 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
3130 + break;
3131 + default:
3132 + BUG();
3133 + }
3134 +
3135 + fe_w32(mdio_cfg, FE_MDIO_CFG);
3136 +
3137 + netif_carrier_on(priv->netdev);
3138 + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
3139 + rt2880_speed_str(priv),
3140 + (DUPLEX_FULL == priv->phy->duplex[0]) ? "Full" : "Half");
3141 +}
3142 +
3143 +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
3144 +{
3145 + int retries;
3146 +
3147 + retries = FE_MDIO_RETRY;
3148 + while (1) {
3149 + u32 t;
3150 +
3151 + t = fe_r32(FE_MDIO_ACCESS);
3152 + if ((t & (0x1 << 31)) == 0)
3153 + return 0;
3154 +
3155 + if (retries-- == 0)
3156 + break;
3157 +
3158 + udelay(1);
3159 + }
3160 +
3161 + dev_err(priv->device, "MDIO operation timed out\n");
3162 + return -ETIMEDOUT;
3163 +}
3164 +
3165 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
3166 +{
3167 + struct fe_priv *priv = bus->priv;
3168 + int err;
3169 + u32 t;
3170 +
3171 + err = rt2880_mdio_wait_ready(priv);
3172 + if (err)
3173 + return 0xffff;
3174 +
3175 + t = (phy_addr << 24) | (phy_reg << 16);
3176 + fe_w32(t, FE_MDIO_ACCESS);
3177 + t |= (1 << 31);
3178 + fe_w32(t, FE_MDIO_ACCESS);
3179 +
3180 + err = rt2880_mdio_wait_ready(priv);
3181 + if (err)
3182 + return 0xffff;
3183 +
3184 + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
3185 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
3186 +
3187 + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
3188 +}
3189 +
3190 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
3191 +{
3192 + struct fe_priv *priv = bus->priv;
3193 + int err;
3194 + u32 t;
3195 +
3196 + pr_info("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
3197 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
3198 +
3199 + err = rt2880_mdio_wait_ready(priv);
3200 + if (err)
3201 + return err;
3202 +
3203 + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
3204 + fe_w32(t, FE_MDIO_ACCESS);
3205 + t |= (1 << 31);
3206 + fe_w32(t, FE_MDIO_ACCESS);
3207 +
3208 + return rt2880_mdio_wait_ready(priv);
3209 +}
3210 +
3211 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
3212 +{
3213 + const __be32 *id = of_get_property(np, "reg", NULL);
3214 + const __be32 *link;
3215 + int size;
3216 + int phy_mode;
3217 +
3218 + if (!id || (be32_to_cpu(*id) != 0)) {
3219 + pr_err("%s: invalid port id\n", np->name);
3220 + return;
3221 + }
3222 +
3223 + priv->phy->phy_fixed[0] = of_get_property(np, "ralink,fixed-link", &size);
3224 + if (priv->phy->phy_fixed[0] && (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
3225 + pr_err("%s: invalid fixed link property\n", np->name);
3226 + priv->phy->phy_fixed[0] = NULL;
3227 + return;
3228 + }
3229 +
3230 + phy_mode = of_get_phy_mode(np);
3231 + switch (phy_mode) {
3232 + case PHY_INTERFACE_MODE_RGMII:
3233 + break;
3234 + case PHY_INTERFACE_MODE_MII:
3235 + break;
3236 + case PHY_INTERFACE_MODE_RMII:
3237 + break;
3238 + default:
3239 + if (!priv->phy->phy_fixed[0])
3240 + dev_err(priv->device, "port %d - invalid phy mode\n", priv->phy->speed[0]);
3241 + break;
3242 + }
3243 +
3244 + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
3245 + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
3246 + return;
3247 +
3248 + if (priv->phy->phy_fixed[0]) {
3249 + link = priv->phy->phy_fixed[0];
3250 + priv->phy->speed[0] = be32_to_cpup(link++);
3251 + priv->phy->duplex[0] = be32_to_cpup(link++);
3252 + priv->phy->tx_fc[0] = be32_to_cpup(link++);
3253 + priv->phy->rx_fc[0] = be32_to_cpup(link++);
3254 +
3255 + priv->link[0] = 1;
3256 + switch (priv->phy->speed[0]) {
3257 + case SPEED_10:
3258 + break;
3259 + case SPEED_100:
3260 + break;
3261 + case SPEED_1000:
3262 + break;
3263 + default:
3264 + dev_err(priv->device, "invalid link speed: %d\n", priv->phy->speed[0]);
3265 + priv->phy->phy_fixed[0] = 0;
3266 + return;
3267 + }
3268 + dev_info(priv->device, "using fixed link parameters\n");
3269 + rt2880_mdio_link_adjust(priv, 0);
3270 + return;
3271 + }
3272 + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0]) {
3273 + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
3274 + }
3275 +
3276 + return;
3277 +}
3278 diff --git a/drivers/net/ethernet/ralink/mdio_rt2880.h b/drivers/net/ethernet/ralink/mdio_rt2880.h
3279 new file mode 100644
3280 index 0000000..51e3633
3281 --- /dev/null
3282 +++ b/drivers/net/ethernet/ralink/mdio_rt2880.h
3283 @@ -0,0 +1,26 @@
3284 +/*
3285 + * This program is free software; you can redistribute it and/or modify
3286 + * it under the terms of the GNU General Public License as published by
3287 + * the Free Software Foundation; version 2 of the License
3288 + *
3289 + * This program is distributed in the hope that it will be useful,
3290 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3291 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3292 + * GNU General Public License for more details.
3293 + *
3294 + * You should have received a copy of the GNU General Public License
3295 + * along with this program; if not, write to the Free Software
3296 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3297 + *
3298 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3299 + */
3300 +
3301 +#ifndef _RALINK_MDIO_RT2880_H__
3302 +#define _RALINK_MDIO_RT2880_H__
3303 +
3304 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
3305 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
3306 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
3307 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
3308 +
3309 +#endif
3310 diff --git a/drivers/net/ethernet/ralink/ralink_soc_eth.c b/drivers/net/ethernet/ralink/ralink_soc_eth.c
3311 new file mode 100644
3312 index 0000000..8c36dba
3313 --- /dev/null
3314 +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.c
3315 @@ -0,0 +1,735 @@
3316 +/*
3317 + * This program is free software; you can redistribute it and/or modify
3318 + * it under the terms of the GNU General Public License as published by
3319 + * the Free Software Foundation; version 2 of the License
3320 + *
3321 + * This program is distributed in the hope that it will be useful,
3322 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3323 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3324 + * GNU General Public License for more details.
3325 + *
3326 + * You should have received a copy of the GNU General Public License
3327 + * along with this program; if not, write to the Free Software
3328 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
3329 + *
3330 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
3331 + */
3332 +
3333 +#include <linux/module.h>
3334 +#include <linux/kernel.h>
3335 +#include <linux/types.h>
3336 +#include <linux/dma-mapping.h>
3337 +#include <linux/init.h>
3338 +#include <linux/skbuff.h>
3339 +#include <linux/etherdevice.h>
3340 +#include <linux/ethtool.h>
3341 +#include <linux/platform_device.h>
3342 +#include <linux/of_device.h>
3343 +#include <linux/clk.h>
3344 +#include <linux/of_net.h>
3345 +#include <linux/of_mdio.h>
3346 +#include <linux/if_vlan.h>
3347 +
3348 +#include <asm/mach-ralink/ralink_regs.h>
3349 +
3350 +#include "ralink_soc_eth.h"
3351 +#include "esw_rt3052.h"
3352 +#include "mdio.h"
3353 +
3354 +#define TX_TIMEOUT (20 * HZ / 100)
3355 +#define MAX_RX_LENGTH 1536
3356 +
3357 +static const u32 fe_reg_table_default[FE_REG_COUNT] = {
3358 + [FE_REG_PDMA_GLO_CFG] = FE_PDMA_GLO_CFG,
3359 + [FE_REG_PDMA_RST_CFG] = FE_PDMA_RST_CFG,
3360 + [FE_REG_DLY_INT_CFG] = FE_DLY_INT_CFG,
3361 + [FE_REG_TX_BASE_PTR0] = FE_TX_BASE_PTR0,
3362 + [FE_REG_TX_MAX_CNT0] = FE_TX_MAX_CNT0,
3363 + [FE_REG_TX_CTX_IDX0] = FE_TX_CTX_IDX0,
3364 + [FE_REG_RX_BASE_PTR0] = FE_RX_BASE_PTR0,
3365 + [FE_REG_RX_MAX_CNT0] = FE_RX_MAX_CNT0,
3366 + [FE_REG_RX_CALC_IDX0] = FE_RX_CALC_IDX0,
3367 + [FE_REG_FE_INT_ENABLE] = FE_FE_INT_ENABLE,
3368 + [FE_REG_FE_INT_STATUS] = FE_FE_INT_STATUS,
3369 +};
3370 +
3371 +static const u32 *fe_reg_table = fe_reg_table_default;
3372 +
3373 +static void __iomem *fe_base = 0;
3374 +
3375 +void fe_w32(u32 val, unsigned reg)
3376 +{
3377 + __raw_writel(val, fe_base + reg);
3378 +}
3379 +
3380 +u32 fe_r32(unsigned reg)
3381 +{
3382 + return __raw_readl(fe_base + reg);
3383 +}
3384 +
3385 +static inline void fe_reg_w32(u32 val, enum fe_reg reg)
3386 +{
3387 + fe_w32(val, fe_reg_table[reg]);
3388 +}
3389 +
3390 +static inline u32 fe_reg_r32(enum fe_reg reg)
3391 +{
3392 + return fe_r32(fe_reg_table[reg]);
3393 +}
3394 +
3395 +static inline void fe_int_disable(u32 mask)
3396 +{
3397 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) & ~mask,
3398 + FE_REG_FE_INT_ENABLE);
3399 + /* flush write */
3400 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3401 +}
3402 +
3403 +static inline void fe_int_enable(u32 mask)
3404 +{
3405 + fe_reg_w32(fe_reg_r32(FE_REG_FE_INT_ENABLE) | mask,
3406 + FE_REG_FE_INT_ENABLE);
3407 + /* flush write */
3408 + fe_reg_r32(FE_REG_FE_INT_ENABLE);
3409 +}
3410 +
3411 +static inline void fe_hw_set_macaddr(struct fe_priv *priv, unsigned char *mac)
3412 +{
3413 + unsigned long flags;
3414 +
3415 + spin_lock_irqsave(&priv->page_lock, flags);
3416 + fe_w32((mac[0] << 8) | mac[1], FE_GDMA1_MAC_ADRH);
3417 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
3418 + FE_GDMA1_MAC_ADRL);
3419 + spin_unlock_irqrestore(&priv->page_lock, flags);
3420 +}
3421 +
3422 +static int fe_set_mac_address(struct net_device *dev, void *p)
3423 +{
3424 + int ret = eth_mac_addr(dev, p);
3425 +
3426 + if (!ret) {
3427 + struct fe_priv *priv = netdev_priv(dev);
3428 +
3429 + if (priv->soc->set_mac)
3430 + priv->soc->set_mac(priv, dev->dev_addr);
3431 + else
3432 + fe_hw_set_macaddr(priv, p);
3433 + }
3434 +
3435 + return ret;
3436 +}
3437 +
3438 +static struct sk_buff* fe_alloc_skb(struct fe_priv *priv)
3439 +{
3440 + struct sk_buff *skb;
3441 +
3442 + skb = netdev_alloc_skb(priv->netdev, MAX_RX_LENGTH + NET_IP_ALIGN);
3443 + if (!skb)
3444 + return NULL;
3445 +
3446 + skb_reserve(skb, NET_IP_ALIGN);
3447 +
3448 + return skb;
3449 +}
3450 +
3451 +static int fe_alloc_rx(struct fe_priv *priv)
3452 +{
3453 + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
3454 + int i;
3455 +
3456 + priv->rx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
3457 + &priv->rx_phys, GFP_ATOMIC);
3458 + if (!priv->rx_dma)
3459 + return -ENOMEM;
3460 +
3461 + memset(priv->rx_dma, 0, size);
3462 +
3463 + for (i = 0; i < NUM_DMA_DESC; i++) {
3464 + priv->rx_skb[i] = fe_alloc_skb(priv);
3465 + if (!priv->rx_skb[i])
3466 + return -ENOMEM;
3467 + }
3468 +
3469 + for (i = 0; i < NUM_DMA_DESC; i++) {
3470 + dma_addr_t dma_addr = dma_map_single(&priv->netdev->dev,
3471 + priv->rx_skb[i]->data,
3472 + MAX_RX_LENGTH,
3473 + DMA_FROM_DEVICE);
3474 + priv->rx_dma[i].rxd1 = (unsigned int) dma_addr;
3475 +
3476 + if (priv->soc->rx_dma)
3477 + priv->soc->rx_dma(priv, i, MAX_RX_LENGTH);
3478 + else
3479 + priv->rx_dma[i].rxd2 = RX_DMA_LSO;
3480 + }
3481 + wmb();
3482 +
3483 + fe_reg_w32(priv->rx_phys, FE_REG_RX_BASE_PTR0);
3484 + fe_reg_w32(NUM_DMA_DESC, FE_REG_RX_MAX_CNT0);
3485 + fe_reg_w32((NUM_DMA_DESC - 1), FE_REG_RX_CALC_IDX0);
3486 + fe_reg_w32(FE_PST_DRX_IDX0, FE_REG_PDMA_RST_CFG);
3487 +
3488 + return 0;
3489 +}
3490 +
3491 +static int fe_alloc_tx(struct fe_priv *priv)
3492 +{
3493 + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
3494 + int i;
3495 +
3496 + priv->tx_free_idx = 0;
3497 +
3498 + priv->tx_dma = dma_alloc_coherent(&priv->netdev->dev, size,
3499 + &priv->tx_phys, GFP_ATOMIC);
3500 + if (!priv->tx_dma)
3501 + return -ENOMEM;
3502 +
3503 + memset(priv->tx_dma, 0, size);
3504 +
3505 + for (i = 0; i < NUM_DMA_DESC; i++) {
3506 + if (priv->soc->tx_dma) {
3507 + priv->soc->tx_dma(priv, i, 0);
3508 + continue;
3509 + }
3510 +
3511 + priv->tx_dma[i].txd2 = TX_DMA_LSO | TX_DMA_DONE;
3512 + priv->tx_dma[i].txd4 = TX_DMA_QN(3) | TX_DMA_PN(1);
3513 + }
3514 +
3515 + fe_reg_w32(priv->tx_phys, FE_REG_TX_BASE_PTR0);
3516 + fe_reg_w32(NUM_DMA_DESC, FE_REG_TX_MAX_CNT0);
3517 + fe_reg_w32(0, FE_REG_TX_CTX_IDX0);
3518 + fe_reg_w32(FE_PST_DTX_IDX0, FE_REG_PDMA_RST_CFG);
3519 +
3520 + return 0;
3521 +}
3522 +
3523 +static void fe_free_dma(struct fe_priv *priv)
3524 +{
3525 + int i;
3526 +
3527 + for (i = 0; i < NUM_DMA_DESC; i++) {
3528 + if (priv->rx_skb[i]) {
3529 + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[i].rxd1,
3530 + MAX_RX_LENGTH, DMA_FROM_DEVICE);
3531 + dev_kfree_skb_any(priv->rx_skb[i]);
3532 + priv->rx_skb[i] = NULL;
3533 + }
3534 +
3535 + if (priv->tx_skb[i]) {
3536 + dev_kfree_skb_any(priv->tx_skb[i]);
3537 + priv->tx_skb[i] = NULL;
3538 + }
3539 + }
3540 +
3541 + if (priv->rx_dma) {
3542 + int size = NUM_DMA_DESC * sizeof(struct fe_rx_dma);
3543 + dma_free_coherent(&priv->netdev->dev, size, priv->rx_dma,
3544 + priv->rx_phys);
3545 + }
3546 +
3547 + if (priv->tx_dma) {
3548 + int size = NUM_DMA_DESC * sizeof(struct fe_tx_dma);
3549 + dma_free_coherent(&priv->netdev->dev, size, priv->tx_dma,
3550 + priv->tx_phys);
3551 + }
3552 +
3553 + netdev_reset_queue(priv->netdev);
3554 +}
3555 +
3556 +static int fe_start_xmit(struct sk_buff *skb, struct net_device *dev)
3557 +{
3558 + struct fe_priv *priv = netdev_priv(dev);
3559 + dma_addr_t mapped_addr;
3560 + u32 tx_next;
3561 + u32 tx;
3562 +
3563 + if (priv->soc->min_pkt_len) {
3564 + if (skb->len < priv->soc->min_pkt_len) {
3565 + if (skb_padto(skb, priv->soc->min_pkt_len)) {
3566 + printk(KERN_ERR
3567 + "fe_eth: skb_padto failed\n");
3568 + kfree_skb(skb);
3569 + return 0;
3570 + }
3571 + skb_put(skb, priv->soc->min_pkt_len - skb->len);
3572 + }
3573 + }
3574 +
3575 + dev->trans_start = jiffies;
3576 + mapped_addr = dma_map_single(&priv->netdev->dev, skb->data,
3577 + skb->len, DMA_TO_DEVICE);
3578 +
3579 + spin_lock(&priv->page_lock);
3580 +
3581 + tx = fe_reg_r32(FE_REG_TX_CTX_IDX0);
3582 + tx_next = (tx + 1) % NUM_DMA_DESC;
3583 +
3584 + if ((priv->tx_skb[tx]) || (priv->tx_skb[tx_next]) ||
3585 + !(priv->tx_dma[tx].txd2 & TX_DMA_DONE) ||
3586 + !(priv->tx_dma[tx_next].txd2 & TX_DMA_DONE))
3587 + {
3588 + spin_unlock(&priv->page_lock);
3589 + dev->stats.tx_dropped++;
3590 + kfree_skb(skb);
3591 +
3592 + return NETDEV_TX_OK;
3593 + }
3594 +
3595 + priv->tx_skb[tx] = skb;
3596 + priv->tx_dma[tx].txd1 = (unsigned int) mapped_addr;
3597 + wmb();
3598 + if (priv->soc->tx_dma)
3599 + priv->soc->tx_dma(priv, tx, skb->len);
3600 + else
3601 + priv->tx_dma[tx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(skb->len);
3602 +
3603 + if (skb->ip_summed == CHECKSUM_PARTIAL)
3604 + priv->tx_dma[tx].txd4 |= TX_DMA_CHKSUM;
3605 + else
3606 + priv->tx_dma[tx].txd4 &= ~TX_DMA_CHKSUM;
3607 +
3608 + priv->tx_dma[tx].txd4 &= ~0x80;
3609 +
3610 + dev->stats.tx_packets++;
3611 + dev->stats.tx_bytes += skb->len;
3612 +
3613 + fe_reg_w32(tx_next, FE_REG_TX_CTX_IDX0);
3614 + netdev_sent_queue(dev, skb->len);
3615 +
3616 + spin_unlock(&priv->page_lock);
3617 +
3618 + return NETDEV_TX_OK;
3619 +}
3620 +
3621 +static int fe_poll_rx(struct napi_struct *napi, int budget)
3622 +{
3623 + struct fe_priv *priv = container_of(napi, struct fe_priv, rx_napi);
3624 + int idx = fe_reg_r32(FE_REG_RX_CALC_IDX0);
3625 + int complete = 0;
3626 + int rx = 0;
3627 +
3628 + while ((rx < budget) && !complete) {
3629 + idx = (idx + 1) % NUM_DMA_DESC;
3630 +
3631 + if (priv->rx_dma[idx].rxd2 & RX_DMA_DONE) {
3632 + struct sk_buff *new_skb = fe_alloc_skb(priv);
3633 +
3634 + if (new_skb) {
3635 + int pktlen = RX_DMA_PLEN0(priv->rx_dma[idx].rxd2);
3636 + dma_addr_t dma_addr;
3637 +
3638 + dma_unmap_single(&priv->netdev->dev, priv->rx_dma[idx].rxd1,
3639 + MAX_RX_LENGTH, DMA_FROM_DEVICE);
3640 +
3641 + skb_put(priv->rx_skb[idx], pktlen);
3642 + priv->rx_skb[idx]->dev = priv->netdev;
3643 + priv->rx_skb[idx]->protocol = eth_type_trans(priv->rx_skb[idx], priv->netdev);
3644 + if (priv->rx_dma[idx].rxd4 & priv->soc->checksum_bit)
3645 + priv->rx_skb[idx]->ip_summed = CHECKSUM_UNNECESSARY;
3646 + else
3647 + priv->rx_skb[idx]->ip_summed = CHECKSUM_NONE;
3648 + priv->netdev->stats.rx_packets++;
3649 + priv->netdev->stats.rx_bytes += pktlen;
3650 + netif_receive_skb(priv->rx_skb[idx]);
3651 +
3652 + priv->rx_skb[idx] = new_skb;
3653 +
3654 + dma_addr = dma_map_single(&priv->netdev->dev,
3655 + new_skb->data,
3656 + MAX_RX_LENGTH,
3657 + DMA_FROM_DEVICE);
3658 + priv->rx_dma[idx].rxd1 = (unsigned int) dma_addr;
3659 + wmb();
3660 + } else {
3661 + priv->netdev->stats.rx_dropped++;
3662 + }
3663 +
3664 + if (priv->soc->rx_dma)
3665 + priv->soc->rx_dma(priv, idx, MAX_RX_LENGTH);
3666 + else
3667 + priv->rx_dma[idx].rxd2 = RX_DMA_LSO;
3668 + fe_reg_w32(idx, FE_REG_RX_CALC_IDX0);
3669 +
3670 + rx++;
3671 + } else {
3672 + complete = 1;
3673 + }
3674 + }
3675 +
3676 + if (complete) {
3677 + napi_complete(&priv->rx_napi);
3678 + fe_int_enable(priv->soc->rx_dly_int);
3679 + }
3680 +
3681 + return rx;
3682 +}
3683 +
3684 +static void fe_tx_housekeeping(unsigned long ptr)
3685 +{
3686 + struct net_device *dev = (struct net_device*)ptr;
3687 + struct fe_priv *priv = netdev_priv(dev);
3688 + unsigned int bytes_compl = 0;
3689 + unsigned int pkts_compl = 0;
3690 +
3691 + spin_lock(&priv->page_lock);
3692 + while (1) {
3693 + struct fe_tx_dma *txd;
3694 +
3695 + txd = &priv->tx_dma[priv->tx_free_idx];
3696 +
3697 + if (!(txd->txd2 & TX_DMA_DONE) || !(priv->tx_skb[priv->tx_free_idx]))
3698 + break;
3699 +
3700 + bytes_compl += priv->tx_skb[priv->tx_free_idx]->len;
3701 + pkts_compl++;
3702 +
3703 + dev_kfree_skb_irq(priv->tx_skb[priv->tx_free_idx]);
3704 + priv->tx_skb[priv->tx_free_idx] = NULL;
3705 + priv->tx_free_idx++;
3706 + if (priv->tx_free_idx >= NUM_DMA_DESC)
3707 + priv->tx_free_idx = 0;
3708 + }
3709 +
3710 + netdev_completed_queue(priv->netdev, pkts_compl, bytes_compl);
3711 + spin_unlock(&priv->page_lock);
3712 +
3713 + fe_int_enable(priv->soc->tx_dly_int);
3714 +}
3715 +
3716 +static void fe_tx_timeout(struct net_device *dev)
3717 +{
3718 + struct fe_priv *priv = netdev_priv(dev);
3719 +
3720 + tasklet_schedule(&priv->tx_tasklet);
3721 + priv->netdev->stats.tx_errors++;
3722 + netdev_err(dev, "transmit timed out, waking up the queue\n");
3723 + netif_wake_queue(dev);
3724 +}
3725 +
3726 +static irqreturn_t fe_handle_irq(int irq, void *dev)
3727 +{
3728 + struct fe_priv *priv = netdev_priv(dev);
3729 + unsigned int status;
3730 + unsigned int mask;
3731 +
3732 + status = fe_reg_r32(FE_REG_FE_INT_STATUS);
3733 + mask = fe_reg_r32(FE_REG_FE_INT_ENABLE);
3734 +
3735 + if (!(status & mask))
3736 + return IRQ_NONE;
3737 +
3738 + if (status & priv->soc->rx_dly_int) {
3739 + fe_int_disable(priv->soc->rx_dly_int);
3740 + napi_schedule(&priv->rx_napi);
3741 + }
3742 +
3743 + if (status & priv->soc->tx_dly_int) {
3744 + fe_int_disable(priv->soc->tx_dly_int);
3745 + tasklet_schedule(&priv->tx_tasklet);
3746 + }
3747 +
3748 + fe_reg_w32(status, FE_REG_FE_INT_STATUS);
3749 +
3750 + return IRQ_HANDLED;
3751 +}
3752 +
3753 +static int fe_hw_init(struct net_device *dev)
3754 +{
3755 + struct fe_priv *priv = netdev_priv(dev);
3756 + int err;
3757 +
3758 + err = devm_request_irq(priv->device, dev->irq, fe_handle_irq, 0,
3759 + dev_name(priv->device), dev);
3760 + if (err)
3761 + return err;
3762 +
3763 + err = fe_alloc_rx(priv);
3764 + if (!err)
3765 + err = fe_alloc_tx(priv);
3766 + if (err)
3767 + return err;
3768 +
3769 + if (priv->soc->set_mac)
3770 + priv->soc->set_mac(priv, dev->dev_addr);
3771 + else
3772 + fe_hw_set_macaddr(priv, dev->dev_addr);
3773 +
3774 + fe_reg_w32(FE_DELAY_INIT, FE_REG_DLY_INT_CFG);
3775 +
3776 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
3777 +
3778 + tasklet_init(&priv->tx_tasklet, fe_tx_housekeeping, (unsigned long)dev);
3779 +
3780 + if (priv->soc->fwd_config) {
3781 + priv->soc->fwd_config(priv);
3782 + } else {
3783 + unsigned long sysclk = priv->sysclk;
3784 +
3785 + if (!sysclk) {
3786 + netdev_err(dev, "unable to get clock\n");
3787 + return -EINVAL;
3788 + }
3789 +
3790 + sysclk /= FE_US_CYC_CNT_DIVISOR;
3791 + sysclk <<= FE_US_CYC_CNT_SHIFT;
3792 +
3793 + fe_w32((fe_r32(FE_FE_GLO_CFG) &
3794 + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
3795 + FE_FE_GLO_CFG);
3796 +
3797 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) & ~0xffff, FE_GDMA1_FWD_CFG);
3798 + fe_w32(fe_r32(FE_GDMA1_FWD_CFG) | (FE_GDM1_ICS_EN | FE_GDM1_TCS_EN | FE_GDM1_UCS_EN),
3799 + FE_GDMA1_FWD_CFG);
3800 + fe_w32(fe_r32(FE_CDMA_CSG_CFG) | (FE_ICS_GEN_EN | FE_TCS_GEN_EN | FE_UCS_GEN_EN),
3801 + FE_CDMA_CSG_CFG);
3802 + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
3803 + }
3804 +
3805 + fe_w32(1, FE_FE_RST_GL);
3806 + fe_w32(0, FE_FE_RST_GL);
3807 +
3808 + return 0;
3809 +}
3810 +
3811 +static int fe_open(struct net_device *dev)
3812 +{
3813 + struct fe_priv *priv = netdev_priv(dev);
3814 + unsigned long flags;
3815 + u32 val;
3816 +
3817 + spin_lock_irqsave(&priv->page_lock, flags);
3818 + napi_enable(&priv->rx_napi);
3819 +
3820 + val = FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
3821 + val |= priv->soc->pdma_glo_cfg;
3822 + fe_reg_w32(val, FE_REG_PDMA_GLO_CFG);
3823 +
3824 + spin_unlock_irqrestore(&priv->page_lock, flags);
3825 +
3826 + if (priv->phy)
3827 + priv->phy->start(priv);
3828 +
3829 + if (priv->soc->has_carrier && priv->soc->has_carrier(priv))
3830 + netif_carrier_on(dev);
3831 +
3832 + netif_start_queue(dev);
3833 + fe_int_enable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
3834 +
3835 + return 0;
3836 +}
3837 +
3838 +static int fe_stop(struct net_device *dev)
3839 +{
3840 + struct fe_priv *priv = netdev_priv(dev);
3841 + unsigned long flags;
3842 +
3843 + fe_int_disable(priv->soc->tx_dly_int | priv->soc->rx_dly_int);
3844 +
3845 + netif_stop_queue(dev);
3846 +
3847 + if (priv->phy)
3848 + priv->phy->stop(priv);
3849 +
3850 + spin_lock_irqsave(&priv->page_lock, flags);
3851 + napi_disable(&priv->rx_napi);
3852 +
3853 + fe_reg_w32(fe_reg_r32(FE_REG_PDMA_GLO_CFG) &
3854 + ~(FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN),
3855 + FE_REG_PDMA_GLO_CFG);
3856 + spin_unlock_irqrestore(&priv->page_lock, flags);
3857 +
3858 + return 0;
3859 +}
3860 +
3861 +static int __init fe_init(struct net_device *dev)
3862 +{
3863 + struct fe_priv *priv = netdev_priv(dev);
3864 + struct device_node *port;
3865 + int err;
3866 +
3867 + BUG_ON(!priv->soc->reset_fe);
3868 + priv->soc->reset_fe();
3869 +
3870 + if (priv->soc->switch_init)
3871 + priv->soc->switch_init(priv);
3872 +
3873 + net_srandom(jiffies);
3874 + memcpy(dev->dev_addr, priv->soc->mac, ETH_ALEN);
3875 + of_get_mac_address_mtd(priv->device->of_node, dev->dev_addr);
3876 +
3877 + err = fe_mdio_init(priv);
3878 + if (err)
3879 + return err;
3880 +
3881 + if (priv->phy) {
3882 + err = priv->phy->connect(priv);
3883 + if (err)
3884 + goto err_mdio_cleanup;
3885 + }
3886 +
3887 + if (priv->soc->port_init)
3888 + for_each_child_of_node(priv->device->of_node, port)
3889 + if (of_device_is_compatible(port, "ralink,eth-port"))
3890 + priv->soc->port_init(priv, port);
3891 +
3892 + err = fe_hw_init(dev);
3893 + if (err)
3894 + goto err_phy_disconnect;
3895 +
3896 + return 0;
3897 +
3898 +err_phy_disconnect:
3899 + if (priv->phy)
3900 + priv->phy->disconnect(priv);
3901 +err_mdio_cleanup:
3902 + fe_mdio_cleanup(priv);
3903 +
3904 + return err;
3905 +}
3906 +
3907 +static void fe_uninit(struct net_device *dev)
3908 +{
3909 + struct fe_priv *priv = netdev_priv(dev);
3910 +
3911 + tasklet_kill(&priv->tx_tasklet);
3912 +
3913 + if (priv->phy)
3914 + priv->phy->disconnect(priv);
3915 + fe_mdio_cleanup(priv);
3916 +
3917 + fe_reg_w32(0, FE_REG_FE_INT_ENABLE);
3918 + free_irq(dev->irq, dev);
3919 +
3920 + fe_free_dma(priv);
3921 +}
3922 +
3923 +static const struct net_device_ops fe_netdev_ops = {
3924 + .ndo_init = fe_init,
3925 + .ndo_uninit = fe_uninit,
3926 + .ndo_open = fe_open,
3927 + .ndo_stop = fe_stop,
3928 + .ndo_start_xmit = fe_start_xmit,
3929 + .ndo_tx_timeout = fe_tx_timeout,
3930 + .ndo_set_mac_address = fe_set_mac_address,
3931 + .ndo_change_mtu = eth_change_mtu,
3932 + .ndo_validate_addr = eth_validate_addr,
3933 +};
3934 +
3935 +static int fe_probe(struct platform_device *pdev)
3936 +{
3937 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3938 + const struct of_device_id *match;
3939 + struct fe_soc_data *soc = NULL;
3940 + struct net_device *netdev;
3941 + struct fe_priv *priv;
3942 + struct clk *sysclk;
3943 + int err;
3944 +
3945 + match = of_match_device(of_fe_match, &pdev->dev);
3946 + soc = (struct fe_soc_data *) match->data;
3947 + if (soc->reg_table)
3948 + fe_reg_table = soc->reg_table;
3949 +
3950 + fe_base = devm_request_and_ioremap(&pdev->dev, res);
3951 + if (!fe_base)
3952 + return -ENOMEM;
3953 +
3954 + netdev = alloc_etherdev(sizeof(struct fe_priv));
3955 + if (!netdev) {
3956 + dev_err(&pdev->dev, "alloc_etherdev failed\n");
3957 + return -ENOMEM;
3958 + }
3959 +
3960 + strcpy(netdev->name, "eth%d");
3961 + netdev->netdev_ops = &fe_netdev_ops;
3962 + netdev->base_addr = (unsigned long) fe_base;
3963 + netdev->watchdog_timeo = TX_TIMEOUT;
3964 + netdev->features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
3965 +
3966 + netdev->irq = platform_get_irq(pdev, 0);
3967 + if (netdev->irq < 0) {
3968 + dev_err(&pdev->dev, "no IRQ resource found\n");
3969 + kfree(netdev);
3970 + return -ENXIO;
3971 + }
3972 +
3973 + priv = netdev_priv(netdev);
3974 + memset(priv, 0, sizeof(struct fe_priv));
3975 + spin_lock_init(&priv->page_lock);
3976 +
3977 + sysclk = devm_clk_get(&pdev->dev, NULL);
3978 + if (!IS_ERR(sysclk))
3979 + priv->sysclk = clk_get_rate(sysclk);
3980 +
3981 + priv->netdev = netdev;
3982 + priv->device = &pdev->dev;
3983 + priv->soc = soc;
3984 +
3985 + err = register_netdev(netdev);
3986 + if (err) {
3987 + dev_err(&pdev->dev, "error bringing up device\n");
3988 + kfree(netdev);
3989 + return err;
3990 + }
3991 + netif_napi_add(netdev, &priv->rx_napi, fe_poll_rx, 32);
3992 +
3993 + platform_set_drvdata(pdev, netdev);
3994 +
3995 + netdev_info(netdev, "done loading\n");
3996 +
3997 + return 0;
3998 +}
3999 +
4000 +static int fe_remove(struct platform_device *pdev)
4001 +{
4002 + struct net_device *dev = platform_get_drvdata(pdev);
4003 + struct fe_priv *priv = netdev_priv(dev);
4004 +
4005 + netif_stop_queue(dev);
4006 + netif_napi_del(&priv->rx_napi);
4007 +
4008 + unregister_netdev(dev);
4009 + free_netdev(dev);
4010 +
4011 + return 0;
4012 +}
4013 +
4014 +static struct platform_driver fe_driver = {
4015 + .probe = fe_probe,
4016 + .remove = fe_remove,
4017 + .driver = {
4018 + .name = "ralink_soc_eth",
4019 + .owner = THIS_MODULE,
4020 + .of_match_table = of_fe_match,
4021 + },
4022 +};
4023 +
4024 +static int __init init_rtfe(void)
4025 +{
4026 + int ret;
4027 +
4028 + ret = rtesw_init();
4029 + if (ret)
4030 + return ret;
4031 +
4032 + ret = platform_driver_register(&fe_driver);
4033 + if (ret)
4034 + rtesw_exit();
4035 +
4036 + return ret;
4037 +}
4038 +
4039 +static void __exit exit_rtfe(void)
4040 +{
4041 + platform_driver_unregister(&fe_driver);
4042 + rtesw_exit();
4043 +}
4044 +
4045 +module_init(init_rtfe);
4046 +module_exit(exit_rtfe);
4047 +
4048 +MODULE_LICENSE("GPL");
4049 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
4050 +MODULE_DESCRIPTION("Ethernet driver for Ralink SoC");
4051 diff --git a/drivers/net/ethernet/ralink/ralink_soc_eth.h b/drivers/net/ethernet/ralink/ralink_soc_eth.h
4052 new file mode 100644
4053 index 0000000..85bc881
4054 --- /dev/null
4055 +++ b/drivers/net/ethernet/ralink/ralink_soc_eth.h
4056 @@ -0,0 +1,374 @@
4057 +/*
4058 + * This program is free software; you can redistribute it and/or modify
4059 + * it under the terms of the GNU General Public License as published by
4060 + * the Free Software Foundation; version 2 of the License
4061 + *
4062 + * This program is distributed in the hope that it will be useful,
4063 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4064 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4065 + * GNU General Public License for more details.
4066 + *
4067 + * You should have received a copy of the GNU General Public License
4068 + * along with this program; if not, write to the Free Software
4069 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4070 + *
4071 + * based on Ralink SDK3.3
4072 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4073 + */
4074 +
4075 +#ifndef FE_ETH_H
4076 +#define FE_ETH_H
4077 +
4078 +#include <linux/mii.h>
4079 +#include <linux/interrupt.h>
4080 +#include <linux/netdevice.h>
4081 +#include <linux/dma-mapping.h>
4082 +#include <linux/phy.h>
4083 +
4084 +
4085 +enum fe_reg {
4086 + FE_REG_PDMA_GLO_CFG = 0,
4087 + FE_REG_PDMA_RST_CFG,
4088 + FE_REG_DLY_INT_CFG,
4089 + FE_REG_TX_BASE_PTR0,
4090 + FE_REG_TX_MAX_CNT0,
4091 + FE_REG_TX_CTX_IDX0,
4092 + FE_REG_RX_BASE_PTR0,
4093 + FE_REG_RX_MAX_CNT0,
4094 + FE_REG_RX_CALC_IDX0,
4095 + FE_REG_FE_INT_ENABLE,
4096 + FE_REG_FE_INT_STATUS,
4097 + FE_REG_FE_DMA_VID_BASE,
4098 + FE_REG_COUNT
4099 +};
4100 +
4101 +#define NUM_DMA_DESC 0x100
4102 +
4103 +#define FE_DELAY_EN_INT 0x80
4104 +#define FE_DELAY_MAX_INT 0x04
4105 +#define FE_DELAY_MAX_TOUT 0x04
4106 +#define FE_DELAY_CHAN (((FE_DELAY_EN_INT | FE_DELAY_MAX_INT) << 8) | FE_DELAY_MAX_TOUT)
4107 +#define FE_DELAY_INIT ((FE_DELAY_CHAN << 16) | FE_DELAY_CHAN)
4108 +#define FE_PSE_FQFC_CFG_INIT 0x80504000
4109 +
4110 +/* interrupt bits */
4111 +#define FE_CNT_PPE_AF BIT(31)
4112 +#define FE_CNT_GDM_AF BIT(29)
4113 +#define FE_PSE_P2_FC BIT(26)
4114 +#define FE_PSE_BUF_DROP BIT(24)
4115 +#define FE_GDM_OTHER_DROP BIT(23)
4116 +#define FE_PSE_P1_FC BIT(22)
4117 +#define FE_PSE_P0_FC BIT(21)
4118 +#define FE_PSE_FQ_EMPTY BIT(20)
4119 +#define FE_GE1_STA_CHG BIT(18)
4120 +#define FE_TX_COHERENT BIT(17)
4121 +#define FE_RX_COHERENT BIT(16)
4122 +#define FE_TX_DONE_INT3 BIT(11)
4123 +#define FE_TX_DONE_INT2 BIT(10)
4124 +#define FE_TX_DONE_INT1 BIT(9)
4125 +#define FE_TX_DONE_INT0 BIT(8)
4126 +#define FE_RX_DONE_INT0 BIT(2)
4127 +#define FE_TX_DLY_INT BIT(1)
4128 +#define FE_RX_DLY_INT BIT(0)
4129 +
4130 +#define RT5350_RX_DLY_INT BIT(30)
4131 +#define RT5350_TX_DLY_INT BIT(28)
4132 +
4133 +/* registers */
4134 +#define FE_FE_OFFSET 0x0000
4135 +#define FE_GDMA_OFFSET 0x0020
4136 +#define FE_PSE_OFFSET 0x0040
4137 +#define FE_GDMA2_OFFSET 0x0060
4138 +#define FE_CDMA_OFFSET 0x0080
4139 +#define FE_DMA_VID0 0x00a8
4140 +#define FE_PDMA_OFFSET 0x0100
4141 +#define FE_PPE_OFFSET 0x0200
4142 +#define FE_CMTABLE_OFFSET 0x0400
4143 +#define FE_POLICYTABLE_OFFSET 0x1000
4144 +
4145 +#define RT5350_PDMA_OFFSET 0x0800
4146 +#define RT5350_SDM_OFFSET 0x0c00
4147 +
4148 +#define FE_MDIO_ACCESS (FE_FE_OFFSET + 0x00)
4149 +#define FE_MDIO_CFG (FE_FE_OFFSET + 0x04)
4150 +#define FE_FE_GLO_CFG (FE_FE_OFFSET + 0x08)
4151 +#define FE_FE_RST_GL (FE_FE_OFFSET + 0x0C)
4152 +#define FE_FE_INT_STATUS (FE_FE_OFFSET + 0x10)
4153 +#define FE_FE_INT_ENABLE (FE_FE_OFFSET + 0x14)
4154 +#define FE_MDIO_CFG2 (FE_FE_OFFSET + 0x18)
4155 +#define FE_FOC_TS_T (FE_FE_OFFSET + 0x1C)
4156 +
4157 +#define FE_GDMA1_FWD_CFG (FE_GDMA_OFFSET + 0x00)
4158 +#define FE_GDMA1_SCH_CFG (FE_GDMA_OFFSET + 0x04)
4159 +#define FE_GDMA1_SHPR_CFG (FE_GDMA_OFFSET + 0x08)
4160 +#define FE_GDMA1_MAC_ADRL (FE_GDMA_OFFSET + 0x0C)
4161 +#define FE_GDMA1_MAC_ADRH (FE_GDMA_OFFSET + 0x10)
4162 +
4163 +#define FE_GDMA2_FWD_CFG (FE_GDMA2_OFFSET + 0x00)
4164 +#define FE_GDMA2_SCH_CFG (FE_GDMA2_OFFSET + 0x04)
4165 +#define FE_GDMA2_SHPR_CFG (FE_GDMA2_OFFSET + 0x08)
4166 +#define FE_GDMA2_MAC_ADRL (FE_GDMA2_OFFSET + 0x0C)
4167 +#define FE_GDMA2_MAC_ADRH (FE_GDMA2_OFFSET + 0x10)
4168 +
4169 +#define FE_PSE_FQ_CFG (FE_PSE_OFFSET + 0x00)
4170 +#define FE_CDMA_FC_CFG (FE_PSE_OFFSET + 0x04)
4171 +#define FE_GDMA1_FC_CFG (FE_PSE_OFFSET + 0x08)
4172 +#define FE_GDMA2_FC_CFG (FE_PSE_OFFSET + 0x0C)
4173 +
4174 +#define FE_CDMA_CSG_CFG (FE_CDMA_OFFSET + 0x00)
4175 +#define FE_CDMA_SCH_CFG (FE_CDMA_OFFSET + 0x04)
4176 +
4177 +#define MT7620A_GDMA_OFFSET 0x0600
4178 +#define MT7620A_GDMA1_FWD_CFG (MT7620A_GDMA_OFFSET + 0x00)
4179 +#define MT7620A_FE_GDMA1_SCH_CFG (MT7620A_GDMA_OFFSET + 0x04)
4180 +#define MT7620A_FE_GDMA1_SHPR_CFG (MT7620A_GDMA_OFFSET + 0x08)
4181 +#define MT7620A_FE_GDMA1_MAC_ADRL (MT7620A_GDMA_OFFSET + 0x0C)
4182 +#define MT7620A_FE_GDMA1_MAC_ADRH (MT7620A_GDMA_OFFSET + 0x10)
4183 +
4184 +#define RT5350_TX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x00)
4185 +#define RT5350_TX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x04)
4186 +#define RT5350_TX_CTX_IDX0 (RT5350_PDMA_OFFSET + 0x08)
4187 +#define RT5350_TX_DTX_IDX0 (RT5350_PDMA_OFFSET + 0x0C)
4188 +#define RT5350_TX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x10)
4189 +#define RT5350_TX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x14)
4190 +#define RT5350_TX_CTX_IDX1 (RT5350_PDMA_OFFSET + 0x18)
4191 +#define RT5350_TX_DTX_IDX1 (RT5350_PDMA_OFFSET + 0x1C)
4192 +#define RT5350_TX_BASE_PTR2 (RT5350_PDMA_OFFSET + 0x20)
4193 +#define RT5350_TX_MAX_CNT2 (RT5350_PDMA_OFFSET + 0x24)
4194 +#define RT5350_TX_CTX_IDX2 (RT5350_PDMA_OFFSET + 0x28)
4195 +#define RT5350_TX_DTX_IDX2 (RT5350_PDMA_OFFSET + 0x2C)
4196 +#define RT5350_TX_BASE_PTR3 (RT5350_PDMA_OFFSET + 0x30)
4197 +#define RT5350_TX_MAX_CNT3 (RT5350_PDMA_OFFSET + 0x34)
4198 +#define RT5350_TX_CTX_IDX3 (RT5350_PDMA_OFFSET + 0x38)
4199 +#define RT5350_TX_DTX_IDX3 (RT5350_PDMA_OFFSET + 0x3C)
4200 +#define RT5350_RX_BASE_PTR0 (RT5350_PDMA_OFFSET + 0x100)
4201 +#define RT5350_RX_MAX_CNT0 (RT5350_PDMA_OFFSET + 0x104)
4202 +#define RT5350_RX_CALC_IDX0 (RT5350_PDMA_OFFSET + 0x108)
4203 +#define RT5350_RX_DRX_IDX0 (RT5350_PDMA_OFFSET + 0x10C)
4204 +#define RT5350_RX_BASE_PTR1 (RT5350_PDMA_OFFSET + 0x110)
4205 +#define RT5350_RX_MAX_CNT1 (RT5350_PDMA_OFFSET + 0x114)
4206 +#define RT5350_RX_CALC_IDX1 (RT5350_PDMA_OFFSET + 0x118)
4207 +#define RT5350_RX_DRX_IDX1 (RT5350_PDMA_OFFSET + 0x11C)
4208 +#define RT5350_PDMA_GLO_CFG (RT5350_PDMA_OFFSET + 0x204)
4209 +#define RT5350_PDMA_RST_CFG (RT5350_PDMA_OFFSET + 0x208)
4210 +#define RT5350_DLY_INT_CFG (RT5350_PDMA_OFFSET + 0x20c)
4211 +#define RT5350_FE_INT_STATUS (RT5350_PDMA_OFFSET + 0x220)
4212 +#define RT5350_FE_INT_ENABLE (RT5350_PDMA_OFFSET + 0x228)
4213 +#define RT5350_PDMA_SCH_CFG (RT5350_PDMA_OFFSET + 0x280)
4214 +
4215 +#define FE_PDMA_GLO_CFG (FE_PDMA_OFFSET + 0x00)
4216 +#define FE_PDMA_RST_CFG (FE_PDMA_OFFSET + 0x04)
4217 +#define FE_PDMA_SCH_CFG (FE_PDMA_OFFSET + 0x08)
4218 +#define FE_DLY_INT_CFG (FE_PDMA_OFFSET + 0x0C)
4219 +#define FE_TX_BASE_PTR0 (FE_PDMA_OFFSET + 0x10)
4220 +#define FE_TX_MAX_CNT0 (FE_PDMA_OFFSET + 0x14)
4221 +#define FE_TX_CTX_IDX0 (FE_PDMA_OFFSET + 0x18)
4222 +#define FE_TX_DTX_IDX0 (FE_PDMA_OFFSET + 0x1C)
4223 +#define FE_TX_BASE_PTR1 (FE_PDMA_OFFSET + 0x20)
4224 +#define FE_TX_MAX_CNT1 (FE_PDMA_OFFSET + 0x24)
4225 +#define FE_TX_CTX_IDX1 (FE_PDMA_OFFSET + 0x28)
4226 +#define FE_TX_DTX_IDX1 (FE_PDMA_OFFSET + 0x2C)
4227 +#define FE_RX_BASE_PTR0 (FE_PDMA_OFFSET + 0x30)
4228 +#define FE_RX_MAX_CNT0 (FE_PDMA_OFFSET + 0x34)
4229 +#define FE_RX_CALC_IDX0 (FE_PDMA_OFFSET + 0x38)
4230 +#define FE_RX_DRX_IDX0 (FE_PDMA_OFFSET + 0x3C)
4231 +#define FE_TX_BASE_PTR2 (FE_PDMA_OFFSET + 0x40)
4232 +#define FE_TX_MAX_CNT2 (FE_PDMA_OFFSET + 0x44)
4233 +#define FE_TX_CTX_IDX2 (FE_PDMA_OFFSET + 0x48)
4234 +#define FE_TX_DTX_IDX2 (FE_PDMA_OFFSET + 0x4C)
4235 +#define FE_TX_BASE_PTR3 (FE_PDMA_OFFSET + 0x50)
4236 +#define FE_TX_MAX_CNT3 (FE_PDMA_OFFSET + 0x54)
4237 +#define FE_TX_CTX_IDX3 (FE_PDMA_OFFSET + 0x58)
4238 +#define FE_TX_DTX_IDX3 (FE_PDMA_OFFSET + 0x5C)
4239 +#define FE_RX_BASE_PTR1 (FE_PDMA_OFFSET + 0x60)
4240 +#define FE_RX_MAX_CNT1 (FE_PDMA_OFFSET + 0x64)
4241 +#define FE_RX_CALC_IDX1 (FE_PDMA_OFFSET + 0x68)
4242 +#define FE_RX_DRX_IDX1 (FE_PDMA_OFFSET + 0x6C)
4243 +
4244 +#define RT5350_SDM_CFG (RT5350_SDM_OFFSET + 0x00) //Switch DMA configuration
4245 +#define RT5350_SDM_RRING (RT5350_SDM_OFFSET + 0x04) //Switch DMA Rx Ring
4246 +#define RT5350_SDM_TRING (RT5350_SDM_OFFSET + 0x08) //Switch DMA Tx Ring
4247 +#define RT5350_SDM_MAC_ADRL (RT5350_SDM_OFFSET + 0x0C) //Switch MAC address LSB
4248 +#define RT5350_SDM_MAC_ADRH (RT5350_SDM_OFFSET + 0x10) //Switch MAC Address MSB
4249 +#define RT5350_SDM_TPCNT (RT5350_SDM_OFFSET + 0x100) //Switch DMA Tx packet count
4250 +#define RT5350_SDM_TBCNT (RT5350_SDM_OFFSET + 0x104) //Switch DMA Tx byte count
4251 +#define RT5350_SDM_RPCNT (RT5350_SDM_OFFSET + 0x108) //Switch DMA rx packet count
4252 +#define RT5350_SDM_RBCNT (RT5350_SDM_OFFSET + 0x10C) //Switch DMA rx byte count
4253 +#define RT5350_SDM_CS_ERR (RT5350_SDM_OFFSET + 0x110) //Switch DMA rx checksum error count
4254 +
4255 +#define RT5350_SDM_ICS_EN BIT(16)
4256 +#define RT5350_SDM_TCS_EN BIT(17)
4257 +#define RT5350_SDM_UCS_EN BIT(18)
4258 +
4259 +
4260 +/* MDIO_CFG register bits */
4261 +#define FE_MDIO_CFG_AUTO_POLL_EN BIT(29)
4262 +#define FE_MDIO_CFG_GP1_BP_EN BIT(16)
4263 +#define FE_MDIO_CFG_GP1_FRC_EN BIT(15)
4264 +#define FE_MDIO_CFG_GP1_SPEED_10 (0 << 13)
4265 +#define FE_MDIO_CFG_GP1_SPEED_100 (1 << 13)
4266 +#define FE_MDIO_CFG_GP1_SPEED_1000 (2 << 13)
4267 +#define FE_MDIO_CFG_GP1_DUPLEX BIT(12)
4268 +#define FE_MDIO_CFG_GP1_FC_TX BIT(11)
4269 +#define FE_MDIO_CFG_GP1_FC_RX BIT(10)
4270 +#define FE_MDIO_CFG_GP1_LNK_DWN BIT(9)
4271 +#define FE_MDIO_CFG_GP1_AN_FAIL BIT(8)
4272 +#define FE_MDIO_CFG_MDC_CLK_DIV_1 (0 << 6)
4273 +#define FE_MDIO_CFG_MDC_CLK_DIV_2 (1 << 6)
4274 +#define FE_MDIO_CFG_MDC_CLK_DIV_4 (2 << 6)
4275 +#define FE_MDIO_CFG_MDC_CLK_DIV_8 (3 << 6)
4276 +#define FE_MDIO_CFG_TURBO_MII_FREQ BIT(5)
4277 +#define FE_MDIO_CFG_TURBO_MII_MODE BIT(4)
4278 +#define FE_MDIO_CFG_RX_CLK_SKEW_0 (0 << 2)
4279 +#define FE_MDIO_CFG_RX_CLK_SKEW_200 (1 << 2)
4280 +#define FE_MDIO_CFG_RX_CLK_SKEW_400 (2 << 2)
4281 +#define FE_MDIO_CFG_RX_CLK_SKEW_INV (3 << 2)
4282 +#define FE_MDIO_CFG_TX_CLK_SKEW_0 0
4283 +#define FE_MDIO_CFG_TX_CLK_SKEW_200 1
4284 +#define FE_MDIO_CFG_TX_CLK_SKEW_400 2
4285 +#define FE_MDIO_CFG_TX_CLK_SKEW_INV 3
4286 +
4287 +/* uni-cast port */
4288 +#define FE_GDM1_ICS_EN BIT(22)
4289 +#define FE_GDM1_TCS_EN BIT(21)
4290 +#define FE_GDM1_UCS_EN BIT(20)
4291 +#define FE_GDM1_JMB_EN BIT(19)
4292 +#define FE_GDM1_STRPCRC BIT(16)
4293 +#define FE_GDM1_UFRC_P_CPU (0 << 12)
4294 +#define FE_GDM1_UFRC_P_GDMA1 (1 << 12)
4295 +#define FE_GDM1_UFRC_P_PPE (6 << 12)
4296 +
4297 +/* checksums */
4298 +#define FE_ICS_GEN_EN BIT(2)
4299 +#define FE_UCS_GEN_EN BIT(1)
4300 +#define FE_TCS_GEN_EN BIT(0)
4301 +
4302 +/* dma ring */
4303 +#define FE_PST_DRX_IDX0 BIT(16)
4304 +#define FE_PST_DTX_IDX3 BIT(3)
4305 +#define FE_PST_DTX_IDX2 BIT(2)
4306 +#define FE_PST_DTX_IDX1 BIT(1)
4307 +#define FE_PST_DTX_IDX0 BIT(0)
4308 +
4309 +#define FE_TX_WB_DDONE BIT(6)
4310 +#define FE_RX_DMA_BUSY BIT(3)
4311 +#define FE_TX_DMA_BUSY BIT(1)
4312 +#define FE_RX_DMA_EN BIT(2)
4313 +#define FE_TX_DMA_EN BIT(0)
4314 +
4315 +#define FE_PDMA_SIZE_4DWORDS (0 << 4)
4316 +#define FE_PDMA_SIZE_8DWORDS (1 << 4)
4317 +#define FE_PDMA_SIZE_16DWORDS (2 << 4)
4318 +
4319 +#define FE_US_CYC_CNT_MASK 0xff
4320 +#define FE_US_CYC_CNT_SHIFT 0x8
4321 +#define FE_US_CYC_CNT_DIVISOR 1000000
4322 +
4323 +#define RX_DMA_PLEN0(_x) (((_x) >> 16) & 0x3fff)
4324 +#define RX_DMA_LSO BIT(30)
4325 +#define RX_DMA_DONE BIT(31)
4326 +#define RX_DMA_L4VALID BIT(30)
4327 +
4328 +struct fe_rx_dma {
4329 + unsigned int rxd1;
4330 + unsigned int rxd2;
4331 + unsigned int rxd3;
4332 + unsigned int rxd4;
4333 +} __packed __aligned(4);
4334 +
4335 +#define TX_DMA_PLEN0_MASK ((0x3fff) << 16)
4336 +#define TX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
4337 +#define TX_DMA_LSO BIT(30)
4338 +#define TX_DMA_DONE BIT(31)
4339 +#define TX_DMA_QN(_x) ((_x) << 16)
4340 +#define TX_DMA_PN(_x) ((_x) << 24)
4341 +#define TX_DMA_QN_MASK TX_DMA_QN(0x7)
4342 +#define TX_DMA_PN_MASK TX_DMA_PN(0x7)
4343 +#define TX_DMA_CHKSUM (0x7 << 29)
4344 +
4345 +struct fe_tx_dma {
4346 + unsigned int txd1;
4347 + unsigned int txd2;
4348 + unsigned int txd3;
4349 + unsigned int txd4;
4350 +} __packed __aligned(4);
4351 +
4352 +struct fe_priv;
4353 +
4354 +struct fe_phy {
4355 + struct phy_device *phy[8];
4356 + struct device_node *phy_node[8];
4357 + const __be32 *phy_fixed[8];
4358 + int duplex[8];
4359 + int speed[8];
4360 + int tx_fc[8];
4361 + int rx_fc[8];
4362 + spinlock_t lock;
4363 +
4364 + int (*connect)(struct fe_priv *priv);
4365 + void (*disconnect)(struct fe_priv *priv);
4366 + void (*start)(struct fe_priv *priv);
4367 + void (*stop)(struct fe_priv *priv);
4368 +};
4369 +
4370 +struct fe_soc_data
4371 +{
4372 + unsigned char mac[6];
4373 + const u32 *reg_table;
4374 +
4375 + void (*reset_fe)(void);
4376 + void (*set_mac)(struct fe_priv *priv, unsigned char *mac);
4377 + void (*fwd_config)(struct fe_priv *priv);
4378 + void (*tx_dma)(struct fe_priv *priv, int idx, int len);
4379 + void (*rx_dma)(struct fe_priv *priv, int idx, int len);
4380 + int (*switch_init)(struct fe_priv *priv);
4381 + void (*port_init)(struct fe_priv *priv, struct device_node *port);
4382 + int (*has_carrier)(struct fe_priv *priv);
4383 + int (*mdio_init)(struct fe_priv *priv);
4384 + void (*mdio_cleanup)(struct fe_priv *priv);
4385 + int (*mdio_write)(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
4386 + int (*mdio_read)(struct mii_bus *bus, int phy_addr, int phy_reg);
4387 + void (*mdio_adjust_link)(struct fe_priv *priv, int port);
4388 +
4389 + void *swpriv;
4390 + u32 pdma_glo_cfg;
4391 + u32 rx_dly_int;
4392 + u32 tx_dly_int;
4393 + u32 checksum_bit;
4394 +
4395 + int min_pkt_len;
4396 +};
4397 +
4398 +struct fe_priv
4399 +{
4400 + spinlock_t page_lock;
4401 +
4402 + struct fe_soc_data *soc;
4403 + struct net_device *netdev;
4404 + struct device *device;
4405 + unsigned long sysclk;
4406 +
4407 + struct fe_rx_dma *rx_dma;
4408 + struct napi_struct rx_napi;
4409 + struct sk_buff *rx_skb[NUM_DMA_DESC];
4410 + dma_addr_t rx_phys;
4411 +
4412 + struct fe_tx_dma *tx_dma;
4413 + struct tasklet_struct tx_tasklet;
4414 + struct sk_buff *tx_skb[NUM_DMA_DESC];
4415 + dma_addr_t tx_phys;
4416 + unsigned int tx_free_idx;
4417 +
4418 + struct fe_phy *phy;
4419 + struct mii_bus *mii_bus;
4420 + int mii_irq[PHY_MAX_ADDR];
4421 +
4422 + int link[8];
4423 +};
4424 +
4425 +extern const struct of_device_id of_fe_match[];
4426 +
4427 +void fe_w32(u32 val, unsigned reg);
4428 +u32 fe_r32(unsigned reg);
4429 +
4430 +#endif /* FE_ETH_H */
4431 diff --git a/drivers/net/ethernet/ralink/soc_mt7620.c b/drivers/net/ethernet/ralink/soc_mt7620.c
4432 new file mode 100644
4433 index 0000000..55e303f
4434 --- /dev/null
4435 +++ b/drivers/net/ethernet/ralink/soc_mt7620.c
4436 @@ -0,0 +1,111 @@
4437 +/*
4438 + * This program is free software; you can redistribute it and/or modify
4439 + * it under the terms of the GNU General Public License as published by
4440 + * the Free Software Foundation; version 2 of the License
4441 + *
4442 + * This program is distributed in the hope that it will be useful,
4443 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4444 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4445 + * GNU General Public License for more details.
4446 + *
4447 + * You should have received a copy of the GNU General Public License
4448 + * along with this program; if not, write to the Free Software
4449 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4450 + *
4451 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4452 + */
4453 +
4454 +#include <linux/module.h>
4455 +#include <linux/platform_device.h>
4456 +
4457 +#include <asm/mach-ralink/ralink_regs.h>
4458 +
4459 +#include "ralink_soc_eth.h"
4460 +#include "gsw_mt7620a.h"
4461 +
4462 +#define MT7620A_CDMA_CSG_CFG 0x400
4463 +#define MT7620_DMA_VID (MT7620A_CDMA_CSG_CFG | 0x30)
4464 +#define MT7620A_DMA_2B_OFFSET BIT(31)
4465 +#define MT7620A_RESET_FE BIT(21)
4466 +#define MT7620A_RESET_ESW BIT(23)
4467 +#define MT7620_L4_VALID BIT(23)
4468 +
4469 +#define SYSC_REG_RESET_CTRL 0x34
4470 +#define MAX_RX_LENGTH 1536
4471 +
4472 +#define CDMA_ICS_EN BIT(2)
4473 +#define CDMA_UCS_EN BIT(1)
4474 +#define CDMA_TCS_EN BIT(0)
4475 +
4476 +#define GDMA_ICS_EN BIT(22)
4477 +#define GDMA_TCS_EN BIT(21)
4478 +#define GDMA_UCS_EN BIT(20)
4479 +
4480 +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
4481 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
4482 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
4483 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
4484 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
4485 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
4486 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
4487 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
4488 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
4489 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
4490 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
4491 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
4492 + [FE_REG_FE_DMA_VID_BASE] = MT7620_DMA_VID,
4493 +};
4494 +
4495 +static void mt7620_fe_reset(void)
4496 +{
4497 + rt_sysc_w32(MT7620A_RESET_FE | MT7620A_RESET_ESW, SYSC_REG_RESET_CTRL);
4498 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
4499 +}
4500 +
4501 +static void mt7620_fwd_config(struct fe_priv *priv)
4502 +{
4503 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) & ~7, MT7620A_GDMA1_FWD_CFG);
4504 + fe_w32(fe_r32(MT7620A_GDMA1_FWD_CFG) | (GDMA_ICS_EN | GDMA_TCS_EN | GDMA_UCS_EN), MT7620A_GDMA1_FWD_CFG);
4505 + fe_w32(fe_r32(MT7620A_CDMA_CSG_CFG) | (CDMA_ICS_EN | CDMA_UCS_EN | CDMA_TCS_EN), MT7620A_CDMA_CSG_CFG);
4506 +}
4507 +
4508 +static void mt7620_tx_dma(struct fe_priv *priv, int idx, int len)
4509 +{
4510 + if (len)
4511 + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_PLEN0(len);
4512 + else
4513 + priv->tx_dma[idx].txd2 = TX_DMA_LSO | TX_DMA_DONE;
4514 +}
4515 +
4516 +static void mt7620_rx_dma(struct fe_priv *priv, int idx, int len)
4517 +{
4518 + priv->rx_dma[idx].rxd2 = RX_DMA_PLEN0(len);
4519 +}
4520 +
4521 +static struct fe_soc_data mt7620_data = {
4522 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4523 + .reset_fe = mt7620_fe_reset,
4524 + .set_mac = mt7620_set_mac,
4525 + .fwd_config = mt7620_fwd_config,
4526 + .tx_dma = mt7620_tx_dma,
4527 + .rx_dma = mt7620_rx_dma,
4528 + .switch_init = mt7620_gsw_probe,
4529 + .port_init = mt7620_port_init,
4530 + .min_pkt_len = 0,
4531 + .reg_table = rt5350_reg_table,
4532 + .pdma_glo_cfg = FE_PDMA_SIZE_16DWORDS | MT7620A_DMA_2B_OFFSET,
4533 + .rx_dly_int = RT5350_RX_DLY_INT,
4534 + .tx_dly_int = RT5350_TX_DLY_INT,
4535 + .checksum_bit = MT7620_L4_VALID,
4536 + .has_carrier = mt7620a_has_carrier,
4537 + .mdio_read = mt7620_mdio_read,
4538 + .mdio_write = mt7620_mdio_write,
4539 + .mdio_adjust_link = mt7620_mdio_link_adjust,
4540 +};
4541 +
4542 +const struct of_device_id of_fe_match[] = {
4543 + { .compatible = "ralink,mt7620a-eth", .data = &mt7620_data },
4544 + {},
4545 +};
4546 +
4547 +MODULE_DEVICE_TABLE(of, of_fe_match);
4548 diff --git a/drivers/net/ethernet/ralink/soc_rt2880.c b/drivers/net/ethernet/ralink/soc_rt2880.c
4549 new file mode 100644
4550 index 0000000..1110947
4551 --- /dev/null
4552 +++ b/drivers/net/ethernet/ralink/soc_rt2880.c
4553 @@ -0,0 +1,51 @@
4554 +/*
4555 + * This program is free software; you can redistribute it and/or modify
4556 + * it under the terms of the GNU General Public License as published by
4557 + * the Free Software Foundation; version 2 of the License
4558 + *
4559 + * This program is distributed in the hope that it will be useful,
4560 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4561 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4562 + * GNU General Public License for more details.
4563 + *
4564 + * You should have received a copy of the GNU General Public License
4565 + * along with this program; if not, write to the Free Software
4566 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4567 + *
4568 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4569 + */
4570 +
4571 +#include <linux/module.h>
4572 +
4573 +#include <asm/mach-ralink/ralink_regs.h>
4574 +
4575 +#include "ralink_soc_eth.h"
4576 +#include "mdio_rt2880.h"
4577 +
4578 +#define SYSC_REG_RESET_CTRL 0x034
4579 +#define RT2880_RESET_FE BIT(18)
4580 +
4581 +void rt2880_fe_reset(void)
4582 +{
4583 + rt_sysc_w32(RT2880_RESET_FE, SYSC_REG_RESET_CTRL);
4584 +}
4585 +
4586 +struct fe_soc_data rt2880_data = {
4587 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4588 + .reset_fe = rt2880_fe_reset,
4589 + .min_pkt_len = 64,
4590 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
4591 + .checksum_bit = RX_DMA_L4VALID,
4592 + .rx_dly_int = FE_RX_DLY_INT,
4593 + .tx_dly_int = FE_TX_DLY_INT,
4594 + .mdio_read = rt2880_mdio_read,
4595 + .mdio_write = rt2880_mdio_write,
4596 + .mdio_adjust_link = rt2880_mdio_link_adjust,
4597 +};
4598 +
4599 +const struct of_device_id of_fe_match[] = {
4600 + { .compatible = "ralink,rt2880-eth", .data = &rt2880_data },
4601 + {},
4602 +};
4603 +
4604 +MODULE_DEVICE_TABLE(of, of_fe_match);
4605 diff --git a/drivers/net/ethernet/ralink/soc_rt305x.c b/drivers/net/ethernet/ralink/soc_rt305x.c
4606 new file mode 100644
4607 index 0000000..482ca1f
4608 --- /dev/null
4609 +++ b/drivers/net/ethernet/ralink/soc_rt305x.c
4610 @@ -0,0 +1,113 @@
4611 +/*
4612 + * This program is free software; you can redistribute it and/or modify
4613 + * it under the terms of the GNU General Public License as published by
4614 + * the Free Software Foundation; version 2 of the License
4615 + *
4616 + * This program is distributed in the hope that it will be useful,
4617 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4618 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4619 + * GNU General Public License for more details.
4620 + *
4621 + * You should have received a copy of the GNU General Public License
4622 + * along with this program; if not, write to the Free Software
4623 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4624 + *
4625 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4626 + */
4627 +
4628 +#include <linux/module.h>
4629 +
4630 +#include <asm/mach-ralink/ralink_regs.h>
4631 +
4632 +#include "ralink_soc_eth.h"
4633 +
4634 +#define RT305X_RESET_FE BIT(21)
4635 +#define RT305X_RESET_ESW BIT(23)
4636 +#define SYSC_REG_RESET_CTRL 0x034
4637 +
4638 +static const u32 rt5350_reg_table[FE_REG_COUNT] = {
4639 + [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
4640 + [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
4641 + [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
4642 + [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
4643 + [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
4644 + [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
4645 + [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
4646 + [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
4647 + [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
4648 + [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
4649 + [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
4650 + [FE_REG_FE_DMA_VID_BASE] = 0,
4651 +};
4652 +
4653 +static void rt305x_fe_reset(void)
4654 +{
4655 + rt_sysc_w32(RT305X_RESET_FE, SYSC_REG_RESET_CTRL);
4656 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
4657 +}
4658 +
4659 +static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
4660 +{
4661 + unsigned long flags;
4662 +
4663 + spin_lock_irqsave(&priv->page_lock, flags);
4664 + fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
4665 + fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
4666 + RT5350_SDM_MAC_ADRL);
4667 + spin_unlock_irqrestore(&priv->page_lock, flags);
4668 +}
4669 +
4670 +static void rt5350_fwd_config(struct fe_priv *priv)
4671 +{
4672 + unsigned long sysclk = priv->sysclk;
4673 +
4674 + if (sysclk) {
4675 + sysclk /= FE_US_CYC_CNT_DIVISOR;
4676 + sysclk <<= FE_US_CYC_CNT_SHIFT;
4677 +
4678 + fe_w32((fe_r32(FE_FE_GLO_CFG) &
4679 + ~(FE_US_CYC_CNT_MASK << FE_US_CYC_CNT_SHIFT)) | sysclk,
4680 + FE_FE_GLO_CFG);
4681 + }
4682 +
4683 + fe_w32(fe_r32(RT5350_SDM_CFG) & ~0xffff, RT5350_SDM_CFG);
4684 + fe_w32(fe_r32(RT5350_SDM_CFG) | RT5350_SDM_ICS_EN | RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN,
4685 + RT5350_SDM_CFG);
4686 +}
4687 +
4688 +static void rt5350_fe_reset(void)
4689 +{
4690 + rt_sysc_w32(RT305X_RESET_FE | RT305X_RESET_ESW, SYSC_REG_RESET_CTRL);
4691 + rt_sysc_w32(0, SYSC_REG_RESET_CTRL);
4692 +}
4693 +
4694 +static struct fe_soc_data rt3050_data = {
4695 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4696 + .reset_fe = rt305x_fe_reset,
4697 + .min_pkt_len = 64,
4698 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
4699 + .checksum_bit = RX_DMA_L4VALID,
4700 + .rx_dly_int = FE_RX_DLY_INT,
4701 + .tx_dly_int = FE_TX_DLY_INT,
4702 +};
4703 +
4704 +static struct fe_soc_data rt5350_data = {
4705 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4706 + .reg_table = rt5350_reg_table,
4707 + .reset_fe = rt5350_fe_reset,
4708 + .set_mac = rt5350_set_mac,
4709 + .fwd_config = rt5350_fwd_config,
4710 + .min_pkt_len = 64,
4711 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
4712 + .checksum_bit = RX_DMA_L4VALID,
4713 + .rx_dly_int = RT5350_RX_DLY_INT,
4714 + .tx_dly_int = RT5350_TX_DLY_INT,
4715 +};
4716 +
4717 +const struct of_device_id of_fe_match[] = {
4718 + { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
4719 + { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
4720 + {},
4721 +};
4722 +
4723 +MODULE_DEVICE_TABLE(of, of_fe_match);
4724 diff --git a/drivers/net/ethernet/ralink/soc_rt3883.c b/drivers/net/ethernet/ralink/soc_rt3883.c
4725 new file mode 100644
4726 index 0000000..c660529
4727 --- /dev/null
4728 +++ b/drivers/net/ethernet/ralink/soc_rt3883.c
4729 @@ -0,0 +1,60 @@
4730 +/*
4731 + * This program is free software; you can redistribute it and/or modify
4732 + * it under the terms of the GNU General Public License as published by
4733 + * the Free Software Foundation; version 2 of the License
4734 + *
4735 + * This program is distributed in the hope that it will be useful,
4736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4738 + * GNU General Public License for more details.
4739 + *
4740 + * You should have received a copy of the GNU General Public License
4741 + * along with this program; if not, write to the Free Software
4742 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
4743 + *
4744 + * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
4745 + */
4746 +
4747 +#include <linux/module.h>
4748 +
4749 +#include <asm/mach-ralink/ralink_regs.h>
4750 +
4751 +#include "ralink_soc_eth.h"
4752 +#include "mdio_rt2880.h"
4753 +
4754 +#define RT3883_SYSC_REG_RSTCTRL 0x34
4755 +#define RT3883_RSTCTRL_FE BIT(21)
4756 +
4757 +static void rt3883_fe_reset(void)
4758 +{
4759 + u32 t;
4760 +
4761 + t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
4762 + t |= RT3883_RSTCTRL_FE;
4763 + rt_sysc_w32(t , RT3883_SYSC_REG_RSTCTRL);
4764 +
4765 + t &= ~RT3883_RSTCTRL_FE;
4766 + rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
4767 +}
4768 +
4769 +static struct fe_soc_data rt3883_data = {
4770 + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
4771 + .reset_fe = rt3883_fe_reset,
4772 + .min_pkt_len = 64,
4773 + .pdma_glo_cfg = FE_PDMA_SIZE_4DWORDS,
4774 + .rx_dly_int = FE_RX_DLY_INT,
4775 + .tx_dly_int = FE_TX_DLY_INT,
4776 + .checksum_bit = RX_DMA_L4VALID,
4777 + .mdio_read = rt2880_mdio_read,
4778 + .mdio_write = rt2880_mdio_write,
4779 + .mdio_adjust_link = rt2880_mdio_link_adjust,
4780 + .port_init = rt2880_port_init,
4781 +};
4782 +
4783 +const struct of_device_id of_fe_match[] = {
4784 + { .compatible = "ralink,rt3883-eth", .data = &rt3883_data },
4785 + {},
4786 +};
4787 +
4788 +MODULE_DEVICE_TABLE(of, of_fe_match);
4789 +
4790 --
4791 1.7.10.4
4792