1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
10 * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
11 * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
12 * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
13 * Copyright (C) 2016 Vittorio Gambaletta <openwrt@vittgam.net>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <asm/mach-ralink/ralink_regs.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
23 #include <linux/switch.h>
24 #include <linux/reset.h>
26 #include "mtk_eth_soc.h"
28 /* HW limitations for this switch:
29 * - No large frame support (PKT_MAX_LEN at most 1536)
30 * - Can't have untagged vlan and tagged vlan on one port at the same time,
31 * though this might be possible using the undocumented PPE.
34 #define RT305X_ESW_REG_ISR 0x00
35 #define RT305X_ESW_REG_IMR 0x04
36 #define RT305X_ESW_REG_FCT0 0x08
37 #define RT305X_ESW_REG_PFC1 0x14
38 #define RT305X_ESW_REG_ATS 0x24
39 #define RT305X_ESW_REG_ATS0 0x28
40 #define RT305X_ESW_REG_ATS1 0x2c
41 #define RT305X_ESW_REG_ATS2 0x30
42 #define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
43 #define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
44 #define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
45 #define RT305X_ESW_REG_POA 0x80
46 #define RT305X_ESW_REG_FPA 0x84
47 #define RT305X_ESW_REG_SOCPC 0x8c
48 #define RT305X_ESW_REG_POC0 0x90
49 #define RT305X_ESW_REG_POC1 0x94
50 #define RT305X_ESW_REG_POC2 0x98
51 #define RT305X_ESW_REG_SGC 0x9c
52 #define RT305X_ESW_REG_STRT 0xa0
53 #define RT305X_ESW_REG_PCR0 0xc0
54 #define RT305X_ESW_REG_PCR1 0xc4
55 #define RT305X_ESW_REG_FPA2 0xc8
56 #define RT305X_ESW_REG_FCT2 0xcc
57 #define RT305X_ESW_REG_SGC2 0xe4
58 #define RT305X_ESW_REG_P0LED 0xa4
59 #define RT305X_ESW_REG_P1LED 0xa8
60 #define RT305X_ESW_REG_P2LED 0xac
61 #define RT305X_ESW_REG_P3LED 0xb0
62 #define RT305X_ESW_REG_P4LED 0xb4
63 #define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
64 #define RT305X_ESW_REG_P1PC 0xec
65 #define RT305X_ESW_REG_P2PC 0xf0
66 #define RT305X_ESW_REG_P3PC 0xf4
67 #define RT305X_ESW_REG_P4PC 0xf8
68 #define RT305X_ESW_REG_P5PC 0xfc
70 #define RT305X_ESW_LED_LINK 0
71 #define RT305X_ESW_LED_100M 1
72 #define RT305X_ESW_LED_DUPLEX 2
73 #define RT305X_ESW_LED_ACTIVITY 3
74 #define RT305X_ESW_LED_COLLISION 4
75 #define RT305X_ESW_LED_LINKACT 5
76 #define RT305X_ESW_LED_DUPLCOLL 6
77 #define RT305X_ESW_LED_10MACT 7
78 #define RT305X_ESW_LED_100MACT 8
79 /* Additional led states not in datasheet: */
80 #define RT305X_ESW_LED_BLINK 10
81 #define RT305X_ESW_LED_ON 12
83 #define RT305X_ESW_LINK_S 25
84 #define RT305X_ESW_DUPLEX_S 9
85 #define RT305X_ESW_SPD_S 0
87 #define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
88 #define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
89 #define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
91 #define RT305X_ESW_PCR1_WT_DONE BIT(0)
93 #define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
94 #define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
96 #define RT305X_ESW_PVIDC_PVID_M 0xfff
97 #define RT305X_ESW_PVIDC_PVID_S 12
99 #define RT305X_ESW_VLANI_VID_M 0xfff
100 #define RT305X_ESW_VLANI_VID_S 12
102 #define RT305X_ESW_VMSC_MSC_M 0xff
103 #define RT305X_ESW_VMSC_MSC_S 8
105 #define RT305X_ESW_SOCPC_DISUN2CPU_S 0
106 #define RT305X_ESW_SOCPC_DISMC2CPU_S 8
107 #define RT305X_ESW_SOCPC_DISBC2CPU_S 16
108 #define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
110 #define RT305X_ESW_POC0_EN_BP_S 0
111 #define RT305X_ESW_POC0_EN_FC_S 8
112 #define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
113 #define RT305X_ESW_POC0_DIS_PORT_M 0x7f
114 #define RT305X_ESW_POC0_DIS_PORT_S 23
116 #define RT305X_ESW_POC2_UNTAG_EN_M 0xff
117 #define RT305X_ESW_POC2_UNTAG_EN_S 0
118 #define RT305X_ESW_POC2_ENAGING_S 8
119 #define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
121 #define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
122 #define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
123 #define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
124 #define RT305X_ESW_SGC2_LAN_PMAP_S 24
126 #define RT305X_ESW_PFC1_EN_VLAN_M 0xff
127 #define RT305X_ESW_PFC1_EN_VLAN_S 16
128 #define RT305X_ESW_PFC1_EN_TOS_S 24
130 #define RT305X_ESW_VLAN_NONE 0xfff
132 #define RT305X_ESW_GSC_BC_STROM_MASK 0x3
133 #define RT305X_ESW_GSC_BC_STROM_SHIFT 4
135 #define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
136 #define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
138 #define RT305X_ESW_POA_LINK_MASK 0x1f
139 #define RT305X_ESW_POA_LINK_SHIFT 25
141 #define RT305X_ESW_PORT_ST_CHG BIT(26)
142 #define RT305X_ESW_PORT0 0
143 #define RT305X_ESW_PORT1 1
144 #define RT305X_ESW_PORT2 2
145 #define RT305X_ESW_PORT3 3
146 #define RT305X_ESW_PORT4 4
147 #define RT305X_ESW_PORT5 5
148 #define RT305X_ESW_PORT6 6
150 #define RT305X_ESW_PORTS_NONE 0
152 #define RT305X_ESW_PMAP_LLLLLL 0x3f
153 #define RT305X_ESW_PMAP_LLLLWL 0x2f
154 #define RT305X_ESW_PMAP_WLLLLL 0x3e
156 #define RT305X_ESW_PORTS_INTERNAL \
157 (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
158 BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
159 BIT(RT305X_ESW_PORT4))
161 #define RT305X_ESW_PORTS_NOCPU \
162 (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
164 #define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
166 #define RT305X_ESW_PORTS_ALL \
167 (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
169 #define RT305X_ESW_NUM_VLANS 16
170 #define RT305X_ESW_NUM_VIDS 4096
171 #define RT305X_ESW_NUM_PORTS 7
172 #define RT305X_ESW_NUM_LANWAN 6
173 #define RT305X_ESW_NUM_LEDS 5
175 #define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
176 #define RT5350_EWS_REG_LED_POLARITY 0x168
179 /* Global attributes. */
180 RT305X_ESW_ATTR_ENABLE_VLAN
,
181 RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
182 RT305X_ESW_ATTR_BC_STATUS
,
183 RT305X_ESW_ATTR_LED_FREQ
,
184 /* Port attributes. */
185 RT305X_ESW_ATTR_PORT_DISABLE
,
186 RT305X_ESW_ATTR_PORT_DOUBLETAG
,
187 RT305X_ESW_ATTR_PORT_UNTAG
,
188 RT305X_ESW_ATTR_PORT_LED
,
189 RT305X_ESW_ATTR_PORT_LAN
,
190 RT305X_ESW_ATTR_PORT_RECV_BAD
,
191 RT305X_ESW_ATTR_PORT_RECV_GOOD
,
192 RT5350_ESW_ATTR_PORT_TR_BAD
,
193 RT5350_ESW_ATTR_PORT_TR_GOOD
,
210 RT305X_ESW_VLAN_CONFIG_NONE
= 0,
211 RT305X_ESW_VLAN_CONFIG_LLLLW
,
212 RT305X_ESW_VLAN_CONFIG_WLLLL
,
220 /* Protects against concurrent register r/w operations. */
221 spinlock_t reg_rw_lock
;
223 unsigned char port_map
;
224 unsigned char port_disable
;
225 unsigned int reg_initval_fct2
;
226 unsigned int reg_initval_fpa2
;
227 unsigned int reg_led_polarity
;
229 struct switch_dev swdev
;
230 bool global_vlan_enable
;
231 bool alt_vlan_disable
;
232 int bc_storm_protect
;
234 struct esw_vlan vlans
[RT305X_ESW_NUM_VLANS
];
235 struct esw_port ports
[RT305X_ESW_NUM_PORTS
];
236 struct reset_control
*rst_esw
;
237 struct reset_control
*rst_ephy
;
241 static inline void esw_w32(struct rt305x_esw
*esw
, u32 val
, unsigned reg
)
243 __raw_writel(val
, esw
->base
+ reg
);
246 static inline u32
esw_r32(struct rt305x_esw
*esw
, unsigned reg
)
248 return __raw_readl(esw
->base
+ reg
);
251 static inline void esw_rmw_raw(struct rt305x_esw
*esw
, unsigned reg
,
252 unsigned long mask
, unsigned long val
)
256 t
= __raw_readl(esw
->base
+ reg
) & ~mask
;
257 __raw_writel(t
| val
, esw
->base
+ reg
);
260 static void esw_reset(struct rt305x_esw
*esw
)
265 reset_control_assert(esw
->rst_esw
);
266 usleep_range(60, 120);
267 reset_control_deassert(esw
->rst_esw
);
268 /* the esw takes long to reset otherwise the board hang */
272 static void esw_reset_ephy(struct rt305x_esw
*esw
)
277 reset_control_assert(esw
->rst_ephy
);
278 usleep_range(60, 120);
279 reset_control_deassert(esw
->rst_ephy
);
280 usleep_range(60, 120);
283 static void esw_rmw(struct rt305x_esw
*esw
, unsigned reg
,
284 unsigned long mask
, unsigned long val
)
288 spin_lock_irqsave(&esw
->reg_rw_lock
, flags
);
289 esw_rmw_raw(esw
, reg
, mask
, val
);
290 spin_unlock_irqrestore(&esw
->reg_rw_lock
, flags
);
293 static u32
rt305x_mii_write(struct rt305x_esw
*esw
, u32 phy_addr
,
294 u32 phy_register
, u32 write_data
)
296 unsigned long t_start
= jiffies
;
300 if (!(esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
301 RT305X_ESW_PCR1_WT_DONE
))
303 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
309 write_data
&= 0xffff;
310 esw_w32(esw
, (write_data
<< RT305X_ESW_PCR0_WT_NWAY_DATA_S
) |
311 (phy_register
<< RT305X_ESW_PCR0_CPU_PHY_REG_S
) |
312 (phy_addr
) | RT305X_ESW_PCR0_WT_PHY_CMD
,
313 RT305X_ESW_REG_PCR0
);
317 if (esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
318 RT305X_ESW_PCR1_WT_DONE
)
321 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
328 dev_err(esw
->dev
, "ramips_eth: MDIO timeout\n");
332 static unsigned esw_get_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
)
337 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
338 val
= esw_r32(esw
, RT305X_ESW_REG_VLANI(vlan
/ 2));
339 val
= (val
>> s
) & RT305X_ESW_VLANI_VID_M
;
344 static void esw_set_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
, unsigned vid
)
348 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
350 RT305X_ESW_REG_VLANI(vlan
/ 2),
351 RT305X_ESW_VLANI_VID_M
<< s
,
352 (vid
& RT305X_ESW_VLANI_VID_M
) << s
);
355 static unsigned esw_get_pvid(struct rt305x_esw
*esw
, unsigned port
)
359 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
360 val
= esw_r32(esw
, RT305X_ESW_REG_PVIDC(port
/ 2));
361 return (val
>> s
) & RT305X_ESW_PVIDC_PVID_M
;
364 static void esw_set_pvid(struct rt305x_esw
*esw
, unsigned port
, unsigned pvid
)
368 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
370 RT305X_ESW_REG_PVIDC(port
/ 2),
371 RT305X_ESW_PVIDC_PVID_M
<< s
,
372 (pvid
& RT305X_ESW_PVIDC_PVID_M
) << s
);
375 static unsigned esw_get_vmsc(struct rt305x_esw
*esw
, unsigned vlan
)
379 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
380 val
= esw_r32(esw
, RT305X_ESW_REG_VMSC(vlan
/ 4));
381 val
= (val
>> s
) & RT305X_ESW_VMSC_MSC_M
;
386 static void esw_set_vmsc(struct rt305x_esw
*esw
, unsigned vlan
, unsigned msc
)
390 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
392 RT305X_ESW_REG_VMSC(vlan
/ 4),
393 RT305X_ESW_VMSC_MSC_M
<< s
,
394 (msc
& RT305X_ESW_VMSC_MSC_M
) << s
);
397 static unsigned esw_get_port_disable(struct rt305x_esw
*esw
)
401 reg
= esw_r32(esw
, RT305X_ESW_REG_POC0
);
402 return (reg
>> RT305X_ESW_POC0_DIS_PORT_S
) &
403 RT305X_ESW_POC0_DIS_PORT_M
;
406 static void esw_set_port_disable(struct rt305x_esw
*esw
, unsigned disable_mask
)
409 unsigned enable_mask
;
413 old_mask
= esw_get_port_disable(esw
);
414 changed
= old_mask
^ disable_mask
;
415 enable_mask
= old_mask
& disable_mask
;
417 /* enable before writing to MII */
418 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
419 (RT305X_ESW_POC0_DIS_PORT_M
<<
420 RT305X_ESW_POC0_DIS_PORT_S
),
421 enable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
423 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++) {
424 if (!(changed
& (1 << i
)))
426 if (disable_mask
& (1 << i
)) {
428 rt305x_mii_write(esw
, i
, MII_BMCR
,
432 rt305x_mii_write(esw
, i
, MII_BMCR
,
440 /* disable after writing to MII */
441 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
442 (RT305X_ESW_POC0_DIS_PORT_M
<<
443 RT305X_ESW_POC0_DIS_PORT_S
),
444 disable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
447 static void esw_set_gsc(struct rt305x_esw
*esw
)
449 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
450 RT305X_ESW_GSC_BC_STROM_MASK
<< RT305X_ESW_GSC_BC_STROM_SHIFT
,
451 esw
->bc_storm_protect
<< RT305X_ESW_GSC_BC_STROM_SHIFT
);
452 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
453 RT305X_ESW_GSC_LED_FREQ_MASK
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
,
454 esw
->led_frequency
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
);
457 static int esw_apply_config(struct switch_dev
*dev
);
459 static void esw_hw_init(struct rt305x_esw
*esw
)
463 u8 port_map
= RT305X_ESW_PMAP_LLLLLL
;
465 /* vodoo from original driver */
466 esw_w32(esw
, 0xC8A07850, RT305X_ESW_REG_FCT0
);
467 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_SGC2
);
468 /* Port priority 1 for all ports, vlan enabled. */
469 esw_w32(esw
, 0x00005555 |
470 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_PFC1_EN_VLAN_S
),
471 RT305X_ESW_REG_PFC1
);
473 /* Enable all ports, Back Pressure and Flow Control */
474 esw_w32(esw
, ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_BP_S
) |
475 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_FC_S
)),
476 RT305X_ESW_REG_POC0
);
478 /* Enable Aging, and VLAN TAG removal */
479 esw_w32(esw
, ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC2_ENAGING_S
) |
480 (RT305X_ESW_PORTS_NOCPU
<< RT305X_ESW_POC2_UNTAG_EN_S
)),
481 RT305X_ESW_REG_POC2
);
483 if (esw
->reg_initval_fct2
)
484 esw_w32(esw
, esw
->reg_initval_fct2
, RT305X_ESW_REG_FCT2
);
486 esw_w32(esw
, 0x0002500c, RT305X_ESW_REG_FCT2
);
488 /* 300s aging timer, max packet len 1536, broadcast storm prevention
489 * disabled, disable collision abort, mac xor48 hash, 10 packet back
490 * pressure jam, GMII disable was_transmit, back pressure disabled,
491 * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
494 esw_w32(esw
, 0x0008a301, RT305X_ESW_REG_SGC
);
496 /* Setup SoC Port control register */
498 (RT305X_ESW_SOCPC_CRC_PADDING
|
499 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISUN2CPU_S
) |
500 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISMC2CPU_S
) |
501 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISBC2CPU_S
)),
502 RT305X_ESW_REG_SOCPC
);
504 /* ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
505 * turbo mii off, rgmi 3.3v off
507 * port6: enabled, gige, full-duplex, rx/tx-flow-control
509 if (esw
->reg_initval_fpa2
)
510 esw_w32(esw
, esw
->reg_initval_fpa2
, RT305X_ESW_REG_FPA2
);
512 esw_w32(esw
, 0x3f502b28, RT305X_ESW_REG_FPA2
);
513 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_FPA
);
515 /* Force Link/Activity on ports */
516 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P0LED
);
517 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P1LED
);
518 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P2LED
);
519 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P3LED
);
520 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P4LED
);
522 /* Copy disabled port configuration from device tree setup */
523 port_disable
= esw
->port_disable
;
525 /* Disable nonexistent ports by reading the switch config
526 * after having enabled all possible ports above
528 port_disable
|= esw_get_port_disable(esw
);
530 for (i
= 0; i
< 6; i
++)
531 esw
->ports
[i
].disable
= (port_disable
& (1 << i
)) != 0;
533 if (ralink_soc
== RT305X_SOC_RT3352
) {
536 rt305x_mii_write(esw
, 0, 31, 0x8000);
537 for (i
= 0; i
< 5; i
++) {
538 if (esw
->ports
[i
].disable
) {
539 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
541 rt305x_mii_write(esw
, i
, MII_BMCR
,
546 /* TX10 waveform coefficient LSB=0 disable PHY */
547 rt305x_mii_write(esw
, i
, 26, 0x1601);
548 /* TX100/TX10 AD/DA current bias */
549 rt305x_mii_write(esw
, i
, 29, 0x7016);
550 /* TX100 slew rate control */
551 rt305x_mii_write(esw
, i
, 30, 0x0038);
554 /* select global register */
555 rt305x_mii_write(esw
, 0, 31, 0x0);
556 /* enlarge agcsel threshold 3 and threshold 2 */
557 rt305x_mii_write(esw
, 0, 1, 0x4a40);
558 /* enlarge agcsel threshold 5 and threshold 4 */
559 rt305x_mii_write(esw
, 0, 2, 0x6254);
560 /* enlarge agcsel threshold */
561 rt305x_mii_write(esw
, 0, 3, 0xa17f);
562 rt305x_mii_write(esw
, 0, 12, 0x7eaa);
563 /* longer TP_IDL tail length */
564 rt305x_mii_write(esw
, 0, 14, 0x65);
565 /* increased squelch pulse count threshold. */
566 rt305x_mii_write(esw
, 0, 16, 0x0684);
567 /* set TX10 signal amplitude threshold to minimum */
568 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
569 /* set squelch amplitude to higher threshold */
570 rt305x_mii_write(esw
, 0, 18, 0x40ba);
571 /* tune TP_IDL tail and head waveform, enable power
572 * down slew rate control
574 rt305x_mii_write(esw
, 0, 22, 0x253f);
575 /* set PLL/Receive bias current are calibrated */
576 rt305x_mii_write(esw
, 0, 27, 0x2fda);
577 /* change PLL/Receive bias current to internal(RT3350) */
578 rt305x_mii_write(esw
, 0, 28, 0xc410);
579 /* change PLL bias current to internal(RT3052_MP3) */
580 rt305x_mii_write(esw
, 0, 29, 0x598b);
581 /* select local register */
582 rt305x_mii_write(esw
, 0, 31, 0x8000);
583 } else if (ralink_soc
== RT305X_SOC_RT5350
) {
586 /* set the led polarity */
587 esw_w32(esw
, esw
->reg_led_polarity
& 0x1F,
588 RT5350_EWS_REG_LED_POLARITY
);
590 /* local registers */
591 rt305x_mii_write(esw
, 0, 31, 0x8000);
592 for (i
= 0; i
< 5; i
++) {
593 if (esw
->ports
[i
].disable
) {
594 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
596 rt305x_mii_write(esw
, i
, MII_BMCR
,
601 /* TX10 waveform coefficient LSB=0 disable PHY */
602 rt305x_mii_write(esw
, i
, 26, 0x1601);
603 /* TX100/TX10 AD/DA current bias */
604 rt305x_mii_write(esw
, i
, 29, 0x7015);
605 /* TX100 slew rate control */
606 rt305x_mii_write(esw
, i
, 30, 0x0038);
609 /* global registers */
610 rt305x_mii_write(esw
, 0, 31, 0x0);
611 /* enlarge agcsel threshold 3 and threshold 2 */
612 rt305x_mii_write(esw
, 0, 1, 0x4a40);
613 /* enlarge agcsel threshold 5 and threshold 4 */
614 rt305x_mii_write(esw
, 0, 2, 0x6254);
615 /* enlarge agcsel threshold 6 */
616 rt305x_mii_write(esw
, 0, 3, 0xa17f);
617 rt305x_mii_write(esw
, 0, 12, 0x7eaa);
618 /* longer TP_IDL tail length */
619 rt305x_mii_write(esw
, 0, 14, 0x65);
620 /* increased squelch pulse count threshold. */
621 rt305x_mii_write(esw
, 0, 16, 0x0684);
622 /* set TX10 signal amplitude threshold to minimum */
623 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
624 /* set squelch amplitude to higher threshold */
625 rt305x_mii_write(esw
, 0, 18, 0x40ba);
626 /* tune TP_IDL tail and head waveform, enable power
627 * down slew rate control
629 rt305x_mii_write(esw
, 0, 22, 0x253f);
630 /* set PLL/Receive bias current are calibrated */
631 rt305x_mii_write(esw
, 0, 27, 0x2fda);
632 /* change PLL/Receive bias current to internal(RT3350) */
633 rt305x_mii_write(esw
, 0, 28, 0xc410);
634 /* change PLL bias current to internal(RT3052_MP3) */
635 rt305x_mii_write(esw
, 0, 29, 0x598b);
636 /* select local register */
637 rt305x_mii_write(esw
, 0, 31, 0x8000);
638 } else if (ralink_soc
== MT762X_SOC_MT7628AN
|| ralink_soc
== MT762X_SOC_MT7688
) {
643 /* set the led polarity */
644 esw_w32(esw
, esw
->reg_led_polarity
& 0x1F,
645 RT5350_EWS_REG_LED_POLARITY
);
647 rt305x_mii_write(esw
, 0, 31, 0x2000); /* change G2 page */
648 rt305x_mii_write(esw
, 0, 26, 0x0020);
650 for (i
= 0; i
< 5; i
++) {
651 rt305x_mii_write(esw
, i
, 31, 0x8000);
652 rt305x_mii_write(esw
, i
, 0, 0x3100);
653 rt305x_mii_write(esw
, i
, 30, 0xa000);
654 rt305x_mii_write(esw
, i
, 31, 0xa000);
655 rt305x_mii_write(esw
, i
, 16, 0x0606);
656 rt305x_mii_write(esw
, i
, 23, 0x0f0e);
657 rt305x_mii_write(esw
, i
, 24, 0x1610);
658 rt305x_mii_write(esw
, i
, 30, 0x1f15);
659 rt305x_mii_write(esw
, i
, 28, 0x6111);
660 rt305x_mii_write(esw
, i
, 31, 0x2000);
661 rt305x_mii_write(esw
, i
, 26, 0x0000);
664 /* 100Base AOI setting */
665 rt305x_mii_write(esw
, 0, 31, 0x5000);
666 rt305x_mii_write(esw
, 0, 19, 0x004a);
667 rt305x_mii_write(esw
, 0, 20, 0x015a);
668 rt305x_mii_write(esw
, 0, 21, 0x00ee);
669 rt305x_mii_write(esw
, 0, 22, 0x0033);
670 rt305x_mii_write(esw
, 0, 23, 0x020a);
671 rt305x_mii_write(esw
, 0, 24, 0x0000);
672 rt305x_mii_write(esw
, 0, 25, 0x024a);
673 rt305x_mii_write(esw
, 0, 26, 0x035a);
674 rt305x_mii_write(esw
, 0, 27, 0x02ee);
675 rt305x_mii_write(esw
, 0, 28, 0x0233);
676 rt305x_mii_write(esw
, 0, 29, 0x000a);
677 rt305x_mii_write(esw
, 0, 30, 0x0000);
679 rt305x_mii_write(esw
, 0, 31, 0x8000);
680 for (i
= 0; i
< 5; i
++) {
681 if (esw
->ports
[i
].disable
) {
682 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
684 rt305x_mii_write(esw
, i
, MII_BMCR
,
689 /* TX10 waveform coefficient */
690 rt305x_mii_write(esw
, i
, 26, 0x1601);
691 /* TX100/TX10 AD/DA current bias */
692 rt305x_mii_write(esw
, i
, 29, 0x7058);
693 /* TX100 slew rate control */
694 rt305x_mii_write(esw
, i
, 30, 0x0018);
698 /* select global register */
699 rt305x_mii_write(esw
, 0, 31, 0x0);
700 /* tune TP_IDL tail and head waveform */
701 rt305x_mii_write(esw
, 0, 22, 0x052f);
702 /* set TX10 signal amplitude threshold to minimum */
703 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
704 /* set squelch amplitude to higher threshold */
705 rt305x_mii_write(esw
, 0, 18, 0x40ba);
706 /* longer TP_IDL tail length */
707 rt305x_mii_write(esw
, 0, 14, 0x65);
708 /* select local register */
709 rt305x_mii_write(esw
, 0, 31, 0x8000);
713 port_map
= esw
->port_map
;
715 port_map
= RT305X_ESW_PMAP_LLLLLL
;
717 /* Unused HW feature, but still nice to be consistent here...
718 * This is also exported to userspace ('lan' attribute) so it's
719 * conveniently usable to decide which ports go into the wan vlan by
722 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
723 RT305X_ESW_SGC2_LAN_PMAP_M
<< RT305X_ESW_SGC2_LAN_PMAP_S
,
724 port_map
<< RT305X_ESW_SGC2_LAN_PMAP_S
);
726 /* make the switch leds blink */
727 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++)
728 esw
->ports
[i
].led
= 0x05;
730 /* Apply the empty config. */
731 esw_apply_config(&esw
->swdev
);
733 /* Only unmask the port change interrupt */
734 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
737 static irqreturn_t
esw_interrupt(int irq
, void *_esw
)
739 struct rt305x_esw
*esw
= (struct rt305x_esw
*)_esw
;
742 status
= esw_r32(esw
, RT305X_ESW_REG_ISR
);
743 if (status
& RT305X_ESW_PORT_ST_CHG
) {
744 u32 link
= esw_r32(esw
, RT305X_ESW_REG_POA
);
746 link
>>= RT305X_ESW_POA_LINK_SHIFT
;
747 link
&= RT305X_ESW_POA_LINK_MASK
;
748 dev_info(esw
->dev
, "link changed 0x%02X\n", link
);
750 esw_w32(esw
, status
, RT305X_ESW_REG_ISR
);
755 static int esw_apply_config(struct switch_dev
*dev
)
757 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
764 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
766 if (esw
->global_vlan_enable
) {
767 vid
= esw
->vlans
[i
].vid
;
768 vmsc
= esw
->vlans
[i
].ports
;
770 vid
= RT305X_ESW_VLAN_NONE
;
771 vmsc
= RT305X_ESW_PORTS_NONE
;
773 esw_set_vlan_id(esw
, i
, vid
);
774 esw_set_vmsc(esw
, i
, vmsc
);
777 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
779 disable
|= esw
->ports
[i
].disable
<< i
;
780 if (esw
->global_vlan_enable
) {
781 doubletag
|= esw
->ports
[i
].doubletag
<< i
;
783 untag
|= esw
->ports
[i
].untag
<< i
;
784 pvid
= esw
->ports
[i
].pvid
;
786 int x
= esw
->alt_vlan_disable
? 0 : 1;
792 esw_set_pvid(esw
, i
, pvid
);
793 if (i
< RT305X_ESW_NUM_LEDS
)
794 esw_w32(esw
, esw
->ports
[i
].led
,
795 RT305X_ESW_REG_P0LED
+ 4*i
);
799 esw_set_port_disable(esw
, disable
);
800 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
801 (RT305X_ESW_SGC2_DOUBLE_TAG_M
<<
802 RT305X_ESW_SGC2_DOUBLE_TAG_S
),
803 doubletag
<< RT305X_ESW_SGC2_DOUBLE_TAG_S
);
804 esw_rmw(esw
, RT305X_ESW_REG_PFC1
,
805 RT305X_ESW_PFC1_EN_VLAN_M
<< RT305X_ESW_PFC1_EN_VLAN_S
,
806 en_vlan
<< RT305X_ESW_PFC1_EN_VLAN_S
);
807 esw_rmw(esw
, RT305X_ESW_REG_POC2
,
808 RT305X_ESW_POC2_UNTAG_EN_M
<< RT305X_ESW_POC2_UNTAG_EN_S
,
809 untag
<< RT305X_ESW_POC2_UNTAG_EN_S
);
811 if (!esw
->global_vlan_enable
) {
813 * Still need to put all ports into vlan 0 or they'll be
815 * NOTE: vlan 0 is special, no vlan tag is prepended
817 esw_set_vlan_id(esw
, 0, 0);
818 esw_set_vmsc(esw
, 0, RT305X_ESW_PORTS_ALL
);
824 static int esw_reset_switch(struct switch_dev
*dev
)
826 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
828 esw
->global_vlan_enable
= 0;
829 memset(esw
->ports
, 0, sizeof(esw
->ports
));
830 memset(esw
->vlans
, 0, sizeof(esw
->vlans
));
836 static int esw_get_vlan_enable(struct switch_dev
*dev
,
837 const struct switch_attr
*attr
,
838 struct switch_val
*val
)
840 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
842 val
->value
.i
= esw
->global_vlan_enable
;
847 static int esw_set_vlan_enable(struct switch_dev
*dev
,
848 const struct switch_attr
*attr
,
849 struct switch_val
*val
)
851 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
853 esw
->global_vlan_enable
= val
->value
.i
!= 0;
858 static int esw_get_alt_vlan_disable(struct switch_dev
*dev
,
859 const struct switch_attr
*attr
,
860 struct switch_val
*val
)
862 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
864 val
->value
.i
= esw
->alt_vlan_disable
;
869 static int esw_set_alt_vlan_disable(struct switch_dev
*dev
,
870 const struct switch_attr
*attr
,
871 struct switch_val
*val
)
873 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
875 esw
->alt_vlan_disable
= val
->value
.i
!= 0;
881 rt305x_esw_set_bc_status(struct switch_dev
*dev
,
882 const struct switch_attr
*attr
,
883 struct switch_val
*val
)
885 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
887 esw
->bc_storm_protect
= val
->value
.i
& RT305X_ESW_GSC_BC_STROM_MASK
;
893 rt305x_esw_get_bc_status(struct switch_dev
*dev
,
894 const struct switch_attr
*attr
,
895 struct switch_val
*val
)
897 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
899 val
->value
.i
= esw
->bc_storm_protect
;
905 rt305x_esw_set_led_freq(struct switch_dev
*dev
,
906 const struct switch_attr
*attr
,
907 struct switch_val
*val
)
909 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
911 esw
->led_frequency
= val
->value
.i
& RT305X_ESW_GSC_LED_FREQ_MASK
;
917 rt305x_esw_get_led_freq(struct switch_dev
*dev
,
918 const struct switch_attr
*attr
,
919 struct switch_val
*val
)
921 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
923 val
->value
.i
= esw
->led_frequency
;
928 static int esw_get_port_link(struct switch_dev
*dev
,
930 struct switch_port_link
*link
)
932 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
935 if (port
< 0 || port
>= RT305X_ESW_NUM_PORTS
)
938 poa
= esw_r32(esw
, RT305X_ESW_REG_POA
) >> port
;
940 link
->link
= (poa
>> RT305X_ESW_LINK_S
) & 1;
941 link
->duplex
= (poa
>> RT305X_ESW_DUPLEX_S
) & 1;
942 if (port
< RT305X_ESW_NUM_LEDS
) {
943 speed
= (poa
>> RT305X_ESW_SPD_S
) & 1;
945 if (port
== RT305X_ESW_NUM_PORTS
- 1)
947 speed
= (poa
>> RT305X_ESW_SPD_S
) & 3;
951 link
->speed
= SWITCH_PORT_SPEED_10
;
954 link
->speed
= SWITCH_PORT_SPEED_100
;
957 case 3: /* forced gige speed can be 2 or 3 */
958 link
->speed
= SWITCH_PORT_SPEED_1000
;
961 link
->speed
= SWITCH_PORT_SPEED_UNKNOWN
;
968 static int esw_get_port_bool(struct switch_dev
*dev
,
969 const struct switch_attr
*attr
,
970 struct switch_val
*val
)
972 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
973 int idx
= val
->port_vlan
;
976 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
)
980 case RT305X_ESW_ATTR_PORT_DISABLE
:
981 reg
= RT305X_ESW_REG_POC0
;
982 shift
= RT305X_ESW_POC0_DIS_PORT_S
;
984 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
985 reg
= RT305X_ESW_REG_SGC2
;
986 shift
= RT305X_ESW_SGC2_DOUBLE_TAG_S
;
988 case RT305X_ESW_ATTR_PORT_UNTAG
:
989 reg
= RT305X_ESW_REG_POC2
;
990 shift
= RT305X_ESW_POC2_UNTAG_EN_S
;
992 case RT305X_ESW_ATTR_PORT_LAN
:
993 reg
= RT305X_ESW_REG_SGC2
;
994 shift
= RT305X_ESW_SGC2_LAN_PMAP_S
;
995 if (idx
>= RT305X_ESW_NUM_LANWAN
)
1002 x
= esw_r32(esw
, reg
);
1003 val
->value
.i
= (x
>> (idx
+ shift
)) & 1;
1008 static int esw_set_port_bool(struct switch_dev
*dev
,
1009 const struct switch_attr
*attr
,
1010 struct switch_val
*val
)
1012 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1013 int idx
= val
->port_vlan
;
1015 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1016 val
->value
.i
< 0 || val
->value
.i
> 1)
1020 case RT305X_ESW_ATTR_PORT_DISABLE
:
1021 esw
->ports
[idx
].disable
= val
->value
.i
;
1023 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
1024 esw
->ports
[idx
].doubletag
= val
->value
.i
;
1026 case RT305X_ESW_ATTR_PORT_UNTAG
:
1027 esw
->ports
[idx
].untag
= val
->value
.i
;
1036 static int esw_get_port_recv_badgood(struct switch_dev
*dev
,
1037 const struct switch_attr
*attr
,
1038 struct switch_val
*val
)
1040 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1041 int idx
= val
->port_vlan
;
1042 int shift
= attr
->id
== RT305X_ESW_ATTR_PORT_RECV_GOOD
? 0 : 16;
1045 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1047 reg
= esw_r32(esw
, RT305X_ESW_REG_PXPC(idx
));
1048 val
->value
.i
= (reg
>> shift
) & 0xffff;
1054 esw_get_port_tr_badgood(struct switch_dev
*dev
,
1055 const struct switch_attr
*attr
,
1056 struct switch_val
*val
)
1058 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1060 int idx
= val
->port_vlan
;
1061 int shift
= attr
->id
== RT5350_ESW_ATTR_PORT_TR_GOOD
? 0 : 16;
1064 if ((ralink_soc
!= RT305X_SOC_RT5350
) && (ralink_soc
!= MT762X_SOC_MT7628AN
) && (ralink_soc
!= MT762X_SOC_MT7688
))
1067 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1070 reg
= esw_r32(esw
, RT5350_ESW_REG_PXTPC(idx
));
1071 val
->value
.i
= (reg
>> shift
) & 0xffff;
1076 static int esw_get_port_led(struct switch_dev
*dev
,
1077 const struct switch_attr
*attr
,
1078 struct switch_val
*val
)
1080 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1081 int idx
= val
->port_vlan
;
1083 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1084 idx
>= RT305X_ESW_NUM_LEDS
)
1087 val
->value
.i
= esw_r32(esw
, RT305X_ESW_REG_P0LED
+ 4*idx
);
1092 static int esw_set_port_led(struct switch_dev
*dev
,
1093 const struct switch_attr
*attr
,
1094 struct switch_val
*val
)
1096 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1097 int idx
= val
->port_vlan
;
1099 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LEDS
)
1102 esw
->ports
[idx
].led
= val
->value
.i
;
1107 static int esw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1109 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1111 if (port
>= RT305X_ESW_NUM_PORTS
)
1114 *val
= esw_get_pvid(esw
, port
);
1119 static int esw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1121 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1123 if (port
>= RT305X_ESW_NUM_PORTS
)
1126 esw
->ports
[port
].pvid
= val
;
1131 static int esw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1133 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1140 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
)
1144 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1145 if (esw_get_vlan_id(esw
, i
) == val
->port_vlan
&&
1146 esw_get_vmsc(esw
, i
) != RT305X_ESW_PORTS_NONE
) {
1155 vmsc
= esw_get_vmsc(esw
, vlan_idx
);
1156 poc2
= esw_r32(esw
, RT305X_ESW_REG_POC2
);
1158 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
1159 struct switch_port
*p
;
1160 int port_mask
= 1 << i
;
1162 if (!(vmsc
& port_mask
))
1165 p
= &val
->value
.ports
[val
->len
++];
1167 if (poc2
& (port_mask
<< RT305X_ESW_POC2_UNTAG_EN_S
))
1170 p
->flags
= 1 << SWITCH_PORT_FLAG_TAGGED
;
1176 static int esw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1178 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1183 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
||
1184 val
->len
> RT305X_ESW_NUM_PORTS
)
1187 /* one of the already defined vlans? */
1188 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1189 if (esw
->vlans
[i
].vid
== val
->port_vlan
&&
1190 esw
->vlans
[i
].ports
!= RT305X_ESW_PORTS_NONE
) {
1196 /* select a free slot */
1197 for (i
= 0; vlan_idx
== -1 && i
< RT305X_ESW_NUM_VLANS
; i
++) {
1198 if (esw
->vlans
[i
].ports
== RT305X_ESW_PORTS_NONE
)
1202 /* bail if all slots are in use */
1206 ports
= RT305X_ESW_PORTS_NONE
;
1207 for (i
= 0; i
< val
->len
; i
++) {
1208 struct switch_port
*p
= &val
->value
.ports
[i
];
1209 int port_mask
= 1 << p
->id
;
1210 bool untagged
= !(p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
));
1212 if (p
->id
>= RT305X_ESW_NUM_PORTS
)
1216 esw
->ports
[p
->id
].untag
= untagged
;
1218 esw
->vlans
[vlan_idx
].ports
= ports
;
1219 if (ports
== RT305X_ESW_PORTS_NONE
)
1220 esw
->vlans
[vlan_idx
].vid
= RT305X_ESW_VLAN_NONE
;
1222 esw
->vlans
[vlan_idx
].vid
= val
->port_vlan
;
1227 static const struct switch_attr esw_global
[] = {
1229 .type
= SWITCH_TYPE_INT
,
1230 .name
= "enable_vlan",
1231 .description
= "VLAN mode (1:enabled)",
1233 .id
= RT305X_ESW_ATTR_ENABLE_VLAN
,
1234 .get
= esw_get_vlan_enable
,
1235 .set
= esw_set_vlan_enable
,
1238 .type
= SWITCH_TYPE_INT
,
1239 .name
= "alternate_vlan_disable",
1240 .description
= "Use en_vlan instead of doubletag to disable"
1243 .id
= RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
1244 .get
= esw_get_alt_vlan_disable
,
1245 .set
= esw_set_alt_vlan_disable
,
1248 .type
= SWITCH_TYPE_INT
,
1249 .name
= "bc_storm_protect",
1250 .description
= "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1252 .id
= RT305X_ESW_ATTR_BC_STATUS
,
1253 .get
= rt305x_esw_get_bc_status
,
1254 .set
= rt305x_esw_set_bc_status
,
1257 .type
= SWITCH_TYPE_INT
,
1258 .name
= "led_frequency",
1259 .description
= "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1261 .id
= RT305X_ESW_ATTR_LED_FREQ
,
1262 .get
= rt305x_esw_get_led_freq
,
1263 .set
= rt305x_esw_set_led_freq
,
1267 static const struct switch_attr esw_port
[] = {
1269 .type
= SWITCH_TYPE_INT
,
1271 .description
= "Port state (1:disabled)",
1273 .id
= RT305X_ESW_ATTR_PORT_DISABLE
,
1274 .get
= esw_get_port_bool
,
1275 .set
= esw_set_port_bool
,
1278 .type
= SWITCH_TYPE_INT
,
1279 .name
= "doubletag",
1280 .description
= "Double tagging for incoming vlan packets "
1283 .id
= RT305X_ESW_ATTR_PORT_DOUBLETAG
,
1284 .get
= esw_get_port_bool
,
1285 .set
= esw_set_port_bool
,
1288 .type
= SWITCH_TYPE_INT
,
1290 .description
= "Untag (1:strip outgoing vlan tag)",
1292 .id
= RT305X_ESW_ATTR_PORT_UNTAG
,
1293 .get
= esw_get_port_bool
,
1294 .set
= esw_set_port_bool
,
1297 .type
= SWITCH_TYPE_INT
,
1299 .description
= "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1300 " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1301 " 8:100mact, 10:blink, 11:off, 12:on)",
1303 .id
= RT305X_ESW_ATTR_PORT_LED
,
1304 .get
= esw_get_port_led
,
1305 .set
= esw_set_port_led
,
1308 .type
= SWITCH_TYPE_INT
,
1310 .description
= "HW port group (0:wan, 1:lan)",
1312 .id
= RT305X_ESW_ATTR_PORT_LAN
,
1313 .get
= esw_get_port_bool
,
1316 .type
= SWITCH_TYPE_INT
,
1318 .description
= "Receive bad packet counter",
1319 .id
= RT305X_ESW_ATTR_PORT_RECV_BAD
,
1320 .get
= esw_get_port_recv_badgood
,
1323 .type
= SWITCH_TYPE_INT
,
1324 .name
= "recv_good",
1325 .description
= "Receive good packet counter",
1326 .id
= RT305X_ESW_ATTR_PORT_RECV_GOOD
,
1327 .get
= esw_get_port_recv_badgood
,
1330 .type
= SWITCH_TYPE_INT
,
1333 .description
= "Transmit bad packet counter. rt5350 only",
1334 .id
= RT5350_ESW_ATTR_PORT_TR_BAD
,
1335 .get
= esw_get_port_tr_badgood
,
1338 .type
= SWITCH_TYPE_INT
,
1341 .description
= "Transmit good packet counter. rt5350 only",
1342 .id
= RT5350_ESW_ATTR_PORT_TR_GOOD
,
1343 .get
= esw_get_port_tr_badgood
,
1347 static const struct switch_attr esw_vlan
[] = {
1350 static const struct switch_dev_ops esw_ops
= {
1353 .n_attr
= ARRAY_SIZE(esw_global
),
1357 .n_attr
= ARRAY_SIZE(esw_port
),
1361 .n_attr
= ARRAY_SIZE(esw_vlan
),
1363 .get_vlan_ports
= esw_get_vlan_ports
,
1364 .set_vlan_ports
= esw_set_vlan_ports
,
1365 .get_port_pvid
= esw_get_port_pvid
,
1366 .set_port_pvid
= esw_set_port_pvid
,
1367 .get_port_link
= esw_get_port_link
,
1368 .apply_config
= esw_apply_config
,
1369 .reset_switch
= esw_reset_switch
,
1372 static int esw_probe(struct platform_device
*pdev
)
1374 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1375 struct device_node
*np
= pdev
->dev
.of_node
;
1376 const __be32
*port_map
, *port_disable
, *reg_init
;
1377 struct switch_dev
*swdev
;
1378 struct rt305x_esw
*esw
;
1381 esw
= devm_kzalloc(&pdev
->dev
, sizeof(*esw
), GFP_KERNEL
);
1385 esw
->dev
= &pdev
->dev
;
1386 esw
->irq
= irq_of_parse_and_map(np
, 0);
1387 esw
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1388 if (IS_ERR(esw
->base
))
1389 return PTR_ERR(esw
->base
);
1391 port_map
= of_get_property(np
, "mediatek,portmap", NULL
);
1393 esw
->port_map
= be32_to_cpu(*port_map
);
1395 port_disable
= of_get_property(np
, "mediatek,portdisable", NULL
);
1397 esw
->port_disable
= be32_to_cpu(*port_disable
);
1399 reg_init
= of_get_property(np
, "ralink,fct2", NULL
);
1401 esw
->reg_initval_fct2
= be32_to_cpu(*reg_init
);
1403 reg_init
= of_get_property(np
, "ralink,fpa2", NULL
);
1405 esw
->reg_initval_fpa2
= be32_to_cpu(*reg_init
);
1407 reg_init
= of_get_property(np
, "mediatek,led_polarity", NULL
);
1409 esw
->reg_led_polarity
= be32_to_cpu(*reg_init
);
1411 esw
->rst_esw
= devm_reset_control_get(&pdev
->dev
, "esw");
1412 if (IS_ERR(esw
->rst_esw
))
1413 esw
->rst_esw
= NULL
;
1414 esw
->rst_ephy
= devm_reset_control_get(&pdev
->dev
, "ephy");
1415 if (IS_ERR(esw
->rst_ephy
))
1416 esw
->rst_ephy
= NULL
;
1418 swdev
= &esw
->swdev
;
1419 swdev
->of_node
= pdev
->dev
.of_node
;
1420 swdev
->name
= "rt305x-esw";
1421 swdev
->alias
= "rt305x";
1422 swdev
->cpu_port
= RT305X_ESW_PORT6
;
1423 swdev
->ports
= RT305X_ESW_NUM_PORTS
;
1424 swdev
->vlans
= RT305X_ESW_NUM_VIDS
;
1425 swdev
->ops
= &esw_ops
;
1427 ret
= register_switch(swdev
, NULL
);
1429 dev_err(&pdev
->dev
, "register_switch failed\n");
1433 platform_set_drvdata(pdev
, esw
);
1435 spin_lock_init(&esw
->reg_rw_lock
);
1439 reg_init
= of_get_property(np
, "ralink,rgmii", NULL
);
1440 if (reg_init
&& be32_to_cpu(*reg_init
) == 1) {
1442 * External switch connected to RGMII interface.
1443 * Unregister the switch device after initialization.
1445 dev_err(&pdev
->dev
, "RGMII mode, not exporting switch device.\n");
1446 unregister_switch(&esw
->swdev
);
1447 platform_set_drvdata(pdev
, NULL
);
1451 ret
= devm_request_irq(&pdev
->dev
, esw
->irq
, esw_interrupt
, 0, "esw",
1455 esw_w32(esw
, RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_ISR
);
1456 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
1462 static int esw_remove(struct platform_device
*pdev
)
1464 struct rt305x_esw
*esw
= platform_get_drvdata(pdev
);
1467 esw_w32(esw
, ~0, RT305X_ESW_REG_IMR
);
1468 platform_set_drvdata(pdev
, NULL
);
1474 static const struct of_device_id ralink_esw_match
[] = {
1475 { .compatible
= "ralink,rt3050-esw" },
1478 MODULE_DEVICE_TABLE(of
, ralink_esw_match
);
1480 static struct platform_driver esw_driver
= {
1482 .remove
= esw_remove
,
1484 .name
= "rt3050-esw",
1485 .owner
= THIS_MODULE
,
1486 .of_match_table
= ralink_esw_match
,
1490 module_platform_driver(esw_driver
);
1492 MODULE_LICENSE("GPL");
1493 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
1494 MODULE_DESCRIPTION("Switch driver for RT305X SoC");
1495 MODULE_VERSION(MTK_FE_DRV_VERSION
);