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>
41 #ifdef CONFIG_SOC_MT7620
42 static inline int soc_is_rt3352(void)
47 static inline int soc_is_mt7628(void)
52 static inline int soc_is_rt5350(void)
57 #include <asm/mach-ralink/rt305x.h>
58 static inline int soc_is_mt7628(void)
64 #include <asm/mach-ralink/rt305x_esw_platform.h>
67 * HW limitations for this switch:
68 * - No large frame support (PKT_MAX_LEN at most 1536)
69 * - Can't have untagged vlan and tagged vlan on one port at the same time,
70 * though this might be possible using the undocumented PPE.
73 #define RT305X_ESW_REG_ISR 0x00
74 #define RT305X_ESW_REG_IMR 0x04
75 #define RT305X_ESW_REG_FCT0 0x08
76 #define RT305X_ESW_REG_PFC1 0x14
77 #define RT305X_ESW_REG_ATS 0x24
78 #define RT305X_ESW_REG_ATS0 0x28
79 #define RT305X_ESW_REG_ATS1 0x2c
80 #define RT305X_ESW_REG_ATS2 0x30
81 #define RT305X_ESW_REG_PVIDC(_n) (0x40 + 4 * (_n))
82 #define RT305X_ESW_REG_VLANI(_n) (0x50 + 4 * (_n))
83 #define RT305X_ESW_REG_VMSC(_n) (0x70 + 4 * (_n))
84 #define RT305X_ESW_REG_POA 0x80
85 #define RT305X_ESW_REG_FPA 0x84
86 #define RT305X_ESW_REG_SOCPC 0x8c
87 #define RT305X_ESW_REG_POC0 0x90
88 #define RT305X_ESW_REG_POC1 0x94
89 #define RT305X_ESW_REG_POC2 0x98
90 #define RT305X_ESW_REG_SGC 0x9c
91 #define RT305X_ESW_REG_STRT 0xa0
92 #define RT305X_ESW_REG_PCR0 0xc0
93 #define RT305X_ESW_REG_PCR1 0xc4
94 #define RT305X_ESW_REG_FPA2 0xc8
95 #define RT305X_ESW_REG_FCT2 0xcc
96 #define RT305X_ESW_REG_SGC2 0xe4
97 #define RT305X_ESW_REG_P0LED 0xa4
98 #define RT305X_ESW_REG_P1LED 0xa8
99 #define RT305X_ESW_REG_P2LED 0xac
100 #define RT305X_ESW_REG_P3LED 0xb0
101 #define RT305X_ESW_REG_P4LED 0xb4
102 #define RT305X_ESW_REG_PXPC(_x) (0xe8 + (4 * _x))
103 #define RT305X_ESW_REG_P1PC 0xec
104 #define RT305X_ESW_REG_P2PC 0xf0
105 #define RT305X_ESW_REG_P3PC 0xf4
106 #define RT305X_ESW_REG_P4PC 0xf8
107 #define RT305X_ESW_REG_P5PC 0xfc
109 #define RT305X_ESW_LED_LINK 0
110 #define RT305X_ESW_LED_100M 1
111 #define RT305X_ESW_LED_DUPLEX 2
112 #define RT305X_ESW_LED_ACTIVITY 3
113 #define RT305X_ESW_LED_COLLISION 4
114 #define RT305X_ESW_LED_LINKACT 5
115 #define RT305X_ESW_LED_DUPLCOLL 6
116 #define RT305X_ESW_LED_10MACT 7
117 #define RT305X_ESW_LED_100MACT 8
118 /* Additional led states not in datasheet: */
119 #define RT305X_ESW_LED_BLINK 10
120 #define RT305X_ESW_LED_ON 12
122 #define RT305X_ESW_LINK_S 25
123 #define RT305X_ESW_DUPLEX_S 9
124 #define RT305X_ESW_SPD_S 0
126 #define RT305X_ESW_PCR0_WT_NWAY_DATA_S 16
127 #define RT305X_ESW_PCR0_WT_PHY_CMD BIT(13)
128 #define RT305X_ESW_PCR0_CPU_PHY_REG_S 8
130 #define RT305X_ESW_PCR1_WT_DONE BIT(0)
132 #define RT305X_ESW_ATS_TIMEOUT (5 * HZ)
133 #define RT305X_ESW_PHY_TIMEOUT (5 * HZ)
135 #define RT305X_ESW_PVIDC_PVID_M 0xfff
136 #define RT305X_ESW_PVIDC_PVID_S 12
138 #define RT305X_ESW_VLANI_VID_M 0xfff
139 #define RT305X_ESW_VLANI_VID_S 12
141 #define RT305X_ESW_VMSC_MSC_M 0xff
142 #define RT305X_ESW_VMSC_MSC_S 8
144 #define RT305X_ESW_SOCPC_DISUN2CPU_S 0
145 #define RT305X_ESW_SOCPC_DISMC2CPU_S 8
146 #define RT305X_ESW_SOCPC_DISBC2CPU_S 16
147 #define RT305X_ESW_SOCPC_CRC_PADDING BIT(25)
149 #define RT305X_ESW_POC0_EN_BP_S 0
150 #define RT305X_ESW_POC0_EN_FC_S 8
151 #define RT305X_ESW_POC0_DIS_RMC2CPU_S 16
152 #define RT305X_ESW_POC0_DIS_PORT_M 0x7f
153 #define RT305X_ESW_POC0_DIS_PORT_S 23
155 #define RT305X_ESW_POC2_UNTAG_EN_M 0xff
156 #define RT305X_ESW_POC2_UNTAG_EN_S 0
157 #define RT305X_ESW_POC2_ENAGING_S 8
158 #define RT305X_ESW_POC2_DIS_UC_PAUSE_S 16
160 #define RT305X_ESW_SGC2_DOUBLE_TAG_M 0x7f
161 #define RT305X_ESW_SGC2_DOUBLE_TAG_S 0
162 #define RT305X_ESW_SGC2_LAN_PMAP_M 0x3f
163 #define RT305X_ESW_SGC2_LAN_PMAP_S 24
165 #define RT305X_ESW_PFC1_EN_VLAN_M 0xff
166 #define RT305X_ESW_PFC1_EN_VLAN_S 16
167 #define RT305X_ESW_PFC1_EN_TOS_S 24
169 #define RT305X_ESW_VLAN_NONE 0xfff
171 #define RT305X_ESW_GSC_BC_STROM_MASK 0x3
172 #define RT305X_ESW_GSC_BC_STROM_SHIFT 4
174 #define RT305X_ESW_GSC_LED_FREQ_MASK 0x3
175 #define RT305X_ESW_GSC_LED_FREQ_SHIFT 23
177 #define RT305X_ESW_POA_LINK_MASK 0x1f
178 #define RT305X_ESW_POA_LINK_SHIFT 25
180 #define RT305X_ESW_PORT_ST_CHG BIT(26)
181 #define RT305X_ESW_PORT0 0
182 #define RT305X_ESW_PORT1 1
183 #define RT305X_ESW_PORT2 2
184 #define RT305X_ESW_PORT3 3
185 #define RT305X_ESW_PORT4 4
186 #define RT305X_ESW_PORT5 5
187 #define RT305X_ESW_PORT6 6
189 #define RT305X_ESW_PORTS_NONE 0
191 #define RT305X_ESW_PMAP_LLLLLL 0x3f
192 #define RT305X_ESW_PMAP_LLLLWL 0x2f
193 #define RT305X_ESW_PMAP_WLLLLL 0x3e
195 #define RT305X_ESW_PORTS_INTERNAL \
196 (BIT(RT305X_ESW_PORT0) | BIT(RT305X_ESW_PORT1) | \
197 BIT(RT305X_ESW_PORT2) | BIT(RT305X_ESW_PORT3) | \
198 BIT(RT305X_ESW_PORT4))
200 #define RT305X_ESW_PORTS_NOCPU \
201 (RT305X_ESW_PORTS_INTERNAL | BIT(RT305X_ESW_PORT5))
203 #define RT305X_ESW_PORTS_CPU BIT(RT305X_ESW_PORT6)
205 #define RT305X_ESW_PORTS_ALL \
206 (RT305X_ESW_PORTS_NOCPU | RT305X_ESW_PORTS_CPU)
208 #define RT305X_ESW_NUM_VLANS 16
209 #define RT305X_ESW_NUM_VIDS 4096
210 #define RT305X_ESW_NUM_PORTS 7
211 #define RT305X_ESW_NUM_LANWAN 6
212 #define RT305X_ESW_NUM_LEDS 5
214 #define RT5350_ESW_REG_PXTPC(_x) (0x150 + (4 * _x))
215 #define RT5350_EWS_REG_LED_POLARITY 0x168
216 #define RT5350_RESET_EPHY BIT(24)
217 #define SYSC_REG_RESET_CTRL 0x34
220 /* Global attributes. */
221 RT305X_ESW_ATTR_ENABLE_VLAN
,
222 RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
223 RT305X_ESW_ATTR_BC_STATUS
,
224 RT305X_ESW_ATTR_LED_FREQ
,
225 /* Port attributes. */
226 RT305X_ESW_ATTR_PORT_DISABLE
,
227 RT305X_ESW_ATTR_PORT_DOUBLETAG
,
228 RT305X_ESW_ATTR_PORT_UNTAG
,
229 RT305X_ESW_ATTR_PORT_LED
,
230 RT305X_ESW_ATTR_PORT_LAN
,
231 RT305X_ESW_ATTR_PORT_RECV_BAD
,
232 RT305X_ESW_ATTR_PORT_RECV_GOOD
,
233 RT5350_ESW_ATTR_PORT_TR_BAD
,
234 RT5350_ESW_ATTR_PORT_TR_GOOD
,
254 const struct rt305x_esw_platform_data
*pdata
;
255 /* Protects against concurrent register rmw operations. */
256 spinlock_t reg_rw_lock
;
258 unsigned char port_map
;
259 unsigned int reg_initval_fct2
;
260 unsigned int reg_initval_fpa2
;
261 unsigned int reg_led_polarity
;
264 struct switch_dev swdev
;
265 bool global_vlan_enable
;
266 bool alt_vlan_disable
;
267 int bc_storm_protect
;
269 struct esw_vlan vlans
[RT305X_ESW_NUM_VLANS
];
270 struct esw_port ports
[RT305X_ESW_NUM_PORTS
];
274 static inline void esw_w32(struct rt305x_esw
*esw
, u32 val
, unsigned reg
)
276 __raw_writel(val
, esw
->base
+ reg
);
279 static inline u32
esw_r32(struct rt305x_esw
*esw
, unsigned reg
)
281 return __raw_readl(esw
->base
+ reg
);
284 static inline void esw_rmw_raw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
289 t
= __raw_readl(esw
->base
+ reg
) & ~mask
;
290 __raw_writel(t
| val
, esw
->base
+ reg
);
293 static void esw_rmw(struct rt305x_esw
*esw
, unsigned reg
, unsigned long mask
,
298 spin_lock_irqsave(&esw
->reg_rw_lock
, flags
);
299 esw_rmw_raw(esw
, reg
, mask
, val
);
300 spin_unlock_irqrestore(&esw
->reg_rw_lock
, flags
);
303 static u32
rt305x_mii_write(struct rt305x_esw
*esw
, u32 phy_addr
, u32 phy_register
,
306 unsigned long t_start
= jiffies
;
310 if (!(esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
311 RT305X_ESW_PCR1_WT_DONE
))
313 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
319 write_data
&= 0xffff;
321 (write_data
<< RT305X_ESW_PCR0_WT_NWAY_DATA_S
) |
322 (phy_register
<< RT305X_ESW_PCR0_CPU_PHY_REG_S
) |
323 (phy_addr
) | RT305X_ESW_PCR0_WT_PHY_CMD
,
324 RT305X_ESW_REG_PCR0
);
328 if (esw_r32(esw
, RT305X_ESW_REG_PCR1
) &
329 RT305X_ESW_PCR1_WT_DONE
)
332 if (time_after(jiffies
, t_start
+ RT305X_ESW_PHY_TIMEOUT
)) {
339 printk(KERN_ERR
"ramips_eth: MDIO timeout\n");
343 static unsigned esw_get_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
)
348 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
349 val
= esw_r32(esw
, RT305X_ESW_REG_VLANI(vlan
/ 2));
350 val
= (val
>> s
) & RT305X_ESW_VLANI_VID_M
;
355 static void esw_set_vlan_id(struct rt305x_esw
*esw
, unsigned vlan
, unsigned vid
)
359 s
= RT305X_ESW_VLANI_VID_S
* (vlan
% 2);
361 RT305X_ESW_REG_VLANI(vlan
/ 2),
362 RT305X_ESW_VLANI_VID_M
<< s
,
363 (vid
& RT305X_ESW_VLANI_VID_M
) << s
);
366 static unsigned esw_get_pvid(struct rt305x_esw
*esw
, unsigned port
)
370 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
371 val
= esw_r32(esw
, RT305X_ESW_REG_PVIDC(port
/ 2));
372 return (val
>> s
) & RT305X_ESW_PVIDC_PVID_M
;
375 static void esw_set_pvid(struct rt305x_esw
*esw
, unsigned port
, unsigned pvid
)
379 s
= RT305X_ESW_PVIDC_PVID_S
* (port
% 2);
381 RT305X_ESW_REG_PVIDC(port
/ 2),
382 RT305X_ESW_PVIDC_PVID_M
<< s
,
383 (pvid
& RT305X_ESW_PVIDC_PVID_M
) << s
);
386 static unsigned esw_get_vmsc(struct rt305x_esw
*esw
, unsigned vlan
)
390 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
391 val
= esw_r32(esw
, RT305X_ESW_REG_VMSC(vlan
/ 4));
392 val
= (val
>> s
) & RT305X_ESW_VMSC_MSC_M
;
397 static void esw_set_vmsc(struct rt305x_esw
*esw
, unsigned vlan
, unsigned msc
)
401 s
= RT305X_ESW_VMSC_MSC_S
* (vlan
% 4);
403 RT305X_ESW_REG_VMSC(vlan
/ 4),
404 RT305X_ESW_VMSC_MSC_M
<< s
,
405 (msc
& RT305X_ESW_VMSC_MSC_M
) << s
);
408 static unsigned esw_get_port_disable(struct rt305x_esw
*esw
)
411 reg
= esw_r32(esw
, RT305X_ESW_REG_POC0
);
412 return (reg
>> RT305X_ESW_POC0_DIS_PORT_S
) &
413 RT305X_ESW_POC0_DIS_PORT_M
;
416 static void esw_set_port_disable(struct rt305x_esw
*esw
, unsigned disable_mask
)
419 unsigned enable_mask
;
423 old_mask
= esw_get_port_disable(esw
);
424 changed
= old_mask
^ disable_mask
;
425 enable_mask
= old_mask
& disable_mask
;
427 /* enable before writing to MII */
428 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
429 (RT305X_ESW_POC0_DIS_PORT_M
<<
430 RT305X_ESW_POC0_DIS_PORT_S
),
431 enable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
433 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++) {
434 if (!(changed
& (1 << i
)))
436 if (disable_mask
& (1 << i
)) {
438 rt305x_mii_write(esw
, i
, MII_BMCR
,
442 rt305x_mii_write(esw
, i
, MII_BMCR
,
450 /* disable after writing to MII */
451 esw_rmw(esw
, RT305X_ESW_REG_POC0
,
452 (RT305X_ESW_POC0_DIS_PORT_M
<<
453 RT305X_ESW_POC0_DIS_PORT_S
),
454 disable_mask
<< RT305X_ESW_POC0_DIS_PORT_S
);
457 static void esw_set_gsc(struct rt305x_esw
*esw
)
459 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
460 RT305X_ESW_GSC_BC_STROM_MASK
<< RT305X_ESW_GSC_BC_STROM_SHIFT
,
461 esw
->bc_storm_protect
<< RT305X_ESW_GSC_BC_STROM_SHIFT
);
462 esw_rmw(esw
, RT305X_ESW_REG_SGC
,
463 RT305X_ESW_GSC_LED_FREQ_MASK
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
,
464 esw
->led_frequency
<< RT305X_ESW_GSC_LED_FREQ_SHIFT
);
467 static int esw_apply_config(struct switch_dev
*dev
);
469 static void esw_hw_init(struct rt305x_esw
*esw
)
473 u8 port_map
= RT305X_ESW_PMAP_LLLLLL
;
475 /* vodoo from original driver */
476 esw_w32(esw
, 0xC8A07850, RT305X_ESW_REG_FCT0
);
477 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_SGC2
);
478 /* Port priority 1 for all ports, vlan enabled. */
479 esw_w32(esw
, 0x00005555 |
480 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_PFC1_EN_VLAN_S
),
481 RT305X_ESW_REG_PFC1
);
483 /* Enable Back Pressure, and Flow Control */
485 ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_BP_S
) |
486 (RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC0_EN_FC_S
)),
487 RT305X_ESW_REG_POC0
);
489 /* Enable Aging, and VLAN TAG removal */
491 ((RT305X_ESW_PORTS_ALL
<< RT305X_ESW_POC2_ENAGING_S
) |
492 (RT305X_ESW_PORTS_NOCPU
<< RT305X_ESW_POC2_UNTAG_EN_S
)),
493 RT305X_ESW_REG_POC2
);
495 if (esw
->reg_initval_fct2
)
496 esw_w32(esw
, esw
->reg_initval_fct2
, RT305X_ESW_REG_FCT2
);
498 esw_w32(esw
, esw
->pdata
->reg_initval_fct2
, RT305X_ESW_REG_FCT2
);
501 * 300s aging timer, max packet len 1536, broadcast storm prevention
502 * disabled, disable collision abort, mac xor48 hash, 10 packet back
503 * pressure jam, GMII disable was_transmit, back pressure disabled,
504 * 30ms led flash, unmatched IGMP as broadcast, rmc tb fault to all
507 esw_w32(esw
, 0x0008a301, RT305X_ESW_REG_SGC
);
509 /* Setup SoC Port control register */
511 (RT305X_ESW_SOCPC_CRC_PADDING
|
512 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISUN2CPU_S
) |
513 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISMC2CPU_S
) |
514 (RT305X_ESW_PORTS_CPU
<< RT305X_ESW_SOCPC_DISBC2CPU_S
)),
515 RT305X_ESW_REG_SOCPC
);
517 if (esw
->reg_initval_fpa2
)
518 esw_w32(esw
, esw
->reg_initval_fpa2
, RT305X_ESW_REG_FPA2
);
520 esw_w32(esw
, esw
->pdata
->reg_initval_fpa2
, RT305X_ESW_REG_FPA2
);
521 esw_w32(esw
, 0x00000000, RT305X_ESW_REG_FPA
);
523 /* Force Link/Activity on ports */
524 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P0LED
);
525 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P1LED
);
526 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P2LED
);
527 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P3LED
);
528 esw_w32(esw
, 0x00000005, RT305X_ESW_REG_P4LED
);
530 /* Copy disabled port configuration from bootloader setup */
531 port_disable
= esw_get_port_disable(esw
);
532 for (i
= 0; i
< 6; i
++)
533 esw
->ports
[i
].disable
= (port_disable
& (1 << i
)) != 0;
535 if (soc_is_rt3352()) {
537 u32 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
538 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
539 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
541 rt305x_mii_write(esw
, 0, 31, 0x8000);
542 for (i
= 0; i
< 5; i
++) {
543 if (esw
->ports
[i
].disable
) {
544 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
546 rt305x_mii_write(esw
, i
, MII_BMCR
,
551 /* TX10 waveform coefficient LSB=0 disable PHY */
552 rt305x_mii_write(esw
, i
, 26, 0x1601);
553 /* TX100/TX10 AD/DA current bias */
554 rt305x_mii_write(esw
, i
, 29, 0x7016);
555 /* TX100 slew rate control */
556 rt305x_mii_write(esw
, i
, 30, 0x0038);
559 /* select global register */
560 rt305x_mii_write(esw
, 0, 31, 0x0);
561 /* enlarge agcsel threshold 3 and threshold 2 */
562 rt305x_mii_write(esw
, 0, 1, 0x4a40);
563 /* enlarge agcsel threshold 5 and threshold 4 */
564 rt305x_mii_write(esw
, 0, 2, 0x6254);
565 /* enlarge agcsel threshold */
566 rt305x_mii_write(esw
, 0, 3, 0xa17f);
567 rt305x_mii_write(esw
, 0,12, 0x7eaa);
568 /* longer TP_IDL tail length */
569 rt305x_mii_write(esw
, 0, 14, 0x65);
570 /* increased squelch pulse count threshold. */
571 rt305x_mii_write(esw
, 0, 16, 0x0684);
572 /* set TX10 signal amplitude threshold to minimum */
573 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
574 /* set squelch amplitude to higher threshold */
575 rt305x_mii_write(esw
, 0, 18, 0x40ba);
576 /* tune TP_IDL tail and head waveform, enable power down slew rate control */
577 rt305x_mii_write(esw
, 0, 22, 0x253f);
578 /* set PLL/Receive bias current are calibrated */
579 rt305x_mii_write(esw
, 0, 27, 0x2fda);
580 /* change PLL/Receive bias current to internal(RT3350) */
581 rt305x_mii_write(esw
, 0, 28, 0xc410);
582 /* change PLL bias current to internal(RT3052_MP3) */
583 rt305x_mii_write(esw
, 0, 29, 0x598b);
584 /* select local register */
585 rt305x_mii_write(esw
, 0, 31, 0x8000);
586 } else if (soc_is_rt5350()) {
588 u32 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
589 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
590 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
592 /* set the led polarity */
593 esw_w32(esw
, esw
->reg_led_polarity
& 0x1F, RT5350_EWS_REG_LED_POLARITY
);
595 /* local registers */
596 rt305x_mii_write(esw
, 0, 31, 0x8000);
597 for (i
= 0; i
< 5; i
++) {
598 if (esw
->ports
[i
].disable
) {
599 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
601 rt305x_mii_write(esw
, i
, MII_BMCR
,
606 /* TX10 waveform coefficient LSB=0 disable PHY */
607 rt305x_mii_write(esw
, i
, 26, 0x1601);
608 /* TX100/TX10 AD/DA current bias */
609 rt305x_mii_write(esw
, i
, 29, 0x7015);
610 /* TX100 slew rate control */
611 rt305x_mii_write(esw
, i
, 30, 0x0038);
614 /* global registers */
615 rt305x_mii_write(esw
, 0, 31, 0x0);
616 /* enlarge agcsel threshold 3 and threshold 2 */
617 rt305x_mii_write(esw
, 0, 1, 0x4a40);
618 /* enlarge agcsel threshold 5 and threshold 4 */
619 rt305x_mii_write(esw
, 0, 2, 0x6254);
620 /* enlarge agcsel threshold 6 */
621 rt305x_mii_write(esw
, 0, 3, 0xa17f);
622 rt305x_mii_write(esw
, 0, 12, 0x7eaa);
623 /* longer TP_IDL tail length */
624 rt305x_mii_write(esw
, 0, 14, 0x65);
625 /* increased squelch pulse count threshold. */
626 rt305x_mii_write(esw
, 0, 16, 0x0684);
627 /* set TX10 signal amplitude threshold to minimum */
628 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
629 /* set squelch amplitude to higher threshold */
630 rt305x_mii_write(esw
, 0, 18, 0x40ba);
631 /* tune TP_IDL tail and head waveform, enable power down slew rate control */
632 rt305x_mii_write(esw
, 0, 22, 0x253f);
633 /* set PLL/Receive bias current are calibrated */
634 rt305x_mii_write(esw
, 0, 27, 0x2fda);
635 /* change PLL/Receive bias current to internal(RT3350) */
636 rt305x_mii_write(esw
, 0, 28, 0xc410);
637 /* change PLL bias current to internal(RT3052_MP3) */
638 rt305x_mii_write(esw
, 0, 29, 0x598b);
639 /* select local register */
640 rt305x_mii_write(esw
, 0, 31, 0x8000);
641 } else if (soc_is_mt7628()) {
647 val
= rt_sysc_r32(SYSC_REG_RESET_CTRL
);
648 rt_sysc_w32(val
| RT5350_RESET_EPHY
, SYSC_REG_RESET_CTRL
);
649 rt_sysc_w32(val
, SYSC_REG_RESET_CTRL
);
651 rt305x_mii_write(esw
, 0, 31, 0x2000); /* change G2 page */
652 rt305x_mii_write(esw
, 0, 26, 0x0020);
654 for (i
= 0; i
< 5; i
++) {
655 rt305x_mii_write(esw
, i
, 31, 0x8000); //change L0 page
656 rt305x_mii_write(esw
, i
, 0, 0x3100);
657 // mii_mgr_read(i, 26, &phy_val);// EEE setting
658 // phy_val |= (1 << 5);
659 // rt305x_mii_write(esw, i, 26, phy_val);
660 rt305x_mii_write(esw
, i
, 30, 0xa000);
661 rt305x_mii_write(esw
, i
, 31, 0xa000); // change L2 page
662 rt305x_mii_write(esw
, i
, 16, 0x0606);
663 rt305x_mii_write(esw
, i
, 23, 0x0f0e);
664 rt305x_mii_write(esw
, i
, 24, 0x1610);
665 rt305x_mii_write(esw
, i
, 30, 0x1f15);
666 rt305x_mii_write(esw
, i
, 28, 0x6111);
667 // mii_mgr_read(i, 4, &phy_val);
668 // phy_val |= (1 << 10);
669 // rt305x_mii_write(esw, i, 4, phy_val);
670 rt305x_mii_write(esw
, i
, 31, 0x2000); // change G2 page
671 rt305x_mii_write(esw
, i
, 26, 0x0000);
674 //100Base AOI setting
675 rt305x_mii_write(esw
, 0, 31, 0x5000); //change G5 page
676 rt305x_mii_write(esw
, 0, 19, 0x004a);
677 rt305x_mii_write(esw
, 0, 20, 0x015a);
678 rt305x_mii_write(esw
, 0, 21, 0x00ee);
679 rt305x_mii_write(esw
, 0, 22, 0x0033);
680 rt305x_mii_write(esw
, 0, 23, 0x020a);
681 rt305x_mii_write(esw
, 0, 24, 0x0000);
682 rt305x_mii_write(esw
, 0, 25, 0x024a);
683 rt305x_mii_write(esw
, 0, 26, 0x035a);
684 rt305x_mii_write(esw
, 0, 27, 0x02ee);
685 rt305x_mii_write(esw
, 0, 28, 0x0233);
686 rt305x_mii_write(esw
, 0, 29, 0x000a);
687 rt305x_mii_write(esw
, 0, 30, 0x0000);
689 rt305x_mii_write(esw
, 0, 31, 0x8000);
690 for (i
= 0; i
< 5; i
++) {
691 if (esw
->ports
[i
].disable
) {
692 rt305x_mii_write(esw
, i
, MII_BMCR
, BMCR_PDOWN
);
694 rt305x_mii_write(esw
, i
, MII_BMCR
,
699 /* TX10 waveform coefficient */
700 rt305x_mii_write(esw
, i
, 26, 0x1601);
701 /* TX100/TX10 AD/DA current bias */
702 rt305x_mii_write(esw
, i
, 29, 0x7058);
703 /* TX100 slew rate control */
704 rt305x_mii_write(esw
, i
, 30, 0x0018);
708 /* select global register */
709 rt305x_mii_write(esw
, 0, 31, 0x0);
710 /* tune TP_IDL tail and head waveform */
711 rt305x_mii_write(esw
, 0, 22, 0x052f);
712 /* set TX10 signal amplitude threshold to minimum */
713 rt305x_mii_write(esw
, 0, 17, 0x0fe0);
714 /* set squelch amplitude to higher threshold */
715 rt305x_mii_write(esw
, 0, 18, 0x40ba);
716 /* longer TP_IDL tail length */
717 rt305x_mii_write(esw
, 0, 14, 0x65);
718 /* select local register */
719 rt305x_mii_write(esw
, 0, 31, 0x8000);
723 port_map
= esw
->port_map
;
725 port_map
= RT305X_ESW_PMAP_LLLLLL
;
728 * Unused HW feature, but still nice to be consistent here...
729 * This is also exported to userspace ('lan' attribute) so it's
730 * conveniently usable to decide which ports go into the wan vlan by
733 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
734 RT305X_ESW_SGC2_LAN_PMAP_M
<< RT305X_ESW_SGC2_LAN_PMAP_S
,
735 port_map
<< RT305X_ESW_SGC2_LAN_PMAP_S
);
737 /* make the switch leds blink */
738 for (i
= 0; i
< RT305X_ESW_NUM_LEDS
; i
++)
739 esw
->ports
[i
].led
= 0x05;
741 /* Apply the empty config. */
742 esw_apply_config(&esw
->swdev
);
744 /* Only unmask the port change interrupt */
745 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
748 static irqreturn_t
esw_interrupt(int irq
, void *_esw
)
750 struct rt305x_esw
*esw
= (struct rt305x_esw
*) _esw
;
753 status
= esw_r32(esw
, RT305X_ESW_REG_ISR
);
754 if (status
& RT305X_ESW_PORT_ST_CHG
) {
755 u32 link
= esw_r32(esw
, RT305X_ESW_REG_POA
);
756 link
>>= RT305X_ESW_POA_LINK_SHIFT
;
757 link
&= RT305X_ESW_POA_LINK_MASK
;
758 dev_info(esw
->dev
, "link changed 0x%02X\n", link
);
760 esw_w32(esw
, status
, RT305X_ESW_REG_ISR
);
765 static int esw_apply_config(struct switch_dev
*dev
)
767 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
774 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
776 if (esw
->global_vlan_enable
) {
777 vid
= esw
->vlans
[i
].vid
;
778 vmsc
= esw
->vlans
[i
].ports
;
780 vid
= RT305X_ESW_VLAN_NONE
;
781 vmsc
= RT305X_ESW_PORTS_NONE
;
783 esw_set_vlan_id(esw
, i
, vid
);
784 esw_set_vmsc(esw
, i
, vmsc
);
787 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
789 disable
|= esw
->ports
[i
].disable
<< i
;
790 if (esw
->global_vlan_enable
) {
791 doubletag
|= esw
->ports
[i
].doubletag
<< i
;
793 untag
|= esw
->ports
[i
].untag
<< i
;
794 pvid
= esw
->ports
[i
].pvid
;
796 int x
= esw
->alt_vlan_disable
? 0 : 1;
802 esw_set_pvid(esw
, i
, pvid
);
803 if (i
< RT305X_ESW_NUM_LEDS
)
804 esw_w32(esw
, esw
->ports
[i
].led
,
805 RT305X_ESW_REG_P0LED
+ 4*i
);
809 esw_set_port_disable(esw
, disable
);
810 esw_rmw(esw
, RT305X_ESW_REG_SGC2
,
811 (RT305X_ESW_SGC2_DOUBLE_TAG_M
<<
812 RT305X_ESW_SGC2_DOUBLE_TAG_S
),
813 doubletag
<< RT305X_ESW_SGC2_DOUBLE_TAG_S
);
814 esw_rmw(esw
, RT305X_ESW_REG_PFC1
,
815 RT305X_ESW_PFC1_EN_VLAN_M
<< RT305X_ESW_PFC1_EN_VLAN_S
,
816 en_vlan
<< RT305X_ESW_PFC1_EN_VLAN_S
);
817 esw_rmw(esw
, RT305X_ESW_REG_POC2
,
818 RT305X_ESW_POC2_UNTAG_EN_M
<< RT305X_ESW_POC2_UNTAG_EN_S
,
819 untag
<< RT305X_ESW_POC2_UNTAG_EN_S
);
821 if (!esw
->global_vlan_enable
) {
823 * Still need to put all ports into vlan 0 or they'll be
825 * NOTE: vlan 0 is special, no vlan tag is prepended
827 esw_set_vlan_id(esw
, 0, 0);
828 esw_set_vmsc(esw
, 0, RT305X_ESW_PORTS_ALL
);
834 static int esw_reset_switch(struct switch_dev
*dev
)
836 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
838 esw
->global_vlan_enable
= 0;
839 memset(esw
->ports
, 0, sizeof(esw
->ports
));
840 memset(esw
->vlans
, 0, sizeof(esw
->vlans
));
846 static int esw_get_vlan_enable(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
->global_vlan_enable
;
857 static int esw_set_vlan_enable(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
->global_vlan_enable
= val
->value
.i
!= 0;
868 static int esw_get_alt_vlan_disable(struct switch_dev
*dev
,
869 const struct switch_attr
*attr
,
870 struct switch_val
*val
)
872 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
874 val
->value
.i
= esw
->alt_vlan_disable
;
879 static int esw_set_alt_vlan_disable(struct switch_dev
*dev
,
880 const struct switch_attr
*attr
,
881 struct switch_val
*val
)
883 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
885 esw
->alt_vlan_disable
= val
->value
.i
!= 0;
891 rt305x_esw_set_bc_status(struct switch_dev
*dev
,
892 const struct switch_attr
*attr
,
893 struct switch_val
*val
)
895 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
897 esw
->bc_storm_protect
= val
->value
.i
& RT305X_ESW_GSC_BC_STROM_MASK
;
903 rt305x_esw_get_bc_status(struct switch_dev
*dev
,
904 const struct switch_attr
*attr
,
905 struct switch_val
*val
)
907 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
909 val
->value
.i
= esw
->bc_storm_protect
;
915 rt305x_esw_set_led_freq(struct switch_dev
*dev
,
916 const struct switch_attr
*attr
,
917 struct switch_val
*val
)
919 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
921 esw
->led_frequency
= val
->value
.i
& RT305X_ESW_GSC_LED_FREQ_MASK
;
927 rt305x_esw_get_led_freq(struct switch_dev
*dev
,
928 const struct switch_attr
*attr
,
929 struct switch_val
*val
)
931 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
933 val
->value
.i
= esw
->led_frequency
;
938 static int esw_get_port_link(struct switch_dev
*dev
,
940 struct switch_port_link
*link
)
942 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
945 if (port
< 0 || port
>= RT305X_ESW_NUM_PORTS
)
948 poa
= esw_r32(esw
, RT305X_ESW_REG_POA
) >> port
;
950 link
->link
= (poa
>> RT305X_ESW_LINK_S
) & 1;
951 link
->duplex
= (poa
>> RT305X_ESW_DUPLEX_S
) & 1;
952 if (port
< RT305X_ESW_NUM_LEDS
) {
953 speed
= (poa
>> RT305X_ESW_SPD_S
) & 1;
955 if (port
== RT305X_ESW_NUM_PORTS
- 1)
957 speed
= (poa
>> RT305X_ESW_SPD_S
) & 3;
961 link
->speed
= SWITCH_PORT_SPEED_10
;
964 link
->speed
= SWITCH_PORT_SPEED_100
;
967 case 3: /* forced gige speed can be 2 or 3 */
968 link
->speed
= SWITCH_PORT_SPEED_1000
;
971 link
->speed
= SWITCH_PORT_SPEED_UNKNOWN
;
978 static int esw_get_port_bool(struct switch_dev
*dev
,
979 const struct switch_attr
*attr
,
980 struct switch_val
*val
)
982 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
983 int idx
= val
->port_vlan
;
986 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
)
990 case RT305X_ESW_ATTR_PORT_DISABLE
:
991 reg
= RT305X_ESW_REG_POC0
;
992 shift
= RT305X_ESW_POC0_DIS_PORT_S
;
994 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
995 reg
= RT305X_ESW_REG_SGC2
;
996 shift
= RT305X_ESW_SGC2_DOUBLE_TAG_S
;
998 case RT305X_ESW_ATTR_PORT_UNTAG
:
999 reg
= RT305X_ESW_REG_POC2
;
1000 shift
= RT305X_ESW_POC2_UNTAG_EN_S
;
1002 case RT305X_ESW_ATTR_PORT_LAN
:
1003 reg
= RT305X_ESW_REG_SGC2
;
1004 shift
= RT305X_ESW_SGC2_LAN_PMAP_S
;
1005 if (idx
>= RT305X_ESW_NUM_LANWAN
)
1012 x
= esw_r32(esw
, reg
);
1013 val
->value
.i
= (x
>> (idx
+ shift
)) & 1;
1018 static int esw_set_port_bool(struct switch_dev
*dev
,
1019 const struct switch_attr
*attr
,
1020 struct switch_val
*val
)
1022 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1023 int idx
= val
->port_vlan
;
1025 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1026 val
->value
.i
< 0 || val
->value
.i
> 1)
1030 case RT305X_ESW_ATTR_PORT_DISABLE
:
1031 esw
->ports
[idx
].disable
= val
->value
.i
;
1033 case RT305X_ESW_ATTR_PORT_DOUBLETAG
:
1034 esw
->ports
[idx
].doubletag
= val
->value
.i
;
1036 case RT305X_ESW_ATTR_PORT_UNTAG
:
1037 esw
->ports
[idx
].untag
= val
->value
.i
;
1046 static int esw_get_port_recv_badgood(struct switch_dev
*dev
,
1047 const struct switch_attr
*attr
,
1048 struct switch_val
*val
)
1050 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1051 int idx
= val
->port_vlan
;
1052 int shift
= attr
->id
== RT305X_ESW_ATTR_PORT_RECV_GOOD
? 0 : 16;
1055 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1057 reg
= esw_r32(esw
, RT305X_ESW_REG_PXPC(idx
));
1058 val
->value
.i
= (reg
>> shift
) & 0xffff;
1064 esw_get_port_tr_badgood(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
);
1070 int idx
= val
->port_vlan
;
1071 int shift
= attr
->id
== RT5350_ESW_ATTR_PORT_TR_GOOD
? 0 : 16;
1074 if (!soc_is_rt5350() && !soc_is_mt7628())
1077 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LANWAN
)
1080 reg
= esw_r32(esw
, RT5350_ESW_REG_PXTPC(idx
));
1081 val
->value
.i
= (reg
>> shift
) & 0xffff;
1086 static int esw_get_port_led(struct switch_dev
*dev
,
1087 const struct switch_attr
*attr
,
1088 struct switch_val
*val
)
1090 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1091 int idx
= val
->port_vlan
;
1093 if (idx
< 0 || idx
>= RT305X_ESW_NUM_PORTS
||
1094 idx
>= RT305X_ESW_NUM_LEDS
)
1097 val
->value
.i
= esw_r32(esw
, RT305X_ESW_REG_P0LED
+ 4*idx
);
1102 static int esw_set_port_led(struct switch_dev
*dev
,
1103 const struct switch_attr
*attr
,
1104 struct switch_val
*val
)
1106 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1107 int idx
= val
->port_vlan
;
1109 if (idx
< 0 || idx
>= RT305X_ESW_NUM_LEDS
)
1112 esw
->ports
[idx
].led
= val
->value
.i
;
1117 static int esw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1119 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1121 if (port
>= RT305X_ESW_NUM_PORTS
)
1124 *val
= esw_get_pvid(esw
, port
);
1129 static int esw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1131 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1133 if (port
>= RT305X_ESW_NUM_PORTS
)
1136 esw
->ports
[port
].pvid
= val
;
1141 static int esw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1143 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1150 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
)
1154 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1155 if (esw_get_vlan_id(esw
, i
) == val
->port_vlan
&&
1156 esw_get_vmsc(esw
, i
) != RT305X_ESW_PORTS_NONE
) {
1165 vmsc
= esw_get_vmsc(esw
, vlan_idx
);
1166 poc2
= esw_r32(esw
, RT305X_ESW_REG_POC2
);
1168 for (i
= 0; i
< RT305X_ESW_NUM_PORTS
; i
++) {
1169 struct switch_port
*p
;
1170 int port_mask
= 1 << i
;
1172 if (!(vmsc
& port_mask
))
1175 p
= &val
->value
.ports
[val
->len
++];
1177 if (poc2
& (port_mask
<< RT305X_ESW_POC2_UNTAG_EN_S
))
1180 p
->flags
= 1 << SWITCH_PORT_FLAG_TAGGED
;
1186 static int esw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1188 struct rt305x_esw
*esw
= container_of(dev
, struct rt305x_esw
, swdev
);
1193 if (val
->port_vlan
< 0 || val
->port_vlan
>= RT305X_ESW_NUM_VIDS
||
1194 val
->len
> RT305X_ESW_NUM_PORTS
)
1197 /* one of the already defined vlans? */
1198 for (i
= 0; i
< RT305X_ESW_NUM_VLANS
; i
++) {
1199 if (esw
->vlans
[i
].vid
== val
->port_vlan
&&
1200 esw
->vlans
[i
].ports
!= RT305X_ESW_PORTS_NONE
) {
1206 /* select a free slot */
1207 for (i
= 0; vlan_idx
== -1 && i
< RT305X_ESW_NUM_VLANS
; i
++) {
1208 if (esw
->vlans
[i
].ports
== RT305X_ESW_PORTS_NONE
)
1212 /* bail if all slots are in use */
1216 ports
= RT305X_ESW_PORTS_NONE
;
1217 for (i
= 0; i
< val
->len
; i
++) {
1218 struct switch_port
*p
= &val
->value
.ports
[i
];
1219 int port_mask
= 1 << p
->id
;
1220 bool untagged
= !(p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
));
1222 if (p
->id
>= RT305X_ESW_NUM_PORTS
)
1226 esw
->ports
[p
->id
].untag
= untagged
;
1228 esw
->vlans
[vlan_idx
].ports
= ports
;
1229 if (ports
== RT305X_ESW_PORTS_NONE
)
1230 esw
->vlans
[vlan_idx
].vid
= RT305X_ESW_VLAN_NONE
;
1232 esw
->vlans
[vlan_idx
].vid
= val
->port_vlan
;
1237 static const struct switch_attr esw_global
[] = {
1239 .type
= SWITCH_TYPE_INT
,
1240 .name
= "enable_vlan",
1241 .description
= "VLAN mode (1:enabled)",
1243 .id
= RT305X_ESW_ATTR_ENABLE_VLAN
,
1244 .get
= esw_get_vlan_enable
,
1245 .set
= esw_set_vlan_enable
,
1248 .type
= SWITCH_TYPE_INT
,
1249 .name
= "alternate_vlan_disable",
1250 .description
= "Use en_vlan instead of doubletag to disable"
1253 .id
= RT305X_ESW_ATTR_ALT_VLAN_DISABLE
,
1254 .get
= esw_get_alt_vlan_disable
,
1255 .set
= esw_set_alt_vlan_disable
,
1258 .type
= SWITCH_TYPE_INT
,
1259 .name
= "bc_storm_protect",
1260 .description
= "Global broadcast storm protection (0:Disable, 1:64 blocks, 2:96 blocks, 3:128 blocks)",
1262 .id
= RT305X_ESW_ATTR_BC_STATUS
,
1263 .get
= rt305x_esw_get_bc_status
,
1264 .set
= rt305x_esw_set_bc_status
,
1267 .type
= SWITCH_TYPE_INT
,
1268 .name
= "led_frequency",
1269 .description
= "LED Flash frequency (0:30mS, 1:60mS, 2:240mS, 3:480mS)",
1271 .id
= RT305X_ESW_ATTR_LED_FREQ
,
1272 .get
= rt305x_esw_get_led_freq
,
1273 .set
= rt305x_esw_set_led_freq
,
1277 static const struct switch_attr esw_port
[] = {
1279 .type
= SWITCH_TYPE_INT
,
1281 .description
= "Port state (1:disabled)",
1283 .id
= RT305X_ESW_ATTR_PORT_DISABLE
,
1284 .get
= esw_get_port_bool
,
1285 .set
= esw_set_port_bool
,
1288 .type
= SWITCH_TYPE_INT
,
1289 .name
= "doubletag",
1290 .description
= "Double tagging for incoming vlan packets "
1293 .id
= RT305X_ESW_ATTR_PORT_DOUBLETAG
,
1294 .get
= esw_get_port_bool
,
1295 .set
= esw_set_port_bool
,
1298 .type
= SWITCH_TYPE_INT
,
1300 .description
= "Untag (1:strip outgoing vlan tag)",
1302 .id
= RT305X_ESW_ATTR_PORT_UNTAG
,
1303 .get
= esw_get_port_bool
,
1304 .set
= esw_set_port_bool
,
1307 .type
= SWITCH_TYPE_INT
,
1309 .description
= "LED mode (0:link, 1:100m, 2:duplex, 3:activity,"
1310 " 4:collision, 5:linkact, 6:duplcoll, 7:10mact,"
1311 " 8:100mact, 10:blink, 11:off, 12:on)",
1313 .id
= RT305X_ESW_ATTR_PORT_LED
,
1314 .get
= esw_get_port_led
,
1315 .set
= esw_set_port_led
,
1318 .type
= SWITCH_TYPE_INT
,
1320 .description
= "HW port group (0:wan, 1:lan)",
1322 .id
= RT305X_ESW_ATTR_PORT_LAN
,
1323 .get
= esw_get_port_bool
,
1326 .type
= SWITCH_TYPE_INT
,
1328 .description
= "Receive bad packet counter",
1329 .id
= RT305X_ESW_ATTR_PORT_RECV_BAD
,
1330 .get
= esw_get_port_recv_badgood
,
1333 .type
= SWITCH_TYPE_INT
,
1334 .name
= "recv_good",
1335 .description
= "Receive good packet counter",
1336 .id
= RT305X_ESW_ATTR_PORT_RECV_GOOD
,
1337 .get
= esw_get_port_recv_badgood
,
1340 .type
= SWITCH_TYPE_INT
,
1343 .description
= "Transmit bad packet counter. rt5350 only",
1344 .id
= RT5350_ESW_ATTR_PORT_TR_BAD
,
1345 .get
= esw_get_port_tr_badgood
,
1348 .type
= SWITCH_TYPE_INT
,
1351 .description
= "Transmit good packet counter. rt5350 only",
1352 .id
= RT5350_ESW_ATTR_PORT_TR_GOOD
,
1353 .get
= esw_get_port_tr_badgood
,
1357 static const struct switch_attr esw_vlan
[] = {
1360 static const struct switch_dev_ops esw_ops
= {
1363 .n_attr
= ARRAY_SIZE(esw_global
),
1367 .n_attr
= ARRAY_SIZE(esw_port
),
1371 .n_attr
= ARRAY_SIZE(esw_vlan
),
1373 .get_vlan_ports
= esw_get_vlan_ports
,
1374 .set_vlan_ports
= esw_set_vlan_ports
,
1375 .get_port_pvid
= esw_get_port_pvid
,
1376 .set_port_pvid
= esw_set_port_pvid
,
1377 .get_port_link
= esw_get_port_link
,
1378 .apply_config
= esw_apply_config
,
1379 .reset_switch
= esw_reset_switch
,
1382 static struct rt305x_esw_platform_data rt3050_esw_data
= {
1383 /* All ports are LAN ports. */
1384 .vlan_config
= RT305X_ESW_VLAN_CONFIG_NONE
,
1385 .reg_initval_fct2
= 0x00d6500c,
1387 * ext phy base addr 31, enable port 5 polling, rx/tx clock skew 1,
1388 * turbo mii off, rgmi 3.3v off
1390 * port6: enabled, gige, full-duplex, rx/tx-flow-control
1392 .reg_initval_fpa2
= 0x3f502b28,
1395 static const struct of_device_id ralink_esw_match
[] = {
1396 { .compatible
= "ralink,rt3050-esw", .data
= &rt3050_esw_data
},
1399 MODULE_DEVICE_TABLE(of
, ralink_esw_match
);
1401 static int esw_probe(struct platform_device
*pdev
)
1403 struct device_node
*np
= pdev
->dev
.of_node
;
1404 const struct rt305x_esw_platform_data
*pdata
;
1405 const __be32
*port_map
, *reg_init
;
1406 struct rt305x_esw
*esw
;
1407 struct switch_dev
*swdev
;
1408 struct resource
*res
, *irq
;
1411 pdata
= pdev
->dev
.platform_data
;
1413 const struct of_device_id
*match
;
1414 match
= of_match_device(ralink_esw_match
, &pdev
->dev
);
1416 pdata
= (struct rt305x_esw_platform_data
*) match
->data
;
1421 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1423 dev_err(&pdev
->dev
, "no memory resource found\n");
1427 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1429 dev_err(&pdev
->dev
, "no irq resource found\n");
1433 esw
= kzalloc(sizeof(struct rt305x_esw
), GFP_KERNEL
);
1435 dev_err(&pdev
->dev
, "no memory for private data\n");
1439 esw
->dev
= &pdev
->dev
;
1440 esw
->irq
= irq
->start
;
1441 esw
->base
= ioremap(res
->start
, resource_size(res
));
1443 dev_err(&pdev
->dev
, "ioremap failed\n");
1448 port_map
= of_get_property(np
, "ralink,portmap", NULL
);
1450 esw
->port_map
= be32_to_cpu(*port_map
);
1452 reg_init
= of_get_property(np
, "ralink,fct2", NULL
);
1454 esw
->reg_initval_fct2
= be32_to_cpu(*reg_init
);
1456 reg_init
= of_get_property(np
, "ralink,fpa2", NULL
);
1458 esw
->reg_initval_fpa2
= be32_to_cpu(*reg_init
);
1460 reg_init
= of_get_property(np
, "ralink,led_polarity", NULL
);
1462 esw
->reg_led_polarity
= be32_to_cpu(*reg_init
);
1464 swdev
= &esw
->swdev
;
1465 swdev
->of_node
= pdev
->dev
.of_node
;
1466 swdev
->name
= "rt305x-esw";
1467 swdev
->alias
= "rt305x";
1468 swdev
->cpu_port
= RT305X_ESW_PORT6
;
1469 swdev
->ports
= RT305X_ESW_NUM_PORTS
;
1470 swdev
->vlans
= RT305X_ESW_NUM_VIDS
;
1471 swdev
->ops
= &esw_ops
;
1473 err
= register_switch(swdev
, NULL
);
1475 dev_err(&pdev
->dev
, "register_switch failed\n");
1479 platform_set_drvdata(pdev
, esw
);
1482 spin_lock_init(&esw
->reg_rw_lock
);
1486 esw_w32(esw
, RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_ISR
);
1487 esw_w32(esw
, ~RT305X_ESW_PORT_ST_CHG
, RT305X_ESW_REG_IMR
);
1488 request_irq(esw
->irq
, esw_interrupt
, 0, "esw", esw
);
1499 static int esw_remove(struct platform_device
*pdev
)
1501 struct rt305x_esw
*esw
;
1503 esw
= platform_get_drvdata(pdev
);
1505 unregister_switch(&esw
->swdev
);
1506 platform_set_drvdata(pdev
, NULL
);
1514 static struct platform_driver esw_driver
= {
1516 .remove
= esw_remove
,
1518 .name
= "rt305x-esw",
1519 .owner
= THIS_MODULE
,
1520 .of_match_table
= ralink_esw_match
,
1524 int __init
rtesw_init(void)
1526 return platform_driver_register(&esw_driver
);
1529 void rtesw_exit(void)
1531 platform_driver_unregister(&esw_driver
);