2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/platform_device.h>
27 #include <linux/of_device.h>
28 #include <linux/clk.h>
29 #include <linux/of_net.h>
30 #include <linux/of_mdio.h>
32 #include <asm/mach-ralink/ralink_regs.h>
34 #include "ralink_soc_eth.h"
36 #include <linux/ioport.h>
37 #include <linux/switch.h>
38 #include <linux/mii.h>
40 #include <ralink_regs.h>
42 #include <asm/mach-ralink/rt305x_esw_platform.h>
45 * HW limitations for this switch:
46 * - No large frame support (PKT_MAX_LEN at most 1536)
47 * - Can't have untagged vlan and tagged vlan on one port at the same time,
48 * though this might be possible using the undocumented PPE.
51 #define RT305X_ESW_REG_ISR 0x00
52 #define RT305X_ESW_REG_IMR 0x04
53 #define RT305X_ESW_REG_FCT0 0x08
54 #define RT305X_ESW_REG_PFC1 0x14
55 #define RT305X_ESW_REG_ATS 0x24
56 #define RT305X_ESW_REG_ATS0 0x28
57 #define RT305X_ESW_REG_ATS1 0x2c
58 #define RT305X_ESW_REG_ATS2 0x30
59 #define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
60 #define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
61 #define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
62 #define RT305X_ESW_REG_POA 0x80
63 #define RT305X_ESW_REG_FPA 0x84
64 #define RT305X_ESW_REG_SOCPC 0x8c
65 #define RT305X_ESW_REG_POC0 0x90
66 #define RT305X_ESW_REG_POC1 0x94
67 #define RT305X_ESW_REG_POC2 0x98
68 #define RT305X_ESW_REG_SGC 0x9c
69 #define RT305X_ESW_REG_STRT 0xa0
70 #define RT305X_ESW_REG_PCR0 0xc0
71 #define RT305X_ESW_REG_PCR1 0xc4
72 #define RT305X_ESW_REG_FPA2 0xc8
73 #define RT305X_ESW_REG_FCT2 0xcc
74 #define RT305X_ESW_REG_SGC2 0xe4
75 #define RT305X_ESW_REG_P0LED 0xa4
76 #define RT305X_ESW_REG_P1LED 0xa8
77 #define RT305X_ESW_REG_P2LED 0xac
78 #define RT305X_ESW_REG_P3LED 0xb0
79 #define RT305X_ESW_REG_P4LED 0xb4
80 #define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
81 #define RT305X_ESW_REG_P1PC 0xec
82 #define RT305X_ESW_REG_P2PC 0xf0
83 #define RT305X_ESW_REG_P3PC 0xf4
84 #define RT305X_ESW_REG_P4PC 0xf8
85 #define RT305X_ESW_REG_P5PC 0xfc
87 #define RT305X_ESW_LED_LINK 0
88 #define RT305X_ESW_LED_100M 1
89 #define RT305X_ESW_LED_DUPLEX 2
90 #define RT305X_ESW_LED_ACTIVITY 3
91 #define RT305X_ESW_LED_COLLISION 4
92 #define RT305X_ESW_LED_LINKACT 5
93 #define RT305X_ESW_LED_DUPLCOLL 6
94 #define RT305X_ESW_LED_10MACT 7
95 #define RT305X_ESW_LED_100MACT 8
96 /* Additional led states not in datasheet: */
97 #define RT305X_ESW_LED_BLINK 10
98 #define RT305X_ESW_LED_ON 12
100 #define RT305X_ESW_LINK_S 25
101 #define RT305X_ESW_DUPLEX_S 9
102 #define RT305X_ESW_SPD_S 0
104 #define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
105 #define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
106 #define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
108 #define RT305X_ESW_PCR1_WT_DONE BIT(0)
110 #define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
111 #define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
113 #define RT305X_ESW_PVIDC_PVID_M 0xfff
114 #define RT305X_ESW_PVIDC_PVID_S 12
116 #define RT305X_ESW_VLANI_VID_M 0xfff
117 #define RT305X_ESW_VLANI_VID_S 12
119 #define RT305X_ESW_VMSC_MSC_M 0xff
120 #define RT305X_ESW_VMSC_MSC_S 8
122 #define RT305X_ESW_SOCPC_DISUN2CPU_S 0
123 #define RT305X_ESW_SOCPC_DISMC2CPU_S 8
124 #define RT305X_ESW_SOCPC_DISBC2CPU_S 16
125 #define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
127 #define RT305X_ESW_POC0_EN_BP_S 0
128 #define RT305X_ESW_POC0_EN_FC_S 8
129 #define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
130 #define RT305X_ESW_POC0_DIS_PORT_M 0x7f
131 #define RT305X_ESW_POC0_DIS_PORT_S 23
133 #define RT305X_ESW_POC2_UNTAG_EN_M 0xff
134 #define RT305X_ESW_POC2_UNTAG_EN_S 0
135 #define RT305X_ESW_POC2_ENAGING_S 8
136 #define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
138 #define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
139 #define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
140 #define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
141 #define RT305X_ESW_SGC2_LAN_PMAP_S 24
143 #define RT305X_ESW_PFC1_EN_VLAN_M 0xff
144 #define RT305X_ESW_PFC1_EN_VLAN_S 16
145 #define RT305X_ESW_PFC1_EN_TOS_S 24
147 #define RT305X_ESW_VLAN_NONE 0xfff
149 #define RT305X_ESW_GSC_BC_STROM_MASK 0x3
150 #define RT305X_ESW_GSC_BC_STROM_SHIFT 4
152 #define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
153 #define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
155 #define RT305X_ESW_POA_LINK_MASK 0x1f
156 #define RT305X_ESW_POA_LINK_SHIFT 25
158 #define RT305X_ESW_PORT_ST_CHG BIT(26)
159 #define RT305X_ESW_PORT0 0
160 #define RT305X_ESW_PORT1 1
161 #define RT305X_ESW_PORT2 2
162 #define RT305X_ESW_PORT3 3
163 #define RT305X_ESW_PORT4 4
164 #define RT305X_ESW_PORT5 5
165 #define RT305X_ESW_PORT6 6
167 #define RT305X_ESW_PORTS_NONE 0
169 #define RT305X_ESW_PMAP_LLLLLL 0x3f
170 #define RT305X_ESW_PMAP_LLLLWL 0x2f
171 #define RT305X_ESW_PMAP_WLLLLL 0x3e
173 #define RT305X_ESW_PORTS_INTERNAL \
174 (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
175 BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
176 BIT(RT305X_ESW_PORT4))
178 #define RT305X_ESW_PORTS_NOCPU \
179 (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
181 #define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
183 #define RT305X_ESW_PORTS_ALL \
184 (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
186 #define RT305X_ESW_NUM_VLANS 16
187 #define RT305X_ESW_NUM_VIDS 4096
188 #define RT305X_ESW_NUM_PORTS 7
189 #define RT305X_ESW_NUM_LANWAN 6
190 #define RT305X_ESW_NUM_LEDS 5
192 #define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
193 #define RT5350_EWS_REG_LED_POLARITY 0x168
194 #define RT5350_RESET_EPHY BIT(24)
195 #define SYSC_REG_RESET_CTRL 0x34
198 /* Global attributes. */
199 RT305X_ESW_ATTR_ENABLE_VLAN
,
200 RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
201 RT305X_ESW_ATTR_BC_STATUS
,
202 RT305X_ESW_ATTR_LED_FREQ
,
203 /* Port attributes. */
204 RT305X_ESW_ATTR_PORT_DISABLE
,
205 RT305X_ESW_ATTR_PORT_DOUBLETAG
,
206 RT305X_ESW_ATTR_PORT_UNTAG
,
207 RT305X_ESW_ATTR_PORT_LED
,
208 RT305X_ESW_ATTR_PORT_LAN
,
209 RT305X_ESW_ATTR_PORT_RECV_BAD
,
210 RT305X_ESW_ATTR_PORT_RECV_GOOD
,
211 RT5350_ESW_ATTR_PORT_TR_BAD
,
212 RT5350_ESW_ATTR_PORT_TR_GOOD
,
232 const struct rt305x_esw_platform_data
*pdata
;
233 /* Protects against concurrent register rmw operations. */
234 spinlock_t reg_rw_lock
;
236 unsigned char port_map
;
237 unsigned int reg_initval_fct2
;
238 unsigned int reg_initval_fpa2
;
239 unsigned int reg_led_polarity
;
242 struct switch_dev swdev
;
243 bool global_vlan_enable
;
244 bool alt_vlan_disable
;
245 int bc_storm_protect
;
247 struct esw_vlan vlans
[RT305X_ESW_NUM_VLANS
];
248 struct esw_port ports
[RT305X_ESW_NUM_PORTS
];
252 static inline void esw_w32(struct rt305x_esw
*esw
, u32 val
, unsigned reg
)
254 __raw_writel(val
, esw
->base
+ reg
);
257 static inline u32
esw_r32(struct rt305x_esw
*esw
, unsigned reg
)
259 return __raw_readl(esw
->base
+ reg
);
262 static inline void esw_rmw_raw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
267 t
= __raw_readl(esw
->base
+ reg
) & ~mask
;
268 __raw_writel(t
| val
, esw
->base
+ reg
);
271 static void esw_rmw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
276 spin_lock_irqsave(&esw
->reg_rw_lock
, flags
);
277 esw_rmw_raw(esw
, reg
, mask
, val
);
278 spin_unlock_irqrestore(&esw
->reg_rw_lock
, flags
);
281 static u32
rt305x_mii_write(struct rt305x_esw
*esw
, u32 phy_addr
, u32 phy_register
,
284 unsigned long t_start
= jiffies
;
288 if (!(esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
289 RT305X_ESW_PCR1_WT_DONE
))
291 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
297 write_data
&= 0xffff;
299 (write_data
<< RT305X_ESW_PCR0_WT_NWAY_DATA_S
) |
300 (phy_register
<< RT305X_ESW_PCR0_CPU_PHY_REG_S
) |
301 (phy_addr
) | RT305X_ESW_PCR0_WT_PHY_CMD
,
302 RT305X_ESW_REG_PCR0
);
306 if (esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
307 RT305X_ESW_PCR1_WT_DONE
)
310 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
317 printk(KERN_ERR
"ramips_eth: MDIO timeout\n");
321 static unsigned esw_get_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
)
326 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
327 val
= esw_r32(esw
, RT305X_ESW_REG_VLANI(vlan
/ 2));
328 val
= (val
>> s
) & RT305X_ESW_VLANI_VID_M
;
333 static void esw_set_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
, unsigned vid
)
337 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
339 RT305X_ESW_REG_VLANI(vlan
/ 2),
340 RT305X_ESW_VLANI_VID_M
<< s
,
341 (vid
& RT305X_ESW_VLANI_VID_M
) << s
);
344 static unsigned esw_get_pvid(struct rt305x_esw
*esw
, unsigned port
)
348 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
349 val
= esw_r32(esw
, RT305X_ESW_REG_PVIDC(port
/ 2));
350 return (val
>> s
) & RT305X_ESW_PVIDC_PVID_M
;
353 static void esw_set_pvid(struct rt305x_esw
*esw
, unsigned port
, unsigned pvid
)
357 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
359 RT305X_ESW_REG_PVIDC(port
/ 2),
360 RT305X_ESW_PVIDC_PVID_M
<< s
,
361 (pvid
& RT305X_ESW_PVIDC_PVID_M
) << s
);
364 static unsigned esw_get_vmsc(struct rt305x_esw
*esw
, unsigned vlan
)
368 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
369 val
= esw_r32(esw
, RT305X_ESW_REG_VMSC(vlan
/ 4));
370 val
= (val
>> s
) & RT305X_ESW_VMSC_MSC_M
;
375 static void esw_set_vmsc(struct rt305x_esw
*esw
, unsigned vlan
, unsigned msc
)
379 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
381 RT305X_ESW_REG_VMSC(vlan
/ 4),
382 RT305X_ESW_VMSC_MSC_M
<< s
,
383 (msc
& RT305X_ESW_VMSC_MSC_M
) << s
);
386 static unsigned esw_get_port_disable(struct rt305x_esw
*esw
)
389 reg
= esw_r32(esw
, RT305X_ESW_REG_POC0
);
390 return (reg
>> RT305X_ESW_POC0_DIS_PORT_S
) &
391 RT305X_ESW_POC0_DIS_PORT_M
;
394 static void esw_set_port_disable(struct rt305x_esw
*esw
, unsigned disable_mask
)
397 unsigned enable_mask
;
401 old_mask
= esw_get_port_disable(esw
);
402 changed
= old_mask
^ disable_mask
;
403 enable_mask
= old_mask
& disable_mask
;
405 /* enable before writing to MII */
406 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
407 (RT305X_ESW_POC0_DIS_PORT_M
<<
408 RT305X_ESW_POC0_DIS_PORT_S
),
409 enable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
411 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++) {
412 if (!(changed
& (1 << i
)))
414 if (disable_mask
& (1 << i
)) {
416 rt305x_mii_write(esw
, i
, MII_BMCR
,
420 rt305x_mii_write(esw
, i
, MII_BMCR
,
428 /* disable after writing to MII */
429 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
430 (RT305X_ESW_POC0_DIS_PORT_M
<<
431 RT305X_ESW_POC0_DIS_PORT_S
),
432 disable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
435 static void esw_set_gsc(struct rt305x_esw
*esw
)
437 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
438 RT305X_ESW_GSC_BC_STROM_MASK
<< RT305X_ESW_GSC_BC_STROM_SHIFT
,
439 esw
->bc_storm_protect
<< RT305X_ESW_GSC_BC_STROM_SHIFT
);
440 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
441 RT305X_ESW_GSC_LED_FREQ_MASK
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
,
442 esw
->led_frequency
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
);
445 static int esw_apply_config(struct switch_dev
*dev
);
447 static void esw_hw_init(struct rt305x_esw
*esw
)
451 u8 port_map
= RT305X_ESW_PMAP_LLLLLL
;
453 /* vodoo from original driver */
454 esw_w32(esw
, 0xC8A07850, RT305X_ESW_REG_FCT0
);
455 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_SGC2
);
456 /* Port priority 1 for all ports, vlan enabled. */
457 esw_w32(esw
, 0x00005555 |
458 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_PFC1_EN_VLAN_S
),
459 RT305X_ESW_REG_PFC1
);
461 /* Enable Back Pressure, and Flow Control */
463 ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_BP_S
) |
464 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_FC_S
)),
465 RT305X_ESW_REG_POC0
);
467 /* Enable Aging, and VLAN TAG removal */
469 ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC2_ENAGING_S
) |
470 (RT305X_ESW_PORTS_NOCPU
<< RT305X_ESW_POC2_UNTAG_EN_S
)),
471 RT305X_ESW_REG_POC2
);
473 if (esw
->reg_initval_fct2
)
474 esw_w32(esw
, esw
->reg_initval_fct2
, RT305X_ESW_REG_FCT2
);
476 esw_w32(esw
, esw
->pdata
->reg_initval_fct2
, RT305X_ESW_REG_FCT2
);
479 * 300s aging timer, max packet len 1536, broadcast storm prevention
480 * disabled, disable collision abort, mac xor48 hash, 10 packet back
481 * pressure jam, GMII disable was_transmit, back pressure disabled,
482 * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
485 esw_w32(esw
, 0x0008a301, RT305X_ESW_REG_SGC
);
487 /* Setup SoC Port control register */
489 (RT305X_ESW_SOCPC_CRC_PADDING
|
490 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISUN2CPU_S
) |
491 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISMC2CPU_S
) |
492 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISBC2CPU_S
)),
493 RT305X_ESW_REG_SOCPC
);
495 if (esw
->reg_initval_fpa2
)
496 esw_w32(esw
, esw
->reg_initval_fpa2
, RT305X_ESW_REG_FPA2
);
498 esw_w32(esw
, esw
->pdata
->reg_initval_fpa2
, RT305X_ESW_REG_FPA2
);
499 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_FPA
);
501 /* Force Link/Activity on ports */
502 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P0LED
);
503 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P1LED
);
504 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P2LED
);
505 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P3LED
);
506 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P4LED
);
508 /* Copy disabled port configuration from bootloader setup */
509 port_disable
= esw_get_port_disable(esw
);
510 for (i
= 0; i
< 6; i
++)
511 esw
->ports
[i
].disable
= (port_disable
& (1 << i
)) != 0;
513 if (ralink_soc
== RT305X_SOC_RT3352
) {
515 u32 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
516 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
517 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
519 rt305x_mii_write(esw
, 0, 31, 0x8000);
520 for (i
= 0; i
< 5; i
++) {
521 if (esw
->ports
[i
].disable
) {
522 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
524 rt305x_mii_write(esw
, i
, MII_BMCR
,
529 /* TX10 waveform coefficient LSB=0 disable PHY */
530 rt305x_mii_write(esw
, i
, 26, 0x1601);
531 /* TX100/TX10 AD/DA current bias */
532 rt305x_mii_write(esw
, i
, 29, 0x7016);
533 /* TX100 slew rate control */
534 rt305x_mii_write(esw
, i
, 30, 0x0038);
537 /* select global register */
538 rt305x_mii_write(esw
, 0, 31, 0x0);
539 /* enlarge agcsel threshold 3 and threshold 2 */
540 rt305x_mii_write(esw
, 0, 1, 0x4a40);
541 /* enlarge agcsel threshold 5 and threshold 4 */
542 rt305x_mii_write(esw
, 0, 2, 0x6254);
543 /* enlarge agcsel threshold */
544 rt305x_mii_write(esw
, 0, 3, 0xa17f);
545 rt305x_mii_write(esw
, 0,12, 0x7eaa);
546 /* longer TP_IDL tail length */
547 rt305x_mii_write(esw
, 0, 14, 0x65);
548 /* increased squelch pulse count threshold. */
549 rt305x_mii_write(esw
, 0, 16, 0x0684);
550 /* set TX10 signal amplitude threshold to minimum */
551 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
552 /* set squelch amplitude to higher threshold */
553 rt305x_mii_write(esw
, 0, 18, 0x40ba);
554 /* tune TP_IDL tail and head waveform, enable power down slew rate control */
555 rt305x_mii_write(esw
, 0, 22, 0x253f);
556 /* set PLL/Receive bias current are calibrated */
557 rt305x_mii_write(esw
, 0, 27, 0x2fda);
558 /* change PLL/Receive bias current to internal(RT3350) */
559 rt305x_mii_write(esw
, 0, 28, 0xc410);
560 /* change PLL bias current to internal(RT3052_MP3) */
561 rt305x_mii_write(esw
, 0, 29, 0x598b);
562 /* select local register */
563 rt305x_mii_write(esw
, 0, 31, 0x8000);
564 } else if (ralink_soc
== RT305X_SOC_RT5350
) {
566 u32 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
567 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
568 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
570 /* set the led polarity */
571 esw_w32(esw
, esw
->reg_led_polarity
& 0x1F, RT5350_EWS_REG_LED_POLARITY
);
573 /* local registers */
574 rt305x_mii_write(esw
, 0, 31, 0x8000);
575 for (i
= 0; i
< 5; i
++) {
576 if (esw
->ports
[i
].disable
) {
577 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
579 rt305x_mii_write(esw
, i
, MII_BMCR
,
584 /* TX10 waveform coefficient LSB=0 disable PHY */
585 rt305x_mii_write(esw
, i
, 26, 0x1601);
586 /* TX100/TX10 AD/DA current bias */
587 rt305x_mii_write(esw
, i
, 29, 0x7015);
588 /* TX100 slew rate control */
589 rt305x_mii_write(esw
, i
, 30, 0x0038);
592 /* global registers */
593 rt305x_mii_write(esw
, 0, 31, 0x0);
594 /* enlarge agcsel threshold 3 and threshold 2 */
595 rt305x_mii_write(esw
, 0, 1, 0x4a40);
596 /* enlarge agcsel threshold 5 and threshold 4 */
597 rt305x_mii_write(esw
, 0, 2, 0x6254);
598 /* enlarge agcsel threshold 6 */
599 rt305x_mii_write(esw
, 0, 3, 0xa17f);
600 rt305x_mii_write(esw
, 0, 12, 0x7eaa);
601 /* longer TP_IDL tail length */
602 rt305x_mii_write(esw
, 0, 14, 0x65);
603 /* increased squelch pulse count threshold. */
604 rt305x_mii_write(esw
, 0, 16, 0x0684);
605 /* set TX10 signal amplitude threshold to minimum */
606 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
607 /* set squelch amplitude to higher threshold */
608 rt305x_mii_write(esw
, 0, 18, 0x40ba);
609 /* tune TP_IDL tail and head waveform, enable power down slew rate control */
610 rt305x_mii_write(esw
, 0, 22, 0x253f);
611 /* set PLL/Receive bias current are calibrated */
612 rt305x_mii_write(esw
, 0, 27, 0x2fda);
613 /* change PLL/Receive bias current to internal(RT3350) */
614 rt305x_mii_write(esw
, 0, 28, 0xc410);
615 /* change PLL bias current to internal(RT3052_MP3) */
616 rt305x_mii_write(esw
, 0, 29, 0x598b);
617 /* select local register */
618 rt305x_mii_write(esw
, 0, 31, 0x8000);
619 } else if (ralink_soc
== MT762X_SOC_MT7628AN
) {
625 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
626 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
627 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
629 rt305x_mii_write(esw
, 0, 31, 0x2000); /* change G2 page */
630 rt305x_mii_write(esw
, 0, 26, 0x0020);
632 for (i
= 0; i
< 5; i
++) {
633 rt305x_mii_write(esw
, i
, 31, 0x8000); //change L0 page
634 rt305x_mii_write(esw
, i
, 0, 0x3100);
635 // mii_mgr_read(i, 26, &phy_val);// EEE setting
636 // phy_val |= (1 << 5);
637 // rt305x_mii_write(esw, i, 26, phy_val);
638 rt305x_mii_write(esw
, i
, 30, 0xa000);
639 rt305x_mii_write(esw
, i
, 31, 0xa000); // change L2 page
640 rt305x_mii_write(esw
, i
, 16, 0x0606);
641 rt305x_mii_write(esw
, i
, 23, 0x0f0e);
642 rt305x_mii_write(esw
, i
, 24, 0x1610);
643 rt305x_mii_write(esw
, i
, 30, 0x1f15);
644 rt305x_mii_write(esw
, i
, 28, 0x6111);
645 // mii_mgr_read(i, 4, &phy_val);
646 // phy_val |= (1 << 10);
647 // rt305x_mii_write(esw, i, 4, phy_val);
648 rt305x_mii_write(esw
, i
, 31, 0x2000); // change G2 page
649 rt305x_mii_write(esw
, i
, 26, 0x0000);
652 //100Base AOI setting
653 rt305x_mii_write(esw
, 0, 31, 0x5000); //change G5 page
654 rt305x_mii_write(esw
, 0, 19, 0x004a);
655 rt305x_mii_write(esw
, 0, 20, 0x015a);
656 rt305x_mii_write(esw
, 0, 21, 0x00ee);
657 rt305x_mii_write(esw
, 0, 22, 0x0033);
658 rt305x_mii_write(esw
, 0, 23, 0x020a);
659 rt305x_mii_write(esw
, 0, 24, 0x0000);
660 rt305x_mii_write(esw
, 0, 25, 0x024a);
661 rt305x_mii_write(esw
, 0, 26, 0x035a);
662 rt305x_mii_write(esw
, 0, 27, 0x02ee);
663 rt305x_mii_write(esw
, 0, 28, 0x0233);
664 rt305x_mii_write(esw
, 0, 29, 0x000a);
665 rt305x_mii_write(esw
, 0, 30, 0x0000);
667 rt305x_mii_write(esw
, 0, 31, 0x8000);
668 for (i
= 0; i
< 5; i
++) {
669 if (esw
->ports
[i
].disable
) {
670 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
672 rt305x_mii_write(esw
, i
, MII_BMCR
,
677 /* TX10 waveform coefficient */
678 rt305x_mii_write(esw
, i
, 26, 0x1601);
679 /* TX100/TX10 AD/DA current bias */
680 rt305x_mii_write(esw
, i
, 29, 0x7058);
681 /* TX100 slew rate control */
682 rt305x_mii_write(esw
, i
, 30, 0x0018);
686 /* select global register */
687 rt305x_mii_write(esw
, 0, 31, 0x0);
688 /* tune TP_IDL tail and head waveform */
689 rt305x_mii_write(esw
, 0, 22, 0x052f);
690 /* set TX10 signal amplitude threshold to minimum */
691 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
692 /* set squelch amplitude to higher threshold */
693 rt305x_mii_write(esw
, 0, 18, 0x40ba);
694 /* longer TP_IDL tail length */
695 rt305x_mii_write(esw
, 0, 14, 0x65);
696 /* select local register */
697 rt305x_mii_write(esw
, 0, 31, 0x8000);
701 port_map
= esw
->port_map
;
703 port_map
= RT305X_ESW_PMAP_LLLLLL
;
706 * Unused HW feature, but still nice to be consistent here...
707 * This is also exported to userspace ('lan' attribute) so it's
708 * conveniently usable to decide which ports go into the wan vlan by
711 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
712 RT305X_ESW_SGC2_LAN_PMAP_M
<< RT305X_ESW_SGC2_LAN_PMAP_S
,
713 port_map
<< RT305X_ESW_SGC2_LAN_PMAP_S
);
715 /* make the switch leds blink */
716 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++)
717 esw
->ports
[i
].led
= 0x05;
719 /* Apply the empty config. */
720 esw_apply_config(&esw
->swdev
);
722 /* Only unmask the port change interrupt */
723 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
726 static irqreturn_t
esw_interrupt(int irq
, void *_esw
)
728 struct rt305x_esw
*esw
= (struct rt305x_esw
*) _esw
;
731 status
= esw_r32(esw
, RT305X_ESW_REG_ISR
);
732 if (status
& RT305X_ESW_PORT_ST_CHG
) {
733 u32 link
= esw_r32(esw
, RT305X_ESW_REG_POA
);
734 link
>>= RT305X_ESW_POA_LINK_SHIFT
;
735 link
&= RT305X_ESW_POA_LINK_MASK
;
736 dev_info(esw
->dev
, "link changed 0x%02X\n", link
);
738 esw_w32(esw
, status
, RT305X_ESW_REG_ISR
);
743 static int esw_apply_config(struct switch_dev
*dev
)
745 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
752 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
754 if (esw
->global_vlan_enable
) {
755 vid
= esw
->vlans
[i
].vid
;
756 vmsc
= esw
->vlans
[i
].ports
;
758 vid
= RT305X_ESW_VLAN_NONE
;
759 vmsc
= RT305X_ESW_PORTS_NONE
;
761 esw_set_vlan_id(esw
, i
, vid
);
762 esw_set_vmsc(esw
, i
, vmsc
);
765 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
767 disable
|= esw
->ports
[i
].disable
<< i
;
768 if (esw
->global_vlan_enable
) {
769 doubletag
|= esw
->ports
[i
].doubletag
<< i
;
771 untag
|= esw
->ports
[i
].untag
<< i
;
772 pvid
= esw
->ports
[i
].pvid
;
774 int x
= esw
->alt_vlan_disable
? 0 : 1;
780 esw_set_pvid(esw
, i
, pvid
);
781 if (i
< RT305X_ESW_NUM_LEDS
)
782 esw_w32(esw
, esw
->ports
[i
].led
,
783 RT305X_ESW_REG_P0LED
+ 4*i
);
787 esw_set_port_disable(esw
, disable
);
788 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
789 (RT305X_ESW_SGC2_DOUBLE_TAG_M
<<
790 RT305X_ESW_SGC2_DOUBLE_TAG_S
),
791 doubletag
<< RT305X_ESW_SGC2_DOUBLE_TAG_S
);
792 esw_rmw(esw
, RT305X_ESW_REG_PFC1
,
793 RT305X_ESW_PFC1_EN_VLAN_M
<< RT305X_ESW_PFC1_EN_VLAN_S
,
794 en_vlan
<< RT305X_ESW_PFC1_EN_VLAN_S
);
795 esw_rmw(esw
, RT305X_ESW_REG_POC2
,
796 RT305X_ESW_POC2_UNTAG_EN_M
<< RT305X_ESW_POC2_UNTAG_EN_S
,
797 untag
<< RT305X_ESW_POC2_UNTAG_EN_S
);
799 if (!esw
->global_vlan_enable
) {
801 * Still need to put all ports into vlan 0 or they'll be
803 * NOTE: vlan 0 is special, no vlan tag is prepended
805 esw_set_vlan_id(esw
, 0, 0);
806 esw_set_vmsc(esw
, 0, RT305X_ESW_PORTS_ALL
);
812 static int esw_reset_switch(struct switch_dev
*dev
)
814 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
816 esw
->global_vlan_enable
= 0;
817 memset(esw
->ports
, 0, sizeof(esw
->ports
));
818 memset(esw
->vlans
, 0, sizeof(esw
->vlans
));
824 static int esw_get_vlan_enable(struct switch_dev
*dev
,
825 const struct switch_attr
*attr
,
826 struct switch_val
*val
)
828 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
830 val
->value
.i
= esw
->global_vlan_enable
;
835 static int esw_set_vlan_enable(struct switch_dev
*dev
,
836 const struct switch_attr
*attr
,
837 struct switch_val
*val
)
839 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
841 esw
->global_vlan_enable
= val
->value
.i
!= 0;
846 static int esw_get_alt_vlan_disable(struct switch_dev
*dev
,
847 const struct switch_attr
*attr
,
848 struct switch_val
*val
)
850 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
852 val
->value
.i
= esw
->alt_vlan_disable
;
857 static int esw_set_alt_vlan_disable(struct switch_dev
*dev
,
858 const struct switch_attr
*attr
,
859 struct switch_val
*val
)
861 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
863 esw
->alt_vlan_disable
= val
->value
.i
!= 0;
869 rt305x_esw_set_bc_status(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
->bc_storm_protect
= val
->value
.i
& RT305X_ESW_GSC_BC_STROM_MASK
;
881 rt305x_esw_get_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 val
->value
.i
= esw
->bc_storm_protect
;
893 rt305x_esw_set_led_freq(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 esw
->led_frequency
= val
->value
.i
& RT305X_ESW_GSC_LED_FREQ_MASK
;
905 rt305x_esw_get_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 val
->value
.i
= esw
->led_frequency
;
916 static int esw_get_port_link(struct switch_dev
*dev
,
918 struct switch_port_link
*link
)
920 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
923 if (port
< 0 || port
>= RT305X_ESW_NUM_PORTS
)
926 poa
= esw_r32(esw
, RT305X_ESW_REG_POA
) >> port
;
928 link
->link
= (poa
>> RT305X_ESW_LINK_S
) & 1;
929 link
->duplex
= (poa
>> RT305X_ESW_DUPLEX_S
) & 1;
930 if (port
< RT305X_ESW_NUM_LEDS
) {
931 speed
= (poa
>> RT305X_ESW_SPD_S
) & 1;
933 if (port
== RT305X_ESW_NUM_PORTS
- 1)
935 speed
= (poa
>> RT305X_ESW_SPD_S
) & 3;
939 link
->speed
= SWITCH_PORT_SPEED_10
;
942 link
->speed
= SWITCH_PORT_SPEED_100
;
945 case 3: /* forced gige speed can be 2 or 3 */
946 link
->speed
= SWITCH_PORT_SPEED_1000
;
949 link
->speed
= SWITCH_PORT_SPEED_UNKNOWN
;
956 static int esw_get_port_bool(struct switch_dev
*dev
,
957 const struct switch_attr
*attr
,
958 struct switch_val
*val
)
960 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
961 int idx
= val
->port_vlan
;
964 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
)
968 case RT305X_ESW_ATTR_PORT_DISABLE
:
969 reg
= RT305X_ESW_REG_POC0
;
970 shift
= RT305X_ESW_POC0_DIS_PORT_S
;
972 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
973 reg
= RT305X_ESW_REG_SGC2
;
974 shift
= RT305X_ESW_SGC2_DOUBLE_TAG_S
;
976 case RT305X_ESW_ATTR_PORT_UNTAG
:
977 reg
= RT305X_ESW_REG_POC2
;
978 shift
= RT305X_ESW_POC2_UNTAG_EN_S
;
980 case RT305X_ESW_ATTR_PORT_LAN
:
981 reg
= RT305X_ESW_REG_SGC2
;
982 shift
= RT305X_ESW_SGC2_LAN_PMAP_S
;
983 if (idx
>= RT305X_ESW_NUM_LANWAN
)
990 x
= esw_r32(esw
, reg
);
991 val
->value
.i
= (x
>> (idx
+ shift
)) & 1;
996 static int esw_set_port_bool(struct switch_dev
*dev
,
997 const struct switch_attr
*attr
,
998 struct switch_val
*val
)
1000 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1001 int idx
= val
->port_vlan
;
1003 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1004 val
->value
.i
< 0 || val
->value
.i
> 1)
1008 case RT305X_ESW_ATTR_PORT_DISABLE
:
1009 esw
->ports
[idx
].disable
= val
->value
.i
;
1011 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
1012 esw
->ports
[idx
].doubletag
= val
->value
.i
;
1014 case RT305X_ESW_ATTR_PORT_UNTAG
:
1015 esw
->ports
[idx
].untag
= val
->value
.i
;
1024 static int esw_get_port_recv_badgood(struct switch_dev
*dev
,
1025 const struct switch_attr
*attr
,
1026 struct switch_val
*val
)
1028 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1029 int idx
= val
->port_vlan
;
1030 int shift
= attr
->id
== RT305X_ESW_ATTR_PORT_RECV_GOOD
? 0 : 16;
1033 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1035 reg
= esw_r32(esw
, RT305X_ESW_REG_PXPC(idx
));
1036 val
->value
.i
= (reg
>> shift
) & 0xffff;
1042 esw_get_port_tr_badgood(struct switch_dev
*dev
,
1043 const struct switch_attr
*attr
,
1044 struct switch_val
*val
)
1046 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1048 int idx
= val
->port_vlan
;
1049 int shift
= attr
->id
== RT5350_ESW_ATTR_PORT_TR_GOOD
? 0 : 16;
1052 if ((ralink_soc
!= RT305X_SOC_RT5350
) && (ralink_soc
!= MT762X_SOC_MT7628AN
))
1055 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1058 reg
= esw_r32(esw
, RT5350_ESW_REG_PXTPC(idx
));
1059 val
->value
.i
= (reg
>> shift
) & 0xffff;
1064 static int esw_get_port_led(struct switch_dev
*dev
,
1065 const struct switch_attr
*attr
,
1066 struct switch_val
*val
)
1068 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1069 int idx
= val
->port_vlan
;
1071 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1072 idx
>= RT305X_ESW_NUM_LEDS
)
1075 val
->value
.i
= esw_r32(esw
, RT305X_ESW_REG_P0LED
+ 4*idx
);
1080 static int esw_set_port_led(struct switch_dev
*dev
,
1081 const struct switch_attr
*attr
,
1082 struct switch_val
*val
)
1084 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1085 int idx
= val
->port_vlan
;
1087 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LEDS
)
1090 esw
->ports
[idx
].led
= val
->value
.i
;
1095 static int esw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1097 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1099 if (port
>= RT305X_ESW_NUM_PORTS
)
1102 *val
= esw_get_pvid(esw
, port
);
1107 static int esw_set_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 esw
->ports
[port
].pvid
= val
;
1119 static int esw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1121 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1128 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
)
1132 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1133 if (esw_get_vlan_id(esw
, i
) == val
->port_vlan
&&
1134 esw_get_vmsc(esw
, i
) != RT305X_ESW_PORTS_NONE
) {
1143 vmsc
= esw_get_vmsc(esw
, vlan_idx
);
1144 poc2
= esw_r32(esw
, RT305X_ESW_REG_POC2
);
1146 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
1147 struct switch_port
*p
;
1148 int port_mask
= 1 << i
;
1150 if (!(vmsc
& port_mask
))
1153 p
= &val
->value
.ports
[val
->len
++];
1155 if (poc2
& (port_mask
<< RT305X_ESW_POC2_UNTAG_EN_S
))
1158 p
->flags
= 1 << SWITCH_PORT_FLAG_TAGGED
;
1164 static int esw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1166 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1171 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
||
1172 val
->len
> RT305X_ESW_NUM_PORTS
)
1175 /* one of the already defined vlans? */
1176 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1177 if (esw
->vlans
[i
].vid
== val
->port_vlan
&&
1178 esw
->vlans
[i
].ports
!= RT305X_ESW_PORTS_NONE
) {
1184 /* select a free slot */
1185 for (i
= 0; vlan_idx
== -1 && i
< RT305X_ESW_NUM_VLANS
; i
++) {
1186 if (esw
->vlans
[i
].ports
== RT305X_ESW_PORTS_NONE
)
1190 /* bail if all slots are in use */
1194 ports
= RT305X_ESW_PORTS_NONE
;
1195 for (i
= 0; i
< val
->len
; i
++) {
1196 struct switch_port
*p
= &val
->value
.ports
[i
];
1197 int port_mask
= 1 << p
->id
;
1198 bool untagged
= !(p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
));
1200 if (p
->id
>= RT305X_ESW_NUM_PORTS
)
1204 esw
->ports
[p
->id
].untag
= untagged
;
1206 esw
->vlans
[vlan_idx
].ports
= ports
;
1207 if (ports
== RT305X_ESW_PORTS_NONE
)
1208 esw
->vlans
[vlan_idx
].vid
= RT305X_ESW_VLAN_NONE
;
1210 esw
->vlans
[vlan_idx
].vid
= val
->port_vlan
;
1215 static const struct switch_attr esw_global
[] = {
1217 .type
= SWITCH_TYPE_INT
,
1218 .name
= "enable_vlan",
1219 .description
= "VLAN mode (1:enabled)",
1221 .id
= RT305X_ESW_ATTR_ENABLE_VLAN
,
1222 .get
= esw_get_vlan_enable
,
1223 .set
= esw_set_vlan_enable
,
1226 .type
= SWITCH_TYPE_INT
,
1227 .name
= "alternate_vlan_disable",
1228 .description
= "Use en_vlan instead of doubletag to disable"
1231 .id
= RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
1232 .get
= esw_get_alt_vlan_disable
,
1233 .set
= esw_set_alt_vlan_disable
,
1236 .type
= SWITCH_TYPE_INT
,
1237 .name
= "bc_storm_protect",
1238 .description
= "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1240 .id
= RT305X_ESW_ATTR_BC_STATUS
,
1241 .get
= rt305x_esw_get_bc_status
,
1242 .set
= rt305x_esw_set_bc_status
,
1245 .type
= SWITCH_TYPE_INT
,
1246 .name
= "led_frequency",
1247 .description
= "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1249 .id
= RT305X_ESW_ATTR_LED_FREQ
,
1250 .get
= rt305x_esw_get_led_freq
,
1251 .set
= rt305x_esw_set_led_freq
,
1255 static const struct switch_attr esw_port
[] = {
1257 .type
= SWITCH_TYPE_INT
,
1259 .description
= "Port state (1:disabled)",
1261 .id
= RT305X_ESW_ATTR_PORT_DISABLE
,
1262 .get
= esw_get_port_bool
,
1263 .set
= esw_set_port_bool
,
1266 .type
= SWITCH_TYPE_INT
,
1267 .name
= "doubletag",
1268 .description
= "Double tagging for incoming vlan packets "
1271 .id
= RT305X_ESW_ATTR_PORT_DOUBLETAG
,
1272 .get
= esw_get_port_bool
,
1273 .set
= esw_set_port_bool
,
1276 .type
= SWITCH_TYPE_INT
,
1278 .description
= "Untag (1:strip outgoing vlan tag)",
1280 .id
= RT305X_ESW_ATTR_PORT_UNTAG
,
1281 .get
= esw_get_port_bool
,
1282 .set
= esw_set_port_bool
,
1285 .type
= SWITCH_TYPE_INT
,
1287 .description
= "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1288 " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1289 " 8:100mact, 10:blink, 11:off, 12:on)",
1291 .id
= RT305X_ESW_ATTR_PORT_LED
,
1292 .get
= esw_get_port_led
,
1293 .set
= esw_set_port_led
,
1296 .type
= SWITCH_TYPE_INT
,
1298 .description
= "HW port group (0:wan, 1:lan)",
1300 .id
= RT305X_ESW_ATTR_PORT_LAN
,
1301 .get
= esw_get_port_bool
,
1304 .type
= SWITCH_TYPE_INT
,
1306 .description
= "Receive bad packet counter",
1307 .id
= RT305X_ESW_ATTR_PORT_RECV_BAD
,
1308 .get
= esw_get_port_recv_badgood
,
1311 .type
= SWITCH_TYPE_INT
,
1312 .name
= "recv_good",
1313 .description
= "Receive good packet counter",
1314 .id
= RT305X_ESW_ATTR_PORT_RECV_GOOD
,
1315 .get
= esw_get_port_recv_badgood
,
1318 .type
= SWITCH_TYPE_INT
,
1321 .description
= "Transmit bad packet counter. rt5350 only",
1322 .id
= RT5350_ESW_ATTR_PORT_TR_BAD
,
1323 .get
= esw_get_port_tr_badgood
,
1326 .type
= SWITCH_TYPE_INT
,
1329 .description
= "Transmit good packet counter. rt5350 only",
1330 .id
= RT5350_ESW_ATTR_PORT_TR_GOOD
,
1331 .get
= esw_get_port_tr_badgood
,
1335 static const struct switch_attr esw_vlan
[] = {
1338 static const struct switch_dev_ops esw_ops
= {
1341 .n_attr
= ARRAY_SIZE(esw_global
),
1345 .n_attr
= ARRAY_SIZE(esw_port
),
1349 .n_attr
= ARRAY_SIZE(esw_vlan
),
1351 .get_vlan_ports
= esw_get_vlan_ports
,
1352 .set_vlan_ports
= esw_set_vlan_ports
,
1353 .get_port_pvid
= esw_get_port_pvid
,
1354 .set_port_pvid
= esw_set_port_pvid
,
1355 .get_port_link
= esw_get_port_link
,
1356 .apply_config
= esw_apply_config
,
1357 .reset_switch
= esw_reset_switch
,
1360 static struct rt305x_esw_platform_data rt3050_esw_data
= {
1361 /* All ports are LAN ports. */
1362 .vlan_config
= RT305X_ESW_VLAN_CONFIG_NONE
,
1363 .reg_initval_fct2
= 0x00d6500c,
1365 * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1366 * turbo mii off, rgmi 3.3v off
1368 * port6: enabled, gige, full-duplex, rx/tx-flow-control
1370 .reg_initval_fpa2
= 0x3f502b28,
1373 static const struct of_device_id ralink_esw_match
[] = {
1374 { .compatible
= "ralink,rt3050-esw", .data
= &rt3050_esw_data
},
1377 MODULE_DEVICE_TABLE(of
, ralink_esw_match
);
1379 static int esw_probe(struct platform_device
*pdev
)
1381 struct device_node
*np
= pdev
->dev
.of_node
;
1382 const struct rt305x_esw_platform_data
*pdata
;
1383 const __be32
*port_map
, *reg_init
;
1384 struct rt305x_esw
*esw
;
1385 struct switch_dev
*swdev
;
1386 struct resource
*res
, *irq
;
1389 pdata
= pdev
->dev
.platform_data
;
1391 const struct of_device_id
*match
;
1392 match
= of_match_device(ralink_esw_match
, &pdev
->dev
);
1394 pdata
= (struct rt305x_esw_platform_data
*) match
->data
;
1399 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1401 dev_err(&pdev
->dev
, "no memory resource found\n");
1405 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1407 dev_err(&pdev
->dev
, "no irq resource found\n");
1411 esw
= kzalloc(sizeof(struct rt305x_esw
), GFP_KERNEL
);
1413 dev_err(&pdev
->dev
, "no memory for private data\n");
1417 esw
->dev
= &pdev
->dev
;
1418 esw
->irq
= irq
->start
;
1419 esw
->base
= ioremap(res
->start
, resource_size(res
));
1421 dev_err(&pdev
->dev
, "ioremap failed\n");
1426 port_map
= of_get_property(np
, "ralink,portmap", NULL
);
1428 esw
->port_map
= be32_to_cpu(*port_map
);
1430 reg_init
= of_get_property(np
, "ralink,fct2", NULL
);
1432 esw
->reg_initval_fct2
= be32_to_cpu(*reg_init
);
1434 reg_init
= of_get_property(np
, "ralink,fpa2", NULL
);
1436 esw
->reg_initval_fpa2
= be32_to_cpu(*reg_init
);
1438 reg_init
= of_get_property(np
, "ralink,led_polarity", NULL
);
1440 esw
->reg_led_polarity
= be32_to_cpu(*reg_init
);
1442 swdev
= &esw
->swdev
;
1443 swdev
->of_node
= pdev
->dev
.of_node
;
1444 swdev
->name
= "rt305x-esw";
1445 swdev
->alias
= "rt305x";
1446 swdev
->cpu_port
= RT305X_ESW_PORT6
;
1447 swdev
->ports
= RT305X_ESW_NUM_PORTS
;
1448 swdev
->vlans
= RT305X_ESW_NUM_VIDS
;
1449 swdev
->ops
= &esw_ops
;
1451 err
= register_switch(swdev
, NULL
);
1453 dev_err(&pdev
->dev
, "register_switch failed\n");
1457 platform_set_drvdata(pdev
, esw
);
1460 spin_lock_init(&esw
->reg_rw_lock
);
1464 esw_w32(esw
, RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_ISR
);
1465 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
1466 request_irq(esw
->irq
, esw_interrupt
, 0, "esw", esw
);
1477 static int esw_remove(struct platform_device
*pdev
)
1479 struct rt305x_esw
*esw
;
1481 esw
= platform_get_drvdata(pdev
);
1483 unregister_switch(&esw
->swdev
);
1484 platform_set_drvdata(pdev
, NULL
);
1492 static struct platform_driver esw_driver
= {
1494 .remove
= esw_remove
,
1496 .name
= "rt305x-esw",
1497 .owner
= THIS_MODULE
,
1498 .of_match_table
= ralink_esw_match
,
1502 int __init
rtesw_init(void)
1504 return platform_driver_register(&esw_driver
);
1507 void rtesw_exit(void)
1509 platform_driver_unregister(&esw_driver
);